blob: 6b13035056673e1cd17bfbd156f0e3bc8110687d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Standard COM flags */
Russell King59a675b2006-02-05 10:52:29 +000055#define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jiri Slabyd4dbe372016-01-12 10:59:06 +010057static const struct {
Jiri Slabyd4dbe372016-01-12 10:59:06 +010058 unsigned int port;
59 unsigned int irq;
Jiri Slabyd4dbe372016-01-12 10:59:06 +010060} old_serial_port[] = {
Jiri Slabyc8e7d142016-01-12 10:59:07 +010061#if defined(CONFIG_PLAT_USRV)
62 /* PORT IRQ FLAGS */
63 { 0x3F8, PLD_IRQ_UART0 }, /* ttyS0 */
64 { 0x2F8, PLD_IRQ_UART1 }, /* ttyS1 */
65#elif defined(CONFIG_SERIAL_M32R_PLDSIO)
66 { ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV }, /* ttyS0 */
67#else
68 { M32R_SIO_OFFSET, M32R_IRQ_SIO0_R }, /* ttyS0 */
69#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
72#define UART_NR ARRAY_SIZE(old_serial_port)
73
74struct uart_sio_port {
75 struct uart_port port;
76 struct timer_list timer; /* "no irq" timer */
77 struct list_head list; /* ports on this IRQ */
78 unsigned short rev;
79 unsigned char acr;
80 unsigned char ier;
81 unsigned char lcr;
82 unsigned char mcr_mask; /* mask of user bits */
83 unsigned char mcr_force; /* mask of forced bits */
84 unsigned char lsr_break_flag;
85
86 /*
87 * We provide a per-port pm hook.
88 */
89 void (*pm)(struct uart_port *port,
90 unsigned int state, unsigned int old);
91};
92
93struct irq_info {
94 spinlock_t lock;
95 struct list_head *head;
96};
97
98static struct irq_info irq_lists[NR_IRQS];
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef CONFIG_SERIAL_M32R_PLDSIO
101
102#define __sio_in(x) inw((unsigned long)(x))
103#define __sio_out(v,x) outw((v),(unsigned long)(x))
104
105static inline void sio_set_baud_rate(unsigned long baud)
106{
107 unsigned short sbaud;
108 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
109 __sio_out(sbaud, PLD_ESIO0BAUR);
110}
111
112static void sio_reset(void)
113{
114 unsigned short tmp;
115
116 tmp = __sio_in(PLD_ESIO0RXB);
117 tmp = __sio_in(PLD_ESIO0RXB);
118 tmp = __sio_in(PLD_ESIO0CR);
119 sio_set_baud_rate(BAUD_RATE);
120 __sio_out(0x0300, PLD_ESIO0CR);
121 __sio_out(0x0003, PLD_ESIO0CR);
122}
123
124static void sio_init(void)
125{
126 unsigned short tmp;
127
128 tmp = __sio_in(PLD_ESIO0RXB);
129 tmp = __sio_in(PLD_ESIO0RXB);
130 tmp = __sio_in(PLD_ESIO0CR);
131 __sio_out(0x0300, PLD_ESIO0CR);
132 __sio_out(0x0003, PLD_ESIO0CR);
133}
134
135static void sio_error(int *status)
136{
137 printk("SIO0 error[%04x]\n", *status);
138 do {
139 sio_init();
140 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
141}
142
143#else /* not CONFIG_SERIAL_M32R_PLDSIO */
144
145#define __sio_in(x) inl(x)
146#define __sio_out(v,x) outl((v),(x))
147
148static inline void sio_set_baud_rate(unsigned long baud)
149{
150 unsigned long i, j;
151
152 i = boot_cpu_data.bus_clock / (baud * 16);
153 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
154 i -= 1;
155 j = (j + 1) >> 1;
156
157 __sio_out(i, M32R_SIO0_BAUR_PORTL);
158 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
159}
160
161static void sio_reset(void)
162{
163 __sio_out(0x00000300, M32R_SIO0_CR_PORTL); /* init status */
164 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL); /* 8bit */
165 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL); /* 1stop non */
166 sio_set_baud_rate(BAUD_RATE);
167 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
168 __sio_out(0x00000003, M32R_SIO0_CR_PORTL); /* RXCEN */
169}
170
171static void sio_init(void)
172{
173 unsigned int tmp;
174
175 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
176 tmp = __sio_in(M32R_SIO0_RXB_PORTL);
177 tmp = __sio_in(M32R_SIO0_STS_PORTL);
178 __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
179}
180
181static void sio_error(int *status)
182{
183 printk("SIO0 error[%04x]\n", *status);
184 do {
185 sio_init();
186 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
187}
188
189#endif /* CONFIG_SERIAL_M32R_PLDSIO */
190
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800191static unsigned int sio_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 return __sio_in(up->port.iobase + offset);
194}
195
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800196static void sio_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 __sio_out(value, up->port.iobase + offset);
199}
200
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800201static unsigned int serial_in(struct uart_sio_port *up, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 if (!offset)
204 return 0;
205
206 return __sio_in(offset);
207}
208
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800209static void serial_out(struct uart_sio_port *up, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 if (!offset)
212 return;
213
214 __sio_out(value, offset);
215}
216
Russell Kingb129a8c2005-08-31 10:12:14 +0100217static void m32r_sio_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200219 struct uart_sio_port *up =
220 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (up->ier & UART_IER_THRI) {
223 up->ier &= ~UART_IER_THRI;
224 serial_out(up, UART_IER, up->ier);
225 }
226}
227
Russell Kingb129a8c2005-08-31 10:12:14 +0100228static void m32r_sio_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230#ifdef CONFIG_SERIAL_M32R_PLDSIO
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200231 struct uart_sio_port *up =
232 container_of(port, struct uart_sio_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700233 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (!(up->ier & UART_IER_THRI)) {
236 up->ier |= UART_IER_THRI;
237 serial_out(up, UART_IER, up->ier);
Peter Hurleyc557d392014-07-06 11:29:52 -0400238 if (!uart_circ_empty(xmit)) {
239 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
240 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
241 up->port.icount.tx++;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
245#else
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200246 struct uart_sio_port *up =
247 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (!(up->ier & UART_IER_THRI)) {
250 up->ier |= UART_IER_THRI;
251 serial_out(up, UART_IER, up->ier);
252 }
253#endif
254}
255
256static void m32r_sio_stop_rx(struct uart_port *port)
257{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200258 struct uart_sio_port *up =
259 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 up->ier &= ~UART_IER_RLSI;
262 up->port.read_status_mask &= ~UART_LSR_DR;
263 serial_out(up, UART_IER, up->ier);
264}
265
266static void m32r_sio_enable_ms(struct uart_port *port)
267{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200268 struct uart_sio_port *up =
269 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 up->ier |= UART_IER_MSI;
272 serial_out(up, UART_IER, up->ier);
273}
274
David Howells7d12e782006-10-05 14:55:46 +0100275static void receive_chars(struct uart_sio_port *up, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100277 struct tty_port *port = &up->port.state->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned char ch;
Alan Cox33f0f882006-01-09 20:54:13 -0800279 unsigned char flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int max_count = 256;
281
282 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ch = sio_in(up, SIORXB);
Alan Cox33f0f882006-01-09 20:54:13 -0800284 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 up->port.icount.rx++;
286
287 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
288 UART_LSR_FE | UART_LSR_OE))) {
289 /*
290 * For statistics only
291 */
292 if (*status & UART_LSR_BI) {
293 *status &= ~(UART_LSR_FE | UART_LSR_PE);
294 up->port.icount.brk++;
295 /*
296 * We do the SysRQ and SAK checking
297 * here because otherwise the break
298 * may get masked by ignore_status_mask
299 * or read_status_mask.
300 */
301 if (uart_handle_break(&up->port))
302 goto ignore_char;
303 } else if (*status & UART_LSR_PE)
304 up->port.icount.parity++;
305 else if (*status & UART_LSR_FE)
306 up->port.icount.frame++;
307 if (*status & UART_LSR_OE)
308 up->port.icount.overrun++;
309
310 /*
311 * Mask off conditions which should be ingored.
312 */
313 *status &= up->port.read_status_mask;
314
315 if (up->port.line == up->port.cons->index) {
316 /* Recover the break flag from console xmit */
317 *status |= up->lsr_break_flag;
318 up->lsr_break_flag = 0;
319 }
320
321 if (*status & UART_LSR_BI) {
Jiri Slabyad245aa2016-01-12 10:59:08 +0100322 pr_debug("handling break....\n");
Alan Cox33f0f882006-01-09 20:54:13 -0800323 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else if (*status & UART_LSR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800325 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 else if (*status & UART_LSR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800327 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
David Howells7d12e782006-10-05 14:55:46 +0100329 if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto ignore_char;
Alan Cox33f0f882006-01-09 20:54:13 -0800331 if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100332 tty_insert_flip_char(port, ch, flag);
Alan Cox33f0f882006-01-09 20:54:13 -0800333
334 if (*status & UART_LSR_OE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /*
336 * Overrun is special, since it's reported
337 * immediately, and doesn't affect the current
338 * character.
339 */
Jiri Slaby92a19f92013-01-03 15:53:03 +0100340 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 ignore_char:
343 *status = serial_in(up, UART_LSR);
344 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
Viresh Kumaraf89f832013-08-19 20:14:16 +0530345
346 spin_unlock(&up->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100347 tty_flip_buffer_push(port);
Viresh Kumaraf89f832013-08-19 20:14:16 +0530348 spin_lock(&up->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800351static void transmit_chars(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700353 struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int count;
355
356 if (up->port.x_char) {
357#ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
358 serial_out(up, UART_TX, up->port.x_char);
359#endif
360 up->port.icount.tx++;
361 up->port.x_char = 0;
362 return;
363 }
364 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100365 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367 }
368
369 count = up->port.fifosize;
370 do {
371 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
372 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
373 up->port.icount.tx++;
374 if (uart_circ_empty(xmit))
375 break;
Julia Lawall022d9172008-03-04 14:29:19 -0800376 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 } while (--count > 0);
379
380 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
381 uart_write_wakeup(&up->port);
382
Jiri Slabyad245aa2016-01-12 10:59:08 +0100383 pr_debug("THRE...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100386 m32r_sio_stop_tx(&up->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
390 * This handles the interrupt from one port.
391 */
392static inline void m32r_sio_handle_port(struct uart_sio_port *up,
David Howells7d12e782006-10-05 14:55:46 +0100393 unsigned int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Jiri Slabyad245aa2016-01-12 10:59:08 +0100395 pr_debug("status = %x...\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (status & 0x04)
David Howells7d12e782006-10-05 14:55:46 +0100398 receive_chars(up, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (status & 0x01)
400 transmit_chars(up);
401}
402
403/*
404 * This is the serial driver's interrupt routine.
405 *
406 * Arjan thinks the old way was overly complex, so it got simplified.
407 * Alan disagrees, saying that need the complexity to handle the weird
408 * nature of ISA shared interrupts. (This is a special exception.)
409 *
410 * In order to handle ISA shared interrupts properly, we need to check
411 * that all ports have been serviced, and therefore the ISA interrupt
412 * line has been de-asserted.
413 *
414 * This means we need to loop through all ports. checking that they
415 * don't have an interrupt pending.
416 */
David Howells7d12e782006-10-05 14:55:46 +0100417static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct irq_info *i = dev_id;
420 struct list_head *l, *end = NULL;
421 int pass_counter = 0;
422
Jiri Slabyad245aa2016-01-12 10:59:08 +0100423 pr_debug("m32r_sio_interrupt(%d)...\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425#ifdef CONFIG_SERIAL_M32R_PLDSIO
426// if (irq == PLD_IRQ_SIO0_SND)
427// irq = PLD_IRQ_SIO0_RCV;
428#else
429 if (irq == M32R_IRQ_SIO0_S)
430 irq = M32R_IRQ_SIO0_R;
431#endif
432
433 spin_lock(&i->lock);
434
435 l = i->head;
436 do {
437 struct uart_sio_port *up;
438 unsigned int sts;
439
440 up = list_entry(l, struct uart_sio_port, list);
441
442 sts = sio_in(up, SIOSTS);
443 if (sts & 0x5) {
444 spin_lock(&up->port.lock);
David Howells7d12e782006-10-05 14:55:46 +0100445 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 spin_unlock(&up->port.lock);
447
448 end = NULL;
449 } else if (end == NULL)
450 end = l;
451
452 l = l->next;
453
454 if (l == i->head && pass_counter++ > PASS_LIMIT) {
455 if (sts & 0xe0)
456 sio_error(&sts);
457 break;
458 }
459 } while (l != end);
460
461 spin_unlock(&i->lock);
462
Jiri Slabyad245aa2016-01-12 10:59:08 +0100463 pr_debug("end.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return IRQ_HANDLED;
466}
467
468/*
469 * To support ISA shared interrupts, we need to have one interrupt
470 * handler that ensures that the IRQ line has been deasserted
471 * before returning. Failing to do this will result in the IRQ
472 * line being stuck active, and, since ISA irqs are edge triggered,
473 * no more IRQs will be seen.
474 */
475static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
476{
477 spin_lock_irq(&i->lock);
478
479 if (!list_empty(i->head)) {
480 if (i->head == &up->list)
481 i->head = i->head->next;
482 list_del(&up->list);
483 } else {
484 BUG_ON(i->head != &up->list);
485 i->head = NULL;
486 }
487
488 spin_unlock_irq(&i->lock);
489}
490
491static int serial_link_irq_chain(struct uart_sio_port *up)
492{
493 struct irq_info *i = irq_lists + up->port.irq;
Hirokazu Takatadab8f492007-10-16 01:26:36 -0700494 int ret, irq_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 spin_lock_irq(&i->lock);
497
498 if (i->head) {
499 list_add(&up->list, i->head);
500 spin_unlock_irq(&i->lock);
501
502 ret = 0;
503 } else {
504 INIT_LIST_HEAD(&up->list);
505 i->head = &up->list;
506 spin_unlock_irq(&i->lock);
507
508 ret = request_irq(up->port.irq, m32r_sio_interrupt,
509 irq_flags, "SIO0-RX", i);
510 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
511 irq_flags, "SIO0-TX", i);
512 if (ret < 0)
513 serial_do_unlink(i, up);
514 }
515
516 return ret;
517}
518
519static void serial_unlink_irq_chain(struct uart_sio_port *up)
520{
521 struct irq_info *i = irq_lists + up->port.irq;
522
523 BUG_ON(i->head == NULL);
524
525 if (list_empty(i->head)) {
526 free_irq(up->port.irq, i);
527 free_irq(up->port.irq + 1, i);
528 }
529
530 serial_do_unlink(i, up);
531}
532
533/*
534 * This function is used to handle ports that do not have an interrupt.
535 */
536static void m32r_sio_timeout(unsigned long data)
537{
538 struct uart_sio_port *up = (struct uart_sio_port *)data;
539 unsigned int timeout;
540 unsigned int sts;
541
542 sts = sio_in(up, SIOSTS);
543 if (sts & 0x5) {
544 spin_lock(&up->port.lock);
Al Viro9c8e7f52006-10-07 16:29:18 +0100545 m32r_sio_handle_port(up, sts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_unlock(&up->port.lock);
547 }
548
549 timeout = up->port.timeout;
550 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
551 mod_timer(&up->timer, jiffies + timeout);
552}
553
554static unsigned int m32r_sio_tx_empty(struct uart_port *port)
555{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200556 struct uart_sio_port *up =
557 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned long flags;
559 unsigned int ret;
560
561 spin_lock_irqsave(&up->port.lock, flags);
562 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
563 spin_unlock_irqrestore(&up->port.lock, flags);
564
565 return ret;
566}
567
568static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
569{
570 return 0;
571}
572
573static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
574{
575
576}
577
578static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
579{
580
581}
582
583static int m32r_sio_startup(struct uart_port *port)
584{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200585 struct uart_sio_port *up =
586 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int retval;
588
589 sio_init();
590
591 /*
592 * If the "interrupt" for this port doesn't correspond with any
593 * hardware interrupt, we use a timer-based system. The original
594 * driver used to do this with IRQ0.
595 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000596 if (!up->port.irq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned int timeout = up->port.timeout;
598
599 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
600
601 up->timer.data = (unsigned long)up;
602 mod_timer(&up->timer, jiffies + timeout);
603 } else {
604 retval = serial_link_irq_chain(up);
605 if (retval)
606 return retval;
607 }
608
609 /*
610 * Finally, enable interrupts. Note: Modem status interrupts
611 * are set via set_termios(), which will be occurring imminently
612 * anyway, so we don't enable them here.
613 * - M32R_SIO: 0x0c
614 * - M32R_PLDSIO: 0x04
615 */
616 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
617 sio_out(up, SIOTRCR, up->ier);
618
619 /*
620 * And clear the interrupt registers again for luck.
621 */
622 sio_reset();
623
624 return 0;
625}
626
627static void m32r_sio_shutdown(struct uart_port *port)
628{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200629 struct uart_sio_port *up =
630 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /*
633 * Disable interrupts from this port
634 */
635 up->ier = 0;
636 sio_out(up, SIOTRCR, 0);
637
638 /*
639 * Disable break condition and FIFOs
640 */
641
642 sio_init();
643
Alan Coxd4e33fa2012-01-26 17:44:09 +0000644 if (!up->port.irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 del_timer_sync(&up->timer);
646 else
647 serial_unlink_irq_chain(up);
648}
649
650static unsigned int m32r_sio_get_divisor(struct uart_port *port,
651 unsigned int baud)
652{
653 return uart_get_divisor(port, baud);
654}
655
656static void m32r_sio_set_termios(struct uart_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800657 struct ktermios *termios, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200659 struct uart_sio_port *up =
660 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 unsigned char cval = 0;
662 unsigned long flags;
663 unsigned int baud, quot;
664
665 switch (termios->c_cflag & CSIZE) {
666 case CS5:
Russell King0a8b80c52005-06-24 19:48:22 +0100667 cval = UART_LCR_WLEN5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669 case CS6:
Russell King0a8b80c52005-06-24 19:48:22 +0100670 cval = UART_LCR_WLEN6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 case CS7:
Russell King0a8b80c52005-06-24 19:48:22 +0100673 cval = UART_LCR_WLEN7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
675 default:
676 case CS8:
Russell King0a8b80c52005-06-24 19:48:22 +0100677 cval = UART_LCR_WLEN8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
679 }
680
681 if (termios->c_cflag & CSTOPB)
Russell King0a8b80c52005-06-24 19:48:22 +0100682 cval |= UART_LCR_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (termios->c_cflag & PARENB)
684 cval |= UART_LCR_PARITY;
685 if (!(termios->c_cflag & PARODD))
686 cval |= UART_LCR_EPAR;
687#ifdef CMSPAR
688 if (termios->c_cflag & CMSPAR)
689 cval |= UART_LCR_SPAR;
690#endif
691
692 /*
693 * Ask the core to calculate the divisor for us.
694 */
695#ifdef CONFIG_SERIAL_M32R_PLDSIO
696 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
697#else
698 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
699#endif
700 quot = m32r_sio_get_divisor(port, baud);
701
702 /*
703 * Ok, we're now changing the port state. Do it with
704 * interrupts disabled.
705 */
706 spin_lock_irqsave(&up->port.lock, flags);
707
708 sio_set_baud_rate(baud);
709
710 /*
711 * Update the per-port timeout.
712 */
713 uart_update_timeout(port, termios->c_cflag, baud);
714
715 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
716 if (termios->c_iflag & INPCK)
717 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400718 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 up->port.read_status_mask |= UART_LSR_BI;
720
721 /*
722 * Characteres to ignore
723 */
724 up->port.ignore_status_mask = 0;
725 if (termios->c_iflag & IGNPAR)
726 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
727 if (termios->c_iflag & IGNBRK) {
728 up->port.ignore_status_mask |= UART_LSR_BI;
729 /*
730 * If we're ignoring parity and break indicators,
731 * ignore overruns too (for real raw support).
732 */
733 if (termios->c_iflag & IGNPAR)
734 up->port.ignore_status_mask |= UART_LSR_OE;
735 }
736
737 /*
738 * ignore all characters if CREAD is not set
739 */
740 if ((termios->c_cflag & CREAD) == 0)
741 up->port.ignore_status_mask |= UART_LSR_DR;
742
743 /*
744 * CTS flow control flag and modem status interrupts
745 */
746 up->ier &= ~UART_IER_MSI;
747 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
748 up->ier |= UART_IER_MSI;
749
750 serial_out(up, UART_IER, up->ier);
751
752 up->lcr = cval; /* Save LCR */
753 spin_unlock_irqrestore(&up->port.lock, flags);
754}
755
756static void m32r_sio_pm(struct uart_port *port, unsigned int state,
757 unsigned int oldstate)
758{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200759 struct uart_sio_port *up =
760 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (up->pm)
763 up->pm(port, state, oldstate);
764}
765
766/*
767 * Resource handling. This is complicated by the fact that resources
768 * depend on the port type. Maybe we should be claiming the standard
769 * 8250 ports, and then trying to get other resources as necessary?
770 */
771static int
772m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
773{
774 unsigned int size = 8 << up->port.regshift;
775#ifndef CONFIG_SERIAL_M32R_PLDSIO
776 unsigned long start;
777#endif
778 int ret = 0;
779
780 switch (up->port.iotype) {
781 case UPIO_MEM:
782 if (up->port.mapbase) {
783#ifdef CONFIG_SERIAL_M32R_PLDSIO
784 *res = request_mem_region(up->port.mapbase, size, "serial");
785#else
786 start = up->port.mapbase;
787 *res = request_mem_region(start, size, "serial");
788#endif
789 if (!*res)
790 ret = -EBUSY;
791 }
792 break;
793
794 case UPIO_PORT:
795 *res = request_region(up->port.iobase, size, "serial");
796 if (!*res)
797 ret = -EBUSY;
798 break;
799 }
800 return ret;
801}
802
803static void m32r_sio_release_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 unsigned long start, offset = 0, size = 0;
808
809 size <<= up->port.regshift;
810
811 switch (up->port.iotype) {
812 case UPIO_MEM:
813 if (up->port.mapbase) {
814 /*
815 * Unmap the area.
816 */
817 iounmap(up->port.membase);
818 up->port.membase = NULL;
819
820 start = up->port.mapbase;
821
822 if (size)
823 release_mem_region(start + offset, size);
824 release_mem_region(start, 8 << up->port.regshift);
825 }
826 break;
827
828 case UPIO_PORT:
829 start = up->port.iobase;
830
831 if (size)
832 release_region(start + offset, size);
833 release_region(start + offset, 8 << up->port.regshift);
834 break;
835
836 default:
837 break;
838 }
839}
840
841static int m32r_sio_request_port(struct uart_port *port)
842{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200843 struct uart_sio_port *up =
844 container_of(port, struct uart_sio_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct resource *res = NULL;
846 int ret = 0;
847
848 ret = m32r_sio_request_std_resource(up, &res);
849
850 /*
851 * If we have a mapbase, then request that as well.
852 */
853 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
Joe Perches28f65c112011-06-09 09:13:32 -0700854 int size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 up->port.membase = ioremap(up->port.mapbase, size);
857 if (!up->port.membase)
858 ret = -ENOMEM;
859 }
860
861 if (ret < 0) {
862 if (res)
863 release_resource(res);
864 }
865
866 return ret;
867}
868
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700869static void m32r_sio_config_port(struct uart_port *port, int unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200871 struct uart_sio_port *up =
872 container_of(port, struct uart_sio_port, port);
KOSAKI Motohiro1e806c52011-05-26 16:25:00 -0700873 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 spin_lock_irqsave(&up->port.lock, flags);
876
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400877 up->port.fifosize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 spin_unlock_irqrestore(&up->port.lock, flags);
880}
881
882static int
883m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
884{
Paul Gortmakerb8ede902012-08-20 19:56:26 -0400885 if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EINVAL;
887 return 0;
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static struct uart_ops m32r_sio_pops = {
891 .tx_empty = m32r_sio_tx_empty,
892 .set_mctrl = m32r_sio_set_mctrl,
893 .get_mctrl = m32r_sio_get_mctrl,
894 .stop_tx = m32r_sio_stop_tx,
895 .start_tx = m32r_sio_start_tx,
896 .stop_rx = m32r_sio_stop_rx,
897 .enable_ms = m32r_sio_enable_ms,
898 .break_ctl = m32r_sio_break_ctl,
899 .startup = m32r_sio_startup,
900 .shutdown = m32r_sio_shutdown,
901 .set_termios = m32r_sio_set_termios,
902 .pm = m32r_sio_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .release_port = m32r_sio_release_port,
904 .request_port = m32r_sio_request_port,
905 .config_port = m32r_sio_config_port,
906 .verify_port = m32r_sio_verify_port,
907};
908
909static struct uart_sio_port m32r_sio_ports[UART_NR];
910
911static void __init m32r_sio_init_ports(void)
912{
913 struct uart_sio_port *up;
914 static int first = 1;
915 int i;
916
917 if (!first)
918 return;
919 first = 0;
920
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100921 for (i = 0, up = m32r_sio_ports; i < UART_NR; i++, up++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 up->port.iobase = old_serial_port[i].port;
923 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyc8e7d142016-01-12 10:59:07 +0100924 up->port.uartclk = BAUD_RATE * 16;
925 up->port.flags = STD_COM_FLAGS;
926 up->port.membase = 0;
927 up->port.iotype = 0;
928 up->port.regshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 up->port.ops = &m32r_sio_pops;
930 }
931}
932
933static void __init m32r_sio_register_ports(struct uart_driver *drv)
934{
935 int i;
936
937 m32r_sio_init_ports();
938
939 for (i = 0; i < UART_NR; i++) {
940 struct uart_sio_port *up = &m32r_sio_ports[i];
941
942 up->port.line = i;
943 up->port.ops = &m32r_sio_pops;
944 init_timer(&up->timer);
945 up->timer.function = m32r_sio_timeout;
946
Paul Gortmaker59087382012-01-04 15:16:24 -0500947 up->mcr_mask = ~0;
948 up->mcr_force = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 uart_add_one_port(drv, &up->port);
951 }
952}
953
954#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
955
956/*
957 * Wait for transmitter & holding register to empty
958 */
Denys Vlasenko9cdb9332015-10-27 18:46:37 +0100959static void wait_for_xmitr(struct uart_sio_port *up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 unsigned int status, tmout = 10000;
962
963 /* Wait up to 10ms for the character(s) to be sent. */
964 do {
965 status = sio_in(up, SIOSTS);
966
967 if (--tmout == 0)
968 break;
969 udelay(1);
970 } while ((status & UART_EMPTY) != UART_EMPTY);
971
972 /* Wait up to 1s for flow control if necessary */
973 if (up->port.flags & UPF_CONS_FLOW) {
974 tmout = 1000000;
975 while (--tmout)
976 udelay(1);
977 }
978}
979
Russell Kingd3587882006-03-20 20:00:09 +0000980static void m32r_sio_console_putchar(struct uart_port *port, int ch)
981{
Fabian Frederickb6b30d62014-10-05 19:01:02 +0200982 struct uart_sio_port *up =
983 container_of(port, struct uart_sio_port, port);
Russell Kingd3587882006-03-20 20:00:09 +0000984
985 wait_for_xmitr(up);
986 sio_out(up, SIOTXB, ch);
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
990 * Print a string to the serial port trying not to disturb
991 * any possible real use of the port...
992 *
993 * The console_lock must be held when we get here.
994 */
995static void m32r_sio_console_write(struct console *co, const char *s,
996 unsigned int count)
997{
998 struct uart_sio_port *up = &m32r_sio_ports[co->index];
999 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /*
1002 * First save the UER then disable the interrupts
1003 */
1004 ier = sio_in(up, SIOTRCR);
1005 sio_out(up, SIOTRCR, 0);
1006
Russell Kingd3587882006-03-20 20:00:09 +00001007 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /*
1010 * Finally, wait for transmitter to become empty
1011 * and restore the IER
1012 */
1013 wait_for_xmitr(up);
1014 sio_out(up, SIOTRCR, ier);
1015}
1016
1017static int __init m32r_sio_console_setup(struct console *co, char *options)
1018{
1019 struct uart_port *port;
1020 int baud = 9600;
1021 int bits = 8;
1022 int parity = 'n';
1023 int flow = 'n';
1024
1025 /*
1026 * Check whether an invalid uart number has been specified, and
1027 * if so, search for the first available port that does have
1028 * console support.
1029 */
1030 if (co->index >= UART_NR)
1031 co->index = 0;
1032 port = &m32r_sio_ports[co->index].port;
1033
1034 /*
1035 * Temporary fix.
1036 */
1037 spin_lock_init(&port->lock);
1038
1039 if (options)
1040 uart_parse_options(options, &baud, &parity, &bits, &flow);
1041
1042 return uart_set_options(port, co, baud, parity, bits, flow);
1043}
1044
Al Viroa828b8e2005-08-23 22:47:27 +01001045static struct uart_driver m32r_sio_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static struct console m32r_sio_console = {
1047 .name = "ttyS",
1048 .write = m32r_sio_console_write,
1049 .device = uart_console_device,
1050 .setup = m32r_sio_console_setup,
1051 .flags = CON_PRINTBUFFER,
1052 .index = -1,
1053 .data = &m32r_sio_reg,
1054};
1055
1056static int __init m32r_sio_console_init(void)
1057{
1058 sio_reset();
1059 sio_init();
1060 m32r_sio_init_ports();
1061 register_console(&m32r_sio_console);
1062 return 0;
1063}
1064console_initcall(m32r_sio_console_init);
1065
1066#define M32R_SIO_CONSOLE &m32r_sio_console
1067#else
1068#define M32R_SIO_CONSOLE NULL
1069#endif
1070
1071static struct uart_driver m32r_sio_reg = {
1072 .owner = THIS_MODULE,
1073 .driver_name = "sio",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 .dev_name = "ttyS",
1075 .major = TTY_MAJOR,
1076 .minor = 64,
1077 .nr = UART_NR,
1078 .cons = M32R_SIO_CONSOLE,
1079};
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static int __init m32r_sio_init(void)
1082{
1083 int ret, i;
1084
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001085 printk(KERN_INFO "Serial: M32R SIO driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Yinghai Lua62c4132008-08-19 20:49:55 -07001087 for (i = 0; i < nr_irqs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_lock_init(&irq_lists[i].lock);
1089
1090 ret = uart_register_driver(&m32r_sio_reg);
1091 if (ret >= 0)
1092 m32r_sio_register_ports(&m32r_sio_reg);
1093
1094 return ret;
1095}
1096
1097static void __exit m32r_sio_exit(void)
1098{
1099 int i;
1100
1101 for (i = 0; i < UART_NR; i++)
1102 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1103
1104 uart_unregister_driver(&m32r_sio_reg);
1105}
1106
1107module_init(m32r_sio_init);
1108module_exit(m32r_sio_exit);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001111MODULE_DESCRIPTION("Generic M32R SIO serial driver");