sdio_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #include <linux/irq.h>
  26. #include <linux/module.h>
  27. #include <linux/crc7.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/mmc/sdio_func.h>
  30. #include <linux/mmc/sdio_ids.h>
  31. #include <linux/mmc/card.h>
  32. #include <linux/mmc/host.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>
  38. #include "wl12xx.h"
  39. #include "io.h"
  40. #include "boot.h"
  41. #ifndef SDIO_VENDOR_ID_TI
  42. #define SDIO_VENDOR_ID_TI 0x0097
  43. #endif
  44. #ifndef SDIO_DEVICE_ID_TI_WL1271
  45. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  46. #endif
  47. static bool rx, tx;
  48. module_param(rx, bool, S_IRUGO | S_IWUSR);
  49. MODULE_PARM_DESC(rx, "Perform rx test. Default (0). "
  50. "This test continuously reads data from the SDIO device.\n");
  51. module_param(tx, bool, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(tx, "Perform tx test. Default (0). "
  53. "This test continuously writes data to the SDIO device.\n");
  54. struct wl1271_test {
  55. struct wl1271 wl;
  56. struct task_struct *test_task;
  57. };
  58. static const struct sdio_device_id wl1271_devices[] = {
  59. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  60. {}
  61. };
  62. static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
  63. {
  64. return wl->if_priv;
  65. }
  66. static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
  67. {
  68. return &(wl_to_func(wl)->dev);
  69. }
  70. static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
  71. size_t len, bool fixed)
  72. {
  73. int ret = 0;
  74. struct sdio_func *func = wl_to_func(wl);
  75. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  76. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  77. wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
  78. addr, ((u8 *)buf)[0]);
  79. } else {
  80. if (fixed)
  81. ret = sdio_readsb(func, buf, addr, len);
  82. else
  83. ret = sdio_memcpy_fromio(func, buf, addr, len);
  84. wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
  85. addr, len);
  86. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  87. }
  88. if (ret)
  89. wl1271_error("sdio read failed (%d)", ret);
  90. }
  91. static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
  92. size_t len, bool fixed)
  93. {
  94. int ret = 0;
  95. struct sdio_func *func = wl_to_func(wl);
  96. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  97. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  98. wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
  99. addr, ((u8 *)buf)[0]);
  100. } else {
  101. wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
  102. addr, len);
  103. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  104. if (fixed)
  105. ret = sdio_writesb(func, addr, buf, len);
  106. else
  107. ret = sdio_memcpy_toio(func, addr, buf, len);
  108. }
  109. if (ret)
  110. wl1271_error("sdio write failed (%d)", ret);
  111. }
  112. static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
  113. {
  114. struct sdio_func *func = wl_to_func(wl);
  115. int ret;
  116. /* Let the SDIO stack handle wlan_enable control, so we
  117. * keep host claimed while wlan is in use to keep wl1271
  118. * alive.
  119. */
  120. if (enable) {
  121. /* Power up the card */
  122. ret = pm_runtime_get_sync(&func->dev);
  123. if (ret < 0)
  124. goto out;
  125. /* Runtime PM might be disabled, power up the card manually */
  126. ret = mmc_power_restore_host(func->card->host);
  127. if (ret < 0)
  128. goto out;
  129. sdio_claim_host(func);
  130. sdio_enable_func(func);
  131. } else {
  132. sdio_disable_func(func);
  133. sdio_release_host(func);
  134. /* Runtime PM might be disabled, power off the card manually */
  135. ret = mmc_power_save_host(func->card->host);
  136. if (ret < 0)
  137. goto out;
  138. /* Power down the card */
  139. ret = pm_runtime_put_sync(&func->dev);
  140. }
  141. out:
  142. return ret;
  143. }
  144. static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
  145. {
  146. }
  147. static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
  148. {
  149. }
  150. static struct wl1271_if_operations sdio_ops = {
  151. .read = wl1271_sdio_raw_read,
  152. .write = wl1271_sdio_raw_write,
  153. .power = wl1271_sdio_set_power,
  154. .dev = wl1271_sdio_wl_to_dev,
  155. .enable_irq = wl1271_sdio_enable_interrupts,
  156. .disable_irq = wl1271_sdio_disable_interrupts,
  157. };
  158. static void wl1271_fw_wakeup(struct wl1271 *wl)
  159. {
  160. u32 elp_reg;
  161. elp_reg = ELPCTRL_WAKE_UP;
  162. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
  163. }
  164. static int wl1271_fetch_firmware(struct wl1271 *wl)
  165. {
  166. const struct firmware *fw;
  167. int ret;
  168. if (wl->chip.id == CHIP_ID_1283_PG20)
  169. ret = request_firmware(&fw, WL128X_FW_NAME,
  170. wl1271_wl_to_dev(wl));
  171. else
  172. ret = request_firmware(&fw, WL127X_FW_NAME,
  173. wl1271_wl_to_dev(wl));
  174. if (ret < 0) {
  175. wl1271_error("could not get firmware: %d", ret);
  176. return ret;
  177. }
  178. if (fw->size % 4) {
  179. wl1271_error("firmware size is not multiple of 32 bits: %zu",
  180. fw->size);
  181. ret = -EILSEQ;
  182. goto out;
  183. }
  184. wl->fw_len = fw->size;
  185. wl->fw = vmalloc(wl->fw_len);
  186. if (!wl->fw) {
  187. wl1271_error("could not allocate memory for the firmware");
  188. ret = -ENOMEM;
  189. goto out;
  190. }
  191. memcpy(wl->fw, fw->data, wl->fw_len);
  192. ret = 0;
  193. out:
  194. release_firmware(fw);
  195. return ret;
  196. }
  197. static int wl1271_fetch_nvs(struct wl1271 *wl)
  198. {
  199. const struct firmware *fw;
  200. int ret;
  201. ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
  202. if (ret < 0) {
  203. wl1271_error("could not get nvs file: %d", ret);
  204. return ret;
  205. }
  206. wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
  207. if (!wl->nvs) {
  208. wl1271_error("could not allocate memory for the nvs file");
  209. ret = -ENOMEM;
  210. goto out;
  211. }
  212. wl->nvs_len = fw->size;
  213. out:
  214. release_firmware(fw);
  215. return ret;
  216. }
  217. static int wl1271_chip_wakeup(struct wl1271 *wl)
  218. {
  219. struct wl1271_partition_set partition;
  220. int ret;
  221. msleep(WL1271_PRE_POWER_ON_SLEEP);
  222. ret = wl1271_power_on(wl);
  223. if (ret)
  224. return ret;
  225. msleep(WL1271_POWER_ON_SLEEP);
  226. /* We don't need a real memory partition here, because we only want
  227. * to use the registers at this point. */
  228. memset(&partition, 0, sizeof(partition));
  229. partition.reg.start = REGISTERS_BASE;
  230. partition.reg.size = REGISTERS_DOWN_SIZE;
  231. wl1271_set_partition(wl, &partition);
  232. /* ELP module wake up */
  233. wl1271_fw_wakeup(wl);
  234. /* whal_FwCtrl_BootSm() */
  235. /* 0. read chip id from CHIP_ID */
  236. wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
  237. /* 1. check if chip id is valid */
  238. switch (wl->chip.id) {
  239. case CHIP_ID_1271_PG10:
  240. wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
  241. wl->chip.id);
  242. break;
  243. case CHIP_ID_1271_PG20:
  244. wl1271_notice("chip id 0x%x (1271 PG20)",
  245. wl->chip.id);
  246. break;
  247. case CHIP_ID_1283_PG20:
  248. wl1271_notice("chip id 0x%x (1283 PG20)",
  249. wl->chip.id);
  250. break;
  251. case CHIP_ID_1283_PG10:
  252. default:
  253. wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
  254. return -ENODEV;
  255. }
  256. return ret;
  257. }
  258. static struct wl1271_partition_set part_down = {
  259. .mem = {
  260. .start = 0x00000000,
  261. .size = 0x000177c0
  262. },
  263. .reg = {
  264. .start = REGISTERS_BASE,
  265. .size = 0x00008800
  266. },
  267. .mem2 = {
  268. .start = 0x00000000,
  269. .size = 0x00000000
  270. },
  271. .mem3 = {
  272. .start = 0x00000000,
  273. .size = 0x00000000
  274. },
  275. };
  276. static int tester(void *data)
  277. {
  278. struct wl1271 *wl = data;
  279. struct sdio_func *func = wl_to_func(wl);
  280. struct device *pdev = &func->dev;
  281. int ret = 0;
  282. bool rx_started = 0;
  283. bool tx_started = 0;
  284. uint8_t *tx_buf, *rx_buf;
  285. int test_size = PAGE_SIZE;
  286. u32 addr = 0;
  287. struct wl1271_partition_set partition;
  288. /* We assume chip is powered up and firmware fetched */
  289. memcpy(&partition, &part_down, sizeof(partition));
  290. partition.mem.start = addr;
  291. wl1271_set_partition(wl, &partition);
  292. tx_buf = kmalloc(test_size, GFP_KERNEL);
  293. rx_buf = kmalloc(test_size, GFP_KERNEL);
  294. if (!tx_buf || !rx_buf) {
  295. dev_err(pdev,
  296. "Could not allocate memory. Test will not run.\n");
  297. ret = -ENOMEM;
  298. goto free;
  299. }
  300. memset(tx_buf, 0x5a, test_size);
  301. /* write something in data area so we can read it back */
  302. wl1271_write(wl, addr, tx_buf, test_size, false);
  303. while (!kthread_should_stop()) {
  304. if (rx && !rx_started) {
  305. dev_info(pdev, "starting rx test\n");
  306. rx_started = 1;
  307. } else if (!rx && rx_started) {
  308. dev_info(pdev, "stopping rx test\n");
  309. rx_started = 0;
  310. }
  311. if (tx && !tx_started) {
  312. dev_info(pdev, "starting tx test\n");
  313. tx_started = 1;
  314. } else if (!tx && tx_started) {
  315. dev_info(pdev, "stopping tx test\n");
  316. tx_started = 0;
  317. }
  318. if (rx_started)
  319. wl1271_read(wl, addr, rx_buf, test_size, false);
  320. if (tx_started)
  321. wl1271_write(wl, addr, tx_buf, test_size, false);
  322. if (!rx_started && !tx_started)
  323. msleep(100);
  324. }
  325. free:
  326. kfree(tx_buf);
  327. kfree(rx_buf);
  328. return ret;
  329. }
  330. static int __devinit wl1271_probe(struct sdio_func *func,
  331. const struct sdio_device_id *id)
  332. {
  333. const struct wl12xx_platform_data *wlan_data;
  334. struct wl1271 *wl;
  335. struct wl1271_test *wl_test;
  336. int ret = 0;
  337. /* wl1271 has 2 sdio functions we handle just the wlan part */
  338. if (func->num != 0x02)
  339. return -ENODEV;
  340. wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
  341. if (!wl_test) {
  342. dev_err(&func->dev, "Could not allocate memory\n");
  343. return -ENOMEM;
  344. }
  345. wl = &wl_test->wl;
  346. wl->if_priv = func;
  347. wl->if_ops = &sdio_ops;
  348. /* Grab access to FN0 for ELP reg. */
  349. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  350. /* Use block mode for transferring over one block size of data */
  351. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  352. wlan_data = wl12xx_get_platform_data();
  353. if (IS_ERR(wlan_data)) {
  354. ret = PTR_ERR(wlan_data);
  355. dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
  356. goto out_free;
  357. }
  358. wl->irq = wlan_data->irq;
  359. wl->ref_clock = wlan_data->board_ref_clock;
  360. wl->tcxo_clock = wlan_data->board_tcxo_clock;
  361. sdio_set_drvdata(func, wl_test);
  362. /* power up the device */
  363. ret = wl1271_chip_wakeup(wl);
  364. if (ret) {
  365. dev_err(&func->dev, "could not wake up chip\n");
  366. goto out_free;
  367. }
  368. if (wl->fw == NULL) {
  369. ret = wl1271_fetch_firmware(wl);
  370. if (ret < 0) {
  371. dev_err(&func->dev, "firmware fetch error\n");
  372. goto out_off;
  373. }
  374. }
  375. /* fetch NVS */
  376. if (wl->nvs == NULL) {
  377. ret = wl1271_fetch_nvs(wl);
  378. if (ret < 0) {
  379. dev_err(&func->dev, "NVS fetch error\n");
  380. goto out_off;
  381. }
  382. }
  383. ret = wl1271_load_firmware(wl);
  384. if (ret < 0) {
  385. dev_err(&func->dev, "firmware load error: %d\n", ret);
  386. goto out_free;
  387. }
  388. dev_info(&func->dev, "initialized\n");
  389. /* I/O testing will be done in the tester thread */
  390. wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
  391. if (IS_ERR(wl_test->test_task)) {
  392. dev_err(&func->dev, "unable to create kernel thread\n");
  393. ret = PTR_ERR(wl_test->test_task);
  394. goto out_free;
  395. }
  396. return 0;
  397. out_off:
  398. /* power off the chip */
  399. wl1271_power_off(wl);
  400. out_free:
  401. kfree(wl_test);
  402. return ret;
  403. }
  404. static void __devexit wl1271_remove(struct sdio_func *func)
  405. {
  406. struct wl1271_test *wl_test = sdio_get_drvdata(func);
  407. /* stop the I/O test thread */
  408. kthread_stop(wl_test->test_task);
  409. /* power off the chip */
  410. wl1271_power_off(&wl_test->wl);
  411. vfree(wl_test->wl.fw);
  412. wl_test->wl.fw = NULL;
  413. kfree(wl_test->wl.nvs);
  414. wl_test->wl.nvs = NULL;
  415. kfree(wl_test);
  416. }
  417. static struct sdio_driver wl1271_sdio_driver = {
  418. .name = "wl12xx_sdio_test",
  419. .id_table = wl1271_devices,
  420. .probe = wl1271_probe,
  421. .remove = __devexit_p(wl1271_remove),
  422. };
  423. static int __init wl1271_init(void)
  424. {
  425. int ret;
  426. ret = sdio_register_driver(&wl1271_sdio_driver);
  427. if (ret < 0)
  428. pr_err("failed to register sdio driver: %d\n", ret);
  429. return ret;
  430. }
  431. module_init(wl1271_init);
  432. static void __exit wl1271_exit(void)
  433. {
  434. sdio_unregister_driver(&wl1271_sdio_driver);
  435. }
  436. module_exit(wl1271_exit);
  437. MODULE_LICENSE("GPL");
  438. MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");