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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * dz.c: Serial port driver for DECStations equiped
3
 *       with the DZ chipset.
4
 *
5
 * Copyright (C) 1998 Olivier A. D. Lebaillif
6
 *
7
 * Email: olivier.lebaillif@ifrsys.com
8
 *
9
 * [31-AUG-98] triemer
10
 * Changed IRQ to use Harald's dec internals interrupts.h
11
 * removed base_addr code - moving address assignment to setup.c
12
 * Changed name of dz_init to rs_init to be consistent with tc code
13
 * [13-NOV-98] triemer fixed code to receive characters
14
 *    after patches by harald to irq code.
15
 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
16
 *            field from "current" - somewhere between 2.1.121 and 2.1.131
17
 Qua Jun 27 15:02:26 BRT 2001
18
 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
19
 *
20
 * Parts (C) 1999 David Airlie, airlied@linux.ie
21
 * [07-SEP-99] Bugfixes
22
 */
23
 
24
#undef DEBUG_DZ
25
 
26
#include <linux/config.h>
27
#include <linux/version.h>
28
#include <linux/kernel.h>
29
#include <linux/sched.h>
30
#include <linux/init.h>
31
#include <linux/slab.h>
32
#include <linux/mm.h>
33
#include <linux/major.h>
34
#include <linux/module.h>
35
#include <linux/param.h>
36
#include <linux/tqueue.h>
37
#include <linux/interrupt.h>
38
 
39
#include <linux/console.h>
40
#include <linux/tty.h>
41
#include <linux/tty_flip.h>
42
#include <linux/serial.h>
43
 
44
#include <linux/ptrace.h>
45
#include <linux/fs.h>
46
 
47
#include <asm/bootinfo.h>
48
#include <asm/dec/interrupts.h>
49
#include <asm/dec/kn01.h>
50
#include <asm/dec/kn02.h>
51
#include <asm/dec/machtype.h>
52
#include <asm/dec/prom.h>
53
#include <asm/irq.h>
54
#include <asm/system.h>
55
#include <asm/uaccess.h>
56
 
57
#define CONSOLE_LINE (3)        /* for definition of struct console */
58
 
59
#include "dz.h"
60
 
61
#define DZ_INTR_DEBUG 1
62
 
63
DECLARE_TASK_QUEUE(tq_serial);
64
 
65
static struct dz_serial *lines[4];
66
static unsigned char tmp_buffer[256];
67
 
68
#ifdef DEBUG_DZ
69
/*
70
 * debugging code to send out chars via prom
71
 */
72
static void debug_console(const char *s, int count)
73
{
74
        unsigned i;
75
 
76
        for (i = 0; i < count; i++) {
77
                if (*s == 10)
78
                        prom_printf("%c", 13);
79
                prom_printf("%c", *s++);
80
        }
81
}
82
#endif
83
 
84
/*
85
 * ------------------------------------------------------------
86
 * dz_in () and dz_out ()
87
 *
88
 * These routines are used to access the registers of the DZ
89
 * chip, hiding relocation differences between implementation.
90
 * ------------------------------------------------------------
91
 */
92
 
93
static inline unsigned short dz_in(struct dz_serial *info, unsigned offset)
94
{
95
        volatile unsigned short *addr =
96
                (volatile unsigned short *) (info->port + offset);
97
        return *addr;
98
}
99
 
100
static inline void dz_out(struct dz_serial *info, unsigned offset,
101
                          unsigned short value)
102
{
103
 
104
        volatile unsigned short *addr =
105
                (volatile unsigned short *) (info->port + offset);
106
        *addr = value;
107
}
108
 
109
/*
110
 * ------------------------------------------------------------
111
 * rs_stop () and rs_start ()
112
 *
113
 * These routines are called before setting or resetting
114
 * tty->stopped. They enable or disable transmitter interrupts,
115
 * as necessary.
116
 * ------------------------------------------------------------
117
 */
118
 
119
static void dz_stop(struct tty_struct *tty)
120
{
121
        struct dz_serial *info;
122
        unsigned short mask, tmp;
123
 
124
        if (tty == 0)
125
                return;
126
 
127
        info = (struct dz_serial *) tty->driver_data;
128
 
129
        mask = 1 << info->line;
130
        tmp = dz_in(info, DZ_TCR);      /* read the TX flag */
131
 
132
        tmp &= ~mask;           /* clear the TX flag */
133
        dz_out(info, DZ_TCR, tmp);
134
}
135
 
136
static void dz_start(struct tty_struct *tty)
137
{
138
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
139
        unsigned short mask, tmp;
140
 
141
        mask = 1 << info->line;
142
        tmp = dz_in(info, DZ_TCR);      /* read the TX flag */
143
 
144
        tmp |= mask;            /* set the TX flag */
145
        dz_out(info, DZ_TCR, tmp);
146
 
147
}
148
 
149
/*
150
 * ------------------------------------------------------------
151
 * Here starts the interrupt handling routines.  All of the
152
 * following subroutines are declared as inline and are folded
153
 * into dz_interrupt.  They were separated out for readability's
154
 * sake.
155
 *
156
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
157
 * runs with interrupts turned off.  People who may want to modify
158
 * rs_interrupt() should try to keep the interrupt handler as fast as
159
 * possible.  After you are done making modifications, it is not a bad
160
 * idea to do:
161
 *
162
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer dz.c
163
 *
164
 * and look at the resulting assemble code in serial.s.
165
 *
166
 * ------------------------------------------------------------
167
 */
168
 
169
/*
170
 * ------------------------------------------------------------
171
 * dz_sched_event ()
172
 *
173
 * This routine is used by the interrupt handler to schedule
174
 * processing in the software interrupt portion of the driver.
175
 * ------------------------------------------------------------
176
 */
177
static inline void dz_sched_event(struct dz_serial *info, int event)
178
{
179
        info->event |= 1 << event;
180
        queue_task(&info->tqueue, &tq_serial);
181
        mark_bh(SERIAL_BH);
182
}
183
 
184
/*
185
 * ------------------------------------------------------------
186
 * receive_char ()
187
 *
188
 * This routine deals with inputs from any lines.
189
 * ------------------------------------------------------------
190
 */
