wl1251_ops.c 17.7 KB
Newer Older
K
Kalle Valo 已提交
1
/*
2
 * This file is part of wl1251
K
Kalle Valo 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Copyright (C) 2008-2009 Nokia Corporation
 *
 * Contact: Kalle Valo <kalle.valo@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>

27
#include "wl1251_ops.h"
K
Kalle Valo 已提交
28
#include "reg.h"
29 30 31 32
#include "wl1251_spi.h"
#include "wl1251_boot.h"
#include "wl1251_event.h"
#include "wl1251_acx.h"
33
#include "wl1251_tx.h"
34 35 36
#include "wl1251_rx.h"
#include "wl1251_ps.h"
#include "wl1251_init.h"
K
Kalle Valo 已提交
37

38
static struct wl1251_partition_set wl1251_part_table[PART_TABLE_LEN] = {
K
Kalle Valo 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	[PART_DOWN] = {
		.mem = {
			.start = 0x00000000,
			.size  = 0x00016800
		},
		.reg = {
			.start = REGISTERS_BASE,
			.size  = REGISTERS_DOWN_SIZE
		},
	},

	[PART_WORK] = {
		.mem = {
			.start = 0x00028000,
			.size  = 0x00014000
		},
		.reg = {
			.start = REGISTERS_BASE,
			.size  = REGISTERS_WORK_SIZE
		},
	},

	/* WL1251 doesn't use the DRPW partition, so we don't set it here */
};

static enum wl12xx_acx_int_reg wl1251_acx_reg_table[ACX_REG_TABLE_LEN] = {
	[ACX_REG_INTERRUPT_TRIG]     = (REGISTERS_BASE + 0x0474),
	[ACX_REG_INTERRUPT_TRIG_H]   = (REGISTERS_BASE + 0x0478),
	[ACX_REG_INTERRUPT_MASK]     = (REGISTERS_BASE + 0x0494),
	[ACX_REG_HINT_MASK_SET]      = (REGISTERS_BASE + 0x0498),
	[ACX_REG_HINT_MASK_CLR]      = (REGISTERS_BASE + 0x049C),
	[ACX_REG_INTERRUPT_NO_CLEAR] = (REGISTERS_BASE + 0x04B0),
	[ACX_REG_INTERRUPT_CLEAR]    = (REGISTERS_BASE + 0x04A4),
	[ACX_REG_INTERRUPT_ACK]      = (REGISTERS_BASE + 0x04A8),
	[ACX_REG_SLV_SOFT_RESET]     = (REGISTERS_BASE + 0x0000),
	[ACX_REG_EE_START]           = (REGISTERS_BASE + 0x080C),
	[ACX_REG_ECPU_CONTROL]       = (REGISTERS_BASE + 0x0804)
};

