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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [sgiserial.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* sgiserial.c: Serial port driver for SGI machines.
2
 *
3
 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
4
 */
5
 
6
/*
7
 * Note: This driver seems to have been derived from some
8
 * version of the sbus/char/zs.c driver.  A lot of clean-up
9
 * and bug fixes seem to have happened to the Sun driver in
10
 * the intervening time.  As of 21.09.1999, I have merged in
11
 * ONLY the changes necessary to fix observed functional
12
 * problems on the Indy.  Someone really ought to do a
13
 * thorough pass to merge in the rest of the updates.
14
 * Better still, someone really ought to make it a common
15
 * code module for both platforms.   kevink@mips.com
16
 */
17
 
18
#include <linux/config.h> /* for CONFIG_KGDB */
19
#include <linux/errno.h>
20
#include <linux/signal.h>
21
#include <linux/sched.h>
22
#include <linux/timer.h>
23
#include <linux/interrupt.h>
24
#include <linux/tty.h>
25
#include <linux/tty_flip.h>
26
#include <linux/major.h>
27
#include <linux/string.h>
28
#include <linux/fcntl.h>
29
#include <linux/mm.h>
30
#include <linux/kernel.h>
31
#include <linux/delay.h>
32
#include <linux/console.h>
33
#include <linux/init.h>
34
 
35
#include <asm/io.h>
36
#include <asm/irq.h>
37
#include <asm/system.h>
38
#include <asm/bitops.h>
39
#include <asm/uaccess.h>
40
#include <asm/sgialib.h>
41
#include <asm/sgi/ioc.h>
42
#include <asm/sgi/ip22.h>
43
 
44
#include "sgiserial.h"
45
 
46
#define NUM_SERIAL 1     /* One chip on board. */
47
#define NUM_CHANNELS (NUM_SERIAL * 2)
48
 
49
struct sgi_zslayout *zs_chips[NUM_SERIAL];
50
struct sgi_zschannel *zs_channels[NUM_CHANNELS];
51
struct sgi_zschannel *zs_conschan;
52
struct sgi_zschannel *zs_kgdbchan;
53
 
54
struct sgi_serial zs_soft[NUM_CHANNELS];
55
struct sgi_serial *zs_chain;  /* IRQ servicing chain */
56
static int zilog_irq = SGI_SERIAL_IRQ;
57
 
58
/* Console hooks... */
59
static int zs_cons_chanout;
60
static int zs_cons_chanin;
61
struct sgi_serial *zs_consinfo;
62
 
63
static unsigned char kgdb_regs[16] = {
64
        0, 0, 0,                     /* write 0, 1, 2 */
65
        (Rx8 | RxENABLE),            /* write 3 */
66
        (X16CLK | SB1 | PAR_EVEN),   /* write 4 */
67
        (Tx8 | TxENAB),              /* write 5 */
68
        0, 0, 0,                     /* write 6, 7, 8 */
69
        (NV),                        /* write 9 */
70
        (NRZ),                       /* write 10 */
71
        (TCBR | RCBR),               /* write 11 */
72
        0, 0,                        /* BRG time constant, write 12 + 13 */
73
        (BRENABL),                   /* write 14 */
74
        (DCDIE)                      /* write 15 */
75
};
76
 
77
static unsigned char zscons_regs[16] = {
78
        0,                           /* write 0 */
79
        (EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
80
        0,                           /* write 2 */
81
        (Rx8 | RxENABLE),            /* write 3 */
82
        (X16CLK),                    /* write 4 */
83
        (DTR | Tx8 | TxENAB),        /* write 5 */
84
        0, 0, 0,                     /* write 6, 7, 8 */
85
        (NV | MIE),                  /* write 9 */
86
        (NRZ),                       /* write 10 */
87
        (TCBR | RCBR),               /* write 11 */
88
        0, 0,                        /* BRG time constant, write 12 + 13 */
89
        (BRENABL),                   /* write 14 */
90
        (DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
91
};
92
 
93
#define ZS_CLOCK         3672000   /* Zilog input clock rate */
94
 
95
DECLARE_TASK_QUEUE(tq_serial);
96
 
97
struct tty_driver serial_driver, callout_driver;
98
struct console *sgisercon;
99
static int serial_refcount;
100
 
101
/* serial subtype definitions */
102
#define SERIAL_TYPE_NORMAL      1
103
#define SERIAL_TYPE_CALLOUT     2
104
 
105
/* number of characters left in xmit buffer before we ask for more */
106
#define WAKEUP_CHARS 256
107
 
108
#undef SERIAL_DEBUG_OPEN
109
 
110
static void change_speed(struct sgi_serial *info);
111
 
112
static struct tty_struct *serial_table[NUM_CHANNELS];
113
static struct termios *serial_termios[NUM_CHANNELS];
114
static struct termios *serial_termios_locked[NUM_CHANNELS];
115
 
116
/*
117
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
118
 * lock it in case the memcpy_fromfs blocks while swapping in a page,
119
 * and some other program tries to do a serial write at the same time.
120
 * Since the lock will only come under contention when the system is
121
 * swapping and available memory is low, it makes sense to share one
122
 * buffer across all the serial ports, since it significantly saves
123
 * memory if large numbers of serial ports are open.
124
 */
125
static unsigned char tmp_buf[PAGE_SIZE]; /* This is cheating */
126
static DECLARE_MUTEX(tmp_buf_sem);
127
 
128
static inline int serial_paranoia_check(struct sgi_serial *info,
129
                                        dev_t device, const char *routine)
130
{
131
#ifdef SERIAL_PARANOIA_CHECK
132
        static const char *badmagic = KERN_WARNING
133
                "Warning: bad magic number for serial struct (%d, %d) in %s\n";
134
        static const char *badinfo = KERN_WARNING
135
                "Warning: null sgi_serial for (%d, %d) in %s\n";
136
 
137
        if (!info) {
138
                printk(badinfo, MAJOR(device), MINOR(device), routine);
139
                return 1;
140
        }
141
        if (info->magic != SERIAL_MAGIC) {
142
                printk(badmagic, MAJOR(device), MINOR(device), routine);
143
                return 1;
144
        }
145
#endif
146
        return 0;
147
}
148
 
149
/*
150
 * This is used to figure out the divisor speeds and the timeouts
151
 */
152
static int baud_table[] = {
153
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
154
        9600, 19200, 38400, 57600, 115200, 0 };
155
 
156
/*
157
 * Reading and writing Zilog8530 registers.  The delays are to make this
158
 * driver work on the Sun4 which needs a settling delay after each chip
159
 * register access, other machines handle this in hardware via auxiliary
160
 * flip-flops which implement the settle time we do in software.
161
 *
162
 * read_zsreg() and write_zsreg() may get called from rs_kgdb_hook() before
163
 * interrupts are enabled. Therefore we have to check ioc_iocontrol before we
164
 * access it.
165
 */
166
static inline unsigned char read_zsreg(struct sgi_zschannel *channel,
167
                                       unsigned char reg)
168
{
169
        unsigned char retval;
170
        volatile unsigned char junk;
171
 
172
        udelay(2);
173
        channel->control = reg;
174
        junk = sgint->istat0;
175
        udelay(1);
176
        retval = channel->control;
177
        return retval;
178
}
179
 
180
static inline void write_zsreg(struct sgi_zschannel *channel,
181
                               unsigned char reg, unsigned char value)
182
{
183
        volatile unsigned char junk;
184
 
185
        udelay(2);
186
        channel->control = reg;
187
        junk = sgint->istat0;
188
        udelay(1);
189
        channel->control = value;
190
        junk = sgint->istat0;
191
        return;
192
}
193
 
194
static inline void load_zsregs(struct sgi_zschannel *channel, unsigned char *regs)
195
{
196
        ZS_CLEARERR(channel);
197
        ZS_CLEARFIFO(channel);
198
        /* Load 'em up */
199
        write_zsreg(channel, R4, regs[R4]);
200
        write_zsreg(channel, R10, regs[R10]);
201
        write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
202
        write_zsreg(channel, R5, regs[R5] & ~TxENAB);
203
        write_zsreg(channel, R1, regs[R1]);
204
        write_zsreg(channel, R9, regs[R9]);
205
        write_zsreg(channel, R11, regs[R11]);
206
        write_zsreg(channel, R12, regs[R12]);
207
        write_zsreg(channel, R13, regs[R13]);
208
        write_zsreg(channel, R14, regs[R14]);
209
        write_zsreg(channel, R15, regs[R15]);
210
        write_zsreg(channel, R3, regs[R3]);
211
        write_zsreg(channel, R5, regs[R5]);
212
        return;
213
}
214
 
215
/* Sets or clears DTR/RTS on the requested line */
216
static inline void zs_rtsdtr(struct sgi_serial *ss, int set)
217
{
218
        if(set) {
219
                ss->curregs[5] |= (RTS | DTR);
220
                write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
221
        } else {
222
                ss->curregs[5] &= ~(RTS | DTR);
223
                write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
224
        }
225
        return;
226
}
227
 
228
static inline void kgdb_chaninit(struct sgi_serial *ss, int intson, int bps)
229
{
230
        int brg;
231
 
232
        if(intson) {
233
                kgdb_regs[R1] = INT_ALL_Rx;
234
                kgdb_regs[R9] |= MIE;
235
        } else {
236
                kgdb_regs[R1] = 0;
237
                kgdb_regs[R9] &= ~MIE;
238
        }
239
        brg = BPS_TO_BRG(bps, ZS_CLOCK/ss->clk_divisor);
240
        kgdb_regs[R12] = (brg & 255);
241
        kgdb_regs[R13] = ((brg >> 8) & 255);
242
        load_zsregs(ss->zs_channel, kgdb_regs);
243
}
244
 
245
/* Utility routines for the Zilog */
246
static inline int get_zsbaud(struct sgi_serial *ss)
247
{
248
        struct sgi_zschannel *channel = ss->zs_channel;
249
        int brg;
250
 
251
        /* The baud rate is split up between two 8-bit registers in
252
         * what is termed 'BRG time constant' format in my docs for
253
         * the chip, it is a function of the clk rate the chip is
254
         * receiving which happens to be constant.
255
         */
256
        brg = ((read_zsreg(channel, 13)&0xff) << 8);
257
        brg |= (read_zsreg(channel, 12)&0xff);
258
        return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
259
}
260
 
261
/*
262
 * ------------------------------------------------------------
263
 * rs_stop() and rs_start()
264
 *
265
 * This routines are called before setting or resetting tty->stopped.
266
 * They enable or disable transmitter interrupts, as necessary.
267
 * ------------------------------------------------------------
268
 */
269
static void rs_stop(struct tty_struct *tty)
270
{
271
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
272
        unsigned long flags;
273
 
274
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
275
                return;
276
 
277
        save_flags(flags); cli();
278
        if (info->curregs[5] & TxENAB) {
279
                info->curregs[5] &= ~TxENAB;
280
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
281
        }
282
        restore_flags(flags);
283
}
284
 
285
static void rs_start(struct tty_struct *tty)
286
{
287
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
288
        unsigned long flags;
289
 
290
        if (serial_paranoia_check(info, tty->device, "rs_start"))
291
                return;
292
 
293
        save_flags(flags); cli();
294
        if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
295
                info->curregs[5] |= TxENAB;
296
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
297
        }
298
        restore_flags(flags);
299
}
300
 
301
/* Drop into either the boot monitor or kadb upon receiving a break
302
 * from keyboard/console input.
303
 */
304
static void batten_down_hatches(void)
305
{
306
        ArcEnterInteractiveMode();
307
#if 0
308
        /* If we are doing kadb, we call the debugger
309
         * else we just drop into the boot monitor.
310
         * Note that we must flush the user windows
311
         * first before giving up control.
312
         */
313
        printk("\n");
314
        if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
315
           (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
316
                sp_enter_debugger();
317
        else
318
                prom_halt();
319
 
320
        /* XXX We want to notify the keyboard driver that all
321
         * XXX keys are in the up state or else weird things
322
         * XXX happen...
323
         */
324
#endif
325
        return;
326
}
327
 
328
/* On receive, this clears errors and the receiver interrupts */
329
static inline void rs_recv_clear(struct sgi_zschannel *zsc)
330
{
331
        volatile unsigned char junk;
332
 
333
        udelay(2);
334
        zsc->control = ERR_RES;
335
        junk = sgint->istat0;
336
        udelay(2);
337
        zsc->control = RES_H_IUS;
338
        junk = sgint->istat0;
339
}
340
 
341
/*
342
 * ----------------------------------------------------------------------
343
 *
344
 * Here starts the interrupt handling routines.  All of the following
345
 * subroutines are declared as inline and are folded into
346
 * rs_interrupt().  They were separated out for readability's sake.
347
 *
348
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
349
 * runs with interrupts turned off.  People who may want to modify
350
 * rs_interrupt() should try to keep the interrupt handler as fast as
351
 * possible.  After you are done making modifications, it is not a bad
352
 * idea to do:
353
 *
354
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
355
 *
356
 * and look at the resulting assemble code in serial.s.
357
 *
358
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
359
 * -----------------------------------------------------------------------
360
 */
361
 
362
/*
363
 * This routine is used by the interrupt handler to schedule
364
 * processing in the software interrupt portion of the driver.
365
 */
366
static inline void rs_sched_event(struct sgi_serial *info,
367
                                    int event)
368
{
369
        info->event |= 1 << event;
370
        queue_task(&info->tqueue, &tq_serial);
371
        mark_bh(SERIAL_BH);
372
}
373
 
374
#ifdef CONFIG_KGDB
375
extern void set_async_breakpoint(unsigned int epc);
376
#endif
377
 
378
static inline void receive_chars(struct sgi_serial *info, struct pt_regs *regs)
379
{
380
        struct tty_struct *tty = info->tty;
381
        volatile unsigned char junk;
382
        unsigned char ch, stat;
383
 
384
        udelay(2);
385
        ch = info->zs_channel->data;
386
        junk = sgint->istat0;
387
        udelay(2);
388
        stat = read_zsreg(info->zs_channel, R1);
389
 
390
        /* If this is the console keyboard, we need to handle
391
         * L1-A's here.
392
         */
393
        if(info->is_cons) {
394
                if(ch==0) { /* whee, break received */
395
                        batten_down_hatches();
396
                        rs_recv_clear(info->zs_channel);
397
                        return;
398
                } else if (ch == 1) {
399
                        show_state();
400
                        return;
401
                } else if (ch == 2) {
402
                        show_buffers();
403
                        return;
404
                }
405
        }
406
        /* Look for kgdb 'stop' character, consult the gdb documentation
407
         * for remote target debugging and arch/sparc/kernel/sparc-stub.c
408
         * to see how all this works.
409
         */
410
#ifdef CONFIG_KGDB
411
        if((info->kgdb_channel) && (ch =='\003')) {
412
                set_async_breakpoint(read_32bit_cp0_register(CP0_EPC));
413
                goto clear_and_exit;
414
        }
415
#endif
416
        if(!tty)
417
                goto clear_and_exit;
418
 
419
        if (tty->flip.count >= TTY_FLIPBUF_SIZE)
420
                queue_task(&tty->flip.tqueue, &tq_timer);
421
        tty->flip.count++;
422
        if(stat & PAR_ERR)
423
                *tty->flip.flag_buf_ptr++ = TTY_PARITY;
424
        else if(stat & Rx_OVR)
425
                *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
426
        else if(stat & CRC_ERR)
427
                *tty->flip.flag_buf_ptr++ = TTY_FRAME;
428
        else
429
                *tty->flip.flag_buf_ptr++ = 0; /* XXX */
430
        *tty->flip.char_buf_ptr++ = ch;
431
 
432
        queue_task(&tty->flip.tqueue, &tq_timer);
433
 
434
clear_and_exit:
435
        rs_recv_clear(info->zs_channel);
436
        return;
437
}
438
 
439
static inline void transmit_chars(struct sgi_serial *info)
440
{
441
        volatile unsigned char junk;
442
 
443
        /* P3: In theory we have to test readiness here because a
444
         * serial console can clog the chip through zs_cons_put_char().
445
         * David did not do this. I think he relies on 3-chars FIFO in 8530.
446
         * Let's watch for lost _output_ characters. XXX
447
         */
448
 
449
        /* SGI ADDENDUM: On most SGI machines, the Zilog does possess
450
         *               a 16 or 17 byte fifo, so no worries. -dm
451
         */
452
 
453
        if (info->x_char) {
454
                /* Send next char */
455
                udelay(2);
456
                info->zs_channel->data = info->x_char;
457
                junk = sgint->istat0;
458
 
459
                info->x_char = 0;
460
                goto clear_and_return;
461
        }
462
 
463
        if((info->xmit_cnt <= 0) || info->tty->stopped) {
464
                /* That's peculiar... */
465
                udelay(2);
466
                info->zs_channel->control = RES_Tx_P;
467
                junk = sgint->istat0;
468
                goto clear_and_return;
469
        }
470
 
471
        /* Send char */
472
        udelay(2);
473
        info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
474
        junk = sgint->istat0;
475
 
476
        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
477
        info->xmit_cnt--;
478
 
479
        if (info->xmit_cnt < WAKEUP_CHARS)
480
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
481
 
482
        if(info->xmit_cnt <= 0) {
483
                udelay(2);
484
                info->zs_channel->control = RES_Tx_P;
485
                junk = sgint->istat0;
486
                goto clear_and_return;
487
        }
488
 
489
clear_and_return:
490
        /* Clear interrupt */
491
        udelay(2);
492
        info->zs_channel->control = RES_H_IUS;
493
        junk = sgint->istat0;
494
        return;
495
}
496
 
497
static inline void status_handle(struct sgi_serial *info)
498
{
499
        volatile unsigned char junk;
500
        unsigned char status;
501
 
502
        /* Get status from Read Register 0 */
503
        udelay(2);
504
        status = info->zs_channel->control;
505
        junk = sgint->istat0;
506
        /* Clear status condition... */
507
        udelay(2);
508
        info->zs_channel->control = RES_EXT_INT;
509
        junk = sgint->istat0;
510
        /* Clear the interrupt */
511
        udelay(2);
512
        info->zs_channel->control = RES_H_IUS;
513
        junk = sgint->istat0;
514
 
515
#if 0
516
        if(status & DCD) {
517
                if((info->tty->termios->c_cflag & CRTSCTS) &&
518
                   ((info->curregs[3] & AUTO_ENAB)==0)) {
519
                        info->curregs[3] |= AUTO_ENAB;
520
                        write_zsreg(info->zs_channel, 3, info->curregs[3]);
521
                }
522
        } else {
523
                if((info->curregs[3] & AUTO_ENAB)) {
524
                        info->curregs[3] &= ~AUTO_ENAB;
525
                        write_zsreg(info->zs_channel, 3, info->curregs[3]);
526
                }
527
        }
528
#endif
529
        /* Whee, if this is console input and this is a
530
         * 'break asserted' status change interrupt, call
531
         * the boot prom.
532
         */
533
        if((status & BRK_ABRT) && info->break_abort)
534
                batten_down_hatches();
535
 
536
        /* XXX Whee, put in a buffer somewhere, the status information
537
         * XXX whee whee whee... Where does the information go...
538
         */
539
        return;
540
}
541
 
542
/*
543
 * This is the serial driver's generic interrupt routine
544
 */
545
void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
546
{
547
        struct sgi_serial * info = (struct sgi_serial *) dev_id;
548
        unsigned char zs_intreg;
549
 
550
        zs_intreg = read_zsreg(info->zs_next->zs_channel, 3);
551
 
552
        /* NOTE: The read register 3, which holds the irq status,
553
         *       does so for both channels on each chip.  Although
554
         *       the status value itself must be read from the A
555
         *       channel and is only valid when read from channel A.
556
         *       Yes... broken hardware...
557
         */
558
#define CHAN_A_IRQMASK (CHARxIP | CHATxIP | CHAEXT)
559
#define CHAN_B_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
560
 
561
        /* *** Chip 1 *** */
562
        /* Channel B -- /dev/ttyb, could be the console */
563
        if(zs_intreg & CHAN_B_IRQMASK) {
564
                if (zs_intreg & CHBRxIP)
565
                        receive_chars(info, regs);
566
                if (zs_intreg & CHBTxIP)
567
                        transmit_chars(info);
568
                if (zs_intreg & CHBEXT)
569
                        status_handle(info);
570
        }
571
 
572
        info=info->zs_next;
573
 
574
        /* Channel A -- /dev/ttya, could be the console */
575
        if(zs_intreg & CHAN_A_IRQMASK) {
576
                if (zs_intreg & CHARxIP)
577
                        receive_chars(info, regs);
578
                if (zs_intreg & CHATxIP)
579
                        transmit_chars(info);
580
                if (zs_intreg & CHAEXT)
581
                        status_handle(info);
582
        }
583
}
584
 
585
/*
586
 * -------------------------------------------------------------------
587
 * Here ends the serial interrupt routines.
588
 * -------------------------------------------------------------------
589
 */
590
 
591
/*
592
 * This routine is used to handle the "bottom half" processing for the
593
 * serial driver, known also the "software interrupt" processing.
594
 * This processing is done at the kernel interrupt level, after the
595
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
596
 * is where time-consuming activities which can not be done in the
597
 * interrupt driver proper are done; the interrupt driver schedules
598
 * them using rs_sched_event(), and they get done here.
599
 */
600
static void do_serial_bh(void)
601
{
602
        run_task_queue(&tq_serial);
603
}
604
 
605
static void do_softint(void *private_)
606
{
607
        struct sgi_serial       *info = (struct sgi_serial *) private_;
608
        struct tty_struct       *tty;
609
 
610
        tty = info->tty;
611
        if (!tty)
612
                return;
613
 
614
        if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
615
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
616
                    tty->ldisc.write_wakeup)
617
                        (tty->ldisc.write_wakeup)(tty);
618
                wake_up_interruptible(&tty->write_wait);
619
        }
620
}
621
 
622
/*
623
 * This routine is called from the scheduler tqueue when the interrupt
624
 * routine has signalled that a hangup has occurred.  The path of
625
 * hangup processing is:
626
 *
627
 *      serial interrupt routine -> (scheduler tqueue) ->
628
 *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
629
 *
630
 */
631
static void do_serial_hangup(void *private_)
632
{
633
        struct sgi_serial       *info = (struct sgi_serial *) private_;
634
        struct tty_struct       *tty;
635
 
636
        tty = info->tty;
637
        if (!tty)
638
                return;
639
 
640
        tty_hangup(tty);
641
}
642
 
643
 
644
static int startup(struct sgi_serial * info)
645
{
646
        volatile unsigned char junk;
647
        unsigned long flags;
648
 
649
        if (info->flags & ZILOG_INITIALIZED)
650
                return 0;
651
 
652
        if (!info->xmit_buf) {
653
                info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
654
                if (!info->xmit_buf)
655
                        return -ENOMEM;
656
        }
657
 
658
        save_flags(flags); cli();
659
 
660
#ifdef SERIAL_DEBUG_OPEN
661
        printk("starting up ttys%d (irq %d)...\n", info->line, info->irq);
662
#endif
663
 
664
        /*
665
         * Clear the FIFO buffers and disable them
666
         * (they will be reenabled in change_speed())
667
         */
668
        ZS_CLEARFIFO(info->zs_channel);
669
        info->xmit_fifo_size = 1;
670
 
671
        /*
672
         * Clear the interrupt registers.
673
         */
674
        udelay(2);
675
        info->zs_channel->control = ERR_RES;
676
        junk = sgint->istat0;
677
        udelay(2);
678
        info->zs_channel->control = RES_H_IUS;
679
        junk = sgint->istat0;
680
 
681
        /*
682
         * Now, initialize the Zilog
683
         */
684
        zs_rtsdtr(info, 1);
685
 
686
        /*
687
         * Finally, enable sequencing and interrupts
688
         */
689
        info->curregs[1] |= (info->curregs[1] & ~0x18) | (EXT_INT_ENAB|INT_ALL_Rx);
690
        info->curregs[3] |= (RxENABLE | Rx8);
691
        /* We enable Tx interrupts as needed. */
692
        info->curregs[5] |= (TxENAB | Tx8);
693
        info->curregs[9] |= (NV | MIE);
694
        write_zsreg(info->zs_channel, 3, info->curregs[3]);
695
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
696
        write_zsreg(info->zs_channel, 9, info->curregs[9]);
697
 
698
        /*
699
         * And clear the interrupt registers again for luck.
700
         */
701
        udelay(2);
702
        info->zs_channel->control = ERR_RES;
703
        junk = sgint->istat0;
704
        udelay(2);
705
        info->zs_channel->control = RES_H_IUS;
706
        junk = sgint->istat0;
707
 
708
        if (info->tty)
709
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
710
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
711
 
712
        /*
713
         * and set the speed of the serial port
714
         */
715
        change_speed(info);
716
 
717
        info->flags |= ZILOG_INITIALIZED;
718
        restore_flags(flags);
719
        return 0;
720
}
721
 
722
/*
723
 * This routine will shutdown a serial port; interrupts are disabled, and
724
 * DTR is dropped if the hangup on close termio flag is on.
725
 */
726
static void shutdown(struct sgi_serial * info)
727
{
728
        unsigned long   flags;
729
 
730
        if (!(info->flags & ZILOG_INITIALIZED))
731
                return;
732
 
733
#ifdef SERIAL_DEBUG_OPEN
734
        printk("Shutting down serial port %d (irq %d)....", info->line,
735
               info->irq);
736
#endif
737
 
738
        save_flags(flags); cli(); /* Disable interrupts */
739
 
740
        if (info->xmit_buf) {
741
                free_page((unsigned long) info->xmit_buf);
742
                info->xmit_buf = 0;
743
        }
744
 
745
        if (info->tty)
746
                set_bit(TTY_IO_ERROR, &info->tty->flags);
747
 
748
        info->flags &= ~ZILOG_INITIALIZED;
749
        restore_flags(flags);
750
}
751
 
752
/*
753
 * This routine is called to set the UART divisor registers to match
754
 * the specified baud rate for a serial port.
755
 */
756
static void change_speed(struct sgi_serial *info)
757
{
758
        unsigned int port, cflag;
759
        int     i;
760
        int     brg;
761
 
762
        if (!info->tty || !info->tty->termios)
763
                return;
764
        cflag = info->tty->termios->c_cflag;
765
        if (!(port = info->port))
766
                return;
767
        i = cflag & CBAUD;
768
        if (i & CBAUDEX) {
769
                /* XXX CBAUDEX is not obeyed.
770
                 * It is impossible at a 32bits SPARC.
771
                 * But we have to report this to user ... someday.
772
                 */
773
                i = B9600;
774
        }
775
        if (i == 0) {
776
                /* XXX B0, hangup the line. */
777
                do_serial_hangup(info);
778
        } else if (baud_table[i]) {
779
                info->zs_baud = baud_table[i];
780
                info->clk_divisor = 16;
781
 
782
                info->curregs[4] = X16CLK;
783
                info->curregs[11] = TCBR | RCBR;
784
                brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
785
                info->curregs[12] = (brg & 255);
786
                info->curregs[13] = ((brg >> 8) & 255);
787
                info->curregs[14] = BRENABL;
788
        }
789
 
790
        /* byte size and parity */
791
        switch (cflag & CSIZE) {
792
        case CS5:
793
                info->curregs[3] &= ~(0xc0);
794
                info->curregs[3] |= Rx5;
795
                info->curregs[5] &= ~(0xe0);
796
                info->curregs[5] |= Tx5;
797
                break;
798
        case CS6:
799
                info->curregs[3] &= ~(0xc0);
800
                info->curregs[3] |= Rx6;
801
                info->curregs[5] &= ~(0xe0);
802
                info->curregs[5] |= Tx6;
803
                break;
804
        case CS7:
805
                info->curregs[3] &= ~(0xc0);
806
                info->curregs[3] |= Rx7;
807
                info->curregs[5] &= ~(0xe0);
808
                info->curregs[5] |= Tx7;
809
                break;
810
        case CS8:
811
        default: /* defaults to 8 bits */
812
                info->curregs[3] &= ~(0xc0);
813
                info->curregs[3] |= Rx8;
814
                info->curregs[5] &= ~(0xe0);
815
                info->curregs[5] |= Tx8;
816
                break;
817
        }
818
        info->curregs[4] &= ~(0x0c);
819
        if (cflag & CSTOPB)
820
                info->curregs[4] |= SB2;
821
        else
822
                info->curregs[4] |= SB1;
823
 
824
        if (cflag & PARENB)
825
                info->curregs[4] |= PAR_ENA;
826
        else
827
                info->curregs[4] &= ~PAR_ENA;
828
 
829
        if (!(cflag & PARODD))
830
                info->curregs[4] |= PAR_EVEN;
831
        else
832
                info->curregs[4] &= ~PAR_EVEN;
833
 
834
        /* Load up the new values */
835
        load_zsregs(info->zs_channel, info->curregs);
836
 
837
        return;
838
}
839
 
840
/* This is for console output over ttya/ttyb */
841
static void zs_cons_put_char(char ch)
842
{
843
        struct sgi_zschannel *chan = zs_conschan;
844
        volatile unsigned char junk;
845
        unsigned long flags;
846
        int loops = 0;
847
 
848
        save_flags(flags); cli();
849
        while(((junk = chan->control) & Tx_BUF_EMP)==0 && loops < 10000) {
850
                loops++;
851
                udelay(2);
852
        }
853
 
854
        udelay(2);
855
        chan->data = ch;
856
        junk = sgint->istat0;
857
        restore_flags(flags);
858
}
859
 
860
/*
861
 * This is the more generic put_char function for the driver.
862
 * In earlier versions of this driver, "rs_put_char" was the
863
 * name of the console-specific fucntion, now called zs_cons_put_char
864
 */
865
 
866
static void rs_put_char(struct tty_struct *tty, char ch)
867
{
868
        struct sgi_zschannel *chan =
869
                ((struct sgi_serial *)tty->driver_data)->zs_channel;
870
        volatile unsigned char junk;
871
        unsigned long flags;
872
        int loops = 0;
873
 
874
        save_flags(flags); cli();
875
        while(((junk = chan->control) & Tx_BUF_EMP)==0 && loops < 10000) {
876
                loops++;
877
                udelay(2);
878
        }
879
 
880
        udelay(2);
881
        chan->data = ch;
882
        junk = sgint->istat0;
883
        restore_flags(flags);
884
}
885
 
886
/* These are for receiving and sending characters under the kgdb
887
 * source level kernel debugger.
888
 */
889
int putDebugChar(char kgdb_char)
890
{
891
        struct sgi_zschannel *chan = zs_kgdbchan;
892
        volatile unsigned char junk;
893
        unsigned long flags;
894
 
895
        save_flags(flags); cli();
896
        udelay(2);
897
        while((chan->control & Tx_BUF_EMP)==0)
898
                udelay(2);
899
 
900
        udelay(2);
901
        chan->data = kgdb_char;
902
        junk = sgint->istat0;
903
        restore_flags(flags);
904
 
905
        return 1;
906
}
907
 
908
char getDebugChar(void)
909
{
910
        struct sgi_zschannel *chan = zs_kgdbchan;
911
        unsigned char junk;
912
 
913
        while((chan->control & Rx_CH_AV)==0)
914
                udelay(2);
915
 
916
        junk = sgint->istat0;
917
        udelay(2);
918
        return chan->data;
919
}
920
 
921
/*
922
 * Fair output driver allows a process to speak.
923
 */
924
static void rs_fair_output(void)
925
{
926
        int left;               /* Output no more than that */
927
        unsigned long flags;
928
        struct sgi_serial *info = zs_consinfo;
929
        volatile unsigned char junk;
930
        char c;
931
 
932
        if (info == 0) return;
933
        if (info->xmit_buf == 0) return;
934
 
935
        save_flags(flags);  cli();
936
        left = info->xmit_cnt;
937
        while (left != 0) {
938
                c = info->xmit_buf[info->xmit_tail];
939
                info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
940
                info->xmit_cnt--;
941
                restore_flags(flags);
942
 
943
                zs_cons_put_char(c);
944
 
945
                save_flags(flags);  cli();
946
                left = min(info->xmit_cnt, left-1);
947
        }
948
 
949
        /* Last character is being transmitted now (hopefully). */
950
        udelay(2);
951
        zs_conschan->control = RES_Tx_P;
952
        junk = sgint->istat0;
953
 
954
        restore_flags(flags);
955
        return;
956
}
957
 
958
 
959
static int rs_write(struct tty_struct * tty, int from_user,
960
                    const unsigned char *buf, int count)
961
{
962
        int     c, total = 0;
963
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
964
        unsigned long flags;
965
 
966
        if (serial_paranoia_check(info, tty->device, "rs_write"))
967
                return 0;
968
 
969
        if (!tty || !info->xmit_buf)
970
                return 0;
971
 
972
        save_flags(flags);
973
        while (1) {
974
                cli();
975
                c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
976
                                   SERIAL_XMIT_SIZE - info->xmit_head));
977
                if (c <= 0)
978
                        break;
979
 
980
                if (from_user) {
981
                        down(&tmp_buf_sem);
982
                        copy_from_user(tmp_buf, buf, c);
983
                        c = min(c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
984
                                       SERIAL_XMIT_SIZE - info->xmit_head));
985
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
986
                        up(&tmp_buf_sem);
987
                } else
