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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [char/] [trioserial.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/* TRIO chip serial port driver
2
 *
3
 * Based on:
4
 *
5
 * drivers/char/68302serial.c
6
 */
7
 
8
#include <linux/errno.h>
9
#include <linux/signal.h>
10
#include <linux/sched.h>
11
#include <linux/timer.h>
12
#include <linux/interrupt.h>
13
#include <linux/tty.h>
14
#include <linux/tty_flip.h>
15
#include <linux/config.h>
16
#include <linux/major.h>
17
#include <linux/string.h>
18
#include <linux/fcntl.h>
19
#include <linux/mm.h>
20
#include <linux/kernel.h>
21
 
22
#include <asm/io.h>
23
#include <asm/irq.h>
24
#include <asm/arch-trio/irq.h>
25
#include <asm/system.h>
26
#include <asm/segment.h>
27
#include <asm/bitops.h>
28
#include <asm/delay.h>
29
#if 0
30
#include <asm/kdebug.h>
31
#endif
32
 
33
#include "trioserial.h"
34
 
35
#define USE_INTS        1
36
#define US_NB           2
37
#define UART_CLOCK      (ARM_CLK/16)
38
 
39
 
40
#define XMIT_SERIAL_SIZE        PAGE_SIZE
41
#define RX_SERIAL_SIZE  PAGE_SIZE
42
 
43
 
44
static struct uart_regs *uarts[US_NB] = {
45
        (struct uart_regs*)USARTA_BASE, (struct uart_regs*)USARTB_BASE
46
};
47
static struct trio_serial trio_info[US_NB];
48
struct tty_struct trio_ttys[US_NB];
49
 
50
/* Console hooks... */
51
/*static int m68k_cons_chanout = 0;
52
static int m68k_cons_chanin = 0;*/
53
 
54
struct trio_serial *trio_consinfo = 0;
55
 
56
#if 0
57
static unsigned char kgdb_regs[16] = {
58
        0, 0, 0,                     /* write 0, 1, 2 */
59
        (Rx8 | RxENABLE),            /* write 3 */
60
        (X16CLK | SB1 | PAR_EVEN),   /* write 4 */
61
        (Tx8 | TxENAB),              /* write 5 */
62
        0, 0, 0,                     /* write 6, 7, 8 */
63
        (NV),                        /* write 9 */
64
        (NRZ),                       /* write 10 */
65
        (TCBR | RCBR),               /* write 11 */
66
        0, 0,                        /* BRG time constant, write 12 + 13 */
67
        (BRSRC | BRENABL),           /* write 14 */
68
        (DCDIE)                      /* write 15 */
69
};
70
#endif
71
 
72
DECLARE_TASK_QUEUE(tq_serial);
73
 
74
struct tq_struct serialpoll;
75
 
76
struct tty_driver serial_driver, callout_driver;
77
static int serial_refcount;
78
 
79
/* serial subtype definitions */
80
#define SERIAL_TYPE_NORMAL      1
81
#define SERIAL_TYPE_CALLOUT     2
82
 
83
/* number of characters left in xmit buffer before we ask for more */
84
#define WAKEUP_CHARS 256
85
 
86
/* Debugging... DEBUG_INTR is bad to use when one of the zs
87
 * lines is your console ;(
88
 */
89
#undef SERIAL_DEBUG_INTR
90
#undef SERIAL_DEBUG_OPEN
91
#undef SERIAL_DEBUG_FLOW
92
 
93
#define RS_ISR_PASS_LIMIT 256
94
 
95
#define _INLINE_
96
 
97
static void serpoll(void *data);
98
 
99
static void change_speed(struct trio_serial *info);
100
 
101
static struct tty_struct *serial_table[US_NB];
102
static struct termios *serial_termios[US_NB];
103
static struct termios *serial_termios_locked[US_NB];
104
 
105
#ifndef MIN
106
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
107
#endif
108
 
109
static char prompt0;
110
static void xmit_char(struct trio_serial* info, char ch);
111
static void xmit_string(struct trio_serial *info, char *p, int len);
112
static void start_rx(struct trio_serial *info);
113
static void wait_EOT(struct uart_regs*);
114
static void uart_init(struct trio_serial *info);
115
static void uart_speed(struct trio_serial *info, unsigned cflag);
116
 
117
static void tx_enable(struct uart_regs *uart);
118
static void rx_enable(struct uart_regs *uart);
119
static void tx_disable(struct uart_regs *uart);
120
static void rx_disable(struct uart_regs *uart);
121
static void tx_stop(struct uart_regs *uart);
122
static void tx_start(struct uart_regs *uart, int ints);
123
static void rx_stop(struct uart_regs *uart);
124
static void rx_start(struct uart_regs *uart, int ints);
125
static void set_ints_mode(int yes, struct trio_serial *info);
126
static void rs_interrupt(struct trio_serial *info);
127
extern void show_net_buffers(void);
128
extern void hard_reset_now(void);
129
 
130
 
131
static void _INLINE_ tx_enable(struct uart_regs *uart){
132
        uart->ier       |= US_TXRDY;
133
}
134
static void _INLINE_ rx_enable(struct uart_regs *uart){
135
        uart->ier       |= US_RXRDY;
136
}
137
static void _INLINE_ tx_disable(struct uart_regs *uart){
138
        uart->idr       |= US_TXRDY;
139
}
140
static void _INLINE_ rx_disable(struct uart_regs *uart){
141
        uart->idr       |= US_RXRDY;
142
}
143
static void _INLINE_ tx_stop(struct uart_regs *uart){
144
        tx_disable(uart);
145
        uart->cr        |= US_RXEN;
146
}
147
static void _INLINE_ tx_start(struct uart_regs *uart, int ints){
148
        if(ints)
149
                tx_enable(uart);
150
        uart->cr        |= US_TXEN;
151
}
152
static void _INLINE_ rx_stop(struct uart_regs *uart){
153
        rx_disable(uart);
154
        uart->cr        |= US_RXDIS;
155
}
156
static void _INLINE_ rx_start(struct uart_regs *uart, int ints){
157
        if(ints)
158
                rx_enable(uart);
159
        uart->cr        |= US_RXEN;
160
}
161
 
162
static void set_ints_mode(int yes, struct trio_serial *info){
163
        info->use_ints = yes;
164
        (yes)?unmask_irq(info->irq):mask_irq(info->irq);
165
}
166
 
167
/*
168
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
169
 * lock it in case the memcpy_fromfs blocks while swapping in a page,
170
 * and some other program tries to do a serial write at the same time.
171
 * Since the lock will only come under contention when the system is
172
 * swapping and available memory is low, it makes sense to share one
173
 * buffer across all the serial ports, since it significantly saves
174
 * memory if large numbers of serial ports are open.
175
 */
176
static unsigned char tmp_buf[XMIT_SERIAL_SIZE]; /* This is cheating */
177
static struct semaphore tmp_buf_sem = MUTEX;
178
 
179
static inline int serial_paranoia_check(struct trio_serial *info,
180
                                        dev_t device, const char *routine)
181
{
182
#ifdef SERIAL_PARANOIA_CHECK
183
        static const char *badmagic =
184
                "Warning: bad magic number for serial struct (%d, %d) in %s\n";
185
        static const char *badinfo =
186
                "Warning: null trio_serial for (%d, %d) in %s\n";
187
 
188
        if (!info) {
189
                printk(badinfo, MAJOR(device), MINOR(device), routine);
190
                return 1;
191
        }
192
        if (info->magic != SERIAL_MAGIC) {
193
                printk(badmagic, MAJOR(device), MINOR(device), routine);
194
                return 1;
195
        }
196
#endif
197
        return 0;
198
}
199
 
200
/* Sets or clears DTR/RTS on the requested line */
201
static inline void trio_rtsdtr(struct trio_serial *ss, int set)
202
{
203
        struct uart_regs *uart;
204
        uart = ss->uart;
205
        if(set) {
206
                uart->mc |= US_DTR | US_RTS;
207
        } else {
208
                uart->mc &= ~(u_32)(US_DTR | US_RTS);
209
        }
210
        return;
211
}
212
 
213
static inline void kgdb_chaninit(struct trio_serial *ss, int intson, int bps)
214
{
215
#if 0
216
        int brg;
217
 
218
        if(intson) {
219
                kgdb_regs[R1] = INT_ALL_Rx;
220
                kgdb_regs[R9] |= MIE;
221
        } else {
222
                kgdb_regs[R1] = 0;
223
                kgdb_regs[R9] &= ~MIE;
224
        }
225
        brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
226
        kgdb_regs[R12] = (brg & 255);
227
        kgdb_regs[R13] = ((brg >> 8) & 255);
228
        load_zsregs(ss->trio_channel, kgdb_regs);
229
#endif
230
}
231
 
232
 
233
/*
234
 * ------------------------------------------------------------
235
 * rs_stop() and rs_start()
236
 *
237
 * This routines are called before setting or resetting tty->stopped.
238
 * They enable or disable transmitter interrupts, as necessary.
239
 * ------------------------------------------------------------
240
 */
241
static void rs_stop(struct tty_struct *tty)
242
{
243
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
244
        unsigned long flags;
245
 
246
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
247
                return;
248
 
249
        save_flags(flags); cli();
250
        tx_stop(info->uart);
251
        rx_stop(info->uart);
252
        restore_flags(flags);
253
}
254
 
255
static void rs_put_char(struct trio_serial *info, char ch)
256
{
257
        int flags = 0;
258
        save_flags(flags); cli();
259
                wait_EOT(info->uart);
260
                xmit_char(info,ch);
261
                wait_EOT(info->uart);
262
        restore_flags(flags);
263
}
264
 
265
static void rs_start(struct tty_struct *tty)
266
{
267
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
268
        unsigned long flags;
269
 
270
        if (serial_paranoia_check(info, tty->device, "rs_start"))
271
                return;
272
 
273
        save_flags(flags); cli();
274
        tx_start(info->uart, info->use_ints);
275
        start_rx(info);
276
        restore_flags(flags);
277
}
278
 
279
/* Drop into either the boot monitor or kadb upon receiving a break
280
 * from keyboard/console input.
281
 */
282
static void batten_down_hatches(void)
283
{
284
        /* If we are doing kadb, we call the debugger
285
         * else we just drop into the boot monitor.
286
         * Note that we must flush the user windows
287
         * first before giving up control.
288
         */
289
#if 0
290
        if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
291
           (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
292
                sp_enter_debugger();
293
        else
294
                panic("trio_serial: batten_down_hatches");
295
        return;
296
#endif
297
}
298
 
299
/*
300
 * ----------------------------------------------------------------------
301
 *
302
 * Here starts the interrupt handling routines.  All of the following
303
 * subroutines are declared as inline and are folded into
304
 * rs_interrupt().  They were separated out for readability's sake.
305
 *
306
 * Note: rs_interrupt() is a "fast" interrupt, which means that it
307
 * runs with interrupts turned off.  People who may want to modify
308
 * rs_interrupt() should try to keep the interrupt handler as fast as
309
 * possible.  After you are done making modifications, it is not a bad
310
 * idea to do:
311
 *
312
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
313
 *
314
 * and look at the resulting assemble code in serial.s.
315
 *
316
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
317
 * -----------------------------------------------------------------------
318
 */
319
 
320
/*
321
 * This routine is used by the interrupt handler to schedule
322
 * processing in the software interrupt portion of the driver.
323
 */
324
static _INLINE_ void rs_sched_event(struct trio_serial *info,
325
                                    int event)
326
{
327
        info->event |= 1 << event;
328
        queue_task_irq_off(&info->tqueue, &tq_serial);
329
        mark_bh(SERIAL_BH);
330
}
331
 
332
extern void breakpoint(void);  /* For the KGDB frame character */
333
 
334
static _INLINE_ void receive_chars(struct trio_serial *info, u_32 status)
335
{
336
        unsigned char ch;
337
        int count;
338
        struct uart_regs *uart = info->uart;
339
#if 0
340
        // hack to receive chars by polling from anywhere
341
        struct trio_serial * info1 = &trio_info;
342
        struct tty_struct *tty = info1->tty;
343
        if (!(info->flags & S_INITIALIZED))
344
                return;
345
#else
346
        struct tty_struct *tty = info->tty;
347
        if (!(info->flags & S_INITIALIZED))
348
                return;
349
#endif  
350
        count = uart->rcr;
351
        // hack to receive chars by polling only BD fields
352
        if (!(status & US_RXRDY)  || !count){
353
                return;
354
        }
355
        ch = info->rx_buf[0];
356
        if(info->is_cons) {
357
                if(status & US_RXBRK) { /* whee, break received */
358
                        batten_down_hatches();
359
                        /*rs_recv_clear(info->trio_channel);*/
360
                        return;
361
                } else if (ch == 0x10) { /* ^P */
362
                        show_state();
363
                        show_free_areas();
364
                        show_buffers();
365
                        show_net_buffers();
366
                        return;
367
                } else if (ch == 0x12) { /* ^R */
368
                        hard_reset_now();
369
                        return;
370
                }
371
                /* It is a 'keyboard interrupt' ;-) */
372
                wake_up(&keypress_wait);
373
        }
374
        /* Look for kgdb 'stop' character, consult the gdb documentation
375
         * for remote target debugging and arch/sparc/kernel/sparc-stub.c
376
         * to see how all this works.
377
         */
378
        /*if((info->kgdb_channel) && (ch =='\003')) {
379
                breakpoint();
380
                goto clear_and_exit;
381
        }*/
382
 
383
        if(!tty)
384
                goto clear_and_exit;
385
 
386
        if (tty->flip.count >= TTY_FLIPBUF_SIZE)
387
                queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
388
        tty->flip.count++;
389
        if(status & US_PARE)
390
                *tty->flip.flag_buf_ptr++ = TTY_PARITY;
391
        else if(status & US_OVRE)
392
                *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
393
        else if(status & US_FRAME)
394
                *tty->flip.flag_buf_ptr++ = TTY_FRAME;
395
        else
396
                *tty->flip.flag_buf_ptr++ = 0; /* XXX */
397
        *tty->flip.char_buf_ptr++ = ch;
398
 
399
        queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
400
 
401
clear_and_exit:
402
        start_rx(info);
403
        return;
404
}
405
 
406
static _INLINE_ void transmit_chars(struct trio_serial *info)
407
{
408
        if (info->x_char) {
409
                /* Send next char */
410
                xmit_char(info, info->x_char);
411
                info->x_char = 0;
412
                goto clear_and_return;
413
        }
414
 
415
        if((info->xmit_cnt <= 0) || info->tty->stopped) {
416
                /* That's peculiar... */
417
//              tx_stop(0);
418
                goto clear_and_return;
419
        }
420
 
421
        /* Send char */
422
        xmit_char(info, info->xmit_buf[info->xmit_tail++]);
423
        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
424
        info->xmit_cnt--;
425
 
426
        if (info->xmit_cnt < WAKEUP_CHARS)
427
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
428
 
429
        if(info->xmit_cnt <= 0) {
430
//              tx_stop(0);
431
                goto clear_and_return;
432
        }
433
 
434
clear_and_return:
435
        /* Clear interrupt (should be auto)*/
436
        return;
437
}
438
 
439
static _INLINE_ void status_handle(struct trio_serial *info, u_32 status)
440
{
441
#if 0
442
        if(status & DCD) {
443
                if((info->tty->termios->c_cflag & CRTSCTS) &&
444
                   ((info->curregs[3] & AUTO_ENAB)==0)) {
445
                        info->curregs[3] |= AUTO_ENAB;
446
                        info->pendregs[3] |= AUTO_ENAB;
447
                        write_zsreg(info->trio_channel, 3, info->curregs[3]);
448
                }
449
        } else {
450
                if((info->curregs[3] & AUTO_ENAB)) {
451
                        info->curregs[3] &= ~AUTO_ENAB;
452
                        info->pendregs[3] &= ~AUTO_ENAB;
453
                        write_zsreg(info->trio_channel, 3, info->curregs[3]);
454
                }
455
        }
456
#endif
457
        /* Whee, if this is console input and this is a
458
         * 'break asserted' status change interrupt, call
459
         * the boot prom.
460
         */
461
        if((status & US_RXBRK) && info->break_abort)
462
                batten_down_hatches();
463
 
464
        /* XXX Whee, put in a buffer somewhere, the status information
465
         * XXX whee whee whee... Where does the information go...
466
         */
467
        return;
468
}
469
 
470
 
471
/*
472
 * This is the serial driver's generic interrupt routine
473
 */
474
void rs_interrupta(int irq, void *dev_id, struct pt_regs * regs){
475
        rs_interrupt(&trio_info[0]);
476
}
477
void rs_interruptb(int irq, void *dev_id, struct pt_regs * regs){
478
        rs_interrupt(&trio_info[1]);
479
}
480
static void rs_interrupt(struct trio_serial *info)
481
{
482
        u_32 status;
483
        status = info->uart->csr;
484
 
485
        if (status & US_TXRDY) {
486
                transmit_chars(info);
487
        }
488
        if (status & US_RXRDY){
489
                receive_chars(info, status);
490
        }
491
        status_handle(info, status);
492
 
493
        if(!info->use_ints){
494
                serialpoll.data = (void *)info;
495
                queue_task_irq_off(&serialpoll, &tq_timer);
496
        }
497
        return;
498
}
499
static void serpoll(void *data){
500
        struct trio_serial * info = data;
501
        rs_interrupt(info);
502
}
503
 
504
/*
505
 * -------------------------------------------------------------------
506
 * Here ends the serial interrupt routines.
507
 * -------------------------------------------------------------------
508
 */
509
 
510
/*
511
 * This routine is used to handle the "bottom half" processing for the
512
 * serial driver, known also the "software interrupt" processing.
513
 * This processing is done at the kernel interrupt level, after the
514
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
515
 * is where time-consuming activities which can not be done in the
516
 * interrupt driver proper are done; the interrupt driver schedules
517
 * them using rs_sched_event(), and they get done here.
518
 */
519
static void do_serial_bh(void)
520
{
521
        run_task_queue(&tq_serial);
522
}
523
 
524
static void do_softint(void *private_)
525
{
526
        struct trio_serial      *info = (struct trio_serial *) private_;
527
        struct tty_struct       *tty;
528
 
529
        tty = info->tty;
530
        if (!tty)
531
                return;
532
 
533
        if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
534
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
535
                    tty->ldisc.write_wakeup)
536
                        (tty->ldisc.write_wakeup)(tty);
537
                wake_up_interruptible(&tty->write_wait);
538
        }
539
}
540
 
