serial.c 14 KB

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