2 * (C) Copyright 2015 Google, Inc
4 * (C) Copyright 2008-2014 Rockchip Electronics
5 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
7 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/clock.h>
17 #include <asm/arch/i2c.h>
18 #include <asm/arch/periph.h>
19 #include <dm/pinctrl.h>
20 #include <linux/sizes.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 #define I2C_TIMEOUT_MS 100
26 #define I2C_RETRY_COUNT 3
28 /* rk i2c fifo max transfer bytes */
29 #define RK_I2C_FIFO_SIZE 32
33 struct i2c_regs *regs;
37 static inline void rk_i2c_get_div(int div, int *divh, int *divl)
43 *divh = DIV_ROUND_UP(div, 2);
47 * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
48 * SCL = PCLK / SCLK Divisor
51 static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
56 /* First get i2c rate from pclk */
57 i2c_rate = clk_get_rate(&i2c->clk);
59 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
63 rk_i2c_get_div(div, &divh, &divl);
64 writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
66 debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
68 debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
69 debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
72 static void rk_i2c_show_regs(struct i2c_regs *regs)
77 debug("i2c_con: 0x%08x\n", readl(®s->con));
78 debug("i2c_clkdiv: 0x%08x\n", readl(®s->clkdiv));
79 debug("i2c_mrxaddr: 0x%08x\n", readl(®s->mrxaddr));
80 debug("i2c_mrxraddR: 0x%08x\n", readl(®s->mrxraddr));
81 debug("i2c_mtxcnt: 0x%08x\n", readl(®s->mtxcnt));
82 debug("i2c_mrxcnt: 0x%08x\n", readl(®s->mrxcnt));
83 debug("i2c_ien: 0x%08x\n", readl(®s->ien));
84 debug("i2c_ipd: 0x%08x\n", readl(®s->ipd));
85 debug("i2c_fcnt: 0x%08x\n", readl(®s->fcnt));
86 for (i = 0; i < 8; i++)
87 debug("i2c_txdata%d: 0x%08x\n", i, readl(®s->txdata[i]));
88 for (i = 0; i < 8; i++)
89 debug("i2c_rxdata%d: 0x%08x\n", i, readl(®s->rxdata[i]));
93 static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
95 struct i2c_regs *regs = i2c->regs;
98 debug("I2c Send Start bit.\n");
99 writel(I2C_IPD_ALL_CLEAN, ®s->ipd);
101 writel(I2C_CON_EN | I2C_CON_START, ®s->con);
102 writel(I2C_STARTIEN, ®s->ien);
104 start = get_timer(0);
106 if (readl(®s->ipd) & I2C_STARTIPD) {
107 writel(I2C_STARTIPD, ®s->ipd);
110 if (get_timer(start) > I2C_TIMEOUT_MS) {
111 debug("I2C Send Start Bit Timeout\n");
112 rk_i2c_show_regs(regs);
121 static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
123 struct i2c_regs *regs = i2c->regs;
126 debug("I2c Send Stop bit.\n");
127 writel(I2C_IPD_ALL_CLEAN, ®s->ipd);
129 writel(I2C_CON_EN | I2C_CON_STOP, ®s->con);
130 writel(I2C_CON_STOP, ®s->ien);
132 start = get_timer(0);
134 if (readl(®s->ipd) & I2C_STOPIPD) {
135 writel(I2C_STOPIPD, ®s->ipd);
138 if (get_timer(start) > I2C_TIMEOUT_MS) {
139 debug("I2C Send Start Bit Timeout\n");
140 rk_i2c_show_regs(regs);
149 static inline void rk_i2c_disable(struct rk_i2c *i2c)
151 writel(0, &i2c->regs->con);
154 static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
155 uchar *buf, uint b_len)
157 struct i2c_regs *regs = i2c->regs;
159 uint bytes_remain_len = b_len;
160 uint bytes_xferred = 0;
161 uint words_xferred = 0;
168 debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
169 chip, reg, r_len, b_len);
171 err = rk_i2c_send_start_bit(i2c);
175 writel(I2C_MRXADDR_SET(1, chip << 1 | 1), ®s->mrxaddr);
177 writel(0, ®s->mrxraddr);
178 } else if (r_len < 4) {
179 writel(I2C_MRXRADDR_SET(r_len, reg), ®s->mrxraddr);
181 debug("I2C Read: addr len %d not supported\n", r_len);
185 while (bytes_remain_len) {
186 if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
187 con = I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TRX);
190 con = I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TRX) |
192 bytes_xferred = bytes_remain_len;
194 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
196 writel(con, ®s->con);
197 writel(bytes_xferred, ®s->mrxcnt);
198 writel(I2C_MBRFIEN | I2C_NAKRCVIEN, ®s->ien);
200 start = get_timer(0);
202 if (readl(®s->ipd) & I2C_NAKRCVIPD) {
203 writel(I2C_NAKRCVIPD, ®s->ipd);
206 if (readl(®s->ipd) & I2C_MBRFIPD) {
207 writel(I2C_MBRFIPD, ®s->ipd);
210 if (get_timer(start) > I2C_TIMEOUT_MS) {
211 debug("I2C Read Data Timeout\n");
213 rk_i2c_show_regs(regs);
219 for (i = 0; i < words_xferred; i++) {
220 rxdata = readl(®s->rxdata[i]);
221 debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
222 for (j = 0; j < 4; j++) {
223 if ((i * 4 + j) == bytes_xferred)
225 *pbuf++ = (rxdata >> (j * 8)) & 0xff;
229 bytes_remain_len -= bytes_xferred;
230 debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
234 rk_i2c_send_stop_bit(i2c);
240 static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
241 uchar *buf, uint b_len)
243 struct i2c_regs *regs = i2c->regs;
246 uint bytes_remain_len = b_len + r_len + 1;
247 uint bytes_xferred = 0;
248 uint words_xferred = 0;
253 debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
254 chip, reg, r_len, b_len);
255 err = rk_i2c_send_start_bit(i2c);
259 while (bytes_remain_len) {
260 if (bytes_remain_len > RK_I2C_FIFO_SIZE)
261 bytes_xferred = RK_I2C_FIFO_SIZE;
263 bytes_xferred = bytes_remain_len;
264 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
266 for (i = 0; i < words_xferred; i++) {
268 for (j = 0; j < 4; j++) {
269 if ((i * 4 + j) == bytes_xferred)
272 if (i == 0 && j == 0 && pbuf == buf) {
273 txdata |= (chip << 1);
274 } else if (i == 0 && j <= r_len && pbuf == buf) {
276 (0xff << ((j - 1) * 8))) << 8;
278 txdata |= (*pbuf++)<<(j * 8);
281 writel(txdata, ®s->txdata[i]);
282 debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
285 writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), ®s->con);
286 writel(bytes_xferred, ®s->mtxcnt);
287 writel(I2C_MBTFIEN | I2C_NAKRCVIEN, ®s->ien);
289 start = get_timer(0);
291 if (readl(®s->ipd) & I2C_NAKRCVIPD) {
292 writel(I2C_NAKRCVIPD, ®s->ipd);
295 if (readl(®s->ipd) & I2C_MBTFIPD) {
296 writel(I2C_MBTFIPD, ®s->ipd);
299 if (get_timer(start) > I2C_TIMEOUT_MS) {
300 debug("I2C Write Data Timeout\n");
302 rk_i2c_show_regs(regs);
308 bytes_remain_len -= bytes_xferred;
309 debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
313 rk_i2c_send_stop_bit(i2c);
319 static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
322 struct rk_i2c *i2c = dev_get_priv(bus);
325 debug("i2c_xfer: %d messages\n", nmsgs);
326 for (; nmsgs > 0; nmsgs--, msg++) {
327 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
328 if (msg->flags & I2C_M_RD) {
329 ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
332 ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
336 debug("i2c_write: error sending\n");
344 int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
346 struct rk_i2c *i2c = dev_get_priv(bus);
348 rk_i2c_set_clk(i2c, speed);
353 static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
355 struct rk_i2c *priv = dev_get_priv(bus);
358 ret = clk_get_by_index(bus, 0, &priv->clk);
360 debug("%s: Could not get clock for %s: %d\n", __func__,
368 static int rockchip_i2c_probe(struct udevice *bus)
370 struct rk_i2c *priv = dev_get_priv(bus);
372 priv->regs = (void *)devfdt_get_addr(bus);
377 static const struct dm_i2c_ops rockchip_i2c_ops = {
378 .xfer = rockchip_i2c_xfer,
379 .set_bus_speed = rockchip_i2c_set_bus_speed,
382 static const struct udevice_id rockchip_i2c_ids[] = {
383 { .compatible = "rockchip,rk3066-i2c" },
384 { .compatible = "rockchip,rk3188-i2c" },
385 { .compatible = "rockchip,rk3288-i2c" },
386 { .compatible = "rockchip,rk3399-i2c" },
390 U_BOOT_DRIVER(i2c_rockchip) = {
391 .name = "i2c_rockchip",
393 .of_match = rockchip_i2c_ids,
394 .ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
395 .probe = rockchip_i2c_probe,
396 .priv_auto_alloc_size = sizeof(struct rk_i2c),
397 .ops = &rockchip_i2c_ops,