serial-uclass.c 10 KB

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