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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [macintosh/] [macserial.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * macserial.c: Serial port driver for Power Macintoshes.
3
 *
4
 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5
 *
6
 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
7
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
8
 *
9
 * Receive DMA code by Takashi Oe <toe@unlserve.unl.edu>.
10
 *
11
 * $Id: macserial.c,v 1.1.1.1 2004-04-15 02:30:02 phoenix Exp $
12
 */
13
 
14
#include <linux/config.h>
15
#include <linux/errno.h>
16
#include <linux/module.h>
17
#include <linux/signal.h>
18
#include <linux/sched.h>
19
#include <linux/timer.h>
20
#include <linux/interrupt.h>
21
#include <linux/tty.h>
22
#include <linux/tty_flip.h>
23
#include <linux/major.h>
24
#include <linux/string.h>
25
#include <linux/fcntl.h>
26
#include <linux/mm.h>
27
#include <linux/kernel.h>
28
#include <linux/delay.h>
29
#include <linux/init.h>
30
#ifdef CONFIG_SERIAL_CONSOLE
31
#include <linux/console.h>
32
#endif
33
#include <linux/slab.h>
34
 
35
#include <asm/sections.h>
36
#include <asm/io.h>
37
#include <asm/pgtable.h>
38
#include <asm/irq.h>
39
#include <asm/prom.h>
40
#include <asm/system.h>
41
#include <asm/segment.h>
42
#include <asm/bitops.h>
43
#include <asm/machdep.h>
44
#include <asm/pmac_feature.h>
45
#include <linux/adb.h>
46
#include <linux/pmu.h>
47
#ifdef CONFIG_KGDB
48
#include <asm/kgdb.h>
49
#endif
50
#include <asm/dbdma.h>
51
 
52
#include "macserial.h"
53
 
54
#ifdef CONFIG_PMAC_PBOOK
55
static int serial_notify_sleep(struct pmu_sleep_notifier *self, int when);
56
static struct pmu_sleep_notifier serial_sleep_notifier = {
57
        serial_notify_sleep,
58
        SLEEP_LEVEL_MISC,
59
};
60
#endif
61
 
62
#define SUPPORT_SERIAL_DMA
63
#define MACSERIAL_VERSION       "2.0"
64
 
65
/*
66
 * It would be nice to dynamically allocate everything that
67
 * depends on NUM_SERIAL, so we could support any number of
68
 * Z8530s, but for now...
69
 */
70
#define NUM_SERIAL      2               /* Max number of ZS chips supported */
71
#define NUM_CHANNELS    (NUM_SERIAL * 2)        /* 2 channels per chip */
72
 
73
/* On PowerMacs, the hardware takes care of the SCC recovery time,
74
   but we need the eieio to make sure that the accesses occur
75
   in the order we want. */
76
#define RECOVERY_DELAY  eieio()
77
 
78
struct mac_zschannel zs_channels[NUM_CHANNELS];
79
 
80
struct mac_serial zs_soft[NUM_CHANNELS];
81
int zs_channels_found;
82
struct mac_serial *zs_chain;    /* list of all channels */
83
 
84
struct tty_struct zs_ttys[NUM_CHANNELS];
85
 
86
static int is_powerbook;
87
 
88
#ifdef CONFIG_SERIAL_CONSOLE
89
static struct console sercons;
90
#endif
91
 
92
#ifdef CONFIG_KGDB
93
struct mac_zschannel *zs_kgdbchan;
94
static unsigned char scc_inittab[] = {
95
        9,  0x80,       /* reset A side (CHRA) */
96
        13, 0,           /* set baud rate divisor */
97
        12, 1,
98
        14, 1,          /* baud rate gen enable, src=rtxc (BRENABL) */
99
        11, 0x50,       /* clocks = br gen (RCBR | TCBR) */
100
        5,  0x6a,       /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
101
        4,  0x44,       /* x16 clock, 1 stop (SB1 | X16CLK)*/
102
        3,  0xc1,       /* rx enable, 8 bits (RxENABLE | Rx8)*/
103
};
104
#endif
105
#define ZS_CLOCK         3686400        /* Z8530 RTxC input clock rate */
106
 
107
static DECLARE_TASK_QUEUE(tq_serial);
108
 
109
static struct tty_driver serial_driver, callout_driver;
110
static int serial_refcount;
111
 
112
/* serial subtype definitions */
113
#define SERIAL_TYPE_NORMAL      1
114
#define SERIAL_TYPE_CALLOUT     2
115
 
116
/* number of characters left in xmit buffer before we ask for more */
117
#define WAKEUP_CHARS 256
118
 
119
/*
120
 * Debugging.
121
 */
122
#undef SERIAL_DEBUG_INTR
123
#undef SERIAL_DEBUG_OPEN
124
#undef SERIAL_DEBUG_FLOW
125
#undef SERIAL_DEBUG_POWER
126
#undef SERIAL_DEBUG_THROTTLE
127
#undef SERIAL_DEBUG_STOP
128
#undef SERIAL_DEBUG_BAUDS
129
 
130
#define RS_STROBE_TIME 10
131
#define RS_ISR_PASS_LIMIT 256
132
 
133
#define _INLINE_ inline
134
 
135
#ifdef SERIAL_DEBUG_OPEN
136
#define OPNDBG(fmt, arg...)     printk(KERN_DEBUG fmt , ## arg)
137
#else
138
#define OPNDBG(fmt, arg...)     do { } while (0)
139
#endif
140
#ifdef SERIAL_DEBUG_POWER
141
#define PWRDBG(fmt, arg...)     printk(KERN_DEBUG fmt , ## arg)
142
#else
143
#define PWRDBG(fmt, arg...)     do { } while (0)
144
#endif
145
#ifdef SERIAL_DEBUG_BAUDS
146
#define BAUDBG(fmt, arg...)     printk(fmt , ## arg)
147
#else
148
#define BAUDBG(fmt, arg...)     do { } while (0)
149
#endif
150
 
151
static void probe_sccs(void);
152
static void change_speed(struct mac_serial *info, struct termios *old);
153
static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
154
static int set_scc_power(struct mac_serial * info, int state);
155
static int setup_scc(struct mac_serial * info);
156
static void dbdma_reset(volatile struct dbdma_regs *dma);
157
static void dbdma_flush(volatile struct dbdma_regs *dma);
158
static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs);
159
static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs);
160
static void dma_init(struct mac_serial * info);
161
static void rxdma_start(struct mac_serial * info, int current);
162
static void rxdma_to_tty(struct mac_serial * info);
163
 
164
static struct tty_struct *serial_table[NUM_CHANNELS];
165
static struct termios *serial_termios[NUM_CHANNELS];
166
static struct termios *serial_termios_locked[NUM_CHANNELS];
167
 
168
#ifndef MIN
169
#define MIN(a,b)        ((a) < (b) ? (a) : (b))
170
#endif
171
 
172
/*
173
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
174
 * lock it in case the copy_from_user blocks while swapping in a page,
175
 * and some other program tries to do a serial write at the same time.
176
 * Since the lock will only come under contention when the system is
177
 * swapping and available memory is low, it makes sense to share one
178
 * buffer across all the serial ports, since it significantly saves
179
 * memory if large numbers of serial ports are open.
180
 */
181
static unsigned char *tmp_buf;
182
static DECLARE_MUTEX(tmp_buf_sem);
183
 
184
 
185
static inline int __pmac
186
serial_paranoia_check(struct mac_serial *info,
187
                      dev_t device, const char *routine)
188
{
189
#ifdef SERIAL_PARANOIA_CHECK
190
        static const char badmagic[] = KERN_WARNING
191
                "Warning: bad magic number for serial struct (%d, %d) in %s\n";
192
        static const char badinfo[] = KERN_WARNING
193
                "Warning: null mac_serial for (%d, %d) in %s\n";
194
 
195
        if (!info) {
196
                printk(badinfo, MAJOR(device), MINOR(device), routine);
197
                return 1;
198
        }
199
        if (info->magic != SERIAL_MAGIC) {
200
                printk(badmagic, MAJOR(device), MINOR(device), routine);
201
                return 1;
202
        }
203
#endif
204
        return 0;
205
}
206
 
207
/*
208
 * Reading and writing Z8530 registers.
209
 */
210
static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel,
211
                                              unsigned char reg)
212
{
213
        unsigned char retval;
214
        unsigned long flags;
215
 
216
        /*
217
         * We have to make this atomic.
218
         */
219
        spin_lock_irqsave(&channel->lock, flags);
220
        if (reg != 0) {
221
                *channel->control = reg;
222
                RECOVERY_DELAY;
223
        }
224
        retval = *channel->control;
225
        RECOVERY_DELAY;
226
        spin_unlock_irqrestore(&channel->lock, flags);
227
        return retval;
228
}
229
 
230
static inline void __pmac write_zsreg(struct mac_zschannel *channel,
231
                                      unsigned char reg, unsigned char value)
232
{
233
        unsigned long flags;
234
 
235
        spin_lock_irqsave(&channel->lock, flags);
236
        if (reg != 0) {
237
                *channel->control = reg;
238
                RECOVERY_DELAY;
239
        }
240
        *channel->control = value;
241
        RECOVERY_DELAY;
242
        spin_unlock_irqrestore(&channel->lock, flags);
243
        return;
244
}
245
 
246
static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel)
247
{
248
        unsigned char retval;
249
 
250
        retval = *channel->data;
251
        RECOVERY_DELAY;
252
        return retval;
253
}
254
 
255
static inline void write_zsdata(struct mac_zschannel *channel,
256
                                unsigned char value)
257
{
258
        *channel->data = value;
259
        RECOVERY_DELAY;
260
        return;
261
}
262
 
263
static inline void load_zsregs(struct mac_zschannel *channel,
264
                               unsigned char *regs)
265
{
266
        ZS_CLEARERR(channel);
267
        ZS_CLEARFIFO(channel);
268
        /* Load 'em up */
269
        write_zsreg(channel, R4, regs[R4]);
270
        write_zsreg(channel, R10, regs[R10]);
271
        write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
272
        write_zsreg(channel, R5, regs[R5] & ~TxENAB);
273
        write_zsreg(channel, R1, regs[R1]);
274
        write_zsreg(channel, R9, regs[R9]);
275
        write_zsreg(channel, R11, regs[R11]);
276
        write_zsreg(channel, R12, regs[R12]);
277
        write_zsreg(channel, R13, regs[R13]);
278
        write_zsreg(channel, R14, regs[R14]);
279
        write_zsreg(channel, R15, regs[R15]);
280
        write_zsreg(channel, R3, regs[R3]);
281
        write_zsreg(channel, R5, regs[R5]);
282
        return;
283
}
284
 
285
/* Sets or clears DTR/RTS on the requested line */
286
static inline void zs_rtsdtr(struct mac_serial *ss, int set)
287
{
288
        if (set)
289
                ss->curregs[5] |= (RTS | DTR);
290
        else
291
                ss->curregs[5] &= ~(RTS | DTR);
292
        write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
293
        return;
294
}
295
 
296
/* Utility routines for the Zilog */
297
static inline int get_zsbaud(struct mac_serial *ss)
298
{
299
        struct mac_zschannel *channel = ss->zs_channel;
300
        int brg;
301
 
302
        if ((ss->curregs[R11] & TCBR) == 0) {
303
                /* higher rates don't use the baud rate generator */
304
                return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16;
305
        }
306
        /* The baud rate is split up between two 8-bit registers in
307
         * what is termed 'BRG time constant' format in my docs for
308
         * the chip, it is a function of the clk rate the chip is
309
         * receiving which happens to be constant.
310
         */
311
        brg = (read_zsreg(channel, 13) << 8);
312
        brg |= read_zsreg(channel, 12);
313
        return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
314
}
315
 
316
/* On receive, this clears errors and the receiver interrupts */
317
static inline void rs_recv_clear(struct mac_zschannel *zsc)
318
{
319
        write_zsreg(zsc, 0, ERR_RES);
320
        write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
321
}
322
 
323
/*
324
 * Reset a Descriptor-Based DMA channel.
325
 */
326
static void dbdma_reset(volatile struct dbdma_regs *dma)
327
{
328
        int i;
329
 
330
        out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16);
331
 
332
        /*
333
         * Yes this looks peculiar, but apparently it needs to be this
334
         * way on some machines.  (We need to make sure the DBDMA
335
         * engine has actually got the write above and responded
336
         * to it. - paulus)
337
         */
338
        for (i = 200; i > 0; --i)
339
                if (ld_le32(&dma->status) & RUN)
340
                        udelay(1);
341
}
342
 
343
/*
344
 * Tells a DBDMA channel to stop and write any buffered data
345
 * it might have to memory.
346
 */
347
static _INLINE_ void dbdma_flush(volatile struct dbdma_regs *dma)
348
{
349
        int i = 0;
350
 
351
        out_le32(&dma->control, (FLUSH << 16) | FLUSH);
352
        while (((in_le32(&dma->status) & FLUSH) != 0) && (i++ < 100))
353
                udelay(1);
354
}
355
 
356
/*
357
 * ----------------------------------------------------------------------
358
 *
359
 * Here starts the interrupt handling routines.  All of the following
360
 * subroutines are declared as inline and are folded into
361
 * rs_interrupt().  They were separated out for readability's sake.
362
 *
363
 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
364
 * -----------------------------------------------------------------------
365
 */
366
 
367
/*
368
 * This routine is used by the interrupt handler to schedule
369
 * processing in the software interrupt portion of the driver.
370
 */
371
static _INLINE_ void rs_sched_event(struct mac_serial *info,
372
                                  int event)
373
{
374
        info->event |= 1 << event;
375
        queue_task(&info->tqueue, &tq_serial);
376
        mark_bh(MACSERIAL_BH);
377
}
378
 
379
/* Work out the flag value for a z8530 status value. */
380
static _INLINE_ int stat_to_flag(int stat)
381
{
382
        int flag;
383
 
384
        if (stat & Rx_OVR) {
385
                flag = TTY_OVERRUN;
386
        } else if (stat & FRM_ERR) {
387
                flag = TTY_FRAME;
388
        } else if (stat & PAR_ERR) {
389
                flag = TTY_PARITY;
390
        } else
391
                flag = 0;
392
        return flag;
393
}
394
 
395
static _INLINE_ void receive_chars(struct mac_serial *info,
396
                                   struct pt_regs *regs)
