aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-lpc32xx/serial.c
blob: 429cfdbb2b3d60c29f4c3fc12d4a5001aa2dd219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * arch/arm/mach-lpc32xx/serial.c
 *
 * Author: Kevin Wells <kevin.wells@nxp.com>
 *
 * Copyright (C) 2010 NXP Semiconductors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/serial_reg.h>
#include <linux/serial_8250.h>
#include <linux/clk.h>
#include <linux/io.h>

#include <mach/hardware.h>
#include <mach/platform.h>
#include "common.h"

#define LPC32XX_SUART_FIFO_SIZE	64

/* Standard 8250/16550 compatible serial ports */
static struct plat_serial8250_port serial_std_platform_data[] = {
#ifdef CONFIG_ARCH_LPC32XX_UART5_SELECT
	{
		.membase        = io_p2v(LPC32XX_UART5_BASE),
		.mapbase        = LPC32XX_UART5_BASE,
		.irq		= IRQ_LPC32XX_UART_IIR5,
		.uartclk	= LPC32XX_MAIN_OSC_FREQ,
		.regshift	= 2,
		.iotype		= UPIO_MEM32,
		.flags		= UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
					UPF_SKIP_TEST,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART3_SELECT
	{
		.membase	= io_p2v(LPC32XX_UART3_BASE),
		.mapbase        = LPC32XX_UART3_BASE,
		.irq		= IRQ_LPC32XX_UART_IIR3,
		.uartclk	= LPC32XX_MAIN_OSC_FREQ,
		.regshift	= 2,
		.iotype		= UPIO_MEM32,
		.flags		= UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
					UPF_SKIP_TEST,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART4_SELECT
	{
		.membase	= io_p2v(LPC32XX_UART4_BASE),
		.mapbase        = LPC32XX_UART4_BASE,
		.irq		= IRQ_LPC32XX_UART_IIR4,
		.uartclk	= LPC32XX_MAIN_OSC_FREQ,
		.regshift	= 2,
		.iotype		= UPIO_MEM32,
		.flags		= UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
					UPF_SKIP_TEST,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART6_SELECT
	{
		.membase	= io_p2v(LPC32XX_UART6_BASE),
		.mapbase        = LPC32XX_UART6_BASE,
		.irq		= IRQ_LPC32XX_UART_IIR6,
		.uartclk	= LPC32XX_MAIN_OSC_FREQ,
		.regshift	= 2,
		.iotype		= UPIO_MEM32,
		.flags		= UPF_BOOT_AUTOCONF | UPF_BUGGY_UART |
					UPF_SKIP_TEST,
	},
#endif
	{ },
};

struct uartinit {
	char *uart_ck_name;
	u32 ck_mode_mask;
	void __iomem *pdiv_clk_reg;
};

static struct uartinit uartinit_data[] __initdata = {
#ifdef CONFIG_ARCH_LPC32XX_UART5_SELECT
	{
		.uart_ck_name = "uart5_ck",
		.ck_mode_mask =
			LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 5),
		.pdiv_clk_reg = LPC32XX_CLKPWR_UART5_CLK_CTRL,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART3_SELECT
	{
		.uart_ck_name = "uart3_ck",
		.ck_mode_mask =
			LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 3),
		.pdiv_clk_reg = LPC32XX_CLKPWR_UART3_CLK_CTRL,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART4_SELECT
	{
		.uart_ck_name = "uart4_ck",
		.ck_mode_mask =
			LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 4),
		.pdiv_clk_reg = LPC32XX_CLKPWR_UART4_CLK_CTRL,
	},
#endif
#ifdef CONFIG_ARCH_LPC32XX_UART6_SELECT
	{
		.uart_ck_name = "uart6_ck",
		.ck_mode_mask =
			LPC32XX_UART_CLKMODE_LOAD(LPC32XX_UART_CLKMODE_ON, 6),
		.pdiv_clk_reg = LPC32XX_CLKPWR_UART6_CLK_CTRL,
	},
#endif
};

static struct platform_device serial_std_platform_device = {
	.name			= "serial8250",
	.id			= 0,
	.dev			= {
		.platform_data	= serial_std_platform_data,
	},
};

static struct platform_device *lpc32xx_serial_devs[] __initdata = {
	&serial_std_platform_device,
};

void __init lpc32xx_serial_init(void)
{
	u32 tmp, clkmodes = 0;
	struct clk *clk;
	unsigned int puart;
	int i, j;

	/* UART clocks are off, let clock driver manage them */
	__raw_writel(0, LPC32XX_CLKPWR_UART_CLK_CTRL);

	for (i = 0; i < ARRAY_SIZE(uartinit_data); i++) {
		clk = clk_get(NULL, uartinit_data[i].uart_ck_name);
		if (!IS_ERR(clk)) {
			clk_enable(clk);
			serial_std_platform_data[i].uartclk =
				clk_get_rate(clk);
		}

		/* Fall back on main osc rate if clock rate return fails */
		if (serial_std_platform_data[i].uartclk == 0)
			serial_std_platform_data[i].uartclk =
				LPC32XX_MAIN_OSC_FREQ;

		/* Setup UART clock modes for all UARTs, disable autoclock */
		clkmodes |= uartinit_data[i].ck_mode_mask;

		/* pre-UART clock divider set to 1 */
		__raw_writel(0x0101, uartinit_data[i].pdiv_clk_reg);
	}

	/* This needs to be done after all UART clocks are setup */
	__raw_writel(clkmodes, LPC32XX_UARTCTL_CLKMODE);
	for (i = 0; i < ARRAY_SIZE(uartinit_data) - 1; i++) {
		/* Force a flush of the RX FIFOs to work around a HW bug */
		puart = serial_std_platform_data[i].mapbase;
		__raw_writel(0xC1, LPC32XX_UART_IIR_FCR(puart));
		__raw_writel(0x00, LPC32XX_UART_DLL_FIFO(puart));
		j = LPC32XX_SUART_FIFO_SIZE;
		while (j--)
			tmp = __raw_readl(LPC32XX_UART_DLL_FIFO(puart));
		__raw_writel(0, LPC32XX_UART_IIR_FCR(puart));
	}

	/* Disable UART5->USB transparent mode or USB won't work */
	tmp = __raw_readl(LPC32XX_UARTCTL_CTRL);
	tmp &= ~LPC32XX_UART_U5_ROUTE_TO_USB;
	__raw_writel(tmp, LPC32XX_UARTCTL_CTRL);

	platform_add_devices(lpc32xx_serial_devs,
		ARRAY_SIZE(lpc32xx_serial_devs));
}