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
24 #include <linux/slab.h>
33 static struct wl1271_partition_set part_table
[PART_TABLE_LEN
] = {
40 .start
= REGISTERS_BASE
,
59 .start
= REGISTERS_BASE
,
92 static void wl1271_boot_set_ecpu_ctrl(struct wl1271
*wl
, u32 flag
)
96 /* 10.5.0 run the firmware (I) */
97 cpu_ctrl
= wl1271_read32(wl
, ACX_REG_ECPU_CONTROL
);
99 /* 10.5.1 run the firmware (II) */
101 wl1271_write32(wl
, ACX_REG_ECPU_CONTROL
, cpu_ctrl
);
104 static void wl1271_parse_fw_ver(struct wl1271
*wl
)
108 ret
= sscanf(wl
->chip
.fw_ver_str
+ 4, "%u.%u.%u.%u.%u",
109 &wl
->chip
.fw_ver
[0], &wl
->chip
.fw_ver
[1],
110 &wl
->chip
.fw_ver
[2], &wl
->chip
.fw_ver
[3],
111 &wl
->chip
.fw_ver
[4]);
114 wl1271_warning("fw version incorrect value");
115 memset(wl
->chip
.fw_ver
, 0, sizeof(wl
->chip
.fw_ver
));
120 static void wl1271_boot_fw_version(struct wl1271
*wl
)
122 struct wl1271_static_data static_data
;
124 wl1271_read(wl
, wl
->cmd_box_addr
, &static_data
, sizeof(static_data
),
127 strncpy(wl
->chip
.fw_ver_str
, static_data
.fw_version
,
128 sizeof(wl
->chip
.fw_ver_str
));
130 /* make sure the string is NULL-terminated */
131 wl
->chip
.fw_ver_str
[sizeof(wl
->chip
.fw_ver_str
) - 1] = '\0';
133 wl1271_parse_fw_ver(wl
);
136 static int wl1271_boot_upload_firmware_chunk(struct wl1271
*wl
, void *buf
,
137 size_t fw_data_len
, u32 dest
)
139 struct wl1271_partition_set partition
;
140 int addr
, chunk_num
, partition_limit
;
143 /* whal_FwCtrl_LoadFwImageSm() */
145 wl1271_debug(DEBUG_BOOT
, "starting firmware upload");
147 wl1271_debug(DEBUG_BOOT
, "fw_data_len %zd chunk_size %d",
148 fw_data_len
, CHUNK_SIZE
);
150 if ((fw_data_len
% 4) != 0) {
151 wl1271_error("firmware length not multiple of four");
155 chunk
= kmalloc(CHUNK_SIZE
, GFP_KERNEL
);
157 wl1271_error("allocation for firmware upload chunk failed");
161 memcpy(&partition
, &part_table
[PART_DOWN
], sizeof(partition
));
162 partition
.mem
.start
= dest
;
163 wl1271_set_partition(wl
, &partition
);
165 /* 10.1 set partition limit and chunk num */
167 partition_limit
= part_table
[PART_DOWN
].mem
.size
;
169 while (chunk_num
< fw_data_len
/ CHUNK_SIZE
) {
170 /* 10.2 update partition, if needed */
171 addr
= dest
+ (chunk_num
+ 2) * CHUNK_SIZE
;
172 if (addr
> partition_limit
) {
173 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
174 partition_limit
= chunk_num
* CHUNK_SIZE
+
175 part_table
[PART_DOWN
].mem
.size
;
176 partition
.mem
.start
= addr
;
177 wl1271_set_partition(wl
, &partition
);
180 /* 10.3 upload the chunk */
181 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
182 p
= buf
+ chunk_num
* CHUNK_SIZE
;
183 memcpy(chunk
, p
, CHUNK_SIZE
);
184 wl1271_debug(DEBUG_BOOT
, "uploading fw chunk 0x%p to 0x%x",
186 wl1271_write(wl
, addr
, chunk
, CHUNK_SIZE
, false);
191 /* 10.4 upload the last chunk */
192 addr
= dest
+ chunk_num
* CHUNK_SIZE
;
193 p
= buf
+ chunk_num
* CHUNK_SIZE
;
194 memcpy(chunk
, p
, fw_data_len
% CHUNK_SIZE
);
195 wl1271_debug(DEBUG_BOOT
, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
196 fw_data_len
% CHUNK_SIZE
, p
, addr
);
197 wl1271_write(wl
, addr
, chunk
, fw_data_len
% CHUNK_SIZE
, false);
203 static int wl1271_boot_upload_firmware(struct wl1271
*wl
)
205 u32 chunks
, addr
, len
;
210 chunks
= be32_to_cpup((__be32
*) fw
);
213 wl1271_debug(DEBUG_BOOT
, "firmware chunks to be uploaded: %u", chunks
);
216 addr
= be32_to_cpup((__be32
*) fw
);
218 len
= be32_to_cpup((__be32
*) fw
);
222 wl1271_info("firmware chunk too long: %u", len
);
225 wl1271_debug(DEBUG_BOOT
, "chunk %d addr 0x%x len %u",
227 ret
= wl1271_boot_upload_firmware_chunk(wl
, fw
, len
, addr
);
236 static int wl1271_boot_upload_nvs(struct wl1271
*wl
)
238 size_t nvs_len
, burst_len
;
241 u8
*nvs_ptr
, *nvs_aligned
;
247 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz band
248 * configurations) can be removed when those NVS files stop floating
251 if (wl
->nvs_len
== sizeof(struct wl1271_nvs_file
) ||
252 wl
->nvs_len
== WL1271_INI_LEGACY_NVS_FILE_SIZE
) {
253 /* for now 11a is unsupported in AP mode */
254 if (wl
->bss_type
!= BSS_TYPE_AP_BSS
&&
255 wl
->nvs
->general_params
.dual_mode_select
)
256 wl
->enable_11a
= true;
259 if (wl
->nvs_len
!= sizeof(struct wl1271_nvs_file
) &&
260 (wl
->nvs_len
!= WL1271_INI_LEGACY_NVS_FILE_SIZE
||
262 wl1271_error("nvs size is not as expected: %zu != %zu",
263 wl
->nvs_len
, sizeof(struct wl1271_nvs_file
));
270 /* only the first part of the NVS needs to be uploaded */
271 nvs_len
= sizeof(wl
->nvs
->nvs
);
272 nvs_ptr
= (u8
*)wl
->nvs
->nvs
;
274 /* update current MAC address to NVS */
275 nvs_ptr
[11] = wl
->mac_addr
[0];
276 nvs_ptr
[10] = wl
->mac_addr
[1];
277 nvs_ptr
[6] = wl
->mac_addr
[2];
278 nvs_ptr
[5] = wl
->mac_addr
[3];
279 nvs_ptr
[4] = wl
->mac_addr
[4];
280 nvs_ptr
[3] = wl
->mac_addr
[5];
283 * Layout before the actual NVS tables:
284 * 1 byte : burst length.
285 * 2 bytes: destination address.
286 * n bytes: data to burst copy.
288 * This is ended by a 0 length, then the NVS tables.
291 /* FIXME: Do we need to check here whether the LSB is 1? */
293 burst_len
= nvs_ptr
[0];
294 dest_addr
= (nvs_ptr
[1] & 0xfe) | ((u32
)(nvs_ptr
[2] << 8));
297 * Due to our new wl1271_translate_reg_addr function,
298 * we need to add the REGISTER_BASE to the destination
300 dest_addr
+= REGISTERS_BASE
;
302 /* We move our pointer to the data */
305 for (i
= 0; i
< burst_len
; i
++) {
306 val
= (nvs_ptr
[0] | (nvs_ptr
[1] << 8)
307 | (nvs_ptr
[2] << 16) | (nvs_ptr
[3] << 24));
309 wl1271_debug(DEBUG_BOOT
,
310 "nvs burst write 0x%x: 0x%x",
312 wl1271_write32(wl
, dest_addr
, val
);
320 * We've reached the first zero length, the first NVS table
321 * is located at an aligned offset which is at least 7 bytes further.
323 nvs_ptr
= (u8
*)wl
->nvs
->nvs
+
324 ALIGN(nvs_ptr
- (u8
*)wl
->nvs
->nvs
+ 7, 4);
325 nvs_len
-= nvs_ptr
- (u8
*)wl
->nvs
->nvs
;
327 /* Now we must set the partition correctly */
328 wl1271_set_partition(wl
, &part_table
[PART_WORK
]);
330 /* Copy the NVS tables to a new block to ensure alignment */
331 nvs_aligned
= kmemdup(nvs_ptr
, nvs_len
, GFP_KERNEL
);
335 /* And finally we upload the NVS tables */
336 wl1271_write(wl
, CMD_MBOX_ADDRESS
, nvs_aligned
, nvs_len
, false);
342 static void wl1271_boot_enable_interrupts(struct wl1271
*wl
)
344 wl1271_enable_interrupts(wl
);
345 wl1271_write32(wl
, ACX_REG_INTERRUPT_MASK
,
346 WL1271_ACX_INTR_ALL
& ~(WL1271_INTR_MASK
));
347 wl1271_write32(wl
, HI_CFG
, HI_CFG_DEF_VAL
);
350 static int wl1271_boot_soft_reset(struct wl1271
*wl
)
352 unsigned long timeout
;
355 /* perform soft reset */
356 wl1271_write32(wl
, ACX_REG_SLV_SOFT_RESET
, ACX_SLV_SOFT_RESET_BIT
);
358 /* SOFT_RESET is self clearing */
359 timeout
= jiffies
+ usecs_to_jiffies(SOFT_RESET_MAX_TIME
);
361 boot_data
= wl1271_read32(wl
, ACX_REG_SLV_SOFT_RESET
);
362 wl1271_debug(DEBUG_BOOT
, "soft reset bootdata 0x%x", boot_data
);
363 if ((boot_data
& ACX_SLV_SOFT_RESET_BIT
) == 0)
366 if (time_after(jiffies
, timeout
)) {
367 /* 1.2 check pWhalBus->uSelfClearTime if the
368 * timeout was reached */
369 wl1271_error("soft reset timeout");
373 udelay(SOFT_RESET_STALL_TIME
);
377 wl1271_write32(wl
, ENABLE
, 0x0);
379 /* disable auto calibration on start*/
380 wl1271_write32(wl
, SPARE_A2
, 0xffff);
385 static int wl1271_boot_run_firmware(struct wl1271
*wl
)
390 wl1271_boot_set_ecpu_ctrl(wl
, ECPU_CONTROL_HALT
);
392 chip_id
= wl1271_read32(wl
, CHIP_ID_B
);
394 wl1271_debug(DEBUG_BOOT
, "chip id after firmware boot: 0x%x", chip_id
);
396 if (chip_id
!= wl
->chip
.id
) {
397 wl1271_error("chip id doesn't match after firmware boot");
401 /* wait for init to complete */
403 while (loop
++ < INIT_LOOP
) {
404 udelay(INIT_LOOP_DELAY
);
405 intr
= wl1271_read32(wl
, ACX_REG_INTERRUPT_NO_CLEAR
);
407 if (intr
== 0xffffffff) {
408 wl1271_error("error reading hardware complete "
412 /* check that ACX_INTR_INIT_COMPLETE is enabled */
413 else if (intr
& WL1271_ACX_INTR_INIT_COMPLETE
) {
414 wl1271_write32(wl
, ACX_REG_INTERRUPT_ACK
,
415 WL1271_ACX_INTR_INIT_COMPLETE
);
420 if (loop
> INIT_LOOP
) {
421 wl1271_error("timeout waiting for the hardware to "
422 "complete initialization");
426 /* get hardware config command mail box */
427 wl
->cmd_box_addr
= wl1271_read32(wl
, REG_COMMAND_MAILBOX_PTR
);
429 /* get hardware config event mail box */
430 wl
->event_box_addr
= wl1271_read32(wl
, REG_EVENT_MAILBOX_PTR
);
432 /* set the working partition to its "running" mode offset */
433 wl1271_set_partition(wl
, &part_table
[PART_WORK
]);
435 wl1271_debug(DEBUG_MAILBOX
, "cmd_box_addr 0x%x event_box_addr 0x%x",
436 wl
->cmd_box_addr
, wl
->event_box_addr
);
438 wl1271_boot_fw_version(wl
);
441 * in case of full asynchronous mode the firmware event must be
442 * ready to receive event from the command mailbox
445 /* unmask required mbox events */
446 wl
->event_mask
= BSS_LOSE_EVENT_ID
|
447 SCAN_COMPLETE_EVENT_ID
|
449 JOIN_EVENT_COMPLETE_ID
|
450 DISCONNECT_EVENT_COMPLETE_ID
|
451 RSSI_SNR_TRIGGER_0_EVENT_ID
|
452 PSPOLL_DELIVERY_FAILURE_EVENT_ID
|
453 SOFT_GEMINI_SENSE_EVENT_ID
;
455 if (wl
->bss_type
== BSS_TYPE_AP_BSS
)
456 wl
->event_mask
|= STA_REMOVE_COMPLETE_EVENT_ID
;
458 ret
= wl1271_event_unmask(wl
);
460 wl1271_error("EVENT mask setting failed");
464 wl1271_event_mbox_config(wl
);
466 /* firmware startup completed */
470 static int wl1271_boot_write_irq_polarity(struct wl1271
*wl
)
474 polarity
= wl1271_top_reg_read(wl
, OCP_REG_POLARITY
);
476 /* We use HIGH polarity, so unset the LOW bit */
477 polarity
&= ~POLARITY_LOW
;
478 wl1271_top_reg_write(wl
, OCP_REG_POLARITY
, polarity
);
483 static void wl1271_boot_hw_version(struct wl1271
*wl
)
487 fuse
= wl1271_top_reg_read(wl
, REG_FUSE_DATA_2_1
);
488 fuse
= (fuse
& PG_VER_MASK
) >> PG_VER_OFFSET
;
490 wl
->hw_pg_ver
= (s8
)fuse
;
492 if (((wl
->hw_pg_ver
& PG_MAJOR_VER_MASK
) >> PG_MAJOR_VER_OFFSET
) < 3)
493 wl
->quirks
|= WL12XX_QUIRK_END_OF_TRANSACTION
;
496 /* uploads NVS and firmware */
497 int wl1271_load_firmware(struct wl1271
*wl
)
502 wl1271_boot_hw_version(wl
);
504 if (wl
->ref_clock
== 0 || wl
->ref_clock
== 2 || wl
->ref_clock
== 4)
505 /* ref clk: 19.2/38.4/38.4-XTAL */
507 else if (wl
->ref_clock
== 1 || wl
->ref_clock
== 3)
513 if (wl
->ref_clock
!= 0) {
515 /* Set clock type (open drain) */
516 val
= wl1271_top_reg_read(wl
, OCP_REG_CLK_TYPE
);
517 val
&= FREF_CLK_TYPE_BITS
;
518 wl1271_top_reg_write(wl
, OCP_REG_CLK_TYPE
, val
);
520 /* Set clock pull mode (no pull) */
521 val
= wl1271_top_reg_read(wl
, OCP_REG_CLK_PULL
);
523 wl1271_top_reg_write(wl
, OCP_REG_CLK_PULL
, val
);
526 /* Set clock polarity */
527 val
= wl1271_top_reg_read(wl
, OCP_REG_CLK_POLARITY
);
528 val
&= FREF_CLK_POLARITY_BITS
;
529 val
|= CLK_REQ_OUTN_SEL
;
530 wl1271_top_reg_write(wl
, OCP_REG_CLK_POLARITY
, val
);
533 wl1271_write32(wl
, PLL_PARAMETERS
, clk
);
535 pause
= wl1271_read32(wl
, PLL_PARAMETERS
);
537 wl1271_debug(DEBUG_BOOT
, "pause1 0x%x", pause
);
539 pause
&= ~(WU_COUNTER_PAUSE_VAL
);
540 pause
|= WU_COUNTER_PAUSE_VAL
;
541 wl1271_write32(wl
, WU_COUNTER_PAUSE
, pause
);
543 /* Continue the ELP wake up sequence */
544 wl1271_write32(wl
, WELP_ARM_COMMAND
, WELP_ARM_COMMAND_VAL
);
547 wl1271_set_partition(wl
, &part_table
[PART_DRPW
]);
549 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
550 to be used by DRPw FW. The RTRIM value will be added by the FW
551 before taking DRPw out of reset */
553 wl1271_debug(DEBUG_BOOT
, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START
);
554 clk
= wl1271_read32(wl
, DRPW_SCRATCH_START
);
556 wl1271_debug(DEBUG_BOOT
, "clk2 0x%x", clk
);
558 clk
|= (wl
->ref_clock
<< 1) << 4;
559 wl1271_write32(wl
, DRPW_SCRATCH_START
, clk
);
561 wl1271_set_partition(wl
, &part_table
[PART_WORK
]);
563 /* Disable interrupts */
564 wl1271_write32(wl
, ACX_REG_INTERRUPT_MASK
, WL1271_ACX_INTR_ALL
);
566 ret
= wl1271_boot_soft_reset(wl
);
570 /* 2. start processing NVS file */
571 ret
= wl1271_boot_upload_nvs(wl
);
575 /* write firmware's last address (ie. it's length) to
576 * ACX_EEPROMLESS_IND_REG */
577 wl1271_debug(DEBUG_BOOT
, "ACX_EEPROMLESS_IND_REG");
579 wl1271_write32(wl
, ACX_EEPROMLESS_IND_REG
, ACX_EEPROMLESS_IND_REG
);
581 tmp
= wl1271_read32(wl
, CHIP_ID_B
);
583 wl1271_debug(DEBUG_BOOT
, "chip id 0x%x", tmp
);
585 /* 6. read the EEPROM parameters */
586 tmp
= wl1271_read32(wl
, SCR_PAD2
);
588 ret
= wl1271_boot_write_irq_polarity(wl
);
592 wl1271_write32(wl
, ACX_REG_INTERRUPT_MASK
,
593 WL1271_ACX_ALL_EVENTS_VECTOR
);
595 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
598 ret
= wl1271_boot_upload_firmware(wl
);
605 EXPORT_SYMBOL_GPL(wl1271_load_firmware
);
607 int wl1271_boot(struct wl1271
*wl
)
611 /* upload NVS and firmware */
612 ret
= wl1271_load_firmware(wl
);
616 /* 10.5 start firmware */
617 ret
= wl1271_boot_run_firmware(wl
);
621 /* Enable firmware interrupts now */
622 wl1271_boot_enable_interrupts(wl
);
624 /* set the wl1271 default filters */
625 wl1271_set_default_filters(wl
);
627 wl1271_event_mbox_config(wl
);