ppc440gx_i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2005 Sandburst Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * Ported from arch/powerpc/cpu/ppc4xx/i2c.c by AS HARNOIS by
  24. * Travis B. Sawyer
  25. * Sandburst Corporation.
  26. */
  27. #include <common.h>
  28. #include <asm/ppc4xx.h>
  29. #include <asm/ppc4xx-i2c.h>
  30. #include <i2c.h>
  31. #include <command.h>
  32. #include "ppc440gx_i2c.h"
  33. #include <asm/io.h>
  34. #ifdef CONFIG_I2C_BUS1
  35. #define IIC_OK 0
  36. #define IIC_NOK 1
  37. #define IIC_NOK_LA 2 /* Lost arbitration */
  38. #define IIC_NOK_ICT 3 /* Incomplete transfer */
  39. #define IIC_NOK_XFRA 4 /* Transfer aborted */
  40. #define IIC_NOK_DATA 5 /* No data in buffer */
  41. #define IIC_NOK_TOUT 6 /* Transfer timeout */
  42. #define IIC_TIMEOUT 1 /* 1 second */
  43. #if defined(CONFIG_SYS_I2C_NOPROBES)
  44. static uchar i2c_no_probes[] = CONFIG_SYS_I2C_NOPROBES;
  45. #endif
  46. static struct ppc4xx_i2c *i2c = (struct ppc4xx_i2c *)I2C_REGISTERS_BUS1_BASE_ADDRESS;
  47. static void _i2c_bus1_reset (void)
  48. {
  49. int i, status;
  50. /* Reset status register */
  51. /* write 1 in SCMP and IRQA to clear these fields */
  52. out_8 (IIC_STS1, 0x0A);
  53. /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
  54. out_8 (IIC_EXTSTS1, 0x8F);
  55. __asm__ volatile ("eieio");
  56. /*
  57. * Get current state, reset bus
  58. * only if no transfers are pending.
  59. */
  60. i = 10;
  61. do {
  62. /* Get status */
  63. status = in_8 (IIC_STS1);
  64. udelay (500); /* 500us */
  65. i--;
  66. } while ((status & IIC_STS_PT) && (i > 0));
  67. /* Soft reset controller */
  68. status = in_8 (IIC_XTCNTLSS1);
  69. out_8 (IIC_XTCNTLSS1, (status | IIC_XTCNTLSS_SRST));
  70. __asm__ volatile ("eieio");
  71. /* make sure where in initial state, data hi, clock hi */
  72. out_8 (IIC_DIRECTCNTL1, 0xC);
  73. for (i = 0; i < 10; i++) {
  74. if ((in_8 (IIC_DIRECTCNTL1) & 0x3) != 0x3) {
  75. /* clock until we get to known state */
  76. out_8 (IIC_DIRECTCNTL1, 0x8); /* clock lo */
  77. udelay (100); /* 100us */
  78. out_8 (IIC_DIRECTCNTL1, 0xC); /* clock hi */
  79. udelay (100); /* 100us */
  80. } else {
  81. break;
  82. }
  83. }
  84. /* send start condition */
  85. out_8 (IIC_DIRECTCNTL1, 0x4);
  86. udelay (1000); /* 1ms */
  87. /* send stop condition */
  88. out_8 (IIC_DIRECTCNTL1, 0xC);
  89. udelay (1000); /* 1ms */
  90. /* Unreset controller */
  91. out_8 (IIC_XTCNTLSS1, (status & ~IIC_XTCNTLSS_SRST));
  92. udelay (1000); /* 1ms */
  93. }
  94. void i2c1_init (int speed, int slaveadd)
  95. {
  96. sys_info_t sysInfo;
  97. unsigned long freqOPB;
  98. int val, divisor;
  99. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  100. /* call board specific i2c bus reset routine before accessing the */
  101. /* environment, which might be in a chip on that bus. For details */
  102. /* about this problem see doc/I2C_Edge_Conditions. */
  103. i2c_init_board();
  104. #endif
  105. /* Handle possible failed I2C state */
  106. /* FIXME: put this into i2c_init_board()? */
  107. _i2c_bus1_reset ();
  108. /* clear lo master address */
  109. out_8 (IIC_LMADR1, 0);
  110. /* clear hi master address */
  111. out_8 (IIC_HMADR1, 0);
  112. /* clear lo slave address */
  113. out_8 (IIC_LSADR1, 0);
  114. /* clear hi slave address */
  115. out_8 (IIC_HSADR1, 0);
  116. /* Clock divide Register */
  117. /* get OPB frequency */
  118. get_sys_info (&sysInfo);
  119. freqOPB = sysInfo.freqPLB / sysInfo.pllOpbDiv;
  120. /* set divisor according to freqOPB */
  121. divisor = (freqOPB - 1) / 10000000;
  122. if (divisor == 0)
  123. divisor = 1;
  124. out_8 (IIC_CLKDIV1, divisor);
  125. /* no interrupts */
  126. out_8 (IIC_INTRMSK1, 0);
  127. /* clear transfer count */
  128. out_8 (IIC_XFRCNT1, 0);
  129. /* clear extended control & stat */
  130. /* write 1 in SRC SRS SWC SWS to clear these fields */
  131. out_8 (IIC_XTCNTLSS1, 0xF0);
  132. /* Mode Control Register
  133. Flush Slave/Master data buffer */
  134. out_8 (IIC_MDCNTL1, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
  135. __asm__ volatile ("eieio");
  136. val = in_8(IIC_MDCNTL1);
  137. __asm__ volatile ("eieio");
  138. /* Ignore General Call, slave transfers are ignored,
  139. disable interrupts, exit unknown bus state, enable hold
  140. SCL
  141. 100kHz normaly or FastMode for 400kHz and above
  142. */
  143. val |= IIC_MDCNTL_EUBS|IIC_MDCNTL_HSCL;
  144. if( speed >= 400000 ){
  145. val |= IIC_MDCNTL_FSM;
  146. }
  147. out_8 (IIC_MDCNTL1, val);
  148. /* clear control reg */
  149. out_8 (IIC_CNTL1, 0x00);
  150. __asm__ volatile ("eieio");
  151. }
  152. /*
  153. This code tries to use the features of the 405GP i2c
  154. controller. It will transfer up to 4 bytes in one pass
  155. on the loop. It only does out_8(lbz) to the buffer when it
  156. is possible to do out16(lhz) transfers.
  157. cmd_type is 0 for write 1 for read.
  158. addr_len can take any value from 0-255, it is only limited
  159. by the char, we could make it larger if needed. If it is
  160. 0 we skip the address write cycle.
  161. Typical case is a Write of an addr followd by a Read. The
  162. IBM FAQ does not cover this. On the last byte of the write
  163. we don't set the creg CHT bit, and on the first bytes of the
  164. read we set the RPST bit.
  165. It does not support address only transfers, there must be
  166. a data part. If you want to write the address yourself, put
  167. it in the data pointer.
  168. It does not support transfer to/from address 0.
  169. It does not check XFRCNT.
  170. */
  171. static
  172. int i2c_transfer1(unsigned char cmd_type,
  173. unsigned char chip,
  174. unsigned char addr[],
  175. unsigned char addr_len,
  176. unsigned char data[],
  177. unsigned short data_len )
  178. {
  179. unsigned char* ptr;
  180. int reading;
  181. int tran,cnt;
  182. int result;
  183. int status;
  184. int i;
  185. uchar creg;
  186. if( data == 0 || data_len == 0 ){
  187. /*Don't support data transfer of no length or to address 0*/
  188. printf( "i2c_transfer: bad call\n" );
  189. return IIC_NOK;
  190. }
  191. if( addr && addr_len ){
  192. ptr = addr;
  193. cnt = addr_len;
  194. reading = 0;
  195. }else{
  196. ptr = data;
  197. cnt = data_len;
  198. reading = cmd_type;
  199. }
  200. /*Clear Stop Complete Bit*/
  201. out_8(IIC_STS1,IIC_STS_SCMP);
  202. /* Check init */
  203. i=10;
  204. do {
  205. /* Get status */
  206. status = in_8(IIC_STS1);
  207. __asm__ volatile("eieio");
  208. i--;
  209. } while ((status & IIC_STS_PT) && (i>0));
  210. if (status & IIC_STS_PT) {
  211. result = IIC_NOK_TOUT;
  212. return(result);
  213. }
  214. /*flush the Master/Slave Databuffers*/
  215. out_8(IIC_MDCNTL1, ((in_8(IIC_MDCNTL1))|IIC_MDCNTL_FMDB|IIC_MDCNTL_FSDB));
  216. /*need to wait 4 OPB clocks? code below should take that long*/
  217. /* 7-bit adressing */
  218. out_8(IIC_HMADR1,0);
  219. out_8(IIC_LMADR1, chip);
  220. __asm__ volatile("eieio");
  221. tran = 0;
  222. result = IIC_OK;
  223. creg = 0;
  224. while ( tran != cnt && (result == IIC_OK)) {
  225. int bc,j;
  226. /* Control register =
  227. Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start,
  228. Transfer is a sequence of transfers
  229. */
  230. creg |= IIC_CNTL_PT;
  231. bc = (cnt - tran) > 4 ? 4 :
  232. cnt - tran;
  233. creg |= (bc-1)<<4;
  234. /* if the real cmd type is write continue trans*/
  235. if ( (!cmd_type && (ptr == addr)) || ((tran+bc) != cnt) )
  236. creg |= IIC_CNTL_CHT;
  237. if (reading)
  238. creg |= IIC_CNTL_READ;
  239. else {
  240. for(j=0; j<bc; j++) {
  241. /* Set buffer */
  242. out_8(IIC_MDBUF1,ptr[tran+j]);
  243. __asm__ volatile("eieio");
  244. }
  245. }
  246. out_8(IIC_CNTL1, creg );
  247. __asm__ volatile("eieio");
  248. /* Transfer is in progress
  249. we have to wait for upto 5 bytes of data
  250. 1 byte chip address+r/w bit then bc bytes
  251. of data.
  252. udelay(10) is 1 bit time at 100khz
  253. Doubled for slop. 20 is too small.
  254. */
  255. i=2*5*8;
  256. do {
  257. /* Get status */
  258. status = in_8(IIC_STS1);
  259. __asm__ volatile("eieio");
  260. udelay (10);
  261. i--;
  262. } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR)
  263. && (i>0));
  264. if (status & IIC_STS_ERR) {
  265. result = IIC_NOK;
  266. status = in_8 (IIC_EXTSTS1);
  267. /* Lost arbitration? */
  268. if (status & IIC_EXTSTS_LA)
  269. result = IIC_NOK_LA;
  270. /* Incomplete transfer? */
  271. if (status & IIC_EXTSTS_ICT)
  272. result = IIC_NOK_ICT;
  273. /* Transfer aborted? */
  274. if (status & IIC_EXTSTS_XFRA)
  275. result = IIC_NOK_XFRA;
  276. } else if ( status & IIC_STS_PT) {
  277. result = IIC_NOK_TOUT;
  278. }
  279. /* Command is reading => get buffer */
  280. if ((reading) && (result == IIC_OK)) {
  281. /* Are there data in buffer */
  282. if (status & IIC_STS_MDBS) {
  283. /*
  284. even if we have data we have to wait 4OPB clocks
  285. for it to hit the front of the FIFO, after that
  286. we can just read. We should check XFCNT here and
  287. if the FIFO is full there is no need to wait.
  288. */
  289. udelay (1);
  290. for(j=0;j<bc;j++) {
  291. ptr[tran+j] = in_8(IIC_MDBUF1);
  292. __asm__ volatile("eieio");
  293. }
  294. } else
  295. result = IIC_NOK_DATA;
  296. }
  297. creg = 0;
  298. tran+=bc;
  299. if( ptr == addr && tran == cnt ) {
  300. ptr = data;
  301. cnt = data_len;
  302. tran = 0;
  303. reading = cmd_type;
  304. if( reading )
  305. creg = IIC_CNTL_RPST;
  306. }
  307. }
  308. return (result);
  309. }
  310. int i2c_probe1 (uchar chip)
  311. {
  312. uchar buf[1];
  313. buf[0] = 0;
  314. /*
  315. * What is needed is to send the chip address and verify that the
  316. * address was <ACK>ed (i.e. there was a chip at that address which
  317. * drove the data line low).
  318. */
  319. return(i2c_transfer1 (1, chip << 1, 0,0, buf, 1) != 0);
  320. }
  321. int i2c_read1 (uchar chip, uint addr, int alen, uchar * buffer, int len)
  322. {
  323. uchar xaddr[4];
  324. int ret;
  325. if ( alen > 4 ) {
  326. printf ("I2C read: addr len %d not supported\n", alen);
  327. return 1;
  328. }
  329. if ( alen > 0 ) {
  330. xaddr[0] = (addr >> 24) & 0xFF;
  331. xaddr[1] = (addr >> 16) & 0xFF;
  332. xaddr[2] = (addr >> 8) & 0xFF;
  333. xaddr[3] = addr & 0xFF;
  334. }
  335. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  336. /*
  337. * EEPROM chips that implement "address overflow" are ones
  338. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  339. * address and the extra bits end up in the "chip address"
  340. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  341. * four 256 byte chips.
  342. *
  343. * Note that we consider the length of the address field to
  344. * still be one byte because the extra address bits are
  345. * hidden in the chip address.
  346. */
  347. if( alen > 0 )
  348. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  349. #endif
  350. if( (ret = i2c_transfer1( 1, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
  351. printf( "I2c read: failed %d\n", ret);
  352. return 1;
  353. }
  354. return 0;
  355. }
  356. int i2c_write1 (uchar chip, uint addr, int alen, uchar * buffer, int len)
  357. {
  358. uchar xaddr[4];
  359. if ( alen > 4 ) {
  360. printf ("I2C write: addr len %d not supported\n", alen);
  361. return 1;
  362. }
  363. if ( alen > 0 ) {
  364. xaddr[0] = (addr >> 24) & 0xFF;
  365. xaddr[1] = (addr >> 16) & 0xFF;
  366. xaddr[2] = (addr >> 8) & 0xFF;
  367. xaddr[3] = addr & 0xFF;
  368. }
  369. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  370. /*
  371. * EEPROM chips that implement "address overflow" are ones
  372. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  373. * address and the extra bits end up in the "chip address"
  374. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  375. * four 256 byte chips.
  376. *
  377. * Note that we consider the length of the address field to
  378. * still be one byte because the extra address bits are
  379. * hidden in the chip address.
  380. */
  381. if( alen > 0 )
  382. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  383. #endif
  384. return (i2c_transfer1( 0, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
  385. }
  386. /*-----------------------------------------------------------------------
  387. * Read a register
  388. */
  389. uchar i2c_reg_read1(uchar i2c_addr, uchar reg)
  390. {
  391. uchar buf;
  392. i2c_read1(i2c_addr, reg, 1, &buf, (uchar)1);
  393. return(buf);
  394. }
  395. /*-----------------------------------------------------------------------
  396. * Write a register
  397. */
  398. void i2c_reg_write1(uchar i2c_addr, uchar reg, uchar val)
  399. {
  400. i2c_write1(i2c_addr, reg, 1, &val, 1);
  401. }
  402. int do_i2c1_probe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  403. {
  404. int j;
  405. #if defined(CONFIG_SYS_I2C_NOPROBES)
  406. int k, skip;
  407. #endif
  408. puts ("Valid chip addresses:");
  409. for(j = 0; j < 128; j++) {
  410. #if defined(CONFIG_SYS_I2C_NOPROBES)
  411. skip = 0;
  412. for (k = 0; k < sizeof(i2c_no_probes); k++){
  413. if (j == i2c_no_probes[k]){
  414. skip = 1;
  415. break;
  416. }
  417. }
  418. if (skip)
  419. continue;
  420. #endif
  421. if(i2c_probe1(j) == 0) {
  422. printf(" %02X", j);
  423. }
  424. }
  425. putc ('\n');
  426. #if defined(CONFIG_SYS_I2C_NOPROBES)
  427. puts ("Excluded chip addresses:");
  428. for( k = 0; k < sizeof(i2c_no_probes); k++ )
  429. printf(" %02X", i2c_no_probes[k] );
  430. putc ('\n');
  431. #endif
  432. return 0;
  433. }
  434. U_BOOT_CMD(
  435. iprobe1, 1, 1, do_i2c1_probe,
  436. "probe to discover valid I2C chip addresses",
  437. ""
  438. );
  439. #endif /* CONFIG_I2C_BUS1 */