1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2011, 2013 Renesas Solutions Corp.
4 * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
6 * NOTE: This driver should be converted to driver model before June 2017.
7 * Please see doc/driver-model/i2c-howto.txt for instructions.
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Every register is 32bit aligned, but only 8bits in size */
17 #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
29 #define SH_I2C_ICCR_ICE (1 << 7)
30 #define SH_I2C_ICCR_RACK (1 << 6)
31 #define SH_I2C_ICCR_RTS (1 << 4)
32 #define SH_I2C_ICCR_BUSY (1 << 2)
33 #define SH_I2C_ICCR_SCP (1 << 0)
36 #define SH_IC_BUSY (1 << 4)
37 #define SH_IC_TACK (1 << 2)
38 #define SH_IC_WAIT (1 << 1)
39 #define SH_IC_DTE (1 << 0)
41 #ifdef CONFIG_SH_I2C_8BIT
42 /* store 8th bit of iccl and icch in ICIC register */
43 #define SH_I2C_ICIC_ICCLB8 (1 << 7)
44 #define SH_I2C_ICIC_ICCHB8 (1 << 6)
47 static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = {
48 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0,
49 #ifdef CONFIG_SYS_I2C_SH_BASE1
50 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1,
52 #ifdef CONFIG_SYS_I2C_SH_BASE2
53 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
55 #ifdef CONFIG_SYS_I2C_SH_BASE3
56 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
58 #ifdef CONFIG_SYS_I2C_SH_BASE4
59 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
63 static u16 iccl, icch;
67 static void sh_irq_dte(struct sh_i2c *dev)
71 for (i = 0; i < IRQ_WAIT; i++) {
72 if (SH_IC_DTE & readb(&dev->icsr))
78 static int sh_irq_dte_with_tack(struct sh_i2c *dev)
82 for (i = 0; i < IRQ_WAIT; i++) {
83 if (SH_IC_DTE & readb(&dev->icsr))
85 if (SH_IC_TACK & readb(&dev->icsr))
92 static void sh_irq_busy(struct sh_i2c *dev)
96 for (i = 0; i < IRQ_WAIT; i++) {
97 if (!(SH_IC_BUSY & readb(&dev->icsr)))
103 static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
105 u8 icic = SH_IC_TACK;
107 debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n",
108 __func__, chip, addr, iccl, icch);
109 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
110 setbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
112 writeb(iccl & 0xff, &dev->iccl);
113 writeb(icch & 0xff, &dev->icch);
114 #ifdef CONFIG_SH_I2C_8BIT
116 icic |= SH_I2C_ICIC_ICCLB8;
118 icic |= SH_I2C_ICIC_ICCHB8;
120 writeb(icic, &dev->icic);
122 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
125 clrbits_8(&dev->icsr, SH_IC_TACK);
126 writeb(chip << 1, &dev->icdr);
127 if (sh_irq_dte_with_tack(dev) != 0)
130 writeb(addr, &dev->icdr);
132 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
134 if (sh_irq_dte_with_tack(dev) != 0)
139 static void sh_i2c_finish(struct sh_i2c *dev)
141 writeb(0, &dev->icsr);
142 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
146 sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
149 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
153 writeb(val, &dev->icdr);
154 if (sh_irq_dte_with_tack(dev) != 0)
157 writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
158 if (sh_irq_dte_with_tack(dev) != 0)
168 static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
172 #if defined(CONFIG_SH73A0)
173 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
176 if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
181 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
184 writeb(chip << 1 | 0x01, &dev->icdr);
185 if (sh_irq_dte_with_tack(dev) != 0)
188 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr);
189 if (sh_irq_dte_with_tack(dev) != 0)
192 ret = readb(&dev->icdr) & 0xff;
194 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr);
195 readb(&dev->icdr); /* Dummy read */
205 sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
209 /* No i2c support prior to relocation */
210 if (!(gd->flags & GD_FLG_RELOC))
214 * Calculate the value for iccl. From the data sheet:
215 * iccl = (p-clock / transfer-rate) * (L / (L + H))
216 * where L and H are the SCL low and high ratio.
218 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW;
219 denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW);
220 tmp = num * 10 / denom;
222 iccl = (u16)((num/denom) + 1);
224 iccl = (u16)(num/denom);
226 /* Calculate the value for icch. From the data sheet:
227 icch = (p clock / transfer rate) * (H / (L + H)) */
228 num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH;
229 tmp = num * 10 / denom;
231 icch = (u16)((num/denom) + 1);
233 icch = (u16)(num/denom);
235 debug("clock: %d, speed %d, iccl: %x, icch: %x\n",
236 CONFIG_SH_I2C_CLOCK, speed, iccl, icch);
239 static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip,
240 uint addr, int alen, u8 *data, int len)
243 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
245 for (i = 0; i < len; i++) {
246 ret = sh_i2c_raw_read(dev, chip, addr + i);
250 data[i] = ret & 0xff;
251 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
257 static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
258 int alen, u8 *data, int len)
260 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
263 for (i = 0; i < len; i++) {
264 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
265 if (sh_i2c_raw_write(dev, chip, addr + i, data[i]) != 0)
272 sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
276 return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
279 static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap,
282 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
285 sh_i2c_init(adap, speed, 0);
291 * Register RCAR i2c adapters
293 U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
294 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED0, 0, 0)
295 #ifdef CONFIG_SYS_I2C_SH_BASE1
296 U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
297 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED1, 0, 1)
299 #ifdef CONFIG_SYS_I2C_SH_BASE2
300 U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
301 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED2, 0, 2)
303 #ifdef CONFIG_SYS_I2C_SH_BASE3
304 U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
305 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED3, 0, 3)
307 #ifdef CONFIG_SYS_I2C_SH_BASE4
308 U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read,
309 sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SH_SPEED4, 0, 4)