i2c_bit.c File Reference

I2C bit-bashing interface. More...

#include <stddef.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <gpxe/bitbash.h>
#include <gpxe/i2c.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static void i2c_delay (void)
 Delay between output state changes.
static void setscl (struct bit_basher *basher, int state)
 Set state of I2C SCL line.
static void setsda (struct bit_basher *basher, int state)
 Set state of I2C SDA line.
static int getsda (struct bit_basher *basher)
 Get state of I2C SDA line.
static void i2c_start (struct bit_basher *basher)
 Send an I2C start condition.
static void i2c_send_bit (struct bit_basher *basher, int bit)
 Send an I2C data bit.
static int i2c_recv_bit (struct bit_basher *basher)
 Receive an I2C data bit.
static void i2c_stop (struct bit_basher *basher)
 Send an I2C stop condition.
static int i2c_send_byte (struct bit_basher *basher, uint8_t byte)
 Send byte via I2C bus and check for acknowledgement.
static uint8_t i2c_recv_byte (struct bit_basher *basher)
 Receive byte via I2C bus.
static int i2c_select (struct bit_basher *basher, struct i2c_device *i2cdev, unsigned int offset, unsigned int direction)
 Select I2C device for reading or writing.
static int i2c_reset (struct bit_basher *basher)
 Reset I2C bus.
static int i2c_bit_read (struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, uint8_t *data, unsigned int len)
 Read data from I2C device via bit-bashing interface.
static int i2c_bit_write (struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, const uint8_t *data, unsigned int len)
 Write data to I2C device via bit-bashing interface.
int init_i2c_bit_basher (struct i2c_bit_basher *i2cbit, struct bit_basher_operations *bash_op)
 Initialise I2C bit-bashing interface.


Detailed Description

I2C bit-bashing interface.

This implements a simple I2C master via a bit-bashing interface that provides two lines: SCL (clock) and SDA (data).

Definition in file i2c_bit.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static void i2c_delay ( void   )  [static]

Delay between output state changes.

Max rated i2c speed (for the basic i2c protocol) is 100kbps, i.e. 200k clock transitions per second.

Definition at line 44 of file i2c_bit.c.

References I2C_UDELAY, and udelay().

Referenced by setscl(), and setsda().

00044                                {
00045         udelay ( I2C_UDELAY );
00046 }

static void setscl ( struct bit_basher basher,
int  state 
) [static]

Set state of I2C SCL line.

Parameters:
basher Bit-bashing interface
state New state of SCL

Definition at line 54 of file i2c_bit.c.

References DBG2, I2C_BIT_SCL, i2c_delay(), and write_bit().

Referenced by i2c_recv_bit(), i2c_reset(), i2c_send_bit(), i2c_start(), and i2c_stop().

00054                                                             {
00055         DBG2 ( "%c", ( state ? '/' : '\\' ) );
00056         write_bit ( basher, I2C_BIT_SCL, state );
00057         i2c_delay();
00058 }

static void setsda ( struct bit_basher basher,
int  state 
) [static]

Set state of I2C SDA line.

Parameters:
basher Bit-bashing interface
state New state of SDA

Definition at line 66 of file i2c_bit.c.

References DBG2, I2C_BIT_SDA, i2c_delay(), and write_bit().

Referenced by i2c_reset(), i2c_send_bit(), i2c_start(), and i2c_stop().

00066                                                             {
00067         DBG2 ( "%c", ( state ? '1' : '0' ) );
00068         write_bit ( basher, I2C_BIT_SDA, state );
00069         i2c_delay();
00070 }

static int getsda ( struct bit_basher basher  )  [static]

Get state of I2C SDA line.

Parameters:
basher Bit-bashing interface
Return values:
state State of SDA

Definition at line 78 of file i2c_bit.c.

References DBG2, I2C_BIT_SDA, and read_bit().

Referenced by i2c_recv_bit(), and i2c_reset().

00078                                                 {
00079         int state;
00080         state = read_bit ( basher, I2C_BIT_SDA );
00081         DBG2 ( "%c", ( state ? '+' : '-' ) );
00082         return state;
00083 }

static void i2c_start ( struct bit_basher basher  )  [static]

Send an I2C start condition.

Parameters:
basher Bit-bashing interface

Definition at line 90 of file i2c_bit.c.

References setscl(), and setsda().

Referenced by i2c_reset(), and i2c_select().

00090                                                     {
00091         setscl ( basher, 1 );
00092         setsda ( basher, 0 );
00093         setscl ( basher, 0 );
00094         setsda ( basher, 1 );
00095 }

static void i2c_send_bit ( struct bit_basher basher,
int  bit 
) [static]

Send an I2C data bit.

Parameters:
basher Bit-bashing interface
bit Bit to send

Definition at line 103 of file i2c_bit.c.

References setscl(), and setsda().

Referenced by i2c_recv_byte(), and i2c_send_byte().

00103                                                                 {
00104         setsda ( basher, bit );
00105         setscl ( basher, 1 );
00106         setscl ( basher, 0 );
00107         setsda ( basher, 1 );
00108 }

static int i2c_recv_bit ( struct bit_basher basher  )  [static]

Receive an I2C data bit.

Parameters:
basher Bit-bashing interface
Return values:
bit Received bit

Definition at line 116 of file i2c_bit.c.

References getsda(), and setscl().