988
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
989
                info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
990
                info->xmit_cnt += c;
991
                restore_flags(flags);
992
                buf += c;
993
                count -= c;
994
                total += c;
995
        }
996
 
997
        if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
998
        /*
999
         * The above test used to include the condition
1000
         * "&& !(info->curregs[5] & TxENAB)", but there
1001
         * is reason to suspect that it is never statisfied
1002
         * when the port is running.  The problem may in fact
1003
         * have been masked by the fact that, if O_POST is set,
1004
         * there is always a rs_flush_xx operation following the
1005
         * rs_write, and the flush ignores that condition when
1006
         * it kicks off the transmit.
1007
         */
1008
                /* Enable transmitter */
1009
                info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1010
                write_zsreg(info->zs_channel, 1, info->curregs[1]);
1011
                info->curregs[5] |= TxENAB;
1012
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
1013
 
1014
        /*
1015
         * The following code is imported from the 2.3.6 Sun sbus zs.c
1016
         * driver, of which an earlier version served as the basis
1017
         * for sgiserial.c.  Perhaps due to changes over time in
1018
         * the line discipline code, ns_write()s with from_user
1019
         * set would not otherwise actually kick-off output in
1020
         * Linux 2.2.x or later.  Maybe it never really worked.
1021
         */
1022
 
