uart_dev_ns8250.c 27.6 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * 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.
 */

29
#include "opt_acpi.h"
30
#include "opt_platform.h"
31
#include "opt_uart.h"
32

33
34
35
36
37
38
39
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
40
41
#include <sys/kernel.h>
#include <sys/sysctl.h>
42
43
#include <machine/bus.h>

44
45
46
47
48
49
#ifdef FDT
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

50
51
#include <dev/uart/uart.h>
#include <dev/uart/uart_cpu.h>
52
53
54
#ifdef FDT
#include <dev/uart/uart_cpu_fdt.h>
#endif
55
#include <dev/uart/uart_bus.h>
56
#include <dev/uart/uart_dev_ns8250.h>
57
#include <dev/uart/uart_ppstypes.h>
58
59
60
#ifdef DEV_ACPI
#include <dev/uart/uart_cpu_acpi.h>
#endif
61
62

#include <dev/ic/ns16550.h>
63
64
65
66
67

#include "uart_if.h"

#define	DEFAULT_RCLK	1843200

68
69
70
71
72
73
74
75
76
77
/*
 * Set the default baudrate tolerance to 3.0%.
 *
 * Some embedded boards have odd reference clocks (eg 25MHz)
 * and we need to handle higher variances in the target baud rate.
 */
#ifndef	UART_DEV_TOLERANCE_PCT
#define	UART_DEV_TOLERANCE_PCT	30
#endif	/* UART_DEV_TOLERANCE_PCT */

78
static int broken_txfifo = 0;
79
SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RWTUN,
80
81
	&broken_txfifo, 0, "UART FIFO has QEMU emulation bug");

82
83
84
85
86
87
88
/*
 * 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)
{
89
	uint8_t iir, lsr;
90
91
92
93

	iir = uart_getreg(bas, REG_IIR);
	while ((iir & IIR_NOPEND) == 0) {
		iir &= IIR_IMASK;
94
95
96
97
98
		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)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
			(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);
116
	divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
117
118
119
120
121
	uart_barrier(bas);
	uart_setreg(bas, REG_LCR, lcr);
	uart_barrier(bas);

	/* 1/10th the time to transmit 1 character (estimate). */
122
123
124
	if (divisor <= 134)
		return (16000000 * divisor / bas->rclk);
	return (16000 * divisor / (bas->rclk / 1000));
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
}

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: */
142
	error = ((actual_baud - baudrate) * 2000 / baudrate + 1) / 2;
143

144
145
	/* enforce maximum error tolerance: */
	if (error < -UART_DEV_TOLERANCE_PCT || error > UART_DEV_TOLERANCE_PCT)
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
		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;
207
208
209
#ifdef CPU_XBURST
	fcr |= FCR_UART_ON;
#endif
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
	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);
243
244
		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
		uart_barrier(bas);
245
246
		uart_setreg(bas, REG_DLL, divisor & 0xff);
		uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
		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);
263
static int ns8250_rxready(struct uart_bas *bas);
264
static int ns8250_getc(struct uart_bas *bas, struct mtx *);
265

266
struct uart_ops uart_ns8250_ops = {
267
268
269
270
	.probe = ns8250_probe,
	.init = ns8250_init,
	.term = ns8250_term,
	.putc = ns8250_putc,
271
	.rxready = ns8250_rxready,
272
273
274
275
276
277
	.getc = ns8250_getc,
};

static int
ns8250_probe(struct uart_bas *bas)
{
278
	u_char val;
279

280
281
282
283
#ifdef CPU_XBURST
	uart_setreg(bas, REG_FCR, FCR_UART_ON);
#endif

284
285
286
287
	/* Check known 0 bits that don't depend on DLAB. */
	val = uart_getreg(bas, REG_IIR);
	if (val & 0x30)
		return (ENXIO);
288
289
290
291
292
293
	/*
	 * 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.
	 */
294
	val = uart_getreg(bas, REG_MCR);
295
	if (val & 0xa0)
296
297
298
299
300
301
302
303
304
		return (ENXIO);

	return (0);
}

