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

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