2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
21 * Purpose:Implement functions to access eeprom
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
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
68 * dwIoBase - I/O base address
69 * byContntOffset - address of EEPROM
73 * Return Value: data read
76 unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
78 unsigned short wDelay, wNoACK;
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))
98 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
100 if ((wDelay < W_MAX_TIMEOUT) &&
101 (!(byWait & I2MCSR_NACK))) {
105 VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
106 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
111 * Description: Write a byte to EEPROM, by MAC I2C
115 * dwIoBase - I/O base address
116 * byContntOffset - address of EEPROM
117 * wData - data to write
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;
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))
146 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
149 if ((wDelay < W_MAX_TIMEOUT) &&
150 (!(byWait & I2MCSR_NACK))) {
154 if (wNoACK == W_MAX_I2CRETRY) {
155 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
158 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
163 * Description: Turn bits on in eeprom
167 * dwIoBase - I/O base address
168 * byContntOffset - address of EEPROM
169 * byBits - bits to turn on
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
189 * dwIoBase - I/O base address
190 * byContntOffset - address of EEPROM
191 * byBits - bits to turn off
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
209 * dwIoBase - I/O base address
210 * byContntOffset - address of EEPROM
211 * byTestBits - bits to test
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
231 * dwIoBase - I/O base address
232 * byContntOffset - address of EEPROM
233 * byTestBits - bits to test
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
253 * dwIoBase - I/O base address
255 * pbyEepromRegs - EEPROM content Buffer
260 void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
264 /* ii = Rom Address */
265 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
266 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase, (unsigned char)ii);
272 * Description: Write all contents of buffer to eeprom
276 * dwIoBase - I/O base address
277 * pbyEepromRegs - EEPROM content Buffer
284 void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
288 /* ii = Rom Address */
289 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
290 SROMbWriteEmbedded(dwIoBase, (unsigned char)ii, *pbyEepromRegs);
296 * Description: Read Ethernet Address from eeprom to buffer
300 * dwIoBase - I/O base address
302 * pbyEtherAddress - Ethernet Address buffer
307 void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
311 /* ii = Rom Address */
312 for (ii = 0; ii < ETH_ALEN; ii++) {
313 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
319 * Description: Write Ethernet Address from buffer to eeprom
323 * dwIoBase - I/O base address
324 * pbyEtherAddress - Ethernet Address buffer
331 void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
335 /* ii = Rom Address */
336 for (ii = 0; ii < ETH_ALEN; ii++) {
337 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
343 * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
347 * dwIoBase - I/O base address
349 * pdwSubSysVenId - Sub_VID and Sub_SysId read
354 void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
356 unsigned char *pbyData;
358 pbyData = (unsigned char *)pdwSubSysVenId;
360 *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
361 *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
363 *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
364 *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
368 * Description: Auto Load EEPROM to MAC register
372 * dwIoBase - I/O base address
376 * Return Value: true if success; otherwise false
379 bool SROMbAutoLoad(unsigned long dwIoBase)
381 unsigned char byWait;
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))
400 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
402 if (ii == EEP_MAX_CONTEXT_SIZE)