uart_dev_ns8250.c revision 157989
1300313Ssjg/*-
2246149Ssjg * Copyright (c) 2003 Marcel Moolenaar
3246149Ssjg * All rights reserved.
4246149Ssjg *
5246149Ssjg * Redistribution and use in source and binary forms, with or without
6246149Ssjg * modification, are permitted provided that the following conditions
7246149Ssjg * are met:
8246149Ssjg *
9246149Ssjg * 1. Redistributions of source code must retain the above copyright
10246149Ssjg *    notice, this list of conditions and the following disclaimer.
11246149Ssjg * 2. Redistributions in binary form must reproduce the above copyright
12246149Ssjg *    notice, this list of conditions and the following disclaimer in the
13246149Ssjg *    documentation and/or other materials provided with the distribution.
14246149Ssjg *
15246149Ssjg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16246149Ssjg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17246149Ssjg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18246149Ssjg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19246149Ssjg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20246149Ssjg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21246149Ssjg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22246149Ssjg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23246149Ssjg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24246149Ssjg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25246149Ssjg */
26246149Ssjg
27246149Ssjg#include <sys/cdefs.h>
28246149Ssjg__FBSDID("$FreeBSD: head/sys/dev/uart/uart_dev_ns8250.c 157989 2006-04-23 21:15:07Z marcel $");
29246149Ssjg
30246149Ssjg#include <sys/param.h>
31246149Ssjg#include <sys/systm.h>
32246149Ssjg#include <sys/bus.h>
33246149Ssjg#include <sys/conf.h>
34246149Ssjg#include <machine/bus.h>
35246149Ssjg
36246149Ssjg#include <dev/uart/uart.h>
37246149Ssjg#include <dev/uart/uart_cpu.h>
38246149Ssjg#include <dev/uart/uart_bus.h>
39246149Ssjg
40246149Ssjg#include <dev/ic/ns16550.h>
41246149Ssjg
42246149Ssjg#include "uart_if.h"
43246149Ssjg
44246149Ssjg#define	DEFAULT_RCLK	1843200
45246149Ssjg
46246149Ssjg/*
47246149Ssjg * Clear pending interrupts. THRE is cleared by reading IIR. Data
48246149Ssjg * that may have been received gets lost here.
49246149Ssjg */
50246149Ssjgstatic void
51246149Ssjgns8250_clrint(struct uart_bas *bas)
52246149Ssjg{
53253883Ssjg	uint8_t iir;
54253883Ssjg
55253883Ssjg	iir = uart_getreg(bas, REG_IIR);
56253883Ssjg	while ((iir & IIR_NOPEND) == 0) {
57253883Ssjg		iir &= IIR_IMASK;
58253883Ssjg		if (iir == IIR_RLS)
59253883Ssjg			(void)uart_getreg(bas, REG_LSR);
60253883Ssjg		else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
61253883Ssjg			(void)uart_getreg(bas, REG_DATA);
62246149Ssjg		else if (iir == IIR_MLSC)
63246149Ssjg			(void)uart_getreg(bas, REG_MSR);
64246149Ssjg		uart_barrier(bas);
65246149Ssjg		iir = uart_getreg(bas, REG_IIR);
66246149Ssjg	}
67246149Ssjg}
68246149Ssjg
69300313Ssjgstatic int
70246149Ssjgns8250_delay(struct uart_bas *bas)
71246149Ssjg{
72246149Ssjg	int divisor;
73246149Ssjg	u_char lcr;
74246149Ssjg
75246149Ssjg	lcr = uart_getreg(bas, REG_LCR);
76246149Ssjg	uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
77246149Ssjg	uart_barrier(bas);
78246149Ssjg	divisor = uart_getdreg(bas, REG_DL);
79246149Ssjg	uart_barrier(bas);
80246149Ssjg	uart_setreg(bas, REG_LCR, lcr);
81246149Ssjg	uart_barrier(bas);
82246149Ssjg
83246149Ssjg	/* 1/10th the time to transmit 1 character (estimate). */
84246149Ssjg	return (16000000 * divisor / bas->rclk);
85246149Ssjg}
86246149Ssjg
87246149Ssjgstatic int
88246149Ssjgns8250_divisor(int rclk, int baudrate)
89246149Ssjg{
90246149Ssjg	int actual_baud, divisor;
91246149Ssjg	int error;
92246149Ssjg
93246149Ssjg	if (baudrate == 0)
94246149Ssjg		return (0);
95246149Ssjg
96246149Ssjg	divisor = (rclk / (baudrate << 3) + 1) >> 1;
97246149Ssjg	if (divisor == 0 || divisor >= 65536)
98246149Ssjg		return (0);
99246149Ssjg	actual_baud = rclk / (divisor << 4);
100246149Ssjg
101246149Ssjg	/* 10 times error in percent: */
102246149Ssjg	error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
103246149Ssjg
104246149Ssjg	/* 3.0% maximum error tolerance: */
105246149Ssjg	if (error < -30 || error > 30)
106246149Ssjg		return (0);
107246149Ssjg
108246149Ssjg	return (divisor);
109246149Ssjg}
110246149Ssjg
111246149Ssjgstatic int
112246149Ssjgns8250_drain(struct uart_bas *bas, int what)
113246149Ssjg{
114246149Ssjg	int delay, limit;
115246149Ssjg
116246149Ssjg	delay = ns8250_delay(bas);
117246149Ssjg
118246149Ssjg	if (what & UART_DRAIN_TRANSMITTER) {
119246149Ssjg		/*
120246149Ssjg		 * Pick an arbitrary high limit to avoid getting stuck in
121246149Ssjg		 * an infinite loop when the hardware is broken. Make the
122246149Ssjg		 * limit high enough to handle large FIFOs.
123246149Ssjg		 */
124246149Ssjg		limit = 10*1024;
125246149Ssjg		while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
126246149Ssjg			DELAY(delay);
127246149Ssjg		if (limit == 0) {
128246149Ssjg			/* printf("ns8250: transmitter appears stuck... "); */
129246149Ssjg			return (EIO);
130246149Ssjg		}
131246149Ssjg	}
132246149Ssjg
133246149Ssjg	if (what & UART_DRAIN_RECEIVER) {
134246149Ssjg		/*
135246149Ssjg		 * Pick an arbitrary high limit to avoid getting stuck in
136246149Ssjg		 * an infinite loop when the hardware is broken. Make the
137246149Ssjg		 * limit high enough to handle large FIFOs and integrated
138246149Ssjg		 * UARTs. The HP rx2600 for example has 3 UARTs on the
139246149Ssjg		 * management board that tend to get a lot of data send
140246149Ssjg		 * to it when the UART is first activated.
141246149Ssjg		 */
142246149Ssjg		limit=10*4096;
143246149Ssjg		while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
144246149Ssjg			(void)uart_getreg(bas, REG_DATA);
145246149Ssjg			uart_barrier(bas);
146246149Ssjg			DELAY(delay << 2);
147246149Ssjg		}
148246149Ssjg		if (limit == 0) {
149246149Ssjg			/* printf("ns8250: receiver appears broken... "); */
150246149Ssjg			return (EIO);
151246149Ssjg		}
152246149Ssjg	}
153246149Ssjg
154246149Ssjg	return (0);
155246149Ssjg}
156246149Ssjg
157246149Ssjg/*
158246149Ssjg * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
159246149Ssjg * drained. WARNING: this function clobbers the FIFO setting!
160246149Ssjg */
161246149Ssjgstatic void
162246149Ssjgns8250_flush(struct uart_bas *bas, int what)
163246149Ssjg{
164246149Ssjg	uint8_t fcr;
165246149Ssjg
166246149Ssjg	fcr = FCR_ENABLE;
167246149Ssjg	if (what & UART_FLUSH_TRANSMITTER)
168246149Ssjg		fcr |= FCR_XMT_RST;
169246149Ssjg	if (what & UART_FLUSH_RECEIVER)
170246149Ssjg		fcr |= FCR_RCV_RST;
171246149Ssjg	uart_setreg(bas, REG_FCR, fcr);
172246149Ssjg	uart_barrier(bas);
173246149Ssjg}
174246149Ssjg
175246149Ssjgstatic int
176246149Ssjgns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
177246149Ssjg    int parity)
178246149Ssjg{
179246149Ssjg	int divisor;
180246149Ssjg	uint8_t lcr;
181246149Ssjg
182246149Ssjg	lcr = 0;
183246149Ssjg	if (databits >= 8)
184246149Ssjg		lcr |= LCR_8BITS;
185246149Ssjg	else if (databits == 7)
186246149Ssjg		lcr |= LCR_7BITS;
187246149Ssjg	else if (databits == 6)
188246149Ssjg		lcr |= LCR_6BITS;
189246149Ssjg	else
190246149Ssjg		lcr |= LCR_5BITS;
191246149Ssjg	if (stopbits > 1)
192246149Ssjg		lcr |= LCR_STOPB;
193246149Ssjg	lcr |= parity << 3;
194246149Ssjg
195246149Ssjg	/* Set baudrate. */
196246149Ssjg	if (baudrate > 0) {
197246149Ssjg		divisor = ns8250_divisor(bas->rclk, baudrate);
198246149Ssjg		if (divisor == 0)
199246149Ssjg			return (EINVAL);
200246149Ssjg		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
201246149Ssjg		uart_barrier(bas);
202246149Ssjg		uart_setdreg(bas, REG_DL, divisor);
203246149Ssjg		uart_barrier(bas);
204246149Ssjg	}
205246149Ssjg
206246149Ssjg	/* Set LCR and clear DLAB. */
207246149Ssjg	uart_setreg(bas, REG_LCR, lcr);
208246149Ssjg	uart_barrier(bas);
209246149Ssjg	return (0);
210246149Ssjg}
211246149Ssjg
212246149Ssjg/*
213246149Ssjg * Low-level UART interface.
214246149Ssjg */
215246149Ssjgstatic int ns8250_probe(struct uart_bas *bas);
216246149Ssjgstatic void ns8250_init(struct uart_bas *bas, int, int, int, int);
217246149Ssjgstatic void ns8250_term(struct uart_bas *bas);
218246149Ssjgstatic void ns8250_putc(struct uart_bas *bas, int);
219246149Ssjgstatic int ns8250_poll(struct uart_bas *bas);
220246149Ssjgstatic int ns8250_getc(struct uart_bas *bas, struct mtx *);
221246149Ssjg
222246149Ssjgstruct uart_ops uart_ns8250_ops = {
223	.probe = ns8250_probe,
224	.init = ns8250_init,
225	.term = ns8250_term,
226	.putc = ns8250_putc,
227	.poll = ns8250_poll,
228	.getc = ns8250_getc,
229};
230
231static int
232ns8250_probe(struct uart_bas *bas)
233{
234	u_char lcr, val;
235
236	/* Check known 0 bits that don't depend on DLAB. */
237	val = uart_getreg(bas, REG_IIR);
238	if (val & 0x30)
239		return (ENXIO);
240	val = uart_getreg(bas, REG_MCR);
241	if (val & 0xe0)
242		return (ENXIO);
243
244	lcr = uart_getreg(bas, REG_LCR);
245	uart_setreg(bas, REG_LCR, lcr & ~LCR_DLAB);
246	uart_barrier(bas);
247
248	/* Check known 0 bits that depend on !DLAB. */
249	val = uart_getreg(bas, REG_IER);
250	if (val & 0xf0)
251		goto fail;
252
253	uart_setreg(bas, REG_LCR, lcr);
254	uart_barrier(bas);
255	return (0);
256
257 fail:
258	uart_setreg(bas, REG_LCR, lcr);
259	uart_barrier(bas);
260	return (ENXIO);
261}
262
263static void
264ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
265    int parity)
266{
267
268	if (bas->rclk == 0)
269		bas->rclk = DEFAULT_RCLK;
270	ns8250_param(bas, baudrate, databits, stopbits, parity);
271
272	/* Disable all interrupt sources. */
273	uart_setreg(bas, REG_IER, 0);
274	uart_barrier(bas);
275
276	/* Disable the FIFO (if present). */
277	uart_setreg(bas, REG_FCR, 0);
278	uart_barrier(bas);
279
280	/* Set RTS & DTR. */
281	uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
282	uart_barrier(bas);
283
284	ns8250_clrint(bas);
285}
286
287static void
288ns8250_term(struct uart_bas *bas)
289{
290
291	/* Clear RTS & DTR. */
292	uart_setreg(bas, REG_MCR, MCR_IE);
293	uart_barrier(bas);
294}
295
296static void
297ns8250_putc(struct uart_bas *bas, int c)
298{
299	int delay, limit;
300
301	/* 1/10th the time to transmit 1 character (estimate). */
302	delay = ns8250_delay(bas);
303
304	limit = 20;
305	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
306		DELAY(delay);
307	uart_setreg(bas, REG_DATA, c);
308	uart_barrier(bas);
309	limit = 40;
310	while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
311		DELAY(delay);
312}
313
314static int
315ns8250_poll(struct uart_bas *bas)
316{
317
318	if (uart_getreg(bas, REG_LSR) & LSR_RXRDY)
319		return (uart_getreg(bas, REG_DATA));
320	return (-1);
321}
322
323static int
324ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
325{
326	int c, delay;
327
328	uart_lock(hwmtx);
329
330	/* 1/10th the time to transmit 1 character (estimate). */
331	delay = ns8250_delay(bas);
332
333	while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
334		uart_unlock(hwmtx);
335		DELAY(delay);
336		uart_lock(hwmtx);
337	}
338
339	c = uart_getreg(bas, REG_DATA);
340
341	uart_unlock(hwmtx);
342
343	return (c);
344}
345
346/*
347 * High-level UART interface.
348 */
349struct ns8250_softc {
350	struct uart_softc base;
351	uint8_t		fcr;
352	uint8_t		ier;
353	uint8_t		mcr;
354};
355
356static int ns8250_bus_attach(struct uart_softc *);
357static int ns8250_bus_detach(struct uart_softc *);
358static int ns8250_bus_flush(struct uart_softc *, int);
359static int ns8250_bus_getsig(struct uart_softc *);
360static int ns8250_bus_ioctl(struct uart_softc *, int, intptr_t);
361static int ns8250_bus_ipend(struct uart_softc *);
362static int ns8250_bus_param(struct uart_softc *, int, int, int, int);
363static int ns8250_bus_probe(struct uart_softc *);
364static int ns8250_bus_receive(struct uart_softc *);
365static int ns8250_bus_setsig(struct uart_softc *, int);
366static int ns8250_bus_transmit(struct uart_softc *);
367
368static kobj_method_t ns8250_methods[] = {
369	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
370	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
371	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
372	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
373	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
374	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
375	KOBJMETHOD(uart_param,		ns8250_bus_param),
376	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
377	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
378	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
379	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
380	{ 0, 0 }
381};
382
383struct uart_class uart_ns8250_class = {
384	"ns8250 class",
385	ns8250_methods,
386	sizeof(struct ns8250_softc),
387	.uc_range = 8,
388	.uc_rclk = DEFAULT_RCLK
389};
390
391#define	SIGCHG(c, i, s, d)				\
392	if (c) {					\
393		i |= (i & s) ? s : s | d;		\
394	} else {					\
395		i = (i & s) ? (i & ~s) | d : i;		\
396	}
397
398static int
399ns8250_bus_attach(struct uart_softc *sc)
400{
401	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
402	struct uart_bas *bas;
403
404	bas = &sc->sc_bas;
405
406	ns8250->mcr = uart_getreg(bas, REG_MCR);
407	ns8250->fcr = FCR_ENABLE | FCR_RX_MEDH;
408	uart_setreg(bas, REG_FCR, ns8250->fcr);
409	uart_barrier(bas);
410	ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
411
412	if (ns8250->mcr & MCR_DTR)
413		sc->sc_hwsig |= SER_DTR;
414	if (ns8250->mcr & MCR_RTS)
415		sc->sc_hwsig |= SER_RTS;
416	ns8250_bus_getsig(sc);
417
418	ns8250_clrint(bas);
419	ns8250->ier = IER_EMSC | IER_ERLS | IER_ERXRDY;
420	uart_setreg(bas, REG_IER, ns8250->ier);
421	uart_barrier(bas);
422	return (0);
423}
424
425static int
426ns8250_bus_detach(struct uart_softc *sc)
427{
428	struct uart_bas *bas;
429
430	bas = &sc->sc_bas;
431	uart_setreg(bas, REG_IER, 0);
432	uart_barrier(bas);
433	ns8250_clrint(bas);
434	return (0);
435}
436
437static int
438ns8250_bus_flush(struct uart_softc *sc, int what)
439{
440	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
441	struct uart_bas *bas;
442	int error;
443
444	bas = &sc->sc_bas;
445	uart_lock(sc->sc_hwmtx);
446	if (sc->sc_rxfifosz > 1) {
447		ns8250_flush(bas, what);
448		uart_setreg(bas, REG_FCR, ns8250->fcr);
449		uart_barrier(bas);
450		error = 0;
451	} else
452		error = ns8250_drain(bas, what);
453	uart_unlock(sc->sc_hwmtx);
454	return (error);
455}
456
457static int
458ns8250_bus_getsig(struct uart_softc *sc)
459{
460	uint32_t new, old, sig;
461	uint8_t msr;
462
463	do {
464		old = sc->sc_hwsig;
465		sig = old;
466		uart_lock(sc->sc_hwmtx);
467		msr = uart_getreg(&sc->sc_bas, REG_MSR);
468		uart_unlock(sc->sc_hwmtx);
469		SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
470		SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
471		SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
472		SIGCHG(msr & MSR_RI,  sig, SER_RI,  SER_DRI);
473		new = sig & ~SER_MASK_DELTA;
474	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
475	return (sig);
476}
477
478static int
479ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
480{
481	struct uart_bas *bas;
482	int baudrate, divisor, error;
483	uint8_t efr, lcr;
484
485	bas = &sc->sc_bas;
486	error = 0;
487	uart_lock(sc->sc_hwmtx);
488	switch (request) {
489	case UART_IOCTL_BREAK:
490		lcr = uart_getreg(bas, REG_LCR);
491		if (data)
492			lcr |= LCR_SBREAK;
493		else
494			lcr &= ~LCR_SBREAK;
495		uart_setreg(bas, REG_LCR, lcr);
496		uart_barrier(bas);
497		break;
498	case UART_IOCTL_IFLOW:
499		lcr = uart_getreg(bas, REG_LCR);
500		uart_barrier(bas);
501		uart_setreg(bas, REG_LCR, 0xbf);
502		uart_barrier(bas);
503		efr = uart_getreg(bas, REG_EFR);
504		if (data)
505			efr |= EFR_RTS;
506		else
507			efr &= ~EFR_RTS;
508		uart_setreg(bas, REG_EFR, efr);
509		uart_barrier(bas);
510		uart_setreg(bas, REG_LCR, lcr);
511		uart_barrier(bas);
512		break;
513	case UART_IOCTL_OFLOW:
514		lcr = uart_getreg(bas, REG_LCR);
515		uart_barrier(bas);
516		uart_setreg(bas, REG_LCR, 0xbf);
517		uart_barrier(bas);
518		efr = uart_getreg(bas, REG_EFR);
519		if (data)
520			efr |= EFR_CTS;
521		else
522			efr &= ~EFR_CTS;
523		uart_setreg(bas, REG_EFR, efr);
524		uart_barrier(bas);
525		uart_setreg(bas, REG_LCR, lcr);
526		uart_barrier(bas);
527		break;
528	case UART_IOCTL_BAUD:
529		lcr = uart_getreg(bas, REG_LCR);
530		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
531		uart_barrier(bas);
532		divisor = uart_getdreg(bas, REG_DL);
533		uart_barrier(bas);
534		uart_setreg(bas, REG_LCR, lcr);
535		uart_barrier(bas);
536		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
537		if (baudrate > 0)
538			*(int*)data = baudrate;
539		else
540			error = ENXIO;
541		break;
542	default:
543		error = EINVAL;
544		break;
545	}
546	uart_unlock(sc->sc_hwmtx);
547	return (error);
548}
549
550static int
551ns8250_bus_ipend(struct uart_softc *sc)
552{
553	struct uart_bas *bas;
554	int ipend;
555	uint8_t iir, lsr;
556
557	bas = &sc->sc_bas;
558	uart_lock(sc->sc_hwmtx);
559	iir = uart_getreg(bas, REG_IIR);
560	if (iir & IIR_NOPEND) {
561		uart_unlock(sc->sc_hwmtx);
562		return (0);
563	}
564	ipend = 0;
565	if (iir & IIR_RXRDY) {
566		lsr = uart_getreg(bas, REG_LSR);
567		uart_unlock(sc->sc_hwmtx);
568		if (lsr & LSR_OE)
569			ipend |= SER_INT_OVERRUN;
570		if (lsr & LSR_BI)
571			ipend |= SER_INT_BREAK;
572		if (lsr & LSR_RXRDY)
573			ipend |= SER_INT_RXREADY;
574	} else {
575		uart_unlock(sc->sc_hwmtx);
576		if (iir & IIR_TXRDY)
577			ipend |= SER_INT_TXIDLE;
578		else
579			ipend |= SER_INT_SIGCHG;
580	}
581	return ((sc->sc_leaving) ? 0 : ipend);
582}
583
584static int
585ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
586    int stopbits, int parity)
587{
588	struct uart_bas *bas;
589	int error;
590
591	bas = &sc->sc_bas;
592	uart_lock(sc->sc_hwmtx);
593	error = ns8250_param(bas, baudrate, databits, stopbits, parity);
594	uart_unlock(sc->sc_hwmtx);
595	return (error);
596}
597
598static int
599ns8250_bus_probe(struct uart_softc *sc)
600{
601	struct uart_bas *bas;
602	int count, delay, error, limit;
603	uint8_t lsr, mcr;
604
605	bas = &sc->sc_bas;
606
607	error = ns8250_probe(bas);
608	if (error)
609		return (error);
610
611	mcr = MCR_IE;
612	if (sc->sc_sysdev == NULL) {
613		/* By using ns8250_init() we also set DTR and RTS. */
614		ns8250_init(bas, 9600, 8, 1, UART_PARITY_NONE);
615	} else
616		mcr |= MCR_DTR | MCR_RTS;
617
618	error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
619	if (error)
620		return (error);
621
622	/*
623	 * Set loopback mode. This avoids having garbage on the wire and
624	 * also allows us send and receive data. We set DTR and RTS to
625	 * avoid the possibility that automatic flow-control prevents
626	 * any data from being sent.
627	 */
628	uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
629	uart_barrier(bas);
630
631	/*
632	 * Enable FIFOs. And check that the UART has them. If not, we're
633	 * done. Since this is the first time we enable the FIFOs, we reset
634	 * them.
635	 */
636	uart_setreg(bas, REG_FCR, FCR_ENABLE);
637	uart_barrier(bas);
638	if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
639		/*
640		 * NS16450 or INS8250. We don't bother to differentiate
641		 * between them. They're too old to be interesting.
642		 */
643		uart_setreg(bas, REG_MCR, mcr);
644		uart_barrier(bas);
645		sc->sc_rxfifosz = sc->sc_txfifosz = 1;
646		device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
647		return (0);
648	}
649
650	uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
651	uart_barrier(bas);
652
653	count = 0;
654	delay = ns8250_delay(bas);
655
656	/* We have FIFOs. Drain the transmitter and receiver. */
657	error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
658	if (error) {
659		uart_setreg(bas, REG_MCR, mcr);
660		uart_setreg(bas, REG_FCR, 0);
661		uart_barrier(bas);
662		goto describe;
663	}
664
665	/*
666	 * We should have a sufficiently clean "pipe" to determine the
667	 * size of the FIFOs. We send as much characters as is reasonable
668	 * and wait for the the overflow bit in the LSR register to be
669	 * asserted, counting the characters as we send them. Based on
670	 * that count we know the FIFO size.
671	 */
672	do {
673		uart_setreg(bas, REG_DATA, 0);
674		uart_barrier(bas);
675		count++;
676
677		limit = 30;
678		lsr = 0;
679		/*
680		 * LSR bits are cleared upon read, so we must accumulate
681		 * them to be able to test LSR_OE below.
682		 */
683		while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
684		    --limit)
685			DELAY(delay);
686		if (limit == 0) {
687			uart_setreg(bas, REG_IER, 0);
688			uart_setreg(bas, REG_MCR, mcr);
689			uart_setreg(bas, REG_FCR, 0);
690			uart_barrier(bas);
691			count = 0;
692			goto describe;
693		}
694	} while ((lsr & LSR_OE) == 0 && count < 130);
695	count--;
696
697	uart_setreg(bas, REG_MCR, mcr);
698
699	/* Reset FIFOs. */
700	ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
701
702 describe:
703	if (count >= 14 && count <= 16) {
704		sc->sc_rxfifosz = 16;
705		device_set_desc(sc->sc_dev, "16550 or compatible");
706	} else if (count >= 28 && count <= 32) {
707		sc->sc_rxfifosz = 32;
708		device_set_desc(sc->sc_dev, "16650 or compatible");
709	} else if (count >= 56 && count <= 64) {
710		sc->sc_rxfifosz = 64;
711		device_set_desc(sc->sc_dev, "16750 or compatible");
712	} else if (count >= 112 && count <= 128) {
713		sc->sc_rxfifosz = 128;
714		device_set_desc(sc->sc_dev, "16950 or compatible");
715	} else {
716		sc->sc_rxfifosz = 16;
717		device_set_desc(sc->sc_dev,
718		    "Non-standard ns8250 class UART with FIFOs");
719	}
720
721	/*
722	 * Force the Tx FIFO size to 16 bytes for now. We don't program the
723	 * Tx trigger. Also, we assume that all data has been sent when the
724	 * interrupt happens.
725	 */
726	sc->sc_txfifosz = 16;
727
728#if 0
729	/*
730	 * XXX there are some issues related to hardware flow control and
731	 * it's likely that uart(4) is the cause. This basicly needs more
732	 * investigation, but we avoid using for hardware flow control
733	 * until then.
734	 */
735	/* 16650s or higher have automatic flow control. */
736	if (sc->sc_rxfifosz > 16) {
737		sc->sc_hwiflow = 1;
738		sc->sc_hwoflow = 1;
739	}
740#endif
741
742	return (0);
743}
744
745static int
746ns8250_bus_receive(struct uart_softc *sc)
747{
748	struct uart_bas *bas;
749	int xc;
750	uint8_t lsr;
751
752	bas = &sc->sc_bas;
753	uart_lock(sc->sc_hwmtx);
754	lsr = uart_getreg(bas, REG_LSR);
755	while (lsr & LSR_RXRDY) {
756		if (uart_rx_full(sc)) {
757			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
758			break;
759		}
760		xc = uart_getreg(bas, REG_DATA);
761		if (lsr & LSR_FE)
762			xc |= UART_STAT_FRAMERR;
763		if (lsr & LSR_PE)
764			xc |= UART_STAT_PARERR;
765		uart_rx_put(sc, xc);
766		lsr = uart_getreg(bas, REG_LSR);
767	}
768	/* Discard everything left in the Rx FIFO. */
769	while (lsr & LSR_RXRDY) {
770		(void)uart_getreg(bas, REG_DATA);
771		uart_barrier(bas);
772		lsr = uart_getreg(bas, REG_LSR);
773	}
774	uart_unlock(sc->sc_hwmtx);
775 	return (0);
776}
777
778static int
779ns8250_bus_setsig(struct uart_softc *sc, int sig)
780{
781	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
782	struct uart_bas *bas;
783	uint32_t new, old;
784
785	bas = &sc->sc_bas;
786	do {
787		old = sc->sc_hwsig;
788		new = old;
789		if (sig & SER_DDTR) {
790			SIGCHG(sig & SER_DTR, new, SER_DTR,
791			    SER_DDTR);
792		}
793		if (sig & SER_DRTS) {
794			SIGCHG(sig & SER_RTS, new, SER_RTS,
795			    SER_DRTS);
796		}
797	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
798	uart_lock(sc->sc_hwmtx);
799	ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
800	if (new & SER_DTR)
801		ns8250->mcr |= MCR_DTR;
802	if (new & SER_RTS)
803		ns8250->mcr |= MCR_RTS;
804	uart_setreg(bas, REG_MCR, ns8250->mcr);
805	uart_barrier(bas);
806	uart_unlock(sc->sc_hwmtx);
807	return (0);
808}
809
810static int
811ns8250_bus_transmit(struct uart_softc *sc)
812{
813	struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
814	struct uart_bas *bas;
815	int i;
816
817	bas = &sc->sc_bas;
818	uart_lock(sc->sc_hwmtx);
819	while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
820		;
821	uart_setreg(bas, REG_IER, ns8250->ier | IER_ETXRDY);
822	uart_barrier(bas);
823	for (i = 0; i < sc->sc_txdatasz; i++) {
824		uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
825		uart_barrier(bas);
826	}
827	sc->sc_txbusy = 1;
828	uart_unlock(sc->sc_hwmtx);
829	return (0);
830}
831