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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [tc/] [zs.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * decserial.c: Serial port driver for IOASIC DECstations.
3
 *
4
 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5
 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6
 *
7
 * DECstation changes
8
 * Copyright (C) 1998-2000 Harald Koerfgen
9
 * Copyright (C) 2000, 2001, 2002, 2003, 2004  Maciej W. Rozycki
10
 *
11
 * For the rest of the code the original Copyright applies:
12
 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14
 *
15
 *
16
 * Note: for IOASIC systems the wiring is as follows:
17
 *
18
 * mouse/keyboard:
19
 * DIN-7 MJ-4  signal        SCC
20
 * 2     1     TxD       <-  A.TxD
21
 * 3     4     RxD       ->  A.RxD
22
 *
23
 * EIA-232/EIA-423:
24
 * DB-25 MMJ-6 signal        SCC
25
 * 2     2     TxD       <-  B.TxD
26
 * 3     5     RxD       ->  B.RxD
27
 * 4           RTS       <- ~A.RTS
28
 * 5           CTS       -> ~B.CTS
29
 * 6     6     DSR       -> ~A.SYNC
30
 * 8           CD        -> ~B.DCD
31
 * 12          DSRS(DCE) -> ~A.CTS  (*)
32
 * 15          TxC       ->  B.TxC
33
 * 17          RxC       ->  B.RxC
34
 * 20    1     DTR       <- ~A.DTR
35
 * 22          RI        -> ~A.DCD
36
 * 23          DSRS(DTE) <- ~B.RTS
37
 *
38
 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39
 *     is shared with DSRS(DTE) at pin 23.
40
 */
41
 
42
#include <linux/config.h>
43
#include <linux/version.h>
44
#include <linux/errno.h>
45
#include <linux/signal.h>
46
#include <linux/sched.h>
47
#include <linux/timer.h>
48
#include <linux/interrupt.h>
49
#include <linux/tty.h>
50
#include <linux/tty_flip.h>
51
#include <linux/major.h>
52
#include <linux/string.h>
53
#include <linux/fcntl.h>
54
#include <linux/mm.h>
55
#include <linux/kernel.h>
56
#include <linux/delay.h>
57
#include <linux/init.h>
58
#include <linux/ioport.h>
59
#ifdef CONFIG_SERIAL_DEC_CONSOLE
60
#include <linux/console.h>
61
#endif
62
 
63
#include <asm/io.h>
64
#include <asm/pgtable.h>
65
#include <asm/irq.h>
66
#include <asm/system.h>
67
#include <asm/segment.h>
68
#include <asm/bitops.h>
69
#include <asm/uaccess.h>
70
#include <asm/bootinfo.h>
71
#ifdef CONFIG_DECSTATION
72
#include <asm/dec/interrupts.h>
73
#include <asm/dec/machtype.h>
74
#include <asm/dec/tc.h>
75
#include <asm/dec/ioasic_addrs.h>
76
#endif
77
#ifdef CONFIG_BAGET_MIPS
78
#include <asm/baget/baget.h>
79
unsigned long system_base;
80
#endif
81
#ifdef CONFIG_KGDB
82
#include <asm/kgdb.h>
83
#endif
84
#ifdef CONFIG_MAGIC_SYSRQ
85
#include <linux/sysrq.h>
86
#endif
87
 
88
#include "zs.h"
89
 
90
/*
91
 * It would be nice to dynamically allocate everything that
92
 * depends on NUM_SERIAL, so we could support any number of
93
 * Z8530s, but for now...
94
 */
95
#define NUM_SERIAL      2               /* Max number of ZS chips supported */
96
#define NUM_CHANNELS    (NUM_SERIAL * 2)        /* 2 channels per chip */
97
#define CHANNEL_A_NR  (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
98
                                        /* Number of channel A in the chip */
99
#define ZS_CHAN_IO_SIZE 8
100
#define ZS_CLOCK        7372800         /* Z8530 RTxC input clock rate */
101
 
102
#define RECOVERY_DELAY  udelay(2)
103
 
104
struct zs_parms {
105
        unsigned long scc0;
106
        unsigned long scc1;
107
        int channel_a_offset;
108
        int channel_b_offset;
109
        int irq0;
110
        int irq1;
111
        int clock;
112
};
113
 
114
static struct zs_parms *zs_parms;
115
 
116
#ifdef CONFIG_DECSTATION
117
static struct zs_parms ds_parms = {
118
        scc0 : IOASIC_SCC0,
119
        scc1 : IOASIC_SCC1,
120
        channel_a_offset : 1,
121
        channel_b_offset : 9,
122
        irq0 : -1,
123
        irq1 : -1,
124
        clock : ZS_CLOCK
125
};
126
#endif
127
#ifdef CONFIG_BAGET_MIPS
128
static struct zs_parms baget_parms = {
129
        scc0 : UNI_SCC0,
130
        scc1 : UNI_SCC1,
131
        channel_a_offset : 9,
132
        channel_b_offset : 1,
133
        irq0 : BAGET_SCC_IRQ,
134
        irq1 : BAGET_SCC_IRQ,
135
        clock : 14745000
136
};
137
#endif
138
 
139
#ifdef CONFIG_DECSTATION
140
#define DS_BUS_PRESENT (IOASIC)
141
#else
142
#define DS_BUS_PRESENT 0
143
#endif
144
 
145
#ifdef CONFIG_BAGET_MIPS
146
#define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
147
#else
148
#define BAGET_BUS_PRESENT 0
149
#endif
150
 
151
#define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
152
 
153
struct dec_zschannel zs_channels[NUM_CHANNELS];
154
struct dec_serial zs_soft[NUM_CHANNELS];
155
int zs_channels_found;
156
struct dec_serial *zs_chain;    /* list of all channels */
157
 
158
struct tty_struct zs_ttys[NUM_CHANNELS];
159
 
160
#ifdef CONFIG_SERIAL_DEC_CONSOLE
161
static struct console sercons;
162
#endif
163
#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
164
    && !defined(MODULE)
165
static unsigned long break_pressed; /* break, really ... */
166
#endif
167
 
168
static unsigned char zs_init_regs[16] __initdata = {
169
        0,                               /* write 0 */
170
        0,                               /* write 1 */
171
        0,                               /* write 2 */
172
        0,                               /* write 3 */
173
        (X16CLK),                       /* write 4 */
174
        0,                               /* write 5 */
175
        0, 0, 0,                   /* write 6, 7, 8 */
176
        (MIE | DLC | NV),               /* write 9 */
177
        (NRZ),                          /* write 10 */
178
        (TCBR | RCBR),                  /* write 11 */
179
        0, 0,                             /* BRG time constant, write 12 + 13 */
180
        (BRSRC | BRENABL),              /* write 14 */
181
 
182
};
183
 
184
DECLARE_TASK_QUEUE(tq_zs_serial);
185
 
186
struct tty_driver serial_driver, callout_driver;
187
static int serial_refcount;
188
 
189
/* serial subtype definitions */
190
#define SERIAL_TYPE_NORMAL      1
191
#define SERIAL_TYPE_CALLOUT     2
192
 
193
/* number of characters left in xmit buffer before we ask for more */
194
#define WAKEUP_CHARS 256
195
 
196
/*
197
 * Debugging.
198
 */
199
#undef SERIAL_DEBUG_INTR
200
#undef SERIAL_DEBUG_OPEN
201
#undef SERIAL_DEBUG_FLOW
202
#undef SERIAL_DEBUG_THROTTLE
203
#undef SERIAL_PARANOIA_CHECK
204
 
205
#undef ZS_DEBUG_REGS
206
 
207
#ifdef SERIAL_DEBUG_THROTTLE
208
#define _tty_name(tty,buf) tty_name(tty,buf)
209
#endif
210
 
211
#define RS_STROBE_TIME 10
212
#define RS_ISR_PASS_LIMIT 256
213
 
214
#define _INLINE_ inline
215
 
216
static void probe_sccs(void);
217
static void change_speed(struct dec_serial *info);
218
static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
219
 
220
static struct tty_struct *serial_table[NUM_CHANNELS];
221
static struct termios *serial_termios[NUM_CHANNELS];
222
static struct termios *serial_termios_locked[NUM_CHANNELS];
223
 
224
#ifndef MIN
225
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
226
#endif
227
 
228
/*
229
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
230
 * lock it in case the copy_from_user blocks while swapping in a page,
231
 * and some other program tries to do a serial write at the same time.
232
 * Since the lock will only come under contention when the system is
233
 * swapping and available memory is low, it makes sense to share one
234
 * buffer across all the serial ports, since it significantly saves
235
 * memory if large numbers of serial ports are open.
236
 */
237
static unsigned char tmp_buf[4096]; /* This is cheating */
238
static DECLARE_MUTEX(tmp_buf_sem);
239
 
240
static inline int serial_paranoia_check(struct dec_serial *info,
241
                                        dev_t device, const char *routine)
242
{
243
#ifdef SERIAL_PARANOIA_CHECK
244
        static const char *badmagic =
245
                "Warning: bad magic number for serial struct (%d, %d) in %s\n";
246
        static const char *badinfo =
247
                "Warning: null mac_serial for (%d, %d) in %s\n";
248
 
249
        if (!info) {
250
                printk(badinfo, MAJOR(device), MINOR(device), routine);
251
                return 1;
252
        }
253
        if (info->magic != SERIAL_MAGIC) {
254
                printk(badmagic, MAJOR(device), MINOR(device), routine);
255
                return 1;
256
        }
257
#endif
258
        return 0;
259
}
260
 
261
/*
262
 * This is used to figure out the divisor speeds and the timeouts
263
 */
264
static int baud_table[] = {
265
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
266
        9600, 19200, 38400, 57600, 115200, 0 };
267
 
268
/*
269
 * Reading and writing Z8530 registers.
270
 */
271
static inline unsigned char read_zsreg(struct dec_zschannel *channel,
272
                                       unsigned char reg)
273
{
274
        unsigned char retval;
275
 
276
        if (reg != 0) {
277
                *channel->control = reg & 0xf;
278
                fast_iob(); RECOVERY_DELAY;
279
        }
280
        retval = *channel->control;
281
        RECOVERY_DELAY;
282
        return retval;
283
}
284
 
285
static inline void write_zsreg(struct dec_zschannel *channel,
286
                               unsigned char reg, unsigned char value)
287
{
288
        if (reg != 0) {
289
                *channel->control = reg & 0xf;
290
                fast_iob(); RECOVERY_DELAY;
291
        }
292
        *channel->control = value;
293
        fast_iob(); RECOVERY_DELAY;
294
        return;
295
}
296
 
297
static inline unsigned char read_zsdata(struct dec_zschannel *channel)
298
{
299
        unsigned char retval;
300
 
301
        retval = *channel->data;
302
        RECOVERY_DELAY;
303
        return retval;
304
}
305
 
306
static inline void write_zsdata(struct dec_zschannel *channel,
307
                                unsigned char value)
308
{
309
        *channel->data = value;
310
        fast_iob(); RECOVERY_DELAY;
311
        return;
312
}
313
 
314
static inline void load_zsregs(struct dec_zschannel *channel,
315
                               unsigned char *regs)
316
{
317
/*      ZS_CLEARERR(channel);
318
        ZS_CLEARFIFO(channel); */
319
        /* Load 'em up */
320
        write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
321
        write_zsreg(channel, R5, regs[R5] & ~TxENAB);
322
        write_zsreg(channel, R4, regs[R4]);
323
        write_zsreg(channel, R9, regs[R9]);
324
        write_zsreg(channel, R1, regs[R1]);
325
        write_zsreg(channel, R2, regs[R2]);
326
        write_zsreg(channel, R10, regs[R10]);
327
        write_zsreg(channel, R11, regs[R11]);
328
        write_zsreg(channel, R12, regs[R12]);
329
        write_zsreg(channel, R13, regs[R13]);
330
        write_zsreg(channel, R14, regs[R14]);
331
        write_zsreg(channel, R15, regs[R15]);
332
        write_zsreg(channel, R3, regs[R3]);
333
        write_zsreg(channel, R5, regs[R5]);
334
        return;
335
}
336
 
337
/* Sets or clears DTR/RTS on the requested line */
338
static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
339
{
340
        unsigned long flags;
341
 
342
 
343
        save_flags(flags); cli();
344
        if (info->zs_channel != info->zs_chan_a) {
345
                if (set) {
346
                        info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
347
                } else {
348
                        info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
349
                }
350
                write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
351
        }
352
        restore_flags(flags);
353
}
354
 
355
/* Utility routines for the Zilog */
356
static inline int get_zsbaud(struct dec_serial *ss)
357
{
358
        struct dec_zschannel *channel = ss->zs_channel;
359
        int brg;
360
 
361
        /* The baud rate is split up between two 8-bit registers in
362
         * what is termed 'BRG time constant' format in my docs for
363
         * the chip, it is a function of the clk rate the chip is
364
         * receiving which happens to be constant.
365
         */
366
        brg = (read_zsreg(channel, 13) << 8);
367
        brg |= read_zsreg(channel, 12);
368
        return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
369
}
370
 
371
/* On receive, this clears errors and the receiver interrupts */
372
static inline void rs_recv_clear(struct dec_zschannel *zsc)
373
{
374
        write_zsreg(zsc, 0, ERR_RES);
375
        write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
376
}
377
 
378
/*
379
 * ----------------------------------------------------------------------
380
 *
381
 * Here starts the interrupt handling routines.  All of the following
382
 * subroutines are declared as inline and are folded into
383
 * rs_interrupt().  They were separated out for readability's sake.
384
 *
385
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
386
 * -----------------------------------------------------------------------
387
 */
388
 
389
static int tty_break;   /* Set whenever BREAK condition is detected.  */
390
 
391
/*
392
 * This routine is used by the interrupt handler to schedule
393
 * processing in the software interrupt portion of the driver.
394
 */
395
static _INLINE_ void rs_sched_event(struct dec_serial *info,
396
                                  int event)
397
{
398
        info->event |= 1 << event;
399
        queue_task(&info->tqueue, &tq_zs_serial);
400
        mark_bh(SERIAL_BH);
401
}
402
 
403
static _INLINE_ void receive_chars(struct dec_serial *info,
404
                                   struct pt_regs *regs)
405
{
406
        struct tty_struct *tty = info->tty;
407
        unsigned char ch, stat, flag;
408
 
409
        while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
410
 
411
                stat = read_zsreg(info->zs_channel, R1);
412
                ch = read_zsdata(info->zs_channel);
413
 
414
                if (!tty && (!info->hook || !info->hook->rx_char))
415
                        continue;
416
 
417
                if (tty_break) {
418
                        tty_break = 0;
419
#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
420
                        if (info->line == sercons.index) {
421
                                if (!break_pressed) {
422
                                        break_pressed = jiffies;
423
                                        goto ignore_char;
424
                                }
425
                                break_pressed = 0;
426
                        }
427
#endif
428
                        flag = TTY_BREAK;
429
                        if (info->flags & ZILOG_SAK)
430
                                do_SAK(tty);
431
                } else {
432
                        if (stat & Rx_OVR) {
433
                                flag = TTY_OVERRUN;
434
                        } else if (stat & FRM_ERR) {
435
                                flag = TTY_FRAME;
436
                        } else if (stat & PAR_ERR) {
437
                                flag = TTY_PARITY;
438
                        } else
439
                                flag = 0;
440
                        if (flag)
441
                                /* reset the error indication */
442
                                write_zsreg(info->zs_channel, R0, ERR_RES);
443
                }
444
 
445
#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
446
                if (break_pressed && info->line == sercons.index) {
447
                        if (ch != 0 &&
448
                            time_before(jiffies, break_pressed + HZ*5)) {
449
                                handle_sysrq(ch, regs, NULL, NULL);
450
                                break_pressed = 0;
451
                                goto ignore_char;
452
                        }
453
                        break_pressed = 0;
454
                }
455
#endif
456
 
457
                if (info->hook && info->hook->rx_char) {
458
                        (*info->hook->rx_char)(ch, flag);
459
                        return;
460
                }
461
 
462
                if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
463
                        static int flip_buf_ovf;
464
                        ++flip_buf_ovf;
465
                        continue;
466
                }
467
                tty->flip.count++;
468
                {
469
                        static int flip_max_cnt;
470
                        if (flip_max_cnt < tty->flip.count)
471
                                flip_max_cnt = tty->flip.count;
472
                }
473
 
474
                *tty->flip.flag_buf_ptr++ = flag;
475
                *tty->flip.char_buf_ptr++ = ch;
476
#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
477
        ignore_char:
478
#endif
479
        }