397
{
398
        struct tty_struct *tty = info->tty;
399
        unsigned char ch, stat, flag;
400
 
401
        while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
402
 
403
                stat = read_zsreg(info->zs_channel, R1);
404
                ch = read_zsdata(info->zs_channel);
405
 
406
#ifdef CONFIG_KGDB
407
                if (info->kgdb_channel) {
408
                        if (ch == 0x03 || ch == '$')
409
                                breakpoint();
410
                        if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
411
                                write_zsreg(info->zs_channel, 0, ERR_RES);
412
                        return;
413
                }
414
#endif
415
                if (!tty)
416
                        continue;
417
                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
418
                        tty_flip_buffer_push(tty);
419
 
420
                if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
421
                        static int flip_buf_ovf;
422
                        if (++flip_buf_ovf <= 1)
423
                                printk(KERN_WARNING "FB. overflow: %d\n",
424
                                                    flip_buf_ovf);
425
                        break;
426
                }
427
                tty->flip.count++;
428
                {
429
                        static int flip_max_cnt;
430
                        if (flip_max_cnt < tty->flip.count)
431
                                flip_max_cnt = tty->flip.count;
432
                }
433
                flag = stat_to_flag(stat);
434
                if (flag)
435
                        /* reset the error indication */
436
                        write_zsreg(info->zs_channel, 0, ERR_RES);
437
                *tty->flip.flag_buf_ptr++ = flag;
438
                *tty->flip.char_buf_ptr++ = ch;
439
        }
440
        if (tty)
441
                tty_flip_buffer_push(tty);
442
}
443
 
444
static void transmit_chars(struct mac_serial *info)
445
{
446
        unsigned long flags;
447
 
448
        save_flags(flags);
449
        cli();
450
        if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
451
                goto out;
452
        info->tx_active = 0;
453
 
454
        if (info->x_char && !info->power_wait) {
455
                /* Send next char */
456
                write_zsdata(info->zs_channel, info->x_char);
457
                info->x_char = 0;
458
                info->tx_active = 1;
459
                goto out;
460
        }
461
 
462
        if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped
463
            || info->power_wait) {
464
                write_zsreg(info->zs_channel, 0, RES_Tx_P);
465
                goto out;
466
        }
467
 
468
        /* Send char */
469
        write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
470
        info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
471
        info->xmit_cnt--;
472
        info->tx_active = 1;
473
 
474
        if (info->xmit_cnt < WAKEUP_CHARS)
475
                rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
476
 
477
 out:
478
        restore_flags(flags);
479
}
480
 
481
static void powerup_done(unsigned long data)
482
{
483
        struct mac_serial *info = (struct mac_serial *) data;
484
 
485
        info->power_wait = 0;
486
        transmit_chars(info);
487
}
488
 
489
static _INLINE_ void status_handle(struct mac_serial *info)
490
{
491
        unsigned char status;
492
 
493
        /* Get status from Read Register 0 */
494
        status = read_zsreg(info->zs_channel, 0);
495
 
496
        /* Check for DCD transitions */
497
        if (((status ^ info->read_reg_zero) & DCD) != 0
498
            && info->tty && !C_CLOCAL(info->tty)) {
499
                if (status & DCD) {
500
                        wake_up_interruptible(&info->open_wait);
501
                } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
502
                        if (info->tty)
503
                                tty_hangup(info->tty);
504
                }
505
        }
506
 
507
        /* Check for CTS transitions */
508
        if (info->tty && C_CRTSCTS(info->tty)) {
509
                /*
510
                 * For some reason, on the Power Macintosh,
511
                 * it seems that the CTS bit is 1 when CTS is
512
                 * *negated* and 0 when it is asserted.
513
                 * The DCD bit doesn't seem to be inverted
514
                 * like this.
515
                 */
516
                if ((status & CTS) == 0) {
517
                        if (info->tx_stopped) {
518
#ifdef SERIAL_DEBUG_FLOW
519
                                printk(KERN_DEBUG "CTS up\n");
520
#endif
521
                                info->tx_stopped = 0;
522
                                if (!info->tx_active)
523
                                        transmit_chars(info);
524
                        }
525
                } else {
526
#ifdef SERIAL_DEBUG_FLOW
527
                        printk(KERN_DEBUG "CTS down\n");
528
#endif
529
                        info->tx_stopped = 1;
530
                }
531
        }
532
 
533
        /* Clear status condition... */
534
        write_zsreg(info->zs_channel, 0, RES_EXT_INT);
535
        info->read_reg_zero = status;
536
}
537
 
538
static _INLINE_ void receive_special_dma(struct mac_serial *info)
539
{
540
        unsigned char stat, flag;
541
        volatile struct dbdma_regs *rd = &info->rx->dma;
542
        int where = RX_BUF_SIZE;
543
 
544
        spin_lock(&info->rx_dma_lock);
545
        if ((ld_le32(&rd->status) & ACTIVE) != 0)
546
                dbdma_flush(rd);
547
        if (in_le32(&rd->cmdptr)
548
            == virt_to_bus(info->rx_cmds[info->rx_cbuf] + 1))
549
                where -= in_le16(&info->rx->res_count);
550
        where--;
551
 
552
        stat = read_zsreg(info->zs_channel, R1);
553
 
554
        flag = stat_to_flag(stat);
555
        if (flag) {
556
                info->rx_flag_buf[info->rx_cbuf][where] = flag;
557
                /* reset the error indication */
558
                write_zsreg(info->zs_channel, 0, ERR_RES);
559
        }
560
 
561
        spin_unlock(&info->rx_dma_lock);
562
}
563
 
564
/*
565
 * This is the serial driver's generic interrupt routine
566
 */
567
static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
568
{
569
        struct mac_serial *info = (struct mac_serial *) dev_id;
570
        unsigned char zs_intreg;
571
        int shift;
572
 
573
        if (!(info->flags & ZILOG_INITIALIZED)) {
574
                printk(KERN_WARNING "rs_interrupt: irq %d, port not "
575
                                    "initialized\n", irq);
576
                disable_irq(irq);
577
                return;
578
        }
579
 
580
        /* NOTE: The read register 3, which holds the irq status,
581
         *       does so for both channels on each chip.  Although
582
         *       the status value itself must be read from the A
583
         *       channel and is only valid when read from channel A.
584
         *       Yes... broken hardware...
585
         */
586
#define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
587
 
588
        if (info->zs_chan_a == info->zs_channel)
589
                shift = 3;      /* Channel A */
590
        else
591
                shift = 0;       /* Channel B */
592
 
593
        for (;;) {
594
                zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
595
#ifdef SERIAL_DEBUG_INTR
596
                printk(KERN_DEBUG "rs_interrupt: irq %d, zs_intreg 0x%x\n",
597
                       irq, (int)zs_intreg);
598
#endif
599
 
600
                if ((zs_intreg & CHAN_IRQMASK) == 0)
601
                        break;
602
 
603
                if (zs_intreg & CHBRxIP) {
604
                        /* If we are doing DMA, we only ask for interrupts
605
                           on characters with errors or special conditions. */
606
                        if (info->dma_initted)
607
                                receive_special_dma(info);
608
                        else
609
                                receive_chars(info, regs);
610
                }
611
                if (zs_intreg & CHBTxIP)
612
                        transmit_chars(info);
613
                if (zs_intreg & CHBEXT)
614
                        status_handle(info);
615
        }
616
}
617
 
618
/* Transmit DMA interrupt - not used at present */
619
static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs)
620
{
621
}
622
 
623
/*
624
 * Receive DMA interrupt.
625
 */
626
static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs)
627
{
628
        struct mac_serial *info = (struct mac_serial *) dev_id;
629
        volatile struct dbdma_cmd *cd;
630
 
631
        if (!info->dma_initted)
632
                return;
633
        spin_lock(&info->rx_dma_lock);
634
        /* First, confirm that this interrupt is, indeed, coming */
635
        /* from Rx DMA */
636
        cd = info->rx_cmds[info->rx_cbuf] + 2;
637
        if ((in_le16(&cd->xfer_status) & (RUN | ACTIVE)) != (RUN | ACTIVE)) {
638
                spin_unlock(&info->rx_dma_lock);
639
                return;
640
        }
641
        if (info->rx_fbuf != RX_NO_FBUF) {
642
                info->rx_cbuf = info->rx_fbuf;
643
                if (++info->rx_fbuf == info->rx_nbuf)
644
                        info->rx_fbuf = 0;
645
                if (info->rx_fbuf == info->rx_ubuf)
646
                        info->rx_fbuf = RX_NO_FBUF;
647
        }
648
        spin_unlock(&info->rx_dma_lock);
649
}
650
 
651
/*
652
 * -------------------------------------------------------------------
653
 * Here ends the serial interrupt routines.
654
 * -------------------------------------------------------------------
655
 */
656
 
657
/*
658
 * ------------------------------------------------------------
659
 * rs_stop() and rs_start()
660
 *
661
 * This routines are called before setting or resetting tty->stopped.
662
 * ------------------------------------------------------------
663
 */
664
static void rs_stop(struct tty_struct *tty)
665
{
666
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
667
 
668
#ifdef SERIAL_DEBUG_STOP
669
        printk(KERN_DEBUG "rs_stop %ld....\n",
670
               tty->ldisc.chars_in_buffer(tty));
671
#endif
672
 
673
        if (serial_paranoia_check(info, tty->device, "rs_stop"))
674
                return;
675
 
676
#if 0
677
        save_flags(flags); cli();
678
        if (info->curregs[5] & TxENAB) {
679
                info->curregs[5] &= ~TxENAB;
680
                info->pendregs[5] &= ~TxENAB;
681
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
682
        }
683
        restore_flags(flags);
684
#endif
685
}
686
 
687
static void rs_start(struct tty_struct *tty)
688
{
689
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
690
        unsigned long flags;
691
 
692
#ifdef SERIAL_DEBUG_STOP
693
        printk(KERN_DEBUG "rs_start %ld....\n",
694
               tty->ldisc.chars_in_buffer(tty));
695
#endif
696
 
697
        if (serial_paranoia_check(info, tty->device, "rs_start"))
698
                return;
699
 
700
        save_flags(flags); cli();
701
#if 0
702
        if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
703
                info->curregs[5] |= TxENAB;
704
                info->pendregs[5] = info->curregs[5];
705
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
706
        }
707
#else
708
        if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
709
                transmit_chars(info);
710
        }
711
#endif
712
        restore_flags(flags);
713
}
714
 
715
/*
716
 * This routine is used to handle the "bottom half" processing for the
717
 * serial driver, known also the "software interrupt" processing.
718
 * This processing is done at the kernel interrupt level, after the
719
 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
720
 * is where time-consuming activities which can not be done in the
721
 * interrupt driver proper are done; the interrupt driver schedules
722
 * them using rs_sched_event(), and they get done here.
723
 */
724
static void do_serial_bh(void)
725
{
726
        run_task_queue(&tq_serial);
727
}
728
 
729
static void do_softint(void *private_)
730
{
731
        struct mac_serial       *info = (struct mac_serial *) private_;
732
        struct tty_struct       *tty;
733
 
734
        tty = info->tty;
735
        if (!tty)
736
                return;
737
 
738
        if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
739
                if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
740
                    tty->ldisc.write_wakeup)
741
                        (tty->ldisc.write_wakeup)(tty);
742
                wake_up_interruptible(&tty->write_wait);
743
        }
744
}
745
 
746
static int startup(struct mac_serial * info)
747
{
748
        int delay;
749
 
750
        OPNDBG("startup() (ttyS%d, irq %d)\n", info->line, info->irq);
751
 
752
        if (info->flags & ZILOG_INITIALIZED) {
753
                OPNDBG(" -> already inited\n");
754
                return 0;
755
        }
756
 
757
        if (!info->xmit_buf) {
758
                info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
759
                if (!info->xmit_buf)
760
                        return -ENOMEM;
761
        }
762
 
763
        OPNDBG("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
764
 
765
        delay = set_scc_power(info, 1);
766
 
767
        setup_scc(info);
768
 
769
        if (delay) {
770
                unsigned long flags;
771
 
772
                /* delay is in ms */
773
                save_flags(flags);
774
                cli();
775
                info->power_wait = 1;
776
                mod_timer(&info->powerup_timer,
777
                          jiffies + (delay * HZ + 999) / 1000);
778
                restore_flags(flags);
779
        }
780
 
781
        OPNDBG("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq);
782
 
783
        info->flags |= ZILOG_INITIALIZED;
784
        enable_irq(info->irq);
785
        if (info->dma_initted) {
786
                enable_irq(info->rx_dma_irq);
787
        }
788
 
789
        return 0;
790
}
791
 
792
static _INLINE_ void rxdma_start(struct mac_serial * info, int current)
793
{
794
        volatile struct dbdma_regs *rd = &info->rx->dma;
795
        volatile struct dbdma_cmd *cd = info->rx_cmds[current];
796
 
797
//printk(KERN_DEBUG "SCC: rxdma_start\n");
798
 
799
        st_le32(&rd->cmdptr, virt_to_bus(cd));
800
        out_le32(&rd->control, (RUN << 16) | RUN);
801
}
802
 
803
static void rxdma_to_tty(struct mac_serial *info)
804
{
805
        struct tty_struct       *tty = info->tty;
806
        volatile struct dbdma_regs *rd = &info->rx->dma;
807
        unsigned long flags;
808
        int residue, available, space, do_queue;
809
 
810
        if (!tty)
811
                return;
812
 
813
        do_queue = 0;
814
        spin_lock_irqsave(&info->rx_dma_lock, flags);
815
more:
816
        space = TTY_FLIPBUF_SIZE - tty->flip.count;
817
        if (!space) {
818
                do_queue++;
819
                goto out;
820
        }
821
        residue = 0;
822
        if (info->rx_ubuf == info->rx_cbuf) {
823
                if ((ld_le32(&rd->status) & ACTIVE) != 0) {
824
                        dbdma_flush(rd);
825
                        if (in_le32(&rd->cmdptr)
826
                            == virt_to_bus(info->rx_cmds[info->rx_cbuf]+1))
827
                                residue = in_le16(&info->rx->res_count);
828
                }
829
        }
830
        available = RX_BUF_SIZE - residue - info->rx_done_bytes;
831
        if (available > space)
832
                available = space;
833
        if (available) {
834
                memcpy(tty->flip.char_buf_ptr,
835
                       info->rx_char_buf[info->rx_ubuf] + info->rx_done_bytes,
836
                       available);
837
                memcpy(tty->flip.flag_buf_ptr,
838
                       info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
839
                       available);
840
                tty->flip.char_buf_ptr += available;
841
                tty->flip.count += available;
842
                tty->flip.flag_buf_ptr += available;
843
                memset(info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes,
844
                       0, available);
845
                info->rx_done_bytes += available;
846
                do_queue++;
847
        }
848
        if (info->rx_done_bytes == RX_BUF_SIZE) {
849
                volatile struct dbdma_cmd *cd = info->rx_cmds[info->rx_ubuf];
850
 
851
                if (info->rx_ubuf == info->rx_cbuf)
852
                        goto out;
853
                /* mark rx_char_buf[rx_ubuf] free */
854
                st_le16(&cd->command, DBDMA_NOP);
855
                cd++;
856
                st_le32(&cd->cmd_dep, 0);
857
                st_le32((unsigned int *)&cd->res_count, 0);
858
                cd++;
859
                st_le16(&cd->xfer_status, 0);
860
 
861
                if (info->rx_fbuf == RX_NO_FBUF) {
862
                        info->rx_fbuf = info->rx_ubuf;
863
                        if (!(ld_le32(&rd->status) & ACTIVE)) {
864
                                dbdma_reset(&info->rx->dma);
865
                                rxdma_start(info, info->rx_ubuf);
866
                                info->rx_cbuf = info->rx_ubuf;
867
                        }
868
                }
869
                info->rx_done_bytes = 0;
870
                if (++info->rx_ubuf == info->rx_nbuf)
871
                        info->rx_ubuf = 0;
872
                if (info->rx_fbuf == info->rx_ubuf)
873
                        info->rx_fbuf = RX_NO_FBUF;
874
                goto more;
875
        }
876
out:
877
        spin_unlock_irqrestore(&info->rx_dma_lock, flags);
878
        if (do_queue)
879
                queue_task(&tty->flip.tqueue, &tq_timer);
880
}
881
 
882
static void poll_rxdma(unsigned long private_)
883
{
884
        struct mac_serial       *info = (struct mac_serial *) private_;
885
        unsigned long flags;
886
 
887
        rxdma_to_tty(info);
888
        spin_lock_irqsave(&info->rx_dma_lock, flags);
889
        mod_timer(&info->poll_dma_timer, RX_DMA_TIMER);
890
        spin_unlock_irqrestore(&info->rx_dma_lock, flags);
891
}
892
 
893
static void dma_init(struct mac_serial * info)
894
{
895
        int i, size;
896
        volatile struct dbdma_cmd *cd;
897
        unsigned char *p;
898
 
899
        info->rx_nbuf = 8;
900
 
901
        /* various mem set up */
902
        size = sizeof(struct dbdma_cmd) * (3 * info->rx_nbuf + 2)
903
                + (RX_BUF_SIZE * 2 + sizeof(*info->rx_cmds)
904
                   + sizeof(*info->rx_char_buf) + sizeof(*info->rx_flag_buf))
905
                * info->rx_nbuf;
906
        info->dma_priv = kmalloc(size, GFP_KERNEL | GFP_DMA);
907
        if (info->dma_priv == NULL)
908
                return;
909
        memset(info->dma_priv, 0, size);
910
 
911
        info->rx_cmds = (volatile struct dbdma_cmd **)info->dma_priv;
912
        info->rx_char_buf = (unsigned char **) (info->rx_cmds + info->rx_nbuf);
913
        info->rx_flag_buf = info->rx_char_buf + info->rx_nbuf;
914
        p = (unsigned char *) (info->rx_flag_buf + info->rx_nbuf);
915
        for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
916
                info->rx_char_buf[i] = p;
917
        for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE)
918
                info->rx_flag_buf[i] = p;
919
 
920
        /* a bit of DMA programming */
921
        cd = info->rx_cmds[0] = (volatile struct dbdma_cmd *) DBDMA_ALIGN(p);
922
        st_le16(&cd->command, DBDMA_NOP);
923
        cd++;
924
        st_le16(&cd->req_count, RX_BUF_SIZE);
925
        st_le16(&cd->command, INPUT_MORE);
926
        st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[0]));
