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