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:
24 * WLAN FAE <wlanfae@realtek.com>.
25 * Larry Finger <Larry.Finger@lwfinger.net>
27 ******************************************************************************/
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "rtl871x_byteorder.h"
34 #include "usb_osintf.h"
36 #define FWBUFF_ALIGN_SZ 512
37 #define MAX_DUMP_FWSZ 49152 /*default = 49152 (48k)*/
39 static u32
rtl871x_open_fw(struct _adapter
*padapter
, void **pphfwfile_hdl
,
40 const u8
**ppmappedfw
)
43 const char firmware_file
[] = "rtlwifi/rtl8712u.bin";
44 const struct firmware
**praw
= (const struct firmware
**)
46 struct dvobj_priv
*pdvobjpriv
= (struct dvobj_priv
*)
47 (&padapter
->dvobjpriv
);
48 struct usb_device
*pusbdev
= pdvobjpriv
->pusbdev
;
50 printk(KERN_INFO
"r8712u: Loading firmware from \"%s\"\n",
52 rc
= request_firmware(praw
, firmware_file
, &pusbdev
->dev
);
54 printk(KERN_ERR
"r8712u: Unable to load firmware\n");
55 printk(KERN_ERR
"r8712u: Install latest linux-firmware\n");
58 *ppmappedfw
= (u8
*)((*praw
)->data
);
61 MODULE_FIRMWARE("rtlwifi/rtl8712u.bin");
63 static void fill_fwpriv(struct _adapter
*padapter
, struct fw_priv
*pfwpriv
)
65 struct dvobj_priv
*pdvobj
= (struct dvobj_priv
*)&padapter
->dvobjpriv
;
66 struct registry_priv
*pregpriv
= &padapter
->registrypriv
;
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
) {
75 pfwpriv
->rf_config
= RTL8712_RFC_1T1R
;
78 pfwpriv
->rf_config
= RTL8712_RFC_2T2R
;
82 pfwpriv
->rf_config
= RTL8712_RFC_1T2R
;
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
;
92 static void update_fwhdr(struct fw_hdr
*pfwhdr
, const u8
*pmappedfw
)
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));
106 static u8
chk_fwhdr(struct fw_hdr
*pfwhdr
, u32 ulfilelength
)
111 /* check signature */
112 if ((pfwhdr
->signature
!= 0x8712) && (pfwhdr
->signature
!= 0x8192))
114 /* check interface */
115 intf
= (u8
)((pfwhdr
->version
&0x3000) >> 12);
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
))
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
+
125 if (fw_sz
!= ulfilelength
)
130 static u8
rtl8712_dl_fw(struct _adapter
*padapter
)
135 u32 maxlen
= 0, tmp32
; /* for compare usage */
136 uint dump_imem_sz
, imem_sz
, dump_emem_sz
, emem_sz
; /* max = 49152; */
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
);
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
)
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
;
156 ptmpchar
= _malloc(maxlen
+ FWBUFF_ALIGN_SZ
);
157 if (ptmpchar
== NULL
)
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
) +
165 /* Download FirmWare */
166 /* 1. determine IMEM code size and Load IMEM Code Section */
167 imem_sz
= fwhdr
.img_IMEM_size
;
169 memset(ptx_desc
, 0, TXDESC_SIZE
);
170 if (imem_sz
> MAX_DUMP_FWSZ
/*49152*/)
171 dump_imem_sz
= MAX_DUMP_FWSZ
;
173 dump_imem_sz
= imem_sz
;
174 ptx_desc
->txdw0
|= cpu_to_le32(BIT(28));
176 ptx_desc
->txdw0
|= cpu_to_le32(dump_imem_sz
&
178 memcpy(ppayload
, ptr
, dump_imem_sz
);
179 r8712_write_mem(padapter
, RTL8712_DMA_VOQ
,
180 dump_imem_sz
+ TXDESC_SIZE
,
183 imem_sz
-= dump_imem_sz
;
184 } while (imem_sz
> 0);
186 tmp16
= r8712_read16(padapter
, TCR
);
187 while (((tmp16
& _IMEM_CODE_DONE
) == 0) && (i
> 0)) {
189 tmp16
= r8712_read16(padapter
, TCR
);
192 if (i
== 0 || (tmp16
& _IMEM_CHK_RPT
) == 0)
195 /* 2.Download EMEM code size and Load EMEM Code Section */
196 emem_sz
= fwhdr
.img_SRAM_size
;
198 memset(ptx_desc
, 0, TXDESC_SIZE
);
199 if (emem_sz
> MAX_DUMP_FWSZ
) /* max=48k */
200 dump_emem_sz
= MAX_DUMP_FWSZ
;
202 dump_emem_sz
= emem_sz
;
203 ptx_desc
->txdw0
|= cpu_to_le32(BIT(28));
205 ptx_desc
->txdw0
|= cpu_to_le32(dump_emem_sz
&
207 memcpy(ppayload
, ptr
, dump_emem_sz
);
208 r8712_write_mem(padapter
, RTL8712_DMA_VOQ
,
209 dump_emem_sz
+TXDESC_SIZE
, (u8
*)ptx_desc
);
211 emem_sz
-= dump_emem_sz
;
212 } while (emem_sz
> 0);
214 tmp16
= r8712_read16(padapter
, TCR
);
215 while (((tmp16
& _EMEM_CODE_DONE
) == 0) && (i
> 0)) {
217 tmp16
= r8712_read16(padapter
, TCR
);
220 if (i
== 0 || (tmp16
& _EMEM_CHK_RPT
) == 0)
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)))
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)))
236 tmp32
= r8712_read32(padapter
, TCR
);
238 /* 4.polling IMEM Ready */
240 tmp16
= r8712_read16(padapter
, TCR
);
241 while (((tmp16
& _IMEM_RDY
) == 0) && (i
> 0)) {
243 tmp16
= r8712_read16(padapter
, TCR
);
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);
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
);
263 /* polling dmem code done */
265 tmp16
= r8712_read16(padapter
, TCR
);
266 while (((tmp16
& _DMEM_CODE_DONE
) == 0) && (i
> 0)) {
268 tmp16
= r8712_read16(padapter
, TCR
);
274 tmp8
= r8712_read8(padapter
, 0x1025000A);
275 if (tmp8
& BIT(4)) /* When boot from EEPROM,
276 & FW need more time to read EEPROM */
278 else /* boot from EFUSE */
280 tmp16
= r8712_read16(padapter
, TCR
);
281 while (((tmp16
& _FWRDY
) == 0) && (i
> 0)) {
283 tmp16
= r8712_read16(padapter
, TCR
);
295 release_firmware((struct firmware
*)phfwfile_hdl
);
299 uint
rtl8712_hal_init(struct _adapter
*padapter
)
304 /* r8712 firmware download */
305 if (rtl8712_dl_fw(padapter
) != _SUCCESS
)
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
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 */
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)
329 for (i
= 0; i
< 6; i
++)
330 padapter
->eeprompriv
.mac_addr
[i
] = r8712_read8(padapter
,
335 uint
rtl8712_hal_deinit(struct _adapter
*padapter
)
337 r8712_write8(padapter
, RF_CTRL
, 0x00);
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
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 */
358 uint
rtl871x_hal_init(struct _adapter
*padapter
)
360 padapter
->hw_init_completed
= false;
361 if (padapter
->halpriv
.hal_bus_init
== NULL
)
364 if (padapter
->halpriv
.hal_bus_init(padapter
) != _SUCCESS
)
367 if (rtl8712_hal_init(padapter
) == _SUCCESS
)
368 padapter
->hw_init_completed
= true;
370 padapter
->hw_init_completed
= false;