480
        if (tty)
481
                tty_flip_buffer_push(tty);
482
}
483
 
484
static void transmit_chars(struct dec_serial *info)
485
{
486
        if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
487
                return;
488
        info->tx_active = 0;
489
 
490
        if (info->x_char) {
491
                /* Send next char */
492
                write_zsdata(info->zs_channel, info->x_char);
493
                info->x_char = 0;
494
                info->tx_active = 1;
495
                return;
496
        }
497
 
498
        if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
499
            || info->tx_stopped) {
500
                write_zsreg(info->zs_channel, R0, RES_Tx_P);
501
                return;
502
        }
503
        /* Send char */
504
        write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
505
        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
506
        info->xmit_cnt--;
507
        info->tx_active = 1;
508
 
509
        if (info->xmit_cnt < WAKEUP_CHARS)
510
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511
}
512
 
513
static _INLINE_ void status_handle(struct dec_serial *info)
514
{
515
        unsigned char stat;
516
 
517
        /* Get status from Read Register 0 */
518
        stat = read_zsreg(info->zs_channel, R0);
519
 
520
        if (stat & BRK_ABRT) {
521
#ifdef SERIAL_DEBUG_INTR
522
                printk("handling break....");
523
#endif
524
                tty_break = 1;
525
        }
526
 
527
        if (info->zs_channel != info->zs_chan_a) {
528
 
529
                /* Check for DCD transitions */
530
                if (info->tty && !C_CLOCAL(info->tty) &&
531
                    ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
532
                        if (stat & DCD) {
533
                                wake_up_interruptible(&info->open_wait);
534
                        } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
535
                                tty_hangup(info->tty);
536
                        }
537
                }
538
 
539
                /* Check for CTS transitions */
540
                if (info->tty && C_CRTSCTS(info->tty)) {
541
                        if ((stat & CTS) != 0) {
542
                                if (info->tx_stopped) {
543
                                        info->tx_stopped = 0;
544
                                        if (!info->tx_active)
545
                                                transmit_chars(info);
546
                                }
547
                        } else {
548
                                info->tx_stopped = 1;
549
                        }
550
                }
551
 
552
        }
553
 
554
        /* Clear status condition... */
555
        write_zsreg(info->zs_channel, R0, RES_EXT_INT);
556
        info->read_reg_zero = stat;
557
}
558
 
559
/*
560
 * This is the serial driver's generic interrupt routine
561
 */
562
void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
563
{
564
        struct dec_serial *info = (struct dec_serial *) dev_id;
565
        unsigned char zs_intreg;
566
        int shift;
567
 
568
        /* NOTE: The read register 3, which holds the irq status,
569
         *       does so for both channels on each chip.  Although
570
         *       the status value itself must be read from the A
571
         *       channel and is only valid when read from channel A.
572
         *       Yes... broken hardware...
573
         */
574
#define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
575
 
576
        if (info->zs_chan_a == info->zs_channel)
577
                shift = 3;      /* Channel A */
578
        else
579
                shift = 0;       /* Channel B */
580
 
581
        for (;;) {
582
                zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
583
                if ((zs_intreg & CHAN_IRQMASK) == 0)
584
                        break;
585
 
586
                if (zs_intreg & CHBRxIP) {
587
                        receive_chars(info, regs);
588
                }
589
                if (zs_intreg & CHBTxIP) {
590
                        transmit_chars(info);
591
                }
592
                if (zs_intreg & CHBEXT) {
593
                        status_handle(info);
594
                }
595
        }
596
 
597
        /* Why do we need this ? */
598
        write_zsreg(info->zs_channel, 0, RES_H_IUS);
599
}
600
 
