sdio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/vmalloc.h>
  26. #include <linux/mmc/sdio_func.h>
  27. #include <linux/mmc/sdio_ids.h>
  28. #include <linux/mmc/card.h>
  29. #include <linux/mmc/host.h>
  30. #include <linux/gpio.h>
  31. #include <linux/wl12xx.h>
  32. #include <linux/pm_runtime.h>
  33. #include "wl12xx.h"
  34. #include "wl12xx_80211.h"
  35. #include "io.h"
  36. #ifndef SDIO_VENDOR_ID_TI
  37. #define SDIO_VENDOR_ID_TI 0x0097
  38. #endif
  39. #ifndef SDIO_DEVICE_ID_TI_WL1271
  40. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  41. #endif
  42. static const struct sdio_device_id wl1271_devices[] __devinitconst = {
  43. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  44. {}
  45. };
  46. MODULE_DEVICE_TABLE(sdio, wl1271_devices);
  47. static void wl1271_sdio_set_block_size(struct wl1271 *wl, unsigned int blksz)
  48. {
  49. sdio_claim_host(wl->if_priv);
  50. sdio_set_block_size(wl->if_priv, blksz);
  51. sdio_release_host(wl->if_priv);
  52. }
  53. static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
  54. {
  55. return wl->if_priv;
  56. }
  57. static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
  58. {
  59. return &(wl_to_func(wl)->dev);
  60. }
  61. static irqreturn_t wl1271_hardirq(int irq, void *cookie)
  62. {
  63. struct wl1271 *wl = cookie;
  64. unsigned long flags;
  65. wl1271_debug(DEBUG_IRQ, "IRQ");
  66. /* complete the ELP completion */
  67. spin_lock_irqsave(&wl->wl_lock, flags);
  68. set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
  69. if (wl->elp_compl) {
  70. complete(wl->elp_compl);
  71. wl->elp_compl = NULL;
  72. }
  73. if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
  74. /* don't enqueue a work right now. mark it as pending */
  75. set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
  76. wl1271_debug(DEBUG_IRQ, "should not enqueue work");
  77. disable_irq_nosync(wl->irq);
  78. pm_wakeup_event(wl1271_sdio_wl_to_dev(wl), 0);
  79. spin_unlock_irqrestore(&wl->wl_lock, flags);
  80. return IRQ_HANDLED;
  81. }
  82. spin_unlock_irqrestore(&wl->wl_lock, flags);
  83. return IRQ_WAKE_THREAD;
  84. }
  85. static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
  86. {
  87. disable_irq(wl->irq);
  88. }
  89. static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
  90. {
  91. enable_irq(wl->irq);
  92. }
  93. static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
  94. size_t len, bool fixed)
  95. {
  96. int ret;
  97. struct sdio_func *func = wl_to_func(wl);
  98. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  99. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  100. wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
  101. addr, ((u8 *)buf)[0]);
  102. } else {
  103. if (fixed)
  104. ret = sdio_readsb(func, buf, addr, len);
  105. else
  106. ret = sdio_memcpy_fromio(func, buf, addr, len);
  107. wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
  108. addr, len);
  109. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  110. }
  111. if (ret)
  112. wl1271_error("sdio read failed (%d)", ret);
  113. }
  114. static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
  115. size_t len, bool fixed)
  116. {
  117. int ret;
  118. struct sdio_func *func = wl_to_func(wl);
  119. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  120. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  121. wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
  122. addr, ((u8 *)buf)[0]);
  123. } else {
  124. wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
  125. addr, len);
  126. wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
  127. if (fixed)
  128. ret = sdio_writesb(func, addr, buf, len);
  129. else
  130. ret = sdio_memcpy_toio(func, addr, buf, len);
  131. }
  132. if (ret)
  133. wl1271_error("sdio write failed (%d)", ret);
  134. }
  135. static int wl1271_sdio_power_on(struct wl1271 *wl)
  136. {
  137. struct sdio_func *func = wl_to_func(wl);
  138. int ret;
  139. /* If enabled, tell runtime PM not to power off the card */
  140. if (pm_runtime_enabled(&func->dev)) {
  141. ret = pm_runtime_get_sync(&func->dev);
  142. if (ret)
  143. goto out;
  144. }
  145. /* Runtime PM might be disabled, so power up the card manually */
  146. ret = mmc_power_restore_host(func->card->host);
  147. if (ret < 0)
  148. goto out;
  149. sdio_claim_host(func);
  150. sdio_enable_func(func);
  151. out:
  152. return ret;
  153. }
  154. static int wl1271_sdio_power_off(struct wl1271 *wl)
  155. {
  156. struct sdio_func *func = wl_to_func(wl);
  157. int ret;
  158. sdio_disable_func(func);
  159. sdio_release_host(func);
  160. /* Runtime PM might be disabled, so power off the card manually */
  161. ret = mmc_power_save_host(func->card->host);
  162. if (ret < 0)
  163. return ret;
  164. /* If enabled, let runtime PM know the card is powered off */
  165. if (pm_runtime_enabled(&func->dev))
  166. ret = pm_runtime_put_sync(&func->dev);
  167. return ret;
  168. }
  169. static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
  170. {
  171. if (enable)
  172. return wl1271_sdio_power_on(wl);
  173. else
  174. return wl1271_sdio_power_off(wl);
  175. }
  176. static struct wl1271_if_operations sdio_ops = {
  177. .read = wl1271_sdio_raw_read,
  178. .write = wl1271_sdio_raw_write,
  179. .power = wl1271_sdio_set_power,
  180. .dev = wl1271_sdio_wl_to_dev,
  181. .enable_irq = wl1271_sdio_enable_interrupts,
  182. .disable_irq = wl1271_sdio_disable_interrupts,
  183. .set_block_size = wl1271_sdio_set_block_size,
  184. };
  185. static int __devinit wl1271_probe(struct sdio_func *func,
  186. const struct sdio_device_id *id)
  187. {
  188. struct ieee80211_hw *hw;
  189. const struct wl12xx_platform_data *wlan_data;
  190. struct wl1271 *wl;
  191. unsigned long irqflags;
  192. mmc_pm_flag_t mmcflags;
  193. int ret;
  194. /* We are only able to handle the wlan function */
  195. if (func->num != 0x02)
  196. return -ENODEV;
  197. hw = wl1271_alloc_hw();
  198. if (IS_ERR(hw))
  199. return PTR_ERR(hw);
  200. wl = hw->priv;
  201. wl->if_priv = func;
  202. wl->if_ops = &sdio_ops;
  203. /* Grab access to FN0 for ELP reg. */
  204. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  205. /* Use block mode for transferring over one block size of data */
  206. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  207. wlan_data = wl12xx_get_platform_data();
  208. if (IS_ERR(wlan_data)) {
  209. ret = PTR_ERR(wlan_data);
  210. wl1271_error("missing wlan platform data: %d", ret);
  211. goto out_free;
  212. }
  213. wl->irq = wlan_data->irq;
  214. wl->ref_clock = wlan_data->board_ref_clock;
  215. wl->tcxo_clock = wlan_data->board_tcxo_clock;
  216. wl->platform_quirks = wlan_data->platform_quirks;
  217. if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
  218. irqflags = IRQF_TRIGGER_RISING;
  219. else
  220. irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
  221. ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq,
  222. irqflags,
  223. DRIVER_NAME, wl);
  224. if (ret < 0) {
  225. wl1271_error("request_irq() failed: %d", ret);
  226. goto out_free;
  227. }
  228. enable_irq_wake(wl->irq);
  229. device_init_wakeup(wl1271_sdio_wl_to_dev(wl), 1);
  230. disable_irq(wl->irq);
  231. /* if sdio can keep power while host is suspended, enable wow */
  232. mmcflags = sdio_get_host_pm_caps(func);
  233. wl1271_debug(DEBUG_SDIO, "sdio PM caps = 0x%x", mmcflags);
  234. if (mmcflags & MMC_PM_KEEP_POWER)
  235. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
  236. ret = wl1271_init_ieee80211(wl);
  237. if (ret)
  238. goto out_irq;
  239. ret = wl1271_register_hw(wl);
  240. if (ret)
  241. goto out_irq;
  242. sdio_set_drvdata(func, wl);
  243. /* Tell PM core that we don't need the card to be powered now */
  244. pm_runtime_put_noidle(&func->dev);
  245. return 0;
  246. out_irq:
  247. free_irq(wl->irq, wl);
  248. out_free:
  249. wl1271_free_hw(wl);
  250. return ret;
  251. }
  252. static void __devexit wl1271_remove(struct sdio_func *func)
  253. {
  254. struct wl1271 *wl = sdio_get_drvdata(func);
  255. /* Undo decrement done above in wl1271_probe */
  256. pm_runtime_get_noresume(&func->dev);
  257. wl1271_unregister_hw(wl);
  258. device_init_wakeup(wl1271_sdio_wl_to_dev(wl), 0);
  259. disable_irq_wake(wl->irq);
  260. free_irq(wl->irq, wl);
  261. wl1271_free_hw(wl);
  262. }
  263. #ifdef CONFIG_PM
  264. static int wl1271_suspend(struct device *dev)
  265. {
  266. /* Tell MMC/SDIO core it's OK to power down the card
  267. * (if it isn't already), but not to remove it completely */
  268. struct sdio_func *func = dev_to_sdio_func(dev);
  269. struct wl1271 *wl = sdio_get_drvdata(func);
  270. mmc_pm_flag_t sdio_flags;
  271. int ret = 0;
  272. wl1271_debug(DEBUG_MAC80211, "wl1271 suspend. wow_enabled: %d",
  273. wl->wow_enabled);
  274. /* check whether sdio should keep power */
  275. if (wl->wow_enabled) {
  276. sdio_flags = sdio_get_host_pm_caps(func);
  277. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  278. wl1271_error("can't keep power while host "
  279. "is suspended");
  280. ret = -EINVAL;
  281. goto out;
  282. }
  283. /* keep power while host suspended */
  284. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  285. if (ret) {
  286. wl1271_error("error while trying to keep power");
  287. goto out;
  288. }
  289. /* release host */
  290. sdio_release_host(func);
  291. }
  292. out:
  293. return ret;
  294. }
  295. static int wl1271_resume(struct device *dev)
  296. {
  297. struct sdio_func *func = dev_to_sdio_func(dev);
  298. struct wl1271 *wl = sdio_get_drvdata(func);
  299. wl1271_debug(DEBUG_MAC80211, "wl1271 resume");
  300. if (wl->wow_enabled) {
  301. /* claim back host */
  302. sdio_claim_host(func);
  303. }
  304. return 0;
  305. }
  306. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  307. .suspend = wl1271_suspend,
  308. .resume = wl1271_resume,
  309. };
  310. #endif
  311. static struct sdio_driver wl1271_sdio_driver = {
  312. .name = "wl1271_sdio",
  313. .id_table = wl1271_devices,
  314. .probe = wl1271_probe,
  315. .remove = __devexit_p(wl1271_remove),
  316. #ifdef CONFIG_PM
  317. .drv = {
  318. .pm = &wl1271_sdio_pm_ops,
  319. },
  320. #endif
  321. };
  322. static int __init wl1271_init(void)
  323. {
  324. return sdio_register_driver(&wl1271_sdio_driver);
  325. }
  326. static void __exit wl1271_exit(void)
  327. {
  328. sdio_unregister_driver(&wl1271_sdio_driver);
  329. }
  330. module_init(wl1271_init);
  331. module_exit(wl1271_exit);
  332. MODULE_LICENSE("GPL");
  333. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  334. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  335. MODULE_FIRMWARE(WL1271_FW_NAME);
  336. MODULE_FIRMWARE(WL128X_FW_NAME);
  337. MODULE_FIRMWARE(WL127X_AP_FW_NAME);
  338. MODULE_FIRMWARE(WL128X_AP_FW_NAME);