fsl_i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. * SPDX-License-Identifier: GPL-2.0
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <i2c.h> /* Functional interface */
  12. #include <asm/io.h>
  13. #include <asm/fsl_i2c.h> /* HW definitions */
  14. #include <dm.h>
  15. #include <mapmem.h>
  16. /* The maximum number of microseconds we will wait until another master has
  17. * released the bus. If not defined in the board header file, then use a
  18. * generic value.
  19. */
  20. #ifndef CONFIG_I2C_MBB_TIMEOUT
  21. #define CONFIG_I2C_MBB_TIMEOUT 100000
  22. #endif
  23. /* The maximum number of microseconds we will wait for a read or write
  24. * operation to complete. If not defined in the board header file, then use a
  25. * generic value.
  26. */
  27. #ifndef CONFIG_I2C_TIMEOUT
  28. #define CONFIG_I2C_TIMEOUT 100000
  29. #endif
  30. #define I2C_READ_BIT 1
  31. #define I2C_WRITE_BIT 0
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #ifndef CONFIG_DM_I2C
  34. static const struct fsl_i2c_base *i2c_base[4] = {
  35. (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
  36. #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
  37. (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
  38. #endif
  39. #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
  40. (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
  41. #endif
  42. #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
  43. (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
  44. #endif
  45. };
  46. #endif
  47. /* I2C speed map for a DFSR value of 1 */
  48. /*
  49. * Map I2C frequency dividers to FDR and DFSR values
  50. *
  51. * This structure is used to define the elements of a table that maps I2C
  52. * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
  53. * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
  54. * Sampling Rate (DFSR) registers.
  55. *
  56. * The actual table should be defined in the board file, and it must be called
  57. * fsl_i2c_speed_map[].
  58. *
  59. * The last entry of the table must have a value of {-1, X}, where X is same
  60. * FDR/DFSR values as the second-to-last entry. This guarantees that any
  61. * search through the array will always find a match.
  62. *
  63. * The values of the divider must be in increasing numerical order, i.e.
  64. * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
  65. *
  66. * For this table, the values are based on a value of 1 for the DFSR
  67. * register. See the application note AN2919 "Determining the I2C Frequency
  68. * Divider Ratio for SCL"
  69. *
  70. * ColdFire I2C frequency dividers for FDR values are different from
  71. * PowerPC. The protocol to use the I2C module is still the same.
  72. * A different table is defined and are based on MCF5xxx user manual.
  73. *
  74. */
  75. static const struct {
  76. unsigned short divider;
  77. u8 fdr;
  78. } fsl_i2c_speed_map[] = {
  79. #ifdef __M68K__
  80. {20, 32}, {22, 33}, {24, 34}, {26, 35},
  81. {28, 0}, {28, 36}, {30, 1}, {32, 37},
  82. {34, 2}, {36, 38}, {40, 3}, {40, 39},
  83. {44, 4}, {48, 5}, {48, 40}, {56, 6},
  84. {56, 41}, {64, 42}, {68, 7}, {72, 43},
  85. {80, 8}, {80, 44}, {88, 9}, {96, 41},
  86. {104, 10}, {112, 42}, {128, 11}, {128, 43},
  87. {144, 12}, {160, 13}, {160, 48}, {192, 14},
  88. {192, 49}, {224, 50}, {240, 15}, {256, 51},
  89. {288, 16}, {320, 17}, {320, 52}, {384, 18},
  90. {384, 53}, {448, 54}, {480, 19}, {512, 55},
  91. {576, 20}, {640, 21}, {640, 56}, {768, 22},
  92. {768, 57}, {960, 23}, {896, 58}, {1024, 59},
  93. {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
  94. {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
  95. {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
  96. {-1, 31}
  97. #endif
  98. };
  99. /**
  100. * Set the I2C bus speed for a given I2C device
  101. *
  102. * @param base: the I2C device registers
  103. * @i2c_clk: I2C bus clock frequency
  104. * @speed: the desired speed of the bus
  105. *
  106. * The I2C device must be stopped before calling this function.
  107. *
  108. * The return value is the actual bus speed that is set.
  109. */
  110. static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base,
  111. unsigned int i2c_clk, unsigned int speed)
  112. {
  113. unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX);
  114. /*
  115. * We want to choose an FDR/DFSR that generates an I2C bus speed that
  116. * is equal to or lower than the requested speed. That means that we
  117. * want the first divider that is equal to or greater than the
  118. * calculated divider.
  119. */
  120. #ifdef __PPC__
  121. u8 dfsr, fdr = 0x31; /* Default if no FDR found */
  122. /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
  123. unsigned short a, b, ga, gb;
  124. unsigned long c_div, est_div;
  125. #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
  126. dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
  127. #else
  128. /* Condition 1: dfsr <= 50/T */
  129. dfsr = (5 * (i2c_clk / 1000)) / 100000;
  130. #endif
  131. #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
  132. fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
  133. speed = i2c_clk / divider; /* Fake something */
  134. #else
  135. debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
  136. if (!dfsr)
  137. dfsr = 1;
  138. est_div = ~0;
  139. for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
  140. for (gb = 0; gb < 8; gb++) {
  141. b = 16 << gb;
  142. c_div = b * (a + ((3*dfsr)/b)*2);
  143. if ((c_div > divider) && (c_div < est_div)) {
  144. unsigned short bin_gb, bin_ga;
  145. est_div = c_div;
  146. bin_gb = gb << 2;
  147. bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
  148. fdr = bin_gb | bin_ga;
  149. speed = i2c_clk / est_div;
  150. debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
  151. "a:%d, b:%d, speed:%d\n",
  152. fdr, est_div, ga, gb, a, b, speed);
  153. /* Condition 2 not accounted for */
  154. debug("Tr <= %d ns\n",
  155. (b - 3 * dfsr) * 1000000 /
  156. (i2c_clk / 1000));
  157. }
  158. }
  159. if (a == 20)
  160. a += 2;
  161. if (a == 24)
  162. a += 4;
  163. }
  164. debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
  165. debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
  166. #endif
  167. writeb(dfsr, &base->dfsrr); /* set default filter */
  168. writeb(fdr, &base->fdr); /* set bus speed */
  169. #else
  170. unsigned int i;
  171. for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
  172. if (fsl_i2c_speed_map[i].divider >= divider) {
  173. u8 fdr;
  174. fdr = fsl_i2c_speed_map[i].fdr;
  175. speed = i2c_clk / fsl_i2c_speed_map[i].divider;
  176. writeb(fdr, &base->fdr); /* set bus speed */
  177. break;
  178. }
  179. #endif
  180. return speed;
  181. }
  182. #ifndef CONFIG_DM_I2C
  183. static unsigned int get_i2c_clock(int bus)
  184. {
  185. if (bus)
  186. return gd->arch.i2c2_clk; /* I2C2 clock */
  187. else
  188. return gd->arch.i2c1_clk; /* I2C1 clock */
  189. }
  190. #endif
  191. static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
  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, &base->cr);
  204. timeval = get_ticks();
  205. while (!(readb(&base->sr) & I2C_SR_MBB)) {
  206. if ((get_ticks() - timeval) > timeout)
  207. goto err;
  208. }
  209. if (readb(&base->sr) & I2C_SR_MAL) {
  210. /* SDA is stuck low */
  211. writeb(0, &base->cr);
  212. udelay(100);
  213. writeb(I2C_CR_MSTA | flags, &base->cr);
  214. writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
  215. }
  216. readb(&base->dr);
  217. timeval = get_ticks();
  218. while (!(readb(&base->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, &base->cr);
  225. writeb(0, &base->sr);
  226. udelay(100);
  227. return ret;
  228. }
  229. static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
  230. slaveadd, int i2c_clk, int busnum)
  231. {
  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. writeb(0, &base->cr); /* stop I2C controller */
  242. udelay(5); /* let it shutdown in peace */
  243. set_i2c_bus_speed(base, i2c_clk, speed);
  244. writeb(slaveadd << 1, &base->adr);/* write slave address */
  245. writeb(0x0, &base->sr); /* clear status register */
  246. writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
  247. timeval = get_ticks();
  248. while (readb(&base->sr) & I2C_SR_MBB) {
  249. if ((get_ticks() - timeval) < timeout)
  250. continue;
  251. if (fsl_i2c_fixup(base))
  252. debug("i2c_init: BUS#%d failed to init\n",
  253. busnum);
  254. break;
  255. }
  256. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  257. /* Call board specific i2c bus reset routine AFTER the bus has been
  258. * initialized. Use either this callpoint or i2c_init_board;
  259. * which is called before i2c_init operations.
  260. * For details about this problem see doc/I2C_Edge_Conditions.
  261. */
  262. i2c_board_late_init();
  263. #endif
  264. }
  265. static int
  266. i2c_wait4bus(const struct fsl_i2c_base *base)
  267. {
  268. unsigned long long timeval = get_ticks();
  269. const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
  270. while (readb(&base->sr) & I2C_SR_MBB) {
  271. if ((get_ticks() - timeval) > timeout)
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. static inline int
  277. i2c_wait(const struct fsl_i2c_base *base, int write)
  278. {
  279. u32 csr;
  280. unsigned long long timeval = get_ticks();
  281. const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
  282. do {
  283. csr = readb(&base->sr);
  284. if (!(csr & I2C_SR_MIF))
  285. continue;
  286. /* Read again to allow register to stabilise */
  287. csr = readb(&base->sr);
  288. writeb(0x0, &base->sr);
  289. if (csr & I2C_SR_MAL) {
  290. debug("i2c_wait: MAL\n");
  291. return -1;
  292. }
  293. if (!(csr & I2C_SR_MCF)) {
  294. debug("i2c_wait: unfinished\n");
  295. return -1;
  296. }
  297. if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
  298. debug("i2c_wait: No RXACK\n");
  299. return -1;
  300. }
  301. return 0;
  302. } while ((get_ticks() - timeval) < timeout);
  303. debug("i2c_wait: timed out\n");
  304. return -1;
  305. }
  306. static inline int
  307. i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta)
  308. {
  309. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
  310. | (rsta ? I2C_CR_RSTA : 0),
  311. &base->cr);
  312. writeb((dev << 1) | dir, &base->dr);
  313. if (i2c_wait(base, I2C_WRITE_BIT) < 0)
  314. return 0;
  315. return 1;
  316. }
  317. static inline int
  318. __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length)
  319. {
  320. int i;
  321. for (i = 0; i < length; i++) {
  322. writeb(data[i], &base->dr);
  323. if (i2c_wait(base, I2C_WRITE_BIT) < 0)
  324. break;
  325. }
  326. return i;
  327. }
  328. static inline int
  329. __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length)
  330. {
  331. int i;
  332. writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
  333. &base->cr);
  334. /* dummy read */
  335. readb(&base->dr);
  336. for (i = 0; i < length; i++) {
  337. if (i2c_wait(base, I2C_READ_BIT) < 0)
  338. break;
  339. /* Generate ack on last next to last byte */
  340. if (i == length - 2)
  341. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
  342. &base->cr);
  343. /* Do not generate stop on last byte */
  344. if (i == length - 1)
  345. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  346. &base->cr);
  347. data[i] = readb(&base->dr);
  348. }
  349. return i;
  350. }
  351. static int
  352. __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
  353. u8 *data, int dlen)
  354. {
  355. int ret = -1; /* signal error */
  356. if (i2c_wait4bus(base) < 0)
  357. return -1;
  358. /* Some drivers use offset lengths in excess of 4 bytes. These drivers
  359. * adhere to the following convention:
  360. * - the offset length is passed as negative (that is, the absolute
  361. * value of olen is the actual offset length)
  362. * - the offset itself is passed in data, which is overwritten by the
  363. * subsequent read operation
  364. */
  365. if (olen < 0) {
  366. if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
  367. ret = __i2c_write_data(base, data, -olen);
  368. if (ret != -olen)
  369. return -1;
  370. if (dlen && i2c_write_addr(base, chip_addr,
  371. I2C_READ_BIT, 1) != 0)
  372. ret = __i2c_read_data(base, data, dlen);
  373. } else {
  374. if ((!dlen || olen > 0) &&
  375. i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
  376. __i2c_write_data(base, offset, olen) == olen)
  377. ret = 0; /* No error so far */
  378. if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
  379. olen ? 1 : 0) != 0)
  380. ret = __i2c_read_data(base, data, dlen);
  381. }
  382. writeb(I2C_CR_MEN, &base->cr);
  383. if (i2c_wait4bus(base)) /* Wait until STOP */
  384. debug("i2c_read: wait4bus timed out\n");
  385. if (ret == dlen)
  386. return 0;
  387. return -1;
  388. }
  389. static int
  390. __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
  391. u8 *data, int dlen)
  392. {
  393. int ret = -1; /* signal error */
  394. if (i2c_wait4bus(base) < 0)
  395. return -1;
  396. if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
  397. __i2c_write_data(base, offset, olen) == olen) {
  398. ret = __i2c_write_data(base, data, dlen);
  399. }
  400. writeb(I2C_CR_MEN, &base->cr);
  401. if (i2c_wait4bus(base)) /* Wait until STOP */
  402. debug("i2c_write: wait4bus timed out\n");
  403. if (ret == dlen)
  404. return 0;
  405. return -1;
  406. }
  407. static int
  408. __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
  409. {
  410. /* For unknow reason the controller will ACK when
  411. * probing for a slave with the same address, so skip
  412. * it.
  413. */
  414. if (chip == (readb(&base->adr) >> 1))
  415. return -1;
  416. return __i2c_read(base, chip, 0, 0, NULL, 0);
  417. }
  418. static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base,
  419. unsigned int speed, int i2c_clk)
  420. {
  421. writeb(0, &base->cr); /* stop controller */
  422. set_i2c_bus_speed(base, i2c_clk, speed);
  423. writeb(I2C_CR_MEN, &base->cr); /* start controller */
  424. return 0;
  425. }
  426. #ifndef CONFIG_DM_I2C
  427. static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  428. {
  429. __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
  430. get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
  431. }
  432. static int
  433. fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
  434. {
  435. return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
  436. }
  437. static int
  438. fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
  439. u8 *data, int dlen)
  440. {
  441. u8 *o = (u8 *)&offset;
  442. return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
  443. olen, data, dlen);
  444. }
  445. static int
  446. fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
  447. u8 *data, int dlen)
  448. {
  449. u8 *o = (u8 *)&offset;
  450. return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
  451. olen, data, dlen);
  452. }
  453. static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
  454. unsigned int speed)
  455. {
  456. return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
  457. get_i2c_clock(adap->hwadapnr));
  458. }
  459. /*
  460. * Register fsl i2c adapters
  461. */
  462. U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
  463. fsl_i2c_write, fsl_i2c_set_bus_speed,
  464. CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
  465. 0)
  466. #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
  467. U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
  468. fsl_i2c_write, fsl_i2c_set_bus_speed,
  469. CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
  470. 1)
  471. #endif
  472. #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
  473. U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
  474. fsl_i2c_write, fsl_i2c_set_bus_speed,
  475. CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
  476. 2)
  477. #endif
  478. #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
  479. U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
  480. fsl_i2c_write, fsl_i2c_set_bus_speed,
  481. CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
  482. 3)
  483. #endif
  484. #else /* CONFIG_DM_I2C */
  485. static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  486. u32 chip_flags)
  487. {
  488. struct fsl_i2c_dev *dev = dev_get_priv(bus);
  489. return __i2c_probe_chip(dev->base, chip_addr);
  490. }
  491. static int fsl_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  492. {
  493. struct fsl_i2c_dev *dev = dev_get_priv(bus);
  494. return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
  495. }
  496. static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
  497. {
  498. struct fsl_i2c_dev *dev = dev_get_priv(bus);
  499. fdt_addr_t addr;
  500. fdt_size_t size;
  501. addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, bus->of_offset,
  502. "reg", 0, &size, false);
  503. dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, size);
  504. if (!dev->base)
  505. return -ENOMEM;
  506. dev->index = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
  507. "cell-index", -1);
  508. dev->slaveadd = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
  509. "u-boot,i2c-slave-addr", 0x7f);
  510. dev->speed = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
  511. "clock-frequency", 400000);
  512. dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk;
  513. return 0;
  514. }
  515. static int fsl_i2c_probe(struct udevice *bus)
  516. {
  517. struct fsl_i2c_dev *dev = dev_get_priv(bus);
  518. __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
  519. dev->index);
  520. return 0;
  521. }
  522. static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  523. {
  524. struct fsl_i2c_dev *dev = dev_get_priv(bus);
  525. struct i2c_msg *dmsg, *omsg, dummy;
  526. memset(&dummy, 0, sizeof(struct i2c_msg));
  527. /* We expect either two messages (one with an offset and one with the
  528. * actucal data) or one message (just data) */
  529. if (nmsgs > 2 || nmsgs == 0) {
  530. debug("%s: Only one or two messages are supported.", __func__);
  531. return -1;
  532. }
  533. omsg = nmsgs == 1 ? &dummy : msg;
  534. dmsg = nmsgs == 1 ? msg : msg + 1;
  535. if (dmsg->flags & I2C_M_RD)
  536. return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
  537. dmsg->buf, dmsg->len);
  538. else
  539. return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
  540. dmsg->buf, dmsg->len);
  541. }
  542. static const struct dm_i2c_ops fsl_i2c_ops = {
  543. .xfer = fsl_i2c_xfer,
  544. .probe_chip = fsl_i2c_probe_chip,
  545. .set_bus_speed = fsl_i2c_set_bus_speed,
  546. };
  547. static const struct udevice_id fsl_i2c_ids[] = {
  548. { .compatible = "fsl-i2c", },
  549. { /* sentinel */ }
  550. };
  551. U_BOOT_DRIVER(i2c_fsl) = {
  552. .name = "i2c_fsl",
  553. .id = UCLASS_I2C,
  554. .of_match = fsl_i2c_ids,
  555. .probe = fsl_i2c_probe,
  556. .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
  557. .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
  558. .ops = &fsl_i2c_ops,
  559. };
  560. #endif /* CONFIG_DM_I2C */