arm64 libc: hide .cerror, .curbrk, .minbrk for WITHOUT_SYMVER
[freebsd-src.git] / sys / mips / rt305x / uart_dev_rt305x.c
blob5d23b1eeaef07fb1cad78e48a4fea098fc206610
1 /* $NetBSD: uart.c,v 1.2 2007/03/23 20:05:47 dogcow Exp $ */
3 /*-
4 * Copyright (c) 2010 Aleksandr Rybalko.
5 * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
6 * Copyright (c) 2007 Oleksandr Tymoshenko.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_ddb.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/kdb.h>
43 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
46 #include <machine/bus.h>
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_cpu.h>
50 #include <dev/uart/uart_bus.h>
52 #include <mips/rt305x/uart_dev_rt305x.h>
53 #include <mips/rt305x/rt305xreg.h>
55 #include "uart_if.h"
57 * Low-level UART interface.
59 static int rt305x_uart_probe(struct uart_bas *bas);
60 static void rt305x_uart_init(struct uart_bas *bas, int, int, int, int);
61 static void rt305x_uart_term(struct uart_bas *bas);
62 static void rt305x_uart_putc(struct uart_bas *bas, int);
63 static int rt305x_uart_rxready(struct uart_bas *bas);
64 static int rt305x_uart_getc(struct uart_bas *bas, struct mtx *);
66 static struct uart_ops uart_rt305x_uart_ops = {
67 .probe = rt305x_uart_probe,
68 .init = rt305x_uart_init,
69 .term = rt305x_uart_term,
70 .putc = rt305x_uart_putc,
71 .rxready = rt305x_uart_rxready,
72 .getc = rt305x_uart_getc,
75 static int uart_output = 1;
76 SYSCTL_INT(_kern, OID_AUTO, uart_output, CTLFLAG_RWTUN,
77 &uart_output, 0, "UART output enabled.");
79 static int
80 rt305x_uart_probe(struct uart_bas *bas)
83 return (0);
86 static void
87 rt305x_uart_init(struct uart_bas *bas, int baudrate, int databits,
88 int stopbits, int parity)
90 #ifdef notyet
91 /* CLKDIV = 384000000/ 3/ 16/ br */
92 /* for 384MHz CLKDIV = 8000000 / baudrate; */
93 switch (databits) {
94 case 5:
95 databits = UART_LCR_5B;
96 break;
97 case 6:
98 databits = UART_LCR_6B;
99 break;
100 case 7:
101 databits = UART_LCR_7B;
102 break;
103 case 8:
104 databits = UART_LCR_8B;
105 break;
106 default:
107 /* Unsupported */
108 return;
110 switch (parity) {
111 case UART_PARITY_EVEN: parity = (UART_LCR_PEN|UART_LCR_EVEN); break;
112 case UART_PARITY_NONE: parity = (UART_LCR_PEN); break;
113 case UART_PARITY_ODD: parity = 0; break;
114 /* Unsupported */
115 default: return;
117 uart_setreg(bas, UART_CDDL_REG, 8000000/baudrate);
118 uart_barrier(bas);
119 uart_setreg(bas, UART_LCR_REG, databits | (stopbits==1?0:4) | parity);
120 uart_barrier(bas);
121 #endif
124 static void
125 rt305x_uart_term(struct uart_bas *bas)
127 uart_setreg(bas, UART_MCR_REG, 0);
128 uart_barrier(bas);
131 static void
132 rt305x_uart_putc(struct uart_bas *bas, int c)
134 char chr;
135 if (!uart_output) return;
136 chr = c;
137 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
138 uart_setreg(bas, UART_TX_REG, c);
139 uart_barrier(bas);
140 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
143 static int
144 rt305x_uart_rxready(struct uart_bas *bas)
146 #ifdef notyet
147 if (uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)
148 return (1);
150 return (0);
151 #else
152 return (1);
153 #endif
156 static int
157 rt305x_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
159 int c;
161 uart_lock(hwmtx);
163 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)) {
164 uart_unlock(hwmtx);
165 DELAY(10);
166 uart_lock(hwmtx);
169 c = uart_getreg(bas, UART_RX_REG);
171 uart_unlock(hwmtx);
173 return (c);
177 * High-level UART interface.
179 struct rt305x_uart_softc {
180 struct uart_softc base;
183 static int rt305x_uart_bus_attach(struct uart_softc *);
184 static int rt305x_uart_bus_detach(struct uart_softc *);
185 static int rt305x_uart_bus_flush(struct uart_softc *, int);
186 static int rt305x_uart_bus_getsig(struct uart_softc *);
187 static int rt305x_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
188 static int rt305x_uart_bus_ipend(struct uart_softc *);
189 static int rt305x_uart_bus_param(struct uart_softc *, int, int, int, int);
190 static int rt305x_uart_bus_probe(struct uart_softc *);
191 static int rt305x_uart_bus_receive(struct uart_softc *);
192 static int rt305x_uart_bus_setsig(struct uart_softc *, int);
193 static int rt305x_uart_bus_transmit(struct uart_softc *);
194 static void rt305x_uart_bus_grab(struct uart_softc *);
195 static void rt305x_uart_bus_ungrab(struct uart_softc *);
197 static kobj_method_t rt305x_uart_methods[] = {
198 KOBJMETHOD(uart_attach, rt305x_uart_bus_attach),
199 KOBJMETHOD(uart_detach, rt305x_uart_bus_detach),
200 KOBJMETHOD(uart_flush, rt305x_uart_bus_flush),
201 KOBJMETHOD(uart_getsig, rt305x_uart_bus_getsig),
202 KOBJMETHOD(uart_ioctl, rt305x_uart_bus_ioctl),
203 KOBJMETHOD(uart_ipend, rt305x_uart_bus_ipend),
204 KOBJMETHOD(uart_param, rt305x_uart_bus_param),
205 KOBJMETHOD(uart_probe, rt305x_uart_bus_probe),
206 KOBJMETHOD(uart_receive, rt305x_uart_bus_receive),
207 KOBJMETHOD(uart_setsig, rt305x_uart_bus_setsig),
208 KOBJMETHOD(uart_transmit, rt305x_uart_bus_transmit),
209 KOBJMETHOD(uart_grab, rt305x_uart_bus_grab),
210 KOBJMETHOD(uart_ungrab, rt305x_uart_bus_ungrab),
211 { 0, 0 }
214 struct uart_class uart_rt305x_uart_class = {
215 "rt305x",
216 rt305x_uart_methods,
217 sizeof(struct rt305x_uart_softc),
218 .uc_ops = &uart_rt305x_uart_ops,
219 .uc_range = 1, /* use hinted range */
220 .uc_rclk = SYSTEM_CLOCK,
221 .uc_rshift = 0
224 #define SIGCHG(c, i, s, d) \
225 if (c) { \
226 i |= (i & s) ? s : s | d; \
227 } else { \
228 i = (i & s) ? (i & ~s) | d : i; \
232 * Disable TX interrupt. uart should be locked
234 static __inline void
235 rt305x_uart_disable_txintr(struct uart_softc *sc)
237 struct uart_bas *bas = &sc->sc_bas;
238 uint8_t cr;
240 cr = uart_getreg(bas, UART_IER_REG);
241 cr &= ~UART_IER_ETBEI;
242 uart_setreg(bas, UART_IER_REG, cr);
243 uart_barrier(bas);
247 * Enable TX interrupt. uart should be locked
249 static __inline void
250 rt305x_uart_enable_txintr(struct uart_softc *sc)
252 struct uart_bas *bas = &sc->sc_bas;
253 uint8_t cr;
255 cr = uart_getreg(bas, UART_IER_REG);
256 cr |= UART_IER_ETBEI;
257 uart_setreg(bas, UART_IER_REG, cr);
258 uart_barrier(bas);
261 static int
262 rt305x_uart_bus_attach(struct uart_softc *sc)
264 struct uart_bas *bas;
265 struct uart_devinfo *di;
267 bas = &sc->sc_bas;
268 if (sc->sc_sysdev != NULL) {
269 di = sc->sc_sysdev;
270 rt305x_uart_init(bas, di->baudrate, di->databits, di->stopbits,
271 di->parity);
272 } else {
273 rt305x_uart_init(bas, 115200, 8, 1, 0);
276 (void)rt305x_uart_bus_getsig(sc);
278 /* Enable FIFO */
279 uart_setreg(bas, UART_FCR_REG,
280 uart_getreg(bas, UART_FCR_REG) |
281 UART_FCR_FIFOEN | UART_FCR_TXTGR_1 | UART_FCR_RXTGR_1);
282 uart_barrier(bas);
283 /* Enable interrupts */
284 uart_setreg(bas, UART_IER_REG,
285 UART_IER_EDSSI | UART_IER_ELSI | UART_IER_ERBFI);
286 uart_barrier(bas);
288 return (0);
291 static int
292 rt305x_uart_bus_detach(struct uart_softc *sc)
295 return (0);
298 static int
299 rt305x_uart_bus_flush(struct uart_softc *sc, int what)
301 struct uart_bas *bas = &sc->sc_bas;
302 uint32_t fcr = uart_getreg(bas, UART_FCR_REG);
303 if (what & UART_FLUSH_TRANSMITTER) {
304 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_TXRST);
305 uart_barrier(bas);
307 if (what & UART_FLUSH_RECEIVER) {
308 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_RXRST);
309 uart_barrier(bas);
311 uart_setreg(bas, UART_FCR_REG, fcr);
312 uart_barrier(bas);
313 return (0);
316 static int
317 rt305x_uart_bus_getsig(struct uart_softc *sc)
319 uint32_t new, old, sig;
320 uint8_t bes;
322 do {
323 old = sc->sc_hwsig;
324 sig = old;
325 uart_lock(sc->sc_hwmtx);
326 bes = uart_getreg(&sc->sc_bas, UART_MSR_REG);
327 uart_unlock(sc->sc_hwmtx);
328 /* XXX: chip can show delta */
329 SIGCHG(bes & UART_MSR_CTS, sig, SER_CTS, SER_DCTS);
330 SIGCHG(bes & UART_MSR_DCD, sig, SER_DCD, SER_DDCD);
331 SIGCHG(bes & UART_MSR_DSR, sig, SER_DSR, SER_DDSR);
332 new = sig & ~SER_MASK_DELTA;
333 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
335 return (sig);
338 static int
339 rt305x_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
341 struct uart_bas *bas;
342 int baudrate, divisor, error;
344 bas = &sc->sc_bas;
345 error = 0;
346 uart_lock(sc->sc_hwmtx);
347 switch (request) {
348 case UART_IOCTL_BREAK:
349 /* TODO: Send BREAK */
350 break;
351 case UART_IOCTL_BAUD:
352 divisor = uart_getreg(bas, UART_CDDL_REG);
353 baudrate = bas->rclk / (divisor * 16);
354 *(int*)data = baudrate;
355 break;
356 default:
357 error = EINVAL;
358 break;
360 uart_unlock(sc->sc_hwmtx);
361 return (error);
364 static int
365 rt305x_uart_bus_ipend(struct uart_softc *sc)
367 struct uart_bas *bas;
368 int ipend;
369 uint8_t iir, lsr, msr;
371 bas = &sc->sc_bas;
372 ipend = 0;
374 uart_lock(sc->sc_hwmtx);
375 iir = uart_getreg(&sc->sc_bas, UART_IIR_REG);
376 lsr = uart_getreg(&sc->sc_bas, UART_LSR_REG);
377 uart_setreg(&sc->sc_bas, UART_LSR_REG, lsr);
378 msr = uart_getreg(&sc->sc_bas, UART_MSR_REG);
379 uart_setreg(&sc->sc_bas, UART_MSR_REG, msr);
380 if (iir & UART_IIR_INTP) {
381 uart_unlock(sc->sc_hwmtx);
382 return (0);
386 switch ((iir >> 1) & 0x07) {
387 case UART_IIR_ID_THRE:
388 ipend |= SER_INT_TXIDLE;
389 break;
390 case UART_IIR_ID_DR2:
391 rt305x_uart_bus_flush(sc, UART_FLUSH_RECEIVER);
392 /* passthrough */
393 case UART_IIR_ID_DR:
394 ipend |= SER_INT_RXREADY;
395 break;
396 case UART_IIR_ID_MST:
397 case UART_IIR_ID_LINESTATUS:
398 ipend |= SER_INT_SIGCHG;
399 if (lsr & UART_LSR_BI)
401 ipend |= SER_INT_BREAK;
402 #ifdef KDB
403 breakpoint();
404 #endif
406 if (lsr & UART_LSR_OE)
407 ipend |= SER_INT_OVERRUN;
408 break;
409 default:
410 /* XXX: maybe return error here */
411 break;
414 uart_unlock(sc->sc_hwmtx);
416 return (ipend);
419 static int
420 rt305x_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
421 int stopbits, int parity)
423 uart_lock(sc->sc_hwmtx);
424 rt305x_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
425 uart_unlock(sc->sc_hwmtx);
426 return (0);
429 static int
430 rt305x_uart_bus_probe(struct uart_softc *sc)
432 char buf[80];
433 int error;
435 error = rt305x_uart_probe(&sc->sc_bas);
436 if (error)
437 return (error);
439 sc->sc_rxfifosz = 16;
440 sc->sc_txfifosz = 16;
442 snprintf(buf, sizeof(buf), "rt305x_uart");
443 device_set_desc_copy(sc->sc_dev, buf);
445 return (0);
448 static int
449 rt305x_uart_bus_receive(struct uart_softc *sc)
451 struct uart_bas *bas;
452 int xc;
453 uint8_t lsr;
455 bas = &sc->sc_bas;
456 uart_lock(sc->sc_hwmtx);
457 lsr = uart_getreg(bas, UART_LSR_REG);
458 while ((lsr & UART_LSR_DR)) {
459 if (uart_rx_full(sc)) {
460 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
461 break;
463 xc = 0;
464 xc = uart_getreg(bas, UART_RX_REG);
465 if (lsr & UART_LSR_FE)
466 xc |= UART_STAT_FRAMERR;
467 if (lsr & UART_LSR_PE)
468 xc |= UART_STAT_PARERR;
469 if (lsr & UART_LSR_OE)
470 xc |= UART_STAT_OVERRUN;
471 uart_barrier(bas);
472 uart_rx_put(sc, xc);
473 lsr = uart_getreg(bas, UART_LSR_REG);
476 uart_unlock(sc->sc_hwmtx);
477 return (0);
480 static int
481 rt305x_uart_bus_setsig(struct uart_softc *sc, int sig)
484 /* TODO: implement (?) */
485 return (0);
488 static int
489 rt305x_uart_bus_transmit(struct uart_softc *sc)
491 struct uart_bas *bas = &sc->sc_bas;
492 int i;
494 if (!uart_output) return (0);
496 bas = &sc->sc_bas;
497 uart_lock(sc->sc_hwmtx);
498 while ((uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE) == 0)
500 rt305x_uart_enable_txintr(sc);
501 for (i = 0; i < sc->sc_txdatasz; i++) {
502 uart_setreg(bas, UART_TX_REG, sc->sc_txbuf[i]);
503 uart_barrier(bas);
505 sc->sc_txbusy = 1;
506 uart_unlock(sc->sc_hwmtx);
507 return (0);
510 static void
511 rt305x_uart_bus_grab(struct uart_softc *sc)
513 struct uart_bas *bas = &sc->sc_bas;
515 /* disable interrupts -- XXX not sure which one is RX, so kill them all */
516 uart_lock(sc->sc_hwmtx);
517 uart_setreg(bas, UART_IER_REG, 0);
518 uart_barrier(bas);
519 uart_unlock(sc->sc_hwmtx);
522 static void
523 rt305x_uart_bus_ungrab(struct uart_softc *sc)
525 struct uart_bas *bas = &sc->sc_bas;
527 /* Enable interrupts */
528 uart_lock(sc->sc_hwmtx);
529 uart_setreg(bas, UART_IER_REG,
530 UART_IER_EDSSI | UART_IER_ELSI | UART_IER_ERBFI);
531 uart_barrier(bas);
532 uart_unlock(sc->sc_hwmtx);