1 /* $Id: sh-sci.c,v 1.1.1.1.2.10 2003/09/17 23:38:56 davidm-sf 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 SecureEdge. David McCullough (2002)
10 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
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>
41 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/bitops.h>
47 #include <linux/generic_serial.h>
49 #ifdef CONFIG_SH_STANDARD_BIOS
50 #include <asm/sh_bios.h>
58 int kgdb_sci_setup(void);
59 static int kgdb_get_char(struct sci_port *port);
60 static void kgdb_put_char(struct sci_port *port, char c);
61 static void kgdb_handle_error(struct sci_port *port);
62 static struct sci_port *kgdb_sci_port;
64 #ifdef CONFIG_SH_KGDB_CONSOLE
65 static struct console kgdbcons;
66 void __init kgdb_console_init(void);
67 #endif /* CONFIG_SH_KGDB_CONSOLE */
69 #endif /* CONFIG_SH_KGDB */
71 #ifdef CONFIG_SERIAL_CONSOLE
72 static struct console sercons;
73 static struct sci_port* sercons_port=0;
74 static int sercons_baud;
75 #ifdef CONFIG_MAGIC_SYSRQ
76 #include <linux/sysrq.h>
77 static int break_pressed;
78 #endif /* CONFIG_MAGIC_SYSRQ */
79 #endif /* CONFIG_SERIAL_CONSOLE */
81 /* Function prototypes */
82 #if !defined(SCIF_ONLY)
83 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag);
86 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag);
87 #if defined(__sh3__) && !defined(CONFIG_CPU_SUBTYPE_SH7300)
88 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag);
91 static void sci_disable_tx_interrupts(void *ptr);
92 static void sci_enable_tx_interrupts(void *ptr);
93 static void sci_disable_rx_interrupts(void *ptr);
94 static void sci_enable_rx_interrupts(void *ptr);
95 static int sci_get_CD(void *ptr);
96 static void sci_shutdown_port(void *ptr);
97 static int sci_set_real_termios(void *ptr);
98 static void sci_hungup(void *ptr);
99 static void sci_close(void *ptr);
100 static int sci_chars_in_buffer(void *ptr);
101 static int sci_request_irq(struct sci_port *port);
102 static void sci_free_irq(struct sci_port *port);
103 static int sci_init_drivers(void);
105 static struct tty_driver sci_driver, sci_callout_driver;
107 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
108 static struct tty_struct *sci_table[SCI_NPORTS] = { NULL, };
109 static struct termios *sci_termios[SCI_NPORTS];
110 static struct termios *sci_termios_locked[SCI_NPORTS];
112 static int sci_refcount;
113 static int sci_debug = 0;
116 MODULE_PARM(sci_debug, "i");
119 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
121 #ifdef CONFIG_SERIAL_CONSOLE
122 static void put_char(struct sci_port *port, char c)
125 unsigned short status;
130 status = sci_in(port, SCxSR);
131 while (!(status & SCxSR_TDxE(port)));
133 sci_out(port, SCxTDR, c);
134 sci_in(port, SCxSR); /* Dummy read */
135 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
137 restore_flags(flags);
141 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
143 static void handle_error(struct sci_port *port)
144 { /* Clear error flags */
145 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
148 static int get_char(struct sci_port *port)
151 unsigned short status;
156 status = sci_in(port, SCxSR);
157 if (status & SCxSR_ERRORS(port)) {
161 } while (!(status & SCxSR_RDxF(port)));
162 c = sci_in(port, SCxRDR);
163 sci_in(port, SCxSR); /* Dummy read */
164 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
165 restore_flags(flags);
170 /* Taken from sh-stub.c of GDB 4.18 */
171 static const char hexchars[] = "0123456789abcdef";
173 static __inline__ char highhex(int x)
175 return hexchars[(x >> 4) & 0xf];
178 static __inline__ char lowhex(int x)
180 return hexchars[x & 0xf];
183 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
186 * Send the packet in buffer. The host gets one chance to read it.
187 * This routine does not wait for a positive acknowledge.
190 #ifdef CONFIG_SERIAL_CONSOLE
191 static void put_string(struct sci_port *port, const char *buffer, int count)
194 const unsigned char *p = buffer;
196 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
200 #ifdef CONFIG_SH_STANDARD_BIOS
201 /* This call only does a trap the first time it is
202 * called, and so is safe to do here unconditionally
204 usegdb |= sh_bios_in_gdb_mode();
206 #ifdef CONFIG_SH_KGDB
207 usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
211 /* $<packet info>#<checksum>. */
215 put_char(port, 'O'); /* 'O'utput to console */
218 for (i=0; i<count; i++) { /* Don't use run length encoding */
229 put_char(port, highhex(checksum));
230 put_char(port, lowhex(checksum));
231 } while (get_char(port) != '+');
233 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
234 for (i=0; i<count; i++) {
236 put_char(port, '\r');
237 put_char(port, *p++);
240 #endif /* CONFIG_SERIAL_CONSOLE */
243 #if defined(CONFIG_SH_SECUREEDGE5410)
245 struct timer_list sci_timer_struct;
246 static unsigned char sci_dcdstatus[2];
249 * This subroutine is called when the RS_TIMER goes off. It is used
250 * to monitor the state of the DCD lines - since they have no edge
251 * sensors and interrupt generators.
253 static void sci_timer(unsigned long data)
256 unsigned char dcdstatus[2];
258 s = SECUREEDGE_READ_IOPORT();
259 dcdstatus[0] = !(s & 0x10);
260 dcdstatus[1] = !(s & 0x1);
262 for (i = 0; i < 2; i++) {
263 if (dcdstatus[i] != sci_dcdstatus[i]) {
264 if (sci_ports[i].gs.count != 0) {
265 if (sci_ports[i].gs.flags & ASYNC_CHECK_CD) {
266 if (dcdstatus[i]) { /* DCD has gone high */
267 wake_up_interruptible(&sci_ports[i].gs.open_wait);
268 } else if (!((sci_ports[i].gs.flags&ASYNC_CALLOUT_ACTIVE) &&
269 (sci_ports[i].gs.flags & ASYNC_CALLOUT_NOHUP))) {
270 if (sci_ports[i].gs.tty)
271 tty_hangup(sci_ports[i].gs.tty);
276 sci_dcdstatus[i] = dcdstatus[i];
279 sci_timer_struct.expires = jiffies + HZ/25;
280 add_timer(&sci_timer_struct);
288 #ifdef CONFIG_SH_KGDB
290 /* Is the SCI ready, ie is there a char waiting? */
291 static int kgdb_is_char_ready(struct sci_port *port)
293 unsigned short status = sci_in(port, SCxSR);
295 if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
296 kgdb_handle_error(port);
298 return (status & SCxSR_RDxF(port));
302 static void kgdb_put_char(struct sci_port *port, char c)
304 unsigned short status;
307 status = sci_in(port, SCxSR);
308 while (!(status & SCxSR_TDxE(port)));
310 sci_out(port, SCxTDR, c);
311 sci_in(port, SCxSR); /* Dummy read */
312 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
315 /* Get a char if there is one, else ret -1 */
316 static int kgdb_get_char(struct sci_port *port)
320 if (kgdb_is_char_ready(port) == 0)
323 c = sci_in(port, SCxRDR);
324 sci_in(port, SCxSR); /* Dummy read */
325 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
331 /* Called from kgdbstub.c to get a character, i.e. is blocking */
332 static int kgdb_sci_getchar(void)
336 /* Keep trying to read a character, this could be neater */
337 while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
342 /* Called from kgdbstub.c to put a character, just a wrapper */
343 static void kgdb_sci_putchar(int c)
346 kgdb_put_char(kgdb_sci_port, c);
349 /* Clear any errors on the SCI */
350 static void kgdb_handle_error(struct sci_port *port)
352 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port)); /* Clear error flags */
355 /* Breakpoint if there's a break sent on the serial port */
356 static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
358 struct sci_port *port = ptr;
359 unsigned short status = sci_in(port, SCxSR);
361 if (status & SCxSR_BRK(port)) {
363 /* Break into the debugger if a break is detected */
367 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
372 #endif /* CONFIG_SH_KGDB */
374 static struct real_driver sci_real_driver = {
375 sci_disable_tx_interrupts,
376 sci_enable_tx_interrupts,
377 sci_disable_rx_interrupts,
378 sci_enable_rx_interrupts,
381 sci_set_real_termios,
388 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
389 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
394 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
396 /* For SH7300, SH7707, SH7709, SH7709A, SH7729 */
397 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
399 unsigned int fcr_val = 0;
401 #if !defined(CONFIG_CPU_SUBTYPE_SH7300) /* SH7300 doesn't use RTS/CTS */
405 /* We need to set SCPCR to enable RTS/CTS */
406 data = ctrl_inw(SCPCR);
407 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
408 ctrl_outw(data&0x0cff, SCPCR);
411 fcr_val |= SCFCR_MCE;
415 /* We need to set SCPCR to enable RTS/CTS */
416 data = ctrl_inw(SCPCR);
417 /* Clear out SCP7MD1,0, SCP4MD1,0,
418 Set SCP6MD1,0 = {01} (output) */
419 ctrl_outw((data&0x0cff)|0x1000, SCPCR);
421 data = ctrl_inb(SCPDR);
422 /* Set /RTS2 (bit6) = 0 */
423 ctrl_outb(data&0xbf, SCPDR);
426 sci_out(port, SCFCR, fcr_val);
429 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
431 unsigned int fcr_val = 0;
434 fcr_val |= SCFCR_MCE;
436 sci_out(port, SCFCR, fcr_val);
442 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
444 unsigned int fcr_val = 0;
446 if (cflag & CRTSCTS) {
447 fcr_val |= SCFCR_MCE;
449 sci_out(port, SCSPTR, 0x0080); /* Set RTS = 1 */
451 sci_out(port, SCFCR, fcr_val);
455 #endif /* SCIF_ONLY || SCI_AND_SCIF */
457 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
459 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
460 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
461 /* If you have signals for DTR and DCD, please implement here. */
463 #if defined(CONFIG_SH_SECUREEDGE5410)
467 if (port == &sci_ports[1]) { /* port 1 only */
469 SECUREEDGE_WRITE_IOPORT(0x0080, 0x0080);
471 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0080);
473 if (port == &sci_ports[0]) { /* port 0 only */
475 SECUREEDGE_WRITE_IOPORT(0x0200, 0x0200);
477 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0200);
479 SECUREEDGE_WRITE_IOPORT(0x0100, 0x0100);
481 SECUREEDGE_WRITE_IOPORT(0x0000, 0x0100);
483 restore_flags(flags);
487 static int sci_getsignals(struct sci_port *port)
489 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
492 #if defined(CONFIG_SH_SECUREEDGE5410)
493 if (port == &sci_ports[1]) { /* port 1 only */
494 unsigned short s = SECUREEDGE_READ_IOPORT();
495 int rc = TIOCM_RTS|TIOCM_DSR|TIOCM_CTS;
497 if ((s & 0x0001) == 0)
499 if ((SECUREEDGE_READ_IOPORT() & 0x0080) == 0)
503 if (port == &sci_ports[0]) { /* port 0 only */
504 unsigned short s = SECUREEDGE_READ_IOPORT();
507 if ((s & 0x0010) == 0)
509 if ((s & 0x0004) == 0)
511 if ((SECUREEDGE_READ_IOPORT() & 0x0200) == 0)
513 if ((SECUREEDGE_READ_IOPORT() & 0x0100) == 0)
519 return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
522 static void sci_set_baud(struct sci_port *port, int baud)
549 if (BPS_230400 != BPS_115200) {
554 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
561 sci_setsignals (port, 1, -1);
563 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
566 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
568 sci_out(port, SCBRR, t);
569 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
571 sci_setsignals (port, 0, -1);
575 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
578 unsigned int smr_val;
581 status = sci_in(port, SCxSR);
582 while (!(status & SCxSR_TEND(port)));
584 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
586 if (port->type == PORT_SCIF) {
587 #if defined(CONFIG_CPU_SUBTYPE_SH7300)
588 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST | SCFCR_TCRST);
590 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
594 smr_val = sci_in(port, SCSMR) & 3;
595 if ((cflag & CSIZE) == CS7)
603 sci_out(port, SCSMR, smr_val);
604 sci_set_baud(port, baud);
606 port->init_pins(port, cflag);
607 sci_out(port, SCSCR, SCSCR_INIT(port));
610 port->gs.flags &= ~ASYNC_CHECK_CD;
612 port->gs.flags |= ASYNC_CHECK_CD;
615 static int sci_set_real_termios(void *ptr)
617 struct sci_port *port = ptr;
619 if (port->old_cflag != port->gs.tty->termios->c_cflag) {
620 port->old_cflag = port->gs.tty->termios->c_cflag;
621 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
622 sci_enable_rx_interrupts(port);
628 /* ********************************************************************** *
629 * the interrupt related routines *
630 * ********************************************************************** */
633 * This routine is used by the interrupt handler to schedule
634 * processing in the software interrupt portion of the driver.
636 static inline void sci_sched_event(struct sci_port *port, int event)
638 port->event |= 1 << event;
639 queue_task(&port->tqueue, &tq_immediate);
640 mark_bh(IMMEDIATE_BH);
643 static void sci_transmit_chars(struct sci_port *port)
645 unsigned int count, i;
648 unsigned short status;
652 status = sci_in(port, SCxSR);
653 if (!(status & SCxSR_TDxE(port))) {
655 ctrl = sci_in(port, SCSCR);
656 if (port->gs.xmit_cnt == 0) {
657 ctrl &= ~SCI_CTRL_FLAGS_TIE;
658 port->gs.flags &= ~GS_TX_INTEN;
660 ctrl |= SCI_CTRL_FLAGS_TIE;
661 sci_out(port, SCSCR, ctrl);
662 restore_flags(flags);
667 count = port->gs.xmit_cnt;
668 if (port->type == PORT_SCIF) {
669 #if defined(CONFIG_CPU_SUBTYPE_SH7300)
670 txroom = 64 - (sci_in(port, SCFDR)>>8);
672 txroom = 16 - (sci_in(port, SCFDR)>>8);
675 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
680 /* Don't copy past the end of the source buffer */
681 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
682 count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
684 /* If for one reason or another, we can't copy more data, we're done! */
688 for (i=0; i<count; i++) {
689 c = port->gs.xmit_buf[port->gs.xmit_tail + i];
690 sci_out(port, SCxTDR, c);
692 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
694 port->icount.tx += count;
696 /* Update the kernel buffer end */
697 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
699 /* This one last. (this is essential)
700 It would allow others to start putting more data into the buffer! */
701 port->gs.xmit_cnt -= count;
704 if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
705 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
708 ctrl = sci_in(port, SCSCR);
709 if (port->gs.xmit_cnt == 0) {
710 ctrl &= ~SCI_CTRL_FLAGS_TIE;
711 port->gs.flags &= ~GS_TX_INTEN;
713 if (port->type == PORT_SCIF) {
714 sci_in(port, SCxSR); /* Dummy read */
715 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
717 ctrl |= SCI_CTRL_FLAGS_TIE;
719 sci_out(port, SCSCR, ctrl);
720 restore_flags(flags);
723 /* On SH3, SCIF may read end-of-break as a space->mark char */
724 #define STEPFN(c) ({int __c=(c); (((__c-1)|(__c)) == -1); })
726 static inline void sci_receive_chars(struct sci_port *port,
727 struct pt_regs *regs)
730 struct tty_struct *tty;
732 unsigned short status;
734 status = sci_in(port, SCxSR);
735 if (!(status & SCxSR_RDxF(port)))
741 if (port->type == PORT_SCIF) {
742 #if defined(CONFIG_CPU_SUBTYPE_SH7300)
743 count = sci_in(port, SCFDR)&0x007f;
745 count = sci_in(port, SCFDR)&0x001f;
748 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
751 /* we must clear RDF or we get stuck in the interrupt for ever */
752 sci_in(port, SCxSR); /* dummy read */
753 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
755 /* If for any reason we can't copy more data, we're done! */
759 if (port->type == PORT_SCI) {
760 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
761 *tty->flip.char_buf_ptr++ = sci_in(port, SCxRDR);
762 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
769 while (count > 0 && tty->flip.count < TTY_FLIPBUF_SIZE){
770 char c = sci_in(port, SCxRDR);
771 status = sci_in(port, SCxSR);
774 /* Skip "chars" during break */
775 if (port->break_flag) {
777 (status & SCxSR_FER(port))) {
781 /* Nonzero => end-of-break */
782 dprintk("scif: debounce<%02x>\n", c);
783 port->break_flag = 0;
790 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
791 if (break_pressed && (port == sercons_port)) {
794 break_pressed + HZ*5)) {
795 handle_sysrq(c, regs,
804 #endif /* CONFIG_SERIAL_CONSOLE && CONFIG_MAGIC_SYSRQ */
806 /* Store data and status */
807 *tty->flip.char_buf_ptr++ = c;
809 if (status&SCxSR_FER(port)) {
810 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
811 dprintk("sci: frame error\n");
812 } else if (status&SCxSR_PER(port)) {
813 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
814 dprintk("sci: parity error\n");
816 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
825 /* drop any remaining chars, we are full */
827 /* force an overrun error on last received char */
828 tty->flip.flag_buf_ptr[TTY_FLIPBUF_SIZE - 1] = TTY_OVERRUN;
830 (void) sci_in(port, SCxRDR);
835 /* Tell the rest of the system the news. New characters! */
836 tty_flip_buffer_push(tty);
838 sci_in(port, SCxSR); /* dummy read */
839 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
843 static inline int sci_handle_errors(struct sci_port *port)
846 unsigned short status = sci_in(port, SCxSR);
847 struct tty_struct *tty = port->gs.tty;
849 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
852 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
853 dprintk("sci: overrun error\n");
856 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
857 if (sci_rxd_in(port) == 0) {
858 /* Notify of BREAK */
860 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
861 dprintk("sci: BREAK detected\n");
866 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
867 dprintk("sci: frame error\n");
871 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
874 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
875 dprintk("sci: parity error\n");
879 tty->flip.count += copied;
880 tty_flip_buffer_push(tty);
886 static inline int sci_handle_breaks(struct sci_port *port)
889 unsigned short status = sci_in(port, SCxSR);
890 struct tty_struct *tty = port->gs.tty;
892 if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
895 if (port->break_flag)
897 port->break_flag = 1;
899 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
900 if (port == sercons_port) {
901 if (break_pressed == 0) {
902 break_pressed = jiffies;
903 dprintk("sci: implied sysrq\n");
906 /* Double break implies a real break */
910 /* Notify of BREAK */
912 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
913 dprintk("sci: BREAK detected\n");
915 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
919 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined (CONFIG_CPU_SUBTYPE_SH7751) || defined(CONFIG_CPU_SUBTYPE_ST40)
920 /* XXX: Handle SCIF overrun error */
921 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
922 sci_out(port, SCLSR, 0);
923 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
925 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
926 dprintk("sci: overrun error\n");
932 tty->flip.count += copied;
933 tty_flip_buffer_push(tty);
939 static void sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
941 struct sci_port *port = ptr;
943 if (port->gs.flags & GS_ACTIVE)
944 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
945 sci_receive_chars(port, regs);
948 sci_disable_rx_interrupts(port);
951 static void sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
953 struct sci_port *port = ptr;
955 if (port->gs.flags & GS_ACTIVE)
956 sci_transmit_chars(port);
958 sci_disable_tx_interrupts(port);
962 static void sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
964 struct sci_port *port = ptr;
967 if (port->type == PORT_SCI) {
968 if(sci_handle_errors(port)) {
969 /* discard character in rx buffer */
971 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
975 sci_rx_interrupt(irq, ptr, regs);
977 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
979 /* Kick the transmission */
980 sci_tx_interrupt(irq, ptr, regs);
983 static void sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
985 struct sci_port *port = ptr;
988 sci_handle_breaks(port);
989 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
992 static void sci_mpxed_interrupt(int irq, void *ptr, struct pt_regs *regs)
994 unsigned short ssr_status, scr_status;
995 struct sci_port *port = ptr;
997 ssr_status=sci_in(port,SCxSR);
998 scr_status=sci_in(port,SCSCR);
1000 if((ssr_status&0x0020) && (scr_status&0x0080)){ /* Tx Interrupt */
1001 sci_tx_interrupt(irq, ptr, regs);
1003 if((ssr_status&0x0002) && (scr_status&0x0040)){ /* Rx Interrupt */
1004 sci_rx_interrupt(irq, ptr, regs);
1006 if((ssr_status&0x0080) && (scr_status&0x0400)){ /* Error Interrupt */
1007 sci_er_interrupt(irq, ptr, regs);
1009 if((ssr_status&0x0010) && (scr_status&0x0200)){ /* Break Interrupt */
1010 sci_br_interrupt(irq, ptr, regs);
1014 static void do_softint(void *private_)
1016 struct sci_port *port = (struct sci_port *) private_;
1017 struct tty_struct *tty;
1023 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
1028 /* ********************************************************************** *
1029 * Here are the routines that actually *
1030 * interface with the generic_serial driver *
1031 * ********************************************************************** */
1033 static void sci_disable_tx_interrupts(void *ptr)
1035 struct sci_port *port = ptr;
1036 unsigned long flags;
1037 unsigned short ctrl;
1039 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1040 save_and_cli(flags);
1041 ctrl = sci_in(port, SCSCR);
1042 ctrl &= ~SCI_CTRL_FLAGS_TIE;
1043 sci_out(port, SCSCR, ctrl);
1044 restore_flags(flags);
1047 static void sci_enable_tx_interrupts(void *ptr)
1049 struct sci_port *port = ptr;
1051 disable_irq(port->irqs[SCIx_TXI_IRQ]);
1052 sci_transmit_chars(port);
1053 enable_irq(port->irqs[SCIx_TXI_IRQ]);
1056 static void sci_disable_rx_interrupts(void * ptr)
1058 struct sci_port *port = ptr;
1059 unsigned long flags;
1060 unsigned short ctrl;
1062 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
1063 save_and_cli(flags);
1064 ctrl = sci_in(port, SCSCR);
1065 ctrl &= ~SCI_CTRL_FLAGS_RIE;
1066 sci_out(port, SCSCR, ctrl);
1067 restore_flags(flags);
1070 static void sci_enable_rx_interrupts(void * ptr)
1072 struct sci_port *port = ptr;
1073 unsigned long flags;
1074 unsigned short ctrl;
1076 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
1077 save_and_cli(flags);
1078 ctrl = sci_in(port, SCSCR);
1079 ctrl |= SCI_CTRL_FLAGS_RIE;
1080 sci_out(port, SCSCR, ctrl);
1081 restore_flags(flags);
1084 static int sci_get_CD(void * ptr)
1086 /* If you have signal for CD (Carrier Detect), please change here. */
1088 #if defined(CONFIG_SH_SECUREEDGE5410)
1089 struct sci_port *port = ptr;
1091 if (port == &sci_ports[0] || port == &sci_ports[1])
1092 if ((sci_getsignals(port) & TIOCM_CAR) == 0)
1099 static int sci_chars_in_buffer(void * ptr)
1101 struct sci_port *port = ptr;
1103 if (port->type == PORT_SCIF) {
1104 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
1106 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
1110 static void sci_shutdown_port(void * ptr)
1112 struct sci_port *port = ptr;
1114 port->gs.flags &= ~ GS_ACTIVE;
1115 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
1116 sci_setsignals(port, 0, 0);
1120 /* ********************************************************************** *
1121 * Here are the routines that actually *
1122 * interface with the rest of the system *
1123 * ********************************************************************** */
1125 static int sci_open(struct tty_struct * tty, struct file * filp)
1127 struct sci_port *port;
1128 int retval = 0, line;
1130 line = MINOR(tty->device) - SCI_MINOR_START;
1132 if ((line < 0) || (line >= SCI_NPORTS))
1135 port = &sci_ports[line];
1137 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1138 if (port->base == 0) {
1139 port->base = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1145 tty->driver_data = port;
1150 port->tqueue.routine = do_softint;
1151 port->tqueue.data = port;
1152 port->break_flag = 0;
1154 if (port->gs.count == 1) {
1157 retval = sci_request_irq(port);
1164 * Start up serial port
1166 retval = gs_init_port(&port->gs);
1171 port->gs.flags |= GS_ACTIVE;
1172 sci_setsignals(port, 1,1);
1174 retval = gs_block_til_ready(port, filp);
1180 if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
1181 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1182 *tty->termios = port->gs.normal_termios;
1184 *tty->termios = port->gs.callout_termios;
1185 sci_set_real_termios(port);
1188 #ifdef CONFIG_SERIAL_CONSOLE
1189 if (sercons.cflag && sercons.index == line) {
1190 tty->termios->c_cflag = sercons.cflag;
1191 port->gs.baud = sercons_baud;
1193 sci_set_real_termios(port);
1197 #ifdef CONFIG_SH_KGDB_CONSOLE
1198 if (kgdbcons.cflag && kgdbcons.index == line) {
1199 tty->termios->c_cflag = kgdbcons.cflag;
1200 port->gs.baud = kgdb_baud;
1202 sci_set_real_termios(port);
1204 #elif CONFIG_SH_KGDB
1205 /* Even for non-console, may defer to kgdb */
1206 if (port == kgdb_sci_port && kgdb_in_gdb_mode) {
1207 tty->termios->c_cflag = kgdb_cflag;
1208 port->gs.baud = kgdb_baud;
1210 sci_set_real_termios(port);
1212 #endif /* CONFIG_SH_KGDB */
1214 sci_enable_rx_interrupts(port);
1216 port->gs.session = current->session;
1217 port->gs.pgrp = current->pgrp;
1229 static void sci_hungup(void *ptr)
1234 static void sci_close(void *ptr)
1239 static int sci_ioctl(struct tty_struct * tty, struct file * filp,
1240 unsigned int cmd, unsigned long arg)
1243 struct sci_port *port = tty->driver_data;
1249 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1250 (unsigned int *) arg);
1253 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
1254 tty->termios->c_cflag =
1255 (tty->termios->c_cflag & ~CLOCAL) |
1256 (ival ? CLOCAL : 0);
1259 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
1260 sizeof(struct serial_struct))) == 0)
1261 rc = gs_getserial(&port->gs, (struct serial_struct *) arg);
1264 if ((rc = verify_area(VERIFY_READ, (void *) arg,
1265 sizeof(struct serial_struct))) == 0)
1266 rc = gs_setserial(&port->gs,
1267 (struct serial_struct *) arg);
1270 ival = sci_getsignals(port);
1271 rc = put_user(ival, (unsigned int *) arg);
1274 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
1275 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
1276 ((ival & TIOCM_RTS) ? 1 : -1));
1279 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
1280 sci_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
1281 ((ival & TIOCM_RTS) ? 0 : -1));
1284 if ((rc = get_user(ival, (unsigned int *)arg)) == 0)
1285 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
1286 ((ival & TIOCM_RTS) ? 1 : 0));
1297 static void sci_throttle(struct tty_struct * tty)
1299 struct sci_port *port = (struct sci_port *)tty->driver_data;
1301 /* If the port is using any type of input flow
1302 * control then throttle the port.
1304 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
1305 port->gs.flags |= SCI_RX_THROTTLE;
1308 static void sci_unthrottle(struct tty_struct * tty)
1310 struct sci_port *port = (struct sci_port *)tty->driver_data;
1312 /* Always unthrottle even if flow control is not enabled on
1313 * this port in case we disabled flow control while the port
1316 port->gs.flags &= ~SCI_RX_THROTTLE;
1317 sci_enable_rx_interrupts(port);
1321 #ifdef CONFIG_PROC_FS
1322 static int sci_read_proc(char *page, char **start, off_t off, int count,
1323 int *eof, void *data)
1326 struct sci_port *port;
1329 len += sprintf(page, "sciinfo:0.1\n");
1330 for (i = 0; i < SCI_NPORTS && len < 4000; i++) {
1331 port = &sci_ports[i];
1332 len += sprintf(page+len, "%d: uart:%s address: %08x", i,
1333 (port->type == PORT_SCI) ? "SCI" : "SCIF",
1335 len += sprintf(page+len, " baud:%d", port->gs.baud);
1336 len += sprintf(page+len, " tx:%d rx:%d",
1337 port->icount.tx, port->icount.rx);
1339 if (port->icount.frame)
1340 len += sprintf(page+len, " fe:%d", port->icount.frame);
1341 if (port->icount.parity)
1342 len += sprintf(page+len, " pe:%d", port->icount.parity);
1343 if (port->icount.brk)
1344 len += sprintf(page+len, " brk:%d", port->icount.brk);
1345 if (port->icount.overrun)
1346 len += sprintf(page+len, " oe:%d", port->icount.overrun);
1347 len += sprintf(page+len, "\n");
1353 /* ********************************************************************** *
1354 * Here are the initialization routines. *
1355 * ********************************************************************** */
1357 static int sci_init_drivers(void)
1360 struct sci_port *port;
1362 memset(&sci_driver, 0, sizeof(sci_driver));
1363 sci_driver.magic = TTY_DRIVER_MAGIC;
1364 sci_driver.driver_name = "sci";
1365 #ifdef CONFIG_DEVFS_FS
1366 sci_driver.name = "ttsc/%d";
1368 sci_driver.name = "ttySC";
1370 sci_driver.major = SCI_MAJOR;
1371 sci_driver.minor_start = SCI_MINOR_START;
1372 sci_driver.num = SCI_NPORTS;
1373 sci_driver.type = TTY_DRIVER_TYPE_SERIAL;
1374 sci_driver.subtype = SERIAL_TYPE_NORMAL;
1375 sci_driver.init_termios = tty_std_termios;
1376 sci_driver.init_termios.c_cflag =
1377 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1378 sci_driver.flags = TTY_DRIVER_REAL_RAW;
1379 sci_driver.refcount = &sci_refcount;
1380 sci_driver.table = sci_table;
1381 sci_driver.termios = sci_termios;
1382 sci_driver.termios_locked = sci_termios_locked;
1384 sci_driver.open = sci_open;
1385 sci_driver.close = gs_close;
1386 sci_driver.write = gs_write;
1387 sci_driver.put_char = gs_put_char;
1388 sci_driver.flush_chars = gs_flush_chars;
1389 sci_driver.write_room = gs_write_room;
1390 sci_driver.chars_in_buffer = gs_chars_in_buffer;
1391 sci_driver.flush_buffer = gs_flush_buffer;
1392 sci_driver.ioctl = sci_ioctl;
1393 sci_driver.throttle = sci_throttle;
1394 sci_driver.unthrottle = sci_unthrottle;
1395 sci_driver.set_termios = gs_set_termios;
1396 sci_driver.stop = gs_stop;
1397 sci_driver.start = gs_start;
1398 sci_driver.hangup = gs_hangup;
1399 #ifdef CONFIG_PROC_FS
1400 sci_driver.read_proc = sci_read_proc;
1403 sci_callout_driver = sci_driver;
1404 #ifdef CONFIG_DEVFS_FS
1405 sci_callout_driver.name = "cusc/%d";
1407 sci_callout_driver.name = "cusc";
1409 sci_callout_driver.major = SCI_MAJOR+1;
1410 sci_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1411 sci_callout_driver.read_proc = NULL;
1413 if ((error = tty_register_driver(&sci_driver))) {
1414 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1418 if ((error = tty_register_driver(&sci_callout_driver))) {
1419 tty_unregister_driver(&sci_driver);
1420 printk(KERN_ERR "sci: Couldn't register SCI callout driver, error = %d\n",
1425 for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1426 port->gs.callout_termios = sci_callout_driver.init_termios;
1427 port->gs.normal_termios = sci_driver.init_termios;
1428 port->gs.magic = SCI_MAGIC;
1429 port->gs.close_delay = HZ/2;
1430 port->gs.closing_wait = 30 * HZ;
1431 port->gs.rd = &sci_real_driver;
1432 init_waitqueue_head(&port->gs.open_wait);
1433 init_waitqueue_head(&port->gs.close_wait);
1434 port->old_cflag = 0;
1435 port->icount.cts = port->icount.dsr =
1436 port->icount.rng = port->icount.dcd = 0;
1437 port->icount.rx = port->icount.tx = 0;
1438 port->icount.frame = port->icount.parity = 0;
1439 port->icount.overrun = port->icount.brk = 0;
1445 static int sci_request_irq(struct sci_port *port)
1448 void (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
1449 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1453 if(port->irqs[0] == port->irqs[1]){
1454 if (!port->irqs[0]){
1455 printk(KERN_ERR "sci: Cannot allocate irq.(IRQ=0)\n");
1458 if (request_irq(port->irqs[0], sci_mpxed_interrupt, SA_INTERRUPT,
1460 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1465 for (i=0; i<4; i++) {
1466 if (!port->irqs[i]) continue;
1467 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1469 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1477 static void sci_free_irq(struct sci_port *port)
1481 if(port->irqs[0] == port->irqs[1]){
1483 printk("sci: sci_free_irq error\n");
1485 free_irq(port->irqs[0], port);
1488 for (i=0; i<4; i++) {
1489 if (!port->irqs[i]) continue;
1490 free_irq(port->irqs[i], port);
1495 static char banner[] __initdata =
1496 KERN_INFO "SuperH SCI(F) driver initialized\n";
1498 int __init sci_init(void)
1500 struct sci_port *port;
1503 printk("%s", banner);
1505 for (j=0; j<SCI_NPORTS; j++) {
1506 port = &sci_ports[j];
1507 printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
1508 (port->type == PORT_SCI) ? "SCI" : "SCIF");
1511 #if defined(CONFIG_SH_SECUREEDGE5410)
1512 init_timer(&sci_timer_struct);
1513 sci_timer_struct.function = sci_timer;
1514 sci_timer_struct.data = 0;
1515 sci_timer_struct.expires = jiffies + HZ/25;
1516 add_timer(&sci_timer_struct);
1518 j = SECUREEDGE_READ_IOPORT();
1519 sci_dcdstatus[0] = !(j & 0x10);
1520 sci_dcdstatus[1] = !(j & 0x1);
1525 #ifdef CONFIG_SH_STANDARD_BIOS
1526 sh_bios_gdb_detach();
1528 return 0; /* Return -EIO when not detected */
1531 module_init(sci_init);
1537 void cleanup_module(void)
1539 #if defined(CONFIG_SH_SECUREEDGE5410)
1540 del_timer(&sci_timer_struct);
1542 tty_unregister_driver(&sci_driver);
1543 tty_unregister_driver(&sci_callout_driver);
1546 #include "generic_serial.c"
1549 #ifdef CONFIG_SERIAL_CONSOLE
1551 * Print a string to the serial port trying not to disturb
1552 * any possible real use of the port...
1554 static void serial_console_write(struct console *co, const char *s,
1557 put_string(sercons_port, s, count);
1560 static kdev_t serial_console_device(struct console *c)
1562 return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1566 * Setup initial baud/bits/parity. We do two things here:
1567 * - construct a cflag setting for the first rs_open()
1568 * - initialize the serial port
1569 * Return non-zero if we didn't find a serial port.
1571 static int __init serial_console_setup(struct console *co, char *options)
1576 int cflag = CREAD | HUPCL | CLOCAL;
1579 sercons_port = &sci_ports[co->index];
1581 #if defined(CONFIG_CPU_SUBTYPE_SH5_101) || defined(CONFIG_CPU_SUBTYPE_SH5_103)
1582 sercons_port->base = onchip_remap(SCIF_ADDR_SH5, 1024, "SCIF");
1583 if (!sercons_port->base)
1588 baud = simple_strtoul(options, NULL, 10);
1590 while(*s >= '0' && *s <= '9')
1592 if (*s) parity = *s++;
1593 if (*s) bits = *s - '0';
1597 * Now construct a cflag setting.
1639 #ifdef CONFIG_SH_KGDB
1640 if (kgdb_in_gdb_mode && sercons_port == kgdb_sci_port) {
1641 co->cflag = kgdb_cflag;
1642 sercons_baud = kgdb_baud;
1643 sercons_port->old_cflag = cflag;
1646 #endif /* CONFIG_SH_KGDB */
1649 sercons_baud = baud;
1651 sci_set_termios_cflag(sercons_port, cflag, baud);
1652 sercons_port->old_cflag = cflag;
1658 static struct console sercons = {
1660 write: serial_console_write,
1661 device: serial_console_device,
1662 setup: serial_console_setup,
1663 flags: CON_PRINTBUFFER,
1671 #ifdef CONFIG_SH_EARLY_PRINTK
1672 extern void sh_console_unregister (void);
1675 void __init sci_console_init(void)
1677 register_console(&sercons);
1678 #ifdef CONFIG_SH_EARLY_PRINTK
1679 /* Now that the real console is available, unregister the one we
1680 * used while first booting.
1682 sh_console_unregister();
1685 #endif /* CONFIG_SERIAL_CONSOLE */
1688 #ifdef CONFIG_SH_KGDB
1690 /* Initialise the KGDB serial port */
1691 int kgdb_sci_setup(void)
1693 int cflag = CREAD | HUPCL | CLOCAL;
1695 if ((kgdb_portnum < 0) || (kgdb_portnum >= SCI_NPORTS))
1698 kgdb_sci_port = &sci_ports[kgdb_portnum];
1700 switch (kgdb_baud) {
1720 switch (kgdb_bits) {
1730 switch (kgdb_parity) {
1740 sci_set_termios_cflag(kgdb_sci_port, kgdb_cflag, kgdb_baud);
1742 /* Set up the interrupt for BREAK from GDB */
1743 /* Commented out for now since it may not be possible yet...
1744 request_irq(kgdb_sci_port->irqs[0], kgdb_break_interrupt,
1745 SA_INTERRUPT, "sci", kgdb_sci_port);
1746 sci_enable_rx_interrupts(kgdb_sci_port);
1749 /* Setup complete: initialize function pointers */
1750 kgdb_getchar = kgdb_sci_getchar;
1751 kgdb_putchar = kgdb_sci_putchar;
1756 #ifdef CONFIG_SH_KGDB_CONSOLE
1758 /* Create a console device */
1759 static kdev_t kgdb_console_device(struct console *c)
1761 return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1764 /* Set up the KGDB console */
1765 static int __init kgdb_console_setup(struct console *co, char *options)
1767 /* NB we ignore 'options' because we've already done the setup */
1768 co->cflag = kgdb_cflag;
1773 /* Register the KGDB console so we get messages (d'oh!) */
1774 void __init kgdb_console_init(void)
1776 register_console(&kgdbcons);
1779 /* The console structure for KGDB */
1780 static struct console kgdbcons = {
1782 write:kgdb_console_write,
1783 device:kgdb_console_device,
1784 wait_key:serial_console_wait_key,
1785 setup:kgdb_console_setup,
1786 flags:CON_PRINTBUFFER | CON_ENABLED,
1790 #endif /* CONFIG_SH_KGDB_CONSOLE */
1792 #endif /* CONFIG_SH_KGDB */