serial-uclass.c 10 KB

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