serial.c 14 KB

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