serial_pxa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <common.h>
  31. #include <watchdog.h>
  32. #include <serial.h>
  33. #include <asm/arch/pxa-regs.h>
  34. #include <asm/io.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #define FFUART_INDEX 0
  37. #define BTUART_INDEX 1
  38. #define STUART_INDEX 2
  39. #ifndef CONFIG_SERIAL_MULTI
  40. #if defined (CONFIG_FFUART)
  41. #define UART_INDEX FFUART_INDEX
  42. #elif defined (CONFIG_BTUART)
  43. #define UART_INDEX BTUART_INDEX
  44. #elif defined (CONFIG_STUART)
  45. #define UART_INDEX STUART_INDEX
  46. #else
  47. #error "Bad: you didn't configure serial ..."
  48. #endif
  49. #endif
  50. void pxa_setbrg_dev (unsigned int uart_index)
  51. {
  52. unsigned int quot = 0;
  53. if (gd->baudrate == 1200)
  54. quot = 768;
  55. else if (gd->baudrate == 9600)
  56. quot = 96;
  57. else if (gd->baudrate == 19200)
  58. quot = 48;
  59. else if (gd->baudrate == 38400)
  60. quot = 24;
  61. else if (gd->baudrate == 57600)
  62. quot = 16;
  63. else if (gd->baudrate == 115200)
  64. quot = 8;
  65. else
  66. hang ();
  67. switch (uart_index) {
  68. case FFUART_INDEX:
  69. #ifdef CONFIG_CPU_MONAHANS
  70. writel(readl(CKENA) | CKENA_22_FFUART, CKENA);
  71. #else
  72. writel(readl(CKEN) | CKEN6_FFUART, CKEN);
  73. #endif /* CONFIG_CPU_MONAHANS */
  74. writel(0, FFIER); /* Disable for now */
  75. writel(0, FFFCR); /* No fifos enabled */
  76. /* set baud rate */
  77. writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, FFLCR);
  78. writel(quot & 0xff, FFDLL);
  79. writel(quot >> 8, FFDLH);
  80. writel(LCR_WLS0 | LCR_WLS1, FFLCR);
  81. writel(IER_UUE, FFIER); /* Enable FFUART */
  82. break;
  83. case BTUART_INDEX:
  84. #ifdef CONFIG_CPU_MONAHANS
  85. writel(readl(CKENA) | CKENA_21_BTUART, CKENA);
  86. #else
  87. writel(readl(CKEN) | CKEN7_BTUART, CKEN);
  88. #endif /* CONFIG_CPU_MONAHANS */
  89. writel(0, BTIER);
  90. writel(0, BTFCR);
  91. /* set baud rate */
  92. writel(LCR_DLAB, BTLCR);
  93. writel(quot & 0xff, BTDLL);
  94. writel(quot >> 8, BTDLH);
  95. writel(LCR_WLS0 | LCR_WLS1, BTLCR);
  96. writel(IER_UUE, BTIER); /* Enable BFUART */
  97. break;
  98. case STUART_INDEX:
  99. #ifdef CONFIG_CPU_MONAHANS
  100. writel(readl(CKENA) | CKENA_23_STUART, CKENA);
  101. #else
  102. writel(readl(CKEN) | CKEN5_STUART, CKEN);
  103. #endif /* CONFIG_CPU_MONAHANS */
  104. writel(0, STIER);
  105. writel(0, STFCR);
  106. /* set baud rate */
  107. writel(LCR_DLAB, STLCR);
  108. writel(quot & 0xff, STDLL);
  109. writel(quot >> 8, STDLH);
  110. writel(LCR_WLS0 | LCR_WLS1, STLCR);
  111. writel(IER_UUE, STIER); /* Enable STUART */
  112. break;
  113. default:
  114. hang();
  115. }
  116. }
  117. /*
  118. * Initialise the serial port with the given baudrate. The settings
  119. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  120. *
  121. */
  122. int pxa_init_dev (unsigned int uart_index)
  123. {
  124. pxa_setbrg_dev (uart_index);
  125. return (0);
  126. }
  127. /*
  128. * Output a single byte to the serial port.
  129. */
  130. void pxa_putc_dev (unsigned int uart_index,const char c)
  131. {
  132. switch (uart_index) {
  133. case FFUART_INDEX:
  134. /* wait for room in the tx FIFO on FFUART */
  135. while ((readl(FFLSR) & LSR_TEMT) == 0)
  136. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  137. writel(c, FFTHR);
  138. break;
  139. case BTUART_INDEX:
  140. while ((readl(BTLSR) & LSR_TEMT) == 0)
  141. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  142. writel(c, BTTHR);
  143. break;
  144. case STUART_INDEX:
  145. while ((readl(STLSR) & LSR_TEMT) == 0)
  146. WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
  147. writel(c, STTHR);
  148. break;
  149. }
  150. /* If \n, also do \r */
  151. if (c == '\n')
  152. pxa_putc_dev (uart_index,'\r');
  153. }
  154. /*
  155. * Read a single byte from the serial port. Returns 1 on success, 0
  156. * otherwise. When the function is succesfull, the character read is
  157. * written into its argument c.
  158. */
  159. int pxa_tstc_dev (unsigned int uart_index)
  160. {
  161. switch (uart_index) {
  162. case FFUART_INDEX:
  163. return readl(FFLSR) & LSR_DR;
  164. case BTUART_INDEX:
  165. return readl(BTLSR) & LSR_DR;
  166. case STUART_INDEX:
  167. return readl(STLSR) & LSR_DR;
  168. }
  169. return -1;
  170. }
  171. /*
  172. * Read a single byte from the serial port. Returns 1 on success, 0
  173. * otherwise. When the function is succesfull, the character read is
  174. * written into its argument c.
  175. */
  176. int pxa_getc_dev (unsigned int uart_index)
  177. {
  178. switch (uart_index) {
  179. case FFUART_INDEX:
  180. while (!(readl(FFLSR) & LSR_DR))
  181. /* Reset HW Watchdog, if needed */
  182. WATCHDOG_RESET();
  183. return (char) readl(FFRBR) & 0xff;
  184. case BTUART_INDEX:
  185. while (!(readl(BTLSR) & LSR_DR))
  186. /* Reset HW Watchdog, if needed */
  187. WATCHDOG_RESET();
  188. return (char) readl(BTRBR) & 0xff;
  189. case STUART_INDEX:
  190. while (!(readl(STLSR) & LSR_DR))
  191. /* Reset HW Watchdog, if needed */
  192. WATCHDOG_RESET();
  193. return (char) readl(STRBR) & 0xff;
  194. }
  195. return -1;
  196. }
  197. void
  198. pxa_puts_dev (unsigned int uart_index,const char *s)
  199. {
  200. while (*s) {
  201. pxa_putc_dev (uart_index,*s++);
  202. }
  203. }
  204. #if defined (CONFIG_FFUART)
  205. static int ffuart_init(void)
  206. {
  207. return pxa_init_dev(FFUART_INDEX);
  208. }
  209. static void ffuart_setbrg(void)
  210. {
  211. return pxa_setbrg_dev(FFUART_INDEX);
  212. }
  213. static void ffuart_putc(const char c)
  214. {
  215. return pxa_putc_dev(FFUART_INDEX,c);
  216. }
  217. static void ffuart_puts(const char *s)
  218. {
  219. return pxa_puts_dev(FFUART_INDEX,s);
  220. }
  221. static int ffuart_getc(void)
  222. {
  223. return pxa_getc_dev(FFUART_INDEX);
  224. }
  225. static int ffuart_tstc(void)
  226. {
  227. return pxa_tstc_dev(FFUART_INDEX);
  228. }
  229. struct serial_device serial_ffuart_device =
  230. {
  231. "serial_ffuart",
  232. "PXA",
  233. ffuart_init,
  234. NULL,
  235. ffuart_setbrg,
  236. ffuart_getc,
  237. ffuart_tstc,
  238. ffuart_putc,
  239. ffuart_puts,
  240. };
  241. #endif
  242. #if defined (CONFIG_BTUART)
  243. static int btuart_init(void)
  244. {
  245. return pxa_init_dev(BTUART_INDEX);
  246. }
  247. static void btuart_setbrg(void)
  248. {
  249. return pxa_setbrg_dev(BTUART_INDEX);
  250. }
  251. static void btuart_putc(const char c)
  252. {
  253. return pxa_putc_dev(BTUART_INDEX,c);
  254. }
  255. static void btuart_puts(const char *s)
  256. {
  257. return pxa_puts_dev(BTUART_INDEX,s);
  258. }
  259. static int btuart_getc(void)
  260. {
  261. return pxa_getc_dev(BTUART_INDEX);
  262. }
  263. static int btuart_tstc(void)
  264. {
  265. return pxa_tstc_dev(BTUART_INDEX);
  266. }
  267. struct serial_device serial_btuart_device =
  268. {
  269. "serial_btuart",
  270. "PXA",
  271. btuart_init,
  272. NULL,
  273. btuart_setbrg,
  274. btuart_getc,
  275. btuart_tstc,
  276. btuart_putc,
  277. btuart_puts,
  278. };
  279. #endif
  280. #if defined (CONFIG_STUART)
  281. static int stuart_init(void)
  282. {
  283. return pxa_init_dev(STUART_INDEX);
  284. }
  285. static void stuart_setbrg(void)
  286. {
  287. return pxa_setbrg_dev(STUART_INDEX);
  288. }
  289. static void stuart_putc(const char c)
  290. {
  291. return pxa_putc_dev(STUART_INDEX,c);
  292. }
  293. static void stuart_puts(const char *s)
  294. {
  295. return pxa_puts_dev(STUART_INDEX,s);
  296. }
  297. static int stuart_getc(void)
  298. {
  299. return pxa_getc_dev(STUART_INDEX);
  300. }
  301. static int stuart_tstc(void)
  302. {
  303. return pxa_tstc_dev(STUART_INDEX);
  304. }
  305. struct serial_device serial_stuart_device =
  306. {
  307. "serial_stuart",
  308. "PXA",
  309. stuart_init,
  310. NULL,
  311. stuart_setbrg,
  312. stuart_getc,
  313. stuart_tstc,
  314. stuart_putc,
  315. stuart_puts,
  316. };
  317. #endif
  318. #ifndef CONFIG_SERIAL_MULTI
  319. inline int serial_init(void) {
  320. return (pxa_init_dev(UART_INDEX));
  321. }
  322. void serial_setbrg(void) {
  323. pxa_setbrg_dev(UART_INDEX);
  324. }
  325. int serial_getc(void) {
  326. return(pxa_getc_dev(UART_INDEX));
  327. }
  328. int serial_tstc(void) {
  329. return(pxa_tstc_dev(UART_INDEX));
  330. }
  331. void serial_putc(const char c) {
  332. pxa_putc_dev(UART_INDEX,c);
  333. }
  334. void serial_puts(const char *s) {
  335. pxa_puts_dev(UART_INDEX,s);
  336. }
  337. #endif /* CONFIG_SERIAL_MULTI */