541
/*
542
 * This routine is called from the scheduler tqueue when the interrupt
543
 * routine has signalled that a hangup has occurred.  The path of
544
 * hangup processing is:
545
 *
546
 *      serial interrupt routine -> (scheduler tqueue) ->
547
 *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
548
 *
549
 */
550
static void do_serial_hangup(void *private_)
551
{
552
        struct trio_serial      *info = (struct trio_serial *) private_;
553
        struct tty_struct       *tty;
554
 
555
        tty = info->tty;
556
        if (!tty)
557
                return;
558
 
559
        tty_hangup(tty);
560
}
561
 
562
 
563
/*
564
 * This subroutine is called when the RS_TIMER goes off.  It is used
565
 * by the serial driver to handle ports that do not have an interrupt
566
 * (irq=0).  This doesn't work at all for 16450's, as a sun has a Z8530.
567
 */
568
 
569
static void rs_timer(void)
570
{
571
        panic("rs_timer called\n");
572
        return;
573
}
574
static u_32 calcCD(u_32 br){
575
        return(UART_CLOCK/br);
576
}
577
static void uart_init(struct trio_serial *info){
578
        struct uart_regs* uart;
579
        if(info){
580
                uart = info->uart;
581
        }else{
582
                uart = uarts[0];
583
        }
584
        uart->cr        = US_RSTRX|US_RSTTX|US_RSTSTA|US_TXDIS|US_RXDIS;
585
        uart->mr        = US_USCLKS(0)|US_CLK0|US_CHMODE(0)|US_NBSTOP(0)|US_PAR(4)|US_CHRL(3);
586
        uart->ier       = 0;
587
        uart->idr       = US_ALL_INTS;
588
        uart->brgr      = calcCD(9600);
589
        uart->rtor      = 100;  // timeout = value * 4 * bit period
590
        uart->ttgr      = 0;     // no guard time
591
        uart->rpr       = 0;
592
        uart->rcr       = 0;
593
        uart->tpr       = 0;
594
        uart->tcr       = 0;
595
        uart->mc        = 0;
596
}
597
 
