cw1200_sdio.c 9.3 KB

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