hc
2024-05-08 f309769f8af08599af39b6de4f675784ce76530d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/******************************************************************************
 *
 * Copyright(c) 2017 - 2019 Realtek Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 ******************************************************************************/
 
#include "halmac_flash_88xx.h"
#include "halmac_88xx_cfg.h"
#include "halmac_common_88xx.h"
 
#if HALMAC_88XX_SUPPORT
 
/**
 * download_flash_88xx() -download firmware to flash
 * @adapter : the adapter of halmac
 * @fw_bin : pointer to fw
 * @size : fw size
 * @rom_addr : flash start address where fw should be download
 * Author : Pablo Chiu
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
download_flash_88xx(struct halmac_adapter *adapter, u8 *fw_bin, u32 size,
           u32 rom_addr)
{
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   enum halmac_ret_status rc;
   struct halmac_h2c_header_info hdr_info;
   u8 value8;
   u8 restore[3];
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = {0};
   u16 seq_num = 0;
   u16 h2c_info_offset;
   u32 pkt_size;
   u32 mem_offset;
   u32 cnt;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   value8 = HALMAC_REG_R8(REG_CR + 1);
   restore[0] = value8;
   value8 = (u8)(value8 | BIT(0));
   HALMAC_REG_W8(REG_CR + 1, value8);
 
   value8 = HALMAC_REG_R8(REG_BCN_CTRL);
   restore[1] = value8;
   value8 = (u8)((value8 & ~(BIT(3))) | BIT(4));
   HALMAC_REG_W8(REG_BCN_CTRL, value8);
 
   value8 = HALMAC_REG_R8(REG_FWHW_TXQ_CTRL + 2);
   restore[2] = value8;
   value8 = (u8)(value8 & ~(BIT(6)));
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, value8);
 
   /* Download FW to Flash flow */
   h2c_info_offset = adapter->txff_alloc.rsvd_h2c_info_addr -
                   adapter->txff_alloc.rsvd_boundary;
   mem_offset = 0;
 
   while (size != 0) {
       if (size >= (DL_FLASH_RSVDPG_SIZE - 48))
           pkt_size = DL_FLASH_RSVDPG_SIZE - 48;
       else
           pkt_size = size;
 
       rc = dl_rsvd_page_88xx(adapter,
                      adapter->txff_alloc.rsvd_h2c_info_addr,
                      fw_bin + mem_offset, pkt_size);
       if (rc != HALMAC_RET_SUCCESS) {
           PLTFM_MSG_ERR("[ERR]dl rsvd pg!!\n");
           return rc;
       }
 
       DOWNLOAD_FLASH_SET_SPI_CMD(h2c_buf, 0x02);
       DOWNLOAD_FLASH_SET_LOCATION(h2c_buf, h2c_info_offset);
       DOWNLOAD_FLASH_SET_SIZE(h2c_buf, pkt_size);
       DOWNLOAD_FLASH_SET_START_ADDR(h2c_buf, rom_addr);
 
       hdr_info.sub_cmd_id = SUB_CMD_ID_DOWNLOAD_FLASH;
       hdr_info.content_size = 20;
       hdr_info.ack = 1;
       set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
       rc = send_h2c_pkt_88xx(adapter, h2c_buf);
 
       if (rc != HALMAC_RET_SUCCESS) {
           PLTFM_MSG_ERR("[ERR]send h2c!!\n");
           return rc;
       }
 
       value8 = HALMAC_REG_R8(REG_MCUTST_I);
       value8 |= BIT(0);
       HALMAC_REG_W8(REG_MCUTST_I, value8);
 
       rom_addr += pkt_size;
       mem_offset += pkt_size;
       size -= pkt_size;
 
       cnt = 1000;
       while (((HALMAC_REG_R8(REG_MCUTST_I)) & BIT(0)) != 0) {
           if (cnt == 0) {
               PLTFM_MSG_ERR("[ERR]dl flash!!\n");
               return  HALMAC_RET_DLFW_FAIL;
           }
           cnt--;
           PLTFM_DELAY_US(1000);
       }
   }
 
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, restore[2]);
   HALMAC_REG_W8(REG_BCN_CTRL, restore[1]);
   HALMAC_REG_W8(REG_CR + 1, restore[0]);
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * read_flash_88xx() -read data from flash
 * @adapter : the adapter of halmac
 * @addr : flash start address where fw should be read
 * Author : Pablo Chiu
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
read_flash_88xx(struct halmac_adapter *adapter, u32 addr, u32 length, u8 *data)
{
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   enum halmac_ret_status status;
   struct halmac_h2c_header_info hdr_info;
   u8 value8;
   u8 restore[3];
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = {0};
   u16 seq_num = 0;
   u16 h2c_pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
   u16 rsvd_pg_addr = adapter->txff_alloc.rsvd_boundary;
   u16 h2c_info_addr;
   u32 cnt;
 
   PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
 
   value8 = HALMAC_REG_R8(REG_CR + 1);
   restore[0] = value8;
   value8 = (u8)(value8 | BIT(0));
   HALMAC_REG_W8(REG_CR + 1, value8);
 
   value8 = HALMAC_REG_R8(REG_BCN_CTRL);
   restore[1] = value8;
   value8 = (u8)((value8 & ~(BIT(3))) | BIT(4));
   HALMAC_REG_W8(REG_BCN_CTRL, value8);
 
   value8 = HALMAC_REG_R8(REG_FWHW_TXQ_CTRL + 2);
   restore[2] = value8;
   value8 = (u8)(value8 & ~(BIT(6)));
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, value8);
 
   HALMAC_REG_W16(REG_FIFOPAGE_CTRL_2, h2c_pg_addr);
   value8 = HALMAC_REG_R8(REG_MCUTST_I);
   value8 |= BIT(0);
   HALMAC_REG_W8(REG_MCUTST_I, value8);
 
   /* Construct H2C Content */
   DOWNLOAD_FLASH_SET_SPI_CMD(h2c_buf, 0x03);
   DOWNLOAD_FLASH_SET_LOCATION(h2c_buf, h2c_pg_addr - rsvd_pg_addr);
   DOWNLOAD_FLASH_SET_SIZE(h2c_buf, length);
   DOWNLOAD_FLASH_SET_START_ADDR(h2c_buf, addr);
 
   /* Fill in H2C Header */
   hdr_info.sub_cmd_id = SUB_CMD_ID_DOWNLOAD_FLASH;
   hdr_info.content_size = 16;
   hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
   /* Send H2C Cmd Packet */
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
       return status;
   }
 
   cnt = 5000;
   while (((HALMAC_REG_R8(REG_MCUTST_I)) & BIT(0)) != 0) {
       if (cnt == 0) {
           PLTFM_MSG_ERR("[ERR]read flash!!\n");
           return  HALMAC_RET_FAIL;
       }
       cnt--;
       PLTFM_DELAY_US(1000);
   }
 
   HALMAC_REG_W8_CLR(REG_MCUTST_I, BIT(0));
 
   HALMAC_REG_W16(REG_FIFOPAGE_CTRL_2, rsvd_pg_addr);
   HALMAC_REG_W8(REG_FWHW_TXQ_CTRL + 2, restore[2]);
   HALMAC_REG_W8(REG_BCN_CTRL, restore[1]);
   HALMAC_REG_W8(REG_CR + 1, restore[0]);
 
   h2c_info_addr = h2c_pg_addr << TX_PAGE_SIZE_SHIFT_88XX;
   status = dump_fifo_88xx(adapter, HAL_FIFO_SEL_TX, h2c_info_addr,
               length, data);
   if (status != HALMAC_RET_SUCCESS) {
       PLTFM_MSG_ERR("[ERR]dump fifo!!\n");
       return status;
   }
   PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
 
   return HALMAC_RET_SUCCESS;
}
 
