fsl_i2c.c 15 KB

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