927
        cd++;
928
        st_le16(&cd->req_count, 4);
929
        st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
930
        st_le32(&cd->phy_addr, virt_to_bus(cd-2));
931
        st_le32(&cd->cmd_dep, DBDMA_STOP);
932
        for (i = 1; i < info->rx_nbuf; i++) {
933
                info->rx_cmds[i] = ++cd;
934
                st_le16(&cd->command, DBDMA_NOP);
935
                cd++;
936
                st_le16(&cd->req_count, RX_BUF_SIZE);
937
                st_le16(&cd->command, INPUT_MORE);
938
                st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[i]));
939
                cd++;
940
                st_le16(&cd->req_count, 4);
941
                st_le16(&cd->command, STORE_WORD | INTR_ALWAYS);
942
                st_le32(&cd->phy_addr, virt_to_bus(cd-2));
943
                st_le32(&cd->cmd_dep, DBDMA_STOP);
944
        }
945
        cd++;
946
        st_le16(&cd->command, DBDMA_NOP | BR_ALWAYS);
947
        st_le32(&cd->cmd_dep, virt_to_bus(info->rx_cmds[0]));
948
 
949
        /* setup DMA to our liking */
950
        dbdma_reset(&info->rx->dma);
951
        st_le32(&info->rx->dma.intr_sel, 0x10001);
952
        st_le32(&info->rx->dma.br_sel, 0x10001);
953
        out_le32(&info->rx->dma.wait_sel, 0x10001);
954
 
955
        /* set various flags */
956
        info->rx_ubuf = 0;
957
        info->rx_cbuf = 0;
958
        info->rx_fbuf = info->rx_ubuf + 1;
959
        if (info->rx_fbuf == info->rx_nbuf)
960
                info->rx_fbuf = RX_NO_FBUF;
961
        info->rx_done_bytes = 0;
962
 
963
        /* setup polling */
964
        init_timer(&info->poll_dma_timer);
965
        info->poll_dma_timer.function = (void *)&poll_rxdma;
966
        info->poll_dma_timer.data = (unsigned long)info;
967
 
968
        info->dma_initted = 1;
969
}
970
 
971
/*
972
 * FixZeroBug....Works around a bug in the SCC receving channel.
973
 * Taken from Darwin code, 15 Sept. 2000  -DanM
974
 *
975
 * The following sequence prevents a problem that is seen with O'Hare ASICs
976
 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
977
 * at the input to the receiver becomes 'stuck' and locks up the receiver.
978
 * This problem can occur as a result of a zero bit at the receiver input
979
 * coincident with any of the following events:
980
 *
981
 *      The SCC is initialized (hardware or software).
982
 *      A framing error is detected.
983
 *      The clocking option changes from synchronous or X1 asynchronous
984
 *              clocking to X16, X32, or X64 asynchronous clocking.
985
 *      The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
986
 *
987
 * This workaround attempts to recover from the lockup condition by placing
988
 * the SCC in synchronous loopback mode with a fast clock before programming
989
 * any of the asynchronous modes.
990
 */
991
static void fix_zero_bug_scc(struct mac_serial * info)
992
{
993
        write_zsreg(info->zs_channel, 9,
994
                    (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
995
        udelay(10);
996
        write_zsreg(info->zs_channel, 9,
997
                    ((info->zs_channel == info->zs_chan_a? CHRA: CHRB) | NV));
998
 
999
        write_zsreg(info->zs_channel, 4, (X1CLK | EXTSYNC));
1000
 
1001
        /* I think this is wrong....but, I just copying code....
1002
        */
1003
        write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
1004
 
1005
        write_zsreg(info->zs_channel, 5, (8 & ~TxENAB));
1006
        write_zsreg(info->zs_channel, 9, NV);   /* Didn't we already do this? */
1007
        write_zsreg(info->zs_channel, 11, (RCBR | TCBR));
1008
        write_zsreg(info->zs_channel, 12, 0);
1009
        write_zsreg(info->zs_channel, 13, 0);
1010
        write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR));
1011
        write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR | BRENABL));
1012
        write_zsreg(info->zs_channel, 3, (8 | RxENABLE));
1013
        write_zsreg(info->zs_channel, 0, RES_EXT_INT);
1014
        write_zsreg(info->zs_channel, 0, RES_EXT_INT);   /* to kill some time */
1015
 
1016
        /* The channel should be OK now, but it is probably receiving
1017
         * loopback garbage.
1018
         * Switch to asynchronous mode, disable the receiver,
1019
         * and discard everything in the receive buffer.
1020
         */
1021
        write_zsreg(info->zs_channel, 9, NV);
1022
        write_zsreg(info->zs_channel, 4, PAR_ENA);
1023
        write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE));
1024
 
1025
        while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV) {
1026
                (void)read_zsreg(info->zs_channel, 8);
1027
                write_zsreg(info->zs_channel, 0, RES_EXT_INT);
1028
                write_zsreg(info->zs_channel, 0, ERR_RES);
1029
        }
1030
}
1031
 
1032
static int setup_scc(struct mac_serial * info)
1033
{
1034
        unsigned long flags;
1035
 
1036
        OPNDBG("setting up ttyS%d SCC...\n", info->line);
1037
 
1038
        save_flags(flags); cli(); /* Disable interrupts */
1039
 
1040
        /* Nice buggy HW ... */
1041
        fix_zero_bug_scc(info);
1042
 
1043
        /*
1044
         * Reset the chip.
1045
         */
1046
        write_zsreg(info->zs_channel, 9,
1047
                    (info->zs_channel == info->zs_chan_a? CHRA: CHRB));
1048
        udelay(10);
1049
        write_zsreg(info->zs_channel, 9, 0);
1050
 
1051
        /*
1052
         * Clear the receive FIFO.
1053
         */
1054
        ZS_CLEARFIFO(info->zs_channel);
1055
        info->xmit_fifo_size = 1;
1056
 
1057
        /*
1058
         * Reset DMAs
1059
         */
1060
        if (info->has_dma)
1061
                dma_init(info);
1062
 
1063
        /*
1064
         * Clear the interrupt registers.
1065
         */
1066
        write_zsreg(info->zs_channel, 0, ERR_RES);
1067
        write_zsreg(info->zs_channel, 0, RES_H_IUS);
1068
 
1069
        /*
1070
         * Turn on RTS and DTR.
1071
         */
1072
        if (!info->is_irda)
1073
                zs_rtsdtr(info, 1);
1074
 
1075
        /*
1076
         * Finally, enable sequencing and interrupts
1077
         */
1078
        if (!info->dma_initted) {
1079
                /* interrupt on ext/status changes, all received chars,
1080
                   transmit ready */
1081
                info->curregs[1] = (info->curregs[1] & ~0x18)
1082
                                | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
1083
        } else {
1084
                /* interrupt on ext/status changes, W/Req pin is
1085
                   receive DMA request */
1086
                info->curregs[1] = (info->curregs[1] & ~(0x18 | TxINT_ENAB))
1087
                                | (EXT_INT_ENAB | WT_RDY_RT | WT_FN_RDYFN);
1088
                write_zsreg(info->zs_channel, 1, info->curregs[1]);
1089
                /* enable W/Req pin */
1090
                info->curregs[1] |= WT_RDY_ENAB;
1091
                write_zsreg(info->zs_channel, 1, info->curregs[1]);
1092
                /* enable interrupts on transmit ready and receive errors */
1093
                info->curregs[1] |= INT_ERR_Rx | TxINT_ENAB;
1094
        }
1095
        info->pendregs[1] = info->curregs[1];
1096
        info->curregs[3] |= (RxENABLE | Rx8);
1097
        info->pendregs[3] = info->curregs[3];
1098
        info->curregs[5] |= (TxENAB | Tx8);
1099
        info->pendregs[5] = info->curregs[5];
1100
        info->curregs[9] |= (NV | MIE);
1101
        info->pendregs[9] = info->curregs[9];
1102
        write_zsreg(info->zs_channel, 3, info->curregs[3]);
1103
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1104
        write_zsreg(info->zs_channel, 9, info->curregs[9]);
1105
 
1106
        if (info->tty)
1107
                clear_bit(TTY_IO_ERROR, &info->tty->flags);
1108
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1109
 
1110
        /*
1111
         * Set the speed of the serial port
1112
         */
1113
        change_speed(info, 0);
1114
 
1115
        /* Save the current value of RR0 */
1116
        info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1117
 
1118
        restore_flags(flags);
1119
 
1120
        if (info->dma_initted) {
1121
                spin_lock_irqsave(&info->rx_dma_lock, flags);
1122
                rxdma_start(info, 0);
1123
                info->poll_dma_timer.expires = RX_DMA_TIMER;
1124
                add_timer(&info->poll_dma_timer);
1125
                spin_unlock_irqrestore(&info->rx_dma_lock, flags);
1126
        }
1127
 
1128
        return 0;
1129
}
1130
 
1131
/*
1132
 * This routine will shutdown a serial port; interrupts are disabled, and
1133
 * DTR is dropped if the hangup on close termio flag is on.
1134
 */
1135
static void shutdown(struct mac_serial * info)
1136
{
1137
        OPNDBG("Shutting down serial port %d (irq %d)....\n", info->line,
1138
               info->irq);
1139
 
1140
        if (!(info->flags & ZILOG_INITIALIZED)) {
1141
                OPNDBG("(already shutdown)\n");
1142
                return;
1143
        }
1144
 
1145
        if (info->has_dma) {
1146
                del_timer(&info->poll_dma_timer);
1147
                dbdma_reset(info->tx_dma);
1148
                dbdma_reset(&info->rx->dma);
1149
                disable_irq(info->tx_dma_irq);
1150
                disable_irq(info->rx_dma_irq);
1151
        }
1152
        disable_irq(info->irq);
1153
 
1154
        info->pendregs[1] = info->curregs[1] = 0;
1155
        write_zsreg(info->zs_channel, 1, 0);     /* no interrupts */
1156
 
1157
        info->curregs[3] &= ~RxENABLE;
1158
        info->pendregs[3] = info->curregs[3];
1159
        write_zsreg(info->zs_channel, 3, info->curregs[3]);
1160
 
1161
        info->curregs[5] &= ~TxENAB;
1162
        if (!info->tty || C_HUPCL(info->tty))
1163
                info->curregs[5] &= ~DTR;
1164
        info->pendregs[5] = info->curregs[5];
1165
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1166
 
1167
        if (info->tty)
1168
                set_bit(TTY_IO_ERROR, &info->tty->flags);
1169
 
1170
        set_scc_power(info, 0);
1171
 
1172
        if (info->xmit_buf) {
1173
                free_page((unsigned long) info->xmit_buf);
1174
                info->xmit_buf = 0;
1175
        }
1176
 
1177
        if (info->has_dma && info->dma_priv) {
1178
                kfree(info->dma_priv);
1179
                info->dma_priv = NULL;
1180
                info->dma_initted = 0;
1181
        }
1182
 
1183
        memset(info->curregs, 0, sizeof(info->curregs));
1184
        memset(info->pendregs, 0, sizeof(info->pendregs));
1185
 
1186
        info->flags &= ~ZILOG_INITIALIZED;
1187
}
1188
 
1189
/*
1190
 * Turn power on or off to the SCC and associated stuff
1191
 * (port drivers, modem, IR port, etc.)
1192
 * Returns the number of milliseconds we should wait before
1193
 * trying to use the port.
1194
 */
