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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [sbus/] [char/] [sunserial.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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