Import 2.3.18pre1
[davej-history.git] / drivers / sbus / char / sab82532.c
blob6a812508032dfc0030d54839daf52b4c8cb264c0
1 /* $Id: sab82532.c,v 1.35 1999/09/01 08:09:29 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 ((readb(&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 (!(readb(&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 u8 val = info->xmit_buf[info->xmit_tail++];
197 writeb(val, &info->regs->w.xfifo[i]);
198 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
199 info->icount.tx++;
200 if (--info->xmit_cnt <= 0)
201 break;
204 /* Issue a Transmit Frame command. */
205 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
206 udelay(1);
207 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
209 out:
210 restore_flags(flags);
215 * ------------------------------------------------------------
216 * sab82532_stop() and sab82532_start()
218 * This routines are called before setting or resetting tty->stopped.
219 * They enable or disable transmitter interrupts, as necessary.
220 * ------------------------------------------------------------
222 static void sab82532_stop(struct tty_struct *tty)
224 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
225 unsigned long flags;
227 if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
228 return;
230 save_flags(flags); cli();
231 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
232 writeb(info->interrupt_mask1, &info->regs->w.imr1);
233 restore_flags(flags);
236 static void sab82532_start(struct tty_struct *tty)
238 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
239 unsigned long flags;
241 if (serial_paranoia_check(info, tty->device, "sab82532_start"))
242 return;
244 save_flags(flags); cli();
245 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
246 writeb(info->interrupt_mask1, &info->regs->w.imr1);
247 sab82532_start_tx(info);
248 restore_flags(flags);
251 static void batten_down_hatches(struct sab82532 *info)
253 unsigned char saved_rfc, tmp;
255 /* If we are doing kadb, we call the debugger
256 * else we just drop into the boot monitor.
257 * Note that we must flush the user windows
258 * first before giving up control.
260 printk("\n");
261 flush_user_windows();
264 * Set FIFO to single character mode.
266 saved_rfc = readb(&info->regs->r.rfc);
267 tmp = readb(&info->regs->rw.rfc);
268 tmp &= ~(SAB82532_RFC_RFDF);
269 writeb(tmp, &info->regs->rw.rfc);
270 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
271 udelay(1);
272 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
274 #ifndef __sparc_v9__
275 if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
276 (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
277 sp_enter_debugger();
278 else
279 #endif
280 prom_cmdline();
283 * Reset FIFO to character + status mode.
285 writeb(saved_rfc, &info->regs->w.rfc);
286 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
287 udelay(1);
288 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
292 * ----------------------------------------------------------------------
294 * Here starts the interrupt handling routines. All of the following
295 * subroutines are declared as inline and are folded into
296 * sab82532_interrupt(). They were separated out for readability's sake.
298 * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
299 * runs with interrupts turned off. People who may want to modify
300 * sab82532_interrupt() should try to keep the interrupt handler as fast as
301 * possible. After you are done making modifications, it is not a bad
302 * idea to do:
304 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
306 * and look at the resulting assemble code in serial.s.
308 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
309 * -----------------------------------------------------------------------
313 * This routine is used by the interrupt handler to schedule
314 * processing in the software interrupt portion of the driver.
316 static inline void sab82532_sched_event(struct sab82532 *info, int event)
318 info->event |= 1 << event;
319 queue_task(&info->tqueue, &tq_serial);
320 mark_bh(SERIAL_BH);
323 static inline void receive_chars(struct sab82532 *info,
324 union sab82532_irq_status *stat)
326 struct tty_struct *tty = info->tty;
327 unsigned char buf[32];
328 unsigned char status;
329 int free_fifo = 0;
330 int i, count = 0;
332 /* Read number of BYTES (Character + Status) available. */
333 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
334 count = info->recv_fifo_size;
335 free_fifo++;
338 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
339 count = readb(&info->regs->r.rbcl) & (info->recv_fifo_size - 1);
340 free_fifo++;
343 /* Issue a FIFO read command in case we where idle. */
344 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
345 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
346 udelay(1);
347 writeb(SAB82532_CMDR_RFRD, &info->regs->w.cmdr);
350 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
351 #if 1
352 printk("sab82532: receive_chars: RFO");
353 #endif
354 free_fifo++;
357 /* Read the FIFO. */
358 for (i = 0; i < count; i++)
359 buf[i] = readb(&info->regs->r.rfifo[i]);
361 /* Issue Receive Message Complete command. */
362 if (free_fifo) {
363 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
364 udelay(1);
365 writeb(SAB82532_CMDR_RMC, &info->regs->w.cmdr);
368 if (info->is_console)
369 wake_up(&keypress_wait);
370 if (!tty)
371 return;
373 for (i = 0; i < count; ) {
374 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
375 #if 1
376 printk("sab82532: receive_chars: tty overrun\n");
377 #endif
378 info->icount.buf_overrun++;
379 break;
382 tty->flip.count++;
383 *tty->flip.char_buf_ptr++ = buf[i++];
384 status = buf[i++];
385 info->icount.rx++;
387 #ifdef SERIAL_DEBUG_INTR
388 printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
389 #endif
391 if (status & SAB82532_RSTAT_PE) {
392 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
393 info->icount.parity++;
394 } else if (status & SAB82532_RSTAT_FE) {
395 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
396 info->icount.frame++;
398 #ifdef CMSPAR
399 else if (status & SAB82532_RSTAT_PARITY)
400 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
401 #endif
402 else
403 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
406 queue_task(&tty->flip.tqueue, &tq_timer);
409 static inline void transmit_chars(struct sab82532 *info,
410 union sab82532_irq_status *stat)
412 int i;
414 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS)
415 info->all_sent = 1;
416 if (!(readb(&info->regs->r.star) & SAB82532_STAR_XFW))
417 return;
419 if (!info->tty) {
420 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
421 writeb(info->interrupt_mask1, &info->regs->w.imr1);
422 return;
425 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
426 info->tty->hw_stopped) {
427 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
428 writeb(info->interrupt_mask1, &info->regs->w.imr1);
429 return;
432 /* Stuff 32 bytes into Transmit FIFO. */
433 info->all_sent = 0;
434 for (i = 0; i < info->xmit_fifo_size; i++) {
435 u8 val = info->xmit_buf[info->xmit_tail++];
436 writeb(val, &info->regs->w.xfifo[i]);
437 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
438 info->icount.tx++;
439 if (--info->xmit_cnt <= 0)
440 break;
443 /* Issue a Transmit Frame command. */
444 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
445 udelay(1);
446 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
448 if (info->xmit_cnt < WAKEUP_CHARS)
449 sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
451 #ifdef SERIAL_DEBUG_INTR
452 printk("THRE...");
453 #endif
454 if (info->xmit_cnt <= 0) {
455 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
456 writeb(info->interrupt_mask1, &info->regs->w.imr1);
460 static inline void check_status(struct sab82532 *info,
461 union sab82532_irq_status *stat)
463 struct tty_struct *tty = info->tty;
464 int modem_change = 0;
466 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
467 if (info->is_console) {
468 batten_down_hatches(info);
469 return;
471 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
472 info->icount.buf_overrun++;
473 goto check_modem;
475 tty->flip.count++;
476 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
477 *tty->flip.char_buf_ptr++ = 0;
478 info->icount.brk++;
481 if (!tty)
482 return;
484 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
485 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
486 info->icount.buf_overrun++;
487 goto check_modem;
489 tty->flip.count++;
490 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
491 *tty->flip.char_buf_ptr++ = 0;
492 info->icount.overrun++;
495 check_modem:
496 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
497 info->dcd = (readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : 1;
498 info->icount.dcd++;
499 modem_change++;
500 #if 0
501 printk("DCD change: %d\n", info->icount.dcd);
502 #endif
504 if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
505 info->cts = readb(&info->regs->r.star) & SAB82532_STAR_CTS;
506 info->icount.cts++;
507 modem_change++;
508 #if 0
509 printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
510 #endif
512 if ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ^ info->dsr) {
513 info->dsr = (readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : 1;
514 info->icount.dsr++;
515 modem_change++;
516 #if 0
517 printk("DSR change: %d\n", info->icount.dsr);
518 #endif
520 if (modem_change)
521 wake_up_interruptible(&info->delta_msr_wait);
523 if ((info->flags & ASYNC_CHECK_CD) &&
524 (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
526 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
527 printk("ttys%d CD now %s...", info->line,
528 (info->dcd) ? "on" : "off");
529 #endif
531 if (info->dcd)
532 wake_up_interruptible(&info->open_wait);
533 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
534 (info->flags & ASYNC_CALLOUT_NOHUP))) {
536 #ifdef SERIAL_DEBUG_OPEN
537 printk("scheduling hangup...");
538 #endif
540 queue_task(&info->tqueue_hangup, &tq_scheduler);
544 if (info->flags & ASYNC_CTS_FLOW) {
545 if (info->tty->hw_stopped) {
546 if (info->cts) {
548 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
549 printk("CTS tx start...");
550 #endif
551 info->tty->hw_stopped = 0;
552 sab82532_sched_event(info,
553 RS_EVENT_WRITE_WAKEUP);
554 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
555 writeb(info->interrupt_mask1, &info->regs->w.imr1);
556 sab82532_start_tx(info);
558 } else {
559 if (!(info->cts)) {
561 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
562 printk("CTS tx stop...");
563 #endif
564 info->tty->hw_stopped = 1;
571 * This is the serial driver's generic interrupt routine
573 static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
575 struct sab82532 *info = dev_id;
576 union sab82532_irq_status status;
578 #ifdef SERIAL_DEBUG_INTR
579 printk("sab82532_interrupt(%d)...", irq);
580 #endif
582 status.stat = 0;
583 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA0)
584 status.sreg.isr0 = readb(&info->regs->r.isr0);
585 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA1)
586 status.sreg.isr1 = readb(&info->regs->r.isr1);
588 #ifdef SERIAL_DEBUG_INTR
589 printk("%d<%02x.%02x>", info->line,
590 status.sreg.isr0, status.sreg.isr1);
591 #endif
593 if (!status.stat)
594 goto next;
596 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
597 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
598 receive_chars(info, &status);
599 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
600 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
601 check_status(info, &status);
602 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
603 transmit_chars(info, &status);
605 next:
606 info = info->next;
607 status.stat = 0;
608 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB0)
609 status.sreg.isr0 = readb(&info->regs->r.isr0);
610 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB1)
611 status.sreg.isr1 = readb(&info->regs->r.isr1);
613 #ifdef SERIAL_DEBUG_INTR
614 printk("%d<%02x.%02x>", info->line,
615 status.sreg.isr0, status.sreg.isr1);
616 #endif
618 if (!status.stat)
619 goto done;
621 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
622 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
623 receive_chars(info, &status);
624 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
625 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
626 check_status(info, &status);
627 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
628 transmit_chars(info, &status);
630 done:
631 #ifdef SERIAL_DEBUG_INTR
632 printk("end.\n");
633 #endif
637 * -------------------------------------------------------------------
638 * Here ends the serial interrupt routines.
639 * -------------------------------------------------------------------
643 * This routine is used to handle the "bottom half" processing for the
644 * serial driver, known also the "software interrupt" processing.
645 * This processing is done at the kernel interrupt level, after the
646 * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
647 * is where time-consuming activities which can not be done in the
648 * interrupt driver proper are done; the interrupt driver schedules
649 * them using sab82532_sched_event(), and they get done here.
651 static void do_serial_bh(void)
653 run_task_queue(&tq_serial);
656 static void do_softint(void *private_)
658 struct sab82532 *info = (struct sab82532 *)private_;
659 struct tty_struct *tty;
661 tty = info->tty;
662 if (!tty)
663 return;
665 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
666 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
667 tty->ldisc.write_wakeup)
668 (tty->ldisc.write_wakeup)(tty);
669 wake_up_interruptible(&tty->write_wait);
674 * This routine is called from the scheduler tqueue when the interrupt
675 * routine has signalled that a hangup has occurred. The path of
676 * hangup processing is:
678 * serial interrupt routine -> (scheduler tqueue) ->
679 * do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
682 static void do_serial_hangup(void *private_)
684 struct sab82532 *info = (struct sab82532 *) private_;
685 struct tty_struct *tty;
687 tty = info->tty;
688 if (!tty)
689 return;
691 tty_hangup(tty);
694 static void
695 sab82532_init_line(struct sab82532 *info)
697 unsigned char stat, tmp;
700 * Wait for any commands or immediate characters
702 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
703 udelay(1);
704 sab82532_tec_wait(info);
707 * Clear the FIFO buffers.
709 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
710 udelay(1);
711 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
712 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
713 udelay(1);
714 writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
717 * Clear the interrupt registers.
719 stat = readb(&info->regs->r.isr0);
720 stat = readb(&info->regs->r.isr1);
723 * Now, initialize the UART
725 writeb(0, &info->regs->w.ccr0); /* power-down */
726 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
727 SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
728 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
729 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
730 SAB82532_CCR2_TOE, &info->regs->w.ccr2);
731 writeb(0, &info->regs->w.ccr3);
732 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
733 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
734 SAB82532_MODE_RAC, &info->regs->w.mode);
735 writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
736 switch (info->recv_fifo_size) {
737 case 1:
738 tmp = readb(&info->regs->w.rfc);
739 tmp |= SAB82532_RFC_RFTH_1;
740 writeb(tmp, &info->regs->w.rfc);
741 break;
742 case 4:
743 tmp = readb(&info->regs->w.rfc);
744 tmp |= SAB82532_RFC_RFTH_4;
745 writeb(tmp, &info->regs->w.rfc);
746 break;
747 case 16:
748 tmp = readb(&info->regs->w.rfc);
749 tmp |= SAB82532_RFC_RFTH_16;
750 writeb(tmp, &info->regs->w.rfc);
751 break;
752 default:
753 info->recv_fifo_size = 32;
754 /* fall through */
755 case 32:
756 tmp = readb(&info->regs->w.rfc);
757 tmp |= SAB82532_RFC_RFTH_32;
758 writeb(tmp, &info->regs->w.rfc);
759 break;
761 tmp = readb(&info->regs->rw.ccr0);
762 tmp |= SAB82532_CCR0_PU; /* power-up */
763 writeb(tmp, &info->regs->rw.ccr0);
766 static int startup(struct sab82532 *info)
768 unsigned long flags;
769 unsigned long page;
770 int retval = 0;
772 page = get_free_page(GFP_KERNEL);
773 if (!page)
774 return -ENOMEM;
776 save_flags(flags); cli();
778 if (info->flags & ASYNC_INITIALIZED) {
779 free_page(page);
780 goto errout;
783 if (!info->regs) {
784 if (info->tty)
785 set_bit(TTY_IO_ERROR, &info->tty->flags);
786 free_page(page);
787 retval = -ENODEV;
788 goto errout;
790 if (info->xmit_buf)
791 free_page(page);
792 else
793 info->xmit_buf = (unsigned char *)page;
795 #ifdef SERIAL_DEBUG_OPEN
796 printk("starting up serial port %d...", info->line);
797 #endif
800 * Initialize the Hardware
802 sab82532_init_line(info);
804 if (info->tty->termios->c_cflag & CBAUD) {
805 u8 tmp;
807 tmp = readb(&info->regs->rw.mode);
808 tmp &= ~(SAB82532_MODE_FRTS);
809 tmp |= SAB82532_MODE_RTS;
810 writeb(tmp, &info->regs->rw.mode);
812 tmp = readb(&info->regs->rw.pvr);
813 tmp &= ~(info->pvr_dtr_bit);
814 writeb(tmp, &info->regs->rw.pvr);
818 * Finally, enable interrupts
820 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
821 SAB82532_IMR0_PLLA;
822 writeb(info->interrupt_mask0, &info->regs->w.imr0);
823 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_XOFF |
824 SAB82532_IMR1_TIN | SAB82532_IMR1_XON |
825 SAB82532_IMR1_XPR;
826 writeb(info->interrupt_mask1, &info->regs->w.imr1);
828 if (info->tty)
829 clear_bit(TTY_IO_ERROR, &info->tty->flags);
830 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
833 * and set the speed of the serial port
835 change_speed(info);
837 info->flags |= ASYNC_INITIALIZED;
838 restore_flags(flags);
839 return 0;
841 errout:
842 restore_flags(flags);
843 return retval;
847 * This routine will shutdown a serial port; interrupts are disabled, and
848 * DTR is dropped if the hangup on close termio flag is on.
850 static void shutdown(struct sab82532 *info)
852 unsigned long flags;
853 u8 tmp;
855 if (!(info->flags & ASYNC_INITIALIZED))
856 return;
858 #ifdef SERIAL_DEBUG_OPEN
859 printk("Shutting down serial port %d...", info->line);
860 #endif
862 save_flags(flags); cli(); /* Disable interrupts */
865 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
866 * here so the queue might never be waken up
868 wake_up_interruptible(&info->delta_msr_wait);
870 if (info->xmit_buf) {
871 free_page((unsigned long)info->xmit_buf);
872 info->xmit_buf = 0;
875 if (info->is_console) {
876 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
877 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
878 writeb(info->interrupt_mask0, &info->regs->w.imr0);
879 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
880 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
881 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
882 SAB82532_IMR1_XPR;
883 writeb(info->interrupt_mask1, &info->regs->w.imr1);
884 if (info->tty)
885 set_bit(TTY_IO_ERROR, &info->tty->flags);
886 info->flags &= ~ASYNC_INITIALIZED;
887 restore_flags(flags);
888 return;
891 /* Disable Interrupts */
892 info->interrupt_mask0 = 0xff;
893 writeb(info->interrupt_mask0, &info->regs->w.imr0);
894 info->interrupt_mask1 = 0xff;
895 writeb(info->interrupt_mask1, &info->regs->w.imr1);
897 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
898 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
899 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
900 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
903 /* Disable break condition */
904 tmp = readb(&info->regs->rw.dafo);
905 tmp &= ~(SAB82532_DAFO_XBRK);
906 writeb(tmp, &info->regs->rw.dafo);
908 /* Disable Receiver */
909 tmp = readb(&info->regs->rw.mode);
910 tmp &= ~(SAB82532_MODE_RAC);
911 writeb(tmp, &info->regs->rw.mode);
913 /* Power Down */
914 tmp = readb(&info->regs->rw.ccr0);
915 tmp &= ~(SAB82532_CCR0_PU);
916 writeb(tmp, &info->regs->rw.ccr0);
918 if (info->tty)
919 set_bit(TTY_IO_ERROR, &info->tty->flags);
921 info->flags &= ~ASYNC_INITIALIZED;
922 restore_flags(flags);
926 * This routine is called to set the UART divisor registers to match
927 * the specified baud rate for a serial port.
929 static void change_speed(struct sab82532 *info)
931 unsigned long flags;
932 unsigned int ebrg;
933 tcflag_t cflag;
934 unsigned char dafo;
935 int i, bits;
937 if (!info->tty || !info->tty->termios)
938 return;
939 cflag = info->tty->termios->c_cflag;
941 /* Byte size and parity */
942 switch (cflag & CSIZE) {
943 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
944 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
945 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
946 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
947 /* Never happens, but GCC is too dumb to figure it out */
948 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
951 if (cflag & CSTOPB) {
952 dafo |= SAB82532_DAFO_STOP;
953 bits++;
956 if (cflag & PARENB) {
957 dafo |= SAB82532_DAFO_PARE;
958 bits++;
961 if (cflag & PARODD) {
962 #ifdef CMSPAR
963 if (cflag & CMSPAR)
964 dafo |= SAB82532_DAFO_PAR_MARK;
965 else
966 #endif
967 dafo |= SAB82532_DAFO_PAR_ODD;
968 } else {
969 #ifdef CMSPAR
970 if (cflag & CMSPAR)
971 dafo |= SAB82532_DAFO_PAR_SPACE;
972 else
973 #endif
974 dafo |= SAB82532_DAFO_PAR_EVEN;
977 /* Determine EBRG values based on baud rate */
978 i = cflag & CBAUD;
979 if (i & CBAUDEX) {
980 i &= ~(CBAUDEX);
981 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
982 info->tty->termios->c_cflag &= ~CBAUDEX;
983 else
984 i += 15;
986 ebrg = ebrg_table[i].n;
987 ebrg |= (ebrg_table[i].m << 6);
989 info->baud = ebrg_table[i].baud;
990 if (info->baud)
991 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
992 else
993 info->timeout = 0;
994 info->timeout += HZ / 50; /* Add .02 seconds of slop */
996 /* CTS flow control flags */
997 if (cflag & CRTSCTS)
998 info->flags |= ASYNC_CTS_FLOW;
999 else
1000 info->flags &= ~(ASYNC_CTS_FLOW);
1002 if (cflag & CLOCAL)
1003 info->flags &= ~(ASYNC_CHECK_CD);
1004 else
1005 info->flags |= ASYNC_CHECK_CD;
1006 if (info->tty)
1007 info->tty->hw_stopped = 0;
1010 * Set up parity check flag
1011 * XXX: not implemented, yet.
1013 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1016 * Characters to ignore
1017 * XXX: not implemented, yet.
1021 * !!! ignore all characters if CREAD is not set
1022 * XXX: not implemented, yet.
1024 if ((cflag & CREAD) == 0)
1025 info->ignore_status_mask |= SAB82532_ISR0_RPF |
1026 SAB82532_ISR0_TCD |
1027 SAB82532_ISR0_TIME;
1029 save_flags(flags); cli();
1030 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
1031 udelay(1);
1032 sab82532_tec_wait(info);
1033 writeb(dafo, &info->regs->w.dafo);
1034 writeb(ebrg & 0xff, &info->regs->w.bgr);
1035 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1036 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1037 if (info->flags & ASYNC_CTS_FLOW) {
1038 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1039 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1040 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1041 } else {
1042 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1043 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1044 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1046 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1047 restore_flags(flags);
1050 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1052 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1053 unsigned long flags;
1055 if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1056 return;
1058 if (!tty || !info->xmit_buf)
1059 return;
1061 save_flags(flags); cli();
1062 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1063 restore_flags(flags);
1064 return;
1067 info->xmit_buf[info->xmit_head++] = ch;
1068 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1069 info->xmit_cnt++;
1070 restore_flags(flags);
1073 static void sab82532_flush_chars(struct tty_struct *tty)
1075 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1076 unsigned long flags;
1078 if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1079 return;
1081 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1082 !info->xmit_buf)
1083 return;
1085 save_flags(flags); cli();
1086 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1087 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1088 sab82532_start_tx(info);
1089 restore_flags(flags);
1092 static int sab82532_write(struct tty_struct * tty, int from_user,
1093 const unsigned char *buf, int count)
1095 int c, ret = 0;
1096 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1097 unsigned long flags;
1099 if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1100 return 0;
1102 if (!tty || !info->xmit_buf || !tmp_buf)
1103 return 0;
1105 if (from_user)
1106 down(&tmp_buf_sem);
1107 save_flags(flags);
1108 while (1) {
1109 cli();
1110 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1111 SERIAL_XMIT_SIZE - info->xmit_head));
1112 if (c <= 0)
1113 break;
1115 if (from_user) {
1116 c -= copy_from_user(tmp_buf, buf, c);
1117 if (!c) {
1118 if (!ret)
1119 ret = -EFAULT;
1120 break;
1122 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1123 SERIAL_XMIT_SIZE - info->xmit_head));
1124 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1125 } else
1126 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1127 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1128 info->xmit_cnt += c;
1129 restore_flags(flags);
1130 buf += c;
1131 count -= c;
1132 ret += c;
1134 if (from_user)
1135 up(&tmp_buf_sem);
1137 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1138 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1139 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1140 sab82532_start_tx(info);
1143 restore_flags(flags);
1144 return ret;
1147 static int sab82532_write_room(struct tty_struct *tty)
1149 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1150 int ret;
1152 if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1153 return 0;
1154 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1155 if (ret < 0)
1156 ret = 0;
1157 return ret;
1160 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1162 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1164 if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1165 return 0;
1166 return info->xmit_cnt;
1169 static void sab82532_flush_buffer(struct tty_struct *tty)
1171 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1173 if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1174 return;
1175 cli();
1176 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1177 sti();
1178 wake_up_interruptible(&tty->write_wait);
1179 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1180 tty->ldisc.write_wakeup)
1181 (tty->ldisc.write_wakeup)(tty);
1185 * This function is used to send a high-priority XON/XOFF character to
1186 * the device
1188 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1190 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1191 unsigned long flags;
1193 if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1194 return;
1196 save_flags(flags); cli();
1197 sab82532_tec_wait(info);
1198 writeb(ch, &info->regs->w.tic);
1199 restore_flags(flags);
1203 * ------------------------------------------------------------
1204 * sab82532_throttle()
1206 * This routine is called by the upper-layer tty layer to signal that
1207 * incoming characters should be throttled.
1208 * ------------------------------------------------------------
1210 static void sab82532_throttle(struct tty_struct * tty)
1212 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1213 #ifdef SERIAL_DEBUG_THROTTLE
1214 char buf[64];
1216 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1217 tty->ldisc.chars_in_buffer(tty));
1218 #endif
1220 if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1221 return;
1223 if (I_IXOFF(tty))
1224 sab82532_send_xchar(tty, STOP_CHAR(tty));
1225 #if 0
1226 if (tty->termios->c_cflag & CRTSCTS)
1227 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1228 #endif
1231 static void sab82532_unthrottle(struct tty_struct * tty)
1233 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1234 #ifdef SERIAL_DEBUG_THROTTLE
1235 char buf[64];
1237 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1238 tty->ldisc.chars_in_buffer(tty));
1239 #endif
1241 if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1242 return;
1244 if (I_IXOFF(tty)) {
1245 if (info->x_char)
1246 info->x_char = 0;
1247 else
1248 sab82532_send_xchar(tty, START_CHAR(tty));
1251 #if 0
1252 if (tty->termios->c_cflag & CRTSCTS)
1253 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1254 #endif
1258 * ------------------------------------------------------------
1259 * sab82532_ioctl() and friends
1260 * ------------------------------------------------------------
1263 static int get_serial_info(struct sab82532 *info,
1264 struct serial_struct *retinfo)
1266 struct serial_struct tmp;
1268 if (!retinfo)
1269 return -EFAULT;
1270 memset(&tmp, 0, sizeof(tmp));
1271 tmp.type = info->type;
1272 tmp.line = info->line;
1273 tmp.port = (unsigned long)info->regs;
1274 tmp.irq = info->irq;
1275 tmp.flags = info->flags;
1276 tmp.xmit_fifo_size = info->xmit_fifo_size;
1277 tmp.baud_base = info->baud_base;
1278 tmp.close_delay = info->close_delay;
1279 tmp.closing_wait = info->closing_wait;
1280 tmp.custom_divisor = info->custom_divisor;
1281 tmp.hub6 = 0;
1282 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1283 return -EFAULT;
1284 return 0;
1287 static int set_serial_info(struct sab82532 *info,
1288 struct serial_struct *new_info)
1290 return 0;
1295 * get_lsr_info - get line status register info
1297 * Purpose: Let user call ioctl() to get info when the UART physically
1298 * is emptied. On bus types like RS485, the transmitter must
1299 * release the bus after transmitting. This must be done when
1300 * the transmit shift register is empty, not be done when the
1301 * transmit holding register is empty. This functionality
1302 * allows an RS485 driver to be written in user space.
1304 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1306 unsigned int result;
1308 result = (!info->xmit_buf && info->all_sent) ? TIOCSER_TEMT : 0;
1309 return put_user(result, value);
1313 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1315 unsigned int result;
1317 result = ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1318 ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1319 : TIOCM_RTS)
1320 | ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1321 | ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1322 | ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1323 | ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1324 return put_user(result,value);
1327 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1328 unsigned int *value)
1330 int error;
1331 unsigned int arg;
1333 error = get_user(arg, value);
1334 if (error)
1335 return error;
1336 switch (cmd) {
1337 case TIOCMBIS:
1338 if (arg & TIOCM_RTS) {
1339 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1340 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1342 if (arg & TIOCM_DTR) {
1343 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1345 break;
1346 case TIOCMBIC:
1347 if (arg & TIOCM_RTS) {
1348 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1349 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1351 if (arg & TIOCM_DTR) {
1352 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1354 break;
1355 case TIOCMSET:
1356 if (arg & TIOCM_RTS) {
1357 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1358 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1359 } else {
1360 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1361 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1363 if (arg & TIOCM_DTR) {
1364 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1365 } else {
1366 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1368 break;
1369 default:
1370 return -EINVAL;
1372 return 0;
1376 * This routine sends a break character out the serial port.
1378 static void sab82532_break(struct tty_struct *tty, int break_state)
1380 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1381 unsigned long flags;
1383 if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1384 return;
1386 if (!info->regs)
1387 return;
1389 #ifdef SERIAL_DEBUG_SEND_BREAK
1390 printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1391 #endif
1392 save_flags(flags); cli();
1393 if (break_state == -1)
1394 writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1395 else
1396 writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1397 restore_flags(flags);
1401 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1402 unsigned int cmd, unsigned long arg)
1404 int error;
1405 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1406 struct async_icount cprev, cnow; /* kernel counter temps */
1407 struct serial_icounter_struct *p_cuser; /* user space */
1409 if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1410 return -ENODEV;
1412 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1413 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1414 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1415 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1416 if (tty->flags & (1 << TTY_IO_ERROR))
1417 return -EIO;
1420 switch (cmd) {
1421 case TIOCGSOFTCAR:
1422 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1423 case TIOCSSOFTCAR:
1424 error = get_user(arg, (unsigned int *) arg);
1425 if (error)
1426 return error;
1427 tty->termios->c_cflag =
1428 ((tty->termios->c_cflag & ~CLOCAL) |
1429 (arg ? CLOCAL : 0));
1430 return 0;
1431 case TIOCMGET:
1432 return get_modem_info(info, (unsigned int *) arg);
1433 case TIOCMBIS:
1434 case TIOCMBIC:
1435 case TIOCMSET:
1436 return set_modem_info(info, cmd, (unsigned int *) arg);
1437 case TIOCGSERIAL:
1438 return get_serial_info(info,
1439 (struct serial_struct *) arg);
1440 case TIOCSSERIAL:
1441 return set_serial_info(info,
1442 (struct serial_struct *) arg);
1444 case TIOCSERGETLSR: /* Get line status register */
1445 return get_lsr_info(info, (unsigned int *) arg);
1447 case TIOCSERGSTRUCT:
1448 if (copy_to_user((struct sab82532 *) arg,
1449 info, sizeof(struct sab82532)))
1450 return -EFAULT;
1451 return 0;
1454 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1455 * - mask passed in arg for lines of interest
1456 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1457 * Caller should use TIOCGICOUNT to see which one it was
1459 case TIOCMIWAIT:
1460 cli();
1461 /* note the counters on entry */
1462 cprev = info->icount;
1463 sti();
1464 while (1) {
1465 interruptible_sleep_on(&info->delta_msr_wait);
1466 /* see if a signal did it */
1467 if (signal_pending(current))
1468 return -ERESTARTSYS;
1469 cli();
1470 cnow = info->icount; /* atomic copy */
1471 sti();
1472 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1473 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1474 return -EIO; /* no change => error */
1475 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1476 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1477 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1478 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1479 return 0;
1481 cprev = cnow;
1483 /* NOTREACHED */
1486 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1487 * Return: write counters to the user passed counter struct
1488 * NB: both 1->0 and 0->1 transitions are counted except for
1489 * RI where only 0->1 is counted.
1491 case TIOCGICOUNT:
1492 cli();
1493 cnow = info->icount;
1494 sti();
1495 p_cuser = (struct serial_icounter_struct *) arg;
1496 error = put_user(cnow.cts, &p_cuser->cts);
1497 if (error) return error;
1498 error = put_user(cnow.dsr, &p_cuser->dsr);
1499 if (error) return error;
1500 error = put_user(cnow.rng, &p_cuser->rng);
1501 if (error) return error;
1502 error = put_user(cnow.dcd, &p_cuser->dcd);
1503 if (error) return error;
1504 return 0;
1506 default:
1507 return -ENOIOCTLCMD;
1509 return 0;
1512 static void sab82532_set_termios(struct tty_struct *tty,
1513 struct termios *old_termios)
1515 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1517 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1518 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1519 == RELEVANT_IFLAG(old_termios->c_iflag)))
1520 return;
1522 change_speed(info);
1524 /* Handle transition to B0 status */
1525 if ((old_termios->c_cflag & CBAUD) &&
1526 !(tty->termios->c_cflag & CBAUD)) {
1527 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1528 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1529 writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1532 /* Handle transition away from B0 status */
1533 if (!(old_termios->c_cflag & CBAUD) &&
1534 (tty->termios->c_cflag & CBAUD)) {
1535 writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1536 if (!tty->hw_stopped ||
1537 !(tty->termios->c_cflag & CRTSCTS)) {
1538 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1539 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1543 /* Handle turning off CRTSCTS */
1544 if ((old_termios->c_cflag & CRTSCTS) &&
1545 !(tty->termios->c_cflag & CRTSCTS)) {
1546 tty->hw_stopped = 0;
1547 sab82532_start(tty);
1550 #if 0
1552 * No need to wake up processes in open wait, since they
1553 * sample the CLOCAL flag once, and don't recheck it.
1554 * XXX It's not clear whether the current behavior is correct
1555 * or not. Hence, this may change.....
1557 if (!(old_termios->c_cflag & CLOCAL) &&
1558 (tty->termios->c_cflag & CLOCAL))
1559 wake_up_interruptible(&info->open_wait);
1560 #endif
1564 * ------------------------------------------------------------
1565 * sab82532_close()
1567 * This routine is called when the serial port gets closed. First, we
1568 * wait for the last remaining data to be sent. Then, we unlink its
1569 * async structure from the interrupt chain if necessary, and we free
1570 * that IRQ if nothing is left in the chain.
1571 * ------------------------------------------------------------
1573 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1575 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1576 unsigned long flags;
1578 if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1579 return;
1581 save_flags(flags); cli();
1583 if (tty_hung_up_p(filp)) {
1584 MOD_DEC_USE_COUNT;
1585 restore_flags(flags);
1586 return;
1589 #ifdef SERIAL_DEBUG_OPEN
1590 printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1591 #endif
1592 if ((tty->count == 1) && (info->count != 1)) {
1594 * Uh, oh. tty->count is 1, which means that the tty
1595 * structure will be freed. info->count should always
1596 * be one in these conditions. If it's greater than
1597 * one, we've got real problems, since it means the
1598 * serial port won't be shutdown.
1600 printk("sab82532_close: bad serial port count; tty->count is 1,"
1601 " info->count is %d\n", info->count);
1602 info->count = 1;
1604 if (--info->count < 0) {
1605 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1606 info->line, info->count);
1607 info->count = 0;
1609 if (info->count) {
1610 MOD_DEC_USE_COUNT;
1611 restore_flags(flags);
1612 return;
1614 info->flags |= ASYNC_CLOSING;
1616 * Save the termios structure, since this port may have
1617 * separate termios for callout and dialin.
1619 if (info->flags & ASYNC_NORMAL_ACTIVE)
1620 info->normal_termios = *tty->termios;
1621 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1622 info->callout_termios = *tty->termios;
1624 * Now we wait for the transmit buffer to clear; and we notify
1625 * the line discipline to only process XON/XOFF characters.
1627 tty->closing = 1;
1628 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1629 tty_wait_until_sent(tty, info->closing_wait);
1632 * At this point we stop accepting input. To do this, we
1633 * disable the receive line status interrupts, and turn off
1634 * the receiver.
1636 info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1637 writeb(info->interrupt_mask0, &info->regs->w.imr0);
1638 #if 0
1639 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RAC), &info->regs->rw.mode);
1640 #endif
1641 if (info->flags & ASYNC_INITIALIZED) {
1643 * Before we drop DTR, make sure the UART transmitter
1644 * has completely drained; this is especially
1645 * important if there is a transmit FIFO!
1647 sab82532_wait_until_sent(tty, info->timeout);
1649 shutdown(info);
1650 if (tty->driver.flush_buffer)
1651 tty->driver.flush_buffer(tty);
1652 if (tty->ldisc.flush_buffer)
1653 tty->ldisc.flush_buffer(tty);
1654 tty->closing = 0;
1655 info->event = 0;
1656 info->tty = 0;
1657 if (info->blocked_open) {
1658 if (info->close_delay) {
1659 current->state = TASK_INTERRUPTIBLE;
1660 schedule_timeout(info->close_delay);
1662 wake_up_interruptible(&info->open_wait);
1664 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1665 ASYNC_CLOSING);
1666 wake_up_interruptible(&info->close_wait);
1667 MOD_DEC_USE_COUNT;
1668 restore_flags(flags);
1672 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1674 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1676 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1677 unsigned long orig_jiffies, char_time;
1679 if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1680 return;
1682 orig_jiffies = jiffies;
1684 * Set the check interval to be 1/5 of the estimated time to
1685 * send a single character, and make it at least 1. The check
1686 * interval should also be less than the timeout.
1688 * Note: we have to use pretty tight timings here to satisfy
1689 * the NIST-PCTS.
1691 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1692 char_time = char_time / 5;
1693 if (char_time == 0)
1694 char_time = 1;
1695 if (timeout)
1696 char_time = MIN(char_time, timeout);
1697 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1698 printk("In sab82532_wait_until_sent(%d) check=%lu...", timeout, char_time);
1699 printk("jiff=%lu...", jiffies);
1700 #endif
1701 while (info->xmit_cnt || !info->all_sent) {
1702 current->state = TASK_INTERRUPTIBLE;
1703 schedule_timeout(char_time);
1704 if (signal_pending(current))
1705 break;
1706 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1707 break;
1709 current->state = TASK_RUNNING;
1710 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1711 printk("xmit_cnt = %d, alls = %d (jiff=%lu)...done\n", info->xmit_cnt, info->all_sent, jiffies);
1712 #endif
1716 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1718 static void sab82532_hangup(struct tty_struct *tty)
1720 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1722 if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1723 return;
1725 if (info->is_console)
1726 return;
1728 sab82532_flush_buffer(tty);
1729 shutdown(info);
1730 info->event = 0;
1731 info->count = 0;
1732 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1733 info->tty = 0;
1734 wake_up_interruptible(&info->open_wait);
1738 * ------------------------------------------------------------
1739 * sab82532_open() and friends
1740 * ------------------------------------------------------------
1742 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1743 struct sab82532 *info)
1745 DECLARE_WAITQUEUE(wait, current);
1746 int retval;
1747 int do_clocal = 0;
1750 * If the device is in the middle of being closed, then block
1751 * until it's done, and then try again.
1753 if (tty_hung_up_p(filp) ||
1754 (info->flags & ASYNC_CLOSING)) {
1755 if (info->flags & ASYNC_CLOSING)
1756 interruptible_sleep_on(&info->close_wait);
1757 #ifdef SERIAL_DO_RESTART
1758 if (info->flags & ASYNC_HUP_NOTIFY)
1759 return -EAGAIN;
1760 else
1761 return -ERESTARTSYS;
1762 #else
1763 return -EAGAIN;
1764 #endif
1768 * If this is a callout device, then just make sure the normal
1769 * device isn't being used.
1771 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1772 if (info->flags & ASYNC_NORMAL_ACTIVE)
1773 return -EBUSY;
1774 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1775 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1776 (info->session != current->session))
1777 return -EBUSY;
1778 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1779 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1780 (info->pgrp != current->pgrp))
1781 return -EBUSY;
1782 info->flags |= ASYNC_CALLOUT_ACTIVE;
1783 return 0;
1787 * If non-blocking mode is set, or the port is not enabled,
1788 * then make the check up front and then exit.
1790 if ((filp->f_flags & O_NONBLOCK) ||
1791 (tty->flags & (1 << TTY_IO_ERROR))) {
1792 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1793 return -EBUSY;
1794 info->flags |= ASYNC_NORMAL_ACTIVE;
1795 return 0;
1798 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1799 if (info->normal_termios.c_cflag & CLOCAL)
1800 do_clocal = 1;
1801 } else {
1802 if (tty->termios->c_cflag & CLOCAL)
1803 do_clocal = 1;
1807 * Block waiting for the carrier detect and the line to become
1808 * free (i.e., not in use by the callout). While we are in
1809 * this loop, info->count is dropped by one, so that
1810 * sab82532_close() knows when to free things. We restore it upon
1811 * exit, either normal or abnormal.
1813 retval = 0;
1814 add_wait_queue(&info->open_wait, &wait);
1815 #ifdef SERIAL_DEBUG_OPEN
1816 printk("block_til_ready before block: ttyS%d, count = %d\n",
1817 info->line, info->count);
1818 #endif
1819 cli();
1820 if (!tty_hung_up_p(filp))
1821 info->count--;
1822 sti();
1823 info->blocked_open++;
1824 while (1) {
1825 cli();
1826 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1827 (tty->termios->c_cflag & CBAUD)) {
1828 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1829 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1830 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1832 sti();
1833 set_current_state(TASK_INTERRUPTIBLE);
1834 if (tty_hung_up_p(filp) ||
1835 !(info->flags & ASYNC_INITIALIZED)) {
1836 #ifdef SERIAL_DO_RESTART
1837 if (info->flags & ASYNC_HUP_NOTIFY)
1838 retval = -EAGAIN;
1839 else
1840 retval = -ERESTARTSYS;
1841 #else
1842 retval = -EAGAIN;
1843 #endif
1844 break;
1846 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1847 !(info->flags & ASYNC_CLOSING) &&
1848 (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1849 break;
1850 if (signal_pending(current)) {
1851 retval = -ERESTARTSYS;
1852 break;
1854 #ifdef SERIAL_DEBUG_OPEN
1855 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1856 info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1857 #endif
1858 schedule();
1860 current->state = TASK_RUNNING;
1861 remove_wait_queue(&info->open_wait, &wait);
1862 if (!tty_hung_up_p(filp))
1863 info->count++;
1864 info->blocked_open--;
1865 #ifdef SERIAL_DEBUG_OPEN
1866 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1867 info->line, info->count);
1868 #endif
1869 if (retval)
1870 return retval;
1871 info->flags |= ASYNC_NORMAL_ACTIVE;
1872 return 0;
1876 * This routine is called whenever a serial port is opened. It
1877 * enables interrupts for a serial port, linking in its async structure into
1878 * the IRQ chain. It also performs the serial-specific
1879 * initialization for the tty structure.
1881 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1883 struct sab82532 *info = sab82532_chain;
1884 int retval, line;
1885 unsigned long page;
1887 #ifdef SERIAL_DEBUG_OPEN
1888 printk("sab82532_open: count = %d\n", info->count);
1889 #endif
1891 line = MINOR(tty->device) - tty->driver.minor_start;
1892 if ((line < 0) || (line >= NR_PORTS))
1893 return -ENODEV;
1895 while (info) {
1896 if (info->line == line)
1897 break;
1898 info = info->next;
1900 if (!info) {
1901 printk("sab82532_open: can't find info for line %d\n",
1902 line);
1903 return -ENODEV;
1906 if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1907 return -ENODEV;
1909 #ifdef SERIAL_DEBUG_OPEN
1910 printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1911 info->count);
1912 #endif
1914 info->count++;
1915 tty->driver_data = info;
1916 info->tty = tty;
1918 if (!tmp_buf) {
1919 page = get_free_page(GFP_KERNEL);
1920 if (!page)
1921 return -ENOMEM;
1922 if (tmp_buf)
1923 free_page(page);
1924 else
1925 tmp_buf = (unsigned char *) page;
1929 * If the port is in the middle of closing, bail out now.
1931 if (tty_hung_up_p(filp) ||
1932 (info->flags & ASYNC_CLOSING)) {
1933 if (info->flags & ASYNC_CLOSING)
1934 interruptible_sleep_on(&info->close_wait);
1935 #ifdef SERIAL_DO_RESTART
1936 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1937 -EAGAIN : -ERESTARTSYS);
1938 #else
1939 return -EAGAIN;
1940 #endif
1944 * Start up serial port
1946 retval = startup(info);
1947 if (retval)
1948 return retval;
1950 MOD_INC_USE_COUNT;
1951 retval = block_til_ready(tty, filp, info);
1952 if (retval) {
1953 #ifdef SERIAL_DEBUG_OPEN
1954 printk("sab82532_open returning after block_til_ready with %d\n",
1955 retval);
1956 #endif
1957 return retval;
1960 if ((info->count == 1) &&
1961 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1962 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1963 *tty->termios = info->normal_termios;
1964 else
1965 *tty->termios = info->callout_termios;
1966 change_speed(info);
1969 #ifdef CONFIG_SERIAL_CONSOLE
1970 if (sab82532_console.cflag && sab82532_console.index == line) {
1971 tty->termios->c_cflag = sab82532_console.cflag;
1972 sab82532_console.cflag = 0;
1973 change_speed(info);
1975 #endif
1977 info->session = current->session;
1978 info->pgrp = current->pgrp;
1980 #ifdef SERIAL_DEBUG_OPEN
1981 printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
1982 #endif
1983 return 0;
1987 * /proc fs routines....
1990 static __inline__ int
1991 line_info(char *buf, struct sab82532 *info)
1993 unsigned long flags;
1994 char stat_buf[30];
1995 int ret;
1997 ret = sprintf(buf, "%d: uart:SAB82532 ", info->line);
1998 switch (info->type) {
1999 case 0:
2000 ret += sprintf(buf+ret, "V1.0 ");
2001 break;
2002 case 1:
2003 ret += sprintf(buf+ret, "V2.0 ");
2004 break;
2005 case 2:
2006 ret += sprintf(buf+ret, "V3.2 ");
2007 break;
2008 default:
2009 ret += sprintf(buf+ret, "V?.? ");
2010 break;
2012 ret += sprintf(buf+ret, "port:%lX irq:%s",
2013 (unsigned long)info->regs, __irq_itoa(info->irq));
2015 if (!info->regs) {
2016 ret += sprintf(buf+ret, "\n");
2017 return ret;
2021 * Figure out the current RS-232 lines
2023 stat_buf[0] = 0;
2024 stat_buf[1] = 0;
2025 save_flags(flags); cli();
2026 if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2027 if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2028 strcat(stat_buf, "|RTS");
2029 } else {
2030 strcat(stat_buf, "|RTS");
2032 if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2033 strcat(stat_buf, "|CTS");
2034 if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2035 strcat(stat_buf, "|DTR");
2036 if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2037 strcat(stat_buf, "|DSR");
2038 if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2039 strcat(stat_buf, "|CD");
2040 restore_flags(flags);
2042 if (info->baud)
2043 ret += sprintf(buf+ret, " baud:%d", info->baud);
2045 if (info->icount.frame)
2046 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2048 if (info->icount.parity)
2049 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2051 if (info->icount.brk)
2052 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2054 if (info->icount.overrun)
2055 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2058 * Last thing is the RS-232 status lines.
2060 ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2061 return ret;
2064 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2065 int *eof, void *data)
2067 struct sab82532 *info = sab82532_chain;
2068 off_t begin = 0;
2069 int len = 0;
2071 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2072 for (info = sab82532_chain; info && len < 4000; info = info->next) {
2073 len += line_info(page + len, info);
2074 if (len+begin > off+count)
2075 goto done;
2076 if (len+begin < off) {
2077 begin += len;
2078 len = 0;
2081 *eof = 1;
2082 done:
2083 if (off >= len+begin)
2084 return 0;
2085 *start = page + (begin-off);
2086 return ((count < begin+len-off) ? count : begin+len-off);
2090 * ---------------------------------------------------------------------
2091 * sab82532_init() and friends
2093 * sab82532_init() is called at boot-time to initialize the serial driver.
2094 * ---------------------------------------------------------------------
2096 static int __init get_sab82532(unsigned long *memory_start)
2098 struct linux_ebus *ebus;
2099 struct linux_ebus_device *edev = 0;
2100 struct sab82532 *sab;
2101 unsigned long regs, offset;
2102 int i;
2104 for_each_ebus(ebus) {
2105 for_each_ebusdev(edev, ebus) {
2106 if (!strcmp(edev->prom_name, "se"))
2107 goto ebus_done;
2110 ebus_done:
2111 if (!edev)
2112 return -ENODEV;
2114 regs = edev->resource[0].start;
2115 offset = sizeof(union sab82532_async_regs);
2117 for (i = 0; i < 2; i++) {
2118 if (memory_start) {
2119 *memory_start = (*memory_start + 7) & ~(7);
2120 sab = (struct sab82532 *)*memory_start;
2121 *memory_start += sizeof(struct sab82532);
2122 } else {
2123 sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2124 GFP_KERNEL);
2125 if (!sab) {
2126 printk("sab82532: can't alloc sab struct\n");
2127 break;
2130 memset(sab, 0, sizeof(struct sab82532));
2132 sab->regs = (union sab82532_async_regs *)(regs + offset);
2133 sab->irq = edev->irqs[0];
2134 sab->line = 1 - i;
2135 sab->xmit_fifo_size = 32;
2136 sab->recv_fifo_size = 32;
2138 writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2140 sab->next = sab82532_chain;
2141 sab82532_chain = sab;
2143 offset -= sizeof(union sab82532_async_regs);
2145 return 0;
2148 static void __init sab82532_kgdb_hook(int line)
2150 prom_printf("sab82532: kgdb support is not implemented, yet\n");
2151 prom_halt();
2154 static inline void __init show_serial_version(void)
2156 char *revision = "$Revision: 1.35 $";
2157 char *version, *p;
2159 version = strchr(revision, ' ');
2160 strcpy(serial_version, ++version);
2161 p = strchr(serial_version, ' ');
2162 *p = '\0';
2163 printk("SAB82532 serial driver version %s\n", serial_version);
2167 * The serial driver boot-time initialization code!
2169 int __init sab82532_init(void)
2171 struct sab82532 *info;
2172 int i;
2174 if (!sab82532_chain)
2175 get_sab82532(0);
2176 if (!sab82532_chain)
2177 return -ENODEV;
2179 init_bh(SERIAL_BH, do_serial_bh);
2181 show_serial_version();
2183 /* Initialize the tty_driver structure */
2184 memset(&serial_driver, 0, sizeof(struct tty_driver));
2185 serial_driver.magic = TTY_DRIVER_MAGIC;
2186 serial_driver.driver_name = "serial";
2187 serial_driver.name = "ttyS";
2188 serial_driver.major = TTY_MAJOR;
2189 serial_driver.minor_start = 64;
2190 serial_driver.num = NR_PORTS;
2191 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2192 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2193 serial_driver.init_termios = tty_std_termios;
2194 serial_driver.init_termios.c_cflag =
2195 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2196 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2197 serial_driver.refcount = &sab82532_refcount;
2198 serial_driver.table = sab82532_table;
2199 serial_driver.termios = sab82532_termios;
2200 serial_driver.termios_locked = sab82532_termios_locked;
2202 serial_driver.open = sab82532_open;
2203 serial_driver.close = sab82532_close;
2204 serial_driver.write = sab82532_write;
2205 serial_driver.put_char = sab82532_put_char;
2206 serial_driver.flush_chars = sab82532_flush_chars;
2207 serial_driver.write_room = sab82532_write_room;
2208 serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2209 serial_driver.flush_buffer = sab82532_flush_buffer;
2210 serial_driver.ioctl = sab82532_ioctl;
2211 serial_driver.throttle = sab82532_throttle;
2212 serial_driver.unthrottle = sab82532_unthrottle;
2213 serial_driver.send_xchar = sab82532_send_xchar;
2214 serial_driver.set_termios = sab82532_set_termios;
2215 serial_driver.stop = sab82532_stop;
2216 serial_driver.start = sab82532_start;
2217 serial_driver.hangup = sab82532_hangup;
2218 serial_driver.break_ctl = sab82532_break;
2219 serial_driver.wait_until_sent = sab82532_wait_until_sent;
2220 serial_driver.read_proc = sab82532_read_proc;
2223 * The callout device is just like normal device except for
2224 * major number and the subtype code.
2226 callout_driver = serial_driver;
2227 callout_driver.name = "cua";
2228 callout_driver.major = TTYAUX_MAJOR;
2229 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2230 callout_driver.read_proc = 0;
2231 callout_driver.proc_entry = 0;
2233 if (tty_register_driver(&serial_driver))
2234 panic("Couldn't register serial driver\n");
2235 if (tty_register_driver(&callout_driver))
2236 panic("Couldn't register callout driver\n");
2238 for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2239 info->magic = SERIAL_MAGIC;
2241 info->type = readb(&info->regs->r.vstr) & 0x0f;
2242 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2243 writeb(0xff, &info->regs->w.pim);
2244 if (info->line == 0) {
2245 info->pvr_dsr_bit = (1 << 0);
2246 info->pvr_dtr_bit = (1 << 1);
2247 } else {
2248 info->pvr_dsr_bit = (1 << 3);
2249 info->pvr_dtr_bit = (1 << 2);
2251 writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2252 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2253 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2255 info->custom_divisor = 16;
2256 info->close_delay = 5*HZ/10;
2257 info->closing_wait = 30*HZ;
2258 info->x_char = 0;
2259 info->event = 0;
2260 info->blocked_open = 0;
2261 info->tqueue.routine = do_softint;
2262 info->tqueue.data = info;
2263 info->tqueue_hangup.routine = do_serial_hangup;
2264 info->tqueue_hangup.data = info;
2265 info->callout_termios = callout_driver.init_termios;
2266 info->normal_termios = serial_driver.init_termios;
2267 init_waitqueue_head(&info->open_wait);
2268 init_waitqueue_head(&info->close_wait);
2269 init_waitqueue_head(&info->delta_msr_wait);
2270 info->icount.cts = info->icount.dsr =
2271 info->icount.rng = info->icount.dcd = 0;
2272 info->icount.rx = info->icount.tx = 0;
2273 info->icount.frame = info->icount.parity = 0;
2274 info->icount.overrun = info->icount.brk = 0;
2276 if (!(info->line & 0x01)) {
2277 if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2278 "serial(sab82532)", info)) {
2279 printk("sab82532: can't get IRQ %x\n",
2280 info->irq);
2281 panic("sab82532 initialization failed");
2285 printk(KERN_INFO
2286 "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2287 info->line, (unsigned long)info->regs,
2288 __irq_itoa(info->irq), sab82532_version[info->type]);
2291 #ifdef SERIAL_LOG_DEVICE
2292 dprint_init(SERIAL_LOG_DEVICE);
2293 #endif
2294 return 0;
2297 int __init sab82532_probe(unsigned long *memory_start)
2299 int node, enode, snode;
2300 char model[32];
2301 int len;
2303 node = prom_getchild(prom_root_node);
2304 node = prom_searchsiblings(node, "pci");
2307 * Check for SUNW,sabre on Ultra 5/10/AXi.
2309 len = prom_getproperty(node, "model", model, sizeof(model));
2310 if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2311 node = prom_getchild(node);
2312 node = prom_searchsiblings(node, "pci");
2316 * For each PCI bus...
2318 while (node) {
2319 enode = prom_getchild(node);
2320 enode = prom_searchsiblings(enode, "ebus");
2323 * For each EBus on this PCI...
2325 while (enode) {
2326 snode = prom_getchild(enode);
2327 snode = prom_searchsiblings(snode, "se");
2328 if (snode)
2329 goto found;
2331 enode = prom_getsibling(enode);
2332 enode = prom_searchsiblings(enode, "ebus");
2334 node = prom_getsibling(node);
2335 node = prom_searchsiblings(node, "pci");
2337 return -ENODEV;
2339 found:
2340 #ifdef CONFIG_SERIAL_CONSOLE
2341 sunserial_setinitfunc(memory_start, sab82532_console_init);
2342 #endif
2343 sunserial_setinitfunc(memory_start, sab82532_init);
2344 rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2345 return 0;
2348 #ifdef MODULE
2349 int init_module(void)
2351 if (get_sab82532(0))
2352 return -ENODEV;
2354 return sab82532_init();
2357 void cleanup_module(void)
2359 unsigned long flags;
2360 int e1, e2;
2362 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2363 save_flags(flags);
2364 cli();
2365 timer_active &= ~(1 << RS_TIMER);
2366 timer_table[RS_TIMER].fn = NULL;
2367 timer_table[RS_TIMER].expires = 0;
2368 remove_bh(SERIAL_BH);
2369 if ((e1 = tty_unregister_driver(&serial_driver)))
2370 printk("SERIAL: failed to unregister serial driver (%d)\n",
2371 e1);
2372 if ((e2 = tty_unregister_driver(&callout_driver)))
2373 printk("SERIAL: failed to unregister callout driver (%d)\n",
2374 e2);
2375 restore_flags(flags);
2377 if (tmp_buf) {
2378 free_page((unsigned long) tmp_buf);
2379 tmp_buf = NULL;
2382 #endif /* MODULE */
2384 #ifdef CONFIG_SERIAL_CONSOLE
2386 static __inline__ void
2387 sab82532_console_putchar(struct sab82532 *info, char c)
2389 unsigned long flags;
2391 save_flags(flags); cli();
2392 sab82532_tec_wait(info);
2393 writeb(c, &info->regs->w.tic);
2394 restore_flags(flags);
2397 static void
2398 sab82532_console_write(struct console *con, const char *s, unsigned n)
2400 struct sab82532 *info;
2401 int i;
2403 info = sab82532_chain;
2404 for (i = con->index; i; i--) {
2405 info = info->next;
2406 if (!info)
2407 return;
2410 for (i = 0; i < n; i++) {
2411 if (*s == '\n')
2412 sab82532_console_putchar(info, '\r');
2413 sab82532_console_putchar(info, *s++);
2415 sab82532_tec_wait(info);
2418 static int
2419 sab82532_console_wait_key(struct console *con)
2421 sleep_on(&keypress_wait);
2422 return 0;
2425 static kdev_t
2426 sab82532_console_device(struct console *con)
2428 return MKDEV(TTY_MAJOR, 64 + con->index);
2431 static int
2432 sab82532_console_setup(struct console *con, char *options)
2434 struct sab82532 *info;
2435 unsigned int ebrg;
2436 tcflag_t cflag;
2437 unsigned char dafo;
2438 int i, bits;
2439 unsigned long flags;
2441 info = sab82532_chain;
2442 for (i = con->index; i; i--) {
2443 info = info->next;
2444 if (!info)
2445 return -ENODEV;
2447 info->is_console = 1;
2450 * Initialize the hardware
2452 sab82532_init_line(info);
2455 * Finally, enable interrupts
2457 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2458 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2459 writeb(info->interrupt_mask0, &info->regs->w.imr0);
2460 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2461 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2462 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2463 SAB82532_IMR1_XPR;
2464 writeb(info->interrupt_mask1, &info->regs->w.imr1);
2466 printk("Console: ttyS%d (SAB82532)\n", info->line);
2468 sunserial_console_termios(con);
2469 cflag = con->cflag;
2471 /* Byte size and parity */
2472 switch (cflag & CSIZE) {
2473 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2474 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
2475 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
2476 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
2477 /* Never happens, but GCC is too dumb to figure it out */
2478 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2481 if (cflag & CSTOPB) {
2482 dafo |= SAB82532_DAFO_STOP;
2483 bits++;
2486 if (cflag & PARENB) {
2487 dafo |= SAB82532_DAFO_PARE;
2488 bits++;
2491 if (cflag & PARODD) {
2492 #ifdef CMSPAR
2493 if (cflag & CMSPAR)
2494 dafo |= SAB82532_DAFO_PAR_MARK;
2495 else
2496 #endif
2497 dafo |= SAB82532_DAFO_PAR_ODD;
2498 } else {
2499 #ifdef CMSPAR
2500 if (cflag & CMSPAR)
2501 dafo |= SAB82532_DAFO_PAR_SPACE;
2502 else
2503 #endif
2504 dafo |= SAB82532_DAFO_PAR_EVEN;
2507 /* Determine EBRG values based on baud rate */
2508 i = cflag & CBAUD;
2509 if (i & CBAUDEX) {
2510 i &= ~(CBAUDEX);
2511 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
2512 cflag &= ~CBAUDEX;
2513 else
2514 i += 15;
2516 ebrg = ebrg_table[i].n;
2517 ebrg |= (ebrg_table[i].m << 6);
2519 info->baud = ebrg_table[i].baud;
2520 if (info->baud)
2521 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
2522 else
2523 info->timeout = 0;
2524 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2526 /* CTS flow control flags */
2527 if (cflag & CRTSCTS)
2528 info->flags |= ASYNC_CTS_FLOW;
2529 else
2530 info->flags &= ~(ASYNC_CTS_FLOW);
2532 if (cflag & CLOCAL)
2533 info->flags &= ~(ASYNC_CHECK_CD);
2534 else
2535 info->flags |= ASYNC_CHECK_CD;
2537 save_flags(flags); cli();
2538 if (readb(&info->regs->r.star) & SAB82532_STAR_CEC)
2539 udelay(1);
2540 sab82532_tec_wait(info);
2541 writeb(dafo, &info->regs->w.dafo);
2542 writeb(ebrg & 0xff, &info->regs->w.bgr);
2543 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
2544 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
2545 if (info->flags & ASYNC_CTS_FLOW) {
2546 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
2547 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2548 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
2549 } else {
2550 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2551 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
2552 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
2554 writeb(~(info->pvr_dtr_bit), &info->regs->rw.pvr);
2555 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
2556 restore_flags(flags);
2558 return 0;
2561 static struct console sab82532_console = {
2562 "ttyS",
2563 sab82532_console_write,
2564 NULL,
2565 sab82532_console_device,
2566 sab82532_console_wait_key,
2567 NULL,
2568 sab82532_console_setup,
2569 CON_PRINTBUFFER,
2572 NULL
2575 int __init sab82532_console_init(void)
2577 extern int con_is_present(void);
2579 if (con_is_present())
2580 return 0;
2582 if (!sab82532_chain) {
2583 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2584 prom_halt();
2587 sab82532_console.index = serial_console - 1;
2588 register_console(&sab82532_console);
2589 return 0;
2592 #ifdef SERIAL_LOG_DEVICE
2594 static int serial_log_device = 0;
2596 static void
2597 dprint_init(int tty)
2599 serial_console = tty + 1;
2600 sab82532_console.index = tty;
2601 sab82532_console_setup(&sab82532_console, "");
2602 serial_console = 0;
2603 serial_log_device = tty + 1;
2607 dprintf(const char *fmt, ...)
2609 static char buffer[4096];
2610 va_list args;
2611 int i;
2613 if (!serial_log_device)
2614 return 0;
2616 va_start(args, fmt);
2617 i = vsprintf(buffer, fmt, args);
2618 va_end(args);
2619 sab82532_console.write(&sab82532_console, buffer, i);
2620 return i;
2622 #endif /* SERIAL_LOG_DEVICE */
2623 #endif /* CONFIG_SERIAL_CONSOLE */