78
static int wl1251_upload_firmware(struct wl1251 *wl)
K
Kalle Valo 已提交
79
{
80
	struct wl1251_partition_set *p_table = wl->chip.p_table;
K
Kalle Valo 已提交
81 82 83 84 85 86
	int addr, chunk_num, partition_limit;
	size_t fw_data_len;
	u8 *p;

	/* whal_FwCtrl_LoadFwImageSm() */

87 88
	wl1251_debug(DEBUG_BOOT, "chip id before fw upload: 0x%x",
		     wl1251_reg_read32(wl, CHIP_ID_B));
K
Kalle Valo 已提交
89 90 91 92 93

	/* 10.0 check firmware length and set partition */
	fw_data_len =  (wl->fw[4] << 24) | (wl->fw[5] << 16) |
		(wl->fw[6] << 8) | (wl->fw[7]);

94
	wl1251_debug(DEBUG_BOOT, "fw_data_len %zu chunk_size %d", fw_data_len,
K
Kalle Valo 已提交
95 96 97
		CHUNK_SIZE);

	if ((fw_data_len % 4) != 0) {
98
		wl1251_error("firmware length not multiple of four");
K
Kalle Valo 已提交
99 100 101
		return -EIO;
	}

102
	wl1251_set_partition(wl,
K
Kalle Valo 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
			     p_table[PART_DOWN].mem.start,
			     p_table[PART_DOWN].mem.size,
			     p_table[PART_DOWN].reg.start,
			     p_table[PART_DOWN].reg.size);

	/* 10.1 set partition limit and chunk num */
	chunk_num = 0;
	partition_limit = p_table[PART_DOWN].mem.size;

	while (chunk_num < fw_data_len / CHUNK_SIZE) {
		/* 10.2 update partition, if needed */
		addr = p_table[PART_DOWN].mem.start +
			(chunk_num + 2) * CHUNK_SIZE;
		if (addr > partition_limit) {
			addr = p_table[PART_DOWN].mem.start +
				chunk_num * CHUNK_SIZE;
			partition_limit = chunk_num * CHUNK_SIZE +
				p_table[PART_DOWN].mem.size;
121
			wl1251_set_partition(wl,
K
Kalle Valo 已提交
122 123 124 125 126 127 128 129 130
					     addr,
					     p_table[PART_DOWN].mem.size,
					     p_table[PART_DOWN].reg.start,
					     p_table[PART_DOWN].reg.size);
		}

		/* 10.3 upload the chunk */
		addr = p_table[PART_DOWN].mem.start + chunk_num * CHUNK_SIZE;
		p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
131
		wl1251_debug(DEBUG_BOOT, "uploading fw chunk 0x%p to 0x%x",
K
Kalle Valo 已提交
132
			     p, addr);
133
		wl1251_spi_mem_write(wl, addr, p, CHUNK_SIZE);
K
Kalle Valo 已提交
134 135 136 137 138 139 140

		chunk_num++;
	}

	/* 10.4 upload the last chunk */
	addr = p_table[PART_DOWN].mem.start + chunk_num * CHUNK_SIZE;
	p = wl->fw + FW_HDR_SIZE + chunk_num * CHUNK_SIZE;
141
	wl1251_debug(DEBUG_BOOT, "uploading fw last chunk (%zu B) 0x%p to 0x%x",
K
Kalle Valo 已提交
142
		     fw_data_len % CHUNK_SIZE, p, addr);
143
	wl1251_spi_mem_write(wl, addr, p, fw_data_len % CHUNK_SIZE);
K
Kalle Valo 已提交
144 145 146 147

	return 0;
}

148
static int wl1251_upload_nvs(struct wl1251 *wl)
K
Kalle Valo 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
	size_t nvs_len, nvs_bytes_written, burst_len;
	int nvs_start, i;
	u32 dest_addr, val;
	u8 *nvs_ptr, *nvs;

	nvs = wl->nvs;
	if (nvs == NULL)
		return -ENODEV;

	nvs_ptr = nvs;

	nvs_len = wl->nvs_len;
	nvs_start = wl->fw_len;

	/*
	 * Layout before the actual NVS tables:
	 * 1 byte : burst length.
	 * 2 bytes: destination address.
	 * n bytes: data to burst copy.
	 *
	 * This is ended by a 0 length, then the NVS tables.
	 */

	while (nvs_ptr[0]) {
		burst_len = nvs_ptr[0];
		dest_addr = (nvs_ptr[1] & 0xfe) | ((u32)(nvs_ptr[2] << 8));

		/* We move our pointer to the data */
		nvs_ptr += 3;

		for (i = 0; i < burst_len; i++) {
			val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
			       | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));

184
			wl1251_debug(DEBUG_BOOT,
K
Kalle Valo 已提交
185 186
				     "nvs burst write 0x%x: 0x%x",
				     dest_addr, val);
187
			wl1251_mem_write32(wl, dest_addr, val);