601
#ifdef ZS_DEBUG_REGS
602
void zs_dump (void) {
603
        int i, j;
604
        for (i = 0; i < zs_channels_found; i++) {
605
                struct dec_zschannel *ch = &zs_channels[i];
606
                if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
607
                        for (j = 0; j < 15; j++) {
608
                                printk("W%d = 0x%x\t",
609
                                       j, (int)ch->curregs[j]);
610
                        }
611
                        for (j = 0; j < 15; j++) {
612
                                printk("R%d = 0x%x\t",
613
                                       j, (int)read_zsreg(ch,j));
614
                        }
615
                        printk("\n\n");
616
                }
617
        }
618
}
619
#endif
620
 
621
/*
622
 * -------------------------------------------------------------------
623
 * Here ends the serial interrupt routines.
624
 * -------------------------------------------------------------------
625
 */
626
 
627
/*
628
 * ------------------------------------------------------------
629
 * rs_stop() and rs_start()
630
 *
631
 * This routines are called before setting or resetting tty->stopped.
632
 * ------------------------------------------------------------
633
 */
634
static void rs_stop(struct tty_struct *tty)
635
{
636
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
637
        unsigned long flags;
638
 
639
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
640
                return;
641
 
642
#if 1
643
        save_flags(flags); cli();
644
        if (info->zs_channel->curregs[5] & TxENAB) {
645
                info->zs_channel->curregs[5] &= ~TxENAB;
646
                write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
647
        }
648
        restore_flags(flags);
649
#endif
650
}
651
 
652
static void rs_start(struct tty_struct *tty)
653
{
654
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
655
        unsigned long flags;
656
 
657
        if (serial_paranoia_check(info, tty->device, "rs_start"))
658
                return;
659
 
660
        save_flags(flags); cli();
661
#if 1
662
        if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
663
                info->zs_channel->curregs[5] |= TxENAB;
664
                write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
665
        }
666
#else
667
        if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
668
                transmit_chars(info);
669
        }
670
#endif
671
        restore_flags(flags);
672
}
673
 
674
/*
675
 * This routine is used to handle the "bottom half" processing for the
676
 * serial driver, known also the "software interrupt" processing.
677
 * This processing is done at the kernel interrupt level, after the
678
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
679
 * is where time-consuming activities which can not be done in the
680
 * interrupt driver proper are done; the interrupt driver schedules
681
 * them using rs_sched_event(), and they get done here.
682
 */
683
static void do_serial_bh(void)
684
{
685
        run_task_queue(&tq_zs_serial);
686
}
687
 
688
static void do_softint(void *private_)
689
{
690
        struct dec_serial       *info = (struct dec_serial *) private_;
691
        struct tty_struct       *tty;
692
 
693
        tty = info->tty;
694
        if (!tty)
695
                return;
696
 
697
        if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
698
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
699
                    tty->ldisc.write_wakeup)
700
                        (tty->ldisc.write_wakeup)(tty);
701
                wake_up_interruptible(&tty->write_wait);
702
        }
703
}
704
 
705
int zs_startup(struct dec_serial * info)
706
{
707
        unsigned long flags;
708
 
709
        if (info->flags & ZILOG_INITIALIZED)
710
                return 0;
711
 
712
        if (!info->xmit_buf) {
713
                info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
714
                if (!info->xmit_buf)
715
                        return -ENOMEM;
716
        }
717
 
718
        save_flags(flags); cli();
719
 
720
#ifdef SERIAL_DEBUG_OPEN
721
        printk("starting up ttyS%02d (irq %d)...", info->line, info->irq);
722
#endif
723
 
724
        /*
725
         * Clear the receive FIFO.
726
         */
727
        ZS_CLEARFIFO(info->zs_channel);
728
        info->xmit_fifo_size = 1;
729
 
730
        /*
731
         * Clear the interrupt registers.
732
         */
733
        write_zsreg(info->zs_channel, R0, ERR_RES);
734
        write_zsreg(info->zs_channel, R0, RES_H_IUS);
735
 
736
        /*
737
         * Set the speed of the serial port
738
         */
739
        change_speed(info);
740
 
741
        /*
742
         * Turn on RTS and DTR.
743
         */
744
        zs_rtsdtr(info, RTS | DTR, 1);
745
 
746
        /*
747
         * Finally, enable sequencing and interrupts
748
         */
749
        info->zs_channel->curregs[R1] &= ~RxINT_MASK;
750
        info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
751
                                          EXT_INT_ENAB);
752
        info->zs_channel->curregs[R3] |= RxENABLE;
753
        info->zs_channel->curregs[R5] |= TxENAB;
754
        info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
755
        write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
756
        write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
757
        write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
758
        write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
759
 
760
        /*
761
         * And clear the interrupt registers again for luck.
762
         */
763
        write_zsreg(info->zs_channel, R0, ERR_RES);
764
        write_zsreg(info->zs_channel, R0, RES_H_IUS);
765
 
766
        /* Save the current value of RR0 */
767
        info->read_reg_zero = read_zsreg(info->zs_channel, R0);
768
 
769
        if (info->tty)
770
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
771
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
772
 
773
        info->flags |= ZILOG_INITIALIZED;
774
        restore_flags(flags);
775
        return 0;
776
}
777
 
778
/*
779
 * This routine will shutdown a serial port; interrupts are disabled, and
780
 * DTR is dropped if the hangup on close termio flag is on.
781
 */
782
static void shutdown(struct dec_serial * info)
783
{
784
        unsigned long   flags;
785
 
786
        if (!(info->flags & ZILOG_INITIALIZED))
787
                return;
788
 
789
#ifdef SERIAL_DEBUG_OPEN
790
        printk("Shutting down serial port %d (irq %d)....", info->line,
791
               info->irq);
792
#endif
793
 
794
        save_flags(flags); cli(); /* Disable interrupts */
795
 
796
        if (info->xmit_buf) {
797
                free_page((unsigned long) info->xmit_buf);
798
                info->xmit_buf = 0;
799
        }
800
 
801
        info->zs_channel->curregs[1] = 0;
802
        write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
803
 
804
        info->zs_channel->curregs[3] &= ~RxENABLE;
805
        write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
806
 
807
        info->zs_channel->curregs[5] &= ~TxENAB;
808
        write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
809
        if (!info->tty || C_HUPCL(info->tty)) {
810
                zs_rtsdtr(info, RTS | DTR, 0);
811
        }
812
 
813
        if (info->tty)
814
                set_bit(TTY_IO_ERROR, &info->tty->flags);
815
 
816
        info->flags &= ~ZILOG_INITIALIZED;
817
        restore_flags(flags);
818
}
819
 
820
/*
821
 * This routine is called to set the UART divisor registers to match
822
 * the specified baud rate for a serial port.
823
 */
824
static void change_speed(struct dec_serial *info)
825
{
826
        unsigned cflag;
827
        int     i;
828
        int     brg, bits;
829
        unsigned long flags;
830
 
831
        if (!info->hook) {
832
                if (!info->tty || !info->tty->termios)
833
                        return;
834
                cflag = info->tty->termios->c_cflag;
835
                if (!info->port)
836
                        return;
837
        } else {
838
                cflag = info->hook->cflags;
839
        }
840
 
841
        i = cflag & CBAUD;
842
        if (i & CBAUDEX) {
843
                i &= ~CBAUDEX;
844
                if (i < 1 || i > 2) {
845
                        if (!info->hook)
846
                                info->tty->termios->c_cflag &= ~CBAUDEX;
847
                        else
848
                                info->hook->cflags &= ~CBAUDEX;
849
                } else
850
                        i += 15;
851
        }
852
 
853
        save_flags(flags); cli();
854
        info->zs_baud = baud_table[i];
855
        if (info->zs_baud) {
856
                brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
857
                info->zs_channel->curregs[12] = (brg & 255);
858
                info->zs_channel->curregs[13] = ((brg >> 8) & 255);
859
                zs_rtsdtr(info, DTR, 1);
860
        } else {
861
                zs_rtsdtr(info, RTS | DTR, 0);
862
                return;
863
        }
864
 
865
        /* byte size and parity */
866
        info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
867
        info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
868
        switch (cflag & CSIZE) {
869
        case CS5:
870
                bits = 7;
871
                info->zs_channel->curregs[3] |= Rx5;
872
                info->zs_channel->curregs[5] |= Tx5;
873
                break;
874
        case CS6:
875
                bits = 8;
876
                info->zs_channel->curregs[3] |= Rx6;
877
                info->zs_channel->curregs[5] |= Tx6;
878
                break;
879
        case CS7:
880
                bits = 9;
881
                info->zs_channel->curregs[3] |= Rx7;
882
                info->zs_channel->curregs[5] |= Tx7;
883
                break;
884
        case CS8:
885
        default: /* defaults to 8 bits */
886
                bits = 10;
887
                info->zs_channel->curregs[3] |= Rx8;
888
                info->zs_channel->curregs[5] |= Tx8;
889
                break;
890
        }
891
 
892
        info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
893
        info->timeout += HZ/50;         /* Add .02 seconds of slop */
894
 
895
        info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
896
        if (cflag & CSTOPB) {
897
                info->zs_channel->curregs[4] |= SB2;
898
        } else {
899
                info->zs_channel->curregs[4] |= SB1;
900
        }
901
        if (cflag & PARENB) {
902
                info->zs_channel->curregs[4] |= PAR_ENA;
903
        }
904
        if (!(cflag & PARODD)) {
905
                info->zs_channel->curregs[4] |= PAR_EVEN;
906
        }
907
 
908
        if (!(cflag & CLOCAL)) {
909
                if (!(info->zs_channel->curregs[15] & DCDIE))
910
                        info->read_reg_zero = read_zsreg(info->zs_channel, 0);
911
                info->zs_channel->curregs[15] |= DCDIE;
912
        } else
913
                info->zs_channel->curregs[15] &= ~DCDIE;
914
        if (cflag & CRTSCTS) {
915
                info->zs_channel->curregs[15] |= CTSIE;
916
                if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
917
                        info->tx_stopped = 1;
918
        } else {
919
                info->zs_channel->curregs[15] &= ~CTSIE;
920
                info->tx_stopped = 0;
921
        }
922
 
923
        /* Load up the new values */
924
        load_zsregs(info->zs_channel, info->zs_channel->curregs);
925
 
926
        restore_flags(flags);
927
}
928
 
