sunsu.c 37.8 KB
Newer Older
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
6
7
8
9
10
11
12
13
14
 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
 *
 * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
 * Copyright (C) 1998-1999  Pete Zaitcev   (zaitcev@yahoo.com)
 *
 * This is mainly a variation of 8250.c, credits go to authors mentioned
 * therein.  In fact this driver should be merged into the generic 8250.c
 * infrastructure perhaps using a 8250_sparc.c module.
 *
 * Fixed to use tty_get_baud_rate().
 *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
 *
 * Converted to new 2.5.x UART layer.
15
 *   David S. Miller (davem@davemloft.net), 2002-Jul-29
Linus Torvalds's avatar
Linus Torvalds committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/circ_buf.h>
#include <linux/serial.h>
#include <linux/sysrq.h>
#include <linux/console.h>
32
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
34
35
36
37
38
#ifdef CONFIG_SERIO
#include <linux/serio.h>
#endif
#include <linux/serial_reg.h>
#include <linux/init.h>
#include <linux/delay.h>
39
#include <linux/of_device.h>
Linus Torvalds's avatar
Linus Torvalds committed
40
41
42

#include <asm/io.h>
#include <asm/irq.h>
43
#include <asm/prom.h>
44
#include <asm/setup.h>
Linus Torvalds's avatar
Linus Torvalds committed
45
46
47
48
49
50

#if defined(CONFIG_SERIAL_SUNSU_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

#include <linux/serial_core.h>
51
#include <linux/sunserialcore.h>
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
56
57
58
59
60

/* We are on a NS PC87303 clocked with 24.0 MHz, which results
 * in a UART clock of 1.8462 MHz.
 */
#define SU_BASE_BAUD	(1846200 / 16)

enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
static char *su_typev[] = { "su(???)", "su(mouse)", "su(kbd)", "su(serial)" };

61
62
63
64
65
66
struct serial_uart_config {
	char	*name;
	int	dfl_xmit_fifo_size;
	int	flags;
};

Linus Torvalds's avatar
Linus Torvalds committed
67
68
69
/*
 * Here we define the default xmit fifo size used for each type of UART.
 */
70
static const struct serial_uart_config uart_config[] = {
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
	{ "unknown",	1,	0 },
	{ "8250",	1,	0 },
	{ "16450",	1,	0 },
	{ "16550",	1,	0 },
	{ "16550A",	16,	UART_CLEAR_FIFO | UART_USE_FIFO },
	{ "Cirrus",	1, 	0 },
	{ "ST16650",	1,	UART_CLEAR_FIFO | UART_STARTECH },
	{ "ST16650V2",	32,	UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
	{ "TI16750",	64,	UART_CLEAR_FIFO | UART_USE_FIFO },
	{ "Startech",	1,	0 },
	{ "16C950/954",	128,	UART_CLEAR_FIFO | UART_USE_FIFO },
	{ "ST16654",	64,	UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
	{ "XR16850",	128,	UART_CLEAR_FIFO | UART_USE_FIFO | UART_STARTECH },
	{ "RSA",	2048,	UART_CLEAR_FIFO | UART_USE_FIFO }
};

struct uart_sunsu_port {
	struct uart_port	port;
	unsigned char		acr;
	unsigned char		ier;
	unsigned short		rev;
	unsigned char		lcr;
	unsigned int		lsr_break_flag;
	unsigned int		cflag;

	/* Probing information.  */
	enum su_type		su_type;
	unsigned int		type_probed;	/* XXX Stupid */
99
	unsigned long		reg_size;
Linus Torvalds's avatar
Linus Torvalds committed
100
101

#ifdef CONFIG_SERIO
102
	struct serio		serio;
Linus Torvalds's avatar
Linus Torvalds committed
103
104
105
106
	int			serio_open;
#endif
};

Adrian Bunk's avatar
Adrian Bunk committed
107
static unsigned int serial_in(struct uart_sunsu_port *up, int offset)
Linus Torvalds's avatar
Linus Torvalds committed
108
109
110
111
{
	offset <<= up->port.regshift;

	switch (up->port.iotype) {
112
	case UPIO_HUB6:
Linus Torvalds's avatar
Linus Torvalds committed
113
114
115
		outb(up->port.hub6 - 1 + offset, up->port.iobase);
		return inb(up->port.iobase + 1);

116
	case UPIO_MEM:
Linus Torvalds's avatar
Linus Torvalds committed
117
118
119
120
121
122
123
		return readb(up->port.membase + offset);

	default:
		return inb(up->port.iobase + offset);
	}
}

Adrian Bunk's avatar
Adrian Bunk committed
124
static void serial_out(struct uart_sunsu_port *up, int offset, int value)
Linus Torvalds's avatar
Linus Torvalds committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
#ifndef CONFIG_SPARC64
	/*
	 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
	 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
	 * gate outputs a logical one. Since we use level triggered interrupts
	 * we have lockup and watchdog reset. We cannot mask IRQ because
	 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
	 * This problem is similar to what Alpha people suffer, see serial.c.
	 */
	if (offset == UART_MCR)
		value |= UART_MCR_OUT2;
#endif
	offset <<= up->port.regshift;

	switch (up->port.iotype) {
141
	case UPIO_HUB6:
Linus Torvalds's avatar
Linus Torvalds committed
142
143
144
145
		outb(up->port.hub6 - 1 + offset, up->port.iobase);
		outb(value, up->port.iobase + 1);
		break;

146
	case UPIO_MEM:
Linus Torvalds's avatar
Linus Torvalds committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
		writeb(value, up->port.membase + offset);
		break;

	default:
		outb(value, up->port.iobase + offset);
	}
}

/*
 * We used to support using pause I/O for certain machines.  We
 * haven't supported this for a while, but just in case it's badly
 * needed for certain old 386 machines, I've left these #define's
 * in....
 */
#define serial_inp(up, offset)		serial_in(up, offset)
#define serial_outp(up, offset, value)	serial_out(up, offset, value)


/*
 * For the 16C950
 */
static void serial_icr_write(struct uart_sunsu_port *up, int offset, int value)
{
	serial_out(up, UART_SCR, offset);
	serial_out(up, UART_ICR, value);
}

#if 0 /* Unused currently */
static unsigned int serial_icr_read(struct uart_sunsu_port *up, int offset)
{
	unsigned int value;

	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
	serial_out(up, UART_SCR, offset);
	value = serial_in(up, UART_ICR);
	serial_icr_write(up, UART_ACR, up->acr);

	return value;
}
#endif

#ifdef CONFIG_SERIAL_8250_RSA
/*
 * Attempts to turn on the RSA FIFO.  Returns zero on failure.
 * We set the port uart clock rate if we succeed.
 */
static int __enable_rsa(struct uart_sunsu_port *up)
{
	unsigned char mode;
	int result;

	mode = serial_inp(up, UART_RSA_MSR);
	result = mode & UART_RSA_MSR_FIFO;

	if (!result) {
		serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
		mode = serial_inp(up, UART_RSA_MSR);
		result = mode & UART_RSA_MSR_FIFO;
	}

	if (result)
		up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;

	return result;
}

static void enable_rsa(struct uart_sunsu_port *up)
{
	if (up->port.type == PORT_RSA) {
		if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
			spin_lock_irq(&up->port.lock);
			__enable_rsa(up);
			spin_unlock_irq(&up->port.lock);
		}
		if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
			serial_outp(up, UART_RSA_FRR, 0);
	}
}

