Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / bitbash / i2c_bit.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <ipxe/bitbash.h>
29 #include <ipxe/i2c.h>
30
31 /** @file
32  *
33  * I2C bit-bashing interface
34  *
35  * This implements a simple I2C master via a bit-bashing interface
36  * that provides two lines: SCL (clock) and SDA (data).
37  */
38
39 /**
40  * Delay between output state changes
41  *
42  * Max rated i2c speed (for the basic i2c protocol) is 100kbps,
43  * i.e. 200k clock transitions per second.
44  */
45 static void i2c_delay ( void ) {
46         udelay ( I2C_UDELAY );
47 }
48
49 /**
50  * Set state of I2C SCL line
51  *
52  * @v basher            Bit-bashing interface
53  * @v state             New state of SCL
54  */
55 static void setscl ( struct bit_basher *basher, int state ) {
56         DBG2 ( "%c", ( state ? '/' : '\\' ) );
57         write_bit ( basher, I2C_BIT_SCL, state );
58         i2c_delay();
59 }
60
61 /**
62  * Set state of I2C SDA line
63  *
64  * @v basher            Bit-bashing interface
65  * @v state             New state of SDA
66  */
67 static void setsda ( struct bit_basher *basher, int state ) {
68         DBG2 ( "%c", ( state ? '1' : '0' ) );
69         write_bit ( basher, I2C_BIT_SDA, state );
70         i2c_delay();
71 }
72
73 /**
74  * Get state of I2C SDA line
75  *
76  * @v basher            Bit-bashing interface
77  * @ret state           State of SDA
78  */
79 static int getsda ( struct bit_basher *basher ) {
80         int state;
81         state = read_bit ( basher, I2C_BIT_SDA );
82         DBG2 ( "%c", ( state ? '+' : '-' ) );
83         return state;
84 }
85
86 /**
87  * Send an I2C start condition
88  *
89  * @v basher            Bit-bashing interface
90  */
91 static void i2c_start ( struct bit_basher *basher ) {
92         setscl ( basher, 1 );
93         setsda ( basher, 0 );
94         setscl ( basher, 0 );
95         setsda ( basher, 1 );
96 }
97
98 /**
99  * Send an I2C data bit
100  *
101  * @v basher            Bit-bashing interface
102  * @v bit               Bit to send
103  */
104 static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
105         setsda ( basher, bit );
106         setscl ( basher, 1 );
107         setscl ( basher, 0 );
108         setsda ( basher, 1 );
109 }
110
111 /**
112  * Receive an I2C data bit
113  *
114  * @v basher            Bit-bashing interface
115  * @ret bit             Received bit
116  */
117 static int i2c_recv_bit ( struct bit_basher *basher ) {
118         int bit;
119
120         setscl ( basher, 1 );
121         bit = getsda ( basher );
122         setscl ( basher, 0 );
123         return bit;
124 }
125
126 /**
127  * Send an I2C stop condition
128  *
129  * @v basher            Bit-bashing interface
130  */
131 static void i2c_stop ( struct bit_basher *basher ) {
132         setsda ( basher, 0 );
133         setscl ( basher, 1 );
134         setsda ( basher, 1 );
135 }
136
137 /**
138  * Send byte via I2C bus and check for acknowledgement
139  *
140  * @v basher            Bit-bashing interface
141  * @v byte              Byte to send
142  * @ret rc              Return status code
143  *
144  * Sends a byte via the I2C bus and checks for an acknowledgement from
145  * the slave device.
146  */
147 static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
148         int i;
149         int ack;
150
151         /* Send byte */
152         DBG2 ( "[send %02x]", byte );
153         for ( i = 8 ; i ; i-- ) {
154                 i2c_send_bit ( basher, byte & 0x80 );
155                 byte <<= 1;
156         }
157
158         /* Check for acknowledgement from slave */
159         ack = ( i2c_recv_bit ( basher ) == 0 );
160         DBG2 ( "%s", ( ack ? "[acked]" : "[not acked]" ) );
161
162         return ( ack ? 0 : -EIO );
163 }
164
165 /**
166  * Receive byte via I2C bus
167  *
168  * @v basher            Bit-bashing interface
169  * @ret byte            Received byte
170  *
171  * Receives a byte via the I2C bus and sends NACK to the slave device.
172  */
173 static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
174         uint8_t byte = 0;
175         int i;
176
177         /* Receive byte */
178         for ( i = 8 ; i ; i-- ) {
179                 byte <<= 1;
180                 byte |= ( i2c_recv_bit ( basher ) & 0x1 );
181         }
182
183         /* Send NACK */
184         i2c_send_bit ( basher, 1 );
185
186         DBG2 ( "[rcvd %02x]", byte );
187         return byte;
188 }
189
190 /**
191  * Select I2C device for reading or writing
192  *
193  * @v basher            Bit-bashing interface
194  * @v i2cdev            I2C device
195  * @v offset            Starting offset within the device
196  * @v direction         I2C_READ or I2C_WRITE
197  * @ret rc              Return status code
198  */
199 static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
200                         unsigned int offset, unsigned int direction ) {
201         unsigned int address;
202         int shift;
203         unsigned int byte;
204         int rc;
205
206         i2c_start ( basher );
207
208         /* Calculate address to appear on bus */
209         address = ( ( ( i2cdev->dev_addr |
210                         ( offset >> ( 8 * i2cdev->word_addr_len ) ) ) << 1 )
211                     | direction );
212
213         /* Send address a byte at a time */
214         for ( shift = ( 8 * ( i2cdev->dev_addr_len - 1 ) ) ;
215               shift >= 0 ; shift -= 8 ) {
216                 byte = ( ( address >> shift ) & 0xff );
217                 if ( ( rc = i2c_send_byte ( basher, byte ) ) != 0 )
218                         return rc;
219         }
220
221         return 0;
222 }
223
224 /**
225  * Reset I2C bus
226  *
227  * @v basher            Bit-bashing interface
228  * @ret rc              Return status code
229  *
230  * i2c devices often don't have a reset line, so even a reboot or
231  * system power cycle is sometimes not enough to bring them back to a
232  * known state.
233  */
234 static int i2c_reset ( struct bit_basher *basher ) {
235         unsigned int i;
236         int sda;
237
238         /* Clock through several cycles, waiting for an opportunity to
239          * pull SDA low while SCL is high (which creates a start
240          * condition).
241          */
242         open_bit ( basher );
243         setscl ( basher, 0 );
244         setsda ( basher, 1 );
245         for ( i = 0 ; i < I2C_RESET_MAX_CYCLES ; i++ ) {
246                 setscl ( basher, 1 );
247                 sda = getsda ( basher );
248                 if ( sda ) {
249                         /* Now that the device will see a start, issue it */
250                         i2c_start ( basher );
251                         /* Stop the bus to leave it in a known good state */
252                         i2c_stop ( basher );
253                         DBGC ( basher, "I2CBIT %p reset after %d attempts\n",
254                                basher, ( i + 1 ) );
255                         close_bit ( basher );
256                         return 0;
257                 }
258                 setscl ( basher, 0 );
259         }
260
261         DBGC ( basher, "I2CBIT %p could not reset after %d attempts\n",
262                basher, i );
263         close_bit ( basher );
264         return -ETIMEDOUT;
265 }
266
267 /**
268  * Read data from I2C device via bit-bashing interface
269  *
270  * @v i2c               I2C interface
271  * @v i2cdev            I2C device
272  * @v offset            Starting offset within the device
273  * @v data              Data buffer
274  * @v len               Length of data buffer
275  * @ret rc              Return status code
276  *
277  * Note that attempting to read zero bytes of data is a valid way to
278  * check for I2C device presence.
279  */
280 static int i2c_bit_read ( struct i2c_interface *i2c,
281                           struct i2c_device *i2cdev, unsigned int offset,
282                           uint8_t *data, unsigned int len ) {
283         struct i2c_bit_basher *i2cbit
284                 = container_of ( i2c, struct i2c_bit_basher, i2c );
285         struct bit_basher *basher = &i2cbit->basher;
286         int rc = 0;
287
288         DBGC ( basher, "I2CBIT %p reading from device %x: ",
289                basher, i2cdev->dev_addr );
290
291         open_bit ( basher );
292
293         for ( ; ; data++, offset++ ) {
294
295                 /* Select device for writing */
296                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
297                                          I2C_WRITE ) ) != 0 )
298                         break;
299
300                 /* Abort at end of data */
301                 if ( ! ( len-- ) )
302                         break;
303
304                 /* Select offset */
305                 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
306                         break;
307                 
308                 /* Select device for reading */
309                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
310                                          I2C_READ ) ) != 0 )
311                         break;
312
313                 /* Read byte */
314                 *data = i2c_recv_byte ( basher );
315                 DBGC ( basher, "%02x ", *data );
316         }
317         
318         DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
319         i2c_stop ( basher );
320         close_bit ( basher );
321         return rc;
322 }
323
324 /**
325  * Write data to I2C device via bit-bashing interface
326  *
327  * @v i2c               I2C interface
328  * @v i2cdev            I2C device
329  * @v offset            Starting offset within the device
330  * @v data              Data buffer
331  * @v len               Length of data buffer
332  * @ret rc              Return status code
333  *
334  * Note that attempting to write zero bytes of data is a valid way to
335  * check for I2C device presence.
336  */
337 static int i2c_bit_write ( struct i2c_interface *i2c,
338                            struct i2c_device *i2cdev, unsigned int offset,
339                            const uint8_t *data, unsigned int len ) {
340         struct i2c_bit_basher *i2cbit
341                 = container_of ( i2c, struct i2c_bit_basher, i2c );
342         struct bit_basher *basher = &i2cbit->basher;
343         int rc = 0;
344
345         DBGC ( basher, "I2CBIT %p writing to device %x: ",
346                basher, i2cdev->dev_addr );
347
348         open_bit ( basher );
349
350         for ( ; ; data++, offset++ ) {
351
352                 /* Select device for writing */
353                 if ( ( rc = i2c_select ( basher, i2cdev, offset,
354                                          I2C_WRITE ) ) != 0 )
355                         break;
356                 
357                 /* Abort at end of data */
358                 if ( ! ( len-- ) )
359                         break;
360
361                 /* Select offset */
362                 if ( ( rc = i2c_send_byte ( basher, offset ) ) != 0 )
363                         break;
364                 
365                 /* Write data to device */
366                 DBGC ( basher, "%02x ", *data );
367                 if ( ( rc = i2c_send_byte ( basher, *data ) ) != 0 )
368                         break;
369         }
370
371         DBGC ( basher, "%s\n", ( rc ? "failed" : "" ) );
372         i2c_stop ( basher );
373         close_bit ( basher );
374         return rc;
375 }
376
377 /**
378  * Initialise I2C bit-bashing interface
379  *
380  * @v i2cbit            I2C bit-bashing interface
381  * @v bash_op           Bit-basher operations
382  */
383 int init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit,
384                           struct bit_basher_operations *bash_op ) {
385         struct bit_basher *basher = &i2cbit->basher;
386         int rc;
387
388         /* Initialise data structures */
389         basher->op = bash_op;
390         assert ( basher->op->read != NULL );
391         assert ( basher->op->write != NULL );
392         i2cbit->i2c.read = i2c_bit_read;
393         i2cbit->i2c.write = i2c_bit_write;
394
395         /* Reset I2C bus */
396         if ( ( rc = i2c_reset ( basher ) ) != 0 ) {
397                 DBGC ( basher, "I2CBIT %p could not reset I2C bus: %s\n",
398                        basher, strerror ( rc ) );
399                 return rc;
400         }
401
402         return 0;
403 }