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 * SPDX-License-Identifier: GPL-2.0+
21 #include <asm/arch/pxa-regs.h>
22 #include <asm/arch/regs-uart.h>
24 #include <linux/compiler.h>
28 DECLARE_GLOBAL_DATA_PTR;
31 * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
32 * easily handle enabling of clock.
34 #ifdef CONFIG_CPU_MONAHANS
35 #define UART_CLK_BASE CKENA_21_BTUART
36 #define UART_CLK_REG CKENA
37 #define BTUART_INDEX 0
38 #define FFUART_INDEX 1
39 #define STUART_INDEX 2
40 #elif CONFIG_CPU_PXA25X
41 #define UART_CLK_BASE (1 << 4) /* HWUART */
42 #define UART_CLK_REG CKEN
43 #define HWUART_INDEX 0
44 #define STUART_INDEX 1
45 #define FFUART_INDEX 2
46 #define BTUART_INDEX 3
48 #define UART_CLK_BASE CKEN5_STUART
49 #define UART_CLK_REG CKEN
50 #define STUART_INDEX 0
51 #define FFUART_INDEX 1
52 #define BTUART_INDEX 2
56 * Only PXA250 has HWUART, to avoid poluting the code with more macros,
57 * artificially introduce this.
59 #ifndef CONFIG_CPU_PXA25X
60 #define HWUART_INDEX 0xff
63 static uint32_t pxa_uart_get_baud_divider(void)
65 if (gd->baudrate == 1200)
67 else if (gd->baudrate == 9600)
69 else if (gd->baudrate == 19200)
71 else if (gd->baudrate == 38400)
73 else if (gd->baudrate == 57600)
75 else if (gd->baudrate == 115200)
77 else /* Unsupported baudrate */
81 static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
84 case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
85 case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
86 case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
87 case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
93 static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
95 uint32_t clk_reg, clk_offset, reg;
97 clk_reg = UART_CLK_REG;
98 clk_offset = UART_CLK_BASE << uart_index;
100 reg = readl(clk_reg);
107 writel(reg, clk_reg);
111 * Enable clock and set baud rate, parity etc.
113 void pxa_setbrg_dev(uint32_t uart_index)
115 uint32_t divider = 0;
116 struct pxa_uart_regs *uart_regs;
118 divider = pxa_uart_get_baud_divider();
122 uart_regs = pxa_uart_index_to_regs(uart_index);
126 pxa_uart_toggle_clock(uart_index, 1);
128 /* Disable interrupts and FIFOs */
129 writel(0, &uart_regs->ier);
130 writel(0, &uart_regs->fcr);
133 writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
134 writel(divider & 0xff, &uart_regs->dll);
135 writel(divider >> 8, &uart_regs->dlh);
136 writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
139 writel(IER_UUE, &uart_regs->ier);
143 * Initialise the serial port with the given baudrate. The settings
144 * are always 8 data bits, no parity, 1 stop bit, no start bits.
146 int pxa_init_dev(unsigned int uart_index)
148 pxa_setbrg_dev (uart_index);
153 * Output a single byte to the serial port.
155 void pxa_putc_dev(unsigned int uart_index, const char c)
157 struct pxa_uart_regs *uart_regs;
159 /* If \n, also do \r */
161 pxa_putc_dev(uart_index, '\r');
163 uart_regs = pxa_uart_index_to_regs(uart_index);
167 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
169 writel(c, &uart_regs->thr);
173 * Read a single byte from the serial port. Returns 1 on success, 0
174 * otherwise. When the function is succesfull, the character read is
175 * written into its argument c.
177 int pxa_tstc_dev(unsigned int uart_index)
179 struct pxa_uart_regs *uart_regs;
181 uart_regs = pxa_uart_index_to_regs(uart_index);
185 return readl(&uart_regs->lsr) & LSR_DR;
189 * Read a single byte from the serial port. Returns 1 on success, 0
190 * otherwise. When the function is succesfull, the character read is
191 * written into its argument c.
193 int pxa_getc_dev(unsigned int uart_index)
195 struct pxa_uart_regs *uart_regs;
197 uart_regs = pxa_uart_index_to_regs(uart_index);
201 while (!(readl(&uart_regs->lsr) & LSR_DR))
203 return readl(&uart_regs->rbr) & 0xff;
206 void pxa_puts_dev(unsigned int uart_index, const char *s)
209 pxa_putc_dev(uart_index, *s++);
212 #define pxa_uart(uart, UART) \
213 int uart##_init(void) \
215 return pxa_init_dev(UART##_INDEX); \
218 void uart##_setbrg(void) \
220 return pxa_setbrg_dev(UART##_INDEX); \
223 void uart##_putc(const char c) \
225 return pxa_putc_dev(UART##_INDEX, c); \
228 void uart##_puts(const char *s) \
230 return pxa_puts_dev(UART##_INDEX, s); \
233 int uart##_getc(void) \
235 return pxa_getc_dev(UART##_INDEX); \
238 int uart##_tstc(void) \
240 return pxa_tstc_dev(UART##_INDEX); \
243 #define pxa_uart_desc(uart) \
244 struct serial_device serial_##uart##_device = \
246 .name = "serial_"#uart, \
247 .start = uart##_init, \
249 .setbrg = uart##_setbrg, \
250 .getc = uart##_getc, \
251 .tstc = uart##_tstc, \
252 .putc = uart##_putc, \
253 .puts = uart##_puts, \
256 #define pxa_uart_multi(uart, UART) \
257 pxa_uart(uart, UART) \
260 #if defined(CONFIG_HWUART)
261 pxa_uart_multi(hwuart, HWUART)
263 #if defined(CONFIG_STUART)
264 pxa_uart_multi(stuart, STUART)
266 #if defined(CONFIG_FFUART)
267 pxa_uart_multi(ffuart, FFUART)
269 #if defined(CONFIG_BTUART)
270 pxa_uart_multi(btuart, BTUART)
273 __weak struct serial_device *default_serial_console(void)
275 #if CONFIG_CONS_INDEX == 1
276 return &serial_hwuart_device;
277 #elif CONFIG_CONS_INDEX == 2
278 return &serial_stuart_device;
279 #elif CONFIG_CONS_INDEX == 3
280 return &serial_ffuart_device;
281 #elif CONFIG_CONS_INDEX == 4
282 return &serial_btuart_device;
284 #error "Bad CONFIG_CONS_INDEX."
288 void pxa_serial_initialize(void)
290 #if defined(CONFIG_FFUART)
291 serial_register(&serial_ffuart_device);
293 #if defined(CONFIG_BTUART)
294 serial_register(&serial_btuart_device);
296 #if defined(CONFIG_STUART)
297 serial_register(&serial_stuart_device);