1195
static int set_scc_power(struct mac_serial * info, int state)
1196
{
1197
        int delay = 0;
1198
 
1199
        if (state) {
1200
                PWRDBG("ttyS%d: powering up hardware\n", info->line);
1201
                pmac_call_feature(
1202
                        PMAC_FTR_SCC_ENABLE,
1203
                        info->dev_node, info->port_type, 1);
1204
                if (info->is_internal_modem) {
1205
                        pmac_call_feature(
1206
                                PMAC_FTR_MODEM_ENABLE,
1207
                                info->dev_node, 0, 1);
1208
                        delay = 2500;   /* wait for 2.5s before using */
1209
                } else if (info->is_irda)
1210
                        mdelay(50);     /* Do better here once the problems
1211
                                         * with blocking have been ironed out
1212
                                         */
1213
        } else {
1214
                /* TODO: Make that depend on a timer, don't power down
1215
                 * immediately
1216
                 */
1217
                PWRDBG("ttyS%d: shutting down hardware\n", info->line);
1218
                if (info->is_internal_modem) {
1219
                        PWRDBG("ttyS%d: shutting down modem\n", info->line);
1220
                        pmac_call_feature(
1221
                                PMAC_FTR_MODEM_ENABLE,
1222
                                info->dev_node, 0, 0);
1223
                }
1224
                pmac_call_feature(
1225
                        PMAC_FTR_SCC_ENABLE,
1226
                        info->dev_node, info->port_type, 0);
1227
        }
1228
        return delay;
1229
}
1230
 
1231
static void irda_rts_pulses(struct mac_serial *info, int w)
1232
{
1233
        unsigned long flags;
1234
 
1235
        udelay(w);
1236
        save_flags(flags); cli();
1237
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1238
        udelay(2);
1239
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1240
        udelay(8);
1241
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB);
1242
        udelay(4);
1243
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1244
        restore_flags(flags);
1245
}
1246
 
1247
/*
1248
 * Set the irda codec on the imac to the specified baud rate.
1249
 */
1250
static void irda_setup(struct mac_serial *info)
1251
{
1252
        int code, speed, t;
1253
        unsigned long flags;
1254
 
1255
        speed = info->tty->termios->c_cflag & CBAUD;
1256
        if (speed < B2400 || speed > B115200)
1257
                return;
1258
        code = 0x4d + B115200 - speed;
1259
 
1260
        /* disable serial interrupts and receive DMA */
1261
        write_zsreg(info->zs_channel, 1, info->curregs[1] & ~0x9f);
1262
 
1263
        /* wait for transmitter to drain */
1264
        t = 10000;
1265
        while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0
1266
               || (read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1267
                if (--t <= 0) {
1268
                        printk(KERN_ERR "transmitter didn't drain\n");
1269
                        return;
1270
                }
1271
                udelay(10);
1272
        }
1273
        udelay(100);
1274
 
1275
        /* set to 8 bits, no parity, 19200 baud, RTS on, DTR off */
1276
        write_zsreg(info->zs_channel, 4, X16CLK | SB1);
1277
        write_zsreg(info->zs_channel, 11, TCBR | RCBR);
1278
        t = BPS_TO_BRG(19200, ZS_CLOCK/16);
1279
        write_zsreg(info->zs_channel, 12, t);
1280
        write_zsreg(info->zs_channel, 13, t >> 8);
1281
        write_zsreg(info->zs_channel, 14, BRENABL);
1282
        write_zsreg(info->zs_channel, 3, Rx8 | RxENABLE);
1283
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1284
 
1285
        /* set TxD low for ~104us and pulse RTS */
1286
        udelay(1000);
1287
        save_flags(flags); cli();
1288
        write_zsdata(info->zs_channel, 0xfe);
1289
        irda_rts_pulses(info, 150);
1290
        restore_flags(flags);
1291
        irda_rts_pulses(info, 180);
1292
        irda_rts_pulses(info, 50);
1293
        udelay(100);
1294
 
1295
        /* assert DTR, wait 30ms, talk to the chip */
1296
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS | DTR);
1297
        mdelay(30);
1298
        while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV)
1299
                read_zsdata(info->zs_channel);
1300
 
1301
        write_zsdata(info->zs_channel, 1);
1302
        t = 1000;
1303
        while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1304
                if (--t <= 0) {
1305
                        printk(KERN_ERR "irda_setup timed out on 1st byte\n");
1306
                        goto out;
1307
                }
1308
                udelay(10);
1309
        }
1310
        t = read_zsdata(info->zs_channel);
1311
        if (t != 4)
1312
                printk(KERN_ERR "irda_setup 1st byte = %x\n", t);
1313
 
1314
        write_zsdata(info->zs_channel, code);
1315
        t = 1000;
1316
        while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) {
1317
                if (--t <= 0) {
1318
                        printk(KERN_ERR "irda_setup timed out on 2nd byte\n");
1319
                        goto out;
1320
                }
1321
                udelay(10);
1322
        }
1323
        t = read_zsdata(info->zs_channel);
1324
        if (t != code)
1325
                printk(KERN_ERR "irda_setup 2nd byte = %x (%x)\n", t, code);
1326
 
1327
        /* Drop DTR again and do some more RTS pulses */
1328
 out:
1329
        udelay(100);
1330
        write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS);
1331
        irda_rts_pulses(info, 80);
1332
 
1333
        /* We should be right to go now.  We assume that load_zsregs
1334
           will get called soon to load up the correct baud rate etc. */
1335
        info->curregs[5] = (info->curregs[5] | RTS) & ~DTR;
1336
        info->pendregs[5] = info->curregs[5];
1337
}
1338
 
1339
/*
1340
 * This routine is called to set the UART divisor registers to match
1341
 * the specified baud rate for a serial port.
1342
 */
1343
static void change_speed(struct mac_serial *info, struct termios *old_termios)
1344
{
1345
        unsigned cflag;
1346
        int     bits;
1347
        int     brg, baud;
1348
        unsigned long flags;
1349
 
1350
        if (!info->tty || !info->tty->termios)
1351
                return;
1352
 
1353
        cflag = info->tty->termios->c_cflag;
1354
        baud = tty_get_baud_rate(info->tty);
1355
        if (baud == 0) {
1356
                if (old_termios) {
1357
                        info->tty->termios->c_cflag &= ~CBAUD;
1358
                        info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1359
                        cflag = info->tty->termios->c_cflag;
1360
                        baud = tty_get_baud_rate(info->tty);
1361
                }
1362
                else
1363
                        baud = info->zs_baud;
1364
        }
1365
        if (baud > 230400)
1366
                baud = 230400;
1367
        else if (baud == 0)
1368
                baud = 38400;
1369
 
1370
        save_flags(flags); cli();
1371
        info->zs_baud = baud;
1372
        info->clk_divisor = 16;
1373
 
1374
        BAUDBG(KERN_DEBUG "set speed to %d bds, ", baud);
1375
 
1376
        switch (baud) {
1377
        case ZS_CLOCK/16:       /* 230400 */
1378
                info->curregs[4] = X16CLK;
1379
                info->curregs[11] = 0;
1380
                break;
1381
        case ZS_CLOCK/32:       /* 115200 */
1382
                info->curregs[4] = X32CLK;
1383
                info->curregs[11] = 0;
1384
                break;
1385
        default:
1386
                info->curregs[4] = X16CLK;
1387
                info->curregs[11] = TCBR | RCBR;
1388
                brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor);
1389
                info->curregs[12] = (brg & 255);
1390
                info->curregs[13] = ((brg >> 8) & 255);
1391
                info->curregs[14] = BRENABL;
1392
        }
1393
 
1394
        /* byte size and parity */
1395
        info->curregs[3] &= ~RxNBITS_MASK;
1396
        info->curregs[5] &= ~TxNBITS_MASK;
1397
        switch (cflag & CSIZE) {
1398
        case CS5:
1399
                info->curregs[3] |= Rx5;
1400
                info->curregs[5] |= Tx5;
1401
                BAUDBG("5 bits, ");
1402
                bits = 7;
1403
                break;
1404
        case CS6:
1405
                info->curregs[3] |= Rx6;
1406
                info->curregs[5] |= Tx6;
1407
                BAUDBG("6 bits, ");
1408
                bits = 8;
1409
                break;
1410
        case CS7:
1411
                info->curregs[3] |= Rx7;
1412
                info->curregs[5] |= Tx7;
1413
                BAUDBG("7 bits, ");
1414
                bits = 9;
1415
                break;
1416
        case CS8:
1417
        default: /* defaults to 8 bits */
1418
                info->curregs[3] |= Rx8;
1419
                info->curregs[5] |= Tx8;
1420
                BAUDBG("8 bits, ");
1421
                bits = 10;
1422
                break;
1423
        }
1424
        info->pendregs[3] = info->curregs[3];
1425
        info->pendregs[5] = info->curregs[5];
1426
 
1427
        info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
1428
        if (cflag & CSTOPB) {
1429
                info->curregs[4] |= SB2;
1430
                bits++;
1431
                BAUDBG("2 stop, ");
1432
        } else {
1433
                info->curregs[4] |= SB1;
1434
                BAUDBG("1 stop, ");
1435
        }
1436
        if (cflag & PARENB) {
1437
                bits++;
1438
                info->curregs[4] |= PAR_ENA;
1439
                BAUDBG("parity, ");
1440
        }
1441
        if (!(cflag & PARODD)) {
1442
                info->curregs[4] |= PAR_EVEN;
1443
        }
1444
        info->pendregs[4] = info->curregs[4];
1445
 
1446
        if (!(cflag & CLOCAL)) {
1447
                if (!(info->curregs[15] & DCDIE))
1448
                        info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1449
                info->curregs[15] |= DCDIE;
1450
        } else
1451
                info->curregs[15] &= ~DCDIE;
1452
        if (cflag & CRTSCTS) {
1453
                info->curregs[15] |= CTSIE;
1454
                if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
1455
                        info->tx_stopped = 1;
1456
        } else {
1457
                info->curregs[15] &= ~CTSIE;
1458
                info->tx_stopped = 0;
1459
        }
1460
        info->pendregs[15] = info->curregs[15];
1461
 
1462
        /* Calc timeout value. This is pretty broken with high baud rates with HZ=100.
1463
           This code would love a larger HZ and a >1 fifo size, but this is not
1464
           a priority. The resulting value must be >HZ/2
1465
         */
1466
        info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud);
1467
        info->timeout += HZ/50+1;       /* Add .02 seconds of slop */
1468
 
1469
        BAUDBG("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ,
1470
               (int)info->baud_base);
1471
 
1472
        /* set the irda codec to the right rate */
1473
        if (info->is_irda)
1474
                irda_setup(info);
1475
 
1476
        /* Load up the new values */
1477
        load_zsregs(info->zs_channel, info->curregs);
1478
 
1479
        restore_flags(flags);
1480
}
1481
 
1482
static void rs_flush_chars(struct tty_struct *tty)
1483
{
1484
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1485
 
1486
        if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1487
                return;
1488
 
1489
        if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
1490
            !info->xmit_buf)
1491
                return;
1492
 
1493
        /* Enable transmitter */
1494
        transmit_chars(info);
1495
}
1496
 
1497
static int rs_write(struct tty_struct * tty, int from_user,
1498
                    const unsigned char *buf, int count)
1499
{
1500
        int     c, ret = 0;
1501
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1502
        unsigned long flags;
1503
 
1504
        if (serial_paranoia_check(info, tty->device, "rs_write"))
1505
                return 0;
1506
 
1507
        if (!tty || !info->xmit_buf || !tmp_buf)
1508
                return 0;
1509
 
1510
        if (from_user) {
1511
                down(&tmp_buf_sem);
1512
                while (1) {
1513
                        c = MIN(count,
1514
                                MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1515
                                    SERIAL_XMIT_SIZE - info->xmit_head));
1516
                        if (c <= 0)
1517
                                break;
1518
 
1519
                        c -= copy_from_user(tmp_buf, buf, c);
1520
                        if (!c) {
1521
                                if (!ret)
1522
                                        ret = -EFAULT;
1523
                                break;
1524
                        }
1525
                        save_flags(flags);
1526
                        cli();
1527
                        c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1528
                                       SERIAL_XMIT_SIZE - info->xmit_head));
1529
                        memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1530
                        info->xmit_head = ((info->xmit_head + c) &
1531
                                           (SERIAL_XMIT_SIZE-1));
1532
                        info->xmit_cnt += c;
1533
                        restore_flags(flags);
1534
                        buf += c;
1535
                        count -= c;
1536
                        ret += c;
1537
                }
1538
                up(&tmp_buf_sem);
1539
        } else {
1540
                while (1) {
1541
                        save_flags(flags);
1542
                        cli();
1543
                        c = MIN(count,
1544
                                MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1545
                                    SERIAL_XMIT_SIZE - info->xmit_head));
1546
                        if (c <= 0) {
1547
                                restore_flags(flags);
1548
                                break;
1549
                        }
1550
                        memcpy(info->xmit_buf + info->xmit_head, buf, c);
1551
                        info->xmit_head = ((info->xmit_head + c) &
1552
                                           (SERIAL_XMIT_SIZE-1));
1553
                        info->xmit_cnt += c;
1554
                        restore_flags(flags);
1555
                        buf += c;
1556
                        count -= c;
1557
                        ret += c;
1558
                }
1559
        }
1560
        if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
1561
            && !info->tx_active)
1562
                transmit_chars(info);
1563
        return ret;
1564
}
1565
 
1566
static int rs_write_room(struct tty_struct *tty)
1567
{
1568
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1569
        int     ret;
1570
 
1571
        if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1572
                return 0;
1573
        ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1574
        if (ret < 0)
1575
                ret = 0;
1576
        return ret;
1577
}
1578
 
1579
static int rs_chars_in_buffer(struct tty_struct *tty)
1580
{
1581
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1582
 
1583
        if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1584
                return 0;
1585
        return info->xmit_cnt;
1586
}
1587
 
1588
static void rs_flush_buffer(struct tty_struct *tty)
1589
{
1590
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1591
        unsigned long flags;
1592
 
1593
        if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1594
                return;
1595
        save_flags(flags); cli();
1596
        info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1597
        restore_flags(flags);
1598
        wake_up_interruptible(&tty->write_wait);
1599
        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1600
            tty->ldisc.write_wakeup)
1601
                (tty->ldisc.write_wakeup)(tty);
1602
}
1603
 
1604
/*
1605
 * ------------------------------------------------------------
1606
 * rs_throttle()
1607
 *
1608
 * This routine is called by the upper-layer tty layer to signal that
1609
 * incoming characters should be throttled.
1610
 * ------------------------------------------------------------
1611
 */
