blob: 41791ffd26ea37fdc8773a2f731a7537150da19e [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
Luciano Coelho2f826f52010-03-26 12:53:21 +02004 * Copyright (C) 2008-2010 Nokia Corporation
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03005 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Shahar Levi5ea417a2011-03-06 16:32:11 +020025#include <linux/wl12xx.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030026
Shahar Levi00d20102010-11-08 11:20:10 +000027#include "acx.h"
28#include "reg.h"
29#include "boot.h"
30#include "io.h"
31#include "event.h"
Arik Nemtsovae113b52010-10-16 18:45:07 +020032#include "rx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030033
34static struct wl1271_partition_set part_table[PART_TABLE_LEN] = {
35 [PART_DOWN] = {
36 .mem = {
37 .start = 0x00000000,
38 .size = 0x000177c0
39 },
40 .reg = {
41 .start = REGISTERS_BASE,
42 .size = 0x00008800
43 },
Juuso Oikarinen451de972009-10-12 15:08:46 +030044 .mem2 = {
45 .start = 0x00000000,
46 .size = 0x00000000
47 },
48 .mem3 = {
49 .start = 0x00000000,
50 .size = 0x00000000
51 },
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030052 },
53
54 [PART_WORK] = {
55 .mem = {
56 .start = 0x00040000,
57 .size = 0x00014fc0
58 },
59 .reg = {
60 .start = REGISTERS_BASE,
Juuso Oikarinen451de972009-10-12 15:08:46 +030061 .size = 0x0000a000
62 },
63 .mem2 = {
64 .start = 0x003004f8,
65 .size = 0x00000004
66 },
67 .mem3 = {
68 .start = 0x00040404,
69 .size = 0x00000000
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030070 },
71 },
72
73 [PART_DRPW] = {
74 .mem = {
75 .start = 0x00040000,
76 .size = 0x00014fc0
77 },
78 .reg = {
79 .start = DRPW_BASE,
80 .size = 0x00006000
Juuso Oikarinen451de972009-10-12 15:08:46 +030081 },
82 .mem2 = {
83 .start = 0x00000000,
84 .size = 0x00000000
85 },
86 .mem3 = {
87 .start = 0x00000000,
88 .size = 0x00000000
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030089 }
90 }
91};
92
93static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
94{
95 u32 cpu_ctrl;
96
97 /* 10.5.0 run the firmware (I) */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020098 cpu_ctrl = wl1271_read32(wl, ACX_REG_ECPU_CONTROL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030099
100 /* 10.5.1 run the firmware (II) */
101 cpu_ctrl |= flag;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200102 wl1271_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300103}
104
Ido Yariv842f1a62011-06-06 14:57:04 +0300105static unsigned int wl12xx_get_fw_ver_quirks(struct wl1271 *wl)
106{
107 unsigned int quirks = 0;
108 unsigned int *fw_ver = wl->chip.fw_ver;
109
Ido Yariv95dac04f2011-06-06 14:57:06 +0300110 /* Only new station firmwares support routing fw logs to the host */
111 if ((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_STA) &&
112 (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_FWLOG_STA_MIN))
113 quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
114
115 /* This feature is not yet supported for AP mode */
116 if (fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_AP)
117 quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
118
Ido Yariv842f1a62011-06-06 14:57:04 +0300119 return quirks;
120}
121
Levi, Shahar4b7fac72011-01-23 07:27:22 +0100122static void wl1271_parse_fw_ver(struct wl1271 *wl)
123{
124 int ret;
125
126 ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
127 &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
128 &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
129 &wl->chip.fw_ver[4]);
130
131 if (ret != 5) {
132 wl1271_warning("fw version incorrect value");
133 memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
134 return;
135 }
Ido Yariv842f1a62011-06-06 14:57:04 +0300136
137 /* Check if any quirks are needed with older fw versions */
138 wl->quirks |= wl12xx_get_fw_ver_quirks(wl);
Levi, Shahar4b7fac72011-01-23 07:27:22 +0100139}
140
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300141static void wl1271_boot_fw_version(struct wl1271 *wl)
142{
143 struct wl1271_static_data static_data;
144
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200145 wl1271_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
146 false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300147
Levi, Shahar4b7fac72011-01-23 07:27:22 +0100148 strncpy(wl->chip.fw_ver_str, static_data.fw_version,
149 sizeof(wl->chip.fw_ver_str));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300150
151 /* make sure the string is NULL-terminated */
Levi, Shahar4b7fac72011-01-23 07:27:22 +0100152 wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
153
154 wl1271_parse_fw_ver(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300155}
156
157static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
158 size_t fw_data_len, u32 dest)
159{
Juuso Oikarinen451de972009-10-12 15:08:46 +0300160 struct wl1271_partition_set partition;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300161 int addr, chunk_num, partition_limit;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300162 u8 *p, *chunk;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300163
164 /* whal_FwCtrl_LoadFwImageSm() */
165
166 wl1271_debug(DEBUG_BOOT, "starting firmware upload");
167
Luciano Coelho73d0a132009-08-11 11:58:27 +0300168 wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
169 fw_data_len, CHUNK_SIZE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300170
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300171 if ((fw_data_len % 4) != 0) {
172 wl1271_error("firmware length not multiple of four");
173 return -EIO;
174 }
175
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300176 chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
Juuso Oikarinened3177882009-10-13 12:47:57 +0300177 if (!chunk) {
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300178 wl1271_error("allocation for firmware upload chunk failed");
179 return -ENOMEM;
180 }
181
Juuso Oikarinen451de972009-10-12 15:08:46 +0300182 memcpy(&partition, &part_table[PART_DOWN], sizeof(partition));
183 partition.mem.start = dest;
184 wl1271_set_partition(wl, &partition);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300185
186 /* 10.1 set partition limit and chunk num */
187 chunk_num = 0;
188 partition_limit = part_table[PART_DOWN].mem.size;
189
190 while (chunk_num < fw_data_len / CHUNK_SIZE) {
191 /* 10.2 update partition, if needed */
192 addr = dest + (chunk_num + 2) * CHUNK_SIZE;
193 if (addr > partition_limit) {
194 addr = dest + chunk_num * CHUNK_SIZE;
195 partition_limit = chunk_num * CHUNK_SIZE +
196 part_table[PART_DOWN].mem.size;
Juuso Oikarinen451de972009-10-12 15:08:46 +0300197 partition.mem.start = addr;
198 wl1271_set_partition(wl, &partition);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300199 }
200
201 /* 10.3 upload the chunk */
202 addr = dest + chunk_num * CHUNK_SIZE;
203 p = buf + chunk_num * CHUNK_SIZE;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300204 memcpy(chunk, p, CHUNK_SIZE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300205 wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
206 p, addr);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200207 wl1271_write(wl, addr, chunk, CHUNK_SIZE, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300208
209 chunk_num++;
210 }
211
212 /* 10.4 upload the last chunk */
213 addr = dest + chunk_num * CHUNK_SIZE;
214 p = buf + chunk_num * CHUNK_SIZE;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300215 memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
Luciano Coelho73d0a132009-08-11 11:58:27 +0300216 wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300217 fw_data_len % CHUNK_SIZE, p, addr);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200218 wl1271_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300219
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300220 kfree(chunk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300221 return 0;
222}
223
224static int wl1271_boot_upload_firmware(struct wl1271 *wl)
225{
226 u32 chunks, addr, len;
Juuso Oikarinened3177882009-10-13 12:47:57 +0300227 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300228 u8 *fw;
229
230 fw = wl->fw;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300231 chunks = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300232 fw += sizeof(u32);
233
234 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
235
236 while (chunks--) {
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300237 addr = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300238 fw += sizeof(u32);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300239 len = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240 fw += sizeof(u32);
241
242 if (len > 300000) {
243 wl1271_info("firmware chunk too long: %u", len);
244 return -EINVAL;
245 }
246 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
247 chunks, addr, len);
Juuso Oikarinened3177882009-10-13 12:47:57 +0300248 ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
249 if (ret != 0)
250 break;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300251 fw += len;
252 }
253
Juuso Oikarinened3177882009-10-13 12:47:57 +0300254 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300255}
256
257static int wl1271_boot_upload_nvs(struct wl1271 *wl)
258{
259 size_t nvs_len, burst_len;
260 int i;
261 u32 dest_addr, val;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200262 u8 *nvs_ptr, *nvs_aligned;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300263
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200264 if (wl->nvs == NULL)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300265 return -ENODEV;
266
Shahar Levibc765bf2011-03-06 16:32:10 +0200267 if (wl->chip.id == CHIP_ID_1283_PG20) {
268 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200269
Shahar Levibc765bf2011-03-06 16:32:10 +0200270 if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
271 if (nvs->general_params.dual_mode_select)
272 wl->enable_11a = true;
273 } else {
274 wl1271_error("nvs size is not as expected: %zu != %zu",
275 wl->nvs_len,
276 sizeof(struct wl128x_nvs_file));
277 kfree(wl->nvs);
278 wl->nvs = NULL;
279 wl->nvs_len = 0;
280 return -EILSEQ;
281 }
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200282
Shahar Levibc765bf2011-03-06 16:32:10 +0200283 /* only the first part of the NVS needs to be uploaded */
284 nvs_len = sizeof(nvs->nvs);
285 nvs_ptr = (u8 *)nvs->nvs;
286
287 } else {
288 struct wl1271_nvs_file *nvs =
289 (struct wl1271_nvs_file *)wl->nvs;
290 /*
291 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
292 * band configurations) can be removed when those NVS files stop
293 * floating around.
294 */
295 if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
296 wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
297 /* for now 11a is unsupported in AP mode */
298 if (wl->bss_type != BSS_TYPE_AP_BSS &&
299 nvs->general_params.dual_mode_select)
300 wl->enable_11a = true;
301 }
302
303 if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
304 (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
305 wl->enable_11a)) {
306 wl1271_error("nvs size is not as expected: %zu != %zu",
307 wl->nvs_len, sizeof(struct wl1271_nvs_file));
308 kfree(wl->nvs);
309 wl->nvs = NULL;
310 wl->nvs_len = 0;
311 return -EILSEQ;
312 }
313
314 /* only the first part of the NVS needs to be uploaded */
315 nvs_len = sizeof(nvs->nvs);
316 nvs_ptr = (u8 *) nvs->nvs;
317 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300318
Juuso Oikarinen1b72aec2010-03-18 12:26:39 +0200319 /* update current MAC address to NVS */
320 nvs_ptr[11] = wl->mac_addr[0];
321 nvs_ptr[10] = wl->mac_addr[1];
322 nvs_ptr[6] = wl->mac_addr[2];
323 nvs_ptr[5] = wl->mac_addr[3];
324 nvs_ptr[4] = wl->mac_addr[4];
325 nvs_ptr[3] = wl->mac_addr[5];
326
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300327 /*
328 * Layout before the actual NVS tables:
329 * 1 byte : burst length.
330 * 2 bytes: destination address.
331 * n bytes: data to burst copy.
332 *
333 * This is ended by a 0 length, then the NVS tables.
334 */
335
336 /* FIXME: Do we need to check here whether the LSB is 1? */
337 while (nvs_ptr[0]) {
338 burst_len = nvs_ptr[0];
339 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
340
Juuso Oikarinen2f63b012010-08-10 06:38:35 +0200341 /*
342 * Due to our new wl1271_translate_reg_addr function,
343 * we need to add the REGISTER_BASE to the destination
344 */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300345 dest_addr += REGISTERS_BASE;
346
347 /* We move our pointer to the data */
348 nvs_ptr += 3;
349
350 for (i = 0; i < burst_len; i++) {
351 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
352 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
353
354 wl1271_debug(DEBUG_BOOT,
355 "nvs burst write 0x%x: 0x%x",
356 dest_addr, val);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200357 wl1271_write32(wl, dest_addr, val);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300358
359 nvs_ptr += 4;
360 dest_addr += 4;
361 }
362 }
363
364 /*
365 * We've reached the first zero length, the first NVS table
Ido Yariv67e02082010-09-22 09:53:13 +0200366 * is located at an aligned offset which is at least 7 bytes further.
Shahar Levibc765bf2011-03-06 16:32:10 +0200367 * NOTE: The wl->nvs->nvs element must be first, in order to
368 * simplify the casting, we assume it is at the beginning of
369 * the wl->nvs structure.
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300370 */
Shahar Levibc765bf2011-03-06 16:32:10 +0200371 nvs_ptr = (u8 *)wl->nvs +
372 ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
373 nvs_len -= nvs_ptr - (u8 *)wl->nvs;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300374
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375 /* Now we must set the partition correctly */
Juuso Oikarinen451de972009-10-12 15:08:46 +0300376 wl1271_set_partition(wl, &part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300377
378 /* Copy the NVS tables to a new block to ensure alignment */
Ido Yariv67e02082010-09-22 09:53:13 +0200379 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
380 if (!nvs_aligned)
381 return -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300382
383 /* And finally we upload the NVS tables */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200384 wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300385
386 kfree(nvs_aligned);
387 return 0;
388}
389
390static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
391{
Teemu Paasikivi54f7e502010-02-22 08:38:22 +0200392 wl1271_enable_interrupts(wl);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200393 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
394 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
395 wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396}
397
398static int wl1271_boot_soft_reset(struct wl1271 *wl)
399{
400 unsigned long timeout;
401 u32 boot_data;
402
403 /* perform soft reset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200404 wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300405
406 /* SOFT_RESET is self clearing */
407 timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
408 while (1) {
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200409 boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300410 wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
411 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
412 break;
413
414 if (time_after(jiffies, timeout)) {
415 /* 1.2 check pWhalBus->uSelfClearTime if the
416 * timeout was reached */
417 wl1271_error("soft reset timeout");
418 return -1;
419 }
420
421 udelay(SOFT_RESET_STALL_TIME);
422 }
423
424 /* disable Rx/Tx */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200425 wl1271_write32(wl, ENABLE, 0x0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300426
427 /* disable auto calibration on start*/
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200428 wl1271_write32(wl, SPARE_A2, 0xffff);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300429
430 return 0;
431}
432
433static int wl1271_boot_run_firmware(struct wl1271 *wl)
434{
435 int loop, ret;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300436 u32 chip_id, intr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300437
438 wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
439
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200440 chip_id = wl1271_read32(wl, CHIP_ID_B);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300441
442 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
443
444 if (chip_id != wl->chip.id) {
445 wl1271_error("chip id doesn't match after firmware boot");
446 return -EIO;
447 }
448
449 /* wait for init to complete */
450 loop = 0;
451 while (loop++ < INIT_LOOP) {
452 udelay(INIT_LOOP_DELAY);
Luciano Coelho23a7a512010-04-28 09:50:02 +0300453 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300454
Luciano Coelho23a7a512010-04-28 09:50:02 +0300455 if (intr == 0xffffffff) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300456 wl1271_error("error reading hardware complete "
457 "init indication");
458 return -EIO;
459 }
460 /* check that ACX_INTR_INIT_COMPLETE is enabled */
Luciano Coelho23a7a512010-04-28 09:50:02 +0300461 else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200462 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
463 WL1271_ACX_INTR_INIT_COMPLETE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300464 break;
465 }
466 }
467
Luciano Coelhoe7d17cf2009-10-29 13:20:04 +0200468 if (loop > INIT_LOOP) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300469 wl1271_error("timeout waiting for the hardware to "
470 "complete initialization");
471 return -EIO;
472 }
473
474 /* get hardware config command mail box */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200475 wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300476
477 /* get hardware config event mail box */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200478 wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300479
480 /* set the working partition to its "running" mode offset */
Juuso Oikarinen451de972009-10-12 15:08:46 +0300481 wl1271_set_partition(wl, &part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300482
483 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
484 wl->cmd_box_addr, wl->event_box_addr);
485
486 wl1271_boot_fw_version(wl);
487
488 /*
489 * in case of full asynchronous mode the firmware event must be
490 * ready to receive event from the command mailbox
491 */
492
Juuso Oikarinenbe823e52009-10-08 21:56:36 +0300493 /* unmask required mbox events */
494 wl->event_mask = BSS_LOSE_EVENT_ID |
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200495 SCAN_COMPLETE_EVENT_ID |
Luciano Coelho99d84c12010-03-26 12:53:20 +0200496 PS_REPORT_EVENT_ID |
Luciano Coelho2f826f52010-03-26 12:53:21 +0200497 JOIN_EVENT_COMPLETE_ID |
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300498 DISCONNECT_EVENT_COMPLETE_ID |
Juuso Oikarinen90494a92010-07-08 17:50:00 +0300499 RSSI_SNR_TRIGGER_0_EVENT_ID |
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300500 PSPOLL_DELIVERY_FAILURE_EVENT_ID |
Luciano Coelho6394c012011-05-10 14:28:27 +0300501 SOFT_GEMINI_SENSE_EVENT_ID |
502 PERIODIC_SCAN_REPORT_EVENT_ID |
503 PERIODIC_SCAN_COMPLETE_EVENT_ID;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300504
Arik Nemtsov203c9032010-10-25 11:17:44 +0200505 if (wl->bss_type == BSS_TYPE_AP_BSS)
Arik Nemtsov3618f302011-06-26 10:36:03 +0300506 wl->event_mask |= STA_REMOVE_COMPLETE_EVENT_ID |
507 INACTIVE_STA_EVENT_ID |
508 MAX_TX_RETRY_EVENT_ID;
Shahar Leviae47c452011-03-06 16:32:14 +0200509 else
Shahar Levi70559a02011-05-22 16:10:22 +0300510 wl->event_mask |= DUMMY_PACKET_EVENT_ID |
511 BA_SESSION_RX_CONSTRAINT_EVENT_ID;
Arik Nemtsov203c9032010-10-25 11:17:44 +0200512
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300513 ret = wl1271_event_unmask(wl);
514 if (ret < 0) {
515 wl1271_error("EVENT mask setting failed");
516 return ret;
517 }
518
519 wl1271_event_mbox_config(wl);
520
521 /* firmware startup completed */
522 return 0;
523}
524
525static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
526{
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300527 u32 polarity;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300528
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300529 polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300530
531 /* We use HIGH polarity, so unset the LOW bit */
532 polarity &= ~POLARITY_LOW;
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300533 wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300534
535 return 0;
536}
537
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300538static void wl1271_boot_hw_version(struct wl1271 *wl)
539{
540 u32 fuse;
541
Gery Kahn6f07b722011-07-18 14:21:49 +0300542 if (wl->chip.id == CHIP_ID_1283_PG20)
543 fuse = wl1271_top_reg_read(wl, WL128X_REG_FUSE_DATA_2_1);
544 else
545 fuse = wl1271_top_reg_read(wl, WL127X_REG_FUSE_DATA_2_1);
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300546 fuse = (fuse & PG_VER_MASK) >> PG_VER_OFFSET;
547
548 wl->hw_pg_ver = (s8)fuse;
549}
550
Ido Yarivd29633b2011-03-31 10:06:57 +0200551static int wl128x_switch_tcxo_to_fref(struct wl1271 *wl)
552{
553 u16 spare_reg;
554
555 /* Mask bits [2] & [8:4] in the sys_clk_cfg register */
556 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
557 if (spare_reg == 0xFFFF)
558 return -EFAULT;
559 spare_reg |= (BIT(3) | BIT(5) | BIT(6));
560 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
561
562 /* Enable FREF_CLK_REQ & mux MCS and coex PLLs to FREF */
563 wl1271_top_reg_write(wl, SYS_CLK_CFG_REG,
564 WL_CLK_REQ_TYPE_PG2 | MCS_PLL_CLK_SEL_FREF);
565
566 /* Delay execution for 15msec, to let the HW settle */
567 mdelay(15);
568
569 return 0;
570}
571
572static bool wl128x_is_tcxo_valid(struct wl1271 *wl)
573{
574 u16 tcxo_detection;
575
576 tcxo_detection = wl1271_top_reg_read(wl, TCXO_CLK_DETECT_REG);
577 if (tcxo_detection & TCXO_DET_FAILED)
578 return false;
579
580 return true;
581}
582
583static bool wl128x_is_fref_valid(struct wl1271 *wl)
584{
585 u16 fref_detection;
586
587 fref_detection = wl1271_top_reg_read(wl, FREF_CLK_DETECT_REG);
588 if (fref_detection & FREF_CLK_DETECT_FAIL)
589 return false;
590
591 return true;
592}
593
594static int wl128x_manually_configure_mcs_pll(struct wl1271 *wl)
595{
596 wl1271_top_reg_write(wl, MCS_PLL_M_REG, MCS_PLL_M_REG_VAL);
597 wl1271_top_reg_write(wl, MCS_PLL_N_REG, MCS_PLL_N_REG_VAL);
598 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, MCS_PLL_CONFIG_REG_VAL);
599
600 return 0;
601}
602
603static int wl128x_configure_mcs_pll(struct wl1271 *wl, int clk)
604{
605 u16 spare_reg;
606 u16 pll_config;
607 u8 input_freq;
608
609 /* Mask bits [3:1] in the sys_clk_cfg register */
610 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
611 if (spare_reg == 0xFFFF)
612 return -EFAULT;
613 spare_reg |= BIT(2);
614 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
615
616 /* Handle special cases of the TCXO clock */
617 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_8 ||
618 wl->tcxo_clock == WL12XX_TCXOCLOCK_33_6)
619 return wl128x_manually_configure_mcs_pll(wl);
620
621 /* Set the input frequency according to the selected clock source */
622 input_freq = (clk & 1) + 1;
623
624 pll_config = wl1271_top_reg_read(wl, MCS_PLL_CONFIG_REG);
625 if (pll_config == 0xFFFF)
626 return -EFAULT;
627 pll_config |= (input_freq << MCS_SEL_IN_FREQ_SHIFT);
628 pll_config |= MCS_PLL_ENABLE_HP;
629 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, pll_config);
630
631 return 0;
632}
633
Shahar Levi5ea417a2011-03-06 16:32:11 +0200634/*
635 * WL128x has two clocks input - TCXO and FREF.
636 * TCXO is the main clock of the device, while FREF is used to sync
637 * between the GPS and the cellular modem.
638 * In cases where TCXO is 32.736MHz or 16.368MHz, the FREF will be used
639 * as the WLAN/BT main clock.
640 */
Ido Yarivd29633b2011-03-31 10:06:57 +0200641static int wl128x_boot_clk(struct wl1271 *wl, int *selected_clock)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300642{
Ido Yarivd29633b2011-03-31 10:06:57 +0200643 u16 sys_clk_cfg;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200644
Ido Yarivd29633b2011-03-31 10:06:57 +0200645 /* For XTAL-only modes, FREF will be used after switching from TCXO */
646 if (wl->ref_clock == WL12XX_REFCLOCK_26_XTAL ||
647 wl->ref_clock == WL12XX_REFCLOCK_38_XTAL) {
648 if (!wl128x_switch_tcxo_to_fref(wl))
649 return -EINVAL;
650 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200651 }
652
Ido Yarivd29633b2011-03-31 10:06:57 +0200653 /* Query the HW, to determine which clock source we should use */
654 sys_clk_cfg = wl1271_top_reg_read(wl, SYS_CLK_CFG_REG);
655 if (sys_clk_cfg == 0xFFFF)
656 return -EINVAL;
657 if (sys_clk_cfg & PRCM_CM_EN_MUX_WLAN_FREF)
658 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200659
Ido Yarivd29633b2011-03-31 10:06:57 +0200660 /* If TCXO is either 32.736MHz or 16.368MHz, switch to FREF */
661 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_368 ||
662 wl->tcxo_clock == WL12XX_TCXOCLOCK_32_736) {
663 if (!wl128x_switch_tcxo_to_fref(wl))
664 return -EINVAL;
665 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200666 }
667
Ido Yarivd29633b2011-03-31 10:06:57 +0200668 /* TCXO clock is selected */
669 if (!wl128x_is_tcxo_valid(wl))
670 return -EINVAL;
671 *selected_clock = wl->tcxo_clock;
672 goto config_mcs_pll;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200673
Ido Yarivd29633b2011-03-31 10:06:57 +0200674fref_clk:
675 /* FREF clock is selected */
676 if (!wl128x_is_fref_valid(wl))
677 return -EINVAL;
678 *selected_clock = wl->ref_clock;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200679
Ido Yarivd29633b2011-03-31 10:06:57 +0200680config_mcs_pll:
681 return wl128x_configure_mcs_pll(wl, *selected_clock);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200682}
683
684static int wl127x_boot_clk(struct wl1271 *wl)
685{
686 u32 pause;
687 u32 clk;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300688
Gery Kahn6f07b722011-07-18 14:21:49 +0300689 if (((wl->hw_pg_ver & PG_MAJOR_VER_MASK) >> PG_MAJOR_VER_OFFSET) < 3)
690 wl->quirks |= WL12XX_QUIRK_END_OF_TRANSACTION;
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300691
Shahar Levi5ea417a2011-03-06 16:32:11 +0200692 if (wl->ref_clock == CONF_REF_CLK_19_2_E ||
693 wl->ref_clock == CONF_REF_CLK_38_4_E ||
694 wl->ref_clock == CONF_REF_CLK_38_4_M_XTAL)
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300695 /* ref clk: 19.2/38.4/38.4-XTAL */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300696 clk = 0x3;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200697 else if (wl->ref_clock == CONF_REF_CLK_26_E ||
698 wl->ref_clock == CONF_REF_CLK_52_E)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300699 /* ref clk: 26/52 */
700 clk = 0x5;
Ohad Ben-Cohen15cea992010-09-16 01:31:51 +0200701 else
702 return -EINVAL;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300703
Shahar Levi5ea417a2011-03-06 16:32:11 +0200704 if (wl->ref_clock != CONF_REF_CLK_19_2_E) {
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300705 u16 val;
Juuso Oikarinen9d4e5bb2010-03-26 12:53:15 +0200706 /* Set clock type (open drain) */
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300707 val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
708 val &= FREF_CLK_TYPE_BITS;
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300709 wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
Juuso Oikarinen9d4e5bb2010-03-26 12:53:15 +0200710
711 /* Set clock pull mode (no pull) */
712 val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
713 val |= NO_PULL;
714 wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300715 } else {
716 u16 val;
717 /* Set clock polarity */
718 val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
719 val &= FREF_CLK_POLARITY_BITS;
720 val |= CLK_REQ_OUTN_SEL;
721 wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
722 }
723
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200724 wl1271_write32(wl, PLL_PARAMETERS, clk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300725
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200726 pause = wl1271_read32(wl, PLL_PARAMETERS);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300727
728 wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
729
Juuso Oikarinen2f63b012010-08-10 06:38:35 +0200730 pause &= ~(WU_COUNTER_PAUSE_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300731 pause |= WU_COUNTER_PAUSE_VAL;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200732 wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300733
Shahar Levi5ea417a2011-03-06 16:32:11 +0200734 return 0;
735}
736
737/* uploads NVS and firmware */
738int wl1271_load_firmware(struct wl1271 *wl)
739{
740 int ret = 0;
741 u32 tmp, clk;
Ido Yarivd29633b2011-03-31 10:06:57 +0200742 int selected_clock = -1;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200743
Gery Kahn6f07b722011-07-18 14:21:49 +0300744 wl1271_boot_hw_version(wl);
745
Shahar Levi5ea417a2011-03-06 16:32:11 +0200746 if (wl->chip.id == CHIP_ID_1283_PG20) {
Ido Yarivd29633b2011-03-31 10:06:57 +0200747 ret = wl128x_boot_clk(wl, &selected_clock);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200748 if (ret < 0)
749 goto out;
750 } else {
751 ret = wl127x_boot_clk(wl);
752 if (ret < 0)
753 goto out;
754 }
755
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300756 /* Continue the ELP wake up sequence */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200757 wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300758 udelay(500);
759
Juuso Oikarinen451de972009-10-12 15:08:46 +0300760 wl1271_set_partition(wl, &part_table[PART_DRPW]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300761
762 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
763 to be used by DRPw FW. The RTRIM value will be added by the FW
764 before taking DRPw out of reset */
765
766 wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200767 clk = wl1271_read32(wl, DRPW_SCRATCH_START);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300768
769 wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
770
Shahar Levi5ea417a2011-03-06 16:32:11 +0200771 if (wl->chip.id == CHIP_ID_1283_PG20) {
Ido Yarivd29633b2011-03-31 10:06:57 +0200772 clk |= ((selected_clock & 0x3) << 1) << 4;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200773 } else {
774 clk |= (wl->ref_clock << 1) << 4;
775 }
776
Shahar Levi0c005042011-06-12 10:34:43 +0300777 if (wl->quirks & WL12XX_QUIRK_LPD_MODE)
778 clk |= SCRATCH_ENABLE_LPD;
779
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200780 wl1271_write32(wl, DRPW_SCRATCH_START, clk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300781
Juuso Oikarinen451de972009-10-12 15:08:46 +0300782 wl1271_set_partition(wl, &part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300783
784 /* Disable interrupts */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200785 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300786
787 ret = wl1271_boot_soft_reset(wl);
788 if (ret < 0)
789 goto out;
790
791 /* 2. start processing NVS file */
792 ret = wl1271_boot_upload_nvs(wl);
793 if (ret < 0)
794 goto out;
795
796 /* write firmware's last address (ie. it's length) to
797 * ACX_EEPROMLESS_IND_REG */
798 wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
799
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200800 wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300801
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200802 tmp = wl1271_read32(wl, CHIP_ID_B);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300803
804 wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
805
806 /* 6. read the EEPROM parameters */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200807 tmp = wl1271_read32(wl, SCR_PAD2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300808
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300809 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
810 * to upload_fw) */
811
Shahar Levi5ea417a2011-03-06 16:32:11 +0200812 if (wl->chip.id == CHIP_ID_1283_PG20)
Luciano Coelhoafb7d3c2011-04-01 20:48:02 +0300813 wl1271_top_reg_write(wl, SDIO_IO_DS, wl->conf.hci_io_ds);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200814
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300815 ret = wl1271_boot_upload_firmware(wl);
816 if (ret < 0)
817 goto out;
818
Roger Quadros870c3672010-11-29 16:24:57 +0200819out:
820 return ret;
821}
822EXPORT_SYMBOL_GPL(wl1271_load_firmware);
823
824int wl1271_boot(struct wl1271 *wl)
825{
826 int ret;
827
828 /* upload NVS and firmware */
829 ret = wl1271_load_firmware(wl);
830 if (ret)
831 return ret;
832
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300833 /* 10.5 start firmware */
834 ret = wl1271_boot_run_firmware(wl);
835 if (ret < 0)
836 goto out;
837
Shahar Levib9b0fde2011-03-06 16:32:06 +0200838 ret = wl1271_boot_write_irq_polarity(wl);
839 if (ret < 0)
840 goto out;
841
842 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
843 WL1271_ACX_ALL_EVENTS_VECTOR);
844
Juuso Oikarineneb5b28d2009-10-13 12:47:45 +0300845 /* Enable firmware interrupts now */
846 wl1271_boot_enable_interrupts(wl);
847
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300848 wl1271_event_mbox_config(wl);
849
850out:
851 return ret;
852}