xref: /openbmc/u-boot/drivers/i2c/at91_i2c.c (revision 8800e0fa20c89df846559d85341c55439405cab6)
1*8800e0faSSongjun Wu /*
2*8800e0faSSongjun Wu  * Atmel I2C driver.
3*8800e0faSSongjun Wu  *
4*8800e0faSSongjun Wu  * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com>
5*8800e0faSSongjun Wu  *
6*8800e0faSSongjun Wu  * SPDX-License-Identifier:	GPL-2.0+
7*8800e0faSSongjun Wu  */
8*8800e0faSSongjun Wu 
9*8800e0faSSongjun Wu #include <asm/io.h>
10*8800e0faSSongjun Wu #include <common.h>
11*8800e0faSSongjun Wu #include <clk_client.h>
12*8800e0faSSongjun Wu #include <dm.h>
13*8800e0faSSongjun Wu #include <errno.h>
14*8800e0faSSongjun Wu #include <fdtdec.h>
15*8800e0faSSongjun Wu #include <i2c.h>
16*8800e0faSSongjun Wu #include <linux/bitops.h>
17*8800e0faSSongjun Wu #include <mach/clk.h>
18*8800e0faSSongjun Wu 
19*8800e0faSSongjun Wu #include "at91_i2c.h"
20*8800e0faSSongjun Wu 
21*8800e0faSSongjun Wu DECLARE_GLOBAL_DATA_PTR;
22*8800e0faSSongjun Wu 
23*8800e0faSSongjun Wu #define I2C_TIMEOUT_MS	100
24*8800e0faSSongjun Wu 
25*8800e0faSSongjun Wu static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status)
26*8800e0faSSongjun Wu {
27*8800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
28*8800e0faSSongjun Wu 	ulong start_time = get_timer(0);
29*8800e0faSSongjun Wu 	u32 sr;
30*8800e0faSSongjun Wu 
31*8800e0faSSongjun Wu 	bus->status = 0;
32*8800e0faSSongjun Wu 
33*8800e0faSSongjun Wu 	do {
34*8800e0faSSongjun Wu 		sr = readl(&reg->sr);
35*8800e0faSSongjun Wu 		bus->status |= sr;
36*8800e0faSSongjun Wu 
37*8800e0faSSongjun Wu 		if (sr & TWI_SR_NACK)
38*8800e0faSSongjun Wu 			return -EREMOTEIO;
39*8800e0faSSongjun Wu 		else if (sr & status)
40*8800e0faSSongjun Wu 			return 0;
41*8800e0faSSongjun Wu 	} while (get_timer(start_time) < I2C_TIMEOUT_MS);
42*8800e0faSSongjun Wu 
43*8800e0faSSongjun Wu 	return -ETIMEDOUT;
44*8800e0faSSongjun Wu }
45*8800e0faSSongjun Wu 
46*8800e0faSSongjun Wu static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg)
47*8800e0faSSongjun Wu {
48*8800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
49*8800e0faSSongjun Wu 	bool is_read = msg->flags & I2C_M_RD;
50*8800e0faSSongjun Wu 	u32 i;
51*8800e0faSSongjun Wu 	int ret = 0;
52*8800e0faSSongjun Wu 
53*8800e0faSSongjun Wu 	readl(&reg->sr);
54*8800e0faSSongjun Wu 	if (is_read) {
55*8800e0faSSongjun Wu 		writel(TWI_CR_START, &reg->cr);
56*8800e0faSSongjun Wu 
57*8800e0faSSongjun Wu 		for (i = 0; !ret && i < (msg->len - 1); i++) {
58*8800e0faSSongjun Wu 			ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
59*8800e0faSSongjun Wu 			msg->buf[i] = readl(&reg->rhr);
60*8800e0faSSongjun Wu 		}
61*8800e0faSSongjun Wu 
62*8800e0faSSongjun Wu 		if (ret)
63*8800e0faSSongjun Wu 			goto error;
64*8800e0faSSongjun Wu 
65*8800e0faSSongjun Wu 		writel(TWI_CR_STOP, &reg->cr);
66*8800e0faSSongjun Wu 
67*8800e0faSSongjun Wu 		ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
68*8800e0faSSongjun Wu 		if (ret)
69*8800e0faSSongjun Wu 			goto error;
70*8800e0faSSongjun Wu 
71*8800e0faSSongjun Wu 		msg->buf[i] = readl(&reg->rhr);
72*8800e0faSSongjun Wu 
73*8800e0faSSongjun Wu 	} else {
74*8800e0faSSongjun Wu 		writel(msg->buf[0], &reg->thr);
75*8800e0faSSongjun Wu 		for (i = 1; !ret && (i < msg->len); i++) {
76*8800e0faSSongjun Wu 			writel(msg->buf[i], &reg->thr);
77*8800e0faSSongjun Wu 			ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
78*8800e0faSSongjun Wu 		}
79*8800e0faSSongjun Wu 
80*8800e0faSSongjun Wu 		if (ret)
81*8800e0faSSongjun Wu 			goto error;
82*8800e0faSSongjun Wu 
83*8800e0faSSongjun Wu 		writel(TWI_CR_STOP, &reg->cr);
84*8800e0faSSongjun Wu 	}
85*8800e0faSSongjun Wu 
86*8800e0faSSongjun Wu 	if (!ret)
87*8800e0faSSongjun Wu 		ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
88*8800e0faSSongjun Wu 
89*8800e0faSSongjun Wu 	if (ret)
90*8800e0faSSongjun Wu 		goto error;
91*8800e0faSSongjun Wu 
92*8800e0faSSongjun Wu 	if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
93*8800e0faSSongjun Wu 		ret = -EIO;
94*8800e0faSSongjun Wu 		goto error;
95*8800e0faSSongjun Wu 	}
96*8800e0faSSongjun Wu 
97*8800e0faSSongjun Wu 	return 0;
98*8800e0faSSongjun Wu 
99*8800e0faSSongjun Wu error:
100*8800e0faSSongjun Wu 	if (bus->status & TWI_SR_LOCK)
101*8800e0faSSongjun Wu 		writel(TWI_CR_LOCKCLR, &reg->cr);
102*8800e0faSSongjun Wu 
103*8800e0faSSongjun Wu 	return ret;
104*8800e0faSSongjun Wu }
105*8800e0faSSongjun Wu 
106*8800e0faSSongjun Wu static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
107*8800e0faSSongjun Wu {
108*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
109*8800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
110*8800e0faSSongjun Wu 	struct i2c_msg *m_start = msg;
111*8800e0faSSongjun Wu 	bool is_read;
112*8800e0faSSongjun Wu 	u32 int_addr_flag = 0;
113*8800e0faSSongjun Wu 	int ret = 0;
114*8800e0faSSongjun Wu 
115*8800e0faSSongjun Wu 	if (nmsgs == 2) {
116*8800e0faSSongjun Wu 		int internal_address = 0;
117*8800e0faSSongjun Wu 		int i;
118*8800e0faSSongjun Wu 
119*8800e0faSSongjun Wu 		/* 1st msg is put into the internal address, start with 2nd */
120*8800e0faSSongjun Wu 		m_start = &msg[1];
121*8800e0faSSongjun Wu 
122*8800e0faSSongjun Wu 		/* the max length of internal address is 3 bytes */
123*8800e0faSSongjun Wu 		if (msg->len > 3)
124*8800e0faSSongjun Wu 			return -EFAULT;
125*8800e0faSSongjun Wu 
126*8800e0faSSongjun Wu 		for (i = 0; i < msg->len; ++i) {
127*8800e0faSSongjun Wu 			const unsigned addr = msg->buf[msg->len - 1 - i];
128*8800e0faSSongjun Wu 
129*8800e0faSSongjun Wu 			internal_address |= addr << (8 * i);
130*8800e0faSSongjun Wu 			int_addr_flag += TWI_MMR_IADRSZ_1;
131*8800e0faSSongjun Wu 		}
132*8800e0faSSongjun Wu 
133*8800e0faSSongjun Wu 		writel(internal_address, &reg->iadr);
134*8800e0faSSongjun Wu 	}
135*8800e0faSSongjun Wu 
136*8800e0faSSongjun Wu 	is_read = m_start->flags & I2C_M_RD;
137*8800e0faSSongjun Wu 
138*8800e0faSSongjun Wu 	writel((m_start->addr << 16) | int_addr_flag |
139*8800e0faSSongjun Wu 	       (is_read ? TWI_MMR_MREAD : 0), &reg->mmr);
140*8800e0faSSongjun Wu 
141*8800e0faSSongjun Wu 	ret = at91_i2c_xfer_msg(bus, m_start);
142*8800e0faSSongjun Wu 
143*8800e0faSSongjun Wu 	return ret;
144*8800e0faSSongjun Wu }
145*8800e0faSSongjun Wu 
146*8800e0faSSongjun Wu /*
147*8800e0faSSongjun Wu  * Calculate symmetric clock as stated in datasheet:
148*8800e0faSSongjun Wu  * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
149*8800e0faSSongjun Wu  */
150*8800e0faSSongjun Wu static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
151*8800e0faSSongjun Wu {
152*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
153*8800e0faSSongjun Wu 	const struct at91_i2c_pdata *pdata = bus->pdata;
154*8800e0faSSongjun Wu 	int offset = pdata->clk_offset;
155*8800e0faSSongjun Wu 	int max_ckdiv = pdata->clk_max_div;
156*8800e0faSSongjun Wu 	int ckdiv, cdiv, div;
157*8800e0faSSongjun Wu 	unsigned long src_rate;
158*8800e0faSSongjun Wu 
159*8800e0faSSongjun Wu 	src_rate = bus->bus_clk_rate;
160*8800e0faSSongjun Wu 
161*8800e0faSSongjun Wu 	div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
162*8800e0faSSongjun Wu 	ckdiv = fls(div >> 8);
163*8800e0faSSongjun Wu 	cdiv = div >> ckdiv;
164*8800e0faSSongjun Wu 
165*8800e0faSSongjun Wu 	if (ckdiv > max_ckdiv) {
166*8800e0faSSongjun Wu 		ckdiv = max_ckdiv;
167*8800e0faSSongjun Wu 		cdiv = 255;
168*8800e0faSSongjun Wu 	}
169*8800e0faSSongjun Wu 
170*8800e0faSSongjun Wu 	bus->speed = DIV_ROUND_UP(src_rate,
171*8800e0faSSongjun Wu 				  (cdiv * (1 << ckdiv) + offset) * 2);
172*8800e0faSSongjun Wu 
173*8800e0faSSongjun Wu 	bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
174*8800e0faSSongjun Wu }
175*8800e0faSSongjun Wu 
176*8800e0faSSongjun Wu static int at91_i2c_enable_clk(struct udevice *dev)
177*8800e0faSSongjun Wu {
178*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
179*8800e0faSSongjun Wu 	struct udevice *dev_clk;
180*8800e0faSSongjun Wu 	struct clk clk;
181*8800e0faSSongjun Wu 	ulong clk_rate;
182*8800e0faSSongjun Wu 	int periph;
183*8800e0faSSongjun Wu 	int ret;
184*8800e0faSSongjun Wu 
185*8800e0faSSongjun Wu 	ret = clk_get_by_index(dev, 0, &clk);
186*8800e0faSSongjun Wu 	if (ret)
187*8800e0faSSongjun Wu 		return -EINVAL;
188*8800e0faSSongjun Wu 
189*8800e0faSSongjun Wu 	periph = fdtdec_get_uint(gd->fdt_blob, clk.dev->of_offset, "reg", -1);
190*8800e0faSSongjun Wu 	if (periph < 0)
191*8800e0faSSongjun Wu 		return -EINVAL;
192*8800e0faSSongjun Wu 
193*8800e0faSSongjun Wu 	dev_clk = dev_get_parent(clk.dev);
194*8800e0faSSongjun Wu 	ret = clk_request(dev_clk, &clk);
195*8800e0faSSongjun Wu 	if (ret)
196*8800e0faSSongjun Wu 		return ret;
197*8800e0faSSongjun Wu 
198*8800e0faSSongjun Wu 	clk.id = periph;
199*8800e0faSSongjun Wu 	ret = clk_enable(&clk);
200*8800e0faSSongjun Wu 	if (ret)
201*8800e0faSSongjun Wu 		return ret;
202*8800e0faSSongjun Wu 
203*8800e0faSSongjun Wu 	ret = clk_get_by_index(dev_clk, 0, &clk);
204*8800e0faSSongjun Wu 	if (ret)
205*8800e0faSSongjun Wu 		return ret;
206*8800e0faSSongjun Wu 
207*8800e0faSSongjun Wu 	clk_rate = clk_get_rate(&clk);
208*8800e0faSSongjun Wu 	if (!clk_rate)
209*8800e0faSSongjun Wu 		return -ENODEV;
210*8800e0faSSongjun Wu 
211*8800e0faSSongjun Wu 	bus->bus_clk_rate = clk_rate;
212*8800e0faSSongjun Wu 
213*8800e0faSSongjun Wu 	clk_free(&clk);
214*8800e0faSSongjun Wu 
215*8800e0faSSongjun Wu 	return 0;
216*8800e0faSSongjun Wu }
217*8800e0faSSongjun Wu 
218*8800e0faSSongjun Wu static int at91_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
219*8800e0faSSongjun Wu {
220*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
221*8800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
222*8800e0faSSongjun Wu 	int ret;
223*8800e0faSSongjun Wu 
224*8800e0faSSongjun Wu 	ret = at91_i2c_enable_clk(dev);
225*8800e0faSSongjun Wu 	if (ret)
226*8800e0faSSongjun Wu 		return ret;
227*8800e0faSSongjun Wu 
228*8800e0faSSongjun Wu 	writel(TWI_CR_SWRST, &reg->cr);
229*8800e0faSSongjun Wu 
230*8800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, bus->clock_frequency);
231*8800e0faSSongjun Wu 
232*8800e0faSSongjun Wu 	writel(bus->cwgr_val, &reg->cwgr);
233*8800e0faSSongjun Wu 	writel(TWI_CR_MSEN, &reg->cr);
234*8800e0faSSongjun Wu 	writel(TWI_CR_SVDIS, &reg->cr);
235*8800e0faSSongjun Wu 
236*8800e0faSSongjun Wu 	return 0;
237*8800e0faSSongjun Wu }
238*8800e0faSSongjun Wu 
239*8800e0faSSongjun Wu static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
240*8800e0faSSongjun Wu {
241*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
242*8800e0faSSongjun Wu 
243*8800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, speed);
244*8800e0faSSongjun Wu 
245*8800e0faSSongjun Wu 	writel(bus->cwgr_val, &bus->regs->cwgr);
246*8800e0faSSongjun Wu 
247*8800e0faSSongjun Wu 	return 0;
248*8800e0faSSongjun Wu }
249*8800e0faSSongjun Wu 
250*8800e0faSSongjun Wu int at91_i2c_get_bus_speed(struct udevice *dev)
251*8800e0faSSongjun Wu {
252*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
253*8800e0faSSongjun Wu 
254*8800e0faSSongjun Wu 	return bus->speed;
255*8800e0faSSongjun Wu }
256*8800e0faSSongjun Wu 
257*8800e0faSSongjun Wu static int at91_i2c_ofdata_to_platdata(struct udevice *dev)
258*8800e0faSSongjun Wu {
259*8800e0faSSongjun Wu 	const void *blob = gd->fdt_blob;
260*8800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
261*8800e0faSSongjun Wu 	int node = dev->of_offset;
262*8800e0faSSongjun Wu 
263*8800e0faSSongjun Wu 	bus->regs = (struct at91_i2c_regs *)dev_get_addr(dev);
264*8800e0faSSongjun Wu 	bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
265*8800e0faSSongjun Wu 	bus->clock_frequency = fdtdec_get_int(blob, node,
266*8800e0faSSongjun Wu 					      "clock-frequency", 100000);
267*8800e0faSSongjun Wu 
268*8800e0faSSongjun Wu 	return 0;
269*8800e0faSSongjun Wu }
270*8800e0faSSongjun Wu 
271*8800e0faSSongjun Wu static const struct dm_i2c_ops at91_i2c_ops = {
272*8800e0faSSongjun Wu 	.xfer		= at91_i2c_xfer,
273*8800e0faSSongjun Wu 	.probe_chip	= at91_i2c_probe,
274*8800e0faSSongjun Wu 	.set_bus_speed	= at91_i2c_set_bus_speed,
275*8800e0faSSongjun Wu 	.get_bus_speed	= at91_i2c_get_bus_speed,
276*8800e0faSSongjun Wu };
277*8800e0faSSongjun Wu 
278*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91rm9200_config = {
279*8800e0faSSongjun Wu 	.clk_max_div = 5,
280*8800e0faSSongjun Wu 	.clk_offset = 3,
281*8800e0faSSongjun Wu };
282*8800e0faSSongjun Wu 
283*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9261_config = {
284*8800e0faSSongjun Wu 	.clk_max_div = 5,
285*8800e0faSSongjun Wu 	.clk_offset = 4,
286*8800e0faSSongjun Wu };
287*8800e0faSSongjun Wu 
288*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9260_config = {
289*8800e0faSSongjun Wu 	.clk_max_div = 7,
290*8800e0faSSongjun Wu 	.clk_offset = 4,
291*8800e0faSSongjun Wu };
292*8800e0faSSongjun Wu 
293*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g20_config = {
294*8800e0faSSongjun Wu 	.clk_max_div = 7,
295*8800e0faSSongjun Wu 	.clk_offset = 4,
296*8800e0faSSongjun Wu };
297*8800e0faSSongjun Wu 
298*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g10_config = {
299*8800e0faSSongjun Wu 	.clk_max_div = 7,
300*8800e0faSSongjun Wu 	.clk_offset = 4,
301*8800e0faSSongjun Wu };
302*8800e0faSSongjun Wu 
303*8800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9x5_config = {
304*8800e0faSSongjun Wu 	.clk_max_div = 7,
305*8800e0faSSongjun Wu 	.clk_offset = 4,
306*8800e0faSSongjun Wu };
307*8800e0faSSongjun Wu 
308*8800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d4_config = {
309*8800e0faSSongjun Wu 	.clk_max_div = 7,
310*8800e0faSSongjun Wu 	.clk_offset = 4,
311*8800e0faSSongjun Wu };
312*8800e0faSSongjun Wu 
313*8800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d2_config = {
314*8800e0faSSongjun Wu 	.clk_max_div = 7,
315*8800e0faSSongjun Wu 	.clk_offset = 3,
316*8800e0faSSongjun Wu };
317*8800e0faSSongjun Wu 
318*8800e0faSSongjun Wu static const struct udevice_id at91_i2c_ids[] = {
319*8800e0faSSongjun Wu { .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config },
320*8800e0faSSongjun Wu { .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config },
321*8800e0faSSongjun Wu { .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config },
322*8800e0faSSongjun Wu { .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config },
323*8800e0faSSongjun Wu { .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config },
324*8800e0faSSongjun Wu { .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config },
325*8800e0faSSongjun Wu { .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config },
326*8800e0faSSongjun Wu { .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config },
327*8800e0faSSongjun Wu { }
328*8800e0faSSongjun Wu };
329*8800e0faSSongjun Wu 
330*8800e0faSSongjun Wu U_BOOT_DRIVER(i2c_at91) = {
331*8800e0faSSongjun Wu 	.name	= "i2c_at91",
332*8800e0faSSongjun Wu 	.id	= UCLASS_I2C,
333*8800e0faSSongjun Wu 	.of_match = at91_i2c_ids,
334*8800e0faSSongjun Wu 	.ofdata_to_platdata = at91_i2c_ofdata_to_platdata,
335*8800e0faSSongjun Wu 	.per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
336*8800e0faSSongjun Wu 	.priv_auto_alloc_size = sizeof(struct at91_i2c_bus),
337*8800e0faSSongjun Wu 	.ops	= &at91_i2c_ops,
338*8800e0faSSongjun Wu };
339