191
static inline void receive_chars(struct dz_serial *info_in)
192
{
193
 
194
        struct dz_serial *info;
195
        struct tty_struct *tty = 0;
196
        struct async_icount *icount;
197
        int ignore = 0;
198
        unsigned short status, tmp;
199
        unsigned char ch;
200
 
201
        /* this code is going to be a problem...
202
           the call to tty_flip_buffer is going to need
203
           to be rethought...
204
         */
205
        do {
206
                status = dz_in(info_in, DZ_RBUF);
207
                info = lines[LINE(status)];
208
 
209
                /* punt so we don't get duplicate characters */
210
                if (!(status & DZ_DVAL))
211
                        goto ignore_char;
212
 
213
                ch = UCHAR(status);     /* grab the char */
214
 
215
#if 0
216
                if (info->is_console) {
217
                        if (ch == 0)
218
                                return; /* it's a break ... */
219
                }
220
#endif
221
 
222
                tty = info->tty;        /* now tty points to the proper dev */
223
                icount = &info->icount;
224
 
225
                if (!tty)
226
                        break;
227
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
228
                        break;
229
 
230
                *tty->flip.char_buf_ptr = ch;
231
                *tty->flip.flag_buf_ptr = 0;
232
                icount->rx++;
233
 
234
                /* keep track of the statistics */
235
                if (status & (DZ_OERR | DZ_FERR | DZ_PERR)) {
236
                        if (status & DZ_PERR)   /* parity error */
237
                                icount->parity++;
238
                        else if (status & DZ_FERR)      /* frame error */
239
                                icount->frame++;
240
                        if (status & DZ_OERR)   /* overrun error */
241
                                icount->overrun++;
242
 
243
                        /*  check to see if we should ignore the character
244
                           and mask off conditions that should be ignored
245
                         */
246
 
247
                        if (status & info->ignore_status_mask) {
248
                                if (++ignore > 100)
249
                                        break;
250
                                goto ignore_char;
251
                        }
252
                        /* mask off the error conditions we want to ignore */
253
                        tmp = status & info->read_status_mask;
254
 
255
                        if (tmp & DZ_PERR) {
256
                                *tty->flip.flag_buf_ptr = TTY_PARITY;
257
#ifdef DEBUG_DZ
258
                                debug_console("PERR\n", 5);
259
#endif
260
                        } else if (tmp & DZ_FERR) {
261
                                *tty->flip.flag_buf_ptr = TTY_FRAME;
262
#ifdef DEBUG_DZ
263
                                debug_console("FERR\n", 5);
264
#endif
265
                        }
266
                        if (tmp & DZ_OERR) {
267
#ifdef DEBUG_DZ
268
                                debug_console("OERR\n", 5);
269
#endif
270
                                if (tty->flip.count < TTY_FLIPBUF_SIZE) {
271
                                        tty->flip.count++;
272
                                        tty->flip.flag_buf_ptr++;
273
                                        tty->flip.char_buf_ptr++;
274
                                        *tty->flip.flag_buf_ptr = TTY_OVERRUN;
275
                                }
276
                        }
277
                }
278
                tty->flip.flag_buf_ptr++;
279
                tty->flip.char_buf_ptr++;
280
                tty->flip.count++;
281
              ignore_char:
282
        } while (status & DZ_DVAL);
283
 
284
        if (tty)
285
                tty_flip_buffer_push(tty);
286
}
287
 
288
/*
289
 * ------------------------------------------------------------
290
 * transmit_char ()
291
 *
292
 * This routine deals with outputs to any lines.
293
 * ------------------------------------------------------------
294
 */
295
static inline void transmit_chars(struct dz_serial *info)
296
{
297
        unsigned char tmp;
298
 
299
 
300
 
301
        if (info->x_char) {     /* XON/XOFF chars */
302
                dz_out(info, DZ_TDR, info->x_char);
303
                info->icount.tx++;
304
                info->x_char = 0;
305
                return;
306
        }
307
        /* if nothing to do or stopped or hardware stopped */
308
        if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tty->hw_stopped) {
309
                dz_stop(info->tty);
310
                return;
311
        }
312
        /*
313
         * if something to do ... (rember the dz has no output fifo so we go
314
         * one char at a time :-<
315
         */
316
        tmp = (unsigned short) info->xmit_buf[info->xmit_tail++];
317
        dz_out(info, DZ_TDR, tmp);
318
        info->xmit_tail = info->xmit_tail & (DZ_XMIT_SIZE - 1);
319
        info->icount.tx++;
320
 
321
        if (--info->xmit_cnt < WAKEUP_CHARS)
322
                dz_sched_event(info, DZ_EVENT_WRITE_WAKEUP);
323
 
324
 
325
        /* Are we done */
326
        if (info->xmit_cnt <= 0)
327
                dz_stop(info->tty);
328
}
329
 
330
/*
331
 * ------------------------------------------------------------
332
 * check_modem_status ()
333
 *
334
 * Only valid for the MODEM line duh !
335
 * ------------------------------------------------------------
336
 */
337
static inline void check_modem_status(struct dz_serial *info)
338
{
339
        unsigned short status;
340
 
341
        /* if not ne modem line just return */
342
        if (info->line != DZ_MODEM)
343
                return;
344
 
345
        status = dz_in(info, DZ_MSR);
346
 
347
        /* it's easy, since DSR2 is the only bit in the register */
348
        if (status)
349
                info->icount.dsr++;
350
}
351
 
352
/*
353
 * ------------------------------------------------------------
354
 * dz_interrupt ()
355
 *
356
 * this is the main interrupt routine for the DZ chip.
357
 * It deals with the multiple ports.
358
 * ------------------------------------------------------------
359
 */
360
static void dz_interrupt(int irq, void *dev, struct pt_regs *regs)
361
{
362
        struct dz_serial *info;
363
        unsigned short status;
364
 
365
        /* get the reason why we just got an irq */
366
        status = dz_in((struct dz_serial *) dev, DZ_CSR);
367
        info = lines[LINE(status)];     /* re-arrange info the proper port */
368
 
369
        if (status & DZ_RDONE)
370
                receive_chars(info);    /* the receive function */
371
 
372
        if (status & DZ_TRDY)
373
                transmit_chars(info);
374
}
375
 
376
/*
377
 * -------------------------------------------------------------------
378
 * Here ends the DZ interrupt routines.
379
 * -------------------------------------------------------------------
380
 */
381
 
382
/*
383
 * This routine is used to handle the "bottom half" processing for the
384
 * serial driver, known also the "software interrupt" processing.
385
 * This processing is done at the kernel interrupt level, after the
386
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
387
 * is where time-consuming activities which can not be done in the
388
 * interrupt driver proper are done; the interrupt driver schedules
389
 * them using rs_sched_event(), and they get done here.
390
 */
391
static void do_serial_bh(void)
392
{
393
        run_task_queue(&tq_serial);
394
}
395
 