static void
ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
    int parity)
{
305
	u_char ier, val;
306
307
308
309
310
311

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

	/* Disable all interrupt sources. */
312
313
314
315
316
317
	/*
	 * 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;
318
	uart_setreg(bas, REG_IER, ier);
319
320
321
	uart_barrier(bas);

	/* Disable the FIFO (if present). */
322
323
	val = 0;
#ifdef CPU_XBURST
324
	val |= FCR_UART_ON;
325
326
#endif
	uart_setreg(bas, REG_FCR, val);
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
	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)
{
348
	int limit;
349

350
	limit = 250000;
351
	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
352
		DELAY(4);
353
	uart_setreg(bas, REG_DATA, c);
354
	uart_barrier(bas);
355
356
357
}

static int
358
ns8250_rxready(struct uart_bas *bas)
359
360
{

361
	return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
362
363
364
}

static int
365
ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
366
{
367
	int c;
368
369

	uart_lock(hwmtx);
370

371
372
	while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
		uart_unlock(hwmtx);
373
		DELAY(4);
374
375
376
377
378
379
380
381
		uart_lock(hwmtx);
	}

	c = uart_getreg(bas, REG_DATA);

	uart_unlock(hwmtx);

	return (c);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
}

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),
396
397
	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
398
399
400
401
	{ 0, 0 }
};

struct uart_class uart_ns8250_class = {
402
	"ns8250",
403
404
	ns8250_methods,
	sizeof(struct ns8250_softc),
405
	.uc_ops = &uart_ns8250_ops,
406
	.uc_range = 8,
407
408
	.uc_rclk = DEFAULT_RCLK,
	.uc_rshift = 0
409
410
};

411
412
413
414
415
416
417
/*
 * XXX -- refactor out ACPI and FDT ifdefs
 */
