1 /* MN10300 On-chip serial port UART driver
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 static const char serial_name[] = "MN10300 Serial driver";
13 static const char serial_version[] = "mn10300_serial-1.0";
14 static const char serial_revdate[] = "2007-11-06";
16 #if defined(CONFIG_MN10300_TTYSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20 #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>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/console.h>
37 #include <linux/sysrq.h>
39 #include <asm/system.h>
42 #include <asm/bitops.h>
43 #include <asm/serial-regs.h>
44 #include <asm/unit/timex.h>
45 #include "mn10300-serial.h"
47 static inline __attribute__((format(printf, 1, 2)))
48 void no_printk(const char *fmt, ...)
52 #define kenter(FMT, ...) \
53 printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
54 #define _enter(FMT, ...) \
55 no_printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
56 #define kdebug(FMT, ...) \
57 printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
58 #define _debug(FMT, ...) \
59 no_printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
60 #define kproto(FMT, ...) \
61 printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
62 #define _proto(FMT, ...) \
63 no_printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
67 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
68 static void mn10300_serial_console_write(struct console *co,
69 const char *s, unsigned count);
70 static int __init mn10300_serial_console_setup(struct console *co,
73 static struct uart_driver mn10300_serial_driver;
74 static struct console mn10300_serial_console = {
76 .write = mn10300_serial_console_write,
77 .device = uart_console_device,
78 .setup = mn10300_serial_console_setup,
79 .flags = CON_PRINTBUFFER,
81 .data = &mn10300_serial_driver,
85 static struct uart_driver mn10300_serial_driver = {
87 .driver_name = "mn10300-serial",
92 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
93 .cons = &mn10300_serial_console,
97 static unsigned int mn10300_serial_tx_empty(struct uart_port *);
98 static void mn10300_serial_set_mctrl(struct uart_port *, unsigned int mctrl);
99 static unsigned int mn10300_serial_get_mctrl(struct uart_port *);
100 static void mn10300_serial_stop_tx(struct uart_port *);
101 static void mn10300_serial_start_tx(struct uart_port *);
102 static void mn10300_serial_send_xchar(struct uart_port *, char ch);
103 static void mn10300_serial_stop_rx(struct uart_port *);
104 static void mn10300_serial_enable_ms(struct uart_port *);
105 static void mn10300_serial_break_ctl(struct uart_port *, int ctl);
106 static int mn10300_serial_startup(struct uart_port *);
107 static void mn10300_serial_shutdown(struct uart_port *);
108 static void mn10300_serial_set_termios(struct uart_port *,
109 struct ktermios *new,
110 struct ktermios *old);
111 static const char *mn10300_serial_type(struct uart_port *);
112 static void mn10300_serial_release_port(struct uart_port *);
113 static int mn10300_serial_request_port(struct uart_port *);
114 static void mn10300_serial_config_port(struct uart_port *, int);
115 static int mn10300_serial_verify_port(struct uart_port *,
116 struct serial_struct *);
118 static const struct uart_ops mn10300_serial_ops = {
119 .tx_empty = mn10300_serial_tx_empty,
120 .set_mctrl = mn10300_serial_set_mctrl,
121 .get_mctrl = mn10300_serial_get_mctrl,
122 .stop_tx = mn10300_serial_stop_tx,
123 .start_tx = mn10300_serial_start_tx,
124 .send_xchar = mn10300_serial_send_xchar,
125 .stop_rx = mn10300_serial_stop_rx,
126 .enable_ms = mn10300_serial_enable_ms,
127 .break_ctl = mn10300_serial_break_ctl,
128 .startup = mn10300_serial_startup,
129 .shutdown = mn10300_serial_shutdown,
130 .set_termios = mn10300_serial_set_termios,
131 .type = mn10300_serial_type,
132 .release_port = mn10300_serial_release_port,
133 .request_port = mn10300_serial_request_port,
134 .config_port = mn10300_serial_config_port,
135 .verify_port = mn10300_serial_verify_port,
138 static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id);
141 * the first on-chip serial port: ttySM0 (aka SIF0)
143 #ifdef CONFIG_MN10300_TTYSM0
144 struct mn10300_serial_port mn10300_serial_port_sif0 = {
145 .uart.ops = &mn10300_serial_ops,
146 .uart.membase = (void __iomem *) &SC0CTR,
147 .uart.mapbase = (unsigned long) &SC0CTR,
148 .uart.iotype = UPIO_MEM,
150 .uart.uartclk = 0, /* MN10300_IOCLK, */
152 .uart.flags = UPF_BOOT_AUTOCONF,
154 .uart.type = PORT_MN10300,
156 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif0.uart.lock),
160 ._status = (volatile u8 *) &SC0STR,
164 .rx_name = "ttySM0/Rx",
165 .tx_name = "ttySM0/Tx",
166 #ifdef CONFIG_MN10300_TTYSM0_TIMER8
167 .tm_name = "ttySM0/Timer8",
172 .div_timer = MNSCx_DIV_TIMER_16BIT,
173 #else /* CONFIG_MN10300_TTYSM0_TIMER2 */
174 .tm_name = "ttySM0/Timer2",
176 ._tmxbr = (volatile u16 *) &TM2BR,
179 .div_timer = MNSCx_DIV_TIMER_8BIT,
183 .rx_icr = &GxICR(SC0RXIRQ),
184 .tx_icr = &GxICR(SC0TXIRQ),
185 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
187 #ifdef CONFIG_GDBSTUB_ON_TTYSM0
191 #endif /* CONFIG_MN10300_TTYSM0 */
194 * the second on-chip serial port: ttySM1 (aka SIF1)
196 #ifdef CONFIG_MN10300_TTYSM1
197 struct mn10300_serial_port mn10300_serial_port_sif1 = {
198 .uart.ops = &mn10300_serial_ops,
199 .uart.membase = (void __iomem *) &SC1CTR,
200 .uart.mapbase = (unsigned long) &SC1CTR,
201 .uart.iotype = UPIO_MEM,
203 .uart.uartclk = 0, /* MN10300_IOCLK, */
205 .uart.flags = UPF_BOOT_AUTOCONF,
207 .uart.type = PORT_MN10300,
209 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif1.uart.lock),
213 ._status = (volatile u8 *) &SC1STR,
217 .rx_name = "ttySM1/Rx",
218 .tx_name = "ttySM1/Tx",
219 #ifdef CONFIG_MN10300_TTYSM1_TIMER9
220 .tm_name = "ttySM1/Timer9",
225 .div_timer = MNSCx_DIV_TIMER_16BIT,
226 #else /* CONFIG_MN10300_TTYSM1_TIMER3 */
227 .tm_name = "ttySM1/Timer3",
229 ._tmxbr = (volatile u16 *) &TM3BR,
232 .div_timer = MNSCx_DIV_TIMER_8BIT,
236 .rx_icr = &GxICR(SC1RXIRQ),
237 .tx_icr = &GxICR(SC1TXIRQ),
238 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
240 #ifdef CONFIG_GDBSTUB_ON_TTYSM1
244 #endif /* CONFIG_MN10300_TTYSM1 */
247 * the third on-chip serial port: ttySM2 (aka SIF2)
249 #ifdef CONFIG_MN10300_TTYSM2
250 struct mn10300_serial_port mn10300_serial_port_sif2 = {
251 .uart.ops = &mn10300_serial_ops,
252 .uart.membase = (void __iomem *) &SC2CTR,
253 .uart.mapbase = (unsigned long) &SC2CTR,
254 .uart.iotype = UPIO_MEM,
256 .uart.uartclk = 0, /* MN10300_IOCLK, */
258 .uart.flags = UPF_BOOT_AUTOCONF,
260 #ifdef CONFIG_MN10300_TTYSM2_CTS
261 .uart.type = PORT_MN10300_CTS,
263 .uart.type = PORT_MN10300,
266 __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif2.uart.lock),
268 .rx_name = "ttySM2/Rx",
269 .tx_name = "ttySM2/Tx",
270 .tm_name = "ttySM2/Timer10",
281 .div_timer = MNSCx_DIV_TIMER_16BIT,
284 .rx_icr = &GxICR(SC2RXIRQ),
285 .tx_icr = &GxICR(SC2TXIRQ),
286 .clock_src = MNSCx_CLOCK_SRC_IOCLK,
287 #ifdef CONFIG_MN10300_TTYSM2_CTS
288 .options = MNSCx_OPT_CTS,
292 #ifdef CONFIG_GDBSTUB_ON_TTYSM2
296 #endif /* CONFIG_MN10300_TTYSM2 */
300 * list of available serial ports
302 struct mn10300_serial_port *mn10300_serial_ports[NR_UARTS + 1] = {
303 #ifdef CONFIG_MN10300_TTYSM0
304 [0] = &mn10300_serial_port_sif0,
306 #ifdef CONFIG_MN10300_TTYSM1
307 [1] = &mn10300_serial_port_sif1,
309 #ifdef CONFIG_MN10300_TTYSM2
310 [2] = &mn10300_serial_port_sif2,
317 * we abuse the serial ports' baud timers' interrupt lines to get the ability
318 * to deliver interrupts to userspace as we use the ports' interrupt lines to
319 * do virtual DMA on account of the ports having no hardware FIFOs
321 * we can generate an interrupt manually in the assembly stubs by writing to
322 * the enable and detect bits in the interrupt control register, so all we need
323 * to do here is disable the interrupt line
325 * note that we can't just leave the line enabled as the baud rate timer *also*
326 * generates interrupts
328 static void mn10300_serial_mask_ack(unsigned int irq)
331 GxICR(irq) = GxICR_LEVEL_6;
332 tmp = GxICR(irq); /* flush write buffer */
335 static void mn10300_serial_nop(unsigned int irq)
339 static struct irq_chip mn10300_serial_pic = {
341 .ack = mn10300_serial_mask_ack,
342 .mask = mn10300_serial_mask_ack,
343 .mask_ack = mn10300_serial_mask_ack,
344 .unmask = mn10300_serial_nop,
345 .end = mn10300_serial_nop,
350 * serial virtual DMA interrupt jump table
352 struct mn10300_serial_int mn10300_serial_int_tbl[NR_IRQS];
354 static void mn10300_serial_dis_tx_intr(struct mn10300_serial_port *port)
357 *port->tx_icr = GxICR_LEVEL_1 | GxICR_DETECT;
361 static void mn10300_serial_en_tx_intr(struct mn10300_serial_port *port)
364 *port->tx_icr = GxICR_LEVEL_1 | GxICR_ENABLE;
368 static void mn10300_serial_dis_rx_intr(struct mn10300_serial_port *port)
371 *port->rx_icr = GxICR_LEVEL_1 | GxICR_DETECT;
376 * multi-bit equivalent of test_and_clear_bit()
378 static int mask_test_and_clear(volatile u8 *ptr, u8 mask)
381 asm volatile(" bclr %1,(%2) \n"
383 : "=d"(epsw) : "d"(mask), "a"(ptr));
384 return !(epsw & EPSW_FLAG_Z);
388 * receive chars from the ring buffer for this serial port
389 * - must do break detection here (not done in the UART)
391 static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
393 struct uart_icount *icount = &port->uart.icount;
394 struct tty_struct *tty = port->uart.info->port.tty;
397 u8 st, ch, push, status, overrun;
399 _enter("%s", port->name);
403 count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
404 count = tty_buffer_request_room(tty, count);
406 if (!tty->low_latency)
407 tty_flip_buffer_push(tty);
412 /* pull chars out of the hat */
414 if (ix == port->rx_inp) {
415 if (push && !tty->low_latency)
416 tty_flip_buffer_push(tty);
420 ch = port->rx_buffer[ix++];
421 st = port->rx_buffer[ix++];
423 port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
424 port->uart.icount.rx++;
426 st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
430 /* the UART doesn't detect BREAK, so we have to do that ourselves
431 * - it starts as a framing error on a NUL character
432 * - then we count another two NUL characters before issuing TTY_BREAK
433 * - then we end on a normal char or one that has all the bottom bits
434 * zero and the top bits set
436 switch (port->rx_brk) {
438 /* not breaking at the moment */
442 if (st & SC01STR_FEF && ch == 0) {
449 if (st & SC01STR_FEF && ch == 0) {
451 _proto("Rx Break Detected");
453 if (uart_handle_break(&port->uart))
455 status |= 1 << TTY_BREAK;
461 if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
462 goto try_again; /* still breaking */
464 port->rx_brk = 0; /* end of the break */
476 /* discard char at probable break end */
483 /* handle framing error */
484 if (st & SC01STR_FEF) {
486 /* framing error with NUL char is probably a BREAK */
491 _proto("Rx Framing Error");
493 status |= 1 << TTY_FRAME;
496 /* handle parity error */
497 if (st & SC01STR_PEF) {
498 _proto("Rx Parity Error");
503 /* handle normal char */
505 if (uart_handle_sysrq_char(&port->uart, ch))
507 status = (1 << TTY_NORMAL);
510 /* handle overrun error */
511 if (st & SC01STR_OEF) {
515 _proto("Rx Overrun Error");
521 status &= port->uart.read_status_mask;
523 if (!overrun && !(status & port->uart.ignore_status_mask)) {
526 if (status & (1 << TTY_BREAK))
528 else if (status & (1 << TTY_PARITY))
530 else if (status & (1 << TTY_FRAME))
535 tty_insert_flip_char(tty, ch, flag);
538 /* overrun is special, since it's reported immediately, and doesn't
539 * affect the current character
542 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
546 if (!tty->low_latency)
547 tty_flip_buffer_push(tty);
561 * handle an interrupt from the serial transmission "virtual DMA" driver
562 * - note: the interrupt routine will disable its own interrupts when the Tx
565 static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
567 _enter("%s", port->name);
569 if (!port->uart.info || !port->uart.info->port.tty) {
570 mn10300_serial_dis_tx_intr(port);
574 if (uart_tx_stopped(&port->uart) ||
575 uart_circ_empty(&port->uart.info->xmit))
576 mn10300_serial_dis_tx_intr(port);
578 if (uart_circ_chars_pending(&port->uart.info->xmit) < WAKEUP_CHARS)
579 uart_write_wakeup(&port->uart);
583 * deal with a change in the status of the CTS line
585 static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
590 port->uart.icount.cts++;
592 /* flip the CTS state selector flag to interrupt when it changes
594 ctr = *port->_control;
596 *port->_control = ctr;
598 uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
599 wake_up_interruptible(&port->uart.info->delta_msr_wait);
603 * handle a virtual interrupt generated by the lower level "virtual DMA"
604 * routines (irq is the baud timer interrupt)
606 static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
608 struct mn10300_serial_port *port = dev_id;
611 spin_lock(&port->uart.lock);
613 if (port->intr_flags) {
614 _debug("INT %s: %x", port->name, port->intr_flags);
616 if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
617 mn10300_serial_receive_interrupt(port);
619 if (mask_test_and_clear(&port->intr_flags,
620 MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
621 mn10300_serial_transmit_interrupt(port);
624 /* the only modem control line amongst the whole lot is CTS on
626 if (port->type == PORT_MN10300_CTS) {
628 if ((port->tx_cts ^ st) & SC2STR_CTS)
629 mn10300_serial_cts_changed(port, st);
632 spin_unlock(&port->uart.lock);
638 * return indication of whether the hardware transmit buffer is empty
640 static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
642 struct mn10300_serial_port *port =
643 container_of(_port, struct mn10300_serial_port, uart);
645 _enter("%s", port->name);
647 return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
652 * set the modem control lines (we don't have any)
654 static void mn10300_serial_set_mctrl(struct uart_port *_port,
657 struct mn10300_serial_port *port =
658 container_of(_port, struct mn10300_serial_port, uart);
660 _enter("%s,%x", port->name, mctrl);
664 * get the modem control line statuses
666 static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
668 struct mn10300_serial_port *port =
669 container_of(_port, struct mn10300_serial_port, uart);
671 _enter("%s", port->name);
673 if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
674 return TIOCM_CAR | TIOCM_DSR;
676 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
680 * stop transmitting characters
682 static void mn10300_serial_stop_tx(struct uart_port *_port)
684 struct mn10300_serial_port *port =
685 container_of(_port, struct mn10300_serial_port, uart);
687 _enter("%s", port->name);
689 /* disable the virtual DMA */
690 mn10300_serial_dis_tx_intr(port);
694 * start transmitting characters
695 * - jump-start transmission if it has stalled
696 * - enable the serial Tx interrupt (used by the virtual DMA controller)
697 * - force an interrupt to happen if necessary
699 static void mn10300_serial_start_tx(struct uart_port *_port)
701 struct mn10300_serial_port *port =
702 container_of(_port, struct mn10300_serial_port, uart);
708 CIRC_CNT(&port->uart.info->xmit.head,
709 &port->uart.info->xmit.tail,
712 /* kick the virtual DMA controller */
716 if (*port->_status & SC01STR_TBF)
717 x &= ~(GxICR_REQUEST | GxICR_DETECT);
719 x |= GxICR_REQUEST | GxICR_DETECT;
721 _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
722 *port->_control, *port->_intr, *port->_status,
723 *port->_tmxmd, *port->_tmxbr, *port->tx_icr);
730 * transmit a high-priority XON/XOFF character
732 static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
734 struct mn10300_serial_port *port =
735 container_of(_port, struct mn10300_serial_port, uart);
737 _enter("%s,%02x", port->name, ch);
739 if (likely(port->gdbstub)) {
742 mn10300_serial_en_tx_intr(port);
747 * stop receiving characters
748 * - called whilst the port is being closed
750 static void mn10300_serial_stop_rx(struct uart_port *_port)
752 struct mn10300_serial_port *port =
753 container_of(_port, struct mn10300_serial_port, uart);
757 _enter("%s", port->name);
759 ctr = *port->_control;
761 *port->_control = ctr;
763 mn10300_serial_dis_rx_intr(port);
767 * enable modem status interrupts
769 static void mn10300_serial_enable_ms(struct uart_port *_port)
771 struct mn10300_serial_port *port =
772 container_of(_port, struct mn10300_serial_port, uart);
776 _enter("%s", port->name);
778 if (port->type == PORT_MN10300_CTS) {
779 /* want to interrupt when CTS goes low if CTS is now high and
782 port->tx_cts = *port->_status;
784 cts = (port->tx_cts & SC2STR_CTS) ?
785 SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
787 ctr = *port->_control;
790 *port->_control = ctr;
792 mn10300_serial_en_tx_intr(port);
797 * transmit or cease transmitting a break signal
799 static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
801 struct mn10300_serial_port *port =
802 container_of(_port, struct mn10300_serial_port, uart);
804 _enter("%s,%d", port->name, ctl);
807 /* tell the virtual DMA handler to assert BREAK */
809 mn10300_serial_en_tx_intr(port);
812 *port->_control &= ~SC01CTR_BKE;
813 mn10300_serial_en_tx_intr(port);
818 * grab the interrupts and enable the port for reception
820 static int mn10300_serial_startup(struct uart_port *_port)
822 struct mn10300_serial_port *port =
823 container_of(_port, struct mn10300_serial_port, uart);
824 struct mn10300_serial_int *pint;
826 _enter("%s{%d}", port->name, port->gdbstub);
828 if (unlikely(port->gdbstub))
831 /* allocate an Rx buffer for the virtual DMA handler */
832 port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
833 if (!port->rx_buffer)
836 port->rx_inp = port->rx_outp = 0;
838 /* finally, enable the device */
839 *port->_intr = SC01ICR_TI;
840 *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
842 pint = &mn10300_serial_int_tbl[port->rx_irq];
844 pint->vdma = mn10300_serial_vdma_rx_handler;
845 pint = &mn10300_serial_int_tbl[port->tx_irq];
847 pint->vdma = mn10300_serial_vdma_tx_handler;
849 set_intr_level(port->rx_irq, GxICR_LEVEL_1);
850 set_intr_level(port->tx_irq, GxICR_LEVEL_1);
851 set_irq_chip(port->tm_irq, &mn10300_serial_pic);
853 if (request_irq(port->rx_irq, mn10300_serial_interrupt,
854 IRQF_DISABLED, port->rx_name, port) < 0)
857 if (request_irq(port->tx_irq, mn10300_serial_interrupt,
858 IRQF_DISABLED, port->tx_name, port) < 0)
861 if (request_irq(port->tm_irq, mn10300_serial_interrupt,
862 IRQF_DISABLED, port->tm_name, port) < 0)
864 mn10300_serial_mask_ack(port->tm_irq);
869 free_irq(port->tx_irq, port);
871 free_irq(port->rx_irq, port);
873 kfree(port->rx_buffer);
874 port->rx_buffer = NULL;
879 * shutdown the port and release interrupts
881 static void mn10300_serial_shutdown(struct uart_port *_port)
883 struct mn10300_serial_port *port =
884 container_of(_port, struct mn10300_serial_port, uart);
886 _enter("%s", port->name);
888 /* disable the serial port and its baud rate timer */
890 *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
893 if (port->rx_buffer) {
894 void *buf = port->rx_buffer;
895 port->rx_buffer = NULL;
899 /* disable all intrs */
900 free_irq(port->tm_irq, port);
901 free_irq(port->rx_irq, port);
902 free_irq(port->tx_irq, port);
904 *port->rx_icr = GxICR_LEVEL_1;
905 *port->tx_icr = GxICR_LEVEL_1;
909 * this routine is called to set the UART divisor registers to match the
910 * specified baud rate for a serial port.
912 static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
913 struct ktermios *new,
914 struct ktermios *old)
917 unsigned long ioclk = port->ioclk;
919 int baud, bits, xdiv, tmp;
922 u8 div_timer = port->div_timer;
924 _enter("%s{%lu}", port->name, ioclk);
926 /* byte size and parity */
927 cflag = new->c_cflag;
928 switch (cflag & CSIZE) {
929 case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
930 case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
931 default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
934 if (cflag & CSTOPB) {
935 scxctr |= SC01CTR_STB_2BIT;
939 if (cflag & PARENB) {
942 scxctr |= SC01CTR_PB_ODD;
944 else if (cflag & CMSPAR)
945 scxctr |= SC01CTR_PB_FIXED0;
948 scxctr |= SC01CTR_PB_EVEN;
951 /* Determine divisor based on baud rate */
954 if (div_timer == MNSCx_DIV_TIMER_16BIT)
955 scxctr |= SC0CTR_CK_TM8UFLOW_8; /* ( == SC1CTR_CK_TM9UFLOW_8
956 * == SC2CTR_CK_TM10UFLOW) */
957 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
958 scxctr |= SC0CTR_CK_TM2UFLOW_8;
961 baud = uart_get_baud_rate(&port->uart, new, old, 0,
964 _debug("ALT %d [baud %d]", battempt, baud);
967 baud = 9600; /* B0 transition handled in rs_set_termios */
970 baud = 269; /* 134 is really 134.5 */
975 (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
977 _debug("CUSTOM %u", port->uart.custom_divisor);
979 if (div_timer == MNSCx_DIV_TIMER_16BIT) {
980 if (port->uart.custom_divisor <= 65535) {
981 tmxmd = TM8MD_SRC_IOCLK;
982 tmxbr = port->uart.custom_divisor;
983 port->uart.uartclk = ioclk;
986 if (port->uart.custom_divisor / 8 <= 65535) {
987 tmxmd = TM8MD_SRC_IOCLK_8;
988 tmxbr = port->uart.custom_divisor / 8;
989 port->uart.custom_divisor = tmxbr * 8;
990 port->uart.uartclk = ioclk / 8;
993 if (port->uart.custom_divisor / 32 <= 65535) {
994 tmxmd = TM8MD_SRC_IOCLK_32;
995 tmxbr = port->uart.custom_divisor / 32;
996 port->uart.custom_divisor = tmxbr * 32;
997 port->uart.uartclk = ioclk / 32;
1001 } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
1002 if (port->uart.custom_divisor <= 255) {
1003 tmxmd = TM2MD_SRC_IOCLK;
1004 tmxbr = port->uart.custom_divisor;
1005 port->uart.uartclk = ioclk;
1008 if (port->uart.custom_divisor / 8 <= 255) {
1009 tmxmd = TM2MD_SRC_IOCLK_8;
1010 tmxbr = port->uart.custom_divisor / 8;
1011 port->uart.custom_divisor = tmxbr * 8;
1012 port->uart.uartclk = ioclk / 8;
1015 if (port->uart.custom_divisor / 32 <= 255) {
1016 tmxmd = TM2MD_SRC_IOCLK_32;
1017 tmxbr = port->uart.custom_divisor / 32;
1018 port->uart.custom_divisor = tmxbr * 32;
1019 port->uart.uartclk = ioclk / 32;
1025 switch (div_timer) {
1026 case MNSCx_DIV_TIMER_16BIT:
1027 port->uart.uartclk = ioclk;
1028 tmxmd = TM8MD_SRC_IOCLK;
1029 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1030 if (tmp > 0 && tmp <= 65535)
1033 port->uart.uartclk = ioclk / 8;
1034 tmxmd = TM8MD_SRC_IOCLK_8;
1035 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1036 if (tmp > 0 && tmp <= 65535)
1039 port->uart.uartclk = ioclk / 32;
1040 tmxmd = TM8MD_SRC_IOCLK_32;
1041 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1042 if (tmp > 0 && tmp <= 65535)
1046 case MNSCx_DIV_TIMER_8BIT:
1047 port->uart.uartclk = ioclk;
1048 tmxmd = TM2MD_SRC_IOCLK;
1049 tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
1050 if (tmp > 0 && tmp <= 255)
1053 port->uart.uartclk = ioclk / 8;
1054 tmxmd = TM2MD_SRC_IOCLK_8;
1055 tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
1056 if (tmp > 0 && tmp <= 255)
1059 port->uart.uartclk = ioclk / 32;
1060 tmxmd = TM2MD_SRC_IOCLK_32;
1061 tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
1062 if (tmp > 0 && tmp <= 255)
1071 /* refuse to change to a baud rate we can't support */
1072 _debug("CAN'T SUPPORT");
1077 new->c_cflag &= ~CBAUD;
1078 new->c_cflag |= (old->c_cflag & CBAUD);
1080 goto try_alternative;
1084 /* as a last resort, if the quotient is zero, default to 9600
1086 new->c_cflag &= ~CBAUD;
1087 new->c_cflag |= B9600;
1089 goto try_alternative;
1092 /* hmmm... can't seem to support 9600 either
1093 * - we could try iterating through the speeds we know about to
1096 new->c_cflag &= ~CBAUD;
1099 if (div_timer == MNSCx_DIV_TIMER_16BIT)
1100 tmxmd = TM8MD_SRC_IOCLK_32;
1101 else if (div_timer == MNSCx_DIV_TIMER_8BIT)
1102 tmxmd = TM2MD_SRC_IOCLK_32;
1105 port->uart.uartclk = ioclk / 32;
1110 _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
1112 /* make the changes */
1113 spin_lock_irqsave(&port->uart.lock, flags);
1115 uart_update_timeout(&port->uart, new->c_cflag, baud);
1117 /* set the timer to produce the required baud rate */
1118 switch (div_timer) {
1119 case MNSCx_DIV_TIMER_16BIT:
1121 *port->_tmxbr = tmxbr;
1122 *port->_tmxmd = TM8MD_INIT_COUNTER;
1123 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1126 case MNSCx_DIV_TIMER_8BIT:
1128 *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
1129 *port->_tmxmd = TM2MD_INIT_COUNTER;
1130 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1134 /* CTS flow control flag and modem status interrupts */
1135 scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
1137 if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
1138 /* want to interrupt when CTS goes low if CTS is now
1139 * high and vice versa
1141 port->tx_cts = *port->_status;
1143 if (port->tx_cts & SC2STR_CTS)
1144 scxctr |= SC2CTR_TWE;
1146 scxctr |= SC2CTR_TWE | SC2CTR_TWS;
1149 /* set up parity check flag */
1150 port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
1151 if (new->c_iflag & INPCK)
1152 port->uart.read_status_mask |=
1153 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1154 if (new->c_iflag & (BRKINT | PARMRK))
1155 port->uart.read_status_mask |= (1 << TTY_BREAK);
1157 /* characters to ignore */
1158 port->uart.ignore_status_mask = 0;
1159 if (new->c_iflag & IGNPAR)
1160 port->uart.ignore_status_mask |=
1161 (1 << TTY_PARITY) | (1 << TTY_FRAME);
1162 if (new->c_iflag & IGNBRK) {
1163 port->uart.ignore_status_mask |= (1 << TTY_BREAK);
1165 * If we're ignoring parity and break indicators,
1166 * ignore overruns to (for real raw support).
1168 if (new->c_iflag & IGNPAR)
1169 port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
1172 /* Ignore all characters if CREAD is not set */
1173 if ((new->c_cflag & CREAD) == 0)
1174 port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
1176 scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
1177 *port->_control = scxctr;
1179 spin_unlock_irqrestore(&port->uart.lock, flags);
1183 * set the terminal I/O parameters
1185 static void mn10300_serial_set_termios(struct uart_port *_port,
1186 struct ktermios *new,
1187 struct ktermios *old)
1189 struct mn10300_serial_port *port =
1190 container_of(_port, struct mn10300_serial_port, uart);
1192 _enter("%s,%p,%p", port->name, new, old);
1194 mn10300_serial_change_speed(port, new, old);
1196 /* handle turning off CRTSCTS */
1197 if (!(new->c_cflag & CRTSCTS)) {
1198 u16 ctr = *port->_control;
1200 *port->_control = ctr;
1205 * return description of port type
1207 static const char *mn10300_serial_type(struct uart_port *_port)
1209 struct mn10300_serial_port *port =
1210 container_of(_port, struct mn10300_serial_port, uart);
1212 if (port->uart.type == PORT_MN10300_CTS)
1213 return "MN10300 SIF_CTS";
1215 return "MN10300 SIF";
1219 * release I/O and memory regions in use by port
1221 static void mn10300_serial_release_port(struct uart_port *_port)
1223 struct mn10300_serial_port *port =
1224 container_of(_port, struct mn10300_serial_port, uart);
1226 _enter("%s", port->name);
1228 release_mem_region((unsigned long) port->_iobase, 16);
1232 * request I/O and memory regions for port
1234 static int mn10300_serial_request_port(struct uart_port *_port)
1236 struct mn10300_serial_port *port =
1237 container_of(_port, struct mn10300_serial_port, uart);
1239 _enter("%s", port->name);
1241 request_mem_region((unsigned long) port->_iobase, 16, port->name);
1246 * configure the type and reserve the ports
1248 static void mn10300_serial_config_port(struct uart_port *_port, int type)
1250 struct mn10300_serial_port *port =
1251 container_of(_port, struct mn10300_serial_port, uart);
1253 _enter("%s", port->name);
1255 port->uart.type = PORT_MN10300;
1257 if (port->options & MNSCx_OPT_CTS)
1258 port->uart.type = PORT_MN10300_CTS;
1260 mn10300_serial_request_port(_port);
1264 * verify serial parameters are suitable for this port type
1266 static int mn10300_serial_verify_port(struct uart_port *_port,
1267 struct serial_struct *ss)
1269 struct mn10300_serial_port *port =
1270 container_of(_port, struct mn10300_serial_port, uart);
1271 void *mapbase = (void *) (unsigned long) port->uart.mapbase;
1273 _enter("%s", port->name);
1275 /* these things may not be changed */
1276 if (ss->irq != port->uart.irq ||
1277 ss->port != port->uart.iobase ||
1278 ss->io_type != port->uart.iotype ||
1279 ss->iomem_base != mapbase ||
1280 ss->iomem_reg_shift != port->uart.regshift ||
1281 ss->hub6 != port->uart.hub6 ||
1282 ss->xmit_fifo_size != port->uart.fifosize)
1285 /* type may be changed on a port that supports CTS */
1286 if (ss->type != port->uart.type) {
1287 if (!(port->options & MNSCx_OPT_CTS))
1290 if (ss->type != PORT_MN10300 &&
1291 ss->type != PORT_MN10300_CTS)
1299 * initialise the MN10300 on-chip UARTs
1301 static int __init mn10300_serial_init(void)
1303 struct mn10300_serial_port *port;
1306 printk(KERN_INFO "%s version %s (%s)\n",
1307 serial_name, serial_version, serial_revdate);
1309 #ifdef CONFIG_MN10300_TTYSM2
1310 SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
1313 set_intr_stub(EXCEP_IRQ_LEVEL1, mn10300_serial_vdma_interrupt);
1315 ret = uart_register_driver(&mn10300_serial_driver);
1317 for (i = 0 ; i < NR_PORTS ; i++) {
1318 port = mn10300_serial_ports[i];
1319 if (!port || port->gdbstub)
1322 switch (port->clock_src) {
1323 case MNSCx_CLOCK_SRC_IOCLK:
1324 port->ioclk = MN10300_IOCLK;
1327 #ifdef MN10300_IOBCLK
1328 case MNSCx_CLOCK_SRC_IOBCLK:
1329 port->ioclk = MN10300_IOBCLK;
1336 ret = uart_add_one_port(&mn10300_serial_driver,
1340 _debug("ERROR %d", -ret);
1346 uart_unregister_driver(&mn10300_serial_driver);
1352 __initcall(mn10300_serial_init);
1355 #ifdef CONFIG_MN10300_TTYSM_CONSOLE
1358 * print a string to the serial port without disturbing the real user of the
1360 * - the console must be locked by the caller
1362 static void mn10300_serial_console_write(struct console *co,
1363 const char *s, unsigned count)
1365 struct mn10300_serial_port *port;
1367 u16 scxctr, txicr, tmp;
1370 port = mn10300_serial_ports[co->index];
1372 /* firstly hijack the serial port from the "virtual DMA" controller */
1373 txicr = *port->tx_icr;
1374 *port->tx_icr = GxICR_LEVEL_1;
1375 tmp = *port->tx_icr;
1377 /* the transmitter may be disabled */
1378 scxctr = *port->_control;
1379 if (!(scxctr & SC01CTR_TXE)) {
1380 /* restart the UART clock */
1381 tmxmd = *port->_tmxmd;
1383 switch (port->div_timer) {
1384 case MNSCx_DIV_TIMER_16BIT:
1386 *port->_tmxmd = TM8MD_INIT_COUNTER;
1387 *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
1390 case MNSCx_DIV_TIMER_8BIT:
1392 *port->_tmxmd = TM2MD_INIT_COUNTER;
1393 *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
1397 /* enable the transmitter */
1398 *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
1400 } else if (scxctr & SC01CTR_BKE) {
1401 /* stop transmitting BREAK */
1402 *port->_control = (scxctr & ~SC01CTR_BKE);
1405 /* send the chars into the serial port (with LF -> LFCR conversion) */
1406 for (i = 0; i < count; i++) {
1409 while (*port->_status & SC01STR_TBF)
1411 *(u8 *) port->_txb = ch;
1414 while (*port->_status & SC01STR_TBF)
1416 *(u8 *) port->_txb = 0xd;
1420 /* can't let the transmitter be turned off if it's actually
1422 while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
1425 /* disable the transmitter if we re-enabled it */
1426 if (!(scxctr & SC01CTR_TXE))
1427 *port->_control = scxctr;
1429 *port->tx_icr = txicr;
1430 tmp = *port->tx_icr;
1434 * set up a serial port as a console
1435 * - construct a cflag setting for the first rs_open()
1436 * - initialize the serial port
1437 * - return non-zero if we didn't find a serial port.
1439 static int __init mn10300_serial_console_setup(struct console *co,
1442 struct mn10300_serial_port *port;
1443 int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
1445 for (i = 0 ; i < NR_PORTS ; i++) {
1446 port = mn10300_serial_ports[i];
1447 if (port && !port->gdbstub && port->uart.line == co->index)
1454 switch (port->clock_src) {
1455 case MNSCx_CLOCK_SRC_IOCLK:
1456 port->ioclk = MN10300_IOCLK;
1459 #ifdef MN10300_IOBCLK
1460 case MNSCx_CLOCK_SRC_IOBCLK:
1461 port->ioclk = MN10300_IOBCLK;
1469 uart_parse_options(options, &baud, &parity, &bits, &flow);
1471 return uart_set_options(&port->uart, co, baud, parity, bits, flow);
1477 static int __init mn10300_serial_console_init(void)
1479 register_console(&mn10300_serial_console);
1483 console_initcall(mn10300_serial_console_init);