cw1200_sdio.c 8.9 KB

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