2 * Copyright (c) 2014 Ganbold Tsagaankhuu <ganbold@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 /* Qualcomm MSM7K/8K uart driver */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <machine/bus.h>
41 #include <dev/uart/uart.h>
42 #include <dev/uart/uart_cpu.h>
43 #include <dev/uart/uart_cpu_fdt.h>
44 #include <dev/uart/uart_bus.h>
45 #include <dev/uart/uart_dev_msm.h>
49 #define DEF_CLK 7372800
51 #define GETREG(bas, reg) \
52 bus_space_read_4((bas)->bst, (bas)->bsh, (reg))
53 #define SETREG(bas, reg, value) \
54 bus_space_write_4((bas)->bst, (bas)->bsh, (reg), (value))
56 static int msm_uart_param(struct uart_bas *, int, int, int, int);
59 * Low-level UART interface.
61 static int msm_probe(struct uart_bas *bas);
62 static void msm_init(struct uart_bas *bas, int, int, int, int);
63 static void msm_term(struct uart_bas *bas);
64 static void msm_putc(struct uart_bas *bas, int);
65 static int msm_rxready(struct uart_bas *bas);
66 static int msm_getc(struct uart_bas *bas, struct mtx *mtx);
68 extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
71 msm_uart_param(struct uart_bas *bas, int baudrate, int databits,
72 int stopbits, int parity)
80 ulcon |= (UART_DM_5_BPS << 4);
83 ulcon |= (UART_DM_6_BPS << 4);
86 ulcon |= (UART_DM_7_BPS << 4);
89 ulcon |= (UART_DM_8_BPS << 4);
96 case UART_PARITY_NONE:
97 ulcon |= UART_DM_NO_PARITY;
100 ulcon |= UART_DM_ODD_PARITY;
102 case UART_PARITY_EVEN:
103 ulcon |= UART_DM_EVEN_PARITY;
105 case UART_PARITY_SPACE:
106 ulcon |= UART_DM_SPACE_PARITY;
108 case UART_PARITY_MARK:
115 ulcon |= (UART_DM_SBL_1 << 2);
118 ulcon |= (UART_DM_SBL_2 << 2);
123 uart_setreg(bas, UART_DM_MR2, ulcon);
129 struct uart_ops uart_msm_ops = {
134 .rxready = msm_rxready,
139 msm_probe(struct uart_bas *bas)
148 msm_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
155 KASSERT(bas->rclk != 0, ("msm_init: Invalid rclk"));
157 /* Set default parameters */
158 msm_uart_param(bas, baudrate, databits, stopbits, parity);
161 * Configure UART mode registers MR1 and MR2.
162 * Hardware flow control isn't supported.
164 uart_setreg(bas, UART_DM_MR1, 0x0);
166 /* Reset interrupt mask register. */
167 uart_setreg(bas, UART_DM_IMR, 0);
170 * Configure Tx and Rx watermarks configuration registers.
171 * TX watermark value is set to 0 - interrupt is generated when
172 * FIFO level is less than or equal to 0.
174 uart_setreg(bas, UART_DM_TFWR, UART_DM_TFW_VALUE);
176 /* Set RX watermark value */
177 uart_setreg(bas, UART_DM_RFWR, UART_DM_RFW_VALUE);
180 * Configure Interrupt Programming Register.
181 * Set initial Stale timeout value.
183 uart_setreg(bas, UART_DM_IPR, UART_DM_STALE_TIMEOUT_LSB);
185 /* Disable IRDA mode */
186 uart_setreg(bas, UART_DM_IRDA, 0x0);
189 * Configure and enable sim interface if required.
190 * Configure hunt character value in HCR register.
191 * Keep it in reset state.
193 uart_setreg(bas, UART_DM_HCR, 0x0);
195 /* Issue soft reset command */
196 SETREG(bas, UART_DM_CR, UART_DM_RESET_TX);
197 SETREG(bas, UART_DM_CR, UART_DM_RESET_RX);
198 SETREG(bas, UART_DM_CR, UART_DM_RESET_ERROR_STATUS);
199 SETREG(bas, UART_DM_CR, UART_DM_RESET_BREAK_INT);
200 SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
202 /* Enable/Disable Rx/Tx DM interfaces */
203 uart_setreg(bas, UART_DM_DMEN, UART_DM_DMEN_RX_SC_ENABLE);
205 /* Enable transmitter and receiver */
206 uart_setreg(bas, UART_DM_CR, UART_DM_CR_RX_ENABLE);
207 uart_setreg(bas, UART_DM_CR, UART_DM_CR_TX_ENABLE);
213 msm_term(struct uart_bas *bas)
220 msm_putc(struct uart_bas *bas, int c)
225 * Write to NO_CHARS_FOR_TX register the number of characters
226 * to be transmitted. However, before writing TX_FIFO must
227 * be empty as indicated by TX_READY interrupt in IMR register
231 * Check if transmit FIFO is empty.
232 * If not wait for TX_READY interrupt.
235 if (!(uart_getreg(bas, UART_DM_SR) & UART_DM_SR_TXEMT)) {
236 while ((uart_getreg(bas, UART_DM_ISR) & UART_DM_TX_READY) == 0
239 SETREG(bas, UART_DM_CR, UART_DM_CLEAR_TX_READY);
241 /* FIFO is ready, write number of characters to be written */
242 uart_setreg(bas, UART_DM_NO_CHARS_FOR_TX, 1);
244 /* Wait till TX FIFO has space */
245 while ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_TXRDY) == 0)
248 /* TX FIFO has space. Write char */
249 SETREG(bas, UART_DM_TF(0), (c & 0xff));
253 msm_rxready(struct uart_bas *bas)
256 /* Wait for a character to come ready */
257 return ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) ==
262 msm_getc(struct uart_bas *bas, struct mtx *mtx)
268 /* Wait for a character to come ready */
269 while ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) !=
273 /* Check for Overrun error. If so reset Error Status */
274 if (uart_getreg(bas, UART_DM_SR) & UART_DM_SR_UART_OVERRUN)
275 uart_setreg(bas, UART_DM_CR, UART_DM_RESET_ERROR_STATUS);
278 c = uart_getreg(bas, UART_DM_RF(0));
286 * High-level UART interface.
288 struct msm_uart_softc {
289 struct uart_softc base;
293 static int msm_bus_probe(struct uart_softc *sc);
294 static int msm_bus_attach(struct uart_softc *sc);
295 static int msm_bus_flush(struct uart_softc *, int);
296 static int msm_bus_getsig(struct uart_softc *);
297 static int msm_bus_ioctl(struct uart_softc *, int, intptr_t);
298 static int msm_bus_ipend(struct uart_softc *);
299 static int msm_bus_param(struct uart_softc *, int, int, int, int);
300 static int msm_bus_receive(struct uart_softc *);
301 static int msm_bus_setsig(struct uart_softc *, int);
302 static int msm_bus_transmit(struct uart_softc *);
303 static void msm_bus_grab(struct uart_softc *);
304 static void msm_bus_ungrab(struct uart_softc *);
306 static kobj_method_t msm_methods[] = {
307 KOBJMETHOD(uart_probe, msm_bus_probe),
308 KOBJMETHOD(uart_attach, msm_bus_attach),
309 KOBJMETHOD(uart_flush, msm_bus_flush),
310 KOBJMETHOD(uart_getsig, msm_bus_getsig),
311 KOBJMETHOD(uart_ioctl, msm_bus_ioctl),
312 KOBJMETHOD(uart_ipend, msm_bus_ipend),
313 KOBJMETHOD(uart_param, msm_bus_param),
314 KOBJMETHOD(uart_receive, msm_bus_receive),
315 KOBJMETHOD(uart_setsig, msm_bus_setsig),
316 KOBJMETHOD(uart_transmit, msm_bus_transmit),
317 KOBJMETHOD(uart_grab, msm_bus_grab),
318 KOBJMETHOD(uart_ungrab, msm_bus_ungrab),
323 msm_bus_probe(struct uart_softc *sc)
325 struct uart_bas *bas;
330 sc->sc_txfifosz = 64;
331 sc->sc_rxfifosz = 64;
333 device_set_desc(sc->sc_dev, "Qualcomm HSUART");
339 msm_bus_attach(struct uart_softc *sc)
341 struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
342 struct uart_bas *bas = &sc->sc_bas;
347 /* Set TX_READY, TXLEV, RXLEV, RXSTALE */
348 u->ier = UART_DM_IMR_ENABLED;
350 /* Configure Interrupt Mask register IMR */
351 uart_setreg(bas, UART_DM_IMR, u->ier);
357 * Write the current transmit buffer to the TX FIFO.
360 msm_bus_transmit(struct uart_softc *sc)
362 struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
363 struct uart_bas *bas = &sc->sc_bas;
366 uart_lock(sc->sc_hwmtx);
368 /* Write some data */
369 for (i = 0; i < sc->sc_txdatasz; i++) {
371 msm_putc(bas, sc->sc_txbuf[i]);
375 /* TX FIFO is empty now, enable TX_READY interrupt */
376 u->ier |= UART_DM_TX_READY;
377 SETREG(bas, UART_DM_IMR, u->ier);
381 * Inform upper layer that it is transmitting data to hardware,
382 * this will be cleared when TXIDLE interrupt occurs.
385 uart_unlock(sc->sc_hwmtx);
391 msm_bus_setsig(struct uart_softc *sc, int sig)
398 msm_bus_receive(struct uart_softc *sc)
400 struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
401 struct uart_bas *bas;
405 uart_lock(sc->sc_hwmtx);
407 /* Initialize Receive Path and interrupt */
408 SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
409 SETREG(bas, UART_DM_CR, UART_DM_STALE_EVENT_ENABLE);
410 u->ier |= UART_DM_RXLEV;
411 SETREG(bas, UART_DM_IMR, u->ier);
413 /* Loop over until we are full, or no data is available */
414 while (uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) {
415 if (uart_rx_full(sc)) {
416 /* No space left in input buffer */
417 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
422 c = uart_getreg(bas, UART_DM_RF(0));
428 uart_unlock(sc->sc_hwmtx);
434 msm_bus_param(struct uart_softc *sc, int baudrate, int databits,
435 int stopbits, int parity)
439 if (sc->sc_bas.rclk == 0)
440 sc->sc_bas.rclk = DEF_CLK;
442 KASSERT(sc->sc_bas.rclk != 0, ("msm_init: Invalid rclk"));
444 uart_lock(sc->sc_hwmtx);
445 error = msm_uart_param(&sc->sc_bas, baudrate, databits, stopbits,
447 uart_unlock(sc->sc_hwmtx);
453 msm_bus_ipend(struct uart_softc *sc)
455 struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
456 struct uart_bas *bas = &sc->sc_bas;
460 uart_lock(sc->sc_hwmtx);
463 isr = GETREG(bas, UART_DM_MISR);
467 /* Uart RX starting, notify upper layer */
468 if (isr & UART_DM_RXLEV) {
469 u->ier &= ~UART_DM_RXLEV;
470 SETREG(bas, UART_DM_IMR, u->ier);
472 ipend |= SER_INT_RXREADY;
475 /* Stale RX interrupt */
476 if (isr & UART_DM_RXSTALE) {
477 /* Disable and reset it */
478 SETREG(bas, UART_DM_CR, UART_DM_STALE_EVENT_DISABLE);
479 SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
481 ipend |= SER_INT_RXREADY;
484 /* TX READY interrupt */
485 if (isr & UART_DM_TX_READY) {
487 SETREG(bas, UART_DM_CR, UART_DM_CLEAR_TX_READY);
489 /* Disable TX_READY */
490 u->ier &= ~UART_DM_TX_READY;
491 SETREG(bas, UART_DM_IMR, u->ier);
494 if (sc->sc_txbusy != 0)
495 ipend |= SER_INT_TXIDLE;
498 if (isr & UART_DM_TXLEV) {
499 /* TX FIFO is empty */
500 u->ier &= ~UART_DM_TXLEV;
501 SETREG(bas, UART_DM_IMR, u->ier);
504 if (sc->sc_txbusy != 0)
505 ipend |= SER_INT_TXIDLE;
508 uart_unlock(sc->sc_hwmtx);
513 msm_bus_flush(struct uart_softc *sc, int what)
520 msm_bus_getsig(struct uart_softc *sc)
527 msm_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
534 msm_bus_grab(struct uart_softc *sc)
536 struct uart_bas *bas = &sc->sc_bas;
539 * XXX: Turn off all interrupts to enter polling mode. Leave the
540 * saved mask alone. We'll restore whatever it was in ungrab.
542 uart_lock(sc->sc_hwmtx);
543 SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
544 SETREG(bas, UART_DM_IMR, 0);
546 uart_unlock(sc->sc_hwmtx);
550 msm_bus_ungrab(struct uart_softc *sc)
552 struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
553 struct uart_bas *bas = &sc->sc_bas;
556 * Restore previous interrupt mask
558 uart_lock(sc->sc_hwmtx);
559 SETREG(bas, UART_DM_IMR, u->ier);
561 uart_unlock(sc->sc_hwmtx);
564 static struct uart_class uart_msm_class = {
567 sizeof(struct msm_uart_softc),
568 .uc_ops = &uart_msm_ops,
574 static struct ofw_compat_data compat_data[] = {
575 {"qcom,msm-uartdm-v1.4", (uintptr_t)&uart_msm_class},
576 {"qcom,msm-uartdm", (uintptr_t)&uart_msm_class},
577 {NULL, (uintptr_t)NULL},
579 UART_FDT_CLASS_AND_DEVICE(compat_data);