2 * This file is part of wl1271
4 * Copyright (C) 2008-2010 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/slab.h>
25 #include <linux/wl12xx.h>
26 #include <linux/export.h>
36 static int wl1271_boot_set_ecpu_ctrl(struct wl1271
*wl
, u32 flag
)
41 /* 10.5.0 run the firmware (I) */
42 ret
= wlcore_read_reg(wl
, REG_ECPU_CONTROL
, &cpu_ctrl
);
46 /* 10.5.1 run the firmware (II) */
48 ret
= wlcore_write_reg(wl
, REG_ECPU_CONTROL
, cpu_ctrl
);
54 static int wlcore_boot_parse_fw_ver(struct wl1271
*wl
,
55 struct wl1271_static_data
*static_data
)
59 strncpy(wl
->chip
.fw_ver_str
, static_data
->fw_version
,
60 sizeof(wl
->chip
.fw_ver_str
));
62 /* make sure the string is NULL-terminated */
63 wl
->chip
.fw_ver_str
[sizeof(wl
->chip
.fw_ver_str
) - 1] = '\0';
65 ret
= sscanf(wl
->chip
.fw_ver_str
+ 4, "%u.%u.%u.%u.%u",
66 &wl
->chip
.fw_ver
[0], &wl
->chip
.fw_ver
[1],
67 &wl
->chip
.fw_ver
[2], &wl
->chip
.fw_ver
[3],
71 wl1271_warning("fw version incorrect value");
72 memset(wl
->chip
.fw_ver
, 0, sizeof(wl
->chip
.fw_ver
));
77 ret
= wlcore_identify_fw(wl
);
84 static int wlcore_validate_fw_ver(struct wl1271
*wl
)
86 unsigned int *fw_ver
= wl
->chip
.fw_ver
;
87 unsigned int *min_ver
= (wl
->fw_type
== WL12XX_FW_TYPE_MULTI
) ?
88 wl
->min_mr_fw_ver
: wl
->min_sr_fw_ver
;
89 char min_fw_str
[32] = "";
92 /* the chip must be exactly equal */
93 if ((min_ver
[FW_VER_CHIP
] != WLCORE_FW_VER_IGNORE
) &&
94 (min_ver
[FW_VER_CHIP
] != fw_ver
[FW_VER_CHIP
]))
97 /* the firmware type must be equal */
98 if ((min_ver
[FW_VER_IF_TYPE
] != WLCORE_FW_VER_IGNORE
) &&
99 (min_ver
[FW_VER_IF_TYPE
] != fw_ver
[FW_VER_IF_TYPE
]))
102 /* the project number must be equal */
103 if ((min_ver
[FW_VER_SUBTYPE
] != WLCORE_FW_VER_IGNORE
) &&
104 (min_ver
[FW_VER_SUBTYPE
] != fw_ver
[FW_VER_SUBTYPE
]))
107 /* the API version must be greater or equal */
108 if ((min_ver
[FW_VER_MAJOR
] != WLCORE_FW_VER_IGNORE
) &&
109 (min_ver
[FW_VER_MAJOR
] > fw_ver
[FW_VER_MAJOR
]))
112 /* if the API version is equal... */
113 if (((min_ver
[FW_VER_MAJOR
] == WLCORE_FW_VER_IGNORE
) ||
114 (min_ver
[FW_VER_MAJOR
] == fw_ver
[FW_VER_MAJOR
])) &&
115 /* ...the minor must be greater or equal */
116 ((min_ver
[FW_VER_MINOR
] != WLCORE_FW_VER_IGNORE
) &&
117 (min_ver
[FW_VER_MINOR
] > fw_ver
[FW_VER_MINOR
])))
123 for (i
= 0; i
< NUM_FW_VER
; i
++)
124 if (min_ver
[i
] == WLCORE_FW_VER_IGNORE
)
125 snprintf(min_fw_str
, sizeof(min_fw_str
),
128 snprintf(min_fw_str
, sizeof(min_fw_str
),
129 "%s%u.", min_fw_str
, min_ver
[i
]);
131 wl1271_error("Your WiFi FW version (%u.%u.%u.%u.%u) is invalid.\n"
132 "Please use at least FW %s\n"
133 "You can get the latest firmwares at:\n"
134 "git://github.com/TI-OpenLink/firmwares.git",
135 fw_ver
[FW_VER_CHIP
], fw_ver
[FW_VER_IF_TYPE
],
136 fw_ver
[FW_VER_MAJOR
], fw_ver
[FW_VER_SUBTYPE
],
137 fw_ver
[FW_VER_MINOR
], min_fw_str
);
141 static int wlcore_boot_static_data(struct wl1271
*wl
)
143 struct wl1271_static_data
*static_data
;
144 size_t len
= sizeof(*static_data
) + wl
->static_data_priv_len
;
147 static_data
= kmalloc(len
, GFP_KERNEL
);
153 ret
= wlcore_read(wl
, wl
->cmd_box_addr
, static_data
, len
, false);
157 ret
= wlcore_boot_parse_fw_ver(wl
, static_data
);
161 ret
= wlcore_validate_fw_ver(wl
);
165 ret
= wlcore_handle_static_data(wl
, static_data
);
175 static int wl1271_boot_upload_firmware_chunk(struct wl1271
*wl
, void *buf
,
176 size_t fw_data_len
, u32 dest
)
178 struct wlcore_partition_set partition
;
179 int addr
, chunk_num
, partition_limit
;
183 /* whal_FwCtrl_LoadFwImageSm() */
185 wl1271_debug(DEBUG_BOOT
, "starting firmware upload");
187 wl1271_debug(DEBUG_BOOT
, "fw_data_len %zd chunk_size %d",
188 fw_data_len
, CHUNK_SIZE
);
190 if ((fw_data_len
% 4) != 0) {
191 wl1271_error("firmware length not multiple of four");
195 chunk
= kmalloc(CHUNK_SIZE
, GFP_KERNEL
);
197 wl1271_error("allocation for firmware upload chunk failed");
201 memcpy(&partition
, &wl
->ptable
[PART_DOWN
], sizeof(partition
));
202 partition
.mem
.start
= dest
;
203 ret
= wlcore_set_partition(wl
, &partition
);
207 /* 10.1 set partition limit and chunk num */
209 partition_limit
= wl
->ptable
[PART_DOWN
].mem
.size
;
211 while (chunk_num
< fw_data_len
/ CHUNK_SIZE
) {
212 /* 10.2 update partition, if needed */
213 addr
= dest
+ (chunk_num
+ 2) * CHUNK_SIZE
;
214 if (addr
> partition_limit
) {
215 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
216 partition_limit
= chunk_num
* CHUNK_SIZE
+
217 wl
->ptable
[PART_DOWN
].mem
.size
;
218 partition
.mem
.start
= addr
;
219 ret
= wlcore_set_partition(wl
, &partition
);
224 /* 10.3 upload the chunk */
225 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
226 p
= buf
+ chunk_num
* CHUNK_SIZE
;
227 memcpy(chunk
, p
, CHUNK_SIZE
);
228 wl1271_debug(DEBUG_BOOT
, "uploading fw chunk 0x%p to 0x%x",
230 ret
= wlcore_write(wl
, addr
, chunk
, CHUNK_SIZE
, false);
237 /* 10.4 upload the last chunk */
238 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
239 p
= buf
+ chunk_num
* CHUNK_SIZE
;
240 memcpy(chunk
, p
, fw_data_len
% CHUNK_SIZE
);
241 wl1271_debug(DEBUG_BOOT
, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
242 fw_data_len
% CHUNK_SIZE
, p
, addr
);
243 ret
= wlcore_write(wl
, addr
, chunk
, fw_data_len
% CHUNK_SIZE
, false);
250 int wlcore_boot_upload_firmware(struct wl1271
*wl
)
252 u32 chunks
, addr
, len
;
257 chunks
= be32_to_cpup((__be32
*) fw
);
260 wl1271_debug(DEBUG_BOOT
, "firmware chunks to be uploaded: %u", chunks
);
263 addr
= be32_to_cpup((__be32
*) fw
);
265 len
= be32_to_cpup((__be32
*) fw
);
269 wl1271_info("firmware chunk too long: %u", len
);
272 wl1271_debug(DEBUG_BOOT
, "chunk %d addr 0x%x len %u",
274 ret
= wl1271_boot_upload_firmware_chunk(wl
, fw
, len
, addr
);
282 EXPORT_SYMBOL_GPL(wlcore_boot_upload_firmware
);
284 int wlcore_boot_upload_nvs(struct wl1271
*wl
)
286 size_t nvs_len
, burst_len
;
289 u8
*nvs_ptr
, *nvs_aligned
;
292 if (wl
->nvs
== NULL
) {
293 wl1271_error("NVS file is needed during boot");
297 if (wl
->quirks
& WLCORE_QUIRK_LEGACY_NVS
) {
298 struct wl1271_nvs_file
*nvs
=
299 (struct wl1271_nvs_file
*)wl
->nvs
;
301 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
302 * band configurations) can be removed when those NVS files stop
305 if (wl
->nvs_len
== sizeof(struct wl1271_nvs_file
) ||
306 wl
->nvs_len
== WL1271_INI_LEGACY_NVS_FILE_SIZE
) {
307 if (nvs
->general_params
.dual_mode_select
)
308 wl
->enable_11a
= true;
311 if (wl
->nvs_len
!= sizeof(struct wl1271_nvs_file
) &&
312 (wl
->nvs_len
!= WL1271_INI_LEGACY_NVS_FILE_SIZE
||
314 wl1271_error("nvs size is not as expected: %zu != %zu",
315 wl
->nvs_len
, sizeof(struct wl1271_nvs_file
));
322 /* only the first part of the NVS needs to be uploaded */
323 nvs_len
= sizeof(nvs
->nvs
);
324 nvs_ptr
= (u8
*) nvs
->nvs
;
326 struct wl128x_nvs_file
*nvs
= (struct wl128x_nvs_file
*)wl
->nvs
;
328 if (wl
->nvs_len
== sizeof(struct wl128x_nvs_file
)) {
329 if (nvs
->general_params
.dual_mode_select
)
330 wl
->enable_11a
= true;
332 wl1271_error("nvs size is not as expected: %zu != %zu",
334 sizeof(struct wl128x_nvs_file
));
341 /* only the first part of the NVS needs to be uploaded */
342 nvs_len
= sizeof(nvs
->nvs
);
343 nvs_ptr
= (u8
*)nvs
->nvs
;
346 /* update current MAC address to NVS */
347 nvs_ptr
[11] = wl
->addresses
[0].addr
[0];
348 nvs_ptr
[10] = wl
->addresses
[0].addr
[1];
349 nvs_ptr
[6] = wl
->addresses
[0].addr
[2];
350 nvs_ptr
[5] = wl
->addresses
[0].addr
[3];
351 nvs_ptr
[4] = wl
->addresses
[0].addr
[4];
352 nvs_ptr
[3] = wl
->addresses
[0].addr
[5];
355 * Layout before the actual NVS tables:
356 * 1 byte : burst length.
357 * 2 bytes: destination address.
358 * n bytes: data to burst copy.
360 * This is ended by a 0 length, then the NVS tables.
363 /* FIXME: Do we need to check here whether the LSB is 1? */
365 burst_len
= nvs_ptr
[0];
366 dest_addr
= (nvs_ptr
[1] & 0xfe) | ((u32
)(nvs_ptr
[2] << 8));
369 * Due to our new wl1271_translate_reg_addr function,
370 * we need to add the register partition start address
373 dest_addr
+= wl
->curr_part
.reg
.start
;
375 /* We move our pointer to the data */
378 for (i
= 0; i
< burst_len
; i
++) {
379 if (nvs_ptr
+ 3 >= (u8
*) wl
->nvs
+ nvs_len
)
382 val
= (nvs_ptr
[0] | (nvs_ptr
[1] << 8)
383 | (nvs_ptr
[2] << 16) | (nvs_ptr
[3] << 24));
385 wl1271_debug(DEBUG_BOOT
,
386 "nvs burst write 0x%x: 0x%x",
388 ret
= wlcore_write32(wl
, dest_addr
, val
);
396 if (nvs_ptr
>= (u8
*) wl
->nvs
+ nvs_len
)
401 * We've reached the first zero length, the first NVS table
402 * is located at an aligned offset which is at least 7 bytes further.
403 * NOTE: The wl->nvs->nvs element must be first, in order to
404 * simplify the casting, we assume it is at the beginning of
405 * the wl->nvs structure.
407 nvs_ptr
= (u8
*)wl
->nvs
+
408 ALIGN(nvs_ptr
- (u8
*)wl
->nvs
+ 7, 4);
410 if (nvs_ptr
>= (u8
*) wl
->nvs
+ nvs_len
)
413 nvs_len
-= nvs_ptr
- (u8
*)wl
->nvs
;
415 /* Now we must set the partition correctly */
416 ret
= wlcore_set_partition(wl
, &wl
->ptable
[PART_WORK
]);
420 /* Copy the NVS tables to a new block to ensure alignment */
421 nvs_aligned
= kmemdup(nvs_ptr
, nvs_len
, GFP_KERNEL
);
425 /* And finally we upload the NVS tables */
426 ret
= wlcore_write_data(wl
, REG_CMD_MBOX_ADDRESS
, nvs_aligned
, nvs_len
,
433 wl1271_error("nvs data is malformed");
436 EXPORT_SYMBOL_GPL(wlcore_boot_upload_nvs
);
438 int wlcore_boot_run_firmware(struct wl1271
*wl
)
443 /* Make sure we have the boot partition */
444 ret
= wlcore_set_partition(wl
, &wl
->ptable
[PART_BOOT
]);
448 ret
= wl1271_boot_set_ecpu_ctrl(wl
, ECPU_CONTROL_HALT
);
452 ret
= wlcore_read_reg(wl
, REG_CHIP_ID_B
, &chip_id
);
456 wl1271_debug(DEBUG_BOOT
, "chip id after firmware boot: 0x%x", chip_id
);
458 if (chip_id
!= wl
->chip
.id
) {
459 wl1271_error("chip id doesn't match after firmware boot");
463 /* wait for init to complete */
465 while (loop
++ < INIT_LOOP
) {
466 udelay(INIT_LOOP_DELAY
);
467 ret
= wlcore_read_reg(wl
, REG_INTERRUPT_NO_CLEAR
, &intr
);
471 if (intr
== 0xffffffff) {
472 wl1271_error("error reading hardware complete "
476 /* check that ACX_INTR_INIT_COMPLETE is enabled */
477 else if (intr
& WL1271_ACX_INTR_INIT_COMPLETE
) {
478 ret
= wlcore_write_reg(wl
, REG_INTERRUPT_ACK
,
479 WL1271_ACX_INTR_INIT_COMPLETE
);
486 if (loop
> INIT_LOOP
) {
487 wl1271_error("timeout waiting for the hardware to "
488 "complete initialization");
492 /* get hardware config command mail box */
493 ret
= wlcore_read_reg(wl
, REG_COMMAND_MAILBOX_PTR
, &wl
->cmd_box_addr
);
497 wl1271_debug(DEBUG_MAILBOX
, "cmd_box_addr 0x%x", wl
->cmd_box_addr
);
499 /* get hardware config event mail box */
500 ret
= wlcore_read_reg(wl
, REG_EVENT_MAILBOX_PTR
, &wl
->mbox_ptr
[0]);
504 wl
->mbox_ptr
[1] = wl
->mbox_ptr
[0] + wl
->mbox_size
;
506 wl1271_debug(DEBUG_MAILBOX
, "MBOX ptrs: 0x%x 0x%x",
507 wl
->mbox_ptr
[0], wl
->mbox_ptr
[1]);
509 ret
= wlcore_boot_static_data(wl
);
511 wl1271_error("error getting static data");
516 * in case of full asynchronous mode the firmware event must be
517 * ready to receive event from the command mailbox
520 /* unmask required mbox events */
521 ret
= wl1271_event_unmask(wl
);
523 wl1271_error("EVENT mask setting failed");
527 /* set the working partition to its "running" mode offset */
528 ret
= wlcore_set_partition(wl
, &wl
->ptable
[PART_WORK
]);
530 /* firmware startup completed */
533 EXPORT_SYMBOL_GPL(wlcore_boot_run_firmware
);