common_twi.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  3. *
  4. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  5. * the the People's Republic of China and other countries.
  6. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  7. *
  8. * DISCLAIMER
  9. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  10. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  11. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  12. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  13. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  14. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  15. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  19. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  21. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  22. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __COMMON_TWI_I_H__
  33. #define __COMMON_TWI_I_H__
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /* TWI Register Offset */
  38. #define TWI_ADDR_REG (0x00) /* 31:8bit reserved,7-1bit for slave addr,0 bit for GCE */
  39. #define TWI_XADDR_REG (0x04) /* 31:8bit reserved,7-0bit for second addr in 10bit addr */
  40. #define TWI_DATA_REG (0x08) /* 31:8bit reserved, 7-0bit send or receive data byte */
  41. #define TWI_CTL_REG (0x0C) /* INT_EN,BUS_EN,M_STA,INT_FLAG,A_ACK */
  42. #define TWI_STAT_REG (0x10) /* 28 interrupt types + 0xF8 normal type = 29 */
  43. #define TWI_CLK_REG (0x14) /* 31:7bit reserved,6-3bit,CLK_M,2-0bit CLK_N */
  44. #define TWI_SRST_REG (0x18) /* 31:1bit reserved;0bit,write 1 to clear 0. */
  45. #define TWI_EFR_REG (0x1C) /* 31:2bit reserved,1:0 bit data byte follow read command */
  46. #define TWI_LCR_REG (0x20) /* 31:6bits reserved 5:0bit for sda&scl control*/
  47. #define TWI_DVFS_REG (0x24) /* 31:3bits reserved 2:0bit for dvfs control. only A10 support. */
  48. #define TWI_DRIVER_CTRL (0x200)
  49. #define TWI_DRIVER_CFG (0x204)
  50. #define TWI_DRIVER_SLV (0x208)
  51. #define TWI_DRIVER_FMT (0x20C)
  52. #define TWI_DRIVER_BUSC (0x210)
  53. #define TWI_DRIVER_INTC (0x214)
  54. #define TWI_DRIVER_DMAC (0x218)
  55. #define TWI_DRIVER_FIFOC (0x21C)
  56. #define TWI_DRIVER_SENDF (0x300)
  57. #define TWI_DRIVER_RECVF (0x304)
  58. /* TWI address register */
  59. /* general call address enable for slave mode */
  60. #define TWI_GCE_EN (0x1<<0)
  61. #define TWI_ADDR_MASK (0x7f<<1) /* 7:1bits */
  62. /* 31:8bits reserved */
  63. /* TWI extend address register */
  64. /* 7:0bits for extend slave address */
  65. #define TWI_XADDR_MASK (0xff)
  66. /* 31:8bits reserved */
  67. /* TWI Data register default is 0x0000_0000 */
  68. /* 7:0bits for send or received */
  69. #define TWI_DATA_MASK (0xff)
  70. /* TWI Control Register Bit Fields & Masks, default value: 0x0000_0000*/
  71. /* 1:0 bits reserved */
  72. /* set 1 to send A_ACK,then low level on SDA */
  73. #define TWI_CTL_ACK (0x1<<2)
  74. /* INT_FLAG,interrupt status flag: set '1' when interrupt coming */
  75. #define TWI_CTL_INTFLG (0x1<<3)
  76. #define TWI_CTL_STP (0x1<<4) /* M_STP,Automatic clear 0 */
  77. #define TWI_CTL_STA (0x1<<5) /* M_STA,atutomatic clear 0 */
  78. /* BUS_EN, master mode should be set 1.*/
  79. #define TWI_CTL_BUSEN (0x1<<6)
  80. #define TWI_CTL_INTEN (0x1<<7) /* INT_EN */
  81. /* 31:8 bit reserved */
  82. /* TWI Clock Register Bit Fields & Masks,default value:0x0000_0000 */
  83. /*
  84. * Fin is APB CLOCK INPUT;
  85. * Fsample = F0 = Fin/2^CLK_N;
  86. * F1 = F0/(CLK_M+1);
  87. *
  88. * Foscl = F1/10 = Fin/(2^CLK_N * (CLK_M+1)*10);
  89. * Foscl is clock SCL;standard mode:100KHz or fast mode:400KHz
  90. */
  91. #define TWI_CLK_DUTY (0x1<<7) /* 7bit */
  92. #define TWI_CLK_DIV_M (0xf<<3) /* 6:3bit */
  93. #define TWI_CLK_DIV_N (0x7<<0) /* 2:0bit */
  94. /* TWI Soft Reset Register Bit Fields & Masks */
  95. /* write 1 to clear 0, when complete soft reset clear 0 */
  96. #define TWI_SRST_SRST (0x1<<0)
  97. /* TWI Enhance Feature Register Bit Fields & Masks */
  98. /* default -- 0x0 */
  99. /* 00:no,01: 1byte, 10:2 bytes, 11: 3bytes */
  100. #define TWI_EFR_MASK (0x3<<0)
  101. #define TWI_EFR_WARC_0 (0x0<<0)
  102. #define TWI_EFR_WARC_1 (0x1<<0)
  103. #define TWI_EFR_WARC_2 (0x2<<0)
  104. #define TWI_EFR_WARC_3 (0x3<<0)
  105. /* twi line control register -default value: 0x0000_003a */
  106. /* SDA line state control enable ,1:enable;0:disable */
  107. #define TWI_LCR_SDA_EN (0x01<<0)
  108. /* SDA line state control bit, 1:high level;0:low level */
  109. #define TWI_LCR_SDA_CTL (0x01<<1)
  110. /* SCL line state control enable ,1:enable;0:disable */
  111. #define TWI_LCR_SCL_EN (0x01<<2)
  112. /* SCL line state control bit, 1:high level;0:low level */
  113. #define TWI_LCR_SCL_CTL (0x01<<3)
  114. /* current state of SDA,readonly bit */
  115. #define TWI_LCR_SDA_STATE_MASK (0x01<<4)
  116. /* current state of SCL,readonly bit */
  117. #define TWI_LCR_SCL_STATE_MASK (0x01<<5)
  118. /* 31:6bits reserved */
  119. #define TWI_LCR_IDLE_STATUS (0x3a)
  120. #define TWI_LCR_NORM_STATUS (0x30) /* normal status */
  121. /* TWI Status Register Bit Fields & Masks */
  122. #define TWI_STAT_MASK (0xff)
  123. /* 7:0 bits use only,default is 0xF8 */
  124. #define TWI_STAT_BUS_ERR (0x00) /* BUS ERROR */
  125. /* Master mode use only */
  126. #define TWI_STAT_TX_STA (0x08) /* START condition transmitted */
  127. /* Repeated START condition transmitted */
  128. #define TWI_STAT_TX_RESTA (0x10)
  129. /* Address+Write bit transmitted, ACK received */
  130. #define TWI_STAT_TX_AW_ACK (0x18)
  131. /* Address+Write bit transmitted, ACK not received */
  132. #define TWI_STAT_TX_AW_NAK (0x20)
  133. /* data byte transmitted in master mode,ack received */
  134. #define TWI_STAT_TXD_ACK (0x28)
  135. /* data byte transmitted in master mode ,ack not received */
  136. #define TWI_STAT_TXD_NAK (0x30)
  137. /* arbitration lost in address or data byte */
  138. #define TWI_STAT_ARBLOST (0x38)
  139. /* Address+Read bit transmitted, ACK received */
  140. #define TWI_STAT_TX_AR_ACK (0x40)
  141. /* Address+Read bit transmitted, ACK not received */
  142. #define TWI_STAT_TX_AR_NAK (0x48)
  143. /* data byte received in master mode ,ack transmitted */
  144. #define TWI_STAT_RXD_ACK (0x50)
  145. /* date byte received in master mode,not ack transmitted */
  146. #define TWI_STAT_RXD_NAK (0x58)
  147. /* Slave mode use only */
  148. /* Slave address+Write bit received, ACK transmitted */
  149. #define TWI_STAT_RXWS_ACK (0x60)
  150. #define TWI_STAT_ARBLOST_RXWS_ACK (0x68)
  151. /* General Call address received, ACK transmitted */
  152. #define TWI_STAT_RXGCAS_ACK (0x70)
  153. #define TWI_STAT_ARBLOST_RXGCAS_ACK (0x78)
  154. #define TWI_STAT_RXDS_ACK (0x80)
  155. #define TWI_STAT_RXDS_NAK (0x88)
  156. #define TWI_STAT_RXDGCAS_ACK (0x90)
  157. #define TWI_STAT_RXDGCAS_NAK (0x98)
  158. #define TWI_STAT_RXSTPS_RXRESTAS (0xA0)
  159. #define TWI_STAT_RXRS_ACK (0xA8)
  160. #define TWI_STAT_ARBLOST_SLAR_ACK (0xB0)
  161. /* 10bit Address, second part of address */
  162. /* Second Address byte+Write bit transmitted,ACK received */
  163. #define TWI_STAT_TX_SAW_ACK (0xD0)
  164. /* Second Address byte+Write bit transmitted,ACK not received */
  165. #define TWI_STAT_TX_SAW_NAK (0xD8)
  166. /* No relevant status information,INT_FLAG = 0 */
  167. #define TWI_STAT_IDLE (0xF8)
  168. /* Offset:0x0200. Twi driver control register(Default Value:0x00F8_0000) */
  169. #define TWI_DRV_EN (0x01<<0)
  170. #define TWI_DRV_RST (0x01<<1)
  171. #define TWI_DRV_STA (0xff<<16)
  172. #define TRAN_RESULT (0x0f<<24)
  173. #define READ_TRAN (0x01<<28)
  174. #define START_TRAN (0x01<<31)
  175. #define TRAN_OK 0x00
  176. #define TRAN_FAIL 0x01
  177. /*
  178. * Offset:0x0204.
  179. * Twi driver transmission configuration register(Default Value:0x1000_0001)
  180. */
  181. #define PACKET_MASK (0xffff<<0)
  182. #define INTERVAL_MASK (0xff<<16)
  183. /* Offset:0x0208. Twi driver slave id register(Default Value:0x0000_0000) */
  184. #define SLV_ID_X (0xff<<0)
  185. #define SLV_RD_CMD (0x01<<8)
  186. #define SLV_ID (0x7f<<9)
  187. /*
  188. * Offset:0x020C.
  189. * Twi driver packet format register(Default Value:0x0001_0001)
  190. */
  191. #define DATA_BYTE 0xffff
  192. #define ADDR_BYTE (0xff<<16)
  193. /* Offset:0x0210. Twi driver bus control register(Default Value:0x0000_00C0) */
  194. #define TWI_DRV_CLK_DUTY (0x01<<16)
  195. #define TWI_DRV_CLK_M (0x0f<<8)
  196. #define TWI_DRV_CLK_N (0x07<<12)
  197. /*
  198. * Offset:0x0214.
  199. * Twi driver interrupt control register(Default Value:0x0000_0000)
  200. */
  201. #define TRAN_COM_PD (0x1<<0)
  202. #define TRAN_ERR_PD (0x1<<1)
  203. #define TX_REQ_PD (0x1<<2)
  204. #define RX_REQ_PD (0x1<<3)
  205. #define TRAN_COM_INT (0x1<<16)
  206. #define TRAN_ERR_INT (0x1<<17)
  207. #define TX_REQ_INT (0x1<<18)
  208. #define RX_REQ_INT (0x1<<19)
  209. #define TWI_DRV_INT_MASK (0x0f<<16)
  210. #define TWI_DRV_STAT_MASK (0x0f<<0)
  211. /*
  212. * Offset:0x0218.
  213. * Twi driver DMA configure register(Default Value:0x0010_0010)
  214. */
  215. #define TRIG_DEFAULT 0x10
  216. #define TRIG_MASK 0x3f
  217. #define DMA_TX (0x01<<8)
  218. #define DMA_RX (0x01<<24)
  219. #define TWI_DRQEN_MASK (DMA_TX | DMA_RX)
  220. /* Offset:0x021C. Twi driver FIFO content register(Default Value:0x0000_0000) */
  221. #define SEND_FIFO_CONT (0x3f<<0)
  222. #define SEND_FIFO_CLEAR (0x01<<6)
  223. #define RECV_FIFO_CONT (0x3f<<16)
  224. #define RECV_FIFO_CLEAR (0x01<<22)
  225. /*
  226. * Offset:0x0300.
  227. * Twi driver send data FIFO access register(Default Value:0x0000_0000)
  228. */
  229. #define SEND_DATA_FIFO (0xff<<0)
  230. /*
  231. * Offset:0x0304.
  232. * Twi driver receive data FIFO access register(Default Value:0x0000_0000)
  233. */
  234. #define RECV_DATA_FIFO (0xff<<0)
  235. /* TWI driver result */
  236. #define RESULT_COMPLETE 1
  237. #define RESULT_ERR 2
  238. /* TWI mode select */
  239. #define TWI_MASTER_MODE (1)
  240. #define TWI_SLAVE_MODE (0) /* seldom use */
  241. #define TWI_SEM_MAX_COUNT 0xFFFFFFFFUL
  242. #define SUNXI_TWI_OK 0
  243. #define SUNXI_TWI_FAIL -1
  244. #define SUNXI_TWI_RETRY -2
  245. #define SUNXI_TWI_SFAIL -3 /* start fail */
  246. #define SUNXI_TWI_TFAIL -4 /* stop fail */
  247. #define DMA_THRESHOLD 32
  248. #define MAX_FIFO 32
  249. #define DMA_TIMEOUT 1000
  250. #define TWI_PIN_NUM 2 /*pin num of twi*/
  251. #ifdef __cplusplus
  252. }
  253. #endif
  254. #endif /* __COMMON_TWI_I_H__ */