1 /* 2 * Copyright (c) 2014 The Chromium OS Authors. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <dm.h> 9 #include <environment.h> 10 #include <errno.h> 11 #include <os.h> 12 #include <serial.h> 13 #include <stdio_dev.h> 14 #include <watchdog.h> 15 #include <dm/lists.h> 16 #include <dm/device-internal.h> 17 #include <dm/of_access.h> 18 #include <dm/uclass-internal.h> 19 20 DECLARE_GLOBAL_DATA_PTR; 21 22 /* 23 * Table with supported baudrates (defined in config_xyz.h) 24 */ 25 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; 26 27 #if !CONFIG_VAL(SYS_MALLOC_F_LEN) 28 #error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work" 29 #endif 30 31 #ifndef CONFIG_CONSOLE_SERIAL_SKIP_INIT 32 static int serial_check_stdout(const void *blob, struct udevice **devp) 33 { 34 int node; 35 36 /* Check for a chosen console */ 37 node = fdtdec_get_chosen_node(blob, "stdout-path"); 38 if (node < 0) { 39 const char *str, *p, *name; 40 41 /* 42 * Deal with things like 43 * stdout-path = "serial0:115200n8"; 44 * 45 * We need to look up the alias and then follow it to the 46 * correct node. 47 */ 48 str = fdtdec_get_chosen_prop(blob, "stdout-path"); 49 if (str) { 50 p = strchr(str, ':'); 51 name = fdt_get_alias_namelen(blob, str, 52 p ? p - str : strlen(str)); 53 if (name) 54 node = fdt_path_offset(blob, name); 55 } 56 } 57 if (node < 0) 58 node = fdt_path_offset(blob, "console"); 59 60 if (gd && gd->serial.using_pre_serial) { 61 const char *serial_path; 62 char serial[12]; 63 64 snprintf(serial, 12, "serial%d", gd->serial.id); 65 serial_path = fdt_get_alias(blob, serial); 66 if (serial_path) { 67 debug("Find alias %s, path: %s\n", serial, serial_path); 68 node = fdt_path_offset(blob, serial_path); 69 if (node < 0) 70 printf("Can't find %s by path\n", serial); 71 } else { 72 printf("Can't find alias %s\n", serial); 73 } 74 } 75 76 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp)) 77 return 0; 78 79 /* 80 * If the console is not marked to be bound before relocation, bind it 81 * anyway. 82 */ 83 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), 84 devp)) { 85 if (!device_probe(*devp)) 86 return 0; 87 } 88 89 return -ENODEV; 90 } 91 #endif 92 93 #if defined(CONFIG_OF_LIVE) && !defined(CONFIG_CONSOLE_SERIAL_SKIP_INIT) 94 /* 95 * Hide and present pinctrl prop int live device tree 96 * 97 * 1. We bind all serial nodes including UART debug node from kernel dtb. 98 * 99 * 2. On some rockchip platforms, UART debug and SDMMC pin are multiplex. 100 * Without this, iomux is switched from SDMMC => UART debug at this time. 101 * 102 * 3. We may switch to UART debug iomux after SDMMC boot failed to print log 103 * by console record mechanism. 104 */ 105 static void serial_console_hide_prop(char **p1, char **p2) 106 { 107 struct udevice *dev; 108 109 if (!of_live_active()) 110 return; 111 112 for (uclass_find_first_device(UCLASS_SERIAL, &dev); 113 dev; 114 uclass_find_next_device(&dev)) { 115 if (dev_read_bool(dev, "u-boot,dm-pre-reloc") || 116 dev_read_bool(dev, "u-boot,dm-spl")) 117 continue; 118 119 if (gd->cur_serial_dev->req_seq == dev->req_seq) { 120 *p1 = (char *)dev_hide_prop(dev, "pinctrl-names"); 121 *p2 = (char *)dev_hide_prop(dev, "pinctrl-0"); 122 } 123 } 124 } 125 126 static void serial_console_present_prop(char *p1, char *p2) 127 { 128 struct udevice *dev; 129 130 if (!of_live_active() || !p1 || !p2) 131 return; 132 133 for (uclass_find_first_device(UCLASS_SERIAL, &dev); 134 dev; 135 uclass_find_next_device(&dev)) { 136 if (dev_read_bool(dev, "u-boot,dm-pre-reloc") || 137 dev_read_bool(dev, "u-boot,dm-spl")) 138 continue; 139 140 if (gd->cur_serial_dev->req_seq == dev->req_seq) { 141 dev_present_prop(dev, p1); 142 dev_present_prop(dev, p2); 143 } 144 } 145 } 146 #else 147 static inline void serial_console_hide_prop(char **p1, char **p2) {} 148 static inline void serial_console_present_prop(char *p1, char *p2) {} 149 #endif 150 151 #ifndef CONFIG_CONSOLE_SERIAL_SKIP_INIT 152 static void serial_find_console_or_panic(void) 153 { 154 const void *blob = gd->fdt_blob; 155 struct udevice *dev; 156 char *p1 = NULL, *p2 = NULL; 157 158 if (CONFIG_IS_ENABLED(OF_PLATDATA)) { 159 uclass_first_device(UCLASS_SERIAL, &dev); 160 if (dev) { 161 gd->cur_serial_dev = dev; 162 return; 163 } 164 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) { 165 /* Live tree has support for stdout */ 166 if (of_live_active()) { 167 struct device_node *np = of_get_stdout(); 168 169 serial_console_hide_prop(&p1, &p2); 170 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL, 171 np_to_ofnode(np), &dev)) { 172 serial_console_present_prop(p1, p2); 173 gd->cur_serial_dev = dev; 174 return; 175 } 176 177 /* 178 * If the console is not marked to be bound, bind it 179 * anyway. 180 */ 181 if (!lists_bind_fdt(gd->dm_root, np_to_ofnode(np), 182 &dev)) { 183 serial_console_hide_prop(&p1, &p2); 184 if (!device_probe(dev)) { 185 serial_console_present_prop(p1, p2); 186 gd->cur_serial_dev = dev; 187 return; 188 } 189 } 190 } else { 191 if (!serial_check_stdout(blob, &dev)) { 192 gd->cur_serial_dev = dev; 193 return; 194 } 195 } 196 } 197 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) { 198 /* 199 * Try to use CONFIG_CONS_INDEX if available (it is numbered 200 * from 1!). 201 * 202 * Failing that, get the device with sequence number 0, or in 203 * extremis just the first serial device we can find. But we 204 * insist on having a console (even if it is silent). 205 */ 206 #ifdef CONFIG_CONS_INDEX 207 #define INDEX (CONFIG_CONS_INDEX - 1) 208 #else 209 #define INDEX 0 210 #endif 211 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || 212 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || 213 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { 214 gd->cur_serial_dev = dev; 215 return; 216 } 217 #undef INDEX 218 } 219 220 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE 221 panic_str("No serial driver found"); 222 #endif 223 } 224 225 /* Called prior to relocation */ 226 int serial_init(void) 227 { 228 serial_find_console_or_panic(); 229 gd->flags |= GD_FLG_SERIAL_READY; 230 231 return 0; 232 } 233 #else 234 int serial_init(void) { return 0; } 235 #endif 236 237 /* Called after relocation */ 238 void serial_initialize(void) 239 { 240 serial_init(); 241 } 242 243 static void _serial_putc(struct udevice *dev, char ch) 244 { 245 struct dm_serial_ops *ops = serial_get_ops(dev); 246 int err; 247 248 if (ch == '\n') 249 _serial_putc(dev, '\r'); 250 251 do { 252 err = ops->putc(dev, ch); 253 } while (err == -EAGAIN); 254 } 255 256 static void _serial_puts(struct udevice *dev, const char *str) 257 { 258 while (*str) 259 _serial_putc(dev, *str++); 260 } 261 262 static int __serial_getc(struct udevice *dev) 263 { 264 struct dm_serial_ops *ops = serial_get_ops(dev); 265 int err; 266 267 do { 268 err = ops->getc(dev); 269 if (err == -EAGAIN) 270 WATCHDOG_RESET(); 271 } while (err == -EAGAIN); 272 273 return err >= 0 ? err : 0; 274 } 275 276 static int __serial_tstc(struct udevice *dev) 277 { 278 struct dm_serial_ops *ops = serial_get_ops(dev); 279 280 if (ops->pending) 281 return ops->pending(dev, true); 282 283 return 1; 284 } 285 286 static void __serial_clear(struct udevice *dev) 287 { 288 struct dm_serial_ops *ops = serial_get_ops(dev); 289 290 if (ops->clear) 291 ops->clear(dev); 292 } 293 294 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 295 static int _serial_tstc(struct udevice *dev) 296 { 297 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 298 299 /* Read all available chars into the RX buffer */ 300 while (__serial_tstc(dev)) { 301 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev); 302 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 303 } 304 305 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0; 306 } 307 308 static int _serial_getc(struct udevice *dev) 309 { 310 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 311 char val; 312 313 val = upriv->buf[upriv->rd_ptr++]; 314 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 315 316 return val; 317 } 318 319 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 320 321 static int _serial_getc(struct udevice *dev) 322 { 323 return __serial_getc(dev); 324 } 325 326 static int _serial_tstc(struct udevice *dev) 327 { 328 return __serial_tstc(dev); 329 } 330 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 331 332 void serial_putc(char ch) 333 { 334 if (gd->cur_serial_dev) 335 _serial_putc(gd->cur_serial_dev, ch); 336 } 337 338 void serial_puts(const char *str) 339 { 340 if (gd->cur_serial_dev) 341 _serial_puts(gd->cur_serial_dev, str); 342 } 343 344 int serial_getc(void) 345 { 346 if (!gd->cur_serial_dev) 347 return 0; 348 349 return _serial_getc(gd->cur_serial_dev); 350 } 351 352 int serial_tstc(void) 353 { 354 if (!gd->cur_serial_dev) 355 return 0; 356 357 return _serial_tstc(gd->cur_serial_dev); 358 } 359 360 void serial_setbrg(void) 361 { 362 struct dm_serial_ops *ops; 363 364 if (!gd->cur_serial_dev) 365 return; 366 367 ops = serial_get_ops(gd->cur_serial_dev); 368 if (ops->setbrg) 369 ops->setbrg(gd->cur_serial_dev, gd->baudrate); 370 } 371 372 void serial_clear(void) 373 { 374 if (!gd->cur_serial_dev) 375 return; 376 377 __serial_clear(gd->cur_serial_dev); 378 } 379 380 void serial_dev_putc(struct udevice *dev, char ch) 381 { 382 if (!dev) 383 return; 384 385 _serial_putc(dev, ch); 386 } 387 388 void serial_dev_puts(struct udevice *dev, const char *str) 389 { 390 if (!dev) 391 return; 392 393 _serial_puts(dev, str); 394 } 395 396 int serial_dev_getc(struct udevice *dev) 397 { 398 if (!dev) 399 return 0; 400 401 return _serial_getc(dev); 402 } 403 404 int serial_dev_tstc(struct udevice *dev) 405 { 406 if (!dev) 407 return 0; 408 409 return _serial_tstc(dev); 410 } 411 412 void serial_dev_setbrg(struct udevice *dev, int baudrate) 413 { 414 struct dm_serial_ops *ops; 415 416 if (!dev) 417 return; 418 419 ops = serial_get_ops(dev); 420 if (ops->setbrg) 421 ops->setbrg(dev, baudrate); 422 } 423 424 void serial_dev_clear(struct udevice *dev) 425 { 426 if (!dev) 427 return; 428 429 __serial_clear(dev); 430 } 431 432 void serial_stdio_init(void) 433 { 434 } 435 436 #if defined(CONFIG_DM_STDIO) 437 438 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 439 static void serial_stub_putc(struct stdio_dev *sdev, const char ch) 440 { 441 _serial_putc(sdev->priv, ch); 442 } 443 #endif 444 445 static void serial_stub_puts(struct stdio_dev *sdev, const char *str) 446 { 447 _serial_puts(sdev->priv, str); 448 } 449 450 static int serial_stub_getc(struct stdio_dev *sdev) 451 { 452 return _serial_getc(sdev->priv); 453 } 454 455 static int serial_stub_tstc(struct stdio_dev *sdev) 456 { 457 return _serial_tstc(sdev->priv); 458 } 459 #endif 460 461 /** 462 * on_baudrate() - Update the actual baudrate when the env var changes 463 * 464 * This will check for a valid baudrate and only apply it if valid. 465 */ 466 static int on_baudrate(const char *name, const char *value, enum env_op op, 467 int flags) 468 { 469 int i; 470 int baudrate; 471 472 switch (op) { 473 case env_op_create: 474 case env_op_overwrite: 475 /* 476 * Switch to new baudrate if new baudrate is supported 477 */ 478 baudrate = simple_strtoul(value, NULL, 10); 479 480 /* Not actually changing */ 481 if (gd->baudrate == baudrate) 482 return 0; 483 484 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 485 if (baudrate == baudrate_table[i]) 486 break; 487 } 488 if (i == ARRAY_SIZE(baudrate_table)) { 489 if ((flags & H_FORCE) == 0) 490 printf("## Baudrate %d bps not supported\n", 491 baudrate); 492 return 1; 493 } 494 if ((flags & H_INTERACTIVE) != 0) { 495 printf("## Switch baudrate to %d bps and press ENTER ...\n", 496 baudrate); 497 udelay(50000); 498 } 499 500 gd->baudrate = baudrate; 501 502 serial_setbrg(); 503 504 udelay(50000); 505 506 if ((flags & H_INTERACTIVE) != 0) 507 while (1) { 508 if (getc() == '\r') 509 break; 510 } 511 512 return 0; 513 case env_op_delete: 514 printf("## Baudrate may not be deleted\n"); 515 return 1; 516 default: 517 return 0; 518 } 519 } 520 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); 521 522 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 523 static int serial_post_probe(struct udevice *dev) 524 { 525 struct dm_serial_ops *ops = serial_get_ops(dev); 526 #ifdef CONFIG_DM_STDIO 527 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 528 struct stdio_dev sdev; 529 #endif 530 int ret; 531 532 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 533 if (ops->setbrg) 534 ops->setbrg += gd->reloc_off; 535 if (ops->getc) 536 ops->getc += gd->reloc_off; 537 if (ops->putc) 538 ops->putc += gd->reloc_off; 539 if (ops->pending) 540 ops->pending += gd->reloc_off; 541 if (ops->clear) 542 ops->clear += gd->reloc_off; 543 #if CONFIG_POST & CONFIG_SYS_POST_UART 544 if (ops->loop) 545 ops->loop += gd->reloc_off 546 #endif 547 #endif 548 /* Set the baud rate */ 549 if (ops->setbrg) { 550 ret = ops->setbrg(dev, gd->baudrate); 551 if (ret) 552 return ret; 553 } 554 555 #ifdef CONFIG_DM_STDIO 556 if (!(gd->flags & GD_FLG_RELOC)) 557 return 0; 558 memset(&sdev, '\0', sizeof(sdev)); 559 560 strncpy(sdev.name, dev->name, sizeof(sdev.name)); 561 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM; 562 sdev.priv = dev; 563 sdev.putc = serial_stub_putc; 564 sdev.puts = serial_stub_puts; 565 sdev.getc = serial_stub_getc; 566 sdev.tstc = serial_stub_tstc; 567 568 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 569 /* Allocate the RX buffer */ 570 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE); 571 #endif 572 573 stdio_register_dev(&sdev, &upriv->sdev); 574 #endif 575 return 0; 576 } 577 578 static int serial_pre_remove(struct udevice *dev) 579 { 580 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) 581 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 582 583 if (stdio_deregister_dev(upriv->sdev, true)) 584 return -EPERM; 585 #endif 586 587 return 0; 588 } 589 590 UCLASS_DRIVER(serial) = { 591 .id = UCLASS_SERIAL, 592 .name = "serial", 593 .flags = DM_UC_FLAG_SEQ_ALIAS, 594 .post_probe = serial_post_probe, 595 .pre_remove = serial_pre_remove, 596 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), 597 }; 598 #endif 599