iPXE
i2c_bit.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <ipxe/bitbash.h>
33 #include <ipxe/i2c.h>
34 
35 /** @file
36  *
37  * I2C bit-bashing interface
38  *
39  * This implements a simple I2C master via a bit-bashing interface
40  * that provides two lines: SCL (clock) and SDA (data).
41  */
42 
43 /**
44  * Delay between output state changes
45  *
46  * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
47  * i.e. 200k clock transitions per second.
48  */
49 static void i2c_delay ( void ) {
50  udelay ( I2C_UDELAY );
51 }
52 
53 /**
54  * Set state of I2C SCL line
55  *
56  * @v basher Bit-bashing interface
57  * @v state New state of SCL
58  */
59 static void setscl ( struct bit_basher *basher, int state ) {
60  DBG2 ( "%c", ( state ? '/' : '\\' ) );
61  write_bit ( basher, I2C_BIT_SCL, state );
62  i2c_delay();
63 }
64 
65 /**
66  * Set state of I2C SDA line
67  *
68  * @v basher Bit-bashing interface
69  * @v state New state of SDA
70  */
71 static void setsda ( struct bit_basher *basher, int state ) {
72  DBG2 ( "%c", ( state ? '1' : '0' ) );
73  write_bit ( basher, I2C_BIT_SDA, state );
74  i2c_delay();
75 }
76 
77 /**
78  * Get state of I2C SDA line
79  *
80  * @v basher Bit-bashing interface
81  * @ret state State of SDA
82  */
83 static int getsda ( struct bit_basher *basher ) {
84  int state;
85  state = read_bit ( basher, I2C_BIT_SDA );
86  DBG2 ( "%c", ( state ? '+' : '-' ) );
87  return state;
88 }
89 
90 /**
91  * Send an I2C start condition
92  *
93  * @v basher Bit-bashing interface
94  */
95 static void i2c_start ( struct bit_basher *basher ) {
96  setscl ( basher, 1 );
97  setsda ( basher, 0 );
98  setscl ( basher, 0 );
99  setsda ( basher, 1 );
100 }
101 
102 /**
103  * Send an I2C data bit
104  *
105  * @v basher Bit-bashing interface
106  * @v bit Bit to send
107  */
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 );
113 }
114 
115 /**
116  * Receive an I2C data bit
117  *
118  * @v basher Bit-bashing interface
119  * @ret bit Received bit
120  */
121 static int i2c_recv_bit ( struct bit_basher *basher ) {
122  int bit;
123 
124  setscl ( basher, 1 );
125  bit = getsda ( basher );
126  setscl ( basher, 0 );
127  return bit;
128 }
129 
130 /**
131  * Send an I2C stop condition
132  *
133  * @v basher Bit-bashing interface
134  */
135 static void i2c_stop ( struct bit_basher *basher ) {
136  setsda ( basher, 0 );
137  setscl ( basher, 1 );
138  setsda ( basher, 1 );
139 }
140 
141 /**
142  * Send byte via I2C bus and check for acknowledgement
143  *
144  * @v basher Bit-bashing interface
145  * @v byte Byte to send
146  * @ret rc Return status code
147  *
148  * Sends a byte via the I2C bus and checks for an acknowledgement from
149  * the slave device.
150  */
151 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
152  int i;
153  int ack;
154 
155  /* Send byte */
156  DBG2 ( "[send %02x]", byte );
157  for ( i = 8 ; i ; i-- ) {
158  i2c_send_bit ( basher, byte & 0x80 );
159  byte <<= 1;
160  }
161 
162  /* Check for acknowledgement from slave */
163  ack = ( i2c_recv_bit ( basher ) == 0 );
164  DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
165 
166  return ( ack ? 0 : -EIO );
167 }
168 
169 /**
170  * Receive byte via I2C bus
171  *
172  * @v basher Bit-bashing interface
173  * @ret byte Received byte
174  *
175  * Receives a byte via the I2C bus and sends NACK to the slave device.
176  */
177 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
178  uint8_t byte = 0;
179  int i;
180 
181  /* Receive byte */
182  for ( i = 8 ; i ; i-- ) {
183  byte <<= 1;
184  byte |= ( i2c_recv_bit ( basher ) & 0x1 );
185  }
186 
187  /* Send NACK */
188  i2c_send_bit ( basher, 1 );
189 
190  DBG2 ( "[rcvd %02x]", byte );
191  return byte;
192 }
193 
194 /**
195  * Select I2C device for reading or writing
196  *
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
202  */
203 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
204  unsigned int offset, unsigned int direction ) {
205  unsigned int address;
206  int shift;
207  unsigned int byte;
208  int rc;
209 
210  i2c_start ( basher );
211 
212  /* Calculate address to appear on bus */
213  address = ( ( ( i2cdev->dev_addr |
214  ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
215  | direction );
216 
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 )
222  return rc;
223  }
224 
225  return 0;
226 }
227 
228 /**
229  * Reset I2C bus
230  *
231  * @v basher Bit-bashing interface
232  * @ret rc Return status code
233  *
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
236  * known state.
237  */
238 static int i2c_reset ( struct bit_basher *basher ) {
239  unsigned int i;
240  int sda;
241 
242  /* Clock through several cycles, waiting for an opportunity to
243  * pull SDA low while SCL is high (which creates a start
244  * condition).
245  */
246  open_bit ( basher );
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 );
252  if ( sda ) {
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 */
256  i2c_stop ( basher );
257  DBGC ( basher, "I2CBIT %p reset after %d attempts\n",
258  basher, ( i + 1 ) );
259  close_bit ( basher );
260  return 0;
261  }
262  setscl ( basher, 0 );
263  }
264 
265  DBGC ( basher, "I2CBIT %p could not reset after %d attempts\n",
266  basher, i );
267  close_bit ( basher );
268  return -ETIMEDOUT;
269 }
270 
271 /**
272  * Read data from I2C device via bit-bashing interface
273  *
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
280  *
281  * Note that attempting to read zero bytes of data is a valid way to
282  * check for I2C device presence.
283  */
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;
290  int rc = 0;
291 
292  DBGC ( basher, "I2CBIT %p reading from device %x: ",
293  basher, i2cdev->dev_addr );
294 
295  open_bit ( basher );
296 
297  for ( ; ; data++, offset++ ) {
298 
299  /* Select device for writing */
300  if ( ( rc = i2c_select ( basher, i2cdev, offset,
301  I2C_WRITE ) ) != 0 )
302  break;
303 
304  /* Abort at end of data */
305  if ( ! ( len-- ) )
306  break;
307 
308  /* Select offset */
309  if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
310  break;
311 
312  /* Select device for reading */
313  if ( ( rc = i2c_select ( basher, i2cdev, offset,
314  I2C_READ ) ) != 0 )
315  break;
316 
317  /* Read byte */
318  *data = i2c_recv_byte ( basher );
319  DBGC ( basher, "%02x ", *data );
320  }
321 
322  DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
323  i2c_stop ( basher );
324  close_bit ( basher );
325  return rc;
326 }
327 
328 /**
329  * Write data to I2C device via bit-bashing interface
330  *
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
337  *
338  * Note that attempting to write zero bytes of data is a valid way to
339  * check for I2C device presence.
340  */
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;
347  int rc = 0;
348 
349  DBGC ( basher, "I2CBIT %p writing to device %x: ",
350  basher, i2cdev->dev_addr );
351 
352  open_bit ( basher );
353 
354  for ( ; ; data++, offset++ ) {
355 
356  /* Select device for writing */
357  if ( ( rc = i2c_select ( basher, i2cdev, offset,
358  I2C_WRITE ) ) != 0 )
359  break;
360 
361  /* Abort at end of data */
362  if ( ! ( len-- ) )
363  break;
364 
365  /* Select offset */
366  if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
367  break;
368 
369  /* Write data to device */
370  DBGC ( basher, "%02x ", *data );
371  if ( ( rc = i2c_send_byte ( basher, *data ) ) != 0 )
372  break;
373  }
374 
375  DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
376  i2c_stop ( basher );
377  close_bit ( basher );
378  return rc;
379 }
380 
381 /**
382  * Initialise I2C bit-bashing interface
383  *
384  * @v i2cbit I2C bit-bashing interface
385  * @v bash_op Bit-basher operations
386  */
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;
390  int rc;
391 
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;
398 
399  /* Reset I2C bus */
400  if ( ( rc = i2c_reset ( basher ) ) != 0 ) {
401  DBGC ( basher, "I2CBIT %p could not reset I2C bus: %s\n",
402  basher, strerror ( rc ) );
403  return rc;
404  }
405 
406  return 0;
407 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void i2c_delay(void)
Delay between output state changes.
Definition: i2c_bit.c:49
static void open_bit(struct bit_basher *basher)
Open bit-bashing interface.
Definition: bitbash.h:65
int(* write)(struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, const uint8_t *data, unsigned int len)
Write data to I2C device.
Definition: i2c.h:81
uint8_t state
State.
Definition: eth_slow.h:47
static unsigned int unsigned int bit
Definition: bigint.h:208
static int i2c_reset(struct bit_basher *basher)
Reset I2C bus.
Definition: i2c_bit.c:238
Error codes.
Bit-bashing operations.
Definition: bitbash.h:15
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.
Definition: i2c_bit.c:203
uint64_t address
Base address.
Definition: ena.h:24
#define DBGC(...)
Definition: compiler.h:505
static void i2c_send_bit(struct bit_basher *basher, int bit)
Send an I2C data bit.
Definition: i2c_bit.c:108
A bit-bashing I2C interface.
Definition: i2c.h:91
uint8_t direction
Direction.
Definition: ena.h:14
An I2C interface.
Definition: i2c.h:57
unsigned int dev_addr
Address of this device.
Definition: i2c.h:31
Serial clock.
Definition: i2c.h:114
static int i2c_recv_bit(struct bit_basher *basher)
Receive an I2C data bit.
Definition: i2c_bit.c:121
struct bit_basher_operations * op
Bit-bashing operations.
Definition: bitbash.h:57
#define I2C_RESET_MAX_CYCLES
Maximum number of cycles to use when attempting a bus reset.
Definition: i2c.h:123
Serial data.
Definition: i2c.h:116
static int i2c_send_byte(struct bit_basher *basher, uint8_t byte)
Send byte via I2C bus and check for acknowledgement.
Definition: i2c_bit.c:151
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
struct i2c_interface i2c
I2C interface.
Definition: i2c.h:93
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.
Definition: i2c_bit.c:341
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
Bit-bashing interfaces.
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
static void setscl(struct bit_basher *basher, int state)
Set state of I2C SCL line.
Definition: i2c_bit.c:59
struct bit_basher basher
Bit-bashing interface.
Definition: i2c.h:95
int(* read)(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.h:51
A bit-bashing interface.
Definition: bitbash.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void i2c_stop(struct bit_basher *basher)
Send an I2C stop condition.
Definition: i2c_bit.c:135
unsigned char uint8_t
Definition: stdint.h:10
int(* read)(struct i2c_interface *i2c, struct i2c_device *i2cdev, unsigned int offset, uint8_t *data, unsigned int len)
Read data from I2C device.
Definition: i2c.h:68
unsigned char byte
Definition: smc9000.h:38
unsigned int word_addr_len
Word adddress length, in bytes.
Definition: i2c.h:49
int read_bit(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.c:60
#define I2C_WRITE
An I2C write command.
Definition: i2c.h:106
static uint8_t i2c_recv_byte(struct bit_basher *basher)
Receive byte via I2C bus.
Definition: i2c_bit.c:177
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void(* write)(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.h:41
uint32_t len
Length.
Definition: ena.h:14
#define I2C_READ
An I2C read command.
Definition: i2c.h:109
#define EIO
Input/output error.
Definition: errno.h:433
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define I2C_UDELAY
Delay required for bit-bashing operation.
Definition: i2c.h:120
static void i2c_start(struct bit_basher *basher)
Send an I2C start condition.
Definition: i2c_bit.c:95
static void close_bit(struct bit_basher *basher)
Close bit-bashing interface.
Definition: bitbash.h:75
static int getsda(struct bit_basher *basher)
Get state of I2C SDA line.
Definition: i2c_bit.c:83
unsigned int dev_addr_len
Device address length, in bytes.
Definition: i2c.h:38
int init_i2c_bit_basher(struct i2c_bit_basher *i2cbit, struct bit_basher_operations *bash_op)
Initialise I2C bit-bashing interface.
Definition: i2c_bit.c:387
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.
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.
Definition: i2c_bit.c:284
An I2C device.
Definition: i2c.h:20
static void setsda(struct bit_basher *basher, int state)
Set state of I2C SDA line.
Definition: i2c_bit.c:71
#define DBG2(...)
Definition: compiler.h:515
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.c:44
I2C interface.