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 } else { 112 if (!serial_check_stdout(blob, &dev)) { 113 gd->cur_serial_dev = dev; 114 return; 115 } 116 } 117 } 118 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) { 119 /* 120 * Try to use CONFIG_CONS_INDEX if available (it is numbered 121 * from 1!). 122 * 123 * Failing that, get the device with sequence number 0, or in 124 * extremis just the first serial device we can find. But we 125 * insist on having a console (even if it is silent). 126 */ 127 #ifdef CONFIG_CONS_INDEX 128 #define INDEX (CONFIG_CONS_INDEX - 1) 129 #else 130 #define INDEX 0 131 #endif 132 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || 133 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || 134 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { 135 gd->cur_serial_dev = dev; 136 return; 137 } 138 #undef INDEX 139 } 140 141 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE 142 panic_str("No serial driver found"); 143 #endif 144 } 145 146 /* Called prior to relocation */ 147 int serial_init(void) 148 { 149 serial_find_console_or_panic(); 150 gd->flags |= GD_FLG_SERIAL_READY; 151 152 return 0; 153 } 154 155 /* Called after relocation */ 156 void serial_initialize(void) 157 { 158 serial_init(); 159 } 160 161 static void _serial_putc(struct udevice *dev, char ch) 162 { 163 struct dm_serial_ops *ops = serial_get_ops(dev); 164 int err; 165 166 if (ch == '\n') 167 _serial_putc(dev, '\r'); 168 169 do { 170 err = ops->putc(dev, ch); 171 } while (err == -EAGAIN); 172 } 173 174 static void _serial_puts(struct udevice *dev, const char *str) 175 { 176 while (*str) 177 _serial_putc(dev, *str++); 178 } 179 180 static int __serial_getc(struct udevice *dev) 181 { 182 struct dm_serial_ops *ops = serial_get_ops(dev); 183 int err; 184 185 do { 186 err = ops->getc(dev); 187 if (err == -EAGAIN) 188 WATCHDOG_RESET(); 189 } while (err == -EAGAIN); 190 191 return err >= 0 ? err : 0; 192 } 193 194 static int __serial_tstc(struct udevice *dev) 195 { 196 struct dm_serial_ops *ops = serial_get_ops(dev); 197 198 if (ops->pending) 199 return ops->pending(dev, true); 200 201 return 1; 202 } 203 204 static void __serial_clear(struct udevice *dev) 205 { 206 struct dm_serial_ops *ops = serial_get_ops(dev); 207 208 if (ops->clear) 209 ops->clear(dev); 210 } 211 212 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 213 static int _serial_tstc(struct udevice *dev) 214 { 215 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 216 217 /* Read all available chars into the RX buffer */ 218 while (__serial_tstc(dev)) { 219 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev); 220 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 221 } 222 223 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0; 224 } 225 226 static int _serial_getc(struct udevice *dev) 227 { 228 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 229 char val; 230 231 val = upriv->buf[upriv->rd_ptr++]; 232 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 233 234 return val; 235 } 236 237 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 238 239 static int _serial_getc(struct udevice *dev) 240 { 241 return __serial_getc(dev); 242 } 243 244 static int _serial_tstc(struct udevice *dev) 245 { 246 return __serial_tstc(dev); 247 } 248 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 249 250 void serial_putc(char ch) 251 { 252 if (gd->cur_serial_dev) 253 _serial_putc(gd->cur_serial_dev, ch); 254 } 255 256 void serial_puts(const char *str) 257 { 258 if (gd->cur_serial_dev) 259 _serial_puts(gd->cur_serial_dev, str); 260 } 261 262 int serial_getc(void) 263 { 264 if (!gd->cur_serial_dev) 265 return 0; 266 267 return _serial_getc(gd->cur_serial_dev); 268 } 269 270 int serial_tstc(void) 271 { 272 if (!gd->cur_serial_dev) 273 return 0; 274 275 return _serial_tstc(gd->cur_serial_dev); 276 } 277 278 void serial_setbrg(void) 279 { 280 struct dm_serial_ops *ops; 281 282 if (!gd->cur_serial_dev) 283 return; 284 285 ops = serial_get_ops(gd->cur_serial_dev); 286 if (ops->setbrg) 287 ops->setbrg(gd->cur_serial_dev, gd->baudrate); 288 } 289 290 void serial_clear(void) 291 { 292 if (!gd->cur_serial_dev) 293 return; 294 295 __serial_clear(gd->cur_serial_dev); 296 } 297 298 void serial_stdio_init(void) 299 { 300 } 301 302 #if defined(CONFIG_DM_STDIO) 303 304 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 305 static void serial_stub_putc(struct stdio_dev *sdev, const char ch) 306 { 307 _serial_putc(sdev->priv, ch); 308 } 309 #endif 310 311 static void serial_stub_puts(struct stdio_dev *sdev, const char *str) 312 { 313 _serial_puts(sdev->priv, str); 314 } 315 316 static int serial_stub_getc(struct stdio_dev *sdev) 317 { 318 return _serial_getc(sdev->priv); 319 } 320 321 static int serial_stub_tstc(struct stdio_dev *sdev) 322 { 323 return _serial_tstc(sdev->priv); 324 } 325 #endif 326 327 /** 328 * on_baudrate() - Update the actual baudrate when the env var changes 329 * 330 * This will check for a valid baudrate and only apply it if valid. 331 */ 332 static int on_baudrate(const char *name, const char *value, enum env_op op, 333 int flags) 334 { 335 int i; 336 int baudrate; 337 338 switch (op) { 339 case env_op_create: 340 case env_op_overwrite: 341 /* 342 * Switch to new baudrate if new baudrate is supported 343 */ 344 baudrate = simple_strtoul(value, NULL, 10); 345 346 /* Not actually changing */ 347 if (gd->baudrate == baudrate) 348 return 0; 349 350 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 351 if (baudrate == baudrate_table[i]) 352 break; 353 } 354 if (i == ARRAY_SIZE(baudrate_table)) { 355 if ((flags & H_FORCE) == 0) 356 printf("## Baudrate %d bps not supported\n", 357 baudrate); 358 return 1; 359 } 360 if ((flags & H_INTERACTIVE) != 0) { 361 printf("## Switch baudrate to %d bps and press ENTER ...\n", 362 baudrate); 363 udelay(50000); 364 } 365 366 gd->baudrate = baudrate; 367 368 serial_setbrg(); 369 370 udelay(50000); 371 372 if ((flags & H_INTERACTIVE) != 0) 373 while (1) { 374 if (getc() == '\r') 375 break; 376 } 377 378 return 0; 379 case env_op_delete: 380 printf("## Baudrate may not be deleted\n"); 381 return 1; 382 default: 383 return 0; 384 } 385 } 386 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); 387 388 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 389 static int serial_post_probe(struct udevice *dev) 390 { 391 struct dm_serial_ops *ops = serial_get_ops(dev); 392 #ifdef CONFIG_DM_STDIO 393 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 394 struct stdio_dev sdev; 395 #endif 396 int ret; 397 398 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 399 if (ops->setbrg) 400 ops->setbrg += gd->reloc_off; 401 if (ops->getc) 402 ops->getc += gd->reloc_off; 403 if (ops->putc) 404 ops->putc += gd->reloc_off; 405 if (ops->pending) 406 ops->pending += gd->reloc_off; 407 if (ops->clear) 408 ops->clear += gd->reloc_off; 409 #if CONFIG_POST & CONFIG_SYS_POST_UART 410 if (ops->loop) 411 ops->loop += gd->reloc_off 412 #endif 413 #endif 414 /* Set the baud rate */ 415 if (ops->setbrg) { 416 ret = ops->setbrg(dev, gd->baudrate); 417 if (ret) 418 return ret; 419 } 420 421 #ifdef CONFIG_DM_STDIO 422 if (!(gd->flags & GD_FLG_RELOC)) 423 return 0; 424 memset(&sdev, '\0', sizeof(sdev)); 425 426 strncpy(sdev.name, dev->name, sizeof(sdev.name)); 427 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM; 428 sdev.priv = dev; 429 sdev.putc = serial_stub_putc; 430 sdev.puts = serial_stub_puts; 431 sdev.getc = serial_stub_getc; 432 sdev.tstc = serial_stub_tstc; 433 434 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 435 /* Allocate the RX buffer */ 436 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE); 437 #endif 438 439 stdio_register_dev(&sdev, &upriv->sdev); 440 #endif 441 return 0; 442 } 443 444 static int serial_pre_remove(struct udevice *dev) 445 { 446 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) 447 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 448 449 if (stdio_deregister_dev(upriv->sdev, true)) 450 return -EPERM; 451 #endif 452 453 return 0; 454 } 455 456 UCLASS_DRIVER(serial) = { 457 .id = UCLASS_SERIAL, 458 .name = "serial", 459 .flags = DM_UC_FLAG_SEQ_ALIAS, 460 .post_probe = serial_post_probe, 461 .pre_remove = serial_pre_remove, 462 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), 463 }; 464 #endif 465