sdio.c 9.8 KB

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