serial_pxa.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifndef CONFIG_SERIAL_MULTI
  72. #if defined(CONFIG_FFUART)
  73. #define UART_INDEX FFUART_INDEX
  74. #elif defined(CONFIG_BTUART)
  75. #define UART_INDEX BTUART_INDEX
  76. #elif defined(CONFIG_STUART)
  77. #define UART_INDEX STUART_INDEX
  78. #elif defined(CONFIG_HWUART)
  79. #define UART_INDEX HWUART_INDEX
  80. #else
  81. #error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file."
  82. #endif
  83. #endif
  84. uint32_t pxa_uart_get_baud_divider(void)
  85. {
  86. if (gd->baudrate == 1200)
  87. return 768;
  88. else if (gd->baudrate == 9600)
  89. return 96;
  90. else if (gd->baudrate == 19200)
  91. return 48;
  92. else if (gd->baudrate == 38400)
  93. return 24;
  94. else if (gd->baudrate == 57600)
  95. return 16;
  96. else if (gd->baudrate == 115200)
  97. return 8;
  98. else /* Unsupported baudrate */
  99. return 0;
  100. }
  101. struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
  102. {
  103. switch (uart_index) {
  104. case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
  105. case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
  106. case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
  107. case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
  108. default:
  109. return NULL;
  110. }
  111. }
  112. void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
  113. {
  114. uint32_t clk_reg, clk_offset, reg;
  115. clk_reg = UART_CLK_REG;
  116. clk_offset = UART_CLK_BASE << uart_index;
  117. reg = readl(clk_reg);
  118. if (enable)
  119. reg |= clk_offset;
  120. else
  121. reg &= ~clk_offset;
  122. writel(reg, clk_reg);
  123. }
  124. /*
  125. * Enable clock and set baud rate, parity etc.
  126. */
  127. void pxa_setbrg_dev(uint32_t uart_index)
  128. {
  129. uint32_t divider = 0;
  130. struct pxa_uart_regs *uart_regs;
  131. divider = pxa_uart_get_baud_divider();
  132. if (!divider)
  133. hang();
  134. uart_regs = pxa_uart_index_to_regs(uart_index);
  135. if (!uart_regs)
  136. hang();
  137. pxa_uart_toggle_clock(uart_index, 1);
  138. /* Disable interrupts and FIFOs */
  139. writel(0, &uart_regs->ier);
  140. writel(0, &uart_regs->fcr);
  141. /* Set baud rate */
  142. writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
  143. writel(divider & 0xff, &uart_regs->dll);
  144. writel(divider >> 8, &uart_regs->dlh);
  145. writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
  146. /* Enable UART */
  147. writel(IER_UUE, &uart_regs->ier);
  148. }
  149. /*
  150. * Initialise the serial port with the given baudrate. The settings
  151. * are always 8 data bits, no parity, 1 stop bit, no start bits.
  152. */
  153. int pxa_init_dev(unsigned int uart_index)
  154. {
  155. pxa_setbrg_dev (uart_index);
  156. return 0;
  157. }
  158. /*
  159. * Output a single byte to the serial port.
  160. */
  161. void pxa_putc_dev(unsigned int uart_index, const char c)
  162. {
  163. struct pxa_uart_regs *uart_regs;
  164. uart_regs = pxa_uart_index_to_regs(uart_index);
  165. if (!uart_regs)
  166. hang();
  167. while (!(readl(&uart_regs->lsr) & LSR_TEMT))
  168. WATCHDOG_RESET();
  169. writel(c, &uart_regs->thr);
  170. /* If \n, also do \r */
  171. if (c == '\n')
  172. pxa_putc_dev (uart_index,'\r');
  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_tstc_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. return readl(&uart_regs->lsr) & LSR_DR;
  186. }
  187. /*
  188. * Read a single byte from the serial port. Returns 1 on success, 0
  189. * otherwise. When the function is succesfull, the character read is
  190. * written into its argument c.
  191. */
  192. int pxa_getc_dev(unsigned int uart_index)
  193. {
  194. struct pxa_uart_regs *uart_regs;
  195. uart_regs = pxa_uart_index_to_regs(uart_index);
  196. if (!uart_regs)
  197. return -1;
  198. while (!(readl(&uart_regs->lsr) & LSR_DR))
  199. WATCHDOG_RESET();
  200. return readl(&uart_regs->rbr) & 0xff;
  201. }
  202. void pxa_puts_dev(unsigned int uart_index, const char *s)
  203. {
  204. while (*s)
  205. pxa_putc_dev(uart_index, *s++);
  206. }
  207. #define pxa_uart(uart, UART) \
  208. int uart##_init(void) \
  209. { \
  210. return pxa_init_dev(UART##_INDEX); \
  211. } \
  212. \
  213. void uart##_setbrg(void) \
  214. { \
  215. return pxa_setbrg_dev(UART##_INDEX); \
  216. } \
  217. \
  218. void uart##_putc(const char c) \
  219. { \
  220. return pxa_putc_dev(UART##_INDEX, c); \
  221. } \
  222. \
  223. void uart##_puts(const char *s) \
  224. { \
  225. return pxa_puts_dev(UART##_INDEX, s); \
  226. } \
  227. \
  228. int uart##_getc(void) \
  229. { \
  230. return pxa_getc_dev(UART##_INDEX); \
  231. } \
  232. \
  233. int uart##_tstc(void) \
  234. { \
  235. return pxa_tstc_dev(UART##_INDEX); \
  236. } \
  237. #define pxa_uart_desc(uart) \
  238. struct serial_device serial_##uart##_device = \
  239. { \
  240. .name = "serial_"#uart, \
  241. .start = uart##_init, \
  242. .stop = NULL, \
  243. .setbrg = uart##_setbrg, \
  244. .getc = uart##_getc, \
  245. .tstc = uart##_tstc, \
  246. .putc = uart##_putc, \
  247. .puts = uart##_puts, \
  248. };
  249. #define pxa_uart_multi(uart, UART) \
  250. pxa_uart(uart, UART) \
  251. pxa_uart_desc(uart)
  252. #if defined(CONFIG_HWUART)
  253. pxa_uart_multi(hwuart, HWUART)
  254. #endif
  255. #if defined(CONFIG_STUART)
  256. pxa_uart_multi(stuart, STUART)
  257. #endif
  258. #if defined(CONFIG_FFUART)
  259. pxa_uart_multi(ffuart, FFUART)
  260. #endif
  261. #if defined(CONFIG_BTUART)
  262. pxa_uart_multi(btuart, BTUART)
  263. #endif
  264. #ifndef CONFIG_SERIAL_MULTI
  265. pxa_uart(serial, UART)
  266. #else
  267. __weak struct serial_device *default_serial_console(void)
  268. {
  269. #if CONFIG_CONS_INDEX == 1
  270. return &serial_hwuart_device;
  271. #elif CONFIG_CONS_INDEX == 2
  272. return &serial_stuart_device;
  273. #elif CONFIG_CONS_INDEX == 3
  274. return &serial_ffuart_device;
  275. #elif CONFIG_CONS_INDEX == 4
  276. return &serial_btuart_device;
  277. #else
  278. #error "Bad CONFIG_CONS_INDEX."
  279. #endif
  280. }
  281. #endif