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