K
Kalle Valo 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

			nvs_ptr += 4;
			dest_addr += 4;
		}
	}

	/*
	 * We've reached the first zero length, the first NVS table
	 * is 7 bytes further.
	 */
	nvs_ptr += 7;
	nvs_len -= nvs_ptr - nvs;
	nvs_len = ALIGN(nvs_len, 4);

	/* Now we must set the partition correctly */
203
	wl1251_set_partition(wl, nvs_start,
K
Kalle Valo 已提交
204 205 206 207 208 209 210 211 212 213 214 215
			     wl->chip.p_table[PART_DOWN].mem.size,
			     wl->chip.p_table[PART_DOWN].reg.start,
			     wl->chip.p_table[PART_DOWN].reg.size);

	/* And finally we upload the NVS tables */
	nvs_bytes_written = 0;
	while (nvs_bytes_written < nvs_len) {
		val = (nvs_ptr[0] | (nvs_ptr[1] << 8)
		       | (nvs_ptr[2] << 16) | (nvs_ptr[3] << 24));

		val = cpu_to_le32(val);

216
		wl1251_debug(DEBUG_BOOT,
K
Kalle Valo 已提交
217 218
			     "nvs write table 0x%x: 0x%x",
			     nvs_start, val);
219
		wl1251_mem_write32(wl, nvs_start, val);
K
Kalle Valo 已提交
220 221 222 223 224 225 226 227 228

		nvs_ptr += 4;
		nvs_bytes_written += 4;
		nvs_start += 4;
	}

	return 0;
}

229
static int wl1251_boot(struct wl1251 *wl)
K
Kalle Valo 已提交
230 231 232 233
{
	int ret = 0, minor_minor_e2_ver;
	u32 tmp, boot_data;

234
	ret = wl1251_boot_soft_reset(wl);
K
Kalle Valo 已提交
235 236 237 238 239 240 241 242 243 244
	if (ret < 0)
		goto out;

	/* 2. start processing NVS file */
	ret = wl->chip.op_upload_nvs(wl);
	if (ret < 0)
		goto out;

	/* write firmware's last address (ie. it's length) to
	 * ACX_EEPROMLESS_IND_REG */
245
	wl1251_reg_write32(wl, ACX_EEPROMLESS_IND_REG, wl->fw_len);
K
Kalle Valo 已提交
246 247

	/* 6. read the EEPROM parameters */
248
	tmp = wl1251_reg_read32(wl, SCR_PAD2);
K
Kalle Valo 已提交
249 250 251 252

	/* 7. read bootdata */
	wl->boot_attr.radio_type = (tmp & 0x0000FF00) >> 8;
	wl->boot_attr.major = (tmp & 0x00FF0000) >> 16;
253
	tmp = wl1251_reg_read32(wl, SCR_PAD3);
K
Kalle Valo 已提交
254 255 256 257 258

	/* 8. check bootdata and call restart sequence */
	wl->boot_attr.minor = (tmp & 0x00FF0000) >> 16;
	minor_minor_e2_ver = (tmp & 0xFF000000) >> 24;

259
	wl1251_debug(DEBUG_BOOT, "radioType 0x%x majorE2Ver 0x%x "
K
Kalle Valo 已提交
260 261 262 263
		     "minorE2Ver 0x%x minor_minor_e2_ver 0x%x",
		     wl->boot_attr.radio_type, wl->boot_attr.major,
		     wl->boot_attr.minor, minor_minor_e2_ver);

264
	ret = wl1251_boot_init_seq(wl);
K
Kalle Valo 已提交
265 266 267 268
	if (ret < 0)
		goto out;

	/* 9. NVS processing done */
269
	boot_data = wl1251_reg_read32(wl, ACX_REG_ECPU_CONTROL);
K
Kalle Valo 已提交
270

271
	wl1251_debug(DEBUG_BOOT, "halt boot_data 0x%x", boot_data);
K
Kalle Valo 已提交
272 273 274 275 276

	/* 10. check that ECPU_CONTROL_HALT bits are set in
	 * pWhalBus->uBootData and start uploading firmware
	 */
	if ((boot_data & ECPU_CONTROL_HALT) == 0) {
277
		wl1251_error("boot failed, ECPU_CONTROL_HALT not set");
K
Kalle Valo 已提交
278 279 280 281 282 283 284 285 286
		ret = -EIO;
		goto out;
	}

	ret = wl->chip.op_upload_fw(wl);
	if (ret < 0)
		goto out;

	/* 10.5 start firmware */
287
	ret = wl1251_boot_run_firmware(wl);
K
Kalle Valo 已提交
288 289 290 291 292 293 294
	if (ret < 0)
		goto out;

out:
	return ret;
}