/*
 * Attempts to turn off the RSA FIFO.  Returns zero on failure.
 * It is unknown why interrupts were disabled in here.  However,
 * the caller is expected to preserve this behaviour by grabbing
 * the spinlock before calling this function.
 */
static void disable_rsa(struct uart_sunsu_port *up)
{
	unsigned char mode;
	int result;

	if (up->port.type == PORT_RSA &&
	    up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
		spin_lock_irq(&up->port.lock);

		mode = serial_inp(up, UART_RSA_MSR);
		result = !(mode & UART_RSA_MSR_FIFO);

		if (!result) {
			serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
			mode = serial_inp(up, UART_RSA_MSR);
			result = !(mode & UART_RSA_MSR_FIFO);
		}

		if (result)
			up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
		spin_unlock_irq(&up->port.lock);
	}
}
#endif /* CONFIG_SERIAL_8250_RSA */

257
258
259
260
261
262
263
264
265
static inline void __stop_tx(struct uart_sunsu_port *p)
{
	if (p->ier & UART_IER_THRI) {
		p->ier &= ~UART_IER_THRI;
		serial_out(p, UART_IER, p->ier);
	}
}

static void sunsu_stop_tx(struct uart_port *port)
Linus Torvalds's avatar
Linus Torvalds committed
266
267
268
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;

269
270
	__stop_tx(up);

Al Viro's avatar
Al Viro committed
271
272
273
274
	/*
	 * We really want to stop the transmitter from sending.
	 */
	if (up->port.type == PORT_16C950) {
Linus Torvalds's avatar
Linus Torvalds committed
275
276
277
278
279
		up->acr |= UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
}

280
static void sunsu_start_tx(struct uart_port *port)
Linus Torvalds's avatar
Linus Torvalds committed
281
282
283
284
285
286
287
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;

	if (!(up->ier & UART_IER_THRI)) {
		up->ier |= UART_IER_THRI;
		serial_out(up, UART_IER, up->ier);
	}
Al Viro's avatar
Al Viro committed
288

Linus Torvalds's avatar
Linus Torvalds committed
289
	/*
Al Viro's avatar
Al Viro committed
290
	 * Re-enable the transmitter if we disabled it.
Linus Torvalds's avatar
Linus Torvalds committed
291
	 */
Al Viro's avatar
Al Viro committed
292
	if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
Linus Torvalds's avatar
Linus Torvalds committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
		up->acr &= ~UART_ACR_TXDIS;
		serial_icr_write(up, UART_ACR, up->acr);
	}
}

static void sunsu_stop_rx(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;

	up->ier &= ~UART_IER_RLSI;
	up->port.read_status_mask &= ~UART_LSR_DR;
	serial_out(up, UART_IER, up->ier);
}

static void sunsu_enable_ms(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned long flags;

	spin_lock_irqsave(&up->port.lock, flags);
	up->ier |= UART_IER_MSI;
	serial_out(up, UART_IER, up->ier);
	spin_unlock_irqrestore(&up->port.lock, flags);
}

Jiri Slaby's avatar
Jiri Slaby committed
318
static void
319
receive_chars(struct uart_sunsu_port *up, unsigned char *status)
Linus Torvalds's avatar
Linus Torvalds committed
320
{
Jiri Slaby's avatar
Jiri Slaby committed
321
	struct tty_port *port = &up->port.state->port;
Alan Cox's avatar
Alan Cox committed
322
	unsigned char ch, flag;
Linus Torvalds's avatar
Linus Torvalds committed
323
324
325
326
327
	int max_count = 256;
	int saw_console_brk = 0;

	do {
		ch = serial_inp(up, UART_RX);
Alan Cox's avatar
Alan Cox committed
328
		flag = TTY_NORMAL;
Linus Torvalds's avatar
Linus Torvalds committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
		up->port.icount.rx++;

		if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
				       UART_LSR_FE | UART_LSR_OE))) {
			/*
			 * For statistics only
			 */
			if (*status & UART_LSR_BI) {
				*status &= ~(UART_LSR_FE | UART_LSR_PE);
				up->port.icount.brk++;
				if (up->port.cons != NULL &&
				    up->port.line == up->port.cons->index)
					saw_console_brk = 1;
				/*
				 * We do the SysRQ and SAK checking
				 * here because otherwise the break
				 * may get masked by ignore_status_mask
				 * or read_status_mask.
				 */
				if (uart_handle_break(&up->port))
					goto ignore_char;
			} else if (*status & UART_LSR_PE)
				up->port.icount.parity++;
			else if (*status & UART_LSR_FE)
				up->port.icount.frame++;
			if (*status & UART_LSR_OE)
				up->port.icount.overrun++;

			/*
			 * Mask off conditions which should be ingored.
			 */
			*status &= up->port.read_status_mask;

			if (up->port.cons != NULL &&
			    up->port.line == up->port.cons->index) {
				/* Recover the break flag from console xmit */
				*status |= up->lsr_break_flag;
				up->lsr_break_flag = 0;
			}

			if (*status & UART_LSR_BI) {
Alan Cox's avatar
Alan Cox committed
370
				flag = TTY_BREAK;
Linus Torvalds's avatar
Linus Torvalds committed
371
			} else if (*status & UART_LSR_PE)
Alan Cox's avatar
Alan Cox committed
372
				flag = TTY_PARITY;
Linus Torvalds's avatar
Linus Torvalds committed
373
			else if (*status & UART_LSR_FE)
Alan Cox's avatar
Alan Cox committed
374
				flag = TTY_FRAME;
Linus Torvalds's avatar
Linus Torvalds committed
375
		}
376
		if (uart_handle_sysrq_char(&up->port, ch))
Linus Torvalds's avatar
Linus Torvalds committed
377
			goto ignore_char;
Alan Cox's avatar
Alan Cox committed
378
		if ((*status & up->port.ignore_status_mask) == 0)
Jiri Slaby's avatar
Jiri Slaby committed
379
			tty_insert_flip_char(port, ch, flag);
Alan Cox's avatar
Alan Cox committed
380
		if (*status & UART_LSR_OE)
Linus Torvalds's avatar
Linus Torvalds committed
381
382
383
384
385
			/*
			 * Overrun is special, since it's reported
			 * immediately, and doesn't affect the current
			 * character.
			 */
Jiri Slaby's avatar
Jiri Slaby committed
386
			 tty_insert_flip_char(port, 0, TTY_OVERRUN);
