2 * Procedure: Init boot code/firmware code/data session
4 * Description: This routine will initialize firmware. If any error occurs
5 * during the initialization process, the routine shall terminate
6 * immediately and return fail.
10 #include "r8192E_hw.h"
12 #include <linux/firmware.h>
14 /* It should be double word alignment */
15 #define GET_COMMAND_PACKET_FRAG_THRESHOLD(v) (4 * (v / 4) - 8)
17 enum firmware_init_step
{
18 FW_INIT_STEP0_BOOT
= 0,
19 FW_INIT_STEP1_MAIN
= 1,
20 FW_INIT_STEP2_DATA
= 2,
25 OPT_FIRMWARE_RESET
= 1,
28 void firmware_init_param(struct net_device
*dev
)
30 struct r8192_priv
*priv
= ieee80211_priv(dev
);
31 rt_firmware
*pfirmware
= priv
->pFirmware
;
33 pfirmware
->cmdpacket_frag_thresold
=
34 GET_COMMAND_PACKET_FRAG_THRESHOLD(MAX_TRANSMIT_BUFFER_SIZE
);
38 * segment the img and use the ptr and length to remember info on each segment
40 static bool fw_download_code(struct net_device
*dev
, u8
*code_virtual_address
,
43 struct r8192_priv
*priv
= ieee80211_priv(dev
);
44 bool rt_status
= true;
46 u16 frag_length
, frag_offset
= 0;
49 rt_firmware
*pfirmware
= priv
->pFirmware
;
51 unsigned char *seg_ptr
;
55 firmware_init_param(dev
);
57 /* Fragmentation might be required */
58 frag_threshold
= pfirmware
->cmdpacket_frag_thresold
;
60 if ((buffer_len
- frag_offset
) > frag_threshold
) {
61 frag_length
= frag_threshold
;
64 frag_length
= buffer_len
- frag_offset
;
69 * Allocate skb buffer to contain firmware info and tx
70 * descriptor info add 4 to avoid packet appending overflow.
72 skb
= dev_alloc_skb(frag_length
+ 4);
73 memcpy((unsigned char *)(skb
->cb
), &dev
, sizeof(dev
));
74 tcb_desc
= (cb_desc
*)(skb
->cb
+ MAX_DEV_ADDR_SIZE
);
75 tcb_desc
->queue_index
= TXCMD_QUEUE
;
76 tcb_desc
->bCmdOrInit
= DESC_PACKET_TYPE_INIT
;
77 tcb_desc
->bLastIniPkt
= bLastIniPkt
;
82 * Transform from little endian to big endian and pending zero
84 for (i
= 0; i
< frag_length
; i
+= 4) {
85 *seg_ptr
++ = ((i
+0) < frag_length
) ?
86 code_virtual_address
[i
+3] : 0;
88 *seg_ptr
++ = ((i
+1) < frag_length
) ?
89 code_virtual_address
[i
+2] : 0;
91 *seg_ptr
++ = ((i
+2) < frag_length
) ?
92 code_virtual_address
[i
+1] : 0;
94 *seg_ptr
++ = ((i
+3) < frag_length
) ?
95 code_virtual_address
[i
+0] : 0;
97 tcb_desc
->txbuf_size
= (u16
)i
;
99 priv
->ieee80211
->softmac_hard_start_xmit(skb
, dev
);
101 code_virtual_address
+= frag_length
;
102 frag_offset
+= frag_length
;
104 } while (frag_offset
< buffer_len
);
110 * Check whether main code is download OK. If OK, turn on CPU
112 * CPU register locates in different page against general
113 * register. Switch to CPU register in the begin and switch
116 static bool CPUcheck_maincodeok_turnonCPU(struct net_device
*dev
)
118 struct r8192_priv
*priv
= ieee80211_priv(dev
);
119 unsigned long timeout
;
120 bool rt_status
= true;
123 /* Check whether put code OK */
124 timeout
= jiffies
+ msecs_to_jiffies(20);
125 while (time_before(jiffies
, timeout
)) {
126 CPU_status
= read_nic_dword(priv
, CPU_GEN
);
128 if (CPU_status
& CPU_GEN_PUT_CODE_OK
)
133 if (!(CPU_status
& CPU_GEN_PUT_CODE_OK
)) {
134 RT_TRACE(COMP_ERR
, "Download Firmware: Put code fail!\n");
135 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail
;
137 RT_TRACE(COMP_FIRMWARE
, "Download Firmware: Put code ok!\n");
141 CPU_status
= read_nic_dword(priv
, CPU_GEN
);
142 write_nic_byte(priv
, CPU_GEN
,
143 (u8
)((CPU_status
| CPU_GEN_PWR_STB_CPU
) & 0xff));
146 /* Check whether CPU boot OK */
147 timeout
= jiffies
+ msecs_to_jiffies(20);
148 while (time_before(jiffies
, timeout
)) {
149 CPU_status
= read_nic_dword(priv
, CPU_GEN
);
151 if (CPU_status
& CPU_GEN_BOOT_RDY
)
156 if (!(CPU_status
& CPU_GEN_BOOT_RDY
))
157 goto CPUCheckMainCodeOKAndTurnOnCPU_Fail
;
159 RT_TRACE(COMP_FIRMWARE
, "Download Firmware: Boot ready!\n");
163 CPUCheckMainCodeOKAndTurnOnCPU_Fail
:
164 RT_TRACE(COMP_ERR
, "ERR in %s()\n", __func__
);
169 static bool CPUcheck_firmware_ready(struct net_device
*dev
)
171 struct r8192_priv
*priv
= ieee80211_priv(dev
);
172 unsigned long timeout
;
173 bool rt_status
= true;
176 /* Check Firmware Ready */
177 timeout
= jiffies
+ msecs_to_jiffies(20);
178 while (time_before(jiffies
, timeout
)) {
179 CPU_status
= read_nic_dword(priv
, CPU_GEN
);
181 if (CPU_status
& CPU_GEN_FIRM_RDY
)
186 if (!(CPU_status
& CPU_GEN_FIRM_RDY
))
187 goto CPUCheckFirmwareReady_Fail
;
189 RT_TRACE(COMP_FIRMWARE
, "Download Firmware: Firmware ready!\n");
193 CPUCheckFirmwareReady_Fail
:
194 RT_TRACE(COMP_ERR
, "ERR in %s()\n", __func__
);
200 bool init_firmware(struct net_device
*dev
)
202 struct r8192_priv
*priv
= ieee80211_priv(dev
);
203 bool rt_status
= true;
205 u8
*mapped_file
= NULL
;
207 enum opt_rst_type rst_opt
= OPT_SYSTEM_RESET
;
208 enum firmware_init_step starting_state
= FW_INIT_STEP0_BOOT
;
210 rt_firmware
*pfirmware
= priv
->pFirmware
;
211 const struct firmware
*fw_entry
;
212 const char *fw_name
[3] = { "RTL8192E/boot.img",
214 "RTL8192E/data.img"};
217 RT_TRACE(COMP_FIRMWARE
, " PlatformInitFirmware()==>\n");
219 if (pfirmware
->firmware_status
== FW_STATUS_0_INIT
) {
220 /* it is called by reset */
221 rst_opt
= OPT_SYSTEM_RESET
;
222 starting_state
= FW_INIT_STEP0_BOOT
;
223 /* TODO: system reset */
225 } else if (pfirmware
->firmware_status
== FW_STATUS_5_READY
) {
226 /* it is called by Initialize */
227 rst_opt
= OPT_FIRMWARE_RESET
;
228 starting_state
= FW_INIT_STEP2_DATA
;
230 RT_TRACE(COMP_FIRMWARE
,
231 "PlatformInitFirmware: undefined firmware state\n");
235 * Download boot, main, and data image for System reset.
236 * Download data image for firmware reseta
238 for (init_step
= starting_state
; init_step
<= FW_INIT_STEP2_DATA
;
241 * Open Image file, and map file to contineous memory if open file success.
242 * or read image file from array. Default load from IMG file
244 if (rst_opt
== OPT_SYSTEM_RESET
) {
245 if (pfirmware
->firmware_buf_size
[init_step
] == 0) {
246 rc
= request_firmware(&fw_entry
,
247 fw_name
[init_step
], &priv
->pdev
->dev
);
250 RT_TRACE(COMP_FIRMWARE
, "request firmware fail!\n");
251 goto download_firmware_fail
;
254 if (fw_entry
->size
> sizeof(pfirmware
->firmware_buf
[init_step
])) {
255 RT_TRACE(COMP_FIRMWARE
,
256 "img file size exceed the container buffer fail!\n");
257 goto download_firmware_fail
;
260 if (init_step
!= FW_INIT_STEP1_MAIN
) {
261 memcpy(pfirmware
->firmware_buf
[init_step
],
262 fw_entry
->data
, fw_entry
->size
);
263 pfirmware
->firmware_buf_size
[init_step
] = fw_entry
->size
;
266 memset(pfirmware
->firmware_buf
[init_step
], 0, 128);
267 memcpy(&pfirmware
->firmware_buf
[init_step
][128], fw_entry
->data
,
269 pfirmware
->firmware_buf_size
[init_step
] = fw_entry
->size
+128;
272 if (rst_opt
== OPT_SYSTEM_RESET
)
273 release_firmware(fw_entry
);
275 mapped_file
= pfirmware
->firmware_buf
[init_step
];
276 file_length
= pfirmware
->firmware_buf_size
[init_step
];
278 } else if (rst_opt
== OPT_FIRMWARE_RESET
) {
279 /* we only need to download data.img here */
280 mapped_file
= pfirmware
->firmware_buf
[init_step
];
281 file_length
= pfirmware
->firmware_buf_size
[init_step
];
284 /* Download image file */
285 /* The firmware download process is just as following,
286 * 1. that is each packet will be segmented and inserted to the
288 * 2. each packet segment will be put in the skb_buff packet.
289 * 3. each skb_buff packet data content will already include
290 * the firmware info and Tx descriptor info
292 rt_status
= fw_download_code(dev
, mapped_file
, file_length
);
293 if (rt_status
!= TRUE
)
294 goto download_firmware_fail
;
297 case FW_INIT_STEP0_BOOT
:
299 * initialize command descriptor.
300 * will set polling bit when firmware code is also
303 pfirmware
->firmware_status
= FW_STATUS_1_MOVE_BOOT_CODE
;
306 * To initialize IMEM, CPU move code from 0x80000080,
307 * hence, we send 0x80 byte packet
311 case FW_INIT_STEP1_MAIN
:
312 /* Download firmware code.
313 * Wait until Boot Ready and Turn on CPU */
314 pfirmware
->firmware_status
= FW_STATUS_2_MOVE_MAIN_CODE
;
316 /* Check Put Code OK and Turn On CPU */
317 rt_status
= CPUcheck_maincodeok_turnonCPU(dev
);
318 if (rt_status
!= TRUE
) {
319 RT_TRACE(COMP_FIRMWARE
,
320 "CPUcheck_maincodeok_turnonCPU fail!\n");
321 goto download_firmware_fail
;
324 pfirmware
->firmware_status
= FW_STATUS_3_TURNON_CPU
;
327 case FW_INIT_STEP2_DATA
:
328 /* download initial data code */
329 pfirmware
->firmware_status
= FW_STATUS_4_MOVE_DATA_CODE
;
332 rt_status
= CPUcheck_firmware_ready(dev
);
333 if (rt_status
!= TRUE
) {
334 RT_TRACE(COMP_FIRMWARE
,
335 "CPUcheck_firmware_ready fail(%d)!\n",
337 goto download_firmware_fail
;
340 /* wait until data code is initialized ready.*/
341 pfirmware
->firmware_status
= FW_STATUS_5_READY
;
346 RT_TRACE(COMP_FIRMWARE
, "Firmware Download Success\n");
349 download_firmware_fail
:
350 RT_TRACE(COMP_ERR
, "ERR in %s() step %d\n", __func__
, init_step
);
355 MODULE_FIRMWARE("RTL8192E/boot.img");
356 MODULE_FIRMWARE("RTL8192E/main.img");
357 MODULE_FIRMWARE("RTL8192E/data.img");