blob: d31721f2744d69db038b69b7c0f2a9e143cc362d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Maciej W. Rozycki93995752006-12-06 20:38:59 -08002 * dz.c: Serial port driver for DECstations equipped
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * with the DZ chipset.
4 *
Ralf Baechlefd8c5972005-11-12 21:59:59 +00005 * Copyright (C) 1998 Olivier A. D. Lebaillif
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Email: olivier.lebaillif@ifrsys.com
8 *
Maciej W. Rozycki93995752006-12-06 20:38:59 -08009 * Copyright (C) 2004, 2006 Maciej W. Rozycki
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * [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
Ralf Baechlefd8c5972005-11-12 21:59:59 +000016 * after patches by harald to irq code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * [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
Ralf Baechlefd8c5972005-11-12 21:59:59 +000021 *
22 * Parts (C) 1999 David Airlie, airlied@linux.ie
23 * [07-SEP-99] Bugfixes
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
26 * Converted to new serial core
27 */
28
29#undef DEBUG_DZ
30
Maciej W. Rozycki93995752006-12-06 20:38:59 -080031#if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32#define SUPPORT_SYSRQ
33#endif
34
35#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/interrupt.h>
38#include <linux/init.h>
39#include <linux/console.h>
Maciej W. Rozycki93995752006-12-06 20:38:59 -080040#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
45
46#include <asm/bootinfo.h>
47#include <asm/dec/interrupts.h>
48#include <asm/dec/kn01.h>
49#include <asm/dec/kn02.h>
50#include <asm/dec/machtype.h>
51#include <asm/dec/prom.h>
52#include <asm/irq.h>
53#include <asm/system.h>
54#include <asm/uaccess.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "dz.h"
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static char *dz_name = "DECstation DZ serial driver version ";
Maciej W. Rozycki93995752006-12-06 20:38:59 -080059static char *dz_version = "1.03";
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61struct dz_port {
62 struct uart_port port;
63 unsigned int cflag;
64};
65
66static struct dz_port dz_ports[DZ_NB_PORT];
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * ------------------------------------------------------------
70 * dz_in () and dz_out ()
71 *
Ralf Baechlefd8c5972005-11-12 21:59:59 +000072 * These routines are used to access the registers of the DZ
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * chip, hiding relocation differences between implementation.
74 * ------------------------------------------------------------
75 */
76
77static inline unsigned short dz_in(struct dz_port *dport, unsigned offset)
78{
79 volatile unsigned short *addr =
80 (volatile unsigned short *) (dport->port.membase + offset);
Maciej W. Rozycki93995752006-12-06 20:38:59 -080081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return *addr;
83}
84
85static inline void dz_out(struct dz_port *dport, unsigned offset,
86 unsigned short value)
87{
88 volatile unsigned short *addr =
89 (volatile unsigned short *) (dport->port.membase + offset);
Maciej W. Rozycki93995752006-12-06 20:38:59 -080090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *addr = value;
92}
93
94/*
95 * ------------------------------------------------------------
96 * rs_stop () and rs_start ()
97 *
Ralf Baechlefd8c5972005-11-12 21:59:59 +000098 * These routines are called before setting or resetting
99 * tty->stopped. They enable or disable transmitter interrupts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * as necessary.
101 * ------------------------------------------------------------
102 */
103
Russell Kingb129a8c2005-08-31 10:12:14 +0100104static void dz_stop_tx(struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct dz_port *dport = (struct dz_port *)uport;
107 unsigned short tmp, mask = 1 << dport->port.line;
108 unsigned long flags;
109
110 spin_lock_irqsave(&dport->port.lock, flags);
111 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
112 tmp &= ~mask; /* clear the TX flag */
113 dz_out(dport, DZ_TCR, tmp);
114 spin_unlock_irqrestore(&dport->port.lock, flags);
115}
116
Russell Kingb129a8c2005-08-31 10:12:14 +0100117static void dz_start_tx(struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct dz_port *dport = (struct dz_port *)uport;
120 unsigned short tmp, mask = 1 << dport->port.line;
121 unsigned long flags;
122
123 spin_lock_irqsave(&dport->port.lock, flags);
124 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
125 tmp |= mask; /* set the TX flag */
126 dz_out(dport, DZ_TCR, tmp);
127 spin_unlock_irqrestore(&dport->port.lock, flags);
128}
129
130static void dz_stop_rx(struct uart_port *uport)
131{
132 struct dz_port *dport = (struct dz_port *)uport;
133 unsigned long flags;
134
135 spin_lock_irqsave(&dport->port.lock, flags);
136 dport->cflag &= ~DZ_CREAD;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800137 dz_out(dport, DZ_LPR, dport->cflag | dport->port.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spin_unlock_irqrestore(&dport->port.lock, flags);
139}
140
141static void dz_enable_ms(struct uart_port *port)
142{
143 /* nothing to do */
144}
145
146/*
147 * ------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800149 * Here start the interrupt handling routines. All of the following
150 * subroutines are declared as inline and are folded into
151 * dz_interrupt. They were separated out for readability's sake.
152 *
153 * Note: dz_interrupt() is a "fast" interrupt, which means that it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * runs with interrupts turned off. People who may want to modify
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800155 * dz_interrupt() should try to keep the interrupt handler as fast as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * possible. After you are done making modifications, it is not a bad
157 * idea to do:
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * make drivers/serial/dz.s
160 *
161 * and look at the resulting assemble code in dz.s.
162 *
163 * ------------------------------------------------------------
164 */
165
166/*
167 * ------------------------------------------------------------
168 * receive_char ()
169 *
170 * This routine deals with inputs from any lines.
171 * ------------------------------------------------------------
172 */
Maciej W. Rozyckide320192007-03-05 00:30:26 -0800173static inline void dz_receive_chars(struct dz_port *dport_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800175 struct dz_port *dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct tty_struct *tty = NULL;
177 struct uart_icount *icount;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800178 int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
179 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned char ch, flag;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800181 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800183 while ((status = dz_in(dport_in, DZ_RBUF)) & DZ_DVAL) {
184 dport = &dz_ports[LINE(status)];
185 tty = dport->port.info->tty; /* point to the proper dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800187 ch = UCHAR(status); /* grab the char */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 icount = &dport->port.icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 icount->rx++;
191
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800192 flag = TTY_NORMAL;
193 if (status & DZ_FERR) { /* frame error */
194 /*
195 * There is no separate BREAK status bit, so
196 * treat framing errors as BREAKs for Magic SysRq
197 * and SAK; normally, otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800199 if (uart_handle_break(&dport->port))
200 continue;
201 if (dport->port.flags & UPF_SAK)
202 flag = TTY_BREAK;
203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 flag = TTY_FRAME;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800205 } else if (status & DZ_OERR) /* overrun error */
206 flag = TTY_OVERRUN;
207 else if (status & DZ_PERR) /* parity error */
208 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800210 /* keep track of the statistics */
211 switch (flag) {
212 case TTY_FRAME:
213 icount->frame++;
214 break;
215 case TTY_PARITY:
216 icount->parity++;
217 break;
218 case TTY_OVERRUN:
219 icount->overrun++;
220 break;
221 case TTY_BREAK:
222 icount->brk++;
223 break;
224 default:
225 break;
226 }
227
Maciej W. Rozyckide320192007-03-05 00:30:26 -0800228 if (uart_handle_sysrq_char(&dport->port, ch))
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800229 continue;
230
231 if ((status & dport->port.ignore_status_mask) == 0) {
232 uart_insert_char(&dport->port,
233 status, DZ_OERR, ch, flag);
234 lines_rx[LINE(status)] = 1;
235 }
236 }
237 for (i = 0; i < DZ_NB_PORT; i++)
238 if (lines_rx[i])
239 tty_flip_buffer_push(dz_ports[i].port.info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/*
243 * ------------------------------------------------------------
244 * transmit_char ()
245 *
246 * This routine deals with outputs to any lines.
247 * ------------------------------------------------------------
248 */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800249static inline void dz_transmit_chars(struct dz_port *dport_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800251 struct dz_port *dport;
252 struct circ_buf *xmit;
253 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned char tmp;
255
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800256 status = dz_in(dport_in, DZ_CSR);
257 dport = &dz_ports[LINE(status)];
258 xmit = &dport->port.info->xmit;
259
260 if (dport->port.x_char) { /* XON/XOFF chars */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dz_out(dport, DZ_TDR, dport->port.x_char);
262 dport->port.icount.tx++;
263 dport->port.x_char = 0;
264 return;
265 }
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800266 /* If nothing to do or stopped or hardware stopped. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100268 dz_stop_tx(&dport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return;
270 }
271
272 /*
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800273 * If something to do... (remember the dz has no output fifo,
274 * so we go one char at a time) :-<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276 tmp = xmit->buf[xmit->tail];
277 xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
278 dz_out(dport, DZ_TDR, tmp);
279 dport->port.icount.tx++;
280
281 if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
282 uart_write_wakeup(&dport->port);
283
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800284 /* Are we are done. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100286 dz_stop_tx(&dport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * ------------------------------------------------------------
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800291 * check_modem_status()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 *
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800293 * DS 3100 & 5100: Only valid for the MODEM line, duh!
294 * DS 5000/200: Valid for the MODEM and PRINTER line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * ------------------------------------------------------------
296 */
297static inline void check_modem_status(struct dz_port *dport)
298{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800299 /*
300 * FIXME:
301 * 1. No status change interrupt; use a timer.
302 * 2. Handle the 3100/5000 as appropriate. --macro
303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned short status;
305
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800306 /* If not the modem line just return. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (dport->port.line != DZ_MODEM)
308 return;
309
310 status = dz_in(dport, DZ_MSR);
311
312 /* it's easy, since DSR2 is the only bit in the register */
313 if (status)
314 dport->port.icount.dsr++;
315}
316
317/*
318 * ------------------------------------------------------------
319 * dz_interrupt ()
320 *
321 * this is the main interrupt routine for the DZ chip.
322 * It deals with the multiple ports.
323 * ------------------------------------------------------------
324 */
David Howells7d12e782006-10-05 14:55:46 +0100325static irqreturn_t dz_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800327 struct dz_port *dport = (struct dz_port *)dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned short status;
329
330 /* get the reason why we just got an irq */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800331 status = dz_in(dport, DZ_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800333 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
Maciej W. Rozyckide320192007-03-05 00:30:26 -0800334 dz_receive_chars(dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800336 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dz_transmit_chars(dport);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return IRQ_HANDLED;
340}
341
342/*
343 * -------------------------------------------------------------------
344 * Here ends the DZ interrupt routines.
345 * -------------------------------------------------------------------
346 */
347
348static unsigned int dz_get_mctrl(struct uart_port *uport)
349{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800350 /*
351 * FIXME: Handle the 3100/5000 as appropriate. --macro
352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct dz_port *dport = (struct dz_port *)uport;
354 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
355
356 if (dport->port.line == DZ_MODEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
358 mctrl &= ~TIOCM_DSR;
359 }
360
361 return mctrl;
362}
363
364static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
365{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800366 /*
367 * FIXME: Handle the 3100/5000 as appropriate. --macro
368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct dz_port *dport = (struct dz_port *)uport;
370 unsigned short tmp;
371
372 if (dport->port.line == DZ_MODEM) {
373 tmp = dz_in(dport, DZ_TCR);
374 if (mctrl & TIOCM_DTR)
375 tmp &= ~DZ_MODEM_DTR;
376 else
377 tmp |= DZ_MODEM_DTR;
378 dz_out(dport, DZ_TCR, tmp);
379 }
380}
381
382/*
383 * -------------------------------------------------------------------
384 * startup ()
385 *
386 * various initialization tasks
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000387 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
389static int dz_startup(struct uart_port *uport)
390{
391 struct dz_port *dport = (struct dz_port *)uport;
392 unsigned long flags;
393 unsigned short tmp;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 spin_lock_irqsave(&dport->port.lock, flags);
396
397 /* enable the interrupt and the scanning */
398 tmp = dz_in(dport, DZ_CSR);
399 tmp |= DZ_RIE | DZ_TIE | DZ_MSE;
400 dz_out(dport, DZ_CSR, tmp);
401
402 spin_unlock_irqrestore(&dport->port.lock, flags);
403
404 return 0;
405}
406
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * -------------------------------------------------------------------
409 * shutdown ()
410 *
411 * This routine will shutdown a serial port; interrupts are disabled, and
412 * DTR is dropped if the hangup on close termio flag is on.
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000413 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
415static void dz_shutdown(struct uart_port *uport)
416{
Russell Kingb129a8c2005-08-31 10:12:14 +0100417 dz_stop_tx(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/*
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800421 * -------------------------------------------------------------------
422 * dz_tx_empty() -- get the transmitter empty status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
424 * Purpose: Let user call ioctl() to get info when the UART physically
425 * is emptied. On bus types like RS485, the transmitter must
426 * release the bus after transmitting. This must be done when
427 * the transmit shift register is empty, not be done when the
428 * transmit holding register is empty. This functionality
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000429 * allows an RS485 driver to be written in user space.
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800430 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
432static unsigned int dz_tx_empty(struct uart_port *uport)
433{
434 struct dz_port *dport = (struct dz_port *)uport;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800435 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800437 tmp = dz_in(dport, DZ_TCR);
438 tmp &= mask;
439
440 return tmp ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443static void dz_break_ctl(struct uart_port *uport, int break_state)
444{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800445 /*
446 * FIXME: Can't access BREAK bits in TDR easily;
447 * reuse the code for polled TX. --macro
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct dz_port *dport = (struct dz_port *)uport;
450 unsigned long flags;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800451 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 spin_lock_irqsave(&uport->lock, flags);
454 tmp = dz_in(dport, DZ_TCR);
455 if (break_state)
456 tmp |= mask;
457 else
458 tmp &= ~mask;
459 dz_out(dport, DZ_TCR, tmp);
460 spin_unlock_irqrestore(&uport->lock, flags);
461}
462
Alan Cox606d0992006-12-08 02:38:45 -0800463static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
464 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct dz_port *dport = (struct dz_port *)uport;
467 unsigned long flags;
468 unsigned int cflag, baud;
469
470 cflag = dport->port.line;
471
472 switch (termios->c_cflag & CSIZE) {
473 case CS5:
474 cflag |= DZ_CS5;
475 break;
476 case CS6:
477 cflag |= DZ_CS6;
478 break;
479 case CS7:
480 cflag |= DZ_CS7;
481 break;
482 case CS8:
483 default:
484 cflag |= DZ_CS8;
485 }
486
487 if (termios->c_cflag & CSTOPB)
488 cflag |= DZ_CSTOPB;
489 if (termios->c_cflag & PARENB)
490 cflag |= DZ_PARENB;
491 if (termios->c_cflag & PARODD)
492 cflag |= DZ_PARODD;
493
494 baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
495 switch (baud) {
496 case 50:
497 cflag |= DZ_B50;
498 break;
499 case 75:
500 cflag |= DZ_B75;
501 break;
502 case 110:
503 cflag |= DZ_B110;
504 break;
505 case 134:
506 cflag |= DZ_B134;
507 break;
508 case 150:
509 cflag |= DZ_B150;
510 break;
511 case 300:
512 cflag |= DZ_B300;
513 break;
514 case 600:
515 cflag |= DZ_B600;
516 break;
517 case 1200:
518 cflag |= DZ_B1200;
519 break;
520 case 1800:
521 cflag |= DZ_B1800;
522 break;
523 case 2000:
524 cflag |= DZ_B2000;
525 break;
526 case 2400:
527 cflag |= DZ_B2400;
528 break;
529 case 3600:
530 cflag |= DZ_B3600;
531 break;
532 case 4800:
533 cflag |= DZ_B4800;
534 break;
535 case 7200:
536 cflag |= DZ_B7200;
537 break;
538 case 9600:
539 default:
540 cflag |= DZ_B9600;
541 }
542
543 if (termios->c_cflag & CREAD)
544 cflag |= DZ_RXENAB;
545
546 spin_lock_irqsave(&dport->port.lock, flags);
547
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800548 dz_out(dport, DZ_LPR, cflag | dport->port.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 dport->cflag = cflag;
550
551 /* setup accept flag */
552 dport->port.read_status_mask = DZ_OERR;
553 if (termios->c_iflag & INPCK)
554 dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
555
556 /* characters to ignore */
557 uport->ignore_status_mask = 0;
558 if (termios->c_iflag & IGNPAR)
559 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
560
561 spin_unlock_irqrestore(&dport->port.lock, flags);
562}
563
564static const char *dz_type(struct uart_port *port)
565{
566 return "DZ";
567}
568
569static void dz_release_port(struct uart_port *port)
570{
571 /* nothing to do */
572}
573
574static int dz_request_port(struct uart_port *port)
575{
576 return 0;
577}
578
579static void dz_config_port(struct uart_port *port, int flags)
580{
581 if (flags & UART_CONFIG_TYPE)
582 port->type = PORT_DZ;
583}
584
585/*
586 * verify the new serial_struct (for TIOCSSERIAL).
587 */
588static int dz_verify_port(struct uart_port *port, struct serial_struct *ser)
589{
590 int ret = 0;
591 if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
592 ret = -EINVAL;
593 if (ser->irq != port->irq)
594 ret = -EINVAL;
595 return ret;
596}
597
598static struct uart_ops dz_ops = {
599 .tx_empty = dz_tx_empty,
600 .get_mctrl = dz_get_mctrl,
601 .set_mctrl = dz_set_mctrl,
602 .stop_tx = dz_stop_tx,
603 .start_tx = dz_start_tx,
604 .stop_rx = dz_stop_rx,
605 .enable_ms = dz_enable_ms,
606 .break_ctl = dz_break_ctl,
607 .startup = dz_startup,
608 .shutdown = dz_shutdown,
609 .set_termios = dz_set_termios,
610 .type = dz_type,
611 .release_port = dz_release_port,
612 .request_port = dz_request_port,
613 .config_port = dz_config_port,
614 .verify_port = dz_verify_port,
615};
616
617static void __init dz_init_ports(void)
618{
619 static int first = 1;
620 struct dz_port *dport;
621 unsigned long base;
622 int i;
623
624 if (!first)
625 return;
626 first = 0;
627
628 if (mips_machtype == MACH_DS23100 ||
629 mips_machtype == MACH_DS5100)
Ralf Baechle46677732005-11-12 22:00:27 +0000630 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 else
Ralf Baechle46677732005-11-12 22:00:27 +0000632 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 for (i = 0, dport = dz_ports; i < DZ_NB_PORT; i++, dport++) {
635 spin_lock_init(&dport->port.lock);
636 dport->port.membase = (char *) base;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800637 dport->port.iotype = UPIO_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dport->port.irq = dec_interrupt[DEC_IRQ_DZ11];
639 dport->port.line = i;
640 dport->port.fifosize = 1;
641 dport->port.ops = &dz_ops;
642 dport->port.flags = UPF_BOOT_AUTOCONF;
643 }
644}
645
646static void dz_reset(struct dz_port *dport)
647{
648 dz_out(dport, DZ_CSR, DZ_CLR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 while (dz_in(dport, DZ_CSR) & DZ_CLR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 iob();
651
652 /* enable scanning */
653 dz_out(dport, DZ_CSR, DZ_MSE);
654}
655
656#ifdef CONFIG_SERIAL_DZ_CONSOLE
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800657/*
658 * -------------------------------------------------------------------
659 * dz_console_putchar() -- transmit a character
660 *
661 * Polled transmission. This is tricky. We need to mask transmit
662 * interrupts so that they do not interfere, enable the transmitter
663 * for the line requested and then wait till the transmit scanner
664 * requests data for this line. But it may request data for another
665 * line first, in which case we have to disable its transmitter and
666 * repeat waiting till our line pops up. Only then the character may
667 * be transmitted. Finally, the state of the transmitter mask is
668 * restored. Welcome to the world of PDP-11!
669 * -------------------------------------------------------------------
670 */
Martin Michlmayrd608ab92006-06-26 18:18:11 +0200671static void dz_console_putchar(struct uart_port *uport, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Russell Kingd3587882006-03-20 20:00:09 +0000673 struct dz_port *dport = (struct dz_port *)uport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned long flags;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800675 unsigned short csr, tcr, trdy, mask;
676 int loops = 10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 spin_lock_irqsave(&dport->port.lock, flags);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800679 csr = dz_in(dport, DZ_CSR);
680 dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
681 tcr = dz_in(dport, DZ_TCR);
682 tcr |= 1 << dport->port.line;
683 mask = tcr;
684 dz_out(dport, DZ_TCR, mask);
685 iob();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spin_unlock_irqrestore(&dport->port.lock, flags);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800687
688 while (loops--) {
689 trdy = dz_in(dport, DZ_CSR);
690 if (!(trdy & DZ_TRDY))
691 continue;
692 trdy = (trdy & DZ_TLINE) >> 8;
693 if (trdy == dport->port.line)
694 break;
695 mask &= ~(1 << trdy);
696 dz_out(dport, DZ_TCR, mask);
697 iob();
698 udelay(2);
699 }
700
701 if (loops) /* Cannot send otherwise. */
702 dz_out(dport, DZ_TDR, ch);
703
704 dz_out(dport, DZ_TCR, tcr);
705 dz_out(dport, DZ_CSR, csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Russell Kingd3587882006-03-20 20:00:09 +0000707
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000708/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * -------------------------------------------------------------------
710 * dz_console_print ()
711 *
712 * dz_console_print is registered for printk.
713 * The console must be locked when we get here.
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000714 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800716static void dz_console_print(struct console *co,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 const char *str,
718 unsigned int count)
719{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800720 struct dz_port *dport = &dz_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#ifdef DEBUG_DZ
722 prom_printf((char *) str);
723#endif
Russell Kingd3587882006-03-20 20:00:09 +0000724 uart_console_write(&dport->port, str, count, dz_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static int __init dz_console_setup(struct console *co, char *options)
728{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800729 struct dz_port *dport = &dz_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int baud = 9600;
731 int bits = 8;
732 int parity = 'n';
733 int flow = 'n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (options)
736 uart_parse_options(options, &baud, &parity, &bits, &flow);
737
738 dz_reset(dport);
739
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800740 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800743static struct uart_driver dz_reg;
744static struct console dz_sercons = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .name = "ttyS",
746 .write = dz_console_print,
747 .device = uart_console_device,
748 .setup = dz_console_setup,
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800749 .flags = CON_PRINTBUFFER,
750 .index = -1,
751 .data = &dz_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752};
753
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800754static int __init dz_serial_console_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800756 if (!IOASIC) {
757 dz_init_ports();
758 register_console(&dz_sercons);
759 return 0;
760 } else
761 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800764console_initcall(dz_serial_console_init);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#define SERIAL_DZ_CONSOLE &dz_sercons
767#else
768#define SERIAL_DZ_CONSOLE NULL
769#endif /* CONFIG_SERIAL_DZ_CONSOLE */
770
771static struct uart_driver dz_reg = {
772 .owner = THIS_MODULE,
773 .driver_name = "serial",
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800774 .dev_name = "ttyS",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 .major = TTY_MAJOR,
776 .minor = 64,
777 .nr = DZ_NB_PORT,
778 .cons = SERIAL_DZ_CONSOLE,
779};
780
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800781static int __init dz_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int ret, i;
784
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800785 if (IOASIC)
786 return -ENXIO;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 printk("%s%s\n", dz_name, dz_version);
789
790 dz_init_ports();
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792#ifndef CONFIG_SERIAL_DZ_CONSOLE
793 /* reset the chip */
794 dz_reset(&dz_ports[0]);
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (request_irq(dz_ports[0].port.irq, dz_interrupt,
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700798 IRQF_DISABLED, "DZ", &dz_ports[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 panic("Unable to register DZ interrupt");
800
801 ret = uart_register_driver(&dz_reg);
802 if (ret != 0)
803 return ret;
804
805 for (i = 0; i < DZ_NB_PORT; i++)
806 uart_add_one_port(&dz_reg, &dz_ports[i].port);
807
808 return ret;
809}
810
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800811module_init(dz_init);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813MODULE_DESCRIPTION("DECstation DZ serial driver");
814MODULE_LICENSE("GPL");