Linus Torvalds's avatar
Linus Torvalds committed
387
388
389
390
391
392
393
394
	ignore_char:
		*status = serial_inp(up, UART_LSR);
	} while ((*status & UART_LSR_DR) && (max_count-- > 0));

	if (saw_console_brk)
		sun_do_break();
}

Adrian Bunk's avatar
Adrian Bunk committed
395
static void transmit_chars(struct uart_sunsu_port *up)
Linus Torvalds's avatar
Linus Torvalds committed
396
{
Alan Cox's avatar
Alan Cox committed
397
	struct circ_buf *xmit = &up->port.state->xmit;
Linus Torvalds's avatar
Linus Torvalds committed
398
399
400
401
402
403
404
405
	int count;

	if (up->port.x_char) {
		serial_outp(up, UART_TX, up->port.x_char);
		up->port.icount.tx++;
		up->port.x_char = 0;
		return;
	}
406
407
408
409
410
411
	if (uart_tx_stopped(&up->port)) {
		sunsu_stop_tx(&up->port);
		return;
	}
	if (uart_circ_empty(xmit)) {
		__stop_tx(up);
Linus Torvalds's avatar
Linus Torvalds committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
		return;
	}

	count = up->port.fifosize;
	do {
		serial_out(up, UART_TX, xmit->buf[xmit->tail]);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		up->port.icount.tx++;
		if (uart_circ_empty(xmit))
			break;
	} while (--count > 0);

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(&up->port);

	if (uart_circ_empty(xmit))
428
		__stop_tx(up);
Linus Torvalds's avatar
Linus Torvalds committed
429
430
}

Adrian Bunk's avatar
Adrian Bunk committed
431
static void check_modem_status(struct uart_sunsu_port *up)
Linus Torvalds's avatar
Linus Torvalds committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
	int status;

	status = serial_in(up, UART_MSR);

	if ((status & UART_MSR_ANY_DELTA) == 0)
		return;

	if (status & UART_MSR_TERI)
		up->port.icount.rng++;
	if (status & UART_MSR_DDSR)
		up->port.icount.dsr++;
	if (status & UART_MSR_DDCD)
		uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
	if (status & UART_MSR_DCTS)
		uart_handle_cts_change(&up->port, status & UART_MSR_CTS);

449
	wake_up_interruptible(&up->port.state->port.delta_msr_wait);
Linus Torvalds's avatar
Linus Torvalds committed
450
451
}

452
static irqreturn_t sunsu_serial_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
453
454
455
456
457
458
459
460
461
462
{
	struct uart_sunsu_port *up = dev_id;
	unsigned long flags;
	unsigned char status;

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

	do {
		status = serial_inp(up, UART_LSR);
		if (status & UART_LSR_DR)
Jiri Slaby's avatar
Jiri Slaby committed
463
			receive_chars(up, &status);
Linus Torvalds's avatar
Linus Torvalds committed
464
465
466
467
468
469
		check_modem_status(up);
		if (status & UART_LSR_THRE)
			transmit_chars(up);

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

Jiri Slaby's avatar
Jiri Slaby committed
470
		tty_flip_buffer_push(&up->port.state->port);
Linus Torvalds's avatar
Linus Torvalds committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

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

	} while (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT));

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

	return IRQ_HANDLED;
}

/* Separate interrupt handling path for keyboard/mouse ports.  */

static void
sunsu_change_speed(struct uart_port *port, unsigned int cflag,
		   unsigned int iflag, unsigned int quot);

static void sunsu_change_mouse_baud(struct uart_sunsu_port *up)
{
	unsigned int cur_cflag = up->cflag;
	int quot, new_baud;

	up->cflag &= ~CBAUD;
	up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);

	quot = up->port.uartclk / (16 * new_baud);

	sunsu_change_speed(&up->port, up->cflag, 0, quot);
}

500
static void receive_kbd_ms_chars(struct uart_sunsu_port *up, int is_break)
Linus Torvalds's avatar
Linus Torvalds committed
501
502
503
504
505
506
507
{
	do {
		unsigned char ch = serial_inp(up, UART_RX);

		/* Stop-A is handled by drivers/char/keyboard.c now. */
		if (up->su_type == SU_PORT_KBD) {
#ifdef CONFIG_SERIO
508
			serio_interrupt(&up->serio, ch, 0);
Linus Torvalds's avatar
Linus Torvalds committed
509
510
511
512
513
514
515
516
517
518
519
520
521
#endif
		} else if (up->su_type == SU_PORT_MS) {
			int ret = suncore_mouse_baud_detection(ch, is_break);

			switch (ret) {
			case 2:
				sunsu_change_mouse_baud(up);
				/* fallthru */
			case 1:
				break;

			case 0:
#ifdef CONFIG_SERIO
522
				serio_interrupt(&up->serio, ch, 0);
Linus Torvalds's avatar
Linus Torvalds committed
523
524
525
526
527
528
529
#endif
				break;
			};
		}
	} while (serial_in(up, UART_LSR) & UART_LSR_DR);
}

530
static irqreturn_t sunsu_kbd_ms_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
531
532
533
534
535
536
537
{
	struct uart_sunsu_port *up = dev_id;

	if (!(serial_in(up, UART_IIR) & UART_IIR_NO_INT)) {
		unsigned char status = serial_inp(up, UART_LSR);

		if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
538
			receive_kbd_ms_chars(up, (status & UART_LSR_BI) != 0);
Linus Torvalds's avatar
Linus Torvalds committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
	}

	return IRQ_HANDLED;
}

static unsigned int sunsu_tx_empty(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned long flags;
	unsigned int ret;

	spin_lock_irqsave(&up->port.lock, flags);
	ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
	spin_unlock_irqrestore(&up->port.lock, flags);

	return ret;
}

static unsigned int sunsu_get_mctrl(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned char status;
	unsigned int ret;

	status = serial_in(up, UART_MSR);

	ret = 0;
	if (status & UART_MSR_DCD)
		ret |= TIOCM_CAR;
	if (status & UART_MSR_RI)
		ret |= TIOCM_RNG;
	if (status & UART_MSR_DSR)
		ret |= TIOCM_DSR;
	if (status & UART_MSR_CTS)
		ret |= TIOCM_CTS;
	return ret;
}

static void sunsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned char mcr = 0;

	if (mctrl & TIOCM_RTS)
		mcr |= UART_MCR_RTS;
	if (mctrl & TIOCM_DTR)
		mcr |= UART_MCR_DTR;
	if (mctrl & TIOCM_OUT1)
		mcr |= UART_MCR_OUT1;
	if (mctrl & TIOCM_OUT2)
		mcr |= UART_MCR_OUT2;
	if (mctrl & TIOCM_LOOP)
		mcr |= UART_MCR_LOOP;

	serial_out(up, UART_MCR, mcr);
}

static void sunsu_break_ctl(struct uart_port *port, int break_state)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned long flags;

	spin_lock_irqsave(&up->port.lock, flags);
	if (break_state == -1)
		up->lcr |= UART_LCR_SBC;
	else
		up->lcr &= ~UART_LCR_SBC;
	serial_out(up, UART_LCR, up->lcr);
	spin_unlock_irqrestore(&up->port.lock, flags);
}