929
static void rs_flush_chars(struct tty_struct *tty)
930
{
931
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
932
        unsigned long flags;
933
 
934
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
935
                return;
936
 
937
        if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
938
            !info->xmit_buf)
939
                return;
940
 
941
        /* Enable transmitter */
942
        save_flags(flags); cli();
943
        transmit_chars(info);
944
        restore_flags(flags);
945
}
946
 
947
static int rs_write(struct tty_struct * tty, int from_user,
948
                    const unsigned char *buf, int count)
949
{
950
        int     c, total = 0;
951
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
952
        unsigned long flags;
953
 
954
        if (serial_paranoia_check(info, tty->device, "rs_write"))
955
                return 0;
956
 
957
        if (!tty || !info->xmit_buf)
958
                return 0;
959
 
960
        save_flags(flags);
961
        while (1) {
962
                cli();
963
                c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
964
                                   SERIAL_XMIT_SIZE - info->xmit_head));
965
                if (c <= 0)
966
                        break;
967
 
968
                if (from_user) {
969
                        down(&tmp_buf_sem);
970
                        copy_from_user(tmp_buf, buf, c);
971
                        c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
972
                                       SERIAL_XMIT_SIZE - info->xmit_head));
973
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
974
                        up(&tmp_buf_sem);
975
                } else
976
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
977
                info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
978
                info->xmit_cnt += c;
979
                restore_flags(flags);
980
                buf += c;
981
                count -= c;
982
                total += c;
983
        }
984
 
985
        if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
986
            && !info->tx_active)
987
                transmit_chars(info);
988
        restore_flags(flags);
989
        return total;
990
}
991
 
992
static int rs_write_room(struct tty_struct *tty)
993
{
994
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
995
        int     ret;
996
 
997
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
998
                return 0;
999
        ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1000
        if (ret < 0)
1001
                ret = 0;
1002
        return ret;
1003
}
1004
 
1005
static int rs_chars_in_buffer(struct tty_struct *tty)
1006
{
1007
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1008
 
1009
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1010
                return 0;
1011
        return info->xmit_cnt;
1012
}
1013
 
1014
static void rs_flush_buffer(struct tty_struct *tty)
1015
{
1016
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1017
 
1018
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1019
                return;
1020
        cli();
1021
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1022
        sti();
1023
        wake_up_interruptible(&tty->write_wait);
1024
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1025
            tty->ldisc.write_wakeup)
1026
                (tty->ldisc.write_wakeup)(tty);
1027
}
1028
 
1029
/*
1030
 * ------------------------------------------------------------
1031
 * rs_throttle()
1032
 *
1033
 * This routine is called by the upper-layer tty layer to signal that
1034
 * incoming characters should be throttled.
1035
 * ------------------------------------------------------------
1036
 */
1037
static void rs_throttle(struct tty_struct * tty)
1038
{
1039
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1040
        unsigned long flags;
1041
 
1042
#ifdef SERIAL_DEBUG_THROTTLE
1043
        char    buf[64];
1044
 
1045
        printk("throttle %s: %d....\n", _tty_name(tty, buf),
1046
               tty->ldisc.chars_in_buffer(tty));
1047
#endif
1048
 
1049
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1050
                return;
1051
 
1052
        if (I_IXOFF(tty)) {
1053
                save_flags(flags); cli();
1054
                info->x_char = STOP_CHAR(tty);
1055
                if (!info->tx_active)
1056
                        transmit_chars(info);
1057
                restore_flags(flags);
1058
        }
1059
 
1060
        if (C_CRTSCTS(tty)) {
1061
                zs_rtsdtr(info, RTS, 0);
1062
        }
1063
}
1064
 
1065
static void rs_unthrottle(struct tty_struct * tty)
1066
{
1067
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1068
        unsigned long flags;
1069
 
1070
#ifdef SERIAL_DEBUG_THROTTLE
1071
        char    buf[64];
1072
 
1073
        printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1074
               tty->ldisc.chars_in_buffer(tty));
1075
#endif
1076
 
1077
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1078
                return;
1079
 
1080
        if (I_IXOFF(tty)) {
1081
                save_flags(flags); cli();
1082
                if (info->x_char)
1083
                        info->x_char = 0;
1084
                else {
1085
                        info->x_char = START_CHAR(tty);
1086
                        if (!info->tx_active)
1087
                                transmit_chars(info);
1088
                }
1089
                restore_flags(flags);
1090
        }
1091
 
1092
        if (C_CRTSCTS(tty)) {
1093
                zs_rtsdtr(info, RTS, 1);
1094
        }
1095
}
1096
 
1097
/*
1098
 * ------------------------------------------------------------
1099
 * rs_ioctl() and friends
1100
 * ------------------------------------------------------------
1101
 */
1102
 
1103
static int get_serial_info(struct dec_serial * info,
1104
                           struct serial_struct * retinfo)
1105
{
1106
        struct serial_struct tmp;
1107
 
1108
        if (!retinfo)
1109
                return -EFAULT;
1110
        memset(&tmp, 0, sizeof(tmp));
1111
        tmp.type = info->type;
1112
        tmp.line = info->line;
1113
        tmp.port = info->port;
1114
        tmp.irq = info->irq;
1115
        tmp.flags = info->flags;
1116
        tmp.baud_base = info->baud_base;
1117
        tmp.close_delay = info->close_delay;
1118
        tmp.closing_wait = info->closing_wait;
1119
        tmp.custom_divisor = info->custom_divisor;
1120
        return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1121
}
1122
 
1123
static int set_serial_info(struct dec_serial * info,
1124
                           struct serial_struct * new_info)
1125
{
1126
        struct serial_struct new_serial;
1127
        struct dec_serial old_info;
1128
        int                     retval = 0;
1129
 
1130
        if (!new_info)
1131
                return -EFAULT;
1132
        copy_from_user(&new_serial,new_info,sizeof(new_serial));
1133
        old_info = *info;
1134
 
1135
        if (!capable(CAP_SYS_ADMIN)) {
1136
                if ((new_serial.baud_base != info->baud_base) ||
1137
                    (new_serial.type != info->type) ||
1138
                    (new_serial.close_delay != info->close_delay) ||
1139
                    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1140
                     (info->flags & ~ZILOG_USR_MASK)))
1141
                        return -EPERM;
1142
                info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1143
                               (new_serial.flags & ZILOG_USR_MASK));
1144
                info->custom_divisor = new_serial.custom_divisor;
1145
                goto check_and_exit;
1146
        }
1147
 
1148
        if (info->count > 1)
1149
                return -EBUSY;
1150
 
1151
        /*
1152
         * OK, past this point, all the error checking has been done.
1153
         * At this point, we start making changes.....
1154
         */
1155
 
1156
        info->baud_base = new_serial.baud_base;
1157
        info->flags = ((info->flags & ~ZILOG_FLAGS) |
1158
                        (new_serial.flags & ZILOG_FLAGS));
1159
        info->type = new_serial.type;
1160
        info->close_delay = new_serial.close_delay;
1161
        info->closing_wait = new_serial.closing_wait;
1162
 
1163
check_and_exit:
1164
        retval = zs_startup(info);
1165
        return retval;
1166
}
1167
 
1168
/*
1169
 * get_lsr_info - get line status register info
1170
 *
1171
 * Purpose: Let user call ioctl() to get info when the UART physically
1172
 *          is emptied.  On bus types like RS485, the transmitter must
1173
 *          release the bus after transmitting. This must be done when
1174
 *          the transmit shift register is empty, not be done when the
1175
 *          transmit holding register is empty.  This functionality
1176
 *          allows an RS485 driver to be written in user space.
1177
 */
1178
static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1179
{
1180
        unsigned char status;
1181
 
1182
        cli();
1183
        status = read_zsreg(info->zs_channel, 0);
1184
        sti();
1185
        put_user(status,value);
1186
        return 0;
1187
}
1188
 
1189
static int get_modem_info(struct dec_serial *info, unsigned int *value)
1190
{
1191
        unsigned char control, status_a, status_b;
1192
        unsigned int result;
1193
 
1194
        if (info->zs_channel == info->zs_chan_a)
1195
                result = 0;
1196
        else {
1197
                cli();
1198
                control = info->zs_chan_a->curregs[5];
1199
                status_a = read_zsreg(info->zs_chan_a, 0);
1200
                status_b = read_zsreg(info->zs_channel, 0);
1201
                sti();
1202
                result =  ((control  & RTS) ? TIOCM_RTS: 0)
1203
                        | ((control  & DTR) ? TIOCM_DTR: 0)
1204
                        | ((status_b & DCD) ? TIOCM_CAR: 0)
1205
                        | ((status_a & DCD) ? TIOCM_RNG: 0)
1206
                        | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1207
                        | ((status_b & CTS) ? TIOCM_CTS: 0);
1208
        }
1209
        put_user(result, value);
1210
        return 0;
1211
}
1212
 
1213
static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1214
                          unsigned int *value)
