File: /usr/src/linux/drivers/char/sh-sci.c
1 /* $Id: sh-sci.c,v 1.40 2000/04/15 06:57:29 gniibe Exp $
2 *
3 * linux/drivers/char/sh-sci.c
4 *
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 *
10 * TTY code is based on sx.c (Specialix SX driver) by:
11 *
12 * (C) 1998 R.E.Wolff@BitWizard.nl
13 *
14 */
15
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
38
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>
44
45 #include <linux/generic_serial.h>
46
47 #ifdef CONFIG_SH_STANDARD_BIOS
48 #include <asm/sh_bios.h>
49 #endif
50
51 #include "sh-sci.h"
52
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
58
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_request_irq(struct sci_port *port);
78 static void sci_free_irq(struct sci_port *port);
79 static int sci_init_drivers(void);
80
81 static struct tty_driver sci_driver, sci_callout_driver;
82
83 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
84 static struct tty_struct *sci_table[SCI_NPORTS] = { NULL, };
85 static struct termios *sci_termios[SCI_NPORTS];
86 static struct termios *sci_termios_locked[SCI_NPORTS];
87
88 static int sci_refcount;
89 static int sci_debug = 0;
90
91 #ifdef MODULE
92 MODULE_PARM(sci_debug, "i");
93 #endif
94
95 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
96
97 #ifdef CONFIG_SERIAL_CONSOLE
98 static void put_char(struct sci_port *port, char c)
99 {
100 unsigned long flags;
101 unsigned short status;
102
103 save_and_cli(flags);
104
105 do
106 status = sci_in(port, SCxSR);
107 while (!(status & SCxSR_TDxE(port)));
108
109 sci_out(port, SCxTDR, c);
110 sci_in(port, SCxSR); /* Dummy read */
111 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
112
113 restore_flags(flags);
114 }
115 #endif
116
117 #ifdef CONFIG_SH_STANDARD_BIOS
118
119 static void handle_error(struct sci_port *port)
120 { /* Clear error flags */
121 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
122 }
123
124 static int get_char(struct sci_port *port)
125 {
126 unsigned long flags;
127 unsigned short status;
128 int c;
129
130 save_and_cli(flags);
131 do {
132 status = sci_in(port, SCxSR);
133 if (status & SCxSR_ERRORS(port)) {
134 handle_error(port);
135 continue;
136 }
137 } while (!(status & SCxSR_RDxF(port)));
138 c = sci_in(port, SCxRDR);
139 sci_in(port, SCxSR); /* Dummy read */
140 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
141 restore_flags(flags);
142
143 return c;
144 }
145
146 /* Taken from sh-stub.c of GDB 4.18 */
147 static const char hexchars[] = "0123456789abcdef";
148
149 static __inline__ char highhex(int x)
150 {
151 return hexchars[(x >> 4) & 0xf];
152 }
153
154 static __inline__ char lowhex(int x)
155 {
156 return hexchars[x & 0xf];
157 }
158
159 #endif
160
161 /*
162 * Send the packet in buffer. The host gets one chance to read it.
163 * This routine does not wait for a positive acknowledge.
164 */
165
166 #ifdef CONFIG_SERIAL_CONSOLE
167 static void put_string(struct sci_port *port, const char *buffer, int count)
168 {
169 int i;
170 const unsigned char *p = buffer;
171 #ifdef CONFIG_SH_STANDARD_BIOS
172 int checksum;
173
174 /* This call only does a trap the first time it is
175 * called, and so is safe to do here unconditionally
176 */
177 if (sh_bios_in_gdb_mode()) {
178 /* $<packet info>#<checksum>. */
179 do {
180 unsigned char c;
181 put_char(port, '$');
182 put_char(port, 'O'); /* 'O'utput to console */
183 checksum = 'O';
184
185 for (i=0; i<count; i++) { /* Don't use run length encoding */
186 int h, l;
187
188 c = *p++;
189 h = highhex(c);
190 l = lowhex(c);
191 put_char(port, h);
192 put_char(port, l);
193 checksum += h + l;
194 }
195 put_char(port, '#');
196 put_char(port, highhex(checksum));
197 put_char(port, lowhex(checksum));
198 } while (get_char(port) != '+');
199 } else
200 #endif
201 for (i=0; i<count; i++) {
202 if (*p == 10)
203 put_char(port, '\r');
204 put_char(port, *p++);
205 }
206 }
207 #endif
208
209
210 static struct real_driver sci_real_driver = {
211 sci_disable_tx_interrupts,
212 sci_enable_tx_interrupts,
213 sci_disable_rx_interrupts,
214 sci_enable_rx_interrupts,
215 sci_get_CD,
216 sci_shutdown_port,
217 sci_set_real_termios,
218 sci_chars_in_buffer,
219 sci_close,
220 sci_hungup,
221 NULL
222 };
223
224 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
225 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
226 {
227 }
228 #endif
229
230 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
231 #if defined(__sh3__)
232 /* For SH7707, SH7709, SH7709A, SH7729 */
233 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
234 {
235 unsigned int fcr_val = 0;
236
237 {
238 unsigned short data;
239
240 /* We need to set SCPCR to enable RTS/CTS */
241 data = ctrl_inw(SCPCR);
242 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
243 ctrl_outw(data&0x0fcf, SCPCR);
244 }
245 if (cflag & CRTSCTS)
246 fcr_val |= SCFCR_MCE;
247 else {
248 unsigned short data;
249
250 /* We need to set SCPCR to enable RTS/CTS */
251 data = ctrl_inw(SCPCR);
252 /* Clear out SCP7MD1,0, SCP4MD1,0,
253 Set SCP6MD1,0 = {01} (output) */
254 ctrl_outw((data&0x0fcf)|0x1000, SCPCR);
255
256 data = ctrl_inb(SCPDR);
257 /* Set /RTS2 (bit6) = 0 */
258 ctrl_outb(data&0xbf, SCPDR);
259 }
260 sci_out(port, SCFCR, fcr_val);
261 }
262
263 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
264 {
265 unsigned int fcr_val = 0;
266
267 if (cflag & CRTSCTS)
268 fcr_val |= SCFCR_MCE;
269
270 sci_out(port, SCFCR, fcr_val);
271 }
272
273 #else
274
275 /* For SH7750 */
276 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
277 {
278 unsigned int fcr_val = 0;
279
280 if (cflag & CRTSCTS) {
281 fcr_val |= SCFCR_MCE;
282 } else {
283 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
284 }
285 sci_out(port, SCFCR, fcr_val);
286 }
287
288 #endif
289 #endif /* SCIF_ONLY || SCI_AND_SCIF */
290
291 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
292 {
293 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
294 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
295 /* If you have signals for DTR and DCD, please implement here. */
296 ;
297 }
298
299 static int sci_getsignals(struct sci_port *port)
300 {
301 /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
302 and CTS/RTS */
303
304 return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
305 /*
306 (((o_stat & OP_DTR)?TIOCM_DTR:0) |
307 ((o_stat & OP_RTS)?TIOCM_RTS:0) |
308 ((i_stat & IP_CTS)?TIOCM_CTS:0) |
309 ((i_stat & IP_DCD)?TIOCM_CAR:0) |
310 ((i_stat & IP_DSR)?TIOCM_DSR:0) |
311 ((i_stat & IP_RI) ?TIOCM_RNG:0)
312 */
313 }
314
315 static void sci_set_baud(struct sci_port *port, int baud)
316 {
317 int t;
318
319 switch (baud) {
320 case 0:
321 t = -1;
322 break;
323 case 2400:
324 t = BPS_2400;
325 break;
326 case 4800:
327 t = BPS_4800;
328 break;
329 case 9600:
330 t = BPS_9600;
331 break;
332 case 19200:
333 t = BPS_19200;
334 break;
335 case 38400:
336 t = BPS_38400;
337 break;
338 case 57600:
339 t = BPS_57600;
340 break;
341 default:
342 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
343 case 115200:
344 t = BPS_115200;
345 break;
346 }
347
348 if (t > 0) {
349 sci_setsignals (port, 1, -1);
350 if(t >= 256) {
351 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
352 t >>= 2;
353 } else {
354 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
355 }
356 sci_out(port, SCBRR, t);
357 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
358 } else {
359 sci_setsignals (port, 0, -1);
360 }
361 }
362
363 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
364 {
365 unsigned int status;
366 unsigned int smr_val;
367
368 do
369 status = sci_in(port, SCxSR);
370 while (!(status & SCxSR_TEND(port)));
371
372 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
373
374 if (port->type == PORT_SCIF) {
375 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
376 }
377
378 smr_val = sci_in(port, SCSMR) & 3;
379 if ((cflag & CSIZE) == CS7)
380 smr_val |= 0x40;
381 if (cflag & PARENB)
382 smr_val |= 0x20;
383 if (cflag & PARODD)
384 smr_val |= 0x10;
385 if (cflag & CSTOPB)
386 smr_val |= 0x08;
387 sci_out(port, SCSMR, smr_val);
388 sci_set_baud(port, baud);
389
390 port->init_pins(port, cflag);
391 sci_out(port, SCSCR, SCSCR_INIT(port));
392 }
393
394 static int sci_set_real_termios(void *ptr)
395 {
396 struct sci_port *port = ptr;
397
398 if (port->old_cflag != port->gs.tty->termios->c_cflag) {
399 port->old_cflag = port->gs.tty->termios->c_cflag;
400 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
401 sci_enable_rx_interrupts(port);
402 }
403
404 /* Tell line discipline whether we will do input cooking */
405 if (I_OTHER(port->gs.tty))
406 clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
407 else
408 set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
409
410 /* Tell line discipline whether we will do output cooking.
411 * If OPOST is set and no other output flags are set then we can do output
412 * processing. Even if only *one* other flag in the O_OTHER group is set
413 * we do cooking in software.
414 */
415 if (O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty))
416 set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
417 else
418 clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
419
420 return 0;
421 }
422
423 /* ********************************************************************** *
424 * the interrupt related routines *
425 * ********************************************************************** */
426
427 /*
428 * This routine is used by the interrupt handler to schedule
429 * processing in the software interrupt portion of the driver.
430 */
431 static inline void sci_sched_event(struct sci_port *port, int event)
432 {
433 port->event |= 1 << event;
434 queue_task(&port->tqueue, &tq_immediate);
435 mark_bh(IMMEDIATE_BH);
436 }
437
438 static void sci_transmit_chars(struct sci_port *port)
439 {
440 int count, i;
441 int txroom;
442 unsigned long flags;
443 unsigned short status;
444 unsigned short ctrl;
445 unsigned char c;
446
447 status = sci_in(port, SCxSR);
448 if (!(status & SCxSR_TDxE(port))) {
449 save_and_cli(flags);
450 ctrl = sci_in(port, SCSCR);
451 if (port->gs.xmit_cnt == 0) {
452 ctrl &= ~SCI_CTRL_FLAGS_TIE;
453 port->gs.flags &= ~GS_TX_INTEN;
454 } else
455 ctrl |= SCI_CTRL_FLAGS_TIE;
456 sci_out(port, SCSCR, ctrl);
457 restore_flags(flags);
458 return;
459 }
460
461 while (1) {
462 count = port->gs.xmit_cnt;
463 if (port->type == PORT_SCIF) {
464 txroom = 16 - (sci_in(port, SCFDR)>>8);
465 } else {
466 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
467 }
468 if (count > txroom)
469 count = txroom;
470
471 /* Don't copy pas the end of the source buffer */
472 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
473 count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
474
475 /* If for one reason or another, we can't copy more data, we're done! */
476 if (count == 0)
477 break;
478
479 for (i=0; i<count; i++) {
480 c = port->gs.xmit_buf[port->gs.xmit_tail + i];
481 sci_out(port, SCxTDR, c);
482 }
483 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
484
485 port->icount.tx += count;
486
487 /* Update the kernel buffer end */
488 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
489
490 /* This one last. (this is essential)
491 It would allow others to start putting more data into the buffer! */
492 port->gs.xmit_cnt -= count;
493 }
494
495 if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
496 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
497
498 save_and_cli(flags);
499 ctrl = sci_in(port, SCSCR);
500 if (port->gs.xmit_cnt == 0) {
501 ctrl &= ~SCI_CTRL_FLAGS_TIE;
502 port->gs.flags &= ~GS_TX_INTEN;
503 } else {
504 if (port->type == PORT_SCIF) {
505 sci_in(port, SCxSR); /* Dummy read */
506 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
507 }
508 ctrl |= SCI_CTRL_FLAGS_TIE;
509 }
510 sci_out(port, SCSCR, ctrl);
511 restore_flags(flags);
512 }
513
514 static inline void sci_receive_chars(struct sci_port *port)
515 {
516 int i, count;
517 struct tty_struct *tty;
518 int copied=0;
519 unsigned short status;
520
521 status = sci_in(port, SCxSR);
522 if (!(status & SCxSR_RDxF(port)))
523 return;
524
525 tty = port->gs.tty;
526 while (1) {
527 if (port->type == PORT_SCIF) {
528 count = sci_in(port, SCFDR)&0x001f;
529 } else {
530 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
531 }
532
533 /* Don't copy more bytes than there is room for in the buffer */
534 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
535 count = TTY_FLIPBUF_SIZE - tty->flip.count;
536
537 /* If for one reason or another, we can't copy more data, we're done! */
538 if (count == 0)
539 break;
540
541 if (port->type == PORT_SCI) {
542 tty->flip.char_buf_ptr[0] = sci_in(port, SCxRDR);
543 tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
544 } else {
545 for (i=0; i<count; i++) {
546 tty->flip.char_buf_ptr[i] = sci_in(port, SCxRDR);
547 status = sci_in(port, SCxSR);
548 if (status&SCxSR_FER(port)) {
549 tty->flip.flag_buf_ptr[i] = TTY_FRAME;
550 dprintk("sci: frame error\n");
551 } else if (status&SCxSR_PER(port)) {
552 tty->flip.flag_buf_ptr[i] = TTY_PARITY;
553 dprintk("sci: parity error\n");
554 } else {
555 tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
556 }
557 }
558 }
559
560 sci_in(port, SCxSR); /* dummy read */
561 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
562
563 /* Update the kernel buffer end */
564 tty->flip.count += count;
565 tty->flip.char_buf_ptr += count;
566 tty->flip.flag_buf_ptr += count;
567
568 copied += count;
569 port->icount.rx += count;
570 }
571
572 if (copied)
573 /* Tell the rest of the system the news. New characters! */
574 tty_flip_buffer_push(tty);
575 }
576
577 static inline int sci_handle_errors(struct sci_port *port)
578 {
579 int copied = 0;
580 unsigned short status = sci_in(port, SCxSR);
581 struct tty_struct *tty = port->gs.tty;
582
583 if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
584 /* overrun error */
585 copied++;
586 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
587 dprintk("sci: overrun error\n");
588 }
589
590 if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
591 if (sci_rxd_in(port) == 0) {
592 /* Notify of BREAK */
593 copied++;
594 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
595 dprintk("sci: BREAK detected\n");
596 }
597 else {
598 /* frame error */
599 copied++;
600 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
601 dprintk("sci: frame error\n");
602 }
603 }
604
605 if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
606 /* parity error */
607 copied++;
608 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
609 dprintk("sci: parity error\n");
610 }
611
612 if (copied) {
613 tty->flip.count += copied;
614 tty_flip_buffer_push(tty);
615 }
616
617 return copied;
618 }
619
620 static inline int sci_handle_breaks(struct sci_port *port)
621 {
622 int copied = 0;
623 unsigned short status = sci_in(port, SCxSR);
624 struct tty_struct *tty = port->gs.tty;
625
626 if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
627 /* Notify of BREAK */
628 copied++;
629 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
630 dprintk("sci: BREAK detected\n");
631 }
632
633 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1)
634 /* XXX: Handle SCIF overrun error */
635 if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
636 sci_out(port, SCLSR, 0);
637 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
638 copied++;
639 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
640 dprintk("sci: overrun error\n");
641 }
642 }
643 #endif
644
645 if (copied) {
646 tty->flip.count += copied;
647 tty_flip_buffer_push(tty);
648 }
649
650 return copied;
651 }
652
653 static void sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
654 {
655 struct sci_port *port = ptr;
656
657 if (port->gs.flags & GS_ACTIVE)
658 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
659 sci_receive_chars(port);
660 return;
661 }
662 sci_disable_rx_interrupts(port);
663 }
664
665 static void sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
666 {
667 struct sci_port *port = ptr;
668
669 if (port->gs.flags & GS_ACTIVE)
670 sci_transmit_chars(port);
671 else {
672 sci_disable_tx_interrupts(port);
673 }
674 }
675
676 static void sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
677 {
678 struct sci_port *port = ptr;
679
680 /* Handle errors */
681 if (port->type == PORT_SCI) {
682 if(sci_handle_errors(port)) {
683 /* discard character in rx buffer */
684 sci_in(port, SCxSR);
685 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
686 }
687 }
688 else
689 sci_rx_interrupt(irq, ptr, regs);
690
691 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
692
693 /* Kick the transmission */
694 sci_tx_interrupt(irq, ptr, regs);
695 }
696
697 static void sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
698 {
699 struct sci_port *port = ptr;
700
701 /* Handle BREAKs */
702 sci_handle_breaks(port);
703 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
704 }
705
706 static void do_softint(void *private_)
707 {
708 struct sci_port *port = (struct sci_port *) private_;
709 struct tty_struct *tty;
710
711 tty = port->gs.tty;
712 if (!tty)
713 return;
714
715 if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
716 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
717 tty->ldisc.write_wakeup)
718 (tty->ldisc.write_wakeup)(tty);
719 wake_up_interruptible(&tty->write_wait);
720 }
721 }
722
723 /* ********************************************************************** *
724 * Here are the routines that actually *
725 * interface with the generic_serial driver *
726 * ********************************************************************** */
727
728 static void sci_disable_tx_interrupts(void *ptr)
729 {
730 struct sci_port *port = ptr;
731 unsigned long flags;
732 unsigned short ctrl;
733
734 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
735 save_and_cli(flags);
736 ctrl = sci_in(port, SCSCR);
737 ctrl &= ~SCI_CTRL_FLAGS_TIE;
738 sci_out(port, SCSCR, ctrl);
739 restore_flags(flags);
740 }
741
742 static void sci_enable_tx_interrupts(void *ptr)
743 {
744 struct sci_port *port = ptr;
745
746 disable_irq(port->irqs[SCIx_TXI_IRQ]);
747 sci_transmit_chars(port);
748 enable_irq(port->irqs[SCIx_TXI_IRQ]);
749 }
750
751 static void sci_disable_rx_interrupts(void * ptr)
752 {
753 struct sci_port *port = ptr;
754 unsigned long flags;
755 unsigned short ctrl;
756
757 /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
758 save_and_cli(flags);
759 ctrl = sci_in(port, SCSCR);
760 ctrl &= ~SCI_CTRL_FLAGS_RIE;
761 sci_out(port, SCSCR, ctrl);
762 restore_flags(flags);
763 }
764
765 static void sci_enable_rx_interrupts(void * ptr)
766 {
767 struct sci_port *port = ptr;
768 unsigned long flags;
769 unsigned short ctrl;
770
771 /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
772 save_and_cli(flags);
773 ctrl = sci_in(port, SCSCR);
774 ctrl |= SCI_CTRL_FLAGS_RIE;
775 sci_out(port, SCSCR, ctrl);
776 restore_flags(flags);
777 }
778
779 static int sci_get_CD(void * ptr)
780 {
781 /* If you have signal for CD (Carrier Detect), please change here. */
782 return 1;
783 }
784
785 static int sci_chars_in_buffer(void * ptr)
786 {
787 struct sci_port *port = ptr;
788
789 if (port->type == PORT_SCIF) {
790 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
791 } else {
792 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
793 }
794 }
795
796 static void sci_shutdown_port(void * ptr)
797 {
798 struct sci_port *port = ptr;
799
800 port->gs.flags &= ~ GS_ACTIVE;
801 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
802 sci_setsignals(port, 0, 0);
803 sci_free_irq(port);
804 }
805
806 /* ********************************************************************** *
807 * Here are the routines that actually *
808 * interface with the rest of the system *
809 * ********************************************************************** */
810
811 static int sci_open(struct tty_struct * tty, struct file * filp)
812 {
813 struct sci_port *port;
814 int retval, line;
815
816 line = MINOR(tty->device) - SCI_MINOR_START;
817
818 if ((line < 0) || (line >= SCI_NPORTS))
819 return -ENODEV;
820
821 port = &sci_ports[line];
822
823 tty->driver_data = port;
824 port->gs.tty = tty;
825 port->gs.count++;
826
827 port->event = 0;
828 port->tqueue.routine = do_softint;
829 port->tqueue.data = port;
830
831 /*
832 * Start up serial port
833 */
834 retval = gs_init_port(&port->gs);
835 if (retval) {
836 goto failed_1;
837 }
838
839 port->gs.flags |= GS_ACTIVE;
840 sci_setsignals(port, 1,1);
841
842 if (port->gs.count == 1) {
843 MOD_INC_USE_COUNT;
844
845 retval = sci_request_irq(port);
846 if (retval) {
847 goto failed_2;
848 }
849 }
850
851 retval = gs_block_til_ready(port, filp);
852
853 if (retval) {
854 goto failed_3;
855 }
856
857 if ((port->gs.count == 1) && (port->gs.flags & ASYNC_SPLIT_TERMIOS)) {
858 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
859 *tty->termios = port->gs.normal_termios;
860 else
861 *tty->termios = port->gs.callout_termios;
862 sci_set_real_termios(port);
863 }
864
865 #ifdef CONFIG_SERIAL_CONSOLE
866 if (sercons.cflag && sercons.index == line) {
867 tty->termios->c_cflag = sercons.cflag;
868 port->gs.baud = sercons_baud;
869 sercons.cflag = 0;
870 sci_set_real_termios(port);
871 }
872 #endif
873
874 sci_enable_rx_interrupts(port);
875
876 port->gs.session = current->session;
877 port->gs.pgrp = current->pgrp;
878
879 return 0;
880
881 failed_3:
882 sci_free_irq(port);
883 failed_2:
884 MOD_DEC_USE_COUNT;
885 failed_1:
886 port->gs.count--;
887 return retval;
888 }
889
890 static void sci_hungup(void *ptr)
891 {
892 MOD_DEC_USE_COUNT;
893 }
894
895 static void sci_close(void *ptr)
896 {
897 MOD_DEC_USE_COUNT;
898 }
899
900 static int sci_ioctl(struct tty_struct * tty, struct file * filp,
901 unsigned int cmd, unsigned long arg)
902 {
903 int rc;
904 struct sci_port *port = tty->driver_data;
905 int ival;
906
907 rc = 0;
908 switch (cmd) {
909 case TIOCGSOFTCAR:
910 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
911 (unsigned int *) arg);
912 break;
913 case TIOCSSOFTCAR:
914 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
915 tty->termios->c_cflag =
916 (tty->termios->c_cflag & ~CLOCAL) |
917 (ival ? CLOCAL : 0);
918 break;
919 case TIOCGSERIAL:
920 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
921 sizeof(struct serial_struct))) == 0)
922 gs_getserial(&port->gs, (struct serial_struct *) arg);
923 break;
924 case TIOCSSERIAL:
925 if ((rc = verify_area(VERIFY_READ, (void *) arg,
926 sizeof(struct serial_struct))) == 0)
927 rc = gs_setserial(&port->gs,
928 (struct serial_struct *) arg);
929 break;
930 case TIOCMGET:
931 ival = sci_getsignals(port);
932 rc = put_user(ival, (unsigned int *) arg);
933 break;
934 case TIOCMBIS:
935 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
936 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
937 ((ival & TIOCM_RTS) ? 1 : -1));
938 break;
939 case TIOCMBIC:
940 if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
941 sci_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
942 ((ival & TIOCM_RTS) ? 0 : -1));
943 break;
944 case TIOCMSET:
945 if ((rc = get_user(ival, (unsigned int *)arg)) == 0)
946 sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
947 ((ival & TIOCM_RTS) ? 1 : 0));
948 break;
949
950 default:
951 rc = -ENOIOCTLCMD;
952 break;
953 }
954
955 return rc;
956 }
957
958 static void sci_throttle(struct tty_struct * tty)
959 {
960 struct sci_port *port = (struct sci_port *)tty->driver_data;
961
962 /* If the port is using any type of input flow
963 * control then throttle the port.
964 */
965 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
966 port->gs.flags |= SCI_RX_THROTTLE;
967 }
968
969 static void sci_unthrottle(struct tty_struct * tty)
970 {
971 struct sci_port *port = (struct sci_port *)tty->driver_data;
972
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
976 */
977 port->gs.flags &= ~SCI_RX_THROTTLE;
978 return;
979 }
980
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)
984 {
985 int i;
986 struct sci_port *port;
987 int len = 0;
988
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);
998
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");
1008 }
1009 return len;
1010 }
1011 #endif
1012
1013 /* ********************************************************************** *
1014 * Here are the initialization routines. *
1015 * ********************************************************************** */
1016
1017 static int sci_init_drivers(void)
1018 {
1019 int error;
1020 struct sci_port *port;
1021
1022 memset(&sci_driver, 0, sizeof(sci_driver));
1023 sci_driver.magic = TTY_DRIVER_MAGIC;
1024 sci_driver.driver_name = "sci";
1025 #ifdef CONFIG_DEVFS_FS
1026 sci_driver.name = "ttsc/%d";
1027 #else
1028 sci_driver.name = "ttySC";
1029 #endif
1030 sci_driver.major = SCI_MAJOR;
1031 sci_driver.minor_start = SCI_MINOR_START;
1032 sci_driver.num = SCI_NPORTS;
1033 sci_driver.type = TTY_DRIVER_TYPE_SERIAL;
1034 sci_driver.subtype = SERIAL_TYPE_NORMAL;
1035 sci_driver.init_termios = tty_std_termios;
1036 sci_driver.init_termios.c_cflag =
1037 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1038 sci_driver.flags = TTY_DRIVER_REAL_RAW;
1039 sci_driver.refcount = &sci_refcount;
1040 sci_driver.table = sci_table;
1041 sci_driver.termios = sci_termios;
1042 sci_driver.termios_locked = sci_termios_locked;
1043
1044 sci_driver.open = sci_open;
1045 sci_driver.close = gs_close;
1046 sci_driver.write = gs_write;
1047 sci_driver.put_char = gs_put_char;
1048 sci_driver.flush_chars = gs_flush_chars;
1049 sci_driver.write_room = gs_write_room;
1050 sci_driver.chars_in_buffer = gs_chars_in_buffer;
1051 sci_driver.flush_buffer = gs_flush_buffer;
1052 sci_driver.ioctl = sci_ioctl;
1053 sci_driver.throttle = sci_throttle;
1054 sci_driver.unthrottle = sci_unthrottle;
1055 sci_driver.set_termios = gs_set_termios;
1056 sci_driver.stop = gs_stop;
1057 sci_driver.start = gs_start;
1058 sci_driver.hangup = gs_hangup;
1059 #ifdef CONFIG_PROC_FS
1060 sci_driver.read_proc = sci_read_proc;
1061 #endif
1062
1063 sci_callout_driver = sci_driver;
1064 #ifdef CONFIG_DEVFS_FS
1065 sci_callout_driver.name = "cusc/%d";
1066 #else
1067 sci_callout_driver.name = "cusc";
1068 #endif
1069 sci_callout_driver.major = SCI_MAJOR+1;
1070 sci_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1071 sci_callout_driver.read_proc = NULL;
1072
1073 if ((error = tty_register_driver(&sci_driver))) {
1074 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1075 error);
1076 return 1;
1077 }
1078 if ((error = tty_register_driver(&sci_callout_driver))) {
1079 tty_unregister_driver(&sci_driver);
1080 printk(KERN_ERR "sci: Couldn't register SCI callout driver, error = %d\n",
1081 error);
1082 return 1;
1083 }
1084
1085 for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1086 port->gs.callout_termios = sci_callout_driver.init_termios;
1087 port->gs.normal_termios = sci_driver.init_termios;
1088 port->gs.magic = SCI_MAGIC;
1089 port->gs.close_delay = HZ/2;
1090 port->gs.closing_wait = 30 * HZ;
1091 port->gs.rd = &sci_real_driver;
1092 init_waitqueue_head(&port->gs.open_wait);
1093 init_waitqueue_head(&port->gs.close_wait);
1094 port->old_cflag = 0;
1095 port->icount.cts = port->icount.dsr =
1096 port->icount.rng = port->icount.dcd = 0;
1097 port->icount.rx = port->icount.tx = 0;
1098 port->icount.frame = port->icount.parity = 0;
1099 port->icount.overrun = port->icount.brk = 0;
1100 }
1101
1102 return 0;
1103 }
1104
1105 static int sci_request_irq(struct sci_port *port)
1106 {
1107 int i;
1108 void (*handlers[4])(int irq, void *ptr, struct pt_regs *regs) = {
1109 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1110 sci_br_interrupt,
1111 };
1112
1113 for (i=0; i<4; i++) {
1114 if (!port->irqs[i]) continue;
1115 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1116 "sci", port)) {
1117 printk(KERN_ERR "sci: Cannot allocate irq.\n");
1118 return -ENODEV;
1119 }
1120 }
1121 return 0;
1122 }
1123
1124 static void sci_free_irq(struct sci_port *port)
1125 {
1126 int i;
1127
1128 for (i=0; i<4; i++) {
1129 if (!port->irqs[i]) continue;
1130 free_irq(port->irqs[i], port);
1131 }
1132 }
1133
1134 static char banner[] __initdata[] =
1135 KERN_INFO "SuperH SCI(F) driver initialized\n";
1136
1137 int __init sci_init(void)
1138 {
1139 struct sci_port *port;
1140 int j;
1141
1142 printk("%s", banner);
1143
1144 for (j=0; j<SCI_NPORTS; j++) {
1145 port = &sci_ports[j];
1146 printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
1147 (port->type == PORT_SCI) ? "SCI" : "SCIF");
1148 }
1149
1150 sci_init_drivers();
1151
1152 #ifdef CONFIG_SH_STANDARD_BIOS
1153 sh_bios_gdb_detach();
1154 #endif
1155 return 0; /* Return -EIO when not detected */
1156 }
1157
1158 module_init(sci_init);
1159
1160 #ifdef MODULE
1161 #undef func_enter
1162 #undef func_exit
1163
1164 void cleanup_module(void)
1165 {
1166 tty_unregister_driver(&sci_driver);
1167 tty_unregister_driver(&sci_callout_driver);
1168 }
1169
1170 #include "generic_serial.c"
1171 #endif
1172
1173 #ifdef CONFIG_SERIAL_CONSOLE
1174 /*
1175 * Print a string to the serial port trying not to disturb
1176 * any possible real use of the port...
1177 */
1178 static void serial_console_write(struct console *co, const char *s,
1179 unsigned count)
1180 {
1181 put_string(sercons_port, s, count);
1182 }
1183
1184 /*
1185 * Receive character from the serial port
1186 */
1187 static int serial_console_wait_key(struct console *co)
1188 {
1189 /* Not implemented yet */
1190 return 0;
1191 }
1192
1193 static kdev_t serial_console_device(struct console *c)
1194 {
1195 return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1196 }
1197
1198 /*
1199 * Setup initial baud/bits/parity. We do two things here:
1200 * - construct a cflag setting for the first rs_open()
1201 * - initialize the serial port
1202 * Return non-zero if we didn't find a serial port.
1203 */
1204 static int __init serial_console_setup(struct console *co, char *options)
1205 {
1206 int baud = 9600;
1207 int bits = 8;
1208 int parity = 'n';
1209 int cflag = CREAD | HUPCL | CLOCAL;
1210 char *s;
1211
1212 sercons_port = &sci_ports[co->index];
1213
1214 if (options) {
1215 baud = simple_strtoul(options, NULL, 10);
1216 s = options;
1217 while(*s >= '0' && *s <= '9')
1218 s++;
1219 if (*s) parity = *s++;
1220 if (*s) bits = *s - '0';
1221 }
1222
1223 /*
1224 * Now construct a cflag setting.
1225 */
1226 switch (baud) {
1227 case 19200:
1228 cflag |= B19200;
1229 break;
1230 case 38400:
1231 cflag |= B38400;
1232 break;
1233 case 57600:
1234 cflag |= B57600;
1235 break;
1236 case 115200:
1237 cflag |= B115200;
1238 break;
1239 case 9600:
1240 default:
1241 cflag |= B9600;
1242 baud = 9600;
1243 break;
1244 }
1245 switch (bits) {
1246 case 7:
1247 cflag |= CS7;
1248 break;
1249 default:
1250 case 8:
1251 cflag |= CS8;
1252 break;
1253 }
1254 switch (parity) {
1255 case 'o': case 'O':
1256 cflag |= PARODD;
1257 break;
1258 case 'e': case 'E':
1259 cflag |= PARENB;
1260 break;
1261 }
1262
1263 co->cflag = cflag;
1264 sercons_baud = baud;
1265
1266 sci_set_termios_cflag(sercons_port, cflag, baud);
1267 sercons_port->old_cflag = cflag;
1268
1269 return 0;
1270 }
1271
1272 static struct console sercons = {
1273 name: "ttySC",
1274 write: serial_console_write,
1275 device: serial_console_device,
1276 wait_key: serial_console_wait_key,
1277 setup: serial_console_setup,
1278 flags: CON_PRINTBUFFER,
1279 index: -1,
1280 };
1281
1282 /*
1283 * Register console.
1284 */
1285
1286 #ifdef CONFIG_SH_EARLY_PRINTK
1287 extern void sh_console_unregister (void);
1288 #endif
1289
1290 void __init sci_console_init(void)
1291 {
1292 register_console(&sercons);
1293 #ifdef CONFIG_SH_EARLY_PRINTK
1294 /* Now that the real console is available, unregister the one we
1295 * used while first booting.
1296 */
1297 sh_console_unregister();
1298 #endif
1299 }
1300 #endif /* CONFIG_SERIAL_CONSOLE */
1301