598
static void uart_speed(struct trio_serial *info, unsigned cflag){
599
        unsigned baud = info->baud;
600
        struct uart_regs *uart = info->uart;
601
 
602
        uart->cr        = US_TXDIS|US_RXDIS;
603
        uart->ier       = 0;
604
        uart->idr       = US_ALL_INTS;
605
        uart->brgr      = calcCD(baud);
606
        uart->rtor      = 100;  // timeout = value * 4 *bit period      
607
        uart->ttgr      = 0;     // no guard time        
608
        uart->rpr       = 0;
609
        uart->rcr       = 0;
610
        uart->tpr       = 0;
611
        uart->tcr       =0;
612
        uart->mc        = 0;
613
        if (cflag != 0xffff){
614
                uart->mr        = US_USCLKS(0)|US_CLK0|US_CHMODE(0)|US_PAR(0);
615
 
616
                if ((cflag & CSIZE) == CS8)
617
                        uart->mr |= US_CHRL(3);         // 8 bit char
618
                else
619
                        uart->mr |= US_CHRL(2);         // 7 bit char
620
 
621
                if (cflag & CSTOPB)
622
                        uart->mr |= US_NBSTOP(2);       // 2 stop bits
623
 
624
                if (!(cflag & PARENB))
625
                        uart->mr |= US_PAR(4);          // parity disabled
626
                else
627
                        if (cflag & PARODD)
628
                                uart->mr |= US_PAR(1);  // odd parity
629
        }
630
        tx_start(uart, info->use_ints);
631
        start_rx(info);
632
}
633
static void wait_EOT(struct uart_regs *uart){
634
        volatile u_32 status;
635
        volatile struct uart_regs* puart;
636
        puart = (volatile struct uart_regs*)uart;
637
        while(1){
638
                status = puart->csr;
639
                if(status & US_TXRDY)
640
                        break;
641
        }
642
}
643
static int startup(struct trio_serial * info)
644
{
645
        unsigned long flags;
646
 
647
        if (info->flags & S_INITIALIZED)
648
                return 0;
649
 
650
        if (!info->xmit_buf) {
651
                info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
652
                if (!info->xmit_buf)
653
                        return -ENOMEM;
654
        }
655
        if (!info->rx_buf) {
656
                info->rx_buf = (unsigned char *) get_free_page(GFP_KERNEL);
657
                if (!info->rx_buf)
658
                        return -ENOMEM;
659
        }
660
        save_flags(flags); cli();
661
#ifdef SERIAL_DEBUG_OPEN
662
        printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
663
#endif
664
        /*
665
         * Clear the FIFO buffers and disable them
666
         * (they will be reenabled in change_speed())
667
         */
668
 
669
        if (info->tty)
670
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
671
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
672
 
673
        /*
674
         * and set the speed of the serial port
675
         */
676
 
677
        uart_init(info);
678
        set_ints_mode(1, info);
679
        change_speed(info);
680
 
681
        info->flags |= S_INITIALIZED;
682
        restore_flags(flags);
683
        return 0;
684
}
685
 
686
/*
687
 * This routine will shutdown a serial port; interrupts are disabled, and
688
 * DTR is dropped if the hangup on close termio flag is on.
689
 */
690
static void shutdown(struct trio_serial * info)
691
{
692
        unsigned long   flags;
693
 
694
        tx_disable(info->uart);
695
        rx_disable(info->uart);
696
        rx_stop(info->uart); /* All off! */
697
        if (!(info->flags & S_INITIALIZED))
698
                return;
699
 
700
#ifdef SERIAL_DEBUG_OPEN
701
        printk("Shutting down serial port %d (irq %d)....\n", info->line,
702
               info->irq);
703
#endif
704
 
705
        save_flags(flags); cli(); /* Disable interrupts */
706
 
707
        if (info->xmit_buf) {
708
                free_page((unsigned long) info->xmit_buf);
709
                info->xmit_buf = 0;
710
        }
711
 
712
        if (info->tty)
713
                set_bit(TTY_IO_ERROR, &info->tty->flags);
714
 
715
        info->flags &= ~S_INITIALIZED;
716
        restore_flags(flags);
717
}
718
 
