mm-only debug patch...
[mmotm.git] / drivers / net / wireless / wl12xx / wl1271_boot.c
bloba27601dc9c0dc565bcaee6944340bc05727b5d10
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 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
20 * 02110-1301 USA
24 #include <linux/gpio.h>
26 #include "wl1271_acx.h"
27 #include "wl1271_reg.h"
28 #include "wl1271_boot.h"
29 #include "wl1271_spi.h"
30 #include "wl1271_event.h"
32 static struct wl1271_partition_set part_table[PART_TABLE_LEN] = {
33 [PART_DOWN] = {
34 .mem = {
35 .start = 0x00000000,
36 .size = 0x000177c0
38 .reg = {
39 .start = REGISTERS_BASE,
40 .size = 0x00008800
42 .mem2 = {
43 .start = 0x00000000,
44 .size = 0x00000000
46 .mem3 = {
47 .start = 0x00000000,
48 .size = 0x00000000
52 [PART_WORK] = {
53 .mem = {
54 .start = 0x00040000,
55 .size = 0x00014fc0
57 .reg = {
58 .start = REGISTERS_BASE,
59 .size = 0x0000a000
61 .mem2 = {
62 .start = 0x003004f8,
63 .size = 0x00000004
65 .mem3 = {
66 .start = 0x00040404,
67 .size = 0x00000000
71 [PART_DRPW] = {
72 .mem = {
73 .start = 0x00040000,
74 .size = 0x00014fc0
76 .reg = {
77 .start = DRPW_BASE,
78 .size = 0x00006000
80 .mem2 = {
81 .start = 0x00000000,
82 .size = 0x00000000
84 .mem3 = {
85 .start = 0x00000000,
86 .size = 0x00000000
91 static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
93 u32 cpu_ctrl;
95 /* 10.5.0 run the firmware (I) */
96 cpu_ctrl = wl1271_spi_read32(wl, ACX_REG_ECPU_CONTROL);
98 /* 10.5.1 run the firmware (II) */
99 cpu_ctrl |= flag;
100 wl1271_spi_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
103 static void wl1271_boot_fw_version(struct wl1271 *wl)
105 struct wl1271_static_data static_data;
107 wl1271_spi_read(wl, wl->cmd_box_addr,
108 &static_data, sizeof(static_data), false);
110 strncpy(wl->chip.fw_ver, static_data.fw_version,
111 sizeof(wl->chip.fw_ver));
113 /* make sure the string is NULL-terminated */
114 wl->chip.fw_ver[sizeof(wl->chip.fw_ver) - 1] = '\0';
117 static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
118 size_t fw_data_len, u32 dest)
120 struct wl1271_partition_set partition;
121 int addr, chunk_num, partition_limit;
122 u8 *p, *chunk;
124 /* whal_FwCtrl_LoadFwImageSm() */
126 wl1271_debug(DEBUG_BOOT, "starting firmware upload");
128 wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
129 fw_data_len, CHUNK_SIZE);
131 if ((fw_data_len % 4) != 0) {
132 wl1271_error("firmware length not multiple of four");
133 return -EIO;
136 chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
137 if (!buf) {
138 wl1271_error("allocation for firmware upload chunk failed");
139 return -ENOMEM;
142 memcpy(&partition, &part_table[PART_DOWN], sizeof(partition));
143 partition.mem.start = dest;
144 wl1271_set_partition(wl, &partition);
146 /* 10.1 set partition limit and chunk num */
147 chunk_num = 0;
148 partition_limit = part_table[PART_DOWN].mem.size;
150 while (chunk_num < fw_data_len / CHUNK_SIZE) {
151 /* 10.2 update partition, if needed */
152 addr = dest + (chunk_num + 2) * CHUNK_SIZE;
153 if (addr > partition_limit) {
154 addr = dest + chunk_num * CHUNK_SIZE;
155 partition_limit = chunk_num * CHUNK_SIZE +
156 part_table[PART_DOWN].mem.size;
157 partition.mem.start = addr;
158 wl1271_set_partition(wl, &partition);
161 /* 10.3 upload the chunk */
162 addr = dest + chunk_num * CHUNK_SIZE;
163 p = buf + chunk_num * CHUNK_SIZE;
164 memcpy(chunk, p, CHUNK_SIZE);
165 wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
166 p, addr);
167 wl1271_spi_write(wl, addr, chunk, CHUNK_SIZE, false);
169 chunk_num++;
172 /* 10.4 upload the last chunk */
173 addr = dest + chunk_num * CHUNK_SIZE;
174 p = buf + chunk_num * CHUNK_SIZE;
175 memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
176 wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
177 fw_data_len % CHUNK_SIZE, p, addr);
178 wl1271_spi_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
180 kfree(chunk);
181 return 0;
184 static int wl1271_boot_upload_firmware(struct wl1271 *wl)
186 u32 chunks, addr, len;
187 u8 *fw;
189 fw = wl->fw;
190 chunks = be32_to_cpup((u32 *) fw);
191 fw += sizeof(u32);
193 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
195 while (chunks--) {
196 addr = be32_to_cpup((u32 *) fw);
197 fw += sizeof(u32);
198 len = be32_to_cpup((u32 *) fw);
199 fw += sizeof(u32);
201 if (len > 300000) {
202 wl1271_info("firmware chunk too long: %u", len);
203 return -EINVAL;
205 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
206 chunks, addr, len);
207 wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
208 fw += len;
211 return 0;
214 static int wl1271_boot_upload_nvs(struct wl1271 *wl)
216 size_t nvs_len, burst_len;
217 int i;
218 u32 dest_addr, val;
219 u8 *nvs_ptr, *nvs, *nvs_aligned;
221 nvs = wl->nvs;
222 if (nvs == NULL)
223 return -ENODEV;
225 nvs_ptr = nvs;
227 nvs_len = wl->nvs_len;
229 /* Update the device MAC address into the nvs */
230 nvs[11] = wl->mac_addr[0];
231 nvs[10] = wl->mac_addr[1];
232 nvs[6] = wl->mac_addr[2];
233 nvs[5] = wl->mac_addr[3];
234 nvs[4] = wl->mac_addr[4];
235 nvs[3] = wl->mac_addr[5];
238 * Layout before the actual NVS tables:
239 * 1 byte : burst length.
240 * 2 bytes: destination address.
241 * n bytes: data to burst copy.
243 * This is ended by a 0 length, then the NVS tables.
246 /* FIXME: Do we need to check here whether the LSB is 1? */
247 while (nvs_ptr[0]) {
248 burst_len = nvs_ptr[0];
249 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
251 /* FIXME: Due to our new wl1271_translate_reg_addr function,
252 we need to add the REGISTER_BASE to the destination */
253 dest_addr += REGISTERS_BASE;
255 /* We move our pointer to the data */
256 nvs_ptr += 3;
258 for (i = 0; i < burst_len; i++) {
259 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
260 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
262 wl1271_debug(DEBUG_BOOT,
263 "nvs burst write 0x%x: 0x%x",
264 dest_addr, val);
265 wl1271_spi_write32(wl, dest_addr, val);
267 nvs_ptr += 4;
268 dest_addr += 4;
273 * We've reached the first zero length, the first NVS table
274 * is 7 bytes further.
276 nvs_ptr += 7;
277 nvs_len -= nvs_ptr - nvs;
278 nvs_len = ALIGN(nvs_len, 4);
280 /* FIXME: The driver sets the partition here, but this is not needed,
281 since it sets to the same one as currently in use */
282 /* Now we must set the partition correctly */
283 wl1271_set_partition(wl, &part_table[PART_WORK]);
285 /* Copy the NVS tables to a new block to ensure alignment */
286 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
288 /* And finally we upload the NVS tables */
289 /* FIXME: In wl1271, we upload everything at once.
290 No endianness handling needed here?! The ref driver doesn't do
291 anything about it at this point */
292 wl1271_spi_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
294 kfree(nvs_aligned);
295 return 0;
298 static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
300 enable_irq(wl->irq);
301 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
302 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
303 wl1271_spi_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
306 static int wl1271_boot_soft_reset(struct wl1271 *wl)
308 unsigned long timeout;
309 u32 boot_data;
311 /* perform soft reset */
312 wl1271_spi_write32(wl, ACX_REG_SLV_SOFT_RESET,
313 ACX_SLV_SOFT_RESET_BIT);
315 /* SOFT_RESET is self clearing */
316 timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
317 while (1) {
318 boot_data = wl1271_spi_read32(wl, ACX_REG_SLV_SOFT_RESET);
319 wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
320 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
321 break;
323 if (time_after(jiffies, timeout)) {
324 /* 1.2 check pWhalBus->uSelfClearTime if the
325 * timeout was reached */
326 wl1271_error("soft reset timeout");
327 return -1;
330 udelay(SOFT_RESET_STALL_TIME);
333 /* disable Rx/Tx */
334 wl1271_spi_write32(wl, ENABLE, 0x0);
336 /* disable auto calibration on start*/
337 wl1271_spi_write32(wl, SPARE_A2, 0xffff);
339 return 0;
342 static int wl1271_boot_run_firmware(struct wl1271 *wl)
344 int loop, ret;
345 u32 chip_id, interrupt;
347 wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
349 chip_id = wl1271_spi_read32(wl, CHIP_ID_B);
351 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
353 if (chip_id != wl->chip.id) {
354 wl1271_error("chip id doesn't match after firmware boot");
355 return -EIO;
358 /* wait for init to complete */
359 loop = 0;
360 while (loop++ < INIT_LOOP) {
361 udelay(INIT_LOOP_DELAY);
362 interrupt = wl1271_spi_read32(wl,
363 ACX_REG_INTERRUPT_NO_CLEAR);
365 if (interrupt == 0xffffffff) {
366 wl1271_error("error reading hardware complete "
367 "init indication");
368 return -EIO;
370 /* check that ACX_INTR_INIT_COMPLETE is enabled */
371 else if (interrupt & WL1271_ACX_INTR_INIT_COMPLETE) {
372 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
373 WL1271_ACX_INTR_INIT_COMPLETE);
374 break;
378 if (loop >= INIT_LOOP) {
379 wl1271_error("timeout waiting for the hardware to "
380 "complete initialization");
381 return -EIO;
384 /* get hardware config command mail box */
385 wl->cmd_box_addr = wl1271_spi_read32(wl, REG_COMMAND_MAILBOX_PTR);
387 /* get hardware config event mail box */
388 wl->event_box_addr = wl1271_spi_read32(wl, REG_EVENT_MAILBOX_PTR);
390 /* set the working partition to its "running" mode offset */
391 wl1271_set_partition(wl, &part_table[PART_WORK]);
393 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
394 wl->cmd_box_addr, wl->event_box_addr);
396 wl1271_boot_fw_version(wl);
399 * in case of full asynchronous mode the firmware event must be
400 * ready to receive event from the command mailbox
403 /* enable gpio interrupts */
404 wl1271_boot_enable_interrupts(wl);
406 /* unmask required mbox events */
407 wl->event_mask = BSS_LOSE_EVENT_ID |
408 SCAN_COMPLETE_EVENT_ID;
410 ret = wl1271_event_unmask(wl);
411 if (ret < 0) {
412 wl1271_error("EVENT mask setting failed");
413 return ret;
416 wl1271_event_mbox_config(wl);
418 /* firmware startup completed */
419 return 0;
422 static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
424 u32 polarity;
426 polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
428 /* We use HIGH polarity, so unset the LOW bit */
429 polarity &= ~POLARITY_LOW;
430 wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
432 return 0;
435 int wl1271_boot(struct wl1271 *wl)
437 int ret = 0;
438 u32 tmp, clk, pause;
440 if (REF_CLOCK == 0 || REF_CLOCK == 2 || REF_CLOCK == 4)
441 /* ref clk: 19.2/38.4/38.4-XTAL */
442 clk = 0x3;
443 else if (REF_CLOCK == 1 || REF_CLOCK == 3)
444 /* ref clk: 26/52 */
445 clk = 0x5;
447 if (REF_CLOCK != 0) {
448 u16 val;
449 /* Set clock type */
450 val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
451 val &= FREF_CLK_TYPE_BITS;
452 val |= CLK_REQ_PRCM;
453 wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
454 } else {
455 u16 val;
456 /* Set clock polarity */
457 val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
458 val &= FREF_CLK_POLARITY_BITS;
459 val |= CLK_REQ_OUTN_SEL;
460 wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
463 wl1271_spi_write32(wl, PLL_PARAMETERS, clk);
465 pause = wl1271_spi_read32(wl, PLL_PARAMETERS);
467 wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
469 pause &= ~(WU_COUNTER_PAUSE_VAL); /* FIXME: This should probably be
470 * WU_COUNTER_PAUSE_VAL instead of
471 * 0x3ff (magic number ). How does
472 * this work?! */
473 pause |= WU_COUNTER_PAUSE_VAL;
474 wl1271_spi_write32(wl, WU_COUNTER_PAUSE, pause);
476 /* Continue the ELP wake up sequence */
477 wl1271_spi_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
478 udelay(500);
480 wl1271_set_partition(wl, &part_table[PART_DRPW]);
482 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
483 to be used by DRPw FW. The RTRIM value will be added by the FW
484 before taking DRPw out of reset */
486 wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
487 clk = wl1271_spi_read32(wl, DRPW_SCRATCH_START);
489 wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
491 /* 2 */
492 clk |= (REF_CLOCK << 1) << 4;
493 wl1271_spi_write32(wl, DRPW_SCRATCH_START, clk);
495 wl1271_set_partition(wl, &part_table[PART_WORK]);
497 /* Disable interrupts */
498 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
500 ret = wl1271_boot_soft_reset(wl);
501 if (ret < 0)
502 goto out;
504 /* 2. start processing NVS file */
505 ret = wl1271_boot_upload_nvs(wl);
506 if (ret < 0)
507 goto out;
509 /* write firmware's last address (ie. it's length) to
510 * ACX_EEPROMLESS_IND_REG */
511 wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
513 wl1271_spi_write32(wl, ACX_EEPROMLESS_IND_REG,
514 ACX_EEPROMLESS_IND_REG);
516 tmp = wl1271_spi_read32(wl, CHIP_ID_B);
518 wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
520 /* 6. read the EEPROM parameters */
521 tmp = wl1271_spi_read32(wl, SCR_PAD2);
523 ret = wl1271_boot_write_irq_polarity(wl);
524 if (ret < 0)
525 goto out;
527 /* FIXME: Need to check whether this is really what we want */
528 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
529 WL1271_ACX_ALL_EVENTS_VECTOR);
531 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
532 * to upload_fw) */
534 ret = wl1271_boot_upload_firmware(wl);
535 if (ret < 0)
536 goto out;
538 /* 10.5 start firmware */
539 ret = wl1271_boot_run_firmware(wl);
540 if (ret < 0)
541 goto out;
543 /* set the wl1271 default filters */
544 wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
545 wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
547 wl1271_event_mbox_config(wl);
549 out:
550 return ret;