ppc440gx_i2c.c 12 KB

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