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

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