295
static int wl1251_mem_cfg(struct wl1251 *wl)
K
Kalle Valo 已提交
296
{
297
	struct wl1251_acx_config_memory *mem_conf;
K
Kalle Valo 已提交
298 299
	int ret, i;

300
	wl1251_debug(DEBUG_ACX, "wl1251 mem cfg");
K
Kalle Valo 已提交
301

302 303 304 305 306 307
	mem_conf = kzalloc(sizeof(*mem_conf), GFP_KERNEL);
	if (!mem_conf) {
		ret = -ENOMEM;
		goto out;
	}

K
Kalle Valo 已提交
308
	/* memory config */
309 310 311 312 313 314 315
	mem_conf->mem_config.num_stations = cpu_to_le16(DEFAULT_NUM_STATIONS);
	mem_conf->mem_config.rx_mem_block_num = 35;
	mem_conf->mem_config.tx_min_mem_block_num = 64;
	mem_conf->mem_config.num_tx_queues = MAX_TX_QUEUES;
	mem_conf->mem_config.host_if_options = HOSTIF_PKT_RING;
	mem_conf->mem_config.num_ssid_profiles = 1;
	mem_conf->mem_config.debug_buffer_size =
K
Kalle Valo 已提交
316 317 318
		cpu_to_le16(TRACE_BUFFER_MAX_SIZE);

	/* RX queue config */
319 320 321 322
	mem_conf->rx_queue_config.dma_address = 0;
	mem_conf->rx_queue_config.num_descs = ACX_RX_DESC_DEF;
	mem_conf->rx_queue_config.priority = DEFAULT_RXQ_PRIORITY;
	mem_conf->rx_queue_config.type = DEFAULT_RXQ_TYPE;
K
Kalle Valo 已提交
323 324 325

	/* TX queue config */
	for (i = 0; i < MAX_TX_QUEUES; i++) {
326 327
		mem_conf->tx_queue_config[i].num_descs = ACX_TX_DESC_DEF;
		mem_conf->tx_queue_config[i].attributes = i;
K
Kalle Valo 已提交
328 329
	}

330
	ret = wl1251_cmd_configure(wl, ACX_MEM_CFG, mem_conf,
331 332
				   sizeof(*mem_conf));
	if (ret < 0) {
333
		wl1251_warning("wl1251 mem config failed: %d", ret);
334 335
		goto out;
	}
K
Kalle Valo 已提交
336

337 338
out:
	kfree(mem_conf);
K
Kalle Valo 已提交
339 340 341
	return ret;
}

342
static int wl1251_hw_init_mem_config(struct wl1251 *wl)
K
Kalle Valo 已提交
343 344 345 346 347 348 349 350 351 352
{
	int ret;

	ret = wl1251_mem_cfg(wl);
	if (ret < 0)
		return ret;

	wl->target_mem_map = kzalloc(sizeof(struct wl1251_acx_mem_map),
					  GFP_KERNEL);
	if (!wl->target_mem_map) {
353
		wl1251_error("couldn't allocate target memory map");
K
Kalle Valo 已提交
354 355 356 357
		return -ENOMEM;
	}

	/* we now ask for the firmware built memory map */
358
	ret = wl1251_acx_mem_map(wl, wl->target_mem_map,
K
Kalle Valo 已提交
359 360
				 sizeof(struct wl1251_acx_mem_map));
	if (ret < 0) {
361
		wl1251_error("couldn't retrieve firmware memory map");
K
Kalle Valo 已提交
362 363 364 365 366 367 368 369
		kfree(wl->target_mem_map);
		wl->target_mem_map = NULL;
		return ret;
	}

	return 0;
}

