dz.c 22.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * dz.c: Serial port driver for DECstations equipped
L
Linus Torvalds 已提交
3 4
 *       with the DZ chipset.
 *
5 6
 * Copyright (C) 1998 Olivier A. D. Lebaillif
 *
L
Linus Torvalds 已提交
7 8
 * Email: olivier.lebaillif@ifrsys.com
 *
9
 * Copyright (C) 2004, 2006, 2007  Maciej W. Rozycki
10
 *
L
Linus Torvalds 已提交
11 12 13 14 15
 * [31-AUG-98] triemer
 * Changed IRQ to use Harald's dec internals interrupts.h
 * removed base_addr code - moving address assignment to setup.c
 * Changed name of dz_init to rs_init to be consistent with tc code
 * [13-NOV-98] triemer fixed code to receive characters
16
 *    after patches by harald to irq code.
L
Linus Torvalds 已提交
17 18 19 20
 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
 *            field from "current" - somewhere between 2.1.121 and 2.1.131
 Qua Jun 27 15:02:26 BRT 2001
 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
21 22 23
 *
 * Parts (C) 1999 David Airlie, airlied@linux.ie
 * [07-SEP-99] Bugfixes
L
Linus Torvalds 已提交
24 25 26 27 28 29 30
 *
 * [06-Jan-2002] Russell King <rmk@arm.linux.org.uk>
 * Converted to new serial core
 */

#undef DEBUG_DZ

31 32 33 34
#if defined(CONFIG_SERIAL_DZ_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

35 36 37
#include <linux/bitops.h>
#include <linux/compiler.h>
#include <linux/console.h>
38
#include <linux/delay.h>
39
#include <linux/errno.h>
L
Linus Torvalds 已提交
40
#include <linux/init.h>
41
#include <linux/interrupt.h>
M
Maciej W. Rozycki 已提交
42
#include <linux/ioport.h>
43 44 45 46 47
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/module.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
48
#include <linux/sysrq.h>
L
Linus Torvalds 已提交
49
#include <linux/tty.h>
50
#include <linux/tty_flip.h>
L
Linus Torvalds 已提交
51

A
Arun Sharma 已提交
52
#include <linux/atomic.h>
L
Linus Torvalds 已提交
53
#include <asm/bootinfo.h>
M
Maciej W. Rozycki 已提交
54
#include <asm/io.h>
55

L
Linus Torvalds 已提交
56 57 58 59 60
#include <asm/dec/interrupts.h>
#include <asm/dec/kn01.h>
#include <asm/dec/kn02.h>
#include <asm/dec/machtype.h>
#include <asm/dec/prom.h>
M
Maciej W. Rozycki 已提交
61
#include <asm/dec/system.h>
L
Linus Torvalds 已提交
62 63 64

#include "dz.h"

M
Maciej W. Rozycki 已提交
65 66 67 68 69 70 71

MODULE_DESCRIPTION("DECstation DZ serial driver");
MODULE_LICENSE("GPL");


static char dz_name[] __initdata = "DECstation DZ serial driver version ";
static char dz_version[] __initdata = "1.04";
L
Linus Torvalds 已提交
72 73

struct dz_port {
M
Maciej W. Rozycki 已提交
74
	struct dz_mux		*mux;
L
Linus Torvalds 已提交
75 76 77 78
	struct uart_port	port;
	unsigned int		cflag;
};

M
Maciej W. Rozycki 已提交
79 80 81 82 83 84 85 86
struct dz_mux {
	struct dz_port		dport[DZ_NB_PORT];
	atomic_t		map_guard;
	atomic_t		irq_guard;
	int			initialised;
};

static struct dz_mux dz_mux;
L
Linus Torvalds 已提交
87

88 89 90 91 92
static inline struct dz_port *to_dport(struct uart_port *uport)
{
	return container_of(uport, struct dz_port, port);
}

L
Linus Torvalds 已提交
93 94 95 96
/*
 * ------------------------------------------------------------
 * dz_in () and dz_out ()
 *
97
 * These routines are used to access the registers of the DZ
L
Linus Torvalds 已提交
98 99 100 101
 * chip, hiding relocation differences between implementation.
 * ------------------------------------------------------------
 */

M
Maciej W. Rozycki 已提交
102
static u16 dz_in(struct dz_port *dport, unsigned offset)
L
Linus Torvalds 已提交
103
{
M
Maciej W. Rozycki 已提交
104
	void __iomem *addr = dport->port.membase + offset;
105

M
Maciej W. Rozycki 已提交
106
	return readw(addr);
L
Linus Torvalds 已提交
107 108
}

M
Maciej W. Rozycki 已提交
109
static void dz_out(struct dz_port *dport, unsigned offset, u16 value)
L
Linus Torvalds 已提交
110
{
M
Maciej W. Rozycki 已提交
111
	void __iomem *addr = dport->port.membase + offset;
112

M
Maciej W. Rozycki 已提交
113
	writew(value, addr);
L
Linus Torvalds 已提交
114 115 116 117 118 119
}

