1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1271
5 * Copyright (C) 2008-2010 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
10 #include <linux/slab.h>
11 #include <linux/export.h>
21 static int wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
26 /* 10.5.0 run the firmware (I) */
27 ret = wlcore_read_reg(wl, REG_ECPU_CONTROL, &cpu_ctrl);
31 /* 10.5.1 run the firmware (II) */
33 ret = wlcore_write_reg(wl, REG_ECPU_CONTROL, cpu_ctrl);
39 static int wlcore_boot_parse_fw_ver(struct wl1271 *wl,
40 struct wl1271_static_data *static_data)
44 strncpy(wl->chip.fw_ver_str, static_data->fw_version,
45 sizeof(wl->chip.fw_ver_str));
47 /* make sure the string is NULL-terminated */
48 wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
50 ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
51 &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
52 &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
56 wl1271_warning("fw version incorrect value");
57 memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
62 ret = wlcore_identify_fw(wl);
69 static int wlcore_validate_fw_ver(struct wl1271 *wl)
71 unsigned int *fw_ver = wl->chip.fw_ver;
72 unsigned int *min_ver = (wl->fw_type == WL12XX_FW_TYPE_MULTI) ?
73 wl->min_mr_fw_ver : wl->min_sr_fw_ver;
74 char min_fw_str[32] = "";
78 /* the chip must be exactly equal */
79 if ((min_ver[FW_VER_CHIP] != WLCORE_FW_VER_IGNORE) &&
80 (min_ver[FW_VER_CHIP] != fw_ver[FW_VER_CHIP]))
83 /* the firmware type must be equal */
84 if ((min_ver[FW_VER_IF_TYPE] != WLCORE_FW_VER_IGNORE) &&
85 (min_ver[FW_VER_IF_TYPE] != fw_ver[FW_VER_IF_TYPE]))
88 /* the project number must be equal */
89 if ((min_ver[FW_VER_SUBTYPE] != WLCORE_FW_VER_IGNORE) &&
90 (min_ver[FW_VER_SUBTYPE] != fw_ver[FW_VER_SUBTYPE]))
93 /* the API version must be greater or equal */
94 if ((min_ver[FW_VER_MAJOR] != WLCORE_FW_VER_IGNORE) &&
95 (min_ver[FW_VER_MAJOR] > fw_ver[FW_VER_MAJOR]))
98 /* if the API version is equal... */
99 if (((min_ver[FW_VER_MAJOR] == WLCORE_FW_VER_IGNORE) ||
100 (min_ver[FW_VER_MAJOR] == fw_ver[FW_VER_MAJOR])) &&
101 /* ...the minor must be greater or equal */
102 ((min_ver[FW_VER_MINOR] != WLCORE_FW_VER_IGNORE) &&
103 (min_ver[FW_VER_MINOR] > fw_ver[FW_VER_MINOR])))
109 for (i = 0; i < NUM_FW_VER && off < sizeof(min_fw_str); i++)
110 if (min_ver[i] == WLCORE_FW_VER_IGNORE)
111 off += snprintf(min_fw_str + off,
112 sizeof(min_fw_str) - off,
115 off += snprintf(min_fw_str + off,
116 sizeof(min_fw_str) - off,
119 wl1271_error("Your WiFi FW version (%u.%u.%u.%u.%u) is invalid.\n"
120 "Please use at least FW %s\n"
121 "You can get the latest firmwares at:\n"
122 "git://git.ti.com/wilink8-wlan/wl18xx_fw.git",
123 fw_ver[FW_VER_CHIP], fw_ver[FW_VER_IF_TYPE],
124 fw_ver[FW_VER_MAJOR], fw_ver[FW_VER_SUBTYPE],
125 fw_ver[FW_VER_MINOR], min_fw_str);
129 static int wlcore_boot_static_data(struct wl1271 *wl)
131 struct wl1271_static_data *static_data;
132 size_t len = sizeof(*static_data) + wl->static_data_priv_len;
135 static_data = kmalloc(len, GFP_KERNEL);
141 ret = wlcore_read(wl, wl->cmd_box_addr, static_data, len, false);
145 ret = wlcore_boot_parse_fw_ver(wl, static_data);
149 ret = wlcore_validate_fw_ver(wl);
153 ret = wlcore_handle_static_data(wl, static_data);
163 static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
164 size_t fw_data_len, u32 dest)
166 struct wlcore_partition_set partition;
167 int addr, chunk_num, partition_limit;
171 /* whal_FwCtrl_LoadFwImageSm() */
173 wl1271_debug(DEBUG_BOOT, "starting firmware upload");
175 wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
176 fw_data_len, CHUNK_SIZE);
178 if ((fw_data_len % 4) != 0) {
179 wl1271_error("firmware length not multiple of four");
183 chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
185 wl1271_error("allocation for firmware upload chunk failed");
189 memcpy(&partition, &wl->ptable[PART_DOWN], sizeof(partition));
190 partition.mem.start = dest;
191 ret = wlcore_set_partition(wl, &partition);
195 /* 10.1 set partition limit and chunk num */
197 partition_limit = wl->ptable[PART_DOWN].mem.size;
199 while (chunk_num < fw_data_len / CHUNK_SIZE) {
200 /* 10.2 update partition, if needed */
201 addr = dest + (chunk_num + 2) * CHUNK_SIZE;
202 if (addr > partition_limit) {
203 addr = dest + chunk_num * CHUNK_SIZE;
204 partition_limit = chunk_num * CHUNK_SIZE +
205 wl->ptable[PART_DOWN].mem.size;
206 partition.mem.start = addr;
207 ret = wlcore_set_partition(wl, &partition);
212 /* 10.3 upload the chunk */
213 addr = dest + chunk_num * CHUNK_SIZE;
214 p = buf + chunk_num * CHUNK_SIZE;
215 memcpy(chunk, p, CHUNK_SIZE);
216 wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
218 ret = wlcore_write(wl, addr, chunk, CHUNK_SIZE, false);
225 /* 10.4 upload the last chunk */
226 addr = dest + chunk_num * CHUNK_SIZE;
227 p = buf + chunk_num * CHUNK_SIZE;
228 memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
229 wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
230 fw_data_len % CHUNK_SIZE, p, addr);
231 ret = wlcore_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
238 int wlcore_boot_upload_firmware(struct wl1271 *wl)
240 u32 chunks, addr, len;
245 chunks = be32_to_cpup((__be32 *) fw);
248 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
251 addr = be32_to_cpup((__be32 *) fw);
253 len = be32_to_cpup((__be32 *) fw);
257 wl1271_info("firmware chunk too long: %u", len);
260 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
262 ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
270 EXPORT_SYMBOL_GPL(wlcore_boot_upload_firmware);
272 int wlcore_boot_upload_nvs(struct wl1271 *wl)
274 struct platform_device *pdev = wl->pdev;
275 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
276 const char *nvs_name = "unknown";
277 size_t nvs_len, burst_len;
280 u8 *nvs_ptr, *nvs_aligned;
283 if (wl->nvs == NULL) {
284 wl1271_error("NVS file is needed during boot");
288 if (pdev_data && pdev_data->family)
289 nvs_name = pdev_data->family->nvs_name;
291 if (wl->quirks & WLCORE_QUIRK_LEGACY_NVS) {
292 struct wl1271_nvs_file *nvs =
293 (struct wl1271_nvs_file *)wl->nvs;
295 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
296 * band configurations) can be removed when those NVS files stop
299 if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
300 wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
301 if (nvs->general_params.dual_mode_select)
302 wl->enable_11a = true;
305 if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
306 (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
308 wl1271_error("%s size is not as expected: %zu != %zu",
309 nvs_name, wl->nvs_len,
310 sizeof(struct wl1271_nvs_file));
317 /* only the first part of the NVS needs to be uploaded */
318 nvs_len = sizeof(nvs->nvs);
319 nvs_ptr = (u8 *) nvs->nvs;
321 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
323 if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
324 if (nvs->general_params.dual_mode_select)
325 wl->enable_11a = true;
327 wl1271_error("%s size is not as expected: %zu != %zu",
328 nvs_name, wl->nvs_len,
329 sizeof(struct wl128x_nvs_file));
336 /* only the first part of the NVS needs to be uploaded */
337 nvs_len = sizeof(nvs->nvs);
338 nvs_ptr = (u8 *)nvs->nvs;
341 /* update current MAC address to NVS */
342 nvs_ptr[11] = wl->addresses[0].addr[0];
343 nvs_ptr[10] = wl->addresses[0].addr[1];
344 nvs_ptr[6] = wl->addresses[0].addr[2];
345 nvs_ptr[5] = wl->addresses[0].addr[3];
346 nvs_ptr[4] = wl->addresses[0].addr[4];
347 nvs_ptr[3] = wl->addresses[0].addr[5];
350 * Layout before the actual NVS tables:
351 * 1 byte : burst length.
352 * 2 bytes: destination address.
353 * n bytes: data to burst copy.
355 * This is ended by a 0 length, then the NVS tables.
358 /* FIXME: Do we need to check here whether the LSB is 1? */
360 burst_len = nvs_ptr[0];
361 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
364 * Due to our new wl1271_translate_reg_addr function,
365 * we need to add the register partition start address
368 dest_addr += wl->curr_part.reg.start;
370 /* We move our pointer to the data */
373 for (i = 0; i < burst_len; i++) {
374 if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
377 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
378 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
380 wl1271_debug(DEBUG_BOOT,
381 "nvs burst write 0x%x: 0x%x",
383 ret = wlcore_write32(wl, dest_addr, val);
391 if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
396 * We've reached the first zero length, the first NVS table
397 * is located at an aligned offset which is at least 7 bytes further.
398 * NOTE: The wl->nvs->nvs element must be first, in order to
399 * simplify the casting, we assume it is at the beginning of
400 * the wl->nvs structure.
402 nvs_ptr = (u8 *)wl->nvs +
403 ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
405 if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
408 nvs_len -= nvs_ptr - (u8 *)wl->nvs;
410 /* Now we must set the partition correctly */
411 ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
415 /* Copy the NVS tables to a new block to ensure alignment */
416 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
420 /* And finally we upload the NVS tables */
421 ret = wlcore_write_data(wl, REG_CMD_MBOX_ADDRESS, nvs_aligned, nvs_len,
428 wl1271_error("nvs data is malformed");
431 EXPORT_SYMBOL_GPL(wlcore_boot_upload_nvs);
433 int wlcore_boot_run_firmware(struct wl1271 *wl)
438 /* Make sure we have the boot partition */
439 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
443 ret = wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
447 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &chip_id);
451 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
453 if (chip_id != wl->chip.id) {
454 wl1271_error("chip id doesn't match after firmware boot");
458 /* wait for init to complete */
460 while (loop++ < INIT_LOOP) {
461 udelay(INIT_LOOP_DELAY);
462 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
466 if (intr == 0xffffffff) {
467 wl1271_error("error reading hardware complete "
471 /* check that ACX_INTR_INIT_COMPLETE is enabled */
472 else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
473 ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
474 WL1271_ACX_INTR_INIT_COMPLETE);
481 if (loop > INIT_LOOP) {
482 wl1271_error("timeout waiting for the hardware to "
483 "complete initialization");
487 /* get hardware config command mail box */
488 ret = wlcore_read_reg(wl, REG_COMMAND_MAILBOX_PTR, &wl->cmd_box_addr);
492 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x", wl->cmd_box_addr);
494 /* get hardware config event mail box */
495 ret = wlcore_read_reg(wl, REG_EVENT_MAILBOX_PTR, &wl->mbox_ptr[0]);
499 wl->mbox_ptr[1] = wl->mbox_ptr[0] + wl->mbox_size;
501 wl1271_debug(DEBUG_MAILBOX, "MBOX ptrs: 0x%x 0x%x",
502 wl->mbox_ptr[0], wl->mbox_ptr[1]);
504 ret = wlcore_boot_static_data(wl);
506 wl1271_error("error getting static data");
511 * in case of full asynchronous mode the firmware event must be
512 * ready to receive event from the command mailbox
515 /* unmask required mbox events */
516 ret = wl1271_event_unmask(wl);
518 wl1271_error("EVENT mask setting failed");
522 /* set the working partition to its "running" mode offset */
523 ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
525 /* firmware startup completed */
528 EXPORT_SYMBOL_GPL(wlcore_boot_run_firmware);