imx_i2c.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2010-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*!
  31. * @file imx_i2c.c
  32. * @brief Main driver for the I2C controller. It initializes the controller
  33. * and handles the master mode.
  34. *
  35. * @ingroup diag_i2c
  36. */
  37. #include "sdk.h"
  38. #include "imx_i2c.h"
  39. #include "imx_i2c_internal.h"
  40. #include "registers/regsi2c.h"
  41. #include "ccm_pll.h"
  42. #include "interrupt.h"
  43. //! Set this macro to 1 to enable tracing of data send and receive.
  44. #define TRACE_I2C 0
  45. //! @brief Get the irq id of I2C by instance number.
  46. //! @param x I2C instance number, from 1 through 3.
  47. #define I2C_IRQS(x) ( (x) == HW_I2C1 ? IMX_INT_I2C1 : (x) == HW_I2C2 ? IMX_INT_I2C2 : (x) == HW_I2C3 ? IMX_INT_I2C3 : 0xFFFFFFFF)
  48. ////////////////////////////////////////////////////////////////////////////////
  49. // Constants
  50. ////////////////////////////////////////////////////////////////////////////////
  51. static const uint16_t i2c_freq_div[50][2] = {
  52. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  53. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  54. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  55. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  56. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  57. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  58. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  59. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  60. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  61. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  62. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  63. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  64. { 3072, 0x1E }, { 3840, 0x1F }
  65. };
  66. ////////////////////////////////////////////////////////////////////////////////
  67. // Prototypes
  68. ////////////////////////////////////////////////////////////////////////////////
  69. static inline int is_bus_free(unsigned int instance);
  70. static int wait_till_busy(uint32_t instance);
  71. static inline void imx_send_stop(unsigned int instance);
  72. static int wait_op_done(uint32_t instance, int is_tx);
  73. static int tx_byte(uint8_t * data, uint32_t instance);
  74. static int rx_bytes(uint8_t * data, uint32_t instance, int sz);
  75. static void set_i2c_clock(uint32_t instance, uint32_t baud);
  76. ////////////////////////////////////////////////////////////////////////////////
  77. // Code
  78. ////////////////////////////////////////////////////////////////////////////////
  79. unsigned i2c_get_request_instance(const imx_i2c_request_t * rq)
  80. {
  81. // First see if there device info is set.
  82. if (rq->device)
  83. {
  84. // Use the instance number in the device info.
  85. return rq->device->port;
  86. }
  87. // Check if the ctl_addr is within the range of instances.
  88. if (rq->ctl_addr >= 1 && rq->ctl_addr <= HW_I2C_INSTANCE_COUNT)
  89. {
  90. // Valid instance number, so use it directly.
  91. return rq->ctl_addr;
  92. }
  93. else
  94. {
  95. // Not a valid instance, so treat it as a base address.
  96. return REGS_I2C_INSTANCE(rq->ctl_addr);
  97. }
  98. }
  99. /*!
  100. * @brief Loop status register for IBB to go 0.
  101. *
  102. * The loop also breaks on max number of iterations.
  103. *
  104. * @param instance Instance number of the I2C module.
  105. *
  106. * @return 0 if successful; -1 otherwise
  107. */
  108. static inline int is_bus_free(unsigned int instance)
  109. {
  110. int i = WAIT_RXAK_LOOPS;
  111. while (HW_I2C_I2SR(instance).B.IBB && (--i > 0));
  112. if (i <= 0) {
  113. debug_printf("Error: I2C Bus not free!\n");
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. /*!
  119. * @brief Loop status register for IBB to go 1.
  120. *
  121. * It breaks the loop if there's an arbitration lost occurred or the maximum
  122. * number of iterations has been reached.
  123. *
  124. * @param instance Instance number of the I2C module.
  125. *
  126. * @return 0 if successful; -1 otherwise
  127. */
  128. static int wait_till_busy(uint32_t instance)
  129. {
  130. int i = WAIT_BUSY_LOOPS;
  131. while (!HW_I2C_I2SR(instance).B.IBB && (--i > 0)) {
  132. if (HW_I2C_I2SR(instance).B.IAL) {
  133. debug_printf("Error: arbitration lost!\n");
  134. return -1;
  135. }
  136. }
  137. if (i <= 0) {
  138. debug_printf("I2C Error: timeout in %s; %d\n", __FUNCTION__, __LINE__);
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. /*!
  144. * Generates a STOP signal, called by rx and tx routines
  145. *
  146. * @param instance Instance number of the I2C module.
  147. *
  148. * @return none
  149. */
  150. static inline void imx_send_stop(unsigned int instance)
  151. {
  152. HW_I2C_I2CR(instance).B.MSTA = 0;
  153. }
  154. /*!
  155. * @brief Wait for operation done.
  156. *
  157. * This function loops until we get an interrupt. On timeout it returns -1.
  158. * It reports arbitration lost if IAL bit of I2SR register is set
  159. * Clears the interrupt
  160. * If operation is transfer byte function will make sure we received an ack
  161. *
  162. * @param instance Instance number of the I2C module.
  163. * @param is_tx Pass 1 for transfering, 0 for receiving
  164. *
  165. * @return 0 if successful; negative integer otherwise
  166. */
  167. static int wait_op_done(uint32_t instance, int is_tx)
  168. {
  169. hw_i2c_i2sr_t v;
  170. int i = WAIT_RXAK_LOOPS;
  171. // Loop until we get an interrupt
  172. do {
  173. v.U = HW_I2C_I2SR_RD(instance);
  174. } while (!v.B.IIF && (--i > 0));
  175. // If timeout error occurred return error
  176. if (i <= 0) {
  177. debug_printf("I2C Error: timeout unexpected\n");
  178. return -1;
  179. }
  180. // Clear the interrupts
  181. HW_I2C_I2SR_WR(instance, 0);
  182. // Check for arbitration lost
  183. if (v.B.IAL) {
  184. debug_printf("Error %d: Arbitration lost\n", __LINE__);
  185. return ERR_ARB_LOST;
  186. }
  187. // Check for ACK received in transmit mode
  188. if (is_tx) {
  189. if (v.B.RXAK) {
  190. // No ACK received, generate STOP by clearing MSTA bit
  191. debug_printf("Error %d: no ack received\n", __LINE__);
  192. // Generate a STOP signal
  193. imx_send_stop(instance);
  194. return ERR_NO_ACK;
  195. }
  196. }
  197. return 0;
  198. }
  199. /*!
  200. * @brief Implements a loop to send a byte to I2C slave.
  201. *
  202. * For master transmit. Always expect a RXAK signal to be set!
  203. *
  204. * @param data return buffer for data
  205. * @param instance Instance number of the I2C module.
  206. *
  207. * @return 0 if successful; -1 otherwise
  208. */
  209. static int tx_byte(uint8_t * data, uint32_t instance)
  210. {
  211. #if TRACE_I2C
  212. debug_printf("%s(data=0x%02x, instance=%d)\n", __FUNCTION__, *data, instance);
  213. #endif // TRACE_I2C
  214. // clear both IAL and IIF bits
  215. HW_I2C_I2SR_WR(instance, 0);
  216. // write to data register
  217. HW_I2C_I2DR_WR(instance, *data);
  218. // wait for transfer of byte to complete
  219. return wait_op_done(instance, 1);
  220. }
  221. /*!
  222. * @brief Implements a loop to receive bytes from I2C slave.
  223. *
  224. * For master receive.
  225. *
  226. * @param data return buffer for data
  227. * @param instance Instance number of the I2C module.
  228. * @param sz number of bytes to receive
  229. *
  230. * @return 0 if successful; -1 otherwise
  231. */
  232. static int rx_bytes(uint8_t * data, uint32_t instance, int sz)
  233. {
  234. int i;
  235. for (i = 0; sz > 0; sz--, i++) {
  236. if (wait_op_done(instance, 0) != 0) {
  237. return -1;
  238. }
  239. // the next two if-statements setup for the next read control register value
  240. if (sz == 1) {
  241. // last byte --> generate STOP
  242. // generate STOP by clearing MSTA bit
  243. imx_send_stop(instance);
  244. }
  245. if (sz == 2) {
  246. // 2nd last byte --> set TXAK bit to NOT generate ACK
  247. HW_I2C_I2CR(instance).B.TXAK = 1;
  248. }
  249. // read the true data
  250. data[i] = HW_I2C_I2DR_RD(instance);
  251. #if TRACE_I2C
  252. debug_printf("OK 0x%02x\n", data[i]);
  253. #endif // TRACE_I2C
  254. }
  255. return 0;
  256. }
  257. static void set_i2c_clock(uint32_t instance, uint32_t baud)
  258. {
  259. // Adjust the divider to get the closest SCL frequency to baud rate
  260. uint32_t src_clk = get_main_clock(IPG_PER_CLK);
  261. uint32_t divider = src_clk / baud;
  262. uint8_t index = 0;
  263. if (divider < i2c_freq_div[0][0])
  264. {
  265. divider = i2c_freq_div[0][0];
  266. index = 0;
  267. debug_printf("Warning :can't find a smaller divider than %d.\n", divider);
  268. debug_printf("SCL frequency is set at %d - expected was %d.\n", src_clk/divider, baud);
  269. }
  270. else if (divider > i2c_freq_div[49][0])
  271. {
  272. divider = i2c_freq_div[49][0];
  273. index = 49;
  274. debug_printf("Warning: can't find a bigger divider than %d.\n", divider);
  275. debug_printf("SCL frequency is set at %d - expected was %d.\n", src_clk/divider, baud);
  276. }
  277. else
  278. {
  279. for (index = 0; i2c_freq_div[index][0] < divider; index++);
  280. divider = i2c_freq_div[index][0];
  281. }
  282. HW_I2C_IFDR_WR(instance, BF_I2C_IFDR_IC(i2c_freq_div[index][1]));
  283. }
  284. int i2c_xfer(const imx_i2c_request_t *rq, int dir)
  285. {
  286. uint32_t reg;
  287. uint32_t ret = 0;
  288. uint16_t i2cr;
  289. uint8_t i;
  290. uint8_t data;
  291. uint32_t instance = i2c_get_request_instance(rq);
  292. uint8_t address = (rq->device ? rq->device->address : rq->dev_addr) << 1;
  293. if (rq->buffer_sz == 0 || rq->buffer == NULL) {
  294. debug_printf("Invalid register address size=%x, buffer size=%x, buffer=%x\n",
  295. rq->reg_addr_sz, rq->buffer_sz, (unsigned int)rq->buffer);
  296. return ERR_INVALID_REQUEST;
  297. }
  298. // clear the status register
  299. HW_I2C_I2SR_WR(instance, 0);
  300. // enable the I2C controller
  301. HW_I2C_I2CR_WR(instance, BM_I2C_I2CR_IEN);
  302. // Check if bus is free, if not return error
  303. if (is_bus_free(instance) != 0) {
  304. return -1;
  305. }
  306. // If the request has device info attached and it has a non-zero bit rate, then
  307. // change the clock to the specified rate.
  308. if (rq->device && rq->device->freq)
  309. {
  310. set_i2c_clock(instance, rq->device->freq);
  311. }
  312. // Step 1: Select master mode, assert START signal and also indicate TX mode
  313. HW_I2C_I2CR_WR(instance, BM_I2C_I2CR_IEN | BM_I2C_I2CR_MSTA | BM_I2C_I2CR_MTX);
  314. // make sure bus is busy after the START signal
  315. if (wait_till_busy(instance) != 0) {
  316. debug_printf("1\n");
  317. return -1;
  318. }
  319. // Step 2: send slave address + read/write at the LSB
  320. data = address | I2C_WRITE;
  321. if ((ret = tx_byte(&data, instance)) != 0) {
  322. debug_printf("START TX ERR %d\n", ret);
  323. if (ret == ERR_NO_ACK) {
  324. return ERR_NO_ACK_ON_START;
  325. } else {
  326. return ret;
  327. }
  328. }
  329. // Step 3: send I2C device register address
  330. if (rq->reg_addr_sz > 4) {
  331. debug_printf("Warning register address size %d should less than 4\n", rq->reg_addr_sz);
  332. return ERR_INVALID_REQUEST;
  333. }
  334. reg = rq->reg_addr;
  335. for (i = 0; i < rq->reg_addr_sz; i++, reg >>= 8) {
  336. data = reg & 0xFF;
  337. #if TRACE_I2C
  338. debug_printf("sending I2C=%d device register: data=0x%x, byte %d\n", instance, data, i);
  339. #endif // TRACE_I2C
  340. if (tx_byte(&data, instance) != 0) {
  341. return -1;
  342. }
  343. }
  344. // Step 4: read/write data
  345. if (dir == I2C_READ) {
  346. // do repeat-start
  347. HW_I2C_I2CR(instance).B.RSTA = 1;
  348. // make sure bus is busy after the REPEATED START signal
  349. if (wait_till_busy(instance) != 0) {
  350. return ERR_TX;
  351. }
  352. // send slave address again, but indicate read operation
  353. data = address | I2C_READ;
  354. if (tx_byte(&data, instance) != 0) {
  355. return -1;
  356. }
  357. // change to receive mode
  358. i2cr = HW_I2C_I2CR_RD(instance) & ~BM_I2C_I2CR_MTX;
  359. // if only one byte to read, make sure don't send ack
  360. if (rq->buffer_sz == 1) {
  361. i2cr |= BM_I2C_I2CR_TXAK;
  362. }
  363. HW_I2C_I2CR_WR(instance, i2cr);
  364. // dummy read
  365. data = HW_I2C_I2DR_RD(instance);
  366. // now reading ...
  367. if (rx_bytes(rq->buffer, instance, rq->buffer_sz) != 0) {
  368. return -1;
  369. }
  370. } else {
  371. // I2C_WRITE
  372. for (i = 0; i < rq->buffer_sz; i++) {
  373. // send device register value
  374. data = rq->buffer[i];
  375. if ((ret = tx_byte(&data, instance)) != 0) {
  376. break;
  377. }
  378. }
  379. }
  380. // generate STOP by clearing MSTA bit
  381. imx_send_stop(instance);
  382. // Check if bus is free, if not return error
  383. if (is_bus_free(instance) != 0) {
  384. debug_printf("WARNING: bus is not free\n");
  385. }
  386. // disable the controller
  387. HW_I2C_I2CR_WR(instance, 0);
  388. return ret;
  389. }
  390. int i2c_read(const imx_i2c_request_t *rq)
  391. {
  392. return i2c_xfer(rq, I2C_READ);
  393. }
  394. int i2c_write(const imx_i2c_request_t *rq)
  395. {
  396. return i2c_xfer(rq, I2C_WRITE);
  397. }
  398. void i2c_setup_interrupt(uint32_t instance, void (*irq_subroutine)(void), bool state)
  399. {
  400. uint32_t irq_id = I2C_IRQS(instance);
  401. if (state) {
  402. // register the IRQ sub-routine
  403. register_interrupt_routine(irq_id, irq_subroutine);
  404. // enable the IRQ at the ARM core level
  405. enable_interrupt(irq_id, CPU_0, 0);
  406. // clear the status register
  407. HW_I2C_I2SR_WR(instance, 0);
  408. // and enable the interrupts in the I2C controller
  409. HW_I2C_I2CR(instance).B.IIEN = 1;
  410. } else {
  411. // disable the IRQ at the ARM core level
  412. disable_interrupt(irq_id, CPU_0);
  413. // and disable the interrupts in the I2C controller
  414. HW_I2C_I2CR(instance).B.IIEN = 0;
  415. // clear the status register
  416. HW_I2C_I2SR_WR(instance, 0);
  417. }
  418. }
  419. int i2c_init(uint32_t base, uint32_t baud)
  420. {
  421. int instance;
  422. // Accept either an instance or base address for the base param.
  423. if (base >= 1 && base <= HW_I2C_INSTANCE_COUNT)
  424. {
  425. instance = base;
  426. }
  427. else
  428. {
  429. instance = REGS_I2C_INSTANCE(base);
  430. }
  431. // enable the source clocks to the I2C port
  432. clock_gating_config(REGS_I2C_BASE(instance), CLOCK_ON);
  433. // Set iomux configuration
  434. i2c_iomux_config(instance);
  435. // reset I2C
  436. HW_I2C_I2CR_WR(instance, 0);
  437. // Set clock.
  438. set_i2c_clock(instance, baud);
  439. // set an I2C slave address
  440. HW_I2C_IADR_WR(instance, IMX6_DEFAULT_SLAVE_ID);
  441. // clear the status register
  442. HW_I2C_I2SR_WR(instance, 0);
  443. // enable the I2C controller
  444. HW_I2C_I2CR_WR(instance, BM_I2C_I2CR_IEN);
  445. return 0;
  446. }
  447. ////////////////////////////////////////////////////////////////////////////////
  448. // EOF
  449. ////////////////////////////////////////////////////////////////////////////////