tegra_i2c.c 13 KB

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