serial-uclass.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. int ret;
  68. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  69. uclass_first_device(UCLASS_SERIAL, &dev);
  70. if (dev) {
  71. gd->cur_serial_dev = dev;
  72. return;
  73. }
  74. } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
  75. /* Live tree has support for stdout */
  76. if (of_live_active()) {
  77. struct device_node *np = of_get_stdout();
  78. if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
  79. np_to_ofnode(np), &dev)) {
  80. gd->cur_serial_dev = dev;
  81. return;
  82. }
  83. } else {
  84. if (!serial_check_stdout(blob, &dev)) {
  85. gd->cur_serial_dev = dev;
  86. return;
  87. }
  88. }
  89. }
  90. if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
  91. /*
  92. * Try to use CONFIG_CONS_INDEX if available (it is numbered
  93. * from 1!).
  94. *
  95. * Failing that, get the device with sequence number 0, or in
  96. * extremis just the first working serial device we can find.
  97. * But we insist on having a console (even if it is silent).
  98. */
  99. #ifdef CONFIG_CONS_INDEX
  100. #define INDEX (CONFIG_CONS_INDEX - 1)
  101. #else
  102. #define INDEX 0
  103. #endif
  104. if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
  105. !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
  106. if (dev->flags & DM_FLAG_ACTIVATED) {
  107. gd->cur_serial_dev = dev;
  108. return;
  109. }
  110. }
  111. /* Search for any working device */
  112. for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
  113. dev;
  114. ret = uclass_next_device_check(&dev)) {
  115. if (!ret) {
  116. /* Device did succeed probing */
  117. gd->cur_serial_dev = dev;
  118. return;
  119. }
  120. }
  121. #undef INDEX
  122. }
  123. #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
  124. panic_str("No serial driver found");
  125. #endif
  126. }
  127. /* Called prior to relocation */
  128. int serial_init(void)
  129. {
  130. serial_find_console_or_panic();
  131. gd->flags |= GD_FLG_SERIAL_READY;
  132. return 0;
  133. }
  134. /* Called after relocation */
  135. void serial_initialize(void)
  136. {
  137. serial_init();
  138. }
  139. static void _serial_putc(struct udevice *dev, char ch)
  140. {
  141. struct dm_serial_ops *ops = serial_get_ops(dev);
  142. int err;
  143. if (ch == '\n')
  144. _serial_putc(dev, '\r');
  145. do {
  146. err = ops->putc(dev, ch);
  147. } while (err == -EAGAIN);
  148. }
  149. static void _serial_puts(struct udevice *dev, const char *str)
  150. {
  151. while (*str)
  152. _serial_putc(dev, *str++);
  153. }
  154. static int __serial_getc(struct udevice *dev)
  155. {
  156. struct dm_serial_ops *ops = serial_get_ops(dev);
  157. int err;
  158. do {
  159. err = ops->getc(dev);
  160. if (err == -EAGAIN)
  161. WATCHDOG_RESET();
  162. } while (err == -EAGAIN);
  163. return err >= 0 ? err : 0;
  164. }
  165. static int __serial_tstc(struct udevice *dev)
  166. {
  167. struct dm_serial_ops *ops = serial_get_ops(dev);
  168. if (ops->pending)
  169. return ops->pending(dev, true);
  170. return 1;
  171. }
  172. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  173. static int _serial_tstc(struct udevice *dev)
  174. {
  175. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  176. /* Read all available chars into the RX buffer */
  177. while (__serial_tstc(dev)) {
  178. upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
  179. upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  180. }
  181. return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
  182. }
  183. static int _serial_getc(struct udevice *dev)
  184. {
  185. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  186. char val;
  187. val = upriv->buf[upriv->rd_ptr++];
  188. upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  189. return val;
  190. }
  191. #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  192. static int _serial_getc(struct udevice *dev)
  193. {
  194. return __serial_getc(dev);
  195. }
  196. static int _serial_tstc(struct udevice *dev)
  197. {
  198. return __serial_tstc(dev);
  199. }
  200. #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  201. void serial_putc(char ch)
  202. {
  203. if (gd->cur_serial_dev)
  204. _serial_putc(gd->cur_serial_dev, ch);
  205. }
  206. void serial_puts(const char *str)
  207. {
  208. if (gd->cur_serial_dev)
  209. _serial_puts(gd->cur_serial_dev, str);
  210. }
  211. int serial_getc(void)
  212. {
  213. if (!gd->cur_serial_dev)
  214. return 0;
  215. return _serial_getc(gd->cur_serial_dev);
  216. }
  217. int serial_tstc(void)
  218. {
  219. if (!gd->cur_serial_dev)
  220. return 0;
  221. return _serial_tstc(gd->cur_serial_dev);
  222. }
  223. void serial_setbrg(void)
  224. {
  225. struct dm_serial_ops *ops;
  226. if (!gd->cur_serial_dev)
  227. return;
  228. ops = serial_get_ops(gd->cur_serial_dev);
  229. if (ops->setbrg)
  230. ops->setbrg(gd->cur_serial_dev, gd->baudrate);
  231. }
  232. void serial_stdio_init(void)
  233. {
  234. }
  235. #if defined(CONFIG_DM_STDIO)
  236. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  237. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  238. {
  239. _serial_putc(sdev->priv, ch);
  240. }
  241. #endif
  242. static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  243. {
  244. _serial_puts(sdev->priv, str);
  245. }
  246. static int serial_stub_getc(struct stdio_dev *sdev)
  247. {
  248. return _serial_getc(sdev->priv);
  249. }
  250. static int serial_stub_tstc(struct stdio_dev *sdev)
  251. {
  252. return _serial_tstc(sdev->priv);
  253. }
  254. #endif
  255. /**
  256. * on_baudrate() - Update the actual baudrate when the env var changes
  257. *
  258. * This will check for a valid baudrate and only apply it if valid.
  259. */
  260. static int on_baudrate(const char *name, const char *value, enum env_op op,
  261. int flags)
  262. {
  263. int i;
  264. int baudrate;
  265. switch (op) {
  266. case env_op_create:
  267. case env_op_overwrite:
  268. /*
  269. * Switch to new baudrate if new baudrate is supported
  270. */
  271. baudrate = simple_strtoul(value, NULL, 10);
  272. /* Not actually changing */
  273. if (gd->baudrate == baudrate)
  274. return 0;
  275. for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
  276. if (baudrate == baudrate_table[i])
  277. break;
  278. }
  279. if (i == ARRAY_SIZE(baudrate_table)) {
  280. if ((flags & H_FORCE) == 0)
  281. printf("## Baudrate %d bps not supported\n",
  282. baudrate);
  283. return 1;
  284. }
  285. if ((flags & H_INTERACTIVE) != 0) {
  286. printf("## Switch baudrate to %d bps and press ENTER ...\n",
  287. baudrate);
  288. udelay(50000);
  289. }
  290. gd->baudrate = baudrate;
  291. serial_setbrg();
  292. udelay(50000);
  293. if ((flags & H_INTERACTIVE) != 0)
  294. while (1) {
  295. if (getc() == '\r')
  296. break;
  297. }
  298. return 0;
  299. case env_op_delete:
  300. printf("## Baudrate may not be deleted\n");
  301. return 1;
  302. default:
  303. return 0;
  304. }
  305. }
  306. U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  307. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  308. static int serial_post_probe(struct udevice *dev)
  309. {
  310. struct dm_serial_ops *ops = serial_get_ops(dev);
  311. #ifdef CONFIG_DM_STDIO
  312. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  313. struct stdio_dev sdev;
  314. #endif
  315. int ret;
  316. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  317. if (ops->setbrg)
  318. ops->setbrg += gd->reloc_off;
  319. if (ops->getc)
  320. ops->getc += gd->reloc_off;
  321. if (ops->putc)
  322. ops->putc += gd->reloc_off;
  323. if (ops->pending)
  324. ops->pending += gd->reloc_off;
  325. if (ops->clear)
  326. ops->clear += gd->reloc_off;
  327. #if CONFIG_POST & CONFIG_SYS_POST_UART
  328. if (ops->loop)
  329. ops->loop += gd->reloc_off
  330. #endif
  331. #endif
  332. /* Set the baud rate */
  333. if (ops->setbrg) {
  334. ret = ops->setbrg(dev, gd->baudrate);
  335. if (ret)
  336. return ret;
  337. }
  338. #ifdef CONFIG_DM_STDIO
  339. if (!(gd->flags & GD_FLG_RELOC))
  340. return 0;
  341. memset(&sdev, '\0', sizeof(sdev));
  342. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  343. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
  344. sdev.priv = dev;
  345. sdev.putc = serial_stub_putc;
  346. sdev.puts = serial_stub_puts;
  347. sdev.getc = serial_stub_getc;
  348. sdev.tstc = serial_stub_tstc;
  349. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  350. /* Allocate the RX buffer */
  351. upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
  352. #endif
  353. stdio_register_dev(&sdev, &upriv->sdev);
  354. #endif
  355. return 0;
  356. }
  357. static int serial_pre_remove(struct udevice *dev)
  358. {
  359. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  360. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  361. if (stdio_deregister_dev(upriv->sdev, true))
  362. return -EPERM;
  363. #endif
  364. return 0;
  365. }
  366. UCLASS_DRIVER(serial) = {
  367. .id = UCLASS_SERIAL,
  368. .name = "serial",
  369. .flags = DM_UC_FLAG_SEQ_ALIAS,
  370. .post_probe = serial_post_probe,
  371. .pre_remove = serial_pre_remove,
  372. .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
  373. };
  374. #endif