blob: 46d76894e6893615ad23d2a9b8391c1cef9c5173 [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 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
31 */
32
33#include <common.h>
wdenk5958f4a2003-09-18 09:21:33 +000034#include <watchdog.h>
stefano babic5ab4f032007-08-30 22:57:04 +020035#include <serial.h>
wdenkc6097192002-11-03 00:24:07 +000036#include <asm/arch/pxa-regs.h>
Marek Vasutac737ff2011-08-28 03:35:13 +020037#include <asm/arch/regs-uart.h>
Marek Vasut2db1e962010-09-09 09:50:39 +020038#include <asm/io.h>
Marek Vasutbc53d142012-09-12 12:26:30 +020039#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000040
Wolfgang Denk6405a152006-03-31 18:32:53 +020041DECLARE_GLOBAL_DATA_PTR;
42
Marek Vasutac737ff2011-08-28 03:35:13 +020043/*
44 * The numbering scheme differs here for PXA25x, PXA27x and PXA3xx so we can
45 * easily handle enabling of clock.
46 */
47#ifdef CONFIG_CPU_MONAHANS
48#define UART_CLK_BASE CKENA_21_BTUART
49#define UART_CLK_REG CKENA
50#define BTUART_INDEX 0
51#define FFUART_INDEX 1
52#define STUART_INDEX 2
Marek Vasut85cc88a2011-11-26 07:20:07 +010053#elif CONFIG_CPU_PXA25X
Marek Vasutac737ff2011-08-28 03:35:13 +020054#define UART_CLK_BASE (1 << 4) /* HWUART */
55#define UART_CLK_REG CKEN
56#define HWUART_INDEX 0
57#define STUART_INDEX 1
58#define FFUART_INDEX 2
59#define BTUART_INDEX 3
60#else /* PXA27x */
61#define UART_CLK_BASE CKEN5_STUART
62#define UART_CLK_REG CKEN
63#define STUART_INDEX 0
64#define FFUART_INDEX 1
65#define BTUART_INDEX 2
66#endif
67
68/*
69 * Only PXA250 has HWUART, to avoid poluting the code with more macros,
70 * artificially introduce this.
71 */
Marek Vasut85cc88a2011-11-26 07:20:07 +010072#ifndef CONFIG_CPU_PXA25X
Marek Vasutac737ff2011-08-28 03:35:13 +020073#define HWUART_INDEX 0xff
74#endif
stefano babic5ab4f032007-08-30 22:57:04 +020075
76#ifndef CONFIG_SERIAL_MULTI
Marek Vasutac737ff2011-08-28 03:35:13 +020077#if defined(CONFIG_FFUART)
Marcel Ziswiler53761bc2007-10-19 00:25:33 +020078#define UART_INDEX FFUART_INDEX
Marek Vasutac737ff2011-08-28 03:35:13 +020079#elif defined(CONFIG_BTUART)
Marcel Ziswiler53761bc2007-10-19 00:25:33 +020080#define UART_INDEX BTUART_INDEX
Marek Vasutac737ff2011-08-28 03:35:13 +020081#elif defined(CONFIG_STUART)
Marcel Ziswiler53761bc2007-10-19 00:25:33 +020082#define UART_INDEX STUART_INDEX
Marek Vasutac737ff2011-08-28 03:35:13 +020083#elif defined(CONFIG_HWUART)
84#define UART_INDEX HWUART_INDEX
stefano babic5ab4f032007-08-30 22:57:04 +020085#else
Marek Vasutac737ff2011-08-28 03:35:13 +020086#error "Please select CONFIG_(FF|BT|ST|HW)UART in board config file."
stefano babic5ab4f032007-08-30 22:57:04 +020087#endif
88#endif
89
Marek Vasutbcf48722012-09-12 12:59:42 +020090static uint32_t pxa_uart_get_baud_divider(void)
wdenkc6097192002-11-03 00:24:07 +000091{
wdenkc6097192002-11-03 00:24:07 +000092 if (gd->baudrate == 1200)
Marek Vasutac737ff2011-08-28 03:35:13 +020093 return 768;
wdenkc6097192002-11-03 00:24:07 +000094 else if (gd->baudrate == 9600)
Marek Vasutac737ff2011-08-28 03:35:13 +020095 return 96;
wdenkc6097192002-11-03 00:24:07 +000096 else if (gd->baudrate == 19200)
Marek Vasutac737ff2011-08-28 03:35:13 +020097 return 48;
wdenkc6097192002-11-03 00:24:07 +000098 else if (gd->baudrate == 38400)
Marek Vasutac737ff2011-08-28 03:35:13 +020099 return 24;
wdenkc6097192002-11-03 00:24:07 +0000100 else if (gd->baudrate == 57600)
Marek Vasutac737ff2011-08-28 03:35:13 +0200101 return 16;
wdenkc6097192002-11-03 00:24:07 +0000102 else if (gd->baudrate == 115200)
Marek Vasutac737ff2011-08-28 03:35:13 +0200103 return 8;
104 else /* Unsupported baudrate */
105 return 0;
106}
wdenkc6097192002-11-03 00:24:07 +0000107
Marek Vasutbcf48722012-09-12 12:59:42 +0200108static struct pxa_uart_regs *pxa_uart_index_to_regs(uint32_t uart_index)
Marek Vasutac737ff2011-08-28 03:35:13 +0200109{
stefano babic5ab4f032007-08-30 22:57:04 +0200110 switch (uart_index) {
Marek Vasutac737ff2011-08-28 03:35:13 +0200111 case FFUART_INDEX: return (struct pxa_uart_regs *)FFUART_BASE;
112 case BTUART_INDEX: return (struct pxa_uart_regs *)BTUART_BASE;
113 case STUART_INDEX: return (struct pxa_uart_regs *)STUART_BASE;
114 case HWUART_INDEX: return (struct pxa_uart_regs *)HWUART_BASE;
115 default:
116 return NULL;
117 }
118}
wdenkc6097192002-11-03 00:24:07 +0000119
Marek Vasutbcf48722012-09-12 12:59:42 +0200120static void pxa_uart_toggle_clock(uint32_t uart_index, int enable)
Marek Vasutac737ff2011-08-28 03:35:13 +0200121{
122 uint32_t clk_reg, clk_offset, reg;
wdenkc6097192002-11-03 00:24:07 +0000123
Marek Vasutac737ff2011-08-28 03:35:13 +0200124 clk_reg = UART_CLK_REG;
125 clk_offset = UART_CLK_BASE << uart_index;
wdenkc6097192002-11-03 00:24:07 +0000126
Marek Vasutac737ff2011-08-28 03:35:13 +0200127 reg = readl(clk_reg);
wdenkc6097192002-11-03 00:24:07 +0000128
Marek Vasutac737ff2011-08-28 03:35:13 +0200129 if (enable)
130 reg |= clk_offset;
131 else
132 reg &= ~clk_offset;
wdenkb02744a2003-04-05 00:53:31 +0000133
Marek Vasutac737ff2011-08-28 03:35:13 +0200134 writel(reg, clk_reg);
135}
wdenkb02744a2003-04-05 00:53:31 +0000136
Marek Vasutac737ff2011-08-28 03:35:13 +0200137/*
138 * Enable clock and set baud rate, parity etc.
139 */
140void pxa_setbrg_dev(uint32_t uart_index)
141{
142 uint32_t divider = 0;
143 struct pxa_uart_regs *uart_regs;
wdenkb02744a2003-04-05 00:53:31 +0000144
Marek Vasutac737ff2011-08-28 03:35:13 +0200145 divider = pxa_uart_get_baud_divider();
146 if (!divider)
147 hang();
stefano babic5ab4f032007-08-30 22:57:04 +0200148
Marek Vasutac737ff2011-08-28 03:35:13 +0200149 uart_regs = pxa_uart_index_to_regs(uart_index);
150 if (!uart_regs)
151 hang();
wdenk5958f4a2003-09-18 09:21:33 +0000152
Marek Vasutac737ff2011-08-28 03:35:13 +0200153 pxa_uart_toggle_clock(uart_index, 1);
wdenk5958f4a2003-09-18 09:21:33 +0000154
Marek Vasutac737ff2011-08-28 03:35:13 +0200155 /* Disable interrupts and FIFOs */
156 writel(0, &uart_regs->ier);
157 writel(0, &uart_regs->fcr);
wdenk5958f4a2003-09-18 09:21:33 +0000158
Marek Vasutac737ff2011-08-28 03:35:13 +0200159 /* Set baud rate */
160 writel(LCR_WLS0 | LCR_WLS1 | LCR_DLAB, &uart_regs->lcr);
161 writel(divider & 0xff, &uart_regs->dll);
162 writel(divider >> 8, &uart_regs->dlh);
163 writel(LCR_WLS0 | LCR_WLS1, &uart_regs->lcr);
wdenk5958f4a2003-09-18 09:21:33 +0000164
Marek Vasutac737ff2011-08-28 03:35:13 +0200165 /* Enable UART */
166 writel(IER_UUE, &uart_regs->ier);
wdenkc6097192002-11-03 00:24:07 +0000167}
168
wdenkc6097192002-11-03 00:24:07 +0000169/*
170 * Initialise the serial port with the given baudrate. The settings
171 * are always 8 data bits, no parity, 1 stop bit, no start bits.
wdenkc6097192002-11-03 00:24:07 +0000172 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200173int pxa_init_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000174{
stefano babic5ab4f032007-08-30 22:57:04 +0200175 pxa_setbrg_dev (uart_index);
Marek Vasutac737ff2011-08-28 03:35:13 +0200176 return 0;
wdenkc6097192002-11-03 00:24:07 +0000177}
178
wdenkc6097192002-11-03 00:24:07 +0000179/*
180 * Output a single byte to the serial port.
181 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200182void pxa_putc_dev(unsigned int uart_index, const char c)
wdenkc6097192002-11-03 00:24:07 +0000183{
Marek Vasutac737ff2011-08-28 03:35:13 +0200184 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200185
Marek Vasutac737ff2011-08-28 03:35:13 +0200186 uart_regs = pxa_uart_index_to_regs(uart_index);
187 if (!uart_regs)
188 hang();
stefano babic5ab4f032007-08-30 22:57:04 +0200189
Marek Vasutac737ff2011-08-28 03:35:13 +0200190 while (!(readl(&uart_regs->lsr) & LSR_TEMT))
191 WATCHDOG_RESET();
192 writel(c, &uart_regs->thr);
wdenkc6097192002-11-03 00:24:07 +0000193
194 /* If \n, also do \r */
195 if (c == '\n')
stefano babic5ab4f032007-08-30 22:57:04 +0200196 pxa_putc_dev (uart_index,'\r');
wdenkc6097192002-11-03 00:24:07 +0000197}
198
199/*
200 * Read a single byte from the serial port. Returns 1 on success, 0
201 * otherwise. When the function is succesfull, the character read is
202 * written into its argument c.
203 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200204int pxa_tstc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000205{
Marek Vasutac737ff2011-08-28 03:35:13 +0200206 struct pxa_uart_regs *uart_regs;
207
208 uart_regs = pxa_uart_index_to_regs(uart_index);
209 if (!uart_regs)
210 return -1;
211
212 return readl(&uart_regs->lsr) & LSR_DR;
wdenkc6097192002-11-03 00:24:07 +0000213}
214
215/*
216 * Read a single byte from the serial port. Returns 1 on success, 0
217 * otherwise. When the function is succesfull, the character read is
218 * written into its argument c.
219 */
Marek Vasutac737ff2011-08-28 03:35:13 +0200220int pxa_getc_dev(unsigned int uart_index)
wdenkc6097192002-11-03 00:24:07 +0000221{
Marek Vasutac737ff2011-08-28 03:35:13 +0200222 struct pxa_uart_regs *uart_regs;
stefano babic5ab4f032007-08-30 22:57:04 +0200223
Marek Vasutac737ff2011-08-28 03:35:13 +0200224 uart_regs = pxa_uart_index_to_regs(uart_index);
225 if (!uart_regs)
226 return -1;
stefano babic5ab4f032007-08-30 22:57:04 +0200227
Marek Vasutac737ff2011-08-28 03:35:13 +0200228 while (!(readl(&uart_regs->lsr) & LSR_DR))
229 WATCHDOG_RESET();
230 return readl(&uart_regs->rbr) & 0xff;
stefano babic5ab4f032007-08-30 22:57:04 +0200231}
232
Marek Vasutac737ff2011-08-28 03:35:13 +0200233void pxa_puts_dev(unsigned int uart_index, const char *s)
stefano babic5ab4f032007-08-30 22:57:04 +0200234{
Marek Vasutac737ff2011-08-28 03:35:13 +0200235 while (*s)
236 pxa_putc_dev(uart_index, *s++);
stefano babic5ab4f032007-08-30 22:57:04 +0200237}
238
Marek Vasutac737ff2011-08-28 03:35:13 +0200239#define pxa_uart(uart, UART) \
240 int uart##_init(void) \
241 { \
242 return pxa_init_dev(UART##_INDEX); \
243 } \
244 \
245 void uart##_setbrg(void) \
246 { \
247 return pxa_setbrg_dev(UART##_INDEX); \
248 } \
249 \
250 void uart##_putc(const char c) \
251 { \
252 return pxa_putc_dev(UART##_INDEX, c); \
253 } \
254 \
255 void uart##_puts(const char *s) \
256 { \
257 return pxa_puts_dev(UART##_INDEX, s); \
258 } \
259 \
260 int uart##_getc(void) \
261 { \
262 return pxa_getc_dev(UART##_INDEX); \
263 } \
264 \
265 int uart##_tstc(void) \
266 { \
267 return pxa_tstc_dev(UART##_INDEX); \
268 } \
stefano babic5ab4f032007-08-30 22:57:04 +0200269
Marek Vasutac737ff2011-08-28 03:35:13 +0200270#define pxa_uart_desc(uart) \
271 struct serial_device serial_##uart##_device = \
272 { \
Marek Vasut5bcdf242012-09-09 18:48:28 +0200273 .name = "serial_"#uart, \
274 .start = uart##_init, \
275 .stop = NULL, \
276 .setbrg = uart##_setbrg, \
277 .getc = uart##_getc, \
278 .tstc = uart##_tstc, \
279 .putc = uart##_putc, \
280 .puts = uart##_puts, \
Marek Vasutac737ff2011-08-28 03:35:13 +0200281 };
stefano babic5ab4f032007-08-30 22:57:04 +0200282
Marek Vasutac737ff2011-08-28 03:35:13 +0200283#define pxa_uart_multi(uart, UART) \
284 pxa_uart(uart, UART) \
285 pxa_uart_desc(uart)
stefano babic5ab4f032007-08-30 22:57:04 +0200286
Marek Vasutac737ff2011-08-28 03:35:13 +0200287#if defined(CONFIG_HWUART)
288 pxa_uart_multi(hwuart, HWUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200289#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200290#if defined(CONFIG_STUART)
291 pxa_uart_multi(stuart, STUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200292#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200293#if defined(CONFIG_FFUART)
294 pxa_uart_multi(ffuart, FFUART)
stefano babic5ab4f032007-08-30 22:57:04 +0200295#endif
Marek Vasutac737ff2011-08-28 03:35:13 +0200296#if defined(CONFIG_BTUART)
297 pxa_uart_multi(btuart, BTUART)
298#endif
stefano babic5ab4f032007-08-30 22:57:04 +0200299
Marek Vasutac737ff2011-08-28 03:35:13 +0200300#ifndef CONFIG_SERIAL_MULTI
301 pxa_uart(serial, UART)
Marek Vasutbc53d142012-09-12 12:26:30 +0200302#else
303__weak struct serial_device *default_serial_console(void)
304{
305#if CONFIG_CONS_INDEX == 1
306 return &serial_hwuart_device;
307#elif CONFIG_CONS_INDEX == 2
308 return &serial_stuart_device;
309#elif CONFIG_CONS_INDEX == 3
310 return &serial_ffuart_device;
311#elif CONFIG_CONS_INDEX == 4
312 return &serial_btuart_device;
313#else
314#error "Bad CONFIG_CONS_INDEX."
315#endif
316}
Marek Vasutf3442012012-09-12 13:57:58 +0200317
318void pxa_serial_initialize(void)
319{
320#if defined(CONFIG_FFUART)
321 serial_register(&serial_ffuart_device);
322#endif
323#if defined(CONFIG_BTUART)
324 serial_register(&serial_btuart_device);
325#endif
326#if defined(CONFIG_STUART)
327 serial_register(&serial_stuart_device);
328#endif
329}
Marek Vasutac737ff2011-08-28 03:35:13 +0200330#endif