1023
                rs_put_char(tty, info->xmit_buf[info->xmit_tail++]);
1024
                info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1025
                info->xmit_cnt--;
1026
        }
1027
 
1028
        restore_flags(flags);
1029
        return total;
1030
}
1031
 
1032
static int rs_write_room(struct tty_struct *tty)
1033
{
1034
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1035
        int     ret;
1036
 
1037
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1038
                return 0;
1039
        ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1040
        if (ret < 0)
1041
                ret = 0;
1042
        return ret;
1043
}
1044
 
1045
static int rs_chars_in_buffer(struct tty_struct *tty)
1046
{
1047
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1048
 
1049
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1050
                return 0;
1051
        return info->xmit_cnt;
1052
}
1053
 
1054
static void rs_flush_buffer(struct tty_struct *tty)
1055
{
1056
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1057
 
1058
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1059
                return;
1060
        cli();
1061
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1062
        sti();
1063
        wake_up_interruptible(&tty->write_wait);
1064
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1065
            tty->ldisc.write_wakeup)
1066
                (tty->ldisc.write_wakeup)(tty);
1067
}
1068
 
1069
static void rs_flush_chars(struct tty_struct *tty)
1070
{
1071
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1072
        unsigned long flags;
1073
 
1074
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1075
                return;
1076
 
1077
        if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1078
            !info->xmit_buf)
