uart_dev_ns8250.c 23.4 KB
Newer Older
1
/*-
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * Copyright (c) 2003 Marcel Moolenaar
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

27
28
#include "opt_platform.h"

29
30
31
32
33
34
35
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
36
37
#include <sys/kernel.h>
#include <sys/sysctl.h>
38
39
#include <machine/bus.h>

40
41
42
43
44
45
#ifdef FDT
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

46
47
#include <dev/uart/uart.h>
#include <dev/uart/uart_cpu.h>
48
49
50
#ifdef FDT
#include <dev/uart/uart_cpu_fdt.h>
#endif
51
#include <dev/uart/uart_bus.h>
52
#include <dev/uart/uart_dev_ns8250.h>
53
54

#include <dev/ic/ns16550.h>
55
56
57
58
59

#include "uart_if.h"

#define	DEFAULT_RCLK	1843200

60
static int broken_txfifo = 0;
61
SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RWTUN,
62
63
	&broken_txfifo, 0, "UART FIFO has QEMU emulation bug");

64
65
66
67
68
69
70
/*
 * Clear pending interrupts. THRE is cleared by reading IIR. Data
 * that may have been received gets lost here.
 */
static void
ns8250_clrint(struct uart_bas *bas)
{
71
	uint8_t iir, lsr;
72
73
74
75

	iir = uart_getreg(bas, REG_IIR);
	while ((iir & IIR_NOPEND) == 0) {
		iir &= IIR_IMASK;
76
77
78
79
80
		if (iir == IIR_RLS) {
			lsr = uart_getreg(bas, REG_LSR);
			if (lsr & (LSR_BI|LSR_FE|LSR_PE))
				(void)uart_getreg(bas, REG_DATA);
		} else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
			(void)uart_getreg(bas, REG_DATA);
		else if (iir == IIR_MLSC)
			(void)uart_getreg(bas, REG_MSR);
		uart_barrier(bas);
		iir = uart_getreg(bas, REG_IIR);
	}
}

static int
ns8250_delay(struct uart_bas *bas)
{
	int divisor;
	u_char lcr;

	lcr = uart_getreg(bas, REG_LCR);
	uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
	uart_barrier(bas);
98
	divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
99
100
101
102
103
	uart_barrier(bas);
	uart_setreg(bas, REG_LCR, lcr);
	uart_barrier(bas);

	/* 1/10th the time to transmit 1 character (estimate). */
104
105
106
	if (divisor <= 134)
		return (16000000 * divisor / bas->rclk);
	return (16000 * divisor / (bas->rclk / 1000));
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
}

static int
ns8250_divisor(int rclk, int baudrate)
{
	int actual_baud, divisor;
	int error;

	if (baudrate == 0)
		return (0);

	divisor = (rclk / (baudrate << 3) + 1) >> 1;
	if (divisor == 0 || divisor >= 65536)
		return (0);
	actual_baud = rclk / (divisor << 4);

	/* 10 times error in percent: */
	error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;

	/* 3.0% maximum error tolerance: */
	if (error < -30 || error > 30)
		return (0);

	return (divisor);
}

static int
ns8250_drain(struct uart_bas *bas, int what)
{
	int delay, limit;

	delay = ns8250_delay(bas);

	if (what & UART_DRAIN_TRANSMITTER) {
		/*
		 * Pick an arbitrary high limit to avoid getting stuck in
		 * an infinite loop when the hardware is broken. Make the
		 * limit high enough to handle large FIFOs.
		 */
		limit = 10*1024;
		while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
			DELAY(delay);
		if (limit == 0) {
			/* printf("ns8250: transmitter appears stuck... "); */
			return (EIO);
		}
	}

	if (what & UART_DRAIN_RECEIVER) {
		/*
		 * Pick an arbitrary high limit to avoid getting stuck in
		 * an infinite loop when the hardware is broken. Make the
		 * limit high enough to handle large FIFOs and integrated
		 * UARTs. The HP rx2600 for example has 3 UARTs on the
		 * management board that tend to get a lot of data send
		 * to it when the UART is first activated.
		 */
		limit=10*4096;
		while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
			(void)uart_getreg(bas, REG_DATA);
			uart_barrier(bas);
			DELAY(delay << 2);
		}
		if (limit == 0) {
			/* printf("ns8250: receiver appears broken... "); */
			return (EIO);
		}
	}

	return (0);
}

