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