sdio_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. default:
  241. wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
  242. return -ENODEV;
  243. }
  244. return ret;
  245. }
  246. static struct wl1271_partition_set part_down = {
  247. .mem = {
  248. .start = 0x00000000,
  249. .size = 0x000177c0
  250. },
  251. .reg = {
  252. .start = REGISTERS_BASE,
  253. .size = 0x00008800
  254. },
  255. .mem2 = {
  256. .start = 0x00000000,
  257. .size = 0x00000000
  258. },
  259. .mem3 = {
  260. .start = 0x00000000,
  261. .size = 0x00000000
  262. },
  263. };
  264. static int tester(void *data)
  265. {
  266. struct wl1271 *wl = data;
  267. struct sdio_func *func = wl_to_func(wl);
  268. struct device *pdev = &func->dev;
  269. int ret = 0;
  270. bool rx_started = 0;
  271. bool tx_started = 0;
  272. uint8_t *tx_buf, *rx_buf;
  273. int test_size = PAGE_SIZE;
  274. u32 addr = 0;
  275. struct wl1271_partition_set partition;
  276. /* We assume chip is powered up and firmware fetched */
  277. memcpy(&partition, &part_down, sizeof(partition));
  278. partition.mem.start = addr;
  279. wl1271_set_partition(wl, &partition);
  280. tx_buf = kmalloc(test_size, GFP_KERNEL);
  281. rx_buf = kmalloc(test_size, GFP_KERNEL);
  282. if (!tx_buf || !rx_buf) {
  283. dev_err(pdev,
  284. "Could not allocate memory. Test will not run.\n");
  285. ret = -ENOMEM;
  286. goto free;
  287. }
  288. memset(tx_buf, 0x5a, test_size);
  289. /* write something in data area so we can read it back */
  290. wl1271_write(wl, addr, tx_buf, test_size, false);
  291. while (!kthread_should_stop()) {
  292. if (rx && !rx_started) {
  293. dev_info(pdev, "starting rx test\n");
  294. rx_started = 1;
  295. } else if (!rx && rx_started) {
  296. dev_info(pdev, "stopping rx test\n");
  297. rx_started = 0;
  298. }
  299. if (tx && !tx_started) {
  300. dev_info(pdev, "starting tx test\n");
  301. tx_started = 1;
  302. } else if (!tx && tx_started) {
  303. dev_info(pdev, "stopping tx test\n");
  304. tx_started = 0;
  305. }
  306. if (rx_started)
  307. wl1271_read(wl, addr, rx_buf, test_size, false);
  308. if (tx_started)
  309. wl1271_write(wl, addr, tx_buf, test_size, false);
  310. if (!rx_started && !tx_started)
  311. msleep(100);
  312. }
  313. free:
  314. kfree(tx_buf);
  315. kfree(rx_buf);
  316. return ret;
  317. }
  318. static int __devinit wl1271_probe(struct sdio_func *func,
  319. const struct sdio_device_id *id)
  320. {
  321. const struct wl12xx_platform_data *wlan_data;
  322. struct wl1271 *wl;
  323. struct wl1271_test *wl_test;
  324. int ret = 0;
  325. /* wl1271 has 2 sdio functions we handle just the wlan part */
  326. if (func->num != 0x02)
  327. return -ENODEV;
  328. wl_test = kzalloc(sizeof(struct wl1271_test), GFP_KERNEL);
  329. if (!wl_test) {
  330. dev_err(&func->dev, "Could not allocate memory\n");
  331. return -ENOMEM;
  332. }
  333. wl = &wl_test->wl;
  334. wl->if_priv = func;
  335. wl->if_ops = &sdio_ops;
  336. /* Grab access to FN0 for ELP reg. */
  337. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  338. wlan_data = wl12xx_get_platform_data();
  339. if (IS_ERR(wlan_data)) {
  340. ret = PTR_ERR(wlan_data);
  341. dev_err(&func->dev, "missing wlan platform data: %d\n", ret);
  342. goto out_free;
  343. }
  344. wl->irq = wlan_data->irq;
  345. wl->ref_clock = wlan_data->board_ref_clock;
  346. wl->tcxo_clock = wlan_data->board_tcxo_clock;
  347. sdio_set_drvdata(func, wl_test);
  348. /* power up the device */
  349. ret = wl1271_chip_wakeup(wl);
  350. if (ret) {
  351. dev_err(&func->dev, "could not wake up chip\n");
  352. goto out_free;
  353. }
  354. if (wl->fw == NULL) {
  355. ret = wl1271_fetch_firmware(wl);
  356. if (ret < 0) {
  357. dev_err(&func->dev, "firmware fetch error\n");
  358. goto out_off;
  359. }
  360. }
  361. /* fetch NVS */
  362. if (wl->nvs == NULL) {
  363. ret = wl1271_fetch_nvs(wl);
  364. if (ret < 0) {
  365. dev_err(&func->dev, "NVS fetch error\n");
  366. goto out_off;
  367. }
  368. }
  369. ret = wl1271_load_firmware(wl);
  370. if (ret < 0) {
  371. dev_err(&func->dev, "firmware load error: %d\n", ret);
  372. goto out_free;
  373. }
  374. dev_info(&func->dev, "initialized\n");
  375. /* I/O testing will be done in the tester thread */
  376. wl_test->test_task = kthread_run(tester, wl, "sdio_tester");
  377. if (IS_ERR(wl_test->test_task)) {
  378. dev_err(&func->dev, "unable to create kernel thread\n");
  379. ret = PTR_ERR(wl_test->test_task);
  380. goto out_free;
  381. }
  382. return 0;
  383. out_off:
  384. /* power off the chip */
  385. wl1271_power_off(wl);
  386. out_free:
  387. kfree(wl_test);
  388. return ret;
  389. }
  390. static void __devexit wl1271_remove(struct sdio_func *func)
  391. {
  392. struct wl1271_test *wl_test = sdio_get_drvdata(func);
  393. /* stop the I/O test thread */
  394. kthread_stop(wl_test->test_task);
  395. /* power off the chip */
  396. wl1271_power_off(&wl_test->wl);
  397. vfree(wl_test->wl.fw);
  398. wl_test->wl.fw = NULL;
  399. kfree(wl_test->wl.nvs);
  400. wl_test->wl.nvs = NULL;
  401. kfree(wl_test);
  402. }
  403. static struct sdio_driver wl1271_sdio_driver = {
  404. .name = "wl12xx_sdio_test",
  405. .id_table = wl1271_devices,
  406. .probe = wl1271_probe,
  407. .remove = __devexit_p(wl1271_remove),
  408. };
  409. static int __init wl1271_init(void)
  410. {
  411. int ret;
  412. ret = sdio_register_driver(&wl1271_sdio_driver);
  413. if (ret < 0)
  414. pr_err("failed to register sdio driver: %d\n", ret);
  415. return ret;
  416. }
  417. module_init(wl1271_init);
  418. static void __exit wl1271_exit(void)
  419. {
  420. sdio_unregister_driver(&wl1271_sdio_driver);
  421. }
  422. module_exit(wl1271_exit);
  423. MODULE_LICENSE("GPL");
  424. MODULE_AUTHOR("Roger Quadros <roger.quadros@nokia.com>");