blob: f857307e6a4f23696de6316ceec2e573bf1548ce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +00002/*
Nobuhiro Iwamatsub55b8ee2013-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 Iwamatsu3dab3e02011-11-14 18:27:04 +00005 *
Simon Glass28527092016-11-23 06:34:44 -07006 * NOTE: This driver should be converted to driver model before June 2017.
Heinrich Schuchardt2799a692020-02-25 21:35:39 +01007 * Please see doc/driver-model/i2c-howto.rst for instructions.
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +00008 */
9
10#include <common.h>
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090011#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000013#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000015
Nobuhiro Iwamatsub55b8ee2013-10-11 16:23:54 +090016DECLARE_GLOBAL_DATA_PTR;
17
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000018/* Every register is 32bit aligned, but only 8bits in size */
19#define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
20struct sh_i2c {
21 ureg(icdr);
22 ureg(iccr);
23 ureg(icsr);
24 ureg(icic);
25 ureg(iccl);
26 ureg(icch);
27};
28#undef ureg
29
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000030/* ICCR */
31#define SH_I2C_ICCR_ICE (1 << 7)
32#define SH_I2C_ICCR_RACK (1 << 6)
33#define SH_I2C_ICCR_RTS (1 << 4)
34#define SH_I2C_ICCR_BUSY (1 << 2)
35#define SH_I2C_ICCR_SCP (1 << 0)
36
37/* ICSR / ICIC */
Tetsuyuki Kobayashi57d7c802012-09-13 19:07:57 +000038#define SH_IC_BUSY (1 << 4)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000039#define SH_IC_TACK (1 << 2)
40#define SH_IC_WAIT (1 << 1)
41#define SH_IC_DTE (1 << 0)
42
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +000043#ifdef CONFIG_SH_I2C_8BIT
44/* store 8th bit of iccl and icch in ICIC register */
45#define SH_I2C_ICIC_ICCLB8 (1 << 7)
46#define SH_I2C_ICIC_ICCHB8 (1 << 6)
47#endif
48
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090049static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = {
50 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0,
51#ifdef CONFIG_SYS_I2C_SH_BASE1
52 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1,
53#endif
54#ifdef CONFIG_SYS_I2C_SH_BASE2
55 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
56#endif
57#ifdef CONFIG_SYS_I2C_SH_BASE3
58 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
59#endif
60#ifdef CONFIG_SYS_I2C_SH_BASE4
61 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
62#endif
63};
64
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +000065static u16 iccl, icch;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000066
67#define IRQ_WAIT 1000
68
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090069static void sh_irq_dte(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000070{
71 int i;
72
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090073 for (i = 0; i < IRQ_WAIT; i++) {
74 if (SH_IC_DTE & readb(&dev->icsr))
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000075 break;
76 udelay(10);
77 }
78}
79
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090080static int sh_irq_dte_with_tack(struct sh_i2c *dev)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000081{
82 int i;
83
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090084 for (i = 0; i < IRQ_WAIT; i++) {
85 if (SH_IC_DTE & readb(&dev->icsr))
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000086 break;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090087 if (SH_IC_TACK & readb(&dev->icsr))
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000088 return -1;
89 udelay(10);
90 }
91 return 0;
92}
93
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090094static void sh_irq_busy(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000095{
96 int i;
97
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090098 for (i = 0; i < IRQ_WAIT; i++) {
99 if (!(SH_IC_BUSY & readb(&dev->icsr)))
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000100 break;
101 udelay(10);
102 }
103}
104
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900105static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000106{
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000107 u8 icic = SH_IC_TACK;
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000108
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900109 debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n",
110 __func__, chip, addr, iccl, icch);
111 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
112 setbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000113
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900114 writeb(iccl & 0xff, &dev->iccl);
115 writeb(icch & 0xff, &dev->icch);
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000116#ifdef CONFIG_SH_I2C_8BIT
117 if (iccl > 0xff)
118 icic |= SH_I2C_ICIC_ICCLB8;
119 if (icch > 0xff)
120 icic |= SH_I2C_ICIC_ICCHB8;
121#endif
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900122 writeb(icic, &dev->icic);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000123
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900124 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
125 sh_irq_dte(dev);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000126
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900127 clrbits_8(&dev->icsr, SH_IC_TACK);
128 writeb(chip << 1, &dev->icdr);
129 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000130 return -1;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000131
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900132 writeb(addr, &dev->icdr);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000133 if (stop)
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900134 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000135
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900136 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000137 return -1;
138 return 0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000139}
140
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900141static void sh_i2c_finish(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000142{
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900143 writeb(0, &dev->icsr);
144 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000145}
146
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900147static int
148sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000149{
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000150 int ret = -1;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900151 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000152 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000153 udelay(10);
154
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900155 writeb(val, &dev->icdr);
156 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000157 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000158
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900159 writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
160 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000161 goto exit0;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900162 sh_irq_busy(dev);
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000163 ret = 0;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900164
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000165exit0:
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900166 sh_i2c_finish(dev);
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000167 return ret;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000168}
169
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900170static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000171{
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000172 int ret = -1;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000173
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000174#if defined(CONFIG_SH73A0)
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900175 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000176 goto exit0;
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000177#else
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900178 if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000179 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000180 udelay(100);
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000181#endif
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000182
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900183 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
184 sh_irq_dte(dev);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000185
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900186 writeb(chip << 1 | 0x01, &dev->icdr);
187 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000188 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000189
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900190 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr);
191 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000192 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000193
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900194 ret = readb(&dev->icdr) & 0xff;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000195
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900196 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr);
197 readb(&dev->icdr); /* Dummy read */
198 sh_irq_busy(dev);
199
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000200exit0:
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900201 sh_i2c_finish(dev);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000202
203 return ret;
204}
205
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900206static void
207sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000208{
209 int num, denom, tmp;
210
Nobuhiro Iwamatsub55b8ee2013-10-11 16:23:54 +0900211 /* No i2c support prior to relocation */
212 if (!(gd->flags & GD_FLG_RELOC))
213 return;
214
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000215 /*
216 * Calculate the value for iccl. From the data sheet:
217 * iccl = (p-clock / transfer-rate) * (L / (L + H))
218 * where L and H are the SCL low and high ratio.
219 */
220 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
221 denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
222 tmp = num * 10 / denom;
223 if (tmp % 10 >= 5)
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000224 iccl = (u16)((num/denom) + 1);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000225 else
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000226 iccl = (u16)(num/denom);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000227
228 /* Calculate the value for icch. From the data sheet:
229 icch = (p clock / transfer rate) * (H / (L + H)) */
230 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
231 tmp = num * 10 / denom;
232 if (tmp % 10 >= 5)
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000233 icch = (u16)((num/denom) + 1);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000234 else
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000235 icch = (u16)(num/denom);
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900236
237 debug("clock: %d, speed %d, iccl: %x, icch: %x\n",
238 CONFIG_SH_I2C_CLOCK, speed, iccl, icch);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000239}
240
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900241static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip,
242 uint addr, int alen, u8 *data, int len)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000243{
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900244 int ret, i;
245 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
246
247 for (i = 0; i < len; i++) {
248 ret = sh_i2c_raw_read(dev, chip, addr + i);
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000249 if (ret < 0)
250 return -1;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900251
252 data[i] = ret & 0xff;
253 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
254 }
255
256 return 0;
257}
258
259static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
260 int alen, u8 *data, int len)
261{
262 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
263 int i;
264
265 for (i = 0; i < len; i++) {
266 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
267 if (sh_i2c_raw_write(dev, chip, addr + i, data[i]) != 0)
268 return -1;
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000269 }
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000270 return 0;
271}
272
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900273static int
274sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000275{
Tetsuyuki Kobayashi7a657682014-04-14 17:13:57 +0900276 u8 dummy[1];
277
278 return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900279}
280
281static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap,
282 unsigned int speed)
283{
284 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
285
286 sh_i2c_finish(dev);
287 sh_i2c_init(adap, speed, 0);
288
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000289 return 0;
290}
291
292/*
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900293 * Register RCAR i2c adapters
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000294 */
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900295U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
296 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED0, 0, 0)
297#ifdef CONFIG_SYS_I2C_SH_BASE1
298U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
299 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED1, 0, 1)
300#endif
301#ifdef CONFIG_SYS_I2C_SH_BASE2
302U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
303 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED2, 0, 2)
304#endif
305#ifdef CONFIG_SYS_I2C_SH_BASE3
306U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
307 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED3, 0, 3)
308#endif
309#ifdef CONFIG_SYS_I2C_SH_BASE4
310U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
311 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED4, 0, 4)
312#endif