1 /* $NetBSD: meson_uart.c,v 1.10 2026/11/10 03:04:57 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Thomas of 3am Software Foundry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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 copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "opt_console.h" 33 #include "locators.h" 34 35 #include <sys/cdefs.h> 36 37 __KERNEL_RCSID(1, "$NetBSD: meson_uart.c,v 1.10 2026/11/10 03:04:57 thorpej Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/bus.h> 41 #include <sys/device.h> 42 #include <sys/conf.h> 43 #include <sys/intr.h> 44 #include <sys/systm.h> 45 #include <sys/time.h> 46 #include <sys/termios.h> 47 #include <sys/kauth.h> 48 #include <sys/lwp.h> 49 #include <sys/tty.h> 50 51 #include <dev/cons.h> 52 53 #include <dev/fdt/fdtvar.h> 54 #include <dev/fdt/fdt_console.h> 55 56 #include <arm/amlogic/meson_uart.h> 57 58 static int meson_uart_match(device_t, cfdata_t, void *); 59 static void meson_uart_attach(device_t, device_t, void *); 60 61 static int meson_uart_intr(void *); 62 static void meson_uart_rxsoft(void *); 63 64 static int meson_uart_cngetc(dev_t); 65 static void meson_uart_cnputc(dev_t, int); 66 static void meson_uart_cnpollc(dev_t, int); 67 68 static void meson_uart_start(struct tty *); 69 static int meson_uart_param(struct tty *, struct termios *); 70 71 extern struct cfdriver mesonuart_cd; 72 73 static const struct device_compatible_entry compat_data[] = { 74 { .compat = "amlogic,meson6-uart" }, 75 { .compat = "amlogic,meson8-uart" }, 76 { .compat = "amlogic,meson8b-uart" }, 77 { .compat = "amlogic,meson-gx-uart" }, 78 DEVICE_COMPAT_EOL 79 }; 80 81 struct meson_uart_softc { 82 device_t sc_dev; 83 bus_space_tag_t sc_bst; 84 bus_space_handle_t sc_bsh; 85 kmutex_t sc_intr_lock; 86 void *sc_ih; 87 void *sc_sih; 88 89 struct tty *sc_tty; 90 91 int sc_ospeed; 92 unsigned int sc_rbuf_w; /* write ptr of sc_rbuf[] */ 93 unsigned int sc_rbuf_r; /* read ptr of sc_rbuf[] */ 94 tcflag_t sc_cflag; 95 96 u_char sc_buf[1024]; 97 #define MESON_RBUFSZ 128 /* must be 2^n */ 98 u_char sc_rbuf[MESON_RBUFSZ]; /* good enough for sizeof RXFIFO */ 99 }; 100 101 static int meson_uart_console_phandle = -1; 102 103 static struct meson_uart_softc meson_uart_cnsc; 104 105 static struct cnm_state meson_uart_cnm_state; 106 107 struct consdev meson_uart_consdev = { 108 .cn_getc = meson_uart_cngetc, 109 .cn_putc = meson_uart_cnputc, 110 .cn_pollc = meson_uart_cnpollc, 111 .cn_dev = NODEV, 112 .cn_pri = CN_NORMAL, 113 }; 114 115 static dev_type_open(meson_uart_open); 116 static dev_type_open(meson_uart_close); 117 static dev_type_read(meson_uart_read); 118 static dev_type_write(meson_uart_write); 119 static dev_type_ioctl(meson_uart_ioctl); 120 static dev_type_tty(meson_uart_tty); 121 static dev_type_poll(meson_uart_poll); 122 static dev_type_stop(meson_uart_stop); 123 124 const struct cdevsw mesonuart_cdevsw = { 125 .d_open = meson_uart_open, 126 .d_close = meson_uart_close, 127 .d_read = meson_uart_read, 128 .d_write = meson_uart_write, 129 .d_ioctl = meson_uart_ioctl, 130 .d_stop = meson_uart_stop, 131 .d_tty = meson_uart_tty, 132 .d_poll = meson_uart_poll, 133 .d_mmap = nommap, 134 .d_kqfilter = ttykqfilter, 135 .d_discard = nodiscard, 136 .d_flag = D_TTY 137 }; 138 139 static int meson_uart_cmajor = -1; 140 141 CFATTACH_DECL_NEW(meson_uart, sizeof(struct meson_uart_softc), 142 meson_uart_match, meson_uart_attach, NULL, NULL); 143 144 static int 145 meson_uart_match(device_t parent, cfdata_t cf, void *aux) 146 { 147 struct fdt_attach_args * const faa = aux; 148 149 return of_compatible_match(faa->faa_phandle, compat_data); 150 } 151 152 static void 153 meson_uart_attach(device_t parent, device_t self, void *aux) 154 { 155 struct meson_uart_softc * const sc = device_private(self); 156 struct fdt_attach_args * const faa = aux; 157 const int phandle = faa->faa_phandle; 158 char intrstr[128]; 159 bus_addr_t addr; 160 bus_size_t size; 161 struct tty *tp; 162 int major, minor, error; 163 uint32_t misc, control; 164 165 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 166 aprint_error(": couldn't get registers\n"); 167 return; 168 } 169 170 sc->sc_dev = self; 171 sc->sc_bst = faa->faa_bst; 172 173 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 174 if (error != 0) { 175 aprint_error(": couldn't map registers\n"); 176 return; 177 } 178 179 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 180 aprint_error(": failed to decode interrupt\n"); 181 return; 182 } 183 184 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL); 185 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, 186 FDT_INTR_MPSAFE, meson_uart_intr, sc, device_xname(self)); 187 if (sc->sc_ih == NULL) { 188 aprint_error(": failed to establish interrupt on %s\n", 189 intrstr); 190 return; 191 } 192 193 sc->sc_sih = softint_establish(SOFTINT_SERIAL, meson_uart_rxsoft, sc); 194 if (sc->sc_sih == NULL) { 195 aprint_error(": failed to establish softint\n"); 196 return; 197 } 198 199 if (meson_uart_cmajor == -1) { 200 /* allocate a major number */ 201 int bmajor = -1, cmajor = -1; 202 error = devsw_attach("mesonuart", NULL, &bmajor, 203 &mesonuart_cdevsw, &cmajor); 204 if (error) { 205 aprint_error(": couldn't allocate major number\n"); 206 return; 207 } 208 meson_uart_cmajor = cmajor; 209 } 210 211 major = cdevsw_lookup_major(&mesonuart_cdevsw); 212 minor = device_unit(self); 213 214 tp = sc->sc_tty = tty_alloc(); 215 tp->t_oproc = meson_uart_start; 216 tp->t_param = meson_uart_param; 217 tp->t_dev = makedev(major, minor); 218 tp->t_sc = sc; 219 tty_attach(tp); 220 221 aprint_naive("\n"); 222 if (meson_uart_console_phandle == phandle) { 223 cn_tab->cn_dev = tp->t_dev; 224 aprint_normal(": console"); 225 } 226 aprint_normal("\n"); 227 228 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 229 230 misc = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_MISC_REG); 231 misc &= ~UART_MISC_TX_IRQ_CNT; 232 misc |= __SHIFTIN(0, UART_MISC_TX_IRQ_CNT); 233 misc &= ~UART_MISC_RX_IRQ_CNT; 234 misc |= __SHIFTIN(1, UART_MISC_RX_IRQ_CNT); 235 bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_MISC_REG, misc); 236 237 control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG); 238 control &= ~(UART_CONTROL_TX_INT_EN|UART_CONTROL_RX_INT_EN); 239 bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control); 240 } 241 242 static int 243 meson_uart_cngetc(dev_t dev) 244 { 245 bus_space_tag_t bst = meson_uart_cnsc.sc_bst; 246 bus_space_handle_t bsh = meson_uart_cnsc.sc_bsh; 247 uint32_t status; 248 int s, c; 249 250 s = splserial(); 251 252 status = bus_space_read_4(bst, bsh, UART_STATUS_REG); 253 if (status & UART_STATUS_RX_EMPTY) { 254 splx(s); 255 return -1; 256 } 257 258 c = bus_space_read_4(bst, bsh, UART_RFIFO_REG) & 0xff; 259 cn_check_magic(dev, c, &meson_uart_cnm_state); 260 261 splx(s); 262 263 return c; 264 } 265 266 static void 267 meson_uart_cnputc(dev_t dev, int c) 268 { 269 bus_space_tag_t bst = meson_uart_cnsc.sc_bst; 270 bus_space_handle_t bsh = meson_uart_cnsc.sc_bsh; 271 int s; 272 273 s = splserial(); 274 275 while ((bus_space_read_4(bst, bsh, UART_STATUS_REG) & UART_STATUS_TX_FULL) != 0) 276 ; 277 278 bus_space_write_4(bst, bsh, UART_WFIFO_REG, c); 279 280 splx(s); 281 } 282 283 284 static void 285 meson_uart_cnpollc(dev_t dev, int on) 286 { 287 } 288 289 static int 290 meson_uart_open(dev_t dev, int flag, int mode, lwp_t *l) 291 { 292 struct meson_uart_softc *sc = 293 device_lookup_private(&mesonuart_cd, minor(dev)); 294 struct tty *tp = sc->sc_tty; 295 uint32_t control; 296 297 if (kauth_authorize_device_tty(l->l_cred, 298 KAUTH_DEVICE_TTY_OPEN, tp) != 0) { 299 return EBUSY; 300 } 301 302 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) { 303 tp->t_dev = dev; 304 ttychars(tp); 305 tp->t_iflag = TTYDEF_IFLAG; 306 tp->t_oflag = TTYDEF_OFLAG; 307 tp->t_cflag = TTYDEF_CFLAG; 308 tp->t_lflag = TTYDEF_LFLAG; 309 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 310 ttsetwater(tp); 311 312 control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG); 313 control |= UART_CONTROL_RX_INT_EN; 314 bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control); 315 } 316 tp->t_state |= TS_CARR_ON; 317 318 return tp->t_linesw->l_open(dev, tp); 319 } 320 321 static int 322 meson_uart_close(dev_t dev, int flag, int mode, lwp_t *l) 323 { 324 struct meson_uart_softc *sc = 325 device_lookup_private(&mesonuart_cd, minor(dev)); 326 struct tty *tp = sc->sc_tty; 327 uint32_t control; 328 329 tp->t_linesw->l_close(tp, flag); 330 ttyclose(tp); 331 332 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 333 control = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG); 334 control &= ~UART_CONTROL_RX_INT_EN; 335 bus_space_write_4(sc->sc_bst, sc->sc_bsh, UART_CONTROL_REG, control); 336 } 337 338 return 0; 339 } 340 341 static int 342 meson_uart_read(dev_t dev, struct uio *uio, int flag) 343 { 344 struct meson_uart_softc *sc = 345 device_lookup_private(&mesonuart_cd, minor(dev)); 346 struct tty *tp = sc->sc_tty; 347 348 return tp->t_linesw->l_read(tp, uio, flag); 349 } 350 351 static int 352 meson_uart_write(dev_t dev, struct uio *uio, int flag) 353 { 354 struct meson_uart_softc *sc = 355 device_lookup_private(&mesonuart_cd, minor(dev)); 356 struct tty *tp = sc->sc_tty; 357 358 return tp->t_linesw->l_write(tp, uio, flag); 359 } 360 361 static int 362 meson_uart_poll(dev_t dev, int events, lwp_t *l) 363 { 364 struct meson_uart_softc *sc = 365 device_lookup_private(&mesonuart_cd, minor(dev)); 366 struct tty *tp = sc->sc_tty; 367 368 return tp->t_linesw->l_poll(tp, events, l); 369 } 370 371 static int 372 meson_uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 373 { 374 struct meson_uart_softc *sc = 375 device_lookup_private(&mesonuart_cd, minor(dev)); 376 struct tty *tp = sc->sc_tty; 377 int error; 378 379 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l); 380 if (error != EPASSTHROUGH) 381 return error; 382 383 return ttioctl(tp, cmd, data, flag, l); 384 } 385 386 static struct tty * 387 meson_uart_tty(dev_t dev) 388 { 389 struct meson_uart_softc *sc = 390 device_lookup_private(&mesonuart_cd, minor(dev)); 391 392 return sc->sc_tty; 393 } 394 395 static void 396 meson_uart_stop(struct tty *tp, int flag) 397 { 398 } 399 400 static void 401 meson_uart_start(struct tty *tp) 402 { 403 struct meson_uart_softc *sc = tp->t_sc; 404 u_char *p = sc->sc_buf; 405 int s, brem; 406 407 s = spltty(); 408 409 if (tp->t_state & (TS_TTSTOP | TS_BUSY | TS_TIMEOUT)) { 410 splx(s); 411 return; 412 } 413 tp->t_state |= TS_BUSY; 414 415 splx(s); 416 417 for (brem = q_to_b(&tp->t_outq, sc->sc_buf, sizeof(sc->sc_buf)); 418 brem > 0; 419 brem--, p++) { 420 while ((bus_space_read_4(sc->sc_bst, sc->sc_bsh, 421 UART_STATUS_REG) & UART_STATUS_TX_FULL) != 0) 422 ; 423 424 bus_space_write_4(sc->sc_bst, sc->sc_bsh, 425 UART_WFIFO_REG, *p); 426 } 427 428 s = spltty(); 429 tp->t_state &= ~TS_BUSY; 430 if (ttypull(tp)) { 431 tp->t_state |= TS_TIMEOUT; 432 callout_schedule(&tp->t_rstrt_ch, 1); 433 } 434 splx(s); 435 } 436 437 static int 438 meson_uart_param(struct tty *tp, struct termios *t) 439 { 440 441 tp->t_ispeed = t->c_ispeed; 442 tp->t_ospeed = t->c_ospeed; 443 tp->t_cflag = t->c_cflag; 444 445 return 0; 446 } 447 448 static int 449 meson_uart_intr(void *priv) 450 { 451 struct meson_uart_softc *sc = priv; 452 struct tty *tp = sc->sc_tty; 453 uint32_t status, c; 454 455 mutex_spin_enter(&sc->sc_intr_lock); 456 for (;;) { 457 status = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 458 UART_STATUS_REG); 459 if (status & UART_STATUS_RX_EMPTY) { 460 break; 461 } 462 if (status & UART_STATUS_BREAK) { 463 if (cn_check_magic(tp->t_dev, CNC_BREAK, 464 &meson_uart_cnm_state)) { 465 continue; 466 } 467 } 468 469 c = bus_space_read_4(sc->sc_bst, sc->sc_bsh, UART_RFIFO_REG); 470 if (cn_check_magic(tp->t_dev, c & 0xff, 471 &meson_uart_cnm_state)) { 472 continue; 473 } 474 475 if ((sc->sc_rbuf_w - sc->sc_rbuf_r) >= (MESON_RBUFSZ - 1)) 476 continue; 477 sc->sc_rbuf[sc->sc_rbuf_w++ & (MESON_RBUFSZ - 1)] = c; 478 } 479 mutex_spin_exit(&sc->sc_intr_lock); 480 481 if (sc->sc_rbuf_w != sc->sc_rbuf_r) 482 softint_schedule(sc->sc_sih); 483 484 return 0; 485 } 486 487 static void 488 meson_uart_rxsoft(void *priv) 489 { 490 struct meson_uart_softc *sc = priv; 491 struct tty *tp = sc->sc_tty; 492 int c; 493 494 while (sc->sc_rbuf_w != sc->sc_rbuf_r) { 495 c = sc->sc_rbuf[sc->sc_rbuf_r++ & (MESON_RBUFSZ - 1)]; 496 if (tp->t_linesw->l_rint(c & 0xff, tp) == -1) 497 break; 498 } 499 } 500 501 static int 502 meson_uart_console_match(int phandle) 503 { 504 return of_compatible_match(phandle, compat_data); 505 } 506 507 static void 508 meson_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 509 { 510 struct meson_uart_softc *sc = &meson_uart_cnsc; 511 const int phandle = faa->faa_phandle; 512 bus_addr_t addr; 513 bus_size_t size; 514 int error; 515 516 fdtbus_get_reg(phandle, 0, &addr, &size); 517 518 sc->sc_bst = faa->faa_bst; 519 sc->sc_ospeed = fdtbus_get_stdout_speed(); 520 if (sc->sc_ospeed < 0) 521 sc->sc_ospeed = 115200; 522 sc->sc_cflag = fdtbus_get_stdout_flags(); 523 524 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 525 if (error != 0) 526 panic("failed to map console, error = %d", error); 527 528 cn_tab = &meson_uart_consdev; 529 cn_init_magic(&meson_uart_cnm_state); 530 cn_set_magic("\047\001"); 531 532 meson_uart_console_phandle = phandle; 533 } 534 535 static const struct fdt_console meson_uart_console = { 536 .match = meson_uart_console_match, 537 .consinit = meson_uart_console_consinit, 538 }; 539 540 FDT_CONSOLE(meson_uart, &meson_uart_console); 541