static int sunsu_startup(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned long flags;
	int retval;

	if (up->port.type == PORT_16C950) {
		/* Wake up and initialize UART */
		up->acr = 0;
		serial_outp(up, UART_LCR, 0xBF);
		serial_outp(up, UART_EFR, UART_EFR_ECB);
		serial_outp(up, UART_IER, 0);
		serial_outp(up, UART_LCR, 0);
		serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
		serial_outp(up, UART_LCR, 0xBF);
		serial_outp(up, UART_EFR, UART_EFR_ECB);
		serial_outp(up, UART_LCR, 0);
	}

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * If this is an RSA port, see if we can kick it up to the
	 * higher speed clock.
	 */
	enable_rsa(up);
#endif

	/*
	 * Clear the FIFO buffers and disable them.
Alexey Dobriyan's avatar
Alexey Dobriyan committed
639
	 * (they will be reenabled in set_termios())
Linus Torvalds's avatar
Linus Torvalds committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
	 */
	if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
		serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
		serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
				UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
		serial_outp(up, UART_FCR, 0);
	}

	/*
	 * Clear the interrupt registers.
	 */
	(void) serial_inp(up, UART_LSR);
	(void) serial_inp(up, UART_RX);
	(void) serial_inp(up, UART_IIR);
	(void) serial_inp(up, UART_MSR);

	/*
	 * At this point, there's no way the LSR could still be 0xff;
	 * if it is, then bail out, because there's likely no UART
	 * here.
	 */
661
	if (!(up->port.flags & UPF_BUGGY_UART) &&
Linus Torvalds's avatar
Linus Torvalds committed
662
663
664
665
666
667
668
	    (serial_inp(up, UART_LSR) == 0xff)) {
		printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
		return -ENODEV;
	}

	if (up->su_type != SU_PORT_PORT) {
		retval = request_irq(up->port.irq, sunsu_kbd_ms_interrupt,
669
				     IRQF_SHARED, su_typev[up->su_type], up);
Linus Torvalds's avatar
Linus Torvalds committed
670
671
	} else {
		retval = request_irq(up->port.irq, sunsu_serial_interrupt,
672
				     IRQF_SHARED, su_typev[up->su_type], up);
Linus Torvalds's avatar
Linus Torvalds committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
	}
	if (retval) {
		printk("su: Cannot register IRQ %d\n", up->port.irq);
		return retval;
	}

	/*
	 * Now, initialize the UART
	 */
	serial_outp(up, UART_LCR, UART_LCR_WLEN8);

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

	up->port.mctrl |= TIOCM_OUT2;

	sunsu_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);

	/*
	 * Finally, enable interrupts.  Note: Modem status interrupts
	 * are set via set_termios(), which will be occurring imminently
	 * anyway, so we don't enable them here.
	 */
	up->ier = UART_IER_RLSI | UART_IER_RDI;
	serial_outp(up, UART_IER, up->ier);

699
	if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds's avatar
Linus Torvalds committed
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
726
727
728
729
730
731
		unsigned int icp;
		/*
		 * Enable interrupts on the AST Fourport board
		 */
		icp = (up->port.iobase & 0xfe0) | 0x01f;
		outb_p(0x80, icp);
		(void) inb_p(icp);
	}

	/*
	 * And clear the interrupt registers again for luck.
	 */
	(void) serial_inp(up, UART_LSR);
	(void) serial_inp(up, UART_RX);
	(void) serial_inp(up, UART_IIR);
	(void) serial_inp(up, UART_MSR);

	return 0;
}

static void sunsu_shutdown(struct uart_port *port)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned long flags;

	/*
	 * Disable interrupts from this port
	 */
	up->ier = 0;
	serial_outp(up, UART_IER, 0);

	spin_lock_irqsave(&up->port.lock, flags);
732
	if (up->port.flags & UPF_FOURPORT) {
Linus Torvalds's avatar
Linus Torvalds committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
		/* reset interrupts on the AST Fourport board */
		inb((up->port.iobase & 0xfe0) | 0x1f);
		up->port.mctrl |= TIOCM_OUT1;
	} else
		up->port.mctrl &= ~TIOCM_OUT2;

	sunsu_set_mctrl(&up->port, up->port.mctrl);
	spin_unlock_irqrestore(&up->port.lock, flags);

	/*
	 * Disable break condition and FIFOs
	 */
	serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
				  UART_FCR_CLEAR_RCVR |
				  UART_FCR_CLEAR_XMIT);
	serial_outp(up, UART_FCR, 0);

#ifdef CONFIG_SERIAL_8250_RSA
	/*
	 * Reset the RSA board back to 115kbps compat mode.
	 */
	disable_rsa(up);
#endif

	/*
	 * Read data port to reset things.
	 */
	(void) serial_in(up, UART_RX);

	free_irq(up->port.irq, up);
}

static void
sunsu_change_speed(struct uart_port *port, unsigned int cflag,
		   unsigned int iflag, unsigned int quot)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;
	unsigned char cval, fcr = 0;
	unsigned long flags;

	switch (cflag & CSIZE) {
	case CS5:
		cval = 0x00;
		break;
	case CS6:
		cval = 0x01;
		break;
	case CS7:
		cval = 0x02;
		break;
	default:
	case CS8:
		cval = 0x03;
		break;
	}

	if (cflag & CSTOPB)
		cval |= 0x04;
	if (cflag & PARENB)
		cval |= UART_LCR_PARITY;
	if (!(cflag & PARODD))
		cval |= UART_LCR_EPAR;
#ifdef CMSPAR
	if (cflag & CMSPAR)
		cval |= UART_LCR_SPAR;
