blob: d5140045bfbb54ee0d790418fe07b7cbb4655343 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Marek Vasutac737ff2011-08-28 03:35:13 +02002 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
3 *
wdenkc6097192002-11-03 00:24:07 +00004 * (C) Copyright 2002
5 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
6 *
7 * (C) Copyright 2002
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
10 *
11 * (C) Copyright 2002
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Alex Zuepke <azu@sysgo.de>
14 *
15 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
16 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000018 */
19
20#include <common.h>
wdenk5958f4a2003-09-18 09:21:33 +000021#include <watchdog.h>
stefano babic5ab4f032007-08-30 22:57:04 +020022#include <serial.h>
wdenkc6097192002-11-03 00:24:07 +000023#include <asm/arch/pxa-regs.h>
Marek Vasutac737ff2011-08-28 03:35:13 +020024#include <asm/arch/regs-uart.h>
Marek Vasut2db1e962010-09-09 09:50:39 +020025#include <asm/io.h>
Marek Vasutbc53d142012-09-12 12:26:30 +020026#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000027
Wolfgang Denk6405a152006-03-31 18:32:53 +020028DECLARE_GLOBAL_DATA_PTR;
29
Marek Vasutac737ff2011-08-28 03:35:13 +020030/*
31 * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
32 * easily handle enabling of clock.
33 */
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
Marek Vasut85cc88a2011-11-26 07:20:07 +010040#elif CONFIG_CPU_PXA25X
Marek Vasutac737ff2011-08-28 03:35:13 +020041#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
47#else /* PXA27x */
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
53#endif
54
55/*
56 * Only PXA250 has HWUART, to avoid poluting the code with more macros,
57 * artificially introduce this.
58 */
Marek Vasut85cc88a2011-11-26 07:20:07 +010059#ifndef CONFIG_CPU_PXA25X
Marek Vasutac737ff2011-08-28 03:35:13 +020060#define HWUART_INDEX 0xff
61#endif
stefano babic5ab4f032007-08-30 22:57:04 +020062
Marek Vasutbcf48722012-09-12 12:59:42 +020063static uint32_t pxa_uart_get_baud_divider(void)
wdenkc6097192002-11-03 00:24:07 +000064{
wdenkc6097192002-11-03 00:24:07 +000065 if (gd->baudrate == 1200)
Marek Vasutac737ff2011-08-28 03:35:13 +020066 return 768;
wdenkc6097192002-11-03 00:24:07 +000067 else if (gd->baudrate == 9600)
Marek Vasutac737ff2011-08-28 03:35:13 +020068 return 96;
wdenkc6097192002-11-03 00:24:07 +000069 else if (gd->baudrate == 19200)
Marek Vasutac737ff2011-08-28 03:35:13 +020070 return 48;
wdenkc6097192002-11-03 00:24:07 +000071 else if (gd->baudrate == 38400)
Marek Vasutac737ff2011-08-28 03:35:13 +020072 return 24;
wdenkc6097192002-11-03 00:24:07 +000073 else if (gd->baudrate == 57600)
Marek Vasutac737ff2011-08-28 03:35:13 +020074 return 16;
wdenkc6097192002-11-03 00:24:07 +000075 else if (gd->baudrate == 115200)
Marek Vasutac737ff2011-08-28 03:35:13 +020076 return 8;
77 else /* Unsupported baudrate */
78 return 0;
79}
wdenkc6097192002-11-03 00:24:07 +000080
Marek Vasutbcf48722012-09-12 12:59:42 +020081static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
Marek Vasutac737ff2011-08-28 03:35:13 +020082{
stefano babic5ab4f032007-08-30 22:57:04 +020083 switch (uart_index) {
Marek Vasutac737ff2011-08-28 03:35:13 +020084 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;
88 default:
89 return NULL;
90 }
91}
wdenkc6097192002-11-03 00:24:07 +000092
Marek Vasutbcf48722012-09-12 12:59:42 +020093static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
Marek Vasutac737ff2011-08-28 03:35:13 +020094{
95 uint32_t clk_reg, clk_offset, reg;
wdenkc6097192002-11-03 00:24:07 +000096
Marek Vasutac737ff2011-08-28 03:35:13 +020097 clk_reg = UART_CLK_REG;
98 clk_offset = UART_CLK_BASE << uart_index;
wdenkc6097192002-11-03 00:24:07 +000099
Marek Vasutac737ff2011-08-28 03:35:13 +0200100 reg = readl(clk_reg);
wdenkc6097192002-11-03 00:24:07 +0000101
Marek Vasutac737ff2011-08-28 03:35:13 +0200102 if (enable)
103 reg |= clk_offset;
104 else
105 reg &= ~clk_offset;
wdenkb02744a2003-04-05 00:53:31 +0000106
Marek Vasutac737ff2011-08-28 03:35:13 +0200107 writel(reg, clk_reg);
108}
wdenkb02744a2003-04-05 00:53:31 +0000109
Marek Vasutac737ff2011-08-28 03:35:13 +0200110/*
111 * Enable clock and set baud rate, parity etc.
112 */
113void pxa_setbrg_dev(uint32_t uart_index)
114{
115 uint32_t divider = 0;
116 struct pxa_uart_regs *uart_regs;
wdenkb02744a2003-04-05 00:53:31 +0000117
Marek Vasutac737ff2011-08-28 03:35:13 +0200118 divider = pxa_uart_get_baud_divider();
119 if (!divider)
120 hang();
stefano babic5ab4f032007-08-30 22:57:04 +0200121
Marek Vasutac737ff2011-08-28 03:35:13 +0200122 uart_regs = pxa_uart_index_to_regs(uart_index);
123 if (!uart_regs)
124 hang();
wdenk5958f4a2003-09-18 09:21:33 +0000125
Marek Vasutac737ff2011-08-28 03:35:13 +0200126 pxa_uart_toggle_clock(uart_index, 1);
wdenk5958f4a2003-09-18 09:21:33 +0000127
Marek Vasutac737ff2011-08-28 03:35:13 +0200128 /* Disable interrupts and FIFOs */
129 writel(0, &uart_regs->ier);
130 writel(0, &uart_regs->fcr);
wdenk5958f4a2003-09-18 09:21:33 +0000131
Marek Vasutac737ff2011-08-28 03:35:13 +0200132 /* Set baud rate */
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);
wdenk5958f4a2003-09-18 09:21:33 +0000137
Marek Vasutac737ff2011-08-28 03:35:13 +0200138 /* Enable UART */
139 writel(IER_UUE, &uart_regs->ier);
wdenkc6097192002-11-03 00:24:07 +0000140}
141
wdenkc6097192002-11-03 00:24:07 +0000142/*
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.
wdenkc6097192002-11-03 00:24:07 +0000145 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200146int pxa_init_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000147{
stefano babic5ab4f032007-08-30 22:57:04 +0200148 pxa_setbrg_dev (uart_index);
Marek Vasutac737ff2011-08-28 03:35:13 +0200149 return 0;
wdenkc6097192002-11-03 00:24:07 +0000150}
151
wdenkc6097192002-11-03 00:24:07 +0000152/*
153 * Output a single byte to the serial port.
154 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200155void pxa_putc_dev(unsigned int uart_index, const char c)
wdenkc6097192002-11-03 00:24:07 +0000156{
Marek Vasutac737ff2011-08-28 03:35:13 +0200157 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200158
Marek Vasutac737ff2011-08-28 03:35:13 +0200159 uart_regs = pxa_uart_index_to_regs(uart_index);
160 if (!uart_regs)
161 hang();
stefano babic5ab4f032007-08-30 22:57:04 +0200162
Marek Vasutac737ff2011-08-28 03:35:13 +0200163 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
164 WATCHDOG_RESET();
165 writel(c, &uart_regs->thr);
wdenkc6097192002-11-03 00:24:07 +0000166
167 /* If \n, also do \r */
168 if (c == '\n')
stefano babic5ab4f032007-08-30 22:57:04 +0200169 pxa_putc_dev (uart_index,'\r');
wdenkc6097192002-11-03 00:24:07 +0000170}
171
172/*
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.
176 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200177int pxa_tstc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000178{
Marek Vasutac737ff2011-08-28 03:35:13 +0200179 struct pxa_uart_regs *uart_regs;
180
181 uart_regs = pxa_uart_index_to_regs(uart_index);
182 if (!uart_regs)
183 return -1;
184
185 return readl(&uart_regs->lsr) & LSR_DR;
wdenkc6097192002-11-03 00:24:07 +0000186}
187
188/*
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.
192 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200193int pxa_getc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000194{
Marek Vasutac737ff2011-08-28 03:35:13 +0200195 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200196
Marek Vasutac737ff2011-08-28 03:35:13 +0200197 uart_regs = pxa_uart_index_to_regs(uart_index);
198 if (!uart_regs)
199 return -1;
stefano babic5ab4f032007-08-30 22:57:04 +0200200
Marek Vasutac737ff2011-08-28 03:35:13 +0200201 while (!(readl(&uart_regs->lsr) & LSR_DR))
202 WATCHDOG_RESET();
203 return readl(&uart_regs->rbr) & 0xff;
stefano babic5ab4f032007-08-30 22:57:04 +0200204}
205
Marek Vasutac737ff2011-08-28 03:35:13 +0200206void pxa_puts_dev(unsigned int uart_index, const char *s)
stefano babic5ab4f032007-08-30 22:57:04 +0200207{
Marek Vasutac737ff2011-08-28 03:35:13 +0200208 while (*s)
209 pxa_putc_dev(uart_index, *s++);
stefano babic5ab4f032007-08-30 22:57:04 +0200210}
211
Marek Vasutac737ff2011-08-28 03:35:13 +0200212#define pxa_uart(uart, UART) \
213 int uart##_init(void) \
214 { \
215 return pxa_init_dev(UART##_INDEX); \
216 } \
217 \
218 void uart##_setbrg(void) \
219 { \
220 return pxa_setbrg_dev(UART##_INDEX); \
221 } \
222 \
223 void uart##_putc(const char c) \
224 { \
225 return pxa_putc_dev(UART##_INDEX, c); \
226 } \
227 \
228 void uart##_puts(const char *s) \
229 { \
230 return pxa_puts_dev(UART##_INDEX, s); \
231 } \
232 \
233 int uart##_getc(void) \
234 { \
235 return pxa_getc_dev(UART##_INDEX); \
236 } \
237 \
238 int uart##_tstc(void) \
239 { \
240 return pxa_tstc_dev(UART##_INDEX); \
241 } \
stefano babic5ab4f032007-08-30 22:57:04 +0200242
Marek Vasutac737ff2011-08-28 03:35:13 +0200243#define pxa_uart_desc(uart) \
244 struct serial_device serial_##uart##_device = \
245 { \
Marek Vasut5bcdf242012-09-09 18:48:28 +0200246 .name = "serial_"#uart, \
247 .start = uart##_init, \
248 .stop = NULL, \
249 .setbrg = uart##_setbrg, \
250 .getc = uart##_getc, \
251 .tstc = uart##_tstc, \
252 .putc = uart##_putc, \
253 .puts = uart##_puts, \
Marek Vasutac737ff2011-08-28 03:35:13 +0200254 };
stefano babic5ab4f032007-08-30 22:57:04 +0200255
Marek Vasutac737ff2011-08-28 03:35:13 +0200256#define pxa_uart_multi(uart, UART) \
257 pxa_uart(uart, UART) \
258 pxa_uart_desc(uart)
stefano babic5ab4f032007-08-30 22:57:04 +0200259
Marek Vasutac737ff2011-08-28 03:35:13 +0200260#if defined(CONFIG_HWUART)
261 pxa_uart_multi(hwuart, HWUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200262#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200263#if defined(CONFIG_STUART)
264 pxa_uart_multi(stuart, STUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200265#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200266#if defined(CONFIG_FFUART)
267 pxa_uart_multi(ffuart, FFUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200268#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200269#if defined(CONFIG_BTUART)
270 pxa_uart_multi(btuart, BTUART)
271#endif
stefano babic5ab4f032007-08-30 22:57:04 +0200272
Marek Vasutbc53d142012-09-12 12:26:30 +0200273__weak struct serial_device *default_serial_console(void)
274{
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;
283#else
284#error "Bad CONFIG_CONS_INDEX."
285#endif
286}
Marek Vasutf3442012012-09-12 13:57:58 +0200287
288void pxa_serial_initialize(void)
289{
290#if defined(CONFIG_FFUART)
291 serial_register(&serial_ffuart_device);
292#endif
293#if defined(CONFIG_BTUART)
294 serial_register(&serial_btuart_device);
295#endif
296#if defined(CONFIG_STUART)
297 serial_register(&serial_stuart_device);
298#endif
299}