serial-uclass.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  161. static int _serial_tstc(struct udevice *dev)
  162. {
  163. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  164. /* Read all available chars into the RX buffer */
  165. while (__serial_tstc(dev)) {
  166. upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
  167. upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  168. }
  169. return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
  170. }
  171. static int _serial_getc(struct udevice *dev)
  172. {
  173. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  174. char val;
  175. val = upriv->buf[upriv->rd_ptr++];
  176. upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  177. return val;
  178. }
  179. #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  180. static int _serial_getc(struct udevice *dev)
  181. {
  182. return __serial_getc(dev);
  183. }
  184. static int _serial_tstc(struct udevice *dev)
  185. {
  186. return __serial_tstc(dev);
  187. }
  188. #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  189. void serial_putc(char ch)
  190. {
  191. if (gd->cur_serial_dev)
  192. _serial_putc(gd->cur_serial_dev, ch);
  193. }
  194. void serial_puts(const char *str)
  195. {
  196. if (gd->cur_serial_dev)
  197. _serial_puts(gd->cur_serial_dev, str);
  198. }
  199. int serial_getc(void)
  200. {
  201. if (!gd->cur_serial_dev)
  202. return 0;
  203. return _serial_getc(gd->cur_serial_dev);
  204. }
  205. int serial_tstc(void)
  206. {
  207. if (!gd->cur_serial_dev)
  208. return 0;
  209. return _serial_tstc(gd->cur_serial_dev);
  210. }
  211. void serial_setbrg(void)
  212. {
  213. struct dm_serial_ops *ops;
  214. if (!gd->cur_serial_dev)
  215. return;
  216. ops = serial_get_ops(gd->cur_serial_dev);
  217. if (ops->setbrg)
  218. ops->setbrg(gd->cur_serial_dev, gd->baudrate);
  219. }
  220. void serial_stdio_init(void)
  221. {
  222. }
  223. #if defined(CONFIG_DM_STDIO)
  224. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  225. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  226. {
  227. _serial_putc(sdev->priv, ch);
  228. }
  229. #endif
  230. static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  231. {
  232. _serial_puts(sdev->priv, str);
  233. }
  234. static int serial_stub_getc(struct stdio_dev *sdev)
  235. {
  236. return _serial_getc(sdev->priv);
  237. }
  238. static int serial_stub_tstc(struct stdio_dev *sdev)
  239. {
  240. return _serial_tstc(sdev->priv);
  241. }
  242. #endif
  243. /**
  244. * on_baudrate() - Update the actual baudrate when the env var changes
  245. *
  246. * This will check for a valid baudrate and only apply it if valid.
  247. */
  248. static int on_baudrate(const char *name, const char *value, enum env_op op,
  249. int flags)
  250. {
  251. int i;
  252. int baudrate;
  253. switch (op) {
  254. case env_op_create:
  255. case env_op_overwrite:
  256. /*
  257. * Switch to new baudrate if new baudrate is supported
  258. */
  259. baudrate = simple_strtoul(value, NULL, 10);
  260. /* Not actually changing */
  261. if (gd->baudrate == baudrate)
  262. return 0;
  263. for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
  264. if (baudrate == baudrate_table[i])
  265. break;
  266. }
  267. if (i == ARRAY_SIZE(baudrate_table)) {
  268. if ((flags & H_FORCE) == 0)
  269. printf("## Baudrate %d bps not supported\n",
  270. baudrate);
  271. return 1;
  272. }
  273. if ((flags & H_INTERACTIVE) != 0) {
  274. printf("## Switch baudrate to %d bps and press ENTER ...\n",
  275. baudrate);
  276. udelay(50000);
  277. }
  278. gd->baudrate = baudrate;
  279. serial_setbrg();
  280. udelay(50000);
  281. if ((flags & H_INTERACTIVE) != 0)
  282. while (1) {
  283. if (getc() == '\r')
  284. break;
  285. }
  286. return 0;
  287. case env_op_delete:
  288. printf("## Baudrate may not be deleted\n");
  289. return 1;
  290. default:
  291. return 0;
  292. }
  293. }
  294. U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  295. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  296. static int serial_post_probe(struct udevice *dev)
  297. {
  298. struct dm_serial_ops *ops = serial_get_ops(dev);
  299. #ifdef CONFIG_DM_STDIO
  300. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  301. struct stdio_dev sdev;
  302. #endif
  303. int ret;
  304. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  305. if (ops->setbrg)
  306. ops->setbrg += gd->reloc_off;
  307. if (ops->getc)
  308. ops->getc += gd->reloc_off;
  309. if (ops->putc)
  310. ops->putc += gd->reloc_off;
  311. if (ops->pending)
  312. ops->pending += gd->reloc_off;
  313. if (ops->clear)
  314. ops->clear += gd->reloc_off;
  315. #if CONFIG_POST & CONFIG_SYS_POST_UART
  316. if (ops->loop)
  317. ops->loop += gd->reloc_off
  318. #endif
  319. #endif
  320. /* Set the baud rate */
  321. if (ops->setbrg) {
  322. ret = ops->setbrg(dev, gd->baudrate);
  323. if (ret)
  324. return ret;
  325. }
  326. #ifdef CONFIG_DM_STDIO
  327. if (!(gd->flags & GD_FLG_RELOC))
  328. return 0;
  329. memset(&sdev, '\0', sizeof(sdev));
  330. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  331. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
  332. sdev.priv = dev;
  333. sdev.putc = serial_stub_putc;
  334. sdev.puts = serial_stub_puts;
  335. sdev.getc = serial_stub_getc;
  336. sdev.tstc = serial_stub_tstc;
  337. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  338. /* Allocate the RX buffer */
  339. upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
  340. #endif
  341. stdio_register_dev(&sdev, &upriv->sdev);
  342. #endif
  343. return 0;
  344. }
  345. static int serial_pre_remove(struct udevice *dev)
  346. {
  347. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  348. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  349. if (stdio_deregister_dev(upriv->sdev, true))
  350. return -EPERM;
  351. #endif
  352. return 0;
  353. }
  354. UCLASS_DRIVER(serial) = {
  355. .id = UCLASS_SERIAL,
  356. .name = "serial",
  357. .flags = DM_UC_FLAG_SEQ_ALIAS,
  358. .post_probe = serial_post_probe,
  359. .pre_remove = serial_pre_remove,
  360. .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
  361. };
  362. #endif