• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /**************************************************************************************************
3  * Procedure:    Init boot code/firmware code/data session
4  *
5  * Description: This routine will initialize firmware. If any error occurs during the initialization
6  *		process, the routine shall terminate immediately and return fail.
7  *		NIC driver should call NdisOpenFile only from MiniportInitialize.
8  *
9  * Arguments:   The pointer of the adapter
10 
11  * Returns:
12  *        NDIS_STATUS_FAILURE - the following initialization process should be terminated
13  *        NDIS_STATUS_SUCCESS - if firmware initialization process success
14  **************************************************************************************************/
15 
16 #include "r8192U.h"
17 #include "r8192U_hw.h"
18 #include "r819xU_firmware_img.h"
19 #include "r819xU_firmware.h"
20 #include <linux/firmware.h>
21 
firmware_init_param(struct net_device * dev)22 static void firmware_init_param(struct net_device *dev)
23 {
24 	struct r8192_priv	*priv = ieee80211_priv(dev);
25 	rt_firmware		*pfirmware = priv->pFirmware;
26 
27 	pfirmware->cmdpacket_frag_threshold = GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE);
28 }
29 
30 /*
31  * segment the img and use the ptr and length to remember info on each segment
32  *
33  */
fw_download_code(struct net_device * dev,u8 * code_virtual_address,u32 buffer_len)34 static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address,
35 			     u32 buffer_len)
36 {
37 	struct r8192_priv   *priv = ieee80211_priv(dev);
38 	bool		    rt_status = true;
39 	u16		    frag_threshold;
40 	u16		    frag_length, frag_offset = 0;
41 	int		    i;
42 
43 	rt_firmware	    *pfirmware = priv->pFirmware;
44 	struct sk_buff	    *skb;
45 	unsigned char	    *seg_ptr;
46 	struct cb_desc		    *tcb_desc;
47 	u8                  bLastIniPkt;
48 	u8		    index;
49 
50 	firmware_init_param(dev);
51 	/* Fragmentation might be required */
52 	frag_threshold = pfirmware->cmdpacket_frag_threshold;
53 	do {
54 		if ((buffer_len - frag_offset) > frag_threshold) {
55 			frag_length = frag_threshold;
56 			bLastIniPkt = 0;
57 		} else {
58 			frag_length = buffer_len - frag_offset;
59 			bLastIniPkt = 1;
60 		}
61 
62 		/* Allocate skb buffer to contain firmware info and tx descriptor info
63 		 * add 4 to avoid packet appending overflow.
64 		 */
65 		skb  = dev_alloc_skb(USB_HWDESC_HEADER_LEN + frag_length + 4);
66 		if (!skb)
67 			return false;
68 		memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
69 		tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
70 		tcb_desc->queue_index = TXCMD_QUEUE;
71 		tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_INIT;
72 		tcb_desc->bLastIniPkt = bLastIniPkt;
73 
74 		skb_reserve(skb, USB_HWDESC_HEADER_LEN);
75 		seg_ptr = skb->data;
76 		/*
77 		 * Transform from little endian to big endian
78 		 * and pending  zero
79 		 */
80 		for (i = 0; i < frag_length; i += 4) {
81 			*seg_ptr++ = ((i+0) < frag_length)?code_virtual_address[i+3] : 0;
82 			*seg_ptr++ = ((i+1) < frag_length)?code_virtual_address[i+2] : 0;
83 			*seg_ptr++ = ((i+2) < frag_length)?code_virtual_address[i+1] : 0;
84 			*seg_ptr++ = ((i+3) < frag_length)?code_virtual_address[i+0] : 0;
85 		}
86 		tcb_desc->txbuf_size = (u16)i;
87 		skb_put(skb, i);
88 
89 		index = tcb_desc->queue_index;
90 		if (!priv->ieee80211->check_nic_enough_desc(dev, index) ||
91 		       (!skb_queue_empty(&priv->ieee80211->skb_waitQ[index])) ||
92 		       (priv->ieee80211->queue_stop)) {
93 			RT_TRACE(COMP_FIRMWARE, "=====================================================> tx full!\n");
94 			skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
95 		} else {
96 			priv->ieee80211->softmac_hard_start_xmit(skb, dev);
97 		}
98 
99 		code_virtual_address += frag_length;
100 		frag_offset += frag_length;
101 
102 	} while (frag_offset < buffer_len);
103 
104 	return rt_status;
105 }
106 
107 /*
108  * Procedure:	Check whether main code is download OK. If OK, turn on CPU
109  *
110  * Description:	CPU register locates in different page against general register.
111  *	    Switch to CPU register in the begin and switch back before return
112  *
113  *
114  * Arguments:   The pointer of the adapter
115  *
116  * Returns:
117  *        NDIS_STATUS_FAILURE - the following initialization process should
118  *				be terminated
119  *        NDIS_STATUS_SUCCESS - if firmware initialization process success
120  */
CPUcheck_maincodeok_turnonCPU(struct net_device * dev)121 static bool CPUcheck_maincodeok_turnonCPU(struct net_device *dev)
122 {
123 	bool		rt_status = true;
124 	int		check_putcodeOK_time = 200000, check_bootOk_time = 200000;
125 	u32		CPU_status = 0;
126 
127 	/* Check whether put code OK */
128 	do {
129 		read_nic_dword(dev, CPU_GEN, &CPU_status);
130 
131 		if (CPU_status&CPU_GEN_PUT_CODE_OK)
132 			break;
133 
134 	} while (check_putcodeOK_time--);
135 
136 	if (!(CPU_status&CPU_GEN_PUT_CODE_OK)) {
137 		RT_TRACE(COMP_ERR, "Download Firmware: Put code fail!\n");
138 		goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
139 	} else {
140 		RT_TRACE(COMP_FIRMWARE, "Download Firmware: Put code ok!\n");
141 	}
142 
143 	/* Turn On CPU */
144 	read_nic_dword(dev, CPU_GEN, &CPU_status);
145 	write_nic_byte(dev, CPU_GEN,
146 		       (u8)((CPU_status | CPU_GEN_PWR_STB_CPU) & 0xff));
147 	mdelay(1000);
148 
149 	/* Check whether CPU boot OK */
150 	do {
151 		read_nic_dword(dev, CPU_GEN, &CPU_status);
152 
153 		if (CPU_status&CPU_GEN_BOOT_RDY)
154 			break;
155 	} while (check_bootOk_time--);
156 
157 	if (!(CPU_status&CPU_GEN_BOOT_RDY))
158 		goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
159 	else
160 		RT_TRACE(COMP_FIRMWARE, "Download Firmware: Boot ready!\n");
161 
162 	return rt_status;
163 
164 CPUCheckMainCodeOKAndTurnOnCPU_Fail:
165 	RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
166 	rt_status = false;
167 	return rt_status;
168 }
169 
CPUcheck_firmware_ready(struct net_device * dev)170 static bool CPUcheck_firmware_ready(struct net_device *dev)
171 {
172 	bool		rt_status = true;
173 	int		check_time = 200000;
174 	u32		CPU_status = 0;
175 
176 	/* Check Firmware Ready */
177 	do {
178 		read_nic_dword(dev, CPU_GEN, &CPU_status);
179 
180 		if (CPU_status&CPU_GEN_FIRM_RDY)
181 			break;
182 
183 	} while (check_time--);
184 
185 	if (!(CPU_status&CPU_GEN_FIRM_RDY))
186 		goto CPUCheckFirmwareReady_Fail;
187 	else
188 		RT_TRACE(COMP_FIRMWARE, "Download Firmware: Firmware ready!\n");
189 
190 	return rt_status;
191 
192 CPUCheckFirmwareReady_Fail:
193 	RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
194 	rt_status = false;
195 	return rt_status;
196 }
197 
init_firmware(struct net_device * dev)198 bool init_firmware(struct net_device *dev)
199 {
200 	struct r8192_priv	*priv = ieee80211_priv(dev);
201 	bool			rt_status = true;
202 
203 	u32			file_length = 0;
204 	u8			*mapped_file = NULL;
205 	u32			init_step = 0;
206 	enum opt_rst_type_e	   rst_opt = OPT_SYSTEM_RESET;
207 	enum firmware_init_step_e  starting_state = FW_INIT_STEP0_BOOT;
208 
209 	rt_firmware		*pfirmware = priv->pFirmware;
210 	const struct firmware	*fw_entry;
211 	const char *fw_name[3] = { "RTL8192U/boot.img",
212 			   "RTL8192U/main.img",
213 			   "RTL8192U/data.img"};
214 	int rc;
215 
216 	RT_TRACE(COMP_FIRMWARE, " PlatformInitFirmware()==>\n");
217 
218 	if (pfirmware->firmware_status == FW_STATUS_0_INIT) {
219 		/* it is called by reset */
220 		rst_opt = OPT_SYSTEM_RESET;
221 		starting_state = FW_INIT_STEP0_BOOT;
222 		/* TODO: system reset */
223 
224 	} else if (pfirmware->firmware_status == FW_STATUS_5_READY) {
225 		/* it is called by Initialize */
226 		rst_opt = OPT_FIRMWARE_RESET;
227 		starting_state = FW_INIT_STEP2_DATA;
228 	} else {
229 		RT_TRACE(COMP_FIRMWARE, "PlatformInitFirmware: undefined firmware state\n");
230 	}
231 
232 	/*
233 	 * Download boot, main, and data image for System reset.
234 	 * Download data image for firmware reset
235 	 */
236 	for (init_step = starting_state; init_step <= FW_INIT_STEP2_DATA; init_step++) {
237 		/*
238 		 * Open image file, and map file to continuous memory if open file success.
239 		 * or read image file from array. Default load from IMG file
240 		 */
241 		if (rst_opt == OPT_SYSTEM_RESET) {
242 			rc = request_firmware(&fw_entry, fw_name[init_step], &priv->udev->dev);
243 			if (rc < 0) {
244 				RT_TRACE(COMP_ERR, "request firmware fail!\n");
245 				goto download_firmware_fail;
246 			}
247 
248 			if (fw_entry->size > sizeof(pfirmware->firmware_buf)) {
249 				RT_TRACE(COMP_ERR, "img file size exceed the container buffer fail!\n");
250 				goto download_firmware_fail;
251 			}
252 
253 			if (init_step != FW_INIT_STEP1_MAIN) {
254 				memcpy(pfirmware->firmware_buf, fw_entry->data, fw_entry->size);
255 				mapped_file = pfirmware->firmware_buf;
256 				file_length = fw_entry->size;
257 			} else {
258 				memset(pfirmware->firmware_buf, 0, 128);
259 				memcpy(&pfirmware->firmware_buf[128], fw_entry->data, fw_entry->size);
260 				mapped_file = pfirmware->firmware_buf;
261 				file_length = fw_entry->size + 128;
262 			}
263 			pfirmware->firmware_buf_size = file_length;
264 		} else if (rst_opt == OPT_FIRMWARE_RESET) {
265 			/* we only need to download data.img here */
266 			mapped_file = pfirmware->firmware_buf;
267 			file_length = pfirmware->firmware_buf_size;
268 		}
269 
270 		/* Download image file */
271 		/* The firmware download process is just as following,
272 		 * 1. that is each packet will be segmented and inserted to the wait queue.
273 		 * 2. each packet segment will be put in the skb_buff packet.
274 		 * 3. each skb_buff packet data content will already include the firmware info
275 		 *   and Tx descriptor info
276 		 */
277 		rt_status = fw_download_code(dev, mapped_file, file_length);
278 		if (rst_opt == OPT_SYSTEM_RESET)
279 			release_firmware(fw_entry);
280 
281 		if (!rt_status)
282 			goto download_firmware_fail;
283 
284 		switch (init_step) {
285 		case FW_INIT_STEP0_BOOT:
286 			/* Download boot
287 			 * initialize command descriptor.
288 			 * will set polling bit when firmware code is also configured
289 			 */
290 			pfirmware->firmware_status = FW_STATUS_1_MOVE_BOOT_CODE;
291 			/* mdelay(1000); */
292 			/*
293 			 * To initialize IMEM, CPU move code  from 0x80000080,
294 			 * hence, we send 0x80 byte packet
295 			 */
296 			break;
297 
298 		case FW_INIT_STEP1_MAIN:
299 			/* Download firmware code. Wait until Boot Ready and Turn on CPU */
300 			pfirmware->firmware_status = FW_STATUS_2_MOVE_MAIN_CODE;
301 
302 			/* Check Put Code OK and Turn On CPU */
303 			rt_status = CPUcheck_maincodeok_turnonCPU(dev);
304 			if (!rt_status) {
305 				RT_TRACE(COMP_ERR, "CPUcheck_maincodeok_turnonCPU fail!\n");
306 				goto download_firmware_fail;
307 			}
308 
309 			pfirmware->firmware_status = FW_STATUS_3_TURNON_CPU;
310 			break;
311 
312 		case FW_INIT_STEP2_DATA:
313 			/* download initial data code */
314 			pfirmware->firmware_status = FW_STATUS_4_MOVE_DATA_CODE;
315 			mdelay(1);
316 
317 			rt_status = CPUcheck_firmware_ready(dev);
318 			if (!rt_status) {
319 				RT_TRACE(COMP_ERR, "CPUcheck_firmware_ready fail(%d)!\n", rt_status);
320 				goto download_firmware_fail;
321 			}
322 
323 			/* wait until data code is initialized ready.*/
324 			pfirmware->firmware_status = FW_STATUS_5_READY;
325 			break;
326 		}
327 	}
328 
329 	RT_TRACE(COMP_FIRMWARE, "Firmware Download Success\n");
330 	return rt_status;
331 
332 download_firmware_fail:
333 	RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
334 	rt_status = false;
335 	return rt_status;
336 }
337 
338 MODULE_FIRMWARE("RTL8192U/boot.img");
339 MODULE_FIRMWARE("RTL8192U/main.img");
340 MODULE_FIRMWARE("RTL8192U/data.img");
341