/*
 * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
 * drained. WARNING: this function clobbers the FIFO setting!
 */
static void
ns8250_flush(struct uart_bas *bas, int what)
{
	uint8_t fcr;

	fcr = FCR_ENABLE;
	if (what & UART_FLUSH_TRANSMITTER)
		fcr |= FCR_XMT_RST;
	if (what & UART_FLUSH_RECEIVER)
		fcr |= FCR_RCV_RST;
	uart_setreg(bas, REG_FCR, fcr);
	uart_barrier(bas);
}

static int
ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
    int parity)
{
	int divisor;
	uint8_t lcr;

	lcr = 0;
	if (databits >= 8)
		lcr |= LCR_8BITS;
	else if (databits == 7)
		lcr |= LCR_7BITS;
	else if (databits == 6)
		lcr |= LCR_6BITS;
	else
		lcr |= LCR_5BITS;
	if (stopbits > 1)
		lcr |= LCR_STOPB;
	lcr |= parity << 3;

	/* Set baudrate. */
	if (baudrate > 0) {
		divisor = ns8250_divisor(bas->rclk, baudrate);
		if (divisor == 0)
			return (EINVAL);
222
223
		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
		uart_barrier(bas);
224
225
		uart_setreg(bas, REG_DLL, divisor & 0xff);
		uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
		uart_barrier(bas);
	}

	/* Set LCR and clear DLAB. */
	uart_setreg(bas, REG_LCR, lcr);
	uart_barrier(bas);
	return (0);
}

/*
 * Low-level UART interface.
 */
static int ns8250_probe(struct uart_bas *bas);
static void ns8250_init(struct uart_bas *bas, int, int, int, int);
static void ns8250_term(struct uart_bas *bas);
static void ns8250_putc(struct uart_bas *bas, int);
242
static int ns8250_rxready(struct uart_bas *bas);
243
static int ns8250_getc(struct uart_bas *bas, struct mtx *);
244

245
struct uart_ops uart_ns8250_ops = {
246
247
248
249
	.probe = ns8250_probe,
	.init = ns8250_init,
	.term = ns8250_term,
	.putc = ns8250_putc,
250
	.rxready = ns8250_rxready,
251
252
253
254
255
256
	.getc = ns8250_getc,
};

static int
ns8250_probe(struct uart_bas *bas)
{
257
	u_char val;
258
259
260
261
262

	/* Check known 0 bits that don't depend on DLAB. */
	val = uart_getreg(bas, REG_IIR);
	if (val & 0x30)
		return (ENXIO);
263
264
265
266
267
268
	/*
	 * Bit 6 of the MCR (= 0x40) appears to be 1 for the Sun1699
	 * chip, but otherwise doesn't seem to have a function. In
	 * other words, uart(4) works regardless. Ignore that bit so
	 * the probe succeeds.
	 */
269
	val = uart_getreg(bas, REG_MCR);
270
	if (val & 0xa0)
271
272
273
274
275
276
277
278
279
		return (ENXIO);

	return (0);
}

static void
ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
    int parity)
{
280
	u_char	ier;
281
282
283
284
285
286

	if (bas->rclk == 0)
		bas->rclk = DEFAULT_RCLK;
	ns8250_param(bas, baudrate, databits, stopbits, parity);

	/* Disable all interrupt sources. */
287
288
289
290
291
292
	/*
	 * We use 0xe0 instead of 0xf0 as the mask because the XScale PXA
	 * UARTs split the receive time-out interrupt bit out separately as
	 * 0x10.  This gets handled by ier_mask and ier_rxbits below.
	 */
	ier = uart_getreg(bas, REG_IER) & 0xe0;
293
	uart_setreg(bas, REG_IER, ier);
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	uart_barrier(bas);

	/* Disable the FIFO (if present). */
	uart_setreg(bas, REG_FCR, 0);
	uart_barrier(bas);

	/* Set RTS & DTR. */
	uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
	uart_barrier(bas);

	ns8250_clrint(bas);
}

static void
ns8250_term(struct uart_bas *bas)
{

	/* Clear RTS & DTR. */
	uart_setreg(bas, REG_MCR, MCR_IE);
	uart_barrier(bas);
}

static void
ns8250_putc(struct uart_bas *bas, int c)
{
319
	int limit;
320

321
	limit = 250000;
322
	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
323
		DELAY(4);
324
	uart_setreg(bas, REG_DATA, c);
325
	uart_barrier(bas);
326
	limit = 250000;
327
	while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
328
		DELAY(4);
329
330
331
}

