1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Alex Zuepke <azu@sysgo.de>
16 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
18 * Modified to add driver model (DM) support
19 * (C) Copyright 2016 Marcel Ziswiler <marcel.ziswiler@toradex.com>
24 #include <asm/arch/pxa-regs.h>
25 #include <asm/arch/regs-uart.h>
26 #include <asm/global_data.h>
29 #include <dm/platform_data/serial_pxa.h>
30 #include <linux/compiler.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 static uint32_t pxa_uart_get_baud_divider(int baudrate)
38 return 921600 / baudrate;
41 static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
43 uint32_t clk_reg, clk_offset, reg;
45 clk_reg = UART_CLK_REG;
46 clk_offset = UART_CLK_BASE << uart_index;
59 * Enable clock and set baud rate, parity etc.
61 void pxa_setbrg_common(struct pxa_uart_regs *uart_regs, int port, int baudrate)
63 uint32_t divider = pxa_uart_get_baud_divider(baudrate);
68 pxa_uart_toggle_clock(port, 1);
70 /* Disable interrupts and FIFOs */
71 writel(0, &uart_regs->ier);
72 writel(0, &uart_regs->fcr);
75 writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
76 writel(divider & 0xff, &uart_regs->dll);
77 writel(divider >> 8, &uart_regs->dlh);
78 writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
81 writel(IER_UUE, &uart_regs->ier);
84 #ifndef CONFIG_DM_SERIAL
85 static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
88 case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
89 case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
90 case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
91 case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
98 * Enable clock and set baud rate, parity etc.
100 void pxa_setbrg_dev(uint32_t uart_index)
102 struct pxa_uart_regs *uart_regs = pxa_uart_index_to_regs(uart_index);
104 panic("Failed getting UART registers\n");
106 pxa_setbrg_common(uart_regs, uart_index, gd->baudrate);
110 * Initialise the serial port with the given baudrate. The settings
111 * are always 8 data bits, no parity, 1 stop bit, no start bits.
113 int pxa_init_dev(unsigned int uart_index)
115 pxa_setbrg_dev(uart_index);
120 * Output a single byte to the serial port.
122 void pxa_putc_dev(unsigned int uart_index, const char c)
124 struct pxa_uart_regs *uart_regs;
126 /* If \n, also do \r */
128 pxa_putc_dev(uart_index, '\r');
130 uart_regs = pxa_uart_index_to_regs(uart_index);
134 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
136 writel(c, &uart_regs->thr);
140 * Read a single byte from the serial port. Returns 1 on success, 0
141 * otherwise. When the function is succesfull, the character read is
142 * written into its argument c.
144 int pxa_tstc_dev(unsigned int uart_index)
146 struct pxa_uart_regs *uart_regs;
148 uart_regs = pxa_uart_index_to_regs(uart_index);
152 return readl(&uart_regs->lsr) & LSR_DR;
156 * Read a single byte from the serial port. Returns 1 on success, 0
157 * otherwise. When the function is succesfull, the character read is
158 * written into its argument c.
160 int pxa_getc_dev(unsigned int uart_index)
162 struct pxa_uart_regs *uart_regs;
164 uart_regs = pxa_uart_index_to_regs(uart_index);
168 while (!(readl(&uart_regs->lsr) & LSR_DR))
170 return readl(&uart_regs->rbr) & 0xff;
173 void pxa_puts_dev(unsigned int uart_index, const char *s)
176 pxa_putc_dev(uart_index, *s++);
179 #define pxa_uart(uart, UART) \
180 int uart##_init(void) \
182 return pxa_init_dev(UART##_INDEX); \
185 void uart##_setbrg(void) \
187 return pxa_setbrg_dev(UART##_INDEX); \
190 void uart##_putc(const char c) \
192 return pxa_putc_dev(UART##_INDEX, c); \
195 void uart##_puts(const char *s) \
197 return pxa_puts_dev(UART##_INDEX, s); \
200 int uart##_getc(void) \
202 return pxa_getc_dev(UART##_INDEX); \
205 int uart##_tstc(void) \
207 return pxa_tstc_dev(UART##_INDEX); \
210 #define pxa_uart_desc(uart) \
211 struct serial_device serial_##uart##_device = \
213 .name = "serial_"#uart, \
214 .start = uart##_init, \
216 .setbrg = uart##_setbrg, \
217 .getc = uart##_getc, \
218 .tstc = uart##_tstc, \
219 .putc = uart##_putc, \
220 .puts = uart##_puts, \
223 #define pxa_uart_multi(uart, UART) \
224 pxa_uart(uart, UART) \
227 #if defined(CONFIG_HWUART)
228 pxa_uart_multi(hwuart, HWUART)
230 #if defined(CONFIG_STUART)
231 pxa_uart_multi(stuart, STUART)
233 #if defined(CONFIG_FFUART)
234 pxa_uart_multi(ffuart, FFUART)
236 #if defined(CONFIG_BTUART)
237 pxa_uart_multi(btuart, BTUART)
240 __weak struct serial_device *default_serial_console(void)
242 #if CONFIG_CONS_INDEX == 1
243 return &serial_hwuart_device;
244 #elif CONFIG_CONS_INDEX == 2
245 return &serial_stuart_device;
246 #elif CONFIG_CONS_INDEX == 3
247 return &serial_ffuart_device;
248 #elif CONFIG_CONS_INDEX == 4
249 return &serial_btuart_device;
251 #error "Bad CONFIG_CONS_INDEX."
255 void pxa_serial_initialize(void)
257 #if defined(CONFIG_FFUART)
258 serial_register(&serial_ffuart_device);
260 #if defined(CONFIG_BTUART)
261 serial_register(&serial_btuart_device);
263 #if defined(CONFIG_STUART)
264 serial_register(&serial_stuart_device);
267 #endif /* CONFIG_DM_SERIAL */
269 #ifdef CONFIG_DM_SERIAL
270 static int pxa_serial_probe(struct udevice *dev)
272 struct pxa_serial_plat *plat = dev_get_plat(dev);
274 pxa_setbrg_common((struct pxa_uart_regs *)plat->base, plat->port,
279 static int pxa_serial_putc(struct udevice *dev, const char ch)
281 struct pxa_serial_plat *plat = dev_get_plat(dev);
282 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
284 /* Wait for last character to go. */
285 if (!(readl(&uart_regs->lsr) & LSR_TEMT))
288 writel(ch, &uart_regs->thr);
293 static int pxa_serial_getc(struct udevice *dev)
295 struct pxa_serial_plat *plat = dev_get_plat(dev);
296 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
298 /* Wait for a character to arrive. */
299 if (!(readl(&uart_regs->lsr) & LSR_DR))
302 return readl(&uart_regs->rbr) & 0xff;
305 int pxa_serial_setbrg(struct udevice *dev, int baudrate)
307 struct pxa_serial_plat *plat = dev_get_plat(dev);
308 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
309 int port = plat->port;
311 pxa_setbrg_common(uart_regs, port, baudrate);
316 static int pxa_serial_pending(struct udevice *dev, bool input)
318 struct pxa_serial_plat *plat = dev_get_plat(dev);
319 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
322 return readl(&uart_regs->lsr) & LSR_DR ? 1 : 0;
324 return readl(&uart_regs->lsr) & LSR_TEMT ? 0 : 1;
329 static const struct dm_serial_ops pxa_serial_ops = {
330 .putc = pxa_serial_putc,
331 .pending = pxa_serial_pending,
332 .getc = pxa_serial_getc,
333 .setbrg = pxa_serial_setbrg,
336 U_BOOT_DRIVER(serial_pxa) = {
337 .name = "serial_pxa",
339 .probe = pxa_serial_probe,
340 .ops = &pxa_serial_ops,
341 .flags = DM_FLAG_PRE_RELOC,
343 #endif /* CONFIG_DM_SERIAL */