#endif

	/*
	 * Work around a bug in the Oxford Semiconductor 952 rev B
	 * chip which causes it to seriously miscalculate baud rates
	 * when DLL is 0.
	 */
	if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
	    up->rev == 0x5201)
		quot ++;

	if (uart_config[up->port.type].flags & UART_USE_FIFO) {
		if ((up->port.uartclk / quot) < (2400 * 16))
			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
#ifdef CONFIG_SERIAL_8250_RSA
		else if (up->port.type == PORT_RSA)
			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_14;
#endif
		else
			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
	}
	if (up->port.type == PORT_16750)
		fcr |= UART_FCR7_64BYTE;

	/*
	 * Ok, we're now changing the port state.  Do it with
	 * interrupts disabled.
	 */
	spin_lock_irqsave(&up->port.lock, flags);

	/*
	 * Update the per-port timeout.
	 */
	uart_update_timeout(port, cflag, (port->uartclk / (16 * quot)));

	up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
	if (iflag & INPCK)
		up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
	if (iflag & (BRKINT | PARMRK))
		up->port.read_status_mask |= UART_LSR_BI;

	/*
	 * Characteres to ignore
	 */
	up->port.ignore_status_mask = 0;
	if (iflag & IGNPAR)
		up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
	if (iflag & IGNBRK) {
		up->port.ignore_status_mask |= UART_LSR_BI;
		/*
		 * If we're ignoring parity and break indicators,
		 * ignore overruns too (for real raw support).
		 */
		if (iflag & IGNPAR)
			up->port.ignore_status_mask |= UART_LSR_OE;
	}

	/*
	 * ignore all characters if CREAD is not set
	 */
	if ((cflag & CREAD) == 0)
		up->port.ignore_status_mask |= UART_LSR_DR;

	/*
	 * CTS flow control flag and modem status interrupts
	 */
	up->ier &= ~UART_IER_MSI;
	if (UART_ENABLE_MS(&up->port, cflag))
		up->ier |= UART_IER_MSI;

	serial_out(up, UART_IER, up->ier);

	if (uart_config[up->port.type].flags & UART_STARTECH) {
		serial_outp(up, UART_LCR, 0xBF);
		serial_outp(up, UART_EFR, cflag & CRTSCTS ? UART_EFR_CTS :0);
	}
	serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
	serial_outp(up, UART_DLL, quot & 0xff);		/* LS of divisor */
	serial_outp(up, UART_DLM, quot >> 8);		/* MS of divisor */
	if (up->port.type == PORT_16750)
		serial_outp(up, UART_FCR, fcr);		/* set fcr */
	serial_outp(up, UART_LCR, cval);		/* reset DLAB */
	up->lcr = cval;					/* Save LCR */
	if (up->port.type != PORT_16750) {
		if (fcr & UART_FCR_ENABLE_FIFO) {
			/* emulated UARTs (Lucent Venus 167x) need two steps */
			serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
		}
		serial_outp(up, UART_FCR, fcr);		/* set fcr */
	}

	up->cflag = cflag;

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

static void
Alan Cox's avatar
Alan Cox committed
896
897
sunsu_set_termios(struct uart_port *port, struct ktermios *termios,
		  struct ktermios *old)
Linus Torvalds's avatar
Linus Torvalds committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
{
	unsigned int baud, quot;

	/*
	 * Ask the core to calculate the divisor for us.
	 */
	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 
	quot = uart_get_divisor(port, baud);

	sunsu_change_speed(port, termios->c_cflag, termios->c_iflag, quot);
}

static void sunsu_release_port(struct uart_port *port)
{
}

static int sunsu_request_port(struct uart_port *port)
{
	return 0;
}

static void sunsu_config_port(struct uart_port *port, int flags)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *) port;

	if (flags & UART_CONFIG_TYPE) {
		/*
		 * We are supposed to call autoconfig here, but this requires
		 * splitting all the OBP probing crap from the UART probing.
		 * We'll do it when we kill sunsu.c altogether.
		 */
		port->type = up->type_probed;	/* XXX */
	}
}

static int
sunsu_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	return -EINVAL;
}

static const char *
sunsu_type(struct uart_port *port)
{
	int type = port->type;

	if (type >= ARRAY_SIZE(uart_config))
		type = 0;
	return uart_config[type].name;
}

static struct uart_ops sunsu_pops = {
	.tx_empty	= sunsu_tx_empty,
	.set_mctrl	= sunsu_set_mctrl,
	.get_mctrl	= sunsu_get_mctrl,
	.stop_tx	= sunsu_stop_tx,
	.start_tx	= sunsu_start_tx,
	.stop_rx	= sunsu_stop_rx,
	.enable_ms	= sunsu_enable_ms,
	.break_ctl	= sunsu_break_ctl,
	.startup	= sunsu_startup,
	.shutdown	= sunsu_shutdown,
	.set_termios	= sunsu_set_termios,
	.type		= sunsu_type,
	.release_port	= sunsu_release_port,
	.request_port	= sunsu_request_port,
	.config_port	= sunsu_config_port,
	.verify_port	= sunsu_verify_port,
};

#define UART_NR	4

static struct uart_sunsu_port sunsu_ports[UART_NR];
971
static int nr_inst; /* Number of already registered ports */
Linus Torvalds's avatar
Linus Torvalds committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031

#ifdef CONFIG_SERIO

static DEFINE_SPINLOCK(sunsu_serio_lock);

static int sunsu_serio_write(struct serio *serio, unsigned char ch)
{
	struct uart_sunsu_port *up = serio->port_data;
	unsigned long flags;
	int lsr;

	spin_lock_irqsave(&sunsu_serio_lock, flags);

	do {
		lsr = serial_in(up, UART_LSR);
	} while (!(lsr & UART_LSR_THRE));

	/* Send the character out. */
	serial_out(up, UART_TX, ch);

	spin_unlock_irqrestore(&sunsu_serio_lock, flags);

	return 0;
}

static int sunsu_serio_open(struct serio *serio)
{
	struct uart_sunsu_port *up = serio->port_data;
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&sunsu_serio_lock, flags);
	if (!up->serio_open) {
		up->serio_open = 1;
		ret = 0;
	} else
		ret = -EBUSY;
	spin_unlock_irqrestore(&sunsu_serio_lock, flags);

	return ret;
}

static void sunsu_serio_close(struct serio *serio)
{
	struct uart_sunsu_port *up = serio->port_data;
	unsigned long flags;

	spin_lock_irqsave(&sunsu_serio_lock, flags);
	up->serio_open = 0;
	spin_unlock_irqrestore(&sunsu_serio_lock, flags);
}

#endif /* CONFIG_SERIO */

