blob: 3335d9482a29f82fc522a68bca9896594a623975 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +00002/*
Nobuhiro Iwamatsu6e35fd22013-10-11 16:23:54 +09003 * Copyright (C) 2011, 2013 Renesas Solutions Corp.
4 * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +00005 *
Simon Glasscb052ff2016-11-23 06:34:44 -07006 * NOTE: This driver should be converted to driver model before June 2017.
Heinrich Schuchardtc79f03c2020-02-25 21:35:39 +01007 * Please see doc/driver-model/i2c-howto.rst for instructions.
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +00008 */
9
10#include <common.h>
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090011#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000014#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000016
Nobuhiro Iwamatsu6e35fd22013-10-11 16:23:54 +090017DECLARE_GLOBAL_DATA_PTR;
18
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000019/* Every register is 32bit aligned, but only 8bits in size */
20#define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
21struct sh_i2c {
22 ureg(icdr);
23 ureg(iccr);
24 ureg(icsr);
25 ureg(icic);
26 ureg(iccl);
27 ureg(icch);
28};
29#undef ureg
30
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000031/* ICCR */
32#define SH_I2C_ICCR_ICE (1 << 7)
33#define SH_I2C_ICCR_RACK (1 << 6)
34#define SH_I2C_ICCR_RTS (1 << 4)
35#define SH_I2C_ICCR_BUSY (1 << 2)
36#define SH_I2C_ICCR_SCP (1 << 0)
37
38/* ICSR / ICIC */
Tetsuyuki Kobayashib788fe32012-09-13 19:07:57 +000039#define SH_IC_BUSY (1 << 4)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000040#define SH_IC_TACK (1 << 2)
41#define SH_IC_WAIT (1 << 1)
42#define SH_IC_DTE (1 << 0)
43
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +000044#ifdef CONFIG_SH_I2C_8BIT
45/* store 8th bit of iccl and icch in ICIC register */
46#define SH_I2C_ICIC_ICCLB8 (1 << 7)
47#define SH_I2C_ICIC_ICCHB8 (1 << 6)
48#endif
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090049
50static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = {
51 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0,
52#ifdef CONFIG_SYS_I2C_SH_BASE1
53 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1,
54#endif
55#ifdef CONFIG_SYS_I2C_SH_BASE2
56 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
57#endif
58#ifdef CONFIG_SYS_I2C_SH_BASE3
59 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
60#endif
61#ifdef CONFIG_SYS_I2C_SH_BASE4
62 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
63#endif
64};
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +000065
66static u16 iccl, icch;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000067
68#define IRQ_WAIT 1000
69
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090070static void sh_irq_dte(struct sh_i2c *dev)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000071{
72 int i;
73
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090074 for (i = 0; i < IRQ_WAIT; i++) {
75 if (SH_IC_DTE & readb(&dev->icsr))
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000076 break;
77 udelay(10);
78 }
79}
80
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090081static int sh_irq_dte_with_tack(struct sh_i2c *dev)
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +000082{
83 int i;
84
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090085 for (i = 0; i < IRQ_WAIT; i++) {
86 if (SH_IC_DTE & readb(&dev->icsr))
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +000087 break;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090088 if (SH_IC_TACK & readb(&dev->icsr))
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +000089 return -1;
90 udelay(10);
91 }
92 return 0;
93}
94
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090095static void sh_irq_busy(struct sh_i2c *dev)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +000096{
97 int i;
98
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +090099 for (i = 0; i < IRQ_WAIT; i++) {
100 if (!(SH_IC_BUSY & readb(&dev->icsr)))
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000101 break;
102 udelay(10);
103 }
104}
105
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900106static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000107{
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +0000108 u8 icic = SH_IC_TACK;
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000109
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900110 debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n",
111 __func__, chip, addr, iccl, icch);
112 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
113 setbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000114
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900115 writeb(iccl & 0xff, &dev->iccl);
116 writeb(icch & 0xff, &dev->icch);
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000117#ifdef CONFIG_SH_I2C_8BIT
118 if (iccl > 0xff)
119 icic |= SH_I2C_ICIC_ICCLB8;
120 if (icch > 0xff)
121 icic |= SH_I2C_ICIC_ICCHB8;
122#endif
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900123 writeb(icic, &dev->icic);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000124
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900125 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
126 sh_irq_dte(dev);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000127
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900128 clrbits_8(&dev->icsr, SH_IC_TACK);
129 writeb(chip << 1, &dev->icdr);
130 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +0000131 return -1;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000132
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900133 writeb(addr, &dev->icdr);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000134 if (stop)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900135 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000136
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900137 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +0000138 return -1;
139 return 0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000140}
141
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900142static void sh_i2c_finish(struct sh_i2c *dev)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000143{
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900144 writeb(0, &dev->icsr);
145 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000146}
147
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900148static int
149sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000150{
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000151 int ret = -1;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900152 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000153 goto exit0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000154 udelay(10);
155
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900156 writeb(val, &dev->icdr);
157 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000158 goto exit0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000159
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900160 writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
161 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000162 goto exit0;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900163 sh_irq_busy(dev);
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000164 ret = 0;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900165
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000166exit0:
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900167 sh_i2c_finish(dev);
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000168 return ret;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000169}
170
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900171static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000172{
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000173 int ret = -1;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000174
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900175 if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000176 goto exit0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000177 udelay(100);
178
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900179 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
180 sh_irq_dte(dev);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000181
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900182 writeb(chip << 1 | 0x01, &dev->icdr);
183 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000184 goto exit0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000185
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900186 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr);
187 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000188 goto exit0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000189
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900190 ret = readb(&dev->icdr) & 0xff;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000191
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900192 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr);
193 readb(&dev->icdr); /* Dummy read */
194 sh_irq_busy(dev);
195
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000196exit0:
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900197 sh_i2c_finish(dev);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000198
199 return ret;
200}
201
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900202static void
203sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000204{
205 int num, denom, tmp;
206
Nobuhiro Iwamatsu6e35fd22013-10-11 16:23:54 +0900207 /* No i2c support prior to relocation */
208 if (!(gd->flags & GD_FLG_RELOC))
209 return;
210
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000211 /*
212 * Calculate the value for iccl. From the data sheet:
213 * iccl = (p-clock / transfer-rate) * (L / (L + H))
214 * where L and H are the SCL low and high ratio.
215 */
216 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
217 denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
218 tmp = num * 10 / denom;
219 if (tmp % 10 >= 5)
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000220 iccl = (u16)((num/denom) + 1);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000221 else
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000222 iccl = (u16)(num/denom);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000223
224 /* Calculate the value for icch. From the data sheet:
225 icch = (p clock / transfer rate) * (H / (L + H)) */
226 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
227 tmp = num * 10 / denom;
228 if (tmp % 10 >= 5)
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000229 icch = (u16)((num/denom) + 1);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000230 else
Tetsuyuki Kobayashicc4283c2012-09-13 19:07:56 +0000231 icch = (u16)(num/denom);
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900232
233 debug("clock: %d, speed %d, iccl: %x, icch: %x\n",
234 CONFIG_SH_I2C_CLOCK, speed, iccl, icch);
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000235}
236
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900237static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip,
238 uint addr, int alen, u8 *data, int len)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000239{
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900240 int ret, i;
241 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
242
243 for (i = 0; i < len; i++) {
244 ret = sh_i2c_raw_read(dev, chip, addr + i);
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000245 if (ret < 0)
246 return -1;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900247
248 data[i] = ret & 0xff;
249 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000250 }
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900251
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000252 return 0;
253}
254
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900255static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
256 int alen, u8 *data, int len)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000257{
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900258 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
259 int i;
260
261 for (i = 0; i < len; i++) {
262 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
263 if (sh_i2c_raw_write(dev, chip, addr + i, data[i]) != 0)
Tetsuyuki Kobayashi773003c2012-09-13 19:08:01 +0000264 return -1;
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900265 }
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000266 return 0;
267}
268
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900269static int
270sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000271{
Tetsuyuki Kobayashi8b5fe132014-04-14 17:13:57 +0900272 u8 dummy[1];
273
274 return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900275}
Tetsuyuki Kobayashicece11c2012-09-13 19:08:00 +0000276
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900277static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap,
278 unsigned int speed)
279{
280 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
281
282 sh_i2c_finish(dev);
283 sh_i2c_init(adap, speed, 0);
284
285 return 0;
Nobuhiro Iwamatsue60b79d2011-11-14 18:27:04 +0000286}
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900287
288/*
289 * Register RCAR i2c adapters
290 */
291U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
Tom Rinib9a254d2021-08-18 23:12:34 -0400292 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 0)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900293#ifdef CONFIG_SYS_I2C_SH_BASE1
294U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
Tom Rinib9a254d2021-08-18 23:12:34 -0400295 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 1)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900296#endif
297#ifdef CONFIG_SYS_I2C_SH_BASE2
298U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
Tom Rinib9a254d2021-08-18 23:12:34 -0400299 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 2)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900300#endif
301#ifdef CONFIG_SYS_I2C_SH_BASE3
302U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
Tom Rinib9a254d2021-08-18 23:12:34 -0400303 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 3)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900304#endif
305#ifdef CONFIG_SYS_I2C_SH_BASE4
306U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
Tom Rinib9a254d2021-08-18 23:12:34 -0400307 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 4)
Nobuhiro Iwamatsu12240102013-10-29 13:33:51 +0900308#endif