serial-uclass.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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[] = CFG_SYS_BAUDRATE_TABLE;
  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;
  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. /*
  37. * This also deals with things like
  38. *
  39. * stdout-path = "serial0:115200n8";
  40. *
  41. * since fdt_path_offset_namelen() treats a str not
  42. * beginning with '/' as an alias and thus applies
  43. * fdt_get_alias_namelen() to it.
  44. */
  45. node = fdt_path_offset_namelen(blob, str, namelen);
  46. }
  47. if (node < 0)
  48. node = fdt_path_offset(blob, "console");
  49. if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
  50. return 0;
  51. /*
  52. * If the console is not marked to be bound before relocation, bind it
  53. * anyway.
  54. */
  55. if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
  56. devp, NULL, false)) {
  57. if (device_get_uclass_id(*devp) == UCLASS_SERIAL &&
  58. !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 (!IS_ENABLED(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(OF_CONTROL) ||
  93. !blob) {
  94. /*
  95. * Try to use CONFIG_CONS_INDEX if available (it is numbered
  96. * from 1!).
  97. *
  98. * Failing that, get the device with sequence number 0, or in
  99. * extremis just the first working serial device we can find.
  100. * But we insist on having a console (even if it is silent).
  101. */
  102. #ifdef CONFIG_CONS_INDEX
  103. #define INDEX (CONFIG_CONS_INDEX - 1)
  104. #else
  105. #define INDEX 0
  106. #endif
  107. #ifdef CONFIG_SERIAL_SEARCH_ALL
  108. if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
  109. !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
  110. if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
  111. gd->cur_serial_dev = dev;
  112. return;
  113. }
  114. }
  115. /* Search for any working device */
  116. for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
  117. dev;
  118. ret = uclass_next_device_check(&dev)) {
  119. if (!ret) {
  120. /* Device did succeed probing */
  121. gd->cur_serial_dev = dev;
  122. return;
  123. }
  124. }
  125. #else
  126. if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
  127. !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
  128. !uclass_first_device_err(UCLASS_SERIAL, &dev)) {
  129. gd->cur_serial_dev = dev;
  130. return;
  131. }
  132. #endif
  133. #undef INDEX
  134. }
  135. #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
  136. panic_str("No serial driver found");
  137. #endif
  138. }
  139. #endif /* CONFIG_SERIAL_PRESENT */
  140. /* Called prior to relocation */
  141. int serial_init(void)
  142. {
  143. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  144. serial_find_console_or_panic();
  145. if(gd->cur_serial_dev)
  146. gd->flags |= GD_FLG_SERIAL_READY;
  147. serial_setbrg();
  148. #endif
  149. return 0;
  150. }
  151. /* Called after relocation */
  152. int serial_initialize(void)
  153. {
  154. /* Scanning uclass to probe devices */
  155. if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
  156. int ret;
  157. ret = uclass_probe_all(UCLASS_SERIAL);
  158. if (ret)
  159. return ret;
  160. }
  161. return serial_init();
  162. }
  163. static void _serial_putc(struct udevice *dev, char ch)
  164. {
  165. struct dm_serial_ops *ops = serial_get_ops(dev);
  166. int err;
  167. if (ch == '\n')
  168. _serial_putc(dev, '\r');
  169. do {
  170. err = ops->putc(dev, ch);
  171. } while (err == -EAGAIN);
  172. }
  173. static int __serial_puts(struct udevice *dev, const char *str, size_t len)
  174. {
  175. struct dm_serial_ops *ops = serial_get_ops(dev);
  176. do {
  177. ssize_t written = ops->puts(dev, str, len);
  178. if (written < 0)
  179. return written;
  180. str += written;
  181. len -= written;
  182. } while (len);
  183. return 0;
  184. }
  185. static void _serial_puts(struct udevice *dev, const char *str)
  186. {
  187. struct dm_serial_ops *ops = serial_get_ops(dev);
  188. if (!CONFIG_IS_ENABLED(SERIAL_PUTS) || !ops->puts) {
  189. while (*str)
  190. _serial_putc(dev, *str++);
  191. return;
  192. }
  193. do {
  194. const char *newline = strchrnul(str, '\n');
  195. size_t len = newline - str;
  196. if (__serial_puts(dev, str, len))
  197. return;
  198. if (*newline && __serial_puts(dev, "\r\n", 2))
  199. return;
  200. str += len + !!*newline;
  201. } while (*str);
  202. }
  203. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  204. static void _serial_flush(struct udevice *dev)
  205. {
  206. struct dm_serial_ops *ops = serial_get_ops(dev);
  207. if (!ops->pending)
  208. return;
  209. while (ops->pending(dev, false) > 0)
  210. ;
  211. }
  212. #endif
  213. static int __serial_getc(struct udevice *dev)
  214. {
  215. struct dm_serial_ops *ops = serial_get_ops(dev);
  216. int err;
  217. do {
  218. err = ops->getc(dev);
  219. if (err == -EAGAIN)
  220. schedule();
  221. } while (err == -EAGAIN);
  222. return err >= 0 ? err : 0;
  223. }
  224. static int __serial_tstc(struct udevice *dev)
  225. {
  226. struct dm_serial_ops *ops = serial_get_ops(dev);
  227. if (ops->pending)
  228. return ops->pending(dev, true);
  229. return 1;
  230. }
  231. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  232. static int _serial_tstc(struct udevice *dev)
  233. {
  234. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  235. /* Read all available chars into the RX buffer */
  236. while (__serial_tstc(dev)) {
  237. upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
  238. upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  239. }
  240. return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
  241. }
  242. static int _serial_getc(struct udevice *dev)
  243. {
  244. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  245. char val;
  246. if (upriv->rd_ptr == upriv->wr_ptr)
  247. return __serial_getc(dev);
  248. val = upriv->buf[upriv->rd_ptr++];
  249. upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
  250. return val;
  251. }
  252. #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  253. static int _serial_getc(struct udevice *dev)
  254. {
  255. return __serial_getc(dev);
  256. }
  257. static int _serial_tstc(struct udevice *dev)
  258. {
  259. return __serial_tstc(dev);
  260. }
  261. #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
  262. void serial_putc(char ch)
  263. {
  264. if (gd->cur_serial_dev)
  265. _serial_putc(gd->cur_serial_dev, ch);
  266. }
  267. void serial_puts(const char *str)
  268. {
  269. if (gd->cur_serial_dev)
  270. _serial_puts(gd->cur_serial_dev, str);
  271. }
  272. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  273. void serial_flush(void)
  274. {
  275. if (!gd->cur_serial_dev)
  276. return;
  277. _serial_flush(gd->cur_serial_dev);
  278. }
  279. #endif
  280. int serial_getc(void)
  281. {
  282. if (!gd->cur_serial_dev)
  283. return 0;
  284. return _serial_getc(gd->cur_serial_dev);
  285. }
  286. int serial_tstc(void)
  287. {
  288. if (!gd->cur_serial_dev)
  289. return 0;
  290. return _serial_tstc(gd->cur_serial_dev);
  291. }
  292. void serial_setbrg(void)
  293. {
  294. struct dm_serial_ops *ops;
  295. if (!gd->cur_serial_dev)
  296. return;
  297. ops = serial_get_ops(gd->cur_serial_dev);
  298. if (ops->setbrg)
  299. ops->setbrg(gd->cur_serial_dev, gd->baudrate);
  300. }
  301. int serial_getconfig(struct udevice *dev, uint *config)
  302. {
  303. struct dm_serial_ops *ops;
  304. ops = serial_get_ops(dev);
  305. if (ops->getconfig)
  306. return ops->getconfig(dev, config);
  307. return 0;
  308. }
  309. int serial_setconfig(struct udevice *dev, uint config)
  310. {
  311. struct dm_serial_ops *ops;
  312. ops = serial_get_ops(dev);
  313. if (ops->setconfig)
  314. return ops->setconfig(dev, config);
  315. return 0;
  316. }
  317. int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
  318. {
  319. struct dm_serial_ops *ops;
  320. if (!info)
  321. return -EINVAL;
  322. info->baudrate = gd->baudrate;
  323. ops = serial_get_ops(dev);
  324. if (ops->getinfo)
  325. return ops->getinfo(dev, info);
  326. return -EINVAL;
  327. }
  328. void serial_stdio_init(void)
  329. {
  330. }
  331. #if CONFIG_IS_ENABLED(DM_STDIO)
  332. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  333. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  334. {
  335. _serial_putc(sdev->priv, ch);
  336. }
  337. static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  338. {
  339. _serial_puts(sdev->priv, str);
  340. }
  341. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  342. static void serial_stub_flush(struct stdio_dev *sdev)
  343. {
  344. _serial_flush(sdev->priv);
  345. }
  346. #endif
  347. static int serial_stub_getc(struct stdio_dev *sdev)
  348. {
  349. return _serial_getc(sdev->priv);
  350. }
  351. static int serial_stub_tstc(struct stdio_dev *sdev)
  352. {
  353. return _serial_tstc(sdev->priv);
  354. }
  355. #endif
  356. #endif
  357. /**
  358. * on_baudrate() - Update the actual baudrate when the env var changes
  359. *
  360. * This will check for a valid baudrate and only apply it if valid.
  361. */
  362. static int on_baudrate(const char *name, const char *value, enum env_op op,
  363. int flags)
  364. {
  365. int i;
  366. int baudrate;
  367. switch (op) {
  368. case env_op_create:
  369. case env_op_overwrite:
  370. /*
  371. * Switch to new baudrate if new baudrate is supported
  372. */
  373. baudrate = dectoul(value, NULL);
  374. /* Not actually changing */
  375. if (gd->baudrate == baudrate)
  376. return 0;
  377. for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
  378. if (baudrate == baudrate_table[i])
  379. break;
  380. }
  381. if (i == ARRAY_SIZE(baudrate_table)) {
  382. if ((flags & H_FORCE) == 0)
  383. printf("## Baudrate %d bps not supported\n",
  384. baudrate);
  385. return 1;
  386. }
  387. if ((flags & H_INTERACTIVE) != 0) {
  388. printf("## Switch baudrate to %d bps and press ENTER ...\n",
  389. baudrate);
  390. udelay(50000);
  391. flush();
  392. }
  393. gd->baudrate = baudrate;
  394. serial_setbrg();
  395. udelay(50000);
  396. if ((flags & H_INTERACTIVE) != 0)
  397. while (1) {
  398. if (getchar() == '\r')
  399. break;
  400. }
  401. return 0;
  402. case env_op_delete:
  403. printf("## Baudrate may not be deleted\n");
  404. return 1;
  405. default:
  406. return 0;
  407. }
  408. }
  409. U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  410. #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
  411. static int serial_post_probe(struct udevice *dev)
  412. {
  413. struct dm_serial_ops *ops = serial_get_ops(dev);
  414. #if CONFIG_IS_ENABLED(DM_STDIO)
  415. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  416. struct stdio_dev sdev;
  417. #endif
  418. int ret;
  419. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  420. if (ops->setbrg)
  421. ops->setbrg += gd->reloc_off;
  422. if (ops->getc)
  423. ops->getc += gd->reloc_off;
  424. if (ops->putc)
  425. ops->putc += gd->reloc_off;
  426. if (ops->pending)
  427. ops->pending += gd->reloc_off;
  428. if (ops->clear)
  429. ops->clear += gd->reloc_off;
  430. if (ops->getconfig)
  431. ops->getconfig += gd->reloc_off;
  432. if (ops->setconfig)
  433. ops->setconfig += gd->reloc_off;
  434. #if CFG_POST & CFG_SYS_POST_UART
  435. if (ops->loop)
  436. ops->loop += gd->reloc_off;
  437. #endif
  438. if (ops->getinfo)
  439. ops->getinfo += gd->reloc_off;
  440. #endif
  441. /* Set the baud rate */
  442. if (ops->setbrg) {
  443. ret = ops->setbrg(dev, gd->baudrate);
  444. if (ret)
  445. return ret;
  446. }
  447. #if CONFIG_IS_ENABLED(DM_STDIO)
  448. if (!(gd->flags & GD_FLG_RELOC))
  449. return 0;
  450. memset(&sdev, '\0', sizeof(sdev));
  451. strncpy(sdev.name, dev->name, sizeof(sdev.name));
  452. sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
  453. sdev.priv = dev;
  454. sdev.putc = serial_stub_putc;
  455. sdev.puts = serial_stub_puts;
  456. STDIO_DEV_ASSIGN_FLUSH(&sdev, serial_stub_flush);
  457. sdev.getc = serial_stub_getc;
  458. sdev.tstc = serial_stub_tstc;
  459. #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
  460. /* Allocate the RX buffer */
  461. upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
  462. #endif
  463. stdio_register_dev(&sdev, &upriv->sdev);
  464. #endif
  465. return 0;
  466. }
  467. static int serial_pre_remove(struct udevice *dev)
  468. {
  469. #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
  470. struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
  471. if (stdio_deregister_dev(upriv->sdev, true))
  472. return -EPERM;
  473. #endif
  474. return 0;
  475. }
  476. UCLASS_DRIVER(serial) = {
  477. .id = UCLASS_SERIAL,
  478. .name = "serial",
  479. .flags = DM_UC_FLAG_SEQ_ALIAS,
  480. .post_probe = serial_post_probe,
  481. .pre_remove = serial_pre_remove,
  482. .per_device_auto = sizeof(struct serial_dev_priv),
  483. };
  484. #endif