#ifdef DEV_ACPI
static struct acpi_uart_compat_data acpi_compat_data[] = {
	{"AMD0020",	&uart_ns8250_class, 0, 2, 0, 48000000, UART_F_BUSY_DETECT, "AMD / Synopsys Designware UART"},
	{"AMDI0020", &uart_ns8250_class, 0, 2, 0, 48000000, UART_F_BUSY_DETECT, "AMD / Synopsys Designware UART"},
418
419
420
	{"MRVL0001", &uart_ns8250_class, 0, 2, 0, 200000000, UART_F_BUSY_DETECT, "Marvell / Synopsys Designware UART"},
	{"SCX0006",  &uart_ns8250_class, 0, 2, 0, 62500000, UART_F_BUSY_DETECT, "SynQuacer / Synopsys Designware UART"},
	{"HISI0031", &uart_ns8250_class, 0, 2, 0, 200000000, UART_F_BUSY_DETECT, "HiSilicon / Synopsys Designware UART"},
421
422
423
424
425
426
427
428
429
430
431
432
433
	{"PNP0500", &uart_ns8250_class, 0, 0, 0, 0, 0, "Standard PC COM port"},
	{"PNP0501", &uart_ns8250_class, 0, 0, 0, 0, 0, "16550A-compatible COM port"},
	{"PNP0502", &uart_ns8250_class, 0, 0, 0, 0, 0, "Multiport serial device (non-intelligent 16550)"},
	{"PNP0510", &uart_ns8250_class, 0, 0, 0, 0, 0, "Generic IRDA-compatible device"},
	{"PNP0511", &uart_ns8250_class, 0, 0, 0, 0, 0, "Generic IRDA-compatible device"},
	{"WACF004", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet PC Screen"},
	{"WACF00E", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet PC Screen 00e"},
	{"FUJ02E5", &uart_ns8250_class, 0, 0, 0, 0, 0, "Wacom Tablet at FuS Lifebook T"},
	{NULL, 			NULL, 0, 0 , 0, 0, 0, NULL},
};
UART_ACPI_CLASS_AND_DEVICE(acpi_compat_data);
#endif

434
435
436
#ifdef FDT
static struct ofw_compat_data compat_data[] = {
	{"ns16550",		(uintptr_t)&uart_ns8250_class},
437
	{"ns16550a",		(uintptr_t)&uart_ns8250_class},
438
439
440
441
442
	{NULL,			(uintptr_t)NULL},
};
UART_FDT_CLASS_AND_DEVICE(compat_data);
#endif

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* Use token-pasting to form SER_ and MSR_ named constants. */
#define	SER(sig)	SER_##sig
#define	SERD(sig)	SER_D##sig
#define	MSR(sig)	MSR_##sig
#define	MSRD(sig)	MSR_D##sig

/*
 * Detect signal changes using software delta detection.  The previous state of
 * the signals is in 'var' the new hardware state is in 'msr', and 'sig' is the
 * short name (DCD, CTS, etc) of the signal bit being processed; 'var' gets the
 * new state of both the signal and the delta bits.
 */
#define SIGCHGSW(var, msr, sig)					\
	if ((msr) & MSR(sig)) {					\
		if ((var & SER(sig)) == 0)			\
			var |= SERD(sig) | SER(sig);		\
	} else {						\
		if ((var & SER(sig)) != 0)			\
			var = SERD(sig) | (var & ~SER(sig));	\
	}

/*
 * Detect signal changes using the hardware msr delta bits.  This is currently
 * used only when PPS timing information is being captured using the "narrow
 * pulse" option.  With a narrow PPS pulse the signal may not still be asserted
 * by time the interrupt handler is invoked.  The hardware will latch the fact
 * that it changed in the delta bits.
 */
#define SIGCHGHW(var, msr, sig)					\
	if ((msr) & MSRD(sig)) {				\
		if (((msr) & MSR(sig)) != 0)			\
			var |= SERD(sig) | SER(sig);		\
		else						\
			var = SERD(sig) | (var & ~SER(sig));	\
477
478
	}

479
int
480
481
482
483
ns8250_bus_attach(struct uart_softc *sc)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
484
	unsigned int ivar;
485
486
487
488
489
490
#ifdef FDT
	phandle_t node;
	pcell_t cell;
#endif

#ifdef FDT
491
	/* Check whether uart has a broken txfifo. */
492
	node = ofw_bus_get_node(sc->sc_dev);
493
494
	if ((OF_getencprop(node, "broken-txfifo", &cell, sizeof(cell))) > 0)
		broken_txfifo =  cell ? 1 : 0;
495
#endif
496
497
498

	bas = &sc->sc_bas;

499
	ns8250->busy_detect = bas->busy_detect;
500
	ns8250->mcr = uart_getreg(bas, REG_MCR);
501
	ns8250->fcr = FCR_ENABLE;
502
503
504
#ifdef CPU_XBURST
	ns8250->fcr |= FCR_UART_ON;
#endif
505
506
507
508
509
510
511
512
513
514
515
516
	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;
517

518
519
520
521
522
	/* 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);
523

524
525
526
527
528
	/* 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);
529

530
531
532
533
534
	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)
535
		sc->sc_hwsig |= SER_DTR;
536
	if (ns8250->mcr & MCR_RTS)
537
		sc->sc_hwsig |= SER_RTS;
538
539
540
	ns8250_bus_getsig(sc);

	ns8250_clrint(bas);
541
542
	ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
	ns8250->ier |= ns8250->ier_rxbits;
543
544
	uart_setreg(bas, REG_IER, ns8250->ier);
	uart_barrier(bas);
545
546
547
548
549
550
551
552
553
554
555
556

	/*
	 * 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.
	 */
557
	DELAY(200);
558
559
560
	return (0);
}

561
int
562
563
ns8250_bus_detach(struct uart_softc *sc)
{
564
	struct ns8250_softc *ns8250;
565
	struct uart_bas *bas;
566
	u_char ier;
567

568
	ns8250 = (struct ns8250_softc *)sc;
569
	bas = &sc->sc_bas;
570
	ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
571
	uart_setreg(bas, REG_IER, ier);
572
573
574
575
576
	uart_barrier(bas);
	ns8250_clrint(bas);
	return (0);
}

577
int
578
579
580
581
ns8250_bus_flush(struct uart_softc *sc, int what)
{
	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
	struct uart_bas *bas;
582
	int error;
583
584

	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
585
	uart_lock(sc->sc_hwmtx);
586
	if (sc->sc_rxfifosz > 1) {
587
588
589
		ns8250_flush(bas, what);
		uart_setreg(bas, REG_FCR, ns8250->fcr);
		uart_barrier(bas);
590
591
592
		error = 0;
	} else
		error = ns8250_drain(bas, what);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
593
	uart_unlock(sc->sc_hwmtx);
594
	return (error);
595
596
}

597
int
598
599
ns8250_bus_getsig(struct uart_softc *sc)
{
600
	uint32_t old, sig;
601
602
	uint8_t msr;

603
604
605
606
607
608
	/*
	 * The delta bits are reputed to be broken on some hardware, so use
	 * software delta detection by default.  Use the hardware delta bits
	 * when capturing PPS pulses which are too narrow for software detection
	 * to see the edges.  Hardware delta for RI doesn't work like the
	 * others, so always use software for it.  Other threads may be changing
609
	 * other (non-MSR) bits in sc_hwsig, so loop until it can successfully
610
611
612
613
	 * update without other changes happening.  Note that the SIGCHGxx()
	 * macros carefully preserve the delta bits when we have to loop several
	 * times and a signal transitions between iterations.
	 */
614
615
616
	do {
		old = sc->sc_hwsig;
		sig = old;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
617
		uart_lock(sc->sc_hwmtx);
618
		msr = uart_getreg(&sc->sc_bas, REG_MSR);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
619
		uart_unlock(sc->sc_hwmtx);
620
621
622
623
624
625
626
627
628
629
630
		if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE) {
			SIGCHGHW(sig, msr, DSR);
			SIGCHGHW(sig, msr, CTS);
			SIGCHGHW(sig, msr, DCD);
		} else {
			SIGCHGSW(sig, msr, DSR);
			SIGCHGSW(sig, msr, CTS);
			SIGCHGSW(sig, msr, DCD);
		}
		SIGCHGSW(sig, msr, RI);
	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, sig & ~SER_MASK_DELTA));
