serial.c 16 KB

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