1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
27 ******************************************************************************/
29 #define _RTL871X_EEPROM_C_
31 #include "osdep_service.h"
32 #include "drv_types.h"
34 static void up_clk(struct _adapter *padapter, u16 *x)
37 r8712_write8(padapter, EE_9346CR, (u8)*x);
41 static void down_clk(struct _adapter *padapter, u16 *x)
44 r8712_write8(padapter, EE_9346CR, (u8)*x);
48 static void shift_out_bits(struct _adapter *padapter, u16 data, u16 count)
52 if (padapter->bSurpriseRemoved == true)
54 mask = 0x01 << (count - 1);
55 x = r8712_read8(padapter, EE_9346CR);
56 x &= ~(_EEDO | _EEDI);
61 if (padapter->bSurpriseRemoved == true)
63 r8712_write8(padapter, EE_9346CR, (u8)x);
66 down_clk(padapter, &x);
69 if (padapter->bSurpriseRemoved == true)
72 r8712_write8(padapter, EE_9346CR, (u8)x);
76 static u16 shift_in_bits(struct _adapter *padapter)
80 if (padapter->bSurpriseRemoved == true)
82 x = r8712_read8(padapter, EE_9346CR);
83 x &= ~(_EEDO | _EEDI);
85 for (i = 0; i < 16; i++) {
88 if (padapter->bSurpriseRemoved == true)
90 x = r8712_read8(padapter, EE_9346CR);
94 down_clk(padapter, &x);
100 static void standby(struct _adapter *padapter)
104 x = r8712_read8(padapter, EE_9346CR);
105 x &= ~(_EECS | _EESK);
106 r8712_write8(padapter, EE_9346CR, x);
109 r8712_write8(padapter, EE_9346CR, x);
113 static u16 wait_eeprom_cmd_done(struct _adapter *padapter)
119 for (i = 0; i < 200; i++) {
120 x = r8712_read8(padapter, EE_9346CR);
128 static void eeprom_clean(struct _adapter *padapter)
132 if (padapter->bSurpriseRemoved == true)
134 x = r8712_read8(padapter, EE_9346CR);
135 if (padapter->bSurpriseRemoved == true)
137 x &= ~(_EECS | _EEDI);
138 r8712_write8(padapter, EE_9346CR, (u8)x);
139 if (padapter->bSurpriseRemoved == true)
141 up_clk(padapter, &x);
142 if (padapter->bSurpriseRemoved == true)
144 down_clk(padapter, &x);
147 void r8712_eeprom_write16(struct _adapter *padapter, u16 reg, u16 data)
150 u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
152 tmp8_ori = r8712_read8(padapter, 0x102502f1);
153 tmp8_new = tmp8_ori & 0xf7;
154 if (tmp8_ori != tmp8_new)
155 r8712_write8(padapter, 0x102502f1, tmp8_new);
156 tmp8_clk_ori = r8712_read8(padapter, 0x10250003);
157 tmp8_clk_new = tmp8_clk_ori | 0x20;
158 if (tmp8_clk_new != tmp8_clk_ori)
159 r8712_write8(padapter, 0x10250003, tmp8_clk_new);
160 x = r8712_read8(padapter, EE_9346CR);
161 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
163 r8712_write8(padapter, EE_9346CR, x);
164 shift_out_bits(padapter, EEPROM_EWEN_OPCODE, 5);
165 if (padapter->EepromAddressSize == 8) /*CF+ and SDIO*/
166 shift_out_bits(padapter, 0, 6);
168 shift_out_bits(padapter, 0, 4);
170 /* Erase this particular word. Write the erase opcode and register
171 * number in that order. The opcode is 3bits in length; reg is 6
175 /* write the new word to the EEPROM
176 * send the write opcode the EEPORM
178 shift_out_bits(padapter, EEPROM_WRITE_OPCODE, 3);
179 /* select which word in the EEPROM that we are writing to. */
180 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
181 /* write the data to the selected EEPROM word. */
182 shift_out_bits(padapter, data, 16);
183 if (wait_eeprom_cmd_done(padapter)) {
185 shift_out_bits(padapter, EEPROM_EWDS_OPCODE, 5);
186 shift_out_bits(padapter, reg, 4);
187 eeprom_clean(padapter);
189 if (tmp8_clk_new != tmp8_clk_ori)
190 r8712_write8(padapter, 0x10250003, tmp8_clk_ori);
191 if (tmp8_new != tmp8_ori)
192 r8712_write8(padapter, 0x102502f1, tmp8_ori);
195 u16 r8712_eeprom_read16(struct _adapter *padapter, u16 reg) /*ReadEEprom*/
199 u8 tmp8_ori, tmp8_new, tmp8_clk_ori, tmp8_clk_new;
201 tmp8_ori = r8712_read8(padapter, 0x102502f1);
202 tmp8_new = tmp8_ori & 0xf7;
203 if (tmp8_ori != tmp8_new)
204 r8712_write8(padapter, 0x102502f1, tmp8_new);
205 tmp8_clk_ori = r8712_read8(padapter, 0x10250003);
206 tmp8_clk_new = tmp8_clk_ori | 0x20;
207 if (tmp8_clk_new != tmp8_clk_ori)
208 r8712_write8(padapter, 0x10250003, tmp8_clk_new);
209 if (padapter->bSurpriseRemoved == true)
211 /* select EEPROM, reset bits, set _EECS */
212 x = r8712_read8(padapter, EE_9346CR);
213 if (padapter->bSurpriseRemoved == true)
215 x &= ~(_EEDI | _EEDO | _EESK | _EEM0);
217 r8712_write8(padapter, EE_9346CR, (unsigned char)x);
218 /* write the read opcode and register number in that order
219 * The opcode is 3bits in length, reg is 6 bits long
221 shift_out_bits(padapter, EEPROM_READ_OPCODE, 3);
222 shift_out_bits(padapter, reg, padapter->EepromAddressSize);
223 /* Now read the data (16 bits) in from the selected EEPROM word */
224 data = shift_in_bits(padapter);
225 eeprom_clean(padapter);
227 if (tmp8_clk_new != tmp8_clk_ori)
228 r8712_write8(padapter, 0x10250003, tmp8_clk_ori);
229 if (tmp8_new != tmp8_ori)
230 r8712_write8(padapter, 0x102502f1, tmp8_ori);