blob: 32b2ee85787ef859763aeb21476aa5e0198ac2ae [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3595f952015-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 Glass3595f952015-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>
14#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080015#include <asm/arch-rockchip/clock.h>
16#include <asm/arch-rockchip/i2c.h>
17#include <asm/arch-rockchip/periph.h>
Simon Glass3595f952015-08-30 16:55:39 -060018#include <dm/pinctrl.h>
19#include <linux/sizes.h>
20
Simon Glass3595f952015-08-30 16:55:39 -060021/* i2c timerout */
22#define I2C_TIMEOUT_MS 100
23#define I2C_RETRY_COUNT 3
24
25/* rk i2c fifo max transfer bytes */
26#define RK_I2C_FIFO_SIZE 32
27
28struct rk_i2c {
Stephen Warrena9622432016-06-17 09:44:00 -060029 struct clk clk;
Simon Glass3595f952015-08-30 16:55:39 -060030 struct i2c_regs *regs;
31 unsigned int speed;
Simon Glass3595f952015-08-30 16:55:39 -060032};
33
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +030034enum {
35 RK_I2C_LEGACY,
36 RK_I2C_NEW,
37};
38
39/**
40 * @controller_type: i2c controller type
41 */
42struct rk_i2c_soc_data {
43 int controller_type;
44};
45
Simon Glass3595f952015-08-30 16:55:39 -060046static inline void rk_i2c_get_div(int div, int *divh, int *divl)
47{
48 *divl = div / 2;
49 if (div % 2 == 0)
50 *divh = div / 2;
51 else
52 *divh = DIV_ROUND_UP(div, 2);
53}
54
55/*
56 * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
57 * SCL = PCLK / SCLK Divisor
58 * i2c_rate = PCLK
59 */
60static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
61{
62 uint32_t i2c_rate;
63 int div, divl, divh;
64
65 /* First get i2c rate from pclk */
Stephen Warrena9622432016-06-17 09:44:00 -060066 i2c_rate = clk_get_rate(&i2c->clk);
Simon Glass3595f952015-08-30 16:55:39 -060067
68 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
69 divh = 0;
70 divl = 0;
71 if (div >= 0)
72 rk_i2c_get_div(div, &divh, &divl);
73 writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
74
75 debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
76 scl_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));
79}
80
81static void rk_i2c_show_regs(struct i2c_regs *regs)
82{
83#ifdef DEBUG
84 uint i;
85
86 debug("i2c_con: 0x%08x\n", readl(&regs->con));
87 debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
88 debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
89 debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
90 debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
91 debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
92 debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
93 debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
94 debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
95 for (i = 0; i < 8; i++)
96 debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
97 for (i = 0; i < 8; i++)
98 debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
99#endif
100}
101
102static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
103{
104 struct i2c_regs *regs = i2c->regs;
105 ulong start;
106
107 debug("I2c Send Start bit.\n");
108 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
109
110 writel(I2C_CON_EN | I2C_CON_START, &regs->con);
111 writel(I2C_STARTIEN, &regs->ien);
112
113 start = get_timer(0);
114 while (1) {
115 if (readl(&regs->ipd) & I2C_STARTIPD) {
116 writel(I2C_STARTIPD, &regs->ipd);
117 break;
118 }
119 if (get_timer(start) > I2C_TIMEOUT_MS) {
120 debug("I2C Send Start Bit Timeout\n");
121 rk_i2c_show_regs(regs);
122 return -ETIMEDOUT;
123 }
124 udelay(1);
125 }
126
127 return 0;
128}
129
130static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
131{
132 struct i2c_regs *regs = i2c->regs;
133 ulong start;
134
135 debug("I2c Send Stop bit.\n");
136 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
137
138 writel(I2C_CON_EN | I2C_CON_STOP, &regs->con);
139 writel(I2C_CON_STOP, &regs->ien);
140
141 start = get_timer(0);
142 while (1) {
143 if (readl(&regs->ipd) & I2C_STOPIPD) {
144 writel(I2C_STOPIPD, &regs->ipd);
145 break;
146 }
147 if (get_timer(start) > I2C_TIMEOUT_MS) {
148 debug("I2C Send Start Bit Timeout\n");
149 rk_i2c_show_regs(regs);
150 return -ETIMEDOUT;
151 }
152 udelay(1);
153 }
154
155 return 0;
156}
157
158static inline void rk_i2c_disable(struct rk_i2c *i2c)
159{
160 writel(0, &i2c->regs->con);
161}
162
163static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
164 uchar *buf, uint b_len)
165{
166 struct i2c_regs *regs = i2c->regs;
167 uchar *pbuf = buf;
168 uint bytes_remain_len = b_len;
169 uint bytes_xferred = 0;
170 uint words_xferred = 0;
171 ulong start;
172 uint con = 0;
173 uint rxdata;
174 uint i, j;
175 int err;
Wadim Egorov838d7542017-08-03 13:48:11 +0200176 bool snd_chunk = false;
Simon Glass3595f952015-08-30 16:55:39 -0600177
178 debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
179 chip, reg, r_len, b_len);
180
181 err = rk_i2c_send_start_bit(i2c);
182 if (err)
183 return err;
184
185 writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
186 if (r_len == 0) {
187 writel(0, &regs->mrxraddr);
188 } else if (r_len < 4) {
189 writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
190 } else {
191 debug("I2C Read: addr len %d not supported\n", r_len);
192 return -EIO;
193 }
194
195 while (bytes_remain_len) {
196 if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
Wadim Egorov838d7542017-08-03 13:48:11 +0200197 con = I2C_CON_EN;
Simon Glass3595f952015-08-30 16:55:39 -0600198 bytes_xferred = 32;
199 } else {
Wadim Egorov838d7542017-08-03 13:48:11 +0200200 /*
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.
203 */
204 con = I2C_CON_EN | I2C_CON_LASTACK;
Simon Glass3595f952015-08-30 16:55:39 -0600205 bytes_xferred = bytes_remain_len;
206 }
207 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
208
Wadim Egorov838d7542017-08-03 13:48:11 +0200209 /*
210 * make sure we are in plain RX mode if we read a second chunk
211 */
212 if (snd_chunk)
213 con |= I2C_CON_MOD(I2C_MODE_RX);
214 else
215 con |= I2C_CON_MOD(I2C_MODE_TRX);
216
Simon Glass3595f952015-08-30 16:55:39 -0600217 writel(con, &regs->con);
218 writel(bytes_xferred, &regs->mrxcnt);
219 writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
220
221 start = get_timer(0);
222 while (1) {
223 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
224 writel(I2C_NAKRCVIPD, &regs->ipd);
225 err = -EREMOTEIO;
226 }
227 if (readl(&regs->ipd) & I2C_MBRFIPD) {
228 writel(I2C_MBRFIPD, &regs->ipd);
229 break;
230 }
231 if (get_timer(start) > I2C_TIMEOUT_MS) {
232 debug("I2C Read Data Timeout\n");
233 err = -ETIMEDOUT;
234 rk_i2c_show_regs(regs);
235 goto i2c_exit;
236 }
237 udelay(1);
238 }
239
240 for (i = 0; i < words_xferred; i++) {
241 rxdata = readl(&regs->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)
245 break;
246 *pbuf++ = (rxdata >> (j * 8)) & 0xff;
247 }
248 }
249
250 bytes_remain_len -= bytes_xferred;
Wadim Egorov838d7542017-08-03 13:48:11 +0200251 snd_chunk = true;
Simon Glass3595f952015-08-30 16:55:39 -0600252 debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
253 }
254
255i2c_exit:
Simon Glass3595f952015-08-30 16:55:39 -0600256 rk_i2c_disable(i2c);
257
258 return err;
259}
260
261static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
262 uchar *buf, uint b_len)
263{
264 struct i2c_regs *regs = i2c->regs;
265 int err;
266 uchar *pbuf = buf;
267 uint bytes_remain_len = b_len + r_len + 1;
268 uint bytes_xferred = 0;
269 uint words_xferred = 0;
270 ulong start;
271 uint txdata;
272 uint i, j;
273
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);
277 if (err)
278 return err;
279
280 while (bytes_remain_len) {
281 if (bytes_remain_len > RK_I2C_FIFO_SIZE)
John Keepingfebe7632016-08-18 20:08:40 +0100282 bytes_xferred = RK_I2C_FIFO_SIZE;
Simon Glass3595f952015-08-30 16:55:39 -0600283 else
284 bytes_xferred = bytes_remain_len;
285 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
286
287 for (i = 0; i < words_xferred; i++) {
288 txdata = 0;
289 for (j = 0; j < 4; j++) {
290 if ((i * 4 + j) == bytes_xferred)
291 break;
292
John Keeping27dfc942016-08-18 20:08:42 +0100293 if (i == 0 && j == 0 && pbuf == buf) {
Simon Glass3595f952015-08-30 16:55:39 -0600294 txdata |= (chip << 1);
John Keeping27dfc942016-08-18 20:08:42 +0100295 } else if (i == 0 && j <= r_len && pbuf == buf) {
Simon Glass3595f952015-08-30 16:55:39 -0600296 txdata |= (reg &
297 (0xff << ((j - 1) * 8))) << 8;
298 } else {
299 txdata |= (*pbuf++)<<(j * 8);
300 }
Simon Glass3595f952015-08-30 16:55:39 -0600301 }
John Keepingbcd11c42016-08-18 20:08:41 +0100302 writel(txdata, &regs->txdata[i]);
303 debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
Simon Glass3595f952015-08-30 16:55:39 -0600304 }
305
306 writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), &regs->con);
307 writel(bytes_xferred, &regs->mtxcnt);
308 writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
309
310 start = get_timer(0);
311 while (1) {
312 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
313 writel(I2C_NAKRCVIPD, &regs->ipd);
314 err = -EREMOTEIO;
315 }
316 if (readl(&regs->ipd) & I2C_MBTFIPD) {
317 writel(I2C_MBTFIPD, &regs->ipd);
318 break;
319 }
320 if (get_timer(start) > I2C_TIMEOUT_MS) {
321 debug("I2C Write Data Timeout\n");
322 err = -ETIMEDOUT;
323 rk_i2c_show_regs(regs);
324 goto i2c_exit;
325 }
326 udelay(1);
327 }
328
329 bytes_remain_len -= bytes_xferred;
330 debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
331 }
332
333i2c_exit:
Simon Glass3595f952015-08-30 16:55:39 -0600334 rk_i2c_disable(i2c);
335
336 return err;
337}
338
339static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
340 int nmsgs)
341{
342 struct rk_i2c *i2c = dev_get_priv(bus);
343 int ret;
344
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,
350 msg->len);
351 } else {
352 ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
353 msg->len);
354 }
355 if (ret) {
356 debug("i2c_write: error sending\n");
357 return -EREMOTEIO;
358 }
359 }
360
Vasily Khoruzhickc7954892019-11-16 11:32:57 -0800361 rk_i2c_send_stop_bit(i2c);
362 rk_i2c_disable(i2c);
363
Simon Glass3595f952015-08-30 16:55:39 -0600364 return 0;
365}
366
367int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
368{
369 struct rk_i2c *i2c = dev_get_priv(bus);
370
371 rk_i2c_set_clk(i2c, speed);
372
373 return 0;
374}
375
Simon Glass3d156052016-01-21 19:43:42 -0700376static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
Simon Glass3595f952015-08-30 16:55:39 -0600377{
Simon Glass3d156052016-01-21 19:43:42 -0700378 struct rk_i2c *priv = dev_get_priv(bus);
Simon Glass3595f952015-08-30 16:55:39 -0600379 int ret;
380
Simon Glass3d156052016-01-21 19:43:42 -0700381 ret = clk_get_by_index(bus, 0, &priv->clk);
382 if (ret < 0) {
383 debug("%s: Could not get clock for %s: %d\n", __func__,
384 bus->name, ret);
Simon Glass3595f952015-08-30 16:55:39 -0600385 return ret;
Simon Glass3d156052016-01-21 19:43:42 -0700386 }
Simon Glass3d156052016-01-21 19:43:42 -0700387
388 return 0;
Simon Glass3595f952015-08-30 16:55:39 -0600389}
390
Simon Glass3d156052016-01-21 19:43:42 -0700391static int rockchip_i2c_probe(struct udevice *bus)
392{
393 struct rk_i2c *priv = dev_get_priv(bus);
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300394 struct rk_i2c_soc_data *soc_data;
395 struct udevice *pinctrl;
396 int bus_nr;
397 int ret;
Simon Glass3d156052016-01-21 19:43:42 -0700398
Philipp Tomsicha9d953f2017-09-11 22:04:23 +0200399 priv->regs = dev_read_addr_ptr(bus);
Simon Glass3d156052016-01-21 19:43:42 -0700400
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300401 soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus);
402
403 if (soc_data->controller_type == RK_I2C_LEGACY) {
404 ret = dev_read_alias_seq(bus, &bus_nr);
405 if (ret < 0) {
406 debug("%s: Could not get alias for %s: %d\n",
407 __func__, bus->name, ret);
408 return ret;
409 }
410
411 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
412 if (ret) {
413 debug("%s: Cannot find pinctrl device\n", __func__);
414 return ret;
415 }
416
417 /* pinctrl will switch I2C to new type */
418 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr);
419 if (ret) {
420 debug("%s: Failed to switch I2C to new type %s: %d\n",
421 __func__, bus->name, ret);
422 return ret;
423 }
424 }
425
Simon Glass3d156052016-01-21 19:43:42 -0700426 return 0;
427}
428
Simon Glass3595f952015-08-30 16:55:39 -0600429static const struct dm_i2c_ops rockchip_i2c_ops = {
430 .xfer = rockchip_i2c_xfer,
431 .set_bus_speed = rockchip_i2c_set_bus_speed,
432};
433
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300434static const struct rk_i2c_soc_data rk3066_soc_data = {
435 .controller_type = RK_I2C_LEGACY,
436};
437
438static const struct rk_i2c_soc_data rk3188_soc_data = {
439 .controller_type = RK_I2C_LEGACY,
440};
441
442static const struct rk_i2c_soc_data rk3228_soc_data = {
443 .controller_type = RK_I2C_NEW,
444};
445
446static const struct rk_i2c_soc_data rk3288_soc_data = {
447 .controller_type = RK_I2C_NEW,
448};
449
450static const struct rk_i2c_soc_data rk3328_soc_data = {
451 .controller_type = RK_I2C_NEW,
452};
453
454static const struct rk_i2c_soc_data rk3399_soc_data = {
455 .controller_type = RK_I2C_NEW,
456};
457
Simon Glass3595f952015-08-30 16:55:39 -0600458static const struct udevice_id rockchip_i2c_ids[] = {
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300459 {
460 .compatible = "rockchip,rk3066-i2c",
461 .data = (ulong)&rk3066_soc_data,
462 },
463 {
464 .compatible = "rockchip,rk3188-i2c",
465 .data = (ulong)&rk3188_soc_data,
466 },
467 {
468 .compatible = "rockchip,rk3228-i2c",
469 .data = (ulong)&rk3228_soc_data,
470 },
471 {
472 .compatible = "rockchip,rk3288-i2c",
473 .data = (ulong)&rk3288_soc_data,
474 },
475 {
476 .compatible = "rockchip,rk3328-i2c",
477 .data = (ulong)&rk3328_soc_data,
478 },
479 {
480 .compatible = "rockchip,rk3399-i2c",
481 .data = (ulong)&rk3399_soc_data,
482 },
Simon Glass3595f952015-08-30 16:55:39 -0600483 { }
484};
485
486U_BOOT_DRIVER(i2c_rockchip) = {
487 .name = "i2c_rockchip",
488 .id = UCLASS_I2C,
489 .of_match = rockchip_i2c_ids,
Simon Glass3d156052016-01-21 19:43:42 -0700490 .ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
Simon Glass3595f952015-08-30 16:55:39 -0600491 .probe = rockchip_i2c_probe,
492 .priv_auto_alloc_size = sizeof(struct rk_i2c),
493 .ops = &rockchip_i2c_ops,
494};