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)
30 rtw_write8(padapter, EE_9346CR, (u8)*x);
31 rtw_udelay_os(CLOCK_RATE);
37 void down_clk(_adapter * padapter, u16 *x )
41 rtw_write8(padapter, EE_9346CR, (u8)*x);
42 rtw_udelay_os(CLOCK_RATE);
46 void shift_out_bits(_adapter * padapter, u16 data, u16 count)
51 if (rtw_is_surprise_removed(padapter)) {
52 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
55 mask = 0x01 << (count - 1);
56 x = rtw_read8(padapter, EE_9346CR);
58 x &= ~(_EEDO | _EEDI);
65 if (rtw_is_surprise_removed(padapter)) {
66 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
69 rtw_write8(padapter, EE_9346CR, (u8)x);
70 rtw_udelay_os(CLOCK_RATE);
72 down_clk(padapter, &x);
75 if (rtw_is_surprise_removed(padapter)) {
76 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
80 rtw_write8(padapter, EE_9346CR, (u8)x);
85 u16 shift_in_bits (_adapter * padapter)
89 if (rtw_is_surprise_removed(padapter)) {
90 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
93 x = rtw_read8(padapter, EE_9346CR);
95 x &= ~( _EEDO | _EEDI);
101 up_clk(padapter, &x);
102 if (rtw_is_surprise_removed(padapter)) {
103 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
106 x = rtw_read8(padapter, EE_9346CR);
112 down_clk(padapter, &x);
120 void standby(_adapter * padapter )
124 x = rtw_read8(padapter, EE_9346CR);
126 x &= ~(_EECS | _EESK);
127 rtw_write8(padapter, EE_9346CR,x);
129 rtw_udelay_os(CLOCK_RATE);
131 rtw_write8(padapter, EE_9346CR, x);
132 rtw_udelay_os(CLOCK_RATE);
136 u16 wait_eeprom_cmd_done(_adapter* padapter)
142 for (i=0; i<200; i++)
144 x = rtw_read8(padapter, EE_9346CR);
149 rtw_udelay_os(CLOCK_RATE);
156 void eeprom_clean(_adapter * padapter)
160 if (rtw_is_surprise_removed(padapter)) {
161 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
164 x = rtw_read8(padapter, EE_9346CR);
165 if (rtw_is_surprise_removed(padapter)) {
166 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
169 x &= ~(_EECS | _EEDI);
170 rtw_write8(padapter, EE_9346CR, (u8)x);
171 if (rtw_is_surprise_removed(padapter)) {
172 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
175 up_clk(padapter, &x);
176 if (rtw_is_surprise_removed(padapter)) {
177 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
180 down_clk(padapter, &x);
185 void eeprom_write16(_adapter * padapter, u16 reg, u16 data)
188 #ifdef CONFIG_RTL8712
189 u8 tmp8_ori,tmp8_new,tmp8_clk_ori,tmp8_clk_new;
190 tmp8_ori=rtw_read8(padapter, 0x102502f1);
191 tmp8_new=tmp8_ori & 0xf7;
192 if(tmp8_ori != tmp8_new){
193 rtw_write8(padapter, 0x102502f1, tmp8_new);
194 RT_TRACE(_module_rtl871x_mp_ioctl_c_,_drv_err_,("====write 0x102502f1=====\n"));
196 tmp8_clk_ori=rtw_read8(padapter,0x10250003);
197 tmp8_clk_new=tmp8_clk_ori|0x20;
198 if(tmp8_clk_new!=tmp8_clk_ori){
199 RT_TRACE(_module_rtl871x_mp_ioctl_c_,_drv_err_,("====write 0x10250003=====\n"));
200 rtw_write8(padapter, 0x10250003, tmp8_clk_new);
205 x = rtw_read8(padapter, EE_9346CR);
207 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
209 rtw_write8(padapter, EE_9346CR, x);
211 shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
213 if(padapter->EepromAddressSize==8) //CF+ and SDIO
214 shift_out_bits(padapter, 0, 6);
216 shift_out_bits(padapter, 0, 4);
220 // Commented out by rcnjko, 2004.0
221 // // Erase this particular word. Write the erase opcode and register
222 // // number in that order. The opcode is 3bits in length; reg is 6 bits long.
223 // shift_out_bits(Adapter, EEPROM_ERASE_OPCODE, 3);
224 // shift_out_bits(Adapter, reg, Adapter->EepromAddressSize);
226 // if (wait_eeprom_cmd_done(Adapter ) == FALSE)
234 // write the new word to the EEPROM
236 // send the write opcode the EEPORM
237 shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
239 // select which word in the EEPROM that we are writing to.
240 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
242 // write the data to the selected EEPROM word.
243 shift_out_bits(padapter, data, 16);
245 if (wait_eeprom_cmd_done(padapter ) == _FALSE)
253 shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
254 shift_out_bits(padapter, reg, 4);
256 eeprom_clean(padapter );
258 #ifdef CONFIG_RTL8712
259 if(tmp8_clk_new!=tmp8_clk_ori)
260 rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
261 if(tmp8_new!=tmp8_ori)
262 rtw_write8(padapter, 0x102502f1, tmp8_ori);
269 u16 eeprom_read16(_adapter * padapter, u16 reg) //ReadEEprom
274 #ifdef CONFIG_RTL8712
275 u8 tmp8_ori,tmp8_new,tmp8_clk_ori,tmp8_clk_new;
276 tmp8_ori= rtw_read8(padapter, 0x102502f1);
277 tmp8_new = tmp8_ori & 0xf7;
278 if(tmp8_ori != tmp8_new){
279 rtw_write8(padapter, 0x102502f1, tmp8_new);
280 RT_TRACE(_module_rtl871x_mp_ioctl_c_,_drv_err_,("====write 0x102502f1=====\n"));
282 tmp8_clk_ori=rtw_read8(padapter,0x10250003);
283 tmp8_clk_new=tmp8_clk_ori|0x20;
284 if(tmp8_clk_new!=tmp8_clk_ori){
285 RT_TRACE(_module_rtl871x_mp_ioctl_c_,_drv_err_,("====write 0x10250003=====\n"));
286 rtw_write8(padapter, 0x10250003, tmp8_clk_new);
291 if (rtw_is_surprise_removed(padapter)) {
292 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
295 // select EEPROM, reset bits, set _EECS
296 x = rtw_read8(padapter, EE_9346CR);
298 if (rtw_is_surprise_removed(padapter)) {
299 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
303 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
305 rtw_write8(padapter, EE_9346CR, (unsigned char)x);
307 // write the read opcode and register number in that order
308 // The opcode is 3bits in length, reg is 6 bits long
309 shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
310 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
312 // Now read the data (16 bits) in from the selected EEPROM word
313 data = shift_in_bits(padapter);
315 eeprom_clean(padapter);
317 #ifdef CONFIG_RTL8712
318 if(tmp8_clk_new!=tmp8_clk_ori)
319 rtw_write8(padapter, 0x10250003, tmp8_clk_ori);
320 if(tmp8_new!=tmp8_ori)
321 rtw_write8(padapter, 0x102502f1, tmp8_ori);
334 void eeprom_read_sz(_adapter * padapter, u16 reg, u8* data, u32 sz)
340 if (rtw_is_surprise_removed(padapter)) {
341 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
344 // select EEPROM, reset bits, set _EECS
345 x = rtw_read8(padapter, EE_9346CR);
347 if (rtw_is_surprise_removed(padapter)) {
348 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
352 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
354 rtw_write8(padapter, EE_9346CR, (unsigned char)x);
356 // write the read opcode and register number in that order
357 // The opcode is 3bits in length, reg is 6 bits long
358 shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
359 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
364 data16 = shift_in_bits(padapter);
365 data[i] = data16 & 0xff;
366 data[i+1] = data16 >>8;
369 eeprom_clean(padapter);
378 //addr_off : address offset of the entry in eeprom (not the tuple number of eeprom (reg); that is addr_off !=reg)
379 u8 eeprom_read(_adapter * padapter, u32 addr_off, u8 sz, u8* rbuf)
381 u8 quotient, remainder, addr_2align_odd;
382 u16 reg, stmp , i=0, idx = 0;
384 reg = (u16)(addr_off >> 1);
385 addr_2align_odd = (u8)(addr_off & 0x1);
387 if(addr_2align_odd) //read that start at high part: e.g 1,3,5,7,9,...
389 stmp = eeprom_read16(padapter, reg);
390 rbuf[idx++] = (u8) ((stmp>>8)&0xff); //return hogh-part of the short
395 remainder = sz & 0x1;
397 for( i=0 ; i < quotient; i++)
399 stmp = eeprom_read16(padapter, reg+i);
400 rbuf[idx++] = (u8) (stmp&0xff);
401 rbuf[idx++] = (u8) ((stmp>>8)&0xff);
405 if(remainder){ //end of read at lower part of short : 0,2,4,6,...
406 stmp = eeprom_read16(padapter, reg);
407 rbuf[idx] = (u8)(stmp & 0xff);
415 VOID read_eeprom_content(_adapter * padapter)