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