/*
 * ------------------------------------------------------------
 * rs_stop () and rs_start ()
 *
120 121
 * These routines are called before setting or resetting
 * tty->stopped. They enable or disable transmitter interrupts,
L
Linus Torvalds 已提交
122 123 124 125
 * as necessary.
 * ------------------------------------------------------------
 */

126
static void dz_stop_tx(struct uart_port *uport)
L
Linus Torvalds 已提交
127
{
128
	struct dz_port *dport = to_dport(uport);
M
Maciej W. Rozycki 已提交
129
	u16 tmp, mask = 1 << dport->port.line;
L
Linus Torvalds 已提交
130 131 132 133 134 135

	tmp = dz_in(dport, DZ_TCR);	/* read the TX flag */
	tmp &= ~mask;			/* clear the TX flag */
	dz_out(dport, DZ_TCR, tmp);
}

136
static void dz_start_tx(struct uart_port *uport)
L
Linus Torvalds 已提交
137
{
138
	struct dz_port *dport = to_dport(uport);
M
Maciej W. Rozycki 已提交
139
	u16 tmp, mask = 1 << dport->port.line;
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147

	tmp = dz_in(dport, DZ_TCR);	/* read the TX flag */
	tmp |= mask;			/* set the TX flag */
	dz_out(dport, DZ_TCR, tmp);
}

static void dz_stop_rx(struct uart_port *uport)
{
148
	struct dz_port *dport = to_dport(uport);
L
Linus Torvalds 已提交
149

150 151
	dport->cflag &= ~DZ_RXENAB;
	dz_out(dport, DZ_LPR, dport->cflag);
L
Linus Torvalds 已提交
152 153 154 155 156
}

/*
 * ------------------------------------------------------------
 *
157 158 159 160 161
 * Here start the interrupt handling routines.  All of the following
 * subroutines are declared as inline and are folded into
 * dz_interrupt.  They were separated out for readability's sake.
 *
 * Note: dz_interrupt() is a "fast" interrupt, which means that it
L
Linus Torvalds 已提交
162
 * runs with interrupts turned off.  People who may want to modify
163
 * dz_interrupt() should try to keep the interrupt handler as fast as
L
Linus Torvalds 已提交
164 165
 * possible.  After you are done making modifications, it is not a bad
 * idea to do:
166
 *
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180
 *	make drivers/serial/dz.s
 *
 * and look at the resulting assemble code in dz.s.
 *
 * ------------------------------------------------------------
 */

/*
 * ------------------------------------------------------------
 * receive_char ()
 *
 * This routine deals with inputs from any lines.
 * ------------------------------------------------------------
 */
M
Maciej W. Rozycki 已提交
181
static inline void dz_receive_chars(struct dz_mux *mux)
L
Linus Torvalds 已提交
182
{
183
	struct uart_port *uport;
M
Maciej W. Rozycki 已提交
184
	struct dz_port *dport = &mux->dport[0];
L
Linus Torvalds 已提交
185
	struct uart_icount *icount;
186
	int lines_rx[DZ_NB_PORT] = { [0 ... DZ_NB_PORT - 1] = 0 };
L
Linus Torvalds 已提交
187
	unsigned char ch, flag;
M
Maciej W. Rozycki 已提交
188
	u16 status;
189
	int i;
L
Linus Torvalds 已提交
190

M
Maciej W. Rozycki 已提交
191 192
	while ((status = dz_in(dport, DZ_RBUF)) & DZ_DVAL) {
		dport = &mux->dport[LINE(status)];
193
		uport = &dport->port;
L
Linus Torvalds 已提交
194

195
		ch = UCHAR(status);		/* grab the char */
196
		flag = TTY_NORMAL;
L
Linus Torvalds 已提交
197

198
		icount = &uport->icount;
L
Linus Torvalds 已提交
199 200
		icount->rx++;

201 202
		if (unlikely(status & (DZ_OERR | DZ_FERR | DZ_PERR))) {

203
			/*
204 205 206 207
			 * There is no separate BREAK status bit, so treat
			 * null characters with framing errors as BREAKs;
			 * normally, otherwise.  For this move the Framing
			 * Error bit to a simulated BREAK bit.
L
Linus Torvalds 已提交
208
			 */
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
			if (!ch) {
				status |= (status & DZ_FERR) >>
					  (ffs(DZ_FERR) - ffs(DZ_BREAK));
				status &= ~DZ_FERR;
			}

			/* Handle SysRq/SAK & keep track of the statistics. */
			if (status & DZ_BREAK) {
				icount->brk++;
				if (uart_handle_break(uport))
					continue;
			} else if (status & DZ_FERR)
				icount->frame++;
			else if (status & DZ_PERR)
				icount->parity++;
			if (status & DZ_OERR)
				icount->overrun++;

			status &= uport->read_status_mask;
			if (status & DZ_BREAK)
229
				flag = TTY_BREAK;
230
			else if (status & DZ_FERR)
L
Linus Torvalds 已提交
231
				flag = TTY_FRAME;
232 233 234
			else if (status & DZ_PERR)
				flag = TTY_PARITY;

L
Linus Torvalds 已提交
235 236
		}

237
		if (uart_handle_sysrq_char(uport, ch))
238 239
			continue;

240 241
		uart_insert_char(uport, status, DZ_OERR, ch, flag);
		lines_rx[LINE(status)] = 1;
242 243 244
	}
	for (i = 0; i < DZ_NB_PORT; i++)
		if (lines_rx[i])
J
Jiri Slaby 已提交
245
			tty_flip_buffer_push(&mux->dport[i].port.state->port);
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254
}