static int
332
ns8250_rxready(struct uart_bas *bas)
333
334
{

335
	return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
336
337
338
}

static int
339
ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
340
{
341
	int c;
342
343

	uart_lock(hwmtx);
344

345
346
	while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
		uart_unlock(hwmtx);
347
		DELAY(4);
348
349
350
351
352
353
354
355
		uart_lock(hwmtx);
	}

	c = uart_getreg(bas, REG_DATA);

	uart_unlock(hwmtx);

	return (c);
356
357
358
359
360
361
362
363
364
365
366
367
368
369
}

static kobj_method_t ns8250_methods[] = {
	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
	KOBJMETHOD(uart_param,		ns8250_bus_param),
	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
370
371
	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
372
373
374
375
	{ 0, 0 }
};

struct uart_class uart_ns8250_class = {
376
	"ns8250",
377
378
	ns8250_methods,
	sizeof(struct ns8250_softc),
379
	.uc_ops = &uart_ns8250_ops,
380
	.uc_range = 8,
381
382
	.uc_rclk = DEFAULT_RCLK,
	.uc_rshift = 0
383
384
};

385
386
387
388
389
390
391
392
#ifdef FDT
static struct ofw_compat_data compat_data[] = {
	{"ns16550",		(uintptr_t)&uart_ns8250_class},
	{NULL,			(uintptr_t)NULL},
};
UART_FDT_CLASS_AND_DEVICE(compat_data);
#endif

393
394
395
396
397
398
399
#define	SIGCHG(c, i, s, d)				\
	if (c) {					\
		i |= (i & s) ? s : s | d;		\
	} else {					\
		i = (i & s) ? (i & ~s) | d : i;		\
	}

400
int
401
402
403
404
ns8250_bus_attach(struct uart_softc *sc)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
405
	unsigned int ivar;
406
407
408
409
410
411
412
413
414
415
416
417
418
#ifdef FDT
	phandle_t node;
	pcell_t cell;
#endif

	ns8250->busy_detect = 0;

#ifdef FDT
	/* 
	 * Check whether uart requires to read USR reg when IIR_BUSY and 
	 * has broken txfifo. 
	 */
	node = ofw_bus_get_node(sc->sc_dev);
419
420
421
422
	if ((OF_getencprop(node, "busy-detect", &cell, sizeof(cell))) > 0)
		ns8250->busy_detect = cell ? 1 : 0;
	if ((OF_getencprop(node, "broken-txfifo", &cell, sizeof(cell))) > 0)
		broken_txfifo =  cell ? 1 : 0;
423
#endif
424
425
426
427

	bas = &sc->sc_bas;

	ns8250->mcr = uart_getreg(bas, REG_MCR);
428
429
430
431
432
433
434
435
436
437
438
439
440
	ns8250->fcr = FCR_ENABLE;
	if (!resource_int_value("uart", device_get_unit(sc->sc_dev), "flags",
	    &ivar)) {
		if (UART_FLAGS_FCR_RX_LOW(ivar)) 
			ns8250->fcr |= FCR_RX_LOW;
		else if (UART_FLAGS_FCR_RX_MEDL(ivar)) 
			ns8250->fcr |= FCR_RX_MEDL;
		else if (UART_FLAGS_FCR_RX_HIGH(ivar)) 
			ns8250->fcr |= FCR_RX_HIGH;
		else
			ns8250->fcr |= FCR_RX_MEDH;
	} else 
		ns8250->fcr |= FCR_RX_MEDH;
441
442
443
444
445
446
447
448
449
450
451
452
453
	
	/* Get IER mask */
	ivar = 0xf0;
	resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_mask",
	    &ivar);
	ns8250->ier_mask = (uint8_t)(ivar & 0xff);
	
	/* Get IER RX interrupt bits */
	ivar = IER_EMSC | IER_ERLS | IER_ERXRDY;
	resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_rxbits",
	    &ivar);
	ns8250->ier_rxbits = (uint8_t)(ivar & 0xff);
	
454
455
456
457
458
	uart_setreg(bas, REG_FCR, ns8250->fcr);
	uart_barrier(bas);
	ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);

	if (ns8250->mcr & MCR_DTR)
459
		sc->sc_hwsig |= SER_DTR;
