serial-uclass.c 7.6 KB

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