mi2cv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. * Copyright (C) 2018 Icenowy Zheng <icenowy@aosc.io>
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. * https://spdx.org/licenses
  7. */
  8. /*
  9. * This driver is for Mentor Graphics Inventra MI2CV IP core, which is used
  10. * for Marvell and Allwinner SoCs in ATF.
  11. */
  12. #include <errno.h>
  13. #include <common/debug.h>
  14. #include <drivers/delay_timer.h>
  15. #include <drivers/mentor/mi2cv.h>
  16. #include <lib/mmio.h>
  17. #include <mentor_i2c_plat.h>
  18. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  19. #define DEBUG_I2C
  20. #endif
  21. #define I2C_TIMEOUT_VALUE 0x500
  22. #define I2C_MAX_RETRY_CNT 1000
  23. #define I2C_CMD_WRITE 0x0
  24. #define I2C_CMD_READ 0x1
  25. #define I2C_DATA_ADDR_7BIT_OFFS 0x1
  26. #define I2C_DATA_ADDR_7BIT_MASK (0xFF << I2C_DATA_ADDR_7BIT_OFFS)
  27. #define I2C_CONTROL_ACK 0x00000004
  28. #define I2C_CONTROL_IFLG 0x00000008
  29. #define I2C_CONTROL_STOP 0x00000010
  30. #define I2C_CONTROL_START 0x00000020
  31. #define I2C_CONTROL_TWSIEN 0x00000040
  32. #define I2C_CONTROL_INTEN 0x00000080
  33. #define I2C_STATUS_START 0x08
  34. #define I2C_STATUS_REPEATED_START 0x10
  35. #define I2C_STATUS_ADDR_W_ACK 0x18
  36. #define I2C_STATUS_DATA_W_ACK 0x28
  37. #define I2C_STATUS_LOST_ARB_DATA_ADDR_TRANSFER 0x38
  38. #define I2C_STATUS_ADDR_R_ACK 0x40
  39. #define I2C_STATUS_DATA_R_ACK 0x50
  40. #define I2C_STATUS_DATA_R_NAK 0x58
  41. #define I2C_STATUS_LOST_ARB_GENERAL_CALL 0x78
  42. #define I2C_STATUS_IDLE 0xF8
  43. #define I2C_UNSTUCK_TRIGGER 0x1
  44. #define I2C_UNSTUCK_ONGOING 0x2
  45. #define I2C_UNSTUCK_ERROR 0x4
  46. static struct mentor_i2c_regs *base;
  47. static int mentor_i2c_lost_arbitration(uint32_t *status)
  48. {
  49. *status = mmio_read_32((uintptr_t)&base->status);
  50. if ((*status == I2C_STATUS_LOST_ARB_DATA_ADDR_TRANSFER) ||
  51. (*status == I2C_STATUS_LOST_ARB_GENERAL_CALL))
  52. return -EAGAIN;
  53. return 0;
  54. }
  55. static void mentor_i2c_interrupt_clear(void)
  56. {
  57. uint32_t reg;
  58. reg = mmio_read_32((uintptr_t)&base->control);
  59. #ifndef I2C_INTERRUPT_CLEAR_INVERTED
  60. reg &= ~(I2C_CONTROL_IFLG);
  61. #else
  62. reg |= I2C_CONTROL_IFLG;
  63. #endif
  64. mmio_write_32((uintptr_t)&base->control, reg);
  65. /* Wait for 1 us for the clear to take effect */
  66. udelay(1);
  67. }
  68. static bool mentor_i2c_interrupt_get(void)
  69. {
  70. uint32_t reg;
  71. /* get the interrupt flag bit */
  72. reg = mmio_read_32((uintptr_t)&base->control);
  73. reg &= I2C_CONTROL_IFLG;
  74. return (reg != 0U);
  75. }
  76. static int mentor_i2c_wait_interrupt(void)
  77. {
  78. uint32_t timeout = 0;
  79. while (!mentor_i2c_interrupt_get() && (timeout++ < I2C_TIMEOUT_VALUE))
  80. ;
  81. if (timeout >= I2C_TIMEOUT_VALUE)
  82. return -ETIMEDOUT;
  83. return 0;
  84. }
  85. static int mentor_i2c_start_bit_set(void)
  86. {
  87. int is_int_flag = 0;
  88. uint32_t status;
  89. if (mentor_i2c_interrupt_get())
  90. is_int_flag = 1;
  91. /* set start bit */
  92. mmio_write_32((uintptr_t)&base->control,
  93. mmio_read_32((uintptr_t)&base->control) |
  94. I2C_CONTROL_START);
  95. /* in case that the int flag was set before i.e. repeated start bit */
  96. if (is_int_flag) {
  97. VERBOSE("%s: repeated start Bit\n", __func__);
  98. mentor_i2c_interrupt_clear();
  99. }
  100. if (mentor_i2c_wait_interrupt()) {
  101. ERROR("Start clear bit timeout\n");
  102. return -ETIMEDOUT;
  103. }
  104. /* check that start bit went down */
  105. if ((mmio_read_32((uintptr_t)&base->control) &
  106. I2C_CONTROL_START) != 0) {
  107. ERROR("Start bit didn't went down\n");
  108. return -EPERM;
  109. }
  110. /* check the status */
  111. if (mentor_i2c_lost_arbitration(&status)) {
  112. ERROR("%s - %d: Lost arbitration, got status %x\n",
  113. __func__, __LINE__, status);
  114. return -EAGAIN;
  115. }
  116. if ((status != I2C_STATUS_START) &&
  117. (status != I2C_STATUS_REPEATED_START)) {
  118. ERROR("Got status %x after enable start bit.\n", status);
  119. return -EPERM;
  120. }
  121. return 0;
  122. }
  123. static int mentor_i2c_stop_bit_set(void)
  124. {
  125. int timeout;
  126. uint32_t status;
  127. /* Generate stop bit */
  128. mmio_write_32((uintptr_t)&base->control,
  129. mmio_read_32((uintptr_t)&base->control) |
  130. I2C_CONTROL_STOP);
  131. mentor_i2c_interrupt_clear();
  132. timeout = 0;
  133. /* Read control register, check the control stop bit */
  134. while ((mmio_read_32((uintptr_t)&base->control) & I2C_CONTROL_STOP) &&
  135. (timeout++ < I2C_TIMEOUT_VALUE))
  136. ;
  137. if (timeout >= I2C_TIMEOUT_VALUE) {
  138. ERROR("Stop bit didn't went down\n");
  139. return -ETIMEDOUT;
  140. }
  141. /* check that stop bit went down */
  142. if ((mmio_read_32((uintptr_t)&base->control) & I2C_CONTROL_STOP) != 0) {
  143. ERROR("Stop bit didn't went down\n");
  144. return -EPERM;
  145. }
  146. /* check the status */
  147. if (mentor_i2c_lost_arbitration(&status)) {
  148. ERROR("%s - %d: Lost arbitration, got status %x\n",
  149. __func__, __LINE__, status);
  150. return -EAGAIN;
  151. }
  152. if (status != I2C_STATUS_IDLE) {
  153. ERROR("Got status %x after enable stop bit.\n", status);
  154. return -EPERM;
  155. }
  156. return 0;
  157. }
  158. static int mentor_i2c_address_set(uint8_t chain, int command)
  159. {
  160. uint32_t reg, status;
  161. reg = (chain << I2C_DATA_ADDR_7BIT_OFFS) & I2C_DATA_ADDR_7BIT_MASK;
  162. reg |= command;
  163. mmio_write_32((uintptr_t)&base->data, reg);
  164. udelay(1);
  165. mentor_i2c_interrupt_clear();
  166. if (mentor_i2c_wait_interrupt()) {
  167. ERROR("Start clear bit timeout\n");
  168. return -ETIMEDOUT;
  169. }
  170. /* check the status */
  171. if (mentor_i2c_lost_arbitration(&status)) {
  172. ERROR("%s - %d: Lost arbitration, got status %x\n",
  173. __func__, __LINE__, status);
  174. return -EAGAIN;
  175. }
  176. if (((status != I2C_STATUS_ADDR_R_ACK) && (command == I2C_CMD_READ)) ||
  177. ((status != I2C_STATUS_ADDR_W_ACK) && (command == I2C_CMD_WRITE))) {
  178. /* only in debug, since in boot we try to read the SPD
  179. * of both DRAM, and we don't want error messages in cas
  180. * DIMM doesn't exist.
  181. */
  182. INFO("%s: ERROR - status %x addr in %s mode.\n", __func__,
  183. status, (command == I2C_CMD_WRITE) ? "Write" : "Read");
  184. return -EPERM;
  185. }
  186. return 0;
  187. }
  188. /*
  189. * The I2C module contains a clock divider to generate the SCL clock.
  190. * This function calculates and sets the <N> and <M> fields in the I2C Baud
  191. * Rate Register (t=01) to obtain given 'requested_speed'.
  192. * The requested_speed will be equal to:
  193. * CONFIG_SYS_TCLK / (10 * (M + 1) * (2 << N))
  194. * Where M is the value represented by bits[6:3] and N is the value represented
  195. * by bits[2:0] of "I2C Baud Rate Register".
  196. * Therefore max M which can be set is 16 (2^4) and max N is 8 (2^3). So the
  197. * lowest possible baudrate is:
  198. * CONFIG_SYS_TCLK/(10 * (16 +1) * (2 << 8), which equals to:
  199. * CONFIG_SYS_TCLK/87040. Assuming that CONFIG_SYS_TCLK=250MHz, the lowest
  200. * possible frequency is ~2,872KHz.
  201. */
  202. static unsigned int mentor_i2c_bus_speed_set(unsigned int requested_speed)
  203. {
  204. unsigned int n, m, freq, margin, min_margin = 0xffffffff;
  205. unsigned int actual_n = 0, actual_m = 0;
  206. int val;
  207. /* Calculate N and M for the TWSI clock baud rate */
  208. for (n = 0; n < 8; n++) {
  209. for (m = 0; m < 16; m++) {
  210. freq = CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
  211. val = requested_speed - freq;
  212. margin = (val > 0) ? val : -val;
  213. if ((freq <= requested_speed) &&
  214. (margin < min_margin)) {
  215. min_margin = margin;
  216. actual_n = n;
  217. actual_m = m;
  218. }
  219. }
  220. }
  221. VERBOSE("%s: actual_n = %u, actual_m = %u\n",
  222. __func__, actual_n, actual_m);
  223. /* Set the baud rate */
  224. mmio_write_32((uintptr_t)&base->baudrate, (actual_m << 3) | actual_n);
  225. return 0;
  226. }
  227. #ifdef DEBUG_I2C
  228. static int mentor_i2c_probe(uint8_t chip)
  229. {
  230. int ret = 0;
  231. ret = mentor_i2c_start_bit_set();
  232. if (ret != 0) {
  233. mentor_i2c_stop_bit_set();
  234. ERROR("%s - %d: %s", __func__, __LINE__,
  235. "mentor_i2c_start_bit_set failed\n");
  236. return -EPERM;
  237. }
  238. ret = mentor_i2c_address_set(chip, I2C_CMD_WRITE);
  239. if (ret != 0) {
  240. mentor_i2c_stop_bit_set();
  241. ERROR("%s - %d: %s", __func__, __LINE__,
  242. "mentor_i2c_address_set failed\n");
  243. return -EPERM;
  244. }
  245. mentor_i2c_stop_bit_set();
  246. VERBOSE("%s: successful I2C probe\n", __func__);
  247. return ret;
  248. }
  249. #endif
  250. /* regular i2c transaction */
  251. static int mentor_i2c_data_receive(uint8_t *p_block, uint32_t block_size)
  252. {
  253. uint32_t reg, status, block_size_read = block_size;
  254. /* Wait for cause interrupt */
  255. if (mentor_i2c_wait_interrupt()) {
  256. ERROR("Start clear bit timeout\n");
  257. return -ETIMEDOUT;
  258. }
  259. while (block_size_read) {
  260. if (block_size_read == 1) {
  261. reg = mmio_read_32((uintptr_t)&base->control);
  262. reg &= ~(I2C_CONTROL_ACK);
  263. mmio_write_32((uintptr_t)&base->control, reg);
  264. }
  265. mentor_i2c_interrupt_clear();
  266. if (mentor_i2c_wait_interrupt()) {
  267. ERROR("Start clear bit timeout\n");
  268. return -ETIMEDOUT;
  269. }
  270. /* check the status */
  271. if (mentor_i2c_lost_arbitration(&status)) {
  272. ERROR("%s - %d: Lost arbitration, got status %x\n",
  273. __func__, __LINE__, status);
  274. return -EAGAIN;
  275. }
  276. if ((status != I2C_STATUS_DATA_R_ACK) &&
  277. (block_size_read != 1)) {
  278. ERROR("Status %x in read transaction\n", status);
  279. return -EPERM;
  280. }
  281. if ((status != I2C_STATUS_DATA_R_NAK) &&
  282. (block_size_read == 1)) {
  283. ERROR("Status %x in Rd Terminate\n", status);
  284. return -EPERM;
  285. }
  286. /* read the data */
  287. *p_block = (uint8_t) mmio_read_32((uintptr_t)&base->data);
  288. VERBOSE("%s: place %d read %x\n", __func__,
  289. block_size - block_size_read, *p_block);
  290. p_block++;
  291. block_size_read--;
  292. }
  293. return 0;
  294. }
  295. static int mentor_i2c_data_transmit(uint8_t *p_block, uint32_t block_size)
  296. {
  297. uint32_t status, block_size_write = block_size;
  298. if (mentor_i2c_wait_interrupt()) {
  299. ERROR("Start clear bit timeout\n");
  300. return -ETIMEDOUT;
  301. }
  302. while (block_size_write) {
  303. /* write the data */
  304. mmio_write_32((uintptr_t)&base->data, (uint32_t) *p_block);
  305. VERBOSE("%s: index = %d, data = %x\n", __func__,
  306. block_size - block_size_write, *p_block);
  307. p_block++;
  308. block_size_write--;
  309. mentor_i2c_interrupt_clear();
  310. if (mentor_i2c_wait_interrupt()) {
  311. ERROR("Start clear bit timeout\n");
  312. return -ETIMEDOUT;
  313. }
  314. /* check the status */
  315. if (mentor_i2c_lost_arbitration(&status)) {
  316. ERROR("%s - %d: Lost arbitration, got status %x\n",
  317. __func__, __LINE__, status);
  318. return -EAGAIN;
  319. }
  320. if (status != I2C_STATUS_DATA_W_ACK) {
  321. ERROR("Status %x in write transaction\n", status);
  322. return -EPERM;
  323. }
  324. }
  325. return 0;
  326. }
  327. static int mentor_i2c_target_offset_set(uint8_t chip, uint32_t addr, int alen)
  328. {
  329. uint8_t off_block[2];
  330. uint32_t off_size;
  331. if (alen == 2) { /* 2-byte addresses support */
  332. off_block[0] = (addr >> 8) & 0xff;
  333. off_block[1] = addr & 0xff;
  334. off_size = 2;
  335. } else { /* 1-byte addresses support */
  336. off_block[0] = addr & 0xff;
  337. off_size = 1;
  338. }
  339. VERBOSE("%s: off_size = %x addr1 = %x addr2 = %x\n", __func__,
  340. off_size, off_block[0], off_block[1]);
  341. return mentor_i2c_data_transmit(off_block, off_size);
  342. }
  343. #ifdef I2C_CAN_UNSTUCK
  344. static int mentor_i2c_unstuck(int ret)
  345. {
  346. uint32_t v;
  347. if (ret != -ETIMEDOUT)
  348. return ret;
  349. VERBOSE("Trying to \"unstuck i2c\"... ");
  350. i2c_init(base);
  351. mmio_write_32((uintptr_t)&base->unstuck, I2C_UNSTUCK_TRIGGER);
  352. do {
  353. v = mmio_read_32((uintptr_t)&base->unstuck);
  354. } while (v & I2C_UNSTUCK_ONGOING);
  355. if (v & I2C_UNSTUCK_ERROR) {
  356. VERBOSE("failed - soft reset i2c\n");
  357. ret = -EPERM;
  358. } else {
  359. VERBOSE("ok\n");
  360. i2c_init(base);
  361. ret = -EAGAIN;
  362. }
  363. return ret;
  364. }
  365. #else
  366. static int mentor_i2c_unstuck(int ret)
  367. {
  368. VERBOSE("Cannot \"unstuck i2c\" - soft reset i2c\n");
  369. return -EPERM;
  370. }
  371. #endif
  372. /*
  373. * API Functions
  374. */
  375. void i2c_init(void *i2c_base)
  376. {
  377. /* For I2C speed and slave address, now we do not set them since
  378. * we just provide the working speed and slave address otherwhere
  379. * for i2c_init
  380. */
  381. base = (struct mentor_i2c_regs *)i2c_base;
  382. /* Reset the I2C logic */
  383. mmio_write_32((uintptr_t)&base->soft_reset, 0);
  384. udelay(200);
  385. mentor_i2c_bus_speed_set(CONFIG_SYS_I2C_SPEED);
  386. /* Enable the I2C and slave */
  387. mmio_write_32((uintptr_t)&base->control,
  388. I2C_CONTROL_TWSIEN | I2C_CONTROL_ACK);
  389. /* set the I2C slave address */
  390. mmio_write_32((uintptr_t)&base->xtnd_slave_addr, 0);
  391. mmio_write_32((uintptr_t)&base->slave_address, CONFIG_SYS_I2C_SLAVE);
  392. /* unmask I2C interrupt */
  393. mmio_write_32((uintptr_t)&base->control,
  394. mmio_read_32((uintptr_t)&base->control) |
  395. I2C_CONTROL_INTEN);
  396. udelay(10);
  397. }
  398. /*
  399. * i2c_read: - Read multiple bytes from an i2c device
  400. *
  401. * The higher level routines take into account that this function is only
  402. * called with len < page length of the device (see configuration file)
  403. *
  404. * @chip: address of the chip which is to be read
  405. * @addr: i2c data address within the chip
  406. * @alen: length of the i2c data address (1..2 bytes)
  407. * @buffer: where to write the data
  408. * @len: how much byte do we want to read
  409. * @return: 0 in case of success
  410. */
  411. int i2c_read(uint8_t chip, uint32_t addr, int alen, uint8_t *buffer, int len)
  412. {
  413. int ret = 0;
  414. uint32_t counter = 0;
  415. #ifdef DEBUG_I2C
  416. mentor_i2c_probe(chip);
  417. #endif
  418. do {
  419. if (ret != -EAGAIN && ret) {
  420. ERROR("i2c transaction failed, after %d retries\n",
  421. counter);
  422. mentor_i2c_stop_bit_set();
  423. return ret;
  424. }
  425. /* wait for 1 us for the interrupt clear to take effect */
  426. if (counter > 0)
  427. udelay(1);
  428. counter++;
  429. ret = mentor_i2c_start_bit_set();
  430. if (ret) {
  431. ret = mentor_i2c_unstuck(ret);
  432. continue;
  433. }
  434. /* if EEPROM device */
  435. if (alen != 0) {
  436. ret = mentor_i2c_address_set(chip, I2C_CMD_WRITE);
  437. if (ret)
  438. continue;
  439. ret = mentor_i2c_target_offset_set(chip, addr, alen);
  440. if (ret)
  441. continue;
  442. ret = mentor_i2c_start_bit_set();
  443. if (ret)
  444. continue;
  445. }
  446. ret = mentor_i2c_address_set(chip, I2C_CMD_READ);
  447. if (ret)
  448. continue;
  449. ret = mentor_i2c_data_receive(buffer, len);
  450. if (ret)
  451. continue;
  452. ret = mentor_i2c_stop_bit_set();
  453. } while ((ret == -EAGAIN) && (counter < I2C_MAX_RETRY_CNT));
  454. if (counter == I2C_MAX_RETRY_CNT) {
  455. ERROR("I2C transactions failed, got EAGAIN %d times\n",
  456. I2C_MAX_RETRY_CNT);
  457. ret = -EPERM;
  458. }
  459. mmio_write_32((uintptr_t)&base->control,
  460. mmio_read_32((uintptr_t)&base->control) |
  461. I2C_CONTROL_ACK);
  462. udelay(1);
  463. return ret;
  464. }
  465. /*
  466. * i2c_write: - Write multiple bytes to an i2c device
  467. *
  468. * The higher level routines take into account that this function is only
  469. * called with len < page length of the device (see configuration file)
  470. *
  471. * @chip: address of the chip which is to be written
  472. * @addr: i2c data address within the chip
  473. * @alen: length of the i2c data address (1..2 bytes)
  474. * @buffer: where to find the data to be written
  475. * @len: how much byte do we want to read
  476. * @return: 0 in case of success
  477. */
  478. int i2c_write(uint8_t chip, uint32_t addr, int alen, uint8_t *buffer, int len)
  479. {
  480. int ret = 0;
  481. uint32_t counter = 0;
  482. do {
  483. if (ret != -EAGAIN && ret) {
  484. ERROR("i2c transaction failed\n");
  485. mentor_i2c_stop_bit_set();
  486. return ret;
  487. }
  488. /* wait for 1 us for the interrupt clear to take effect */
  489. if (counter > 0)
  490. udelay(1);
  491. counter++;
  492. ret = mentor_i2c_start_bit_set();
  493. if (ret) {
  494. ret = mentor_i2c_unstuck(ret);
  495. continue;
  496. }
  497. ret = mentor_i2c_address_set(chip, I2C_CMD_WRITE);
  498. if (ret)
  499. continue;
  500. /* if EEPROM device */
  501. if (alen != 0) {
  502. ret = mentor_i2c_target_offset_set(chip, addr, alen);
  503. if (ret)
  504. continue;
  505. }
  506. ret = mentor_i2c_data_transmit(buffer, len);
  507. if (ret)
  508. continue;
  509. ret = mentor_i2c_stop_bit_set();
  510. } while ((ret == -EAGAIN) && (counter < I2C_MAX_RETRY_CNT));
  511. if (counter == I2C_MAX_RETRY_CNT) {
  512. ERROR("I2C transactions failed, got EAGAIN %d times\n",
  513. I2C_MAX_RETRY_CNT);
  514. ret = -EPERM;
  515. }
  516. udelay(1);
  517. return ret;
  518. }