Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / sunsab.c
blob8caaf2e5e47c01815e5e570b67d1c329a0e91402
1 /* sunsab.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
3 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 * Maxim Krasnyanskiy <maxk@qualcomm.com>
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART. Also eliminated a lot of
11 * duplicated code in the console setup.
12 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
14 * Ported to new 2.5.x UART layer.
15 * David S. Miller <davem@redhat.com>
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/major.h>
26 #include <linux/string.h>
27 #include <linux/ptrace.h>
28 #include <linux/ioport.h>
29 #include <linux/circ_buf.h>
30 #include <linux/serial.h>
31 #include <linux/sysrq.h>
32 #include <linux/console.h>
33 #include <linux/spinlock.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/init.h>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/oplib.h>
41 #include <asm/ebus.h>
43 #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
44 #define SUPPORT_SYSRQ
45 #endif
47 #include <linux/serial_core.h>
49 #include "suncore.h"
50 #include "sunsab.h"
52 struct uart_sunsab_port {
53 struct uart_port port; /* Generic UART port */
54 union sab82532_async_regs __iomem *regs; /* Chip registers */
55 unsigned long irqflags; /* IRQ state flags */
56 int dsr; /* Current DSR state */
57 unsigned int cec_timeout; /* Chip poll timeout... */
58 unsigned int tec_timeout; /* likewise */
59 unsigned char interrupt_mask0;/* ISR0 masking */
60 unsigned char interrupt_mask1;/* ISR1 masking */
61 unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
62 unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
63 int type; /* SAB82532 version */
67 * This assumes you have a 29.4912 MHz clock for your UART.
69 #define SAB_BASE_BAUD ( 29491200 / 16 )
71 static char *sab82532_version[16] = {
72 "V1.0", "V2.0", "V3.2", "V(0x03)",
73 "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
74 "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
75 "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
78 #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
79 #define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
81 #define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
82 #define SAB82532_XMIT_FIFO_SIZE 32
84 static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
86 int timeout = up->tec_timeout;
88 while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
89 udelay(1);
92 static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
94 int timeout = up->cec_timeout;
96 while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
97 udelay(1);
100 static struct tty_struct *
101 receive_chars(struct uart_sunsab_port *up,
102 union sab82532_irq_status *stat,
103 struct pt_regs *regs)
105 struct tty_struct *tty = NULL;
106 unsigned char buf[32];
107 int saw_console_brk = 0;
108 int free_fifo = 0;
109 int count = 0;
110 int i;
112 if (up->port.info != NULL) /* Unopened serial console */
113 tty = up->port.info->tty;
115 /* Read number of BYTES (Character + Status) available. */
116 if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
117 count = SAB82532_RECV_FIFO_SIZE;
118 free_fifo++;
121 if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
122 count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
123 free_fifo++;
126 /* Issue a FIFO read command in case we where idle. */
127 if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
128 sunsab_cec_wait(up);
129 writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
130 return tty;
133 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
134 free_fifo++;
136 /* Read the FIFO. */
137 for (i = 0; i < count; i++)
138 buf[i] = readb(&up->regs->r.rfifo[i]);
140 /* Issue Receive Message Complete command. */
141 if (free_fifo) {
142 sunsab_cec_wait(up);
143 writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
146 /* Count may be zero for BRK, so we check for it here */
147 if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
148 (up->port.line == up->port.cons->index))
149 saw_console_brk = 1;
151 for (i = 0; i < count; i++) {
152 unsigned char ch = buf[i];
154 if (tty == NULL) {
155 uart_handle_sysrq_char(&up->port, ch, regs);
156 continue;
159 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
160 tty->flip.work.func((void *)tty);
161 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
162 return tty; // if TTY_DONT_FLIP is set
165 *tty->flip.char_buf_ptr = ch;
166 *tty->flip.flag_buf_ptr = TTY_NORMAL;
167 up->port.icount.rx++;
169 if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
170 SAB82532_ISR0_FERR |
171 SAB82532_ISR0_RFO)) ||
172 unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
174 * For statistics only
176 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
177 stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
178 SAB82532_ISR0_FERR);
179 up->port.icount.brk++;
181 * We do the SysRQ and SAK checking
182 * here because otherwise the break
183 * may get masked by ignore_status_mask
184 * or read_status_mask.
186 if (uart_handle_break(&up->port))
187 continue;
188 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
189 up->port.icount.parity++;
190 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
191 up->port.icount.frame++;
192 if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
193 up->port.icount.overrun++;
196 * Mask off conditions which should be ingored.
198 stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
199 stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
201 if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
202 *tty->flip.flag_buf_ptr = TTY_BREAK;
203 } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
204 *tty->flip.flag_buf_ptr = TTY_PARITY;
205 else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
206 *tty->flip.flag_buf_ptr = TTY_FRAME;
209 if (uart_handle_sysrq_char(&up->port, ch, regs))
210 continue;
212 if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
213 (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0){
214 tty->flip.flag_buf_ptr++;
215 tty->flip.char_buf_ptr++;
216 tty->flip.count++;
218 if ((stat->sreg.isr0 & SAB82532_ISR0_RFO) &&
219 tty->flip.count < TTY_FLIPBUF_SIZE) {
221 * Overrun is special, since it's reported
222 * immediately, and doesn't affect the current
223 * character.
225 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
226 tty->flip.flag_buf_ptr++;
227 tty->flip.char_buf_ptr++;
228 tty->flip.count++;
232 if (saw_console_brk)
233 sun_do_break();
235 return tty;
238 static void sunsab_stop_tx(struct uart_port *, unsigned int);
240 static void transmit_chars(struct uart_sunsab_port *up,
241 union sab82532_irq_status *stat)
243 struct circ_buf *xmit = &up->port.info->xmit;
244 int i;
246 if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
247 up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
248 writeb(up->interrupt_mask1, &up->regs->w.imr1);
249 set_bit(SAB82532_ALLS, &up->irqflags);
252 #if 0 /* bde@nwlink.com says this check causes problems */
253 if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
254 return;
255 #endif
257 if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
258 return;
260 set_bit(SAB82532_XPR, &up->irqflags);
262 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
263 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
264 writeb(up->interrupt_mask1, &up->regs->w.imr1);
265 uart_write_wakeup(&up->port);
266 return;
269 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
270 writeb(up->interrupt_mask1, &up->regs->w.imr1);
271 clear_bit(SAB82532_ALLS, &up->irqflags);
273 /* Stuff 32 bytes into Transmit FIFO. */
274 clear_bit(SAB82532_XPR, &up->irqflags);
275 for (i = 0; i < up->port.fifosize; i++) {
276 writeb(xmit->buf[xmit->tail],
277 &up->regs->w.xfifo[i]);
278 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
279 up->port.icount.tx++;
280 if (uart_circ_empty(xmit))
281 break;
284 /* Issue a Transmit Frame command. */
285 sunsab_cec_wait(up);
286 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
288 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
289 uart_write_wakeup(&up->port);
291 if (uart_circ_empty(xmit))
292 sunsab_stop_tx(&up->port, 0);
295 static void check_status(struct uart_sunsab_port *up,
296 union sab82532_irq_status *stat)
298 if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
299 uart_handle_dcd_change(&up->port,
300 !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
302 if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
303 uart_handle_cts_change(&up->port,
304 (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
306 if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
307 up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
308 up->port.icount.dsr++;
311 wake_up_interruptible(&up->port.info->delta_msr_wait);
314 static irqreturn_t sunsab_interrupt(int irq, void *dev_id, struct pt_regs *regs)
316 struct uart_sunsab_port *up = dev_id;
317 struct tty_struct *tty;
318 union sab82532_irq_status status;
319 unsigned long flags;
321 spin_lock_irqsave(&up->port.lock, flags);
323 status.stat = 0;
324 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA0)
325 status.sreg.isr0 = readb(&up->regs->r.isr0);
326 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA1)
327 status.sreg.isr1 = readb(&up->regs->r.isr1);
329 tty = NULL;
330 if (status.stat) {
331 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
332 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
333 (status.sreg.isr1 & SAB82532_ISR1_BRK))
334 tty = receive_chars(up, &status, regs);
335 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
336 (status.sreg.isr1 & SAB82532_ISR1_CSC))
337 check_status(up, &status);
338 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
339 transmit_chars(up, &status);
342 spin_unlock(&up->port.lock);
344 if (tty)
345 tty_flip_buffer_push(tty);
347 up++;
349 spin_lock(&up->port.lock);
351 status.stat = 0;
352 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB0)
353 status.sreg.isr0 = readb(&up->regs->r.isr0);
354 if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB1)
355 status.sreg.isr1 = readb(&up->regs->r.isr1);
357 tty = NULL;
358 if (status.stat) {
359 if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
360 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
361 (status.sreg.isr1 & SAB82532_ISR1_BRK))
363 tty = receive_chars(up, &status, regs);
364 if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
365 (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
366 check_status(up, &status);
367 if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
368 transmit_chars(up, &status);
371 spin_unlock_irqrestore(&up->port.lock, flags);
373 if (tty)
374 tty_flip_buffer_push(tty);
376 return IRQ_HANDLED;
379 /* port->lock is not held. */
380 static unsigned int sunsab_tx_empty(struct uart_port *port)
382 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
383 int ret;
385 /* Do not need a lock for a state test like this. */
386 if (test_bit(SAB82532_ALLS, &up->irqflags))
387 ret = TIOCSER_TEMT;
388 else
389 ret = 0;
391 return ret;
394 /* port->lock held by caller. */
395 static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
397 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
399 if (mctrl & TIOCM_RTS) {
400 writeb(readb(&up->regs->rw.mode) & ~SAB82532_MODE_FRTS,
401 &up->regs->rw.mode);
402 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
403 &up->regs->rw.mode);
404 } else {
405 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
406 &up->regs->rw.mode);
407 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
408 &up->regs->rw.mode);
410 if (mctrl & TIOCM_DTR) {
411 writeb(readb(&up->regs->rw.pvr) & ~(up->pvr_dtr_bit), &up->regs->rw.pvr);
412 } else {
413 writeb(readb(&up->regs->rw.pvr) | up->pvr_dtr_bit, &up->regs->rw.pvr);
417 /* port->lock is not held. */
418 static unsigned int sunsab_get_mctrl(struct uart_port *port)
420 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
421 unsigned long flags;
422 unsigned char val;
423 unsigned int result;
425 result = 0;
427 spin_lock_irqsave(&up->port.lock, flags);
429 val = readb(&up->regs->r.pvr);
430 result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
432 val = readb(&up->regs->r.vstr);
433 result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
435 val = readb(&up->regs->r.star);
436 result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
438 spin_unlock_irqrestore(&up->port.lock, flags);
440 return result;
443 /* port->lock held by caller. */
444 static void sunsab_stop_tx(struct uart_port *port, unsigned int tty_stop)
446 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
448 up->interrupt_mask1 |= SAB82532_IMR1_XPR;
449 writeb(up->interrupt_mask1, &up->regs->w.imr1);
452 /* port->lock held by caller. */
453 static void sunsab_start_tx(struct uart_port *port, unsigned int tty_start)
455 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
456 struct circ_buf *xmit = &up->port.info->xmit;
457 int i;
459 up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
460 writeb(up->interrupt_mask1, &up->regs->w.imr1);
462 if (!test_bit(SAB82532_XPR, &up->irqflags))
463 return;
465 clear_bit(SAB82532_ALLS, &up->irqflags);
466 clear_bit(SAB82532_XPR, &up->irqflags);
468 for (i = 0; i < up->port.fifosize; i++) {
469 writeb(xmit->buf[xmit->tail],
470 &up->regs->w.xfifo[i]);
471 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
472 up->port.icount.tx++;
473 if (uart_circ_empty(xmit))
474 break;
477 /* Issue a Transmit Frame command. */
478 sunsab_cec_wait(up);
479 writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
482 /* port->lock is not held. */
483 static void sunsab_send_xchar(struct uart_port *port, char ch)
485 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
486 unsigned long flags;
488 spin_lock_irqsave(&up->port.lock, flags);
490 sunsab_tec_wait(up);
491 writeb(ch, &up->regs->w.tic);
493 spin_unlock_irqrestore(&up->port.lock, flags);
496 /* port->lock held by caller. */
497 static void sunsab_stop_rx(struct uart_port *port)
499 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
501 up->interrupt_mask0 |= SAB82532_ISR0_TCD;
502 writeb(up->interrupt_mask1, &up->regs->w.imr0);
505 /* port->lock held by caller. */
506 static void sunsab_enable_ms(struct uart_port *port)
508 /* For now we always receive these interrupts. */
511 /* port->lock is not held. */
512 static void sunsab_break_ctl(struct uart_port *port, int break_state)
514 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
515 unsigned long flags;
516 unsigned char val;
518 spin_lock_irqsave(&up->port.lock, flags);
520 val = readb(&up->regs->rw.dafo);
521 if (break_state)
522 val |= SAB82532_DAFO_XBRK;
523 else
524 val &= ~SAB82532_DAFO_XBRK;
525 writeb(val, &up->regs->rw.dafo);
527 spin_unlock_irqrestore(&up->port.lock, flags);
530 /* port->lock is not held. */
531 static int sunsab_startup(struct uart_port *port)
533 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
534 unsigned long flags;
535 unsigned char tmp;
537 spin_lock_irqsave(&up->port.lock, flags);
540 * Wait for any commands or immediate characters
542 sunsab_cec_wait(up);
543 sunsab_tec_wait(up);
546 * Clear the FIFO buffers.
548 writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
549 sunsab_cec_wait(up);
550 writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
553 * Clear the interrupt registers.
555 (void) readb(&up->regs->r.isr0);
556 (void) readb(&up->regs->r.isr1);
559 * Now, initialize the UART
561 writeb(0, &up->regs->w.ccr0); /* power-down */
562 writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
563 SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
564 writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
565 writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
566 SAB82532_CCR2_TOE, &up->regs->w.ccr2);
567 writeb(0, &up->regs->w.ccr3);
568 writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
569 writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
570 SAB82532_MODE_RAC, &up->regs->w.mode);
571 writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
573 tmp = readb(&up->regs->rw.ccr0);
574 tmp |= SAB82532_CCR0_PU; /* power-up */
575 writeb(tmp, &up->regs->rw.ccr0);
578 * Finally, enable interrupts
580 up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
581 SAB82532_IMR0_PLLA);
582 writeb(up->interrupt_mask0, &up->regs->w.imr0);
583 up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
584 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
585 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
586 SAB82532_IMR1_XPR);
587 writeb(up->interrupt_mask1, &up->regs->w.imr1);
588 set_bit(SAB82532_ALLS, &up->irqflags);
589 set_bit(SAB82532_XPR, &up->irqflags);
591 spin_unlock_irqrestore(&up->port.lock, flags);
593 return 0;
596 /* port->lock is not held. */
597 static void sunsab_shutdown(struct uart_port *port)
599 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
600 unsigned long flags;
601 unsigned char tmp;
603 spin_lock_irqsave(&up->port.lock, flags);
605 /* Disable Interrupts */
606 up->interrupt_mask0 = 0xff;
607 writeb(up->interrupt_mask0, &up->regs->w.imr0);
608 up->interrupt_mask1 = 0xff;
609 writeb(up->interrupt_mask1, &up->regs->w.imr1);
611 /* Disable break condition */
612 tmp = readb(&up->regs->rw.dafo);
613 tmp &= ~SAB82532_DAFO_XBRK;
614 writeb(tmp, &up->regs->rw.dafo);
616 /* Disable Receiver */
617 tmp = readb(&up->regs->rw.mode);
618 tmp &= ~SAB82532_MODE_RAC;
619 writeb(tmp, &up->regs->rw.mode);
622 * XXX FIXME
624 * If the chip is powered down here the system hangs/crashes during
625 * reboot or shutdown. This needs to be investigated further,
626 * similar behaviour occurs in 2.4 when the driver is configured
627 * as a module only. One hint may be that data is sometimes
628 * transmitted at 9600 baud during shutdown (regardless of the
629 * speed the chip was configured for when the port was open).
631 #if 0
632 /* Power Down */
633 tmp = readb(&up->regs->rw.ccr0);
634 tmp &= ~SAB82532_CCR0_PU;
635 writeb(tmp, &up->regs->rw.ccr0);
636 #endif
638 spin_unlock_irqrestore(&up->port.lock, flags);
642 * This is used to figure out the divisor speeds.
644 * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
646 * with 0 <= N < 64 and 0 <= M < 16
649 static void calc_ebrg(int baud, int *n_ret, int *m_ret)
651 int n, m;
653 if (baud == 0) {
654 *n_ret = 0;
655 *m_ret = 0;
656 return;
660 * We scale numbers by 10 so that we get better accuracy
661 * without having to use floating point. Here we increment m
662 * until n is within the valid range.
664 n = (SAB_BASE_BAUD * 10) / baud;
665 m = 0;
666 while (n >= 640) {
667 n = n / 2;
668 m++;
670 n = (n+5) / 10;
672 * We try very hard to avoid speeds with M == 0 since they may
673 * not work correctly for XTAL frequences above 10 MHz.
675 if ((m == 0) && ((n & 1) == 0)) {
676 n = n / 2;
677 m++;
679 *n_ret = n - 1;
680 *m_ret = m;
683 /* Internal routine, port->lock is held and local interrupts are disabled. */
684 static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
685 unsigned int iflag, int baud)
687 unsigned int ebrg;
688 unsigned char dafo;
689 int bits, n, m;
691 /* Byte size and parity */
692 switch (cflag & CSIZE) {
693 case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
694 case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
695 case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
696 case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
697 /* Never happens, but GCC is too dumb to figure it out */
698 default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
701 if (cflag & CSTOPB) {
702 dafo |= SAB82532_DAFO_STOP;
703 bits++;
706 if (cflag & PARENB) {
707 dafo |= SAB82532_DAFO_PARE;
708 bits++;
711 if (cflag & PARODD) {
712 dafo |= SAB82532_DAFO_PAR_ODD;
713 } else {
714 dafo |= SAB82532_DAFO_PAR_EVEN;
717 calc_ebrg(baud, &n, &m);
719 ebrg = n | (m << 6);
721 up->tec_timeout = (10 * 1000000) / baud;
722 up->cec_timeout = up->tec_timeout >> 2;
724 /* CTS flow control flags */
725 /* We encode read_status_mask and ignore_status_mask like so:
727 * ---------------------
728 * | ... | ISR1 | ISR0 |
729 * ---------------------
730 * .. 15 8 7 0
733 up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
734 SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
735 SAB82532_ISR0_CDSC);
736 up->port.read_status_mask |= (SAB82532_ISR1_CSC |
737 SAB82532_ISR1_ALLS |
738 SAB82532_ISR1_XPR) << 8;
739 if (iflag & INPCK)
740 up->port.read_status_mask |= (SAB82532_ISR0_PERR |
741 SAB82532_ISR0_FERR);
742 if (iflag & (BRKINT | PARMRK))
743 up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
746 * Characteres to ignore
748 up->port.ignore_status_mask = 0;
749 if (iflag & IGNPAR)
750 up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
751 SAB82532_ISR0_FERR);
752 if (iflag & IGNBRK) {
753 up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
755 * If we're ignoring parity and break indicators,
756 * ignore overruns too (for real raw support).
758 if (iflag & IGNPAR)
759 up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
763 * ignore all characters if CREAD is not set
765 if ((cflag & CREAD) == 0)
766 up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
767 SAB82532_ISR0_TCD);
769 /* Now bang the new settings into the chip. */
770 sunsab_cec_wait(up);
771 sunsab_tec_wait(up);
772 writeb(dafo, &up->regs->w.dafo);
773 writeb(ebrg & 0xff, &up->regs->w.bgr);
774 writeb((readb(&up->regs->rw.ccr2) & ~0xc0) | ((ebrg >> 2) & 0xc0),
775 &up->regs->rw.ccr2);
777 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RAC, &up->regs->rw.mode);
781 /* port->lock is not held. */
782 static void sunsab_set_termios(struct uart_port *port, struct termios *termios,
783 struct termios *old)
785 struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
786 unsigned long flags;
787 int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
789 spin_lock_irqsave(&up->port.lock, flags);
790 sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud);
791 spin_unlock_irqrestore(&up->port.lock, flags);
794 static const char *sunsab_type(struct uart_port *port)
796 struct uart_sunsab_port *up = (void *)port;
797 static char buf[36];
799 sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
800 return buf;
803 static void sunsab_release_port(struct uart_port *port)
807 static int sunsab_request_port(struct uart_port *port)
809 return 0;
812 static void sunsab_config_port(struct uart_port *port, int flags)
816 static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
818 return -EINVAL;
821 static struct uart_ops sunsab_pops = {
822 .tx_empty = sunsab_tx_empty,
823 .set_mctrl = sunsab_set_mctrl,
824 .get_mctrl = sunsab_get_mctrl,
825 .stop_tx = sunsab_stop_tx,
826 .start_tx = sunsab_start_tx,
827 .send_xchar = sunsab_send_xchar,
828 .stop_rx = sunsab_stop_rx,
829 .enable_ms = sunsab_enable_ms,
830 .break_ctl = sunsab_break_ctl,
831 .startup = sunsab_startup,
832 .shutdown = sunsab_shutdown,
833 .set_termios = sunsab_set_termios,
834 .type = sunsab_type,
835 .release_port = sunsab_release_port,
836 .request_port = sunsab_request_port,
837 .config_port = sunsab_config_port,
838 .verify_port = sunsab_verify_port,
841 static struct uart_driver sunsab_reg = {
842 .owner = THIS_MODULE,
843 .driver_name = "serial",
844 .devfs_name = "tts/",
845 .dev_name = "ttyS",
846 .major = TTY_MAJOR,
849 static struct uart_sunsab_port *sunsab_ports;
850 static int num_channels;
852 #ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
854 static __inline__ void sunsab_console_putchar(struct uart_sunsab_port *up, char c)
856 unsigned long flags;
858 spin_lock_irqsave(&up->port.lock, flags);
860 sunsab_tec_wait(up);
861 writeb(c, &up->regs->w.tic);
863 spin_unlock_irqrestore(&up->port.lock, flags);
866 static void sunsab_console_write(struct console *con, const char *s, unsigned n)
868 struct uart_sunsab_port *up = &sunsab_ports[con->index];
869 int i;
871 for (i = 0; i < n; i++) {
872 if (*s == '\n')
873 sunsab_console_putchar(up, '\r');
874 sunsab_console_putchar(up, *s++);
876 sunsab_tec_wait(up);
879 static int sunsab_console_setup(struct console *con, char *options)
881 struct uart_sunsab_port *up = &sunsab_ports[con->index];
882 unsigned long flags;
883 int baud;
885 printk("Console: ttyS%d (SAB82532)\n",
886 (sunsab_reg.minor - 64) + con->index);
888 sunserial_console_termios(con);
890 /* Firmware console speed is limited to 150-->38400 baud so
891 * this hackish cflag thing is OK.
893 switch (con->cflag & CBAUD) {
894 case B150: baud = 150; break;
895 case B300: baud = 300; break;
896 case B600: baud = 600; break;
897 case B1200: baud = 1200; break;
898 case B2400: baud = 2400; break;
899 case B4800: baud = 4800; break;
900 default: case B9600: baud = 9600; break;
901 case B19200: baud = 19200; break;
902 case B38400: baud = 38400; break;
906 * Temporary fix.
908 spin_lock_init(&up->port.lock);
911 * Initialize the hardware
913 sunsab_startup(&up->port);
915 spin_lock_irqsave(&up->port.lock, flags);
918 * Finally, enable interrupts
920 up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
921 SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
922 writeb(up->interrupt_mask0, &up->regs->w.imr0);
923 up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
924 SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
925 SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
926 SAB82532_IMR1_XPR;
927 writeb(up->interrupt_mask1, &up->regs->w.imr1);
929 sunsab_convert_to_sab(up, con->cflag, 0, baud);
930 sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
932 spin_unlock_irqrestore(&up->port.lock, flags);
934 return 0;
937 static struct console sunsab_console = {
938 .name = "ttyS",
939 .write = sunsab_console_write,
940 .device = uart_console_device,
941 .setup = sunsab_console_setup,
942 .flags = CON_PRINTBUFFER,
943 .index = -1,
944 .data = &sunsab_reg,
946 #define SUNSAB_CONSOLE (&sunsab_console)
948 static void __init sunsab_console_init(void)
950 int i;
952 if (con_is_present())
953 return;
955 for (i = 0; i < num_channels; i++) {
956 int this_minor = sunsab_reg.minor + i;
958 if ((this_minor - 64) == (serial_console - 1))
959 break;
961 if (i == num_channels)
962 return;
964 sunsab_console.index = i;
965 register_console(&sunsab_console);
967 #else
968 #define SUNSAB_CONSOLE (NULL)
969 #define sunsab_console_init() do { } while (0)
970 #endif
972 static void __init for_each_sab_edev(void (*callback)(struct linux_ebus_device *, void *), void *arg)
974 struct linux_ebus *ebus;
975 struct linux_ebus_device *edev = NULL;
977 for_each_ebus(ebus) {
978 for_each_ebusdev(edev, ebus) {
979 if (!strcmp(edev->prom_name, "se")) {
980 callback(edev, arg);
981 continue;
982 } else if (!strcmp(edev->prom_name, "serial")) {
983 char compat[32];
984 int clen;
986 /* On RIO this can be an SE, check it. We could
987 * just check ebus->is_rio, but this is more portable.
989 clen = prom_getproperty(edev->prom_node, "compatible",
990 compat, sizeof(compat));
991 if (clen > 0) {
992 if (strncmp(compat, "sab82532", 8) == 0) {
993 callback(edev, arg);
994 continue;
1002 static void __init sab_count_callback(struct linux_ebus_device *edev, void *arg)
1004 int *count_p = arg;
1006 (*count_p)++;
1009 static void __init sab_attach_callback(struct linux_ebus_device *edev, void *arg)
1011 int *instance_p = arg;
1012 struct uart_sunsab_port *up;
1013 unsigned long regs, offset;
1014 int i;
1016 /* Note: ports are located in reverse order */
1017 regs = edev->resource[0].start;
1018 offset = sizeof(union sab82532_async_regs);
1019 for (i = 0; i < 2; i++) {
1020 up = &sunsab_ports[(*instance_p * 2) + 1 - i];
1022 memset(up, 0, sizeof(*up));
1023 up->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
1024 up->port.irq = edev->irqs[0];
1025 up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
1026 up->port.mapbase = (unsigned long)up->regs;
1027 up->port.iotype = SERIAL_IO_MEM;
1029 writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
1031 offset -= sizeof(union sab82532_async_regs);
1034 (*instance_p)++;
1037 static int __init probe_for_sabs(void)
1039 int this_sab = 0;
1041 /* Find device instances. */
1042 for_each_sab_edev(&sab_count_callback, &this_sab);
1043 if (!this_sab)
1044 return -ENODEV;
1046 /* Allocate tables. */
1047 sunsab_ports = kmalloc(sizeof(struct uart_sunsab_port) * this_sab * 2,
1048 GFP_KERNEL);
1049 if (!sunsab_ports)
1050 return -ENOMEM;
1052 num_channels = this_sab * 2;
1054 this_sab = 0;
1055 for_each_sab_edev(&sab_attach_callback, &this_sab);
1056 return 0;
1059 static void __init sunsab_init_hw(void)
1061 int i;
1063 for (i = 0; i < num_channels; i++) {
1064 struct uart_sunsab_port *up = &sunsab_ports[i];
1066 up->port.line = i;
1067 up->port.ops = &sunsab_pops;
1068 up->port.type = PORT_SUNSAB;
1069 up->port.uartclk = SAB_BASE_BAUD;
1071 up->type = readb(&up->regs->r.vstr) & 0x0f;
1072 writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
1073 writeb(0xff, &up->regs->w.pim);
1074 if (up->port.line == 0) {
1075 up->pvr_dsr_bit = (1 << 0);
1076 up->pvr_dtr_bit = (1 << 1);
1077 } else {
1078 up->pvr_dsr_bit = (1 << 3);
1079 up->pvr_dtr_bit = (1 << 2);
1081 writeb((1 << 1) | (1 << 2) | (1 << 4), &up->regs->w.pvr);
1082 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
1083 &up->regs->rw.mode);
1084 writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
1085 &up->regs->rw.mode);
1087 up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
1088 up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1090 if (!(up->port.line & 0x01)) {
1091 if (request_irq(up->port.irq, sunsab_interrupt,
1092 SA_SHIRQ, "serial(sab82532)", up)) {
1093 printk("sunsab%d: can't get IRQ %x\n",
1094 i, up->port.irq);
1095 continue;
1101 static int __init sunsab_init(void)
1103 int ret = probe_for_sabs();
1104 int i;
1106 if (ret < 0)
1107 return ret;
1109 sunsab_init_hw();
1111 sunsab_reg.minor = sunserial_current_minor;
1112 sunsab_reg.nr = num_channels;
1113 sunsab_reg.cons = SUNSAB_CONSOLE;
1115 ret = uart_register_driver(&sunsab_reg);
1116 if (ret < 0) {
1117 int i;
1119 for (i = 0; i < num_channels; i++) {
1120 struct uart_sunsab_port *up = &sunsab_ports[i];
1122 if (!(up->port.line & 0x01))
1123 free_irq(up->port.irq, up);
1124 iounmap(up->regs);
1126 kfree(sunsab_ports);
1127 sunsab_ports = NULL;
1129 return ret;
1132 sunserial_current_minor += num_channels;
1134 sunsab_console_init();
1136 for (i = 0; i < num_channels; i++) {
1137 struct uart_sunsab_port *up = &sunsab_ports[i];
1139 uart_add_one_port(&sunsab_reg, &up->port);
1142 return 0;
1145 static void __exit sunsab_exit(void)
1147 int i;
1149 for (i = 0; i < num_channels; i++) {
1150 struct uart_sunsab_port *up = &sunsab_ports[i];
1152 uart_remove_one_port(&sunsab_reg, &up->port);
1154 if (!(up->port.line & 0x01))
1155 free_irq(up->port.irq, up);
1156 iounmap(up->regs);
1159 sunserial_current_minor -= num_channels;
1160 uart_unregister_driver(&sunsab_reg);
1162 kfree(sunsab_ports);
1163 sunsab_ports = NULL;
1166 module_init(sunsab_init);
1167 module_exit(sunsab_exit);
1169 MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
1170 MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
1171 MODULE_LICENSE("GPL");