2 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Alex Zuepke <azu@sysgo.de>
15 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
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.
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.
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
36 #include <asm/arch/pxa-regs.h>
37 #include <asm/arch/regs-uart.h>
40 DECLARE_GLOBAL_DATA_PTR;
43 * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
44 * easily handle enabling of clock.
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
52 #elif CONFIG_CPU_PXA25X
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
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
68 * Only PXA250 has HWUART, to avoid poluting the code with more macros,
69 * artificially introduce this.
71 #ifndef CONFIG_CPU_PXA25X
72 #define HWUART_INDEX 0xff
75 #ifndef CONFIG_SERIAL_MULTI
76 #if defined(CONFIG_FFUART)
77 #define UART_INDEX FFUART_INDEX
78 #elif defined(CONFIG_BTUART)
79 #define UART_INDEX BTUART_INDEX
80 #elif defined(CONFIG_STUART)
81 #define UART_INDEX STUART_INDEX
82 #elif defined(CONFIG_HWUART)
83 #define UART_INDEX HWUART_INDEX
85 #error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file."
89 uint32_t pxa_uart_get_baud_divider(void)
91 if (gd->baudrate == 1200)
93 else if (gd->baudrate == 9600)
95 else if (gd->baudrate == 19200)
97 else if (gd->baudrate == 38400)
99 else if (gd->baudrate == 57600)
101 else if (gd->baudrate == 115200)
103 else /* Unsupported baudrate */
107 struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
109 switch (uart_index) {
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;
119 void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
121 uint32_t clk_reg, clk_offset, reg;
123 clk_reg = UART_CLK_REG;
124 clk_offset = UART_CLK_BASE << uart_index;
126 reg = readl(clk_reg);
133 writel(reg, clk_reg);
137 * Enable clock and set baud rate, parity etc.
139 void pxa_setbrg_dev(uint32_t uart_index)
141 uint32_t divider = 0;
142 struct pxa_uart_regs *uart_regs;
144 divider = pxa_uart_get_baud_divider();
148 uart_regs = pxa_uart_index_to_regs(uart_index);
152 pxa_uart_toggle_clock(uart_index, 1);
154 /* Disable interrupts and FIFOs */
155 writel(0, &uart_regs->ier);
156 writel(0, &uart_regs->fcr);
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);
165 writel(IER_UUE, &uart_regs->ier);
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.
172 int pxa_init_dev(unsigned int uart_index)
174 pxa_setbrg_dev (uart_index);
179 * Output a single byte to the serial port.
181 void pxa_putc_dev(unsigned int uart_index, const char c)
183 struct pxa_uart_regs *uart_regs;
185 uart_regs = pxa_uart_index_to_regs(uart_index);
189 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
191 writel(c, &uart_regs->thr);
193 /* If \n, also do \r */
195 pxa_putc_dev (uart_index,'\r');
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.
203 int pxa_tstc_dev(unsigned int uart_index)
205 struct pxa_uart_regs *uart_regs;
207 uart_regs = pxa_uart_index_to_regs(uart_index);
211 return readl(&uart_regs->lsr) & LSR_DR;
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.
219 int pxa_getc_dev(unsigned int uart_index)
221 struct pxa_uart_regs *uart_regs;
223 uart_regs = pxa_uart_index_to_regs(uart_index);
227 while (!(readl(&uart_regs->lsr) & LSR_DR))
229 return readl(&uart_regs->rbr) & 0xff;
232 void pxa_puts_dev(unsigned int uart_index, const char *s)
235 pxa_putc_dev(uart_index, *s++);
238 #define pxa_uart(uart, UART) \
239 int uart##_init(void) \
241 return pxa_init_dev(UART##_INDEX); \
244 void uart##_setbrg(void) \
246 return pxa_setbrg_dev(UART##_INDEX); \
249 void uart##_putc(const char c) \
251 return pxa_putc_dev(UART##_INDEX, c); \
254 void uart##_puts(const char *s) \
256 return pxa_puts_dev(UART##_INDEX, s); \
259 int uart##_getc(void) \
261 return pxa_getc_dev(UART##_INDEX); \
264 int uart##_tstc(void) \
266 return pxa_tstc_dev(UART##_INDEX); \
269 #define pxa_uart_desc(uart) \
270 struct serial_device serial_##uart##_device = \
272 .name = "serial_"#uart, \
273 .start = uart##_init, \
275 .setbrg = uart##_setbrg, \
276 .getc = uart##_getc, \
277 .tstc = uart##_tstc, \
278 .putc = uart##_putc, \
279 .puts = uart##_puts, \
282 #define pxa_uart_multi(uart, UART) \
283 pxa_uart(uart, UART) \
286 #if defined(CONFIG_HWUART)
287 pxa_uart_multi(hwuart, HWUART)
289 #if defined(CONFIG_STUART)
290 pxa_uart_multi(stuart, STUART)
292 #if defined(CONFIG_FFUART)
293 pxa_uart_multi(ffuart, FFUART)
295 #if defined(CONFIG_BTUART)
296 pxa_uart_multi(btuart, BTUART)
299 #ifndef CONFIG_SERIAL_MULTI
300 pxa_uart(serial, UART)