631
632
633
	return (sig);
}

634
int
635
636
637
ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
{
	struct uart_bas *bas;
638
	int baudrate, divisor, error;
639
	uint8_t efr, lcr;
640
641

	bas = &sc->sc_bas;
642
	error = 0;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
643
	uart_lock(sc->sc_hwmtx);
644
645
646
647
648
649
650
651
652
653
	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;
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
	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;
684
685
686
687
	case UART_IOCTL_BAUD:
		lcr = uart_getreg(bas, REG_LCR);
		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
		uart_barrier(bas);
688
689
		divisor = uart_getreg(bas, REG_DLL) |
		    (uart_getreg(bas, REG_DLH) << 8);
690
691
692
		uart_barrier(bas);
		uart_setreg(bas, REG_LCR, lcr);
		uart_barrier(bas);
693
694
695
696
697
		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
		if (baudrate > 0)
			*(int*)data = baudrate;
		else
			error = ENXIO;
698
		break;
699
	default:
700
701
		error = EINVAL;
		break;
702
	}
Marcel Moolenaar's avatar
Marcel Moolenaar committed
703
	uart_unlock(sc->sc_hwmtx);
704
	return (error);
705
706
}

707
int
708
709
710
ns8250_bus_ipend(struct uart_softc *sc)
{
	struct uart_bas *bas;
711
	struct ns8250_softc *ns8250;
712
713
714
	int ipend;
	uint8_t iir, lsr;

715
	ns8250 = (struct ns8250_softc *)sc;
716
	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
717
	uart_lock(sc->sc_hwmtx);
718
	iir = uart_getreg(bas, REG_IIR);
719
720
721
722
723
724

	if (ns8250->busy_detect && (iir & IIR_BUSY) == IIR_BUSY) {
		(void)uart_getreg(bas, DW_REG_USR);
		uart_unlock(sc->sc_hwmtx);
		return (0);
	}
725
	if (iir & IIR_NOPEND) {
Marcel Moolenaar's avatar
Marcel Moolenaar committed
726
		uart_unlock(sc->sc_hwmtx);
727
		return (0);
728
	}
729
730
731
732
	ipend = 0;
	if (iir & IIR_RXRDY) {
		lsr = uart_getreg(bas, REG_LSR);
		if (lsr & LSR_OE)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
733
			ipend |= SER_INT_OVERRUN;
734
		if (lsr & LSR_BI)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
735
			ipend |= SER_INT_BREAK;
736
		if (lsr & LSR_RXRDY)
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
737
			ipend |= SER_INT_RXREADY;
738
	} else {
739
		if (iir & IIR_TXRDY) {
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
740
			ipend |= SER_INT_TXIDLE;
741
			ns8250->ier &= ~IER_ETXRDY;
742
			uart_setreg(bas, REG_IER, ns8250->ier);
743
			uart_barrier(bas);
744
		} else
Marcel Moolenaar's avatar
MFp4:    
Marcel Moolenaar committed
745
			ipend |= SER_INT_SIGCHG;
746
	}
747
748
749
	if (ipend == 0)
		ns8250_clrint(bas);
	uart_unlock(sc->sc_hwmtx);
750
	return (ipend);
751
752
}

