fsl_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright 2006,2009 Freescale Semiconductor, Inc.
  3. *
  4. * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
  5. * Changes for multibus/multiadapter I2C support.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * Version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <i2c.h> /* Functional interface */
  24. #include <asm/io.h>
  25. #include <asm/fsl_i2c.h> /* HW definitions */
  26. /* The maximum number of microseconds we will wait until another master has
  27. * released the bus. If not defined in the board header file, then use a
  28. * generic value.
  29. */
  30. #ifndef CONFIG_I2C_MBB_TIMEOUT
  31. #define CONFIG_I2C_MBB_TIMEOUT 100000
  32. #endif
  33. /* The maximum number of microseconds we will wait for a read or write
  34. * operation to complete. If not defined in the board header file, then use a
  35. * generic value.
  36. */
  37. #ifndef CONFIG_I2C_TIMEOUT
  38. #define CONFIG_I2C_TIMEOUT 10000
  39. #endif
  40. #define I2C_READ_BIT 1
  41. #define I2C_WRITE_BIT 0
  42. DECLARE_GLOBAL_DATA_PTR;
  43. static const struct fsl_i2c *i2c_dev[2] = {
  44. (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
  45. #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
  46. (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET)
  47. #endif
  48. };
  49. /* I2C speed map for a DFSR value of 1 */
  50. /*
  51. * Map I2C frequency dividers to FDR and DFSR values
  52. *
  53. * This structure is used to define the elements of a table that maps I2C
  54. * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
  55. * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
  56. * Sampling Rate (DFSR) registers.
  57. *
  58. * The actual table should be defined in the board file, and it must be called
  59. * fsl_i2c_speed_map[].
  60. *
  61. * The last entry of the table must have a value of {-1, X}, where X is same
  62. * FDR/DFSR values as the second-to-last entry. This guarantees that any
  63. * search through the array will always find a match.
  64. *
  65. * The values of the divider must be in increasing numerical order, i.e.
  66. * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
  67. *
  68. * For this table, the values are based on a value of 1 for the DFSR
  69. * register. See the application note AN2919 "Determining the I2C Frequency
  70. * Divider Ratio for SCL"
  71. *
  72. * ColdFire I2C frequency dividers for FDR values are different from
  73. * PowerPC. The protocol to use the I2C module is still the same.
  74. * A different table is defined and are based on MCF5xxx user manual.
  75. *
  76. */
  77. static const struct {
  78. unsigned short divider;
  79. u8 fdr;
  80. } fsl_i2c_speed_map[] = {
  81. #ifdef __M68K__
  82. {20, 32}, {22, 33}, {24, 34}, {26, 35},
  83. {28, 0}, {28, 36}, {30, 1}, {32, 37},
  84. {34, 2}, {36, 38}, {40, 3}, {40, 39},
  85. {44, 4}, {48, 5}, {48, 40}, {56, 6},
  86. {56, 41}, {64, 42}, {68, 7}, {72, 43},
  87. {80, 8}, {80, 44}, {88, 9}, {96, 41},
  88. {104, 10}, {112, 42}, {128, 11}, {128, 43},
  89. {144, 12}, {160, 13}, {160, 48}, {192, 14},
  90. {192, 49}, {224, 50}, {240, 15}, {256, 51},
  91. {288, 16}, {320, 17}, {320, 52}, {384, 18},
  92. {384, 53}, {448, 54}, {480, 19}, {512, 55},
  93. {576, 20}, {640, 21}, {640, 56}, {768, 22},
  94. {768, 57}, {960, 23}, {896, 58}, {1024, 59},
  95. {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
  96. {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
  97. {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
  98. {-1, 31}
  99. #endif
  100. };
  101. /**
  102. * Set the I2C bus speed for a given I2C device
  103. *
  104. * @param dev: the I2C device
  105. * @i2c_clk: I2C bus clock frequency
  106. * @speed: the desired speed of the bus
  107. *
  108. * The I2C device must be stopped before calling this function.
  109. *
  110. * The return value is the actual bus speed that is set.
  111. */
  112. static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev,
  113. unsigned int i2c_clk, unsigned int speed)
  114. {
  115. unsigned short divider = min(i2c_clk / speed, (unsigned short) -1);
  116. /*
  117. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  118. * is equal to or lower than the requested speed. That means that we
  119. * want the first divider that is equal to or greater than the
  120. * calculated divider.
  121. */
  122. #ifdef __PPC__
  123. u8 dfsr, fdr = 0x31; /* Default if no FDR found */
  124. /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
  125. unsigned short a, b, ga, gb;
  126. unsigned long c_div, est_div;
  127. #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
  128. dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
  129. #else
  130. /* Condition 1: dfsr <= 50/T */
  131. dfsr = (5 * (i2c_clk / 1000)) / 100000;
  132. #endif
  133. #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
  134. fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
  135. speed = i2c_clk / divider; /* Fake something */
  136. #else
  137. debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
  138. if (!dfsr)
  139. dfsr = 1;
  140. est_div = ~0;
  141. for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
  142. for (gb = 0; gb < 8; gb++) {
  143. b = 16 << gb;
  144. c_div = b * (a + ((3*dfsr)/b)*2);
  145. if ((c_div > divider) && (c_div < est_div)) {
  146. unsigned short bin_gb, bin_ga;
  147. est_div = c_div;
  148. bin_gb = gb << 2;
  149. bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
  150. fdr = bin_gb | bin_ga;
  151. speed = i2c_clk / est_div;
  152. debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
  153. "a:%d, b:%d, speed:%d\n",
  154. fdr, est_div, ga, gb, a, b, speed);
  155. /* Condition 2 not accounted for */
  156. debug("Tr <= %d ns\n",
  157. (b - 3 * dfsr) * 1000000 /
  158. (i2c_clk / 1000));
  159. }
  160. }
  161. if (a == 20)
  162. a += 2;
  163. if (a == 24)
  164. a += 4;
  165. }
  166. debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
  167. debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
  168. #endif
  169. writeb(dfsr, &dev->dfsrr); /* set default filter */
  170. writeb(fdr, &dev->fdr); /* set bus speed */
  171. #else
  172. unsigned int i;
  173. for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
  174. if (fsl_i2c_speed_map[i].divider >= divider) {
  175. u8 fdr;
  176. fdr = fsl_i2c_speed_map[i].fdr;
  177. speed = i2c_clk / fsl_i2c_speed_map[i].divider;
  178. writeb(fdr, &dev->fdr); /* set bus speed */
  179. break;
  180. }
  181. #endif
  182. return speed;
  183. }
  184. static unsigned int get_i2c_clock(int bus)
  185. {
  186. if (bus)
  187. return gd->arch.i2c2_clk; /* I2C2 clock */
  188. else
  189. return gd->arch.i2c1_clk; /* I2C1 clock */
  190. }
  191. static int fsl_i2c_fixup(const struct fsl_i2c *dev)
  192. {
  193. const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
  194. unsigned long long timeval = 0;
  195. int ret = -1;
  196. unsigned int flags = 0;
  197. #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
  198. unsigned int svr = get_svr();
  199. if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
  200. (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
  201. flags = I2C_CR_BIT6;
  202. #endif
  203. writeb(I2C_CR_MEN | I2C_CR_MSTA, &dev->cr);
  204. timeval = get_ticks();
  205. while (!(readb(&dev->sr) & I2C_SR_MBB)) {
  206. if ((get_ticks() - timeval) > timeout)
  207. goto err;
  208. }
  209. if (readb(&dev->sr) & I2C_SR_MAL) {
  210. /* SDA is stuck low */
  211. writeb(0, &dev->cr);
  212. udelay(100);
  213. writeb(I2C_CR_MSTA | flags, &dev->cr);
  214. writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &dev->cr);
  215. }
  216. readb(&dev->dr);
  217. timeval = get_ticks();
  218. while (!(readb(&dev->sr) & I2C_SR_MIF)) {
  219. if ((get_ticks() - timeval) > timeout)
  220. goto err;
  221. }
  222. ret = 0;
  223. err:
  224. writeb(I2C_CR_MEN | flags, &dev->cr);
  225. writeb(0, &dev->sr);
  226. udelay(100);
  227. return ret;
  228. }
  229. static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  230. {
  231. const struct fsl_i2c *dev;
  232. const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
  233. unsigned long long timeval;
  234. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  235. /* Call board specific i2c bus reset routine before accessing the
  236. * environment, which might be in a chip on that bus. For details
  237. * about this problem see doc/I2C_Edge_Conditions.
  238. */
  239. i2c_init_board();
  240. #endif
  241. dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  242. writeb(0, &dev->cr); /* stop I2C controller */
  243. udelay(5); /* let it shutdown in peace */
  244. set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
  245. writeb(slaveadd << 1, &dev->adr);/* write slave address */
  246. writeb(0x0, &dev->sr); /* clear status register */
  247. writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
  248. timeval = get_ticks();
  249. while (readb(&dev->sr) & I2C_SR_MBB) {
  250. if ((get_ticks() - timeval) < timeout)
  251. continue;
  252. if (fsl_i2c_fixup(dev))
  253. debug("i2c_init: BUS#%d failed to init\n",
  254. adap->hwadapnr);
  255. break;
  256. }
  257. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  258. /* Call board specific i2c bus reset routine AFTER the bus has been
  259. * initialized. Use either this callpoint or i2c_init_board;
  260. * which is called before i2c_init operations.
  261. * For details about this problem see doc/I2C_Edge_Conditions.
  262. */
  263. i2c_board_late_init();
  264. #endif
  265. }
  266. static int
  267. i2c_wait4bus(struct i2c_adapter *adap)
  268. {
  269. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  270. unsigned long long timeval = get_ticks();
  271. const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
  272. while (readb(&dev->sr) & I2C_SR_MBB) {
  273. if ((get_ticks() - timeval) > timeout)
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. static __inline__ int
  279. i2c_wait(struct i2c_adapter *adap, int write)
  280. {
  281. u32 csr;
  282. unsigned long long timeval = get_ticks();
  283. const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
  284. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  285. do {
  286. csr = readb(&dev->sr);
  287. if (!(csr & I2C_SR_MIF))
  288. continue;
  289. /* Read again to allow register to stabilise */
  290. csr = readb(&dev->sr);
  291. writeb(0x0, &dev->sr);
  292. if (csr & I2C_SR_MAL) {
  293. debug("i2c_wait: MAL\n");
  294. return -1;
  295. }
  296. if (!(csr & I2C_SR_MCF)) {
  297. debug("i2c_wait: unfinished\n");
  298. return -1;
  299. }
  300. if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
  301. debug("i2c_wait: No RXACK\n");
  302. return -1;
  303. }
  304. return 0;
  305. } while ((get_ticks() - timeval) < timeout);
  306. debug("i2c_wait: timed out\n");
  307. return -1;
  308. }
  309. static __inline__ int
  310. i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta)
  311. {
  312. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  313. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
  314. | (rsta ? I2C_CR_RSTA : 0),
  315. &device->cr);
  316. writeb((dev << 1) | dir, &device->dr);
  317. if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
  318. return 0;
  319. return 1;
  320. }
  321. static __inline__ int
  322. __i2c_write(struct i2c_adapter *adap, u8 *data, int length)
  323. {
  324. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  325. int i;
  326. for (i = 0; i < length; i++) {
  327. writeb(data[i], &dev->dr);
  328. if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
  329. break;
  330. }
  331. return i;
  332. }
  333. static __inline__ int
  334. __i2c_read(struct i2c_adapter *adap, u8 *data, int length)
  335. {
  336. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  337. int i;
  338. writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
  339. &dev->cr);
  340. /* dummy read */
  341. readb(&dev->dr);
  342. for (i = 0; i < length; i++) {
  343. if (i2c_wait(adap, I2C_READ_BIT) < 0)
  344. break;
  345. /* Generate ack on last next to last byte */
  346. if (i == length - 2)
  347. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
  348. &dev->cr);
  349. /* Do not generate stop on last byte */
  350. if (i == length - 1)
  351. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  352. &dev->cr);
  353. data[i] = readb(&dev->dr);
  354. }
  355. return i;
  356. }
  357. static int
  358. fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data,
  359. int length)
  360. {
  361. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  362. int i = -1; /* signal error */
  363. u8 *a = (u8*)&addr;
  364. if (i2c_wait4bus(adap) < 0)
  365. return -1;
  366. if ((!length || alen > 0)
  367. && i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0
  368. && __i2c_write(adap, &a[4 - alen], alen) == alen)
  369. i = 0; /* No error so far */
  370. if (length &&
  371. i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0)
  372. i = __i2c_read(adap, data, length);
  373. writeb(I2C_CR_MEN, &device->cr);
  374. if (i2c_wait4bus(adap)) /* Wait until STOP */
  375. debug("i2c_read: wait4bus timed out\n");
  376. if (i == length)
  377. return 0;
  378. return -1;
  379. }
  380. static int
  381. fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen,
  382. u8 *data, int length)
  383. {
  384. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  385. int i = -1; /* signal error */
  386. u8 *a = (u8*)&addr;
  387. if (i2c_wait4bus(adap) < 0)
  388. return -1;
  389. if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 &&
  390. __i2c_write(adap, &a[4 - alen], alen) == alen) {
  391. i = __i2c_write(adap, data, length);
  392. }
  393. writeb(I2C_CR_MEN, &device->cr);
  394. if (i2c_wait4bus(adap)) /* Wait until STOP */
  395. debug("i2c_write: wait4bus timed out\n");
  396. if (i == length)
  397. return 0;
  398. return -1;
  399. }
  400. static int
  401. fsl_i2c_probe(struct i2c_adapter *adap, uchar chip)
  402. {
  403. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  404. /* For unknow reason the controller will ACK when
  405. * probing for a slave with the same address, so skip
  406. * it.
  407. */
  408. if (chip == (readb(&dev->adr) >> 1))
  409. return -1;
  410. return fsl_i2c_read(adap, chip, 0, 0, NULL, 0);
  411. }
  412. static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
  413. unsigned int speed)
  414. {
  415. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  416. writeb(0, &dev->cr); /* stop controller */
  417. set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
  418. writeb(I2C_CR_MEN, &dev->cr); /* start controller */
  419. return 0;
  420. }
  421. /*
  422. * Register fsl i2c adapters
  423. */
  424. U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
  425. fsl_i2c_write, fsl_i2c_set_bus_speed,
  426. CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
  427. 0)
  428. #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
  429. U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
  430. fsl_i2c_write, fsl_i2c_set_bus_speed,
  431. CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
  432. 1)
  433. #endif