1079
                return;
1080
 
1081
        /* Enable transmitter */
1082
        save_flags(flags); cli();
1083
        info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1084
        write_zsreg(info->zs_channel, 1, info->curregs[1]);
1085
        info->curregs[5] |= TxENAB;
1086
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1087
 
1088
        /*
1089
         * Send a first (bootstrapping) character. A best solution is
1090
         * to call transmit_chars() here which handles output in a
1091
         * generic way. Current transmit_chars() not only transmits,
1092
         * but resets interrupts also what we do not desire here.
1093
         * XXX Discuss with David.
1094
         */
1095
        if (info->zs_channel->control & Tx_BUF_EMP) {
1096
                volatile unsigned char junk;
1097
 
1098
                /* Send char */
1099
                udelay(2);
1100
                info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
1101
                junk = sgint->istat0;
1102
                info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1103
                info->xmit_cnt--;
1104
        }
1105
        restore_flags(flags);
1106
}
1107
 
1108
/*
1109
 * ------------------------------------------------------------
1110
 * rs_throttle()
1111
 *
1112
 * This routine is called by the upper-layer tty layer to signal that
1113
 * incoming characters should be throttled.
1114
 * ------------------------------------------------------------
1115
 */
1116
static void rs_throttle(struct tty_struct * tty)
1117
{
1118
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1119
#ifdef SERIAL_DEBUG_THROTTLE
1120
        char    buf[64];
1121
 
1122
        printk("throttle %s: %d....\n", _tty_name(tty, buf),
1123
               tty->ldisc.chars_in_buffer(tty));
1124
#endif
1125
 
1126
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1127
                return;
1128
 
1129
        if (I_IXOFF(tty))
1130
                info->x_char = STOP_CHAR(tty);
1131
 
1132
        /* Turn off RTS line */
1133
        cli();
1134
        info->curregs[5] &= ~RTS;
1135
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1136
        sti();
1137
}
1138
 
1139
static void rs_unthrottle(struct tty_struct * tty)
1140
{
1141
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1142
#ifdef SERIAL_DEBUG_THROTTLE
1143
        char    buf[64];
1144
 
1145
        printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1146
               tty->ldisc.chars_in_buffer(tty));
1147
#endif
1148
 
1149
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1150
                return;
1151
 
1152
        if (I_IXOFF(tty)) {
1153
                if (info->x_char)
1154
                        info->x_char = 0;
1155
                else
1156
                        info->x_char = START_CHAR(tty);
1157
        }
1158
 
1159
        /* Assert RTS line */
1160
        cli();
1161
        info->curregs[5] |= RTS;
1162
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1163
        sti();
1164
}
1165
 
1166
/*
1167
 * ------------------------------------------------------------
1168
 * rs_ioctl() and friends
1169
 * ------------------------------------------------------------
1170
 */
1171
 
1172
static int get_serial_info(struct sgi_serial * info,
1173
                           struct serial_struct * retinfo)
