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