sdhci-pxav3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) 2010 Marvell International Ltd.
  3. * Zhangfei Gao <zhangfei.gao@marvell.com>
  4. * Kevin Wang <dwang4@marvell.com>
  5. * Mingwei Wang <mwwang@marvell.com>
  6. * Philip Rakity <prakity@marvell.com>
  7. * Mark Brown <markb@marvell.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/mmc/host.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include <linux/platform_data/pxa_sdhci.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_gpio.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_runtime.h>
  37. #include "sdhci.h"
  38. #include "sdhci-pltfm.h"
  39. #define PXAV3_RPM_DELAY_MS 50
  40. #define SD_CLOCK_BURST_SIZE_SETUP 0x10A
  41. #define SDCLK_SEL 0x100
  42. #define SDCLK_DELAY_SHIFT 9
  43. #define SDCLK_DELAY_MASK 0x1f
  44. #define SD_CFG_FIFO_PARAM 0x100
  45. #define SDCFG_GEN_PAD_CLK_ON (1<<6)
  46. #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
  47. #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
  48. #define SD_SPI_MODE 0x108
  49. #define SD_CE_ATA_1 0x10C
  50. #define SD_CE_ATA_2 0x10E
  51. #define SDCE_MISC_INT (1<<2)
  52. #define SDCE_MISC_INT_EN (1<<1)
  53. static void pxav3_set_private_registers(struct sdhci_host *host, u8 mask)
  54. {
  55. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  56. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  57. if (mask == SDHCI_RESET_ALL) {
  58. /*
  59. * tune timing of read data/command when crc error happen
  60. * no performance impact
  61. */
  62. if (pdata && 0 != pdata->clk_delay_cycles) {
  63. u16 tmp;
  64. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  65. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  66. << SDCLK_DELAY_SHIFT;
  67. tmp |= SDCLK_SEL;
  68. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  69. }
  70. }
  71. }
  72. #define MAX_WAIT_COUNT 5
  73. static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  74. {
  75. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  76. struct sdhci_pxa *pxa = pltfm_host->priv;
  77. u16 tmp;
  78. int count;
  79. if (pxa->power_mode == MMC_POWER_UP
  80. && power_mode == MMC_POWER_ON) {
  81. dev_dbg(mmc_dev(host->mmc),
  82. "%s: slot->power_mode = %d,"
  83. "ios->power_mode = %d\n",
  84. __func__,
  85. pxa->power_mode,
  86. power_mode);
  87. /* set we want notice of when 74 clocks are sent */
  88. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  89. tmp |= SDCE_MISC_INT_EN;
  90. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  91. /* start sending the 74 clocks */
  92. tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
  93. tmp |= SDCFG_GEN_PAD_CLK_ON;
  94. writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
  95. /* slowest speed is about 100KHz or 10usec per clock */
  96. udelay(740);
  97. count = 0;
  98. while (count++ < MAX_WAIT_COUNT) {
  99. if ((readw(host->ioaddr + SD_CE_ATA_2)
  100. & SDCE_MISC_INT) == 0)
  101. break;
  102. udelay(10);
  103. }
  104. if (count == MAX_WAIT_COUNT)
  105. dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
  106. /* clear the interrupt bit if posted */
  107. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  108. tmp |= SDCE_MISC_INT;
  109. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  110. }
  111. pxa->power_mode = power_mode;
  112. }
  113. static int pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
  114. {
  115. u16 ctrl_2;
  116. /*
  117. * Set V18_EN -- UHS modes do not work without this.
  118. * does not change signaling voltage
  119. */
  120. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  121. /* Select Bus Speed Mode for host */
  122. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  123. switch (uhs) {
  124. case MMC_TIMING_UHS_SDR12:
  125. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  126. break;
  127. case MMC_TIMING_UHS_SDR25:
  128. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  129. break;
  130. case MMC_TIMING_UHS_SDR50:
  131. ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
  132. break;
  133. case MMC_TIMING_UHS_SDR104:
  134. ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
  135. break;
  136. case MMC_TIMING_UHS_DDR50:
  137. ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
  138. break;
  139. }
  140. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  141. dev_dbg(mmc_dev(host->mmc),
  142. "%s uhs = %d, ctrl_2 = %04X\n",
  143. __func__, uhs, ctrl_2);
  144. return 0;
  145. }
  146. static struct sdhci_ops pxav3_sdhci_ops = {
  147. .platform_reset_exit = pxav3_set_private_registers,
  148. .set_uhs_signaling = pxav3_set_uhs_signaling,
  149. .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
  150. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  151. };
  152. #ifdef CONFIG_OF
  153. static const struct of_device_id sdhci_pxav3_of_match[] = {
  154. {
  155. .compatible = "mrvl,pxav3-mmc",
  156. },
  157. {},
  158. };
  159. MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
  160. static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  161. {
  162. struct sdhci_pxa_platdata *pdata;
  163. struct device_node *np = dev->of_node;
  164. u32 bus_width;
  165. u32 clk_delay_cycles;
  166. enum of_gpio_flags gpio_flags;
  167. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  168. if (!pdata)
  169. return NULL;
  170. if (of_find_property(np, "non-removable", NULL))
  171. pdata->flags |= PXA_FLAG_CARD_PERMANENT;
  172. of_property_read_u32(np, "bus-width", &bus_width);
  173. if (bus_width == 8)
  174. pdata->flags |= PXA_FLAG_SD_8_BIT_CAPABLE_SLOT;
  175. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  176. if (clk_delay_cycles > 0)
  177. pdata->clk_delay_cycles = clk_delay_cycles;
  178. pdata->ext_cd_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &gpio_flags);
  179. if (gpio_flags != OF_GPIO_ACTIVE_LOW)
  180. pdata->host_caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  181. return pdata;
  182. }
  183. #else
  184. static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  185. {
  186. return NULL;
  187. }
  188. #endif
  189. static int sdhci_pxav3_probe(struct platform_device *pdev)
  190. {
  191. struct sdhci_pltfm_host *pltfm_host;
  192. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  193. struct device *dev = &pdev->dev;
  194. struct sdhci_host *host = NULL;
  195. struct sdhci_pxa *pxa = NULL;
  196. const struct of_device_id *match;
  197. int ret;
  198. struct clk *clk;
  199. pxa = kzalloc(sizeof(struct sdhci_pxa), GFP_KERNEL);
  200. if (!pxa)
  201. return -ENOMEM;
  202. host = sdhci_pltfm_init(pdev, NULL);
  203. if (IS_ERR(host)) {
  204. kfree(pxa);
  205. return PTR_ERR(host);
  206. }
  207. pltfm_host = sdhci_priv(host);
  208. pltfm_host->priv = pxa;
  209. clk = clk_get(dev, NULL);
  210. if (IS_ERR(clk)) {
  211. dev_err(dev, "failed to get io clock\n");
  212. ret = PTR_ERR(clk);
  213. goto err_clk_get;
  214. }
  215. pltfm_host->clk = clk;
  216. clk_prepare_enable(clk);
  217. host->quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL
  218. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  219. | SDHCI_QUIRK_32BIT_ADMA_SIZE
  220. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  221. /* enable 1/8V DDR capable */
  222. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  223. match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
  224. if (match)
  225. pdata = pxav3_get_mmc_pdata(dev);
  226. if (pdata) {
  227. if (pdata->flags & PXA_FLAG_CARD_PERMANENT) {
  228. /* on-chip device */
  229. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  230. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  231. }
  232. /* If slot design supports 8 bit data, indicate this to MMC. */
  233. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  234. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  235. if (pdata->quirks)
  236. host->quirks |= pdata->quirks;
  237. if (pdata->quirks2)
  238. host->quirks2 |= pdata->quirks2;
  239. if (pdata->host_caps)
  240. host->mmc->caps |= pdata->host_caps;
  241. if (pdata->host_caps2)
  242. host->mmc->caps2 |= pdata->host_caps2;
  243. if (pdata->pm_caps)
  244. host->mmc->pm_caps |= pdata->pm_caps;
  245. if (gpio_is_valid(pdata->ext_cd_gpio)) {
  246. ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio);
  247. if (ret) {
  248. dev_err(mmc_dev(host->mmc),
  249. "failed to allocate card detect gpio\n");
  250. goto err_cd_req;
  251. }
  252. }
  253. }
  254. host->ops = &pxav3_sdhci_ops;
  255. sdhci_get_of_property(pdev);
  256. pm_runtime_set_active(&pdev->dev);
  257. pm_runtime_enable(&pdev->dev);
  258. pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS);
  259. pm_runtime_use_autosuspend(&pdev->dev);
  260. pm_suspend_ignore_children(&pdev->dev, 1);
  261. pm_runtime_get_noresume(&pdev->dev);
  262. ret = sdhci_add_host(host);
  263. if (ret) {
  264. dev_err(&pdev->dev, "failed to add host\n");
  265. pm_runtime_forbid(&pdev->dev);
  266. pm_runtime_disable(&pdev->dev);
  267. goto err_add_host;
  268. }
  269. platform_set_drvdata(pdev, host);
  270. if (pdata->pm_caps & MMC_PM_KEEP_POWER) {
  271. device_init_wakeup(&pdev->dev, 1);
  272. host->mmc->pm_flags |= MMC_PM_WAKE_SDIO_IRQ;
  273. } else {
  274. device_init_wakeup(&pdev->dev, 0);
  275. }
  276. pm_runtime_put_autosuspend(&pdev->dev);
  277. return 0;
  278. err_add_host:
  279. clk_disable_unprepare(clk);
  280. clk_put(clk);
  281. err_cd_req:
  282. err_clk_get:
  283. sdhci_pltfm_free(pdev);
  284. kfree(pxa);
  285. return ret;
  286. }
  287. static int sdhci_pxav3_remove(struct platform_device *pdev)
  288. {
  289. struct sdhci_host *host = platform_get_drvdata(pdev);
  290. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  291. struct sdhci_pxa *pxa = pltfm_host->priv;
  292. pm_runtime_get_sync(&pdev->dev);
  293. sdhci_remove_host(host, 1);
  294. pm_runtime_disable(&pdev->dev);
  295. clk_disable_unprepare(pltfm_host->clk);
  296. clk_put(pltfm_host->clk);
  297. sdhci_pltfm_free(pdev);
  298. kfree(pxa);
  299. platform_set_drvdata(pdev, NULL);
  300. return 0;
  301. }
  302. #ifdef CONFIG_PM_SLEEP
  303. static int sdhci_pxav3_suspend(struct device *dev)
  304. {
  305. int ret;
  306. struct sdhci_host *host = dev_get_drvdata(dev);
  307. pm_runtime_get_sync(dev);
  308. ret = sdhci_suspend_host(host);
  309. pm_runtime_mark_last_busy(dev);
  310. pm_runtime_put_autosuspend(dev);
  311. return ret;
  312. }
  313. static int sdhci_pxav3_resume(struct device *dev)
  314. {
  315. int ret;
  316. struct sdhci_host *host = dev_get_drvdata(dev);
  317. pm_runtime_get_sync(dev);
  318. ret = sdhci_resume_host(host);
  319. pm_runtime_mark_last_busy(dev);
  320. pm_runtime_put_autosuspend(dev);
  321. return ret;
  322. }
  323. #endif
  324. #ifdef CONFIG_PM_RUNTIME
  325. static int sdhci_pxav3_runtime_suspend(struct device *dev)
  326. {
  327. struct sdhci_host *host = dev_get_drvdata(dev);
  328. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  329. unsigned long flags;
  330. if (pltfm_host->clk) {
  331. spin_lock_irqsave(&host->lock, flags);
  332. host->runtime_suspended = true;
  333. spin_unlock_irqrestore(&host->lock, flags);
  334. clk_disable_unprepare(pltfm_host->clk);
  335. }
  336. return 0;
  337. }
  338. static int sdhci_pxav3_runtime_resume(struct device *dev)
  339. {
  340. struct sdhci_host *host = dev_get_drvdata(dev);
  341. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  342. unsigned long flags;
  343. if (pltfm_host->clk) {
  344. clk_prepare_enable(pltfm_host->clk);
  345. spin_lock_irqsave(&host->lock, flags);
  346. host->runtime_suspended = false;
  347. spin_unlock_irqrestore(&host->lock, flags);
  348. }
  349. return 0;
  350. }
  351. #endif
  352. #ifdef CONFIG_PM
  353. static const struct dev_pm_ops sdhci_pxav3_pmops = {
  354. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume)
  355. SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend,
  356. sdhci_pxav3_runtime_resume, NULL)
  357. };
  358. #define SDHCI_PXAV3_PMOPS (&sdhci_pxav3_pmops)
  359. #else
  360. #define SDHCI_PXAV3_PMOPS NULL
  361. #endif
  362. static struct platform_driver sdhci_pxav3_driver = {
  363. .driver = {
  364. .name = "sdhci-pxav3",
  365. #ifdef CONFIG_OF
  366. .of_match_table = sdhci_pxav3_of_match,
  367. #endif
  368. .owner = THIS_MODULE,
  369. .pm = SDHCI_PXAV3_PMOPS,
  370. },
  371. .probe = sdhci_pxav3_probe,
  372. .remove = sdhci_pxav3_remove,
  373. };
  374. module_platform_driver(sdhci_pxav3_driver);
  375. MODULE_DESCRIPTION("SDHCI driver for pxav3");
  376. MODULE_AUTHOR("Marvell International Ltd.");
  377. MODULE_LICENSE("GPL v2");