Linux 2.3.1pre3
[davej-history.git] / drivers / sbus / char / sab82532.c
blob82c7e61e06e9c2fb17359f96a2c4573f356b17a6
1 /* $Id: sab82532.c,v 1.31 1999/05/12 11:15:10 davem Exp $
2 * sab82532.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
6 */
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/timer.h>
14 #include <linux/interrupt.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/serial_reg.h>
19 #include <linux/console.h>
20 #include <linux/major.h>
21 #include <linux/string.h>
22 #include <linux/fcntl.h>
23 #include <linux/ptrace.h>
24 #include <linux/ioport.h>
25 #include <linux/mm.h>
26 #include <linux/malloc.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <asm/sab82532.h>
31 #include <asm/uaccess.h>
32 #include <asm/ebus.h>
33 #include <asm/irq.h>
35 #include "sunserial.h"
37 static DECLARE_TASK_QUEUE(tq_serial);
39 static struct tty_driver serial_driver, callout_driver;
40 static int sab82532_refcount;
42 /* number of characters left in xmit buffer before we ask for more */
43 #define WAKEUP_CHARS 256
45 #define SERIAL_PARANOIA_CHECK
46 #define SERIAL_DO_RESTART
48 /* Set of debugging defines */
49 #undef SERIAL_DEBUG_OPEN
50 #undef SERIAL_DEBUG_FLOW
51 #undef SERIAL_DEBUG_WAIT_UNTIL_SENT
52 #undef SERIAL_DEBUG_SEND_BREAK
53 #undef SERIAL_DEBUG_INTR
55 /* Trace things on serial device, useful for console debugging: */
56 #undef SERIAL_LOG_DEVICE
58 #ifdef SERIAL_LOG_DEVICE
59 static void dprint_init(int tty);
60 #endif
62 static void change_speed(struct sab82532 *info);
63 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout);
66 * This assumes you have a 29.4912 MHz clock for your UART.
68 #define BASE_BAUD ( 29491200 / 16 )
70 static struct sab82532 *sab82532_chain = 0;
71 static struct tty_struct *sab82532_table[NR_PORTS];
72 static struct termios *sab82532_termios[NR_PORTS];
73 static struct termios *sab82532_termios_locked[NR_PORTS];
75 #ifdef CONFIG_SERIAL_CONSOLE
76 extern int serial_console;
77 static struct console sab82532_console;
78 static int sab82532_console_init(void);
79 #endif
81 #ifndef MIN
82 #define MIN(a,b) ((a) < (b) ? (a) : (b))
83 #endif
85 static char *sab82532_version[16] = {
86 "V1.0", "V2.0", "V3.2", "V(0x03)",
87 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
88 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
89 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
91 static char serial_version[16];
94 * tmp_buf is used as a temporary buffer by sab82532_write. We need to
95 * lock it in case the copy_from_user blocks while swapping in a page,
96 * and some other program tries to do a serial write at the same time.
97 * Since the lock will only come under contention when the system is
98 * swapping and available memory is low, it makes sense to share one
99 * buffer across all the serial ports, since it significantly saves
100 * memory if large numbers of serial ports are open.
102 static unsigned char *tmp_buf = 0;
103 static DECLARE_MUTEX(tmp_buf_sem);
105 static inline int serial_paranoia_check(struct sab82532 *info,
106 kdev_t device, const char *routine)
108 #ifdef SERIAL_PARANOIA_CHECK
109 static const char *badmagic =
110 "Warning: bad magic number for serial struct (%s) in %s\n";
111 static const char *badinfo =
112 "Warning: null sab82532 for (%s) in %s\n";
114 if (!info) {
115 printk(badinfo, kdevname(device), routine);
116 return 1;
118 if (info->magic != SERIAL_MAGIC) {
119 printk(badmagic, kdevname(device), routine);
120 return 1;
122 #endif
123 return 0;
127 * This is used to figure out the divisor speeds.
129 * The formula is: Baud = BASE_BAUD / ((N + 1) * (1 << M)),
131 * with 0 <= N < 64 and 0 <= M < 16
133 * XXX: Speeds with M = 0 might not work properly for XTAL frequencies
134 * above 10 MHz.
136 struct ebrg_struct {
137 int baud;
138 int n;
139 int m;
142 static struct ebrg_struct ebrg_table[] = {
143 { 0, 0, 0 },
144 { 50, 35, 10 },
145 { 75, 47, 9 },
146 { 110, 32, 9 },
147 { 134, 53, 8 },
148 { 150, 47, 8 },
149 { 200, 35, 8 },
150 { 300, 47, 7 },
151 { 600, 47, 6 },
152 { 1200, 47, 5 },
153 { 1800, 31, 5 },
154 { 2400, 47, 4 },
155 { 4800, 47, 3 },
156 { 9600, 47, 2 },
157 { 19200, 47, 1 },
158 { 38400, 23, 1 },
159 { 57600, 15, 1 },
160 { 115200, 7, 1 },
161 { 230400, 3, 1 },
162 { 460800, 1, 1 },
163 { 76800, 11, 1 },
164 { 153600, 5, 1 },
165 { 307200, 3, 1 },
166 { 614400, 3, 0 },
167 { 921600, 0, 1 },
170 #define NR_EBRG_VALUES (sizeof(ebrg_table)/sizeof(struct ebrg_struct))
172 #define SAB82532_MAX_TEC_DELAY 2000 /* 2 ms */
174 static __inline__ void sab82532_tec_wait(struct sab82532 *info)
176 int count = SAB82532_MAX_TEC_DELAY;
177 while ((info->regs->r.star & SAB82532_STAR_TEC) && --count)
178 udelay(1);
181 static __inline__ void sab82532_start_tx(struct sab82532 *info)
183 unsigned long flags;
184 int i;
186 save_flags(flags); cli();
188 if (info->xmit_cnt <= 0)
189 goto out;
191 if (!(info->regs->r.star & SAB82532_STAR_XFW))
192 goto out;
194 info->all_sent = 0;
195 for (i = 0; i < info->xmit_fifo_size; i++) {
196 info->regs->w.xfifo[i] = info->xmit_buf[info->xmit_tail++];
197 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
198 info->icount.tx++;
199 if (--info->xmit_cnt <= 0)
200 break;
203 /* Issue a Transmit Frame command. */
204 if (info->regs->r.star & SAB82532_STAR_CEC)
205 udelay(1);
206 info->regs->w.cmdr = SAB82532_CMDR_XF;
208 out:
209 restore_flags(flags);
214 * ------------------------------------------------------------
215 * sab82532_stop() and sab82532_start()
217 * This routines are called before setting or resetting tty->stopped.
218 * They enable or disable transmitter interrupts, as necessary.
219 * ------------------------------------------------------------
221 static void sab82532_stop(struct tty_struct *tty)
223 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
224 unsigned long flags;
226 if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
227 return;
229 save_flags(flags); cli();
230 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
231 info->regs->w.imr1 = info->interrupt_mask1;
232 restore_flags(flags);
235 static void sab82532_start(struct tty_struct *tty)
237 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
238 unsigned long flags;
240 if (serial_paranoia_check(info, tty->device, "sab82532_start"))
241 return;
243 save_flags(flags); cli();
244 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
245 info->regs->w.imr1 = info->interrupt_mask1;
246 sab82532_start_tx(info);
247 restore_flags(flags);
250 static void batten_down_hatches(struct sab82532 *info)
252 unsigned char saved_rfc;
254 /* If we are doing kadb, we call the debugger
255 * else we just drop into the boot monitor.
256 * Note that we must flush the user windows
257 * first before giving up control.
259 printk("\n");
260 flush_user_windows();
263 * Set FIFO to single character mode.
265 saved_rfc = info->regs->r.rfc;
266 info->regs->rw.rfc &= ~(SAB82532_RFC_RFDF);
267 if (info->regs->r.star & SAB82532_STAR_CEC)
268 udelay(1);
269 info->regs->w.cmdr = SAB82532_CMDR_RRES;
271 #ifndef __sparc_v9__
272 if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
273 (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
274 sp_enter_debugger();
275 else
276 #endif
277 prom_cmdline();
280 * Reset FIFO to character + status mode.
282 info->regs->w.rfc = saved_rfc;
283 if (info->regs->r.star & SAB82532_STAR_CEC)
284 udelay(1);
285 info->regs->w.cmdr = SAB82532_CMDR_RRES;
289 * ----------------------------------------------------------------------
291 * Here starts the interrupt handling routines. All of the following
292 * subroutines are declared as inline and are folded into
293 * sab82532_interrupt(). They were separated out for readability's sake.
295 * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
296 * runs with interrupts turned off. People who may want to modify
297 * sab82532_interrupt() should try to keep the interrupt handler as fast as
298 * possible. After you are done making modifications, it is not a bad
299 * idea to do:
301 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
303 * and look at the resulting assemble code in serial.s.
305 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
306 * -----------------------------------------------------------------------
310 * This routine is used by the interrupt handler to schedule
311 * processing in the software interrupt portion of the driver.
313 static inline void sab82532_sched_event(struct sab82532 *info, int event)
315 info->event |= 1 << event;
316 queue_task(&info->tqueue, &tq_serial);
317 mark_bh(SERIAL_BH);
320 static inline void receive_chars(struct sab82532 *info,
321 union sab82532_irq_status *stat)
323 struct tty_struct *tty = info->tty;
324 unsigned char buf[32];
325 unsigned char status;
326 int free_fifo = 0;
327 int i, count = 0;
329 /* Read number of BYTES (Character + Status) available. */
330 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
331 count = info->recv_fifo_size;
332 free_fifo++;
335 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
336 count = info->regs->r.rbcl & (info->recv_fifo_size - 1);
337 free_fifo++;
340 /* Issue a FIFO read command in case we where idle. */
341 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
342 if (info->regs->r.star & SAB82532_STAR_CEC)
343 udelay(1);
344 info->regs->w.cmdr = SAB82532_CMDR_RFRD;
347 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
348 #if 1
349 printk("sab82532: receive_chars: RFO");
350 #endif
351 free_fifo++;
354 /* Read the FIFO. */
355 for (i = 0; i < count; i++)
356 buf[i] = info->regs->r.rfifo[i];
358 /* Issue Receive Message Complete command. */
359 if (free_fifo) {
360 if (info->regs->r.star & SAB82532_STAR_CEC)
361 udelay(1);
362 info->regs->w.cmdr = SAB82532_CMDR_RMC;
365 if (info->is_console)
366 wake_up(&keypress_wait);
367 if (!tty)
368 return;
370 for (i = 0; i < count; ) {
371 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
372 #if 1
373 printk("sab82532: receive_chars: tty overrun\n");
374 #endif
375 info->icount.buf_overrun++;
376 break;
379 tty->flip.count++;
380 *tty->flip.char_buf_ptr++ = buf[i++];
381 status = buf[i++];
382 info->icount.rx++;
384 #ifdef SERIAL_DEBUG_INTR
385 printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
386 #endif
388 if (status & SAB82532_RSTAT_PE) {
389 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
390 info->icount.parity++;
391 } else if (status & SAB82532_RSTAT_FE) {
392 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
393 info->icount.frame++;
395 #ifdef CMSPAR
396 else if (status & SAB82532_RSTAT_PARITY)
397 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
398 #endif
399 else
400 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
403 queue_task(&tty->flip.tqueue, &tq_timer);
406 static inline void transmit_chars(struct sab82532 *info,
407 union sab82532_irq_status *stat)
409 int i;
411 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS)
412 info->all_sent = 1;
413 if (!(info->regs->r.star & SAB82532_STAR_XFW))
414 return;
416 if (!info->tty) {
417 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
418 info->regs->w.imr1 = info->interrupt_mask1;
419 return;
422 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
423 info->tty->hw_stopped) {
424 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
425 info->regs->w.imr1 = info->interrupt_mask1;
426 return;
429 /* Stuff 32 bytes into Transmit FIFO. */
430 info->all_sent = 0;
431 for (i = 0; i < info->xmit_fifo_size; i++) {
432 info->regs->w.xfifo[i] = info->xmit_buf[info->xmit_tail++];
433 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
434 info->icount.tx++;
435 if (--info->xmit_cnt <= 0)
436 break;
439 /* Issue a Transmit Frame command. */
440 if (info->regs->r.star & SAB82532_STAR_CEC)
441 udelay(1);
442 info->regs->w.cmdr = SAB82532_CMDR_XF;
444 if (info->xmit_cnt < WAKEUP_CHARS)
445 sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
447 #ifdef SERIAL_DEBUG_INTR
448 printk("THRE...");
449 #endif
450 if (info->xmit_cnt <= 0) {
451 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
452 info->regs->w.imr1 = info->interrupt_mask1;
456 static inline void check_status(struct sab82532 *info,
457 union sab82532_irq_status *stat)
459 struct tty_struct *tty = info->tty;
460 int modem_change = 0;
462 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
463 if (info->is_console) {
464 batten_down_hatches(info);
465 return;
467 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
468 info->icount.buf_overrun++;
469 goto check_modem;
471 tty->flip.count++;
472 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
473 *tty->flip.char_buf_ptr++ = 0;
474 info->icount.brk++;
477 if (!tty)
478 return;
480 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
481 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
482 info->icount.buf_overrun++;
483 goto check_modem;
485 tty->flip.count++;
486 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
487 *tty->flip.char_buf_ptr++ = 0;
488 info->icount.overrun++;
491 check_modem:
492 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
493 info->dcd = (info->regs->r.vstr & SAB82532_VSTR_CD) ? 0 : 1;
494 info->icount.dcd++;
495 modem_change++;
496 #if 0
497 printk("DCD change: %d\n", info->icount.dcd);
498 #endif
500 if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
501 info->cts = info->regs->r.star & SAB82532_STAR_CTS;
502 info->icount.cts++;
503 modem_change++;
504 #if 0
505 printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
506 #endif
508 if ((info->regs->r.pvr & info->pvr_dsr_bit) ^ info->dsr) {
509 info->dsr = (info->regs->r.pvr & info->pvr_dsr_bit) ? 0 : 1;
510 info->icount.dsr++;
511 modem_change++;
512 #if 0
513 printk("DSR change: %d\n", info->icount.dsr);
514 #endif
516 if (modem_change)
517 wake_up_interruptible(&info->delta_msr_wait);
519 if ((info->flags & ASYNC_CHECK_CD) &&
520 (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
522 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
523 printk("ttys%d CD now %s...", info->line,
524 (info->dcd) ? "on" : "off");
525 #endif
527 if (info->dcd)
528 wake_up_interruptible(&info->open_wait);
529 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
530 (info->flags & ASYNC_CALLOUT_NOHUP))) {
532 #ifdef SERIAL_DEBUG_OPEN
533 printk("scheduling hangup...");
534 #endif
536 queue_task(&info->tqueue_hangup, &tq_scheduler);
540 if (info->flags & ASYNC_CTS_FLOW) {
541 if (info->tty->hw_stopped) {
542 if (info->cts) {
544 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
545 printk("CTS tx start...");
546 #endif
547 info->tty->hw_stopped = 0;
548 sab82532_sched_event(info,
549 RS_EVENT_WRITE_WAKEUP);
550 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
551 info->regs->w.imr1 = info->interrupt_mask1;
552 sab82532_start_tx(info);
554 } else {
555 if (!(info->cts)) {
557 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
558 printk("CTS tx stop...");
559 #endif
560 info->tty->hw_stopped = 1;
567 * This is the serial driver's generic interrupt routine
569 static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
571 struct sab82532 *info = dev_id;
572 union sab82532_irq_status status;
574 #ifdef SERIAL_DEBUG_INTR
575 printk("sab82532_interrupt(%d)...", irq);
576 #endif
578 status.stat = 0;
579 if (info->regs->r.gis & SAB82532_GIS_ISA0)
580 status.sreg.isr0 = info->regs->r.isr0;
581 if (info->regs->r.gis & SAB82532_GIS_ISA1)
582 status.sreg.isr1 = info->regs->r.isr1;
584 #ifdef SERIAL_DEBUG_INTR
585 printk("%d<%02x.%02x>", info->line,
586 status.sreg.isr0, status.sreg.isr1);
587 #endif
589 if (!status.stat)
590 goto next;
592 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
593 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
594 receive_chars(info, &status);
595 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
596 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
597 check_status(info, &status);
598 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
599 transmit_chars(info, &status);
601 next:
602 info = info->next;
603 status.stat = 0;
604 if (info->regs->r.gis & SAB82532_GIS_ISB0)
605 status.sreg.isr0 = info->regs->r.isr0;
606 if (info->regs->r.gis & SAB82532_GIS_ISB1)
607 status.sreg.isr1 = info->regs->r.isr1;
609 #ifdef SERIAL_DEBUG_INTR
610 printk("%d<%02x.%02x>", info->line,
611 status.sreg.isr0, status.sreg.isr1);
612 #endif
614 if (!status.stat)
615 goto done;
617 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
618 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
619 receive_chars(info, &status);
620 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
621 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
622 check_status(info, &status);
623 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
624 transmit_chars(info, &status);
626 done:
627 #ifdef SERIAL_DEBUG_INTR
628 printk("end.\n");
629 #endif
633 * -------------------------------------------------------------------
634 * Here ends the serial interrupt routines.
635 * -------------------------------------------------------------------
639 * This routine is used to handle the "bottom half" processing for the
640 * serial driver, known also the "software interrupt" processing.
641 * This processing is done at the kernel interrupt level, after the
642 * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
643 * is where time-consuming activities which can not be done in the
644 * interrupt driver proper are done; the interrupt driver schedules
645 * them using sab82532_sched_event(), and they get done here.
647 static void do_serial_bh(void)
649 run_task_queue(&tq_serial);
652 static void do_softint(void *private_)
654 struct sab82532 *info = (struct sab82532 *)private_;
655 struct tty_struct *tty;
657 tty = info->tty;
658 if (!tty)
659 return;
661 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
662 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
663 tty->ldisc.write_wakeup)
664 (tty->ldisc.write_wakeup)(tty);
665 wake_up_interruptible(&tty->write_wait);
670 * This routine is called from the scheduler tqueue when the interrupt
671 * routine has signalled that a hangup has occurred. The path of
672 * hangup processing is:
674 * serial interrupt routine -> (scheduler tqueue) ->
675 * do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
678 static void do_serial_hangup(void *private_)
680 struct sab82532 *info = (struct sab82532 *) private_;
681 struct tty_struct *tty;
683 tty = info->tty;
684 if (!tty)
685 return;
687 tty_hangup(tty);
690 static void
691 sab82532_init_line(struct sab82532 *info)
693 unsigned char stat;
696 * Wait for any commands or immediate characters
698 if (info->regs->r.star & SAB82532_STAR_CEC)
699 udelay(1);
700 sab82532_tec_wait(info);
703 * Clear the FIFO buffers.
705 if (info->regs->r.star & SAB82532_STAR_CEC)
706 udelay(1);
707 info->regs->w.cmdr = SAB82532_CMDR_RRES;
708 if (info->regs->r.star & SAB82532_STAR_CEC)
709 udelay(1);
710 info->regs->w.cmdr = SAB82532_CMDR_XRES;
713 * Clear the interrupt registers.
715 stat = info->regs->r.isr0;
716 stat = info->regs->r.isr1;
719 * Now, initialize the UART
721 info->regs->w.ccr0 = 0; /* power-down */
722 info->regs->w.ccr0 = SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
723 SAB82532_CCR0_SM_ASYNC;
724 info->regs->w.ccr1 = SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7;
725 info->regs->w.ccr2 = SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
726 SAB82532_CCR2_TOE;
727 info->regs->w.ccr3 = 0;
728 info->regs->w.ccr4 = SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG;
729 info->regs->w.mode = SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
730 SAB82532_MODE_RAC;
731 info->regs->w.rfc = SAB82532_RFC_DPS | SAB82532_RFC_RFDF;
732 switch (info->recv_fifo_size) {
733 case 1:
734 info->regs->w.rfc |= SAB82532_RFC_RFTH_1;
735 break;
736 case 4:
737 info->regs->w.rfc |= SAB82532_RFC_RFTH_4;
738 break;
739 case 16:
740 info->regs->w.rfc |= SAB82532_RFC_RFTH_16;
741 break;
742 default:
743 info->recv_fifo_size = 32;
744 /* fall through */
745 case 32:
746 info->regs->w.rfc |= SAB82532_RFC_RFTH_32;
747 break;
749 info->regs->rw.ccr0 |= SAB82532_CCR0_PU; /* power-up */
752 static int startup(struct sab82532 *info)
754 unsigned long flags;
755 unsigned long page;
756 int retval = 0;
758 page = get_free_page(GFP_KERNEL);
759 if (!page)
760 return -ENOMEM;
762 save_flags(flags); cli();
764 if (info->flags & ASYNC_INITIALIZED) {
765 free_page(page);
766 goto errout;
769 if (!info->regs) {
770 if (info->tty)
771 set_bit(TTY_IO_ERROR, &info->tty->flags);
772 free_page(page);
773 retval = -ENODEV;
774 goto errout;
776 if (info->xmit_buf)
777 free_page(page);
778 else
779 info->xmit_buf = (unsigned char *)page;
781 #ifdef SERIAL_DEBUG_OPEN
782 printk("starting up serial port %d...", info->line);
783 #endif
786 * Initialize the Hardware
788 sab82532_init_line(info);
790 if (info->tty->termios->c_cflag & CBAUD) {
791 info->regs->rw.mode &= ~(SAB82532_MODE_FRTS);
792 info->regs->rw.mode |= SAB82532_MODE_RTS;
793 info->regs->rw.pvr &= ~(info->pvr_dtr_bit);
797 * Finally, enable interrupts
799 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
800 SAB82532_IMR0_PLLA;
801 info->regs->w.imr0 = info->interrupt_mask0;
802 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_XOFF |
803 SAB82532_IMR1_TIN | SAB82532_IMR1_XON |
804 SAB82532_IMR1_XPR;
805 info->regs->w.imr1 = info->interrupt_mask1;
807 if (info->tty)
808 clear_bit(TTY_IO_ERROR, &info->tty->flags);
809 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
812 * and set the speed of the serial port
814 change_speed(info);
816 info->flags |= ASYNC_INITIALIZED;
817 restore_flags(flags);
818 return 0;
820 errout:
821 restore_flags(flags);
822 return retval;
826 * This routine will shutdown a serial port; interrupts are disabled, and
827 * DTR is dropped if the hangup on close termio flag is on.
829 static void shutdown(struct sab82532 *info)
831 unsigned long flags;
833 if (!(info->flags & ASYNC_INITIALIZED))
834 return;
836 #ifdef SERIAL_DEBUG_OPEN
837 printk("Shutting down serial port %d...", info->line);
838 #endif
840 save_flags(flags); cli(); /* Disable interrupts */
843 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
844 * here so the queue might never be waken up
846 wake_up_interruptible(&info->delta_msr_wait);
848 if (info->xmit_buf) {
849 free_page((unsigned long)info->xmit_buf);
850 info->xmit_buf = 0;
853 if (info->is_console) {
854 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
855 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
856 info->regs->w.imr0 = info->interrupt_mask0;
857 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
858 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
859 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
860 SAB82532_IMR1_XPR;
861 info->regs->w.imr1 = info->interrupt_mask1;
862 if (info->tty)
863 set_bit(TTY_IO_ERROR, &info->tty->flags);
864 info->flags &= ~ASYNC_INITIALIZED;
865 restore_flags(flags);
866 return;
869 /* Disable Interrupts */
870 info->interrupt_mask0 = 0xff;
871 info->regs->w.imr0 = info->interrupt_mask0;
872 info->interrupt_mask1 = 0xff;
873 info->regs->w.imr1 = info->interrupt_mask1;
875 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
876 info->regs->rw.mode |= SAB82532_MODE_FRTS;
877 info->regs->rw.mode |= SAB82532_MODE_RTS;
878 info->regs->rw.pvr |= info->pvr_dtr_bit;
881 /* Disable break condition */
882 info->regs->rw.dafo &= ~(SAB82532_DAFO_XBRK);
884 /* Disable Receiver */
885 info->regs->rw.mode &= ~(SAB82532_MODE_RAC);
887 /* Power Down */
888 info->regs->rw.ccr0 &= ~(SAB82532_CCR0_PU);
890 if (info->tty)
891 set_bit(TTY_IO_ERROR, &info->tty->flags);
893 info->flags &= ~ASYNC_INITIALIZED;
894 restore_flags(flags);
898 * This routine is called to set the UART divisor registers to match
899 * the specified baud rate for a serial port.
901 static void change_speed(struct sab82532 *info)
903 unsigned long flags;
904 unsigned int ebrg;
905 tcflag_t cflag;
906 unsigned char dafo;
907 int i, bits;
909 if (!info->tty || !info->tty->termios)
910 return;
911 cflag = info->tty->termios->c_cflag;
913 /* Byte size and parity */
914 switch (cflag & CSIZE) {
915 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
916 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
917 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
918 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
919 /* Never happens, but GCC is too dumb to figure it out */
920 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
923 if (cflag & CSTOPB) {
924 dafo |= SAB82532_DAFO_STOP;
925 bits++;
928 if (cflag & PARENB) {
929 dafo |= SAB82532_DAFO_PARE;
930 bits++;
933 if (cflag & PARODD) {
934 #ifdef CMSPAR
935 if (cflag & CMSPAR)
936 dafo |= SAB82532_DAFO_PAR_MARK;
937 else
938 #endif
939 dafo |= SAB82532_DAFO_PAR_ODD;
940 } else {
941 #ifdef CMSPAR
942 if (cflag & CMSPAR)
943 dafo |= SAB82532_DAFO_PAR_SPACE;
944 else
945 #endif
946 dafo |= SAB82532_DAFO_PAR_EVEN;
949 /* Determine EBRG values based on baud rate */
950 i = cflag & CBAUD;
951 if (i & CBAUDEX) {
952 i &= ~(CBAUDEX);
953 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
954 info->tty->termios->c_cflag &= ~CBAUDEX;
955 else
956 i += 15;
958 ebrg = ebrg_table[i].n;
959 ebrg |= (ebrg_table[i].m << 6);
961 info->baud = ebrg_table[i].baud;
962 if (info->baud)
963 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
964 else
965 info->timeout = 0;
966 info->timeout += HZ / 50; /* Add .02 seconds of slop */
968 /* CTS flow control flags */
969 if (cflag & CRTSCTS)
970 info->flags |= ASYNC_CTS_FLOW;
971 else
972 info->flags &= ~(ASYNC_CTS_FLOW);
974 if (cflag & CLOCAL)
975 info->flags &= ~(ASYNC_CHECK_CD);
976 else
977 info->flags |= ASYNC_CHECK_CD;
978 if (info->tty)
979 info->tty->hw_stopped = 0;
982 * Set up parity check flag
983 * XXX: not implemented, yet.
985 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
988 * Characters to ignore
989 * XXX: not implemented, yet.
993 * !!! ignore all characters if CREAD is not set
994 * XXX: not implemented, yet.
996 if ((cflag & CREAD) == 0)
997 info->ignore_status_mask |= SAB82532_ISR0_RPF |
998 SAB82532_ISR0_TCD |
999 SAB82532_ISR0_TIME;
1001 save_flags(flags); cli();
1002 if (info->regs->r.star & SAB82532_STAR_CEC)
1003 udelay(1);
1004 sab82532_tec_wait(info);
1005 info->regs->w.dafo = dafo;
1006 info->regs->w.bgr = ebrg & 0xff;
1007 info->regs->rw.ccr2 &= ~(0xc0);
1008 info->regs->rw.ccr2 |= (ebrg >> 2) & 0xc0;
1009 if (info->flags & ASYNC_CTS_FLOW) {
1010 info->regs->rw.mode &= ~(SAB82532_MODE_RTS);
1011 info->regs->rw.mode |= SAB82532_MODE_FRTS;
1012 info->regs->rw.mode &= ~(SAB82532_MODE_FCTS);
1013 } else {
1014 info->regs->rw.mode |= SAB82532_MODE_RTS;
1015 info->regs->rw.mode &= ~(SAB82532_MODE_FRTS);
1016 info->regs->rw.mode |= SAB82532_MODE_FCTS;
1018 info->regs->rw.mode |= SAB82532_MODE_RAC;
1019 restore_flags(flags);
1022 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1024 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1025 unsigned long flags;
1027 if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1028 return;
1030 if (!tty || !info->xmit_buf)
1031 return;
1033 save_flags(flags); cli();
1034 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1035 restore_flags(flags);
1036 return;
1039 info->xmit_buf[info->xmit_head++] = ch;
1040 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1041 info->xmit_cnt++;
1042 restore_flags(flags);
1045 static void sab82532_flush_chars(struct tty_struct *tty)
1047 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1048 unsigned long flags;
1050 if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1051 return;
1053 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1054 !info->xmit_buf)
1055 return;
1057 save_flags(flags); cli();
1058 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1059 info->regs->w.imr1 = info->interrupt_mask1;
1060 sab82532_start_tx(info);
1061 restore_flags(flags);
1064 static int sab82532_write(struct tty_struct * tty, int from_user,
1065 const unsigned char *buf, int count)
1067 int c, ret = 0;
1068 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1069 unsigned long flags;
1071 if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1072 return 0;
1074 if (!tty || !info->xmit_buf || !tmp_buf)
1075 return 0;
1077 if (from_user)
1078 down(&tmp_buf_sem);
1079 save_flags(flags);
1080 while (1) {
1081 cli();
1082 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1083 SERIAL_XMIT_SIZE - info->xmit_head));
1084 if (c <= 0)
1085 break;
1087 if (from_user) {
1088 c -= copy_from_user(tmp_buf, buf, c);
1089 if (!c) {
1090 if (!ret)
1091 ret = -EFAULT;
1092 break;
1094 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1095 SERIAL_XMIT_SIZE - info->xmit_head));
1096 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1097 } else
1098 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1099 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1100 info->xmit_cnt += c;
1101 restore_flags(flags);
1102 buf += c;
1103 count -= c;
1104 ret += c;
1106 if (from_user)
1107 up(&tmp_buf_sem);
1109 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1110 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1111 info->regs->w.imr1 = info->interrupt_mask1;
1112 sab82532_start_tx(info);
1115 restore_flags(flags);
1116 return ret;
1119 static int sab82532_write_room(struct tty_struct *tty)
1121 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1122 int ret;
1124 if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1125 return 0;
1126 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1127 if (ret < 0)
1128 ret = 0;
1129 return ret;
1132 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1134 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1136 if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1137 return 0;
1138 return info->xmit_cnt;
1141 static void sab82532_flush_buffer(struct tty_struct *tty)
1143 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1145 if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1146 return;
1147 cli();
1148 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1149 sti();
1150 wake_up_interruptible(&tty->write_wait);
1151 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1152 tty->ldisc.write_wakeup)
1153 (tty->ldisc.write_wakeup)(tty);
1157 * This function is used to send a high-priority XON/XOFF character to
1158 * the device
1160 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1162 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1163 unsigned long flags;
1165 if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1166 return;
1168 save_flags(flags); cli();
1169 sab82532_tec_wait(info);
1170 info->regs->w.tic = ch;
1171 restore_flags(flags);
1175 * ------------------------------------------------------------
1176 * sab82532_throttle()
1178 * This routine is called by the upper-layer tty layer to signal that
1179 * incoming characters should be throttled.
1180 * ------------------------------------------------------------
1182 static void sab82532_throttle(struct tty_struct * tty)
1184 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1185 #ifdef SERIAL_DEBUG_THROTTLE
1186 char buf[64];
1188 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1189 tty->ldisc.chars_in_buffer(tty));
1190 #endif
1192 if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1193 return;
1195 if (I_IXOFF(tty))
1196 sab82532_send_xchar(tty, STOP_CHAR(tty));
1197 #if 0
1198 if (tty->termios->c_cflag & CRTSCTS)
1199 info->regs->rw.mode |= SAB82532_MODE_RTS;
1200 #endif
1203 static void sab82532_unthrottle(struct tty_struct * tty)
1205 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1206 #ifdef SERIAL_DEBUG_THROTTLE
1207 char buf[64];
1209 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1210 tty->ldisc.chars_in_buffer(tty));
1211 #endif
1213 if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1214 return;
1216 if (I_IXOFF(tty)) {
1217 if (info->x_char)
1218 info->x_char = 0;
1219 else
1220 sab82532_send_xchar(tty, START_CHAR(tty));
1223 #if 0
1224 if (tty->termios->c_cflag & CRTSCTS)
1225 info->regs->rw.mode &= ~(SAB82532_MODE_RTS);
1226 #endif
1230 * ------------------------------------------------------------
1231 * sab82532_ioctl() and friends
1232 * ------------------------------------------------------------
1235 static int get_serial_info(struct sab82532 *info,
1236 struct serial_struct *retinfo)
1238 struct serial_struct tmp;
1240 if (!retinfo)
1241 return -EFAULT;
1242 memset(&tmp, 0, sizeof(tmp));
1243 tmp.type = info->type;
1244 tmp.line = info->line;
1245 tmp.port = (unsigned long)info->regs;
1246 tmp.irq = info->irq;
1247 tmp.flags = info->flags;
1248 tmp.xmit_fifo_size = info->xmit_fifo_size;
1249 tmp.baud_base = info->baud_base;
1250 tmp.close_delay = info->close_delay;
1251 tmp.closing_wait = info->closing_wait;
1252 tmp.custom_divisor = info->custom_divisor;
1253 tmp.hub6 = 0;
1254 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1255 return -EFAULT;
1256 return 0;
1259 static int set_serial_info(struct sab82532 *info,
1260 struct serial_struct *new_info)
1262 return 0;
1267 * get_lsr_info - get line status register info
1269 * Purpose: Let user call ioctl() to get info when the UART physically
1270 * is emptied. On bus types like RS485, the transmitter must
1271 * release the bus after transmitting. This must be done when
1272 * the transmit shift register is empty, not be done when the
1273 * transmit holding register is empty. This functionality
1274 * allows an RS485 driver to be written in user space.
1276 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1278 unsigned int result;
1280 result = (!info->xmit_buf && info->all_sent) ? TIOCSER_TEMT : 0;
1281 return put_user(result, value);
1285 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1287 unsigned int result;
1289 result = ((info->regs->r.mode & SAB82532_MODE_RTS) ?
1290 ((info->regs->r.mode & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1291 : TIOCM_RTS)
1292 | ((info->regs->r.pvr & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1293 | ((info->regs->r.vstr & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1294 | ((info->regs->r.pvr & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1295 | ((info->regs->r.star & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1296 return put_user(result,value);
1299 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1300 unsigned int *value)
1302 int error;
1303 unsigned int arg;
1305 error = get_user(arg, value);
1306 if (error)
1307 return error;
1308 switch (cmd) {
1309 case TIOCMBIS:
1310 if (arg & TIOCM_RTS) {
1311 info->regs->rw.mode &= ~(SAB82532_MODE_FRTS);
1312 info->regs->rw.mode |= SAB82532_MODE_RTS;
1314 if (arg & TIOCM_DTR) {
1315 info->regs->rw.pvr &= ~(info->pvr_dtr_bit);
1317 break;
1318 case TIOCMBIC:
1319 if (arg & TIOCM_RTS) {
1320 info->regs->rw.mode |= SAB82532_MODE_FRTS;
1321 info->regs->rw.mode |= SAB82532_MODE_RTS;
1323 if (arg & TIOCM_DTR) {
1324 info->regs->rw.pvr |= info->pvr_dtr_bit;
1326 break;
1327 case TIOCMSET:
1328 if (arg & TIOCM_RTS) {
1329 info->regs->rw.mode &= ~(SAB82532_MODE_FRTS);
1330 info->regs->rw.mode |= SAB82532_MODE_RTS;
1331 } else {
1332 info->regs->rw.mode |= SAB82532_MODE_FRTS;
1333 info->regs->rw.mode |= SAB82532_MODE_RTS;
1335 if (arg & TIOCM_DTR) {
1336 info->regs->rw.pvr &= ~(info->pvr_dtr_bit);
1337 } else {
1338 info->regs->rw.pvr |= info->pvr_dtr_bit;
1340 break;
1341 default:
1342 return -EINVAL;
1344 return 0;
1348 * This routine sends a break character out the serial port.
1350 static void sab82532_break(struct tty_struct *tty, int break_state)
1352 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1353 unsigned long flags;
1355 if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1356 return;
1358 if (!info->regs)
1359 return;
1361 #ifdef SERIAL_DEBUG_SEND_BREAK
1362 printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1363 #endif
1364 save_flags(flags); cli();
1365 if (break_state == -1)
1366 info->regs->rw.dafo |= SAB82532_DAFO_XBRK;
1367 else
1368 info->regs->rw.dafo &= ~(SAB82532_DAFO_XBRK);
1369 restore_flags(flags);
1373 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1374 unsigned int cmd, unsigned long arg)
1376 int error;
1377 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1378 struct async_icount cprev, cnow; /* kernel counter temps */
1379 struct serial_icounter_struct *p_cuser; /* user space */
1381 if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1382 return -ENODEV;
1384 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1385 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1386 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1387 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1388 if (tty->flags & (1 << TTY_IO_ERROR))
1389 return -EIO;
1392 switch (cmd) {
1393 case TIOCGSOFTCAR:
1394 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1395 case TIOCSSOFTCAR:
1396 error = get_user(arg, (unsigned int *) arg);
1397 if (error)
1398 return error;
1399 tty->termios->c_cflag =
1400 ((tty->termios->c_cflag & ~CLOCAL) |
1401 (arg ? CLOCAL : 0));
1402 return 0;
1403 case TIOCMGET:
1404 return get_modem_info(info, (unsigned int *) arg);
1405 case TIOCMBIS:
1406 case TIOCMBIC:
1407 case TIOCMSET:
1408 return set_modem_info(info, cmd, (unsigned int *) arg);
1409 case TIOCGSERIAL:
1410 return get_serial_info(info,
1411 (struct serial_struct *) arg);
1412 case TIOCSSERIAL:
1413 return set_serial_info(info,
1414 (struct serial_struct *) arg);
1416 case TIOCSERGETLSR: /* Get line status register */
1417 return get_lsr_info(info, (unsigned int *) arg);
1419 case TIOCSERGSTRUCT:
1420 if (copy_to_user((struct sab82532 *) arg,
1421 info, sizeof(struct sab82532)))
1422 return -EFAULT;
1423 return 0;
1426 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1427 * - mask passed in arg for lines of interest
1428 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1429 * Caller should use TIOCGICOUNT to see which one it was
1431 case TIOCMIWAIT:
1432 cli();
1433 /* note the counters on entry */
1434 cprev = info->icount;
1435 sti();
1436 while (1) {
1437 interruptible_sleep_on(&info->delta_msr_wait);
1438 /* see if a signal did it */
1439 if (signal_pending(current))
1440 return -ERESTARTSYS;
1441 cli();
1442 cnow = info->icount; /* atomic copy */
1443 sti();
1444 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1445 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1446 return -EIO; /* no change => error */
1447 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1448 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1449 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1450 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1451 return 0;
1453 cprev = cnow;
1455 /* NOTREACHED */
1458 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1459 * Return: write counters to the user passed counter struct
1460 * NB: both 1->0 and 0->1 transitions are counted except for
1461 * RI where only 0->1 is counted.
1463 case TIOCGICOUNT:
1464 cli();
1465 cnow = info->icount;
1466 sti();
1467 p_cuser = (struct serial_icounter_struct *) arg;
1468 error = put_user(cnow.cts, &p_cuser->cts);
1469 if (error) return error;
1470 error = put_user(cnow.dsr, &p_cuser->dsr);
1471 if (error) return error;
1472 error = put_user(cnow.rng, &p_cuser->rng);
1473 if (error) return error;
1474 error = put_user(cnow.dcd, &p_cuser->dcd);
1475 if (error) return error;
1476 return 0;
1478 default:
1479 return -ENOIOCTLCMD;
1481 return 0;
1484 static void sab82532_set_termios(struct tty_struct *tty,
1485 struct termios *old_termios)
1487 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1489 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1490 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1491 == RELEVANT_IFLAG(old_termios->c_iflag)))
1492 return;
1494 change_speed(info);
1496 /* Handle transition to B0 status */
1497 if ((old_termios->c_cflag & CBAUD) &&
1498 !(tty->termios->c_cflag & CBAUD)) {
1499 info->regs->w.mode |= SAB82532_MODE_FRTS;
1500 info->regs->w.mode |= SAB82532_MODE_RTS;
1501 info->regs->w.pvr |= info->pvr_dtr_bit;
1504 /* Handle transition away from B0 status */
1505 if (!(old_termios->c_cflag & CBAUD) &&
1506 (tty->termios->c_cflag & CBAUD)) {
1507 info->regs->w.pvr &= ~(info->pvr_dtr_bit);
1508 if (!tty->hw_stopped ||
1509 !(tty->termios->c_cflag & CRTSCTS)) {
1510 info->regs->w.mode &= ~(SAB82532_MODE_FRTS);
1511 info->regs->w.mode |= SAB82532_MODE_RTS;
1515 /* Handle turning off CRTSCTS */
1516 if ((old_termios->c_cflag & CRTSCTS) &&
1517 !(tty->termios->c_cflag & CRTSCTS)) {
1518 tty->hw_stopped = 0;
1519 sab82532_start(tty);
1522 #if 0
1524 * No need to wake up processes in open wait, since they
1525 * sample the CLOCAL flag once, and don't recheck it.
1526 * XXX It's not clear whether the current behavior is correct
1527 * or not. Hence, this may change.....
1529 if (!(old_termios->c_cflag & CLOCAL) &&
1530 (tty->termios->c_cflag & CLOCAL))
1531 wake_up_interruptible(&info->open_wait);
1532 #endif
1536 * ------------------------------------------------------------
1537 * sab82532_close()
1539 * This routine is called when the serial port gets closed. First, we
1540 * wait for the last remaining data to be sent. Then, we unlink its
1541 * async structure from the interrupt chain if necessary, and we free
1542 * that IRQ if nothing is left in the chain.
1543 * ------------------------------------------------------------
1545 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1547 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1548 unsigned long flags;
1550 if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1551 return;
1553 save_flags(flags); cli();
1555 if (tty_hung_up_p(filp)) {
1556 MOD_DEC_USE_COUNT;
1557 restore_flags(flags);
1558 return;
1561 #ifdef SERIAL_DEBUG_OPEN
1562 printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1563 #endif
1564 if ((tty->count == 1) && (info->count != 1)) {
1566 * Uh, oh. tty->count is 1, which means that the tty
1567 * structure will be freed. info->count should always
1568 * be one in these conditions. If it's greater than
1569 * one, we've got real problems, since it means the
1570 * serial port won't be shutdown.
1572 printk("sab82532_close: bad serial port count; tty->count is 1,"
1573 " info->count is %d\n", info->count);
1574 info->count = 1;
1576 if (--info->count < 0) {
1577 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1578 info->line, info->count);
1579 info->count = 0;
1581 if (info->count) {
1582 MOD_DEC_USE_COUNT;
1583 restore_flags(flags);
1584 return;
1586 info->flags |= ASYNC_CLOSING;
1588 * Save the termios structure, since this port may have
1589 * separate termios for callout and dialin.
1591 if (info->flags & ASYNC_NORMAL_ACTIVE)
1592 info->normal_termios = *tty->termios;
1593 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1594 info->callout_termios = *tty->termios;
1596 * Now we wait for the transmit buffer to clear; and we notify
1597 * the line discipline to only process XON/XOFF characters.
1599 tty->closing = 1;
1600 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1601 tty_wait_until_sent(tty, info->closing_wait);
1604 * At this point we stop accepting input. To do this, we
1605 * disable the receive line status interrupts, and turn off
1606 * the receiver.
1608 info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1609 info->regs->w.imr0 = info->interrupt_mask0;
1610 #if 0
1611 info->regs->rw.mode &= ~(SAB82532_MODE_RAC);
1612 #endif
1613 if (info->flags & ASYNC_INITIALIZED) {
1615 * Before we drop DTR, make sure the UART transmitter
1616 * has completely drained; this is especially
1617 * important if there is a transmit FIFO!
1619 sab82532_wait_until_sent(tty, info->timeout);
1621 shutdown(info);
1622 if (tty->driver.flush_buffer)
1623 tty->driver.flush_buffer(tty);
1624 if (tty->ldisc.flush_buffer)
1625 tty->ldisc.flush_buffer(tty);
1626 tty->closing = 0;
1627 info->event = 0;
1628 info->tty = 0;
1629 if (info->blocked_open) {
1630 if (info->close_delay) {
1631 current->state = TASK_INTERRUPTIBLE;
1632 schedule_timeout(info->close_delay);
1634 wake_up_interruptible(&info->open_wait);
1636 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1637 ASYNC_CLOSING);
1638 wake_up_interruptible(&info->close_wait);
1639 MOD_DEC_USE_COUNT;
1640 restore_flags(flags);
1644 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1646 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1648 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1649 unsigned long orig_jiffies, char_time;
1651 if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1652 return;
1654 orig_jiffies = jiffies;
1656 * Set the check interval to be 1/5 of the estimated time to
1657 * send a single character, and make it at least 1. The check
1658 * interval should also be less than the timeout.
1660 * Note: we have to use pretty tight timings here to satisfy
1661 * the NIST-PCTS.
1663 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1664 char_time = char_time / 5;
1665 if (char_time == 0)
1666 char_time = 1;
1667 if (timeout)
1668 char_time = MIN(char_time, timeout);
1669 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1670 printk("In sab82532_wait_until_sent(%d) check=%lu...", timeout, char_time);
1671 printk("jiff=%lu...", jiffies);
1672 #endif
1673 while (info->xmit_cnt || !info->all_sent) {
1674 current->state = TASK_INTERRUPTIBLE;
1675 current->counter = 0;
1676 schedule_timeout(char_time);
1677 if (signal_pending(current))
1678 break;
1679 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1680 break;
1682 current->state = TASK_RUNNING;
1683 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1684 printk("xmit_cnt = %d, alls = %d (jiff=%lu)...done\n", info->xmit_cnt, info->all_sent, jiffies);
1685 #endif
1689 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1691 static void sab82532_hangup(struct tty_struct *tty)
1693 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1695 if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1696 return;
1698 if (info->is_console)
1699 return;
1701 sab82532_flush_buffer(tty);
1702 shutdown(info);
1703 info->event = 0;
1704 info->count = 0;
1705 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1706 info->tty = 0;
1707 wake_up_interruptible(&info->open_wait);
1711 * ------------------------------------------------------------
1712 * sab82532_open() and friends
1713 * ------------------------------------------------------------
1715 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1716 struct sab82532 *info)
1718 DECLARE_WAITQUEUE(wait, current);
1719 int retval;
1720 int do_clocal = 0;
1723 * If the device is in the middle of being closed, then block
1724 * until it's done, and then try again.
1726 if (tty_hung_up_p(filp) ||
1727 (info->flags & ASYNC_CLOSING)) {
1728 if (info->flags & ASYNC_CLOSING)
1729 interruptible_sleep_on(&info->close_wait);
1730 #ifdef SERIAL_DO_RESTART
1731 if (info->flags & ASYNC_HUP_NOTIFY)
1732 return -EAGAIN;
1733 else
1734 return -ERESTARTSYS;
1735 #else
1736 return -EAGAIN;
1737 #endif
1741 * If this is a callout device, then just make sure the normal
1742 * device isn't being used.
1744 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1745 if (info->flags & ASYNC_NORMAL_ACTIVE)
1746 return -EBUSY;
1747 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1748 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1749 (info->session != current->session))
1750 return -EBUSY;
1751 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1752 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1753 (info->pgrp != current->pgrp))
1754 return -EBUSY;
1755 info->flags |= ASYNC_CALLOUT_ACTIVE;
1756 return 0;
1760 * If non-blocking mode is set, or the port is not enabled,
1761 * then make the check up front and then exit.
1763 if ((filp->f_flags & O_NONBLOCK) ||
1764 (tty->flags & (1 << TTY_IO_ERROR))) {
1765 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1766 return -EBUSY;
1767 info->flags |= ASYNC_NORMAL_ACTIVE;
1768 return 0;
1771 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1772 if (info->normal_termios.c_cflag & CLOCAL)
1773 do_clocal = 1;
1774 } else {
1775 if (tty->termios->c_cflag & CLOCAL)
1776 do_clocal = 1;
1780 * Block waiting for the carrier detect and the line to become
1781 * free (i.e., not in use by the callout). While we are in
1782 * this loop, info->count is dropped by one, so that
1783 * sab82532_close() knows when to free things. We restore it upon
1784 * exit, either normal or abnormal.
1786 retval = 0;
1787 add_wait_queue(&info->open_wait, &wait);
1788 #ifdef SERIAL_DEBUG_OPEN
1789 printk("block_til_ready before block: ttyS%d, count = %d\n",
1790 info->line, info->count);
1791 #endif
1792 cli();
1793 if (!tty_hung_up_p(filp))
1794 info->count--;
1795 sti();
1796 info->blocked_open++;
1797 while (1) {
1798 cli();
1799 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1800 (tty->termios->c_cflag & CBAUD)) {
1801 info->regs->rw.pvr &= ~(info->pvr_dtr_bit);
1802 info->regs->rw.mode |= SAB82532_MODE_FRTS;
1803 info->regs->rw.mode &= ~(SAB82532_MODE_RTS);
1805 sti();
1806 current->state = TASK_INTERRUPTIBLE;
1807 if (tty_hung_up_p(filp) ||
1808 !(info->flags & ASYNC_INITIALIZED)) {
1809 #ifdef SERIAL_DO_RESTART
1810 if (info->flags & ASYNC_HUP_NOTIFY)
1811 retval = -EAGAIN;
1812 else
1813 retval = -ERESTARTSYS;
1814 #else
1815 retval = -EAGAIN;
1816 #endif
1817 break;
1819 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1820 !(info->flags & ASYNC_CLOSING) &&
1821 (do_clocal || !(info->regs->r.vstr & SAB82532_VSTR_CD)))
1822 break;
1823 if (signal_pending(current)) {
1824 retval = -ERESTARTSYS;
1825 break;
1827 #ifdef SERIAL_DEBUG_OPEN
1828 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1829 info->line, info->count, info->flags, do_clocal, info->regs->r.vstr);
1830 #endif
1831 schedule();
1833 current->state = TASK_RUNNING;
1834 remove_wait_queue(&info->open_wait, &wait);
1835 if (!tty_hung_up_p(filp))
1836 info->count++;
1837 info->blocked_open--;
1838 #ifdef SERIAL_DEBUG_OPEN
1839 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1840 info->line, info->count);
1841 #endif
1842 if (retval)
1843 return retval;
1844 info->flags |= ASYNC_NORMAL_ACTIVE;
1845 return 0;
1849 * This routine is called whenever a serial port is opened. It
1850 * enables interrupts for a serial port, linking in its async structure into
1851 * the IRQ chain. It also performs the serial-specific
1852 * initialization for the tty structure.
1854 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1856 struct sab82532 *info = sab82532_chain;
1857 int retval, line;
1858 unsigned long page;
1860 #ifdef SERIAL_DEBUG_OPEN
1861 printk("sab82532_open: count = %d\n", info->count);
1862 #endif
1864 line = MINOR(tty->device) - tty->driver.minor_start;
1865 if ((line < 0) || (line >= NR_PORTS))
1866 return -ENODEV;
1868 while (info) {
1869 if (info->line == line)
1870 break;
1871 info = info->next;
1873 if (!info) {
1874 printk("sab82532_open: can't find info for line %d\n",
1875 line);
1876 return -ENODEV;
1879 if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1880 return -ENODEV;
1882 #ifdef SERIAL_DEBUG_OPEN
1883 printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1884 info->count);
1885 #endif
1887 info->count++;
1888 tty->driver_data = info;
1889 info->tty = tty;
1891 if (!tmp_buf) {
1892 page = get_free_page(GFP_KERNEL);
1893 if (!page)
1894 return -ENOMEM;
1895 if (tmp_buf)
1896 free_page(page);
1897 else
1898 tmp_buf = (unsigned char *) page;
1902 * If the port is in the middle of closing, bail out now.
1904 if (tty_hung_up_p(filp) ||
1905 (info->flags & ASYNC_CLOSING)) {
1906 if (info->flags & ASYNC_CLOSING)
1907 interruptible_sleep_on(&info->close_wait);
1908 #ifdef SERIAL_DO_RESTART
1909 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1910 -EAGAIN : -ERESTARTSYS);
1911 #else
1912 return -EAGAIN;
1913 #endif
1917 * Start up serial port
1919 retval = startup(info);
1920 if (retval)
1921 return retval;
1923 MOD_INC_USE_COUNT;
1924 retval = block_til_ready(tty, filp, info);
1925 if (retval) {
1926 #ifdef SERIAL_DEBUG_OPEN
1927 printk("sab82532_open returning after block_til_ready with %d\n",
1928 retval);
1929 #endif
1930 return retval;
1933 if ((info->count == 1) &&
1934 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1935 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1936 *tty->termios = info->normal_termios;
1937 else
1938 *tty->termios = info->callout_termios;
1939 change_speed(info);
1942 #ifdef CONFIG_SERIAL_CONSOLE
1943 if (sab82532_console.cflag && sab82532_console.index == line) {
1944 tty->termios->c_cflag = sab82532_console.cflag;
1945 sab82532_console.cflag = 0;
1946 change_speed(info);
1948 #endif
1950 info->session = current->session;
1951 info->pgrp = current->pgrp;
1953 #ifdef SERIAL_DEBUG_OPEN
1954 printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
1955 #endif
1956 return 0;
1960 * /proc fs routines....
1963 static __inline__ int
1964 line_info(char *buf, struct sab82532 *info)
1966 unsigned long flags;
1967 char stat_buf[30];
1968 int ret;
1970 ret = sprintf(buf, "%d: uart:SAB82532 ", info->line);
1971 switch (info->type) {
1972 case 0:
1973 ret += sprintf(buf+ret, "V1.0 ");
1974 break;
1975 case 1:
1976 ret += sprintf(buf+ret, "V2.0 ");
1977 break;
1978 case 2:
1979 ret += sprintf(buf+ret, "V3.2 ");
1980 break;
1981 default:
1982 ret += sprintf(buf+ret, "V?.? ");
1983 break;
1985 ret += sprintf(buf+ret, "port:%lX irq:%s",
1986 (unsigned long)info->regs, __irq_itoa(info->irq));
1988 if (!info->regs) {
1989 ret += sprintf(buf+ret, "\n");
1990 return ret;
1994 * Figure out the current RS-232 lines
1996 stat_buf[0] = 0;
1997 stat_buf[1] = 0;
1998 save_flags(flags); cli();
1999 if (info->regs->r.mode & SAB82532_MODE_RTS) {
2000 if (!(info->regs->r.mode & SAB82532_MODE_FRTS))
2001 strcat(stat_buf, "|RTS");
2002 } else {
2003 strcat(stat_buf, "|RTS");
2005 if (info->regs->r.star & SAB82532_STAR_CTS)
2006 strcat(stat_buf, "|CTS");
2007 if (!(info->regs->r.pvr & info->pvr_dtr_bit))
2008 strcat(stat_buf, "|DTR");
2009 if (!(info->regs->r.pvr & info->pvr_dsr_bit))
2010 strcat(stat_buf, "|DSR");
2011 if (!(info->regs->r.vstr & SAB82532_VSTR_CD))
2012 strcat(stat_buf, "|CD");
2013 restore_flags(flags);
2015 if (info->baud)
2016 ret += sprintf(buf+ret, " baud:%d", info->baud);
2018 if (info->icount.frame)
2019 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2021 if (info->icount.parity)
2022 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2024 if (info->icount.brk)
2025 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2027 if (info->icount.overrun)
2028 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2031 * Last thing is the RS-232 status lines.
2033 ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2034 return ret;
2037 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2038 int *eof, void *data)
2040 struct sab82532 *info = sab82532_chain;
2041 off_t begin = 0;
2042 int len = 0;
2044 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2045 for (info = sab82532_chain; info && len < 4000; info = info->next) {
2046 len += line_info(page + len, info);
2047 if (len+begin > off+count)
2048 goto done;
2049 if (len+begin < off) {
2050 begin += len;
2051 len = 0;
2054 *eof = 1;
2055 done:
2056 if (off >= len+begin)
2057 return 0;
2058 *start = page + (begin-off);
2059 return ((count < begin+len-off) ? count : begin+len-off);
2063 * ---------------------------------------------------------------------
2064 * sab82532_init() and friends
2066 * sab82532_init() is called at boot-time to initialize the serial driver.
2067 * ---------------------------------------------------------------------
2069 __initfunc(static int get_sab82532(unsigned long *memory_start))
2071 struct linux_ebus *ebus;
2072 struct linux_ebus_device *edev = 0;
2073 struct sab82532 *sab;
2074 unsigned long regs, offset;
2075 int i;
2077 for_each_ebus(ebus) {
2078 for_each_ebusdev(edev, ebus) {
2079 if (!strcmp(edev->prom_name, "se"))
2080 goto ebus_done;
2083 ebus_done:
2084 if (!edev)
2085 return -ENODEV;
2087 regs = edev->base_address[0];
2088 offset = sizeof(union sab82532_async_regs);
2090 for (i = 0; i < 2; i++) {
2091 if (memory_start) {
2092 *memory_start = (*memory_start + 7) & ~(7);
2093 sab = (struct sab82532 *)*memory_start;
2094 *memory_start += sizeof(struct sab82532);
2095 } else {
2096 sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2097 GFP_KERNEL);
2098 if (!sab) {
2099 printk("sab82532: can't alloc sab struct\n");
2100 break;
2103 memset(sab, 0, sizeof(struct sab82532));
2105 sab->regs = (union sab82532_async_regs *)(regs + offset);
2106 sab->irq = edev->irqs[0];
2107 sab->line = 1 - i;
2108 sab->xmit_fifo_size = 32;
2109 sab->recv_fifo_size = 32;
2111 if (check_region((unsigned long)sab->regs,
2112 sizeof(union sab82532_async_regs))) {
2113 kfree(sab);
2114 continue;
2116 request_region((unsigned long)sab->regs,
2117 sizeof(union sab82532_async_regs),
2118 "serial(sab82532)");
2120 sab->regs->w.ipc = SAB82532_IPC_IC_ACT_LOW;
2122 sab->next = sab82532_chain;
2123 sab82532_chain = sab;
2125 offset -= sizeof(union sab82532_async_regs);
2127 return 0;
2130 __initfunc(static void
2131 sab82532_kgdb_hook(int line))
2133 prom_printf("sab82532: kgdb support is not implemented, yet\n");
2134 prom_halt();
2137 __initfunc(static inline void show_serial_version(void))
2139 char *revision = "$Revision: 1.31 $";
2140 char *version, *p;
2142 version = strchr(revision, ' ');
2143 strcpy(serial_version, ++version);
2144 p = strchr(serial_version, ' ');
2145 *p = '\0';
2146 printk("SAB82532 serial driver version %s\n", serial_version);
2150 * The serial driver boot-time initialization code!
2152 __initfunc(int sab82532_init(void))
2154 struct sab82532 *info;
2155 int i;
2157 if (!sab82532_chain)
2158 get_sab82532(0);
2159 if (!sab82532_chain)
2160 return -ENODEV;
2162 init_bh(SERIAL_BH, do_serial_bh);
2164 show_serial_version();
2166 /* Initialize the tty_driver structure */
2167 memset(&serial_driver, 0, sizeof(struct tty_driver));
2168 serial_driver.magic = TTY_DRIVER_MAGIC;
2169 serial_driver.driver_name = "serial";
2170 serial_driver.name = "ttyS";
2171 serial_driver.major = TTY_MAJOR;
2172 serial_driver.minor_start = 64;
2173 serial_driver.num = NR_PORTS;
2174 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2175 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2176 serial_driver.init_termios = tty_std_termios;
2177 serial_driver.init_termios.c_cflag =
2178 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2179 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2180 serial_driver.refcount = &sab82532_refcount;
2181 serial_driver.table = sab82532_table;
2182 serial_driver.termios = sab82532_termios;
2183 serial_driver.termios_locked = sab82532_termios_locked;
2185 serial_driver.open = sab82532_open;
2186 serial_driver.close = sab82532_close;
2187 serial_driver.write = sab82532_write;
2188 serial_driver.put_char = sab82532_put_char;
2189 serial_driver.flush_chars = sab82532_flush_chars;
2190 serial_driver.write_room = sab82532_write_room;
2191 serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2192 serial_driver.flush_buffer = sab82532_flush_buffer;
2193 serial_driver.ioctl = sab82532_ioctl;
2194 serial_driver.throttle = sab82532_throttle;
2195 serial_driver.unthrottle = sab82532_unthrottle;
2196 serial_driver.send_xchar = sab82532_send_xchar;
2197 serial_driver.set_termios = sab82532_set_termios;
2198 serial_driver.stop = sab82532_stop;
2199 serial_driver.start = sab82532_start;
2200 serial_driver.hangup = sab82532_hangup;
2201 serial_driver.break_ctl = sab82532_break;
2202 serial_driver.wait_until_sent = sab82532_wait_until_sent;
2203 serial_driver.read_proc = sab82532_read_proc;
2206 * The callout device is just like normal device except for
2207 * major number and the subtype code.
2209 callout_driver = serial_driver;
2210 callout_driver.name = "cua";
2211 callout_driver.major = TTYAUX_MAJOR;
2212 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2213 callout_driver.read_proc = 0;
2214 callout_driver.proc_entry = 0;
2216 if (tty_register_driver(&serial_driver))
2217 panic("Couldn't register serial driver\n");
2218 if (tty_register_driver(&callout_driver))
2219 panic("Couldn't register callout driver\n");
2221 for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2222 info->magic = SERIAL_MAGIC;
2224 info->type = info->regs->r.vstr & 0x0f;
2225 info->regs->w.pcr = ~((1 << 1) | (1 << 2) | (1 << 4));
2226 info->regs->w.pim = 0xff;
2227 if (info->line == 0) {
2228 info->pvr_dsr_bit = (1 << 0);
2229 info->pvr_dtr_bit = (1 << 1);
2230 } else {
2231 info->pvr_dsr_bit = (1 << 3);
2232 info->pvr_dtr_bit = (1 << 2);
2234 info->regs->w.pvr = (1 << 1) | (1 << 2) | (1 << 4);
2235 info->regs->rw.mode |= SAB82532_MODE_FRTS;
2236 info->regs->rw.mode |= SAB82532_MODE_RTS;
2238 info->custom_divisor = 16;
2239 info->close_delay = 5*HZ/10;
2240 info->closing_wait = 30*HZ;
2241 info->x_char = 0;
2242 info->event = 0;
2243 info->blocked_open = 0;
2244 info->tqueue.routine = do_softint;
2245 info->tqueue.data = info;
2246 info->tqueue_hangup.routine = do_serial_hangup;
2247 info->tqueue_hangup.data = info;
2248 info->callout_termios = callout_driver.init_termios;
2249 info->normal_termios = serial_driver.init_termios;
2250 init_waitqueue_head(&info->open_wait);
2251 init_waitqueue_head(&info->close_wait);
2252 init_waitqueue_head(&info->delta_msr_wait);
2253 info->icount.cts = info->icount.dsr =
2254 info->icount.rng = info->icount.dcd = 0;
2255 info->icount.rx = info->icount.tx = 0;
2256 info->icount.frame = info->icount.parity = 0;
2257 info->icount.overrun = info->icount.brk = 0;
2259 if (!(info->line & 0x01)) {
2260 if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2261 "serial(sab82532)", info)) {
2262 printk("sab82532: can't get IRQ %x\n",
2263 info->irq);
2264 panic("sab82532 initialization failed");
2268 printk(KERN_INFO
2269 "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2270 info->line, (unsigned long)info->regs,
2271 __irq_itoa(info->irq), sab82532_version[info->type]);
2274 #ifdef SERIAL_LOG_DEVICE
2275 dprint_init(SERIAL_LOG_DEVICE);
2276 #endif
2277 return 0;
2280 __initfunc(int sab82532_probe(unsigned long *memory_start))
2282 int node, enode, snode;
2283 char model[32];
2284 int len;
2286 node = prom_getchild(prom_root_node);
2287 node = prom_searchsiblings(node, "pci");
2290 * Check for SUNW,sabre on Ultra 5/10/AXi.
2292 len = prom_getproperty(node, "model", model, sizeof(model));
2293 if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2294 node = prom_getchild(node);
2295 node = prom_searchsiblings(node, "pci");
2299 * For each PCI bus...
2301 while (node) {
2302 enode = prom_getchild(node);
2303 enode = prom_searchsiblings(enode, "ebus");
2306 * For each EBus on this PCI...
2308 while (enode) {
2309 snode = prom_getchild(enode);
2310 snode = prom_searchsiblings(snode, "se");
2311 if (snode)
2312 goto found;
2314 enode = prom_getsibling(enode);
2315 enode = prom_searchsiblings(enode, "ebus");
2317 node = prom_getsibling(node);
2318 node = prom_searchsiblings(node, "pci");
2320 return -ENODEV;
2322 found:
2323 #ifdef CONFIG_SERIAL_CONSOLE
2324 sunserial_setinitfunc(memory_start, sab82532_console_init);
2325 #endif
2326 sunserial_setinitfunc(memory_start, sab82532_init);
2327 rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2328 return 0;
2331 #ifdef MODULE
2332 int init_module(void)
2334 if (get_sab82532(0))
2335 return -ENODEV;
2337 return sab82532_init();
2340 void cleanup_module(void)
2342 unsigned long flags;
2343 int e1, e2;
2344 int i;
2346 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2347 save_flags(flags);
2348 cli();
2349 timer_active &= ~(1 << RS_TIMER);
2350 timer_table[RS_TIMER].fn = NULL;
2351 timer_table[RS_TIMER].expires = 0;
2352 remove_bh(SERIAL_BH);
2353 if ((e1 = tty_unregister_driver(&serial_driver)))
2354 printk("SERIAL: failed to unregister serial driver (%d)\n",
2355 e1);
2356 if ((e2 = tty_unregister_driver(&callout_driver)))
2357 printk("SERIAL: failed to unregister callout driver (%d)\n",
2358 e2);
2359 restore_flags(flags);
2361 for (i = 0; i < NR_PORTS; i++) {
2362 struct sab82532 *info = (struct sab82532 *)sab82532_table[i]->driver_data;
2363 if (info->type != PORT_UNKNOWN)
2364 release_region((unsigned long)info->regs,
2365 sizeof(union sab82532_async_regs));
2367 if (tmp_buf) {
2368 free_page((unsigned long) tmp_buf);
2369 tmp_buf = NULL;
2372 #endif /* MODULE */
2374 #ifdef CONFIG_SERIAL_CONSOLE
2376 static __inline__ void
2377 sab82532_console_putchar(struct sab82532 *info, char c)
2379 unsigned long flags;
2381 save_flags(flags); cli();
2382 sab82532_tec_wait(info);
2383 info->regs->w.tic = c;
2384 restore_flags(flags);
2387 static void
2388 sab82532_console_write(struct console *con, const char *s, unsigned n)
2390 struct sab82532 *info;
2391 int i;
2393 info = sab82532_chain;
2394 for (i = con->index; i; i--) {
2395 info = info->next;
2396 if (!info)
2397 return;
2400 for (i = 0; i < n; i++) {
2401 if (*s == '\n')
2402 sab82532_console_putchar(info, '\r');
2403 sab82532_console_putchar(info, *s++);
2405 sab82532_tec_wait(info);
2408 static int
2409 sab82532_console_wait_key(struct console *con)
2411 sleep_on(&keypress_wait);
2412 return 0;
2415 static kdev_t
2416 sab82532_console_device(struct console *con)
2418 return MKDEV(TTY_MAJOR, 64 + con->index);
2421 static int
2422 sab82532_console_setup(struct console *con, char *options)
2424 struct sab82532 *info;
2425 unsigned int ebrg;
2426 tcflag_t cflag;
2427 unsigned char dafo;
2428 int i, bits;
2429 unsigned long flags;
2431 info = sab82532_chain;
2432 for (i = con->index; i; i--) {
2433 info = info->next;
2434 if (!info)
2435 return -ENODEV;
2437 info->is_console = 1;
2440 * Initialize the hardware
2442 sab82532_init_line(info);
2445 * Finally, enable interrupts
2447 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2448 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2449 info->regs->w.imr0 = info->interrupt_mask0;
2450 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2451 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2452 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2453 SAB82532_IMR1_XPR;
2454 info->regs->w.imr1 = info->interrupt_mask1;
2456 printk("Console: ttyS%d (SAB82532)\n", info->line);
2458 sunserial_console_termios(con);
2459 cflag = con->cflag;
2461 /* Byte size and parity */
2462 switch (cflag & CSIZE) {
2463 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2464 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
2465 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
2466 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
2467 /* Never happens, but GCC is too dumb to figure it out */
2468 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2471 if (cflag & CSTOPB) {
2472 dafo |= SAB82532_DAFO_STOP;
2473 bits++;
2476 if (cflag & PARENB) {
2477 dafo |= SAB82532_DAFO_PARE;
2478 bits++;
2481 if (cflag & PARODD) {
2482 #ifdef CMSPAR
2483 if (cflag & CMSPAR)
2484 dafo |= SAB82532_DAFO_PAR_MARK;
2485 else
2486 #endif
2487 dafo |= SAB82532_DAFO_PAR_ODD;
2488 } else {
2489 #ifdef CMSPAR
2490 if (cflag & CMSPAR)
2491 dafo |= SAB82532_DAFO_PAR_SPACE;
2492 else
2493 #endif
2494 dafo |= SAB82532_DAFO_PAR_EVEN;
2497 /* Determine EBRG values based on baud rate */
2498 i = cflag & CBAUD;
2499 if (i & CBAUDEX) {
2500 i &= ~(CBAUDEX);
2501 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
2502 cflag &= ~CBAUDEX;
2503 else
2504 i += 15;
2506 ebrg = ebrg_table[i].n;
2507 ebrg |= (ebrg_table[i].m << 6);
2509 info->baud = ebrg_table[i].baud;
2510 if (info->baud)
2511 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
2512 else
2513 info->timeout = 0;
2514 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2516 /* CTS flow control flags */
2517 if (cflag & CRTSCTS)
2518 info->flags |= ASYNC_CTS_FLOW;
2519 else
2520 info->flags &= ~(ASYNC_CTS_FLOW);
2522 if (cflag & CLOCAL)
2523 info->flags &= ~(ASYNC_CHECK_CD);
2524 else
2525 info->flags |= ASYNC_CHECK_CD;
2527 save_flags(flags); cli();
2528 if (info->regs->r.star & SAB82532_STAR_CEC)
2529 udelay(1);
2530 sab82532_tec_wait(info);
2531 info->regs->w.dafo = dafo;
2532 info->regs->w.bgr = ebrg & 0xff;
2533 info->regs->rw.ccr2 &= ~(0xc0);
2534 info->regs->rw.ccr2 |= (ebrg >> 2) & 0xc0;
2535 if (info->flags & ASYNC_CTS_FLOW) {
2536 info->regs->rw.mode &= ~(SAB82532_MODE_RTS);
2537 info->regs->rw.mode |= SAB82532_MODE_FRTS;
2538 info->regs->rw.mode &= ~(SAB82532_MODE_FCTS);
2539 } else {
2540 info->regs->rw.mode |= SAB82532_MODE_RTS;
2541 info->regs->rw.mode &= ~(SAB82532_MODE_FRTS);
2542 info->regs->rw.mode |= SAB82532_MODE_FCTS;
2544 info->regs->rw.pvr &= ~(info->pvr_dtr_bit);
2545 info->regs->rw.mode |= SAB82532_MODE_RAC;
2546 restore_flags(flags);
2548 return 0;
2551 static struct console sab82532_console = {
2552 "ttyS",
2553 sab82532_console_write,
2554 NULL,
2555 sab82532_console_device,
2556 sab82532_console_wait_key,
2557 NULL,
2558 sab82532_console_setup,
2559 CON_PRINTBUFFER,
2562 NULL
2565 __initfunc(int sab82532_console_init(void))
2567 extern int con_is_present(void);
2569 if (con_is_present())
2570 return 0;
2572 if (!sab82532_chain) {
2573 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2574 prom_halt();
2577 sab82532_console.index = serial_console - 1;
2578 register_console(&sab82532_console);
2579 return 0;
2582 #ifdef SERIAL_LOG_DEVICE
2584 static int serial_log_device = 0;
2586 static void
2587 dprint_init(int tty)
2589 serial_console = tty + 1;
2590 sab82532_console.index = tty;
2591 sab82532_console_setup(&sab82532_console, "");
2592 serial_console = 0;
2593 serial_log_device = tty + 1;
2597 dprintf(const char *fmt, ...)
2599 static char buffer[4096];
2600 va_list args;
2601 int i;
2603 if (!serial_log_device)
2604 return 0;
2606 va_start(args, fmt);
2607 i = vsprintf(buffer, fmt, args);
2608 va_end(args);
2609 sab82532_console.write(&sab82532_console, buffer, i);
2610 return i;
2612 #endif /* SERIAL_LOG_DEVICE */
2613 #endif /* CONFIG_SERIAL_CONSOLE */