grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / devs / networks / realtek8180 / eeprom.c
blobfba4bba46bbf09fe9a68285020a01a712ed8dc02
1 /*
3 Copyright (C) 2001-2011 Neil Cafferkey
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.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA.
23 #include "device.h"
25 #include "eeprom_protos.h"
26 #include "timer_protos.h"
27 #include "realtek8187.h"
30 static ULONG ReadEEPROMBits(struct DevUnit *unit, UBYTE count,
31 struct DevBase *base);
32 static VOID WriteEEPROMBits(struct DevUnit *unit, ULONG value, UBYTE count,
33 struct DevBase *base);
34 static BOOL ReadEEPROMBit(struct DevUnit *unit, struct DevBase *base);
35 static BOOL WriteEEPROMBit(struct DevUnit *unit, BOOL is_one,
36 struct DevBase *base);
39 /****i* realtek8180.device/GetEEPROMAddressSize ****************************
41 * NAME
42 * GetEEPROMAddressSize
44 * SYNOPSIS
45 * size = GetEEPROMAddressSize(unit)
47 * UWORD GetEEPROMAddressSize(struct DevUnit *);
49 * INPUTS
50 * unit - A unit of this device.
52 * RESULT
53 * size - Width of EEPROM addresses.
55 ****************************************************************************
59 #if 0
60 static UWORD GetEEPROMAddressSize(struct DevUnit *unit,
61 struct DevBase *base)
63 UWORD size;
65 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM, R8180REG_EEPROMF_SELECT);
66 WriteEEPROMBits(unit, 0x6, 3, base);
67 for(size = 1; WriteEEPROMBit(unit, FALSE, base); size++);
68 ReadEEPROMBits(unit, 16, base);
69 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM, 0);
70 BusyMicroDelay(1, base);
72 return size;
74 #endif
78 /****i* realtek8180.device/ReadEEPROM **************************************
80 * NAME
81 * ReadEEPROM -- Read an EEPROM location.
83 * SYNOPSIS
84 * value = ReadEEPROM(unit, index)
86 * UWORD ReadEEPROM(struct DevUnit *, UWORD);
88 * INPUTS
89 * unit - A unit of this device.
90 * index - Offset within EEPROM.
92 * RESULT
93 * value - Contents of specified EEPROM location.
95 ****************************************************************************
99 UWORD ReadEEPROM(struct DevUnit *unit, UWORD index,
100 struct DevBase *base)
102 UWORD value;
104 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM,
105 R8180ECMD_PROGRAM | R8180REG_EEPROMF_SELECT);
106 WriteEEPROMBits(unit, 0x6, 3, base);
107 WriteEEPROMBits(unit, index, unit->eeprom_addr_size, base);
108 value = ReadEEPROMBits(unit, 16, base);
109 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM, R8180ECMD_PROGRAM);
110 BusyMicroDelay(1, base);
112 return value;
117 /****i* realtek8180.device/ReadEEPROMBits **********************************
119 * NAME
120 * ReadEEPROMBits -- Read a stream of bits from the EEPROM.
122 * SYNOPSIS
123 * value = ReadEEPROMBits(unit, count)
125 * ULONG ReadEEPROMBits(struct DevUnit *, UBYTE);
127 * INPUTS
128 * unit - A unit of this device.
129 * count - Number of bits to be read.
131 * RESULT
132 * value - The bits read from the EEPROM, right-justified.
134 ****************************************************************************
138 static ULONG ReadEEPROMBits(struct DevUnit *unit, UBYTE count,
139 struct DevBase *base)
141 UBYTE i;
142 ULONG value = 0;
144 for(i = 0; i < count; i++)
146 value <<= 1;
147 if(ReadEEPROMBit(unit, base))
148 value |= 0x1;
151 return value;
155 /****i* realtek8180.device/WriteEEPROMBits *********************************
157 * NAME
158 * WriteEEPROMBits -- Write a stream of bits to the EEPROM.
160 * SYNOPSIS
161 * WriteEEPROMBits(unit, value, count)
163 * VOID WriteEEPROMBits(struct DevUnit *, ULONG, UBYTE);
165 * INPUTS
166 * unit - A unit of this device.
167 * value - The bits to write to the EEPROM, right-justified.
168 * count - Number of bits to be Write.
170 * RESULT
171 * None.
173 ****************************************************************************
177 static VOID WriteEEPROMBits(struct DevUnit *unit, ULONG value, UBYTE count,
178 struct DevBase *base)
180 ULONG mask;
182 for(mask = 1 << (count - 1); mask != 0; mask >>= 1)
183 WriteEEPROMBit(unit, (value & mask) != 0, base);
185 return;
190 /****i* realtek8180.device/ReadEEPROMBit ***********************************
192 * NAME
193 * ReadEEPROMBit -- Read a bit from the EEPROM.
195 * SYNOPSIS
196 * value = ReadEEPROMBit(unit)
198 * BOOL ReadEEPROMBit(struct DevUnit *);
200 * INPUTS
201 * unit - A unit of this device.
203 * RESULT
204 * value - True for one, false for zero.
206 ****************************************************************************
210 static BOOL ReadEEPROMBit(struct DevUnit *unit, struct DevBase *base)
212 BOOL is_one;
214 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM,
215 R8180ECMD_PROGRAM | R8180REG_EEPROMF_SELECT | R8180REG_EEPROMF_CLK);
216 BusyMicroDelay(2, base);
217 is_one =
218 (unit->ByteIn(unit->card, 0x100 + R8180REG_EEPROM)
219 & R8180REG_EEPROMF_DATAIN) != 0;
220 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM,
221 R8180ECMD_PROGRAM | R8180REG_EEPROMF_SELECT);
222 BusyMicroDelay(2, base);
224 return is_one;
229 /****i* realtek8180.device/WriteEEPROMBit **********************************
231 * NAME
232 * WriteEEPROMBit -- Write a bit to the EEPROM.
234 * SYNOPSIS
235 * data_in = WriteEEPROMBit(unit, is_one)
237 * BOOL WriteEEPROMBit(struct DevUnit *, BOOL);
239 * INPUTS
240 * unit - A unit of this device.
241 * is_one - True if a set bit should be written.
243 * RESULT
244 * data_in - True if data-in bit is set.
246 ****************************************************************************
250 static BOOL WriteEEPROMBit(struct DevUnit *unit, BOOL is_one,
251 struct DevBase *base)
253 UWORD data_out;
255 if(is_one)
256 data_out = R8180REG_EEPROMF_DATAOUT;
257 else
258 data_out = 0;
260 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM,
261 R8180ECMD_PROGRAM | R8180REG_EEPROMF_SELECT | data_out);
262 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM, R8180ECMD_PROGRAM
263 | R8180REG_EEPROMF_SELECT | R8180REG_EEPROMF_CLK | data_out);
264 BusyMicroDelay(2, base);
265 unit->ByteOut(unit->card, 0x100 + R8180REG_EEPROM,
266 R8180ECMD_PROGRAM | R8180REG_EEPROMF_SELECT | data_out);
267 BusyMicroDelay(2, base);
269 return (unit->ByteIn(unit->card, 0x100 + R8180REG_EEPROM)
270 & R8180REG_EEPROMF_DATAIN) != 0;