]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/i2c/sh_i2c.c
2 * Copyright (C) 2011, 2013 Renesas Solutions Corp.
3 * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 DECLARE_GLOBAL_DATA_PTR
;
13 /* Every register is 32bit aligned, but only 8bits in size */
14 #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1;
25 static struct sh_i2c
*base
;
28 #define SH_I2C_ICCR_ICE (1 << 7)
29 #define SH_I2C_ICCR_RACK (1 << 6)
30 #define SH_I2C_ICCR_RTS (1 << 4)
31 #define SH_I2C_ICCR_BUSY (1 << 2)
32 #define SH_I2C_ICCR_SCP (1 << 0)
35 #define SH_IC_BUSY (1 << 4)
36 #define SH_IC_TACK (1 << 2)
37 #define SH_IC_WAIT (1 << 1)
38 #define SH_IC_DTE (1 << 0)
40 #ifdef CONFIG_SH_I2C_8BIT
41 /* store 8th bit of iccl and icch in ICIC register */
42 #define SH_I2C_ICIC_ICCLB8 (1 << 7)
43 #define SH_I2C_ICIC_ICCHB8 (1 << 6)
46 static u16 iccl
, icch
;
50 static void irq_dte(struct sh_i2c
*base
)
54 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
55 if (SH_IC_DTE
& readb(&base
->icsr
))
61 static int irq_dte_with_tack(struct sh_i2c
*base
)
65 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
66 if (SH_IC_DTE
& readb(&base
->icsr
))
68 if (SH_IC_TACK
& readb(&base
->icsr
))
75 static void irq_busy(struct sh_i2c
*base
)
79 for (i
= 0 ; i
< IRQ_WAIT
; i
++) {
80 if (!(SH_IC_BUSY
& readb(&base
->icsr
)))
86 static int i2c_set_addr(struct sh_i2c
*base
, u8 id
, u8 reg
, int stop
)
90 clrbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
91 setbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
93 writeb(iccl
& 0xff, &base
->iccl
);
94 writeb(icch
& 0xff, &base
->icch
);
95 #ifdef CONFIG_SH_I2C_8BIT
97 icic
|= SH_I2C_ICIC_ICCLB8
;
99 icic
|= SH_I2C_ICIC_ICCHB8
;
101 writeb(icic
, &base
->icic
);
103 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
|SH_I2C_ICCR_BUSY
), &base
->iccr
);
106 clrbits_8(&base
->icsr
, SH_IC_TACK
);
107 writeb(id
<< 1, &base
->icdr
);
108 if (irq_dte_with_tack(base
) != 0)
111 writeb(reg
, &base
->icdr
);
113 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
), &base
->iccr
);
115 if (irq_dte_with_tack(base
) != 0)
120 static void i2c_finish(struct sh_i2c
*base
)
122 writeb(0, &base
->icsr
);
123 clrbits_8(&base
->iccr
, SH_I2C_ICCR_ICE
);
126 static int i2c_raw_write(struct sh_i2c
*base
, u8 id
, u8 reg
, u8 val
)
129 if (i2c_set_addr(base
, id
, reg
, 0) != 0)
133 writeb(val
, &base
->icdr
);
134 if (irq_dte_with_tack(base
) != 0)
137 writeb((SH_I2C_ICCR_ICE
| SH_I2C_ICCR_RTS
), &base
->iccr
);
138 if (irq_dte_with_tack(base
) != 0)
147 static int i2c_raw_read(struct sh_i2c
*base
, u8 id
, u8 reg
)
151 #if defined(CONFIG_SH73A0)
152 if (i2c_set_addr(base
, id
, reg
, 0) != 0)
155 if (i2c_set_addr(base
, id
, reg
, 1) != 0)
160 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RTS
|SH_I2C_ICCR_BUSY
), &base
->iccr
);
163 writeb(id
<< 1 | 0x01, &base
->icdr
);
164 if (irq_dte_with_tack(base
) != 0)
167 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_SCP
), &base
->iccr
);
168 if (irq_dte_with_tack(base
) != 0)
171 ret
= readb(&base
->icdr
) & 0xff;
173 writeb((SH_I2C_ICCR_ICE
|SH_I2C_ICCR_RACK
), &base
->iccr
);
174 readb(&base
->icdr
); /* Dummy read */
182 #ifdef CONFIG_I2C_MULTI_BUS
183 static unsigned int current_bus
;
186 * i2c_set_bus_num - change active I2C bus
187 * @bus: bus index, zero based
188 * @returns: 0 on success, non-0 on failure
190 int i2c_set_bus_num(unsigned int bus
)
192 if ((bus
< 0) || (bus
>= CONFIG_SYS_MAX_I2C_BUS
)) {
193 printf("Bad bus: %d\n", bus
);
199 base
= (void *)CONFIG_SH_I2C_BASE0
;
202 base
= (void *)CONFIG_SH_I2C_BASE1
;
204 #ifdef CONFIG_SH_I2C_BASE2
206 base
= (void *)CONFIG_SH_I2C_BASE2
;
209 #ifdef CONFIG_SH_I2C_BASE3
211 base
= (void *)CONFIG_SH_I2C_BASE3
;
214 #ifdef CONFIG_SH_I2C_BASE4
216 base
= (void *)CONFIG_SH_I2C_BASE4
;
228 * i2c_get_bus_num - returns index of active I2C bus
230 unsigned int i2c_get_bus_num(void)
236 #define SH_I2C_ICCL_CALC(clk, date, t_low, t_high) \
237 ((clk / rate) * (t_low / t_low + t_high))
238 #define SH_I2C_ICCH_CALC(clk, date, t_low, t_high) \
239 ((clk / rate) * (t_high / t_low + t_high))
241 void i2c_init(int speed
, int slaveaddr
)
245 /* No i2c support prior to relocation */
246 if (!(gd
->flags
& GD_FLG_RELOC
))
249 #ifdef CONFIG_I2C_MULTI_BUS
252 base
= (struct sh_i2c
*)CONFIG_SH_I2C_BASE0
;
255 * Calculate the value for iccl. From the data sheet:
256 * iccl = (p-clock / transfer-rate) * (L / (L + H))
257 * where L and H are the SCL low and high ratio.
259 num
= CONFIG_SH_I2C_CLOCK
* CONFIG_SH_I2C_DATA_LOW
;
260 denom
= speed
* (CONFIG_SH_I2C_DATA_HIGH
+ CONFIG_SH_I2C_DATA_LOW
);
261 tmp
= num
* 10 / denom
;
263 iccl
= (u16
)((num
/denom
) + 1);
265 iccl
= (u16
)(num
/denom
);
267 /* Calculate the value for icch. From the data sheet:
268 icch = (p clock / transfer rate) * (H / (L + H)) */
269 num
= CONFIG_SH_I2C_CLOCK
* CONFIG_SH_I2C_DATA_HIGH
;
270 tmp
= num
* 10 / denom
;
272 icch
= (u16
)((num
/denom
) + 1);
274 icch
= (u16
)(num
/denom
);
278 * i2c_read: - Read multiple bytes from an i2c device
280 * The higher level routines take into account that this function is only
281 * called with len < page length of the device (see configuration file)
283 * @chip: address of the chip which is to be read
284 * @addr: i2c data address within the chip
285 * @alen: length of the i2c data address (1..2 bytes)
286 * @buffer: where to write the data
287 * @len: how much byte do we want to read
288 * @return: 0 in case of success
290 int i2c_read(u8 chip
, u32 addr
, int alen
, u8
*buffer
, int len
)
294 for (i
= 0 ; i
< len
; i
++) {
295 ret
= i2c_raw_read(base
, chip
, addr
+ i
);
298 buffer
[i
] = ret
& 0xff;
304 * i2c_write: - Write multiple bytes to an i2c device
306 * The higher level routines take into account that this function is only
307 * called with len < page length of the device (see configuration file)
309 * @chip: address of the chip which is to be written
310 * @addr: i2c data address within the chip
311 * @alen: length of the i2c data address (1..2 bytes)
312 * @buffer: where to find the data to be written
313 * @len: how much byte do we want to read
314 * @return: 0 in case of success
316 int i2c_write(u8 chip
, u32 addr
, int alen
, u8
*buffer
, int len
)
319 for (i
= 0; i
< len
; i
++)
320 if (i2c_raw_write(base
, chip
, addr
+ i
, buffer
[i
]) != 0)
326 * i2c_probe: - Test if a chip answers for a given i2c address
328 * @chip: address of the chip which is searched for
329 * @return: 0 if a chip was found, -1 otherwhise
331 int i2c_probe(u8 chip
)
335 ret
= i2c_set_addr(base
, chip
, 0, 1);