Referenced by i2c_recv_byte(), and i2c_send_byte().

00116                                                       {
00117         int bit;
00118 
00119         setscl ( basher, 1 );
00120         bit = getsda ( basher );
00121         setscl ( basher, 0 );
00122         return bit;
00123 }

static void i2c_stop ( struct bit_basher basher  )  [static]

Send an I2C stop condition.

Parameters:
basher Bit-bashing interface

Definition at line 130 of file i2c_bit.c.

References setscl(), and setsda().

Referenced by i2c_bit_read(), i2c_bit_write(), and i2c_reset().

00130                                                    {
00131         setsda ( basher, 0 );
00132         setscl ( basher, 1 );
00133         setsda ( basher, 1 );
00134 }

static int i2c_send_byte ( struct bit_basher basher,
uint8_t  byte 
) [static]

Send byte via I2C bus and check for acknowledgement.

Parameters:
basher Bit-bashing interface
byte Byte to send
Return values:
rc Return status code
Sends a byte via the I2C bus and checks for an acknowledgement from the slave device.

Definition at line 146 of file i2c_bit.c.

References DBG2, EIO, i2c_recv_bit(), and i2c_send_bit().

Referenced by i2c_bit_read(), i2c_bit_write(), and i2c_select().

00146                                                                      {
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 }

static uint8_t i2c_recv_byte ( struct bit_basher basher  )  [static]

Receive byte via I2C bus.

Parameters:
basher Bit-bashing interface
Return values:
byte Received byte
Receives a byte via the I2C bus and sends NACK to the slave device.

Definition at line 172 of file i2c_bit.c.

References DBG2, i2c_recv_bit(), and i2c_send_bit().

Referenced by i2c_bit_read().

00172                                                            {
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 }

static int i2c_select ( struct bit_basher basher,
struct i2c_device i2cdev,
unsigned int  offset,
unsigned int  direction 
) [static]

Select I2C device for reading or writing.

Parameters:
basher Bit-bashing interface
i2cdev I2C device
offset Starting offset within the device
direction I2C_READ or I2C_WRITE
Return values:
rc Return status code

Definition at line 198 of file i2c_bit.c.

References i2c_device::dev_addr, i2c_device::dev_addr_len, i2c_send_byte(), i2c_start(), and i2c_device::word_addr_len.

Referenced by i2c_bit_read(), and i2c_bit_write().

00199                                                                       {
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 }

static int i2c_reset ( struct bit_basher basher  )  [static]

Reset I2C bus.

Parameters:
basher Bit-bashing interface
Return values:
rc Return status code
i2c devices often don't have a reset line, so even a reboot or system power cycle is sometimes not enough to bring them back to a known state.

Definition at line 233 of file i2c_bit.c.

References DBGC, ETIMEDOUT, getsda(), I2C_RESET_MAX_CYCLES, i2c_start(), i2c_stop(), setscl(), and setsda().

Referenced by init_i2c_bit_basher().

00233                                                    {
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 }

static int i2c_bit_read ( struct i2c_interface i2c,
struct i2c_device i2cdev,
unsigned int  offset,
uint8_t data,
unsigned int  len 
) [static]

Read data from I2C device via bit-bashing interface.

Parameters:
i2c I2C interface
i2cdev I2C device
offset Starting offset within the device
data Data buffer
len Length of data buffer
Return values:
rc Return status code
Note that attempting to read zero bytes of data is a valid way to check for I2C device presence.

Definition at line 276 of file i2c_bit.c.

References i2c_bit_basher::basher, container_of, DBGC, i2c_device::dev_addr, I2C_READ, i2c_recv_byte(), i2c_select(), i2c_send_byte(), i2c_stop(), and I2C_WRITE.

Referenced by init_i2c_bit_basher().

00278                                                             {
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 }

static int i2c_bit_write ( struct i2c_interface i2c,
struct i2c_device i2cdev,
unsigned int  offset,
const uint8_t data,
unsigned int  len 
) [static]

Write data to I2C device via bit-bashing interface.

Parameters:
i2c I2C interface
i2cdev I2C device
offset Starting offset within the device
data Data buffer
len Length of data buffer
Return values:
rc Return status code
Note that attempting to write zero bytes of data is a valid way to check for I2C device presence.

Definition at line 330 of file i2c_bit.c.

References i2c_bit_basher::basher, container_of, DBGC, i2c_device::dev_addr, i2c_select(), i2c_send_byte(), i2c_stop(), and I2C_WRITE.

Referenced by init_i2c_bit_basher().

00332                                                                    {
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 }

int init_i2c_bit_basher ( struct i2c_bit_basher i2cbit,
struct bit_basher_operations bash_op 
)

Initialise I2C bit-bashing interface.

Parameters:
i2cbit I2C bit-bashing interface
bash_op Bit-basher operations

Definition at line 373 of file i2c_bit.c.

References assert, i2c_bit_basher::basher, DBGC, i2c_bit_basher::i2c, i2c_bit_read(), i2c_bit_write(), i2c_reset(), NULL, bit_basher::op, i2c_interface::read, bit_basher_operations::read, strerror(), i2c_interface::write, and bit_basher_operations::write.

Referenced by falcon_probe_spi(), and linda_init_i2c().

00374                                                                   {
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:01:16 2010 for gPXE by  doxygen 1.5.7.1