2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/bitbash.h>
30 * I2C bit-bashing interface
32 * This implements a simple I2C master via a bit-bashing interface
33 * that provides two lines: SCL (clock) and SDA (data).
37 * Delay between output state changes
39 * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
40 * i.e. 200k clock transitions per second.
42 static void i2c_delay ( void ) {
43 udelay ( I2C_UDELAY );
47 * Set state of I2C SCL line
49 * @v basher Bit-bashing interface
50 * @v state New state of SCL
52 static void setscl ( struct bit_basher *basher, int state ) {
53 write_bit ( basher, I2C_BIT_SCL, state );
58 * Set state of I2C SDA line
60 * @v basher Bit-bashing interface
61 * @v state New state of SDA
63 static void setsda ( struct bit_basher *basher, int state ) {
64 write_bit ( basher, I2C_BIT_SDA, state );
69 * Get state of I2C SDA line
71 * @v basher Bit-bashing interface
72 * @ret state State of SDA
74 static int getsda ( struct bit_basher *basher ) {
75 return read_bit ( basher, I2C_BIT_SDA );
79 * Send an I2C start condition
81 * @v basher Bit-bashing interface
83 static void i2c_start ( struct bit_basher *basher ) {
91 * Send an I2C data bit
93 * @v basher Bit-bashing interface
96 static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
97 setsda ( basher, bit );
100 setsda ( basher, 1 );
104 * Receive an I2C data bit
106 * @v basher Bit-bashing interface
107 * @ret bit Received bit
109 static int i2c_recv_bit ( struct bit_basher *basher ) {
112 setscl ( basher, 1 );
113 bit = getsda ( basher );
114 setscl ( basher, 0 );
119 * Send an I2C stop condition
121 * @v basher Bit-bashing interface
123 static void i2c_stop ( struct bit_basher *basher ) {
124 setsda ( basher, 0 );
125 setscl ( basher, 1 );
126 setsda ( basher, 1 );
130 * Send byte via I2C bus and check for acknowledgement
132 * @v basher Bit-bashing interface
133 * @v byte Byte to send
134 * @ret rc Return status code
136 * Sends a byte via the I2C bus and checks for an acknowledgement from
139 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
143 for ( i = 8 ; i ; i-- ) {
144 i2c_send_bit ( basher, byte & 0x80 );
148 /* Check for acknowledgement from slave */
149 return ( i2c_recv_bit ( basher ) == 0 ? 0 : -EIO );
153 * Receive byte via I2C bus
155 * @v basher Bit-bashing interface
156 * @ret byte Received byte
158 * Receives a byte via the I2C bus and sends NACK to the slave device.
160 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
165 for ( i = 8 ; i ; i-- ) {
167 value |= ( i2c_recv_bit ( basher ) & 0x1 );
171 i2c_send_bit ( basher, 1 );
177 * Select I2C device for reading or writing
179 * @v basher Bit-bashing interface
180 * @v i2cdev I2C device
181 * @v direction I2C_READ or I2C_WRITE
182 * @ret rc Return status code
184 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
185 unsigned int direction ) {
186 unsigned int address;
189 i2c_start ( basher );
191 /* First byte of the address */
192 address = i2cdev->address;
193 if ( i2cdev->tenbit ) {
194 address |= I2C_TENBIT_ADDRESS;
197 if ( ( rc = i2c_send_byte ( basher,
198 ( ( address << 1 ) | direction ) ) ) != 0 )
201 /* Second byte of the address (10-bit addresses only) */
202 if ( i2cdev->tenbit ) {
203 if ( ( rc = i2c_send_byte ( basher,
204 ( i2cdev->address & 0xff ) ) ) !=0)
212 * Read data from I2C device via bit-bashing interface
214 * @v i2c I2C interface
215 * @v i2cdev I2C device
216 * @v offset Starting offset within the device
217 * @v data Data buffer
218 * @v len Length of data buffer
219 * @ret rc Return status code
221 * Note that attempting to read zero bytes of data is a valid way to
222 * check for I2C device presence.
224 static int i2c_bit_read ( struct i2c_interface *i2c,
225 struct i2c_device *i2cdev, unsigned int offset,
226 uint8_t *data, unsigned int len ) {
227 struct i2c_bit_basher *i2cbit
228 = container_of ( i2c, struct i2c_bit_basher, i2c );
229 struct bit_basher *basher = &i2cbit->basher;
232 DBG ( "Reading from I2C device %x: ", i2cdev->address );
236 /* Select device for writing */
237 if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
240 /* Abort at end of data */
245 if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
248 /* Select device for reading */
249 if ( ( rc = i2c_select ( basher, i2cdev, I2C_READ ) ) != 0 )
253 *data++ = i2c_recv_byte ( basher );
254 DBG ( "%02x ", *(data - 1) );
257 DBG ( "%s\n", ( rc ? "failed" : "" ) );
263 * Write data to I2C device via bit-bashing interface
265 * @v i2c I2C interface
266 * @v i2cdev I2C device
267 * @v offset Starting offset within the device
268 * @v data Data buffer
269 * @v len Length of data buffer
270 * @ret rc Return status code
272 * Note that attempting to write zero bytes of data is a valid way to
273 * check for I2C device presence.
275 static int i2c_bit_write ( struct i2c_interface *i2c,
276 struct i2c_device *i2cdev, unsigned int offset,
277 const uint8_t *data, unsigned int len ) {
278 struct i2c_bit_basher *i2cbit
279 = container_of ( i2c, struct i2c_bit_basher, i2c );
280 struct bit_basher *basher = &i2cbit->basher;
283 DBG ( "Writing to I2C device %x: ", i2cdev->address );
287 /* Select device for writing */
288 if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
291 /* Abort at end of data */
296 if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
299 /* Write data to device */
300 DBG ( "%02x ", *data );
301 if ( ( rc = i2c_send_byte ( basher, *data++ ) ) != 0 )
305 DBG ( "%s\n", ( rc ? "failed" : "" ) );
311 * Initialise I2C bit-bashing interface
313 * @v i2cbit I2C bit-bashing interface
315 void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
316 struct bit_basher *basher = &i2cbit->basher;
318 assert ( basher->read != NULL );
319 assert ( basher->write != NULL );
320 i2cbit->i2c.read = i2c_bit_read;
321 i2cbit->i2c.write = i2c_bit_write;