sdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. /* debugfs hooks */
  221. static int iwm_debugfs_sdio_open(struct inode *inode, struct file *filp)
  222. {
  223. filp->private_data = inode->i_private;
  224. return 0;
  225. }
  226. static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
  227. size_t count, loff_t *ppos)
  228. {
  229. struct iwm_priv *iwm = filp->private_data;
  230. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  231. char *buf;
  232. u8 cccr;
  233. int buf_len = 4096, ret;
  234. size_t len = 0;
  235. if (*ppos != 0)
  236. return 0;
  237. if (count < sizeof(buf))
  238. return -ENOSPC;
  239. buf = kzalloc(buf_len, GFP_KERNEL);
  240. if (!buf)
  241. return -ENOMEM;
  242. sdio_claim_host(hw->func);
  243. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
  244. if (ret) {
  245. IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
  246. goto err;
  247. }
  248. len += snprintf(buf + len, buf_len - len, "CCCR_IOEx: 0x%x\n", cccr);
  249. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
  250. if (ret) {
  251. IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
  252. goto err;
  253. }
  254. len += snprintf(buf + len, buf_len - len, "CCCR_IORx: 0x%x\n", cccr);
  255. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
  256. if (ret) {
  257. IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
  258. goto err;
  259. }
  260. len += snprintf(buf + len, buf_len - len, "CCCR_IENx: 0x%x\n", cccr);
  261. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
  262. if (ret) {
  263. IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
  264. goto err;
  265. }
  266. len += snprintf(buf + len, buf_len - len, "CCCR_INTx: 0x%x\n", cccr);
  267. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
  268. if (ret) {
  269. IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
  270. goto err;
  271. }
  272. len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
  273. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
  274. if (ret) {
  275. IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
  276. goto err;
  277. }
  278. len += snprintf(buf + len, buf_len - len, "CCCR_IF: 0x%x\n", cccr);
  279. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
  280. if (ret) {
  281. IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
  282. goto err;
  283. }
  284. len += snprintf(buf + len, buf_len - len, "CCCR_CAPS: 0x%x\n", cccr);
  285. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
  286. if (ret) {
  287. IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
  288. goto err;
  289. }
  290. len += snprintf(buf + len, buf_len - len, "CCCR_CIS: 0x%x\n", cccr);
  291. ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
  292. err:
  293. sdio_release_host(hw->func);
  294. kfree(buf);
  295. return ret;
  296. }
  297. static const struct file_operations iwm_debugfs_sdio_fops = {
  298. .owner = THIS_MODULE,
  299. .open = iwm_debugfs_sdio_open,
  300. .read = iwm_debugfs_sdio_read,
  301. .llseek = default_llseek,
  302. };
  303. static void if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
  304. {
  305. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  306. hw->cccr_dentry = debugfs_create_file("cccr", 0200,
  307. parent_dir, iwm,
  308. &iwm_debugfs_sdio_fops);
  309. }
  310. static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
  311. {
  312. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  313. debugfs_remove(hw->cccr_dentry);
  314. }
  315. static struct iwm_if_ops if_sdio_ops = {
  316. .enable = if_sdio_enable,
  317. .disable = if_sdio_disable,
  318. .send_chunk = if_sdio_send_chunk,
  319. .debugfs_init = if_sdio_debugfs_init,
  320. .debugfs_exit = if_sdio_debugfs_exit,
  321. .umac_name = "iwmc3200wifi-umac-sdio.bin",
  322. .calib_lmac_name = "iwmc3200wifi-calib-sdio.bin",
  323. .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
  324. };
  325. MODULE_FIRMWARE("iwmc3200wifi-umac-sdio.bin");
  326. MODULE_FIRMWARE("iwmc3200wifi-calib-sdio.bin");
  327. MODULE_FIRMWARE("iwmc3200wifi-lmac-sdio.bin");
  328. static int iwm_sdio_probe(struct sdio_func *func,
  329. const struct sdio_device_id *id)
  330. {
  331. struct iwm_priv *iwm;
  332. struct iwm_sdio_priv *hw;
  333. struct device *dev = &func->dev;
  334. int ret;
  335. /* check if TOP has already initialized the card */
  336. sdio_claim_host(func);
  337. ret = sdio_enable_func(func);
  338. if (ret) {
  339. dev_err(dev, "wait for TOP to enable the device\n");
  340. sdio_release_host(func);
  341. return ret;
  342. }
  343. ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
  344. sdio_disable_func(func);
  345. sdio_release_host(func);
  346. if (ret < 0) {
  347. dev_err(dev, "Failed to set block size: %d\n", ret);
  348. return ret;
  349. }
  350. iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
  351. if (IS_ERR(iwm)) {
  352. dev_err(dev, "allocate SDIO interface failed\n");
  353. return PTR_ERR(iwm);
  354. }
  355. hw = iwm_private(iwm);
  356. hw->iwm = iwm;
  357. iwm_debugfs_init(iwm);
  358. sdio_set_drvdata(func, hw);
  359. hw->func = func;
  360. hw->blk_size = IWM_SDIO_BLK_SIZE;
  361. hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
  362. if (!hw->isr_wq) {
  363. ret = -ENOMEM;
  364. goto debugfs_exit;
  365. }
  366. INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
  367. ret = iwm_if_add(iwm);
  368. if (ret) {
  369. dev_err(dev, "add SDIO interface failed\n");
  370. goto destroy_wq;
  371. }
  372. dev_info(dev, "IWM SDIO probe\n");
  373. return 0;
  374. destroy_wq:
  375. destroy_workqueue(hw->isr_wq);
  376. debugfs_exit:
  377. iwm_debugfs_exit(iwm);
  378. iwm_if_free(iwm);
  379. return ret;
  380. }
  381. static void iwm_sdio_remove(struct sdio_func *func)
  382. {
  383. struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
  384. struct iwm_priv *iwm = hw_to_iwm(hw);
  385. struct device *dev = &func->dev;
  386. iwm_if_remove(iwm);
  387. destroy_workqueue(hw->isr_wq);
  388. iwm_debugfs_exit(iwm);
  389. iwm_if_free(iwm);
  390. sdio_set_drvdata(func, NULL);
  391. dev_info(dev, "IWM SDIO remove\n");
  392. }
  393. static const struct sdio_device_id iwm_sdio_ids[] = {
  394. /* Global/AGN SKU */
  395. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1403) },
  396. /* BGN SKU */
  397. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1408) },
  398. { /* end: all zeroes */ },
  399. };
  400. MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
  401. static struct sdio_driver iwm_sdio_driver = {
  402. .name = "iwm_sdio",
  403. .id_table = iwm_sdio_ids,
  404. .probe = iwm_sdio_probe,
  405. .remove = iwm_sdio_remove,
  406. };
  407. static int __init iwm_sdio_init_module(void)
  408. {
  409. return sdio_register_driver(&iwm_sdio_driver);
  410. }
  411. static void __exit iwm_sdio_exit_module(void)
  412. {
  413. sdio_unregister_driver(&iwm_sdio_driver);
  414. }
  415. module_init(iwm_sdio_init_module);
  416. module_exit(iwm_sdio_exit_module);
  417. MODULE_LICENSE("GPL");
  418. MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);