370
static void wl1251_set_ecpu_ctrl(struct wl1251 *wl, u32 flag)
K
Kalle Valo 已提交
371 372 373 374
{
	u32 cpu_ctrl;

	/* 10.5.0 run the firmware (I) */
375
	cpu_ctrl = wl1251_reg_read32(wl, ACX_REG_ECPU_CONTROL);
K
Kalle Valo 已提交
376 377 378

	/* 10.5.1 run the firmware (II) */
	cpu_ctrl &= ~flag;
379
	wl1251_reg_write32(wl, ACX_REG_ECPU_CONTROL, cpu_ctrl);
K
Kalle Valo 已提交
380 381
}

382
static void wl1251_target_enable_interrupts(struct wl1251 *wl)
K
Kalle Valo 已提交
383 384 385 386 387 388 389 390
{
	/* Enable target's interrupts */
	wl->intr_mask = WL1251_ACX_INTR_RX0_DATA |
		WL1251_ACX_INTR_RX1_DATA |
		WL1251_ACX_INTR_TX_RESULT |
		WL1251_ACX_INTR_EVENT_A |
		WL1251_ACX_INTR_EVENT_B |
		WL1251_ACX_INTR_INIT_COMPLETE;
391
	wl1251_boot_target_enable_interrupts(wl);
K
Kalle Valo 已提交
392 393
}

394
static void wl1251_fw_version(struct wl1251 *wl)
395
{
396
	wl1251_acx_fw_version(wl, wl->chip.fw_ver, sizeof(wl->chip.fw_ver));
397 398
}

K
Kalle Valo 已提交
399 400 401
static void wl1251_irq_work(struct work_struct *work)
{
	u32 intr;
402 403
	struct wl1251 *wl =
		container_of(work, struct wl1251, irq_work);
K
Kalle Valo 已提交
404
	int ret;
K
Kalle Valo 已提交
405 406 407

	mutex_lock(&wl->mutex);

408
	wl1251_debug(DEBUG_IRQ, "IRQ work");
K
Kalle Valo 已提交
409

410
	if (wl->state == WL1251_STATE_OFF)
K
Kalle Valo 已提交
411 412
		goto out;

413
	ret = wl1251_ps_elp_wakeup(wl);
K
Kalle Valo 已提交
414 415
	if (ret < 0)
		goto out;
K
Kalle Valo 已提交
416

417
	wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
K
Kalle Valo 已提交
418

419 420
	intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
	wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
K
Kalle Valo 已提交
421 422

	if (wl->data_path) {
423
		wl->rx_counter =
424
			wl1251_mem_read32(wl, wl->data_path->rx_control_addr);
K
Kalle Valo 已提交
425 426 427 428

		/* We handle a frmware bug here */
		switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
		case 0:
429
			wl1251_debug(DEBUG_IRQ, "RX: FW and host in sync");
K
Kalle Valo 已提交
430 431 432 433
			intr &= ~WL1251_ACX_INTR_RX0_DATA;
			intr &= ~WL1251_ACX_INTR_RX1_DATA;
			break;
		case 1:
434
			wl1251_debug(DEBUG_IRQ, "RX: FW +1");
K
Kalle Valo 已提交
435 436 437 438
			intr |= WL1251_ACX_INTR_RX0_DATA;
			intr &= ~WL1251_ACX_INTR_RX1_DATA;
			break;
		case 2:
439
			wl1251_debug(DEBUG_IRQ, "RX: FW +2");
K
Kalle Valo 已提交
440 441 442 443
			intr |= WL1251_ACX_INTR_RX0_DATA;
			intr |= WL1251_ACX_INTR_RX1_DATA;
			break;
		default:
444
			wl1251_warning("RX: FW and host out of sync: %d",
K
Kalle Valo 已提交
445 446 447 448 449 450 451
				       wl->rx_counter - wl->rx_handled);
			break;
		}

		wl->rx_handled = wl->rx_counter;


452
		wl1251_debug(DEBUG_IRQ, "RX counter: %d", wl->rx_counter);
K
Kalle Valo 已提交
453 454 455 456 457
	}

	intr &= wl->intr_mask;

	if (intr == 0) {
458 459
		wl1251_debug(DEBUG_IRQ, "INTR is 0");
		wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
K
Kalle Valo 已提交
460 461 462 463 464 465
				   ~(wl->intr_mask));

		goto out_sleep;
	}

	if (intr & WL1251_ACX_INTR_RX0_DATA) {
466 467
		wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
		wl1251_rx(wl);
K
Kalle Valo 已提交
468 469 470
	}

	if (intr & WL1251_ACX_INTR_RX1_DATA) {
471 472
		wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
		wl1251_rx(wl);
K
Kalle Valo 已提交
473 474 475
	}

	if (intr & WL1251_ACX_INTR_TX_RESULT) {
476
		wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
477
		wl1251_tx_complete(wl);
K
Kalle Valo 已提交
478 479 480
	}

	if (intr & (WL1251_ACX_INTR_EVENT_A | WL1251_ACX_INTR_EVENT_B)) {
481
		wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)", intr);
