xref: /rk3399_rockchip-uboot/drivers/i2c/rk_i2c.c (revision 88d32174b464f6c3e5de6eb415f6cbe6b3c7d56c)
134374699SSimon Glass /*
234374699SSimon Glass  * (C) Copyright 2015 Google, Inc
334374699SSimon Glass  *
434374699SSimon Glass  * (C) Copyright 2008-2014 Rockchip Electronics
534374699SSimon Glass  * Peter, Software Engineering, <superpeter.cai@gmail.com>.
634374699SSimon Glass  *
734374699SSimon Glass  * SPDX-License-Identifier:     GPL-2.0+
834374699SSimon Glass  */
934374699SSimon Glass 
1034374699SSimon Glass #include <common.h>
1134374699SSimon Glass #include <clk.h>
1234374699SSimon Glass #include <dm.h>
1334374699SSimon Glass #include <errno.h>
1434374699SSimon Glass #include <i2c.h>
1534374699SSimon Glass #include <asm/io.h>
1634374699SSimon Glass #include <asm/arch/clock.h>
1734374699SSimon Glass #include <asm/arch/i2c.h>
1834374699SSimon Glass #include <asm/arch/periph.h>
1934374699SSimon Glass #include <dm/pinctrl.h>
2034374699SSimon Glass #include <linux/sizes.h>
2134374699SSimon Glass 
2234374699SSimon Glass DECLARE_GLOBAL_DATA_PTR;
2334374699SSimon Glass 
2434374699SSimon Glass /* i2c timerout */
2534374699SSimon Glass #define I2C_TIMEOUT_MS		100
2634374699SSimon Glass #define I2C_RETRY_COUNT		3
2734374699SSimon Glass 
2834374699SSimon Glass /* rk i2c fifo max transfer bytes */
2934374699SSimon Glass #define RK_I2C_FIFO_SIZE	32
3034374699SSimon Glass 
3134374699SSimon Glass struct rk_i2c {
32135aa950SStephen Warren 	struct clk clk;
3334374699SSimon Glass 	struct i2c_regs *regs;
3434374699SSimon Glass 	unsigned int speed;
353d5347f8SDavid Wu 	unsigned int cfg;
3634374699SSimon Glass };
3734374699SSimon Glass 
383d5347f8SDavid Wu struct i2c_spec_values {
393d5347f8SDavid Wu 	unsigned int min_low_ns;
403d5347f8SDavid Wu 	unsigned int min_high_ns;
413d5347f8SDavid Wu 	unsigned int max_rise_ns;
423d5347f8SDavid Wu 	unsigned int max_fall_ns;
433d5347f8SDavid Wu };
443d5347f8SDavid Wu 
453d5347f8SDavid Wu enum {
463d5347f8SDavid Wu 	RK_I2C_VERSION0 = 0,
473d5347f8SDavid Wu 	RK_I2C_VERSION1,
483d5347f8SDavid Wu 	RK_I2C_VERSION5 = 5,
493d5347f8SDavid Wu };
503d5347f8SDavid Wu 
513d5347f8SDavid Wu /********************* Private Variable Definition ***************************/
523d5347f8SDavid Wu 
533d5347f8SDavid Wu static const struct i2c_spec_values standard_mode_spec = {
543d5347f8SDavid Wu 	.min_low_ns = 4700,
553d5347f8SDavid Wu 	.min_high_ns = 4000,
563d5347f8SDavid Wu 	.max_rise_ns = 1000,
573d5347f8SDavid Wu 	.max_fall_ns = 300,
583d5347f8SDavid Wu };
593d5347f8SDavid Wu 
603d5347f8SDavid Wu static const struct i2c_spec_values fast_mode_spec = {
613d5347f8SDavid Wu 	.min_low_ns = 1300,
623d5347f8SDavid Wu 	.min_high_ns = 600,
633d5347f8SDavid Wu 	.max_rise_ns = 300,
643d5347f8SDavid Wu 	.max_fall_ns = 300,
653d5347f8SDavid Wu };
663d5347f8SDavid Wu 
673d5347f8SDavid Wu static const struct i2c_spec_values fast_modeplus_spec = {
683d5347f8SDavid Wu 	.min_low_ns = 500,
693d5347f8SDavid Wu 	.min_high_ns = 260,
703d5347f8SDavid Wu 	.max_rise_ns = 120,
713d5347f8SDavid Wu 	.max_fall_ns = 120,
723d5347f8SDavid Wu };
733d5347f8SDavid Wu 
rk_i2c_get_spec(unsigned int speed)743d5347f8SDavid Wu static const struct i2c_spec_values *rk_i2c_get_spec(unsigned int speed)
7534374699SSimon Glass {
763d5347f8SDavid Wu 	if (speed == 1000)
773d5347f8SDavid Wu 		return &fast_modeplus_spec;
783d5347f8SDavid Wu 	else if (speed == 400)
793d5347f8SDavid Wu 		return &fast_mode_spec;
8034374699SSimon Glass 	else
813d5347f8SDavid Wu 		return &standard_mode_spec;
8234374699SSimon Glass }
8334374699SSimon Glass 
rk_i2c_show_regs(struct i2c_regs * regs)8434374699SSimon Glass static void rk_i2c_show_regs(struct i2c_regs *regs)
8534374699SSimon Glass {
8634374699SSimon Glass #ifdef DEBUG
8734374699SSimon Glass 	uint i;
8834374699SSimon Glass 
8934374699SSimon Glass 	debug("i2c_con: 0x%08x\n", readl(&regs->con));
9034374699SSimon Glass 	debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
9134374699SSimon Glass 	debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
9234374699SSimon Glass 	debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
9334374699SSimon Glass 	debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
9434374699SSimon Glass 	debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
9534374699SSimon Glass 	debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
9634374699SSimon Glass 	debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
9734374699SSimon Glass 	debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
9834374699SSimon Glass 	for (i = 0; i < 8; i++)
9934374699SSimon Glass 		debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
10034374699SSimon Glass 	for (i = 0; i < 8; i++)
10134374699SSimon Glass 		debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
10234374699SSimon Glass #endif
10334374699SSimon Glass }
10434374699SSimon Glass 
rk_i2c_get_div(int div,int * divh,int * divl)1053d5347f8SDavid Wu static inline void rk_i2c_get_div(int div, int *divh, int *divl)
1063d5347f8SDavid Wu {
1073d5347f8SDavid Wu 	*divl = div / 2;
1083d5347f8SDavid Wu 	if (div % 2 == 0)
1093d5347f8SDavid Wu 		*divh = div / 2;
1103d5347f8SDavid Wu 	else
1113d5347f8SDavid Wu 		*divh = DIV_ROUND_UP(div, 2);
1123d5347f8SDavid Wu }
1133d5347f8SDavid Wu 
1143d5347f8SDavid Wu /*
1153d5347f8SDavid Wu  * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
1163d5347f8SDavid Wu  * SCL = PCLK / SCLK Divisor
1173d5347f8SDavid Wu  * i2c_rate = PCLK
1183d5347f8SDavid Wu  */
rk_i2c_set_clk(struct rk_i2c * i2c,unsigned int scl_rate)1193d5347f8SDavid Wu static void rk_i2c_set_clk(struct rk_i2c *i2c, unsigned int scl_rate)
1203d5347f8SDavid Wu {
1213d5347f8SDavid Wu 	unsigned int i2c_rate;
1223d5347f8SDavid Wu 	int div, divl, divh;
1233d5347f8SDavid Wu 
1243d5347f8SDavid Wu 	/* First get i2c rate from pclk */
1253d5347f8SDavid Wu 	i2c_rate = clk_get_rate(&i2c->clk);
1263d5347f8SDavid Wu 
1273d5347f8SDavid Wu 	div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
1283d5347f8SDavid Wu 	divh = 0;
1293d5347f8SDavid Wu 	divl = 0;
1303d5347f8SDavid Wu 	if (div >= 0)
1313d5347f8SDavid Wu 		rk_i2c_get_div(div, &divh, &divl);
1323d5347f8SDavid Wu 	writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
1333d5347f8SDavid Wu 
1343d5347f8SDavid Wu 	debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
1353d5347f8SDavid Wu 	      scl_rate);
1363d5347f8SDavid Wu 	debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
1373d5347f8SDavid Wu 	debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
1383d5347f8SDavid Wu }
1393d5347f8SDavid Wu 
rk_i2c_adapter_clk(struct rk_i2c * i2c,unsigned int scl_rate)1403d5347f8SDavid Wu static int rk_i2c_adapter_clk(struct rk_i2c *i2c, unsigned int scl_rate)
1413d5347f8SDavid Wu {
1423d5347f8SDavid Wu 	const struct i2c_spec_values *spec;
1433d5347f8SDavid Wu 	unsigned int min_total_div, min_low_div, min_high_div, min_hold_div;
1443d5347f8SDavid Wu 	unsigned int low_div, high_div, extra_div, extra_low_div;
1453d5347f8SDavid Wu 	unsigned int min_low_ns, min_high_ns;
1463d5347f8SDavid Wu 	unsigned int start_setup = 0;
1473d5347f8SDavid Wu 	unsigned int i2c_rate = clk_get_rate(&i2c->clk);
1483d5347f8SDavid Wu 	unsigned int speed;
1493d5347f8SDavid Wu 
1503d5347f8SDavid Wu 	debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
1513d5347f8SDavid Wu 	      scl_rate);
1523d5347f8SDavid Wu 
1533d5347f8SDavid Wu 	if (scl_rate <= 100000 && scl_rate >= 1000) {
1543d5347f8SDavid Wu 		start_setup = 1;
1553d5347f8SDavid Wu 		speed = 100;
1563d5347f8SDavid Wu 	} else if (scl_rate <= 400000 && scl_rate >= 100000) {
1573d5347f8SDavid Wu 		speed = 400;
1583d5347f8SDavid Wu 	} else if (scl_rate <= 1000000 && scl_rate > 400000) {
1593d5347f8SDavid Wu 		speed = 1000;
1603d5347f8SDavid Wu 	} else {
1613d5347f8SDavid Wu 		debug("invalid i2c speed : %d\n", scl_rate);
1623d5347f8SDavid Wu 		return -EINVAL;
1633d5347f8SDavid Wu 	}
1643d5347f8SDavid Wu 
1653d5347f8SDavid Wu 	spec = rk_i2c_get_spec(speed);
1663d5347f8SDavid Wu 	i2c_rate = DIV_ROUND_UP(i2c_rate, 1000);
1673d5347f8SDavid Wu 	speed = DIV_ROUND_UP(scl_rate, 1000);
1683d5347f8SDavid Wu 
1693d5347f8SDavid Wu 	min_total_div = DIV_ROUND_UP(i2c_rate, speed * 8);
1703d5347f8SDavid Wu 
1713d5347f8SDavid Wu 	min_high_ns = spec->max_rise_ns + spec->min_high_ns;
1723d5347f8SDavid Wu 	min_high_div = DIV_ROUND_UP(i2c_rate * min_high_ns, 8 * 1000000);
1733d5347f8SDavid Wu 
1743d5347f8SDavid Wu 	min_low_ns = spec->max_fall_ns + spec->min_low_ns;
1753d5347f8SDavid Wu 	min_low_div = DIV_ROUND_UP(i2c_rate * min_low_ns, 8 * 1000000);
1763d5347f8SDavid Wu 
1773d5347f8SDavid Wu 	min_high_div = (min_high_div < 1) ? 2 : min_high_div;
1783d5347f8SDavid Wu 	min_low_div = (min_low_div < 1) ? 2 : min_low_div;
1793d5347f8SDavid Wu 
1803d5347f8SDavid Wu 	min_hold_div = min_high_div + min_low_div;
1813d5347f8SDavid Wu 
1823d5347f8SDavid Wu 	if (min_hold_div >= min_total_div) {
1833d5347f8SDavid Wu 		high_div = min_high_div;
1843d5347f8SDavid Wu 		low_div = min_low_div;
1853d5347f8SDavid Wu 	} else {
1863d5347f8SDavid Wu 		extra_div = min_total_div - min_hold_div;
1873d5347f8SDavid Wu 		extra_low_div = DIV_ROUND_UP(min_low_div * extra_div,
1883d5347f8SDavid Wu 					     min_hold_div);
1893d5347f8SDavid Wu 
1903d5347f8SDavid Wu 		low_div = min_low_div + extra_low_div;
1913d5347f8SDavid Wu 		high_div = min_high_div + (extra_div - extra_low_div);
1923d5347f8SDavid Wu 	}
1933d5347f8SDavid Wu 
1943d5347f8SDavid Wu 	high_div--;
1953d5347f8SDavid Wu 	low_div--;
1963d5347f8SDavid Wu 
1973d5347f8SDavid Wu 	if (high_div > 0xffff || low_div > 0xffff)
1983d5347f8SDavid Wu 		return -EINVAL;
1993d5347f8SDavid Wu 
2003d5347f8SDavid Wu 	/* 1 for data hold/setup time is enough */
2013d5347f8SDavid Wu 	i2c->cfg = I2C_CON_SDA_CFG(1) | I2C_CON_STA_CFG(start_setup);
2023d5347f8SDavid Wu 	writel((high_div << I2C_CLK_DIV_HIGH_SHIFT) | low_div,
2033d5347f8SDavid Wu 	       &i2c->regs->clkdiv);
2043d5347f8SDavid Wu 
2053d5347f8SDavid Wu 	debug("set clk(I2C_TIMING: 0x%08x)\n", i2c->cfg);
2063d5347f8SDavid Wu 	debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
2073d5347f8SDavid Wu 
2083d5347f8SDavid Wu 	return 0;
2093d5347f8SDavid Wu }
2103d5347f8SDavid Wu 
rk_i2c_send_start_bit(struct rk_i2c * i2c,u32 con)21156082ed3SDavid Wu static int rk_i2c_send_start_bit(struct rk_i2c *i2c, u32 con)
21234374699SSimon Glass {
21334374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
21434374699SSimon Glass 	ulong start;
21534374699SSimon Glass 
21634374699SSimon Glass 	debug("I2c Send Start bit.\n");
21734374699SSimon Glass 	writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
21834374699SSimon Glass 
21934374699SSimon Glass 	writel(I2C_STARTIEN, &regs->ien);
2203d5347f8SDavid Wu 	writel(I2C_CON_EN | I2C_CON_START | i2c->cfg | con, &regs->con);
22134374699SSimon Glass 
22234374699SSimon Glass 	start = get_timer(0);
22334374699SSimon Glass 	while (1) {
22434374699SSimon Glass 		if (readl(&regs->ipd) & I2C_STARTIPD) {
22534374699SSimon Glass 			writel(I2C_STARTIPD, &regs->ipd);
22634374699SSimon Glass 			break;
22734374699SSimon Glass 		}
22834374699SSimon Glass 		if (get_timer(start) > I2C_TIMEOUT_MS) {
22934374699SSimon Glass 			debug("I2C Send Start Bit Timeout\n");
23034374699SSimon Glass 			rk_i2c_show_regs(regs);
23134374699SSimon Glass 			return -ETIMEDOUT;
23234374699SSimon Glass 		}
23334374699SSimon Glass 		udelay(1);
23434374699SSimon Glass 	}
23534374699SSimon Glass 
23656082ed3SDavid Wu 	/* clean start bit */
2373d5347f8SDavid Wu 	writel(I2C_CON_EN | i2c->cfg | con, &regs->con);
23856082ed3SDavid Wu 
23934374699SSimon Glass 	return 0;
24034374699SSimon Glass }
24134374699SSimon Glass 
rk_i2c_send_stop_bit(struct rk_i2c * i2c)24234374699SSimon Glass static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
24334374699SSimon Glass {
24434374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
24534374699SSimon Glass 	ulong start;
24634374699SSimon Glass 
24734374699SSimon Glass 	debug("I2c Send Stop bit.\n");
24834374699SSimon Glass 	writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
24934374699SSimon Glass 
2503d5347f8SDavid Wu 	writel(I2C_CON_EN | i2c->cfg | I2C_CON_STOP, &regs->con);
25134374699SSimon Glass 	writel(I2C_CON_STOP, &regs->ien);
25234374699SSimon Glass 
25334374699SSimon Glass 	start = get_timer(0);
25434374699SSimon Glass 	while (1) {
25534374699SSimon Glass 		if (readl(&regs->ipd) & I2C_STOPIPD) {
25634374699SSimon Glass 			writel(I2C_STOPIPD, &regs->ipd);
25734374699SSimon Glass 			break;
25834374699SSimon Glass 		}
25934374699SSimon Glass 		if (get_timer(start) > I2C_TIMEOUT_MS) {
26034374699SSimon Glass 			debug("I2C Send Start Bit Timeout\n");
26134374699SSimon Glass 			rk_i2c_show_regs(regs);
26234374699SSimon Glass 			return -ETIMEDOUT;
26334374699SSimon Glass 		}
26434374699SSimon Glass 		udelay(1);
26534374699SSimon Glass 	}
26634374699SSimon Glass 
26756082ed3SDavid Wu 	udelay(1);
26834374699SSimon Glass 	return 0;
26934374699SSimon Glass }
27034374699SSimon Glass 
rk_i2c_disable(struct rk_i2c * i2c)27134374699SSimon Glass static inline void rk_i2c_disable(struct rk_i2c *i2c)
27234374699SSimon Glass {
2737ef28ab6SDavid Wu 	writel(0, &i2c->regs->ien);
2747ef28ab6SDavid Wu 	writel(I2C_IPD_ALL_CLEAN, &i2c->regs->ipd);
27534374699SSimon Glass 	writel(0, &i2c->regs->con);
27634374699SSimon Glass }
27734374699SSimon Glass 
rk_i2c_read(struct rk_i2c * i2c,uchar chip,uint reg,uint r_len,uchar * buf,uint b_len,bool snd)27834374699SSimon Glass static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
27956082ed3SDavid Wu 		       uchar *buf, uint b_len, bool snd)
28034374699SSimon Glass {
28134374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
28234374699SSimon Glass 	uchar *pbuf = buf;
28334374699SSimon Glass 	uint bytes_remain_len = b_len;
28434374699SSimon Glass 	uint bytes_xferred = 0;
28534374699SSimon Glass 	uint words_xferred = 0;
28634374699SSimon Glass 	ulong start;
28734374699SSimon Glass 	uint con = 0;
28834374699SSimon Glass 	uint rxdata;
28934374699SSimon Glass 	uint i, j;
29056082ed3SDavid Wu 	int err = 0;
2915deaa530SWadim Egorov 	bool snd_chunk = false;
29234374699SSimon Glass 
29334374699SSimon Glass 	debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
29434374699SSimon Glass 	      chip, reg, r_len, b_len);
29534374699SSimon Glass 
29656082ed3SDavid Wu 	/* If the second message for TRX read, resetting internal state. */
29756082ed3SDavid Wu 	if (snd)
29856082ed3SDavid Wu 		writel(0, &regs->con);
29934374699SSimon Glass 
30034374699SSimon Glass 	writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
30134374699SSimon Glass 	if (r_len == 0) {
30234374699SSimon Glass 		writel(0, &regs->mrxraddr);
30334374699SSimon Glass 	} else if (r_len < 4) {
30434374699SSimon Glass 		writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
30534374699SSimon Glass 	} else {
30634374699SSimon Glass 		debug("I2C Read: addr len %d not supported\n", r_len);
30734374699SSimon Glass 		return -EIO;
30834374699SSimon Glass 	}
30934374699SSimon Glass 
31034374699SSimon Glass 	while (bytes_remain_len) {
31134374699SSimon Glass 		if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
3125deaa530SWadim Egorov 			con = I2C_CON_EN;
31334374699SSimon Glass 			bytes_xferred = 32;
31434374699SSimon Glass 		} else {
3155deaa530SWadim Egorov 			/*
3165deaa530SWadim Egorov 			 * The hw can read up to 32 bytes at a time. If we need
3175deaa530SWadim Egorov 			 * more than one chunk, send an ACK after the last byte.
3185deaa530SWadim Egorov 			 */
3195deaa530SWadim Egorov 			con = I2C_CON_EN | I2C_CON_LASTACK;
32034374699SSimon Glass 			bytes_xferred = bytes_remain_len;
32134374699SSimon Glass 		}
32234374699SSimon Glass 		words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
32334374699SSimon Glass 
3245deaa530SWadim Egorov 		/*
32556082ed3SDavid Wu 		 * make sure we are in plain RX mode if we read a second chunk;
32656082ed3SDavid Wu 		 * and first rx read need to send start bit.
3275deaa530SWadim Egorov 		 */
32856082ed3SDavid Wu 		if (snd_chunk) {
3295deaa530SWadim Egorov 			con |= I2C_CON_MOD(I2C_MODE_RX);
3303d5347f8SDavid Wu 			writel(con | i2c->cfg, &regs->con);
33156082ed3SDavid Wu 		} else {
33256082ed3SDavid Wu 			con |= I2C_CON_MOD(I2C_MODE_TRX);
33356082ed3SDavid Wu 			err = rk_i2c_send_start_bit(i2c, con);
33456082ed3SDavid Wu 			if (err)
33556082ed3SDavid Wu 				return err;
33656082ed3SDavid Wu 		}
33756082ed3SDavid Wu 
33834374699SSimon Glass 		writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
33956082ed3SDavid Wu 		writel(bytes_xferred, &regs->mrxcnt);
34034374699SSimon Glass 
34134374699SSimon Glass 		start = get_timer(0);
34234374699SSimon Glass 		while (1) {
34334374699SSimon Glass 			if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
34434374699SSimon Glass 				writel(I2C_NAKRCVIPD, &regs->ipd);
34534374699SSimon Glass 				err = -EREMOTEIO;
346ea93ada1SDavid Wu 				goto i2c_exit;
34734374699SSimon Glass 			}
34834374699SSimon Glass 			if (readl(&regs->ipd) & I2C_MBRFIPD) {
34934374699SSimon Glass 				writel(I2C_MBRFIPD, &regs->ipd);
35034374699SSimon Glass 				break;
35134374699SSimon Glass 			}
35234374699SSimon Glass 			if (get_timer(start) > I2C_TIMEOUT_MS) {
35334374699SSimon Glass 				debug("I2C Read Data Timeout\n");
35434374699SSimon Glass 				err =  -ETIMEDOUT;
35534374699SSimon Glass 				rk_i2c_show_regs(regs);
35634374699SSimon Glass 				goto i2c_exit;
35734374699SSimon Glass 			}
35834374699SSimon Glass 			udelay(1);
35934374699SSimon Glass 		}
36034374699SSimon Glass 
36134374699SSimon Glass 		for (i = 0; i < words_xferred; i++) {
36234374699SSimon Glass 			rxdata = readl(&regs->rxdata[i]);
36334374699SSimon Glass 			debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
36434374699SSimon Glass 			for (j = 0; j < 4; j++) {
36534374699SSimon Glass 				if ((i * 4 + j) == bytes_xferred)
36634374699SSimon Glass 					break;
36734374699SSimon Glass 				*pbuf++ = (rxdata >> (j * 8)) & 0xff;
36834374699SSimon Glass 			}
36934374699SSimon Glass 		}
37034374699SSimon Glass 
37134374699SSimon Glass 		bytes_remain_len -= bytes_xferred;
3725deaa530SWadim Egorov 		snd_chunk = true;
37334374699SSimon Glass 		debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
37434374699SSimon Glass 	}
37534374699SSimon Glass 
37634374699SSimon Glass i2c_exit:
37734374699SSimon Glass 	return err;
37834374699SSimon Glass }
37934374699SSimon Glass 
rk_i2c_write(struct rk_i2c * i2c,uchar chip,uint reg,uint r_len,uchar * buf,uint b_len)38034374699SSimon Glass static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
38134374699SSimon Glass 			uchar *buf, uint b_len)
38234374699SSimon Glass {
38334374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
38456082ed3SDavid Wu 	int err = 0;
38534374699SSimon Glass 	uchar *pbuf = buf;
38634374699SSimon Glass 	uint bytes_remain_len = b_len + r_len + 1;
38734374699SSimon Glass 	uint bytes_xferred = 0;
38834374699SSimon Glass 	uint words_xferred = 0;
38956082ed3SDavid Wu 	bool next = false;
39034374699SSimon Glass 	ulong start;
39134374699SSimon Glass 	uint txdata;
39234374699SSimon Glass 	uint i, j;
39334374699SSimon Glass 
39434374699SSimon Glass 	debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
39534374699SSimon Glass 	      chip, reg, r_len, b_len);
39634374699SSimon Glass 
39734374699SSimon Glass 	while (bytes_remain_len) {
39834374699SSimon Glass 		if (bytes_remain_len > RK_I2C_FIFO_SIZE)
39980333fd8SJohn Keeping 			bytes_xferred = RK_I2C_FIFO_SIZE;
40034374699SSimon Glass 		else
40134374699SSimon Glass 			bytes_xferred = bytes_remain_len;
40234374699SSimon Glass 		words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
40334374699SSimon Glass 
40434374699SSimon Glass 		for (i = 0; i < words_xferred; i++) {
40534374699SSimon Glass 			txdata = 0;
40634374699SSimon Glass 			for (j = 0; j < 4; j++) {
40734374699SSimon Glass 				if ((i * 4 + j) == bytes_xferred)
40834374699SSimon Glass 					break;
40934374699SSimon Glass 
41021d4b7d4SJohn Keeping 				if (i == 0 && j == 0 && pbuf == buf) {
41134374699SSimon Glass 					txdata |= (chip << 1);
41221d4b7d4SJohn Keeping 				} else if (i == 0 && j <= r_len && pbuf == buf) {
41334374699SSimon Glass 					txdata |= (reg &
41434374699SSimon Glass 						(0xff << ((j - 1) * 8))) << 8;
41534374699SSimon Glass 				} else {
41634374699SSimon Glass 					txdata |= (*pbuf++)<<(j * 8);
41734374699SSimon Glass 				}
41834374699SSimon Glass 			}
419551288bdSJohn Keeping 			writel(txdata, &regs->txdata[i]);
420551288bdSJohn Keeping 			debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
42134374699SSimon Glass 		}
42234374699SSimon Glass 
42356082ed3SDavid Wu 		/* If the write is the first, need to send start bit */
42456082ed3SDavid Wu 		if (!next) {
4253d5347f8SDavid Wu 			err = rk_i2c_send_start_bit(i2c, I2C_CON_EN |
4263d5347f8SDavid Wu 					   I2C_CON_MOD(I2C_MODE_TX));
42756082ed3SDavid Wu 			if (err)
42856082ed3SDavid Wu 				return err;
42956082ed3SDavid Wu 			next = true;
43056082ed3SDavid Wu 		} else {
4313d5347f8SDavid Wu 			writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX) | i2c->cfg,
4323d5347f8SDavid Wu 			       &regs->con);
43356082ed3SDavid Wu 		}
43434374699SSimon Glass 		writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
43556082ed3SDavid Wu 		writel(bytes_xferred, &regs->mtxcnt);
43634374699SSimon Glass 
43734374699SSimon Glass 		start = get_timer(0);
43834374699SSimon Glass 		while (1) {
43934374699SSimon Glass 			if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
44034374699SSimon Glass 				writel(I2C_NAKRCVIPD, &regs->ipd);
44134374699SSimon Glass 				err = -EREMOTEIO;
442ea93ada1SDavid Wu 				goto i2c_exit;
44334374699SSimon Glass 			}
44434374699SSimon Glass 			if (readl(&regs->ipd) & I2C_MBTFIPD) {
44534374699SSimon Glass 				writel(I2C_MBTFIPD, &regs->ipd);
44634374699SSimon Glass 				break;
44734374699SSimon Glass 			}
44834374699SSimon Glass 			if (get_timer(start) > I2C_TIMEOUT_MS) {
44934374699SSimon Glass 				debug("I2C Write Data Timeout\n");
45034374699SSimon Glass 				err =  -ETIMEDOUT;
45134374699SSimon Glass 				rk_i2c_show_regs(regs);
45234374699SSimon Glass 				goto i2c_exit;
45334374699SSimon Glass 			}
45434374699SSimon Glass 			udelay(1);
45534374699SSimon Glass 		}
45634374699SSimon Glass 
45734374699SSimon Glass 		bytes_remain_len -= bytes_xferred;
45834374699SSimon Glass 		debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
45934374699SSimon Glass 	}
46034374699SSimon Glass 
46134374699SSimon Glass i2c_exit:
46234374699SSimon Glass 	return err;
46334374699SSimon Glass }
46434374699SSimon Glass 
rockchip_i2c_xfer(struct udevice * bus,struct i2c_msg * msg,int nmsgs)46534374699SSimon Glass static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
46634374699SSimon Glass 			     int nmsgs)
46734374699SSimon Glass {
46834374699SSimon Glass 	struct rk_i2c *i2c = dev_get_priv(bus);
46956082ed3SDavid Wu 	bool snd = false; /* second message for TRX read */
47034374699SSimon Glass 	int ret;
47175adbedeSJoseph Chen #ifdef CONFIG_IRQ
47275adbedeSJoseph Chen 	ulong flags;
47375adbedeSJoseph Chen #endif
47434374699SSimon Glass 
47534374699SSimon Glass 	debug("i2c_xfer: %d messages\n", nmsgs);
47656082ed3SDavid Wu 	if (nmsgs > 2 || ((nmsgs == 2) && (msg->flags & I2C_M_RD))) {
47756082ed3SDavid Wu 		debug("Not support more messages now, split them\n");
47856082ed3SDavid Wu 		return -EINVAL;
47956082ed3SDavid Wu 	}
48056082ed3SDavid Wu 
48175adbedeSJoseph Chen #ifdef CONFIG_IRQ
48275adbedeSJoseph Chen 	local_irq_save(flags);
48375adbedeSJoseph Chen #endif
484ea93ada1SDavid Wu 	/* Nack enabled */
485ea93ada1SDavid Wu 	i2c->cfg |= I2C_CON_ACTACK;
48634374699SSimon Glass 	for (; nmsgs > 0; nmsgs--, msg++) {
48734374699SSimon Glass 		debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
48856082ed3SDavid Wu 
48934374699SSimon Glass 		if (msg->flags & I2C_M_RD) {
49056082ed3SDavid Wu 			/* If snd is true, it is TRX mode. */
49134374699SSimon Glass 			ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
49256082ed3SDavid Wu 					  msg->len, snd);
49334374699SSimon Glass 		} else {
49456082ed3SDavid Wu 			snd = true;
49534374699SSimon Glass 			ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
49634374699SSimon Glass 					   msg->len);
49734374699SSimon Glass 		}
49856082ed3SDavid Wu 
49934374699SSimon Glass 		if (ret) {
50034374699SSimon Glass 			debug("i2c_write: error sending\n");
50156082ed3SDavid Wu 			goto exit;
50234374699SSimon Glass 		}
50334374699SSimon Glass 	}
50434374699SSimon Glass 
50556082ed3SDavid Wu exit:
506133495afSVasily Khoruzhick 	rk_i2c_send_stop_bit(i2c);
507133495afSVasily Khoruzhick 	rk_i2c_disable(i2c);
50875adbedeSJoseph Chen #ifdef CONFIG_IRQ
50975adbedeSJoseph Chen 	local_irq_restore(flags);
51075adbedeSJoseph Chen #endif
51156082ed3SDavid Wu 	return ret;
51234374699SSimon Glass }
51334374699SSimon Glass 
rk3x_i2c_get_version(struct rk_i2c * i2c)5143d5347f8SDavid Wu static unsigned int rk3x_i2c_get_version(struct rk_i2c *i2c)
5153d5347f8SDavid Wu {
5163d5347f8SDavid Wu 	struct i2c_regs *regs = i2c->regs;
5173d5347f8SDavid Wu 	uint version;
5183d5347f8SDavid Wu 
5193d5347f8SDavid Wu 	version = readl(&regs->con) & I2C_CON_VERSION;
5203d5347f8SDavid Wu 
5213d5347f8SDavid Wu 	return version >>= I2C_CON_VERSION_SHIFT;
5223d5347f8SDavid Wu }
5233d5347f8SDavid Wu 
rockchip_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)52434374699SSimon Glass int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
52534374699SSimon Glass {
52634374699SSimon Glass 	struct rk_i2c *i2c = dev_get_priv(bus);
52734374699SSimon Glass 
5283d5347f8SDavid Wu 	if (rk3x_i2c_get_version(i2c) >= RK_I2C_VERSION1)
5293d5347f8SDavid Wu 		rk_i2c_adapter_clk(i2c, speed);
5303d5347f8SDavid Wu 	else
53134374699SSimon Glass 		rk_i2c_set_clk(i2c, speed);
53234374699SSimon Glass 
53334374699SSimon Glass 	return 0;
53434374699SSimon Glass }
53534374699SSimon Glass 
rockchip_i2c_ofdata_to_platdata(struct udevice * bus)536930bc374SSimon Glass static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
53734374699SSimon Glass {
538930bc374SSimon Glass 	struct rk_i2c *priv = dev_get_priv(bus);
53934374699SSimon Glass 	int ret;
54034374699SSimon Glass 
541930bc374SSimon Glass 	ret = clk_get_by_index(bus, 0, &priv->clk);
542930bc374SSimon Glass 	if (ret < 0) {
543930bc374SSimon Glass 		debug("%s: Could not get clock for %s: %d\n", __func__,
544930bc374SSimon Glass 		      bus->name, ret);
54534374699SSimon Glass 		return ret;
546930bc374SSimon Glass 	}
547930bc374SSimon Glass 
548930bc374SSimon Glass 	return 0;
549930bc374SSimon Glass }
550930bc374SSimon Glass 
rockchip_i2c_probe(struct udevice * bus)551930bc374SSimon Glass static int rockchip_i2c_probe(struct udevice *bus)
552930bc374SSimon Glass {
553930bc374SSimon Glass 	struct rk_i2c *priv = dev_get_priv(bus);
554930bc374SSimon Glass 
555e2186db8SPhilipp Tomsich 	priv->regs = dev_read_addr_ptr(bus);
556930bc374SSimon Glass 
557930bc374SSimon Glass 	return 0;
55834374699SSimon Glass }
55934374699SSimon Glass 
56034374699SSimon Glass static const struct dm_i2c_ops rockchip_i2c_ops = {
56134374699SSimon Glass 	.xfer		= rockchip_i2c_xfer,
56234374699SSimon Glass 	.set_bus_speed	= rockchip_i2c_set_bus_speed,
56334374699SSimon Glass };
56434374699SSimon Glass 
56534374699SSimon Glass static const struct udevice_id rockchip_i2c_ids[] = {
56602a7d833SHeiko Stübner 	{ .compatible = "rockchip,rk3066-i2c" },
56702a7d833SHeiko Stübner 	{ .compatible = "rockchip,rk3188-i2c" },
56834374699SSimon Glass 	{ .compatible = "rockchip,rk3288-i2c" },
5694d786a23SElaine Zhang 	{ .compatible = "rockchip,rk3328-i2c" },
570b644354aSeric.gao@rock-chips.com 	{ .compatible = "rockchip,rk3399-i2c" },
571f312c7e4SShunqing Chen 	{ .compatible = "rockchip,rk3228-i2c" },
5722fe2ebadSElaine Zhang 	{ .compatible = "rockchip,rv1108-i2c" },
573*88d32174SDavid Wu 	{ .compatible = "rockchip,rv1126b-i2c" },
57434374699SSimon Glass 	{ }
57534374699SSimon Glass };
57634374699SSimon Glass 
57734374699SSimon Glass U_BOOT_DRIVER(i2c_rockchip) = {
57834374699SSimon Glass 	.name	= "i2c_rockchip",
57934374699SSimon Glass 	.id	= UCLASS_I2C,
58034374699SSimon Glass 	.of_match = rockchip_i2c_ids,
581930bc374SSimon Glass 	.ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
58234374699SSimon Glass 	.probe	= rockchip_i2c_probe,
58334374699SSimon Glass 	.priv_auto_alloc_size = sizeof(struct rk_i2c),
58434374699SSimon Glass 	.ops	= &rockchip_i2c_ops,
58534374699SSimon Glass };
586