serial-uclass.c 11 KB

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