1215
{
1216
        int error;
1217
        unsigned int arg, bits;
1218
 
1219
        error = verify_area(VERIFY_READ, value, sizeof(int));
1220
        if (error)
1221
                return error;
1222
 
1223
        if (info->zs_channel == info->zs_chan_a)
1224
                return 0;
1225
 
1226
        get_user(arg, value);
1227
        bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1228
        cli();
1229
        switch (cmd) {
1230
        case TIOCMBIS:
1231
                info->zs_chan_a->curregs[5] |= bits;
1232
                break;
1233
        case TIOCMBIC:
1234
                info->zs_chan_a->curregs[5] &= ~bits;
1235
                break;
1236
        case TIOCMSET:
1237
                info->zs_chan_a->curregs[5] =
1238
                        (info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1239
                break;
1240
        default:
1241
                sti();
1242
                return -EINVAL;
1243
        }
1244
        write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1245
        sti();
1246
        return 0;
1247
}
1248
 
1249
/*
1250
 * rs_break - turn transmit break condition on/off
1251
 */
1252
static void rs_break(struct tty_struct *tty, int break_state)
1253
{
1254
        struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1255
        unsigned long flags;
1256
 
1257
        if (serial_paranoia_check(info, tty->device, "rs_break"))
1258
                return;
1259
        if (!info->port)
1260
                return;
1261
 
1262
        save_flags(flags); cli();
1263
        if (break_state == -1)
1264
                info->zs_channel->curregs[5] |= SND_BRK;
1265
        else
1266
                info->zs_channel->curregs[5] &= ~SND_BRK;
1267
        write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1268
        restore_flags(flags);
1269
}
1270
 
1271
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1272
                    unsigned int cmd, unsigned long arg)
1273
{
1274
        int error;
1275
        struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1276
 
1277
        if (info->hook)
1278
                return -ENODEV;
1279
 
1280
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1281
                return -ENODEV;
1282
 
1283
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1284
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1285
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1286
                if (tty->flags & (1 << TTY_IO_ERROR))
1287
                    return -EIO;
1288
        }
1289
 
1290
        switch (cmd) {
1291
                case TIOCMGET:
1292
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1293
                                sizeof(unsigned int));
1294
                        if (error)
1295
                                return error;
1296
                        return get_modem_info(info, (unsigned int *) arg);
1297
                case TIOCMBIS:
1298
                case TIOCMBIC:
1299
                case TIOCMSET:
1300
                        return set_modem_info(info, cmd, (unsigned int *) arg);
1301
                case TIOCGSERIAL:
1302
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1303
                                                sizeof(struct serial_struct));
1304
                        if (error)
1305
                                return error;
1306
                        return get_serial_info(info,
1307
                                               (struct serial_struct *) arg);
1308
                case TIOCSSERIAL:
1309
                        return set_serial_info(info,
1310
                                               (struct serial_struct *) arg);
1311
                case TIOCSERGETLSR: /* Get line status register */
1312
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1313
                                sizeof(unsigned int));
1314
                        if (error)
1315
                                return error;
1316
                        else
1317
                            return get_lsr_info(info, (unsigned int *) arg);
1318
 
1319
                case TIOCSERGSTRUCT:
1320
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1321
                                                sizeof(struct dec_serial));
1322
                        if (error)
1323
                                return error;
1324
                        copy_from_user((struct dec_serial *) arg,
1325
                                       info, sizeof(struct dec_serial));
1326
                        return 0;
1327
 
1328
                default:
1329
                        return -ENOIOCTLCMD;
1330
                }
1331
        return 0;
1332
}
1333
 
1334
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1335
{
1336
        struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1337
        int was_stopped;
1338
 
1339
        if (tty->termios->c_cflag == old_termios->c_cflag)
1340
                return;
1341
        was_stopped = info->tx_stopped;
1342
 
1343
        change_speed(info);
1344
 
1345
        if (was_stopped && !info->tx_stopped)
1346
                rs_start(tty);
1347
}
1348
 
1349
/*
1350
 * ------------------------------------------------------------
1351
 * rs_close()
1352
 *
1353
 * This routine is called when the serial port gets closed.
1354
 * Wait for the last remaining data to be sent.
1355
 * ------------------------------------------------------------
1356
 */
1357
static void rs_close(struct tty_struct *tty, struct file * filp)
1358
{
1359
        struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1360
        unsigned long flags;
1361
 
1362
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1363
                return;
1364
 
1365
        save_flags(flags); cli();
1366
 
1367
        if (tty_hung_up_p(filp)) {
1368
                restore_flags(flags);
1369
                return;
1370
        }
1371
 
1372
#ifdef SERIAL_DEBUG_OPEN
1373
        printk("rs_close ttyS%02d, count = %d\n", info->line, info->count);
1374
#endif
1375
        if ((tty->count == 1) && (info->count != 1)) {
1376
                /*
1377
                 * Uh, oh.  tty->count is 1, which means that the tty
1378
                 * structure will be freed.  Info->count should always
1379
                 * be one in these conditions.  If it's greater than
1380
                 * one, we've got real problems, since it means the
1381
                 * serial port won't be shutdown.
1382
                 */
1383
                printk("rs_close: bad serial port count; tty->count is 1, "
1384
                       "info->count is %d\n", info->count);
1385
                info->count = 1;
1386
        }
1387
        if (--info->count < 0) {
1388
                printk("rs_close: bad serial port count for ttyS%02d: %d\n",
1389
                       info->line, info->count);
1390
                info->count = 0;
1391
        }
1392
        if (info->count) {
1393
                restore_flags(flags);
1394
                return;
1395
        }
1396
        info->flags |= ZILOG_CLOSING;
1397
        /*
1398
         * Save the termios structure, since this port may have
1399
         * separate termios for callout and dialin.
1400
         */
1401
        if (info->flags & ZILOG_NORMAL_ACTIVE)
1402
                info->normal_termios = *tty->termios;
1403
        if (info->flags & ZILOG_CALLOUT_ACTIVE)
1404
                info->callout_termios = *tty->termios;
1405
        /*
1406
         * Now we wait for the transmit buffer to clear; and we notify
1407
         * the line discipline to only process XON/XOFF characters.
1408
         */
1409
        tty->closing = 1;
1410
        if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1411
                tty_wait_until_sent(tty, info->closing_wait);
1412
        /*
1413
         * At this point we stop accepting input.  To do this, we
1414
         * disable the receiver and receive interrupts.
1415
         */
1416
        info->zs_channel->curregs[3] &= ~RxENABLE;
1417
        write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1418
        info->zs_channel->curregs[1] = 0;        /* disable any rx ints */
1419
        write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1420
        ZS_CLEARFIFO(info->zs_channel);
1421
        if (info->flags & ZILOG_INITIALIZED) {
1422
                /*
1423
                 * Before we drop DTR, make sure the SCC transmitter
1424
                 * has completely drained.
1425
                 */
1426
                rs_wait_until_sent(tty, info->timeout);
1427
        }
1428
 
1429
        shutdown(info);
1430
        if (tty->driver.flush_buffer)
1431
                tty->driver.flush_buffer(tty);
1432
        if (tty->ldisc.flush_buffer)
1433
                tty->ldisc.flush_buffer(tty);
1434
        tty->closing = 0;
1435
        info->event = 0;
1436
        info->tty = 0;
1437
        if (info->blocked_open) {
1438
                if (info->close_delay) {
1439
                        current->state = TASK_INTERRUPTIBLE;
1440
                        schedule_timeout(info->close_delay);
1441
                }
1442
                wake_up_interruptible(&info->open_wait);
1443
        }
1444
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1445
                         ZILOG_CLOSING);
1446
        wake_up_interruptible(&info->close_wait);
1447
        restore_flags(flags);
1448
}
1449
 
1450
/*
1451
 * rs_wait_until_sent() --- wait until the transmitter is empty
1452
 */
1453
static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1454
{
1455
        struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1456
        unsigned long orig_jiffies, char_time;
1457
 
1458
        if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1459
                return;
1460
 
1461
        orig_jiffies = jiffies;
1462
        /*
1463
         * Set the check interval to be 1/5 of the estimated time to
1464
         * send a single character, and make it at least 1.  The check
1465
         * interval should also be less than the timeout.
1466
         */
1467
        char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1468
        char_time = char_time / 5;
1469
        if (char_time == 0)
1470
                char_time = 1;
1471
        if (timeout)
1472
                char_time = MIN(char_time, timeout);
1473
        while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1474
                current->state = TASK_INTERRUPTIBLE;
1475
                schedule_timeout(char_time);
1476
                if (signal_pending(current))
1477
                        break;
1478
                if (timeout && time_after(jiffies, orig_jiffies + timeout))
1479
                        break;
1480
        }
1481
        current->state = TASK_RUNNING;
1482
}
1483
 
1484
/*
1485
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1486
 */
1487
void rs_hangup(struct tty_struct *tty)
1488
{
1489
        struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1490
 
1491
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1492
                return;
1493
 
1494
        rs_flush_buffer(tty);
1495
        shutdown(info);
1496
        info->event = 0;
1497
        info->count = 0;
1498
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1499
        info->tty = 0;
1500
        wake_up_interruptible(&info->open_wait);
1501
}
1502
 
1503
/*
1504
 * ------------------------------------------------------------
1505
 * rs_open() and friends
1506
 * ------------------------------------------------------------
1507
 */
1508
static int block_til_ready(struct tty_struct *tty, struct file * filp,
1509
                           struct dec_serial *info)
1510
{
1511
        DECLARE_WAITQUEUE(wait, current);
1512
        int             retval;
1513
        int             do_clocal = 0;
1514
 
1515
        /*
1516
         * If the device is in the middle of being closed, then block
1517
         * until it's done, and then try again.
1518
         */
1519
        if (info->flags & ZILOG_CLOSING) {
1520
                interruptible_sleep_on(&info->close_wait);
1521
#ifdef SERIAL_DO_RESTART
1522
                return ((info->flags & ZILOG_HUP_NOTIFY) ?
1523
                        -EAGAIN : -ERESTARTSYS);
1524
#else
1525
                return -EAGAIN;
1526
#endif
1527
        }
1528
 
1529
        /*
1530
         * If this is a callout device, then just make sure the normal
1531
         * device isn't being used.
1532
         */
1533
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1534
                if (info->flags & ZILOG_NORMAL_ACTIVE)
1535
                        return -EBUSY;
1536
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1537
                    (info->flags & ZILOG_SESSION_LOCKOUT) &&
1538
                    (info->session != current->session))
