15449c685SForest Bond /* 25449c685SForest Bond * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc. 35449c685SForest Bond * All rights reserved. 45449c685SForest Bond * 55449c685SForest Bond * This program is free software; you can redistribute it and/or modify 65449c685SForest Bond * it under the terms of the GNU General Public License as published by 75449c685SForest Bond * the Free Software Foundation; either version 2 of the License, or 85449c685SForest Bond * (at your option) any later version. 95449c685SForest Bond * 105449c685SForest Bond * This program is distributed in the hope that it will be useful, 115449c685SForest Bond * but WITHOUT ANY WARRANTY; without even the implied warranty of 125449c685SForest Bond * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135449c685SForest Bond * GNU General Public License for more details. 145449c685SForest Bond * 155449c685SForest Bond * You should have received a copy of the GNU General Public License along 165449c685SForest Bond * with this program; if not, write to the Free Software Foundation, Inc., 175449c685SForest Bond * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 185449c685SForest Bond * 195449c685SForest Bond * File: srom.c 205449c685SForest Bond * 215449c685SForest Bond * Purpose:Implement functions to access eeprom 225449c685SForest Bond * 235449c685SForest Bond * Author: Jerry Chen 245449c685SForest Bond * 255449c685SForest Bond * Date: Jan 29, 2003 265449c685SForest Bond * 275449c685SForest Bond * Functions: 285449c685SForest Bond * SROMbyReadEmbedded - Embedded read eeprom via MAC 295449c685SForest Bond * SROMbWriteEmbedded - Embedded write eeprom via MAC 305449c685SForest Bond * SROMvRegBitsOn - Set Bits On in eeprom 315449c685SForest Bond * SROMvRegBitsOff - Clear Bits Off in eeprom 325449c685SForest Bond * SROMbIsRegBitsOn - Test if Bits On in eeprom 335449c685SForest Bond * SROMbIsRegBitsOff - Test if Bits Off in eeprom 345449c685SForest Bond * SROMvReadAllContents - Read all contents in eeprom 355449c685SForest Bond * SROMvWriteAllContents - Write all contents in eeprom 365449c685SForest Bond * SROMvReadEtherAddress - Read Ethernet Address in eeprom 375449c685SForest Bond * SROMvWriteEtherAddress - Write Ethernet Address in eeprom 385449c685SForest Bond * SROMvReadSubSysVenId - Read Sub_VID and Sub_SysId in eeprom 395449c685SForest Bond * SROMbAutoLoad - Auto Load eeprom to MAC register 405449c685SForest Bond * 415449c685SForest Bond * Revision History: 425449c685SForest Bond * 435449c685SForest Bond */ 445449c685SForest Bond 455449c685SForest Bond #include "upc.h" 465449c685SForest Bond #include "tmacro.h" 475449c685SForest Bond #include "tether.h" 485449c685SForest Bond #include "mac.h" 495449c685SForest Bond #include "srom.h" 505449c685SForest Bond 515449c685SForest Bond /*--------------------- Static Definitions -------------------------*/ 525449c685SForest Bond 535449c685SForest Bond /*--------------------- Static Classes ----------------------------*/ 545449c685SForest Bond 555449c685SForest Bond /*--------------------- Static Variables --------------------------*/ 565449c685SForest Bond 575449c685SForest Bond /*--------------------- Static Functions --------------------------*/ 585449c685SForest Bond 595449c685SForest Bond /*--------------------- Export Variables --------------------------*/ 605449c685SForest Bond 615449c685SForest Bond /*--------------------- Export Functions --------------------------*/ 625449c685SForest Bond 635449c685SForest Bond 645449c685SForest Bond 655449c685SForest Bond 665449c685SForest Bond /* 675449c685SForest Bond * Description: Read a byte from EEPROM, by MAC I2C 685449c685SForest Bond * 695449c685SForest Bond * Parameters: 705449c685SForest Bond * In: 715449c685SForest Bond * dwIoBase - I/O base address 725449c685SForest Bond * byContntOffset - address of EEPROM 735449c685SForest Bond * Out: 745449c685SForest Bond * none 755449c685SForest Bond * 765449c685SForest Bond * Return Value: data read 775449c685SForest Bond * 785449c685SForest Bond */ 793fc9b584SCharles Clément unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset) 805449c685SForest Bond { 812986db5fSCharles Clément unsigned short wDelay, wNoACK; 823fc9b584SCharles Clément unsigned char byWait; 833fc9b584SCharles Clément unsigned char byData; 843fc9b584SCharles Clément unsigned char byOrg; 855449c685SForest Bond 865449c685SForest Bond byData = 0xFF; 875449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg); 88ec8f002eSSteven Harms /* turn off hardware retry for getting NACK */ 895449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY))); 905449c685SForest Bond for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) { 915449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID); 925449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset); 935449c685SForest Bond 94ec8f002eSSteven Harms /* issue read command */ 955449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMR); 96ec8f002eSSteven Harms /* wait DONE be set */ 975449c685SForest Bond for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) { 985449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait); 99256a816bSJim Lieb if (byWait & (I2MCSR_DONE | I2MCSR_NACK)) 1005449c685SForest Bond break; 1015449c685SForest Bond PCAvDelayByIO(CB_DELAY_LOOP_WAIT); 1025449c685SForest Bond } 1035449c685SForest Bond if ((wDelay < W_MAX_TIMEOUT) && 104256a816bSJim Lieb ( !(byWait & I2MCSR_NACK))) { 1055449c685SForest Bond break; 1065449c685SForest Bond } 1075449c685SForest Bond } 1085449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData); 1095449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg); 1105449c685SForest Bond return byData; 1115449c685SForest Bond } 1125449c685SForest Bond 1135449c685SForest Bond 1145449c685SForest Bond /* 1155449c685SForest Bond * Description: Write a byte to EEPROM, by MAC I2C 1165449c685SForest Bond * 1175449c685SForest Bond * Parameters: 1185449c685SForest Bond * In: 1195449c685SForest Bond * dwIoBase - I/O base address 1205449c685SForest Bond * byContntOffset - address of EEPROM 1215449c685SForest Bond * wData - data to write 1225449c685SForest Bond * Out: 1235449c685SForest Bond * none 1245449c685SForest Bond * 1255449c685SForest Bond * Return Value: TRUE if succeeded; FALSE if failed. 1265449c685SForest Bond * 1275449c685SForest Bond */ 1283fc9b584SCharles Clément BOOL SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData) 1295449c685SForest Bond { 1302986db5fSCharles Clément unsigned short wDelay, wNoACK; 1313fc9b584SCharles Clément unsigned char byWait; 1325449c685SForest Bond 1333fc9b584SCharles Clément unsigned char byOrg; 1345449c685SForest Bond 1355449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg); 136ec8f002eSSteven Harms /* turn off hardware retry for getting NACK */ 1375449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY))); 1385449c685SForest Bond for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) { 1395449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID); 1405449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset); 1415449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MDOPT, byData); 1425449c685SForest Bond 143ec8f002eSSteven Harms /* issue write command */ 1445449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMW); 145ec8f002eSSteven Harms /* wait DONE be set */ 1465449c685SForest Bond for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) { 1475449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait); 148256a816bSJim Lieb if (byWait & (I2MCSR_DONE | I2MCSR_NACK)) 1495449c685SForest Bond break; 1505449c685SForest Bond PCAvDelayByIO(CB_DELAY_LOOP_WAIT); 1515449c685SForest Bond } 1525449c685SForest Bond 1535449c685SForest Bond if ((wDelay < W_MAX_TIMEOUT) && 154256a816bSJim Lieb ( !(byWait & I2MCSR_NACK))) { 1555449c685SForest Bond break; 1565449c685SForest Bond } 1575449c685SForest Bond } 1585449c685SForest Bond if (wNoACK == W_MAX_I2CRETRY) { 1595449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg); 1605449c685SForest Bond return FALSE; 1615449c685SForest Bond } 1625449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg); 1635449c685SForest Bond return TRUE; 1645449c685SForest Bond } 1655449c685SForest Bond 1665449c685SForest Bond 1675449c685SForest Bond /* 1685449c685SForest Bond * Description: Turn bits on in eeprom 1695449c685SForest Bond * 1705449c685SForest Bond * Parameters: 1715449c685SForest Bond * In: 1725449c685SForest Bond * dwIoBase - I/O base address 1735449c685SForest Bond * byContntOffset - address of EEPROM 1745449c685SForest Bond * byBits - bits to turn on 1755449c685SForest Bond * Out: 1765449c685SForest Bond * none 1775449c685SForest Bond * 1785449c685SForest Bond * Return Value: none 1795449c685SForest Bond * 1805449c685SForest Bond */ 1813fc9b584SCharles Clément void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits) 1825449c685SForest Bond { 1833fc9b584SCharles Clément unsigned char byOrgData; 1845449c685SForest Bond 1855449c685SForest Bond byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset); 1863fc9b584SCharles Clément SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData | byBits)); 1875449c685SForest Bond } 1885449c685SForest Bond 1895449c685SForest Bond 1905449c685SForest Bond /* 1915449c685SForest Bond * Description: Turn bits off in eeprom 1925449c685SForest Bond * 1935449c685SForest Bond * Parameters: 1945449c685SForest Bond * In: 1955449c685SForest Bond * dwIoBase - I/O base address 1965449c685SForest Bond * byContntOffset - address of EEPROM 1975449c685SForest Bond * byBits - bits to turn off 1985449c685SForest Bond * Out: 1995449c685SForest Bond * none 2005449c685SForest Bond * 2015449c685SForest Bond */ 2023fc9b584SCharles Clément void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits) 2035449c685SForest Bond { 2043fc9b584SCharles Clément unsigned char byOrgData; 2055449c685SForest Bond 2065449c685SForest Bond byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset); 2073fc9b584SCharles Clément SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData & (~byBits))); 2085449c685SForest Bond } 2095449c685SForest Bond 2105449c685SForest Bond 2115449c685SForest Bond /* 2125449c685SForest Bond * Description: Test if bits on in eeprom 2135449c685SForest Bond * 2145449c685SForest Bond * Parameters: 2155449c685SForest Bond * In: 2165449c685SForest Bond * dwIoBase - I/O base address 2175449c685SForest Bond * byContntOffset - address of EEPROM 2185449c685SForest Bond * byTestBits - bits to test 2195449c685SForest Bond * Out: 2205449c685SForest Bond * none 2215449c685SForest Bond * 2225449c685SForest Bond * Return Value: TRUE if all test bits on; otherwise FALSE 2235449c685SForest Bond * 2245449c685SForest Bond */ 2253fc9b584SCharles Clément BOOL SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits) 2265449c685SForest Bond { 2273fc9b584SCharles Clément unsigned char byOrgData; 2285449c685SForest Bond 2295449c685SForest Bond byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset); 230256a816bSJim Lieb return (byOrgData & byTestBits) == byTestBits; 2315449c685SForest Bond } 2325449c685SForest Bond 2335449c685SForest Bond 2345449c685SForest Bond /* 2355449c685SForest Bond * Description: Test if bits off in eeprom 2365449c685SForest Bond * 2375449c685SForest Bond * Parameters: 2385449c685SForest Bond * In: 2395449c685SForest Bond * dwIoBase - I/O base address 2405449c685SForest Bond * byContntOffset - address of EEPROM 2415449c685SForest Bond * byTestBits - bits to test 2425449c685SForest Bond * Out: 2435449c685SForest Bond * none 2445449c685SForest Bond * 2455449c685SForest Bond * Return Value: TRUE if all test bits off; otherwise FALSE 2465449c685SForest Bond * 2475449c685SForest Bond */ 2483fc9b584SCharles Clément BOOL SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits) 2495449c685SForest Bond { 2503fc9b584SCharles Clément unsigned char byOrgData; 2515449c685SForest Bond 2525449c685SForest Bond byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset); 253256a816bSJim Lieb return !(byOrgData & byTestBits); 2545449c685SForest Bond } 2555449c685SForest Bond 2565449c685SForest Bond 2575449c685SForest Bond /* 2585449c685SForest Bond * Description: Read all contents of eeprom to buffer 2595449c685SForest Bond * 2605449c685SForest Bond * Parameters: 2615449c685SForest Bond * In: 2625449c685SForest Bond * dwIoBase - I/O base address 2635449c685SForest Bond * Out: 2645449c685SForest Bond * pbyEepromRegs - EEPROM content Buffer 2655449c685SForest Bond * 2665449c685SForest Bond * Return Value: none 2675449c685SForest Bond * 2685449c685SForest Bond */ 269412b2d08SCharles Clément void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs) 2705449c685SForest Bond { 2715449c685SForest Bond int ii; 2725449c685SForest Bond 273ec8f002eSSteven Harms /* ii = Rom Address */ 2745449c685SForest Bond for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) { 2753fc9b584SCharles Clément *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase,(unsigned char) ii); 2765449c685SForest Bond pbyEepromRegs++; 2775449c685SForest Bond } 2785449c685SForest Bond } 2795449c685SForest Bond 2805449c685SForest Bond 2815449c685SForest Bond /* 2825449c685SForest Bond * Description: Write all contents of buffer to eeprom 2835449c685SForest Bond * 2845449c685SForest Bond * Parameters: 2855449c685SForest Bond * In: 2865449c685SForest Bond * dwIoBase - I/O base address 2875449c685SForest Bond * pbyEepromRegs - EEPROM content Buffer 2885449c685SForest Bond * Out: 2895449c685SForest Bond * none 2905449c685SForest Bond * 2915449c685SForest Bond * Return Value: none 2925449c685SForest Bond * 2935449c685SForest Bond */ 294412b2d08SCharles Clément void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs) 2955449c685SForest Bond { 2965449c685SForest Bond int ii; 2975449c685SForest Bond 298ec8f002eSSteven Harms /* ii = Rom Address */ 2995449c685SForest Bond for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) { 3003fc9b584SCharles Clément SROMbWriteEmbedded(dwIoBase,(unsigned char) ii, *pbyEepromRegs); 3015449c685SForest Bond pbyEepromRegs++; 3025449c685SForest Bond } 3035449c685SForest Bond } 3045449c685SForest Bond 3055449c685SForest Bond 3065449c685SForest Bond /* 3075449c685SForest Bond * Description: Read Ethernet Address from eeprom to buffer 3085449c685SForest Bond * 3095449c685SForest Bond * Parameters: 3105449c685SForest Bond * In: 3115449c685SForest Bond * dwIoBase - I/O base address 3125449c685SForest Bond * Out: 3135449c685SForest Bond * pbyEtherAddress - Ethernet Address buffer 3145449c685SForest Bond * 3155449c685SForest Bond * Return Value: none 3165449c685SForest Bond * 3175449c685SForest Bond */ 318412b2d08SCharles Clément void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress) 3195449c685SForest Bond { 3203fc9b584SCharles Clément unsigned char ii; 3215449c685SForest Bond 322ec8f002eSSteven Harms /* ii = Rom Address */ 323078b078fSCharles Clément for (ii = 0; ii < ETH_ALEN; ii++) { 3245449c685SForest Bond *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii); 3255449c685SForest Bond pbyEtherAddress++; 3265449c685SForest Bond } 3275449c685SForest Bond } 3285449c685SForest Bond 3295449c685SForest Bond 3305449c685SForest Bond /* 3315449c685SForest Bond * Description: Write Ethernet Address from buffer to eeprom 3325449c685SForest Bond * 3335449c685SForest Bond * Parameters: 3345449c685SForest Bond * In: 3355449c685SForest Bond * dwIoBase - I/O base address 3365449c685SForest Bond * pbyEtherAddress - Ethernet Address buffer 3375449c685SForest Bond * Out: 3385449c685SForest Bond * none 3395449c685SForest Bond * 3405449c685SForest Bond * Return Value: none 3415449c685SForest Bond * 3425449c685SForest Bond */ 343412b2d08SCharles Clément void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress) 3445449c685SForest Bond { 3453fc9b584SCharles Clément unsigned char ii; 3465449c685SForest Bond 347ec8f002eSSteven Harms /* ii = Rom Address */ 348078b078fSCharles Clément for (ii = 0; ii < ETH_ALEN; ii++) { 3495449c685SForest Bond SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress); 3505449c685SForest Bond pbyEtherAddress++; 3515449c685SForest Bond } 3525449c685SForest Bond } 3535449c685SForest Bond 3545449c685SForest Bond 3555449c685SForest Bond /* 3565449c685SForest Bond * Description: Read Sub_VID and Sub_SysId from eeprom to buffer 3575449c685SForest Bond * 3585449c685SForest Bond * Parameters: 3595449c685SForest Bond * In: 3605449c685SForest Bond * dwIoBase - I/O base address 3615449c685SForest Bond * Out: 3625449c685SForest Bond * pdwSubSysVenId - Sub_VID and Sub_SysId read 3635449c685SForest Bond * 3645449c685SForest Bond * Return Value: none 3655449c685SForest Bond * 3665449c685SForest Bond */ 367412b2d08SCharles Clément void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId) 3685449c685SForest Bond { 3692989e96fSCharles Clément unsigned char *pbyData; 3705449c685SForest Bond 3712989e96fSCharles Clément pbyData = (unsigned char *)pdwSubSysVenId; 372ec8f002eSSteven Harms /* sub vendor */ 3735449c685SForest Bond *pbyData = SROMbyReadEmbedded(dwIoBase, 6); 3745449c685SForest Bond *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7); 375ec8f002eSSteven Harms /* sub system */ 3765449c685SForest Bond *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8); 3775449c685SForest Bond *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9); 3785449c685SForest Bond } 3795449c685SForest Bond 3805449c685SForest Bond /* 3815449c685SForest Bond * Description: Auto Load EEPROM to MAC register 3825449c685SForest Bond * 3835449c685SForest Bond * Parameters: 3845449c685SForest Bond * In: 3855449c685SForest Bond * dwIoBase - I/O base address 3865449c685SForest Bond * Out: 3875449c685SForest Bond * none 3885449c685SForest Bond * 3895449c685SForest Bond * Return Value: TRUE if success; otherwise FALSE 3905449c685SForest Bond * 3915449c685SForest Bond */ 392412b2d08SCharles Clément BOOL SROMbAutoLoad(unsigned long dwIoBase) 3935449c685SForest Bond { 3943fc9b584SCharles Clément unsigned char byWait; 3955449c685SForest Bond int ii; 3965449c685SForest Bond 3973fc9b584SCharles Clément unsigned char byOrg; 3985449c685SForest Bond 3995449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg); 400ec8f002eSSteven Harms /* turn on hardware retry */ 4015449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY)); 4025449c685SForest Bond 4035449c685SForest Bond MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD); 4045449c685SForest Bond 405ec8f002eSSteven Harms /* ii = Rom Address */ 4065449c685SForest Bond for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) { 4075449c685SForest Bond MACvTimer0MicroSDelay(dwIoBase, CB_EEPROM_READBYTE_WAIT); 4085449c685SForest Bond VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait); 409256a816bSJim Lieb if ( !(byWait & I2MCSR_AUTOLD)) 4105449c685SForest Bond break; 4115449c685SForest Bond } 4125449c685SForest Bond 4135449c685SForest Bond VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg); 4145449c685SForest Bond 4155449c685SForest Bond if (ii == EEP_MAX_CONTEXT_SIZE) 4165449c685SForest Bond return FALSE; 4175449c685SForest Bond return TRUE; 4185449c685SForest Bond } 4195449c685SForest Bond 4205449c685SForest Bond 421