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 "bsp/GT64260TWSI.h" |
---|
16 | |
---|
17 | /* #define I2C_DEBUG*/ |
---|
18 | typedef unsigned int u32; |
---|
19 | typedef unsigned char unchar; |
---|
20 | |
---|
21 | unchar I2cAddrPack(unchar busAddr,u32 offset) |
---|
22 | { |
---|
23 | return(busAddr | ((offset & 0x700) >> 7)); |
---|
24 | } |
---|
25 | unchar I2cDevByteAddr(u32 devA2A1A0, unchar byteNum) |
---|
26 | { |
---|
27 | return(( devA2A1A0 >>(byteNum*8)) & 0xff); |
---|
28 | } |
---|
29 | /**************************************************************************** |
---|
30 | * I2Cread_eeprom - read EEPROM VPD from the I2C |
---|
31 | */ |
---|
32 | int I2Cread_eeprom(unchar I2cBusAddr,u32 devA2A1A0,u32 AddrBytes,unchar *pBuff,u32 numBytes) |
---|
33 | { |
---|
34 | int status=0, lastByte=0; |
---|
35 | |
---|
36 | switch (AddrBytes) { |
---|
37 | case 1: |
---|
38 | if ((status=GT64260TWSIstart()) != -1) { |
---|
39 | if ((status=GT64260TWSIwrite(I2cAddrPack(I2cBusAddr,devA2A1A0)))!= -1){ |
---|
40 | if ((status=GT64260TWSIwrite(devA2A1A0))!=-1){ |
---|
41 | if ((status=GT64260TWSIstart())!=-1) |
---|
42 | status=GT64260TWSIwrite(I2cAddrPack((I2cBusAddr|0x01),devA2A1A0)); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | break; |
---|
47 | case 2: |
---|
48 | if ((status=GT64260TWSIstart())!=-1) { |
---|
49 | if ((status=GT64260TWSIwrite(I2cBusAddr))!= -1) { |
---|
50 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!=-1) { |
---|
51 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){ |
---|
52 | if ((status=GT64260TWSIstart()) != -1) { |
---|
53 | status = GT64260TWSIwrite((I2cBusAddr | 0x01)); |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | break; |
---|
60 | case 3: |
---|
61 | if ((status = GT64260TWSIstart())!= -1) { |
---|
62 | if ((status = GT64260TWSIwrite(I2cBusAddr))!= -1) { |
---|
63 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,2)))!= -1){ |
---|
64 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,1)))!= -1){ |
---|
65 | if ((status=GT64260TWSIwrite(I2cDevByteAddr(devA2A1A0,0)))!= -1){ |
---|
66 | if ((status=GT64260TWSIstart())!= -1) { |
---|
67 | status = GT64260TWSIwrite(I2cBusAddr | 0x01); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | break; |
---|
75 | default: |
---|
76 | status=-1; |
---|
77 | break; |
---|
78 | } |
---|
79 | if (status !=-1) { |
---|
80 | #ifdef I2C_DEBUG |
---|
81 | printk("\n"); |
---|
82 | #endif |
---|
83 | /* read data from device */ |
---|
84 | for ( ; numBytes > 0; numBytes-- ) { |
---|
85 | if ( numBytes == 1) lastByte=1; |
---|
86 | if (GT64260TWSIread(pBuff,lastByte) == -1) return (-1); |
---|
87 | #ifdef I2C_DEBUG |
---|
88 | printk("%2x ", *pBuff); |
---|
89 | if ( (numBytes % 20)==0 ) printk("\n"); |
---|
90 | #endif |
---|
91 | pBuff++; |
---|
92 | } |
---|
93 | #ifdef I2C_DEBUG |
---|
94 | printk("\n"); |
---|
95 | #endif |
---|
96 | if (GT64260TWSIstop() == -1) return (-1); |
---|
97 | } |
---|
98 | return (status); |
---|
99 | } |
---|
100 | |
---|