- David Miller: sparc and net updates. Fix merge_segments.
[davej-history.git] / drivers / sbus / char / sab82532.c
blob5de83158b295317997a668e16f2ef5ca8ddcd91f
1 /* $Id: sab82532.c,v 1.54 2000/12/07 04:35:39 anton 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/serialP.h>
19 #include <linux/serial_reg.h>
20 #include <linux/console.h>
21 #include <linux/major.h>
22 #include <linux/string.h>
23 #include <linux/fcntl.h>
24 #include <linux/ptrace.h>
25 #include <linux/ioport.h>
26 #include <linux/mm.h>
27 #include <linux/malloc.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
31 #include <asm/sab82532.h>
32 #include <asm/uaccess.h>
33 #include <asm/ebus.h>
34 #include <asm/irq.h>
36 #include "sunserial.h"
38 static DECLARE_TASK_QUEUE(tq_serial);
40 /* This is (one of many) a special gross hack to allow SU and
41 * SAB serials to co-exist on the same machine. -DaveM
43 #undef SERIAL_BH
44 #define SERIAL_BH AURORA_BH
46 static struct tty_driver serial_driver, callout_driver;
47 static int sab82532_refcount;
49 /* number of characters left in xmit buffer before we ask for more */
50 #define WAKEUP_CHARS 256
52 #define SERIAL_PARANOIA_CHECK
53 #define SERIAL_DO_RESTART
55 /* Set of debugging defines */
56 #undef SERIAL_DEBUG_OPEN
57 #undef SERIAL_DEBUG_FLOW
58 #undef SERIAL_DEBUG_MODEM
59 #undef SERIAL_DEBUG_WAIT_UNTIL_SENT
60 #undef SERIAL_DEBUG_SEND_BREAK
61 #undef SERIAL_DEBUG_INTR
62 #define SERIAL_DEBUG_OVERFLOW 1
64 /* Trace things on serial device, useful for console debugging: */
65 #undef SERIAL_LOG_DEVICE
67 #ifdef SERIAL_LOG_DEVICE
68 static void dprint_init(int tty);
69 #endif
71 static void change_speed(struct sab82532 *info);
72 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout);
75 * This assumes you have a 29.4912 MHz clock for your UART.
77 #define BASE_BAUD ( 29491200 / 16 )
79 static struct sab82532 *sab82532_chain = 0;
80 static struct tty_struct *sab82532_table[NR_PORTS];
81 static struct termios *sab82532_termios[NR_PORTS];
82 static struct termios *sab82532_termios_locked[NR_PORTS];
84 #ifdef MODULE
85 #undef CONFIG_SERIAL_CONSOLE
86 #endif
88 #ifdef CONFIG_SERIAL_CONSOLE
89 extern int serial_console;
90 static struct console sab82532_console;
91 static int sab82532_console_init(void);
92 static void batten_down_hatches(struct sab82532 *info);
93 #endif
95 #ifndef MIN
96 #define MIN(a,b) ((a) < (b) ? (a) : (b))
97 #endif
99 static char *sab82532_version[16] = {
100 "V1.0", "V2.0", "V3.2", "V(0x03)",
101 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
102 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
103 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
105 static char serial_version[16];
108 * tmp_buf is used as a temporary buffer by sab82532_write. We need to
109 * lock it in case the copy_from_user blocks while swapping in a page,
110 * and some other program tries to do a serial write at the same time.
111 * Since the lock will only come under contention when the system is
112 * swapping and available memory is low, it makes sense to share one
113 * buffer across all the serial ports, since it significantly saves
114 * memory if large numbers of serial ports are open.
116 static unsigned char *tmp_buf = 0;
117 static DECLARE_MUTEX(tmp_buf_sem);
119 static inline int serial_paranoia_check(struct sab82532 *info,
120 kdev_t device, const char *routine)
122 #ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct (%s) in %s\n";
125 static const char *badinfo =
126 "Warning: null sab82532 for (%s) in %s\n";
128 if (!info) {
129 printk(badinfo, kdevname(device), routine);
130 return 1;
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, kdevname(device), routine);
134 return 1;
136 #endif
137 return 0;
141 * This is used to figure out the divisor speeds.
143 * The formula is: Baud = BASE_BAUD / ((N + 1) * (1 << M)),
145 * with 0 <= N < 64 and 0 <= M < 16
147 * XXX: Speeds with M = 0 might not work properly for XTAL frequencies
148 * above 10 MHz.
150 struct ebrg_struct {
151 int baud;
152 int n;
153 int m;
156 static struct ebrg_struct ebrg_table[] = {
157 { 0, 0, 0 },
158 { 50, 35, 10 },
159 { 75, 47, 9 },
160 { 110, 32, 9 },
161 { 134, 53, 8 },
162 { 150, 47, 8 },
163 { 200, 35, 8 },
164 { 300, 47, 7 },
165 { 600, 47, 6 },
166 { 1200, 47, 5 },
167 { 1800, 31, 5 },
168 { 2400, 47, 4 },
169 { 4800, 47, 3 },
170 { 9600, 47, 2 },
171 { 19200, 47, 1 },
172 { 38400, 23, 1 },
173 { 57600, 15, 1 },
174 { 115200, 7, 1 },
175 { 230400, 3, 1 },
176 { 460800, 1, 1 },
177 { 76800, 11, 1 },
178 { 153600, 5, 1 },
179 { 307200, 3, 1 },
180 { 614400, 3, 0 },
181 { 921600, 0, 1 },
184 #define NR_EBRG_VALUES (sizeof(ebrg_table)/sizeof(struct ebrg_struct))
186 #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
187 #define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
189 static __inline__ void sab82532_tec_wait(struct sab82532 *info)
191 int timeout = info->tec_timeout;
193 while ((readb(&info->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
194 udelay(1);
197 static __inline__ void sab82532_cec_wait(struct sab82532 *info)
199 int timeout = info->cec_timeout;
201 while ((readb(&info->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
202 udelay(1);
205 static __inline__ void sab82532_start_tx(struct sab82532 *info)
207 unsigned long flags;
208 int i;
210 save_flags(flags); cli();
212 if (info->xmit_cnt <= 0)
213 goto out;
215 if (!(readb(&info->regs->r.star) & SAB82532_STAR_XFW))
216 goto out;
218 info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
219 writeb(info->interrupt_mask1, &info->regs->w.imr1);
220 info->all_sent = 0;
222 for (i = 0; i < info->xmit_fifo_size; i++) {
223 u8 val = info->xmit_buf[info->xmit_tail++];
224 writeb(val, &info->regs->w.xfifo[i]);
225 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
226 info->icount.tx++;
227 if (--info->xmit_cnt <= 0)
228 break;
231 /* Issue a Transmit Frame command. */
232 sab82532_cec_wait(info);
233 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
235 out:
236 restore_flags(flags);
241 * ------------------------------------------------------------
242 * sab82532_stop() and sab82532_start()
244 * This routines are called before setting or resetting tty->stopped.
245 * They enable or disable transmitter interrupts, as necessary.
246 * ------------------------------------------------------------
248 static void sab82532_stop(struct tty_struct *tty)
250 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
251 unsigned long flags;
253 if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
254 return;
256 save_flags(flags); cli();
257 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
258 writeb(info->interrupt_mask1, &info->regs->w.imr1);
259 restore_flags(flags);
262 static void sab82532_start(struct tty_struct *tty)
264 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
265 unsigned long flags;
267 if (serial_paranoia_check(info, tty->device, "sab82532_start"))
268 return;
270 save_flags(flags); cli();
271 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
272 writeb(info->interrupt_mask1, &info->regs->w.imr1);
273 sab82532_start_tx(info);
274 restore_flags(flags);
278 * ----------------------------------------------------------------------
280 * Here starts the interrupt handling routines. All of the following
281 * subroutines are declared as inline and are folded into
282 * sab82532_interrupt(). They were separated out for readability's sake.
284 * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
285 * runs with interrupts turned off. People who may want to modify
286 * sab82532_interrupt() should try to keep the interrupt handler as fast as
287 * possible. After you are done making modifications, it is not a bad
288 * idea to do:
290 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
292 * and look at the resulting assemble code in serial.s.
294 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
295 * -----------------------------------------------------------------------
299 * This routine is used by the interrupt handler to schedule
300 * processing in the software interrupt portion of the driver.
302 static inline void sab82532_sched_event(struct sab82532 *info, int event)
304 info->event |= 1 << event;
305 queue_task(&info->tqueue, &tq_serial);
306 mark_bh(SERIAL_BH);
309 static inline void receive_chars(struct sab82532 *info,
310 union sab82532_irq_status *stat)
312 struct tty_struct *tty = info->tty;
313 unsigned char buf[32];
314 unsigned char status;
315 int free_fifo = 0;
316 int i, count = 0;
318 /* Read number of BYTES (Character + Status) available. */
319 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
320 count = info->recv_fifo_size;
321 free_fifo++;
324 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
325 count = readb(&info->regs->r.rbcl) & (info->recv_fifo_size - 1);
326 free_fifo++;
329 /* Issue a FIFO read command in case we where idle. */
330 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
331 sab82532_cec_wait(info);
332 writeb(SAB82532_CMDR_RFRD, &info->regs->w.cmdr);
333 return;
336 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
337 #ifdef SERIAL_DEBUG_OVERFLOW
338 printk("sab82532: receive_chars: RFO");
339 #endif
340 free_fifo++;
343 /* Read the FIFO. */
344 for (i = 0; i < count; i++)
345 buf[i] = readb(&info->regs->r.rfifo[i]);
347 /* Issue Receive Message Complete command. */
348 if (free_fifo) {
349 sab82532_cec_wait(info);
350 writeb(SAB82532_CMDR_RMC, &info->regs->w.cmdr);
353 #ifdef CONFIG_SERIAL_CONSOLE
354 if (info->is_console)
355 wake_up(&keypress_wait);
356 #endif
357 if (!tty)
358 return;
360 for (i = 0; i < count; ) {
361 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
362 #ifdef SERIAL_DEBUG_OVERFLOW
363 printk("sab82532: receive_chars: tty overrun\n");
364 #endif
365 info->icount.buf_overrun++;
366 break;
369 tty->flip.count++;
370 *tty->flip.char_buf_ptr++ = buf[i++];
371 status = buf[i++];
372 info->icount.rx++;
374 #ifdef SERIAL_DEBUG_INTR
375 printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
376 #endif
378 if (status & SAB82532_RSTAT_PE) {
379 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
380 info->icount.parity++;
381 } else if (status & SAB82532_RSTAT_FE) {
382 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
383 info->icount.frame++;
385 else
386 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
389 queue_task(&tty->flip.tqueue, &tq_timer);
392 static inline void transmit_chars(struct sab82532 *info,
393 union sab82532_irq_status *stat)
395 int i;
397 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
398 info->interrupt_mask1 |= SAB82532_IMR1_ALLS;
399 writeb(info->interrupt_mask1, &info->regs->w.imr1);
400 info->all_sent = 1;
403 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
404 return;
406 if (!info->tty) {
407 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
408 writeb(info->interrupt_mask1, &info->regs->w.imr1);
409 return;
412 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
413 info->tty->hw_stopped) {
414 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
415 writeb(info->interrupt_mask1, &info->regs->w.imr1);
416 return;
419 info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
420 writeb(info->interrupt_mask1, &info->regs->w.imr1);
421 info->all_sent = 0;
423 /* Stuff 32 bytes into Transmit FIFO. */
424 for (i = 0; i < info->xmit_fifo_size; i++) {
425 u8 val = info->xmit_buf[info->xmit_tail++];
426 writeb(val, &info->regs->w.xfifo[i]);
427 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
428 info->icount.tx++;
429 if (--info->xmit_cnt <= 0)
430 break;
433 /* Issue a Transmit Frame command. */
434 sab82532_cec_wait(info);
435 writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
437 if (info->xmit_cnt < WAKEUP_CHARS)
438 sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
440 #ifdef SERIAL_DEBUG_INTR
441 printk("THRE...");
442 #endif
443 if (info->xmit_cnt <= 0) {
444 info->interrupt_mask1 |= SAB82532_IMR1_XPR;
445 writeb(info->interrupt_mask1, &info->regs->w.imr1);
449 static inline void check_status(struct sab82532 *info,
450 union sab82532_irq_status *stat)
452 struct tty_struct *tty = info->tty;
453 int modem_change = 0;
455 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
456 #ifdef CONFIG_SERIAL_CONSOLE
457 if (info->is_console) {
458 batten_down_hatches(info);
459 return;
461 #endif
462 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
463 info->icount.buf_overrun++;
464 goto check_modem;
466 tty->flip.count++;
467 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
468 *tty->flip.char_buf_ptr++ = 0;
469 info->icount.brk++;
472 if (!tty)
473 return;
475 if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
476 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
477 info->icount.buf_overrun++;
478 goto check_modem;
480 tty->flip.count++;
481 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
482 *tty->flip.char_buf_ptr++ = 0;
483 info->icount.overrun++;
486 check_modem:
487 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
488 info->dcd = (readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : 1;
489 info->icount.dcd++;
490 modem_change++;
491 #ifdef SERIAL_DEBUG_MODEM
492 printk("DCD change: %d\n", info->icount.dcd);
493 #endif
495 if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
496 info->cts = readb(&info->regs->r.star) & SAB82532_STAR_CTS;
497 info->icount.cts++;
498 modem_change++;
499 #ifdef SERIAL_DEBUG_MODEM
500 printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
501 #endif
503 if ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ^ info->dsr) {
504 info->dsr = (readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : 1;
505 info->icount.dsr++;
506 modem_change++;
507 #ifdef SERIAL_DEBUG_MODEM
508 printk("DSR change: %d\n", info->icount.dsr);
509 #endif
511 if (modem_change)
512 wake_up_interruptible(&info->delta_msr_wait);
514 if ((info->flags & ASYNC_CHECK_CD) &&
515 (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
517 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
518 printk("ttys%d CD now %s...", info->line,
519 (info->dcd) ? "on" : "off");
520 #endif
522 if (info->dcd)
523 wake_up_interruptible(&info->open_wait);
524 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
525 (info->flags & ASYNC_CALLOUT_NOHUP))) {
527 #ifdef SERIAL_DEBUG_OPEN
528 printk("scheduling hangup...");
529 #endif
530 MOD_INC_USE_COUNT;
531 if (schedule_task(&info->tqueue_hangup) == 0)
532 MOD_DEC_USE_COUNT;
536 if (info->flags & ASYNC_CTS_FLOW) {
537 if (info->tty->hw_stopped) {
538 if (info->cts) {
540 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
541 printk("CTS tx start...");
542 #endif
543 info->tty->hw_stopped = 0;
544 sab82532_sched_event(info,
545 RS_EVENT_WRITE_WAKEUP);
546 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
547 writeb(info->interrupt_mask1, &info->regs->w.imr1);
548 sab82532_start_tx(info);
550 } else {
551 if (!(info->cts)) {
553 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
554 printk("CTS tx stop...");
555 #endif
556 info->tty->hw_stopped = 1;
563 * This is the serial driver's generic interrupt routine
565 static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
567 struct sab82532 *info = dev_id;
568 union sab82532_irq_status status;
570 #ifdef SERIAL_DEBUG_INTR
571 printk("sab82532_interrupt(%d)...", irq);
572 #endif
574 status.stat = 0;
575 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA0)
576 status.sreg.isr0 = readb(&info->regs->r.isr0);
577 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA1)
578 status.sreg.isr1 = readb(&info->regs->r.isr1);
580 #ifdef SERIAL_DEBUG_INTR
581 printk("%d<%02x.%02x>", info->line,
582 status.sreg.isr0, status.sreg.isr1);
583 #endif
585 if (!status.stat)
586 goto next;
588 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
589 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
590 receive_chars(info, &status);
591 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
592 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
593 check_status(info, &status);
594 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
595 transmit_chars(info, &status);
597 next:
598 info = info->next;
599 status.stat = 0;
600 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB0)
601 status.sreg.isr0 = readb(&info->regs->r.isr0);
602 if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB1)
603 status.sreg.isr1 = readb(&info->regs->r.isr1);
605 #ifdef SERIAL_DEBUG_INTR
606 printk("%d<%02x.%02x>", info->line,
607 status.sreg.isr0, status.sreg.isr1);
608 #endif
610 if (!status.stat)
611 goto done;
613 if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
614 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
615 receive_chars(info, &status);
616 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
617 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
618 check_status(info, &status);
619 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
620 transmit_chars(info, &status);
622 done:
623 #ifdef SERIAL_DEBUG_INTR
624 printk("end.\n");
625 #endif
629 * -------------------------------------------------------------------
630 * Here ends the serial interrupt routines.
631 * -------------------------------------------------------------------
635 * This routine is used to handle the "bottom half" processing for the
636 * serial driver, known also the "software interrupt" processing.
637 * This processing is done at the kernel interrupt level, after the
638 * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
639 * is where time-consuming activities which can not be done in the
640 * interrupt driver proper are done; the interrupt driver schedules
641 * them using sab82532_sched_event(), and they get done here.
643 static void do_serial_bh(void)
645 run_task_queue(&tq_serial);
648 static void do_softint(void *private_)
650 struct sab82532 *info = (struct sab82532 *)private_;
651 struct tty_struct *tty;
653 tty = info->tty;
654 if (!tty)
655 return;
657 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
658 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
659 tty->ldisc.write_wakeup)
660 (tty->ldisc.write_wakeup)(tty);
661 wake_up_interruptible(&tty->write_wait);
666 * This routine is called from the scheduler tqueue when the interrupt
667 * routine has signalled that a hangup has occurred. The path of
668 * hangup processing is:
670 * serial interrupt routine -> (scheduler tqueue) ->
671 * do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
674 static void do_serial_hangup(void *private_)
676 struct sab82532 *info = (struct sab82532 *) private_;
677 struct tty_struct *tty;
679 tty = info->tty;
680 if (tty)
681 tty_hangup(tty);
682 MOD_DEC_USE_COUNT;
685 static void
686 sab82532_init_line(struct sab82532 *info)
688 unsigned char stat, tmp;
691 * Wait for any commands or immediate characters
693 sab82532_cec_wait(info);
694 sab82532_tec_wait(info);
697 * Clear the FIFO buffers.
699 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
700 sab82532_cec_wait(info);
701 writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
704 * Clear the interrupt registers.
706 stat = readb(&info->regs->r.isr0);
707 stat = readb(&info->regs->r.isr1);
710 * Now, initialize the UART
712 writeb(0, &info->regs->w.ccr0); /* power-down */
713 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
714 SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
715 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
716 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
717 SAB82532_CCR2_TOE, &info->regs->w.ccr2);
718 writeb(0, &info->regs->w.ccr3);
719 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
720 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
721 SAB82532_MODE_RAC, &info->regs->w.mode);
722 writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
723 switch (info->recv_fifo_size) {
724 case 1:
725 tmp = readb(&info->regs->w.rfc);
726 tmp |= SAB82532_RFC_RFTH_1;
727 writeb(tmp, &info->regs->w.rfc);
728 break;
729 case 4:
730 tmp = readb(&info->regs->w.rfc);
731 tmp |= SAB82532_RFC_RFTH_4;
732 writeb(tmp, &info->regs->w.rfc);
733 break;
734 case 16:
735 tmp = readb(&info->regs->w.rfc);
736 tmp |= SAB82532_RFC_RFTH_16;
737 writeb(tmp, &info->regs->w.rfc);
738 break;
739 default:
740 info->recv_fifo_size = 32;
741 /* fall through */
742 case 32:
743 tmp = readb(&info->regs->w.rfc);
744 tmp |= SAB82532_RFC_RFTH_32;
745 writeb(tmp, &info->regs->w.rfc);
746 break;
748 tmp = readb(&info->regs->rw.ccr0);
749 tmp |= SAB82532_CCR0_PU; /* power-up */
750 writeb(tmp, &info->regs->rw.ccr0);
753 static int startup(struct sab82532 *info)
755 unsigned long flags;
756 unsigned long page;
757 int retval = 0;
759 page = get_free_page(GFP_KERNEL);
760 if (!page)
761 return -ENOMEM;
763 save_flags(flags); cli();
765 if (info->flags & ASYNC_INITIALIZED) {
766 free_page(page);
767 goto errout;
770 if (!info->regs) {
771 if (info->tty)
772 set_bit(TTY_IO_ERROR, &info->tty->flags);
773 free_page(page);
774 retval = -ENODEV;
775 goto errout;
777 if (info->xmit_buf)
778 free_page(page);
779 else
780 info->xmit_buf = (unsigned char *)page;
782 #ifdef SERIAL_DEBUG_OPEN
783 printk("starting up serial port %d...", info->line);
784 #endif
787 * Initialize the Hardware
789 sab82532_init_line(info);
791 if (info->tty->termios->c_cflag & CBAUD) {
792 u8 tmp;
794 tmp = readb(&info->regs->rw.mode);
795 tmp &= ~(SAB82532_MODE_FRTS);
796 tmp |= SAB82532_MODE_RTS;
797 writeb(tmp, &info->regs->rw.mode);
799 tmp = readb(&info->regs->rw.pvr);
800 tmp &= ~(info->pvr_dtr_bit);
801 writeb(tmp, &info->regs->rw.pvr);
805 * Finally, enable interrupts
807 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
808 SAB82532_IMR0_PLLA;
809 writeb(info->interrupt_mask0, &info->regs->w.imr0);
810 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
811 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
812 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
813 SAB82532_IMR1_XPR;
814 writeb(info->interrupt_mask1, &info->regs->w.imr1);
815 info->all_sent = 1;
817 if (info->tty)
818 clear_bit(TTY_IO_ERROR, &info->tty->flags);
819 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
822 * and set the speed of the serial port
824 change_speed(info);
826 info->flags |= ASYNC_INITIALIZED;
827 restore_flags(flags);
828 return 0;
830 errout:
831 restore_flags(flags);
832 return retval;
836 * This routine will shutdown a serial port; interrupts are disabled, and
837 * DTR is dropped if the hangup on close termio flag is on.
839 static void shutdown(struct sab82532 *info)
841 unsigned long flags;
842 u8 tmp;
844 if (!(info->flags & ASYNC_INITIALIZED))
845 return;
847 #ifdef SERIAL_DEBUG_OPEN
848 printk("Shutting down serial port %d...", info->line);
849 #endif
851 save_flags(flags); cli(); /* Disable interrupts */
854 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
855 * here so the queue might never be waken up
857 wake_up_interruptible(&info->delta_msr_wait);
859 if (info->xmit_buf) {
860 free_page((unsigned long)info->xmit_buf);
861 info->xmit_buf = 0;
864 #ifdef CONFIG_SERIAL_CONSOLE
865 if (info->is_console) {
866 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
867 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
868 writeb(info->interrupt_mask0, &info->regs->w.imr0);
869 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
870 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
871 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
872 SAB82532_IMR1_XPR;
873 writeb(info->interrupt_mask1, &info->regs->w.imr1);
874 if (info->tty)
875 set_bit(TTY_IO_ERROR, &info->tty->flags);
876 info->flags &= ~ASYNC_INITIALIZED;
877 restore_flags(flags);
878 return;
880 #endif
882 /* Disable Interrupts */
883 info->interrupt_mask0 = 0xff;
884 writeb(info->interrupt_mask0, &info->regs->w.imr0);
885 info->interrupt_mask1 = 0xff;
886 writeb(info->interrupt_mask1, &info->regs->w.imr1);
888 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
889 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
890 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
891 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
894 /* Disable break condition */
895 tmp = readb(&info->regs->rw.dafo);
896 tmp &= ~(SAB82532_DAFO_XBRK);
897 writeb(tmp, &info->regs->rw.dafo);
899 /* Disable Receiver */
900 tmp = readb(&info->regs->rw.mode);
901 tmp &= ~(SAB82532_MODE_RAC);
902 writeb(tmp, &info->regs->rw.mode);
904 /* Power Down */
905 tmp = readb(&info->regs->rw.ccr0);
906 tmp &= ~(SAB82532_CCR0_PU);
907 writeb(tmp, &info->regs->rw.ccr0);
909 if (info->tty)
910 set_bit(TTY_IO_ERROR, &info->tty->flags);
912 info->flags &= ~ASYNC_INITIALIZED;
913 restore_flags(flags);
917 * This routine is called to set the UART divisor registers to match
918 * the specified baud rate for a serial port.
920 static void change_speed(struct sab82532 *info)
922 unsigned long flags;
923 unsigned int ebrg;
924 tcflag_t cflag;
925 unsigned char dafo;
926 int i, bits;
928 if (!info->tty || !info->tty->termios)
929 return;
930 cflag = info->tty->termios->c_cflag;
932 /* Byte size and parity */
933 switch (cflag & CSIZE) {
934 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
935 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
936 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
937 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
938 /* Never happens, but GCC is too dumb to figure it out */
939 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
942 if (cflag & CSTOPB) {
943 dafo |= SAB82532_DAFO_STOP;
944 bits++;
947 if (cflag & PARENB) {
948 dafo |= SAB82532_DAFO_PARE;
949 bits++;
952 if (cflag & PARODD) {
953 #ifdef CMSPAR
954 if (cflag & CMSPAR)
955 dafo |= SAB82532_DAFO_PAR_MARK;
956 else
957 #endif
958 dafo |= SAB82532_DAFO_PAR_ODD;
959 } else {
960 #ifdef CMSPAR
961 if (cflag & CMSPAR)
962 dafo |= SAB82532_DAFO_PAR_SPACE;
963 else
964 #endif
965 dafo |= SAB82532_DAFO_PAR_EVEN;
968 /* Determine EBRG values based on baud rate */
969 i = cflag & CBAUD;
970 if (i & CBAUDEX) {
971 i &= ~(CBAUDEX);
972 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
973 info->tty->termios->c_cflag &= ~CBAUDEX;
974 else
975 i += 15;
977 ebrg = ebrg_table[i].n;
978 ebrg |= (ebrg_table[i].m << 6);
980 info->baud = ebrg_table[i].baud;
981 if (info->baud) {
982 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
983 info->tec_timeout = (10 * 1000000) / info->baud;
984 info->cec_timeout = info->tec_timeout >> 2;
985 } else {
986 info->timeout = 0;
987 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
988 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
990 info->timeout += HZ / 50; /* Add .02 seconds of slop */
992 /* CTS flow control flags */
993 if (cflag & CRTSCTS)
994 info->flags |= ASYNC_CTS_FLOW;
995 else
996 info->flags &= ~(ASYNC_CTS_FLOW);
998 if (cflag & CLOCAL)
999 info->flags &= ~(ASYNC_CHECK_CD);
1000 else
1001 info->flags |= ASYNC_CHECK_CD;
1002 if (info->tty)
1003 info->tty->hw_stopped = 0;
1006 * Set up parity check flag
1007 * XXX: not implemented, yet.
1009 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1012 * Characters to ignore
1013 * XXX: not implemented, yet.
1017 * !!! ignore all characters if CREAD is not set
1018 * XXX: not implemented, yet.
1020 if ((cflag & CREAD) == 0)
1021 info->ignore_status_mask |= SAB82532_ISR0_RPF |
1022 SAB82532_ISR0_TCD |
1023 SAB82532_ISR0_TIME;
1025 save_flags(flags); cli();
1026 sab82532_cec_wait(info);
1027 sab82532_tec_wait(info);
1028 writeb(dafo, &info->regs->w.dafo);
1029 writeb(ebrg & 0xff, &info->regs->w.bgr);
1030 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1031 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1032 if (info->flags & ASYNC_CTS_FLOW) {
1033 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1034 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1035 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1036 info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1037 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1038 } else {
1039 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1040 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1041 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1042 info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1043 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1045 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1046 restore_flags(flags);
1049 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1051 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1052 unsigned long flags;
1054 if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1055 return;
1057 if (!tty || !info->xmit_buf)
1058 return;
1060 save_flags(flags); cli();
1061 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1062 restore_flags(flags);
1063 return;
1066 info->xmit_buf[info->xmit_head++] = ch;
1067 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1068 info->xmit_cnt++;
1069 restore_flags(flags);
1072 static void sab82532_flush_chars(struct tty_struct *tty)
1074 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1075 unsigned long flags;
1077 if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1078 return;
1080 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1081 !info->xmit_buf)
1082 return;
1084 save_flags(flags); cli();
1085 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1086 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1087 sab82532_start_tx(info);
1088 restore_flags(flags);
1091 static int sab82532_write(struct tty_struct * tty, int from_user,
1092 const unsigned char *buf, int count)
1094 int c, ret = 0;
1095 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1096 unsigned long flags;
1098 if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1099 return 0;
1101 if (!tty || !info->xmit_buf || !tmp_buf)
1102 return 0;
1104 if (from_user)
1105 down(&tmp_buf_sem);
1106 save_flags(flags);
1107 while (1) {
1108 cli();
1109 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1110 SERIAL_XMIT_SIZE - info->xmit_head));
1111 if (c <= 0)
1112 break;
1114 if (from_user) {
1115 c -= copy_from_user(tmp_buf, buf, c);
1116 if (!c) {
1117 if (!ret)
1118 ret = -EFAULT;
1119 break;
1121 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1122 SERIAL_XMIT_SIZE - info->xmit_head));
1123 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1124 } else
1125 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1126 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1127 info->xmit_cnt += c;
1128 restore_flags(flags);
1129 buf += c;
1130 count -= c;
1131 ret += c;
1133 if (from_user)
1134 up(&tmp_buf_sem);
1136 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1137 info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1138 writeb(info->interrupt_mask1, &info->regs->w.imr1);
1139 sab82532_start_tx(info);
1142 restore_flags(flags);
1143 return ret;
1146 static int sab82532_write_room(struct tty_struct *tty)
1148 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1149 int ret;
1151 if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1152 return 0;
1153 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1154 if (ret < 0)
1155 ret = 0;
1156 return ret;
1159 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1161 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1163 if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1164 return 0;
1165 return info->xmit_cnt;
1168 static void sab82532_flush_buffer(struct tty_struct *tty)
1170 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1172 if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1173 return;
1174 cli();
1175 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1176 sti();
1177 wake_up_interruptible(&tty->write_wait);
1178 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1179 tty->ldisc.write_wakeup)
1180 (tty->ldisc.write_wakeup)(tty);
1184 * This function is used to send a high-priority XON/XOFF character to
1185 * the device
1187 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1189 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1190 unsigned long flags;
1192 if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1193 return;
1195 save_flags(flags); cli();
1196 sab82532_tec_wait(info);
1197 writeb(ch, &info->regs->w.tic);
1198 restore_flags(flags);
1202 * ------------------------------------------------------------
1203 * sab82532_throttle()
1205 * This routine is called by the upper-layer tty layer to signal that
1206 * incoming characters should be throttled.
1207 * ------------------------------------------------------------
1209 static void sab82532_throttle(struct tty_struct * tty)
1211 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1212 #ifdef SERIAL_DEBUG_THROTTLE
1213 char buf[64];
1215 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1216 tty->ldisc.chars_in_buffer(tty));
1217 #endif
1219 if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1220 return;
1222 if (I_IXOFF(tty))
1223 sab82532_send_xchar(tty, STOP_CHAR(tty));
1226 static void sab82532_unthrottle(struct tty_struct * tty)
1228 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1229 #ifdef SERIAL_DEBUG_THROTTLE
1230 char buf[64];
1232 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1233 tty->ldisc.chars_in_buffer(tty));
1234 #endif
1236 if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1237 return;
1239 if (I_IXOFF(tty)) {
1240 if (info->x_char)
1241 info->x_char = 0;
1242 else
1243 sab82532_send_xchar(tty, START_CHAR(tty));
1248 * ------------------------------------------------------------
1249 * sab82532_ioctl() and friends
1250 * ------------------------------------------------------------
1253 static int get_serial_info(struct sab82532 *info,
1254 struct serial_struct *retinfo)
1256 struct serial_struct tmp;
1258 if (!retinfo)
1259 return -EFAULT;
1260 memset(&tmp, 0, sizeof(tmp));
1261 tmp.type = info->type;
1262 tmp.line = info->line;
1263 tmp.port = (unsigned long)info->regs;
1264 tmp.irq = info->irq;
1265 tmp.flags = info->flags;
1266 tmp.xmit_fifo_size = info->xmit_fifo_size;
1267 tmp.baud_base = info->baud_base;
1268 tmp.close_delay = info->close_delay;
1269 tmp.closing_wait = info->closing_wait;
1270 tmp.custom_divisor = info->custom_divisor;
1271 tmp.hub6 = 0;
1272 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1273 return -EFAULT;
1274 return 0;
1277 static int set_serial_info(struct sab82532 *info,
1278 struct serial_struct *new_info)
1280 return 0;
1285 * get_lsr_info - get line status register info
1287 * Purpose: Let user call ioctl() to get info when the UART physically
1288 * is emptied. On bus types like RS485, the transmitter must
1289 * release the bus after transmitting. This must be done when
1290 * the transmit shift register is empty, not be done when the
1291 * transmit holding register is empty. This functionality
1292 * allows an RS485 driver to be written in user space.
1294 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1296 unsigned int result;
1298 result = (!info->xmit_buf && info->all_sent) ? TIOCSER_TEMT : 0;
1299 return put_user(result, value);
1303 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1305 unsigned int result;
1307 result = ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1308 ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1309 : TIOCM_RTS)
1310 | ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1311 | ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1312 | ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1313 | ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1314 return put_user(result,value);
1317 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1318 unsigned int *value)
1320 int error;
1321 unsigned int arg;
1323 error = get_user(arg, value);
1324 if (error)
1325 return error;
1326 switch (cmd) {
1327 case TIOCMBIS:
1328 if (arg & TIOCM_RTS) {
1329 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1330 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1332 if (arg & TIOCM_DTR) {
1333 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1335 break;
1336 case TIOCMBIC:
1337 if (arg & TIOCM_RTS) {
1338 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1339 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1341 if (arg & TIOCM_DTR) {
1342 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1344 break;
1345 case TIOCMSET:
1346 if (arg & TIOCM_RTS) {
1347 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1348 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1349 } else {
1350 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1351 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1353 if (arg & TIOCM_DTR) {
1354 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1355 } else {
1356 writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1358 break;
1359 default:
1360 return -EINVAL;
1362 return 0;
1366 * This routine sends a break character out the serial port.
1368 static void sab82532_break(struct tty_struct *tty, int break_state)
1370 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1371 unsigned long flags;
1373 if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1374 return;
1376 if (!info->regs)
1377 return;
1379 #ifdef SERIAL_DEBUG_SEND_BREAK
1380 printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1381 #endif
1382 save_flags(flags); cli();
1383 if (break_state == -1)
1384 writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1385 else
1386 writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1387 restore_flags(flags);
1391 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1392 unsigned int cmd, unsigned long arg)
1394 int error;
1395 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1396 struct async_icount cprev, cnow; /* kernel counter temps */
1397 struct serial_icounter_struct *p_cuser; /* user space */
1399 if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1400 return -ENODEV;
1402 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1403 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1404 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1405 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1406 if (tty->flags & (1 << TTY_IO_ERROR))
1407 return -EIO;
1410 switch (cmd) {
1411 case TIOCGSOFTCAR:
1412 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1413 case TIOCSSOFTCAR:
1414 error = get_user(arg, (unsigned int *) arg);
1415 if (error)
1416 return error;
1417 tty->termios->c_cflag =
1418 ((tty->termios->c_cflag & ~CLOCAL) |
1419 (arg ? CLOCAL : 0));
1420 return 0;
1421 case TIOCMGET:
1422 return get_modem_info(info, (unsigned int *) arg);
1423 case TIOCMBIS:
1424 case TIOCMBIC:
1425 case TIOCMSET:
1426 return set_modem_info(info, cmd, (unsigned int *) arg);
1427 case TIOCGSERIAL:
1428 return get_serial_info(info,
1429 (struct serial_struct *) arg);
1430 case TIOCSSERIAL:
1431 return set_serial_info(info,
1432 (struct serial_struct *) arg);
1434 case TIOCSERGETLSR: /* Get line status register */
1435 return get_lsr_info(info, (unsigned int *) arg);
1437 case TIOCSERGSTRUCT:
1438 if (copy_to_user((struct sab82532 *) arg,
1439 info, sizeof(struct sab82532)))
1440 return -EFAULT;
1441 return 0;
1444 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1445 * - mask passed in arg for lines of interest
1446 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1447 * Caller should use TIOCGICOUNT to see which one it was
1449 case TIOCMIWAIT:
1450 cli();
1451 /* note the counters on entry */
1452 cprev = info->icount;
1453 sti();
1454 while (1) {
1455 interruptible_sleep_on(&info->delta_msr_wait);
1456 /* see if a signal did it */
1457 if (signal_pending(current))
1458 return -ERESTARTSYS;
1459 cli();
1460 cnow = info->icount; /* atomic copy */
1461 sti();
1462 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1463 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1464 return -EIO; /* no change => error */
1465 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1466 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1467 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1468 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1469 return 0;
1471 cprev = cnow;
1473 /* NOTREACHED */
1476 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1477 * Return: write counters to the user passed counter struct
1478 * NB: both 1->0 and 0->1 transitions are counted except for
1479 * RI where only 0->1 is counted.
1481 case TIOCGICOUNT:
1482 cli();
1483 cnow = info->icount;
1484 sti();
1485 p_cuser = (struct serial_icounter_struct *) arg;
1486 error = put_user(cnow.cts, &p_cuser->cts);
1487 if (error) return error;
1488 error = put_user(cnow.dsr, &p_cuser->dsr);
1489 if (error) return error;
1490 error = put_user(cnow.rng, &p_cuser->rng);
1491 if (error) return error;
1492 error = put_user(cnow.dcd, &p_cuser->dcd);
1493 if (error) return error;
1494 return 0;
1496 default:
1497 return -ENOIOCTLCMD;
1499 return 0;
1502 static void sab82532_set_termios(struct tty_struct *tty,
1503 struct termios *old_termios)
1505 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1507 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1508 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1509 == RELEVANT_IFLAG(old_termios->c_iflag)))
1510 return;
1512 change_speed(info);
1514 /* Handle transition to B0 status */
1515 if ((old_termios->c_cflag & CBAUD) &&
1516 !(tty->termios->c_cflag & CBAUD)) {
1517 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1518 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1519 writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1522 /* Handle transition away from B0 status */
1523 if (!(old_termios->c_cflag & CBAUD) &&
1524 (tty->termios->c_cflag & CBAUD)) {
1525 writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1526 if (!tty->hw_stopped ||
1527 !(tty->termios->c_cflag & CRTSCTS)) {
1528 writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1529 writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1533 /* Handle turning off CRTSCTS */
1534 if ((old_termios->c_cflag & CRTSCTS) &&
1535 !(tty->termios->c_cflag & CRTSCTS)) {
1536 tty->hw_stopped = 0;
1537 sab82532_start(tty);
1542 * ------------------------------------------------------------
1543 * sab82532_close()
1545 * This routine is called when the serial port gets closed. First, we
1546 * wait for the last remaining data to be sent. Then, we unlink its
1547 * async structure from the interrupt chain if necessary, and we free
1548 * that IRQ if nothing is left in the chain.
1549 * ------------------------------------------------------------
1551 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1553 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1554 unsigned long flags;
1556 if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1557 return;
1559 save_flags(flags); cli();
1561 if (tty_hung_up_p(filp)) {
1562 MOD_DEC_USE_COUNT;
1563 restore_flags(flags);
1564 return;
1567 #ifdef SERIAL_DEBUG_OPEN
1568 printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1569 #endif
1570 if ((tty->count == 1) && (info->count != 1)) {
1572 * Uh, oh. tty->count is 1, which means that the tty
1573 * structure will be freed. info->count should always
1574 * be one in these conditions. If it's greater than
1575 * one, we've got real problems, since it means the
1576 * serial port won't be shutdown.
1578 printk("sab82532_close: bad serial port count; tty->count is 1,"
1579 " info->count is %d\n", info->count);
1580 info->count = 1;
1582 if (--info->count < 0) {
1583 printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1584 info->line, info->count);
1585 info->count = 0;
1587 if (info->count) {
1588 MOD_DEC_USE_COUNT;
1589 restore_flags(flags);
1590 return;
1592 info->flags |= ASYNC_CLOSING;
1594 * Save the termios structure, since this port may have
1595 * separate termios for callout and dialin.
1597 if (info->flags & ASYNC_NORMAL_ACTIVE)
1598 info->normal_termios = *tty->termios;
1599 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1600 info->callout_termios = *tty->termios;
1602 * Now we wait for the transmit buffer to clear; and we notify
1603 * the line discipline to only process XON/XOFF characters.
1605 tty->closing = 1;
1606 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1607 tty_wait_until_sent(tty, info->closing_wait);
1610 * At this point we stop accepting input. To do this, we
1611 * disable the receive line status interrupts, and turn off
1612 * the receiver.
1614 info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1615 writeb(info->interrupt_mask0, &info->regs->w.imr0);
1616 if (info->flags & ASYNC_INITIALIZED) {
1618 * Before we drop DTR, make sure the UART transmitter
1619 * has completely drained; this is especially
1620 * important if there is a transmit FIFO!
1622 sab82532_wait_until_sent(tty, info->timeout);
1624 shutdown(info);
1625 if (tty->driver.flush_buffer)
1626 tty->driver.flush_buffer(tty);
1627 if (tty->ldisc.flush_buffer)
1628 tty->ldisc.flush_buffer(tty);
1629 tty->closing = 0;
1630 info->event = 0;
1631 info->tty = 0;
1632 if (info->blocked_open) {
1633 if (info->close_delay) {
1634 current->state = TASK_INTERRUPTIBLE;
1635 schedule_timeout(info->close_delay);
1637 wake_up_interruptible(&info->open_wait);
1639 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1640 ASYNC_CLOSING);
1641 wake_up_interruptible(&info->close_wait);
1642 MOD_DEC_USE_COUNT;
1643 restore_flags(flags);
1647 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1649 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1651 struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1652 unsigned long orig_jiffies, char_time;
1654 if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1655 return;
1657 orig_jiffies = jiffies;
1659 * Set the check interval to be 1/5 of the estimated time to
1660 * send a single character, and make it at least 1. The check
1661 * interval should also be less than the timeout.
1663 * Note: we have to use pretty tight timings here to satisfy
1664 * the NIST-PCTS.
1666 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1667 char_time = char_time / 5;
1668 if (char_time == 0)
1669 char_time = 1;
1670 if (timeout)
1671 char_time = MIN(char_time, timeout);
1672 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1673 printk("In sab82532_wait_until_sent(%d) check=%lu...", timeout, char_time);
1674 printk("jiff=%lu...", jiffies);
1675 #endif
1676 while (info->xmit_cnt || !info->all_sent) {
1677 current->state = TASK_INTERRUPTIBLE;
1678 schedule_timeout(char_time);
1679 if (signal_pending(current))
1680 break;
1681 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1682 break;
1684 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1685 printk("xmit_cnt = %d, alls = %d (jiff=%lu)...done\n", info->xmit_cnt, info->all_sent, jiffies);
1686 #endif
1690 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1692 static void sab82532_hangup(struct tty_struct *tty)
1694 struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1696 if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1697 return;
1699 #ifdef CONFIG_SERIAL_CONSOLE
1700 if (info->is_console)
1701 return;
1702 #endif
1704 sab82532_flush_buffer(tty);
1705 shutdown(info);
1706 info->event = 0;
1707 info->count = 0;
1708 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1709 info->tty = 0;
1710 wake_up_interruptible(&info->open_wait);
1714 * ------------------------------------------------------------
1715 * sab82532_open() and friends
1716 * ------------------------------------------------------------
1718 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1719 struct sab82532 *info)
1721 DECLARE_WAITQUEUE(wait, current);
1722 int retval;
1723 int do_clocal = 0;
1726 * If the device is in the middle of being closed, then block
1727 * until it's done, and then try again.
1729 if (tty_hung_up_p(filp) ||
1730 (info->flags & ASYNC_CLOSING)) {
1731 if (info->flags & ASYNC_CLOSING)
1732 interruptible_sleep_on(&info->close_wait);
1733 #ifdef SERIAL_DO_RESTART
1734 if (info->flags & ASYNC_HUP_NOTIFY)
1735 return -EAGAIN;
1736 else
1737 return -ERESTARTSYS;
1738 #else
1739 return -EAGAIN;
1740 #endif
1744 * If this is a callout device, then just make sure the normal
1745 * device isn't being used.
1747 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1748 if (info->flags & ASYNC_NORMAL_ACTIVE)
1749 return -EBUSY;
1750 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1751 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1752 (info->session != current->session))
1753 return -EBUSY;
1754 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1755 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1756 (info->pgrp != current->pgrp))
1757 return -EBUSY;
1758 info->flags |= ASYNC_CALLOUT_ACTIVE;
1759 return 0;
1763 * If non-blocking mode is set, or the port is not enabled,
1764 * then make the check up front and then exit.
1766 if ((filp->f_flags & O_NONBLOCK) ||
1767 (tty->flags & (1 << TTY_IO_ERROR))) {
1768 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1769 return -EBUSY;
1770 info->flags |= ASYNC_NORMAL_ACTIVE;
1771 return 0;
1774 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1775 if (info->normal_termios.c_cflag & CLOCAL)
1776 do_clocal = 1;
1777 } else {
1778 if (tty->termios->c_cflag & CLOCAL)
1779 do_clocal = 1;
1783 * Block waiting for the carrier detect and the line to become
1784 * free (i.e., not in use by the callout). While we are in
1785 * this loop, info->count is dropped by one, so that
1786 * sab82532_close() knows when to free things. We restore it upon
1787 * exit, either normal or abnormal.
1789 retval = 0;
1790 add_wait_queue(&info->open_wait, &wait);
1791 #ifdef SERIAL_DEBUG_OPEN
1792 printk("block_til_ready before block: ttyS%d, count = %d\n",
1793 info->line, info->count);
1794 #endif
1795 cli();
1796 if (!tty_hung_up_p(filp))
1797 info->count--;
1798 sti();
1799 info->blocked_open++;
1800 while (1) {
1801 cli();
1802 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1803 (tty->termios->c_cflag & CBAUD)) {
1804 writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1805 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1806 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1808 sti();
1809 set_current_state(TASK_INTERRUPTIBLE);
1810 if (tty_hung_up_p(filp) ||
1811 !(info->flags & ASYNC_INITIALIZED)) {
1812 #ifdef SERIAL_DO_RESTART
1813 if (info->flags & ASYNC_HUP_NOTIFY)
1814 retval = -EAGAIN;
1815 else
1816 retval = -ERESTARTSYS;
1817 #else
1818 retval = -EAGAIN;
1819 #endif
1820 break;
1822 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1823 !(info->flags & ASYNC_CLOSING) &&
1824 (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1825 break;
1826 if (signal_pending(current)) {
1827 retval = -ERESTARTSYS;
1828 break;
1830 #ifdef SERIAL_DEBUG_OPEN
1831 printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1832 info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1833 #endif
1834 schedule();
1836 current->state = TASK_RUNNING;
1837 remove_wait_queue(&info->open_wait, &wait);
1838 if (!tty_hung_up_p(filp))
1839 info->count++;
1840 info->blocked_open--;
1841 #ifdef SERIAL_DEBUG_OPEN
1842 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1843 info->line, info->count);
1844 #endif
1845 if (retval)
1846 return retval;
1847 info->flags |= ASYNC_NORMAL_ACTIVE;
1848 return 0;
1852 * This routine is called whenever a serial port is opened. It
1853 * enables interrupts for a serial port, linking in its async structure into
1854 * the IRQ chain. It also performs the serial-specific
1855 * initialization for the tty structure.
1857 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1859 struct sab82532 *info = sab82532_chain;
1860 int retval, line;
1861 unsigned long page;
1863 #ifdef SERIAL_DEBUG_OPEN
1864 printk("sab82532_open: count = %d\n", info->count);
1865 #endif
1867 line = MINOR(tty->device) - tty->driver.minor_start;
1868 if ((line < 0) || (line >= NR_PORTS))
1869 return -ENODEV;
1871 while (info) {
1872 if (info->line == line)
1873 break;
1874 info = info->next;
1876 if (!info) {
1877 printk("sab82532_open: can't find info for line %d\n",
1878 line);
1879 return -ENODEV;
1882 if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1883 return -ENODEV;
1885 #ifdef SERIAL_DEBUG_OPEN
1886 printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1887 info->count);
1888 #endif
1890 info->count++;
1891 tty->driver_data = info;
1892 info->tty = tty;
1894 if (!tmp_buf) {
1895 page = get_free_page(GFP_KERNEL);
1896 if (!page)
1897 return -ENOMEM;
1898 if (tmp_buf)
1899 free_page(page);
1900 else
1901 tmp_buf = (unsigned char *) page;
1905 * If the port is in the middle of closing, bail out now.
1907 if (tty_hung_up_p(filp) ||
1908 (info->flags & ASYNC_CLOSING)) {
1909 if (info->flags & ASYNC_CLOSING)
1910 interruptible_sleep_on(&info->close_wait);
1911 #ifdef SERIAL_DO_RESTART
1912 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1913 -EAGAIN : -ERESTARTSYS);
1914 #else
1915 return -EAGAIN;
1916 #endif
1920 * Start up serial port
1922 retval = startup(info);
1923 if (retval)
1924 return retval;
1926 MOD_INC_USE_COUNT;
1927 retval = block_til_ready(tty, filp, info);
1928 if (retval) {
1929 #ifdef SERIAL_DEBUG_OPEN
1930 printk("sab82532_open returning after block_til_ready with %d\n",
1931 retval);
1932 #endif
1933 return retval;
1936 if ((info->count == 1) &&
1937 (info->flags & ASYNC_SPLIT_TERMIOS)) {
1938 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1939 *tty->termios = info->normal_termios;
1940 else
1941 *tty->termios = info->callout_termios;
1942 change_speed(info);
1945 #ifdef CONFIG_SERIAL_CONSOLE
1946 if (sab82532_console.cflag && sab82532_console.index == line) {
1947 tty->termios->c_cflag = sab82532_console.cflag;
1948 sab82532_console.cflag = 0;
1949 change_speed(info);
1951 #endif
1953 info->session = current->session;
1954 info->pgrp = current->pgrp;
1956 #ifdef SERIAL_DEBUG_OPEN
1957 printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
1958 #endif
1959 return 0;
1963 * /proc fs routines....
1966 static __inline__ int
1967 line_info(char *buf, struct sab82532 *info)
1969 unsigned long flags;
1970 char stat_buf[30];
1971 int ret;
1973 ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
1974 switch (info->type) {
1975 case 0:
1976 ret += sprintf(buf+ret, "V1.0 ");
1977 break;
1978 case 1:
1979 ret += sprintf(buf+ret, "V2.0 ");
1980 break;
1981 case 2:
1982 ret += sprintf(buf+ret, "V3.2 ");
1983 break;
1984 default:
1985 ret += sprintf(buf+ret, "V?.? ");
1986 break;
1988 ret += sprintf(buf+ret, "port:%lX irq:%s",
1989 (unsigned long)info->regs, __irq_itoa(info->irq));
1991 if (!info->regs) {
1992 ret += sprintf(buf+ret, "\n");
1993 return ret;
1997 * Figure out the current RS-232 lines
1999 stat_buf[0] = 0;
2000 stat_buf[1] = 0;
2001 save_flags(flags); cli();
2002 if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2003 if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2004 strcat(stat_buf, "|RTS");
2005 } else {
2006 strcat(stat_buf, "|RTS");
2008 if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2009 strcat(stat_buf, "|CTS");
2010 if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2011 strcat(stat_buf, "|DTR");
2012 if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2013 strcat(stat_buf, "|DSR");
2014 if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2015 strcat(stat_buf, "|CD");
2016 restore_flags(flags);
2018 if (info->baud)
2019 ret += sprintf(buf+ret, " baud:%u", info->baud);
2021 ret += sprintf(buf+ret, " tx:%u rx:%u",
2022 info->icount.tx, info->icount.rx);
2024 if (info->icount.frame)
2025 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2027 if (info->icount.parity)
2028 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2030 if (info->icount.brk)
2031 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2033 if (info->icount.overrun)
2034 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2037 * Last thing is the RS-232 status lines.
2039 ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2040 return ret;
2043 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2044 int *eof, void *data)
2046 struct sab82532 *info = sab82532_chain;
2047 off_t begin = 0;
2048 int len = 0;
2050 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2051 for (info = sab82532_chain; info && len < 4000; info = info->next) {
2052 len += line_info(page + len, info);
2053 if (len+begin > off+count)
2054 goto done;
2055 if (len+begin < off) {
2056 begin += len;
2057 len = 0;
2060 *eof = 1;
2061 done:
2062 if (off >= len+begin)
2063 return 0;
2064 *start = page + (off-begin);
2065 return ((count < begin+len-off) ? count : begin+len-off);
2069 * ---------------------------------------------------------------------
2070 * sab82532_init() and friends
2072 * sab82532_init() is called at boot-time to initialize the serial driver.
2073 * ---------------------------------------------------------------------
2075 static int __init get_sab82532(unsigned long *memory_start)
2077 struct linux_ebus *ebus;
2078 struct linux_ebus_device *edev = 0;
2079 struct sab82532 *sab;
2080 unsigned long regs, offset;
2081 int i;
2083 for_each_ebus(ebus) {
2084 for_each_ebusdev(edev, ebus) {
2085 if (!strcmp(edev->prom_name, "se"))
2086 goto ebus_done;
2089 ebus_done:
2090 if (!edev)
2091 return -ENODEV;
2093 regs = edev->resource[0].start;
2094 offset = sizeof(union sab82532_async_regs);
2096 for (i = 0; i < 2; i++) {
2097 if (memory_start) {
2098 *memory_start = (*memory_start + 7) & ~(7);
2099 sab = (struct sab82532 *)*memory_start;
2100 *memory_start += sizeof(struct sab82532);
2101 } else {
2102 sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2103 GFP_KERNEL);
2104 if (!sab) {
2105 printk("sab82532: can't alloc sab struct\n");
2106 break;
2109 memset(sab, 0, sizeof(struct sab82532));
2111 sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2112 sab->irq = edev->irqs[0];
2113 sab->line = 1 - i;
2114 sab->xmit_fifo_size = 32;
2115 sab->recv_fifo_size = 32;
2117 writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2119 sab->next = sab82532_chain;
2120 sab82532_chain = sab;
2122 offset -= sizeof(union sab82532_async_regs);
2124 return 0;
2127 #ifndef MODULE
2128 static void __init sab82532_kgdb_hook(int line)
2130 prom_printf("sab82532: kgdb support is not implemented, yet\n");
2131 prom_halt();
2133 #endif
2135 static inline void __init show_serial_version(void)
2137 char *revision = "$Revision: 1.54 $";
2138 char *version, *p;
2140 version = strchr(revision, ' ');
2141 strcpy(serial_version, ++version);
2142 p = strchr(serial_version, ' ');
2143 *p = '\0';
2144 printk("SAB82532 serial driver version %s\n", serial_version);
2147 extern int su_num_ports;
2150 * The serial driver boot-time initialization code!
2152 int __init 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 #ifdef CONFIG_DEVFS_FS
2171 serial_driver.name = "tts/%d";
2172 #else
2173 serial_driver.name = "ttyS";
2174 #endif
2175 serial_driver.major = TTY_MAJOR;
2176 serial_driver.minor_start = 64 + su_num_ports;
2177 serial_driver.num = NR_PORTS;
2178 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2179 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2180 serial_driver.init_termios = tty_std_termios;
2181 serial_driver.init_termios.c_cflag =
2182 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2183 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2184 serial_driver.refcount = &sab82532_refcount;
2185 serial_driver.table = sab82532_table;
2186 serial_driver.termios = sab82532_termios;
2187 serial_driver.termios_locked = sab82532_termios_locked;
2189 serial_driver.open = sab82532_open;
2190 serial_driver.close = sab82532_close;
2191 serial_driver.write = sab82532_write;
2192 serial_driver.put_char = sab82532_put_char;
2193 serial_driver.flush_chars = sab82532_flush_chars;
2194 serial_driver.write_room = sab82532_write_room;
2195 serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2196 serial_driver.flush_buffer = sab82532_flush_buffer;
2197 serial_driver.ioctl = sab82532_ioctl;
2198 serial_driver.throttle = sab82532_throttle;
2199 serial_driver.unthrottle = sab82532_unthrottle;
2200 serial_driver.send_xchar = sab82532_send_xchar;
2201 serial_driver.set_termios = sab82532_set_termios;
2202 serial_driver.stop = sab82532_stop;
2203 serial_driver.start = sab82532_start;
2204 serial_driver.hangup = sab82532_hangup;
2205 serial_driver.break_ctl = sab82532_break;
2206 serial_driver.wait_until_sent = sab82532_wait_until_sent;
2207 serial_driver.read_proc = sab82532_read_proc;
2210 * The callout device is just like normal device except for
2211 * major number and the subtype code.
2213 callout_driver = serial_driver;
2214 #ifdef CONFIG_DEVFS_FS
2215 callout_driver.name = "cua/%d";
2216 #else
2217 callout_driver.name = "cua";
2218 #endif
2219 callout_driver.major = TTYAUX_MAJOR;
2220 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2221 callout_driver.read_proc = 0;
2222 callout_driver.proc_entry = 0;
2224 if (tty_register_driver(&serial_driver))
2225 panic("Couldn't register serial driver\n");
2226 if (tty_register_driver(&callout_driver))
2227 panic("Couldn't register callout driver\n");
2229 for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2230 info->magic = SERIAL_MAGIC;
2232 info->type = readb(&info->regs->r.vstr) & 0x0f;
2233 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2234 writeb(0xff, &info->regs->w.pim);
2235 if (info->line == 0) {
2236 info->pvr_dsr_bit = (1 << 0);
2237 info->pvr_dtr_bit = (1 << 1);
2238 } else {
2239 info->pvr_dsr_bit = (1 << 3);
2240 info->pvr_dtr_bit = (1 << 2);
2242 writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2243 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2244 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2246 info->custom_divisor = 16;
2247 info->close_delay = 5*HZ/10;
2248 info->closing_wait = 30*HZ;
2249 info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2250 info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2251 info->x_char = 0;
2252 info->event = 0;
2253 info->blocked_open = 0;
2254 info->tqueue.routine = do_softint;
2255 info->tqueue.data = info;
2256 info->tqueue_hangup.routine = do_serial_hangup;
2257 info->tqueue_hangup.data = info;
2258 info->callout_termios = callout_driver.init_termios;
2259 info->normal_termios = serial_driver.init_termios;
2260 init_waitqueue_head(&info->open_wait);
2261 init_waitqueue_head(&info->close_wait);
2262 init_waitqueue_head(&info->delta_msr_wait);
2263 info->icount.cts = info->icount.dsr =
2264 info->icount.rng = info->icount.dcd = 0;
2265 info->icount.rx = info->icount.tx = 0;
2266 info->icount.frame = info->icount.parity = 0;
2267 info->icount.overrun = info->icount.brk = 0;
2269 if (!(info->line & 0x01)) {
2270 if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2271 "serial(sab82532)", info)) {
2272 printk("sab82532: can't get IRQ %x\n",
2273 info->irq);
2274 panic("sab82532 initialization failed");
2278 printk(KERN_INFO
2279 "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2280 info->line + su_num_ports, (unsigned long)info->regs,
2281 __irq_itoa(info->irq), sab82532_version[info->type]);
2284 #ifdef SERIAL_LOG_DEVICE
2285 dprint_init(SERIAL_LOG_DEVICE);
2286 #endif
2287 return 0;
2290 int __init sab82532_probe(void)
2292 int node, enode, snode;
2293 char model[32];
2294 int len;
2296 node = prom_getchild(prom_root_node);
2297 node = prom_searchsiblings(node, "pci");
2300 * Check for SUNW,sabre on Ultra 5/10/AXi.
2302 len = prom_getproperty(node, "model", model, sizeof(model));
2303 if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2304 node = prom_getchild(node);
2305 node = prom_searchsiblings(node, "pci");
2309 * For each PCI bus...
2311 while (node) {
2312 enode = prom_getchild(node);
2313 enode = prom_searchsiblings(enode, "ebus");
2316 * For each EBus on this PCI...
2318 while (enode) {
2319 snode = prom_getchild(enode);
2320 snode = prom_searchsiblings(snode, "se");
2321 if (snode)
2322 goto found;
2324 enode = prom_getsibling(enode);
2325 enode = prom_searchsiblings(enode, "ebus");
2327 node = prom_getsibling(node);
2328 node = prom_searchsiblings(node, "pci");
2330 return -ENODEV;
2332 found:
2333 #ifdef CONFIG_SERIAL_CONSOLE
2334 sunserial_setinitfunc(sab82532_console_init);
2335 #endif
2336 #ifndef MODULE
2337 sunserial_setinitfunc(sab82532_init);
2338 rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2339 #endif
2340 return 0;
2343 #ifdef MODULE
2344 int init_module(void)
2346 if (get_sab82532(0))
2347 return -ENODEV;
2349 return sab82532_init();
2352 void cleanup_module(void)
2354 struct sab82532 *sab;
2355 unsigned long flags;
2356 int e1, e2;
2358 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2359 save_flags(flags);
2360 cli();
2361 remove_bh(SERIAL_BH);
2362 if ((e1 = tty_unregister_driver(&serial_driver)))
2363 printk("SERIAL: failed to unregister serial driver (%d)\n",
2364 e1);
2365 if ((e2 = tty_unregister_driver(&callout_driver)))
2366 printk("SERIAL: failed to unregister callout driver (%d)\n",
2367 e2);
2368 restore_flags(flags);
2370 if (tmp_buf) {
2371 free_page((unsigned long) tmp_buf);
2372 tmp_buf = NULL;
2374 for (sab = sab82532_chain; sab; sab = sab->next) {
2375 if (!(sab->line & 0x01))
2376 free_irq(sab->irq, sab);
2377 iounmap(sab->regs);
2380 #endif /* MODULE */
2382 #ifdef CONFIG_SERIAL_CONSOLE
2383 static void
2384 batten_down_hatches(struct sab82532 *info)
2386 unsigned char saved_rfc, tmp;
2388 if (!stop_a_enabled)
2389 return;
2391 /* If we are doing kadb, we call the debugger
2392 * else we just drop into the boot monitor.
2393 * Note that we must flush the user windows
2394 * first before giving up control.
2396 printk("\n");
2397 flush_user_windows();
2400 * Set FIFO to single character mode.
2402 saved_rfc = readb(&info->regs->r.rfc);
2403 tmp = readb(&info->regs->rw.rfc);
2404 tmp &= ~(SAB82532_RFC_RFDF);
2405 writeb(tmp, &info->regs->rw.rfc);
2406 sab82532_cec_wait(info);
2407 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2409 #ifndef __sparc_v9__
2410 if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2411 (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2412 sp_enter_debugger();
2413 else
2414 #endif
2415 prom_cmdline();
2418 * Reset FIFO to character + status mode.
2420 writeb(saved_rfc, &info->regs->w.rfc);
2421 sab82532_cec_wait(info);
2422 writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2425 static __inline__ void
2426 sab82532_console_putchar(struct sab82532 *info, char c)
2428 unsigned long flags;
2430 save_flags(flags); cli();
2431 sab82532_tec_wait(info);
2432 writeb(c, &info->regs->w.tic);
2433 restore_flags(flags);
2436 static void
2437 sab82532_console_write(struct console *con, const char *s, unsigned n)
2439 struct sab82532 *info;
2440 int i;
2442 info = sab82532_chain;
2443 for (i = con->index; i; i--) {
2444 info = info->next;
2445 if (!info)
2446 return;
2449 for (i = 0; i < n; i++) {
2450 if (*s == '\n')
2451 sab82532_console_putchar(info, '\r');
2452 sab82532_console_putchar(info, *s++);
2454 sab82532_tec_wait(info);
2457 static int
2458 sab82532_console_wait_key(struct console *con)
2460 sleep_on(&keypress_wait);
2461 return 0;
2464 static kdev_t
2465 sab82532_console_device(struct console *con)
2467 return MKDEV(TTY_MAJOR, 64 + con->index);
2470 static int
2471 sab82532_console_setup(struct console *con, char *options)
2473 struct sab82532 *info;
2474 unsigned int ebrg;
2475 tcflag_t cflag;
2476 unsigned char dafo;
2477 int i, bits;
2478 unsigned long flags;
2480 info = sab82532_chain;
2481 for (i = con->index; i; i--) {
2482 info = info->next;
2483 if (!info)
2484 return -ENODEV;
2486 info->is_console = 1;
2489 * Initialize the hardware
2491 sab82532_init_line(info);
2494 * Finally, enable interrupts
2496 info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2497 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2498 writeb(info->interrupt_mask0, &info->regs->w.imr0);
2499 info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2500 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2501 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2502 SAB82532_IMR1_XPR;
2503 writeb(info->interrupt_mask1, &info->regs->w.imr1);
2505 printk("Console: ttyS%d (SAB82532)\n", info->line);
2507 sunserial_console_termios(con);
2508 cflag = con->cflag;
2510 /* Byte size and parity */
2511 switch (cflag & CSIZE) {
2512 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2513 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
2514 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
2515 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
2516 /* Never happens, but GCC is too dumb to figure it out */
2517 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
2520 if (cflag & CSTOPB) {
2521 dafo |= SAB82532_DAFO_STOP;
2522 bits++;
2525 if (cflag & PARENB) {
2526 dafo |= SAB82532_DAFO_PARE;
2527 bits++;
2530 if (cflag & PARODD) {
2531 #ifdef CMSPAR
2532 if (cflag & CMSPAR)
2533 dafo |= SAB82532_DAFO_PAR_MARK;
2534 else
2535 #endif
2536 dafo |= SAB82532_DAFO_PAR_ODD;
2537 } else {
2538 #ifdef CMSPAR
2539 if (cflag & CMSPAR)
2540 dafo |= SAB82532_DAFO_PAR_SPACE;
2541 else
2542 #endif
2543 dafo |= SAB82532_DAFO_PAR_EVEN;
2546 /* Determine EBRG values based on baud rate */
2547 i = cflag & CBAUD;
2548 if (i & CBAUDEX) {
2549 i &= ~(CBAUDEX);
2550 if ((i < 1) || ((i + 15) >= NR_EBRG_VALUES))
2551 cflag &= ~CBAUDEX;
2552 else
2553 i += 15;
2555 ebrg = ebrg_table[i].n;
2556 ebrg |= (ebrg_table[i].m << 6);
2558 info->baud = ebrg_table[i].baud;
2559 if (info->baud)
2560 info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
2561 else
2562 info->timeout = 0;
2563 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2565 /* CTS flow control flags */
2566 if (cflag & CRTSCTS)
2567 info->flags |= ASYNC_CTS_FLOW;
2568 else
2569 info->flags &= ~(ASYNC_CTS_FLOW);
2571 if (cflag & CLOCAL)
2572 info->flags &= ~(ASYNC_CHECK_CD);
2573 else
2574 info->flags |= ASYNC_CHECK_CD;
2576 save_flags(flags); cli();
2577 sab82532_cec_wait(info);
2578 sab82532_tec_wait(info);
2579 writeb(dafo, &info->regs->w.dafo);
2580 writeb(ebrg & 0xff, &info->regs->w.bgr);
2581 writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
2582 writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
2583 if (info->flags & ASYNC_CTS_FLOW) {
2584 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
2585 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2586 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
2587 } else {
2588 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2589 writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
2590 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
2592 writeb(~(info->pvr_dtr_bit), &info->regs->rw.pvr);
2593 writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
2594 restore_flags(flags);
2596 return 0;
2599 static struct console sab82532_console = {
2600 name: "ttyS",
2601 write: sab82532_console_write,
2602 device: sab82532_console_device,
2603 wait_key: sab82532_console_wait_key,
2604 setup: sab82532_console_setup,
2605 flags: CON_PRINTBUFFER,
2606 index: -1,
2609 int __init sab82532_console_init(void)
2611 extern int con_is_present(void);
2612 extern int su_console_registered;
2614 if (con_is_present() || su_console_registered)
2615 return 0;
2617 if (!sab82532_chain) {
2618 prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2619 prom_halt();
2622 sab82532_console.index = serial_console - 1;
2623 register_console(&sab82532_console);
2624 return 0;
2627 #ifdef SERIAL_LOG_DEVICE
2629 static int serial_log_device = 0;
2631 static void
2632 dprint_init(int tty)
2634 serial_console = tty + 1;
2635 sab82532_console.index = tty;
2636 sab82532_console_setup(&sab82532_console, "");
2637 serial_console = 0;
2638 serial_log_device = tty + 1;
2642 dprintf(const char *fmt, ...)
2644 static char buffer[4096];
2645 va_list args;
2646 int i;
2648 if (!serial_log_device)
2649 return 0;
2651 va_start(args, fmt);
2652 i = vsprintf(buffer, fmt, args);
2653 va_end(args);
2654 sab82532_console.write(&sab82532_console, buffer, i);
2655 return i;
2657 #endif /* SERIAL_LOG_DEVICE */
2658 #endif /* CONFIG_SERIAL_CONSOLE */