2 * SDIO testing driver for wl12xx
4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Roger Quadros <roger.quadros@nokia.com>
8 * wl12xx read/write routines taken from the main module
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/mmc/host.h>
34 #include <linux/gpio.h>
35 #include <linux/wl12xx.h>
36 #include <linux/kthread.h>
37 #include <linux/firmware.h>
38 #include <linux/pm_runtime.h>
44 #ifndef SDIO_VENDOR_ID_TI
45 #define SDIO_VENDOR_ID_TI 0x0097
48 #ifndef SDIO_DEVICE_ID_TI_WL1271
49 #define SDIO_DEVICE_ID_TI_WL1271 0x4076
54 module_param(rx, bool, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(rx, "Perform rx test. Default (0). "
56 "This test continuously reads data from the SDIO device.\n");
58 module_param(tx, bool, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(tx, "Perform tx test. Default (0). "
60 "This test continuously writes data to the SDIO device.\n");
64 struct task_struct *test_task;
67 static const struct sdio_device_id wl1271_devices[] = {
68 { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
72 static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
77 static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
79 return &(wl_to_func(wl)->dev);
82 static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
83 size_t len, bool fixed)
86 struct sdio_func *func = wl_to_func(wl);
88 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
89 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
90 wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
91 addr, ((u8 *)buf)[0]);
94 ret = sdio_readsb(func, buf, addr, len);
96 ret = sdio_memcpy_fromio(func, buf, addr, len);
98 wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
100 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
104 wl1271_error("sdio read failed (%d)", ret);
107 static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
108 size_t len, bool fixed)
111 struct sdio_func *func = wl_to_func(wl);
113 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
114 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
115 wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
116 addr, ((u8 *)buf)[0]);
118 wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
120 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
123 ret = sdio_writesb(func, addr, buf, len);
125 ret = sdio_memcpy_toio(func, addr, buf, len);
128 wl1271_error("sdio write failed (%d)", ret);
132 static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
134 struct sdio_func *func = wl_to_func(wl);
137 /* Let the SDIO stack handle wlan_enable control, so we
138 * keep host claimed while wlan is in use to keep wl1271
142 /* Power up the card */
143 ret = pm_runtime_get_sync(&func->dev);
147 /* Runtime PM might be disabled, power up the card manually */
148 ret = mmc_power_restore_host(func->card->host);
152 sdio_claim_host(func);
153 sdio_enable_func(func);
155 sdio_disable_func(func);
156 sdio_release_host(func);
158 /* Runtime PM might be disabled, power off the card manually */
159 ret = mmc_power_save_host(func->card->host);
163 /* Power down the card */
164 ret = pm_runtime_put_sync(&func->dev);
171 static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
175 static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
180 static struct wl1271_if_operations sdio_ops = {
181 .read = wl1271_sdio_raw_read,
182 .write = wl1271_sdio_raw_write,
183 .power = wl1271_sdio_set_power,
184 .dev = wl1271_sdio_wl_to_dev,
185 .enable_irq = wl1271_sdio_enable_interrupts,
186 .disable_irq = wl1271_sdio_disable_interrupts,
189 static void wl1271_fw_wakeup(struct wl1271 *wl)
193 elp_reg = ELPCTRL_WAKE_UP;
194 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
197 static int wl1271_fetch_firmware(struct wl1271 *wl)
199 const struct firmware *fw;
202 if (wl->chip.id == CHIP_ID_1283_PG20)
203 ret = request_firmware(&fw, WL128X_FW_NAME,
204 wl1271_wl_to_dev(wl));
206 ret = request_firmware(&fw, WL127X_FW_NAME,
207 wl1271_wl_to_dev(wl));
210 wl1271_error("could not get firmware: %d", ret);
215 wl1271_error("firmware size is not multiple of 32 bits: %zu",
221 wl->fw_len = fw->size;
222 wl->fw = vmalloc(wl->fw_len);
225 wl1271_error("could not allocate memory for the firmware");
230 memcpy(wl->fw, fw->data, wl->fw_len);
235 release_firmware(fw);
240 static int wl1271_fetch_nvs(struct wl1271 *wl)
242 const struct firmware *fw;
245 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
248 wl1271_error("could not get nvs file: %d", ret);
252 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
255 wl1271_error("could not allocate memory for the nvs file");
260 wl->nvs_len = fw->size;
263 release_firmware(fw);
268 static int wl1271_chip_wakeup(struct wl1271 *wl)
270 struct wl1271_partition_set partition;
273 msleep(WL1271_PRE_POWER_ON_SLEEP);
274 ret = wl1271_power_on(wl);
278 msleep(WL1271_POWER_ON_SLEEP);
280 /* We don't need a real memory partition here, because we only want
281 * to use the registers at this point. */
282 memset(&partition, 0, sizeof(partition));
283 partition.reg.start = REGISTERS_BASE;
284 partition.reg.size = REGISTERS_DOWN_SIZE;
285 wl1271_set_partition(wl, &partition);
287 /* ELP module wake up */
288 wl1271_fw_wakeup(wl);
290 /* whal_FwCtrl_BootSm() */
292 /* 0. read chip id from CHIP_ID */
293 wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
295 /* 1. check if chip id is valid */
297 switch (wl->chip.id) {
298 case CHIP_ID_1271_PG10:
299 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
302 case CHIP_ID_1271_PG20:
303 wl1271_notice("chip id 0x%x (1271 PG20)",
306 case CHIP_ID_1283_PG20:
307 wl1271_notice("chip id 0x%x (1283 PG20)",
310 case CHIP_ID_1283_PG10:
312 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
319 static struct wl1271_partition_set part_down = {
325 .start = REGISTERS_BASE,
338 static int tester(void *data)
340 struct wl1271 *wl = data;
341 struct sdio_func *func = wl_to_func(wl);
342 struct device *pdev = &func->dev;
346 uint8_t *tx_buf, *rx_buf;
347 int test_size = PAGE_SIZE;
349 struct wl1271_partition_set partition;
351 /* We assume chip is powered up and firmware fetched */
353 memcpy(&partition, &part_down, sizeof(partition));
354 partition.mem.start = addr;
355 wl1271_set_partition(wl, &partition);
357 tx_buf = kmalloc(test_size, GFP_KERNEL);
358 rx_buf = kmalloc(test_size, GFP_KERNEL);
359 if (!tx_buf || !rx_buf) {
361 "Could not allocate memory. Test will not run.\n");
366 memset(tx_buf, 0x5a, test_size);
368 /* write something in data area so we can read it back */
369 wl1271_write(wl, addr, tx_buf, test_size, false);
371 while (!kthread_should_stop()) {
372 if (rx && !rx_started) {
373 dev_info(pdev, "starting rx test\n");
375 } else if (!rx && rx_started) {
376 dev_info(pdev, "stopping rx test\n");
380 if (tx && !tx_started) {
381 dev_info(pdev, "starting tx test\n");
383 } else if (!tx && tx_started) {
384 dev_info(pdev, "stopping tx test\n");
389 wl1271_read(wl, addr, rx_buf, test_size, false);
392 wl1271_write(wl, addr, tx_buf, test_size, false);
394 if (!rx_started && !tx_started)
404 static int __devinit wl1271_probe(struct sdio_func *func,
405 const struct sdio_device_id *id)
407 const struct wl12xx_platform_data *wlan_data;
409 struct wl1271_test *wl_test;
412 /* wl1271 has 2 sdio functions we handle just the wlan part */
413 if (func->num != 0x02)
416 wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
418 dev_err(&func->dev, "Could not allocate memory\n");
425 wl->if_ops = &sdio_ops;
427 /* Grab access to FN0 for ELP reg. */
428 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
430 /* Use block mode for transferring over one block size of data */
431 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
433 wlan_data = wl12xx_get_platform_data();
434 if (IS_ERR(wlan_data)) {
435 ret = PTR_ERR(wlan_data);
436 dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
440 wl->irq = wlan_data->irq;
441 wl->ref_clock = wlan_data->board_ref_clock;
442 wl->tcxo_clock = wlan_data->board_tcxo_clock;
444 sdio_set_drvdata(func, wl_test);
446 /* power up the device */
447 ret = wl1271_chip_wakeup(wl);
449 dev_err(&func->dev, "could not wake up chip\n");
453 if (wl->fw == NULL) {
454 ret = wl1271_fetch_firmware(wl);
456 dev_err(&func->dev, "firmware fetch error\n");
462 if (wl->nvs == NULL) {
463 ret = wl1271_fetch_nvs(wl);
465 dev_err(&func->dev, "NVS fetch error\n");
470 ret = wl1271_load_firmware(wl);
472 dev_err(&func->dev, "firmware load error: %d\n", ret);
476 dev_info(&func->dev, "initialized\n");
478 /* I/O testing will be done in the tester thread */
480 wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
481 if (IS_ERR(wl_test->test_task)) {
482 dev_err(&func->dev, "unable to create kernel thread\n");
483 ret = PTR_ERR(wl_test->test_task);
490 /* power off the chip */
491 wl1271_power_off(wl);
498 static void __devexit wl1271_remove(struct sdio_func *func)
500 struct wl1271_test *wl_test = sdio_get_drvdata(func);
502 /* stop the I/O test thread */
503 kthread_stop(wl_test->test_task);
505 /* power off the chip */
506 wl1271_power_off(&wl_test->wl);
508 vfree(wl_test->wl.fw);
509 wl_test->wl.fw = NULL;
510 kfree(wl_test->wl.nvs);
511 wl_test->wl.nvs = NULL;
516 static struct sdio_driver wl1271_sdio_driver = {
517 .name = "wl12xx_sdio_test",
518 .id_table = wl1271_devices,
519 .probe = wl1271_probe,
520 .remove = __devexit_p(wl1271_remove),
523 static int __init wl1271_init(void)
527 ret = sdio_register_driver(&wl1271_sdio_driver);
529 pr_err("failed to register sdio driver: %d\n", ret);
533 module_init(wl1271_init);
535 static void __exit wl1271_exit(void)
537 sdio_unregister_driver(&wl1271_sdio_driver);
539 module_exit(wl1271_exit);
541 MODULE_LICENSE("GPL");
542 MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");