Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / sh-sci.c
blob27eb8544b6aa6ce080339c28328b89ce418015e1
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/slab.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_SH_STANDARD_BIOS
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 int sci_chars_in_buffer(void *ptr);
75 static int sci_request_irq(struct sci_port *port);
76 static void sci_free_irq(struct sci_port *port);
77 static int sci_init_drivers(void);
79 static struct tty_driver *sci_driver;
81 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
83 static int sci_debug = 0;
85 #ifdef MODULE
86 MODULE_PARM(sci_debug, "i");
87 #endif
89 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
91 #ifdef CONFIG_SERIAL_CONSOLE
92 static void put_char(struct sci_port *port, char c)
94 unsigned long flags;
95 unsigned short status;
97 save_and_cli(flags);
100 status = sci_in(port, SCxSR);
101 while (!(status & SCxSR_TDxE(port)));
103 sci_out(port, SCxTDR, c);
104 sci_in(port, SCxSR); /* Dummy read */
105 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
107 restore_flags(flags);
109 #endif
111 #ifdef CONFIG_SH_STANDARD_BIOS
113 static void handle_error(struct sci_port *port)
114 { /* Clear error flags */
115 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
118 static int get_char(struct sci_port *port)
120 unsigned long flags;
121 unsigned short status;
122 int c;
124 save_and_cli(flags);
125 do {
126 status = sci_in(port, SCxSR);
127 if (status & SCxSR_ERRORS(port)) {
128 handle_error(port);
129 continue;
131 } while (!(status & SCxSR_RDxF(port)));
132 c = sci_in(port, SCxRDR);
133 sci_in(port, SCxSR); /* Dummy read */
134 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
135 restore_flags(flags);
137 return c;
140 /* Taken from sh-stub.c of GDB 4.18 */
141 static const char hexchars[] = "0123456789abcdef";
143 static __inline__ char highhex(int x)
145 return hexchars[(x >> 4) & 0xf];
148 static __inline__ char lowhex(int x)
150 return hexchars[x & 0xf];
153 #endif
156 * Send the packet in buffer. The host gets one chance to read it.
157 * This routine does not wait for a positive acknowledge.
160 #ifdef CONFIG_SERIAL_CONSOLE
161 static void put_string(struct sci_port *port, const char *buffer, int count)
163 int i;
164 const unsigned char *p = buffer;
165 #ifdef CONFIG_SH_STANDARD_BIOS
166 int checksum;
168 /* This call only does a trap the first time it is
169 * called, and so is safe to do here unconditionally
171 if (sh_bios_in_gdb_mode()) {
172 /* $<packet info>#<checksum>. */
173 do {
174 unsigned char c;
175 put_char(port, '$');
176 put_char(port, 'O'); /* 'O'utput to console */
177 checksum = 'O';
179 for (i=0; i<count; i++) { /* Don't use run length encoding */
180 int h, l;
182 c = *p++;
183 h = highhex(c);
184 l = lowhex(c);
185 put_char(port, h);
186 put_char(port, l);
187 checksum += h + l;
189 put_char(port, '#');
190 put_char(port, highhex(checksum));
191 put_char(port, lowhex(checksum));
192 } while (get_char(port) != '+');
193 } else
194 #endif
195 for (i=0; i<count; i++) {
196 if (*p == 10)
197 put_char(port, '\r');
198 put_char(port, *p++);
201 #endif
204 static struct real_driver sci_real_driver = {
205 sci_disable_tx_interrupts,
206 sci_enable_tx_interrupts,
207 sci_disable_rx_interrupts,
208 sci_enable_rx_interrupts,
209 sci_get_CD,
210 sci_shutdown_port,
211 sci_set_real_termios,
212 sci_chars_in_buffer,
213 NULL
216 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
217 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
220 #endif
222 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
223 #if defined(__sh3__)
224 /* For SH7707, SH7709, SH7709A, SH7729 */
225 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
227 unsigned int fcr_val = 0;
230 unsigned short data;
232 /* We need to set SCPCR to enable RTS/CTS */
233 data = ctrl_inw(SCPCR);
234 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
235 ctrl_outw(data&0x0fcf, SCPCR);
237 if (cflag & CRTSCTS)
238 fcr_val |= SCFCR_MCE;
239 else {
240 unsigned short data;
242 /* We need to set SCPCR to enable RTS/CTS */
243 data = ctrl_inw(SCPCR);
244 /* Clear out SCP7MD1,0, SCP4MD1,0,
245 Set SCP6MD1,0 = {01} (output) */
246 ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
248 data = ctrl_inb(SCPDR);
249 /* Set /RTS2 (bit6) = 0 */
250 ctrl_outb(data&0xbf, SCPDR);
252 sci_out(port, SCFCR, fcr_val);
255 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
257 unsigned int fcr_val = 0;
259 if (cflag & CRTSCTS)
260 fcr_val |= SCFCR_MCE;
262 sci_out(port, SCFCR, fcr_val);
265 #else
267 /* For SH7750 */
268 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
270 unsigned int fcr_val = 0;
272 if (cflag & CRTSCTS) {
273 fcr_val |= SCFCR_MCE;
274 } else {
275 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
277 sci_out(port, SCFCR, fcr_val);
280 #endif
281 #endif /* SCIF_ONLY || SCI_AND_SCIF */
283 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
285 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
286 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
287 /* If you have signals for DTR and DCD, please implement here. */
291 static int sci_getsignals(struct sci_port *port)
293 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
294 and CTS/RTS */
296 return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
298 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
299 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
300 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
301 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
302 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
303 ((i_stat & IP_RI) ?TIOCM_RNG:0)
307 static void sci_set_baud(struct sci_port *port, int baud)
309 int t;
311 switch (baud) {
312 case 0:
313 t = -1;
314 break;
315 case 2400:
316 t = BPS_2400;
317 break;
318 case 4800:
319 t = BPS_4800;
320 break;
321 case 9600:
322 t = BPS_9600;
323 break;
324 case 19200:
325 t = BPS_19200;
326 break;
327 case 38400:
328 t = BPS_38400;
329 break;
330 case 57600:
331 t = BPS_57600;
332 break;
333 default:
334 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
335 case 115200:
336 t = BPS_115200;
337 break;
340 if (t > 0) {
341 sci_setsignals (port, 1, -1);
342 if(t >= 256) {
343 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
344 t >>= 2;
345 } else {
346 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
348 sci_out(port, SCBRR, t);
349 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
350 } else {
351 sci_setsignals (port, 0, -1);
355 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
357 unsigned int status;
358 unsigned int smr_val;
361 status = sci_in(port, SCxSR);
362 while (!(status & SCxSR_TEND(port)));
364 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
366 if (port->type == PORT_SCIF) {
367 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
370 smr_val = sci_in(port, SCSMR) & 3;
371 if ((cflag & CSIZE) == CS7)
372 smr_val |= 0x40;
373 if (cflag & PARENB)
374 smr_val |= 0x20;
375 if (cflag & PARODD)
376 smr_val |= 0x10;
377 if (cflag & CSTOPB)
378 smr_val |= 0x08;
379 sci_out(port, SCSMR, smr_val);
380 sci_set_baud(port, baud);
382 port->init_pins(port, cflag);
383 sci_out(port, SCSCR, SCSCR_INIT(port));
386 static int sci_set_real_termios(void *ptr)
388 struct sci_port *port = ptr;
390 if (port->old_cflag != port->gs.tty->termios->c_cflag) {
391 port->old_cflag = port->gs.tty->termios->c_cflag;
392 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
393 sci_enable_rx_interrupts(port);
396 /* Tell line discipline whether we will do input cooking */
397 if (I_OTHER(port->gs.tty))
398 clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
399 else
400 set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
402 /* Tell line discipline whether we will do output cooking.
403 * If OPOST is set and no other output flags are set then we can do output
404 * processing. Even if only *one* other flag in the O_OTHER group is set
405 * we do cooking in software.
407 if (O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty))
408 set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
409 else
410 clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
412 return 0;
415 /* ********************************************************************** *
416 * the interrupt related routines *
417 * ********************************************************************** */
420 * This routine is used by the interrupt handler to schedule
421 * processing in the software interrupt portion of the driver.
423 static inline void sci_sched_event(struct sci_port *port, int event)
425 port->event |= 1 << event;
426 schedule_work(&port->tqueue);
429 static void sci_transmit_chars(struct sci_port *port)
431 int count, i;
432 int txroom;
433 unsigned long flags;
434 unsigned short status;
435 unsigned short ctrl;
436 unsigned char c;
438 status = sci_in(port, SCxSR);
439 if (!(status & SCxSR_TDxE(port))) {
440 save_and_cli(flags);
441 ctrl = sci_in(port, SCSCR);
442 if (port->gs.xmit_cnt == 0) {
443 ctrl &= ~SCI_CTRL_FLAGS_TIE;
444 port->gs.flags &= ~GS_TX_INTEN;
445 } else
446 ctrl |= SCI_CTRL_FLAGS_TIE;
447 sci_out(port, SCSCR, ctrl);
448 restore_flags(flags);
449 return;
452 while (1) {
453 count = port->gs.xmit_cnt;
454 if (port->type == PORT_SCIF) {
455 txroom = 16 - (sci_in(port, SCFDR)>>8);
456 } else {
457 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
459 if (count > txroom)
460 count = txroom;
462 /* Don't copy pas the end of the source buffer */
463 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
464 count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
466 /* If for one reason or another, we can't copy more data, we're done! */
467 if (count == 0)
468 break;
470 for (i=0; i<count; i++) {
471 c = port->gs.xmit_buf[port->gs.xmit_tail + i];
472 sci_out(port, SCxTDR, c);
474 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
476 port->icount.tx += count;
478 /* Update the kernel buffer end */
479 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
481 /* This one last. (this is essential)
482 It would allow others to start putting more data into the buffer! */
483 port->gs.xmit_cnt -= count;
486 if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
487 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
489 save_and_cli(flags);
490 ctrl = sci_in(port, SCSCR);
491 if (port->gs.xmit_cnt == 0) {
492 ctrl &= ~SCI_CTRL_FLAGS_TIE;
493 port->gs.flags &= ~GS_TX_INTEN;
494 } else {
495 if (port->type == PORT_SCIF) {
496 sci_in(port, SCxSR); /* Dummy read */
497 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
499 ctrl |= SCI_CTRL_FLAGS_TIE;
501 sci_out(port, SCSCR, ctrl);
502 restore_flags(flags);
505 static inline void sci_receive_chars(struct sci_port *port)
507 int i, count;
508 struct tty_struct *tty;
509 int copied=0;
510 unsigned short status;
512 status = sci_in(port, SCxSR);
513 if (!(status & SCxSR_RDxF(port)))
514 return;
516 tty = port->gs.tty;
517 while (1) {
518 if (port->type == PORT_SCIF) {
519 count = sci_in(port, SCFDR)&0x001f;
520 } else {
521 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
524 /* Don't copy more bytes than there is room for in the buffer */
525 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
526 count = TTY_FLIPBUF_SIZE - tty->flip.count;
528 /* If for one reason or another, we can't copy more data, we're done! */
529 if (count == 0)
530 break;
532 if (port->type == PORT_SCI) {
533 tty->flip.char_buf_ptr[0] = sci_in(port, SCxRDR);
534 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
535 } else {
536 for (i=0; i<count; i++) {
537 tty->flip.char_buf_ptr[i] = sci_in(port, SCxRDR);
538 status = sci_in(port, SCxSR);
539 if (status&SCxSR_FER(port)) {
540 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
541 dprintk("sci: frame error\n");
542 } else if (status&SCxSR_PER(port)) {
543 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
544 dprintk("sci: parity error\n");
545 } else {
546 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
551 sci_in(port, SCxSR); /* dummy read */
552 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
554 /* Update the kernel buffer end */
555 tty->flip.count += count;
556 tty->flip.char_buf_ptr += count;
557 tty->flip.flag_buf_ptr += count;
559 copied += count;
560 port->icount.rx += count;
563 if (copied)
564 /* Tell the rest of the system the news. New characters! */
565 tty_flip_buffer_push(tty);
568 static inline int sci_handle_errors(struct sci_port *port)
570 int copied = 0;
571 unsigned short status = sci_in(port, SCxSR);
572 struct tty_struct *tty = port->gs.tty;
574 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
575 /* overrun error */
576 copied++;
577 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
578 dprintk("sci: overrun error\n");
581 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
582 if (sci_rxd_in(port) == 0) {
583 /* Notify of BREAK */
584 copied++;
585 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
586 dprintk("sci: BREAK detected\n");
588 else {
589 /* frame error */
590 copied++;
591 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
592 dprintk("sci: frame error\n");
596 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
597 /* parity error */
598 copied++;
599 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
600 dprintk("sci: parity error\n");
603 if (copied) {
604 tty->flip.count += copied;
605 tty_flip_buffer_push(tty);
608 return copied;
611 static inline int sci_handle_breaks(struct sci_port *port)
613 int copied = 0;
614 unsigned short status = sci_in(port, SCxSR);
615 struct tty_struct *tty = port->gs.tty;
617 if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
618 /* Notify of BREAK */
619 copied++;
620 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
621 dprintk("sci: BREAK detected\n");
624 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1)
625 /* XXX: Handle SCIF overrun error */
626 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
627 sci_out(port, SCLSR, 0);
628 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
629 copied++;
630 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
631 dprintk("sci: overrun error\n");
634 #endif
636 if (copied) {
637 tty->flip.count += copied;
638 tty_flip_buffer_push(tty);
641 return copied;
644 static void sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
646 struct sci_port *port = ptr;
648 if (port->gs.flags & GS_ACTIVE)
649 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
650 sci_receive_chars(port);
651 return;
653 sci_disable_rx_interrupts(port);
656 static void sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
658 struct sci_port *port = ptr;
660 if (port->gs.flags & GS_ACTIVE)
661 sci_transmit_chars(port);
662 else {
663 sci_disable_tx_interrupts(port);
667 static void sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
669 struct sci_port *port = ptr;
671 /* Handle errors */
672 if (port->type == PORT_SCI) {
673 if(sci_handle_errors(port)) {
674 /* discard character in rx buffer */
675 sci_in(port, SCxSR);
676 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
679 else
680 sci_rx_interrupt(irq, ptr, regs);
682 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
684 /* Kick the transmission */
685 sci_tx_interrupt(irq, ptr, regs);
688 static void sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
690 struct sci_port *port = ptr;
692 /* Handle BREAKs */
693 sci_handle_breaks(port);
694 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
697 static void do_softint(void *private_)
699 struct sci_port *port = (struct sci_port *) private_;
700 struct tty_struct *tty;
702 tty = port->gs.tty;
703 if (!tty)
704 return;
706 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
707 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
708 tty->ldisc.write_wakeup)
709 (tty->ldisc.write_wakeup)(tty);
710 wake_up_interruptible(&tty->write_wait);
714 /* ********************************************************************** *
715 * Here are the routines that actually *
716 * interface with the generic_serial driver *
717 * ********************************************************************** */
719 static void sci_disable_tx_interrupts(void *ptr)
721 struct sci_port *port = ptr;
722 unsigned long flags;
723 unsigned short ctrl;
725 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
726 save_and_cli(flags);
727 ctrl = sci_in(port, SCSCR);
728 ctrl &= ~SCI_CTRL_FLAGS_TIE;
729 sci_out(port, SCSCR, ctrl);
730 restore_flags(flags);
733 static void sci_enable_tx_interrupts(void *ptr)
735 struct sci_port *port = ptr;
737 disable_irq(port->irqs[SCIx_TXI_IRQ]);
738 sci_transmit_chars(port);
739 enable_irq(port->irqs[SCIx_TXI_IRQ]);
742 static void sci_disable_rx_interrupts(void * ptr)
744 struct sci_port *port = ptr;
745 unsigned long flags;
746 unsigned short ctrl;
748 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
749 save_and_cli(flags);
750 ctrl = sci_in(port, SCSCR);
751 ctrl &= ~SCI_CTRL_FLAGS_RIE;
752 sci_out(port, SCSCR, ctrl);
753 restore_flags(flags);
756 static void sci_enable_rx_interrupts(void * ptr)
758 struct sci_port *port = ptr;
759 unsigned long flags;
760 unsigned short ctrl;
762 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
763 save_and_cli(flags);
764 ctrl = sci_in(port, SCSCR);
765 ctrl |= SCI_CTRL_FLAGS_RIE;
766 sci_out(port, SCSCR, ctrl);
767 restore_flags(flags);
770 static int sci_get_CD(void * ptr)
772 /* If you have signal for CD (Carrier Detect), please change here. */
773 return 1;
776 static int sci_chars_in_buffer(void * ptr)
778 struct sci_port *port = ptr;
780 if (port->type == PORT_SCIF) {
781 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
782 } else {
783 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
787 static void sci_shutdown_port(void * ptr)
789 struct sci_port *port = ptr;
791 port->gs.flags &= ~ GS_ACTIVE;
792 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
793 sci_setsignals(port, 0, 0);
794 sci_free_irq(port);
797 /* ********************************************************************** *
798 * Here are the routines that actually *
799 * interface with the rest of the system *
800 * ********************************************************************** */
802 static int sci_open(struct tty_struct * tty, struct file * filp)
804 struct sci_port *port;
805 int retval, line;
807 line = tty->index;
809 if ((line < 0) || (line >= SCI_NPORTS))
810 return -ENODEV;
812 port = &sci_ports[line];
814 tty->driver_data = port;
815 port->gs.tty = tty;
816 port->gs.count++;
818 port->event = 0;
819 INIT_WORK(&port->tqueue, do_softint, port);
822 * Start up serial port
824 retval = gs_init_port(&port->gs);
825 if (retval) {
826 goto failed_1;
829 port->gs.flags |= GS_ACTIVE;
830 sci_setsignals(port, 1,1);
832 if (port->gs.count == 1) {
833 retval = sci_request_irq(port);
836 retval = gs_block_til_ready(port, filp);
838 if (retval) {
839 goto failed_3;
842 #ifdef CONFIG_SERIAL_CONSOLE
843 if (sercons.cflag && sercons.index == line) {
844 tty->termios->c_cflag = sercons.cflag;
845 port->gs.baud = sercons_baud;
846 sercons.cflag = 0;
847 sci_set_real_termios(port);
849 #endif
851 sci_enable_rx_interrupts(port);
853 return 0;
855 failed_3:
856 sci_free_irq(port);
857 failed_1:
858 port->gs.count--;
859 return retval;
862 static int sci_ioctl(struct tty_struct * tty, struct file * filp,
863 unsigned int cmd, unsigned long arg)
865 int rc;
866 struct sci_port *port = tty->driver_data;
867 int ival;
869 rc = 0;
870 switch (cmd) {
871 case TIOCGSOFTCAR:
872 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
873 (unsigned int *) arg);
874 break;
875 case TIOCSSOFTCAR:
876 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
877 tty->termios->c_cflag =
878 (tty->termios->c_cflag & ~CLOCAL) |
879 (ival ? CLOCAL : 0);
880 break;
881 case TIOCGSERIAL:
882 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
883 sizeof(struct serial_struct))) == 0)
884 rc = gs_getserial(&port->gs, (struct serial_struct *) arg);
885 break;
886 case TIOCSSERIAL:
887 if ((rc = verify_area(VERIFY_READ, (void *) arg,
888 sizeof(struct serial_struct))) == 0)
889 rc = gs_setserial(&port->gs,
890 (struct serial_struct *) arg);
891 break;
892 case TIOCMGET:
893 ival = sci_getsignals(port);
894 rc = put_user(ival, (unsigned int *) arg);
895 break;
896 case TIOCMBIS:
897 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
898 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
899 ((ival & TIOCM_RTS) ? 1 : -1));
900 break;
901 case TIOCMBIC:
902 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
903 sci_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
904 ((ival & TIOCM_RTS) ? 0 : -1));
905 break;
906 case TIOCMSET:
907 if ((rc = get_user(ival, (unsigned int *)arg)) == 0)
908 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
909 ((ival & TIOCM_RTS) ? 1 : 0));
910 break;
912 default:
913 rc = -ENOIOCTLCMD;
914 break;
917 return rc;
920 static void sci_throttle(struct tty_struct * tty)
922 struct sci_port *port = (struct sci_port *)tty->driver_data;
924 /* If the port is using any type of input flow
925 * control then throttle the port.
927 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
928 port->gs.flags |= SCI_RX_THROTTLE;
931 static void sci_unthrottle(struct tty_struct * tty)
933 struct sci_port *port = (struct sci_port *)tty->driver_data;
935 /* Always unthrottle even if flow control is not enabled on
936 * this port in case we disabled flow control while the port
937 * was throttled
939 port->gs.flags &= ~SCI_RX_THROTTLE;
940 return;
943 #ifdef CONFIG_PROC_FS
944 static int sci_read_proc(char *page, char **start, off_t off, int count,
945 int *eof, void *data)
947 int i;
948 struct sci_port *port;
949 int len = 0;
951 len += sprintf(page, "sciinfo:0.1\n");
952 for (i = 0; i < SCI_NPORTS && len < 4000; i++) {
953 port = &sci_ports[i];
954 len += sprintf(page+len, "%d: uart:%s address: %08x", i,
955 (port->type == PORT_SCI) ? "SCI" : "SCIF",
956 port->base);
957 len += sprintf(page+len, " baud:%d", port->gs.baud);
958 len += sprintf(page+len, " tx:%d rx:%d",
959 port->icount.tx, port->icount.rx);
961 if (port->icount.frame)
962 len += sprintf(page+len, " fe:%d", port->icount.frame);
963 if (port->icount.parity)
964 len += sprintf(page+len, " pe:%d", port->icount.parity);
965 if (port->icount.brk)
966 len += sprintf(page+len, " brk:%d", port->icount.brk);
967 if (port->icount.overrun)
968 len += sprintf(page+len, " oe:%d", port->icount.overrun);
969 len += sprintf(page+len, "\n");
971 return len;
973 #endif
975 static struct tty_operations sci_ops = {
976 .open = sci_open,
977 .close = gs_close,
978 .write = gs_write,
979 .put_char = gs_put_char,
980 .flush_chars = gs_flush_chars,
981 .write_room = gs_write_room,
982 .chars_in_buffer = gs_chars_in_buffer,
983 .flush_buffer = gs_flush_buffer,
984 .ioctl = sci_ioctl,
985 .throttle = sci_throttle,
986 .unthrottle = sci_unthrottle,
987 .set_termios = gs_set_termios,
988 .stop = gs_stop,
989 .start = gs_start,
990 .hangup = gs_hangup,
991 #ifdef CONFIG_PROC_FS
992 .read_proc = sci_read_proc,
993 #endif
996 /* ********************************************************************** *
997 * Here are the initialization routines. *
998 * ********************************************************************** */
1000 static int sci_init_drivers(void)
1002 int error;
1003 struct sci_port *port;
1004 sci_driver = alloc_tty_driver(SCI_NPORTS);
1005 if (!sci_driver)
1006 return -ENOMEM;
1008 sci_driver->owner = THIS_MODULE;
1009 sci_driver->driver_name = "sci";
1010 sci_driver->name = "ttySC";
1011 sci_driver->devfs_name = "ttsc/";
1012 sci_driver->major = SCI_MAJOR;
1013 sci_driver->minor_start = SCI_MINOR_START;
1014 sci_driver->type = TTY_DRIVER_TYPE_SERIAL;
1015 sci_driver->subtype = SERIAL_TYPE_NORMAL;
1016 sci_driver->init_termios = tty_std_termios;
1017 sci_driver->init_termios.c_cflag =
1018 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1019 sci_driver->flags = TTY_DRIVER_REAL_RAW;
1020 tty_set_operations(sci_driver, &sci_ops);
1021 if ((error = tty_register_driver(sci_driver))) {
1022 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1023 error);
1024 put_tty_driver(sci_driver);
1025 return 1;
1028 for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1029 port->gs.magic = SCI_MAGIC;
1030 port->gs.close_delay = HZ/2;
1031 port->gs.closing_wait = 30 * HZ;
1032 port->gs.rd = &sci_real_driver;
1033 init_waitqueue_head(&port->gs.open_wait);
1034 init_waitqueue_head(&port->gs.close_wait);
1035 port->old_cflag = 0;
1036 port->icount.cts = port->icount.dsr =
1037 port->icount.rng = port->icount.dcd = 0;
1038 port->icount.rx = port->icount.tx = 0;
1039 port->icount.frame = port->icount.parity = 0;
1040 port->icount.overrun = port->icount.brk = 0;
1043 return 0;
1046 static int sci_request_irq(struct sci_port *port)
1048 int i;
1049 void (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
1050 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1051 sci_br_interrupt,
1054 for (i=0; i<4; i++) {
1055 if (!port->irqs[i]) continue;
1056 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1057 "sci", port)) {
1058 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1059 return -ENODEV;
1062 return 0;
1065 static void sci_free_irq(struct sci_port *port)
1067 int i;
1069 for (i=0; i<4; i++) {
1070 if (!port->irqs[i]) continue;
1071 free_irq(port->irqs[i], port);
1075 static char banner[] __initdata =
1076 KERN_INFO "SuperH SCI(F) driver initialized\n";
1078 int __init sci_init(void)
1080 struct sci_port *port;
1081 int j;
1083 printk("%s", banner);
1085 for (j=0; j<SCI_NPORTS; j++) {
1086 port = &sci_ports[j];
1087 printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
1088 (port->type == PORT_SCI) ? "SCI" : "SCIF");
1091 sci_init_drivers();
1093 #ifdef CONFIG_SH_STANDARD_BIOS
1094 sh_bios_gdb_detach();
1095 #endif
1096 return 0; /* Return -EIO when not detected */
1099 module_init(sci_init);
1101 #ifdef MODULE
1102 #undef func_enter
1103 #undef func_exit
1105 void cleanup_module(void)
1107 tty_unregister_driver(sci_driver);
1108 put_tty_driver(sci_driver);
1111 #include "generic_serial.c"
1112 #endif
1114 #ifdef CONFIG_SERIAL_CONSOLE
1116 * Print a string to the serial port trying not to disturb
1117 * any possible real use of the port...
1119 static void serial_console_write(struct console *co, const char *s,
1120 unsigned count)
1122 put_string(sercons_port, s, count);
1125 static struct tty_driver *serial_console_device(struct console *c, int *index)
1127 *index = c->index;
1128 return sci_driver;
1132 * Setup initial baud/bits/parity. We do two things here:
1133 * - construct a cflag setting for the first rs_open()
1134 * - initialize the serial port
1135 * Return non-zero if we didn't find a serial port.
1137 static int __init serial_console_setup(struct console *co, char *options)
1139 int baud = 9600;
1140 int bits = 8;
1141 int parity = 'n';
1142 int cflag = CREAD | HUPCL | CLOCAL;
1143 char *s;
1145 sercons_port = &sci_ports[co->index];
1147 if (options) {
1148 baud = simple_strtoul(options, NULL, 10);
1149 s = options;
1150 while(*s >= '0' && *s <= '9')
1151 s++;
1152 if (*s) parity = *s++;
1153 if (*s) bits = *s - '0';
1157 * Now construct a cflag setting.
1159 switch (baud) {
1160 case 19200:
1161 cflag |= B19200;
1162 break;
1163 case 38400:
1164 cflag |= B38400;
1165 break;
1166 case 57600:
1167 cflag |= B57600;
1168 break;
1169 case 115200:
1170 cflag |= B115200;
1171 break;
1172 case 9600:
1173 default:
1174 cflag |= B9600;
1175 baud = 9600;
1176 break;
1178 switch (bits) {
1179 case 7:
1180 cflag |= CS7;
1181 break;
1182 default:
1183 case 8:
1184 cflag |= CS8;
1185 break;
1187 switch (parity) {
1188 case 'o': case 'O':
1189 cflag |= PARODD;
1190 break;
1191 case 'e': case 'E':
1192 cflag |= PARENB;
1193 break;
1196 co->cflag = cflag;
1197 sercons_baud = baud;
1199 sci_set_termios_cflag(sercons_port, cflag, baud);
1200 sercons_port->old_cflag = cflag;
1202 return 0;
1205 static struct console sercons = {
1206 .name = "ttySC",
1207 .write = serial_console_write,
1208 .device = serial_console_device,
1209 .setup = serial_console_setup,
1210 .flags = CON_PRINTBUFFER,
1211 .index = -1,
1215 * Register console.
1218 #ifdef CONFIG_SH_EARLY_PRINTK
1219 extern void sh_console_unregister (void);
1220 #endif
1222 static int __init sci_console_init(void)
1224 register_console(&sercons);
1225 #ifdef CONFIG_SH_EARLY_PRINTK
1226 /* Now that the real console is available, unregister the one we
1227 * used while first booting.
1229 sh_console_unregister();
1230 #endif
1231 return 0;
1233 console_initcall(sci_console_init);
1235 #endif /* CONFIG_SERIAL_CONSOLE */