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.rst for instructions.
13 #include <asm/global_data.h>
15 #include <linux/delay.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* Every register is 32bit aligned, but only 8bits in size */
20 #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
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)
39 #define SH_IC_BUSY (1 << 4)
40 #define SH_IC_TACK (1 << 2)
41 #define SH_IC_WAIT (1 << 1)
42 #define SH_IC_DTE (1 << 0)
44 #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)
50 static 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,
55 #ifdef CONFIG_SYS_I2C_SH_BASE2
56 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2,
58 #ifdef CONFIG_SYS_I2C_SH_BASE3
59 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3,
61 #ifdef CONFIG_SYS_I2C_SH_BASE4
62 (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4,
66 static u16 iccl, icch;
70 static void sh_irq_dte(struct sh_i2c *dev)
74 for (i = 0; i < IRQ_WAIT; i++) {
75 if (SH_IC_DTE & readb(&dev->icsr))
81 static int sh_irq_dte_with_tack(struct sh_i2c *dev)
85 for (i = 0; i < IRQ_WAIT; i++) {
86 if (SH_IC_DTE & readb(&dev->icsr))
88 if (SH_IC_TACK & readb(&dev->icsr))
95 static void sh_irq_busy(struct sh_i2c *dev)
99 for (i = 0; i < IRQ_WAIT; i++) {
100 if (!(SH_IC_BUSY & readb(&dev->icsr)))
106 static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop)
108 u8 icic = SH_IC_TACK;
110 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);
115 writeb(iccl & 0xff, &dev->iccl);
116 writeb(icch & 0xff, &dev->icch);
117 #ifdef CONFIG_SH_I2C_8BIT
119 icic |= SH_I2C_ICIC_ICCLB8;
121 icic |= SH_I2C_ICIC_ICCHB8;
123 writeb(icic, &dev->icic);
125 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
128 clrbits_8(&dev->icsr, SH_IC_TACK);
129 writeb(chip << 1, &dev->icdr);
130 if (sh_irq_dte_with_tack(dev) != 0)
133 writeb(addr, &dev->icdr);
135 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr);
137 if (sh_irq_dte_with_tack(dev) != 0)
142 static void sh_i2c_finish(struct sh_i2c *dev)
144 writeb(0, &dev->icsr);
145 clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE);
149 sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val)
152 if (sh_i2c_set_addr(dev, chip, addr, 0) != 0)
156 writeb(val, &dev->icdr);
157 if (sh_irq_dte_with_tack(dev) != 0)
160 writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr);
161 if (sh_irq_dte_with_tack(dev) != 0)
171 static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr)
175 if (sh_i2c_set_addr(dev, chip, addr, 1) != 0)
179 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr);
182 writeb(chip << 1 | 0x01, &dev->icdr);
183 if (sh_irq_dte_with_tack(dev) != 0)
186 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr);
187 if (sh_irq_dte_with_tack(dev) != 0)
190 ret = readb(&dev->icdr) & 0xff;
192 writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr);
193 readb(&dev->icdr); /* Dummy read */
203 sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
207 /* No i2c support prior to relocation */
208 if (!(gd->flags & GD_FLG_RELOC))
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.
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;
220 iccl = (u16)((num/denom) + 1);
222 iccl = (u16)(num/denom);
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;
229 icch = (u16)((num/denom) + 1);
231 icch = (u16)(num/denom);
233 debug("clock: %d, speed %d, iccl: %x, icch: %x\n",
234 CONFIG_SH_I2C_CLOCK, speed, iccl, icch);
237 static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip,
238 uint addr, int alen, u8 *data, int len)
241 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
243 for (i = 0; i < len; i++) {
244 ret = sh_i2c_raw_read(dev, chip, addr + i);
248 data[i] = ret & 0xff;
249 debug("%s: data[%d]: %02x\n", __func__, i, data[i]);
255 static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr,
256 int alen, u8 *data, int len)
258 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
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)
270 sh_i2c_probe(struct i2c_adapter *adap, u8 dev)
274 return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy);
277 static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap,
280 struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr];
283 sh_i2c_init(adap, speed, 0);
289 * Register RCAR i2c adapters
291 U_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_SPEED, 0, 0)
293 #ifdef CONFIG_SYS_I2C_SH_BASE1
294 U_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_SPEED, 0, 1)
297 #ifdef CONFIG_SYS_I2C_SH_BASE2
298 U_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_SPEED, 0, 2)
301 #ifdef CONFIG_SYS_I2C_SH_BASE3
302 U_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_SPEED, 0, 3)
305 #ifdef CONFIG_SYS_I2C_SH_BASE4
306 U_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_SPEED, 0, 4)