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