tegra_i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  3. * Copyright (c) 2010-2011 NVIDIA Corporation
  4. * NVIDIA Corporation <www.nvidia.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <fdtdec.h>
  10. #include <i2c.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/funcmux.h>
  14. #include <asm/arch/gpio.h>
  15. #include <asm/arch/pinmux.h>
  16. #include <asm/arch-tegra/clk_rst.h>
  17. #include <asm/arch-tegra/tegra_i2c.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /* Information about i2c controller */
  20. struct i2c_bus {
  21. int id;
  22. enum periph_id periph_id;
  23. int speed;
  24. int pinmux_config;
  25. struct i2c_control *control;
  26. struct i2c_ctlr *regs;
  27. int is_dvc; /* DVC type, rather than I2C */
  28. int is_scs; /* single clock source (T114+) */
  29. int inited; /* bus is inited */
  30. };
  31. static struct i2c_bus i2c_controllers[TEGRA_I2C_NUM_CONTROLLERS];
  32. static void set_packet_mode(struct i2c_bus *i2c_bus)
  33. {
  34. u32 config;
  35. config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
  36. if (i2c_bus->is_dvc) {
  37. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  38. writel(config, &dvc->cnfg);
  39. } else {
  40. writel(config, &i2c_bus->regs->cnfg);
  41. /*
  42. * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
  43. * issues, i.e., some slaves may be wrongly detected.
  44. */
  45. setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
  46. }
  47. }
  48. static void i2c_reset_controller(struct i2c_bus *i2c_bus)
  49. {
  50. /* Reset I2C controller. */
  51. reset_periph(i2c_bus->periph_id, 1);
  52. /* re-program config register to packet mode */
  53. set_packet_mode(i2c_bus);
  54. }
  55. static void i2c_init_controller(struct i2c_bus *i2c_bus)
  56. {
  57. /*
  58. * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
  59. * here, in section 23.3.1, but in fact we seem to need a factor of
  60. * 16 to get the right frequency.
  61. */
  62. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  63. i2c_bus->speed * 2 * 8);
  64. if (i2c_bus->is_scs) {
  65. /*
  66. * T114 I2C went to a single clock source for standard/fast and
  67. * HS clock speeds. The new clock rate setting calculation is:
  68. * SCL = CLK_SOURCE.I2C /
  69. * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
  70. * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
  71. *
  72. * NOTE: We do this here, after the initial clock/pll start,
  73. * because if we read the clk_div reg before the controller
  74. * is running, we hang, and we need it for the new calc.
  75. */
  76. int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
  77. debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
  78. clk_div_stdfst_mode);
  79. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  80. CLK_MULT_STD_FAST_MODE * (clk_div_stdfst_mode + 1) *
  81. i2c_bus->speed * 2);
  82. }
  83. /* Reset I2C controller. */
  84. i2c_reset_controller(i2c_bus);
  85. /* Configure I2C controller. */
  86. if (i2c_bus->is_dvc) { /* only for DVC I2C */
  87. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  88. setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
  89. }
  90. funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
  91. }
  92. static void send_packet_headers(
  93. struct i2c_bus *i2c_bus,
  94. struct i2c_trans_info *trans,
  95. u32 packet_id)
  96. {
  97. u32 data;
  98. /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
  99. data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
  100. data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
  101. data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
  102. writel(data, &i2c_bus->control->tx_fifo);
  103. debug("pkt header 1 sent (0x%x)\n", data);
  104. /* prepare header2 */
  105. data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
  106. writel(data, &i2c_bus->control->tx_fifo);
  107. debug("pkt header 2 sent (0x%x)\n", data);
  108. /* prepare IO specific header: configure the slave address */
  109. data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
  110. /* Enable Read if it is not a write transaction */
  111. if (!(trans->flags & I2C_IS_WRITE))
  112. data |= PKT_HDR3_READ_MODE_MASK;
  113. /* Write I2C specific header */
  114. writel(data, &i2c_bus->control->tx_fifo);
  115. debug("pkt header 3 sent (0x%x)\n", data);
  116. }
  117. static int wait_for_tx_fifo_empty(struct i2c_control *control)
  118. {
  119. u32 count;
  120. int timeout_us = I2C_TIMEOUT_USEC;
  121. while (timeout_us >= 0) {
  122. count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
  123. >> TX_FIFO_EMPTY_CNT_SHIFT;
  124. if (count == I2C_FIFO_DEPTH)
  125. return 1;
  126. udelay(10);
  127. timeout_us -= 10;
  128. }
  129. return 0;
  130. }
  131. static int wait_for_rx_fifo_notempty(struct i2c_control *control)
  132. {
  133. u32 count;
  134. int timeout_us = I2C_TIMEOUT_USEC;
  135. while (timeout_us >= 0) {
  136. count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
  137. >> TX_FIFO_FULL_CNT_SHIFT;
  138. if (count)
  139. return 1;
  140. udelay(10);
  141. timeout_us -= 10;
  142. }
  143. return 0;
  144. }
  145. static int wait_for_transfer_complete(struct i2c_control *control)
  146. {
  147. int int_status;
  148. int timeout_us = I2C_TIMEOUT_USEC;
  149. while (timeout_us >= 0) {
  150. int_status = readl(&control->int_status);
  151. if (int_status & I2C_INT_NO_ACK_MASK)
  152. return -int_status;
  153. if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
  154. return -int_status;
  155. if (int_status & I2C_INT_XFER_COMPLETE_MASK)
  156. return 0;
  157. udelay(10);
  158. timeout_us -= 10;
  159. }
  160. return -1;
  161. }
  162. static int send_recv_packets(struct i2c_bus *i2c_bus,
  163. struct i2c_trans_info *trans)
  164. {
  165. struct i2c_control *control = i2c_bus->control;
  166. u32 int_status;
  167. u32 words;
  168. u8 *dptr;
  169. u32 local;
  170. uchar last_bytes;
  171. int error = 0;
  172. int is_write = trans->flags & I2C_IS_WRITE;
  173. /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
  174. int_status = readl(&control->int_status);
  175. writel(int_status, &control->int_status);
  176. send_packet_headers(i2c_bus, trans, 1);
  177. words = DIV_ROUND_UP(trans->num_bytes, 4);
  178. last_bytes = trans->num_bytes & 3;
  179. dptr = trans->buf;
  180. while (words) {
  181. u32 *wptr = (u32 *)dptr;
  182. if (is_write) {
  183. /* deal with word alignment */
  184. if ((unsigned)dptr & 3) {
  185. memcpy(&local, dptr, sizeof(u32));
  186. writel(local, &control->tx_fifo);
  187. debug("pkt data sent (0x%x)\n", local);
  188. } else {
  189. writel(*wptr, &control->tx_fifo);
  190. debug("pkt data sent (0x%x)\n", *wptr);
  191. }
  192. if (!wait_for_tx_fifo_empty(control)) {
  193. error = -1;
  194. goto exit;
  195. }
  196. } else {
  197. if (!wait_for_rx_fifo_notempty(control)) {
  198. error = -1;
  199. goto exit;
  200. }
  201. /*
  202. * for the last word, we read into our local buffer,
  203. * in case that caller did not provide enough buffer.
  204. */
  205. local = readl(&control->rx_fifo);
  206. if ((words == 1) && last_bytes)
  207. memcpy(dptr, (char *)&local, last_bytes);
  208. else if ((unsigned)dptr & 3)
  209. memcpy(dptr, &local, sizeof(u32));
  210. else
  211. *wptr = local;
  212. debug("pkt data received (0x%x)\n", local);
  213. }
  214. words--;
  215. dptr += sizeof(u32);
  216. }
  217. if (wait_for_transfer_complete(control)) {
  218. error = -1;
  219. goto exit;
  220. }
  221. return 0;
  222. exit:
  223. /* error, reset the controller. */
  224. i2c_reset_controller(i2c_bus);
  225. return error;
  226. }
  227. static int tegra_i2c_write_data(struct i2c_bus *bus, u32 addr, u8 *data,
  228. u32 len)
  229. {
  230. int error;
  231. struct i2c_trans_info trans_info;
  232. trans_info.address = addr;
  233. trans_info.buf = data;
  234. trans_info.flags = I2C_IS_WRITE;
  235. trans_info.num_bytes = len;
  236. trans_info.is_10bit_address = 0;
  237. error = send_recv_packets(bus, &trans_info);
  238. if (error)
  239. debug("tegra_i2c_write_data: Error (%d) !!!\n", error);
  240. return error;
  241. }
  242. static int tegra_i2c_read_data(struct i2c_bus *bus, u32 addr, u8 *data,
  243. u32 len)
  244. {
  245. int error;
  246. struct i2c_trans_info trans_info;
  247. trans_info.address = addr | 1;
  248. trans_info.buf = data;
  249. trans_info.flags = 0;
  250. trans_info.num_bytes = len;
  251. trans_info.is_10bit_address = 0;
  252. error = send_recv_packets(bus, &trans_info);
  253. if (error)
  254. debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
  255. return error;
  256. }
  257. #ifndef CONFIG_OF_CONTROL
  258. #error "Please enable device tree support to use this driver"
  259. #endif
  260. /**
  261. * Check that a bus number is valid and return a pointer to it
  262. *
  263. * @param bus_num Bus number to check / return
  264. * @return pointer to bus, if valid, else NULL
  265. */
  266. static struct i2c_bus *tegra_i2c_get_bus(struct i2c_adapter *adap)
  267. {
  268. struct i2c_bus *bus;
  269. bus = &i2c_controllers[adap->hwadapnr];
  270. if (!bus->inited) {
  271. debug("%s: Bus %u not available\n", __func__, adap->hwadapnr);
  272. return NULL;
  273. }
  274. return bus;
  275. }
  276. static unsigned int tegra_i2c_set_bus_speed(struct i2c_adapter *adap,
  277. unsigned int speed)
  278. {
  279. struct i2c_bus *bus;
  280. bus = tegra_i2c_get_bus(adap);
  281. if (!bus)
  282. return 0;
  283. bus->speed = speed;
  284. i2c_init_controller(bus);
  285. return 0;
  286. }
  287. static int i2c_get_config(const void *blob, int node, struct i2c_bus *i2c_bus)
  288. {
  289. i2c_bus->regs = (struct i2c_ctlr *)fdtdec_get_addr(blob, node, "reg");
  290. /*
  291. * We don't have a binding for pinmux yet. Leave it out for now. So
  292. * far no one needs anything other than the default.
  293. */
  294. i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
  295. i2c_bus->speed = fdtdec_get_int(blob, node, "clock-frequency", 0);
  296. i2c_bus->periph_id = clock_decode_periph_id(blob, node);
  297. /*
  298. * We can't specify the pinmux config in the fdt, so I2C2 will not
  299. * work on Seaboard. It normally has no devices on it anyway.
  300. * You could add in this little hack if you need to use it.
  301. * The correct solution is a pinmux binding in the fdt.
  302. *
  303. * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
  304. * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
  305. */
  306. if (i2c_bus->periph_id == -1)
  307. return -FDT_ERR_NOTFOUND;
  308. return 0;
  309. }
  310. /*
  311. * Process a list of nodes, adding them to our list of I2C ports.
  312. *
  313. * @param blob fdt blob
  314. * @param node_list list of nodes to process (any <=0 are ignored)
  315. * @param count number of nodes to process
  316. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  317. * @param is_scs 1 if this HW uses a single clock source (T114+)
  318. * @return 0 if ok, -1 on error
  319. */
  320. static int process_nodes(const void *blob, int node_list[], int count,
  321. int is_dvc, int is_scs)
  322. {
  323. struct i2c_bus *i2c_bus;
  324. int i;
  325. /* build the i2c_controllers[] for each controller */
  326. for (i = 0; i < count; i++) {
  327. int node = node_list[i];
  328. if (node <= 0)
  329. continue;
  330. i2c_bus = &i2c_controllers[i];
  331. i2c_bus->id = i;
  332. if (i2c_get_config(blob, node, i2c_bus)) {
  333. printf("i2c_init_board: failed to decode bus %d\n", i);
  334. return -1;
  335. }
  336. i2c_bus->is_scs = is_scs;
  337. i2c_bus->is_dvc = is_dvc;
  338. if (is_dvc) {
  339. i2c_bus->control =
  340. &((struct dvc_ctlr *)i2c_bus->regs)->control;
  341. } else {
  342. i2c_bus->control = &i2c_bus->regs->control;
  343. }
  344. debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
  345. is_dvc ? "dvc" : "i2c", i, i2c_bus->regs,
  346. i2c_bus->periph_id, i2c_bus->speed);
  347. i2c_init_controller(i2c_bus);
  348. debug("ok\n");
  349. i2c_bus->inited = 1;
  350. /* Mark position as used */
  351. node_list[i] = -1;
  352. }
  353. return 0;
  354. }
  355. /* Sadly there is no error return from this function */
  356. void i2c_init_board(void)
  357. {
  358. int node_list[TEGRA_I2C_NUM_CONTROLLERS];
  359. const void *blob = gd->fdt_blob;
  360. int count;
  361. /* First check for newer (T114+) I2C ports */
  362. count = fdtdec_find_aliases_for_id(blob, "i2c",
  363. COMPAT_NVIDIA_TEGRA114_I2C, node_list,
  364. TEGRA_I2C_NUM_CONTROLLERS);
  365. if (process_nodes(blob, node_list, count, 0, 1))
  366. return;
  367. /* Now get the older (T20/T30) normal I2C ports */
  368. count = fdtdec_find_aliases_for_id(blob, "i2c",
  369. COMPAT_NVIDIA_TEGRA20_I2C, node_list,
  370. TEGRA_I2C_NUM_CONTROLLERS);
  371. if (process_nodes(blob, node_list, count, 0, 0))
  372. return;
  373. /* Now look for dvc ports */
  374. count = fdtdec_add_aliases_for_id(blob, "i2c",
  375. COMPAT_NVIDIA_TEGRA20_DVC, node_list,
  376. TEGRA_I2C_NUM_CONTROLLERS);
  377. if (process_nodes(blob, node_list, count, 1, 0))
  378. return;
  379. }
  380. static void tegra_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  381. {
  382. /* This will override the speed selected in the fdt for that port */
  383. debug("i2c_init(speed=%u, slaveaddr=0x%x)\n", speed, slaveaddr);
  384. i2c_set_bus_speed(speed);
  385. }
  386. /* i2c write version without the register address */
  387. int i2c_write_data(struct i2c_bus *bus, uchar chip, uchar *buffer, int len)
  388. {
  389. int rc;
  390. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  391. debug("write_data: ");
  392. /* use rc for counter */
  393. for (rc = 0; rc < len; ++rc)
  394. debug(" 0x%02x", buffer[rc]);
  395. debug("\n");
  396. /* Shift 7-bit address over for lower-level i2c functions */
  397. rc = tegra_i2c_write_data(bus, chip << 1, buffer, len);
  398. if (rc)
  399. debug("i2c_write_data(): rc=%d\n", rc);
  400. return rc;
  401. }
  402. /* i2c read version without the register address */
  403. int i2c_read_data(struct i2c_bus *bus, uchar chip, uchar *buffer, int len)
  404. {
  405. int rc;
  406. debug("inside i2c_read_data():\n");
  407. /* Shift 7-bit address over for lower-level i2c functions */
  408. rc = tegra_i2c_read_data(bus, chip << 1, buffer, len);
  409. if (rc) {
  410. debug("i2c_read_data(): rc=%d\n", rc);
  411. return rc;
  412. }
  413. debug("i2c_read_data: ");
  414. /* reuse rc for counter*/
  415. for (rc = 0; rc < len; ++rc)
  416. debug(" 0x%02x", buffer[rc]);
  417. debug("\n");
  418. return 0;
  419. }
  420. /* Probe to see if a chip is present. */
  421. static int tegra_i2c_probe(struct i2c_adapter *adap, uchar chip)
  422. {
  423. struct i2c_bus *bus;
  424. int rc;
  425. uchar reg;
  426. debug("i2c_probe: addr=0x%x\n", chip);
  427. bus = tegra_i2c_get_bus(adap);
  428. if (!bus)
  429. return 1;
  430. reg = 0;
  431. rc = i2c_write_data(bus, chip, &reg, 1);
  432. if (rc) {
  433. debug("Error probing 0x%x.\n", chip);
  434. return 1;
  435. }
  436. return 0;
  437. }
  438. static int i2c_addr_ok(const uint addr, const int alen)
  439. {
  440. /* We support 7 or 10 bit addresses, so one or two bytes each */
  441. return alen == 1 || alen == 2;
  442. }
  443. /* Read bytes */
  444. static int tegra_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  445. int alen, uchar *buffer, int len)
  446. {
  447. struct i2c_bus *bus;
  448. uint offset;
  449. int i;
  450. debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
  451. chip, addr, len);
  452. bus = tegra_i2c_get_bus(adap);
  453. if (!bus)
  454. return 1;
  455. if (!i2c_addr_ok(addr, alen)) {
  456. debug("i2c_read: Bad address %x.%d.\n", addr, alen);
  457. return 1;
  458. }
  459. for (offset = 0; offset < len; offset++) {
  460. if (alen) {
  461. uchar data[alen];
  462. for (i = 0; i < alen; i++) {
  463. data[alen - i - 1] =
  464. (addr + offset) >> (8 * i);
  465. }
  466. if (i2c_write_data(bus, chip, data, alen)) {
  467. debug("i2c_read: error sending (0x%x)\n",
  468. addr);
  469. return 1;
  470. }
  471. }
  472. if (i2c_read_data(bus, chip, buffer + offset, 1)) {
  473. debug("i2c_read: error reading (0x%x)\n", addr);
  474. return 1;
  475. }
  476. }
  477. return 0;
  478. }
  479. /* Write bytes */
  480. static int tegra_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  481. int alen, uchar *buffer, int len)
  482. {
  483. struct i2c_bus *bus;
  484. uint offset;
  485. int i;
  486. debug("i2c_write: chip=0x%x, addr=0x%x, len=0x%x\n",
  487. chip, addr, len);
  488. bus = tegra_i2c_get_bus(adap);
  489. if (!bus)
  490. return 1;
  491. if (!i2c_addr_ok(addr, alen)) {
  492. debug("i2c_write: Bad address %x.%d.\n", addr, alen);
  493. return 1;
  494. }
  495. for (offset = 0; offset < len; offset++) {
  496. uchar data[alen + 1];
  497. for (i = 0; i < alen; i++)
  498. data[alen - i - 1] = (addr + offset) >> (8 * i);
  499. data[alen] = buffer[offset];
  500. if (i2c_write_data(bus, chip, data, alen + 1)) {
  501. debug("i2c_write: error sending (0x%x)\n", addr);
  502. return 1;
  503. }
  504. }
  505. return 0;
  506. }
  507. int tegra_i2c_get_dvc_bus_num(void)
  508. {
  509. int i;
  510. for (i = 0; i < TEGRA_I2C_NUM_CONTROLLERS; i++) {
  511. struct i2c_bus *bus = &i2c_controllers[i];
  512. if (bus->inited && bus->is_dvc)
  513. return i;
  514. }
  515. return -1;
  516. }
  517. /*
  518. * Register soft i2c adapters
  519. */
  520. U_BOOT_I2C_ADAP_COMPLETE(tegra0, tegra_i2c_init, tegra_i2c_probe,
  521. tegra_i2c_read, tegra_i2c_write,
  522. tegra_i2c_set_bus_speed, 100000, 0, 0)
  523. U_BOOT_I2C_ADAP_COMPLETE(tegra1, tegra_i2c_init, tegra_i2c_probe,
  524. tegra_i2c_read, tegra_i2c_write,
  525. tegra_i2c_set_bus_speed, 100000, 0, 1)
  526. U_BOOT_I2C_ADAP_COMPLETE(tegra2, tegra_i2c_init, tegra_i2c_probe,
  527. tegra_i2c_read, tegra_i2c_write,
  528. tegra_i2c_set_bus_speed, 100000, 0, 2)
  529. U_BOOT_I2C_ADAP_COMPLETE(tegra3, tegra_i2c_init, tegra_i2c_probe,
  530. tegra_i2c_read, tegra_i2c_write,
  531. tegra_i2c_set_bus_speed, 100000, 0, 3)