mxc_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * i2c driver for Freescale i.MX series
  3. *
  4. * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  5. * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
  6. *
  7. * Based on i2c-imx.c from linux kernel:
  8. * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
  9. * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
  10. * Copyright (C) 2007 RightHand Technologies, Inc.
  11. * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
  12. *
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <asm/arch/clock.h>
  34. #include <asm/arch/imx-regs.h>
  35. #include <asm/errno.h>
  36. #include <asm/io.h>
  37. #include <i2c.h>
  38. #include <watchdog.h>
  39. struct mxc_i2c_regs {
  40. uint32_t iadr;
  41. uint32_t ifdr;
  42. uint32_t i2cr;
  43. uint32_t i2sr;
  44. uint32_t i2dr;
  45. };
  46. #define I2CR_IEN (1 << 7)
  47. #define I2CR_IIEN (1 << 6)
  48. #define I2CR_MSTA (1 << 5)
  49. #define I2CR_MTX (1 << 4)
  50. #define I2CR_TX_NO_AK (1 << 3)
  51. #define I2CR_RSTA (1 << 2)
  52. #define I2SR_ICF (1 << 7)
  53. #define I2SR_IBB (1 << 5)
  54. #define I2SR_IAL (1 << 4)
  55. #define I2SR_IIF (1 << 1)
  56. #define I2SR_RX_NO_AK (1 << 0)
  57. #if defined(CONFIG_HARD_I2C) && !defined(CONFIG_SYS_I2C_BASE)
  58. #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
  59. #endif
  60. static u16 i2c_clk_div[50][2] = {
  61. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  62. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  63. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  64. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  65. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  66. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  67. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  68. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  69. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  70. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  71. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  72. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  73. { 3072, 0x1E }, { 3840, 0x1F }
  74. };
  75. /*
  76. * Calculate and set proper clock divider
  77. */
  78. static uint8_t i2c_imx_get_clk(unsigned int rate)
  79. {
  80. unsigned int i2c_clk_rate;
  81. unsigned int div;
  82. u8 clk_div;
  83. #if defined(CONFIG_MX31)
  84. struct clock_control_regs *sc_regs =
  85. (struct clock_control_regs *)CCM_BASE;
  86. /* start the required I2C clock */
  87. writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
  88. &sc_regs->cgr0);
  89. #endif
  90. /* Divider value calculation */
  91. i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
  92. div = (i2c_clk_rate + rate - 1) / rate;
  93. if (div < i2c_clk_div[0][0])
  94. clk_div = 0;
  95. else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
  96. clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
  97. else
  98. for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
  99. ;
  100. /* Store divider value */
  101. return clk_div;
  102. }
  103. /*
  104. * Set I2C Bus speed
  105. */
  106. int bus_i2c_set_bus_speed(void *base, int speed)
  107. {
  108. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
  109. u8 clk_idx = i2c_imx_get_clk(speed);
  110. u8 idx = i2c_clk_div[clk_idx][1];
  111. /* Store divider value */
  112. writeb(idx, &i2c_regs->ifdr);
  113. /* Reset module */
  114. writeb(0, &i2c_regs->i2cr);
  115. writeb(0, &i2c_regs->i2sr);
  116. return 0;
  117. }
  118. /*
  119. * Get I2C Speed
  120. */
  121. unsigned int bus_i2c_get_bus_speed(void *base)
  122. {
  123. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
  124. u8 clk_idx = readb(&i2c_regs->ifdr);
  125. u8 clk_div;
  126. for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
  127. ;
  128. return mxc_get_clock(MXC_I2C_CLK) / i2c_clk_div[clk_div][0];
  129. }
  130. #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
  131. #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
  132. #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
  133. static int wait_for_sr_state(struct mxc_i2c_regs *i2c_regs, unsigned state)
  134. {
  135. unsigned sr;
  136. ulong elapsed;
  137. ulong start_time = get_timer(0);
  138. for (;;) {
  139. sr = readb(&i2c_regs->i2sr);
  140. if (sr & I2SR_IAL) {
  141. writeb(sr & ~I2SR_IAL, &i2c_regs->i2sr);
  142. printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
  143. __func__, sr, readb(&i2c_regs->i2cr), state);
  144. return -ERESTART;
  145. }
  146. if ((sr & (state >> 8)) == (unsigned char)state)
  147. return sr;
  148. WATCHDOG_RESET();
  149. elapsed = get_timer(start_time);
  150. if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
  151. break;
  152. }
  153. printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
  154. sr, readb(&i2c_regs->i2cr), state);
  155. return -ETIMEDOUT;
  156. }
  157. static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
  158. {
  159. int ret;
  160. writeb(0, &i2c_regs->i2sr);
  161. writeb(byte, &i2c_regs->i2dr);
  162. ret = wait_for_sr_state(i2c_regs, ST_IIF);
  163. if (ret < 0)
  164. return ret;
  165. if (ret & I2SR_RX_NO_AK)
  166. return -ENODEV;
  167. return 0;
  168. }
  169. /*
  170. * Stop I2C transaction
  171. */
  172. static void i2c_imx_stop(struct mxc_i2c_regs *i2c_regs)
  173. {
  174. int ret;
  175. unsigned int temp = readb(&i2c_regs->i2cr);
  176. temp &= ~(I2CR_MSTA | I2CR_MTX);
  177. writeb(temp, &i2c_regs->i2cr);
  178. ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
  179. if (ret < 0)
  180. printf("%s:trigger stop failed\n", __func__);
  181. }
  182. /*
  183. * Send start signal, chip address and
  184. * write register address
  185. */
  186. static int i2c_init_transfer_(struct mxc_i2c_regs *i2c_regs,
  187. uchar chip, uint addr, int alen)
  188. {
  189. unsigned int temp;
  190. int ret;
  191. /* Enable I2C controller */
  192. if (!(readb(&i2c_regs->i2cr) & I2CR_IEN)) {
  193. writeb(I2CR_IEN, &i2c_regs->i2cr);
  194. /* Wait for controller to be stable */
  195. udelay(50);
  196. }
  197. if (readb(&i2c_regs->iadr) == (chip << 1))
  198. writeb((chip << 1) ^ 2, &i2c_regs->iadr);
  199. writeb(0, &i2c_regs->i2sr);
  200. ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
  201. if (ret < 0)
  202. return ret;
  203. /* Start I2C transaction */
  204. temp = readb(&i2c_regs->i2cr);
  205. temp |= I2CR_MSTA;
  206. writeb(temp, &i2c_regs->i2cr);
  207. ret = wait_for_sr_state(i2c_regs, ST_BUS_BUSY);
  208. if (ret < 0)
  209. return ret;
  210. temp |= I2CR_MTX | I2CR_TX_NO_AK;
  211. writeb(temp, &i2c_regs->i2cr);
  212. /* write slave address */
  213. ret = tx_byte(i2c_regs, chip << 1);
  214. if (ret < 0)
  215. return ret;
  216. while (alen--) {
  217. ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
  218. if (ret < 0)
  219. return ret;
  220. }
  221. return 0;
  222. }
  223. static int i2c_idle_bus(void *base);
  224. static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs,
  225. uchar chip, uint addr, int alen)
  226. {
  227. int retry;
  228. int ret;
  229. for (retry = 0; retry < 3; retry++) {
  230. ret = i2c_init_transfer_(i2c_regs, chip, addr, alen);
  231. if (ret >= 0)
  232. return 0;
  233. i2c_imx_stop(i2c_regs);
  234. if (ret == -ENODEV)
  235. return ret;
  236. printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
  237. retry);
  238. if (ret != -ERESTART)
  239. writeb(0, &i2c_regs->i2cr); /* Disable controller */
  240. udelay(100);
  241. if (i2c_idle_bus(i2c_regs) < 0)
  242. break;
  243. }
  244. printf("%s: give up i2c_regs=%p\n", __func__, i2c_regs);
  245. return ret;
  246. }
  247. /*
  248. * Read data from I2C device
  249. */
  250. int bus_i2c_read(void *base, uchar chip, uint addr, int alen, uchar *buf,
  251. int len)
  252. {
  253. int ret;
  254. unsigned int temp;
  255. int i;
  256. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
  257. ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
  258. if (ret < 0)
  259. return ret;
  260. temp = readb(&i2c_regs->i2cr);
  261. temp |= I2CR_RSTA;
  262. writeb(temp, &i2c_regs->i2cr);
  263. ret = tx_byte(i2c_regs, (chip << 1) | 1);
  264. if (ret < 0) {
  265. i2c_imx_stop(i2c_regs);
  266. return ret;
  267. }
  268. /* setup bus to read data */
  269. temp = readb(&i2c_regs->i2cr);
  270. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  271. if (len == 1)
  272. temp |= I2CR_TX_NO_AK;
  273. writeb(temp, &i2c_regs->i2cr);
  274. writeb(0, &i2c_regs->i2sr);
  275. readb(&i2c_regs->i2dr); /* dummy read to clear ICF */
  276. /* read data */
  277. for (i = 0; i < len; i++) {
  278. ret = wait_for_sr_state(i2c_regs, ST_IIF);
  279. if (ret < 0) {
  280. i2c_imx_stop(i2c_regs);
  281. return ret;
  282. }
  283. /*
  284. * It must generate STOP before read I2DR to prevent
  285. * controller from generating another clock cycle
  286. */
  287. if (i == (len - 1)) {
  288. i2c_imx_stop(i2c_regs);
  289. } else if (i == (len - 2)) {
  290. temp = readb(&i2c_regs->i2cr);
  291. temp |= I2CR_TX_NO_AK;
  292. writeb(temp, &i2c_regs->i2cr);
  293. }
  294. writeb(0, &i2c_regs->i2sr);
  295. buf[i] = readb(&i2c_regs->i2dr);
  296. }
  297. i2c_imx_stop(i2c_regs);
  298. return 0;
  299. }
  300. /*
  301. * Write data to I2C device
  302. */
  303. int bus_i2c_write(void *base, uchar chip, uint addr, int alen,
  304. const uchar *buf, int len)
  305. {
  306. int ret;
  307. int i;
  308. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)base;
  309. ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
  310. if (ret < 0)
  311. return ret;
  312. for (i = 0; i < len; i++) {
  313. ret = tx_byte(i2c_regs, buf[i]);
  314. if (ret < 0)
  315. break;
  316. }
  317. i2c_imx_stop(i2c_regs);
  318. return ret;
  319. }
  320. struct i2c_parms {
  321. void *base;
  322. void *idle_bus_data;
  323. int (*idle_bus_fn)(void *p);
  324. };
  325. struct sram_data {
  326. unsigned curr_i2c_bus;
  327. struct i2c_parms i2c_data[3];
  328. };
  329. /*
  330. * For SPL boot some boards need i2c before SDRAM is initialized so force
  331. * variables to live in SRAM
  332. */
  333. static struct sram_data __attribute__((section(".data"))) srdata;
  334. void *get_base(void)
  335. {
  336. #ifdef CONFIG_SYS_I2C_BASE
  337. #ifdef CONFIG_I2C_MULTI_BUS
  338. void *ret = srdata.i2c_data[srdata.curr_i2c_bus].base;
  339. if (ret)
  340. return ret;
  341. #endif
  342. return (void *)CONFIG_SYS_I2C_BASE;
  343. #elif defined(CONFIG_I2C_MULTI_BUS)
  344. return srdata.i2c_data[srdata.curr_i2c_bus].base;
  345. #else
  346. return srdata.i2c_data[0].base;
  347. #endif
  348. }
  349. static struct i2c_parms *i2c_get_parms(void *base)
  350. {
  351. int i = 0;
  352. struct i2c_parms *p = srdata.i2c_data;
  353. while (i < ARRAY_SIZE(srdata.i2c_data)) {
  354. if (p->base == base)
  355. return p;
  356. p++;
  357. i++;
  358. }
  359. printf("Invalid I2C base: %p\n", base);
  360. return NULL;
  361. }
  362. static int i2c_idle_bus(void *base)
  363. {
  364. struct i2c_parms *p = i2c_get_parms(base);
  365. if (p && p->idle_bus_fn)
  366. return p->idle_bus_fn(p->idle_bus_data);
  367. return 0;
  368. }
  369. #ifdef CONFIG_I2C_MULTI_BUS
  370. unsigned int i2c_get_bus_num(void)
  371. {
  372. return srdata.curr_i2c_bus;
  373. }
  374. int i2c_set_bus_num(unsigned bus_idx)
  375. {
  376. if (bus_idx >= ARRAY_SIZE(srdata.i2c_data))
  377. return -1;
  378. if (!srdata.i2c_data[bus_idx].base)
  379. return -1;
  380. srdata.curr_i2c_bus = bus_idx;
  381. return 0;
  382. }
  383. #endif
  384. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  385. {
  386. return bus_i2c_read(get_base(), chip, addr, alen, buf, len);
  387. }
  388. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  389. {
  390. return bus_i2c_write(get_base(), chip, addr, alen, buf, len);
  391. }
  392. /*
  393. * Test if a chip at a given address responds (probe the chip)
  394. */
  395. int i2c_probe(uchar chip)
  396. {
  397. return bus_i2c_write(get_base(), chip, 0, 0, NULL, 0);
  398. }
  399. void bus_i2c_init(void *base, int speed, int unused,
  400. int (*idle_bus_fn)(void *p), void *idle_bus_data)
  401. {
  402. int i = 0;
  403. struct i2c_parms *p = srdata.i2c_data;
  404. if (!base)
  405. return;
  406. for (;;) {
  407. if (!p->base || (p->base == base)) {
  408. p->base = base;
  409. if (idle_bus_fn) {
  410. p->idle_bus_fn = idle_bus_fn;
  411. p->idle_bus_data = idle_bus_data;
  412. }
  413. break;
  414. }
  415. p++;
  416. i++;
  417. if (i >= ARRAY_SIZE(srdata.i2c_data))
  418. return;
  419. }
  420. bus_i2c_set_bus_speed(base, speed);
  421. }
  422. /*
  423. * Init I2C Bus
  424. */
  425. void i2c_init(int speed, int unused)
  426. {
  427. bus_i2c_init(get_base(), speed, unused, NULL, NULL);
  428. }
  429. /*
  430. * Set I2C Speed
  431. */
  432. int i2c_set_bus_speed(unsigned int speed)
  433. {
  434. return bus_i2c_set_bus_speed(get_base(), speed);
  435. }
  436. /*
  437. * Get I2C Speed
  438. */
  439. unsigned int i2c_get_bus_speed(void)
  440. {
  441. return bus_i2c_get_bus_speed(get_base());
  442. }