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.4KB

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