cw1200_sdio.c 9.3 KB

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