1174
{
1175
        struct serial_struct tmp;
1176
 
1177
        if (!retinfo)
1178
                return -EFAULT;
1179
        memset(&tmp, 0, sizeof(tmp));
1180
        tmp.type = info->type;
1181
        tmp.line = info->line;
1182
        tmp.port = info->port;
1183
        tmp.irq = info->irq;
1184
        tmp.flags = info->flags;
1185
        tmp.baud_base = info->baud_base;
1186
        tmp.close_delay = info->close_delay;
1187
        tmp.closing_wait = info->closing_wait;
1188
        tmp.custom_divisor = info->custom_divisor;
1189
        return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1190
}
1191
 
1192
static int set_serial_info(struct sgi_serial * info,
1193
                           struct serial_struct * new_info)
1194
{
1195
        struct serial_struct new_serial;
1196
        struct sgi_serial old_info;
1197
        int                     retval = 0;
1198
 
1199
        if (!new_info)
1200
                return -EFAULT;
1201
        copy_from_user(&new_serial,new_info,sizeof(new_serial));
1202
        old_info = *info;
1203
 
1204
        if (!capable(CAP_SYS_ADMIN)) {
1205
                if ((new_serial.baud_base != info->baud_base) ||
1206
                    (new_serial.type != info->type) ||
1207
                    (new_serial.close_delay != info->close_delay) ||
1208
                    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1209
                     (info->flags & ~ZILOG_USR_MASK)))
1210
                        return -EPERM;
1211
                info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1212
                               (new_serial.flags & ZILOG_USR_MASK));
1213
                info->custom_divisor = new_serial.custom_divisor;
1214
                goto check_and_exit;
1215
        }
1216
 
1217
        if (info->count > 1)
1218
                return -EBUSY;
1219
 
1220
        /*
1221
         * OK, past this point, all the error checking has been done.
1222
         * At this point, we start making changes.....
1223
         */
1224
 
1225
        info->baud_base = new_serial.baud_base;
1226
        info->flags = ((info->flags & ~ZILOG_FLAGS) |
1227
                        (new_serial.flags & ZILOG_FLAGS));
1228
        info->type = new_serial.type;
1229
        info->close_delay = new_serial.close_delay;
1230
        info->closing_wait = new_serial.closing_wait;
1231
 
1232
check_and_exit:
1233
        retval = startup(info);
1234
        return retval;
1235
}
1236
 
1237
/*
1238
 * get_lsr_info - get line status register info
1239
 *
1240
 * Purpose: Let user call ioctl() to get info when the UART physically
1241
 *          is emptied.  On bus types like RS485, the transmitter must
1242
 *          release the bus after transmitting. This must be done when
1243
 *          the transmit shift register is empty, not be done when the
1244
 *          transmit holding register is empty.  This functionality
1245
 *          allows an RS485 driver to be written in user space.
1246
 */
1247
static int get_lsr_info(struct sgi_serial * info, unsigned int *value)
1248
{
1249
        volatile unsigned char junk;
1250
        unsigned char status;
1251
 
1252
        cli();
1253
        udelay(2);
1254
        status = info->zs_channel->control;
1255
        junk = sgint->istat0;
1256
        sti();
1257
        return put_user(status,value);
1258
}
1259
 
1260
static int get_modem_info(struct sgi_serial * info, unsigned int *value)
1261
{
1262
        unsigned char status;
1263
        unsigned int result;
1264
 
1265
        cli();
1266
        status = info->zs_channel->control;
1267
        udelay(2);
1268
        sti();
1269
        result =  ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
1270
                | ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
1271
                | ((status  & DCD) ? TIOCM_CAR : 0)
1272
                | ((status  & SYNC) ? TIOCM_DSR : 0)
1273
                | ((status  & CTS) ? TIOCM_CTS : 0);
1274
        if (put_user(result, value))
1275
                return -EFAULT;
1276
        return 0;
1277
}
1278
 
1279
static int set_modem_info(struct sgi_serial * info, unsigned int cmd,
1280
                          unsigned int *value)
1281
{
1282
        unsigned int arg;
1283
 
1284
        if (get_user(arg, value))
1285
                return -EFAULT;
1286
        switch (cmd) {
1287
        case TIOCMBIS:
1288
                if (arg & TIOCM_RTS)
1289
                        info->curregs[5] |= RTS;
1290
                if (arg & TIOCM_DTR)
1291
                        info->curregs[5] |= DTR;
1292
                break;
1293
        case TIOCMBIC:
1294
                if (arg & TIOCM_RTS)
1295
                        info->curregs[5] &= ~RTS;
1296
                if (arg & TIOCM_DTR)
1297
                        info->curregs[5] &= ~DTR;
1298
                break;
1299
        case TIOCMSET:
1300
                info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
1301
                             | ((arg & TIOCM_RTS) ? RTS : 0)
1302
                             | ((arg & TIOCM_DTR) ? DTR : 0));
1303
                break;
1304
        default:
1305
                return -EINVAL;
1306
        }
1307
        cli();
1308
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1309
        sti();
1310
        return 0;
1311
}
1312
 
1313
/*
1314
 * This routine sends a break character out the serial port.
1315
 */
1316
static void send_break( struct sgi_serial * info, int duration)
1317
{
1318
        if (!info->port)
1319
                return;
1320
        current->state = TASK_INTERRUPTIBLE;
1321
        cli();
1322
        write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1323
        schedule_timeout(duration);
1324
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1325
        sti();
1326
}
1327
 
1328
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1329
                    unsigned int cmd, unsigned long arg)
1330
{
1331
        struct sgi_serial * info = (struct sgi_serial *) tty->driver_data;
1332
        int retval;
1333
 
1334
        if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
1335
                return -ENODEV;
1336
 
1337
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1338
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1339
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1340
                if (tty->flags & (1 << TTY_IO_ERROR))
1341
                    return -EIO;
1342
        }
1343
 
1344
        switch (cmd) {
1345
                case TCSBRK:    /* SVID version: non-zero arg --> no break */
1346
                        retval = tty_check_change(tty);
1347
                        if (retval)
1348
                                return retval;
1349
                        tty_wait_until_sent(tty, 0);
1350
                        if (!arg)
1351
                                send_break(info, HZ/4); /* 1/4 second */
1352
                        return 0;
1353
                case TCSBRKP:   /* support for POSIX tcsendbreak() */
1354
                        retval = tty_check_change(tty);
1355
                        if (retval)
1356
                                return retval;
1357
                        tty_wait_until_sent(tty, 0);
1358
                        send_break(info, arg ? arg*(HZ/10) : HZ/4);
1359
                        return 0;
1360
                case TIOCGSOFTCAR:
1361
                        if (put_user(C_CLOCAL(tty) ? 1 : 0,
1362
                                     (unsigned long *) arg))
1363
                                return -EFAULT;
1364
                        return 0;
1365
                case TIOCSSOFTCAR:
1366
                        if (get_user(arg, (unsigned long *) arg))
1367
                                return -EFAULT;
1368
                        tty->termios->c_cflag =
1369
                                ((tty->termios->c_cflag & ~CLOCAL) |
1370
                                 (arg ? CLOCAL : 0));
1371
                        return 0;
1372
                case TIOCMGET:
1373
                        return get_modem_info(info, (unsigned int *) arg);
1374
                case TIOCMBIS:
1375
                case TIOCMBIC:
1376
                case TIOCMSET:
1377
                        return set_modem_info(info, cmd, (unsigned int *) arg);
1378
                case TIOCGSERIAL:
1379
                        return get_serial_info(info,
1380
                                               (struct serial_struct *) arg);
1381
                case TIOCSSERIAL:
1382
                        return set_serial_info(info,
1383
                                               (struct serial_struct *) arg);
1384
                case TIOCSERGETLSR: /* Get line status register */
1385
                        return get_lsr_info(info, (unsigned int *) arg);
1386
 
1387
                case TIOCSERGSTRUCT:
1388
                        if (copy_to_user((struct sgi_serial *) arg,
1389
                                    info, sizeof(struct sgi_serial)))
1390
                                return -EFAULT;
1391
                        return 0;
1392
 
1393
                default:
1394
                        return -ENOIOCTLCMD;
1395
                }
1396
        return 0;
1397
}
1398
 
1399
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1400
{
1401
        struct sgi_serial *info = (struct sgi_serial *)tty->driver_data;
1402
 
1403
        if (tty->termios->c_cflag == old_termios->c_cflag)
1404
                return;
1405
 
1406
        change_speed(info);
1407
 
1408
        if ((old_termios->c_cflag & CRTSCTS) &&
1409
            !(tty->termios->c_cflag & CRTSCTS)) {
1410
                tty->hw_stopped = 0;
1411
                rs_start(tty);
1412
        }
1413
}
1414
 
1415
/*
1416
 * ------------------------------------------------------------
1417
 * rs_close()
1418
 *
1419
 * This routine is called when the serial port gets closed.  First, we
1420
 * wait for the last remaining data to be sent.  Then, we unlink its
1421
 * ZILOG structure from the interrupt chain if necessary, and we free
1422
 * that IRQ if nothing is left in the chain.
1423
 * ------------------------------------------------------------
1424
 */
