cris: add arch/cris/include/asm/serial.h
[linux-2.6/next.git] / drivers / net / wireless / wl12xx / sdio_test.c
blobf2891539287733c6354a5322460c4e4e159b8765
1 /*
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
22 * 02110-1301 USA
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>
37 #include <linux/pm_runtime.h>
39 #include "wl12xx.h"
40 #include "io.h"
41 #include "boot.h"
43 #ifndef SDIO_VENDOR_ID_TI
44 #define SDIO_VENDOR_ID_TI 0x0097
45 #endif
47 #ifndef SDIO_DEVICE_ID_TI_WL1271
48 #define SDIO_DEVICE_ID_TI_WL1271 0x4076
49 #endif
51 static bool rx, tx;
53 module_param(rx, bool, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(rx, "Perform rx test. Default (0). "
55 "This test continuously reads data from the SDIO device.\n");
57 module_param(tx, bool, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(tx, "Perform tx test. Default (0). "
59 "This test continuously writes data to the SDIO device.\n");
61 struct wl1271_test {
62 struct wl1271 wl;
63 struct task_struct *test_task;
66 static const struct sdio_device_id wl1271_devices[] = {
67 { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
71 static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
73 return wl->if_priv;
76 static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
78 return &(wl_to_func(wl)->dev);
81 static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
82 size_t len, bool fixed)
84 int ret = 0;
85 struct sdio_func *func = wl_to_func(wl);
87 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
88 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
89 wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
90 addr, ((u8 *)buf)[0]);
91 } else {
92 if (fixed)
93 ret = sdio_readsb(func, buf, addr, len);
94 else
95 ret = sdio_memcpy_fromio(func, buf, addr, len);
97 wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
98 addr, len);
99 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
102 if (ret)
103 wl1271_error("sdio read failed (%d)", ret);
106 static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
107 size_t len, bool fixed)
109 int ret = 0;
110 struct sdio_func *func = wl_to_func(wl);
112 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
113 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
114 wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
115 addr, ((u8 *)buf)[0]);
116 } else {
117 wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
118 addr, len);
119 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
121 if (fixed)
122 ret = sdio_writesb(func, addr, buf, len);
123 else
124 ret = sdio_memcpy_toio(func, addr, buf, len);
126 if (ret)
127 wl1271_error("sdio write failed (%d)", ret);
131 static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
133 struct sdio_func *func = wl_to_func(wl);
134 int ret;
136 /* Let the SDIO stack handle wlan_enable control, so we
137 * keep host claimed while wlan is in use to keep wl1271
138 * alive.
140 if (enable) {
141 /* Power up the card */
142 ret = pm_runtime_get_sync(&func->dev);
143 if (ret < 0)
144 goto out;
145 sdio_claim_host(func);
146 sdio_enable_func(func);
147 sdio_release_host(func);
148 } else {
149 sdio_claim_host(func);
150 sdio_disable_func(func);
151 sdio_release_host(func);
153 /* Power down the card */
154 ret = pm_runtime_put_sync(&func->dev);
157 out:
158 return ret;
161 static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
165 static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
170 static struct wl1271_if_operations sdio_ops = {
171 .read = wl1271_sdio_raw_read,
172 .write = wl1271_sdio_raw_write,
173 .power = wl1271_sdio_set_power,
174 .dev = wl1271_sdio_wl_to_dev,
175 .enable_irq = wl1271_sdio_enable_interrupts,
176 .disable_irq = wl1271_sdio_disable_interrupts,
179 static void wl1271_fw_wakeup(struct wl1271 *wl)
181 u32 elp_reg;
183 elp_reg = ELPCTRL_WAKE_UP;
184 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
187 static int wl1271_fetch_firmware(struct wl1271 *wl)
189 const struct firmware *fw;
190 int ret;
192 if (wl->chip.id == CHIP_ID_1283_PG20)
193 ret = request_firmware(&fw, WL128X_FW_NAME,
194 wl1271_wl_to_dev(wl));
195 else
196 ret = request_firmware(&fw, WL1271_FW_NAME,
197 wl1271_wl_to_dev(wl));
199 if (ret < 0) {
200 wl1271_error("could not get firmware: %d", ret);
201 return ret;
204 if (fw->size % 4) {
205 wl1271_error("firmware size is not multiple of 32 bits: %zu",
206 fw->size);
207 ret = -EILSEQ;
208 goto out;
211 wl->fw_len = fw->size;
212 wl->fw = vmalloc(wl->fw_len);
214 if (!wl->fw) {
215 wl1271_error("could not allocate memory for the firmware");
216 ret = -ENOMEM;
217 goto out;
220 memcpy(wl->fw, fw->data, wl->fw_len);
222 ret = 0;
224 out:
225 release_firmware(fw);
227 return ret;
230 static int wl1271_fetch_nvs(struct wl1271 *wl)
232 const struct firmware *fw;
233 int ret;
235 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
237 if (ret < 0) {
238 wl1271_error("could not get nvs file: %d", ret);
239 return ret;
242 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
244 if (!wl->nvs) {
245 wl1271_error("could not allocate memory for the nvs file");
246 ret = -ENOMEM;
247 goto out;
250 wl->nvs_len = fw->size;
252 out:
253 release_firmware(fw);
255 return ret;
258 static int wl1271_chip_wakeup(struct wl1271 *wl)
260 struct wl1271_partition_set partition;
261 int ret;
263 msleep(WL1271_PRE_POWER_ON_SLEEP);
264 ret = wl1271_power_on(wl);
265 if (ret)
266 return ret;
268 msleep(WL1271_POWER_ON_SLEEP);
270 /* We don't need a real memory partition here, because we only want
271 * to use the registers at this point. */
272 memset(&partition, 0, sizeof(partition));
273 partition.reg.start = REGISTERS_BASE;
274 partition.reg.size = REGISTERS_DOWN_SIZE;
275 wl1271_set_partition(wl, &partition);
277 /* ELP module wake up */
278 wl1271_fw_wakeup(wl);
280 /* whal_FwCtrl_BootSm() */
282 /* 0. read chip id from CHIP_ID */
283 wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
285 /* 1. check if chip id is valid */
287 switch (wl->chip.id) {
288 case CHIP_ID_1271_PG10:
289 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
290 wl->chip.id);
291 break;
292 case CHIP_ID_1271_PG20:
293 wl1271_notice("chip id 0x%x (1271 PG20)",
294 wl->chip.id);
295 break;
296 case CHIP_ID_1283_PG20:
297 wl1271_notice("chip id 0x%x (1283 PG20)",
298 wl->chip.id);
299 break;
300 case CHIP_ID_1283_PG10:
301 default:
302 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
303 return -ENODEV;
306 return ret;
309 static struct wl1271_partition_set part_down = {
310 .mem = {
311 .start = 0x00000000,
312 .size = 0x000177c0
314 .reg = {
315 .start = REGISTERS_BASE,
316 .size = 0x00008800
318 .mem2 = {
319 .start = 0x00000000,
320 .size = 0x00000000
322 .mem3 = {
323 .start = 0x00000000,
324 .size = 0x00000000
328 static int tester(void *data)
330 struct wl1271 *wl = data;
331 struct sdio_func *func = wl_to_func(wl);
332 struct device *pdev = &func->dev;
333 int ret = 0;
334 bool rx_started = 0;
335 bool tx_started = 0;
336 uint8_t *tx_buf, *rx_buf;
337 int test_size = PAGE_SIZE;
338 u32 addr = 0;
339 struct wl1271_partition_set partition;
341 /* We assume chip is powered up and firmware fetched */
343 memcpy(&partition, &part_down, sizeof(partition));
344 partition.mem.start = addr;
345 wl1271_set_partition(wl, &partition);
347 tx_buf = kmalloc(test_size, GFP_KERNEL);
348 rx_buf = kmalloc(test_size, GFP_KERNEL);
349 if (!tx_buf || !rx_buf) {
350 dev_err(pdev,
351 "Could not allocate memory. Test will not run.\n");
352 ret = -ENOMEM;
353 goto free;
356 memset(tx_buf, 0x5a, test_size);
358 /* write something in data area so we can read it back */
359 wl1271_write(wl, addr, tx_buf, test_size, false);
361 while (!kthread_should_stop()) {
362 if (rx && !rx_started) {
363 dev_info(pdev, "starting rx test\n");
364 rx_started = 1;
365 } else if (!rx && rx_started) {
366 dev_info(pdev, "stopping rx test\n");
367 rx_started = 0;
370 if (tx && !tx_started) {
371 dev_info(pdev, "starting tx test\n");
372 tx_started = 1;
373 } else if (!tx && tx_started) {
374 dev_info(pdev, "stopping tx test\n");
375 tx_started = 0;
378 if (rx_started)
379 wl1271_read(wl, addr, rx_buf, test_size, false);
381 if (tx_started)
382 wl1271_write(wl, addr, tx_buf, test_size, false);
384 if (!rx_started && !tx_started)
385 msleep(100);
388 free:
389 kfree(tx_buf);
390 kfree(rx_buf);
391 return ret;
394 static int __devinit wl1271_probe(struct sdio_func *func,
395 const struct sdio_device_id *id)
397 const struct wl12xx_platform_data *wlan_data;
398 struct wl1271 *wl;
399 struct wl1271_test *wl_test;
400 int ret = 0;
402 /* wl1271 has 2 sdio functions we handle just the wlan part */
403 if (func->num != 0x02)
404 return -ENODEV;
406 wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
407 if (!wl_test) {
408 dev_err(&func->dev, "Could not allocate memory\n");
409 return -ENOMEM;
412 wl = &wl_test->wl;
414 wl->if_priv = func;
415 wl->if_ops = &sdio_ops;
417 /* Grab access to FN0 for ELP reg. */
418 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
420 /* Use block mode for transferring over one block size of data */
421 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
423 wlan_data = wl12xx_get_platform_data();
424 if (IS_ERR(wlan_data)) {
425 ret = PTR_ERR(wlan_data);
426 dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
427 goto out_free;
430 wl->irq = wlan_data->irq;
431 wl->ref_clock = wlan_data->board_ref_clock;
432 wl->tcxo_clock = wlan_data->board_tcxo_clock;
434 sdio_set_drvdata(func, wl_test);
437 /* power up the device */
438 ret = wl1271_chip_wakeup(wl);
439 if (ret) {
440 dev_err(&func->dev, "could not wake up chip\n");
441 goto out_free;
444 if (wl->fw == NULL) {
445 ret = wl1271_fetch_firmware(wl);
446 if (ret < 0) {
447 dev_err(&func->dev, "firmware fetch error\n");
448 goto out_off;
452 /* fetch NVS */
453 if (wl->nvs == NULL) {
454 ret = wl1271_fetch_nvs(wl);
455 if (ret < 0) {
456 dev_err(&func->dev, "NVS fetch error\n");
457 goto out_off;
461 ret = wl1271_load_firmware(wl);
462 if (ret < 0) {
463 dev_err(&func->dev, "firmware load error: %d\n", ret);
464 goto out_free;
467 dev_info(&func->dev, "initialized\n");
469 /* I/O testing will be done in the tester thread */
471 wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
472 if (IS_ERR(wl_test->test_task)) {
473 dev_err(&func->dev, "unable to create kernel thread\n");
474 ret = PTR_ERR(wl_test->test_task);
475 goto out_free;
478 return 0;
480 out_off:
481 /* power off the chip */
482 wl1271_power_off(wl);
484 out_free:
485 kfree(wl_test);
486 return ret;
489 static void __devexit wl1271_remove(struct sdio_func *func)
491 struct wl1271_test *wl_test = sdio_get_drvdata(func);
493 /* stop the I/O test thread */
494 kthread_stop(wl_test->test_task);
496 /* power off the chip */
497 wl1271_power_off(&wl_test->wl);
499 vfree(wl_test->wl.fw);
500 wl_test->wl.fw = NULL;
501 kfree(wl_test->wl.nvs);
502 wl_test->wl.nvs = NULL;
504 kfree(wl_test);
507 static struct sdio_driver wl1271_sdio_driver = {
508 .name = "wl12xx_sdio_test",
509 .id_table = wl1271_devices,
510 .probe = wl1271_probe,
511 .remove = __devexit_p(wl1271_remove),
514 static int __init wl1271_init(void)
516 int ret;
518 ret = sdio_register_driver(&wl1271_sdio_driver);
519 if (ret < 0)
520 pr_err("failed to register sdio driver: %d\n", ret);
522 return ret;
524 module_init(wl1271_init);
526 static void __exit wl1271_exit(void)
528 sdio_unregister_driver(&wl1271_sdio_driver);
530 module_exit(wl1271_exit);
532 MODULE_LICENSE("GPL");
533 MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");