396
static void do_softint(void *private_data)
397
{
398
        struct dz_serial *info = (struct dz_serial *) private_data;
399
        struct tty_struct *tty = info->tty;
400
 
401
        if (!tty)
402
                return;
403
 
404
        if (test_and_clear_bit(DZ_EVENT_WRITE_WAKEUP, &info->event)) {
405
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
406
                        (tty->ldisc.write_wakeup) (tty);
407
                wake_up_interruptible(&tty->write_wait);
408
        }
409
}
410
 
411
/*
412
 * -------------------------------------------------------------------
413
 * This routine is called from the scheduler tqueue when the interrupt
414
 * routine has signalled that a hangup has occurred.  The path of
415
 * hangup processing is:
416
 *
417
 *      serial interrupt routine -> (scheduler tqueue) ->
418
 *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
419
 * -------------------------------------------------------------------
420
 */
421
static void do_serial_hangup(void *private_data)
422
{
423
        struct dz_serial *info = (struct dz_serial *) private_data;
424
        struct tty_struct *tty = info->tty;;
425
 
426
        if (!tty)
427
                return;
428
 
429
        tty_hangup(tty);
430
}
431
 
432
/*
433
 * -------------------------------------------------------------------
434
 * startup ()
435
 *
436
 * various initialization tasks
437
 * -------------------------------------------------------------------
438
 */
439
static int startup(struct dz_serial *info)
440
{
441
        unsigned long page, flags;
442
        unsigned short tmp;
443
 
444
        if (info->is_initialized)
445
                return 0;
446
 
447
        save_flags(flags);
448
        cli();
449
 
450
        if (!info->port) {
451
                if (info->tty)
452
                        set_bit(TTY_IO_ERROR, &info->tty->flags);
453
                restore_flags(flags);
454
                return -ENODEV;
455
        }
456
        if (!info->xmit_buf) {
457
                page = get_free_page(GFP_KERNEL);
458
                if (!page) {
459
                        restore_flags(flags);
460
                        return -ENOMEM;
461
                }
462
                info->xmit_buf = (unsigned char *) page;
463
        }
464
        if (info->tty)
465
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
466
 
467
        /* enable the interrupt and the scanning */
468
        tmp = dz_in(info, DZ_CSR);
469
        tmp |= (DZ_RIE | DZ_TIE | DZ_MSE);
470
        dz_out(info, DZ_CSR, tmp);
471
 
472
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
473
 
474
        /* set up the speed */
475
        change_speed(info);
476
 
477
        /* clear the line transmitter buffer
478
           I can't figure out why I need to do this - but
479
           its necessary - in order for the console portion
480
           and the interrupt portion to live happily side by side.
481
         */
482
 
483
        /* clear the line transmitter buffer
484
           I can't figure out why I need to do this - but
485
           its necessary - in order for the console portion
486
           and the interrupt portion to live happily side by side.
487
         */
488
 
489
        info->is_initialized = 1;
490
 
491
        restore_flags(flags);
492
        return 0;
493
}
494
 
495
/*
496
 * -------------------------------------------------------------------
497
 * shutdown ()
498
 *
499
 * This routine will shutdown a serial port; interrupts are disabled, and
500
 * DTR is dropped if the hangup on close termio flag is on.
501
 * -------------------------------------------------------------------
502
 */
503
static void shutdown(struct dz_serial *info)
504
{
505
        unsigned long flags;
506
        unsigned short tmp;
507
 
508
        if (!info->is_initialized)
509
                return;
510
 
511
        save_flags(flags);
512
        cli();
513
 
514
        dz_stop(info->tty);
515
 
516
 
517
 
518
        info->cflags &= ~DZ_CREAD;      /* turn off receive enable flag */
519
        dz_out(info, DZ_LPR, info->cflags);
520
 
521
        if (info->xmit_buf) {   /* free Tx buffer */
522
                free_page((unsigned long) info->xmit_buf);
523
                info->xmit_buf = 0;
524
        }
525
        if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
526
                tmp = dz_in(info, DZ_TCR);
527
                if (tmp & DZ_MODEM_DTR) {
528
                        tmp &= ~DZ_MODEM_DTR;
529
                        dz_out(info, DZ_TCR, tmp);
530
                }
531
        }
532
        if (info->tty)
533
                set_bit(TTY_IO_ERROR, &info->tty->flags);
534
 
535
        info->is_initialized = 0;
536
        restore_flags(flags);
537
}
538
 
539
/*
540
 * -------------------------------------------------------------------
541
 * change_speed ()
542
 *
543
 * set the baud rate.
544
 * -------------------------------------------------------------------
545
 */
546
static void change_speed(struct dz_serial *info)
547
{
548
        unsigned long flags;
549
        unsigned cflag;
550
        int baud;
551
 
552
        if (!info->tty || !info->tty->termios)
553
                return;
554
 
555
        save_flags(flags);
556
        cli();
557
 
558
        info->cflags = info->line;
559
 
560
        cflag = info->tty->termios->c_cflag;
561
 
562
        switch (cflag & CSIZE) {
563
        case CS5:
564
                info->cflags |= DZ_CS5;
565
                break;
566
        case CS6:
567
                info->cflags |= DZ_CS6;
568
                break;
569
        case CS7:
570
                info->cflags |= DZ_CS7;
571
                break;
572
        case CS8:
573
        default:
574
                info->cflags |= DZ_CS8;
575
        }
576
 
577
        if (cflag & CSTOPB)
578
                info->cflags |= DZ_CSTOPB;
579
        if (cflag & PARENB)
580
                info->cflags |= DZ_PARENB;
581
        if (cflag & PARODD)
582
                info->cflags |= DZ_PARODD;
583
 
584
        baud = tty_get_baud_rate(info->tty);
585
        switch (baud) {
586
        case 50:
587
                info->cflags |= DZ_B50;
588
                break;
589
        case 75:
590
                info->cflags |= DZ_B75;
591
                break;
592
        case 110:
593
                info->cflags |= DZ_B110;
594
                break;
595
        case 134:
596
                info->cflags |= DZ_B134;
597
                break;
598
        case 150:
599
                info->cflags |= DZ_B150;
600
                break;
601
        case 300:
602
                info->cflags |= DZ_B300;
603
                break;
604
        case 600:
605
                info->cflags |= DZ_B600;
606
                break;
607
        case 1200:
608
                info->cflags |= DZ_B1200;
609
                break;
610
        case 1800:
611
                info->cflags |= DZ_B1800;
612
                break;
613
        case 2000:
614
                info->cflags |= DZ_B2000;
615
                break;
616
        case 2400:
617
                info->cflags |= DZ_B2400;
618
                break;
619
        case 3600:
620
                info->cflags |= DZ_B3600;
621
                break;
622
        case 4800:
623
                info->cflags |= DZ_B4800;
624
                break;
625
        case 7200:
626
                info->cflags |= DZ_B7200;
627
                break;
628
        case 9600:
629
        default:
630
                info->cflags |= DZ_B9600;
631
        }
632
 
633
        info->cflags |= DZ_RXENAB;
634
        dz_out(info, DZ_LPR, info->cflags);
635
 
636
        /* setup accept flag */
637
        info->read_status_mask = DZ_OERR;
638
        if (I_INPCK(info->tty))
639
                info->read_status_mask |= (DZ_FERR | DZ_PERR);
640
 
641
        /* characters to ignore */
642
        info->ignore_status_mask = 0;
643
        if (I_IGNPAR(info->tty))
644
                info->ignore_status_mask |= (DZ_FERR | DZ_PERR);
645
 
646
        restore_flags(flags);
647
}
648
 
