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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <gpxe/bitbash.h>
  23. #include <gpxe/i2c.h>
  24. /** @file
  25. *
  26. * I2C bit-bashing interface
  27. *
  28. * This implements a simple I2C master via a bit-bashing interface
  29. * that provides two lines: SCL (clock) and SDA (data).
  30. */
  31. /**
  32. * Set state of I2C SCL line
  33. *
  34. * @v basher Bit-bashing interface
  35. * @v state New state of SCL
  36. */
  37. static inline __attribute__ (( always_inline )) void
  38. setscl ( struct bit_basher *basher, int state ) {
  39. write_bit ( basher, I2C_BIT_SCL, state );
  40. }
  41. /**
  42. * Set state of I2C SDA line
  43. *
  44. * @v basher Bit-bashing interface
  45. * @v state New state of SDA
  46. */
  47. static inline __attribute__ (( always_inline )) void
  48. setsda ( struct bit_basher *basher, int state ) {
  49. write_bit ( basher, I2C_BIT_SDA, state );
  50. }
  51. /**
  52. * Get state of I2C SDA line
  53. *
  54. * @v basher Bit-bashing interface
  55. * @ret state State of SDA
  56. */
  57. static inline __attribute__ (( always_inline )) int
  58. getsda ( struct bit_basher *basher ) {
  59. return read_bit ( basher, I2C_BIT_SDA );
  60. }
  61. /**
  62. * Send an I2C start condition
  63. *
  64. * @v basher Bit-bashing interface
  65. */
  66. static void i2c_start ( struct bit_basher *basher ) {
  67. setscl ( basher, 1 );
  68. setsda ( basher, 0 );
  69. setscl ( basher, 0 );
  70. setsda ( basher, 1 );
  71. }
  72. /**
  73. * Send an I2C data bit
  74. *
  75. * @v basher Bit-bashing interface
  76. * @v bit Bit to send
  77. */
  78. static void i2c_send_bit ( struct bit_basher *basher, int bit ) {
  79. setsda ( basher, bit );
  80. setscl ( basher, 1 );
  81. setscl ( basher, 0 );
  82. setsda ( basher, 1 );
  83. }
  84. /**
  85. * Receive an I2C data bit
  86. *
  87. * @v basher Bit-bashing interface
  88. * @ret bit Received bit
  89. */
  90. static int i2c_recv_bit ( struct bit_basher *basher ) {
  91. int bit;
  92. setscl ( basher, 1 );
  93. bit = getsda ( basher );
  94. setscl ( basher, 0 );
  95. return bit;
  96. }
  97. /**
  98. * Send an I2C stop condition
  99. *
  100. * @v basher Bit-bashing interface
  101. */
  102. static void i2c_stop ( struct bit_basher *basher ) {
  103. setsda ( basher, 0 );
  104. setscl ( basher, 1 );
  105. setsda ( basher, 1 );
  106. }
  107. /**
  108. * Send byte via I2C bus and check for acknowledgement
  109. *
  110. * @v basher Bit-bashing interface
  111. * @v byte Byte to send
  112. * @ret rc Return status code
  113. *
  114. * Sends a byte via the I2C bus and checks for an acknowledgement from
  115. * the slave device.
  116. */
  117. static int i2c_send_byte ( struct bit_basher *basher, uint8_t byte ) {
  118. int i;
  119. /* Send byte */
  120. for ( i = 8 ; i ; i-- ) {
  121. i2c_send_bit ( basher, byte & 0x80 );
  122. byte <<= 1;
  123. }
  124. /* Check for acknowledgement from slave */
  125. return ( i2c_recv_bit ( basher ) == 0 ? 0 : -EIO );
  126. }
  127. /**
  128. * Receive byte via I2C bus
  129. *
  130. * @v basher Bit-bashing interface
  131. * @ret byte Received byte
  132. *
  133. * Receives a byte via the I2C bus and sends NACK to the slave device.
  134. */
  135. static uint8_t i2c_recv_byte ( struct bit_basher *basher ) {
  136. uint8_t value = 0;
  137. int i;
  138. /* Receive byte */
  139. for ( i = 8 ; i ; i-- ) {
  140. value <<= 1;
  141. value |= i2c_recv_bit ( basher );
  142. }
  143. /* Send NACK */
  144. i2c_send_bit ( basher, 1 );
  145. return value;
  146. }
  147. /**
  148. * Select I2C device for reading or writing
  149. *
  150. * @v basher Bit-bashing interface
  151. * @v i2cdev I2C device
  152. * @v direction I2C_READ or I2C_WRITE
  153. * @ret rc Return status code
  154. */
  155. static int i2c_select ( struct bit_basher *basher, struct i2c_device *i2cdev,
  156. unsigned int direction ) {
  157. unsigned int address;
  158. int rc;
  159. i2c_start ( basher );
  160. /* First byte of the address */
  161. address = i2cdev->address;
  162. if ( i2cdev->tenbit ) {
  163. address |= I2C_TENBIT_ADDRESS;
  164. address >>= 8;
  165. }
  166. if ( ( rc = i2c_send_byte ( basher,
  167. ( ( address << 1 ) | direction ) ) ) != 0 )
  168. return rc;
  169. /* Second byte of the address (10-bit addresses only) */
  170. if ( i2cdev->tenbit ) {
  171. if ( ( rc = i2c_send_byte ( basher,
  172. ( i2cdev->address & 0xff ) ) ) !=0)
  173. return rc;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * Read data from I2C device via bit-bashing interface
  179. *
  180. * @v i2c I2C interface
  181. * @v i2cdev I2C device
  182. * @v offset Starting offset within the device
  183. * @v data Data buffer
  184. * @v len Length of data buffer
  185. * @ret rc Return status code
  186. *
  187. * Note that attempting to read zero bytes of data is a valid way to
  188. * check for I2C device presence.
  189. */
  190. static int i2c_bit_read ( struct i2c_interface *i2c,
  191. struct i2c_device *i2cdev, unsigned int offset,
  192. uint8_t *data, unsigned int len ) {
  193. struct i2c_bit_basher *i2cbit
  194. = container_of ( i2c, struct i2c_bit_basher, i2c );
  195. struct bit_basher *basher = &i2cbit->basher;
  196. int rc = 0;
  197. DBG ( "Reading from I2C device %x: ", i2cdev->address );
  198. while ( 1 ) {
  199. /* Select device for writing */
  200. if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
  201. break;
  202. /* Abort at end of data */
  203. if ( ! ( len-- ) )
  204. break;
  205. /* Select offset */
  206. if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
  207. break;
  208. /* Select device for reading */
  209. if ( ( rc = i2c_select ( basher, i2cdev, I2C_READ ) ) != 0 )
  210. break;
  211. /* Read byte */
  212. *data++ = i2c_recv_byte ( basher );
  213. DBG ( "%02x ", *(data - 1) );
  214. }
  215. DBG ( "%s\n", ( rc ? "failed" : "" ) );
  216. i2c_stop ( basher );
  217. return rc;
  218. }
  219. /**
  220. * Write data to I2C device via bit-bashing interface
  221. *
  222. * @v i2c I2C interface
  223. * @v i2cdev I2C device
  224. * @v offset Starting offset within the device
  225. * @v data Data buffer
  226. * @v len Length of data buffer
  227. * @ret rc Return status code
  228. *
  229. * Note that attempting to write zero bytes of data is a valid way to
  230. * check for I2C device presence.
  231. */
  232. static int i2c_bit_write ( struct i2c_interface *i2c,
  233. struct i2c_device *i2cdev, unsigned int offset,
  234. const uint8_t *data, unsigned int len ) {
  235. struct i2c_bit_basher *i2cbit
  236. = container_of ( i2c, struct i2c_bit_basher, i2c );
  237. struct bit_basher *basher = &i2cbit->basher;
  238. int rc = 0;
  239. DBG ( "Writing to I2C device %x: ", i2cdev->address );
  240. while ( 1 ) {
  241. /* Select device for writing */
  242. if ( ( rc = i2c_select ( basher, i2cdev, I2C_WRITE ) ) != 0 )
  243. break;
  244. /* Abort at end of data */
  245. if ( ! ( len-- ) )
  246. break;
  247. /* Select offset */
  248. if ( ( rc = i2c_send_byte ( basher, offset++ ) ) != 0 )
  249. break;
  250. /* Write data to device */
  251. DBG ( "%02x ", *data );
  252. if ( ( rc = i2c_send_byte ( basher, *data++ ) ) != 0 )
  253. break;
  254. }
  255. DBG ( "%s\n", ( rc ? "failed" : "" ) );
  256. i2c_stop ( basher );
  257. return rc;
  258. }
  259. /**
  260. * Initialise I2C bit-bashing interface
  261. *
  262. * @v i2cbit I2C bit-bashing interface
  263. */
  264. void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit ) {
  265. struct bit_basher *basher = &i2cbit->basher;
  266. assert ( basher->read != NULL );
  267. assert ( basher->write != NULL );
  268. i2cbit->i2c.read = i2c_bit_read;
  269. i2cbit->i2c.write = i2c_bit_write;
  270. basher->udelay = I2C_UDELAY;
  271. i2c_stop ( basher );
  272. }