719
/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
720
 
721
static int baud_table[] = {
722
        0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
723
        9600, 19200, 38400, 57600, 115200, 0 };
724
 
725
/*
726
 * This routine is called to set the UART divisor registers to match
727
 * the specified baud rate for a serial port.
728
 */
729
static void change_speed(struct trio_serial *info)
730
{
731
        unsigned cflag;
732
        int     i;
733
 
734
        if (!info->tty || !info->tty->termios)
735
                return;
736
        cflag = info->tty->termios->c_cflag;
737
 
738
        /* First disable the interrupts */
739
        tx_stop(info->uart);
740
        rx_stop(info->uart);
741
        /* set the baudrate */
742
        i = cflag & CBAUD;
743
 
744
        info->baud = baud_table[i];
745
        uart_speed(info, cflag);
746
        start_rx(info);
747
        tx_start(info->uart, info->use_ints);
748
        return;
749
}
750
static void start_rx(struct trio_serial *info){
751
        struct uart_regs *uart = info->uart;
752
        uart->rcr = (u_32)RX_SERIAL_SIZE;
753
        uart->rpr = (u_32)info->rx_buf;
754
        rx_start(uart, info->use_ints);
755
}
756
static void xmit_char(struct trio_serial *info, char ch){
757
        prompt0 = ch;
758
        xmit_string(info, &prompt0, 1);
759
}
760
static void xmit_string(struct trio_serial *info, char *p, int len){
761
        info->uart->tcr = (u_32)len;
762
        info->uart->tpr = (u_32)p;
763
        tx_start(info->uart, info->use_ints);
764
}
765
 
766
#if 0
767
/* These are for receiving and sending characters under the kgdb
768
 * source level kernel debugger.
769
 */
770
void putDebugChar(char kgdb_char)
771
{
772
        struct sun_zschannel *chan = trio_kgdbchan;
773
 
774
        while((chan->control & Tx_BUF_EMP)==0)
775
                udelay(5);
776
 
777
        chan->data = kgdb_char;
778
}
779
 
780
char getDebugChar(void)
781
{
782
        struct sun_zschannel *chan = trio_kgdbchan;
783
 
784
        while((chan->control & Rx_CH_AV)==0)
785
                barrier();
786
        return chan->data;
787
}
788
#endif
789
 
790
/*
791
 * Fair output driver allows a process to speak.
792
 */
793
static void rs_fair_output(     struct trio_serial *info)
794
{
795
        int left;               /* Output no more than that */
796
        unsigned long flags;
797
        char c;
798
 
799
        if (info == 0) return;
800
        if (info->xmit_buf == 0) return;
801
 
802
        save_flags(flags);  cli();
803
        left = info->xmit_cnt;
804
        while (left != 0) {
805
                c = info->xmit_buf[info->xmit_tail];
806
                info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
807
                info->xmit_cnt--;
808
                restore_flags(flags);
809
 
810
                rs_put_char(info, c);
811
 
812
                save_flags(flags);  cli();
813
                left = MIN(info->xmit_cnt, left-1);
814
        }
815
 
816
        /* Last character is being transmitted now (hopefully). */
817
//      udelay(20);
818
        wait_EOT(info->uart);
819
        restore_flags(flags);
820
        return;
821
}
822
 
823
/*
824
 * trio_console_print is registered for printk.
825
 */
826
static int console_initialized = 0;
827
static void init_console(void){
828
        struct trio_serial *info;
829
        info = &trio_info[0];
830
        memset(info, 0, sizeof(struct trio_serial));
831
#if 0   
832
        info->uart = uarts[0];
833
#else   
834
        info->uart = (struct uart_regs*)USARTA_BASE;
835
#endif
836
        info->tty = 0;
837
        info->irqmask = AIC_UA;
838
        info->irq = IRQ_USARTA;
839
        info->port = 1;
840
        info->use_ints = 0;
841
        info->is_cons = 1;
842
        console_initialized = 1;
843
}
844
void console_print_trio(const char *p)
845
{
846
        char c;
847
        struct trio_serial *info;
848
        info = &trio_info[0];
849
 
850
//      if (!(info->flags & S_INITIALIZED)){
851
        if(!console_initialized){
852
                init_console();
853
                uart_init(info);
854
                info->baud = 9600;
855
                uart_speed(info,0xffff);
856
        }
857
        while((c=*(p++)) != 0) {
858
                if(c == '\n')
859
                        rs_put_char(info, '\r');
860
                rs_put_char(info, c);
861
        }
862
 
863
        /* Comment this if you want to have a strict interrupt-driven output */
864
//      if (!info->use_ints)
865
//              rs_fair_output(info);
866
 
867
        return;
868
}
869
 
870
static void rs_set_ldisc(struct tty_struct *tty)
871
{
872
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
873
 
874
        if (serial_paranoia_check(info, tty->device, "rs_set_ldisc"))
875
                return;
876
 
877
        info->is_cons = (tty->termios->c_line == N_TTY);
878
 
879
        printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
880
}
881
 
882
static void rs_flush_chars(struct tty_struct *tty)
883
{
884
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
885
        unsigned long flags;
886
 
887
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
888
                return;
889
        if(!info->use_ints){
890
                for(;;) {
891
                        if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
892
                                !info->xmit_buf)
893
                                return;
894
 
895
                        /* Enable transmitter */
896
                        save_flags(flags); cli();
897
                        tx_start(info->uart,info->use_ints);
898
                }
899
        }else{
900
                if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
901
                        !info->xmit_buf)
902
                        return;
903
 
904
                        /* Enable transmitter */
905
                save_flags(flags); cli();
906
                tx_start(info->uart, info->use_ints);
907
        }
908
 
909
        if(!info->use_ints)
910
                wait_EOT(info->uart);
911
                /* Send char */
912
        xmit_char(info, info->xmit_buf[info->xmit_tail++]);
913
        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
914
        info->xmit_cnt--;
915
 
916
        restore_flags(flags);
917
}
918
 
919
extern void console_printn(const char * b, int count);
920
 
921
static int rs_write(struct tty_struct * tty, int from_user,
922
                    const unsigned char *buf, int count)
923
{
924
        int     c, total = 0;
925
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
926
        unsigned long flags;
927
 
928
        if (serial_paranoia_check(info, tty->device, "rs_write"))
929
                return 0;
930
 
931
        if (!tty || !info->xmit_buf)
932
                return 0;
933
 
934
        /*buf = "123456";
935
        count = 6;
936
 
937
        printk("Writing '%s' to serial port\n", buf);*/
938
 
939
        /*printk("rs_write of %d bytes\n", count);*/
940
 
941
 
942
        save_flags(flags);
943
        while (1) {
944
                cli();
945
                c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
946
                                   SERIAL_XMIT_SIZE - info->xmit_head));
947
                if (c <= 0)
948
                        break;
949
 
950
                if (from_user) {
951
                        down(&tmp_buf_sem);
952
                        memcpy_fromfs(tmp_buf, buf, c);
953
 
954
#if 0           // HN already done                      
955
                        c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
956
                                       SERIAL_XMIT_SIZE - info->xmit_head));
957
#endif                  
958
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
959
                        up(&tmp_buf_sem);
960
                } else
961
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
962
                info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
963
                info->xmit_cnt += c;
964
                restore_flags(flags);
965
                buf += c;
966
                count -= c;
967
                total += c;
968
        }
969
 
970
        if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped ){
971
                /* Enable transmitter */
972
 
973
                cli();
974
                /*printk("Enabling transmitter\n");*/
975
 
976
                if(!info->use_ints){
977
                        while(info->xmit_cnt) {
978
                                wait_EOT(info->uart);
979
                                /* Send char */
980
                                xmit_char(info, info->xmit_buf[info->xmit_tail++]);
981
                                wait_EOT(info->uart);
982
                                info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
983
                                info->xmit_cnt--;
984
                        }
985
                }else{
986
                        if (info->xmit_cnt){
987
                                /* Send char */
988
                                wait_EOT(info->uart);
989
                                xmit_string(info, &info->xmit_buf[info->xmit_tail], info->xmit_cnt);
990
                                info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
991
                                info->xmit_cnt=0;
992
                        }
993
                }
994
                restore_flags(flags);
995
        } else {
996
                /*printk("Skipping transmit\n");*/
997
        }
