blob: 835b5697a455f52bd007aa41d9f444b14725fa22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * m32r_sio.c
3 *
4 * Driver for M32R serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.c.
8 *
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18/*
19 * A note about mapbase / membase
20 *
21 * mapbase is the physical address of the IO port. Currently, we don't
22 * support this very well, and it may well be dropped from this driver
23 * in future. As such, mapbase should be NULL.
24 *
25 * membase is an 'ioremapped' cookie. This is compatible with the old
26 * serial.c driver, and is currently the preferred form.
27 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020035#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
37#include <linux/init.h>
38#include <linux/console.h>
39#include <linux/sysrq.h>
40#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/delay.h>
42
43#include <asm/m32r.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define BAUD_RATE 115200
48
49#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "m32r_sio_reg.h"
51
52/*
53 * Debugging.
54 */
55#if 0
56#define DEBUG_AUTOCONF(fmt...) printk(fmt)
57#else
58#define DEBUG_AUTOCONF(fmt...) do { } while (0)
59#endif
60
61#if 0
62#define DEBUG_INTR(fmt...) printk(fmt)
63#else
64#define DEBUG_INTR(fmt...) do { } while (0)
65#endif
66
67#define PASS_LIMIT 256
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Standard COM flags */
Russell King59a675b2006-02-05 10:52:29 +000070#define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jiri Slabyd4dbe372016-01-12 10:59:06 +010072static const struct {
Jiri Slabyd4dbe372016-01-12 10:59:06 +010073 unsigned int port;
74 unsigned int irq;
Jiri Slabyd4dbe372016-01-12 10:59:06 +010075} old_serial_port[] = {
Jiri Slabyc8e7d142016-01-12 10:59:07 +010076#if defined(CONFIG_PLAT_USRV)
77 /* PORT IRQ FLAGS */
78 { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
79 { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
80#elif defined(CONFIG_SERIAL_M32R_PLDSIO)
81 { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
82#else
83 { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87#define UART_NR ARRAY_SIZE(old_serial_port)
88
89struct uart_sio_port {
90 struct uart_port port;
91 struct timer_list timer; /* "no irq" timer */
92 struct list_head list; /* ports on this IRQ */
93 unsigned short rev;
94 unsigned char acr;
95 unsigned char ier;
96 unsigned char lcr;
97 unsigned char mcr_mask; /* mask of user bits */
98 unsigned char mcr_force; /* mask of forced bits */
99 unsigned char lsr_break_flag;
100
101 /*
102 * We provide a per-port pm hook.
103 */
104 void (*pm)(struct uart_port *port,
105 unsigned int state, unsigned int old);
106};
107
108struct irq_info {
109 spinlock_t lock;
110 struct list_head *head;
111};
112
113static struct irq_info irq_lists[NR_IRQS];
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#ifdef CONFIG_SERIAL_M32R_PLDSIO
116
117#define __sio_in(x) inw((unsigned long)(x))
118#define __sio_out(v,x) outw((v),(unsigned long)(x))
119
120static inline void sio_set_baud_rate(unsigned long baud)
121{
122 unsigned short sbaud;
123 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
124 __sio_out(sbaud, PLD_ESIO0BAUR);
125}
126
127static void sio_reset(void)
128{
129 unsigned short tmp;
130
131 tmp = __sio_in(PLD_ESIO0RXB);
132 tmp = __sio_in(PLD_ESIO0RXB);
133 tmp = __sio_in(PLD_ESIO0CR);
134 sio_set_baud_rate(BAUD_RATE);
135 __sio_out(0x0300, PLD_ESIO0CR);
136 __sio_out(0x0003, PLD_ESIO0CR);
137}
138
139static void sio_init(void)
140{
141 unsigned short tmp;
142
143 tmp = __sio_in(PLD_ESIO0RXB);
144 tmp = __sio_in(PLD_ESIO0RXB);
145 tmp = __sio_in(PLD_ESIO0CR);
146 __sio_out(0x0300, PLD_ESIO0CR);
147 __sio_out(0x0003, PLD_ESIO0CR);
148}
149
150static void sio_error(int *status)
151{
152 printk("SIO0 error[%04x]\n", *status);
153 do {
154 sio_init();
155 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
156}
157
158#else /* not CONFIG_SERIAL_M32R_PLDSIO */
159
160#define __sio_in(x) inl(x)
161#define __sio_out(v,x) outl((v),(x))
162
163static inline void sio_set_baud_rate(unsigned long baud)
164{
165 unsigned long i, j;
166
167 i = boot_cpu_data.bus_clock / (baud * 16);
168 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
169 i -= 1;
170 j = (j + 1) >> 1;
171
172 __sio_out(i, M32R_SIO0_BAUR_PORTL);
173 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
174}
175
176static void sio_reset(void)
177{
178 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
179 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
180 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
181 sio_set_baud_rate(BAUD_RATE);
182 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
183 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
184}
185
186static void sio_init(void)
187{
188 unsigned int tmp;
189
190 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
191 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
192 tmp = __sio_in(M32R_SIO0_STS_PORTL);
193 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
194}
195
196static void sio_error(int *status)
197{
198 printk("SIO0 error[%04x]\n", *status);
199 do {
200 sio_init();
201 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
202}
203
204#endif /* CONFIG_SERIAL_M32R_PLDSIO */
205
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800206static unsigned int sio_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 return __sio_in(up->port.iobase + offset);
209}
210
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800211static void sio_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 __sio_out(value, up->port.iobase + offset);
214}
215
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800216static unsigned int serial_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 if (!offset)
219 return 0;
220
221 return __sio_in(offset);
222}
223
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800224static void serial_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 if (!offset)
227 return;
228
229 __sio_out(value, offset);
230}
231
Russell Kingb129a8c2005-08-31 10:12:14 +0100232static void m32r_sio_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200234 struct uart_sio_port *up =
235 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (up->ier & UART_IER_THRI) {
238 up->ier &= ~UART_IER_THRI;
239 serial_out(up, UART_IER, up->ier);
240 }
241}
242
Russell Kingb129a8c2005-08-31 10:12:14 +0100243static void m32r_sio_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245#ifdef CONFIG_SERIAL_M32R_PLDSIO
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200246 struct uart_sio_port *up =
247 container_of(port, struct uart_sio_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700248 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (!(up->ier & UART_IER_THRI)) {
251 up->ier |= UART_IER_THRI;
252 serial_out(up, UART_IER, up->ier);
Peter Hurleyc557d392014-07-06 11:29:52 -0400253 if (!uart_circ_empty(xmit)) {
254 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
255 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
256 up->port.icount.tx++;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
260#else
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200261 struct uart_sio_port *up =
262 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (!(up->ier & UART_IER_THRI)) {
265 up->ier |= UART_IER_THRI;
266 serial_out(up, UART_IER, up->ier);
267 }
268#endif
269}
270
271static void m32r_sio_stop_rx(struct uart_port *port)
272{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200273 struct uart_sio_port *up =
274 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 up->ier &= ~UART_IER_RLSI;
277 up->port.read_status_mask &= ~UART_LSR_DR;
278 serial_out(up, UART_IER, up->ier);
279}
280
281static void m32r_sio_enable_ms(struct uart_port *port)
282{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200283 struct uart_sio_port *up =
284 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 up->ier |= UART_IER_MSI;
287 serial_out(up, UART_IER, up->ier);
288}
289
David Howells7d12e782006-10-05 14:55:46 +0100290static void receive_chars(struct uart_sio_port *up, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100292 struct tty_port *port = &up->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned char ch;
Alan Cox33f0f882006-01-09 20:54:13 -0800294 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int max_count = 256;
296
297 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ch = sio_in(up, SIORXB);
Alan Cox33f0f882006-01-09 20:54:13 -0800299 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 up->port.icount.rx++;
301
302 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
303 UART_LSR_FE | UART_LSR_OE))) {
304 /*
305 * For statistics only
306 */
307 if (*status & UART_LSR_BI) {
308 *status &= ~(UART_LSR_FE | UART_LSR_PE);
309 up->port.icount.brk++;
310 /*
311 * We do the SysRQ and SAK checking
312 * here because otherwise the break
313 * may get masked by ignore_status_mask
314 * or read_status_mask.
315 */
316 if (uart_handle_break(&up->port))
317 goto ignore_char;
318 } else if (*status & UART_LSR_PE)
319 up->port.icount.parity++;
320 else if (*status & UART_LSR_FE)
321 up->port.icount.frame++;
322 if (*status & UART_LSR_OE)
323 up->port.icount.overrun++;
324
325 /*
326 * Mask off conditions which should be ingored.
327 */
328 *status &= up->port.read_status_mask;
329
330 if (up->port.line == up->port.cons->index) {
331 /* Recover the break flag from console xmit */
332 *status |= up->lsr_break_flag;
333 up->lsr_break_flag = 0;
334 }
335
336 if (*status & UART_LSR_BI) {
337 DEBUG_INTR("handling break....");
Alan Cox33f0f882006-01-09 20:54:13 -0800338 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800340 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800342 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
David Howells7d12e782006-10-05 14:55:46 +0100344 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800346 if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100347 tty_insert_flip_char(port, ch, flag);
Alan Cox33f0f882006-01-09 20:54:13 -0800348
349 if (*status & UART_LSR_OE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 * Overrun is special, since it's reported
352 * immediately, and doesn't affect the current
353 * character.
354 */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100355 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 ignore_char:
358 *status = serial_in(up, UART_LSR);
359 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Viresh Kumaraf89f832013-08-19 20:14:16 +0530360
361 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100362 tty_flip_buffer_push(port);
Viresh Kumaraf89f832013-08-19 20:14:16 +0530363 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800366static void transmit_chars(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700368 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int count;
370
371 if (up->port.x_char) {
372#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
373 serial_out(up, UART_TX, up->port.x_char);
374#endif
375 up->port.icount.tx++;
376 up->port.x_char = 0;
377 return;
378 }
379 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100380 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return;
382 }
383
384 count = up->port.fifosize;
385 do {
386 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
387 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
388 up->port.icount.tx++;
389 if (uart_circ_empty(xmit))
390 break;
Julia Lawall022d9172008-03-04 14:29:19 -0800391 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 } while (--count > 0);
394
395 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
396 uart_write_wakeup(&up->port);
397
398 DEBUG_INTR("THRE...");
399
400 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100401 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * This handles the interrupt from one port.
406 */
407static inline void m32r_sio_handle_port(struct uart_sio_port *up,
David Howells7d12e782006-10-05 14:55:46 +0100408 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 DEBUG_INTR("status = %x...", status);
411
412 if (status & 0x04)
David Howells7d12e782006-10-05 14:55:46 +0100413 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (status & 0x01)
415 transmit_chars(up);
416}
417
418/*
419 * This is the serial driver's interrupt routine.
420 *
421 * Arjan thinks the old way was overly complex, so it got simplified.
422 * Alan disagrees, saying that need the complexity to handle the weird
423 * nature of ISA shared interrupts. (This is a special exception.)
424 *
425 * In order to handle ISA shared interrupts properly, we need to check
426 * that all ports have been serviced, and therefore the ISA interrupt
427 * line has been de-asserted.
428 *
429 * This means we need to loop through all ports. checking that they
430 * don't have an interrupt pending.
431 */
David Howells7d12e782006-10-05 14:55:46 +0100432static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct irq_info *i = dev_id;
435 struct list_head *l, *end = NULL;
436 int pass_counter = 0;
437
438 DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
439
440#ifdef CONFIG_SERIAL_M32R_PLDSIO
441// if (irq == PLD_IRQ_SIO0_SND)
442// irq = PLD_IRQ_SIO0_RCV;
443#else
444 if (irq == M32R_IRQ_SIO0_S)
445 irq = M32R_IRQ_SIO0_R;
446#endif
447
448 spin_lock(&i->lock);
449
450 l = i->head;
451 do {
452 struct uart_sio_port *up;
453 unsigned int sts;
454
455 up = list_entry(l, struct uart_sio_port, list);
456
457 sts = sio_in(up, SIOSTS);
458 if (sts & 0x5) {
459 spin_lock(&up->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100460 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 spin_unlock(&up->port.lock);
462
463 end = NULL;
464 } else if (end == NULL)
465 end = l;
466
467 l = l->next;
468
469 if (l == i->head && pass_counter++ > PASS_LIMIT) {
470 if (sts & 0xe0)
471 sio_error(&sts);
472 break;
473 }
474 } while (l != end);
475
476 spin_unlock(&i->lock);
477
478 DEBUG_INTR("end.\n");
479
480 return IRQ_HANDLED;
481}
482
483/*
484 * To support ISA shared interrupts, we need to have one interrupt
485 * handler that ensures that the IRQ line has been deasserted
486 * before returning. Failing to do this will result in the IRQ
487 * line being stuck active, and, since ISA irqs are edge triggered,
488 * no more IRQs will be seen.
489 */
490static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
491{
492 spin_lock_irq(&i->lock);
493
494 if (!list_empty(i->head)) {
495 if (i->head == &up->list)
496 i->head = i->head->next;
497 list_del(&up->list);
498 } else {
499 BUG_ON(i->head != &up->list);
500 i->head = NULL;
501 }
502
503 spin_unlock_irq(&i->lock);
504}
505
506static int serial_link_irq_chain(struct uart_sio_port *up)
507{
508 struct irq_info *i = irq_lists + up->port.irq;
Hirokazu Takatadab8f492007-10-16 01:26:36 -0700509 int ret, irq_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 spin_lock_irq(&i->lock);
512
513 if (i->head) {
514 list_add(&up->list, i->head);
515 spin_unlock_irq(&i->lock);
516
517 ret = 0;
518 } else {
519 INIT_LIST_HEAD(&up->list);
520 i->head = &up->list;
521 spin_unlock_irq(&i->lock);
522
523 ret = request_irq(up->port.irq, m32r_sio_interrupt,
524 irq_flags, "SIO0-RX", i);
525 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
526 irq_flags, "SIO0-TX", i);
527 if (ret < 0)
528 serial_do_unlink(i, up);
529 }
530
531 return ret;
532}
533
534static void serial_unlink_irq_chain(struct uart_sio_port *up)
535{
536 struct irq_info *i = irq_lists + up->port.irq;
537
538 BUG_ON(i->head == NULL);
539
540 if (list_empty(i->head)) {
541 free_irq(up->port.irq, i);
542 free_irq(up->port.irq + 1, i);
543 }
544
545 serial_do_unlink(i, up);
546}
547
548/*
549 * This function is used to handle ports that do not have an interrupt.
550 */
551static void m32r_sio_timeout(unsigned long data)
552{
553 struct uart_sio_port *up = (struct uart_sio_port *)data;
554 unsigned int timeout;
555 unsigned int sts;
556
557 sts = sio_in(up, SIOSTS);
558 if (sts & 0x5) {
559 spin_lock(&up->port.lock);
Al Viro9c8e7f52006-10-07 16:29:18 +0100560 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_unlock(&up->port.lock);
562 }
563
564 timeout = up->port.timeout;
565 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
566 mod_timer(&up->timer, jiffies + timeout);
567}
568
569static unsigned int m32r_sio_tx_empty(struct uart_port *port)
570{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200571 struct uart_sio_port *up =
572 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 unsigned long flags;
574 unsigned int ret;
575
576 spin_lock_irqsave(&up->port.lock, flags);
577 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
578 spin_unlock_irqrestore(&up->port.lock, flags);
579
580 return ret;
581}
582
583static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
584{
585 return 0;
586}
587
588static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
589{
590
591}
592
593static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
594{
595
596}
597
598static int m32r_sio_startup(struct uart_port *port)
599{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200600 struct uart_sio_port *up =
601 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int retval;
603
604 sio_init();
605
606 /*
607 * If the "interrupt" for this port doesn't correspond with any
608 * hardware interrupt, we use a timer-based system. The original
609 * driver used to do this with IRQ0.
610 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000611 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned int timeout = up->port.timeout;
613
614 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
615
616 up->timer.data = (unsigned long)up;
617 mod_timer(&up->timer, jiffies + timeout);
618 } else {
619 retval = serial_link_irq_chain(up);
620 if (retval)
621 return retval;
622 }
623
624 /*
625 * Finally, enable interrupts. Note: Modem status interrupts
626 * are set via set_termios(), which will be occurring imminently
627 * anyway, so we don't enable them here.
628 * - M32R_SIO: 0x0c
629 * - M32R_PLDSIO: 0x04
630 */
631 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
632 sio_out(up, SIOTRCR, up->ier);
633
634 /*
635 * And clear the interrupt registers again for luck.
636 */
637 sio_reset();
638
639 return 0;
640}
641
642static void m32r_sio_shutdown(struct uart_port *port)
643{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200644 struct uart_sio_port *up =
645 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * Disable interrupts from this port
649 */
650 up->ier = 0;
651 sio_out(up, SIOTRCR, 0);
652
653 /*
654 * Disable break condition and FIFOs
655 */
656
657 sio_init();
658
Alan Coxd4e33fa2012-01-26 17:44:09 +0000659 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 del_timer_sync(&up->timer);
661 else
662 serial_unlink_irq_chain(up);
663}
664
665static unsigned int m32r_sio_get_divisor(struct uart_port *port,
666 unsigned int baud)
667{
668 return uart_get_divisor(port, baud);
669}
670
671static void m32r_sio_set_termios(struct uart_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800672 struct ktermios *termios, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200674 struct uart_sio_port *up =
675 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 unsigned char cval = 0;
677 unsigned long flags;
678 unsigned int baud, quot;
679
680 switch (termios->c_cflag & CSIZE) {
681 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100682 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100685 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100688 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
690 default:
691 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100692 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 }
695
696 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100697 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (termios->c_cflag & PARENB)
699 cval |= UART_LCR_PARITY;
700 if (!(termios->c_cflag & PARODD))
701 cval |= UART_LCR_EPAR;
702#ifdef CMSPAR
703 if (termios->c_cflag & CMSPAR)
704 cval |= UART_LCR_SPAR;
705#endif
706
707 /*
708 * Ask the core to calculate the divisor for us.
709 */
710#ifdef CONFIG_SERIAL_M32R_PLDSIO
711 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
712#else
713 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
714#endif
715 quot = m32r_sio_get_divisor(port, baud);
716
717 /*
718 * Ok, we're now changing the port state. Do it with
719 * interrupts disabled.
720 */
721 spin_lock_irqsave(&up->port.lock, flags);
722
723 sio_set_baud_rate(baud);
724
725 /*
726 * Update the per-port timeout.
727 */
728 uart_update_timeout(port, termios->c_cflag, baud);
729
730 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
731 if (termios->c_iflag & INPCK)
732 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400733 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 up->port.read_status_mask |= UART_LSR_BI;
735
736 /*
737 * Characteres to ignore
738 */
739 up->port.ignore_status_mask = 0;
740 if (termios->c_iflag & IGNPAR)
741 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
742 if (termios->c_iflag & IGNBRK) {
743 up->port.ignore_status_mask |= UART_LSR_BI;
744 /*
745 * If we're ignoring parity and break indicators,
746 * ignore overruns too (for real raw support).
747 */
748 if (termios->c_iflag & IGNPAR)
749 up->port.ignore_status_mask |= UART_LSR_OE;
750 }
751
752 /*
753 * ignore all characters if CREAD is not set
754 */
755 if ((termios->c_cflag & CREAD) == 0)
756 up->port.ignore_status_mask |= UART_LSR_DR;
757
758 /*
759 * CTS flow control flag and modem status interrupts
760 */
761 up->ier &= ~UART_IER_MSI;
762 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
763 up->ier |= UART_IER_MSI;
764
765 serial_out(up, UART_IER, up->ier);
766
767 up->lcr = cval; /* Save LCR */
768 spin_unlock_irqrestore(&up->port.lock, flags);
769}
770
771static void m32r_sio_pm(struct uart_port *port, unsigned int state,
772 unsigned int oldstate)
773{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200774 struct uart_sio_port *up =
775 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (up->pm)
778 up->pm(port, state, oldstate);
779}
780
781/*
782 * Resource handling. This is complicated by the fact that resources
783 * depend on the port type. Maybe we should be claiming the standard
784 * 8250 ports, and then trying to get other resources as necessary?
785 */
786static int
787m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
788{
789 unsigned int size = 8 << up->port.regshift;
790#ifndef CONFIG_SERIAL_M32R_PLDSIO
791 unsigned long start;
792#endif
793 int ret = 0;
794
795 switch (up->port.iotype) {
796 case UPIO_MEM:
797 if (up->port.mapbase) {
798#ifdef CONFIG_SERIAL_M32R_PLDSIO
799 *res = request_mem_region(up->port.mapbase, size, "serial");
800#else
801 start = up->port.mapbase;
802 *res = request_mem_region(start, size, "serial");
803#endif
804 if (!*res)
805 ret = -EBUSY;
806 }
807 break;
808
809 case UPIO_PORT:
810 *res = request_region(up->port.iobase, size, "serial");
811 if (!*res)
812 ret = -EBUSY;
813 break;
814 }
815 return ret;
816}
817
818static void m32r_sio_release_port(struct uart_port *port)
819{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200820 struct uart_sio_port *up =
821 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned long start, offset = 0, size = 0;
823
824 size <<= up->port.regshift;
825
826 switch (up->port.iotype) {
827 case UPIO_MEM:
828 if (up->port.mapbase) {
829 /*
830 * Unmap the area.
831 */
832 iounmap(up->port.membase);
833 up->port.membase = NULL;
834
835 start = up->port.mapbase;
836
837 if (size)
838 release_mem_region(start + offset, size);
839 release_mem_region(start, 8 << up->port.regshift);
840 }
841 break;
842
843 case UPIO_PORT:
844 start = up->port.iobase;
845
846 if (size)
847 release_region(start + offset, size);
848 release_region(start + offset, 8 << up->port.regshift);
849 break;
850
851 default:
852 break;
853 }
854}
855
856static int m32r_sio_request_port(struct uart_port *port)
857{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200858 struct uart_sio_port *up =
859 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct resource *res = NULL;
861 int ret = 0;
862
863 ret = m32r_sio_request_std_resource(up, &res);
864
865 /*
866 * If we have a mapbase, then request that as well.
867 */
868 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
Joe Perches28f65c112011-06-09 09:13:32 -0700869 int size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 up->port.membase = ioremap(up->port.mapbase, size);
872 if (!up->port.membase)
873 ret = -ENOMEM;
874 }
875
876 if (ret < 0) {
877 if (res)
878 release_resource(res);
879 }
880
881 return ret;
882}
883
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700884static void m32r_sio_config_port(struct uart_port *port, int unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200886 struct uart_sio_port *up =
887 container_of(port, struct uart_sio_port, port);
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700888 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 spin_lock_irqsave(&up->port.lock, flags);
891
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400892 up->port.fifosize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 spin_unlock_irqrestore(&up->port.lock, flags);
895}
896
897static int
898m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
899{
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400900 if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EINVAL;
902 return 0;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static struct uart_ops m32r_sio_pops = {
906 .tx_empty = m32r_sio_tx_empty,
907 .set_mctrl = m32r_sio_set_mctrl,
908 .get_mctrl = m32r_sio_get_mctrl,
909 .stop_tx = m32r_sio_stop_tx,
910 .start_tx = m32r_sio_start_tx,
911 .stop_rx = m32r_sio_stop_rx,
912 .enable_ms = m32r_sio_enable_ms,
913 .break_ctl = m32r_sio_break_ctl,
914 .startup = m32r_sio_startup,
915 .shutdown = m32r_sio_shutdown,
916 .set_termios = m32r_sio_set_termios,
917 .pm = m32r_sio_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .release_port = m32r_sio_release_port,
919 .request_port = m32r_sio_request_port,
920 .config_port = m32r_sio_config_port,
921 .verify_port = m32r_sio_verify_port,
922};
923
924static struct uart_sio_port m32r_sio_ports[UART_NR];
925
926static void __init m32r_sio_init_ports(void)
927{
928 struct uart_sio_port *up;
929 static int first = 1;
930 int i;
931
932 if (!first)
933 return;
934 first = 0;
935
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100936 for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 up->port.iobase = old_serial_port[i].port;
938 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100939 up->port.uartclk = BAUD_RATE * 16;
940 up->port.flags = STD_COM_FLAGS;
941 up->port.membase = 0;
942 up->port.iotype = 0;
943 up->port.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 up->port.ops = &m32r_sio_pops;
945 }
946}
947
948static void __init m32r_sio_register_ports(struct uart_driver *drv)
949{
950 int i;
951
952 m32r_sio_init_ports();
953
954 for (i = 0; i < UART_NR; i++) {
955 struct uart_sio_port *up = &m32r_sio_ports[i];
956
957 up->port.line = i;
958 up->port.ops = &m32r_sio_pops;
959 init_timer(&up->timer);
960 up->timer.function = m32r_sio_timeout;
961
Paul Gortmaker59087382012-01-04 15:16:24 -0500962 up->mcr_mask = ~0;
963 up->mcr_force = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 uart_add_one_port(drv, &up->port);
966 }
967}
968
969#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
970
971/*
972 * Wait for transmitter & holding register to empty
973 */
Denys Vlasenko9cdb9332015-10-27 18:46:37 +0100974static void wait_for_xmitr(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 unsigned int status, tmout = 10000;
977
978 /* Wait up to 10ms for the character(s) to be sent. */
979 do {
980 status = sio_in(up, SIOSTS);
981
982 if (--tmout == 0)
983 break;
984 udelay(1);
985 } while ((status & UART_EMPTY) != UART_EMPTY);
986
987 /* Wait up to 1s for flow control if necessary */
988 if (up->port.flags & UPF_CONS_FLOW) {
989 tmout = 1000000;
990 while (--tmout)
991 udelay(1);
992 }
993}
994
Russell Kingd3587882006-03-20 20:00:09 +0000995static void m32r_sio_console_putchar(struct uart_port *port, int ch)
996{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200997 struct uart_sio_port *up =
998 container_of(port, struct uart_sio_port, port);
Russell Kingd3587882006-03-20 20:00:09 +0000999
1000 wait_for_xmitr(up);
1001 sio_out(up, SIOTXB, ch);
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/*
1005 * Print a string to the serial port trying not to disturb
1006 * any possible real use of the port...
1007 *
1008 * The console_lock must be held when we get here.
1009 */
1010static void m32r_sio_console_write(struct console *co, const char *s,
1011 unsigned int count)
1012{
1013 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1014 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /*
1017 * First save the UER then disable the interrupts
1018 */
1019 ier = sio_in(up, SIOTRCR);
1020 sio_out(up, SIOTRCR, 0);
1021
Russell Kingd3587882006-03-20 20:00:09 +00001022 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /*
1025 * Finally, wait for transmitter to become empty
1026 * and restore the IER
1027 */
1028 wait_for_xmitr(up);
1029 sio_out(up, SIOTRCR, ier);
1030}
1031
1032static int __init m32r_sio_console_setup(struct console *co, char *options)
1033{
1034 struct uart_port *port;
1035 int baud = 9600;
1036 int bits = 8;
1037 int parity = 'n';
1038 int flow = 'n';
1039
1040 /*
1041 * Check whether an invalid uart number has been specified, and
1042 * if so, search for the first available port that does have
1043 * console support.
1044 */
1045 if (co->index >= UART_NR)
1046 co->index = 0;
1047 port = &m32r_sio_ports[co->index].port;
1048
1049 /*
1050 * Temporary fix.
1051 */
1052 spin_lock_init(&port->lock);
1053
1054 if (options)
1055 uart_parse_options(options, &baud, &parity, &bits, &flow);
1056
1057 return uart_set_options(port, co, baud, parity, bits, flow);
1058}
1059
Al Viroa828b8e2005-08-23 22:47:27 +01001060static struct uart_driver m32r_sio_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static struct console m32r_sio_console = {
1062 .name = "ttyS",
1063 .write = m32r_sio_console_write,
1064 .device = uart_console_device,
1065 .setup = m32r_sio_console_setup,
1066 .flags = CON_PRINTBUFFER,
1067 .index = -1,
1068 .data = &m32r_sio_reg,
1069};
1070
1071static int __init m32r_sio_console_init(void)
1072{
1073 sio_reset();
1074 sio_init();
1075 m32r_sio_init_ports();
1076 register_console(&m32r_sio_console);
1077 return 0;
1078}
1079console_initcall(m32r_sio_console_init);
1080
1081#define M32R_SIO_CONSOLE &m32r_sio_console
1082#else
1083#define M32R_SIO_CONSOLE NULL
1084#endif
1085
1086static struct uart_driver m32r_sio_reg = {
1087 .owner = THIS_MODULE,
1088 .driver_name = "sio",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 .dev_name = "ttyS",
1090 .major = TTY_MAJOR,
1091 .minor = 64,
1092 .nr = UART_NR,
1093 .cons = M32R_SIO_CONSOLE,
1094};
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static int __init m32r_sio_init(void)
1097{
1098 int ret, i;
1099
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001100 printk(KERN_INFO "Serial: M32R SIO driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Yinghai Lua62c4132008-08-19 20:49:55 -07001102 for (i = 0; i < nr_irqs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_lock_init(&irq_lists[i].lock);
1104
1105 ret = uart_register_driver(&m32r_sio_reg);
1106 if (ret >= 0)
1107 m32r_sio_register_ports(&m32r_sio_reg);
1108
1109 return ret;
1110}
1111
1112static void __exit m32r_sio_exit(void)
1113{
1114 int i;
1115
1116 for (i = 0; i < UART_NR; i++)
1117 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1118
1119 uart_unregister_driver(&m32r_sio_reg);
1120}
1121
1122module_init(m32r_sio_init);
1123module_exit(m32r_sio_exit);
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001126MODULE_DESCRIPTION("Generic M32R SIO serial driver");