xenbus_client.c: correct exit path for xenbus_map_ring_valloc_hvm
[linux/fpc-iii.git] / drivers / staging / vt6655 / srom.c
blobeaddc33c9d2d79d2855d6103cc634ded3dd78dc0
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
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.
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.
19 * File: srom.c
21 * Purpose:Implement functions to access eeprom
23 * Author: Jerry Chen
25 * Date: Jan 29, 2003
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
41 * Revision History:
45 #include "upc.h"
46 #include "tmacro.h"
47 #include "tether.h"
48 #include "mac.h"
49 #include "srom.h"
51 /*--------------------- Static Definitions -------------------------*/
53 /*--------------------- Static Classes ----------------------------*/
55 /*--------------------- Static Variables --------------------------*/
57 /*--------------------- Static Functions --------------------------*/
59 /*--------------------- Export Variables --------------------------*/
61 /*--------------------- Export Functions --------------------------*/
64 * Description: Read a byte from EEPROM, by MAC I2C
66 * Parameters:
67 * In:
68 * dwIoBase - I/O base address
69 * byContntOffset - address of EEPROM
70 * Out:
71 * none
73 * Return Value: data read
76 unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
78 unsigned short wDelay, wNoACK;
79 unsigned char byWait;
80 unsigned char byData;
81 unsigned char byOrg;
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);
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);
100 if ((wDelay < W_MAX_TIMEOUT) &&
101 (!(byWait & I2MCSR_NACK))) {
102 break;
105 VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
106 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
107 return byData;
111 * Description: Write a byte to EEPROM, by MAC I2C
113 * Parameters:
114 * In:
115 * dwIoBase - I/O base address
116 * byContntOffset - address of EEPROM
117 * wData - data to write
118 * Out:
119 * none
121 * Return Value: true if succeeded; false if failed.
124 bool SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData)
126 unsigned short wDelay, wNoACK;
127 unsigned char byWait;
129 unsigned char byOrg;
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);
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);
149 if ((wDelay < W_MAX_TIMEOUT) &&
150 (!(byWait & I2MCSR_NACK))) {
151 break;
154 if (wNoACK == W_MAX_I2CRETRY) {
155 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
156 return false;
158 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
159 return true;
163 * Description: Turn bits on in eeprom
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
173 * Return Value: none
176 void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
178 unsigned char byOrgData;
180 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
181 SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData | byBits));
185 * Description: Turn bits off in eeprom
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
196 void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
198 unsigned char byOrgData;
200 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
201 SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData & (~byBits)));
205 * Description: Test if bits on in eeprom
207 * Parameters:
208 * In:
209 * dwIoBase - I/O base address
210 * byContntOffset - address of EEPROM
211 * byTestBits - bits to test
212 * Out:
213 * none
215 * Return Value: true if all test bits on; otherwise false
218 bool SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
220 unsigned char byOrgData;
222 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
223 return (byOrgData & byTestBits) == byTestBits;
227 * Description: Test if bits off in eeprom
229 * Parameters:
230 * In:
231 * dwIoBase - I/O base address
232 * byContntOffset - address of EEPROM
233 * byTestBits - bits to test
234 * Out:
235 * none
237 * Return Value: true if all test bits off; otherwise false
240 bool SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
242 unsigned char byOrgData;
244 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
245 return !(byOrgData & byTestBits);
249 * Description: Read all contents of eeprom to buffer
251 * Parameters:
252 * In:
253 * dwIoBase - I/O base address
254 * Out:
255 * pbyEepromRegs - EEPROM content Buffer
257 * Return Value: none
260 void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
262 int ii;
264 /* ii = Rom Address */
265 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
266 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase, (unsigned char)ii);
267 pbyEepromRegs++;
272 * Description: Write all contents of buffer to eeprom
274 * Parameters:
275 * In:
276 * dwIoBase - I/O base address
277 * pbyEepromRegs - EEPROM content Buffer
278 * Out:
279 * none
281 * Return Value: none
284 void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
286 int ii;
288 /* ii = Rom Address */
289 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
290 SROMbWriteEmbedded(dwIoBase, (unsigned char)ii, *pbyEepromRegs);
291 pbyEepromRegs++;
296 * Description: Read Ethernet Address from eeprom to buffer
298 * Parameters:
299 * In:
300 * dwIoBase - I/O base address
301 * Out:
302 * pbyEtherAddress - Ethernet Address buffer
304 * Return Value: none
307 void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
309 unsigned char ii;
311 /* ii = Rom Address */
312 for (ii = 0; ii < ETH_ALEN; ii++) {
313 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
314 pbyEtherAddress++;
319 * Description: Write Ethernet Address from buffer to eeprom
321 * Parameters:
322 * In:
323 * dwIoBase - I/O base address
324 * pbyEtherAddress - Ethernet Address buffer
325 * Out:
326 * none
328 * Return Value: none
331 void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
333 unsigned char ii;
335 /* ii = Rom Address */
336 for (ii = 0; ii < ETH_ALEN; ii++) {
337 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
338 pbyEtherAddress++;
343 * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
345 * Parameters:
346 * In:
347 * dwIoBase - I/O base address
348 * Out:
349 * pdwSubSysVenId - Sub_VID and Sub_SysId read
351 * Return Value: none
354 void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
356 unsigned char *pbyData;
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);
368 * Description: Auto Load EEPROM to MAC register
370 * Parameters:
371 * In:
372 * dwIoBase - I/O base address
373 * Out:
374 * none
376 * Return Value: true if success; otherwise false
379 bool SROMbAutoLoad(unsigned long dwIoBase)
381 unsigned char byWait;
382 int ii;
384 unsigned char byOrg;
386 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
387 /* turn on hardware retry */
388 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
390 MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
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;
400 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
402 if (ii == EEP_MAX_CONTEXT_SIZE)
403 return false;
404 return true;