wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 2 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 3 | * |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | |
| 33 | #include <common.h> |
wdenk | 5f535fe | 2003-09-18 09:21:33 +0000 | [diff] [blame] | 34 | #include <watchdog.h> |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 35 | #include <serial.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | #include <asm/arch/pxa-regs.h> |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 37 | #include <asm/arch/regs-uart.h> |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 38 | #include <asm/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 42 | /* |
| 43 | * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can |
| 44 | * easily handle enabling of clock. |
| 45 | */ |
| 46 | #ifdef CONFIG_CPU_MONAHANS |
| 47 | #define UART_CLK_BASE CKENA_21_BTUART |
| 48 | #define UART_CLK_REG CKENA |
| 49 | #define BTUART_INDEX 0 |
| 50 | #define FFUART_INDEX 1 |
| 51 | #define STUART_INDEX 2 |
Marek Vasut | abc20ab | 2011-11-26 07:20:07 +0100 | [diff] [blame^] | 52 | #elif CONFIG_CPU_PXA25X |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 53 | #define UART_CLK_BASE (1 << 4) /* HWUART */ |
| 54 | #define UART_CLK_REG CKEN |
| 55 | #define HWUART_INDEX 0 |
| 56 | #define STUART_INDEX 1 |
| 57 | #define FFUART_INDEX 2 |
| 58 | #define BTUART_INDEX 3 |
| 59 | #else /* PXA27x */ |
| 60 | #define UART_CLK_BASE CKEN5_STUART |
| 61 | #define UART_CLK_REG CKEN |
| 62 | #define STUART_INDEX 0 |
| 63 | #define FFUART_INDEX 1 |
| 64 | #define BTUART_INDEX 2 |
| 65 | #endif |
| 66 | |
| 67 | /* |
| 68 | * Only PXA250 has HWUART, to avoid poluting the code with more macros, |
| 69 | * artificially introduce this. |
| 70 | */ |
Marek Vasut | abc20ab | 2011-11-26 07:20:07 +0100 | [diff] [blame^] | 71 | #ifndef CONFIG_CPU_PXA25X |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 72 | #define HWUART_INDEX 0xff |
| 73 | #endif |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 74 | |
| 75 | #ifndef CONFIG_SERIAL_MULTI |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 76 | #if defined(CONFIG_FFUART) |
Marcel Ziswiler | 2a4741d | 2007-10-19 00:25:33 +0200 | [diff] [blame] | 77 | #define UART_INDEX FFUART_INDEX |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 78 | #elif defined(CONFIG_BTUART) |
Marcel Ziswiler | 2a4741d | 2007-10-19 00:25:33 +0200 | [diff] [blame] | 79 | #define UART_INDEX BTUART_INDEX |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 80 | #elif defined(CONFIG_STUART) |
Marcel Ziswiler | 2a4741d | 2007-10-19 00:25:33 +0200 | [diff] [blame] | 81 | #define UART_INDEX STUART_INDEX |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 82 | #elif defined(CONFIG_HWUART) |
| 83 | #define UART_INDEX HWUART_INDEX |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 84 | #else |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 85 | #error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file." |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 86 | #endif |
| 87 | #endif |
| 88 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 89 | uint32_t pxa_uart_get_baud_divider(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | if (gd->baudrate == 1200) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 92 | return 768; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 93 | else if (gd->baudrate == 9600) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 94 | return 96; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | else if (gd->baudrate == 19200) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 96 | return 48; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | else if (gd->baudrate == 38400) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 98 | return 24; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | else if (gd->baudrate == 57600) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 100 | return 16; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | else if (gd->baudrate == 115200) |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 102 | return 8; |
| 103 | else /* Unsupported baudrate */ |
| 104 | return 0; |
| 105 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 107 | struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index) |
| 108 | { |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 109 | switch (uart_index) { |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 110 | case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE; |
| 111 | case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE; |
| 112 | case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE; |
| 113 | case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE; |
| 114 | default: |
| 115 | return NULL; |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 116 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 119 | void pxa_uart_toggle_clock(uint32_t uart_index, int enable) |
| 120 | { |
| 121 | uint32_t clk_reg, clk_offset, reg; |
| 122 | |
| 123 | clk_reg = UART_CLK_REG; |
| 124 | clk_offset = UART_CLK_BASE << uart_index; |
| 125 | |
| 126 | reg = readl(clk_reg); |
| 127 | |
| 128 | if (enable) |
| 129 | reg |= clk_offset; |
| 130 | else |
| 131 | reg &= ~clk_offset; |
| 132 | |
| 133 | writel(reg, clk_reg); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Enable clock and set baud rate, parity etc. |
| 138 | */ |
| 139 | void pxa_setbrg_dev(uint32_t uart_index) |
| 140 | { |
| 141 | uint32_t divider = 0; |
| 142 | struct pxa_uart_regs *uart_regs; |
| 143 | |
| 144 | divider = pxa_uart_get_baud_divider(); |
| 145 | if (!divider) |
| 146 | hang(); |
| 147 | |
| 148 | uart_regs = pxa_uart_index_to_regs(uart_index); |
| 149 | if (!uart_regs) |
| 150 | hang(); |
| 151 | |
| 152 | pxa_uart_toggle_clock(uart_index, 1); |
| 153 | |
| 154 | /* Disable interrupts and FIFOs */ |
| 155 | writel(0, &uart_regs->ier); |
| 156 | writel(0, &uart_regs->fcr); |
| 157 | |
| 158 | /* Set baud rate */ |
| 159 | writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr); |
| 160 | writel(divider & 0xff, &uart_regs->dll); |
| 161 | writel(divider >> 8, &uart_regs->dlh); |
| 162 | writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr); |
| 163 | |
| 164 | /* Enable UART */ |
| 165 | writel(IER_UUE, &uart_regs->ier); |
| 166 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Initialise the serial port with the given baudrate. The settings |
| 170 | * are always 8 data bits, no parity, 1 stop bit, no start bits. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | */ |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 172 | int pxa_init_dev(unsigned int uart_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | { |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 174 | pxa_setbrg_dev (uart_index); |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 175 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Output a single byte to the serial port. |
| 180 | */ |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 181 | void pxa_putc_dev(unsigned int uart_index, const char c) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | { |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 183 | struct pxa_uart_regs *uart_regs; |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 184 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 185 | uart_regs = pxa_uart_index_to_regs(uart_index); |
| 186 | if (!uart_regs) |
| 187 | hang(); |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 188 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 189 | while (!(readl(&uart_regs->lsr) & LSR_TEMT)) |
| 190 | WATCHDOG_RESET(); |
| 191 | writel(c, &uart_regs->thr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | |
| 193 | /* If \n, also do \r */ |
| 194 | if (c == '\n') |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 195 | pxa_putc_dev (uart_index,'\r'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 200 | * otherwise. When the function is succesfull, the character read is |
| 201 | * written into its argument c. |
| 202 | */ |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 203 | int pxa_tstc_dev(unsigned int uart_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | { |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 205 | struct pxa_uart_regs *uart_regs; |
| 206 | |
| 207 | uart_regs = pxa_uart_index_to_regs(uart_index); |
| 208 | if (!uart_regs) |
| 209 | return -1; |
| 210 | |
| 211 | return readl(&uart_regs->lsr) & LSR_DR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Read a single byte from the serial port. Returns 1 on success, 0 |
| 216 | * otherwise. When the function is succesfull, the character read is |
| 217 | * written into its argument c. |
| 218 | */ |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 219 | int pxa_getc_dev(unsigned int uart_index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | { |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 221 | struct pxa_uart_regs *uart_regs; |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 222 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 223 | uart_regs = pxa_uart_index_to_regs(uart_index); |
| 224 | if (!uart_regs) |
| 225 | return -1; |
| 226 | |
| 227 | while (!(readl(&uart_regs->lsr) & LSR_DR)) |
| 228 | WATCHDOG_RESET(); |
| 229 | return readl(&uart_regs->rbr) & 0xff; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 232 | void pxa_puts_dev(unsigned int uart_index, const char *s) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | { |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 234 | while (*s) |
| 235 | pxa_putc_dev(uart_index, *s++); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | } |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 237 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 238 | #define pxa_uart(uart, UART) \ |
| 239 | int uart##_init(void) \ |
| 240 | { \ |
| 241 | return pxa_init_dev(UART##_INDEX); \ |
| 242 | } \ |
| 243 | \ |
| 244 | void uart##_setbrg(void) \ |
| 245 | { \ |
| 246 | return pxa_setbrg_dev(UART##_INDEX); \ |
| 247 | } \ |
| 248 | \ |
| 249 | void uart##_putc(const char c) \ |
| 250 | { \ |
| 251 | return pxa_putc_dev(UART##_INDEX, c); \ |
| 252 | } \ |
| 253 | \ |
| 254 | void uart##_puts(const char *s) \ |
| 255 | { \ |
| 256 | return pxa_puts_dev(UART##_INDEX, s); \ |
| 257 | } \ |
| 258 | \ |
| 259 | int uart##_getc(void) \ |
| 260 | { \ |
| 261 | return pxa_getc_dev(UART##_INDEX); \ |
| 262 | } \ |
| 263 | \ |
| 264 | int uart##_tstc(void) \ |
| 265 | { \ |
| 266 | return pxa_tstc_dev(UART##_INDEX); \ |
| 267 | } \ |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 268 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 269 | #define pxa_uart_desc(uart) \ |
| 270 | struct serial_device serial_##uart##_device = \ |
| 271 | { \ |
| 272 | "serial_"#uart, \ |
| 273 | uart##_init, \ |
| 274 | NULL, \ |
| 275 | uart##_setbrg, \ |
| 276 | uart##_getc, \ |
| 277 | uart##_tstc, \ |
| 278 | uart##_putc, \ |
| 279 | uart##_puts, \ |
| 280 | }; |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 281 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 282 | #define pxa_uart_multi(uart, UART) \ |
| 283 | pxa_uart(uart, UART) \ |
| 284 | pxa_uart_desc(uart) |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 285 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 286 | #if defined(CONFIG_HWUART) |
| 287 | pxa_uart_multi(hwuart, HWUART) |
| 288 | #endif |
| 289 | #if defined(CONFIG_STUART) |
| 290 | pxa_uart_multi(stuart, STUART) |
| 291 | #endif |
| 292 | #if defined(CONFIG_FFUART) |
| 293 | pxa_uart_multi(ffuart, FFUART) |
| 294 | #endif |
| 295 | #if defined(CONFIG_BTUART) |
| 296 | pxa_uart_multi(btuart, BTUART) |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 297 | #endif |
| 298 | |
Marek Vasut | 237ce0f | 2011-08-28 03:35:13 +0200 | [diff] [blame] | 299 | #ifndef CONFIG_SERIAL_MULTI |
| 300 | pxa_uart(serial, UART) |
stefano babic | 80172c6 | 2007-08-30 22:57:04 +0200 | [diff] [blame] | 301 | #endif |