i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #ifdef CFG_SPD_BUS_NUM
  42. static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = CFG_SPD_BUS_NUM;
  43. #else
  44. static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = 0;
  45. #endif
  46. #endif /* CONFIG_I2C_MULTI_BUS */
  47. static void _i2c_bus_reset(void)
  48. {
  49. int i;
  50. u8 dc;
  51. /* Reset status register */
  52. /* write 1 in SCMP and IRQA to clear these fields */
  53. out_8((u8 *)IIC_STS, 0x0A);
  54. /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
  55. out_8((u8 *)IIC_EXTSTS, 0x8F);
  56. /* Place chip in the reset state */
  57. out_8((u8 *)IIC_XTCNTLSS, IIC_XTCNTLSS_SRST);
  58. /* Check if bus is free */
  59. dc = in_8((u8 *)IIC_DIRECTCNTL);
  60. if (!DIRCTNL_FREE(dc)){
  61. /* Try to set bus free state */
  62. out_8((u8 *)IIC_DIRECTCNTL, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
  63. /* Wait until we regain bus control */
  64. for (i = 0; i < 100; ++i) {
  65. dc = in_8((u8 *)IIC_DIRECTCNTL);
  66. if (DIRCTNL_FREE(dc))
  67. break;
  68. /* Toggle SCL line */
  69. dc ^= IIC_DIRCNTL_SCC;
  70. out_8((u8 *)IIC_DIRECTCNTL, dc);
  71. udelay(10);
  72. dc ^= IIC_DIRCNTL_SCC;
  73. out_8((u8 *)IIC_DIRECTCNTL, dc);
  74. }
  75. }
  76. /* Remove reset */
  77. out_8((u8 *)IIC_XTCNTLSS, 0);
  78. }
  79. void i2c_init(int speed, int slaveadd)
  80. {
  81. sys_info_t sysInfo;
  82. unsigned long freqOPB;
  83. int val, divisor;
  84. int bus;
  85. #ifdef CFG_I2C_INIT_BOARD
  86. /* call board specific i2c bus reset routine before accessing the */
  87. /* environment, which might be in a chip on that bus. For details */
  88. /* about this problem see doc/I2C_Edge_Conditions. */
  89. i2c_init_board();
  90. #endif
  91. for (bus = 0; bus < CFG_MAX_I2C_BUS; bus++) {
  92. I2C_SET_BUS(bus);
  93. /* Handle possible failed I2C state */
  94. /* FIXME: put this into i2c_init_board()? */
  95. _i2c_bus_reset();
  96. /* clear lo master address */
  97. out_8((u8 *)IIC_LMADR, 0);
  98. /* clear hi master address */
  99. out_8((u8 *)IIC_HMADR, 0);
  100. /* clear lo slave address */
  101. out_8((u8 *)IIC_LSADR, 0);
  102. /* clear hi slave address */
  103. out_8((u8 *)IIC_HSADR, 0);
  104. /* Clock divide Register */
  105. /* get OPB frequency */
  106. get_sys_info(&sysInfo);
  107. freqOPB = sysInfo.freqPLB / sysInfo.pllOpbDiv;
  108. /* set divisor according to freqOPB */
  109. divisor = (freqOPB - 1) / 10000000;
  110. if (divisor == 0)
  111. divisor = 1;
  112. out_8((u8 *)IIC_CLKDIV, divisor);
  113. /* no interrupts */
  114. out_8((u8 *)IIC_INTRMSK, 0);
  115. /* clear transfer count */
  116. out_8((u8 *)IIC_XFRCNT, 0);
  117. /* clear extended control & stat */
  118. /* write 1 in SRC SRS SWC SWS to clear these fields */
  119. out_8((u8 *)IIC_XTCNTLSS, 0xF0);
  120. /* Mode Control Register
  121. Flush Slave/Master data buffer */
  122. out_8((u8 *)IIC_MDCNTL, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
  123. val = in_8((u8 *)IIC_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((u8 *)IIC_MDCNTL, val);
  132. /* clear control reg */
  133. out_8((u8 *)IIC_CNTL, 0x00);
  134. }
  135. /* set to SPD bus as default bus upon powerup */
  136. I2C_SET_BUS(CFG_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. unsigned char* ptr;
  171. int reading;
  172. int tran,cnt;
  173. int result;
  174. int status;
  175. int i;
  176. uchar creg;
  177. if (data == 0 || data_len == 0) {
  178. /* Don't support data transfer of no length or to address 0 */
  179. printf( "i2c_transfer: bad call\n" );
  180. return IIC_NOK;
  181. }
  182. if (addr && addr_len) {
  183. ptr = addr;
  184. cnt = addr_len;
  185. reading = 0;
  186. } else {
  187. ptr = data;
  188. cnt = data_len;
  189. reading = cmd_type;
  190. }
  191. /* Clear Stop Complete Bit */
  192. out_8((u8 *)IIC_STS, IIC_STS_SCMP);
  193. /* Check init */
  194. i = 10;
  195. do {
  196. /* Get status */
  197. status = in_8((u8 *)IIC_STS);
  198. i--;
  199. } while ((status & IIC_STS_PT) && (i > 0));
  200. if (status & IIC_STS_PT) {
  201. result = IIC_NOK_TOUT;
  202. return(result);
  203. }
  204. /* flush the Master/Slave Databuffers */
  205. out_8((u8 *)IIC_MDCNTL, ((in_8((u8 *)IIC_MDCNTL))|IIC_MDCNTL_FMDB|IIC_MDCNTL_FSDB));
  206. /* need to wait 4 OPB clocks? code below should take that long */
  207. /* 7-bit adressing */
  208. out_8((u8 *)IIC_HMADR, 0);
  209. out_8((u8 *)IIC_LMADR, chip);
  210. tran = 0;
  211. result = IIC_OK;
  212. creg = 0;
  213. while (tran != cnt && (result == IIC_OK)) {
  214. int bc,j;
  215. /* Control register =
  216. * Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start,
  217. * Transfer is a sequence of transfers
  218. */
  219. creg |= IIC_CNTL_PT;
  220. bc = (cnt - tran) > 4 ? 4 : cnt - tran;
  221. creg |= (bc - 1) << 4;
  222. /* if the real cmd type is write continue trans */
  223. if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
  224. creg |= IIC_CNTL_CHT;
  225. if (reading)
  226. creg |= IIC_CNTL_READ;
  227. else
  228. for(j=0; j < bc; j++)
  229. /* Set buffer */
  230. out_8((u8 *)IIC_MDBUF, ptr[tran+j]);
  231. out_8((u8 *)IIC_CNTL, creg);
  232. /* Transfer is in progress
  233. * we have to wait for upto 5 bytes of data
  234. * 1 byte chip address+r/w bit then bc bytes
  235. * of data.
  236. * udelay(10) is 1 bit time at 100khz
  237. * Doubled for slop. 20 is too small.
  238. */
  239. i = 2*5*8;
  240. do {
  241. /* Get status */
  242. status = in_8((u8 *)IIC_STS);
  243. udelay(10);
  244. i--;
  245. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) && (i > 0));
  246. if (status & IIC_STS_ERR) {
  247. result = IIC_NOK;
  248. status = in_8((u8 *)IIC_EXTSTS);
  249. /* Lost arbitration? */
  250. if (status & IIC_EXTSTS_LA)
  251. result = IIC_NOK_LA;
  252. /* Incomplete transfer? */
  253. if (status & IIC_EXTSTS_ICT)
  254. result = IIC_NOK_ICT;
  255. /* Transfer aborted? */
  256. if (status & IIC_EXTSTS_XFRA)
  257. result = IIC_NOK_XFRA;
  258. } else if ( status & IIC_STS_PT) {
  259. result = IIC_NOK_TOUT;
  260. }
  261. /* Command is reading => get buffer */
  262. if ((reading) && (result == IIC_OK)) {
  263. /* Are there data in buffer */
  264. if (status & IIC_STS_MDBS) {
  265. /*
  266. * even if we have data we have to wait 4OPB clocks
  267. * for it to hit the front of the FIFO, after that
  268. * we can just read. We should check XFCNT here and
  269. * if the FIFO is full there is no need to wait.
  270. */
  271. udelay(1);
  272. for (j=0; j<bc; j++)
  273. ptr[tran+j] = in_8((u8 *)IIC_MDBUF);
  274. } else
  275. result = IIC_NOK_DATA;
  276. }
  277. creg = 0;
  278. tran += bc;
  279. if (ptr == addr && tran == cnt) {
  280. ptr = data;
  281. cnt = data_len;
  282. tran = 0;
  283. reading = cmd_type;
  284. if (reading)
  285. creg = IIC_CNTL_RPST;
  286. }
  287. }
  288. return (result);
  289. }
  290. int i2c_probe(uchar chip)
  291. {
  292. uchar buf[1];
  293. buf[0] = 0;
  294. /*
  295. * What is needed is to send the chip address and verify that the
  296. * address was <ACK>ed (i.e. there was a chip at that address which
  297. * drove the data line low).
  298. */
  299. return (i2c_transfer(1, chip << 1, 0,0, buf, 1) != 0);
  300. }
  301. int i2c_read(uchar chip, uint addr, int alen, uchar * buffer, int len)
  302. {
  303. uchar xaddr[4];
  304. int ret;
  305. if (alen > 4) {
  306. printf ("I2C read: addr len %d not supported\n", alen);
  307. return 1;
  308. }
  309. if (alen > 0) {
  310. xaddr[0] = (addr >> 24) & 0xFF;
  311. xaddr[1] = (addr >> 16) & 0xFF;
  312. xaddr[2] = (addr >> 8) & 0xFF;
  313. xaddr[3] = addr & 0xFF;
  314. }
  315. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  316. /*
  317. * EEPROM chips that implement "address overflow" are ones
  318. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  319. * address and the extra bits end up in the "chip address"
  320. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  321. * four 256 byte chips.
  322. *
  323. * Note that we consider the length of the address field to
  324. * still be one byte because the extra address bits are
  325. * hidden in the chip address.
  326. */
  327. if (alen > 0)
  328. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  329. #endif
  330. if ((ret = i2c_transfer(1, chip<<1, &xaddr[4-alen], alen, buffer, len)) != 0) {
  331. if (gd->have_console)
  332. printf( "I2c read: failed %d\n", ret);
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. int i2c_write(uchar chip, uint addr, int alen, uchar * buffer, int len)
  338. {
  339. uchar xaddr[4];
  340. if (alen > 4) {
  341. printf ("I2C write: addr len %d not supported\n", alen);
  342. return 1;
  343. }
  344. if (alen > 0) {
  345. xaddr[0] = (addr >> 24) & 0xFF;
  346. xaddr[1] = (addr >> 16) & 0xFF;
  347. xaddr[2] = (addr >> 8) & 0xFF;
  348. xaddr[3] = addr & 0xFF;
  349. }
  350. #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
  351. /*
  352. * EEPROM chips that implement "address overflow" are ones
  353. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  354. * address and the extra bits end up in the "chip address"
  355. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  356. * four 256 byte chips.
  357. *
  358. * Note that we consider the length of the address field to
  359. * still be one byte because the extra address bits are
  360. * hidden in the chip address.
  361. */
  362. if (alen > 0)
  363. chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
  364. #endif
  365. return (i2c_transfer(0, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
  366. }
  367. /*-----------------------------------------------------------------------
  368. * Read a register
  369. */
  370. uchar i2c_reg_read(uchar i2c_addr, uchar reg)
  371. {
  372. uchar buf;
  373. i2c_read(i2c_addr, reg, 1, &buf, 1);
  374. return (buf);
  375. }
  376. /*-----------------------------------------------------------------------
  377. * Write a register
  378. */
  379. void i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
  380. {
  381. i2c_write(i2c_addr, reg, 1, &val, 1);
  382. }
  383. #if defined(CONFIG_I2C_MULTI_BUS)
  384. /*
  385. * Functions for multiple I2C bus handling
  386. */
  387. unsigned int i2c_get_bus_num(void)
  388. {
  389. return i2c_bus_num;
  390. }
  391. int i2c_set_bus_num(unsigned int bus)
  392. {
  393. if (bus >= CFG_MAX_I2C_BUS)
  394. return -1;
  395. i2c_bus_num = bus;
  396. return 0;
  397. }
  398. /* TODO: add 100/400k switching */
  399. unsigned int i2c_get_bus_speed(void)
  400. {
  401. return CFG_I2C_SPEED;
  402. }
  403. int i2c_set_bus_speed(unsigned int speed)
  404. {
  405. if (speed != CFG_I2C_SPEED)
  406. return -1;
  407. return 0;
  408. }
  409. #endif /* CONFIG_I2C_MULTI_BUS */
  410. #endif /* CONFIG_HARD_I2C */