serial.c 17 KB

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