tegra_i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <fdtdec.h>
  26. #include <i2c.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/funcmux.h>
  30. #include <asm/arch/gpio.h>
  31. #include <asm/arch/pinmux.h>
  32. #include <asm/arch-tegra/clk_rst.h>
  33. #include <asm/arch-tegra/tegra_i2c.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. static unsigned int i2c_bus_num;
  36. /* Information about i2c controller */
  37. struct i2c_bus {
  38. int id;
  39. enum periph_id periph_id;
  40. int speed;
  41. int pinmux_config;
  42. struct i2c_control *control;
  43. struct i2c_ctlr *regs;
  44. int is_dvc; /* DVC type, rather than I2C */
  45. int is_scs; /* single clock source (T114+) */
  46. int inited; /* bus is inited */
  47. };
  48. static struct i2c_bus i2c_controllers[TEGRA_I2C_NUM_CONTROLLERS];
  49. static void set_packet_mode(struct i2c_bus *i2c_bus)
  50. {
  51. u32 config;
  52. config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
  53. if (i2c_bus->is_dvc) {
  54. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  55. writel(config, &dvc->cnfg);
  56. } else {
  57. writel(config, &i2c_bus->regs->cnfg);
  58. /*
  59. * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
  60. * issues, i.e., some slaves may be wrongly detected.
  61. */
  62. setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
  63. }
  64. }
  65. static void i2c_reset_controller(struct i2c_bus *i2c_bus)
  66. {
  67. /* Reset I2C controller. */
  68. reset_periph(i2c_bus->periph_id, 1);
  69. /* re-program config register to packet mode */
  70. set_packet_mode(i2c_bus);
  71. }
  72. static void i2c_init_controller(struct i2c_bus *i2c_bus)
  73. {
  74. /*
  75. * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
  76. * here, in section 23.3.1, but in fact we seem to need a factor of
  77. * 16 to get the right frequency.
  78. */
  79. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  80. i2c_bus->speed * 2 * 8);
  81. if (i2c_bus->is_scs) {
  82. /*
  83. * T114 I2C went to a single clock source for standard/fast and
  84. * HS clock speeds. The new clock rate setting calculation is:
  85. * SCL = CLK_SOURCE.I2C /
  86. * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
  87. * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
  88. *
  89. * NOTE: We do this here, after the initial clock/pll start,
  90. * because if we read the clk_div reg before the controller
  91. * is running, we hang, and we need it for the new calc.
  92. */
  93. int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
  94. debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
  95. clk_div_stdfst_mode);
  96. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  97. CLK_MULT_STD_FAST_MODE * (clk_div_stdfst_mode + 1) *
  98. i2c_bus->speed * 2);
  99. }
  100. /* Reset I2C controller. */
  101. i2c_reset_controller(i2c_bus);
  102. /* Configure I2C controller. */
  103. if (i2c_bus->is_dvc) { /* only for DVC I2C */
  104. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  105. setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
  106. }
  107. funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
  108. }
  109. static void send_packet_headers(
  110. struct i2c_bus *i2c_bus,
  111. struct i2c_trans_info *trans,
  112. u32 packet_id)
  113. {
  114. u32 data;
  115. /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
  116. data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
  117. data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
  118. data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
  119. writel(data, &i2c_bus->control->tx_fifo);
  120. debug("pkt header 1 sent (0x%x)\n", data);
  121. /* prepare header2 */
  122. data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
  123. writel(data, &i2c_bus->control->tx_fifo);
  124. debug("pkt header 2 sent (0x%x)\n", data);
  125. /* prepare IO specific header: configure the slave address */
  126. data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
  127. /* Enable Read if it is not a write transaction */
  128. if (!(trans->flags & I2C_IS_WRITE))
  129. data |= PKT_HDR3_READ_MODE_MASK;
  130. /* Write I2C specific header */
  131. writel(data, &i2c_bus->control->tx_fifo);
  132. debug("pkt header 3 sent (0x%x)\n", data);
  133. }
  134. static int wait_for_tx_fifo_empty(struct i2c_control *control)
  135. {
  136. u32 count;
  137. int timeout_us = I2C_TIMEOUT_USEC;
  138. while (timeout_us >= 0) {
  139. count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
  140. >> TX_FIFO_EMPTY_CNT_SHIFT;
  141. if (count == I2C_FIFO_DEPTH)
  142. return 1;
  143. udelay(10);
  144. timeout_us -= 10;
  145. }
  146. return 0;
  147. }
  148. static int wait_for_rx_fifo_notempty(struct i2c_control *control)
  149. {
  150. u32 count;
  151. int timeout_us = I2C_TIMEOUT_USEC;
  152. while (timeout_us >= 0) {
  153. count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
  154. >> TX_FIFO_FULL_CNT_SHIFT;
  155. if (count)
  156. return 1;
  157. udelay(10);
  158. timeout_us -= 10;
  159. }
  160. return 0;
  161. }
  162. static int wait_for_transfer_complete(struct i2c_control *control)
  163. {
  164. int int_status;
  165. int timeout_us = I2C_TIMEOUT_USEC;
  166. while (timeout_us >= 0) {
  167. int_status = readl(&control->int_status);
  168. if (int_status & I2C_INT_NO_ACK_MASK)
  169. return -int_status;
  170. if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
  171. return -int_status;
  172. if (int_status & I2C_INT_XFER_COMPLETE_MASK)
  173. return 0;
  174. udelay(10);
  175. timeout_us -= 10;
  176. }
  177. return -1;
  178. }
  179. static int send_recv_packets(struct i2c_bus *i2c_bus,
  180. struct i2c_trans_info *trans)
  181. {
  182. struct i2c_control *control = i2c_bus->control;
  183. u32 int_status;
  184. u32 words;
  185. u8 *dptr;
  186. u32 local;
  187. uchar last_bytes;
  188. int error = 0;
  189. int is_write = trans->flags & I2C_IS_WRITE;
  190. /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
  191. int_status = readl(&control->int_status);
  192. writel(int_status, &control->int_status);
  193. send_packet_headers(i2c_bus, trans, 1);
  194. words = DIV_ROUND_UP(trans->num_bytes, 4);
  195. last_bytes = trans->num_bytes & 3;
  196. dptr = trans->buf;
  197. while (words) {
  198. u32 *wptr = (u32 *)dptr;
  199. if (is_write) {
  200. /* deal with word alignment */
  201. if ((unsigned)dptr & 3) {
  202. memcpy(&local, dptr, sizeof(u32));
  203. writel(local, &control->tx_fifo);
  204. debug("pkt data sent (0x%x)\n", local);
  205. } else {
  206. writel(*wptr, &control->tx_fifo);
  207. debug("pkt data sent (0x%x)\n", *wptr);
  208. }
  209. if (!wait_for_tx_fifo_empty(control)) {
  210. error = -1;
  211. goto exit;
  212. }
  213. } else {
  214. if (!wait_for_rx_fifo_notempty(control)) {
  215. error = -1;
  216. goto exit;
  217. }
  218. /*
  219. * for the last word, we read into our local buffer,
  220. * in case that caller did not provide enough buffer.
  221. */
  222. local = readl(&control->rx_fifo);
  223. if ((words == 1) && last_bytes)
  224. memcpy(dptr, (char *)&local, last_bytes);
  225. else if ((unsigned)dptr & 3)
  226. memcpy(dptr, &local, sizeof(u32));
  227. else
  228. *wptr = local;
  229. debug("pkt data received (0x%x)\n", local);
  230. }
  231. words--;
  232. dptr += sizeof(u32);
  233. }
  234. if (wait_for_transfer_complete(control)) {
  235. error = -1;
  236. goto exit;
  237. }
  238. return 0;
  239. exit:
  240. /* error, reset the controller. */
  241. i2c_reset_controller(i2c_bus);
  242. return error;
  243. }
  244. static int tegra_i2c_write_data(u32 addr, u8 *data, u32 len)
  245. {
  246. int error;
  247. struct i2c_trans_info trans_info;
  248. trans_info.address = addr;
  249. trans_info.buf = data;
  250. trans_info.flags = I2C_IS_WRITE;
  251. trans_info.num_bytes = len;
  252. trans_info.is_10bit_address = 0;
  253. error = send_recv_packets(&i2c_controllers[i2c_bus_num], &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(u32 addr, u8 *data, u32 len)
  259. {
  260. int error;
  261. struct i2c_trans_info trans_info;
  262. trans_info.address = addr | 1;
  263. trans_info.buf = data;
  264. trans_info.flags = 0;
  265. trans_info.num_bytes = len;
  266. trans_info.is_10bit_address = 0;
  267. error = send_recv_packets(&i2c_controllers[i2c_bus_num], &trans_info);
  268. if (error)
  269. debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
  270. return error;
  271. }
  272. #ifndef CONFIG_OF_CONTROL
  273. #error "Please enable device tree support to use this driver"
  274. #endif
  275. unsigned int i2c_get_bus_speed(void)
  276. {
  277. return i2c_controllers[i2c_bus_num].speed;
  278. }
  279. int i2c_set_bus_speed(unsigned int speed)
  280. {
  281. struct i2c_bus *i2c_bus;
  282. i2c_bus = &i2c_controllers[i2c_bus_num];
  283. i2c_bus->speed = speed;
  284. i2c_init_controller(i2c_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. void i2c_init(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(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(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(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(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. int i2c_probe(uchar chip)
  422. {
  423. int rc;
  424. uchar reg;
  425. debug("i2c_probe: addr=0x%x\n", chip);
  426. reg = 0;
  427. rc = i2c_write_data(chip, &reg, 1);
  428. if (rc) {
  429. debug("Error probing 0x%x.\n", chip);
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. static int i2c_addr_ok(const uint addr, const int alen)
  435. {
  436. /* We support 7 or 10 bit addresses, so one or two bytes each */
  437. return alen == 1 || alen == 2;
  438. }
  439. /* Read bytes */
  440. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  441. {
  442. uint offset;
  443. int i;
  444. debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
  445. chip, addr, len);
  446. if (!i2c_addr_ok(addr, alen)) {
  447. debug("i2c_read: Bad address %x.%d.\n", addr, alen);
  448. return 1;
  449. }
  450. for (offset = 0; offset < len; offset++) {
  451. if (alen) {
  452. uchar data[alen];
  453. for (i = 0; i < alen; i++) {
  454. data[alen - i - 1] =
  455. (addr + offset) >> (8 * i);
  456. }
  457. if (i2c_write_data(chip, data, alen)) {
  458. debug("i2c_read: error sending (0x%x)\n",
  459. addr);
  460. return 1;
  461. }
  462. }
  463. if (i2c_read_data(chip, buffer + offset, 1)) {
  464. debug("i2c_read: error reading (0x%x)\n", addr);
  465. return 1;
  466. }
  467. }
  468. return 0;
  469. }
  470. /* Write bytes */
  471. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  472. {
  473. uint offset;
  474. int i;
  475. debug("i2c_write: chip=0x%x, addr=0x%x, len=0x%x\n",
  476. chip, addr, len);
  477. if (!i2c_addr_ok(addr, alen)) {
  478. debug("i2c_write: Bad address %x.%d.\n", addr, alen);
  479. return 1;
  480. }
  481. for (offset = 0; offset < len; offset++) {
  482. uchar data[alen + 1];
  483. for (i = 0; i < alen; i++)
  484. data[alen - i - 1] = (addr + offset) >> (8 * i);
  485. data[alen] = buffer[offset];
  486. if (i2c_write_data(chip, data, alen + 1)) {
  487. debug("i2c_write: error sending (0x%x)\n", addr);
  488. return 1;
  489. }
  490. }
  491. return 0;
  492. }
  493. #if defined(CONFIG_I2C_MULTI_BUS)
  494. /*
  495. * Functions for multiple I2C bus handling
  496. */
  497. unsigned int i2c_get_bus_num(void)
  498. {
  499. return i2c_bus_num;
  500. }
  501. int i2c_set_bus_num(unsigned int bus)
  502. {
  503. if (bus >= TEGRA_I2C_NUM_CONTROLLERS || !i2c_controllers[bus].inited)
  504. return -1;
  505. i2c_bus_num = bus;
  506. return 0;
  507. }
  508. #endif
  509. int tegra_i2c_get_dvc_bus_num(void)
  510. {
  511. int i;
  512. for (i = 0; i < CONFIG_SYS_MAX_I2C_BUS; i++) {
  513. struct i2c_bus *bus = &i2c_controllers[i];
  514. if (bus->inited && bus->is_dvc)
  515. return i;
  516. }
  517. return -1;
  518. }