blob: 1b01504cf3060df73ac55a1cd2cdc08e48b423f1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define PASS_LIMIT 256
53
Jiri Slabyd4dbe372016-01-12 10:59:06 +010054static const struct {
Jiri Slabyd4dbe372016-01-12 10:59:06 +010055 unsigned int port;
56 unsigned int irq;
Jiri Slabyd4dbe372016-01-12 10:59:06 +010057} old_serial_port[] = {
Jiri Slabyc8e7d142016-01-12 10:59:07 +010058#if defined(CONFIG_PLAT_USRV)
59 /* PORT IRQ FLAGS */
60 { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
61 { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
62#elif defined(CONFIG_SERIAL_M32R_PLDSIO)
63 { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
64#else
65 { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
66#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69#define UART_NR ARRAY_SIZE(old_serial_port)
70
71struct uart_sio_port {
72 struct uart_port port;
73 struct timer_list timer; /* "no irq" timer */
74 struct list_head list; /* ports on this IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned char ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
78struct irq_info {
79 spinlock_t lock;
80 struct list_head *head;
81};
82
83static struct irq_info irq_lists[NR_IRQS];
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_SERIAL_M32R_PLDSIO
86
87#define __sio_in(x) inw((unsigned long)(x))
88#define __sio_out(v,x) outw((v),(unsigned long)(x))
89
90static inline void sio_set_baud_rate(unsigned long baud)
91{
92 unsigned short sbaud;
93 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
94 __sio_out(sbaud, PLD_ESIO0BAUR);
95}
96
97static void sio_reset(void)
98{
99 unsigned short tmp;
100
101 tmp = __sio_in(PLD_ESIO0RXB);
102 tmp = __sio_in(PLD_ESIO0RXB);
103 tmp = __sio_in(PLD_ESIO0CR);
104 sio_set_baud_rate(BAUD_RATE);
105 __sio_out(0x0300, PLD_ESIO0CR);
106 __sio_out(0x0003, PLD_ESIO0CR);
107}
108
109static void sio_init(void)
110{
111 unsigned short tmp;
112
113 tmp = __sio_in(PLD_ESIO0RXB);
114 tmp = __sio_in(PLD_ESIO0RXB);
115 tmp = __sio_in(PLD_ESIO0CR);
116 __sio_out(0x0300, PLD_ESIO0CR);
117 __sio_out(0x0003, PLD_ESIO0CR);
118}
119
120static void sio_error(int *status)
121{
122 printk("SIO0 error[%04x]\n", *status);
123 do {
124 sio_init();
125 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
126}
127
128#else /* not CONFIG_SERIAL_M32R_PLDSIO */
129
130#define __sio_in(x) inl(x)
131#define __sio_out(v,x) outl((v),(x))
132
133static inline void sio_set_baud_rate(unsigned long baud)
134{
135 unsigned long i, j;
136
137 i = boot_cpu_data.bus_clock / (baud * 16);
138 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
139 i -= 1;
140 j = (j + 1) >> 1;
141
142 __sio_out(i, M32R_SIO0_BAUR_PORTL);
143 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
144}
145
146static void sio_reset(void)
147{
148 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
149 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
150 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
151 sio_set_baud_rate(BAUD_RATE);
152 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
153 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
154}
155
156static void sio_init(void)
157{
158 unsigned int tmp;
159
160 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
161 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
162 tmp = __sio_in(M32R_SIO0_STS_PORTL);
163 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
164}
165
166static void sio_error(int *status)
167{
168 printk("SIO0 error[%04x]\n", *status);
169 do {
170 sio_init();
171 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
172}
173
174#endif /* CONFIG_SERIAL_M32R_PLDSIO */
175
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800176static unsigned int sio_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 return __sio_in(up->port.iobase + offset);
179}
180
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800181static void sio_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 __sio_out(value, up->port.iobase + offset);
184}
185
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800186static unsigned int serial_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 if (!offset)
189 return 0;
190
191 return __sio_in(offset);
192}
193
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800194static void serial_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 if (!offset)
197 return;
198
199 __sio_out(value, offset);
200}
201
Russell Kingb129a8c2005-08-31 10:12:14 +0100202static void m32r_sio_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200204 struct uart_sio_port *up =
205 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (up->ier & UART_IER_THRI) {
208 up->ier &= ~UART_IER_THRI;
209 serial_out(up, UART_IER, up->ier);
210 }
211}
212
Russell Kingb129a8c2005-08-31 10:12:14 +0100213static void m32r_sio_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215#ifdef CONFIG_SERIAL_M32R_PLDSIO
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200216 struct uart_sio_port *up =
217 container_of(port, struct uart_sio_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700218 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!(up->ier & UART_IER_THRI)) {
221 up->ier |= UART_IER_THRI;
222 serial_out(up, UART_IER, up->ier);
Peter Hurleyc557d392014-07-06 11:29:52 -0400223 if (!uart_circ_empty(xmit)) {
224 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
225 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
226 up->port.icount.tx++;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
230#else
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200231 struct uart_sio_port *up =
232 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!(up->ier & UART_IER_THRI)) {
235 up->ier |= UART_IER_THRI;
236 serial_out(up, UART_IER, up->ier);
237 }
238#endif
239}
240
241static void m32r_sio_stop_rx(struct uart_port *port)
242{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200243 struct uart_sio_port *up =
244 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 up->ier &= ~UART_IER_RLSI;
247 up->port.read_status_mask &= ~UART_LSR_DR;
248 serial_out(up, UART_IER, up->ier);
249}
250
251static void m32r_sio_enable_ms(struct uart_port *port)
252{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200253 struct uart_sio_port *up =
254 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 up->ier |= UART_IER_MSI;
257 serial_out(up, UART_IER, up->ier);
258}
259
David Howells7d12e782006-10-05 14:55:46 +0100260static void receive_chars(struct uart_sio_port *up, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100262 struct tty_port *port = &up->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned char ch;
Alan Cox33f0f882006-01-09 20:54:13 -0800264 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int max_count = 256;
266
267 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ch = sio_in(up, SIORXB);
Alan Cox33f0f882006-01-09 20:54:13 -0800269 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 up->port.icount.rx++;
271
272 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
273 UART_LSR_FE | UART_LSR_OE))) {
274 /*
275 * For statistics only
276 */
277 if (*status & UART_LSR_BI) {
278 *status &= ~(UART_LSR_FE | UART_LSR_PE);
279 up->port.icount.brk++;
280 /*
281 * We do the SysRQ and SAK checking
282 * here because otherwise the break
283 * may get masked by ignore_status_mask
284 * or read_status_mask.
285 */
286 if (uart_handle_break(&up->port))
287 goto ignore_char;
288 } else if (*status & UART_LSR_PE)
289 up->port.icount.parity++;
290 else if (*status & UART_LSR_FE)
291 up->port.icount.frame++;
292 if (*status & UART_LSR_OE)
293 up->port.icount.overrun++;
294
295 /*
296 * Mask off conditions which should be ingored.
297 */
298 *status &= up->port.read_status_mask;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (*status & UART_LSR_BI) {
Jiri Slabyad245aa2016-01-12 10:59:08 +0100301 pr_debug("handling break....\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800302 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800304 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800306 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
David Howells7d12e782006-10-05 14:55:46 +0100308 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800310 if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100311 tty_insert_flip_char(port, ch, flag);
Alan Cox33f0f882006-01-09 20:54:13 -0800312
313 if (*status & UART_LSR_OE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /*
315 * Overrun is special, since it's reported
316 * immediately, and doesn't affect the current
317 * character.
318 */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100319 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 ignore_char:
322 *status = serial_in(up, UART_LSR);
323 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Viresh Kumaraf89f832013-08-19 20:14:16 +0530324
325 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100326 tty_flip_buffer_push(port);
Viresh Kumaraf89f832013-08-19 20:14:16 +0530327 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800330static void transmit_chars(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700332 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int count;
334
335 if (up->port.x_char) {
336#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
337 serial_out(up, UART_TX, up->port.x_char);
338#endif
339 up->port.icount.tx++;
340 up->port.x_char = 0;
341 return;
342 }
343 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100344 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return;
346 }
347
348 count = up->port.fifosize;
349 do {
350 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
351 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
352 up->port.icount.tx++;
353 if (uart_circ_empty(xmit))
354 break;
Julia Lawall022d9172008-03-04 14:29:19 -0800355 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 } while (--count > 0);
358
359 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
360 uart_write_wakeup(&up->port);
361
Jiri Slabyad245aa2016-01-12 10:59:08 +0100362 pr_debug("THRE...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100365 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/*
369 * This handles the interrupt from one port.
370 */
371static inline void m32r_sio_handle_port(struct uart_sio_port *up,
David Howells7d12e782006-10-05 14:55:46 +0100372 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jiri Slabyad245aa2016-01-12 10:59:08 +0100374 pr_debug("status = %x...\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (status & 0x04)
David Howells7d12e782006-10-05 14:55:46 +0100377 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (status & 0x01)
379 transmit_chars(up);
380}
381
382/*
383 * This is the serial driver's interrupt routine.
384 *
385 * Arjan thinks the old way was overly complex, so it got simplified.
386 * Alan disagrees, saying that need the complexity to handle the weird
387 * nature of ISA shared interrupts. (This is a special exception.)
388 *
389 * In order to handle ISA shared interrupts properly, we need to check
390 * that all ports have been serviced, and therefore the ISA interrupt
391 * line has been de-asserted.
392 *
393 * This means we need to loop through all ports. checking that they
394 * don't have an interrupt pending.
395 */
David Howells7d12e782006-10-05 14:55:46 +0100396static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct irq_info *i = dev_id;
399 struct list_head *l, *end = NULL;
400 int pass_counter = 0;
401
Jiri Slabyad245aa2016-01-12 10:59:08 +0100402 pr_debug("m32r_sio_interrupt(%d)...\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404#ifdef CONFIG_SERIAL_M32R_PLDSIO
405// if (irq == PLD_IRQ_SIO0_SND)
406// irq = PLD_IRQ_SIO0_RCV;
407#else
408 if (irq == M32R_IRQ_SIO0_S)
409 irq = M32R_IRQ_SIO0_R;
410#endif
411
412 spin_lock(&i->lock);
413
414 l = i->head;
415 do {
416 struct uart_sio_port *up;
417 unsigned int sts;
418
419 up = list_entry(l, struct uart_sio_port, list);
420
421 sts = sio_in(up, SIOSTS);
422 if (sts & 0x5) {
423 spin_lock(&up->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100424 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock(&up->port.lock);
426
427 end = NULL;
428 } else if (end == NULL)
429 end = l;
430
431 l = l->next;
432
433 if (l == i->head && pass_counter++ > PASS_LIMIT) {
434 if (sts & 0xe0)
435 sio_error(&sts);
436 break;
437 }
438 } while (l != end);
439
440 spin_unlock(&i->lock);
441
Jiri Slabyad245aa2016-01-12 10:59:08 +0100442 pr_debug("end.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return IRQ_HANDLED;
445}
446
447/*
448 * To support ISA shared interrupts, we need to have one interrupt
449 * handler that ensures that the IRQ line has been deasserted
450 * before returning. Failing to do this will result in the IRQ
451 * line being stuck active, and, since ISA irqs are edge triggered,
452 * no more IRQs will be seen.
453 */
454static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
455{
456 spin_lock_irq(&i->lock);
457
458 if (!list_empty(i->head)) {
459 if (i->head == &up->list)
460 i->head = i->head->next;
461 list_del(&up->list);
462 } else {
463 BUG_ON(i->head != &up->list);
464 i->head = NULL;
465 }
466
467 spin_unlock_irq(&i->lock);
468}
469
470static int serial_link_irq_chain(struct uart_sio_port *up)
471{
472 struct irq_info *i = irq_lists + up->port.irq;
Hirokazu Takatadab8f492007-10-16 01:26:36 -0700473 int ret, irq_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 spin_lock_irq(&i->lock);
476
477 if (i->head) {
478 list_add(&up->list, i->head);
479 spin_unlock_irq(&i->lock);
480
481 ret = 0;
482 } else {
483 INIT_LIST_HEAD(&up->list);
484 i->head = &up->list;
485 spin_unlock_irq(&i->lock);
486
487 ret = request_irq(up->port.irq, m32r_sio_interrupt,
488 irq_flags, "SIO0-RX", i);
489 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
490 irq_flags, "SIO0-TX", i);
491 if (ret < 0)
492 serial_do_unlink(i, up);
493 }
494
495 return ret;
496}
497
498static void serial_unlink_irq_chain(struct uart_sio_port *up)
499{
500 struct irq_info *i = irq_lists + up->port.irq;
501
502 BUG_ON(i->head == NULL);
503
504 if (list_empty(i->head)) {
505 free_irq(up->port.irq, i);
506 free_irq(up->port.irq + 1, i);
507 }
508
509 serial_do_unlink(i, up);
510}
511
512/*
513 * This function is used to handle ports that do not have an interrupt.
514 */
515static void m32r_sio_timeout(unsigned long data)
516{
517 struct uart_sio_port *up = (struct uart_sio_port *)data;
518 unsigned int timeout;
519 unsigned int sts;
520
521 sts = sio_in(up, SIOSTS);
522 if (sts & 0x5) {
523 spin_lock(&up->port.lock);
Al Viro9c8e7f52006-10-07 16:29:18 +0100524 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 spin_unlock(&up->port.lock);
526 }
527
528 timeout = up->port.timeout;
529 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
530 mod_timer(&up->timer, jiffies + timeout);
531}
532
533static unsigned int m32r_sio_tx_empty(struct uart_port *port)
534{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200535 struct uart_sio_port *up =
536 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned long flags;
538 unsigned int ret;
539
540 spin_lock_irqsave(&up->port.lock, flags);
541 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
542 spin_unlock_irqrestore(&up->port.lock, flags);
543
544 return ret;
545}
546
547static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
548{
549 return 0;
550}
551
552static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
553{
554
555}
556
557static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
558{
559
560}
561
562static int m32r_sio_startup(struct uart_port *port)
563{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200564 struct uart_sio_port *up =
565 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int retval;
567
568 sio_init();
569
570 /*
571 * If the "interrupt" for this port doesn't correspond with any
572 * hardware interrupt, we use a timer-based system. The original
573 * driver used to do this with IRQ0.
574 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000575 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 unsigned int timeout = up->port.timeout;
577
578 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
579
580 up->timer.data = (unsigned long)up;
581 mod_timer(&up->timer, jiffies + timeout);
582 } else {
583 retval = serial_link_irq_chain(up);
584 if (retval)
585 return retval;
586 }
587
588 /*
589 * Finally, enable interrupts. Note: Modem status interrupts
590 * are set via set_termios(), which will be occurring imminently
591 * anyway, so we don't enable them here.
592 * - M32R_SIO: 0x0c
593 * - M32R_PLDSIO: 0x04
594 */
595 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
596 sio_out(up, SIOTRCR, up->ier);
597
598 /*
599 * And clear the interrupt registers again for luck.
600 */
601 sio_reset();
602
603 return 0;
604}
605
606static void m32r_sio_shutdown(struct uart_port *port)
607{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200608 struct uart_sio_port *up =
609 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * Disable interrupts from this port
613 */
614 up->ier = 0;
615 sio_out(up, SIOTRCR, 0);
616
617 /*
618 * Disable break condition and FIFOs
619 */
620
621 sio_init();
622
Alan Coxd4e33fa2012-01-26 17:44:09 +0000623 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 del_timer_sync(&up->timer);
625 else
626 serial_unlink_irq_chain(up);
627}
628
629static unsigned int m32r_sio_get_divisor(struct uart_port *port,
630 unsigned int baud)
631{
632 return uart_get_divisor(port, baud);
633}
634
635static void m32r_sio_set_termios(struct uart_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800636 struct ktermios *termios, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200638 struct uart_sio_port *up =
639 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 unsigned char cval = 0;
641 unsigned long flags;
642 unsigned int baud, quot;
643
644 switch (termios->c_cflag & CSIZE) {
645 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100646 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100649 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100652 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 default:
655 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100656 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 }
659
660 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100661 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (termios->c_cflag & PARENB)
663 cval |= UART_LCR_PARITY;
664 if (!(termios->c_cflag & PARODD))
665 cval |= UART_LCR_EPAR;
666#ifdef CMSPAR
667 if (termios->c_cflag & CMSPAR)
668 cval |= UART_LCR_SPAR;
669#endif
670
671 /*
672 * Ask the core to calculate the divisor for us.
673 */
674#ifdef CONFIG_SERIAL_M32R_PLDSIO
675 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
676#else
677 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
678#endif
679 quot = m32r_sio_get_divisor(port, baud);
680
681 /*
682 * Ok, we're now changing the port state. Do it with
683 * interrupts disabled.
684 */
685 spin_lock_irqsave(&up->port.lock, flags);
686
687 sio_set_baud_rate(baud);
688
689 /*
690 * Update the per-port timeout.
691 */
692 uart_update_timeout(port, termios->c_cflag, baud);
693
694 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
695 if (termios->c_iflag & INPCK)
696 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400697 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 up->port.read_status_mask |= UART_LSR_BI;
699
700 /*
701 * Characteres to ignore
702 */
703 up->port.ignore_status_mask = 0;
704 if (termios->c_iflag & IGNPAR)
705 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
706 if (termios->c_iflag & IGNBRK) {
707 up->port.ignore_status_mask |= UART_LSR_BI;
708 /*
709 * If we're ignoring parity and break indicators,
710 * ignore overruns too (for real raw support).
711 */
712 if (termios->c_iflag & IGNPAR)
713 up->port.ignore_status_mask |= UART_LSR_OE;
714 }
715
716 /*
717 * ignore all characters if CREAD is not set
718 */
719 if ((termios->c_cflag & CREAD) == 0)
720 up->port.ignore_status_mask |= UART_LSR_DR;
721
722 /*
723 * CTS flow control flag and modem status interrupts
724 */
725 up->ier &= ~UART_IER_MSI;
726 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
727 up->ier |= UART_IER_MSI;
728
729 serial_out(up, UART_IER, up->ier);
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 spin_unlock_irqrestore(&up->port.lock, flags);
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Resource handling. This is complicated by the fact that resources
736 * depend on the port type. Maybe we should be claiming the standard
737 * 8250 ports, and then trying to get other resources as necessary?
738 */
739static int
740m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
741{
742 unsigned int size = 8 << up->port.regshift;
743#ifndef CONFIG_SERIAL_M32R_PLDSIO
744 unsigned long start;
745#endif
746 int ret = 0;
747
748 switch (up->port.iotype) {
749 case UPIO_MEM:
750 if (up->port.mapbase) {
751#ifdef CONFIG_SERIAL_M32R_PLDSIO
752 *res = request_mem_region(up->port.mapbase, size, "serial");
753#else
754 start = up->port.mapbase;
755 *res = request_mem_region(start, size, "serial");
756#endif
757 if (!*res)
758 ret = -EBUSY;
759 }
760 break;
761
762 case UPIO_PORT:
763 *res = request_region(up->port.iobase, size, "serial");
764 if (!*res)
765 ret = -EBUSY;
766 break;
767 }
768 return ret;
769}
770
771static void m32r_sio_release_port(struct uart_port *port)
772{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200773 struct uart_sio_port *up =
774 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned long start, offset = 0, size = 0;
776
777 size <<= up->port.regshift;
778
779 switch (up->port.iotype) {
780 case UPIO_MEM:
781 if (up->port.mapbase) {
782 /*
783 * Unmap the area.
784 */
785 iounmap(up->port.membase);
786 up->port.membase = NULL;
787
788 start = up->port.mapbase;
789
790 if (size)
791 release_mem_region(start + offset, size);
792 release_mem_region(start, 8 << up->port.regshift);
793 }
794 break;
795
796 case UPIO_PORT:
797 start = up->port.iobase;
798
799 if (size)
800 release_region(start + offset, size);
801 release_region(start + offset, 8 << up->port.regshift);
802 break;
803
804 default:
805 break;
806 }
807}
808
809static int m32r_sio_request_port(struct uart_port *port)
810{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200811 struct uart_sio_port *up =
812 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct resource *res = NULL;
814 int ret = 0;
815
816 ret = m32r_sio_request_std_resource(up, &res);
817
818 /*
819 * If we have a mapbase, then request that as well.
820 */
821 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
Joe Perches28f65c112011-06-09 09:13:32 -0700822 int size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 up->port.membase = ioremap(up->port.mapbase, size);
825 if (!up->port.membase)
826 ret = -ENOMEM;
827 }
828
829 if (ret < 0) {
830 if (res)
831 release_resource(res);
832 }
833
834 return ret;
835}
836
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700837static void m32r_sio_config_port(struct uart_port *port, int unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200839 struct uart_sio_port *up =
840 container_of(port, struct uart_sio_port, port);
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 spin_lock_irqsave(&up->port.lock, flags);
844
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400845 up->port.fifosize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 spin_unlock_irqrestore(&up->port.lock, flags);
848}
849
850static int
851m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
852{
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400853 if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -EINVAL;
855 return 0;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static struct uart_ops m32r_sio_pops = {
859 .tx_empty = m32r_sio_tx_empty,
860 .set_mctrl = m32r_sio_set_mctrl,
861 .get_mctrl = m32r_sio_get_mctrl,
862 .stop_tx = m32r_sio_stop_tx,
863 .start_tx = m32r_sio_start_tx,
864 .stop_rx = m32r_sio_stop_rx,
865 .enable_ms = m32r_sio_enable_ms,
866 .break_ctl = m32r_sio_break_ctl,
867 .startup = m32r_sio_startup,
868 .shutdown = m32r_sio_shutdown,
869 .set_termios = m32r_sio_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 .release_port = m32r_sio_release_port,
871 .request_port = m32r_sio_request_port,
872 .config_port = m32r_sio_config_port,
873 .verify_port = m32r_sio_verify_port,
874};
875
876static struct uart_sio_port m32r_sio_ports[UART_NR];
877
878static void __init m32r_sio_init_ports(void)
879{
880 struct uart_sio_port *up;
881 static int first = 1;
882 int i;
883
884 if (!first)
885 return;
886 first = 0;
887
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100888 for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 up->port.iobase = old_serial_port[i].port;
890 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100891 up->port.uartclk = BAUD_RATE * 16;
Jiri Slaby6137b7a2016-05-09 09:11:57 +0200892 up->port.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST;
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100893 up->port.membase = 0;
894 up->port.iotype = 0;
895 up->port.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 up->port.ops = &m32r_sio_pops;
897 }
898}
899
900static void __init m32r_sio_register_ports(struct uart_driver *drv)
901{
902 int i;
903
904 m32r_sio_init_ports();
905
906 for (i = 0; i < UART_NR; i++) {
907 struct uart_sio_port *up = &m32r_sio_ports[i];
908
909 up->port.line = i;
910 up->port.ops = &m32r_sio_pops;
911 init_timer(&up->timer);
912 up->timer.function = m32r_sio_timeout;
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 uart_add_one_port(drv, &up->port);
915 }
916}
917
918#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
919
920/*
921 * Wait for transmitter & holding register to empty
922 */
Denys Vlasenko9cdb9332015-10-27 18:46:37 +0100923static void wait_for_xmitr(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 unsigned int status, tmout = 10000;
926
927 /* Wait up to 10ms for the character(s) to be sent. */
928 do {
929 status = sio_in(up, SIOSTS);
930
931 if (--tmout == 0)
932 break;
933 udelay(1);
934 } while ((status & UART_EMPTY) != UART_EMPTY);
935
936 /* Wait up to 1s for flow control if necessary */
937 if (up->port.flags & UPF_CONS_FLOW) {
938 tmout = 1000000;
939 while (--tmout)
940 udelay(1);
941 }
942}
943
Russell Kingd3587882006-03-20 20:00:09 +0000944static void m32r_sio_console_putchar(struct uart_port *port, int ch)
945{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200946 struct uart_sio_port *up =
947 container_of(port, struct uart_sio_port, port);
Russell Kingd3587882006-03-20 20:00:09 +0000948
949 wait_for_xmitr(up);
950 sio_out(up, SIOTXB, ch);
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
954 * Print a string to the serial port trying not to disturb
955 * any possible real use of the port...
956 *
957 * The console_lock must be held when we get here.
958 */
959static void m32r_sio_console_write(struct console *co, const char *s,
960 unsigned int count)
961{
962 struct uart_sio_port *up = &m32r_sio_ports[co->index];
963 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * First save the UER then disable the interrupts
967 */
968 ier = sio_in(up, SIOTRCR);
969 sio_out(up, SIOTRCR, 0);
970
Russell Kingd3587882006-03-20 20:00:09 +0000971 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * Finally, wait for transmitter to become empty
975 * and restore the IER
976 */
977 wait_for_xmitr(up);
978 sio_out(up, SIOTRCR, ier);
979}
980
981static int __init m32r_sio_console_setup(struct console *co, char *options)
982{
983 struct uart_port *port;
984 int baud = 9600;
985 int bits = 8;
986 int parity = 'n';
987 int flow = 'n';
988
989 /*
990 * Check whether an invalid uart number has been specified, and
991 * if so, search for the first available port that does have
992 * console support.
993 */
994 if (co->index >= UART_NR)
995 co->index = 0;
996 port = &m32r_sio_ports[co->index].port;
997
998 /*
999 * Temporary fix.
1000 */
1001 spin_lock_init(&port->lock);
1002
1003 if (options)
1004 uart_parse_options(options, &baud, &parity, &bits, &flow);
1005
1006 return uart_set_options(port, co, baud, parity, bits, flow);
1007}
1008
Al Viroa828b8e2005-08-23 22:47:27 +01001009static struct uart_driver m32r_sio_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010static struct console m32r_sio_console = {
1011 .name = "ttyS",
1012 .write = m32r_sio_console_write,
1013 .device = uart_console_device,
1014 .setup = m32r_sio_console_setup,
1015 .flags = CON_PRINTBUFFER,
1016 .index = -1,
1017 .data = &m32r_sio_reg,
1018};
1019
1020static int __init m32r_sio_console_init(void)
1021{
1022 sio_reset();
1023 sio_init();
1024 m32r_sio_init_ports();
1025 register_console(&m32r_sio_console);
1026 return 0;
1027}
1028console_initcall(m32r_sio_console_init);
1029
1030#define M32R_SIO_CONSOLE &m32r_sio_console
1031#else
1032#define M32R_SIO_CONSOLE NULL
1033#endif
1034
1035static struct uart_driver m32r_sio_reg = {
1036 .owner = THIS_MODULE,
1037 .driver_name = "sio",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .dev_name = "ttyS",
1039 .major = TTY_MAJOR,
1040 .minor = 64,
1041 .nr = UART_NR,
1042 .cons = M32R_SIO_CONSOLE,
1043};
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045static int __init m32r_sio_init(void)
1046{
1047 int ret, i;
1048
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001049 printk(KERN_INFO "Serial: M32R SIO driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Yinghai Lua62c4132008-08-19 20:49:55 -07001051 for (i = 0; i < nr_irqs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_lock_init(&irq_lists[i].lock);
1053
1054 ret = uart_register_driver(&m32r_sio_reg);
1055 if (ret >= 0)
1056 m32r_sio_register_ports(&m32r_sio_reg);
1057
1058 return ret;
1059}
1060
1061static void __exit m32r_sio_exit(void)
1062{
1063 int i;
1064
1065 for (i = 0; i < UART_NR; i++)
1066 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1067
1068 uart_unregister_driver(&m32r_sio_reg);
1069}
1070
1071module_init(m32r_sio_init);
1072module_exit(m32r_sio_exit);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001075MODULE_DESCRIPTION("Generic M32R SIO serial driver");