ppc4xx_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * (C) Copyright 2007-2009
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * based on work by Anne Sophie Harnois <anne-sophie.harnois@nextream.fr>
  6. *
  7. * (C) Copyright 2001
  8. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <asm/ppc4xx.h>
  14. #include <asm/ppc4xx-i2c.h>
  15. #include <i2c.h>
  16. #include <asm/io.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr)
  19. {
  20. unsigned long base;
  21. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  22. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  23. defined(CONFIG_460EX) || defined(CONFIG_460GT)
  24. base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100);
  25. #elif defined(CONFIG_440) || defined(CONFIG_405EX)
  26. /* all remaining 440 variants */
  27. base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
  28. #else
  29. /* all 405 variants */
  30. base = 0xEF600500 + (hwadapnr * 0x100);
  31. #endif
  32. return (struct ppc4xx_i2c *)base;
  33. }
  34. static void _i2c_bus_reset(struct i2c_adapter *adap)
  35. {
  36. struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
  37. int i;
  38. u8 dc;
  39. /* Reset status register */
  40. /* write 1 in SCMP and IRQA to clear these fields */
  41. out_8(&i2c->sts, 0x0A);
  42. /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
  43. out_8(&i2c->extsts, 0x8F);
  44. /* Place chip in the reset state */
  45. out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
  46. /* Check if bus is free */
  47. dc = in_8(&i2c->directcntl);
  48. if (!DIRCTNL_FREE(dc)){
  49. /* Try to set bus free state */
  50. out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
  51. /* Wait until we regain bus control */
  52. for (i = 0; i < 100; ++i) {
  53. dc = in_8(&i2c->directcntl);
  54. if (DIRCTNL_FREE(dc))
  55. break;
  56. /* Toggle SCL line */
  57. dc ^= IIC_DIRCNTL_SCC;
  58. out_8(&i2c->directcntl, dc);
  59. udelay(10);
  60. dc ^= IIC_DIRCNTL_SCC;
  61. out_8(&i2c->directcntl, dc);
  62. }
  63. }
  64. /* Remove reset */
  65. out_8(&i2c->xtcntlss, 0);
  66. }
  67. static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  68. {
  69. struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
  70. int val, divisor;
  71. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  72. /*
  73. * Call board specific i2c bus reset routine before accessing the
  74. * environment, which might be in a chip on that bus. For details
  75. * about this problem see doc/I2C_Edge_Conditions.
  76. */
  77. i2c_init_board();
  78. #endif
  79. /* Handle possible failed I2C state */
  80. /* FIXME: put this into i2c_init_board()? */
  81. _i2c_bus_reset(adap);
  82. /* clear lo master address */
  83. out_8(&i2c->lmadr, 0);
  84. /* clear hi master address */
  85. out_8(&i2c->hmadr, 0);
  86. /* clear lo slave address */
  87. out_8(&i2c->lsadr, 0);
  88. /* clear hi slave address */
  89. out_8(&i2c->hsadr, 0);
  90. /* Clock divide Register */
  91. /* set divisor according to freq_opb */
  92. divisor = (get_OPB_freq() - 1) / 10000000;
  93. if (divisor == 0)
  94. divisor = 1;
  95. out_8(&i2c->clkdiv, divisor);
  96. /* no interrupts */
  97. out_8(&i2c->intrmsk, 0);
  98. /* clear transfer count */
  99. out_8(&i2c->xfrcnt, 0);
  100. /* clear extended control & stat */
  101. /* write 1 in SRC SRS SWC SWS to clear these fields */
  102. out_8(&i2c->xtcntlss, 0xF0);
  103. /* Mode Control Register
  104. Flush Slave/Master data buffer */
  105. out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
  106. val = in_8(&i2c->mdcntl);
  107. /* Ignore General Call, slave transfers are ignored,
  108. * disable interrupts, exit unknown bus state, enable hold
  109. * SCL 100kHz normaly or FastMode for 400kHz and above
  110. */
  111. val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
  112. if (speed >= 400000)
  113. val |= IIC_MDCNTL_FSM;
  114. out_8(&i2c->mdcntl, val);
  115. /* clear control reg */
  116. out_8(&i2c->cntl, 0x00);
  117. }
  118. /*
  119. * This code tries to use the features of the 405GP i2c
  120. * controller. It will transfer up to 4 bytes in one pass
  121. * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
  122. * is possible to do out16(lhz) transfers.
  123. *
  124. * cmd_type is 0 for write 1 for read.
  125. *
  126. * addr_len can take any value from 0-255, it is only limited
  127. * by the char, we could make it larger if needed. If it is
  128. * 0 we skip the address write cycle.
  129. *
  130. * Typical case is a Write of an addr followd by a Read. The
  131. * IBM FAQ does not cover this. On the last byte of the write
  132. * we don't set the creg CHT bit but the RPST bit.
  133. *
  134. * It does not support address only transfers, there must be
  135. * a data part. If you want to write the address yourself, put
  136. * it in the data pointer.
  137. *
  138. * It does not support transfer to/from address 0.
  139. *
  140. * It does not check XFRCNT.
  141. */
  142. static int _i2c_transfer(struct i2c_adapter *adap,
  143. unsigned char cmd_type,
  144. unsigned char chip,
  145. unsigned char addr[],
  146. unsigned char addr_len,
  147. unsigned char data[],
  148. unsigned short data_len)
  149. {
  150. struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
  151. u8 *ptr;
  152. int reading;
  153. int tran, cnt;
  154. int result;
  155. int status;
  156. int i;
  157. u8 creg;
  158. if (data == 0 || data_len == 0) {
  159. /* Don't support data transfer of no length or to address 0 */
  160. printf( "i2c_transfer: bad call\n" );
  161. return IIC_NOK;
  162. }
  163. if (addr && addr_len) {
  164. ptr = addr;
  165. cnt = addr_len;
  166. reading = 0;
  167. } else {
  168. ptr = data;
  169. cnt = data_len;
  170. reading = cmd_type;
  171. }
  172. /* Clear Stop Complete Bit */
  173. out_8(&i2c->sts, IIC_STS_SCMP);
  174. /* Check init */
  175. i = 10;
  176. do {
  177. /* Get status */
  178. status = in_8(&i2c->sts);
  179. i--;
  180. } while ((status & IIC_STS_PT) && (i > 0));
  181. if (status & IIC_STS_PT) {
  182. result = IIC_NOK_TOUT;
  183. return(result);
  184. }
  185. /* flush the Master/Slave Databuffers */
  186. out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
  187. IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
  188. /* need to wait 4 OPB clocks? code below should take that long */
  189. /* 7-bit adressing */
  190. out_8(&i2c->hmadr, 0);
  191. out_8(&i2c->lmadr, chip);
  192. tran = 0;
  193. result = IIC_OK;
  194. creg = 0;
  195. while (tran != cnt && (result == IIC_OK)) {
  196. int bc,j;
  197. /*
  198. * Control register =
  199. * Normal transfer, 7-bits adressing, Transfer up to
  200. * bc bytes, Normal start, Transfer is a sequence of transfers
  201. */
  202. creg |= IIC_CNTL_PT;
  203. bc = (cnt - tran) > 4 ? 4 : cnt - tran;
  204. creg |= (bc - 1) << 4;
  205. /* if the real cmd type is write continue trans */
  206. if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
  207. creg |= IIC_CNTL_CHT;
  208. /* last part of address, prepare for repeated start on read */
  209. if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
  210. creg |= IIC_CNTL_RPST;
  211. if (reading) {
  212. creg |= IIC_CNTL_READ;
  213. } else {
  214. for(j = 0; j < bc; j++) {
  215. /* Set buffer */
  216. out_8(&i2c->mdbuf, ptr[tran + j]);
  217. }
  218. }
  219. out_8(&i2c->cntl, creg);
  220. /*
  221. * Transfer is in progress
  222. * we have to wait for upto 5 bytes of data
  223. * 1 byte chip address+r/w bit then bc bytes
  224. * of data.
  225. * udelay(10) is 1 bit time at 100khz
  226. * Doubled for slop. 20 is too small.
  227. */
  228. i = 2 * 5 * 8;
  229. do {
  230. /* Get status */
  231. status = in_8(&i2c->sts);
  232. udelay(10);
  233. i--;
  234. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
  235. (i > 0));
  236. if (status & IIC_STS_ERR) {
  237. result = IIC_NOK;
  238. status = in_8(&i2c->extsts);
  239. /* Lost arbitration? */
  240. if (status & IIC_EXTSTS_LA)
  241. result = IIC_NOK_LA;
  242. /* Incomplete transfer? */
  243. if (status & IIC_EXTSTS_ICT)
  244. result = IIC_NOK_ICT;
  245. /* Transfer aborted? */
  246. if (status & IIC_EXTSTS_XFRA)
  247. result = IIC_NOK_XFRA;
  248. /* Is bus free?
  249. * If error happened during combined xfer
  250. * IIC interface is usually stuck in some strange
  251. * state without a valid stop condition.
  252. * Brute, but working: generate stop, then soft reset.
  253. */
  254. if ((status & IIC_EXTSTS_BCS_MASK)
  255. != IIC_EXTSTS_BCS_FREE){
  256. u8 mdcntl = in_8(&i2c->mdcntl);
  257. /* Generate valid stop condition */
  258. out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
  259. out_8(&i2c->directcntl, IIC_DIRCNTL_SCC);
  260. udelay(10);
  261. out_8(&i2c->directcntl,
  262. IIC_DIRCNTL_SCC | IIC_DIRCNTL_SDAC);
  263. out_8(&i2c->xtcntlss, 0);
  264. ppc4xx_i2c_init(adap, (mdcntl & IIC_MDCNTL_FSM)
  265. ? 400000 : 100000, 0);
  266. }
  267. } else if ( status & IIC_STS_PT) {
  268. result = IIC_NOK_TOUT;
  269. }
  270. /* Command is reading => get buffer */
  271. if ((reading) && (result == IIC_OK)) {
  272. /* Are there data in buffer */
  273. if (status & IIC_STS_MDBS) {
  274. /*
  275. * even if we have data we have to wait 4OPB
  276. * clocks for it to hit the front of the FIFO,
  277. * after that we can just read. We should check
  278. * XFCNT here and if the FIFO is full there is
  279. * no need to wait.
  280. */
  281. udelay(1);
  282. for (j = 0; j < bc; j++)
  283. ptr[tran + j] = in_8(&i2c->mdbuf);
  284. } else
  285. result = IIC_NOK_DATA;
  286. }
  287. creg = 0;
  288. tran += bc;
  289. if (ptr == addr && tran == cnt) {
  290. ptr = data;
  291. cnt = data_len;
  292. tran = 0;
  293. reading = cmd_type;
  294. }
  295. }
  296. return result;
  297. }
  298. static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
  299. {
  300. uchar buf[1];
  301. buf[0] = 0;
  302. /*
  303. * What is needed is to send the chip address and verify that the
  304. * address was <ACK>ed (i.e. there was a chip at that address which
  305. * drove the data line low).
  306. */
  307. return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
  308. }
  309. static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
  310. int alen, uchar *buffer, int len, int read)
  311. {
  312. uchar xaddr[4];
  313. int ret;
  314. if (alen > 4) {
  315. printf("I2C: addr len %d not supported\n", alen);
  316. return 1;
  317. }
  318. if (alen > 0) {
  319. xaddr[0] = (addr >> 24) & 0xFF;
  320. xaddr[1] = (addr >> 16) & 0xFF;
  321. xaddr[2] = (addr >> 8) & 0xFF;
  322. xaddr[3] = addr & 0xFF;
  323. }
  324. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  325. /*
  326. * EEPROM chips that implement "address overflow" are ones
  327. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  328. * address and the extra bits end up in the "chip address"
  329. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  330. * four 256 byte chips.
  331. *
  332. * Note that we consider the length of the address field to
  333. * still be one byte because the extra address bits are
  334. * hidden in the chip address.
  335. */
  336. if (alen > 0)
  337. chip |= ((addr >> (alen * 8)) &
  338. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  339. #endif
  340. ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
  341. buffer, len);
  342. if (ret) {
  343. printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
  344. return 1;
  345. }
  346. return 0;
  347. }
  348. static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  349. int alen, uchar *buffer, int len)
  350. {
  351. return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
  352. }
  353. static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  354. int alen, uchar *buffer, int len)
  355. {
  356. return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
  357. }
  358. static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
  359. unsigned int speed)
  360. {
  361. if (speed != adap->speed)
  362. return -1;
  363. return speed;
  364. }
  365. /*
  366. * Register ppc4xx i2c adapters
  367. */
  368. #ifdef CONFIG_SYS_I2C_PPC4XX_CH0
  369. U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
  370. ppc4xx_i2c_read, ppc4xx_i2c_write,
  371. ppc4xx_i2c_set_bus_speed,
  372. CONFIG_SYS_I2C_PPC4XX_SPEED_0,
  373. CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
  374. #endif
  375. #ifdef CONFIG_SYS_I2C_PPC4XX_CH1
  376. U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
  377. ppc4xx_i2c_read, ppc4xx_i2c_write,
  378. ppc4xx_i2c_set_bus_speed,
  379. CONFIG_SYS_I2C_PPC4XX_SPEED_1,
  380. CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
  381. #endif