1539
                    return -EBUSY;
1540
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1541
                    (info->flags & ZILOG_PGRP_LOCKOUT) &&
1542
                    (info->pgrp != current->pgrp))
1543
                    return -EBUSY;
1544
                info->flags |= ZILOG_CALLOUT_ACTIVE;
1545
                return 0;
1546
        }
1547
 
1548
        /*
1549
         * If non-blocking mode is set, or the port is not enabled,
1550
         * then make the check up front and then exit.
1551
         */
1552
        if ((filp->f_flags & O_NONBLOCK) ||
1553
            (tty->flags & (1 << TTY_IO_ERROR))) {
1554
                if (info->flags & ZILOG_CALLOUT_ACTIVE)
1555
                        return -EBUSY;
1556
                info->flags |= ZILOG_NORMAL_ACTIVE;
1557
                return 0;
1558
        }
1559
 
1560
        if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1561
                if (info->normal_termios.c_cflag & CLOCAL)
1562
                        do_clocal = 1;
1563
        } else {
1564
                if (tty->termios->c_cflag & CLOCAL)
1565
                        do_clocal = 1;
1566
        }
1567
 
1568
        /*
1569
         * Block waiting for the carrier detect and the line to become
1570
         * free (i.e., not in use by the callout).  While we are in
1571
         * this loop, info->count is dropped by one, so that
1572
         * rs_close() knows when to free things.  We restore it upon
1573
         * exit, either normal or abnormal.
1574
         */
1575
        retval = 0;
1576
        add_wait_queue(&info->open_wait, &wait);
1577
#ifdef SERIAL_DEBUG_OPEN
1578
        printk("block_til_ready before block: ttyS%02d, count = %d\n",
1579
               info->line, info->count);
1580
#endif
1581
        cli();
1582
        if (!tty_hung_up_p(filp))
1583
                info->count--;
1584
        sti();
1585
        info->blocked_open++;
1586
        while (1) {
1587
                cli();
1588
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1589
                    (tty->termios->c_cflag & CBAUD))
1590
                        zs_rtsdtr(info, RTS | DTR, 1);
1591
                sti();
1592
                set_current_state(TASK_INTERRUPTIBLE);
1593
                if (tty_hung_up_p(filp) ||
1594
                    !(info->flags & ZILOG_INITIALIZED)) {
1595
#ifdef SERIAL_DO_RESTART
1596
                        if (info->flags & ZILOG_HUP_NOTIFY)
1597
                                retval = -EAGAIN;
1598
                        else
1599
                                retval = -ERESTARTSYS;
1600
#else
1601
                        retval = -EAGAIN;
1602
#endif
1603
                        break;
1604
                }
1605
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1606
                    !(info->flags & ZILOG_CLOSING) &&
1607
                    (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1608
                        break;
1609
                if (signal_pending(current)) {
1610
                        retval = -ERESTARTSYS;
1611
                        break;
1612
                }
1613
#ifdef SERIAL_DEBUG_OPEN
1614
                printk("block_til_ready blocking: ttyS%02d, count = %d\n",
1615
                       info->line, info->count);
1616
#endif
1617
                schedule();
1618
        }
1619
        current->state = TASK_RUNNING;
1620
        remove_wait_queue(&info->open_wait, &wait);
1621
        if (!tty_hung_up_p(filp))
1622
                info->count++;
1623
        info->blocked_open--;
1624
#ifdef SERIAL_DEBUG_OPEN
1625
        printk("block_til_ready after blocking: ttyS%02d, count = %d\n",
1626
               info->line, info->count);
1627
#endif
1628
        if (retval)
1629
                return retval;
1630
        info->flags |= ZILOG_NORMAL_ACTIVE;
1631
        return 0;
1632
}
1633
 
1634
/*
1635
 * This routine is called whenever a serial port is opened.  It
1636
 * enables interrupts for a serial port, linking in its ZILOG structure into
1637
 * the IRQ chain.   It also performs the serial-specific
1638
 * initialization for the tty structure.
1639
 */
1640
int rs_open(struct tty_struct *tty, struct file * filp)
1641
{
1642
        struct dec_serial       *info;
1643
        int                     retval, line;
1644
 
1645
        line = MINOR(tty->device) - tty->driver.minor_start;
1646
        if ((line < 0) || (line >= zs_channels_found))
1647
                return -ENODEV;
1648
        info = zs_soft + line;
1649
 
1650
        if (info->hook)
1651
                return -ENODEV;
1652
 
1653
        if (serial_paranoia_check(info, tty->device, "rs_open"))
1654
                return -ENODEV;
1655
#ifdef SERIAL_DEBUG_OPEN
1656
        printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1657
               info->count);
1658
#endif
1659
 
1660
        info->count++;
1661
        tty->driver_data = info;
1662
        info->tty = tty;
1663
 
1664
        /*
1665
         * If the port is the middle of closing, bail out now
1666
         */
1667
        if (tty_hung_up_p(filp) ||
1668
            (info->flags & ZILOG_CLOSING)) {
1669
                if (info->flags & ZILOG_CLOSING)
1670
                        interruptible_sleep_on(&info->close_wait);
1671
#ifdef SERIAL_DO_RESTART
1672
                return ((info->flags & ZILOG_HUP_NOTIFY) ?
1673
                        -EAGAIN : -ERESTARTSYS);
1674
#else
1675
                return -EAGAIN;
1676
#endif
1677
        }
1678
 
1679
        /*
1680
         * Start up serial port
1681
         */
1682
        retval = zs_startup(info);
1683
        if (retval)
1684
                return retval;
1685
 
1686
        retval = block_til_ready(tty, filp, info);
1687
        if (retval) {
1688
#ifdef SERIAL_DEBUG_OPEN
1689
                printk("rs_open returning after block_til_ready with %d\n",
1690
                       retval);
1691
#endif
1692
                return retval;
1693
        }
1694
 
1695
        if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1696
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1697
                        *tty->termios = info->normal_termios;
1698
                else
1699
                        *tty->termios = info->callout_termios;
1700
                change_speed(info);
1701
        }
1702
#ifdef CONFIG_SERIAL_DEC_CONSOLE
1703
        if (sercons.cflag && sercons.index == line) {
1704
                tty->termios->c_cflag = sercons.cflag;
1705
                sercons.cflag = 0;
1706
                change_speed(info);
1707
        }
1708
#endif
1709
 
1710
        info->session = current->session;
1711
        info->pgrp = current->pgrp;
1712
 
1713
#ifdef SERIAL_DEBUG_OPEN
1714
        printk("rs_open ttyS%02d successful...", info->line);
1715
#endif
1716
/* tty->low_latency = 1; */
1717
        return 0;
1718
}
1719
 
1720
/* Finally, routines used to initialize the serial driver. */
1721
 
1722
static void __init show_serial_version(void)
1723
{
1724
        printk("DECstation Z8530 serial driver version 0.08\n");
1725
}
1726
 
1727
/*  Initialize Z8530s zs_channels
1728
 */
1729
 
