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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [char/] [68332serial.c] - Blame information for rev 1626

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

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

powered by: WebSVN 2.1.0

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