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