blob: 9927af94a80b1dfbb66c41e276d52118ba6b996f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass34374692015-08-30 16:55:39 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 *
5 * (C) Copyright 2008-2014 Rockchip Electronics
6 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
Simon Glass34374692015-08-30 16:55:39 -06007 */
8
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <errno.h>
13#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass34374692015-08-30 16:55:39 -060015#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080016#include <asm/arch-rockchip/clock.h>
17#include <asm/arch-rockchip/i2c.h>
18#include <asm/arch-rockchip/periph.h>
Simon Glass34374692015-08-30 16:55:39 -060019#include <dm/pinctrl.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glass34374692015-08-30 16:55:39 -060021#include <linux/sizes.h>
22
Simon Glass34374692015-08-30 16:55:39 -060023/* i2c timerout */
24#define I2C_TIMEOUT_MS 100
25#define I2C_RETRY_COUNT 3
26
27/* rk i2c fifo max transfer bytes */
28#define RK_I2C_FIFO_SIZE 32
29
30struct rk_i2c {
Stephen Warren135aa952016-06-17 09:44:00 -060031 struct clk clk;
Simon Glass34374692015-08-30 16:55:39 -060032 struct i2c_regs *regs;
33 unsigned int speed;
Simon Glass34374692015-08-30 16:55:39 -060034};
35
Alexander Kochetkov5944bd92018-02-26 20:42:54 +030036enum {
37 RK_I2C_LEGACY,
38 RK_I2C_NEW,
39};
40
41/**
42 * @controller_type: i2c controller type
43 */
44struct rk_i2c_soc_data {
45 int controller_type;
46};
47
Simon Glass34374692015-08-30 16:55:39 -060048static inline void rk_i2c_get_div(int div, int *divh, int *divl)
49{
50 *divl = div / 2;
51 if (div % 2 == 0)
52 *divh = div / 2;
53 else
54 *divh = DIV_ROUND_UP(div, 2);
55}
56
57/*
58 * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
59 * SCL = PCLK / SCLK Divisor
60 * i2c_rate = PCLK
61 */
62static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
63{
64 uint32_t i2c_rate;
65 int div, divl, divh;
66
67 /* First get i2c rate from pclk */
Stephen Warren135aa952016-06-17 09:44:00 -060068 i2c_rate = clk_get_rate(&i2c->clk);
Simon Glass34374692015-08-30 16:55:39 -060069
70 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
71 divh = 0;
72 divl = 0;
73 if (div >= 0)
74 rk_i2c_get_div(div, &divh, &divl);
75 writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
76
77 debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
78 scl_rate);
79 debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
80 debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
81}
82
83static void rk_i2c_show_regs(struct i2c_regs *regs)
84{
85#ifdef DEBUG
86 uint i;
87
88 debug("i2c_con: 0x%08x\n", readl(&regs->con));
89 debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
90 debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
91 debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
92 debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
93 debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
94 debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
95 debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
96 debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
97 for (i = 0; i < 8; i++)
98 debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
99 for (i = 0; i < 8; i++)
100 debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
101#endif
102}
103
104static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
105{
106 struct i2c_regs *regs = i2c->regs;
107 ulong start;
108
109 debug("I2c Send Start bit.\n");
110 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
111
112 writel(I2C_CON_EN | I2C_CON_START, &regs->con);
113 writel(I2C_STARTIEN, &regs->ien);
114
115 start = get_timer(0);
116 while (1) {
117 if (readl(&regs->ipd) & I2C_STARTIPD) {
118 writel(I2C_STARTIPD, &regs->ipd);
119 break;
120 }
121 if (get_timer(start) > I2C_TIMEOUT_MS) {
122 debug("I2C Send Start Bit Timeout\n");
123 rk_i2c_show_regs(regs);
124 return -ETIMEDOUT;
125 }
126 udelay(1);
127 }
128
129 return 0;
130}
131
132static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
133{
134 struct i2c_regs *regs = i2c->regs;
135 ulong start;
136
137 debug("I2c Send Stop bit.\n");
138 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
139
140 writel(I2C_CON_EN | I2C_CON_STOP, &regs->con);
141 writel(I2C_CON_STOP, &regs->ien);
142
143 start = get_timer(0);
144 while (1) {
145 if (readl(&regs->ipd) & I2C_STOPIPD) {
146 writel(I2C_STOPIPD, &regs->ipd);
147 break;
148 }
149 if (get_timer(start) > I2C_TIMEOUT_MS) {
150 debug("I2C Send Start Bit Timeout\n");
151 rk_i2c_show_regs(regs);
152 return -ETIMEDOUT;
153 }
154 udelay(1);
155 }
156
157 return 0;
158}
159
160static inline void rk_i2c_disable(struct rk_i2c *i2c)
161{
162 writel(0, &i2c->regs->con);
163}
164
165static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
166 uchar *buf, uint b_len)
167{
168 struct i2c_regs *regs = i2c->regs;
169 uchar *pbuf = buf;
170 uint bytes_remain_len = b_len;
171 uint bytes_xferred = 0;
172 uint words_xferred = 0;
173 ulong start;
174 uint con = 0;
175 uint rxdata;
176 uint i, j;
177 int err;
Wadim Egorov5deaa532017-08-03 13:48:11 +0200178 bool snd_chunk = false;
Simon Glass34374692015-08-30 16:55:39 -0600179
180 debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
181 chip, reg, r_len, b_len);
182
183 err = rk_i2c_send_start_bit(i2c);
184 if (err)
185 return err;
186
187 writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
188 if (r_len == 0) {
189 writel(0, &regs->mrxraddr);
190 } else if (r_len < 4) {
191 writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
192 } else {
193 debug("I2C Read: addr len %d not supported\n", r_len);
194 return -EIO;
195 }
196
197 while (bytes_remain_len) {
198 if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
Wadim Egorov5deaa532017-08-03 13:48:11 +0200199 con = I2C_CON_EN;
Simon Glass34374692015-08-30 16:55:39 -0600200 bytes_xferred = 32;
201 } else {
Wadim Egorov5deaa532017-08-03 13:48:11 +0200202 /*
203 * The hw can read up to 32 bytes at a time. If we need
204 * more than one chunk, send an ACK after the last byte.
205 */
206 con = I2C_CON_EN | I2C_CON_LASTACK;
Simon Glass34374692015-08-30 16:55:39 -0600207 bytes_xferred = bytes_remain_len;
208 }
209 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
210
Wadim Egorov5deaa532017-08-03 13:48:11 +0200211 /*
212 * make sure we are in plain RX mode if we read a second chunk
213 */
214 if (snd_chunk)
215 con |= I2C_CON_MOD(I2C_MODE_RX);
216 else
217 con |= I2C_CON_MOD(I2C_MODE_TRX);
218
Simon Glass34374692015-08-30 16:55:39 -0600219 writel(con, &regs->con);
220 writel(bytes_xferred, &regs->mrxcnt);
221 writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
222
223 start = get_timer(0);
224 while (1) {
225 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
226 writel(I2C_NAKRCVIPD, &regs->ipd);
227 err = -EREMOTEIO;
228 }
229 if (readl(&regs->ipd) & I2C_MBRFIPD) {
230 writel(I2C_MBRFIPD, &regs->ipd);
231 break;
232 }
233 if (get_timer(start) > I2C_TIMEOUT_MS) {
234 debug("I2C Read Data Timeout\n");
235 err = -ETIMEDOUT;
236 rk_i2c_show_regs(regs);
237 goto i2c_exit;
238 }
239 udelay(1);
240 }
241
242 for (i = 0; i < words_xferred; i++) {
243 rxdata = readl(&regs->rxdata[i]);
244 debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
245 for (j = 0; j < 4; j++) {
246 if ((i * 4 + j) == bytes_xferred)
247 break;
248 *pbuf++ = (rxdata >> (j * 8)) & 0xff;
249 }
250 }
251
252 bytes_remain_len -= bytes_xferred;
Wadim Egorov5deaa532017-08-03 13:48:11 +0200253 snd_chunk = true;
Simon Glass34374692015-08-30 16:55:39 -0600254 debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
255 }
256
257i2c_exit:
Simon Glass34374692015-08-30 16:55:39 -0600258 rk_i2c_disable(i2c);
259
260 return err;
261}
262
263static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
264 uchar *buf, uint b_len)
265{
266 struct i2c_regs *regs = i2c->regs;
267 int err;
268 uchar *pbuf = buf;
269 uint bytes_remain_len = b_len + r_len + 1;
270 uint bytes_xferred = 0;
271 uint words_xferred = 0;
272 ulong start;
273 uint txdata;
274 uint i, j;
275
276 debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
277 chip, reg, r_len, b_len);
278 err = rk_i2c_send_start_bit(i2c);
279 if (err)
280 return err;
281
282 while (bytes_remain_len) {
283 if (bytes_remain_len > RK_I2C_FIFO_SIZE)
John Keeping80333fd2016-08-18 20:08:40 +0100284 bytes_xferred = RK_I2C_FIFO_SIZE;
Simon Glass34374692015-08-30 16:55:39 -0600285 else
286 bytes_xferred = bytes_remain_len;
287 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
288
289 for (i = 0; i < words_xferred; i++) {
290 txdata = 0;
291 for (j = 0; j < 4; j++) {
292 if ((i * 4 + j) == bytes_xferred)
293 break;
294
John Keeping21d4b7d2016-08-18 20:08:42 +0100295 if (i == 0 && j == 0 && pbuf == buf) {
Simon Glass34374692015-08-30 16:55:39 -0600296 txdata |= (chip << 1);
John Keeping21d4b7d2016-08-18 20:08:42 +0100297 } else if (i == 0 && j <= r_len && pbuf == buf) {
Simon Glass34374692015-08-30 16:55:39 -0600298 txdata |= (reg &
299 (0xff << ((j - 1) * 8))) << 8;
300 } else {
301 txdata |= (*pbuf++)<<(j * 8);
302 }
Simon Glass34374692015-08-30 16:55:39 -0600303 }
John Keeping551288b2016-08-18 20:08:41 +0100304 writel(txdata, &regs->txdata[i]);
305 debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
Simon Glass34374692015-08-30 16:55:39 -0600306 }
307
308 writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), &regs->con);
309 writel(bytes_xferred, &regs->mtxcnt);
310 writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
311
312 start = get_timer(0);
313 while (1) {
314 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
315 writel(I2C_NAKRCVIPD, &regs->ipd);
316 err = -EREMOTEIO;
317 }
318 if (readl(&regs->ipd) & I2C_MBTFIPD) {
319 writel(I2C_MBTFIPD, &regs->ipd);
320 break;
321 }
322 if (get_timer(start) > I2C_TIMEOUT_MS) {
323 debug("I2C Write Data Timeout\n");
324 err = -ETIMEDOUT;
325 rk_i2c_show_regs(regs);
326 goto i2c_exit;
327 }
328 udelay(1);
329 }
330
331 bytes_remain_len -= bytes_xferred;
332 debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
333 }
334
335i2c_exit:
Simon Glass34374692015-08-30 16:55:39 -0600336 rk_i2c_disable(i2c);
337
338 return err;
339}
340
341static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
342 int nmsgs)
343{
344 struct rk_i2c *i2c = dev_get_priv(bus);
Ondrej Jirmanba5b9202023-05-25 14:18:17 +0200345 int ret = 0;
Simon Glass34374692015-08-30 16:55:39 -0600346
347 debug("i2c_xfer: %d messages\n", nmsgs);
348 for (; nmsgs > 0; nmsgs--, msg++) {
349 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
350 if (msg->flags & I2C_M_RD) {
351 ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
352 msg->len);
353 } else {
354 ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
355 msg->len);
356 }
357 if (ret) {
358 debug("i2c_write: error sending\n");
Ondrej Jirmanba5b9202023-05-25 14:18:17 +0200359 ret = -EREMOTEIO;
360 break;
Simon Glass34374692015-08-30 16:55:39 -0600361 }
362 }
363
Vasily Khoruzhickc9fca5e2019-11-16 11:32:57 -0800364 rk_i2c_send_stop_bit(i2c);
365 rk_i2c_disable(i2c);
366
Ondrej Jirmanba5b9202023-05-25 14:18:17 +0200367 return ret;
Simon Glass34374692015-08-30 16:55:39 -0600368}
369
370int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
371{
372 struct rk_i2c *i2c = dev_get_priv(bus);
373
374 rk_i2c_set_clk(i2c, speed);
375
376 return 0;
377}
378
Simon Glassd1998a92020-12-03 16:55:21 -0700379static int rockchip_i2c_of_to_plat(struct udevice *bus)
Simon Glass34374692015-08-30 16:55:39 -0600380{
Simon Glass930bc372016-01-21 19:43:42 -0700381 struct rk_i2c *priv = dev_get_priv(bus);
Simon Glass34374692015-08-30 16:55:39 -0600382 int ret;
383
Simon Glass930bc372016-01-21 19:43:42 -0700384 ret = clk_get_by_index(bus, 0, &priv->clk);
385 if (ret < 0) {
386 debug("%s: Could not get clock for %s: %d\n", __func__,
387 bus->name, ret);
Simon Glass34374692015-08-30 16:55:39 -0600388 return ret;
Simon Glass930bc372016-01-21 19:43:42 -0700389 }
Simon Glass930bc372016-01-21 19:43:42 -0700390
391 return 0;
392}
393
394static int rockchip_i2c_probe(struct udevice *bus)
395{
396 struct rk_i2c *priv = dev_get_priv(bus);
Alexander Kochetkov5944bd92018-02-26 20:42:54 +0300397 struct rk_i2c_soc_data *soc_data;
398 struct udevice *pinctrl;
399 int bus_nr;
400 int ret;
Simon Glass930bc372016-01-21 19:43:42 -0700401
Philipp Tomsichcc91bdf2017-09-11 22:04:23 +0200402 priv->regs = dev_read_addr_ptr(bus);
Simon Glass930bc372016-01-21 19:43:42 -0700403
Alexander Kochetkov5944bd92018-02-26 20:42:54 +0300404 soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus);
405
406 if (soc_data->controller_type == RK_I2C_LEGACY) {
407 ret = dev_read_alias_seq(bus, &bus_nr);
408 if (ret < 0) {
409 debug("%s: Could not get alias for %s: %d\n",
410 __func__, bus->name, ret);
411 return ret;
412 }
413
414 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
415 if (ret) {
416 debug("%s: Cannot find pinctrl device\n", __func__);
417 return ret;
418 }
419
420 /* pinctrl will switch I2C to new type */
421 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr);
422 if (ret) {
423 debug("%s: Failed to switch I2C to new type %s: %d\n",
424 __func__, bus->name, ret);
425 return ret;
426 }
427 }
428
Simon Glass930bc372016-01-21 19:43:42 -0700429 return 0;
Simon Glass34374692015-08-30 16:55:39 -0600430}
431
432static const struct dm_i2c_ops rockchip_i2c_ops = {
433 .xfer = rockchip_i2c_xfer,
434 .set_bus_speed = rockchip_i2c_set_bus_speed,
435};
436
Alexander Kochetkov5944bd92018-02-26 20:42:54 +0300437static const struct rk_i2c_soc_data rk3066_soc_data = {
438 .controller_type = RK_I2C_LEGACY,
439};
440
441static const struct rk_i2c_soc_data rk3188_soc_data = {
442 .controller_type = RK_I2C_LEGACY,
443};
444
445static const struct rk_i2c_soc_data rk3228_soc_data = {
446 .controller_type = RK_I2C_NEW,
447};
448
449static const struct rk_i2c_soc_data rk3288_soc_data = {
450 .controller_type = RK_I2C_NEW,
451};
452
453static const struct rk_i2c_soc_data rk3328_soc_data = {
454 .controller_type = RK_I2C_NEW,
455};
456
457static const struct rk_i2c_soc_data rk3399_soc_data = {
458 .controller_type = RK_I2C_NEW,
459};
460
Simon Glass34374692015-08-30 16:55:39 -0600461static const struct udevice_id rockchip_i2c_ids[] = {
Alexander Kochetkov5944bd92018-02-26 20:42:54 +0300462 {
463 .compatible = "rockchip,rk3066-i2c",
464 .data = (ulong)&rk3066_soc_data,
465 },
466 {
467 .compatible = "rockchip,rk3188-i2c",
468 .data = (ulong)&rk3188_soc_data,
469 },
470 {
471 .compatible = "rockchip,rk3228-i2c",
472 .data = (ulong)&rk3228_soc_data,
473 },
474 {
475 .compatible = "rockchip,rk3288-i2c",
476 .data = (ulong)&rk3288_soc_data,
477 },
478 {
479 .compatible = "rockchip,rk3328-i2c",
480 .data = (ulong)&rk3328_soc_data,
481 },
482 {
483 .compatible = "rockchip,rk3399-i2c",
484 .data = (ulong)&rk3399_soc_data,
485 },
Simon Glass34374692015-08-30 16:55:39 -0600486 { }
487};
488
Walter Lozanoe3e24702020-06-25 01:10:04 -0300489U_BOOT_DRIVER(rockchip_rk3066_i2c) = {
490 .name = "rockchip_rk3066_i2c",
Simon Glass34374692015-08-30 16:55:39 -0600491 .id = UCLASS_I2C,
492 .of_match = rockchip_i2c_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700493 .of_to_plat = rockchip_i2c_of_to_plat,
Simon Glass34374692015-08-30 16:55:39 -0600494 .probe = rockchip_i2c_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700495 .priv_auto = sizeof(struct rk_i2c),
Simon Glass34374692015-08-30 16:55:39 -0600496 .ops = &rockchip_i2c_ops,
497};
Walter Lozanoaddf3582020-06-25 01:10:06 -0300498
Simon Glassbdf8fd72020-12-28 20:34:57 -0700499DM_DRIVER_ALIAS(rockchip_rk3066_i2c, rockchip_rk3288_i2c)