serial.c 15 KB

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