]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/uart/uart_dev_lpc.c
MFC r308533 by andrew:
[FreeBSD/FreeBSD.git] / sys / dev / uart / uart_dev_lpc.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_cpu_fdt.h>
39 #include <dev/uart/uart_bus.h>
40
41 #include <dev/ic/ns16550.h>
42 #include <arm/lpc/lpcreg.h>
43
44 #include "uart_if.h"
45
46 #define DEFAULT_RCLK            (13 * 1000 * 1000)
47
48 static bus_space_handle_t bsh_clkpwr;
49
50 #define lpc_ns8250_get_clkreg(_bas, _reg)       \
51     bus_space_read_4((_bas)->bst, bsh_clkpwr, (_reg))
52 #define lpc_ns8250_set_clkreg(_bas, _reg, _val) \
53     bus_space_write_4((_bas)->bst, bsh_clkpwr, (_reg), (_val))
54
55 /*
56  * Clear pending interrupts. THRE is cleared by reading IIR. Data
57  * that may have been received gets lost here.
58  */
59 static void
60 lpc_ns8250_clrint(struct uart_bas *bas)
61 {
62         uint8_t iir, lsr;
63
64         iir = uart_getreg(bas, REG_IIR);
65         while ((iir & IIR_NOPEND) == 0) {
66                 iir &= IIR_IMASK;
67                 if (iir == IIR_RLS) {
68                         lsr = uart_getreg(bas, REG_LSR);
69                         if (lsr & (LSR_BI|LSR_FE|LSR_PE))
70                                 (void)uart_getreg(bas, REG_DATA);
71                 } else if (iir == IIR_RXRDY || iir == IIR_RXTOUT)
72                         (void)uart_getreg(bas, REG_DATA);
73                 else if (iir == IIR_MLSC)
74                         (void)uart_getreg(bas, REG_MSR);
75                 uart_barrier(bas);
76                 iir = uart_getreg(bas, REG_IIR);
77         }
78 }
79
80 static int
81 lpc_ns8250_delay(struct uart_bas *bas)
82 {
83         uint32_t uclk;
84         int x, y;
85
86         uclk = lpc_ns8250_get_clkreg(bas, LPC_CLKPWR_UART_U5CLK);
87         
88         x = (uclk >> 8) & 0xff;
89         y = uclk & 0xff;
90
91         return (16000000 / (bas->rclk * x / y));
92 }
93
94 static void
95 lpc_ns8250_divisor(int rclk, int baudrate, int *x, int *y)
96 {
97
98         switch (baudrate) {
99         case 2400:
100                 *x = 1;
101                 *y = 255;
102                 return;
103         case 4800:
104                 *x = 1;
105                 *y = 169;
106                 return;
107         case 9600:
108                 *x = 3;
109                 *y = 254;
110                 return;
111         case 19200:
112                 *x = 3;
113                 *y = 127;
114                 return;
115         case 38400:
116                 *x = 6;
117                 *y = 127;
118                 return;
119         case 57600:
120                 *x = 9;
121                 *y = 127;
122                 return;
123         default:
124         case 115200:
125                 *x = 19;
126                 *y = 134;
127                 return;
128         case 230400:
129                 *x = 19;
130                 *y = 67;
131                 return; 
132         case 460800:
133                 *x = 38;
134                 *y = 67;
135                 return;
136         }
137 }
138
139 static int
140 lpc_ns8250_drain(struct uart_bas *bas, int what)
141 {
142         int delay, limit;
143
144         delay = lpc_ns8250_delay(bas);
145
146         if (what & UART_DRAIN_TRANSMITTER) {
147                 /*
148                  * Pick an arbitrary high limit to avoid getting stuck in
149                  * an infinite loop when the hardware is broken. Make the
150                  * limit high enough to handle large FIFOs.
151                  */
152                 limit = 10*1024;
153                 while ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0 && --limit)
154                         DELAY(delay);
155                 if (limit == 0) {
156                         /* printf("lpc_ns8250: transmitter appears stuck... "); */
157                         return (EIO);
158                 }
159         }
160
161         if (what & UART_DRAIN_RECEIVER) {
162                 /*
163                  * Pick an arbitrary high limit to avoid getting stuck in
164                  * an infinite loop when the hardware is broken. Make the
165                  * limit high enough to handle large FIFOs and integrated
166                  * UARTs. The HP rx2600 for example has 3 UARTs on the
167                  * management board that tend to get a lot of data send
168                  * to it when the UART is first activated.
169                  */
170                 limit=10*4096;
171                 while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) && --limit) {
172                         (void)uart_getreg(bas, REG_DATA);
173                         uart_barrier(bas);
174                         DELAY(delay << 2);
175                 }
176                 if (limit == 0) {
177                         /* printf("lpc_ns8250: receiver appears broken... "); */
178                         return (EIO);
179                 }
180         }
181
182         return (0);
183 }
184
185 /*
186  * We can only flush UARTs with FIFOs. UARTs without FIFOs should be
187  * drained. WARNING: this function clobbers the FIFO setting!
188  */
189 static void
190 lpc_ns8250_flush(struct uart_bas *bas, int what)
191 {
192         uint8_t fcr;
193
194         fcr = FCR_ENABLE;
195         if (what & UART_FLUSH_TRANSMITTER)
196                 fcr |= FCR_XMT_RST;
197         if (what & UART_FLUSH_RECEIVER)
198                 fcr |= FCR_RCV_RST;
199         uart_setreg(bas, REG_FCR, fcr);
200         uart_barrier(bas);
201 }
202
203 static int
204 lpc_ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
205     int parity)
206 {
207         int xdiv, ydiv;
208         uint8_t lcr;
209
210         lcr = 0;
211         if (databits >= 8)
212                 lcr |= LCR_8BITS;
213         else if (databits == 7)
214                 lcr |= LCR_7BITS;
215         else if (databits == 6)
216                 lcr |= LCR_6BITS;
217         else
218                 lcr |= LCR_5BITS;
219         if (stopbits > 1)
220                 lcr |= LCR_STOPB;
221         lcr |= parity << 3;
222
223         /* Set baudrate. */
224         if (baudrate > 0) {
225                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
226                 uart_barrier(bas);
227                 uart_setreg(bas, REG_DLL, 0x00);
228                 uart_setreg(bas, REG_DLH, 0x00);
229                 uart_barrier(bas);
230
231                 lpc_ns8250_divisor(bas->rclk, baudrate, &xdiv, &ydiv);
232                 lpc_ns8250_set_clkreg(bas,
233                     LPC_CLKPWR_UART_U5CLK,
234                     LPC_CLKPWR_UART_UCLK_X(xdiv) |
235                     LPC_CLKPWR_UART_UCLK_Y(ydiv));
236         }
237
238         /* Set LCR and clear DLAB. */
239         uart_setreg(bas, REG_LCR, lcr);
240         uart_barrier(bas);
241         return (0);
242 }
243
244 /*
245  * Low-level UART interface.
246  */
247 static int lpc_ns8250_probe(struct uart_bas *bas);
248 static void lpc_ns8250_init(struct uart_bas *bas, int, int, int, int);
249 static void lpc_ns8250_term(struct uart_bas *bas);
250 static void lpc_ns8250_putc(struct uart_bas *bas, int);
251 static int lpc_ns8250_rxready(struct uart_bas *bas);
252 static int lpc_ns8250_getc(struct uart_bas *bas, struct mtx *);
253
254 static struct uart_ops uart_lpc_ns8250_ops = {
255         .probe = lpc_ns8250_probe,
256         .init = lpc_ns8250_init,
257         .term = lpc_ns8250_term,
258         .putc = lpc_ns8250_putc,
259         .rxready = lpc_ns8250_rxready,
260         .getc = lpc_ns8250_getc,
261 };
262
263 static int
264 lpc_ns8250_probe(struct uart_bas *bas)
265 {
266 #if 0
267         u_char val;
268
269         /* Check known 0 bits that don't depend on DLAB. */
270         val = uart_getreg(bas, REG_IIR);
271         if (val & 0x30)
272                 return (ENXIO);
273         /*
274          * Bit 6 of the MCR (= 0x40) appears to be 1 for the Sun1699
275          * chip, but otherwise doesn't seem to have a function. In
276          * other words, uart(4) works regardless. Ignore that bit so
277          * the probe succeeds.
278          */
279         val = uart_getreg(bas, REG_MCR);
280         if (val & 0xa0)
281                 return (ENXIO);
282 #endif
283         return (0);
284 }
285
286 static void
287 lpc_ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
288     int parity)
289 {
290         u_char  ier;
291         u_long  clkmode;
292         
293         /* Enable UART clock */
294         bus_space_map(bas->bst, LPC_CLKPWR_PHYS_BASE, LPC_CLKPWR_SIZE, 0,
295             &bsh_clkpwr);
296         clkmode = lpc_ns8250_get_clkreg(bas, LPC_UART_CLKMODE);
297         lpc_ns8250_set_clkreg(bas, LPC_UART_CLKMODE, clkmode | 
298             LPC_UART_CLKMODE_UART5(1));
299
300 #if 0
301         /* Work around H/W bug */
302         uart_setreg(bas, REG_DATA, 0x00);
303 #endif
304         if (bas->rclk == 0)
305                 bas->rclk = DEFAULT_RCLK;
306         lpc_ns8250_param(bas, baudrate, databits, stopbits, parity);
307
308         /* Disable all interrupt sources. */
309         /*
310          * We use 0xe0 instead of 0xf0 as the mask because the XScale PXA
311          * UARTs split the receive time-out interrupt bit out separately as
312          * 0x10.  This gets handled by ier_mask and ier_rxbits below.
313          */
314         ier = uart_getreg(bas, REG_IER) & 0xe0;
315         uart_setreg(bas, REG_IER, ier);
316         uart_barrier(bas);
317
318         /* Disable the FIFO (if present). */
319         uart_setreg(bas, REG_FCR, 0);
320         uart_barrier(bas);
321
322         /* Set RTS & DTR. */
323         uart_setreg(bas, REG_MCR, MCR_IE | MCR_RTS | MCR_DTR);
324         uart_barrier(bas);
325
326         lpc_ns8250_clrint(bas);
327 }
328
329 static void
330 lpc_ns8250_term(struct uart_bas *bas)
331 {
332
333         /* Clear RTS & DTR. */
334         uart_setreg(bas, REG_MCR, MCR_IE);
335         uart_barrier(bas);
336 }
337
338 static void
339 lpc_ns8250_putc(struct uart_bas *bas, int c)
340 {
341         int limit;
342
343         limit = 250000;
344         while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0 && --limit)
345                 DELAY(4);
346         uart_setreg(bas, REG_DATA, c);
347         uart_barrier(bas);
348 }
349
350 static int
351 lpc_ns8250_rxready(struct uart_bas *bas)
352 {
353
354         return ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) != 0 ? 1 : 0);
355 }
356
357 static int
358 lpc_ns8250_getc(struct uart_bas *bas, struct mtx *hwmtx)
359 {
360         int c;
361
362         uart_lock(hwmtx);
363
364         while ((uart_getreg(bas, REG_LSR) & LSR_RXRDY) == 0) {
365                 uart_unlock(hwmtx);
366                 DELAY(4);
367                 uart_lock(hwmtx);
368         }
369
370         c = uart_getreg(bas, REG_DATA);
371
372         uart_unlock(hwmtx);
373
374         return (c);
375 }
376
377 /*
378  * High-level UART interface.
379  */
380 struct lpc_ns8250_softc {
381         struct uart_softc base;
382         uint8_t         fcr;
383         uint8_t         ier;
384         uint8_t         mcr;
385         
386         uint8_t         ier_mask;
387         uint8_t         ier_rxbits;
388 };
389
390 static int lpc_ns8250_bus_attach(struct uart_softc *);
391 static int lpc_ns8250_bus_detach(struct uart_softc *);
392 static int lpc_ns8250_bus_flush(struct uart_softc *, int);
393 static int lpc_ns8250_bus_getsig(struct uart_softc *);
394 static int lpc_ns8250_bus_ioctl(struct uart_softc *, int, intptr_t);
395 static int lpc_ns8250_bus_ipend(struct uart_softc *);
396 static int lpc_ns8250_bus_param(struct uart_softc *, int, int, int, int);
397 static int lpc_ns8250_bus_probe(struct uart_softc *);
398 static int lpc_ns8250_bus_receive(struct uart_softc *);
399 static int lpc_ns8250_bus_setsig(struct uart_softc *, int);
400 static int lpc_ns8250_bus_transmit(struct uart_softc *);
401 static void lpc_ns8250_bus_grab(struct uart_softc *);
402 static void lpc_ns8250_bus_ungrab(struct uart_softc *);
403
404 static kobj_method_t lpc_ns8250_methods[] = {
405         KOBJMETHOD(uart_attach,         lpc_ns8250_bus_attach),
406         KOBJMETHOD(uart_detach,         lpc_ns8250_bus_detach),
407         KOBJMETHOD(uart_flush,          lpc_ns8250_bus_flush),
408         KOBJMETHOD(uart_getsig,         lpc_ns8250_bus_getsig),
409         KOBJMETHOD(uart_ioctl,          lpc_ns8250_bus_ioctl),
410         KOBJMETHOD(uart_ipend,          lpc_ns8250_bus_ipend),
411         KOBJMETHOD(uart_param,          lpc_ns8250_bus_param),
412         KOBJMETHOD(uart_probe,          lpc_ns8250_bus_probe),
413         KOBJMETHOD(uart_receive,        lpc_ns8250_bus_receive),
414         KOBJMETHOD(uart_setsig,         lpc_ns8250_bus_setsig),
415         KOBJMETHOD(uart_transmit,       lpc_ns8250_bus_transmit),
416         KOBJMETHOD(uart_grab,           lpc_ns8250_bus_grab),
417         KOBJMETHOD(uart_ungrab,         lpc_ns8250_bus_ungrab),
418         { 0, 0 }
419 };
420
421 static struct uart_class uart_lpc_class = {
422         "lpc_ns8250",
423         lpc_ns8250_methods,
424         sizeof(struct lpc_ns8250_softc),
425         .uc_ops = &uart_lpc_ns8250_ops,
426         .uc_range = 8,
427         .uc_rclk = DEFAULT_RCLK,
428         .uc_rshift = 0
429 };
430
431 static struct ofw_compat_data compat_data[] = {
432         {"lpc,uart",            (uintptr_t)&uart_lpc_class},
433         {NULL,                  (uintptr_t)NULL},
434 };
435 UART_FDT_CLASS_AND_DEVICE(compat_data);
436
437 #define SIGCHG(c, i, s, d)                              \
438         if (c) {                                        \
439                 i |= (i & s) ? s : s | d;               \
440         } else {                                        \
441                 i = (i & s) ? (i & ~s) | d : i;         \
442         }
443
444 static int
445 lpc_ns8250_bus_attach(struct uart_softc *sc)
446 {
447         struct lpc_ns8250_softc *lpc_ns8250 = (struct lpc_ns8250_softc*)sc;
448         struct uart_bas *bas;
449         unsigned int ivar;
450
451         bas = &sc->sc_bas;
452
453         lpc_ns8250->mcr = uart_getreg(bas, REG_MCR);
454         lpc_ns8250->fcr = FCR_ENABLE | FCR_DMA;
455         if (!resource_int_value("uart", device_get_unit(sc->sc_dev), "flags",
456             &ivar)) {
457                 if (UART_FLAGS_FCR_RX_LOW(ivar)) 
458                         lpc_ns8250->fcr |= FCR_RX_LOW;
459                 else if (UART_FLAGS_FCR_RX_MEDL(ivar)) 
460                         lpc_ns8250->fcr |= FCR_RX_MEDL;
461                 else if (UART_FLAGS_FCR_RX_HIGH(ivar)) 
462                         lpc_ns8250->fcr |= FCR_RX_HIGH;
463                 else
464                         lpc_ns8250->fcr |= FCR_RX_MEDH;
465         } else 
466                 lpc_ns8250->fcr |= FCR_RX_HIGH;
467         
468         /* Get IER mask */
469         ivar = 0xf0;
470         resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_mask",
471             &ivar);
472         lpc_ns8250->ier_mask = (uint8_t)(ivar & 0xff);
473         
474         /* Get IER RX interrupt bits */
475         ivar = IER_EMSC | IER_ERLS | IER_ERXRDY;
476         resource_int_value("uart", device_get_unit(sc->sc_dev), "ier_rxbits",
477             &ivar);
478         lpc_ns8250->ier_rxbits = (uint8_t)(ivar & 0xff);
479         
480         uart_setreg(bas, REG_FCR, lpc_ns8250->fcr);
481         uart_barrier(bas);
482         lpc_ns8250_bus_flush(sc, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
483
484         if (lpc_ns8250->mcr & MCR_DTR)
485                 sc->sc_hwsig |= SER_DTR;
486         if (lpc_ns8250->mcr & MCR_RTS)
487                 sc->sc_hwsig |= SER_RTS;
488         lpc_ns8250_bus_getsig(sc);
489
490         lpc_ns8250_clrint(bas);
491         lpc_ns8250->ier = uart_getreg(bas, REG_IER) & lpc_ns8250->ier_mask;
492         lpc_ns8250->ier |= lpc_ns8250->ier_rxbits;
493         uart_setreg(bas, REG_IER, lpc_ns8250->ier);
494         uart_barrier(bas);
495         
496         return (0);
497 }
498
499 static int
500 lpc_ns8250_bus_detach(struct uart_softc *sc)
501 {
502         struct lpc_ns8250_softc *lpc_ns8250;
503         struct uart_bas *bas;
504         u_char ier;
505
506         lpc_ns8250 = (struct lpc_ns8250_softc *)sc;
507         bas = &sc->sc_bas;
508         ier = uart_getreg(bas, REG_IER) & lpc_ns8250->ier_mask;
509         uart_setreg(bas, REG_IER, ier);
510         uart_barrier(bas);
511         lpc_ns8250_clrint(bas);
512         return (0);
513 }
514
515 static int
516 lpc_ns8250_bus_flush(struct uart_softc *sc, int what)
517 {
518         struct lpc_ns8250_softc *lpc_ns8250 = (struct lpc_ns8250_softc*)sc;
519         struct uart_bas *bas;
520         int error;
521
522         bas = &sc->sc_bas;
523         uart_lock(sc->sc_hwmtx);
524         if (sc->sc_rxfifosz > 1) {
525                 lpc_ns8250_flush(bas, what);
526                 uart_setreg(bas, REG_FCR, lpc_ns8250->fcr);
527                 uart_barrier(bas);
528                 error = 0;
529         } else
530                 error = lpc_ns8250_drain(bas, what);
531         uart_unlock(sc->sc_hwmtx);
532         return (error);
533 }
534
535 static int
536 lpc_ns8250_bus_getsig(struct uart_softc *sc)
537 {
538         uint32_t new, old, sig;
539         uint8_t msr;
540
541         do {
542                 old = sc->sc_hwsig;
543                 sig = old;
544                 uart_lock(sc->sc_hwmtx);
545                 msr = uart_getreg(&sc->sc_bas, REG_MSR);
546                 uart_unlock(sc->sc_hwmtx);
547                 SIGCHG(msr & MSR_DSR, sig, SER_DSR, SER_DDSR);
548                 SIGCHG(msr & MSR_CTS, sig, SER_CTS, SER_DCTS);
549                 SIGCHG(msr & MSR_DCD, sig, SER_DCD, SER_DDCD);
550                 SIGCHG(msr & MSR_RI,  sig, SER_RI,  SER_DRI);
551                 new = sig & ~SER_MASK_DELTA;
552         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
553         return (sig);
554 }
555
556 static int
557 lpc_ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
558 {
559         struct uart_bas *bas;
560         int baudrate, divisor, error;
561         uint8_t efr, lcr;
562
563         bas = &sc->sc_bas;
564         error = 0;
565         uart_lock(sc->sc_hwmtx);
566         switch (request) {
567         case UART_IOCTL_BREAK:
568                 lcr = uart_getreg(bas, REG_LCR);
569                 if (data)
570                         lcr |= LCR_SBREAK;
571                 else
572                         lcr &= ~LCR_SBREAK;
573                 uart_setreg(bas, REG_LCR, lcr);
574                 uart_barrier(bas);
575                 break;
576         case UART_IOCTL_IFLOW:
577                 lcr = uart_getreg(bas, REG_LCR);
578                 uart_barrier(bas);
579                 uart_setreg(bas, REG_LCR, 0xbf);
580                 uart_barrier(bas);
581                 efr = uart_getreg(bas, REG_EFR);
582                 if (data)
583                         efr |= EFR_RTS;
584                 else
585                         efr &= ~EFR_RTS;
586                 uart_setreg(bas, REG_EFR, efr);
587                 uart_barrier(bas);
588                 uart_setreg(bas, REG_LCR, lcr);
589                 uart_barrier(bas);
590                 break;
591         case UART_IOCTL_OFLOW:
592                 lcr = uart_getreg(bas, REG_LCR);
593                 uart_barrier(bas);
594                 uart_setreg(bas, REG_LCR, 0xbf);
595                 uart_barrier(bas);
596                 efr = uart_getreg(bas, REG_EFR);
597                 if (data)
598                         efr |= EFR_CTS;
599                 else
600                         efr &= ~EFR_CTS;
601                 uart_setreg(bas, REG_EFR, efr);
602                 uart_barrier(bas);
603                 uart_setreg(bas, REG_LCR, lcr);
604                 uart_barrier(bas);
605                 break;
606         case UART_IOCTL_BAUD:
607                 lcr = uart_getreg(bas, REG_LCR);
608                 uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
609                 uart_barrier(bas);
610                 divisor = uart_getreg(bas, REG_DLL) |
611                     (uart_getreg(bas, REG_DLH) << 8);
612                 uart_barrier(bas);
613                 uart_setreg(bas, REG_LCR, lcr);
614                 uart_barrier(bas);
615                 baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
616                 if (baudrate > 0)
617                         *(int*)data = baudrate;
618                 else
619                         error = ENXIO;
620                 break;
621         default:
622                 error = EINVAL;
623                 break;
624         }
625         uart_unlock(sc->sc_hwmtx);
626         return (error);
627 }
628
629 static int
630 lpc_ns8250_bus_ipend(struct uart_softc *sc)
631 {
632         struct uart_bas *bas;
633         struct lpc_ns8250_softc *lpc_ns8250;
634         int ipend;
635         uint8_t iir, lsr;
636
637         lpc_ns8250 = (struct lpc_ns8250_softc *)sc;
638         bas = &sc->sc_bas;
639         uart_lock(sc->sc_hwmtx);
640         iir = uart_getreg(bas, REG_IIR);
641         if (iir & IIR_NOPEND) {
642                 uart_unlock(sc->sc_hwmtx);
643                 return (0);
644         }
645         ipend = 0;
646         if (iir & IIR_RXRDY) {
647                 lsr = uart_getreg(bas, REG_LSR);
648                 if (lsr & LSR_OE)
649                         ipend |= SER_INT_OVERRUN;
650                 if (lsr & LSR_BI)
651                         ipend |= SER_INT_BREAK;
652                 if (lsr & LSR_RXRDY)
653                         ipend |= SER_INT_RXREADY;
654         } else {
655                 if (iir & IIR_TXRDY) {
656                         ipend |= SER_INT_TXIDLE;
657                         uart_setreg(bas, REG_IER, lpc_ns8250->ier);
658                         uart_barrier(bas);
659                 } else
660                         ipend |= SER_INT_SIGCHG;
661         }
662         if (ipend == 0)
663                 lpc_ns8250_clrint(bas);
664         uart_unlock(sc->sc_hwmtx);
665         return (ipend);
666 }
667
668 static int
669 lpc_ns8250_bus_param(struct uart_softc *sc, int baudrate, int databits,
670     int stopbits, int parity)
671 {
672         struct uart_bas *bas;
673         int error;
674
675         bas = &sc->sc_bas;
676         uart_lock(sc->sc_hwmtx);
677         error = lpc_ns8250_param(bas, baudrate, databits, stopbits, parity);
678         uart_unlock(sc->sc_hwmtx);
679         return (error);
680 }
681
682 static int
683 lpc_ns8250_bus_probe(struct uart_softc *sc)
684 {
685         struct lpc_ns8250_softc *lpc_ns8250;
686         struct uart_bas *bas;
687         int count, delay, error, limit;
688         uint8_t lsr, mcr, ier;
689
690         lpc_ns8250 = (struct lpc_ns8250_softc *)sc;
691         bas = &sc->sc_bas;
692
693         error = lpc_ns8250_probe(bas);
694         if (error)
695                 return (error);
696
697         mcr = MCR_IE;
698         if (sc->sc_sysdev == NULL) {
699                 /* By using lpc_ns8250_init() we also set DTR and RTS. */
700                 lpc_ns8250_init(bas, 115200, 8, 1, UART_PARITY_NONE);
701         } else
702                 mcr |= MCR_DTR | MCR_RTS;
703
704         error = lpc_ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
705         if (error)
706                 return (error);
707
708         /*
709          * Set loopback mode. This avoids having garbage on the wire and
710          * also allows us send and receive data. We set DTR and RTS to
711          * avoid the possibility that automatic flow-control prevents
712          * any data from being sent.
713          */
714         uart_setreg(bas, REG_MCR, MCR_LOOPBACK | MCR_IE | MCR_DTR | MCR_RTS);
715         uart_barrier(bas);
716
717         /*
718          * Enable FIFOs. And check that the UART has them. If not, we're
719          * done. Since this is the first time we enable the FIFOs, we reset
720          * them.
721          */
722         uart_setreg(bas, REG_FCR, FCR_ENABLE);
723         uart_barrier(bas);
724         if (!(uart_getreg(bas, REG_IIR) & IIR_FIFO_MASK)) {
725                 /*
726                  * NS16450 or INS8250. We don't bother to differentiate
727                  * between them. They're too old to be interesting.
728                  */
729                 uart_setreg(bas, REG_MCR, mcr);
730                 uart_barrier(bas);
731                 sc->sc_rxfifosz = sc->sc_txfifosz = 1;
732                 device_set_desc(sc->sc_dev, "8250 or 16450 or compatible");
733                 return (0);
734         }
735
736         uart_setreg(bas, REG_FCR, FCR_ENABLE | FCR_XMT_RST | FCR_RCV_RST);
737         uart_barrier(bas);
738
739         count = 0;
740         delay = lpc_ns8250_delay(bas);
741
742         /* We have FIFOs. Drain the transmitter and receiver. */
743         error = lpc_ns8250_drain(bas, UART_DRAIN_RECEIVER|UART_DRAIN_TRANSMITTER);
744         if (error) {
745                 uart_setreg(bas, REG_MCR, mcr);
746                 uart_setreg(bas, REG_FCR, 0);
747                 uart_barrier(bas);
748                 goto done;
749         }
750
751         /*
752          * We should have a sufficiently clean "pipe" to determine the
753          * size of the FIFOs. We send as much characters as is reasonable
754          * and wait for the overflow bit in the LSR register to be
755          * asserted, counting the characters as we send them. Based on
756          * that count we know the FIFO size.
757          */
758         do {
759                 uart_setreg(bas, REG_DATA, 0);
760                 uart_barrier(bas);
761                 count++;
762
763                 limit = 30;
764                 lsr = 0;
765                 /*
766                  * LSR bits are cleared upon read, so we must accumulate
767                  * them to be able to test LSR_OE below.
768                  */
769                 while (((lsr |= uart_getreg(bas, REG_LSR)) & LSR_TEMT) == 0 &&
770                     --limit)
771                         DELAY(delay);
772                 if (limit == 0) {
773                         ier = uart_getreg(bas, REG_IER) & lpc_ns8250->ier_mask;
774                         uart_setreg(bas, REG_IER, ier);
775                         uart_setreg(bas, REG_MCR, mcr);
776                         uart_setreg(bas, REG_FCR, 0);
777                         uart_barrier(bas);
778                         count = 0;
779                         goto done;
780                 }
781         } while ((lsr & LSR_OE) == 0 && count < 130);
782         count--;
783
784         uart_setreg(bas, REG_MCR, mcr);
785
786         /* Reset FIFOs. */
787         lpc_ns8250_flush(bas, UART_FLUSH_RECEIVER|UART_FLUSH_TRANSMITTER);
788
789 done:
790         sc->sc_rxfifosz = 64;
791         device_set_desc(sc->sc_dev, "LPC32x0 UART with FIFOs");
792
793         /*
794          * Force the Tx FIFO size to 16 bytes for now. We don't program the
795          * Tx trigger. Also, we assume that all data has been sent when the
796          * interrupt happens.
797          */
798         sc->sc_txfifosz = 16;
799
800 #if 0
801         /*
802          * XXX there are some issues related to hardware flow control and
803          * it's likely that uart(4) is the cause. This basically needs more
804          * investigation, but we avoid using for hardware flow control
805          * until then.
806          */
807         /* 16650s or higher have automatic flow control. */
808         if (sc->sc_rxfifosz > 16) {
809                 sc->sc_hwiflow = 1;
810                 sc->sc_hwoflow = 1;
811         }
812 #endif
813         return (0);
814 }
815
816 static int
817 lpc_ns8250_bus_receive(struct uart_softc *sc)
818 {
819         struct uart_bas *bas;
820         int xc;
821         uint8_t lsr;
822
823         bas = &sc->sc_bas;
824         uart_lock(sc->sc_hwmtx);
825         lsr = uart_getreg(bas, REG_LSR);
826         while (lsr & LSR_RXRDY) {
827                 if (uart_rx_full(sc)) {
828                         sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
829                         break;
830                 }
831                 xc = uart_getreg(bas, REG_DATA);
832                 if (lsr & LSR_FE)
833                         xc |= UART_STAT_FRAMERR;
834                 if (lsr & LSR_PE)
835                         xc |= UART_STAT_PARERR;
836                 uart_rx_put(sc, xc);
837                 lsr = uart_getreg(bas, REG_LSR);
838         }
839         /* Discard everything left in the Rx FIFO. */
840         while (lsr & LSR_RXRDY) {
841                 (void)uart_getreg(bas, REG_DATA);
842                 uart_barrier(bas);
843                 lsr = uart_getreg(bas, REG_LSR);
844         }
845         uart_unlock(sc->sc_hwmtx);
846         return (0);
847 }
848
849 static int
850 lpc_ns8250_bus_setsig(struct uart_softc *sc, int sig)
851 {
852         struct lpc_ns8250_softc *lpc_ns8250 = (struct lpc_ns8250_softc*)sc;
853         struct uart_bas *bas;
854         uint32_t new, old;
855
856         bas = &sc->sc_bas;
857         do {
858                 old = sc->sc_hwsig;
859                 new = old;
860                 if (sig & SER_DDTR) {
861                         SIGCHG(sig & SER_DTR, new, SER_DTR,
862                             SER_DDTR);
863                 }
864                 if (sig & SER_DRTS) {
865                         SIGCHG(sig & SER_RTS, new, SER_RTS,
866                             SER_DRTS);
867                 }
868         } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
869         uart_lock(sc->sc_hwmtx);
870         lpc_ns8250->mcr &= ~(MCR_DTR|MCR_RTS);
871         if (new & SER_DTR)
872                 lpc_ns8250->mcr |= MCR_DTR;
873         if (new & SER_RTS)
874                 lpc_ns8250->mcr |= MCR_RTS;
875         uart_setreg(bas, REG_MCR, lpc_ns8250->mcr);
876         uart_barrier(bas);
877         uart_unlock(sc->sc_hwmtx);
878         return (0);
879 }
880
881 static int
882 lpc_ns8250_bus_transmit(struct uart_softc *sc)
883 {
884         struct lpc_ns8250_softc *lpc_ns8250 = (struct lpc_ns8250_softc*)sc;
885         struct uart_bas *bas;
886         int i;
887
888         bas = &sc->sc_bas;
889         uart_lock(sc->sc_hwmtx);
890         if (sc->sc_txdatasz > 1) {
891                 if ((uart_getreg(bas, REG_LSR) & LSR_TEMT) == 0)
892                         lpc_ns8250_drain(bas, UART_DRAIN_TRANSMITTER);
893         } else {
894                 while ((uart_getreg(bas, REG_LSR) & LSR_THRE) == 0)
895                         DELAY(4);
896         }
897         for (i = 0; i < sc->sc_txdatasz; i++) {
898                 uart_setreg(bas, REG_DATA, sc->sc_txbuf[i]);
899                 uart_barrier(bas);
900         }
901         uart_setreg(bas, REG_IER, lpc_ns8250->ier | IER_ETXRDY);
902         uart_barrier(bas);
903         sc->sc_txbusy = 1;
904         uart_unlock(sc->sc_hwmtx);
905         return (0);
906 }
907
908 void
909 lpc_ns8250_bus_grab(struct uart_softc *sc)
910 {
911         struct uart_bas *bas = &sc->sc_bas;
912
913         /*
914          * turn off all interrupts to enter polling mode. Leave the
915          * saved mask alone. We'll restore whatever it was in ungrab.
916          * All pending interrupt signals are reset when IER is set to 0.
917          */
918         uart_lock(sc->sc_hwmtx);
919         uart_setreg(bas, REG_IER, 0);
920         uart_barrier(bas);
921         uart_unlock(sc->sc_hwmtx);
922 }
923
924 void
925 lpc_ns8250_bus_ungrab(struct uart_softc *sc)
926 {
927         struct lpc_ns8250_softc *lpc_ns8250 = (struct lpc_ns8250_softc*)sc;
928         struct uart_bas *bas = &sc->sc_bas;
929
930         /*
931          * Restore previous interrupt mask
932          */
933         uart_lock(sc->sc_hwmtx);
934         uart_setreg(bas, REG_IER, lpc_ns8250->ier);
935         uart_barrier(bas);
936         uart_unlock(sc->sc_hwmtx);
937 }