649
/*
650
 * -------------------------------------------------------------------
651
 * dz_flush_char ()
652
 *
653
 * Flush the buffer.
654
 * -------------------------------------------------------------------
655
 */
656
static void dz_flush_chars(struct tty_struct *tty)
657
{
658
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
659
        unsigned long flags;
660
 
661
        if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped || !info->xmit_buf)
662
                return;
663
 
664
        save_flags(flags);
665
        cli();
666
 
667
        dz_start(info->tty);
668
 
669
        restore_flags(flags);
670
}
671
 
672
 
673
/*
674
 * -------------------------------------------------------------------
675
 * dz_write ()
676
 *
677
 * main output routine.
678
 * -------------------------------------------------------------------
679
 */
680
static int dz_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
681
{
682
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
683
        unsigned long flags;
684
        int c, ret = 0;
685
 
686
        if (!tty)
687
                return ret;
688
        if (!info->xmit_buf)
689
                return ret;
690
        if (!tmp_buf)
691
                tmp_buf = tmp_buffer;
692
 
693
 
694
 
695
        if (from_user) {
696
 
697
                down(&tmp_buf_sem);
698
                while (1) {
699
                        c = MIN(count, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1, DZ_XMIT_SIZE - info->xmit_head));
700
                        if (c <= 0)
701
                                break;
702
 
703
                        c -= copy_from_user(tmp_buf, buf, c);
704
                        if (!c) {
705
                                if (!ret)
706
                                        ret = -EFAULT;
707
                                break;
708
                        }
709
                        save_flags(flags);
710
                        cli();
711
 
712
                        c = MIN(c, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1, DZ_XMIT_SIZE - info->xmit_head));
713
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
714
                        info->xmit_head = ((info->xmit_head + c) & (DZ_XMIT_SIZE - 1));
715
                        info->xmit_cnt += c;
716
 
717
                        restore_flags(flags);
718
 
719
                        buf += c;
720
                        count -= c;
721
                        ret += c;
722
                }
723
 
724
                up(&tmp_buf_sem);
725
        } else {
726
 
727
 
728
                while (1) {
729
                        save_flags(flags);
730
                        cli();
731
 
732
                        c = MIN(count, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1, DZ_XMIT_SIZE - info->xmit_head));
733
                        if (c <= 0) {
734
                                restore_flags(flags);
735
                                break;
736
                        }
737
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
738
                        info->xmit_head = ((info->xmit_head + c) & (DZ_XMIT_SIZE - 1));
739
                        info->xmit_cnt += c;
740
 
741
                        restore_flags(flags);
742
 
743
                        buf += c;
744
                        count -= c;
745
                        ret += c;
746
                }
747
        }
748
 
749
 
750
        if (info->xmit_cnt) {
751
                if (!tty->stopped) {
752
                        if (!tty->hw_stopped) {
753
                                dz_start(info->tty);
754
                        }
755
                }
756
        }
757
        return ret;
758
}
759
 
760
/*
761
 * -------------------------------------------------------------------
762
 * dz_write_room ()
763
 *
764
 * compute the amount of space available for writing.
765
 * -------------------------------------------------------------------
766
 */
767
static int dz_write_room(struct tty_struct *tty)
768
{
769
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
770
        int ret;
771
 
772
        ret = DZ_XMIT_SIZE - info->xmit_cnt - 1;
773
        if (ret < 0)
774
                ret = 0;
775
        return ret;
776
}
777
 
778
/*
779
 * -------------------------------------------------------------------
780
 * dz_chars_in_buffer ()
781
 *
782
 * compute the amount of char left to be transmitted
783
 * -------------------------------------------------------------------
784
 */
785
static int dz_chars_in_buffer(struct tty_struct *tty)
786
{
787
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
788
 
789
        return info->xmit_cnt;
790
}
791
 
792
/*
793
 * -------------------------------------------------------------------
794
 * dz_flush_buffer ()
795
 *
796
 * Empty the output buffer
797
 * -------------------------------------------------------------------
798
 */
799
static void dz_flush_buffer(struct tty_struct *tty)
800
{
801
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
802
 
803
        cli();
804
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
805
        sti();
806
 
807
        wake_up_interruptible(&tty->write_wait);
808
 
809
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
810
                (tty->ldisc.write_wakeup) (tty);
811
}
812
 
813
/*
814
 * ------------------------------------------------------------
815
 * dz_throttle () and dz_unthrottle ()
816
 *
817
 * This routine is called by the upper-layer tty layer to signal that
818
 * incoming characters should be throttled (or not).
819
 * ------------------------------------------------------------
820
 */
821
static void dz_throttle(struct tty_struct *tty)
822
{
823
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
824
 
825
        if (I_IXOFF(tty))
826
                info->x_char = STOP_CHAR(tty);
827
}
828
 
829
static void dz_unthrottle(struct tty_struct *tty)
830
{
831
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
832
 
833
        if (I_IXOFF(tty)) {
834
                if (info->x_char)
835
                        info->x_char = 0;
836
                else
837
                        info->x_char = START_CHAR(tty);
838
        }
839
}
840
 
841
static void dz_send_xchar(struct tty_struct *tty, char ch)
842
{
843
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
844
 
845
        info->x_char = ch;
846
 
847
        if (ch)
848
                dz_start(info->tty);
849
}
850
 
851
/*
852
 * ------------------------------------------------------------
853
 * rs_ioctl () and friends
854
 * ------------------------------------------------------------
855
 */
856
static int get_serial_info(struct dz_serial *info,
857
                           struct serial_struct *retinfo)
