sdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Intel Wireless Multicomm 3200 WiFi driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Intel Corporation nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * Intel Corporation <ilw@linux.intel.com>
  34. * Samuel Ortiz <samuel.ortiz@intel.com>
  35. * Zhu Yi <yi.zhu@intel.com>
  36. *
  37. */
  38. /*
  39. * This is the SDIO bus specific hooks for iwm.
  40. * It also is the module's entry point.
  41. *
  42. * Interesting code paths:
  43. * iwm_sdio_probe() (Called by an SDIO bus scan)
  44. * -> iwm_if_alloc() (netdev.c)
  45. * -> iwm_wdev_alloc() (cfg80211.c, allocates and register our wiphy)
  46. * -> wiphy_new()
  47. * -> wiphy_register()
  48. * -> alloc_netdev_mq()
  49. * -> register_netdev()
  50. *
  51. * iwm_sdio_remove()
  52. * -> iwm_if_free() (netdev.c)
  53. * -> unregister_netdev()
  54. * -> iwm_wdev_free() (cfg80211.c)
  55. * -> wiphy_unregister()
  56. * -> wiphy_free()
  57. *
  58. * iwm_sdio_isr() (called in process context from the SDIO core code)
  59. * -> queue_work(.., isr_worker)
  60. * -- [async] --> iwm_sdio_isr_worker()
  61. * -> iwm_rx_handle()
  62. */
  63. #include <linux/kernel.h>
  64. #include <linux/module.h>
  65. #include <linux/slab.h>
  66. #include <linux/netdevice.h>
  67. #include <linux/debugfs.h>
  68. #include <linux/mmc/sdio_ids.h>
  69. #include <linux/mmc/sdio.h>
  70. #include <linux/mmc/sdio_func.h>
  71. #include "iwm.h"
  72. #include "debug.h"
  73. #include "bus.h"
  74. #include "sdio.h"
  75. static void iwm_sdio_isr_worker(struct work_struct *work)
  76. {
  77. struct iwm_sdio_priv *hw;
  78. struct iwm_priv *iwm;
  79. struct iwm_rx_info *rx_info;
  80. struct sk_buff *skb;
  81. u8 *rx_buf;
  82. unsigned long rx_size;
  83. hw = container_of(work, struct iwm_sdio_priv, isr_worker);
  84. iwm = hw_to_iwm(hw);
  85. while (!skb_queue_empty(&iwm->rx_list)) {
  86. skb = skb_dequeue(&iwm->rx_list);
  87. rx_info = skb_to_rx_info(skb);
  88. rx_size = rx_info->rx_size;
  89. rx_buf = skb->data;
  90. IWM_HEXDUMP(iwm, DBG, SDIO, "RX: ", rx_buf, rx_size);
  91. if (iwm_rx_handle(iwm, rx_buf, rx_size) < 0)
  92. IWM_WARN(iwm, "RX error\n");
  93. kfree_skb(skb);
  94. }
  95. }
  96. static void iwm_sdio_isr(struct sdio_func *func)
  97. {
  98. struct iwm_priv *iwm;
  99. struct iwm_sdio_priv *hw;
  100. struct iwm_rx_info *rx_info;
  101. struct sk_buff *skb;
  102. unsigned long buf_size, read_size;
  103. int ret;
  104. u8 val;
  105. hw = sdio_get_drvdata(func);
  106. iwm = hw_to_iwm(hw);
  107. buf_size = hw->blk_size;
  108. /* We're checking the status */
  109. val = sdio_readb(func, IWM_SDIO_INTR_STATUS_ADDR, &ret);
  110. if (val == 0 || ret < 0) {
  111. IWM_ERR(iwm, "Wrong INTR_STATUS\n");
  112. return;
  113. }
  114. /* See if we have free buffers */
  115. if (skb_queue_len(&iwm->rx_list) > IWM_RX_LIST_SIZE) {
  116. IWM_ERR(iwm, "No buffer for more Rx frames\n");
  117. return;
  118. }
  119. /* We first read the transaction size */
  120. read_size = sdio_readb(func, IWM_SDIO_INTR_GET_SIZE_ADDR + 1, &ret);
  121. read_size = read_size << 8;
  122. if (ret < 0) {
  123. IWM_ERR(iwm, "Couldn't read the xfer size\n");
  124. return;
  125. }
  126. /* We need to clear the INT register */
  127. sdio_writeb(func, 1, IWM_SDIO_INTR_CLEAR_ADDR, &ret);
  128. if (ret < 0) {
  129. IWM_ERR(iwm, "Couldn't clear the INT register\n");
  130. return;
  131. }
  132. while (buf_size < read_size)
  133. buf_size <<= 1;
  134. skb = dev_alloc_skb(buf_size);
  135. if (!skb) {
  136. IWM_ERR(iwm, "Couldn't alloc RX skb\n");
  137. return;
  138. }
  139. rx_info = skb_to_rx_info(skb);
  140. rx_info->rx_size = read_size;
  141. rx_info->rx_buf_size = buf_size;
  142. /* Now we can read the actual buffer */
  143. ret = sdio_memcpy_fromio(func, skb_put(skb, read_size),
  144. IWM_SDIO_DATA_ADDR, read_size);
  145. /* The skb is put on a driver's specific Rx SKB list */
  146. skb_queue_tail(&iwm->rx_list, skb);
  147. /* We can now schedule the actual worker */
  148. queue_work(hw->isr_wq, &hw->isr_worker);
  149. }
  150. static void iwm_sdio_rx_free(struct iwm_sdio_priv *hw)
  151. {
  152. struct iwm_priv *iwm = hw_to_iwm(hw);
  153. flush_workqueue(hw->isr_wq);
  154. skb_queue_purge(&iwm->rx_list);
  155. }
  156. /* Bus ops */
  157. static int if_sdio_enable(struct iwm_priv *iwm)
  158. {
  159. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  160. int ret;
  161. sdio_claim_host(hw->func);
  162. ret = sdio_enable_func(hw->func);
  163. if (ret) {
  164. IWM_ERR(iwm, "Couldn't enable the device: is TOP driver "
  165. "loaded and functional?\n");
  166. goto release_host;
  167. }
  168. iwm_reset(iwm);
  169. ret = sdio_claim_irq(hw->func, iwm_sdio_isr);
  170. if (ret) {
  171. IWM_ERR(iwm, "Failed to claim irq: %d\n", ret);
  172. goto release_host;
  173. }
  174. sdio_writeb(hw->func, 1, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  175. if (ret < 0) {
  176. IWM_ERR(iwm, "Couldn't enable INTR: %d\n", ret);
  177. goto release_irq;
  178. }
  179. sdio_release_host(hw->func);
  180. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO enable\n");
  181. return 0;
  182. release_irq:
  183. sdio_release_irq(hw->func);
  184. release_host:
  185. sdio_release_host(hw->func);
  186. return ret;
  187. }
  188. static int if_sdio_disable(struct iwm_priv *iwm)
  189. {
  190. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  191. int ret;
  192. sdio_claim_host(hw->func);
  193. sdio_writeb(hw->func, 0, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  194. if (ret < 0)
  195. IWM_WARN(iwm, "Couldn't disable INTR: %d\n", ret);
  196. sdio_release_irq(hw->func);
  197. sdio_disable_func(hw->func);
  198. sdio_release_host(hw->func);
  199. iwm_sdio_rx_free(hw);
  200. iwm_reset(iwm);
  201. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO disable\n");
  202. return 0;
  203. }
  204. static int if_sdio_send_chunk(struct iwm_priv *iwm, u8 *buf, int count)
  205. {
  206. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  207. int aligned_count = ALIGN(count, hw->blk_size);
  208. int ret;
  209. if ((unsigned long)buf & 0x3) {
  210. IWM_ERR(iwm, "buf <%p> is not dword aligned\n", buf);
  211. /* TODO: Is this a hardware limitation? use get_unligned */
  212. return -EINVAL;
  213. }
  214. sdio_claim_host(hw->func);
  215. ret = sdio_memcpy_toio(hw->func, IWM_SDIO_DATA_ADDR, buf,
  216. aligned_count);
  217. sdio_release_host(hw->func);
  218. return ret;
  219. }
  220. static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
  221. size_t count, loff_t *ppos)
  222. {
  223. struct iwm_priv *iwm = filp->private_data;
  224. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  225. char *buf;
  226. u8 cccr;
  227. int buf_len = 4096, ret;
  228. size_t len = 0;
  229. if (*ppos != 0)
  230. return 0;
  231. if (count < sizeof(buf))
  232. return -ENOSPC;
  233. buf = kzalloc(buf_len, GFP_KERNEL);
  234. if (!buf)
  235. return -ENOMEM;
  236. sdio_claim_host(hw->func);
  237. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
  238. if (ret) {
  239. IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
  240. goto err;
  241. }
  242. len += snprintf(buf + len, buf_len - len, "CCCR_IOEx: 0x%x\n", cccr);
  243. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
  244. if (ret) {
  245. IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
  246. goto err;
  247. }
  248. len += snprintf(buf + len, buf_len - len, "CCCR_IORx: 0x%x\n", cccr);
  249. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
  250. if (ret) {
  251. IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
  252. goto err;
  253. }
  254. len += snprintf(buf + len, buf_len - len, "CCCR_IENx: 0x%x\n", cccr);
  255. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
  256. if (ret) {
  257. IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
  258. goto err;
  259. }
  260. len += snprintf(buf + len, buf_len - len, "CCCR_INTx: 0x%x\n", cccr);
  261. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
  262. if (ret) {
  263. IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
  264. goto err;
  265. }
  266. len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
  267. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
  268. if (ret) {
  269. IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
  270. goto err;
  271. }
  272. len += snprintf(buf + len, buf_len - len, "CCCR_IF: 0x%x\n", cccr);
  273. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
  274. if (ret) {
  275. IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
  276. goto err;
  277. }
  278. len += snprintf(buf + len, buf_len - len, "CCCR_CAPS: 0x%x\n", cccr);
  279. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
  280. if (ret) {
  281. IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
  282. goto err;
  283. }
  284. len += snprintf(buf + len, buf_len - len, "CCCR_CIS: 0x%x\n", cccr);
  285. ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
  286. err:
  287. sdio_release_host(hw->func);
  288. kfree(buf);
  289. return ret;
  290. }
  291. static const struct file_operations iwm_debugfs_sdio_fops = {
  292. .owner = THIS_MODULE,
  293. .open = simple_open,
  294. .read = iwm_debugfs_sdio_read,
  295. .llseek = default_llseek,
  296. };
  297. static void if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
  298. {
  299. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  300. hw->cccr_dentry = debugfs_create_file("cccr", 0200,
  301. parent_dir, iwm,
  302. &iwm_debugfs_sdio_fops);
  303. }
  304. static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
  305. {
  306. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  307. debugfs_remove(hw->cccr_dentry);
  308. }
  309. static struct iwm_if_ops if_sdio_ops = {
  310. .enable = if_sdio_enable,
  311. .disable = if_sdio_disable,
  312. .send_chunk = if_sdio_send_chunk,
  313. .debugfs_init = if_sdio_debugfs_init,
  314. .debugfs_exit = if_sdio_debugfs_exit,
  315. .umac_name = "iwmc3200wifi-umac-sdio.bin",
  316. .calib_lmac_name = "iwmc3200wifi-calib-sdio.bin",
  317. .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
  318. };
  319. MODULE_FIRMWARE("iwmc3200wifi-umac-sdio.bin");
  320. MODULE_FIRMWARE("iwmc3200wifi-calib-sdio.bin");
  321. MODULE_FIRMWARE("iwmc3200wifi-lmac-sdio.bin");
  322. static int iwm_sdio_probe(struct sdio_func *func,
  323. const struct sdio_device_id *id)
  324. {
  325. struct iwm_priv *iwm;
  326. struct iwm_sdio_priv *hw;
  327. struct device *dev = &func->dev;
  328. int ret;
  329. /* check if TOP has already initialized the card */
  330. sdio_claim_host(func);
  331. ret = sdio_enable_func(func);
  332. if (ret) {
  333. dev_err(dev, "wait for TOP to enable the device\n");
  334. sdio_release_host(func);
  335. return ret;
  336. }
  337. ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
  338. sdio_disable_func(func);
  339. sdio_release_host(func);
  340. if (ret < 0) {
  341. dev_err(dev, "Failed to set block size: %d\n", ret);
  342. return ret;
  343. }
  344. iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
  345. if (IS_ERR(iwm)) {
  346. dev_err(dev, "allocate SDIO interface failed\n");
  347. return PTR_ERR(iwm);
  348. }
  349. hw = iwm_private(iwm);
  350. hw->iwm = iwm;
  351. iwm_debugfs_init(iwm);
  352. sdio_set_drvdata(func, hw);
  353. hw->func = func;
  354. hw->blk_size = IWM_SDIO_BLK_SIZE;
  355. hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
  356. if (!hw->isr_wq) {
  357. ret = -ENOMEM;
  358. goto debugfs_exit;
  359. }
  360. INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
  361. ret = iwm_if_add(iwm);
  362. if (ret) {
  363. dev_err(dev, "add SDIO interface failed\n");
  364. goto destroy_wq;
  365. }
  366. dev_info(dev, "IWM SDIO probe\n");
  367. return 0;
  368. destroy_wq:
  369. destroy_workqueue(hw->isr_wq);
  370. debugfs_exit:
  371. iwm_debugfs_exit(iwm);
  372. iwm_if_free(iwm);
  373. return ret;
  374. }
  375. static void iwm_sdio_remove(struct sdio_func *func)
  376. {
  377. struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
  378. struct iwm_priv *iwm = hw_to_iwm(hw);
  379. struct device *dev = &func->dev;
  380. iwm_if_remove(iwm);
  381. destroy_workqueue(hw->isr_wq);
  382. iwm_debugfs_exit(iwm);
  383. iwm_if_free(iwm);
  384. sdio_set_drvdata(func, NULL);
  385. dev_info(dev, "IWM SDIO remove\n");
  386. }
  387. static const struct sdio_device_id iwm_sdio_ids[] = {
  388. /* Global/AGN SKU */
  389. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1403) },
  390. /* BGN SKU */
  391. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1408) },
  392. { /* end: all zeroes */ },
  393. };
  394. MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
  395. static struct sdio_driver iwm_sdio_driver = {
  396. .name = "iwm_sdio",
  397. .id_table = iwm_sdio_ids,
  398. .probe = iwm_sdio_probe,
  399. .remove = iwm_sdio_remove,
  400. };
  401. static int __init iwm_sdio_init_module(void)
  402. {
  403. return sdio_register_driver(&iwm_sdio_driver);
  404. }
  405. static void __exit iwm_sdio_exit_module(void)
  406. {
  407. sdio_unregister_driver(&iwm_sdio_driver);
  408. }
  409. module_init(iwm_sdio_init_module);
  410. module_exit(iwm_sdio_exit_module);
  411. MODULE_LICENSE("GPL");
  412. MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);