/**
 * erase_flash_88xx() -erase flash data
 * @adapter : the adapter of halmac
 * @erase_cmd : erase command
 * @addr : flash start address where fw should be erased
 * Author : Pablo Chiu
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
erase_flash_88xx(struct halmac_adapter *adapter, u8 erase_cmd, u32 addr)
{
   enum halmac_ret_status status;
   struct halmac_h2c_header_info hdr_info;
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   u8 value8;
   u8 h2c_buf[H2C_PKT_SIZE_88XX] = {0};
   u16 seq_num = 0;
   u32 cnt;
 
   /* Construct H2C Content */
   DOWNLOAD_FLASH_SET_SPI_CMD(h2c_buf, erase_cmd);
   DOWNLOAD_FLASH_SET_LOCATION(h2c_buf, 0);
   DOWNLOAD_FLASH_SET_START_ADDR(h2c_buf, addr);
   DOWNLOAD_FLASH_SET_SIZE(h2c_buf, 0);
 
   value8 = HALMAC_REG_R8(REG_MCUTST_I);
   value8 |= BIT(0);
   HALMAC_REG_W8(REG_MCUTST_I, value8);
 
   /* Fill in H2C Header */
   hdr_info.sub_cmd_id = SUB_CMD_ID_DOWNLOAD_FLASH;
   hdr_info.content_size = 16;
   hdr_info.ack = 1;
   set_h2c_pkt_hdr_88xx(adapter, h2c_buf, &hdr_info, &seq_num);
 
   /* Send H2C Cmd Packet */
   status = send_h2c_pkt_88xx(adapter, h2c_buf);
 
   if (status != HALMAC_RET_SUCCESS)
       PLTFM_MSG_ERR("[ERR]send h2c!!\n");
 
   cnt = 5000;
   while (((HALMAC_REG_R8(REG_MCUTST_I)) & BIT(0)) != 0 && cnt != 0) {
       PLTFM_DELAY_US(1000);
       cnt--;
   }
 
   if (cnt == 0)
       return HALMAC_RET_FAIL;
   else
       return HALMAC_RET_SUCCESS;
}
 
