serial.c 14 KB

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