460
	if (ns8250->mcr & MCR_RTS)
461
		sc->sc_hwsig |= SER_RTS;
462
463
464
	ns8250_bus_getsig(sc);

	ns8250_clrint(bas);
465
466
	ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
	ns8250->ier |= ns8250->ier_rxbits;
467
468
	uart_setreg(bas, REG_IER, ns8250->ier);
	uart_barrier(bas);
469
470
471
472
473
474
475
476
477
478
479
480

	/*
	 * Timing of the H/W access was changed with r253161 of uart_core.c
	 * It has been observed that an ITE IT8513E would signal a break
	 * condition with pretty much every character it received, unless
	 * it had enough time to settle between ns8250_bus_attach() and
	 * ns8250_bus_ipend() -- which it accidentally had before r253161.
	 * It's not understood why the UART chip behaves this way and it
	 * could very well be that the DELAY make the H/W work in the same
	 * accidental manner as before. More analysis is warranted, but
	 * at least now we fixed a known regression.
	 */
481
	DELAY(200);
482
483
484
	return (0);
}

485
int
486
487
ns8250_bus_detach(struct uart_softc *sc)
{
488
	struct ns8250_softc *ns8250;
489
	struct uart_bas *bas;
490
	u_char ier;
491

492
	ns8250 = (struct ns8250_softc *)sc;
493
	bas = &sc->sc_bas;
494
	ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
495
	uart_setreg(bas, REG_IER, ier);
496
497
498
499
500
	uart_barrier(bas);
	ns8250_clrint(bas);
	return (0);
}

501
int
502
503
504
505
ns8250_bus_flush(struct uart_softc *sc, int what)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
506
	int error;
507
508

	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
509
	uart_lock(sc->sc_hwmtx);
510
	if (sc->sc_rxfifosz > 1) {
511
512
513
		ns8250_flush(bas, what);
		uart_setreg(bas, REG_FCR, ns8250->fcr);
		uart_barrier(bas);
514
515
516
		error = 0;
	} else
		error = ns8250_drain(bas, what);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
517
	uart_unlock(sc->sc_hwmtx);
518
	return (error);
519
520
}

521
int
522
523
524
525
526
527
528
529
ns8250_bus_getsig(struct uart_softc *sc)
{
	uint32_t new, old, sig;
	uint8_t msr;

	do {
		old = sc->sc_hwsig;
		sig = old;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
530
		uart_lock(sc->sc_hwmtx);
531
		msr = uart_getreg(&sc->sc_bas, REG_MSR);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
532
		uart_unlock(sc->sc_hwmtx);
533
534
535
536
		SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
		SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
		SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
		SIGCHG(msr & MSR_RI,  sig, SER_RI,  SER_DRI);
537
		new = sig & ~SER_MASK_DELTA;
538
539
540
541
	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
	return (sig);
}

542
int
543
544
545
ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
{
	struct uart_bas *bas;
546
	int baudrate, divisor, error;
547
	uint8_t efr, lcr;
548
549

	bas = &sc->sc_bas;
550
	error = 0;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
551
	uart_lock(sc->sc_hwmtx);
552
553
554
555
556
557
558
559
560
561
	switch (request) {
	case UART_IOCTL_BREAK:
		lcr = uart_getreg(bas, REG_LCR);
		if (data)
			lcr |= LCR_SBREAK;
		else
			lcr &= ~LCR_SBREAK;
		uart_setreg(bas, REG_LCR, lcr);
		uart_barrier(bas);
		break;
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
	case UART_IOCTL_IFLOW:
		lcr = uart_getreg(bas, REG_LCR);
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, 0xbf);
		uart_barrier(bas);
		efr = uart_getreg(bas, REG_EFR);
		if (data)
			efr |= EFR_RTS;
		else
			efr &= ~EFR_RTS;
		uart_setreg(bas, REG_EFR, efr);
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, lcr);
		uart_barrier(bas);
		break;
	case UART_IOCTL_OFLOW:
		lcr = uart_getreg(bas, REG_LCR);
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, 0xbf);
		uart_barrier(bas);
		efr = uart_getreg(bas, REG_EFR);
		if (data)
			efr |= EFR_CTS;
		else
			efr &= ~EFR_CTS;
		uart_setreg(bas, REG_EFR, efr);
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, lcr);
		uart_barrier(bas);
		break;
