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