i2c_bit.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stddef.h>
00022 #include <stdint.h>
00023 #include <errno.h>
00024 #include <string.h>
00025 #include <assert.h>
00026 #include <unistd.h>
00027 #include <gpxe/bitbash.h>
00028 #include <gpxe/i2c.h>
00029 
00030 /** @file
00031  *
00032  * I2C bit-bashing interface
00033  *
00034  * This implements a simple I2C master via a bit-bashing interface
00035  * that provides two lines: SCL (clock) and SDA (data).
00036  */
00037 
00038 /**
00039  * Delay between output state changes
00040  *
00041  * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
00042  * i.e. 200k clock transitions per second.
00043  */
00044 static void i2c_delay ( void ) {
00045         udelay ( I2C_UDELAY );
00046 }
00047 
00048 /**
00049  * Set state of I2C SCL line
00050  *
00051  * @v basher            Bit-bashing interface
00052  * @v state             New state of SCL
00053  */
00054 static void setscl ( struct bit_basher *basher, int state ) {
00055         DBG2 ( "%c", ( state ? '/' : '\\' ) );
00056         write_bit ( basher, I2C_BIT_SCL, state );
00057         i2c_delay();
00058 }
00059 
00060 /**
00061  * Set state of I2C SDA line
00062  *
00063  * @v basher            Bit-bashing interface
00064  * @v state             New state of SDA
00065  */
00066 static void setsda ( struct bit_basher *basher, int state ) {
00067         DBG2 ( "%c", ( state ? '1' : '0' ) );
00068         write_bit ( basher, I2C_BIT_SDA, state );
00069         i2c_delay();
00070 }
00071 
00072 /**
00073  * Get state of I2C SDA line
00074  *
00075  * @v basher            Bit-bashing interface
00076  * @ret state           State of SDA
00077  */
00078 static int getsda ( struct bit_basher *basher ) {
00079         int state;
00080         state = read_bit ( basher, I2C_BIT_SDA );
00081         DBG2 ( "%c", ( state ? '+' : '-' ) );
00082         return state;
00083 }
00084 
00085 /**
00086  * Send an I2C start condition
00087  *
00088  * @v basher            Bit-bashing interface
00089  */
00090 static void i2c_start ( struct bit_basher *basher ) {
00091         setscl ( basher, 1 );
00092         setsda ( basher, 0 );
00093         setscl ( basher, 0 );
00094         setsda ( basher, 1 );
00095 }
00096 
00097 /**
00098  * Send an I2C data bit
00099  *
00100  * @v basher            Bit-bashing interface
00101  * @v bit               Bit to send
00102  */
00103 static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
00104         setsda ( basher, bit );
00105         setscl ( basher, 1 );
00106         setscl ( basher, 0 );
00107         setsda ( basher, 1 );
00108 }
00109 
00110 /**
00111  * Receive an I2C data bit
00112  *
00113  * @v basher            Bit-bashing interface
00114  * @ret bit             Received bit
00115  */
00116 static int i2c_recv_bit ( struct bit_basher *basher ) {
00117         int bit;
00118 
00119         setscl ( basher, 1 );
00120         bit = getsda ( basher );
00121         setscl ( basher, 0 );
00122         return bit;
00123 }
00124 
00125 /**
00126  * Send an I2C stop condition
00127  *
00128  * @v basher            Bit-bashing interface
00129  */
00130 static void i2c_stop ( struct bit_basher *basher ) {
00131         setsda ( basher, 0 );
00132         setscl ( basher, 1 );
00133         setsda ( basher, 1 );
00134 }
00135 
00136 /**
00137  * Send byte via I2C bus and check for acknowledgement
00138  *
00139  * @v basher            Bit-bashing interface
00140  * @v byte              Byte to send
00141  * @ret rc              Return status code
00142  *
00143  * Sends a byte via the I2C bus and checks for an acknowledgement from
00144  * the slave device.
00145  */
00146 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
00147         int i;
00148         int ack;
00149 
00150         /* Send byte */
00151         DBG2 ( "[send %02x]", byte );
00152         for ( i = 8 ; i ; i-- ) {
00153                 i2c_send_bit ( basher, byte & 0x80 );
00154                 byte <<= 1;
00155         }
00156 
00157         /* Check for acknowledgement from slave */
00158         ack = ( i2c_recv_bit ( basher ) == 0 );
00159         DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
00160 
00161         return ( ack ? 0 : -EIO );
00162 }
00163 
00164 /**
00165  * Receive byte via I2C bus
00166  *
00167  * @v basher            Bit-bashing interface
00168  * @ret byte            Received byte
00169  *
00170  * Receives a byte via the I2C bus and sends NACK to the slave device.
00171  */
00172 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
00173         uint8_t byte = 0;
00174         int i;
00175 
00176         /* Receive byte */
00177         for ( i = 8 ; i ; i-- ) {
00178                 byte <<= 1;
00179                 byte |= ( i2c_recv_bit ( basher ) & 0x1 );
00180         }
00181 
00182         /* Send NACK */
00183         i2c_send_bit ( basher, 1 );
00184 
00185         DBG2 ( "[rcvd %02x]", byte );
00186         return byte;
00187 }
00188 
00189 /**
00190  * Select I2C device for reading or writing
00191  *
00192  * @v basher            Bit-bashing interface
00193  * @v i2cdev            I2C device
00194  * @v offset            Starting offset within the device
00195  * @v direction         I2C_READ or I2C_WRITE
00196  * @ret rc              Return status code
00197  */
00198 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
00199                         unsigned int offset, unsigned int direction ) {
00200         unsigned int address;
00201         int shift;
00202         unsigned int byte;
00203         int rc;
00204 
00205         i2c_start ( basher );
00206 
00207         /* Calculate address to appear on bus */
00208         address = ( ( ( i2cdev->dev_addr |
00209                         ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
00210                     | direction );
00211 
00212         /* Send address a byte at a time */
00213         for ( shift = ( 8 * ( i2cdev->dev_addr_len - 1 ) ) ;
00214               shift >= 0 ; shift -= 8 ) {
00215                 byte = ( ( address >> shift ) & 0xff );
00216                 if ( ( rc = i2c_send_byte ( basher, byte ) ) != 0 )
00217                         return rc;
00218         }
00219 
00220         return 0;
00221 }
00222 
00223 /**
00224  * Reset I2C bus
00225  *
00226  * @v basher            Bit-bashing interface
00227  * @ret rc              Return status code
00228  *
00229  * i2c devices often don't have a reset line, so even a reboot or
00230  * system power cycle is sometimes not enough to bring them back to a
00231  * known state.
00232  */
00233 static int i2c_reset ( struct bit_basher *basher ) {
00234         unsigned int i;
00235         int sda;
00236 
00237         /* Clock through several cycles, waiting for an opportunity to
00238          * pull SDA low while SCL is high (which creates a start
00239          * condition).
00240          */
00241         setscl ( basher, 0 );
00242         setsda ( basher, 1 );
00243         for ( i = 0 ; i < I2C_RESET_MAX_CYCLES ; i++ ) {
00244                 setscl ( basher, 1 );
00245                 sda = getsda ( basher );
00246                 if ( sda ) {
00247                         /* Now that the device will see a start, issue it */
00248                         i2c_start ( basher );
00249                         /* Stop the bus to leave it in a known good state */
00250                         i2c_stop ( basher );
00251                         DBGC ( basher, "I2CBIT %p reset after %d attempts\n",
00252                                basher, ( i + 1 ) );
00253                         return 0;
00254                 }
00255                 setscl ( basher, 0 );
00256         }
00257 
00258         DBGC ( basher, "I2CBIT %p could not reset after %d attempts\n",
00259                basher, i );
00260         return -ETIMEDOUT;
00261 }
00262 
00263 /**
00264  * Read data from I2C device via bit-bashing interface
00265  *
00266  * @v i2c               I2C interface
00267  * @v i2cdev            I2C device
00268  * @v offset            Starting offset within the device
00269  * @v data              Data buffer
00270  * @v len               Length of data buffer
00271  * @ret rc              Return status code
00272  *
00273  * Note that attempting to read zero bytes of data is a valid way to
00274  * check for I2C device presence.
00275  */
00276 static int i2c_bit_read ( struct i2c_interface *i2c,
00277                           struct i2c_device *i2cdev, unsigned int offset,
00278                           uint8_t *data, unsigned int len ) {
00279         struct i2c_bit_basher *i2cbit
00280                 = container_of ( i2c, struct i2c_bit_basher, i2c );
00281         struct bit_basher *basher = &i2cbit->basher;
00282         int rc = 0;
00283 
00284         DBGC ( basher, "I2CBIT %p reading from device %x: ",
00285                basher, i2cdev->dev_addr );
00286 
00287         for ( ; ; data++, offset++ ) {
00288 
00289                 /* Select device for writing */
00290                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00291                                          I2C_WRITE ) ) != 0 )
00292                         break;
00293 
00294                 /* Abort at end of data */
00295                 if ( ! ( len-- ) )
00296                         break;
00297 
00298                 /* Select offset */
00299                 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
00300                         break;
00301                 
00302                 /* Select device for reading */
00303                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00304                                          I2C_READ ) ) != 0 )
00305                         break;
00306 
00307                 /* Read byte */
00308                 *data = i2c_recv_byte ( basher );
00309                 DBGC ( basher, "%02x ", *data );
00310         }
00311         
00312         DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
00313         i2c_stop ( basher );
00314         return rc;
00315 }
00316 
00317 /**
00318  * Write data to I2C device via bit-bashing interface
00319  *
00320  * @v i2c               I2C interface
00321  * @v i2cdev            I2C device
00322  * @v offset            Starting offset within the device
00323  * @v data              Data buffer
00324  * @v len               Length of data buffer
00325  * @ret rc              Return status code
00326  *
00327  * Note that attempting to write zero bytes of data is a valid way to
00328  * check for I2C device presence.
00329  */
00330 static int i2c_bit_write ( struct i2c_interface *i2c,
00331                            struct i2c_device *i2cdev, unsigned int offset,
00332                            const uint8_t *data, unsigned int len ) {
00333         struct i2c_bit_basher *i2cbit
00334                 = container_of ( i2c, struct i2c_bit_basher, i2c );
00335         struct bit_basher *basher = &i2cbit->basher;
00336         int rc = 0;
00337 
00338         DBGC ( basher, "I2CBIT %p writing to device %x: ",
00339                basher, i2cdev->dev_addr );
00340 
00341         for ( ; ; data++, offset++ ) {
00342 
00343                 /* Select device for writing */
00344                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00345                                          I2C_WRITE ) ) != 0 )
00346                         break;
00347                 
00348                 /* Abort at end of data */
00349                 if ( ! ( len-- ) )
00350                         break;
00351 
00352                 /* Select offset */
00353                 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
00354                         break;
00355                 
00356                 /* Write data to device */
00357                 DBGC ( basher, "%02x ", *data );
00358                 if ( ( rc = i2c_send_byte ( basher, *data ) ) != 0 )
00359                         break;
00360         }
00361         
00362         DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
00363         i2c_stop ( basher );
00364         return rc;
00365 }
00366 
00367 /**
00368  * Initialise I2C bit-bashing interface
00369  *
00370  * @v i2cbit            I2C bit-bashing interface
00371  * @v bash_op           Bit-basher operations
00372  */
00373 int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
00374                           struct bit_basher_operations *bash_op ) {
00375         struct bit_basher *basher = &i2cbit->basher;
00376         int rc;
00377 
00378         /* Initialise data structures */
00379         basher->op = bash_op;
00380         assert ( basher->op->read != NULL );
00381         assert ( basher->op->write != NULL );
00382         i2cbit->i2c.read = i2c_bit_read;
00383         i2cbit->i2c.write = i2c_bit_write;
00384 
00385         /* Reset I2C bus */
00386         if ( ( rc = i2c_reset ( basher ) ) != 0 ) {
00387                 DBGC ( basher, "I2CBIT %p could not reset I2C bus: %s\n",
00388                        basher, strerror ( rc ) );
00389                 return rc;
00390         }
00391 
00392         return 0;
00393 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1