858
{
859
        struct serial_struct tmp;
860
 
861
        if (!retinfo)
862
                return -EFAULT;
863
 
864
        memset(&tmp, 0, sizeof(tmp));
865
 
866
        tmp.type = info->type;
867
        tmp.line = info->line;
868
        tmp.port = info->port;
869
        tmp.irq = dec_interrupt[DEC_IRQ_DZ11];
870
        tmp.flags = info->flags;
871
        tmp.baud_base = info->baud_base;
872
        tmp.close_delay = info->close_delay;
873
        tmp.closing_wait = info->closing_wait;
874
 
875
        return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
876
}
877
 
878
static int set_serial_info(struct dz_serial *info,
879
                           struct serial_struct *new_info)
880
{
881
        struct serial_struct new_serial;
882
        struct dz_serial old_info;
883
        int retval = 0;
884
 
885
        if (!new_info)
886
                return -EFAULT;
887
 
888
        if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
889
                return -EFAULT;
890
 
891
        old_info = *info;
892
 
893
        if (!capable(CAP_SYS_ADMIN))
894
                return -EPERM;
895
 
896
        if (info->count > 1)
897
                return -EBUSY;
898
 
899
        /*
900
         * OK, past this point, all the error checking has been done.
901
         * At this point, we start making changes.....
902
         */
903
 
904
        info->baud_base = new_serial.baud_base;
905
        info->type = new_serial.type;
906
        info->close_delay = new_serial.close_delay;
907
        info->closing_wait = new_serial.closing_wait;
908
 
909
        retval = startup(info);
910
        return retval;
911
}
912
 
913
/*
914
 * get_lsr_info - get line status register info
915
 *
916
 * Purpose: Let user call ioctl() to get info when the UART physically
917
 *          is emptied.  On bus types like RS485, the transmitter must
918
 *          release the bus after transmitting. This must be done when
919
 *          the transmit shift register is empty, not be done when the
920
 *          transmit holding register is empty.  This functionality
921
 *          allows an RS485 driver to be written in user space.
922
 */
923
static int get_lsr_info(struct dz_serial *info, unsigned int *value)
924
{
925
        unsigned short status = dz_in(info, DZ_LPR);
926
 
927
        return put_user(status, value);
928
}
929
 
930
/*
931
 * This routine sends a break character out the serial port.
932
 */
933
static void send_break(struct dz_serial *info, int duration)
934
{
935
        unsigned long flags;
936
        unsigned short tmp, mask;
937
 
938
        if (!info->port)
939
                return;
940
 
941
        mask = 1 << info->line;
942
        tmp = dz_in(info, DZ_TCR);
943
        tmp |= mask;
944
 
945
        current->state = TASK_INTERRUPTIBLE;
946
 
947
        save_flags(flags);
948
        cli();
949
 
950
        dz_out(info, DZ_TCR, tmp);
951
 
952
        schedule_timeout(duration);
953
 
954
        tmp &= ~mask;
955
        dz_out(info, DZ_TCR, tmp);
956
 
957
        restore_flags(flags);
958
}
959
 
960
static int dz_ioctl(struct tty_struct *tty, struct file *file,
961
                    unsigned int cmd, unsigned long arg)
962
{
963
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
964
        int retval;
965
 
966
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
967
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
968
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
969
                if (tty->flags & (1 << TTY_IO_ERROR))
970
                        return -EIO;
971
        }
972
        switch (cmd) {
973
        case TCSBRK:            /* SVID version: non-zero arg --> no break */
974
                retval = tty_check_change(tty);
975
                if (retval)
976
                        return retval;
977
                tty_wait_until_sent(tty, 0);
978
                if (!arg)
979
                        send_break(info, HZ / 4);       /* 1/4 second */
980
                return 0;
981
 
982
        case TCSBRKP:           /* support for POSIX tcsendbreak() */
983
                retval = tty_check_change(tty);
984
                if (retval)
985
                        return retval;
986
                tty_wait_until_sent(tty, 0);
987
                send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
988
                return 0;
989
 
990
        case TIOCGSOFTCAR:
991
                return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
992
 
993
        case TIOCSSOFTCAR:
994
                if (get_user(arg, (unsigned long *) arg))
995
                        return -EFAULT;
996
                tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
997
                                         (arg ? CLOCAL : 0));
998
                return 0;
999
 
1000
        case TIOCGSERIAL:
1001
                return get_serial_info(info, (struct serial_struct *) arg);
1002
 
1003
        case TIOCSSERIAL:
1004
                return set_serial_info(info, (struct serial_struct *) arg);
1005
 
1006
        case TIOCSERGETLSR:     /* Get line status register */
1007
                return get_lsr_info(info, (unsigned int *) arg);
1008
 
1009
        case TIOCSERGSTRUCT:
1010
                return copy_to_user((struct dz_serial *) arg, info,
1011
                                 sizeof(struct dz_serial)) ? -EFAULT : 0;
1012
 
1013
        default:
1014
                return -ENOIOCTLCMD;
1015
        }
1016
 
1017
        return 0;
1018
}
1019
 
1020
static void dz_set_termios(struct tty_struct *tty,
1021
                           struct termios *old_termios)
1022
{
1023
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
1024
 
1025
        if (tty->termios->c_cflag == old_termios->c_cflag)
1026
                return;
1027
 
1028
        change_speed(info);
1029
 
1030
        if ((old_termios->c_cflag & CRTSCTS) &&
1031
            !(tty->termios->c_cflag & CRTSCTS)) {
1032
                tty->hw_stopped = 0;
1033
                dz_start(tty);
1034
        }
1035
}
1036
 
1037
/*
1038
 * ------------------------------------------------------------
1039
 * dz_close()
1040
 *
1041
 * This routine is called when the serial port gets closed.  First, we
1042
 * wait for the last remaining data to be sent.  Then, we turn off
1043
 * the transmit enable and receive enable flags.
1044
 * ------------------------------------------------------------
1045
 */
