sdhci-pxav3.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 <linux/mbus.h>
  38. #include "sdhci.h"
  39. #include "sdhci-pltfm.h"
  40. #define PXAV3_RPM_DELAY_MS 50
  41. #define SD_CLOCK_BURST_SIZE_SETUP 0x10A
  42. #define SDCLK_SEL 0x100
  43. #define SDCLK_DELAY_SHIFT 9
  44. #define SDCLK_DELAY_MASK 0x1f
  45. #define SD_CFG_FIFO_PARAM 0x100
  46. #define SDCFG_GEN_PAD_CLK_ON (1<<6)
  47. #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
  48. #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
  49. #define SD_SPI_MODE 0x108
  50. #define SD_CE_ATA_1 0x10C
  51. #define SD_CE_ATA_2 0x10E
  52. #define SDCE_MISC_INT (1<<2)
  53. #define SDCE_MISC_INT_EN (1<<1)
  54. /*
  55. * These registers are relative to the second register region, for the
  56. * MBus bridge.
  57. */
  58. #define SDHCI_WINDOW_CTRL(i) (0x80 + ((i) << 3))
  59. #define SDHCI_WINDOW_BASE(i) (0x84 + ((i) << 3))
  60. #define SDHCI_MAX_WIN_NUM 8
  61. static int mv_conf_mbus_windows(struct platform_device *pdev,
  62. const struct mbus_dram_target_info *dram)
  63. {
  64. int i;
  65. void __iomem *regs;
  66. struct resource *res;
  67. if (!dram) {
  68. dev_err(&pdev->dev, "no mbus dram info\n");
  69. return -EINVAL;
  70. }
  71. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  72. if (!res) {
  73. dev_err(&pdev->dev, "cannot get mbus registers\n");
  74. return -EINVAL;
  75. }
  76. regs = ioremap(res->start, resource_size(res));
  77. if (!regs) {
  78. dev_err(&pdev->dev, "cannot map mbus registers\n");
  79. return -ENOMEM;
  80. }
  81. for (i = 0; i < SDHCI_MAX_WIN_NUM; i++) {
  82. writel(0, regs + SDHCI_WINDOW_CTRL(i));
  83. writel(0, regs + SDHCI_WINDOW_BASE(i));
  84. }
  85. for (i = 0; i < dram->num_cs; i++) {
  86. const struct mbus_dram_window *cs = dram->cs + i;
  87. /* Write size, attributes and target id to control register */
  88. writel(((cs->size - 1) & 0xffff0000) |
  89. (cs->mbus_attr << 8) |
  90. (dram->mbus_dram_target_id << 4) | 1,
  91. regs + SDHCI_WINDOW_CTRL(i));
  92. /* Write base address to base register */
  93. writel(cs->base, regs + SDHCI_WINDOW_BASE(i));
  94. }
  95. iounmap(regs);
  96. return 0;
  97. }
  98. static void pxav3_reset(struct sdhci_host *host, u8 mask)
  99. {
  100. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  101. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  102. sdhci_reset(host, mask);
  103. if (mask == SDHCI_RESET_ALL) {
  104. /*
  105. * tune timing of read data/command when crc error happen
  106. * no performance impact
  107. */
  108. if (pdata && 0 != pdata->clk_delay_cycles) {
  109. u16 tmp;
  110. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  111. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  112. << SDCLK_DELAY_SHIFT;
  113. tmp |= SDCLK_SEL;
  114. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  115. }
  116. }
  117. }
  118. #define MAX_WAIT_COUNT 5
  119. static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  120. {
  121. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  122. struct sdhci_pxa *pxa = pltfm_host->priv;
  123. u16 tmp;
  124. int count;
  125. if (pxa->power_mode == MMC_POWER_UP
  126. && power_mode == MMC_POWER_ON) {
  127. dev_dbg(mmc_dev(host->mmc),
  128. "%s: slot->power_mode = %d,"
  129. "ios->power_mode = %d\n",
  130. __func__,
  131. pxa->power_mode,
  132. power_mode);
  133. /* set we want notice of when 74 clocks are sent */
  134. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  135. tmp |= SDCE_MISC_INT_EN;
  136. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  137. /* start sending the 74 clocks */
  138. tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
  139. tmp |= SDCFG_GEN_PAD_CLK_ON;
  140. writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
  141. /* slowest speed is about 100KHz or 10usec per clock */
  142. udelay(740);
  143. count = 0;
  144. while (count++ < MAX_WAIT_COUNT) {
  145. if ((readw(host->ioaddr + SD_CE_ATA_2)
  146. & SDCE_MISC_INT) == 0)
  147. break;
  148. udelay(10);
  149. }
  150. if (count == MAX_WAIT_COUNT)
  151. dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
  152. /* clear the interrupt bit if posted */
  153. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  154. tmp |= SDCE_MISC_INT;
  155. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  156. }
  157. pxa->power_mode = power_mode;
  158. }
  159. static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
  160. {
  161. u16 ctrl_2;
  162. /*
  163. * Set V18_EN -- UHS modes do not work without this.
  164. * does not change signaling voltage
  165. */
  166. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  167. /* Select Bus Speed Mode for host */
  168. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  169. switch (uhs) {
  170. case MMC_TIMING_UHS_SDR12:
  171. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  172. break;
  173. case MMC_TIMING_UHS_SDR25:
  174. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  175. break;
  176. case MMC_TIMING_UHS_SDR50:
  177. ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
  178. break;
  179. case MMC_TIMING_UHS_SDR104:
  180. ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
  181. break;
  182. case MMC_TIMING_UHS_DDR50:
  183. ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
  184. break;
  185. }
  186. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  187. dev_dbg(mmc_dev(host->mmc),
  188. "%s uhs = %d, ctrl_2 = %04X\n",
  189. __func__, uhs, ctrl_2);
  190. }
  191. static const struct sdhci_ops pxav3_sdhci_ops = {
  192. .set_clock = sdhci_set_clock,
  193. .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
  194. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  195. .set_bus_width = sdhci_set_bus_width,
  196. .reset = pxav3_reset,
  197. .set_uhs_signaling = pxav3_set_uhs_signaling,
  198. };
  199. static struct sdhci_pltfm_data sdhci_pxav3_pdata = {
  200. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
  201. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  202. | SDHCI_QUIRK_32BIT_ADMA_SIZE
  203. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  204. .ops = &pxav3_sdhci_ops,
  205. };
  206. #ifdef CONFIG_OF
  207. static const struct of_device_id sdhci_pxav3_of_match[] = {
  208. {
  209. .compatible = "mrvl,pxav3-mmc",
  210. },
  211. {
  212. .compatible = "marvell,armada-380-sdhci",
  213. },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
  217. static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  218. {
  219. struct sdhci_pxa_platdata *pdata;
  220. struct device_node *np = dev->of_node;
  221. u32 clk_delay_cycles;
  222. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  223. if (!pdata)
  224. return NULL;
  225. of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles);
  226. if (clk_delay_cycles > 0)
  227. pdata->clk_delay_cycles = clk_delay_cycles;
  228. return pdata;
  229. }
  230. #else
  231. static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  232. {
  233. return NULL;
  234. }
  235. #endif
  236. static int sdhci_pxav3_probe(struct platform_device *pdev)
  237. {
  238. struct sdhci_pltfm_host *pltfm_host;
  239. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  240. struct device *dev = &pdev->dev;
  241. struct device_node *np = pdev->dev.of_node;
  242. struct sdhci_host *host = NULL;
  243. struct sdhci_pxa *pxa = NULL;
  244. const struct of_device_id *match;
  245. int ret;
  246. struct clk *clk;
  247. pxa = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_pxa), GFP_KERNEL);
  248. if (!pxa)
  249. return -ENOMEM;
  250. host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, 0);
  251. if (IS_ERR(host))
  252. return PTR_ERR(host);
  253. if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) {
  254. ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info());
  255. if (ret < 0)
  256. goto err_mbus_win;
  257. }
  258. pltfm_host = sdhci_priv(host);
  259. pltfm_host->priv = pxa;
  260. clk = devm_clk_get(dev, NULL);
  261. if (IS_ERR(clk)) {
  262. dev_err(dev, "failed to get io clock\n");
  263. ret = PTR_ERR(clk);
  264. goto err_clk_get;
  265. }
  266. pltfm_host->clk = clk;
  267. clk_prepare_enable(clk);
  268. /* enable 1/8V DDR capable */
  269. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  270. match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
  271. if (match) {
  272. ret = mmc_of_parse(host->mmc);
  273. if (ret)
  274. goto err_of_parse;
  275. sdhci_get_of_property(pdev);
  276. pdata = pxav3_get_mmc_pdata(dev);
  277. } else if (pdata) {
  278. /* on-chip device */
  279. if (pdata->flags & PXA_FLAG_CARD_PERMANENT)
  280. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  281. /* If slot design supports 8 bit data, indicate this to MMC. */
  282. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  283. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  284. if (pdata->quirks)
  285. host->quirks |= pdata->quirks;
  286. if (pdata->quirks2)
  287. host->quirks2 |= pdata->quirks2;
  288. if (pdata->host_caps)
  289. host->mmc->caps |= pdata->host_caps;
  290. if (pdata->host_caps2)
  291. host->mmc->caps2 |= pdata->host_caps2;
  292. if (pdata->pm_caps)
  293. host->mmc->pm_caps |= pdata->pm_caps;
  294. if (gpio_is_valid(pdata->ext_cd_gpio)) {
  295. ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio,
  296. 0);
  297. if (ret) {
  298. dev_err(mmc_dev(host->mmc),
  299. "failed to allocate card detect gpio\n");
  300. goto err_cd_req;
  301. }
  302. }
  303. }
  304. pm_runtime_enable(&pdev->dev);
  305. pm_runtime_get_sync(&pdev->dev);
  306. pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS);
  307. pm_runtime_use_autosuspend(&pdev->dev);
  308. pm_suspend_ignore_children(&pdev->dev, 1);
  309. ret = sdhci_add_host(host);
  310. if (ret) {
  311. dev_err(&pdev->dev, "failed to add host\n");
  312. goto err_add_host;
  313. }
  314. platform_set_drvdata(pdev, host);
  315. if (host->mmc->pm_caps & MMC_PM_KEEP_POWER) {
  316. device_init_wakeup(&pdev->dev, 1);
  317. host->mmc->pm_flags |= MMC_PM_WAKE_SDIO_IRQ;
  318. } else {
  319. device_init_wakeup(&pdev->dev, 0);
  320. }
  321. pm_runtime_put_autosuspend(&pdev->dev);
  322. return 0;
  323. err_add_host:
  324. pm_runtime_put_sync(&pdev->dev);
  325. pm_runtime_disable(&pdev->dev);
  326. err_of_parse:
  327. err_cd_req:
  328. clk_disable_unprepare(clk);
  329. err_clk_get:
  330. err_mbus_win:
  331. sdhci_pltfm_free(pdev);
  332. return ret;
  333. }
  334. static int sdhci_pxav3_remove(struct platform_device *pdev)
  335. {
  336. struct sdhci_host *host = platform_get_drvdata(pdev);
  337. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  338. pm_runtime_get_sync(&pdev->dev);
  339. sdhci_remove_host(host, 1);
  340. pm_runtime_disable(&pdev->dev);
  341. clk_disable_unprepare(pltfm_host->clk);
  342. sdhci_pltfm_free(pdev);
  343. return 0;
  344. }
  345. #ifdef CONFIG_PM_SLEEP
  346. static int sdhci_pxav3_suspend(struct device *dev)
  347. {
  348. int ret;
  349. struct sdhci_host *host = dev_get_drvdata(dev);
  350. pm_runtime_get_sync(dev);
  351. ret = sdhci_suspend_host(host);
  352. pm_runtime_mark_last_busy(dev);
  353. pm_runtime_put_autosuspend(dev);
  354. return ret;
  355. }
  356. static int sdhci_pxav3_resume(struct device *dev)
  357. {
  358. int ret;
  359. struct sdhci_host *host = dev_get_drvdata(dev);
  360. pm_runtime_get_sync(dev);
  361. ret = sdhci_resume_host(host);
  362. pm_runtime_mark_last_busy(dev);
  363. pm_runtime_put_autosuspend(dev);
  364. return ret;
  365. }
  366. #endif
  367. #ifdef CONFIG_PM
  368. static int sdhci_pxav3_runtime_suspend(struct device *dev)
  369. {
  370. struct sdhci_host *host = dev_get_drvdata(dev);
  371. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  372. unsigned long flags;
  373. if (pltfm_host->clk) {
  374. spin_lock_irqsave(&host->lock, flags);
  375. host->runtime_suspended = true;
  376. spin_unlock_irqrestore(&host->lock, flags);
  377. clk_disable_unprepare(pltfm_host->clk);
  378. }
  379. return 0;
  380. }
  381. static int sdhci_pxav3_runtime_resume(struct device *dev)
  382. {
  383. struct sdhci_host *host = dev_get_drvdata(dev);
  384. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  385. unsigned long flags;
  386. if (pltfm_host->clk) {
  387. clk_prepare_enable(pltfm_host->clk);
  388. spin_lock_irqsave(&host->lock, flags);
  389. host->runtime_suspended = false;
  390. spin_unlock_irqrestore(&host->lock, flags);
  391. }
  392. return 0;
  393. }
  394. #endif
  395. #ifdef CONFIG_PM
  396. static const struct dev_pm_ops sdhci_pxav3_pmops = {
  397. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume)
  398. SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend,
  399. sdhci_pxav3_runtime_resume, NULL)
  400. };
  401. #define SDHCI_PXAV3_PMOPS (&sdhci_pxav3_pmops)
  402. #else
  403. #define SDHCI_PXAV3_PMOPS NULL
  404. #endif
  405. static struct platform_driver sdhci_pxav3_driver = {
  406. .driver = {
  407. .name = "sdhci-pxav3",
  408. #ifdef CONFIG_OF
  409. .of_match_table = sdhci_pxav3_of_match,
  410. #endif
  411. .pm = SDHCI_PXAV3_PMOPS,
  412. },
  413. .probe = sdhci_pxav3_probe,
  414. .remove = sdhci_pxav3_remove,
  415. };
  416. module_platform_driver(sdhci_pxav3_driver);
  417. MODULE_DESCRIPTION("SDHCI driver for pxav3");
  418. MODULE_AUTHOR("Marvell International Ltd.");
  419. MODULE_LICENSE("GPL v2");