You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c_bit.c 9.6KB

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