1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
5 * (C) Copyright 2008-2014 Rockchip Electronics
6 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
15 #include <asm/arch-rockchip/clock.h>
16 #include <asm/arch-rockchip/i2c.h>
17 #include <asm/arch-rockchip/periph.h>
18 #include <dm/pinctrl.h>
19 #include <linux/sizes.h>
22 #define I2C_TIMEOUT_MS 100
23 #define I2C_RETRY_COUNT 3
25 /* rk i2c fifo max transfer bytes */
26 #define RK_I2C_FIFO_SIZE 32
30 struct i2c_regs *regs;
40 * @controller_type: i2c controller type
42 struct rk_i2c_soc_data {
46 static inline void rk_i2c_get_div(int div, int *divh, int *divl)
52 *divh = DIV_ROUND_UP(div, 2);
56 * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
57 * SCL = PCLK / SCLK Divisor
60 static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
65 /* First get i2c rate from pclk */
66 i2c_rate = clk_get_rate(&i2c->clk);
68 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
72 rk_i2c_get_div(div, &divh, &divl);
73 writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
75 debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
77 debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
78 debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
81 static void rk_i2c_show_regs(struct i2c_regs *regs)
86 debug("i2c_con: 0x%08x\n", readl(®s->con));
87 debug("i2c_clkdiv: 0x%08x\n", readl(®s->clkdiv));
88 debug("i2c_mrxaddr: 0x%08x\n", readl(®s->mrxaddr));
89 debug("i2c_mrxraddR: 0x%08x\n", readl(®s->mrxraddr));
90 debug("i2c_mtxcnt: 0x%08x\n", readl(®s->mtxcnt));
91 debug("i2c_mrxcnt: 0x%08x\n", readl(®s->mrxcnt));
92 debug("i2c_ien: 0x%08x\n", readl(®s->ien));
93 debug("i2c_ipd: 0x%08x\n", readl(®s->ipd));
94 debug("i2c_fcnt: 0x%08x\n", readl(®s->fcnt));
95 for (i = 0; i < 8; i++)
96 debug("i2c_txdata%d: 0x%08x\n", i, readl(®s->txdata[i]));
97 for (i = 0; i < 8; i++)
98 debug("i2c_rxdata%d: 0x%08x\n", i, readl(®s->rxdata[i]));
102 static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
104 struct i2c_regs *regs = i2c->regs;
107 debug("I2c Send Start bit.\n");
108 writel(I2C_IPD_ALL_CLEAN, ®s->ipd);
110 writel(I2C_CON_EN | I2C_CON_START, ®s->con);
111 writel(I2C_STARTIEN, ®s->ien);
113 start = get_timer(0);
115 if (readl(®s->ipd) & I2C_STARTIPD) {
116 writel(I2C_STARTIPD, ®s->ipd);
119 if (get_timer(start) > I2C_TIMEOUT_MS) {
120 debug("I2C Send Start Bit Timeout\n");
121 rk_i2c_show_regs(regs);
130 static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
132 struct i2c_regs *regs = i2c->regs;
135 debug("I2c Send Stop bit.\n");
136 writel(I2C_IPD_ALL_CLEAN, ®s->ipd);
138 writel(I2C_CON_EN | I2C_CON_STOP, ®s->con);
139 writel(I2C_CON_STOP, ®s->ien);
141 start = get_timer(0);
143 if (readl(®s->ipd) & I2C_STOPIPD) {
144 writel(I2C_STOPIPD, ®s->ipd);
147 if (get_timer(start) > I2C_TIMEOUT_MS) {
148 debug("I2C Send Start Bit Timeout\n");
149 rk_i2c_show_regs(regs);
158 static inline void rk_i2c_disable(struct rk_i2c *i2c)
160 writel(0, &i2c->regs->con);
163 static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
164 uchar *buf, uint b_len)
166 struct i2c_regs *regs = i2c->regs;
168 uint bytes_remain_len = b_len;
169 uint bytes_xferred = 0;
170 uint words_xferred = 0;
176 bool snd_chunk = false;
178 debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
179 chip, reg, r_len, b_len);
181 err = rk_i2c_send_start_bit(i2c);
185 writel(I2C_MRXADDR_SET(1, chip << 1 | 1), ®s->mrxaddr);
187 writel(0, ®s->mrxraddr);
188 } else if (r_len < 4) {
189 writel(I2C_MRXRADDR_SET(r_len, reg), ®s->mrxraddr);
191 debug("I2C Read: addr len %d not supported\n", r_len);
195 while (bytes_remain_len) {
196 if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
201 * The hw can read up to 32 bytes at a time. If we need
202 * more than one chunk, send an ACK after the last byte.
204 con = I2C_CON_EN | I2C_CON_LASTACK;
205 bytes_xferred = bytes_remain_len;
207 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
210 * make sure we are in plain RX mode if we read a second chunk
213 con |= I2C_CON_MOD(I2C_MODE_RX);
215 con |= I2C_CON_MOD(I2C_MODE_TRX);
217 writel(con, ®s->con);
218 writel(bytes_xferred, ®s->mrxcnt);
219 writel(I2C_MBRFIEN | I2C_NAKRCVIEN, ®s->ien);
221 start = get_timer(0);
223 if (readl(®s->ipd) & I2C_NAKRCVIPD) {
224 writel(I2C_NAKRCVIPD, ®s->ipd);
227 if (readl(®s->ipd) & I2C_MBRFIPD) {
228 writel(I2C_MBRFIPD, ®s->ipd);
231 if (get_timer(start) > I2C_TIMEOUT_MS) {
232 debug("I2C Read Data Timeout\n");
234 rk_i2c_show_regs(regs);
240 for (i = 0; i < words_xferred; i++) {
241 rxdata = readl(®s->rxdata[i]);
242 debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
243 for (j = 0; j < 4; j++) {
244 if ((i * 4 + j) == bytes_xferred)
246 *pbuf++ = (rxdata >> (j * 8)) & 0xff;
250 bytes_remain_len -= bytes_xferred;
252 debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
261 static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
262 uchar *buf, uint b_len)
264 struct i2c_regs *regs = i2c->regs;
267 uint bytes_remain_len = b_len + r_len + 1;
268 uint bytes_xferred = 0;
269 uint words_xferred = 0;
274 debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
275 chip, reg, r_len, b_len);
276 err = rk_i2c_send_start_bit(i2c);
280 while (bytes_remain_len) {
281 if (bytes_remain_len > RK_I2C_FIFO_SIZE)
282 bytes_xferred = RK_I2C_FIFO_SIZE;
284 bytes_xferred = bytes_remain_len;
285 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
287 for (i = 0; i < words_xferred; i++) {
289 for (j = 0; j < 4; j++) {
290 if ((i * 4 + j) == bytes_xferred)
293 if (i == 0 && j == 0 && pbuf == buf) {
294 txdata |= (chip << 1);
295 } else if (i == 0 && j <= r_len && pbuf == buf) {
297 (0xff << ((j - 1) * 8))) << 8;
299 txdata |= (*pbuf++)<<(j * 8);
302 writel(txdata, ®s->txdata[i]);
303 debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
306 writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), ®s->con);
307 writel(bytes_xferred, ®s->mtxcnt);
308 writel(I2C_MBTFIEN | I2C_NAKRCVIEN, ®s->ien);
310 start = get_timer(0);
312 if (readl(®s->ipd) & I2C_NAKRCVIPD) {
313 writel(I2C_NAKRCVIPD, ®s->ipd);
316 if (readl(®s->ipd) & I2C_MBTFIPD) {
317 writel(I2C_MBTFIPD, ®s->ipd);
320 if (get_timer(start) > I2C_TIMEOUT_MS) {
321 debug("I2C Write Data Timeout\n");
323 rk_i2c_show_regs(regs);
329 bytes_remain_len -= bytes_xferred;
330 debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
339 static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
342 struct rk_i2c *i2c = dev_get_priv(bus);
345 debug("i2c_xfer: %d messages\n", nmsgs);
346 for (; nmsgs > 0; nmsgs--, msg++) {
347 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
348 if (msg->flags & I2C_M_RD) {
349 ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
352 ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
356 debug("i2c_write: error sending\n");
361 rk_i2c_send_stop_bit(i2c);
367 int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
369 struct rk_i2c *i2c = dev_get_priv(bus);
371 rk_i2c_set_clk(i2c, speed);
376 static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
378 struct rk_i2c *priv = dev_get_priv(bus);
381 ret = clk_get_by_index(bus, 0, &priv->clk);
383 debug("%s: Could not get clock for %s: %d\n", __func__,
391 static int rockchip_i2c_probe(struct udevice *bus)
393 struct rk_i2c *priv = dev_get_priv(bus);
394 struct rk_i2c_soc_data *soc_data;
395 struct udevice *pinctrl;
399 priv->regs = dev_read_addr_ptr(bus);
401 soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus);
403 if (soc_data->controller_type == RK_I2C_LEGACY) {
404 ret = dev_read_alias_seq(bus, &bus_nr);
406 debug("%s: Could not get alias for %s: %d\n",
407 __func__, bus->name, ret);
411 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
413 debug("%s: Cannot find pinctrl device\n", __func__);
417 /* pinctrl will switch I2C to new type */
418 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr);
420 debug("%s: Failed to switch I2C to new type %s: %d\n",
421 __func__, bus->name, ret);
429 static const struct dm_i2c_ops rockchip_i2c_ops = {
430 .xfer = rockchip_i2c_xfer,
431 .set_bus_speed = rockchip_i2c_set_bus_speed,
434 static const struct rk_i2c_soc_data rk3066_soc_data = {
435 .controller_type = RK_I2C_LEGACY,
438 static const struct rk_i2c_soc_data rk3188_soc_data = {
439 .controller_type = RK_I2C_LEGACY,
442 static const struct rk_i2c_soc_data rk3228_soc_data = {
443 .controller_type = RK_I2C_NEW,
446 static const struct rk_i2c_soc_data rk3288_soc_data = {
447 .controller_type = RK_I2C_NEW,
450 static const struct rk_i2c_soc_data rk3328_soc_data = {
451 .controller_type = RK_I2C_NEW,
454 static const struct rk_i2c_soc_data rk3399_soc_data = {
455 .controller_type = RK_I2C_NEW,
458 static const struct udevice_id rockchip_i2c_ids[] = {
460 .compatible = "rockchip,rk3066-i2c",
461 .data = (ulong)&rk3066_soc_data,
464 .compatible = "rockchip,rk3188-i2c",
465 .data = (ulong)&rk3188_soc_data,
468 .compatible = "rockchip,rk3228-i2c",
469 .data = (ulong)&rk3228_soc_data,
472 .compatible = "rockchip,rk3288-i2c",
473 .data = (ulong)&rk3288_soc_data,
476 .compatible = "rockchip,rk3328-i2c",
477 .data = (ulong)&rk3328_soc_data,
480 .compatible = "rockchip,rk3399-i2c",
481 .data = (ulong)&rk3399_soc_data,
486 U_BOOT_DRIVER(i2c_rockchip) = {
487 .name = "i2c_rockchip",
489 .of_match = rockchip_i2c_ids,
490 .ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
491 .probe = rockchip_i2c_probe,
492 .priv_auto_alloc_size = sizeof(struct rk_i2c),
493 .ops = &rockchip_i2c_ops,