Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-block.git] / drivers / staging / vt6655 / srom.c
CommitLineData
5449c685
FB
1/*
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
5449c685 45#include "upc.h"
5449c685 46#include "tmacro.h"
5449c685 47#include "tether.h"
5449c685 48#include "mac.h"
5449c685 49#include "srom.h"
5449c685
FB
50
51/*--------------------- Static Definitions -------------------------*/
52
53/*--------------------- Static Classes ----------------------------*/
54
55/*--------------------- Static Variables --------------------------*/
56
57/*--------------------- Static Functions --------------------------*/
58
59/*--------------------- Export Variables --------------------------*/
60
61/*--------------------- Export Functions --------------------------*/
62
63
64
65
66/*
67 * Description: Read a byte from EEPROM, by MAC I2C
68 *
69 * Parameters:
70 * In:
71 * dwIoBase - I/O base address
72 * byContntOffset - address of EEPROM
73 * Out:
74 * none
75 *
76 * Return Value: data read
77 *
78 */
3fc9b584 79unsigned char SROMbyReadEmbedded(unsigned long dwIoBase, unsigned char byContntOffset)
5449c685 80{
2986db5f 81 unsigned short wDelay, wNoACK;
3fc9b584
CC
82 unsigned char byWait;
83 unsigned char byData;
84 unsigned char byOrg;
5449c685
FB
85
86 byData = 0xFF;
87 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
ec8f002e 88 /* turn off hardware retry for getting NACK */
5449c685
FB
89 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
90 for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
91 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
92 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
93
ec8f002e 94 /* issue read command */
5449c685 95 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMR);
ec8f002e 96 /* wait DONE be set */
5449c685
FB
97 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
98 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
256a816b 99 if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
5449c685
FB
100 break;
101 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
102 }
103 if ((wDelay < W_MAX_TIMEOUT) &&
256a816b 104 ( !(byWait & I2MCSR_NACK))) {
5449c685
FB
105 break;
106 }
107 }
108 VNSvInPortB(dwIoBase + MAC_REG_I2MDIPT, &byData);
109 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
110 return byData;
111}
112
113
114/*
115 * Description: Write a byte to EEPROM, by MAC I2C
116 *
117 * Parameters:
118 * In:
119 * dwIoBase - I/O base address
120 * byContntOffset - address of EEPROM
121 * wData - data to write
122 * Out:
123 * none
124 *
5a5a2a6a 125 * Return Value: true if succeeded; false if failed.
5449c685
FB
126 *
127 */
7b6a0013 128bool SROMbWriteEmbedded(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byData)
5449c685 129{
2986db5f 130 unsigned short wDelay, wNoACK;
3fc9b584 131 unsigned char byWait;
5449c685 132
3fc9b584 133 unsigned char byOrg;
5449c685
FB
134
135 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
ec8f002e 136 /* turn off hardware retry for getting NACK */
5449c685
FB
137 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg & (~I2MCFG_NORETRY)));
138 for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
139 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGID, EEP_I2C_DEV_ID);
140 VNSvOutPortB(dwIoBase + MAC_REG_I2MTGAD, byContntOffset);
141 VNSvOutPortB(dwIoBase + MAC_REG_I2MDOPT, byData);
142
ec8f002e 143 /* issue write command */
5449c685 144 VNSvOutPortB(dwIoBase + MAC_REG_I2MCSR, I2MCSR_EEMW);
ec8f002e 145 /* wait DONE be set */
5449c685
FB
146 for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
147 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
256a816b 148 if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
5449c685
FB
149 break;
150 PCAvDelayByIO(CB_DELAY_LOOP_WAIT);
151 }
152
153 if ((wDelay < W_MAX_TIMEOUT) &&
256a816b 154 ( !(byWait & I2MCSR_NACK))) {
5449c685
FB
155 break;
156 }
157 }
158 if (wNoACK == W_MAX_I2CRETRY) {
159 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
5a5a2a6a 160 return false;
5449c685
FB
161 }
162 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
1b12068a 163 return true;
5449c685
FB
164}
165
166
167/*
168 * Description: Turn bits on in eeprom
169 *
170 * Parameters:
171 * In:
172 * dwIoBase - I/O base address
173 * byContntOffset - address of EEPROM
174 * byBits - bits to turn on
175 * Out:
176 * none
177 *
178 * Return Value: none
179 *
180 */
3fc9b584 181void SROMvRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
5449c685 182{
3fc9b584 183 unsigned char byOrgData;
5449c685
FB
184
185 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
3fc9b584 186 SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData | byBits));
5449c685
FB
187}
188
189
190/*
191 * Description: Turn bits off in eeprom
192 *
193 * Parameters:
194 * In:
195 * dwIoBase - I/O base address
196 * byContntOffset - address of EEPROM
197 * byBits - bits to turn off
198 * Out:
199 * none
200 *
201 */
3fc9b584 202void SROMvRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byBits)
5449c685 203{
3fc9b584 204 unsigned char byOrgData;
5449c685
FB
205
206 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
3fc9b584 207 SROMbWriteEmbedded(dwIoBase, byContntOffset,(unsigned char)(byOrgData & (~byBits)));
5449c685
FB
208}
209
210
211/*
212 * Description: Test if bits on in eeprom
213 *
214 * Parameters:
215 * In:
216 * dwIoBase - I/O base address
217 * byContntOffset - address of EEPROM
218 * byTestBits - bits to test
219 * Out:
220 * none
221 *
5a5a2a6a 222 * Return Value: true if all test bits on; otherwise false
5449c685
FB
223 *
224 */
7b6a0013 225bool SROMbIsRegBitsOn(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
5449c685 226{
3fc9b584 227 unsigned char byOrgData;
5449c685
FB
228
229 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
256a816b 230 return (byOrgData & byTestBits) == byTestBits;
5449c685
FB
231}
232
233
234/*
235 * Description: Test if bits off in eeprom
236 *
237 * Parameters:
238 * In:
239 * dwIoBase - I/O base address
240 * byContntOffset - address of EEPROM
241 * byTestBits - bits to test
242 * Out:
243 * none
244 *
5a5a2a6a 245 * Return Value: true if all test bits off; otherwise false
5449c685
FB
246 *
247 */
7b6a0013 248bool SROMbIsRegBitsOff(unsigned long dwIoBase, unsigned char byContntOffset, unsigned char byTestBits)
5449c685 249{
3fc9b584 250 unsigned char byOrgData;
5449c685
FB
251
252 byOrgData = SROMbyReadEmbedded(dwIoBase, byContntOffset);
256a816b 253 return !(byOrgData & byTestBits);
5449c685
FB
254}
255
256
257/*
258 * Description: Read all contents of eeprom to buffer
259 *
260 * Parameters:
261 * In:
262 * dwIoBase - I/O base address
263 * Out:
264 * pbyEepromRegs - EEPROM content Buffer
265 *
266 * Return Value: none
267 *
268 */
412b2d08 269void SROMvReadAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
5449c685
FB
270{
271 int ii;
272
ec8f002e 273 /* ii = Rom Address */
5449c685 274 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
3fc9b584 275 *pbyEepromRegs = SROMbyReadEmbedded(dwIoBase,(unsigned char) ii);
5449c685
FB
276 pbyEepromRegs++;
277 }
278}
279
280
281/*
282 * Description: Write all contents of buffer to eeprom
283 *
284 * Parameters:
285 * In:
286 * dwIoBase - I/O base address
287 * pbyEepromRegs - EEPROM content Buffer
288 * Out:
289 * none
290 *
291 * Return Value: none
292 *
293 */
412b2d08 294void SROMvWriteAllContents(unsigned long dwIoBase, unsigned char *pbyEepromRegs)
5449c685
FB
295{
296 int ii;
297
ec8f002e 298 /* ii = Rom Address */
5449c685 299 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
3fc9b584 300 SROMbWriteEmbedded(dwIoBase,(unsigned char) ii, *pbyEepromRegs);
5449c685
FB
301 pbyEepromRegs++;
302 }
303}
304
305
306/*
307 * Description: Read Ethernet Address from eeprom to buffer
308 *
309 * Parameters:
310 * In:
311 * dwIoBase - I/O base address
312 * Out:
313 * pbyEtherAddress - Ethernet Address buffer
314 *
315 * Return Value: none
316 *
317 */
412b2d08 318void SROMvReadEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
5449c685 319{
3fc9b584 320 unsigned char ii;
5449c685 321
ec8f002e 322 /* ii = Rom Address */
078b078f 323 for (ii = 0; ii < ETH_ALEN; ii++) {
5449c685
FB
324 *pbyEtherAddress = SROMbyReadEmbedded(dwIoBase, ii);
325 pbyEtherAddress++;
326 }
327}
328
329
330/*
331 * Description: Write Ethernet Address from buffer to eeprom
332 *
333 * Parameters:
334 * In:
335 * dwIoBase - I/O base address
336 * pbyEtherAddress - Ethernet Address buffer
337 * Out:
338 * none
339 *
340 * Return Value: none
341 *
342 */
412b2d08 343void SROMvWriteEtherAddress(unsigned long dwIoBase, unsigned char *pbyEtherAddress)
5449c685 344{
3fc9b584 345 unsigned char ii;
5449c685 346
ec8f002e 347 /* ii = Rom Address */
078b078f 348 for (ii = 0; ii < ETH_ALEN; ii++) {
5449c685
FB
349 SROMbWriteEmbedded(dwIoBase, ii, *pbyEtherAddress);
350 pbyEtherAddress++;
351 }
352}
353
354
355/*
356 * Description: Read Sub_VID and Sub_SysId from eeprom to buffer
357 *
358 * Parameters:
359 * In:
360 * dwIoBase - I/O base address
361 * Out:
362 * pdwSubSysVenId - Sub_VID and Sub_SysId read
363 *
364 * Return Value: none
365 *
366 */
412b2d08 367void SROMvReadSubSysVenId(unsigned long dwIoBase, unsigned long *pdwSubSysVenId)
5449c685 368{
2989e96f 369 unsigned char *pbyData;
5449c685 370
2989e96f 371 pbyData = (unsigned char *)pdwSubSysVenId;
ec8f002e 372 /* sub vendor */
5449c685
FB
373 *pbyData = SROMbyReadEmbedded(dwIoBase, 6);
374 *(pbyData+1) = SROMbyReadEmbedded(dwIoBase, 7);
ec8f002e 375 /* sub system */
5449c685
FB
376 *(pbyData+2) = SROMbyReadEmbedded(dwIoBase, 8);
377 *(pbyData+3) = SROMbyReadEmbedded(dwIoBase, 9);
378}
379
380/*
381 * Description: Auto Load EEPROM to MAC register
382 *
383 * Parameters:
384 * In:
385 * dwIoBase - I/O base address
386 * Out:
387 * none
388 *
5a5a2a6a 389 * Return Value: true if success; otherwise false
5449c685
FB
390 *
391 */
7b6a0013 392bool SROMbAutoLoad(unsigned long dwIoBase)
5449c685 393{
3fc9b584 394 unsigned char byWait;
5449c685
FB
395 int ii;
396
3fc9b584 397 unsigned char byOrg;
5449c685
FB
398
399 VNSvInPortB(dwIoBase + MAC_REG_I2MCFG, &byOrg);
ec8f002e 400 /* turn on hardware retry */
5449c685
FB
401 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, (byOrg | I2MCFG_NORETRY));
402
403 MACvRegBitsOn(dwIoBase, MAC_REG_I2MCSR, I2MCSR_AUTOLD);
404
ec8f002e 405 /* ii = Rom Address */
5449c685
FB
406 for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
407 MACvTimer0MicroSDelay(dwIoBase, CB_EEPROM_READBYTE_WAIT);
408 VNSvInPortB(dwIoBase + MAC_REG_I2MCSR, &byWait);
256a816b 409 if ( !(byWait & I2MCSR_AUTOLD))
5449c685
FB
410 break;
411 }
412
413 VNSvOutPortB(dwIoBase + MAC_REG_I2MCFG, byOrg);
414
415 if (ii == EEP_MAX_CONTEXT_SIZE)
5a5a2a6a 416 return false;
1b12068a 417 return true;
5449c685
FB
418}
419
420