]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/uart/uart_dev_ns8250.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <machine/bus.h>
35
36 #include <dev/uart/uart.h>
37 #include <dev/uart/uart_cpu.h>
38 #include <dev/uart/uart_bus.h>
39
40 #include <dev/ic/ns16550.h>
41
42 #include "uart_if.h"
43
44 #define DEFAULT_RCLK    1843200
45
46 /*
47  * Clear pending interrupts. THRE is cleared by reading IIR. Data
48  * that may have been received gets lost here.
49  */
50 static void
51 ns8250_clrint(struct uart_bas *bas)
52 {
53         uint8_t iir;
54
55         iir = uart_getreg(bas, REG_IIR);
56         while ((iir & IIR_NOPEND) == 0) {
57                 iir &= IIR_IMASK;
58                 if (iir == IIR_RLS)
59                         (void)uart_getreg(bas, REG_LSR);
60                 else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
61                         (void)uart_getreg(bas, REG_DATA);
62                 else if (iir == IIR_MLSC)
63                         (void)uart_getreg(bas, REG_MSR);
64                 uart_barrier(bas);
65                 iir = uart_getreg(bas, REG_IIR);
66         }
67 }
68
69 static int
70 ns8250_delay(struct uart_bas *bas)
71 {
72         int divisor;
73         u_char lcr;
74
75         lcr = uart_getreg(bas, REG_LCR);
76         uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
77         uart_barrier(bas);
78         divisor = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
79         uart_barrier(bas);
80         uart_setreg(bas, REG_LCR, lcr);
81         uart_barrier(bas);
82
83         /* 1/10th the time to transmit 1 character (estimate). */
84         return (16000000 * divisor / bas->rclk);
85 }
86
87 static int
88 ns8250_divisor(int rclk, int baudrate)
89 {
90         int actual_baud, divisor;
91         int error;
92
93         if (baudrate == 0)
94                 return (0);
95
96         divisor = (rclk / (baudrate << 3) + 1) >> 1;
97         if (divisor == 0 || divisor >= 65536)
98                 return (0);
99         actual_baud = rclk / (divisor << 4);
100
101         /* 10 times error in percent: */
102         error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
103
104         /* 3.0% maximum error tolerance: */
105         if (error < -30 || error > 30)
106                 return (0);
107
108         return (divisor);
109 }
110
111 static int
112 ns8250_drain(struct uart_bas *bas, int what)
113 {
114         int delay, limit;
115
116         delay = ns8250_delay(bas);
117
118         if (what & UART_DRAIN_TRANSMITTER) {
119                 /*
120                  * Pick an arbitrary high limit to avoid getting stuck in
121                  * an infinite loop when the hardware is broken. Make the
122                  * limit high enough to handle large FIFOs.
123                  */
124                 limit = 10*1024;
125                 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
126                         DELAY(delay);
127                 if (limit == 0) {
128                         /* printf("ns8250: transmitter appears stuck... "); */
129                         return (EIO);
130                 }
131         }
132
133         if (what & UART_DRAIN_RECEIVER) {
134                 /*
135                  * Pick an arbitrary high limit to avoid getting stuck in
136                  * an infinite loop when the hardware is broken. Make the
137                  * limit high enough to handle large FIFOs and integrated
138                  * UARTs. The HP rx2600 for example has 3 UARTs on the
139                  * management board that tend to get a lot of data send
140                  * to it when the UART is first activated.
141                  */
142                 limit=10*4096;
143                 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
144                         (void)uart_getreg(bas, REG_DATA);
145                         uart_barrier(bas);
146                         DELAY(delay << 2);
147                 }
148                 if (limit == 0) {
149                         /* printf("ns8250: receiver appears broken... "); */
150                         return (EIO);
151                 }
152         }
153
154         return (0);
155 }
156
157 /*
158  * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
159  * drained. WARNING: this function clobbers the FIFO setting!
160  */
161 static void
162 ns8250_flush(struct uart_bas *bas, int what)
163 {
164         uint8_t fcr;
165
166         fcr = FCR_ENABLE;
167         if (what & UART_FLUSH_TRANSMITTER)
168                 fcr |= FCR_XMT_RST;
169         if (what & UART_FLUSH_RECEIVER)
170                 fcr |= FCR_RCV_RST;
171         uart_setreg(bas, REG_FCR, fcr);
172         uart_barrier(bas);
173 }
174
175 static int
176 ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
177     int parity)
178 {
179         int divisor;
180         uint8_t lcr;
181
182         lcr = 0;
183         if (databits >= 8)
184                 lcr |= LCR_8BITS;
185         else if (databits == 7)
186                 lcr |= LCR_7BITS;
187         else if (databits == 6)
188                 lcr |= LCR_6BITS;
189         else
190                 lcr |= LCR_5BITS;
191         if (stopbits > 1)
192                 lcr |= LCR_STOPB;
193         lcr |= parity << 3;
194
195         /* Set baudrate. */
196         if (baudrate > 0) {
197                 divisor = ns8250_divisor(bas->rclk, baudrate);
198                 if (divisor == 0)
199                         return (EINVAL);
200                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
201                 uart_barrier(bas);
202                 uart_setreg(bas, REG_DLL, divisor & 0xff);
203                 uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
204                 uart_barrier(bas);
205         }
206
207         /* Set LCR and clear DLAB. */
208         uart_setreg(bas, REG_LCR, lcr);
209         uart_barrier(bas);
210         return (0);
211 }
212
213 /*
214  * Low-level UART interface.
215  */
216 static int ns8250_probe(struct uart_bas *bas);
217 static void ns8250_init(struct uart_bas *bas, int, int, int, int);
218 static void ns8250_term(struct uart_bas *bas);
219 static void ns8250_putc(struct uart_bas *bas, int);
220 static int ns8250_poll(struct uart_bas *bas);
221 static int ns8250_getc(struct uart_bas *bas);
222
223 struct uart_ops uart_ns8250_ops = {
224         .probe = ns8250_probe,
225         .init = ns8250_init,
226         .term = ns8250_term,
227         .putc = ns8250_putc,
228         .poll = ns8250_poll,
229         .getc = ns8250_getc,
230 };
231
232 static int
233 ns8250_probe(struct uart_bas *bas)
234 {
235         u_char val;
236
237         /* Check known 0 bits that don't depend on DLAB. */
238         val = uart_getreg(bas, REG_IIR);
239         if (val & 0x30)
240                 return (ENXIO);
241         val = uart_getreg(bas, REG_MCR);
242         if (val & 0xe0)
243                 return (ENXIO);
244
245         return (0);
246 }
247
248 static void
249 ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
250     int parity)
251 {
252         u_char  ier;
253
254         if (bas->rclk == 0)
255                 bas->rclk = DEFAULT_RCLK;
256         ns8250_param(bas, baudrate, databits, stopbits, parity);
257
258         /* Disable all interrupt sources. */
259         ier = uart_getreg(bas, REG_IER) & 0xf0;
260         uart_setreg(bas, REG_IER, ier);
261         uart_barrier(bas);
262
263         /* Disable the FIFO (if present). */
264         uart_setreg(bas, REG_FCR, 0);
265         uart_barrier(bas);
266
267         /* Set RTS & DTR. */
268         uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
269         uart_barrier(bas);
270
271         ns8250_clrint(bas);
272 }
273
274 static void
275 ns8250_term(struct uart_bas *bas)
276 {
277
278         /* Clear RTS & DTR. */
279         uart_setreg(bas, REG_MCR, MCR_IE);
280         uart_barrier(bas);
281 }
282
283 static void
284 ns8250_putc(struct uart_bas *bas, int c)
285 {
286         int delay, limit;
287
288         /* 1/10th the time to transmit 1 character (estimate). */
289         delay = ns8250_delay(bas);
290
291         limit = 20;
292         while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
293                 DELAY(delay);
294         uart_setreg(bas, REG_DATA, c);
295         uart_barrier(bas);
296         limit = 40;
297         while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
298                 DELAY(delay);
299 }
300
301 static int
302 ns8250_poll(struct uart_bas *bas)
303 {
304
305         if (uart_getreg(bas, REG_LSR) & LSR_RXRDY)
306                 return (uart_getreg(bas, REG_DATA));
307         return (-1);
308 }
309
310 static int
311 ns8250_getc(struct uart_bas *bas)
312 {
313         int delay;
314
315         /* 1/10th the time to transmit 1 character (estimate). */
316         delay = ns8250_delay(bas);
317
318         while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0)
319                 DELAY(delay);
320         return (uart_getreg(bas, REG_DATA));
321 }
322
323 /*
324  * High-level UART interface.
325  */
326 struct ns8250_softc {
327         struct uart_softc base;
328         uint8_t         fcr;
329         uint8_t         ier;
330         uint8_t         mcr;
331 };
332
333 static int ns8250_bus_attach(struct uart_softc *);
334 static int ns8250_bus_detach(struct uart_softc *);
335 static int ns8250_bus_flush(struct uart_softc *, int);
336 static int ns8250_bus_getsig(struct uart_softc *);
337 static int ns8250_bus_ioctl(struct uart_softc *, int, intptr_t);
338 static int ns8250_bus_ipend(struct uart_softc *);
339 static int ns8250_bus_param(struct uart_softc *, int, int, int, int);
340 static int ns8250_bus_probe(struct uart_softc *);
341 static int ns8250_bus_receive(struct uart_softc *);
342 static int ns8250_bus_setsig(struct uart_softc *, int);
343 static int ns8250_bus_transmit(struct uart_softc *);
344
345 static kobj_method_t ns8250_methods[] = {
346         KOBJMETHOD(uart_attach,         ns8250_bus_attach),
347         KOBJMETHOD(uart_detach,         ns8250_bus_detach),
348         KOBJMETHOD(uart_flush,          ns8250_bus_flush),
349         KOBJMETHOD(uart_getsig,         ns8250_bus_getsig),
350         KOBJMETHOD(uart_ioctl,          ns8250_bus_ioctl),
351         KOBJMETHOD(uart_ipend,          ns8250_bus_ipend),
352         KOBJMETHOD(uart_param,          ns8250_bus_param),
353         KOBJMETHOD(uart_probe,          ns8250_bus_probe),
354         KOBJMETHOD(uart_receive,        ns8250_bus_receive),
355         KOBJMETHOD(uart_setsig,         ns8250_bus_setsig),
356         KOBJMETHOD(uart_transmit,       ns8250_bus_transmit),
357         { 0, 0 }
358 };
359
360 struct uart_class uart_ns8250_class = {
361         "ns8250 class",
362         ns8250_methods,
363         sizeof(struct ns8250_softc),
364         .uc_range = 8,
365         .uc_rclk = DEFAULT_RCLK
366 };
367
368 #define SIGCHG(c, i, s, d)                              \
369         if (c) {                                        \
370                 i |= (i & s) ? s : s | d;               \
371         } else {                                        \
372                 i = (i & s) ? (i & ~s) | d : i;         \
373         }
374
375 static int
376 ns8250_bus_attach(struct uart_softc *sc)
377 {
378         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
379         struct uart_bas *bas;
380
381         bas = &sc->sc_bas;
382
383         ns8250->mcr = uart_getreg(bas, REG_MCR);
384         ns8250->fcr = FCR_ENABLE | FCR_RX_MEDH;
385         uart_setreg(bas, REG_FCR, ns8250->fcr);
386         uart_barrier(bas);
387         ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
388
389         if (ns8250->mcr & MCR_DTR)
390                 sc->sc_hwsig |= SER_DTR;
391         if (ns8250->mcr & MCR_RTS)
392                 sc->sc_hwsig |= SER_RTS;
393         ns8250_bus_getsig(sc);
394
395         ns8250_clrint(bas);
396         ns8250->ier = uart_getreg(bas, REG_IER) & 0xf0;
397         ns8250->ier |= IER_EMSC | IER_ERLS | IER_ERXRDY;
398         uart_setreg(bas, REG_IER, ns8250->ier);
399         uart_barrier(bas);
400         return (0);
401 }
402
403 static int
404 ns8250_bus_detach(struct uart_softc *sc)
405 {
406         struct uart_bas *bas;
407         u_char ier;
408
409         bas = &sc->sc_bas;
410         ier = uart_getreg(bas, REG_IER) & 0xf0;
411         uart_setreg(bas, REG_IER, ier);
412         uart_barrier(bas);
413         ns8250_clrint(bas);
414         return (0);
415 }
416
417 static int
418 ns8250_bus_flush(struct uart_softc *sc, int what)
419 {
420         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
421         struct uart_bas *bas;
422         int error;
423
424         bas = &sc->sc_bas;
425         mtx_lock_spin(&sc->sc_hwmtx);
426         if (sc->sc_hasfifo) {
427                 ns8250_flush(bas, what);
428                 uart_setreg(bas, REG_FCR, ns8250->fcr);
429                 uart_barrier(bas);
430                 error = 0;
431         } else
432                 error = ns8250_drain(bas, what);
433         mtx_unlock_spin(&sc->sc_hwmtx);
434         return (error);
435 }
436
437 static int
438 ns8250_bus_getsig(struct uart_softc *sc)
439 {
440         uint32_t new, old, sig;
441         uint8_t msr;
442
443         do {
444                 old = sc->sc_hwsig;
445                 sig = old;
446                 mtx_lock_spin(&sc->sc_hwmtx);
447                 msr = uart_getreg(&sc->sc_bas, REG_MSR);
448                 mtx_unlock_spin(&sc->sc_hwmtx);
449                 SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
450                 SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
451                 SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
452                 SIGCHG(msr & MSR_RI,  sig, SER_RI,  SER_DRI);
453                 new = sig & ~UART_SIGMASK_DELTA;
454         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
455         return (sig);
456 }
457
458 static int
459 ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
460 {
461         struct uart_bas *bas;
462         int baudrate, divisor, error;
463         uint8_t efr, lcr;
464
465         bas = &sc->sc_bas;
466         error = 0;
467         mtx_lock_spin(&sc->sc_hwmtx);
468         switch (request) {
469         case UART_IOCTL_BREAK:
470                 lcr = uart_getreg(bas, REG_LCR);
471                 if (data)
472                         lcr |= LCR_SBREAK;
473                 else
474                         lcr &= ~LCR_SBREAK;
475                 uart_setreg(bas, REG_LCR, lcr);
476                 uart_barrier(bas);
477                 break;
478         case UART_IOCTL_IFLOW:
479                 lcr = uart_getreg(bas, REG_LCR);
480                 uart_barrier(bas);
481                 uart_setreg(bas, REG_LCR, 0xbf);
482                 uart_barrier(bas);
483                 efr = uart_getreg(bas, REG_EFR);
484                 if (data)
485                         efr |= EFR_RTS;
486                 else
487                         efr &= ~EFR_RTS;
488                 uart_setreg(bas, REG_EFR, efr);
489                 uart_barrier(bas);
490                 uart_setreg(bas, REG_LCR, lcr);
491                 uart_barrier(bas);
492                 break;
493         case UART_IOCTL_OFLOW:
494                 lcr = uart_getreg(bas, REG_LCR);
495                 uart_barrier(bas);
496                 uart_setreg(bas, REG_LCR, 0xbf);
497                 uart_barrier(bas);
498                 efr = uart_getreg(bas, REG_EFR);
499                 if (data)
500                         efr |= EFR_CTS;
501                 else
502                         efr &= ~EFR_CTS;
503                 uart_setreg(bas, REG_EFR, efr);
504                 uart_barrier(bas);
505                 uart_setreg(bas, REG_LCR, lcr);
506                 uart_barrier(bas);
507                 break;
508         case UART_IOCTL_BAUD:
509                 lcr = uart_getreg(bas, REG_LCR);
510                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
511                 uart_barrier(bas);
512                 divisor = uart_getreg(bas, REG_DLL) |
513                     (uart_getreg(bas, REG_DLH) << 8);
514                 uart_barrier(bas);
515                 uart_setreg(bas, REG_LCR, lcr);
516                 uart_barrier(bas);
517                 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
518                 if (baudrate > 0)
519                         *(int*)data = baudrate;
520                 else
521                         error = ENXIO;
522                 break;
523         default:
524                 error = EINVAL;
525                 break;
526         }
527         mtx_unlock_spin(&sc->sc_hwmtx);
528         return (error);
529 }
530
531 static int
532 ns8250_bus_ipend(struct uart_softc *sc)
533 {
534         struct uart_bas *bas;
535         int ipend;
536         uint8_t iir, lsr;
537
538         bas = &sc->sc_bas;
539         mtx_lock_spin(&sc->sc_hwmtx);
540         iir = uart_getreg(bas, REG_IIR);
541         if (iir & IIR_NOPEND) {
542                 mtx_unlock_spin(&sc->sc_hwmtx);
543                 return (0);
544         }
545         ipend = 0;
546         if (iir & IIR_RXRDY) {
547                 lsr = uart_getreg(bas, REG_LSR);
548                 mtx_unlock_spin(&sc->sc_hwmtx);
549                 if (lsr & LSR_OE)
550                         ipend |= UART_IPEND_OVERRUN;
551                 if (lsr & LSR_BI)
552                         ipend |= UART_IPEND_BREAK;
553                 if (lsr & LSR_RXRDY)
554                         ipend |= UART_IPEND_RXREADY;
555         } else {
556                 mtx_unlock_spin(&sc->sc_hwmtx);
557                 if (iir & IIR_TXRDY)
558                         ipend |= UART_IPEND_TXIDLE;
559                 else
560                         ipend |= UART_IPEND_SIGCHG;
561         }
562         return ((sc->sc_leaving) ? 0 : ipend);
563 }
564
565 static int
566 ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
567     int stopbits, int parity)
568 {
569         struct uart_bas *bas;
570         int error;
571
572         bas = &sc->sc_bas;
573         mtx_lock_spin(&sc->sc_hwmtx);
574         error = ns8250_param(bas, baudrate, databits, stopbits, parity);
575         mtx_unlock_spin(&sc->sc_hwmtx);
576         return (error);
577 }
578
579 static int
580 ns8250_bus_probe(struct uart_softc *sc)
581 {
582         struct uart_bas *bas;
583         int count, delay, error, limit;
584         uint8_t lsr, mcr, ier;
585
586         bas = &sc->sc_bas;
587
588         error = ns8250_probe(bas);
589         if (error)
590                 return (error);
591
592         mcr = MCR_IE;
593         if (sc->sc_sysdev == NULL) {
594                 /* By using ns8250_init() we also set DTR and RTS. */
595                 ns8250_init(bas, 9600, 8, 1, UART_PARITY_NONE);
596         } else
597                 mcr |= MCR_DTR | MCR_RTS;
598
599         error = ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
600         if (error)
601                 return (error);
602
603         /*
604          * Set loopback mode. This avoids having garbage on the wire and
605          * also allows us send and receive data. We set DTR and RTS to
606          * avoid the possibility that automatic flow-control prevents
607          * any data from being sent.
608          */
609         uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
610         uart_barrier(bas);
611
612         /*
613          * Enable FIFOs. And check that the UART has them. If not, we're
614          * done. Since this is the first time we enable the FIFOs, we reset
615          * them.
616          */
617         uart_setreg(bas, REG_FCR, FCR_ENABLE);
618         uart_barrier(bas);
619         sc->sc_hasfifo = (uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK) ? 1 : 0;
620         if (!sc->sc_hasfifo) {
621                 /*
622                  * NS16450 or INS8250. We don't bother to differentiate
623                  * between them. They're too old to be interesting.
624                  */
625                 uart_setreg(bas, REG_MCR, mcr);
626                 uart_barrier(bas);
627                 device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
628                 return (0);
629         }
630
631         uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
632         uart_barrier(bas);
633
634         count = 0;
635         delay = ns8250_delay(bas);
636
637         /* We have FIFOs. Drain the transmitter and receiver. */
638         error = ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
639         if (error) {
640                 uart_setreg(bas, REG_MCR, mcr);
641                 uart_setreg(bas, REG_FCR, 0);
642                 uart_barrier(bas);
643                 goto describe;
644         }
645
646         /*
647          * We should have a sufficiently clean "pipe" to determine the
648          * size of the FIFOs. We send as much characters as is reasonable
649          * and wait for the the overflow bit in the LSR register to be
650          * asserted, counting the characters as we send them. Based on
651          * that count we know the FIFO size.
652          */
653         do {
654                 uart_setreg(bas, REG_DATA, 0);
655                 uart_barrier(bas);
656                 count++;
657
658                 limit = 30;
659                 lsr = 0;
660                 /*
661                  * LSR bits are cleared upon read, so we must accumulate
662                  * them to be able to test LSR_OE below.
663                  */
664                 while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
665                     --limit)
666                         DELAY(delay);
667                 if (limit == 0) {
668                         ier = uart_getreg(bas, REG_IER) & 0xf0;
669                         uart_setreg(bas, REG_IER, ier);
670                         uart_setreg(bas, REG_MCR, mcr);
671                         uart_setreg(bas, REG_FCR, 0);
672                         uart_barrier(bas);
673                         count = 0;
674                         goto describe;
675                 }
676         } while ((lsr & LSR_OE) == 0 && count < 130);
677         count--;
678
679         uart_setreg(bas, REG_MCR, mcr);
680
681         /* Reset FIFOs. */
682         ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
683
684  describe:
685         if (count >= 14 && count <= 16) {
686                 sc->sc_rxfifosz = 16;
687                 device_set_desc(sc->sc_dev, "16550 or compatible");
688         } else if (count >= 28 && count <= 32) {
689                 sc->sc_rxfifosz = 32;
690                 device_set_desc(sc->sc_dev, "16650 or compatible");
691         } else if (count >= 56 && count <= 64) {
692                 sc->sc_rxfifosz = 64;
693                 device_set_desc(sc->sc_dev, "16750 or compatible");
694         } else if (count >= 112 && count <= 128) {
695                 sc->sc_rxfifosz = 128;
696                 device_set_desc(sc->sc_dev, "16950 or compatible");
697         } else {
698                 sc->sc_rxfifosz = 16;
699                 device_set_desc(sc->sc_dev,
700                     "Non-standard ns8250 class UART with FIFOs");
701         }
702
703         /*
704          * Force the Tx FIFO size to 16 bytes for now. We don't program the
705          * Tx trigger. Also, we assume that all data has been sent when the
706          * interrupt happens.
707          */
708         sc->sc_txfifosz = 16;
709
710 #if 0
711         /*
712          * XXX there are some issues related to hardware flow control and
713          * it's likely that uart(4) is the cause. This basicly needs more
714          * investigation, but we avoid using for hardware flow control
715          * until then.
716          */
717         /* 16650s or higher have automatic flow control. */
718         if (sc->sc_rxfifosz > 16) {
719                 sc->sc_hwiflow = 1;
720                 sc->sc_hwoflow = 1;
721         }
722 #endif
723
724         return (0);
725 }
726
727 static int
728 ns8250_bus_receive(struct uart_softc *sc)
729 {
730         struct uart_bas *bas;
731         int xc;
732         uint8_t lsr;
733
734         bas = &sc->sc_bas;
735         mtx_lock_spin(&sc->sc_hwmtx);
736         lsr = uart_getreg(bas, REG_LSR);
737         while (lsr & LSR_RXRDY) {
738                 if (uart_rx_full(sc)) {
739                         sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
740                         break;
741                 }
742                 xc = uart_getreg(bas, REG_DATA);
743                 if (lsr & LSR_FE)
744                         xc |= UART_STAT_FRAMERR;
745                 if (lsr & LSR_PE)
746                         xc |= UART_STAT_PARERR;
747                 uart_rx_put(sc, xc);
748                 lsr = uart_getreg(bas, REG_LSR);
749         }
750         /* Discard everything left in the Rx FIFO. */
751         while (lsr & LSR_RXRDY) {
752                 (void)uart_getreg(bas, REG_DATA);
753                 uart_barrier(bas);
754                 lsr = uart_getreg(bas, REG_LSR);
755         }
756         mtx_unlock_spin(&sc->sc_hwmtx);
757         return (0);
758 }
759
760 static int
761 ns8250_bus_setsig(struct uart_softc *sc, int sig)
762 {
763         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
764         struct uart_bas *bas;
765         uint32_t new, old;
766
767         bas = &sc->sc_bas;
768         do {
769                 old = sc->sc_hwsig;
770                 new = old;
771                 if (sig & SER_DDTR) {
772                         SIGCHG(sig & SER_DTR, new, SER_DTR,
773                             SER_DDTR);
774                 }
775                 if (sig & SER_DRTS) {
776                         SIGCHG(sig & SER_RTS, new, SER_RTS,
777                             SER_DRTS);
778                 }
779         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
780         mtx_lock_spin(&sc->sc_hwmtx);
781         ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
782         if (new & SER_DTR)
783                 ns8250->mcr |= MCR_DTR;
784         if (new & SER_RTS)
785                 ns8250->mcr |= MCR_RTS;
786         uart_setreg(bas, REG_MCR, ns8250->mcr);
787         uart_barrier(bas);
788         mtx_unlock_spin(&sc->sc_hwmtx);
789         return (0);
790 }
791
792 static int
793 ns8250_bus_transmit(struct uart_softc *sc)
794 {
795         struct ns8250_softc *ns8250 = (struct ns8250_softc*)sc;
796         struct uart_bas *bas;
797         int i;
798
799         bas = &sc->sc_bas;
800         mtx_lock_spin(&sc->sc_hwmtx);
801         while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
802                 ;
803         uart_setreg(bas, REG_IER, ns8250->ier | IER_ETXRDY);
804         uart_barrier(bas);
805         for (i = 0; i < sc->sc_txdatasz; i++) {
806                 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
807                 uart_barrier(bas);
808         }
809         sc->sc_txbusy = 1;
810         mtx_unlock_spin(&sc->sc_hwmtx);
811         return (0);
812 }