1 // SPDX-License-Identifier: GPL-2.0+
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
14 #define I2C_TIMEOUT_MS 100
16 /* Control register fields */
17 #define REG_CTRL_START BIT(0)
18 #define REG_CTRL_ACK_IGNORE BIT(1)
19 #define REG_CTRL_STATUS BIT(2)
20 #define REG_CTRL_ERROR BIT(3)
21 #define REG_CTRL_CLKDIV_SHIFT 12
22 #define REG_CTRL_CLKDIV_MASK GENMASK(21, 12)
23 #define REG_CTRL_CLKDIVEXT_SHIFT 28
24 #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, 28)
29 TOKEN_SLAVE_ADDR_WRITE,
30 TOKEN_SLAVE_ADDR_READ,
47 struct meson_i2c_data {
48 unsigned char div_factor;
52 const struct meson_i2c_data *data;
54 struct i2c_regs *regs;
55 struct i2c_msg *msg; /* Current I2C message */
56 bool last; /* Whether the message is the last */
57 uint count; /* Number of bytes in the current transfer */
58 uint pos; /* Position of current transfer in message */
59 u32 tokens[2]; /* Sequence of tokens to be written */
60 uint num_tokens; /* Number of tokens to be written */
63 static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
70 static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
72 if (i2c->num_tokens < 8)
73 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
75 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
81 * Retrieve data for the current transfer (which can be at most 8
82 * bytes) from the device internal buffer.
84 static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len)
89 rdata0 = readl(&i2c->regs->tok_rdata0);
90 rdata1 = readl(&i2c->regs->tok_rdata1);
92 debug("meson i2c: read data %08x %08x len %d\n", rdata0, rdata1, len);
94 for (i = 0; i < min(4, len); i++)
95 *buf++ = (rdata0 >> i * 8) & 0xff;
97 for (i = 4; i < min(8, len); i++)
98 *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
102 * Write data for the current transfer (which can be at most 8 bytes)
103 * to the device internal buffer.
105 static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len)
107 u32 wdata0 = 0, wdata1 = 0;
110 for (i = 0; i < min(4, len); i++)
111 wdata0 |= *buf++ << (i * 8);
113 for (i = 4; i < min(8, len); i++)
114 wdata1 |= *buf++ << ((i - 4) * 8);
116 writel(wdata0, &i2c->regs->tok_wdata0);
117 writel(wdata1, &i2c->regs->tok_wdata1);
119 debug("meson i2c: write data %08x %08x len %d\n", wdata0, wdata1, len);
123 * Prepare the next transfer: pick the next 8 bytes in the remaining
124 * part of message and write tokens and data (if needed) to the
127 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
129 bool write = !(i2c->msg->flags & I2C_M_RD);
132 i2c->count = min(i2c->msg->len - i2c->pos, 8u);
134 for (i = 0; i + 1 < i2c->count; i++)
135 meson_i2c_add_token(i2c, TOKEN_DATA);
138 if (write || i2c->pos + i2c->count < i2c->msg->len)
139 meson_i2c_add_token(i2c, TOKEN_DATA);
141 meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
145 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
147 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
148 meson_i2c_add_token(i2c, TOKEN_STOP);
150 writel(i2c->tokens[0], &i2c->regs->tok_list0);
151 writel(i2c->tokens[1], &i2c->regs->tok_list1);
154 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
158 token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
159 TOKEN_SLAVE_ADDR_WRITE;
161 writel(msg->addr << 1, &i2c->regs->slave_addr);
162 meson_i2c_add_token(i2c, TOKEN_START);
163 meson_i2c_add_token(i2c, token);
166 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
171 debug("meson i2c: %s addr %u len %u\n",
172 (msg->flags & I2C_M_RD) ? "read" : "write",
173 msg->addr, msg->len);
180 meson_i2c_reset_tokens(i2c);
181 meson_i2c_do_start(i2c, msg);
184 meson_i2c_prepare_xfer(i2c);
186 /* start the transfer */
187 setbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
188 start = get_timer(0);
189 while (readl(&i2c->regs->ctrl) & REG_CTRL_STATUS) {
190 if (get_timer(start) > I2C_TIMEOUT_MS) {
191 clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
192 debug("meson i2c: timeout\n");
197 meson_i2c_reset_tokens(i2c);
198 clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
200 if (readl(&i2c->regs->ctrl) & REG_CTRL_ERROR) {
201 debug("meson i2c: error\n");
205 if ((msg->flags & I2C_M_RD) && i2c->count) {
206 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
209 i2c->pos += i2c->count;
210 } while (i2c->pos < msg->len);
215 static int meson_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
218 struct meson_i2c *i2c = dev_get_priv(bus);
221 for (i = 0; i < nmsgs; i++) {
222 ret = meson_i2c_xfer_msg(i2c, msg + i, i == nmsgs - 1);
230 static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
232 struct meson_i2c *i2c = dev_get_priv(bus);
236 clk_rate = clk_get_rate(&i2c->clk);
237 if (IS_ERR_VALUE(clk_rate))
240 div = DIV_ROUND_UP(clk_rate, speed * i2c->data->div_factor);
242 /* clock divider has 12 bits */
243 if (div >= (1 << 12)) {
244 debug("meson i2c: requested bus frequency too low\n");
248 clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIV_MASK,
249 (div & GENMASK(9, 0)) << REG_CTRL_CLKDIV_SHIFT);
251 clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIVEXT_MASK,
252 (div >> 10) << REG_CTRL_CLKDIVEXT_SHIFT);
254 debug("meson i2c: set clk %u, src %lu, div %u\n", speed, clk_rate, div);
259 static int meson_i2c_probe(struct udevice *bus)
261 struct meson_i2c *i2c = dev_get_priv(bus);
264 i2c->data = (const struct meson_i2c_data *)dev_get_driver_data(bus);
266 ret = clk_get_by_index(bus, 0, &i2c->clk);
270 ret = clk_enable(&i2c->clk);
274 i2c->regs = dev_read_addr_ptr(bus);
275 clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
280 static const struct dm_i2c_ops meson_i2c_ops = {
281 .xfer = meson_i2c_xfer,
282 .set_bus_speed = meson_i2c_set_bus_speed,
285 static const struct meson_i2c_data i2c_meson6_data = {
289 static const struct meson_i2c_data i2c_gxbb_data = {
293 static const struct meson_i2c_data i2c_axg_data = {
297 static const struct udevice_id meson_i2c_ids[] = {
298 {.compatible = "amlogic,meson6-i2c", .data = (ulong)&i2c_meson6_data},
299 {.compatible = "amlogic,meson-gx-i2c", .data = (ulong)&i2c_gxbb_data},
300 {.compatible = "amlogic,meson-gxbb-i2c", .data = (ulong)&i2c_gxbb_data},
301 {.compatible = "amlogic,meson-axg-i2c", .data = (ulong)&i2c_axg_data},
305 U_BOOT_DRIVER(i2c_meson) = {
308 .of_match = meson_i2c_ids,
309 .probe = meson_i2c_probe,
310 .priv_auto = sizeof(struct meson_i2c),
311 .ops = &meson_i2c_ops,