1 /*
2 * decserial.c: Serial port driver for IOASIC DECstations.
3 *
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6 *
7 * DECstation changes
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000, 2001, 2002, 2003, 2004 Maciej W. Rozycki
10 *
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 *
15 *
16 * Note: for IOASIC systems the wiring is as follows:
17 *
18 * mouse/keyboard:
19 * DIN-7 MJ-4 signal SCC
20 * 2 1 TxD <- A.TxD
21 * 3 4 RxD -> A.RxD
22 *
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal SCC
25 * 2 2 TxD <- B.TxD
26 * 3 5 RxD -> B.RxD
27 * 4 RTS <- ~A.RTS
28 * 5 CTS -> ~B.CTS
29 * 6 6 DSR -> ~A.SYNC
30 * 8 CD -> ~B.DCD
31 * 12 DSRS(DCE) -> ~A.CTS (*)
32 * 15 TxC -> B.TxC
33 * 17 RxC -> B.RxC
34 * 20 1 DTR <- ~A.DTR
35 * 22 RI -> ~A.DCD
36 * 23 DSRS(DTE) <- ~B.RTS
37 *
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 * is shared with DSRS(DTE) at pin 23.
40 */
41
42 #include <linux/config.h>
43 #include <linux/errno.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/mm.h>
54 #include <linux/kernel.h>
55 #include <linux/delay.h>
56 #include <linux/init.h>
57 #include <linux/ioport.h>
58 #ifdef CONFIG_SERIAL_DEC_CONSOLE
59 #include <linux/console.h>
60 #endif
61
62 #include <asm/io.h>
63 #include <asm/pgtable.h>
64 #include <asm/irq.h>
65 #include <asm/system.h>
66 #include <asm/uaccess.h>
67 #include <asm/bootinfo.h>
68 #include <asm/dec/serial.h>
69
70 #ifdef CONFIG_MACH_DECSTATION
71 #include <asm/dec/interrupts.h>
72 #include <asm/dec/machtype.h>
73 #include <asm/dec/tc.h>
74 #include <asm/dec/ioasic_addrs.h>
75 #endif
76 #ifdef CONFIG_KGDB
77 #include <asm/kgdb.h>
78 #endif
79 #ifdef CONFIG_MAGIC_SYSRQ
80 #include <linux/sysrq.h>
81 #endif
82
83 #include "zs.h"
84
85 /*
86 * It would be nice to dynamically allocate everything that
87 * depends on NUM_SERIAL, so we could support any number of
88 * Z8530s, but for now...
89 */
90 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
91 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
92 #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
93 /* Number of channel A in the chip */
94 #define ZS_CHAN_IO_SIZE 8
95 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
96
97 #define RECOVERY_DELAY udelay(2)
98
99 struct zs_parms {
100 unsigned long scc0;
101 unsigned long scc1;
102 int channel_a_offset;
103 int channel_b_offset;
104 int irq0;
105 int irq1;
106 int clock;
107 };
108
109 static struct zs_parms *zs_parms;
110
111 #ifdef CONFIG_MACH_DECSTATION
112 static struct zs_parms ds_parms = {
113 scc0 : IOASIC_SCC0,
114 scc1 : IOASIC_SCC1,
115 channel_a_offset : 1,
116 channel_b_offset : 9,
117 irq0 : -1,
118 irq1 : -1,
119 clock : ZS_CLOCK
120 };
121 #endif
122
123 #ifdef CONFIG_MACH_DECSTATION
124 #define DS_BUS_PRESENT (IOASIC)
125 #else
126 #define DS_BUS_PRESENT 0
127 #endif
128
129 #define BUS_PRESENT (DS_BUS_PRESENT)
130
131 struct dec_zschannel zs_channels[NUM_CHANNELS];
132 struct dec_serial zs_soft[NUM_CHANNELS];
133 int zs_channels_found;
134 struct dec_serial *zs_chain; /* list of all channels */
135
136 struct tty_struct zs_ttys[NUM_CHANNELS];
137
138 #ifdef CONFIG_SERIAL_DEC_CONSOLE
139 static struct console sercons;
140 #endif
141 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
142 !defined(MODULE)
143 static unsigned long break_pressed; /* break, really ... */
144 #endif
145
146 static unsigned char zs_init_regs[16] __initdata = {
147 0, /* write 0 */
148 0, /* write 1 */
149 0, /* write 2 */
150 0, /* write 3 */
151 (X16CLK), /* write 4 */
152 0, /* write 5 */
153 0, 0, 0, /* write 6, 7, 8 */
154 (MIE | DLC | NV), /* write 9 */
155 (NRZ), /* write 10 */
156 (TCBR | RCBR), /* write 11 */
157 0, 0, /* BRG time constant, write 12 + 13 */
158 (BRSRC | BRENABL), /* write 14 */
159 0 /* write 15 */
160 };
161
162 DECLARE_TASK_QUEUE(tq_zs_serial);
163
164 static struct tty_driver *serial_driver;
165
166 /* serial subtype definitions */
167 #define SERIAL_TYPE_NORMAL 1
168
169 /* number of characters left in xmit buffer before we ask for more */
170 #define WAKEUP_CHARS 256
171
172 /*
173 * Debugging.
174 */
175 #undef SERIAL_DEBUG_OPEN
176 #undef SERIAL_DEBUG_FLOW
177 #undef SERIAL_DEBUG_THROTTLE
178 #undef SERIAL_PARANOIA_CHECK
179
180 #undef ZS_DEBUG_REGS
181
182 #ifdef SERIAL_DEBUG_THROTTLE
183 #define _tty_name(tty,buf) tty_name(tty,buf)
184 #endif
185
186 #define RS_STROBE_TIME 10
187 #define RS_ISR_PASS_LIMIT 256
188
189 #define _INLINE_ inline
190
191 static void probe_sccs(void);
192 static void change_speed(struct dec_serial *info);
193 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
194
195 /*
196 * tmp_buf is used as a temporary buffer by serial_write. We need to
197 * lock it in case the copy_from_user blocks while swapping in a page,
198 * and some other program tries to do a serial write at the same time.
199 * Since the lock will only come under contention when the system is
200 * swapping and available memory is low, it makes sense to share one
201 * buffer across all the serial ports, since it significantly saves
202 * memory if large numbers of serial ports are open.
203 */
204 static unsigned char tmp_buf[4096]; /* This is cheating */
205 static DECLARE_MUTEX(tmp_buf_sem);
206
207 static inline int serial_paranoia_check(struct dec_serial *info,
208 char *name, const char *routine)
209 {
210 #ifdef SERIAL_PARANOIA_CHECK
211 static const char *badmagic =
212 "Warning: bad magic number for serial struct %s in %s\n";
213 static const char *badinfo =
214 "Warning: null mac_serial for %s in %s\n";
215
216 if (!info) {
217 printk(badinfo, name, routine);
218 return 1;
219 }
220 if (info->magic != SERIAL_MAGIC) {
221 printk(badmagic, name, routine);
222 return 1;
223 }
224 #endif
225 return 0;
226 }
227
228 /*
229 * This is used to figure out the divisor speeds and the timeouts
230 */
231 static int baud_table[] = {
232 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
233 9600, 19200, 38400, 57600, 115200, 0 };
234
235 /*
236 * Reading and writing Z8530 registers.
237 */
238 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
239 unsigned char reg)
240 {
241 unsigned char retval;
242
243 if (reg != 0) {
244 *channel->control = reg & 0xf;
245 fast_iob(); RECOVERY_DELAY;
246 }
247 retval = *channel->control;
248 RECOVERY_DELAY;
249 return retval;
250 }
251
252 static inline void write_zsreg(struct dec_zschannel *channel,
253 unsigned char reg, unsigned char value)
254 {
255 if (reg != 0) {
256 *channel->control = reg & 0xf;
257 fast_iob(); RECOVERY_DELAY;
258 }
259 *channel->control = value;
260 fast_iob(); RECOVERY_DELAY;
261 return;
262 }
263
264 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
265 {
266 unsigned char retval;
267
268 retval = *channel->data;
269 RECOVERY_DELAY;
270 return retval;
271 }
272
273 static inline void write_zsdata(struct dec_zschannel *channel,
274 unsigned char value)
275 {
276 *channel->data = value;
277 fast_iob(); RECOVERY_DELAY;
278 return;
279 }
280
281 static inline void load_zsregs(struct dec_zschannel *channel,
282 unsigned char *regs)
283 {
284 /* ZS_CLEARERR(channel);
285 ZS_CLEARFIFO(channel); */
286 /* Load 'em up */
287 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
288 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
289 write_zsreg(channel, R4, regs[R4]);
290 write_zsreg(channel, R9, regs[R9]);
291 write_zsreg(channel, R1, regs[R1]);
292 write_zsreg(channel, R2, regs[R2]);
293 write_zsreg(channel, R10, regs[R10]);
294 write_zsreg(channel, R11, regs[R11]);
295 write_zsreg(channel, R12, regs[R12]);
296 write_zsreg(channel, R13, regs[R13]);
297 write_zsreg(channel, R14, regs[R14]);
298 write_zsreg(channel, R15, regs[R15]);
299 write_zsreg(channel, R3, regs[R3]);
300 write_zsreg(channel, R5, regs[R5]);
301 return;
302 }
303
304 /* Sets or clears DTR/RTS on the requested line */
305 static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
306 {
307 unsigned long flags;
308
309
310 save_flags(flags); cli();
311 if (info->zs_channel != info->zs_chan_a) {
312 if (set) {
313 info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
314 } else {
315 info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
316 }
317 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
318 }
319 restore_flags(flags);
320 }
321
322 /* Utility routines for the Zilog */
323 static inline int get_zsbaud(struct dec_serial *ss)
324 {
325 struct dec_zschannel *channel = ss->zs_channel;
326 int brg;
327
328 /* The baud rate is split up between two 8-bit registers in
329 * what is termed 'BRG time constant' format in my docs for
330 * the chip, it is a function of the clk rate the chip is
331 * receiving which happens to be constant.
332 */
333 brg = (read_zsreg(channel, 13) << 8);
334 brg |= read_zsreg(channel, 12);
335 return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
336 }
337
338 /* On receive, this clears errors and the receiver interrupts */
339 static inline void rs_recv_clear(struct dec_zschannel *zsc)
340 {
341 write_zsreg(zsc, 0, ERR_RES);
342 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
343 }
344
345 /*
346 * ----------------------------------------------------------------------
347 *
348 * Here starts the interrupt handling routines. All of the following
349 * subroutines are declared as inline and are folded into
350 * rs_interrupt(). They were separated out for readability's sake.
351 *
352 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
353 * -----------------------------------------------------------------------
354 */
355
356 /*
357 * This routine is used by the interrupt handler to schedule
358 * processing in the software interrupt portion of the driver.
359 */
360 static _INLINE_ void rs_sched_event(struct dec_serial *info,
361 int event)
362 {
363 info->event |= 1 << event;
364 queue_task(&info->tqueue, &tq_zs_serial);
365 mark_bh(SERIAL_BH);
366 }
367
368 static _INLINE_ void receive_chars(struct dec_serial *info,
369 struct pt_regs *regs)
370 {
371 struct tty_struct *tty = info->tty;
372 unsigned char ch, stat, flag;
373
374 while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
375
376 stat = read_zsreg(info->zs_channel, R1);
377 ch = read_zsdata(info->zs_channel);
378
379 if (!tty && (!info->hook || !info->hook->rx_char))
380 continue;
381
382 flag = TTY_NORMAL;
383 if (info->tty_break) {
384 info->tty_break = 0;
385 flag = TTY_BREAK;
386 if (info->flags & ZILOG_SAK)
387 do_SAK(tty);
388 /* Ignore the null char got when BREAK is removed. */
389 if (ch == 0)
390 continue;
391 } else {
392 if (stat & Rx_OVR) {
393 flag = TTY_OVERRUN;
394 } else if (stat & FRM_ERR) {
395 flag = TTY_FRAME;
396 } else if (stat & PAR_ERR) {
397 flag = TTY_PARITY;
398 }
399 if (flag != TTY_NORMAL)
400 /* reset the error indication */
401 write_zsreg(info->zs_channel, R0, ERR_RES);
402 }
403
404 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
405 !defined(MODULE)
406 if (break_pressed && info->line == sercons.index) {
407 /* Ignore the null char got when BREAK is removed. */
408 if (ch == 0)
409 continue;
410 if (time_before(jiffies, break_pressed + HZ * 5)) {
411 handle_sysrq(ch, regs, NULL);
412 break_pressed = 0;
413 continue;
414 }
415 break_pressed = 0;
416 }
417 #endif
418
419 if (info->hook && info->hook->rx_char) {
420 (*info->hook->rx_char)(ch, flag);
421 return;
422 }
423
424 tty_insert_flip_char(tty, ch, flag);
425 }
426 if (tty)
427 tty_flip_buffer_push(tty);
428 }
429
430 static void transmit_chars(struct dec_serial *info)
431 {
432 if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
433 return;
434 info->tx_active = 0;
435
436 if (info->x_char) {
437 /* Send next char */
438 write_zsdata(info->zs_channel, info->x_char);
439 info->x_char = 0;
440 info->tx_active = 1;
441 return;
442 }
443
444 if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
445 || info->tx_stopped) {
446 write_zsreg(info->zs_channel, R0, RES_Tx_P);
447 return;
448 }
449 /* Send char */
450 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
451 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
452 info->xmit_cnt--;
453 info->tx_active = 1;
454
455 if (info->xmit_cnt < WAKEUP_CHARS)
456 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
457 }
458
459 static _INLINE_ void status_handle(struct dec_serial *info)
460 {
461 unsigned char stat;
462
463 /* Get status from Read Register 0 */
464 stat = read_zsreg(info->zs_channel, R0);
465
466 if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
467 #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
468 !defined(MODULE)
469 if (info->line == sercons.index) {
470 if (!break_pressed)
471 break_pressed = jiffies;
472 } else
473 #endif
474 info->tty_break = 1;
475 }
476
477 if (info->zs_channel != info->zs_chan_a) {
478
479 /* Check for DCD transitions */
480 if (info->tty && !C_CLOCAL(info->tty) &&
481 ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
482 if (stat & DCD) {
483 wake_up_interruptible(&info->open_wait);
484 } else {
485 tty_hangup(info->tty);
486 }
487 }
488
489 /* Check for CTS transitions */
490 if (info->tty && C_CRTSCTS(info->tty)) {
491 if ((stat & CTS) != 0) {
492 if (info->tx_stopped) {
493 info->tx_stopped = 0;
494 if (!info->tx_active)
495 transmit_chars(info);
496 }
497 } else {
498 info->tx_stopped = 1;
499 }
500 }
501
502 }
503
504 /* Clear status condition... */
505 write_zsreg(info->zs_channel, R0, RES_EXT_INT);
506 info->read_reg_zero = stat;
507 }
508
509 /*
510 * This is the serial driver's generic interrupt routine
511 */
512 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
513 {
514 struct dec_serial *info = (struct dec_serial *) dev_id;
515 unsigned char zs_intreg;
516 int shift;
517
518 /* NOTE: The read register 3, which holds the irq status,
519 * does so for both channels on each chip. Although
520 * the status value itself must be read from the A
521 * channel and is only valid when read from channel A.
522 * Yes... broken hardware...
523 */
524 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
525
526 if (info->zs_chan_a == info->zs_channel)
527 shift = 3; /* Channel A */
528 else
529 shift = 0; /* Channel B */
530
531 for (;;) {
532 zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
533 if ((zs_intreg & CHAN_IRQMASK) == 0)
534 break;
535
536 if (zs_intreg & CHBRxIP) {
537 receive_chars(info, regs);
538 }
539 if (zs_intreg & CHBTxIP) {
540 transmit_chars(info);
541 }
542 if (zs_intreg & CHBEXT) {
543 status_handle(info);
544 }
545 }
546
547 /* Why do we need this ? */
548 write_zsreg(info->zs_channel, 0, RES_H_IUS);
549 }
550
551 #ifdef ZS_DEBUG_REGS
552 void zs_dump (void) {
553 int i, j;
554 for (i = 0; i < zs_channels_found; i++) {
555 struct dec_zschannel *ch = &zs_channels[i];
556 if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
557 for (j = 0; j < 15; j++) {
558 printk("W%d = 0x%x\t",
559 j, (int)ch->curregs[j]);
560 }
561 for (j = 0; j < 15; j++) {
562 printk("R%d = 0x%x\t",
563 j, (int)read_zsreg(ch,j));
564 }
565 printk("\n\n");
566 }
567 }
568 }
569 #endif
570
571 /*
572 * -------------------------------------------------------------------
573 * Here ends the serial interrupt routines.
574 * -------------------------------------------------------------------
575 */
576
577 /*
578 * ------------------------------------------------------------
579 * rs_stop() and rs_start()
580 *
581 * This routines are called before setting or resetting tty->stopped.
582 * ------------------------------------------------------------
583 */
584 static void rs_stop(struct tty_struct *tty)
585 {
586 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
587 unsigned long flags;
588
589 if (serial_paranoia_check(info, tty->name, "rs_stop"))
590 return;
591
592 #if 1
593 save_flags(flags); cli();
594 if (info->zs_channel->curregs[5] & TxENAB) {
595 info->zs_channel->curregs[5] &= ~TxENAB;
596 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
597 }
598 restore_flags(flags);
599 #endif
600 }
601
602 static void rs_start(struct tty_struct *tty)
603 {
604 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
605 unsigned long flags;
606
607 if (serial_paranoia_check(info, tty->name, "rs_start"))
608 return;
609
610 save_flags(flags); cli();
611 #if 1
612 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
613 info->zs_channel->curregs[5] |= TxENAB;
614 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
615 }
616 #else
617 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
618 transmit_chars(info);
619 }
620 #endif
621 restore_flags(flags);
622 }
623
624 /*
625 * This routine is used to handle the "bottom half" processing for the
626 * serial driver, known also the "software interrupt" processing.
627 * This processing is done at the kernel interrupt level, after the
628 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
629 * is where time-consuming activities which can not be done in the
630 * interrupt driver proper are done; the interrupt driver schedules
631 * them using rs_sched_event(), and they get done here.
632 */
633 static void do_serial_bh(void)
634 {
635 run_task_queue(&tq_zs_serial);
636 }
637
638 static void do_softint(void *private_)
639 {
640 struct dec_serial *info = (struct dec_serial *) private_;
641 struct tty_struct *tty;
642
643 tty = info->tty;
644 if (!tty)
645 return;
646
647 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
648 tty_wakeup(tty);
649 }
650 }
651
652 int zs_startup(struct dec_serial * info)
653 {
654 unsigned long flags;
655
656 if (info->flags & ZILOG_INITIALIZED)
657 return 0;
658
659 if (!info->xmit_buf) {
660 info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
661 if (!info->xmit_buf)
662 return -ENOMEM;
663 }
664
665 save_flags(flags); cli();
666
667 #ifdef SERIAL_DEBUG_OPEN
668 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
669 #endif
670
671 /*
672 * Clear the receive FIFO.
673 */
674 ZS_CLEARFIFO(info->zs_channel);
675 info->xmit_fifo_size = 1;
676
677 /*
678 * Clear the interrupt registers.
679 */
680 write_zsreg(info->zs_channel, R0, ERR_RES);
681 write_zsreg(info->zs_channel, R0, RES_H_IUS);
682
683 /*
684 * Set the speed of the serial port
685 */
686 change_speed(info);
687
688 /*
689 * Turn on RTS and DTR.
690 */
691 zs_rtsdtr(info, RTS | DTR, 1);
692
693 /*
694 * Finally, enable sequencing and interrupts
695 */
696 info->zs_channel->curregs[R1] &= ~RxINT_MASK;
697 info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
698 EXT_INT_ENAB);
699 info->zs_channel->curregs[R3] |= RxENABLE;
700 info->zs_channel->curregs[R5] |= TxENAB;
701 info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
702 write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
703 write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
704 write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
705 write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
706
707 /*
708 * And clear the interrupt registers again for luck.
709 */
710 write_zsreg(info->zs_channel, R0, ERR_RES);
711 write_zsreg(info->zs_channel, R0, RES_H_IUS);
712
713 /* Save the current value of RR0 */
714 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
715
716 if (info->tty)
717 clear_bit(TTY_IO_ERROR, &info->tty->flags);
718 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
719
720 info->flags |= ZILOG_INITIALIZED;
721 restore_flags(flags);
722 return 0;
723 }
724
725 /*
726 * This routine will shutdown a serial port; interrupts are disabled, and
727 * DTR is dropped if the hangup on close termio flag is on.
728 */
729 static void shutdown(struct dec_serial * info)
730 {
731 unsigned long flags;
732
733 if (!(info->flags & ZILOG_INITIALIZED))
734 return;
735
736 #ifdef SERIAL_DEBUG_OPEN
737 printk("Shutting down serial port %d (irq %d)....", info->line,
738 info->irq);
739 #endif
740
741 save_flags(flags); cli(); /* Disable interrupts */
742
743 if (info->xmit_buf) {
744 free_page((unsigned long) info->xmit_buf);
745 info->xmit_buf = 0;
746 }
747
748 info->zs_channel->curregs[1] = 0;
749 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
750
751 info->zs_channel->curregs[3] &= ~RxENABLE;
752 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
753
754 info->zs_channel->curregs[5] &= ~TxENAB;
755 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
756 if (!info->tty || C_HUPCL(info->tty)) {
757 zs_rtsdtr(info, RTS | DTR, 0);
758 }
759
760 if (info->tty)
761 set_bit(TTY_IO_ERROR, &info->tty->flags);
762
763 info->flags &= ~ZILOG_INITIALIZED;
764 restore_flags(flags);
765 }
766
767 /*
768 * This routine is called to set the UART divisor registers to match
769 * the specified baud rate for a serial port.
770 */
771 static void change_speed(struct dec_serial *info)
772 {
773 unsigned cflag;
774 int i;
775 int brg, bits;
776 unsigned long flags;
777
778 if (!info->hook) {
779 if (!info->tty || !info->tty->termios)
780 return;
781 cflag = info->tty->termios->c_cflag;
782 if (!info->port)
783 return;
784 } else {
785 cflag = info->hook->cflags;
786 }
787
788 i = cflag & CBAUD;
789 if (i & CBAUDEX) {
790 i &= ~CBAUDEX;
791 if (i < 1 || i > 2) {
792 if (!info->hook)
793 info->tty->termios->c_cflag &= ~CBAUDEX;
794 else
795 info->hook->cflags &= ~CBAUDEX;
796 } else
797 i += 15;
798 }
799
800 save_flags(flags); cli();
801 info->zs_baud = baud_table[i];
802 if (info->zs_baud) {
803 brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
804 info->zs_channel->curregs[12] = (brg & 255);
805 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
806 zs_rtsdtr(info, DTR, 1);
807 } else {
808 zs_rtsdtr(info, RTS | DTR, 0);
809 return;
810 }
811
812 /* byte size and parity */
813 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
814 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
815 switch (cflag & CSIZE) {
816 case CS5:
817 bits = 7;
818 info->zs_channel->curregs[3] |= Rx5;
819 info->zs_channel->curregs[5] |= Tx5;
820 break;
821 case CS6:
822 bits = 8;
823 info->zs_channel->curregs[3] |= Rx6;
824 info->zs_channel->curregs[5] |= Tx6;
825 break;
826 case CS7:
827 bits = 9;
828 info->zs_channel->curregs[3] |= Rx7;
829 info->zs_channel->curregs[5] |= Tx7;
830 break;
831 case CS8:
832 default: /* defaults to 8 bits */
833 bits = 10;
834 info->zs_channel->curregs[3] |= Rx8;
835 info->zs_channel->curregs[5] |= Tx8;
836 break;
837 }
838
839 info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
840 info->timeout += HZ/50; /* Add .02 seconds of slop */
841
842 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
843 if (cflag & CSTOPB) {
844 info->zs_channel->curregs[4] |= SB2;
845 } else {
846 info->zs_channel->curregs[4] |= SB1;
847 }
848 if (cflag & PARENB) {
849 info->zs_channel->curregs[4] |= PAR_ENA;
850 }
851 if (!(cflag & PARODD)) {
852 info->zs_channel->curregs[4] |= PAR_EVEN;
853 }
854
855 if (!(cflag & CLOCAL)) {
856 if (!(info->zs_channel->curregs[15] & DCDIE))
857 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
858 info->zs_channel->curregs[15] |= DCDIE;
859 } else
860 info->zs_channel->curregs[15] &= ~DCDIE;
861 if (cflag & CRTSCTS) {
862 info->zs_channel->curregs[15] |= CTSIE;
863 if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
864 info->tx_stopped = 1;
865 } else {
866 info->zs_channel->curregs[15] &= ~CTSIE;
867 info->tx_stopped = 0;
868 }
869
870 /* Load up the new values */
871 load_zsregs(info->zs_channel, info->zs_channel->curregs);
872
873 restore_flags(flags);
874 }
875
876 static void rs_flush_chars(struct tty_struct *tty)
877 {
878 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
879 unsigned long flags;
880
881 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
882 return;
883
884 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
885 !info->xmit_buf)
886 return;
887
888 /* Enable transmitter */
889 save_flags(flags); cli();
890 transmit_chars(info);
891 restore_flags(flags);
892 }
893
894 static int rs_write(struct tty_struct * tty,
895 const unsigned char *buf, int count)
896 {
897 int c, total = 0;
898 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
899 unsigned long flags;
900
901 if (serial_paranoia_check(info, tty->name, "rs_write"))
902 return 0;
903
904 if (!tty || !info->xmit_buf)
905 return 0;
906
907 save_flags(flags);
908 while (1) {
909 cli();
910 c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
911 SERIAL_XMIT_SIZE - info->xmit_head));
912 if (c <= 0)
913 break;
914
915 if (from_user) {
916 down(&tmp_buf_sem);
917 copy_from_user(tmp_buf, buf, c);
918 c = min(c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
919 SERIAL_XMIT_SIZE - info->xmit_head));
920 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
921 up(&tmp_buf_sem);
922 } else
923 memcpy(info->xmit_buf + info->xmit_head, buf, c);
924 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
925 info->xmit_cnt += c;
926 restore_flags(flags);
927 buf += c;
928 count -= c;
929 total += c;
930 }
931
932 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
933 && !info->tx_active)
934 transmit_chars(info);
935 restore_flags(flags);
936 return total;
937 }
938
939 static int rs_write_room(struct tty_struct *tty)
940 {
941 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
942 int ret;
943
944 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
945 return 0;
946 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
947 if (ret < 0)
948 ret = 0;
949 return ret;
950 }
951
952 static int rs_chars_in_buffer(struct tty_struct *tty)
953 {
954 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
955
956 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
957 return 0;
958 return info->xmit_cnt;
959 }
960
961 static void rs_flush_buffer(struct tty_struct *tty)
962 {
963 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
964
965 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
966 return;
967 cli();
968 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
969 sti();
970 tty_wakeup(tty);
971 }
972
973 /*
974 * ------------------------------------------------------------
975 * rs_throttle()
976 *
977 * This routine is called by the upper-layer tty layer to signal that
978 * incoming characters should be throttled.
979 * ------------------------------------------------------------
980 */
981 static void rs_throttle(struct tty_struct * tty)
982 {
983 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
984 unsigned long flags;
985
986 #ifdef SERIAL_DEBUG_THROTTLE
987 char buf[64];
988
989 printk("throttle %s: %d....\n", _tty_name(tty, buf),
990 tty->ldisc.chars_in_buffer(tty));
991 #endif
992
993 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
994 return;
995
996 if (I_IXOFF(tty)) {
997 save_flags(flags); cli();
998 info->x_char = STOP_CHAR(tty);
999 if (!info->tx_active)
1000 transmit_chars(info);
1001 restore_flags(flags);
1002 }
1003
1004 if (C_CRTSCTS(tty)) {
1005 zs_rtsdtr(info, RTS, 0);
1006 }
1007 }
1008
1009 static void rs_unthrottle(struct tty_struct * tty)
1010 {
1011 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1012 unsigned long flags;
1013
1014 #ifdef SERIAL_DEBUG_THROTTLE
1015 char buf[64];
1016
1017 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1018 tty->ldisc.chars_in_buffer(tty));
1019 #endif
1020
1021 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1022 return;
1023
1024 if (I_IXOFF(tty)) {
1025 save_flags(flags); cli();
1026 if (info->x_char)
1027 info->x_char = 0;
1028 else {
1029 info->x_char = START_CHAR(tty);
1030 if (!info->tx_active)
1031 transmit_chars(info);
1032 }
1033 restore_flags(flags);
1034 }
1035
1036 if (C_CRTSCTS(tty)) {
1037 zs_rtsdtr(info, RTS, 1);
1038 }
1039 }
1040
1041 /*
1042 * ------------------------------------------------------------
1043 * rs_ioctl() and friends
1044 * ------------------------------------------------------------
1045 */
1046
1047 static int get_serial_info(struct dec_serial * info,
1048 struct serial_struct * retinfo)
1049 {
1050 struct serial_struct tmp;
1051
1052 if (!retinfo)
1053 return -EFAULT;
1054 memset(&tmp, 0, sizeof(tmp));
1055 tmp.type = info->type;
1056 tmp.line = info->line;
1057 tmp.port = info->port;
1058 tmp.irq = info->irq;
1059 tmp.flags = info->flags;
1060 tmp.baud_base = info->baud_base;
1061 tmp.close_delay = info->close_delay;
1062 tmp.closing_wait = info->closing_wait;
1063 tmp.custom_divisor = info->custom_divisor;
1064 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1065 }
1066
1067 static int set_serial_info(struct dec_serial * info,
1068 struct serial_struct * new_info)
1069 {
1070 struct serial_struct new_serial;
1071 struct dec_serial old_info;
1072 int retval = 0;
1073
1074 if (!new_info)
1075 return -EFAULT;
1076 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1077 old_info = *info;
1078
1079 if (!capable(CAP_SYS_ADMIN)) {
1080 if ((new_serial.baud_base != info->baud_base) ||
1081 (new_serial.type != info->type) ||
1082 (new_serial.close_delay != info->close_delay) ||
1083 ((new_serial.flags & ~ZILOG_USR_MASK) !=
1084 (info->flags & ~ZILOG_USR_MASK)))
1085 return -EPERM;
1086 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1087 (new_serial.flags & ZILOG_USR_MASK));
1088 info->custom_divisor = new_serial.custom_divisor;
1089 goto check_and_exit;
1090 }
1091
1092 if (info->count > 1)
1093 return -EBUSY;
1094
1095 /*
1096 * OK, past this point, all the error checking has been done.
1097 * At this point, we start making changes.....
1098 */
1099
1100 info->baud_base = new_serial.baud_base;
1101 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1102 (new_serial.flags & ZILOG_FLAGS));
1103 info->type = new_serial.type;
1104 info->close_delay = new_serial.close_delay;
1105 info->closing_wait = new_serial.closing_wait;
1106
1107 check_and_exit:
1108 retval = zs_startup(info);
1109 return retval;
1110 }
1111
1112 /*
1113 * get_lsr_info - get line status register info
1114 *
1115 * Purpose: Let user call ioctl() to get info when the UART physically
1116 * is emptied. On bus types like RS485, the transmitter must
1117 * release the bus after transmitting. This must be done when
1118 * the transmit shift register is empty, not be done when the
1119 * transmit holding register is empty. This functionality
1120 * allows an RS485 driver to be written in user space.
1121 */
1122 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1123 {
1124 unsigned char status;
1125
1126 cli();
1127 status = read_zsreg(info->zs_channel, 0);
1128 sti();
1129 put_user(status,value);
1130 return 0;
1131 }
1132
1133 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1134 {
1135 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1136 unsigned char control, status_a, status_b;
1137 unsigned int result;
1138
1139 if (info->hook)
1140 return -ENODEV;
1141
1142 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1143 return -ENODEV;
1144
1145 if (tty->flags & (1 << TTY_IO_ERROR))
1146 return -EIO;
1147
1148 if (info->zs_channel == info->zs_chan_a)
1149 result = 0;
1150 else {
1151 cli();
1152 control = info->zs_chan_a->curregs[5];
1153 status_a = read_zsreg(info->zs_chan_a, 0);
1154 status_b = read_zsreg(info->zs_channel, 0);
1155 sti();
1156 result = ((control & RTS) ? TIOCM_RTS: 0)
1157 | ((control & DTR) ? TIOCM_DTR: 0)
1158 | ((status_b & DCD) ? TIOCM_CAR: 0)
1159 | ((status_a & DCD) ? TIOCM_RNG: 0)
1160 | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1161 | ((status_b & CTS) ? TIOCM_CTS: 0);
1162 }
1163 return result;
1164 }
1165
1166 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1167 unsigned int set, unsigned int clear)
1168 {
1169 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1170 int error;
1171 unsigned int arg, bits;
1172
1173 if (info->hook)
1174 return -ENODEV;
1175
1176 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1177 return -ENODEV;
1178
1179 if (tty->flags & (1 << TTY_IO_ERROR))
1180 return -EIO;
1181
1182 if (info->zs_channel == info->zs_chan_a)
1183 return 0;
1184
1185 get_user(arg, value);
1186 cli();
1187 if (set & TIOCM_RTS)
1188 info->zs_chan_a->curregs[5] |= RTS;
1189 if (set & TIOCM_DTR)
1190 info->zs_chan_a->curregs[5] |= DTR;
1191 if (clear & TIOCM_RTS)
1192 info->zs_chan_a->curregs[5] &= ~RTS;
1193 if (clear & TIOCM_DTR)
1194 info->zs_chan_a->curregs[5] &= ~DTR;
1195 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1196 sti();
1197 return 0;
1198 }
1199
1200 /*
1201 * rs_break - turn transmit break condition on/off
1202 */
1203 static void rs_break(struct tty_struct *tty, int break_state)
1204 {
1205 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1206 unsigned long flags;
1207
1208 if (serial_paranoia_check(info, tty->name, "rs_break"))
1209 return;
1210 if (!info->port)
1211 return;
1212
1213 save_flags(flags); cli();
1214 if (break_state == -1)
1215 info->zs_channel->curregs[5] |= SND_BRK;
1216 else
1217 info->zs_channel->curregs[5] &= ~SND_BRK;
1218 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1219 restore_flags(flags);
1220 }
1221
1222 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1223 unsigned int cmd, unsigned long arg)
1224 {
1225 int error;
1226 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1227
1228 if (info->hook)
1229 return -ENODEV;
1230
1231 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1232 return -ENODEV;
1233
1234 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1235 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1236 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1237 if (tty->flags & (1 << TTY_IO_ERROR))
1238 return -EIO;
1239 }
1240
1241 switch (cmd) {
1242 case TIOCGSERIAL:
1243 error = verify_area(VERIFY_WRITE, (void *)arg,
1244 sizeof(struct serial_struct));
1245 if (error)
1246 return error;
1247 return get_serial_info(info, (struct serial_struct *)arg);
1248
1249 case TIOCSSERIAL:
1250 return set_serial_info(info, (struct serial_struct *)arg);
1251
1252 case TIOCSERGETLSR: /* Get line status register */
1253 error = verify_area(VERIFY_WRITE, (void *)arg,
1254 sizeof(unsigned int));
1255 if (error)
1256 return error;
1257 else
1258 return get_lsr_info(info, (unsigned int *)arg);
1259
1260 case TIOCSERGSTRUCT:
1261 error = verify_area(VERIFY_WRITE, (void *)arg,
1262 sizeof(struct dec_serial));
1263 if (error)
1264 return error;
1265 copy_from_user((struct dec_serial *)arg, info,
1266 sizeof(struct dec_serial));
1267 return 0;
1268
1269 default:
1270 return -ENOIOCTLCMD;
1271 }
1272 return 0;
1273 }
1274
1275 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1276 {
1277 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1278 int was_stopped;
1279
1280 if (tty->termios->c_cflag == old_termios->c_cflag)
1281 return;
1282 was_stopped = info->tx_stopped;
1283
1284 change_speed(info);
1285
1286 if (was_stopped && !info->tx_stopped)
1287 rs_start(tty);
1288 }
1289
1290 /*
1291 * ------------------------------------------------------------
1292 * rs_close()
1293 *
1294 * This routine is called when the serial port gets closed.
1295 * Wait for the last remaining data to be sent.
1296 * ------------------------------------------------------------
1297 */
1298 static void rs_close(struct tty_struct *tty, struct file * filp)
1299 {
1300 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1301 unsigned long flags;
1302
1303 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1304 return;
1305
1306 save_flags(flags); cli();
1307
1308 if (tty_hung_up_p(filp)) {
1309 restore_flags(flags);
1310 return;
1311 }
1312
1313 #ifdef SERIAL_DEBUG_OPEN
1314 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1315 #endif
1316 if ((tty->count == 1) && (info->count != 1)) {
1317 /*
1318 * Uh, oh. tty->count is 1, which means that the tty
1319 * structure will be freed. Info->count should always
1320 * be one in these conditions. If it's greater than
1321 * one, we've got real problems, since it means the
1322 * serial port won't be shutdown.
1323 */
1324 printk("rs_close: bad serial port count; tty->count is 1, "
1325 "info->count is %d\n", info->count);
1326 info->count = 1;
1327 }
1328 if (--info->count < 0) {
1329 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1330 info->line, info->count);
1331 info->count = 0;
1332 }
1333 if (info->count) {
1334 restore_flags(flags);
1335 return;
1336 }
1337 info->flags |= ZILOG_CLOSING;
1338 /*
1339 * Now we wait for the transmit buffer to clear; and we notify
1340 * the line discipline to only process XON/XOFF characters.
1341 */
1342 tty->closing = 1;
1343 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1344 tty_wait_until_sent(tty, info->closing_wait);
1345 /*
1346 * At this point we stop accepting input. To do this, we
1347 * disable the receiver and receive interrupts.
1348 */
1349 info->zs_channel->curregs[3] &= ~RxENABLE;
1350 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1351 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1352 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1353 ZS_CLEARFIFO(info->zs_channel);
1354 if (info->flags & ZILOG_INITIALIZED) {
1355 /*
1356 * Before we drop DTR, make sure the SCC transmitter
1357 * has completely drained.
1358 */
1359 rs_wait_until_sent(tty, info->timeout);
1360 }
1361
1362 shutdown(info);
1363 if (tty->driver->flush_buffer)
1364 tty->driver->flush_buffer(tty);
1365 tty_ldisc_flush(tty);
1366 tty->closing = 0;
1367 info->event = 0;
1368 info->tty = 0;
1369 if (info->blocked_open) {
1370 if (info->close_delay) {
1371 current->state = TASK_INTERRUPTIBLE;
1372 schedule_timeout(info->close_delay);
1373 }
1374 wake_up_interruptible(&info->open_wait);
1375 }
1376 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
1377 wake_up_interruptible(&info->close_wait);
1378 restore_flags(flags);
1379 }
1380
1381 /*
1382 * rs_wait_until_sent() --- wait until the transmitter is empty
1383 */
1384 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1385 {
1386 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1387 unsigned long orig_jiffies;
1388 int char_time;
1389
1390 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1391 return;
1392
1393 orig_jiffies = jiffies;
1394 /*
1395 * Set the check interval to be 1/5 of the estimated time to
1396 * send a single character, and make it at least 1. The check
1397 * interval should also be less than the timeout.
1398 */
1399 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1400 char_time = char_time / 5;
1401 if (char_time == 0)
1402 char_time = 1;
1403 if (timeout)
1404 char_time = min(char_time, timeout);
1405 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1406 current->state = TASK_INTERRUPTIBLE;
1407 schedule_timeout(char_time);
1408 if (signal_pending(current))
1409 break;
1410 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1411 break;
1412 }
1413 current->state = TASK_RUNNING;
1414 }
1415
1416 /*
1417 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1418 */
1419 void rs_hangup(struct tty_struct *tty)
1420 {
1421 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1422
1423 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1424 return;
1425
1426 rs_flush_buffer(tty);
1427 shutdown(info);
1428 info->event = 0;
1429 info->count = 0;
1430 info->flags &= ~ZILOG_NORMAL_ACTIVE;
1431 info->tty = 0;
1432 wake_up_interruptible(&info->open_wait);
1433 }
1434
1435 /*
1436 * ------------------------------------------------------------
1437 * rs_open() and friends
1438 * ------------------------------------------------------------
1439 */
1440 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1441 struct dec_serial *info)
1442 {
1443 DECLARE_WAITQUEUE(wait, current);
1444 int retval;
1445 int do_clocal = 0;
1446
1447 /*
1448 * If the device is in the middle of being closed, then block
1449 * until it's done, and then try again.
1450 */
1451 if (info->flags & ZILOG_CLOSING) {
1452 interruptible_sleep_on(&info->close_wait);
1453 #ifdef SERIAL_DO_RESTART
1454 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1455 -EAGAIN : -ERESTARTSYS);
1456 #else
1457 return -EAGAIN;
1458 #endif
1459 }
1460
1461 /*
1462 * If non-blocking mode is set, or the port is not enabled,
1463 * then make the check up front and then exit.
1464 */
1465 if ((filp->f_flags & O_NONBLOCK) ||
1466 (tty->flags & (1 << TTY_IO_ERROR))) {
1467 info->flags |= ZILOG_NORMAL_ACTIVE;
1468 return 0;
1469 }
1470
1471 if (tty->termios->c_cflag & CLOCAL)
1472 do_clocal = 1;
1473
1474 /*
1475 * Block waiting for the carrier detect and the line to become
1476 * free (i.e., not in use by the callout). While we are in
1477 * this loop, info->count is dropped by one, so that
1478 * rs_close() knows when to free things. We restore it upon
1479 * exit, either normal or abnormal.
1480 */
1481 retval = 0;
1482 add_wait_queue(&info->open_wait, &wait);
1483 #ifdef SERIAL_DEBUG_OPEN
1484 printk("block_til_ready before block: ttyS%d, count = %d\n",
1485 info->line, info->count);
1486 #endif
1487 cli();
1488 if (!tty_hung_up_p(filp))
1489 info->count--;
1490 sti();
1491 info->blocked_open++;
1492 while (1) {
1493 cli();
1494 if (tty->termios->c_cflag & CBAUD)
1495 zs_rtsdtr(info, RTS | DTR, 1);
1496 sti();
1497 set_current_state(TASK_INTERRUPTIBLE);
1498 if (tty_hung_up_p(filp) ||
1499 !(info->flags & ZILOG_INITIALIZED)) {
1500 #ifdef SERIAL_DO_RESTART
1501 if (info->flags & ZILOG_HUP_NOTIFY)
1502 retval = -EAGAIN;
1503 else
1504 retval = -ERESTARTSYS;
1505 #else
1506 retval = -EAGAIN;
1507 #endif
1508 break;
1509 }
1510 if (!(info->flags & ZILOG_CLOSING) &&
1511 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1512 break;
1513 if (signal_pending(current)) {
1514 retval = -ERESTARTSYS;
1515 break;
1516 }
1517 #ifdef SERIAL_DEBUG_OPEN
1518 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1519 info->line, info->count);
1520 #endif
1521 schedule();
1522 }
1523 current->state = TASK_RUNNING;
1524 remove_wait_queue(&info->open_wait, &wait);
1525 if (!tty_hung_up_p(filp))
1526 info->count++;
1527 info->blocked_open--;
1528 #ifdef SERIAL_DEBUG_OPEN
1529 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1530 info->line, info->count);
1531 #endif
1532 if (retval)
1533 return retval;
1534 info->flags |= ZILOG_NORMAL_ACTIVE;
1535 return 0;
1536 }
1537
1538 /*
1539 * This routine is called whenever a serial port is opened. It
1540 * enables interrupts for a serial port, linking in its ZILOG structure into
1541 * the IRQ chain. It also performs the serial-specific
1542 * initialization for the tty structure.
1543 */
1544 int rs_open(struct tty_struct *tty, struct file * filp)
1545 {
1546 struct dec_serial *info;
1547 int retval, line;
1548
1549 line = tty->index;
1550 if ((line < 0) || (line >= zs_channels_found))
1551 return -ENODEV;
1552 info = zs_soft + line;
1553
1554 if (info->hook)
1555 return -ENODEV;
1556
1557 if (serial_paranoia_check(info, tty->name, "rs_open"))
1558 return -ENODEV;
1559 #ifdef SERIAL_DEBUG_OPEN
1560 printk("rs_open %s, count = %d\n", tty->name, info->count);
1561 #endif
1562
1563 info->count++;
1564 tty->driver_data = info;
1565 info->tty = tty;
1566
1567 /*
1568 * If the port is the middle of closing, bail out now
1569 */
1570 if (tty_hung_up_p(filp) ||
1571 (info->flags & ZILOG_CLOSING)) {
1572 if (info->flags & ZILOG_CLOSING)
1573 interruptible_sleep_on(&info->close_wait);
1574 #ifdef SERIAL_DO_RESTART
1575 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1576 -EAGAIN : -ERESTARTSYS);
1577 #else
1578 return -EAGAIN;
1579 #endif
1580 }
1581
1582 /*
1583 * Start up serial port
1584 */
1585 retval = zs_startup(info);
1586 if (retval)
1587 return retval;
1588
1589 retval = block_til_ready(tty, filp, info);
1590 if (retval) {
1591 #ifdef SERIAL_DEBUG_OPEN
1592 printk("rs_open returning after block_til_ready with %d\n",
1593 retval);
1594 #endif
1595 return retval;
1596 }
1597
1598 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1599 if (sercons.cflag && sercons.index == line) {
1600 tty->termios->c_cflag = sercons.cflag;
1601 sercons.cflag = 0;
1602 change_speed(info);
1603 }
1604 #endif
1605
1606 #ifdef SERIAL_DEBUG_OPEN
1607 printk("rs_open %s successful...", tty->name);
1608 #endif
1609 /* tty->low_latency = 1; */
1610 return 0;
1611 }
1612
1613 /* Finally, routines used to initialize the serial driver. */
1614
1615 static void __init show_serial_version(void)
1616 {
1617 printk("DECstation Z8530 serial driver version 0.09\n");
1618 }
1619
1620 /* Initialize Z8530s zs_channels
1621 */
1622
1623 static void __init probe_sccs(void)
1624 {
1625 struct dec_serial **pp;
1626 int i, n, n_chips = 0, n_channels, chip, channel;
1627 unsigned long flags;
1628
1629 /*
1630 * did we get here by accident?
1631 */
1632 if(!BUS_PRESENT) {
1633 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1634 return;
1635 }
1636
1637 /*
1638 * When serial console is activated, tc_init has not been called yet
1639 * and system_base is undefined. Unfortunately we have to hardcode
1640 * system_base for this case :-(. HK
1641 */
1642 switch(mips_machtype) {
1643 #ifdef CONFIG_MACH_DECSTATION
1644 case MACH_DS5000_2X0:
1645 case MACH_DS5900:
1646 system_base = KSEG1ADDR(0x1f800000);
1647 n_chips = 2;
1648 zs_parms = &ds_parms;
1649 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1650 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1651 break;
1652 case MACH_DS5000_1XX:
1653 system_base = KSEG1ADDR(0x1c000000);
1654 n_chips = 2;
1655 zs_parms = &ds_parms;
1656 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1657 zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1658 break;
1659 case MACH_DS5000_XX:
1660 system_base = KSEG1ADDR(0x1c000000);
1661 n_chips = 1;
1662 zs_parms = &ds_parms;
1663 zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1664 break;
1665 #endif
1666 default:
1667 panic("zs: unsupported bus");
1668 }
1669 if (!zs_parms)
1670 panic("zs: uninitialized parms");
1671
1672 pp = &zs_chain;
1673
1674 n_channels = 0;
1675
1676 for (chip = 0; chip < n_chips; chip++) {
1677 for (channel = 0; channel <= 1; channel++) {
1678 /*
1679 * The sccs reside on the high byte of the 16 bit IOBUS
1680 */
1681 zs_channels[n_channels].control =
1682 (volatile unsigned char *)system_base +
1683 (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1684 (0 == channel ? zs_parms->channel_a_offset :
1685 zs_parms->channel_b_offset);
1686 zs_channels[n_channels].data =
1687 zs_channels[n_channels].control + 4;
1688
1689 #ifndef CONFIG_SERIAL_DEC_CONSOLE
1690 /*
1691 * We're called early and memory managment isn't up, yet.
1692 * Thus check_region would fail.
1693 */
1694 if (!request_region((unsigned long)
1695 zs_channels[n_channels].control,
1696 ZS_CHAN_IO_SIZE, "SCC"))
1697 panic("SCC I/O region is not free");
1698 #endif
1699 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1700 /* HACK alert! */
1701 if (!(chip & 1))
1702 zs_soft[n_channels].irq = zs_parms->irq0;
1703 else
1704 zs_soft[n_channels].irq = zs_parms->irq1;
1705
1706 /*
1707 * Identification of channel A. Location of channel A
1708 * inside chip depends on mapping of internal address
1709 * the chip decodes channels by.
1710 * CHANNEL_A_NR returns either 0 (in case of
1711 * DECstations) or 1 (in case of Baget).
1712 */
1713 if (CHANNEL_A_NR == channel)
1714 zs_soft[n_channels].zs_chan_a =
1715 &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1716 else
1717 zs_soft[n_channels].zs_chan_a =
1718 &zs_channels[n_channels];
1719
1720 *pp = &zs_soft[n_channels];
1721 pp = &zs_soft[n_channels].zs_next;
1722 n_channels++;
1723 }
1724 }
1725
1726 *pp = 0;
1727 zs_channels_found = n_channels;
1728
1729 for (n = 0; n < zs_channels_found; n++) {
1730 for (i = 0; i < 16; i++) {
1731 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1732 }
1733 }
1734
1735 save_and_cli(flags);
1736 for (n = 0; n < zs_channels_found; n++) {
1737 if (n % 2 == 0) {
1738 write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1739 udelay(10);
1740 write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1741 }
1742 load_zsregs(zs_soft[n].zs_channel,
1743 zs_soft[n].zs_channel->curregs);
1744 }
1745 restore_flags(flags);
1746 }
1747
1748 static struct tty_operations serial_ops = {
1749 .open = rs_open,
1750 .close = rs_close,
1751 .write = rs_write,
1752 .flush_chars = rs_flush_chars,
1753 .write_room = rs_write_room,
1754 .chars_in_buffer = rs_chars_in_buffer,
1755 .flush_buffer = rs_flush_buffer,
1756 .ioctl = rs_ioctl,
1757 .throttle = rs_throttle,
1758 .unthrottle = rs_unthrottle,
1759 .set_termios = rs_set_termios,
1760 .stop = rs_stop,
1761 .start = rs_start,
1762 .hangup = rs_hangup,
1763 .break_ctl = rs_break,
1764 .wait_until_sent = rs_wait_until_sent,
1765 .tiocmget = rs_tiocmget,
1766 .tiocmset = rs_tiocmset,
1767 };
1768
1769 /* zs_init inits the driver */
1770 int __init zs_init(void)
1771 {
1772 int channel, i;
1773 struct dec_serial *info;
1774
1775 if(!BUS_PRESENT)
1776 return -ENODEV;
1777
1778 /* Setup base handler, and timer table. */
1779 init_bh(SERIAL_BH, do_serial_bh);
1780
1781 /* Find out how many Z8530 SCCs we have */
1782 if (zs_chain == 0)
1783 probe_sccs();
1784 serial_driver = alloc_tty_driver(zs_channels_found);
1785 if (!serial_driver)
1786 return -ENOMEM;
1787
1788 show_serial_version();
1789
1790 /* Initialize the tty_driver structure */
1791 /* Not all of this is exactly right for us. */
1792
1793 serial_driver->owner = THIS_MODULE;
1794 serial_driver->devfs_name = "tts/";
1795 serial_driver->name = "ttyS";
1796 serial_driver->major = TTY_MAJOR;
1797 serial_driver->minor_start = 64;
1798 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1799 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1800 serial_driver->init_termios = tty_std_termios;
1801 serial_driver->init_termios.c_cflag =
1802 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1803 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1804 tty_set_operations(serial_driver, &serial_ops);
1805
1806 if (tty_register_driver(serial_driver))
1807 panic("Couldn't register serial driver");
1808
1809 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1810
1811 /* Needed before interrupts are enabled. */
1812 info->tty = 0;
1813 info->x_char = 0;
1814
1815 if (info->hook && info->hook->init_info) {
1816 (*info->hook->init_info)(info);
1817 continue;
1818 }
1819
1820 info->magic = SERIAL_MAGIC;
1821 info->port = (int) info->zs_channel->control;
1822 info->line = i;
1823 info->custom_divisor = 16;
1824 info->close_delay = 50;
1825 info->closing_wait = 3000;
1826 info->event = 0;
1827 info->count = 0;
1828 info->blocked_open = 0;
1829 info->tqueue.routine = do_softint;
1830 info->tqueue.data = info;
1831 init_waitqueue_head(&info->open_wait);
1832 init_waitqueue_head(&info->close_wait);
1833 printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
1834 info->line, info->port, info->irq);
1835 tty_register_device(serial_driver, info->line, NULL);
1836
1837 }
1838
1839 for (channel = 0; channel < zs_channels_found; ++channel) {
1840 zs_soft[channel].clk_divisor = 16;
1841 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1842
1843 if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1844 "scc", &zs_soft[channel]))
1845 printk(KERN_ERR "decserial: can't get irq %d\n",
1846 zs_soft[channel].irq);
1847
1848 if (zs_soft[channel].hook) {
1849 zs_startup(&zs_soft[channel]);
1850 if (zs_soft[channel].hook->init_channel)
1851 (*zs_soft[channel].hook->init_channel)
1852 (&zs_soft[channel]);
1853 }
1854 }
1855
1856 return 0;
1857 }
1858
1859 /*
1860 * polling I/O routines
1861 */
1862 static int
1863 zs_poll_tx_char(void *handle, unsigned char ch)
1864 {
1865 struct dec_serial *info = handle;
1866 struct dec_zschannel *chan = info->zs_channel;
1867 int ret;
1868
1869 if(chan) {
1870 int loops = 10000;
1871
1872 while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
1873 loops--;
1874
1875 if (loops) {
1876 write_zsdata(chan, ch);
1877 ret = 0;
1878 } else
1879 ret = -EAGAIN;
1880
1881 return ret;
1882 } else
1883 return -ENODEV;
1884 }
1885
1886 static int
1887 zs_poll_rx_char(void *handle)
1888 {
1889 struct dec_serial *info = handle;
1890 struct dec_zschannel *chan = info->zs_channel;
1891 int ret;
1892
1893 if(chan) {
1894 int loops = 10000;
1895
1896 while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
1897 loops--;
1898
1899 if (loops)
1900 ret = read_zsdata(chan);
1901 else
1902 ret = -EAGAIN;
1903
1904 return ret;
1905 } else
1906 return -ENODEV;
1907 }
1908
1909 int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
1910 {
1911 struct dec_serial *info = &zs_soft[channel];
1912
1913 if (info->hook) {
1914 printk("%s: line %d has already a hook registered\n",
1915 __FUNCTION__, channel);
1916
1917 return 0;
1918 } else {
1919 hook->poll_rx_char = zs_poll_rx_char;
1920 hook->poll_tx_char = zs_poll_tx_char;
1921 info->hook = hook;
1922
1923 return 1;
1924 }
1925 }
1926
1927 int unregister_zs_hook(unsigned int channel)
1928 {
1929 struct dec_serial *info = &zs_soft[channel];
1930
1931 if (info->hook) {
1932 info->hook = NULL;
1933 return 1;
1934 } else {
1935 printk("%s: trying to unregister hook on line %d,"
1936 " but none is registered\n", __FUNCTION__, channel);
1937 return 0;
1938 }
1939 }
1940
1941 /*
1942 * ------------------------------------------------------------
1943 * Serial console driver
1944 * ------------------------------------------------------------
1945 */
1946 #ifdef CONFIG_SERIAL_DEC_CONSOLE
1947
1948
1949 /*
1950 * Print a string to the serial port trying not to disturb
1951 * any possible real use of the port...
1952 */
1953 static void serial_console_write(struct console *co, const char *s,
1954 unsigned count)
1955 {
1956 struct dec_serial *info;
1957 int i;
1958
1959 info = zs_soft + co->index;
1960
1961 for (i = 0; i < count; i++, s++) {
1962 if(*s == '\n')
1963 zs_poll_tx_char(info, '\r');
1964 zs_poll_tx_char(info, *s);
1965 }
1966 }
1967
1968 static struct tty_driver *serial_console_device(struct console *c, int *index)
1969 {
1970 *index = c->index;
1971 return serial_driver;
1972 }
1973
1974 /*
1975 * Setup initial baud/bits/parity. We do two things here:
1976 * - construct a cflag setting for the first rs_open()
1977 * - initialize the serial port
1978 * Return non-zero if we didn't find a serial port.
1979 */
1980 static int __init serial_console_setup(struct console *co, char *options)
1981 {
1982 struct dec_serial *info;
1983 int baud = 9600;
1984 int bits = 8;
1985 int parity = 'n';
1986 int cflag = CREAD | HUPCL | CLOCAL;
1987 int clk_divisor = 16;
1988 int brg;
1989 char *s;
1990 unsigned long flags;
1991
1992 if(!BUS_PRESENT)
1993 return -ENODEV;
1994
1995 info = zs_soft + co->index;
1996
1997 if (zs_chain == 0)
1998 probe_sccs();
1999
2000 info->is_cons = 1;
2001
2002 if (options) {
2003 baud = simple_strtoul(options, NULL, 10);
2004 s = options;
2005 while(*s >= '' && *s <= '9')
2006 s++;
2007 if (*s)
2008 parity = *s++;
2009 if (*s)
2010 bits = *s - '';
2011 }
2012
2013 /*
2014 * Now construct a cflag setting.
2015 */
2016 switch(baud) {
2017 case 1200:
2018 cflag |= B1200;
2019 break;
2020 case 2400:
2021 cflag |= B2400;
2022 break;
2023 case 4800:
2024 cflag |= B4800;
2025 break;
2026 case 19200:
2027 cflag |= B19200;
2028 break;
2029 case 38400:
2030 cflag |= B38400;
2031 break;
2032 case 57600:
2033 cflag |= B57600;
2034 break;
2035 case 115200:
2036 cflag |= B115200;
2037 break;
2038 case 9600:
2039 default:
2040 cflag |= B9600;
2041 /*
2042 * Set this to a sane value to prevent a divide error.
2043 */
2044 baud = 9600;
2045 break;
2046 }
2047 switch(bits) {
2048 case 7:
2049 cflag |= CS7;
2050 break;
2051 default:
2052 case 8:
2053 cflag |= CS8;
2054 break;
2055 }
2056 switch(parity) {
2057 case 'o': case 'O':
2058 cflag |= PARODD;
2059 break;
2060 case 'e': case 'E':
2061 cflag |= PARENB;
2062 break;
2063 }
2064 co->cflag = cflag;
2065
2066 save_and_cli(flags);
2067
2068 /*
2069 * Set up the baud rate generator.
2070 */
2071 brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
2072 info->zs_channel->curregs[R12] = (brg & 255);
2073 info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
2074
2075 /*
2076 * Set byte size and parity.
2077 */
2078 if (bits == 7) {
2079 info->zs_channel->curregs[R3] |= Rx7;
2080 info->zs_channel->curregs[R5] |= Tx7;
2081 } else {
2082 info->zs_channel->curregs[R3] |= Rx8;
2083 info->zs_channel->curregs[R5] |= Tx8;
2084 }
2085 if (cflag & PARENB) {
2086 info->zs_channel->curregs[R4] |= PAR_ENA;
2087 }
2088 if (!(cflag & PARODD)) {
2089 info->zs_channel->curregs[R4] |= PAR_EVEN;
2090 }
2091 info->zs_channel->curregs[R4] |= SB1;
2092
2093 /*
2094 * Turn on RTS and DTR.
2095 */
2096 zs_rtsdtr(info, RTS | DTR, 1);
2097
2098 /*
2099 * Finally, enable sequencing.
2100 */
2101 info->zs_channel->curregs[R3] |= RxENABLE;
2102 info->zs_channel->curregs[R5] |= TxENAB;
2103
2104 /*
2105 * Clear the interrupt registers.
2106 */
2107 write_zsreg(info->zs_channel, R0, ERR_RES);
2108 write_zsreg(info->zs_channel, R0, RES_H_IUS);
2109
2110 /*
2111 * Load up the new values.
2112 */
2113 load_zsregs(info->zs_channel, info->zs_channel->curregs);
2114
2115 /* Save the current value of RR0 */
2116 info->read_reg_zero = read_zsreg(info->zs_channel, R0);
2117
2118 zs_soft[co->index].clk_divisor = clk_divisor;
2119 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2120
2121 restore_flags(flags);
2122
2123 return 0;
2124 }
2125
2126 static struct console sercons = {
2127 .name = "ttyS",
2128 .write = serial_console_write,
2129 .device = serial_console_device,
2130 .setup = serial_console_setup,
2131 .flags = CON_PRINTBUFFER,
2132 .index = -1,
2133 };
2134
2135 /*
2136 * Register console.
2137 */
2138 void __init zs_serial_console_init(void)
2139 {
2140 register_console(&sercons);
2141 }
2142 #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2143
2144 #ifdef CONFIG_KGDB
2145 struct dec_zschannel *zs_kgdbchan;
2146 static unsigned char scc_inittab[] = {
2147 9, 0x80, /* reset A side (CHRA) */
2148 13, 0, /* set baud rate divisor */
2149 12, 1,
2150 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
2151 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
2152 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2153 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
2154 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
2155 };
2156
2157 /* These are for receiving and sending characters under the kgdb
2158 * source level kernel debugger.
2159 */
2160 void putDebugChar(char kgdb_char)
2161 {
2162 struct dec_zschannel *chan = zs_kgdbchan;
2163 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2164 RECOVERY_DELAY;
2165 write_zsdata(chan, kgdb_char);
2166 }
2167 char getDebugChar(void)
2168 {
2169 struct dec_zschannel *chan = zs_kgdbchan;
2170 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2171 eieio(); /*barrier();*/
2172 return read_zsdata(chan);
2173 }
2174 void kgdb_interruptible(int yes)
2175 {
2176 struct dec_zschannel *chan = zs_kgdbchan;
2177 int one, nine;
2178 nine = read_zsreg(chan, 9);
2179 if (yes == 1) {
2180 one = EXT_INT_ENAB|RxINT_ALL;
2181 nine |= MIE;
2182 printk("turning serial ints on\n");
2183 } else {
2184 one = RxINT_DISAB;
2185 nine &= ~MIE;
2186 printk("turning serial ints off\n");
2187 }
2188 write_zsreg(chan, 1, one);
2189 write_zsreg(chan, 9, nine);
2190 }
2191
2192 static int kgdbhook_init_channel(void *handle)
2193 {
2194 return 0;
2195 }
2196
2197 static void kgdbhook_init_info(void *handle)
2198 {
2199 }
2200
2201 static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
2202 {
2203 struct dec_serial *info = handle;
2204
2205 if (fl != TTY_NORMAL)
2206 return;
2207 if (ch == 0x03 || ch == '$')
2208 breakpoint();
2209 }
2210
2211 /* This sets up the serial port we're using, and turns on
2212 * interrupts for that channel, so kgdb is usable once we're done.
2213 */
2214 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2215 {
2216 int brg;
2217 int i, x;
2218 volatile char *sccc = ms->control;
2219 brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2220 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2221 for (i = 20000; i != 0; --i) {
2222 x = *sccc; eieio();
2223 }
2224 for (i = 0; i < sizeof(scc_inittab); ++i) {
2225 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2226 i++;
2227 }
2228 }
2229 /* This is called at boot time to prime the kgdb serial debugging
2230 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2231 * for /dev/ttyb which is determined in setup_arch() from the
2232 * boot command line flags.
2233 */
2234 struct dec_serial_hook zs_kgdbhook = {
2235 .init_channel = kgdbhook_init_channel,
2236 .init_info = kgdbhook_init_info,
2237 .rx_char = kgdbhook_rx_char,
2238 .cflags = B38400 | CS8 | CLOCAL,
2239 }
2240
2241 void __init zs_kgdb_hook(int tty_num)
2242 {
2243 /* Find out how many Z8530 SCCs we have */
2244 if (zs_chain == 0)
2245 probe_sccs();
2246 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2247 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2248 zs_soft[tty_num].change_needed = 0;
2249 zs_soft[tty_num].clk_divisor = 16;
2250 zs_soft[tty_num].zs_baud = 38400;
2251 zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2252 /* Turn on transmitter/receiver at 8-bits/char */
2253 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2254 printk("KGDB: on channel %d initialized\n", tty_num);
2255 set_debug_traps(); /* init stub */
2256 }
2257 #endif /* ifdef CONFIG_KGDB */
2258
2259
2260
|
This page was automatically generated by the
LXR engine.
|