static void sunsu_autoconfig(struct uart_sunsu_port *up)
{
	unsigned char status1, status2, scratch, scratch2, scratch3;
	unsigned char save_lcr, save_mcr;
	unsigned long flags;

1032
	if (up->su_type == SU_PORT_NONE)
Linus Torvalds's avatar
Linus Torvalds committed
1033
1034
1035
		return;

	up->type_probed = PORT_UNKNOWN;
1036
	up->port.iotype = UPIO_MEM;
Linus Torvalds's avatar
Linus Torvalds committed
1037
1038
1039

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

1040
	if (!(up->port.flags & UPF_BUGGY_UART)) {
Linus Torvalds's avatar
Linus Torvalds committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
		/*
		 * Do a simple existence test first; if we fail this, there's
		 * no point trying anything else.
		 *
		 * 0x80 is used as a nonsense port to prevent against false
		 * positives due to ISA bus float.  The assumption is that
		 * 0x80 is a non-existent port; which should be safe since
		 * include/asm/io.h also makes this assumption.
		 */
		scratch = serial_inp(up, UART_IER);
		serial_outp(up, UART_IER, 0);
#ifdef __i386__
		outb(0xff, 0x080);
#endif
		scratch2 = serial_inp(up, UART_IER);
		serial_outp(up, UART_IER, 0x0f);
#ifdef __i386__
		outb(0, 0x080);
#endif
		scratch3 = serial_inp(up, UART_IER);
		serial_outp(up, UART_IER, scratch);
		if (scratch2 != 0 || scratch3 != 0x0F)
			goto out;	/* We failed; there's nothing here */
	}

	save_mcr = serial_in(up, UART_MCR);
	save_lcr = serial_in(up, UART_LCR);

	/* 
	 * Check to see if a UART is really there.  Certain broken
	 * internal modems based on the Rockwell chipset fail this
	 * test, because they apparently don't implement the loopback
	 * test mode.  So this test is skipped on the COM 1 through
	 * COM 4 ports.  This *should* be safe, since no board
	 * manufacturer would be stupid enough to design a board
	 * that conflicts with COM 1-4 --- we hope!
	 */
1078
	if (!(up->port.flags & UPF_SKIP_TEST)) {
Linus Torvalds's avatar
Linus Torvalds committed
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
		serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
		status1 = serial_inp(up, UART_MSR) & 0xF0;
		serial_outp(up, UART_MCR, save_mcr);
		if (status1 != 0x90)
			goto out;	/* We failed loopback test */
	}
	serial_outp(up, UART_LCR, 0xBF);	/* set up for StarTech test */
	serial_outp(up, UART_EFR, 0);		/* EFR is the same as FCR */
	serial_outp(up, UART_LCR, 0);
	serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	scratch = serial_in(up, UART_IIR) >> 6;
	switch (scratch) {
		case 0:
			up->port.type = PORT_16450;
			break;
		case 1:
			up->port.type = PORT_UNKNOWN;
			break;
		case 2:
			up->port.type = PORT_16550;
			break;
		case 3:
			up->port.type = PORT_16550A;
			break;
	}
	if (up->port.type == PORT_16550A) {
		/* Check for Startech UART's */
		serial_outp(up, UART_LCR, UART_LCR_DLAB);
		if (serial_in(up, UART_EFR) == 0) {
			up->port.type = PORT_16650;
		} else {
			serial_outp(up, UART_LCR, 0xBF);
			if (serial_in(up, UART_EFR) == 0)
				up->port.type = PORT_16650V2;
		}
	}
	if (up->port.type == PORT_16550A) {
		/* Check for TI 16750 */
		serial_outp(up, UART_LCR, save_lcr | UART_LCR_DLAB);
		serial_outp(up, UART_FCR,
			    UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
		scratch = serial_in(up, UART_IIR) >> 5;
		if (scratch == 7) {
			/*
			 * If this is a 16750, and not a cheap UART
			 * clone, then it should only go into 64 byte
			 * mode if the UART_FCR7_64BYTE bit was set
			 * while UART_LCR_DLAB was latched.
			 */
 			serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
			serial_outp(up, UART_LCR, 0);
			serial_outp(up, UART_FCR,
				    UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
			scratch = serial_in(up, UART_IIR) >> 5;
			if (scratch == 6)
				up->port.type = PORT_16750;
		}
		serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
	}
	serial_outp(up, UART_LCR, save_lcr);
	if (up->port.type == PORT_16450) {
		scratch = serial_in(up, UART_SCR);
		serial_outp(up, UART_SCR, 0xa5);
		status1 = serial_in(up, UART_SCR);
		serial_outp(up, UART_SCR, 0x5a);
		status2 = serial_in(up, UART_SCR);
		serial_outp(up, UART_SCR, scratch);

		if ((status1 != 0xa5) || (status2 != 0x5a))
			up->port.type = PORT_8250;
	}

	up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;

	if (up->port.type == PORT_UNKNOWN)
		goto out;
	up->type_probed = up->port.type;	/* XXX */

	/*
	 * Reset the UART.
	 */
#ifdef CONFIG_SERIAL_8250_RSA
	if (up->port.type == PORT_RSA)
		serial_outp(up, UART_RSA_FRR, 0);
#endif
	serial_outp(up, UART_MCR, save_mcr);
	serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
				     UART_FCR_CLEAR_RCVR |
				     UART_FCR_CLEAR_XMIT));
	serial_outp(up, UART_FCR, 0);
	(void)serial_in(up, UART_RX);
	serial_outp(up, UART_IER, 0);

out:
	spin_unlock_irqrestore(&up->port.lock, flags);
}

static struct uart_driver sunsu_reg = {
	.owner			= THIS_MODULE,
1178
	.driver_name		= "sunsu",
Linus Torvalds's avatar
Linus Torvalds committed
1179
1180
1181
1182
	.dev_name		= "ttyS",
	.major			= TTY_MAJOR,
};

Bill Pemberton's avatar
Bill Pemberton committed
1183
static int sunsu_kbd_ms_init(struct uart_sunsu_port *up)
Linus Torvalds's avatar
Linus Torvalds committed
1184
{
1185
	int quot, baud;
Linus Torvalds's avatar
Linus Torvalds committed
1186
1187
1188
1189
#ifdef CONFIG_SERIO
	struct serio *serio;
#endif

1190
	if (up->su_type == SU_PORT_KBD) {
Linus Torvalds's avatar
Linus Torvalds committed
1191
		up->cflag = B1200 | CS8 | CLOCAL | CREAD;
1192
1193
		baud = 1200;
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1194
		up->cflag = B4800 | CS8 | CLOCAL | CREAD;
1195
1196
1197
		baud = 4800;
	}
	quot = up->port.uartclk / (16 * baud);
Linus Torvalds's avatar
Linus Torvalds committed
1198
1199
1200

	sunsu_autoconfig(up);
	if (up->port.type == PORT_UNKNOWN)
1201
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1202

1203
	printk("%s: %s port at %llx, irq %u\n",
1204
	       up->port.dev->of_node->full_name,
1205
	       (up->su_type == SU_PORT_KBD) ? "Keyboard" : "Mouse",
1206
1207
	       (unsigned long long) up->port.mapbase,
	       up->port.irq);
1208

Linus Torvalds's avatar
Linus Torvalds committed
1209
#ifdef CONFIG_SERIO
1210
1211
	serio = &up->serio;
	serio->port_data = up;
Linus Torvalds's avatar
Linus Torvalds committed
1212

1213
1214
1215
1216
	serio->id.type = SERIO_RS232;
	if (up->su_type == SU_PORT_KBD) {
		serio->id.proto = SERIO_SUNKBD;
		strlcpy(serio->name, "sukbd", sizeof(serio->name));
Linus Torvalds's avatar
Linus Torvalds committed
1217
	} else {
1218
1219
1220
		serio->id.proto = SERIO_SUN;
		serio->id.extra = 1;
		strlcpy(serio->name, "sums", sizeof(serio->name));
Linus Torvalds's avatar
Linus Torvalds committed
1221
	}
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
	strlcpy(serio->phys,
		(!(up->port.line & 1) ? "su/serio0" : "su/serio1"),
		sizeof(serio->phys));

	serio->write = sunsu_serio_write;
	serio->open = sunsu_serio_open;
	serio->close = sunsu_serio_close;
	serio->dev.parent = up->port.dev;

	serio_register_port(serio);
Linus Torvalds's avatar
Linus Torvalds committed
1232
1233
#endif

1234
1235
	sunsu_change_speed(&up->port, up->cflag, 0, quot);

Linus Torvalds's avatar
Linus Torvalds committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
	sunsu_startup(&up->port);
	return 0;
}

