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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <ipxe/bitbash.h>
33 * I2C bit-bashing interface
35 * This implements a simple I2C master via a bit-bashing interface
36 * that provides two lines: SCL (clock) and SDA (data).
40 * Delay between output state changes
42 * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
43 * i.e. 200k clock transitions per second.
45 static void i2c_delay ( void ) {
46 udelay ( I2C_UDELAY );
50 * Set state of I2C SCL line
52 * @v basher Bit-bashing interface
53 * @v state New state of SCL
55 static void setscl ( struct bit_basher *basher, int state ) {
56 DBG2 ( "%c", ( state ? '/' : '\\' ) );
57 write_bit ( basher, I2C_BIT_SCL, state );
62 * Set state of I2C SDA line
64 * @v basher Bit-bashing interface
65 * @v state New state of SDA
67 static void setsda ( struct bit_basher *basher, int state ) {
68 DBG2 ( "%c", ( state ? '1' : '0' ) );
69 write_bit ( basher, I2C_BIT_SDA, state );
74 * Get state of I2C SDA line
76 * @v basher Bit-bashing interface
77 * @ret state State of SDA
79 static int getsda ( struct bit_basher *basher ) {
81 state = read_bit ( basher, I2C_BIT_SDA );
82 DBG2 ( "%c", ( state ? '+' : '-' ) );
87 * Send an I2C start condition
89 * @v basher Bit-bashing interface
91 static void i2c_start ( struct bit_basher *basher ) {
99 * Send an I2C data bit
101 * @v basher Bit-bashing interface
104 static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
105 setsda ( basher, bit );
106 setscl ( basher, 1 );
107 setscl ( basher, 0 );
108 setsda ( basher, 1 );
112 * Receive an I2C data bit
114 * @v basher Bit-bashing interface
115 * @ret bit Received bit
117 static int i2c_recv_bit ( struct bit_basher *basher ) {
120 setscl ( basher, 1 );
121 bit = getsda ( basher );
122 setscl ( basher, 0 );
127 * Send an I2C stop condition
129 * @v basher Bit-bashing interface
131 static void i2c_stop ( struct bit_basher *basher ) {
132 setsda ( basher, 0 );
133 setscl ( basher, 1 );
134 setsda ( basher, 1 );
138 * Send byte via I2C bus and check for acknowledgement
140 * @v basher Bit-bashing interface
141 * @v byte Byte to send
142 * @ret rc Return status code
144 * Sends a byte via the I2C bus and checks for an acknowledgement from
147 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
152 DBG2 ( "[send %02x]", byte );
153 for ( i = 8 ; i ; i-- ) {
154 i2c_send_bit ( basher, byte & 0x80 );
158 /* Check for acknowledgement from slave */
159 ack = ( i2c_recv_bit ( basher ) == 0 );
160 DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
162 return ( ack ? 0 : -EIO );
166 * Receive byte via I2C bus
168 * @v basher Bit-bashing interface
169 * @ret byte Received byte
171 * Receives a byte via the I2C bus and sends NACK to the slave device.
173 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
178 for ( i = 8 ; i ; i-- ) {
180 byte |= ( i2c_recv_bit ( basher ) & 0x1 );
184 i2c_send_bit ( basher, 1 );
186 DBG2 ( "[rcvd %02x]", byte );
191 * Select I2C device for reading or writing
193 * @v basher Bit-bashing interface
194 * @v i2cdev I2C device
195 * @v offset Starting offset within the device
196 * @v direction I2C_READ or I2C_WRITE
197 * @ret rc Return status code
199 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
200 unsigned int offset, unsigned int direction ) {
201 unsigned int address;
206 i2c_start ( basher );
208 /* Calculate address to appear on bus */
209 address = ( ( ( i2cdev->dev_addr |
210 ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
213 /* Send address a byte at a time */
214 for ( shift = ( 8 * ( i2cdev->dev_addr_len - 1 ) ) ;
215 shift >= 0 ; shift -= 8 ) {
216 byte = ( ( address >> shift ) & 0xff );
217 if ( ( rc = i2c_send_byte ( basher, byte ) ) != 0 )
227 * @v basher Bit-bashing interface
228 * @ret rc Return status code
230 * i2c devices often don't have a reset line, so even a reboot or
231 * system power cycle is sometimes not enough to bring them back to a
234 static int i2c_reset ( struct bit_basher *basher ) {
238 /* Clock through several cycles, waiting for an opportunity to
239 * pull SDA low while SCL is high (which creates a start
243 setscl ( basher, 0 );
244 setsda ( basher, 1 );
245 for ( i = 0 ; i < I2C_RESET_MAX_CYCLES ; i++ ) {
246 setscl ( basher, 1 );
247 sda = getsda ( basher );
249 /* Now that the device will see a start, issue it */
250 i2c_start ( basher );
251 /* Stop the bus to leave it in a known good state */
253 DBGC ( basher, "I2CBIT %p reset after %d attempts\n",
255 close_bit ( basher );
258 setscl ( basher, 0 );
261 DBGC ( basher, "I2CBIT %p could not reset after %d attempts\n",
263 close_bit ( basher );
268 * Read data from I2C device via bit-bashing interface
270 * @v i2c I2C interface
271 * @v i2cdev I2C device
272 * @v offset Starting offset within the device
273 * @v data Data buffer
274 * @v len Length of data buffer
275 * @ret rc Return status code
277 * Note that attempting to read zero bytes of data is a valid way to
278 * check for I2C device presence.
280 static int i2c_bit_read ( struct i2c_interface *i2c,
281 struct i2c_device *i2cdev, unsigned int offset,
282 uint8_t *data, unsigned int len ) {
283 struct i2c_bit_basher *i2cbit
284 = container_of ( i2c, struct i2c_bit_basher, i2c );
285 struct bit_basher *basher = &i2cbit->basher;
288 DBGC ( basher, "I2CBIT %p reading from device %x: ",
289 basher, i2cdev->dev_addr );
293 for ( ; ; data++, offset++ ) {
295 /* Select device for writing */
296 if ( ( rc = i2c_select ( basher, i2cdev, offset,
300 /* Abort at end of data */
305 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
308 /* Select device for reading */
309 if ( ( rc = i2c_select ( basher, i2cdev, offset,
314 *data = i2c_recv_byte ( basher );
315 DBGC ( basher, "%02x ", *data );
318 DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
320 close_bit ( basher );
325 * Write data to I2C device via bit-bashing interface
327 * @v i2c I2C interface
328 * @v i2cdev I2C device
329 * @v offset Starting offset within the device
330 * @v data Data buffer
331 * @v len Length of data buffer
332 * @ret rc Return status code
334 * Note that attempting to write zero bytes of data is a valid way to
335 * check for I2C device presence.
337 static int i2c_bit_write ( struct i2c_interface *i2c,
338 struct i2c_device *i2cdev, unsigned int offset,
339 const uint8_t *data, unsigned int len ) {
340 struct i2c_bit_basher *i2cbit
341 = container_of ( i2c, struct i2c_bit_basher, i2c );
342 struct bit_basher *basher = &i2cbit->basher;
345 DBGC ( basher, "I2CBIT %p writing to device %x: ",
346 basher, i2cdev->dev_addr );
350 for ( ; ; data++, offset++ ) {
352 /* Select device for writing */
353 if ( ( rc = i2c_select ( basher, i2cdev, offset,
357 /* Abort at end of data */
362 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
365 /* Write data to device */
366 DBGC ( basher, "%02x ", *data );
367 if ( ( rc = i2c_send_byte ( basher, *data ) ) != 0 )
371 DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
373 close_bit ( basher );
378 * Initialise I2C bit-bashing interface
380 * @v i2cbit I2C bit-bashing interface
381 * @v bash_op Bit-basher operations
383 int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
384 struct bit_basher_operations *bash_op ) {
385 struct bit_basher *basher = &i2cbit->basher;
388 /* Initialise data structures */
389 basher->op = bash_op;
390 assert ( basher->op->read != NULL );
391 assert ( basher->op->write != NULL );
392 i2cbit->i2c.read = i2c_bit_read;
393 i2cbit->i2c.write = i2c_bit_write;
396 if ( ( rc = i2c_reset ( basher ) ) != 0 ) {
397 DBGC ( basher, "I2CBIT %p could not reset I2C bus: %s\n",
398 basher, strerror ( rc ) );