serial-uclass.c 9.4 KB

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