wl12xx: Check for FW quirks as soon as the FW boots
[deliverable/linux.git] / drivers / net / wireless / wl12xx / boot.c
CommitLineData
f5fc0f86
LC
1/*
2 * This file is part of wl1271
3 *
2f826f55 4 * Copyright (C) 2008-2010 Nokia Corporation
f5fc0f86
LC
5 *
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
5a0e3ad6 24#include <linux/slab.h>
5ea417ae 25#include <linux/wl12xx.h>
f5fc0f86 26
00d20100
SL
27#include "acx.h"
28#include "reg.h"
29#include "boot.h"
30#include "io.h"
31#include "event.h"
ae113b57 32#include "rx.h"
f5fc0f86
LC
33
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 },
451de97a
JO
44 .mem2 = {
45 .start = 0x00000000,
46 .size = 0x00000000
47 },
48 .mem3 = {
49 .start = 0x00000000,
50 .size = 0x00000000
51 },
f5fc0f86
LC
52 },
53
54 [PART_WORK] = {
55 .mem = {
56 .start = 0x00040000,
57 .size = 0x00014fc0
58 },
59 .reg = {
60 .start = REGISTERS_BASE,
451de97a
JO
61 .size = 0x0000a000
62 },
63 .mem2 = {
64 .start = 0x003004f8,
65 .size = 0x00000004
66 },
67 .mem3 = {
68 .start = 0x00040404,
69 .size = 0x00000000
f5fc0f86
LC
70 },
71 },
72
73 [PART_DRPW] = {
74 .mem = {
75 .start = 0x00040000,
76 .size = 0x00014fc0
77 },
78 .reg = {
79 .start = DRPW_BASE,
80 .size = 0x00006000
451de97a
JO
81 },
82 .mem2 = {
83 .start = 0x00000000,
84 .size = 0x00000000
85 },
86 .mem3 = {
87 .start = 0x00000000,
88 .size = 0x00000000
f5fc0f86
LC
89 }
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) */
7b048c52 98 cpu_ctrl = wl1271_read32(wl, ACX_REG_ECPU_CONTROL);
f5fc0f86
LC
99
100 /* 10.5.1 run the firmware (II) */
101 cpu_ctrl |= flag;
7b048c52 102 wl1271_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
f5fc0f86
LC
103}
104
842f1a6c
IY
105static 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
110 /* Only for wl127x */
111 if ((fw_ver[FW_VER_CHIP] == FW_VER_CHIP_WL127X) &&
112 /* Check STA version */
113 (((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_STA) &&
114 (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_1_SPARE_STA_MIN)) ||
115 /* Check AP version */
116 ((fw_ver[FW_VER_IF_TYPE] == FW_VER_IF_TYPE_AP) &&
117 (fw_ver[FW_VER_MINOR] < FW_VER_MINOR_1_SPARE_AP_MIN))))
118 quirks |= WL12XX_QUIRK_USE_2_SPARE_BLOCKS;
119
120 return quirks;
121}
122
4b7fac77
LS
123static void wl1271_parse_fw_ver(struct wl1271 *wl)
124{
125 int ret;
126
127 ret = sscanf(wl->chip.fw_ver_str + 4, "%u.%u.%u.%u.%u",
128 &wl->chip.fw_ver[0], &wl->chip.fw_ver[1],
129 &wl->chip.fw_ver[2], &wl->chip.fw_ver[3],
130 &wl->chip.fw_ver[4]);
131
132 if (ret != 5) {
133 wl1271_warning("fw version incorrect value");
134 memset(wl->chip.fw_ver, 0, sizeof(wl->chip.fw_ver));
135 return;
136 }
842f1a6c
IY
137
138 /* Check if any quirks are needed with older fw versions */
139 wl->quirks |= wl12xx_get_fw_ver_quirks(wl);
4b7fac77
LS
140}
141
f5fc0f86
LC
142static void wl1271_boot_fw_version(struct wl1271 *wl)
143{
144 struct wl1271_static_data static_data;
145
7b048c52
TP
146 wl1271_read(wl, wl->cmd_box_addr, &static_data, sizeof(static_data),
147 false);
f5fc0f86 148
4b7fac77
LS
149 strncpy(wl->chip.fw_ver_str, static_data.fw_version,
150 sizeof(wl->chip.fw_ver_str));
f5fc0f86
LC
151
152 /* make sure the string is NULL-terminated */
4b7fac77
LS
153 wl->chip.fw_ver_str[sizeof(wl->chip.fw_ver_str) - 1] = '\0';
154
155 wl1271_parse_fw_ver(wl);
f5fc0f86
LC
156}
157
158static int wl1271_boot_upload_firmware_chunk(struct wl1271 *wl, void *buf,
159 size_t fw_data_len, u32 dest)
160{
451de97a 161 struct wl1271_partition_set partition;
f5fc0f86 162 int addr, chunk_num, partition_limit;
1fba4974 163 u8 *p, *chunk;
f5fc0f86
LC
164
165 /* whal_FwCtrl_LoadFwImageSm() */
166
167 wl1271_debug(DEBUG_BOOT, "starting firmware upload");
168
73d0a13c
LC
169 wl1271_debug(DEBUG_BOOT, "fw_data_len %zd chunk_size %d",
170 fw_data_len, CHUNK_SIZE);
f5fc0f86 171
f5fc0f86
LC
172 if ((fw_data_len % 4) != 0) {
173 wl1271_error("firmware length not multiple of four");
174 return -EIO;
175 }
176
1fba4974 177 chunk = kmalloc(CHUNK_SIZE, GFP_KERNEL);
ed317788 178 if (!chunk) {
1fba4974
JO
179 wl1271_error("allocation for firmware upload chunk failed");
180 return -ENOMEM;
181 }
182
451de97a
JO
183 memcpy(&partition, &part_table[PART_DOWN], sizeof(partition));
184 partition.mem.start = dest;
185 wl1271_set_partition(wl, &partition);
f5fc0f86
LC
186
187 /* 10.1 set partition limit and chunk num */
188 chunk_num = 0;
189 partition_limit = part_table[PART_DOWN].mem.size;
190
191 while (chunk_num < fw_data_len / CHUNK_SIZE) {
192 /* 10.2 update partition, if needed */
193 addr = dest + (chunk_num + 2) * CHUNK_SIZE;
194 if (addr > partition_limit) {
195 addr = dest + chunk_num * CHUNK_SIZE;
196 partition_limit = chunk_num * CHUNK_SIZE +
197 part_table[PART_DOWN].mem.size;
451de97a
JO
198 partition.mem.start = addr;
199 wl1271_set_partition(wl, &partition);
f5fc0f86
LC
200 }
201
202 /* 10.3 upload the chunk */
203 addr = dest + chunk_num * CHUNK_SIZE;
204 p = buf + chunk_num * CHUNK_SIZE;
1fba4974 205 memcpy(chunk, p, CHUNK_SIZE);
f5fc0f86
LC
206 wl1271_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
207 p, addr);
7b048c52 208 wl1271_write(wl, addr, chunk, CHUNK_SIZE, false);
f5fc0f86
LC
209
210 chunk_num++;
211 }
212
213 /* 10.4 upload the last chunk */
214 addr = dest + chunk_num * CHUNK_SIZE;
215 p = buf + chunk_num * CHUNK_SIZE;
1fba4974 216 memcpy(chunk, p, fw_data_len % CHUNK_SIZE);
73d0a13c 217 wl1271_debug(DEBUG_BOOT, "uploading fw last chunk (%zd B) 0x%p to 0x%x",
f5fc0f86 218 fw_data_len % CHUNK_SIZE, p, addr);
7b048c52 219 wl1271_write(wl, addr, chunk, fw_data_len % CHUNK_SIZE, false);
f5fc0f86 220
1fba4974 221 kfree(chunk);
f5fc0f86
LC
222 return 0;
223}
224
225static int wl1271_boot_upload_firmware(struct wl1271 *wl)
226{
227 u32 chunks, addr, len;
ed317788 228 int ret = 0;
f5fc0f86
LC
229 u8 *fw;
230
231 fw = wl->fw;
d0f63b20 232 chunks = be32_to_cpup((__be32 *) fw);
f5fc0f86
LC
233 fw += sizeof(u32);
234
235 wl1271_debug(DEBUG_BOOT, "firmware chunks to be uploaded: %u", chunks);
236
237 while (chunks--) {
d0f63b20 238 addr = be32_to_cpup((__be32 *) fw);
f5fc0f86 239 fw += sizeof(u32);
d0f63b20 240 len = be32_to_cpup((__be32 *) fw);
f5fc0f86
LC
241 fw += sizeof(u32);
242
243 if (len > 300000) {
244 wl1271_info("firmware chunk too long: %u", len);
245 return -EINVAL;
246 }
247 wl1271_debug(DEBUG_BOOT, "chunk %d addr 0x%x len %u",
248 chunks, addr, len);
ed317788
JO
249 ret = wl1271_boot_upload_firmware_chunk(wl, fw, len, addr);
250 if (ret != 0)
251 break;
f5fc0f86
LC
252 fw += len;
253 }
254
ed317788 255 return ret;
f5fc0f86
LC
256}
257
258static int wl1271_boot_upload_nvs(struct wl1271 *wl)
259{
260 size_t nvs_len, burst_len;
261 int i;
262 u32 dest_addr, val;
152ee6e0 263 u8 *nvs_ptr, *nvs_aligned;
f5fc0f86 264
152ee6e0 265 if (wl->nvs == NULL)
f5fc0f86
LC
266 return -ENODEV;
267
bc765bf3
SL
268 if (wl->chip.id == CHIP_ID_1283_PG20) {
269 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
270
271 if (wl->nvs_len == sizeof(struct wl128x_nvs_file)) {
272 if (nvs->general_params.dual_mode_select)
273 wl->enable_11a = true;
274 } else {
275 wl1271_error("nvs size is not as expected: %zu != %zu",
276 wl->nvs_len,
277 sizeof(struct wl128x_nvs_file));
278 kfree(wl->nvs);
279 wl->nvs = NULL;
280 wl->nvs_len = 0;
281 return -EILSEQ;
282 }
02fabb0e 283
bc765bf3
SL
284 /* only the first part of the NVS needs to be uploaded */
285 nvs_len = sizeof(nvs->nvs);
286 nvs_ptr = (u8 *)nvs->nvs;
287
288 } else {
289 struct wl1271_nvs_file *nvs =
290 (struct wl1271_nvs_file *)wl->nvs;
291 /*
292 * FIXME: the LEGACY NVS image support (NVS's missing the 5GHz
293 * band configurations) can be removed when those NVS files stop
294 * floating around.
295 */
296 if (wl->nvs_len == sizeof(struct wl1271_nvs_file) ||
297 wl->nvs_len == WL1271_INI_LEGACY_NVS_FILE_SIZE) {
298 /* for now 11a is unsupported in AP mode */
299 if (wl->bss_type != BSS_TYPE_AP_BSS &&
300 nvs->general_params.dual_mode_select)
301 wl->enable_11a = true;
302 }
02fabb0e 303
bc765bf3
SL
304 if (wl->nvs_len != sizeof(struct wl1271_nvs_file) &&
305 (wl->nvs_len != WL1271_INI_LEGACY_NVS_FILE_SIZE ||
306 wl->enable_11a)) {
307 wl1271_error("nvs size is not as expected: %zu != %zu",
308 wl->nvs_len, sizeof(struct wl1271_nvs_file));
309 kfree(wl->nvs);
310 wl->nvs = NULL;
311 wl->nvs_len = 0;
312 return -EILSEQ;
313 }
314
315 /* only the first part of the NVS needs to be uploaded */
316 nvs_len = sizeof(nvs->nvs);
317 nvs_ptr = (u8 *) nvs->nvs;
318 }
f5fc0f86 319
1b72aecd
JO
320 /* update current MAC address to NVS */
321 nvs_ptr[11] = wl->mac_addr[0];
322 nvs_ptr[10] = wl->mac_addr[1];
323 nvs_ptr[6] = wl->mac_addr[2];
324 nvs_ptr[5] = wl->mac_addr[3];
325 nvs_ptr[4] = wl->mac_addr[4];
326 nvs_ptr[3] = wl->mac_addr[5];
327
f5fc0f86
LC
328 /*
329 * Layout before the actual NVS tables:
330 * 1 byte : burst length.
331 * 2 bytes: destination address.
332 * n bytes: data to burst copy.
333 *
334 * This is ended by a 0 length, then the NVS tables.
335 */
336
337 /* FIXME: Do we need to check here whether the LSB is 1? */
338 while (nvs_ptr[0]) {
339 burst_len = nvs_ptr[0];
340 dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));
341
2f63b011
JO
342 /*
343 * Due to our new wl1271_translate_reg_addr function,
344 * we need to add the REGISTER_BASE to the destination
345 */
f5fc0f86
LC
346 dest_addr += REGISTERS_BASE;
347
348 /* We move our pointer to the data */
349 nvs_ptr += 3;
350
351 for (i = 0; i < burst_len; i++) {
352 val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
353 | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));
354
355 wl1271_debug(DEBUG_BOOT,
356 "nvs burst write 0x%x: 0x%x",
357 dest_addr, val);
7b048c52 358 wl1271_write32(wl, dest_addr, val);
f5fc0f86
LC
359
360 nvs_ptr += 4;
361 dest_addr += 4;
362 }
363 }
364
365 /*
366 * We've reached the first zero length, the first NVS table
67e0208a 367 * is located at an aligned offset which is at least 7 bytes further.
bc765bf3
SL
368 * NOTE: The wl->nvs->nvs element must be first, in order to
369 * simplify the casting, we assume it is at the beginning of
370 * the wl->nvs structure.
f5fc0f86 371 */
bc765bf3
SL
372 nvs_ptr = (u8 *)wl->nvs +
373 ALIGN(nvs_ptr - (u8 *)wl->nvs + 7, 4);
374 nvs_len -= nvs_ptr - (u8 *)wl->nvs;
f5fc0f86 375
f5fc0f86 376 /* Now we must set the partition correctly */
451de97a 377 wl1271_set_partition(wl, &part_table[PART_WORK]);
f5fc0f86
LC
378
379 /* Copy the NVS tables to a new block to ensure alignment */
67e0208a
IY
380 nvs_aligned = kmemdup(nvs_ptr, nvs_len, GFP_KERNEL);
381 if (!nvs_aligned)
382 return -ENOMEM;
f5fc0f86
LC
383
384 /* And finally we upload the NVS tables */
7b048c52 385 wl1271_write(wl, CMD_MBOX_ADDRESS, nvs_aligned, nvs_len, false);
f5fc0f86
LC
386
387 kfree(nvs_aligned);
388 return 0;
389}
390
391static void wl1271_boot_enable_interrupts(struct wl1271 *wl)
392{
54f7e503 393 wl1271_enable_interrupts(wl);
7b048c52
TP
394 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
395 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
396 wl1271_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
f5fc0f86
LC
397}
398
399static int wl1271_boot_soft_reset(struct wl1271 *wl)
400{
401 unsigned long timeout;
402 u32 boot_data;
403
404 /* perform soft reset */
7b048c52 405 wl1271_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
f5fc0f86
LC
406
407 /* SOFT_RESET is self clearing */
408 timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
409 while (1) {
7b048c52 410 boot_data = wl1271_read32(wl, ACX_REG_SLV_SOFT_RESET);
f5fc0f86
LC
411 wl1271_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
412 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
413 break;
414
415 if (time_after(jiffies, timeout)) {
416 /* 1.2 check pWhalBus->uSelfClearTime if the
417 * timeout was reached */
418 wl1271_error("soft reset timeout");
419 return -1;
420 }
421
422 udelay(SOFT_RESET_STALL_TIME);
423 }
424
425 /* disable Rx/Tx */
7b048c52 426 wl1271_write32(wl, ENABLE, 0x0);
f5fc0f86
LC
427
428 /* disable auto calibration on start*/
7b048c52 429 wl1271_write32(wl, SPARE_A2, 0xffff);
f5fc0f86
LC
430
431 return 0;
432}
433
434static int wl1271_boot_run_firmware(struct wl1271 *wl)
435{
436 int loop, ret;
23a7a51c 437 u32 chip_id, intr;
f5fc0f86
LC
438
439 wl1271_boot_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
440
7b048c52 441 chip_id = wl1271_read32(wl, CHIP_ID_B);
f5fc0f86
LC
442
443 wl1271_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
444
445 if (chip_id != wl->chip.id) {
446 wl1271_error("chip id doesn't match after firmware boot");
447 return -EIO;
448 }
449
450 /* wait for init to complete */
451 loop = 0;
452 while (loop++ < INIT_LOOP) {
453 udelay(INIT_LOOP_DELAY);
23a7a51c 454 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
f5fc0f86 455
23a7a51c 456 if (intr == 0xffffffff) {
f5fc0f86
LC
457 wl1271_error("error reading hardware complete "
458 "init indication");
459 return -EIO;
460 }
461 /* check that ACX_INTR_INIT_COMPLETE is enabled */
23a7a51c 462 else if (intr & WL1271_ACX_INTR_INIT_COMPLETE) {
7b048c52
TP
463 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
464 WL1271_ACX_INTR_INIT_COMPLETE);
f5fc0f86
LC
465 break;
466 }
467 }
468
e7d17cf4 469 if (loop > INIT_LOOP) {
f5fc0f86
LC
470 wl1271_error("timeout waiting for the hardware to "
471 "complete initialization");
472 return -EIO;
473 }
474
475 /* get hardware config command mail box */
7b048c52 476 wl->cmd_box_addr = wl1271_read32(wl, REG_COMMAND_MAILBOX_PTR);
f5fc0f86
LC
477
478 /* get hardware config event mail box */
7b048c52 479 wl->event_box_addr = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
f5fc0f86
LC
480
481 /* set the working partition to its "running" mode offset */
451de97a 482 wl1271_set_partition(wl, &part_table[PART_WORK]);
f5fc0f86
LC
483
484 wl1271_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
485 wl->cmd_box_addr, wl->event_box_addr);
486
487 wl1271_boot_fw_version(wl);
488
489 /*
490 * in case of full asynchronous mode the firmware event must be
491 * ready to receive event from the command mailbox
492 */
493
be823e5b
JO
494 /* unmask required mbox events */
495 wl->event_mask = BSS_LOSE_EVENT_ID |
19ad0715 496 SCAN_COMPLETE_EVENT_ID |
99d84c1d 497 PS_REPORT_EVENT_ID |
2f826f55 498 JOIN_EVENT_COMPLETE_ID |
00236aed 499 DISCONNECT_EVENT_COMPLETE_ID |
90494a90 500 RSSI_SNR_TRIGGER_0_EVENT_ID |
8d2ef7bd 501 PSPOLL_DELIVERY_FAILURE_EVENT_ID |
6394c01b
LC
502 SOFT_GEMINI_SENSE_EVENT_ID |
503 PERIODIC_SCAN_REPORT_EVENT_ID |
504 PERIODIC_SCAN_COMPLETE_EVENT_ID;
f5fc0f86 505
203c903c 506 if (wl->bss_type == BSS_TYPE_AP_BSS)
25eaea30 507 wl->event_mask |= STA_REMOVE_COMPLETE_EVENT_ID;
ae47c45f 508 else
70559a06
SL
509 wl->event_mask |= DUMMY_PACKET_EVENT_ID |
510 BA_SESSION_RX_CONSTRAINT_EVENT_ID;
203c903c 511
f5fc0f86
LC
512 ret = wl1271_event_unmask(wl);
513 if (ret < 0) {
514 wl1271_error("EVENT mask setting failed");
515 return ret;
516 }
517
518 wl1271_event_mbox_config(wl);
519
520 /* firmware startup completed */
521 return 0;
522}
523
524static int wl1271_boot_write_irq_polarity(struct wl1271 *wl)
525{
e8768eeb 526 u32 polarity;
f5fc0f86 527
e8768eeb 528 polarity = wl1271_top_reg_read(wl, OCP_REG_POLARITY);
f5fc0f86
LC
529
530 /* We use HIGH polarity, so unset the LOW bit */
531 polarity &= ~POLARITY_LOW;
e8768eeb 532 wl1271_top_reg_write(wl, OCP_REG_POLARITY, polarity);
f5fc0f86
LC
533
534 return 0;
535}
536
d717fd61
JO
537static void wl1271_boot_hw_version(struct wl1271 *wl)
538{
539 u32 fuse;
540
541 fuse = wl1271_top_reg_read(wl, REG_FUSE_DATA_2_1);
542 fuse = (fuse & PG_VER_MASK) >> PG_VER_OFFSET;
543
544 wl->hw_pg_ver = (s8)fuse;
606ea9fa
IY
545
546 if (((wl->hw_pg_ver & PG_MAJOR_VER_MASK) >> PG_MAJOR_VER_OFFSET) < 3)
547 wl->quirks |= WL12XX_QUIRK_END_OF_TRANSACTION;
d717fd61
JO
548}
549
d29633b4 550static int wl128x_switch_tcxo_to_fref(struct wl1271 *wl)
f5fc0f86 551{
d29633b4 552 u16 spare_reg;
5ea417ae 553
d29633b4
IY
554 /* Mask bits [2] & [8:4] in the sys_clk_cfg register */
555 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
556 if (spare_reg == 0xFFFF)
557 return -EFAULT;
558 spare_reg |= (BIT(3) | BIT(5) | BIT(6));
559 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
5ea417ae 560
d29633b4
IY
561 /* Enable FREF_CLK_REQ & mux MCS and coex PLLs to FREF */
562 wl1271_top_reg_write(wl, SYS_CLK_CFG_REG,
563 WL_CLK_REQ_TYPE_PG2 | MCS_PLL_CLK_SEL_FREF);
5ea417ae 564
d29633b4
IY
565 /* Delay execution for 15msec, to let the HW settle */
566 mdelay(15);
5ea417ae 567
d29633b4
IY
568 return 0;
569}
5ea417ae 570
d29633b4
IY
571static bool wl128x_is_tcxo_valid(struct wl1271 *wl)
572{
573 u16 tcxo_detection;
574
575 tcxo_detection = wl1271_top_reg_read(wl, TCXO_CLK_DETECT_REG);
576 if (tcxo_detection & TCXO_DET_FAILED)
577 return false;
5ea417ae 578
d29633b4 579 return true;
5ea417ae
SL
580}
581
d29633b4 582static bool wl128x_is_fref_valid(struct wl1271 *wl)
5ea417ae 583{
d29633b4 584 u16 fref_detection;
5ea417ae 585
d29633b4
IY
586 fref_detection = wl1271_top_reg_read(wl, FREF_CLK_DETECT_REG);
587 if (fref_detection & FREF_CLK_DETECT_FAIL)
588 return false;
5ea417ae 589
d29633b4
IY
590 return true;
591}
5ea417ae 592
d29633b4
IY
593static int wl128x_manually_configure_mcs_pll(struct wl1271 *wl)
594{
595 wl1271_top_reg_write(wl, MCS_PLL_M_REG, MCS_PLL_M_REG_VAL);
596 wl1271_top_reg_write(wl, MCS_PLL_N_REG, MCS_PLL_N_REG_VAL);
597 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, MCS_PLL_CONFIG_REG_VAL);
5ea417ae 598
d29633b4
IY
599 return 0;
600}
5ea417ae 601
d29633b4
IY
602static int wl128x_configure_mcs_pll(struct wl1271 *wl, int clk)
603{
604 u16 spare_reg;
605 u16 pll_config;
606 u8 input_freq;
607
608 /* Mask bits [3:1] in the sys_clk_cfg register */
609 spare_reg = wl1271_top_reg_read(wl, WL_SPARE_REG);
610 if (spare_reg == 0xFFFF)
611 return -EFAULT;
612 spare_reg |= BIT(2);
613 wl1271_top_reg_write(wl, WL_SPARE_REG, spare_reg);
614
615 /* Handle special cases of the TCXO clock */
616 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_8 ||
617 wl->tcxo_clock == WL12XX_TCXOCLOCK_33_6)
618 return wl128x_manually_configure_mcs_pll(wl);
619
620 /* Set the input frequency according to the selected clock source */
621 input_freq = (clk & 1) + 1;
622
623 pll_config = wl1271_top_reg_read(wl, MCS_PLL_CONFIG_REG);
624 if (pll_config == 0xFFFF)
625 return -EFAULT;
626 pll_config |= (input_freq << MCS_SEL_IN_FREQ_SHIFT);
627 pll_config |= MCS_PLL_ENABLE_HP;
628 wl1271_top_reg_write(wl, MCS_PLL_CONFIG_REG, pll_config);
5ea417ae 629
d29633b4
IY
630 return 0;
631}
632
633/*
634 * WL128x has two clocks input - TCXO and FREF.
635 * TCXO is the main clock of the device, while FREF is used to sync
636 * between the GPS and the cellular modem.
637 * In cases where TCXO is 32.736MHz or 16.368MHz, the FREF will be used
638 * as the WLAN/BT main clock.
639 */
640static int wl128x_boot_clk(struct wl1271 *wl, int *selected_clock)
641{
642 u16 sys_clk_cfg;
643
644 /* For XTAL-only modes, FREF will be used after switching from TCXO */
645 if (wl->ref_clock == WL12XX_REFCLOCK_26_XTAL ||
646 wl->ref_clock == WL12XX_REFCLOCK_38_XTAL) {
647 if (!wl128x_switch_tcxo_to_fref(wl))
648 return -EINVAL;
649 goto fref_clk;
5ea417ae
SL
650 }
651
d29633b4
IY
652 /* Query the HW, to determine which clock source we should use */
653 sys_clk_cfg = wl1271_top_reg_read(wl, SYS_CLK_CFG_REG);
654 if (sys_clk_cfg == 0xFFFF)
655 return -EINVAL;
656 if (sys_clk_cfg & PRCM_CM_EN_MUX_WLAN_FREF)
657 goto fref_clk;
658
659 /* If TCXO is either 32.736MHz or 16.368MHz, switch to FREF */
660 if (wl->tcxo_clock == WL12XX_TCXOCLOCK_16_368 ||
661 wl->tcxo_clock == WL12XX_TCXOCLOCK_32_736) {
662 if (!wl128x_switch_tcxo_to_fref(wl))
663 return -EINVAL;
664 goto fref_clk;
665 }
666
667 /* TCXO clock is selected */
668 if (!wl128x_is_tcxo_valid(wl))
669 return -EINVAL;
670 *selected_clock = wl->tcxo_clock;
671 goto config_mcs_pll;
672
673fref_clk:
674 /* FREF clock is selected */
675 if (!wl128x_is_fref_valid(wl))
676 return -EINVAL;
677 *selected_clock = wl->ref_clock;
678
679config_mcs_pll:
680 return wl128x_configure_mcs_pll(wl, *selected_clock);
5ea417ae
SL
681}
682
683static int wl127x_boot_clk(struct wl1271 *wl)
684{
685 u32 pause;
686 u32 clk;
f5fc0f86 687
d717fd61
JO
688 wl1271_boot_hw_version(wl);
689
5ea417ae
SL
690 if (wl->ref_clock == CONF_REF_CLK_19_2_E ||
691 wl->ref_clock == CONF_REF_CLK_38_4_E ||
692 wl->ref_clock == CONF_REF_CLK_38_4_M_XTAL)
284134eb 693 /* ref clk: 19.2/38.4/38.4-XTAL */
f5fc0f86 694 clk = 0x3;
5ea417ae
SL
695 else if (wl->ref_clock == CONF_REF_CLK_26_E ||
696 wl->ref_clock == CONF_REF_CLK_52_E)
f5fc0f86
LC
697 /* ref clk: 26/52 */
698 clk = 0x5;
15cea993
OBC
699 else
700 return -EINVAL;
f5fc0f86 701
5ea417ae 702 if (wl->ref_clock != CONF_REF_CLK_19_2_E) {
284134eb 703 u16 val;
9d4e5bb3 704 /* Set clock type (open drain) */
284134eb
JO
705 val = wl1271_top_reg_read(wl, OCP_REG_CLK_TYPE);
706 val &= FREF_CLK_TYPE_BITS;
284134eb 707 wl1271_top_reg_write(wl, OCP_REG_CLK_TYPE, val);
9d4e5bb3
JO
708
709 /* Set clock pull mode (no pull) */
710 val = wl1271_top_reg_read(wl, OCP_REG_CLK_PULL);
711 val |= NO_PULL;
712 wl1271_top_reg_write(wl, OCP_REG_CLK_PULL, val);
284134eb
JO
713 } else {
714 u16 val;
715 /* Set clock polarity */
716 val = wl1271_top_reg_read(wl, OCP_REG_CLK_POLARITY);
717 val &= FREF_CLK_POLARITY_BITS;
718 val |= CLK_REQ_OUTN_SEL;
719 wl1271_top_reg_write(wl, OCP_REG_CLK_POLARITY, val);
720 }
721
7b048c52 722 wl1271_write32(wl, PLL_PARAMETERS, clk);
f5fc0f86 723
7b048c52 724 pause = wl1271_read32(wl, PLL_PARAMETERS);
f5fc0f86
LC
725
726 wl1271_debug(DEBUG_BOOT, "pause1 0x%x", pause);
727
2f63b011 728 pause &= ~(WU_COUNTER_PAUSE_VAL);
f5fc0f86 729 pause |= WU_COUNTER_PAUSE_VAL;
7b048c52 730 wl1271_write32(wl, WU_COUNTER_PAUSE, pause);
f5fc0f86 731
5ea417ae
SL
732 return 0;
733}
734
735/* uploads NVS and firmware */
736int wl1271_load_firmware(struct wl1271 *wl)
737{
738 int ret = 0;
739 u32 tmp, clk;
d29633b4 740 int selected_clock = -1;
5ea417ae
SL
741
742 if (wl->chip.id == CHIP_ID_1283_PG20) {
d29633b4 743 ret = wl128x_boot_clk(wl, &selected_clock);
5ea417ae
SL
744 if (ret < 0)
745 goto out;
746 } else {
747 ret = wl127x_boot_clk(wl);
748 if (ret < 0)
749 goto out;
750 }
751
f5fc0f86 752 /* Continue the ELP wake up sequence */
7b048c52 753 wl1271_write32(wl, WELP_ARM_COMMAND, WELP_ARM_COMMAND_VAL);
f5fc0f86
LC
754 udelay(500);
755
451de97a 756 wl1271_set_partition(wl, &part_table[PART_DRPW]);
f5fc0f86
LC
757
758 /* Read-modify-write DRPW_SCRATCH_START register (see next state)
759 to be used by DRPw FW. The RTRIM value will be added by the FW
760 before taking DRPw out of reset */
761
762 wl1271_debug(DEBUG_BOOT, "DRPW_SCRATCH_START %08x", DRPW_SCRATCH_START);
7b048c52 763 clk = wl1271_read32(wl, DRPW_SCRATCH_START);
f5fc0f86
LC
764
765 wl1271_debug(DEBUG_BOOT, "clk2 0x%x", clk);
766
5ea417ae 767 if (wl->chip.id == CHIP_ID_1283_PG20) {
d29633b4 768 clk |= ((selected_clock & 0x3) << 1) << 4;
5ea417ae
SL
769 } else {
770 clk |= (wl->ref_clock << 1) << 4;
771 }
772
0c005048
SL
773 if (wl->quirks & WL12XX_QUIRK_LPD_MODE)
774 clk |= SCRATCH_ENABLE_LPD;
775
7b048c52 776 wl1271_write32(wl, DRPW_SCRATCH_START, clk);
f5fc0f86 777
451de97a 778 wl1271_set_partition(wl, &part_table[PART_WORK]);
f5fc0f86
LC
779
780 /* Disable interrupts */
7b048c52 781 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
f5fc0f86
LC
782
783 ret = wl1271_boot_soft_reset(wl);
784 if (ret < 0)
785 goto out;
786
787 /* 2. start processing NVS file */
788 ret = wl1271_boot_upload_nvs(wl);
789 if (ret < 0)
790 goto out;
791
792 /* write firmware's last address (ie. it's length) to
793 * ACX_EEPROMLESS_IND_REG */
794 wl1271_debug(DEBUG_BOOT, "ACX_EEPROMLESS_IND_REG");
795
7b048c52 796 wl1271_write32(wl, ACX_EEPROMLESS_IND_REG, ACX_EEPROMLESS_IND_REG);
f5fc0f86 797
7b048c52 798 tmp = wl1271_read32(wl, CHIP_ID_B);
f5fc0f86
LC
799
800 wl1271_debug(DEBUG_BOOT, "chip id 0x%x", tmp);
801
802 /* 6. read the EEPROM parameters */
7b048c52 803 tmp = wl1271_read32(wl, SCR_PAD2);
f5fc0f86 804
f5fc0f86
LC
805 /* WL1271: The reference driver skips steps 7 to 10 (jumps directly
806 * to upload_fw) */
807
5ea417ae 808 if (wl->chip.id == CHIP_ID_1283_PG20)
afb7d3cd 809 wl1271_top_reg_write(wl, SDIO_IO_DS, wl->conf.hci_io_ds);
5ea417ae 810
f5fc0f86
LC
811 ret = wl1271_boot_upload_firmware(wl);
812 if (ret < 0)
813 goto out;
814
870c367c
RQ
815out:
816 return ret;
817}
818EXPORT_SYMBOL_GPL(wl1271_load_firmware);
819
820int wl1271_boot(struct wl1271 *wl)
821{
822 int ret;
823
824 /* upload NVS and firmware */
825 ret = wl1271_load_firmware(wl);
826 if (ret)
827 return ret;
828
f5fc0f86
LC
829 /* 10.5 start firmware */
830 ret = wl1271_boot_run_firmware(wl);
831 if (ret < 0)
832 goto out;
833
b9b0fdea
SL
834 ret = wl1271_boot_write_irq_polarity(wl);
835 if (ret < 0)
836 goto out;
837
838 wl1271_write32(wl, ACX_REG_INTERRUPT_MASK,
839 WL1271_ACX_ALL_EVENTS_VECTOR);
840
eb5b28d0
JO
841 /* Enable firmware interrupts now */
842 wl1271_boot_enable_interrupts(wl);
843
f5fc0f86 844 /* set the wl1271 default filters */
ae113b57 845 wl1271_set_default_filters(wl);
f5fc0f86
LC
846
847 wl1271_event_mbox_config(wl);
848
849out:
850 return ret;
851}
This page took 0.357838 seconds and 5 git commands to generate.