i2c.c 11 KB

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