/*
 * ------------------------------------------------------------
 * transmit_char ()
 *
 * This routine deals with outputs to any lines.
 * ------------------------------------------------------------
 */
M
Maciej W. Rozycki 已提交
255
static inline void dz_transmit_chars(struct dz_mux *mux)
L
Linus Torvalds 已提交
256
{
M
Maciej W. Rozycki 已提交
257
	struct dz_port *dport = &mux->dport[0];
258
	struct circ_buf *xmit;
L
Linus Torvalds 已提交
259
	unsigned char tmp;
M
Maciej W. Rozycki 已提交
260
	u16 status;
L
Linus Torvalds 已提交
261

M
Maciej W. Rozycki 已提交
262 263
	status = dz_in(dport, DZ_CSR);
	dport = &mux->dport[LINE(status)];
A
Alan Cox 已提交
264
	xmit = &dport->port.state->xmit;
265 266

	if (dport->port.x_char) {		/* XON/XOFF chars */
L
Linus Torvalds 已提交
267 268 269 270 271
		dz_out(dport, DZ_TDR, dport->port.x_char);
		dport->port.icount.tx++;
		dport->port.x_char = 0;
		return;
	}
272
	/* If nothing to do or stopped or hardware stopped. */
L
Linus Torvalds 已提交
273
	if (uart_circ_empty(xmit) || uart_tx_stopped(&dport->port)) {
M
Maciej W. Rozycki 已提交
274
		spin_lock(&dport->port.lock);
275
		dz_stop_tx(&dport->port);
M
Maciej W. Rozycki 已提交
276
		spin_unlock(&dport->port.lock);
L
Linus Torvalds 已提交
277 278 279 280
		return;
	}

	/*
281 282
	 * If something to do... (remember the dz has no output fifo,
	 * so we go one char at a time) :-<
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291
	 */
	tmp = xmit->buf[xmit->tail];
	xmit->tail = (xmit->tail + 1) & (DZ_XMIT_SIZE - 1);
	dz_out(dport, DZ_TDR, tmp);
	dport->port.icount.tx++;

	if (uart_circ_chars_pending(xmit) < DZ_WAKEUP_CHARS)
		uart_write_wakeup(&dport->port);

292
	/* Are we are done. */
M
Maciej W. Rozycki 已提交
293 294
	if (uart_circ_empty(xmit)) {
		spin_lock(&dport->port.lock);
295
		dz_stop_tx(&dport->port);
M
Maciej W. Rozycki 已提交
296 297
		spin_unlock(&dport->port.lock);
	}
L
Linus Torvalds 已提交
298 299 300 301
}

/*
 * ------------------------------------------------------------
302
 * check_modem_status()
L
Linus Torvalds 已提交
303
 *
304 305
 * DS 3100 & 5100: Only valid for the MODEM line, duh!
 * DS 5000/200: Valid for the MODEM and PRINTER line.
L
Linus Torvalds 已提交
306 307 308 309
 * ------------------------------------------------------------
 */
static inline void check_modem_status(struct dz_port *dport)
{
310 311 312 313 314
	/*
	 * FIXME:
	 * 1. No status change interrupt; use a timer.
	 * 2. Handle the 3100/5000 as appropriate. --macro
	 */
M
Maciej W. Rozycki 已提交
315
	u16 status;
L
Linus Torvalds 已提交
316

317
	/* If not the modem line just return.  */
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	if (dport->port.line != DZ_MODEM)
		return;

	status = dz_in(dport, DZ_MSR);

	/* it's easy, since DSR2 is the only bit in the register */
	if (status)
		dport->port.icount.dsr++;
}

/*
 * ------------------------------------------------------------
 * dz_interrupt ()
 *
 * this is the main interrupt routine for the DZ chip.
 * It deals with the multiple ports.
 * ------------------------------------------------------------
 */