/*
 * ------------------------------------------------------------
 * Serial console driver
 * ------------------------------------------------------------
 */

#ifdef CONFIG_SERIAL_SUNSU_CONSOLE

#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

/*
 *	Wait for transmitter & holding register to empty
 */
static __inline__ void wait_for_xmitr(struct uart_sunsu_port *up)
{
	unsigned int status, tmout = 10000;

	/* Wait up to 10ms for the character(s) to be sent. */
	do {
		status = serial_in(up, UART_LSR);

		if (status & UART_LSR_BI)
			up->lsr_break_flag = UART_LSR_BI;

		if (--tmout == 0)
			break;
		udelay(1);
	} while ((status & BOTH_EMPTY) != BOTH_EMPTY);

	/* Wait up to 1s for flow control if necessary */
1270
	if (up->port.flags & UPF_CONS_FLOW) {
Linus Torvalds's avatar
Linus Torvalds committed
1271
1272
1273
1274
1275
1276
1277
		tmout = 1000000;
		while (--tmout &&
		       ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
			udelay(1);
	}
}

1278
1279
1280
1281
1282
1283
1284
1285
static void sunsu_console_putchar(struct uart_port *port, int ch)
{
	struct uart_sunsu_port *up = (struct uart_sunsu_port *)port;

	wait_for_xmitr(up);
	serial_out(up, UART_TX, ch);
}

Linus Torvalds's avatar
Linus Torvalds committed
1286
1287
1288
1289
1290
1291
1292
1293
/*
 *	Print a string to the serial port trying not to disturb
 *	any possible real use of the port...
 */
static void sunsu_console_write(struct console *co, const char *s,
				unsigned int count)
{
	struct uart_sunsu_port *up = &sunsu_ports[co->index];
1294
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1295
	unsigned int ier;
1296
1297
1298
1299
1300
1301
1302
1303
1304
	int locked = 1;

	local_irq_save(flags);
	if (up->port.sysrq) {
		locked = 0;
	} else if (oops_in_progress) {
		locked = spin_trylock(&up->port.lock);
	} else
		spin_lock(&up->port.lock);
Linus Torvalds's avatar
Linus Torvalds committed
1305
1306
1307
1308
1309
1310
1311

	/*
	 *	First save the UER then disable the interrupts
	 */
	ier = serial_in(up, UART_IER);
	serial_out(up, UART_IER, 0);

1312
	uart_console_write(&up->port, s, count, sunsu_console_putchar);
Linus Torvalds's avatar
Linus Torvalds committed
1313
1314
1315
1316
1317
1318
1319

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up);
	serial_out(up, UART_IER, ier);
1320
1321
1322
1323

	if (locked)
		spin_unlock(&up->port.lock);
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1324
1325
1326
1327
1328
1329
1330
1331
}

/*
 *	Setup initial baud/bits/parity. We do two things here:
 *	- construct a cflag setting for the first su_open()
 *	- initialize the serial port
 *	Return non-zero if we didn't find a serial port.
 */
1332
static int __init sunsu_console_setup(struct console *co, char *options)
Linus Torvalds's avatar
Linus Torvalds committed
1333
{
1334
1335
	static struct ktermios dummy;
	struct ktermios termios;
Linus Torvalds's avatar
Linus Torvalds committed
1336
1337
1338
1339
1340
	struct uart_port *port;

	printk("Console: ttyS%d (SU)\n",
	       (sunsu_reg.minor - 64) + co->index);

1341
1342
	if (co->index > nr_inst)
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1343
1344
1345
1346
1347
1348
1349
	port = &sunsu_ports[co->index].port;

	/*
	 * Temporary fix.
	 */
	spin_lock_init(&port->lock);

1350
	/* Get firmware console settings.  */
1351
	sunserial_console_termios(co, port->dev->of_node);
Linus Torvalds's avatar
Linus Torvalds committed
1352

1353
1354
1355
1356
1357
1358
	memset(&termios, 0, sizeof(struct ktermios));
	termios.c_cflag = co->cflag;
	port->mctrl |= TIOCM_DTR;
	port->ops->set_termios(port, &termios, &dummy);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1359
1360
}

1361
static struct console sunsu_console = {
Linus Torvalds's avatar
Linus Torvalds committed
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
	.name	=	"ttyS",
	.write	=	sunsu_console_write,
	.device	=	uart_console_device,
	.setup	=	sunsu_console_setup,
	.flags	=	CON_PRINTBUFFER,
	.index	=	-1,
	.data	=	&sunsu_reg,
};

/*
 *	Register console.
 */

1375
static inline struct console *SUNSU_CONSOLE(void)
Linus Torvalds's avatar
Linus Torvalds committed
1376
{
1377
	return &sunsu_console;
Linus Torvalds's avatar
Linus Torvalds committed
1378
1379
}
#else
1380
#define SUNSU_CONSOLE()			(NULL)
Linus Torvalds's avatar
Linus Torvalds committed
1381
1382
1383
#define sunsu_serial_console_init()	do { } while (0)
#endif