1425
static void rs_close(struct tty_struct *tty, struct file * filp)
1426
{
1427
        struct sgi_serial * info = (struct sgi_serial *)tty->driver_data;
1428
        unsigned long flags;
1429
 
1430
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1431
                return;
1432
 
1433
        save_flags(flags); cli();
1434
 
1435
        if (tty_hung_up_p(filp)) {
1436
                restore_flags(flags);
1437
                return;
1438
        }
1439
 
1440
#ifdef SERIAL_DEBUG_OPEN
1441
        printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1442
#endif
1443
        if ((tty->count == 1) && (info->count != 1)) {
1444
                /*
1445
                 * Uh, oh.  tty->count is 1, which means that the tty
1446
                 * structure will be freed.  Info->count should always
1447
                 * be one in these conditions.  If it's greater than
1448
                 * one, we've got real problems, since it means the
1449
                 * serial port won't be shutdown.
1450
                 */
1451
                printk("rs_close: bad serial port count; tty->count is 1, "
1452
                       "info->count is %d\n", info->count);
1453
                info->count = 1;
1454
        }
1455
        if (--info->count < 0) {
1456
                printk("rs_close: bad serial port count for ttys%d: %d\n",
1457
                       info->line, info->count);
1458
                info->count = 0;
1459
        }
1460
        if (info->count) {
1461
                restore_flags(flags);
1462
                return;
1463
        }
1464
        info->flags |= ZILOG_CLOSING;
1465
        /*
1466
         * Save the termios structure, since this port may have
1467
         * separate termios for callout and dialin.
1468
         */
1469
        if (info->flags & ZILOG_NORMAL_ACTIVE)
1470
                info->normal_termios = *tty->termios;
1471
        if (info->flags & ZILOG_CALLOUT_ACTIVE)
1472
                info->callout_termios = *tty->termios;
1473
        /*
1474
         * Now we wait for the transmit buffer to clear; and we notify
1475
         * the line discipline to only process XON/XOFF characters.
1476
         */
1477
        tty->closing = 1;
1478
        if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1479
                tty_wait_until_sent(tty, info->closing_wait);
1480
        /*
1481
         * At this point we stop accepting input.  To do this, we
1482
         * disable the receive line status interrupts, and tell the
1483
         * interrupt driver to stop checking the data ready bit in the
1484
         * line status register.
1485
         */
1486
        /** if (!info->iscons) ... **/
1487
        info->curregs[3] &= ~RxENABLE;
1488
        write_zsreg(info->zs_channel, 3, info->curregs[3]);
1489
        info->curregs[1] &= ~(0x18);
1490
        write_zsreg(info->zs_channel, 1, info->curregs[1]);
1491
        ZS_CLEARFIFO(info->zs_channel);
1492
 
1493
        shutdown(info);
1494
        if (tty->driver.flush_buffer)
1495
                tty->driver.flush_buffer(tty);
1496
        if (tty->ldisc.flush_buffer)
1497
                tty->ldisc.flush_buffer(tty);
1498
        tty->closing = 0;
1499
        info->event = 0;
1500
        info->tty = 0;
1501
        if (tty->ldisc.num != ldiscs[N_TTY].num) {
1502
                if (tty->ldisc.close)
1503
                        (tty->ldisc.close)(tty);
1504
                tty->ldisc = ldiscs[N_TTY];
1505
                tty->termios->c_line = N_TTY;
1506
                if (tty->ldisc.open)
1507
                        (tty->ldisc.open)(tty);
1508
        }
1509
        if (info->blocked_open) {
1510
                if (info->close_delay) {
1511
                        current->state = TASK_INTERRUPTIBLE;
1512
                        schedule_timeout(info->close_delay);
1513
                }
1514
                wake_up_interruptible(&info->open_wait);
1515
        }
1516
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1517
                         ZILOG_CLOSING);
1518
        wake_up_interruptible(&info->close_wait);
1519
        restore_flags(flags);
1520
}
1521
 
1522
/*
1523
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1524
 */
1525
void rs_hangup(struct tty_struct *tty)
1526
{
1527
        struct sgi_serial * info = (struct sgi_serial *)tty->driver_data;
1528
 
1529
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1530
                return;
1531
 
1532
        rs_flush_buffer(tty);
1533
        shutdown(info);
1534
        info->event = 0;
1535
        info->count = 0;
1536
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1537
        info->tty = 0;
1538
        wake_up_interruptible(&info->open_wait);
1539
}
1540
 
1541
/*
1542
 * ------------------------------------------------------------
1543
 * rs_open() and friends
1544
 * ------------------------------------------------------------
1545
 */
1546
static int block_til_ready(struct tty_struct *tty, struct file * filp,
1547
                           struct sgi_serial *info)
1548
{
1549
        DECLARE_WAITQUEUE(wait, current);
1550
        int             retval;
1551
        int             do_clocal = 0;
1552
 
1553
        /*
1554
         * If the device is in the middle of being closed, then block
1555
         * until it's done, and then try again.
1556
         */
1557
        if (info->flags & ZILOG_CLOSING) {
1558
                interruptible_sleep_on(&info->close_wait);
1559
#ifdef SERIAL_DO_RESTART
1560
                if (info->flags & ZILOG_HUP_NOTIFY)
1561
                        return -EAGAIN;
1562
                else
1563
                        return -ERESTARTSYS;
1564
#else
1565
                return -EAGAIN;
1566
#endif
1567
        }
1568
 
1569
        /*
1570
         * If this is a callout device, then just make sure the normal
1571
         * device isn't being used.
1572
         */
1573
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1574
                if (info->flags & ZILOG_NORMAL_ACTIVE)
1575
                        return -EBUSY;
1576
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1577
                    (info->flags & ZILOG_SESSION_LOCKOUT) &&
1578
                    (info->session != current->session))
1579
                    return -EBUSY;
1580
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1581
                    (info->flags & ZILOG_PGRP_LOCKOUT) &&
1582
                    (info->pgrp != current->pgrp))
1583
                    return -EBUSY;
1584
                info->flags |= ZILOG_CALLOUT_ACTIVE;
1585
                return 0;
1586
        }
1587
 
1588
        /*
1589
         * If non-blocking mode is set, or the port is not enabled,
1590
         * then make the check up front and then exit.
1591
         */
1592
        if ((filp->f_flags & O_NONBLOCK) ||
1593
            (tty->flags & (1 << TTY_IO_ERROR))) {
1594
                if (info->flags & ZILOG_CALLOUT_ACTIVE)
1595
                        return -EBUSY;
1596
                info->flags |= ZILOG_NORMAL_ACTIVE;
1597
                return 0;
1598
        }
1599
 
1600
        if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1601
                if (info->normal_termios.c_cflag & CLOCAL)
1602
                        do_clocal = 1;
1603
        } else {
1604
                if (tty->termios->c_cflag & CLOCAL)
1605
                        do_clocal = 1;
1606
        }
1607
 
1608
        /*
1609
         * Block waiting for the carrier detect and the line to become
1610
         * free (i.e., not in use by the callout).  While we are in
1611
         * this loop, info->count is dropped by one, so that
1612
         * rs_close() knows when to free things.  We restore it upon
1613
         * exit, either normal or abnormal.
1614
         */
1615
        retval = 0;
1616
        add_wait_queue(&info->open_wait, &wait);
1617
#ifdef SERIAL_DEBUG_OPEN
1618
        printk("block_til_ready before block: ttys%d, count = %d\n",
1619
               info->line, info->count);
1620
#endif
1621
        info->count--;
1622
        info->blocked_open++;
1623
        while (1) {
1624
                cli();
1625
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1626
                        zs_rtsdtr(info, 1);
1627
                sti();
1628
                set_current_state(TASK_INTERRUPTIBLE);
1629
                if (tty_hung_up_p(filp) ||
1630
                    !(info->flags & ZILOG_INITIALIZED)) {
1631
#ifdef SERIAL_DO_RESTART
1632
                        if (info->flags & ZILOG_HUP_NOTIFY)
1633
                                retval = -EAGAIN;
1634
                        else
1635
                                retval = -ERESTARTSYS;
1636
#else
1637
                        retval = -EAGAIN;
1638
#endif
1639
                        break;
1640
                }
1641
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1642
                    !(info->flags & ZILOG_CLOSING) && do_clocal)
1643
                        break;
1644
                if (signal_pending(current)) {
1645
                        retval = -ERESTARTSYS;
1646
                        break;
1647
                }
1648
#ifdef SERIAL_DEBUG_OPEN
1649
                printk("block_til_ready blocking: ttys%d, count = %d\n",
1650
                       info->line, info->count);
1651
#endif
1652
                schedule();
1653
        }
1654
        current->state = TASK_RUNNING;
1655
        remove_wait_queue(&info->open_wait, &wait);
1656
        if (!tty_hung_up_p(filp))
1657
                info->count++;
1658
        info->blocked_open--;
1659
#ifdef SERIAL_DEBUG_OPEN
1660
        printk("block_til_ready after blocking: ttys%d, count = %d\n",
1661
               info->line, info->count);
1662
#endif
1663
        if (retval)
1664
                return retval;
1665
        info->flags |= ZILOG_NORMAL_ACTIVE;
1666
        return 0;
1667
}
1668
 
1669
/*
1670
 * This routine is called whenever a serial port is opened.  It
1671
 * enables interrupts for a serial port, linking in its ZILOG structure into
1672
 * the IRQ chain.   It also performs the serial-specific
1673
 * initialization for the tty structure.
1674
 */
1675
int rs_open(struct tty_struct *tty, struct file * filp)
1676
{
1677
        struct sgi_serial       *info;
1678
        int                     retval, line;
1679
 
1680
        line = MINOR(tty->device) - tty->driver.minor_start;
1681
        /* The zilog lines for the mouse/keyboard must be
1682
         * opened using their respective drivers.
1683
         */
1684
        if ((line < 0) || (line >= NUM_CHANNELS))
1685
                return -ENODEV;
1686
        info = zs_soft + line;
1687
        /* Is the kgdb running over this line? */
1688
        if (info->kgdb_channel)
1689
                return -ENODEV;
1690
        if (serial_paranoia_check(info, tty->device, "rs_open"))
1691
                return -ENODEV;
1692
#ifdef SERIAL_DEBUG_OPEN
1693
        printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1694
               info->count);
1695
#endif
1696
        info->count++;
1697
        tty->driver_data = info;
1698
        info->tty = tty;
1699
 
1700
        /*
1701
         * Start up serial port
1702
         */
