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 uart_port *port)
94 { /* Clear error flags */
95 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
98 static int get_char(struct uart_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, SH7300*/
336 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
338 unsigned int fcr_val = 0;
339 #if !defined(CONFIG_CPU_SUBTYPE_SH7300) /* SH7300 doesn't use RTS/CTS */
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);
364 sci_out(port, SCFCR, fcr_val);
367 static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag)
369 unsigned int fcr_val = 0;
372 fcr_val |= SCFCR_MCE;
374 sci_out(port, SCFCR, fcr_val);
380 static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag)
382 unsigned int fcr_val = 0;
384 if (cflag & CRTSCTS) {
385 fcr_val |= SCFCR_MCE;
387 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
389 sci_out(port, SCFCR, fcr_val);
393 #endif /* SCIF_ONLY || SCI_AND_SCIF */
395 /* ********************************************************************** *
396 * the interrupt related routines *
397 * ********************************************************************** */
399 static void sci_transmit_chars(struct uart_port *port)
401 struct circ_buf *xmit = &port->info->xmit;
402 unsigned int stopped = uart_tx_stopped(port);
404 unsigned short status;
408 status = sci_in(port, SCxSR);
409 if (!(status & SCxSR_TDxE(port))) {
410 local_irq_save(flags);
411 ctrl = sci_in(port, SCSCR);
412 if (uart_circ_empty(xmit)) {
413 ctrl &= ~SCI_CTRL_FLAGS_TIE;
415 ctrl |= SCI_CTRL_FLAGS_TIE;
417 sci_out(port, SCSCR, ctrl);
418 local_irq_restore(flags);
422 #if !defined(SCI_ONLY)
423 if (port->type == PORT_SCIF) {
424 txroom = SCIF_TXROOM_MAX - (sci_in(port, SCFDR)>>8);
426 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
429 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
440 } else if (!uart_circ_empty(xmit) && !stopped) {
441 c = xmit->buf[xmit->tail];
442 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
447 sci_out(port, SCxTDR, c);
450 } while (--count > 0);
452 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
454 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
455 uart_write_wakeup(port);
456 if (uart_circ_empty(xmit)) {
457 sci_stop_tx(port, 0);
459 local_irq_save(flags);
460 ctrl = sci_in(port, SCSCR);
462 #if !defined(SCI_ONLY)
463 if (port->type == PORT_SCIF) {
464 sci_in(port, SCxSR); /* Dummy read */
465 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
469 ctrl |= SCI_CTRL_FLAGS_TIE;
470 sci_out(port, SCSCR, ctrl);
471 local_irq_restore(flags);
475 /* On SH3, SCIF may read end-of-break as a space->mark char */
476 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
478 static inline void sci_receive_chars(struct uart_port *port,
479 struct pt_regs *regs)
481 struct tty_struct *tty = port->info->tty;
482 int i, count, copied = 0;
483 unsigned short status;
485 status = sci_in(port, SCxSR);
486 if (!(status & SCxSR_RDxF(port)))
490 #if !defined(SCI_ONLY)
491 if (port->type == PORT_SCIF) {
492 count = sci_in(port, SCFDR)&SCIF_RFDC_MASK ;
494 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
497 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
500 /* Don't copy more bytes than there is room for in the buffer */
501 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
502 count = TTY_FLIPBUF_SIZE - tty->flip.count;
504 /* If for any reason we can't copy more data, we're done! */
508 if (port->type == PORT_SCI) {
509 char c = sci_in(port, SCxRDR);
510 if(((struct sci_port *)port)->break_flag
511 || uart_handle_sysrq_char(port, c, regs)) {
514 tty->flip.char_buf_ptr[0] = c;
515 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
518 for (i=0; i<count; i++) {
519 char c = sci_in(port, SCxRDR);
520 status = sci_in(port, SCxSR);
521 #if defined(CONFIG_CPU_SH3)
522 /* Skip "chars" during break */
523 if (((struct sci_port *)port)->break_flag) {
525 (status & SCxSR_FER(port))) {
529 /* Nonzero => end-of-break */
530 pr_debug("scif: debounce<%02x>\n", c);
531 ((struct sci_port *)port)->break_flag = 0;
537 #endif /* CONFIG_CPU_SH3 */
538 if (uart_handle_sysrq_char(port, c, regs)) {
543 /* Store data and status */
544 tty->flip.char_buf_ptr[i] = c;
545 if (status&SCxSR_FER(port)) {
546 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
547 pr_debug("sci: frame error\n");
548 } else if (status&SCxSR_PER(port)) {
549 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
550 pr_debug("sci: parity error\n");
552 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
557 sci_in(port, SCxSR); /* dummy read */
558 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
560 /* Update the kernel buffer end */
561 tty->flip.count += count;
562 tty->flip.char_buf_ptr += count;
563 tty->flip.flag_buf_ptr += count;
565 port->icount.rx += count;
569 /* Tell the rest of the system the news. New characters! */
570 tty_flip_buffer_push(tty);
572 sci_in(port, SCxSR); /* dummy read */
573 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
577 #define SCI_BREAK_JIFFIES (HZ/20)
578 /* The sci generates interrupts during the break,
579 * 1 per millisecond or so during the break period, for 9600 baud.
580 * So dont bother disabling interrupts.
581 * But dont want more than 1 break event.
582 * Use a kernel timer to periodically poll the rx line until
583 * the break is finished.
585 static void sci_schedule_break_timer(struct sci_port *port)
587 port->break_timer.expires = jiffies + SCI_BREAK_JIFFIES;
588 add_timer(&port->break_timer);
590 /* Ensure that two consecutive samples find the break over. */
591 static void sci_break_timer(unsigned long data)
593 struct sci_port * port = (struct sci_port *)data;
594 if(sci_rxd_in(&port->port) == 0) {
595 port->break_flag = 1;
596 sci_schedule_break_timer(port);
597 } else if(port->break_flag == 1){
599 port->break_flag = 2;
600 sci_schedule_break_timer(port);
601 } else port->break_flag = 0;
604 static inline int sci_handle_errors(struct uart_port *port)
607 unsigned short status = sci_in(port, SCxSR);
608 struct tty_struct *tty = port->info->tty;
610 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
613 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
614 pr_debug("sci: overrun error\n");
617 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
618 if (sci_rxd_in(port) == 0) {
619 /* Notify of BREAK */
620 struct sci_port * sci_port = (struct sci_port *)port;
621 if(!sci_port->break_flag) {
622 sci_port->break_flag = 1;
623 sci_schedule_break_timer((struct sci_port *)port);
624 /* Do sysrq handling. */
625 if(uart_handle_break(port)) {
628 pr_debug("sci: BREAK detected\n");
630 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
636 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
637 pr_debug("sci: frame error\n");
641 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
644 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
645 pr_debug("sci: parity error\n");
649 tty->flip.count += copied;
650 tty_flip_buffer_push(tty);
656 static inline int sci_handle_breaks(struct uart_port *port)
659 unsigned short status = sci_in(port, SCxSR);
660 struct tty_struct *tty = port->info->tty;
661 struct sci_port *s = &sci_ports[port->line];
663 if (!s->break_flag && status & SCxSR_BRK(port) &&
664 tty->flip.count < TTY_FLIPBUF_SIZE) {
665 #if defined(CONFIG_CPU_SH3)
669 /* Notify of BREAK */
671 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
672 pr_debug("sci: BREAK detected\n");
675 #if defined(SCIF_ORER)
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 #if defined(SCIF_ORER)
730 if((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
731 struct tty_struct *tty = port->info->tty;
733 sci_out(port, SCLSR, 0);
734 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
735 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
737 tty_flip_buffer_push(tty);
738 pr_debug("scif: overrun error\n");
742 sci_rx_interrupt(irq, ptr, regs);
745 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
747 /* Kick the transmission */
748 sci_tx_interrupt(irq, ptr, regs);
753 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
755 struct uart_port *port = ptr;
758 sci_handle_breaks(port);
759 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
764 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
766 unsigned short ssr_status, scr_status;
767 struct uart_port *port = ptr;
769 ssr_status = sci_in(port,SCxSR);
770 scr_status = sci_in(port,SCSCR);
773 if ((ssr_status&0x0020) && (scr_status&0x0080))
774 sci_tx_interrupt(irq, ptr, regs);
776 if ((ssr_status&0x0002) && (scr_status&0x0040))
777 sci_rx_interrupt(irq, ptr, regs);
778 /* Error Interrupt */
779 if ((ssr_status&0x0080) && (scr_status&0x0400))
780 sci_er_interrupt(irq, ptr, regs);
781 /* Break Interrupt */
782 if ((ssr_status&0x0010) && (scr_status&0x0200))
783 sci_br_interrupt(irq, ptr, regs);
788 #ifdef CONFIG_CPU_FREQ
790 * Here we define a transistion notifier so that we can update all of our
791 * ports' baud rate when the peripheral clock changes.
793 static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
795 struct cpufreq_freqs *freqs = p;
798 if ((phase == CPUFREQ_POSTCHANGE) ||
799 (phase == CPUFREQ_RESUMECHANGE)){
800 for (i = 0; i < SCI_NPORTS; i++) {
801 struct uart_port *port = &sci_ports[i];
804 * Update the uartclk per-port if frequency has
805 * changed, since it will no longer necessarily be
806 * consistent with the old frequency.
808 * Really we want to be able to do something like
809 * uart_change_speed() or something along those lines
810 * here to implicitly reset the per-port baud rate..
812 * Clean this up later..
814 port->uartclk = current_cpu_data.module_clock * 16;
817 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
818 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
824 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
825 #endif /* CONFIG_CPU_FREQ */
827 static int sci_request_irq(struct sci_port *port)
830 irqreturn_t (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
831 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
834 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
835 "SCI Transmit Data Empty", "SCI Break" };
837 if (port->irqs[0] == port->irqs[1]) {
838 if (!port->irqs[0]) {
839 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
842 if (request_irq(port->irqs[0], sci_mpxed_interrupt, SA_INTERRUPT,
844 printk(KERN_ERR "sci: Cannot allocate irq.\n");
848 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
851 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
853 printk(KERN_ERR "sci: Cannot allocate irq.\n");
862 static void sci_free_irq(struct sci_port *port)
866 if (port->irqs[0] == port->irqs[1]) {
868 printk("sci: sci_free_irq error\n");
870 free_irq(port->irqs[0], port);
872 for (i = 0; i < ARRAY_SIZE(port->irqs); i++) {
876 free_irq(port->irqs[i], port);
881 static unsigned int sci_tx_empty(struct uart_port *port)
887 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
889 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
890 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
891 /* If you have signals for DTR and DCD, please implement here. */
894 static unsigned int sci_get_mctrl(struct uart_port *port)
896 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
899 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
902 static void sci_start_tx(struct uart_port *port, unsigned int tty_start)
904 struct sci_port *s = &sci_ports[port->line];
906 disable_irq(s->irqs[SCIx_TXI_IRQ]);
907 sci_transmit_chars(port);
908 enable_irq(s->irqs[SCIx_TXI_IRQ]);
911 static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop)
916 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
917 local_irq_save(flags);
918 ctrl = sci_in(port, SCSCR);
919 ctrl &= ~SCI_CTRL_FLAGS_TIE;
920 sci_out(port, SCSCR, ctrl);
921 local_irq_restore(flags);
924 static void sci_start_rx(struct uart_port *port, unsigned int tty_start)
929 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
930 local_irq_save(flags);
931 ctrl = sci_in(port, SCSCR);
932 ctrl |= SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE;
933 sci_out(port, SCSCR, ctrl);
934 local_irq_restore(flags);
937 static void sci_stop_rx(struct uart_port *port)
942 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
943 local_irq_save(flags);
944 ctrl = sci_in(port, SCSCR);
945 ctrl &= ~(SCI_CTRL_FLAGS_RIE | SCI_CTRL_FLAGS_REIE);
946 sci_out(port, SCSCR, ctrl);
947 local_irq_restore(flags);
950 static void sci_enable_ms(struct uart_port *port)
952 /* Nothing here yet .. */
955 static void sci_break_ctl(struct uart_port *port, int break_state)
957 /* Nothing here yet .. */
960 static int sci_startup(struct uart_port *port)
962 struct sci_port *s = &sci_ports[port->line];
964 #if defined(__H8300S__)
965 h8300_sci_enable(port, sci_enable);
969 sci_start_tx(port, 1);
970 sci_start_rx(port, 1);
975 static void sci_shutdown(struct uart_port *port)
977 struct sci_port *s = &sci_ports[port->line];
980 sci_stop_tx(port, 1);
983 #if defined(__H8300S__)
984 h8300_sci_enable(port, sci_disable);
988 static void sci_set_termios(struct uart_port *port, struct termios *termios,
991 struct sci_port *s = &sci_ports[port->line];
992 unsigned int status, baud, smr_val;
996 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
998 spin_lock_irqsave(&port->lock, flags);
1001 status = sci_in(port, SCxSR);
1002 } while (!(status & SCxSR_TEND(port)));
1004 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1006 #if !defined(SCI_ONLY)
1007 if (port->type == PORT_SCIF) {
1008 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1012 smr_val = sci_in(port, SCSMR) & 3;
1013 if ((termios->c_cflag & CSIZE) == CS7)
1015 if (termios->c_cflag & PARENB)
1017 if (termios->c_cflag & PARODD)
1019 if (termios->c_cflag & CSTOPB)
1022 uart_update_timeout(port, termios->c_cflag, baud);
1024 sci_out(port, SCSMR, smr_val);
1027 case 0: t = -1; break;
1028 case 2400: t = BPS_2400; break;
1029 case 4800: t = BPS_4800; break;
1030 case 9600: t = BPS_9600; break;
1031 case 19200: t = BPS_19200; break;
1032 case 38400: t = BPS_38400; break;
1033 case 57600: t = BPS_57600; break;
1034 case 115200: t = BPS_115200; break;
1035 default: t = SCBRR_VALUE(baud); break;
1040 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1043 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1045 sci_out(port, SCBRR, t);
1046 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1049 s->init_pins(port, termios->c_cflag);
1050 sci_out(port, SCSCR, SCSCR_INIT(port));
1052 if ((termios->c_cflag & CREAD) != 0)
1053 sci_start_rx(port,0);
1055 spin_unlock_irqrestore(&port->lock, flags);
1058 static const char *sci_type(struct uart_port *port)
1060 switch (port->type) {
1061 case PORT_SCI: return "sci";
1062 case PORT_SCIF: return "scif";
1063 case PORT_IRDA: return "irda";
1069 static void sci_release_port(struct uart_port *port)
1071 /* Nothing here yet .. */
1074 static int sci_request_port(struct uart_port *port)
1076 /* Nothing here yet .. */
1080 static void sci_config_port(struct uart_port *port, int flags)
1082 struct sci_port *s = &sci_ports[port->line];
1084 port->type = s->type;
1086 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1087 if (port->mapbase == 0)
1088 port->mapbase = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1090 port->membase = (void *)port->mapbase;
1094 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1096 struct sci_port *s = &sci_ports[port->line];
1098 if (ser->irq != s->irqs[SCIx_TXI_IRQ] || ser->irq > NR_IRQS)
1100 if (ser->baud_base < 2400)
1101 /* No paper tape reader for Mitch.. */
1107 static struct uart_ops sci_uart_ops = {
1108 .tx_empty = sci_tx_empty,
1109 .set_mctrl = sci_set_mctrl,
1110 .get_mctrl = sci_get_mctrl,
1111 .start_tx = sci_start_tx,
1112 .stop_tx = sci_stop_tx,
1113 .stop_rx = sci_stop_rx,
1114 .enable_ms = sci_enable_ms,
1115 .break_ctl = sci_break_ctl,
1116 .startup = sci_startup,
1117 .shutdown = sci_shutdown,
1118 .set_termios = sci_set_termios,
1120 .release_port = sci_release_port,
1121 .request_port = sci_request_port,
1122 .config_port = sci_config_port,
1123 .verify_port = sci_verify_port,
1126 static struct sci_port sci_ports[SCI_NPORTS] = {
1127 #if defined(CONFIG_CPU_SUBTYPE_SH7708)
1130 .membase = (void *)0xfffffe80,
1131 .mapbase = 0xfffffe80,
1132 .iotype = SERIAL_IO_MEM,
1134 .ops = &sci_uart_ops,
1135 .flags = ASYNC_BOOT_AUTOCONF,
1140 .init_pins = sci_init_pins_sci,
1142 #elif defined(CONFIG_CPU_SUBTYPE_SH7707) || defined(CONFIG_CPU_SUBTYPE_SH7709)
1145 .membase = (void *)0xfffffe80,
1146 .mapbase = 0xfffffe80,
1147 .iotype = SERIAL_IO_MEM,
1149 .ops = &sci_uart_ops,
1150 .flags = ASYNC_BOOT_AUTOCONF,
1155 .init_pins = sci_init_pins_sci,
1159 .membase = (void *)0xa4000150,
1160 .mapbase = 0xa4000150,
1161 .iotype = SERIAL_IO_MEM,
1163 .ops = &sci_uart_ops,
1164 .flags = ASYNC_BOOT_AUTOCONF,
1168 .irqs = SH3_SCIF_IRQS,
1169 .init_pins = sci_init_pins_scif,
1173 .membase = (void *)0xa4000140,
1174 .mapbase = 0xa4000140,
1175 .iotype = SERIAL_IO_MEM,
1177 .ops = &sci_uart_ops,
1178 .flags = ASYNC_BOOT_AUTOCONF,
1182 .irqs = SH3_IRDA_IRQS,
1183 .init_pins = sci_init_pins_irda,
1185 #elif defined(CONFIG_CPU_SUBTYPE_SH7300)
1188 .membase = (void *)0xA4430000,
1189 .mapbase = 0xA4430000,
1190 .iotype = SERIAL_IO_MEM,
1192 .ops = &sci_uart_ops,
1193 .flags = ASYNC_BOOT_AUTOCONF,
1197 .irqs = SH7300_SCIF0_IRQS,
1198 .init_pins = sci_init_pins_scif,
1200 #elif defined(CONFIG_SH_RTS7751R2D)
1203 .membase = (void *)0xffe80000,
1204 .mapbase = 0xffe80000,
1205 .iotype = SERIAL_IO_MEM,
1207 .ops = &sci_uart_ops,
1208 .flags = ASYNC_BOOT_AUTOCONF,
1212 .irqs = SH4_SCIF_IRQS,
1213 .init_pins = sci_init_pins_scif,
1215 #elif defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_SH7751)
1218 .membase = (void *)0xffe00000,
1219 .mapbase = 0xffe00000,
1220 .iotype = SERIAL_IO_MEM,
1222 .ops = &sci_uart_ops,
1223 .flags = ASYNC_BOOT_AUTOCONF,
1228 .init_pins = sci_init_pins_sci,
1232 .membase = (void *)0xffe80000,
1233 .mapbase = 0xffe80000,
1234 .iotype = SERIAL_IO_MEM,
1236 .ops = &sci_uart_ops,
1237 .flags = ASYNC_BOOT_AUTOCONF,
1241 .irqs = SH4_SCIF_IRQS,
1242 .init_pins = sci_init_pins_scif,
1244 #elif defined(CONFIG_CPU_SUBTYPE_SH7760)
1247 .membase = (void *)0xfe600000,
1248 .mapbase = 0xfe600000,
1249 .iotype = SERIAL_IO_MEM,
1251 .ops = &sci_uart_ops,
1252 .flags = ASYNC_BOOT_AUTOCONF,
1256 .irqs = SH7760_SCIF0_IRQS,
1257 .init_pins = sci_init_pins_scif,
1261 .membase = (void *)0xfe610000,
1262 .mapbase = 0xfe610000,
1263 .iotype = SERIAL_IO_MEM,
1265 .ops = &sci_uart_ops,
1266 .flags = ASYNC_BOOT_AUTOCONF,
1270 .irqs = SH7760_SCIF1_IRQS,
1271 .init_pins = sci_init_pins_scif,
1275 .membase = (void *)0xfe620000,
1276 .mapbase = 0xfe620000,
1277 .iotype = SERIAL_IO_MEM,
1279 .ops = &sci_uart_ops,
1280 .flags = ASYNC_BOOT_AUTOCONF,
1284 .irqs = SH7760_SCIF2_IRQS,
1285 .init_pins = sci_init_pins_scif,
1287 #elif defined(CONFIG_CPU_SUBTYPE_ST40STB1)
1290 .membase = (void *)0xffe00000,
1291 .mapbase = 0xffe00000,
1292 .iotype = SERIAL_IO_MEM,
1294 .ops = &sci_uart_ops,
1295 .flags = ASYNC_BOOT_AUTOCONF,
1299 .irqs = STB1_SCIF1_IRQS,
1300 .init_pins = sci_init_pins_scif,
1304 .membase = (void *)0xffe80000,
1305 .mapbase = 0xffe80000,
1306 .iotype = SERIAL_IO_MEM,
1308 .ops = &sci_uart_ops,
1309 .flags = ASYNC_BOOT_AUTOCONF,
1313 .irqs = SH4_SCIF_IRQS,
1314 .init_pins = sci_init_pins_scif,
1316 #elif defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1319 .iotype = SERIAL_IO_MEM,
1321 .ops = &sci_uart_ops,
1322 .flags = ASYNC_BOOT_AUTOCONF,
1326 .irqs = SH5_SCIF_IRQS,
1327 .init_pins = sci_init_pins_scif,
1329 #elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
1332 .membase = (void *)0x00ffffb0,
1333 .mapbase = 0x00ffffb0,
1334 .iotype = SERIAL_IO_MEM,
1336 .ops = &sci_uart_ops,
1337 .flags = ASYNC_BOOT_AUTOCONF,
1341 .irqs = H8300H_SCI_IRQS0,
1342 .init_pins = sci_init_pins_sci,
1346 .membase = (void *)0x00ffffb8,
1347 .mapbase = 0x00ffffb8,
1348 .iotype = SERIAL_IO_MEM,
1350 .ops = &sci_uart_ops,
1351 .flags = ASYNC_BOOT_AUTOCONF,
1355 .irqs = H8300H_SCI_IRQS1,
1356 .init_pins = sci_init_pins_sci,
1360 .membase = (void *)0x00ffffc0,
1361 .mapbase = 0x00ffffc0,
1362 .iotype = SERIAL_IO_MEM,
1364 .ops = &sci_uart_ops,
1365 .flags = ASYNC_BOOT_AUTOCONF,
1369 .irqs = H8300H_SCI_IRQS2,
1370 .init_pins = sci_init_pins_sci,
1372 #elif defined(CONFIG_H8S2678)
1375 .membase = (void *)0x00ffff78,
1376 .mapbase = 0x00ffff78,
1377 .iotype = SERIAL_IO_MEM,
1379 .ops = &sci_uart_ops,
1380 .flags = ASYNC_BOOT_AUTOCONF,
1384 .irqs = H8S_SCI_IRQS0,
1385 .init_pins = sci_init_pins_sci,
1389 .membase = (void *)0x00ffff80,
1390 .mapbase = 0x00ffff80,
1391 .iotype = SERIAL_IO_MEM,
1393 .ops = &sci_uart_ops,
1394 .flags = ASYNC_BOOT_AUTOCONF,
1398 .irqs = H8S_SCI_IRQS1,
1399 .init_pins = sci_init_pins_sci,
1403 .membase = (void *)0x00ffff88,
1404 .mapbase = 0x00ffff88,
1405 .iotype = SERIAL_IO_MEM,
1407 .ops = &sci_uart_ops,
1408 .flags = ASYNC_BOOT_AUTOCONF,
1412 .irqs = H8S_SCI_IRQS2,
1413 .init_pins = sci_init_pins_sci,
1416 #error "CPU subtype not defined"
1420 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1422 * Print a string to the serial port trying not to disturb
1423 * any possible real use of the port...
1425 static void serial_console_write(struct console *co, const char *s,
1428 put_string(serial_console_port, s, count);
1431 static int __init serial_console_setup(struct console *co, char *options)
1433 struct uart_port *port;
1440 if (co->index >= SCI_NPORTS)
1443 serial_console_port = &sci_ports[co->index];
1444 port = &serial_console_port->port;
1445 port->type = serial_console_port->type;
1447 #ifdef CONFIG_SUPERH64
1448 /* This is especially needed on sh64 to remap the SCIF */
1449 sci_config_port(port, 0);
1453 * We need to set the initial uartclk here, since otherwise it will
1454 * only ever be setup at sci_init() time.
1456 #if !defined(__H8300H__) && !defined(__H8300S__)
1457 port->uartclk = current_cpu_data.module_clock * 16;
1459 port->uartclk = CONFIG_CPU_CLOCK;
1461 #if defined(__H8300S__)
1462 h8300_sci_enable(port, sci_enable);
1465 uart_parse_options(options, &baud, &parity, &bits, &flow);
1467 ret = uart_set_options(port, co, baud, parity, bits, flow);
1468 #if defined(__H8300H__) || defined(__H8300S__)
1469 /* disable rx interrupt */
1476 static struct console serial_console = {
1478 .device = uart_console_device,
1479 .write = serial_console_write,
1480 .setup = serial_console_setup,
1481 .flags = CON_PRINTBUFFER,
1483 .data = &sci_uart_driver,
1486 static int __init sci_console_init(void)
1488 register_console(&serial_console);
1492 console_initcall(sci_console_init);
1493 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1495 #ifdef CONFIG_SH_KGDB
1497 * FIXME: Most of this can go away.. at the moment, we rely on
1498 * arch/sh/kernel/setup.c to do the command line parsing for kgdb, though
1499 * most of that can easily be done here instead.
1501 * For the time being, just accept the values that were parsed earlier..
1503 static void __init kgdb_console_get_options(struct uart_port *port, int *baud,
1504 int *parity, int *bits)
1507 *parity = tolower(kgdb_parity);
1508 *bits = kgdb_bits - '0';
1512 * The naming here is somewhat misleading, since kgdb_console_setup() takes
1513 * care of the early-on initialization for kgdb, regardless of whether we
1514 * actually use kgdb as a console or not.
1516 * On the plus side, this lets us kill off the old kgdb_sci_setup() nonsense.
1518 int __init kgdb_console_setup(struct console *co, char *options)
1520 struct uart_port *port = &sci_ports[kgdb_portnum].port;
1526 if (co->index >= SCI_NPORTS || co->index != kgdb_portnum)
1527 co->index = kgdb_portnum;
1530 uart_parse_options(options, &baud, &parity, &bits, &flow);
1532 kgdb_console_get_options(port, &baud, &parity, &bits);
1534 kgdb_getchar = kgdb_sci_getchar;
1535 kgdb_putchar = kgdb_sci_putchar;
1537 return uart_set_options(port, co, baud, parity, bits, flow);
1539 #endif /* CONFIG_SH_KGDB */
1541 #ifdef CONFIG_SH_KGDB_CONSOLE
1542 static struct console kgdb_console = {
1544 .write = kgdb_console_write,
1545 .setup = kgdb_console_setup,
1546 .flags = CON_PRINTBUFFER | CON_ENABLED,
1548 .data = &sci_uart_driver,
1551 /* Register the KGDB console so we get messages (d'oh!) */
1552 static int __init kgdb_console_init(void)
1554 register_console(&kgdb_console);
1558 console_initcall(kgdb_console_init);
1559 #endif /* CONFIG_SH_KGDB_CONSOLE */
1561 #if defined(CONFIG_SH_KGDB_CONSOLE)
1562 #define SCI_CONSOLE &kgdb_console
1563 #elif defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1564 #define SCI_CONSOLE &serial_console
1566 #define SCI_CONSOLE 0
1569 static char banner[] __initdata =
1570 KERN_INFO "SuperH SCI(F) driver initialized\n";
1572 static struct uart_driver sci_uart_driver = {
1573 .owner = THIS_MODULE,
1574 .driver_name = "sci",
1575 #ifdef CONFIG_DEVFS_FS
1576 .devfs_name = "ttsc/",
1578 .dev_name = "ttySC",
1580 .minor = SCI_MINOR_START,
1582 .cons = SCI_CONSOLE,
1585 static int __init sci_init(void)
1589 printk("%s", banner);
1591 ret = uart_register_driver(&sci_uart_driver);
1593 for (chan = 0; chan < SCI_NPORTS; chan++) {
1594 struct sci_port *sciport = &sci_ports[chan];
1596 #if !defined(__H8300H__) && !defined(__H8300S__)
1597 sciport->port.uartclk = (current_cpu_data.module_clock * 16);
1599 sciport->port.uartclk = CONFIG_CPU_CLOCK;
1601 uart_add_one_port(&sci_uart_driver, &sciport->port);
1602 sciport->break_timer.data = (unsigned long)sciport;
1603 sciport->break_timer.function = sci_break_timer;
1604 init_timer(&sciport->break_timer);
1608 #ifdef CONFIG_CPU_FREQ
1609 cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER);
1610 printk("sci: CPU frequency notifier registered\n");
1613 #ifdef CONFIG_SH_STANDARD_BIOS
1614 sh_bios_gdb_detach();
1620 static void __exit sci_exit(void)
1624 for (chan = 0; chan < SCI_NPORTS; chan++)
1625 uart_remove_one_port(&sci_uart_driver, &sci_ports[chan].port);
1627 uart_unregister_driver(&sci_uart_driver);
1630 module_init(sci_init);
1631 module_exit(sci_exit);