blob: 330fc127ecb3e03634a4193c16d33993e3047823 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Marek Vasutac737ff2011-08-28 03:35:13 +02003 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
4 *
wdenkc6097192002-11-03 00:24:07 +00005 * (C) Copyright 2002
6 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
7 *
8 * (C) Copyright 2002
9 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Marius Groeger <mgroeger@sysgo.de>
11 *
12 * (C) Copyright 2002
13 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14 * Alex Zuepke <azu@sysgo.de>
15 *
16 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
17 *
Marcel Ziswiler90392d02016-11-14 21:40:26 +010018 * Modified to add driver model (DM) support
19 * (C) Copyright 2016 Marcel Ziswiler <marcel.ziswiler@toradex.com>
wdenkc6097192002-11-03 00:24:07 +000020 */
21
22#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -070023#include <hang.h>
wdenkc6097192002-11-03 00:24:07 +000024#include <asm/arch/pxa-regs.h>
Marek Vasutac737ff2011-08-28 03:35:13 +020025#include <asm/arch/regs-uart.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060026#include <asm/global_data.h>
Marek Vasut2db1e962010-09-09 09:50:39 +020027#include <asm/io.h>
Marcel Ziswiler90392d02016-11-14 21:40:26 +010028#include <dm.h>
29#include <dm/platform_data/serial_pxa.h>
Marek Vasutbc53d142012-09-12 12:26:30 +020030#include <linux/compiler.h>
Marcel Ziswiler7cfcbee2015-08-16 04:16:29 +020031#include <serial.h>
32#include <watchdog.h>
wdenkc6097192002-11-03 00:24:07 +000033
Wolfgang Denk6405a152006-03-31 18:32:53 +020034DECLARE_GLOBAL_DATA_PTR;
35
Marcel Ziswiler90392d02016-11-14 21:40:26 +010036static uint32_t pxa_uart_get_baud_divider(int baudrate)
Marek Vasutac737ff2011-08-28 03:35:13 +020037{
Marcel Ziswiler90392d02016-11-14 21:40:26 +010038 return 921600 / baudrate;
Marek Vasutac737ff2011-08-28 03:35:13 +020039}
wdenkc6097192002-11-03 00:24:07 +000040
Marek Vasutbcf48722012-09-12 12:59:42 +020041static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
Marek Vasutac737ff2011-08-28 03:35:13 +020042{
43 uint32_t clk_reg, clk_offset, reg;
wdenkc6097192002-11-03 00:24:07 +000044
Marek Vasutac737ff2011-08-28 03:35:13 +020045 clk_reg = UART_CLK_REG;
46 clk_offset = UART_CLK_BASE << uart_index;
wdenkc6097192002-11-03 00:24:07 +000047
Marek Vasutac737ff2011-08-28 03:35:13 +020048 reg = readl(clk_reg);
wdenkc6097192002-11-03 00:24:07 +000049
Marek Vasutac737ff2011-08-28 03:35:13 +020050 if (enable)
51 reg |= clk_offset;
52 else
53 reg &= ~clk_offset;
wdenkb02744a2003-04-05 00:53:31 +000054
Marek Vasutac737ff2011-08-28 03:35:13 +020055 writel(reg, clk_reg);
56}
wdenkb02744a2003-04-05 00:53:31 +000057
Marek Vasutac737ff2011-08-28 03:35:13 +020058/*
59 * Enable clock and set baud rate, parity etc.
60 */
Marcel Ziswiler90392d02016-11-14 21:40:26 +010061void pxa_setbrg_common(struct pxa_uart_regs *uart_regs, int port, int baudrate)
Marek Vasutac737ff2011-08-28 03:35:13 +020062{
Marcel Ziswiler90392d02016-11-14 21:40:26 +010063 uint32_t divider = pxa_uart_get_baud_divider(baudrate);
Marek Vasutac737ff2011-08-28 03:35:13 +020064 if (!divider)
65 hang();
stefano babic5ab4f032007-08-30 22:57:04 +020066
wdenk5958f4a2003-09-18 09:21:33 +000067
Marcel Ziswiler90392d02016-11-14 21:40:26 +010068 pxa_uart_toggle_clock(port, 1);
wdenk5958f4a2003-09-18 09:21:33 +000069
Marek Vasutac737ff2011-08-28 03:35:13 +020070 /* Disable interrupts and FIFOs */
71 writel(0, &uart_regs->ier);
72 writel(0, &uart_regs->fcr);
wdenk5958f4a2003-09-18 09:21:33 +000073
Marek Vasutac737ff2011-08-28 03:35:13 +020074 /* Set baud rate */
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);
wdenk5958f4a2003-09-18 09:21:33 +000079
Marek Vasutac737ff2011-08-28 03:35:13 +020080 /* Enable UART */
81 writel(IER_UUE, &uart_regs->ier);
wdenkc6097192002-11-03 00:24:07 +000082}
83
Marcel Ziswiler90392d02016-11-14 21:40:26 +010084#ifndef CONFIG_DM_SERIAL
85static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
86{
87 switch (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;
92 default:
93 return NULL;
94 }
95}
96
97/*
98 * Enable clock and set baud rate, parity etc.
99 */
100void pxa_setbrg_dev(uint32_t uart_index)
101{
102 struct pxa_uart_regs *uart_regs = pxa_uart_index_to_regs(uart_index);
103 if (!uart_regs)
104 panic("Failed getting UART registers\n");
105
106 pxa_setbrg_common(uart_regs, uart_index, gd->baudrate);
107}
108
wdenkc6097192002-11-03 00:24:07 +0000109/*
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.
wdenkc6097192002-11-03 00:24:07 +0000112 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200113int pxa_init_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000114{
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100115 pxa_setbrg_dev(uart_index);
Marek Vasutac737ff2011-08-28 03:35:13 +0200116 return 0;
wdenkc6097192002-11-03 00:24:07 +0000117}
118
wdenkc6097192002-11-03 00:24:07 +0000119/*
120 * Output a single byte to the serial port.
121 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200122void pxa_putc_dev(unsigned int uart_index, const char c)
wdenkc6097192002-11-03 00:24:07 +0000123{
Marek Vasutac737ff2011-08-28 03:35:13 +0200124 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200125
Alison Wang23e06b52016-03-02 11:00:37 +0800126 /* If \n, also do \r */
127 if (c == '\n')
128 pxa_putc_dev(uart_index, '\r');
129
Marek Vasutac737ff2011-08-28 03:35:13 +0200130 uart_regs = pxa_uart_index_to_regs(uart_index);
131 if (!uart_regs)
132 hang();
stefano babic5ab4f032007-08-30 22:57:04 +0200133
Marek Vasutac737ff2011-08-28 03:35:13 +0200134 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
135 WATCHDOG_RESET();
136 writel(c, &uart_regs->thr);
wdenkc6097192002-11-03 00:24:07 +0000137}
138
139/*
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.
143 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200144int pxa_tstc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000145{
Marek Vasutac737ff2011-08-28 03:35:13 +0200146 struct pxa_uart_regs *uart_regs;
147
148 uart_regs = pxa_uart_index_to_regs(uart_index);
149 if (!uart_regs)
150 return -1;
151
152 return readl(&uart_regs->lsr) & LSR_DR;
wdenkc6097192002-11-03 00:24:07 +0000153}
154
155/*
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.
159 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200160int pxa_getc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000161{
Marek Vasutac737ff2011-08-28 03:35:13 +0200162 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200163
Marek Vasutac737ff2011-08-28 03:35:13 +0200164 uart_regs = pxa_uart_index_to_regs(uart_index);
165 if (!uart_regs)
166 return -1;
stefano babic5ab4f032007-08-30 22:57:04 +0200167
Marek Vasutac737ff2011-08-28 03:35:13 +0200168 while (!(readl(&uart_regs->lsr) & LSR_DR))
169 WATCHDOG_RESET();
170 return readl(&uart_regs->rbr) & 0xff;
stefano babic5ab4f032007-08-30 22:57:04 +0200171}
172
Marek Vasutac737ff2011-08-28 03:35:13 +0200173void pxa_puts_dev(unsigned int uart_index, const char *s)
stefano babic5ab4f032007-08-30 22:57:04 +0200174{
Marek Vasutac737ff2011-08-28 03:35:13 +0200175 while (*s)
176 pxa_putc_dev(uart_index, *s++);
stefano babic5ab4f032007-08-30 22:57:04 +0200177}
178
Marek Vasutac737ff2011-08-28 03:35:13 +0200179#define pxa_uart(uart, UART) \
180 int uart##_init(void) \
181 { \
182 return pxa_init_dev(UART##_INDEX); \
183 } \
184 \
185 void uart##_setbrg(void) \
186 { \
187 return pxa_setbrg_dev(UART##_INDEX); \
188 } \
189 \
190 void uart##_putc(const char c) \
191 { \
192 return pxa_putc_dev(UART##_INDEX, c); \
193 } \
194 \
195 void uart##_puts(const char *s) \
196 { \
197 return pxa_puts_dev(UART##_INDEX, s); \
198 } \
199 \
200 int uart##_getc(void) \
201 { \
202 return pxa_getc_dev(UART##_INDEX); \
203 } \
204 \
205 int uart##_tstc(void) \
206 { \
207 return pxa_tstc_dev(UART##_INDEX); \
208 } \
stefano babic5ab4f032007-08-30 22:57:04 +0200209
Marek Vasutac737ff2011-08-28 03:35:13 +0200210#define pxa_uart_desc(uart) \
211 struct serial_device serial_##uart##_device = \
212 { \
Marek Vasut5bcdf242012-09-09 18:48:28 +0200213 .name = "serial_"#uart, \
214 .start = uart##_init, \
215 .stop = NULL, \
216 .setbrg = uart##_setbrg, \
217 .getc = uart##_getc, \
218 .tstc = uart##_tstc, \
219 .putc = uart##_putc, \
220 .puts = uart##_puts, \
Marek Vasutac737ff2011-08-28 03:35:13 +0200221 };
stefano babic5ab4f032007-08-30 22:57:04 +0200222
Marek Vasutac737ff2011-08-28 03:35:13 +0200223#define pxa_uart_multi(uart, UART) \
224 pxa_uart(uart, UART) \
225 pxa_uart_desc(uart)
stefano babic5ab4f032007-08-30 22:57:04 +0200226
Marek Vasutac737ff2011-08-28 03:35:13 +0200227#if defined(CONFIG_HWUART)
228 pxa_uart_multi(hwuart, HWUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200229#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200230#if defined(CONFIG_STUART)
231 pxa_uart_multi(stuart, STUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200232#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200233#if defined(CONFIG_FFUART)
234 pxa_uart_multi(ffuart, FFUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200235#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200236#if defined(CONFIG_BTUART)
237 pxa_uart_multi(btuart, BTUART)
238#endif
stefano babic5ab4f032007-08-30 22:57:04 +0200239
Marek Vasutbc53d142012-09-12 12:26:30 +0200240__weak struct serial_device *default_serial_console(void)
241{
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;
250#else
251#error "Bad CONFIG_CONS_INDEX."
252#endif
253}
Marek Vasutf3442012012-09-12 13:57:58 +0200254
255void pxa_serial_initialize(void)
256{
257#if defined(CONFIG_FFUART)
258 serial_register(&serial_ffuart_device);
259#endif
260#if defined(CONFIG_BTUART)
261 serial_register(&serial_btuart_device);
262#endif
263#if defined(CONFIG_STUART)
264 serial_register(&serial_stuart_device);
265#endif
266}
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100267#endif /* CONFIG_DM_SERIAL */
268
269#ifdef CONFIG_DM_SERIAL
270static int pxa_serial_probe(struct udevice *dev)
271{
Simon Glass95588622020-12-22 19:30:28 -0700272 struct pxa_serial_plat *plat = dev_get_plat(dev);
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100273
274 pxa_setbrg_common((struct pxa_uart_regs *)plat->base, plat->port,
275 plat->baudrate);
276 return 0;
277}
278
279static int pxa_serial_putc(struct udevice *dev, const char ch)
280{
Simon Glass95588622020-12-22 19:30:28 -0700281 struct pxa_serial_plat *plat = dev_get_plat(dev);
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100282 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
283
284 /* Wait for last character to go. */
285 if (!(readl(&uart_regs->lsr) & LSR_TEMT))
286 return -EAGAIN;
287
288 writel(ch, &uart_regs->thr);
289
290 return 0;
291}
292
293static int pxa_serial_getc(struct udevice *dev)
294{
Simon Glass95588622020-12-22 19:30:28 -0700295 struct pxa_serial_plat *plat = dev_get_plat(dev);
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100296 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
297
298 /* Wait for a character to arrive. */
299 if (!(readl(&uart_regs->lsr) & LSR_DR))
300 return -EAGAIN;
301
302 return readl(&uart_regs->rbr) & 0xff;
303}
304
305int pxa_serial_setbrg(struct udevice *dev, int baudrate)
306{
Simon Glass95588622020-12-22 19:30:28 -0700307 struct pxa_serial_plat *plat = dev_get_plat(dev);
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100308 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
309 int port = plat->port;
310
311 pxa_setbrg_common(uart_regs, port, baudrate);
312
313 return 0;
314}
315
316static int pxa_serial_pending(struct udevice *dev, bool input)
317{
Simon Glass95588622020-12-22 19:30:28 -0700318 struct pxa_serial_plat *plat = dev_get_plat(dev);
Marcel Ziswiler90392d02016-11-14 21:40:26 +0100319 struct pxa_uart_regs *uart_regs = (struct pxa_uart_regs *)plat->base;
320
321 if (input)
322 return readl(&uart_regs->lsr) & LSR_DR ? 1 : 0;
323 else
324 return readl(&uart_regs->lsr) & LSR_TEMT ? 0 : 1;
325
326 return 0;
327}
328
329static 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,
334};
335
336U_BOOT_DRIVER(serial_pxa) = {
337 .name = "serial_pxa",
338 .id = UCLASS_SERIAL,
339 .probe = pxa_serial_probe,
340 .ops = &pxa_serial_ops,
341 .flags = DM_FLAG_PRE_RELOC,
342};
343#endif /* CONFIG_DM_SERIAL */