serial.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <environment.h>
  9. #include <serial.h>
  10. #include <stdio_dev.h>
  11. #include <post.h>
  12. #include <linux/compiler.h>
  13. #include <errno.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static struct serial_device *serial_devices;
  16. static struct serial_device *serial_current;
  17. /*
  18. * Table with supported baudrates (defined in config_xyz.h)
  19. */
  20. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  21. /**
  22. * serial_null() - Void registration routine of a serial driver
  23. *
  24. * This routine implements a void registration routine of a serial
  25. * driver. The registration routine of a particular driver is aliased
  26. * to this empty function in case the driver is not compiled into
  27. * U-Boot.
  28. */
  29. static void serial_null(void)
  30. {
  31. }
  32. /**
  33. * on_baudrate() - Update the actual baudrate when the env var changes
  34. *
  35. * This will check for a valid baudrate and only apply it if valid.
  36. */
  37. static int on_baudrate(const char *name, const char *value, enum env_op op,
  38. int flags)
  39. {
  40. int i;
  41. int baudrate;
  42. switch (op) {
  43. case env_op_create:
  44. case env_op_overwrite:
  45. /*
  46. * Switch to new baudrate if new baudrate is supported
  47. */
  48. baudrate = simple_strtoul(value, NULL, 10);
  49. /* Not actually changing */
  50. if (gd->baudrate == baudrate)
  51. return 0;
  52. for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
  53. if (baudrate == baudrate_table[i])
  54. break;
  55. }
  56. if (i == ARRAY_SIZE(baudrate_table)) {
  57. if ((flags & H_FORCE) == 0)
  58. printf("## Baudrate %d bps not supported\n",
  59. baudrate);
  60. return 1;
  61. }
  62. if ((flags & H_INTERACTIVE) != 0) {
  63. printf("## Switch baudrate to %d"
  64. " bps and press ENTER ...\n", baudrate);
  65. udelay(50000);
  66. }
  67. gd->baudrate = baudrate;
  68. serial_setbrg();
  69. udelay(50000);
  70. if ((flags & H_INTERACTIVE) != 0)
  71. while (1) {
  72. if (getc() == '\r')
  73. break;
  74. }
  75. return 0;
  76. case env_op_delete:
  77. printf("## Baudrate may not be deleted\n");
  78. return 1;
  79. default:
  80. return 0;
  81. }
  82. }
  83. U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
  84. /**
  85. * serial_initfunc() - Forward declare of driver registration routine
  86. * @name: Name of the real driver registration routine.
  87. *
  88. * This macro expands onto forward declaration of a driver registration
  89. * routine, which is then used below in serial_initialize() function.
  90. * The declaration is made weak and aliases to serial_null() so in case
  91. * the driver is not compiled in, the function is still declared and can
  92. * be used, but aliases to serial_null() and thus is optimized away.
  93. */
  94. #define serial_initfunc(name) \
  95. void name(void) \
  96. __attribute__((weak, alias("serial_null")));
  97. serial_initfunc(mpc8xx_serial_initialize);
  98. serial_initfunc(ns16550_serial_initialize);
  99. serial_initfunc(pxa_serial_initialize);
  100. serial_initfunc(s3c24xx_serial_initialize);
  101. serial_initfunc(s5p_serial_initialize);
  102. serial_initfunc(zynq_serial_initialize);
  103. serial_initfunc(bfin_serial_initialize);
  104. serial_initfunc(bfin_jtag_initialize);
  105. serial_initfunc(mpc512x_serial_initialize);
  106. serial_initfunc(uartlite_serial_initialize);
  107. serial_initfunc(au1x00_serial_initialize);
  108. serial_initfunc(asc_serial_initialize);
  109. serial_initfunc(jz_serial_initialize);
  110. serial_initfunc(mpc5xx_serial_initialize);
  111. serial_initfunc(mpc8260_scc_serial_initialize);
  112. serial_initfunc(mpc8260_smc_serial_initialize);
  113. serial_initfunc(mpc85xx_serial_initialize);
  114. serial_initfunc(iop480_serial_initialize);
  115. serial_initfunc(leon2_serial_initialize);
  116. serial_initfunc(leon3_serial_initialize);
  117. serial_initfunc(marvell_serial_initialize);
  118. serial_initfunc(amirix_serial_initialize);
  119. serial_initfunc(bmw_serial_initialize);
  120. serial_initfunc(cogent_serial_initialize);
  121. serial_initfunc(cpci750_serial_initialize);
  122. serial_initfunc(evb64260_serial_initialize);
  123. serial_initfunc(ml2_serial_initialize);
  124. serial_initfunc(sconsole_serial_initialize);
  125. serial_initfunc(p3mx_serial_initialize);
  126. serial_initfunc(altera_jtag_serial_initialize);
  127. serial_initfunc(altera_serial_initialize);
  128. serial_initfunc(atmel_serial_initialize);
  129. serial_initfunc(lpc32xx_serial_initialize);
  130. serial_initfunc(mcf_serial_initialize);
  131. serial_initfunc(oc_serial_initialize);
  132. serial_initfunc(sandbox_serial_initialize);
  133. serial_initfunc(clps7111_serial_initialize);
  134. serial_initfunc(imx_serial_initialize);
  135. serial_initfunc(ks8695_serial_initialize);
  136. serial_initfunc(lh7a40x_serial_initialize);
  137. serial_initfunc(max3100_serial_initialize);
  138. serial_initfunc(mxc_serial_initialize);
  139. serial_initfunc(pl01x_serial_initialize);
  140. serial_initfunc(sa1100_serial_initialize);
  141. serial_initfunc(sh_serial_initialize);
  142. serial_initfunc(arm_dcc_initialize);
  143. serial_initfunc(mxs_auart_initialize);
  144. serial_initfunc(arc_serial_initialize);
  145. serial_initfunc(uniphier_serial_initialize);
  146. /**
  147. * serial_register() - Register serial driver with serial driver core
  148. * @dev: Pointer to the serial driver structure
  149. *
  150. * This function registers the serial driver supplied via @dev with
  151. * serial driver core, thus making U-Boot aware of it and making it
  152. * available for U-Boot to use. On platforms that still require manual
  153. * relocation of constant variables, relocation of the supplied structure
  154. * is performed.
  155. */
  156. void serial_register(struct serial_device *dev)
  157. {
  158. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  159. if (dev->start)
  160. dev->start += gd->reloc_off;
  161. if (dev->stop)
  162. dev->stop += gd->reloc_off;
  163. if (dev->setbrg)
  164. dev->setbrg += gd->reloc_off;
  165. if (dev->getc)
  166. dev->getc += gd->reloc_off;
  167. if (dev->tstc)
  168. dev->tstc += gd->reloc_off;
  169. if (dev->putc)
  170. dev->putc += gd->reloc_off;
  171. if (dev->puts)
  172. dev->puts += gd->reloc_off;
  173. #endif
  174. dev->next = serial_devices;
  175. serial_devices = dev;
  176. }
  177. /**
  178. * serial_initialize() - Register all compiled-in serial port drivers
  179. *
  180. * This function registers all serial port drivers that are compiled
  181. * into the U-Boot binary with the serial core, thus making them
  182. * available to U-Boot to use. Lastly, this function assigns a default
  183. * serial port to the serial core. That serial port is then used as a
  184. * default output.
  185. */
  186. void serial_initialize(void)
  187. {
  188. mpc8xx_serial_initialize();
  189. ns16550_serial_initialize();
  190. pxa_serial_initialize();
  191. s3c24xx_serial_initialize();
  192. s5p_serial_initialize();
  193. mpc512x_serial_initialize();
  194. bfin_serial_initialize();
  195. bfin_jtag_initialize();
  196. uartlite_serial_initialize();
  197. zynq_serial_initialize();
  198. au1x00_serial_initialize();
  199. asc_serial_initialize();
  200. jz_serial_initialize();
  201. mpc5xx_serial_initialize();
  202. mpc8260_scc_serial_initialize();
  203. mpc8260_smc_serial_initialize();
  204. mpc85xx_serial_initialize();
  205. iop480_serial_initialize();
  206. leon2_serial_initialize();
  207. leon3_serial_initialize();
  208. marvell_serial_initialize();
  209. amirix_serial_initialize();
  210. bmw_serial_initialize();
  211. cogent_serial_initialize();
  212. cpci750_serial_initialize();
  213. evb64260_serial_initialize();
  214. ml2_serial_initialize();
  215. sconsole_serial_initialize();
  216. p3mx_serial_initialize();
  217. altera_jtag_serial_initialize();
  218. altera_serial_initialize();
  219. atmel_serial_initialize();
  220. lpc32xx_serial_initialize();
  221. mcf_serial_initialize();
  222. oc_serial_initialize();
  223. sandbox_serial_initialize();
  224. clps7111_serial_initialize();
  225. imx_serial_initialize();
  226. ks8695_serial_initialize();
  227. lh7a40x_serial_initialize();
  228. max3100_serial_initialize();
  229. mxc_serial_initialize();
  230. pl01x_serial_initialize();
  231. sa1100_serial_initialize();
  232. sh_serial_initialize();
  233. arm_dcc_initialize();
  234. mxs_auart_initialize();
  235. arc_serial_initialize();
  236. uniphier_serial_initialize();
  237. serial_assign(default_serial_console()->name);
  238. }
  239. static int serial_stub_start(struct stdio_dev *sdev)
  240. {
  241. struct serial_device *dev = sdev->priv;
  242. return dev->start();
  243. }
  244. static int serial_stub_stop(struct stdio_dev *sdev)
  245. {
  246. struct serial_device *dev = sdev->priv;
  247. return dev->stop();
  248. }
  249. static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
  250. {
  251. struct serial_device *dev = sdev->priv;
  252. dev->putc(ch);
  253. }
  254. static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
  255. {
  256. struct serial_device *dev = sdev->priv;
  257. dev->puts(str);
  258. }
  259. int serial_stub_getc(struct stdio_dev *sdev)
  260. {
  261. struct serial_device *dev = sdev->priv;
  262. return dev->getc();
  263. }
  264. int serial_stub_tstc(struct stdio_dev *sdev)
  265. {
  266. struct serial_device *dev = sdev->priv;
  267. return dev->tstc();
  268. }
  269. /**
  270. * serial_stdio_init() - Register serial ports with STDIO core
  271. *
  272. * This function generates a proxy driver for each serial port driver.
  273. * These proxy drivers then register with the STDIO core, making the
  274. * serial drivers available as STDIO devices.
  275. */
  276. void serial_stdio_init(void)
  277. {
  278. struct stdio_dev dev;
  279. struct serial_device *s = serial_devices;
  280. while (s) {
  281. memset(&dev, 0, sizeof(dev));
  282. strcpy(dev.name, s->name);
  283. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  284. dev.start = serial_stub_start;
  285. dev.stop = serial_stub_stop;
  286. dev.putc = serial_stub_putc;
  287. dev.puts = serial_stub_puts;
  288. dev.getc = serial_stub_getc;
  289. dev.tstc = serial_stub_tstc;
  290. dev.priv = s;
  291. stdio_register(&dev);
  292. s = s->next;
  293. }
  294. }
  295. /**
  296. * serial_assign() - Select the serial output device by name
  297. * @name: Name of the serial driver to be used as default output
  298. *
  299. * This function configures the serial output multiplexing by
  300. * selecting which serial device will be used as default. In case
  301. * the STDIO "serial" device is selected as stdin/stdout/stderr,
  302. * the serial device previously configured by this function will be
  303. * used for the particular operation.
  304. *
  305. * Returns 0 on success, negative on error.
  306. */
  307. int serial_assign(const char *name)
  308. {
  309. struct serial_device *s;
  310. for (s = serial_devices; s; s = s->next) {
  311. if (strcmp(s->name, name))
  312. continue;
  313. serial_current = s;
  314. return 0;
  315. }
  316. return -EINVAL;
  317. }
  318. /**
  319. * serial_reinit_all() - Reinitialize all compiled-in serial ports
  320. *
  321. * This function reinitializes all serial ports that are compiled
  322. * into U-Boot by calling their serial_start() functions.
  323. */
  324. void serial_reinit_all(void)
  325. {
  326. struct serial_device *s;
  327. for (s = serial_devices; s; s = s->next)
  328. s->start();
  329. }
  330. /**
  331. * get_current() - Return pointer to currently selected serial port
  332. *
  333. * This function returns a pointer to currently selected serial port.
  334. * The currently selected serial port is altered by serial_assign()
  335. * function.
  336. *
  337. * In case this function is called before relocation or before any serial
  338. * port is configured, this function calls default_serial_console() to
  339. * determine the serial port. Otherwise, the configured serial port is
  340. * returned.
  341. *
  342. * Returns pointer to the currently selected serial port on success,
  343. * NULL on error.
  344. */
  345. static struct serial_device *get_current(void)
  346. {
  347. struct serial_device *dev;
  348. if (!(gd->flags & GD_FLG_RELOC))
  349. dev = default_serial_console();
  350. else if (!serial_current)
  351. dev = default_serial_console();
  352. else
  353. dev = serial_current;
  354. /* We must have a console device */
  355. if (!dev) {
  356. #ifdef CONFIG_SPL_BUILD
  357. puts("Cannot find console\n");
  358. hang();
  359. #else
  360. panic("Cannot find console\n");
  361. #endif
  362. }
  363. return dev;
  364. }
  365. /**
  366. * serial_init() - Initialize currently selected serial port
  367. *
  368. * This function initializes the currently selected serial port. This
  369. * usually involves setting up the registers of that particular port,
  370. * enabling clock and such. This function uses the get_current() call
  371. * to determine which port is selected.
  372. *
  373. * Returns 0 on success, negative on error.
  374. */
  375. int serial_init(void)
  376. {
  377. gd->flags |= GD_FLG_SERIAL_READY;
  378. return get_current()->start();
  379. }
  380. /**
  381. * serial_setbrg() - Configure baud-rate of currently selected serial port
  382. *
  383. * This function configures the baud-rate of the currently selected
  384. * serial port. The baud-rate is retrieved from global data within
  385. * the serial port driver. This function uses the get_current() call
  386. * to determine which port is selected.
  387. *
  388. * Returns 0 on success, negative on error.
  389. */
  390. void serial_setbrg(void)
  391. {
  392. get_current()->setbrg();
  393. }
  394. /**
  395. * serial_getc() - Read character from currently selected serial port
  396. *
  397. * This function retrieves a character from currently selected serial
  398. * port. In case there is no character waiting on the serial port,
  399. * this function will block and wait for the character to appear. This
  400. * function uses the get_current() call to determine which port is
  401. * selected.
  402. *
  403. * Returns the character on success, negative on error.
  404. */
  405. int serial_getc(void)
  406. {
  407. return get_current()->getc();
  408. }
  409. /**
  410. * serial_tstc() - Test if data is available on currently selected serial port
  411. *
  412. * This function tests if one or more characters are available on
  413. * currently selected serial port. This function never blocks. This
  414. * function uses the get_current() call to determine which port is
  415. * selected.
  416. *
  417. * Returns positive if character is available, zero otherwise.
  418. */
  419. int serial_tstc(void)
  420. {
  421. return get_current()->tstc();
  422. }
  423. /**
  424. * serial_putc() - Output character via currently selected serial port
  425. * @c: Single character to be output from the serial port.
  426. *
  427. * This function outputs a character via currently selected serial
  428. * port. This character is passed to the serial port driver responsible
  429. * for controlling the hardware. The hardware may still be in process
  430. * of transmitting another character, therefore this function may block
  431. * for a short amount of time. This function uses the get_current()
  432. * call to determine which port is selected.
  433. */
  434. void serial_putc(const char c)
  435. {
  436. get_current()->putc(c);
  437. }
  438. /**
  439. * serial_puts() - Output string via currently selected serial port
  440. * @s: Zero-terminated string to be output from the serial port.
  441. *
  442. * This function outputs a zero-terminated string via currently
  443. * selected serial port. This function behaves as an accelerator
  444. * in case the hardware can queue multiple characters for transfer.
  445. * The whole string that is to be output is available to the function
  446. * implementing the hardware manipulation. Transmitting the whole
  447. * string may take some time, thus this function may block for some
  448. * amount of time. This function uses the get_current() call to
  449. * determine which port is selected.
  450. */
  451. void serial_puts(const char *s)
  452. {
  453. get_current()->puts(s);
  454. }
  455. /**
  456. * default_serial_puts() - Output string by calling serial_putc() in loop
  457. * @s: Zero-terminated string to be output from the serial port.
  458. *
  459. * This function outputs a zero-terminated string by calling serial_putc()
  460. * in a loop. Most drivers do not support queueing more than one byte for
  461. * transfer, thus this function precisely implements their serial_puts().
  462. *
  463. * To optimize the number of get_current() calls, this function only
  464. * calls get_current() once and then directly accesses the putc() call
  465. * of the &struct serial_device .
  466. */
  467. void default_serial_puts(const char *s)
  468. {
  469. struct serial_device *dev = get_current();
  470. while (*s)
  471. dev->putc(*s++);
  472. }
  473. #if CONFIG_POST & CONFIG_SYS_POST_UART
  474. static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
  475. /**
  476. * uart_post_test() - Test the currently selected serial port using POST
  477. * @flags: POST framework flags
  478. *
  479. * Do a loopback test of the currently selected serial port. This
  480. * function is only useful in the context of the POST testing framwork.
  481. * The serial port is firstly configured into loopback mode and then
  482. * characters are sent through it.
  483. *
  484. * Returns 0 on success, value otherwise.
  485. */
  486. /* Mark weak until post/cpu/.../uart.c migrate over */
  487. __weak
  488. int uart_post_test(int flags)
  489. {
  490. unsigned char c;
  491. int ret, saved_baud, b;
  492. struct serial_device *saved_dev, *s;
  493. /* Save current serial state */
  494. ret = 0;
  495. saved_dev = serial_current;
  496. saved_baud = gd->baudrate;
  497. for (s = serial_devices; s; s = s->next) {
  498. /* If this driver doesn't support loop back, skip it */
  499. if (!s->loop)
  500. continue;
  501. /* Test the next device */
  502. serial_current = s;
  503. ret = serial_init();
  504. if (ret)
  505. goto done;
  506. /* Consume anything that happens to be queued */
  507. while (serial_tstc())
  508. serial_getc();
  509. /* Enable loop back */
  510. s->loop(1);
  511. /* Test every available baud rate */
  512. for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
  513. gd->baudrate = bauds[b];
  514. serial_setbrg();
  515. /*
  516. * Stick to printable chars to avoid issues:
  517. * - terminal corruption
  518. * - serial program reacting to sequences and sending
  519. * back random extra data
  520. * - most serial drivers add in extra chars (like \r\n)
  521. */
  522. for (c = 0x20; c < 0x7f; ++c) {
  523. /* Send it out */
  524. serial_putc(c);
  525. /* Make sure it's the same one */
  526. ret = (c != serial_getc());
  527. if (ret) {
  528. s->loop(0);
  529. goto done;
  530. }
  531. /* Clean up the output in case it was sent */
  532. serial_putc('\b');
  533. ret = ('\b' != serial_getc());
  534. if (ret) {
  535. s->loop(0);
  536. goto done;
  537. }
  538. }
  539. }
  540. /* Disable loop back */
  541. s->loop(0);
  542. /* XXX: There is no serial_stop() !? */
  543. if (s->stop)
  544. s->stop();
  545. }
  546. done:
  547. /* Restore previous serial state */
  548. serial_current = saved_dev;
  549. gd->baudrate = saved_baud;
  550. serial_reinit_all();
  551. serial_setbrg();
  552. return ret;
  553. }
  554. #endif