serial.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(pl01x_serial_initialize);
  103. serial_initfunc(s3c44b0_serial_initialize);
  104. serial_initfunc(sa1100_serial_initialize);
  105. serial_initfunc(sh_serial_initialize);
  106. /**
  107. * serial_register() - Register serial driver with serial driver core
  108. * @dev: Pointer to the serial driver structure
  109. *
  110. * This function registers the serial driver supplied via @dev with
  111. * serial driver core, thus making U-Boot aware of it and making it
  112. * available for U-Boot to use. On platforms that still require manual
  113. * relocation of constant variables, relocation of the supplied structure
  114. * is performed.
  115. */
  116. void serial_register(struct serial_device *dev)
  117. {
  118. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  119. if (dev->start)
  120. dev->start += gd->reloc_off;
  121. if (dev->stop)
  122. dev->stop += gd->reloc_off;
  123. if (dev->setbrg)
  124. dev->setbrg += gd->reloc_off;
  125. if (dev->getc)
  126. dev->getc += gd->reloc_off;
  127. if (dev->tstc)
  128. dev->tstc += gd->reloc_off;
  129. if (dev->putc)
  130. dev->putc += gd->reloc_off;
  131. if (dev->puts)
  132. dev->puts += gd->reloc_off;
  133. #endif
  134. dev->next = serial_devices;
  135. serial_devices = dev;
  136. }
  137. /**
  138. * serial_initialize() - Register all compiled-in serial port drivers
  139. *
  140. * This function registers all serial port drivers that are compiled
  141. * into the U-Boot binary with the serial core, thus making them
  142. * available to U-Boot to use. Lastly, this function assigns a default
  143. * serial port to the serial core. That serial port is then used as a
  144. * default output.
  145. */
  146. void serial_initialize(void)
  147. {
  148. mpc8xx_serial_initialize();
  149. ns16550_serial_initialize();
  150. pxa_serial_initialize();
  151. s3c24xx_serial_initialize();
  152. s5p_serial_initialize();
  153. mpc512x_serial_initialize();
  154. bfin_serial_initialize();
  155. bfin_jtag_initialize();
  156. uartlite_serial_initialize();
  157. zynq_serial_initalize();
  158. au1x00_serial_initialize();
  159. asc_serial_initialize();
  160. jz_serial_initialize();
  161. mpc5xx_serial_initialize();
  162. mpc8220_serial_initialize();
  163. mpc8260_scc_serial_initialize();
  164. mpc8260_smc_serial_initialize();
  165. mpc85xx_serial_initialize();
  166. iop480_serial_initialize();
  167. leon2_serial_initialize();
  168. leon3_serial_initialize();
  169. marvell_serial_initialize();
  170. amirix_serial_initialize();
  171. bmw_serial_initialize();
  172. cogent_serial_initialize();
  173. cpci750_serial_initialize();
  174. evb64260_serial_initialize();
  175. ml2_serial_initialize();
  176. sconsole_serial_initialize();
  177. p3mx_serial_initialize();
  178. altera_jtag_serial_initialize();
  179. altera_serial_initialize();
  180. atmel_serial_initialize();
  181. lpc32xx_serial_initialize();
  182. mcf_serial_initialize();
  183. ns9750_serial_initialize();
  184. oc_serial_initialize();
  185. s3c64xx_serial_initialize();
  186. sandbox_serial_initialize();
  187. clps7111_serial_initialize();
  188. imx_serial_initialize();
  189. ixp_serial_initialize();
  190. ks8695_serial_initialize();
  191. lh7a40x_serial_initialize();
  192. max3100_serial_initialize();
  193. mxc_serial_initialize();
  194. pl01x_serial_initialize();
  195. s3c44b0_serial_initialize();
  196. sa1100_serial_initialize();
  197. sh_serial_initialize();
  198. serial_assign(default_serial_console()->name);
  199. }
  200. /**
  201. * serial_stdio_init() - Register serial ports with STDIO core
  202. *
  203. * This function generates a proxy driver for each serial port driver.
  204. * These proxy drivers then register with the STDIO core, making the
  205. * serial drivers available as STDIO devices.
  206. */
  207. void serial_stdio_init(void)
  208. {
  209. struct stdio_dev dev;
  210. struct serial_device *s = serial_devices;
  211. while (s) {
  212. memset(&dev, 0, sizeof(dev));
  213. strcpy(dev.name, s->name);
  214. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  215. dev.start = s->start;
  216. dev.stop = s->stop;
  217. dev.putc = s->putc;
  218. dev.puts = s->puts;
  219. dev.getc = s->getc;
  220. dev.tstc = s->tstc;
  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. return get_current()->start();
  308. }
  309. /**
  310. * serial_setbrg() - Configure baud-rate of currently selected serial port
  311. *
  312. * This function configures the baud-rate of the currently selected
  313. * serial port. The baud-rate is retrieved from global data within
  314. * the serial port driver. This function uses the get_current() call
  315. * to determine which port is selected.
  316. *
  317. * Returns 0 on success, negative on error.
  318. */
  319. void serial_setbrg(void)
  320. {
  321. get_current()->setbrg();
  322. }
  323. /**
  324. * serial_getc() - Read character from currently selected serial port
  325. *
  326. * This function retrieves a character from currently selected serial
  327. * port. In case there is no character waiting on the serial port,
  328. * this function will block and wait for the character to appear. This
  329. * function uses the get_current() call to determine which port is
  330. * selected.
  331. *
  332. * Returns the character on success, negative on error.
  333. */
  334. int serial_getc(void)
  335. {
  336. return get_current()->getc();
  337. }
  338. /**
  339. * serial_tstc() - Test if data is available on currently selected serial port
  340. *
  341. * This function tests if one or more characters are available on
  342. * currently selected serial port. This function never blocks. This
  343. * function uses the get_current() call to determine which port is
  344. * selected.
  345. *
  346. * Returns positive if character is available, zero otherwise.
  347. */
  348. int serial_tstc(void)
  349. {
  350. return get_current()->tstc();
  351. }
  352. /**
  353. * serial_putc() - Output character via currently selected serial port
  354. * @c: Single character to be output from the serial port.
  355. *
  356. * This function outputs a character via currently selected serial
  357. * port. This character is passed to the serial port driver responsible
  358. * for controlling the hardware. The hardware may still be in process
  359. * of transmitting another character, therefore this function may block
  360. * for a short amount of time. This function uses the get_current()
  361. * call to determine which port is selected.
  362. */
  363. void serial_putc(const char c)
  364. {
  365. get_current()->putc(c);
  366. }
  367. /**
  368. * serial_puts() - Output string via currently selected serial port
  369. * @s: Zero-terminated string to be output from the serial port.
  370. *
  371. * This function outputs a zero-terminated string via currently
  372. * selected serial port. This function behaves as an accelerator
  373. * in case the hardware can queue multiple characters for transfer.
  374. * The whole string that is to be output is available to the function
  375. * implementing the hardware manipulation. Transmitting the whole
  376. * string may take some time, thus this function may block for some
  377. * amount of time. This function uses the get_current() call to
  378. * determine which port is selected.
  379. */
  380. void serial_puts(const char *s)
  381. {
  382. get_current()->puts(s);
  383. }
  384. /**
  385. * default_serial_puts() - Output string by calling serial_putc() in loop
  386. * @s: Zero-terminated string to be output from the serial port.
  387. *
  388. * This function outputs a zero-terminated string by calling serial_putc()
  389. * in a loop. Most drivers do not support queueing more than one byte for
  390. * transfer, thus this function precisely implements their serial_puts().
  391. *
  392. * To optimize the number of get_current() calls, this function only
  393. * calls get_current() once and then directly accesses the putc() call
  394. * of the &struct serial_device .
  395. */
  396. void default_serial_puts(const char *s)
  397. {
  398. struct serial_device *dev = get_current();
  399. while (*s)
  400. dev->putc(*s++);
  401. }
  402. #if CONFIG_POST & CONFIG_SYS_POST_UART
  403. static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
  404. /**
  405. * uart_post_test() - Test the currently selected serial port using POST
  406. * @flags: POST framework flags
  407. *
  408. * Do a loopback test of the currently selected serial port. This
  409. * function is only useful in the context of the POST testing framwork.
  410. * The serial port is firstly configured into loopback mode and then
  411. * characters are sent through it.
  412. *
  413. * Returns 0 on success, value otherwise.
  414. */
  415. /* Mark weak until post/cpu/.../uart.c migrate over */
  416. __weak
  417. int uart_post_test(int flags)
  418. {
  419. unsigned char c;
  420. int ret, saved_baud, b;
  421. struct serial_device *saved_dev, *s;
  422. bd_t *bd = gd->bd;
  423. /* Save current serial state */
  424. ret = 0;
  425. saved_dev = serial_current;
  426. saved_baud = bd->bi_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. bd->bi_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. bd->bi_baudrate = saved_baud;
  480. serial_reinit_all();
  481. serial_setbrg();
  482. return ret;
  483. }
  484. #endif