998
 
999
 
1000
#if 0
1001
                printk("Enabling stuff anyhow\n");
1002
                tx_start(0);
1003
 
1004
                if (SCC_EOT(0,0)) {
1005
                        printk("TX FIFO empty.\n");
1006
                        /* Send char */
1007
                        trio_xmit_char(info->uart, info->xmit_buf[info->xmit_tail++]);
1008
                        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1009
                        info->xmit_cnt--;
1010
                }
1011
#endif
1012
 
1013
        restore_flags(flags);
1014
        return total;
1015
}
1016
 
1017
static int rs_write_room(struct tty_struct *tty)
1018
{
1019
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1020
        int     ret;
1021
 
1022
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1023
                return 0;
1024
        ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1025
        if (ret < 0)
1026
                ret = 0;
1027
        return ret;
1028
}
1029
 
1030
static int rs_chars_in_buffer(struct tty_struct *tty)
1031
{
1032
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1033
 
1034
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1035
                return 0;
1036
        return info->xmit_cnt;
1037
}
1038
 
1039
static void rs_flush_buffer(struct tty_struct *tty)
1040
{
1041
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1042
 
1043
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1044
                return;
1045
        cli();
1046
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1047
        sti();
1048
        wake_up_interruptible(&tty->write_wait);
1049
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1050
            tty->ldisc.write_wakeup)
1051
                (tty->ldisc.write_wakeup)(tty);
1052
}
1053
 
1054
/*
1055
 * ------------------------------------------------------------
1056
 * rs_throttle()
1057
 *
1058
 * This routine is called by the upper-layer tty layer to signal that
1059
 * incoming characters should be throttled.
1060
 * ------------------------------------------------------------
1061
 */
1062
static void rs_throttle(struct tty_struct * tty)
1063
{
1064
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1065
#ifdef SERIAL_DEBUG_THROTTLE
1066
        char    buf[64];
1067
 
1068
        printk("throttle %s: %d....\n", _tty_name(tty, buf),
1069
               tty->ldisc.chars_in_buffer(tty));
1070
#endif
1071
 
1072
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1073
                return;
1074
 
1075
        if (I_IXOFF(tty))
1076
                info->x_char = STOP_CHAR(tty);
1077
 
1078
        /* Turn off RTS line (do this atomic) */
1079
}
1080
 
1081
static void rs_unthrottle(struct tty_struct * tty)
1082
{
1083
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1084
#ifdef SERIAL_DEBUG_THROTTLE
1085
        char    buf[64];
1086
 
1087
        printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1088
               tty->ldisc.chars_in_buffer(tty));
1089
#endif
1090
 
1091
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1092
                return;
1093
 
1094
        if (I_IXOFF(tty)) {
1095
                if (info->x_char)
1096
                        info->x_char = 0;
1097
                else
1098
                        info->x_char = START_CHAR(tty);
1099
        }
1100
 
1101
        /* Assert RTS line (do this atomic) */
1102
}
1103
 
1104
/*
1105
 * ------------------------------------------------------------
1106
 * rs_ioctl() and friends
1107
 * ------------------------------------------------------------
1108
 */
1109
 
1110
static int get_serial_info(struct trio_serial * info,
1111
                           struct serial_struct * retinfo)
1112
{
1113
        struct serial_struct tmp;
1114
 
1115
        if (!retinfo)
1116
                return -EFAULT;
1117
        memset(&tmp, 0, sizeof(tmp));
1118
        tmp.type = info->type;
1119
        tmp.line = info->line;
1120
        tmp.irq = info->irq;
1121
        tmp.port = info->port;
1122
        tmp.flags = info->flags;
1123
        tmp.baud_base = info->baud_base;
1124
        tmp.close_delay = info->close_delay;
1125
        tmp.closing_wait = info->closing_wait;
1126
        tmp.custom_divisor = info->custom_divisor;
1127
        memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1128
        return 0;
1129
}
1130
 
1131
static int set_serial_info(struct trio_serial * info,
1132
                           struct serial_struct * new_info)
1133
{
1134
        struct serial_struct new_serial;
1135
        struct trio_serial old_info;
1136
        int                     retval = 0;
1137
 
1138
        if (!new_info)
1139
                return -EFAULT;
1140
        memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1141
        old_info = *info;
1142
 
1143
        if (!suser()) {
1144
                if ((new_serial.baud_base != info->baud_base) ||
1145
                    (new_serial.type != info->type) ||
1146
                    (new_serial.close_delay != info->close_delay) ||
1147
                    ((new_serial.flags & ~S_USR_MASK) !=
1148
                     (info->flags & ~S_USR_MASK)))
1149
                        return -EPERM;
1150
                info->flags = ((info->flags & ~S_USR_MASK) |
1151
                               (new_serial.flags & S_USR_MASK));
1152
                info->custom_divisor = new_serial.custom_divisor;
1153
                goto check_and_exit;
1154
        }
1155
 
1156
        if (info->count > 1)
1157
                return -EBUSY;
1158
 
1159
        /*
1160
         * OK, past this point, all the error checking has been done.
1161
         * At this point, we start making changes.....
1162
         */
1163
 
1164
        info->baud_base = new_serial.baud_base;
1165
        info->flags = ((info->flags & ~S_FLAGS) |
1166
                        (new_serial.flags & S_FLAGS));
1167
        info->type = new_serial.type;
1168
        info->close_delay = new_serial.close_delay;
1169
        info->closing_wait = new_serial.closing_wait;
1170
 
1171
check_and_exit:
1172
        retval = startup(info);
1173
        return retval;
1174
}
1175
 
1176
/*
1177
 * get_lsr_info - get line status register info
1178
 *
1179
 * Purpose: Let user call ioctl() to get info when the UART physically
1180
 *          is emptied.  On bus types like RS485, the transmitter must
1181
 *          release the bus after transmitting. This must be done when
1182
 *          the transmit shift register is empty, not be done when the
1183
 *          transmit holding register is empty.  This functionality
1184
 *          allows an RS485 driver to be written in user space.
1185
 */
1186
static int get_lsr_info(struct trio_serial * info, unsigned int *value)
1187
{
1188
        unsigned char status;
1189
 
1190
        cli();
1191
        status = info->uart->csr;
1192
        status &= US_TXEMPTY;
1193
        sti();
1194
        put_user(status,value);
1195
        return 0;
1196
}
1197
 
1198
/*
1199
 * This routine sends a break character out the serial port.
1200
 */
1201
static void send_break( struct trio_serial * info, int duration)
1202
{
1203
        current->state = TASK_INTERRUPTIBLE;
1204
        current->timeout = jiffies + duration;
1205
        cli();
1206
        info->uart->cr |= US_STTBRK;
1207
        if(!info->use_ints){
1208
                while(US_TXRDY != (info->uart->csr & US_TXRDY)){
1209
                        ;                       // this takes max 2ms at 9600
1210
                }
1211
                info->uart->cr |= US_STTBRK;
1212
        }
1213
        sti();
1214
}
1215
 
1216
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1217
                    unsigned int cmd, unsigned long arg)