/**
 * check_flash_88xx() -check flash data
 * @adapter : the adapter of halmac
 * @fw_bin : pointer to fw
 * @size : fw size
 * @addr : flash start address where fw should be checked
 * Author : Pablo Chiu
 * Return : enum halmac_ret_status
 * More details of status code can be found in prototype document
 */
enum halmac_ret_status
check_flash_88xx(struct halmac_adapter *adapter, u8 *fw_bin, u32 size,
        u32 addr)
{
   struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
   u8 value8;
   u16 i;
   u16 residue;
   u16 pg_addr;
   u32 pkt_size;
   u32 start_page;
   u32 cnt;
   u8 *data;
 
   pg_addr = adapter->txff_alloc.rsvd_h2c_info_addr;
 
   data = (u8 *)PLTFM_MALLOC(4096);
 
   while (size != 0) {
       start_page = ((pg_addr << 7) >> 12) + 0x780;
       residue = (pg_addr << 7) & (4096 - 1);
 
       if (size >= DL_FLASH_RSVDPG_SIZE)
           pkt_size = DL_FLASH_RSVDPG_SIZE;
       else
           pkt_size = size;
 
       read_flash_88xx(adapter, addr, 4096, data);
 
       cnt = 0;
       while (cnt < pkt_size) {
           HALMAC_REG_W16(REG_PKTBUF_DBG_CTRL, (u16)(start_page));
           for (i = 0x8000 + residue; i <= 0x8FFF; i++) {
               value8 = HALMAC_REG_R8(i);
               if (*fw_bin != value8) {
                   PLTFM_MSG_ERR("[ERR]check flash!!\n");
                   PLTFM_FREE(data, 4096);
                   return HALMAC_RET_FAIL;
               }
 
               fw_bin++;
               cnt++;
               if (cnt == pkt_size)
                   break;
           }
           residue = 0;
           start_page++;
       }
       addr += pkt_size;
       size -= pkt_size;
   }
 
   PLTFM_FREE(data, 4096);
 
   return HALMAC_RET_SUCCESS;
}
 
#endif /* HALMAC_88XX_SUPPORT */