1703
        retval = startup(info);
1704
        if (retval)
1705
                return retval;
1706
 
1707
        retval = block_til_ready(tty, filp, info);
1708
        if (retval) {
1709
#ifdef SERIAL_DEBUG_OPEN
1710
                printk("rs_open returning after block_til_ready with %d\n",
1711
                       retval);
1712
#endif
1713
                return retval;
1714
        }
1715
 
1716
        if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1717
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1718
                        *tty->termios = info->normal_termios;
1719
                else
1720
                        *tty->termios = info->callout_termios;
1721
                change_speed(info);
1722
        }
1723
 
1724
        /* If this is the serial console change the speed to
1725
         * the right value
1726
         */
1727
        if (info->is_cons) {
1728
                info->tty->termios->c_cflag = sgisercon->cflag;
1729
                change_speed(info);
1730
        }
1731
 
1732
        info->session = current->session;
1733
        info->pgrp = current->pgrp;
1734
 
1735
#ifdef SERIAL_DEBUG_OPEN
1736
        printk("rs_open ttys%d successful...\n", info->line);
1737
#endif
1738
        return 0;
1739
}
1740
 
1741
/* Finally, routines used to initialize the serial driver. */
1742
 
1743
static void show_serial_version(void)
1744
{
1745
        printk("SGI Zilog8530 serial driver version 1.00\n");
1746
}
1747
 
1748
/* Return layout for the requested zs chip number. */
1749
static inline struct sgi_zslayout *get_zs(int chip)
1750
{
1751
        if (chip > 0)
1752
                panic("Wheee, bogus zs chip number requested.");
1753
 
1754
        return (struct sgi_zslayout *) (&sgioc->serport);
1755
}
1756
 
1757
 
1758
static inline void
1759
rs_cons_check(struct sgi_serial *ss, int channel)
1760
{
1761
        int i, o, io;
1762
        static int msg_printed = 0;
1763
 
1764
        i = o = io = 0;
1765
 
1766
        /* Is this one of the serial console lines? */
1767
        if((zs_cons_chanout != channel) &&
1768
           (zs_cons_chanin != channel))
1769
                return;
1770
        zs_conschan = ss->zs_channel;
1771
        zs_consinfo = ss;
1772
 
1773
 
1774
 
1775
        /* If this is console input, we handle the break received
1776
         * status interrupt on this line to mean prom_halt().
1777
         */
1778
        if(zs_cons_chanin == channel) {
1779
                ss->break_abort = 1;
1780
                i = 1;
1781
        }
1782
        if(o && i)
1783
                io = 1;
1784
 
1785
        /* Set flag variable for this port so that it cannot be
1786
         * opened for other uses by accident.
1787
         */
1788
        ss->is_cons = 1;
1789
 
1790
        if(io) {
1791
                if (!msg_printed) {
1792
                        printk("zs%d: console I/O\n", ((channel>>1)&1));
1793
                        msg_printed = 1;
1794
                }
1795
 
1796
        } else {
1797
                printk("zs%d: console %s\n", ((channel>>1)&1),
1798
                       (i==1 ? "input" : (o==1 ? "output" : "WEIRD")));
1799
        }
1800
}
1801
 
1802
/* rs_init inits the driver */
1803
int rs_init(void)
1804
{
1805
        unsigned long flags;
1806
        int chip, channel, i;
1807
        struct sgi_serial *info;
1808
 
1809
 
1810
        /* Setup base handler, and timer table. */
1811
        init_bh(SERIAL_BH, do_serial_bh);
1812
 
1813
        show_serial_version();
1814
 
1815
        /* Initialize the tty_driver structure */
1816
        /* SGI: Not all of this is exactly right for us. */
1817
 
1818
        memset(&serial_driver, 0, sizeof(struct tty_driver));
1819
        serial_driver.magic = TTY_DRIVER_MAGIC;
1820
#ifdef CONFIG_DEVFS_FS
1821
        serial_driver.name = "tts/%d";
1822
#else
1823
        serial_driver.name = "ttyS";
1824
#endif
1825
        serial_driver.major = TTY_MAJOR;
1826
        serial_driver.minor_start = 64;
1827
        serial_driver.num = NUM_CHANNELS;
1828
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1829
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
1830
        serial_driver.init_termios = tty_std_termios;
1831
 
1832
        serial_driver.init_termios.c_cflag =
1833
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1834
        serial_driver.flags = TTY_DRIVER_REAL_RAW;
1835
        serial_driver.refcount = &serial_refcount;
1836
        serial_driver.table = serial_table;
1837
        serial_driver.termios = serial_termios;
1838
        serial_driver.termios_locked = serial_termios_locked;
1839
 
1840
        serial_driver.open = rs_open;
1841
        serial_driver.close = rs_close;
1842
        serial_driver.write = rs_write;
1843
        serial_driver.flush_chars = rs_flush_chars;
1844
        serial_driver.write_room = rs_write_room;
1845
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
1846
        serial_driver.flush_buffer = rs_flush_buffer;
1847
        serial_driver.ioctl = rs_ioctl;
1848
        serial_driver.throttle = rs_throttle;
1849
        serial_driver.unthrottle = rs_unthrottle;
1850
        serial_driver.set_termios = rs_set_termios;
1851
        serial_driver.stop = rs_stop;
1852
        serial_driver.start = rs_start;
1853
        serial_driver.hangup = rs_hangup;
1854
 
1855
        /*
1856
         * The callout device is just like normal device except for
1857
         * major number and the subtype code.
1858
         */
1859
        callout_driver = serial_driver;
1860
#ifdef CONFIG_DEVFS_FS
1861
        callout_driver.name = "cua/%d";
1862
#else
1863
        callout_driver.name = "cua";
1864
#endif
1865
        callout_driver.major = TTYAUX_MAJOR;
1866
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1867
 
1868
        if (tty_register_driver(&serial_driver))
1869
                panic("Couldn't register serial driver");
1870
        if (tty_register_driver(&callout_driver))
1871
                panic("Couldn't register callout driver");
1872
 
1873
        save_flags(flags); cli();
1874
 
1875
        /* Set up our interrupt linked list */
1876
        zs_chain = &zs_soft[0];
1877
        zs_soft[0].zs_next = &zs_soft[1];
1878
        zs_soft[1].zs_next = 0;
1879
 
1880
        for(chip = 0; chip < NUM_SERIAL; chip++) {
1881
                /* If we are doing kgdb over one of the channels on
1882
                 * chip zero, kgdb_channel will be set to 1 by the
1883
                 * rs_kgdb_hook() routine below.
1884
                 */
1885
                if(!zs_chips[chip]) {
1886
                        zs_chips[chip] = get_zs(chip);
1887
                        /* Two channels per chip */
1888
                        zs_channels[(chip*2)] = &zs_chips[chip]->channelB;
1889
                        zs_channels[(chip*2)+1] = &zs_chips[chip]->channelA;
1890
                        zs_soft[(chip*2)].kgdb_channel = 0;
1891
                        zs_soft[(chip*2)+1].kgdb_channel = 0;
1892
                }
1893
                /* First, set up channel A on this chip. */
1894
                channel = chip * 2;
1895
                zs_soft[channel].zs_channel = zs_channels[channel];
1896
                zs_soft[channel].change_needed = 0;
1897
                zs_soft[channel].clk_divisor = 16;
1898
                zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1899
                zs_soft[channel].cons_mouse = 0;
1900
                /* If not keyboard/mouse and is console serial
1901
                 * line, then enable receiver interrupts.
1902
                 */
1903
                if(zs_soft[channel].is_cons) {
1904
                        write_zsreg(zs_soft[channel].zs_channel, R1,
1905
                                    (EXT_INT_ENAB | INT_ALL_Rx));
1906
                        write_zsreg(zs_soft[channel].zs_channel, R9, (NV | MIE));
1907
                        write_zsreg(zs_soft[channel].zs_channel, R10, (NRZ));
1908
                        write_zsreg(zs_soft[channel].zs_channel, R3, (Rx8|RxENABLE));
1909
                        write_zsreg(zs_soft[channel].zs_channel, R5, (Tx8 | TxENAB));
1910
                }
1911
                /* If this is the kgdb line, enable interrupts because we
1912
                 * now want to receive the 'control-c' character from the
1913
                 * client attached to us asynchronously.
1914
                 */
1915
                if(zs_soft[channel].kgdb_channel)
1916
                        kgdb_chaninit(&zs_soft[channel], 1,
1917
                                      zs_soft[channel].zs_baud);
1918
 
1919
                /* Now, channel B */
1920
                channel++;
1921
                zs_soft[channel].zs_channel = zs_channels[channel];
1922
                zs_soft[channel].change_needed = 0;
1923
                zs_soft[channel].clk_divisor = 16;
1924
                zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1925
                zs_soft[channel].cons_keyb = 0;
1926
                /* If console serial line, then enable receiver interrupts. */
1927
                if(zs_soft[channel].is_cons) {
1928
                        write_zsreg(zs_soft[channel].zs_channel, R1,
1929
                                    (EXT_INT_ENAB | INT_ALL_Rx));
1930
                        write_zsreg(zs_soft[channel].zs_channel, R9,
1931
                                    (NV | MIE));
1932
                        write_zsreg(zs_soft[channel].zs_channel, R10,
1933
                                    (NRZ));
1934
                        write_zsreg(zs_soft[channel].zs_channel, R3,
1935
                                    (Rx8|RxENABLE));
1936
                        write_zsreg(zs_soft[channel].zs_channel, R5,
1937
                                    (Tx8 | TxENAB | RTS | DTR));
1938
                }
1939
        }
1940
 
1941
        for(info=zs_chain, i=0; info; info = info->zs_next, i++)
1942
        {
1943
                info->magic = SERIAL_MAGIC;
1944
                info->port = (int) info->zs_channel;
1945
                info->line = i;
1946
                info->tty = 0;
1947
                info->irq = zilog_irq;
1948
                info->custom_divisor = 16;
1949
                info->close_delay = 50;
1950
                info->closing_wait = 3000;
1951
                info->x_char = 0;
1952
                info->event = 0;
1953
                info->count = 0;
1954
                info->blocked_open = 0;
1955
                info->tqueue.routine = do_softint;
1956
                info->tqueue.data = info;
1957
                info->tqueue_hangup.routine = do_serial_hangup;
1958
                info->tqueue_hangup.data = info;
1959
                info->callout_termios =callout_driver.init_termios;
1960
                info->normal_termios = serial_driver.init_termios;
1961
                init_waitqueue_head(&info->open_wait);
1962
                init_waitqueue_head(&info->close_wait);
1963
                printk("tty%02d at 0x%04x (irq = %d)", info->line,
1964
                       info->port, info->irq);
1965
                printk(" is a Zilog8530\n");
1966
        }
1967
 
1968
        if (request_irq(zilog_irq, rs_interrupt, (SA_INTERRUPT),
1969
                        "Zilog8530", zs_chain))
1970
                panic("Unable to attach zs intr");
1971
        restore_flags(flags);
1972
 
1973
        return 0;
1974
}
1975
 
