1 /* $NetBSD: exynos_uart.c,v 1.10 2026/11/10 03:04:58 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2013-2021 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 and Jared McNeill. 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 "locators.h" 33 34 #include <sys/cdefs.h> 35 36 __KERNEL_RCSID(1, "$NetBSD: exynos_uart.c,v 1.10 2026/11/10 03:04:58 thorpej Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/device.h> 41 #include <sys/conf.h> 42 #include <sys/intr.h> 43 #include <sys/systm.h> 44 #include <sys/time.h> 45 #include <sys/termios.h> 46 #include <sys/kauth.h> 47 #include <sys/lwp.h> 48 #include <sys/tty.h> 49 50 #include <dev/cons.h> 51 52 #include <dev/fdt/fdtvar.h> 53 #include <dev/fdt/fdt_console.h> 54 55 #include <arm/samsung/sscom_reg.h> 56 57 static int exynos_uart_match(device_t, cfdata_t, void *); 58 static void exynos_uart_attach(device_t, device_t, void *); 59 60 static int exynos_uart_intr(void *); 61 62 static int exynos_uart_cngetc(dev_t); 63 static void exynos_uart_cnputc(dev_t, int); 64 static void exynos_uart_cnpollc(dev_t, int); 65 66 static void exynos_uart_start(struct tty *); 67 static int exynos_uart_param(struct tty *, struct termios *); 68 69 extern struct cfdriver exuart_cd; 70 71 enum exynos_uart_type { 72 EXYNOS_UART_SAMSUNG, 73 EXYNOS_UART_APPLE, 74 }; 75 76 struct exynos_uart_config { 77 enum exynos_uart_type type; 78 uint32_t rxfull; 79 uint32_t txfull; 80 uint32_t rxcount; 81 }; 82 83 struct exynos_uart_softc { 84 device_t sc_dev; 85 bus_space_tag_t sc_bst; 86 bus_space_handle_t sc_bsh; 87 kmutex_t sc_lock; 88 u_int sc_freq; 89 void *sc_ih; 90 91 bool sc_console; 92 struct tty *sc_tty; 93 94 int sc_ospeed; 95 tcflag_t sc_cflag; 96 97 const struct exynos_uart_config *sc_conf; 98 99 u_char sc_buf[1024]; 100 }; 101 102 #define RD4(sc, reg) \ 103 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 104 #define WR4(sc, reg, val) \ 105 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 106 107 static bus_addr_t exynos_uart_consaddr; 108 109 static struct exynos_uart_softc exynos_uart_cnsc; 110 111 static struct cnm_state exynos_uart_cnm_state; 112 113 struct consdev exynos_uart_consdev = { 114 .cn_getc = exynos_uart_cngetc, 115 .cn_putc = exynos_uart_cnputc, 116 .cn_pollc = exynos_uart_cnpollc, 117 .cn_dev = NODEV, 118 .cn_pri = CN_NORMAL, 119 }; 120 121 static dev_type_open(exynos_uart_open); 122 static dev_type_open(exynos_uart_close); 123 static dev_type_read(exynos_uart_read); 124 static dev_type_write(exynos_uart_write); 125 static dev_type_ioctl(exynos_uart_ioctl); 126 static dev_type_tty(exynos_uart_tty); 127 static dev_type_poll(exynos_uart_poll); 128 static dev_type_stop(exynos_uart_stop); 129 130 const struct cdevsw exuart_cdevsw = { 131 .d_open = exynos_uart_open, 132 .d_close = exynos_uart_close, 133 .d_read = exynos_uart_read, 134 .d_write = exynos_uart_write, 135 .d_ioctl = exynos_uart_ioctl, 136 .d_stop = exynos_uart_stop, 137 .d_tty = exynos_uart_tty, 138 .d_poll = exynos_uart_poll, 139 .d_mmap = nommap, 140 .d_kqfilter = ttykqfilter, 141 .d_discard = nodiscard, 142 .d_flag = D_TTY 143 }; 144 145 static int exynos_uart_cmajor = -1; 146 147 static const struct exynos_uart_config exynos_uart_samsung = { 148 .type = EXYNOS_UART_SAMSUNG, 149 .rxfull = UFSTAT_RXFULL, 150 .txfull = UFSTAT_TXFULL, 151 .rxcount = UFSTAT_RXCOUNT, 152 }; 153 154 static const struct exynos_uart_config exynos_uart_apple = { 155 .type = EXYNOS_UART_APPLE, 156 .rxfull = UFSTAT_S5L_RXFULL, 157 .txfull = UFSTAT_S5L_TXFULL, 158 .rxcount = UFSTAT_S5L_RXCOUNT, 159 }; 160 161 static const struct device_compatible_entry compat_data[] = { 162 { .compat = "samsung,exynos4210-uart", .data = &exynos_uart_samsung }, 163 { .compat = "apple,s5l-uart", .data = &exynos_uart_apple }, 164 DEVICE_COMPAT_EOL 165 }; 166 167 CFATTACH_DECL_NEW(exynos_uart, sizeof(struct exynos_uart_softc), 168 exynos_uart_match, exynos_uart_attach, NULL, NULL); 169 170 static int 171 exynos_uart_match(device_t parent, cfdata_t cf, void *aux) 172 { 173 struct fdt_attach_args * const faa = aux; 174 175 return of_compatible_match(faa->faa_phandle, compat_data); 176 } 177 178 static void 179 exynos_uart_attach(device_t parent, device_t self, void *aux) 180 { 181 struct exynos_uart_softc * const sc = device_private(self); 182 struct fdt_attach_args * const faa = aux; 183 const int phandle = faa->faa_phandle; 184 char intrstr[128]; 185 struct clk *clk_uart, *clk_uart_baud0; 186 struct tty *tp; 187 int major, minor; 188 bus_addr_t addr; 189 bus_size_t size; 190 uint32_t ucon; 191 192 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 193 aprint_error(": couldn't get registers\n"); 194 return; 195 } 196 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 197 aprint_error(": failed to decode interrupt\n"); 198 return; 199 } 200 clk_uart = fdtbus_clock_get(phandle, "uart"); 201 if (clk_uart == NULL || clk_enable(clk_uart) != 0) { 202 aprint_error(": failed to enable uart clock\n"); 203 return; 204 } 205 clk_uart_baud0 = fdtbus_clock_get(phandle, "clk_uart_baud0"); 206 if (clk_uart_baud0 == NULL || clk_enable(clk_uart_baud0) != 0) { 207 aprint_error(": failed to enable clk_uart_baud0 clock\n"); 208 return; 209 } 210 211 const bool is_console = exynos_uart_consaddr == addr; 212 213 sc->sc_dev = self; 214 sc->sc_bst = faa->faa_bst; 215 sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data; 216 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH); 217 sc->sc_console = is_console; 218 if (is_console) { 219 sc->sc_bsh = exynos_uart_cnsc.sc_bsh; 220 } else { 221 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 222 aprint_error(": failed to map registers\n"); 223 return; 224 } 225 } 226 sc->sc_freq = clk_get_rate(clk_uart_baud0); 227 228 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, 229 0, exynos_uart_intr, sc, device_xname(self)); 230 if (sc->sc_ih == NULL) { 231 aprint_error(": failed to establish interrupt on %s\n", 232 intrstr); 233 return; 234 } 235 236 if (exynos_uart_cmajor == -1) { 237 /* allocate a major number */ 238 int bmajor = -1, cmajor = -1; 239 int error = devsw_attach("exuart", NULL, &bmajor, 240 &exuart_cdevsw, &cmajor); 241 if (error) { 242 aprint_error(": couldn't allocate major number\n"); 243 return; 244 } 245 exynos_uart_cmajor = cmajor; 246 } 247 248 major = cdevsw_lookup_major(&exuart_cdevsw); 249 minor = device_unit(self); 250 251 tp = sc->sc_tty = tty_alloc(); 252 tp->t_oproc = exynos_uart_start; 253 tp->t_param = exynos_uart_param; 254 tp->t_dev = makedev(major, minor); 255 tp->t_sc = sc; 256 tty_attach(tp); 257 258 aprint_naive("\n"); 259 if (is_console) { 260 cn_tab->cn_dev = tp->t_dev; 261 aprint_normal(": console"); 262 } 263 aprint_normal("\n"); 264 265 if (is_console) 266 delay(10000); 267 268 /* Initialize device */ 269 WR4(sc, SSCOM_UFCON, 270 __SHIFTIN(2, UFCON_TXTRIGGER) | 271 __SHIFTIN(1, UFCON_RXTRIGGER) | 272 UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET | 273 UFCON_FIFO_ENABLE); 274 275 /* Configure PIO mode with RX timeout interrupts */ 276 ucon = UCON_TOINT | UCON_ERRINT | 277 UCON_TXMODE_INT | UCON_RXMODE_INT; 278 WR4(sc, SSCOM_UCON, ucon); 279 280 switch (sc->sc_conf->type) { 281 case EXYNOS_UART_SAMSUNG: 282 WR4(sc, SSCOM_UCON, ucon | __SHIFTIN(3, UCON_RXTO)); 283 /* Disable interrupts */ 284 WR4(sc, SSCOM_UINTM, ~0u); 285 break; 286 case EXYNOS_UART_APPLE: 287 break; 288 } 289 290 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 291 } 292 293 static int 294 exynos_uart_cngetc(dev_t dev) 295 { 296 struct exynos_uart_softc * const sc = &exynos_uart_cnsc; 297 uint32_t ufstat; 298 int s, c; 299 300 s = splserial(); 301 302 ufstat = RD4(sc, SSCOM_UFSTAT); 303 if (__SHIFTOUT(ufstat, sc->sc_conf->rxcount) == 0) { 304 splx(s); 305 return -1; 306 } 307 308 c = RD4(sc, SSCOM_URXH); 309 310 cn_check_magic(dev, c & 0xff, &exynos_uart_cnm_state); 311 312 splx(s); 313 314 return c & 0xff; 315 } 316 317 static void 318 exynos_uart_cnputc(dev_t dev, int c) 319 { 320 struct exynos_uart_softc * const sc = &exynos_uart_cnsc; 321 int s; 322 323 s = splserial(); 324 while ((RD4(sc, SSCOM_UFSTAT) & sc->sc_conf->txfull) != 0) 325 ; 326 327 WR4(sc, SSCOM_UTXH, c & 0xff); 328 329 splx(s); 330 } 331 332 333 static void 334 exynos_uart_cnpollc(dev_t dev, int on) 335 { 336 } 337 338 static void 339 exynos_uart_cnattach(bus_space_tag_t bst, bus_space_handle_t bsh, 340 int ospeed, tcflag_t cflag, const struct exynos_uart_config *conf) 341 { 342 struct exynos_uart_softc *sc = &exynos_uart_cnsc; 343 344 cn_tab = &exynos_uart_consdev; 345 cn_init_magic(&exynos_uart_cnm_state); 346 cn_set_magic("\047\001"); 347 348 sc->sc_bst = bst; 349 sc->sc_bsh = bsh; 350 sc->sc_ospeed = ospeed; 351 sc->sc_cflag = cflag; 352 sc->sc_conf = conf; 353 } 354 355 static int 356 exynos_uart_open(dev_t dev, int flag, int mode, lwp_t *l) 357 { 358 struct exynos_uart_softc *sc = 359 device_lookup_private(&exuart_cd, minor(dev)); 360 struct tty *tp = sc->sc_tty; 361 uint32_t ucon; 362 363 if (kauth_authorize_device_tty(l->l_cred, 364 KAUTH_DEVICE_TTY_OPEN, tp) != 0) { 365 return EBUSY; 366 } 367 368 mutex_enter(&sc->sc_lock); 369 370 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) { 371 tp->t_dev = dev; 372 ttychars(tp); 373 tp->t_iflag = TTYDEF_IFLAG; 374 tp->t_oflag = TTYDEF_OFLAG; 375 tp->t_lflag = TTYDEF_LFLAG; 376 if (sc->sc_console) { 377 tp->t_ispeed = tp->t_ospeed = exynos_uart_cnsc.sc_ospeed; 378 tp->t_cflag = exynos_uart_cnsc.sc_cflag; 379 } else { 380 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 381 tp->t_cflag = TTYDEF_CFLAG; 382 } 383 ttsetwater(tp); 384 } 385 tp->t_state |= TS_CARR_ON; 386 387 /* Enable RX and error interrupts */ 388 switch (sc->sc_conf->type) { 389 case EXYNOS_UART_SAMSUNG: 390 WR4(sc, SSCOM_UINTM, ~0u & ~(UINT_RXD|UINT_ERROR)); 391 break; 392 case EXYNOS_UART_APPLE: 393 ucon = RD4(sc, SSCOM_UCON); 394 ucon |= UCON_S5L_RXTHRESH | UCON_S5L_RX_TIMEOUT; 395 WR4(sc, SSCOM_UCON, ucon); 396 break; 397 } 398 399 mutex_exit(&sc->sc_lock); 400 401 return tp->t_linesw->l_open(dev, tp); 402 } 403 404 static int 405 exynos_uart_close(dev_t dev, int flag, int mode, lwp_t *l) 406 { 407 struct exynos_uart_softc *sc = 408 device_lookup_private(&exuart_cd, minor(dev)); 409 struct tty *tp = sc->sc_tty; 410 uint32_t ucon; 411 412 mutex_enter(&sc->sc_lock); 413 414 tp->t_linesw->l_close(tp, flag); 415 ttyclose(tp); 416 417 /* Disable interrupts */ 418 switch (sc->sc_conf->type) { 419 case EXYNOS_UART_SAMSUNG: 420 WR4(sc, SSCOM_UINTM, ~0u); 421 break; 422 case EXYNOS_UART_APPLE: 423 ucon = RD4(sc, SSCOM_UCON); 424 ucon &= ~(UCON_S5L_RXTHRESH | UCON_S5L_RX_TIMEOUT); 425 WR4(sc, SSCOM_UCON, ucon); 426 break; 427 } 428 429 mutex_exit(&sc->sc_lock); 430 431 return 0; 432 } 433 434 static int 435 exynos_uart_read(dev_t dev, struct uio *uio, int flag) 436 { 437 struct exynos_uart_softc *sc = 438 device_lookup_private(&exuart_cd, minor(dev)); 439 struct tty *tp = sc->sc_tty; 440 441 return tp->t_linesw->l_read(tp, uio, flag); 442 } 443 444 static int 445 exynos_uart_write(dev_t dev, struct uio *uio, int flag) 446 { 447 struct exynos_uart_softc *sc = 448 device_lookup_private(&exuart_cd, minor(dev)); 449 struct tty *tp = sc->sc_tty; 450 451 return tp->t_linesw->l_write(tp, uio, flag); 452 } 453 454 static int 455 exynos_uart_poll(dev_t dev, int events, lwp_t *l) 456 { 457 struct exynos_uart_softc *sc = 458 device_lookup_private(&exuart_cd, minor(dev)); 459 struct tty *tp = sc->sc_tty; 460 461 return tp->t_linesw->l_poll(tp, events, l); 462 } 463 464 static int 465 exynos_uart_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l) 466 { 467 struct exynos_uart_softc *sc = 468 device_lookup_private(&exuart_cd, minor(dev)); 469 struct tty *tp = sc->sc_tty; 470 int error; 471 472 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l); 473 if (error != EPASSTHROUGH) 474 return error; 475 476 return ttioctl(tp, cmd, data, flag, l); 477 } 478 479 static struct tty * 480 exynos_uart_tty(dev_t dev) 481 { 482 struct exynos_uart_softc *sc = 483 device_lookup_private(&exuart_cd, minor(dev)); 484 485 return sc->sc_tty; 486 } 487 488 static void 489 exynos_uart_stop(struct tty *tp, int flag) 490 { 491 } 492 493 static void 494 exynos_uart_start(struct tty *tp) 495 { 496 struct exynos_uart_softc *sc = tp->t_sc; 497 u_char *p = sc->sc_buf; 498 int s, brem; 499 500 s = spltty(); 501 502 if (tp->t_state & (TS_TTSTOP | TS_BUSY | TS_TIMEOUT)) { 503 splx(s); 504 return; 505 } 506 tp->t_state |= TS_BUSY; 507 508 for (brem = q_to_b(&tp->t_outq, sc->sc_buf, sizeof(sc->sc_buf)); 509 brem > 0; 510 brem--, p++) { 511 while ((RD4(sc, SSCOM_UFSTAT) & sc->sc_conf->txfull) != 0) 512 ; 513 514 WR4(sc, SSCOM_UTXH, *p); 515 } 516 517 tp->t_state &= ~TS_BUSY; 518 if (ttypull(tp)) { 519 tp->t_state |= TS_TIMEOUT; 520 callout_schedule(&tp->t_rstrt_ch, 1); 521 } 522 splx(s); 523 } 524 525 static int 526 exynos_uart_param(struct tty *tp, struct termios *t) 527 { 528 struct exynos_uart_softc *sc = tp->t_sc; 529 530 mutex_enter(&sc->sc_lock); 531 532 if (tp->t_cflag != t->c_cflag) { 533 uint32_t ulcon = 0; 534 switch (ISSET(t->c_cflag, CSIZE)) { 535 case CS5: 536 ulcon |= ULCON_LENGTH_5; 537 break; 538 case CS6: 539 ulcon |= ULCON_LENGTH_6; 540 break; 541 case CS7: 542 ulcon |= ULCON_LENGTH_7; 543 break; 544 case CS8: 545 ulcon |= ULCON_LENGTH_8; 546 break; 547 } 548 switch (ISSET(t->c_cflag, PARENB|PARODD)) { 549 case PARENB|PARODD: 550 ulcon |= ULCON_PARITY_ODD; 551 break; 552 case PARENB: 553 ulcon |= ULCON_PARITY_EVEN; 554 break; 555 default: 556 ulcon |= ULCON_PARITY_NONE; 557 break; 558 } 559 if (ISSET(t->c_cflag, CSTOPB)) 560 ulcon |= ULCON_STOP; 561 WR4(sc, SSCOM_ULCON, ulcon); 562 } 563 564 if (tp->t_ospeed != t->c_ospeed) { 565 const uint32_t ubrdiv = (sc->sc_freq / 16) / t->c_ospeed - 1; 566 WR4(sc, SSCOM_UBRDIV, ubrdiv); 567 } 568 569 tp->t_ispeed = t->c_ispeed; 570 tp->t_ospeed = t->c_ospeed; 571 tp->t_cflag = t->c_cflag; 572 573 mutex_exit(&sc->sc_lock); 574 575 return 0; 576 } 577 578 static int 579 exynos_uart_intr(void *priv) 580 { 581 struct exynos_uart_softc *sc = priv; 582 struct tty *tp = sc->sc_tty; 583 uint32_t ack, uerstat, ufstat, c; 584 585 mutex_enter(&sc->sc_lock); 586 587 if (sc->sc_conf->type == EXYNOS_UART_APPLE) { 588 ack = RD4(sc, SSCOM_UTRSTAT); 589 } else { 590 ack = RD4(sc, SSCOM_UINTP); 591 } 592 593 for (;;) { 594 uerstat = RD4(sc, SSCOM_UERSTAT); 595 if (uerstat & UERSTAT_BREAK) { 596 if (cn_check_magic(tp->t_dev, CNC_BREAK, 597 &exynos_uart_cnm_state)) { 598 continue; 599 } 600 } 601 602 ufstat = RD4(sc, SSCOM_UFSTAT); 603 if (__SHIFTOUT(ufstat, sc->sc_conf->rxcount) == 0) { 604 break; 605 } 606 607 c = RD4(sc, SSCOM_URXH); 608 if (cn_check_magic(tp->t_dev, c & 0xff, 609 &exynos_uart_cnm_state)) { 610 continue; 611 } 612 tp->t_linesw->l_rint(c & 0xff, tp); 613 } 614 615 if (sc->sc_conf->type == EXYNOS_UART_APPLE) { 616 WR4(sc, SSCOM_UTRSTAT, ack); 617 } else { 618 WR4(sc, SSCOM_UINTP, ack); 619 } 620 621 mutex_exit(&sc->sc_lock); 622 623 return 1; 624 } 625 626 /* 627 * Console support 628 */ 629 630 static int 631 exynos_uart_console_match(int phandle) 632 { 633 return of_compatible_match(phandle, compat_data); 634 } 635 636 static void 637 exynos_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 638 { 639 const int phandle = faa->faa_phandle; 640 bus_space_tag_t bst = faa->faa_bst; 641 bus_space_handle_t bsh; 642 bus_addr_t addr; 643 bus_size_t size; 644 tcflag_t flags; 645 int speed; 646 const struct exynos_uart_config *conf; 647 648 speed = fdtbus_get_stdout_speed(); 649 if (speed < 0) 650 speed = 115200; /* default */ 651 flags = fdtbus_get_stdout_flags(); 652 conf = of_compatible_lookup(phandle, compat_data)->data; 653 654 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) 655 panic("exynos_uart: couldn't get registers"); 656 if (bus_space_map(bst, addr, size, 0, &bsh) != 0) 657 panic("exynos_uart: couldn't map registers"); 658 659 exynos_uart_consaddr = addr; 660 661 exynos_uart_cnattach(bst, bsh, speed, flags, conf); 662 } 663 664 static const struct fdt_console exynos_uart_console = { 665 .match = exynos_uart_console_match, 666 .consinit = exynos_uart_console_consinit, 667 }; 668 669 FDT_CONSOLE(exynos_uart, &exynos_uart_console); 670