summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/mvme5500/GT64260/MVME5500I2C.c
blob: 107b8f15cbd72bffff4564244dd07e9c562aa364 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * To read information of the EEPROM via the I2C
 */

/*
 * Copyright (c) 2003, 2004 Brookhaven National Laboratory
 * Author:    S. Kate Feng <feng1@bnl.gov>
 * All rights reserved.
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution.
 */

#include <bsp.h>
#include <rtems/bspIo.h>      /* printk */
#include <stdint.h>           /* uint32_t */
#include "bsp/GT64260TWSI.h"

/* #define I2C_DEBUG*/

static unsigned char I2cAddrPack(unsigned char busAddr,uint32_t offset)
{
  return(busAddr | ((offset & 0x700) >> 7));
}

static unsigned char I2cDevByteAddr(uint32_t devA2A1A0, unsigned char byteNum)
{
  return(( devA2A1A0 >>(byteNum*8)) & 0xff);
}
/****************************************************************************
* I2Cread_eeprom - read EEPROM VPD from the I2C
*/
int I2Cread_eeprom(
  unsigned char  I2cBusAddr,
  uint32_t       devA2A1A0,
  uint32_t       AddrBytes,
  unsigned char *pBuff,
  uint32_t       numBytes
)
{
  int status=0, lastByte=0;

  switch (AddrBytes) {
    case 1:
      if ((status=GT64260TWSIstart()) != -1) {
        if ((status=GT64260TWSIwrite(I2cAddrPack(I2cBusAddr,devA2A1A0)))!= -1){
          if ((status=GT64260TWSIwrite(devA2A1A0))!=-1){
            if ((status=GT64260TWSIstart())!=-1)
                status=GT64260TWSIwrite(I2cAddrPack((I2cBusAddr|0x01),devA2A1A0));
          }
        }
      }
      break;
    case 2:
      if ((status=GT64260TWSIstart())!=-1) {
        if ((status=GT64260TWSIwrite(I2cBusAddr))!= -1) {
          if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!=-1) {
            if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){
              if ((status=GT64260TWSIstart()) != -1) {
                status = GT64260TWSIwrite((I2cBusAddr | 0x01));
              }
            }
          }
        }
      }
      break;
    case 3:
      if ((status = GT64260TWSIstart())!= -1) {
        if ((status = GT64260TWSIwrite(I2cBusAddr))!= -1) {
          if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,2)))!= -1){
            if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!= -1){
              if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){
                  if ((status=GT64260TWSIstart())!= -1) {
                  status = GT64260TWSIwrite(I2cBusAddr | 0x01);
                }
              }
            }
          }
        }
      }
      break;
    default:
      status=-1;
      break;
  }
  if (status !=-1) {
#ifdef I2C_DEBUG
     printk("\n");
#endif
     /* read data from device */
     for ( ; numBytes > 0; numBytes-- ) {
       if ( numBytes == 1) lastByte=1;
       if (GT64260TWSIread(pBuff,lastByte) == -1) return (-1);
#ifdef I2C_DEBUG
       printk("%2x ", *pBuff);
       if ( (numBytes % 20)==0 ) printk("\n");
#endif
       pBuff++;
     }
#ifdef I2C_DEBUG
     printk("\n");
#endif
     if (GT64260TWSIstop() == -1) return (-1);
  }
  return (status);
}