- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / sh-sci.c
blob01812ecb0096e85649466114f835f7c212c0d4c2
1 /* $Id: sh-sci.c,v 1.40 2000/04/15 06:57:29 gniibe 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).
10 * TTY code is based on sx.c (Specialix SX driver) by:
12 * (C) 1998 R.E.Wolff@BitWizard.nl
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/signal.h>
20 #include <linux/sched.h>
21 #include <linux/timer.h>
22 #include <linux/interrupt.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
29 #include <linux/ptrace.h>
30 #include <linux/ioport.h>
31 #include <linux/mm.h>
32 #include <linux/malloc.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #ifdef CONFIG_SERIAL_CONSOLE
36 #include <linux/console.h>
37 #endif
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/uaccess.h>
43 #include <asm/bitops.h>
45 #include <linux/generic_serial.h>
47 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
48 #include <asm/sh_bios.h>
49 #endif
51 #include "sh-sci.h"
53 #ifdef CONFIG_SERIAL_CONSOLE
54 static struct console sercons;
55 static struct sci_port* sercons_port=0;
56 static int sercons_baud;
57 #endif
59 /* Function prototypes */
60 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag);
61 #ifndef SCI_ONLY
62 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag);
63 #if defined(__sh3__)
64 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag);
65 #endif
66 #endif
67 static void sci_disable_tx_interrupts(void *ptr);
68 static void sci_enable_tx_interrupts(void *ptr);
69 static void sci_disable_rx_interrupts(void *ptr);
70 static void sci_enable_rx_interrupts(void *ptr);
71 static int sci_get_CD(void *ptr);
72 static void sci_shutdown_port(void *ptr);
73 static int sci_set_real_termios(void *ptr);
74 static void sci_hungup(void *ptr);
75 static void sci_close(void *ptr);
76 static int sci_chars_in_buffer(void *ptr);
77 static int sci_init_drivers(void);
79 static struct tty_driver sci_driver, sci_callout_driver;
81 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
82 static struct tty_struct *sci_table[SCI_NPORTS] = { NULL, };
83 static struct termios *sci_termios[SCI_NPORTS];
84 static struct termios *sci_termios_locked[SCI_NPORTS];
86 int sci_refcount;
87 int sci_debug = 0;
89 #ifdef MODULE
90 MODULE_PARM(sci_debug, "i");
91 #endif
93 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
95 static void put_char(struct sci_port *port, char c)
97 unsigned long flags;
98 unsigned short status;
100 save_and_cli(flags);
103 status = sci_in(port, SCxSR);
104 while (!(status & SCxSR_TDxE(port)));
106 sci_out(port, SCxTDR, c);
107 sci_in(port, SCxSR); /* Dummy read */
108 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
110 restore_flags(flags);
113 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
115 static void handle_error(struct sci_port *port)
116 { /* Clear error flags */
117 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
120 static int get_char(struct sci_port *port)
122 unsigned long flags;
123 unsigned short status;
124 int c;
126 save_and_cli(flags);
127 do {
128 status = sci_in(port, SCxSR);
129 if (status & SCxSR_ERRORS(port)) {
130 handle_error(port);
131 continue;
133 } while (!(status & SCxSR_RDxF(port)));
134 c = sci_in(port, SCxRDR);
135 sci_in(port, SCxSR); /* Dummy read */
136 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
137 restore_flags(flags);
139 return c;
142 /* Taken from sh-stub.c of GDB 4.18 */
143 static const char hexchars[] = "0123456789abcdef";
145 static __inline__ char highhex(int x)
147 return hexchars[(x >> 4) & 0xf];
150 static __inline__ char lowhex(int x)
152 return hexchars[x & 0xf];
155 #endif
158 * Send the packet in buffer. The host gets one chance to read it.
159 * This routine does not wait for a positive acknowledge.
162 static void put_string(struct sci_port *port,
163 const char *buffer, int count)
165 int i;
166 const unsigned char *p = buffer;
167 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
168 int checksum;
170 /* This call only does a trap the first time it is
171 * called, and so is safe to do here unconditionally
173 if (sh_bios_in_gdb_mode()) {
174 /* $<packet info>#<checksum>. */
175 do {
176 unsigned char c;
177 put_char(port, '$');
178 put_char(port, 'O'); /* 'O'utput to console */
179 checksum = 'O';
181 for (i=0; i<count; i++) { /* Don't use run length encoding */
182 int h, l;
184 c = *p++;
185 h = highhex(c);
186 l = lowhex(c);
187 put_char(port, h);
188 put_char(port, l);
189 checksum += h + l;
191 put_char(port, '#');
192 put_char(port, highhex(checksum));
193 put_char(port, lowhex(checksum));
194 } while (get_char(port) != '+');
195 } else
196 #endif
197 for (i=0; i<count; i++) {
198 if (*p == 10)
199 put_char(port, '\r');
200 put_char(port, *p++);
206 static struct real_driver sci_real_driver = {
207 sci_disable_tx_interrupts,
208 sci_enable_tx_interrupts,
209 sci_disable_rx_interrupts,
210 sci_enable_rx_interrupts,
211 sci_get_CD,
212 sci_shutdown_port,
213 sci_set_real_termios,
214 sci_chars_in_buffer,
215 sci_close,
216 sci_hungup,
217 NULL
220 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
221 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
224 #endif
226 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
227 #if defined(__sh3__)
228 /* For SH7707, SH7709, SH7709A, SH7729 */
229 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
231 unsigned int fcr_val = 0;
234 unsigned short data;
236 /* We need to set SCPCR to enable RTS/CTS */
237 data = ctrl_inw(SCPCR);
238 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
239 ctrl_outw(data&0x0fcf, SCPCR);
241 if (cflag & CRTSCTS)
242 fcr_val |= SCFCR_MCE;
243 else {
244 unsigned short data;
246 /* We need to set SCPCR to enable RTS/CTS */
247 data = ctrl_inw(SCPCR);
248 /* Clear out SCP7MD1,0, SCP4MD1,0,
249 Set SCP6MD1,0 = {01} (output) */
250 ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
252 data = ctrl_inb(SCPDR);
253 /* Set /RTS2 (bit6) = 0 */
254 ctrl_outb(data&0xbf, SCPDR);
256 sci_out(port, SCFCR, fcr_val);
259 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
261 unsigned int fcr_val = 0;
263 if (cflag & CRTSCTS)
264 fcr_val |= SCFCR_MCE;
266 sci_out(port, SCFCR, fcr_val);
269 #else
271 /* For SH7750 */
272 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
274 unsigned int fcr_val = 0;
276 if (cflag & CRTSCTS) {
277 fcr_val |= SCFCR_MCE;
278 } else {
279 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
281 sci_out(port, SCFCR, fcr_val);
284 #endif
285 #endif /* SCIF_ONLY || SCI_AND_SCIF */
287 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
289 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
290 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
291 /* If you have signals for DTR and DCD, please implement here. */
295 static int sci_getsignals(struct sci_port *port)
297 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
298 and CTS/RTS */
300 return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
302 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
303 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
304 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
305 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
306 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
307 ((i_stat & IP_RI) ?TIOCM_RNG:0)
311 static void sci_set_baud(struct sci_port *port, int baud)
313 int t;
315 switch (baud) {
316 case 0:
317 t = -1;
318 break;
319 case 2400:
320 t = BPS_2400;
321 break;
322 case 4800:
323 t = BPS_4800;
324 break;
325 case 9600:
326 t = BPS_9600;
327 break;
328 case 19200:
329 t = BPS_19200;
330 break;
331 case 38400:
332 t = BPS_38400;
333 break;
334 case 57600:
335 t = BPS_57600;
336 break;
337 default:
338 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
339 case 115200:
340 t = BPS_115200;
341 break;
344 if (t > 0) {
345 sci_setsignals (port, 1, -1);
346 if(t >= 256) {
347 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
348 t >>= 2;
349 } else {
350 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
352 sci_out(port, SCBRR, t);
353 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
354 } else {
355 sci_setsignals (port, 0, -1);
359 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
361 unsigned int status;
362 unsigned int smr_val;
365 status = sci_in(port, SCxSR);
366 while (!(status & SCxSR_TEND(port)));
368 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
370 if (port->type == PORT_SCIF) {
371 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
374 smr_val = sci_in(port, SCSMR) & 3;
375 if ((cflag & CSIZE) == CS7)
376 smr_val |= 0x40;
377 if (cflag & PARENB)
378 smr_val |= 0x20;
379 if (cflag & PARODD)
380 smr_val |= 0x10;
381 if (cflag & CSTOPB)
382 smr_val |= 0x08;
383 sci_out(port, SCSMR, smr_val);
384 sci_set_baud(port, baud);
386 port->init_pins(port, cflag);
387 sci_out(port, SCSCR, SCSCR_INIT(port));
390 static int sci_set_real_termios(void *ptr)
392 struct sci_port *port = ptr;
394 if (port->old_cflag != port->gs.tty->termios->c_cflag) {
395 port->old_cflag = port->gs.tty->termios->c_cflag;
396 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
397 sci_enable_rx_interrupts(port);
400 /* Tell line discipline whether we will do input cooking */
401 if (I_OTHER(port->gs.tty))
402 clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
403 else
404 set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
406 /* Tell line discipline whether we will do output cooking.
407 * If OPOST is set and no other output flags are set then we can do output
408 * processing. Even if only *one* other flag in the O_OTHER group is set
409 * we do cooking in software.
411 if (O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty))
412 set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
413 else
414 clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
416 return 0;
419 /* ********************************************************************** *
420 * the interrupt related routines *
421 * ********************************************************************** */
424 * This routine is used by the interrupt handler to schedule
425 * processing in the software interrupt portion of the driver.
427 static inline void sci_sched_event(struct sci_port *port, int event)
429 port->event |= 1 << event;
430 queue_task(&port->tqueue, &tq_immediate);
431 mark_bh(IMMEDIATE_BH);
434 static void sci_transmit_chars(struct sci_port *port)
436 int count, i;
437 int txroom;
438 unsigned long flags;
439 unsigned short status;
440 unsigned short ctrl;
441 unsigned char c;
443 status = sci_in(port, SCxSR);
444 if (!(status & SCxSR_TDxE(port))) {
445 save_and_cli(flags);
446 ctrl = sci_in(port, SCSCR);
447 if (port->gs.xmit_cnt == 0) {
448 ctrl &= ~SCI_CTRL_FLAGS_TIE;
449 port->gs.flags &= ~GS_TX_INTEN;
450 } else
451 ctrl |= SCI_CTRL_FLAGS_TIE;
452 sci_out(port, SCSCR, ctrl);
453 restore_flags(flags);
454 return;
457 while (1) {
458 count = port->gs.xmit_cnt;
459 if (port->type == PORT_SCIF) {
460 txroom = 16 - (sci_in(port, SCFDR)>>8);
461 } else {
462 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
464 if (count > txroom)
465 count = txroom;
467 /* Don't copy pas the end of the source buffer */
468 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
469 count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
471 /* If for one reason or another, we can't copy more data, we're done! */
472 if (count == 0)
473 break;
475 for (i=0; i<count; i++) {
476 c = port->gs.xmit_buf[port->gs.xmit_tail + i];
477 sci_out(port, SCxTDR, c);
479 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
481 port->icount.tx += count;
483 /* Update the kernel buffer end */
484 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
486 /* This one last. (this is essential)
487 It would allow others to start putting more data into the buffer! */
488 port->gs.xmit_cnt -= count;
491 if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
492 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
494 save_and_cli(flags);
495 ctrl = sci_in(port, SCSCR);
496 if (port->gs.xmit_cnt == 0) {
497 ctrl &= ~SCI_CTRL_FLAGS_TIE;
498 port->gs.flags &= ~GS_TX_INTEN;
499 } else {
500 if (port->type == PORT_SCIF) {
501 sci_in(port, SCxSR); /* Dummy read */
502 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
504 ctrl |= SCI_CTRL_FLAGS_TIE;
506 sci_out(port, SCSCR, ctrl);
507 restore_flags(flags);
510 static inline void sci_receive_chars(struct sci_port *port)
512 int i, count;
513 struct tty_struct *tty;
514 int copied=0;
515 unsigned short status;
517 status = sci_in(port, SCxSR);
518 if (!(status & SCxSR_RDxF(port)))
519 return;
521 tty = port->gs.tty;
522 while (1) {
523 if (port->type == PORT_SCIF) {
524 count = sci_in(port, SCFDR)&0x001f;
525 } else {
526 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
529 /* Don't copy more bytes than there is room for in the buffer */
530 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
531 count = TTY_FLIPBUF_SIZE - tty->flip.count;
533 /* If for one reason or another, we can't copy more data, we're done! */
534 if (count == 0)
535 break;
537 if (port->type == PORT_SCI) {
538 tty->flip.char_buf_ptr[0] = sci_in(port, SCxRDR);
539 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
540 } else {
541 for (i=0; i<count; i++) {
542 tty->flip.char_buf_ptr[i] = sci_in(port, SCxRDR);
543 status = sci_in(port, SCxSR);
544 if (status&SCxSR_FER(port)) {
545 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
546 dprintk("sci: frame error\n");
547 } else if (status&SCxSR_PER(port)) {
548 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
549 dprintk("sci: parity error\n");
550 } else {
551 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
556 sci_in(port, SCxSR); /* dummy read */
557 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
559 /* Update the kernel buffer end */
560 tty->flip.count += count;
561 tty->flip.char_buf_ptr += count;
562 tty->flip.flag_buf_ptr += count;
564 copied += count;
565 port->icount.rx += count;
568 if (copied)
569 /* Tell the rest of the system the news. New characters! */
570 tty_flip_buffer_push(tty);
573 static inline int sci_handle_errors(struct sci_port *port)
575 int copied = 0;
576 unsigned short status = sci_in(port, SCxSR);
577 struct tty_struct *tty = port->gs.tty;
579 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
580 /* overrun error */
581 copied++;
582 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
583 dprintk("sci: overrun error\n");
586 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
587 if (sci_rxd_in(port) == 0) {
588 /* Notify of BREAK */
589 copied++;
590 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
591 dprintk("sci: BREAK detected\n");
593 else {
594 /* frame error */
595 copied++;
596 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
597 dprintk("sci: frame error\n");
601 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
602 /* parity error */
603 copied++;
604 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
605 dprintk("sci: parity error\n");
608 if (copied) {
609 tty->flip.count += copied;
610 tty_flip_buffer_push(tty);
613 return copied;
616 static inline int sci_handle_breaks(struct sci_port *port)
618 int copied = 0;
619 unsigned short status = sci_in(port, SCxSR);
620 struct tty_struct *tty = port->gs.tty;
622 if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
623 /* Notify of BREAK */
624 copied++;
625 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
626 dprintk("sci: BREAK detected\n");
629 #if defined(CONFIG_CPU_SUBTYPE_SH7750)
630 /* XXX: Handle SCIF overrun error */
631 if (port->type == PORT_SCIF && (ctrl_inw(SCLSR2) & SCIF_ORER) != 0) {
632 ctrl_outw(0, SCLSR2);
633 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
634 copied++;
635 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
636 dprintk("sci: overrun error\n");
639 #endif
641 if (copied) {
642 tty->flip.count += copied;
643 tty_flip_buffer_push(tty);
646 return copied;
649 static void sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
651 struct sci_port *port = ptr;
653 if (port->gs.flags & GS_ACTIVE)
654 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
655 sci_receive_chars(port);
656 return;
658 sci_disable_rx_interrupts(port);
661 static void sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
663 struct sci_port *port = ptr;
665 if (port->gs.flags & GS_ACTIVE)
666 sci_transmit_chars(port);
667 else {
668 sci_disable_tx_interrupts(port);
672 static void sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
674 struct sci_port *port = ptr;
676 /* Handle errors */
677 if (port->type == PORT_SCI) {
678 if(sci_handle_errors(port)) {
679 /* discard character in rx buffer */
680 sci_in(port, SCxSR);
681 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
684 else
685 sci_rx_interrupt(irq, ptr, regs);
687 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
689 /* Kick the transmission */
690 sci_tx_interrupt(irq, ptr, regs);
693 static void sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
695 struct sci_port *port = ptr;
697 /* Handle BREAKs */
698 sci_handle_breaks(port);
699 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
702 static void do_softint(void *private_)
704 struct sci_port *port = (struct sci_port *) private_;
705 struct tty_struct *tty;
707 tty = port->gs.tty;
708 if (!tty)
709 return;
711 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
712 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
713 tty->ldisc.write_wakeup)
714 (tty->ldisc.write_wakeup)(tty);
715 wake_up_interruptible(&tty->write_wait);
719 /* ********************************************************************** *
720 * Here are the routines that actually *
721 * interface with the generic_serial driver *
722 * ********************************************************************** */
724 static void sci_disable_tx_interrupts(void *ptr)
726 struct sci_port *port = ptr;
727 unsigned long flags;
728 unsigned short ctrl;
730 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
731 save_and_cli(flags);
732 ctrl = sci_in(port, SCSCR);
733 ctrl &= ~SCI_CTRL_FLAGS_TIE;
734 sci_out(port, SCSCR, ctrl);
735 restore_flags(flags);
738 static void sci_enable_tx_interrupts(void *ptr)
740 struct sci_port *port = ptr;
742 disable_irq(port->irqs[SCIx_TXI_IRQ]);
743 sci_transmit_chars(port);
744 enable_irq(port->irqs[SCIx_TXI_IRQ]);
747 static void sci_disable_rx_interrupts(void * ptr)
749 struct sci_port *port = ptr;
750 unsigned long flags;
751 unsigned short ctrl;
753 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
754 save_and_cli(flags);
755 ctrl = sci_in(port, SCSCR);
756 ctrl &= ~SCI_CTRL_FLAGS_RIE;
757 sci_out(port, SCSCR, ctrl);
758 restore_flags(flags);
761 static void sci_enable_rx_interrupts(void * ptr)
763 struct sci_port *port = ptr;
764 unsigned long flags;
765 unsigned short ctrl;
767 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
768 save_and_cli(flags);
769 ctrl = sci_in(port, SCSCR);
770 ctrl |= SCI_CTRL_FLAGS_RIE;
771 sci_out(port, SCSCR, ctrl);
772 restore_flags(flags);
775 static int sci_get_CD(void * ptr)
777 /* If you have signal for CD (Carrier Detect), please change here. */
778 return 1;
781 static int sci_chars_in_buffer(void * ptr)
783 struct sci_port *port = ptr;
785 if (port->type == PORT_SCIF) {
786 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
787 } else {
788 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
792 static void sci_shutdown_port(void * ptr)
794 struct sci_port *port = ptr;
796 port->gs.flags &= ~ GS_ACTIVE;
797 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
798 sci_setsignals(port, 0, 0);
801 /* ********************************************************************** *
802 * Here are the routines that actually *
803 * interface with the rest of the system *
804 * ********************************************************************** */
806 static int sci_open(struct tty_struct * tty, struct file * filp)
808 struct sci_port *port;
809 int retval, line;
811 line = MINOR(tty->device) - SCI_MINOR_START;
813 if ((line < 0) || (line >= SCI_NPORTS))
814 return -ENODEV;
816 port = &sci_ports[line];
818 tty->driver_data = port;
819 port->gs.tty = tty;
820 port->gs.count++;
822 port->event = 0;
823 port->tqueue.routine = do_softint;
824 port->tqueue.data = port;
827 * Start up serial port
829 retval = gs_init_port(&port->gs);
830 if (retval) {
831 port->gs.count--;
832 return retval;
835 port->gs.flags |= GS_ACTIVE;
836 sci_setsignals(port, 1,1);
838 if (port->gs.count == 1) {
839 MOD_INC_USE_COUNT;
842 retval = block_til_ready(port, filp);
844 if (retval) {
845 MOD_DEC_USE_COUNT;
846 port->gs.count--;
847 return retval;
850 if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
851 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
852 *tty->termios = port->gs.normal_termios;
853 else
854 *tty->termios = port->gs.callout_termios;
855 sci_set_real_termios(port);
858 #ifdef CONFIG_SERIAL_CONSOLE
859 if (sercons.cflag && sercons.index == line) {
860 tty->termios->c_cflag = sercons.cflag;
861 port->gs.baud = sercons_baud;
862 sercons.cflag = 0;
863 sci_set_real_termios(port);
865 #endif
867 sci_enable_rx_interrupts(port);
869 port->gs.session = current->session;
870 port->gs.pgrp = current->pgrp;
872 return 0;
875 static void sci_hungup(void *ptr)
877 MOD_DEC_USE_COUNT;
880 static void sci_close(void *ptr)
882 MOD_DEC_USE_COUNT;
885 static int sci_ioctl(struct tty_struct * tty, struct file * filp,
886 unsigned int cmd, unsigned long arg)
888 int rc;
889 struct sci_port *port = tty->driver_data;
890 int ival;
892 rc = 0;
893 switch (cmd) {
894 case TIOCGSOFTCAR:
895 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
896 (unsigned int *) arg);
897 break;
898 case TIOCSSOFTCAR:
899 if ((rc = verify_area(VERIFY_READ, (void *) arg,
900 sizeof(int))) == 0) {
901 get_user(ival, (unsigned int *) arg);
902 tty->termios->c_cflag =
903 (tty->termios->c_cflag & ~CLOCAL) |
904 (ival ? CLOCAL : 0);
906 break;
907 case TIOCGSERIAL:
908 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
909 sizeof(struct serial_struct))) == 0)
910 gs_getserial(&port->gs, (struct serial_struct *) arg);
911 break;
912 case TIOCSSERIAL:
913 if ((rc = verify_area(VERIFY_READ, (void *) arg,
914 sizeof(struct serial_struct))) == 0)
915 rc = gs_setserial(&port->gs,
916 (struct serial_struct *) arg);
917 break;
918 case TIOCMGET:
919 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
920 sizeof(unsigned int))) == 0) {
921 ival = sci_getsignals(port);
922 put_user(ival, (unsigned int *) arg);
924 break;
925 case TIOCMBIS:
926 if ((rc = verify_area(VERIFY_READ, (void *) arg,
927 sizeof(unsigned int))) == 0) {
928 get_user(ival, (unsigned int *) arg);
929 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
930 ((ival & TIOCM_RTS) ? 1 : -1));
932 break;
933 case TIOCMBIC:
934 if ((rc = verify_area(VERIFY_READ, (void *) arg,
935 sizeof(unsigned int))) == 0) {
936 get_user(ival, (unsigned int *) arg);
937 sci_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
938 ((ival & TIOCM_RTS) ? 0 : -1));
940 break;
941 case TIOCMSET:
942 if ((rc = verify_area(VERIFY_READ, (void *) arg,
943 sizeof(unsigned int))) == 0) {
944 get_user(ival, (unsigned int *)arg);
945 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
946 ((ival & TIOCM_RTS) ? 1 : 0));
948 break;
950 default:
951 rc = -ENOIOCTLCMD;
952 break;
955 return rc;
958 static void sci_throttle(struct tty_struct * tty)
960 struct sci_port *port = (struct sci_port *)tty->driver_data;
962 /* If the port is using any type of input flow
963 * control then throttle the port.
965 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
966 port->gs.flags |= SCI_RX_THROTTLE;
969 static void sci_unthrottle(struct tty_struct * tty)
971 struct sci_port *port = (struct sci_port *)tty->driver_data;
973 /* Always unthrottle even if flow control is not enabled on
974 * this port in case we disabled flow control while the port
975 * was throttled
977 port->gs.flags &= ~SCI_RX_THROTTLE;
978 return;
981 #ifdef CONFIG_PROC_FS
982 static int sci_read_proc(char *page, char **start, off_t off, int count,
983 int *eof, void *data)
985 int i;
986 struct sci_port *port;
987 int len = 0;
989 len += sprintf(page, "sciinfo:0.1\n");
990 for (i = 0; i < SCI_NPORTS && len < 4000; i++) {
991 port = &sci_ports[i];
992 len += sprintf(page+len, "%d: uart:%s address: %08x", i,
993 (port->type == PORT_SCI) ? "SCI" : "SCIF",
994 port->base);
995 len += sprintf(page+len, " baud:%d", port->gs.baud);
996 len += sprintf(page+len, " tx:%d rx:%d",
997 port->icount.tx, port->icount.rx);
999 if (port->icount.frame)
1000 len += sprintf(page+len, " fe:%d", port->icount.frame);
1001 if (port->icount.parity)
1002 len += sprintf(page+len, " pe:%d", port->icount.parity);
1003 if (port->icount.brk)
1004 len += sprintf(page+len, " brk:%d", port->icount.brk);
1005 if (port->icount.overrun)
1006 len += sprintf(page+len, " oe:%d", port->icount.overrun);
1007 len += sprintf(page+len, "\n");
1009 return len;
1011 #endif
1013 /* ********************************************************************** *
1014 * Here are the initialization routines. *
1015 * ********************************************************************** */
1017 static int sci_init_drivers(void)
1019 int error;
1020 struct sci_port *port;
1022 memset(&sci_driver, 0, sizeof(sci_driver));
1023 sci_driver.magic = TTY_DRIVER_MAGIC;
1024 sci_driver.driver_name = "sci";
1025 sci_driver.name = "ttySC";
1026 sci_driver.major = SCI_MAJOR;
1027 sci_driver.minor_start = SCI_MINOR_START;
1028 sci_driver.num = SCI_NPORTS;
1029 sci_driver.type = TTY_DRIVER_TYPE_SERIAL;
1030 sci_driver.subtype = SERIAL_TYPE_NORMAL;
1031 sci_driver.init_termios = tty_std_termios;
1032 sci_driver.init_termios.c_cflag =
1033 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1034 sci_driver.flags = TTY_DRIVER_REAL_RAW;
1035 sci_driver.refcount = &sci_refcount;
1036 sci_driver.table = sci_table;
1037 sci_driver.termios = sci_termios;
1038 sci_driver.termios_locked = sci_termios_locked;
1040 sci_driver.open = sci_open;
1041 sci_driver.close = gs_close;
1042 sci_driver.write = gs_write;
1043 sci_driver.put_char = gs_put_char;
1044 sci_driver.flush_chars = gs_flush_chars;
1045 sci_driver.write_room = gs_write_room;
1046 sci_driver.chars_in_buffer = gs_chars_in_buffer;
1047 sci_driver.flush_buffer = gs_flush_buffer;
1048 sci_driver.ioctl = sci_ioctl;
1049 sci_driver.throttle = sci_throttle;
1050 sci_driver.unthrottle = sci_unthrottle;
1051 sci_driver.set_termios = gs_set_termios;
1052 sci_driver.stop = gs_stop;
1053 sci_driver.start = gs_start;
1054 sci_driver.hangup = gs_hangup;
1055 #ifdef CONFIG_PROC_FS
1056 sci_driver.read_proc = sci_read_proc;
1057 #endif
1059 sci_callout_driver = sci_driver;
1060 sci_callout_driver.name = "cusc";
1061 sci_callout_driver.major = SCI_MAJOR+1;
1062 sci_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1063 sci_callout_driver.read_proc = NULL;
1065 if ((error = tty_register_driver(&sci_driver))) {
1066 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1067 error);
1068 return 1;
1070 if ((error = tty_register_driver(&sci_callout_driver))) {
1071 tty_unregister_driver(&sci_driver);
1072 printk(KERN_ERR "sci: Couldn't register SCI callout driver, error = %d\n",
1073 error);
1074 return 1;
1077 for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1078 port->gs.callout_termios = sci_callout_driver.init_termios;
1079 port->gs.normal_termios = sci_driver.init_termios;
1080 port->gs.magic = SCI_MAGIC;
1081 port->gs.close_delay = HZ/2;
1082 port->gs.closing_wait = 30 * HZ;
1083 port->gs.rd = &sci_real_driver;
1084 init_waitqueue_head(&port->gs.open_wait);
1085 init_waitqueue_head(&port->gs.close_wait);
1086 port->old_cflag = 0;
1087 port->icount.cts = port->icount.dsr =
1088 port->icount.rng = port->icount.dcd = 0;
1089 port->icount.rx = port->icount.tx = 0;
1090 port->icount.frame = port->icount.parity = 0;
1091 port->icount.overrun = port->icount.brk = 0;
1094 return 0;
1097 int __init sci_init(void)
1099 struct sci_port *port;
1100 int i, j;
1101 void (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
1102 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1103 sci_br_interrupt,
1106 printk("SuperH SCI(F) driver initialized\n");
1108 for (j=0; j<SCI_NPORTS; j++) {
1109 port = &sci_ports[j];
1110 printk("ttySC%d at 0x%08x is a %s\n", j, port->base,
1111 (port->type == PORT_SCI) ? "SCI" : "SCIF");
1112 for (i=0; i<4; i++) {
1113 if (!port->irqs[i]) continue;
1114 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1115 "sci", port)) {
1116 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1117 return -ENODEV;
1122 sci_init_drivers();
1124 #ifdef CONFIG_DEBUG_KERNEL_WITH_GDB_STUB
1125 sh_bios_gdb_detach();
1126 #endif
1127 return 0; /* Return -EIO when not detected */
1130 module_init(sci_init);
1132 #ifdef MODULE
1133 #undef func_enter
1134 #undef func_exit
1136 void cleanup_module(void)
1138 int i;
1140 for (i=SCI_ERI_IRQ; i<SCI_TEI_IRQ; i++) /* XXX: irq_end?? */
1141 free_irq(i, port);
1143 tty_unregister_driver(&sci_driver);
1144 tty_unregister_driver(&sci_callout_driver);
1147 #include "generic_serial.c"
1148 #endif
1150 #ifdef CONFIG_SERIAL_CONSOLE
1152 * Print a string to the serial port trying not to disturb
1153 * any possible real use of the port...
1155 static void serial_console_write(struct console *co, const char *s,
1156 unsigned count)
1158 put_string(sercons_port, s, count);
1162 * Receive character from the serial port
1164 static int serial_console_wait_key(struct console *co)
1166 /* Not implemented yet */
1167 return 0;
1170 static kdev_t serial_console_device(struct console *c)
1172 return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1176 * Setup initial baud/bits/parity. We do two things here:
1177 * - construct a cflag setting for the first rs_open()
1178 * - initialize the serial port
1179 * Return non-zero if we didn't find a serial port.
1181 static int __init serial_console_setup(struct console *co, char *options)
1183 int baud = 9600;
1184 int bits = 8;
1185 int parity = 'n';
1186 int cflag = CREAD | HUPCL | CLOCAL;
1187 char *s;
1189 sercons_port = &sci_ports[co->index];
1191 if (options) {
1192 baud = simple_strtoul(options, NULL, 10);
1193 s = options;
1194 while(*s >= '0' && *s <= '9')
1195 s++;
1196 if (*s) parity = *s++;
1197 if (*s) bits = *s - '0';
1201 * Now construct a cflag setting.
1203 switch (baud) {
1204 case 19200:
1205 cflag |= B19200;
1206 break;
1207 case 38400:
1208 cflag |= B38400;
1209 break;
1210 case 57600:
1211 cflag |= B57600;
1212 break;
1213 case 115200:
1214 cflag |= B115200;
1215 break;
1216 case 9600:
1217 default:
1218 cflag |= B9600;
1219 baud = 9600;
1220 break;
1222 switch (bits) {
1223 case 7:
1224 cflag |= CS7;
1225 break;
1226 default:
1227 case 8:
1228 cflag |= CS8;
1229 break;
1231 switch (parity) {
1232 case 'o': case 'O':
1233 cflag |= PARODD;
1234 break;
1235 case 'e': case 'E':
1236 cflag |= PARENB;
1237 break;
1240 co->cflag = cflag;
1241 sercons_baud = baud;
1243 sci_set_termios_cflag(sercons_port, cflag, baud);
1244 sercons_port->old_cflag = cflag;
1246 return 0;
1249 static struct console sercons = {
1250 name: "ttySC",
1251 write: serial_console_write,
1252 device: serial_console_device,
1253 wait_key: serial_console_wait_key,
1254 setup: serial_console_setup,
1255 flags: CON_PRINTBUFFER,
1256 index: -1,
1260 * Register console.
1263 #ifdef CONFIG_SH_EARLY_PRINTK
1264 extern void sh_console_unregister (void);
1265 #endif
1267 void __init sci_console_init(void)
1269 register_console(&sercons);
1270 #ifdef CONFIG_SH_EARLY_PRINTK
1271 /* Now that the real console is available, unregister the one we
1272 * used while first booting.
1274 sh_console_unregister();
1275 #endif
1277 #endif /* CONFIG_SERIAL_CONSOLE */