sdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/slab.h>
  65. #include <linux/netdevice.h>
  66. #include <linux/debugfs.h>
  67. #include <linux/mmc/sdio_ids.h>
  68. #include <linux/mmc/sdio.h>
  69. #include <linux/mmc/sdio_func.h>
  70. #include "iwm.h"
  71. #include "debug.h"
  72. #include "bus.h"
  73. #include "sdio.h"
  74. static void iwm_sdio_isr_worker(struct work_struct *work)
  75. {
  76. struct iwm_sdio_priv *hw;
  77. struct iwm_priv *iwm;
  78. struct iwm_rx_info *rx_info;
  79. struct sk_buff *skb;
  80. u8 *rx_buf;
  81. unsigned long rx_size;
  82. hw = container_of(work, struct iwm_sdio_priv, isr_worker);
  83. iwm = hw_to_iwm(hw);
  84. while (!skb_queue_empty(&iwm->rx_list)) {
  85. skb = skb_dequeue(&iwm->rx_list);
  86. rx_info = skb_to_rx_info(skb);
  87. rx_size = rx_info->rx_size;
  88. rx_buf = skb->data;
  89. IWM_HEXDUMP(iwm, DBG, SDIO, "RX: ", rx_buf, rx_size);
  90. if (iwm_rx_handle(iwm, rx_buf, rx_size) < 0)
  91. IWM_WARN(iwm, "RX error\n");
  92. kfree_skb(skb);
  93. }
  94. }
  95. static void iwm_sdio_isr(struct sdio_func *func)
  96. {
  97. struct iwm_priv *iwm;
  98. struct iwm_sdio_priv *hw;
  99. struct iwm_rx_info *rx_info;
  100. struct sk_buff *skb;
  101. unsigned long buf_size, read_size;
  102. int ret;
  103. u8 val;
  104. hw = sdio_get_drvdata(func);
  105. iwm = hw_to_iwm(hw);
  106. buf_size = hw->blk_size;
  107. /* We're checking the status */
  108. val = sdio_readb(func, IWM_SDIO_INTR_STATUS_ADDR, &ret);
  109. if (val == 0 || ret < 0) {
  110. IWM_ERR(iwm, "Wrong INTR_STATUS\n");
  111. return;
  112. }
  113. /* See if we have free buffers */
  114. if (skb_queue_len(&iwm->rx_list) > IWM_RX_LIST_SIZE) {
  115. IWM_ERR(iwm, "No buffer for more Rx frames\n");
  116. return;
  117. }
  118. /* We first read the transaction size */
  119. read_size = sdio_readb(func, IWM_SDIO_INTR_GET_SIZE_ADDR + 1, &ret);
  120. read_size = read_size << 8;
  121. if (ret < 0) {
  122. IWM_ERR(iwm, "Couldn't read the xfer size\n");
  123. return;
  124. }
  125. /* We need to clear the INT register */
  126. sdio_writeb(func, 1, IWM_SDIO_INTR_CLEAR_ADDR, &ret);
  127. if (ret < 0) {
  128. IWM_ERR(iwm, "Couldn't clear the INT register\n");
  129. return;
  130. }
  131. while (buf_size < read_size)
  132. buf_size <<= 1;
  133. skb = dev_alloc_skb(buf_size);
  134. if (!skb) {
  135. IWM_ERR(iwm, "Couldn't alloc RX skb\n");
  136. return;
  137. }
  138. rx_info = skb_to_rx_info(skb);
  139. rx_info->rx_size = read_size;
  140. rx_info->rx_buf_size = buf_size;
  141. /* Now we can read the actual buffer */
  142. ret = sdio_memcpy_fromio(func, skb_put(skb, read_size),
  143. IWM_SDIO_DATA_ADDR, read_size);
  144. /* The skb is put on a driver's specific Rx SKB list */
  145. skb_queue_tail(&iwm->rx_list, skb);
  146. /* We can now schedule the actual worker */
  147. queue_work(hw->isr_wq, &hw->isr_worker);
  148. }
  149. static void iwm_sdio_rx_free(struct iwm_sdio_priv *hw)
  150. {
  151. struct iwm_priv *iwm = hw_to_iwm(hw);
  152. flush_workqueue(hw->isr_wq);
  153. skb_queue_purge(&iwm->rx_list);
  154. }
  155. /* Bus ops */
  156. static int if_sdio_enable(struct iwm_priv *iwm)
  157. {
  158. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  159. int ret;
  160. sdio_claim_host(hw->func);
  161. ret = sdio_enable_func(hw->func);
  162. if (ret) {
  163. IWM_ERR(iwm, "Couldn't enable the device: is TOP driver "
  164. "loaded and functional?\n");
  165. goto release_host;
  166. }
  167. iwm_reset(iwm);
  168. ret = sdio_claim_irq(hw->func, iwm_sdio_isr);
  169. if (ret) {
  170. IWM_ERR(iwm, "Failed to claim irq: %d\n", ret);
  171. goto release_host;
  172. }
  173. sdio_writeb(hw->func, 1, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  174. if (ret < 0) {
  175. IWM_ERR(iwm, "Couldn't enable INTR: %d\n", ret);
  176. goto release_irq;
  177. }
  178. sdio_release_host(hw->func);
  179. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO enable\n");
  180. return 0;
  181. release_irq:
  182. sdio_release_irq(hw->func);
  183. release_host:
  184. sdio_release_host(hw->func);
  185. return ret;
  186. }
  187. static int if_sdio_disable(struct iwm_priv *iwm)
  188. {
  189. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  190. int ret;
  191. sdio_claim_host(hw->func);
  192. sdio_writeb(hw->func, 0, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  193. if (ret < 0)
  194. IWM_WARN(iwm, "Couldn't disable INTR: %d\n", ret);
  195. sdio_release_irq(hw->func);
  196. sdio_disable_func(hw->func);
  197. sdio_release_host(hw->func);
  198. iwm_sdio_rx_free(hw);
  199. iwm_reset(iwm);
  200. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO disable\n");
  201. return 0;
  202. }
  203. static int if_sdio_send_chunk(struct iwm_priv *iwm, u8 *buf, int count)
  204. {
  205. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  206. int aligned_count = ALIGN(count, hw->blk_size);
  207. int ret;
  208. if ((unsigned long)buf & 0x3) {
  209. IWM_ERR(iwm, "buf <%p> is not dword aligned\n", buf);
  210. /* TODO: Is this a hardware limitation? use get_unligned */
  211. return -EINVAL;
  212. }
  213. sdio_claim_host(hw->func);
  214. ret = sdio_memcpy_toio(hw->func, IWM_SDIO_DATA_ADDR, buf,
  215. aligned_count);
  216. sdio_release_host(hw->func);
  217. return ret;
  218. }
  219. /* debugfs hooks */
  220. static int iwm_debugfs_sdio_open(struct inode *inode, struct file *filp)
  221. {
  222. filp->private_data = inode->i_private;
  223. return 0;
  224. }
  225. static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
  226. size_t count, loff_t *ppos)
  227. {
  228. struct iwm_priv *iwm = filp->private_data;
  229. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  230. char *buf;
  231. u8 cccr;
  232. int buf_len = 4096, ret;
  233. size_t len = 0;
  234. if (*ppos != 0)
  235. return 0;
  236. if (count < sizeof(buf))
  237. return -ENOSPC;
  238. buf = kzalloc(buf_len, GFP_KERNEL);
  239. if (!buf)
  240. return -ENOMEM;
  241. sdio_claim_host(hw->func);
  242. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
  243. if (ret) {
  244. IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
  245. goto err;
  246. }
  247. len += snprintf(buf + len, buf_len - len, "CCCR_IOEx: 0x%x\n", cccr);
  248. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
  249. if (ret) {
  250. IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
  251. goto err;
  252. }
  253. len += snprintf(buf + len, buf_len - len, "CCCR_IORx: 0x%x\n", cccr);
  254. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
  255. if (ret) {
  256. IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
  257. goto err;
  258. }
  259. len += snprintf(buf + len, buf_len - len, "CCCR_IENx: 0x%x\n", cccr);
  260. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
  261. if (ret) {
  262. IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
  263. goto err;
  264. }
  265. len += snprintf(buf + len, buf_len - len, "CCCR_INTx: 0x%x\n", cccr);
  266. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
  267. if (ret) {
  268. IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
  269. goto err;
  270. }
  271. len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
  272. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
  273. if (ret) {
  274. IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
  275. goto err;
  276. }
  277. len += snprintf(buf + len, buf_len - len, "CCCR_IF: 0x%x\n", cccr);
  278. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
  279. if (ret) {
  280. IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
  281. goto err;
  282. }
  283. len += snprintf(buf + len, buf_len - len, "CCCR_CAPS: 0x%x\n", cccr);
  284. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
  285. if (ret) {
  286. IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
  287. goto err;
  288. }
  289. len += snprintf(buf + len, buf_len - len, "CCCR_CIS: 0x%x\n", cccr);
  290. ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
  291. err:
  292. sdio_release_host(hw->func);
  293. kfree(buf);
  294. return ret;
  295. }
  296. static const struct file_operations iwm_debugfs_sdio_fops = {
  297. .owner = THIS_MODULE,
  298. .open = iwm_debugfs_sdio_open,
  299. .read = iwm_debugfs_sdio_read,
  300. };
  301. static void if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
  302. {
  303. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  304. hw->cccr_dentry = debugfs_create_file("cccr", 0200,
  305. parent_dir, iwm,
  306. &iwm_debugfs_sdio_fops);
  307. }
  308. static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
  309. {
  310. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  311. debugfs_remove(hw->cccr_dentry);
  312. }
  313. static struct iwm_if_ops if_sdio_ops = {
  314. .enable = if_sdio_enable,
  315. .disable = if_sdio_disable,
  316. .send_chunk = if_sdio_send_chunk,
  317. .debugfs_init = if_sdio_debugfs_init,
  318. .debugfs_exit = if_sdio_debugfs_exit,
  319. .umac_name = "iwmc3200wifi-umac-sdio.bin",
  320. .calib_lmac_name = "iwmc3200wifi-calib-sdio.bin",
  321. .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
  322. };
  323. MODULE_FIRMWARE("iwmc3200wifi-umac-sdio.bin");
  324. MODULE_FIRMWARE("iwmc3200wifi-calib-sdio.bin");
  325. MODULE_FIRMWARE("iwmc3200wifi-lmac-sdio.bin");
  326. static int iwm_sdio_probe(struct sdio_func *func,
  327. const struct sdio_device_id *id)
  328. {
  329. struct iwm_priv *iwm;
  330. struct iwm_sdio_priv *hw;
  331. struct device *dev = &func->dev;
  332. int ret;
  333. /* check if TOP has already initialized the card */
  334. sdio_claim_host(func);
  335. ret = sdio_enable_func(func);
  336. if (ret) {
  337. dev_err(dev, "wait for TOP to enable the device\n");
  338. sdio_release_host(func);
  339. return ret;
  340. }
  341. ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
  342. sdio_disable_func(func);
  343. sdio_release_host(func);
  344. if (ret < 0) {
  345. dev_err(dev, "Failed to set block size: %d\n", ret);
  346. return ret;
  347. }
  348. iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
  349. if (IS_ERR(iwm)) {
  350. dev_err(dev, "allocate SDIO interface failed\n");
  351. return PTR_ERR(iwm);
  352. }
  353. hw = iwm_private(iwm);
  354. hw->iwm = iwm;
  355. iwm_debugfs_init(iwm);
  356. sdio_set_drvdata(func, hw);
  357. hw->func = func;
  358. hw->blk_size = IWM_SDIO_BLK_SIZE;
  359. hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
  360. if (!hw->isr_wq) {
  361. ret = -ENOMEM;
  362. goto debugfs_exit;
  363. }
  364. INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
  365. ret = iwm_if_add(iwm);
  366. if (ret) {
  367. dev_err(dev, "add SDIO interface failed\n");
  368. goto destroy_wq;
  369. }
  370. dev_info(dev, "IWM SDIO probe\n");
  371. return 0;
  372. destroy_wq:
  373. destroy_workqueue(hw->isr_wq);
  374. debugfs_exit:
  375. iwm_debugfs_exit(iwm);
  376. iwm_if_free(iwm);
  377. return ret;
  378. }
  379. static void iwm_sdio_remove(struct sdio_func *func)
  380. {
  381. struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
  382. struct iwm_priv *iwm = hw_to_iwm(hw);
  383. struct device *dev = &func->dev;
  384. iwm_if_remove(iwm);
  385. destroy_workqueue(hw->isr_wq);
  386. iwm_debugfs_exit(iwm);
  387. iwm_if_free(iwm);
  388. sdio_set_drvdata(func, NULL);
  389. dev_info(dev, "IWM SDIO remove\n");
  390. }
  391. static const struct sdio_device_id iwm_sdio_ids[] = {
  392. /* Global/AGN SKU */
  393. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1403) },
  394. /* BGN SKU */
  395. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1408) },
  396. { /* end: all zeroes */ },
  397. };
  398. MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
  399. static struct sdio_driver iwm_sdio_driver = {
  400. .name = "iwm_sdio",
  401. .id_table = iwm_sdio_ids,
  402. .probe = iwm_sdio_probe,
  403. .remove = iwm_sdio_remove,
  404. };
  405. static int __init iwm_sdio_init_module(void)
  406. {
  407. return sdio_register_driver(&iwm_sdio_driver);
  408. }
  409. static void __exit iwm_sdio_exit_module(void)
  410. {
  411. sdio_unregister_driver(&iwm_sdio_driver);
  412. }
  413. module_init(iwm_sdio_init_module);
  414. module_exit(iwm_sdio_exit_module);
  415. MODULE_LICENSE("GPL");
  416. MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);