1 /* $NetBSD: uart.c,v 1.2 2007/03/23 20:05:47 dogcow Exp $ */
4 * Copyright (c) 2013, Alexander A. Mityaev <sansan@adm.ua>
5 * Copyright (c) 2010 Aleksandr Rybalko.
6 * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
7 * Copyright (c) 2007 Oleksandr Tymoshenko.
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
44 #include <sys/reboot.h>
45 #include <sys/sysctl.h>
46 #include <sys/kernel.h>
47 #include <machine/bus.h>
48 #include <machine/fdt.h>
50 #include <dev/uart/uart.h>
51 #include <dev/uart/uart_cpu.h>
52 #include <dev/uart/uart_cpu_fdt.h>
53 #include <dev/uart/uart_bus.h>
55 #include <mips/mediatek/uart_dev_mtk.h>
56 #include <mips/mediatek/mtk_soc.h>
57 #include <mips/mediatek/mtk_sysctl.h>
61 /* Set some reference clock value. Real value will be taken from FDT */
62 #define DEFAULT_RCLK (120 * 1000 * 1000)
65 * Low-level UART interface.
67 static int mtk_uart_probe(struct uart_bas *bas);
68 static void mtk_uart_init(struct uart_bas *bas, int, int, int, int);
69 static void mtk_uart_term(struct uart_bas *bas);
70 static void mtk_uart_putc(struct uart_bas *bas, int);
71 static int mtk_uart_rxready(struct uart_bas *bas);
72 static int mtk_uart_getc(struct uart_bas *bas, struct mtx *);
74 static struct uart_ops uart_mtk_ops = {
75 .probe = mtk_uart_probe,
76 .init = mtk_uart_init,
77 .term = mtk_uart_term,
78 .putc = mtk_uart_putc,
79 .rxready = mtk_uart_rxready,
80 .getc = mtk_uart_getc,
83 static int uart_output = 1;
84 TUNABLE_INT("kern.uart_output", &uart_output);
85 SYSCTL_INT(_kern, OID_AUTO, uart_output, CTLFLAG_RW,
86 &uart_output, 0, "UART output enabled.");
89 mtk_uart_probe(struct uart_bas *bas)
95 mtk_uart_init(struct uart_bas *bas, int baudrate, int databits,
96 int stopbits, int parity)
98 /* CLKDIV = 384000000/ 3/ 16/ br */
99 /* for 384MHz CLKDIV = 8000000 / baudrate; */
102 databits = UART_LCR_5B;
105 databits = UART_LCR_6B;
108 databits = UART_LCR_7B;
111 databits = UART_LCR_8B;
118 case UART_PARITY_EVEN: parity = (UART_LCR_PEN|UART_LCR_EVEN); break;
119 case UART_PARITY_ODD: parity = (UART_LCR_PEN); break;
120 case UART_PARITY_NONE: parity = 0; break;
125 if (bas->rclk && baudrate) {
126 uart_setreg(bas, UART_CDDL_REG, bas->rclk/16/baudrate);
130 uart_setreg(bas, UART_LCR_REG, databits |
131 (stopbits==1?0:UART_LCR_STB_15) |
137 mtk_uart_term(struct uart_bas *bas)
139 uart_setreg(bas, UART_MCR_REG, 0);
144 mtk_uart_putc(struct uart_bas *bas, int c)
147 if (!uart_output) return;
149 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
150 uart_setreg(bas, UART_TX_REG, c);
152 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE));
156 mtk_uart_rxready(struct uart_bas *bas)
158 if (uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)
164 mtk_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
170 while (!(uart_getreg(bas, UART_LSR_REG) & UART_LSR_DR)) {
176 c = uart_getreg(bas, UART_RX_REG);
184 * High-level UART interface.
186 struct uart_mtk_softc {
187 struct uart_softc base;
192 static int mtk_uart_bus_attach(struct uart_softc *);
193 static int mtk_uart_bus_detach(struct uart_softc *);
194 static int mtk_uart_bus_flush(struct uart_softc *, int);
195 static int mtk_uart_bus_getsig(struct uart_softc *);
196 static int mtk_uart_bus_ioctl(struct uart_softc *, int, intptr_t);
197 static int mtk_uart_bus_ipend(struct uart_softc *);
198 static int mtk_uart_bus_param(struct uart_softc *, int, int, int, int);
199 static int mtk_uart_bus_probe(struct uart_softc *);
200 static int mtk_uart_bus_receive(struct uart_softc *);
201 static int mtk_uart_bus_setsig(struct uart_softc *, int);
202 static int mtk_uart_bus_transmit(struct uart_softc *);
203 static void mtk_uart_bus_grab(struct uart_softc *);
204 static void mtk_uart_bus_ungrab(struct uart_softc *);
206 static kobj_method_t uart_mtk_methods[] = {
207 KOBJMETHOD(uart_attach, mtk_uart_bus_attach),
208 KOBJMETHOD(uart_detach, mtk_uart_bus_detach),
209 KOBJMETHOD(uart_flush, mtk_uart_bus_flush),
210 KOBJMETHOD(uart_getsig, mtk_uart_bus_getsig),
211 KOBJMETHOD(uart_ioctl, mtk_uart_bus_ioctl),
212 KOBJMETHOD(uart_ipend, mtk_uart_bus_ipend),
213 KOBJMETHOD(uart_param, mtk_uart_bus_param),
214 KOBJMETHOD(uart_probe, mtk_uart_bus_probe),
215 KOBJMETHOD(uart_receive, mtk_uart_bus_receive),
216 KOBJMETHOD(uart_setsig, mtk_uart_bus_setsig),
217 KOBJMETHOD(uart_transmit, mtk_uart_bus_transmit),
218 KOBJMETHOD(uart_grab, mtk_uart_bus_grab),
219 KOBJMETHOD(uart_ungrab, mtk_uart_bus_ungrab),
223 struct uart_class uart_mtk_class = {
226 sizeof(struct uart_mtk_softc),
227 .uc_ops = &uart_mtk_ops,
228 .uc_range = 1, /* use hinted range */
232 static struct ofw_compat_data compat_data[] = {
233 { "ralink,rt2880-uart", (uintptr_t)&uart_mtk_class },
234 { "ralink,rt3050-uart", (uintptr_t)&uart_mtk_class },
235 { "ralink,rt3352-uart", (uintptr_t)&uart_mtk_class },
236 { "ralink,rt3883-uart", (uintptr_t)&uart_mtk_class },
237 { "ralink,rt5350-uart", (uintptr_t)&uart_mtk_class },
238 { "ralink,mt7620a-uart", (uintptr_t)&uart_mtk_class },
239 { NULL, (uintptr_t)NULL },
241 UART_FDT_CLASS_AND_DEVICE(compat_data);
243 #define SIGCHG(c, i, s, d) \
245 i |= (i & s) ? s : s | d; \
247 i = (i & s) ? (i & ~s) | d : i; \
251 * Disable TX interrupt. uart should be locked
254 mtk_uart_disable_txintr(struct uart_softc *sc)
256 struct uart_bas *bas = &sc->sc_bas;
259 cr = uart_getreg(bas, UART_IER_REG);
260 cr &= ~UART_IER_ETBEI;
261 uart_setreg(bas, UART_IER_REG, cr);
266 * Enable TX interrupt. uart should be locked
269 mtk_uart_enable_txintr(struct uart_softc *sc)
271 struct uart_bas *bas = &sc->sc_bas;
274 cr = uart_getreg(bas, UART_IER_REG);
275 cr |= UART_IER_ETBEI;
276 uart_setreg(bas, UART_IER_REG, cr);
281 mtk_uart_bus_attach(struct uart_softc *sc)
283 struct uart_bas *bas;
284 struct uart_devinfo *di;
285 struct uart_mtk_softc *usc = (struct uart_mtk_softc *)sc;
290 bas->rclk = mtk_soc_get_uartclk();
293 if (sc->sc_sysdev != NULL) {
295 mtk_uart_init(bas, di->baudrate, di->databits, di->stopbits,
298 mtk_uart_init(bas, 57600, 8, 1, 0);
301 sc->sc_rxfifosz = 16;
302 sc->sc_txfifosz = 16;
304 (void)mtk_uart_bus_getsig(sc);
307 uart_setreg(bas, UART_FCR_REG,
308 uart_getreg(bas, UART_FCR_REG) |
309 UART_FCR_FIFOEN | UART_FCR_TXTGR_1 | UART_FCR_RXTGR_1);
311 /* Enable interrupts */
312 usc->ier_mask = 0xf0;
313 uart_setreg(bas, UART_IER_REG,
314 UART_IER_EDSSI | UART_IER_ELSI | UART_IER_ERBFI);
321 mtk_uart_bus_detach(struct uart_softc *sc)
327 mtk_uart_bus_flush(struct uart_softc *sc, int what)
329 struct uart_bas *bas = &sc->sc_bas;
330 uint32_t fcr = uart_getreg(bas, UART_FCR_REG);
332 if (what & UART_FLUSH_TRANSMITTER) {
333 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_TXRST);
336 if (what & UART_FLUSH_RECEIVER) {
337 uart_setreg(bas, UART_FCR_REG, fcr|UART_FCR_RXRST);
340 uart_setreg(bas, UART_FCR_REG, fcr);
346 mtk_uart_bus_getsig(struct uart_softc *sc)
348 uint32_t new, old, sig;
355 uart_lock(sc->sc_hwmtx);
356 bes = uart_getreg(&sc->sc_bas, UART_MSR_REG);
357 uart_unlock(sc->sc_hwmtx);
358 /* XXX: chip can show delta */
359 SIGCHG(bes & UART_MSR_CTS, sig, SER_CTS, SER_DCTS);
360 SIGCHG(bes & UART_MSR_DCD, sig, SER_DCD, SER_DDCD);
361 SIGCHG(bes & UART_MSR_DSR, sig, SER_DSR, SER_DDSR);
362 new = sig & ~SER_MASK_DELTA;
363 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
369 mtk_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
371 struct uart_bas *bas;
372 int baudrate, divisor, error;
376 uart_lock(sc->sc_hwmtx);
378 case UART_IOCTL_BREAK:
379 /* TODO: Send BREAK */
381 case UART_IOCTL_BAUD:
382 divisor = uart_getreg(bas, UART_CDDL_REG);
383 baudrate = bas->rclk / (divisor * 16);
384 *(int*)data = baudrate;
390 uart_unlock(sc->sc_hwmtx);
395 mtk_uart_bus_ipend(struct uart_softc *sc)
397 struct uart_bas *bas;
399 uint8_t iir, lsr, msr;
406 uart_lock(sc->sc_hwmtx);
407 iir = uart_getreg(&sc->sc_bas, UART_IIR_REG);
408 lsr = uart_getreg(&sc->sc_bas, UART_LSR_REG);
409 uart_setreg(&sc->sc_bas, UART_LSR_REG, lsr);
410 msr = uart_getreg(&sc->sc_bas, UART_MSR_REG);
411 uart_setreg(&sc->sc_bas, UART_MSR_REG, msr);
412 if (iir & UART_IIR_INTP) {
413 uart_unlock(sc->sc_hwmtx);
416 switch ((iir >> 1) & 0x07) {
417 case UART_IIR_ID_THRE:
418 ipend |= SER_INT_TXIDLE;
420 case UART_IIR_ID_DR2:
421 mtk_uart_bus_flush(sc, UART_FLUSH_RECEIVER);
424 ipend |= SER_INT_RXREADY;
426 case UART_IIR_ID_MST:
427 case UART_IIR_ID_LINESTATUS:
428 ipend |= SER_INT_SIGCHG;
429 if (lsr & UART_LSR_BI)
430 ipend |= SER_INT_BREAK;
431 if (lsr & UART_LSR_OE)
432 ipend |= SER_INT_OVERRUN;
435 /* XXX: maybe return error here */
439 uart_unlock(sc->sc_hwmtx);
445 mtk_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
446 int stopbits, int parity)
448 uart_lock(sc->sc_hwmtx);
449 mtk_uart_init(&sc->sc_bas, baudrate, databits, stopbits, parity);
450 uart_unlock(sc->sc_hwmtx);
455 mtk_uart_bus_probe(struct uart_softc *sc)
459 error = mtk_uart_probe(&sc->sc_bas);
463 device_set_desc(sc->sc_dev, "MTK UART Controller");
469 mtk_uart_bus_receive(struct uart_softc *sc)
471 struct uart_bas *bas;
476 uart_lock(sc->sc_hwmtx);
477 lsr = uart_getreg(bas, UART_LSR_REG);
478 while ((lsr & UART_LSR_DR)) {
479 if (uart_rx_full(sc)) {
480 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
484 xc = uart_getreg(bas, UART_RX_REG);
485 if (lsr & UART_LSR_FE)
486 xc |= UART_STAT_FRAMERR;
487 if (lsr & UART_LSR_PE)
488 xc |= UART_STAT_PARERR;
489 if (lsr & UART_LSR_OE)
490 xc |= UART_STAT_OVERRUN;
493 lsr = uart_getreg(bas, UART_LSR_REG);
496 uart_unlock(sc->sc_hwmtx);
501 mtk_uart_bus_setsig(struct uart_softc *sc, int sig)
503 /* TODO: implement (?) */
508 mtk_uart_bus_transmit(struct uart_softc *sc)
510 struct uart_bas *bas = &sc->sc_bas;
513 if (!uart_output) return (0);
516 uart_lock(sc->sc_hwmtx);
517 while ((uart_getreg(bas, UART_LSR_REG) & UART_LSR_THRE) == 0);
518 mtk_uart_enable_txintr(sc);
519 for (i = 0; i < sc->sc_txdatasz; i++) {
520 uart_setreg(bas, UART_TX_REG, sc->sc_txbuf[i]);
524 uart_unlock(sc->sc_hwmtx);
529 mtk_uart_bus_grab(struct uart_softc *sc)
531 struct uart_bas *bas = &sc->sc_bas;
532 struct uart_mtk_softc *usc = (struct uart_mtk_softc *)sc;
534 uart_lock(sc->sc_hwmtx);
535 usc->ier = uart_getreg(bas, UART_IER_REG);
536 uart_setreg(bas, UART_IER_REG, usc->ier & usc->ier_mask);
538 uart_unlock(sc->sc_hwmtx);
542 mtk_uart_bus_ungrab(struct uart_softc *sc)
544 struct uart_mtk_softc *usc = (struct uart_mtk_softc *)sc;
545 struct uart_bas *bas = &sc->sc_bas;
547 uart_lock(sc->sc_hwmtx);
548 uart_setreg(bas, UART_IER_REG, usc->ier);
550 uart_unlock(sc->sc_hwmtx);