]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/uart/uart_dev_ns8250.c
MFC r357212: libfetch: fix urldecode buffer overrun
[FreeBSD/stable/10.git] / sys / dev / uart / uart_dev_ns8250.c
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "opt_platform.h"
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <machine/bus.h>
39
40 #ifdef FDT
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #endif
45
46 #include <dev/uart/uart.h>
47 #include <dev/uart/uart_cpu.h>
48 #ifdef FDT
49 #include <dev/uart/uart_cpu_fdt.h>
50 #endif
51 #include <dev/uart/uart_bus.h>
52 #include <dev/uart/uart_dev_ns8250.h>
53 #include <dev/uart/uart_ppstypes.h>
54
55 #include <dev/ic/ns16550.h>
56
57 #include "uart_if.h"
58
59 #define DEFAULT_RCLK    1843200
60
61 static int broken_txfifo = 0;
62 SYSCTL_INT(_hw, OID_AUTO, broken_txfifo, CTLFLAG_RW | CTLFLAG_TUN,
63         &broken_txfifo, 0, "UART FIFO has QEMU emulation bug");
64 TUNABLE_INT("hw.broken_txfifo", &broken_txfifo);
65
66 /*
67  * Clear pending interrupts. THRE is cleared by reading IIR. Data
68  * that may have been received gets lost here.
69  */
70 static void
71 ns8250_clrint(struct uart_bas *bas)
72 {
73         uint8_t iir, lsr;
74
75         iir = uart_getreg(bas, REG_IIR);
76         while ((iir & IIR_NOPEND) == 0) {
77                 iir &= IIR_IMASK;
78                 if (iir == IIR_RLS) {
79                         lsr = uart_getreg(bas, REG_LSR);
80                         if (lsr & (LSR_BI|LSR_FE|LSR_PE))
81                                 (void)uart_getreg(bas, REG_DATA);
82                 } else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
83                         (void)uart_getreg(bas, REG_DATA);
84                 else if (iir == IIR_MLSC)
85                         (void)uart_getreg(bas, REG_MSR);
86                 uart_barrier(bas);
87                 iir = uart_getreg(bas, REG_IIR);
88         }
89 }
90
91 static int
92 ns8250_delay(struct uart_bas *bas)
93 {
94         int divisor;
95         u_char lcr;
96
97         lcr = uart_getreg(bas, REG_LCR);
98         uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
99         uart_barrier(bas);
100         divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
101         uart_barrier(bas);
102         uart_setreg(bas, REG_LCR, lcr);
103         uart_barrier(bas);
104
105         /* 1/10th the time to transmit 1 character (estimate). */
106         if (divisor <= 134)
107                 return (16000000 * divisor / bas->rclk);
108         return (16000 * divisor / (bas->rclk / 1000));
109 }
110
111 static int
112 ns8250_divisor(int rclk, int baudrate)
113 {
114         int actual_baud, divisor;
115         int error;
116
117         if (baudrate == 0)
118                 return (0);
119
120         divisor = (rclk / (baudrate << 3) + 1) >> 1;
121         if (divisor == 0 || divisor >= 65536)
122                 return (0);
123         actual_baud = rclk / (divisor << 4);
124
125         /* 10 times error in percent: */
126         error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
127
128         /* 3.0% maximum error tolerance: */
129         if (error < -30 || error > 30)
130                 return (0);
131
132         return (divisor);
133 }
134
135 static int
136 ns8250_drain(struct uart_bas *bas, int what)
137 {
138         int delay, limit;
139
140         delay = ns8250_delay(bas);
141
142         if (what & UART_DRAIN_TRANSMITTER) {
143                 /*
144                  * Pick an arbitrary high limit to avoid getting stuck in
145                  * an infinite loop when the hardware is broken. Make the
146                  * limit high enough to handle large FIFOs.
147                  */
148                 limit = 10*1024;
149                 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
150                         DELAY(delay);
151                 if (limit == 0) {
152                         /* printf("ns8250: transmitter appears stuck... "); */
153                         return (EIO);
154                 }
155         }
156
157         if (what & UART_DRAIN_RECEIVER) {
158                 /*
159                  * Pick an arbitrary high limit to avoid getting stuck in
160                  * an infinite loop when the hardware is broken. Make the
161                  * limit high enough to handle large FIFOs and integrated
162                  * UARTs. The HP rx2600 for example has 3 UARTs on the
163                  * management board that tend to get a lot of data send
164                  * to it when the UART is first activated.
165                  */
166                 limit=10*4096;
167                 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
168                         (void)uart_getreg(bas, REG_DATA);
169                         uart_barrier(bas);
170                         DELAY(delay << 2);
171                 }
172                 if (limit == 0) {
173                         /* printf("ns8250: receiver appears broken... "); */
174                         return (EIO);
175                 }
176         }
177
178         return (0);
179 }
180
181 /*
182  * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
183  * drained. WARNING: this function clobbers the FIFO setting!
184  */
185 static void
186 ns8250_flush(struct uart_bas *bas, int what)
187 {
188         uint8_t fcr;
189
190         fcr = FCR_ENABLE;
191 #ifdef CPU_XBURST
192         fcr |= FCR_UART_ON;
193 #endif
194         if (what & UART_FLUSH_TRANSMITTER)
195                 fcr |= FCR_XMT_RST;
196         if (what & UART_FLUSH_RECEIVER)
197                 fcr |= FCR_RCV_RST;
198         uart_setreg(bas, REG_FCR, fcr);
199         uart_barrier(bas);
200 }
201
202 static int
203 ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
204     int parity)
205 {
206         int divisor;
207         uint8_t lcr;
208
209         lcr = 0;
210         if (databits >= 8)
211                 lcr |= LCR_8BITS;
212         else if (databits == 7)
213                 lcr |= LCR_7BITS;
214         else if (databits == 6)
215                 lcr |= LCR_6BITS;
216         else
217                 lcr |= LCR_5BITS;
218         if (stopbits > 1)
219                 lcr |= LCR_STOPB;
220         lcr |= parity << 3;
221
222         /* Set baudrate. */
223         if (baudrate > 0) {
224                 divisor = ns8250_divisor(bas->rclk, baudrate);
225                 if (divisor == 0)
226                         return (EINVAL);
227                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
228                 uart_barrier(bas);
229                 uart_setreg(bas, REG_DLL, divisor & 0xff);
230                 uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
231                 uart_barrier(bas);
232         }
233
234         /* Set LCR and clear DLAB. */
235         uart_setreg(bas, REG_LCR, lcr);
236         uart_barrier(bas);
237         return (0);
238 }
239
240 /*
241  * Low-level UART interface.
242  */
243 static int ns8250_probe(struct uart_bas *bas);
244 static void ns8250_init(struct uart_bas *bas, int, int, int, int);
245 static void ns8250_term(struct uart_bas *bas);
246 static void ns8250_putc(struct uart_bas *bas, int);
247 static int ns8250_rxready(struct uart_bas *bas);
248 static int ns8250_getc(struct uart_bas *bas, struct mtx *);
249
250 struct uart_ops uart_ns8250_ops = {
251         .probe = ns8250_probe,
252         .init = ns8250_init,
253         .term = ns8250_term,
254         .putc = ns8250_putc,
255         .rxready = ns8250_rxready,
256         .getc = ns8250_getc,
257 };
258
259 static int
260 ns8250_probe(struct uart_bas *bas)
261 {
262         u_char val;
263
264 #ifdef CPU_XBURST
265         uart_setreg(bas, REG_FCR, FCR_UART_ON);
266 #endif
267
268         /* Check known 0 bits that don't depend on DLAB. */
269         val = uart_getreg(bas, REG_IIR);
270         if (val & 0x30)
271                 return (ENXIO);
272         /*
273          * Bit 6 of the MCR (= 0x40) appears to be 1 for the Sun1699
274          * chip, but otherwise doesn't seem to have a function. In
275          * other words, uart(4) works regardless. Ignore that bit so
276          * the probe succeeds.
277          */
278         val = uart_getreg(bas, REG_MCR);
279         if (val & 0xa0)
280                 return (ENXIO);
281
282         return (0);
283 }
284
285 static void
286 ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
287     int parity)
288 {
289         u_char ier, val;
290
291         if (bas->rclk == 0)
292                 bas->rclk = DEFAULT_RCLK;
293         ns8250_param(bas, baudrate, databits, stopbits, parity);
294
295         /* Disable all interrupt sources. */
296         /*
297          * We use 0xe0 instead of 0xf0 as the mask because the XScale PXA
298          * UARTs split the receive time-out interrupt bit out separately as
299          * 0x10.  This gets handled by ier_mask and ier_rxbits below.
300          */
301         ier = uart_getreg(bas, REG_IER) & 0xe0;
302         uart_setreg(bas, REG_IER, ier);
303         uart_barrier(bas);
304
305         /* Disable the FIFO (if present). */
306         val = 0;
307 #ifdef CPU_XBURST
308         val |= FCR_UART_ON;
309 #endif
310         uart_setreg(bas, REG_FCR, val);
311         uart_barrier(bas);
312
313         /* Set RTS & DTR. */
314         uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
315         uart_barrier(bas);
316
317         ns8250_clrint(bas);
318 }
319
320 static void
321 ns8250_term(struct uart_bas *bas)
322 {
323
324         /* Clear RTS & DTR. */
325         uart_setreg(bas, REG_MCR, MCR_IE);
326         uart_barrier(bas);
327 }
328
329 static void
330 ns8250_putc(struct uart_bas *bas, int c)
331 {
332         int limit;
333
334         limit = 250000;
335         while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
336                 DELAY(4);
337         uart_setreg(bas, REG_DATA, c);
338         uart_barrier(bas);
339 }
340
341 static int
342 ns8250_rxready(struct uart_bas *bas)
343 {
344
345         return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
346 }
347
348 static int
349 ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
350 {
351         int c;
352
353         uart_lock(hwmtx);
354
355         while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
356                 uart_unlock(hwmtx);
357                 DELAY(4);
358                 uart_lock(hwmtx);
359         }
360
361         c = uart_getreg(bas, REG_DATA);
362
363         uart_unlock(hwmtx);
364
365         return (c);
366 }
367
368 static 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         KOBJMETHOD(uart_grab,           ns8250_bus_grab),
381         KOBJMETHOD(uart_ungrab,         ns8250_bus_ungrab),
382         { 0, 0 }
383 };
384
385 struct uart_class uart_ns8250_class = {
386         "ns8250",
387         ns8250_methods,
388         sizeof(struct ns8250_softc),
389         .uc_ops = &uart_ns8250_ops,
390         .uc_range = 8,
391         .uc_rclk = DEFAULT_RCLK
392 };
393
394 #ifdef FDT
395 static struct ofw_compat_data compat_data[] = {
396         {"ns16550",             (uintptr_t)&uart_ns8250_class},
397         {NULL,                  (uintptr_t)NULL},
398 };
399 UART_FDT_CLASS_AND_DEVICE(compat_data);
400 #endif
401
402 /* Use token-pasting to form SER_ and MSR_ named constants. */
403 #define SER(sig)        SER_##sig
404 #define SERD(sig)       SER_D##sig
405 #define MSR(sig)        MSR_##sig
406 #define MSRD(sig)       MSR_D##sig
407
408 /*
409  * Detect signal changes using software delta detection.  The previous state of
410  * the signals is in 'var' the new hardware state is in 'msr', and 'sig' is the
411  * short name (DCD, CTS, etc) of the signal bit being processed; 'var' gets the
412  * new state of both the signal and the delta bits.
413  */
414 #define SIGCHGSW(var, msr, sig)                                 \
415         if ((msr) & MSR(sig)) {                                 \
416                 if ((var & SER(sig)) == 0)                      \
417                         var |= SERD(sig) | SER(sig);            \
418         } else {                                                \
419                 if ((var & SER(sig)) != 0)                      \
420                         var = SERD(sig) | (var & ~SER(sig));    \
421         }
422
423 /*
424  * Detect signal changes using the hardware msr delta bits.  This is currently
425  * used only when PPS timing information is being captured using the "narrow
426  * pulse" option.  With a narrow PPS pulse the signal may not still be asserted
427  * by time the interrupt handler is invoked.  The hardware will latch the fact
428  * that it changed in the delta bits.
429  */
430 #define SIGCHGHW(var, msr, sig)                                 \
431         if ((msr) & MSRD(sig)) {                                \
432                 if (((msr) & MSR(sig)) != 0)                    \
433                         var |= SERD(sig) | SER(sig);            \
434                 else                                            \
435                         var = SERD(sig) | (var & ~SER(sig));    \
436         }
437
438 int
439 ns8250_bus_attach(struct uart_softc *sc)
440 {
441         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
442         struct uart_bas *bas;
443         unsigned int ivar;
444 #ifdef FDT
445         phandle_t node;
446         pcell_t cell;
447 #endif
448
449         ns8250->busy_detect = 0;
450
451 #ifdef FDT
452         /* 
453          * Check whether uart requires to read USR reg when IIR_BUSY and 
454          * has broken txfifo. 
455          */
456         node = ofw_bus_get_node(sc->sc_dev);
457         if ((OF_getprop(node, "busy-detect", &cell, sizeof(cell))) > 0)
458                 ns8250->busy_detect = 1;
459         if ((OF_getprop(node, "broken-txfifo", &cell, sizeof(cell))) > 0)
460                 broken_txfifo = 1;
461 #endif
462
463         bas = &sc->sc_bas;
464
465         ns8250->mcr = uart_getreg(bas, REG_MCR);
466         ns8250->fcr = FCR_ENABLE;
467 #ifdef CPU_XBURST
468         ns8250->fcr |= FCR_UART_ON;
469 #endif
470         if (!resource_int_value("uart", device_get_unit(sc->sc_dev), "flags",
471             &ivar)) {
472                 if (UART_FLAGS_FCR_RX_LOW(ivar)) 
473                         ns8250->fcr |= FCR_RX_LOW;
474                 else if (UART_FLAGS_FCR_RX_MEDL(ivar)) 
475                         ns8250->fcr |= FCR_RX_MEDL;
476                 else if (UART_FLAGS_FCR_RX_HIGH(ivar)) 
477                         ns8250->fcr |= FCR_RX_HIGH;
478                 else
479                         ns8250->fcr |= FCR_RX_MEDH;
480         } else 
481                 ns8250->fcr |= FCR_RX_MEDH;
482         
483         /* Get IER mask */
484         ivar = 0xf0;
485         resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_mask",
486             &ivar);
487         ns8250->ier_mask = (uint8_t)(ivar & 0xff);
488         
489         /* Get IER RX interrupt bits */
490         ivar = IER_EMSC | IER_ERLS | IER_ERXRDY;
491         resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_rxbits",
492             &ivar);
493         ns8250->ier_rxbits = (uint8_t)(ivar & 0xff);
494         
495         uart_setreg(bas, REG_FCR, ns8250->fcr);
496         uart_barrier(bas);
497         ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
498
499         if (ns8250->mcr & MCR_DTR)
500                 sc->sc_hwsig |= SER_DTR;
501         if (ns8250->mcr & MCR_RTS)
502                 sc->sc_hwsig |= SER_RTS;
503         ns8250_bus_getsig(sc);
504
505         ns8250_clrint(bas);
506         ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
507         ns8250->ier |= ns8250->ier_rxbits;
508         uart_setreg(bas, REG_IER, ns8250->ier);
509         uart_barrier(bas);
510
511         /*
512          * Timing of the H/W access was changed with r253161 of uart_core.c
513          * It has been observed that an ITE IT8513E would signal a break
514          * condition with pretty much every character it received, unless
515          * it had enough time to settle between ns8250_bus_attach() and
516          * ns8250_bus_ipend() -- which it accidentally had before r253161.
517          * It's not understood why the UART chip behaves this way and it
518          * could very well be that the DELAY make the H/W work in the same
519          * accidental manner as before. More analysis is warranted, but
520          * at least now we fixed a known regression.
521          */
522         DELAY(200);
523         return (0);
524 }
525
526 int
527 ns8250_bus_detach(struct uart_softc *sc)
528 {
529         struct ns8250_softc *ns8250;
530         struct uart_bas *bas;
531         u_char ier;
532
533         ns8250 = (struct ns8250_softc *)sc;
534         bas = &sc->sc_bas;
535         ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
536         uart_setreg(bas, REG_IER, ier);
537         uart_barrier(bas);
538         ns8250_clrint(bas);
539         return (0);
540 }
541
542 int
543 ns8250_bus_flush(struct uart_softc *sc, int what)
544 {
545         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
546         struct uart_bas *bas;
547         int error;
548
549         bas = &sc->sc_bas;
550         uart_lock(sc->sc_hwmtx);
551         if (sc->sc_rxfifosz > 1) {
552                 ns8250_flush(bas, what);
553                 uart_setreg(bas, REG_FCR, ns8250->fcr);
554                 uart_barrier(bas);
555                 error = 0;
556         } else
557                 error = ns8250_drain(bas, what);
558         uart_unlock(sc->sc_hwmtx);
559         return (error);
560 }
561
562 int
563 ns8250_bus_getsig(struct uart_softc *sc)
564 {
565         uint32_t old, sig;
566         uint8_t msr;
567
568         /*
569          * The delta bits are reputed to be broken on some hardware, so use
570          * software delta detection by default.  Use the hardware delta bits
571          * when capturing PPS pulses which are too narrow for software detection
572          * to see the edges.  Hardware delta for RI doesn't work like the
573          * others, so always use software for it.  Other threads may be changing
574          * other (non-MSR) bits in sc_hwsig, so loop until it can succesfully
575          * update without other changes happening.  Note that the SIGCHGxx()
576          * macros carefully preserve the delta bits when we have to loop several
577          * times and a signal transitions between iterations.
578          */
579         do {
580                 old = sc->sc_hwsig;
581                 sig = old;
582                 uart_lock(sc->sc_hwmtx);
583                 msr = uart_getreg(&sc->sc_bas, REG_MSR);
584                 uart_unlock(sc->sc_hwmtx);
585                 if (sc->sc_pps_mode & UART_PPS_NARROW_PULSE) {
586                         SIGCHGHW(sig, msr, DSR);
587                         SIGCHGHW(sig, msr, CTS);
588                         SIGCHGHW(sig, msr, DCD);
589                 } else {
590                         SIGCHGSW(sig, msr, DSR);
591                         SIGCHGSW(sig, msr, CTS);
592                         SIGCHGSW(sig, msr, DCD);
593                 }
594                 SIGCHGSW(sig, msr, RI);
595         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, sig & ~SER_MASK_DELTA));
596         return (sig);
597 }
598
599 int
600 ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
601 {
602         struct uart_bas *bas;
603         int baudrate, divisor, error;
604         uint8_t efr, lcr;
605
606         bas = &sc->sc_bas;
607         error = 0;
608         uart_lock(sc->sc_hwmtx);
609         switch (request) {
610         case UART_IOCTL_BREAK:
611                 lcr = uart_getreg(bas, REG_LCR);
612                 if (data)
613                         lcr |= LCR_SBREAK;
614                 else
615                         lcr &= ~LCR_SBREAK;
616                 uart_setreg(bas, REG_LCR, lcr);
617                 uart_barrier(bas);
618                 break;
619         case UART_IOCTL_IFLOW:
620                 lcr = uart_getreg(bas, REG_LCR);
621                 uart_barrier(bas);
622                 uart_setreg(bas, REG_LCR, 0xbf);
623                 uart_barrier(bas);
624                 efr = uart_getreg(bas, REG_EFR);
625                 if (data)
626                         efr |= EFR_RTS;
627                 else
628                         efr &= ~EFR_RTS;
629                 uart_setreg(bas, REG_EFR, efr);
630                 uart_barrier(bas);
631                 uart_setreg(bas, REG_LCR, lcr);
632                 uart_barrier(bas);
633                 break;
634         case UART_IOCTL_OFLOW:
635                 lcr = uart_getreg(bas, REG_LCR);
636                 uart_barrier(bas);
637                 uart_setreg(bas, REG_LCR, 0xbf);
638                 uart_barrier(bas);
639                 efr = uart_getreg(bas, REG_EFR);
640                 if (data)
641                         efr |= EFR_CTS;
642                 else
643                         efr &= ~EFR_CTS;
644                 uart_setreg(bas, REG_EFR, efr);
645                 uart_barrier(bas);
646                 uart_setreg(bas, REG_LCR, lcr);
647                 uart_barrier(bas);
648                 break;
649         case UART_IOCTL_BAUD:
650                 lcr = uart_getreg(bas, REG_LCR);
651                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
652                 uart_barrier(bas);
653                 divisor = uart_getreg(bas, REG_DLL) |
654                     (uart_getreg(bas, REG_DLH) << 8);
655                 uart_barrier(bas);
656                 uart_setreg(bas, REG_LCR, lcr);
657                 uart_barrier(bas);
658                 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
659                 if (baudrate > 0)
660                         *(int*)data = baudrate;
661                 else
662                         error = ENXIO;
663                 break;
664         default:
665                 error = EINVAL;
666                 break;
667         }
668         uart_unlock(sc->sc_hwmtx);
669         return (error);
670 }
671
672 int
673 ns8250_bus_ipend(struct uart_softc *sc)
674 {
675         struct uart_bas *bas;
676         struct ns8250_softc *ns8250;
677         int ipend;
678         uint8_t iir, lsr;
679
680         ns8250 = (struct ns8250_softc *)sc;
681         bas = &sc->sc_bas;
682         uart_lock(sc->sc_hwmtx);
683         iir = uart_getreg(bas, REG_IIR);
684
685         if (ns8250->busy_detect && (iir & IIR_BUSY) == IIR_BUSY) {
686                 (void)uart_getreg(bas, DW_REG_USR);
687                 uart_unlock(sc->sc_hwmtx);
688                 return (0);
689         }
690         if (iir & IIR_NOPEND) {
691                 uart_unlock(sc->sc_hwmtx);
692                 return (0);
693         }
694         ipend = 0;
695         if (iir & IIR_RXRDY) {
696                 lsr = uart_getreg(bas, REG_LSR);
697                 if (lsr & LSR_OE)
698                         ipend |= SER_INT_OVERRUN;
699                 if (lsr & LSR_BI)
700                         ipend |= SER_INT_BREAK;
701                 if (lsr & LSR_RXRDY)
702                         ipend |= SER_INT_RXREADY;
703         } else {
704                 if (iir & IIR_TXRDY) {
705                         ipend |= SER_INT_TXIDLE;
706                         uart_setreg(bas, REG_IER, ns8250->ier);
707                 } else
708                         ipend |= SER_INT_SIGCHG;
709         }
710         if (ipend == 0)
711                 ns8250_clrint(bas);
712         uart_unlock(sc->sc_hwmtx);
713         return (ipend);
714 }
715
716 int
717 ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
718     int stopbits, int parity)
719 {
720         struct ns8250_softc *ns8250;
721         struct uart_bas *bas;
722         int error, limit;
723
724         ns8250 = (struct ns8250_softc*)sc;
725         bas = &sc->sc_bas;
726         uart_lock(sc->sc_hwmtx);
727         /*
728          * When using DW UART with BUSY detection it is necessary to wait
729          * until all serial transfers are finished before manipulating the
730          * line control. LCR will not be affected when UART is busy.
731          */
732         if (ns8250->busy_detect != 0) {
733                 /*
734                  * Pick an arbitrary high limit to avoid getting stuck in
735                  * an infinite loop in case when the hardware is broken.
736                  */
737                 limit = 10 * 1024;
738                 while (((uart_getreg(bas, DW_REG_USR) & USR_BUSY) != 0) &&
739                     --limit)
740                         DELAY(4);
741
742                 if (limit <= 0) {
743                         /* UART appears to be stuck */
744                         uart_unlock(sc->sc_hwmtx);
745                         return (EIO);
746                 }
747         }
748
749         error = ns8250_param(bas, baudrate, databits, stopbits, parity);
750         uart_unlock(sc->sc_hwmtx);
751         return (error);
752 }
753
754 int
755 ns8250_bus_probe(struct uart_softc *sc)
756 {
757         struct ns8250_softc *ns8250;
758         struct uart_bas *bas;
759         int count, delay, error, limit;
760         uint8_t lsr, mcr, ier;
761         uint8_t val;
762
763         ns8250 = (struct ns8250_softc *)sc;
764         bas = &sc->sc_bas;
765
766         error = ns8250_probe(bas);
767         if (error)
768                 return (error);
769
770         mcr = MCR_IE;
771         if (sc->sc_sysdev == NULL) {
772                 /* By using ns8250_init() we also set DTR and RTS. */
773                 ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE);
774         } else
775                 mcr |= MCR_DTR | MCR_RTS;
776
777         error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
778         if (error)
779                 return (error);
780
781         /*
782          * Set loopback mode. This avoids having garbage on the wire and
783          * also allows us send and receive data. We set DTR and RTS to
784          * avoid the possibility that automatic flow-control prevents
785          * any data from being sent.
786          */
787         uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
788         uart_barrier(bas);
789
790         /*
791          * Enable FIFOs. And check that the UART has them. If not, we're
792          * done. Since this is the first time we enable the FIFOs, we reset
793          * them.
794          */
795         val = FCR_ENABLE;
796 #ifdef CPU_XBURST
797         val |= FCR_UART_ON;
798 #endif
799         uart_setreg(bas, REG_FCR, val);
800         uart_barrier(bas);
801         if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
802                 /*
803                  * NS16450 or INS8250. We don't bother to differentiate
804                  * between them. They're too old to be interesting.
805                  */
806                 uart_setreg(bas, REG_MCR, mcr);
807                 uart_barrier(bas);
808                 sc->sc_rxfifosz = sc->sc_txfifosz = 1;
809                 device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
810                 return (0);
811         }
812
813         val = FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST;
814 #ifdef CPU_XBURST
815         val |= FCR_UART_ON;
816 #endif
817         uart_setreg(bas, REG_FCR, val);
818         uart_barrier(bas);
819
820         count = 0;
821         delay = ns8250_delay(bas);
822
823         /* We have FIFOs. Drain the transmitter and receiver. */
824         error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
825         if (error) {
826                 uart_setreg(bas, REG_MCR, mcr);
827                 val = 0;
828 #ifdef CPU_XBURST
829                 val |= FCR_UART_ON;
830 #endif
831                 uart_setreg(bas, REG_FCR, val);
832                 uart_barrier(bas);
833                 goto describe;
834         }
835
836         /*
837          * We should have a sufficiently clean "pipe" to determine the
838          * size of the FIFOs. We send as much characters as is reasonable
839          * and wait for the overflow bit in the LSR register to be
840          * asserted, counting the characters as we send them. Based on
841          * that count we know the FIFO size.
842          */
843         do {
844                 uart_setreg(bas, REG_DATA, 0);
845                 uart_barrier(bas);
846                 count++;
847
848                 limit = 30;
849                 lsr = 0;
850                 /*
851                  * LSR bits are cleared upon read, so we must accumulate
852                  * them to be able to test LSR_OE below.
853                  */
854                 while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
855                     --limit)
856                         DELAY(delay);
857                 if (limit == 0) {
858                         ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
859                         uart_setreg(bas, REG_IER, ier);
860                         uart_setreg(bas, REG_MCR, mcr);
861                         val = 0;
862 #ifdef CPU_XBURST
863                         val |= FCR_UART_ON;
864 #endif
865                         uart_setreg(bas, REG_FCR, val);
866                         uart_barrier(bas);
867                         count = 0;
868                         goto describe;
869                 }
870         } while ((lsr & LSR_OE) == 0 && count < 130);
871         count--;
872
873         uart_setreg(bas, REG_MCR, mcr);
874
875         /* Reset FIFOs. */
876         ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
877
878  describe:
879         if (count >= 14 && count <= 16) {
880                 sc->sc_rxfifosz = 16;
881                 device_set_desc(sc->sc_dev, "16550 or compatible");
882         } else if (count >= 28 && count <= 32) {
883                 sc->sc_rxfifosz = 32;
884                 device_set_desc(sc->sc_dev, "16650 or compatible");
885         } else if (count >= 56 && count <= 64) {
886                 sc->sc_rxfifosz = 64;
887                 device_set_desc(sc->sc_dev, "16750 or compatible");
888         } else if (count >= 112 && count <= 128) {
889                 sc->sc_rxfifosz = 128;
890                 device_set_desc(sc->sc_dev, "16950 or compatible");
891         } else {
892                 sc->sc_rxfifosz = 16;
893                 device_set_desc(sc->sc_dev,
894                     "Non-standard ns8250 class UART with FIFOs");
895         }
896
897         /*
898          * Force the Tx FIFO size to 16 bytes for now. We don't program the
899          * Tx trigger. Also, we assume that all data has been sent when the
900          * interrupt happens.
901          */
902         sc->sc_txfifosz = 16;
903
904 #if 0
905         /*
906          * XXX there are some issues related to hardware flow control and
907          * it's likely that uart(4) is the cause. This basicly needs more
908          * investigation, but we avoid using for hardware flow control
909          * until then.
910          */
911         /* 16650s or higher have automatic flow control. */
912         if (sc->sc_rxfifosz > 16) {
913                 sc->sc_hwiflow = 1;
914                 sc->sc_hwoflow = 1;
915         }
916 #endif
917
918         return (0);
919 }
920
921 int
922 ns8250_bus_receive(struct uart_softc *sc)
923 {
924         struct uart_bas *bas;
925         int xc;
926         uint8_t lsr;
927
928         bas = &sc->sc_bas;
929         uart_lock(sc->sc_hwmtx);
930         lsr = uart_getreg(bas, REG_LSR);
931         while (lsr & LSR_RXRDY) {
932                 if (uart_rx_full(sc)) {
933                         sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
934                         break;
935                 }
936                 xc = uart_getreg(bas, REG_DATA);
937                 if (lsr & LSR_FE)
938                         xc |= UART_STAT_FRAMERR;
939                 if (lsr & LSR_PE)
940                         xc |= UART_STAT_PARERR;
941                 uart_rx_put(sc, xc);
942                 lsr = uart_getreg(bas, REG_LSR);
943         }
944         /* Discard everything left in the Rx FIFO. */
945         while (lsr & LSR_RXRDY) {
946                 (void)uart_getreg(bas, REG_DATA);
947                 uart_barrier(bas);
948                 lsr = uart_getreg(bas, REG_LSR);
949         }
950         uart_unlock(sc->sc_hwmtx);
951         return (0);
952 }
953
954 int
955 ns8250_bus_setsig(struct uart_softc *sc, int sig)
956 {
957         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
958         struct uart_bas *bas;
959         uint32_t new, old;
960
961         bas = &sc->sc_bas;
962         do {
963                 old = sc->sc_hwsig;
964                 new = old;
965                 if (sig & SER_DDTR) {
966                         new = (new & ~SER_DTR) | (sig & (SER_DTR | SER_DDTR));
967                 }
968                 if (sig & SER_DRTS) {
969                         new = (new & ~SER_RTS) | (sig & (SER_RTS | SER_DRTS));
970                 }
971         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
972         uart_lock(sc->sc_hwmtx);
973         ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
974         if (new & SER_DTR)
975                 ns8250->mcr |= MCR_DTR;
976         if (new & SER_RTS)
977                 ns8250->mcr |= MCR_RTS;
978         uart_setreg(bas, REG_MCR, ns8250->mcr);
979         uart_barrier(bas);
980         uart_unlock(sc->sc_hwmtx);
981         return (0);
982 }
983
984 int
985 ns8250_bus_transmit(struct uart_softc *sc)
986 {
987         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
988         struct uart_bas *bas;
989         int i;
990
991         bas = &sc->sc_bas;
992         uart_lock(sc->sc_hwmtx);
993         if (sc->sc_txdatasz > 1) {
994                 if ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0)
995                         ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
996         } else {
997                 while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
998                         DELAY(4);
999         }
1000         uart_setreg(bas, REG_IER, ns8250->ier | IER_ETXRDY);
1001         uart_barrier(bas);
1002         for (i = 0; i < sc->sc_txdatasz; i++) {
1003                 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
1004                 uart_barrier(bas);
1005         }
1006         if (broken_txfifo)
1007                 ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
1008         else
1009                 sc->sc_txbusy = 1;
1010         uart_unlock(sc->sc_hwmtx);
1011         if (broken_txfifo)
1012                 uart_sched_softih(sc, SER_INT_TXIDLE);
1013         return (0);
1014 }
1015
1016 void
1017 ns8250_bus_grab(struct uart_softc *sc)
1018 {
1019         struct uart_bas *bas = &sc->sc_bas;
1020
1021         /*
1022          * turn off all interrupts to enter polling mode. Leave the
1023          * saved mask alone. We'll restore whatever it was in ungrab.
1024          * All pending interupt signals are reset when IER is set to 0.
1025          */
1026         uart_lock(sc->sc_hwmtx);
1027         uart_setreg(bas, REG_IER, 0);
1028         uart_barrier(bas);
1029         uart_unlock(sc->sc_hwmtx);
1030 }
1031
1032 void
1033 ns8250_bus_ungrab(struct uart_softc *sc)
1034 {
1035         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
1036         struct uart_bas *bas = &sc->sc_bas;
1037
1038         /*
1039          * Restore previous interrupt mask
1040          */
1041         uart_lock(sc->sc_hwmtx);
1042         uart_setreg(bas, REG_IER, ns8250->ier);
1043         uart_barrier(bas);
1044         uart_unlock(sc->sc_hwmtx);
1045 }