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