tegra_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 <dm.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <i2c.h>
  13. #include <asm/io.h>
  14. #ifdef CONFIG_TEGRA186
  15. #include <clk.h>
  16. #include <reset.h>
  17. #else
  18. #include <asm/arch/clock.h>
  19. #include <asm/arch/funcmux.h>
  20. #include <asm/arch/pinmux.h>
  21. #include <asm/arch-tegra/clk_rst.h>
  22. #endif
  23. #include <asm/arch/gpio.h>
  24. #include <asm/arch-tegra/tegra_i2c.h>
  25. /*
  26. * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
  27. * should not be present. These are needed because newer Tegra SoCs support
  28. * only the standard clock/reset APIs, whereas older Tegra SoCs support only
  29. * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
  30. * fixed to implement the standard APIs, and all drivers converted to solely
  31. * use the new standard APIs, with no ifdefs.
  32. */
  33. DECLARE_GLOBAL_DATA_PTR;
  34. enum i2c_type {
  35. TYPE_114,
  36. TYPE_STD,
  37. TYPE_DVC,
  38. };
  39. /* Information about i2c controller */
  40. struct i2c_bus {
  41. int id;
  42. #ifdef CONFIG_TEGRA186
  43. struct reset_ctl reset_ctl;
  44. struct clk clk;
  45. #else
  46. enum periph_id periph_id;
  47. #endif
  48. int speed;
  49. int pinmux_config;
  50. struct i2c_control *control;
  51. struct i2c_ctlr *regs;
  52. enum i2c_type type;
  53. int inited; /* bus is inited */
  54. };
  55. static void set_packet_mode(struct i2c_bus *i2c_bus)
  56. {
  57. u32 config;
  58. config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
  59. if (i2c_bus->type == TYPE_DVC) {
  60. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  61. writel(config, &dvc->cnfg);
  62. } else {
  63. writel(config, &i2c_bus->regs->cnfg);
  64. /*
  65. * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
  66. * issues, i.e., some slaves may be wrongly detected.
  67. */
  68. setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
  69. }
  70. }
  71. static void i2c_reset_controller(struct i2c_bus *i2c_bus)
  72. {
  73. /* Reset I2C controller. */
  74. #ifdef CONFIG_TEGRA186
  75. reset_assert(&i2c_bus->reset_ctl);
  76. udelay(1);
  77. reset_deassert(&i2c_bus->reset_ctl);
  78. udelay(1);
  79. #else
  80. reset_periph(i2c_bus->periph_id, 1);
  81. #endif
  82. /* re-program config register to packet mode */
  83. set_packet_mode(i2c_bus);
  84. }
  85. #ifdef CONFIG_TEGRA186
  86. static int i2c_init_clock(struct i2c_bus *i2c_bus, unsigned rate)
  87. {
  88. int ret;
  89. ret = reset_assert(&i2c_bus->reset_ctl);
  90. if (ret)
  91. return ret;
  92. ret = clk_enable(&i2c_bus->clk);
  93. if (ret)
  94. return ret;
  95. ret = clk_set_rate(&i2c_bus->clk, rate);
  96. if (IS_ERR_VALUE(ret))
  97. return ret;
  98. ret = reset_deassert(&i2c_bus->reset_ctl);
  99. if (ret)
  100. return ret;
  101. return 0;
  102. }
  103. #endif
  104. static void i2c_init_controller(struct i2c_bus *i2c_bus)
  105. {
  106. if (!i2c_bus->speed)
  107. return;
  108. debug("%s: speed=%d\n", __func__, i2c_bus->speed);
  109. /*
  110. * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
  111. * here, in section 23.3.1, but in fact we seem to need a factor of
  112. * 16 to get the right frequency.
  113. */
  114. #ifdef CONFIG_TEGRA186
  115. i2c_init_clock(i2c_bus, i2c_bus->speed * 2 * 8);
  116. #else
  117. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  118. i2c_bus->speed * 2 * 8);
  119. #endif
  120. if (i2c_bus->type == TYPE_114) {
  121. /*
  122. * T114 I2C went to a single clock source for standard/fast and
  123. * HS clock speeds. The new clock rate setting calculation is:
  124. * SCL = CLK_SOURCE.I2C /
  125. * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
  126. * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
  127. *
  128. * NOTE: We do this here, after the initial clock/pll start,
  129. * because if we read the clk_div reg before the controller
  130. * is running, we hang, and we need it for the new calc.
  131. */
  132. int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
  133. unsigned rate = CLK_MULT_STD_FAST_MODE *
  134. (clk_div_stdfst_mode + 1) * i2c_bus->speed * 2;
  135. debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
  136. clk_div_stdfst_mode);
  137. #ifdef CONFIG_TEGRA186
  138. i2c_init_clock(i2c_bus, rate);
  139. #else
  140. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  141. rate);
  142. #endif
  143. }
  144. /* Reset I2C controller. */
  145. i2c_reset_controller(i2c_bus);
  146. /* Configure I2C controller. */
  147. if (i2c_bus->type == TYPE_DVC) { /* only for DVC I2C */
  148. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  149. setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
  150. }
  151. #ifndef CONFIG_TEGRA186
  152. funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
  153. #endif
  154. }
  155. static void send_packet_headers(
  156. struct i2c_bus *i2c_bus,
  157. struct i2c_trans_info *trans,
  158. u32 packet_id,
  159. bool end_with_repeated_start)
  160. {
  161. u32 data;
  162. /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
  163. data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
  164. data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
  165. data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
  166. writel(data, &i2c_bus->control->tx_fifo);
  167. debug("pkt header 1 sent (0x%x)\n", data);
  168. /* prepare header2 */
  169. data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
  170. writel(data, &i2c_bus->control->tx_fifo);
  171. debug("pkt header 2 sent (0x%x)\n", data);
  172. /* prepare IO specific header: configure the slave address */
  173. data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
  174. /* Enable Read if it is not a write transaction */
  175. if (!(trans->flags & I2C_IS_WRITE))
  176. data |= PKT_HDR3_READ_MODE_MASK;
  177. if (end_with_repeated_start)
  178. data |= PKT_HDR3_REPEAT_START_MASK;
  179. /* Write I2C specific header */
  180. writel(data, &i2c_bus->control->tx_fifo);
  181. debug("pkt header 3 sent (0x%x)\n", data);
  182. }
  183. static int wait_for_tx_fifo_empty(struct i2c_control *control)
  184. {
  185. u32 count;
  186. int timeout_us = I2C_TIMEOUT_USEC;
  187. while (timeout_us >= 0) {
  188. count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
  189. >> TX_FIFO_EMPTY_CNT_SHIFT;
  190. if (count == I2C_FIFO_DEPTH)
  191. return 1;
  192. udelay(10);
  193. timeout_us -= 10;
  194. }
  195. return 0;
  196. }
  197. static int wait_for_rx_fifo_notempty(struct i2c_control *control)
  198. {
  199. u32 count;
  200. int timeout_us = I2C_TIMEOUT_USEC;
  201. while (timeout_us >= 0) {
  202. count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
  203. >> TX_FIFO_FULL_CNT_SHIFT;
  204. if (count)
  205. return 1;
  206. udelay(10);
  207. timeout_us -= 10;
  208. }
  209. return 0;
  210. }
  211. static int wait_for_transfer_complete(struct i2c_control *control)
  212. {
  213. int int_status;
  214. int timeout_us = I2C_TIMEOUT_USEC;
  215. while (timeout_us >= 0) {
  216. int_status = readl(&control->int_status);
  217. if (int_status & I2C_INT_NO_ACK_MASK)
  218. return -int_status;
  219. if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
  220. return -int_status;
  221. if (int_status & I2C_INT_XFER_COMPLETE_MASK)
  222. return 0;
  223. udelay(10);
  224. timeout_us -= 10;
  225. }
  226. return -1;
  227. }
  228. static int send_recv_packets(struct i2c_bus *i2c_bus,
  229. struct i2c_trans_info *trans)
  230. {
  231. struct i2c_control *control = i2c_bus->control;
  232. u32 int_status;
  233. u32 words;
  234. u8 *dptr;
  235. u32 local;
  236. uchar last_bytes;
  237. int error = 0;
  238. int is_write = trans->flags & I2C_IS_WRITE;
  239. /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
  240. int_status = readl(&control->int_status);
  241. writel(int_status, &control->int_status);
  242. send_packet_headers(i2c_bus, trans, 1,
  243. trans->flags & I2C_USE_REPEATED_START);
  244. words = DIV_ROUND_UP(trans->num_bytes, 4);
  245. last_bytes = trans->num_bytes & 3;
  246. dptr = trans->buf;
  247. while (words) {
  248. u32 *wptr = (u32 *)dptr;
  249. if (is_write) {
  250. /* deal with word alignment */
  251. if ((words == 1) && last_bytes) {
  252. local = 0;
  253. memcpy(&local, dptr, last_bytes);
  254. } else if ((unsigned long)dptr & 3) {
  255. memcpy(&local, dptr, sizeof(u32));
  256. } else {
  257. local = *wptr;
  258. }
  259. writel(local, &control->tx_fifo);
  260. debug("pkt data sent (0x%x)\n", local);
  261. if (!wait_for_tx_fifo_empty(control)) {
  262. error = -1;
  263. goto exit;
  264. }
  265. } else {
  266. if (!wait_for_rx_fifo_notempty(control)) {
  267. error = -1;
  268. goto exit;
  269. }
  270. /*
  271. * for the last word, we read into our local buffer,
  272. * in case that caller did not provide enough buffer.
  273. */
  274. local = readl(&control->rx_fifo);
  275. if ((words == 1) && last_bytes)
  276. memcpy(dptr, (char *)&local, last_bytes);
  277. else if ((unsigned long)dptr & 3)
  278. memcpy(dptr, &local, sizeof(u32));
  279. else
  280. *wptr = local;
  281. debug("pkt data received (0x%x)\n", local);
  282. }
  283. words--;
  284. dptr += sizeof(u32);
  285. }
  286. if (wait_for_transfer_complete(control)) {
  287. error = -1;
  288. goto exit;
  289. }
  290. return 0;
  291. exit:
  292. /* error, reset the controller. */
  293. i2c_reset_controller(i2c_bus);
  294. return error;
  295. }
  296. static int tegra_i2c_write_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data,
  297. u32 len, bool end_with_repeated_start)
  298. {
  299. int error;
  300. struct i2c_trans_info trans_info;
  301. trans_info.address = addr;
  302. trans_info.buf = data;
  303. trans_info.flags = I2C_IS_WRITE;
  304. if (end_with_repeated_start)
  305. trans_info.flags |= I2C_USE_REPEATED_START;
  306. trans_info.num_bytes = len;
  307. trans_info.is_10bit_address = 0;
  308. error = send_recv_packets(i2c_bus, &trans_info);
  309. if (error)
  310. debug("tegra_i2c_write_data: Error (%d) !!!\n", error);
  311. return error;
  312. }
  313. static int tegra_i2c_read_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data,
  314. u32 len)
  315. {
  316. int error;
  317. struct i2c_trans_info trans_info;
  318. trans_info.address = addr | 1;
  319. trans_info.buf = data;
  320. trans_info.flags = 0;
  321. trans_info.num_bytes = len;
  322. trans_info.is_10bit_address = 0;
  323. error = send_recv_packets(i2c_bus, &trans_info);
  324. if (error)
  325. debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
  326. return error;
  327. }
  328. static int tegra_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
  329. {
  330. struct i2c_bus *i2c_bus = dev_get_priv(dev);
  331. i2c_bus->speed = speed;
  332. i2c_init_controller(i2c_bus);
  333. return 0;
  334. }
  335. static int tegra_i2c_probe(struct udevice *dev)
  336. {
  337. struct i2c_bus *i2c_bus = dev_get_priv(dev);
  338. #ifdef CONFIG_TEGRA186
  339. int ret;
  340. #else
  341. const void *blob = gd->fdt_blob;
  342. int node = dev->of_offset;
  343. #endif
  344. bool is_dvc;
  345. i2c_bus->id = dev->seq;
  346. i2c_bus->type = dev_get_driver_data(dev);
  347. i2c_bus->regs = (struct i2c_ctlr *)dev_get_addr(dev);
  348. /*
  349. * We don't have a binding for pinmux yet. Leave it out for now. So
  350. * far no one needs anything other than the default.
  351. */
  352. #ifdef CONFIG_TEGRA186
  353. ret = reset_get_by_name(dev, "i2c", &i2c_bus->reset_ctl);
  354. if (ret) {
  355. error("reset_get_by_name() failed: %d\n", ret);
  356. return ret;
  357. }
  358. ret = clk_get_by_name(dev, "i2c", &i2c_bus->clk);
  359. if (ret) {
  360. error("clk_get_by_name() failed: %d\n", ret);
  361. return ret;
  362. }
  363. #else
  364. i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
  365. i2c_bus->periph_id = clock_decode_periph_id(blob, node);
  366. /*
  367. * We can't specify the pinmux config in the fdt, so I2C2 will not
  368. * work on Seaboard. It normally has no devices on it anyway.
  369. * You could add in this little hack if you need to use it.
  370. * The correct solution is a pinmux binding in the fdt.
  371. *
  372. * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
  373. * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
  374. */
  375. if (i2c_bus->periph_id == -1)
  376. return -EINVAL;
  377. #endif
  378. is_dvc = dev_get_driver_data(dev) == TYPE_DVC;
  379. if (is_dvc) {
  380. i2c_bus->control =
  381. &((struct dvc_ctlr *)i2c_bus->regs)->control;
  382. } else {
  383. i2c_bus->control = &i2c_bus->regs->control;
  384. }
  385. i2c_init_controller(i2c_bus);
  386. debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
  387. is_dvc ? "dvc" : "i2c", dev->seq, i2c_bus->regs,
  388. #ifndef CONFIG_TEGRA186
  389. i2c_bus->periph_id,
  390. #else
  391. -1,
  392. #endif
  393. i2c_bus->speed);
  394. return 0;
  395. }
  396. /* i2c write version without the register address */
  397. static int i2c_write_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer,
  398. int len, bool end_with_repeated_start)
  399. {
  400. int rc;
  401. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  402. debug("write_data: ");
  403. /* use rc for counter */
  404. for (rc = 0; rc < len; ++rc)
  405. debug(" 0x%02x", buffer[rc]);
  406. debug("\n");
  407. /* Shift 7-bit address over for lower-level i2c functions */
  408. rc = tegra_i2c_write_data(i2c_bus, chip << 1, buffer, len,
  409. end_with_repeated_start);
  410. if (rc)
  411. debug("i2c_write_data(): rc=%d\n", rc);
  412. return rc;
  413. }
  414. /* i2c read version without the register address */
  415. static int i2c_read_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer,
  416. int len)
  417. {
  418. int rc;
  419. debug("inside i2c_read_data():\n");
  420. /* Shift 7-bit address over for lower-level i2c functions */
  421. rc = tegra_i2c_read_data(i2c_bus, chip << 1, buffer, len);
  422. if (rc) {
  423. debug("i2c_read_data(): rc=%d\n", rc);
  424. return rc;
  425. }
  426. debug("i2c_read_data: ");
  427. /* reuse rc for counter*/
  428. for (rc = 0; rc < len; ++rc)
  429. debug(" 0x%02x", buffer[rc]);
  430. debug("\n");
  431. return 0;
  432. }
  433. /* Probe to see if a chip is present. */
  434. static int tegra_i2c_probe_chip(struct udevice *bus, uint chip_addr,
  435. uint chip_flags)
  436. {
  437. struct i2c_bus *i2c_bus = dev_get_priv(bus);
  438. int rc;
  439. u8 reg;
  440. /* Shift 7-bit address over for lower-level i2c functions */
  441. rc = tegra_i2c_write_data(i2c_bus, chip_addr << 1, &reg, sizeof(reg),
  442. false);
  443. return rc;
  444. }
  445. static int tegra_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  446. int nmsgs)
  447. {
  448. struct i2c_bus *i2c_bus = dev_get_priv(bus);
  449. int ret;
  450. debug("i2c_xfer: %d messages\n", nmsgs);
  451. for (; nmsgs > 0; nmsgs--, msg++) {
  452. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  453. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  454. if (msg->flags & I2C_M_RD) {
  455. ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
  456. msg->len);
  457. } else {
  458. ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
  459. msg->len, next_is_read);
  460. }
  461. if (ret) {
  462. debug("i2c_write: error sending\n");
  463. return -EREMOTEIO;
  464. }
  465. }
  466. return 0;
  467. }
  468. int tegra_i2c_get_dvc_bus(struct udevice **busp)
  469. {
  470. struct udevice *bus;
  471. for (uclass_first_device(UCLASS_I2C, &bus);
  472. bus;
  473. uclass_next_device(&bus)) {
  474. if (dev_get_driver_data(bus) == TYPE_DVC) {
  475. *busp = bus;
  476. return 0;
  477. }
  478. }
  479. return -ENODEV;
  480. }
  481. static const struct dm_i2c_ops tegra_i2c_ops = {
  482. .xfer = tegra_i2c_xfer,
  483. .probe_chip = tegra_i2c_probe_chip,
  484. .set_bus_speed = tegra_i2c_set_bus_speed,
  485. };
  486. static const struct udevice_id tegra_i2c_ids[] = {
  487. { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  488. { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
  489. { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
  490. { }
  491. };
  492. U_BOOT_DRIVER(i2c_tegra) = {
  493. .name = "i2c_tegra",
  494. .id = UCLASS_I2C,
  495. .of_match = tegra_i2c_ids,
  496. .probe = tegra_i2c_probe,
  497. .priv_auto_alloc_size = sizeof(struct i2c_bus),
  498. .ops = &tegra_i2c_ops,
  499. };