123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
-
-
- #include <stddef.h>
- #include <stdint.h>
- #include <errno.h>
- #include <assert.h>
- #include <gpxe/bitbash.h>
- #include <gpxe/i2c.h>
-
-
-
-
- static inline __attribute__ (( always_inline )) void
- setscl ( struct bit_basher *basher, int state ) {
- write_bit ( basher, I2C_BIT_SCL, state );
- }
-
-
- static inline __attribute__ (( always_inline )) void
- setsda ( struct bit_basher *basher, int state ) {
- write_bit ( basher, I2C_BIT_SDA, state );
- }
-
-
- static inline __attribute__ (( always_inline )) int
- getsda ( struct bit_basher *basher ) {
- return read_bit ( basher, I2C_BIT_SDA );
- }
-
-
- static void i2c_start ( struct bit_basher *basher ) {
- setscl ( basher, 1 );
- setsda ( basher, 0 );
- setscl ( basher, 0 );
- setsda ( basher, 1 );
- }
-
-
- static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
- setsda ( basher, bit );
- setscl ( basher, 1 );
- setscl ( basher, 0 );
- setsda ( basher, 1 );
- }
-
-
- static int i2c_recv_bit ( struct bit_basher *basher ) {
- int bit;
-
- setscl ( basher, 1 );
- bit = getsda ( basher );
- setscl ( basher, 0 );
- return bit;
- }
-
-
- static void i2c_stop ( struct bit_basher *basher ) {
- setsda ( basher, 0 );
- setscl ( basher, 1 );
- setsda ( basher, 1 );
- }
-
-
- static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
- int i;
-
-
- for ( i = 8 ; i ; i-- ) {
- i2c_send_bit ( basher, byte & 0x80 );
- byte <<= 1;
- }
-
-
- return ( i2c_recv_bit ( basher ) == 0 ? 0 : -EIO );
- }
-
-
- static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
- uint8_t value = 0;
- int i;
-
-
- for ( i = 8 ; i ; i-- ) {
- value <<= 1;
- value |= i2c_recv_bit ( basher );
- }
-
-
- i2c_send_bit ( basher, 1 );
-
- return value;
- }
-
-
- static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
- unsigned int direction ) {
- unsigned int address;
- int rc;
-
- i2c_start ( basher );
-
-
- address = i2cdev->address;
- if ( i2cdev->tenbit ) {
- address |= I2C_TENBIT_ADDRESS;
- address >>= 8;
- }
- if ( ( rc = i2c_send_byte ( basher,
- ( ( address << 1 ) | direction ) ) ) != 0 )
- return rc;
-
-
- if ( i2cdev->tenbit ) {
- if ( ( rc = i2c_send_byte ( basher,
- ( i2cdev->address & 0xff ) ) ) !=0)
- return rc;
- }
-
- return 0;
- }
-
-
- static int i2c_bit_read ( struct i2c_interface *i2c,
- struct i2c_device *i2cdev, unsigned int offset,
- uint8_t *data, unsigned int len ) {
- struct i2c_bit_basher *i2cbit
- = container_of ( i2c, struct i2c_bit_basher, i2c );
- struct bit_basher *basher = &i2cbit->basher;
- int rc = 0;
-
- DBG ( "Reading from I2C device %x: ", i2cdev->address );
-
- while ( 1 ) {
-
-
- if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
- break;
-
-
- if ( ! ( len-- ) )
- break;
-
-
- if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
- break;
-
-
- if ( ( rc = i2c_select ( basher, i2cdev, I2C_READ ) ) != 0 )
- break;
-
-
- *data++ = i2c_recv_byte ( basher );
- DBG ( "%02x ", *(data - 1) );
- }
-
- DBG ( "%s\n", ( rc ? "failed" : "" ) );
- i2c_stop ( basher );
- return rc;
- }
-
-
- static int i2c_bit_write ( struct i2c_interface *i2c,
- struct i2c_device *i2cdev, unsigned int offset,
- const uint8_t *data, unsigned int len ) {
- struct i2c_bit_basher *i2cbit
- = container_of ( i2c, struct i2c_bit_basher, i2c );
- struct bit_basher *basher = &i2cbit->basher;
- int rc = 0;
-
- DBG ( "Writing to I2C device %x: ", i2cdev->address );
-
- while ( 1 ) {
-
-
- if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
- break;
-
-
- if ( ! ( len-- ) )
- break;
-
-
- if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
- break;
-
-
- DBG ( "%02x ", *data );
- if ( ( rc = i2c_send_byte ( basher, *data++ ) ) != 0 )
- break;
- }
-
- DBG ( "%s\n", ( rc ? "failed" : "" ) );
- i2c_stop ( basher );
- return rc;
- }
-
-
- void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
- struct bit_basher *basher = &i2cbit->basher;
-
- assert ( basher->read != NULL );
- assert ( basher->write != NULL );
- i2cbit->i2c.read = i2c_bit_read;
- i2cbit->i2c.write = i2c_bit_write;
- basher->udelay = I2C_UDELAY;
- i2c_stop ( basher );
- }
|