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