2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
27 #include <gpxe/bitbash.h>
32 * I2C bit-bashing interface
34 * This implements a simple I2C master via a bit-bashing interface
35 * that provides two lines: SCL (clock) and SDA (data).
39 * Delay between output state changes
41 * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
42 * i.e. 200k clock transitions per second.
44 static void i2c_delay ( void ) {
45 udelay ( I2C_UDELAY
);
49 * Set state of I2C SCL line
51 * @v basher Bit-bashing interface
52 * @v state New state of SCL
54 static void setscl ( struct bit_basher
*basher
, int state
) {
55 DBG2 ( "%c", ( state
? '/' : '\\' ) );
56 write_bit ( basher
, I2C_BIT_SCL
, state
);
61 * Set state of I2C SDA line
63 * @v basher Bit-bashing interface
64 * @v state New state of SDA
66 static void setsda ( struct bit_basher
*basher
, int state
) {
67 DBG2 ( "%c", ( state
? '1' : '0' ) );
68 write_bit ( basher
, I2C_BIT_SDA
, state
);
73 * Get state of I2C SDA line
75 * @v basher Bit-bashing interface
76 * @ret state State of SDA
78 static int getsda ( struct bit_basher
*basher
) {
80 state
= read_bit ( basher
, I2C_BIT_SDA
);
81 DBG2 ( "%c", ( state
? '+' : '-' ) );
86 * Send an I2C start condition
88 * @v basher Bit-bashing interface
90 static void i2c_start ( struct bit_basher
*basher
) {
98 * Send an I2C data bit
100 * @v basher Bit-bashing interface
103 static void i2c_send_bit ( struct bit_basher
*basher
, int bit
) {
104 setsda ( basher
, bit
);
105 setscl ( basher
, 1 );
106 setscl ( basher
, 0 );
107 setsda ( basher
, 1 );
111 * Receive an I2C data bit
113 * @v basher Bit-bashing interface
114 * @ret bit Received bit
116 static int i2c_recv_bit ( struct bit_basher
*basher
) {
119 setscl ( basher
, 1 );
120 bit
= getsda ( basher
);
121 setscl ( basher
, 0 );
126 * Send an I2C stop condition
128 * @v basher Bit-bashing interface
130 static void i2c_stop ( struct bit_basher
*basher
) {
131 setsda ( basher
, 0 );
132 setscl ( basher
, 1 );
133 setsda ( basher
, 1 );
137 * Send byte via I2C bus and check for acknowledgement
139 * @v basher Bit-bashing interface
140 * @v byte Byte to send
141 * @ret rc Return status code
143 * Sends a byte via the I2C bus and checks for an acknowledgement from
146 static int i2c_send_byte ( struct bit_basher
*basher
, uint8_t byte
) {
151 DBG2 ( "[send %02x]", byte
);
152 for ( i
= 8 ; i
; i
-- ) {
153 i2c_send_bit ( basher
, byte
& 0x80 );
157 /* Check for acknowledgement from slave */
158 ack
= ( i2c_recv_bit ( basher
) == 0 );
159 DBG2 ( "%s", ( ack
? "[acked]" : "[not acked]" ) );
161 return ( ack
? 0 : -EIO
);
165 * Receive byte via I2C bus
167 * @v basher Bit-bashing interface
168 * @ret byte Received byte
170 * Receives a byte via the I2C bus and sends NACK to the slave device.
172 static uint8_t i2c_recv_byte ( struct bit_basher
*basher
) {
177 for ( i
= 8 ; i
; i
-- ) {
179 byte
|= ( i2c_recv_bit ( basher
) & 0x1 );
183 i2c_send_bit ( basher
, 1 );
185 DBG2 ( "[rcvd %02x]", byte
);
190 * Select I2C device for reading or writing
192 * @v basher Bit-bashing interface
193 * @v i2cdev I2C device
194 * @v offset Starting offset within the device
195 * @v direction I2C_READ or I2C_WRITE
196 * @ret rc Return status code
198 static int i2c_select ( struct bit_basher
*basher
, struct i2c_device
*i2cdev
,
199 unsigned int offset
, unsigned int direction
) {
200 unsigned int address
;
205 i2c_start ( basher
);
207 /* Calculate address to appear on bus */
208 address
= ( ( ( i2cdev
->dev_addr
|
209 ( offset
>> ( 8 * i2cdev
->word_addr_len
) ) ) << 1 )
212 /* Send address a byte at a time */
213 for ( shift
= ( 8 * ( i2cdev
->dev_addr_len
- 1 ) ) ;
214 shift
>= 0 ; shift
-= 8 ) {
215 byte
= ( ( address
>> shift
) & 0xff );
216 if ( ( rc
= i2c_send_byte ( basher
, byte
) ) != 0 )
226 * @v basher Bit-bashing interface
227 * @ret rc Return status code
229 * i2c devices often don't have a reset line, so even a reboot or
230 * system power cycle is sometimes not enough to bring them back to a
233 static int i2c_reset ( struct bit_basher
*basher
) {
237 /* Clock through several cycles, waiting for an opportunity to
238 * pull SDA low while SCL is high (which creates a start
241 setscl ( basher
, 0 );
242 setsda ( basher
, 1 );
243 for ( i
= 0 ; i
< I2C_RESET_MAX_CYCLES
; i
++ ) {
244 setscl ( basher
, 1 );
245 sda
= getsda ( basher
);
247 /* Now that the device will see a start, issue it */
248 i2c_start ( basher
);
249 /* Stop the bus to leave it in a known good state */
251 DBGC ( basher
, "I2CBIT %p reset after %d attempts\n",
255 setscl ( basher
, 0 );
258 DBGC ( basher
, "I2CBIT %p could not reset after %d attempts\n",
264 * Read data from I2C device via bit-bashing interface
266 * @v i2c I2C interface
267 * @v i2cdev I2C device
268 * @v offset Starting offset within the device
269 * @v data Data buffer
270 * @v len Length of data buffer
271 * @ret rc Return status code
273 * Note that attempting to read zero bytes of data is a valid way to
274 * check for I2C device presence.
276 static int i2c_bit_read ( struct i2c_interface
*i2c
,
277 struct i2c_device
*i2cdev
, unsigned int offset
,
278 uint8_t *data
, unsigned int len
) {
279 struct i2c_bit_basher
*i2cbit
280 = container_of ( i2c
, struct i2c_bit_basher
, i2c
);
281 struct bit_basher
*basher
= &i2cbit
->basher
;
284 DBGC ( basher
, "I2CBIT %p reading from device %x: ",
285 basher
, i2cdev
->dev_addr
);
287 for ( ; ; data
++, offset
++ ) {
289 /* Select device for writing */
290 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
294 /* Abort at end of data */
299 if ( ( rc
= i2c_send_byte ( basher
, offset
) ) != 0 )
302 /* Select device for reading */
303 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
308 *data
= i2c_recv_byte ( basher
);
309 DBGC ( basher
, "%02x ", *data
);
312 DBGC ( basher
, "%s\n", ( rc
? "failed" : "" ) );
318 * Write data to I2C device via bit-bashing interface
320 * @v i2c I2C interface
321 * @v i2cdev I2C device
322 * @v offset Starting offset within the device
323 * @v data Data buffer
324 * @v len Length of data buffer
325 * @ret rc Return status code
327 * Note that attempting to write zero bytes of data is a valid way to
328 * check for I2C device presence.
330 static int i2c_bit_write ( struct i2c_interface
*i2c
,
331 struct i2c_device
*i2cdev
, unsigned int offset
,
332 const uint8_t *data
, unsigned int len
) {
333 struct i2c_bit_basher
*i2cbit
334 = container_of ( i2c
, struct i2c_bit_basher
, i2c
);
335 struct bit_basher
*basher
= &i2cbit
->basher
;
338 DBGC ( basher
, "I2CBIT %p writing to device %x: ",
339 basher
, i2cdev
->dev_addr
);
341 for ( ; ; data
++, offset
++ ) {
343 /* Select device for writing */
344 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
348 /* Abort at end of data */
353 if ( ( rc
= i2c_send_byte ( basher
, offset
) ) != 0 )
356 /* Write data to device */
357 DBGC ( basher
, "%02x ", *data
);
358 if ( ( rc
= i2c_send_byte ( basher
, *data
) ) != 0 )
362 DBGC ( basher
, "%s\n", ( rc
? "failed" : "" ) );
368 * Initialise I2C bit-bashing interface
370 * @v i2cbit I2C bit-bashing interface
371 * @v bash_op Bit-basher operations
373 int init_i2c_bit_basher ( struct i2c_bit_basher
*i2cbit
,
374 struct bit_basher_operations
*bash_op
) {
375 struct bit_basher
*basher
= &i2cbit
->basher
;
378 /* Initialise data structures */
379 basher
->op
= bash_op
;
380 assert ( basher
->op
->read
!= NULL
);
381 assert ( basher
->op
->write
!= NULL
);
382 i2cbit
->i2c
.read
= i2c_bit_read
;
383 i2cbit
->i2c
.write
= i2c_bit_write
;
386 if ( ( rc
= i2c_reset ( basher
) ) != 0 ) {
387 DBGC ( basher
, "I2CBIT %p could not reset I2C bus: %s\n",
388 basher
, strerror ( rc
) );