cw1200_sdio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Mac80211 SDIO driver for ST-Ericsson CW1200 device
  4. *
  5. * Copyright (c) 2010, ST-Ericsson
  6. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/gpio.h>
  11. #include <linux/delay.h>
  12. #include <linux/mmc/host.h>
  13. #include <linux/mmc/sdio_func.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/sdio.h>
  16. #include <net/mac80211.h>
  17. #include "cw1200.h"
  18. #include "hwbus.h"
  19. #include <linux/platform_data/net-cw1200.h>
  20. #include "hwio.h"
  21. MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
  22. MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
  23. MODULE_LICENSE("GPL");
  24. #define SDIO_BLOCK_SIZE (512)
  25. /* Default platform data for Sagrad modules */
  26. static struct cw1200_platform_data_sdio sagrad_109x_evk_platform_data = {
  27. .ref_clk = 38400,
  28. .have_5ghz = false,
  29. .sdd_file = "sdd_sagrad_1091_1098.bin",
  30. };
  31. /* Allow platform data to be overridden */
  32. static struct cw1200_platform_data_sdio *global_plat_data = &sagrad_109x_evk_platform_data;
  33. void __init cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio *pdata)
  34. {
  35. global_plat_data = pdata;
  36. }
  37. struct hwbus_priv {
  38. struct sdio_func *func;
  39. struct cw1200_common *core;
  40. const struct cw1200_platform_data_sdio *pdata;
  41. };
  42. #ifndef SDIO_VENDOR_ID_STE
  43. #define SDIO_VENDOR_ID_STE 0x0020
  44. #endif
  45. #ifndef SDIO_DEVICE_ID_STE_CW1200
  46. #define SDIO_DEVICE_ID_STE_CW1200 0x2280
  47. #endif
  48. static const struct sdio_device_id cw1200_sdio_ids[] = {
  49. { SDIO_DEVICE(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200) },
  50. { /* end: all zeroes */ },
  51. };
  52. MODULE_DEVICE_TABLE(sdio, cw1200_sdio_ids);
  53. /* hwbus_ops implemetation */
  54. static int cw1200_sdio_memcpy_fromio(struct hwbus_priv *self,
  55. unsigned int addr,
  56. void *dst, int count)
  57. {
  58. return sdio_memcpy_fromio(self->func, dst, addr, count);
  59. }
  60. static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self,
  61. unsigned int addr,
  62. const void *src, int count)
  63. {
  64. return sdio_memcpy_toio(self->func, addr, (void *)src, count);
  65. }
  66. static void cw1200_sdio_lock(struct hwbus_priv *self)
  67. {
  68. sdio_claim_host(self->func);
  69. }
  70. static void cw1200_sdio_unlock(struct hwbus_priv *self)
  71. {
  72. sdio_release_host(self->func);
  73. }
  74. static void cw1200_sdio_irq_handler(struct sdio_func *func)
  75. {
  76. struct hwbus_priv *self = sdio_get_drvdata(func);
  77. /* note: sdio_host already claimed here. */
  78. if (self->core)
  79. cw1200_irq_handler(self->core);
  80. }
  81. static irqreturn_t cw1200_gpio_hardirq(int irq, void *dev_id)
  82. {
  83. return IRQ_WAKE_THREAD;
  84. }
  85. static irqreturn_t cw1200_gpio_irq(int irq, void *dev_id)
  86. {
  87. struct hwbus_priv *self = dev_id;
  88. if (self->core) {
  89. cw1200_sdio_lock(self);
  90. cw1200_irq_handler(self->core);
  91. cw1200_sdio_unlock(self);
  92. return IRQ_HANDLED;
  93. } else {
  94. return IRQ_NONE;
  95. }
  96. }
  97. static int cw1200_request_irq(struct hwbus_priv *self)
  98. {
  99. int ret;
  100. u8 cccr;
  101. cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret);
  102. if (WARN_ON(ret))
  103. goto err;
  104. /* Master interrupt enable ... */
  105. cccr |= BIT(0);
  106. /* ... for our function */
  107. cccr |= BIT(self->func->num);
  108. sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret);
  109. if (WARN_ON(ret))
  110. goto err;
  111. ret = enable_irq_wake(self->pdata->irq);
  112. if (WARN_ON(ret))
  113. goto err;
  114. /* Request the IRQ */
  115. ret = request_threaded_irq(self->pdata->irq, cw1200_gpio_hardirq,
  116. cw1200_gpio_irq,
  117. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  118. "cw1200_wlan_irq", self);
  119. if (WARN_ON(ret))
  120. goto err;
  121. return 0;
  122. err:
  123. return ret;
  124. }
  125. static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self)
  126. {
  127. int ret = 0;
  128. pr_debug("SW IRQ subscribe\n");
  129. sdio_claim_host(self->func);
  130. if (self->pdata->irq)
  131. ret = cw1200_request_irq(self);
  132. else
  133. ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler);
  134. sdio_release_host(self->func);
  135. return ret;
  136. }
  137. static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self)
  138. {
  139. int ret = 0;
  140. pr_debug("SW IRQ unsubscribe\n");
  141. if (self->pdata->irq) {
  142. disable_irq_wake(self->pdata->irq);
  143. free_irq(self->pdata->irq, self);
  144. } else {
  145. sdio_claim_host(self->func);
  146. ret = sdio_release_irq(self->func);
  147. sdio_release_host(self->func);
  148. }
  149. return ret;
  150. }
  151. static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata)
  152. {
  153. if (pdata->reset) {
  154. gpio_set_value(pdata->reset, 0);
  155. msleep(30); /* Min is 2 * CLK32K cycles */
  156. gpio_free(pdata->reset);
  157. }
  158. if (pdata->power_ctrl)
  159. pdata->power_ctrl(pdata, false);
  160. if (pdata->clk_ctrl)
  161. pdata->clk_ctrl(pdata, false);
  162. return 0;
  163. }
  164. static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata)
  165. {
  166. /* Ensure I/Os are pulled low */
  167. if (pdata->reset) {
  168. gpio_request(pdata->reset, "cw1200_wlan_reset");
  169. gpio_direction_output(pdata->reset, 0);
  170. }
  171. if (pdata->powerup) {
  172. gpio_request(pdata->powerup, "cw1200_wlan_powerup");
  173. gpio_direction_output(pdata->powerup, 0);
  174. }
  175. if (pdata->reset || pdata->powerup)
  176. msleep(10); /* Settle time? */
  177. /* Enable 3v3 and 1v8 to hardware */
  178. if (pdata->power_ctrl) {
  179. if (pdata->power_ctrl(pdata, true)) {
  180. pr_err("power_ctrl() failed!\n");
  181. return -1;
  182. }
  183. }
  184. /* Enable CLK32K */
  185. if (pdata->clk_ctrl) {
  186. if (pdata->clk_ctrl(pdata, true)) {
  187. pr_err("clk_ctrl() failed!\n");
  188. return -1;
  189. }
  190. msleep(10); /* Delay until clock is stable for 2 cycles */
  191. }
  192. /* Enable POWERUP signal */
  193. if (pdata->powerup) {
  194. gpio_set_value(pdata->powerup, 1);
  195. msleep(250); /* or more..? */
  196. }
  197. /* Enable RSTn signal */
  198. if (pdata->reset) {
  199. gpio_set_value(pdata->reset, 1);
  200. msleep(50); /* Or more..? */
  201. }
  202. return 0;
  203. }
  204. static size_t cw1200_sdio_align_size(struct hwbus_priv *self, size_t size)
  205. {
  206. if (self->pdata->no_nptb)
  207. size = round_up(size, SDIO_BLOCK_SIZE);
  208. else
  209. size = sdio_align_size(self->func, size);
  210. return size;
  211. }
  212. static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend)
  213. {
  214. int ret = 0;
  215. if (self->pdata->irq)
  216. ret = irq_set_irq_wake(self->pdata->irq, suspend);
  217. return ret;
  218. }
  219. static const struct hwbus_ops cw1200_sdio_hwbus_ops = {
  220. .hwbus_memcpy_fromio = cw1200_sdio_memcpy_fromio,
  221. .hwbus_memcpy_toio = cw1200_sdio_memcpy_toio,
  222. .lock = cw1200_sdio_lock,
  223. .unlock = cw1200_sdio_unlock,
  224. .align_size = cw1200_sdio_align_size,
  225. .power_mgmt = cw1200_sdio_pm,
  226. };
  227. /* Probe Function to be called by SDIO stack when device is discovered */
  228. static int cw1200_sdio_probe(struct sdio_func *func,
  229. const struct sdio_device_id *id)
  230. {
  231. struct hwbus_priv *self;
  232. int status;
  233. pr_info("cw1200_wlan_sdio: Probe called\n");
  234. /* We are only able to handle the wlan function */
  235. if (func->num != 0x01)
  236. return -ENODEV;
  237. self = kzalloc(sizeof(*self), GFP_KERNEL);
  238. if (!self) {
  239. pr_err("Can't allocate SDIO hwbus_priv.\n");
  240. return -ENOMEM;
  241. }
  242. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  243. self->pdata = global_plat_data; /* FIXME */
  244. self->func = func;
  245. sdio_set_drvdata(func, self);
  246. sdio_claim_host(func);
  247. sdio_enable_func(func);
  248. sdio_release_host(func);
  249. status = cw1200_sdio_irq_subscribe(self);
  250. status = cw1200_core_probe(&cw1200_sdio_hwbus_ops,
  251. self, &func->dev, &self->core,
  252. self->pdata->ref_clk,
  253. self->pdata->macaddr,
  254. self->pdata->sdd_file,
  255. self->pdata->have_5ghz);
  256. if (status) {
  257. cw1200_sdio_irq_unsubscribe(self);
  258. sdio_claim_host(func);
  259. sdio_disable_func(func);
  260. sdio_release_host(func);
  261. sdio_set_drvdata(func, NULL);
  262. kfree(self);
  263. }
  264. return status;
  265. }
  266. /* Disconnect Function to be called by SDIO stack when
  267. * device is disconnected
  268. */
  269. static void cw1200_sdio_disconnect(struct sdio_func *func)
  270. {
  271. struct hwbus_priv *self = sdio_get_drvdata(func);
  272. if (self) {
  273. cw1200_sdio_irq_unsubscribe(self);
  274. if (self->core) {
  275. cw1200_core_release(self->core);
  276. self->core = NULL;
  277. }
  278. sdio_claim_host(func);
  279. sdio_disable_func(func);
  280. sdio_release_host(func);
  281. sdio_set_drvdata(func, NULL);
  282. kfree(self);
  283. }
  284. }
  285. #ifdef CONFIG_PM
  286. static int cw1200_sdio_suspend(struct device *dev)
  287. {
  288. int ret;
  289. struct sdio_func *func = dev_to_sdio_func(dev);
  290. struct hwbus_priv *self = sdio_get_drvdata(func);
  291. if (!cw1200_can_suspend(self->core))
  292. return -EAGAIN;
  293. /* Notify SDIO that CW1200 will remain powered during suspend */
  294. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  295. if (ret)
  296. pr_err("Error setting SDIO pm flags: %i\n", ret);
  297. return ret;
  298. }
  299. static int cw1200_sdio_resume(struct device *dev)
  300. {
  301. return 0;
  302. }
  303. static const struct dev_pm_ops cw1200_pm_ops = {
  304. .suspend = cw1200_sdio_suspend,
  305. .resume = cw1200_sdio_resume,
  306. };
  307. #endif
  308. static struct sdio_driver sdio_driver = {
  309. .name = "cw1200_wlan_sdio",
  310. .id_table = cw1200_sdio_ids,
  311. .probe = cw1200_sdio_probe,
  312. .remove = cw1200_sdio_disconnect,
  313. #ifdef CONFIG_PM
  314. .drv = {
  315. .pm = &cw1200_pm_ops,
  316. }
  317. #endif
  318. };
  319. /* Init Module function -> Called by insmod */
  320. static int __init cw1200_sdio_init(void)
  321. {
  322. const struct cw1200_platform_data_sdio *pdata;
  323. int ret;
  324. /* FIXME -- this won't support multiple devices */
  325. pdata = global_plat_data;
  326. if (cw1200_sdio_on(pdata)) {
  327. ret = -1;
  328. goto err;
  329. }
  330. ret = sdio_register_driver(&sdio_driver);
  331. if (ret)
  332. goto err;
  333. return 0;
  334. err:
  335. cw1200_sdio_off(pdata);
  336. return ret;
  337. }
  338. /* Called at Driver Unloading */
  339. static void __exit cw1200_sdio_exit(void)
  340. {
  341. const struct cw1200_platform_data_sdio *pdata;
  342. /* FIXME -- this won't support multiple devices */
  343. pdata = global_plat_data;
  344. sdio_unregister_driver(&sdio_driver);
  345. cw1200_sdio_off(pdata);
  346. }
  347. module_init(cw1200_sdio_init);
  348. module_exit(cw1200_sdio_exit);