592
593
594
595
	case UART_IOCTL_BAUD:
		lcr = uart_getreg(bas, REG_LCR);
		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
		uart_barrier(bas);
596
597
		divisor = uart_getreg(bas, REG_DLL) |
		    (uart_getreg(bas, REG_DLH) << 8);
598
599
600
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, lcr);
		uart_barrier(bas);
601
602
603
604
605
		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
		if (baudrate > 0)
			*(int*)data = baudrate;
		else
			error = ENXIO;
606
		break;
607
	default:
608
609
		error = EINVAL;
		break;
610
	}
Marcel Moolenaar's avatar
Marcel Moolenaar committed
611
	uart_unlock(sc->sc_hwmtx);
612
	return (error);
613
614
}

615
int
616
617
618
ns8250_bus_ipend(struct uart_softc *sc)
{
	struct uart_bas *bas;
619
	struct ns8250_softc *ns8250;
620
621
622
	int ipend;
	uint8_t iir, lsr;

623
	ns8250 = (struct ns8250_softc *)sc;
624
	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
625
	uart_lock(sc->sc_hwmtx);
626
	iir = uart_getreg(bas, REG_IIR);
627
628
629
630
631
632

	if (ns8250->busy_detect && (iir & IIR_BUSY) == IIR_BUSY) {
		(void)uart_getreg(bas, DW_REG_USR);
		uart_unlock(sc->sc_hwmtx);
		return (0);
	}
633
	if (iir & IIR_NOPEND) {
Marcel Moolenaar's avatar
Marcel Moolenaar committed
634
		uart_unlock(sc->sc_hwmtx);
635
		return (0);
636
	}
637
638
639
640
	ipend = 0;
	if (iir & IIR_RXRDY) {
		lsr = uart_getreg(bas, REG_LSR);
		if (lsr & LSR_OE)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
641
			ipend |= SER_INT_OVERRUN;
642
		if (lsr & LSR_BI)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
643
			ipend |= SER_INT_BREAK;
644
		if (lsr & LSR_RXRDY)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
645
			ipend |= SER_INT_RXREADY;
646
	} else {
647
		if (iir & IIR_TXRDY) {
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
648
			ipend |= SER_INT_TXIDLE;
649
650
			uart_setreg(bas, REG_IER, ns8250->ier);
		} else
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
651
			ipend |= SER_INT_SIGCHG;
652
	}
653
654
655
	if (ipend == 0)
		ns8250_clrint(bas);
	uart_unlock(sc->sc_hwmtx);
656
	return (ipend);
657
658
}

659
int
660
661
662
ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
    int stopbits, int parity)
{
663
	struct ns8250_softc *ns8250;
664
	struct uart_bas *bas;
665
	int error, limit;
666

667
	ns8250 = (struct ns8250_softc*)sc;
668
	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
669
	uart_lock(sc->sc_hwmtx);
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
	/*
	 * When using DW UART with BUSY detection it is necessary to wait
	 * until all serial transfers are finished before manipulating the
	 * line control. LCR will not be affected when UART is busy.
	 */
	if (ns8250->busy_detect != 0) {
		/*
		 * Pick an arbitrary high limit to avoid getting stuck in
		 * an infinite loop in case when the hardware is broken.
		 */
		limit = 10 * 1024;
		while (((uart_getreg(bas, DW_REG_USR) & USR_BUSY) != 0) &&
		    --limit)
			DELAY(4);

		if (limit <= 0) {
			/* UART appears to be stuck */
			uart_unlock(sc->sc_hwmtx);
			return (EIO);
		}
	}

692
	error = ns8250_param(bas, baudrate, databits, stopbits, parity);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
693
	uart_unlock(sc->sc_hwmtx);
694
	return (error);
695
696
}

697
int
698
699
ns8250_bus_probe(struct uart_softc *sc)
{
700
	struct ns8250_softc *ns8250;
701
702
	struct uart_bas *bas;
	int count, delay, error, limit;
703
	uint8_t lsr, mcr, ier;
704

705
	ns8250 = (struct ns8250_softc *)sc;
706
707
708
709
710
711
712
713
714
	bas = &sc->sc_bas;

	error = ns8250_probe(bas);
	if (error)
		return (error);

	mcr = MCR_IE;
	if (sc->sc_sysdev == NULL) {
		/* By using ns8250_init() we also set DTR and RTS. */
715
		ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE);
716
717
718
719
720
721
722
723
724
725
726
	} else
		mcr |= MCR_DTR | MCR_RTS;

	error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
	if (error)
		return (error);

	/*
	 * Set loopback mode. This avoids having garbage on the wire and
	 * also allows us send and receive data. We set DTR and RTS to
	 * avoid the possibility that automatic flow-control prevents
727
	 * any data from being sent.
728
	 */
