xref: /openbmc/u-boot/drivers/i2c/meson_i2c.c (revision 8c47ab6b)
1f8d9ca18SBeniamino Galvani /*
2f8d9ca18SBeniamino Galvani  * (C) Copyright 2017 - Beniamino Galvani <b.galvani@gmail.com>
3f8d9ca18SBeniamino Galvani  *
4f8d9ca18SBeniamino Galvani  * SPDX-License-Identifier:	GPL-2.0+
5f8d9ca18SBeniamino Galvani  */
6f8d9ca18SBeniamino Galvani #include <common.h>
7f8d9ca18SBeniamino Galvani #include <asm/arch/i2c.h>
8f8d9ca18SBeniamino Galvani #include <asm/io.h>
9f8d9ca18SBeniamino Galvani #include <dm.h>
10f8d9ca18SBeniamino Galvani #include <i2c.h>
11f8d9ca18SBeniamino Galvani 
12*8c47ab6bSBeniamino Galvani #define I2C_TIMEOUT_MS		100
13f8d9ca18SBeniamino Galvani 
14f8d9ca18SBeniamino Galvani /* Control register fields */
15f8d9ca18SBeniamino Galvani #define REG_CTRL_START		BIT(0)
16f8d9ca18SBeniamino Galvani #define REG_CTRL_ACK_IGNORE	BIT(1)
17f8d9ca18SBeniamino Galvani #define REG_CTRL_STATUS		BIT(2)
18f8d9ca18SBeniamino Galvani #define REG_CTRL_ERROR		BIT(3)
19f8d9ca18SBeniamino Galvani #define REG_CTRL_CLKDIV_SHIFT	12
20f8d9ca18SBeniamino Galvani #define REG_CTRL_CLKDIV_MASK	GENMASK(21, 12)
21f8d9ca18SBeniamino Galvani #define REG_CTRL_CLKDIVEXT_SHIFT 28
22f8d9ca18SBeniamino Galvani #define REG_CTRL_CLKDIVEXT_MASK	GENMASK(29, 28)
23f8d9ca18SBeniamino Galvani 
24f8d9ca18SBeniamino Galvani enum {
25f8d9ca18SBeniamino Galvani 	TOKEN_END = 0,
26f8d9ca18SBeniamino Galvani 	TOKEN_START,
27f8d9ca18SBeniamino Galvani 	TOKEN_SLAVE_ADDR_WRITE,
28f8d9ca18SBeniamino Galvani 	TOKEN_SLAVE_ADDR_READ,
29f8d9ca18SBeniamino Galvani 	TOKEN_DATA,
30f8d9ca18SBeniamino Galvani 	TOKEN_DATA_LAST,
31f8d9ca18SBeniamino Galvani 	TOKEN_STOP,
32f8d9ca18SBeniamino Galvani };
33f8d9ca18SBeniamino Galvani 
34f8d9ca18SBeniamino Galvani struct i2c_regs {
35f8d9ca18SBeniamino Galvani 	u32 ctrl;
36f8d9ca18SBeniamino Galvani 	u32 slave_addr;
37f8d9ca18SBeniamino Galvani 	u32 tok_list0;
38f8d9ca18SBeniamino Galvani 	u32 tok_list1;
39f8d9ca18SBeniamino Galvani 	u32 tok_wdata0;
40f8d9ca18SBeniamino Galvani 	u32 tok_wdata1;
41f8d9ca18SBeniamino Galvani 	u32 tok_rdata0;
42f8d9ca18SBeniamino Galvani 	u32 tok_rdata1;
43f8d9ca18SBeniamino Galvani };
44f8d9ca18SBeniamino Galvani 
45f8d9ca18SBeniamino Galvani struct meson_i2c {
46f8d9ca18SBeniamino Galvani 	struct i2c_regs *regs;
47f8d9ca18SBeniamino Galvani 	struct i2c_msg *msg;
48f8d9ca18SBeniamino Galvani 	bool last;
49f8d9ca18SBeniamino Galvani 	uint count;
50f8d9ca18SBeniamino Galvani 	uint pos;
51f8d9ca18SBeniamino Galvani 	u32 tokens[2];
52f8d9ca18SBeniamino Galvani 	uint num_tokens;
53f8d9ca18SBeniamino Galvani };
54f8d9ca18SBeniamino Galvani 
55f8d9ca18SBeniamino Galvani static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
56f8d9ca18SBeniamino Galvani {
57f8d9ca18SBeniamino Galvani 	i2c->tokens[0] = 0;
58f8d9ca18SBeniamino Galvani 	i2c->tokens[1] = 0;
59f8d9ca18SBeniamino Galvani 	i2c->num_tokens = 0;
60f8d9ca18SBeniamino Galvani }
61f8d9ca18SBeniamino Galvani 
62f8d9ca18SBeniamino Galvani static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
63f8d9ca18SBeniamino Galvani {
64f8d9ca18SBeniamino Galvani 	if (i2c->num_tokens < 8)
65f8d9ca18SBeniamino Galvani 		i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
66f8d9ca18SBeniamino Galvani 	else
67f8d9ca18SBeniamino Galvani 		i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
68f8d9ca18SBeniamino Galvani 
69f8d9ca18SBeniamino Galvani 	i2c->num_tokens++;
70f8d9ca18SBeniamino Galvani }
71f8d9ca18SBeniamino Galvani 
72f8d9ca18SBeniamino Galvani static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len)
73f8d9ca18SBeniamino Galvani {
74f8d9ca18SBeniamino Galvani 	u32 rdata0, rdata1;
75f8d9ca18SBeniamino Galvani 	int i;
76f8d9ca18SBeniamino Galvani 
77f8d9ca18SBeniamino Galvani 	rdata0 = readl(&i2c->regs->tok_rdata0);
78f8d9ca18SBeniamino Galvani 	rdata1 = readl(&i2c->regs->tok_rdata1);
79f8d9ca18SBeniamino Galvani 
80f8d9ca18SBeniamino Galvani 	debug("meson i2c: read data %08x %08x len %d\n", rdata0, rdata1, len);
81f8d9ca18SBeniamino Galvani 
82f8d9ca18SBeniamino Galvani 	for (i = 0; i < min(4, len); i++)
83f8d9ca18SBeniamino Galvani 		*buf++ = (rdata0 >> i * 8) & 0xff;
84f8d9ca18SBeniamino Galvani 
85f8d9ca18SBeniamino Galvani 	for (i = 4; i < min(8, len); i++)
86f8d9ca18SBeniamino Galvani 		*buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
87f8d9ca18SBeniamino Galvani }
88f8d9ca18SBeniamino Galvani 
89f8d9ca18SBeniamino Galvani static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len)
90f8d9ca18SBeniamino Galvani {
91f8d9ca18SBeniamino Galvani 	u32 wdata0 = 0, wdata1 = 0;
92f8d9ca18SBeniamino Galvani 	int i;
93f8d9ca18SBeniamino Galvani 
94f8d9ca18SBeniamino Galvani 	for (i = 0; i < min(4, len); i++)
95f8d9ca18SBeniamino Galvani 		wdata0 |= *buf++ << (i * 8);
96f8d9ca18SBeniamino Galvani 
97f8d9ca18SBeniamino Galvani 	for (i = 4; i < min(8, len); i++)
98f8d9ca18SBeniamino Galvani 		wdata1 |= *buf++ << ((i - 4) * 8);
99f8d9ca18SBeniamino Galvani 
100f8d9ca18SBeniamino Galvani 	writel(wdata0, &i2c->regs->tok_wdata0);
101f8d9ca18SBeniamino Galvani 	writel(wdata1, &i2c->regs->tok_wdata1);
102f8d9ca18SBeniamino Galvani 
103f8d9ca18SBeniamino Galvani 	debug("meson i2c: write data %08x %08x len %d\n", wdata0, wdata1, len);
104f8d9ca18SBeniamino Galvani }
105f8d9ca18SBeniamino Galvani 
106f8d9ca18SBeniamino Galvani static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
107f8d9ca18SBeniamino Galvani {
108f8d9ca18SBeniamino Galvani 	bool write = !(i2c->msg->flags & I2C_M_RD);
109f8d9ca18SBeniamino Galvani 	int i;
110f8d9ca18SBeniamino Galvani 
111f8d9ca18SBeniamino Galvani 	i2c->count = min(i2c->msg->len - i2c->pos, 8u);
112f8d9ca18SBeniamino Galvani 
113f8d9ca18SBeniamino Galvani 	for (i = 0; i + 1 < i2c->count; i++)
114f8d9ca18SBeniamino Galvani 		meson_i2c_add_token(i2c, TOKEN_DATA);
115f8d9ca18SBeniamino Galvani 
116f8d9ca18SBeniamino Galvani 	if (i2c->count) {
117f8d9ca18SBeniamino Galvani 		if (write || i2c->pos + i2c->count < i2c->msg->len)
118f8d9ca18SBeniamino Galvani 			meson_i2c_add_token(i2c, TOKEN_DATA);
119f8d9ca18SBeniamino Galvani 		else
120f8d9ca18SBeniamino Galvani 			meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
121f8d9ca18SBeniamino Galvani 	}
122f8d9ca18SBeniamino Galvani 
123f8d9ca18SBeniamino Galvani 	if (write)
124f8d9ca18SBeniamino Galvani 		meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
125f8d9ca18SBeniamino Galvani 
126f8d9ca18SBeniamino Galvani 	if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
127f8d9ca18SBeniamino Galvani 		meson_i2c_add_token(i2c, TOKEN_STOP);
128f8d9ca18SBeniamino Galvani 
129f8d9ca18SBeniamino Galvani 	writel(i2c->tokens[0], &i2c->regs->tok_list0);
130f8d9ca18SBeniamino Galvani 	writel(i2c->tokens[1], &i2c->regs->tok_list1);
131f8d9ca18SBeniamino Galvani }
132f8d9ca18SBeniamino Galvani 
133f8d9ca18SBeniamino Galvani static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
134f8d9ca18SBeniamino Galvani {
135f8d9ca18SBeniamino Galvani 	int token;
136f8d9ca18SBeniamino Galvani 
137f8d9ca18SBeniamino Galvani 	token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
138f8d9ca18SBeniamino Galvani 		TOKEN_SLAVE_ADDR_WRITE;
139f8d9ca18SBeniamino Galvani 
140f8d9ca18SBeniamino Galvani 	writel(msg->addr << 1, &i2c->regs->slave_addr);
141f8d9ca18SBeniamino Galvani 	meson_i2c_add_token(i2c, TOKEN_START);
142f8d9ca18SBeniamino Galvani 	meson_i2c_add_token(i2c, token);
143f8d9ca18SBeniamino Galvani }
144f8d9ca18SBeniamino Galvani 
145f8d9ca18SBeniamino Galvani static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
146f8d9ca18SBeniamino Galvani 			      int last)
147f8d9ca18SBeniamino Galvani {
148f8d9ca18SBeniamino Galvani 	ulong start;
149f8d9ca18SBeniamino Galvani 
150f8d9ca18SBeniamino Galvani 	debug("meson i2c: %s addr %u len %u\n",
151f8d9ca18SBeniamino Galvani 	      (msg->flags & I2C_M_RD) ? "read" : "write",
152f8d9ca18SBeniamino Galvani 	      msg->addr, msg->len);
153f8d9ca18SBeniamino Galvani 
154f8d9ca18SBeniamino Galvani 	i2c->msg = msg;
155f8d9ca18SBeniamino Galvani 	i2c->last = last;
156f8d9ca18SBeniamino Galvani 	i2c->pos = 0;
157f8d9ca18SBeniamino Galvani 	i2c->count = 0;
158f8d9ca18SBeniamino Galvani 
159f8d9ca18SBeniamino Galvani 	meson_i2c_reset_tokens(i2c);
160f8d9ca18SBeniamino Galvani 	meson_i2c_do_start(i2c, msg);
161f8d9ca18SBeniamino Galvani 
162f8d9ca18SBeniamino Galvani 	do {
163f8d9ca18SBeniamino Galvani 		meson_i2c_prepare_xfer(i2c);
164f8d9ca18SBeniamino Galvani 
165f8d9ca18SBeniamino Galvani 		/* start the transfer */
166f8d9ca18SBeniamino Galvani 		setbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
167f8d9ca18SBeniamino Galvani 		start = get_timer(0);
168f8d9ca18SBeniamino Galvani 		while (readl(&i2c->regs->ctrl) & REG_CTRL_STATUS) {
169f8d9ca18SBeniamino Galvani 			if (get_timer(start) > I2C_TIMEOUT_MS) {
170f8d9ca18SBeniamino Galvani 				clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
171f8d9ca18SBeniamino Galvani 				debug("meson i2c: timeout\n");
172f8d9ca18SBeniamino Galvani 				return -ETIMEDOUT;
173f8d9ca18SBeniamino Galvani 			}
174f8d9ca18SBeniamino Galvani 			udelay(1);
175f8d9ca18SBeniamino Galvani 		}
176f8d9ca18SBeniamino Galvani 		meson_i2c_reset_tokens(i2c);
177f8d9ca18SBeniamino Galvani 		clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
178f8d9ca18SBeniamino Galvani 
179f8d9ca18SBeniamino Galvani 		if (readl(&i2c->regs->ctrl) & REG_CTRL_ERROR) {
180f8d9ca18SBeniamino Galvani 			debug("meson i2c: error\n");
181f8d9ca18SBeniamino Galvani 			return -ENXIO;
182f8d9ca18SBeniamino Galvani 		}
183f8d9ca18SBeniamino Galvani 
184f8d9ca18SBeniamino Galvani 		if ((msg->flags & I2C_M_RD) && i2c->count) {
185f8d9ca18SBeniamino Galvani 			meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
186f8d9ca18SBeniamino Galvani 					   i2c->count);
187f8d9ca18SBeniamino Galvani 		}
188f8d9ca18SBeniamino Galvani 		i2c->pos += i2c->count;
189f8d9ca18SBeniamino Galvani 	} while (i2c->pos < msg->len);
190f8d9ca18SBeniamino Galvani 
191f8d9ca18SBeniamino Galvani 	return 0;
192f8d9ca18SBeniamino Galvani }
193f8d9ca18SBeniamino Galvani 
194f8d9ca18SBeniamino Galvani static int meson_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
195f8d9ca18SBeniamino Galvani 			  int nmsgs)
196f8d9ca18SBeniamino Galvani {
197f8d9ca18SBeniamino Galvani 	struct meson_i2c *i2c = dev_get_priv(bus);
198f8d9ca18SBeniamino Galvani 	int i, ret = 0;
199f8d9ca18SBeniamino Galvani 
200f8d9ca18SBeniamino Galvani 	for (i = 0; i < nmsgs; i++) {
201f8d9ca18SBeniamino Galvani 		ret = meson_i2c_xfer_msg(i2c, msg + i, i == nmsgs - 1);
202f8d9ca18SBeniamino Galvani 		if (ret)
203f8d9ca18SBeniamino Galvani 			return -EREMOTEIO;
204f8d9ca18SBeniamino Galvani 	}
205f8d9ca18SBeniamino Galvani 
206f8d9ca18SBeniamino Galvani 	return 0;
207f8d9ca18SBeniamino Galvani }
208f8d9ca18SBeniamino Galvani 
209f8d9ca18SBeniamino Galvani static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
210f8d9ca18SBeniamino Galvani {
211f8d9ca18SBeniamino Galvani 	struct meson_i2c *i2c = dev_get_priv(bus);
212f8d9ca18SBeniamino Galvani 	unsigned int clk_rate = MESON_I2C_CLK_RATE;
213f8d9ca18SBeniamino Galvani 	unsigned int div;
214f8d9ca18SBeniamino Galvani 
215f8d9ca18SBeniamino Galvani 	div = DIV_ROUND_UP(clk_rate, speed * 4);
216f8d9ca18SBeniamino Galvani 
217f8d9ca18SBeniamino Galvani 	/* clock divider has 12 bits */
218f8d9ca18SBeniamino Galvani 	if (div >= (1 << 12)) {
219f8d9ca18SBeniamino Galvani 		debug("meson i2c: requested bus frequency too low\n");
220f8d9ca18SBeniamino Galvani 		div = (1 << 12) - 1;
221f8d9ca18SBeniamino Galvani 	}
222f8d9ca18SBeniamino Galvani 
223f8d9ca18SBeniamino Galvani 	clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIV_MASK,
224f8d9ca18SBeniamino Galvani 			(div & GENMASK(9, 0)) << REG_CTRL_CLKDIV_SHIFT);
225f8d9ca18SBeniamino Galvani 
226f8d9ca18SBeniamino Galvani 	clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIVEXT_MASK,
227f8d9ca18SBeniamino Galvani 			(div >> 10) << REG_CTRL_CLKDIVEXT_SHIFT);
228f8d9ca18SBeniamino Galvani 
229f8d9ca18SBeniamino Galvani 	debug("meson i2c: set clk %u, src %u, div %u\n", speed, clk_rate, div);
230f8d9ca18SBeniamino Galvani 
231f8d9ca18SBeniamino Galvani 	return 0;
232f8d9ca18SBeniamino Galvani }
233f8d9ca18SBeniamino Galvani 
234f8d9ca18SBeniamino Galvani static int meson_i2c_probe(struct udevice *bus)
235f8d9ca18SBeniamino Galvani {
236f8d9ca18SBeniamino Galvani 	struct meson_i2c *i2c = dev_get_priv(bus);
237f8d9ca18SBeniamino Galvani 
238f8d9ca18SBeniamino Galvani 	i2c->regs = dev_read_addr_ptr(bus);
239f8d9ca18SBeniamino Galvani 	clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
240f8d9ca18SBeniamino Galvani 
241f8d9ca18SBeniamino Galvani 	return 0;
242f8d9ca18SBeniamino Galvani }
243f8d9ca18SBeniamino Galvani 
244f8d9ca18SBeniamino Galvani static const struct dm_i2c_ops meson_i2c_ops = {
245f8d9ca18SBeniamino Galvani 	.xfer          = meson_i2c_xfer,
246f8d9ca18SBeniamino Galvani 	.set_bus_speed = meson_i2c_set_bus_speed,
247f8d9ca18SBeniamino Galvani };
248f8d9ca18SBeniamino Galvani 
249f8d9ca18SBeniamino Galvani static const struct udevice_id meson_i2c_ids[] = {
250f8d9ca18SBeniamino Galvani 	{ .compatible = "amlogic,meson6-i2c" },
251f8d9ca18SBeniamino Galvani 	{ .compatible = "amlogic,meson-gx-i2c" },
252f8d9ca18SBeniamino Galvani 	{ .compatible = "amlogic,meson-gxbb-i2c" },
253f8d9ca18SBeniamino Galvani 	{ }
254f8d9ca18SBeniamino Galvani };
255f8d9ca18SBeniamino Galvani 
256f8d9ca18SBeniamino Galvani U_BOOT_DRIVER(i2c_meson) = {
257f8d9ca18SBeniamino Galvani 	.name = "i2c_meson",
258f8d9ca18SBeniamino Galvani 	.id   = UCLASS_I2C,
259f8d9ca18SBeniamino Galvani 	.of_match = meson_i2c_ids,
260f8d9ca18SBeniamino Galvani 	.probe = meson_i2c_probe,
261f8d9ca18SBeniamino Galvani 	.priv_auto_alloc_size = sizeof(struct meson_i2c),
262f8d9ca18SBeniamino Galvani 	.ops = &meson_i2c_ops,
263f8d9ca18SBeniamino Galvani };
264