i2c.c 12 KB

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