OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [sh-sci.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.