ppc4xx_i2c.c 11 KB

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