3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 static void i2c_start (void);
32 static void i2c_stop (void);
33 static int i2c_write (u8 data
);
34 static void i2c_read (u8
* data
);
36 static inline void i2c_port_start (void);
37 static inline void i2c_clock (unsigned int val
);
38 static inline void i2c_data (unsigned int val
);
39 static inline unsigned int
41 static inline void i2c_write_bit (unsigned int val
);
42 static inline unsigned int
45 static inline void i2c_udelay (unsigned int time
);
56 err
= ! i2c_write(dev
);
60 err
= ! i2c_write(offset
);
70 err
= ! i2c_write(dev
| 0x01);
83 static inline void i2c_udelay (
88 asm volatile("mtdec %0" : : "r" (time
* ((CFG_BUS_CLK
/ 4) / 1000000)));
92 asm volatile("isync; mfdec %0" : "=r" (v
));
96 /* Low-level hardware access
99 #define BIT_GPDATA 0x80000000
100 #define BIT_GPCLK 0x40000000
102 static inline void i2c_port_start (void)
104 out32(REG(CPC0
, GPDIR
), in32(REG(CPC0
, GPDIR
)) & ~(BIT_GPCLK
| BIT_GPDATA
));
105 out32(REG(CPC0
, GPOUT
), in32(REG(CPC0
, GPOUT
)) & ~(BIT_GPCLK
| BIT_GPDATA
));
111 static inline void i2c_clock (
116 out32(REG(CPC0
, GPDIR
), in32(REG(CPC0
, GPDIR
)) & ~BIT_GPCLK
);
120 out32(REG(CPC0
, GPDIR
), in32(REG(CPC0
, GPDIR
)) | BIT_GPCLK
);
128 static inline void i2c_data (
133 out32(REG(CPC0
, GPDIR
), in32(REG(CPC0
, GPDIR
)) & ~BIT_GPDATA
);
137 out32(REG(CPC0
, GPDIR
), in32(REG(CPC0
, GPDIR
)) | BIT_GPDATA
);
145 static inline unsigned int i2c_in (void)
147 unsigned int val
= ((in32(REG(CPC0
, GPIN
)) & BIT_GPDATA
) != 0)?1:0;
155 /* Protocol implementation
158 static inline void i2c_write_bit (
169 static inline unsigned int i2c_read_bit (void)
187 unsigned int i2c_reset (void)
203 } while ((i
<9)&&(val
==0));
208 static void i2c_start (void)
219 static void i2c_stop (void)
229 static int i2c_write (
234 for (i
= 0; i
< 8; i
++)
236 i2c_write_bit(data
>> 7);
240 return i2c_read_bit() == 0;
243 static void i2c_read (
249 for (i
= 0; i
< 8; i
++)
252 val
|= i2c_read_bit();
256 i2c_write_bit(1); /* NoAck */