1046
static void dz_close(struct tty_struct *tty, struct file *filp)
1047
{
1048
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
1049
        unsigned long flags;
1050
 
1051
        if (!info)
1052
                return;
1053
 
1054
        save_flags(flags);
1055
        cli();
1056
 
1057
        if (tty_hung_up_p(filp)) {
1058
                restore_flags(flags);
1059
                return;
1060
        }
1061
        if ((tty->count == 1) && (info->count != 1)) {
1062
                /*
1063
                 * Uh, oh.  tty->count is 1, which means that the tty
1064
                 * structure will be freed.  Info->count should always
1065
                 * be one in these conditions.  If it's greater than
1066
                 * one, we've got real problems, since it means the
1067
                 * serial port won't be shutdown.
1068
                 */
1069
                printk("dz_close: bad serial port count; tty->count is 1, "
1070
                       "info->count is %d\n", info->count);
1071
                info->count = 1;
1072
        }
1073
        if (--info->count < 0) {
1074
                printk("ds_close: bad serial port count for ttyS%02d: %d\n",
1075
                       info->line, info->count);
1076
                info->count = 0;
1077
        }
1078
        if (info->count) {
1079
                restore_flags(flags);
1080
                return;
1081
        }
1082
        info->flags |= DZ_CLOSING;
1083
        /*
1084
         * Save the termios structure, since this port may have
1085
         * separate termios for callout and dialin.
1086
         */
1087
        if (info->flags & DZ_NORMAL_ACTIVE)
1088
                info->normal_termios = *tty->termios;
1089
        if (info->flags & DZ_CALLOUT_ACTIVE)
1090
                info->callout_termios = *tty->termios;
1091
        /*
1092
         * Now we wait for the transmit buffer to clear; and we notify
1093
         * the line discipline to only process XON/XOFF characters.
1094
         */
1095
        tty->closing = 1;
1096
 
1097
        if (info->closing_wait != DZ_CLOSING_WAIT_NONE)
1098
                tty_wait_until_sent(tty, info->closing_wait);
1099
 
1100
        /*
1101
         * At this point we stop accepting input.  To do this, we
1102
         * disable the receive line status interrupts.
1103
         */
1104
 
1105
        shutdown(info);
1106
 
1107
        if (tty->driver.flush_buffer)
1108
                tty->driver.flush_buffer(tty);
1109
        if (tty->ldisc.flush_buffer)
1110
                tty->ldisc.flush_buffer(tty);
1111
        tty->closing = 0;
1112
        info->event = 0;
1113
        info->tty = 0;
1114
 
1115
        if (tty->ldisc.num != ldiscs[N_TTY].num) {
1116
                if (tty->ldisc.close)
1117
                        (tty->ldisc.close) (tty);
1118
                tty->ldisc = ldiscs[N_TTY];
1119
                tty->termios->c_line = N_TTY;
1120
                if (tty->ldisc.open)
1121
                        (tty->ldisc.open) (tty);
1122
        }
1123
        if (info->blocked_open) {
1124
                if (info->close_delay) {
1125
                        current->state = TASK_INTERRUPTIBLE;
1126
                        schedule_timeout(info->close_delay);
1127
                }
1128
                wake_up_interruptible(&info->open_wait);
1129
        }
1130
        info->flags &= ~(DZ_NORMAL_ACTIVE | DZ_CALLOUT_ACTIVE | DZ_CLOSING);
1131
        wake_up_interruptible(&info->close_wait);
1132
 
1133
        restore_flags(flags);
1134
}
1135
 
1136
/*
1137
 * dz_hangup () --- called by tty_hangup() when a hangup is signaled.
1138
 */
1139
static void dz_hangup(struct tty_struct *tty)
1140
{
1141
        struct dz_serial *info = (struct dz_serial *) tty->driver_data;
1142
 
1143
        dz_flush_buffer(tty);
1144
        shutdown(info);
1145
        info->event = 0;
1146
        info->count = 0;
1147
        info->flags &= ~(DZ_NORMAL_ACTIVE | DZ_CALLOUT_ACTIVE);
1148
        info->tty = 0;
1149
        wake_up_interruptible(&info->open_wait);
1150
}
1151
 
1152
/*
1153
 * ------------------------------------------------------------
1154
 * rs_open() and friends
1155
 * ------------------------------------------------------------
1156
 */
1157
static int block_til_ready(struct tty_struct *tty, struct file *filp, struct dz_serial *info)
1158
{
1159
        DECLARE_WAITQUEUE(wait, current);
1160
        int retval;
1161
        int do_clocal = 0;
1162
 
1163
        /*
1164
         * If the device is in the middle of being closed, then block
1165
         * until it's done, and then try again.
1166
         */
1167
        if (info->flags & DZ_CLOSING) {
1168
                interruptible_sleep_on(&info->close_wait);
1169
                return -EAGAIN;
1170
        }
1171
        /*
1172
         * If this is a callout device, then just make sure the normal
1173
         * device isn't being used.
1174
         */
1175
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1176
                if (info->flags & DZ_NORMAL_ACTIVE)
1177
                        return -EBUSY;
1178
 
1179
                if ((info->flags & DZ_CALLOUT_ACTIVE) &&
1180
                    (info->flags & DZ_SESSION_LOCKOUT) &&
1181
                    (info->session != current->session))
1182
                        return -EBUSY;
1183
 
1184
                if ((info->flags & DZ_CALLOUT_ACTIVE) &&
1185
                    (info->flags & DZ_PGRP_LOCKOUT) &&
1186
                    (info->pgrp != current->pgrp))
1187
                        return -EBUSY;
1188
                info->flags |= DZ_CALLOUT_ACTIVE;
1189
                return 0;
1190
        }
1191
        /*
1192
         * If non-blocking mode is set, or the port is not enabled,
1193
         * then make the check up front and then exit.
1194
         */
1195
        if ((filp->f_flags & O_NONBLOCK) ||
1196
            (tty->flags & (1 << TTY_IO_ERROR))) {
1197
                if (info->flags & DZ_CALLOUT_ACTIVE)
1198
                        return -EBUSY;
1199
                info->flags |= DZ_NORMAL_ACTIVE;
1200
                return 0;
1201
        }
1202
        if (info->flags & DZ_CALLOUT_ACTIVE) {
1203
                if (info->normal_termios.c_cflag & CLOCAL)
1204
                        do_clocal = 1;
1205
        } else {
1206
                if (tty->termios->c_cflag & CLOCAL)
1207
                        do_clocal = 1;
1208
        }
1209
 
1210
        /*
1211
         * Block waiting for the carrier detect and the line to become
1212
         * free (i.e., not in use by the callout).  While we are in
1213
         * this loop, info->count is dropped by one, so that
1214
         * dz_close() knows when to free things.  We restore it upon
1215
         * exit, either normal or abnormal.
1216
         */
1217
        retval = 0;
1218
        add_wait_queue(&info->open_wait, &wait);
1219
 
1220
        info->count--;
1221
        info->blocked_open++;
1222
        while (1) {
1223
                set_current_state(TASK_INTERRUPTIBLE);
1224
                if (tty_hung_up_p(filp) || !(info->is_initialized)) {
1225
                        retval = -EAGAIN;
1226
                        break;
1227
                }
1228
                if (!(info->flags & DZ_CALLOUT_ACTIVE) &&
1229
                    !(info->flags & DZ_CLOSING) && do_clocal)
1230
                        break;
1231
                if (signal_pending(current)) {
1232
                        retval = -ERESTARTSYS;
1233
                        break;
1234
                }
1235
                schedule();
1236
        }
