serial-uclass.c 7.5 KB

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