serial.c 15 KB

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