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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
32 #include <ipxe/bitbash.h>
37 * I2C bit-bashing interface
39 * This implements a simple I2C master via a bit-bashing interface
40 * that provides two lines: SCL (clock) and SDA (data).
44 * Delay between output state changes
46 * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
47 * i.e. 200k clock transitions per second.
49 static void i2c_delay ( void ) {
50 udelay ( I2C_UDELAY
);
54 * Set state of I2C SCL line
56 * @v basher Bit-bashing interface
57 * @v state New state of SCL
59 static void setscl ( struct bit_basher
*basher
, int state
) {
60 DBG2 ( "%c", ( state ?
'/' : '\\' ) );
61 write_bit ( basher
, I2C_BIT_SCL
, state
);
66 * Set state of I2C SDA line
68 * @v basher Bit-bashing interface
69 * @v state New state of SDA
71 static void setsda ( struct bit_basher
*basher
, int state
) {
72 DBG2 ( "%c", ( state ?
'1' : '0' ) );
73 write_bit ( basher
, I2C_BIT_SDA
, state
);
78 * Get state of I2C SDA line
80 * @v basher Bit-bashing interface
81 * @ret state State of SDA
83 static int getsda ( struct bit_basher
*basher
) {
85 state
= read_bit ( basher
, I2C_BIT_SDA
);
86 DBG2 ( "%c", ( state ?
'+' : '-' ) );
91 * Send an I2C start condition
93 * @v basher Bit-bashing interface
95 static void i2c_start ( struct bit_basher
*basher
) {
103 * Send an I2C data bit
105 * @v basher Bit-bashing interface
108 static void i2c_send_bit ( struct bit_basher
*basher
, int bit
) {
109 setsda ( basher
, bit
);
110 setscl ( basher
, 1 );
111 setscl ( basher
, 0 );
112 setsda ( basher
, 1 );
116 * Receive an I2C data bit
118 * @v basher Bit-bashing interface
119 * @ret bit Received bit
121 static int i2c_recv_bit ( struct bit_basher
*basher
) {
124 setscl ( basher
, 1 );
125 bit
= getsda ( basher
);
126 setscl ( basher
, 0 );
131 * Send an I2C stop condition
133 * @v basher Bit-bashing interface
135 static void i2c_stop ( struct bit_basher
*basher
) {
136 setsda ( basher
, 0 );
137 setscl ( basher
, 1 );
138 setsda ( basher
, 1 );
142 * Send byte via I2C bus and check for acknowledgement
144 * @v basher Bit-bashing interface
145 * @v byte Byte to send
146 * @ret rc Return status code
148 * Sends a byte via the I2C bus and checks for an acknowledgement from
151 static int i2c_send_byte ( struct bit_basher
*basher
, uint8_t byte
) {
156 DBG2 ( "[send %02x]", byte
);
157 for ( i
= 8 ; i
; i
-- ) {
158 i2c_send_bit ( basher
, byte
& 0x80 );
162 /* Check for acknowledgement from slave */
163 ack
= ( i2c_recv_bit ( basher
) == 0 );
164 DBG2 ( "%s", ( ack ?
"[acked]" : "[not acked]" ) );
166 return ( ack ?
0 : -EIO
);
170 * Receive byte via I2C bus
172 * @v basher Bit-bashing interface
173 * @ret byte Received byte
175 * Receives a byte via the I2C bus and sends NACK to the slave device.
177 static uint8_t i2c_recv_byte ( struct bit_basher
*basher
) {
182 for ( i
= 8 ; i
; i
-- ) {
184 byte
|= ( i2c_recv_bit ( basher
) & 0x1 );
188 i2c_send_bit ( basher
, 1 );
190 DBG2 ( "[rcvd %02x]", byte
);
195 * Select I2C device for reading or writing
197 * @v basher Bit-bashing interface
198 * @v i2cdev I2C device
199 * @v offset Starting offset within the device
200 * @v direction I2C_READ or I2C_WRITE
201 * @ret rc Return status code
203 static int i2c_select ( struct bit_basher
*basher
, struct i2c_device
*i2cdev
,
204 unsigned int offset
, unsigned int direction
) {
205 unsigned int address
;
210 i2c_start ( basher
);
212 /* Calculate address to appear on bus */
213 address
= ( ( ( i2cdev
->dev_addr
|
214 ( offset
>> ( 8 * i2cdev
->word_addr_len
) ) ) << 1 )
217 /* Send address a byte at a time */
218 for ( shift
= ( 8 * ( i2cdev
->dev_addr_len
- 1 ) ) ;
219 shift
>= 0 ; shift
-= 8 ) {
220 byte
= ( ( address
>> shift
) & 0xff );
221 if ( ( rc
= i2c_send_byte ( basher
, byte
) ) != 0 )
231 * @v basher Bit-bashing interface
232 * @ret rc Return status code
234 * i2c devices often don't have a reset line, so even a reboot or
235 * system power cycle is sometimes not enough to bring them back to a
238 static int i2c_reset ( struct bit_basher
*basher
) {
242 /* Clock through several cycles, waiting for an opportunity to
243 * pull SDA low while SCL is high (which creates a start
247 setscl ( basher
, 0 );
248 setsda ( basher
, 1 );
249 for ( i
= 0 ; i
< I2C_RESET_MAX_CYCLES
; i
++ ) {
250 setscl ( basher
, 1 );
251 sda
= getsda ( basher
);
253 /* Now that the device will see a start, issue it */
254 i2c_start ( basher
);
255 /* Stop the bus to leave it in a known good state */
257 DBGC ( basher
, "I2CBIT %p reset after %d attempts\n",
259 close_bit ( basher
);
262 setscl ( basher
, 0 );
265 DBGC ( basher
, "I2CBIT %p could not reset after %d attempts\n",
267 close_bit ( basher
);
272 * Read data from I2C device via bit-bashing interface
274 * @v i2c I2C interface
275 * @v i2cdev I2C device
276 * @v offset Starting offset within the device
277 * @v data Data buffer
278 * @v len Length of data buffer
279 * @ret rc Return status code
281 * Note that attempting to read zero bytes of data is a valid way to
282 * check for I2C device presence.
284 static int i2c_bit_read ( struct i2c_interface
*i2c
,
285 struct i2c_device
*i2cdev
, unsigned int offset
,
286 uint8_t *data
, unsigned int len
) {
287 struct i2c_bit_basher
*i2cbit
288 = container_of ( i2c
, struct i2c_bit_basher
, i2c
);
289 struct bit_basher
*basher
= &i2cbit
->basher
;
292 DBGC ( basher
, "I2CBIT %p reading from device %x: ",
293 basher
, i2cdev
->dev_addr
);
297 for ( ; ; data
++, offset
++ ) {
299 /* Select device for writing */
300 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
304 /* Abort at end of data */
309 if ( ( rc
= i2c_send_byte ( basher
, offset
) ) != 0 )
312 /* Select device for reading */
313 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
318 *data
= i2c_recv_byte ( basher
);
319 DBGC ( basher
, "%02x ", *data
);
322 DBGC ( basher
, "%s\n", ( rc ?
"failed" : "" ) );
324 close_bit ( basher
);
329 * Write data to I2C device via bit-bashing interface
331 * @v i2c I2C interface
332 * @v i2cdev I2C device
333 * @v offset Starting offset within the device
334 * @v data Data buffer
335 * @v len Length of data buffer
336 * @ret rc Return status code
338 * Note that attempting to write zero bytes of data is a valid way to
339 * check for I2C device presence.
341 static int i2c_bit_write ( struct i2c_interface
*i2c
,
342 struct i2c_device
*i2cdev
, unsigned int offset
,
343 const uint8_t *data
, unsigned int len
) {
344 struct i2c_bit_basher
*i2cbit
345 = container_of ( i2c
, struct i2c_bit_basher
, i2c
);
346 struct bit_basher
*basher
= &i2cbit
->basher
;
349 DBGC ( basher
, "I2CBIT %p writing to device %x: ",
350 basher
, i2cdev
->dev_addr
);
354 for ( ; ; data
++, offset
++ ) {
356 /* Select device for writing */
357 if ( ( rc
= i2c_select ( basher
, i2cdev
, offset
,
361 /* Abort at end of data */
366 if ( ( rc
= i2c_send_byte ( basher
, offset
) ) != 0 )
369 /* Write data to device */
370 DBGC ( basher
, "%02x ", *data
);
371 if ( ( rc
= i2c_send_byte ( basher
, *data
) ) != 0 )
375 DBGC ( basher
, "%s\n", ( rc ?
"failed" : "" ) );
377 close_bit ( basher
);
382 * Initialise I2C bit-bashing interface
384 * @v i2cbit I2C bit-bashing interface
385 * @v bash_op Bit-basher operations
387 int init_i2c_bit_basher ( struct i2c_bit_basher
*i2cbit
,
388 struct bit_basher_operations
*bash_op
) {
389 struct bit_basher
*basher
= &i2cbit
->basher
;
392 /* Initialise data structures */
393 basher
->op
= bash_op
;
394 assert ( basher
->op
->read
!= NULL
);
395 assert ( basher
->op
->write
!= NULL
);
396 i2cbit
->i2c
.read
= i2c_bit_read
;
397 i2cbit
->i2c
.write
= i2c_bit_write
;
400 if ( ( rc
= i2c_reset ( basher
) ) != 0 ) {
401 DBGC ( basher
, "I2CBIT %p could not reset I2C bus: %s\n",
402 basher
, strerror ( rc
) );