1 | /* GT64260TWSI.c : Two-Wire Serial Interface (TWSI) support for the GT64260 |
---|
2 | * |
---|
3 | * Copyright (c) 2004, Brookhaven National Laboratory and |
---|
4 | * Shuchen 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 | * See section 24:TWSI interface of "the GT-64260B System Controller |
---|
11 | * for powerPc Processors Data Sheet". |
---|
12 | * |
---|
13 | * For full TWSI protocol description look in Philips Semiconductor |
---|
14 | * TWSI spec. |
---|
15 | * |
---|
16 | * We need it to read out I2C devices used for the MVME5500 |
---|
17 | * (eg. the memory SPD and VPD). |
---|
18 | * |
---|
19 | */ |
---|
20 | #include <libcpu/spr.h> /*registers.h included here for rtems_bsp_delay()*/ |
---|
21 | #include <libcpu/io.h> |
---|
22 | #include <rtems/bspIo.h> /* printk */ |
---|
23 | |
---|
24 | #include "bsp/gtreg.h" |
---|
25 | #include "bsp/GT64260TWSI.h" |
---|
26 | |
---|
27 | #define MAX_LOOP 100 |
---|
28 | |
---|
29 | #define TWSI_DEBUG 0 |
---|
30 | |
---|
31 | int TWSI_initFlg = 0; /* TWSI Initialization Flag */ |
---|
32 | |
---|
33 | void GT64260TWSIinit(void) |
---|
34 | { |
---|
35 | |
---|
36 | if ( !TWSI_initFlg) { |
---|
37 | #if TWSI_DEBUG |
---|
38 | printk("GT64260TWSIinit("); |
---|
39 | #endif |
---|
40 | outl( 0, TWSI_SFT_RST); /* soft reset */ |
---|
41 | rtems_bsp_delay(1000); |
---|
42 | |
---|
43 | /* See 24.2.5 : Assume bus speed is 133MHZ |
---|
44 | * Try to be close to the default frequency : 62.5KHZ |
---|
45 | * value 0x2c: 69.27 KHz TWSI bus clock |
---|
46 | */ |
---|
47 | outl(0x2c, TWSI_BAUDE_RATE); |
---|
48 | rtems_bsp_delay(1000); |
---|
49 | |
---|
50 | /* Set Acknowledge and enable TWSI in the Control register */ |
---|
51 | outl(0x44, TWSI_CTRL); |
---|
52 | rtems_bsp_delay(4000); |
---|
53 | TWSI_initFlg = 1; |
---|
54 | #if TWSI_DEBUG |
---|
55 | printk(")\n"); |
---|
56 | #endif |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | /* return the interrupt flag */ |
---|
61 | int GT64260TWSIintFlag(void) |
---|
62 | { |
---|
63 | unsigned int loop; |
---|
64 | |
---|
65 | for (loop = 0; loop < MAX_LOOP; loop++ ) { |
---|
66 | /* Return 1 if the interrupt flag is set */ |
---|
67 | if (inl(TWSI_CTRL) & TWSI_INTFLG) return(1); |
---|
68 | rtems_bsp_delay(1000); |
---|
69 | } |
---|
70 | return(0); |
---|
71 | } |
---|
72 | |
---|
73 | int GT64260TWSIstop(void) |
---|
74 | { |
---|
75 | |
---|
76 | #if TWSI_DEBUG |
---|
77 | printk("GT64260TWSIstop("); |
---|
78 | #endif |
---|
79 | |
---|
80 | outl((inl(TWSI_CTRL) | TWSI_STOP), TWSI_CTRL); |
---|
81 | rtems_bsp_delay(1000); |
---|
82 | |
---|
83 | /* Check if interrupt flag bit is set*/ |
---|
84 | if (GT64260TWSIintFlag()) { |
---|
85 | outl((inl( TWSI_CTRL) & ~TWSI_INTFLG), TWSI_CTRL); |
---|
86 | rtems_bsp_delay(1000); |
---|
87 | #if TWSI_DEBUG |
---|
88 | printk(")\n"); |
---|
89 | #endif |
---|
90 | return(0); |
---|
91 | } |
---|
92 | #if TWSI_DEBUG |
---|
93 | printk("NoIntFlag\n"); |
---|
94 | #endif |
---|
95 | return(-1); |
---|
96 | } |
---|
97 | |
---|
98 | int GT64260TWSIstart(void) |
---|
99 | { |
---|
100 | unsigned int loop; |
---|
101 | unsigned int status; |
---|
102 | |
---|
103 | #if TWSI_DEBUG |
---|
104 | printk("GT64260TWSIstart("); |
---|
105 | #endif |
---|
106 | /* Initialize the TWSI interface */ |
---|
107 | GT64260TWSIinit(); |
---|
108 | |
---|
109 | /* set the start bit */ |
---|
110 | outl((TWSI_START | TWSI_TWSIEN), TWSI_CTRL); |
---|
111 | rtems_bsp_delay(1000); |
---|
112 | |
---|
113 | if (GT64260TWSIintFlag()) { |
---|
114 | /* Check for completion of START sequence */ |
---|
115 | for (loop = 0; loop<MAX_LOOP; loop++ ) { |
---|
116 | /* if (start condition transmitted) || |
---|
117 | * (repeated start condition transmitted ) |
---|
118 | */ |
---|
119 | if (((status= inl( TWSI_STATUS)) == 8) || (status == 0x10)) { |
---|
120 | #if TWSI_DEBUG |
---|
121 | printk(")"); |
---|
122 | #endif |
---|
123 | return(0); |
---|
124 | } |
---|
125 | rtems_bsp_delay(1000); |
---|
126 | } |
---|
127 | } |
---|
128 | /* if loop ends or intFlag ==0 */ |
---|
129 | GT64260TWSIstop(); |
---|
130 | return(-1); |
---|
131 | } |
---|
132 | |
---|
133 | int GT64260TWSIread(unsigned char * pData, int lastByte) |
---|
134 | { |
---|
135 | unsigned int loop; |
---|
136 | |
---|
137 | #if TWSI_DEBUG |
---|
138 | printk("GT64260TWSIread("); |
---|
139 | #endif |
---|
140 | /* Clear INTFLG and set ACK and ENABLE bits */ |
---|
141 | outl((TWSI_ACK | TWSI_TWSIEN), TWSI_CTRL); |
---|
142 | rtems_bsp_delay(1000); |
---|
143 | |
---|
144 | if (GT64260TWSIintFlag()) { |
---|
145 | for (loop = 0; loop< MAX_LOOP; loop++) { |
---|
146 | /* if Master received read data, acknowledge transmitted */ |
---|
147 | if ( (inl( TWSI_STATUS) == 0x50)) { |
---|
148 | *pData = (unsigned char) inl( TWSI_DATA); |
---|
149 | rtems_bsp_delay(1500); |
---|
150 | |
---|
151 | /* Clear INTFLAG and set Enable bit only */ |
---|
152 | if (lastByte) outl(TWSI_TWSIEN, TWSI_CTRL); |
---|
153 | rtems_bsp_delay(1500); |
---|
154 | #if TWSI_DEBUG |
---|
155 | printk(")\n"); |
---|
156 | #endif |
---|
157 | return(0); |
---|
158 | } |
---|
159 | rtems_bsp_delay(1000); |
---|
160 | } /* end for */ |
---|
161 | } |
---|
162 | /* if loop ends or intFlag ==0 */ |
---|
163 | GT64260TWSIstop(); |
---|
164 | return(-1); |
---|
165 | } |
---|
166 | |
---|
167 | /* do a TWSI write cycle on the TWSI bus*/ |
---|
168 | int GT64260TWSIwrite(unsigned char Data) |
---|
169 | { |
---|
170 | unsigned int loop; |
---|
171 | unsigned int status; |
---|
172 | |
---|
173 | #if TWSI_DEBUG |
---|
174 | printk("GT64260TWSIwrite("); |
---|
175 | #endif |
---|
176 | /* Write data into the TWSI data register */ |
---|
177 | outl(((unsigned int) Data), TWSI_DATA); |
---|
178 | rtems_bsp_delay(1000); |
---|
179 | |
---|
180 | /* Clear INTFLG in the control register to drive data onto TWSI bus */ |
---|
181 | outl(0, TWSI_CTRL); |
---|
182 | rtems_bsp_delay(1000); |
---|
183 | |
---|
184 | if (GT64260TWSIintFlag() ) { |
---|
185 | for (loop = 0; loop< MAX_LOOP; loop++) { |
---|
186 | rtems_bsp_delay(1000); |
---|
187 | /* if address + write bit transmitted, acknowledge not received */ |
---|
188 | if ( (status = inl( TWSI_STATUS)) == 0x20) { |
---|
189 | /* No device responding, generate STOP and return -1 */ |
---|
190 | printk("no device responding\n"); |
---|
191 | GT64260TWSIstop(); |
---|
192 | return(-1); |
---|
193 | } |
---|
194 | /* if (address + write bit transmitted, acknowledge received) |
---|
195 | * (Master transmmitted data byte, acknowledge received) |
---|
196 | * (address + read bit transmitted, acknowledge received) |
---|
197 | */ |
---|
198 | if ((status == 0x18)||(status == 0x28)||(status == 0x40)) { |
---|
199 | #if TWSI_DEBUG |
---|
200 | printk(")\n"); |
---|
201 | #endif |
---|
202 | return(0); |
---|
203 | } |
---|
204 | rtems_bsp_delay(1000); |
---|
205 | } /* end for */ |
---|
206 | } |
---|
207 | printk("No correct status, timeout\n"); |
---|
208 | GT64260TWSIstop(); |
---|
209 | return(-1); |
---|
210 | } |
---|