729
	uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
730
731
732
733
	uart_barrier(bas);

	/*
	 * Enable FIFOs. And check that the UART has them. If not, we're
734
735
	 * done. Since this is the first time we enable the FIFOs, we reset
	 * them.
736
737
738
	 */
	uart_setreg(bas, REG_FCR, FCR_ENABLE);
	uart_barrier(bas);
739
	if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
740
741
742
743
744
745
		/*
		 * NS16450 or INS8250. We don't bother to differentiate
		 * between them. They're too old to be interesting.
		 */
		uart_setreg(bas, REG_MCR, mcr);
		uart_barrier(bas);
746
		sc->sc_rxfifosz = sc->sc_txfifosz = 1;
747
748
749
750
		device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
		return (0);
	}

751
	uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
	uart_barrier(bas);

	count = 0;
	delay = ns8250_delay(bas);

	/* We have FIFOs. Drain the transmitter and receiver. */
	error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
	if (error) {
		uart_setreg(bas, REG_MCR, mcr);
		uart_setreg(bas, REG_FCR, 0);
		uart_barrier(bas);
		goto describe;
	}

	/*
	 * We should have a sufficiently clean "pipe" to determine the
	 * size of the FIFOs. We send as much characters as is reasonable
769
	 * and wait for the overflow bit in the LSR register to be
770
771
	 * asserted, counting the characters as we send them. Based on
	 * that count we know the FIFO size.
772
	 */
773
	do {
774
775
776
777
778
		uart_setreg(bas, REG_DATA, 0);
		uart_barrier(bas);
		count++;

		limit = 30;
779
780
781
782
783
784
785
		lsr = 0;
		/*
		 * LSR bits are cleared upon read, so we must accumulate
		 * them to be able to test LSR_OE below.
		 */
		while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
		    --limit)
786
787
			DELAY(delay);
		if (limit == 0) {
788
			ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
789
			uart_setreg(bas, REG_IER, ier);
790
791
792
793
794
795
			uart_setreg(bas, REG_MCR, mcr);
			uart_setreg(bas, REG_FCR, 0);
			uart_barrier(bas);
			count = 0;
			goto describe;
		}
796
	} while ((lsr & LSR_OE) == 0 && count < 130);
797
	count--;
798
799
800
801
802
803
804

	uart_setreg(bas, REG_MCR, mcr);

	/* Reset FIFOs. */
	ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);

 describe:
805
	if (count >= 14 && count <= 16) {
806
807
		sc->sc_rxfifosz = 16;
		device_set_desc(sc->sc_dev, "16550 or compatible");
808
	} else if (count >= 28 && count <= 32) {
809
810
		sc->sc_rxfifosz = 32;
		device_set_desc(sc->sc_dev, "16650 or compatible");
811
	} else if (count >= 56 && count <= 64) {
812
813
		sc->sc_rxfifosz = 64;
		device_set_desc(sc->sc_dev, "16750 or compatible");
814
	} else if (count >= 112 && count <= 128) {
815
816
817
		sc->sc_rxfifosz = 128;
		device_set_desc(sc->sc_dev, "16950 or compatible");
	} else {
818
		sc->sc_rxfifosz = 16;
819
820
821
822
823
824
825
826
827
828
829
		device_set_desc(sc->sc_dev,
		    "Non-standard ns8250 class UART with FIFOs");
	}

	/*
	 * Force the Tx FIFO size to 16 bytes for now. We don't program the
	 * Tx trigger. Also, we assume that all data has been sent when the
	 * interrupt happens.
	 */
	sc->sc_txfifosz = 16;

830
831
832
833
834
835
836
#if 0
	/*
	 * XXX there are some issues related to hardware flow control and
	 * it's likely that uart(4) is the cause. This basicly needs more
	 * investigation, but we avoid using for hardware flow control
	 * until then.
	 */
837
838
839
840
841
	/* 16650s or higher have automatic flow control. */
	if (sc->sc_rxfifosz > 16) {
		sc->sc_hwiflow = 1;
		sc->sc_hwoflow = 1;
	}
