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 * Modified to add driver model (DM) support
18 * (C) Copyright 2016 Marcel Ziswiler <marcel.ziswiler@toradex.com>
20 * SPDX-License-Identifier: GPL-2.0+
24 #include <asm/arch/pxa-regs.h>
25 #include <asm/arch/regs-uart.h>
28 #include <dm/platform_data/serial_pxa.h>
29 #include <linux/compiler.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 static uint32_t pxa_uart_get_baud_divider(int baudrate)
37 return 921600 / baudrate;
40 static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
42 uint32_t clk_reg, clk_offset, reg;
44 clk_reg = UART_CLK_REG;
45 clk_offset = UART_CLK_BASE << uart_index;
58 * Enable clock and set baud rate, parity etc.
60 void pxa_setbrg_common(struct pxa_uart_regs *uart_regs, int port, int baudrate)
62 uint32_t divider = pxa_uart_get_baud_divider(baudrate);
67 pxa_uart_toggle_clock(port, 1);
69 /* Disable interrupts and FIFOs */
70 writel(0, &uart_regs->ier);
71 writel(0, &uart_regs->fcr);
74 writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
75 writel(divider & 0xff, &uart_regs->dll);
76 writel(divider >> 8, &uart_regs->dlh);
77 writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
80 writel(IER_UUE, &uart_regs->ier);
83 #ifndef CONFIG_DM_SERIAL
84 static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
87 case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
88 case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
89 case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
90 case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
97 * Enable clock and set baud rate, parity etc.
99 void pxa_setbrg_dev(uint32_t uart_index)
101 struct pxa_uart_regs *uart_regs = pxa_uart_index_to_regs(uart_index);
103 panic("Failed getting UART registers\n");
105 pxa_setbrg_common(uart_regs, uart_index, gd->baudrate);
109 * Initialise the serial port with the given baudrate. The settings
110 * are always 8 data bits, no parity, 1 stop bit, no start bits.
112 int pxa_init_dev(unsigned int uart_index)
114 pxa_setbrg_dev(uart_index);
119 * Output a single byte to the serial port.
121 void pxa_putc_dev(unsigned int uart_index, const char c)
123 struct pxa_uart_regs *uart_regs;
125 /* If \n, also do \r */
127 pxa_putc_dev(uart_index, '\r');
129 uart_regs = pxa_uart_index_to_regs(uart_index);
133 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
135 writel(c, &uart_regs->thr);
139 * Read a single byte from the serial port. Returns 1 on success, 0
140 * otherwise. When the function is succesfull, the character read is
141 * written into its argument c.
143 int pxa_tstc_dev(unsigned int uart_index)
145 struct pxa_uart_regs *uart_regs;
147 uart_regs = pxa_uart_index_to_regs(uart_index);
151 return readl(&uart_regs->lsr) & LSR_DR;
155 * Read a single byte from the serial port. Returns 1 on success, 0
156 * otherwise. When the function is succesfull, the character read is
157 * written into its argument c.
159 int pxa_getc_dev(unsigned int uart_index)
161 struct pxa_uart_regs *uart_regs;
163 uart_regs = pxa_uart_index_to_regs(uart_index);
167 while (!(readl(&uart_regs->lsr) & LSR_DR))
169 return readl(&uart_regs->rbr) & 0xff;
172 void pxa_puts_dev(unsigned int uart_index, const char *s)
175 pxa_putc_dev(uart_index, *s++);
178 #define pxa_uart(uart, UART) \
179 int uart##_init(void) \
181 return pxa_init_dev(UART##_INDEX); \
184 void uart##_setbrg(void) \
186 return pxa_setbrg_dev(UART##_INDEX); \
189 void uart##_putc(const char c) \
191 return pxa_putc_dev(UART##_INDEX, c); \
194 void uart##_puts(const char *s) \
196 return pxa_puts_dev(UART##_INDEX, s); \
199 int uart##_getc(void) \
201 return pxa_getc_dev(UART##_INDEX); \
204 int uart##_tstc(void) \
206 return pxa_tstc_dev(UART##_INDEX); \
209 #define pxa_uart_desc(uart) \
210 struct serial_device serial_##uart##_device = \
212 .name = "serial_"#uart, \
213 .start = uart##_init, \
215 .setbrg = uart##_setbrg, \
216 .getc = uart##_getc, \
217 .tstc = uart##_tstc, \
218 .putc = uart##_putc, \
219 .puts = uart##_puts, \
222 #define pxa_uart_multi(uart, UART) \
223 pxa_uart(uart, UART) \
226 #if defined(CONFIG_HWUART)
227 pxa_uart_multi(hwuart, HWUART)
229 #if defined(CONFIG_STUART)
230 pxa_uart_multi(stuart, STUART)
232 #if defined(CONFIG_FFUART)
233 pxa_uart_multi(ffuart, FFUART)
235 #if defined(CONFIG_BTUART)
236 pxa_uart_multi(btuart, BTUART)
239 __weak struct serial_device *default_serial_console(void)
241 #if CONFIG_CONS_INDEX == 1
242 return &serial_hwuart_device;
243 #elif CONFIG_CONS_INDEX == 2
244 return &serial_stuart_device;
245 #elif CONFIG_CONS_INDEX == 3
246 return &serial_ffuart_device;
247 #elif CONFIG_CONS_INDEX == 4
248 return &serial_btuart_device;
250 #error "Bad CONFIG_CONS_INDEX."
254 void pxa_serial_initialize(void)
256 #if defined(CONFIG_FFUART)
257 serial_register(&serial_ffuart_device);
259 #if defined(CONFIG_BTUART)
260 serial_register(&serial_btuart_device);
262 #if defined(CONFIG_STUART)
263 serial_register(&serial_stuart_device);
266 #endif /* CONFIG_DM_SERIAL */
268 #ifdef CONFIG_DM_SERIAL
269 static int pxa_serial_probe(struct udevice *dev)
271 struct pxa_serial_platdata *plat = dev->platdata;
273 pxa_setbrg_common((struct pxa_uart_regs *)plat->base, plat->port,
278 static int pxa_serial_putc(struct udevice *dev, const char ch)
280 struct pxa_serial_platdata *plat = dev->platdata;
281 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
283 /* Wait for last character to go. */
284 if (!(readl(&uart_regs->lsr) & LSR_TEMT))
287 writel(ch, &uart_regs->thr);
292 static int pxa_serial_getc(struct udevice *dev)
294 struct pxa_serial_platdata *plat = dev->platdata;
295 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
297 /* Wait for a character to arrive. */
298 if (!(readl(&uart_regs->lsr) & LSR_DR))
301 return readl(&uart_regs->rbr) & 0xff;
304 int pxa_serial_setbrg(struct udevice *dev, int baudrate)
306 struct pxa_serial_platdata *plat = dev->platdata;
307 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
308 int port = plat->port;
310 pxa_setbrg_common(uart_regs, port, baudrate);
315 static int pxa_serial_pending(struct udevice *dev, bool input)
317 struct pxa_serial_platdata *plat = dev->platdata;
318 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
321 return readl(&uart_regs->lsr) & LSR_DR ? 1 : 0;
323 return readl(&uart_regs->lsr) & LSR_TEMT ? 0 : 1;
328 static const struct dm_serial_ops pxa_serial_ops = {
329 .putc = pxa_serial_putc,
330 .pending = pxa_serial_pending,
331 .getc = pxa_serial_getc,
332 .setbrg = pxa_serial_setbrg,
335 U_BOOT_DRIVER(serial_pxa) = {
336 .name = "serial_pxa",
338 .probe = pxa_serial_probe,
339 .ops = &pxa_serial_ops,
340 .flags = DM_FLAG_PRE_RELOC,
342 #endif /* CONFIG_DM_SERIAL */