1 /* $Id: sh-sci.c,v 1.16 2004/02/10 17:04:17 lethal Exp $
3 * linux/drivers/char/sh-sci.c
5 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 1999, 2000 Niibe Yutaka
7 * Copyright (C) 2000 Sugioka Toshinobu
8 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
9 * Modified to support SH7760 SCIF. Paul Mundt (Oct 2003).
10 * Modified to support H8/300 Series. Yoshinori Sato (Feb 2004).
12 * TTY code is based on sx.c (Specialix SX driver) by:
14 * (C) 1998 R.E.Wolff@BitWizard.nl
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SH_KGDB_CONSOLE)
38 #include <linux/console.h>
40 #ifdef CONFIG_CPU_FREQ
41 #include <linux/notifier.h>
42 #include <linux/cpufreq.h>
45 #include <asm/system.h>
48 #include <asm/uaccess.h>
49 #include <asm/bitops.h>
51 #include <linux/generic_serial.h>
53 #ifdef CONFIG_SH_STANDARD_BIOS
54 #include <asm/sh_bios.h>
62 int kgdb_sci_setup(void);
63 static int kgdb_get_char(struct sci_port *port);
64 static void kgdb_put_char(struct sci_port *port, char c);
65 static void kgdb_handle_error(struct sci_port *port);
66 static struct sci_port *kgdb_sci_port;
68 #ifdef CONFIG_SH_KGDB_CONSOLE
69 static struct console kgdbcons;
70 void __init kgdb_console_init(void);
71 #endif /* CONFIG_SH_KGDB_CONSOLE */
73 #endif /* CONFIG_SH_KGDB */
75 #ifdef CONFIG_SERIAL_CONSOLE
76 static struct console sercons;
77 static struct sci_port* sercons_port=0;
78 static int sercons_baud;
79 #ifdef CONFIG_MAGIC_SYSRQ
80 #include <linux/sysrq.h>
81 static int break_pressed;
82 #endif /* CONFIG_MAGIC_SYSRQ */
83 #endif /* CONFIG_SERIAL_CONSOLE */
85 /* Function prototypes */
86 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag);
88 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag);
89 #if defined(CONFIG_CPU_SH3)
90 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag);
93 static void sci_disable_tx_interrupts(void *ptr);
94 static void sci_enable_tx_interrupts(void *ptr);
95 static void sci_disable_rx_interrupts(void *ptr);
96 static void sci_enable_rx_interrupts(void *ptr);
97 static int sci_get_CD(void *ptr);
98 static void sci_shutdown_port(void *ptr);
99 static int sci_set_real_termios(void *ptr);
100 static void sci_hungup(void *ptr);
101 static void sci_close(void *ptr);
102 static int sci_chars_in_buffer(void *ptr);
103 static int sci_request_irq(struct sci_port *port);
104 static void sci_free_irq(struct sci_port *port);
105 static int sci_init_drivers(void);
107 static struct tty_driver *sci_driver;
109 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
111 static int sci_debug = 0;
114 MODULE_PARM(sci_debug, "i");
117 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
119 #ifdef CONFIG_SERIAL_CONSOLE
120 static void put_char(struct sci_port *port, char c)
123 unsigned short status;
125 local_irq_save(flags);
128 status = sci_in(port, SCxSR);
129 while (!(status & SCxSR_TDxE(port)));
131 sci_out(port, SCxTDR, c);
132 sci_in(port, SCxSR); /* Dummy read */
133 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
135 local_irq_restore(flags);
139 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
141 static void handle_error(struct sci_port *port)
142 { /* Clear error flags */
143 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
146 static int get_char(struct sci_port *port)
149 unsigned short status;
152 local_irq_save(flags);
154 status = sci_in(port, SCxSR);
155 if (status & SCxSR_ERRORS(port)) {
159 } while (!(status & SCxSR_RDxF(port)));
160 c = sci_in(port, SCxRDR);
161 sci_in(port, SCxSR); /* Dummy read */
162 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
163 local_irq_restore(flags);
168 /* Taken from sh-stub.c of GDB 4.18 */
169 static const char hexchars[] = "0123456789abcdef";
171 static __inline__ char highhex(int x)
173 return hexchars[(x >> 4) & 0xf];
176 static __inline__ char lowhex(int x)
178 return hexchars[x & 0xf];
181 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
184 * Send the packet in buffer. The host gets one chance to read it.
185 * This routine does not wait for a positive acknowledge.
188 #ifdef CONFIG_SERIAL_CONSOLE
189 static void put_string(struct sci_port *port, const char *buffer, int count)
192 const unsigned char *p = buffer;
194 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
198 #ifdef CONFIG_SH_STANDARD_BIOS
199 /* This call only does a trap the first time it is
200 * called, and so is safe to do here unconditionally
202 usegdb |= sh_bios_in_gdb_mode();
204 #ifdef CONFIG_SH_KGDB
205 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
209 /* $<packet info>#<checksum>. */
213 put_char(port, 'O'); /* 'O'utput to console */
216 for (i=0; i<count; i++) { /* Don't use run length encoding */
227 put_char(port, highhex(checksum));
228 put_char(port, lowhex(checksum));
229 } while (get_char(port) != '+');
231 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
232 for (i=0; i<count; i++) {
234 put_char(port, '\r');
235 put_char(port, *p++);
238 #endif /* CONFIG_SERIAL_CONSOLE */
241 #ifdef CONFIG_SH_KGDB
243 /* Is the SCI ready, ie is there a char waiting? */
244 static int kgdb_is_char_ready(struct sci_port *port)
246 unsigned short status = sci_in(port, SCxSR);
248 if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
249 kgdb_handle_error(port);
251 return (status & SCxSR_RDxF(port));
255 static void kgdb_put_char(struct sci_port *port, char c)
257 unsigned short status;
260 status = sci_in(port, SCxSR);
261 while (!(status & SCxSR_TDxE(port)));
263 sci_out(port, SCxTDR, c);
264 sci_in(port, SCxSR); /* Dummy read */
265 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
268 /* Get a char if there is one, else ret -1 */
269 static int kgdb_get_char(struct sci_port *port)
273 if (kgdb_is_char_ready(port) == 0)
276 c = sci_in(port, SCxRDR);
277 sci_in(port, SCxSR); /* Dummy read */
278 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
284 /* Called from kgdbstub.c to get a character, i.e. is blocking */
285 static int kgdb_sci_getchar(void)
289 /* Keep trying to read a character, this could be neater */
290 while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
295 /* Called from kgdbstub.c to put a character, just a wrapper */
296 static void kgdb_sci_putchar(int c)
299 kgdb_put_char(kgdb_sci_port, c);
302 /* Clear any errors on the SCI */
303 static void kgdb_handle_error(struct sci_port *port)
305 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
308 /* Breakpoint if there's a break sent on the serial port */
309 static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
311 struct sci_port *port = ptr;
312 unsigned short status = sci_in(port, SCxSR);
314 if (status & SCxSR_BRK(port)) {
316 /* Break into the debugger if a break is detected */
320 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
325 #endif /* CONFIG_SH_KGDB */
327 static struct real_driver sci_real_driver = {
328 sci_disable_tx_interrupts,
329 sci_enable_tx_interrupts,
330 sci_disable_rx_interrupts,
331 sci_enable_rx_interrupts,
334 sci_set_real_termios,
341 #if !defined(__H8300H__) && !defined(__H8300S__)
342 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
343 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
348 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
349 #if defined(CONFIG_CPU_SH3)
350 /* For SH7707, SH7709, SH7709A, SH7729 */
351 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
353 unsigned int fcr_val = 0;
358 /* We need to set SCPCR to enable RTS/CTS */
359 data = ctrl_inw(SCPCR);
360 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
361 ctrl_outw(data&0x0cff, SCPCR);
364 fcr_val |= SCFCR_MCE;
368 /* We need to set SCPCR to enable RTS/CTS */
369 data = ctrl_inw(SCPCR);
370 /* Clear out SCP7MD1,0, SCP4MD1,0,
371 Set SCP6MD1,0 = {01} (output) */
372 ctrl_outw((data&0x0cff)|0x1000, SCPCR);
374 data = ctrl_inb(SCPDR);
375 /* Set /RTS2 (bit6) = 0 */
376 ctrl_outb(data&0xbf, SCPDR);
378 sci_out(port, SCFCR, fcr_val);
381 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
383 unsigned int fcr_val = 0;
386 fcr_val |= SCFCR_MCE;
388 sci_out(port, SCFCR, fcr_val);
394 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
396 unsigned int fcr_val = 0;
398 if (cflag & CRTSCTS) {
399 fcr_val |= SCFCR_MCE;
401 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
403 sci_out(port, SCFCR, fcr_val);
407 #endif /* SCIF_ONLY || SCI_AND_SCIF */
408 #else /* !defined(__H8300H__) && !defined(__H8300S__) */
409 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
411 int ch = (port->base - SMR0) >> 3;
413 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
414 H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
416 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
419 #if defined(__H8300S__)
420 enum {sci_disable,sci_enable};
422 static void h8300_sci_enable(struct sci_port* port, unsigned int ctrl)
424 volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
425 int ch = (port->base - SMR0) >> 3;
426 unsigned char mask = 1 << (ch+1);
427 if (ctrl == sci_disable)
435 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
437 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
438 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
439 /* If you have signals for DTR and DCD, please implement here. */
443 static int sci_getsignals(struct sci_port *port)
445 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
448 return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
450 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
451 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
452 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
453 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
454 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
455 ((i_stat & IP_RI) ?TIOCM_RNG:0)
459 static void sci_set_baud(struct sci_port *port, int baud)
486 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
493 sci_setsignals (port, 1, -1);
495 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
498 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
500 sci_out(port, SCBRR, t);
501 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
503 sci_setsignals (port, 0, -1);
507 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
510 unsigned int smr_val;
513 status = sci_in(port, SCxSR);
514 while (!(status & SCxSR_TEND(port)));
516 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
518 #if !defined(SCI_ONLY)
519 if (port->type == PORT_SCIF) {
520 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
524 smr_val = sci_in(port, SCSMR) & 3;
525 if ((cflag & CSIZE) == CS7)
533 sci_out(port, SCSMR, smr_val);
534 sci_set_baud(port, baud);
536 port->init_pins(port, cflag);
537 sci_out(port, SCSCR, SCSCR_INIT(port));
540 static int sci_set_real_termios(void *ptr)
542 struct sci_port *port = ptr;
544 if (port->old_cflag != port->gs.tty->termios->c_cflag) {
545 port->old_cflag = port->gs.tty->termios->c_cflag;
546 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
547 sci_enable_rx_interrupts(port);
553 /* ********************************************************************** *
554 * the interrupt related routines *
555 * ********************************************************************** */
558 * This routine is used by the interrupt handler to schedule
559 * processing in the software interrupt portion of the driver.
561 static inline void sci_sched_event(struct sci_port *port, int event)
563 port->event |= 1 << event;
564 schedule_work(&port->tqueue);
567 static void sci_transmit_chars(struct sci_port *port)
572 unsigned short status;
576 status = sci_in(port, SCxSR);
577 if (!(status & SCxSR_TDxE(port))) {
578 local_irq_save(flags);
579 ctrl = sci_in(port, SCSCR);
580 if (port->gs.xmit_cnt == 0) {
581 ctrl &= ~SCI_CTRL_FLAGS_TIE;
582 port->gs.flags &= ~GS_TX_INTEN;
584 ctrl |= SCI_CTRL_FLAGS_TIE;
585 sci_out(port, SCSCR, ctrl);
586 local_irq_restore(flags);
591 count = port->gs.xmit_cnt;
592 #if !defined(SCI_ONLY)
593 if (port->type == PORT_SCIF) {
594 txroom = 16 - (sci_in(port, SCFDR)>>8);
596 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
599 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
604 /* Don't copy past the end of the source buffer */
605 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
606 count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
608 /* If for one reason or another, we can't copy more data, we're done! */
612 for (i=0; i<count; i++) {
613 c = port->gs.xmit_buf[port->gs.xmit_tail + i];
614 sci_out(port, SCxTDR, c);
616 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
618 port->icount.tx += count;
620 /* Update the kernel buffer end */
621 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
623 /* This one last. (this is essential)
624 It would allow others to start putting more data into the buffer! */
625 port->gs.xmit_cnt -= count;
628 if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
629 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
631 local_irq_save(flags);
632 ctrl = sci_in(port, SCSCR);
633 if (port->gs.xmit_cnt == 0) {
634 ctrl &= ~SCI_CTRL_FLAGS_TIE;
635 port->gs.flags &= ~GS_TX_INTEN;
637 #if !defined(SCI_ONLY)
638 if (port->type == PORT_SCIF) {
639 sci_in(port, SCxSR); /* Dummy read */
640 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
643 ctrl |= SCI_CTRL_FLAGS_TIE;
645 sci_out(port, SCSCR, ctrl);
646 local_irq_restore(flags);
649 /* On SH3, SCIF may read end-of-break as a space->mark char */
650 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
652 static inline void sci_receive_chars(struct sci_port *port,
653 struct pt_regs *regs)
656 struct tty_struct *tty;
658 unsigned short status;
660 status = sci_in(port, SCxSR);
661 if (!(status & SCxSR_RDxF(port)))
666 #if !defined(SCI_ONLY)
667 if (port->type == PORT_SCIF) {
668 count = sci_in(port, SCFDR)&0x001f;
670 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
673 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
676 /* Don't copy more bytes than there is room for in the buffer */
677 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
678 count = TTY_FLIPBUF_SIZE - tty->flip.count;
680 /* If for any reason we can't copy more data, we're done! */
684 if (port->type == PORT_SCI) {
685 tty->flip.char_buf_ptr[0] = sci_in(port, SCxRDR);
686 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
688 for (i=0; i<count; i++) {
689 char c = sci_in(port, SCxRDR);
690 status = sci_in(port, SCxSR);
692 /* Skip "chars" during break */
693 if (port->break_flag) {
695 (status & SCxSR_FER(port))) {
699 /* Nonzero => end-of-break */
700 dprintk("scif: debounce<%02x>\n", c);
701 port->break_flag = 0;
708 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
709 if (break_pressed && (port == sercons_port)) {
712 break_pressed + HZ*5)) {
713 handle_sysrq(c, regs, NULL);
721 #endif /* CONFIG_SERIAL_CONSOLE && CONFIG_MAGIC_SYSRQ */
723 /* Store data and status */
724 tty->flip.char_buf_ptr[i] = c;
725 if (status&SCxSR_FER(port)) {
726 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
727 dprintk("sci: frame error\n");
728 } else if (status&SCxSR_PER(port)) {
729 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
730 dprintk("sci: parity error\n");
732 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
737 sci_in(port, SCxSR); /* dummy read */
738 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
740 /* Update the kernel buffer end */
741 tty->flip.count += count;
742 tty->flip.char_buf_ptr += count;
743 tty->flip.flag_buf_ptr += count;
746 port->icount.rx += count;
750 /* Tell the rest of the system the news. New characters! */
751 tty_flip_buffer_push(tty);
753 sci_in(port, SCxSR); /* dummy read */
754 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
758 static inline int sci_handle_errors(struct sci_port *port)
761 unsigned short status = sci_in(port, SCxSR);
762 struct tty_struct *tty = port->gs.tty;
764 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
767 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
768 dprintk("sci: overrun error\n");
771 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
772 if (sci_rxd_in(port) == 0) {
773 /* Notify of BREAK */
775 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
776 dprintk("sci: BREAK detected\n");
781 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
782 dprintk("sci: frame error\n");
786 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
789 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
790 dprintk("sci: parity error\n");
794 tty->flip.count += copied;
795 tty_flip_buffer_push(tty);
801 static inline int sci_handle_breaks(struct sci_port *port)
804 unsigned short status = sci_in(port, SCxSR);
805 struct tty_struct *tty = port->gs.tty;
807 if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
810 if (port->break_flag)
812 port->break_flag = 1;
814 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
815 if (port == sercons_port) {
816 if (break_pressed == 0) {
817 break_pressed = jiffies;
818 dprintk("sci: implied sysrq\n");
821 /* Double break implies a real break */
825 /* Notify of BREAK */
827 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
828 dprintk("sci: BREAK detected\n");
832 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1) || \
833 defined(CONFIG_CPU_SUBTYPE_SH7760)
834 /* XXX: Handle SCIF overrun error */
835 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
836 sci_out(port, SCLSR, 0);
837 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
839 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
840 dprintk("sci: overrun error\n");
846 tty->flip.count += copied;
847 tty_flip_buffer_push(tty);
853 static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
855 struct sci_port *port = ptr;
857 if (port->gs.flags & GS_ACTIVE)
858 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
859 sci_receive_chars(port, regs);
863 sci_disable_rx_interrupts(port);
868 static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
870 struct sci_port *port = ptr;
872 if (port->gs.flags & GS_ACTIVE)
873 sci_transmit_chars(port);
875 sci_disable_tx_interrupts(port);
881 static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
883 struct sci_port *port = ptr;
886 if (port->type == PORT_SCI) {
887 if(sci_handle_errors(port)) {
888 /* discard character in rx buffer */
890 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
894 sci_rx_interrupt(irq, ptr, regs);
896 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
898 /* Kick the transmission */
899 sci_tx_interrupt(irq, ptr, regs);
904 #if !defined(SCI_ONLY)
905 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
907 struct sci_port *port = ptr;
910 sci_handle_breaks(port);
911 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
917 static void do_softint(void *private_)
919 struct sci_port *port = (struct sci_port *) private_;
920 struct tty_struct *tty;
926 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
927 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
928 tty->ldisc.write_wakeup)
929 (tty->ldisc.write_wakeup)(tty);
930 wake_up_interruptible(&tty->write_wait);
934 /* ********************************************************************** *
935 * Here are the routines that actually *
936 * interface with the generic_serial driver *
937 * ********************************************************************** */
939 static void sci_disable_tx_interrupts(void *ptr)
941 struct sci_port *port = ptr;
945 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
946 local_irq_save(flags);
947 ctrl = sci_in(port, SCSCR);
948 ctrl &= ~SCI_CTRL_FLAGS_TIE;
949 sci_out(port, SCSCR, ctrl);
950 local_irq_restore(flags);
953 static void sci_enable_tx_interrupts(void *ptr)
955 struct sci_port *port = ptr;
957 disable_irq(port->irqs[SCIx_TXI_IRQ]);
958 sci_transmit_chars(port);
959 enable_irq(port->irqs[SCIx_TXI_IRQ]);
962 static void sci_disable_rx_interrupts(void * ptr)
964 struct sci_port *port = ptr;
968 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
969 local_irq_save(flags);
970 ctrl = sci_in(port, SCSCR);
971 ctrl &= ~SCI_CTRL_FLAGS_RIE;
972 sci_out(port, SCSCR, ctrl);
973 local_irq_restore(flags);
976 static void sci_enable_rx_interrupts(void * ptr)
978 struct sci_port *port = ptr;
982 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
983 local_irq_save(flags);
984 ctrl = sci_in(port, SCSCR);
985 ctrl |= SCI_CTRL_FLAGS_RIE;
986 sci_out(port, SCSCR, ctrl);
987 local_irq_restore(flags);
990 static int sci_get_CD(void * ptr)
992 /* If you have signal for CD (Carrier Detect), please change here. */
996 static int sci_chars_in_buffer(void * ptr)
998 struct sci_port *port = ptr;
1000 #if !defined(SCI_ONLY)
1001 if (port->type == PORT_SCIF) {
1002 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
1004 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
1007 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
1011 static void sci_shutdown_port(void * ptr)
1013 struct sci_port *port = ptr;
1015 port->gs.flags &= ~ GS_ACTIVE;
1016 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
1017 sci_setsignals(port, 0, 0);
1019 #if defined(__H8300S__)
1020 h8300_sci_enable(port,sci_disable);
1024 /* ********************************************************************** *
1025 * Here are the routines that actually *
1026 * interface with the rest of the system *
1027 * ********************************************************************** */
1029 static int sci_open(struct tty_struct * tty, struct file * filp)
1031 struct sci_port *port;
1036 if ((line < 0) || (line >= SCI_NPORTS))
1039 port = &sci_ports[line];
1041 tty->driver_data = port;
1046 INIT_WORK(&port->tqueue, do_softint, port);
1048 #if defined(__H8300S__)
1049 h8300_sci_enable(port,sci_enable);
1053 * Start up serial port
1055 retval = gs_init_port(&port->gs);
1060 port->gs.flags |= GS_ACTIVE;
1061 sci_setsignals(port, 1,1);
1063 if (port->gs.count == 1) {
1064 retval = sci_request_irq(port);
1067 retval = gs_block_til_ready(port, filp);
1073 #ifdef CONFIG_SERIAL_CONSOLE
1074 if (sercons.cflag && sercons.index == line) {
1075 tty->termios->c_cflag = sercons.cflag;
1076 port->gs.baud = sercons_baud;
1078 sci_set_real_termios(port);
1082 #ifdef CONFIG_SH_KGDB_CONSOLE
1083 if (kgdbcons.cflag && kgdbcons.index == line) {
1084 tty->termios->c_cflag = kgdbcons.cflag;
1085 port->gs.baud = kgdb_baud;
1087 sci_set_real_termios(port);
1091 sci_enable_rx_interrupts(port);
1102 static void sci_hungup(void *ptr)
1107 static void sci_close(void *ptr)
1112 static int sci_tiocmget(struct tty_struct *tty, struct file *file)
1114 struct sci_port *port = tty->driver_data;
1115 return sci_getsignals(port);
1118 static int sci_tiocmset(struct tty_struct *tty, struct file *file,
1119 unsigned int set, unsigned int clear)
1121 struct sci_port *port = tty->driver_data;
1122 int rts = -1, dtr = -1;
1124 if (set & TIOCM_RTS)
1126 if (set & TIOCM_DTR)
1128 if (clear & TIOCM_RTS)
1130 if (clear & TIOCM_DTR)
1133 sci_setsignals(port, dtr, rts);
1137 static int sci_ioctl(struct tty_struct * tty, struct file * filp,
1138 unsigned int cmd, unsigned long arg)
1141 struct sci_port *port = tty->driver_data;
1147 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1148 (unsigned int __user *) arg);
1151 if ((rc = get_user(ival, (unsigned int __user *) arg)) == 0)
1152 tty->termios->c_cflag =
1153 (tty->termios->c_cflag & ~CLOCAL) |
1154 (ival ? CLOCAL : 0);
1157 if ((rc = verify_area(VERIFY_WRITE, (void __user *) arg,
1158 sizeof(struct serial_struct))) == 0)
1159 rc = gs_getserial(&port->gs, (struct serial_struct *) arg);
1162 if ((rc = verify_area(VERIFY_READ, (void __user *) arg,
1163 sizeof(struct serial_struct))) == 0)
1164 rc = gs_setserial(&port->gs,
1165 (struct serial_struct *) arg);
1175 static void sci_throttle(struct tty_struct * tty)
1177 struct sci_port *port = (struct sci_port *)tty->driver_data;
1179 /* If the port is using any type of input flow
1180 * control then throttle the port.
1182 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
1183 port->gs.flags |= SCI_RX_THROTTLE;
1186 static void sci_unthrottle(struct tty_struct * tty)
1188 struct sci_port *port = (struct sci_port *)tty->driver_data;
1190 /* Always unthrottle even if flow control is not enabled on
1191 * this port in case we disabled flow control while the port
1194 port->gs.flags &= ~SCI_RX_THROTTLE;
1195 sci_enable_rx_interrupts(port);
1199 #ifdef CONFIG_PROC_FS
1200 static int sci_read_proc(char *page, char **start, off_t off, int count,
1201 int *eof, void *data)
1204 struct sci_port *port;
1207 len += sprintf(page, "sciinfo:0.1\n");
1208 for (i = 0; i < SCI_NPORTS && len < 4000; i++) {
1209 port = &sci_ports[i];
1210 len += sprintf(page+len, "%d: uart:%s address: %08x", i,
1211 (port->type == PORT_SCI) ? "SCI" : "SCIF",
1213 len += sprintf(page+len, " baud:%d", port->gs.baud);
1214 len += sprintf(page+len, " tx:%d rx:%d",
1215 port->icount.tx, port->icount.rx);
1217 if (port->icount.frame)
1218 len += sprintf(page+len, " fe:%d", port->icount.frame);
1219 if (port->icount.parity)
1220 len += sprintf(page+len, " pe:%d", port->icount.parity);
1221 if (port->icount.brk)
1222 len += sprintf(page+len, " brk:%d", port->icount.brk);
1223 if (port->icount.overrun)
1224 len += sprintf(page+len, " oe:%d", port->icount.overrun);
1225 len += sprintf(page+len, "\n");
1231 #ifdef CONFIG_CPU_FREQ
1233 * Here we define a transistion notifier so that we can update all of our
1234 * ports' baud rate when the peripheral clock changes.
1237 static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
1239 struct cpufreq_freqs *freqs = p;
1242 if (phase == CPUFREQ_POSTCHANGE) {
1243 for (i = 0; i < SCI_NPORTS; i++) {
1245 * This will force a baud rate change in hardware.
1247 if (sci_ports[i].gs.tty != NULL) {
1248 sci_set_baud(&sci_ports[i], sci_ports[i].gs.baud);
1251 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
1252 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
1258 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
1259 #endif /* CONFIG_CPU_FREQ */
1261 static struct tty_operations sci_ops = {
1265 .put_char = gs_put_char,
1266 .flush_chars = gs_flush_chars,
1267 .write_room = gs_write_room,
1268 .chars_in_buffer = gs_chars_in_buffer,
1269 .flush_buffer = gs_flush_buffer,
1271 .throttle = sci_throttle,
1272 .unthrottle = sci_unthrottle,
1273 .set_termios = gs_set_termios,
1276 .hangup = gs_hangup,
1277 #ifdef CONFIG_PROC_FS
1278 .read_proc = sci_read_proc,
1280 .tiocmget = sci_tiocmget,
1281 .tiocmset = sci_tiocmset,
1284 /* ********************************************************************** *
1285 * Here are the initialization routines. *
1286 * ********************************************************************** */
1288 static int sci_init_drivers(void)
1291 struct sci_port *port;
1292 sci_driver = alloc_tty_driver(SCI_NPORTS);
1296 sci_driver->owner = THIS_MODULE;
1297 sci_driver->driver_name = "sci";
1298 sci_driver->name = "ttySC";
1299 sci_driver->devfs_name = "ttsc/";
1300 sci_driver->major = SCI_MAJOR;
1301 sci_driver->minor_start = SCI_MINOR_START;
1302 sci_driver->type = TTY_DRIVER_TYPE_SERIAL;
1303 sci_driver->subtype = SERIAL_TYPE_NORMAL;
1304 sci_driver->init_termios = tty_std_termios;
1305 sci_driver->init_termios.c_cflag =
1306 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1307 sci_driver->flags = TTY_DRIVER_REAL_RAW;
1308 tty_set_operations(sci_driver, &sci_ops);
1309 if ((error = tty_register_driver(sci_driver))) {
1310 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1312 put_tty_driver(sci_driver);
1316 for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1317 port->gs.magic = SCI_MAGIC;
1318 port->gs.close_delay = HZ/2;
1319 port->gs.closing_wait = 30 * HZ;
1320 port->gs.rd = &sci_real_driver;
1321 init_waitqueue_head(&port->gs.open_wait);
1322 init_waitqueue_head(&port->gs.close_wait);
1323 port->old_cflag = 0;
1324 port->icount.cts = port->icount.dsr =
1325 port->icount.rng = port->icount.dcd = 0;
1326 port->icount.rx = port->icount.tx = 0;
1327 port->icount.frame = port->icount.parity = 0;
1328 port->icount.overrun = port->icount.brk = 0;
1331 #ifdef CONFIG_CPU_FREQ
1332 /* Setup transition notifier */
1333 if (cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER) < 0) {
1334 printk(KERN_ERR "sci: Unable to register CPU frequency notifier\n");
1337 printk("sci: CPU frequency notifier registered\n");
1342 static int sci_request_irq(struct sci_port *port)
1345 #if !defined(SCI_ONLY)
1346 irqreturn_t (*handlers[4])(int irq, void *p, struct pt_regs *regs) = {
1347 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1351 void (*handlers[3])(int irq, void *ptr, struct pt_regs *regs) = {
1352 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1355 for (i=0; i<(sizeof(handlers)/sizeof(handlers[0])); i++) {
1356 if (!port->irqs[i]) continue;
1357 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1359 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1366 static void sci_free_irq(struct sci_port *port)
1370 for (i=0; i<4; i++) {
1371 if (!port->irqs[i]) continue;
1372 free_irq(port->irqs[i], port);
1376 static char banner[] __initdata =
1377 KERN_INFO "SuperH SCI(F) driver initialized\n";
1379 int __init sci_init(void)
1381 struct sci_port *port;
1384 printk("%s", banner);
1386 for (j=0; j<SCI_NPORTS; j++) {
1387 port = &sci_ports[j];
1388 printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
1389 (port->type == PORT_SCI) ? "SCI" : "SCIF");
1394 #ifdef CONFIG_SH_STANDARD_BIOS
1395 sh_bios_gdb_detach();
1397 return 0; /* Return -EIO when not detected */
1400 module_init(sci_init);
1406 void cleanup_module(void)
1408 tty_unregister_driver(sci_driver);
1409 put_tty_driver(sci_driver);
1412 #include "generic_serial.c"
1415 #ifdef CONFIG_SERIAL_CONSOLE
1417 * Print a string to the serial port trying not to disturb
1418 * any possible real use of the port...
1420 static void serial_console_write(struct console *co, const char *s,
1423 put_string(sercons_port, s, count);
1426 static struct tty_driver *serial_console_device(struct console *c, int *index)
1433 * Setup initial baud/bits/parity. We do two things here:
1434 * - construct a cflag setting for the first rs_open()
1435 * - initialize the serial port
1436 * Return non-zero if we didn't find a serial port.
1438 static int __init serial_console_setup(struct console *co, char *options)
1443 int cflag = CREAD | HUPCL | CLOCAL;
1446 sercons_port = &sci_ports[co->index];
1449 baud = simple_strtoul(options, NULL, 10);
1451 while(*s >= '0' && *s <= '9')
1453 if (*s) parity = *s++;
1454 if (*s) bits = *s - '0';
1458 * Now construct a cflag setting.
1498 sercons_baud = baud;
1500 #if defined(__H8300S__)
1501 h8300_sci_enable(sercons_port,sci_enable);
1503 sci_set_termios_cflag(sercons_port, cflag, baud);
1504 sercons_port->old_cflag = cflag;
1509 static struct console sercons = {
1511 .write = serial_console_write,
1512 .device = serial_console_device,
1513 .setup = serial_console_setup,
1514 .flags = CON_PRINTBUFFER,
1522 #ifdef CONFIG_SH_EARLY_PRINTK
1523 extern void sh_console_unregister (void);
1526 static int __init sci_console_init(void)
1528 register_console(&sercons);
1529 #ifdef CONFIG_SH_EARLY_PRINTK
1530 /* Now that the real console is available, unregister the one we
1531 * used while first booting.
1533 sh_console_unregister();
1537 console_initcall(sci_console_init);
1539 #endif /* CONFIG_SERIAL_CONSOLE */
1542 #ifdef CONFIG_SH_KGDB
1544 /* Initialise the KGDB serial port */
1545 int kgdb_sci_setup(void)
1547 int cflag = CREAD | HUPCL | CLOCAL;
1549 if ((kgdb_portnum < 0) || (kgdb_portnum >= SCI_NPORTS))
1552 kgdb_sci_port = &sci_ports[kgdb_portnum];
1554 switch (kgdb_baud) {
1574 switch (kgdb_bits) {
1584 switch (kgdb_parity) {
1594 sci_set_termios_cflag(kgdb_sci_port, kgdb_cflag, kgdb_baud);
1596 /* Set up the interrupt for BREAK from GDB */
1597 /* Commented out for now since it may not be possible yet...
1598 request_irq(kgdb_sci_port->irqs[0], kgdb_break_interrupt,
1599 SA_INTERRUPT, "sci", kgdb_sci_port);
1600 sci_enable_rx_interrupts(kgdb_sci_port);
1603 /* Setup complete: initialize function pointers */
1604 kgdb_getchar = kgdb_sci_getchar;
1605 kgdb_putchar = kgdb_sci_putchar;
1610 #ifdef CONFIG_SH_KGDB_CONSOLE
1612 /* Create a console device */
1613 static kdev_t kgdb_console_device(struct console *c)
1615 return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1618 /* Set up the KGDB console */
1619 static int __init kgdb_console_setup(struct console *co, char *options)
1621 /* NB we ignore 'options' because we've already done the setup */
1622 co->cflag = kgdb_cflag;
1627 /* Register the KGDB console so we get messages (d'oh!) */
1628 void __init kgdb_console_init(void)
1630 register_console(&kgdbcons);
1633 /* The console structure for KGDB */
1634 static struct console kgdbcons = {
1636 write:kgdb_console_write,
1637 device:kgdb_console_device,
1638 wait_key:serial_console_wait_key,
1639 setup:kgdb_console_setup,
1640 flags:CON_PRINTBUFFER | CON_ENABLED,
1644 #endif /* CONFIG_SH_KGDB_CONSOLE */
1646 #endif /* CONFIG_SH_KGDB */