1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * Purpose:Implement functions to access eeprom
7  *
8  * Author: Jerry Chen
9  *
10  * Date: Jan 29, 2003
11  *
12  * Functions:
13  *      SROMbyReadEmbedded - Embedded read eeprom via MAC
14  *      SROMbWriteEmbedded - Embedded write eeprom via MAC
15  *      SROMvRegBitsOn - Set Bits On in eeprom
16  *      SROMvRegBitsOff - Clear Bits Off in eeprom
17  *      SROMbIsRegBitsOn - Test if Bits On in eeprom
18  *      SROMbIsRegBitsOff - Test if Bits Off in eeprom
19  *      SROMvReadAllContents - Read all contents in eeprom
20  *      SROMvWriteAllContents - Write all contents in eeprom
21  *      SROMvReadEtherAddress - Read Ethernet Address in eeprom
22  *      SROMvWriteEtherAddress - Write Ethernet Address in eeprom
23  *      SROMvReadSubSysVenId - Read Sub_VID and Sub_SysId in eeprom
24  *      SROMbAutoLoad - Auto Load eeprom to MAC register
25  *
26  * Revision History:
27  *
28  */
29 
30 #include "device.h"
31 #include "mac.h"
32 #include "srom.h"
33 
34 /*---------------------  Static Definitions -------------------------*/
35 
36 /*---------------------  Static Classes  ----------------------------*/
37 
38 /*---------------------  Static Variables  --------------------------*/
39 
40 /*---------------------  Static Functions  --------------------------*/
41 
42 /*---------------------  Export Variables  --------------------------*/
43 
44 /*---------------------  Export Functions  --------------------------*/
45 
46 /*
47  * Description: Read a byte from EEPROM, by MAC I2C
48  *
49  * Parameters:
50  *  In:
51  *      iobase          - I/O base address
52  *      contnt_offset  - address of EEPROM
53  *  Out:
54  *      none
55  *
56  * Return Value: data read
57  *
58  */
SROMbyReadEmbedded(void __iomem * iobase,unsigned char contnt_offset)59 unsigned char SROMbyReadEmbedded(void __iomem *iobase,
60 				 unsigned char contnt_offset)
61 {
62 	unsigned short wDelay, wNoACK;
63 	unsigned char byWait;
64 	unsigned char byData;
65 	unsigned char byOrg;
66 
67 	byOrg = ioread8(iobase + MAC_REG_I2MCFG);
68 	/* turn off hardware retry for getting NACK */
69 	iowrite8(byOrg & (~I2MCFG_NORETRY), iobase + MAC_REG_I2MCFG);
70 	for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
71 		iowrite8(EEP_I2C_DEV_ID, iobase + MAC_REG_I2MTGID);
72 		iowrite8(contnt_offset, iobase + MAC_REG_I2MTGAD);
73 
74 		/* issue read command */
75 		iowrite8(I2MCSR_EEMR, iobase + MAC_REG_I2MCSR);
76 		/* wait DONE be set */
77 		for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
78 			byWait = ioread8(iobase + MAC_REG_I2MCSR);
79 			if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
80 				break;
81 			udelay(CB_DELAY_LOOP_WAIT);
82 		}
83 		if ((wDelay < W_MAX_TIMEOUT) &&
84 		    (!(byWait & I2MCSR_NACK))) {
85 			break;
86 		}
87 	}
88 	byData = ioread8(iobase + MAC_REG_I2MDIPT);
89 	iowrite8(byOrg, iobase + MAC_REG_I2MCFG);
90 	return byData;
91 }
92 
93 /*
94  * Description: Read all contents of eeprom to buffer
95  *
96  * Parameters:
97  *  In:
98  *      iobase          - I/O base address
99  *  Out:
100  *      pbyEepromRegs   - EEPROM content Buffer
101  *
102  * Return Value: none
103  *
104  */
SROMvReadAllContents(void __iomem * iobase,unsigned char * pbyEepromRegs)105 void SROMvReadAllContents(void __iomem *iobase, unsigned char *pbyEepromRegs)
106 {
107 	int     ii;
108 
109 	/* ii = Rom Address */
110 	for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
111 		*pbyEepromRegs = SROMbyReadEmbedded(iobase,
112 						    (unsigned char)ii);
113 		pbyEepromRegs++;
114 	}
115 }
116 
117 /*
118  * Description: Read Ethernet Address from eeprom to buffer
119  *
120  * Parameters:
121  *  In:
122  *      iobase          - I/O base address
123  *  Out:
124  *      pbyEtherAddress - Ethernet Address buffer
125  *
126  * Return Value: none
127  *
128  */
SROMvReadEtherAddress(void __iomem * iobase,unsigned char * pbyEtherAddress)129 void SROMvReadEtherAddress(void __iomem *iobase,
130 			   unsigned char *pbyEtherAddress)
131 {
132 	unsigned char ii;
133 
134 	/* ii = Rom Address */
135 	for (ii = 0; ii < ETH_ALEN; ii++) {
136 		*pbyEtherAddress = SROMbyReadEmbedded(iobase, ii);
137 		pbyEtherAddress++;
138 	}
139 }
140