]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/wl12xx/sdio_test.c
iwlagn: fix a race in the unmapping of the TFDs
[~andy/linux] / drivers / net / wireless / wl12xx / sdio_test.c
1 /*
2  * SDIO testing driver for wl12xx
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  *
6  * Contact: Roger Quadros <roger.quadros@nokia.com>
7  *
8  * wl12xx read/write routines taken from the main module
9  *
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.
13  *
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.
18  *
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
23  *
24  */
25
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>
39
40 #include "wl12xx.h"
41 #include "io.h"
42 #include "boot.h"
43
44 #ifndef SDIO_VENDOR_ID_TI
45 #define SDIO_VENDOR_ID_TI               0x0097
46 #endif
47
48 #ifndef SDIO_DEVICE_ID_TI_WL1271
49 #define SDIO_DEVICE_ID_TI_WL1271        0x4076
50 #endif
51
52 static bool rx, tx;
53
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");
57
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");
61
62 struct wl1271_test {
63         struct wl1271 wl;
64         struct task_struct *test_task;
65 };
66
67 static const struct sdio_device_id wl1271_devices[] = {
68         { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
69         {}
70 };
71
72 static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
73 {
74         return wl->if_priv;
75 }
76
77 static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
78 {
79         return &(wl_to_func(wl)->dev);
80 }
81
82 static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
83                 size_t len, bool fixed)
84 {
85         int ret = 0;
86         struct sdio_func *func = wl_to_func(wl);
87
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]);
92         } else {
93                 if (fixed)
94                         ret = sdio_readsb(func, buf, addr, len);
95                 else
96                         ret = sdio_memcpy_fromio(func, buf, addr, len);
97
98                 wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
99                                 addr, len);
100                 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
101         }
102
103         if (ret)
104                 wl1271_error("sdio read failed (%d)", ret);
105 }
106
107 static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
108                 size_t len, bool fixed)
109 {
110         int ret = 0;
111         struct sdio_func *func = wl_to_func(wl);
112
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]);
117         } else {
118                 wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
119                                 addr, len);
120                 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
121
122                 if (fixed)
123                         ret = sdio_writesb(func, addr, buf, len);
124                 else
125                         ret = sdio_memcpy_toio(func, addr, buf, len);
126         }
127         if (ret)
128                 wl1271_error("sdio write failed (%d)", ret);
129
130 }
131
132 static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
133 {
134         struct sdio_func *func = wl_to_func(wl);
135         int ret;
136
137         /* Let the SDIO stack handle wlan_enable control, so we
138          * keep host claimed while wlan is in use to keep wl1271
139          * alive.
140          */
141         if (enable) {
142                 /* Power up the card */
143                 ret = pm_runtime_get_sync(&func->dev);
144                 if (ret < 0)
145                         goto out;
146
147                 /* Runtime PM might be disabled, power up the card manually */
148                 ret = mmc_power_restore_host(func->card->host);
149                 if (ret < 0)
150                         goto out;
151
152                 sdio_claim_host(func);
153                 sdio_enable_func(func);
154         } else {
155                 sdio_disable_func(func);
156                 sdio_release_host(func);
157
158                 /* Runtime PM might be disabled, power off the card manually */
159                 ret = mmc_power_save_host(func->card->host);
160                 if (ret < 0)
161                         goto out;
162
163                 /* Power down the card */
164                 ret = pm_runtime_put_sync(&func->dev);
165         }
166
167 out:
168         return ret;
169 }
170
171 static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
172 {
173 }
174
175 static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
176 {
177 }
178
179
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,
187 };
188
189 static void wl1271_fw_wakeup(struct wl1271 *wl)
190 {
191         u32 elp_reg;
192
193         elp_reg = ELPCTRL_WAKE_UP;
194         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
195 }
196
197 static int wl1271_fetch_firmware(struct wl1271 *wl)
198 {
199         const struct firmware *fw;
200         int ret;
201
202         if (wl->chip.id == CHIP_ID_1283_PG20)
203                 ret = request_firmware(&fw, WL128X_FW_NAME,
204                                        wl1271_wl_to_dev(wl));
205         else
206                 ret = request_firmware(&fw, WL127X_FW_NAME,
207                                        wl1271_wl_to_dev(wl));
208
209         if (ret < 0) {
210                 wl1271_error("could not get firmware: %d", ret);
211                 return ret;
212         }
213
214         if (fw->size % 4) {
215                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
216                                 fw->size);
217                 ret = -EILSEQ;
218                 goto out;
219         }
220
221         wl->fw_len = fw->size;
222         wl->fw = vmalloc(wl->fw_len);
223
224         if (!wl->fw) {
225                 wl1271_error("could not allocate memory for the firmware");
226                 ret = -ENOMEM;
227                 goto out;
228         }
229
230         memcpy(wl->fw, fw->data, wl->fw_len);
231
232         ret = 0;
233
234 out:
235         release_firmware(fw);
236
237         return ret;
238 }
239
240 static int wl1271_fetch_nvs(struct wl1271 *wl)
241 {
242         const struct firmware *fw;
243         int ret;
244
245         ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
246
247         if (ret < 0) {
248                 wl1271_error("could not get nvs file: %d", ret);
249                 return ret;
250         }
251
252         wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
253
254         if (!wl->nvs) {
255                 wl1271_error("could not allocate memory for the nvs file");
256                 ret = -ENOMEM;
257                 goto out;
258         }
259
260         wl->nvs_len = fw->size;
261
262 out:
263         release_firmware(fw);
264
265         return ret;
266 }
267
268 static int wl1271_chip_wakeup(struct wl1271 *wl)
269 {
270         struct wl1271_partition_set partition;
271         int ret;
272
273         msleep(WL1271_PRE_POWER_ON_SLEEP);
274         ret = wl1271_power_on(wl);
275         if (ret)
276                 return ret;
277
278         msleep(WL1271_POWER_ON_SLEEP);
279
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);
286
287         /* ELP module wake up */
288         wl1271_fw_wakeup(wl);
289
290         /* whal_FwCtrl_BootSm() */
291
292         /* 0. read chip id from CHIP_ID */
293         wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
294
295         /* 1. check if chip id is valid */
296
297         switch (wl->chip.id) {
298         case CHIP_ID_1271_PG10:
299                 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
300                                 wl->chip.id);
301                 break;
302         case CHIP_ID_1271_PG20:
303                 wl1271_notice("chip id 0x%x (1271 PG20)",
304                                 wl->chip.id);
305                 break;
306         case CHIP_ID_1283_PG20:
307                 wl1271_notice("chip id 0x%x (1283 PG20)",
308                                 wl->chip.id);
309                 break;
310         case CHIP_ID_1283_PG10:
311         default:
312                 wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
313                 return -ENODEV;
314         }
315
316         return ret;
317 }
318
319 static struct wl1271_partition_set part_down = {
320         .mem = {
321                 .start = 0x00000000,
322                 .size  = 0x000177c0
323         },
324         .reg = {
325                 .start = REGISTERS_BASE,
326                 .size  = 0x00008800
327         },
328         .mem2 = {
329                 .start = 0x00000000,
330                 .size  = 0x00000000
331         },
332         .mem3 = {
333                 .start = 0x00000000,
334                 .size  = 0x00000000
335         },
336 };
337
338 static int tester(void *data)
339 {
340         struct wl1271 *wl = data;
341         struct sdio_func *func = wl_to_func(wl);
342         struct device *pdev = &func->dev;
343         int ret = 0;
344         bool rx_started = 0;
345         bool tx_started = 0;
346         uint8_t *tx_buf, *rx_buf;
347         int test_size = PAGE_SIZE;
348         u32 addr = 0;
349         struct wl1271_partition_set partition;
350
351         /* We assume chip is powered up and firmware fetched */
352
353         memcpy(&partition, &part_down, sizeof(partition));
354         partition.mem.start = addr;
355         wl1271_set_partition(wl, &partition);
356
357         tx_buf = kmalloc(test_size, GFP_KERNEL);
358         rx_buf = kmalloc(test_size, GFP_KERNEL);
359         if (!tx_buf || !rx_buf) {
360                 dev_err(pdev,
361                         "Could not allocate memory. Test will not run.\n");
362                 ret = -ENOMEM;
363                 goto free;
364         }
365
366         memset(tx_buf, 0x5a, test_size);
367
368         /* write something in data area so we can read it back */
369         wl1271_write(wl, addr, tx_buf, test_size, false);
370
371         while (!kthread_should_stop()) {
372                 if (rx && !rx_started) {
373                         dev_info(pdev, "starting rx test\n");
374                         rx_started = 1;
375                 } else if (!rx && rx_started) {
376                         dev_info(pdev, "stopping rx test\n");
377                         rx_started = 0;
378                 }
379
380                 if (tx && !tx_started) {
381                         dev_info(pdev, "starting tx test\n");
382                         tx_started = 1;
383                 } else if (!tx && tx_started) {
384                         dev_info(pdev, "stopping tx test\n");
385                         tx_started = 0;
386                 }
387
388                 if (rx_started)
389                         wl1271_read(wl, addr, rx_buf, test_size, false);
390
391                 if (tx_started)
392                         wl1271_write(wl, addr, tx_buf, test_size, false);
393
394                 if (!rx_started && !tx_started)
395                         msleep(100);
396         }
397
398 free:
399         kfree(tx_buf);
400         kfree(rx_buf);
401         return ret;
402 }
403
404 static int __devinit wl1271_probe(struct sdio_func *func,
405                 const struct sdio_device_id *id)
406 {
407         const struct wl12xx_platform_data *wlan_data;
408         struct wl1271 *wl;
409         struct wl1271_test *wl_test;
410         int ret = 0;
411
412         /* wl1271 has 2 sdio functions we handle just the wlan part */
413         if (func->num != 0x02)
414                 return -ENODEV;
415
416         wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
417         if (!wl_test) {
418                 dev_err(&func->dev, "Could not allocate memory\n");
419                 return -ENOMEM;
420         }
421
422         wl = &wl_test->wl;
423
424         wl->if_priv = func;
425         wl->if_ops = &sdio_ops;
426
427         /* Grab access to FN0 for ELP reg. */
428         func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
429
430         /* Use block mode for transferring over one block size of data */
431         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
432
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);
437                 goto out_free;
438         }
439
440         wl->irq = wlan_data->irq;
441         wl->ref_clock = wlan_data->board_ref_clock;
442         wl->tcxo_clock = wlan_data->board_tcxo_clock;
443
444         sdio_set_drvdata(func, wl_test);
445
446         /* power up the device */
447         ret = wl1271_chip_wakeup(wl);
448         if (ret) {
449                 dev_err(&func->dev, "could not wake up chip\n");
450                 goto out_free;
451         }
452
453         if (wl->fw == NULL) {
454                 ret = wl1271_fetch_firmware(wl);
455                 if (ret < 0) {
456                         dev_err(&func->dev, "firmware fetch error\n");
457                         goto out_off;
458                 }
459         }
460
461         /* fetch NVS */
462         if (wl->nvs == NULL) {
463                 ret = wl1271_fetch_nvs(wl);
464                 if (ret < 0) {
465                         dev_err(&func->dev, "NVS fetch error\n");
466                         goto out_off;
467                 }
468         }
469
470         ret = wl1271_load_firmware(wl);
471         if (ret < 0) {
472                 dev_err(&func->dev, "firmware load error: %d\n", ret);
473                 goto out_free;
474         }
475
476         dev_info(&func->dev, "initialized\n");
477
478         /* I/O testing will be done in the tester thread */
479
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);
484                 goto out_free;
485         }
486
487         return 0;
488
489 out_off:
490         /* power off the chip */
491         wl1271_power_off(wl);
492
493 out_free:
494         kfree(wl_test);
495         return ret;
496 }
497
498 static void __devexit wl1271_remove(struct sdio_func *func)
499 {
500         struct wl1271_test *wl_test = sdio_get_drvdata(func);
501
502         /* stop the I/O test thread */
503         kthread_stop(wl_test->test_task);
504
505         /* power off the chip */
506         wl1271_power_off(&wl_test->wl);
507
508         vfree(wl_test->wl.fw);
509         wl_test->wl.fw = NULL;
510         kfree(wl_test->wl.nvs);
511         wl_test->wl.nvs = NULL;
512
513         kfree(wl_test);
514 }
515
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),
521 };
522
523 static int __init wl1271_init(void)
524 {
525         int ret;
526
527         ret = sdio_register_driver(&wl1271_sdio_driver);
528         if (ret < 0)
529                 pr_err("failed to register sdio driver: %d\n", ret);
530
531         return ret;
532 }
533 module_init(wl1271_init);
534
535 static void __exit wl1271_exit(void)
536 {
537         sdio_unregister_driver(&wl1271_sdio_driver);
538 }
539 module_exit(wl1271_exit);
540
541 MODULE_LICENSE("GPL");
542 MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");
543