sdio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/irq.h>
  24. #include <linux/module.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mmc/sdio.h>
  28. #include <linux/mmc/sdio_func.h>
  29. #include <linux/mmc/sdio_ids.h>
  30. #include <linux/mmc/card.h>
  31. #include <linux/mmc/host.h>
  32. #include <linux/gpio.h>
  33. #include <linux/wl12xx.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/printk.h>
  36. #include "wlcore.h"
  37. #include "wl12xx_80211.h"
  38. #include "io.h"
  39. #ifndef SDIO_VENDOR_ID_TI
  40. #define SDIO_VENDOR_ID_TI 0x0097
  41. #endif
  42. #ifndef SDIO_DEVICE_ID_TI_WL1271
  43. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  44. #endif
  45. static bool dump = false;
  46. struct wl12xx_sdio_glue {
  47. struct device *dev;
  48. struct platform_device *core;
  49. };
  50. static const struct sdio_device_id wl1271_devices[] = {
  51. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  52. {}
  53. };
  54. MODULE_DEVICE_TABLE(sdio, wl1271_devices);
  55. static void wl1271_sdio_set_block_size(struct device *child,
  56. unsigned int blksz)
  57. {
  58. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  59. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  60. sdio_claim_host(func);
  61. sdio_set_block_size(func, blksz);
  62. sdio_release_host(func);
  63. }
  64. static int __must_check wl12xx_sdio_raw_read(struct device *child, int addr,
  65. void *buf, size_t len, bool fixed)
  66. {
  67. int ret;
  68. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  69. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  70. sdio_claim_host(func);
  71. if (unlikely(dump)) {
  72. printk(KERN_DEBUG "wlcore_sdio: READ from 0x%04x\n", addr);
  73. print_hex_dump(KERN_DEBUG, "wlcore_sdio: READ ",
  74. DUMP_PREFIX_OFFSET, 16, 1,
  75. buf, len, false);
  76. }
  77. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
  78. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  79. dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
  80. addr, ((u8 *)buf)[0]);
  81. } else {
  82. if (fixed)
  83. ret = sdio_readsb(func, buf, addr, len);
  84. else
  85. ret = sdio_memcpy_fromio(func, buf, addr, len);
  86. dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
  87. addr, len);
  88. }
  89. sdio_release_host(func);
  90. if (WARN_ON(ret))
  91. dev_err(child->parent, "sdio read failed (%d)\n", ret);
  92. return ret;
  93. }
  94. static int __must_check wl12xx_sdio_raw_write(struct device *child, int addr,
  95. void *buf, size_t len, bool fixed)
  96. {
  97. int ret;
  98. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  99. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  100. sdio_claim_host(func);
  101. if (unlikely(dump)) {
  102. printk(KERN_DEBUG "wlcore_sdio: WRITE to 0x%04x\n", addr);
  103. print_hex_dump(KERN_DEBUG, "wlcore_sdio: WRITE ",
  104. DUMP_PREFIX_OFFSET, 16, 1,
  105. buf, len, false);
  106. }
  107. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
  108. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  109. dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
  110. addr, ((u8 *)buf)[0]);
  111. } else {
  112. dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
  113. addr, len);
  114. if (fixed)
  115. ret = sdio_writesb(func, addr, buf, len);
  116. else
  117. ret = sdio_memcpy_toio(func, addr, buf, len);
  118. }
  119. sdio_release_host(func);
  120. if (WARN_ON(ret))
  121. dev_err(child->parent, "sdio write failed (%d)\n", ret);
  122. return ret;
  123. }
  124. static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
  125. {
  126. int ret;
  127. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  128. struct mmc_card *card = func->card;
  129. ret = pm_runtime_get_sync(&card->dev);
  130. if (ret) {
  131. /*
  132. * Runtime PM might be temporarily disabled, or the device
  133. * might have a positive reference counter. Make sure it is
  134. * really powered on.
  135. */
  136. ret = mmc_power_restore_host(card->host);
  137. if (ret < 0) {
  138. pm_runtime_put_sync(&card->dev);
  139. goto out;
  140. }
  141. }
  142. sdio_claim_host(func);
  143. sdio_enable_func(func);
  144. sdio_release_host(func);
  145. out:
  146. return ret;
  147. }
  148. static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
  149. {
  150. int ret;
  151. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  152. struct mmc_card *card = func->card;
  153. sdio_claim_host(func);
  154. sdio_disable_func(func);
  155. sdio_release_host(func);
  156. /* Power off the card manually in case it wasn't powered off above */
  157. ret = mmc_power_save_host(card->host);
  158. if (ret < 0)
  159. goto out;
  160. /* Let runtime PM know the card is powered off */
  161. pm_runtime_put_sync(&card->dev);
  162. out:
  163. return ret;
  164. }
  165. static int wl12xx_sdio_set_power(struct device *child, bool enable)
  166. {
  167. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  168. if (enable)
  169. return wl12xx_sdio_power_on(glue);
  170. else
  171. return wl12xx_sdio_power_off(glue);
  172. }
  173. static struct wl1271_if_operations sdio_ops = {
  174. .read = wl12xx_sdio_raw_read,
  175. .write = wl12xx_sdio_raw_write,
  176. .power = wl12xx_sdio_set_power,
  177. .set_block_size = wl1271_sdio_set_block_size,
  178. };
  179. static int wl1271_probe(struct sdio_func *func,
  180. const struct sdio_device_id *id)
  181. {
  182. struct wlcore_platdev_data *pdev_data;
  183. struct wl12xx_sdio_glue *glue;
  184. struct resource res[1];
  185. mmc_pm_flag_t mmcflags;
  186. int ret = -ENOMEM;
  187. const char *chip_family;
  188. /* We are only able to handle the wlan function */
  189. if (func->num != 0x02)
  190. return -ENODEV;
  191. pdev_data = kzalloc(sizeof(*pdev_data), GFP_KERNEL);
  192. if (!pdev_data)
  193. goto out;
  194. pdev_data->if_ops = &sdio_ops;
  195. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  196. if (!glue) {
  197. dev_err(&func->dev, "can't allocate glue\n");
  198. goto out_free_pdev_data;
  199. }
  200. glue->dev = &func->dev;
  201. /* Grab access to FN0 for ELP reg. */
  202. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  203. /* Use block mode for transferring over one block size of data */
  204. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  205. pdev_data->pdata = wl12xx_get_platform_data();
  206. if (IS_ERR(pdev_data->pdata)) {
  207. ret = PTR_ERR(pdev_data->pdata);
  208. dev_err(glue->dev, "missing wlan platform data: %d\n", ret);
  209. goto out_free_glue;
  210. }
  211. /* if sdio can keep power while host is suspended, enable wow */
  212. mmcflags = sdio_get_host_pm_caps(func);
  213. dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
  214. if (mmcflags & MMC_PM_KEEP_POWER)
  215. pdev_data->pdata->pwr_in_suspend = true;
  216. sdio_set_drvdata(func, glue);
  217. /* Tell PM core that we don't need the card to be powered now */
  218. pm_runtime_put_noidle(&func->dev);
  219. /*
  220. * Due to a hardware bug, we can't differentiate wl18xx from
  221. * wl12xx, because both report the same device ID. The only
  222. * way to differentiate is by checking the SDIO revision,
  223. * which is 3.00 on the wl18xx chips.
  224. */
  225. if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
  226. chip_family = "wl18xx";
  227. else
  228. chip_family = "wl12xx";
  229. glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
  230. if (!glue->core) {
  231. dev_err(glue->dev, "can't allocate platform_device");
  232. ret = -ENOMEM;
  233. goto out_free_glue;
  234. }
  235. glue->core->dev.parent = &func->dev;
  236. memset(res, 0x00, sizeof(res));
  237. res[0].start = pdev_data->pdata->irq;
  238. res[0].flags = IORESOURCE_IRQ;
  239. res[0].name = "irq";
  240. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  241. if (ret) {
  242. dev_err(glue->dev, "can't add resources\n");
  243. goto out_dev_put;
  244. }
  245. ret = platform_device_add_data(glue->core, pdev_data,
  246. sizeof(*pdev_data));
  247. if (ret) {
  248. dev_err(glue->dev, "can't add platform data\n");
  249. goto out_dev_put;
  250. }
  251. ret = platform_device_add(glue->core);
  252. if (ret) {
  253. dev_err(glue->dev, "can't add platform device\n");
  254. goto out_dev_put;
  255. }
  256. return 0;
  257. out_dev_put:
  258. platform_device_put(glue->core);
  259. out_free_glue:
  260. kfree(glue);
  261. out_free_pdev_data:
  262. kfree(pdev_data);
  263. out:
  264. return ret;
  265. }
  266. static void wl1271_remove(struct sdio_func *func)
  267. {
  268. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  269. /* Undo decrement done above in wl1271_probe */
  270. pm_runtime_get_noresume(&func->dev);
  271. platform_device_unregister(glue->core);
  272. kfree(glue);
  273. }
  274. #ifdef CONFIG_PM
  275. static int wl1271_suspend(struct device *dev)
  276. {
  277. /* Tell MMC/SDIO core it's OK to power down the card
  278. * (if it isn't already), but not to remove it completely */
  279. struct sdio_func *func = dev_to_sdio_func(dev);
  280. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  281. struct wl1271 *wl = platform_get_drvdata(glue->core);
  282. mmc_pm_flag_t sdio_flags;
  283. int ret = 0;
  284. dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
  285. wl->wow_enabled);
  286. /* check whether sdio should keep power */
  287. if (wl->wow_enabled) {
  288. sdio_flags = sdio_get_host_pm_caps(func);
  289. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  290. dev_err(dev, "can't keep power while host "
  291. "is suspended\n");
  292. ret = -EINVAL;
  293. goto out;
  294. }
  295. /* keep power while host suspended */
  296. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  297. if (ret) {
  298. dev_err(dev, "error while trying to keep power\n");
  299. goto out;
  300. }
  301. }
  302. out:
  303. return ret;
  304. }
  305. static int wl1271_resume(struct device *dev)
  306. {
  307. dev_dbg(dev, "wl1271 resume\n");
  308. return 0;
  309. }
  310. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  311. .suspend = wl1271_suspend,
  312. .resume = wl1271_resume,
  313. };
  314. #endif
  315. static struct sdio_driver wl1271_sdio_driver = {
  316. .name = "wl1271_sdio",
  317. .id_table = wl1271_devices,
  318. .probe = wl1271_probe,
  319. .remove = wl1271_remove,
  320. #ifdef CONFIG_PM
  321. .drv = {
  322. .pm = &wl1271_sdio_pm_ops,
  323. },
  324. #endif
  325. };
  326. static int __init wl1271_init(void)
  327. {
  328. return sdio_register_driver(&wl1271_sdio_driver);
  329. }
  330. static void __exit wl1271_exit(void)
  331. {
  332. sdio_unregister_driver(&wl1271_sdio_driver);
  333. }
  334. module_init(wl1271_init);
  335. module_exit(wl1271_exit);
  336. module_param(dump, bool, S_IRUSR | S_IWUSR);
  337. MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
  338. MODULE_LICENSE("GPL");
  339. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  340. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");