serial.c 14 KB

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