serial_pxa.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  3. *
  4. * (C) Copyright 2002
  5. * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
  6. *
  7. * (C) Copyright 2002
  8. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  9. * Marius Groeger <mgroeger@sysgo.de>
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Alex Zuepke <azu@sysgo.de>
  14. *
  15. * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. */
  32. #include <common.h>
  33. #include <watchdog.h>
  34. #include <serial.h>
  35. #include <asm/arch/pxa-regs.h>
  36. #include <asm/arch/regs-uart.h>
  37. #include <asm/io.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. /*
  40. * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
  41. * easily handle enabling of clock.
  42. */
  43. #ifdef CONFIG_CPU_MONAHANS
  44. #define UART_CLK_BASE CKENA_21_BTUART
  45. #define UART_CLK_REG CKENA
  46. #define BTUART_INDEX 0
  47. #define FFUART_INDEX 1
  48. #define STUART_INDEX 2
  49. #elif CONFIG_CPU_PXA25X
  50. #define UART_CLK_BASE (1 << 4) /* HWUART */
  51. #define UART_CLK_REG CKEN
  52. #define HWUART_INDEX 0
  53. #define STUART_INDEX 1
  54. #define FFUART_INDEX 2
  55. #define BTUART_INDEX 3
  56. #else /* PXA27x */
  57. #define UART_CLK_BASE CKEN5_STUART
  58. #define UART_CLK_REG CKEN
  59. #define STUART_INDEX 0
  60. #define FFUART_INDEX 1
  61. #define BTUART_INDEX 2
  62. #endif
  63. /*
  64. * Only PXA250 has HWUART, to avoid poluting the code with more macros,
  65. * artificially introduce this.
  66. */
  67. #ifndef CONFIG_CPU_PXA25X
  68. #define HWUART_INDEX 0xff
  69. #endif
  70. #ifndef CONFIG_SERIAL_MULTI
  71. #if defined(CONFIG_FFUART)
  72. #define UART_INDEX FFUART_INDEX
  73. #elif defined(CONFIG_BTUART)
  74. #define UART_INDEX BTUART_INDEX
  75. #elif defined(CONFIG_STUART)
  76. #define UART_INDEX STUART_INDEX
  77. #elif defined(CONFIG_HWUART)
  78. #define UART_INDEX HWUART_INDEX
  79. #else
  80. #error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file."
  81. #endif
  82. #endif
  83. uint32_t pxa_uart_get_baud_divider(void)
  84. {
  85. if (gd->baudrate == 1200)
  86. return 768;
  87. else if (gd->baudrate == 9600)
  88. return 96;
  89. else if (gd->baudrate == 19200)
  90. return 48;
  91. else if (gd->baudrate == 38400)
  92. return 24;
  93. else if (gd->baudrate == 57600)
  94. return 16;
  95. else if (gd->baudrate == 115200)
  96. return 8;
  97. else /* Unsupported baudrate */
  98. return 0;
  99. }
  100. struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
  101. {
  102. switch (uart_index) {
  103. case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
  104. case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
  105. case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
  106. case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
  107. default:
  108. return NULL;
  109. }
  110. }
  111. void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
  112. {
  113. uint32_t clk_reg, clk_offset, reg;
  114. clk_reg = UART_CLK_REG;
  115. clk_offset = UART_CLK_BASE << uart_index;
  116. reg = readl(clk_reg);
  117. if (enable)
  118. reg |= clk_offset;
  119. else
  120. reg &= ~clk_offset;
  121. writel(reg, clk_reg);
  122. }
  123. /*
  124. * Enable clock and set baud rate, parity etc.
  125. */
  126. void pxa_setbrg_dev(uint32_t uart_index)
  127. {
  128. uint32_t divider = 0;
  129. struct pxa_uart_regs *uart_regs;
  130. divider = pxa_uart_get_baud_divider();
  131. if (!divider)
  132. hang();
  133. uart_regs = pxa_uart_index_to_regs(uart_index);
  134. if (!uart_regs)
  135. hang();
  136. pxa_uart_toggle_clock(uart_index, 1);
  137. /* Disable interrupts and FIFOs */
  138. writel(0, &uart_regs->ier);
  139. writel(0, &uart_regs->fcr);
  140. /* Set baud rate */
  141. writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
  142. writel(divider & 0xff, &uart_regs->dll);
  143. writel(divider >> 8, &uart_regs->dlh);
  144. writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
  145. /* Enable UART */
  146. writel(IER_UUE, &uart_regs->ier);
  147. }
  148. /*
  149. * Initialise the serial port with the given baudrate. The settings
  150. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  151. */
  152. int pxa_init_dev(unsigned int uart_index)
  153. {
  154. pxa_setbrg_dev (uart_index);
  155. return 0;
  156. }
  157. /*
  158. * Output a single byte to the serial port.
  159. */
  160. void pxa_putc_dev(unsigned int uart_index, const char c)
  161. {
  162. struct pxa_uart_regs *uart_regs;
  163. uart_regs = pxa_uart_index_to_regs(uart_index);
  164. if (!uart_regs)
  165. hang();
  166. while (!(readl(&uart_regs->lsr) & LSR_TEMT))
  167. WATCHDOG_RESET();
  168. writel(c, &uart_regs->thr);
  169. /* If \n, also do \r */
  170. if (c == '\n')
  171. pxa_putc_dev (uart_index,'\r');
  172. }
  173. /*
  174. * Read a single byte from the serial port. Returns 1 on success, 0
  175. * otherwise. When the function is succesfull, the character read is
  176. * written into its argument c.
  177. */
  178. int pxa_tstc_dev(unsigned int uart_index)
  179. {
  180. struct pxa_uart_regs *uart_regs;
  181. uart_regs = pxa_uart_index_to_regs(uart_index);
  182. if (!uart_regs)
  183. return -1;
  184. return readl(&uart_regs->lsr) & LSR_DR;
  185. }
  186. /*
  187. * Read a single byte from the serial port. Returns 1 on success, 0
  188. * otherwise. When the function is succesfull, the character read is
  189. * written into its argument c.
  190. */
  191. int pxa_getc_dev(unsigned int uart_index)
  192. {
  193. struct pxa_uart_regs *uart_regs;
  194. uart_regs = pxa_uart_index_to_regs(uart_index);
  195. if (!uart_regs)
  196. return -1;
  197. while (!(readl(&uart_regs->lsr) & LSR_DR))
  198. WATCHDOG_RESET();
  199. return readl(&uart_regs->rbr) & 0xff;
  200. }
  201. void pxa_puts_dev(unsigned int uart_index, const char *s)
  202. {
  203. while (*s)
  204. pxa_putc_dev(uart_index, *s++);
  205. }
  206. #define pxa_uart(uart, UART) \
  207. int uart##_init(void) \
  208. { \
  209. return pxa_init_dev(UART##_INDEX); \
  210. } \
  211. \
  212. void uart##_setbrg(void) \
  213. { \
  214. return pxa_setbrg_dev(UART##_INDEX); \
  215. } \
  216. \
  217. void uart##_putc(const char c) \
  218. { \
  219. return pxa_putc_dev(UART##_INDEX, c); \
  220. } \
  221. \
  222. void uart##_puts(const char *s) \
  223. { \
  224. return pxa_puts_dev(UART##_INDEX, s); \
  225. } \
  226. \
  227. int uart##_getc(void) \
  228. { \
  229. return pxa_getc_dev(UART##_INDEX); \
  230. } \
  231. \
  232. int uart##_tstc(void) \
  233. { \
  234. return pxa_tstc_dev(UART##_INDEX); \
  235. } \
  236. #define pxa_uart_desc(uart) \
  237. struct serial_device serial_##uart##_device = \
  238. { \
  239. "serial_"#uart, \
  240. uart##_init, \
  241. NULL, \
  242. uart##_setbrg, \
  243. uart##_getc, \
  244. uart##_tstc, \
  245. uart##_putc, \
  246. uart##_puts, \
  247. };
  248. #define pxa_uart_multi(uart, UART) \
  249. pxa_uart(uart, UART) \
  250. pxa_uart_desc(uart)
  251. #if defined(CONFIG_HWUART)
  252. pxa_uart_multi(hwuart, HWUART)
  253. #endif
  254. #if defined(CONFIG_STUART)
  255. pxa_uart_multi(stuart, STUART)
  256. #endif
  257. #if defined(CONFIG_FFUART)
  258. pxa_uart_multi(ffuart, FFUART)
  259. #endif
  260. #if defined(CONFIG_BTUART)
  261. pxa_uart_multi(btuart, BTUART)
  262. #endif
  263. #ifndef CONFIG_SERIAL_MULTI
  264. pxa_uart(serial, UART)
  265. #endif