sdio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/irq.h>
  24. #include <linux/module.h>
  25. #include <linux/crc7.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mmc/sdio_func.h>
  28. #include <linux/mmc/sdio_ids.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/gpio.h>
  32. #include <linux/wl12xx.h>
  33. #include <linux/pm_runtime.h>
  34. #include "wl12xx.h"
  35. #include "wl12xx_80211.h"
  36. #include "io.h"
  37. #ifndef SDIO_VENDOR_ID_TI
  38. #define SDIO_VENDOR_ID_TI 0x0097
  39. #endif
  40. #ifndef SDIO_DEVICE_ID_TI_WL1271
  41. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  42. #endif
  43. static const struct sdio_device_id wl1271_devices[] = {
  44. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  45. {}
  46. };
  47. MODULE_DEVICE_TABLE(sdio, wl1271_devices);
  48. static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
  49. {
  50. return wl->if_priv;
  51. }
  52. static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
  53. {
  54. return &(wl_to_func(wl)->dev);
  55. }
  56. static irqreturn_t wl1271_hardirq(int irq, void *cookie)
  57. {
  58. struct wl1271 *wl = cookie;
  59. unsigned long flags;
  60. wl1271_debug(DEBUG_IRQ, "IRQ");
  61. /* complete the ELP completion */
  62. spin_lock_irqsave(&wl->wl_lock, flags);
  63. set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
  64. if (wl->elp_compl) {
  65. complete(wl->elp_compl);
  66. wl->elp_compl = NULL;
  67. }
  68. spin_unlock_irqrestore(&wl->wl_lock, flags);
  69. return IRQ_WAKE_THREAD;
  70. }
  71. static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
  72. {
  73. disable_irq(wl->irq);
  74. }
  75. static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
  76. {
  77. enable_irq(wl->irq);
  78. }
  79. static void wl1271_sdio_reset(struct wl1271 *wl)
  80. {
  81. }
  82. static void wl1271_sdio_init(struct wl1271 *wl)
  83. {
  84. }
  85. static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
  86. size_t len, bool fixed)
  87. {
  88. int ret;
  89. struct sdio_func *func = wl_to_func(wl);
  90. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  91. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  92. wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
  93. addr, ((u8 *)buf)[0]);
  94. } else {
  95. if (fixed)
  96. ret = sdio_readsb(func, buf, addr, len);
  97. else
  98. ret = sdio_memcpy_fromio(func, buf, addr, len);
  99. wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
  100. addr, len);
  101. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  102. }
  103. if (ret)
  104. wl1271_error("sdio read failed (%d)", ret);
  105. }
  106. static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
  107. size_t len, bool fixed)
  108. {
  109. int ret;
  110. struct sdio_func *func = wl_to_func(wl);
  111. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  112. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  113. wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
  114. addr, ((u8 *)buf)[0]);
  115. } else {
  116. wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
  117. addr, len);
  118. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  119. if (fixed)
  120. ret = sdio_writesb(func, addr, buf, len);
  121. else
  122. ret = sdio_memcpy_toio(func, addr, buf, len);
  123. }
  124. if (ret)
  125. wl1271_error("sdio write failed (%d)", ret);
  126. }
  127. static int wl1271_sdio_power_on(struct wl1271 *wl)
  128. {
  129. struct sdio_func *func = wl_to_func(wl);
  130. int ret;
  131. /* Make sure the card will not be powered off by runtime PM */
  132. ret = pm_runtime_get_sync(&func->dev);
  133. if (ret < 0)
  134. goto out;
  135. /* Runtime PM might be disabled, so power up the card manually */
  136. ret = mmc_power_restore_host(func->card->host);
  137. if (ret < 0)
  138. goto out;
  139. sdio_claim_host(func);
  140. sdio_enable_func(func);
  141. out:
  142. return ret;
  143. }
  144. static int wl1271_sdio_power_off(struct wl1271 *wl)
  145. {
  146. struct sdio_func *func = wl_to_func(wl);
  147. int ret;
  148. sdio_disable_func(func);
  149. sdio_release_host(func);
  150. /* Runtime PM might be disabled, so power off the card manually */
  151. ret = mmc_power_save_host(func->card->host);
  152. if (ret < 0)
  153. return ret;
  154. /* Let runtime PM know the card is powered off */
  155. return pm_runtime_put_sync(&func->dev);
  156. }
  157. static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
  158. {
  159. if (enable)
  160. return wl1271_sdio_power_on(wl);
  161. else
  162. return wl1271_sdio_power_off(wl);
  163. }
  164. static struct wl1271_if_operations sdio_ops = {
  165. .read = wl1271_sdio_raw_read,
  166. .write = wl1271_sdio_raw_write,
  167. .reset = wl1271_sdio_reset,
  168. .init = wl1271_sdio_init,
  169. .power = wl1271_sdio_set_power,
  170. .dev = wl1271_sdio_wl_to_dev,
  171. .enable_irq = wl1271_sdio_enable_interrupts,
  172. .disable_irq = wl1271_sdio_disable_interrupts
  173. };
  174. static int __devinit wl1271_probe(struct sdio_func *func,
  175. const struct sdio_device_id *id)
  176. {
  177. struct ieee80211_hw *hw;
  178. const struct wl12xx_platform_data *wlan_data;
  179. struct wl1271 *wl;
  180. int ret;
  181. /* We are only able to handle the wlan function */
  182. if (func->num != 0x02)
  183. return -ENODEV;
  184. hw = wl1271_alloc_hw();
  185. if (IS_ERR(hw))
  186. return PTR_ERR(hw);
  187. wl = hw->priv;
  188. wl->if_priv = func;
  189. wl->if_ops = &sdio_ops;
  190. /* Grab access to FN0 for ELP reg. */
  191. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  192. wlan_data = wl12xx_get_platform_data();
  193. if (IS_ERR(wlan_data)) {
  194. ret = PTR_ERR(wlan_data);
  195. wl1271_error("missing wlan platform data: %d", ret);
  196. goto out_free;
  197. }
  198. wl->irq = wlan_data->irq;
  199. wl->ref_clock = wlan_data->board_ref_clock;
  200. ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq,
  201. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  202. DRIVER_NAME, wl);
  203. if (ret < 0) {
  204. wl1271_error("request_irq() failed: %d", ret);
  205. goto out_free;
  206. }
  207. disable_irq(wl->irq);
  208. ret = wl1271_init_ieee80211(wl);
  209. if (ret)
  210. goto out_irq;
  211. ret = wl1271_register_hw(wl);
  212. if (ret)
  213. goto out_irq;
  214. sdio_set_drvdata(func, wl);
  215. /* Tell PM core that we don't need the card to be powered now */
  216. pm_runtime_put_noidle(&func->dev);
  217. wl1271_notice("initialized");
  218. return 0;
  219. out_irq:
  220. free_irq(wl->irq, wl);
  221. out_free:
  222. wl1271_free_hw(wl);
  223. return ret;
  224. }
  225. static void __devexit wl1271_remove(struct sdio_func *func)
  226. {
  227. struct wl1271 *wl = sdio_get_drvdata(func);
  228. /* Undo decrement done above in wl1271_probe */
  229. pm_runtime_get_noresume(&func->dev);
  230. wl1271_unregister_hw(wl);
  231. free_irq(wl->irq, wl);
  232. wl1271_free_hw(wl);
  233. }
  234. static int wl1271_suspend(struct device *dev)
  235. {
  236. /* Tell MMC/SDIO core it's OK to power down the card
  237. * (if it isn't already), but not to remove it completely */
  238. return 0;
  239. }
  240. static int wl1271_resume(struct device *dev)
  241. {
  242. return 0;
  243. }
  244. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  245. .suspend = wl1271_suspend,
  246. .resume = wl1271_resume,
  247. };
  248. static struct sdio_driver wl1271_sdio_driver = {
  249. .name = "wl1271_sdio",
  250. .id_table = wl1271_devices,
  251. .probe = wl1271_probe,
  252. .remove = __devexit_p(wl1271_remove),
  253. .drv = {
  254. .pm = &wl1271_sdio_pm_ops,
  255. },
  256. };
  257. static int __init wl1271_init(void)
  258. {
  259. int ret;
  260. ret = sdio_register_driver(&wl1271_sdio_driver);
  261. if (ret < 0) {
  262. wl1271_error("failed to register sdio driver: %d", ret);
  263. goto out;
  264. }
  265. out:
  266. return ret;
  267. }
  268. static void __exit wl1271_exit(void)
  269. {
  270. sdio_unregister_driver(&wl1271_sdio_driver);
  271. wl1271_notice("unloaded");
  272. }
  273. module_init(wl1271_init);
  274. module_exit(wl1271_exit);
  275. MODULE_LICENSE("GPL");
  276. MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
  277. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  278. MODULE_FIRMWARE(WL1271_FW_NAME);
  279. MODULE_FIRMWARE(WL1271_AP_FW_NAME);