sdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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/pm_runtime.h>
  34. #include <linux/printk.h>
  35. #include <linux/of.h>
  36. #include <linux/of_irq.h>
  37. #include "wlcore.h"
  38. #include "wl12xx_80211.h"
  39. #include "io.h"
  40. #ifndef SDIO_VENDOR_ID_TI
  41. #define SDIO_VENDOR_ID_TI 0x0097
  42. #endif
  43. #ifndef SDIO_DEVICE_ID_TI_WL1271
  44. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  45. #endif
  46. static bool dump = false;
  47. struct wl12xx_sdio_glue {
  48. struct device *dev;
  49. struct platform_device *core;
  50. };
  51. static const struct sdio_device_id wl1271_devices[] = {
  52. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  53. {}
  54. };
  55. MODULE_DEVICE_TABLE(sdio, wl1271_devices);
  56. static void wl1271_sdio_set_block_size(struct device *child,
  57. unsigned int blksz)
  58. {
  59. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  60. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  61. sdio_claim_host(func);
  62. sdio_set_block_size(func, blksz);
  63. sdio_release_host(func);
  64. }
  65. static int __must_check wl12xx_sdio_raw_read(struct device *child, int addr,
  66. void *buf, size_t len, bool fixed)
  67. {
  68. int ret;
  69. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  70. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  71. sdio_claim_host(func);
  72. if (unlikely(dump)) {
  73. printk(KERN_DEBUG "wlcore_sdio: READ from 0x%04x\n", addr);
  74. print_hex_dump(KERN_DEBUG, "wlcore_sdio: READ ",
  75. DUMP_PREFIX_OFFSET, 16, 1,
  76. buf, len, false);
  77. }
  78. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
  79. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  80. dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
  81. addr, ((u8 *)buf)[0]);
  82. } else {
  83. if (fixed)
  84. ret = sdio_readsb(func, buf, addr, len);
  85. else
  86. ret = sdio_memcpy_fromio(func, buf, addr, len);
  87. dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
  88. addr, len);
  89. }
  90. sdio_release_host(func);
  91. if (WARN_ON(ret))
  92. dev_err(child->parent, "sdio read failed (%d)\n", ret);
  93. return ret;
  94. }
  95. static int __must_check wl12xx_sdio_raw_write(struct device *child, int addr,
  96. void *buf, size_t len, bool fixed)
  97. {
  98. int ret;
  99. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  100. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  101. sdio_claim_host(func);
  102. if (unlikely(dump)) {
  103. printk(KERN_DEBUG "wlcore_sdio: WRITE to 0x%04x\n", addr);
  104. print_hex_dump(KERN_DEBUG, "wlcore_sdio: WRITE ",
  105. DUMP_PREFIX_OFFSET, 16, 1,
  106. buf, len, false);
  107. }
  108. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
  109. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  110. dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
  111. addr, ((u8 *)buf)[0]);
  112. } else {
  113. dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
  114. addr, len);
  115. if (fixed)
  116. ret = sdio_writesb(func, addr, buf, len);
  117. else
  118. ret = sdio_memcpy_toio(func, addr, buf, len);
  119. }
  120. sdio_release_host(func);
  121. if (WARN_ON(ret))
  122. dev_err(child->parent, "sdio write failed (%d)\n", ret);
  123. return ret;
  124. }
  125. static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
  126. {
  127. int ret;
  128. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  129. struct mmc_card *card = func->card;
  130. ret = pm_runtime_get_sync(&card->dev);
  131. if (ret) {
  132. /*
  133. * Runtime PM might be temporarily disabled, or the device
  134. * might have a positive reference counter. Make sure it is
  135. * really powered on.
  136. */
  137. ret = mmc_power_restore_host(card->host);
  138. if (ret < 0) {
  139. pm_runtime_put_sync(&card->dev);
  140. goto out;
  141. }
  142. }
  143. sdio_claim_host(func);
  144. sdio_enable_func(func);
  145. sdio_release_host(func);
  146. out:
  147. return ret;
  148. }
  149. static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
  150. {
  151. int ret;
  152. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  153. struct mmc_card *card = func->card;
  154. sdio_claim_host(func);
  155. sdio_disable_func(func);
  156. sdio_release_host(func);
  157. /* Power off the card manually in case it wasn't powered off above */
  158. ret = mmc_power_save_host(card->host);
  159. if (ret < 0)
  160. goto out;
  161. /* Let runtime PM know the card is powered off */
  162. pm_runtime_put_sync(&card->dev);
  163. out:
  164. return ret;
  165. }
  166. static int wl12xx_sdio_set_power(struct device *child, bool enable)
  167. {
  168. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  169. if (enable)
  170. return wl12xx_sdio_power_on(glue);
  171. else
  172. return wl12xx_sdio_power_off(glue);
  173. }
  174. static struct wl1271_if_operations sdio_ops = {
  175. .read = wl12xx_sdio_raw_read,
  176. .write = wl12xx_sdio_raw_write,
  177. .power = wl12xx_sdio_set_power,
  178. .set_block_size = wl1271_sdio_set_block_size,
  179. };
  180. #ifdef CONFIG_OF
  181. static const struct wilink_family_data wl127x_data = {
  182. .name = "wl127x",
  183. .nvs_name = "ti-connectivity/wl127x-nvs.bin",
  184. };
  185. static const struct wilink_family_data wl128x_data = {
  186. .name = "wl128x",
  187. .nvs_name = "ti-connectivity/wl128x-nvs.bin",
  188. };
  189. static const struct wilink_family_data wl18xx_data = {
  190. .name = "wl18xx",
  191. .cfg_name = "ti-connectivity/wl18xx-conf.bin",
  192. };
  193. static const struct of_device_id wlcore_sdio_of_match_table[] = {
  194. { .compatible = "ti,wl1271", .data = &wl127x_data },
  195. { .compatible = "ti,wl1273", .data = &wl127x_data },
  196. { .compatible = "ti,wl1281", .data = &wl128x_data },
  197. { .compatible = "ti,wl1283", .data = &wl128x_data },
  198. { .compatible = "ti,wl1801", .data = &wl18xx_data },
  199. { .compatible = "ti,wl1805", .data = &wl18xx_data },
  200. { .compatible = "ti,wl1807", .data = &wl18xx_data },
  201. { .compatible = "ti,wl1831", .data = &wl18xx_data },
  202. { .compatible = "ti,wl1835", .data = &wl18xx_data },
  203. { .compatible = "ti,wl1837", .data = &wl18xx_data },
  204. { }
  205. };
  206. static int wlcore_probe_of(struct device *dev, int *irq,
  207. struct wlcore_platdev_data *pdev_data)
  208. {
  209. struct device_node *np = dev->of_node;
  210. const struct of_device_id *of_id;
  211. of_id = of_match_node(wlcore_sdio_of_match_table, np);
  212. if (!of_id)
  213. return -ENODEV;
  214. pdev_data->family = of_id->data;
  215. *irq = irq_of_parse_and_map(np, 0);
  216. if (!*irq) {
  217. dev_err(dev, "No irq in platform data\n");
  218. return -EINVAL;
  219. }
  220. /* optional clock frequency params */
  221. of_property_read_u32(np, "ref-clock-frequency",
  222. &pdev_data->ref_clock_freq);
  223. of_property_read_u32(np, "tcxo-clock-frequency",
  224. &pdev_data->tcxo_clock_freq);
  225. return 0;
  226. }
  227. #else
  228. static int wlcore_probe_of(struct device *dev, int *irq,
  229. struct wlcore_platdev_data *pdev_data)
  230. {
  231. return -ENODATA;
  232. }
  233. #endif
  234. static int wl1271_probe(struct sdio_func *func,
  235. const struct sdio_device_id *id)
  236. {
  237. struct wlcore_platdev_data *pdev_data;
  238. struct wl12xx_sdio_glue *glue;
  239. struct resource res[1];
  240. mmc_pm_flag_t mmcflags;
  241. int ret = -ENOMEM;
  242. int irq;
  243. const char *chip_family;
  244. /* We are only able to handle the wlan function */
  245. if (func->num != 0x02)
  246. return -ENODEV;
  247. pdev_data = devm_kzalloc(&func->dev, sizeof(*pdev_data), GFP_KERNEL);
  248. if (!pdev_data)
  249. return -ENOMEM;
  250. pdev_data->if_ops = &sdio_ops;
  251. glue = devm_kzalloc(&func->dev, sizeof(*glue), GFP_KERNEL);
  252. if (!glue)
  253. return -ENOMEM;
  254. glue->dev = &func->dev;
  255. /* Grab access to FN0 for ELP reg. */
  256. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  257. /* Use block mode for transferring over one block size of data */
  258. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  259. ret = wlcore_probe_of(&func->dev, &irq, pdev_data);
  260. if (ret)
  261. goto out;
  262. /* if sdio can keep power while host is suspended, enable wow */
  263. mmcflags = sdio_get_host_pm_caps(func);
  264. dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
  265. if (mmcflags & MMC_PM_KEEP_POWER)
  266. pdev_data->pwr_in_suspend = true;
  267. sdio_set_drvdata(func, glue);
  268. /* Tell PM core that we don't need the card to be powered now */
  269. pm_runtime_put_noidle(&func->dev);
  270. /*
  271. * Due to a hardware bug, we can't differentiate wl18xx from
  272. * wl12xx, because both report the same device ID. The only
  273. * way to differentiate is by checking the SDIO revision,
  274. * which is 3.00 on the wl18xx chips.
  275. */
  276. if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
  277. chip_family = "wl18xx";
  278. else
  279. chip_family = "wl12xx";
  280. glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
  281. if (!glue->core) {
  282. dev_err(glue->dev, "can't allocate platform_device");
  283. ret = -ENOMEM;
  284. goto out;
  285. }
  286. glue->core->dev.parent = &func->dev;
  287. memset(res, 0x00, sizeof(res));
  288. res[0].start = irq;
  289. res[0].flags = IORESOURCE_IRQ |
  290. irqd_get_trigger_type(irq_get_irq_data(irq));
  291. res[0].name = "irq";
  292. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  293. if (ret) {
  294. dev_err(glue->dev, "can't add resources\n");
  295. goto out_dev_put;
  296. }
  297. ret = platform_device_add_data(glue->core, pdev_data,
  298. sizeof(*pdev_data));
  299. if (ret) {
  300. dev_err(glue->dev, "can't add platform data\n");
  301. goto out_dev_put;
  302. }
  303. ret = platform_device_add(glue->core);
  304. if (ret) {
  305. dev_err(glue->dev, "can't add platform device\n");
  306. goto out_dev_put;
  307. }
  308. return 0;
  309. out_dev_put:
  310. platform_device_put(glue->core);
  311. out:
  312. return ret;
  313. }
  314. static void wl1271_remove(struct sdio_func *func)
  315. {
  316. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  317. /* Undo decrement done above in wl1271_probe */
  318. pm_runtime_get_noresume(&func->dev);
  319. platform_device_unregister(glue->core);
  320. }
  321. #ifdef CONFIG_PM
  322. static int wl1271_suspend(struct device *dev)
  323. {
  324. /* Tell MMC/SDIO core it's OK to power down the card
  325. * (if it isn't already), but not to remove it completely */
  326. struct sdio_func *func = dev_to_sdio_func(dev);
  327. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  328. struct wl1271 *wl = platform_get_drvdata(glue->core);
  329. mmc_pm_flag_t sdio_flags;
  330. int ret = 0;
  331. dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
  332. wl->wow_enabled);
  333. /* check whether sdio should keep power */
  334. if (wl->wow_enabled) {
  335. sdio_flags = sdio_get_host_pm_caps(func);
  336. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  337. dev_err(dev, "can't keep power while host "
  338. "is suspended\n");
  339. ret = -EINVAL;
  340. goto out;
  341. }
  342. /* keep power while host suspended */
  343. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  344. if (ret) {
  345. dev_err(dev, "error while trying to keep power\n");
  346. goto out;
  347. }
  348. }
  349. out:
  350. return ret;
  351. }
  352. static int wl1271_resume(struct device *dev)
  353. {
  354. dev_dbg(dev, "wl1271 resume\n");
  355. return 0;
  356. }
  357. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  358. .suspend = wl1271_suspend,
  359. .resume = wl1271_resume,
  360. };
  361. #endif
  362. static struct sdio_driver wl1271_sdio_driver = {
  363. .name = "wl1271_sdio",
  364. .id_table = wl1271_devices,
  365. .probe = wl1271_probe,
  366. .remove = wl1271_remove,
  367. #ifdef CONFIG_PM
  368. .drv = {
  369. .pm = &wl1271_sdio_pm_ops,
  370. },
  371. #endif
  372. };
  373. static int __init wl1271_init(void)
  374. {
  375. return sdio_register_driver(&wl1271_sdio_driver);
  376. }
  377. static void __exit wl1271_exit(void)
  378. {
  379. sdio_unregister_driver(&wl1271_sdio_driver);
  380. }
  381. module_init(wl1271_init);
  382. module_exit(wl1271_exit);
  383. module_param(dump, bool, S_IRUSR | S_IWUSR);
  384. MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
  385. MODULE_LICENSE("GPL");
  386. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  387. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");