blob: cac0f0da92037e3cd9fe61d9e5b063d0b23d2670 [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
Mark Salter8d160022012-12-12 15:36:38 +0000411static void mn10300_serial_low_mask(struct irq_data *d)
412{
413 unsigned long flags;
414 u16 tmp;
415
416 flags = arch_local_cli_save();
417 GxICR(d->irq) = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
418 tmp = GxICR(d->irq); /* flush write buffer */
419 arch_local_irq_restore(flags);
420}
421
422static void mn10300_serial_low_unmask(struct irq_data *d)
423{
424 unsigned long flags;
425 u16 tmp;
426
427 flags = arch_local_cli_save();
428 GxICR(d->irq) =
429 NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL) | GxICR_ENABLE;
430 tmp = GxICR(d->irq); /* flush write buffer */
431 arch_local_irq_restore(flags);
432}
433
434static struct irq_chip mn10300_serial_low_pic = {
435 .name = "mnserial-low",
436 .irq_mask = mn10300_serial_low_mask,
437 .irq_unmask = mn10300_serial_low_unmask,
438};
David Howellsb920de12008-02-08 04:19:31 -0800439
440/*
441 * serial virtual DMA interrupt jump table
442 */
443struct mn10300_serial_int mn10300_serial_int_tbl[NR_IRQS];
444
445static void mn10300_serial_dis_tx_intr(struct mn10300_serial_port *port)
446{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100447 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800448 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100449
450 flags = arch_local_cli_save();
451 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -0800452 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100453 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800454}
455
456static void mn10300_serial_en_tx_intr(struct mn10300_serial_port *port)
457{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100458 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800459 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100460
461 flags = arch_local_cli_save();
462 *port->tx_icr =
463 NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL) | GxICR_ENABLE;
David Howellsb920de12008-02-08 04:19:31 -0800464 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100465 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800466}
467
468static void mn10300_serial_dis_rx_intr(struct mn10300_serial_port *port)
469{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100470 unsigned long flags;
David Howellsb920de12008-02-08 04:19:31 -0800471 u16 x;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100472
473 flags = arch_local_cli_save();
474 *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -0800475 x = *port->rx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100476 arch_local_irq_restore(flags);
David Howellsb920de12008-02-08 04:19:31 -0800477}
478
479/*
480 * multi-bit equivalent of test_and_clear_bit()
481 */
482static int mask_test_and_clear(volatile u8 *ptr, u8 mask)
483{
484 u32 epsw;
485 asm volatile(" bclr %1,(%2) \n"
486 " mov epsw,%0 \n"
Mark Salterd6bb7a12010-01-08 14:43:16 -0800487 : "=d"(epsw) : "d"(mask), "a"(ptr)
488 : "cc", "memory");
David Howellsb920de12008-02-08 04:19:31 -0800489 return !(epsw & EPSW_FLAG_Z);
490}
491
492/*
493 * receive chars from the ring buffer for this serial port
494 * - must do break detection here (not done in the UART)
495 */
496static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
497{
498 struct uart_icount *icount = &port->uart.icount;
David Howells70430782009-09-23 10:40:24 +0100499 struct tty_struct *tty = port->uart.state->port.tty;
David Howellsb920de12008-02-08 04:19:31 -0800500 unsigned ix;
501 int count;
502 u8 st, ch, push, status, overrun;
503
504 _enter("%s", port->name);
505
506 push = 0;
507
508 count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
509 count = tty_buffer_request_room(tty, count);
510 if (count == 0) {
511 if (!tty->low_latency)
512 tty_flip_buffer_push(tty);
513 return;
514 }
515
516try_again:
517 /* pull chars out of the hat */
David Howellsc98c4062012-12-12 15:36:37 +0000518 ix = ACCESS_ONCE(port->rx_outp);
519 if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0) {
David Howellsb920de12008-02-08 04:19:31 -0800520 if (push && !tty->low_latency)
521 tty_flip_buffer_push(tty);
522 return;
523 }
524
David Howellsc98c4062012-12-12 15:36:37 +0000525 smp_read_barrier_depends();
David Howellsb920de12008-02-08 04:19:31 -0800526 ch = port->rx_buffer[ix++];
527 st = port->rx_buffer[ix++];
David Howellsc98c4062012-12-12 15:36:37 +0000528 smp_mb();
David Howellsb920de12008-02-08 04:19:31 -0800529 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
530 port->uart.icount.rx++;
531
532 st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
533 status = 0;
534 overrun = 0;
535
536 /* the UART doesn't detect BREAK, so we have to do that ourselves
537 * - it starts as a framing error on a NUL character
538 * - then we count another two NUL characters before issuing TTY_BREAK
539 * - then we end on a normal char or one that has all the bottom bits
540 * zero and the top bits set
541 */
542 switch (port->rx_brk) {
543 case 0:
544 /* not breaking at the moment */
545 break;
546
547 case 1:
548 if (st & SC01STR_FEF && ch == 0) {
549 port->rx_brk = 2;
550 goto try_again;
551 }
552 goto not_break;
553
554 case 2:
555 if (st & SC01STR_FEF && ch == 0) {
556 port->rx_brk = 3;
557 _proto("Rx Break Detected");
558 icount->brk++;
559 if (uart_handle_break(&port->uart))
560 goto ignore_char;
561 status |= 1 << TTY_BREAK;
562 goto insert;
563 }
564 goto not_break;
565
566 default:
567 if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
568 goto try_again; /* still breaking */
569
570 port->rx_brk = 0; /* end of the break */
571
572 switch (ch) {
573 case 0xFF:
574 case 0xFE:
575 case 0xFC:
576 case 0xF8:
577 case 0xF0:
578 case 0xE0:
579 case 0xC0:
580 case 0x80:
581 case 0x00:
582 /* discard char at probable break end */
583 goto try_again;
584 }
585 break;
586 }
587
588process_errors:
589 /* handle framing error */
590 if (st & SC01STR_FEF) {
591 if (ch == 0) {
592 /* framing error with NUL char is probably a BREAK */
593 port->rx_brk = 1;
594 goto try_again;
595 }
596
597 _proto("Rx Framing Error");
598 icount->frame++;
599 status |= 1 << TTY_FRAME;
600 }
601
602 /* handle parity error */
603 if (st & SC01STR_PEF) {
604 _proto("Rx Parity Error");
605 icount->parity++;
606 status = TTY_PARITY;
607 }
608
609 /* handle normal char */
610 if (status == 0) {
611 if (uart_handle_sysrq_char(&port->uart, ch))
612 goto ignore_char;
613 status = (1 << TTY_NORMAL);
614 }
615
616 /* handle overrun error */
617 if (st & SC01STR_OEF) {
618 if (port->rx_brk)
619 goto try_again;
620
621 _proto("Rx Overrun Error");
622 icount->overrun++;
623 overrun = 1;
624 }
625
626insert:
627 status &= port->uart.read_status_mask;
628
629 if (!overrun && !(status & port->uart.ignore_status_mask)) {
630 int flag;
631
632 if (status & (1 << TTY_BREAK))
633 flag = TTY_BREAK;
634 else if (status & (1 << TTY_PARITY))
635 flag = TTY_PARITY;
636 else if (status & (1 << TTY_FRAME))
637 flag = TTY_FRAME;
638 else
639 flag = TTY_NORMAL;
640
641 tty_insert_flip_char(tty, ch, flag);
642 }
643
644 /* overrun is special, since it's reported immediately, and doesn't
645 * affect the current character
646 */
647 if (overrun)
648 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
649
650 count--;
651 if (count <= 0) {
652 if (!tty->low_latency)
653 tty_flip_buffer_push(tty);
654 return;
655 }
656
657ignore_char:
658 push = 1;
659 goto try_again;
660
661not_break:
662 port->rx_brk = 0;
663 goto process_errors;
664}
665
666/*
667 * handle an interrupt from the serial transmission "virtual DMA" driver
668 * - note: the interrupt routine will disable its own interrupts when the Tx
669 * buffer is empty
670 */
671static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
672{
673 _enter("%s", port->name);
674
David Howells70430782009-09-23 10:40:24 +0100675 if (!port->uart.state || !port->uart.state->port.tty) {
Akira Takeuchia8893fb2008-12-10 12:43:24 +0000676 mn10300_serial_dis_tx_intr(port);
677 return;
678 }
679
David Howellsb920de12008-02-08 04:19:31 -0800680 if (uart_tx_stopped(&port->uart) ||
David Howells70430782009-09-23 10:40:24 +0100681 uart_circ_empty(&port->uart.state->xmit))
David Howellsb920de12008-02-08 04:19:31 -0800682 mn10300_serial_dis_tx_intr(port);
683
David Howells70430782009-09-23 10:40:24 +0100684 if (uart_circ_chars_pending(&port->uart.state->xmit) < WAKEUP_CHARS)
David Howellsb920de12008-02-08 04:19:31 -0800685 uart_write_wakeup(&port->uart);
686}
687
688/*
689 * deal with a change in the status of the CTS line
690 */
691static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
692{
693 u16 ctr;
694
695 port->tx_cts = st;
696 port->uart.icount.cts++;
697
698 /* flip the CTS state selector flag to interrupt when it changes
699 * back */
700 ctr = *port->_control;
701 ctr ^= SC2CTR_TWS;
702 *port->_control = ctr;
703
704 uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
David Howells70430782009-09-23 10:40:24 +0100705 wake_up_interruptible(&port->uart.state->port.delta_msr_wait);
David Howellsb920de12008-02-08 04:19:31 -0800706}
707
708/*
709 * handle a virtual interrupt generated by the lower level "virtual DMA"
710 * routines (irq is the baud timer interrupt)
711 */
712static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
713{
714 struct mn10300_serial_port *port = dev_id;
715 u8 st;
716
717 spin_lock(&port->uart.lock);
718
719 if (port->intr_flags) {
720 _debug("INT %s: %x", port->name, port->intr_flags);
721
722 if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
723 mn10300_serial_receive_interrupt(port);
724
725 if (mask_test_and_clear(&port->intr_flags,
726 MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
727 mn10300_serial_transmit_interrupt(port);
728 }
729
730 /* the only modem control line amongst the whole lot is CTS on
731 * serial port 2 */
732 if (port->type == PORT_MN10300_CTS) {
733 st = *port->_status;
734 if ((port->tx_cts ^ st) & SC2STR_CTS)
735 mn10300_serial_cts_changed(port, st);
736 }
737
738 spin_unlock(&port->uart.lock);
739
740 return IRQ_HANDLED;
741}
742
743/*
744 * return indication of whether the hardware transmit buffer is empty
745 */
746static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
747{
748 struct mn10300_serial_port *port =
749 container_of(_port, struct mn10300_serial_port, uart);
750
751 _enter("%s", port->name);
752
753 return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
754 0 : TIOCSER_TEMT;
755}
756
757/*
758 * set the modem control lines (we don't have any)
759 */
760static void mn10300_serial_set_mctrl(struct uart_port *_port,
761 unsigned int mctrl)
762{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100763 struct mn10300_serial_port *port __attribute__ ((unused)) =
David Howellsb920de12008-02-08 04:19:31 -0800764 container_of(_port, struct mn10300_serial_port, uart);
765
766 _enter("%s,%x", port->name, mctrl);
767}
768
769/*
770 * get the modem control line statuses
771 */
772static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
773{
774 struct mn10300_serial_port *port =
775 container_of(_port, struct mn10300_serial_port, uart);
776
777 _enter("%s", port->name);
778
779 if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
780 return TIOCM_CAR | TIOCM_DSR;
781
782 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
783}
784
785/*
786 * stop transmitting characters
787 */
788static void mn10300_serial_stop_tx(struct uart_port *_port)
789{
790 struct mn10300_serial_port *port =
791 container_of(_port, struct mn10300_serial_port, uart);
792
793 _enter("%s", port->name);
794
795 /* disable the virtual DMA */
796 mn10300_serial_dis_tx_intr(port);
797}
798
799/*
800 * start transmitting characters
801 * - jump-start transmission if it has stalled
802 * - enable the serial Tx interrupt (used by the virtual DMA controller)
803 * - force an interrupt to happen if necessary
804 */
805static void mn10300_serial_start_tx(struct uart_port *_port)
806{
807 struct mn10300_serial_port *port =
808 container_of(_port, struct mn10300_serial_port, uart);
809
810 u16 x;
811
812 _enter("%s{%lu}",
813 port->name,
David Howells70430782009-09-23 10:40:24 +0100814 CIRC_CNT(&port->uart.state->xmit.head,
815 &port->uart.state->xmit.tail,
David Howellsb920de12008-02-08 04:19:31 -0800816 UART_XMIT_SIZE));
817
818 /* kick the virtual DMA controller */
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100819 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -0800820 x = *port->tx_icr;
821 x |= GxICR_ENABLE;
822
823 if (*port->_status & SC01STR_TBF)
824 x &= ~(GxICR_REQUEST | GxICR_DETECT);
825 else
826 x |= GxICR_REQUEST | GxICR_DETECT;
827
828 _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
829 *port->_control, *port->_intr, *port->_status,
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100830 *port->_tmxmd,
831 (port->div_timer == MNSCx_DIV_TIMER_8BIT) ?
832 *(volatile u8 *)port->_tmxbr : *port->_tmxbr,
833 *port->tx_icr);
David Howellsb920de12008-02-08 04:19:31 -0800834
835 *port->tx_icr = x;
836 x = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100837 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -0800838}
839
840/*
841 * transmit a high-priority XON/XOFF character
842 */
843static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
844{
845 struct mn10300_serial_port *port =
846 container_of(_port, struct mn10300_serial_port, uart);
847
848 _enter("%s,%02x", port->name, ch);
849
850 if (likely(port->gdbstub)) {
851 port->tx_xchar = ch;
852 if (ch)
853 mn10300_serial_en_tx_intr(port);
854 }
855}
856
857/*
858 * stop receiving characters
859 * - called whilst the port is being closed
860 */
861static void mn10300_serial_stop_rx(struct uart_port *_port)
862{
863 struct mn10300_serial_port *port =
864 container_of(_port, struct mn10300_serial_port, uart);
865
866 u16 ctr;
867
868 _enter("%s", port->name);
869
870 ctr = *port->_control;
871 ctr &= ~SC01CTR_RXE;
872 *port->_control = ctr;
873
874 mn10300_serial_dis_rx_intr(port);
875}
876
877/*
878 * enable modem status interrupts
879 */
880static void mn10300_serial_enable_ms(struct uart_port *_port)
881{
882 struct mn10300_serial_port *port =
883 container_of(_port, struct mn10300_serial_port, uart);
884
885 u16 ctr, cts;
886
887 _enter("%s", port->name);
888
889 if (port->type == PORT_MN10300_CTS) {
890 /* want to interrupt when CTS goes low if CTS is now high and
891 * vice versa
892 */
893 port->tx_cts = *port->_status;
894
895 cts = (port->tx_cts & SC2STR_CTS) ?
896 SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
897
898 ctr = *port->_control;
899 ctr &= ~SC2CTR_TWS;
900 ctr |= cts;
901 *port->_control = ctr;
902
903 mn10300_serial_en_tx_intr(port);
904 }
905}
906
907/*
908 * transmit or cease transmitting a break signal
909 */
910static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
911{
912 struct mn10300_serial_port *port =
913 container_of(_port, struct mn10300_serial_port, uart);
914
915 _enter("%s,%d", port->name, ctl);
916
917 if (ctl) {
918 /* tell the virtual DMA handler to assert BREAK */
919 port->tx_break = 1;
920 mn10300_serial_en_tx_intr(port);
921 } else {
922 port->tx_break = 0;
923 *port->_control &= ~SC01CTR_BKE;
924 mn10300_serial_en_tx_intr(port);
925 }
926}
927
928/*
929 * grab the interrupts and enable the port for reception
930 */
931static int mn10300_serial_startup(struct uart_port *_port)
932{
933 struct mn10300_serial_port *port =
934 container_of(_port, struct mn10300_serial_port, uart);
935 struct mn10300_serial_int *pint;
936
937 _enter("%s{%d}", port->name, port->gdbstub);
938
939 if (unlikely(port->gdbstub))
940 return -EBUSY;
941
942 /* allocate an Rx buffer for the virtual DMA handler */
943 port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
944 if (!port->rx_buffer)
945 return -ENOMEM;
946
947 port->rx_inp = port->rx_outp = 0;
948
949 /* finally, enable the device */
950 *port->_intr = SC01ICR_TI;
951 *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
952
953 pint = &mn10300_serial_int_tbl[port->rx_irq];
954 pint->port = port;
955 pint->vdma = mn10300_serial_vdma_rx_handler;
956 pint = &mn10300_serial_int_tbl[port->tx_irq];
957 pint->port = port;
958 pint->vdma = mn10300_serial_vdma_tx_handler;
959
Mark Salter8d160022012-12-12 15:36:38 +0000960 irq_set_chip(port->rx_irq, &mn10300_serial_low_pic);
961 irq_set_chip(port->tx_irq, &mn10300_serial_low_pic);
Thomas Gleixnerf4c547e2011-03-24 17:35:56 +0100962 irq_set_chip(port->tm_irq, &mn10300_serial_pic);
David Howellsb920de12008-02-08 04:19:31 -0800963
964 if (request_irq(port->rx_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000965 IRQF_DISABLED | IRQF_NOBALANCING,
966 port->rx_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800967 goto error;
968
969 if (request_irq(port->tx_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000970 IRQF_DISABLED | IRQF_NOBALANCING,
971 port->tx_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800972 goto error2;
973
974 if (request_irq(port->tm_irq, mn10300_serial_interrupt,
Mark Salter7d361cb2012-12-12 15:36:37 +0000975 IRQF_DISABLED | IRQF_NOBALANCING,
976 port->tm_name, port) < 0)
David Howellsb920de12008-02-08 04:19:31 -0800977 goto error3;
978 mn10300_serial_mask_ack(port->tm_irq);
979
980 return 0;
981
982error3:
983 free_irq(port->tx_irq, port);
984error2:
985 free_irq(port->rx_irq, port);
986error:
987 kfree(port->rx_buffer);
988 port->rx_buffer = NULL;
989 return -EBUSY;
990}
991
992/*
993 * shutdown the port and release interrupts
994 */
995static void mn10300_serial_shutdown(struct uart_port *_port)
996{
Akira Takeuchi368dd5a2010-10-27 17:28:55 +0100997 u16 x;
David Howellsb920de12008-02-08 04:19:31 -0800998 struct mn10300_serial_port *port =
999 container_of(_port, struct mn10300_serial_port, uart);
1000
1001 _enter("%s", port->name);
1002
1003 /* disable the serial port and its baud rate timer */
1004 port->tx_break = 0;
1005 *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1006 *port->_tmxmd = 0;
1007
1008 if (port->rx_buffer) {
1009 void *buf = port->rx_buffer;
1010 port->rx_buffer = NULL;
1011 kfree(buf);
1012 }
1013
1014 /* disable all intrs */
1015 free_irq(port->tm_irq, port);
1016 free_irq(port->rx_irq, port);
1017 free_irq(port->tx_irq, port);
1018
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001019 arch_local_cli();
1020 *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
1021 x = *port->rx_icr;
1022 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
1023 x = *port->tx_icr;
1024 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -08001025}
1026
1027/*
1028 * this routine is called to set the UART divisor registers to match the
1029 * specified baud rate for a serial port.
1030 */
1031static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
1032 struct ktermios *new,
1033 struct ktermios *old)
1034{
1035 unsigned long flags;
1036 unsigned long ioclk = port->ioclk;
1037 unsigned cflag;
1038 int baud, bits, xdiv, tmp;
1039 u16 tmxbr, scxctr;
1040 u8 tmxmd, battempt;
1041 u8 div_timer = port->div_timer;
1042
1043 _enter("%s{%lu}", port->name, ioclk);
1044
1045 /* byte size and parity */
1046 cflag = new->c_cflag;
1047 switch (cflag & CSIZE) {
1048 case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
1049 case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
1050 default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
1051 }
1052
1053 if (cflag & CSTOPB) {
1054 scxctr |= SC01CTR_STB_2BIT;
1055 bits++;
1056 }
1057
1058 if (cflag & PARENB) {
1059 bits++;
1060 if (cflag & PARODD)
1061 scxctr |= SC01CTR_PB_ODD;
1062#ifdef CMSPAR
1063 else if (cflag & CMSPAR)
1064 scxctr |= SC01CTR_PB_FIXED0;
1065#endif
1066 else
1067 scxctr |= SC01CTR_PB_EVEN;
1068 }
1069
1070 /* Determine divisor based on baud rate */
1071 battempt = 0;
1072
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001073 switch (port->uart.line) {
1074#ifdef CONFIG_MN10300_TTYSM0
1075 case 0: /* ttySM0 */
1076#if defined(CONFIG_MN10300_TTYSM0_TIMER8)
1077 scxctr |= SC0CTR_CK_TM8UFLOW_8;
1078#elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
1079 scxctr |= SC0CTR_CK_TM0UFLOW_8;
1080#elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
David Howellsb920de12008-02-08 04:19:31 -08001081 scxctr |= SC0CTR_CK_TM2UFLOW_8;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001082#else
1083#error "Unknown config for ttySM0"
1084#endif
1085 break;
1086#endif /* CONFIG_MN10300_TTYSM0 */
1087
1088#ifdef CONFIG_MN10300_TTYSM1
1089 case 1: /* ttySM1 */
1090#if defined(CONFIG_AM33_2) || defined(CONFIG_AM33_3)
1091#if defined(CONFIG_MN10300_TTYSM1_TIMER9)
1092 scxctr |= SC1CTR_CK_TM9UFLOW_8;
1093#elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
1094 scxctr |= SC1CTR_CK_TM3UFLOW_8;
1095#else
1096#error "Unknown config for ttySM1"
1097#endif
1098#else /* CONFIG_AM33_2 || CONFIG_AM33_3 */
1099#if defined(CONFIG_MN10300_TTYSM1_TIMER12)
1100 scxctr |= SC1CTR_CK_TM12UFLOW_8;
1101#else
1102#error "Unknown config for ttySM1"
1103#endif
1104#endif /* CONFIG_AM33_2 || CONFIG_AM33_3 */
1105 break;
1106#endif /* CONFIG_MN10300_TTYSM1 */
1107
1108#ifdef CONFIG_MN10300_TTYSM2
1109 case 2: /* ttySM2 */
1110#if defined(CONFIG_AM33_2)
1111#if defined(CONFIG_MN10300_TTYSM2_TIMER10)
1112 scxctr |= SC2CTR_CK_TM10UFLOW;
1113#else
1114#error "Unknown config for ttySM2"
1115#endif
1116#else /* CONFIG_AM33_2 */
1117#if defined(CONFIG_MN10300_TTYSM2_TIMER9)
1118 scxctr |= SC2CTR_CK_TM9UFLOW_8;
1119#elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
1120 scxctr |= SC2CTR_CK_TM1UFLOW_8;
1121#elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
1122 scxctr |= SC2CTR_CK_TM3UFLOW_8;
1123#else
1124#error "Unknown config for ttySM2"
1125#endif
1126#endif /* CONFIG_AM33_2 */
1127 break;
1128#endif /* CONFIG_MN10300_TTYSM2 */
1129
1130 default:
1131 break;
1132 }
David Howellsb920de12008-02-08 04:19:31 -08001133
1134try_alternative:
1135 baud = uart_get_baud_rate(&port->uart, new, old, 0,
1136 port->ioclk / 8);
1137
1138 _debug("ALT %d [baud %d]", battempt, baud);
1139
1140 if (!baud)
1141 baud = 9600; /* B0 transition handled in rs_set_termios */
1142 xdiv = 1;
1143 if (baud == 134) {
1144 baud = 269; /* 134 is really 134.5 */
1145 xdiv = 2;
1146 }
1147
1148 if (baud == 38400 &&
1149 (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
1150 ) {
1151 _debug("CUSTOM %u", port->uart.custom_divisor);
1152
1153 if (div_timer == MNSCx_DIV_TIMER_16BIT) {
1154 if (port->uart.custom_divisor <= 65535) {
1155 tmxmd = TM8MD_SRC_IOCLK;
1156 tmxbr = port->uart.custom_divisor;
1157 port->uart.uartclk = ioclk;
1158 goto timer_okay;
1159 }
1160 if (port->uart.custom_divisor / 8 <= 65535) {
1161 tmxmd = TM8MD_SRC_IOCLK_8;
1162 tmxbr = port->uart.custom_divisor / 8;
1163 port->uart.custom_divisor = tmxbr * 8;
1164 port->uart.uartclk = ioclk / 8;
1165 goto timer_okay;
1166 }
1167 if (port->uart.custom_divisor / 32 <= 65535) {
1168 tmxmd = TM8MD_SRC_IOCLK_32;
1169 tmxbr = port->uart.custom_divisor / 32;
1170 port->uart.custom_divisor = tmxbr * 32;
1171 port->uart.uartclk = ioclk / 32;
1172 goto timer_okay;
1173 }
1174
1175 } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
1176 if (port->uart.custom_divisor <= 255) {
1177 tmxmd = TM2MD_SRC_IOCLK;
1178 tmxbr = port->uart.custom_divisor;
1179 port->uart.uartclk = ioclk;
1180 goto timer_okay;
1181 }
1182 if (port->uart.custom_divisor / 8 <= 255) {
1183 tmxmd = TM2MD_SRC_IOCLK_8;
1184 tmxbr = port->uart.custom_divisor / 8;
1185 port->uart.custom_divisor = tmxbr * 8;
1186 port->uart.uartclk = ioclk / 8;
1187 goto timer_okay;
1188 }
1189 if (port->uart.custom_divisor / 32 <= 255) {
1190 tmxmd = TM2MD_SRC_IOCLK_32;
1191 tmxbr = port->uart.custom_divisor / 32;
1192 port->uart.custom_divisor = tmxbr * 32;
1193 port->uart.uartclk = ioclk / 32;
1194 goto timer_okay;
1195 }
1196 }
1197 }
1198
1199 switch (div_timer) {
1200 case MNSCx_DIV_TIMER_16BIT:
1201 port->uart.uartclk = ioclk;
1202 tmxmd = TM8MD_SRC_IOCLK;
1203 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1204 if (tmp > 0 && tmp <= 65535)
1205 goto timer_okay;
1206
1207 port->uart.uartclk = ioclk / 8;
1208 tmxmd = TM8MD_SRC_IOCLK_8;
1209 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1210 if (tmp > 0 && tmp <= 65535)
1211 goto timer_okay;
1212
1213 port->uart.uartclk = ioclk / 32;
1214 tmxmd = TM8MD_SRC_IOCLK_32;
1215 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1216 if (tmp > 0 && tmp <= 65535)
1217 goto timer_okay;
1218 break;
1219
1220 case MNSCx_DIV_TIMER_8BIT:
1221 port->uart.uartclk = ioclk;
1222 tmxmd = TM2MD_SRC_IOCLK;
1223 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1224 if (tmp > 0 && tmp <= 255)
1225 goto timer_okay;
1226
1227 port->uart.uartclk = ioclk / 8;
1228 tmxmd = TM2MD_SRC_IOCLK_8;
1229 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1230 if (tmp > 0 && tmp <= 255)
1231 goto timer_okay;
1232
1233 port->uart.uartclk = ioclk / 32;
1234 tmxmd = TM2MD_SRC_IOCLK_32;
1235 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1236 if (tmp > 0 && tmp <= 255)
1237 goto timer_okay;
1238 break;
1239
1240 default:
1241 BUG();
1242 return;
1243 }
1244
1245 /* refuse to change to a baud rate we can't support */
1246 _debug("CAN'T SUPPORT");
1247
1248 switch (battempt) {
1249 case 0:
1250 if (old) {
1251 new->c_cflag &= ~CBAUD;
1252 new->c_cflag |= (old->c_cflag & CBAUD);
1253 battempt = 1;
1254 goto try_alternative;
1255 }
1256
1257 case 1:
1258 /* as a last resort, if the quotient is zero, default to 9600
1259 * bps */
1260 new->c_cflag &= ~CBAUD;
1261 new->c_cflag |= B9600;
1262 battempt = 2;
1263 goto try_alternative;
1264
1265 default:
1266 /* hmmm... can't seem to support 9600 either
1267 * - we could try iterating through the speeds we know about to
1268 * find the lowest
1269 */
1270 new->c_cflag &= ~CBAUD;
1271 new->c_cflag |= B0;
1272
1273 if (div_timer == MNSCx_DIV_TIMER_16BIT)
1274 tmxmd = TM8MD_SRC_IOCLK_32;
1275 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
1276 tmxmd = TM2MD_SRC_IOCLK_32;
1277 tmxbr = 1;
1278
1279 port->uart.uartclk = ioclk / 32;
1280 break;
1281 }
1282timer_okay:
1283
1284 _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
1285
1286 /* make the changes */
1287 spin_lock_irqsave(&port->uart.lock, flags);
1288
1289 uart_update_timeout(&port->uart, new->c_cflag, baud);
1290
1291 /* set the timer to produce the required baud rate */
1292 switch (div_timer) {
1293 case MNSCx_DIV_TIMER_16BIT:
1294 *port->_tmxmd = 0;
1295 *port->_tmxbr = tmxbr;
1296 *port->_tmxmd = TM8MD_INIT_COUNTER;
1297 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1298 break;
1299
1300 case MNSCx_DIV_TIMER_8BIT:
1301 *port->_tmxmd = 0;
1302 *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
1303 *port->_tmxmd = TM2MD_INIT_COUNTER;
1304 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1305 break;
1306 }
1307
1308 /* CTS flow control flag and modem status interrupts */
1309 scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
1310
1311 if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
1312 /* want to interrupt when CTS goes low if CTS is now
1313 * high and vice versa
1314 */
1315 port->tx_cts = *port->_status;
1316
1317 if (port->tx_cts & SC2STR_CTS)
1318 scxctr |= SC2CTR_TWE;
1319 else
1320 scxctr |= SC2CTR_TWE | SC2CTR_TWS;
1321 }
1322
1323 /* set up parity check flag */
1324 port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
1325 if (new->c_iflag & INPCK)
1326 port->uart.read_status_mask |=
1327 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1328 if (new->c_iflag & (BRKINT | PARMRK))
1329 port->uart.read_status_mask |= (1 << TTY_BREAK);
1330
1331 /* characters to ignore */
1332 port->uart.ignore_status_mask = 0;
1333 if (new->c_iflag & IGNPAR)
1334 port->uart.ignore_status_mask |=
1335 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1336 if (new->c_iflag & IGNBRK) {
1337 port->uart.ignore_status_mask |= (1 << TTY_BREAK);
1338 /*
1339 * If we're ignoring parity and break indicators,
1340 * ignore overruns to (for real raw support).
1341 */
1342 if (new->c_iflag & IGNPAR)
1343 port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
1344 }
1345
1346 /* Ignore all characters if CREAD is not set */
1347 if ((new->c_cflag & CREAD) == 0)
1348 port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
1349
1350 scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1351 *port->_control = scxctr;
1352
1353 spin_unlock_irqrestore(&port->uart.lock, flags);
1354}
1355
1356/*
1357 * set the terminal I/O parameters
1358 */
1359static void mn10300_serial_set_termios(struct uart_port *_port,
1360 struct ktermios *new,
1361 struct ktermios *old)
1362{
1363 struct mn10300_serial_port *port =
1364 container_of(_port, struct mn10300_serial_port, uart);
1365
1366 _enter("%s,%p,%p", port->name, new, old);
1367
1368 mn10300_serial_change_speed(port, new, old);
1369
1370 /* handle turning off CRTSCTS */
1371 if (!(new->c_cflag & CRTSCTS)) {
1372 u16 ctr = *port->_control;
1373 ctr &= ~SC2CTR_TWE;
1374 *port->_control = ctr;
1375 }
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001376
1377 /* change Transfer bit-order (LSB/MSB) */
1378 if (new->c_cflag & CODMSB)
1379 *port->_control |= SC01CTR_OD_MSBFIRST; /* MSB MODE */
1380 else
1381 *port->_control &= ~SC01CTR_OD_MSBFIRST; /* LSB MODE */
David Howellsb920de12008-02-08 04:19:31 -08001382}
1383
1384/*
1385 * return description of port type
1386 */
1387static const char *mn10300_serial_type(struct uart_port *_port)
1388{
1389 struct mn10300_serial_port *port =
1390 container_of(_port, struct mn10300_serial_port, uart);
1391
1392 if (port->uart.type == PORT_MN10300_CTS)
1393 return "MN10300 SIF_CTS";
1394
1395 return "MN10300 SIF";
1396}
1397
1398/*
1399 * release I/O and memory regions in use by port
1400 */
1401static void mn10300_serial_release_port(struct uart_port *_port)
1402{
1403 struct mn10300_serial_port *port =
1404 container_of(_port, struct mn10300_serial_port, uart);
1405
1406 _enter("%s", port->name);
1407
1408 release_mem_region((unsigned long) port->_iobase, 16);
1409}
1410
1411/*
1412 * request I/O and memory regions for port
1413 */
1414static int mn10300_serial_request_port(struct uart_port *_port)
1415{
1416 struct mn10300_serial_port *port =
1417 container_of(_port, struct mn10300_serial_port, uart);
1418
1419 _enter("%s", port->name);
1420
1421 request_mem_region((unsigned long) port->_iobase, 16, port->name);
1422 return 0;
1423}
1424
1425/*
1426 * configure the type and reserve the ports
1427 */
1428static void mn10300_serial_config_port(struct uart_port *_port, int type)
1429{
1430 struct mn10300_serial_port *port =
1431 container_of(_port, struct mn10300_serial_port, uart);
1432
1433 _enter("%s", port->name);
1434
1435 port->uart.type = PORT_MN10300;
1436
1437 if (port->options & MNSCx_OPT_CTS)
1438 port->uart.type = PORT_MN10300_CTS;
1439
1440 mn10300_serial_request_port(_port);
1441}
1442
1443/*
1444 * verify serial parameters are suitable for this port type
1445 */
1446static int mn10300_serial_verify_port(struct uart_port *_port,
1447 struct serial_struct *ss)
1448{
1449 struct mn10300_serial_port *port =
1450 container_of(_port, struct mn10300_serial_port, uart);
1451 void *mapbase = (void *) (unsigned long) port->uart.mapbase;
1452
1453 _enter("%s", port->name);
1454
1455 /* these things may not be changed */
1456 if (ss->irq != port->uart.irq ||
1457 ss->port != port->uart.iobase ||
1458 ss->io_type != port->uart.iotype ||
1459 ss->iomem_base != mapbase ||
1460 ss->iomem_reg_shift != port->uart.regshift ||
1461 ss->hub6 != port->uart.hub6 ||
1462 ss->xmit_fifo_size != port->uart.fifosize)
1463 return -EINVAL;
1464
1465 /* type may be changed on a port that supports CTS */
1466 if (ss->type != port->uart.type) {
1467 if (!(port->options & MNSCx_OPT_CTS))
1468 return -EINVAL;
1469
1470 if (ss->type != PORT_MN10300 &&
1471 ss->type != PORT_MN10300_CTS)
1472 return -EINVAL;
1473 }
1474
1475 return 0;
1476}
1477
1478/*
1479 * initialise the MN10300 on-chip UARTs
1480 */
1481static int __init mn10300_serial_init(void)
1482{
1483 struct mn10300_serial_port *port;
1484 int ret, i;
1485
1486 printk(KERN_INFO "%s version %s (%s)\n",
1487 serial_name, serial_version, serial_revdate);
1488
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001489#if defined(CONFIG_MN10300_TTYSM2) && defined(CONFIG_AM33_2)
1490 {
1491 int tmp;
1492 SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
1493 tmp = SC2TIM;
1494 }
David Howellsb920de12008-02-08 04:19:31 -08001495#endif
1496
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001497 set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL),
1498 mn10300_serial_vdma_interrupt);
David Howellsb920de12008-02-08 04:19:31 -08001499
1500 ret = uart_register_driver(&mn10300_serial_driver);
1501 if (!ret) {
1502 for (i = 0 ; i < NR_PORTS ; i++) {
1503 port = mn10300_serial_ports[i];
1504 if (!port || port->gdbstub)
1505 continue;
1506
1507 switch (port->clock_src) {
1508 case MNSCx_CLOCK_SRC_IOCLK:
1509 port->ioclk = MN10300_IOCLK;
1510 break;
1511
1512#ifdef MN10300_IOBCLK
1513 case MNSCx_CLOCK_SRC_IOBCLK:
1514 port->ioclk = MN10300_IOBCLK;
1515 break;
1516#endif
1517 default:
1518 BUG();
1519 }
1520
1521 ret = uart_add_one_port(&mn10300_serial_driver,
1522 &port->uart);
1523
1524 if (ret < 0) {
1525 _debug("ERROR %d", -ret);
1526 break;
1527 }
1528 }
1529
1530 if (ret)
1531 uart_unregister_driver(&mn10300_serial_driver);
1532 }
1533
1534 return ret;
1535}
1536
1537__initcall(mn10300_serial_init);
1538
1539
1540#ifdef CONFIG_MN10300_TTYSM_CONSOLE
1541
1542/*
1543 * print a string to the serial port without disturbing the real user of the
1544 * port too much
1545 * - the console must be locked by the caller
1546 */
1547static void mn10300_serial_console_write(struct console *co,
1548 const char *s, unsigned count)
1549{
1550 struct mn10300_serial_port *port;
1551 unsigned i;
1552 u16 scxctr, txicr, tmp;
1553 u8 tmxmd;
1554
1555 port = mn10300_serial_ports[co->index];
1556
1557 /* firstly hijack the serial port from the "virtual DMA" controller */
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001558 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -08001559 txicr = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001560 *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
David Howellsb920de12008-02-08 04:19:31 -08001561 tmp = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001562 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -08001563
1564 /* the transmitter may be disabled */
1565 scxctr = *port->_control;
1566 if (!(scxctr & SC01CTR_TXE)) {
1567 /* restart the UART clock */
1568 tmxmd = *port->_tmxmd;
1569
1570 switch (port->div_timer) {
1571 case MNSCx_DIV_TIMER_16BIT:
1572 *port->_tmxmd = 0;
1573 *port->_tmxmd = TM8MD_INIT_COUNTER;
1574 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1575 break;
1576
1577 case MNSCx_DIV_TIMER_8BIT:
1578 *port->_tmxmd = 0;
1579 *port->_tmxmd = TM2MD_INIT_COUNTER;
1580 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1581 break;
1582 }
1583
1584 /* enable the transmitter */
1585 *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
1586
1587 } else if (scxctr & SC01CTR_BKE) {
1588 /* stop transmitting BREAK */
1589 *port->_control = (scxctr & ~SC01CTR_BKE);
1590 }
1591
1592 /* send the chars into the serial port (with LF -> LFCR conversion) */
1593 for (i = 0; i < count; i++) {
1594 char ch = *s++;
1595
1596 while (*port->_status & SC01STR_TBF)
1597 continue;
1598 *(u8 *) port->_txb = ch;
1599
1600 if (ch == 0x0a) {
1601 while (*port->_status & SC01STR_TBF)
1602 continue;
1603 *(u8 *) port->_txb = 0xd;
1604 }
1605 }
1606
1607 /* can't let the transmitter be turned off if it's actually
1608 * transmitting */
1609 while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
1610 continue;
1611
1612 /* disable the transmitter if we re-enabled it */
1613 if (!(scxctr & SC01CTR_TXE))
1614 *port->_control = scxctr;
1615
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001616 arch_local_cli();
David Howellsb920de12008-02-08 04:19:31 -08001617 *port->tx_icr = txicr;
1618 tmp = *port->tx_icr;
Akira Takeuchi368dd5a2010-10-27 17:28:55 +01001619 arch_local_sti();
David Howellsb920de12008-02-08 04:19:31 -08001620}
1621
1622/*
1623 * set up a serial port as a console
1624 * - construct a cflag setting for the first rs_open()
1625 * - initialize the serial port
1626 * - return non-zero if we didn't find a serial port.
1627 */
1628static int __init mn10300_serial_console_setup(struct console *co,
1629 char *options)
1630{
1631 struct mn10300_serial_port *port;
1632 int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
1633
1634 for (i = 0 ; i < NR_PORTS ; i++) {
1635 port = mn10300_serial_ports[i];
1636 if (port && !port->gdbstub && port->uart.line == co->index)
1637 goto found_device;
1638 }
1639
1640 return -ENODEV;
1641
1642found_device:
1643 switch (port->clock_src) {
1644 case MNSCx_CLOCK_SRC_IOCLK:
1645 port->ioclk = MN10300_IOCLK;
1646 break;
1647
1648#ifdef MN10300_IOBCLK
1649 case MNSCx_CLOCK_SRC_IOBCLK:
1650 port->ioclk = MN10300_IOBCLK;
1651 break;
1652#endif
1653 default:
1654 BUG();
1655 }
1656
1657 if (options)
1658 uart_parse_options(options, &baud, &parity, &bits, &flow);
1659
1660 return uart_set_options(&port->uart, co, baud, parity, bits, flow);
1661}
1662
1663/*
1664 * register console
1665 */
1666static int __init mn10300_serial_console_init(void)
1667{
1668 register_console(&mn10300_serial_console);
1669 return 0;
1670}
1671
1672console_initcall(mn10300_serial_console_init);
1673#endif
David Howells52885b32011-03-18 16:54:32 +00001674
1675#ifdef CONFIG_CONSOLE_POLL
1676/*
1677 * Polled character reception for the kernel debugger
1678 */
1679static int mn10300_serial_poll_get_char(struct uart_port *_port)
1680{
1681 struct mn10300_serial_port *port =
1682 container_of(_port, struct mn10300_serial_port, uart);
1683 unsigned ix;
1684 u8 st, ch;
1685
1686 _enter("%s", port->name);
1687
1688 do {
1689 /* pull chars out of the hat */
David Howellsc98c4062012-12-12 15:36:37 +00001690 ix = ACCESS_ONCE(port->rx_outp);
1691 if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0)
David Howells52885b32011-03-18 16:54:32 +00001692 return NO_POLL_CHAR;
1693
David Howellsc98c4062012-12-12 15:36:37 +00001694 smp_read_barrier_depends();
David Howells52885b32011-03-18 16:54:32 +00001695 ch = port->rx_buffer[ix++];
1696 st = port->rx_buffer[ix++];
David Howellsc98c4062012-12-12 15:36:37 +00001697 smp_mb();
David Howells52885b32011-03-18 16:54:32 +00001698 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
1699
1700 } while (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF));
1701
1702 return ch;
1703}
1704
1705
1706/*
1707 * Polled character transmission for the kernel debugger
1708 */
1709static void mn10300_serial_poll_put_char(struct uart_port *_port,
1710 unsigned char ch)
1711{
1712 struct mn10300_serial_port *port =
1713 container_of(_port, struct mn10300_serial_port, uart);
1714 u8 intr, tmp;
1715
1716 /* wait for the transmitter to finish anything it might be doing (and
1717 * this includes the virtual DMA handler, so it might take a while) */
1718 while (*port->_status & (SC01STR_TBF | SC01STR_TXF))
1719 continue;
1720
1721 /* disable the Tx ready interrupt */
1722 intr = *port->_intr;
1723 *port->_intr = intr & ~SC01ICR_TI;
1724 tmp = *port->_intr;
1725
1726 if (ch == 0x0a) {
1727 *(u8 *) port->_txb = 0x0d;
1728 while (*port->_status & SC01STR_TBF)
1729 continue;
1730 }
1731
1732 *(u8 *) port->_txb = ch;
1733 while (*port->_status & SC01STR_TBF)
1734 continue;
1735
1736 /* restore the Tx interrupt flag */
1737 *port->_intr = intr;
1738 tmp = *port->_intr;
1739}
1740
1741#endif /* CONFIG_CONSOLE_POLL */