K
Kalle Valo 已提交
482
		if (intr & WL1251_ACX_INTR_EVENT_A)
483
			wl1251_event_handle(wl, 0);
K
Kalle Valo 已提交
484
		else
485
			wl1251_event_handle(wl, 1);
K
Kalle Valo 已提交
486 487 488
	}

	if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
489
		wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_INIT_COMPLETE");
K
Kalle Valo 已提交
490

491
	wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
K
Kalle Valo 已提交
492 493

out_sleep:
494
	wl1251_ps_elp_sleep(wl);
K
Kalle Valo 已提交
495

K
Kalle Valo 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
out:
	mutex_unlock(&wl->mutex);
}

static int wl1251_hw_init_txq_fill(u8 qid,
				   struct acx_tx_queue_qos_config *config,
				   u32 num_blocks)
{
	config->qid = qid;

	switch (qid) {
	case QOS_AC_BE:
		config->high_threshold =
			(QOS_TX_HIGH_BE_DEF * num_blocks) / 100;
		config->low_threshold =
			(QOS_TX_LOW_BE_DEF * num_blocks) / 100;
		break;
	case QOS_AC_BK:
		config->high_threshold =
			(QOS_TX_HIGH_BK_DEF * num_blocks) / 100;
		config->low_threshold =
			(QOS_TX_LOW_BK_DEF * num_blocks) / 100;
		break;
	case QOS_AC_VI:
		config->high_threshold =
			(QOS_TX_HIGH_VI_DEF * num_blocks) / 100;
		config->low_threshold =
			(QOS_TX_LOW_VI_DEF * num_blocks) / 100;
		break;
	case QOS_AC_VO:
		config->high_threshold =
			(QOS_TX_HIGH_VO_DEF * num_blocks) / 100;
		config->low_threshold =
			(QOS_TX_LOW_VO_DEF * num_blocks) / 100;
		break;
	default:
532
		wl1251_error("Invalid TX queue id: %d", qid);
K
Kalle Valo 已提交
533 534 535 536 537 538
		return -EINVAL;
	}

	return 0;
}

