Merge branch 'stable' of git://git.kernel.org/pub/scm/linux/kernel/git/cmetcalf/linux...
[cascardo/linux.git] / drivers / staging / vt6655 / srom.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: srom.c
20  *
21  * Purpose:Implement functions to access eeprom
22  *
23  * Author: Jerry Chen
24  *
25  * Date: Jan 29, 2003
26  *
27  * Functions:
28  *      SROMbyReadEmbedded - Embedded read eeprom via MAC
29  *      SROMbWriteEmbedded - Embedded write eeprom via MAC
30  *      SROMvRegBitsOn - Set Bits On in eeprom
31  *      SROMvRegBitsOff - Clear Bits Off in eeprom
32  *      SROMbIsRegBitsOn - Test if Bits On in eeprom
33  *      SROMbIsRegBitsOff - Test if Bits Off in eeprom
34  *      SROMvReadAllContents - Read all contents in eeprom
35  *      SROMvWriteAllContents - Write all contents in eeprom
36  *      SROMvReadEtherAddress - Read Ethernet Address in eeprom
37  *      SROMvWriteEtherAddress - Write Ethernet Address in eeprom
38  *      SROMvReadSubSysVenId - Read Sub_VID and Sub_SysId in eeprom
39  *      SROMbAutoLoad - Auto Load eeprom to MAC register
40  *
41  * Revision History:
42  *
43  */
44
45 #include "upc.h"
46 #include "tmacro.h"
47 #include "tether.h"
48 #include "mac.h"
49 #include "srom.h"
50
51 /*---------------------  Static Definitions -------------------------*/
52
53 /*---------------------  Static Classes  ----------------------------*/
54
55 /*---------------------  Static Variables  --------------------------*/
56
57 /*---------------------  Static Functions  --------------------------*/
58
59 /*---------------------  Export Variables  --------------------------*/
60
61 /*---------------------  Export Functions  --------------------------*/
62
63 /*
64  * Description: Read a byte from EEPROM, by MAC I2C
65  *
66  * Parameters:
67  *  In:
68  *      dwIoBase        - I/O base address
69  *      byContntOffset  - address of EEPROM
70  *  Out:
71  *      none
72  *
73  * Return Value: data read
74  *
75  */
76 unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
77 {
78         unsigned short wDelay, wNoACK;
79         unsigned char byWait;
80         unsigned char byData;
81         unsigned char byOrg;
82
83         byData = 0xFF;
84         VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
85         /* turn off hardware retry for getting NACK */
86         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
87         for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
88                 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
89                 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
90
91                 /* issue read command */
92                 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMR);
93                 /* wait DONE be set */
94                 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
95                         VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
96                         if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
97                                 break;
98                         PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
99                 }
100                 if ((wDelay < W_MAX_TIMEOUT) &&
101                     (!(byWait & I2MCSR_NACK))) {
102                         break;
103                 }
104         }
105         VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
106         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
107         return byData;
108 }
109
110 /*
111  * Description: Write a byte to EEPROM, by MAC I2C
112  *
113  * Parameters:
114  *  In:
115  *      dwIoBase        - I/O base address
116  *      byContntOffset  - address of EEPROM
117  *      wData           - data to write
118  *  Out:
119  *      none
120  *
121  * Return Value: true if succeeded; false if failed.
122  *
123  */
124 bool SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData)
125 {
126         unsigned short wDelay, wNoACK;
127         unsigned char byWait;
128
129         unsigned char byOrg;
130
131         VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
132         /* turn off hardware retry for getting NACK */
133         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
134         for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
135                 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
136                 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
137                 VNSvOutPortB(dwIoBase + MAC_REG_I2MDOPT, byData);
138
139                 /* issue write command */
140                 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMW);
141                 /* wait DONE be set */
142                 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
143                         VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
144                         if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
145                                 break;
146                         PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
147                 }
148
149                 if ((wDelay < W_MAX_TIMEOUT) &&
150                     (!(byWait & I2MCSR_NACK))) {
151                         break;
152                 }
153         }
154         if (wNoACK == W_MAX_I2CRETRY) {
155                 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
156                 return false;
157         }
158         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
159         return true;
160 }
161
162 /*
163  * Description: Turn bits on in eeprom
164  *
165  * Parameters:
166  *  In:
167  *      dwIoBase        - I/O base address
168  *      byContntOffset  - address of EEPROM
169  *      byBits          - bits to turn on
170  *  Out:
171  *      none
172  *
173  * Return Value: none
174  *
175  */
176 void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
177 {
178         unsigned char byOrgData;
179
180         byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
181         SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData | byBits));
182 }
183
184 /*
185  * Description: Turn bits off in eeprom
186  *
187  * Parameters:
188  *  In:
189  *      dwIoBase        - I/O base address
190  *      byContntOffset  - address of EEPROM
191  *      byBits          - bits to turn off
192  *  Out:
193  *      none
194  *
195  */
196 void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
197 {
198         unsigned char byOrgData;
199
200         byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
201         SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData & (~byBits)));
202 }
203
204 /*
205  * Description: Test if bits on in eeprom
206  *
207  * Parameters:
208  *  In:
209  *      dwIoBase        - I/O base address
210  *      byContntOffset  - address of EEPROM
211  *      byTestBits      - bits to test
212  *  Out:
213  *      none
214  *
215  * Return Value: true if all test bits on; otherwise false
216  *
217  */
218 bool SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
219 {
220         unsigned char byOrgData;
221
222         byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
223         return (byOrgData & byTestBits) == byTestBits;
224 }
225
226 /*
227  * Description: Test if bits off in eeprom
228  *
229  * Parameters:
230  *  In:
231  *      dwIoBase        - I/O base address
232  *      byContntOffset  - address of EEPROM
233  *      byTestBits      - bits to test
234  *  Out:
235  *      none
236  *
237  * Return Value: true if all test bits off; otherwise false
238  *
239  */
240 bool SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
241 {
242         unsigned char byOrgData;
243
244         byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
245         return !(byOrgData & byTestBits);
246 }
247
248 /*
249  * Description: Read all contents of eeprom to buffer
250  *
251  * Parameters:
252  *  In:
253  *      dwIoBase        - I/O base address
254  *  Out:
255  *      pbyEepromRegs   - EEPROM content Buffer
256  *
257  * Return Value: none
258  *
259  */
260 void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
261 {
262         int     ii;
263
264         /* ii = Rom Address */
265         for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
266                 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase, (unsigned char)ii);
267                 pbyEepromRegs++;
268         }
269 }
270
271 /*
272  * Description: Write all contents of buffer to eeprom
273  *
274  * Parameters:
275  *  In:
276  *      dwIoBase        - I/O base address
277  *      pbyEepromRegs   - EEPROM content Buffer
278  *  Out:
279  *      none
280  *
281  * Return Value: none
282  *
283  */
284 void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
285 {
286         int     ii;
287
288         /* ii = Rom Address */
289         for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
290                 SROMbWriteEmbedded(dwIoBase, (unsigned char)ii, *pbyEepromRegs);
291                 pbyEepromRegs++;
292         }
293 }
294
295 /*
296  * Description: Read Ethernet Address from eeprom to buffer
297  *
298  * Parameters:
299  *  In:
300  *      dwIoBase        - I/O base address
301  *  Out:
302  *      pbyEtherAddress - Ethernet Address buffer
303  *
304  * Return Value: none
305  *
306  */
307 void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
308 {
309         unsigned char ii;
310
311         /* ii = Rom Address */
312         for (ii = 0; ii < ETH_ALEN; ii++) {
313                 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
314                 pbyEtherAddress++;
315         }
316 }
317
318 /*
319  * Description: Write Ethernet Address from buffer to eeprom
320  *
321  * Parameters:
322  *  In:
323  *      dwIoBase        - I/O base address
324  *      pbyEtherAddress - Ethernet Address buffer
325  *  Out:
326  *      none
327  *
328  * Return Value: none
329  *
330  */
331 void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
332 {
333         unsigned char ii;
334
335         /* ii = Rom Address */
336         for (ii = 0; ii < ETH_ALEN; ii++) {
337                 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
338                 pbyEtherAddress++;
339         }
340 }
341
342 /*
343  * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
344  *
345  * Parameters:
346  *  In:
347  *      dwIoBase        - I/O base address
348  *  Out:
349  *      pdwSubSysVenId  - Sub_VID and Sub_SysId read
350  *
351  * Return Value: none
352  *
353  */
354 void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
355 {
356         unsigned char *pbyData;
357
358         pbyData = (unsigned char *)pdwSubSysVenId;
359         /* sub vendor */
360         *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
361         *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
362         /* sub system */
363         *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
364         *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
365 }
366
367 /*
368  * Description: Auto Load EEPROM to MAC register
369  *
370  * Parameters:
371  *  In:
372  *      dwIoBase        - I/O base address
373  *  Out:
374  *      none
375  *
376  * Return Value: true if success; otherwise false
377  *
378  */
379 bool SROMbAutoLoad(unsigned long dwIoBase)
380 {
381         unsigned char byWait;
382         int     ii;
383
384         unsigned char byOrg;
385
386         VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
387         /* turn on hardware retry */
388         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
389
390         MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
391
392         /* ii = Rom Address */
393         for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
394                 MACvTimer0MicroSDelay(dwIoBase, CB_EEPROM_READBYTE_WAIT);
395                 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
396                 if (!(byWait & I2MCSR_AUTOLD))
397                         break;
398         }
399
400         VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
401
402         if (ii == EEP_MAX_CONTEXT_SIZE)
403                 return false;
404         return true;
405 }