sdhci-pxav3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 int armada_38x_quirks(struct platform_device *pdev,
  99. struct sdhci_host *host)
  100. {
  101. struct device_node *np = pdev->dev.of_node;
  102. host->quirks |= SDHCI_QUIRK_MISSING_CAPS;
  103. /*
  104. * According to erratum 'FE-2946959' both SDR50 and DDR50
  105. * modes require specific clock adjustments in SDIO3
  106. * Configuration register, if the adjustment is not done,
  107. * remove them from the capabilities.
  108. */
  109. host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  110. host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
  111. /*
  112. * According to erratum 'ERR-7878951' Armada 38x SDHCI
  113. * controller has different capabilities than the ones shown
  114. * in its registers
  115. */
  116. host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  117. if (of_property_read_bool(np, "no-1-8-v")) {
  118. host->caps &= ~SDHCI_CAN_VDD_180;
  119. host->mmc->caps &= ~MMC_CAP_1_8V_DDR;
  120. } else {
  121. host->caps &= ~SDHCI_CAN_VDD_330;
  122. }
  123. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_USE_SDR50_TUNING);
  124. return 0;
  125. }
  126. static void pxav3_reset(struct sdhci_host *host, u8 mask)
  127. {
  128. struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
  129. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  130. sdhci_reset(host, mask);
  131. if (mask == SDHCI_RESET_ALL) {
  132. /*
  133. * tune timing of read data/command when crc error happen
  134. * no performance impact
  135. */
  136. if (pdata && 0 != pdata->clk_delay_cycles) {
  137. u16 tmp;
  138. tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  139. tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
  140. << SDCLK_DELAY_SHIFT;
  141. tmp |= SDCLK_SEL;
  142. writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
  143. }
  144. }
  145. }
  146. #define MAX_WAIT_COUNT 5
  147. static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
  148. {
  149. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  150. struct sdhci_pxa *pxa = pltfm_host->priv;
  151. u16 tmp;
  152. int count;
  153. if (pxa->power_mode == MMC_POWER_UP
  154. && power_mode == MMC_POWER_ON) {
  155. dev_dbg(mmc_dev(host->mmc),
  156. "%s: slot->power_mode = %d,"
  157. "ios->power_mode = %d\n",
  158. __func__,
  159. pxa->power_mode,
  160. power_mode);
  161. /* set we want notice of when 74 clocks are sent */
  162. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  163. tmp |= SDCE_MISC_INT_EN;
  164. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  165. /* start sending the 74 clocks */
  166. tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
  167. tmp |= SDCFG_GEN_PAD_CLK_ON;
  168. writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
  169. /* slowest speed is about 100KHz or 10usec per clock */
  170. udelay(740);
  171. count = 0;
  172. while (count++ < MAX_WAIT_COUNT) {
  173. if ((readw(host->ioaddr + SD_CE_ATA_2)
  174. & SDCE_MISC_INT) == 0)
  175. break;
  176. udelay(10);
  177. }
  178. if (count == MAX_WAIT_COUNT)
  179. dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
  180. /* clear the interrupt bit if posted */
  181. tmp = readw(host->ioaddr + SD_CE_ATA_2);
  182. tmp |= SDCE_MISC_INT;
  183. writew(tmp, host->ioaddr + SD_CE_ATA_2);
  184. }
  185. pxa->power_mode = power_mode;
  186. }
  187. static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
  188. {
  189. u16 ctrl_2;
  190. /*
  191. * Set V18_EN -- UHS modes do not work without this.
  192. * does not change signaling voltage
  193. */
  194. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  195. /* Select Bus Speed Mode for host */
  196. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  197. switch (uhs) {
  198. case MMC_TIMING_UHS_SDR12:
  199. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  200. break;
  201. case MMC_TIMING_UHS_SDR25:
  202. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  203. break;
  204. case MMC_TIMING_UHS_SDR50:
  205. ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
  206. break;
  207. case MMC_TIMING_UHS_SDR104:
  208. ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
  209. break;
  210. case MMC_TIMING_UHS_DDR50:
  211. ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
  212. break;
  213. }
  214. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  215. dev_dbg(mmc_dev(host->mmc),
  216. "%s uhs = %d, ctrl_2 = %04X\n",
  217. __func__, uhs, ctrl_2);
  218. }
  219. static const struct sdhci_ops pxav3_sdhci_ops = {
  220. .set_clock = sdhci_set_clock,
  221. .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
  222. .get_max_clock = sdhci_pltfm_clk_get_max_clock,
  223. .set_bus_width = sdhci_set_bus_width,
  224. .reset = pxav3_reset,
  225. .set_uhs_signaling = pxav3_set_uhs_signaling,
  226. };
  227. static struct sdhci_pltfm_data sdhci_pxav3_pdata = {
  228. .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
  229. | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
  230. | SDHCI_QUIRK_32BIT_ADMA_SIZE
  231. | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
  232. .ops = &pxav3_sdhci_ops,
  233. };
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id sdhci_pxav3_of_match[] = {
  236. {
  237. .compatible = "mrvl,pxav3-mmc",
  238. },
  239. {
  240. .compatible = "marvell,armada-380-sdhci",
  241. },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
  245. static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  246. {
  247. struct sdhci_pxa_platdata *pdata;
  248. struct device_node *np = dev->of_node;
  249. u32 clk_delay_cycles;
  250. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  251. if (!pdata)
  252. return NULL;
  253. if (!of_property_read_u32(np, "mrvl,clk-delay-cycles",
  254. &clk_delay_cycles))
  255. pdata->clk_delay_cycles = clk_delay_cycles;
  256. return pdata;
  257. }
  258. #else
  259. static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
  260. {
  261. return NULL;
  262. }
  263. #endif
  264. static int sdhci_pxav3_probe(struct platform_device *pdev)
  265. {
  266. struct sdhci_pltfm_host *pltfm_host;
  267. struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
  268. struct device *dev = &pdev->dev;
  269. struct device_node *np = pdev->dev.of_node;
  270. struct sdhci_host *host = NULL;
  271. struct sdhci_pxa *pxa = NULL;
  272. const struct of_device_id *match;
  273. int ret;
  274. struct clk *clk;
  275. pxa = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_pxa), GFP_KERNEL);
  276. if (!pxa)
  277. return -ENOMEM;
  278. host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, 0);
  279. if (IS_ERR(host))
  280. return PTR_ERR(host);
  281. /* enable 1/8V DDR capable */
  282. host->mmc->caps |= MMC_CAP_1_8V_DDR;
  283. pltfm_host = sdhci_priv(host);
  284. pltfm_host->priv = pxa;
  285. clk = devm_clk_get(dev, NULL);
  286. if (IS_ERR(clk)) {
  287. dev_err(dev, "failed to get io clock\n");
  288. ret = PTR_ERR(clk);
  289. goto err_clk_get;
  290. }
  291. pltfm_host->clk = clk;
  292. clk_prepare_enable(clk);
  293. if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) {
  294. ret = armada_38x_quirks(pdev, host);
  295. if (ret < 0)
  296. goto err_clk_get;
  297. ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info());
  298. if (ret < 0)
  299. goto err_mbus_win;
  300. }
  301. match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
  302. if (match) {
  303. ret = mmc_of_parse(host->mmc);
  304. if (ret)
  305. goto err_of_parse;
  306. sdhci_get_of_property(pdev);
  307. pdata = pxav3_get_mmc_pdata(dev);
  308. } else if (pdata) {
  309. /* on-chip device */
  310. if (pdata->flags & PXA_FLAG_CARD_PERMANENT)
  311. host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  312. /* If slot design supports 8 bit data, indicate this to MMC. */
  313. if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
  314. host->mmc->caps |= MMC_CAP_8_BIT_DATA;
  315. if (pdata->quirks)
  316. host->quirks |= pdata->quirks;
  317. if (pdata->quirks2)
  318. host->quirks2 |= pdata->quirks2;
  319. if (pdata->host_caps)
  320. host->mmc->caps |= pdata->host_caps;
  321. if (pdata->host_caps2)
  322. host->mmc->caps2 |= pdata->host_caps2;
  323. if (pdata->pm_caps)
  324. host->mmc->pm_caps |= pdata->pm_caps;
  325. if (gpio_is_valid(pdata->ext_cd_gpio)) {
  326. ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio,
  327. 0);
  328. if (ret) {
  329. dev_err(mmc_dev(host->mmc),
  330. "failed to allocate card detect gpio\n");
  331. goto err_cd_req;
  332. }
  333. }
  334. }
  335. pm_runtime_get_noresume(&pdev->dev);
  336. pm_runtime_set_active(&pdev->dev);
  337. pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS);
  338. pm_runtime_use_autosuspend(&pdev->dev);
  339. pm_runtime_enable(&pdev->dev);
  340. pm_suspend_ignore_children(&pdev->dev, 1);
  341. ret = sdhci_add_host(host);
  342. if (ret) {
  343. dev_err(&pdev->dev, "failed to add host\n");
  344. goto err_add_host;
  345. }
  346. platform_set_drvdata(pdev, host);
  347. if (host->mmc->pm_caps & MMC_PM_KEEP_POWER) {
  348. device_init_wakeup(&pdev->dev, 1);
  349. host->mmc->pm_flags |= MMC_PM_WAKE_SDIO_IRQ;
  350. } else {
  351. device_init_wakeup(&pdev->dev, 0);
  352. }
  353. pm_runtime_put_autosuspend(&pdev->dev);
  354. return 0;
  355. err_add_host:
  356. pm_runtime_disable(&pdev->dev);
  357. pm_runtime_put_noidle(&pdev->dev);
  358. err_of_parse:
  359. err_cd_req:
  360. err_mbus_win:
  361. clk_disable_unprepare(clk);
  362. err_clk_get:
  363. sdhci_pltfm_free(pdev);
  364. return ret;
  365. }
  366. static int sdhci_pxav3_remove(struct platform_device *pdev)
  367. {
  368. struct sdhci_host *host = platform_get_drvdata(pdev);
  369. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  370. pm_runtime_get_sync(&pdev->dev);
  371. sdhci_remove_host(host, 1);
  372. pm_runtime_disable(&pdev->dev);
  373. clk_disable_unprepare(pltfm_host->clk);
  374. sdhci_pltfm_free(pdev);
  375. return 0;
  376. }
  377. #ifdef CONFIG_PM_SLEEP
  378. static int sdhci_pxav3_suspend(struct device *dev)
  379. {
  380. int ret;
  381. struct sdhci_host *host = dev_get_drvdata(dev);
  382. pm_runtime_get_sync(dev);
  383. ret = sdhci_suspend_host(host);
  384. pm_runtime_mark_last_busy(dev);
  385. pm_runtime_put_autosuspend(dev);
  386. return ret;
  387. }
  388. static int sdhci_pxav3_resume(struct device *dev)
  389. {
  390. int ret;
  391. struct sdhci_host *host = dev_get_drvdata(dev);
  392. pm_runtime_get_sync(dev);
  393. ret = sdhci_resume_host(host);
  394. pm_runtime_mark_last_busy(dev);
  395. pm_runtime_put_autosuspend(dev);
  396. return ret;
  397. }
  398. #endif
  399. #ifdef CONFIG_PM_RUNTIME
  400. static int sdhci_pxav3_runtime_suspend(struct device *dev)
  401. {
  402. struct sdhci_host *host = dev_get_drvdata(dev);
  403. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  404. unsigned long flags;
  405. if (pltfm_host->clk) {
  406. spin_lock_irqsave(&host->lock, flags);
  407. host->runtime_suspended = true;
  408. spin_unlock_irqrestore(&host->lock, flags);
  409. clk_disable_unprepare(pltfm_host->clk);
  410. }
  411. return 0;
  412. }
  413. static int sdhci_pxav3_runtime_resume(struct device *dev)
  414. {
  415. struct sdhci_host *host = dev_get_drvdata(dev);
  416. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  417. unsigned long flags;
  418. if (pltfm_host->clk) {
  419. clk_prepare_enable(pltfm_host->clk);
  420. spin_lock_irqsave(&host->lock, flags);
  421. host->runtime_suspended = false;
  422. spin_unlock_irqrestore(&host->lock, flags);
  423. }
  424. return 0;
  425. }
  426. #endif
  427. #ifdef CONFIG_PM
  428. static const struct dev_pm_ops sdhci_pxav3_pmops = {
  429. SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume)
  430. SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend,
  431. sdhci_pxav3_runtime_resume, NULL)
  432. };
  433. #define SDHCI_PXAV3_PMOPS (&sdhci_pxav3_pmops)
  434. #else
  435. #define SDHCI_PXAV3_PMOPS NULL
  436. #endif
  437. static struct platform_driver sdhci_pxav3_driver = {
  438. .driver = {
  439. .name = "sdhci-pxav3",
  440. #ifdef CONFIG_OF
  441. .of_match_table = sdhci_pxav3_of_match,
  442. #endif
  443. .pm = SDHCI_PXAV3_PMOPS,
  444. },
  445. .probe = sdhci_pxav3_probe,
  446. .remove = sdhci_pxav3_remove,
  447. };
  448. module_platform_driver(sdhci_pxav3_driver);
  449. MODULE_DESCRIPTION("SDHCI driver for pxav3");
  450. MODULE_AUTHOR("Marvell International Ltd.");
  451. MODULE_LICENSE("GPL v2");