1612
static void rs_throttle(struct tty_struct * tty)
1613
{
1614
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1615
        unsigned long flags;
1616
#ifdef SERIAL_DEBUG_THROTTLE
1617
        printk(KERN_DEBUG "throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));
1618
#endif
1619
 
1620
        if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1621
                return;
1622
 
1623
        if (I_IXOFF(tty)) {
1624
                save_flags(flags); cli();
1625
                info->x_char = STOP_CHAR(tty);
1626
                if (!info->tx_active)
1627
                        transmit_chars(info);
1628
                restore_flags(flags);
1629
        }
1630
 
1631
        if (C_CRTSCTS(tty)) {
1632
                /*
1633
                 * Here we want to turn off the RTS line.  On Macintoshes,
1634
                 * the external serial ports using a DIN-8 or DIN-9
1635
                 * connector only have the DTR line (which is usually
1636
                 * wired to both RTS and DTR on an external modem in
1637
                 * the cable).  RTS doesn't go out to the serial port
1638
                 * socket, it acts as an output enable for the transmit
1639
                 * data line.  So in this case we don't drop RTS.
1640
                 *
1641
                 * Macs with internal modems generally do have both RTS
1642
                 * and DTR wired to the modem, so in that case we do
1643
                 * drop RTS.
1644
                 */
1645
                if (info->is_internal_modem) {
1646
                        save_flags(flags); cli();
1647
                        info->curregs[5] &= ~RTS;
1648
                        info->pendregs[5] &= ~RTS;
1649
                        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1650
                        restore_flags(flags);
1651
                }
1652
        }
1653
 
1654
#ifdef CDTRCTS
1655
        if (tty->termios->c_cflag & CDTRCTS) {
1656
                save_flags(flags); cli();
1657
                info->curregs[5] &= ~DTR;
1658
                info->pendregs[5] &= ~DTR;
1659
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
1660
                restore_flags(flags);
1661
        }
1662
#endif /* CDTRCTS */
1663
}
1664
 
1665
static void rs_unthrottle(struct tty_struct * tty)
1666
{
1667
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1668
        unsigned long flags;
1669
#ifdef SERIAL_DEBUG_THROTTLE
1670
        printk(KERN_DEBUG "unthrottle %s: %d....\n",
1671
                        tty->ldisc.chars_in_buffer(tty));
1672
#endif
1673
 
1674
        if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1675
                return;
1676
 
1677
        if (I_IXOFF(tty)) {
1678
                save_flags(flags); cli();
1679
                if (info->x_char)
1680
                        info->x_char = 0;
1681
                else {
1682
                        info->x_char = START_CHAR(tty);
1683
                        if (!info->tx_active)
1684
                                transmit_chars(info);
1685
                }
1686
                restore_flags(flags);
1687
        }
1688
 
1689
        if (C_CRTSCTS(tty) && info->is_internal_modem) {
1690
                /* Assert RTS line */
1691
                save_flags(flags); cli();
1692
                info->curregs[5] |= RTS;
1693
                info->pendregs[5] |= RTS;
1694
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
1695
                restore_flags(flags);
1696
        }
1697
 
1698
#ifdef CDTRCTS
1699
        if (tty->termios->c_cflag & CDTRCTS) {
1700
                /* Assert DTR line */
1701
                save_flags(flags); cli();
1702
                info->curregs[5] |= DTR;
1703
                info->pendregs[5] |= DTR;
1704
                write_zsreg(info->zs_channel, 5, info->curregs[5]);
1705
                restore_flags(flags);
1706
        }
1707
#endif
1708
}
1709
 
1710
/*
1711
 * ------------------------------------------------------------
1712
 * rs_ioctl() and friends
1713
 * ------------------------------------------------------------
1714
 */
1715
 
1716
static int get_serial_info(struct mac_serial * info,
1717
                           struct serial_struct * retinfo)
1718
{
1719
        struct serial_struct tmp;
1720
 
1721
        if (!retinfo)
1722
                return -EFAULT;
1723
        memset(&tmp, 0, sizeof(tmp));
1724
        tmp.type = info->type;
1725
        tmp.line = info->line;
1726
        tmp.port = info->port;
1727
        tmp.irq = info->irq;
1728
        tmp.flags = info->flags;
1729
        tmp.baud_base = info->baud_base;
1730
        tmp.close_delay = info->close_delay;
1731
        tmp.closing_wait = info->closing_wait;
1732
        tmp.custom_divisor = info->custom_divisor;
1733
        if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1734
                return -EFAULT;
1735
        return 0;
1736
}
1737
 
1738
static int set_serial_info(struct mac_serial * info,
1739
                           struct serial_struct * new_info)
1740
{
1741
        struct serial_struct new_serial;
1742
        struct mac_serial old_info;
1743
        int                     retval = 0;
1744
 
1745
        if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1746
                return -EFAULT;
1747
        old_info = *info;
1748
 
1749
        if (!capable(CAP_SYS_ADMIN)) {
1750
                if ((new_serial.baud_base != info->baud_base) ||
1751
                    (new_serial.type != info->type) ||
1752
                    (new_serial.close_delay != info->close_delay) ||
1753
                    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1754
                     (info->flags & ~ZILOG_USR_MASK)))
1755
                        return -EPERM;
1756
                info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1757
                               (new_serial.flags & ZILOG_USR_MASK));
1758
                info->custom_divisor = new_serial.custom_divisor;
1759
                goto check_and_exit;
1760
        }
1761
 
1762
        if (info->count > 1)
1763
                return -EBUSY;
1764
 
1765
        /*
1766
         * OK, past this point, all the error checking has been done.
1767
         * At this point, we start making changes.....
1768
         */
1769
 
1770
        info->baud_base = new_serial.baud_base;
1771
        info->flags = ((info->flags & ~ZILOG_FLAGS) |
1772
                        (new_serial.flags & ZILOG_FLAGS));
1773
        info->type = new_serial.type;
1774
        info->close_delay = new_serial.close_delay;
1775
        info->closing_wait = new_serial.closing_wait;
1776
 
1777
check_and_exit:
1778
        if (info->flags & ZILOG_INITIALIZED)
1779
                retval = setup_scc(info);
1780
        return retval;
1781
}
1782
 
1783
/*
1784
 * get_lsr_info - get line status register info
1785
 *
1786
 * Purpose: Let user call ioctl() to get info when the UART physically
1787
 *          is emptied.  On bus types like RS485, the transmitter must
1788
 *          release the bus after transmitting. This must be done when
1789
 *          the transmit shift register is empty, not be done when the
1790
 *          transmit holding register is empty.  This functionality
1791
 *          allows an RS485 driver to be written in user space.
1792
 */
1793
static int get_lsr_info(struct mac_serial * info, unsigned int *value)
1794
{
1795
        unsigned char status;
1796
        unsigned long flags;
1797
 
1798
        save_flags(flags); cli();
1799
        status = read_zsreg(info->zs_channel, 0);
1800
        restore_flags(flags);
1801
        status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0;
1802
        return put_user(status,value);
1803
}
1804
 
1805
static int get_modem_info(struct mac_serial *info, unsigned int *value)
1806
{
1807
        unsigned char control, status;
1808
        unsigned int result;
1809
        unsigned long flags;
1810
 
1811
        save_flags(flags); cli();
1812
        control = info->curregs[5];
1813
        status = read_zsreg(info->zs_channel, 0);
1814
        restore_flags(flags);
1815
        result =  ((control & RTS) ? TIOCM_RTS: 0)
1816
                | ((control & DTR) ? TIOCM_DTR: 0)
1817
                | ((status  & DCD) ? TIOCM_CAR: 0)
1818
                | ((status  & CTS) ? 0: TIOCM_CTS);
1819
        return put_user(result,value);
1820
}
1821
 
1822
static int set_modem_info(struct mac_serial *info, unsigned int cmd,
1823
                          unsigned int *value)
1824
{
1825
        unsigned int arg, bits;
1826
        unsigned long flags;
1827
 
1828
        if (get_user(arg, value))
1829
                return -EFAULT;
1830
        bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1831
        save_flags(flags); cli();
1832
        switch (cmd) {
1833
        case TIOCMBIS:
1834
                info->curregs[5] |= bits;
1835
                break;
1836
        case TIOCMBIC:
1837
                info->curregs[5] &= ~bits;
1838
                break;
1839
        case TIOCMSET:
1840
                info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits;
1841
                break;
1842
        default:
1843
                restore_flags(flags);
1844
                return -EINVAL;
1845
        }
1846
        info->pendregs[5] = info->curregs[5];
1847
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1848
        restore_flags(flags);
1849
        return 0;
1850
}
1851
 
1852
/*
1853
 * rs_break - turn transmit break condition on/off
1854
 */
1855
static void rs_break(struct tty_struct *tty, int break_state)
1856
{
1857
        struct mac_serial *info = (struct mac_serial *) tty->driver_data;
1858
        unsigned long flags;
1859
 
1860
        if (serial_paranoia_check(info, tty->device, "rs_break"))
1861
                return;
1862
 
1863
        save_flags(flags); cli();
1864
        if (break_state == -1)
1865
                info->curregs[5] |= SND_BRK;
1866
        else
1867
                info->curregs[5] &= ~SND_BRK;
1868
        write_zsreg(info->zs_channel, 5, info->curregs[5]);
1869
        restore_flags(flags);
1870
}
1871
 
1872
static int rs_ioctl(struct tty_struct *tty, struct file * file,
1873
                    unsigned int cmd, unsigned long arg)
1874
{
1875
        struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1876
 
1877
#ifdef CONFIG_KGDB
1878
        if (info->kgdb_channel)
1879
                return -ENODEV;
1880
#endif
1881
        if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1882
                return -ENODEV;
1883
 
1884
        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1885
            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) {
1886
                if (tty->flags & (1 << TTY_IO_ERROR))
1887
                    return -EIO;
1888
        }
1889
 
1890
        switch (cmd) {
1891
                case TIOCMGET:
1892
                        return get_modem_info(info, (unsigned int *) arg);
1893
                case TIOCMBIS:
1894
                case TIOCMBIC:
1895
                case TIOCMSET:
1896
                        return set_modem_info(info, cmd, (unsigned int *) arg);
1897
                case TIOCGSERIAL:
1898
                        return get_serial_info(info,
1899
                                               (struct serial_struct *) arg);
1900
                case TIOCSSERIAL:
1901
                        return set_serial_info(info,
1902
                                               (struct serial_struct *) arg);
1903
                case TIOCSERGETLSR: /* Get line status register */
1904
                        return get_lsr_info(info, (unsigned int *) arg);
1905
 
1906
                case TIOCSERGSTRUCT:
1907
                        if (copy_to_user((struct mac_serial *) arg,
1908
                                         info, sizeof(struct mac_serial)))
1909
                                return -EFAULT;
1910
                        return 0;
1911
 
1912
                default:
1913
                        return -ENOIOCTLCMD;
1914
                }
1915
        return 0;
1916
}
1917
 
1918
static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1919
{
1920
        struct mac_serial *info = (struct mac_serial *)tty->driver_data;
1921
        int was_stopped;
1922
 
1923
        if (tty->termios->c_cflag == old_termios->c_cflag)
1924
                return;
1925
        was_stopped = info->tx_stopped;
1926
 
1927
        change_speed(info, old_termios);
1928
 
1929
        if (was_stopped && !info->tx_stopped) {
1930
                tty->hw_stopped = 0;
1931
                rs_start(tty);
1932
        }
1933
}
1934
 
1935
/*
1936
 * ------------------------------------------------------------
1937
 * rs_close()
1938
 *
1939
 * This routine is called when the serial port gets closed.
1940
 * Wait for the last remaining data to be sent.
1941
 * ------------------------------------------------------------
1942
 */
1943
static void rs_close(struct tty_struct *tty, struct file * filp)
1944
{
1945
        struct mac_serial * info = (struct mac_serial *)tty->driver_data;
1946
        unsigned long flags;
1947
 
1948
        if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1949
                return;
1950
 
1951
        save_flags(flags); cli();
1952
 
1953
        if (tty_hung_up_p(filp)) {
1954
                MOD_DEC_USE_COUNT;
1955
                restore_flags(flags);
1956
                return;
1957
        }
1958
 
1959
        OPNDBG("rs_close ttyS%d, count = %d\n", info->line, info->count);
1960
        if ((tty->count == 1) && (info->count != 1)) {
1961
                /*
1962
                 * Uh, oh.  tty->count is 1, which means that the tty
1963
                 * structure will be freed.  Info->count should always
1964
                 * be one in these conditions.  If it's greater than
1965
                 * one, we've got real problems, since it means the
1966
                 * serial port won't be shutdown.
1967
                 */
1968
                printk(KERN_ERR "rs_close: bad serial port count; tty->count "
1969
                                "is 1, info->count is %d\n", info->count);
1970
                info->count = 1;
1971
        }
1972
        if (--info->count < 0) {
1973
                printk(KERN_ERR "rs_close: bad serial port count for "
1974
                                "ttyS%d: %d\n", info->line, info->count);
1975
                info->count = 0;
1976
        }
1977
        if (info->count) {
1978
                MOD_DEC_USE_COUNT;
1979
                restore_flags(flags);
1980
                return;
1981
        }
1982
        info->flags |= ZILOG_CLOSING;
1983
        /*
1984
         * Save the termios structure, since this port may have
1985
         * separate termios for callout and dialin.
1986
         */
1987
        if (info->flags & ZILOG_NORMAL_ACTIVE)
1988
                info->normal_termios = *tty->termios;
1989
        if (info->flags & ZILOG_CALLOUT_ACTIVE)
1990
                info->callout_termios = *tty->termios;
1991
        /*
1992
         * Now we wait for the transmit buffer to clear; and we notify
1993
         * the line discipline to only process XON/XOFF characters.
1994
         */
1995
        OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait);
1996
        tty->closing = 1;
1997
        if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) {
1998
                restore_flags(flags);
1999
                tty_wait_until_sent(tty, info->closing_wait);
2000
                save_flags(flags); cli();
2001
        }
2002
 
2003
        /*
2004
         * At this point we stop accepting input.  To do this, we
2005
         * disable the receiver and receive interrupts.
2006
         */
2007
        info->curregs[3] &= ~RxENABLE;
2008
        info->pendregs[3] = info->curregs[3];
2009
        write_zsreg(info->zs_channel, 3, info->curregs[3]);
2010
        info->curregs[1] &= ~(0x18);    /* disable any rx ints */
2011
        info->pendregs[1] = info->curregs[1];
2012
        write_zsreg(info->zs_channel, 1, info->curregs[1]);
2013
        ZS_CLEARFIFO(info->zs_channel);
2014
        if (info->flags & ZILOG_INITIALIZED) {
2015
                /*
2016
                 * Before we drop DTR, make sure the SCC transmitter
2017
                 * has completely drained.
2018
                 */
2019
                OPNDBG("waiting end of Rx...\n");
2020
                restore_flags(flags);
2021
                rs_wait_until_sent(tty, info->timeout);
2022
                save_flags(flags); cli();
2023
        }
