This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / char / sh-sci.c
1 /* $Id: sh-sci.c,v 1.16 2004/02/10 17:04:17 lethal 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  *  Modified to support SH7760 SCIF. Paul Mundt (Oct 2003).
10  *  Modified to support H8/300 Series. Yoshinori Sato (Feb 2004).
11  *
12  * TTY code is based on sx.c (Specialix SX driver) by:
13  *
14  *   (C) 1998 R.E.Wolff@BitWizard.nl
15  *
16  */
17
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
33 #include <linux/mm.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SH_KGDB_CONSOLE)
38 #include <linux/console.h>
39 #endif
40 #ifdef CONFIG_CPU_FREQ
41 #include <linux/notifier.h>
42 #include <linux/cpufreq.h>
43 #endif
44
45 #include <asm/system.h>
46 #include <asm/io.h>
47 #include <asm/irq.h>
48 #include <asm/uaccess.h>
49 #include <asm/bitops.h>
50
51 #include <linux/generic_serial.h>
52
53 #ifdef CONFIG_SH_STANDARD_BIOS
54 #include <asm/sh_bios.h>
55 #endif
56
57 #include "sh-sci.h"
58
59 #ifdef CONFIG_SH_KGDB
60 #include <asm/kgdb.h>
61
62 int kgdb_sci_setup(void);
63 static int kgdb_get_char(struct sci_port *port);
64 static void kgdb_put_char(struct sci_port *port, char c);
65 static void kgdb_handle_error(struct sci_port *port);
66 static struct sci_port *kgdb_sci_port;
67
68 #ifdef CONFIG_SH_KGDB_CONSOLE
69 static struct console kgdbcons;
70 void __init kgdb_console_init(void);
71 #endif /* CONFIG_SH_KGDB_CONSOLE */
72
73 #endif /* CONFIG_SH_KGDB */
74
75 #ifdef CONFIG_SERIAL_CONSOLE
76 static struct console sercons;
77 static struct sci_port* sercons_port=0;
78 static int sercons_baud;
79 #ifdef CONFIG_MAGIC_SYSRQ
80 #include <linux/sysrq.h>
81 static int break_pressed;
82 #endif /* CONFIG_MAGIC_SYSRQ */
83 #endif /* CONFIG_SERIAL_CONSOLE */
84
85 /* Function prototypes */
86 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag);
87 #ifndef SCI_ONLY
88 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag);
89 #if defined(CONFIG_CPU_SH3)
90 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag);
91 #endif
92 #endif
93 static void sci_disable_tx_interrupts(void *ptr);
94 static void sci_enable_tx_interrupts(void *ptr);
95 static void sci_disable_rx_interrupts(void *ptr);
96 static void sci_enable_rx_interrupts(void *ptr);
97 static int  sci_get_CD(void *ptr);
98 static void sci_shutdown_port(void *ptr);
99 static int sci_set_real_termios(void *ptr);
100 static void sci_hungup(void *ptr);
101 static void sci_close(void *ptr);
102 static int sci_chars_in_buffer(void *ptr);
103 static int sci_request_irq(struct sci_port *port);
104 static void sci_free_irq(struct sci_port *port);
105 static int sci_init_drivers(void);
106
107 static struct tty_driver *sci_driver;
108
109 static struct sci_port sci_ports[SCI_NPORTS] = SCI_INIT;
110
111 static int sci_debug = 0;
112
113 #ifdef MODULE
114 MODULE_PARM(sci_debug, "i");
115 #endif
116
117 #define dprintk(x...) do { if (sci_debug) printk(x); } while(0)
118
119 #ifdef CONFIG_SERIAL_CONSOLE
120 static void put_char(struct sci_port *port, char c)
121 {
122         unsigned long flags;
123         unsigned short status;
124
125         local_irq_save(flags);
126
127         do
128                 status = sci_in(port, SCxSR);
129         while (!(status & SCxSR_TDxE(port)));
130         
131         sci_out(port, SCxTDR, c);
132         sci_in(port, SCxSR);            /* Dummy read */
133         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
134
135         local_irq_restore(flags);
136 }
137 #endif
138
139 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
140
141 static void handle_error(struct sci_port *port)
142 {                               /* Clear error flags */
143         sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
144 }
145
146 static int get_char(struct sci_port *port)
147 {
148         unsigned long flags;
149         unsigned short status;
150         int c;
151
152         local_irq_save(flags);
153         do {
154                 status = sci_in(port, SCxSR);
155                 if (status & SCxSR_ERRORS(port)) {
156                         handle_error(port);
157                         continue;
158                 }
159         } while (!(status & SCxSR_RDxF(port)));
160         c = sci_in(port, SCxRDR);
161         sci_in(port, SCxSR);            /* Dummy read */
162         sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
163         local_irq_restore(flags);
164
165         return c;
166 }
167
168 /* Taken from sh-stub.c of GDB 4.18 */
169 static const char hexchars[] = "0123456789abcdef";
170
171 static __inline__ char highhex(int  x)
172 {
173         return hexchars[(x >> 4) & 0xf];
174 }
175
176 static __inline__ char lowhex(int  x)
177 {
178         return hexchars[x & 0xf];
179 }
180
181 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
182
183 /*
184  * Send the packet in buffer.  The host gets one chance to read it.
185  * This routine does not wait for a positive acknowledge.
186  */
187
188 #ifdef CONFIG_SERIAL_CONSOLE
189 static void put_string(struct sci_port *port, const char *buffer, int count)
190 {
191         int i;
192         const unsigned char *p = buffer;
193
194 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
195         int checksum;
196         int usegdb=0;
197
198 #ifdef CONFIG_SH_STANDARD_BIOS
199         /* This call only does a trap the first time it is
200          * called, and so is safe to do here unconditionally
201          */
202         usegdb |= sh_bios_in_gdb_mode();
203 #endif
204 #ifdef CONFIG_SH_KGDB
205         usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));
206 #endif
207
208         if (usegdb) {
209             /*  $<packet info>#<checksum>. */
210             do {
211                 unsigned char c;
212                 put_char(port, '$');
213                 put_char(port, 'O'); /* 'O'utput to console */
214                 checksum = 'O';
215
216                 for (i=0; i<count; i++) { /* Don't use run length encoding */
217                         int h, l;
218
219                         c = *p++;
220                         h = highhex(c);
221                         l = lowhex(c);
222                         put_char(port, h);
223                         put_char(port, l);
224                         checksum += h + l;
225                 }
226                 put_char(port, '#');
227                 put_char(port, highhex(checksum));
228                 put_char(port, lowhex(checksum));
229             } while  (get_char(port) != '+');
230         } else
231 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
232         for (i=0; i<count; i++) {
233                 if (*p == 10)
234                         put_char(port, '\r');
235                 put_char(port, *p++);
236         }
237 }
238 #endif /* CONFIG_SERIAL_CONSOLE */
239
240
241 #ifdef CONFIG_SH_KGDB
242
243 /* Is the SCI ready, ie is there a char waiting? */
244 static int kgdb_is_char_ready(struct sci_port *port)
245 {
246         unsigned short status = sci_in(port, SCxSR);
247
248         if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))
249                 kgdb_handle_error(port);
250
251         return (status & SCxSR_RDxF(port));
252 }
253
254 /* Write a char */
255 static void kgdb_put_char(struct sci_port *port, char c)
256 {
257         unsigned short status;
258
259         do
260                 status = sci_in(port, SCxSR);
261         while (!(status & SCxSR_TDxE(port)));
262
263         sci_out(port, SCxTDR, c);
264         sci_in(port, SCxSR);    /* Dummy read */
265         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
266 }
267
268 /* Get a char if there is one, else ret -1 */
269 static int kgdb_get_char(struct sci_port *port)
270 {
271         int c;
272
273         if (kgdb_is_char_ready(port) == 0)
274                 c = -1;
275         else {
276                 c = sci_in(port, SCxRDR);
277                 sci_in(port, SCxSR);    /* Dummy read */
278                 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
279         }
280
281         return c;
282 }
283
284 /* Called from kgdbstub.c to get a character, i.e. is blocking */
285 static int kgdb_sci_getchar(void)
286 {
287         volatile int c;
288
289         /* Keep trying to read a character, this could be neater */
290         while ((c = kgdb_get_char(kgdb_sci_port)) < 0);
291
292         return c;
293 }
294
295 /* Called from kgdbstub.c to put a character, just a wrapper */
296 static void kgdb_sci_putchar(int c)
297 {
298
299         kgdb_put_char(kgdb_sci_port, c);
300 }
301
302 /* Clear any errors on the SCI */
303 static void kgdb_handle_error(struct sci_port *port)
304 {
305         sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));  /* Clear error flags */
306 }
307
308 /* Breakpoint if there's a break sent on the serial port */
309 static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs)
310 {
311         struct sci_port *port = ptr;
312         unsigned short status = sci_in(port, SCxSR);
313
314         if (status & SCxSR_BRK(port)) {
315
316                 /* Break into the debugger if a break is detected */
317                 BREAKPOINT();
318
319                 /* Clear */
320                 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
321                 return;
322         }
323 }
324
325 #endif /* CONFIG_SH_KGDB */
326
327 static struct real_driver sci_real_driver = {
328         sci_disable_tx_interrupts,
329         sci_enable_tx_interrupts,
330         sci_disable_rx_interrupts,
331         sci_enable_rx_interrupts,
332         sci_get_CD,
333         sci_shutdown_port,
334         sci_set_real_termios,
335         sci_chars_in_buffer,
336         sci_close,
337         sci_hungup,
338         NULL
339 };
340
341 #if !defined(__H8300H__) && !defined(__H8300S__)
342 #if defined(SCI_ONLY) || defined(SCI_AND_SCIF)
343 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
344 {
345 }
346 #endif
347
348 #if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)
349 #if defined(CONFIG_CPU_SH3)
350 /* For SH7707, SH7709, SH7709A, SH7729 */
351 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
352 {
353         unsigned int fcr_val = 0;
354
355         {
356                 unsigned short data;
357
358                 /* We need to set SCPCR to enable RTS/CTS */
359                 data = ctrl_inw(SCPCR);
360                 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
361                 ctrl_outw(data&0x0cff, SCPCR);
362         }
363         if (cflag & CRTSCTS)
364                 fcr_val |= SCFCR_MCE;
365         else {
366                 unsigned short data;
367
368                 /* We need to set SCPCR to enable RTS/CTS */
369                 data = ctrl_inw(SCPCR);
370                 /* Clear out SCP7MD1,0, SCP4MD1,0,
371                    Set SCP6MD1,0 = {01} (output)  */
372                 ctrl_outw((data&0x0cff)|0x1000, SCPCR);
373
374                 data = ctrl_inb(SCPDR);
375                 /* Set /RTS2 (bit6) = 0 */
376                 ctrl_outb(data&0xbf, SCPDR);
377         }
378         sci_out(port, SCFCR, fcr_val);
379 }
380
381 static void sci_init_pins_irda(struct sci_port* port, unsigned int cflag)
382 {
383         unsigned int fcr_val = 0;
384
385         if (cflag & CRTSCTS)
386                 fcr_val |= SCFCR_MCE;
387
388         sci_out(port, SCFCR, fcr_val);
389 }
390
391 #else
392
393 /* For SH7750 */
394 static void sci_init_pins_scif(struct sci_port* port, unsigned int cflag)
395 {
396         unsigned int fcr_val = 0;
397
398         if (cflag & CRTSCTS) {
399                 fcr_val |= SCFCR_MCE;
400         } else {
401                 ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */
402         }
403         sci_out(port, SCFCR, fcr_val);
404 }
405
406 #endif
407 #endif /* SCIF_ONLY || SCI_AND_SCIF */
408 #else /* !defined(__H8300H__) && !defined(__H8300S__) */
409 static void sci_init_pins_sci(struct sci_port* port, unsigned int cflag)
410 {
411         int ch = (port->base - SMR0) >> 3;
412         /* set DDR regs */
413         H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);
414         H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);
415         /* tx mark output*/
416         H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
417 }
418
419 #if defined(__H8300S__)
420 enum {sci_disable,sci_enable};
421
422 static void h8300_sci_enable(struct sci_port* port, unsigned int ctrl)
423 {
424         volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;
425         int ch = (port->base  - SMR0) >> 3;
426         unsigned char mask = 1 << (ch+1);
427         if (ctrl == sci_disable)
428                 *mstpcrl |= mask;
429         else
430                 *mstpcrl &= ~mask;
431 }
432 #endif
433 #endif
434
435 static void sci_setsignals(struct sci_port *port, int dtr, int rts)
436 {
437         /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
438         /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
439         /* If you have signals for DTR and DCD, please implement here. */
440         ;
441 }
442
443 static int sci_getsignals(struct sci_port *port)
444 {
445         /* This routine is used for geting signals of: DTR, DCD, DSR, RI,
446            and CTS/RTS */
447
448         return TIOCM_DTR|TIOCM_RTS|TIOCM_DSR;
449 /*
450         (((o_stat & OP_DTR)?TIOCM_DTR:0) |
451          ((o_stat & OP_RTS)?TIOCM_RTS:0) |
452          ((i_stat & IP_CTS)?TIOCM_CTS:0) |
453          ((i_stat & IP_DCD)?TIOCM_CAR:0) |
454          ((i_stat & IP_DSR)?TIOCM_DSR:0) |
455          ((i_stat & IP_RI) ?TIOCM_RNG:0)
456 */
457 }
458
459 static void sci_set_baud(struct sci_port *port, int baud)
460 {
461         int t;
462
463         switch (baud) {
464         case 0:
465                 t = -1;
466                 break;
467         case 2400:
468                 t = BPS_2400;
469                 break;
470         case 4800:
471                 t = BPS_4800;
472                 break;
473         case 9600:
474                 t = BPS_9600;
475                 break;
476         case 19200:
477                 t = BPS_19200;
478                 break;
479         case 38400:
480                 t = BPS_38400;
481                 break;
482         case 57600:
483                 t = BPS_57600;
484                 break;
485         default:
486                 printk(KERN_INFO "sci: unsupported baud rate: %d, using 115200 instead.\n", baud);
487         case 115200:
488                 t = BPS_115200;
489                 break;
490         }
491
492         if (t > 0) {
493                 sci_setsignals (port, 1, -1);
494                 if(t >= 256) {
495                         sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
496                         t >>= 2;
497                 } else {
498                         sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
499                 }
500                 sci_out(port, SCBRR, t);
501                 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
502         } else {
503                 sci_setsignals (port, 0, -1);
504         }
505 }
506
507 static void sci_set_termios_cflag(struct sci_port *port, int cflag, int baud)
508 {
509         unsigned int status;
510         unsigned int smr_val;
511
512         do
513                 status = sci_in(port, SCxSR);
514         while (!(status & SCxSR_TEND(port)));
515
516         sci_out(port, SCSCR, 0x00);     /* TE=0, RE=0, CKE1=0 */
517
518 #if !defined(SCI_ONLY)
519         if (port->type == PORT_SCIF) {
520                 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
521         }
522 #endif
523
524         smr_val = sci_in(port, SCSMR) & 3;
525         if ((cflag & CSIZE) == CS7)
526                 smr_val |= 0x40;
527         if (cflag & PARENB)
528                 smr_val |= 0x20;
529         if (cflag & PARODD)
530                 smr_val |= 0x30;
531         if (cflag & CSTOPB)
532                 smr_val |= 0x08;
533         sci_out(port, SCSMR, smr_val);
534         sci_set_baud(port, baud);
535
536         port->init_pins(port, cflag);
537         sci_out(port, SCSCR, SCSCR_INIT(port));
538 }
539
540 static int sci_set_real_termios(void *ptr)
541 {
542         struct sci_port *port = ptr;
543
544         if (port->old_cflag != port->gs.tty->termios->c_cflag) {
545                 port->old_cflag = port->gs.tty->termios->c_cflag;
546                 sci_set_termios_cflag(port, port->old_cflag, port->gs.baud);
547                 sci_enable_rx_interrupts(port);
548         }
549
550         return 0;
551 }
552
553 /* ********************************************************************** *
554  *                   the interrupt related routines                       *
555  * ********************************************************************** */
556
557 /*
558  * This routine is used by the interrupt handler to schedule
559  * processing in the software interrupt portion of the driver.
560  */
561 static inline void sci_sched_event(struct sci_port *port, int event)
562 {
563         port->event |= 1 << event;
564         schedule_work(&port->tqueue);
565 }
566
567 static void sci_transmit_chars(struct sci_port *port)
568 {
569         int count, i;
570         int txroom;
571         unsigned long flags;
572         unsigned short status;
573         unsigned short ctrl;
574         unsigned char c;
575
576         status = sci_in(port, SCxSR);
577         if (!(status & SCxSR_TDxE(port))) {
578                 local_irq_save(flags);
579                 ctrl = sci_in(port, SCSCR);
580                 if (port->gs.xmit_cnt == 0) {
581                         ctrl &= ~SCI_CTRL_FLAGS_TIE;
582                         port->gs.flags &= ~GS_TX_INTEN;
583                 } else
584                         ctrl |= SCI_CTRL_FLAGS_TIE;
585                 sci_out(port, SCSCR, ctrl);
586                 local_irq_restore(flags);
587                 return;
588         }
589
590         while (1) {
591                 count = port->gs.xmit_cnt;
592 #if !defined(SCI_ONLY)
593                 if (port->type == PORT_SCIF) {
594                         txroom = 16 - (sci_in(port, SCFDR)>>8);
595                 } else {
596                         txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
597                 }
598 #else
599                 txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;
600 #endif
601                 if (count > txroom)
602                         count = txroom;
603
604                 /* Don't copy past the end of the source buffer */
605                 if (count > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
606                         count = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
607
608                 /* If for one reason or another, we can't copy more data, we're done! */
609                 if (count == 0)
610                         break;
611
612                 for (i=0; i<count; i++) {
613                         c = port->gs.xmit_buf[port->gs.xmit_tail + i];
614                         sci_out(port, SCxTDR, c);
615                 }
616                 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
617
618                 port->icount.tx += count;
619
620                 /* Update the kernel buffer end */
621                 port->gs.xmit_tail = (port->gs.xmit_tail + count) & (SERIAL_XMIT_SIZE-1);
622
623                 /* This one last. (this is essential)
624                    It would allow others to start putting more data into the buffer! */
625                 port->gs.xmit_cnt -= count;
626         }
627
628         if (port->gs.xmit_cnt <= port->gs.wakeup_chars)
629                 sci_sched_event(port, SCI_EVENT_WRITE_WAKEUP);
630
631         local_irq_save(flags);
632         ctrl = sci_in(port, SCSCR);
633         if (port->gs.xmit_cnt == 0) {
634                 ctrl &= ~SCI_CTRL_FLAGS_TIE;
635                 port->gs.flags &= ~GS_TX_INTEN;
636         } else {
637 #if !defined(SCI_ONLY)
638                 if (port->type == PORT_SCIF) {
639                         sci_in(port, SCxSR); /* Dummy read */
640                         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
641                 }
642 #endif
643                 ctrl |= SCI_CTRL_FLAGS_TIE;
644         }
645         sci_out(port, SCSCR, ctrl);
646         local_irq_restore(flags);
647 }
648
649 /* On SH3, SCIF may read end-of-break as a space->mark char */
650 #define STEPFN(c)  ({int __c=(c); (((__c-1)|(__c)) == -1); })
651
652 static inline void sci_receive_chars(struct sci_port *port,
653                                      struct pt_regs *regs)
654 {
655         int i, count;
656         struct tty_struct *tty;
657         int copied=0;
658         unsigned short status;
659
660         status = sci_in(port, SCxSR);
661         if (!(status & SCxSR_RDxF(port)))
662                 return;
663
664         tty = port->gs.tty;
665         while (1) {
666 #if !defined(SCI_ONLY)
667                 if (port->type == PORT_SCIF) {
668                         count = sci_in(port, SCFDR)&0x001f;
669                 } else {
670                         count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
671                 }
672 #else
673                 count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;
674 #endif
675
676                 /* Don't copy more bytes than there is room for in the buffer */
677                 if (tty->flip.count + count > TTY_FLIPBUF_SIZE)
678                         count = TTY_FLIPBUF_SIZE - tty->flip.count;
679
680                 /* If for any reason we can't copy more data, we're done! */
681                 if (count == 0)
682                         break;
683
684                 if (port->type == PORT_SCI) {
685                         tty->flip.char_buf_ptr[0] = sci_in(port, SCxRDR);
686                         tty->flip.flag_buf_ptr[0] = TTY_NORMAL;
687                 } else {
688                         for (i=0; i<count; i++) {
689                                 char c = sci_in(port, SCxRDR);
690                                 status = sci_in(port, SCxSR);
691 #if defined(__SH3__)
692                                 /* Skip "chars" during break */
693                                 if (port->break_flag) {
694                                         if ((c == 0) &&
695                                             (status & SCxSR_FER(port))) {
696                                                 count--; i--;
697                                                 continue;
698                                         }
699                                         /* Nonzero => end-of-break */
700                                         dprintk("scif: debounce<%02x>\n", c);
701                                         port->break_flag = 0;
702                                         if (STEPFN(c)) {
703                                                 count--; i--;
704                                                 continue;
705                                         }
706                                 }
707 #endif /* __SH3__ */
708 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
709                                 if (break_pressed && (port == sercons_port)) {
710                                         if (c != 0 &&
711                                             time_before(jiffies,
712                                                         break_pressed + HZ*5)) {
713                                                 handle_sysrq(c, regs, NULL);
714                                                 break_pressed = 0;
715                                                 count--; i--;
716                                                 continue;
717                                         } else if (c != 0) {
718                                                 break_pressed = 0;
719                                         }
720                                 }
721 #endif /* CONFIG_SERIAL_CONSOLE && CONFIG_MAGIC_SYSRQ */
722
723                                 /* Store data and status */
724                                 tty->flip.char_buf_ptr[i] = c;
725                                 if (status&SCxSR_FER(port)) {
726                                         tty->flip.flag_buf_ptr[i] = TTY_FRAME;
727                                         dprintk("sci: frame error\n");
728                                 } else if (status&SCxSR_PER(port)) {
729                                         tty->flip.flag_buf_ptr[i] = TTY_PARITY;
730                                         dprintk("sci: parity error\n");
731                                 } else {
732                                         tty->flip.flag_buf_ptr[i] = TTY_NORMAL;
733                                 }
734                         }
735                 }
736
737                 sci_in(port, SCxSR); /* dummy read */
738                 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
739
740                 /* Update the kernel buffer end */
741                 tty->flip.count += count;
742                 tty->flip.char_buf_ptr += count;
743                 tty->flip.flag_buf_ptr += count;
744
745                 copied += count;
746                 port->icount.rx += count;
747         }
748
749         if (copied)
750                 /* Tell the rest of the system the news. New characters! */
751                 tty_flip_buffer_push(tty);
752         else {
753                 sci_in(port, SCxSR); /* dummy read */
754                 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
755         }
756 }
757
758 static inline int sci_handle_errors(struct sci_port *port)
759 {
760         int copied = 0;
761         unsigned short status = sci_in(port, SCxSR);
762         struct tty_struct *tty = port->gs.tty;
763
764         if (status&SCxSR_ORER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
765                 /* overrun error */
766                 copied++;
767                 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
768                 dprintk("sci: overrun error\n");
769         }
770
771         if (status&SCxSR_FER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
772                 if (sci_rxd_in(port) == 0) {
773                         /* Notify of BREAK */
774                         copied++;
775                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
776                         dprintk("sci: BREAK detected\n");
777                 }
778                 else {
779                         /* frame error */
780                         copied++;
781                         *tty->flip.flag_buf_ptr++ = TTY_FRAME;
782                         dprintk("sci: frame error\n");
783                 }
784         }
785
786         if (status&SCxSR_PER(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
787                 /* parity error */
788                 copied++;
789                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
790                 dprintk("sci: parity error\n");
791         }
792
793         if (copied) {
794                 tty->flip.count += copied;
795                 tty_flip_buffer_push(tty);
796         }
797
798         return copied;
799 }
800
801 static inline int sci_handle_breaks(struct sci_port *port)
802 {
803         int copied = 0;
804         unsigned short status = sci_in(port, SCxSR);
805         struct tty_struct *tty = port->gs.tty;
806
807         if (status&SCxSR_BRK(port) && tty->flip.count<TTY_FLIPBUF_SIZE) {
808 #if defined(__SH3__)
809                 /* Debounce break */
810                 if (port->break_flag)
811                         goto break_continue;
812                 port->break_flag = 1;
813 #endif
814 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
815                 if (port == sercons_port) {
816                         if (break_pressed == 0) {
817                                 break_pressed = jiffies;
818                                 dprintk("sci: implied sysrq\n");
819                                 goto break_continue;
820                         }
821                         /* Double break implies a real break */
822                         break_pressed = 0;
823                 }
824 #endif
825                 /* Notify of BREAK */
826                 copied++;
827                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
828                 dprintk("sci: BREAK detected\n");
829         }
830  break_continue:
831
832 #if defined(CONFIG_CPU_SUBTYPE_SH7750) || defined(CONFIG_CPU_SUBTYPE_ST40STB1) || \
833     defined(CONFIG_CPU_SUBTYPE_SH7760)
834         /* XXX: Handle SCIF overrun error */
835         if (port->type == PORT_SCIF && (sci_in(port, SCLSR) & SCIF_ORER) != 0) {
836                 sci_out(port, SCLSR, 0);
837                 if(tty->flip.count<TTY_FLIPBUF_SIZE) {
838                         copied++;
839                         *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
840                         dprintk("sci: overrun error\n");
841                 }
842         }
843 #endif
844
845         if (copied) {
846                 tty->flip.count += copied;
847                 tty_flip_buffer_push(tty);
848         }
849
850         return copied;
851 }
852
853 static irqreturn_t sci_rx_interrupt(int irq, void *ptr, struct pt_regs *regs)
854 {
855         struct sci_port *port = ptr;
856
857         if (port->gs.flags & GS_ACTIVE)
858                 if (!(port->gs.flags & SCI_RX_THROTTLE)) {
859                         sci_receive_chars(port, regs);
860                         return IRQ_HANDLED;
861
862                 }
863         sci_disable_rx_interrupts(port);
864
865         return IRQ_HANDLED;
866 }
867
868 static irqreturn_t sci_tx_interrupt(int irq, void *ptr, struct pt_regs *regs)
869 {
870         struct sci_port *port = ptr;
871
872         if (port->gs.flags & GS_ACTIVE)
873                 sci_transmit_chars(port);
874         else {
875                 sci_disable_tx_interrupts(port);
876         }
877
878         return IRQ_HANDLED;
879 }
880
881 static irqreturn_t sci_er_interrupt(int irq, void *ptr, struct pt_regs *regs)
882 {
883         struct sci_port *port = ptr;
884
885         /* Handle errors */
886         if (port->type == PORT_SCI) {
887                 if(sci_handle_errors(port)) {
888                         /* discard character in rx buffer */
889                         sci_in(port, SCxSR);
890                         sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
891                 }
892         }
893         else
894                 sci_rx_interrupt(irq, ptr, regs);
895                 
896         sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
897
898         /* Kick the transmission */
899         sci_tx_interrupt(irq, ptr, regs);
900
901         return IRQ_HANDLED;
902 }
903
904 #if !defined(SCI_ONLY)
905 static irqreturn_t sci_br_interrupt(int irq, void *ptr, struct pt_regs *regs)
906 {
907         struct sci_port *port = ptr;
908
909         /* Handle BREAKs */
910         sci_handle_breaks(port);
911         sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
912
913         return IRQ_HANDLED;
914 }
915 #endif
916
917 static void do_softint(void *private_)
918 {
919         struct sci_port *port = (struct sci_port *) private_;
920         struct tty_struct       *tty;
921         
922         tty = port->gs.tty;
923         if (!tty)
924                 return;
925
926         if (test_and_clear_bit(SCI_EVENT_WRITE_WAKEUP, &port->event)) {
927                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
928                     tty->ldisc.write_wakeup)
929                         (tty->ldisc.write_wakeup)(tty);
930                 wake_up_interruptible(&tty->write_wait);
931         }
932 }
933
934 /* ********************************************************************** *
935  *                Here are the routines that actually                     *
936  *              interface with the generic_serial driver                  *
937  * ********************************************************************** */
938
939 static void sci_disable_tx_interrupts(void *ptr)
940 {
941         struct sci_port *port = ptr;
942         unsigned long flags;
943         unsigned short ctrl;
944
945         /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
946         local_irq_save(flags);
947         ctrl = sci_in(port, SCSCR);
948         ctrl &= ~SCI_CTRL_FLAGS_TIE;
949         sci_out(port, SCSCR, ctrl);
950         local_irq_restore(flags);
951 }
952
953 static void sci_enable_tx_interrupts(void *ptr)
954 {
955         struct sci_port *port = ptr; 
956
957         disable_irq(port->irqs[SCIx_TXI_IRQ]);
958         sci_transmit_chars(port);
959         enable_irq(port->irqs[SCIx_TXI_IRQ]);
960 }
961
962 static void sci_disable_rx_interrupts(void * ptr)
963 {
964         struct sci_port *port = ptr;
965         unsigned long flags;
966         unsigned short ctrl;
967
968         /* Clear RIE (Receive Interrupt Enable) bit in SCSCR */
969         local_irq_save(flags);
970         ctrl = sci_in(port, SCSCR);
971         ctrl &= ~SCI_CTRL_FLAGS_RIE;
972         sci_out(port, SCSCR, ctrl);
973         local_irq_restore(flags);
974 }
975
976 static void sci_enable_rx_interrupts(void * ptr)
977 {
978         struct sci_port *port = ptr;
979         unsigned long flags;
980         unsigned short ctrl;
981
982         /* Set RIE (Receive Interrupt Enable) bit in SCSCR */
983         local_irq_save(flags);
984         ctrl = sci_in(port, SCSCR);
985         ctrl |= SCI_CTRL_FLAGS_RIE;
986         sci_out(port, SCSCR, ctrl);
987         local_irq_restore(flags);
988 }
989
990 static int sci_get_CD(void * ptr)
991 {
992         /* If you have signal for CD (Carrier Detect), please change here. */
993         return 1;
994 }
995
996 static int sci_chars_in_buffer(void * ptr)
997 {
998         struct sci_port *port = ptr;
999
1000 #if !defined(SCI_ONLY)
1001         if (port->type == PORT_SCIF) {
1002                 return (sci_in(port, SCFDR) >> 8) + ((sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1);
1003         } else {
1004                 return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
1005         }
1006 #else
1007         return (sci_in(port, SCxSR) & SCxSR_TEND(port))? 0: 1;
1008 #endif
1009 }
1010
1011 static void sci_shutdown_port(void * ptr)
1012 {
1013         struct sci_port *port = ptr; 
1014
1015         port->gs.flags &= ~ GS_ACTIVE;
1016         if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL)
1017                 sci_setsignals(port, 0, 0);
1018         sci_free_irq(port);
1019 #if defined(__H8300S__)
1020         h8300_sci_enable(port,sci_disable);
1021 #endif
1022 }
1023
1024 /* ********************************************************************** *
1025  *                Here are the routines that actually                     *
1026  *               interface with the rest of the system                    *
1027  * ********************************************************************** */
1028
1029 static int sci_open(struct tty_struct * tty, struct file * filp)
1030 {
1031         struct sci_port *port;
1032         int retval, line;
1033
1034         line = tty->index;
1035
1036         if ((line < 0) || (line >= SCI_NPORTS))
1037                 return -ENODEV;
1038
1039         port = &sci_ports[line];
1040
1041         tty->driver_data = port;
1042         port->gs.tty = tty;
1043         port->gs.count++;
1044
1045         port->event = 0;
1046         INIT_WORK(&port->tqueue, do_softint, port);
1047
1048 #if defined(__H8300S__)
1049                 h8300_sci_enable(port,sci_enable);
1050 #endif
1051
1052         /*
1053          * Start up serial port
1054          */
1055         retval = gs_init_port(&port->gs);
1056         if (retval) {
1057                 goto failed_1;
1058         }
1059
1060         port->gs.flags |= GS_ACTIVE;
1061         sci_setsignals(port, 1,1);
1062
1063         if (port->gs.count == 1) {
1064                 retval = sci_request_irq(port);
1065         }
1066
1067         retval = gs_block_til_ready(port, filp);
1068
1069         if (retval) {
1070                 goto failed_3;
1071         }
1072
1073 #ifdef CONFIG_SERIAL_CONSOLE
1074         if (sercons.cflag && sercons.index == line) {
1075                 tty->termios->c_cflag = sercons.cflag;
1076                 port->gs.baud = sercons_baud;
1077                 sercons.cflag = 0;
1078                 sci_set_real_termios(port);
1079         }
1080 #endif
1081
1082 #ifdef CONFIG_SH_KGDB_CONSOLE
1083         if (kgdbcons.cflag && kgdbcons.index == line) {
1084                 tty->termios->c_cflag = kgdbcons.cflag;
1085                 port->gs.baud = kgdb_baud;
1086                 sercons.cflag = 0;
1087                 sci_set_real_termios(port);
1088         }
1089 #endif
1090
1091         sci_enable_rx_interrupts(port);
1092
1093         return 0;
1094
1095 failed_3:
1096         sci_free_irq(port);
1097 failed_1:
1098         port->gs.count--;
1099         return retval;
1100 }
1101
1102 static void sci_hungup(void *ptr)
1103 {
1104         return;
1105 }
1106
1107 static void sci_close(void *ptr)
1108 {
1109         return;
1110 }
1111
1112 static int sci_tiocmget(struct tty_struct *tty, struct file *file)
1113 {
1114         struct sci_port *port = tty->driver_data;
1115         return sci_getsignals(port);
1116 }
1117
1118 static int sci_tiocmset(struct tty_struct *tty, struct file *file,
1119                         unsigned int set, unsigned int clear)
1120 {
1121         struct sci_port *port = tty->driver_data;
1122         int rts = -1, dtr = -1;
1123
1124         if (set & TIOCM_RTS)
1125                 rts = 1;
1126         if (set & TIOCM_DTR)
1127                 dtr = 1;
1128         if (clear & TIOCM_RTS)
1129                 rts = 0;
1130         if (clear & TIOCM_DTR)
1131                 dtr = 0;
1132
1133         sci_setsignals(port, dtr, rts);
1134         return 0;
1135 }
1136
1137 static int sci_ioctl(struct tty_struct * tty, struct file * filp, 
1138                      unsigned int cmd, unsigned long arg)
1139 {
1140         int rc;
1141         struct sci_port *port = tty->driver_data;
1142         int ival;
1143
1144         rc = 0;
1145         switch (cmd) {
1146         case TIOCGSOFTCAR:
1147                 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1148                               (unsigned int __user *) arg);
1149                 break;
1150         case TIOCSSOFTCAR:
1151                 if ((rc = get_user(ival, (unsigned int __user *) arg)) == 0)
1152                         tty->termios->c_cflag =
1153                                 (tty->termios->c_cflag & ~CLOCAL) |
1154                                 (ival ? CLOCAL : 0);
1155                 break;
1156         case TIOCGSERIAL:
1157                 if ((rc = verify_area(VERIFY_WRITE, (void __user *) arg,
1158                                       sizeof(struct serial_struct))) == 0)
1159                         rc = gs_getserial(&port->gs, (struct serial_struct *) arg);
1160                 break;
1161         case TIOCSSERIAL:
1162                 if ((rc = verify_area(VERIFY_READ, (void __user *) arg,
1163                                       sizeof(struct serial_struct))) == 0)
1164                         rc = gs_setserial(&port->gs,
1165                                           (struct serial_struct *) arg);
1166                 break;
1167         default:
1168                 rc = -ENOIOCTLCMD;
1169                 break;
1170         }
1171
1172         return rc;
1173 }
1174
1175 static void sci_throttle(struct tty_struct * tty)
1176 {
1177         struct sci_port *port = (struct sci_port *)tty->driver_data;
1178
1179         /* If the port is using any type of input flow
1180          * control then throttle the port.
1181          */
1182         if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty)) )
1183                 port->gs.flags |= SCI_RX_THROTTLE;
1184 }
1185
1186 static void sci_unthrottle(struct tty_struct * tty)
1187 {
1188         struct sci_port *port = (struct sci_port *)tty->driver_data;
1189
1190         /* Always unthrottle even if flow control is not enabled on
1191          * this port in case we disabled flow control while the port
1192          * was throttled
1193          */
1194         port->gs.flags &= ~SCI_RX_THROTTLE;
1195         sci_enable_rx_interrupts(port);
1196         return;
1197 }
1198
1199 #ifdef CONFIG_PROC_FS
1200 static int sci_read_proc(char *page, char **start, off_t off, int count,
1201                          int *eof, void *data)
1202 {
1203         int i;
1204         struct sci_port *port;
1205         int len = 0;
1206         
1207         len += sprintf(page, "sciinfo:0.1\n");
1208         for (i = 0; i < SCI_NPORTS && len < 4000; i++) {
1209                 port = &sci_ports[i];
1210                 len += sprintf(page+len, "%d: uart:%s address: %08x", i,
1211                                (port->type == PORT_SCI) ? "SCI" : "SCIF",
1212                                port->base);
1213                 len += sprintf(page+len, " baud:%d", port->gs.baud);
1214                 len += sprintf(page+len, " tx:%d rx:%d",
1215                                port->icount.tx, port->icount.rx);
1216
1217                 if (port->icount.frame)
1218                         len += sprintf(page+len, " fe:%d", port->icount.frame);
1219                 if (port->icount.parity)
1220                         len += sprintf(page+len, " pe:%d", port->icount.parity);
1221                 if (port->icount.brk)
1222                         len += sprintf(page+len, " brk:%d", port->icount.brk);
1223                 if (port->icount.overrun)
1224                         len += sprintf(page+len, " oe:%d", port->icount.overrun);
1225                 len += sprintf(page+len, "\n");
1226         }
1227         return len;
1228 }
1229 #endif
1230
1231 #ifdef CONFIG_CPU_FREQ
1232 /*
1233  * Here we define a transistion notifier so that we can update all of our
1234  * ports' baud rate when the peripheral clock changes.
1235  */
1236
1237 static int sci_notifier(struct notifier_block *self, unsigned long phase, void *p)
1238 {
1239         struct cpufreq_freqs *freqs = p;
1240         int i;
1241
1242         if (phase == CPUFREQ_POSTCHANGE) {
1243                 for (i = 0; i < SCI_NPORTS; i++) {
1244                         /*
1245                          * This will force a baud rate change in hardware.
1246                          */
1247                         if (sci_ports[i].gs.tty != NULL) {
1248                                 sci_set_baud(&sci_ports[i], sci_ports[i].gs.baud);
1249                         }
1250                 }
1251                 printk("%s: got a postchange notification for cpu %d (old %d, new %d)\n",
1252                                 __FUNCTION__, freqs->cpu, freqs->old, freqs->new);
1253         }
1254
1255         return NOTIFY_OK;
1256 }
1257
1258 static struct notifier_block sci_nb = { &sci_notifier, NULL, 0 };
1259 #endif /* CONFIG_CPU_FREQ */
1260
1261 static struct tty_operations sci_ops = {
1262         .open   = sci_open,
1263         .close = gs_close,
1264         .write = gs_write,
1265         .put_char = gs_put_char,
1266         .flush_chars = gs_flush_chars,
1267         .write_room = gs_write_room,
1268         .chars_in_buffer = gs_chars_in_buffer,
1269         .flush_buffer = gs_flush_buffer,
1270         .ioctl = sci_ioctl,
1271         .throttle = sci_throttle,
1272         .unthrottle = sci_unthrottle,
1273         .set_termios = gs_set_termios,
1274         .stop = gs_stop,
1275         .start = gs_start,
1276         .hangup = gs_hangup,
1277 #ifdef CONFIG_PROC_FS
1278         .read_proc = sci_read_proc,
1279 #endif
1280         .tiocmget = sci_tiocmget,
1281         .tiocmset = sci_tiocmset,
1282 };
1283
1284 /* ********************************************************************** *
1285  *                    Here are the initialization routines.               *
1286  * ********************************************************************** */
1287
1288 static int sci_init_drivers(void)
1289 {
1290         int error;
1291         struct sci_port *port;
1292         sci_driver = alloc_tty_driver(SCI_NPORTS);
1293         if (!sci_driver)
1294                 return -ENOMEM;
1295
1296         sci_driver->owner = THIS_MODULE;
1297         sci_driver->driver_name = "sci";
1298         sci_driver->name = "ttySC";
1299         sci_driver->devfs_name = "ttsc/";
1300         sci_driver->major = SCI_MAJOR;
1301         sci_driver->minor_start = SCI_MINOR_START;
1302         sci_driver->type = TTY_DRIVER_TYPE_SERIAL;
1303         sci_driver->subtype = SERIAL_TYPE_NORMAL;
1304         sci_driver->init_termios = tty_std_termios;
1305         sci_driver->init_termios.c_cflag =
1306                 B9600 | CS8 | CREAD | HUPCL | CLOCAL | CRTSCTS;
1307         sci_driver->flags = TTY_DRIVER_REAL_RAW;
1308         tty_set_operations(sci_driver, &sci_ops);
1309         if ((error = tty_register_driver(sci_driver))) {
1310                 printk(KERN_ERR "sci: Couldn't register SCI driver, error = %d\n",
1311                        error);
1312                 put_tty_driver(sci_driver);
1313                 return 1;
1314         }
1315
1316         for (port = &sci_ports[0]; port < &sci_ports[SCI_NPORTS]; port++) {
1317                 port->gs.magic = SCI_MAGIC;
1318                 port->gs.close_delay = HZ/2;
1319                 port->gs.closing_wait = 30 * HZ;
1320                 port->gs.rd = &sci_real_driver;
1321                 init_waitqueue_head(&port->gs.open_wait);
1322                 init_waitqueue_head(&port->gs.close_wait);
1323                 port->old_cflag = 0;
1324                 port->icount.cts = port->icount.dsr = 
1325                         port->icount.rng = port->icount.dcd = 0;
1326                 port->icount.rx = port->icount.tx = 0;
1327                 port->icount.frame = port->icount.parity = 0;
1328                 port->icount.overrun = port->icount.brk = 0;
1329         }
1330
1331 #ifdef CONFIG_CPU_FREQ
1332         /* Setup transition notifier */
1333         if (cpufreq_register_notifier(&sci_nb, CPUFREQ_TRANSITION_NOTIFIER) < 0) {
1334                 printk(KERN_ERR "sci: Unable to register CPU frequency notifier\n");
1335                 return 1;
1336         }
1337         printk("sci: CPU frequency notifier registered\n");
1338 #endif
1339         return 0;
1340 }
1341
1342 static int sci_request_irq(struct sci_port *port)
1343 {
1344         int i;
1345 #if !defined(SCI_ONLY)
1346         irqreturn_t (*handlers[4])(int irq, void *p, struct pt_regs *regs) = {
1347                 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1348                 sci_br_interrupt,
1349         };
1350 #else
1351         void (*handlers[3])(int irq, void *ptr, struct pt_regs *regs) = {
1352                 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
1353         };
1354 #endif
1355         for (i=0; i<(sizeof(handlers)/sizeof(handlers[0])); i++) {
1356                 if (!port->irqs[i]) continue;
1357                 if (request_irq(port->irqs[i], handlers[i], SA_INTERRUPT,
1358                                 "sci", port)) {
1359                         printk(KERN_ERR "sci: Cannot allocate irq.\n");
1360                         return -ENODEV;
1361                 }
1362         }
1363         return 0;
1364 }
1365
1366 static void sci_free_irq(struct sci_port *port)
1367 {
1368         int i;
1369
1370         for (i=0; i<4; i++) {
1371                 if (!port->irqs[i]) continue;
1372                 free_irq(port->irqs[i], port);
1373         }
1374 }
1375
1376 static char banner[] __initdata =
1377         KERN_INFO "SuperH SCI(F) driver initialized\n";
1378
1379 int __init sci_init(void)
1380 {
1381         struct sci_port *port;
1382         int j;
1383
1384         printk("%s", banner);
1385
1386         for (j=0; j<SCI_NPORTS; j++) {
1387                 port = &sci_ports[j];
1388                 printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
1389                        (port->type == PORT_SCI) ? "SCI" : "SCIF");
1390         }
1391
1392         sci_init_drivers();
1393
1394 #ifdef CONFIG_SH_STANDARD_BIOS
1395         sh_bios_gdb_detach();
1396 #endif
1397         return 0;               /* Return -EIO when not detected */
1398 }
1399
1400 module_init(sci_init);
1401
1402 #ifdef MODULE
1403 #undef func_enter
1404 #undef func_exit
1405
1406 void cleanup_module(void)
1407 {
1408         tty_unregister_driver(sci_driver);
1409         put_tty_driver(sci_driver);
1410 }
1411
1412 #include "generic_serial.c"
1413 #endif
1414
1415 #ifdef CONFIG_SERIAL_CONSOLE
1416 /*
1417  *      Print a string to the serial port trying not to disturb
1418  *      any possible real use of the port...
1419  */
1420 static void serial_console_write(struct console *co, const char *s,
1421                                  unsigned count)
1422 {
1423         put_string(sercons_port, s, count);
1424 }
1425
1426 static struct tty_driver *serial_console_device(struct console *c, int *index)
1427 {
1428         *index = c->index;
1429         return sci_driver;
1430 }
1431
1432 /*
1433  *      Setup initial baud/bits/parity. We do two things here:
1434  *      - construct a cflag setting for the first rs_open()
1435  *      - initialize the serial port
1436  *      Return non-zero if we didn't find a serial port.
1437  */
1438 static int __init serial_console_setup(struct console *co, char *options)
1439 {
1440         int     baud = 9600;
1441         int     bits = 8;
1442         int     parity = 'n';
1443         int     cflag = CREAD | HUPCL | CLOCAL;
1444         char    *s;
1445
1446         sercons_port = &sci_ports[co->index];
1447
1448         if (options) {
1449                 baud = simple_strtoul(options, NULL, 10);
1450                 s = options;
1451                 while(*s >= '0' && *s <= '9')
1452                         s++;
1453                 if (*s) parity = *s++;
1454                 if (*s) bits   = *s - '0';
1455         }
1456
1457         /*
1458          *      Now construct a cflag setting.
1459          */
1460         switch (baud) {
1461                 case 19200:
1462                         cflag |= B19200;
1463                         break;
1464                 case 38400:
1465                         cflag |= B38400;
1466                         break;
1467                 case 57600:
1468                         cflag |= B57600;
1469                         break;
1470                 case 115200:
1471                         cflag |= B115200;
1472                         break;
1473                 case 9600:
1474                 default:
1475                         cflag |= B9600;
1476                         baud = 9600;
1477                         break;
1478         }
1479         switch (bits) {
1480                 case 7:
1481                         cflag |= CS7;
1482                         break;
1483                 default:
1484                 case 8:
1485                         cflag |= CS8;
1486                         break;
1487         }
1488         switch (parity) {
1489                 case 'o': case 'O':
1490                         cflag |= PARODD;
1491                         break;
1492                 case 'e': case 'E':
1493                         cflag |= PARENB;
1494                         break;
1495         }
1496
1497         co->cflag = cflag;
1498         sercons_baud = baud;
1499
1500 #if defined(__H8300S__)
1501         h8300_sci_enable(sercons_port,sci_enable);
1502 #endif
1503         sci_set_termios_cflag(sercons_port, cflag, baud);
1504         sercons_port->old_cflag = cflag;
1505
1506         return 0;
1507 }
1508
1509 static struct console sercons = {
1510         .name           = "ttySC",
1511         .write          = serial_console_write,
1512         .device         = serial_console_device,
1513         .setup          = serial_console_setup,
1514         .flags          = CON_PRINTBUFFER,
1515         .index          = -1,
1516 };
1517
1518 /*
1519  *      Register console.
1520  */
1521
1522 #ifdef CONFIG_SH_EARLY_PRINTK
1523 extern void sh_console_unregister (void);
1524 #endif
1525
1526 static int __init sci_console_init(void)
1527 {
1528         register_console(&sercons);
1529 #ifdef CONFIG_SH_EARLY_PRINTK
1530         /* Now that the real console is available, unregister the one we
1531          * used while first booting.
1532          */
1533         sh_console_unregister();
1534 #endif
1535         return 0;
1536 }
1537 console_initcall(sci_console_init);
1538
1539 #endif /* CONFIG_SERIAL_CONSOLE */
1540
1541
1542 #ifdef CONFIG_SH_KGDB
1543
1544 /* Initialise the KGDB serial port */
1545 int kgdb_sci_setup(void)
1546 {
1547         int cflag = CREAD | HUPCL | CLOCAL;
1548
1549         if ((kgdb_portnum < 0) || (kgdb_portnum >= SCI_NPORTS))
1550                 return -1;
1551
1552         kgdb_sci_port = &sci_ports[kgdb_portnum];
1553
1554         switch (kgdb_baud) {
1555         case 115200:
1556                 cflag |= B115200;
1557                 break;
1558         case 57600:
1559                 cflag |= B57600;
1560                 break;
1561         case 38400:
1562                 cflag |= B38400;
1563                 break;
1564         case 19200:
1565                 cflag |= B19200;
1566                 break;
1567         case 9600:
1568         default:
1569                 cflag |= B9600;
1570                 kgdb_baud = 9600;
1571                 break;
1572         }
1573
1574         switch (kgdb_bits) {
1575         case '7':
1576                 cflag |= CS7;
1577                 break;
1578         default:
1579         case '8':
1580                 cflag |= CS8;
1581                 break;
1582         }
1583
1584         switch (kgdb_parity) {
1585         case 'O':
1586                 cflag |= PARODD;
1587                 break;
1588         case 'E':
1589                 cflag |= PARENB;
1590                 break;
1591         }
1592
1593         kgdb_cflag = cflag;
1594         sci_set_termios_cflag(kgdb_sci_port, kgdb_cflag, kgdb_baud);
1595
1596         /* Set up the interrupt for BREAK from GDB */
1597         /* Commented out for now since it may not be possible yet...
1598            request_irq(kgdb_sci_port->irqs[0], kgdb_break_interrupt,
1599                        SA_INTERRUPT, "sci", kgdb_sci_port);
1600            sci_enable_rx_interrupts(kgdb_sci_port);
1601         */
1602
1603         /* Setup complete: initialize function pointers */
1604         kgdb_getchar = kgdb_sci_getchar;
1605         kgdb_putchar = kgdb_sci_putchar;
1606
1607         return 0;
1608 }
1609
1610 #ifdef CONFIG_SH_KGDB_CONSOLE
1611
1612 /* Create a console device */
1613 static kdev_t kgdb_console_device(struct console *c)
1614 {
1615         return MKDEV(SCI_MAJOR, SCI_MINOR_START + c->index);
1616 }
1617
1618 /* Set up the KGDB console */
1619 static int __init kgdb_console_setup(struct console *co, char *options)
1620 {
1621         /* NB we ignore 'options' because we've already done the setup */
1622         co->cflag = kgdb_cflag;
1623
1624         return 0;
1625 }
1626
1627 /* Register the KGDB console so we get messages (d'oh!) */
1628 void __init kgdb_console_init(void)
1629 {
1630         register_console(&kgdbcons);
1631 }
1632
1633 /* The console structure for KGDB */
1634 static struct console kgdbcons = {
1635         name:"ttySC",
1636         write:kgdb_console_write,
1637         device:kgdb_console_device,
1638         wait_key:serial_console_wait_key,
1639         setup:kgdb_console_setup,
1640         flags:CON_PRINTBUFFER | CON_ENABLED,
1641         index:-1,
1642 };
1643
1644 #endif /* CONFIG_SH_KGDB_CONSOLE */
1645
1646 #endif /* CONFIG_SH_KGDB */