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