OSDN Git Service

Add rtl8723bu driver version 4.4.5
[android-x86/external-kernel-drivers.git] / rtl8723bu / core / rtw_eeprom.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
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.
8  *
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
12  * more details.
13  *
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
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_EEPROM_C_
21
22 #include <drv_conf.h>
23 #include <osdep_service.h>
24 #include <drv_types.h>
25
26 void up_clk(_adapter*   padapter,        u16 *x)
27 {
28 _func_enter_;
29         *x = *x | _EESK;
30         rtw_write8(padapter, EE_9346CR, (u8)*x);
31         rtw_udelay_os(CLOCK_RATE);
32
33 _func_exit_;
34
35 }
36
37 void down_clk(_adapter *        padapter, u16 *x        )
38 {
39 _func_enter_;
40         *x = *x & ~_EESK;
41         rtw_write8(padapter, EE_9346CR, (u8)*x);
42         rtw_udelay_os(CLOCK_RATE);
43 _func_exit_;
44 }
45
46 void shift_out_bits(_adapter * padapter, u16 data, u16 count)
47 {
48         u16 x,mask;
49 _func_enter_;
50
51         if (rtw_is_surprise_removed(padapter)) {
52                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
53                 goto out;
54         }
55         mask = 0x01 << (count - 1);
56         x = rtw_read8(padapter, EE_9346CR);
57
58         x &= ~(_EEDO | _EEDI);
59
60         do
61         {
62                 x &= ~_EEDI;
63                 if(data & mask)
64                         x |= _EEDI;
65                 if (rtw_is_surprise_removed(padapter)) {
66                         RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
67                         goto out;
68                 }
69                 rtw_write8(padapter, EE_9346CR, (u8)x);
70                 rtw_udelay_os(CLOCK_RATE);
71                 up_clk(padapter, &x);
72                 down_clk(padapter, &x);
73                 mask = mask >> 1;
74         } while(mask);
75         if (rtw_is_surprise_removed(padapter)) {
76                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
77                 goto out;
78         }
79         x &= ~_EEDI;
80         rtw_write8(padapter, EE_9346CR, (u8)x);
81 out:
82 _func_exit_;
83 }
84
85 u16 shift_in_bits (_adapter * padapter)
86 {
87         u16 x,d=0,i;
88 _func_enter_;
89         if (rtw_is_surprise_removed(padapter)) {
90                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
91                 goto out;
92         }
93         x = rtw_read8(padapter, EE_9346CR);
94
95         x &= ~( _EEDO | _EEDI);
96         d = 0;
97
98         for(i=0; i<16; i++)
99         {
100                 d = d << 1;
101                 up_clk(padapter, &x);
102         if (rtw_is_surprise_removed(padapter)) {
103                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
104                 goto out;
105         }
106                 x = rtw_read8(padapter, EE_9346CR);
107
108                 x &= ~(_EEDI);
109                 if(x & _EEDO)
110                 d |= 1;
111
112                 down_clk(padapter, &x);
113         }
114 out:
115 _func_exit_;
116
117         return d;
118 }
119
120 void standby(_adapter * padapter        )
121 {
122         u8   x;
123 _func_enter_;
124         x = rtw_read8(padapter, EE_9346CR);
125
126         x &= ~(_EECS | _EESK);
127         rtw_write8(padapter, EE_9346CR,x);
128
129         rtw_udelay_os(CLOCK_RATE);
130         x |= _EECS;
131         rtw_write8(padapter, EE_9346CR, x);
132         rtw_udelay_os(CLOCK_RATE);
133 _func_exit_;
134 }
135
136 u16 wait_eeprom_cmd_done(_adapter* padapter)
137 {
138         u8      x;
139         u16     i,res=_FALSE;
140 _func_enter_;
141         standby(padapter );
142         for (i=0; i<200; i++)
143         {
144                 x = rtw_read8(padapter, EE_9346CR);
145                 if (x & _EEDO){
146                         res=_TRUE;
147                         goto exit;
148                         }
149                 rtw_udelay_os(CLOCK_RATE);
150         }
151 exit:
152 _func_exit_;
153         return res;
154 }
155
156 void eeprom_clean(_adapter * padapter)
157 {
158         u16 x;
159 _func_enter_;
160         if (rtw_is_surprise_removed(padapter)) {
161                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
162                 goto out;
163         }
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"));
167                 goto out;
168         }
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"));
173                 goto out;
174         }
175         up_clk(padapter, &x);
176         if (rtw_is_surprise_removed(padapter)) {
177                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
178                 goto out;
179         }
180         down_clk(padapter, &x);
181 out:
182 _func_exit_;
183 }
184
185 void eeprom_write16(_adapter * padapter, u16 reg, u16 data)
186 {
187         u8 x;
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"));
195         }
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);
201         }
202 #endif
203 _func_enter_;
204
205         x = rtw_read8(padapter, EE_9346CR);
206
207         x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
208         x |= _EEM1 | _EECS;
209         rtw_write8(padapter, EE_9346CR, x);
210
211         shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
212
213         if(padapter->EepromAddressSize==8)      //CF+ and SDIO
214                 shift_out_bits(padapter, 0, 6);
215         else                                                                    //USB
216                 shift_out_bits(padapter, 0, 4);
217
218         standby( padapter);
219
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);
225 //
226 //      if (wait_eeprom_cmd_done(Adapter ) == FALSE)
227 //      {
228 //              return;
229 //      }
230
231
232         standby(padapter );
233
234         // write the new word to the EEPROM
235
236         // send the write opcode the EEPORM
237         shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
238
239         // select which word in the EEPROM that we are writing to.
240         shift_out_bits(padapter, reg, padapter->EepromAddressSize);
241
242         // write the data to the selected EEPROM word.
243         shift_out_bits(padapter, data, 16);
244
245         if (wait_eeprom_cmd_done(padapter ) == _FALSE)
246         {
247
248                 goto exit;
249         }
250
251         standby(padapter );
252
253         shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
254         shift_out_bits(padapter, reg, 4);
255
256         eeprom_clean(padapter );
257 exit:
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);
263
264 #endif
265 _func_exit_;
266         return;
267 }
268
269 u16 eeprom_read16(_adapter * padapter, u16 reg) //ReadEEprom
270 {
271
272         u16 x;
273         u16 data=0;
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"));
281         }
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);
287         }
288 #endif
289 _func_enter_;
290
291         if (rtw_is_surprise_removed(padapter)) {
292                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
293                 goto out;
294         }
295         // select EEPROM, reset bits, set _EECS
296         x = rtw_read8(padapter, EE_9346CR);
297
298         if (rtw_is_surprise_removed(padapter)) {
299                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
300                 goto out;
301         }
302
303         x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
304         x |= _EEM1 | _EECS;
305         rtw_write8(padapter, EE_9346CR, (unsigned char)x);
306
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);
311
312         // Now read the data (16 bits) in from the selected EEPROM word
313         data = shift_in_bits(padapter);
314
315         eeprom_clean(padapter);
316 out:
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);
322
323 #endif
324 _func_exit_;
325         return data;
326
327
328 }
329
330
331
332
333 //From even offset
334 void eeprom_read_sz(_adapter * padapter, u16 reg, u8* data, u32 sz)
335 {
336
337         u16 x, data16;
338         u32 i;
339 _func_enter_;
340         if (rtw_is_surprise_removed(padapter)) {
341                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
342                 goto out;
343         }
344         // select EEPROM, reset bits, set _EECS
345         x = rtw_read8(padapter, EE_9346CR);
346
347         if (rtw_is_surprise_removed(padapter)) {
348                 RT_TRACE(_module_rtl871x_eeprom_c_, _drv_err_, ("padapter->bSurpriseRemoved==_TRUE"));
349                 goto out;
350         }
351
352         x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
353         x |= _EEM1 | _EECS;
354         rtw_write8(padapter, EE_9346CR, (unsigned char)x);
355
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);
360
361
362         for(i=0; i<sz; i+=2)
363         {
364                 data16 = shift_in_bits(padapter);
365                 data[i] = data16 & 0xff;
366                 data[i+1] = data16 >>8;
367         }
368
369         eeprom_clean(padapter);
370 out:
371 _func_exit_;
372
373
374
375 }
376
377
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)
380 {
381         u8 quotient, remainder, addr_2align_odd;
382         u16 reg, stmp , i=0, idx = 0;
383 _func_enter_;
384         reg = (u16)(addr_off >> 1);
385         addr_2align_odd = (u8)(addr_off & 0x1);
386
387         if(addr_2align_odd) //read that start at high part: e.g  1,3,5,7,9,...
388         {
389                 stmp = eeprom_read16(padapter, reg);
390                 rbuf[idx++] = (u8) ((stmp>>8)&0xff); //return hogh-part of the short
391                 reg++; sz--;
392         }
393
394         quotient = sz >> 1;
395         remainder = sz & 0x1;
396
397         for( i=0 ; i < quotient; i++)
398         {
399                 stmp = eeprom_read16(padapter, reg+i);
400                 rbuf[idx++] = (u8) (stmp&0xff);
401                 rbuf[idx++] = (u8) ((stmp>>8)&0xff);
402         }
403
404         reg = reg+i;
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);
408         }
409 _func_exit_;
410         return _TRUE;
411 }
412
413
414
415 VOID read_eeprom_content(_adapter *     padapter)
416 {
417
418 _func_enter_;
419
420
421 _func_exit_;
422 }