sdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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(addr == HW_ACCESS_ELP_CTRL_REG)) {
  73. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  74. dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
  75. addr, ((u8 *)buf)[0]);
  76. } else {
  77. if (fixed)
  78. ret = sdio_readsb(func, buf, addr, len);
  79. else
  80. ret = sdio_memcpy_fromio(func, buf, addr, len);
  81. dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
  82. addr, len);
  83. }
  84. sdio_release_host(func);
  85. if (WARN_ON(ret))
  86. dev_err(child->parent, "sdio read failed (%d)\n", ret);
  87. if (unlikely(dump)) {
  88. printk(KERN_DEBUG "wlcore_sdio: READ from 0x%04x\n", addr);
  89. print_hex_dump(KERN_DEBUG, "wlcore_sdio: READ ",
  90. DUMP_PREFIX_OFFSET, 16, 1,
  91. buf, len, false);
  92. }
  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. .nvs_name = "ti-connectivity/wl1271-nvs.bin",
  193. };
  194. static const struct of_device_id wlcore_sdio_of_match_table[] = {
  195. { .compatible = "ti,wl1271", .data = &wl127x_data },
  196. { .compatible = "ti,wl1273", .data = &wl127x_data },
  197. { .compatible = "ti,wl1281", .data = &wl128x_data },
  198. { .compatible = "ti,wl1283", .data = &wl128x_data },
  199. { .compatible = "ti,wl1285", .data = &wl128x_data },
  200. { .compatible = "ti,wl1801", .data = &wl18xx_data },
  201. { .compatible = "ti,wl1805", .data = &wl18xx_data },
  202. { .compatible = "ti,wl1807", .data = &wl18xx_data },
  203. { .compatible = "ti,wl1831", .data = &wl18xx_data },
  204. { .compatible = "ti,wl1835", .data = &wl18xx_data },
  205. { .compatible = "ti,wl1837", .data = &wl18xx_data },
  206. { }
  207. };
  208. static int wlcore_probe_of(struct device *dev, int *irq,
  209. struct wlcore_platdev_data *pdev_data)
  210. {
  211. struct device_node *np = dev->of_node;
  212. const struct of_device_id *of_id;
  213. of_id = of_match_node(wlcore_sdio_of_match_table, np);
  214. if (!of_id)
  215. return -ENODEV;
  216. pdev_data->family = of_id->data;
  217. *irq = irq_of_parse_and_map(np, 0);
  218. if (!*irq) {
  219. dev_err(dev, "No irq in platform data\n");
  220. return -EINVAL;
  221. }
  222. /* optional clock frequency params */
  223. of_property_read_u32(np, "ref-clock-frequency",
  224. &pdev_data->ref_clock_freq);
  225. of_property_read_u32(np, "tcxo-clock-frequency",
  226. &pdev_data->tcxo_clock_freq);
  227. return 0;
  228. }
  229. #else
  230. static int wlcore_probe_of(struct device *dev, int *irq,
  231. struct wlcore_platdev_data *pdev_data)
  232. {
  233. return -ENODATA;
  234. }
  235. #endif
  236. static int wl1271_probe(struct sdio_func *func,
  237. const struct sdio_device_id *id)
  238. {
  239. struct wlcore_platdev_data *pdev_data;
  240. struct wl12xx_sdio_glue *glue;
  241. struct resource res[1];
  242. mmc_pm_flag_t mmcflags;
  243. int ret = -ENOMEM;
  244. int irq;
  245. const char *chip_family;
  246. /* We are only able to handle the wlan function */
  247. if (func->num != 0x02)
  248. return -ENODEV;
  249. pdev_data = devm_kzalloc(&func->dev, sizeof(*pdev_data), GFP_KERNEL);
  250. if (!pdev_data)
  251. return -ENOMEM;
  252. pdev_data->if_ops = &sdio_ops;
  253. glue = devm_kzalloc(&func->dev, sizeof(*glue), GFP_KERNEL);
  254. if (!glue)
  255. return -ENOMEM;
  256. glue->dev = &func->dev;
  257. /* Grab access to FN0 for ELP reg. */
  258. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  259. /* Use block mode for transferring over one block size of data */
  260. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  261. ret = wlcore_probe_of(&func->dev, &irq, pdev_data);
  262. if (ret)
  263. goto out;
  264. /* if sdio can keep power while host is suspended, enable wow */
  265. mmcflags = sdio_get_host_pm_caps(func);
  266. dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
  267. if (mmcflags & MMC_PM_KEEP_POWER)
  268. pdev_data->pwr_in_suspend = true;
  269. sdio_set_drvdata(func, glue);
  270. /* Tell PM core that we don't need the card to be powered now */
  271. pm_runtime_put_noidle(&func->dev);
  272. /*
  273. * Due to a hardware bug, we can't differentiate wl18xx from
  274. * wl12xx, because both report the same device ID. The only
  275. * way to differentiate is by checking the SDIO revision,
  276. * which is 3.00 on the wl18xx chips.
  277. */
  278. if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
  279. chip_family = "wl18xx";
  280. else
  281. chip_family = "wl12xx";
  282. glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
  283. if (!glue->core) {
  284. dev_err(glue->dev, "can't allocate platform_device");
  285. ret = -ENOMEM;
  286. goto out;
  287. }
  288. glue->core->dev.parent = &func->dev;
  289. memset(res, 0x00, sizeof(res));
  290. res[0].start = irq;
  291. res[0].flags = IORESOURCE_IRQ |
  292. irqd_get_trigger_type(irq_get_irq_data(irq));
  293. res[0].name = "irq";
  294. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  295. if (ret) {
  296. dev_err(glue->dev, "can't add resources\n");
  297. goto out_dev_put;
  298. }
  299. ret = platform_device_add_data(glue->core, pdev_data,
  300. sizeof(*pdev_data));
  301. if (ret) {
  302. dev_err(glue->dev, "can't add platform data\n");
  303. goto out_dev_put;
  304. }
  305. ret = platform_device_add(glue->core);
  306. if (ret) {
  307. dev_err(glue->dev, "can't add platform device\n");
  308. goto out_dev_put;
  309. }
  310. return 0;
  311. out_dev_put:
  312. platform_device_put(glue->core);
  313. out:
  314. return ret;
  315. }
  316. static void wl1271_remove(struct sdio_func *func)
  317. {
  318. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  319. /* Undo decrement done above in wl1271_probe */
  320. pm_runtime_get_noresume(&func->dev);
  321. platform_device_unregister(glue->core);
  322. }
  323. #ifdef CONFIG_PM
  324. static int wl1271_suspend(struct device *dev)
  325. {
  326. /* Tell MMC/SDIO core it's OK to power down the card
  327. * (if it isn't already), but not to remove it completely */
  328. struct sdio_func *func = dev_to_sdio_func(dev);
  329. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  330. struct wl1271 *wl = platform_get_drvdata(glue->core);
  331. mmc_pm_flag_t sdio_flags;
  332. int ret = 0;
  333. dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
  334. wl->wow_enabled);
  335. /* check whether sdio should keep power */
  336. if (wl->wow_enabled) {
  337. sdio_flags = sdio_get_host_pm_caps(func);
  338. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  339. dev_err(dev, "can't keep power while host "
  340. "is suspended\n");
  341. ret = -EINVAL;
  342. goto out;
  343. }
  344. /* keep power while host suspended */
  345. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  346. if (ret) {
  347. dev_err(dev, "error while trying to keep power\n");
  348. goto out;
  349. }
  350. }
  351. out:
  352. return ret;
  353. }
  354. static int wl1271_resume(struct device *dev)
  355. {
  356. dev_dbg(dev, "wl1271 resume\n");
  357. return 0;
  358. }
  359. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  360. .suspend = wl1271_suspend,
  361. .resume = wl1271_resume,
  362. };
  363. #endif
  364. static struct sdio_driver wl1271_sdio_driver = {
  365. .name = "wl1271_sdio",
  366. .id_table = wl1271_devices,
  367. .probe = wl1271_probe,
  368. .remove = wl1271_remove,
  369. #ifdef CONFIG_PM
  370. .drv = {
  371. .pm = &wl1271_sdio_pm_ops,
  372. },
  373. #endif
  374. };
  375. static int __init wl1271_init(void)
  376. {
  377. return sdio_register_driver(&wl1271_sdio_driver);
  378. }
  379. static void __exit wl1271_exit(void)
  380. {
  381. sdio_unregister_driver(&wl1271_sdio_driver);
  382. }
  383. module_init(wl1271_init);
  384. module_exit(wl1271_exit);
  385. module_param(dump, bool, 0600);
  386. MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
  387. MODULE_LICENSE("GPL");
  388. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  389. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");