539
static int wl1251_hw_init_tx_queue_config(struct wl1251 *wl)
K
Kalle Valo 已提交
540
{
541
	struct acx_tx_queue_qos_config *config;
K
Kalle Valo 已提交
542 543 544
	struct wl1251_acx_mem_map *wl_mem_map = wl->target_mem_map;
	int ret, i;

545
	wl1251_debug(DEBUG_ACX, "acx tx queue config");
K
Kalle Valo 已提交
546

547 548 549 550 551
	config = kzalloc(sizeof(*config), GFP_KERNEL);
	if (!config) {
		ret = -ENOMEM;
		goto out;
	}
K
Kalle Valo 已提交
552 553

	for (i = 0; i < MAX_NUM_OF_AC; i++) {
554
		ret = wl1251_hw_init_txq_fill(i, config,
K
Kalle Valo 已提交
555 556
					      wl_mem_map->num_tx_mem_blocks);
		if (ret < 0)
557
			goto out;
K
Kalle Valo 已提交
558

559
		ret = wl1251_cmd_configure(wl, ACX_TX_QUEUE_CFG,
560
					   config, sizeof(*config));
K
Kalle Valo 已提交
561
		if (ret < 0)
562
			goto out;
K
Kalle Valo 已提交
563 564
	}

565 566 567
out:
	kfree(config);
	return ret;
K
Kalle Valo 已提交
568 569
}

570
static int wl1251_hw_init_data_path_config(struct wl1251 *wl)
K
Kalle Valo 已提交
571 572 573 574 575 576 577
{
	int ret;

	/* asking for the data path parameters */
	wl->data_path = kzalloc(sizeof(struct acx_data_path_params_resp),
				GFP_KERNEL);
	if (!wl->data_path) {
578
		wl1251_error("Couldnt allocate data path parameters");
K
Kalle Valo 已提交
579 580 581
		return -ENOMEM;
	}

582
	ret = wl1251_acx_data_path_params(wl, wl->data_path);
K
Kalle Valo 已提交
583 584 585 586 587 588 589 590 591
	if (ret < 0) {
		kfree(wl->data_path);
		wl->data_path = NULL;
		return ret;
	}

	return 0;
}

592
static int wl1251_hw_init(struct wl1251 *wl)
K
Kalle Valo 已提交
593 594 595 596
{
	struct wl1251_acx_mem_map *wl_mem_map;
	int ret;

597
	ret = wl1251_hw_init_hwenc_config(wl);
K
Kalle Valo 已提交
598 599 600 601
	if (ret < 0)
		return ret;

	/* Template settings */
602
	ret = wl1251_hw_init_templates_config(wl);
K
Kalle Valo 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616
	if (ret < 0)
		return ret;

	/* Default memory configuration */
	ret = wl1251_hw_init_mem_config(wl);
	if (ret < 0)
		return ret;

	/* Default data path configuration  */
	ret = wl1251_hw_init_data_path_config(wl);
	if (ret < 0)
		goto out_free_memmap;

	/* RX config */
617
	ret = wl1251_hw_init_rx_config(wl,
K
Kalle Valo 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630
				       RX_CFG_PROMISCUOUS | RX_CFG_TSF,
				       RX_FILTER_OPTION_DEF);
	/* RX_CONFIG_OPTION_ANY_DST_ANY_BSS,
	   RX_FILTER_OPTION_FILTER_ALL); */
	if (ret < 0)
		goto out_free_data_path;

	/* TX queues config */
	ret = wl1251_hw_init_tx_queue_config(wl);
	if (ret < 0)
		goto out_free_data_path;

	/* PHY layer config */
631
	ret = wl1251_hw_init_phy_config(wl);
K
Kalle Valo 已提交
632 633 634 635
	if (ret < 0)
		goto out_free_data_path;

	/* Beacon filtering */
636
	ret = wl1251_hw_init_beacon_filter(wl);
K
Kalle Valo 已提交
637 638 639 640
	if (ret < 0)
		goto out_free_data_path;

	/* Bluetooth WLAN coexistence */
641
	ret = wl1251_hw_init_pta(wl);
K
Kalle Valo 已提交
642 643 644 645
	if (ret < 0)
		goto out_free_data_path;

	/* Energy detection */
646
	ret = wl1251_hw_init_energy_detection(wl);
K
Kalle Valo 已提交
647 648 649 650
	if (ret < 0)
		goto out_free_data_path;

	/* Beacons and boradcast settings */
651
	ret = wl1251_hw_init_beacon_broadcast(wl);
K
Kalle Valo 已提交
652 653 654 655
	if (ret < 0)
		goto out_free_data_path;

	/* Enable data path */
656
	ret = wl1251_cmd_data_path(wl, wl->channel, 1);
K
Kalle Valo 已提交
657 658 659 660
	if (ret < 0)
		goto out_free_data_path;

	/* Default power state */
661
	ret = wl1251_hw_init_power_auth(wl);
K
Kalle Valo 已提交
662 663 664 665
	if (ret < 0)
		goto out_free_data_path;

	wl_mem_map = wl->target_mem_map;
666
	wl1251_info("%d tx blocks at 0x%x, %d rx blocks at 0x%x",
K
Kalle Valo 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
		    wl_mem_map->num_tx_mem_blocks,
		    wl->data_path->tx_control_addr,
		    wl_mem_map->num_rx_mem_blocks,
		    wl->data_path->rx_control_addr);

	return 0;

 out_free_data_path:
	kfree(wl->data_path);

 out_free_memmap:
	kfree(wl->target_mem_map);

	return ret;
}

