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 */
59unsigned 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	byData = 0xFF;
68	byOrg = ioread8(iobase + MAC_REG_I2MCFG);
69	/* turn off hardware retry for getting NACK */
70	iowrite8(byOrg & (~I2MCFG_NORETRY), iobase + MAC_REG_I2MCFG);
71	for (wNoACK = 0; wNoACK < W_MAX_I2CRETRY; wNoACK++) {
72		iowrite8(EEP_I2C_DEV_ID, iobase + MAC_REG_I2MTGID);
73		iowrite8(contnt_offset, iobase + MAC_REG_I2MTGAD);
74
75		/* issue read command */
76		iowrite8(I2MCSR_EEMR, iobase + MAC_REG_I2MCSR);
77		/* wait DONE be set */
78		for (wDelay = 0; wDelay < W_MAX_TIMEOUT; wDelay++) {
79			byWait = ioread8(iobase + MAC_REG_I2MCSR);
80			if (byWait & (I2MCSR_DONE | I2MCSR_NACK))
81				break;
82			udelay(CB_DELAY_LOOP_WAIT);
83		}
84		if ((wDelay < W_MAX_TIMEOUT) &&
85		    (!(byWait & I2MCSR_NACK))) {
86			break;
87		}
88	}
89	byData = ioread8(iobase + MAC_REG_I2MDIPT);
90	iowrite8(byOrg, iobase + MAC_REG_I2MCFG);
91	return byData;
92}
93
94/*
95 * Description: Read all contents of eeprom to buffer
96 *
97 * Parameters:
98 *  In:
99 *      iobase          - I/O base address
100 *  Out:
101 *      pbyEepromRegs   - EEPROM content Buffer
102 *
103 * Return Value: none
104 *
105 */
106void SROMvReadAllContents(void __iomem *iobase, unsigned char *pbyEepromRegs)
107{
108	int     ii;
109
110	/* ii = Rom Address */
111	for (ii = 0; ii < EEP_MAX_CONTEXT_SIZE; ii++) {
112		*pbyEepromRegs = SROMbyReadEmbedded(iobase,
113						    (unsigned char)ii);
114		pbyEepromRegs++;
115	}
116}
117
118/*
119 * Description: Read Ethernet Address from eeprom to buffer
120 *
121 * Parameters:
122 *  In:
123 *      iobase          - I/O base address
124 *  Out:
125 *      pbyEtherAddress - Ethernet Address buffer
126 *
127 * Return Value: none
128 *
129 */
130void SROMvReadEtherAddress(void __iomem *iobase,
131			   unsigned char *pbyEtherAddress)
132{
133	unsigned char ii;
134
135	/* ii = Rom Address */
136	for (ii = 0; ii < ETH_ALEN; ii++) {
137		*pbyEtherAddress = SROMbyReadEmbedded(iobase, ii);
138		pbyEtherAddress++;
139	}
140}
141