2024
 
2025
        shutdown(info);
2026
        /* restore flags now since shutdown() will have disabled this port's
2027
           specific irqs */
2028
        restore_flags(flags);
2029
 
2030
        if (tty->driver.flush_buffer)
2031
                tty->driver.flush_buffer(tty);
2032
        if (tty->ldisc.flush_buffer)
2033
                tty->ldisc.flush_buffer(tty);
2034
        tty->closing = 0;
2035
        info->event = 0;
2036
        info->tty = 0;
2037
 
2038
        if (info->blocked_open) {
2039
                if (info->close_delay) {
2040
                        current->state = TASK_INTERRUPTIBLE;
2041
                        schedule_timeout(info->close_delay);
2042
                }
2043
                wake_up_interruptible(&info->open_wait);
2044
        }
2045
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
2046
                         ZILOG_CLOSING);
2047
        wake_up_interruptible(&info->close_wait);
2048
        MOD_DEC_USE_COUNT;
2049
}
2050
 
2051
/*
2052
 * rs_wait_until_sent() --- wait until the transmitter is empty
2053
 */
2054
static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2055
{
2056
        struct mac_serial *info = (struct mac_serial *) tty->driver_data;
2057
        unsigned long orig_jiffies, char_time;
2058
 
2059
        if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2060
                return;
2061
 
2062
/*      printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n",
2063
                        timeout, tty->stopped, info->tx_stopped);
2064
*/
2065
        orig_jiffies = jiffies;
2066
        /*
2067
         * Set the check interval to be 1/5 of the estimated time to
2068
         * send a single character, and make it at least 1.  The check
2069
         * interval should also be less than the timeout.
2070
         */
2071
        if (info->timeout <= HZ/50) {
2072
                printk(KERN_INFO "macserial: invalid info->timeout=%d\n",
2073
                                    info->timeout);
2074
                info->timeout = HZ/50+1;
2075
        }
2076
 
2077
        char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2078
        char_time = char_time / 5;
2079
        if (char_time > HZ) {
2080
                printk(KERN_WARNING "macserial: char_time %ld >HZ !!!\n",
2081
                                    char_time);
2082
                char_time = 1;
2083
        } else if (char_time == 0)
2084
                char_time = 1;
2085
        if (timeout)
2086
                char_time = MIN(char_time, timeout);
2087
        while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
2088
                current->state = TASK_INTERRUPTIBLE;
2089
                schedule_timeout(char_time);
2090
                if (signal_pending(current))
2091
                        break;
2092
                if (timeout && time_after(jiffies, orig_jiffies + timeout))
2093
                        break;
2094
        }
2095
        current->state = TASK_RUNNING;
2096
}
2097
 
2098
/*
2099
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2100
 */
2101
static void rs_hangup(struct tty_struct *tty)
2102
{
2103
        struct mac_serial * info = (struct mac_serial *)tty->driver_data;
2104
 
2105
        if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2106
                return;
2107
 
2108
        rs_flush_buffer(tty);
2109
        shutdown(info);
2110
        info->event = 0;
2111
        info->count = 0;
2112
        info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
2113
        info->tty = 0;
2114
        wake_up_interruptible(&info->open_wait);
2115
}
2116
 
2117
/*
2118
 * ------------------------------------------------------------
2119
 * rs_open() and friends
2120
 * ------------------------------------------------------------
2121
 */
2122
static int block_til_ready(struct tty_struct *tty, struct file * filp,
2123
                           struct mac_serial *info)
2124
{
2125
        DECLARE_WAITQUEUE(wait,current);
2126
        int             retval;
2127
        int             do_clocal = 0;
2128
 
2129
        /*
2130
         * If the device is in the middle of being closed, then block
2131
         * until it's done, and then try again.
2132
         */
2133
        if (info->flags & ZILOG_CLOSING) {
2134
                interruptible_sleep_on(&info->close_wait);
2135
#ifdef SERIAL_DO_RESTART
2136
                return ((info->flags & ZILOG_HUP_NOTIFY) ?
2137
                        -EAGAIN : -ERESTARTSYS);
2138
#else
2139
                return -EAGAIN;
2140
#endif
2141
        }
2142
 
2143
        /*
2144
         * If this is a callout device, then just make sure the normal
2145
         * device isn't being used.
2146
         */
2147
        if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2148
                if (info->flags & ZILOG_NORMAL_ACTIVE)
2149
                        return -EBUSY;
2150
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
2151
                    (info->flags & ZILOG_SESSION_LOCKOUT) &&
2152
                    (info->session != current->session))
2153
                    return -EBUSY;
2154
                if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
2155
                    (info->flags & ZILOG_PGRP_LOCKOUT) &&
2156
                    (info->pgrp != current->pgrp))
2157
                    return -EBUSY;
2158
                info->flags |= ZILOG_CALLOUT_ACTIVE;
2159
                return 0;
2160
        }
2161
 
2162
        /*
2163
         * If non-blocking mode is set, or the port is not enabled,
2164
         * then make the check up front and then exit.
2165
         */
2166
        if ((filp->f_flags & O_NONBLOCK) ||
2167
            (tty->flags & (1 << TTY_IO_ERROR))) {
2168
                if (info->flags & ZILOG_CALLOUT_ACTIVE)
2169
                        return -EBUSY;
2170
                info->flags |= ZILOG_NORMAL_ACTIVE;
2171
                return 0;
2172
        }
2173
 
2174
        if (info->flags & ZILOG_CALLOUT_ACTIVE) {
2175
                if (info->normal_termios.c_cflag & CLOCAL)
2176
                        do_clocal = 1;
2177
        } else {
2178
                if (tty->termios->c_cflag & CLOCAL)
2179
                        do_clocal = 1;
2180
        }
2181
 
2182
        /*
2183
         * Block waiting for the carrier detect and the line to become
2184
         * free (i.e., not in use by the callout).  While we are in
2185
         * this loop, info->count is dropped by one, so that
2186
         * rs_close() knows when to free things.  We restore it upon
2187
         * exit, either normal or abnormal.
2188
         */
2189
        retval = 0;
2190
        add_wait_queue(&info->open_wait, &wait);
2191
        OPNDBG("block_til_ready before block: ttyS%d, count = %d\n",
2192
               info->line, info->count);
2193
        cli();
2194
        if (!tty_hung_up_p(filp))
2195
                info->count--;
2196
        sti();
2197
        info->blocked_open++;
2198
        while (1) {
2199
                cli();
2200
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
2201
                    (tty->termios->c_cflag & CBAUD) &&
2202
                    !info->is_irda)
2203
                        zs_rtsdtr(info, 1);
2204
                sti();
2205
                set_current_state(TASK_INTERRUPTIBLE);
2206
                if (tty_hung_up_p(filp) ||
2207
                    !(info->flags & ZILOG_INITIALIZED)) {
2208
#ifdef SERIAL_DO_RESTART
2209
                        if (info->flags & ZILOG_HUP_NOTIFY)
2210
                                retval = -EAGAIN;
2211
                        else
2212
                                retval = -ERESTARTSYS;
2213
#else
2214
                        retval = -EAGAIN;
2215
#endif
2216
                        break;
2217
                }
2218
                if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
2219
                    !(info->flags & ZILOG_CLOSING) &&
2220
                    (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
2221
                        break;
2222
                if (signal_pending(current)) {
2223
                        retval = -ERESTARTSYS;
2224
                        break;
2225
                }
2226
                OPNDBG("block_til_ready blocking: ttyS%d, count = %d\n",
2227
                       info->line, info->count);
2228
                schedule();
2229
        }
2230
        current->state = TASK_RUNNING;
2231
        remove_wait_queue(&info->open_wait, &wait);
2232
        if (!tty_hung_up_p(filp))
2233
                info->count++;
2234
        info->blocked_open--;
2235
        OPNDBG("block_til_ready after blocking: ttyS%d, count = %d\n",
2236
               info->line, info->count);
2237
        if (retval)
2238
                return retval;
2239
        info->flags |= ZILOG_NORMAL_ACTIVE;
2240
        return 0;
2241
}
2242
 
2243
/*
2244
 * This routine is called whenever a serial port is opened.  It
2245
 * enables interrupts for a serial port, linking in its ZILOG structure into
2246
 * the IRQ chain.   It also performs the serial-specific
2247
 * initialization for the tty structure.
2248
 */
2249
static int rs_open(struct tty_struct *tty, struct file * filp)
2250
{
2251
        struct mac_serial       *info;
2252
        int                     retval, line;
2253
        unsigned long           page;
2254
 
2255
        MOD_INC_USE_COUNT;
2256
        line = MINOR(tty->device) - tty->driver.minor_start;
2257
        if ((line < 0) || (line >= zs_channels_found)) {
2258
                MOD_DEC_USE_COUNT;
2259
                return -ENODEV;
2260
        }
2261
        info = zs_soft + line;
2262
 
2263
#ifdef CONFIG_KGDB
2264
        if (info->kgdb_channel) {
2265
                MOD_DEC_USE_COUNT;
2266
                return -ENODEV;
2267
        }
2268
#endif
2269
        if (serial_paranoia_check(info, tty->device, "rs_open"))
2270
                return -ENODEV;
2271
        OPNDBG("rs_open %s%d, count = %d, tty=%p\n", tty->driver.name,
2272
               info->line, info->count, tty);
2273
 
2274
        info->count++;
2275
        tty->driver_data = info;
2276
        info->tty = tty;
2277
 
2278
        if (!tmp_buf) {
2279
                page = get_free_page(GFP_KERNEL);
2280
                if (!page)
2281
                        return -ENOMEM;
2282
                if (tmp_buf)
2283
                        free_page(page);
2284
                else
2285
                        tmp_buf = (unsigned char *) page;
2286
        }
2287
 
2288
        /*
2289
         * If the port is the middle of closing, bail out now
2290
         */
2291
        if (tty_hung_up_p(filp) ||
2292
            (info->flags & ZILOG_CLOSING)) {
2293
                if (info->flags & ZILOG_CLOSING)
2294
                        interruptible_sleep_on(&info->close_wait);
2295
#ifdef SERIAL_DO_RESTART
2296
                return ((info->flags & ZILOG_HUP_NOTIFY) ?
2297
                        -EAGAIN : -ERESTARTSYS);
2298
#else
2299
                return -EAGAIN;
2300
#endif
2301
        }
2302
 
2303
        /*
2304
         * Start up serial port
2305
         */
2306
 
2307
        retval = startup(info);
2308
        if (retval)
2309
                return retval;
2310
 
2311
        retval = block_til_ready(tty, filp, info);
2312
        if (retval) {
2313
                OPNDBG("rs_open returning after block_til_ready with %d\n",
2314
                        retval);
2315
                return retval;
2316
        }
2317
 
2318
        if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
2319
                if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2320
                        *tty->termios = info->normal_termios;
2321
                else
2322
                        *tty->termios = info->callout_termios;
2323
                change_speed(info, 0);
2324
        }
2325
#ifdef CONFIG_SERIAL_CONSOLE
2326
        if (sercons.cflag && sercons.index == line) {
2327
                tty->termios->c_cflag = sercons.cflag;
2328
                sercons.cflag = 0;
2329
                change_speed(info, 0);
2330
        }
2331
#endif
2332
 
2333
        info->session = current->session;
2334
        info->pgrp = current->pgrp;
2335
 
2336
        OPNDBG("rs_open ttyS%d successful...\n", info->line);
2337
        return 0;
2338
}
2339
 
2340
/* Finally, routines used to initialize the serial driver. */
2341
 
2342
static void show_serial_version(void)
2343
{
2344
        printk(KERN_INFO "PowerMac Z8530 serial driver version " MACSERIAL_VERSION "\n");
2345
}
2346
 
2347
/*
2348
 * Initialize one channel, both the mac_serial and mac_zschannel
2349
 * structs.  We use the dev_node field of the mac_serial struct.
2350
 */
2351
static int
2352
chan_init(struct mac_serial *zss, struct mac_zschannel *zs_chan,
2353
          struct mac_zschannel *zs_chan_a)
2354
{
2355
        struct device_node *ch = zss->dev_node;
2356
        char *conn;
2357
        int len;
2358
        struct slot_names_prop {
2359
                int     count;
2360
                char    name[1];
2361
        } *slots;
2362
 
2363
        zss->irq = ch->intrs[0].line;
2364
        zss->has_dma = 0;
2365
#if !defined(CONFIG_KGDB) && defined(SUPPORT_SERIAL_DMA)
2366
        if (ch->n_addrs >= 3 && ch->n_intrs == 3)
2367
                zss->has_dma = 1;
2368
#endif
2369
        zss->dma_initted = 0;
2370
 
2371
        zs_chan->control = (volatile unsigned char *)
2372
                ioremap(ch->addrs[0].address, 0x1000);
2373
        zs_chan->data = zs_chan->control + 0x10;
2374
        spin_lock_init(&zs_chan->lock);
2375
        zs_chan->parent = zss;
2376
        zss->zs_channel = zs_chan;
2377
        zss->zs_chan_a = zs_chan_a;
2378
 
2379
        /* setup misc varariables */
2380
        zss->kgdb_channel = 0;
2381
 
2382
        /* For now, we assume you either have a slot-names property
2383
         * with "Modem" in it, or your channel is compatible with
2384
         * "cobalt". Might need additional fixups
2385
         */
2386
        zss->is_internal_modem = device_is_compatible(ch, "cobalt");
2387
        conn = get_property(ch, "AAPL,connector", &len);
2388
        zss->is_irda = conn && (strcmp(conn, "infrared") == 0);
2389
        zss->port_type = PMAC_SCC_ASYNC;
2390
        /* 1999 Powerbook G3 has slot-names property instead */
2391
        slots = (struct slot_names_prop *)get_property(ch, "slot-names", &len);
2392
        if (slots && slots->count > 0) {
2393
                if (strcmp(slots->name, "IrDA") == 0)
2394
                        zss->is_irda = 1;
2395
                else if (strcmp(slots->name, "Modem") == 0)
2396
                        zss->is_internal_modem = 1;
2397
        }
2398
        if (zss->is_irda)
2399
                zss->port_type = PMAC_SCC_IRDA;
2400
        if (zss->is_internal_modem) {
2401
                struct device_node* i2c_modem = find_devices("i2c-modem");
2402
                if (i2c_modem) {
2403
                        char* mid = get_property(i2c_modem, "modem-id", NULL);
2404
                        if (mid) switch(*mid) {
2405
                        case 0x04 :
2406
                        case 0x05 :
2407
                        case 0x07 :
2408
                        case 0x08 :
2409
                        case 0x0b :
2410
                        case 0x0c :
2411
                                zss->port_type = PMAC_SCC_I2S1;
2412
                        }
2413
                        printk(KERN_INFO "macserial: i2c-modem detected, id: %d\n",
2414
                                mid ? (*mid) : 0);
2415
                } else {
2416
                        printk(KERN_INFO "macserial: serial modem detected\n");
2417
                }
2418
        }
2419
 
2420
        while (zss->has_dma) {
2421
                zss->dma_priv = NULL;
2422
                /* it seems that the last two addresses are the
2423
                   DMA controllers */
2424
                zss->tx_dma = (volatile struct dbdma_regs *)
2425
                        ioremap(ch->addrs[ch->n_addrs - 2].address, 0x100);
2426
                zss->rx = (volatile struct mac_dma *)
2427
                        ioremap(ch->addrs[ch->n_addrs - 1].address, 0x100);
2428
                zss->tx_dma_irq = ch->intrs[1].line;
2429
                zss->rx_dma_irq = ch->intrs[2].line;
2430
                spin_lock_init(&zss->rx_dma_lock);
2431
                break;
2432
        }
2433
 
2434
        init_timer(&zss->powerup_timer);
2435
        zss->powerup_timer.function = powerup_done;
2436
        zss->powerup_timer.data = (unsigned long) zss;
2437
        return 0;
2438
}
2439
 
