sdio_test.c 11 KB

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