Bill Pemberton's avatar
Bill Pemberton committed
1384
static enum su_type su_get_type(struct device_node *dp)
Linus Torvalds's avatar
Linus Torvalds committed
1385
{
1386
	struct device_node *ap = of_find_node_by_path("/aliases");
Linus Torvalds's avatar
Linus Torvalds committed
1387

1388
	if (ap) {
1389
1390
		const char *keyb = of_get_property(ap, "keyboard", NULL);
		const char *ms = of_get_property(ap, "mouse", NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1391

1392
1393
1394
1395
1396
1397
1398
1399
1400
		if (keyb) {
			if (dp == of_find_node_by_path(keyb))
				return SU_PORT_KBD;
		}
		if (ms) {
			if (dp == of_find_node_by_path(ms))
				return SU_PORT_MS;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1401

1402
1403
	return SU_PORT_PORT;
}
Linus Torvalds's avatar
Linus Torvalds committed
1404

Bill Pemberton's avatar
Bill Pemberton committed
1405
static int su_probe(struct platform_device *op)
1406
{
1407
	struct device_node *dp = op->dev.of_node;
1408
1409
	struct uart_sunsu_port *up;
	struct resource *rp;
1410
	enum su_type type;
1411
	bool ignore_line;
1412
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1413

1414
1415
	type = su_get_type(dp);
	if (type == SU_PORT_PORT) {
1416
		if (nr_inst >= UART_NR)
1417
			return -EINVAL;
1418
		up = &sunsu_ports[nr_inst];
1419
1420
1421
1422
1423
	} else {
		up = kzalloc(sizeof(*up), GFP_KERNEL);
		if (!up)
			return -ENOMEM;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1424

1425
	up->port.line = nr_inst;
Linus Torvalds's avatar
Linus Torvalds committed
1426

1427
	spin_lock_init(&up->port.lock);
Linus Torvalds's avatar
Linus Torvalds committed
1428

1429
	up->su_type = type;
1430

1431
	rp = &op->resource[0];
1432
	up->port.mapbase = rp->start;
1433
	up->reg_size = resource_size(rp);
1434
	up->port.membase = of_ioremap(rp, 0, up->reg_size, "su");
1435
1436
1437
	if (!up->port.membase) {
		if (type != SU_PORT_PORT)
			kfree(up);
1438
		return -ENOMEM;
1439
	}
1440

1441
	up->port.irq = op->archdata.irqs[0];
Linus Torvalds's avatar
Linus Torvalds committed
1442

1443
	up->port.dev = &op->dev;
Linus Torvalds's avatar
Linus Torvalds committed
1444

1445
1446
	up->port.type = PORT_UNKNOWN;
	up->port.uartclk = (SU_BASE_BAUD * 16);
Linus Torvalds's avatar
Linus Torvalds committed
1447

1448
1449
1450
	err = 0;
	if (up->su_type == SU_PORT_KBD || up->su_type == SU_PORT_MS) {
		err = sunsu_kbd_ms_init(up);
1451
		if (err) {
1452
1453
			of_iounmap(&op->resource[0],
				   up->port.membase, up->reg_size);
1454
			kfree(up);
1455
			return err;
1456
1457
		}
		dev_set_drvdata(&op->dev, up);
1458

1459
1460
		nr_inst++;

1461
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1462
1463
	}

1464
	up->port.flags |= UPF_BOOT_AUTOCONF;
Linus Torvalds's avatar
Linus Torvalds committed
1465

1466
	sunsu_autoconfig(up);
Linus Torvalds's avatar
Linus Torvalds committed
1467

1468
1469
1470
	err = -ENODEV;
	if (up->port.type == PORT_UNKNOWN)
		goto out_unmap;
Linus Torvalds's avatar
Linus Torvalds committed
1471

1472
	up->port.ops = &sunsu_pops;
Linus Torvalds's avatar
Linus Torvalds committed
1473

1474
1475
1476
1477
1478
	ignore_line = false;
	if (!strcmp(dp->name, "rsc-console") ||
	    !strcmp(dp->name, "lom-console"))
		ignore_line = true;

1479
	sunserial_console_match(SUNSU_CONSOLE(), dp,
1480
				&sunsu_reg, up->port.line,
1481
				ignore_line);
1482
1483
1484
	err = uart_add_one_port(&sunsu_reg, &up->port);
	if (err)
		goto out_unmap;
Linus Torvalds's avatar
Linus Torvalds committed
1485

1486
	dev_set_drvdata(&op->dev, up);
Linus Torvalds's avatar
Linus Torvalds committed
1487

1488
	nr_inst++;
Linus Torvalds's avatar
Linus Torvalds committed
1489

1490
1491
1492
	return 0;

out_unmap:
1493
	of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1494
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1495
1496
}

Bill Pemberton's avatar
Bill Pemberton committed
1497
static int su_remove(struct platform_device *op)
Linus Torvalds's avatar
Linus Torvalds committed
1498
{
1499
	struct uart_sunsu_port *up = dev_get_drvdata(&op->dev);
1500
	bool kbdms = false;
Linus Torvalds's avatar
Linus Torvalds committed
1501

1502
	if (up->su_type == SU_PORT_MS ||
1503
1504
1505
1506
	    up->su_type == SU_PORT_KBD)
		kbdms = true;

	if (kbdms) {
1507
1508
1509
#ifdef CONFIG_SERIO
		serio_unregister_port(&up->serio);
#endif
1510
	} else if (up->port.type != PORT_UNKNOWN)
1511
		uart_remove_one_port(&sunsu_reg, &up->port);
1512

1513
	if (up->port.membase)
1514
		of_iounmap(&op->resource[0], up->port.membase, up->reg_size);
1515

1516
1517
1518
	if (kbdms)
		kfree(up);

1519
	dev_set_drvdata(&op->dev, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1520

1521
1522
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1523

1524
static const struct of_device_id su_match[] = {
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
	{
		.name = "su",
	},
	{
		.name = "su_pnp",
	},
	{
		.name = "serial",
		.compatible = "su",
	},
1535
1536
1537
1538
	{
		.type = "serial",
		.compatible = "su",
	},
1539
1540
1541
	{},
};
MODULE_DEVICE_TABLE(of, su_match);
Linus Torvalds's avatar
Linus Torvalds committed
1542

1543
static struct platform_driver su_driver = {
1544
1545
1546
1547
1548
	.driver = {
		.name = "su",
		.owner = THIS_MODULE,
		.of_match_table = su_match,
	},
1549
	.probe		= su_probe,
1550
	.remove		= su_remove,
1551
};
Linus Torvalds's avatar
Linus Torvalds committed
1552

1553
1554
1555
1556
static int __init sunsu_init(void)
{
	struct device_node *dp;
	int err;
1557
	int num_uart = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1558

1559
1560
1561
	for_each_node_by_name(dp, "su") {
		if (su_get_type(dp) == SU_PORT_PORT)
			num_uart++;
Linus Torvalds's avatar
Linus Torvalds committed
1562
	}
1563
1564
1565
1566
1567
1568
1569
1570
1571
	for_each_node_by_name(dp, "su_pnp") {
		if (su_get_type(dp) == SU_PORT_PORT)
			num_uart++;
	}
	for_each_node_by_name(dp, "serial") {
		if (of_device_is_compatible(dp, "su")) {
			if (su_get_type(dp) == SU_PORT_PORT)
				num_uart++;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1572
	}
1573
1574
1575
1576
1577
1578
	for_each_node_by_type(dp, "serial") {
		if (of_device_is_compatible(dp, "su")) {
			if (su_get_type(dp) == SU_PORT_PORT)
				num_uart++;
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1579

1580
	if (num_uart) {
1581
		err = sunserial_register_minors(&sunsu_reg, num_uart);
1582
1583
1584
		if (err)
			return err;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1585

1586
	err = platform_driver_register(&su_driver);
1587
	if (err && num_uart)
1588
		sunserial_unregister_minors(&sunsu_reg, num_uart);
Linus Torvalds's avatar
Linus Torvalds committed
1589

1590
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1591
1592
1593
1594
}

static void __exit sunsu_exit(void)
{
1595
	platform_driver_unregister(&su_driver);
1596
1597
	if (sunsu_reg.nr)
		sunserial_unregister_minors(&sunsu_reg, sunsu_reg.nr);
Linus Torvalds's avatar
Linus Torvalds committed
1598
1599
}

1600
module_init(sunsu_init);
Linus Torvalds's avatar
Linus Torvalds committed
1601
module_exit(sunsu_exit);
1602
1603
1604
1605

MODULE_AUTHOR("Eddie C. Dost, Peter Zaitcev, and David S. Miller");
MODULE_DESCRIPTION("Sun SU serial port driver");
MODULE_VERSION("2.0");
Andrew Morton's avatar
Andrew Morton committed
1606
MODULE_LICENSE("GPL");