2 * drivers/serial/sh-sci.c
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 2002, 2003 Paul Mundt
8 * based off of the old drivers/char/sh-sci.c by:
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/sysrq.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/console.h>
44 #ifdef CONFIG_CPU_FREQ
45 #include <linux/notifier.h>
46 #include <linux/cpufreq.h>
49 #include <asm/system.h>
52 #include <asm/uaccess.h>
53 #include <asm/bitops.h>
55 #include <linux/generic_serial.h>
57 #ifdef CONFIG_SH_STANDARD_BIOS
58 #include <asm/sh_bios.h>
61 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
70 static int kgdb_get_char(struct sci_port *port);
71 static void kgdb_put_char(struct sci_port *port, char c);
72 static void kgdb_handle_error(struct sci_port *port);
73 static struct sci_port *kgdb_sci_port;
74 #endif /* CONFIG_SH_KGDB */
76 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
77 static struct sci_port *serial_console_port = 0;
78 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
80 /* Function prototypes */
81 static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop);
82 static void sci_start_tx(struct uart_port *port, unsigned int tty_start);
83 static void sci_start_rx(struct uart_port *port, unsigned int tty_start);
84 static void sci_stop_rx(struct uart_port *port);
85 static int sci_request_irq(struct sci_port *port);
86 static void sci_free_irq(struct sci_port *port);
88 static struct sci_port sci_ports[SCI_NPORTS];
89 static struct uart_driver sci_uart_driver;
91 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
93 static void handle_error(struct sci_port *port)
94 { /* Clear error flags */
95 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
98 static int get_char(struct sci_port *port)
101 unsigned short status;
104 local_irq_save(flags);
106 status = sci_in(port, SCxSR);
107 if (status & SCxSR_ERRORS(port)) {
111 } while (!(status & SCxSR_RDxF(port)));
112 c = sci_in(port, SCxRDR);
113 sci_in(port, SCxSR); /* Dummy read */
114 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
115 local_irq_restore(flags);
120 /* Taken from sh-stub.c of GDB 4.18 */
121 static const char hexchars[] = "0123456789abcdef";
123 static __inline__ char highhex(int x)
125 return hexchars[(x >> 4) & 0xf];
128 static __inline__ char lowhex(int x)
130 return hexchars[x & 0xf];
133 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
136 * Send the packet in buffer. The host gets one chance to read it.
137 * This routine does not wait for a positive acknowledge.
140 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
141 static void put_char(struct uart_port *port, char c)
144 unsigned short status;
146 local_irq_save(flags);
149 status = sci_in(port, SCxSR);
150 } while (!(status & SCxSR_TDxE(port)));
152 sci_out(port, SCxTDR, c);
153 sci_in(port, SCxSR); /* Dummy read */
154 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
156 local_irq_restore(flags);
159 static void put_string(struct sci_port *sci_port, const char *buffer, int count)
161 struct uart_port *port = &sci_port->port;
162 const unsigned char *p = buffer;
165 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
169 #ifdef CONFIG_SH_STANDARD_BIOS
170 /* This call only does a trap the first time it is
171 * called, and so is safe to do here unconditionally
173 usegdb |= sh_bios_in_gdb_mode();
175 #ifdef CONFIG_SH_KGDB
176 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
180 /* $<packet info>#<checksum>. */
184 put_char(port, 'O'); /* 'O'utput to console */
187 for (i=0; i<count; i++) { /* Don't use run length encoding */
198 put_char(port, highhex(checksum));
199 put_char(port, lowhex(checksum));
200 } while (get_char(port) != '+');
202 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
203 for (i=0; i<count; i++) {
205 put_char(port, '\r');
206 put_char(port, *p++);
209 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
212 #ifdef CONFIG_SH_KGDB
214 /* Is the SCI ready, ie is there a char waiting? */
215 static int kgdb_is_char_ready(struct sci_port *port)
217 unsigned short status = sci_in(port, SCxSR);
219 if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
220 kgdb_handle_error(port);
222 return (status & SCxSR_RDxF(port));
226 static void kgdb_put_char(struct sci_port *port, char c)
228 unsigned short status;
231 status = sci_in(port, SCxSR);
232 while (!(status & SCxSR_TDxE(port)));
234 sci_out(port, SCxTDR, c);
235 sci_in(port, SCxSR); /* Dummy read */
236 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
239 /* Get a char if there is one, else ret -1 */
240 static int kgdb_get_char(struct sci_port *port)
244 if (kgdb_is_char_ready(port) == 0)
247 c = sci_in(port, SCxRDR);
248 sci_in(port, SCxSR); /* Dummy read */
249 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
255 /* Called from kgdbstub.c to get a character, i.e. is blocking */
256 static int kgdb_sci_getchar(void)
260 /* Keep trying to read a character, this could be neater */
261 while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
266 /* Called from kgdbstub.c to put a character, just a wrapper */
267 static void kgdb_sci_putchar(int c)
270 kgdb_put_char(kgdb_sci_port, c);
273 /* Clear any errors on the SCI */
274 static void kgdb_handle_error(struct sci_port *port)
276 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
279 /* Breakpoint if there's a break sent on the serial port */
280 static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
282 struct sci_port *port = ptr;
283 unsigned short status = sci_in(port, SCxSR);
285 if (status & SCxSR_BRK(port)) {
287 /* Break into the debugger if a break is detected */
291 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
295 #endif /* CONFIG_SH_KGDB */
297 #if defined(__H8300S__)
298 enum { sci_disable, sci_enable };
300 static void h8300_sci_enable(struct uart_port* port, unsigned int ctrl)
302 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
303 int ch = (port->mapbase - SMR0) >> 3;
304 unsigned char mask = 1 << (ch+1);
306 if (ctrl == sci_disable) {
314 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
315 #if defined(__H8300H__) || defined(__H8300S__)
316 static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag)
318 int ch = (port->mapbase - SMR0) >> 3;
321 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
322 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
324 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
327 static void sci_init_pins_sci(struct uart_port *port, unsigned int cflag)
333 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
334 #if defined(CONFIG_CPU_SH3)
335 /* For SH7707, SH7709, SH7709A, SH7729 */
336 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
338 unsigned int fcr_val = 0;
343 /* We need to set SCPCR to enable RTS/CTS */
344 data = ctrl_inw(SCPCR);
345 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
346 ctrl_outw(data&0x0fcf, SCPCR);
349 fcr_val |= SCFCR_MCE;
353 /* We need to set SCPCR to enable RTS/CTS */
354 data = ctrl_inw(SCPCR);
355 /* Clear out SCP7MD1,0, SCP4MD1,0,
356 Set SCP6MD1,0 = {01} (output) */
357 ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
359 data = ctrl_inb(SCPDR);
360 /* Set /RTS2 (bit6) = 0 */
361 ctrl_outb(data&0xbf, SCPDR);
363 sci_out(port, SCFCR, fcr_val);
366 static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
368 unsigned int fcr_val = 0;
371 fcr_val |= SCFCR_MCE;
373 sci_out(port, SCFCR, fcr_val);
379 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
381 unsigned int fcr_val = 0;
383 if (cflag & CRTSCTS) {
384 fcr_val |= SCFCR_MCE;
386 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
388 sci_out(port, SCFCR, fcr_val);
392 #endif /* SCIF_ONLY || SCI_AND_SCIF */
394 /* ********************************************************************** *
395 * the interrupt related routines *
396 * ********************************************************************** */
398 static void sci_transmit_chars(struct uart_port *port)
400 struct circ_buf *xmit = &port->info->xmit;
401 unsigned int stopped = uart_tx_stopped(port);
403 unsigned short status;
407 status = sci_in(port, SCxSR);
408 if (!(status & SCxSR_TDxE(port))) {
409 local_irq_save(flags);
410 ctrl = sci_in(port, SCSCR);
411 if (uart_circ_empty(xmit)) {
412 ctrl &= ~SCI_CTRL_FLAGS_TIE;
414 ctrl |= SCI_CTRL_FLAGS_TIE;
416 sci_out(port, SCSCR, ctrl);
417 local_irq_restore(flags);
421 #if !defined(SCI_ONLY)
422 if (port->type == PORT_SCIF) {
423 txroom = 16 - (sci_in(port, SCFDR)>>8);
425 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
428 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
439 } else if (!uart_circ_empty(xmit) && !stopped) {
440 c = xmit->buf[xmit->tail];
441 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
446 sci_out(port, SCxTDR, c);
449 } while (--count > 0);
451 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
453 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
454 uart_write_wakeup(port);
455 if (uart_circ_empty(xmit)) {
456 sci_stop_tx(port, 0);
458 local_irq_save(flags);
459 ctrl = sci_in(port, SCSCR);
461 #if !defined(SCI_ONLY)
462 if (port->type == PORT_SCIF) {
463 sci_in(port, SCxSR); /* Dummy read */
464 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
468 ctrl |= SCI_CTRL_FLAGS_TIE;
469 sci_out(port, SCSCR, ctrl);
470 local_irq_restore(flags);
474 /* On SH3, SCIF may read end-of-break as a space->mark char */
475 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
477 static inline void sci_receive_chars(struct uart_port *port,
478 struct pt_regs *regs)
480 struct tty_struct *tty = port->info->tty;
481 int i, count, copied = 0;
482 unsigned short status;
484 status = sci_in(port, SCxSR);
485 if (!(status & SCxSR_RDxF(port)))
489 #if !defined(SCI_ONLY)
490 if (port->type == PORT_SCIF) {
491 count = sci_in(port, SCFDR)&0x001f;
493 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
496 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
499 /* Don't copy more bytes than there is room for in the buffer */
500 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
501 count = TTY_FLIPBUF_SIZE - tty->flip.count;
503 /* If for any reason we can't copy more data, we're done! */
507 if (port->type == PORT_SCI) {
508 char c = sci_in(port, SCxRDR);
509 if(((struct sci_port *)port)->break_flag
510 || uart_handle_sysrq_char(port, c, regs)) {
513 tty->flip.char_buf_ptr[0] = c;
514 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
517 for (i=0; i<count; i++) {
518 char c = sci_in(port, SCxRDR);
519 status = sci_in(port, SCxSR);
520 #if defined(CONFIG_CPU_SH3)
521 /* Skip "chars" during break */
522 if (((struct sci_port *)port)->break_flag) {
524 (status & SCxSR_FER(port))) {
528 /* Nonzero => end-of-break */
529 pr_debug("scif: debounce<%02x>\n", c);
530 ((struct sci_port *)port)->break_flag = 0;
536 #endif /* CONFIG_CPU_SH3 */
537 if (uart_handle_sysrq_char(port, c, regs)) {
542 /* Store data and status */
543 tty->flip.char_buf_ptr[i] = c;
544 if (status&SCxSR_FER(port)) {
545 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
546 pr_debug("sci: frame error\n");
547 } else if (status&SCxSR_PER(port)) {
548 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
549 pr_debug("sci: parity error\n");
551 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
556 sci_in(port, SCxSR); /* dummy read */
557 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
559 /* Update the kernel buffer end */
560 tty->flip.count += count;
561 tty->flip.char_buf_ptr += count;
562 tty->flip.flag_buf_ptr += count;
564 port->icount.rx += count;
568 /* Tell the rest of the system the news. New characters! */
569 tty_flip_buffer_push(tty);
571 sci_in(port, SCxSR); /* dummy read */
572 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
576 #define SCI_BREAK_JIFFIES (HZ/20)
577 /* The sci generates interrupts during the break,
578 * 1 per millisecond or so during the break period, for 9600 baud.
579 * So dont bother disabling interrupts.
580 * But dont want more than 1 break event.
581 * Use a kernel timer to periodically poll the rx line until
582 * the break is finished.
584 static void sci_schedule_break_timer(struct sci_port *port)
586 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
587 add_timer(&port->break_timer);
589 /* Ensure that two consecutive samples find the break over. */
590 static void sci_break_timer(unsigned long data)
592 struct sci_port * port = (struct sci_port *)data;
593 if(sci_rxd_in(&port->port) == 0) {
594 port->break_flag = 1;
595 sci_schedule_break_timer(port);
596 } else if(port->break_flag == 1){
598 port->break_flag = 2;
599 sci_schedule_break_timer(port);
600 } else port->break_flag = 0;
603 static inline int sci_handle_errors(struct uart_port *port)
606 unsigned short status = sci_in(port, SCxSR);
607 struct tty_struct *tty = port->info->tty;
609 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
612 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
613 pr_debug("sci: overrun error\n");
616 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
617 if (sci_rxd_in(port) == 0) {
618 /* Notify of BREAK */
619 struct sci_port * sci_port = (struct sci_port *)port;
620 if(!sci_port->break_flag) {
621 sci_port->break_flag = 1;
622 sci_schedule_break_timer((struct sci_port *)port);
623 /* Do sysrq handling. */
624 if(uart_handle_break(port)) {
627 pr_debug("sci: BREAK detected\n");
629 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
635 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
636 pr_debug("sci: frame error\n");
640 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
643 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
644 pr_debug("sci: parity error\n");
648 tty->flip.count += copied;
649 tty_flip_buffer_push(tty);
655 static inline int sci_handle_breaks(struct uart_port *port)
658 unsigned short status = sci_in(port, SCxSR);
659 struct tty_struct *tty = port->info->tty;
660 struct sci_port *s = &sci_ports[port->line];
662 if (!s->break_flag && status & SCxSR_BRK(port) &&
663 tty->flip.count < TTY_FLIPBUF_SIZE) {
664 #if defined(CONFIG_CPU_SH3)
668 /* Notify of BREAK */
670 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
671 pr_debug("sci: BREAK detected\n");
674 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1) || \
675 defined(CONFIG_CPU_SUBTYPE_SH7760)
676 /* XXX: Handle SCIF overrun error */
677 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
678 sci_out(port, SCLSR, 0);
679 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
681 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
682 pr_debug("sci: overrun error\n");
688 tty->flip.count += copied;
689 tty_flip_buffer_push(tty);
695 static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
697 struct uart_port *port = ptr;
699 /* I think sci_receive_chars has to be called irrespective
700 * of whether the I_IXOFF is set, otherwise, how is the interrupt
703 sci_receive_chars(port, regs);
708 static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
710 struct uart_port *port = ptr;
712 sci_transmit_chars(port);
717 static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
719 struct uart_port *port = ptr;
722 if (port->type == PORT_SCI) {
723 if (sci_handle_errors(port)) {
724 /* discard character in rx buffer */
726 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
729 sci_rx_interrupt(irq, ptr, regs);
732 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
734 /* Kick the transmission */
735 sci_tx_interrupt(irq, ptr, regs);
740 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
742 struct uart_port *port = ptr;
745 sci_handle_breaks(port);
746 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
751 #ifdef CONFIG_CPU_FREQ
753 * Here we define a transistion notifier so that we can update all of our
754 * ports' baud rate when the peripheral clock changes.
756 static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
758 struct cpufreq_freqs *freqs = p;
761 if (phase == CPUFREQ_POSTCHANGE) {
762 for (i = 0; i < SCI_NPORTS; i++) {
763 struct uart_port *port = &sci_ports[i];
766 * Update the uartclk per-port if frequency has
767 * changed, since it will no longer necessarily be
768 * consistent with the old frequency.
770 * Really we want to be able to do something like
771 * uart_change_speed() or something along those lines
772 * here to implicitly reset the per-port baud rate..
774 * Clean this up later..
776 port->uartclk = current_cpu_data.module_clock * 16;
779 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
780 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
786 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
787 #endif /* CONFIG_CPU_FREQ */
789 static int sci_request_irq(struct sci_port *port)
792 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
793 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
796 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
797 "SCI Transmit Data Empty", "SCI Break" };
799 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
802 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
804 printk(KERN_ERR "sci: Cannot allocate irq.\n");
812 static void sci_free_irq(struct sci_port *port)
816 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
820 free_irq(port->irqs[i], port);
824 static unsigned int sci_tx_empty(struct uart_port *port)
830 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
832 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
833 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
834 /* If you have signals for DTR and DCD, please implement here. */
837 static unsigned int sci_get_mctrl(struct uart_port *port)
839 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
842 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
845 static void sci_start_tx(struct uart_port *port, unsigned int tty_start)
847 struct sci_port *s = &sci_ports[port->line];
849 disable_irq(s->irqs[SCIx_TXI_IRQ]);
850 sci_transmit_chars(port);
851 enable_irq(s->irqs[SCIx_TXI_IRQ]);
854 static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop)
859 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
860 local_irq_save(flags);
861 ctrl = sci_in(port, SCSCR);
862 ctrl &= ~SCI_CTRL_FLAGS_TIE;
863 sci_out(port, SCSCR, ctrl);
864 local_irq_restore(flags);
867 static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
872 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
873 local_irq_save(flags);
874 ctrl = sci_in(port, SCSCR);
875 ctrl |= SCI_CTRL_FLAGS_RIE;
876 sci_out(port, SCSCR, ctrl);
877 local_irq_restore(flags);
880 static void sci_stop_rx(struct uart_port *port)
885 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
886 local_irq_save(flags);
887 ctrl = sci_in(port, SCSCR);
888 ctrl &= ~SCI_CTRL_FLAGS_RIE;
889 sci_out(port, SCSCR, ctrl);
890 local_irq_restore(flags);
893 static void sci_enable_ms(struct uart_port *port)
895 /* Nothing here yet .. */
898 static void sci_break_ctl(struct uart_port *port, int break_state)
900 /* Nothing here yet .. */
903 static int sci_startup(struct uart_port *port)
905 struct sci_port *s = &sci_ports[port->line];
908 sci_start_tx(port, 1);
909 sci_start_rx(port, 1);
911 #if defined(__H8300S__)
912 h8300_sci_enable(port, sci_enable);
918 static void sci_shutdown(struct uart_port *port)
920 struct sci_port *s = &sci_ports[port->line];
923 sci_stop_tx(port, 1);
926 #if defined(__H8300S__)
927 h8300_sci_enable(port, sci_disable);
931 static void sci_set_termios(struct uart_port *port, struct termios *termios,
934 struct sci_port *s = &sci_ports[port->line];
935 unsigned int status, baud, smr_val;
939 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
941 spin_lock_irqsave(&port->lock, flags);
944 status = sci_in(port, SCxSR);
945 } while (!(status & SCxSR_TEND(port)));
947 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
949 #if !defined(SCI_ONLY)
950 if (port->type == PORT_SCIF) {
951 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
955 smr_val = sci_in(port, SCSMR) & 3;
956 if ((termios->c_cflag & CSIZE) == CS7)
958 if (termios->c_cflag & PARENB)
960 if (termios->c_cflag & PARODD)
962 if (termios->c_cflag & CSTOPB)
965 uart_update_timeout(port, termios->c_cflag, baud);
967 sci_out(port, SCSMR, smr_val);
970 case 0: t = -1; break;
971 case 2400: t = BPS_2400; break;
972 case 4800: t = BPS_4800; break;
973 case 9600: t = BPS_9600; break;
974 case 19200: t = BPS_19200; break;
975 case 38400: t = BPS_38400; break;
976 case 57600: t = BPS_57600; break;
977 case 115200: t = BPS_115200; break;
978 default: t = BPS_115200; break;
983 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
986 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
988 sci_out(port, SCBRR, t);
989 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
992 s->init_pins(port, termios->c_cflag);
993 sci_out(port, SCSCR, SCSCR_INIT(port));
995 if ((termios->c_cflag & CREAD) != 0)
996 sci_start_rx(port,0);
998 spin_unlock_irqrestore(&port->lock, flags);
1001 static const char *sci_type(struct uart_port *port)
1003 switch (port->type) {
1004 case PORT_SCI: return "sci";
1005 case PORT_SCIF: return "scif";
1006 case PORT_IRDA: return "irda";
1012 static void sci_release_port(struct uart_port *port)
1014 /* Nothing here yet .. */
1017 static int sci_request_port(struct uart_port *port)
1019 /* Nothing here yet .. */
1023 static void sci_config_port(struct uart_port *port, int flags)
1025 struct sci_port *s = &sci_ports[port->line];
1027 port->type = s->type;
1030 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1032 struct sci_port *s = &sci_ports[port->line];
1034 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1036 if (ser->baud_base < 2400)
1037 /* No paper tape reader for Mitch.. */
1043 static struct uart_ops sci_uart_ops = {
1044 .tx_empty = sci_tx_empty,
1045 .set_mctrl = sci_set_mctrl,
1046 .get_mctrl = sci_get_mctrl,
1047 .start_tx = sci_start_tx,
1048 .stop_tx = sci_stop_tx,
1049 .stop_rx = sci_stop_rx,
1050 .enable_ms = sci_enable_ms,
1051 .break_ctl = sci_break_ctl,
1052 .startup = sci_startup,
1053 .shutdown = sci_shutdown,
1054 .set_termios = sci_set_termios,
1056 .release_port = sci_release_port,
1057 .request_port = sci_request_port,
1058 .config_port = sci_config_port,
1059 .verify_port = sci_verify_port,
1062 static struct sci_port sci_ports[SCI_NPORTS] = {
1063 #if defined(CONFIG_CPU_SUBTYPE_SH7708)
1066 .membase = (void *)0xfffffe80,
1067 .mapbase = 0xfffffe80,
1068 .iotype = SERIAL_IO_MEM,
1070 .ops = &sci_uart_ops,
1071 .flags = ASYNC_BOOT_AUTOCONF,
1076 .init_pins = sci_init_pins_sci,
1078 #elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
1081 .membase = (void *)0xfffffe80,
1082 .mapbase = 0xfffffe80,
1083 .iotype = SERIAL_IO_MEM,
1085 .ops = &sci_uart_ops,
1086 .flags = ASYNC_BOOT_AUTOCONF,
1091 .init_pins = sci_init_pins_sci,
1095 .membase = (void *)0xa4000150,
1096 .mapbase = 0xa4000150,
1097 .iotype = SERIAL_IO_MEM,
1099 .ops = &sci_uart_ops,
1100 .flags = ASYNC_BOOT_AUTOCONF,
1104 .irqs = SH3_SCIF_IRQS,
1105 .init_pins = sci_init_pins_scif,
1109 .membase = (void *)0xa4000140,
1110 .mapbase = 0xa4000140,
1111 .iotype = SERIAL_IO_MEM,
1113 .ops = &sci_uart_ops,
1114 .flags = ASYNC_BOOT_AUTOCONF,
1118 .irqs = SH3_IRDA_IRQS,
1119 .init_pins = sci_init_pins_irda,
1121 #elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
1124 .membase = (void *)0xffe00000,
1125 .mapbase = 0xffe00000,
1126 .iotype = SERIAL_IO_MEM,
1128 .ops = &sci_uart_ops,
1129 .flags = ASYNC_BOOT_AUTOCONF,
1134 .init_pins = sci_init_pins_sci,
1138 .membase = (void *)0xffe80000,
1139 .mapbase = 0xffe80000,
1140 .iotype = SERIAL_IO_MEM,
1142 .ops = &sci_uart_ops,
1143 .flags = ASYNC_BOOT_AUTOCONF,
1147 .irqs = SH4_SCIF_IRQS,
1148 .init_pins = sci_init_pins_scif,
1150 #elif defined(CONFIG_CPU_SUBTYPE_SH7760)
1153 .membase = (void *)0xfe600000,
1154 .mapbase = 0xfe600000,
1155 .iotype = SERIAL_IO_MEM,
1157 .ops = &sci_uart_ops,
1158 .flags = ASYNC_BOOT_AUTOCONF,
1162 .irqs = SH7760_SCIF0_IRQS,
1163 .init_pins = sci_init_pins_scif,
1167 .membase = (void *)0xfe610000,
1168 .mapbase = 0xfe610000,
1169 .iotype = SERIAL_IO_MEM,
1171 .ops = &sci_uart_ops,
1172 .flags = ASYNC_BOOT_AUTOCONF,
1176 .irqs = SH7760_SCIF1_IRQS,
1177 .init_pins = sci_init_pins_scif,
1181 .membase = (void *)0xfe620000,
1182 .mapbase = 0xfe620000,
1183 .iotype = SERIAL_IO_MEM,
1185 .ops = &sci_uart_ops,
1186 .flags = ASYNC_BOOT_AUTOCONF,
1190 .irqs = SH7760_SCIF2_IRQS,
1191 .init_pins = sci_init_pins_scif,
1193 #elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
1196 .membase = (void *)0xffe00000,
1197 .mapbase = 0xffe00000,
1198 .iotype = SERIAL_IO_MEM,
1200 .ops = &sci_uart_ops,
1201 .flags = ASYNC_BOOT_AUTOCONF,
1205 .irqs = STB1_SCIF1_IRQS,
1206 .init_pins = sci_init_pins_scif,
1210 .membase = (void *)0xffe80000,
1211 .mapbase = 0xffe80000,
1212 .iotype = SERIAL_IO_MEM,
1214 .ops = &sci_uart_ops,
1215 .flags = ASYNC_BOOT_AUTOCONF,
1219 .irqs = SH4_SCIF_IRQS,
1220 .init_pins = sci_init_pins_scif,
1222 #elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
1225 .membase = (void *)0x00ffffb0,
1226 .mapbase = 0x00ffffb0,
1227 .iotype = SERIAL_IO_MEM,
1229 .ops = &sci_uart_ops,
1230 .flags = ASYNC_BOOT_AUTOCONF,
1234 .irqs = H8300H_SCI_IRQS0,
1235 .init_pins = sci_init_pins_sci,
1239 .membase = (void *)0x00ffffb8,
1240 .mapbase = 0x00ffffb8,
1241 .iotype = SERIAL_IO_MEM,
1243 .ops = &sci_uart_ops,
1244 .flags = ASYNC_BOOT_AUTOCONF,
1248 .irqs = H8300H_SCI_IRQS1,
1249 .init_pins = sci_init_pins_sci,
1253 .membase = (void *)0x00ffffc0,
1254 .mapbase = 0x00ffffc0,
1255 .iotype = SERIAL_IO_MEM,
1257 .ops = &sci_uart_ops,
1258 .flags = ASYNC_BOOT_AUTOCONF,
1262 .irqs = H8300H_SCI_IRQS2,
1263 .init_pins = sci_init_pins_sci,
1265 #elif defined(CONFIG_H8S2678)
1268 .membase = (void *)0x00ffff78,
1269 .mapbase = 0x00ffff78,
1270 .iotype = SERIAL_IO_MEM,
1272 .ops = &sci_uart_ops,
1273 .flags = ASYNC_BOOT_AUTOCONF,
1277 .irqs = H8S_SCI_IRQS0,
1278 .init_pins = sci_init_pins_sci,
1282 .membase = (void *)0x00ffff80,
1283 .mapbase = 0x00ffff80,
1284 .iotype = SERIAL_IO_MEM,
1286 .ops = &sci_uart_ops,
1287 .flags = ASYNC_BOOT_AUTOCONF,
1291 .irqs = H8S_SCI_IRQS1,
1292 .init_pins = sci_init_pins_sci,
1296 .membase = (void *)0x00ffff88,
1297 .mapbase = 0x00ffff88,
1298 .iotype = SERIAL_IO_MEM,
1300 .ops = &sci_uart_ops,
1301 .flags = ASYNC_BOOT_AUTOCONF,
1305 .irqs = H8S_SCI_IRQS2,
1306 .init_pins = sci_init_pins_sci,
1309 #error "CPU subtype not defined"
1313 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1315 * Print a string to the serial port trying not to disturb
1316 * any possible real use of the port...
1318 static void serial_console_write(struct console *co, const char *s,
1321 put_string(serial_console_port, s, count);
1324 static int __init serial_console_setup(struct console *co, char *options)
1326 struct uart_port *port;
1332 if (co->index >= SCI_NPORTS)
1335 serial_console_port = &sci_ports[co->index];
1336 port = &serial_console_port->port;
1337 port->type = serial_console_port->type;
1340 * We need to set the initial uartclk here, since otherwise it will
1341 * only ever be setup at sci_init() time.
1343 #if !defined(__H8300H__) && !defined(__H8300S__)
1344 port->uartclk = current_cpu_data.module_clock * 16;
1346 port->uartclk = CONFIG_CPU_CLOCK;
1349 uart_parse_options(options, &baud, &parity, &bits, &flow);
1351 return uart_set_options(port, co, baud, parity, bits, flow);
1354 static struct console serial_console = {
1356 .device = uart_console_device,
1357 .write = serial_console_write,
1358 .setup = serial_console_setup,
1359 .flags = CON_PRINTBUFFER,
1361 .data = &sci_uart_driver,
1364 static int __init sci_console_init(void)
1366 #ifdef CONFIG_SH_EARLY_PRINTK
1367 extern void sh_console_unregister(void);
1370 * Now that the real console is available, unregister the one we
1371 * used while first booting.
1373 sh_console_unregister();
1376 register_console(&serial_console);
1381 console_initcall(sci_console_init);
1382 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1384 #ifdef CONFIG_SH_KGDB
1386 * FIXME: Most of this can go away.. at the moment, we rely on
1387 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1388 * most of that can easily be done here instead.
1390 * For the time being, just accept the values that were parsed earlier..
1392 static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1393 int *parity, int *bits)
1396 *parity = tolower(kgdb_parity);
1397 *bits = kgdb_bits - '0';
1401 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1402 * care of the early-on initialization for kgdb, regardless of whether we
1403 * actually use kgdb as a console or not.
1405 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1407 int __init kgdb_console_setup(struct console *co, char *options)
1409 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1415 if (co->index >= SCI_NPORTS || co->index != kgdb_portnum)
1416 co->index = kgdb_portnum;
1419 uart_parse_options(options, &baud, &parity, &bits, &flow);
1421 kgdb_console_get_options(port, &baud, &parity, &bits);
1423 kgdb_getchar = kgdb_sci_getchar;
1424 kgdb_putchar = kgdb_sci_putchar;
1426 return uart_set_options(port, co, baud, parity, bits, flow);
1428 #endif /* CONFIG_SH_KGDB */
1430 #ifdef CONFIG_SH_KGDB_CONSOLE
1431 static struct console kgdb_console = {
1433 .write = kgdb_console_write,
1434 .setup = kgdb_console_setup,
1435 .flags = CON_PRINTBUFFER | CON_ENABLED,
1437 .data = &sci_uart_driver,
1440 /* Register the KGDB console so we get messages (d'oh!) */
1441 static int __init kgdb_console_init(void)
1443 register_console(&kgdb_console);
1448 console_initcall(kgdb_console_init);
1449 #endif /* CONFIG_SH_KGDB_CONSOLE */
1451 #if defined(CONFIG_SH_KGDB_CONSOLE)
1452 #define SCI_CONSOLE &kgdb_console
1453 #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1454 #define SCI_CONSOLE &serial_console
1456 #define SCI_CONSOLE 0
1459 static char banner[] __initdata =
1460 KERN_INFO "SuperH SCI(F) driver initialized\n";
1462 static struct uart_driver sci_uart_driver = {
1463 .owner = THIS_MODULE,
1464 .driver_name = "sci",
1465 #ifdef CONFIG_DEVFS_FS
1466 .devfs_name = "ttsc/",
1468 .dev_name = "ttySC",
1470 .minor = SCI_MINOR_START,
1472 .cons = SCI_CONSOLE,
1475 static int __init sci_init(void)
1479 printk("%s", banner);
1481 ret = uart_register_driver(&sci_uart_driver);
1483 for (chan = 0; chan < SCI_NPORTS; chan++) {
1484 struct sci_port *sciport = &sci_ports[chan];
1486 #if !defined(__H8300H__) && !defined(__H8300S__)
1487 sciport->port.uartclk = (current_cpu_data.module_clock * 16);
1489 sciport->port.uartclk = CONFIG_CPU_CLOCK;
1491 uart_add_one_port(&sci_uart_driver, &sciport->port);
1492 sciport->break_timer.data = (unsigned long)sciport;
1493 sciport->break_timer.function = sci_break_timer;
1494 init_timer(&sciport->break_timer);
1498 #ifdef CONFIG_CPU_FREQ
1499 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1500 printk("sci: CPU frequency notifier registered\n");
1503 #ifdef CONFIG_SH_STANDARD_BIOS
1504 sh_bios_gdb_detach();
1510 static void __exit sci_exit(void)
1514 for (chan = 0; chan < SCI_NPORTS; chan++)
1515 uart_remove_one_port(&sci_uart_driver, &sci_ports[chan].port);
1517 uart_unregister_driver(&sci_uart_driver);
1520 module_init(sci_init);
1521 module_exit(sci_exit);