proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / net / wireless / wl12xx / boot.c
blob4df04f84d7f106a71fd87741d2e857f48bf01cc0
1 /*
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
20 * 02110-1301 USA
24 #include <linux/slab.h>
26 #include "acx.h"
27 #include "reg.h"
28 #include "boot.h"
29 #include "io.h"
30 #include "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_read32(wl, ACX_REG_ECPU_CONTROL);
98 /* 10.5.1 run the firmware (II) */
99 cpu_ctrl |= flag;
100 wl1271_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_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
108 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 (!chunk) {
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_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_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 int ret = 0;
188 u8 *fw;
190 fw = wl->fw;
191 chunks = be32_to_cpup((__be32 *) fw);
192 fw += sizeof(u32);
194 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
196 while (chunks--) {
197 addr = be32_to_cpup((__be32 *) fw);
198 fw += sizeof(u32);
199 len = be32_to_cpup((__be32 *) fw);
200 fw += sizeof(u32);
202 if (len > 300000) {
203 wl1271_info("firmware chunk too long: %u", len);
204 return -EINVAL;
206 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
207 chunks, addr, len);
208 ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
209 if (ret != 0)
210 break;
211 fw += len;
214 return ret;
217 static int wl1271_boot_upload_nvs(struct wl1271 *wl)
219 size_t nvs_len, burst_len;
220 int i;
221 u32 dest_addr, val;
222 u8 *nvs_ptr, *nvs_aligned;
224 if (wl->nvs == NULL)
225 return -ENODEV;
228 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz band
229 * configurations) can be removed when those NVS files stop floating
230 * around.
232 if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
233 wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
234 if (wl->nvs->general_params.dual_mode_select)
235 wl->enable_11a = true;
238 if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
239 (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
240 wl->enable_11a)) {
241 wl1271_error("nvs size is not as expected: %zu != %zu",
242 wl->nvs_len, sizeof(struct wl1271_nvs_file));
243 kfree(wl->nvs);
244 wl->nvs = NULL;
245 wl->nvs_len = 0;
246 return -EILSEQ;
249 /* only the first part of the NVS needs to be uploaded */
250 nvs_len = sizeof(wl->nvs->nvs);
251 nvs_ptr = (u8 *)wl->nvs->nvs;
253 /* update current MAC address to NVS */
254 nvs_ptr[11] = wl->mac_addr[0];
255 nvs_ptr[10] = wl->mac_addr[1];
256 nvs_ptr[6] = wl->mac_addr[2];
257 nvs_ptr[5] = wl->mac_addr[3];
258 nvs_ptr[4] = wl->mac_addr[4];
259 nvs_ptr[3] = wl->mac_addr[5];
262 * Layout before the actual NVS tables:
263 * 1 byte : burst length.
264 * 2 bytes: destination address.
265 * n bytes: data to burst copy.
267 * This is ended by a 0 length, then the NVS tables.
270 /* FIXME: Do we need to check here whether the LSB is 1? */
271 while (nvs_ptr[0]) {
272 burst_len = nvs_ptr[0];
273 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
276 * Due to our new wl1271_translate_reg_addr function,
277 * we need to add the REGISTER_BASE to the destination
279 dest_addr += REGISTERS_BASE;
281 /* We move our pointer to the data */
282 nvs_ptr += 3;
284 for (i = 0; i < burst_len; i++) {
285 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
286 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
288 wl1271_debug(DEBUG_BOOT,
289 "nvs burst write 0x%x: 0x%x",
290 dest_addr, val);
291 wl1271_write32(wl, dest_addr, val);
293 nvs_ptr += 4;
294 dest_addr += 4;
299 * We've reached the first zero length, the first NVS table
300 * is located at an aligned offset which is at least 7 bytes further.
302 nvs_ptr = (u8 *)wl->nvs->nvs +
303 ALIGN(nvs_ptr - (u8 *)wl->nvs->nvs + 7, 4);
304 nvs_len -= nvs_ptr - (u8 *)wl->nvs->nvs;
306 /* Now we must set the partition correctly */
307 wl1271_set_partition(wl, &part_table[PART_WORK]);
309 /* Copy the NVS tables to a new block to ensure alignment */
310 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
311 if (!nvs_aligned)
312 return -ENOMEM;
314 /* And finally we upload the NVS tables */
315 wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
317 kfree(nvs_aligned);
318 return 0;
321 static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
323 wl1271_enable_interrupts(wl);
324 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
325 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
326 wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
329 static int wl1271_boot_soft_reset(struct wl1271 *wl)
331 unsigned long timeout;
332 u32 boot_data;
334 /* perform soft reset */
335 wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
337 /* SOFT_RESET is self clearing */
338 timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
339 while (1) {
340 boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
341 wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
342 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
343 break;
345 if (time_after(jiffies, timeout)) {
346 /* 1.2 check pWhalBus->uSelfClearTime if the
347 * timeout was reached */
348 wl1271_error("soft reset timeout");
349 return -1;
352 udelay(SOFT_RESET_STALL_TIME);
355 /* disable Rx/Tx */
356 wl1271_write32(wl, ENABLE, 0x0);
358 /* disable auto calibration on start*/
359 wl1271_write32(wl, SPARE_A2, 0xffff);
361 return 0;
364 static int wl1271_boot_run_firmware(struct wl1271 *wl)
366 int loop, ret;
367 u32 chip_id, intr;
369 wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
371 chip_id = wl1271_read32(wl, CHIP_ID_B);
373 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
375 if (chip_id != wl->chip.id) {
376 wl1271_error("chip id doesn't match after firmware boot");
377 return -EIO;
380 /* wait for init to complete */
381 loop = 0;
382 while (loop++ < INIT_LOOP) {
383 udelay(INIT_LOOP_DELAY);
384 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
386 if (intr == 0xffffffff) {
387 wl1271_error("error reading hardware complete "
388 "init indication");
389 return -EIO;
391 /* check that ACX_INTR_INIT_COMPLETE is enabled */
392 else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
393 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
394 WL1271_ACX_INTR_INIT_COMPLETE);
395 break;
399 if (loop > INIT_LOOP) {
400 wl1271_error("timeout waiting for the hardware to "
401 "complete initialization");
402 return -EIO;
405 /* get hardware config command mail box */
406 wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
408 /* get hardware config event mail box */
409 wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
411 /* set the working partition to its "running" mode offset */
412 wl1271_set_partition(wl, &part_table[PART_WORK]);
414 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
415 wl->cmd_box_addr, wl->event_box_addr);
417 wl1271_boot_fw_version(wl);
420 * in case of full asynchronous mode the firmware event must be
421 * ready to receive event from the command mailbox
424 /* unmask required mbox events */
425 wl->event_mask = BSS_LOSE_EVENT_ID |
426 SCAN_COMPLETE_EVENT_ID |
427 PS_REPORT_EVENT_ID |
428 JOIN_EVENT_COMPLETE_ID |
429 DISCONNECT_EVENT_COMPLETE_ID |
430 RSSI_SNR_TRIGGER_0_EVENT_ID |
431 PSPOLL_DELIVERY_FAILURE_EVENT_ID |
432 SOFT_GEMINI_SENSE_EVENT_ID;
434 ret = wl1271_event_unmask(wl);
435 if (ret < 0) {
436 wl1271_error("EVENT mask setting failed");
437 return ret;
440 wl1271_event_mbox_config(wl);
442 /* firmware startup completed */
443 return 0;
446 static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
448 u32 polarity;
450 polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
452 /* We use HIGH polarity, so unset the LOW bit */
453 polarity &= ~POLARITY_LOW;
454 wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
456 return 0;
459 static void wl1271_boot_hw_version(struct wl1271 *wl)
461 u32 fuse;
463 fuse = wl1271_top_reg_read(wl, REG_FUSE_DATA_2_1);
464 fuse = (fuse & PG_VER_MASK) >> PG_VER_OFFSET;
466 wl->hw_pg_ver = (s8)fuse;
469 /* uploads NVS and firmware */
470 int wl1271_load_firmware(struct wl1271 *wl)
472 int ret = 0;
473 u32 tmp, clk, pause;
475 wl1271_boot_hw_version(wl);
477 if (wl->ref_clock == 0 || wl->ref_clock == 2 || wl->ref_clock == 4)
478 /* ref clk: 19.2/38.4/38.4-XTAL */
479 clk = 0x3;
480 else if (wl->ref_clock == 1 || wl->ref_clock == 3)
481 /* ref clk: 26/52 */
482 clk = 0x5;
483 else
484 return -EINVAL;
486 if (wl->ref_clock != 0) {
487 u16 val;
488 /* Set clock type (open drain) */
489 val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
490 val &= FREF_CLK_TYPE_BITS;
491 wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
493 /* Set clock pull mode (no pull) */
494 val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
495 val |= NO_PULL;
496 wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
497 } else {
498 u16 val;
499 /* Set clock polarity */
500 val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
501 val &= FREF_CLK_POLARITY_BITS;
502 val |= CLK_REQ_OUTN_SEL;
503 wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
506 wl1271_write32(wl, PLL_PARAMETERS, clk);
508 pause = wl1271_read32(wl, PLL_PARAMETERS);
510 wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
512 pause &= ~(WU_COUNTER_PAUSE_VAL);
513 pause |= WU_COUNTER_PAUSE_VAL;
514 wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
516 /* Continue the ELP wake up sequence */
517 wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
518 udelay(500);
520 wl1271_set_partition(wl, &part_table[PART_DRPW]);
522 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
523 to be used by DRPw FW. The RTRIM value will be added by the FW
524 before taking DRPw out of reset */
526 wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
527 clk = wl1271_read32(wl, DRPW_SCRATCH_START);
529 wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
531 clk |= (wl->ref_clock << 1) << 4;
532 wl1271_write32(wl, DRPW_SCRATCH_START, clk);
534 wl1271_set_partition(wl, &part_table[PART_WORK]);
536 /* Disable interrupts */
537 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
539 ret = wl1271_boot_soft_reset(wl);
540 if (ret < 0)
541 goto out;
543 /* 2. start processing NVS file */
544 ret = wl1271_boot_upload_nvs(wl);
545 if (ret < 0)
546 goto out;
548 /* write firmware's last address (ie. it's length) to
549 * ACX_EEPROMLESS_IND_REG */
550 wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
552 wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
554 tmp = wl1271_read32(wl, CHIP_ID_B);
556 wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
558 /* 6. read the EEPROM parameters */
559 tmp = wl1271_read32(wl, SCR_PAD2);
561 ret = wl1271_boot_write_irq_polarity(wl);
562 if (ret < 0)
563 goto out;
565 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
566 WL1271_ACX_ALL_EVENTS_VECTOR);
568 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
569 * to upload_fw) */
571 ret = wl1271_boot_upload_firmware(wl);
572 if (ret < 0)
573 goto out;
575 out:
576 return ret;
578 EXPORT_SYMBOL_GPL(wl1271_load_firmware);
580 int wl1271_boot(struct wl1271 *wl)
582 int ret;
584 /* upload NVS and firmware */
585 ret = wl1271_load_firmware(wl);
586 if (ret)
587 return ret;
589 /* 10.5 start firmware */
590 ret = wl1271_boot_run_firmware(wl);
591 if (ret < 0)
592 goto out;
594 /* Enable firmware interrupts now */
595 wl1271_boot_enable_interrupts(wl);
597 /* set the wl1271 default filters */
598 wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
599 wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
601 wl1271_event_mbox_config(wl);
603 out:
604 return ret;