blob: e7e96921d21d6efa7ab56e6ccceb41d5f918d4ef [file] [log] [blame]
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +00001/*
Nobuhiro Iwamatsub55b8ee2013-10-11 16:23:54 +09002 * Copyright (C) 2011, 2013 Renesas Solutions Corp.
3 * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +00004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +00006 */
7
8#include <common.h>
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +09009#include <i2c.h>
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000010#include <asm/io.h>
11
Nobuhiro Iwamatsub55b8ee2013-10-11 16:23:54 +090012DECLARE_GLOBAL_DATA_PTR;
13
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000014/* Every register is 32bit aligned, but only 8bits in size */
15#define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
16struct sh_i2c {
17 ureg(icdr);
18 ureg(iccr);
19 ureg(icsr);
20 ureg(icic);
21 ureg(iccl);
22 ureg(icch);
23};
24#undef ureg
25
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000026/* ICCR */
27#define SH_I2C_ICCR_ICE (1 << 7)
28#define SH_I2C_ICCR_RACK (1 << 6)
29#define SH_I2C_ICCR_RTS (1 << 4)
30#define SH_I2C_ICCR_BUSY (1 << 2)
31#define SH_I2C_ICCR_SCP (1 << 0)
32
33/* ICSR / ICIC */
Tetsuyuki Kobayashi57d7c802012-09-13 19:07:57 +000034#define SH_IC_BUSY (1 << 4)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000035#define SH_IC_TACK (1 << 2)
36#define SH_IC_WAIT (1 << 1)
37#define SH_IC_DTE (1 << 0)
38
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +000039#ifdef CONFIG_SH_I2C_8BIT
40/* store 8th bit of iccl and icch in ICIC register */
41#define SH_I2C_ICIC_ICCLB8 (1 << 7)
42#define SH_I2C_ICIC_ICCHB8 (1 << 6)
43#endif
44
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090045static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = {
46 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0,
47#ifdef CONFIG_SYS_I2C_SH_BASE1
48 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1,
49#endif
50#ifdef CONFIG_SYS_I2C_SH_BASE2
51 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
52#endif
53#ifdef CONFIG_SYS_I2C_SH_BASE3
54 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
55#endif
56#ifdef CONFIG_SYS_I2C_SH_BASE4
57 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
58#endif
59};
60
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +000061static u16 iccl, icch;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000062
63#define IRQ_WAIT 1000
64
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090065static void sh_irq_dte(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000066{
67 int i;
68
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090069 for (i = 0; i < IRQ_WAIT; i++) {
70 if (SH_IC_DTE & readb(&dev->icsr))
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000071 break;
72 udelay(10);
73 }
74}
75
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090076static int sh_irq_dte_with_tack(struct sh_i2c *dev)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000077{
78 int i;
79
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090080 for (i = 0; i < IRQ_WAIT; i++) {
81 if (SH_IC_DTE & readb(&dev->icsr))
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000082 break;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090083 if (SH_IC_TACK & readb(&dev->icsr))
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +000084 return -1;
85 udelay(10);
86 }
87 return 0;
88}
89
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090090static void sh_irq_busy(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000091{
92 int i;
93
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +090094 for (i = 0; i < IRQ_WAIT; i++) {
95 if (!(SH_IC_BUSY & readb(&dev->icsr)))
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +000096 break;
97 udelay(10);
98 }
99}
100
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900101static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000102{
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000103 u8 icic = SH_IC_TACK;
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000104
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900105 debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n",
106 __func__, chip, addr, iccl, icch);
107 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
108 setbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000109
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900110 writeb(iccl & 0xff, &dev->iccl);
111 writeb(icch & 0xff, &dev->icch);
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000112#ifdef CONFIG_SH_I2C_8BIT
113 if (iccl > 0xff)
114 icic |= SH_I2C_ICIC_ICCLB8;
115 if (icch > 0xff)
116 icic |= SH_I2C_ICIC_ICCHB8;
117#endif
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900118 writeb(icic, &dev->icic);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000119
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900120 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
121 sh_irq_dte(dev);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000122
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900123 clrbits_8(&dev->icsr, SH_IC_TACK);
124 writeb(chip << 1, &dev->icdr);
125 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000126 return -1;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000127
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900128 writeb(addr, &dev->icdr);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000129 if (stop)
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900130 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000131
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900132 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashid042d712012-09-13 19:08:00 +0000133 return -1;
134 return 0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000135}
136
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900137static void sh_i2c_finish(struct sh_i2c *dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000138{
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900139 writeb(0, &dev->icsr);
140 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000141}
142
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900143static int
144sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000145{
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000146 int ret = -1;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900147 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000148 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000149 udelay(10);
150
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900151 writeb(val, &dev->icdr);
152 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000153 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000154
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900155 writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
156 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000157 goto exit0;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900158 sh_irq_busy(dev);
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000159 ret = 0;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900160
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000161exit0:
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900162 sh_i2c_finish(dev);
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000163 return ret;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000164}
165
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900166static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000167{
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000168 int ret = -1;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000169
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000170#if defined(CONFIG_SH73A0)
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900171 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000172 goto exit0;
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000173#else
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900174 if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000175 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000176 udelay(100);
Tetsuyuki Kobayashi3ce27032012-09-13 19:07:58 +0000177#endif
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000178
Nobuhiro Iwamatsu2035d772013-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 Iwamatsu3dab3e02011-11-14 18:27:04 +0000181
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900182 writeb(chip << 1 | 0x01, &dev->icdr);
183 if (sh_irq_dte_with_tack(dev) != 0)
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000184 goto exit0;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000185
Nobuhiro Iwamatsu2035d772013-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 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 ret = readb(&dev->icdr) & 0xff;
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000191
Nobuhiro Iwamatsu2035d772013-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 Kobayashi0e5fb332012-09-13 19:08:01 +0000196exit0:
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900197 sh_i2c_finish(dev);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000198
199 return ret;
200}
201
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900202static void
203sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000204{
205 int num, denom, tmp;
206
Nobuhiro Iwamatsub55b8ee2013-10-11 16:23:54 +0900207 /* No i2c support prior to relocation */
208 if (!(gd->flags & GD_FLG_RELOC))
209 return;
210
Nobuhiro Iwamatsu3dab3e02011-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 Kobayashib1af67f2012-09-13 19:07:56 +0000220 iccl = (u16)((num/denom) + 1);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000221 else
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000222 iccl = (u16)(num/denom);
Nobuhiro Iwamatsu3dab3e02011-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 Kobayashib1af67f2012-09-13 19:07:56 +0000229 icch = (u16)((num/denom) + 1);
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000230 else
Tetsuyuki Kobayashib1af67f2012-09-13 19:07:56 +0000231 icch = (u16)(num/denom);
Nobuhiro Iwamatsu2035d772013-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 Iwamatsu3dab3e02011-11-14 18:27:04 +0000235}
236
Nobuhiro Iwamatsu2035d772013-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 Iwamatsu3dab3e02011-11-14 18:27:04 +0000239{
Nobuhiro Iwamatsu2035d772013-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 Kobayashi0e5fb332012-09-13 19:08:01 +0000245 if (ret < 0)
246 return -1;
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900247
248 data[i] = ret & 0xff;
249 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
250 }
251
252 return 0;
253}
254
255static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
256 int alen, u8 *data, int len)
257{
258 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)
264 return -1;
Tetsuyuki Kobayashi0e5fb332012-09-13 19:08:01 +0000265 }
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000266 return 0;
267}
268
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900269static int
270sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000271{
Tetsuyuki Kobayashi7a657682014-04-14 17:13:57 +0900272 u8 dummy[1];
273
274 return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900275}
276
277static 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
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000285 return 0;
286}
287
288/*
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900289 * Register RCAR i2c adapters
Nobuhiro Iwamatsu3dab3e02011-11-14 18:27:04 +0000290 */
Nobuhiro Iwamatsu2035d772013-10-29 13:33:51 +0900291U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
292 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED0, 0, 0)
293#ifdef CONFIG_SYS_I2C_SH_BASE1
294U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
295 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED1, 0, 1)
296#endif
297#ifdef CONFIG_SYS_I2C_SH_BASE2
298U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
299 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED2, 0, 2)
300#endif
301#ifdef CONFIG_SYS_I2C_SH_BASE3
302U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
303 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED3, 0, 3)
304#endif
305#ifdef CONFIG_SYS_I2C_SH_BASE4
306U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
307 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED4, 0, 4)
308#endif