753
int
754
755
756
ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
    int stopbits, int parity)
{
757
	struct ns8250_softc *ns8250;
758
	struct uart_bas *bas;
759
	int error, limit;
760

761
	ns8250 = (struct ns8250_softc*)sc;
762
	bas = &sc->sc_bas;
Marcel Moolenaar's avatar
Marcel Moolenaar committed
763
	uart_lock(sc->sc_hwmtx);
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
	/*
	 * 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);
		}
	}

786
	error = ns8250_param(bas, baudrate, databits, stopbits, parity);
Marcel Moolenaar's avatar
Marcel Moolenaar committed
787
	uart_unlock(sc->sc_hwmtx);
788
	return (error);
789
790
}

791
int
792
793
ns8250_bus_probe(struct uart_softc *sc)
{
794
	struct ns8250_softc *ns8250;
795
796
	struct uart_bas *bas;
	int count, delay, error, limit;
797
	uint8_t lsr, mcr, ier;
798
	uint8_t val;
799

800
	ns8250 = (struct ns8250_softc *)sc;
801
802
803
804
805
806
807
808
809
	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. */
810
		ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE);
811
812
813
814
815
816
817
818
819
820
821
	} 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
822
	 * any data from being sent.
823
	 */
824
	uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
825
826
827
828
	uart_barrier(bas);

	/*
	 * Enable FIFOs. And check that the UART has them. If not, we're
829
830
	 * done. Since this is the first time we enable the FIFOs, we reset
	 * them.
831
	 */
832
833
834
835
836
	val = FCR_ENABLE;
#ifdef CPU_XBURST
	val |= FCR_UART_ON;
#endif
	uart_setreg(bas, REG_FCR, val);
837
	uart_barrier(bas);
838
	if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
