sdio_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. if (wl->chip.id == CHIP_ID_1283_PG20)
  162. ret = request_firmware(&fw, WL128X_FW_NAME,
  163. wl1271_wl_to_dev(wl));
  164. else
  165. ret = request_firmware(&fw, WL1271_FW_NAME,
  166. wl1271_wl_to_dev(wl));
  167. if (ret < 0) {
  168. wl1271_error("could not get firmware: %d", ret);
  169. return ret;
  170. }
  171. if (fw->size % 4) {
  172. wl1271_error("firmware size is not multiple of 32 bits: %zu",
  173. fw->size);
  174. ret = -EILSEQ;
  175. goto out;
  176. }
  177. wl->fw_len = fw->size;
  178. wl->fw = vmalloc(wl->fw_len);
  179. if (!wl->fw) {
  180. wl1271_error("could not allocate memory for the firmware");
  181. ret = -ENOMEM;
  182. goto out;
  183. }
  184. memcpy(wl->fw, fw->data, wl->fw_len);
  185. ret = 0;
  186. out:
  187. release_firmware(fw);
  188. return ret;
  189. }
  190. static int wl1271_fetch_nvs(struct wl1271 *wl)
  191. {
  192. const struct firmware *fw;
  193. int ret;
  194. ret = request_firmware(&fw, WL12XX_NVS_NAME, wl1271_wl_to_dev(wl));
  195. if (ret < 0) {
  196. wl1271_error("could not get nvs file: %d", ret);
  197. return ret;
  198. }
  199. wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
  200. if (!wl->nvs) {
  201. wl1271_error("could not allocate memory for the nvs file");
  202. ret = -ENOMEM;
  203. goto out;
  204. }
  205. wl->nvs_len = fw->size;
  206. out:
  207. release_firmware(fw);
  208. return ret;
  209. }
  210. static int wl1271_chip_wakeup(struct wl1271 *wl)
  211. {
  212. struct wl1271_partition_set partition;
  213. int ret;
  214. msleep(WL1271_PRE_POWER_ON_SLEEP);
  215. ret = wl1271_power_on(wl);
  216. if (ret)
  217. return ret;
  218. msleep(WL1271_POWER_ON_SLEEP);
  219. /* We don't need a real memory partition here, because we only want
  220. * to use the registers at this point. */
  221. memset(&partition, 0, sizeof(partition));
  222. partition.reg.start = REGISTERS_BASE;
  223. partition.reg.size = REGISTERS_DOWN_SIZE;
  224. wl1271_set_partition(wl, &partition);
  225. /* ELP module wake up */
  226. wl1271_fw_wakeup(wl);
  227. /* whal_FwCtrl_BootSm() */
  228. /* 0. read chip id from CHIP_ID */
  229. wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
  230. /* 1. check if chip id is valid */
  231. switch (wl->chip.id) {
  232. case CHIP_ID_1271_PG10:
  233. wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
  234. wl->chip.id);
  235. break;
  236. case CHIP_ID_1271_PG20:
  237. wl1271_notice("chip id 0x%x (1271 PG20)",
  238. wl->chip.id);
  239. break;
  240. case CHIP_ID_1283_PG20:
  241. wl1271_notice("chip id 0x%x (1283 PG20)",
  242. wl->chip.id);
  243. break;
  244. case CHIP_ID_1283_PG10:
  245. default:
  246. wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
  247. return -ENODEV;
  248. }
  249. return ret;
  250. }
  251. static struct wl1271_partition_set part_down = {
  252. .mem = {
  253. .start = 0x00000000,
  254. .size = 0x000177c0
  255. },
  256. .reg = {
  257. .start = REGISTERS_BASE,
  258. .size = 0x00008800
  259. },
  260. .mem2 = {
  261. .start = 0x00000000,
  262. .size = 0x00000000
  263. },
  264. .mem3 = {
  265. .start = 0x00000000,
  266. .size = 0x00000000
  267. },
  268. };
  269. static int tester(void *data)
  270. {
  271. struct wl1271 *wl = data;
  272. struct sdio_func *func = wl_to_func(wl);
  273. struct device *pdev = &func->dev;
  274. int ret = 0;
  275. bool rx_started = 0;
  276. bool tx_started = 0;
  277. uint8_t *tx_buf, *rx_buf;
  278. int test_size = PAGE_SIZE;
  279. u32 addr = 0;
  280. struct wl1271_partition_set partition;
  281. /* We assume chip is powered up and firmware fetched */
  282. memcpy(&partition, &part_down, sizeof(partition));
  283. partition.mem.start = addr;
  284. wl1271_set_partition(wl, &partition);
  285. tx_buf = kmalloc(test_size, GFP_KERNEL);
  286. rx_buf = kmalloc(test_size, GFP_KERNEL);
  287. if (!tx_buf || !rx_buf) {
  288. dev_err(pdev,
  289. "Could not allocate memory. Test will not run.\n");
  290. ret = -ENOMEM;
  291. goto free;
  292. }
  293. memset(tx_buf, 0x5a, test_size);
  294. /* write something in data area so we can read it back */
  295. wl1271_write(wl, addr, tx_buf, test_size, false);
  296. while (!kthread_should_stop()) {
  297. if (rx && !rx_started) {
  298. dev_info(pdev, "starting rx test\n");
  299. rx_started = 1;
  300. } else if (!rx && rx_started) {
  301. dev_info(pdev, "stopping rx test\n");
  302. rx_started = 0;
  303. }
  304. if (tx && !tx_started) {
  305. dev_info(pdev, "starting tx test\n");
  306. tx_started = 1;
  307. } else if (!tx && tx_started) {
  308. dev_info(pdev, "stopping tx test\n");
  309. tx_started = 0;
  310. }
  311. if (rx_started)
  312. wl1271_read(wl, addr, rx_buf, test_size, false);
  313. if (tx_started)
  314. wl1271_write(wl, addr, tx_buf, test_size, false);
  315. if (!rx_started && !tx_started)
  316. msleep(100);
  317. }
  318. free:
  319. kfree(tx_buf);
  320. kfree(rx_buf);
  321. return ret;
  322. }
  323. static int __devinit wl1271_probe(struct sdio_func *func,
  324. const struct sdio_device_id *id)
  325. {
  326. const struct wl12xx_platform_data *wlan_data;
  327. struct wl1271 *wl;
  328. struct wl1271_test *wl_test;
  329. int ret = 0;
  330. /* wl1271 has 2 sdio functions we handle just the wlan part */
  331. if (func->num != 0x02)
  332. return -ENODEV;
  333. wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
  334. if (!wl_test) {
  335. dev_err(&func->dev, "Could not allocate memory\n");
  336. return -ENOMEM;
  337. }
  338. wl = &wl_test->wl;
  339. wl->if_priv = func;
  340. wl->if_ops = &sdio_ops;
  341. /* Grab access to FN0 for ELP reg. */
  342. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  343. /* Use block mode for transferring over one block size of data */
  344. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  345. wlan_data = wl12xx_get_platform_data();
  346. if (IS_ERR(wlan_data)) {
  347. ret = PTR_ERR(wlan_data);
  348. dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
  349. goto out_free;
  350. }
  351. wl->irq = wlan_data->irq;
  352. wl->ref_clock = wlan_data->board_ref_clock;
  353. wl->tcxo_clock = wlan_data->board_tcxo_clock;
  354. sdio_set_drvdata(func, wl_test);
  355. /* power up the device */
  356. ret = wl1271_chip_wakeup(wl);
  357. if (ret) {
  358. dev_err(&func->dev, "could not wake up chip\n");
  359. goto out_free;
  360. }
  361. if (wl->fw == NULL) {
  362. ret = wl1271_fetch_firmware(wl);
  363. if (ret < 0) {
  364. dev_err(&func->dev, "firmware fetch error\n");
  365. goto out_off;
  366. }
  367. }
  368. /* fetch NVS */
  369. if (wl->nvs == NULL) {
  370. ret = wl1271_fetch_nvs(wl);
  371. if (ret < 0) {
  372. dev_err(&func->dev, "NVS fetch error\n");
  373. goto out_off;
  374. }
  375. }
  376. ret = wl1271_load_firmware(wl);
  377. if (ret < 0) {
  378. dev_err(&func->dev, "firmware load error: %d\n", ret);
  379. goto out_free;
  380. }
  381. dev_info(&func->dev, "initialized\n");
  382. /* I/O testing will be done in the tester thread */
  383. wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
  384. if (IS_ERR(wl_test->test_task)) {
  385. dev_err(&func->dev, "unable to create kernel thread\n");
  386. ret = PTR_ERR(wl_test->test_task);
  387. goto out_free;
  388. }
  389. return 0;
  390. out_off:
  391. /* power off the chip */
  392. wl1271_power_off(wl);
  393. out_free:
  394. kfree(wl_test);
  395. return ret;
  396. }
  397. static void __devexit wl1271_remove(struct sdio_func *func)
  398. {
  399. struct wl1271_test *wl_test = sdio_get_drvdata(func);
  400. /* stop the I/O test thread */
  401. kthread_stop(wl_test->test_task);
  402. /* power off the chip */
  403. wl1271_power_off(&wl_test->wl);
  404. vfree(wl_test->wl.fw);
  405. wl_test->wl.fw = NULL;
  406. kfree(wl_test->wl.nvs);
  407. wl_test->wl.nvs = NULL;
  408. kfree(wl_test);
  409. }
  410. static struct sdio_driver wl1271_sdio_driver = {
  411. .name = "wl12xx_sdio_test",
  412. .id_table = wl1271_devices,
  413. .probe = wl1271_probe,
  414. .remove = __devexit_p(wl1271_remove),
  415. };
  416. static int __init wl1271_init(void)
  417. {
  418. int ret;
  419. ret = sdio_register_driver(&wl1271_sdio_driver);
  420. if (ret < 0)
  421. pr_err("failed to register sdio driver: %d\n", ret);
  422. return ret;
  423. }
  424. module_init(wl1271_init);
  425. static void __exit wl1271_exit(void)
  426. {
  427. sdio_unregister_driver(&wl1271_sdio_driver);
  428. }
  429. module_exit(wl1271_exit);
  430. MODULE_LICENSE("GPL");
  431. MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");