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