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