serial-uclass.c 7.3 KB

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