tegra_i2c.c 13 KB

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