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 <ppc4xx.h>
  30. #include <4xx_i2c.h>
  31. #include <i2c.h>
  32. #include <asm-ppc/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 = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
  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. /* Handle possible failed I2C state */
  97. /* FIXME: put this into i2c_init_board()? */
  98. _i2c_bus_reset();
  99. /* clear lo master address */
  100. out_8(&i2c->lmadr, 0);
  101. /* clear hi master address */
  102. out_8(&i2c->hmadr, 0);
  103. /* clear lo slave address */
  104. out_8(&i2c->lsadr, 0);
  105. /* clear hi slave address */
  106. out_8(&i2c->hsadr, 0);
  107. /* Clock divide Register */
  108. /* set divisor according to freq_opb */
  109. divisor = (get_OPB_freq() - 1) / 10000000;
  110. if (divisor == 0)
  111. divisor = 1;
  112. out_8(&i2c->clkdiv, divisor);
  113. /* no interrupts */
  114. out_8(&i2c->intrmsk, 0);
  115. /* clear transfer count */
  116. out_8(&i2c->xfrcnt, 0);
  117. /* clear extended control & stat */
  118. /* write 1 in SRC SRS SWC SWS to clear these fields */
  119. out_8(&i2c->xtcntlss, 0xF0);
  120. /* Mode Control Register
  121. Flush Slave/Master data buffer */
  122. out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
  123. val = in_8(&i2c->mdcntl);
  124. /* Ignore General Call, slave transfers are ignored,
  125. * disable interrupts, exit unknown bus state, enable hold
  126. * SCL 100kHz normaly or FastMode for 400kHz and above
  127. */
  128. val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
  129. if (speed >= 400000)
  130. val |= IIC_MDCNTL_FSM;
  131. out_8(&i2c->mdcntl, val);
  132. /* clear control reg */
  133. out_8(&i2c->cntl, 0x00);
  134. }
  135. /* set to SPD bus as default bus upon powerup */
  136. I2C_SET_BUS(CONFIG_SYS_SPD_BUS_NUM);
  137. }
  138. /*
  139. * This code tries to use the features of the 405GP i2c
  140. * controller. It will transfer up to 4 bytes in one pass
  141. * on the loop. It only does out_8((u8 *)lbz) to the buffer when it
  142. * is possible to do out16(lhz) transfers.
  143. *
  144. * cmd_type is 0 for write 1 for read.
  145. *
  146. * addr_len can take any value from 0-255, it is only limited
  147. * by the char, we could make it larger if needed. If it is
  148. * 0 we skip the address write cycle.
  149. *
  150. * Typical case is a Write of an addr followd by a Read. The
  151. * IBM FAQ does not cover this. On the last byte of the write
  152. * we don't set the creg CHT bit, and on the first bytes of the
  153. * read we set the RPST bit.
  154. *
  155. * It does not support address only transfers, there must be
  156. * a data part. If you want to write the address yourself, put
  157. * it in the data pointer.
  158. *
  159. * It does not support transfer to/from address 0.
  160. *
  161. * It does not check XFRCNT.
  162. */
  163. static int i2c_transfer(unsigned char cmd_type,
  164. unsigned char chip,
  165. unsigned char addr[],
  166. unsigned char addr_len,
  167. unsigned char data[],
  168. unsigned short data_len)
  169. {
  170. struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_BASE_ADDR;
  171. u8 *ptr;
  172. int reading;
  173. int tran, cnt;
  174. int result;
  175. int status;
  176. int i;
  177. u8 creg;
  178. if (data == 0 || data_len == 0) {
  179. /* Don't support data transfer of no length or to address 0 */
  180. printf( "i2c_transfer: bad call\n" );
  181. return IIC_NOK;
  182. }
  183. if (addr && addr_len) {
  184. ptr = addr;
  185. cnt = addr_len;
  186. reading = 0;
  187. } else {
  188. ptr = data;
  189. cnt = data_len;
  190. reading = cmd_type;
  191. }
  192. /* Clear Stop Complete Bit */
  193. out_8(&i2c->sts, IIC_STS_SCMP);
  194. /* Check init */
  195. i = 10;
  196. do {
  197. /* Get status */
  198. status = in_8(&i2c->sts);
  199. i--;
  200. } while ((status & IIC_STS_PT) && (i > 0));
  201. if (status & IIC_STS_PT) {
  202. result = IIC_NOK_TOUT;
  203. return(result);
  204. }
  205. /* flush the Master/Slave Databuffers */
  206. out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
  207. IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
  208. /* need to wait 4 OPB clocks? code below should take that long */
  209. /* 7-bit adressing */
  210. out_8(&i2c->hmadr, 0);
  211. out_8(&i2c->lmadr, chip);
  212. tran = 0;
  213. result = IIC_OK;
  214. creg = 0;
  215. while (tran != cnt && (result == IIC_OK)) {
  216. int bc,j;
  217. /*
  218. * Control register =
  219. * Normal transfer, 7-bits adressing, Transfer up to
  220. * bc bytes, Normal start, Transfer is a sequence of transfers
  221. */
  222. creg |= IIC_CNTL_PT;
  223. bc = (cnt - tran) > 4 ? 4 : cnt - tran;
  224. creg |= (bc - 1) << 4;
  225. /* if the real cmd type is write continue trans */
  226. if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
  227. creg |= IIC_CNTL_CHT;
  228. if (reading) {
  229. creg |= IIC_CNTL_READ;
  230. } else {
  231. for(j = 0; j < bc; j++) {
  232. /* Set buffer */
  233. out_8(&i2c->mdbuf, ptr[tran + j]);
  234. }
  235. }
  236. out_8(&i2c->cntl, creg);
  237. /*
  238. * Transfer is in progress
  239. * we have to wait for upto 5 bytes of data
  240. * 1 byte chip address+r/w bit then bc bytes
  241. * of data.
  242. * udelay(10) is 1 bit time at 100khz
  243. * Doubled for slop. 20 is too small.
  244. */
  245. i = 2 * 5 * 8;
  246. do {
  247. /* Get status */
  248. status = in_8(&i2c->sts);
  249. udelay(10);
  250. i--;
  251. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
  252. (i > 0));
  253. if (status & IIC_STS_ERR) {
  254. result = IIC_NOK;
  255. status = in_8(&i2c->extsts);
  256. /* Lost arbitration? */
  257. if (status & IIC_EXTSTS_LA)
  258. result = IIC_NOK_LA;
  259. /* Incomplete transfer? */
  260. if (status & IIC_EXTSTS_ICT)
  261. result = IIC_NOK_ICT;
  262. /* Transfer aborted? */
  263. if (status & IIC_EXTSTS_XFRA)
  264. result = IIC_NOK_XFRA;
  265. } else if ( status & IIC_STS_PT) {
  266. result = IIC_NOK_TOUT;
  267. }
  268. /* Command is reading => get buffer */
  269. if ((reading) && (result == IIC_OK)) {
  270. /* Are there data in buffer */
  271. if (status & IIC_STS_MDBS) {
  272. /*
  273. * even if we have data we have to wait 4OPB
  274. * clocks for it to hit the front of the FIFO,
  275. * after that we can just read. We should check
  276. * XFCNT here and if the FIFO is full there is
  277. * no need to wait.
  278. */
  279. udelay(1);
  280. for (j = 0; j < bc; j++)
  281. ptr[tran + j] = in_8(&i2c->mdbuf);
  282. } else
  283. result = IIC_NOK_DATA;
  284. }
  285. creg = 0;
  286. tran += bc;
  287. if (ptr == addr && tran == cnt) {
  288. ptr = data;
  289. cnt = data_len;
  290. tran = 0;
  291. reading = cmd_type;
  292. if (reading)
  293. creg = IIC_CNTL_RPST;
  294. }
  295. }
  296. return result;
  297. }
  298. int i2c_probe(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(1, chip << 1, 0, 0, buf, 1) != 0);
  308. }
  309. static int ppc4xx_i2c_transfer(uchar chip, uint addr, int alen, uchar *buffer,
  310. 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. if ((ret = i2c_transfer(read, chip << 1, &xaddr[4 - alen], alen,
  341. buffer, len)) != 0) {
  342. if (gd->have_console) {
  343. printf("I2C %s: failed %d\n",
  344. read ? "read" : "write", ret);
  345. }
  346. return 1;
  347. }
  348. return 0;
  349. }
  350. int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  351. {
  352. return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 1);
  353. }
  354. int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  355. {
  356. return ppc4xx_i2c_transfer(chip, addr, alen, buffer, len, 0);
  357. }
  358. #if defined(CONFIG_I2C_MULTI_BUS)
  359. /*
  360. * Functions for multiple I2C bus handling
  361. */
  362. unsigned int i2c_get_bus_num(void)
  363. {
  364. return i2c_bus_num;
  365. }
  366. int i2c_set_bus_num(unsigned int bus)
  367. {
  368. if (bus >= CONFIG_SYS_MAX_I2C_BUS)
  369. return -1;
  370. i2c_bus_num = bus;
  371. return 0;
  372. }
  373. #endif /* CONFIG_I2C_MULTI_BUS */
  374. #endif /* CONFIG_HARD_I2C */