sdio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * wl12xx SDIO routines
  4. *
  5. * Copyright (C) 2005 Texas Instruments Incorporated
  6. * Copyright (C) 2008 Google Inc
  7. * Copyright (C) 2009 Bob Copeland ([email protected])
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/mmc/sdio_func.h>
  13. #include <linux/mmc/sdio_ids.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/wl12xx.h>
  16. #include <linux/irq.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include "wl1251.h"
  21. struct wl1251_sdio {
  22. struct sdio_func *func;
  23. u32 elp_val;
  24. };
  25. static struct sdio_func *wl_to_func(struct wl1251 *wl)
  26. {
  27. struct wl1251_sdio *wl_sdio = wl->if_priv;
  28. return wl_sdio->func;
  29. }
  30. static void wl1251_sdio_interrupt(struct sdio_func *func)
  31. {
  32. struct wl1251 *wl = sdio_get_drvdata(func);
  33. wl1251_debug(DEBUG_IRQ, "IRQ");
  34. /* FIXME should be synchronous for sdio */
  35. ieee80211_queue_work(wl->hw, &wl->irq_work);
  36. }
  37. static const struct sdio_device_id wl1251_devices[] = {
  38. { SDIO_DEVICE(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251) },
  39. {}
  40. };
  41. MODULE_DEVICE_TABLE(sdio, wl1251_devices);
  42. static void wl1251_sdio_read(struct wl1251 *wl, int addr,
  43. void *buf, size_t len)
  44. {
  45. int ret;
  46. struct sdio_func *func = wl_to_func(wl);
  47. sdio_claim_host(func);
  48. ret = sdio_memcpy_fromio(func, buf, addr, len);
  49. if (ret)
  50. wl1251_error("sdio read failed (%d)", ret);
  51. sdio_release_host(func);
  52. }
  53. static void wl1251_sdio_write(struct wl1251 *wl, int addr,
  54. void *buf, size_t len)
  55. {
  56. int ret;
  57. struct sdio_func *func = wl_to_func(wl);
  58. sdio_claim_host(func);
  59. ret = sdio_memcpy_toio(func, addr, buf, len);
  60. if (ret)
  61. wl1251_error("sdio write failed (%d)", ret);
  62. sdio_release_host(func);
  63. }
  64. static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
  65. {
  66. int ret = 0;
  67. struct wl1251_sdio *wl_sdio = wl->if_priv;
  68. struct sdio_func *func = wl_sdio->func;
  69. /*
  70. * The hardware only supports RAW (read after write) access for
  71. * reading, regular sdio_readb won't work here (it interprets
  72. * the unused bits of CMD52 as write data even if we send read
  73. * request).
  74. */
  75. sdio_claim_host(func);
  76. *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
  77. sdio_release_host(func);
  78. if (ret)
  79. wl1251_error("sdio_readb failed (%d)", ret);
  80. }
  81. static void wl1251_sdio_write_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. sdio_claim_host(func);
  87. sdio_writeb(func, val, addr, &ret);
  88. sdio_release_host(func);
  89. if (ret)
  90. wl1251_error("sdio_writeb failed (%d)", ret);
  91. else
  92. wl_sdio->elp_val = val;
  93. }
  94. static void wl1251_sdio_reset(struct wl1251 *wl)
  95. {
  96. }
  97. static void wl1251_sdio_enable_irq(struct wl1251 *wl)
  98. {
  99. struct sdio_func *func = wl_to_func(wl);
  100. sdio_claim_host(func);
  101. sdio_claim_irq(func, wl1251_sdio_interrupt);
  102. sdio_release_host(func);
  103. }
  104. static void wl1251_sdio_disable_irq(struct wl1251 *wl)
  105. {
  106. struct sdio_func *func = wl_to_func(wl);
  107. sdio_claim_host(func);
  108. sdio_release_irq(func);
  109. sdio_release_host(func);
  110. }
  111. /* Interrupts when using dedicated WLAN_IRQ pin */
  112. static irqreturn_t wl1251_line_irq(int irq, void *cookie)
  113. {
  114. struct wl1251 *wl = cookie;
  115. ieee80211_queue_work(wl->hw, &wl->irq_work);
  116. return IRQ_HANDLED;
  117. }
  118. static void wl1251_enable_line_irq(struct wl1251 *wl)
  119. {
  120. return enable_irq(wl->irq);
  121. }
  122. static void wl1251_disable_line_irq(struct wl1251 *wl)
  123. {
  124. return disable_irq(wl->irq);
  125. }
  126. static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
  127. {
  128. struct sdio_func *func = wl_to_func(wl);
  129. int ret;
  130. if (enable) {
  131. ret = pm_runtime_get_sync(&func->dev);
  132. if (ret < 0) {
  133. pm_runtime_put_sync(&func->dev);
  134. goto out;
  135. }
  136. sdio_claim_host(func);
  137. sdio_enable_func(func);
  138. sdio_release_host(func);
  139. } else {
  140. sdio_claim_host(func);
  141. sdio_disable_func(func);
  142. sdio_release_host(func);
  143. ret = pm_runtime_put_sync(&func->dev);
  144. if (ret < 0)
  145. goto out;
  146. }
  147. out:
  148. return ret;
  149. }
  150. static struct wl1251_if_operations wl1251_sdio_ops = {
  151. .read = wl1251_sdio_read,
  152. .write = wl1251_sdio_write,
  153. .write_elp = wl1251_sdio_write_elp,
  154. .read_elp = wl1251_sdio_read_elp,
  155. .reset = wl1251_sdio_reset,
  156. .power = wl1251_sdio_set_power,
  157. };
  158. static int wl1251_sdio_probe(struct sdio_func *func,
  159. const struct sdio_device_id *id)
  160. {
  161. int ret;
  162. struct wl1251 *wl;
  163. struct ieee80211_hw *hw;
  164. struct wl1251_sdio *wl_sdio;
  165. const struct wl1251_platform_data *wl1251_board_data;
  166. struct device_node *np = func->dev.of_node;
  167. hw = wl1251_alloc_hw();
  168. if (IS_ERR(hw))
  169. return PTR_ERR(hw);
  170. wl = hw->priv;
  171. wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
  172. if (wl_sdio == NULL) {
  173. ret = -ENOMEM;
  174. goto out_free_hw;
  175. }
  176. sdio_claim_host(func);
  177. ret = sdio_enable_func(func);
  178. if (ret)
  179. goto release;
  180. sdio_set_block_size(func, 512);
  181. sdio_release_host(func);
  182. SET_IEEE80211_DEV(hw, &func->dev);
  183. wl_sdio->func = func;
  184. wl->if_priv = wl_sdio;
  185. wl->if_ops = &wl1251_sdio_ops;
  186. wl1251_board_data = wl1251_get_platform_data();
  187. if (!IS_ERR(wl1251_board_data)) {
  188. wl->irq = wl1251_board_data->irq;
  189. wl->use_eeprom = wl1251_board_data->use_eeprom;
  190. } else if (np) {
  191. wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
  192. wl->irq = of_irq_get(np, 0);
  193. if (wl->irq == -EPROBE_DEFER) {
  194. ret = -EPROBE_DEFER;
  195. goto disable;
  196. }
  197. }
  198. if (wl->irq) {
  199. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  200. ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
  201. if (ret < 0) {
  202. wl1251_error("request_irq() failed: %d", ret);
  203. goto disable;
  204. }
  205. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  206. wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
  207. wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
  208. wl1251_info("using dedicated interrupt line");
  209. } else {
  210. wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
  211. wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
  212. wl1251_info("using SDIO interrupt");
  213. }
  214. ret = wl1251_init_ieee80211(wl);
  215. if (ret)
  216. goto out_free_irq;
  217. sdio_set_drvdata(func, wl);
  218. /* Tell PM core that we don't need the card to be powered now */
  219. pm_runtime_put_noidle(&func->dev);
  220. return ret;
  221. out_free_irq:
  222. if (wl->irq)
  223. free_irq(wl->irq, wl);
  224. disable:
  225. sdio_claim_host(func);
  226. sdio_disable_func(func);
  227. release:
  228. sdio_release_host(func);
  229. kfree(wl_sdio);
  230. out_free_hw:
  231. wl1251_free_hw(wl);
  232. return ret;
  233. }
  234. static void wl1251_sdio_remove(struct sdio_func *func)
  235. {
  236. struct wl1251 *wl = sdio_get_drvdata(func);
  237. struct wl1251_sdio *wl_sdio = wl->if_priv;
  238. /* Undo decrement done above in wl1251_probe */
  239. pm_runtime_get_noresume(&func->dev);
  240. if (wl->irq)
  241. free_irq(wl->irq, wl);
  242. wl1251_free_hw(wl);
  243. kfree(wl_sdio);
  244. sdio_claim_host(func);
  245. sdio_release_irq(func);
  246. sdio_disable_func(func);
  247. sdio_release_host(func);
  248. }
  249. static int wl1251_suspend(struct device *dev)
  250. {
  251. /*
  252. * Tell MMC/SDIO core it's OK to power down the card
  253. * (if it isn't already), but not to remove it completely.
  254. */
  255. return 0;
  256. }
  257. static int wl1251_resume(struct device *dev)
  258. {
  259. return 0;
  260. }
  261. static const struct dev_pm_ops wl1251_sdio_pm_ops = {
  262. .suspend = wl1251_suspend,
  263. .resume = wl1251_resume,
  264. };
  265. static struct sdio_driver wl1251_sdio_driver = {
  266. .name = "wl1251_sdio",
  267. .id_table = wl1251_devices,
  268. .probe = wl1251_sdio_probe,
  269. .remove = wl1251_sdio_remove,
  270. .drv.pm = &wl1251_sdio_pm_ops,
  271. };
  272. static int __init wl1251_sdio_init(void)
  273. {
  274. int err;
  275. err = sdio_register_driver(&wl1251_sdio_driver);
  276. if (err)
  277. wl1251_error("failed to register sdio driver: %d", err);
  278. return err;
  279. }
  280. static void __exit wl1251_sdio_exit(void)
  281. {
  282. sdio_unregister_driver(&wl1251_sdio_driver);
  283. wl1251_notice("unloaded");
  284. }
  285. module_init(wl1251_sdio_init);
  286. module_exit(wl1251_sdio_exit);
  287. MODULE_LICENSE("GPL");
  288. MODULE_AUTHOR("Kalle Valo <[email protected]>");