i2c.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. i2c.c
  9. Abstract:
  10. This module implements I2C support for the TI AM335x SoC in UEFI.
  11. Author:
  12. Evan Green 5-Jan-2015
  13. Environment:
  14. Firmware
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <uefifw.h>
  20. #include <minoca/soc/am335x.h>
  21. #include "bbonefw.h"
  22. //
  23. // --------------------------------------------------------------------- Macros
  24. //
  25. #define AM335_I2C_READ(_Register) \
  26. EfiReadRegister32((VOID *)(AM335_I2C_0_BASE + _Register))
  27. #define AM335_I2C_WRITE(_Register, _Value) \
  28. EfiWriteRegister32((VOID *)(AM335_I2C_0_BASE + _Register), _Value)
  29. //
  30. // ---------------------------------------------------------------- Definitions
  31. //
  32. //
  33. // ------------------------------------------------------ Data Type Definitions
  34. //
  35. //
  36. // ----------------------------------------------- Internal Function Prototypes
  37. //
  38. VOID
  39. EfipAm335I2cConfigureBusClock (
  40. VOID
  41. );
  42. //
  43. // -------------------------------------------------------------------- Globals
  44. //
  45. //
  46. // ------------------------------------------------------------------ Functions
  47. //
  48. VOID
  49. EfipAm335I2c0Initialize (
  50. VOID
  51. )
  52. /*++
  53. Routine Description:
  54. This routine initializes the I2c bus.
  55. Arguments:
  56. None.
  57. Return Value:
  58. None.
  59. --*/
  60. {
  61. UINT32 Value;
  62. //
  63. // Set the pin muxing on I2C 0.
  64. //
  65. Value = (1 << AM335_SOC_CONF_MUX_PUTYPESEL_SHIFT) |
  66. (1 << AM335_SOC_CONF_MUX_RXACTIVE_SHIFT) |
  67. (1 << AM335_SOC_CONF_MUX_SLEWCTRL_SHIFT);
  68. EfiWriteRegister32(
  69. (VOID *)(AM335_SOC_CONTROL_REGISTERS + AM335_SOC_CONTROL_I2C0_SDA),
  70. Value);
  71. Value = (1 << AM335_SOC_CONF_MUX_PUTYPESEL_SHIFT) |
  72. (1 << AM335_SOC_CONF_MUX_RXACTIVE_SHIFT) |
  73. (1 << AM335_SOC_CONF_MUX_SLEWCTRL_SHIFT);
  74. EfiWriteRegister32(
  75. (VOID *)(AM335_SOC_CONTROL_REGISTERS + AM335_SOC_CONTROL_I2C0_SCL),
  76. Value);
  77. //
  78. // Disable the I2C controller.
  79. //
  80. Value = AM335_I2C_READ(Am3I2cControl);
  81. Value &= ~AM335_I2C_CONTROL_ENABLE;
  82. AM335_I2C_WRITE(Am3I2cControl, Value);
  83. //
  84. // Reset the controller.
  85. //
  86. Value = AM335_I2C_READ(Am3I2cSysControl);
  87. Value |= AM335_I2C_SYSTEM_CONTROL_SOFT_RESET;
  88. AM335_I2C_WRITE(Am3I2cSysControl, Value);
  89. //
  90. // Disable auto idle.
  91. //
  92. Value = AM335_I2C_READ(Am3I2cSysControl);
  93. Value &= ~AM335_I2C_SYSTEM_CONTROL_AUTO_IDLE;
  94. AM335_I2C_WRITE(Am3I2cSysControl, Value);
  95. //
  96. // Configure the bus speed to be 100kHz.
  97. //
  98. EfipAm335I2cConfigureBusClock();
  99. //
  100. // Enable the I2C controller.
  101. //
  102. Value = AM335_I2C_READ(Am3I2cControl);
  103. Value |= AM335_I2C_CONTROL_ENABLE;
  104. AM335_I2C_WRITE(Am3I2cControl, Value);
  105. //
  106. // Wait for the system status to indicate the controller is ready.
  107. //
  108. do {
  109. Value = AM335_I2C_READ(Am3I2cSysStatus);
  110. } while ((Value & AM335_I2C_SYSTEM_STATUS_RESET_DONE) == 0);
  111. return;
  112. }
  113. VOID
  114. EfipAm335I2c0SetSlaveAddress (
  115. UINT8 SlaveAddress
  116. )
  117. /*++
  118. Routine Description:
  119. This routine sets which address on the I2C bus to talk to.
  120. Arguments:
  121. SlaveAddress - Supplies the slave address to communicate with.
  122. Return Value:
  123. None.
  124. --*/
  125. {
  126. AM335_I2C_WRITE(Am3I2cSlaveAddress, SlaveAddress);
  127. return;
  128. }
  129. VOID
  130. EfipAm335I2c0Read (
  131. UINT32 Register,
  132. UINT32 Size,
  133. UINT8 *Data
  134. )
  135. /*++
  136. Routine Description:
  137. This routine performs a read from the I2C bus. This routine assumes the
  138. slave address has already been set.
  139. Arguments:
  140. Register - Supplies the register to read from. Supply -1 to skip
  141. transmitting a register number.
  142. Size - Supplies the number of data bytes to read.
  143. Data - Supplies a pointer where the data will be returned on success.
  144. Return Value:
  145. None.
  146. --*/
  147. {
  148. UINT32 Value;
  149. //
  150. // Transmit the register number. Set the size to 1, clear all interrupts,
  151. // start the transfer, write the byte, clear the transmit ready interrupt,
  152. // and wait for the ready bit.
  153. //
  154. if (Register <= MAX_UINT8) {
  155. AM335_I2C_WRITE(Am3I2cCount, 1);
  156. AM335_I2C_WRITE(Am3I2cInterruptStatus,
  157. AM335_I2C_INTERRUPT_STATUS_MASK);
  158. Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_TRANSMIT |
  159. AM335_I2C_CONTROL_ENABLE;
  160. AM335_I2C_WRITE(Am3I2cControl, Value);
  161. Value |= AM335_I2C_CONTROL_START;
  162. AM335_I2C_WRITE(Am3I2cControl, Value);
  163. do {
  164. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  165. } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
  166. AM335_I2C_WRITE(Am3I2cData, Register);
  167. AM335_I2C_WRITE(Am3I2cInterruptStatus,
  168. AM335_I2C_INTERRUPT_TX_READY);
  169. do {
  170. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  171. } while ((Value & AM335_I2C_INTERRUPT_ACCESS_READY) == 0);
  172. }
  173. //
  174. // Now set the data count to the number of bytes, and set up the receive.
  175. //
  176. AM335_I2C_WRITE(Am3I2cCount, Size);
  177. AM335_I2C_WRITE(Am3I2cInterruptStatus, AM335_I2C_INTERRUPT_STATUS_MASK);
  178. Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_ENABLE;
  179. AM335_I2C_WRITE(Am3I2cControl, Value);
  180. Value |= AM335_I2C_CONTROL_START;
  181. AM335_I2C_WRITE(Am3I2cControl, Value);
  182. do {
  183. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  184. } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
  185. //
  186. // Loop reading the data bytes.
  187. //
  188. while (Size != 0) {
  189. do {
  190. Value = AM335_I2C_READ(Am3I2cBufferStatus);
  191. Value = (Value & AM335_I2C_BUFFER_STATUS_RX_MASK) >>
  192. AM335_I2C_BUFFER_STATUS_RX_SHIFT;
  193. } while (Value == 0);
  194. *Data = AM335_I2C_READ(Am3I2cData);
  195. Data += 1;
  196. Size -= 1;
  197. }
  198. //
  199. // Make it stop.
  200. //
  201. Value = AM335_I2C_READ(Am3I2cControl);
  202. Value |= AM335_I2C_CONTROL_STOP;
  203. AM335_I2C_WRITE(Am3I2cControl, Value);
  204. do {
  205. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  206. } while ((Value & AM335_I2C_INTERRUPT_BUS_FREE) == 0);
  207. AM335_I2C_WRITE(Am3I2cInterruptStatus,
  208. AM335_I2C_INTERRUPT_BUS_FREE);
  209. return;
  210. }
  211. VOID
  212. EfipAm335I2c0Write (
  213. UINT32 Register,
  214. UINT32 Size,
  215. UINT8 *Data
  216. )
  217. /*++
  218. Routine Description:
  219. This routine performs a write to the I2C bus. This routine assumes the
  220. slave address has already been set.
  221. Arguments:
  222. Register - Supplies the register to write to. Supply -1 to skip transmitting
  223. a register number.
  224. Size - Supplies the number of data bytes to write (not including the
  225. register byte itself).
  226. Data - Supplies a pointer to the data to write.
  227. Return Value:
  228. None.
  229. --*/
  230. {
  231. UINT32 Index;
  232. UINT32 Value;
  233. //
  234. // Transmit the register number. Set the size to the register plus the
  235. // data, clear all interrupts, start the transfer, write the byte, clear
  236. // the transmit ready interrupt, and wait for the ready bit.
  237. //
  238. if (Register <= MAX_UINT8) {
  239. Size += 1;
  240. }
  241. AM335_I2C_WRITE(Am3I2cCount, Size);
  242. AM335_I2C_WRITE(Am3I2cInterruptStatus, AM335_I2C_INTERRUPT_STATUS_MASK);
  243. Value = AM335_I2C_CONTROL_MASTER | AM335_I2C_CONTROL_TRANSMIT |
  244. AM335_I2C_CONTROL_ENABLE;
  245. AM335_I2C_WRITE(Am3I2cControl, Value);
  246. Value |= AM335_I2C_CONTROL_START;
  247. AM335_I2C_WRITE(Am3I2cControl, Value);
  248. do {
  249. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  250. } while ((Value & AM335_I2C_INTERRUPT_BUS_BUSY) == 0);
  251. Index = 0;
  252. while (Index < Size) {
  253. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  254. if ((Value & AM335_I2C_INTERRUPT_TX_READY) == 0) {
  255. break;
  256. }
  257. //
  258. // The first time around write the register number, all the others
  259. // write the data.
  260. //
  261. if (Register <= MAX_UINT8) {
  262. if (Index == 0) {
  263. Value = Register;
  264. } else {
  265. Value = Data[Index - 1];
  266. }
  267. //
  268. // No register, just transmit the data.
  269. //
  270. } else {
  271. Value = Data[Index];
  272. }
  273. AM335_I2C_WRITE(Am3I2cData, Value);
  274. AM335_I2C_WRITE(Am3I2cInterruptStatus,
  275. AM335_I2C_INTERRUPT_TX_READY);
  276. Index += 1;
  277. }
  278. //
  279. // Make it stop.
  280. //
  281. Value = AM335_I2C_READ(Am3I2cControl);
  282. Value |= AM335_I2C_CONTROL_STOP;
  283. AM335_I2C_WRITE(Am3I2cControl, Value);
  284. do {
  285. Value = AM335_I2C_READ(Am3I2cInterruptStatusRaw);
  286. } while ((Value & AM335_I2C_INTERRUPT_BUS_FREE) == 0);
  287. AM335_I2C_WRITE(Am3I2cInterruptStatus,
  288. AM335_I2C_INTERRUPT_BUS_FREE);
  289. return;
  290. }
  291. //
  292. // --------------------------------------------------------- Internal Functions
  293. //
  294. VOID
  295. EfipAm335I2cConfigureBusClock (
  296. VOID
  297. )
  298. /*++
  299. Routine Description:
  300. This routine initializes the bus clock of the I2C module.
  301. Arguments:
  302. None.
  303. Return Value:
  304. None.
  305. --*/
  306. {
  307. UINT32 Divider;
  308. UINT32 Prescaler;
  309. Prescaler = (AM335_I2C_SYSTEM_CLOCK_SPEED /
  310. AM335_I2C_INTERNAL_CLOCK_SPEED) - 1;
  311. AM335_I2C_WRITE(Am3I2cPrescale, Prescaler);
  312. Divider = (AM335_I2C_INTERNAL_CLOCK_SPEED /
  313. AM335_I2C_OUTPUT_CLOCK_SPEED) / 2;
  314. AM335_I2C_WRITE(Am3I2cSclLowTime, Divider - 7);
  315. AM335_I2C_WRITE(Am3I2cSclHighTime, Divider - 5);
  316. return;
  317. }