[go: nahoru, domu]

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/wl12xx.h>
26#include <linux/export.h>
27
28#include "debug.h"
29#include "acx.h"
30#include "boot.h"
31#include "io.h"
32#include "event.h"
33#include "rx.h"
34#include "hw_ops.h"
35
36static int wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
37{
38	u32 cpu_ctrl;
39	int ret;
40
41	/* 10.5.0 run the firmware (I) */
42	ret = wlcore_read_reg(wl, REG_ECPU_CONTROL, &cpu_ctrl);
43	if (ret < 0)
44		goto out;
45
46	/* 10.5.1 run the firmware (II) */
47	cpu_ctrl |= flag;
48	ret = wlcore_write_reg(wl, REG_ECPU_CONTROL, cpu_ctrl);
49
50out:
51	return ret;
52}
53
54static int wlcore_boot_parse_fw_ver(struct wl1271 *wl,
55				    struct wl1271_static_data *static_data)
56{
57	int ret;
58
59	strncpy(wl->chip.fw_ver_str, static_data->fw_version,
60		sizeof(wl->chip.fw_ver_str));
61
62	/* make sure the string is NULL-terminated */
63	wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
64
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],
68		     &wl->chip.fw_ver[4]);
69
70	if (ret != 5) {
71		wl1271_warning("fw version incorrect value");
72		memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
73		ret = -EINVAL;
74		goto out;
75	}
76
77	ret = wlcore_identify_fw(wl);
78	if (ret < 0)
79		goto out;
80out:
81	return ret;
82}
83
84static int wlcore_validate_fw_ver(struct wl1271 *wl)
85{
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] = "";
90	int i;
91
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]))
95		goto fail;
96
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]))
100		goto fail;
101
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]))
105		goto fail;
106
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]))
110		goto fail;
111
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])))
118		goto fail;
119
120	return 0;
121
122fail:
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),
126				  "%s*.", min_fw_str);
127		else
128			snprintf(min_fw_str, sizeof(min_fw_str),
129				  "%s%u.", min_fw_str, min_ver[i]);
130
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);
138	return -EINVAL;
139}
140
141static int wlcore_boot_static_data(struct wl1271 *wl)
142{
143	struct wl1271_static_data *static_data;
144	size_t len = sizeof(*static_data) + wl->static_data_priv_len;
145	int ret;
146
147	static_data = kmalloc(len, GFP_KERNEL);
148	if (!static_data) {
149		ret = -ENOMEM;
150		goto out;
151	}
152
153	ret = wlcore_read(wl, wl->cmd_box_addr, static_data, len, false);
154	if (ret < 0)
155		goto out_free;
156
157	ret = wlcore_boot_parse_fw_ver(wl, static_data);
158	if (ret < 0)
159		goto out_free;
160
161	ret = wlcore_validate_fw_ver(wl);
162	if (ret < 0)
163		goto out_free;
164
165	ret = wlcore_handle_static_data(wl, static_data);
166	if (ret < 0)
167		goto out_free;
168
169out_free:
170	kfree(static_data);
171out:
172	return ret;
173}
174
175static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
176					     size_t fw_data_len, u32 dest)
177{
178	struct wlcore_partition_set partition;
179	int addr, chunk_num, partition_limit;
180	u8 *p, *chunk;
181	int ret;
182
183	/* whal_FwCtrl_LoadFwImageSm() */
184
185	wl1271_debug(DEBUG_BOOT, "starting firmware upload");
186
187	wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
188		     fw_data_len, CHUNK_SIZE);
189
190	if ((fw_data_len % 4) != 0) {
191		wl1271_error("firmware length not multiple of four");
192		return -EIO;
193	}
194
195	chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
196	if (!chunk) {
197		wl1271_error("allocation for firmware upload chunk failed");
198		return -ENOMEM;
199	}
200
201	memcpy(&partition, &wl->ptable[PART_DOWN], sizeof(partition));
202	partition.mem.start = dest;
203	ret = wlcore_set_partition(wl, &partition);
204	if (ret < 0)
205		goto out;
206
207	/* 10.1 set partition limit and chunk num */
208	chunk_num = 0;
209	partition_limit = wl->ptable[PART_DOWN].mem.size;
210
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);
220			if (ret < 0)
221				goto out;
222		}
223
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",
229			     p, addr);
230		ret = wlcore_write(wl, addr, chunk, CHUNK_SIZE, false);
231		if (ret < 0)
232			goto out;
233
234		chunk_num++;
235	}
236
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);
244
245out:
246	kfree(chunk);
247	return ret;
248}
249
250int wlcore_boot_upload_firmware(struct wl1271 *wl)
251{
252	u32 chunks, addr, len;
253	int ret = 0;
254	u8 *fw;
255
256	fw = wl->fw;
257	chunks = be32_to_cpup((__be32 *) fw);
258	fw += sizeof(u32);
259
260	wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
261
262	while (chunks--) {
263		addr = be32_to_cpup((__be32 *) fw);
264		fw += sizeof(u32);
265		len = be32_to_cpup((__be32 *) fw);
266		fw += sizeof(u32);
267
268		if (len > 300000) {
269			wl1271_info("firmware chunk too long: %u", len);
270			return -EINVAL;
271		}
272		wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
273			     chunks, addr, len);
274		ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
275		if (ret != 0)
276			break;
277		fw += len;
278	}
279
280	return ret;
281}
282EXPORT_SYMBOL_GPL(wlcore_boot_upload_firmware);
283
284int wlcore_boot_upload_nvs(struct wl1271 *wl)
285{
286	size_t nvs_len, burst_len;
287	int i;
288	u32 dest_addr, val;
289	u8 *nvs_ptr, *nvs_aligned;
290	int ret;
291
292	if (wl->nvs == NULL) {
293		wl1271_error("NVS file is needed during boot");
294		return -ENODEV;
295	}
296
297	if (wl->quirks & WLCORE_QUIRK_LEGACY_NVS) {
298		struct wl1271_nvs_file *nvs =
299			(struct wl1271_nvs_file *)wl->nvs;
300		/*
301		 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
302		 * band configurations) can be removed when those NVS files stop
303		 * floating around.
304		 */
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;
309		}
310
311		if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
312		    (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
313		     wl->enable_11a)) {
314			wl1271_error("nvs size is not as expected: %zu != %zu",
315				wl->nvs_len, sizeof(struct wl1271_nvs_file));
316			kfree(wl->nvs);
317			wl->nvs = NULL;
318			wl->nvs_len = 0;
319			return -EILSEQ;
320		}
321
322		/* only the first part of the NVS needs to be uploaded */
323		nvs_len = sizeof(nvs->nvs);
324		nvs_ptr = (u8 *) nvs->nvs;
325	} else {
326		struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
327
328		if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
329			if (nvs->general_params.dual_mode_select)
330				wl->enable_11a = true;
331		} else {
332			wl1271_error("nvs size is not as expected: %zu != %zu",
333				     wl->nvs_len,
334				     sizeof(struct wl128x_nvs_file));
335			kfree(wl->nvs);
336			wl->nvs = NULL;
337			wl->nvs_len = 0;
338			return -EILSEQ;
339		}
340
341		/* only the first part of the NVS needs to be uploaded */
342		nvs_len = sizeof(nvs->nvs);
343		nvs_ptr = (u8 *)nvs->nvs;
344	}
345
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];
353
354	/*
355	 * Layout before the actual NVS tables:
356	 * 1 byte : burst length.
357	 * 2 bytes: destination address.
358	 * n bytes: data to burst copy.
359	 *
360	 * This is ended by a 0 length, then the NVS tables.
361	 */
362
363	/* FIXME: Do we need to check here whether the LSB is 1? */
364	while (nvs_ptr[0]) {
365		burst_len = nvs_ptr[0];
366		dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
367
368		/*
369		 * Due to our new wl1271_translate_reg_addr function,
370		 * we need to add the register partition start address
371		 * to the destination
372		 */
373		dest_addr += wl->curr_part.reg.start;
374
375		/* We move our pointer to the data */
376		nvs_ptr += 3;
377
378		for (i = 0; i < burst_len; i++) {
379			if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
380				goto out_badnvs;
381
382			val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
383			       | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
384
385			wl1271_debug(DEBUG_BOOT,
386				     "nvs burst write 0x%x: 0x%x",
387				     dest_addr, val);
388			ret = wlcore_write32(wl, dest_addr, val);
389			if (ret < 0)
390				return ret;
391
392			nvs_ptr += 4;
393			dest_addr += 4;
394		}
395
396		if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
397			goto out_badnvs;
398	}
399
400	/*
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.
406	 */
407	nvs_ptr = (u8 *)wl->nvs +
408			ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
409
410	if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
411		goto out_badnvs;
412
413	nvs_len -= nvs_ptr - (u8 *)wl->nvs;
414
415	/* Now we must set the partition correctly */
416	ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
417	if (ret < 0)
418		return ret;
419
420	/* Copy the NVS tables to a new block to ensure alignment */
421	nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
422	if (!nvs_aligned)
423		return -ENOMEM;
424
425	/* And finally we upload the NVS tables */
426	ret = wlcore_write_data(wl, REG_CMD_MBOX_ADDRESS, nvs_aligned, nvs_len,
427				false);
428
429	kfree(nvs_aligned);
430	return ret;
431
432out_badnvs:
433	wl1271_error("nvs data is malformed");
434	return -EILSEQ;
435}
436EXPORT_SYMBOL_GPL(wlcore_boot_upload_nvs);
437
438int wlcore_boot_run_firmware(struct wl1271 *wl)
439{
440	int loop, ret;
441	u32 chip_id, intr;
442
443	/* Make sure we have the boot partition */
444	ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
445	if (ret < 0)
446		return ret;
447
448	ret = wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
449	if (ret < 0)
450		return ret;
451
452	ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &chip_id);
453	if (ret < 0)
454		return ret;
455
456	wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
457
458	if (chip_id != wl->chip.id) {
459		wl1271_error("chip id doesn't match after firmware boot");
460		return -EIO;
461	}
462
463	/* wait for init to complete */
464	loop = 0;
465	while (loop++ < INIT_LOOP) {
466		udelay(INIT_LOOP_DELAY);
467		ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
468		if (ret < 0)
469			return ret;
470
471		if (intr == 0xffffffff) {
472			wl1271_error("error reading hardware complete "
473				     "init indication");
474			return -EIO;
475		}
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);
480			if (ret < 0)
481				return ret;
482			break;
483		}
484	}
485
486	if (loop > INIT_LOOP) {
487		wl1271_error("timeout waiting for the hardware to "
488			     "complete initialization");
489		return -EIO;
490	}
491
492	/* get hardware config command mail box */
493	ret = wlcore_read_reg(wl, REG_COMMAND_MAILBOX_PTR, &wl->cmd_box_addr);
494	if (ret < 0)
495		return ret;
496
497	wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x", wl->cmd_box_addr);
498
499	/* get hardware config event mail box */
500	ret = wlcore_read_reg(wl, REG_EVENT_MAILBOX_PTR, &wl->mbox_ptr[0]);
501	if (ret < 0)
502		return ret;
503
504	wl->mbox_ptr[1] = wl->mbox_ptr[0] + wl->mbox_size;
505
506	wl1271_debug(DEBUG_MAILBOX, "MBOX ptrs: 0x%x 0x%x",
507		     wl->mbox_ptr[0], wl->mbox_ptr[1]);
508
509	ret = wlcore_boot_static_data(wl);
510	if (ret < 0) {
511		wl1271_error("error getting static data");
512		return ret;
513	}
514
515	/*
516	 * in case of full asynchronous mode the firmware event must be
517	 * ready to receive event from the command mailbox
518	 */
519
520	/* unmask required mbox events  */
521	ret = wl1271_event_unmask(wl);
522	if (ret < 0) {
523		wl1271_error("EVENT mask setting failed");
524		return ret;
525	}
526
527	/* set the working partition to its "running" mode offset */
528	ret = wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
529
530	/* firmware startup completed */
531	return ret;
532}
533EXPORT_SYMBOL_GPL(wlcore_boot_run_firmware);
534