summaryrefslogtreecommitdiffstats
path: root/drivers/serial/amba-pl011.c
blob: 4c4067d5b52c0bb6b11757821d02bcc7ac86f006 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * (C) Copyright 2000
 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
 *
 * (C) Copyright 2004
 * ARM Ltd.
 * Philippe Robin, <philippe.robin@arm.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 */

/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */

#include <common.h>
#include <driver.h>
#include <init.h>
#include <malloc.h>
#include <io.h>
#include <regulator.h>
#include <linux/amba/serial.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/amba/bus.h>

/*
 * We wrap our port structure around the generic console_device.
 */
struct amba_uart_port {
	void __iomem		*base;
	struct console_device	uart;		/* uart */
	struct clk		*clk;		/* uart clock */
	u32			uartclk;
	struct vendor_data	*vendor;
};

/* There is by now at least one vendor with differing details, so handle it */
struct vendor_data {
	unsigned int		lcrh_tx;
	unsigned int		lcrh_rx;
};

static struct vendor_data vendor_arm = {
	.lcrh_tx		= UART011_LCRH,
	.lcrh_rx		= UART011_LCRH,
};

static struct vendor_data vendor_st = {
	.lcrh_tx		= ST_UART011_LCRH_TX,
	.lcrh_rx		= ST_UART011_LCRH_RX,
};

static inline struct amba_uart_port *
to_amba_uart_port(struct console_device *uart)
{
	return container_of(uart, struct amba_uart_port, uart);
}

static int pl011_setbaudrate(struct console_device *cdev, int baudrate)
{
	struct amba_uart_port *uart = to_amba_uart_port(cdev);
	unsigned int temp;
	unsigned int divider;
	unsigned int remainder;
	unsigned int fraction;

	/*
	 ** Set baud rate
	 **
	 ** IBRD = UART_CLK / (16 * BAUD_RATE)
	 ** FBRD = ROUND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
	 */
	temp = 16 * baudrate;
	divider = uart->uartclk / temp;
	remainder = uart->uartclk % temp;
	temp = (8 * remainder) / baudrate;
	fraction = (temp >> 1) + (temp & 1);

	writel(divider, uart->base + UART011_IBRD);
	writel(fraction, uart->base + UART011_FBRD);

	return 0;
}

static void pl011_putc(struct console_device *cdev, char c)
{
	struct amba_uart_port *uart = to_amba_uart_port(cdev);

	/* Wait until there is space in the FIFO */
	while (readl(uart->base + UART01x_FR) & UART01x_FR_TXFF);

	/* Send the character */
	writel(c, uart->base + UART01x_DR);
}

static int pl011_getc(struct console_device *cdev)
{
	struct amba_uart_port *uart = to_amba_uart_port(cdev);
	unsigned int data;

	/* Wait until there is data in the FIFO */
	while (readl(uart->base + UART01x_FR) & UART01x_FR_RXFE);

	data = readl(uart->base + UART01x_DR);

	/* Check for an error flag */
	if (data & 0xffffff00) {
		/* Clear the error */
		writel(0xffffffff, uart->base + UART01x_ECR);
		return -1;
	}

	return (int)data;
}

static int pl011_tstc(struct console_device *cdev)
{
	struct amba_uart_port *uart = to_amba_uart_port(cdev);

	return !(readl(uart->base + UART01x_FR) & UART01x_FR_RXFE);
}

static void pl011_rlcr(struct amba_uart_port *uart, u32 lcr)
{
	struct vendor_data	*vendor = uart->vendor;

	writew(lcr, uart->base + vendor->lcrh_rx);
	if (vendor->lcrh_tx != vendor->lcrh_rx) {
		int i;
		/*
		 * Wait 10 PCLKs before writing LCRH_TX register,
		 * to get this delay write read only register 10 times
		 */
		for (i = 0; i < 10; ++i)
			writew(0xff, uart->base + UART011_MIS);
		writew(lcr, uart->base +  vendor->lcrh_tx);
	}
}

int pl011_init_port (struct console_device *cdev)
{
	struct amba_uart_port *uart = to_amba_uart_port(cdev);

	/*
	 ** First, disable everything.
	 */
	writel(0x0, uart->base + UART011_CR);

	/*
	 * Try to enable the clock producer.
	 */
	clk_enable(uart->clk);

	uart->uartclk = clk_get_rate(uart->clk);

	/*
	 ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled.
	 */
	pl011_rlcr(uart, UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN);

	/*
	 ** Finally, enable the UART
	 */
	writel((UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE | UART011_CR_RTS),
	       uart->base + UART011_CR);

	return 0;
}

static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
{
	struct amba_uart_port *uart;
	struct console_device *cdev;
	struct regulator *r;

	r = regulator_get(&dev->dev, NULL);
	if (r) {
		int ret;

		ret = regulator_enable(r);
		if (ret)
			return ret;
	}

	uart = xzalloc(sizeof(struct amba_uart_port));
	uart->clk = clk_get(&dev->dev, NULL);
	uart->base = amba_get_mem_region(dev);
	uart->vendor = (void*)id->data;

	if (IS_ERR(uart->clk))
		return PTR_ERR(uart->clk);

	cdev = &uart->uart;
	cdev->dev = &dev->dev;
	cdev->tstc = pl011_tstc;
	cdev->putc = pl011_putc;
	cdev->getc = pl011_getc;
	cdev->setbrg = pl011_setbaudrate;

	pl011_init_port(cdev);

	/* Enable UART */

	console_register(cdev);

	return 0;
}

static struct amba_id pl011_ids[] = {
	{
		.id	= 0x00041011,
		.mask	= 0x000fffff,
		.data	= &vendor_arm,
	},
	{
		.id	= 0x00380802,
		.mask	= 0x00ffffff,
		.data	= &vendor_st,
	},
	{ 0, 0 },
};

struct amba_driver pl011_driver = {
	.drv = {
		.name = "uart-pl011",
	},
	.probe		= pl011_probe,
	.id_table	= pl011_ids,
};

static int pl011_init(void)
{
	amba_driver_register(&pl011_driver);
	return 0;
}

console_initcall(pl011_init);