tegra_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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/clk_rst.h>
  29. #include <asm/arch/clock.h>
  30. #include <asm/arch/funcmux.h>
  31. #include <asm/arch/gpio.h>
  32. #include <asm/arch/pinmux.h>
  33. #include <asm/arch/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 inited; /* bus is inited */
  46. };
  47. static struct i2c_bus i2c_controllers[TEGRA_I2C_NUM_CONTROLLERS];
  48. static void set_packet_mode(struct i2c_bus *i2c_bus)
  49. {
  50. u32 config;
  51. config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
  52. if (i2c_bus->is_dvc) {
  53. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  54. writel(config, &dvc->cnfg);
  55. } else {
  56. writel(config, &i2c_bus->regs->cnfg);
  57. /*
  58. * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
  59. * issues, i.e., some slaves may be wrongly detected.
  60. */
  61. setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
  62. }
  63. }
  64. static void i2c_reset_controller(struct i2c_bus *i2c_bus)
  65. {
  66. /* Reset I2C controller. */
  67. reset_periph(i2c_bus->periph_id, 1);
  68. /* re-program config register to packet mode */
  69. set_packet_mode(i2c_bus);
  70. }
  71. static void i2c_init_controller(struct i2c_bus *i2c_bus)
  72. {
  73. /*
  74. * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
  75. * here, in section 23.3.1, but in fact we seem to need a factor of
  76. * 16 to get the right frequency.
  77. */
  78. clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
  79. i2c_bus->speed * 2 * 8);
  80. /* Reset I2C controller. */
  81. i2c_reset_controller(i2c_bus);
  82. /* Configure I2C controller. */
  83. if (i2c_bus->is_dvc) { /* only for DVC I2C */
  84. struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
  85. setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
  86. }
  87. funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
  88. }
  89. static void send_packet_headers(
  90. struct i2c_bus *i2c_bus,
  91. struct i2c_trans_info *trans,
  92. u32 packet_id)
  93. {
  94. u32 data;
  95. /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
  96. data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
  97. data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
  98. data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
  99. writel(data, &i2c_bus->control->tx_fifo);
  100. debug("pkt header 1 sent (0x%x)\n", data);
  101. /* prepare header2 */
  102. data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
  103. writel(data, &i2c_bus->control->tx_fifo);
  104. debug("pkt header 2 sent (0x%x)\n", data);
  105. /* prepare IO specific header: configure the slave address */
  106. data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
  107. /* Enable Read if it is not a write transaction */
  108. if (!(trans->flags & I2C_IS_WRITE))
  109. data |= PKT_HDR3_READ_MODE_MASK;
  110. /* Write I2C specific header */
  111. writel(data, &i2c_bus->control->tx_fifo);
  112. debug("pkt header 3 sent (0x%x)\n", data);
  113. }
  114. static int wait_for_tx_fifo_empty(struct i2c_control *control)
  115. {
  116. u32 count;
  117. int timeout_us = I2C_TIMEOUT_USEC;
  118. while (timeout_us >= 0) {
  119. count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
  120. >> TX_FIFO_EMPTY_CNT_SHIFT;
  121. if (count == I2C_FIFO_DEPTH)
  122. return 1;
  123. udelay(10);
  124. timeout_us -= 10;
  125. }
  126. return 0;
  127. }
  128. static int wait_for_rx_fifo_notempty(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_FULL_CNT_MASK)
  134. >> TX_FIFO_FULL_CNT_SHIFT;
  135. if (count)
  136. return 1;
  137. udelay(10);
  138. timeout_us -= 10;
  139. }
  140. return 0;
  141. }
  142. static int wait_for_transfer_complete(struct i2c_control *control)
  143. {
  144. int int_status;
  145. int timeout_us = I2C_TIMEOUT_USEC;
  146. while (timeout_us >= 0) {
  147. int_status = readl(&control->int_status);
  148. if (int_status & I2C_INT_NO_ACK_MASK)
  149. return -int_status;
  150. if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
  151. return -int_status;
  152. if (int_status & I2C_INT_XFER_COMPLETE_MASK)
  153. return 0;
  154. udelay(10);
  155. timeout_us -= 10;
  156. }
  157. return -1;
  158. }
  159. static int send_recv_packets(struct i2c_bus *i2c_bus,
  160. struct i2c_trans_info *trans)
  161. {
  162. struct i2c_control *control = i2c_bus->control;
  163. u32 int_status;
  164. u32 words;
  165. u8 *dptr;
  166. u32 local;
  167. uchar last_bytes;
  168. int error = 0;
  169. int is_write = trans->flags & I2C_IS_WRITE;
  170. /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
  171. int_status = readl(&control->int_status);
  172. writel(int_status, &control->int_status);
  173. send_packet_headers(i2c_bus, trans, 1);
  174. words = DIV_ROUND_UP(trans->num_bytes, 4);
  175. last_bytes = trans->num_bytes & 3;
  176. dptr = trans->buf;
  177. while (words) {
  178. u32 *wptr = (u32 *)dptr;
  179. if (is_write) {
  180. /* deal with word alignment */
  181. if ((unsigned)dptr & 3) {
  182. memcpy(&local, dptr, sizeof(u32));
  183. writel(local, &control->tx_fifo);
  184. debug("pkt data sent (0x%x)\n", local);
  185. } else {
  186. writel(*wptr, &control->tx_fifo);
  187. debug("pkt data sent (0x%x)\n", *wptr);
  188. }
  189. if (!wait_for_tx_fifo_empty(control)) {
  190. error = -1;
  191. goto exit;
  192. }
  193. } else {
  194. if (!wait_for_rx_fifo_notempty(control)) {
  195. error = -1;
  196. goto exit;
  197. }
  198. /*
  199. * for the last word, we read into our local buffer,
  200. * in case that caller did not provide enough buffer.
  201. */
  202. local = readl(&control->rx_fifo);
  203. if ((words == 1) && last_bytes)
  204. memcpy(dptr, (char *)&local, last_bytes);
  205. else if ((unsigned)dptr & 3)
  206. memcpy(dptr, &local, sizeof(u32));
  207. else
  208. *wptr = local;
  209. debug("pkt data received (0x%x)\n", local);
  210. }
  211. words--;
  212. dptr += sizeof(u32);
  213. }
  214. if (wait_for_transfer_complete(control)) {
  215. error = -1;
  216. goto exit;
  217. }
  218. return 0;
  219. exit:
  220. /* error, reset the controller. */
  221. i2c_reset_controller(i2c_bus);
  222. return error;
  223. }
  224. static int tegra20_i2c_write_data(u32 addr, u8 *data, u32 len)
  225. {
  226. int error;
  227. struct i2c_trans_info trans_info;
  228. trans_info.address = addr;
  229. trans_info.buf = data;
  230. trans_info.flags = I2C_IS_WRITE;
  231. trans_info.num_bytes = len;
  232. trans_info.is_10bit_address = 0;
  233. error = send_recv_packets(&i2c_controllers[i2c_bus_num], &trans_info);
  234. if (error)
  235. debug("tegra20_i2c_write_data: Error (%d) !!!\n", error);
  236. return error;
  237. }
  238. static int tegra20_i2c_read_data(u32 addr, u8 *data, u32 len)
  239. {
  240. int error;
  241. struct i2c_trans_info trans_info;
  242. trans_info.address = addr | 1;
  243. trans_info.buf = data;
  244. trans_info.flags = 0;
  245. trans_info.num_bytes = len;
  246. trans_info.is_10bit_address = 0;
  247. error = send_recv_packets(&i2c_controllers[i2c_bus_num], &trans_info);
  248. if (error)
  249. debug("tegra20_i2c_read_data: Error (%d) !!!\n", error);
  250. return error;
  251. }
  252. #ifndef CONFIG_OF_CONTROL
  253. #error "Please enable device tree support to use this driver"
  254. #endif
  255. unsigned int i2c_get_bus_speed(void)
  256. {
  257. return i2c_controllers[i2c_bus_num].speed;
  258. }
  259. int i2c_set_bus_speed(unsigned int speed)
  260. {
  261. struct i2c_bus *i2c_bus;
  262. i2c_bus = &i2c_controllers[i2c_bus_num];
  263. i2c_bus->speed = speed;
  264. i2c_init_controller(i2c_bus);
  265. return 0;
  266. }
  267. static int i2c_get_config(const void *blob, int node, struct i2c_bus *i2c_bus)
  268. {
  269. i2c_bus->regs = (struct i2c_ctlr *)fdtdec_get_addr(blob, node, "reg");
  270. /*
  271. * We don't have a binding for pinmux yet. Leave it out for now. So
  272. * far no one needs anything other than the default.
  273. */
  274. i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
  275. i2c_bus->speed = fdtdec_get_int(blob, node, "clock-frequency", 0);
  276. i2c_bus->periph_id = clock_decode_periph_id(blob, node);
  277. /*
  278. * We can't specify the pinmux config in the fdt, so I2C2 will not
  279. * work on Seaboard. It normally has no devices on it anyway.
  280. * You could add in this little hack if you need to use it.
  281. * The correct solution is a pinmux binding in the fdt.
  282. *
  283. * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
  284. * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
  285. */
  286. if (i2c_bus->periph_id == -1)
  287. return -FDT_ERR_NOTFOUND;
  288. return 0;
  289. }
  290. /*
  291. * Process a list of nodes, adding them to our list of I2C ports.
  292. *
  293. * @param blob fdt blob
  294. * @param node_list list of nodes to process (any <=0 are ignored)
  295. * @param count number of nodes to process
  296. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  297. * @return 0 if ok, -1 on error
  298. */
  299. static int process_nodes(const void *blob, int node_list[], int count,
  300. int is_dvc)
  301. {
  302. struct i2c_bus *i2c_bus;
  303. int i;
  304. /* build the i2c_controllers[] for each controller */
  305. for (i = 0; i < count; i++) {
  306. int node = node_list[i];
  307. if (node <= 0)
  308. continue;
  309. i2c_bus = &i2c_controllers[i];
  310. i2c_bus->id = i;
  311. if (i2c_get_config(blob, node, i2c_bus)) {
  312. printf("i2c_init_board: failed to decode bus %d\n", i);
  313. return -1;
  314. }
  315. i2c_bus->is_dvc = is_dvc;
  316. if (is_dvc) {
  317. i2c_bus->control =
  318. &((struct dvc_ctlr *)i2c_bus->regs)->control;
  319. } else {
  320. i2c_bus->control = &i2c_bus->regs->control;
  321. }
  322. debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
  323. is_dvc ? "dvc" : "i2c", i, i2c_bus->regs,
  324. i2c_bus->periph_id, i2c_bus->speed);
  325. i2c_init_controller(i2c_bus);
  326. debug("ok\n");
  327. i2c_bus->inited = 1;
  328. /* Mark position as used */
  329. node_list[i] = -1;
  330. }
  331. return 0;
  332. }
  333. /* Sadly there is no error return from this function */
  334. void i2c_init_board(void)
  335. {
  336. int node_list[TEGRA_I2C_NUM_CONTROLLERS];
  337. const void *blob = gd->fdt_blob;
  338. int count;
  339. /* First get the normal i2c ports */
  340. count = fdtdec_find_aliases_for_id(blob, "i2c",
  341. COMPAT_NVIDIA_TEGRA20_I2C, node_list,
  342. TEGRA_I2C_NUM_CONTROLLERS);
  343. if (process_nodes(blob, node_list, count, 0))
  344. return;
  345. /* Now look for dvc ports */
  346. count = fdtdec_add_aliases_for_id(blob, "i2c",
  347. COMPAT_NVIDIA_TEGRA20_DVC, node_list,
  348. TEGRA_I2C_NUM_CONTROLLERS);
  349. if (process_nodes(blob, node_list, count, 1))
  350. return;
  351. }
  352. void i2c_init(int speed, int slaveaddr)
  353. {
  354. /* This will override the speed selected in the fdt for that port */
  355. debug("i2c_init(speed=%u, slaveaddr=0x%x)\n", speed, slaveaddr);
  356. i2c_set_bus_speed(speed);
  357. }
  358. /* i2c write version without the register address */
  359. int i2c_write_data(uchar chip, uchar *buffer, int len)
  360. {
  361. int rc;
  362. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  363. debug("write_data: ");
  364. /* use rc for counter */
  365. for (rc = 0; rc < len; ++rc)
  366. debug(" 0x%02x", buffer[rc]);
  367. debug("\n");
  368. /* Shift 7-bit address over for lower-level i2c functions */
  369. rc = tegra20_i2c_write_data(chip << 1, buffer, len);
  370. if (rc)
  371. debug("i2c_write_data(): rc=%d\n", rc);
  372. return rc;
  373. }
  374. /* i2c read version without the register address */
  375. int i2c_read_data(uchar chip, uchar *buffer, int len)
  376. {
  377. int rc;
  378. debug("inside i2c_read_data():\n");
  379. /* Shift 7-bit address over for lower-level i2c functions */
  380. rc = tegra20_i2c_read_data(chip << 1, buffer, len);
  381. if (rc) {
  382. debug("i2c_read_data(): rc=%d\n", rc);
  383. return rc;
  384. }
  385. debug("i2c_read_data: ");
  386. /* reuse rc for counter*/
  387. for (rc = 0; rc < len; ++rc)
  388. debug(" 0x%02x", buffer[rc]);
  389. debug("\n");
  390. return 0;
  391. }
  392. /* Probe to see if a chip is present. */
  393. int i2c_probe(uchar chip)
  394. {
  395. int rc;
  396. uchar reg;
  397. debug("i2c_probe: addr=0x%x\n", chip);
  398. reg = 0;
  399. rc = i2c_write_data(chip, &reg, 1);
  400. if (rc) {
  401. debug("Error probing 0x%x.\n", chip);
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. static int i2c_addr_ok(const uint addr, const int alen)
  407. {
  408. /* We support 7 or 10 bit addresses, so one or two bytes each */
  409. return alen == 1 || alen == 2;
  410. }
  411. /* Read bytes */
  412. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  413. {
  414. uint offset;
  415. int i;
  416. debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
  417. chip, addr, len);
  418. if (!i2c_addr_ok(addr, alen)) {
  419. debug("i2c_read: Bad address %x.%d.\n", addr, alen);
  420. return 1;
  421. }
  422. for (offset = 0; offset < len; offset++) {
  423. if (alen) {
  424. uchar data[alen];
  425. for (i = 0; i < alen; i++) {
  426. data[alen - i - 1] =
  427. (addr + offset) >> (8 * i);
  428. }
  429. if (i2c_write_data(chip, data, alen)) {
  430. debug("i2c_read: error sending (0x%x)\n",
  431. addr);
  432. return 1;
  433. }
  434. }
  435. if (i2c_read_data(chip, buffer + offset, 1)) {
  436. debug("i2c_read: error reading (0x%x)\n", addr);
  437. return 1;
  438. }
  439. }
  440. return 0;
  441. }
  442. /* Write bytes */
  443. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  444. {
  445. uint offset;
  446. int i;
  447. debug("i2c_write: chip=0x%x, addr=0x%x, len=0x%x\n",
  448. chip, addr, len);
  449. if (!i2c_addr_ok(addr, alen)) {
  450. debug("i2c_write: Bad address %x.%d.\n", addr, alen);
  451. return 1;
  452. }
  453. for (offset = 0; offset < len; offset++) {
  454. uchar data[alen + 1];
  455. for (i = 0; i < alen; i++)
  456. data[alen - i - 1] = (addr + offset) >> (8 * i);
  457. data[alen] = buffer[offset];
  458. if (i2c_write_data(chip, data, alen + 1)) {
  459. debug("i2c_write: error sending (0x%x)\n", addr);
  460. return 1;
  461. }
  462. }
  463. return 0;
  464. }
  465. #if defined(CONFIG_I2C_MULTI_BUS)
  466. /*
  467. * Functions for multiple I2C bus handling
  468. */
  469. unsigned int i2c_get_bus_num(void)
  470. {
  471. return i2c_bus_num;
  472. }
  473. int i2c_set_bus_num(unsigned int bus)
  474. {
  475. if (bus >= TEGRA_I2C_NUM_CONTROLLERS || !i2c_controllers[bus].inited)
  476. return -1;
  477. i2c_bus_num = bus;
  478. return 0;
  479. }
  480. #endif
  481. int tegra_i2c_get_dvc_bus_num(void)
  482. {
  483. int i;
  484. for (i = 0; i < CONFIG_SYS_MAX_I2C_BUS; i++) {
  485. struct i2c_bus *bus = &i2c_controllers[i];
  486. if (bus->inited && bus->is_dvc)
  487. return i;
  488. }
  489. return -1;
  490. }