blob: c054c1bd8b24fa27a1542087fdca317b87ecc205 [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. Rozycki87cff7f2008-02-07 00:15:09 -08009 * Copyright (C) 2004, 2006, 2007 Maciej W. Rozycki
Maciej W. Rozycki93995752006-12-06 20:38:59 -080010 *
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
Maciej W. Rozycki87cff7f2008-02-07 00:15:09 -080035#include <linux/bitops.h>
36#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/console.h>
Maciej W. Rozycki87cff7f2008-02-07 00:15:09 -080038#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>
Maciej W. Rozycki93995752006-12-06 20:38:59 -080047#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/bootinfo.h>
Maciej W. Rozycki87cff7f2008-02-07 00:15:09 -080051#include <asm/system.h>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "dz.h"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static char *dz_name = "DECstation DZ serial driver version ";
Maciej W. Rozycki93995752006-12-06 20:38:59 -080062static char *dz_version = "1.03";
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64struct dz_port {
65 struct uart_port port;
66 unsigned int cflag;
67};
68
69static struct dz_port dz_ports[DZ_NB_PORT];
70
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -080071static inline struct dz_port *to_dport(struct uart_port *uport)
72{
73 return container_of(uport, struct dz_port, port);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * ------------------------------------------------------------
78 * dz_in () and dz_out ()
79 *
Ralf Baechlefd8c5972005-11-12 21:59:59 +000080 * These routines are used to access the registers of the DZ
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * chip, hiding relocation differences between implementation.
82 * ------------------------------------------------------------
83 */
84
85static 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);
Maciej W. Rozycki93995752006-12-06 20:38:59 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return *addr;
91}
92
93static 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);
Maciej W. Rozycki93995752006-12-06 20:38:59 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *addr = value;
100}
101
102/*
103 * ------------------------------------------------------------
104 * rs_stop () and rs_start ()
105 *
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000106 * These routines are called before setting or resetting
107 * tty->stopped. They enable or disable transmitter interrupts,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * as necessary.
109 * ------------------------------------------------------------
110 */
111
Russell Kingb129a8c2005-08-31 10:12:14 +0100112static void dz_stop_tx(struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800114 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
118 tmp &= ~mask; /* clear the TX flag */
119 dz_out(dport, DZ_TCR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Russell Kingb129a8c2005-08-31 10:12:14 +0100122static void dz_start_tx(struct uart_port *uport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800124 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 tmp = dz_in(dport, DZ_TCR); /* read the TX flag */
128 tmp |= mask; /* set the TX flag */
129 dz_out(dport, DZ_TCR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static void dz_stop_rx(struct uart_port *uport)
133{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800134 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800136 dport->cflag &= ~DZ_RXENAB;
137 dz_out(dport, DZ_LPR, dport->cflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static void dz_enable_ms(struct uart_port *port)
141{
142 /* nothing to do */
143}
144
145/*
146 * ------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800148 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * runs with interrupts turned off. People who may want to modify
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800154 * dz_interrupt() should try to keep the interrupt handler as fast as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * possible. After you are done making modifications, it is not a bad
156 * idea to do:
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000157 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * 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 */
Maciej W. Rozyckide320192007-03-05 00:30:26 -0800172static inline void dz_receive_chars(struct dz_port *dport_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800174 struct uart_port *uport;
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)];
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800185 uport = &dport->port;
186 tty = uport->info->tty; /* point to the proper dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800188 ch = UCHAR(status); /* grab the char */
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800189 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800191 icount = &uport->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 icount->rx++;
193
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800194 if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800196 /*
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
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800228 }
229
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800230 if (uart_handle_sysrq_char(uport, ch))
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800231 continue;
232
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800233 uart_insert_char(uport, status, DZ_OERR, ch, flag);
234 lines_rx[LINE(status)] = 1;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800235 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/*
242 * ------------------------------------------------------------
243 * transmit_char ()
244 *
245 * This routine deals with outputs to any lines.
246 * ------------------------------------------------------------
247 */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800248static inline void dz_transmit_chars(struct dz_port *dport_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800250 struct dz_port *dport;
251 struct circ_buf *xmit;
252 unsigned short status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned char tmp;
254
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800255 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dz_out(dport, DZ_TDR, dport->port.x_char);
261 dport->port.icount.tx++;
262 dport->port.x_char = 0;
263 return;
264 }
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800265 /* If nothing to do or stopped or hardware stopped. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
Maciej W. Rozycki43d46ab2008-02-07 00:15:11 -0800267 spin_lock(&dport->port.lock);
Russell Kingb129a8c2005-08-31 10:12:14 +0100268 dz_stop_tx(&dport->port);
Maciej W. Rozycki43d46ab2008-02-07 00:15:11 -0800269 spin_unlock(&dport->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return;
271 }
272
273 /*
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800274 * If something to do... (remember the dz has no output fifo,
275 * so we go one char at a time) :-<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
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
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800285 /* Are we are done. */
Maciej W. Rozycki43d46ab2008-02-07 00:15:11 -0800286 if (uart_circ_empty(xmit)) {
287 spin_lock(&dport->port.lock);
Russell Kingb129a8c2005-08-31 10:12:14 +0100288 dz_stop_tx(&dport->port);
Maciej W. Rozycki43d46ab2008-02-07 00:15:11 -0800289 spin_unlock(&dport->port.lock);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * ------------------------------------------------------------
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800295 * check_modem_status()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800297 * DS 3100 & 5100: Only valid for the MODEM line, duh!
298 * DS 5000/200: Valid for the MODEM and PRINTER line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * ------------------------------------------------------------
300 */
301static inline void check_modem_status(struct dz_port *dport)
302{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800303 /*
304 * FIXME:
305 * 1. No status change interrupt; use a timer.
306 * 2. Handle the 3100/5000 as appropriate. --macro
307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned short status;
309
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800310 /* If not the modem line just return. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 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 */
David Howells7d12e782006-10-05 14:55:46 +0100329static irqreturn_t dz_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800331 struct dz_port *dport = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned short status;
333
334 /* get the reason why we just got an irq */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800335 status = dz_in(dport, DZ_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800337 if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
Maciej W. Rozyckide320192007-03-05 00:30:26 -0800338 dz_receive_chars(dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800340 if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dz_transmit_chars(dport);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return IRQ_HANDLED;
344}
345
346/*
347 * -------------------------------------------------------------------
348 * Here ends the DZ interrupt routines.
349 * -------------------------------------------------------------------
350 */
351
352static unsigned int dz_get_mctrl(struct uart_port *uport)
353{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800354 /*
355 * FIXME: Handle the 3100/5000 as appropriate. --macro
356 */
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800357 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
359
360 if (dport->port.line == DZ_MODEM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
362 mctrl &= ~TIOCM_DSR;
363 }
364
365 return mctrl;
366}
367
368static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
369{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800370 /*
371 * FIXME: Handle the 3100/5000 as appropriate. --macro
372 */
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800373 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 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
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000391 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
393static int dz_startup(struct uart_port *uport)
394{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800395 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned long flags;
397 unsigned short tmp;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 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
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000411/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * -------------------------------------------------------------------
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.
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000417 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419static void dz_shutdown(struct uart_port *uport)
420{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800421 struct dz_port *dport = to_dport(uport);
Maciej W. Rozycki43d46ab2008-02-07 00:15:11 -0800422 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800430 * -------------------------------------------------------------------
431 * dz_tx_empty() -- get the transmitter empty status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
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
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000438 * allows an RS485 driver to be written in user space.
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800439 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
441static unsigned int dz_tx_empty(struct uart_port *uport)
442{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800443 struct dz_port *dport = to_dport(uport);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800444 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800446 tmp = dz_in(dport, DZ_TCR);
447 tmp &= mask;
448
449 return tmp ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static void dz_break_ctl(struct uart_port *uport, int break_state)
453{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800454 /*
455 * FIXME: Can't access BREAK bits in TDR easily;
456 * reuse the code for polled TX. --macro
457 */
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800458 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 unsigned long flags;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800460 unsigned short tmp, mask = 1 << dport->port.line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
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
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800472static 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
Alan Cox606d0992006-12-08 02:38:45 -0800510static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
511 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800513 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 unsigned long flags;
515 unsigned int cflag, baud;
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800516 int bflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
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);
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800543 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800553 cflag |= bflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (termios->c_cflag & CREAD)
556 cflag |= DZ_RXENAB;
557
558 spin_lock_irqsave(&dport->port.lock, flags);
559
Maciej W. Rozyckiff11d072008-02-07 00:15:14 -0800560 uart_update_timeout(uport, termios->c_cflag, baud);
561
562 dz_out(dport, DZ_LPR, cflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 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;
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800569 if (termios->c_iflag & (BRKINT | PARMRK))
570 dport->port.read_status_mask |= DZ_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* characters to ignore */
573 uport->ignore_status_mask = 0;
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800574 if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
575 dport->port.ignore_status_mask |= DZ_OERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (termios->c_iflag & IGNPAR)
577 dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
Maciej W. Rozycki54c0f372008-02-07 00:15:12 -0800578 if (termios->c_iflag & IGNBRK)
579 dport->port.ignore_status_mask |= DZ_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 spin_unlock_irqrestore(&dport->port.lock, flags);
582}
583
584static const char *dz_type(struct uart_port *port)
585{
586 return "DZ";
587}
588
589static void dz_release_port(struct uart_port *port)
590{
591 /* nothing to do */
592}
593
594static int dz_request_port(struct uart_port *port)
595{
596 return 0;
597}
598
599static 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 */
608static 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
618static 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
637static 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)
Ralf Baechle46677732005-11-12 22:00:27 +0000650 base = CKSEG1ADDR(KN01_SLOT_BASE + KN01_DZ11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 else
Ralf Baechle46677732005-11-12 22:00:27 +0000652 base = CKSEG1ADDR(KN02_SLOT_BASE + KN02_DZ11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
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;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800657 dport->port.iotype = UPIO_MEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 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
666static void dz_reset(struct dz_port *dport)
667{
668 dz_out(dport, DZ_CSR, DZ_CLR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 while (dz_in(dport, DZ_CSR) & DZ_CLR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 iob();
671
672 /* enable scanning */
673 dz_out(dport, DZ_CSR, DZ_MSE);
674}
675
676#ifdef CONFIG_SERIAL_DZ_CONSOLE
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800677/*
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 */
Martin Michlmayrd608ab92006-06-26 18:18:11 +0200691static void dz_console_putchar(struct uart_port *uport, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Maciej W. Rozyckif5519ca2008-02-07 00:15:15 -0800693 struct dz_port *dport = to_dport(uport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned long flags;
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800695 unsigned short csr, tcr, trdy, mask;
696 int loops = 10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 spin_lock_irqsave(&dport->port.lock, flags);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800699 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 spin_unlock_irqrestore(&dport->port.lock, flags);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800707
Maciej W. Rozyckidbab8122008-02-07 00:15:07 -0800708 do {
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800709 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);
Maciej W. Rozyckidbab8122008-02-07 00:15:07 -0800719 } while (loops--);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800720
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Russell Kingd3587882006-03-20 20:00:09 +0000727
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000728/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * -------------------------------------------------------------------
730 * dz_console_print ()
731 *
732 * dz_console_print is registered for printk.
733 * The console must be locked when we get here.
Ralf Baechlefd8c5972005-11-12 21:59:59 +0000734 * -------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800736static void dz_console_print(struct console *co,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 const char *str,
738 unsigned int count)
739{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800740 struct dz_port *dport = &dz_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#ifdef DEBUG_DZ
742 prom_printf((char *) str);
743#endif
Russell Kingd3587882006-03-20 20:00:09 +0000744 uart_console_write(&dport->port, str, count, dz_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static int __init dz_console_setup(struct console *co, char *options)
748{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800749 struct dz_port *dport = &dz_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int baud = 9600;
751 int bits = 8;
752 int parity = 'n';
753 int flow = 'n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (options)
756 uart_parse_options(options, &baud, &parity, &bits, &flow);
757
758 dz_reset(dport);
759
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800760 return uart_set_options(&dport->port, co, baud, parity, bits, flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800763static struct uart_driver dz_reg;
Maciej W. Rozycki6d83c062008-02-07 00:15:10 -0800764static struct console dz_console = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .name = "ttyS",
766 .write = dz_console_print,
767 .device = uart_console_device,
768 .setup = dz_console_setup,
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800769 .flags = CON_PRINTBUFFER,
770 .index = -1,
771 .data = &dz_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772};
773
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800774static int __init dz_serial_console_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800776 if (!IOASIC) {
777 dz_init_ports();
Maciej W. Rozycki6d83c062008-02-07 00:15:10 -0800778 register_console(&dz_console);
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800779 return 0;
780 } else
781 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800784console_initcall(dz_serial_console_init);
785
Maciej W. Rozycki6d83c062008-02-07 00:15:10 -0800786#define SERIAL_DZ_CONSOLE &dz_console
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#else
788#define SERIAL_DZ_CONSOLE NULL
789#endif /* CONFIG_SERIAL_DZ_CONSOLE */
790
791static struct uart_driver dz_reg = {
792 .owner = THIS_MODULE,
793 .driver_name = "serial",
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800794 .dev_name = "ttyS",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .major = TTY_MAJOR,
796 .minor = 64,
797 .nr = DZ_NB_PORT,
798 .cons = SERIAL_DZ_CONSOLE,
799};
800
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800801static int __init dz_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int ret, i;
804
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800805 if (IOASIC)
806 return -ENXIO;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 printk("%s%s\n", dz_name, dz_version);
809
810 dz_init_ports();
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#ifndef CONFIG_SERIAL_DZ_CONSOLE
813 /* reset the chip */
814 dz_reset(&dz_ports[0]);
815#endif
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ret = uart_register_driver(&dz_reg);
818 if (ret != 0)
Maciej W. Rozycki0ba137e2008-02-07 00:15:08 -0800819 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 for (i = 0; i < DZ_NB_PORT; i++)
830 uart_add_one_port(&dz_reg, &dz_ports[i].port);
831
832 return ret;
Maciej W. Rozycki0ba137e2008-02-07 00:15:08 -0800833
834out_unregister:
835 uart_unregister_driver(&dz_reg);
836
837out:
838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Maciej W. Rozycki93995752006-12-06 20:38:59 -0800841module_init(dz_init);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843MODULE_DESCRIPTION("DECstation DZ serial driver");
844MODULE_LICENSE("GPL");