1 /******************************************************************************
2 * hal_init.c
3 *
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
6 *
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.
10 *
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
14 * more details.
15 *
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
19 *
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
22 *
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>.
25 * Larry Finger <Larry.Finger@lwfinger.net>
26 *
27 ******************************************************************************/
28
29 #define _HAL_INIT_C_
30
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "rtl871x_byteorder.h"
34 #include "usb_osintf.h"
35
36 #define FWBUFF_ALIGN_SZ 512
37 #define MAX_DUMP_FWSZ 49152 /*default = 49152 (48k)*/
38
rtl871x_open_fw(struct _adapter * padapter,void ** pphfwfile_hdl,const u8 ** ppmappedfw)39 static u32 rtl871x_open_fw(struct _adapter *padapter, void **pphfwfile_hdl,
40 const u8 **ppmappedfw)
41 {
42 int rc;
43 const char firmware_file[] = "rtlwifi/rtl8712u.bin";
44 const struct firmware **praw = (const struct firmware **)
45 (pphfwfile_hdl);
46 struct dvobj_priv *pdvobjpriv = (struct dvobj_priv *)
47 (&padapter->dvobjpriv);
48 struct usb_device *pusbdev = pdvobjpriv->pusbdev;
49
50 printk(KERN_INFO "r8712u: Loading firmware from \"%s\"\n",
51 firmware_file);
52 rc = request_firmware(praw, firmware_file, &pusbdev->dev);
53 if (rc < 0) {
54 printk(KERN_ERR "r8712u: Unable to load firmware\n");
55 printk(KERN_ERR "r8712u: Install latest linux-firmware\n");
56 return 0;
57 }
58 *ppmappedfw = (u8 *)((*praw)->data);
59 return (*praw)->size;
60 }
61 MODULE_FIRMWARE("rtlwifi/rtl8712u.bin");
62
fill_fwpriv(struct _adapter * padapter,struct fw_priv * pfwpriv)63 static void fill_fwpriv(struct _adapter *padapter, struct fw_priv *pfwpriv)
64 {
65 struct dvobj_priv *pdvobj = (struct dvobj_priv *)&padapter->dvobjpriv;
66 struct registry_priv *pregpriv = &padapter->registrypriv;
67
68 memset(pfwpriv, 0, sizeof(struct fw_priv));
69 /* todo: check if needs endian conversion */
70 pfwpriv->hci_sel = RTL8712_HCI_TYPE_72USB;
71 pfwpriv->usb_ep_num = (u8)pdvobj->nr_endpoint;
72 pfwpriv->bw_40MHz_en = pregpriv->cbw40_enable;
73 switch (pregpriv->rf_config) {
74 case RTL8712_RF_1T1R:
75 pfwpriv->rf_config = RTL8712_RFC_1T1R;
76 break;
77 case RTL8712_RF_2T2R:
78 pfwpriv->rf_config = RTL8712_RFC_2T2R;
79 break;
80 case RTL8712_RF_1T2R:
81 default:
82 pfwpriv->rf_config = RTL8712_RFC_1T2R;
83 }
84 pfwpriv->mp_mode = (pregpriv->mp_mode == 1) ? 1 : 0;
85 pfwpriv->vcsType = pregpriv->vrtl_carrier_sense; /* 0:off 1:on 2:auto */
86 pfwpriv->vcsMode = pregpriv->vcs_type; /* 1:RTS/CTS 2:CTS to self */
87 /* default enable turboMode */
88 pfwpriv->turboMode = ((pregpriv->wifi_test == 1) ? 0 : 1);
89 pfwpriv->lowPowerMode = pregpriv->low_power;
90 }
91
update_fwhdr(struct fw_hdr * pfwhdr,const u8 * pmappedfw)92 static void update_fwhdr(struct fw_hdr *pfwhdr, const u8 *pmappedfw)
93 {
94 pfwhdr->signature = le16_to_cpu(*(u16 *)pmappedfw);
95 pfwhdr->version = le16_to_cpu(*(u16 *)(pmappedfw+2));
96 /* define the size of boot loader */
97 pfwhdr->dmem_size = le32_to_cpu(*(uint *)(pmappedfw+4));
98 /* define the size of FW in IMEM */
99 pfwhdr->img_IMEM_size = le32_to_cpu(*(uint *)(pmappedfw+8));
100 /* define the size of FW in SRAM */
101 pfwhdr->img_SRAM_size = le32_to_cpu(*(uint *)(pmappedfw+12));
102 /* define the size of DMEM variable */
103 pfwhdr->fw_priv_sz = le32_to_cpu(*(uint *)(pmappedfw+16));
104 }
105
chk_fwhdr(struct fw_hdr * pfwhdr,u32 ulfilelength)106 static u8 chk_fwhdr(struct fw_hdr *pfwhdr, u32 ulfilelength)
107 {
108 u32 fwhdrsz, fw_sz;
109 u8 intf, rfconf;
110
111 /* check signature */
112 if ((pfwhdr->signature != 0x8712) && (pfwhdr->signature != 0x8192))
113 return _FAIL;
114 /* check interface */
115 intf = (u8)((pfwhdr->version&0x3000) >> 12);
116 /* check rf_conf */
117 rfconf = (u8)((pfwhdr->version&0xC000) >> 14);
118 /* check fw_priv_sze & sizeof(struct fw_priv) */
119 if (pfwhdr->fw_priv_sz != sizeof(struct fw_priv))
120 return _FAIL;
121 /* check fw_sz & image_fw_sz */
122 fwhdrsz = FIELD_OFFSET(struct fw_hdr, fwpriv) + pfwhdr->fw_priv_sz;
123 fw_sz = fwhdrsz + pfwhdr->img_IMEM_size + pfwhdr->img_SRAM_size +
124 pfwhdr->dmem_size;
125 if (fw_sz != ulfilelength)
126 return _FAIL;
127 return _SUCCESS;
128 }
129
rtl8712_dl_fw(struct _adapter * padapter)130 static u8 rtl8712_dl_fw(struct _adapter *padapter)
131 {
132 sint i;
133 u8 tmp8, tmp8_a;
134 u16 tmp16;
135 u32 maxlen = 0, tmp32; /* for compare usage */
136 uint dump_imem_sz, imem_sz, dump_emem_sz, emem_sz; /* max = 49152; */
137 struct fw_hdr fwhdr;
138 u32 ulfilelength; /* FW file size */
139 void *phfwfile_hdl = NULL;
140 const u8 *pmappedfw = NULL;
141 u8 *ptmpchar = NULL, *ppayload, *ptr;
142 struct tx_desc *ptx_desc;
143 u32 txdscp_sz = sizeof(struct tx_desc);
144 u8 ret = _FAIL;
145
146 ulfilelength = rtl871x_open_fw(padapter, &phfwfile_hdl, &pmappedfw);
147 if (pmappedfw && (ulfilelength > 0)) {
148 update_fwhdr(&fwhdr, pmappedfw);
149 if (chk_fwhdr(&fwhdr, ulfilelength) == _FAIL)
150 goto firmware_rel;
151 fill_fwpriv(padapter, &fwhdr.fwpriv);
152 /* firmware check ok */
153 maxlen = (fwhdr.img_IMEM_size > fwhdr.img_SRAM_size) ?
154 fwhdr.img_IMEM_size : fwhdr.img_SRAM_size;
155 maxlen += txdscp_sz;
156 ptmpchar = _malloc(maxlen + FWBUFF_ALIGN_SZ);
157 if (ptmpchar == NULL)
158 goto firmware_rel;
159
160 ptx_desc = (struct tx_desc *)(ptmpchar + FWBUFF_ALIGN_SZ -
161 ((addr_t)(ptmpchar) & (FWBUFF_ALIGN_SZ - 1)));
162 ppayload = (u8 *)(ptx_desc) + txdscp_sz;
163 ptr = (u8 *)pmappedfw + FIELD_OFFSET(struct fw_hdr, fwpriv) +
164 fwhdr.fw_priv_sz;
165 /* Download FirmWare */
166 /* 1. determine IMEM code size and Load IMEM Code Section */
167 imem_sz = fwhdr.img_IMEM_size;
168 do {
169 memset(ptx_desc, 0, TXDESC_SIZE);
170 if (imem_sz > MAX_DUMP_FWSZ/*49152*/)
171 dump_imem_sz = MAX_DUMP_FWSZ;
172 else {
173 dump_imem_sz = imem_sz;
174 ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
175 }
176 ptx_desc->txdw0 |= cpu_to_le32(dump_imem_sz &
177 0x0000ffff);
178 memcpy(ppayload, ptr, dump_imem_sz);
179 r8712_write_mem(padapter, RTL8712_DMA_VOQ,
180 dump_imem_sz + TXDESC_SIZE,
181 (u8 *)ptx_desc);
182 ptr += dump_imem_sz;
183 imem_sz -= dump_imem_sz;
184 } while (imem_sz > 0);
185 i = 10;
186 tmp16 = r8712_read16(padapter, TCR);
187 while (((tmp16 & _IMEM_CODE_DONE) == 0) && (i > 0)) {
188 udelay(10);
189 tmp16 = r8712_read16(padapter, TCR);
190 i--;
191 }
192 if (i == 0 || (tmp16 & _IMEM_CHK_RPT) == 0)
193 goto exit_fail;
194
195 /* 2.Download EMEM code size and Load EMEM Code Section */
196 emem_sz = fwhdr.img_SRAM_size;
197 do {
198 memset(ptx_desc, 0, TXDESC_SIZE);
199 if (emem_sz > MAX_DUMP_FWSZ) /* max=48k */
200 dump_emem_sz = MAX_DUMP_FWSZ;
201 else {
202 dump_emem_sz = emem_sz;
203 ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
204 }
205 ptx_desc->txdw0 |= cpu_to_le32(dump_emem_sz &
206 0x0000ffff);
207 memcpy(ppayload, ptr, dump_emem_sz);
208 r8712_write_mem(padapter, RTL8712_DMA_VOQ,
209 dump_emem_sz+TXDESC_SIZE, (u8 *)ptx_desc);
210 ptr += dump_emem_sz;
211 emem_sz -= dump_emem_sz;
212 } while (emem_sz > 0);
213 i = 5;
214 tmp16 = r8712_read16(padapter, TCR);
215 while (((tmp16 & _EMEM_CODE_DONE) == 0) && (i > 0)) {
216 udelay(10);
217 tmp16 = r8712_read16(padapter, TCR);
218 i--;
219 }
220 if (i == 0 || (tmp16 & _EMEM_CHK_RPT) == 0)
221 goto exit_fail;
222
223 /* 3.Enable CPU */
224 tmp8 = r8712_read8(padapter, SYS_CLKR);
225 r8712_write8(padapter, SYS_CLKR, tmp8|BIT(2));
226 tmp8_a = r8712_read8(padapter, SYS_CLKR);
227 if (tmp8_a != (tmp8|BIT(2)))
228 goto exit_fail;
229
230 tmp8 = r8712_read8(padapter, SYS_FUNC_EN + 1);
231 r8712_write8(padapter, SYS_FUNC_EN+1, tmp8|BIT(2));
232 tmp8_a = r8712_read8(padapter, SYS_FUNC_EN + 1);
233 if (tmp8_a != (tmp8|BIT(2)))
234 goto exit_fail;
235
236 tmp32 = r8712_read32(padapter, TCR);
237
238 /* 4.polling IMEM Ready */
239 i = 100;
240 tmp16 = r8712_read16(padapter, TCR);
241 while (((tmp16 & _IMEM_RDY) == 0) && (i > 0)) {
242 msleep(20);
243 tmp16 = r8712_read16(padapter, TCR);
244 i--;
245 }
246 if (i == 0) {
247 r8712_write16(padapter, 0x10250348, 0xc000);
248 r8712_write16(padapter, 0x10250348, 0xc001);
249 r8712_write16(padapter, 0x10250348, 0x2000);
250 r8712_write16(padapter, 0x10250348, 0x2001);
251 r8712_write16(padapter, 0x10250348, 0x2002);
252 r8712_write16(padapter, 0x10250348, 0x2003);
253 goto exit_fail;
254 }
255 /* 5.Download DMEM code size and Load EMEM Code Section */
256 memset(ptx_desc, 0, TXDESC_SIZE);
257 ptx_desc->txdw0 |= cpu_to_le32(fwhdr.fw_priv_sz&0x0000ffff);
258 ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
259 memcpy(ppayload, &fwhdr.fwpriv, fwhdr.fw_priv_sz);
260 r8712_write_mem(padapter, RTL8712_DMA_VOQ,
261 fwhdr.fw_priv_sz + TXDESC_SIZE, (u8 *)ptx_desc);
262
263 /* polling dmem code done */
264 i = 100;
265 tmp16 = r8712_read16(padapter, TCR);
266 while (((tmp16 & _DMEM_CODE_DONE) == 0) && (i > 0)) {
267 msleep(20);
268 tmp16 = r8712_read16(padapter, TCR);
269 i--;
270 }
271 if (i == 0)
272 goto exit_fail;
273
274 tmp8 = r8712_read8(padapter, 0x1025000A);
275 if (tmp8 & BIT(4)) /* When boot from EEPROM,
276 & FW need more time to read EEPROM */
277 i = 60;
278 else /* boot from EFUSE */
279 i = 30;
280 tmp16 = r8712_read16(padapter, TCR);
281 while (((tmp16 & _FWRDY) == 0) && (i > 0)) {
282 msleep(100);
283 tmp16 = r8712_read16(padapter, TCR);
284 i--;
285 }
286 if (i == 0)
287 goto exit_fail;
288 } else
289 goto exit_fail;
290 ret = _SUCCESS;
291
292 exit_fail:
293 kfree(ptmpchar);
294 firmware_rel:
295 release_firmware((struct firmware *)phfwfile_hdl);
296 return ret;
297 }
298
rtl8712_hal_init(struct _adapter * padapter)299 uint rtl8712_hal_init(struct _adapter *padapter)
300 {
301 u32 val32;
302 int i;
303
304 /* r8712 firmware download */
305 if (rtl8712_dl_fw(padapter) != _SUCCESS)
306 return _FAIL;
307
308 printk(KERN_INFO "r8712u: 1 RCR=0x%x\n", r8712_read32(padapter, RCR));
309 val32 = r8712_read32(padapter, RCR);
310 r8712_write32(padapter, RCR, (val32 | BIT(26))); /* Enable RX TCP
311 Checksum offload */
312 printk(KERN_INFO "r8712u: 2 RCR=0x%x\n", r8712_read32(padapter, RCR));
313 val32 = r8712_read32(padapter, RCR);
314 r8712_write32(padapter, RCR, (val32|BIT(25))); /* Append PHY status */
315 val32 = 0;
316 val32 = r8712_read32(padapter, 0x10250040);
317 r8712_write32(padapter, 0x10250040, (val32&0x00FFFFFF));
318 /* for usb rx aggregation */
319 r8712_write8(padapter, 0x102500B5, r8712_read8(padapter, 0x102500B5) |
320 BIT(0)); /* page = 128bytes */
321 r8712_write8(padapter, 0x102500BD, r8712_read8(padapter, 0x102500BD) |
322 BIT(7)); /* enable usb rx aggregation */
323 r8712_write8(padapter, 0x102500D9, 1); /* TH=1 => means that invalidate
324 * usb rx aggregation */
325 r8712_write8(padapter, 0x1025FE5B, 0x04); /* 1.7ms/4 */
326 /* Fix the RX FIFO issue(USB error) */
327 r8712_write8(padapter, 0x1025fe5C, r8712_read8(padapter, 0x1025fe5C)
328 | BIT(7));
329 for (i = 0; i < 6; i++)
330 padapter->eeprompriv.mac_addr[i] = r8712_read8(padapter,
331 MACID + i);
332 return _SUCCESS;
333 }
334
rtl8712_hal_deinit(struct _adapter * padapter)335 uint rtl8712_hal_deinit(struct _adapter *padapter)
336 {
337 r8712_write8(padapter, RF_CTRL, 0x00);
338 /* Turn off BB */
339 msleep(20);
340 /* Turn off MAC */
341 r8712_write8(padapter, SYS_CLKR+1, 0x38); /* Switch Control Path */
342 r8712_write8(padapter, SYS_FUNC_EN+1, 0x70);
343 r8712_write8(padapter, PMC_FSM, 0x06); /* Enable Loader Data Keep */
344 r8712_write8(padapter, SYS_ISO_CTRL, 0xF9); /* Isolation signals from
345 * CORE, PLL */
346 r8712_write8(padapter, SYS_ISO_CTRL+1, 0xe8); /* Enable EFUSE 1.2V */
347 r8712_write8(padapter, AFE_PLL_CTRL, 0x00); /* Disable AFE PLL. */
348 r8712_write8(padapter, LDOA15_CTRL, 0x54); /* Disable A15V */
349 r8712_write8(padapter, SYS_FUNC_EN+1, 0x50); /* Disable E-Fuse 1.2V */
350 r8712_write8(padapter, LDOV12D_CTRL, 0x24); /* Disable LDO12(for CE) */
351 r8712_write8(padapter, AFE_MISC, 0x30); /* Disable AFE BG&MB */
352 /* Option for Disable 1.6V LDO. */
353 r8712_write8(padapter, SPS0_CTRL, 0x56); /* Disable 1.6V LDO */
354 r8712_write8(padapter, SPS0_CTRL+1, 0x43); /* Set SW PFM */
355 return _SUCCESS;
356 }
357
rtl871x_hal_init(struct _adapter * padapter)358 uint rtl871x_hal_init(struct _adapter *padapter)
359 {
360 padapter->hw_init_completed = false;
361 if (padapter->halpriv.hal_bus_init == NULL)
362 return _FAIL;
363 else {
364 if (padapter->halpriv.hal_bus_init(padapter) != _SUCCESS)
365 return _FAIL;
366 }
367 if (rtl8712_hal_init(padapter) == _SUCCESS)
368 padapter->hw_init_completed = true;
369 else {
370 padapter->hw_init_completed = false;
371 return _FAIL;
372 }
373 return _SUCCESS;
374 }
375