tegra_i2c.c 12 KB

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