M
Maciej W. Rozycki 已提交
336
static irqreturn_t dz_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
337
{
M
Maciej W. Rozycki 已提交
338 339 340
	struct dz_mux *mux = dev_id;
	struct dz_port *dport = &mux->dport[0];
	u16 status;
L
Linus Torvalds 已提交
341 342

	/* get the reason why we just got an irq */
343
	status = dz_in(dport, DZ_CSR);
L
Linus Torvalds 已提交
344

345
	if ((status & (DZ_RDONE | DZ_RIE)) == (DZ_RDONE | DZ_RIE))
M
Maciej W. Rozycki 已提交
346
		dz_receive_chars(mux);
L
Linus Torvalds 已提交
347

348
	if ((status & (DZ_TRDY | DZ_TIE)) == (DZ_TRDY | DZ_TIE))
M
Maciej W. Rozycki 已提交
349
		dz_transmit_chars(mux);
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361

	return IRQ_HANDLED;
}

/*
 * -------------------------------------------------------------------
 * Here ends the DZ interrupt routines.
 * -------------------------------------------------------------------
 */

static unsigned int dz_get_mctrl(struct uart_port *uport)
{
362 363 364
	/*
	 * FIXME: Handle the 3100/5000 as appropriate. --macro
	 */
365
	struct dz_port *dport = to_dport(uport);
L
Linus Torvalds 已提交
366 367 368 369 370 371 372 373 374 375 376 377
	unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;

	if (dport->port.line == DZ_MODEM) {
		if (dz_in(dport, DZ_MSR) & DZ_MODEM_DSR)
			mctrl &= ~TIOCM_DSR;
	}

	return mctrl;
}

static void dz_set_mctrl(struct uart_port *uport, unsigned int mctrl)
{
378 379 380
	/*
	 * FIXME: Handle the 3100/5000 as appropriate. --macro
	 */
381
	struct dz_port *dport = to_dport(uport);
M
Maciej W. Rozycki 已提交
382
	u16 tmp;
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

	if (dport->port.line == DZ_MODEM) {
		tmp = dz_in(dport, DZ_TCR);
		if (mctrl & TIOCM_DTR)
			tmp &= ~DZ_MODEM_DTR;
		else
			tmp |= DZ_MODEM_DTR;
		dz_out(dport, DZ_TCR, tmp);
	}
}

/*
 * -------------------------------------------------------------------
 * startup ()
 *
 * various initialization tasks
399
 * -------------------------------------------------------------------
L
Linus Torvalds 已提交
400 401 402
 */
static int dz_startup(struct uart_port *uport)
{
403
	struct dz_port *dport = to_dport(uport);
M
Maciej W. Rozycki 已提交
404
	struct dz_mux *mux = dport->mux;
L
Linus Torvalds 已提交
405
	unsigned long flags;
M
Maciej W. Rozycki 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	int irq_guard;
	int ret;
	u16 tmp;

	irq_guard = atomic_add_return(1, &mux->irq_guard);
	if (irq_guard != 1)
		return 0;

	ret = request_irq(dport->port.irq, dz_interrupt,
			  IRQF_SHARED, "dz", mux);
	if (ret) {
		atomic_add(-1, &mux->irq_guard);
		printk(KERN_ERR "dz: Cannot get IRQ %d!\n", dport->port.irq);
		return ret;
	}
L
Linus Torvalds 已提交
421 422 423

	spin_lock_irqsave(&dport->port.lock, flags);

M
Maciej W. Rozycki 已提交
424
	/* Enable interrupts.  */
L
Linus Torvalds 已提交
425
	tmp = dz_in(dport, DZ_CSR);
M
Maciej W. Rozycki 已提交
426
	tmp |= DZ_RIE | DZ_TIE;
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
	dz_out(dport, DZ_CSR, tmp);

	spin_unlock_irqrestore(&dport->port.lock, flags);

	return 0;
}

434
/*
L
Linus Torvalds 已提交
435 436 437 438 439
 * -------------------------------------------------------------------
 * shutdown ()
 *
 * This routine will shutdown a serial port; interrupts are disabled, and
 * DTR is dropped if the hangup on close termio flag is on.
440
 * -------------------------------------------------------------------
L
Linus Torvalds 已提交
441 442 443
 */
static void dz_shutdown(struct uart_port *uport)
{
444
	struct dz_port *dport = to_dport(uport);
M
Maciej W. Rozycki 已提交
445
	struct dz_mux *mux = dport->mux;
M
Maciej W. Rozycki 已提交
446
	unsigned long flags;
M
Maciej W. Rozycki 已提交
447 448
	int irq_guard;
	u16 tmp;
M
Maciej W. Rozycki 已提交
449 450 451 452

	spin_lock_irqsave(&dport->port.lock, flags);
	dz_stop_tx(&dport->port);
	spin_unlock_irqrestore(&dport->port.lock, flags);
M
Maciej W. Rozycki 已提交
453 454 455 456 457 458 459 460 461 462

	irq_guard = atomic_add_return(-1, &mux->irq_guard);
	if (!irq_guard) {
		/* Disable interrupts.  */
		tmp = dz_in(dport, DZ_CSR);
		tmp &= ~(DZ_RIE | DZ_TIE);
		dz_out(dport, DZ_CSR, tmp);

		free_irq(dport->port.irq, mux);
	}
L
Linus Torvalds 已提交
463 464 465
}

