]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/i2c/sh_i2c.c
2 * Copyright (C) 2011 Renesas Solutions Corp.
3 * Copyright (C) 2011 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 /* Every register is 32bit aligned, but only 8bits in size */
12 #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
23 static struct sh_i2c
*base
;
26 #define SH_I2C_ICCR_ICE (1 << 7)
27 #define SH_I2C_ICCR_RACK (1 << 6)
28 #define SH_I2C_ICCR_RTS (1 << 4)
29 #define SH_I2C_ICCR_BUSY (1 << 2)
30 #define SH_I2C_ICCR_SCP (1 << 0)
33 #define SH_IC_BUSY (1 << 4)
34 #define SH_IC_TACK (1 << 2)
35 #define SH_IC_WAIT (1 << 1)
36 #define SH_IC_DTE (1 << 0)
38 #ifdef CONFIG_SH_I2C_8BIT
39 /* store 8th bit of iccl and icch in ICIC register */
40 #define SH_I2C_ICIC_ICCLB8 (1 << 7)
41 #define SH_I2C_ICIC_ICCHB8 (1 << 6)
44 static u16 iccl
, icch
;
48 static void irq_dte(struct sh_i2c
*base
)
52 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
53 if (SH_IC_DTE
& readb(&base
->icsr
))
59 static int irq_dte_with_tack(struct sh_i2c
*base
)
63 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
64 if (SH_IC_DTE
& readb(&base
->icsr
))
66 if (SH_IC_TACK
& readb(&base
->icsr
))
73 static void irq_busy(struct sh_i2c
*base
)
77 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
78 if (!(SH_IC_BUSY
& readb(&base
->icsr
)))
84 static int i2c_set_addr(struct sh_i2c
*base
, u8 id
, u8 reg
, int stop
)
88 clrbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
89 setbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
91 writeb(iccl
& 0xff, &base
->iccl
);
92 writeb(icch
& 0xff, &base
->icch
);
93 #ifdef CONFIG_SH_I2C_8BIT
95 icic
|= SH_I2C_ICIC_ICCLB8
;
97 icic
|= SH_I2C_ICIC_ICCHB8
;
99 writeb(icic
, &base
->icic
);
101 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
|SH_I2C_ICCR_BUSY
), &base
->iccr
);
104 clrbits_8(&base
->icsr
, SH_IC_TACK
);
105 writeb(id
<< 1, &base
->icdr
);
106 if (irq_dte_with_tack(base
) != 0)
109 writeb(reg
, &base
->icdr
);
111 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
), &base
->iccr
);
113 if (irq_dte_with_tack(base
) != 0)
118 static void i2c_finish(struct sh_i2c
*base
)
120 writeb(0, &base
->icsr
);
121 clrbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
124 static int i2c_raw_write(struct sh_i2c
*base
, u8 id
, u8 reg
, u8 val
)
127 if (i2c_set_addr(base
, id
, reg
, 0) != 0)
131 writeb(val
, &base
->icdr
);
132 if (irq_dte_with_tack(base
) != 0)
135 writeb((SH_I2C_ICCR_ICE
| SH_I2C_ICCR_RTS
), &base
->iccr
);
136 if (irq_dte_with_tack(base
) != 0)
145 static int i2c_raw_read(struct sh_i2c
*base
, u8 id
, u8 reg
)
149 #if defined(CONFIG_SH73A0)
150 if (i2c_set_addr(base
, id
, reg
, 0) != 0)
153 if (i2c_set_addr(base
, id
, reg
, 1) != 0)
158 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
|SH_I2C_ICCR_BUSY
), &base
->iccr
);
161 writeb(id
<< 1 | 0x01, &base
->icdr
);
162 if (irq_dte_with_tack(base
) != 0)
165 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_SCP
), &base
->iccr
);
166 if (irq_dte_with_tack(base
) != 0)
169 ret
= readb(&base
->icdr
) & 0xff;
171 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RACK
), &base
->iccr
);
172 readb(&base
->icdr
); /* Dummy read */
180 #ifdef CONFIG_I2C_MULTI_BUS
181 static unsigned int current_bus
;
184 * i2c_set_bus_num - change active I2C bus
185 * @bus: bus index, zero based
186 * @returns: 0 on success, non-0 on failure
188 int i2c_set_bus_num(unsigned int bus
)
190 if ((bus
< 0) || (bus
>= CONFIG_SYS_MAX_I2C_BUS
)) {
191 printf("Bad bus: %d\n", bus
);
197 base
= (void *)CONFIG_SH_I2C_BASE0
;
200 base
= (void *)CONFIG_SH_I2C_BASE1
;
202 #ifdef CONFIG_SH_I2C_BASE2
204 base
= (void *)CONFIG_SH_I2C_BASE2
;
207 #ifdef CONFIG_SH_I2C_BASE3
209 base
= (void *)CONFIG_SH_I2C_BASE3
;
212 #ifdef CONFIG_SH_I2C_BASE4
214 base
= (void *)CONFIG_SH_I2C_BASE4
;
226 * i2c_get_bus_num - returns index of active I2C bus
228 unsigned int i2c_get_bus_num(void)
234 #define SH_I2C_ICCL_CALC(clk, date, t_low, t_high) \
235 ((clk / rate) * (t_low / t_low + t_high))
236 #define SH_I2C_ICCH_CALC(clk, date, t_low, t_high) \
237 ((clk / rate) * (t_high / t_low + t_high))
239 void i2c_init(int speed
, int slaveaddr
)
243 #ifdef CONFIG_I2C_MULTI_BUS
246 base
= (struct sh_i2c
*)CONFIG_SH_I2C_BASE0
;
249 * Calculate the value for iccl. From the data sheet:
250 * iccl = (p-clock / transfer-rate) * (L / (L + H))
251 * where L and H are the SCL low and high ratio.
253 num
= CONFIG_SH_I2C_CLOCK
* CONFIG_SH_I2C_DATA_LOW
;
254 denom
= speed
* (CONFIG_SH_I2C_DATA_HIGH
+ CONFIG_SH_I2C_DATA_LOW
);
255 tmp
= num
* 10 / denom
;
257 iccl
= (u16
)((num
/denom
) + 1);
259 iccl
= (u16
)(num
/denom
);
261 /* Calculate the value for icch. From the data sheet:
262 icch = (p clock / transfer rate) * (H / (L + H)) */
263 num
= CONFIG_SH_I2C_CLOCK
* CONFIG_SH_I2C_DATA_HIGH
;
264 tmp
= num
* 10 / denom
;
266 icch
= (u16
)((num
/denom
) + 1);
268 icch
= (u16
)(num
/denom
);
272 * i2c_read: - Read multiple bytes from an i2c device
274 * The higher level routines take into account that this function is only
275 * called with len < page length of the device (see configuration file)
277 * @chip: address of the chip which is to be read
278 * @addr: i2c data address within the chip
279 * @alen: length of the i2c data address (1..2 bytes)
280 * @buffer: where to write the data
281 * @len: how much byte do we want to read
282 * @return: 0 in case of success
284 int i2c_read(u8 chip
, u32 addr
, int alen
, u8
*buffer
, int len
)
288 for (i
= 0 ; i
< len
; i
++) {
289 ret
= i2c_raw_read(base
, chip
, addr
+ i
);
292 buffer
[i
] = ret
& 0xff;
298 * i2c_write: - Write multiple bytes to an i2c device
300 * The higher level routines take into account that this function is only
301 * called with len < page length of the device (see configuration file)
303 * @chip: address of the chip which is to be written
304 * @addr: i2c data address within the chip
305 * @alen: length of the i2c data address (1..2 bytes)
306 * @buffer: where to find the data to be written
307 * @len: how much byte do we want to read
308 * @return: 0 in case of success
310 int i2c_write(u8 chip
, u32 addr
, int alen
, u8
*buffer
, int len
)
313 for (i
= 0; i
< len
; i
++)
314 if (i2c_raw_write(base
, chip
, addr
+ i
, buffer
[i
]) != 0)
320 * i2c_probe: - Test if a chip answers for a given i2c address
322 * @chip: address of the chip which is searched for
323 * @return: 0 if a chip was found, -1 otherwhise
325 int i2c_probe(u8 chip
)
329 ret
= i2c_set_addr(base
, chip
, 0, 1);