blob: 4968cfe66c068f4f1f6301b038b9098b5f933d14 [file] [log] [blame]
David Howellsb920de12008-02-08 04:19:31 -08001/* MN10300 On-chip serial port UART driver
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12static const char serial_name[] = "MN10300 Serial driver";
13static const char serial_version[] = "mn10300_serial-1.0";
14static const char serial_revdate[] = "2007-11-06";
15
16#if defined(CONFIG_MN10300_TTYSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17#define SUPPORT_SYSRQ
18#endif
19
David Howellsb920de12008-02-08 04:19:31 -080020#include <linux/module.h>
21#include <linux/serial.h>
22#include <linux/circ_buf.h>
23#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/major.h>
31#include <linux/string.h>
32#include <linux/ioport.h>
33#include <linux/mm.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
38
David Howellsb920de12008-02-08 04:19:31 -080039#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/bitops.h>
42#include <asm/serial-regs.h>
David Howells2f2a2132009-04-10 14:33:48 +010043#include <unit/timex.h>
David Howellsb920de12008-02-08 04:19:31 -080044#include "mn10300-serial.h"
45
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010046#ifdef CONFIG_SMP
47#undef GxICR
48#define GxICR(X) CROSS_GxICR(X, 0)
49#endif /* CONFIG_SMP */
50
David Howellsb920de12008-02-08 04:19:31 -080051#define kenter(FMT, ...) \
52 printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
53#define _enter(FMT, ...) \
54 no_printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
55#define kdebug(FMT, ...) \
56 printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
57#define _debug(FMT, ...) \
58 no_printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
59#define kproto(FMT, ...) \
60 printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
61#define _proto(FMT, ...) \
62 no_printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
63
Akira Takeuchi368dd5a2010-10-27 17:28:55 +010064#ifndef CODMSB
65/* c_cflag bit meaning */
66#define CODMSB 004000000000 /* change Transfer bit-order */
67#endif
68
David Howellsb920de12008-02-08 04:19:31 -080069#define NR_UARTS 3
70
71#ifdef CONFIG_MN10300_TTYSM_CONSOLE
72static void mn10300_serial_console_write(struct console *co,
73 const char *s, unsigned count);
74static int __init mn10300_serial_console_setup(struct console *co,
75 char *options);
76
77static struct uart_driver mn10300_serial_driver;
78static struct console mn10300_serial_console = {
79 .name = "ttySM",
80 .write = mn10300_serial_console_write,
81 .device = uart_console_device,
82 .setup = mn10300_serial_console_setup,
83 .flags = CON_PRINTBUFFER,
84 .index = -1,
85 .data = &mn10300_serial_driver,
86};
87#endif
88
89static struct uart_driver mn10300_serial_driver = {
90 .owner = NULL,
91 .driver_name = "mn10300-serial",
92 .dev_name = "ttySM",
93 .major = TTY_MAJOR,
94 .minor = 128,
95 .nr = NR_UARTS,
96#ifdef CONFIG_MN10300_TTYSM_CONSOLE
97 .cons = &mn10300_serial_console,
98#endif
99};
100
101static unsigned int mn10300_serial_tx_empty(struct uart_port *);
102static void mn10300_serial_set_mctrl(struct uart_port *, unsigned int mctrl);
103static unsigned int mn10300_serial_get_mctrl(struct uart_port *);
104static void mn10300_serial_stop_tx(struct uart_port *);
105static void mn10300_serial_start_tx(struct uart_port *);
106static void mn10300_serial_send_xchar(struct uart_port *, char ch);
107static void mn10300_serial_stop_rx(struct uart_port *);
108static void mn10300_serial_enable_ms(struct uart_port *);
109static void mn10300_serial_break_ctl(struct uart_port *, int ctl);
110static int mn10300_serial_startup(struct uart_port *);
111static void mn10300_serial_shutdown(struct uart_port *);
112static void mn10300_serial_set_termios(struct uart_port *,
113 struct ktermios *new,
114 struct ktermios *old);
115static const char *mn10300_serial_type(struct uart_port *);
116static void mn10300_serial_release_port(struct uart_port *);
117static int mn10300_serial_request_port(struct uart_port *);
118static void mn10300_serial_config_port(struct uart_port *, int);
119static int mn10300_serial_verify_port(struct uart_port *,
120 struct serial_struct *);
David Howells52885b32011-03-18 16:54:32 +0000121#ifdef CONFIG_CONSOLE_POLL
122static void mn10300_serial_poll_put_char(struct uart_port *, unsigned char);
123static int mn10300_serial_poll_get_char(struct uart_port *);
124#endif
David Howellsb920de12008-02-08 04:19:31 -0800125
126static const struct uart_ops mn10300_serial_ops = {
127 .tx_empty = mn10300_serial_tx_empty,
128 .set_mctrl = mn10300_serial_set_mctrl,
129 .get_mctrl = mn10300_serial_get_mctrl,
130 .stop_tx = mn10300_serial_stop_tx,
131 .start_tx = mn10300_serial_start_tx,
132 .send_xchar = mn10300_serial_send_xchar,
133 .stop_rx = mn10300_serial_stop_rx,
134 .enable_ms = mn10300_serial_enable_ms,
135 .break_ctl = mn10300_serial_break_ctl,
136 .startup = mn10300_serial_startup,
137 .shutdown = mn10300_serial_shutdown,
138 .set_termios = mn10300_serial_set_termios,
139 .type = mn10300_serial_type,
140 .release_port = mn10300_serial_release_port,
141 .request_port = mn10300_serial_request_port,
142 .config_port = mn10300_serial_config_port,
143 .verify_port = mn10300_serial_verify_port,
David Howells52885b32011-03-18 16:54:32 +0000144#ifdef CONFIG_CONSOLE_POLL
145 .poll_put_char = mn10300_serial_poll_put_char,
146 .poll_get_char = mn10300_serial_poll_get_char,
147#endif
David Howellsb920de12008-02-08 04:19:31 -0800148};
149
150static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id);
151
152/*
153 * the first on-chip serial port: ttySM0 (aka SIF0)
154 */
155#ifdef CONFIG_MN10300_TTYSM0
156struct mn10300_serial_port mn10300_serial_port_sif0 = {
157 .uart.ops = &mn10300_serial_ops,
158 .uart.membase = (void __iomem *) &SC0CTR,
159 .uart.mapbase = (unsigned long) &SC0CTR,
160 .uart.iotype = UPIO_MEM,
161 .uart.irq = 0,
162 .uart.uartclk = 0, /* MN10300_IOCLK, */
163 .uart.fifosize = 1,
164 .uart.flags = UPF_BOOT_AUTOCONF,
165 .uart.line = 0,
166 .uart.type = PORT_MN10300,
167 .uart.lock =
168 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif0.uart.lock),
169 .name = "ttySM0",
170 ._iobase = &SC0CTR,
171 ._control = &SC0CTR,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100172 ._status = (volatile u8 *)&SC0STR,
David Howellsb920de12008-02-08 04:19:31 -0800173 ._intr = &SC0ICR,
174 ._rxb = &SC0RXB,
175 ._txb = &SC0TXB,
David Howellsa4128b02010-09-14 17:59:15 +0100176 .rx_name = "ttySM0:Rx",
177 .tx_name = "ttySM0:Tx",
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100178#if defined(CONFIG_MN10300_TTYSM0_TIMER8)
David Howellsa4128b02010-09-14 17:59:15 +0100179 .tm_name = "ttySM0:Timer8",
David Howellsb920de12008-02-08 04:19:31 -0800180 ._tmxmd = &TM8MD,
181 ._tmxbr = &TM8BR,
182 ._tmicr = &TM8ICR,
183 .tm_irq = TM8IRQ,
184 .div_timer = MNSCx_DIV_TIMER_16BIT,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100185#elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
186 .tm_name = "ttySM0:Timer0",
187 ._tmxmd = &TM0MD,
188 ._tmxbr = (volatile u16 *)&TM0BR,
189 ._tmicr = &TM0ICR,
190 .tm_irq = TM0IRQ,
191 .div_timer = MNSCx_DIV_TIMER_8BIT,
192#elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
David Howellsa4128b02010-09-14 17:59:15 +0100193 .tm_name = "ttySM0:Timer2",
David Howellsb920de12008-02-08 04:19:31 -0800194 ._tmxmd = &TM2MD,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100195 ._tmxbr = (volatile u16 *)&TM2BR,
David Howellsb920de12008-02-08 04:19:31 -0800196 ._tmicr = &TM2ICR,
197 .tm_irq = TM2IRQ,
198 .div_timer = MNSCx_DIV_TIMER_8BIT,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100199#else
200#error "Unknown config for ttySM0"
David Howellsb920de12008-02-08 04:19:31 -0800201#endif
202 .rx_irq = SC0RXIRQ,
203 .tx_irq = SC0TXIRQ,
204 .rx_icr = &GxICR(SC0RXIRQ),
205 .tx_icr = &GxICR(SC0TXIRQ),
206 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
207 .options = 0,
208#ifdef CONFIG_GDBSTUB_ON_TTYSM0
209 .gdbstub = 1,
210#endif
211};
212#endif /* CONFIG_MN10300_TTYSM0 */
213
214/*
215 * the second on-chip serial port: ttySM1 (aka SIF1)
216 */
217#ifdef CONFIG_MN10300_TTYSM1
218struct mn10300_serial_port mn10300_serial_port_sif1 = {
219 .uart.ops = &mn10300_serial_ops,
220 .uart.membase = (void __iomem *) &SC1CTR,
221 .uart.mapbase = (unsigned long) &SC1CTR,
222 .uart.iotype = UPIO_MEM,
223 .uart.irq = 0,
224 .uart.uartclk = 0, /* MN10300_IOCLK, */
225 .uart.fifosize = 1,
226 .uart.flags = UPF_BOOT_AUTOCONF,
227 .uart.line = 1,
228 .uart.type = PORT_MN10300,
229 .uart.lock =
230 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif1.uart.lock),
231 .name = "ttySM1",
232 ._iobase = &SC1CTR,
233 ._control = &SC1CTR,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100234 ._status = (volatile u8 *)&SC1STR,
David Howellsb920de12008-02-08 04:19:31 -0800235 ._intr = &SC1ICR,
236 ._rxb = &SC1RXB,
237 ._txb = &SC1TXB,
David Howellsa4128b02010-09-14 17:59:15 +0100238 .rx_name = "ttySM1:Rx",
239 .tx_name = "ttySM1:Tx",
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100240#if defined(CONFIG_MN10300_TTYSM1_TIMER9)
David Howellsa4128b02010-09-14 17:59:15 +0100241 .tm_name = "ttySM1:Timer9",
David Howellsb920de12008-02-08 04:19:31 -0800242 ._tmxmd = &TM9MD,
243 ._tmxbr = &TM9BR,
244 ._tmicr = &TM9ICR,
245 .tm_irq = TM9IRQ,
246 .div_timer = MNSCx_DIV_TIMER_16BIT,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100247#elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
David Howellsa4128b02010-09-14 17:59:15 +0100248 .tm_name = "ttySM1:Timer3",
David Howellsb920de12008-02-08 04:19:31 -0800249 ._tmxmd = &TM3MD,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100250 ._tmxbr = (volatile u16 *)&TM3BR,
David Howellsb920de12008-02-08 04:19:31 -0800251 ._tmicr = &TM3ICR,
252 .tm_irq = TM3IRQ,
253 .div_timer = MNSCx_DIV_TIMER_8BIT,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100254#elif defined(CONFIG_MN10300_TTYSM1_TIMER12)
255 .tm_name = "ttySM1/Timer12",
256 ._tmxmd = &TM12MD,
257 ._tmxbr = &TM12BR,
258 ._tmicr = &TM12ICR,
259 .tm_irq = TM12IRQ,
260 .div_timer = MNSCx_DIV_TIMER_16BIT,
261#else
262#error "Unknown config for ttySM1"
David Howellsb920de12008-02-08 04:19:31 -0800263#endif
264 .rx_irq = SC1RXIRQ,
265 .tx_irq = SC1TXIRQ,
266 .rx_icr = &GxICR(SC1RXIRQ),
267 .tx_icr = &GxICR(SC1TXIRQ),
268 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
269 .options = 0,
270#ifdef CONFIG_GDBSTUB_ON_TTYSM1
271 .gdbstub = 1,
272#endif
273};
274#endif /* CONFIG_MN10300_TTYSM1 */
275
276/*
277 * the third on-chip serial port: ttySM2 (aka SIF2)
278 */
279#ifdef CONFIG_MN10300_TTYSM2
280struct mn10300_serial_port mn10300_serial_port_sif2 = {
281 .uart.ops = &mn10300_serial_ops,
282 .uart.membase = (void __iomem *) &SC2CTR,
283 .uart.mapbase = (unsigned long) &SC2CTR,
284 .uart.iotype = UPIO_MEM,
285 .uart.irq = 0,
286 .uart.uartclk = 0, /* MN10300_IOCLK, */
287 .uart.fifosize = 1,
288 .uart.flags = UPF_BOOT_AUTOCONF,
289 .uart.line = 2,
290#ifdef CONFIG_MN10300_TTYSM2_CTS
291 .uart.type = PORT_MN10300_CTS,
292#else
293 .uart.type = PORT_MN10300,
294#endif
295 .uart.lock =
296 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif2.uart.lock),
297 .name = "ttySM2",
David Howellsb920de12008-02-08 04:19:31 -0800298 ._iobase = &SC2CTR,
299 ._control = &SC2CTR,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100300 ._status = (volatile u8 *)&SC2STR,
David Howellsb920de12008-02-08 04:19:31 -0800301 ._intr = &SC2ICR,
302 ._rxb = &SC2RXB,
303 ._txb = &SC2TXB,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100304 .rx_name = "ttySM2:Rx",
305 .tx_name = "ttySM2:Tx",
306#if defined(CONFIG_MN10300_TTYSM2_TIMER10)
307 .tm_name = "ttySM2/Timer10",
David Howellsb920de12008-02-08 04:19:31 -0800308 ._tmxmd = &TM10MD,
309 ._tmxbr = &TM10BR,
310 ._tmicr = &TM10ICR,
311 .tm_irq = TM10IRQ,
312 .div_timer = MNSCx_DIV_TIMER_16BIT,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100313#elif defined(CONFIG_MN10300_TTYSM2_TIMER9)
314 .tm_name = "ttySM2/Timer9",
315 ._tmxmd = &TM9MD,
316 ._tmxbr = &TM9BR,
317 ._tmicr = &TM9ICR,
318 .tm_irq = TM9IRQ,
319 .div_timer = MNSCx_DIV_TIMER_16BIT,
320#elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
321 .tm_name = "ttySM2/Timer1",
322 ._tmxmd = &TM1MD,
323 ._tmxbr = (volatile u16 *)&TM1BR,
324 ._tmicr = &TM1ICR,
325 .tm_irq = TM1IRQ,
326 .div_timer = MNSCx_DIV_TIMER_8BIT,
327#elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
328 .tm_name = "ttySM2/Timer3",
329 ._tmxmd = &TM3MD,
330 ._tmxbr = (volatile u16 *)&TM3BR,
331 ._tmicr = &TM3ICR,
332 .tm_irq = TM3IRQ,
333 .div_timer = MNSCx_DIV_TIMER_8BIT,
334#else
335#error "Unknown config for ttySM2"
336#endif
David Howellsb920de12008-02-08 04:19:31 -0800337 .rx_irq = SC2RXIRQ,
338 .tx_irq = SC2TXIRQ,
339 .rx_icr = &GxICR(SC2RXIRQ),
340 .tx_icr = &GxICR(SC2TXIRQ),
341 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
342#ifdef CONFIG_MN10300_TTYSM2_CTS
343 .options = MNSCx_OPT_CTS,
344#else
345 .options = 0,
346#endif
347#ifdef CONFIG_GDBSTUB_ON_TTYSM2
348 .gdbstub = 1,
349#endif
350};
351#endif /* CONFIG_MN10300_TTYSM2 */
352
353
354/*
355 * list of available serial ports
356 */
357struct mn10300_serial_port *mn10300_serial_ports[NR_UARTS + 1] = {
358#ifdef CONFIG_MN10300_TTYSM0
359 [0] = &mn10300_serial_port_sif0,
360#endif
361#ifdef CONFIG_MN10300_TTYSM1
362 [1] = &mn10300_serial_port_sif1,
363#endif
364#ifdef CONFIG_MN10300_TTYSM2
365 [2] = &mn10300_serial_port_sif2,
366#endif
367 [NR_UARTS] = NULL,
368};
369
370
371/*
372 * we abuse the serial ports' baud timers' interrupt lines to get the ability
373 * to deliver interrupts to userspace as we use the ports' interrupt lines to
374 * do virtual DMA on account of the ports having no hardware FIFOs
375 *
376 * we can generate an interrupt manually in the assembly stubs by writing to
377 * the enable and detect bits in the interrupt control register, so all we need
378 * to do here is disable the interrupt line
379 *
380 * note that we can't just leave the line enabled as the baud rate timer *also*
381 * generates interrupts
382 */
383static void mn10300_serial_mask_ack(unsigned int irq)
384{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100385 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800386 u16 tmp;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100387
388 flags = arch_local_cli_save();
David Howellsb920de12008-02-08 04:19:31 -0800389 GxICR(irq) = GxICR_LEVEL_6;
390 tmp = GxICR(irq); /* flush write buffer */
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100391 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800392}
393
Thomas Gleixnerb023ba52011-03-18 16:52:52 +0000394static void mn10300_serial_chip_mask_ack(struct irq_data *d)
395{
396 mn10300_serial_mask_ack(d->irq);
397}
398
399static void mn10300_serial_nop(struct irq_data *d)
David Howellsb920de12008-02-08 04:19:31 -0800400{
401}
402
403static struct irq_chip mn10300_serial_pic = {
404 .name = "mnserial",
Thomas Gleixnerb023ba52011-03-18 16:52:52 +0000405 .irq_ack = mn10300_serial_chip_mask_ack,
406 .irq_mask = mn10300_serial_chip_mask_ack,
407 .irq_mask_ack = mn10300_serial_chip_mask_ack,
408 .irq_unmask = mn10300_serial_nop,
David Howellsb920de12008-02-08 04:19:31 -0800409};
410
411
412/*
413 * serial virtual DMA interrupt jump table
414 */
415struct mn10300_serial_int mn10300_serial_int_tbl[NR_IRQS];
416
417static void mn10300_serial_dis_tx_intr(struct mn10300_serial_port *port)
418{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100419 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800420 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100421
422 flags = arch_local_cli_save();
423 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -0800424 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100425 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800426}
427
428static void mn10300_serial_en_tx_intr(struct mn10300_serial_port *port)
429{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100430 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800431 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100432
433 flags = arch_local_cli_save();
434 *port->tx_icr =
435 NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL) | GxICR_ENABLE;
David Howellsb920de12008-02-08 04:19:31 -0800436 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100437 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800438}
439
440static void mn10300_serial_dis_rx_intr(struct mn10300_serial_port *port)
441{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100442 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800443 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100444
445 flags = arch_local_cli_save();
446 *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -0800447 x = *port->rx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100448 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800449}
450
451/*
452 * multi-bit equivalent of test_and_clear_bit()
453 */
454static int mask_test_and_clear(volatile u8 *ptr, u8 mask)
455{
456 u32 epsw;
457 asm volatile(" bclr %1,(%2) \n"
458 " mov epsw,%0 \n"
Mark Salterd6bb7a12010-01-08 14:43:16 -0800459 : "=d"(epsw) : "d"(mask), "a"(ptr)
460 : "cc", "memory");
David Howellsb920de12008-02-08 04:19:31 -0800461 return !(epsw & EPSW_FLAG_Z);
462}
463
464/*
465 * receive chars from the ring buffer for this serial port
466 * - must do break detection here (not done in the UART)
467 */
468static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
469{
470 struct uart_icount *icount = &port->uart.icount;
David Howells70430782009-09-23 10:40:24 +0100471 struct tty_struct *tty = port->uart.state->port.tty;
David Howellsb920de12008-02-08 04:19:31 -0800472 unsigned ix;
473 int count;
474 u8 st, ch, push, status, overrun;
475
476 _enter("%s", port->name);
477
478 push = 0;
479
480 count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
481 count = tty_buffer_request_room(tty, count);
482 if (count == 0) {
483 if (!tty->low_latency)
484 tty_flip_buffer_push(tty);
485 return;
486 }
487
488try_again:
489 /* pull chars out of the hat */
David Howellsc98c4062012-12-12 15:36:37 +0000490 ix = ACCESS_ONCE(port->rx_outp);
491 if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0) {
David Howellsb920de12008-02-08 04:19:31 -0800492 if (push && !tty->low_latency)
493 tty_flip_buffer_push(tty);
494 return;
495 }
496
David Howellsc98c4062012-12-12 15:36:37 +0000497 smp_read_barrier_depends();
David Howellsb920de12008-02-08 04:19:31 -0800498 ch = port->rx_buffer[ix++];
499 st = port->rx_buffer[ix++];
David Howellsc98c4062012-12-12 15:36:37 +0000500 smp_mb();
David Howellsb920de12008-02-08 04:19:31 -0800501 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
502 port->uart.icount.rx++;
503
504 st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
505 status = 0;
506 overrun = 0;
507
508 /* the UART doesn't detect BREAK, so we have to do that ourselves
509 * - it starts as a framing error on a NUL character
510 * - then we count another two NUL characters before issuing TTY_BREAK
511 * - then we end on a normal char or one that has all the bottom bits
512 * zero and the top bits set
513 */
514 switch (port->rx_brk) {
515 case 0:
516 /* not breaking at the moment */
517 break;
518
519 case 1:
520 if (st & SC01STR_FEF && ch == 0) {
521 port->rx_brk = 2;
522 goto try_again;
523 }
524 goto not_break;
525
526 case 2:
527 if (st & SC01STR_FEF && ch == 0) {
528 port->rx_brk = 3;
529 _proto("Rx Break Detected");
530 icount->brk++;
531 if (uart_handle_break(&port->uart))
532 goto ignore_char;
533 status |= 1 << TTY_BREAK;
534 goto insert;
535 }
536 goto not_break;
537
538 default:
539 if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
540 goto try_again; /* still breaking */
541
542 port->rx_brk = 0; /* end of the break */
543
544 switch (ch) {
545 case 0xFF:
546 case 0xFE:
547 case 0xFC:
548 case 0xF8:
549 case 0xF0:
550 case 0xE0:
551 case 0xC0:
552 case 0x80:
553 case 0x00:
554 /* discard char at probable break end */
555 goto try_again;
556 }
557 break;
558 }
559
560process_errors:
561 /* handle framing error */
562 if (st & SC01STR_FEF) {
563 if (ch == 0) {
564 /* framing error with NUL char is probably a BREAK */
565 port->rx_brk = 1;
566 goto try_again;
567 }
568
569 _proto("Rx Framing Error");
570 icount->frame++;
571 status |= 1 << TTY_FRAME;
572 }
573
574 /* handle parity error */
575 if (st & SC01STR_PEF) {
576 _proto("Rx Parity Error");
577 icount->parity++;
578 status = TTY_PARITY;
579 }
580
581 /* handle normal char */
582 if (status == 0) {
583 if (uart_handle_sysrq_char(&port->uart, ch))
584 goto ignore_char;
585 status = (1 << TTY_NORMAL);
586 }
587
588 /* handle overrun error */
589 if (st & SC01STR_OEF) {
590 if (port->rx_brk)
591 goto try_again;
592
593 _proto("Rx Overrun Error");
594 icount->overrun++;
595 overrun = 1;
596 }
597
598insert:
599 status &= port->uart.read_status_mask;
600
601 if (!overrun && !(status & port->uart.ignore_status_mask)) {
602 int flag;
603
604 if (status & (1 << TTY_BREAK))
605 flag = TTY_BREAK;
606 else if (status & (1 << TTY_PARITY))
607 flag = TTY_PARITY;
608 else if (status & (1 << TTY_FRAME))
609 flag = TTY_FRAME;
610 else
611 flag = TTY_NORMAL;
612
613 tty_insert_flip_char(tty, ch, flag);
614 }
615
616 /* overrun is special, since it's reported immediately, and doesn't
617 * affect the current character
618 */
619 if (overrun)
620 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
621
622 count--;
623 if (count <= 0) {
624 if (!tty->low_latency)
625 tty_flip_buffer_push(tty);
626 return;
627 }
628
629ignore_char:
630 push = 1;
631 goto try_again;
632
633not_break:
634 port->rx_brk = 0;
635 goto process_errors;
636}
637
638/*
639 * handle an interrupt from the serial transmission "virtual DMA" driver
640 * - note: the interrupt routine will disable its own interrupts when the Tx
641 * buffer is empty
642 */
643static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
644{
645 _enter("%s", port->name);
646
David Howells70430782009-09-23 10:40:24 +0100647 if (!port->uart.state || !port->uart.state->port.tty) {
Akira Takeuchia8893fb2008-12-10 12:43:24 +0000648 mn10300_serial_dis_tx_intr(port);
649 return;
650 }
651
David Howellsb920de12008-02-08 04:19:31 -0800652 if (uart_tx_stopped(&port->uart) ||
David Howells70430782009-09-23 10:40:24 +0100653 uart_circ_empty(&port->uart.state->xmit))
David Howellsb920de12008-02-08 04:19:31 -0800654 mn10300_serial_dis_tx_intr(port);
655
David Howells70430782009-09-23 10:40:24 +0100656 if (uart_circ_chars_pending(&port->uart.state->xmit) < WAKEUP_CHARS)
David Howellsb920de12008-02-08 04:19:31 -0800657 uart_write_wakeup(&port->uart);
658}
659
660/*
661 * deal with a change in the status of the CTS line
662 */
663static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
664{
665 u16 ctr;
666
667 port->tx_cts = st;
668 port->uart.icount.cts++;
669
670 /* flip the CTS state selector flag to interrupt when it changes
671 * back */
672 ctr = *port->_control;
673 ctr ^= SC2CTR_TWS;
674 *port->_control = ctr;
675
676 uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
David Howells70430782009-09-23 10:40:24 +0100677 wake_up_interruptible(&port->uart.state->port.delta_msr_wait);
David Howellsb920de12008-02-08 04:19:31 -0800678}
679
680/*
681 * handle a virtual interrupt generated by the lower level "virtual DMA"
682 * routines (irq is the baud timer interrupt)
683 */
684static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
685{
686 struct mn10300_serial_port *port = dev_id;
687 u8 st;
688
689 spin_lock(&port->uart.lock);
690
691 if (port->intr_flags) {
692 _debug("INT %s: %x", port->name, port->intr_flags);
693
694 if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
695 mn10300_serial_receive_interrupt(port);
696
697 if (mask_test_and_clear(&port->intr_flags,
698 MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
699 mn10300_serial_transmit_interrupt(port);
700 }
701
702 /* the only modem control line amongst the whole lot is CTS on
703 * serial port 2 */
704 if (port->type == PORT_MN10300_CTS) {
705 st = *port->_status;
706 if ((port->tx_cts ^ st) & SC2STR_CTS)
707 mn10300_serial_cts_changed(port, st);
708 }
709
710 spin_unlock(&port->uart.lock);
711
712 return IRQ_HANDLED;
713}
714
715/*
716 * return indication of whether the hardware transmit buffer is empty
717 */
718static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
719{
720 struct mn10300_serial_port *port =
721 container_of(_port, struct mn10300_serial_port, uart);
722
723 _enter("%s", port->name);
724
725 return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
726 0 : TIOCSER_TEMT;
727}
728
729/*
730 * set the modem control lines (we don't have any)
731 */
732static void mn10300_serial_set_mctrl(struct uart_port *_port,
733 unsigned int mctrl)
734{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100735 struct mn10300_serial_port *port __attribute__ ((unused)) =
David Howellsb920de12008-02-08 04:19:31 -0800736 container_of(_port, struct mn10300_serial_port, uart);
737
738 _enter("%s,%x", port->name, mctrl);
739}
740
741/*
742 * get the modem control line statuses
743 */
744static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
745{
746 struct mn10300_serial_port *port =
747 container_of(_port, struct mn10300_serial_port, uart);
748
749 _enter("%s", port->name);
750
751 if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
752 return TIOCM_CAR | TIOCM_DSR;
753
754 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
755}
756
757/*
758 * stop transmitting characters
759 */
760static void mn10300_serial_stop_tx(struct uart_port *_port)
761{
762 struct mn10300_serial_port *port =
763 container_of(_port, struct mn10300_serial_port, uart);
764
765 _enter("%s", port->name);
766
767 /* disable the virtual DMA */
768 mn10300_serial_dis_tx_intr(port);
769}
770
771/*
772 * start transmitting characters
773 * - jump-start transmission if it has stalled
774 * - enable the serial Tx interrupt (used by the virtual DMA controller)
775 * - force an interrupt to happen if necessary
776 */
777static void mn10300_serial_start_tx(struct uart_port *_port)
778{
779 struct mn10300_serial_port *port =
780 container_of(_port, struct mn10300_serial_port, uart);
781
782 u16 x;
783
784 _enter("%s{%lu}",
785 port->name,
David Howells70430782009-09-23 10:40:24 +0100786 CIRC_CNT(&port->uart.state->xmit.head,
787 &port->uart.state->xmit.tail,
David Howellsb920de12008-02-08 04:19:31 -0800788 UART_XMIT_SIZE));
789
790 /* kick the virtual DMA controller */
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100791 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -0800792 x = *port->tx_icr;
793 x |= GxICR_ENABLE;
794
795 if (*port->_status & SC01STR_TBF)
796 x &= ~(GxICR_REQUEST | GxICR_DETECT);
797 else
798 x |= GxICR_REQUEST | GxICR_DETECT;
799
800 _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
801 *port->_control, *port->_intr, *port->_status,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100802 *port->_tmxmd,
803 (port->div_timer == MNSCx_DIV_TIMER_8BIT) ?
804 *(volatile u8 *)port->_tmxbr : *port->_tmxbr,
805 *port->tx_icr);
David Howellsb920de12008-02-08 04:19:31 -0800806
807 *port->tx_icr = x;
808 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100809 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -0800810}
811
812/*
813 * transmit a high-priority XON/XOFF character
814 */
815static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
816{
817 struct mn10300_serial_port *port =
818 container_of(_port, struct mn10300_serial_port, uart);
819
820 _enter("%s,%02x", port->name, ch);
821
822 if (likely(port->gdbstub)) {
823 port->tx_xchar = ch;
824 if (ch)
825 mn10300_serial_en_tx_intr(port);
826 }
827}
828
829/*
830 * stop receiving characters
831 * - called whilst the port is being closed
832 */
833static void mn10300_serial_stop_rx(struct uart_port *_port)
834{
835 struct mn10300_serial_port *port =
836 container_of(_port, struct mn10300_serial_port, uart);
837
838 u16 ctr;
839
840 _enter("%s", port->name);
841
842 ctr = *port->_control;
843 ctr &= ~SC01CTR_RXE;
844 *port->_control = ctr;
845
846 mn10300_serial_dis_rx_intr(port);
847}
848
849/*
850 * enable modem status interrupts
851 */
852static void mn10300_serial_enable_ms(struct uart_port *_port)
853{
854 struct mn10300_serial_port *port =
855 container_of(_port, struct mn10300_serial_port, uart);
856
857 u16 ctr, cts;
858
859 _enter("%s", port->name);
860
861 if (port->type == PORT_MN10300_CTS) {
862 /* want to interrupt when CTS goes low if CTS is now high and
863 * vice versa
864 */
865 port->tx_cts = *port->_status;
866
867 cts = (port->tx_cts & SC2STR_CTS) ?
868 SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
869
870 ctr = *port->_control;
871 ctr &= ~SC2CTR_TWS;
872 ctr |= cts;
873 *port->_control = ctr;
874
875 mn10300_serial_en_tx_intr(port);
876 }
877}
878
879/*
880 * transmit or cease transmitting a break signal
881 */
882static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
883{
884 struct mn10300_serial_port *port =
885 container_of(_port, struct mn10300_serial_port, uart);
886
887 _enter("%s,%d", port->name, ctl);
888
889 if (ctl) {
890 /* tell the virtual DMA handler to assert BREAK */
891 port->tx_break = 1;
892 mn10300_serial_en_tx_intr(port);
893 } else {
894 port->tx_break = 0;
895 *port->_control &= ~SC01CTR_BKE;
896 mn10300_serial_en_tx_intr(port);
897 }
898}
899
900/*
901 * grab the interrupts and enable the port for reception
902 */
903static int mn10300_serial_startup(struct uart_port *_port)
904{
905 struct mn10300_serial_port *port =
906 container_of(_port, struct mn10300_serial_port, uart);
907 struct mn10300_serial_int *pint;
908
909 _enter("%s{%d}", port->name, port->gdbstub);
910
911 if (unlikely(port->gdbstub))
912 return -EBUSY;
913
914 /* allocate an Rx buffer for the virtual DMA handler */
915 port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
916 if (!port->rx_buffer)
917 return -ENOMEM;
918
919 port->rx_inp = port->rx_outp = 0;
920
921 /* finally, enable the device */
922 *port->_intr = SC01ICR_TI;
923 *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
924
925 pint = &mn10300_serial_int_tbl[port->rx_irq];
926 pint->port = port;
927 pint->vdma = mn10300_serial_vdma_rx_handler;
928 pint = &mn10300_serial_int_tbl[port->tx_irq];
929 pint->port = port;
930 pint->vdma = mn10300_serial_vdma_tx_handler;
931
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100932 set_intr_level(port->rx_irq,
933 NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL));
934 set_intr_level(port->tx_irq,
935 NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL));
Thomas Gleixnerf4c547e2011-03-24 17:35:56 +0100936 irq_set_chip(port->tm_irq, &mn10300_serial_pic);
David Howellsb920de12008-02-08 04:19:31 -0800937
938 if (request_irq(port->rx_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000939 IRQF_DISABLED | IRQF_NOBALANCING,
940 port->rx_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800941 goto error;
942
943 if (request_irq(port->tx_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000944 IRQF_DISABLED | IRQF_NOBALANCING,
945 port->tx_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800946 goto error2;
947
948 if (request_irq(port->tm_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000949 IRQF_DISABLED | IRQF_NOBALANCING,
950 port->tm_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800951 goto error3;
952 mn10300_serial_mask_ack(port->tm_irq);
953
954 return 0;
955
956error3:
957 free_irq(port->tx_irq, port);
958error2:
959 free_irq(port->rx_irq, port);
960error:
961 kfree(port->rx_buffer);
962 port->rx_buffer = NULL;
963 return -EBUSY;
964}
965
966/*
967 * shutdown the port and release interrupts
968 */
969static void mn10300_serial_shutdown(struct uart_port *_port)
970{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100971 u16 x;
David Howellsb920de12008-02-08 04:19:31 -0800972 struct mn10300_serial_port *port =
973 container_of(_port, struct mn10300_serial_port, uart);
974
975 _enter("%s", port->name);
976
977 /* disable the serial port and its baud rate timer */
978 port->tx_break = 0;
979 *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
980 *port->_tmxmd = 0;
981
982 if (port->rx_buffer) {
983 void *buf = port->rx_buffer;
984 port->rx_buffer = NULL;
985 kfree(buf);
986 }
987
988 /* disable all intrs */
989 free_irq(port->tm_irq, port);
990 free_irq(port->rx_irq, port);
991 free_irq(port->tx_irq, port);
992
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100993 arch_local_cli();
994 *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
995 x = *port->rx_icr;
996 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
997 x = *port->tx_icr;
998 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -0800999}
1000
1001/*
1002 * this routine is called to set the UART divisor registers to match the
1003 * specified baud rate for a serial port.
1004 */
1005static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
1006 struct ktermios *new,
1007 struct ktermios *old)
1008{
1009 unsigned long flags;
1010 unsigned long ioclk = port->ioclk;
1011 unsigned cflag;
1012 int baud, bits, xdiv, tmp;
1013 u16 tmxbr, scxctr;
1014 u8 tmxmd, battempt;
1015 u8 div_timer = port->div_timer;
1016
1017 _enter("%s{%lu}", port->name, ioclk);
1018
1019 /* byte size and parity */
1020 cflag = new->c_cflag;
1021 switch (cflag & CSIZE) {
1022 case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
1023 case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
1024 default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
1025 }
1026
1027 if (cflag & CSTOPB) {
1028 scxctr |= SC01CTR_STB_2BIT;
1029 bits++;
1030 }
1031
1032 if (cflag & PARENB) {
1033 bits++;
1034 if (cflag & PARODD)
1035 scxctr |= SC01CTR_PB_ODD;
1036#ifdef CMSPAR
1037 else if (cflag & CMSPAR)
1038 scxctr |= SC01CTR_PB_FIXED0;
1039#endif
1040 else
1041 scxctr |= SC01CTR_PB_EVEN;
1042 }
1043
1044 /* Determine divisor based on baud rate */
1045 battempt = 0;
1046
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001047 switch (port->uart.line) {
1048#ifdef CONFIG_MN10300_TTYSM0
1049 case 0: /* ttySM0 */
1050#if defined(CONFIG_MN10300_TTYSM0_TIMER8)
1051 scxctr |= SC0CTR_CK_TM8UFLOW_8;
1052#elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
1053 scxctr |= SC0CTR_CK_TM0UFLOW_8;
1054#elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
David Howellsb920de12008-02-08 04:19:31 -08001055 scxctr |= SC0CTR_CK_TM2UFLOW_8;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001056#else
1057#error "Unknown config for ttySM0"
1058#endif
1059 break;
1060#endif /* CONFIG_MN10300_TTYSM0 */
1061
1062#ifdef CONFIG_MN10300_TTYSM1
1063 case 1: /* ttySM1 */
1064#if defined(CONFIG_AM33_2) || defined(CONFIG_AM33_3)
1065#if defined(CONFIG_MN10300_TTYSM1_TIMER9)
1066 scxctr |= SC1CTR_CK_TM9UFLOW_8;
1067#elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
1068 scxctr |= SC1CTR_CK_TM3UFLOW_8;
1069#else
1070#error "Unknown config for ttySM1"
1071#endif
1072#else /* CONFIG_AM33_2 || CONFIG_AM33_3 */
1073#if defined(CONFIG_MN10300_TTYSM1_TIMER12)
1074 scxctr |= SC1CTR_CK_TM12UFLOW_8;
1075#else
1076#error "Unknown config for ttySM1"
1077#endif
1078#endif /* CONFIG_AM33_2 || CONFIG_AM33_3 */
1079 break;
1080#endif /* CONFIG_MN10300_TTYSM1 */
1081
1082#ifdef CONFIG_MN10300_TTYSM2
1083 case 2: /* ttySM2 */
1084#if defined(CONFIG_AM33_2)
1085#if defined(CONFIG_MN10300_TTYSM2_TIMER10)
1086 scxctr |= SC2CTR_CK_TM10UFLOW;
1087#else
1088#error "Unknown config for ttySM2"
1089#endif
1090#else /* CONFIG_AM33_2 */
1091#if defined(CONFIG_MN10300_TTYSM2_TIMER9)
1092 scxctr |= SC2CTR_CK_TM9UFLOW_8;
1093#elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
1094 scxctr |= SC2CTR_CK_TM1UFLOW_8;
1095#elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
1096 scxctr |= SC2CTR_CK_TM3UFLOW_8;
1097#else
1098#error "Unknown config for ttySM2"
1099#endif
1100#endif /* CONFIG_AM33_2 */
1101 break;
1102#endif /* CONFIG_MN10300_TTYSM2 */
1103
1104 default:
1105 break;
1106 }
David Howellsb920de12008-02-08 04:19:31 -08001107
1108try_alternative:
1109 baud = uart_get_baud_rate(&port->uart, new, old, 0,
1110 port->ioclk / 8);
1111
1112 _debug("ALT %d [baud %d]", battempt, baud);
1113
1114 if (!baud)
1115 baud = 9600; /* B0 transition handled in rs_set_termios */
1116 xdiv = 1;
1117 if (baud == 134) {
1118 baud = 269; /* 134 is really 134.5 */
1119 xdiv = 2;
1120 }
1121
1122 if (baud == 38400 &&
1123 (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
1124 ) {
1125 _debug("CUSTOM %u", port->uart.custom_divisor);
1126
1127 if (div_timer == MNSCx_DIV_TIMER_16BIT) {
1128 if (port->uart.custom_divisor <= 65535) {
1129 tmxmd = TM8MD_SRC_IOCLK;
1130 tmxbr = port->uart.custom_divisor;
1131 port->uart.uartclk = ioclk;
1132 goto timer_okay;
1133 }
1134 if (port->uart.custom_divisor / 8 <= 65535) {
1135 tmxmd = TM8MD_SRC_IOCLK_8;
1136 tmxbr = port->uart.custom_divisor / 8;
1137 port->uart.custom_divisor = tmxbr * 8;
1138 port->uart.uartclk = ioclk / 8;
1139 goto timer_okay;
1140 }
1141 if (port->uart.custom_divisor / 32 <= 65535) {
1142 tmxmd = TM8MD_SRC_IOCLK_32;
1143 tmxbr = port->uart.custom_divisor / 32;
1144 port->uart.custom_divisor = tmxbr * 32;
1145 port->uart.uartclk = ioclk / 32;
1146 goto timer_okay;
1147 }
1148
1149 } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
1150 if (port->uart.custom_divisor <= 255) {
1151 tmxmd = TM2MD_SRC_IOCLK;
1152 tmxbr = port->uart.custom_divisor;
1153 port->uart.uartclk = ioclk;
1154 goto timer_okay;
1155 }
1156 if (port->uart.custom_divisor / 8 <= 255) {
1157 tmxmd = TM2MD_SRC_IOCLK_8;
1158 tmxbr = port->uart.custom_divisor / 8;
1159 port->uart.custom_divisor = tmxbr * 8;
1160 port->uart.uartclk = ioclk / 8;
1161 goto timer_okay;
1162 }
1163 if (port->uart.custom_divisor / 32 <= 255) {
1164 tmxmd = TM2MD_SRC_IOCLK_32;
1165 tmxbr = port->uart.custom_divisor / 32;
1166 port->uart.custom_divisor = tmxbr * 32;
1167 port->uart.uartclk = ioclk / 32;
1168 goto timer_okay;
1169 }
1170 }
1171 }
1172
1173 switch (div_timer) {
1174 case MNSCx_DIV_TIMER_16BIT:
1175 port->uart.uartclk = ioclk;
1176 tmxmd = TM8MD_SRC_IOCLK;
1177 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1178 if (tmp > 0 && tmp <= 65535)
1179 goto timer_okay;
1180
1181 port->uart.uartclk = ioclk / 8;
1182 tmxmd = TM8MD_SRC_IOCLK_8;
1183 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1184 if (tmp > 0 && tmp <= 65535)
1185 goto timer_okay;
1186
1187 port->uart.uartclk = ioclk / 32;
1188 tmxmd = TM8MD_SRC_IOCLK_32;
1189 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1190 if (tmp > 0 && tmp <= 65535)
1191 goto timer_okay;
1192 break;
1193
1194 case MNSCx_DIV_TIMER_8BIT:
1195 port->uart.uartclk = ioclk;
1196 tmxmd = TM2MD_SRC_IOCLK;
1197 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1198 if (tmp > 0 && tmp <= 255)
1199 goto timer_okay;
1200
1201 port->uart.uartclk = ioclk / 8;
1202 tmxmd = TM2MD_SRC_IOCLK_8;
1203 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1204 if (tmp > 0 && tmp <= 255)
1205 goto timer_okay;
1206
1207 port->uart.uartclk = ioclk / 32;
1208 tmxmd = TM2MD_SRC_IOCLK_32;
1209 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1210 if (tmp > 0 && tmp <= 255)
1211 goto timer_okay;
1212 break;
1213
1214 default:
1215 BUG();
1216 return;
1217 }
1218
1219 /* refuse to change to a baud rate we can't support */
1220 _debug("CAN'T SUPPORT");
1221
1222 switch (battempt) {
1223 case 0:
1224 if (old) {
1225 new->c_cflag &= ~CBAUD;
1226 new->c_cflag |= (old->c_cflag & CBAUD);
1227 battempt = 1;
1228 goto try_alternative;
1229 }
1230
1231 case 1:
1232 /* as a last resort, if the quotient is zero, default to 9600
1233 * bps */
1234 new->c_cflag &= ~CBAUD;
1235 new->c_cflag |= B9600;
1236 battempt = 2;
1237 goto try_alternative;
1238
1239 default:
1240 /* hmmm... can't seem to support 9600 either
1241 * - we could try iterating through the speeds we know about to
1242 * find the lowest
1243 */
1244 new->c_cflag &= ~CBAUD;
1245 new->c_cflag |= B0;
1246
1247 if (div_timer == MNSCx_DIV_TIMER_16BIT)
1248 tmxmd = TM8MD_SRC_IOCLK_32;
1249 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
1250 tmxmd = TM2MD_SRC_IOCLK_32;
1251 tmxbr = 1;
1252
1253 port->uart.uartclk = ioclk / 32;
1254 break;
1255 }
1256timer_okay:
1257
1258 _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
1259
1260 /* make the changes */
1261 spin_lock_irqsave(&port->uart.lock, flags);
1262
1263 uart_update_timeout(&port->uart, new->c_cflag, baud);
1264
1265 /* set the timer to produce the required baud rate */
1266 switch (div_timer) {
1267 case MNSCx_DIV_TIMER_16BIT:
1268 *port->_tmxmd = 0;
1269 *port->_tmxbr = tmxbr;
1270 *port->_tmxmd = TM8MD_INIT_COUNTER;
1271 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1272 break;
1273
1274 case MNSCx_DIV_TIMER_8BIT:
1275 *port->_tmxmd = 0;
1276 *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
1277 *port->_tmxmd = TM2MD_INIT_COUNTER;
1278 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1279 break;
1280 }
1281
1282 /* CTS flow control flag and modem status interrupts */
1283 scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
1284
1285 if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
1286 /* want to interrupt when CTS goes low if CTS is now
1287 * high and vice versa
1288 */
1289 port->tx_cts = *port->_status;
1290
1291 if (port->tx_cts & SC2STR_CTS)
1292 scxctr |= SC2CTR_TWE;
1293 else
1294 scxctr |= SC2CTR_TWE | SC2CTR_TWS;
1295 }
1296
1297 /* set up parity check flag */
1298 port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
1299 if (new->c_iflag & INPCK)
1300 port->uart.read_status_mask |=
1301 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1302 if (new->c_iflag & (BRKINT | PARMRK))
1303 port->uart.read_status_mask |= (1 << TTY_BREAK);
1304
1305 /* characters to ignore */
1306 port->uart.ignore_status_mask = 0;
1307 if (new->c_iflag & IGNPAR)
1308 port->uart.ignore_status_mask |=
1309 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1310 if (new->c_iflag & IGNBRK) {
1311 port->uart.ignore_status_mask |= (1 << TTY_BREAK);
1312 /*
1313 * If we're ignoring parity and break indicators,
1314 * ignore overruns to (for real raw support).
1315 */
1316 if (new->c_iflag & IGNPAR)
1317 port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
1318 }
1319
1320 /* Ignore all characters if CREAD is not set */
1321 if ((new->c_cflag & CREAD) == 0)
1322 port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
1323
1324 scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1325 *port->_control = scxctr;
1326
1327 spin_unlock_irqrestore(&port->uart.lock, flags);
1328}
1329
1330/*
1331 * set the terminal I/O parameters
1332 */
1333static void mn10300_serial_set_termios(struct uart_port *_port,
1334 struct ktermios *new,
1335 struct ktermios *old)
1336{
1337 struct mn10300_serial_port *port =
1338 container_of(_port, struct mn10300_serial_port, uart);
1339
1340 _enter("%s,%p,%p", port->name, new, old);
1341
1342 mn10300_serial_change_speed(port, new, old);
1343
1344 /* handle turning off CRTSCTS */
1345 if (!(new->c_cflag & CRTSCTS)) {
1346 u16 ctr = *port->_control;
1347 ctr &= ~SC2CTR_TWE;
1348 *port->_control = ctr;
1349 }
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001350
1351 /* change Transfer bit-order (LSB/MSB) */
1352 if (new->c_cflag & CODMSB)
1353 *port->_control |= SC01CTR_OD_MSBFIRST; /* MSB MODE */
1354 else
1355 *port->_control &= ~SC01CTR_OD_MSBFIRST; /* LSB MODE */
David Howellsb920de12008-02-08 04:19:31 -08001356}
1357
1358/*
1359 * return description of port type
1360 */
1361static const char *mn10300_serial_type(struct uart_port *_port)
1362{
1363 struct mn10300_serial_port *port =
1364 container_of(_port, struct mn10300_serial_port, uart);
1365
1366 if (port->uart.type == PORT_MN10300_CTS)
1367 return "MN10300 SIF_CTS";
1368
1369 return "MN10300 SIF";
1370}
1371
1372/*
1373 * release I/O and memory regions in use by port
1374 */
1375static void mn10300_serial_release_port(struct uart_port *_port)
1376{
1377 struct mn10300_serial_port *port =
1378 container_of(_port, struct mn10300_serial_port, uart);
1379
1380 _enter("%s", port->name);
1381
1382 release_mem_region((unsigned long) port->_iobase, 16);
1383}
1384
1385/*
1386 * request I/O and memory regions for port
1387 */
1388static int mn10300_serial_request_port(struct uart_port *_port)
1389{
1390 struct mn10300_serial_port *port =
1391 container_of(_port, struct mn10300_serial_port, uart);
1392
1393 _enter("%s", port->name);
1394
1395 request_mem_region((unsigned long) port->_iobase, 16, port->name);
1396 return 0;
1397}
1398
1399/*
1400 * configure the type and reserve the ports
1401 */
1402static void mn10300_serial_config_port(struct uart_port *_port, int type)
1403{
1404 struct mn10300_serial_port *port =
1405 container_of(_port, struct mn10300_serial_port, uart);
1406
1407 _enter("%s", port->name);
1408
1409 port->uart.type = PORT_MN10300;
1410
1411 if (port->options & MNSCx_OPT_CTS)
1412 port->uart.type = PORT_MN10300_CTS;
1413
1414 mn10300_serial_request_port(_port);
1415}
1416
1417/*
1418 * verify serial parameters are suitable for this port type
1419 */
1420static int mn10300_serial_verify_port(struct uart_port *_port,
1421 struct serial_struct *ss)
1422{
1423 struct mn10300_serial_port *port =
1424 container_of(_port, struct mn10300_serial_port, uart);
1425 void *mapbase = (void *) (unsigned long) port->uart.mapbase;
1426
1427 _enter("%s", port->name);
1428
1429 /* these things may not be changed */
1430 if (ss->irq != port->uart.irq ||
1431 ss->port != port->uart.iobase ||
1432 ss->io_type != port->uart.iotype ||
1433 ss->iomem_base != mapbase ||
1434 ss->iomem_reg_shift != port->uart.regshift ||
1435 ss->hub6 != port->uart.hub6 ||
1436 ss->xmit_fifo_size != port->uart.fifosize)
1437 return -EINVAL;
1438
1439 /* type may be changed on a port that supports CTS */
1440 if (ss->type != port->uart.type) {
1441 if (!(port->options & MNSCx_OPT_CTS))
1442 return -EINVAL;
1443
1444 if (ss->type != PORT_MN10300 &&
1445 ss->type != PORT_MN10300_CTS)
1446 return -EINVAL;
1447 }
1448
1449 return 0;
1450}
1451
1452/*
1453 * initialise the MN10300 on-chip UARTs
1454 */
1455static int __init mn10300_serial_init(void)
1456{
1457 struct mn10300_serial_port *port;
1458 int ret, i;
1459
1460 printk(KERN_INFO "%s version %s (%s)\n",
1461 serial_name, serial_version, serial_revdate);
1462
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001463#if defined(CONFIG_MN10300_TTYSM2) && defined(CONFIG_AM33_2)
1464 {
1465 int tmp;
1466 SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
1467 tmp = SC2TIM;
1468 }
David Howellsb920de12008-02-08 04:19:31 -08001469#endif
1470
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001471 set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL),
1472 mn10300_serial_vdma_interrupt);
David Howellsb920de12008-02-08 04:19:31 -08001473
1474 ret = uart_register_driver(&mn10300_serial_driver);
1475 if (!ret) {
1476 for (i = 0 ; i < NR_PORTS ; i++) {
1477 port = mn10300_serial_ports[i];
1478 if (!port || port->gdbstub)
1479 continue;
1480
1481 switch (port->clock_src) {
1482 case MNSCx_CLOCK_SRC_IOCLK:
1483 port->ioclk = MN10300_IOCLK;
1484 break;
1485
1486#ifdef MN10300_IOBCLK
1487 case MNSCx_CLOCK_SRC_IOBCLK:
1488 port->ioclk = MN10300_IOBCLK;
1489 break;
1490#endif
1491 default:
1492 BUG();
1493 }
1494
1495 ret = uart_add_one_port(&mn10300_serial_driver,
1496 &port->uart);
1497
1498 if (ret < 0) {
1499 _debug("ERROR %d", -ret);
1500 break;
1501 }
1502 }
1503
1504 if (ret)
1505 uart_unregister_driver(&mn10300_serial_driver);
1506 }
1507
1508 return ret;
1509}
1510
1511__initcall(mn10300_serial_init);
1512
1513
1514#ifdef CONFIG_MN10300_TTYSM_CONSOLE
1515
1516/*
1517 * print a string to the serial port without disturbing the real user of the
1518 * port too much
1519 * - the console must be locked by the caller
1520 */
1521static void mn10300_serial_console_write(struct console *co,
1522 const char *s, unsigned count)
1523{
1524 struct mn10300_serial_port *port;
1525 unsigned i;
1526 u16 scxctr, txicr, tmp;
1527 u8 tmxmd;
1528
1529 port = mn10300_serial_ports[co->index];
1530
1531 /* firstly hijack the serial port from the "virtual DMA" controller */
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001532 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -08001533 txicr = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001534 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -08001535 tmp = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001536 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -08001537
1538 /* the transmitter may be disabled */
1539 scxctr = *port->_control;
1540 if (!(scxctr & SC01CTR_TXE)) {
1541 /* restart the UART clock */
1542 tmxmd = *port->_tmxmd;
1543
1544 switch (port->div_timer) {
1545 case MNSCx_DIV_TIMER_16BIT:
1546 *port->_tmxmd = 0;
1547 *port->_tmxmd = TM8MD_INIT_COUNTER;
1548 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1549 break;
1550
1551 case MNSCx_DIV_TIMER_8BIT:
1552 *port->_tmxmd = 0;
1553 *port->_tmxmd = TM2MD_INIT_COUNTER;
1554 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1555 break;
1556 }
1557
1558 /* enable the transmitter */
1559 *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
1560
1561 } else if (scxctr & SC01CTR_BKE) {
1562 /* stop transmitting BREAK */
1563 *port->_control = (scxctr & ~SC01CTR_BKE);
1564 }
1565
1566 /* send the chars into the serial port (with LF -> LFCR conversion) */
1567 for (i = 0; i < count; i++) {
1568 char ch = *s++;
1569
1570 while (*port->_status & SC01STR_TBF)
1571 continue;
1572 *(u8 *) port->_txb = ch;
1573
1574 if (ch == 0x0a) {
1575 while (*port->_status & SC01STR_TBF)
1576 continue;
1577 *(u8 *) port->_txb = 0xd;
1578 }
1579 }
1580
1581 /* can't let the transmitter be turned off if it's actually
1582 * transmitting */
1583 while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
1584 continue;
1585
1586 /* disable the transmitter if we re-enabled it */
1587 if (!(scxctr & SC01CTR_TXE))
1588 *port->_control = scxctr;
1589
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001590 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -08001591 *port->tx_icr = txicr;
1592 tmp = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001593 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -08001594}
1595
1596/*
1597 * set up a serial port as a console
1598 * - construct a cflag setting for the first rs_open()
1599 * - initialize the serial port
1600 * - return non-zero if we didn't find a serial port.
1601 */
1602static int __init mn10300_serial_console_setup(struct console *co,
1603 char *options)
1604{
1605 struct mn10300_serial_port *port;
1606 int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
1607
1608 for (i = 0 ; i < NR_PORTS ; i++) {
1609 port = mn10300_serial_ports[i];
1610 if (port && !port->gdbstub && port->uart.line == co->index)
1611 goto found_device;
1612 }
1613
1614 return -ENODEV;
1615
1616found_device:
1617 switch (port->clock_src) {
1618 case MNSCx_CLOCK_SRC_IOCLK:
1619 port->ioclk = MN10300_IOCLK;
1620 break;
1621
1622#ifdef MN10300_IOBCLK
1623 case MNSCx_CLOCK_SRC_IOBCLK:
1624 port->ioclk = MN10300_IOBCLK;
1625 break;
1626#endif
1627 default:
1628 BUG();
1629 }
1630
1631 if (options)
1632 uart_parse_options(options, &baud, &parity, &bits, &flow);
1633
1634 return uart_set_options(&port->uart, co, baud, parity, bits, flow);
1635}
1636
1637/*
1638 * register console
1639 */
1640static int __init mn10300_serial_console_init(void)
1641{
1642 register_console(&mn10300_serial_console);
1643 return 0;
1644}
1645
1646console_initcall(mn10300_serial_console_init);
1647#endif
David Howells52885b32011-03-18 16:54:32 +00001648
1649#ifdef CONFIG_CONSOLE_POLL
1650/*
1651 * Polled character reception for the kernel debugger
1652 */
1653static int mn10300_serial_poll_get_char(struct uart_port *_port)
1654{
1655 struct mn10300_serial_port *port =
1656 container_of(_port, struct mn10300_serial_port, uart);
1657 unsigned ix;
1658 u8 st, ch;
1659
1660 _enter("%s", port->name);
1661
1662 do {
1663 /* pull chars out of the hat */
David Howellsc98c4062012-12-12 15:36:37 +00001664 ix = ACCESS_ONCE(port->rx_outp);
1665 if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0)
David Howells52885b32011-03-18 16:54:32 +00001666 return NO_POLL_CHAR;
1667
David Howellsc98c4062012-12-12 15:36:37 +00001668 smp_read_barrier_depends();
David Howells52885b32011-03-18 16:54:32 +00001669 ch = port->rx_buffer[ix++];
1670 st = port->rx_buffer[ix++];
David Howellsc98c4062012-12-12 15:36:37 +00001671 smp_mb();
David Howells52885b32011-03-18 16:54:32 +00001672 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
1673
1674 } while (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF));
1675
1676 return ch;
1677}
1678
1679
1680/*
1681 * Polled character transmission for the kernel debugger
1682 */
1683static void mn10300_serial_poll_put_char(struct uart_port *_port,
1684 unsigned char ch)
1685{
1686 struct mn10300_serial_port *port =
1687 container_of(_port, struct mn10300_serial_port, uart);
1688 u8 intr, tmp;
1689
1690 /* wait for the transmitter to finish anything it might be doing (and
1691 * this includes the virtual DMA handler, so it might take a while) */
1692 while (*port->_status & (SC01STR_TBF | SC01STR_TXF))
1693 continue;
1694
1695 /* disable the Tx ready interrupt */
1696 intr = *port->_intr;
1697 *port->_intr = intr & ~SC01ICR_TI;
1698 tmp = *port->_intr;
1699
1700 if (ch == 0x0a) {
1701 *(u8 *) port->_txb = 0x0d;
1702 while (*port->_status & SC01STR_TBF)
1703 continue;
1704 }
1705
1706 *(u8 *) port->_txb = ch;
1707 while (*port->_status & SC01STR_TBF)
1708 continue;
1709
1710 /* restore the Tx interrupt flag */
1711 *port->_intr = intr;
1712 tmp = *port->_intr;
1713}
1714
1715#endif /* CONFIG_CONSOLE_POLL */