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