xref: /openbmc/linux/drivers/staging/vt6655/srom.c (revision d7c43082)
1d7c43082SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
25449c685SForest Bond /*
35449c685SForest Bond  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
45449c685SForest Bond  * All rights reserved.
55449c685SForest Bond  *
65449c685SForest Bond  * This program is free software; you can redistribute it and/or modify
75449c685SForest Bond  * it under the terms of the GNU General Public License as published by
85449c685SForest Bond  * the Free Software Foundation; either version 2 of the License, or
95449c685SForest Bond  * (at your option) any later version.
105449c685SForest Bond  *
115449c685SForest Bond  * This program is distributed in the hope that it will be useful,
125449c685SForest Bond  * but WITHOUT ANY WARRANTY; without even the implied warranty of
135449c685SForest Bond  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145449c685SForest Bond  * GNU General Public License for more details.
155449c685SForest Bond  *
165449c685SForest Bond  * File: srom.c
175449c685SForest Bond  *
185449c685SForest Bond  * Purpose:Implement functions to access eeprom
195449c685SForest Bond  *
205449c685SForest Bond  * Author: Jerry Chen
215449c685SForest Bond  *
225449c685SForest Bond  * Date: Jan 29, 2003
235449c685SForest Bond  *
245449c685SForest Bond  * Functions:
255449c685SForest Bond  *      SROMbyReadEmbedded - Embedded read eeprom via MAC
265449c685SForest Bond  *      SROMbWriteEmbedded - Embedded write eeprom via MAC
275449c685SForest Bond  *      SROMvRegBitsOn - Set Bits On in eeprom
285449c685SForest Bond  *      SROMvRegBitsOff - Clear Bits Off in eeprom
295449c685SForest Bond  *      SROMbIsRegBitsOn - Test if Bits On in eeprom
305449c685SForest Bond  *      SROMbIsRegBitsOff - Test if Bits Off in eeprom
315449c685SForest Bond  *      SROMvReadAllContents - Read all contents in eeprom
325449c685SForest Bond  *      SROMvWriteAllContents - Write all contents in eeprom
335449c685SForest Bond  *      SROMvReadEtherAddress - Read Ethernet Address in eeprom
345449c685SForest Bond  *      SROMvWriteEtherAddress - Write Ethernet Address in eeprom
355449c685SForest Bond  *      SROMvReadSubSysVenId - Read Sub_VID and Sub_SysId in eeprom
365449c685SForest Bond  *      SROMbAutoLoad - Auto Load eeprom to MAC register
375449c685SForest Bond  *
385449c685SForest Bond  * Revision History:
395449c685SForest Bond  *
405449c685SForest Bond  */
415449c685SForest Bond 
425449c685SForest Bond #include "upc.h"
435449c685SForest Bond #include "tmacro.h"
445449c685SForest Bond #include "mac.h"
455449c685SForest Bond #include "srom.h"
465449c685SForest Bond 
475449c685SForest Bond /*---------------------  Static Definitions -------------------------*/
485449c685SForest Bond 
495449c685SForest Bond /*---------------------  Static Classes  ----------------------------*/
505449c685SForest Bond 
515449c685SForest Bond /*---------------------  Static Variables  --------------------------*/
525449c685SForest Bond 
535449c685SForest Bond /*---------------------  Static Functions  --------------------------*/
545449c685SForest Bond 
555449c685SForest Bond /*---------------------  Export Variables  --------------------------*/
565449c685SForest Bond 
575449c685SForest Bond /*---------------------  Export Functions  --------------------------*/
585449c685SForest Bond 
595449c685SForest Bond /*
605449c685SForest Bond  * Description: Read a byte from EEPROM, by MAC I2C
615449c685SForest Bond  *
625449c685SForest Bond  * Parameters:
635449c685SForest Bond  *  In:
6426f64a6bSVarsha Rao  *      iobase          - I/O base address
655449c685SForest Bond  *      byContntOffset  - address of EEPROM
665449c685SForest Bond  *  Out:
675449c685SForest Bond  *      none
685449c685SForest Bond  *
695449c685SForest Bond  * Return Value: data read
705449c685SForest Bond  *
715449c685SForest Bond  */
7226f64a6bSVarsha Rao unsigned char SROMbyReadEmbedded(void __iomem *iobase,
735150d01eSKathryn Hampton 				 unsigned char byContntOffset)
745449c685SForest Bond {
752986db5fSCharles Clément 	unsigned short wDelay, wNoACK;
763fc9b584SCharles Clément 	unsigned char byWait;
773fc9b584SCharles Clément 	unsigned char byData;
783fc9b584SCharles Clément 	unsigned char byOrg;
795449c685SForest Bond 
805449c685SForest Bond 	byData = 0xFF;
8126f64a6bSVarsha Rao 	VNSvInPortB(iobase + MAC_REG_I2MCFG, &byOrg);
82ec8f002eSSteven Harms 	/* turn off hardware retry for getting NACK */
8326f64a6bSVarsha Rao 	VNSvOutPortB(iobase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
845449c685SForest Bond 	for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
8526f64a6bSVarsha Rao 		VNSvOutPortB(iobase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
8626f64a6bSVarsha Rao 		VNSvOutPortB(iobase + MAC_REG_I2MTGAD, byContntOffset);
875449c685SForest Bond 
88ec8f002eSSteven Harms 		/* issue read command */
8926f64a6bSVarsha Rao 		VNSvOutPortB(iobase + MAC_REG_I2MCSR, I2MCSR_EEMR);
90ec8f002eSSteven Harms 		/* wait DONE be set */
915449c685SForest Bond 		for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
9226f64a6bSVarsha Rao 			VNSvInPortB(iobase + MAC_REG_I2MCSR, &byWait);
93256a816bSJim Lieb 			if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
945449c685SForest Bond 				break;
955449c685SForest Bond 			PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
965449c685SForest Bond 		}
975449c685SForest Bond 		if ((wDelay < W_MAX_TIMEOUT) &&
98256a816bSJim Lieb 		    (!(byWait & I2MCSR_NACK))) {
995449c685SForest Bond 			break;
1005449c685SForest Bond 		}
1015449c685SForest Bond 	}
10226f64a6bSVarsha Rao 	VNSvInPortB(iobase + MAC_REG_I2MDIPT, &byData);
10326f64a6bSVarsha Rao 	VNSvOutPortB(iobase + MAC_REG_I2MCFG, byOrg);
1045449c685SForest Bond 	return byData;
1055449c685SForest Bond }
1065449c685SForest Bond 
1075449c685SForest Bond /*
1085449c685SForest Bond  * Description: Read all contents of eeprom to buffer
1095449c685SForest Bond  *
1105449c685SForest Bond  * Parameters:
1115449c685SForest Bond  *  In:
11226f64a6bSVarsha Rao  *      iobase          - I/O base address
1135449c685SForest Bond  *  Out:
1145449c685SForest Bond  *      pbyEepromRegs   - EEPROM content Buffer
1155449c685SForest Bond  *
1165449c685SForest Bond  * Return Value: none
1175449c685SForest Bond  *
1185449c685SForest Bond  */
11926f64a6bSVarsha Rao void SROMvReadAllContents(void __iomem *iobase, unsigned char *pbyEepromRegs)
1205449c685SForest Bond {
1215449c685SForest Bond 	int     ii;
1225449c685SForest Bond 
123ec8f002eSSteven Harms 	/* ii = Rom Address */
1245449c685SForest Bond 	for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
12526f64a6bSVarsha Rao 		*pbyEepromRegs = SROMbyReadEmbedded(iobase,
1265150d01eSKathryn Hampton 						    (unsigned char)ii);
1275449c685SForest Bond 		pbyEepromRegs++;
1285449c685SForest Bond 	}
1295449c685SForest Bond }
1305449c685SForest Bond 
1315449c685SForest Bond /*
1325449c685SForest Bond  * Description: Read Ethernet Address from eeprom to buffer
1335449c685SForest Bond  *
1345449c685SForest Bond  * Parameters:
1355449c685SForest Bond  *  In:
13626f64a6bSVarsha Rao  *      iobase          - I/O base address
1375449c685SForest Bond  *  Out:
1385449c685SForest Bond  *      pbyEtherAddress - Ethernet Address buffer
1395449c685SForest Bond  *
1405449c685SForest Bond  * Return Value: none
1415449c685SForest Bond  *
1425449c685SForest Bond  */
14326f64a6bSVarsha Rao void SROMvReadEtherAddress(void __iomem *iobase,
1445150d01eSKathryn Hampton 			   unsigned char *pbyEtherAddress)
1455449c685SForest Bond {
1463fc9b584SCharles Clément 	unsigned char ii;
1475449c685SForest Bond 
148ec8f002eSSteven Harms 	/* ii = Rom Address */
149078b078fSCharles Clément 	for (ii = 0; ii < ETH_ALEN; ii++) {
15026f64a6bSVarsha Rao 		*pbyEtherAddress = SROMbyReadEmbedded(iobase, ii);
1515449c685SForest Bond 		pbyEtherAddress++;
1525449c685SForest Bond 	}
1535449c685SForest Bond }
154