00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 static void i2c_delay ( void ) {
00045 udelay ( I2C_UDELAY );
00046 }
00047
00048
00049
00050
00051
00052
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
00062
00063
00064
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
00074
00075
00076
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
00087
00088
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
00099
00100
00101
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
00112
00113
00114
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
00127
00128
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
00138
00139
00140
00141
00142
00143
00144
00145
00146 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
00147 int i;
00148 int ack;
00149
00150
00151 DBG2 ( "[send %02x]", byte );
00152 for ( i = 8 ; i ; i-- ) {
00153 i2c_send_bit ( basher, byte & 0x80 );
00154 byte <<= 1;
00155 }
00156
00157
00158 ack = ( i2c_recv_bit ( basher ) == 0 );
00159 DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
00160
00161 return ( ack ? 0 : -EIO );
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
00173 uint8_t byte = 0;
00174 int i;
00175
00176
00177 for ( i = 8 ; i ; i-- ) {
00178 byte <<= 1;
00179 byte |= ( i2c_recv_bit ( basher ) & 0x1 );
00180 }
00181
00182
00183 i2c_send_bit ( basher, 1 );
00184
00185 DBG2 ( "[rcvd %02x]", byte );
00186 return byte;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
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
00208 address = ( ( ( i2cdev->dev_addr |
00209 ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
00210 | direction );
00211
00212
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
00225
00226
00227
00228
00229
00230
00231
00232
00233 static int i2c_reset ( struct bit_basher *basher ) {
00234 unsigned int i;
00235 int sda;
00236
00237
00238
00239
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
00248 i2c_start ( basher );
00249
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
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
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
00290 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00291 I2C_WRITE ) ) != 0 )
00292 break;
00293
00294
00295 if ( ! ( len-- ) )
00296 break;
00297
00298
00299 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
00300 break;
00301
00302
00303 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00304 I2C_READ ) ) != 0 )
00305 break;
00306
00307
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
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
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
00344 if ( ( rc = i2c_select ( basher, i2cdev, offset,
00345 I2C_WRITE ) ) != 0 )
00346 break;
00347
00348
00349 if ( ! ( len-- ) )
00350 break;
00351
00352
00353 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
00354 break;
00355
00356
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
00369
00370
00371
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
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
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 }