1218
{
1219
        int error;
1220
        struct trio_serial * info = (struct trio_serial *)tty->driver_data;
1221
        int retval;
1222
 
1223
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1224
                return -ENODEV;
1225
 
1226
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1227
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1228
            (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1229
                if (tty->flags & (1 << TTY_IO_ERROR))
1230
                    return -EIO;
1231
        }
1232
 
1233
        switch (cmd) {
1234
                case TCSBRK:    /* SVID version: non-zero arg --> no break */
1235
                        retval = tty_check_change(tty);
1236
                        if (retval)
1237
                                return retval;
1238
                        tty_wait_until_sent(tty, 0);
1239
                        if (!arg)
1240
                                send_break(info, HZ/4); /* 1/4 second */
1241
                        return 0;
1242
                case TCSBRKP:   /* support for POSIX tcsendbreak() */
1243
                        retval = tty_check_change(tty);
1244
                        if (retval)
1245
                                return retval;
1246
                        tty_wait_until_sent(tty, 0);
1247
                        send_break(info, arg ? arg*(HZ/10) : HZ/4);
1248
                        return 0;
1249
                case TIOCGSOFTCAR:
1250
                        error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1251
                        if (error)
1252
                                return error;
1253
                        put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1254
                                    (unsigned long *) arg);
1255
                        return 0;
1256
                case TIOCSSOFTCAR:
1257
                        arg = get_fs_long((unsigned long *) arg);
1258
                        tty->termios->c_cflag =
1259
                                ((tty->termios->c_cflag & ~CLOCAL) |
1260
                                 (arg ? CLOCAL : 0));
1261
                        return 0;
1262
                case TIOCGSERIAL:
1263
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1264
                                                sizeof(struct serial_struct));
1265
                        if (error)
1266
                                return error;
1267
                        return get_serial_info(info,
1268
                                               (struct serial_struct *) arg);
1269
                case TIOCSSERIAL:
1270
                        return set_serial_info(info,
1271
                                               (struct serial_struct *) arg);
1272
                case TIOCSERGETLSR: /* Get line status register */
1273
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1274
                                sizeof(unsigned int));
1275
                        if (error)
1276
                                return error;
1277
                        else
1278
                            return get_lsr_info(info, (unsigned int *) arg);
1279
 
1280
                case TIOCSERGSTRUCT:
1281
                        error = verify_area(VERIFY_WRITE, (void *) arg,
1282
                                                sizeof(struct trio_serial));
1283
                        if (error)
1284
                                return error;
1285
                        memcpy_tofs((struct trio_serial *) arg,
1286
                                    info, sizeof(struct trio_serial));
1287
                        return 0;
1288
 
1289
                default:
1290
                        return -ENOIOCTLCMD;
1291
                }
1292
        return 0;
1293
}
1294
 
1295
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1296
{
1297
        struct trio_serial *info = (struct trio_serial *)tty->driver_data;
1298
 
1299
        if (tty->termios->c_cflag == old_termios->c_cflag)
1300
                return;
1301
 
1302
        change_speed(info);
1303
 
1304
        if ((old_termios->c_cflag & CRTSCTS) &&
1305
            !(tty->termios->c_cflag & CRTSCTS)) {
1306
                tty->hw_stopped = 0;
1307
                rs_start(tty);
1308
        }
1309
 
1310
}
1311
 
1312
/*
1313
 * ------------------------------------------------------------
1314
 * rs_close()
1315
 *
1316
 * This routine is called when the serial port gets closed.  First, we
1317
 * wait for the last remaining data to be sent.  Then, we unlink its
1318
 * S structure from the interrupt chain if necessary, and we free
1319
 * that IRQ if nothing is left in the chain.
1320
 * ------------------------------------------------------------
1321
 */
1322
static void rs_close(struct tty_struct *tty, struct file * filp)
1323
{
1324
        struct trio_serial * info = (struct trio_serial *)tty->driver_data;
1325
        unsigned long flags;
1326
 
1327
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1328
                return;
1329
 
1330
        save_flags(flags); cli();
1331
 
1332
        if (tty_hung_up_p(filp)) {
1333
                restore_flags(flags);
1334
                return;
1335
        }
1336
 
1337
#ifdef SERIAL_DEBUG_OPEN
1338
        printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1339
#endif
1340
        if ((tty->count == 1) && (info->count != 1)) {
1341
                /*
1342
                 * Uh, oh.  tty->count is 1, which means that the tty
1343
                 * structure will be freed.  Info->count should always
1344
                 * be one in these conditions.  If it's greater than
1345
                 * one, we've got real problems, since it means the
1346
                 * serial port won't be shutdown.
1347
                 */
1348
                printk("rs_close: bad serial port count; tty->count is 1, "
1349
                       "info->count is %d\n", info->count);
1350
                info->count = 1;
1351
        }
1352
        if (--info->count < 0) {
1353
                printk("rs_close: bad serial port count for ttyS%d: %d\n",
1354
                       info->line, info->count);
1355
                info->count = 0;
1356
        }
1357
        if (info->count) {
1358
                restore_flags(flags);
1359
                return;
1360
        }
1361
        // closing port so disable interrupts
1362
        set_ints_mode(0, info);
1363
 
1364
        info->flags |= S_CLOSING;
1365
        /*
1366
         * Save the termios structure, since this port may have
1367
         * separate termios for callout and dialin.
1368
         */
1369
        if (info->flags & S_NORMAL_ACTIVE)
1370
                info->normal_termios = *tty->termios;
1371
        if (info->flags & S_CALLOUT_ACTIVE)
1372
                info->callout_termios = *tty->termios;
1373
        /*
1374
         * Now we wait for the transmit buffer to clear; and we notify
1375
         * the line discipline to only process XON/XOFF characters.
1376
         */
1377
        tty->closing = 1;
1378
        if (info->closing_wait != S_CLOSING_WAIT_NONE)
1379
                tty_wait_until_sent(tty, info->closing_wait);
1380
        /*
1381
         * At this point we stop accepting input.  To do this, we
1382
         * disable the receive line status interrupts, and tell the
1383
         * interrupt driver to stop checking the data ready bit in the
1384
         * line status register.
1385
         */
1386
 
1387
        shutdown(info);
1388
        if (tty->driver.flush_buffer)
1389
                tty->driver.flush_buffer(tty);
1390
        if (tty->ldisc.flush_buffer)
1391
                tty->ldisc.flush_buffer(tty);
1392
        tty->closing = 0;
1393
        info->event = 0;
1394
        info->tty = 0;
1395
        if (tty->ldisc.num != ldiscs[N_TTY].num) {
1396
                if (tty->ldisc.close)
1397
                        (tty->ldisc.close)(tty);
1398
                tty->ldisc = ldiscs[N_TTY];
1399
                tty->termios->c_line = N_TTY;
1400
                if (tty->ldisc.open)
1401
                        (tty->ldisc.open)(tty);
1402
        }
1403
        if (info->blocked_open) {
1404
                if (info->close_delay) {
1405
                        current->state = TASK_INTERRUPTIBLE;
1406
                        current->timeout = jiffies + info->close_delay;
1407
                        schedule();
1408
                }
1409
                wake_up_interruptible(&info->open_wait);
1410
        }
1411
        info->flags &= ~(S_NORMAL_ACTIVE|S_CALLOUT_ACTIVE|
1412
                         S_CLOSING);
1413
        wake_up_interruptible(&info->close_wait);
1414
        restore_flags(flags);
1415
}
1416
 
1417
/*
1418
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1419
 */
1420
void rs_hangup(struct tty_struct *tty)
1421
{
1422
        struct trio_serial * info = (struct trio_serial *)tty->driver_data;
1423
 
1424
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1425
                return;
1426
 
1427
        rs_flush_buffer(tty);
1428
        shutdown(info);
1429
        info->event = 0;
1430
        info->count = 0;
1431
        info->flags &= ~(S_NORMAL_ACTIVE|S_CALLOUT_ACTIVE);
1432
        info->tty = 0;
1433
        wake_up_interruptible(&info->open_wait);
1434
}
1435
 
1436
/*
1437
 * ------------------------------------------------------------
1438
 * rs_open() and friends
1439
 * ------------------------------------------------------------
1440
 */
1441
static int block_til_ready(struct tty_struct *tty, struct file * filp,
1442
                           struct trio_serial *info)
