1 | /* MVME5500I2C.c |
---|
2 | * |
---|
3 | * Copyright (c) 2003, 2004 Brookhaven National Laboratory |
---|
4 | * Author: S. Kate Feng <feng1@bnl.gov> |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution. |
---|
9 | * |
---|
10 | * To read inoformation of the EEPROM via the I2C |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | #include <rtems/bspIo.h> /* printk */ |
---|
15 | #include <rtems/stdint.h> |
---|
16 | #include "bsp/GT64260TWSI.h" |
---|
17 | |
---|
18 | /* #define I2C_DEBUG*/ |
---|
19 | |
---|
20 | unsigned char I2cAddrPack(unsigned char busAddr,uint32_t offset) |
---|
21 | { |
---|
22 | return(busAddr | ((offset & 0x700) >> 7)); |
---|
23 | } |
---|
24 | unsigned char I2cDevByteAddr(uint32_t devA2A1A0, unsigned char byteNum) |
---|
25 | { |
---|
26 | return(( devA2A1A0 >>(byteNum*8)) & 0xff); |
---|
27 | } |
---|
28 | /**************************************************************************** |
---|
29 | * I2Cread_eeprom - read EEPROM VPD from the I2C |
---|
30 | */ |
---|
31 | int I2Cread_eeprom(unsigned char I2cBusAddr,uint32_t devA2A1A0,uint32_t AddrBytes,unsigned char *pBuff,uint32_t numBytes) |
---|
32 | { |
---|
33 | int status=0, lastByte=0; |
---|
34 | |
---|
35 | switch (AddrBytes) { |
---|
36 | case 1: |
---|
37 | if ((status=GT64260TWSIstart()) != -1) { |
---|
38 | if ((status=GT64260TWSIwrite(I2cAddrPack(I2cBusAddr,devA2A1A0)))!= -1){ |
---|
39 | if ((status=GT64260TWSIwrite(devA2A1A0))!=-1){ |
---|
40 | if ((status=GT64260TWSIstart())!=-1) |
---|
41 | status=GT64260TWSIwrite(I2cAddrPack((I2cBusAddr|0x01),devA2A1A0)); |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | break; |
---|
46 | case 2: |
---|
47 | if ((status=GT64260TWSIstart())!=-1) { |
---|
48 | if ((status=GT64260TWSIwrite(I2cBusAddr))!= -1) { |
---|
49 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!=-1) { |
---|
50 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){ |
---|
51 | if ((status=GT64260TWSIstart()) != -1) { |
---|
52 | status = GT64260TWSIwrite((I2cBusAddr | 0x01)); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | break; |
---|
59 | case 3: |
---|
60 | if ((status = GT64260TWSIstart())!= -1) { |
---|
61 | if ((status = GT64260TWSIwrite(I2cBusAddr))!= -1) { |
---|
62 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,2)))!= -1){ |
---|
63 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!= -1){ |
---|
64 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){ |
---|
65 | if ((status=GT64260TWSIstart())!= -1) { |
---|
66 | status = GT64260TWSIwrite(I2cBusAddr | 0x01); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | break; |
---|
74 | default: |
---|
75 | status=-1; |
---|
76 | break; |
---|
77 | } |
---|
78 | if (status !=-1) { |
---|
79 | #ifdef I2C_DEBUG |
---|
80 | printk("\n"); |
---|
81 | #endif |
---|
82 | /* read data from device */ |
---|
83 | for ( ; numBytes > 0; numBytes-- ) { |
---|
84 | if ( numBytes == 1) lastByte=1; |
---|
85 | if (GT64260TWSIread(pBuff,lastByte) == -1) return (-1); |
---|
86 | #ifdef I2C_DEBUG |
---|
87 | printk("%2x ", *pBuff); |
---|
88 | if ( (numBytes % 20)==0 ) printk("\n"); |
---|
89 | #endif |
---|
90 | pBuff++; |
---|
91 | } |
---|
92 | #ifdef I2C_DEBUG |
---|
93 | printk("\n"); |
---|
94 | #endif |
---|
95 | if (GT64260TWSIstop() == -1) return (-1); |
---|
96 | } |
---|
97 | return (status); |
---|
98 | } |
---|
99 | |
---|