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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [char/] [68302serial.c] - Blame information for rev 199

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

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

powered by: WebSVN 2.1.0

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