sdio.c 12 KB

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