i2c.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #ifndef I2C_H
  2. #define I2C_H
  3. /****************************************************
  4. *
  5. * Copyright Motrola 1999
  6. *
  7. ****************************************************/
  8. #define get_eumbbar() CFG_EUMB_ADDR
  9. #define I2CADR 0x00003000
  10. #define I2CFDR 0x00003004
  11. #define I2CCR 0x00003008
  12. #define I2CSR 0x0000300C
  13. #define I2CDR 0x00003010
  14. typedef enum _i2cstatus
  15. {
  16. I2CSUCCESS = 0x3000,
  17. I2CADDRESS,
  18. I2CERROR,
  19. I2CBUFFFULL,
  20. I2CBUFFEMPTY,
  21. I2CXMITERROR,
  22. I2CRCVERROR,
  23. I2CBUSBUSY,
  24. I2CALOSS,
  25. I2CNOEVENT,
  26. } I2CStatus;
  27. typedef enum i2c_control
  28. {
  29. MEN = 0x00000080,
  30. MIEN = 0x00000040,
  31. MSTA = 0x00000020,
  32. MTX = 0x00000010,
  33. TXAK = 0x00000008,
  34. RSTA = 0x00000004,
  35. } I2C_CONTROL;
  36. typedef enum i2c_status
  37. {
  38. MCF = 0x00000080,
  39. MAAS = 0x00000040,
  40. MBB = 0x00000020,
  41. MAL = 0x00000010,
  42. SRW = 0x00000004,
  43. MIF = 0x00000002,
  44. RXAK = 0x00000001,
  45. } I2C_STATUS;
  46. typedef struct _i2c_ctrl
  47. {
  48. unsigned int reserved0 : 24;
  49. unsigned int men : 1;
  50. unsigned int mien : 1;
  51. unsigned int msta : 1;
  52. unsigned int mtx : 1;
  53. unsigned int txak : 1;
  54. unsigned int rsta : 1;
  55. unsigned int reserved1 : 2;
  56. } I2C_CTRL;
  57. typedef struct _i2c_stat
  58. {
  59. unsigned int rsrv0 : 24;
  60. unsigned int mcf : 1;
  61. unsigned int maas : 1;
  62. unsigned int mbb : 1;
  63. unsigned int mal : 1;
  64. unsigned int rsrv1 : 1;
  65. unsigned int srw : 1;
  66. unsigned int mif : 1;
  67. unsigned int rxak : 1;
  68. } I2C_STAT;
  69. typedef enum _i2c_mode
  70. {
  71. RCV = 0,
  72. XMIT = 1,
  73. } I2C_MODE;
  74. /******************** App. API ********************
  75. * The application API is for user level application
  76. * to use the funcitonality provided by I2C driver
  77. *
  78. * Note: Its App.s responsibility to swap the data
  79. * byte. In our API, we just transfer whatever
  80. * we are given
  81. **************************************************/
  82. /**
  83. * Note:
  84. *
  85. * In all following functions,
  86. * the caller shall pass the configured embedded utility memory
  87. * block base, EUMBBAR.
  88. **/
  89. /* Send a buffer of data to the intended rcv_addr.
  90. * If stop_flag is set, after the whole buffer
  91. * is sent, generate a STOP signal provided that the
  92. * receiver doesn't signal the STOP in the middle.
  93. * I2C is the master performing transmitting. If
  94. * no STOP signal is generated at the end of current
  95. * transaction, the master can generate a START signal
  96. * to another slave addr.
  97. *
  98. * return I2CSUCCESS if no error.
  99. */
  100. static I2CStatus I2C_put( unsigned int eumbbar,
  101. unsigned char rcv_addr, /* receiver's address */
  102. unsigned char *buffer_ptr, /* pointer of data to be sent */
  103. unsigned int length, /* number of byte of in the buffer */
  104. unsigned int stop_flag, /* 1 - signal STOP when buffer is empty
  105. * 0 - no STOP signal when buffer is empty
  106. */
  107. unsigned int is_cnt ); /* 1 - this is a restart, don't check MBB
  108. * 0 - this is a new start, check MBB
  109. */
  110. /* Receive a buffer of data from the desired sender_addr
  111. * If stop_flag is set, when the buffer is full and the
  112. * sender does not signal STOP, generate a STOP signal.
  113. * I2C is the master performing receiving. If no STOP signal
  114. * is generated, the master can generate a START signal
  115. * to another slave addr.
  116. *
  117. * return I2CSUCCESS if no error.
  118. */
  119. static I2CStatus I2C_get( unsigned int eumbbar,
  120. unsigned char sender_addr, /* sender's address */
  121. unsigned char *buffer_ptr, /* pointer of receiving buffer */
  122. unsigned int length, /* length of the receiving buffer */
  123. unsigned int stop_flag, /* 1 - signal STOP when buffer is full
  124. * 0 - no STOP signal when buffer is full
  125. */
  126. unsigned int is_cnt ); /* 1 - this is a restart, don't check MBB
  127. * 0 - this is a new start, check MBB
  128. */
  129. #if 0 /* the I2C_write and I2C_read functions are not active */
  130. /* Send a buffer of data to the requiring master.
  131. * If stop_flag is set, after the whole buffer is sent,
  132. * generate a STOP signal provided that the requiring
  133. * receiver doesn't signal the STOP in the middle.
  134. * I2C is the slave performing transmitting.
  135. *
  136. * return I2CSUCCESS if no error.
  137. *
  138. * Note: due to the Kahlua design, slave transmitter
  139. * shall not signal STOP since there is no way
  140. * for master to detect it, causing I2C bus hung.
  141. *
  142. * For the above reason, the stop_flag is always
  143. * set, i.e., 1.
  144. *
  145. * programmer shall use the timer on Kahlua to
  146. * control the interval of data byte at the
  147. * master side.
  148. */
  149. static I2CStatus I2C_write( unsigned int eumbbar,
  150. unsigned char *buffer_ptr, /* pointer of data to be sent */
  151. unsigned int length, /* number of byte of in the buffer */
  152. unsigned int stop_flag ); /* 1 - signal STOP when buffer is empty
  153. * 0 - no STOP signal when buffer is empty
  154. */
  155. /* Receive a buffer of data from the sending master.
  156. * If stop_flag is set, when the buffer is full and the
  157. * sender does not signal STOP, generate a STOP signal.
  158. * I2C is the slave performing receiving.
  159. *
  160. * return I2CSUCCESS if no error.
  161. */
  162. static I2CStatus I2C_read(unsigned int eumbbar,
  163. unsigned char *buffer_ptr, /* pointer of receiving buffer */
  164. unsigned int length, /* length of the receiving buffer */
  165. unsigned int stop_flag ); /* 1 - signal STOP when buffer is full
  166. * 0 - no STOP signal when buffer is full
  167. */
  168. #endif /* of if0 for turning off I2C_read & I2C_write */
  169. /* if interrupt is not used, this is the timer event handler.
  170. * After each fixed time interval, this function can be called
  171. * to check the I2C status and call appropriate function to
  172. * handle the status event.
  173. */
  174. static I2CStatus I2C_Timer_Event( unsigned int eumbbar, I2CStatus (*handler)( unsigned int ) );
  175. /********************* Kernel API ************************
  176. * Kernel APIs are functions I2C driver provides to the
  177. * O.S.
  178. *********************************************************/
  179. /******************* device I/O function ***************/
  180. /* Generate a START signal in the desired mode.
  181. * I2C is the master.
  182. *
  183. * return I2CSUCCESS if no error.
  184. * I2CERROR if i2c unit is not enabled.
  185. * I2CBUSBUSY if bus cannot be granted
  186. */
  187. static I2CStatus I2C_Start( unsigned int eumbbar,
  188. unsigned char slave_addr, /* address of the receiver */
  189. I2C_MODE mode, /* XMIT(1) - put (write)
  190. * RCV(0) - get (read)
  191. */
  192. unsigned int is_cnt ); /* 1 - this is a restart, don't check MBB
  193. * 0 - this is a new start, check MBB
  194. */
  195. /* Generate a STOP signal to terminate the transaction. */
  196. static I2CStatus I2C_Stop( unsigned int eumbbar );
  197. /* Do a one-byte master transmit.
  198. *
  199. * return I2CBUFFEMPTY if this is the last byte.
  200. * Otherwise return I2CSUCCESS
  201. */
  202. static I2CStatus I2C_Master_Xmit( unsigned int eumbbar );
  203. /* Do a one-byte master receive.
  204. *
  205. * return I2CBUFFFULL if this is the last byte.
  206. * Otherwise return I2CSUCCESS
  207. */
  208. static I2CStatus I2C_Master_Rcv( unsigned int eumbbar );
  209. /* Do a one-byte slave transmit.
  210. *
  211. * return I2CBUFFEMPTY if this is the last byte.
  212. * Otherwise return I2CSUCCESS
  213. *
  214. */
  215. static I2CStatus I2C_Slave_Xmit( unsigned int eumbbar );
  216. /* Do a one-byte slave receive.
  217. *
  218. * return I2CBUFFFULL if this is the last byte.
  219. * Otherwise return I2CSUCCESS
  220. */
  221. static I2CStatus I2C_Slave_Rcv( unsigned int eumbbar );
  222. /* Process slave address phase.
  223. *
  224. * return I2CADDRESS if this is slave receiver's address phase
  225. * Otherwise return the result of slave xmit one byte.
  226. */
  227. static I2CStatus I2C_Slave_Addr( unsigned int eumbbar );
  228. /******************* Device Control Fucntion ****************/
  229. /* Initialize I2C unit with desired frequency divider,
  230. * driver's slave address w/o interrupt enabled.
  231. *
  232. * This function must be called before I2C unit can
  233. * be used.
  234. */
  235. static I2CStatus I2C_Init( unsigned int eumbbar,
  236. unsigned char fdr, /* frequency divider */
  237. unsigned char addr, /* driver's address used for receiving */
  238. unsigned int en_int); /* 1 - enable I2C interrupt
  239. * 0 - disable I2C interrup
  240. */
  241. /* I2C interrupt service routine.
  242. *
  243. * return I2CADDRESS if it is receiver's (either master or slave) address phase.
  244. * return the result of xmit or receive one byte
  245. */
  246. static I2CStatus I2C_ISR(unsigned int eumbbar );
  247. /* Set I2C Status, i.e., write to I2CSR */
  248. static void I2C_Set_Stat( unsigned int eumbbar, I2C_STAT stat );
  249. /* Query I2C Status, i.e., read I2CSR */
  250. static I2C_STAT I2C_Get_Stat( unsigned int eumbbar );
  251. /* Change I2C Control bits, i.e., write to I2CCR */
  252. static void I2C_Set_Ctrl( unsigned int eumbbar, I2C_CTRL ); /* new control value */
  253. /* Query I2C Control bits, i.e., read I2CCR */
  254. static I2C_CTRL I2C_Get_Ctrl( unsigned int eumbbar );
  255. /* This function performs the work for I2C_do_transaction. The work is
  256. * split into this function to enable I2C_do_transaction to first transmit
  257. * the data address to the I2C slave device without putting the data address
  258. * into the first byte of the buffer.
  259. *
  260. * en_int controls interrupt/polling mode
  261. * act is the type of transaction
  262. * i2c_addr is the I2C address of the slave device
  263. * len is the length of data to send or receive
  264. * buffer is the address of the data buffer
  265. * stop = I2C_NO_STOP, don't signal STOP at end of transaction
  266. * I2C_STOP, signal STOP at end of transaction
  267. * retry is the timeout retry value, currently ignored
  268. * rsta = I2C_NO_RESTART, this is not continuation of existing transaction
  269. * I2C_RESTART, this is a continuation of existing transaction
  270. */
  271. static I2C_Status I2C_do_buffer( I2C_INTERRUPT_MODE en_int,
  272. I2C_TRANSACTION_MODE act,
  273. unsigned char i2c_addr,
  274. int len,
  275. unsigned char *buffer,
  276. I2C_STOP_MODE stop,
  277. int retry,
  278. I2C_RESTART_MODE rsta);
  279. #endif