ppc4xx_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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, and on the first bytes of the
  133. * read we set the RPST bit.
  134. *
  135. * It does not support address only transfers, there must be
  136. * a data part. If you want to write the address yourself, put
  137. * it in the data pointer.
  138. *
  139. * It does not support transfer to/from address 0.
  140. *
  141. * It does not check XFRCNT.
  142. */
  143. static int _i2c_transfer(struct i2c_adapter *adap,
  144. unsigned char cmd_type,
  145. unsigned char chip,
  146. unsigned char addr[],
  147. unsigned char addr_len,
  148. unsigned char data[],
  149. unsigned short data_len)
  150. {
  151. struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
  152. u8 *ptr;
  153. int reading;
  154. int tran, cnt;
  155. int result;
  156. int status;
  157. int i;
  158. u8 creg;
  159. if (data == 0 || data_len == 0) {
  160. /* Don't support data transfer of no length or to address 0 */
  161. printf( "i2c_transfer: bad call\n" );
  162. return IIC_NOK;
  163. }
  164. if (addr && addr_len) {
  165. ptr = addr;
  166. cnt = addr_len;
  167. reading = 0;
  168. } else {
  169. ptr = data;
  170. cnt = data_len;
  171. reading = cmd_type;
  172. }
  173. /* Clear Stop Complete Bit */
  174. out_8(&i2c->sts, IIC_STS_SCMP);
  175. /* Check init */
  176. i = 10;
  177. do {
  178. /* Get status */
  179. status = in_8(&i2c->sts);
  180. i--;
  181. } while ((status & IIC_STS_PT) && (i > 0));
  182. if (status & IIC_STS_PT) {
  183. result = IIC_NOK_TOUT;
  184. return(result);
  185. }
  186. /* flush the Master/Slave Databuffers */
  187. out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
  188. IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
  189. /* need to wait 4 OPB clocks? code below should take that long */
  190. /* 7-bit adressing */
  191. out_8(&i2c->hmadr, 0);
  192. out_8(&i2c->lmadr, chip);
  193. tran = 0;
  194. result = IIC_OK;
  195. creg = 0;
  196. while (tran != cnt && (result == IIC_OK)) {
  197. int bc,j;
  198. /*
  199. * Control register =
  200. * Normal transfer, 7-bits adressing, Transfer up to
  201. * bc bytes, Normal start, Transfer is a sequence of transfers
  202. */
  203. creg |= IIC_CNTL_PT;
  204. bc = (cnt - tran) > 4 ? 4 : cnt - tran;
  205. creg |= (bc - 1) << 4;
  206. /* if the real cmd type is write continue trans */
  207. if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
  208. creg |= IIC_CNTL_CHT;
  209. if (reading) {
  210. creg |= IIC_CNTL_READ;
  211. } else {
  212. for(j = 0; j < bc; j++) {
  213. /* Set buffer */
  214. out_8(&i2c->mdbuf, ptr[tran + j]);
  215. }
  216. }
  217. out_8(&i2c->cntl, creg);
  218. /*
  219. * Transfer is in progress
  220. * we have to wait for upto 5 bytes of data
  221. * 1 byte chip address+r/w bit then bc bytes
  222. * of data.
  223. * udelay(10) is 1 bit time at 100khz
  224. * Doubled for slop. 20 is too small.
  225. */
  226. i = 2 * 5 * 8;
  227. do {
  228. /* Get status */
  229. status = in_8(&i2c->sts);
  230. udelay(10);
  231. i--;
  232. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
  233. (i > 0));
  234. if (status & IIC_STS_ERR) {
  235. result = IIC_NOK;
  236. status = in_8(&i2c->extsts);
  237. /* Lost arbitration? */
  238. if (status & IIC_EXTSTS_LA)
  239. result = IIC_NOK_LA;
  240. /* Incomplete transfer? */
  241. if (status & IIC_EXTSTS_ICT)
  242. result = IIC_NOK_ICT;
  243. /* Transfer aborted? */
  244. if (status & IIC_EXTSTS_XFRA)
  245. result = IIC_NOK_XFRA;
  246. } else if ( status & IIC_STS_PT) {
  247. result = IIC_NOK_TOUT;
  248. }
  249. /* Command is reading => get buffer */
  250. if ((reading) && (result == IIC_OK)) {
  251. /* Are there data in buffer */
  252. if (status & IIC_STS_MDBS) {
  253. /*
  254. * even if we have data we have to wait 4OPB
  255. * clocks for it to hit the front of the FIFO,
  256. * after that we can just read. We should check
  257. * XFCNT here and if the FIFO is full there is
  258. * no need to wait.
  259. */
  260. udelay(1);
  261. for (j = 0; j < bc; j++)
  262. ptr[tran + j] = in_8(&i2c->mdbuf);
  263. } else
  264. result = IIC_NOK_DATA;
  265. }
  266. creg = 0;
  267. tran += bc;
  268. if (ptr == addr && tran == cnt) {
  269. ptr = data;
  270. cnt = data_len;
  271. tran = 0;
  272. reading = cmd_type;
  273. if (reading)
  274. creg = IIC_CNTL_RPST;
  275. }
  276. }
  277. return result;
  278. }
  279. static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
  280. {
  281. uchar buf[1];
  282. buf[0] = 0;
  283. /*
  284. * What is needed is to send the chip address and verify that the
  285. * address was <ACK>ed (i.e. there was a chip at that address which
  286. * drove the data line low).
  287. */
  288. return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
  289. }
  290. static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
  291. int alen, uchar *buffer, int len, int read)
  292. {
  293. uchar xaddr[4];
  294. int ret;
  295. if (alen > 4) {
  296. printf("I2C: addr len %d not supported\n", alen);
  297. return 1;
  298. }
  299. if (alen > 0) {
  300. xaddr[0] = (addr >> 24) & 0xFF;
  301. xaddr[1] = (addr >> 16) & 0xFF;
  302. xaddr[2] = (addr >> 8) & 0xFF;
  303. xaddr[3] = addr & 0xFF;
  304. }
  305. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  306. /*
  307. * EEPROM chips that implement "address overflow" are ones
  308. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  309. * address and the extra bits end up in the "chip address"
  310. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  311. * four 256 byte chips.
  312. *
  313. * Note that we consider the length of the address field to
  314. * still be one byte because the extra address bits are
  315. * hidden in the chip address.
  316. */
  317. if (alen > 0)
  318. chip |= ((addr >> (alen * 8)) &
  319. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  320. #endif
  321. ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
  322. buffer, len);
  323. if (ret) {
  324. printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  330. int alen, uchar *buffer, int len)
  331. {
  332. return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
  333. }
  334. static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  335. int alen, uchar *buffer, int len)
  336. {
  337. return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
  338. }
  339. static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
  340. unsigned int speed)
  341. {
  342. if (speed != adap->speed)
  343. return -1;
  344. return speed;
  345. }
  346. /*
  347. * Register ppc4xx i2c adapters
  348. */
  349. #ifdef CONFIG_SYS_I2C_PPC4XX_CH0
  350. U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
  351. ppc4xx_i2c_read, ppc4xx_i2c_write,
  352. ppc4xx_i2c_set_bus_speed,
  353. CONFIG_SYS_I2C_PPC4XX_SPEED_0,
  354. CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
  355. #endif
  356. #ifdef CONFIG_SYS_I2C_PPC4XX_CH1
  357. U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
  358. ppc4xx_i2c_read, ppc4xx_i2c_write,
  359. ppc4xx_i2c_set_bus_speed,
  360. CONFIG_SYS_I2C_PPC4XX_SPEED_1,
  361. CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
  362. #endif