serial-uclass.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright (c) 2014 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <environment.h>
  9. #include <errno.h>
  10. #include <os.h>
  11. #include <serial.h>
  12. #include <stdio_dev.h>
  13. #include <watchdog.h>
  14. #include <dm/lists.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/of_access.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /*
  19. * Table with supported baudrates (defined in config_xyz.h)
  20. */
  21. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  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. static int serial_check_stdout(const void *blob, struct udevice **devp)
  26. {
  27. int node;
  28. /* Check for a chosen console */
  29. node = fdtdec_get_chosen_node(blob, "stdout-path");
  30. if (node < 0) {
  31. const char *str, *p, *name;
  32. /*
  33. * Deal with things like
  34. * stdout-path = "serial0:115200n8";
  35. *
  36. * We need to look up the alias and then follow it to the
  37. * correct node.
  38. */
  39. str = fdtdec_get_chosen_prop(blob, "stdout-path");
  40. if (str) {
  41. p = strchr(str, ':');
  42. name = fdt_get_alias_namelen(blob, str,
  43. p ? p - str : strlen(str));
  44. if (name)
  45. node = fdt_path_offset(blob, name);
  46. }
  47. }
  48. if (node < 0)
  49. node = fdt_path_offset(blob, "console");
  50. if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
  51. return 0;
  52. /*
  53. * If the console is not marked to be bound before relocation, bind it
  54. * anyway.
  55. */
  56. if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
  57. devp)) {
  58. if (!device_probe(*devp))
  59. return 0;
  60. }
  61. return -ENODEV;
  62. }
  63. static void serial_find_console_or_panic(void)
  64. {
  65. const void *blob = gd->fdt_blob;
  66. struct udevice *dev;
  67. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  68. uclass_first_device(UCLASS_SERIAL, &dev);
  69. if (dev) {
  70. gd->cur_serial_dev = dev;
  71. return;
  72. }
  73. } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
  74. /* Live tree has support for stdout */
  75. if (of_live_active()) {
  76. struct device_node *np = of_get_stdout();
  77. if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
  78. np_to_ofnode(np), &dev)) {
  79. gd->cur_serial_dev = dev;
  80. return;
  81. }
  82. } else {
  83. if (!serial_check_stdout(blob, &dev)) {
  84. gd->cur_serial_dev = dev;
  85. return;
  86. }
  87. }
  88. }
  89. if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
  90. /*
  91. * Try to use CONFIG_CONS_INDEX if available (it is numbered
  92. * from 1!).
  93. *
  94. * Failing that, get the device with sequence number 0, or in
  95. * extremis just the first serial device we can find. But we
  96. * insist on having a console (even if it is silent).
  97. */
  98. #ifdef CONFIG_CONS_INDEX
  99. #define INDEX (CONFIG_CONS_INDEX - 1)
  100. #else
  101. #define INDEX 0
  102. #endif
  103. if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
  104. !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
  105. (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
  106. gd->cur_serial_dev = dev;
  107. return;
  108. }
  109. #undef INDEX
  110. }
  111. #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
  112. panic_str("No serial driver found");
  113. #endif
  114. }
  115. /* Called prior to relocation */
  116. int serial_init(void)
  117. {
  118. serial_find_console_or_panic();
  119. gd->flags |= GD_FLG_SERIAL_READY;
  120. return 0;
  121. }
  122. /* Called after relocation */
  123. void serial_initialize(void)
  124. {
  125. serial_init();
  126. }
  127. static void _serial_putc(struct udevice *dev, char ch)
  128. {
  129. struct dm_serial_ops *ops = serial_get_ops(dev);
  130. int err;
  131. if (ch == '\n')
  132. _serial_putc(dev, '\r');
  133. do {
  134. err = ops->putc(dev, ch);
  135. } while (err == -EAGAIN);
  136. }
  137. static void _serial_puts(struct udevice *dev, const char *str)
  138. {
  139. while (*str)
  140. _serial_putc(dev, *str++);
  141. }
  142. static int _serial_getc(struct udevice *dev)
  143. {
  144. struct dm_serial_ops *ops = serial_get_ops(dev);
  145. int err;
  146. do {
  147. err = ops->getc(dev);
  148. if (err == -EAGAIN)
  149. WATCHDOG_RESET();
  150. } while (err == -EAGAIN);
  151. return err >= 0 ? err : 0;
  152. }
  153. static int _serial_tstc(struct udevice *dev)
  154. {
  155. struct dm_serial_ops *ops = serial_get_ops(dev);
  156. if (ops->pending)
  157. return ops->pending(dev, true);
  158. return 1;
  159. }
  160. void serial_putc(char ch)
  161. {
  162. if (gd->cur_serial_dev)
  163. _serial_putc(gd->cur_serial_dev, ch);
  164. }
  165. void serial_puts(const char *str)
  166. {
  167. if (gd->cur_serial_dev)
  168. _serial_puts(gd->cur_serial_dev, str);
  169. }
  170. int serial_getc(void)
  171. {
  172. if (!gd->cur_serial_dev)
  173. return 0;
  174. return _serial_getc(gd->cur_serial_dev);
  175. }
  176. int serial_tstc(void)
  177. {
  178. if (!gd->cur_serial_dev)
  179. return 0;
  180. return _serial_tstc(gd->cur_serial_dev);
  181. }
  182. void serial_setbrg(void)
  183. {
  184. struct dm_serial_ops *ops;
  185. if (!gd->cur_serial_dev)
  186. return;
  187. ops = serial_get_ops(gd->cur_serial_dev);
  188. if (ops->setbrg)
  189. ops->setbrg(gd->cur_serial_dev, gd->baudrate);
  190. }
  191. void serial_stdio_init(void)
  192. {
  193. }
  194. #if defined(CONFIG_DM_STDIO)
  195. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  196. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  197. {
  198. _serial_putc(sdev->priv, ch);
  199. }
  200. #endif
  201. static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  202. {
  203. _serial_puts(sdev->priv, str);
  204. }
  205. static int serial_stub_getc(struct stdio_dev *sdev)
  206. {
  207. return _serial_getc(sdev->priv);
  208. }
  209. static int serial_stub_tstc(struct stdio_dev *sdev)
  210. {
  211. return _serial_tstc(sdev->priv);
  212. }
  213. #endif
  214. /**
  215. * on_baudrate() - Update the actual baudrate when the env var changes
  216. *
  217. * This will check for a valid baudrate and only apply it if valid.
  218. */
  219. static int on_baudrate(const char *name, const char *value, enum env_op op,
  220. int flags)
  221. {
  222. int i;
  223. int baudrate;
  224. switch (op) {
  225. case env_op_create:
  226. case env_op_overwrite:
  227. /*
  228. * Switch to new baudrate if new baudrate is supported
  229. */
  230. baudrate = simple_strtoul(value, NULL, 10);
  231. /* Not actually changing */
  232. if (gd->baudrate == baudrate)
  233. return 0;
  234. for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
  235. if (baudrate == baudrate_table[i])
  236. break;
  237. }
  238. if (i == ARRAY_SIZE(baudrate_table)) {
  239. if ((flags & H_FORCE) == 0)
  240. printf("## Baudrate %d bps not supported\n",
  241. baudrate);
  242. return 1;
  243. }
  244. if ((flags & H_INTERACTIVE) != 0) {
  245. printf("## Switch baudrate to %d bps and press ENTER ...\n",
  246. baudrate);
  247. udelay(50000);
  248. }
  249. gd->baudrate = baudrate;
  250. serial_setbrg();
  251. udelay(50000);
  252. if ((flags & H_INTERACTIVE) != 0)
  253. while (1) {
  254. if (getc() == '\r')
  255. break;
  256. }
  257. return 0;
  258. case env_op_delete:
  259. printf("## Baudrate may not be deleted\n");
  260. return 1;
  261. default:
  262. return 0;
  263. }
  264. }
  265. U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  266. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  267. static int serial_post_probe(struct udevice *dev)
  268. {
  269. struct dm_serial_ops *ops = serial_get_ops(dev);
  270. #ifdef CONFIG_DM_STDIO
  271. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  272. struct stdio_dev sdev;
  273. #endif
  274. int ret;
  275. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  276. if (ops->setbrg)
  277. ops->setbrg += gd->reloc_off;
  278. if (ops->getc)
  279. ops->getc += gd->reloc_off;
  280. if (ops->putc)
  281. ops->putc += gd->reloc_off;
  282. if (ops->pending)
  283. ops->pending += gd->reloc_off;
  284. if (ops->clear)
  285. ops->clear += gd->reloc_off;
  286. #if CONFIG_POST & CONFIG_SYS_POST_UART
  287. if (ops->loop)
  288. ops->loop += gd->reloc_off
  289. #endif
  290. #endif
  291. /* Set the baud rate */
  292. if (ops->setbrg) {
  293. ret = ops->setbrg(dev, gd->baudrate);
  294. if (ret)
  295. return ret;
  296. }
  297. #ifdef CONFIG_DM_STDIO
  298. if (!(gd->flags & GD_FLG_RELOC))
  299. return 0;
  300. memset(&sdev, '\0', sizeof(sdev));
  301. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  302. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  303. sdev.priv = dev;
  304. sdev.putc = serial_stub_putc;
  305. sdev.puts = serial_stub_puts;
  306. sdev.getc = serial_stub_getc;
  307. sdev.tstc = serial_stub_tstc;
  308. stdio_register_dev(&sdev, &upriv->sdev);
  309. #endif
  310. return 0;
  311. }
  312. static int serial_pre_remove(struct udevice *dev)
  313. {
  314. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  315. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  316. if (stdio_deregister_dev(upriv->sdev, true))
  317. return -EPERM;
  318. #endif
  319. return 0;
  320. }
  321. UCLASS_DRIVER(serial) = {
  322. .id = UCLASS_SERIAL,
  323. .name = "serial",
  324. .flags = DM_UC_FLAG_SEQ_ALIAS,
  325. .post_probe = serial_post_probe,
  326. .pre_remove = serial_pre_remove,
  327. .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
  328. };
  329. #endif