1976
/*
1977
 * register_serial and unregister_serial allows for serial ports to be
1978
 * configured at run-time, to support PCMCIA modems.
1979
 */
1980
/* SGI: Unused at this time, just here to make things link. */
1981
int register_serial(struct serial_struct *req)
1982
{
1983
        return -1;
1984
}
1985
 
1986
void unregister_serial(int line)
1987
{
1988
        return;
1989
}
1990
 
1991
/* Hooks for running a serial console.  con_init() calls this if the
1992
 * console is being run over one of the ttya/ttyb serial ports.
1993
 * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
1994
 * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
1995
 * are addressed backwards, channel B is first, then channel A.
1996
 */
1997
void
1998
rs_cons_hook(int chip, int out, int line)
1999
{
2000
        int channel;
2001
 
2002
        if(chip)
2003
                panic("rs_cons_hook called with chip not zero");
2004
        if(line != 0 && line != 1)
2005
                panic("rs_cons_hook called with line not ttya or ttyb");
2006
        channel = line;
2007
        if(!zs_chips[chip]) {
2008
                zs_chips[chip] = get_zs(chip);
2009
                /* Two channels per chip */
2010
                zs_channels[(chip*2)] = &zs_chips[chip]->channelB;
2011
                zs_channels[(chip*2)+1] = &zs_chips[chip]->channelA;
2012
        }
2013
        zs_soft[channel].zs_channel = zs_channels[channel];
2014
        zs_soft[channel].change_needed = 0;
2015
        zs_soft[channel].clk_divisor = 16;
2016
        zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2017
        if(out)
2018
                zs_cons_chanout = ((chip * 2) + channel);
2019
        else
2020
                zs_cons_chanin = ((chip * 2) + channel);
2021
 
2022
        rs_cons_check(&zs_soft[channel], channel);
2023
}
2024
 
2025
/* This is called at boot time to prime the kgdb serial debugging
2026
 * serial line.  The 'tty_num' argument is 0 for /dev/ttyd2 and 1 for
2027
 * /dev/ttyd1 (yes they are backwards on purpose) which is determined
2028
 * in setup_arch() from the boot command line flags.
2029
 */
2030
void
2031
rs_kgdb_hook(int tty_num)
2032
{
2033
        int chip = 0;
2034
 
2035
        if(!zs_chips[chip]) {
2036
                zs_chips[chip] = get_zs(chip);
2037
                /* Two channels per chip */
2038
                zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2039
                zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2040
        }
2041
        zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2042
        zs_kgdbchan = zs_soft[tty_num].zs_channel;
2043
        zs_soft[tty_num].change_needed = 0;
2044
        zs_soft[tty_num].clk_divisor = 16;
2045
        zs_soft[tty_num].zs_baud = get_zsbaud(&zs_soft[tty_num]);
2046
        zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
2047
        zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2048
 
2049
        /* Turn on transmitter/receiver at 8-bits/char */
2050
        kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2051
        ZS_CLEARERR(zs_kgdbchan);
2052
        udelay(5);
2053
        ZS_CLEARFIFO(zs_kgdbchan);
2054
}
2055
 
2056
static void zs_console_write(struct console *co, const char *str,
2057
                             unsigned int count)
2058
{
2059
 
2060
        while(count--) {
2061
                if(*str == '\n')
2062
                        zs_cons_put_char('\r');
2063
                zs_cons_put_char(*str++);
2064
        }
2065
 
2066
        /* Comment this if you want to have a strict interrupt-driven output */
2067
        rs_fair_output();
2068
}
2069
 
2070
static kdev_t zs_console_device(struct console *con)
2071
{
2072
        return MKDEV(TTY_MAJOR, 64 + con->index);
2073
}
2074
 
2075
 
2076
static int __init zs_console_setup(struct console *con, char *options)
2077
{
2078
        struct sgi_serial *info;
2079
        int baud = 9600;
2080
        int bits = 8;
2081
        int parity = 'n';
2082
        int cflag = CREAD | HUPCL | CLOCAL;
2083
        char *s;
2084
        int i, brg;
2085
 
2086
        if(options) {
2087
                baud = simple_strtoul(options, NULL, 10);
2088
                s = options;
2089
                while(*s >= '0' && *s <= '9')
2090
                        s++;
2091
                if (*s) parity = *s++;
2092
                if (*s) bits   = *s - '0';
2093
        }
2094
        /* Now construct a cflag setting. */
2095
        switch(baud) {
2096
                case 1200:
2097
                        cflag |= B1200;
2098
                        break;
2099
                case 2400:
2100
                        cflag |= B2400;
2101
                        break;
2102
                case 4800:
2103
                        cflag |= B4800;
2104
                        break;
2105
                case 19200:
2106
                        cflag |= B19200;
2107
                        break;
2108
                case 38400:
2109
                        cflag |= B38400;
2110
                        break;
2111
                case 57600:
2112
                        cflag |= B57600;
2113
                        break;
2114
                case 115200:
2115
                        cflag |= B115200;
2116
                        break;
2117
                case 9600:
2118
                default:
2119
                        cflag |= B9600;
2120
                        break;
2121
        }
2122
        switch(bits) {
2123
                case 7:
2124
                        cflag |= CS7;
2125
                        break;
2126
                default:
2127
                case 8:
2128
                        cflag |= CS8;
2129
                        break;
2130
        }
2131
        switch(parity) {
2132
                case 'o': case 'O':
2133
                        cflag |= PARODD;
2134
                        break;
2135
                case 'e': case 'E':
2136
                        cflag |= PARENB;
2137
                        break;
2138
        }
2139
        con->cflag = cflag;
2140
 
2141
        rs_cons_hook(0, 0, con->index);
2142
        info = zs_soft + con->index;
2143
        info->is_cons = 1;
2144
 
2145
        printk("Console: ttyS%d (Zilog8530), %d baud\n",
2146
                                                info->line, baud);
2147
 
2148
        i = con->cflag & CBAUD;
2149
        if (con->cflag & CBAUDEX) {
2150
                i &= ~CBAUDEX;
2151
                con->cflag &= ~CBAUDEX;
2152
        }
2153
        info->zs_baud = baud;
2154
 
2155
        switch (con->cflag & CSIZE) {
2156
                case CS5:
2157
                        zscons_regs[3] = Rx5 | RxENABLE;
2158
                        zscons_regs[5] = Tx5 | TxENAB;
2159
                        break;
2160
                case CS6:
2161
                        zscons_regs[3] = Rx6 | RxENABLE;
2162
                        zscons_regs[5] = Tx6 | TxENAB;
2163
                        break;
2164
                case CS7:
2165
                        zscons_regs[3] = Rx7 | RxENABLE;
2166
                        zscons_regs[5] = Tx7 | TxENAB;
2167
                        break;
2168
                default:
2169
                case CS8:
2170
                        zscons_regs[3] = Rx8 | RxENABLE;
2171
                        zscons_regs[5] = Tx8 | TxENAB;
2172
                        break;
2173
        }
2174
        zscons_regs[5] |= DTR;
2175
 
2176
        if (con->cflag & PARENB)
2177
                zscons_regs[4] |= PAR_ENA;
2178
        if (!(con->cflag & PARODD))
2179
                zscons_regs[4] |= PAR_EVEN;
2180
 
2181
        if (con->cflag & CSTOPB)
2182
                zscons_regs[4] |= SB2;
2183
        else
2184
                zscons_regs[4] |= SB1;
2185
 
2186
        sgisercon = con;
2187
 
2188
        brg = BPS_TO_BRG(baud, ZS_CLOCK / info->clk_divisor);
2189
        zscons_regs[12] = brg & 0xff;
2190
        zscons_regs[13] = (brg >> 8) & 0xff;
2191
        memcpy(info->curregs, zscons_regs, sizeof(zscons_regs));
2192
        load_zsregs(info->zs_channel, zscons_regs);
2193
        ZS_CLEARERR(info->zs_channel);
2194
        ZS_CLEARFIFO(info->zs_channel);
2195
        return 0;
2196
}
2197
 
2198
static struct console sgi_console_driver = {
2199
        .name           = "ttyS",
2200
        .write          = zs_console_write,
2201
        .device         = zs_console_device,
2202
        .setup          = zs_console_setup,
2203
        .flags          = CON_PRINTBUFFER,
2204
        .index          = -1,
2205
};
2206
 
2207
/*
2208
 *      Register console.
2209
 */
2210
void __init sgi_serial_console_init(void)
2211
{
2212
        register_console(&sgi_console_driver);
2213
}
2214
__initcall(rs_init);

powered by: WebSVN 2.1.0

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