serial-uclass.c 10 KB

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