serial_pxa.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <linux/compiler.h>
  39. DECLARE_GLOBAL_DATA_PTR;
  40. /*
  41. * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
  42. * easily handle enabling of clock.
  43. */
  44. #ifdef CONFIG_CPU_MONAHANS
  45. #define UART_CLK_BASE CKENA_21_BTUART
  46. #define UART_CLK_REG CKENA
  47. #define BTUART_INDEX 0
  48. #define FFUART_INDEX 1
  49. #define STUART_INDEX 2
  50. #elif CONFIG_CPU_PXA25X
  51. #define UART_CLK_BASE (1 << 4) /* HWUART */
  52. #define UART_CLK_REG CKEN
  53. #define HWUART_INDEX 0
  54. #define STUART_INDEX 1
  55. #define FFUART_INDEX 2
  56. #define BTUART_INDEX 3
  57. #else /* PXA27x */
  58. #define UART_CLK_BASE CKEN5_STUART
  59. #define UART_CLK_REG CKEN
  60. #define STUART_INDEX 0
  61. #define FFUART_INDEX 1
  62. #define BTUART_INDEX 2
  63. #endif
  64. /*
  65. * Only PXA250 has HWUART, to avoid poluting the code with more macros,
  66. * artificially introduce this.
  67. */
  68. #ifndef CONFIG_CPU_PXA25X
  69. #define HWUART_INDEX 0xff
  70. #endif
  71. static uint32_t pxa_uart_get_baud_divider(void)
  72. {
  73. if (gd->baudrate == 1200)
  74. return 768;
  75. else if (gd->baudrate == 9600)
  76. return 96;
  77. else if (gd->baudrate == 19200)
  78. return 48;
  79. else if (gd->baudrate == 38400)
  80. return 24;
  81. else if (gd->baudrate == 57600)
  82. return 16;
  83. else if (gd->baudrate == 115200)
  84. return 8;
  85. else /* Unsupported baudrate */
  86. return 0;
  87. }
  88. static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
  89. {
  90. switch (uart_index) {
  91. case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
  92. case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
  93. case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
  94. case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
  95. default:
  96. return NULL;
  97. }
  98. }
  99. static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
  100. {
  101. uint32_t clk_reg, clk_offset, reg;
  102. clk_reg = UART_CLK_REG;
  103. clk_offset = UART_CLK_BASE << uart_index;
  104. reg = readl(clk_reg);
  105. if (enable)
  106. reg |= clk_offset;
  107. else
  108. reg &= ~clk_offset;
  109. writel(reg, clk_reg);
  110. }
  111. /*
  112. * Enable clock and set baud rate, parity etc.
  113. */
  114. void pxa_setbrg_dev(uint32_t uart_index)
  115. {
  116. uint32_t divider = 0;
  117. struct pxa_uart_regs *uart_regs;
  118. divider = pxa_uart_get_baud_divider();
  119. if (!divider)
  120. hang();
  121. uart_regs = pxa_uart_index_to_regs(uart_index);
  122. if (!uart_regs)
  123. hang();
  124. pxa_uart_toggle_clock(uart_index, 1);
  125. /* Disable interrupts and FIFOs */
  126. writel(0, &uart_regs->ier);
  127. writel(0, &uart_regs->fcr);
  128. /* Set baud rate */
  129. writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
  130. writel(divider & 0xff, &uart_regs->dll);
  131. writel(divider >> 8, &uart_regs->dlh);
  132. writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
  133. /* Enable UART */
  134. writel(IER_UUE, &uart_regs->ier);
  135. }
  136. /*
  137. * Initialise the serial port with the given baudrate. The settings
  138. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  139. */
  140. int pxa_init_dev(unsigned int uart_index)
  141. {
  142. pxa_setbrg_dev (uart_index);
  143. return 0;
  144. }
  145. /*
  146. * Output a single byte to the serial port.
  147. */
  148. void pxa_putc_dev(unsigned int uart_index, const char c)
  149. {
  150. struct pxa_uart_regs *uart_regs;
  151. uart_regs = pxa_uart_index_to_regs(uart_index);
  152. if (!uart_regs)
  153. hang();
  154. while (!(readl(&uart_regs->lsr) & LSR_TEMT))
  155. WATCHDOG_RESET();
  156. writel(c, &uart_regs->thr);
  157. /* If \n, also do \r */
  158. if (c == '\n')
  159. pxa_putc_dev (uart_index,'\r');
  160. }
  161. /*
  162. * Read a single byte from the serial port. Returns 1 on success, 0
  163. * otherwise. When the function is succesfull, the character read is
  164. * written into its argument c.
  165. */
  166. int pxa_tstc_dev(unsigned int uart_index)
  167. {
  168. struct pxa_uart_regs *uart_regs;
  169. uart_regs = pxa_uart_index_to_regs(uart_index);
  170. if (!uart_regs)
  171. return -1;
  172. return readl(&uart_regs->lsr) & LSR_DR;
  173. }
  174. /*
  175. * Read a single byte from the serial port. Returns 1 on success, 0
  176. * otherwise. When the function is succesfull, the character read is
  177. * written into its argument c.
  178. */
  179. int pxa_getc_dev(unsigned int uart_index)
  180. {
  181. struct pxa_uart_regs *uart_regs;
  182. uart_regs = pxa_uart_index_to_regs(uart_index);
  183. if (!uart_regs)
  184. return -1;
  185. while (!(readl(&uart_regs->lsr) & LSR_DR))
  186. WATCHDOG_RESET();
  187. return readl(&uart_regs->rbr) & 0xff;
  188. }
  189. void pxa_puts_dev(unsigned int uart_index, const char *s)
  190. {
  191. while (*s)
  192. pxa_putc_dev(uart_index, *s++);
  193. }
  194. #define pxa_uart(uart, UART) \
  195. int uart##_init(void) \
  196. { \
  197. return pxa_init_dev(UART##_INDEX); \
  198. } \
  199. \
  200. void uart##_setbrg(void) \
  201. { \
  202. return pxa_setbrg_dev(UART##_INDEX); \
  203. } \
  204. \
  205. void uart##_putc(const char c) \
  206. { \
  207. return pxa_putc_dev(UART##_INDEX, c); \
  208. } \
  209. \
  210. void uart##_puts(const char *s) \
  211. { \
  212. return pxa_puts_dev(UART##_INDEX, s); \
  213. } \
  214. \
  215. int uart##_getc(void) \
  216. { \
  217. return pxa_getc_dev(UART##_INDEX); \
  218. } \
  219. \
  220. int uart##_tstc(void) \
  221. { \
  222. return pxa_tstc_dev(UART##_INDEX); \
  223. } \
  224. #define pxa_uart_desc(uart) \
  225. struct serial_device serial_##uart##_device = \
  226. { \
  227. .name = "serial_"#uart, \
  228. .start = uart##_init, \
  229. .stop = NULL, \
  230. .setbrg = uart##_setbrg, \
  231. .getc = uart##_getc, \
  232. .tstc = uart##_tstc, \
  233. .putc = uart##_putc, \
  234. .puts = uart##_puts, \
  235. };
  236. #define pxa_uart_multi(uart, UART) \
  237. pxa_uart(uart, UART) \
  238. pxa_uart_desc(uart)
  239. #if defined(CONFIG_HWUART)
  240. pxa_uart_multi(hwuart, HWUART)
  241. #endif
  242. #if defined(CONFIG_STUART)
  243. pxa_uart_multi(stuart, STUART)
  244. #endif
  245. #if defined(CONFIG_FFUART)
  246. pxa_uart_multi(ffuart, FFUART)
  247. #endif
  248. #if defined(CONFIG_BTUART)
  249. pxa_uart_multi(btuart, BTUART)
  250. #endif
  251. __weak struct serial_device *default_serial_console(void)
  252. {
  253. #if CONFIG_CONS_INDEX == 1
  254. return &serial_hwuart_device;
  255. #elif CONFIG_CONS_INDEX == 2
  256. return &serial_stuart_device;
  257. #elif CONFIG_CONS_INDEX == 3
  258. return &serial_ffuart_device;
  259. #elif CONFIG_CONS_INDEX == 4
  260. return &serial_btuart_device;
  261. #else
  262. #error "Bad CONFIG_CONS_INDEX."
  263. #endif
  264. }
  265. void pxa_serial_initialize(void)
  266. {
  267. #if defined(CONFIG_FFUART)
  268. serial_register(&serial_ffuart_device);
  269. #endif
  270. #if defined(CONFIG_BTUART)
  271. serial_register(&serial_btuart_device);
  272. #endif
  273. #if defined(CONFIG_STUART)
  274. serial_register(&serial_stuart_device);
  275. #endif
  276. }