1237
 
1238
        current->state = TASK_RUNNING;
1239
        remove_wait_queue(&info->open_wait, &wait);
1240
        if (!tty_hung_up_p(filp))
1241
                info->count++;
1242
        info->blocked_open--;
1243
 
1244
        if (retval)
1245
                return retval;
1246
        info->flags |= DZ_NORMAL_ACTIVE;
1247
        return 0;
1248
}
1249
 
1250
/*
1251
 * This routine is called whenever a serial port is opened.  It
1252
 * enables interrupts for a serial port. It also performs the
1253
 * serial-specific initialization for the tty structure.
1254
 */
1255
static int dz_open(struct tty_struct *tty, struct file *filp)
1256
{
1257
        struct dz_serial *info;
1258
        int retval, line;
1259
 
1260
        line = MINOR(tty->device) - tty->driver.minor_start;
1261
 
1262
        /* The dz lines for the mouse/keyboard must be
1263
         * opened using their respective drivers.
1264
         */
1265
        if ((line < 0) || (line >= DZ_NB_PORT))
1266
                return -ENODEV;
1267
 
1268
        if ((line == DZ_KEYBOARD) || (line == DZ_MOUSE))
1269
                return -ENODEV;
1270
 
1271
        info = lines[line];
1272
        info->count++;
1273
 
1274
        tty->driver_data = info;
1275
        info->tty = tty;
1276
 
1277
        /*
1278
         * Start up serial port
1279
         */
1280
        retval = startup(info);
1281
        if (retval)
1282
                return retval;
1283
 
1284
        retval = block_til_ready(tty, filp, info);
1285
        if (retval)
1286
                return retval;
1287
 
1288
        if ((info->count == 1) && (info->flags & DZ_SPLIT_TERMIOS)) {
1289
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1290
                        *tty->termios = info->normal_termios;
1291
                else
1292
                        *tty->termios = info->callout_termios;
1293
                change_speed(info);
1294
 
1295
        }
1296
        info->session = current->session;
1297
        info->pgrp = current->pgrp;
1298
        return 0;
1299
}
1300
 
1301
static void show_serial_version(void)
1302
{
1303
        printk("%s%s\n", dz_name, dz_version);
1304
}
1305
 
1306
int __init dz_init(void)
1307
{
1308
        int i;
1309
        long flags;
1310
        struct dz_serial *info;
1311
 
1312
        /* Setup base handler, and timer table. */
1313
        init_bh(SERIAL_BH, do_serial_bh);
1314
 
1315
        show_serial_version();
1316
 
1317
        memset(&serial_driver, 0, sizeof(struct tty_driver));
1318
        serial_driver.magic = TTY_DRIVER_MAGIC;
1319
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1320
        serial_driver.name = "ttyS";
1321
#else
1322
        serial_driver.name = "tts/%d";
1323
#endif
1324
        serial_driver.major = TTY_MAJOR;
1325
        serial_driver.minor_start = 64;
1326
        serial_driver.num = DZ_NB_PORT;
1327
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1328
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
1329
        serial_driver.init_termios = tty_std_termios;
1330
 
1331
        serial_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1332
                                             CLOCAL;
1333
        serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1334
        serial_driver.refcount = &serial_refcount;
1335
        serial_driver.table = serial_table;
1336
        serial_driver.termios = serial_termios;
1337
        serial_driver.termios_locked = serial_termios_locked;
1338
 
1339
        serial_driver.open = dz_open;
1340
        serial_driver.close = dz_close;
1341
        serial_driver.write = dz_write;
1342
        serial_driver.flush_chars = dz_flush_chars;
1343
        serial_driver.write_room = dz_write_room;
1344
        serial_driver.chars_in_buffer = dz_chars_in_buffer;
1345
        serial_driver.flush_buffer = dz_flush_buffer;
1346
        serial_driver.ioctl = dz_ioctl;
1347
        serial_driver.throttle = dz_throttle;
1348
        serial_driver.unthrottle = dz_unthrottle;
1349
        serial_driver.send_xchar = dz_send_xchar;
1350
        serial_driver.set_termios = dz_set_termios;
1351
        serial_driver.stop = dz_stop;
1352
        serial_driver.start = dz_start;
1353
        serial_driver.hangup = dz_hangup;
1354
 
1355
        /*
1356
         * The callout device is just like normal device except for
1357
         * major number and the subtype code.
1358
         */
1359
        callout_driver = serial_driver;
1360
#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1361
        callout_driver.name = "cua";
1362
#else
1363
        callout_driver.name = "cua/%d";
1364
#endif
1365
        callout_driver.major = TTYAUX_MAJOR;
1366
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1367
 
1368
        if (tty_register_driver(&serial_driver))
1369
                panic("Couldn't register serial driver");
1370
        if (tty_register_driver(&callout_driver))
1371
                panic("Couldn't register callout driver");
1372
        save_flags(flags);
1373
        cli();
1374
 
1375
        for (i = 0; i < DZ_NB_PORT; i++) {
1376
                info = &multi[i];
1377
                lines[i] = info;
1378
                info->magic = SERIAL_MAGIC;
1379
 
1380
                if (mips_machtype == MACH_DS23100 ||
1381
                    mips_machtype == MACH_DS5100)
1382
                        info->port = (unsigned long) KN01_DZ11_BASE;
1383
                else
1384
                        info->port = (unsigned long) KN02_DZ11_BASE;
1385
 
1386
                info->line = i;
1387
                info->tty = 0;
1388
                info->close_delay = 50;
1389
                info->closing_wait = 3000;
1390
                info->x_char = 0;
1391
                info->event = 0;
1392
                info->count = 0;
1393
                info->blocked_open = 0;
1394
                info->tqueue.routine = do_softint;
1395
                info->tqueue.data = info;
1396
                info->tqueue_hangup.routine = do_serial_hangup;
1397
                info->tqueue_hangup.data = info;
1398
                info->callout_termios = callout_driver.init_termios;
1399
                info->normal_termios = serial_driver.init_termios;
1400
                init_waitqueue_head(&info->open_wait);
1401
                init_waitqueue_head(&info->close_wait);
1402
 
1403
                /*
1404
                 * If we are pointing to address zero then punt - not correctly
1405
                 * set up in setup.c to handle this.
1406
                 */
1407
                if (!info->port)
1408
                        return 0;
1409
 
1410
                printk("ttyS%02d at 0x%08x (irq = %d)\n", info->line,
1411
                       info->port, dec_interrupt[DEC_IRQ_DZ11]);
1412
 
1413
                tty_register_devfs(&serial_driver, 0,
1414
                                 serial_driver.minor_start + info->line);
1415
                tty_register_devfs(&callout_driver, 0,
1416
                                callout_driver.minor_start + info->line);
1417
        }
1418
 
1419
        /* reset the chip */
1420
#ifndef CONFIG_SERIAL_DEC_CONSOLE
1421
        dz_out(info, DZ_CSR, DZ_CLR);
1422
        while (dz_in(info, DZ_CSR) & DZ_CLR);
1423
        iob();
1424
 
1425
        /* enable scanning */
1426
        dz_out(info, DZ_CSR, DZ_MSE);
1427
#endif
1428
 
1429
        /* order matters here... the trick is that flags
1430
           is updated... in request_irq - to immediatedly obliterate
1431
           it is unwise. */
1432
        restore_flags(flags);
1433
 
1434
 
1435
        if (request_irq(dec_interrupt[DEC_IRQ_DZ11], dz_interrupt,
1436
                        SA_INTERRUPT, "DZ", lines[0]))
1437
                panic("Unable to register DZ interrupt");
1438
 
1439
        return 0;
1440
}
1441
 