1443
{
1444
        struct wait_queue wait = { current, NULL };
1445
        int             retval;
1446
        int             do_clocal = 0;
1447
 
1448
        /*
1449
         * If the device is in the middle of being closed, then block
1450
         * until it's done, and then try again.
1451
         */
1452
        if (info->flags & S_CLOSING) {
1453
                interruptible_sleep_on(&info->close_wait);
1454
#ifdef SERIAL_DO_RESTART
1455
                if (info->flags & S_HUP_NOTIFY)
1456
                        return -EAGAIN;
1457
                else
1458
                        return -ERESTARTSYS;
1459
#else
1460
                return -EAGAIN;
1461
#endif
1462
        }
1463
 
1464
        /*
1465
         * If this is a callout device, then just make sure the normal
1466
         * device isn't being used.
1467
         */
1468
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1469
                if (info->flags & S_NORMAL_ACTIVE)
1470
                        return -EBUSY;
1471
                if ((info->flags & S_CALLOUT_ACTIVE) &&
1472
                    (info->flags & S_SESSION_LOCKOUT) &&
1473
                    (info->session != current->session))
1474
                    return -EBUSY;
1475
                if ((info->flags & S_CALLOUT_ACTIVE) &&
1476
                    (info->flags & S_PGRP_LOCKOUT) &&
1477
                    (info->pgrp != current->pgrp))
1478
                    return -EBUSY;
1479
                info->flags |= S_CALLOUT_ACTIVE;
1480
                return 0;
1481
        }
1482
 
1483
        /*
1484
         * If non-blocking mode is set, or the port is not enabled,
1485
         * then make the check up front and then exit.
1486
         */
1487
        if ((filp->f_flags & O_NONBLOCK) ||
1488
            (tty->flags & (1 << TTY_IO_ERROR))) {
1489
                if (info->flags & S_CALLOUT_ACTIVE)
1490
                        return -EBUSY;
1491
                info->flags |= S_NORMAL_ACTIVE;
1492
                return 0;
1493
        }
1494
 
1495
        if (info->flags & S_CALLOUT_ACTIVE) {
1496
                if (info->normal_termios.c_cflag & CLOCAL)
1497
                        do_clocal = 1;
1498
        } else {
1499
                if (tty->termios->c_cflag & CLOCAL)
1500
                        do_clocal = 1;
1501
        }
1502
 
1503
        /*
1504
         * Block waiting for the carrier detect and the line to become
1505
         * free (i.e., not in use by the callout).  While we are in
1506
         * this loop, info->count is dropped by one, so that
1507
         * rs_close() knows when to free things.  We restore it upon
1508
         * exit, either normal or abnormal.
1509
         */
1510
        retval = 0;
1511
        add_wait_queue(&info->open_wait, &wait);
1512
#ifdef SERIAL_DEBUG_OPEN
1513
        printk("block_til_ready before block: ttyS%d, count = %d\n",
1514
               info->line, info->count);
1515
#endif
1516
        info->count--;
1517
        info->blocked_open++;
1518
        while (1) {
1519
                cli();
1520
                if (!(info->flags & S_CALLOUT_ACTIVE))
1521
                        trio_rtsdtr(info, 1);
1522
                sti();
1523
                current->state = TASK_INTERRUPTIBLE;
1524
                if (tty_hung_up_p(filp) ||
1525
                    !(info->flags & S_INITIALIZED)) {
1526
#ifdef SERIAL_DO_RESTART
1527
                        if (info->flags & S_HUP_NOTIFY)
1528
                                retval = -EAGAIN;
1529
                        else
1530
                                retval = -ERESTARTSYS;
1531
#else
1532
                        retval = -EAGAIN;
1533
#endif
1534
                        break;
1535
                }
1536
                if (!(info->flags & S_CALLOUT_ACTIVE) &&
1537
                    !(info->flags & S_CLOSING) && do_clocal)
1538
                        break;
1539
                if (current->signal & ~current->blocked) {
1540
                        retval = -ERESTARTSYS;
1541
                        break;
1542
                }
1543
#ifdef SERIAL_DEBUG_OPEN
1544
                printk("block_til_ready blocking: ttyS%d, count = %d\n",
1545
                       info->line, info->count);
1546
#endif
1547
                schedule();
1548
        }
1549
        current->state = TASK_RUNNING;
1550
        remove_wait_queue(&info->open_wait, &wait);
1551
        if (!tty_hung_up_p(filp))
1552
                info->count++;
1553
        info->blocked_open--;
1554
#ifdef SERIAL_DEBUG_OPEN
1555
        printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1556
               info->line, info->count);
1557
#endif
1558
        if (retval)
1559
                return retval;
1560
        info->flags |= S_NORMAL_ACTIVE;
1561
        if(!info->use_ints){
1562
                serialpoll.data = (void *)info;
1563
                queue_task(&serialpoll, &tq_timer);
1564
        }
1565
        return 0;
1566
}
1567
 
1568
/*
1569
 * This routine is called whenever a serial port is opened.  It
1570
 * enables interrupts for a serial port, linking in its S structure into
1571
 * the IRQ chain.   It also performs the serial-specific
1572
 * initialization for the tty structure.
1573
 */
1574
int rs_open(struct tty_struct *tty, struct file * filp)
1575
{
1576
        struct trio_serial      *info;
1577
        int                     retval, line;
1578
 
1579
        line = MINOR(tty->device) - tty->driver.minor_start;
1580
 
1581
        if (line != 0) /* we have exactly one */
1582
                return -ENODEV;
1583
 
1584
        info = &trio_info[0];
1585
#if 0
1586
        /* Is the kgdb running over this line? */
1587
        if (info->kgdb_channel)
1588
                return -ENODEV;
1589
#endif
1590
        if (serial_paranoia_check(info, tty->device, "rs_open"))
1591
                return -ENODEV;
1592
#ifdef SERIAL_DEBUG_OPEN
1593
        printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1594
               info->count);
1595
#endif
1596
        info->count++;
1597
        tty->driver_data = info;
1598
        info->tty = tty;
1599
 
1600
        /*
1601
         * Start up serial port
1602
         */
1603
        retval = startup(info);
1604
        if (retval)
1605
                return retval;
1606
 
1607
        retval = block_til_ready(tty, filp, info);
1608
        if (retval) {
1609
#ifdef SERIAL_DEBUG_OPEN
1610
                printk("rs_open returning after block_til_ready with %d\n",
1611
                       retval);
1612
#endif
1613
                return retval;
1614
        }
1615
 
1616
        if ((info->count == 1) && (info->flags & S_SPLIT_TERMIOS)) {
1617
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1618
                        *tty->termios = info->normal_termios;
1619
                else
1620
                        *tty->termios = info->callout_termios;
1621
                change_speed(info);
1622
        }
1623
 
1624
        info->session = current->session;
1625
        info->pgrp = current->pgrp;
1626
 
1627
#ifdef SERIAL_DEBUG_OPEN
1628
        printk("rs_open ttyS%d successful...\n", info->line);
1629
#endif
1630
        return 0;
1631
}
1632
 
1633
extern void register_console(void (*proc)(const char *));
1634
#if 0
1635
 
1636
static inline void
1637
rs_cons_check(struct trio_serial *ss, int channel)
1638
{
1639
        int i, o, io;
1640
        static consout_registered = 0;
1641
        static msg_printed = 0;
1642
 
1643
        i = o = io = 0;
1644
 
1645
        /* Is this one of the serial console lines? */
1646
        if((trio_cons_chanout != channel) &&
1647
           (trio_cons_chanin != channel))
1648
                return;
1649
        trio_conschan = ss->trio_channel;
1650
        trio_consinfo = ss;
1651
 
1652
        /* Register the console output putchar, if necessary */
1653
        if((trio_cons_chanout == channel)) {
1654
                o = 1;
1655
                /* double whee.. */
1656
                if(!consout_registered) {
1657
                        register_console(trio_console_print);
1658
                        consout_registered = 1;
1659
                }
1660
        }
1661
 
1662
        /* If this is console input, we handle the break received
1663
         * status interrupt on this line to mean prom_halt().
1664
         */
1665
        if(trio_cons_chanin == channel) {
1666
                ss->break_abort = 1;
1667
                i = 1;
1668
        }
1669
        if(o && i)
1670
                io = 1;
1671
        if(ss->baud != 9600)
1672
                panic("Console baud rate weirdness");
1673
 
1674
        /* Set flag variable for this port so that it cannot be
1675
         * opened for other uses by accident.
1676
         */
1677
        ss->is_cons = 1;
1678
 
1679
        if(io) {
1680
                if(!msg_printed) {
1681
                        printk("zs%d: console I/O\n", ((channel>>1)&1));
1682
                        msg_printed = 1;
1683
                }
1684
        } else {
1685
                printk("zs%d: console %s\n", ((channel>>1)&1),
1686
                       (i==1 ? "input" : (o==1 ? "output" : "WEIRD")));
1687
        }
1688
}
1689
#endif
1690
 