1730
static void __init probe_sccs(void)
1731
{
1732
        struct dec_serial **pp;
1733
        int i, n, n_chips = 0, n_channels, chip, channel;
1734
        unsigned long flags;
1735
 
1736
        /*
1737
         * did we get here by accident?
1738
         */
1739
        if(!BUS_PRESENT) {
1740
                printk("Not on JUNKIO machine, skipping probe_sccs\n");
1741
                return;
1742
        }
1743
 
1744
        /*
1745
         * When serial console is activated, tc_init has not been called yet
1746
         * and system_base is undefined. Unfortunately we have to hardcode
1747
         * system_base for this case :-(. HK
1748
         */
1749
        switch(mips_machtype) {
1750
#ifdef CONFIG_DECSTATION
1751
        case MACH_DS5000_2X0:
1752
        case MACH_DS5900:
1753
                system_base = KSEG1ADDR(0x1f800000);
1754
                n_chips = 2;
1755
                zs_parms = &ds_parms;
1756
                zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1757
                zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1758
                break;
1759
        case MACH_DS5000_1XX:
1760
                system_base = KSEG1ADDR(0x1c000000);
1761
                n_chips = 2;
1762
                zs_parms = &ds_parms;
1763
                zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1764
                zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1765
                break;
1766
        case MACH_DS5000_XX:
1767
                system_base = KSEG1ADDR(0x1c000000);
1768
                n_chips = 1;
1769
                zs_parms = &ds_parms;
1770
                zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1771
                break;
1772
#endif
1773
#ifdef CONFIG_BAGET_MIPS
1774
        case MACH_BAGET202:
1775
                system_base = UNI_IO_BASE;
1776
                n_chips = 2;
1777
                zs_parms = &baget_parms;
1778
                zs_init_regs[2] = 0x8;
1779
                break;
1780
#endif
1781
        default:
1782
                panic("zs: unsupported bus");
1783
        }
1784
        if (!zs_parms)
1785
                panic("zs: uninitialized parms");
1786
 
1787
        pp = &zs_chain;
1788
 
1789
        n_channels = 0;
1790
 
1791
        for (chip = 0; chip < n_chips; chip++) {
1792
                for (channel = 0; channel <= 1; channel++) {
1793
                        /*
1794
                         * The sccs reside on the high byte of the 16 bit IOBUS
1795
                         */
1796
                        zs_channels[n_channels].control =
1797
                                (volatile unsigned char *)system_base +
1798
                          (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1799
                          (0 == channel ? zs_parms->channel_a_offset :
1800
                                          zs_parms->channel_b_offset);
1801
                        zs_channels[n_channels].data =
1802
                                zs_channels[n_channels].control + 4;
1803
 
1804
#ifndef CONFIG_SERIAL_DEC_CONSOLE
1805
                        /*
1806
                         * We're called early and memory managment isn't up, yet.
1807
                         * Thus check_region would fail.
1808
                         */
1809
                        if (check_region((unsigned long)
1810
                                         zs_channels[n_channels].control,
1811
                                         ZS_CHAN_IO_SIZE) < 0) {
1812
                                panic("SCC I/O region is not free");
1813
                        }
1814
                        request_region((unsigned long)
1815
                                       zs_channels[n_channels].control,
1816
                                       ZS_CHAN_IO_SIZE, "SCC");
1817
#endif
1818
                        zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1819
                        /* HACK alert! */
1820
                        if (!(chip & 1))
1821
                                zs_soft[n_channels].irq = zs_parms->irq0;
1822
                        else
1823
                                zs_soft[n_channels].irq = zs_parms->irq1;
1824
 
1825
                        /*
1826
                         *  Identification of channel A. Location of channel A
1827
                         *  inside chip depends on mapping of internal address
1828
                         *  the chip decodes channels by.
1829
                         *  CHANNEL_A_NR returns either 0 (in case of
1830
                         *  DECstations) or 1 (in case of Baget).
1831
                         */
1832
                        if (CHANNEL_A_NR == channel)
1833
                                zs_soft[n_channels].zs_chan_a =
1834
                                    &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1835
                        else
1836
                                zs_soft[n_channels].zs_chan_a =
1837
                                    &zs_channels[n_channels];
1838
 
1839
                        *pp = &zs_soft[n_channels];
1840
                        pp = &zs_soft[n_channels].zs_next;
1841
                        n_channels++;
1842
                }
1843
        }
1844
 
1845
        *pp = 0;
1846
        zs_channels_found = n_channels;
1847
 
1848
        for (n = 0; n < zs_channels_found; n++) {
1849
                for (i = 0; i < 16; i++) {
1850
                        zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1851
                }
1852
        }
1853
 
1854
        save_and_cli(flags);
1855
        for (n = 0; n < zs_channels_found; n++) {
1856
                if (n % 2 == 0) {
1857
                        write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1858
                        udelay(10);
1859
                        write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1860
                }
1861
                load_zsregs(zs_soft[n].zs_channel,
1862
                            zs_soft[n].zs_channel->curregs);
1863
        }
1864
        restore_flags(flags);
1865
}
1866
 
1867
/* zs_init inits the driver */
1868
int __init zs_init(void)
1869
{
1870
        int channel, i;
1871
        struct dec_serial *info;
1872
 
1873
        if(!BUS_PRESENT)
1874
                return -ENODEV;
1875
 
1876
        /* Setup base handler, and timer table. */
1877
        init_bh(SERIAL_BH, do_serial_bh);
1878
 
1879
        /* Find out how many Z8530 SCCs we have */
1880
        if (zs_chain == 0)
1881
                probe_sccs();
1882
 
1883
        show_serial_version();
1884
 
1885
        /* Initialize the tty_driver structure */
1886
        /* Not all of this is exactly right for us. */
1887
 
1888
        memset(&serial_driver, 0, sizeof(struct tty_driver));
1889
        serial_driver.magic = TTY_DRIVER_MAGIC;
1890
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1891
        serial_driver.name = "tts/%d";
1892
#else
1893
        serial_driver.name = "ttyS";
1894
#endif
1895
        serial_driver.major = TTY_MAJOR;
1896
        serial_driver.minor_start = 64;
1897
        serial_driver.num = zs_channels_found;
1898
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1899
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
1900
        serial_driver.init_termios = tty_std_termios;
1901
 
1902
        serial_driver.init_termios.c_cflag =
1903
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1904
        serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1905
        serial_driver.refcount = &serial_refcount;
1906
        serial_driver.table = serial_table;
1907
        serial_driver.termios = serial_termios;
1908
        serial_driver.termios_locked = serial_termios_locked;
1909
 
1910
        serial_driver.open = rs_open;
1911
        serial_driver.close = rs_close;
1912
        serial_driver.write = rs_write;
1913
        serial_driver.flush_chars = rs_flush_chars;
1914
        serial_driver.write_room = rs_write_room;
1915
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
1916
        serial_driver.flush_buffer = rs_flush_buffer;
1917
        serial_driver.ioctl = rs_ioctl;
1918
        serial_driver.throttle = rs_throttle;
1919
        serial_driver.unthrottle = rs_unthrottle;
1920
        serial_driver.set_termios = rs_set_termios;
1921
        serial_driver.stop = rs_stop;
1922
        serial_driver.start = rs_start;
1923
        serial_driver.hangup = rs_hangup;
1924
        serial_driver.break_ctl = rs_break;
1925
        serial_driver.wait_until_sent = rs_wait_until_sent;
1926
 
1927
        /*
1928
         * The callout device is just like normal device except for
1929
         * major number and the subtype code.
1930
         */
1931
        callout_driver = serial_driver;
1932
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1933
        callout_driver.name = "cua/%d";
1934
#else
1935
        callout_driver.name = "cua";
1936
#endif
1937
        callout_driver.major = TTYAUX_MAJOR;
1938
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1939
 
1940
        if (tty_register_driver(&serial_driver))
1941
                panic("Couldn't register serial driver");
1942
        if (tty_register_driver(&callout_driver))
1943
                panic("Couldn't register callout driver");
1944
 
1945
        for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1946
 
1947
                /* Needed before interrupts are enabled. */
1948
                info->tty = 0;
1949
                info->x_char = 0;
1950
 
1951
                if (info->hook && info->hook->init_info) {
1952
                        (*info->hook->init_info)(info);
1953
                        continue;
1954
                }
1955
 
1956
                info->magic = SERIAL_MAGIC;
1957
                info->port = (int) info->zs_channel->control;
1958
                info->line = i;
1959
                info->custom_divisor = 16;
1960
                info->close_delay = 50;
1961
                info->closing_wait = 3000;
1962
                info->event = 0;
1963
                info->count = 0;
1964
                info->blocked_open = 0;
1965
                info->tqueue.routine = do_softint;
1966
                info->tqueue.data = info;
1967
                info->callout_termios = callout_driver.init_termios;
1968
                info->normal_termios = serial_driver.init_termios;
1969
                init_waitqueue_head(&info->open_wait);
1970
                init_waitqueue_head(&info->close_wait);
1971
                printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1972
                       info->line, info->port, info->irq);
1973
                tty_register_devfs(&serial_driver, 0,
1974
                                   serial_driver.minor_start + info->line);
1975
                tty_register_devfs(&callout_driver, 0,
1976
                                   callout_driver.minor_start + info->line);
1977
 
1978
        }
1979
 
1980
        for (channel = 0; channel < zs_channels_found; ++channel) {
1981
                zs_soft[channel].clk_divisor = 16;
1982
                zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1983
 
1984
                if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1985
                                "scc", &zs_soft[channel]))
1986
                        printk(KERN_ERR "decserial: can't get irq %d\n",
1987
                               zs_soft[channel].irq);
1988
 
1989
                if (zs_soft[channel].hook) {
1990
                        zs_startup(&zs_soft[channel]);
1991
                        if (zs_soft[channel].hook->init_channel)
1992
                                (*zs_soft[channel].hook->init_channel)
1993
                                        (&zs_soft[channel]);
1994
                }
1995
        }
1996
 
1997
        return 0;
1998
}
1999
 
2000
/*
2001
 * polling I/O routines
2002
 */
2003
static int
2004
zs_poll_tx_char(struct dec_serial *info, unsigned char ch)
2005
{
2006
        struct dec_zschannel *chan = info->zs_channel;
2007
        int    ret;
2008
 
2009
        if(chan) {
2010
                int loops = 10000;
2011
 
2012
                while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
2013
                        loops--;
2014
 
2015
                if (loops) {
2016
                        write_zsdata(chan, ch);
2017
                        ret = 0;
2018
                } else
2019
                        ret = -EAGAIN;
2020
 
2021
                return ret;
2022
        } else
2023
                return -ENODEV;
2024
}
2025
 
2026
static int
2027
zs_poll_rx_char(struct dec_serial *info)
2028
{
2029
        struct dec_zschannel *chan = info->zs_channel;
2030
        int    ret;
2031
 
2032
        if(chan) {
2033
                int loops = 10000;
2034
 
2035
                while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
2036
                        loops--;
2037
 
2038
                if (loops)
2039
                        ret = read_zsdata(chan);
2040
                else
2041
                        ret = -EAGAIN;
2042
 
2043
                return ret;
2044
        } else
2045
                return -ENODEV;
2046
}
2047
 
2048
unsigned int register_zs_hook(unsigned int channel, struct zs_hook *hook)
2049
{
2050
        struct dec_serial *info = &zs_soft[channel];
2051
 
2052
        if (info->hook) {
2053
                printk(__FUNCTION__": line %d has already a hook registered\n", channel);
2054
 
2055
                return 0;
2056
        } else {
2057
                hook->poll_rx_char = zs_poll_rx_char;
2058
                hook->poll_tx_char = zs_poll_tx_char;
2059
                info->hook = hook;
2060
 
2061
                return 1;
2062
        }
2063
}
2064
 
2065
unsigned int unregister_zs_hook(unsigned int channel)
2066
{
2067
        struct dec_serial *info = &zs_soft[channel];
2068
 
2069
        if (info->hook) {
2070
                info->hook = NULL;
2071
                return 1;
2072
        } else {
2073
                printk(__FUNCTION__": trying to unregister hook on line %d,"
2074
                       " but none is registered\n", channel);
2075
                return 0;
2076
        }
2077
}
2078
 
2079
/*
2080
 * ------------------------------------------------------------
2081
 * Serial console driver
2082
 * ------------------------------------------------------------
2083
 */
2084
#ifdef CONFIG_SERIAL_DEC_CONSOLE
2085
 
2086
 
2087
/*
2088
 *      Print a string to the serial port trying not to disturb
2089
 *      any possible real use of the port...
2090
 */
2091
static void serial_console_write(struct console *co, const char *s,
2092
                                 unsigned count)
