blob: 84d295126edf6b14c59516f28df78ef50f6d159f [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>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040026#include <linux/export.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030027
Luciano Coelho0f4e3122011-10-07 11:02:42 +030028#include "debug.h"
Shahar Levi00d20102010-11-08 11:20:10 +000029#include "acx.h"
30#include "reg.h"
31#include "boot.h"
32#include "io.h"
33#include "event.h"
Arik Nemtsovae113b52010-10-16 18:45:07 +020034#include "rx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030036static void wl1271_boot_set_ecpu_ctrl(struct wl1271 *wl, u32 flag)
37{
38 u32 cpu_ctrl;
39
40 /* 10.5.0 run the firmware (I) */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020041 cpu_ctrl = wl1271_read32(wl, ACX_REG_ECPU_CONTROL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030042
43 /* 10.5.1 run the firmware (II) */
44 cpu_ctrl |= flag;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020045 wl1271_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030046}
47
Ido Yariv842f1a62011-06-06 14:57:04 +030048static unsigned int wl12xx_get_fw_ver_quirks(struct wl1271 *wl)
49{
50 unsigned int quirks = 0;
51 unsigned int *fw_ver = wl->chip.fw_ver;
52
Ido Yariv95dac04f2011-06-06 14:57:06 +030053 /* Only new station firmwares support routing fw logs to the host */
54 if ((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_STA) &&
55 (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_FWLOG_STA_MIN))
56 quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
57
58 /* This feature is not yet supported for AP mode */
59 if (fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_AP)
60 quirks |= WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED;
61
Ido Yariv842f1a62011-06-06 14:57:04 +030062 return quirks;
63}
64
Levi, Shahar4b7fac72011-01-23 07:27:22 +010065static void wl1271_parse_fw_ver(struct wl1271 *wl)
66{
67 int ret;
68
69 ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
70 &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
71 &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
72 &wl->chip.fw_ver[4]);
73
74 if (ret != 5) {
75 wl1271_warning("fw version incorrect value");
76 memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
77 return;
78 }
Ido Yariv842f1a62011-06-06 14:57:04 +030079
80 /* Check if any quirks are needed with older fw versions */
81 wl->quirks |= wl12xx_get_fw_ver_quirks(wl);
Levi, Shahar4b7fac72011-01-23 07:27:22 +010082}
83
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030084static void wl1271_boot_fw_version(struct wl1271 *wl)
85{
86 struct wl1271_static_data static_data;
87
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020088 wl1271_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
89 false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030090
Levi, Shahar4b7fac72011-01-23 07:27:22 +010091 strncpy(wl->chip.fw_ver_str, static_data.fw_version,
92 sizeof(wl->chip.fw_ver_str));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030093
94 /* make sure the string is NULL-terminated */
Levi, Shahar4b7fac72011-01-23 07:27:22 +010095 wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
96
97 wl1271_parse_fw_ver(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030098}
99
100static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
101 size_t fw_data_len, u32 dest)
102{
Juuso Oikarinen451de972009-10-12 15:08:46 +0300103 struct wl1271_partition_set partition;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300104 int addr, chunk_num, partition_limit;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300105 u8 *p, *chunk;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300106
107 /* whal_FwCtrl_LoadFwImageSm() */
108
109 wl1271_debug(DEBUG_BOOT, "starting firmware upload");
110
Luciano Coelho73d0a132009-08-11 11:58:27 +0300111 wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
112 fw_data_len, CHUNK_SIZE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300113
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300114 if ((fw_data_len % 4) != 0) {
115 wl1271_error("firmware length not multiple of four");
116 return -EIO;
117 }
118
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300119 chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
Juuso Oikarinened3177882009-10-13 12:47:57 +0300120 if (!chunk) {
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300121 wl1271_error("allocation for firmware upload chunk failed");
122 return -ENOMEM;
123 }
124
Luciano Coelho0becb142012-01-12 14:45:34 +0200125 memcpy(&partition, &wl12xx_part_table[PART_DOWN], sizeof(partition));
Juuso Oikarinen451de972009-10-12 15:08:46 +0300126 partition.mem.start = dest;
127 wl1271_set_partition(wl, &partition);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300128
129 /* 10.1 set partition limit and chunk num */
130 chunk_num = 0;
Luciano Coelho0becb142012-01-12 14:45:34 +0200131 partition_limit = wl12xx_part_table[PART_DOWN].mem.size;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300132
133 while (chunk_num < fw_data_len / CHUNK_SIZE) {
134 /* 10.2 update partition, if needed */
135 addr = dest + (chunk_num + 2) * CHUNK_SIZE;
136 if (addr > partition_limit) {
137 addr = dest + chunk_num * CHUNK_SIZE;
138 partition_limit = chunk_num * CHUNK_SIZE +
Luciano Coelho0becb142012-01-12 14:45:34 +0200139 wl12xx_part_table[PART_DOWN].mem.size;
Juuso Oikarinen451de972009-10-12 15:08:46 +0300140 partition.mem.start = addr;
141 wl1271_set_partition(wl, &partition);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300142 }
143
144 /* 10.3 upload the chunk */
145 addr = dest + chunk_num * CHUNK_SIZE;
146 p = buf + chunk_num * CHUNK_SIZE;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300147 memcpy(chunk, p, CHUNK_SIZE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300148 wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
149 p, addr);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200150 wl1271_write(wl, addr, chunk, CHUNK_SIZE, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300151
152 chunk_num++;
153 }
154
155 /* 10.4 upload the last chunk */
156 addr = dest + chunk_num * CHUNK_SIZE;
157 p = buf + chunk_num * CHUNK_SIZE;
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300158 memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
Luciano Coelho73d0a132009-08-11 11:58:27 +0300159 wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300160 fw_data_len % CHUNK_SIZE, p, addr);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200161 wl1271_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300162
Juuso Oikarinen1fba4972009-10-08 21:56:32 +0300163 kfree(chunk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164 return 0;
165}
166
167static int wl1271_boot_upload_firmware(struct wl1271 *wl)
168{
169 u32 chunks, addr, len;
Juuso Oikarinened3177882009-10-13 12:47:57 +0300170 int ret = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300171 u8 *fw;
172
173 fw = wl->fw;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300174 chunks = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175 fw += sizeof(u32);
176
177 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
178
179 while (chunks--) {
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300180 addr = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300181 fw += sizeof(u32);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300182 len = be32_to_cpup((__be32 *) fw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300183 fw += sizeof(u32);
184
185 if (len > 300000) {
186 wl1271_info("firmware chunk too long: %u", len);
187 return -EINVAL;
188 }
189 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
190 chunks, addr, len);
Juuso Oikarinened3177882009-10-13 12:47:57 +0300191 ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
192 if (ret != 0)
193 break;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300194 fw += len;
195 }
196
Juuso Oikarinened3177882009-10-13 12:47:57 +0300197 return ret;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300198}
199
200static int wl1271_boot_upload_nvs(struct wl1271 *wl)
201{
202 size_t nvs_len, burst_len;
203 int i;
204 u32 dest_addr, val;
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200205 u8 *nvs_ptr, *nvs_aligned;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300206
Juuso Oikarinen152ee6e2010-02-18 13:25:42 +0200207 if (wl->nvs == NULL)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300208 return -ENODEV;
209
Shahar Levibc765bf2011-03-06 16:32:10 +0200210 if (wl->chip.id == CHIP_ID_1283_PG20) {
211 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200212
Shahar Levibc765bf2011-03-06 16:32:10 +0200213 if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
214 if (nvs->general_params.dual_mode_select)
215 wl->enable_11a = true;
216 } else {
217 wl1271_error("nvs size is not as expected: %zu != %zu",
218 wl->nvs_len,
219 sizeof(struct wl128x_nvs_file));
220 kfree(wl->nvs);
221 wl->nvs = NULL;
222 wl->nvs_len = 0;
223 return -EILSEQ;
224 }
Juuso Oikarinen02fabb02010-08-19 04:41:15 +0200225
Shahar Levibc765bf2011-03-06 16:32:10 +0200226 /* only the first part of the NVS needs to be uploaded */
227 nvs_len = sizeof(nvs->nvs);
228 nvs_ptr = (u8 *)nvs->nvs;
229
230 } else {
231 struct wl1271_nvs_file *nvs =
232 (struct wl1271_nvs_file *)wl->nvs;
233 /*
234 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
235 * band configurations) can be removed when those NVS files stop
236 * floating around.
237 */
238 if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
239 wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
Arik Nemtsovcabb81c2011-08-23 15:56:22 +0300240 if (nvs->general_params.dual_mode_select)
Shahar Levibc765bf2011-03-06 16:32:10 +0200241 wl->enable_11a = true;
242 }
243
244 if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
245 (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
246 wl->enable_11a)) {
247 wl1271_error("nvs size is not as expected: %zu != %zu",
248 wl->nvs_len, sizeof(struct wl1271_nvs_file));
249 kfree(wl->nvs);
250 wl->nvs = NULL;
251 wl->nvs_len = 0;
252 return -EILSEQ;
253 }
254
255 /* only the first part of the NVS needs to be uploaded */
256 nvs_len = sizeof(nvs->nvs);
257 nvs_ptr = (u8 *) nvs->nvs;
258 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300259
Juuso Oikarinen1b72aec2010-03-18 12:26:39 +0200260 /* update current MAC address to NVS */
261 nvs_ptr[11] = wl->mac_addr[0];
262 nvs_ptr[10] = wl->mac_addr[1];
263 nvs_ptr[6] = wl->mac_addr[2];
264 nvs_ptr[5] = wl->mac_addr[3];
265 nvs_ptr[4] = wl->mac_addr[4];
266 nvs_ptr[3] = wl->mac_addr[5];
267
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300268 /*
269 * Layout before the actual NVS tables:
270 * 1 byte : burst length.
271 * 2 bytes: destination address.
272 * n bytes: data to burst copy.
273 *
274 * This is ended by a 0 length, then the NVS tables.
275 */
276
277 /* FIXME: Do we need to check here whether the LSB is 1? */
278 while (nvs_ptr[0]) {
279 burst_len = nvs_ptr[0];
280 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
281
Juuso Oikarinen2f63b012010-08-10 06:38:35 +0200282 /*
283 * Due to our new wl1271_translate_reg_addr function,
284 * we need to add the REGISTER_BASE to the destination
285 */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300286 dest_addr += REGISTERS_BASE;
287
288 /* We move our pointer to the data */
289 nvs_ptr += 3;
290
291 for (i = 0; i < burst_len; i++) {
Pontus Fuchsf6efe962011-10-18 09:23:42 +0200292 if (nvs_ptr + 3 >= (u8 *) wl->nvs + nvs_len)
293 goto out_badnvs;
294
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300295 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
296 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
297
298 wl1271_debug(DEBUG_BOOT,
299 "nvs burst write 0x%x: 0x%x",
300 dest_addr, val);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200301 wl1271_write32(wl, dest_addr, val);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300302
303 nvs_ptr += 4;
304 dest_addr += 4;
305 }
Pontus Fuchsf6efe962011-10-18 09:23:42 +0200306
307 if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
308 goto out_badnvs;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300309 }
310
311 /*
312 * We've reached the first zero length, the first NVS table
Ido Yariv67e02082010-09-22 09:53:13 +0200313 * is located at an aligned offset which is at least 7 bytes further.
Shahar Levibc765bf2011-03-06 16:32:10 +0200314 * NOTE: The wl->nvs->nvs element must be first, in order to
315 * simplify the casting, we assume it is at the beginning of
316 * the wl->nvs structure.
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300317 */
Shahar Levibc765bf2011-03-06 16:32:10 +0200318 nvs_ptr = (u8 *)wl->nvs +
319 ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
Pontus Fuchsf6efe962011-10-18 09:23:42 +0200320
321 if (nvs_ptr >= (u8 *) wl->nvs + nvs_len)
322 goto out_badnvs;
323
Shahar Levibc765bf2011-03-06 16:32:10 +0200324 nvs_len -= nvs_ptr - (u8 *)wl->nvs;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300325
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300326 /* Now we must set the partition correctly */
Luciano Coelho0becb142012-01-12 14:45:34 +0200327 wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300328
329 /* Copy the NVS tables to a new block to ensure alignment */
Ido Yariv67e02082010-09-22 09:53:13 +0200330 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
331 if (!nvs_aligned)
332 return -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300333
334 /* And finally we upload the NVS tables */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200335 wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300336
337 kfree(nvs_aligned);
338 return 0;
Pontus Fuchsf6efe962011-10-18 09:23:42 +0200339
340out_badnvs:
341 wl1271_error("nvs data is malformed");
342 return -EILSEQ;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300343}
344
345static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
346{
Teemu Paasikivi54f7e502010-02-22 08:38:22 +0200347 wl1271_enable_interrupts(wl);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200348 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
349 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
350 wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300351}
352
353static int wl1271_boot_soft_reset(struct wl1271 *wl)
354{
355 unsigned long timeout;
356 u32 boot_data;
357
358 /* perform soft reset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200359 wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300360
361 /* SOFT_RESET is self clearing */
362 timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
363 while (1) {
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200364 boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300365 wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
366 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
367 break;
368
369 if (time_after(jiffies, timeout)) {
370 /* 1.2 check pWhalBus->uSelfClearTime if the
371 * timeout was reached */
372 wl1271_error("soft reset timeout");
373 return -1;
374 }
375
376 udelay(SOFT_RESET_STALL_TIME);
377 }
378
379 /* disable Rx/Tx */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200380 wl1271_write32(wl, ENABLE, 0x0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300381
382 /* disable auto calibration on start*/
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200383 wl1271_write32(wl, SPARE_A2, 0xffff);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300384
385 return 0;
386}
387
388static int wl1271_boot_run_firmware(struct wl1271 *wl)
389{
390 int loop, ret;
Luciano Coelho23a7a512010-04-28 09:50:02 +0300391 u32 chip_id, intr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300392
393 wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
394
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200395 chip_id = wl1271_read32(wl, CHIP_ID_B);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396
397 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
398
399 if (chip_id != wl->chip.id) {
400 wl1271_error("chip id doesn't match after firmware boot");
401 return -EIO;
402 }
403
404 /* wait for init to complete */
405 loop = 0;
406 while (loop++ < INIT_LOOP) {
407 udelay(INIT_LOOP_DELAY);
Luciano Coelho23a7a512010-04-28 09:50:02 +0300408 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300409
Luciano Coelho23a7a512010-04-28 09:50:02 +0300410 if (intr == 0xffffffff) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300411 wl1271_error("error reading hardware complete "
412 "init indication");
413 return -EIO;
414 }
415 /* check that ACX_INTR_INIT_COMPLETE is enabled */
Luciano Coelho23a7a512010-04-28 09:50:02 +0300416 else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200417 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
418 WL1271_ACX_INTR_INIT_COMPLETE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300419 break;
420 }
421 }
422
Luciano Coelhoe7d17cf2009-10-29 13:20:04 +0200423 if (loop > INIT_LOOP) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300424 wl1271_error("timeout waiting for the hardware to "
425 "complete initialization");
426 return -EIO;
427 }
428
429 /* get hardware config command mail box */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200430 wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300431
432 /* get hardware config event mail box */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200433 wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300434
435 /* set the working partition to its "running" mode offset */
Luciano Coelho0becb142012-01-12 14:45:34 +0200436 wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300437
438 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
439 wl->cmd_box_addr, wl->event_box_addr);
440
441 wl1271_boot_fw_version(wl);
442
443 /*
444 * in case of full asynchronous mode the firmware event must be
445 * ready to receive event from the command mailbox
446 */
447
Juuso Oikarinenbe823e52009-10-08 21:56:36 +0300448 /* unmask required mbox events */
449 wl->event_mask = BSS_LOSE_EVENT_ID |
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200450 SCAN_COMPLETE_EVENT_ID |
Luciano Coelho99d84c12010-03-26 12:53:20 +0200451 PS_REPORT_EVENT_ID |
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300452 DISCONNECT_EVENT_COMPLETE_ID |
Juuso Oikarinen90494a92010-07-08 17:50:00 +0300453 RSSI_SNR_TRIGGER_0_EVENT_ID |
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300454 PSPOLL_DELIVERY_FAILURE_EVENT_ID |
Luciano Coelho6394c012011-05-10 14:28:27 +0300455 SOFT_GEMINI_SENSE_EVENT_ID |
456 PERIODIC_SCAN_REPORT_EVENT_ID |
Eliad Pellerc690ec82011-08-14 13:17:07 +0300457 PERIODIC_SCAN_COMPLETE_EVENT_ID |
458 DUMMY_PACKET_EVENT_ID |
459 PEER_REMOVE_COMPLETE_EVENT_ID |
460 BA_SESSION_RX_CONSTRAINT_EVENT_ID |
461 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID |
462 INACTIVE_STA_EVENT_ID |
Shahar Levi6d158ff2011-09-08 13:01:33 +0300463 MAX_TX_RETRY_EVENT_ID |
464 CHANNEL_SWITCH_COMPLETE_EVENT_ID;
Arik Nemtsov203c9032010-10-25 11:17:44 +0200465
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300466 ret = wl1271_event_unmask(wl);
467 if (ret < 0) {
468 wl1271_error("EVENT mask setting failed");
469 return ret;
470 }
471
472 wl1271_event_mbox_config(wl);
473
474 /* firmware startup completed */
475 return 0;
476}
477
478static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
479{
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300480 u32 polarity;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300481
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300482 polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300483
484 /* We use HIGH polarity, so unset the LOW bit */
485 polarity &= ~POLARITY_LOW;
Juuso Oikarinene8768ee2009-10-12 15:08:48 +0300486 wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300487
488 return 0;
489}
490
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300491static void wl1271_boot_hw_version(struct wl1271 *wl)
492{
493 u32 fuse;
494
Gery Kahn6f07b722011-07-18 14:21:49 +0300495 if (wl->chip.id == CHIP_ID_1283_PG20)
496 fuse = wl1271_top_reg_read(wl, WL128X_REG_FUSE_DATA_2_1);
497 else
498 fuse = wl1271_top_reg_read(wl, WL127X_REG_FUSE_DATA_2_1);
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300499 fuse = (fuse & PG_VER_MASK) >> PG_VER_OFFSET;
500
501 wl->hw_pg_ver = (s8)fuse;
502}
503
Ido Yarivd29633b2011-03-31 10:06:57 +0200504static int wl128x_switch_tcxo_to_fref(struct wl1271 *wl)
505{
506 u16 spare_reg;
507
508 /* Mask bits [2] & [8:4] in the sys_clk_cfg register */
509 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
510 if (spare_reg == 0xFFFF)
511 return -EFAULT;
512 spare_reg |= (BIT(3) | BIT(5) | BIT(6));
513 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
514
515 /* Enable FREF_CLK_REQ & mux MCS and coex PLLs to FREF */
516 wl1271_top_reg_write(wl, SYS_CLK_CFG_REG,
517 WL_CLK_REQ_TYPE_PG2 | MCS_PLL_CLK_SEL_FREF);
518
519 /* Delay execution for 15msec, to let the HW settle */
520 mdelay(15);
521
522 return 0;
523}
524
525static bool wl128x_is_tcxo_valid(struct wl1271 *wl)
526{
527 u16 tcxo_detection;
528
529 tcxo_detection = wl1271_top_reg_read(wl, TCXO_CLK_DETECT_REG);
530 if (tcxo_detection & TCXO_DET_FAILED)
531 return false;
532
533 return true;
534}
535
536static bool wl128x_is_fref_valid(struct wl1271 *wl)
537{
538 u16 fref_detection;
539
540 fref_detection = wl1271_top_reg_read(wl, FREF_CLK_DETECT_REG);
541 if (fref_detection & FREF_CLK_DETECT_FAIL)
542 return false;
543
544 return true;
545}
546
547static int wl128x_manually_configure_mcs_pll(struct wl1271 *wl)
548{
549 wl1271_top_reg_write(wl, MCS_PLL_M_REG, MCS_PLL_M_REG_VAL);
550 wl1271_top_reg_write(wl, MCS_PLL_N_REG, MCS_PLL_N_REG_VAL);
551 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, MCS_PLL_CONFIG_REG_VAL);
552
553 return 0;
554}
555
556static int wl128x_configure_mcs_pll(struct wl1271 *wl, int clk)
557{
558 u16 spare_reg;
559 u16 pll_config;
560 u8 input_freq;
561
562 /* Mask bits [3:1] in the sys_clk_cfg register */
563 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
564 if (spare_reg == 0xFFFF)
565 return -EFAULT;
566 spare_reg |= BIT(2);
567 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
568
569 /* Handle special cases of the TCXO clock */
570 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_8 ||
571 wl->tcxo_clock == WL12XX_TCXOCLOCK_33_6)
572 return wl128x_manually_configure_mcs_pll(wl);
573
574 /* Set the input frequency according to the selected clock source */
575 input_freq = (clk & 1) + 1;
576
577 pll_config = wl1271_top_reg_read(wl, MCS_PLL_CONFIG_REG);
578 if (pll_config == 0xFFFF)
579 return -EFAULT;
580 pll_config |= (input_freq << MCS_SEL_IN_FREQ_SHIFT);
581 pll_config |= MCS_PLL_ENABLE_HP;
582 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, pll_config);
583
584 return 0;
585}
586
Shahar Levi5ea417a2011-03-06 16:32:11 +0200587/*
588 * WL128x has two clocks input - TCXO and FREF.
589 * TCXO is the main clock of the device, while FREF is used to sync
590 * between the GPS and the cellular modem.
591 * In cases where TCXO is 32.736MHz or 16.368MHz, the FREF will be used
592 * as the WLAN/BT main clock.
593 */
Ido Yarivd29633b2011-03-31 10:06:57 +0200594static int wl128x_boot_clk(struct wl1271 *wl, int *selected_clock)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300595{
Ido Yarivd29633b2011-03-31 10:06:57 +0200596 u16 sys_clk_cfg;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200597
Ido Yarivd29633b2011-03-31 10:06:57 +0200598 /* For XTAL-only modes, FREF will be used after switching from TCXO */
599 if (wl->ref_clock == WL12XX_REFCLOCK_26_XTAL ||
600 wl->ref_clock == WL12XX_REFCLOCK_38_XTAL) {
601 if (!wl128x_switch_tcxo_to_fref(wl))
602 return -EINVAL;
603 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200604 }
605
Ido Yarivd29633b2011-03-31 10:06:57 +0200606 /* Query the HW, to determine which clock source we should use */
607 sys_clk_cfg = wl1271_top_reg_read(wl, SYS_CLK_CFG_REG);
608 if (sys_clk_cfg == 0xFFFF)
609 return -EINVAL;
610 if (sys_clk_cfg & PRCM_CM_EN_MUX_WLAN_FREF)
611 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200612
Ido Yarivd29633b2011-03-31 10:06:57 +0200613 /* If TCXO is either 32.736MHz or 16.368MHz, switch to FREF */
614 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_368 ||
615 wl->tcxo_clock == WL12XX_TCXOCLOCK_32_736) {
616 if (!wl128x_switch_tcxo_to_fref(wl))
617 return -EINVAL;
618 goto fref_clk;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200619 }
620
Ido Yarivd29633b2011-03-31 10:06:57 +0200621 /* TCXO clock is selected */
622 if (!wl128x_is_tcxo_valid(wl))
623 return -EINVAL;
624 *selected_clock = wl->tcxo_clock;
625 goto config_mcs_pll;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200626
Ido Yarivd29633b2011-03-31 10:06:57 +0200627fref_clk:
628 /* FREF clock is selected */
629 if (!wl128x_is_fref_valid(wl))
630 return -EINVAL;
631 *selected_clock = wl->ref_clock;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200632
Ido Yarivd29633b2011-03-31 10:06:57 +0200633config_mcs_pll:
634 return wl128x_configure_mcs_pll(wl, *selected_clock);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200635}
636
637static int wl127x_boot_clk(struct wl1271 *wl)
638{
639 u32 pause;
640 u32 clk;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300641
Gery Kahn6f07b722011-07-18 14:21:49 +0300642 if (((wl->hw_pg_ver & PG_MAJOR_VER_MASK) >> PG_MAJOR_VER_OFFSET) < 3)
643 wl->quirks |= WL12XX_QUIRK_END_OF_TRANSACTION;
Juuso Oikarinend717fd62010-05-07 11:38:58 +0300644
Shahar Levi5ea417a2011-03-06 16:32:11 +0200645 if (wl->ref_clock == CONF_REF_CLK_19_2_E ||
646 wl->ref_clock == CONF_REF_CLK_38_4_E ||
647 wl->ref_clock == CONF_REF_CLK_38_4_M_XTAL)
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300648 /* ref clk: 19.2/38.4/38.4-XTAL */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300649 clk = 0x3;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200650 else if (wl->ref_clock == CONF_REF_CLK_26_E ||
651 wl->ref_clock == CONF_REF_CLK_52_E)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300652 /* ref clk: 26/52 */
653 clk = 0x5;
Ohad Ben-Cohen15cea992010-09-16 01:31:51 +0200654 else
655 return -EINVAL;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300656
Shahar Levi5ea417a2011-03-06 16:32:11 +0200657 if (wl->ref_clock != CONF_REF_CLK_19_2_E) {
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300658 u16 val;
Juuso Oikarinen9d4e5bb2010-03-26 12:53:15 +0200659 /* Set clock type (open drain) */
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300660 val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
661 val &= FREF_CLK_TYPE_BITS;
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300662 wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
Juuso Oikarinen9d4e5bb2010-03-26 12:53:15 +0200663
664 /* Set clock pull mode (no pull) */
665 val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
666 val |= NO_PULL;
667 wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
Juuso Oikarinen284134e2009-10-12 15:08:49 +0300668 } else {
669 u16 val;
670 /* Set clock polarity */
671 val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
672 val &= FREF_CLK_POLARITY_BITS;
673 val |= CLK_REQ_OUTN_SEL;
674 wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
675 }
676
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200677 wl1271_write32(wl, PLL_PARAMETERS, clk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300678
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200679 pause = wl1271_read32(wl, PLL_PARAMETERS);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300680
681 wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
682
Juuso Oikarinen2f63b012010-08-10 06:38:35 +0200683 pause &= ~(WU_COUNTER_PAUSE_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300684 pause |= WU_COUNTER_PAUSE_VAL;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200685 wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300686
Shahar Levi5ea417a2011-03-06 16:32:11 +0200687 return 0;
688}
689
690/* uploads NVS and firmware */
691int wl1271_load_firmware(struct wl1271 *wl)
692{
693 int ret = 0;
694 u32 tmp, clk;
Ido Yarivd29633b2011-03-31 10:06:57 +0200695 int selected_clock = -1;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200696
Gery Kahn6f07b722011-07-18 14:21:49 +0300697 wl1271_boot_hw_version(wl);
698
Shahar Levi5ea417a2011-03-06 16:32:11 +0200699 if (wl->chip.id == CHIP_ID_1283_PG20) {
Ido Yarivd29633b2011-03-31 10:06:57 +0200700 ret = wl128x_boot_clk(wl, &selected_clock);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200701 if (ret < 0)
702 goto out;
703 } else {
704 ret = wl127x_boot_clk(wl);
705 if (ret < 0)
706 goto out;
707 }
708
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300709 /* Continue the ELP wake up sequence */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200710 wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300711 udelay(500);
712
Luciano Coelho0becb142012-01-12 14:45:34 +0200713 wl1271_set_partition(wl, &wl12xx_part_table[PART_DRPW]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300714
715 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
716 to be used by DRPw FW. The RTRIM value will be added by the FW
717 before taking DRPw out of reset */
718
719 wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200720 clk = wl1271_read32(wl, DRPW_SCRATCH_START);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300721
722 wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
723
Shahar Levi5ea417a2011-03-06 16:32:11 +0200724 if (wl->chip.id == CHIP_ID_1283_PG20) {
Ido Yarivd29633b2011-03-31 10:06:57 +0200725 clk |= ((selected_clock & 0x3) << 1) << 4;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200726 } else {
727 clk |= (wl->ref_clock << 1) << 4;
728 }
729
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200730 wl1271_write32(wl, DRPW_SCRATCH_START, clk);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300731
Luciano Coelho0becb142012-01-12 14:45:34 +0200732 wl1271_set_partition(wl, &wl12xx_part_table[PART_WORK]);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300733
734 /* Disable interrupts */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200735 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300736
737 ret = wl1271_boot_soft_reset(wl);
738 if (ret < 0)
739 goto out;
740
741 /* 2. start processing NVS file */
742 ret = wl1271_boot_upload_nvs(wl);
743 if (ret < 0)
744 goto out;
745
746 /* write firmware's last address (ie. it's length) to
747 * ACX_EEPROMLESS_IND_REG */
748 wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
749
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200750 wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300751
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200752 tmp = wl1271_read32(wl, CHIP_ID_B);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300753
754 wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
755
756 /* 6. read the EEPROM parameters */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200757 tmp = wl1271_read32(wl, SCR_PAD2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300758
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300759 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
760 * to upload_fw) */
761
Shahar Levi5ea417a2011-03-06 16:32:11 +0200762 if (wl->chip.id == CHIP_ID_1283_PG20)
Luciano Coelhoafb7d3c2011-04-01 20:48:02 +0300763 wl1271_top_reg_write(wl, SDIO_IO_DS, wl->conf.hci_io_ds);
Shahar Levi5ea417a2011-03-06 16:32:11 +0200764
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300765 ret = wl1271_boot_upload_firmware(wl);
766 if (ret < 0)
767 goto out;
768
Roger Quadros870c3672010-11-29 16:24:57 +0200769out:
770 return ret;
771}
772EXPORT_SYMBOL_GPL(wl1271_load_firmware);
773
774int wl1271_boot(struct wl1271 *wl)
775{
776 int ret;
777
778 /* upload NVS and firmware */
779 ret = wl1271_load_firmware(wl);
780 if (ret)
781 return ret;
782
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300783 /* 10.5 start firmware */
784 ret = wl1271_boot_run_firmware(wl);
785 if (ret < 0)
786 goto out;
787
Shahar Levib9b0fde2011-03-06 16:32:06 +0200788 ret = wl1271_boot_write_irq_polarity(wl);
789 if (ret < 0)
790 goto out;
791
792 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
793 WL1271_ACX_ALL_EVENTS_VECTOR);
794
Juuso Oikarineneb5b28d2009-10-13 12:47:45 +0300795 /* Enable firmware interrupts now */
796 wl1271_boot_enable_interrupts(wl);
797
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300798 wl1271_event_mbox_config(wl);
799
800out:
801 return ret;
802}