sdio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * wl12xx SDIO routines
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. *
  18. * Copyright (C) 2005 Texas Instruments Incorporated
  19. * Copyright (C) 2008 Google Inc
  20. * Copyright (C) 2009 Bob Copeland (me@bobcopeland.com)
  21. */
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/mod_devicetable.h>
  25. #include <linux/mmc/sdio_func.h>
  26. #include <linux/mmc/sdio_ids.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/wl12xx.h>
  29. #include <linux/irq.h>
  30. #include <linux/pm_runtime.h>
  31. #include "wl1251.h"
  32. #ifndef SDIO_VENDOR_ID_TI
  33. #define SDIO_VENDOR_ID_TI 0x104c
  34. #endif
  35. #ifndef SDIO_DEVICE_ID_TI_WL1251
  36. #define SDIO_DEVICE_ID_TI_WL1251 0x9066
  37. #endif
  38. struct wl1251_sdio {
  39. struct sdio_func *func;
  40. u32 elp_val;
  41. };
  42. static struct sdio_func *wl_to_func(struct wl1251 *wl)
  43. {
  44. struct wl1251_sdio *wl_sdio = wl->if_priv;
  45. return wl_sdio->func;
  46. }
  47. static void wl1251_sdio_interrupt(struct sdio_func *func)
  48. {
  49. struct wl1251 *wl = sdio_get_drvdata(func);
  50. wl1251_debug(DEBUG_IRQ, "IRQ");
  51. /* FIXME should be synchronous for sdio */
  52. ieee80211_queue_work(wl->hw, &wl->irq_work);
  53. }
  54. static const struct sdio_device_id wl1251_devices[] = {
  55. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1251) },
  56. {}
  57. };
  58. MODULE_DEVICE_TABLE(sdio, wl1251_devices);
  59. static void wl1251_sdio_read(struct wl1251 *wl, int addr,
  60. void *buf, size_t len)
  61. {
  62. int ret;
  63. struct sdio_func *func = wl_to_func(wl);
  64. sdio_claim_host(func);
  65. ret = sdio_memcpy_fromio(func, buf, addr, len);
  66. if (ret)
  67. wl1251_error("sdio read failed (%d)", ret);
  68. sdio_release_host(func);
  69. }
  70. static void wl1251_sdio_write(struct wl1251 *wl, int addr,
  71. void *buf, size_t len)
  72. {
  73. int ret;
  74. struct sdio_func *func = wl_to_func(wl);
  75. sdio_claim_host(func);
  76. ret = sdio_memcpy_toio(func, addr, buf, len);
  77. if (ret)
  78. wl1251_error("sdio write failed (%d)", ret);
  79. sdio_release_host(func);
  80. }
  81. static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
  82. {
  83. int ret = 0;
  84. struct wl1251_sdio *wl_sdio = wl->if_priv;
  85. struct sdio_func *func = wl_sdio->func;
  86. /*
  87. * The hardware only supports RAW (read after write) access for
  88. * reading, regular sdio_readb won't work here (it interprets
  89. * the unused bits of CMD52 as write data even if we send read
  90. * request).
  91. */
  92. sdio_claim_host(func);
  93. *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
  94. sdio_release_host(func);
  95. if (ret)
  96. wl1251_error("sdio_readb failed (%d)", ret);
  97. }
  98. static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
  99. {
  100. int ret = 0;
  101. struct wl1251_sdio *wl_sdio = wl->if_priv;
  102. struct sdio_func *func = wl_sdio->func;
  103. sdio_claim_host(func);
  104. sdio_writeb(func, val, addr, &ret);
  105. sdio_release_host(func);
  106. if (ret)
  107. wl1251_error("sdio_writeb failed (%d)", ret);
  108. else
  109. wl_sdio->elp_val = val;
  110. }
  111. static void wl1251_sdio_reset(struct wl1251 *wl)
  112. {
  113. }
  114. static void wl1251_sdio_enable_irq(struct wl1251 *wl)
  115. {
  116. struct sdio_func *func = wl_to_func(wl);
  117. sdio_claim_host(func);
  118. sdio_claim_irq(func, wl1251_sdio_interrupt);
  119. sdio_release_host(func);
  120. }
  121. static void wl1251_sdio_disable_irq(struct wl1251 *wl)
  122. {
  123. struct sdio_func *func = wl_to_func(wl);
  124. sdio_claim_host(func);
  125. sdio_release_irq(func);
  126. sdio_release_host(func);
  127. }
  128. /* Interrupts when using dedicated WLAN_IRQ pin */
  129. static irqreturn_t wl1251_line_irq(int irq, void *cookie)
  130. {
  131. struct wl1251 *wl = cookie;
  132. ieee80211_queue_work(wl->hw, &wl->irq_work);
  133. return IRQ_HANDLED;
  134. }
  135. static void wl1251_enable_line_irq(struct wl1251 *wl)
  136. {
  137. return enable_irq(wl->irq);
  138. }
  139. static void wl1251_disable_line_irq(struct wl1251 *wl)
  140. {
  141. return disable_irq(wl->irq);
  142. }
  143. static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
  144. {
  145. struct sdio_func *func = wl_to_func(wl);
  146. int ret;
  147. if (enable) {
  148. /*
  149. * Power is controlled by runtime PM, but we still call board
  150. * callback in case it wants to do any additional setup,
  151. * for example enabling clock buffer for the module.
  152. */
  153. if (wl->set_power)
  154. wl->set_power(true);
  155. ret = pm_runtime_get_sync(&func->dev);
  156. if (ret < 0)
  157. goto out;
  158. sdio_claim_host(func);
  159. sdio_enable_func(func);
  160. sdio_release_host(func);
  161. } else {
  162. sdio_claim_host(func);
  163. sdio_disable_func(func);
  164. sdio_release_host(func);
  165. ret = pm_runtime_put_sync(&func->dev);
  166. if (ret < 0)
  167. goto out;
  168. if (wl->set_power)
  169. wl->set_power(false);
  170. }
  171. out:
  172. return ret;
  173. }
  174. static struct wl1251_if_operations wl1251_sdio_ops = {
  175. .read = wl1251_sdio_read,
  176. .write = wl1251_sdio_write,
  177. .write_elp = wl1251_sdio_write_elp,
  178. .read_elp = wl1251_sdio_read_elp,
  179. .reset = wl1251_sdio_reset,
  180. .power = wl1251_sdio_set_power,
  181. };
  182. static int wl1251_sdio_probe(struct sdio_func *func,
  183. const struct sdio_device_id *id)
  184. {
  185. int ret;
  186. struct wl1251 *wl;
  187. struct ieee80211_hw *hw;
  188. struct wl1251_sdio *wl_sdio;
  189. const struct wl12xx_platform_data *wl12xx_board_data;
  190. hw = wl1251_alloc_hw();
  191. if (IS_ERR(hw))
  192. return PTR_ERR(hw);
  193. wl = hw->priv;
  194. wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
  195. if (wl_sdio == NULL) {
  196. ret = -ENOMEM;
  197. goto out_free_hw;
  198. }
  199. sdio_claim_host(func);
  200. ret = sdio_enable_func(func);
  201. if (ret)
  202. goto release;
  203. sdio_set_block_size(func, 512);
  204. sdio_release_host(func);
  205. SET_IEEE80211_DEV(hw, &func->dev);
  206. wl_sdio->func = func;
  207. wl->if_priv = wl_sdio;
  208. wl->if_ops = &wl1251_sdio_ops;
  209. wl12xx_board_data = wl12xx_get_platform_data();
  210. if (!IS_ERR(wl12xx_board_data)) {
  211. wl->set_power = wl12xx_board_data->set_power;
  212. wl->irq = wl12xx_board_data->irq;
  213. wl->use_eeprom = wl12xx_board_data->use_eeprom;
  214. }
  215. if (wl->irq) {
  216. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  217. ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
  218. if (ret < 0) {
  219. wl1251_error("request_irq() failed: %d", ret);
  220. goto disable;
  221. }
  222. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  223. wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
  224. wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
  225. wl1251_info("using dedicated interrupt line");
  226. } else {
  227. wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
  228. wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
  229. wl1251_info("using SDIO interrupt");
  230. }
  231. ret = wl1251_init_ieee80211(wl);
  232. if (ret)
  233. goto out_free_irq;
  234. sdio_set_drvdata(func, wl);
  235. /* Tell PM core that we don't need the card to be powered now */
  236. pm_runtime_put_noidle(&func->dev);
  237. return ret;
  238. out_free_irq:
  239. if (wl->irq)
  240. free_irq(wl->irq, wl);
  241. disable:
  242. sdio_claim_host(func);
  243. sdio_disable_func(func);
  244. release:
  245. sdio_release_host(func);
  246. kfree(wl_sdio);
  247. out_free_hw:
  248. wl1251_free_hw(wl);
  249. return ret;
  250. }
  251. static void __devexit wl1251_sdio_remove(struct sdio_func *func)
  252. {
  253. struct wl1251 *wl = sdio_get_drvdata(func);
  254. struct wl1251_sdio *wl_sdio = wl->if_priv;
  255. /* Undo decrement done above in wl1251_probe */
  256. pm_runtime_get_noresume(&func->dev);
  257. if (wl->irq)
  258. free_irq(wl->irq, wl);
  259. wl1251_free_hw(wl);
  260. kfree(wl_sdio);
  261. sdio_claim_host(func);
  262. sdio_release_irq(func);
  263. sdio_disable_func(func);
  264. sdio_release_host(func);
  265. }
  266. static int wl1251_suspend(struct device *dev)
  267. {
  268. /*
  269. * Tell MMC/SDIO core it's OK to power down the card
  270. * (if it isn't already), but not to remove it completely.
  271. */
  272. return 0;
  273. }
  274. static int wl1251_resume(struct device *dev)
  275. {
  276. return 0;
  277. }
  278. static const struct dev_pm_ops wl1251_sdio_pm_ops = {
  279. .suspend = wl1251_suspend,
  280. .resume = wl1251_resume,
  281. };
  282. static struct sdio_driver wl1251_sdio_driver = {
  283. .name = "wl1251_sdio",
  284. .id_table = wl1251_devices,
  285. .probe = wl1251_sdio_probe,
  286. .remove = __devexit_p(wl1251_sdio_remove),
  287. .drv.pm = &wl1251_sdio_pm_ops,
  288. };
  289. static int __init wl1251_sdio_init(void)
  290. {
  291. int err;
  292. err = sdio_register_driver(&wl1251_sdio_driver);
  293. if (err)
  294. wl1251_error("failed to register sdio driver: %d", err);
  295. return err;
  296. }
  297. static void __exit wl1251_sdio_exit(void)
  298. {
  299. sdio_unregister_driver(&wl1251_sdio_driver);
  300. wl1251_notice("unloaded");
  301. }
  302. module_init(wl1251_sdio_init);
  303. module_exit(wl1251_sdio_exit);
  304. MODULE_LICENSE("GPL");
  305. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");