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