fsl_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  192. {
  193. const struct fsl_i2c *dev;
  194. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  195. /* Call board specific i2c bus reset routine before accessing the
  196. * environment, which might be in a chip on that bus. For details
  197. * about this problem see doc/I2C_Edge_Conditions.
  198. */
  199. i2c_init_board();
  200. #endif
  201. dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  202. writeb(0, &dev->cr); /* stop I2C controller */
  203. udelay(5); /* let it shutdown in peace */
  204. set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
  205. writeb(slaveadd << 1, &dev->adr);/* write slave address */
  206. writeb(0x0, &dev->sr); /* clear status register */
  207. writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
  208. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  209. /* Call board specific i2c bus reset routine AFTER the bus has been
  210. * initialized. Use either this callpoint or i2c_init_board;
  211. * which is called before i2c_init operations.
  212. * For details about this problem see doc/I2C_Edge_Conditions.
  213. */
  214. i2c_board_late_init();
  215. #endif
  216. }
  217. static int
  218. i2c_wait4bus(struct i2c_adapter *adap)
  219. {
  220. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  221. unsigned long long timeval = get_ticks();
  222. const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
  223. while (readb(&dev->sr) & I2C_SR_MBB) {
  224. if ((get_ticks() - timeval) > timeout)
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. static __inline__ int
  230. i2c_wait(struct i2c_adapter *adap, int write)
  231. {
  232. u32 csr;
  233. unsigned long long timeval = get_ticks();
  234. const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
  235. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  236. do {
  237. csr = readb(&dev->sr);
  238. if (!(csr & I2C_SR_MIF))
  239. continue;
  240. /* Read again to allow register to stabilise */
  241. csr = readb(&dev->sr);
  242. writeb(0x0, &dev->sr);
  243. if (csr & I2C_SR_MAL) {
  244. debug("i2c_wait: MAL\n");
  245. return -1;
  246. }
  247. if (!(csr & I2C_SR_MCF)) {
  248. debug("i2c_wait: unfinished\n");
  249. return -1;
  250. }
  251. if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
  252. debug("i2c_wait: No RXACK\n");
  253. return -1;
  254. }
  255. return 0;
  256. } while ((get_ticks() - timeval) < timeout);
  257. debug("i2c_wait: timed out\n");
  258. return -1;
  259. }
  260. static __inline__ int
  261. i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta)
  262. {
  263. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  264. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
  265. | (rsta ? I2C_CR_RSTA : 0),
  266. &device->cr);
  267. writeb((dev << 1) | dir, &device->dr);
  268. if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
  269. return 0;
  270. return 1;
  271. }
  272. static __inline__ int
  273. __i2c_write(struct i2c_adapter *adap, u8 *data, int length)
  274. {
  275. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  276. int i;
  277. for (i = 0; i < length; i++) {
  278. writeb(data[i], &dev->dr);
  279. if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
  280. break;
  281. }
  282. return i;
  283. }
  284. static __inline__ int
  285. __i2c_read(struct i2c_adapter *adap, u8 *data, int length)
  286. {
  287. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  288. int i;
  289. writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
  290. &dev->cr);
  291. /* dummy read */
  292. readb(&dev->dr);
  293. for (i = 0; i < length; i++) {
  294. if (i2c_wait(adap, I2C_READ_BIT) < 0)
  295. break;
  296. /* Generate ack on last next to last byte */
  297. if (i == length - 2)
  298. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
  299. &dev->cr);
  300. /* Do not generate stop on last byte */
  301. if (i == length - 1)
  302. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  303. &dev->cr);
  304. data[i] = readb(&dev->dr);
  305. }
  306. return i;
  307. }
  308. static int
  309. fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data,
  310. int length)
  311. {
  312. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  313. int i = -1; /* signal error */
  314. u8 *a = (u8*)&addr;
  315. if (i2c_wait4bus(adap) < 0)
  316. return -1;
  317. if ((!length || alen > 0)
  318. && i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0
  319. && __i2c_write(adap, &a[4 - alen], alen) == alen)
  320. i = 0; /* No error so far */
  321. if (length &&
  322. i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0)
  323. i = __i2c_read(adap, data, length);
  324. writeb(I2C_CR_MEN, &device->cr);
  325. if (i2c_wait4bus(adap)) /* Wait until STOP */
  326. debug("i2c_read: wait4bus timed out\n");
  327. if (i == length)
  328. return 0;
  329. return -1;
  330. }
  331. static int
  332. fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen,
  333. u8 *data, int length)
  334. {
  335. struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  336. int i = -1; /* signal error */
  337. u8 *a = (u8*)&addr;
  338. if (i2c_wait4bus(adap) >= 0 &&
  339. i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 &&
  340. __i2c_write(adap, &a[4 - alen], alen) == alen) {
  341. i = __i2c_write(adap, data, length);
  342. }
  343. writeb(I2C_CR_MEN, &device->cr);
  344. if (i2c_wait4bus(adap)) /* Wait until STOP */
  345. debug("i2c_write: wait4bus timed out\n");
  346. if (i == length)
  347. return 0;
  348. return -1;
  349. }
  350. static int
  351. fsl_i2c_probe(struct i2c_adapter *adap, uchar chip)
  352. {
  353. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  354. /* For unknow reason the controller will ACK when
  355. * probing for a slave with the same address, so skip
  356. * it.
  357. */
  358. if (chip == (readb(&dev->adr) >> 1))
  359. return -1;
  360. return fsl_i2c_read(adap, chip, 0, 0, NULL, 0);
  361. }
  362. static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
  363. unsigned int speed)
  364. {
  365. struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
  366. writeb(0, &dev->cr); /* stop controller */
  367. set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
  368. writeb(I2C_CR_MEN, &dev->cr); /* start controller */
  369. return 0;
  370. }
  371. /*
  372. * Register fsl i2c adapters
  373. */
  374. U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
  375. fsl_i2c_write, fsl_i2c_set_bus_speed,
  376. CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
  377. 0)
  378. #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
  379. U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
  380. fsl_i2c_write, fsl_i2c_set_bus_speed,
  381. CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
  382. 1)
  383. #endif