/*
466 467
 * -------------------------------------------------------------------
 * dz_tx_empty() -- get the transmitter empty status
L
Linus Torvalds 已提交
468 469 470 471 472 473
 *
 * Purpose: Let user call ioctl() to get info when the UART physically
 *          is emptied.  On bus types like RS485, the transmitter must
 *          release the bus after transmitting. This must be done when
 *          the transmit shift register is empty, not be done when the
 *          transmit holding register is empty.  This functionality
474
 *          allows an RS485 driver to be written in user space.
475
 * -------------------------------------------------------------------
L
Linus Torvalds 已提交
476 477 478
 */
static unsigned int dz_tx_empty(struct uart_port *uport)
{
479
	struct dz_port *dport = to_dport(uport);
480
	unsigned short tmp, mask = 1 << dport->port.line;
L
Linus Torvalds 已提交
481

482 483 484 485
	tmp = dz_in(dport, DZ_TCR);
	tmp &= mask;

	return tmp ? 0 : TIOCSER_TEMT;
L
Linus Torvalds 已提交
486 487 488 489
}

static void dz_break_ctl(struct uart_port *uport, int break_state)
{
490 491 492 493
	/*
	 * FIXME: Can't access BREAK bits in TDR easily;
	 * reuse the code for polled TX. --macro
	 */
494
	struct dz_port *dport = to_dport(uport);
L
Linus Torvalds 已提交
495
	unsigned long flags;
496
	unsigned short tmp, mask = 1 << dport->port.line;
L
Linus Torvalds 已提交
497 498 499 500 501 502 503 504 505 506 507

	spin_lock_irqsave(&uport->lock, flags);
	tmp = dz_in(dport, DZ_TCR);
	if (break_state)
		tmp |= mask;
	else
		tmp &= ~mask;
	dz_out(dport, DZ_TCR, tmp);
	spin_unlock_irqrestore(&uport->lock, flags);
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static int dz_encode_baud_rate(unsigned int baud)
{
	switch (baud) {
	case 50:
		return DZ_B50;
	case 75:
		return DZ_B75;
	case 110:
		return DZ_B110;
	case 134:
		return DZ_B134;
	case 150:
		return DZ_B150;
	case 300:
		return DZ_B300;
	case 600:
		return DZ_B600;
	case 1200:
		return DZ_B1200;
	case 1800:
		return DZ_B1800;
	case 2000:
		return DZ_B2000;
	case 2400:
		return DZ_B2400;
	case 3600:
		return DZ_B3600;
	case 4800:
		return DZ_B4800;
	case 7200:
		return DZ_B7200;
	case 9600:
		return DZ_B9600;
	default:
		return -1;
	}
}

M
Maciej W. Rozycki 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

static void dz_reset(struct dz_port *dport)
{
	struct dz_mux *mux = dport->mux;

	if (mux->initialised)
		return;

	dz_out(dport, DZ_CSR, DZ_CLR);
	while (dz_in(dport, DZ_CSR) & DZ_CLR);
	iob();

	/* Enable scanning.  */
	dz_out(dport, DZ_CSR, DZ_MSE);

	mux->initialised = 1;
}

A
Alan Cox 已提交
564 565
static void dz_set_termios(struct uart_port *uport, struct ktermios *termios,
			   struct ktermios *old_termios)
L
Linus Torvalds 已提交
566
{
567
	struct dz_port *dport = to_dport(uport);
L
Linus Torvalds 已提交
568 569
	unsigned long flags;
	unsigned int cflag, baud;
570
	int bflag;
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

	cflag = dport->port.line;

	switch (termios->c_cflag & CSIZE) {
	case CS5:
		cflag |= DZ_CS5;
		break;
	case CS6:
		cflag |= DZ_CS6;
		break;
	case CS7:
		cflag |= DZ_CS7;
		break;
	case CS8:
	default:
		cflag |= DZ_CS8;
	}

	if (termios->c_cflag & CSTOPB)
		cflag |= DZ_CSTOPB;
	if (termios->c_cflag & PARENB)
		cflag |= DZ_PARENB;
	if (termios->c_cflag & PARODD)
		cflag |= DZ_PARODD;

	baud = uart_get_baud_rate(uport, termios, old_termios, 50, 9600);
597 598 599 600 601 602 603 604 605
	bflag = dz_encode_baud_rate(baud);
	if (bflag < 0)	{			/* Try to keep unchanged.  */
		baud = uart_get_baud_rate(uport, old_termios, NULL, 50, 9600);
		bflag = dz_encode_baud_rate(baud);
		if (bflag < 0)	{		/* Resort to 9600.  */
			baud = 9600;
			bflag = DZ_B9600;
		}
		tty_termios_encode_baud_rate(termios, baud, baud);
L
Linus Torvalds 已提交
606
	}
607
	cflag |= bflag;
L
Linus Torvalds 已提交
608 609 610 611 612 613

	if (termios->c_cflag & CREAD)
		cflag |= DZ_RXENAB;

	spin_lock_irqsave(&dport->port.lock, flags);

614 615 616
	uart_update_timeout(uport, termios->c_cflag, baud);

	dz_out(dport, DZ_LPR, cflag);
L
Linus Torvalds 已提交
617 618 619 620 621 622
	dport->cflag = cflag;

	/* setup accept flag */
	dport->port.read_status_mask = DZ_OERR;
	if (termios->c_iflag & INPCK)
		dport->port.read_status_mask |= DZ_FERR | DZ_PERR;
P
Peter Hurley 已提交
623
	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
624
		dport->port.read_status_mask |= DZ_BREAK;
L
Linus Torvalds 已提交
625 626 627

	/* characters to ignore */
	uport->ignore_status_mask = 0;
628 629
	if ((termios->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
		dport->port.ignore_status_mask |= DZ_OERR;
L
Linus Torvalds 已提交
630 631
	if (termios->c_iflag & IGNPAR)
		dport->port.ignore_status_mask |= DZ_FERR | DZ_PERR;
632 633
	if (termios->c_iflag & IGNBRK)
		dport->port.ignore_status_mask |= DZ_BREAK;
L
Linus Torvalds 已提交
634 635 636 637

	spin_unlock_irqrestore(&dport->port.lock, flags);
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
/*
 * Hack alert!
 * Required solely so that the initial PROM-based console
 * works undisturbed in parallel with this one.
 */
static void dz_pm(struct uart_port *uport, unsigned int state,
		  unsigned int oldstate)
{
	struct dz_port *dport = to_dport(uport);
	unsigned long flags;

	spin_lock_irqsave(&dport->port.lock, flags);
	if (state < 3)
		dz_start_tx(&dport->port);
	else
		dz_stop_tx(&dport->port);
	spin_unlock_irqrestore(&dport->port.lock, flags);
}


M
Maciej W. Rozycki 已提交
658
static const char *dz_type(struct uart_port *uport)
L
Linus Torvalds 已提交
659 660 661 662
{
	return "DZ";
}

M
Maciej W. Rozycki 已提交
663
static void dz_release_port(struct uart_port *uport)
L
Linus Torvalds 已提交
664
{
M
Maciej W. Rozycki 已提交
665 666 667 668 669 670 671 672 673
	struct dz_mux *mux = to_dport(uport)->mux;
	int map_guard;

	iounmap(uport->membase);
	uport->membase = NULL;

	map_guard = atomic_add_return(-1, &mux->map_guard);
	if (!map_guard)
		release_mem_region(uport->mapbase, dec_kn_slot_size);
L
Linus Torvalds 已提交
674 675
}

M
Maciej W. Rozycki 已提交
676
static int dz_map_port(struct uart_port *uport)
L
Linus Torvalds 已提交
677
{
M
Maciej W. Rozycki 已提交
678 679 680 681 682 683 684
	if (!uport->membase)
		uport->membase = ioremap_nocache(uport->mapbase,
						 dec_kn_slot_size);
	if (!uport->membase) {
		printk(KERN_ERR "dz: Cannot map MMIO\n");
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
685 686 687
	return 0;
}

M
Maciej W. Rozycki 已提交
688
static int dz_request_port(struct uart_port *uport)
L
Linus Torvalds 已提交
689
{
M
Maciej W. Rozycki 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	struct dz_mux *mux = to_dport(uport)->mux;
	int map_guard;
	int ret;

	map_guard = atomic_add_return(1, &mux->map_guard);
	if (map_guard == 1) {
		if (!request_mem_region(uport->mapbase, dec_kn_slot_size,
					"dz")) {
			atomic_add(-1, &mux->map_guard);
			printk(KERN_ERR
			       "dz: Unable to reserve MMIO resource\n");
			return -EBUSY;
		}
	}
	ret = dz_map_port(uport);
	if (ret) {
		map_guard = atomic_add_return(-1, &mux->map_guard);
		if (!map_guard)
			release_mem_region(uport->mapbase, dec_kn_slot_size);
		return ret;
	}
	return 0;
}

static void dz_config_port(struct uart_port *uport, int flags)
{
	struct dz_port *dport = to_dport(uport);

	if (flags & UART_CONFIG_TYPE) {
		if (dz_request_port(uport))
			return;

		uport->type = PORT_DZ;

		dz_reset(dport);
	}
L
Linus Torvalds 已提交
726 727 728
}

/*
M
Maciej W. Rozycki 已提交
729
 * Verify the new serial_struct (for TIOCSSERIAL).
L
Linus Torvalds 已提交
730
 */
M
Maciej W. Rozycki 已提交
731
static int dz_verify_port(struct uart_port *uport, struct serial_struct *ser)
L
Linus Torvalds 已提交
732 733
{
	int ret = 0;
M
Maciej W. Rozycki 已提交
734

L
Linus Torvalds 已提交
735 736
	if (ser->type != PORT_UNKNOWN && ser->type != PORT_DZ)
		ret = -EINVAL;
M
Maciej W. Rozycki 已提交
737
	if (ser->irq != uport->irq)
L
Linus Torvalds 已提交
738 739 740 741
		ret = -EINVAL;
	return ret;
}

742
static const struct uart_ops dz_ops = {
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750 751 752
	.tx_empty	= dz_tx_empty,
	.get_mctrl	= dz_get_mctrl,
	.set_mctrl	= dz_set_mctrl,
	.stop_tx	= dz_stop_tx,
	.start_tx	= dz_start_tx,
	.stop_rx	= dz_stop_rx,
	.break_ctl	= dz_break_ctl,
	.startup	= dz_startup,
	.shutdown	= dz_shutdown,
	.set_termios	= dz_set_termios,
753
	.pm		= dz_pm,
L
Linus Torvalds 已提交
754 755 756 757 758 759 760 761 762 763 764
	.type		= dz_type,
	.release_port	= dz_release_port,
	.request_port	= dz_request_port,
	.config_port	= dz_config_port,
	.verify_port	= dz_verify_port,
};

static void __init dz_init_ports(void)
{
	static int first = 1;
	unsigned long base;
M
Maciej W. Rozycki 已提交
765
	int line;
L
Linus Torvalds 已提交
766 767 768 769 770

	if (!first)
		return;
	first = 0;

M
Maciej W. Rozycki 已提交
771 772
	if (mips_machtype == MACH_DS23100 || mips_machtype == MACH_DS5100)
		base = dec_kn_slot_base + KN01_DZ11;
L
Linus Torvalds 已提交
773
	else
M
Maciej W. Rozycki 已提交
774
		base = dec_kn_slot_base + KN02_DZ11;
L
Linus Torvalds 已提交
775

M
Maciej W. Rozycki 已提交
776 777 778
	for (line = 0; line < DZ_NB_PORT; line++) {
		struct dz_port *dport = &dz_mux.dport[line];
		struct uart_port *uport = &dport->port;
L
Linus Torvalds 已提交
779

M
Maciej W. Rozycki 已提交
780 781 782 783 784 785 786 787 788 789
		dport->mux	= &dz_mux;

		uport->irq	= dec_interrupt[DEC_IRQ_DZ11];
		uport->fifosize	= 1;
		uport->iotype	= UPIO_MEM;
		uport->flags	= UPF_BOOT_AUTOCONF;
		uport->ops	= &dz_ops;
		uport->line	= line;
		uport->mapbase	= base;
	}
L
Linus Torvalds 已提交
790 791 792
}

#ifdef CONFIG_SERIAL_DZ_CONSOLE
793 794 795 796 797 798 799 800 801 802 803 804 805 806
/*
 * -------------------------------------------------------------------
 * dz_console_putchar() -- transmit a character
 *
 * Polled transmission.  This is tricky.  We need to mask transmit
 * interrupts so that they do not interfere, enable the transmitter
 * for the line requested and then wait till the transmit scanner
 * requests data for this line.  But it may request data for another
 * line first, in which case we have to disable its transmitter and
 * repeat waiting till our line pops up.  Only then the character may
 * be transmitted.  Finally, the state of the transmitter mask is
 * restored.  Welcome to the world of PDP-11!
 * -------------------------------------------------------------------
 */
807
static void dz_console_putchar(struct uart_port *uport, int ch)
L
Linus Torvalds 已提交
808
{
809
	struct dz_port *dport = to_dport(uport);
L
Linus Torvalds 已提交
810
	unsigned long flags;
811 812
	unsigned short csr, tcr, trdy, mask;
	int loops = 10000;
L
Linus Torvalds 已提交
813 814

	spin_lock_irqsave(&dport->port.lock, flags);
815 816 817 818 819 820 821 822
	csr = dz_in(dport, DZ_CSR);
	dz_out(dport, DZ_CSR, csr & ~DZ_TIE);
	tcr = dz_in(dport, DZ_TCR);
	tcr |= 1 << dport->port.line;
	mask = tcr;
	dz_out(dport, DZ_TCR, mask);
	iob();
	spin_unlock_irqrestore(&dport->port.lock, flags);
L
Linus Torvalds 已提交
823

824
	do {
825 826 827 828 829 830 831 832 833 834
		trdy = dz_in(dport, DZ_CSR);
		if (!(trdy & DZ_TRDY))
			continue;
		trdy = (trdy & DZ_TLINE) >> 8;
		if (trdy == dport->port.line)
			break;
		mask &= ~(1 << trdy);
		dz_out(dport, DZ_TCR, mask);
		iob();
		udelay(2);
835
	} while (--loops);
L
Linus Torvalds 已提交
836

837 838
	if (loops)				/* Cannot send otherwise. */
		dz_out(dport, DZ_TDR, ch);
L
Linus Torvalds 已提交
839

840 841
	dz_out(dport, DZ_TCR, tcr);
	dz_out(dport, DZ_CSR, csr);
L
Linus Torvalds 已提交
842
}
843

844
/*
L
Linus Torvalds 已提交
845 846 847 848 849
 * -------------------------------------------------------------------
 * dz_console_print ()
 *
 * dz_console_print is registered for printk.
 * The console must be locked when we get here.
850
 * -------------------------------------------------------------------
L
Linus Torvalds 已提交
851
 */
852
static void dz_console_print(struct console *co,
L
Linus Torvalds 已提交
853 854 855
			     const char *str,
			     unsigned int count)
{
M
Maciej W. Rozycki 已提交
856
	struct dz_port *dport = &dz_mux.dport[co->index];
L
Linus Torvalds 已提交
857 858 859
#ifdef DEBUG_DZ
	prom_printf((char *) str);
#endif
860
	uart_console_write(&dport->port, str, count, dz_console_putchar);
L
Linus Torvalds 已提交
861 862 863 864
}

static int __init dz_console_setup(struct console *co, char *options)
{
M
Maciej W. Rozycki 已提交
865 866
	struct dz_port *dport = &dz_mux.dport[co->index];
	struct uart_port *uport = &dport->port;
L
Linus Torvalds 已提交
867 868 869 870
	int baud = 9600;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';
M
Maciej W. Rozycki 已提交
871
	int ret;
L
Linus Torvalds 已提交
872

M
Maciej W. Rozycki 已提交
873 874 875
	ret = dz_map_port(uport);
	if (ret)
		return ret;
L
Linus Torvalds 已提交
876

877 878
	spin_lock_init(&dport->port.lock);	/* For dz_pm().  */

L
Linus Torvalds 已提交
879
	dz_reset(dport);
880
	dz_pm(uport, 0, -1);
L
Linus Torvalds 已提交
881

M
Maciej W. Rozycki 已提交
882 883 884
	if (options)
		uart_parse_options(options, &baud, &parity, &bits, &flow);

885
	return uart_set_options(&dport->port, co, baud, parity, bits, flow);
L
Linus Torvalds 已提交
886 887
}

888
static struct uart_driver dz_reg;
889
static struct console dz_console = {
L
Linus Torvalds 已提交
890 891 892 893
	.name	= "ttyS",
	.write	= dz_console_print,
	.device	= uart_console_device,
	.setup	= dz_console_setup,
894 895 896
	.flags	= CON_PRINTBUFFER,
	.index	= -1,
	.data	= &dz_reg,
L
Linus Torvalds 已提交
897 898
};

899
static int __init dz_serial_console_init(void)
L
Linus Torvalds 已提交
900
{
901 902
	if (!IOASIC) {
		dz_init_ports();
903
		register_console(&dz_console);
904 905 906
		return 0;
	} else
		return -ENXIO;
L
Linus Torvalds 已提交
907 908
}

909 910
console_initcall(dz_serial_console_init);

911
#define SERIAL_DZ_CONSOLE	&dz_console
L
Linus Torvalds 已提交
912 913 914 915 916 917 918
#else
#define SERIAL_DZ_CONSOLE	NULL
#endif /* CONFIG_SERIAL_DZ_CONSOLE */

static struct uart_driver dz_reg = {
	.owner			= THIS_MODULE,
	.driver_name		= "serial",
919
	.dev_name		= "ttyS",
L
Linus Torvalds 已提交
920 921 922 923 924 925
	.major			= TTY_MAJOR,
	.minor			= 64,
	.nr			= DZ_NB_PORT,
	.cons			= SERIAL_DZ_CONSOLE,
};

926
static int __init dz_init(void)
L
Linus Torvalds 已提交
927 928 929
{
	int ret, i;

930 931 932
	if (IOASIC)
		return -ENXIO;

L
Linus Torvalds 已提交
933 934 935 936 937
	printk("%s%s\n", dz_name, dz_version);

	dz_init_ports();

	ret = uart_register_driver(&dz_reg);
M
Maciej W. Rozycki 已提交
938 939
	if (ret)
		return ret;
L
Linus Torvalds 已提交
940 941

	for (i = 0; i < DZ_NB_PORT; i++)
M
Maciej W. Rozycki 已提交
942
		uart_add_one_port(&dz_reg, &dz_mux.dport[i].port);
943

M
Maciej W. Rozycki 已提交
944
	return 0;
L
Linus Torvalds 已提交
945 946
}

947
module_init(dz_init);