2093
{
2094
        struct dec_serial *info;
2095
        int i;
2096
 
2097
        info = zs_soft + co->index;
2098
 
2099
        for (i = 0; i < count; i++, s++) {
2100
                if(*s == '\n')
2101
                        zs_poll_tx_char(info, '\r');
2102
                zs_poll_tx_char(info, *s);
2103
        }
2104
}
2105
 
2106
static kdev_t serial_console_device(struct console *c)
2107
{
2108
        return MKDEV(TTY_MAJOR, 64 + c->index);
2109
}
2110
 
2111
/*
2112
 *      Setup initial baud/bits/parity. We do two things here:
2113
 *      - construct a cflag setting for the first rs_open()
2114
 *      - initialize the serial port
2115
 *      Return non-zero if we didn't find a serial port.
2116
 */
2117
static int __init serial_console_setup(struct console *co, char *options)
2118
{
2119
        struct dec_serial *info;
2120
        int baud = 9600;
2121
        int bits = 8;
2122
        int parity = 'n';
2123
        int cflag = CREAD | HUPCL | CLOCAL;
2124
        int clk_divisor = 16;
2125
        int brg;
2126
        char *s;
2127
        unsigned long flags;
2128
 
2129
        if(!BUS_PRESENT)
2130
                return -ENODEV;
2131
 
2132
        info = zs_soft + co->index;
2133
 
2134
        if (zs_chain == 0)
2135
                probe_sccs();
2136
 
2137
        info->is_cons = 1;
2138
 
2139
        if (options) {
2140
                baud = simple_strtoul(options, NULL, 10);
2141
                s = options;
2142
                while(*s >= '0' && *s <= '9')
2143
                        s++;
2144
                if (*s)
2145
                        parity = *s++;
2146
                if (*s)
2147
                        bits   = *s - '0';
2148
        }
2149
 
2150
        /*
2151
         *      Now construct a cflag setting.
2152
         */
2153
        switch(baud) {
2154
        case 1200:
2155
                cflag |= B1200;
2156
                break;
2157
        case 2400:
2158
                cflag |= B2400;
2159
                break;
2160
        case 4800:
2161
                cflag |= B4800;
2162
                break;
2163
        case 19200:
2164
                cflag |= B19200;
2165
                break;
2166
        case 38400:
2167
                cflag |= B38400;
2168
                break;
2169
        case 57600:
2170
                cflag |= B57600;
2171
                break;
2172
        case 115200:
2173
                cflag |= B115200;
2174
                break;
2175
        case 9600:
2176
        default:
2177
                cflag |= B9600;
2178
                /*
2179
                 * Set this to a sane value to prevent a divide error.
2180
                 */
2181
                baud  = 9600;
2182
                break;
2183
        }
2184
        switch(bits) {
2185
        case 7:
2186
                cflag |= CS7;
2187
                break;
2188
        default:
2189
        case 8:
2190
                cflag |= CS8;
2191
                break;
2192
        }
2193
        switch(parity) {
2194
        case 'o': case 'O':
2195
                cflag |= PARODD;
2196
                break;
2197
        case 'e': case 'E':
2198
                cflag |= PARENB;
2199
                break;
2200
        }
2201
        co->cflag = cflag;
2202
 
2203
        save_and_cli(flags);
2204
 
2205
        /*
2206
         * Set up the baud rate generator.
2207
         */
2208
        brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2209
        info->zs_channel->curregs[R12] = (brg & 255);
2210
        info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2211
 
2212
        /*
2213
         * Set byte size and parity.
2214
         */
2215
        if (bits == 7) {
2216
                info->zs_channel->curregs[R3] |= Rx7;
2217
                info->zs_channel->curregs[R5] |= Tx7;
2218
        } else {
2219
                info->zs_channel->curregs[R3] |= Rx8;
2220
                info->zs_channel->curregs[R5] |= Tx8;
2221
        }
2222
        if (cflag & PARENB) {
2223
                info->zs_channel->curregs[R4] |= PAR_ENA;
2224
        }
2225
        if (!(cflag & PARODD)) {
2226
                info->zs_channel->curregs[R4] |= PAR_EVEN;
2227
        }
2228
        info->zs_channel->curregs[R4] |= SB1;
2229
 
2230
        /*
2231
         * Turn on RTS and DTR.
2232
         */
2233
        zs_rtsdtr(info, RTS | DTR, 1);
2234
 
2235
        /*
2236
         * Finally, enable sequencing.
2237
         */
2238
        info->zs_channel->curregs[R3] |= RxENABLE;
2239
        info->zs_channel->curregs[R5] |= TxENAB;
2240
 
2241
        /*
2242
         * Clear the interrupt registers.
2243
         */
2244
        write_zsreg(info->zs_channel, R0, ERR_RES);
2245
        write_zsreg(info->zs_channel, R0, RES_H_IUS);
2246
 
2247
        /*
2248
         * Load up the new values.
2249
         */
2250
        load_zsregs(info->zs_channel, info->zs_channel->curregs);
2251
 
2252
        /* Save the current value of RR0 */
2253
        info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2254
 
2255
        zs_soft[co->index].clk_divisor = clk_divisor;
2256
        zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2257
 
2258
        restore_flags(flags);
2259
 
2260
        return 0;
2261
}
2262
 
2263
static struct console sercons = {
2264
        .name           = "ttyS",
2265
        .write          = serial_console_write,
2266
        .device         = serial_console_device,
2267
        .setup          = serial_console_setup,
2268
        .flags          = CON_PRINTBUFFER,
2269
        .index          = -1,
2270
};
2271
 
2272
/*
2273
 *      Register console.
2274
 */
2275
void __init zs_serial_console_init(void)
2276
{
2277
        register_console(&sercons);
2278
}
2279
#endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2280
 
2281
#ifdef CONFIG_KGDB
2282
struct dec_zschannel *zs_kgdbchan;
2283
static unsigned char scc_inittab[] = {
2284
        9,  0x80,       /* reset A side (CHRA) */
2285
        13, 0,           /* set baud rate divisor */
2286
        12, 1,
2287
        14, 1,          /* baud rate gen enable, src=rtxc (BRENABL) */
2288
        11, 0x50,       /* clocks = br gen (RCBR | TCBR) */
2289
        5,  0x6a,       /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2290
        4,  0x44,       /* x16 clock, 1 stop (SB1 | X16CLK)*/
2291
        3,  0xc1,       /* rx enable, 8 bits (RxENABLE | Rx8)*/
2292
};
2293
 
2294
/* These are for receiving and sending characters under the kgdb
2295
 * source level kernel debugger.
2296
 */
2297
void putDebugChar(char kgdb_char)
2298
{
2299
        struct dec_zschannel *chan = zs_kgdbchan;
2300
        while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2301
                RECOVERY_DELAY;
2302
        write_zsdata(chan, kgdb_char);
2303
}
2304
char getDebugChar(void)
2305
{
2306
        struct dec_zschannel *chan = zs_kgdbchan;
2307
        while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2308
                eieio(); /*barrier();*/
2309
        return read_zsdata(chan);
2310
}
2311
void kgdb_interruptible(int yes)
2312
{
2313
        struct dec_zschannel *chan = zs_kgdbchan;
2314
        int one, nine;
2315
        nine = read_zsreg(chan, 9);
2316
        if (yes == 1) {
2317
                one = EXT_INT_ENAB|RxINT_ALL;
2318
                nine |= MIE;
2319
                printk("turning serial ints on\n");
2320
        } else {
2321
                one = RxINT_DISAB;
2322
                nine &= ~MIE;
2323
                printk("turning serial ints off\n");
2324
        }
2325
        write_zsreg(chan, 1, one);
2326
        write_zsreg(chan, 9, nine);
2327
}
2328
 
2329
static int kgdbhook_init_channel(struct dec_serial* info)
2330
{
2331
        return 0;
2332
}
2333
 
2334
static void kgdbhook_init_info(struct dec_serial* info)
2335
{
2336
}
2337
 
2338
static void kgdbhook_rx_char(struct dec_serial* info,
2339
                             unsigned char ch, unsigned char stat)
2340
{
2341
        if (ch == 0x03 || ch == '$')
2342
                breakpoint();
2343
        if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
2344
                write_zsreg(info->zs_channel, 0, ERR_RES);
2345
}
2346
 
2347
/* This sets up the serial port we're using, and turns on
2348
 * interrupts for that channel, so kgdb is usable once we're done.
2349
 */
2350
static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2351
{
2352
        int brg;
2353
        int i, x;
2354
        volatile char *sccc = ms->control;
2355
        brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2356
        printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2357
        for (i = 20000; i != 0; --i) {
2358
                x = *sccc; eieio();
2359
        }
2360
        for (i = 0; i < sizeof(scc_inittab); ++i) {
2361
                write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2362
                i++;
2363
        }
2364
}
2365
/* This is called at boot time to prime the kgdb serial debugging
2366
 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2367
 * for /dev/ttyb which is determined in setup_arch() from the
2368
 * boot command line flags.
2369
 */
2370
struct zs_hook zs_kgdbhook = {
2371
        init_channel : kgdbhook_init_channel,
2372
        init_info    : kgdbhook_init_info,
2373
        cflags       : B38400|CS8|CLOCAL,
2374
        rx_char      : kgdbhook_rx_char,
2375
}
2376
 
2377
void __init zs_kgdb_hook(int tty_num)
2378
{
2379
        /* Find out how many Z8530 SCCs we have */
2380
        if (zs_chain == 0)
2381
                probe_sccs();
2382
        zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2383
        zs_kgdbchan = zs_soft[tty_num].zs_channel;
2384
        zs_soft[tty_num].change_needed = 0;
2385
        zs_soft[tty_num].clk_divisor = 16;
2386
        zs_soft[tty_num].zs_baud = 38400;
2387
        zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2388
        /* Turn on transmitter/receiver at 8-bits/char */
2389
        kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2390
        printk("KGDB: on channel %d initialized\n", tty_num);
2391
        set_debug_traps(); /* init stub */
2392
}
2393
#endif /* ifdef CONFIG_KGDB */
2394
 
2395
 

powered by: WebSVN 2.1.0

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