sdio.c 13 KB

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