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