842
#endif
843

844
845
846
	return (0);
}

847
int
848
849
850
851
852
853
854
ns8250_bus_receive(struct uart_softc *sc)
{
	struct uart_bas *bas;
	int xc;
	uint8_t lsr;

	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
855
	uart_lock(sc->sc_hwmtx);
856
857
858
859
	lsr = uart_getreg(bas, REG_LSR);
	while (lsr & LSR_RXRDY) {
		if (uart_rx_full(sc)) {
			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
860
			break;
861
		}
862
863
864
865
866
867
		xc = uart_getreg(bas, REG_DATA);
		if (lsr & LSR_FE)
			xc |= UART_STAT_FRAMERR;
		if (lsr & LSR_PE)
			xc |= UART_STAT_PARERR;
		uart_rx_put(sc, xc);
868
869
870
871
872
873
874
		lsr = uart_getreg(bas, REG_LSR);
	}
	/* Discard everything left in the Rx FIFO. */
	while (lsr & LSR_RXRDY) {
		(void)uart_getreg(bas, REG_DATA);
		uart_barrier(bas);
		lsr = uart_getreg(bas, REG_LSR);
875
	}
Marcel Moolenaar's avatar
Marcel Moolenaar committed
876
	uart_unlock(sc->sc_hwmtx);
877
878
879
 	return (0);
}

880
int
881
882
883
884
885
886
887
888
889
890
ns8250_bus_setsig(struct uart_softc *sc, int sig)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
	uint32_t new, old;

	bas = &sc->sc_bas;
	do {
		old = sc->sc_hwsig;
		new = old;
891
892
893
		if (sig & SER_DDTR) {
			SIGCHG(sig & SER_DTR, new, SER_DTR,
			    SER_DDTR);
894
		}
895
896
897
		if (sig & SER_DRTS) {
			SIGCHG(sig & SER_RTS, new, SER_RTS,
			    SER_DRTS);
898
899
		}
	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
Marcel Moolenaar's avatar
Marcel Moolenaar committed
900
	uart_lock(sc->sc_hwmtx);
901
	ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
902
	if (new & SER_DTR)
903
		ns8250->mcr |= MCR_DTR;
904
	if (new & SER_RTS)
905
906
907
		ns8250->mcr |= MCR_RTS;
	uart_setreg(bas, REG_MCR, ns8250->mcr);
	uart_barrier(bas);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
908
	uart_unlock(sc->sc_hwmtx);
909
910
911
	return (0);
}

912
int
913
914
915
916
917
918
919
ns8250_bus_transmit(struct uart_softc *sc)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
	int i;

	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
920
	uart_lock(sc->sc_hwmtx);
921
922
923
924
925
926
927
928
	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
		;
	uart_setreg(bas, REG_IER, ns8250->ier | IER_ETXRDY);
	uart_barrier(bas);
	for (i = 0; i < sc->sc_txdatasz; i++) {
		uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
		uart_barrier(bas);
	}
929
930
931
932
	if (broken_txfifo)
		ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
	else
		sc->sc_txbusy = 1;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
933
	uart_unlock(sc->sc_hwmtx);
934
935
	if (broken_txfifo)
		uart_sched_softih(sc, SER_INT_TXIDLE);
936
937
	return (0);
}
938
939
940
941
942

void
ns8250_bus_grab(struct uart_softc *sc)
{
	struct uart_bas *bas = &sc->sc_bas;
943
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
944
	u_char ier;
945
946
947
948
949
950
951

	/*
	 * turn off all interrupts to enter polling mode. Leave the
	 * saved mask alone. We'll restore whatever it was in ungrab.
	 * All pending interupt signals are reset when IER is set to 0.
	 */
	uart_lock(sc->sc_hwmtx);
952
953
	ier = uart_getreg(bas, REG_IER);
	uart_setreg(bas, REG_IER, ier & ns8250->ier_mask);
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
	uart_barrier(bas);
	uart_unlock(sc->sc_hwmtx);
}

void
ns8250_bus_ungrab(struct uart_softc *sc)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas = &sc->sc_bas;

	/*
	 * Restore previous interrupt mask
	 */
	uart_lock(sc->sc_hwmtx);
	uart_setreg(bas, REG_IER, ns8250->ier);
	uart_barrier(bas);
	uart_unlock(sc->sc_hwmtx);
}