blob: 0bc548adae52f2617a0a6260a4ea9465675dfc5e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020034#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/delay.h>
41
42#include <asm/m32r.h>
43#include <asm/io.h>
44#include <asm/irq.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define BAUD_RATE 115200
47
48#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "m32r_sio_reg.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define PASS_LIMIT 256
52
Jiri Slabyd4dbe372016-01-12 10:59:06 +010053static const struct {
Jiri Slabyd4dbe372016-01-12 10:59:06 +010054 unsigned int port;
55 unsigned int irq;
Jiri Slabyd4dbe372016-01-12 10:59:06 +010056} old_serial_port[] = {
Jiri Slabyc8e7d142016-01-12 10:59:07 +010057#if defined(CONFIG_PLAT_USRV)
58 /* PORT IRQ FLAGS */
59 { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
60 { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
61#elif defined(CONFIG_SERIAL_M32R_PLDSIO)
62 { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
63#else
64 { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
65#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68#define UART_NR ARRAY_SIZE(old_serial_port)
69
70struct uart_sio_port {
71 struct uart_port port;
72 struct timer_list timer; /* "no irq" timer */
73 struct list_head list; /* ports on this IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unsigned char ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
77struct irq_info {
78 spinlock_t lock;
79 struct list_head *head;
80};
81
82static struct irq_info irq_lists[NR_IRQS];
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#ifdef CONFIG_SERIAL_M32R_PLDSIO
85
86#define __sio_in(x) inw((unsigned long)(x))
87#define __sio_out(v,x) outw((v),(unsigned long)(x))
88
89static inline void sio_set_baud_rate(unsigned long baud)
90{
91 unsigned short sbaud;
92 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
93 __sio_out(sbaud, PLD_ESIO0BAUR);
94}
95
96static void sio_reset(void)
97{
98 unsigned short tmp;
99
100 tmp = __sio_in(PLD_ESIO0RXB);
101 tmp = __sio_in(PLD_ESIO0RXB);
102 tmp = __sio_in(PLD_ESIO0CR);
103 sio_set_baud_rate(BAUD_RATE);
104 __sio_out(0x0300, PLD_ESIO0CR);
105 __sio_out(0x0003, PLD_ESIO0CR);
106}
107
108static void sio_init(void)
109{
110 unsigned short tmp;
111
112 tmp = __sio_in(PLD_ESIO0RXB);
113 tmp = __sio_in(PLD_ESIO0RXB);
114 tmp = __sio_in(PLD_ESIO0CR);
115 __sio_out(0x0300, PLD_ESIO0CR);
116 __sio_out(0x0003, PLD_ESIO0CR);
117}
118
119static void sio_error(int *status)
120{
121 printk("SIO0 error[%04x]\n", *status);
122 do {
123 sio_init();
124 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
125}
126
127#else /* not CONFIG_SERIAL_M32R_PLDSIO */
128
129#define __sio_in(x) inl(x)
130#define __sio_out(v,x) outl((v),(x))
131
132static inline void sio_set_baud_rate(unsigned long baud)
133{
134 unsigned long i, j;
135
136 i = boot_cpu_data.bus_clock / (baud * 16);
137 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
138 i -= 1;
139 j = (j + 1) >> 1;
140
141 __sio_out(i, M32R_SIO0_BAUR_PORTL);
142 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
143}
144
145static void sio_reset(void)
146{
147 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
148 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
149 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
150 sio_set_baud_rate(BAUD_RATE);
151 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
152 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
153}
154
155static void sio_init(void)
156{
157 unsigned int tmp;
158
159 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
160 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
161 tmp = __sio_in(M32R_SIO0_STS_PORTL);
162 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
163}
164
165static void sio_error(int *status)
166{
167 printk("SIO0 error[%04x]\n", *status);
168 do {
169 sio_init();
170 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
171}
172
173#endif /* CONFIG_SERIAL_M32R_PLDSIO */
174
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800175static unsigned int sio_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 return __sio_in(up->port.iobase + offset);
178}
179
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800180static void sio_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 __sio_out(value, up->port.iobase + offset);
183}
184
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800185static unsigned int serial_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 if (!offset)
188 return 0;
189
190 return __sio_in(offset);
191}
192
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800193static void serial_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (!offset)
196 return;
197
198 __sio_out(value, offset);
199}
200
Russell Kingb129a8c2005-08-31 10:12:14 +0100201static void m32r_sio_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200203 struct uart_sio_port *up =
204 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (up->ier & UART_IER_THRI) {
207 up->ier &= ~UART_IER_THRI;
208 serial_out(up, UART_IER, up->ier);
209 }
210}
211
Russell Kingb129a8c2005-08-31 10:12:14 +0100212static void m32r_sio_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214#ifdef CONFIG_SERIAL_M32R_PLDSIO
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200215 struct uart_sio_port *up =
216 container_of(port, struct uart_sio_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700217 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!(up->ier & UART_IER_THRI)) {
220 up->ier |= UART_IER_THRI;
221 serial_out(up, UART_IER, up->ier);
Peter Hurleyc557d392014-07-06 11:29:52 -0400222 if (!uart_circ_empty(xmit)) {
223 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
224 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
225 up->port.icount.tx++;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
229#else
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200230 struct uart_sio_port *up =
231 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!(up->ier & UART_IER_THRI)) {
234 up->ier |= UART_IER_THRI;
235 serial_out(up, UART_IER, up->ier);
236 }
237#endif
238}
239
240static void m32r_sio_stop_rx(struct uart_port *port)
241{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200242 struct uart_sio_port *up =
243 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 up->ier &= ~UART_IER_RLSI;
246 up->port.read_status_mask &= ~UART_LSR_DR;
247 serial_out(up, UART_IER, up->ier);
248}
249
250static void m32r_sio_enable_ms(struct uart_port *port)
251{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200252 struct uart_sio_port *up =
253 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 up->ier |= UART_IER_MSI;
256 serial_out(up, UART_IER, up->ier);
257}
258
David Howells7d12e782006-10-05 14:55:46 +0100259static void receive_chars(struct uart_sio_port *up, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100261 struct tty_port *port = &up->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned char ch;
Alan Cox33f0f882006-01-09 20:54:13 -0800263 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 int max_count = 256;
265
266 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ch = sio_in(up, SIORXB);
Alan Cox33f0f882006-01-09 20:54:13 -0800268 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 up->port.icount.rx++;
270
271 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
272 UART_LSR_FE | UART_LSR_OE))) {
273 /*
274 * For statistics only
275 */
276 if (*status & UART_LSR_BI) {
277 *status &= ~(UART_LSR_FE | UART_LSR_PE);
278 up->port.icount.brk++;
279 /*
280 * We do the SysRQ and SAK checking
281 * here because otherwise the break
282 * may get masked by ignore_status_mask
283 * or read_status_mask.
284 */
285 if (uart_handle_break(&up->port))
286 goto ignore_char;
287 } else if (*status & UART_LSR_PE)
288 up->port.icount.parity++;
289 else if (*status & UART_LSR_FE)
290 up->port.icount.frame++;
291 if (*status & UART_LSR_OE)
292 up->port.icount.overrun++;
293
294 /*
295 * Mask off conditions which should be ingored.
296 */
297 *status &= up->port.read_status_mask;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (*status & UART_LSR_BI) {
Jiri Slabyad245aa2016-01-12 10:59:08 +0100300 pr_debug("handling break....\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800301 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800303 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800305 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
David Howells7d12e782006-10-05 14:55:46 +0100307 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800309 if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100310 tty_insert_flip_char(port, ch, flag);
Alan Cox33f0f882006-01-09 20:54:13 -0800311
312 if (*status & UART_LSR_OE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Overrun is special, since it's reported
315 * immediately, and doesn't affect the current
316 * character.
317 */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100318 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 ignore_char:
321 *status = serial_in(up, UART_LSR);
322 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Viresh Kumaraf89f832013-08-19 20:14:16 +0530323
324 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100325 tty_flip_buffer_push(port);
Viresh Kumaraf89f832013-08-19 20:14:16 +0530326 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800329static void transmit_chars(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700331 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int count;
333
334 if (up->port.x_char) {
335#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
336 serial_out(up, UART_TX, up->port.x_char);
337#endif
338 up->port.icount.tx++;
339 up->port.x_char = 0;
340 return;
341 }
342 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100343 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return;
345 }
346
347 count = up->port.fifosize;
348 do {
349 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
350 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
351 up->port.icount.tx++;
352 if (uart_circ_empty(xmit))
353 break;
Julia Lawall022d9172008-03-04 14:29:19 -0800354 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 } while (--count > 0);
357
358 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
359 uart_write_wakeup(&up->port);
360
Jiri Slabyad245aa2016-01-12 10:59:08 +0100361 pr_debug("THRE...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100364 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/*
368 * This handles the interrupt from one port.
369 */
370static inline void m32r_sio_handle_port(struct uart_sio_port *up,
David Howells7d12e782006-10-05 14:55:46 +0100371 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Jiri Slabyad245aa2016-01-12 10:59:08 +0100373 pr_debug("status = %x...\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 if (status & 0x04)
David Howells7d12e782006-10-05 14:55:46 +0100376 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (status & 0x01)
378 transmit_chars(up);
379}
380
381/*
382 * This is the serial driver's interrupt routine.
383 *
384 * Arjan thinks the old way was overly complex, so it got simplified.
385 * Alan disagrees, saying that need the complexity to handle the weird
386 * nature of ISA shared interrupts. (This is a special exception.)
387 *
388 * In order to handle ISA shared interrupts properly, we need to check
389 * that all ports have been serviced, and therefore the ISA interrupt
390 * line has been de-asserted.
391 *
392 * This means we need to loop through all ports. checking that they
393 * don't have an interrupt pending.
394 */
David Howells7d12e782006-10-05 14:55:46 +0100395static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct irq_info *i = dev_id;
398 struct list_head *l, *end = NULL;
399 int pass_counter = 0;
400
Jiri Slabyad245aa2016-01-12 10:59:08 +0100401 pr_debug("m32r_sio_interrupt(%d)...\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403#ifdef CONFIG_SERIAL_M32R_PLDSIO
404// if (irq == PLD_IRQ_SIO0_SND)
405// irq = PLD_IRQ_SIO0_RCV;
406#else
407 if (irq == M32R_IRQ_SIO0_S)
408 irq = M32R_IRQ_SIO0_R;
409#endif
410
411 spin_lock(&i->lock);
412
413 l = i->head;
414 do {
415 struct uart_sio_port *up;
416 unsigned int sts;
417
418 up = list_entry(l, struct uart_sio_port, list);
419
420 sts = sio_in(up, SIOSTS);
421 if (sts & 0x5) {
422 spin_lock(&up->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100423 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 spin_unlock(&up->port.lock);
425
426 end = NULL;
427 } else if (end == NULL)
428 end = l;
429
430 l = l->next;
431
432 if (l == i->head && pass_counter++ > PASS_LIMIT) {
433 if (sts & 0xe0)
434 sio_error(&sts);
435 break;
436 }
437 } while (l != end);
438
439 spin_unlock(&i->lock);
440
Jiri Slabyad245aa2016-01-12 10:59:08 +0100441 pr_debug("end.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return IRQ_HANDLED;
444}
445
446/*
447 * To support ISA shared interrupts, we need to have one interrupt
448 * handler that ensures that the IRQ line has been deasserted
449 * before returning. Failing to do this will result in the IRQ
450 * line being stuck active, and, since ISA irqs are edge triggered,
451 * no more IRQs will be seen.
452 */
453static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
454{
455 spin_lock_irq(&i->lock);
456
457 if (!list_empty(i->head)) {
458 if (i->head == &up->list)
459 i->head = i->head->next;
460 list_del(&up->list);
461 } else {
462 BUG_ON(i->head != &up->list);
463 i->head = NULL;
464 }
465
466 spin_unlock_irq(&i->lock);
467}
468
469static int serial_link_irq_chain(struct uart_sio_port *up)
470{
471 struct irq_info *i = irq_lists + up->port.irq;
Hirokazu Takatadab8f492007-10-16 01:26:36 -0700472 int ret, irq_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 spin_lock_irq(&i->lock);
475
476 if (i->head) {
477 list_add(&up->list, i->head);
478 spin_unlock_irq(&i->lock);
479
480 ret = 0;
481 } else {
482 INIT_LIST_HEAD(&up->list);
483 i->head = &up->list;
484 spin_unlock_irq(&i->lock);
485
486 ret = request_irq(up->port.irq, m32r_sio_interrupt,
487 irq_flags, "SIO0-RX", i);
488 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
489 irq_flags, "SIO0-TX", i);
490 if (ret < 0)
491 serial_do_unlink(i, up);
492 }
493
494 return ret;
495}
496
497static void serial_unlink_irq_chain(struct uart_sio_port *up)
498{
499 struct irq_info *i = irq_lists + up->port.irq;
500
501 BUG_ON(i->head == NULL);
502
503 if (list_empty(i->head)) {
504 free_irq(up->port.irq, i);
505 free_irq(up->port.irq + 1, i);
506 }
507
508 serial_do_unlink(i, up);
509}
510
511/*
512 * This function is used to handle ports that do not have an interrupt.
513 */
Kees Cookf0f62c62017-10-16 16:27:37 -0700514static void m32r_sio_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Kees Cookf0f62c62017-10-16 16:27:37 -0700516 struct uart_sio_port *up = from_timer(up, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned int timeout;
518 unsigned int sts;
519
520 sts = sio_in(up, SIOSTS);
521 if (sts & 0x5) {
522 spin_lock(&up->port.lock);
Al Viro9c8e7f52006-10-07 16:29:18 +0100523 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 spin_unlock(&up->port.lock);
525 }
526
527 timeout = up->port.timeout;
528 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
529 mod_timer(&up->timer, jiffies + timeout);
530}
531
532static unsigned int m32r_sio_tx_empty(struct uart_port *port)
533{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200534 struct uart_sio_port *up =
535 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 unsigned long flags;
537 unsigned int ret;
538
539 spin_lock_irqsave(&up->port.lock, flags);
540 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
541 spin_unlock_irqrestore(&up->port.lock, flags);
542
543 return ret;
544}
545
546static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
547{
548 return 0;
549}
550
551static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
552{
553
554}
555
556static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
557{
558
559}
560
561static int m32r_sio_startup(struct uart_port *port)
562{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200563 struct uart_sio_port *up =
564 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int retval;
566
567 sio_init();
568
569 /*
570 * If the "interrupt" for this port doesn't correspond with any
571 * hardware interrupt, we use a timer-based system. The original
572 * driver used to do this with IRQ0.
573 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000574 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned int timeout = up->port.timeout;
576
577 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 mod_timer(&up->timer, jiffies + timeout);
580 } else {
581 retval = serial_link_irq_chain(up);
582 if (retval)
583 return retval;
584 }
585
586 /*
587 * Finally, enable interrupts. Note: Modem status interrupts
588 * are set via set_termios(), which will be occurring imminently
589 * anyway, so we don't enable them here.
590 * - M32R_SIO: 0x0c
591 * - M32R_PLDSIO: 0x04
592 */
593 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
594 sio_out(up, SIOTRCR, up->ier);
595
596 /*
597 * And clear the interrupt registers again for luck.
598 */
599 sio_reset();
600
601 return 0;
602}
603
604static void m32r_sio_shutdown(struct uart_port *port)
605{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200606 struct uart_sio_port *up =
607 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * Disable interrupts from this port
611 */
612 up->ier = 0;
613 sio_out(up, SIOTRCR, 0);
614
615 /*
616 * Disable break condition and FIFOs
617 */
618
619 sio_init();
620
Alan Coxd4e33fa2012-01-26 17:44:09 +0000621 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 del_timer_sync(&up->timer);
623 else
624 serial_unlink_irq_chain(up);
625}
626
627static unsigned int m32r_sio_get_divisor(struct uart_port *port,
628 unsigned int baud)
629{
630 return uart_get_divisor(port, baud);
631}
632
633static void m32r_sio_set_termios(struct uart_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800634 struct ktermios *termios, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200636 struct uart_sio_port *up =
637 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned char cval = 0;
639 unsigned long flags;
640 unsigned int baud, quot;
641
642 switch (termios->c_cflag & CSIZE) {
643 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100644 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100647 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100650 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 default:
653 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100654 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657
658 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100659 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (termios->c_cflag & PARENB)
661 cval |= UART_LCR_PARITY;
662 if (!(termios->c_cflag & PARODD))
663 cval |= UART_LCR_EPAR;
664#ifdef CMSPAR
665 if (termios->c_cflag & CMSPAR)
666 cval |= UART_LCR_SPAR;
667#endif
668
669 /*
670 * Ask the core to calculate the divisor for us.
671 */
672#ifdef CONFIG_SERIAL_M32R_PLDSIO
673 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
674#else
675 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
676#endif
677 quot = m32r_sio_get_divisor(port, baud);
678
679 /*
680 * Ok, we're now changing the port state. Do it with
681 * interrupts disabled.
682 */
683 spin_lock_irqsave(&up->port.lock, flags);
684
685 sio_set_baud_rate(baud);
686
687 /*
688 * Update the per-port timeout.
689 */
690 uart_update_timeout(port, termios->c_cflag, baud);
691
692 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
693 if (termios->c_iflag & INPCK)
694 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400695 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 up->port.read_status_mask |= UART_LSR_BI;
697
698 /*
699 * Characteres to ignore
700 */
701 up->port.ignore_status_mask = 0;
702 if (termios->c_iflag & IGNPAR)
703 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
704 if (termios->c_iflag & IGNBRK) {
705 up->port.ignore_status_mask |= UART_LSR_BI;
706 /*
707 * If we're ignoring parity and break indicators,
708 * ignore overruns too (for real raw support).
709 */
710 if (termios->c_iflag & IGNPAR)
711 up->port.ignore_status_mask |= UART_LSR_OE;
712 }
713
714 /*
715 * ignore all characters if CREAD is not set
716 */
717 if ((termios->c_cflag & CREAD) == 0)
718 up->port.ignore_status_mask |= UART_LSR_DR;
719
720 /*
721 * CTS flow control flag and modem status interrupts
722 */
723 up->ier &= ~UART_IER_MSI;
724 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
725 up->ier |= UART_IER_MSI;
726
727 serial_out(up, UART_IER, up->ier);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_unlock_irqrestore(&up->port.lock, flags);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * Resource handling. This is complicated by the fact that resources
734 * depend on the port type. Maybe we should be claiming the standard
735 * 8250 ports, and then trying to get other resources as necessary?
736 */
737static int
738m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
739{
740 unsigned int size = 8 << up->port.regshift;
741#ifndef CONFIG_SERIAL_M32R_PLDSIO
742 unsigned long start;
743#endif
744 int ret = 0;
745
746 switch (up->port.iotype) {
747 case UPIO_MEM:
748 if (up->port.mapbase) {
749#ifdef CONFIG_SERIAL_M32R_PLDSIO
750 *res = request_mem_region(up->port.mapbase, size, "serial");
751#else
752 start = up->port.mapbase;
753 *res = request_mem_region(start, size, "serial");
754#endif
755 if (!*res)
756 ret = -EBUSY;
757 }
758 break;
759
760 case UPIO_PORT:
761 *res = request_region(up->port.iobase, size, "serial");
762 if (!*res)
763 ret = -EBUSY;
764 break;
765 }
766 return ret;
767}
768
769static void m32r_sio_release_port(struct uart_port *port)
770{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200771 struct uart_sio_port *up =
772 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 unsigned long start, offset = 0, size = 0;
774
775 size <<= up->port.regshift;
776
777 switch (up->port.iotype) {
778 case UPIO_MEM:
779 if (up->port.mapbase) {
780 /*
781 * Unmap the area.
782 */
783 iounmap(up->port.membase);
784 up->port.membase = NULL;
785
786 start = up->port.mapbase;
787
788 if (size)
789 release_mem_region(start + offset, size);
790 release_mem_region(start, 8 << up->port.regshift);
791 }
792 break;
793
794 case UPIO_PORT:
795 start = up->port.iobase;
796
797 if (size)
798 release_region(start + offset, size);
799 release_region(start + offset, 8 << up->port.regshift);
800 break;
801
802 default:
803 break;
804 }
805}
806
807static int m32r_sio_request_port(struct uart_port *port)
808{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200809 struct uart_sio_port *up =
810 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct resource *res = NULL;
812 int ret = 0;
813
814 ret = m32r_sio_request_std_resource(up, &res);
815
816 /*
817 * If we have a mapbase, then request that as well.
818 */
819 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
Joe Perches28f65c112011-06-09 09:13:32 -0700820 int size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 up->port.membase = ioremap(up->port.mapbase, size);
823 if (!up->port.membase)
824 ret = -ENOMEM;
825 }
826
827 if (ret < 0) {
828 if (res)
829 release_resource(res);
830 }
831
832 return ret;
833}
834
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700835static void m32r_sio_config_port(struct uart_port *port, int unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200837 struct uart_sio_port *up =
838 container_of(port, struct uart_sio_port, port);
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700839 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 spin_lock_irqsave(&up->port.lock, flags);
842
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400843 up->port.fifosize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 spin_unlock_irqrestore(&up->port.lock, flags);
846}
847
848static int
849m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
850{
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400851 if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -EINVAL;
853 return 0;
854}
855
Julia Lawallcdb93942017-08-13 08:21:48 +0200856static const struct uart_ops m32r_sio_pops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .tx_empty = m32r_sio_tx_empty,
858 .set_mctrl = m32r_sio_set_mctrl,
859 .get_mctrl = m32r_sio_get_mctrl,
860 .stop_tx = m32r_sio_stop_tx,
861 .start_tx = m32r_sio_start_tx,
862 .stop_rx = m32r_sio_stop_rx,
863 .enable_ms = m32r_sio_enable_ms,
864 .break_ctl = m32r_sio_break_ctl,
865 .startup = m32r_sio_startup,
866 .shutdown = m32r_sio_shutdown,
867 .set_termios = m32r_sio_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 .release_port = m32r_sio_release_port,
869 .request_port = m32r_sio_request_port,
870 .config_port = m32r_sio_config_port,
871 .verify_port = m32r_sio_verify_port,
872};
873
874static struct uart_sio_port m32r_sio_ports[UART_NR];
875
876static void __init m32r_sio_init_ports(void)
877{
878 struct uart_sio_port *up;
879 static int first = 1;
880 int i;
881
882 if (!first)
883 return;
884 first = 0;
885
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100886 for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 up->port.iobase = old_serial_port[i].port;
888 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100889 up->port.uartclk = BAUD_RATE * 16;
Jiri Slaby6137b7a2016-05-09 09:11:57 +0200890 up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST;
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100891 up->port.membase = 0;
892 up->port.iotype = 0;
893 up->port.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 up->port.ops = &m32r_sio_pops;
895 }
896}
897
898static void __init m32r_sio_register_ports(struct uart_driver *drv)
899{
900 int i;
901
902 m32r_sio_init_ports();
903
904 for (i = 0; i < UART_NR; i++) {
905 struct uart_sio_port *up = &m32r_sio_ports[i];
906
907 up->port.line = i;
908 up->port.ops = &m32r_sio_pops;
Kees Cookf0f62c62017-10-16 16:27:37 -0700909 timer_setup(&up->timer, m32r_sio_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 uart_add_one_port(drv, &up->port);
912 }
913}
914
915#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
916
917/*
918 * Wait for transmitter & holding register to empty
919 */
Denys Vlasenko9cdb9332015-10-27 18:46:37 +0100920static void wait_for_xmitr(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 unsigned int status, tmout = 10000;
923
924 /* Wait up to 10ms for the character(s) to be sent. */
925 do {
926 status = sio_in(up, SIOSTS);
927
928 if (--tmout == 0)
929 break;
930 udelay(1);
931 } while ((status & UART_EMPTY) != UART_EMPTY);
932
933 /* Wait up to 1s for flow control if necessary */
934 if (up->port.flags & UPF_CONS_FLOW) {
935 tmout = 1000000;
936 while (--tmout)
937 udelay(1);
938 }
939}
940
Russell Kingd3587882006-03-20 20:00:09 +0000941static void m32r_sio_console_putchar(struct uart_port *port, int ch)
942{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200943 struct uart_sio_port *up =
944 container_of(port, struct uart_sio_port, port);
Russell Kingd3587882006-03-20 20:00:09 +0000945
946 wait_for_xmitr(up);
947 sio_out(up, SIOTXB, ch);
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * Print a string to the serial port trying not to disturb
952 * any possible real use of the port...
953 *
954 * The console_lock must be held when we get here.
955 */
956static void m32r_sio_console_write(struct console *co, const char *s,
957 unsigned int count)
958{
959 struct uart_sio_port *up = &m32r_sio_ports[co->index];
960 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /*
963 * First save the UER then disable the interrupts
964 */
965 ier = sio_in(up, SIOTRCR);
966 sio_out(up, SIOTRCR, 0);
967
Russell Kingd3587882006-03-20 20:00:09 +0000968 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /*
971 * Finally, wait for transmitter to become empty
972 * and restore the IER
973 */
974 wait_for_xmitr(up);
975 sio_out(up, SIOTRCR, ier);
976}
977
978static int __init m32r_sio_console_setup(struct console *co, char *options)
979{
980 struct uart_port *port;
981 int baud = 9600;
982 int bits = 8;
983 int parity = 'n';
984 int flow = 'n';
985
986 /*
987 * Check whether an invalid uart number has been specified, and
988 * if so, search for the first available port that does have
989 * console support.
990 */
991 if (co->index >= UART_NR)
992 co->index = 0;
993 port = &m32r_sio_ports[co->index].port;
994
995 /*
996 * Temporary fix.
997 */
998 spin_lock_init(&port->lock);
999
1000 if (options)
1001 uart_parse_options(options, &baud, &parity, &bits, &flow);
1002
1003 return uart_set_options(port, co, baud, parity, bits, flow);
1004}
1005
Al Viroa828b8e2005-08-23 22:47:27 +01001006static struct uart_driver m32r_sio_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static struct console m32r_sio_console = {
1008 .name = "ttyS",
1009 .write = m32r_sio_console_write,
1010 .device = uart_console_device,
1011 .setup = m32r_sio_console_setup,
1012 .flags = CON_PRINTBUFFER,
1013 .index = -1,
1014 .data = &m32r_sio_reg,
1015};
1016
1017static int __init m32r_sio_console_init(void)
1018{
1019 sio_reset();
1020 sio_init();
1021 m32r_sio_init_ports();
1022 register_console(&m32r_sio_console);
1023 return 0;
1024}
1025console_initcall(m32r_sio_console_init);
1026
1027#define M32R_SIO_CONSOLE &m32r_sio_console
1028#else
1029#define M32R_SIO_CONSOLE NULL
1030#endif
1031
1032static struct uart_driver m32r_sio_reg = {
1033 .owner = THIS_MODULE,
1034 .driver_name = "sio",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 .dev_name = "ttyS",
1036 .major = TTY_MAJOR,
1037 .minor = 64,
1038 .nr = UART_NR,
1039 .cons = M32R_SIO_CONSOLE,
1040};
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static int __init m32r_sio_init(void)
1043{
1044 int ret, i;
1045
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001046 printk(KERN_INFO "Serial: M32R SIO driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Yinghai Lua62c4132008-08-19 20:49:55 -07001048 for (i = 0; i < nr_irqs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_lock_init(&irq_lists[i].lock);
1050
1051 ret = uart_register_driver(&m32r_sio_reg);
1052 if (ret >= 0)
1053 m32r_sio_register_ports(&m32r_sio_reg);
1054
1055 return ret;
1056}
Paul Gortmakera10aebe2016-06-20 18:55:05 -04001057device_initcall(m32r_sio_init);