ppc4xx_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <asm/ppc4xx.h>
  30. #include <asm/ppc4xx-i2c.h>
  31. #include <i2c.h>
  32. #include <asm/io.h>
  33. #ifdef CONFIG_HARD_I2C
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if defined(CONFIG_I2C_MULTI_BUS)
  36. /*
  37. * Initialize the bus pointer to whatever one the SPD EEPROM is on.
  38. * Default is bus 0. This is necessary because the DDR initialization
  39. * runs from ROM, and we can't switch buses because we can't modify
  40. * the global variables.
  41. */
  42. #ifndef CONFIG_SYS_SPD_BUS_NUM
  43. #define CONFIG_SYS_SPD_BUS_NUM 0
  44. #endif
  45. static unsigned int i2c_bus_num __attribute__ ((section (".data"))) =
  46. CONFIG_SYS_SPD_BUS_NUM;
  47. #endif /* CONFIG_I2C_MULTI_BUS */
  48. static void _i2c_bus_reset(void)
  49. {
  50. struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
  51. int i;
  52. u8 dc;
  53. /* Reset status register */
  54. /* write 1 in SCMP and IRQA to clear these fields */
  55. out_8(&i2c->sts, 0x0A);
  56. /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
  57. out_8(&i2c->extsts, 0x8F);
  58. /* Place chip in the reset state */
  59. out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
  60. /* Check if bus is free */
  61. dc = in_8(&i2c->directcntl);
  62. if (!DIRCTNL_FREE(dc)){
  63. /* Try to set bus free state */
  64. out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
  65. /* Wait until we regain bus control */
  66. for (i = 0; i < 100; ++i) {
  67. dc = in_8(&i2c->directcntl);
  68. if (DIRCTNL_FREE(dc))
  69. break;
  70. /* Toggle SCL line */
  71. dc ^= IIC_DIRCNTL_SCC;
  72. out_8(&i2c->directcntl, dc);
  73. udelay(10);
  74. dc ^= IIC_DIRCNTL_SCC;
  75. out_8(&i2c->directcntl, dc);
  76. }
  77. }
  78. /* Remove reset */
  79. out_8(&i2c->xtcntlss, 0);
  80. }
  81. void i2c_init(int speed, int slaveaddr)
  82. {
  83. struct ppc4xx_i2c *i2c;
  84. int val, divisor;
  85. int bus;
  86. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  87. /*
  88. * Call board specific i2c bus reset routine before accessing the
  89. * environment, which might be in a chip on that bus. For details
  90. * about this problem see doc/I2C_Edge_Conditions.
  91. */
  92. i2c_init_board();
  93. #endif
  94. for (bus = 0; bus < CONFIG_SYS_MAX_I2C_BUS; bus++) {
  95. I2C_SET_BUS(bus);
  96. /* Set i2c pointer after calling I2C_SET_BUS() */
  97. i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
  98. /* Handle possible failed I2C state */
  99. /* FIXME: put this into i2c_init_board()? */
  100. _i2c_bus_reset();
  101. /* clear lo master address */
  102. out_8(&i2c->lmadr, 0);
  103. /* clear hi master address */
  104. out_8(&i2c->hmadr, 0);
  105. /* clear lo slave address */
  106. out_8(&i2c->lsadr, 0);
  107. /* clear hi slave address */
  108. out_8(&i2c->hsadr, 0);
  109. /* Clock divide Register */
  110. /* set divisor according to freq_opb */
  111. divisor = (get_OPB_freq() - 1) / 10000000;
  112. if (divisor == 0)
  113. divisor = 1;
  114. out_8(&i2c->clkdiv, divisor);
  115. /* no interrupts */
  116. out_8(&i2c->intrmsk, 0);
  117. /* clear transfer count */
  118. out_8(&i2c->xfrcnt, 0);
  119. /* clear extended control & stat */
  120. /* write 1 in SRC SRS SWC SWS to clear these fields */
  121. out_8(&i2c->xtcntlss, 0xF0);
  122. /* Mode Control Register
  123. Flush Slave/Master data buffer */
  124. out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
  125. val = in_8(&i2c->mdcntl);
  126. /* Ignore General Call, slave transfers are ignored,
  127. * disable interrupts, exit unknown bus state, enable hold
  128. * SCL 100kHz normaly or FastMode for 400kHz and above
  129. */
  130. val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
  131. if (speed >= 400000)
  132. val |= IIC_MDCNTL_FSM;
  133. out_8(&i2c->mdcntl, val);
  134. /* clear control reg */
  135. out_8(&i2c->cntl, 0x00);
  136. }
  137. /* set to SPD bus as default bus upon powerup */
  138. I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM);
  139. }
  140. /*
  141. * This code tries to use the features of the 405GP i2c
  142. * controller. It will transfer up to 4 bytes in one pass
  143. * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
  144. * is possible to do out16(lhz) transfers.
  145. *
  146. * cmd_type is 0 for write 1 for read.
  147. *
  148. * addr_len can take any value from 0-255, it is only limited
  149. * by the char, we could make it larger if needed. If it is
  150. * 0 we skip the address write cycle.
  151. *
  152. * Typical case is a Write of an addr followd by a Read. The
  153. * IBM FAQ does not cover this. On the last byte of the write
  154. * we don't set the creg CHT bit, and on the first bytes of the
  155. * read we set the RPST bit.
  156. *
  157. * It does not support address only transfers, there must be
  158. * a data part. If you want to write the address yourself, put
  159. * it in the data pointer.
  160. *
  161. * It does not support transfer to/from address 0.
  162. *
  163. * It does not check XFRCNT.
  164. */
  165. static int i2c_transfer(unsigned char cmd_type,
  166. unsigned char chip,
  167. unsigned char addr[],
  168. unsigned char addr_len,
  169. unsigned char data[],
  170. unsigned short data_len)
  171. {
  172. struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
  173. u8 *ptr;
  174. int reading;
  175. int tran, cnt;
  176. int result;
  177. int status;
  178. int i;
  179. u8 creg;
  180. if (data == 0 || data_len == 0) {
  181. /* Don't support data transfer of no length or to address 0 */
  182. printf( "i2c_transfer: bad call\n" );
  183. return IIC_NOK;
  184. }
  185. if (addr && addr_len) {
  186. ptr = addr;
  187. cnt = addr_len;
  188. reading = 0;
  189. } else {
  190. ptr = data;
  191. cnt = data_len;
  192. reading = cmd_type;
  193. }
  194. /* Clear Stop Complete Bit */
  195. out_8(&i2c->sts, IIC_STS_SCMP);
  196. /* Check init */
  197. i = 10;
  198. do {
  199. /* Get status */
  200. status = in_8(&i2c->sts);
  201. i--;
  202. } while ((status & IIC_STS_PT) && (i > 0));
  203. if (status & IIC_STS_PT) {
  204. result = IIC_NOK_TOUT;
  205. return(result);
  206. }
  207. /* flush the Master/Slave Databuffers */
  208. out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
  209. IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
  210. /* need to wait 4 OPB clocks? code below should take that long */
  211. /* 7-bit adressing */
  212. out_8(&i2c->hmadr, 0);
  213. out_8(&i2c->lmadr, chip);
  214. tran = 0;
  215. result = IIC_OK;
  216. creg = 0;
  217. while (tran != cnt && (result == IIC_OK)) {
  218. int bc,j;
  219. /*
  220. * Control register =
  221. * Normal transfer, 7-bits adressing, Transfer up to
  222. * bc bytes, Normal start, Transfer is a sequence of transfers
  223. */
  224. creg |= IIC_CNTL_PT;
  225. bc = (cnt - tran) > 4 ? 4 : cnt - tran;
  226. creg |= (bc - 1) << 4;
  227. /* if the real cmd type is write continue trans */
  228. if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
  229. creg |= IIC_CNTL_CHT;
  230. if (reading) {
  231. creg |= IIC_CNTL_READ;
  232. } else {
  233. for(j = 0; j < bc; j++) {
  234. /* Set buffer */
  235. out_8(&i2c->mdbuf, ptr[tran + j]);
  236. }
  237. }
  238. out_8(&i2c->cntl, creg);
  239. /*
  240. * Transfer is in progress
  241. * we have to wait for upto 5 bytes of data
  242. * 1 byte chip address+r/w bit then bc bytes
  243. * of data.
  244. * udelay(10) is 1 bit time at 100khz
  245. * Doubled for slop. 20 is too small.
  246. */
  247. i = 2 * 5 * 8;
  248. do {
  249. /* Get status */
  250. status = in_8(&i2c->sts);
  251. udelay(10);
  252. i--;
  253. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
  254. (i > 0));
  255. if (status & IIC_STS_ERR) {
  256. result = IIC_NOK;
  257. status = in_8(&i2c->extsts);
  258. /* Lost arbitration? */
  259. if (status & IIC_EXTSTS_LA)
  260. result = IIC_NOK_LA;
  261. /* Incomplete transfer? */
  262. if (status & IIC_EXTSTS_ICT)
  263. result = IIC_NOK_ICT;
  264. /* Transfer aborted? */
  265. if (status & IIC_EXTSTS_XFRA)
  266. result = IIC_NOK_XFRA;
  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. if (reading)
  295. creg = IIC_CNTL_RPST;
  296. }
  297. }
  298. return result;
  299. }
  300. int i2c_probe(uchar chip)
  301. {
  302. uchar buf[1];
  303. buf[0] = 0;
  304. /*
  305. * What is needed is to send the chip address and verify that the
  306. * address was <ACK>ed (i.e. there was a chip at that address which
  307. * drove the data line low).
  308. */
  309. return (i2c_transfer(1, chip << 1, 0, 0, buf, 1) != 0);
  310. }
  311. static int ppc4xx_i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer,
  312. int len, int read)
  313. {
  314. uchar xaddr[4];
  315. int ret;
  316. if (alen > 4) {
  317. printf("I2C: addr len %d not supported\n", alen);
  318. return 1;
  319. }
  320. if (alen > 0) {
  321. xaddr[0] = (addr >> 24) & 0xFF;
  322. xaddr[1] = (addr >> 16) & 0xFF;
  323. xaddr[2] = (addr >> 8) & 0xFF;
  324. xaddr[3] = addr & 0xFF;
  325. }
  326. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  327. /*
  328. * EEPROM chips that implement "address overflow" are ones
  329. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  330. * address and the extra bits end up in the "chip address"
  331. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  332. * four 256 byte chips.
  333. *
  334. * Note that we consider the length of the address field to
  335. * still be one byte because the extra address bits are
  336. * hidden in the chip address.
  337. */
  338. if (alen > 0)
  339. chip |= ((addr >> (alen * 8)) &
  340. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  341. #endif
  342. if ((ret = i2c_transfer(read, chip << 1, &xaddr[4 - alen], alen,
  343. buffer, len)) != 0) {
  344. printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  350. {
  351. return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 1);
  352. }
  353. int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  354. {
  355. return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 0);
  356. }
  357. #if defined(CONFIG_I2C_MULTI_BUS)
  358. /*
  359. * Functions for multiple I2C bus handling
  360. */
  361. unsigned int i2c_get_bus_num(void)
  362. {
  363. return i2c_bus_num;
  364. }
  365. int i2c_set_bus_num(unsigned int bus)
  366. {
  367. if (bus >= CONFIG_SYS_MAX_I2C_BUS)
  368. return -1;
  369. i2c_bus_num = bus;
  370. return 0;
  371. }
  372. #endif /* CONFIG_I2C_MULTI_BUS */
  373. #endif /* CONFIG_HARD_I2C */