blob: eaddc33c9d2d79d2855d6103cc634ded3dd78dc0 [file] [log] [blame]
Forest Bond5449c682009-04-25 10:30:44 -04001/*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
4 *
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.
9 *
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.
14 *
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.
18 *
19 * File: srom.c
20 *
21 * Purpose:Implement functions to access eeprom
22 *
23 * Author: Jerry Chen
24 *
25 * Date: Jan 29, 2003
26 *
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
40 *
41 * Revision History:
42 *
43 */
44
Forest Bond5449c682009-04-25 10:30:44 -040045#include "upc.h"
Forest Bond5449c682009-04-25 10:30:44 -040046#include "tmacro.h"
Forest Bond5449c682009-04-25 10:30:44 -040047#include "tether.h"
Forest Bond5449c682009-04-25 10:30:44 -040048#include "mac.h"
Forest Bond5449c682009-04-25 10:30:44 -040049#include "srom.h"
Forest Bond5449c682009-04-25 10:30:44 -040050
51/*--------------------- Static Definitions -------------------------*/
52
53/*--------------------- Static Classes ----------------------------*/
54
55/*--------------------- Static Variables --------------------------*/
56
57/*--------------------- Static Functions --------------------------*/
58
59/*--------------------- Export Variables --------------------------*/
60
61/*--------------------- Export Functions --------------------------*/
62
Forest Bond5449c682009-04-25 10:30:44 -040063/*
64 * Description: Read a byte from EEPROM, by MAC I2C
65 *
66 * Parameters:
67 * In:
68 * dwIoBase - I/O base address
69 * byContntOffset - address of EEPROM
70 * Out:
71 * none
72 *
73 * Return Value: data read
74 *
75 */
Charles Clément3fc9b582010-06-24 11:02:27 -070076unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
Forest Bond5449c682009-04-25 10:30:44 -040077{
Joe Perches0c979142013-03-18 10:45:02 -070078 unsigned short wDelay, wNoACK;
79 unsigned char byWait;
80 unsigned char byData;
81 unsigned char byOrg;
Forest Bond5449c682009-04-25 10:30:44 -040082
Joe Perches0c979142013-03-18 10:45:02 -070083 byData = 0xFF;
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);
Forest Bond5449c682009-04-25 10:30:44 -040090
Joe Perches0c979142013-03-18 10:45:02 -070091 /* 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))
97 break;
98 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
99 }
100 if ((wDelay < W_MAX_TIMEOUT) &&
101 (!(byWait & I2MCSR_NACK))) {
102 break;
103 }
104 }
105 VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
106 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
107 return byData;
Forest Bond5449c682009-04-25 10:30:44 -0400108}
109
Forest Bond5449c682009-04-25 10:30:44 -0400110/*
111 * Description: Write a byte to EEPROM, by MAC I2C
112 *
113 * Parameters:
114 * In:
115 * dwIoBase - I/O base address
116 * byContntOffset - address of EEPROM
117 * wData - data to write
118 * Out:
119 * none
120 *
Charles Clément5a5a2a62010-08-01 17:15:49 +0200121 * Return Value: true if succeeded; false if failed.
Forest Bond5449c682009-04-25 10:30:44 -0400122 *
123 */
Charles Clément7b6a0012010-08-01 17:15:50 +0200124bool SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData)
Forest Bond5449c682009-04-25 10:30:44 -0400125{
Joe Perches0c979142013-03-18 10:45:02 -0700126 unsigned short wDelay, wNoACK;
127 unsigned char byWait;
Forest Bond5449c682009-04-25 10:30:44 -0400128
Joe Perches0c979142013-03-18 10:45:02 -0700129 unsigned char byOrg;
Forest Bond5449c682009-04-25 10:30:44 -0400130
Joe Perches0c979142013-03-18 10:45:02 -0700131 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);
Forest Bond5449c682009-04-25 10:30:44 -0400138
Joe Perches0c979142013-03-18 10:45:02 -0700139 /* 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))
145 break;
146 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
147 }
Forest Bond5449c682009-04-25 10:30:44 -0400148
Joe Perches0c979142013-03-18 10:45:02 -0700149 if ((wDelay < W_MAX_TIMEOUT) &&
150 (!(byWait & I2MCSR_NACK))) {
151 break;
152 }
153 }
154 if (wNoACK == W_MAX_I2CRETRY) {
155 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
156 return false;
157 }
158 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
159 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400160}
161
Forest Bond5449c682009-04-25 10:30:44 -0400162/*
163 * Description: Turn bits on in eeprom
164 *
165 * Parameters:
166 * In:
167 * dwIoBase - I/O base address
168 * byContntOffset - address of EEPROM
169 * byBits - bits to turn on
170 * Out:
171 * none
172 *
173 * Return Value: none
174 *
175 */
Charles Clément3fc9b582010-06-24 11:02:27 -0700176void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
Forest Bond5449c682009-04-25 10:30:44 -0400177{
Joe Perches0c979142013-03-18 10:45:02 -0700178 unsigned char byOrgData;
Forest Bond5449c682009-04-25 10:30:44 -0400179
Joe Perches0c979142013-03-18 10:45:02 -0700180 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
181 SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData | byBits));
Forest Bond5449c682009-04-25 10:30:44 -0400182}
183
Forest Bond5449c682009-04-25 10:30:44 -0400184/*
185 * Description: Turn bits off in eeprom
186 *
187 * Parameters:
188 * In:
189 * dwIoBase - I/O base address
190 * byContntOffset - address of EEPROM
191 * byBits - bits to turn off
192 * Out:
193 * none
194 *
195 */
Charles Clément3fc9b582010-06-24 11:02:27 -0700196void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
Forest Bond5449c682009-04-25 10:30:44 -0400197{
Joe Perches0c979142013-03-18 10:45:02 -0700198 unsigned char byOrgData;
Forest Bond5449c682009-04-25 10:30:44 -0400199
Joe Perches0c979142013-03-18 10:45:02 -0700200 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
201 SROMbWriteEmbedded(dwIoBase, byContntOffset, (unsigned char)(byOrgData & (~byBits)));
Forest Bond5449c682009-04-25 10:30:44 -0400202}
203
Forest Bond5449c682009-04-25 10:30:44 -0400204/*
205 * Description: Test if bits on in eeprom
206 *
207 * Parameters:
208 * In:
209 * dwIoBase - I/O base address
210 * byContntOffset - address of EEPROM
211 * byTestBits - bits to test
212 * Out:
213 * none
214 *
Charles Clément5a5a2a62010-08-01 17:15:49 +0200215 * Return Value: true if all test bits on; otherwise false
Forest Bond5449c682009-04-25 10:30:44 -0400216 *
217 */
Charles Clément7b6a0012010-08-01 17:15:50 +0200218bool SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
Forest Bond5449c682009-04-25 10:30:44 -0400219{
Joe Perches0c979142013-03-18 10:45:02 -0700220 unsigned char byOrgData;
Forest Bond5449c682009-04-25 10:30:44 -0400221
Joe Perches0c979142013-03-18 10:45:02 -0700222 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
223 return (byOrgData & byTestBits) == byTestBits;
Forest Bond5449c682009-04-25 10:30:44 -0400224}
225
Forest Bond5449c682009-04-25 10:30:44 -0400226/*
227 * Description: Test if bits off in eeprom
228 *
229 * Parameters:
230 * In:
231 * dwIoBase - I/O base address
232 * byContntOffset - address of EEPROM
233 * byTestBits - bits to test
234 * Out:
235 * none
236 *
Charles Clément5a5a2a62010-08-01 17:15:49 +0200237 * Return Value: true if all test bits off; otherwise false
Forest Bond5449c682009-04-25 10:30:44 -0400238 *
239 */
Charles Clément7b6a0012010-08-01 17:15:50 +0200240bool SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
Forest Bond5449c682009-04-25 10:30:44 -0400241{
Joe Perches0c979142013-03-18 10:45:02 -0700242 unsigned char byOrgData;
Forest Bond5449c682009-04-25 10:30:44 -0400243
Joe Perches0c979142013-03-18 10:45:02 -0700244 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
245 return !(byOrgData & byTestBits);
Forest Bond5449c682009-04-25 10:30:44 -0400246}
247
Forest Bond5449c682009-04-25 10:30:44 -0400248/*
249 * Description: Read all contents of eeprom to buffer
250 *
251 * Parameters:
252 * In:
253 * dwIoBase - I/O base address
254 * Out:
255 * pbyEepromRegs - EEPROM content Buffer
256 *
257 * Return Value: none
258 *
259 */
Charles Clément412b2d02010-06-22 08:54:42 -0700260void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
Forest Bond5449c682009-04-25 10:30:44 -0400261{
Joe Perches0c979142013-03-18 10:45:02 -0700262 int ii;
Forest Bond5449c682009-04-25 10:30:44 -0400263
Joe Perches0c979142013-03-18 10:45:02 -0700264 /* ii = Rom Address */
265 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
266 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase, (unsigned char)ii);
267 pbyEepromRegs++;
268 }
Forest Bond5449c682009-04-25 10:30:44 -0400269}
270
Forest Bond5449c682009-04-25 10:30:44 -0400271/*
272 * Description: Write all contents of buffer to eeprom
273 *
274 * Parameters:
275 * In:
276 * dwIoBase - I/O base address
277 * pbyEepromRegs - EEPROM content Buffer
278 * Out:
279 * none
280 *
281 * Return Value: none
282 *
283 */
Charles Clément412b2d02010-06-22 08:54:42 -0700284void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
Forest Bond5449c682009-04-25 10:30:44 -0400285{
Joe Perches0c979142013-03-18 10:45:02 -0700286 int ii;
Forest Bond5449c682009-04-25 10:30:44 -0400287
Joe Perches0c979142013-03-18 10:45:02 -0700288 /* ii = Rom Address */
289 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
290 SROMbWriteEmbedded(dwIoBase, (unsigned char)ii, *pbyEepromRegs);
291 pbyEepromRegs++;
292 }
Forest Bond5449c682009-04-25 10:30:44 -0400293}
294
Forest Bond5449c682009-04-25 10:30:44 -0400295/*
296 * Description: Read Ethernet Address from eeprom to buffer
297 *
298 * Parameters:
299 * In:
300 * dwIoBase - I/O base address
301 * Out:
302 * pbyEtherAddress - Ethernet Address buffer
303 *
304 * Return Value: none
305 *
306 */
Charles Clément412b2d02010-06-22 08:54:42 -0700307void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
Forest Bond5449c682009-04-25 10:30:44 -0400308{
Joe Perches0c979142013-03-18 10:45:02 -0700309 unsigned char ii;
Forest Bond5449c682009-04-25 10:30:44 -0400310
Joe Perches0c979142013-03-18 10:45:02 -0700311 /* ii = Rom Address */
312 for (ii = 0; ii < ETH_ALEN; ii++) {
313 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
314 pbyEtherAddress++;
315 }
Forest Bond5449c682009-04-25 10:30:44 -0400316}
317
Forest Bond5449c682009-04-25 10:30:44 -0400318/*
319 * Description: Write Ethernet Address from buffer to eeprom
320 *
321 * Parameters:
322 * In:
323 * dwIoBase - I/O base address
324 * pbyEtherAddress - Ethernet Address buffer
325 * Out:
326 * none
327 *
328 * Return Value: none
329 *
330 */
Charles Clément412b2d02010-06-22 08:54:42 -0700331void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
Forest Bond5449c682009-04-25 10:30:44 -0400332{
Joe Perches0c979142013-03-18 10:45:02 -0700333 unsigned char ii;
Forest Bond5449c682009-04-25 10:30:44 -0400334
Joe Perches0c979142013-03-18 10:45:02 -0700335 /* ii = Rom Address */
336 for (ii = 0; ii < ETH_ALEN; ii++) {
337 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
338 pbyEtherAddress++;
339 }
Forest Bond5449c682009-04-25 10:30:44 -0400340}
341
Forest Bond5449c682009-04-25 10:30:44 -0400342/*
343 * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
344 *
345 * Parameters:
346 * In:
347 * dwIoBase - I/O base address
348 * Out:
349 * pdwSubSysVenId - Sub_VID and Sub_SysId read
350 *
351 * Return Value: none
352 *
353 */
Charles Clément412b2d02010-06-22 08:54:42 -0700354void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
Forest Bond5449c682009-04-25 10:30:44 -0400355{
Joe Perches0c979142013-03-18 10:45:02 -0700356 unsigned char *pbyData;
Forest Bond5449c682009-04-25 10:30:44 -0400357
Joe Perches0c979142013-03-18 10:45:02 -0700358 pbyData = (unsigned char *)pdwSubSysVenId;
359 /* sub vendor */
360 *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
361 *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
362 /* sub system */
363 *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
364 *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
Forest Bond5449c682009-04-25 10:30:44 -0400365}
366
367/*
368 * Description: Auto Load EEPROM to MAC register
369 *
370 * Parameters:
371 * In:
372 * dwIoBase - I/O base address
373 * Out:
374 * none
375 *
Charles Clément5a5a2a62010-08-01 17:15:49 +0200376 * Return Value: true if success; otherwise false
Forest Bond5449c682009-04-25 10:30:44 -0400377 *
378 */
Charles Clément7b6a0012010-08-01 17:15:50 +0200379bool SROMbAutoLoad(unsigned long dwIoBase)
Forest Bond5449c682009-04-25 10:30:44 -0400380{
Joe Perches0c979142013-03-18 10:45:02 -0700381 unsigned char byWait;
382 int ii;
Forest Bond5449c682009-04-25 10:30:44 -0400383
Joe Perches0c979142013-03-18 10:45:02 -0700384 unsigned char byOrg;
Forest Bond5449c682009-04-25 10:30:44 -0400385
Joe Perches0c979142013-03-18 10:45:02 -0700386 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
387 /* turn on hardware retry */
388 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
Forest Bond5449c682009-04-25 10:30:44 -0400389
Joe Perches0c979142013-03-18 10:45:02 -0700390 MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
Forest Bond5449c682009-04-25 10:30:44 -0400391
Joe Perches0c979142013-03-18 10:45:02 -0700392 /* 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))
397 break;
398 }
Forest Bond5449c682009-04-25 10:30:44 -0400399
Joe Perches0c979142013-03-18 10:45:02 -0700400 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
Forest Bond5449c682009-04-25 10:30:44 -0400401
Joe Perches0c979142013-03-18 10:45:02 -0700402 if (ii == EEP_MAX_CONTEXT_SIZE)
403 return false;
404 return true;
Forest Bond5449c682009-04-25 10:30:44 -0400405}