1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 ******************************************************************************/
20 #define _RTW_EEPROM_C_
23 #include <osdep_service.h>
24 #include <drv_types.h>
26 void up_clk(_adapter *padapter, u16 *x)
29 rtw_write8(padapter, EE_9346CR, (u8)*x);
30 rtw_udelay_os(CLOCK_RATE);
35 void down_clk(_adapter *padapter, u16 *x)
38 rtw_write8(padapter, EE_9346CR, (u8)*x);
39 rtw_udelay_os(CLOCK_RATE);
42 void shift_out_bits(_adapter *padapter, u16 data, u16 count)
46 if (rtw_is_surprise_removed(padapter)) {
49 mask = 0x01 << (count - 1);
50 x = rtw_read8(padapter, EE_9346CR);
52 x &= ~(_EEDO | _EEDI);
58 if (rtw_is_surprise_removed(padapter)) {
61 rtw_write8(padapter, EE_9346CR, (u8)x);
62 rtw_udelay_os(CLOCK_RATE);
64 down_clk(padapter, &x);
67 if (rtw_is_surprise_removed(padapter)) {
71 rtw_write8(padapter, EE_9346CR, (u8)x);
76 u16 shift_in_bits(_adapter *padapter)
79 if (rtw_is_surprise_removed(padapter)) {
82 x = rtw_read8(padapter, EE_9346CR);
84 x &= ~(_EEDO | _EEDI);
87 for (i = 0; i < 16; i++) {
90 if (rtw_is_surprise_removed(padapter)) {
93 x = rtw_read8(padapter, EE_9346CR);
99 down_clk(padapter, &x);
106 void standby(_adapter *padapter)
109 x = rtw_read8(padapter, EE_9346CR);
111 x &= ~(_EECS | _EESK);
112 rtw_write8(padapter, EE_9346CR, x);
114 rtw_udelay_os(CLOCK_RATE);
116 rtw_write8(padapter, EE_9346CR, x);
117 rtw_udelay_os(CLOCK_RATE);
120 u16 wait_eeprom_cmd_done(_adapter *padapter)
125 for (i = 0; i < 200; i++) {
126 x = rtw_read8(padapter, EE_9346CR);
131 rtw_udelay_os(CLOCK_RATE);
137 void eeprom_clean(_adapter *padapter)
140 if (rtw_is_surprise_removed(padapter)) {
143 x = rtw_read8(padapter, EE_9346CR);
144 if (rtw_is_surprise_removed(padapter)) {
147 x &= ~(_EECS | _EEDI);
148 rtw_write8(padapter, EE_9346CR, (u8)x);
149 if (rtw_is_surprise_removed(padapter)) {
152 up_clk(padapter, &x);
153 if (rtw_is_surprise_removed(padapter)) {
156 down_clk(padapter, &x);
161 void eeprom_write16(_adapter *padapter, u16 reg, u16 data)
164 #ifdef CONFIG_RTL8712
165 u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
166 tmp8_ori = rtw_read8(padapter, 0x102502f1);
167 tmp8_new = tmp8_ori & 0xf7;
168 if (tmp8_ori != tmp8_new) {
169 rtw_write8(padapter, 0x102502f1, tmp8_new);
171 tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
172 tmp8_clk_new = tmp8_clk_ori | 0x20;
173 if (tmp8_clk_new != tmp8_clk_ori) {
174 rtw_write8(padapter, 0x10250003, tmp8_clk_new);
178 x = rtw_read8(padapter, EE_9346CR);
180 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
182 rtw_write8(padapter, EE_9346CR, x);
184 shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
186 if (padapter->EepromAddressSize == 8) /* CF+ and SDIO */
187 shift_out_bits(padapter, 0, 6);
189 shift_out_bits(padapter, 0, 4);
193 /* Commented out by rcnjko, 2004.0
194 * Erase this particular word. Write the erase opcode and register
195 * number in that order. The opcode is 3bits in length; reg is 6 bits long. */
196 /* shift_out_bits(Adapter, EEPROM_ERASE_OPCODE, 3);
197 * shift_out_bits(Adapter, reg, Adapter->EepromAddressSize);
199 * if (wait_eeprom_cmd_done(Adapter ) == FALSE)
207 /* write the new word to the EEPROM */
209 /* send the write opcode the EEPORM */
210 shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
212 /* select which word in the EEPROM that we are writing to. */
213 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
215 /* write the data to the selected EEPROM word. */
216 shift_out_bits(padapter, data, 16);
218 if (wait_eeprom_cmd_done(padapter) == _FALSE)
224 shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
225 shift_out_bits(padapter, reg, 4);
227 eeprom_clean(padapter);
229 #ifdef CONFIG_RTL8712
230 if (tmp8_clk_new != tmp8_clk_ori)
231 rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
232 if (tmp8_new != tmp8_ori)
233 rtw_write8(padapter, 0x102502f1, tmp8_ori);
239 u16 eeprom_read16(_adapter *padapter, u16 reg) /* ReadEEprom */
244 #ifdef CONFIG_RTL8712
245 u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
246 tmp8_ori = rtw_read8(padapter, 0x102502f1);
247 tmp8_new = tmp8_ori & 0xf7;
248 if (tmp8_ori != tmp8_new) {
249 rtw_write8(padapter, 0x102502f1, tmp8_new);
251 tmp8_clk_ori = rtw_read8(padapter, 0x10250003);
252 tmp8_clk_new = tmp8_clk_ori | 0x20;
253 if (tmp8_clk_new != tmp8_clk_ori) {
254 rtw_write8(padapter, 0x10250003, tmp8_clk_new);
258 if (rtw_is_surprise_removed(padapter)) {
261 /* select EEPROM, reset bits, set _EECS */
262 x = rtw_read8(padapter, EE_9346CR);
264 if (rtw_is_surprise_removed(padapter)) {
268 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
270 rtw_write8(padapter, EE_9346CR, (unsigned char)x);
272 /* write the read opcode and register number in that order */
273 /* The opcode is 3bits in length, reg is 6 bits long */
274 shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
275 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
277 /* Now read the data (16 bits) in from the selected EEPROM word */
278 data = shift_in_bits(padapter);
280 eeprom_clean(padapter);
282 #ifdef CONFIG_RTL8712
283 if (tmp8_clk_new != tmp8_clk_ori)
284 rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
285 if (tmp8_new != tmp8_ori)
286 rtw_write8(padapter, 0x102502f1, tmp8_ori);
297 /* From even offset */
298 void eeprom_read_sz(_adapter *padapter, u16 reg, u8 *data, u32 sz)
303 if (rtw_is_surprise_removed(padapter)) {
306 /* select EEPROM, reset bits, set _EECS */
307 x = rtw_read8(padapter, EE_9346CR);
309 if (rtw_is_surprise_removed(padapter)) {
313 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
315 rtw_write8(padapter, EE_9346CR, (unsigned char)x);
317 /* write the read opcode and register number in that order */
318 /* The opcode is 3bits in length, reg is 6 bits long */
319 shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
320 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
323 for (i = 0; i < sz; i += 2) {
324 data16 = shift_in_bits(padapter);
325 data[i] = data16 & 0xff;
326 data[i + 1] = data16 >> 8;
329 eeprom_clean(padapter);
335 /* addr_off : address offset of the entry in eeprom (not the tuple number of eeprom (reg); that is addr_off !=reg) */
336 u8 eeprom_read(_adapter *padapter, u32 addr_off, u8 sz, u8 *rbuf)
338 u8 quotient, remainder, addr_2align_odd;
339 u16 reg, stmp , i = 0, idx = 0;
340 reg = (u16)(addr_off >> 1);
341 addr_2align_odd = (u8)(addr_off & 0x1);
343 if (addr_2align_odd) { /* read that start at high part: e.g 1,3,5,7,9,... */
344 stmp = eeprom_read16(padapter, reg);
345 rbuf[idx++] = (u8)((stmp >> 8) & 0xff); /* return hogh-part of the short */
351 remainder = sz & 0x1;
353 for (i = 0 ; i < quotient; i++) {
354 stmp = eeprom_read16(padapter, reg + i);
355 rbuf[idx++] = (u8)(stmp & 0xff);
356 rbuf[idx++] = (u8)((stmp >> 8) & 0xff);
360 if (remainder) { /* end of read at lower part of short : 0,2,4,6,... */
361 stmp = eeprom_read16(padapter, reg);
362 rbuf[idx] = (u8)(stmp & 0xff);
369 VOID read_eeprom_content(_adapter *padapter)