839
840
841
842
843
844
		/*
		 * 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);
845
		sc->sc_rxfifosz = sc->sc_txfifosz = 1;
846
847
848
849
		device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
		return (0);
	}

850
851
852
853
854
	val = FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST;
#ifdef CPU_XBURST
	val |= FCR_UART_ON;
#endif
	uart_setreg(bas, REG_FCR, val);
855
856
857
858
859
860
861
862
863
	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);
864
865
866
867
868
		val = 0;
#ifdef CPU_XBURST
		val |= FCR_UART_ON;
#endif
		uart_setreg(bas, REG_FCR, val);
869
870
871
872
873
874
875
		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
876
	 * and wait for the overflow bit in the LSR register to be
877
878
	 * asserted, counting the characters as we send them. Based on
	 * that count we know the FIFO size.
879
	 */
880
	do {
881
882
883
884
885
		uart_setreg(bas, REG_DATA, 0);
		uart_barrier(bas);
		count++;

		limit = 30;
886
887
888
889
890
891
892
		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)
893
894
			DELAY(delay);
		if (limit == 0) {
895
			ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
896
			uart_setreg(bas, REG_IER, ier);
897
			uart_setreg(bas, REG_MCR, mcr);
898
899
900
901
902
			val = 0;
#ifdef CPU_XBURST
			val |= FCR_UART_ON;
#endif
			uart_setreg(bas, REG_FCR, val);
903
904
905
906
			uart_barrier(bas);
			count = 0;
			goto describe;
		}
Ed Maste's avatar
Ed Maste committed
907
	} while ((lsr & LSR_OE) == 0 && count < 260);
908
	count--;
909
910
911
912
913
914
915

	uart_setreg(bas, REG_MCR, mcr);

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

 describe:
916
	if (count >= 14 && count <= 16) {
917
918
		sc->sc_rxfifosz = 16;
		device_set_desc(sc->sc_dev, "16550 or compatible");
919
	} else if (count >= 28 && count <= 32) {
920
921
		sc->sc_rxfifosz = 32;
		device_set_desc(sc->sc_dev, "16650 or compatible");
922
	} else if (count >= 56 && count <= 64) {
923
924
		sc->sc_rxfifosz = 64;
		device_set_desc(sc->sc_dev, "16750 or compatible");
925
	} else if (count >= 112 && count <= 128) {
926
927
		sc->sc_rxfifosz = 128;
		device_set_desc(sc->sc_dev, "16950 or compatible");
Ed Maste's avatar
Ed Maste committed
928
929
930
	} else if (count >= 224 && count <= 256) {
		sc->sc_rxfifosz = 256;
		device_set_desc(sc->sc_dev, "16x50 with 256 byte FIFO");
931
	} else {
932
		sc->sc_rxfifosz = 16;
933
934
935
936
937
938
939
940
941
942
943
		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;

944
945
946
#if 0
	/*
	 * XXX there are some issues related to hardware flow control and
947
	 * it's likely that uart(4) is the cause. This basically needs more
948
949
950
	 * investigation, but we avoid using for hardware flow control
	 * until then.
	 */
951
952
953
954
955
	/* 16650s or higher have automatic flow control. */
	if (sc->sc_rxfifosz > 16) {
		sc->sc_hwiflow = 1;
		sc->sc_hwoflow = 1;
	}
956
#endif
957

958
959
960
	return (0);
}

961
int
962
963
964
965
966
967
968
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
969
	uart_lock(sc->sc_hwmtx);
970
971
972
973
	lsr = uart_getreg(bas, REG_LSR);
	while (lsr & LSR_RXRDY) {
		if (uart_rx_full(sc)) {
			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
974
			break;
975
		}
976
977
978
979
980
981
		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);
982
983
984
985
986
987
988
		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);
989
	}
Marcel Moolenaar's avatar
Marcel Moolenaar committed
990
	uart_unlock(sc->sc_hwmtx);
991
992
993
 	return (0);
}

994
int
995
996
997
998
999
1000
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;

For faster browsing, not all history is shown. View entire blame