1 // SPDX-License-Identifier: GPL-2.0
2 /**************************************************************************************************
3 * Procedure: Init boot code/firmware code/data session
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.
9 * Arguments: The pointer of the adapter
12 * NDIS_STATUS_FAILURE - the following initialization process should be terminated
13 * NDIS_STATUS_SUCCESS - if firmware initialization process success
14 **************************************************************************************************/
17 #include "r8192U_hw.h"
18 #include "r819xU_firmware_img.h"
19 #include "r819xU_firmware.h"
20 #include <linux/firmware.h>
22 static void firmware_init_param(struct net_device *dev)
24 struct r8192_priv *priv = ieee80211_priv(dev);
25 rt_firmware *pfirmware = priv->pFirmware;
27 pfirmware->cmdpacket_frag_threshold = GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE);
31 * segment the img and use the ptr and length to remember info on each segment
34 static bool fw_download_code(struct net_device *dev, u8 *code_virtual_address,
37 struct r8192_priv *priv = ieee80211_priv(dev);
38 bool rt_status = true;
40 u16 frag_length, frag_offset = 0;
43 rt_firmware *pfirmware = priv->pFirmware;
45 unsigned char *seg_ptr;
46 struct cb_desc *tcb_desc;
50 firmware_init_param(dev);
51 /* Fragmentation might be required */
52 frag_threshold = pfirmware->cmdpacket_frag_threshold;
54 if ((buffer_len - frag_offset) > frag_threshold) {
55 frag_length = frag_threshold;
58 frag_length = buffer_len - frag_offset;
62 /* Allocate skb buffer to contain firmware info and tx descriptor info
63 * add 4 to avoid packet appending overflow.
65 skb = dev_alloc_skb(USB_HWDESC_HEADER_LEN + frag_length + 4);
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;
74 skb_reserve(skb, USB_HWDESC_HEADER_LEN);
77 * Transform from little endian to big endian
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;
86 tcb_desc->txbuf_size = (u16)i;
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);
96 priv->ieee80211->softmac_hard_start_xmit(skb, dev);
99 code_virtual_address += frag_length;
100 frag_offset += frag_length;
102 } while (frag_offset < buffer_len);
108 * Procedure: Check whether main code is download OK. If OK, turn on CPU
110 * Description: CPU register locates in different page against general register.
111 * Switch to CPU register in the begin and switch back before return
114 * Arguments: The pointer of the adapter
117 * NDIS_STATUS_FAILURE - the following initialization process should
119 * NDIS_STATUS_SUCCESS - if firmware initialization process success
121 static bool CPUcheck_maincodeok_turnonCPU(struct net_device *dev)
123 bool rt_status = true;
124 int check_putcodeOK_time = 200000, check_bootOk_time = 200000;
127 /* Check whether put code OK */
129 read_nic_dword(dev, CPU_GEN, &CPU_status);
131 if (CPU_status&CPU_GEN_PUT_CODE_OK)
134 } while (check_putcodeOK_time--);
136 if (!(CPU_status&CPU_GEN_PUT_CODE_OK)) {
137 RT_TRACE(COMP_ERR, "Download Firmware: Put code fail!\n");
138 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
140 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Put code ok!\n");
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));
149 /* Check whether CPU boot OK */
151 read_nic_dword(dev, CPU_GEN, &CPU_status);
153 if (CPU_status&CPU_GEN_BOOT_RDY)
155 } while (check_bootOk_time--);
157 if (!(CPU_status&CPU_GEN_BOOT_RDY))
158 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail;
160 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Boot ready!\n");
164 CPUCheckMainCodeOKAndTurnOnCPU_Fail:
165 RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
170 static bool CPUcheck_firmware_ready(struct net_device *dev)
172 bool rt_status = true;
173 int check_time = 200000;
176 /* Check Firmware Ready */
178 read_nic_dword(dev, CPU_GEN, &CPU_status);
180 if (CPU_status&CPU_GEN_FIRM_RDY)
183 } while (check_time--);
185 if (!(CPU_status&CPU_GEN_FIRM_RDY))
186 goto CPUCheckFirmwareReady_Fail;
188 RT_TRACE(COMP_FIRMWARE, "Download Firmware: Firmware ready!\n");
192 CPUCheckFirmwareReady_Fail:
193 RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
198 bool init_firmware(struct net_device *dev)
200 struct r8192_priv *priv = ieee80211_priv(dev);
201 bool rt_status = true;
204 u8 *mapped_file = NULL;
206 enum opt_rst_type_e rst_opt = OPT_SYSTEM_RESET;
207 enum firmware_init_step_e starting_state = FW_INIT_STEP0_BOOT;
209 rt_firmware *pfirmware = priv->pFirmware;
210 const struct firmware *fw_entry;
211 const char *fw_name[3] = { "RTL8192U/boot.img",
213 "RTL8192U/data.img"};
216 RT_TRACE(COMP_FIRMWARE, " PlatformInitFirmware()==>\n");
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 */
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;
229 RT_TRACE(COMP_FIRMWARE, "PlatformInitFirmware: undefined firmware state\n");
233 * Download boot, main, and data image for System reset.
234 * Download data image for firmware reset
236 for (init_step = starting_state; init_step <= FW_INIT_STEP2_DATA; init_step++) {
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
241 if (rst_opt == OPT_SYSTEM_RESET) {
242 rc = request_firmware(&fw_entry, fw_name[init_step], &priv->udev->dev);
244 RT_TRACE(COMP_ERR, "request firmware fail!\n");
245 goto download_firmware_fail;
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;
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;
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;
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;
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
277 rt_status = fw_download_code(dev, mapped_file, file_length);
278 if (rst_opt == OPT_SYSTEM_RESET)
279 release_firmware(fw_entry);
282 goto download_firmware_fail;
285 case FW_INIT_STEP0_BOOT:
287 * initialize command descriptor.
288 * will set polling bit when firmware code is also configured
290 pfirmware->firmware_status = FW_STATUS_1_MOVE_BOOT_CODE;
293 * To initialize IMEM, CPU move code from 0x80000080,
294 * hence, we send 0x80 byte packet
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;
302 /* Check Put Code OK and Turn On CPU */
303 rt_status = CPUcheck_maincodeok_turnonCPU(dev);
305 RT_TRACE(COMP_ERR, "CPUcheck_maincodeok_turnonCPU fail!\n");
306 goto download_firmware_fail;
309 pfirmware->firmware_status = FW_STATUS_3_TURNON_CPU;
312 case FW_INIT_STEP2_DATA:
313 /* download initial data code */
314 pfirmware->firmware_status = FW_STATUS_4_MOVE_DATA_CODE;
317 rt_status = CPUcheck_firmware_ready(dev);
319 RT_TRACE(COMP_ERR, "CPUcheck_firmware_ready fail(%d)!\n", rt_status);
320 goto download_firmware_fail;
323 /* wait until data code is initialized ready.*/
324 pfirmware->firmware_status = FW_STATUS_5_READY;
329 RT_TRACE(COMP_FIRMWARE, "Firmware Download Success\n");
332 download_firmware_fail:
333 RT_TRACE(COMP_ERR, "ERR in %s()\n", __func__);
338 MODULE_FIRMWARE("RTL8192U/boot.img");
339 MODULE_FIRMWARE("RTL8192U/main.img");
340 MODULE_FIRMWARE("RTL8192U/data.img");