1442
#ifdef CONFIG_SERIAL_DEC_CONSOLE
1443
static void dz_console_put_char(unsigned char ch)
1444
{
1445
        unsigned long flags;
1446
        int loops = 2500;
1447
        unsigned short tmp = ch;
1448
        /* this code sends stuff out to serial device - spinning its
1449
           wheels and waiting. */
1450
 
1451
        /* force the issue - point it at lines[3] */
1452
        dz_console = &multi[CONSOLE_LINE];
1453
 
1454
        save_flags(flags);
1455
        cli();
1456
 
1457
 
1458
        /* spin our wheels */
1459
        while (((dz_in(dz_console, DZ_CSR) & DZ_TRDY) != DZ_TRDY) && loops--);
1460
 
1461
        /* Actually transmit the character. */
1462
        dz_out(dz_console, DZ_TDR, tmp);
1463
 
1464
        restore_flags(flags);
1465
}
1466
/*
1467
 * -------------------------------------------------------------------
1468
 * dz_console_print ()
1469
 *
1470
 * dz_console_print is registered for printk.
1471
 * The console must be locked when we get here.
1472
 * -------------------------------------------------------------------
1473
 */
1474
static void dz_console_print(struct console *cons,
1475
                             const char *str,
1476
                             unsigned int count)
1477
{
1478
#ifdef DEBUG_DZ
1479
        prom_printf((char *) str);
1480
#endif
1481
        while (count--) {
1482
                if (*str == '\n')
1483
                        dz_console_put_char('\r');
1484
                dz_console_put_char(*str++);
1485
        }
1486
}
1487
 
1488
static kdev_t dz_console_device(struct console *c)
1489
{
1490
        return MKDEV(TTY_MAJOR, 64 + c->index);
1491
}
1492
 
1493
static int __init dz_console_setup(struct console *co, char *options)
1494
{
1495
        int baud = 9600;
1496
        int bits = 8;
1497
        int parity = 'n';
1498
        int cflag = CREAD | HUPCL | CLOCAL;
1499
        char *s;
1500
        unsigned short mask, tmp;
1501
 
1502
        if (options) {
1503
                baud = simple_strtoul(options, NULL, 10);
1504
                s = options;
1505
                while (*s >= '0' && *s <= '9')
1506
                        s++;
1507
                if (*s)
1508
                        parity = *s++;
1509
                if (*s)
1510
                        bits = *s - '0';
1511
        }
1512
        /*
1513
         *    Now construct a cflag setting.
1514
         */
1515
        switch (baud) {
1516
        case 1200:
1517
                cflag |= DZ_B1200;
1518
                break;
1519
        case 2400:
1520
                cflag |= DZ_B2400;
1521
                break;
1522
        case 4800:
1523
                cflag |= DZ_B4800;
1524
                break;
1525
        case 9600:
1526
        default:
1527
                cflag |= DZ_B9600;
1528
                break;
1529
        }
1530
        switch (bits) {
1531
        case 7:
1532
                cflag |= DZ_CS7;
1533
                break;
1534
        default:
1535
        case 8:
1536
                cflag |= DZ_CS8;
1537
                break;
1538
        }
1539
        switch (parity) {
1540
        case 'o':
1541
        case 'O':
1542
                cflag |= DZ_PARODD;
1543
                break;
1544
        case 'e':
1545
        case 'E':
1546
                cflag |= DZ_PARENB;
1547
                break;
1548
        }
1549
        co->cflag = cflag;
1550
 
1551
        /* TOFIX: force to console line */
1552
        dz_console = &multi[CONSOLE_LINE];
1553
        if ((mips_machtype == MACH_DS23100) || (mips_machtype == MACH_DS5100))
1554
                dz_console->port = KN01_DZ11_BASE;
1555
        else
1556
                dz_console->port = KN02_DZ11_BASE;
1557
        dz_console->line = CONSOLE_LINE;
1558
 
1559
        dz_out(dz_console, DZ_CSR, DZ_CLR);
1560
        while ((tmp = dz_in(dz_console, DZ_CSR)) & DZ_CLR);
1561
 
1562
        /* enable scanning */
1563
        dz_out(dz_console, DZ_CSR, DZ_MSE);
1564
 
1565
        /*  Set up flags... */
1566
        dz_console->cflags = 0;
1567
        dz_console->cflags |= DZ_B9600;
1568
        dz_console->cflags |= DZ_CS8;
1569
        dz_console->cflags |= DZ_PARENB;
1570
        dz_out(dz_console, DZ_LPR, dz_console->cflags);
1571
 
1572
        mask = 1 << dz_console->line;
1573
        tmp = dz_in(dz_console, DZ_TCR);        /* read the TX flag */
1574
        if (!(tmp & mask)) {
1575
                tmp |= mask;    /* set the TX flag */
1576
                dz_out(dz_console, DZ_TCR, tmp);
1577
        }
1578
        return 0;
1579
}
1580
 
1581
static struct console dz_sercons =
1582
{
1583
    .name       = "ttyS",
1584
    .write      = dz_console_print,
1585
    .device     = dz_console_device,
1586
    .setup      = dz_console_setup,
1587
    .flags      = CON_CONSDEV | CON_PRINTBUFFER,
1588
    .index      = CONSOLE_LINE,
1589
};
1590
 
1591
void __init dz_serial_console_init(void)
1592
{
1593
        register_console(&dz_sercons);
1594
}
1595
 
1596
#endif /* CONFIG_SERIAL_DEC_CONSOLE */
1597
 
1598
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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