Staging: Add pristine upstream vt6656 driver sources to drivers/staging/vt6656.
[firewire-audio.git] / drivers / staging / vt6655 / srom.c
blob655d6859370159ee37bd15287bffc4730b40f700
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:
46 #if !defined(__UPC_H__)
47 #include "upc.h"
48 #endif
49 #if !defined(__TMACRO_H__)
50 #include "tmacro.h"
51 #endif
52 #if !defined(__TBIT_H__)
53 #include "tbit.h"
54 #endif
55 #if !defined(__TETHER_H__)
56 #include "tether.h"
57 #endif
58 #if !defined(__MAC_H__)
59 #include "mac.h"
60 #endif
61 #if !defined(__SROM_H__)
62 #include "srom.h"
63 #endif
68 /*--------------------- Static Definitions -------------------------*/
70 /*--------------------- Static Classes ----------------------------*/
72 /*--------------------- Static Variables --------------------------*/
74 /*--------------------- Static Functions --------------------------*/
76 /*--------------------- Export Variables --------------------------*/
78 /*--------------------- Export Functions --------------------------*/
84 * Description: Read a byte from EEPROM, by MAC I2C
86 * Parameters:
87 * In:
88 * dwIoBase - I/O base address
89 * byContntOffset - address of EEPROM
90 * Out:
91 * none
93 * Return Value: data read
96 BYTE SROMbyReadEmbedded(DWORD_PTR dwIoBase, BYTE byContntOffset)
98 WORD wDelay, wNoACK;
99 BYTE byWait;
100 BYTE byData;
101 BYTE byOrg;
103 byData = 0xFF;
104 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
105 // turn off hardware retry for getting NACK
106 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
107 for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
108 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
109 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
111 // issue read command
112 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMR);
113 // wait DONE be set
114 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
115 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
116 if (BITbIsAnyBitsOn(byWait, (I2MCSR_DONE | I2MCSR_NACK)))
117 break;
118 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
120 if ((wDelay < W_MAX_TIMEOUT) &&
121 (BITbIsBitOff(byWait, I2MCSR_NACK))) {
122 break;
125 VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
126 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
127 return byData;
132 * Description: Write a byte to EEPROM, by MAC I2C
134 * Parameters:
135 * In:
136 * dwIoBase - I/O base address
137 * byContntOffset - address of EEPROM
138 * wData - data to write
139 * Out:
140 * none
142 * Return Value: TRUE if succeeded; FALSE if failed.
145 BOOL SROMbWriteEmbedded (DWORD_PTR dwIoBase, BYTE byContntOffset, BYTE byData)
147 WORD wDelay, wNoACK;
148 BYTE byWait;
150 BYTE byOrg;
152 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
153 // turn off hardware retry for getting NACK
154 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
155 for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
156 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
157 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
158 VNSvOutPortB(dwIoBase + MAC_REG_I2MDOPT, byData);
160 // issue write command
161 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMW);
162 // wait DONE be set
163 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
164 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
165 if (BITbIsAnyBitsOn(byWait, (I2MCSR_DONE | I2MCSR_NACK)))
166 break;
167 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
170 if ((wDelay < W_MAX_TIMEOUT) &&
171 (BITbIsBitOff(byWait, I2MCSR_NACK))) {
172 break;
175 if (wNoACK == W_MAX_I2CRETRY) {
176 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
177 return FALSE;
179 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
180 return TRUE;
185 * Description: Turn bits on in eeprom
187 * Parameters:
188 * In:
189 * dwIoBase - I/O base address
190 * byContntOffset - address of EEPROM
191 * byBits - bits to turn on
192 * Out:
193 * none
195 * Return Value: none
198 void SROMvRegBitsOn (DWORD_PTR dwIoBase, BYTE byContntOffset, BYTE byBits)
200 BYTE byOrgData;
202 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
203 SROMbWriteEmbedded(dwIoBase, byContntOffset,(BYTE)(byOrgData | byBits));
208 * Description: Turn bits off in eeprom
210 * Parameters:
211 * In:
212 * dwIoBase - I/O base address
213 * byContntOffset - address of EEPROM
214 * byBits - bits to turn off
215 * Out:
216 * none
219 void SROMvRegBitsOff (DWORD_PTR dwIoBase, BYTE byContntOffset, BYTE byBits)
221 BYTE byOrgData;
223 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
224 SROMbWriteEmbedded(dwIoBase, byContntOffset,(BYTE)(byOrgData & (~byBits)));
229 * Description: Test if bits on in eeprom
231 * Parameters:
232 * In:
233 * dwIoBase - I/O base address
234 * byContntOffset - address of EEPROM
235 * byTestBits - bits to test
236 * Out:
237 * none
239 * Return Value: TRUE if all test bits on; otherwise FALSE
242 BOOL SROMbIsRegBitsOn (DWORD_PTR dwIoBase, BYTE byContntOffset, BYTE byTestBits)
244 BYTE byOrgData;
246 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
247 return BITbIsAllBitsOn(byOrgData, byTestBits);
252 * Description: Test if bits off in eeprom
254 * Parameters:
255 * In:
256 * dwIoBase - I/O base address
257 * byContntOffset - address of EEPROM
258 * byTestBits - bits to test
259 * Out:
260 * none
262 * Return Value: TRUE if all test bits off; otherwise FALSE
265 BOOL SROMbIsRegBitsOff (DWORD_PTR dwIoBase, BYTE byContntOffset, BYTE byTestBits)
267 BYTE byOrgData;
269 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
270 return BITbIsAllBitsOff(byOrgData, byTestBits);
275 * Description: Read all contents of eeprom to buffer
277 * Parameters:
278 * In:
279 * dwIoBase - I/O base address
280 * Out:
281 * pbyEepromRegs - EEPROM content Buffer
283 * Return Value: none
286 void SROMvReadAllContents (DWORD_PTR dwIoBase, PBYTE pbyEepromRegs)
288 int ii;
290 // ii = Rom Address
291 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
292 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase,(BYTE) ii);
293 pbyEepromRegs++;
299 * Description: Write all contents of buffer to eeprom
301 * Parameters:
302 * In:
303 * dwIoBase - I/O base address
304 * pbyEepromRegs - EEPROM content Buffer
305 * Out:
306 * none
308 * Return Value: none
311 void SROMvWriteAllContents (DWORD_PTR dwIoBase, PBYTE pbyEepromRegs)
313 int ii;
315 // ii = Rom Address
316 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
317 SROMbWriteEmbedded(dwIoBase,(BYTE) ii, *pbyEepromRegs);
318 pbyEepromRegs++;
324 * Description: Read Ethernet Address from eeprom to buffer
326 * Parameters:
327 * In:
328 * dwIoBase - I/O base address
329 * Out:
330 * pbyEtherAddress - Ethernet Address buffer
332 * Return Value: none
335 void SROMvReadEtherAddress (DWORD_PTR dwIoBase, PBYTE pbyEtherAddress)
337 BYTE ii;
339 // ii = Rom Address
340 for (ii = 0; ii < U_ETHER_ADDR_LEN; ii++) {
341 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
342 pbyEtherAddress++;
348 * Description: Write Ethernet Address from buffer to eeprom
350 * Parameters:
351 * In:
352 * dwIoBase - I/O base address
353 * pbyEtherAddress - Ethernet Address buffer
354 * Out:
355 * none
357 * Return Value: none
360 void SROMvWriteEtherAddress (DWORD_PTR dwIoBase, PBYTE pbyEtherAddress)
362 BYTE ii;
364 // ii = Rom Address
365 for (ii = 0; ii < U_ETHER_ADDR_LEN; ii++) {
366 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
367 pbyEtherAddress++;
373 * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
375 * Parameters:
376 * In:
377 * dwIoBase - I/O base address
378 * Out:
379 * pdwSubSysVenId - Sub_VID and Sub_SysId read
381 * Return Value: none
384 void SROMvReadSubSysVenId (DWORD_PTR dwIoBase, PDWORD pdwSubSysVenId)
386 PBYTE pbyData;
388 pbyData = (PBYTE)pdwSubSysVenId;
389 // sub vendor
390 *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
391 *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
392 // sub system
393 *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
394 *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
398 * Description: Auto Load EEPROM to MAC register
400 * Parameters:
401 * In:
402 * dwIoBase - I/O base address
403 * Out:
404 * none
406 * Return Value: TRUE if success; otherwise FALSE
409 BOOL SROMbAutoLoad (DWORD_PTR dwIoBase)
411 BYTE byWait;
412 int ii;
414 BYTE byOrg;
416 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
417 // turn on hardware retry
418 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
420 MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
422 // ii = Rom Address
423 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
424 MACvTimer0MicroSDelay(dwIoBase, CB_EEPROM_READBYTE_WAIT);
425 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
426 if (BITbIsBitOff(byWait, I2MCSR_AUTOLD))
427 break;
430 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
432 if (ii == EEP_MAX_CONTEXT_SIZE)
433 return FALSE;
434 return TRUE;