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.
23 #include <gpxe/bitbash.h>
28 * I2C bit-bashing interface
30 * This implements a simple I2C master via a bit-bashing interface
31 * that provides two lines: SCL (clock) and SDA (data).
35 * Delay between output state changes
37 * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
38 * i.e. 200k clock transitions per second.
40 static void i2c_delay ( void ) {
41 udelay ( I2C_UDELAY );
45 * Set state of I2C SCL line
47 * @v basher Bit-bashing interface
48 * @v state New state of SCL
50 static void setscl ( struct bit_basher *basher, int state ) {
51 write_bit ( basher, I2C_BIT_SCL, state );
56 * Set state of I2C SDA line
58 * @v basher Bit-bashing interface
59 * @v state New state of SDA
61 static void setsda ( struct bit_basher *basher, int state ) {
62 write_bit ( basher, I2C_BIT_SDA, state );
67 * Get state of I2C SDA line
69 * @v basher Bit-bashing interface
70 * @ret state State of SDA
72 static int getsda ( struct bit_basher *basher ) {
73 return read_bit ( basher, I2C_BIT_SDA );
77 * Send an I2C start condition
79 * @v basher Bit-bashing interface
81 static void i2c_start ( struct bit_basher *basher ) {
89 * Send an I2C data bit
91 * @v basher Bit-bashing interface
94 static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
95 setsda ( basher, bit );
102 * Receive an I2C data bit
104 * @v basher Bit-bashing interface
105 * @ret bit Received bit
107 static int i2c_recv_bit ( struct bit_basher *basher ) {
110 setscl ( basher, 1 );
111 bit = getsda ( basher );
112 setscl ( basher, 0 );
117 * Send an I2C stop condition
119 * @v basher Bit-bashing interface
121 static void i2c_stop ( struct bit_basher *basher ) {
122 setsda ( basher, 0 );
123 setscl ( basher, 1 );
124 setsda ( basher, 1 );
128 * Send byte via I2C bus and check for acknowledgement
130 * @v basher Bit-bashing interface
131 * @v byte Byte to send
132 * @ret rc Return status code
134 * Sends a byte via the I2C bus and checks for an acknowledgement from
137 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
141 for ( i = 8 ; i ; i-- ) {
142 i2c_send_bit ( basher, byte & 0x80 );
146 /* Check for acknowledgement from slave */
147 return ( i2c_recv_bit ( basher ) == 0 ? 0 : -EIO );
151 * Receive byte via I2C bus
153 * @v basher Bit-bashing interface
154 * @ret byte Received byte
156 * Receives a byte via the I2C bus and sends NACK to the slave device.
158 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
163 for ( i = 8 ; i ; i-- ) {
165 value |= ( i2c_recv_bit ( basher ) & 0x1 );
169 i2c_send_bit ( basher, 1 );
175 * Select I2C device for reading or writing
177 * @v basher Bit-bashing interface
178 * @v i2cdev I2C device
179 * @v direction I2C_READ or I2C_WRITE
180 * @ret rc Return status code
182 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
183 unsigned int direction ) {
184 unsigned int address;
187 i2c_start ( basher );
189 /* First byte of the address */
190 address = i2cdev->address;
191 if ( i2cdev->tenbit ) {
192 address |= I2C_TENBIT_ADDRESS;
195 if ( ( rc = i2c_send_byte ( basher,
196 ( ( address << 1 ) | direction ) ) ) != 0 )
199 /* Second byte of the address (10-bit addresses only) */
200 if ( i2cdev->tenbit ) {
201 if ( ( rc = i2c_send_byte ( basher,
202 ( i2cdev->address & 0xff ) ) ) !=0)
210 * Read data from I2C device via bit-bashing interface
212 * @v i2c I2C interface
213 * @v i2cdev I2C device
214 * @v offset Starting offset within the device
215 * @v data Data buffer
216 * @v len Length of data buffer
217 * @ret rc Return status code
219 * Note that attempting to read zero bytes of data is a valid way to
220 * check for I2C device presence.
222 static int i2c_bit_read ( struct i2c_interface *i2c,
223 struct i2c_device *i2cdev, unsigned int offset,
224 uint8_t *data, unsigned int len ) {
225 struct i2c_bit_basher *i2cbit
226 = container_of ( i2c, struct i2c_bit_basher, i2c );
227 struct bit_basher *basher = &i2cbit->basher;
230 DBG ( "Reading from I2C device %x: ", i2cdev->address );
234 /* Select device for writing */
235 if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
238 /* Abort at end of data */
243 if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
246 /* Select device for reading */
247 if ( ( rc = i2c_select ( basher, i2cdev, I2C_READ ) ) != 0 )
251 *data++ = i2c_recv_byte ( basher );
252 DBG ( "%02x ", *(data - 1) );
255 DBG ( "%s\n", ( rc ? "failed" : "" ) );
261 * Write data to I2C device via bit-bashing interface
263 * @v i2c I2C interface
264 * @v i2cdev I2C device
265 * @v offset Starting offset within the device
266 * @v data Data buffer
267 * @v len Length of data buffer
268 * @ret rc Return status code
270 * Note that attempting to write zero bytes of data is a valid way to
271 * check for I2C device presence.
273 static int i2c_bit_write ( struct i2c_interface *i2c,
274 struct i2c_device *i2cdev, unsigned int offset,
275 const uint8_t *data, unsigned int len ) {
276 struct i2c_bit_basher *i2cbit
277 = container_of ( i2c, struct i2c_bit_basher, i2c );
278 struct bit_basher *basher = &i2cbit->basher;
281 DBG ( "Writing to I2C device %x: ", i2cdev->address );
285 /* Select device for writing */
286 if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
289 /* Abort at end of data */
294 if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
297 /* Write data to device */
298 DBG ( "%02x ", *data );
299 if ( ( rc = i2c_send_byte ( basher, *data++ ) ) != 0 )
303 DBG ( "%s\n", ( rc ? "failed" : "" ) );
309 * Initialise I2C bit-bashing interface
311 * @v i2cbit I2C bit-bashing interface
313 void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
314 struct bit_basher *basher = &i2cbit->basher;
316 assert ( basher->read != NULL );
317 assert ( basher->write != NULL );
318 i2cbit->i2c.read = i2c_bit_read;
319 i2cbit->i2c.write = i2c_bit_write;