2 * wl1271_sdio_test.c - SDIO testing driver for wl1271
4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Roger Quadros <roger.quadros@nokia.com>
8 * wl1271 read/write routines taken from wl1271_sdio.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/crc7.h>
29 #include <linux/vmalloc.h>
30 #include <linux/mmc/sdio_func.h>
31 #include <linux/mmc/sdio_ids.h>
32 #include <linux/mmc/card.h>
33 #include <linux/gpio.h>
34 #include <linux/wl12xx.h>
35 #include <linux/kthread.h>
36 #include <linux/firmware.h>
39 #include "wl1271_io.h"
40 #include "wl1271_boot.h"
42 #ifndef SDIO_VENDOR_ID_TI
43 #define SDIO_VENDOR_ID_TI 0x0097
46 #ifndef SDIO_DEVICE_ID_TI_WL1271
47 #define SDIO_DEVICE_ID_TI_WL1271 0x4076
52 module_param(rx, bool, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(rx, "Perform rx test. Default (0). "
54 "This test continuously reads data from the SDIO device.\n");
56 module_param(tx, bool, S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(tx, "Perform tx test. Default (0). "
58 "This test continuously writes data to the SDIO device.\n");
62 struct task_struct *test_task;
65 static const struct sdio_device_id wl1271_devices[] = {
66 { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
70 static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
75 static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
77 return &(wl_to_func(wl)->dev);
80 static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
81 size_t len, bool fixed)
84 struct sdio_func *func = wl_to_func(wl);
86 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
87 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
88 wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
89 addr, ((u8 *)buf)[0]);
92 ret = sdio_readsb(func, buf, addr, len);
94 ret = sdio_memcpy_fromio(func, buf, addr, len);
96 wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
98 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
102 wl1271_error("sdio read failed (%d)", ret);
105 static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
106 size_t len, bool fixed)
109 struct sdio_func *func = wl_to_func(wl);
111 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
112 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
113 wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
114 addr, ((u8 *)buf)[0]);
116 wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
118 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
121 ret = sdio_writesb(func, addr, buf, len);
123 ret = sdio_memcpy_toio(func, addr, buf, len);
126 wl1271_error("sdio write failed (%d)", ret);
130 static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
132 struct sdio_func *func = wl_to_func(wl);
134 /* Let the SDIO stack handle wlan_enable control, so we
135 * keep host claimed while wlan is in use to keep wl1271
139 sdio_claim_power(func);
140 sdio_claim_host(func);
141 sdio_enable_func(func);
143 sdio_disable_func(func);
144 sdio_release_host(func);
145 sdio_release_power(func);
151 static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
155 static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
160 static struct wl1271_if_operations sdio_ops = {
161 .read = wl1271_sdio_raw_read,
162 .write = wl1271_sdio_raw_write,
163 .power = wl1271_sdio_set_power,
164 .dev = wl1271_sdio_wl_to_dev,
165 .enable_irq = wl1271_sdio_enable_interrupts,
166 .disable_irq = wl1271_sdio_disable_interrupts,
169 static void wl1271_fw_wakeup(struct wl1271 *wl)
173 elp_reg = ELPCTRL_WAKE_UP;
174 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
177 static int wl1271_fetch_firmware(struct wl1271 *wl)
179 const struct firmware *fw;
182 ret = request_firmware(&fw, WL1271_FW_NAME, wl1271_wl_to_dev(wl));
185 wl1271_error("could not get firmware: %d", ret);
190 wl1271_error("firmware size is not multiple of 32 bits: %zu",
196 wl->fw_len = fw->size;
197 wl->fw = vmalloc(wl->fw_len);
200 wl1271_error("could not allocate memory for the firmware");
205 memcpy(wl->fw, fw->data, wl->fw_len);
210 release_firmware(fw);
215 static int wl1271_fetch_nvs(struct wl1271 *wl)
217 const struct firmware *fw;
220 ret = request_firmware(&fw, WL1271_NVS_NAME, wl1271_wl_to_dev(wl));
223 wl1271_error("could not get nvs file: %d", ret);
227 wl->nvs = kmemdup(fw->data, sizeof(struct wl1271_nvs_file), GFP_KERNEL);
230 wl1271_error("could not allocate memory for the nvs file");
235 wl->nvs_len = fw->size;
238 release_firmware(fw);
243 static int wl1271_chip_wakeup(struct wl1271 *wl)
245 struct wl1271_partition_set partition;
248 msleep(WL1271_PRE_POWER_ON_SLEEP);
249 ret = wl1271_power_on(wl);
253 msleep(WL1271_POWER_ON_SLEEP);
255 /* We don't need a real memory partition here, because we only want
256 * to use the registers at this point. */
257 memset(&partition, 0, sizeof(partition));
258 partition.reg.start = REGISTERS_BASE;
259 partition.reg.size = REGISTERS_DOWN_SIZE;
260 wl1271_set_partition(wl, &partition);
262 /* ELP module wake up */
263 wl1271_fw_wakeup(wl);
265 /* whal_FwCtrl_BootSm() */
267 /* 0. read chip id from CHIP_ID */
268 wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
270 /* 1. check if chip id is valid */
272 switch (wl->chip.id) {
273 case CHIP_ID_1271_PG10:
274 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
277 case CHIP_ID_1271_PG20:
278 wl1271_notice("chip id 0x%x (1271 PG20)",
282 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
289 static struct wl1271_partition_set part_down = {
295 .start = REGISTERS_BASE,
308 static int tester(void *data)
310 struct wl1271 *wl = data;
311 struct sdio_func *func = wl_to_func(wl);
312 struct device *pdev = &func->dev;
316 uint8_t *tx_buf, *rx_buf;
317 int test_size = PAGE_SIZE;
319 struct wl1271_partition_set partition;
321 /* We assume chip is powered up and firmware fetched */
323 memcpy(&partition, &part_down, sizeof(partition));
324 partition.mem.start = addr;
325 wl1271_set_partition(wl, &partition);
327 tx_buf = kmalloc(test_size, GFP_KERNEL);
328 rx_buf = kmalloc(test_size, GFP_KERNEL);
329 if (!tx_buf || !rx_buf) {
331 "Could not allocate memory. Test will not run.\n");
336 memset(tx_buf, 0x5a, test_size);
338 /* write something in data area so we can read it back */
339 wl1271_write(wl, addr, tx_buf, test_size, false);
341 while (!kthread_should_stop()) {
342 if (rx && !rx_started) {
343 dev_info(pdev, "starting rx test\n");
345 } else if (!rx && rx_started) {
346 dev_info(pdev, "stopping rx test\n");
350 if (tx && !tx_started) {
351 dev_info(pdev, "starting tx test\n");
353 } else if (!tx && tx_started) {
354 dev_info(pdev, "stopping tx test\n");
359 wl1271_read(wl, addr, rx_buf, test_size, false);
362 wl1271_write(wl, addr, tx_buf, test_size, false);
364 if (!rx_started && !tx_started)
374 static int __devinit wl1271_probe(struct sdio_func *func,
375 const struct sdio_device_id *id)
377 const struct wl12xx_platform_data *wlan_data;
379 struct wl1271_test *wl_test;
382 /* wl1271 has 2 sdio functions we handle just the wlan part */
383 if (func->num != 0x02)
386 wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
388 dev_err(&func->dev, "Could not allocate memory\n");
395 wl->if_ops = &sdio_ops;
397 /* Grab access to FN0 for ELP reg. */
398 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
400 wlan_data = wl12xx_get_platform_data();
401 if (IS_ERR(wlan_data)) {
402 ret = PTR_ERR(wlan_data);
403 dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
407 wl->irq = wlan_data->irq;
408 wl->ref_clock = wlan_data->board_ref_clock;
410 sdio_set_drvdata(func, wl_test);
413 /* power up the device */
414 ret = wl1271_chip_wakeup(wl);
416 dev_err(&func->dev, "could not wake up chip\n");
420 if (wl->fw == NULL) {
421 ret = wl1271_fetch_firmware(wl);
423 dev_err(&func->dev, "firmware fetch error\n");
429 if (wl->nvs == NULL) {
430 ret = wl1271_fetch_nvs(wl);
432 dev_err(&func->dev, "NVS fetch error\n");
437 ret = wl1271_load_firmware(wl);
439 dev_err(&func->dev, "firmware load error: %d\n", ret);
443 dev_info(&func->dev, "initialized\n");
445 /* I/O testing will be done in the tester thread */
447 wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
448 if (IS_ERR(wl_test->test_task)) {
449 dev_err(&func->dev, "unable to create kernel thread\n");
450 ret = PTR_ERR(wl_test->test_task);
457 /* power off the chip */
458 wl1271_power_off(wl);
465 static void __devexit wl1271_remove(struct sdio_func *func)
467 struct wl1271_test *wl_test = sdio_get_drvdata(func);
469 /* stop the I/O test thread */
470 kthread_stop(wl_test->test_task);
472 /* power off the chip */
473 wl1271_power_off(&wl_test->wl);
475 vfree(wl_test->wl.fw);
476 wl_test->wl.fw = NULL;
477 kfree(wl_test->wl.nvs);
478 wl_test->wl.nvs = NULL;
483 static struct sdio_driver wl1271_sdio_driver = {
484 .name = "wl1271_sdio_test",
485 .id_table = wl1271_devices,
486 .probe = wl1271_probe,
487 .remove = __devexit_p(wl1271_remove),
490 static int __init wl1271_init(void)
494 ret = sdio_register_driver(&wl1271_sdio_driver);
496 pr_err("failed to register sdio driver: %d\n", ret);
500 module_init(wl1271_init);
502 static void __exit wl1271_exit(void)
504 sdio_unregister_driver(&wl1271_sdio_driver);
506 module_exit(wl1271_exit);
508 MODULE_LICENSE("GPL");
509 MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");