2440
/*
2441
 * /proc fs routines. TODO: Add status lines & error stats
2442
 */
2443
static inline int
2444
line_info(char *buf, struct mac_serial *info)
2445
{
2446
        int             ret=0;
2447
        unsigned char* connector;
2448
        int lenp;
2449
 
2450
        ret += sprintf(buf, "%d: port:0x%X irq:%d", info->line, info->port, info->irq);
2451
 
2452
        connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2453
        if (connector)
2454
                ret+=sprintf(buf+ret," con:%s ", connector);
2455
        if (info->is_internal_modem) {
2456
                if (!connector)
2457
                        ret+=sprintf(buf+ret," con:");
2458
                ret+=sprintf(buf+ret,"%s", " (internal modem)");
2459
        }
2460
        if (info->is_irda) {
2461
                if (!connector)
2462
                        ret+=sprintf(buf+ret," con:");
2463
                ret+=sprintf(buf+ret,"%s", " (IrDA)");
2464
        }
2465
        ret+=sprintf(buf+ret,"\n");
2466
 
2467
        return ret;
2468
}
2469
 
2470
int macserial_read_proc(char *page, char **start, off_t off, int count,
2471
                 int *eof, void *data)
2472
{
2473
        int l, len = 0;
2474
        off_t   begin = 0;
2475
        struct mac_serial *info;
2476
 
2477
        len += sprintf(page, "serinfo:1.0 driver:" MACSERIAL_VERSION "\n");
2478
        for (info = zs_chain; info && len < 4000; info = info->zs_next) {
2479
                l = line_info(page + len, info);
2480
                len += l;
2481
                if (len+begin > off+count)
2482
                        goto done;
2483
                if (len+begin < off) {
2484
                        begin += len;
2485
                        len = 0;
2486
                }
2487
        }
2488
        *eof = 1;
2489
done:
2490
        if (off >= len+begin)
2491
                return 0;
2492
        *start = page + (off-begin);
2493
        return ((count < begin+len-off) ? count : begin+len-off);
2494
}
2495
 
2496
/* Ask the PROM how many Z8530s we have and initialize their zs_channels */
2497
static void
2498
probe_sccs()
2499
{
2500
        struct device_node *dev, *ch;
2501
        struct mac_serial **pp;
2502
        int n, chip, nchan;
2503
        struct mac_zschannel *zs_chan;
2504
        int chan_a_index;
2505
 
2506
        n = 0;
2507
        pp = &zs_chain;
2508
        zs_chan = zs_channels;
2509
        for (dev = find_devices("escc"); dev != 0; dev = dev->next) {
2510
                nchan = 0;
2511
                chip = n;
2512
                if (n >= NUM_CHANNELS) {
2513
                        printk(KERN_WARNING "Sorry, can't use %s: no more "
2514
                                            "channels\n", dev->full_name);
2515
                        continue;
2516
                }
2517
                chan_a_index = 0;
2518
                for (ch = dev->child; ch != 0; ch = ch->sibling) {
2519
                        if (nchan >= 2) {
2520
                                printk(KERN_WARNING "SCC: Only 2 channels per "
2521
                                        "chip are supported\n");
2522
                                break;
2523
                        }
2524
                        if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) {
2525
                                printk("Can't use %s: %d addrs %d intrs\n",
2526
                                      ch->full_name, ch->n_addrs, ch->n_intrs);
2527
                                continue;
2528
                        }
2529
 
2530
                        /* The channel with the higher address
2531
                           will be the A side. */
2532
                        if (nchan > 0 &&
2533
                            ch->addrs[0].address
2534
                            > zs_soft[n-1].dev_node->addrs[0].address)
2535
                                chan_a_index = 1;
2536
 
2537
                        /* minimal initialization for now */
2538
                        zs_soft[n].dev_node = ch;
2539
                        *pp = &zs_soft[n];
2540
                        pp = &zs_soft[n].zs_next;
2541
                        ++nchan;
2542
                        ++n;
2543
                }
2544
                if (nchan == 0)
2545
                        continue;
2546
 
2547
                /* set up A side */
2548
                if (chan_init(&zs_soft[chip + chan_a_index], zs_chan, zs_chan))
2549
                        continue;
2550
                ++zs_chan;
2551
 
2552
                /* set up B side, if it exists */
2553
                if (nchan > 1)
2554
                        if (chan_init(&zs_soft[chip + 1 - chan_a_index],
2555
                                  zs_chan, zs_chan - 1))
2556
                                continue;
2557
                ++zs_chan;
2558
        }
2559
        *pp = 0;
2560
 
2561
        zs_channels_found = n;
2562
#ifdef CONFIG_PMAC_PBOOK
2563
        if (n)
2564
                pmu_register_sleep_notifier(&serial_sleep_notifier);
2565
#endif /* CONFIG_PMAC_PBOOK */
2566
}
2567
 
2568
/* rs_init inits the driver */
2569
int macserial_init(void)
2570
{
2571
        int channel, i;
2572
        unsigned long flags;
2573
        struct mac_serial *info;
2574
 
2575
        /* Setup base handler, and timer table. */
2576
        init_bh(MACSERIAL_BH, do_serial_bh);
2577
 
2578
        /* Find out how many Z8530 SCCs we have */
2579
        if (zs_chain == 0)
2580
                probe_sccs();
2581
 
2582
        /* XXX assume it's a powerbook if we have a via-pmu
2583
         *
2584
         * This is OK for core99 machines as well.
2585
         */
2586
        is_powerbook = find_devices("via-pmu") != 0;
2587
 
2588
        /* Register the interrupt handler for each one
2589
         * We also request the OF resources here as probe_sccs()
2590
         * might be called too early for that
2591
         */
2592
        save_flags(flags); cli();
2593
        for (i = 0; i < zs_channels_found; ++i) {
2594
                struct device_node* ch = zs_soft[i].dev_node;
2595
                if (!request_OF_resource(ch, 0, NULL)) {
2596
                        printk(KERN_ERR "macserial: can't request IO resource !\n");
2597
                        return -ENODEV;
2598
                }
2599
                if (zs_soft[i].has_dma) {
2600
                        if (!request_OF_resource(ch, ch->n_addrs - 2, " (tx dma)")) {
2601
                                printk(KERN_ERR "macserial: can't request TX DMA resource !\n");
2602
                                zs_soft[i].has_dma = 0;
2603
                                goto no_dma;
2604
                        }
2605
                        if (!request_OF_resource(ch, ch->n_addrs - 1, " (rx dma)")) {
2606
                                release_OF_resource(ch, ch->n_addrs - 2);
2607
                                printk(KERN_ERR "macserial: can't request RX DMA resource !\n");
2608
                                zs_soft[i].has_dma = 0;
2609
                                goto no_dma;
2610
                        }
2611
                        if (request_irq(zs_soft[i].tx_dma_irq, rs_txdma_irq, 0,
2612
                                        "SCC-txdma", &zs_soft[i]))
2613
                                printk(KERN_ERR "macserial: can't get irq %d\n",
2614
                                       zs_soft[i].tx_dma_irq);
2615
                        disable_irq(zs_soft[i].tx_dma_irq);
2616
                        if (request_irq(zs_soft[i].rx_dma_irq, rs_rxdma_irq, 0,
2617
                                        "SCC-rxdma", &zs_soft[i]))
2618
                                printk(KERN_ERR "macserial: can't get irq %d\n",
2619
                                       zs_soft[i].rx_dma_irq);
2620
                        disable_irq(zs_soft[i].rx_dma_irq);
2621
                }
2622
no_dma:
2623
                if (request_irq(zs_soft[i].irq, rs_interrupt, 0,
2624
                                "SCC", &zs_soft[i]))
2625
                        printk(KERN_ERR "macserial: can't get irq %d\n",
2626
                               zs_soft[i].irq);
2627
                disable_irq(zs_soft[i].irq);
2628
        }
2629
        restore_flags(flags);
2630
 
2631
        show_serial_version();
2632
 
2633
        /* Initialize the tty_driver structure */
2634
        /* Not all of this is exactly right for us. */
2635
 
2636
        memset(&serial_driver, 0, sizeof(struct tty_driver));
2637
        serial_driver.magic = TTY_DRIVER_MAGIC;
2638
        serial_driver.driver_name = "macserial";
2639
#ifdef CONFIG_DEVFS_FS
2640
        serial_driver.name = "tts/%d";
2641
#else
2642
        serial_driver.name = "ttyS";
2643
#endif /* CONFIG_DEVFS_FS */
2644
        serial_driver.major = TTY_MAJOR;
2645
        serial_driver.minor_start = 64;
2646
        serial_driver.num = zs_channels_found;
2647
        serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2648
        serial_driver.subtype = SERIAL_TYPE_NORMAL;
2649
        serial_driver.init_termios = tty_std_termios;
2650
 
2651
        serial_driver.init_termios.c_cflag =
2652
                B38400 | CS8 | CREAD | HUPCL | CLOCAL;
2653
        serial_driver.flags = TTY_DRIVER_REAL_RAW;
2654
        serial_driver.refcount = &serial_refcount;
2655
        serial_driver.table = serial_table;
2656
        serial_driver.termios = serial_termios;
2657
        serial_driver.termios_locked = serial_termios_locked;
2658
 
2659
        serial_driver.open = rs_open;
2660
        serial_driver.close = rs_close;
2661
        serial_driver.write = rs_write;
2662
        serial_driver.flush_chars = rs_flush_chars;
2663
        serial_driver.write_room = rs_write_room;
2664
        serial_driver.chars_in_buffer = rs_chars_in_buffer;
2665
        serial_driver.flush_buffer = rs_flush_buffer;
2666
        serial_driver.ioctl = rs_ioctl;
2667
        serial_driver.throttle = rs_throttle;
2668
        serial_driver.unthrottle = rs_unthrottle;
2669
        serial_driver.set_termios = rs_set_termios;
2670
        serial_driver.stop = rs_stop;
2671
        serial_driver.start = rs_start;
2672
        serial_driver.hangup = rs_hangup;
2673
        serial_driver.break_ctl = rs_break;
2674
        serial_driver.wait_until_sent = rs_wait_until_sent;
2675
        serial_driver.read_proc = macserial_read_proc;
2676
 
2677
        /*
2678
         * The callout device is just like normal device except for
2679
         * major number and the subtype code.
2680
         */
2681
        callout_driver = serial_driver;
2682
#ifdef CONFIG_DEVFS_FS
2683
        callout_driver.name = "cua/%d";
2684
#else
2685
        callout_driver.name = "cua";
2686
#endif /* CONFIG_DEVFS_FS */
2687
        callout_driver.major = TTYAUX_MAJOR;
2688
        callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2689
        callout_driver.read_proc = 0;
2690
        callout_driver.proc_entry = 0;
2691
 
2692
        if (tty_register_driver(&serial_driver))
2693
                panic("Couldn't register serial driver\n");
2694
        if (tty_register_driver(&callout_driver))
2695
                panic("Couldn't register callout driver\n");
2696
 
2697
        for (channel = 0; channel < zs_channels_found; ++channel) {
2698
#ifdef CONFIG_KGDB
2699
                if (zs_soft[channel].kgdb_channel) {
2700
                        kgdb_interruptible(1);
2701
                        continue;
2702
                }
2703
#endif
2704
                zs_soft[channel].clk_divisor = 16;
2705
/* -- we are not sure the SCC is powered ON at this point
2706
                zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2707
*/
2708
                zs_soft[channel].zs_baud = 38400;
2709
 
2710
                /* If console serial line, then enable interrupts. */
2711
                if (zs_soft[channel].is_cons) {
2712
                        printk(KERN_INFO "macserial: console line, enabling "
2713
                                        "interrupt %d\n", zs_soft[channel].irq);
2714
                        panic("macserial: console not supported yet !");
2715
                        write_zsreg(zs_soft[channel].zs_channel, R1,
2716
                                    (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
2717
                        write_zsreg(zs_soft[channel].zs_channel, R9,
2718
                                    (NV | MIE));
2719
                }
2720
        }
2721
 
2722
        for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2723
        {
2724
                unsigned char* connector;
2725
                int lenp;
2726
 
2727
#ifdef CONFIG_KGDB
2728
                if (info->kgdb_channel) {
2729
                        continue;
2730
                }
2731
#endif
2732
                info->magic = SERIAL_MAGIC;
2733
                info->port = (int) info->zs_channel->control;
2734
                info->line = i;
2735
                info->tty = 0;
2736
                info->custom_divisor = 16;
2737
                info->timeout = 0;
2738
                info->close_delay = 50;
2739
                info->closing_wait = 3000;
2740
                info->x_char = 0;
2741
                info->event = 0;
2742
                info->count = 0;
2743
                info->blocked_open = 0;
2744
                info->tqueue.routine = do_softint;
2745
                info->tqueue.data = info;
2746
                info->callout_termios =callout_driver.init_termios;
2747
                info->normal_termios = serial_driver.init_termios;
2748
                init_waitqueue_head(&info->open_wait);
2749
                init_waitqueue_head(&info->close_wait);
2750
                info->timeout = HZ;
2751
                printk(KERN_INFO "tty%02d at 0x%08x (irq = %d)", info->line,
2752
                        info->port, info->irq);
2753
                printk(" is a Z8530 ESCC");
2754
                connector = get_property(info->dev_node, "AAPL,connector", &lenp);
2755
                if (connector)
2756
                        printk(", port = %s", connector);
2757
                if (info->is_internal_modem)
2758
                        printk(" (internal modem)");
2759
                if (info->is_irda)
2760
                        printk(" (IrDA)");
2761
                printk("\n");
2762
        }
2763
        tmp_buf = 0;
2764
 
2765
        return 0;
2766
}
2767
 
2768
void macserial_cleanup(void)
2769
{
2770
        int i;
2771
        unsigned long flags;
2772
        struct mac_serial *info;
2773
 
2774
        for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
2775
                set_scc_power(info, 0);
2776
        save_flags(flags); cli();
2777
        for (i = 0; i < zs_channels_found; ++i) {
2778
                free_irq(zs_soft[i].irq, &zs_soft[i]);
2779
                if (zs_soft[i].has_dma) {
2780
                        free_irq(zs_soft[i].tx_dma_irq, &zs_soft[i]);
2781
                        free_irq(zs_soft[i].rx_dma_irq, &zs_soft[i]);
2782
                }
2783
                release_OF_resource(zs_soft[i].dev_node, 0);
2784
                if (zs_soft[i].has_dma) {
2785
                        struct device_node* ch = zs_soft[i].dev_node;
2786
                        release_OF_resource(ch, ch->n_addrs - 2);
2787
                        release_OF_resource(ch, ch->n_addrs - 1);
2788
                }
2789
        }
2790
        restore_flags(flags);
2791
        tty_unregister_driver(&callout_driver);
2792
        tty_unregister_driver(&serial_driver);
2793
 
2794
        if (tmp_buf) {
2795
                free_page((unsigned long) tmp_buf);
2796
                tmp_buf = 0;
2797
        }
2798
 
2799
#ifdef CONFIG_PMAC_PBOOK
2800
        if (zs_channels_found)
2801
                pmu_unregister_sleep_notifier(&serial_sleep_notifier);
2802
#endif /* CONFIG_PMAC_PBOOK */
2803
}
2804
 
2805
module_init(macserial_init);
2806
module_exit(macserial_cleanup);
2807
MODULE_LICENSE("GPL");
2808
EXPORT_NO_SYMBOLS;
2809
 
2810
#if 0
2811
/*
2812
 * register_serial and unregister_serial allows for serial ports to be
2813
 * configured at run-time, to support PCMCIA modems.
2814
 */
2815
/* PowerMac: Unused at this time, just here to make things link. */
2816
int register_serial(struct serial_struct *req)
2817
{
2818
        return -1;
2819
}
2820
 
2821
void unregister_serial(int line)
2822
{
2823
        return;
2824
}
2825
#endif
2826
 
2827
/*
2828
 * ------------------------------------------------------------
2829
 * Serial console driver
2830
 * ------------------------------------------------------------
2831
 */
2832
#ifdef CONFIG_SERIAL_CONSOLE
2833
 
2834
/*
2835
 *      Print a string to the serial port trying not to disturb
2836
 *      any possible real use of the port...
2837
 */
2838
static void serial_console_write(struct console *co, const char *s,
2839
                                 unsigned count)
2840
{
2841
        struct mac_serial *info = zs_soft + co->index;
2842
        int i;
2843
 
2844
        /* Turn of interrupts and enable the transmitter. */
2845
        write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB);
2846
        write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR);
2847
 
2848
        for (i=0; i<count; i++) {
2849
                /* Wait for the transmit buffer to empty. */
2850
                while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) {
2851
                        eieio();
2852
                }
2853
 
2854
                write_zsdata(info->zs_channel, s[i]);
2855
                if (s[i] == 10) {
2856
                        while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP)
2857
                                == 0)
