dz.c: Use a helper to cast from "struct uart_port *"
[safe/jmp/linux-2.6] / drivers / serial / dz.c
1 /*
2  * dz.c: Serial port driver for DECstations equipped
3  *       with the DZ chipset.
4  *
5  * Copyright (C) 1998 Olivier A. D. Lebaillif
6  *
7  * Email: olivier.lebaillif@ifrsys.com
8  *
9  * Copyright (C) 2004, 2006, 2007  Maciej W. Rozycki
10  *
11  * [31-AUG-98] triemer
12  * Changed IRQ to use Harald's dec internals interrupts.h
13  * removed base_addr code - moving address assignment to setup.c
14  * Changed name of dz_init to rs_init to be consistent with tc code
15  * [13-NOV-98] triemer fixed code to receive characters
16  *    after patches by harald to irq code.
17  * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
18  *            field from "current" - somewhere between 2.1.121 and 2.1.131
19  Qua Jun 27 15:02:26 BRT 2001
20  * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
21  *
22  * Parts (C) 1999 David Airlie, airlied@linux.ie
23  * [07-SEP-99] Bugfixes
24  *
25  * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
26  * Converted to new serial core
27  */
28
29 #undef DEBUG_DZ
30
31 #if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32 #define SUPPORT_SYSRQ
33 #endif
34
35 #include <linux/bitops.h>
36 #include <linux/compiler.h>
37 #include <linux/console.h>
38 #include <linux/delay.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/module.h>
45 #include <linux/serial.h>
46 #include <linux/serial_core.h>
47 #include <linux/sysrq.h>
48 #include <linux/tty.h>
49
50 #include <asm/bootinfo.h>
51 #include <asm/system.h>
52
53 #include <asm/dec/interrupts.h>
54 #include <asm/dec/kn01.h>
55 #include <asm/dec/kn02.h>
56 #include <asm/dec/machtype.h>
57 #include <asm/dec/prom.h>
58
59 #include "dz.h"
60
61 static char *dz_name = "DECstation DZ serial driver version ";
62 static char *dz_version = "1.03";
63
64 struct dz_port {
65         struct uart_port        port;
66         unsigned int            cflag;
67 };
68
69 static struct dz_port dz_ports[DZ_NB_PORT];
70
71 static inline struct dz_port *to_dport(struct uart_port *uport)
72 {
73         return container_of(uport, struct dz_port, port);
74 }
75
76 /*
77  * ------------------------------------------------------------
78  * dz_in () and dz_out ()
79  *
80  * These routines are used to access the registers of the DZ
81  * chip, hiding relocation differences between implementation.
82  * ------------------------------------------------------------
83  */
84
85 static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
86 {
87         volatile unsigned short *addr =
88                 (volatile unsigned short *) (dport->port.membase + offset);
89
90         return *addr;
91 }
92
93 static inline void dz_out(struct dz_port *dport, unsigned offset,
94                           unsigned short value)
95 {
96         volatile unsigned short *addr =
97                 (volatile unsigned short *) (dport->port.membase + offset);
98
99         *addr = value;
100 }
101
102 /*
103  * ------------------------------------------------------------
104  * rs_stop () and rs_start ()
105  *
106  * These routines are called before setting or resetting
107  * tty->stopped. They enable or disable transmitter interrupts,
108  * as necessary.
109  * ------------------------------------------------------------
110  */
111
112 static void dz_stop_tx(struct uart_port *uport)
113 {
114         struct dz_port *dport = to_dport(uport);
115         unsigned short tmp, mask = 1 << dport->port.line;
116
117         tmp = dz_in(dport, DZ_TCR);     /* read the TX flag */
118         tmp &= ~mask;                   /* clear the TX flag */
119         dz_out(dport, DZ_TCR, tmp);
120 }
121
122 static void dz_start_tx(struct uart_port *uport)
123 {
124         struct dz_port *dport = to_dport(uport);
125         unsigned short tmp, mask = 1 << dport->port.line;
126
127         tmp = dz_in(dport, DZ_TCR);     /* read the TX flag */
128         tmp |= mask;                    /* set the TX flag */
129         dz_out(dport, DZ_TCR, tmp);
130 }
131
132 static void dz_stop_rx(struct uart_port *uport)
133 {
134         struct dz_port *dport = to_dport(uport);
135
136         dport->cflag &= ~DZ_RXENAB;
137         dz_out(dport, DZ_LPR, dport->cflag);
138 }
139
140 static void dz_enable_ms(struct uart_port *port)
141 {
142         /* nothing to do */
143 }
144
145 /*
146  * ------------------------------------------------------------
147  *
148  * Here start the interrupt handling routines.  All of the following
149  * subroutines are declared as inline and are folded into
150  * dz_interrupt.  They were separated out for readability's sake.
151  *
152  * Note: dz_interrupt() is a "fast" interrupt, which means that it
153  * runs with interrupts turned off.  People who may want to modify
154  * dz_interrupt() should try to keep the interrupt handler as fast as
155  * possible.  After you are done making modifications, it is not a bad
156  * idea to do:
157  *
158  *      make drivers/serial/dz.s
159  *
160  * and look at the resulting assemble code in dz.s.
161  *
162  * ------------------------------------------------------------
163  */
164
165 /*
166  * ------------------------------------------------------------
167  * receive_char ()
168  *
169  * This routine deals with inputs from any lines.
170  * ------------------------------------------------------------
171  */
172 static inline void dz_receive_chars(struct dz_port *dport_in)
173 {
174         struct uart_port *uport;
175         struct dz_port *dport;
176         struct tty_struct *tty = NULL;
177         struct uart_icount *icount;
178         int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
179         unsigned short status;
180         unsigned char ch, flag;
181         int i;
182
183         while ((status = dz_in(dport_in, DZ_RBUF)) & DZ_DVAL) {
184                 dport = &dz_ports[LINE(status)];
185                 uport = &dport->port;
186                 tty = uport->info->tty;         /* point to the proper dev */
187
188                 ch = UCHAR(status);             /* grab the char */
189                 flag = TTY_NORMAL;
190
191                 icount = &uport->icount;
192                 icount->rx++;
193
194                 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
195
196                         /*
197                          * There is no separate BREAK status bit, so treat
198                          * null characters with framing errors as BREAKs;
199                          * normally, otherwise.  For this move the Framing
200                          * Error bit to a simulated BREAK bit.
201                          */
202                         if (!ch) {
203                                 status |= (status & DZ_FERR) >>
204                                           (ffs(DZ_FERR) - ffs(DZ_BREAK));
205                                 status &= ~DZ_FERR;
206                         }
207
208                         /* Handle SysRq/SAK & keep track of the statistics. */
209                         if (status & DZ_BREAK) {
210                                 icount->brk++;
211                                 if (uart_handle_break(uport))
212                                         continue;
213                         } else if (status & DZ_FERR)
214                                 icount->frame++;
215                         else if (status & DZ_PERR)
216                                 icount->parity++;
217                         if (status & DZ_OERR)
218                                 icount->overrun++;
219
220                         status &= uport->read_status_mask;
221                         if (status & DZ_BREAK)
222                                 flag = TTY_BREAK;
223                         else if (status & DZ_FERR)
224                                 flag = TTY_FRAME;
225                         else if (status & DZ_PERR)
226                                 flag = TTY_PARITY;
227
228                 }
229
230                 if (uart_handle_sysrq_char(uport, ch))
231                         continue;
232
233                 uart_insert_char(uport, status, DZ_OERR, ch, flag);
234                 lines_rx[LINE(status)] = 1;
235         }
236         for (i = 0; i < DZ_NB_PORT; i++)
237                 if (lines_rx[i])
238                         tty_flip_buffer_push(dz_ports[i].port.info->tty);
239 }
240
241 /*
242  * ------------------------------------------------------------
243  * transmit_char ()
244  *
245  * This routine deals with outputs to any lines.
246  * ------------------------------------------------------------
247  */
248 static inline void dz_transmit_chars(struct dz_port *dport_in)
249 {
250         struct dz_port *dport;
251         struct circ_buf *xmit;
252         unsigned short status;
253         unsigned char tmp;
254
255         status = dz_in(dport_in, DZ_CSR);
256         dport = &dz_ports[LINE(status)];
257         xmit = &dport->port.info->xmit;
258
259         if (dport->port.x_char) {               /* XON/XOFF chars */
260                 dz_out(dport, DZ_TDR, dport->port.x_char);
261                 dport->port.icount.tx++;
262                 dport->port.x_char = 0;
263                 return;
264         }
265         /* If nothing to do or stopped or hardware stopped. */
266         if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
267                 spin_lock(&dport->port.lock);
268                 dz_stop_tx(&dport->port);
269                 spin_unlock(&dport->port.lock);
270                 return;
271         }
272
273         /*
274          * If something to do... (remember the dz has no output fifo,
275          * so we go one char at a time) :-<
276          */
277         tmp = xmit->buf[xmit->tail];
278         xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
279         dz_out(dport, DZ_TDR, tmp);
280         dport->port.icount.tx++;
281
282         if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
283                 uart_write_wakeup(&dport->port);
284
285         /* Are we are done. */
286         if (uart_circ_empty(xmit)) {
287                 spin_lock(&dport->port.lock);
288                 dz_stop_tx(&dport->port);
289                 spin_unlock(&dport->port.lock);
290         }
291 }
292
293 /*
294  * ------------------------------------------------------------
295  * check_modem_status()
296  *
297  * DS 3100 & 5100: Only valid for the MODEM line, duh!
298  * DS 5000/200: Valid for the MODEM and PRINTER line.
299  * ------------------------------------------------------------
300  */
301 static inline void check_modem_status(struct dz_port *dport)
302 {
303         /*
304          * FIXME:
305          * 1. No status change interrupt; use a timer.
306          * 2. Handle the 3100/5000 as appropriate. --macro
307          */
308         unsigned short status;
309
310         /* If not the modem line just return.  */
311         if (dport->port.line != DZ_MODEM)
312                 return;
313
314         status = dz_in(dport, DZ_MSR);
315
316         /* it's easy, since DSR2 is the only bit in the register */
317         if (status)
318                 dport->port.icount.dsr++;
319 }
320
321 /*
322  * ------------------------------------------------------------
323  * dz_interrupt ()
324  *
325  * this is the main interrupt routine for the DZ chip.
326  * It deals with the multiple ports.
327  * ------------------------------------------------------------
328  */
329 static irqreturn_t dz_interrupt(int irq, void *dev)
330 {
331         struct dz_port *dport = dev;
332         unsigned short status;
333
334         /* get the reason why we just got an irq */
335         status = dz_in(dport, DZ_CSR);
336
337         if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
338                 dz_receive_chars(dport);
339
340         if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
341                 dz_transmit_chars(dport);
342
343         return IRQ_HANDLED;
344 }
345
346 /*
347  * -------------------------------------------------------------------
348  * Here ends the DZ interrupt routines.
349  * -------------------------------------------------------------------
350  */
351
352 static unsigned int dz_get_mctrl(struct uart_port *uport)
353 {
354         /*
355          * FIXME: Handle the 3100/5000 as appropriate. --macro
356          */
357         struct dz_port *dport = to_dport(uport);
358         unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
359
360         if (dport->port.line == DZ_MODEM) {
361                 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
362                         mctrl &= ~TIOCM_DSR;
363         }
364
365         return mctrl;
366 }
367
368 static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
369 {
370         /*
371          * FIXME: Handle the 3100/5000 as appropriate. --macro
372          */
373         struct dz_port *dport = to_dport(uport);
374         unsigned short tmp;
375
376         if (dport->port.line == DZ_MODEM) {
377                 tmp = dz_in(dport, DZ_TCR);
378                 if (mctrl & TIOCM_DTR)
379                         tmp &= ~DZ_MODEM_DTR;
380                 else
381                         tmp |= DZ_MODEM_DTR;
382                 dz_out(dport, DZ_TCR, tmp);
383         }
384 }
385
386 /*
387  * -------------------------------------------------------------------
388  * startup ()
389  *
390  * various initialization tasks
391  * -------------------------------------------------------------------
392  */
393 static int dz_startup(struct uart_port *uport)
394 {
395         struct dz_port *dport = to_dport(uport);
396         unsigned long flags;
397         unsigned short tmp;
398
399         spin_lock_irqsave(&dport->port.lock, flags);
400
401         /* enable the interrupt and the scanning */
402         tmp = dz_in(dport, DZ_CSR);
403         tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
404         dz_out(dport, DZ_CSR, tmp);
405
406         spin_unlock_irqrestore(&dport->port.lock, flags);
407
408         return 0;
409 }
410
411 /*
412  * -------------------------------------------------------------------
413  * shutdown ()
414  *
415  * This routine will shutdown a serial port; interrupts are disabled, and
416  * DTR is dropped if the hangup on close termio flag is on.
417  * -------------------------------------------------------------------
418  */
419 static void dz_shutdown(struct uart_port *uport)
420 {
421         struct dz_port *dport = to_dport(uport);
422         unsigned long flags;
423
424         spin_lock_irqsave(&dport->port.lock, flags);
425         dz_stop_tx(&dport->port);
426         spin_unlock_irqrestore(&dport->port.lock, flags);
427 }
428
429 /*
430  * -------------------------------------------------------------------
431  * dz_tx_empty() -- get the transmitter empty status
432  *
433  * Purpose: Let user call ioctl() to get info when the UART physically
434  *          is emptied.  On bus types like RS485, the transmitter must
435  *          release the bus after transmitting. This must be done when
436  *          the transmit shift register is empty, not be done when the
437  *          transmit holding register is empty.  This functionality
438  *          allows an RS485 driver to be written in user space.
439  * -------------------------------------------------------------------
440  */
441 static unsigned int dz_tx_empty(struct uart_port *uport)
442 {
443         struct dz_port *dport = to_dport(uport);
444         unsigned short tmp, mask = 1 << dport->port.line;
445
446         tmp = dz_in(dport, DZ_TCR);
447         tmp &= mask;
448
449         return tmp ? 0 : TIOCSER_TEMT;
450 }
451
452 static void dz_break_ctl(struct uart_port *uport, int break_state)
453 {
454         /*
455          * FIXME: Can't access BREAK bits in TDR easily;
456          * reuse the code for polled TX. --macro
457          */
458         struct dz_port *dport = to_dport(uport);
459         unsigned long flags;
460         unsigned short tmp, mask = 1 << dport->port.line;
461
462         spin_lock_irqsave(&uport->lock, flags);
463         tmp = dz_in(dport, DZ_TCR);
464         if (break_state)
465                 tmp |= mask;
466         else
467                 tmp &= ~mask;
468         dz_out(dport, DZ_TCR, tmp);
469         spin_unlock_irqrestore(&uport->lock, flags);
470 }
471
472 static int dz_encode_baud_rate(unsigned int baud)
473 {
474         switch (baud) {
475         case 50:
476                 return DZ_B50;
477         case 75:
478                 return DZ_B75;
479         case 110:
480                 return DZ_B110;
481         case 134:
482                 return DZ_B134;
483         case 150:
484                 return DZ_B150;
485         case 300:
486                 return DZ_B300;
487         case 600:
488                 return DZ_B600;
489         case 1200:
490                 return DZ_B1200;
491         case 1800:
492                 return DZ_B1800;
493         case 2000:
494                 return DZ_B2000;
495         case 2400:
496                 return DZ_B2400;
497         case 3600:
498                 return DZ_B3600;
499         case 4800:
500                 return DZ_B4800;
501         case 7200:
502                 return DZ_B7200;
503         case 9600:
504                 return DZ_B9600;
505         default:
506                 return -1;
507         }
508 }
509
510 static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
511                            struct ktermios *old_termios)
512 {
513         struct dz_port *dport = to_dport(uport);
514         unsigned long flags;
515         unsigned int cflag, baud;
516         int bflag;
517
518         cflag = dport->port.line;
519
520         switch (termios->c_cflag & CSIZE) {
521         case CS5:
522                 cflag |= DZ_CS5;
523                 break;
524         case CS6:
525                 cflag |= DZ_CS6;
526                 break;
527         case CS7:
528                 cflag |= DZ_CS7;
529                 break;
530         case CS8:
531         default:
532                 cflag |= DZ_CS8;
533         }
534
535         if (termios->c_cflag & CSTOPB)
536                 cflag |= DZ_CSTOPB;
537         if (termios->c_cflag & PARENB)
538                 cflag |= DZ_PARENB;
539         if (termios->c_cflag & PARODD)
540                 cflag |= DZ_PARODD;
541
542         baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
543         bflag = dz_encode_baud_rate(baud);
544         if (bflag < 0)  {                       /* Try to keep unchanged.  */
545                 baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
546                 bflag = dz_encode_baud_rate(baud);
547                 if (bflag < 0)  {               /* Resort to 9600.  */
548                         baud = 9600;
549                         bflag = DZ_B9600;
550                 }
551                 tty_termios_encode_baud_rate(termios, baud, baud);
552         }
553         cflag |= bflag;
554
555         if (termios->c_cflag & CREAD)
556                 cflag |= DZ_RXENAB;
557
558         spin_lock_irqsave(&dport->port.lock, flags);
559
560         uart_update_timeout(uport, termios->c_cflag, baud);
561
562         dz_out(dport, DZ_LPR, cflag);
563         dport->cflag = cflag;
564
565         /* setup accept flag */
566         dport->port.read_status_mask = DZ_OERR;
567         if (termios->c_iflag & INPCK)
568                 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
569         if (termios->c_iflag & (BRKINT | PARMRK))
570                 dport->port.read_status_mask |= DZ_BREAK;
571
572         /* characters to ignore */
573         uport->ignore_status_mask = 0;
574         if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
575                 dport->port.ignore_status_mask |= DZ_OERR;
576         if (termios->c_iflag & IGNPAR)
577                 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
578         if (termios->c_iflag & IGNBRK)
579                 dport->port.ignore_status_mask |= DZ_BREAK;
580
581         spin_unlock_irqrestore(&dport->port.lock, flags);
582 }
583
584 static const char *dz_type(struct uart_port *port)
585 {
586         return "DZ";
587 }
588
589 static void dz_release_port(struct uart_port *port)
590 {
591         /* nothing to do */
592 }
593
594 static int dz_request_port(struct uart_port *port)
595 {
596         return 0;
597 }
598
599 static void dz_config_port(struct uart_port *port, int flags)
600 {
601         if (flags & UART_CONFIG_TYPE)
602                 port->type = PORT_DZ;
603 }
604
605 /*
606  * verify the new serial_struct (for TIOCSSERIAL).
607  */
608 static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
609 {
610         int ret = 0;
611         if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
612                 ret = -EINVAL;
613         if (ser->irq != port->irq)
614                 ret = -EINVAL;
615         return ret;
616 }
617
618 static struct uart_ops dz_ops = {
619         .tx_empty       = dz_tx_empty,
620         .get_mctrl      = dz_get_mctrl,
621         .set_mctrl      = dz_set_mctrl,
622         .stop_tx        = dz_stop_tx,
623         .start_tx       = dz_start_tx,
624         .stop_rx        = dz_stop_rx,
625         .enable_ms      = dz_enable_ms,
626         .break_ctl      = dz_break_ctl,
627         .startup        = dz_startup,
628         .shutdown       = dz_shutdown,
629         .set_termios    = dz_set_termios,
630         .type           = dz_type,
631         .release_port   = dz_release_port,
632         .request_port   = dz_request_port,
633         .config_port    = dz_config_port,
634         .verify_port    = dz_verify_port,
635 };
636
637 static void __init dz_init_ports(void)
638 {
639         static int first = 1;
640         struct dz_port *dport;
641         unsigned long base;
642         int i;
643
644         if (!first)
645                 return;
646         first = 0;
647
648         if (mips_machtype == MACH_DS23100 ||
649             mips_machtype == MACH_DS5100)
650                 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
651         else
652                 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
653
654         for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
655                 spin_lock_init(&dport->port.lock);
656                 dport->port.membase     = (char *) base;
657                 dport->port.iotype      = UPIO_MEM;
658                 dport->port.irq         = dec_interrupt[DEC_IRQ_DZ11];
659                 dport->port.line        = i;
660                 dport->port.fifosize    = 1;
661                 dport->port.ops         = &dz_ops;
662                 dport->port.flags       = UPF_BOOT_AUTOCONF;
663         }
664 }
665
666 static void dz_reset(struct dz_port *dport)
667 {
668         dz_out(dport, DZ_CSR, DZ_CLR);
669         while (dz_in(dport, DZ_CSR) & DZ_CLR);
670         iob();
671
672         /* enable scanning */
673         dz_out(dport, DZ_CSR, DZ_MSE);
674 }
675
676 #ifdef CONFIG_SERIAL_DZ_CONSOLE
677 /*
678  * -------------------------------------------------------------------
679  * dz_console_putchar() -- transmit a character
680  *
681  * Polled transmission.  This is tricky.  We need to mask transmit
682  * interrupts so that they do not interfere, enable the transmitter
683  * for the line requested and then wait till the transmit scanner
684  * requests data for this line.  But it may request data for another
685  * line first, in which case we have to disable its transmitter and
686  * repeat waiting till our line pops up.  Only then the character may
687  * be transmitted.  Finally, the state of the transmitter mask is
688  * restored.  Welcome to the world of PDP-11!
689  * -------------------------------------------------------------------
690  */
691 static void dz_console_putchar(struct uart_port *uport, int ch)
692 {
693         struct dz_port *dport = to_dport(uport);
694         unsigned long flags;
695         unsigned short csr, tcr, trdy, mask;
696         int loops = 10000;
697
698         spin_lock_irqsave(&dport->port.lock, flags);
699         csr = dz_in(dport, DZ_CSR);
700         dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
701         tcr = dz_in(dport, DZ_TCR);
702         tcr |= 1 << dport->port.line;
703         mask = tcr;
704         dz_out(dport, DZ_TCR, mask);
705         iob();
706         spin_unlock_irqrestore(&dport->port.lock, flags);
707
708         do {
709                 trdy = dz_in(dport, DZ_CSR);
710                 if (!(trdy & DZ_TRDY))
711                         continue;
712                 trdy = (trdy & DZ_TLINE) >> 8;
713                 if (trdy == dport->port.line)
714                         break;
715                 mask &= ~(1 << trdy);
716                 dz_out(dport, DZ_TCR, mask);
717                 iob();
718                 udelay(2);
719         } while (loops--);
720
721         if (loops)                              /* Cannot send otherwise. */
722                 dz_out(dport, DZ_TDR, ch);
723
724         dz_out(dport, DZ_TCR, tcr);
725         dz_out(dport, DZ_CSR, csr);
726 }
727
728 /*
729  * -------------------------------------------------------------------
730  * dz_console_print ()
731  *
732  * dz_console_print is registered for printk.
733  * The console must be locked when we get here.
734  * -------------------------------------------------------------------
735  */
736 static void dz_console_print(struct console *co,
737                              const char *str,
738                              unsigned int count)
739 {
740         struct dz_port *dport = &dz_ports[co->index];
741 #ifdef DEBUG_DZ
742         prom_printf((char *) str);
743 #endif
744         uart_console_write(&dport->port, str, count, dz_console_putchar);
745 }
746
747 static int __init dz_console_setup(struct console *co, char *options)
748 {
749         struct dz_port *dport = &dz_ports[co->index];
750         int baud = 9600;
751         int bits = 8;
752         int parity = 'n';
753         int flow = 'n';
754
755         if (options)
756                 uart_parse_options(options, &baud, &parity, &bits, &flow);
757
758         dz_reset(dport);
759
760         return uart_set_options(&dport->port, co, baud, parity, bits, flow);
761 }
762
763 static struct uart_driver dz_reg;
764 static struct console dz_console = {
765         .name   = "ttyS",
766         .write  = dz_console_print,
767         .device = uart_console_device,
768         .setup  = dz_console_setup,
769         .flags  = CON_PRINTBUFFER,
770         .index  = -1,
771         .data   = &dz_reg,
772 };
773
774 static int __init dz_serial_console_init(void)
775 {
776         if (!IOASIC) {
777                 dz_init_ports();
778                 register_console(&dz_console);
779                 return 0;
780         } else
781                 return -ENXIO;
782 }
783
784 console_initcall(dz_serial_console_init);
785
786 #define SERIAL_DZ_CONSOLE       &dz_console
787 #else
788 #define SERIAL_DZ_CONSOLE       NULL
789 #endif /* CONFIG_SERIAL_DZ_CONSOLE */
790
791 static struct uart_driver dz_reg = {
792         .owner                  = THIS_MODULE,
793         .driver_name            = "serial",
794         .dev_name               = "ttyS",
795         .major                  = TTY_MAJOR,
796         .minor                  = 64,
797         .nr                     = DZ_NB_PORT,
798         .cons                   = SERIAL_DZ_CONSOLE,
799 };
800
801 static int __init dz_init(void)
802 {
803         int ret, i;
804
805         if (IOASIC)
806                 return -ENXIO;
807
808         printk("%s%s\n", dz_name, dz_version);
809
810         dz_init_ports();
811
812 #ifndef CONFIG_SERIAL_DZ_CONSOLE
813         /* reset the chip */
814         dz_reset(&dz_ports[0]);
815 #endif
816
817         ret = uart_register_driver(&dz_reg);
818         if (ret != 0)
819                 goto out;
820
821         ret = request_irq(dz_ports[0].port.irq, dz_interrupt, IRQF_DISABLED,
822                           "DZ", &dz_ports[0]);
823         if (ret != 0) {
824                 printk(KERN_ERR "dz: Cannot get IRQ %d!\n",
825                        dz_ports[0].port.irq);
826                 goto out_unregister;
827         }
828
829         for (i = 0; i < DZ_NB_PORT; i++)
830                 uart_add_one_port(&dz_reg, &dz_ports[i].port);
831
832         return ret;
833
834 out_unregister:
835         uart_unregister_driver(&dz_reg);
836
837 out:
838         return ret;
839 }
840
841 module_init(dz_init);
842
843 MODULE_DESCRIPTION("DECstation DZ serial driver");
844 MODULE_LICENSE("GPL");