2 * drivers/i2c/rcar_i2c.c
4 * Copyright (C) 2013 Renesas Electronics Corporation
5 * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
7 * SPDX-License-Identifier: GPL-2.0
14 DECLARE_GLOBAL_DATA_PTR
;
33 #define MCR_MDBS 0x80 /* non-fifo mode switch */
34 #define MCR_FSCL 0x40 /* override SCL pin */
35 #define MCR_FSDA 0x20 /* override SDA pin */
36 #define MCR_OBPC 0x10 /* override pins */
37 #define MCR_MIE 0x08 /* master if enable */
39 #define MCR_FSB 0x02 /* force stop bit */
40 #define MCR_ESG 0x01 /* en startbit gen. */
43 #define MSR_MNR 0x40 /* nack received */
44 #define MSR_MAL 0x20 /* arbitration lost */
45 #define MSR_MST 0x10 /* sent a stop */
49 #define MSR_MAT 0x01 /* slave addr xfer done */
51 static const struct rcar_i2c
*i2c_dev
[CONFIF_SYS_RCAR_I2C_NUM_CONTROLLERS
] = {
52 (struct rcar_i2c
*)CONFIG_SYS_RCAR_I2C0_BASE
,
53 (struct rcar_i2c
*)CONFIG_SYS_RCAR_I2C1_BASE
,
54 (struct rcar_i2c
*)CONFIG_SYS_RCAR_I2C2_BASE
,
55 (struct rcar_i2c
*)CONFIG_SYS_RCAR_I2C3_BASE
,
58 static void rcar_i2c_raw_rw_common(struct rcar_i2c
*dev
, u8 chip
, uint addr
)
60 /* set slave address */
61 writel(chip
<< 1, &dev
->icmar
);
62 /* set register address */
63 writel(addr
, &dev
->icrxdtxd
);
65 writel(0, &dev
->icmsr
);
66 /* start master send */
67 writel(MCR_MDBS
| MCR_MIE
| MCR_ESG
, &dev
->icmcr
);
69 while ((readl(&dev
->icmsr
) & (MSR_MAT
| MSR_MDE
))
70 != (MSR_MAT
| MSR_MDE
))
74 writel(MCR_MDBS
| MCR_MIE
, &dev
->icmcr
);
76 writel(~(MSR_MAT
| MSR_MDE
), &dev
->icmsr
);
78 while (!(readl(&dev
->icmsr
) & MSR_MDE
))
82 static void rcar_i2c_raw_rw_finish(struct rcar_i2c
*dev
)
84 while (!(readl(&dev
->icmsr
) & MSR_MST
))
87 writel(0, &dev
->icmcr
);
91 rcar_i2c_raw_write(struct rcar_i2c
*dev
, u8 chip
, uint addr
, u8
*val
, int size
)
93 rcar_i2c_raw_rw_common(dev
, chip
, addr
);
96 writel(*val
, &dev
->icrxdtxd
);
98 writel(~MSR_MDE
, &dev
->icmsr
);
100 while (!(readl(&dev
->icmsr
) & MSR_MDE
))
103 /* set stop condition */
104 writel(MCR_MDBS
| MCR_MIE
| MCR_FSB
, &dev
->icmcr
);
106 writel(~MSR_MDE
, &dev
->icmsr
);
108 rcar_i2c_raw_rw_finish(dev
);
114 rcar_i2c_raw_read(struct rcar_i2c
*dev
, u8 chip
, uint addr
)
118 rcar_i2c_raw_rw_common(dev
, chip
, addr
);
120 /* set slave address, receive */
121 writel((chip
<< 1) | 1, &dev
->icmar
);
123 writel(0, &dev
->icmsr
);
124 /* start master receive */
125 writel(MCR_MDBS
| MCR_MIE
| MCR_ESG
, &dev
->icmcr
);
127 while ((readl(&dev
->icmsr
) & (MSR_MAT
| MSR_MDR
))
128 != (MSR_MAT
| MSR_MDR
))
132 writel(MCR_MDBS
| MCR_MIE
, &dev
->icmcr
);
133 /* prepare stop condition */
134 writel(MCR_MDBS
| MCR_MIE
| MCR_FSB
, &dev
->icmcr
);
136 writel(~(MSR_MAT
| MSR_MDR
), &dev
->icmsr
);
138 while (!(readl(&dev
->icmsr
) & MSR_MDR
))
141 /* get receive data */
142 ret
= (u8
)readl(&dev
->icrxdtxd
);
144 writel(~MSR_MDR
, &dev
->icmsr
);
146 rcar_i2c_raw_rw_finish(dev
);
152 * SCL = iicck / (20 + SCGD * 8 + F[(ticf + tr + intd) * iicck])
153 * iicck : I2C internal clock < 20 MHz
154 * ticf : I2C SCL falling time: 35 ns
155 * tr : I2C SCL rising time: 200 ns
156 * intd : LSI internal delay: I2C0: 50 ns I2C1-3: 5
157 * F[n] : n rounded up to an integer
159 static u32
rcar_clock_gen(int i2c_no
, u32 bus_speed
)
161 u32 iicck
, f
, scl
, scgd
;
164 int bit
= 0, cdf_width
= 3;
165 for (bit
= 0; bit
< (1 << cdf_width
); bit
++) {
166 iicck
= CONFIG_HP_CLK_FREQ
/ (1 + bit
);
167 if (iicck
< 20000000)
171 if (bit
> (1 << cdf_width
)) {
172 puts("rcar-i2c: Can not get CDF\n");
179 f
= (35 + 200 + intd
) * (iicck
/ 1000000000);
181 for (scgd
= 0; scgd
< 0x40; scgd
++) {
182 scl
= iicck
/ (20 + (scgd
* 8) + f
);
183 if (scl
<= bus_speed
)
188 puts("rcar-i2c: Can not get SDGB\n");
192 debug("%s: scl: %d\n", __func__
, scl
);
193 debug("%s: bit %x\n", __func__
, bit
);
194 debug("%s: scgd %x\n", __func__
, scgd
);
195 debug("%s: iccr %x\n", __func__
, (scgd
<< (cdf_width
) | bit
));
197 return scgd
<< (cdf_width
) | bit
;
201 rcar_i2c_init(struct i2c_adapter
*adap
, int speed
, int slaveadd
)
203 struct rcar_i2c
*dev
= (struct rcar_i2c
*)i2c_dev
[adap
->hwadapnr
];
206 /* No i2c support prior to relocation */
207 if (!(gd
->flags
& GD_FLG_RELOC
))
212 * slave mode is not used on this driver
214 writel(0, &dev
->icsier
);
215 writel(0, &dev
->icsar
);
216 writel(0, &dev
->icscr
);
217 writel(0, &dev
->icssr
);
219 /* reset master mode */
220 writel(0, &dev
->icmier
);
221 writel(0, &dev
->icmcr
);
222 writel(0, &dev
->icmsr
);
223 writel(0, &dev
->icmar
);
225 icccr
= rcar_clock_gen(adap
->hwadapnr
, adap
->speed
);
227 puts("I2C: Init failed\n");
229 writel(icccr
, &dev
->icccr
);
232 static int rcar_i2c_read(struct i2c_adapter
*adap
, uint8_t chip
,
233 uint addr
, int alen
, u8
*data
, int len
)
235 struct rcar_i2c
*dev
= (struct rcar_i2c
*)i2c_dev
[adap
->hwadapnr
];
238 for (i
= 0; i
< len
; i
++)
239 data
[i
] = rcar_i2c_raw_read(dev
, chip
, addr
+ i
);
244 static int rcar_i2c_write(struct i2c_adapter
*adap
, uint8_t chip
, uint addr
,
245 int alen
, u8
*data
, int len
)
247 struct rcar_i2c
*dev
= (struct rcar_i2c
*)i2c_dev
[adap
->hwadapnr
];
248 return rcar_i2c_raw_write(dev
, chip
, addr
, data
, len
);
252 rcar_i2c_probe(struct i2c_adapter
*adap
, u8 dev
)
254 return rcar_i2c_read(adap
, dev
, 0, 0, NULL
, 0);
257 static unsigned int rcar_i2c_set_bus_speed(struct i2c_adapter
*adap
,
260 struct rcar_i2c
*dev
= (struct rcar_i2c
*)i2c_dev
[adap
->hwadapnr
];
264 rcar_i2c_raw_rw_finish(dev
);
266 icccr
= rcar_clock_gen(adap
->hwadapnr
, speed
);
268 puts("I2C: Init failed\n");
271 writel(icccr
, &dev
->icccr
);
277 * Register RCAR i2c adapters
279 U_BOOT_I2C_ADAP_COMPLETE(rcar_0
, rcar_i2c_init
, rcar_i2c_probe
, rcar_i2c_read
,
280 rcar_i2c_write
, rcar_i2c_set_bus_speed
,
281 CONFIG_SYS_RCAR_I2C0_SPEED
, 0, 0)
282 U_BOOT_I2C_ADAP_COMPLETE(rcar_1
, rcar_i2c_init
, rcar_i2c_probe
, rcar_i2c_read
,
283 rcar_i2c_write
, rcar_i2c_set_bus_speed
,
284 CONFIG_SYS_RCAR_I2C1_SPEED
, 0, 1)
285 U_BOOT_I2C_ADAP_COMPLETE(rcar_2
, rcar_i2c_init
, rcar_i2c_probe
, rcar_i2c_read
,
286 rcar_i2c_write
, rcar_i2c_set_bus_speed
,
287 CONFIG_SYS_RCAR_I2C2_SPEED
, 0, 2)
288 U_BOOT_I2C_ADAP_COMPLETE(rcar_3
, rcar_i2c_init
, rcar_i2c_probe
, rcar_i2c_read
,
289 rcar_i2c_write
, rcar_i2c_set_bus_speed
,
290 CONFIG_SYS_RCAR_I2C3_SPEED
, 0, 3)