1691
static struct irqaction irq_usarta = { rs_interrupta, 0, 0, "usarta", NULL, NULL};
1692
static struct irqaction irq_usartb = { rs_interruptb, 0, 0, "usartb", NULL, NULL};
1693
 
1694
extern int setup_arm_irq(int, struct irqaction *);
1695
 
1696
void interrupts_init(void)
1697
{
1698
        setup_arm_irq(IRQ_USARTA, &irq_usarta);
1699
        setup_arm_irq(IRQ_USARTB, &irq_usartb);
1700
}
1701
 
1702
/* rs_init inits the driver */
1703
int rs_trio_init(void)
1704
{
1705
        int flags,i;
1706
        struct trio_serial *info;
1707
        /* Setup base handler, and timer table. */
1708
        init_bh(SERIAL_BH, do_serial_bh);
1709
        timer_table[RS_TIMER].fn = rs_timer;
1710
        timer_table[RS_TIMER].expires = 0;
1711
 
1712
        /* Initialize the tty_driver structure */
1713
 
1714
        memset(&serial_driver, 0, sizeof(struct tty_driver));
1715
        serial_driver.magic = TTY_DRIVER_MAGIC;
1716
        serial_driver.name = "ttyS";
1717
        serial_driver.major = TTY_MAJOR;
1718
        serial_driver.minor_start = 64;
1719
        serial_driver.num = 1;
1720
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1721
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
1722
        serial_driver.init_termios = tty_std_termios;
1723
 
1724
        serial_driver.init_termios.c_cflag =
1725
                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1726
        serial_driver.flags = TTY_DRIVER_REAL_RAW;
1727
        serial_driver.refcount = &serial_refcount;
1728
        serial_driver.table = serial_table;
1729
        serial_driver.termios = serial_termios;
1730
        serial_driver.termios_locked = serial_termios_locked;
1731
 
1732
        serial_driver.open = rs_open;
1733
        serial_driver.close = rs_close;
1734
        serial_driver.write = rs_write;
1735
        serial_driver.flush_chars = rs_flush_chars;
1736
        serial_driver.write_room = rs_write_room;
1737
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
1738
        serial_driver.flush_buffer = rs_flush_buffer;
1739
        serial_driver.ioctl = rs_ioctl;
1740
        serial_driver.throttle = rs_throttle;
1741
        serial_driver.unthrottle = rs_unthrottle;
1742
        serial_driver.set_termios = rs_set_termios;
1743
        serial_driver.stop = rs_stop;
1744
        serial_driver.start = rs_start;
1745
        serial_driver.hangup = rs_hangup;
1746
        serial_driver.set_ldisc = rs_set_ldisc;
1747
 
1748
        /*
1749
         * The callout device is just like normal device except for
1750
         * major number and the subtype code.
1751
         */
1752
        callout_driver = serial_driver;
1753
        callout_driver.name = "cua";
1754
        callout_driver.major = TTYAUX_MAJOR;
1755
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1756
 
1757
        if (tty_register_driver(&serial_driver))
1758
                panic("Couldn't register serial driver\n");
1759
        if (tty_register_driver(&callout_driver))
1760
                panic("Couldn't register callout driver\n");
1761
 
1762
        save_flags(flags); cli();
1763
        i=0;
1764
        while(i<US_NB){
1765
                info = &trio_info[i];
1766
                info->magic = SERIAL_MAGIC;
1767
                info->uart = uarts[i];
1768
                info->tty = 0;
1769
                info->irqmask = (i)?AIC_UB:AIC_UA;
1770
                info->irq = (i)?IRQ_USARTB:IRQ_USARTA;
1771
                info->port = i+1;
1772
                set_ints_mode(0,info);
1773
                info->custom_divisor = 16;
1774
                info->close_delay = 50;
1775
                info->closing_wait = 3000;
1776
                info->x_char = 0;
1777
                info->event = 0;
1778
                info->count = 0;
1779
                info->blocked_open = 0;
1780
                info->tqueue.routine = do_softint;
1781
                info->tqueue.data = info;
1782
                info->tqueue_hangup.routine = do_serial_hangup;
1783
                info->tqueue_hangup.data = info;
1784
                info->callout_termios =callout_driver.init_termios;
1785
                info->normal_termios = serial_driver.init_termios;
1786
                info->open_wait = 0;
1787
                info->close_wait = 0;
1788
                info->line = 0;
1789
                info->is_cons = (i)?0:1; /* Means shortcuts work */
1790
                i++;
1791
        }
1792
        interrupts_init();
1793
        restore_flags(flags);
1794
// hack to do polling
1795
        serialpoll.routine = serpoll;
1796
        serialpoll.data = 0;
1797
 
1798
        return 0;
1799
}
1800
 
1801
/*
1802
 * register_serial and unregister_serial allows for serial ports to be
1803
 * configured at run-time, to support PCMCIA modems.
1804
 */
1805
/* SPARC: Unused at this time, just here to make things link. */
1806
int register_serial(struct serial_struct *req)
1807
{
1808
        return -1;
1809
}
1810
 
1811
void unregister_serial(int line)
1812
{
1813
        return;
1814
}
1815
 
1816
#if 0
1817
/* Hooks for running a serial console.  con_init() calls this if the
1818
 * console is being run over one of the ttya/ttyb serial ports.
1819
 * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
1820
 * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
1821
 * are addressed backwards, channel B is first, then channel A.
1822
 */
1823
void
1824
rs_cons_hook(int chip, int out, int channel)
1825
{
1826
        if(chip)
1827
                panic("rs_cons_hook called with chip not zero");
1828
        if(!trio_chips[chip]) {
1829
                trio_chips[chip] = get_zs(chip);
1830
                /* Two channels per chip */
1831
                trio_channels[(chip*2)] = &trio_chips[chip]->channelA;
1832
                trio_channels[(chip*2)+1] = &trio_chips[chip]->channelB;
1833
        }
1834
        trio_info[channel].trio_channel = trio_channels[channel];
1835
        trio_info[channel].change_needed = 0;
1836
        trio_info[channel].clk_divisor = 16;
1837
        trio_info[channel].trio_baud = get_zsbaud(&trio_info[channel]);
1838
        rs_cons_check(&trio_info[channel], channel);
1839
        if(out)
1840
                trio_cons_chanout = ((chip * 2) + channel);
1841
        else
1842
                trio_cons_chanin = ((chip * 2) + channel);
1843
 
1844
}
1845
 
1846
/* This is called at boot time to prime the kgdb serial debugging
1847
 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
1848
 * for /dev/ttyb which is determined in setup_arch() from the
1849
 * boot command line flags.
1850
 */
1851
void
1852
rs_kgdb_hook(int tty_num)
1853
{
1854
        int chip = 0;
1855
 
1856
        if(!trio_chips[chip]) {
1857
                trio_chips[chip] = get_zs(chip);
1858
                /* Two channels per chip */
1859
                trio_channels[(chip*2)] = &trio_chips[chip]->channelA;
1860
                trio_channels[(chip*2)+1] = &trio_chips[chip]->channelB;
1861
        }
1862
        trio_info[tty_num].trio_channel = trio_channels[tty_num];
1863
        trio_kgdbchan = trio_info[tty_num].trio_channel;
1864
        trio_info[tty_num].change_needed = 0;
1865
        trio_info[tty_num].clk_divisor = 16;
1866
        trio_info[tty_num].trio_baud = get_zsbaud(&trio_info[tty_num]);
1867
        trio_info[tty_num].kgdb_channel = 1;     /* This runs kgdb */
1868
        trio_info[tty_num ^ 1].kgdb_channel = 0; /* This does not */
1869
        /* Turn on transmitter/receiver at 8-bits/char */
1870
        kgdb_chaninit(&trio_info[tty_num], 0, 9600);
1871
        ZS_CLEARERR(trio_kgdbchan);
1872
        udelay(5);
1873
        ZS_CLEARFIFO(trio_kgdbchan);
1874
}
1875
#endif

powered by: WebSVN 2.1.0

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