blob: fa167268ae718f4370e7a726895be7df1397dd48 [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
Simon Glass3595f952015-08-30 16:55:39 -06009#include <clk.h>
10#include <dm.h>
11#include <errno.h>
12#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass3595f952015-08-30 16:55:39 -060014#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>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glass3595f952015-08-30 16:55:39 -060020#include <linux/sizes.h>
21
Simon Glass3595f952015-08-30 16:55:39 -060022/* i2c timerout */
23#define I2C_TIMEOUT_MS 100
24#define I2C_RETRY_COUNT 3
25
26/* rk i2c fifo max transfer bytes */
27#define RK_I2C_FIFO_SIZE 32
28
29struct rk_i2c {
Stephen Warrena9622432016-06-17 09:44:00 -060030 struct clk clk;
Simon Glass3595f952015-08-30 16:55:39 -060031 struct i2c_regs *regs;
32 unsigned int speed;
Simon Glass3595f952015-08-30 16:55:39 -060033};
34
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +030035enum {
36 RK_I2C_LEGACY,
37 RK_I2C_NEW,
38};
39
40/**
41 * @controller_type: i2c controller type
42 */
43struct rk_i2c_soc_data {
44 int controller_type;
45};
46
Simon Glass3595f952015-08-30 16:55:39 -060047static inline void rk_i2c_get_div(int div, int *divh, int *divl)
48{
49 *divl = div / 2;
50 if (div % 2 == 0)
51 *divh = div / 2;
52 else
53 *divh = DIV_ROUND_UP(div, 2);
54}
55
56/*
57 * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
58 * SCL = PCLK / SCLK Divisor
59 * i2c_rate = PCLK
60 */
61static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
62{
63 uint32_t i2c_rate;
64 int div, divl, divh;
65
66 /* First get i2c rate from pclk */
Stephen Warrena9622432016-06-17 09:44:00 -060067 i2c_rate = clk_get_rate(&i2c->clk);
Simon Glass3595f952015-08-30 16:55:39 -060068
69 div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
70 divh = 0;
71 divl = 0;
72 if (div >= 0)
73 rk_i2c_get_div(div, &divh, &divl);
74 writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
75
76 debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
77 scl_rate);
78 debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
79 debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
80}
81
82static void rk_i2c_show_regs(struct i2c_regs *regs)
83{
84#ifdef DEBUG
85 uint i;
86
87 debug("i2c_con: 0x%08x\n", readl(&regs->con));
88 debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
89 debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
90 debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
91 debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
92 debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
93 debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
94 debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
95 debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
96 for (i = 0; i < 8; i++)
97 debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
98 for (i = 0; i < 8; i++)
99 debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
100#endif
101}
102
103static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
104{
105 struct i2c_regs *regs = i2c->regs;
106 ulong start;
107
108 debug("I2c Send Start bit.\n");
109 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
110
111 writel(I2C_CON_EN | I2C_CON_START, &regs->con);
112 writel(I2C_STARTIEN, &regs->ien);
113
114 start = get_timer(0);
115 while (1) {
116 if (readl(&regs->ipd) & I2C_STARTIPD) {
117 writel(I2C_STARTIPD, &regs->ipd);
118 break;
119 }
120 if (get_timer(start) > I2C_TIMEOUT_MS) {
121 debug("I2C Send Start Bit Timeout\n");
122 rk_i2c_show_regs(regs);
123 return -ETIMEDOUT;
124 }
125 udelay(1);
126 }
127
128 return 0;
129}
130
131static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
132{
133 struct i2c_regs *regs = i2c->regs;
134 ulong start;
135
136 debug("I2c Send Stop bit.\n");
137 writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
138
139 writel(I2C_CON_EN | I2C_CON_STOP, &regs->con);
140 writel(I2C_CON_STOP, &regs->ien);
141
142 start = get_timer(0);
143 while (1) {
144 if (readl(&regs->ipd) & I2C_STOPIPD) {
145 writel(I2C_STOPIPD, &regs->ipd);
146 break;
147 }
148 if (get_timer(start) > I2C_TIMEOUT_MS) {
149 debug("I2C Send Start Bit Timeout\n");
150 rk_i2c_show_regs(regs);
151 return -ETIMEDOUT;
152 }
153 udelay(1);
154 }
155
156 return 0;
157}
158
159static inline void rk_i2c_disable(struct rk_i2c *i2c)
160{
161 writel(0, &i2c->regs->con);
162}
163
164static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
165 uchar *buf, uint b_len)
166{
167 struct i2c_regs *regs = i2c->regs;
168 uchar *pbuf = buf;
169 uint bytes_remain_len = b_len;
170 uint bytes_xferred = 0;
171 uint words_xferred = 0;
172 ulong start;
173 uint con = 0;
174 uint rxdata;
175 uint i, j;
176 int err;
Wadim Egorov838d7542017-08-03 13:48:11 +0200177 bool snd_chunk = false;
Simon Glass3595f952015-08-30 16:55:39 -0600178
179 debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
180 chip, reg, r_len, b_len);
181
182 err = rk_i2c_send_start_bit(i2c);
183 if (err)
184 return err;
185
186 writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
187 if (r_len == 0) {
188 writel(0, &regs->mrxraddr);
189 } else if (r_len < 4) {
190 writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
191 } else {
192 debug("I2C Read: addr len %d not supported\n", r_len);
193 return -EIO;
194 }
195
196 while (bytes_remain_len) {
197 if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
Wadim Egorov838d7542017-08-03 13:48:11 +0200198 con = I2C_CON_EN;
Simon Glass3595f952015-08-30 16:55:39 -0600199 bytes_xferred = 32;
200 } else {
Wadim Egorov838d7542017-08-03 13:48:11 +0200201 /*
202 * The hw can read up to 32 bytes at a time. If we need
203 * more than one chunk, send an ACK after the last byte.
204 */
205 con = I2C_CON_EN | I2C_CON_LASTACK;
Simon Glass3595f952015-08-30 16:55:39 -0600206 bytes_xferred = bytes_remain_len;
207 }
208 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
209
Wadim Egorov838d7542017-08-03 13:48:11 +0200210 /*
211 * make sure we are in plain RX mode if we read a second chunk
212 */
213 if (snd_chunk)
214 con |= I2C_CON_MOD(I2C_MODE_RX);
215 else
216 con |= I2C_CON_MOD(I2C_MODE_TRX);
217
Simon Glass3595f952015-08-30 16:55:39 -0600218 writel(con, &regs->con);
219 writel(bytes_xferred, &regs->mrxcnt);
220 writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
221
222 start = get_timer(0);
223 while (1) {
224 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
225 writel(I2C_NAKRCVIPD, &regs->ipd);
226 err = -EREMOTEIO;
227 }
228 if (readl(&regs->ipd) & I2C_MBRFIPD) {
229 writel(I2C_MBRFIPD, &regs->ipd);
230 break;
231 }
232 if (get_timer(start) > I2C_TIMEOUT_MS) {
233 debug("I2C Read Data Timeout\n");
234 err = -ETIMEDOUT;
235 rk_i2c_show_regs(regs);
236 goto i2c_exit;
237 }
238 udelay(1);
239 }
240
241 for (i = 0; i < words_xferred; i++) {
242 rxdata = readl(&regs->rxdata[i]);
243 debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
244 for (j = 0; j < 4; j++) {
245 if ((i * 4 + j) == bytes_xferred)
246 break;
247 *pbuf++ = (rxdata >> (j * 8)) & 0xff;
248 }
249 }
250
251 bytes_remain_len -= bytes_xferred;
Wadim Egorov838d7542017-08-03 13:48:11 +0200252 snd_chunk = true;
Simon Glass3595f952015-08-30 16:55:39 -0600253 debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
254 }
255
256i2c_exit:
Simon Glass3595f952015-08-30 16:55:39 -0600257 rk_i2c_disable(i2c);
258
259 return err;
260}
261
262static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
263 uchar *buf, uint b_len)
264{
265 struct i2c_regs *regs = i2c->regs;
266 int err;
267 uchar *pbuf = buf;
268 uint bytes_remain_len = b_len + r_len + 1;
269 uint bytes_xferred = 0;
270 uint words_xferred = 0;
271 ulong start;
272 uint txdata;
273 uint i, j;
274
275 debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
276 chip, reg, r_len, b_len);
277 err = rk_i2c_send_start_bit(i2c);
278 if (err)
279 return err;
280
281 while (bytes_remain_len) {
282 if (bytes_remain_len > RK_I2C_FIFO_SIZE)
John Keepingfebe7632016-08-18 20:08:40 +0100283 bytes_xferred = RK_I2C_FIFO_SIZE;
Simon Glass3595f952015-08-30 16:55:39 -0600284 else
285 bytes_xferred = bytes_remain_len;
286 words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
287
288 for (i = 0; i < words_xferred; i++) {
289 txdata = 0;
290 for (j = 0; j < 4; j++) {
291 if ((i * 4 + j) == bytes_xferred)
292 break;
293
John Keeping27dfc942016-08-18 20:08:42 +0100294 if (i == 0 && j == 0 && pbuf == buf) {
Simon Glass3595f952015-08-30 16:55:39 -0600295 txdata |= (chip << 1);
John Keeping27dfc942016-08-18 20:08:42 +0100296 } else if (i == 0 && j <= r_len && pbuf == buf) {
Simon Glass3595f952015-08-30 16:55:39 -0600297 txdata |= (reg &
298 (0xff << ((j - 1) * 8))) << 8;
299 } else {
300 txdata |= (*pbuf++)<<(j * 8);
301 }
Simon Glass3595f952015-08-30 16:55:39 -0600302 }
John Keepingbcd11c42016-08-18 20:08:41 +0100303 writel(txdata, &regs->txdata[i]);
304 debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
Simon Glass3595f952015-08-30 16:55:39 -0600305 }
306
307 writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), &regs->con);
308 writel(bytes_xferred, &regs->mtxcnt);
309 writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
310
311 start = get_timer(0);
312 while (1) {
313 if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
314 writel(I2C_NAKRCVIPD, &regs->ipd);
315 err = -EREMOTEIO;
316 }
317 if (readl(&regs->ipd) & I2C_MBTFIPD) {
318 writel(I2C_MBTFIPD, &regs->ipd);
319 break;
320 }
321 if (get_timer(start) > I2C_TIMEOUT_MS) {
322 debug("I2C Write Data Timeout\n");
323 err = -ETIMEDOUT;
324 rk_i2c_show_regs(regs);
325 goto i2c_exit;
326 }
327 udelay(1);
328 }
329
330 bytes_remain_len -= bytes_xferred;
331 debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
332 }
333
334i2c_exit:
Simon Glass3595f952015-08-30 16:55:39 -0600335 rk_i2c_disable(i2c);
336
337 return err;
338}
339
340static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
341 int nmsgs)
342{
343 struct rk_i2c *i2c = dev_get_priv(bus);
Ondrej Jirmandc6e2d12023-05-25 14:18:17 +0200344 int ret = 0;
Simon Glass3595f952015-08-30 16:55:39 -0600345
346 debug("i2c_xfer: %d messages\n", nmsgs);
347 for (; nmsgs > 0; nmsgs--, msg++) {
348 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
349 if (msg->flags & I2C_M_RD) {
350 ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
351 msg->len);
352 } else {
353 ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
354 msg->len);
355 }
356 if (ret) {
357 debug("i2c_write: error sending\n");
Ondrej Jirmandc6e2d12023-05-25 14:18:17 +0200358 ret = -EREMOTEIO;
359 break;
Simon Glass3595f952015-08-30 16:55:39 -0600360 }
361 }
362
Vasily Khoruzhickc7954892019-11-16 11:32:57 -0800363 rk_i2c_send_stop_bit(i2c);
364 rk_i2c_disable(i2c);
365
Ondrej Jirmandc6e2d12023-05-25 14:18:17 +0200366 return ret;
Simon Glass3595f952015-08-30 16:55:39 -0600367}
368
369int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
370{
371 struct rk_i2c *i2c = dev_get_priv(bus);
372
373 rk_i2c_set_clk(i2c, speed);
374
375 return 0;
376}
377
Simon Glassaad29ae2020-12-03 16:55:21 -0700378static int rockchip_i2c_of_to_plat(struct udevice *bus)
Simon Glass3595f952015-08-30 16:55:39 -0600379{
Simon Glass3d156052016-01-21 19:43:42 -0700380 struct rk_i2c *priv = dev_get_priv(bus);
Simon Glass3595f952015-08-30 16:55:39 -0600381 int ret;
382
Simon Glass3d156052016-01-21 19:43:42 -0700383 ret = clk_get_by_index(bus, 0, &priv->clk);
384 if (ret < 0) {
385 debug("%s: Could not get clock for %s: %d\n", __func__,
386 bus->name, ret);
Simon Glass3595f952015-08-30 16:55:39 -0600387 return ret;
Simon Glass3d156052016-01-21 19:43:42 -0700388 }
Simon Glass3d156052016-01-21 19:43:42 -0700389
390 return 0;
Simon Glass3595f952015-08-30 16:55:39 -0600391}
392
Simon Glass3d156052016-01-21 19:43:42 -0700393static int rockchip_i2c_probe(struct udevice *bus)
394{
395 struct rk_i2c *priv = dev_get_priv(bus);
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300396 struct rk_i2c_soc_data *soc_data;
397 struct udevice *pinctrl;
398 int bus_nr;
399 int ret;
Simon Glass3d156052016-01-21 19:43:42 -0700400
Philipp Tomsicha9d953f2017-09-11 22:04:23 +0200401 priv->regs = dev_read_addr_ptr(bus);
Simon Glass3d156052016-01-21 19:43:42 -0700402
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300403 soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus);
404
405 if (soc_data->controller_type == RK_I2C_LEGACY) {
406 ret = dev_read_alias_seq(bus, &bus_nr);
407 if (ret < 0) {
408 debug("%s: Could not get alias for %s: %d\n",
409 __func__, bus->name, ret);
410 return ret;
411 }
412
413 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
414 if (ret) {
415 debug("%s: Cannot find pinctrl device\n", __func__);
416 return ret;
417 }
418
419 /* pinctrl will switch I2C to new type */
420 ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr);
421 if (ret) {
422 debug("%s: Failed to switch I2C to new type %s: %d\n",
423 __func__, bus->name, ret);
424 return ret;
425 }
426 }
427
Simon Glass3d156052016-01-21 19:43:42 -0700428 return 0;
429}
430
Simon Glass3595f952015-08-30 16:55:39 -0600431static const struct dm_i2c_ops rockchip_i2c_ops = {
432 .xfer = rockchip_i2c_xfer,
433 .set_bus_speed = rockchip_i2c_set_bus_speed,
434};
435
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300436static const struct rk_i2c_soc_data rk3066_soc_data = {
437 .controller_type = RK_I2C_LEGACY,
438};
439
440static const struct rk_i2c_soc_data rk3188_soc_data = {
441 .controller_type = RK_I2C_LEGACY,
442};
443
444static const struct rk_i2c_soc_data rk3228_soc_data = {
445 .controller_type = RK_I2C_NEW,
446};
447
448static const struct rk_i2c_soc_data rk3288_soc_data = {
449 .controller_type = RK_I2C_NEW,
450};
451
452static const struct rk_i2c_soc_data rk3328_soc_data = {
453 .controller_type = RK_I2C_NEW,
454};
455
456static const struct rk_i2c_soc_data rk3399_soc_data = {
457 .controller_type = RK_I2C_NEW,
458};
459
Simon Glass3595f952015-08-30 16:55:39 -0600460static const struct udevice_id rockchip_i2c_ids[] = {
Alexander Kochetkovc0830bf2018-02-26 20:42:54 +0300461 {
462 .compatible = "rockchip,rk3066-i2c",
463 .data = (ulong)&rk3066_soc_data,
464 },
465 {
466 .compatible = "rockchip,rk3188-i2c",
467 .data = (ulong)&rk3188_soc_data,
468 },
469 {
470 .compatible = "rockchip,rk3228-i2c",
471 .data = (ulong)&rk3228_soc_data,
472 },
473 {
474 .compatible = "rockchip,rk3288-i2c",
475 .data = (ulong)&rk3288_soc_data,
476 },
477 {
478 .compatible = "rockchip,rk3328-i2c",
479 .data = (ulong)&rk3328_soc_data,
480 },
481 {
482 .compatible = "rockchip,rk3399-i2c",
483 .data = (ulong)&rk3399_soc_data,
484 },
Simon Glass3595f952015-08-30 16:55:39 -0600485 { }
486};
487
Walter Lozano2901ac62020-06-25 01:10:04 -0300488U_BOOT_DRIVER(rockchip_rk3066_i2c) = {
489 .name = "rockchip_rk3066_i2c",
Simon Glass3595f952015-08-30 16:55:39 -0600490 .id = UCLASS_I2C,
491 .of_match = rockchip_i2c_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700492 .of_to_plat = rockchip_i2c_of_to_plat,
Simon Glass3595f952015-08-30 16:55:39 -0600493 .probe = rockchip_i2c_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700494 .priv_auto = sizeof(struct rk_i2c),
Simon Glass3595f952015-08-30 16:55:39 -0600495 .ops = &rockchip_i2c_ops,
496};
Walter Lozano48e5b042020-06-25 01:10:06 -0300497
Simon Glassdf65db82020-12-28 20:34:57 -0700498DM_DRIVER_ALIAS(rockchip_rk3066_i2c, rockchip_rk3288_i2c)