sdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 < 0) {
  132. pm_runtime_put_noidle(&card->dev);
  133. dev_err(glue->dev, "%s: failed to get_sync(%d)\n",
  134. __func__, ret);
  135. return ret;
  136. }
  137. sdio_claim_host(func);
  138. sdio_enable_func(func);
  139. sdio_release_host(func);
  140. return 0;
  141. }
  142. static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
  143. {
  144. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  145. struct mmc_card *card = func->card;
  146. int error;
  147. sdio_claim_host(func);
  148. sdio_disable_func(func);
  149. sdio_release_host(func);
  150. /* Let runtime PM know the card is powered off */
  151. error = pm_runtime_put(&card->dev);
  152. if (error < 0 && error != -EBUSY) {
  153. dev_err(&card->dev, "%s failed: %i\n", __func__, error);
  154. return error;
  155. }
  156. return 0;
  157. }
  158. static int wl12xx_sdio_set_power(struct device *child, bool enable)
  159. {
  160. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  161. if (enable)
  162. return wl12xx_sdio_power_on(glue);
  163. else
  164. return wl12xx_sdio_power_off(glue);
  165. }
  166. static struct wl1271_if_operations sdio_ops = {
  167. .read = wl12xx_sdio_raw_read,
  168. .write = wl12xx_sdio_raw_write,
  169. .power = wl12xx_sdio_set_power,
  170. .set_block_size = wl1271_sdio_set_block_size,
  171. };
  172. #ifdef CONFIG_OF
  173. static const struct wilink_family_data wl127x_data = {
  174. .name = "wl127x",
  175. .nvs_name = "ti-connectivity/wl127x-nvs.bin",
  176. };
  177. static const struct wilink_family_data wl128x_data = {
  178. .name = "wl128x",
  179. .nvs_name = "ti-connectivity/wl128x-nvs.bin",
  180. };
  181. static const struct wilink_family_data wl18xx_data = {
  182. .name = "wl18xx",
  183. .cfg_name = "ti-connectivity/wl18xx-conf.bin",
  184. .nvs_name = "ti-connectivity/wl1271-nvs.bin",
  185. };
  186. static const struct of_device_id wlcore_sdio_of_match_table[] = {
  187. { .compatible = "ti,wl1271", .data = &wl127x_data },
  188. { .compatible = "ti,wl1273", .data = &wl127x_data },
  189. { .compatible = "ti,wl1281", .data = &wl128x_data },
  190. { .compatible = "ti,wl1283", .data = &wl128x_data },
  191. { .compatible = "ti,wl1285", .data = &wl128x_data },
  192. { .compatible = "ti,wl1801", .data = &wl18xx_data },
  193. { .compatible = "ti,wl1805", .data = &wl18xx_data },
  194. { .compatible = "ti,wl1807", .data = &wl18xx_data },
  195. { .compatible = "ti,wl1831", .data = &wl18xx_data },
  196. { .compatible = "ti,wl1835", .data = &wl18xx_data },
  197. { .compatible = "ti,wl1837", .data = &wl18xx_data },
  198. { }
  199. };
  200. static int wlcore_probe_of(struct device *dev, int *irq, int *wakeirq,
  201. struct wlcore_platdev_data *pdev_data)
  202. {
  203. struct device_node *np = dev->of_node;
  204. const struct of_device_id *of_id;
  205. of_id = of_match_node(wlcore_sdio_of_match_table, np);
  206. if (!of_id)
  207. return -ENODEV;
  208. pdev_data->family = of_id->data;
  209. *irq = irq_of_parse_and_map(np, 0);
  210. if (!*irq) {
  211. dev_err(dev, "No irq in platform data\n");
  212. return -EINVAL;
  213. }
  214. *wakeirq = irq_of_parse_and_map(np, 1);
  215. /* optional clock frequency params */
  216. of_property_read_u32(np, "ref-clock-frequency",
  217. &pdev_data->ref_clock_freq);
  218. of_property_read_u32(np, "tcxo-clock-frequency",
  219. &pdev_data->tcxo_clock_freq);
  220. return 0;
  221. }
  222. #else
  223. static int wlcore_probe_of(struct device *dev, int *irq, int *wakeirq,
  224. struct wlcore_platdev_data *pdev_data)
  225. {
  226. return -ENODATA;
  227. }
  228. #endif
  229. static int wl1271_probe(struct sdio_func *func,
  230. const struct sdio_device_id *id)
  231. {
  232. struct wlcore_platdev_data *pdev_data;
  233. struct wl12xx_sdio_glue *glue;
  234. struct resource res[2];
  235. mmc_pm_flag_t mmcflags;
  236. int ret = -ENOMEM;
  237. int irq, wakeirq;
  238. const char *chip_family;
  239. /* We are only able to handle the wlan function */
  240. if (func->num != 0x02)
  241. return -ENODEV;
  242. pdev_data = devm_kzalloc(&func->dev, sizeof(*pdev_data), GFP_KERNEL);
  243. if (!pdev_data)
  244. return -ENOMEM;
  245. pdev_data->if_ops = &sdio_ops;
  246. glue = devm_kzalloc(&func->dev, sizeof(*glue), GFP_KERNEL);
  247. if (!glue)
  248. return -ENOMEM;
  249. glue->dev = &func->dev;
  250. /* Grab access to FN0 for ELP reg. */
  251. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  252. /* Use block mode for transferring over one block size of data */
  253. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  254. ret = wlcore_probe_of(&func->dev, &irq, &wakeirq, pdev_data);
  255. if (ret)
  256. goto out;
  257. /* if sdio can keep power while host is suspended, enable wow */
  258. mmcflags = sdio_get_host_pm_caps(func);
  259. dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
  260. if (mmcflags & MMC_PM_KEEP_POWER)
  261. pdev_data->pwr_in_suspend = true;
  262. sdio_set_drvdata(func, glue);
  263. /* Tell PM core that we don't need the card to be powered now */
  264. pm_runtime_put_noidle(&func->dev);
  265. /*
  266. * Due to a hardware bug, we can't differentiate wl18xx from
  267. * wl12xx, because both report the same device ID. The only
  268. * way to differentiate is by checking the SDIO revision,
  269. * which is 3.00 on the wl18xx chips.
  270. */
  271. if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
  272. chip_family = "wl18xx";
  273. else
  274. chip_family = "wl12xx";
  275. glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
  276. if (!glue->core) {
  277. dev_err(glue->dev, "can't allocate platform_device");
  278. ret = -ENOMEM;
  279. goto out;
  280. }
  281. glue->core->dev.parent = &func->dev;
  282. memset(res, 0x00, sizeof(res));
  283. res[0].start = irq;
  284. res[0].flags = IORESOURCE_IRQ |
  285. irqd_get_trigger_type(irq_get_irq_data(irq));
  286. res[0].name = "irq";
  287. res[1].start = wakeirq;
  288. res[1].flags = IORESOURCE_IRQ |
  289. irqd_get_trigger_type(irq_get_irq_data(wakeirq));
  290. res[1].name = "wakeirq";
  291. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  292. if (ret) {
  293. dev_err(glue->dev, "can't add resources\n");
  294. goto out_dev_put;
  295. }
  296. ret = platform_device_add_data(glue->core, pdev_data,
  297. sizeof(*pdev_data));
  298. if (ret) {
  299. dev_err(glue->dev, "can't add platform data\n");
  300. goto out_dev_put;
  301. }
  302. ret = platform_device_add(glue->core);
  303. if (ret) {
  304. dev_err(glue->dev, "can't add platform device\n");
  305. goto out_dev_put;
  306. }
  307. return 0;
  308. out_dev_put:
  309. platform_device_put(glue->core);
  310. out:
  311. return ret;
  312. }
  313. static void wl1271_remove(struct sdio_func *func)
  314. {
  315. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  316. /* Undo decrement done above in wl1271_probe */
  317. pm_runtime_get_noresume(&func->dev);
  318. platform_device_unregister(glue->core);
  319. }
  320. #ifdef CONFIG_PM
  321. static int wl1271_suspend(struct device *dev)
  322. {
  323. /* Tell MMC/SDIO core it's OK to power down the card
  324. * (if it isn't already), but not to remove it completely */
  325. struct sdio_func *func = dev_to_sdio_func(dev);
  326. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  327. struct wl1271 *wl = platform_get_drvdata(glue->core);
  328. mmc_pm_flag_t sdio_flags;
  329. int ret = 0;
  330. if (!wl) {
  331. dev_err(dev, "no wilink module was probed\n");
  332. goto out;
  333. }
  334. dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
  335. wl->wow_enabled);
  336. /* check whether sdio should keep power */
  337. if (wl->wow_enabled) {
  338. sdio_flags = sdio_get_host_pm_caps(func);
  339. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  340. dev_err(dev, "can't keep power while host "
  341. "is suspended\n");
  342. ret = -EINVAL;
  343. goto out;
  344. }
  345. /* keep power while host suspended */
  346. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  347. if (ret) {
  348. dev_err(dev, "error while trying to keep power\n");
  349. goto out;
  350. }
  351. }
  352. out:
  353. return ret;
  354. }
  355. static int wl1271_resume(struct device *dev)
  356. {
  357. dev_dbg(dev, "wl1271 resume\n");
  358. return 0;
  359. }
  360. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  361. .suspend = wl1271_suspend,
  362. .resume = wl1271_resume,
  363. };
  364. #endif
  365. static struct sdio_driver wl1271_sdio_driver = {
  366. .name = "wl1271_sdio",
  367. .id_table = wl1271_devices,
  368. .probe = wl1271_probe,
  369. .remove = wl1271_remove,
  370. #ifdef CONFIG_PM
  371. .drv = {
  372. .pm = &wl1271_sdio_pm_ops,
  373. },
  374. #endif
  375. };
  376. static int __init wl1271_init(void)
  377. {
  378. return sdio_register_driver(&wl1271_sdio_driver);
  379. }
  380. static void __exit wl1271_exit(void)
  381. {
  382. sdio_unregister_driver(&wl1271_sdio_driver);
  383. }
  384. module_init(wl1271_init);
  385. module_exit(wl1271_exit);
  386. module_param(dump, bool, 0600);
  387. MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
  388. MODULE_LICENSE("GPL");
  389. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  390. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");