blob: 7b83a8aab4954b0ce31d7d9a282b7f7fc7863ed7 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * m32r_sio.c
4 *
5 * Driver for M32R serial ports
6 *
7 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Based on drivers/serial/8250.c.
9 *
10 * Copyright (C) 2001 Russell King.
11 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14/*
15 * A note about mapbase / membase
16 *
17 * mapbase is the physical address of the IO port. Currently, we don't
18 * support this very well, and it may well be dropped from this driver
19 * in future. As such, mapbase should be NULL.
20 *
21 * membase is an 'ioremapped' cookie. This is compatible with the old
22 * serial.c driver, and is currently the preferred form.
23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
26#define SUPPORT_SYSRQ
27#endif
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020030#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37
38#include <asm/m32r.h>
39#include <asm/io.h>
40#include <asm/irq.h>
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define BAUD_RATE 115200
43
44#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "m32r_sio_reg.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define PASS_LIMIT 256
48
Jiri Slabyd4dbe372016-01-12 10:59:06 +010049static const struct {
Jiri Slabyd4dbe372016-01-12 10:59:06 +010050 unsigned int port;
51 unsigned int irq;
Jiri Slabyd4dbe372016-01-12 10:59:06 +010052} old_serial_port[] = {
Jiri Slabyc8e7d142016-01-12 10:59:07 +010053#if defined(CONFIG_PLAT_USRV)
54 /* PORT IRQ FLAGS */
55 { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
56 { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
57#elif defined(CONFIG_SERIAL_M32R_PLDSIO)
58 { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
59#else
60 { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
61#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#define UART_NR ARRAY_SIZE(old_serial_port)
65
66struct uart_sio_port {
67 struct uart_port port;
68 struct timer_list timer; /* "no irq" timer */
69 struct list_head list; /* ports on this IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned char ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73struct irq_info {
74 spinlock_t lock;
75 struct list_head *head;
76};
77
78static struct irq_info irq_lists[NR_IRQS];
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef CONFIG_SERIAL_M32R_PLDSIO
81
82#define __sio_in(x) inw((unsigned long)(x))
83#define __sio_out(v,x) outw((v),(unsigned long)(x))
84
85static inline void sio_set_baud_rate(unsigned long baud)
86{
87 unsigned short sbaud;
88 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
89 __sio_out(sbaud, PLD_ESIO0BAUR);
90}
91
92static void sio_reset(void)
93{
94 unsigned short tmp;
95
96 tmp = __sio_in(PLD_ESIO0RXB);
97 tmp = __sio_in(PLD_ESIO0RXB);
98 tmp = __sio_in(PLD_ESIO0CR);
99 sio_set_baud_rate(BAUD_RATE);
100 __sio_out(0x0300, PLD_ESIO0CR);
101 __sio_out(0x0003, PLD_ESIO0CR);
102}
103
104static void sio_init(void)
105{
106 unsigned short tmp;
107
108 tmp = __sio_in(PLD_ESIO0RXB);
109 tmp = __sio_in(PLD_ESIO0RXB);
110 tmp = __sio_in(PLD_ESIO0CR);
111 __sio_out(0x0300, PLD_ESIO0CR);
112 __sio_out(0x0003, PLD_ESIO0CR);
113}
114
115static void sio_error(int *status)
116{
117 printk("SIO0 error[%04x]\n", *status);
118 do {
119 sio_init();
120 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
121}
122
123#else /* not CONFIG_SERIAL_M32R_PLDSIO */
124
125#define __sio_in(x) inl(x)
126#define __sio_out(v,x) outl((v),(x))
127
128static inline void sio_set_baud_rate(unsigned long baud)
129{
130 unsigned long i, j;
131
132 i = boot_cpu_data.bus_clock / (baud * 16);
133 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
134 i -= 1;
135 j = (j + 1) >> 1;
136
137 __sio_out(i, M32R_SIO0_BAUR_PORTL);
138 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
139}
140
141static void sio_reset(void)
142{
143 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
144 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
145 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
146 sio_set_baud_rate(BAUD_RATE);
147 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
148 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
149}
150
151static void sio_init(void)
152{
153 unsigned int tmp;
154
155 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
156 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
157 tmp = __sio_in(M32R_SIO0_STS_PORTL);
158 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
159}
160
161static void sio_error(int *status)
162{
163 printk("SIO0 error[%04x]\n", *status);
164 do {
165 sio_init();
166 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
167}
168
169#endif /* CONFIG_SERIAL_M32R_PLDSIO */
170
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800171static unsigned int sio_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 return __sio_in(up->port.iobase + offset);
174}
175
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800176static void sio_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 __sio_out(value, up->port.iobase + offset);
179}
180
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800181static unsigned int serial_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 if (!offset)
184 return 0;
185
186 return __sio_in(offset);
187}
188
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800189static void serial_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 if (!offset)
192 return;
193
194 __sio_out(value, offset);
195}
196
Russell Kingb129a8c2005-08-31 10:12:14 +0100197static void m32r_sio_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200199 struct uart_sio_port *up =
200 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (up->ier & UART_IER_THRI) {
203 up->ier &= ~UART_IER_THRI;
204 serial_out(up, UART_IER, up->ier);
205 }
206}
207
Russell Kingb129a8c2005-08-31 10:12:14 +0100208static void m32r_sio_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210#ifdef CONFIG_SERIAL_M32R_PLDSIO
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200211 struct uart_sio_port *up =
212 container_of(port, struct uart_sio_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700213 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if (!(up->ier & UART_IER_THRI)) {
216 up->ier |= UART_IER_THRI;
217 serial_out(up, UART_IER, up->ier);
Peter Hurleyc557d392014-07-06 11:29:52 -0400218 if (!uart_circ_empty(xmit)) {
219 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
220 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
221 up->port.icount.tx++;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
225#else
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200226 struct uart_sio_port *up =
227 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (!(up->ier & UART_IER_THRI)) {
230 up->ier |= UART_IER_THRI;
231 serial_out(up, UART_IER, up->ier);
232 }
233#endif
234}
235
236static void m32r_sio_stop_rx(struct uart_port *port)
237{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200238 struct uart_sio_port *up =
239 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 up->ier &= ~UART_IER_RLSI;
242 up->port.read_status_mask &= ~UART_LSR_DR;
243 serial_out(up, UART_IER, up->ier);
244}
245
246static void m32r_sio_enable_ms(struct uart_port *port)
247{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200248 struct uart_sio_port *up =
249 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 up->ier |= UART_IER_MSI;
252 serial_out(up, UART_IER, up->ier);
253}
254
David Howells7d12e782006-10-05 14:55:46 +0100255static void receive_chars(struct uart_sio_port *up, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100257 struct tty_port *port = &up->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned char ch;
Alan Cox33f0f882006-01-09 20:54:13 -0800259 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int max_count = 256;
261
262 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ch = sio_in(up, SIORXB);
Alan Cox33f0f882006-01-09 20:54:13 -0800264 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 up->port.icount.rx++;
266
267 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
268 UART_LSR_FE | UART_LSR_OE))) {
269 /*
270 * For statistics only
271 */
272 if (*status & UART_LSR_BI) {
273 *status &= ~(UART_LSR_FE | UART_LSR_PE);
274 up->port.icount.brk++;
275 /*
276 * We do the SysRQ and SAK checking
277 * here because otherwise the break
278 * may get masked by ignore_status_mask
279 * or read_status_mask.
280 */
281 if (uart_handle_break(&up->port))
282 goto ignore_char;
283 } else if (*status & UART_LSR_PE)
284 up->port.icount.parity++;
285 else if (*status & UART_LSR_FE)
286 up->port.icount.frame++;
287 if (*status & UART_LSR_OE)
288 up->port.icount.overrun++;
289
290 /*
291 * Mask off conditions which should be ingored.
292 */
293 *status &= up->port.read_status_mask;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (*status & UART_LSR_BI) {
Jiri Slabyad245aa2016-01-12 10:59:08 +0100296 pr_debug("handling break....\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800297 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800299 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800301 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
David Howells7d12e782006-10-05 14:55:46 +0100303 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800305 if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100306 tty_insert_flip_char(port, ch, flag);
Alan Cox33f0f882006-01-09 20:54:13 -0800307
308 if (*status & UART_LSR_OE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /*
310 * Overrun is special, since it's reported
311 * immediately, and doesn't affect the current
312 * character.
313 */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100314 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 ignore_char:
317 *status = serial_in(up, UART_LSR);
318 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Viresh Kumaraf89f832013-08-19 20:14:16 +0530319
320 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100321 tty_flip_buffer_push(port);
Viresh Kumaraf89f832013-08-19 20:14:16 +0530322 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800325static void transmit_chars(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700327 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int count;
329
330 if (up->port.x_char) {
331#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
332 serial_out(up, UART_TX, up->port.x_char);
333#endif
334 up->port.icount.tx++;
335 up->port.x_char = 0;
336 return;
337 }
338 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100339 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return;
341 }
342
343 count = up->port.fifosize;
344 do {
345 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
346 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
347 up->port.icount.tx++;
348 if (uart_circ_empty(xmit))
349 break;
Julia Lawall022d9172008-03-04 14:29:19 -0800350 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 } while (--count > 0);
353
354 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
355 uart_write_wakeup(&up->port);
356
Jiri Slabyad245aa2016-01-12 10:59:08 +0100357 pr_debug("THRE...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100360 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
364 * This handles the interrupt from one port.
365 */
366static inline void m32r_sio_handle_port(struct uart_sio_port *up,
David Howells7d12e782006-10-05 14:55:46 +0100367 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jiri Slabyad245aa2016-01-12 10:59:08 +0100369 pr_debug("status = %x...\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (status & 0x04)
David Howells7d12e782006-10-05 14:55:46 +0100372 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (status & 0x01)
374 transmit_chars(up);
375}
376
377/*
378 * This is the serial driver's interrupt routine.
379 *
380 * Arjan thinks the old way was overly complex, so it got simplified.
381 * Alan disagrees, saying that need the complexity to handle the weird
382 * nature of ISA shared interrupts. (This is a special exception.)
383 *
384 * In order to handle ISA shared interrupts properly, we need to check
385 * that all ports have been serviced, and therefore the ISA interrupt
386 * line has been de-asserted.
387 *
388 * This means we need to loop through all ports. checking that they
389 * don't have an interrupt pending.
390 */
David Howells7d12e782006-10-05 14:55:46 +0100391static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct irq_info *i = dev_id;
394 struct list_head *l, *end = NULL;
395 int pass_counter = 0;
396
Jiri Slabyad245aa2016-01-12 10:59:08 +0100397 pr_debug("m32r_sio_interrupt(%d)...\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399#ifdef CONFIG_SERIAL_M32R_PLDSIO
400// if (irq == PLD_IRQ_SIO0_SND)
401// irq = PLD_IRQ_SIO0_RCV;
402#else
403 if (irq == M32R_IRQ_SIO0_S)
404 irq = M32R_IRQ_SIO0_R;
405#endif
406
407 spin_lock(&i->lock);
408
409 l = i->head;
410 do {
411 struct uart_sio_port *up;
412 unsigned int sts;
413
414 up = list_entry(l, struct uart_sio_port, list);
415
416 sts = sio_in(up, SIOSTS);
417 if (sts & 0x5) {
418 spin_lock(&up->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100419 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_unlock(&up->port.lock);
421
422 end = NULL;
423 } else if (end == NULL)
424 end = l;
425
426 l = l->next;
427
428 if (l == i->head && pass_counter++ > PASS_LIMIT) {
429 if (sts & 0xe0)
430 sio_error(&sts);
431 break;
432 }
433 } while (l != end);
434
435 spin_unlock(&i->lock);
436
Jiri Slabyad245aa2016-01-12 10:59:08 +0100437 pr_debug("end.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return IRQ_HANDLED;
440}
441
442/*
443 * To support ISA shared interrupts, we need to have one interrupt
444 * handler that ensures that the IRQ line has been deasserted
445 * before returning. Failing to do this will result in the IRQ
446 * line being stuck active, and, since ISA irqs are edge triggered,
447 * no more IRQs will be seen.
448 */
449static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
450{
451 spin_lock_irq(&i->lock);
452
453 if (!list_empty(i->head)) {
454 if (i->head == &up->list)
455 i->head = i->head->next;
456 list_del(&up->list);
457 } else {
458 BUG_ON(i->head != &up->list);
459 i->head = NULL;
460 }
461
462 spin_unlock_irq(&i->lock);
463}
464
465static int serial_link_irq_chain(struct uart_sio_port *up)
466{
467 struct irq_info *i = irq_lists + up->port.irq;
Hirokazu Takatadab8f492007-10-16 01:26:36 -0700468 int ret, irq_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 spin_lock_irq(&i->lock);
471
472 if (i->head) {
473 list_add(&up->list, i->head);
474 spin_unlock_irq(&i->lock);
475
476 ret = 0;
477 } else {
478 INIT_LIST_HEAD(&up->list);
479 i->head = &up->list;
480 spin_unlock_irq(&i->lock);
481
482 ret = request_irq(up->port.irq, m32r_sio_interrupt,
483 irq_flags, "SIO0-RX", i);
484 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
485 irq_flags, "SIO0-TX", i);
486 if (ret < 0)
487 serial_do_unlink(i, up);
488 }
489
490 return ret;
491}
492
493static void serial_unlink_irq_chain(struct uart_sio_port *up)
494{
495 struct irq_info *i = irq_lists + up->port.irq;
496
497 BUG_ON(i->head == NULL);
498
499 if (list_empty(i->head)) {
500 free_irq(up->port.irq, i);
501 free_irq(up->port.irq + 1, i);
502 }
503
504 serial_do_unlink(i, up);
505}
506
507/*
508 * This function is used to handle ports that do not have an interrupt.
509 */
Kees Cookf0f62c62017-10-16 16:27:37 -0700510static void m32r_sio_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Kees Cookf0f62c62017-10-16 16:27:37 -0700512 struct uart_sio_port *up = from_timer(up, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unsigned int timeout;
514 unsigned int sts;
515
516 sts = sio_in(up, SIOSTS);
517 if (sts & 0x5) {
518 spin_lock(&up->port.lock);
Al Viro9c8e7f52006-10-07 16:29:18 +0100519 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spin_unlock(&up->port.lock);
521 }
522
523 timeout = up->port.timeout;
524 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
525 mod_timer(&up->timer, jiffies + timeout);
526}
527
528static unsigned int m32r_sio_tx_empty(struct uart_port *port)
529{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200530 struct uart_sio_port *up =
531 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned long flags;
533 unsigned int ret;
534
535 spin_lock_irqsave(&up->port.lock, flags);
536 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
537 spin_unlock_irqrestore(&up->port.lock, flags);
538
539 return ret;
540}
541
542static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
543{
544 return 0;
545}
546
547static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
548{
549
550}
551
552static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
553{
554
555}
556
557static int m32r_sio_startup(struct uart_port *port)
558{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200559 struct uart_sio_port *up =
560 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int retval;
562
563 sio_init();
564
565 /*
566 * If the "interrupt" for this port doesn't correspond with any
567 * hardware interrupt, we use a timer-based system. The original
568 * driver used to do this with IRQ0.
569 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000570 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned int timeout = up->port.timeout;
572
573 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mod_timer(&up->timer, jiffies + timeout);
576 } else {
577 retval = serial_link_irq_chain(up);
578 if (retval)
579 return retval;
580 }
581
582 /*
583 * Finally, enable interrupts. Note: Modem status interrupts
584 * are set via set_termios(), which will be occurring imminently
585 * anyway, so we don't enable them here.
586 * - M32R_SIO: 0x0c
587 * - M32R_PLDSIO: 0x04
588 */
589 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
590 sio_out(up, SIOTRCR, up->ier);
591
592 /*
593 * And clear the interrupt registers again for luck.
594 */
595 sio_reset();
596
597 return 0;
598}
599
600static void m32r_sio_shutdown(struct uart_port *port)
601{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200602 struct uart_sio_port *up =
603 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /*
606 * Disable interrupts from this port
607 */
608 up->ier = 0;
609 sio_out(up, SIOTRCR, 0);
610
611 /*
612 * Disable break condition and FIFOs
613 */
614
615 sio_init();
616
Alan Coxd4e33fa2012-01-26 17:44:09 +0000617 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 del_timer_sync(&up->timer);
619 else
620 serial_unlink_irq_chain(up);
621}
622
623static unsigned int m32r_sio_get_divisor(struct uart_port *port,
624 unsigned int baud)
625{
626 return uart_get_divisor(port, baud);
627}
628
629static void m32r_sio_set_termios(struct uart_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800630 struct ktermios *termios, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200632 struct uart_sio_port *up =
633 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unsigned char cval = 0;
635 unsigned long flags;
636 unsigned int baud, quot;
637
638 switch (termios->c_cflag & CSIZE) {
639 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100640 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100643 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100646 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 default:
649 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100650 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 }
653
654 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100655 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (termios->c_cflag & PARENB)
657 cval |= UART_LCR_PARITY;
658 if (!(termios->c_cflag & PARODD))
659 cval |= UART_LCR_EPAR;
660#ifdef CMSPAR
661 if (termios->c_cflag & CMSPAR)
662 cval |= UART_LCR_SPAR;
663#endif
664
665 /*
666 * Ask the core to calculate the divisor for us.
667 */
668#ifdef CONFIG_SERIAL_M32R_PLDSIO
669 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
670#else
671 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
672#endif
673 quot = m32r_sio_get_divisor(port, baud);
674
675 /*
676 * Ok, we're now changing the port state. Do it with
677 * interrupts disabled.
678 */
679 spin_lock_irqsave(&up->port.lock, flags);
680
681 sio_set_baud_rate(baud);
682
683 /*
684 * Update the per-port timeout.
685 */
686 uart_update_timeout(port, termios->c_cflag, baud);
687
688 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
689 if (termios->c_iflag & INPCK)
690 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400691 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 up->port.read_status_mask |= UART_LSR_BI;
693
694 /*
695 * Characteres to ignore
696 */
697 up->port.ignore_status_mask = 0;
698 if (termios->c_iflag & IGNPAR)
699 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
700 if (termios->c_iflag & IGNBRK) {
701 up->port.ignore_status_mask |= UART_LSR_BI;
702 /*
703 * If we're ignoring parity and break indicators,
704 * ignore overruns too (for real raw support).
705 */
706 if (termios->c_iflag & IGNPAR)
707 up->port.ignore_status_mask |= UART_LSR_OE;
708 }
709
710 /*
711 * ignore all characters if CREAD is not set
712 */
713 if ((termios->c_cflag & CREAD) == 0)
714 up->port.ignore_status_mask |= UART_LSR_DR;
715
716 /*
717 * CTS flow control flag and modem status interrupts
718 */
719 up->ier &= ~UART_IER_MSI;
720 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
721 up->ier |= UART_IER_MSI;
722
723 serial_out(up, UART_IER, up->ier);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 spin_unlock_irqrestore(&up->port.lock, flags);
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/*
729 * Resource handling. This is complicated by the fact that resources
730 * depend on the port type. Maybe we should be claiming the standard
731 * 8250 ports, and then trying to get other resources as necessary?
732 */
733static int
734m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
735{
736 unsigned int size = 8 << up->port.regshift;
737#ifndef CONFIG_SERIAL_M32R_PLDSIO
738 unsigned long start;
739#endif
740 int ret = 0;
741
742 switch (up->port.iotype) {
743 case UPIO_MEM:
744 if (up->port.mapbase) {
745#ifdef CONFIG_SERIAL_M32R_PLDSIO
746 *res = request_mem_region(up->port.mapbase, size, "serial");
747#else
748 start = up->port.mapbase;
749 *res = request_mem_region(start, size, "serial");
750#endif
751 if (!*res)
752 ret = -EBUSY;
753 }
754 break;
755
756 case UPIO_PORT:
757 *res = request_region(up->port.iobase, size, "serial");
758 if (!*res)
759 ret = -EBUSY;
760 break;
761 }
762 return ret;
763}
764
765static void m32r_sio_release_port(struct uart_port *port)
766{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200767 struct uart_sio_port *up =
768 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 unsigned long start, offset = 0, size = 0;
770
771 size <<= up->port.regshift;
772
773 switch (up->port.iotype) {
774 case UPIO_MEM:
775 if (up->port.mapbase) {
776 /*
777 * Unmap the area.
778 */
779 iounmap(up->port.membase);
780 up->port.membase = NULL;
781
782 start = up->port.mapbase;
783
784 if (size)
785 release_mem_region(start + offset, size);
786 release_mem_region(start, 8 << up->port.regshift);
787 }
788 break;
789
790 case UPIO_PORT:
791 start = up->port.iobase;
792
793 if (size)
794 release_region(start + offset, size);
795 release_region(start + offset, 8 << up->port.regshift);
796 break;
797
798 default:
799 break;
800 }
801}
802
803static int m32r_sio_request_port(struct uart_port *port)
804{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200805 struct uart_sio_port *up =
806 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct resource *res = NULL;
808 int ret = 0;
809
810 ret = m32r_sio_request_std_resource(up, &res);
811
812 /*
813 * If we have a mapbase, then request that as well.
814 */
815 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
Joe Perches28f65c112011-06-09 09:13:32 -0700816 int size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 up->port.membase = ioremap(up->port.mapbase, size);
819 if (!up->port.membase)
820 ret = -ENOMEM;
821 }
822
823 if (ret < 0) {
824 if (res)
825 release_resource(res);
826 }
827
828 return ret;
829}
830
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700831static void m32r_sio_config_port(struct uart_port *port, int unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200833 struct uart_sio_port *up =
834 container_of(port, struct uart_sio_port, port);
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700835 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 spin_lock_irqsave(&up->port.lock, flags);
838
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400839 up->port.fifosize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 spin_unlock_irqrestore(&up->port.lock, flags);
842}
843
844static int
845m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
846{
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400847 if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EINVAL;
849 return 0;
850}
851
Julia Lawallcdb93942017-08-13 08:21:48 +0200852static const struct uart_ops m32r_sio_pops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 .tx_empty = m32r_sio_tx_empty,
854 .set_mctrl = m32r_sio_set_mctrl,
855 .get_mctrl = m32r_sio_get_mctrl,
856 .stop_tx = m32r_sio_stop_tx,
857 .start_tx = m32r_sio_start_tx,
858 .stop_rx = m32r_sio_stop_rx,
859 .enable_ms = m32r_sio_enable_ms,
860 .break_ctl = m32r_sio_break_ctl,
861 .startup = m32r_sio_startup,
862 .shutdown = m32r_sio_shutdown,
863 .set_termios = m32r_sio_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 .release_port = m32r_sio_release_port,
865 .request_port = m32r_sio_request_port,
866 .config_port = m32r_sio_config_port,
867 .verify_port = m32r_sio_verify_port,
868};
869
870static struct uart_sio_port m32r_sio_ports[UART_NR];
871
872static void __init m32r_sio_init_ports(void)
873{
874 struct uart_sio_port *up;
875 static int first = 1;
876 int i;
877
878 if (!first)
879 return;
880 first = 0;
881
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100882 for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 up->port.iobase = old_serial_port[i].port;
884 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100885 up->port.uartclk = BAUD_RATE * 16;
Jiri Slaby6137b7a2016-05-09 09:11:57 +0200886 up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST;
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100887 up->port.membase = 0;
888 up->port.iotype = 0;
889 up->port.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 up->port.ops = &m32r_sio_pops;
891 }
892}
893
894static void __init m32r_sio_register_ports(struct uart_driver *drv)
895{
896 int i;
897
898 m32r_sio_init_ports();
899
900 for (i = 0; i < UART_NR; i++) {
901 struct uart_sio_port *up = &m32r_sio_ports[i];
902
903 up->port.line = i;
904 up->port.ops = &m32r_sio_pops;
Kees Cookf0f62c62017-10-16 16:27:37 -0700905 timer_setup(&up->timer, m32r_sio_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 uart_add_one_port(drv, &up->port);
908 }
909}
910
911#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
912
913/*
914 * Wait for transmitter & holding register to empty
915 */
Denys Vlasenko9cdb9332015-10-27 18:46:37 +0100916static void wait_for_xmitr(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 unsigned int status, tmout = 10000;
919
920 /* Wait up to 10ms for the character(s) to be sent. */
921 do {
922 status = sio_in(up, SIOSTS);
923
924 if (--tmout == 0)
925 break;
926 udelay(1);
927 } while ((status & UART_EMPTY) != UART_EMPTY);
928
929 /* Wait up to 1s for flow control if necessary */
930 if (up->port.flags & UPF_CONS_FLOW) {
931 tmout = 1000000;
932 while (--tmout)
933 udelay(1);
934 }
935}
936
Russell Kingd3587882006-03-20 20:00:09 +0000937static void m32r_sio_console_putchar(struct uart_port *port, int ch)
938{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200939 struct uart_sio_port *up =
940 container_of(port, struct uart_sio_port, port);
Russell Kingd3587882006-03-20 20:00:09 +0000941
942 wait_for_xmitr(up);
943 sio_out(up, SIOTXB, ch);
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * Print a string to the serial port trying not to disturb
948 * any possible real use of the port...
949 *
950 * The console_lock must be held when we get here.
951 */
952static void m32r_sio_console_write(struct console *co, const char *s,
953 unsigned int count)
954{
955 struct uart_sio_port *up = &m32r_sio_ports[co->index];
956 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /*
959 * First save the UER then disable the interrupts
960 */
961 ier = sio_in(up, SIOTRCR);
962 sio_out(up, SIOTRCR, 0);
963
Russell Kingd3587882006-03-20 20:00:09 +0000964 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /*
967 * Finally, wait for transmitter to become empty
968 * and restore the IER
969 */
970 wait_for_xmitr(up);
971 sio_out(up, SIOTRCR, ier);
972}
973
974static int __init m32r_sio_console_setup(struct console *co, char *options)
975{
976 struct uart_port *port;
977 int baud = 9600;
978 int bits = 8;
979 int parity = 'n';
980 int flow = 'n';
981
982 /*
983 * Check whether an invalid uart number has been specified, and
984 * if so, search for the first available port that does have
985 * console support.
986 */
987 if (co->index >= UART_NR)
988 co->index = 0;
989 port = &m32r_sio_ports[co->index].port;
990
991 /*
992 * Temporary fix.
993 */
994 spin_lock_init(&port->lock);
995
996 if (options)
997 uart_parse_options(options, &baud, &parity, &bits, &flow);
998
999 return uart_set_options(port, co, baud, parity, bits, flow);
1000}
1001
Al Viroa828b8e2005-08-23 22:47:27 +01001002static struct uart_driver m32r_sio_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003static struct console m32r_sio_console = {
1004 .name = "ttyS",
1005 .write = m32r_sio_console_write,
1006 .device = uart_console_device,
1007 .setup = m32r_sio_console_setup,
1008 .flags = CON_PRINTBUFFER,
1009 .index = -1,
1010 .data = &m32r_sio_reg,
1011};
1012
1013static int __init m32r_sio_console_init(void)
1014{
1015 sio_reset();
1016 sio_init();
1017 m32r_sio_init_ports();
1018 register_console(&m32r_sio_console);
1019 return 0;
1020}
1021console_initcall(m32r_sio_console_init);
1022
1023#define M32R_SIO_CONSOLE &m32r_sio_console
1024#else
1025#define M32R_SIO_CONSOLE NULL
1026#endif
1027
1028static struct uart_driver m32r_sio_reg = {
1029 .owner = THIS_MODULE,
1030 .driver_name = "sio",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 .dev_name = "ttyS",
1032 .major = TTY_MAJOR,
1033 .minor = 64,
1034 .nr = UART_NR,
1035 .cons = M32R_SIO_CONSOLE,
1036};
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static int __init m32r_sio_init(void)
1039{
1040 int ret, i;
1041
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001042 printk(KERN_INFO "Serial: M32R SIO driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Yinghai Lua62c4132008-08-19 20:49:55 -07001044 for (i = 0; i < nr_irqs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_lock_init(&irq_lists[i].lock);
1046
1047 ret = uart_register_driver(&m32r_sio_reg);
1048 if (ret >= 0)
1049 m32r_sio_register_ports(&m32r_sio_reg);
1050
1051 return ret;
1052}
Paul Gortmakera10aebe2016-06-20 18:55:05 -04001053device_initcall(m32r_sio_init);