2858
                                eieio();
2859
 
2860
                        write_zsdata(info->zs_channel, 13);
2861
                }
2862
        }
2863
 
2864
        /* Restore the values in the registers. */
2865
        write_zsreg(info->zs_channel, R1, info->curregs[1]);
2866
        /* Don't disable the transmitter. */
2867
}
2868
 
2869
static kdev_t serial_console_device(struct console *c)
2870
{
2871
        return MKDEV(TTY_MAJOR, 64 + c->index);
2872
}
2873
 
2874
/*
2875
 *      Setup initial baud/bits/parity. We do two things here:
2876
 *      - construct a cflag setting for the first rs_open()
2877
 *      - initialize the serial port
2878
 *      Return non-zero if we didn't find a serial port.
2879
 */
2880
static int __init serial_console_setup(struct console *co, char *options)
2881
{
2882
        struct mac_serial *info;
2883
        int     baud = 38400;
2884
        int     bits = 8;
2885
        int     parity = 'n';
2886
        int     cflag = CREAD | HUPCL | CLOCAL;
2887
        int     brg;
2888
        char    *s;
2889
        long    flags;
2890
 
2891
        /* Find out how many Z8530 SCCs we have */
2892
        if (zs_chain == 0)
2893
                probe_sccs();
2894
 
2895
        if (zs_chain == 0)
2896
                return -1;
2897
 
2898
        /* Do we have the device asked for? */
2899
        if (co->index >= zs_channels_found)
2900
                return -1;
2901
        info = zs_soft + co->index;
2902
 
2903
        set_scc_power(info, 1);
2904
 
2905
        /* Reset the channel */
2906
        write_zsreg(info->zs_channel, R9, CHRA);
2907
 
2908
        if (options) {
2909
                baud = simple_strtoul(options, NULL, 10);
2910
                s = options;
2911
                while(*s >= '0' && *s <= '9')
2912
                        s++;
2913
                if (*s)
2914
                        parity = *s++;
2915
                if (*s)
2916
                        bits   = *s - '0';
2917
        }
2918
 
2919
        /*
2920
         *      Now construct a cflag setting.
2921
         */
2922
        switch(baud) {
2923
        case 1200:
2924
                cflag |= B1200;
2925
                break;
2926
        case 2400:
2927
                cflag |= B2400;
2928
                break;
2929
        case 4800:
2930
                cflag |= B4800;
2931
                break;
2932
        case 9600:
2933
                cflag |= B9600;
2934
                break;
2935
        case 19200:
2936
                cflag |= B19200;
2937
                break;
2938
        case 57600:
2939
                cflag |= B57600;
2940
                break;
2941
        case 115200:
2942
                cflag |= B115200;
2943
                break;
2944
        case 38400:
2945
        default:
2946
                cflag |= B38400;
2947
                break;
2948
        }
2949
        switch(bits) {
2950
        case 7:
2951
                cflag |= CS7;
2952
                break;
2953
        default:
2954
        case 8:
2955
                cflag |= CS8;
2956
                break;
2957
        }
2958
        switch(parity) {
2959
        case 'o': case 'O':
2960
                cflag |= PARENB | PARODD;
2961
                break;
2962
        case 'e': case 'E':
2963
                cflag |= PARENB;
2964
                break;
2965
        }
2966
        co->cflag = cflag;
2967
 
2968
        save_flags(flags); cli();
2969
        memset(info->curregs, 0, sizeof(info->curregs));
2970
 
2971
        info->zs_baud = baud;
2972
        info->clk_divisor = 16;
2973
        switch (info->zs_baud) {
2974
        case ZS_CLOCK/16:       /* 230400 */
2975
                info->curregs[4] = X16CLK;
2976
                info->curregs[11] = 0;
2977
                break;
2978
        case ZS_CLOCK/32:       /* 115200 */
2979
                info->curregs[4] = X32CLK;
2980
                info->curregs[11] = 0;
2981
                break;
2982
        default:
2983
                info->curregs[4] = X16CLK;
2984
                info->curregs[11] = TCBR | RCBR;
2985
                brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
2986
                info->curregs[12] = (brg & 255);
2987
                info->curregs[13] = ((brg >> 8) & 255);
2988
                info->curregs[14] = BRENABL;
2989
        }
2990
 
2991
        /* byte size and parity */
2992
        info->curregs[3] &= ~RxNBITS_MASK;
2993
        info->curregs[5] &= ~TxNBITS_MASK;
2994
        switch (cflag & CSIZE) {
2995
        case CS5:
2996
                info->curregs[3] |= Rx5;
2997
                info->curregs[5] |= Tx5;
2998
                break;
2999
        case CS6:
3000
                info->curregs[3] |= Rx6;
3001
                info->curregs[5] |= Tx6;
3002
                break;
3003
        case CS7:
3004
                info->curregs[3] |= Rx7;
3005
                info->curregs[5] |= Tx7;
3006
                break;
3007
        case CS8:
3008
        default: /* defaults to 8 bits */
3009
                info->curregs[3] |= Rx8;
3010
                info->curregs[5] |= Tx8;
3011
                break;
3012
        }
3013
        info->curregs[5] |= TxENAB | RTS | DTR;
3014
        info->pendregs[3] = info->curregs[3];
3015
        info->pendregs[5] = info->curregs[5];
3016
 
3017
        info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
3018
        if (cflag & CSTOPB) {
3019
                info->curregs[4] |= SB2;
3020
        } else {
3021
                info->curregs[4] |= SB1;
3022
        }
3023
        if (cflag & PARENB) {
3024
                info->curregs[4] |= PAR_ENA;
3025
                if (!(cflag & PARODD)) {
3026
                        info->curregs[4] |= PAR_EVEN;
3027
                }
3028
        }
3029
        info->pendregs[4] = info->curregs[4];
3030
 
3031
        if (!(cflag & CLOCAL)) {
3032
                if (!(info->curregs[15] & DCDIE))
3033
                        info->read_reg_zero = read_zsreg(info->zs_channel, 0);
3034
                info->curregs[15] |= DCDIE;
3035
        } else
3036
                info->curregs[15] &= ~DCDIE;
3037
        if (cflag & CRTSCTS) {
3038
                info->curregs[15] |= CTSIE;
3039
                if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
3040
                        info->tx_stopped = 1;
3041
        } else {
3042
                info->curregs[15] &= ~CTSIE;
3043
                info->tx_stopped = 0;
3044
        }
3045
        info->pendregs[15] = info->curregs[15];
3046
 
3047
        /* Load up the new values */
3048
        load_zsregs(info->zs_channel, info->curregs);
3049
 
3050
        restore_flags(flags);
3051
 
3052
        return 0;
3053
}
3054
 
3055
static struct console sercons = {
3056
        name:           "ttyS",
3057
        write:          serial_console_write,
3058
        device:         serial_console_device,
3059
        setup:          serial_console_setup,
3060
        flags:          CON_PRINTBUFFER,
3061
        index:          -1,
3062
};
3063
 
3064
/*
3065
 *      Register console.
3066
 */
3067
void __init mac_scc_console_init(void)
3068
{
3069
        register_console(&sercons);
3070
}
3071
#endif /* ifdef CONFIG_SERIAL_CONSOLE */
3072
 
3073
#ifdef CONFIG_KGDB
3074
/* These are for receiving and sending characters under the kgdb
3075
 * source level kernel debugger.
3076
 */
3077
void putDebugChar(char kgdb_char)
3078
{
3079
        struct mac_zschannel *chan = zs_kgdbchan;
3080
        while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
3081
                udelay(5);
3082
        write_zsdata(chan, kgdb_char);
3083
}
3084
 
3085
char getDebugChar(void)
3086
{
3087
        struct mac_zschannel *chan = zs_kgdbchan;
3088
        while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
3089
                eieio(); /*barrier();*/
3090
        return read_zsdata(chan);
3091
}
3092
 
3093
void kgdb_interruptible(int yes)
3094
{
3095
        struct mac_zschannel *chan = zs_kgdbchan;
3096
        int one, nine;
3097
        nine = read_zsreg(chan, 9);
3098
        if (yes == 1) {
3099
                one = EXT_INT_ENAB|INT_ALL_Rx;
3100
                nine |= MIE;
3101
                printk("turning serial ints on\n");
3102
        } else {
3103
                one = RxINT_DISAB;
3104
                nine &= ~MIE;
3105
                printk("turning serial ints off\n");
3106
        }
3107
        write_zsreg(chan, 1, one);
3108
        write_zsreg(chan, 9, nine);
3109
}
3110
 
3111
/* This sets up the serial port we're using, and turns on
3112
 * interrupts for that channel, so kgdb is usable once we're done.
3113
 */
3114
static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps)
3115
{
3116
        int brg;
3117
        int i, x;
3118
        volatile char *sccc = ms->control;
3119
        brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
3120
        printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
3121
        for (i = 20000; i != 0; --i) {
3122
                x = *sccc; eieio();
3123
        }
3124
        for (i = 0; i < sizeof(scc_inittab); ++i) {
3125
                write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
3126
                i++;
3127
        }
3128
}
3129
 
3130
/* This is called at boot time to prime the kgdb serial debugging
3131
 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
3132
 * for /dev/ttyb which is determined in setup_arch() from the
3133
 * boot command line flags.
3134
 * XXX at the moment probably only channel A will work
3135
 */
3136
void __init zs_kgdb_hook(int tty_num)
3137
{
3138
        /* Find out how many Z8530 SCCs we have */
3139
        if (zs_chain == 0)
3140
                probe_sccs();
3141
 
3142
        set_scc_power(&zs_soft[tty_num], 1);
3143
 
3144
        zs_kgdbchan = zs_soft[tty_num].zs_channel;
3145
        zs_soft[tty_num].change_needed = 0;
3146
        zs_soft[tty_num].clk_divisor = 16;
3147
        zs_soft[tty_num].zs_baud = 38400;
3148
        zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
3149
 
3150
        /* Turn on transmitter/receiver at 8-bits/char */
3151
        kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
3152
        printk("KGDB: on channel %d initialized\n", tty_num);
3153
        set_debug_traps(); /* init stub */
3154
}
3155
#endif /* ifdef CONFIG_KGDB */
3156
 
3157
#ifdef CONFIG_PMAC_PBOOK
3158
/*
3159
 * notify clients before sleep and reset bus afterwards
3160
 */
3161
int
3162
serial_notify_sleep(struct pmu_sleep_notifier *self, int when)
3163
{
3164
        int i;
3165
 
3166
        switch (when) {
3167
        case PBOOK_SLEEP_REQUEST:
3168
        case PBOOK_SLEEP_REJECT:
3169
                break;
3170
 
3171
        case PBOOK_SLEEP_NOW:
3172
                for (i=0; i<zs_channels_found; i++) {
3173
                        struct mac_serial *info = &zs_soft[i];
3174
                        if (info->flags & ZILOG_INITIALIZED) {
3175
                                shutdown(info);
3176
                                info->flags |= ZILOG_SLEEPING;
3177
                        }
3178
                }
3179
                break;
3180
        case PBOOK_WAKE:
3181
                for (i=0; i<zs_channels_found; i++) {
3182
                        struct mac_serial *info = &zs_soft[i];
3183
                        if (info->flags & ZILOG_SLEEPING) {
3184
                                info->flags &= ~ZILOG_SLEEPING;
3185
                                startup(info);
3186
                        }
3187
                }
3188
                break;
3189
        }
3190
        return PBOOK_SLEEP_OK;
3191
}
3192
#endif /* CONFIG_PMAC_PBOOK */

powered by: WebSVN 2.1.0

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