Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
Note: File does not exist in v3.1.
  1/*
  2 * This file is part of wl1271
  3 *
  4 * Copyright (C) 2008-2010 Nokia Corporation
  5 *
  6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7 *
  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.
 11 *
 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.
 16 *
 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
 20 * 02110-1301 USA
 21 *
 22 */
 23
 24#include <linux/slab.h>
 25#include <linux/export.h>
 26
 27#include "debug.h"
 28#include "acx.h"
 29#include "boot.h"
 30#include "io.h"
 31#include "event.h"
 32#include "rx.h"
 33#include "hw_ops.h"
 34
 35static int wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
 36{
 37	u32 cpu_ctrl;
 38	int ret;
 39
 40	/* 10.5.0 run the firmware (I) */
 41	ret = wlcore_read_reg(wl, REG_ECPU_CONTROL, &cpu_ctrl);
 42	if (ret < 0)
 43		goto out;
 44
 45	/* 10.5.1 run the firmware (II) */
 46	cpu_ctrl |= flag;
 47	ret = wlcore_write_reg(wl, REG_ECPU_CONTROL, cpu_ctrl);
 48
 49out:
 50	return ret;
 51}
 52
 53static int wlcore_boot_parse_fw_ver(struct wl1271 *wl,
 54				    struct wl1271_static_data *static_data)
 55{
 56	int ret;
 57
 58	strncpy(wl->chip.fw_ver_str, static_data->fw_version,
 59		sizeof(wl->chip.fw_ver_str));
 60
 61	/* make sure the string is NULL-terminated */
 62	wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
 63
 64	ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
 65		     &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
 66		     &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
 67		     &wl->chip.fw_ver[4]);
 68
 69	if (ret != 5) {
 70		wl1271_warning("fw version incorrect value");
 71		memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
 72		ret = -EINVAL;
 73		goto out;
 74	}
 75
 76	ret = wlcore_identify_fw(wl);
 77	if (ret < 0)
 78		goto out;
 79out:
 80	return ret;
 81}
 82
 83static int wlcore_validate_fw_ver(struct wl1271 *wl)
 84{
 85	unsigned int *fw_ver = wl->chip.fw_ver;
 86	unsigned int *min_ver = (wl->fw_type == WL12XX_FW_TYPE_MULTI) ?
 87		wl->min_mr_fw_ver : wl->min_sr_fw_ver;
 88	char min_fw_str[32] = "";
 89	int i;
 90
 91	/* the chip must be exactly equal */
 92	if ((min_ver[FW_VER_CHIP] != WLCORE_FW_VER_IGNORE) &&
 93	    (min_ver[FW_VER_CHIP] != fw_ver[FW_VER_CHIP]))
 94		goto fail;
 95
 96	/* the firmware type must be equal */
 97	if ((min_ver[FW_VER_IF_TYPE] != WLCORE_FW_VER_IGNORE) &&
 98	    (min_ver[FW_VER_IF_TYPE] != fw_ver[FW_VER_IF_TYPE]))
 99		goto fail;
100
101	/* the project number must be equal */
102	if ((min_ver[FW_VER_SUBTYPE] != WLCORE_FW_VER_IGNORE) &&
103	    (min_ver[FW_VER_SUBTYPE] != fw_ver[FW_VER_SUBTYPE]))
104		goto fail;
105
106	/* the API version must be greater or equal */
107	if ((min_ver[FW_VER_MAJOR] != WLCORE_FW_VER_IGNORE) &&
108		 (min_ver[FW_VER_MAJOR] > fw_ver[FW_VER_MAJOR]))
109		goto fail;
110
111	/* if the API version is equal... */
112	if (((min_ver[FW_VER_MAJOR] == WLCORE_FW_VER_IGNORE) ||
113	     (min_ver[FW_VER_MAJOR] == fw_ver[FW_VER_MAJOR])) &&
114	    /* ...the minor must be greater or equal */
115	    ((min_ver[FW_VER_MINOR] != WLCORE_FW_VER_IGNORE) &&
116	     (min_ver[FW_VER_MINOR] > fw_ver[FW_VER_MINOR])))
117		goto fail;
118
119	return 0;
120
121fail:
122	for (i = 0; i < NUM_FW_VER; i++)
123		if (min_ver[i] == WLCORE_FW_VER_IGNORE)
124			snprintf(min_fw_str, sizeof(min_fw_str),
125				  "%s*.", min_fw_str);
126		else
127			snprintf(min_fw_str, sizeof(min_fw_str),
128				  "%s%u.", min_fw_str, min_ver[i]);
129
130	wl1271_error("Your WiFi FW version (%u.%u.%u.%u.%u) is invalid.\n"
131		     "Please use at least FW %s\n"
132		     "You can get the latest firmwares at:\n"
133		     "git://git.ti.com/wilink8-wlan/wl18xx_fw.git",
134		     fw_ver[FW_VER_CHIP], fw_ver[FW_VER_IF_TYPE],
135		     fw_ver[FW_VER_MAJOR], fw_ver[FW_VER_SUBTYPE],
136		     fw_ver[FW_VER_MINOR], min_fw_str);
137	return -EINVAL;
138}
139
140static int wlcore_boot_static_data(struct wl1271 *wl)
141{
142	struct wl1271_static_data *static_data;
143	size_t len = sizeof(*static_data) + wl->static_data_priv_len;
144	int ret;
145
146	static_data = kmalloc(len, GFP_KERNEL);
147	if (!static_data) {
148		ret = -ENOMEM;
149		goto out;
150	}
151
152	ret = wlcore_read(wl, wl->cmd_box_addr, static_data, len, false);
153	if (ret < 0)
154		goto out_free;
155
156	ret = wlcore_boot_parse_fw_ver(wl, static_data);
157	if (ret < 0)
158		goto out_free;
159
160	ret = wlcore_validate_fw_ver(wl);
161	if (ret < 0)
162		goto out_free;
163
164	ret = wlcore_handle_static_data(wl, static_data);
165	if (ret < 0)
166		goto out_free;
167
168out_free:
169	kfree(static_data);
170out:
171	return ret;
172}
173
174static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
175					     size_t fw_data_len, u32 dest)
176{
177	struct wlcore_partition_set partition;
178	int addr, chunk_num, partition_limit;
179	u8 *p, *chunk;
180	int ret;
181
182	/* whal_FwCtrl_LoadFwImageSm() */
183
184	wl1271_debug(DEBUG_BOOT, "starting firmware upload");
185
186	wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
187		     fw_data_len, CHUNK_SIZE);
188
189	if ((fw_data_len % 4) != 0) {
190		wl1271_error("firmware length not multiple of four");
191		return -EIO;
192	}
193
194	chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
195	if (!chunk) {
196		wl1271_error("allocation for firmware upload chunk failed");
197		return -ENOMEM;
198	}
199
200	memcpy(&partition, &wl->ptable[PART_DOWN], sizeof(partition));
201	partition.mem.start = dest;
202	ret = wlcore_set_partition(wl, &partition);
203	if (ret < 0)
204		goto out;
205
206	/* 10.1 set partition limit and chunk num */
207	chunk_num = 0;
208	partition_limit = wl->ptable[PART_DOWN].mem.size;
209
210	while (chunk_num < fw_data_len / CHUNK_SIZE) {
211		/* 10.2 update partition, if needed */
212		addr = dest + (chunk_num + 2) * CHUNK_SIZE;
213		if (addr > partition_limit) {
214			addr = dest + chunk_num * CHUNK_SIZE;
215			partition_limit = chunk_num * CHUNK_SIZE +
216				wl->ptable[PART_DOWN].mem.size;
217			partition.mem.start = addr;
218			ret = wlcore_set_partition(wl, &partition);
219			if (ret < 0)
220				goto out;
221		}
222
223		/* 10.3 upload the chunk */
224		addr = dest + chunk_num * CHUNK_SIZE;
225		p = buf + chunk_num * CHUNK_SIZE;
226		memcpy(chunk, p, CHUNK_SIZE);
227		wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
228			     p, addr);
229		ret = wlcore_write(wl, addr, chunk, CHUNK_SIZE, false);
230		if (ret < 0)
231			goto out;
232
233		chunk_num++;
234	}
235
236	/* 10.4 upload the last chunk */
237	addr = dest + chunk_num * CHUNK_SIZE;
238	p = buf + chunk_num * CHUNK_SIZE;
239	memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
240	wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
241		     fw_data_len % CHUNK_SIZE, p, addr);
242	ret = wlcore_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
243
244out:
245	kfree(chunk);
246	return ret;
247}
248
249int wlcore_boot_upload_firmware(struct wl1271 *wl)
250{
251	u32 chunks, addr, len;
252	int ret = 0;
253	u8 *fw;
254
255	fw = wl->fw;
256	chunks = be32_to_cpup((__be32 *) fw);
257	fw += sizeof(u32);
258
259	wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
260
261	while (chunks--) {
262		addr = be32_to_cpup((__be32 *) fw);
263		fw += sizeof(u32);
264		len = be32_to_cpup((__be32 *) fw);
265		fw += sizeof(u32);
266
267		if (len > 300000) {
268			wl1271_info("firmware chunk too long: %u", len);
269			return -EINVAL;
270		}
271		wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
272			     chunks, addr, len);
273		ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
274		if (ret != 0)
275			break;
276		fw += len;
277	}
278
279	return ret;
280}
281EXPORT_SYMBOL_GPL(wlcore_boot_upload_firmware);
282
283int wlcore_boot_upload_nvs(struct wl1271 *wl)
284{
285	struct platform_device *pdev = wl->pdev;
286	struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
287	const char *nvs_name = "unknown";
288	size_t nvs_len, burst_len;
289	int i;
290	u32 dest_addr, val;
291	u8 *nvs_ptr, *nvs_aligned;
292	int ret;
293
294	if (wl->nvs == NULL) {
295		wl1271_error("NVS file is needed during boot");
296		return -ENODEV;
297	}
298
299	if (pdev_data && pdev_data->family)
300		nvs_name = pdev_data->family->nvs_name;
301
302	if (wl->quirks & WLCORE_QUIRK_LEGACY_NVS) {
303		struct wl1271_nvs_file *nvs =
304			(struct wl1271_nvs_file *)wl->nvs;
305		/*
306		 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
307		 * band configurations) can be removed when those NVS files stop
308		 * floating around.
309		 */
310		if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
311		    wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
312			if (nvs->general_params.dual_mode_select)
313				wl->enable_11a = true;
314		}
315
316		if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
317		    (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
318		     wl->enable_11a)) {
319			wl1271_error("%s size is not as expected: %zu != %zu",
320				     nvs_name, wl->nvs_len,
321				     sizeof(struct wl1271_nvs_file));
322			kfree(wl->nvs);
323			wl->nvs = NULL;
324			wl->nvs_len = 0;
325			return -EILSEQ;
326		}
327
328		/* only the first part of the NVS needs to be uploaded */
329		nvs_len = sizeof(nvs->nvs);
330		nvs_ptr = (u8 *) nvs->nvs;
331	} else {
332		struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
333
334		if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
335			if (nvs->general_params.dual_mode_select)
336				wl->enable_11a = true;
337		} else {
338			wl1271_error("%s size is not as expected: %zu != %zu",
339				     nvs_name, wl->nvs_len,
340				     sizeof(struct wl128x_nvs_file));
341			kfree(wl->nvs);
342			wl->nvs = NULL;
343			wl->nvs_len = 0;
344			return -EILSEQ;
345		}
346
347		/* only the first part of the NVS needs to be uploaded */
348		nvs_len = sizeof(nvs->nvs);
349		nvs_ptr = (u8 *)nvs->nvs;
350	}
351
352	/* update current MAC address to NVS */
353	nvs_ptr[11] = wl->addresses[0].addr[0];
354	nvs_ptr[10] = wl->addresses[0].addr[1];
355	nvs_ptr[6] = wl->addresses[0].addr[2];
356	nvs_ptr[5] = wl->addresses[0].addr[3];
357	nvs_ptr[4] = wl->addresses[0].addr[4];
358	nvs_ptr[3] = wl->addresses[0].addr[5];
359
360	/*
361	 * Layout before the actual NVS tables:
362	 * 1 byte : burst length.
363	 * 2 bytes: destination address.
364	 * n bytes: data to burst copy.
365	 *
366	 * This is ended by a 0 length, then the NVS tables.
367	 */
368
369	/* FIXME: Do we need to check here whether the LSB is 1? */
370	while (nvs_ptr[0]) {
371		burst_len = nvs_ptr[0];
372		dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
373
374		/*
375		 * Due to our new wl1271_translate_reg_addr function,
376		 * we need to add the register partition start address
377		 * to the destination
378		 */
379		dest_addr += wl->curr_part.reg.start;
380
381		/* We move our pointer to the data */
382		nvs_ptr += 3;
383
384		for (i = 0; i < burst_len; i++) {
385			if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
386				goto out_badnvs;
387
388			val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
389			       | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
390
391			wl1271_debug(DEBUG_BOOT,
392				     "nvs burst write 0x%x: 0x%x",
393				     dest_addr, val);
394			ret = wlcore_write32(wl, dest_addr, val);
395			if (ret < 0)
396				return ret;
397
398			nvs_ptr += 4;
399			dest_addr += 4;
400		}
401
402		if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
403			goto out_badnvs;
404	}
405
406	/*
407	 * We've reached the first zero length, the first NVS table
408	 * is located at an aligned offset which is at least 7 bytes further.
409	 * NOTE: The wl->nvs->nvs element must be first, in order to
410	 * simplify the casting, we assume it is at the beginning of
411	 * the wl->nvs structure.
412	 */
413	nvs_ptr = (u8 *)wl->nvs +
414			ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
415
416	if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
417		goto out_badnvs;
418
419	nvs_len -= nvs_ptr - (u8 *)wl->nvs;
420
421	/* Now we must set the partition correctly */
422	ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
423	if (ret < 0)
424		return ret;
425
426	/* Copy the NVS tables to a new block to ensure alignment */
427	nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
428	if (!nvs_aligned)
429		return -ENOMEM;
430
431	/* And finally we upload the NVS tables */
432	ret = wlcore_write_data(wl, REG_CMD_MBOX_ADDRESS, nvs_aligned, nvs_len,
433				false);
434
435	kfree(nvs_aligned);
436	return ret;
437
438out_badnvs:
439	wl1271_error("nvs data is malformed");
440	return -EILSEQ;
441}
442EXPORT_SYMBOL_GPL(wlcore_boot_upload_nvs);
443
444int wlcore_boot_run_firmware(struct wl1271 *wl)
445{
446	int loop, ret;
447	u32 chip_id, intr;
448
449	/* Make sure we have the boot partition */
450	ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
451	if (ret < 0)
452		return ret;
453
454	ret = wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
455	if (ret < 0)
456		return ret;
457
458	ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &chip_id);
459	if (ret < 0)
460		return ret;
461
462	wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
463
464	if (chip_id != wl->chip.id) {
465		wl1271_error("chip id doesn't match after firmware boot");
466		return -EIO;
467	}
468
469	/* wait for init to complete */
470	loop = 0;
471	while (loop++ < INIT_LOOP) {
472		udelay(INIT_LOOP_DELAY);
473		ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
474		if (ret < 0)
475			return ret;
476
477		if (intr == 0xffffffff) {
478			wl1271_error("error reading hardware complete "
479				     "init indication");
480			return -EIO;
481		}
482		/* check that ACX_INTR_INIT_COMPLETE is enabled */
483		else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
484			ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
485					       WL1271_ACX_INTR_INIT_COMPLETE);
486			if (ret < 0)
487				return ret;
488			break;
489		}
490	}
491
492	if (loop > INIT_LOOP) {
493		wl1271_error("timeout waiting for the hardware to "
494			     "complete initialization");
495		return -EIO;
496	}
497
498	/* get hardware config command mail box */
499	ret = wlcore_read_reg(wl, REG_COMMAND_MAILBOX_PTR, &wl->cmd_box_addr);
500	if (ret < 0)
501		return ret;
502
503	wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x", wl->cmd_box_addr);
504
505	/* get hardware config event mail box */
506	ret = wlcore_read_reg(wl, REG_EVENT_MAILBOX_PTR, &wl->mbox_ptr[0]);
507	if (ret < 0)
508		return ret;
509
510	wl->mbox_ptr[1] = wl->mbox_ptr[0] + wl->mbox_size;
511
512	wl1271_debug(DEBUG_MAILBOX, "MBOX ptrs: 0x%x 0x%x",
513		     wl->mbox_ptr[0], wl->mbox_ptr[1]);
514
515	ret = wlcore_boot_static_data(wl);
516	if (ret < 0) {
517		wl1271_error("error getting static data");
518		return ret;
519	}
520
521	/*
522	 * in case of full asynchronous mode the firmware event must be
523	 * ready to receive event from the command mailbox
524	 */
525
526	/* unmask required mbox events  */
527	ret = wl1271_event_unmask(wl);
528	if (ret < 0) {
529		wl1271_error("EVENT mask setting failed");
530		return ret;
531	}
532
533	/* set the working partition to its "running" mode offset */
534	ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
535
536	/* firmware startup completed */
537	return ret;
538}
539EXPORT_SYMBOL_GPL(wlcore_boot_run_firmware);