683
static int wl1251_plt_init(struct wl1251 *wl)
K
Kalle Valo 已提交
684 685 686 687 688 689 690
{
	int ret;

	ret = wl1251_hw_init_mem_config(wl);
	if (ret < 0)
		return ret;

691
	ret = wl1251_cmd_data_path(wl, wl->channel, 1);
K
Kalle Valo 已提交
692 693 694 695 696 697
	if (ret < 0)
		return ret;

	return 0;
}

698
void wl1251_setup(struct wl1251 *wl)
K
Kalle Valo 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
{
	/* FIXME: Is it better to use strncpy here or is this ok? */
	wl->chip.fw_filename = WL1251_FW_NAME;
	wl->chip.nvs_filename = WL1251_NVS_NAME;

	/* Now we know what chip we're using, so adjust the power on sleep
	 * time accordingly */
	wl->chip.power_on_sleep = WL1251_POWER_ON_SLEEP;

	wl->chip.intr_cmd_complete = WL1251_ACX_INTR_CMD_COMPLETE;
	wl->chip.intr_init_complete = WL1251_ACX_INTR_INIT_COMPLETE;

	wl->chip.op_upload_nvs = wl1251_upload_nvs;
	wl->chip.op_upload_fw = wl1251_upload_firmware;
	wl->chip.op_boot = wl1251_boot;
	wl->chip.op_set_ecpu_ctrl = wl1251_set_ecpu_ctrl;
	wl->chip.op_target_enable_interrupts = wl1251_target_enable_interrupts;
	wl->chip.op_hw_init = wl1251_hw_init;
	wl->chip.op_plt_init = wl1251_plt_init;
718
	wl->chip.op_fw_version = wl1251_fw_version;
719
	wl->chip.op_tx_flush = wl1251_tx_flush;
720
	wl->chip.op_cmd_join = wl1251_cmd_join;
K
Kalle Valo 已提交
721 722 723 724 725

	wl->chip.p_table = wl1251_part_table;
	wl->chip.acx_reg_table = wl1251_acx_reg_table;

	INIT_WORK(&wl->irq_work, wl1251_irq_work);
726 727
	INIT_WORK(&wl->tx_work, wl1251_tx_work);

K
Kalle Valo 已提交
728
}