blob: e55037ceda734ff0e581fc8e2eecb664d882363f [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +08002/*
3 * Copyright (C) 2010 Marvell International Ltd.
4 * Zhangfei Gao <zhangfei.gao@marvell.com>
5 * Kevin Wang <dwang4@marvell.com>
6 * Mingwei Wang <mwwang@marvell.com>
7 * Philip Rakity <prakity@marvell.com>
8 * Mark Brown <markb@marvell.com>
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +08009 */
10#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/clk.h>
14#include <linux/io.h>
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +080015#include <linux/mmc/card.h>
16#include <linux/mmc/host.h>
Zhangfei Gaobfed3452011-06-20 22:11:52 +080017#include <linux/platform_data/pxa_sdhci.h>
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +080018#include <linux/slab.h>
19#include <linux/delay.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
Chris Ballb6503522012-04-10 22:34:33 -040021#include <linux/of.h>
22#include <linux/of_device.h>
Kevin Liubb691ae2013-02-01 17:48:30 +080023#include <linux/pm.h>
24#include <linux/pm_runtime.h>
Marcin Wojtas5491ce32014-02-18 16:08:29 +010025#include <linux/mbus.h>
Chris Ballb6503522012-04-10 22:34:33 -040026
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +080027#include "sdhci.h"
28#include "sdhci-pltfm.h"
29
Kevin Liubb691ae2013-02-01 17:48:30 +080030#define PXAV3_RPM_DELAY_MS 50
31
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +080032#define SD_CLOCK_BURST_SIZE_SETUP 0x10A
33#define SDCLK_SEL 0x100
34#define SDCLK_DELAY_SHIFT 9
35#define SDCLK_DELAY_MASK 0x1f
36
37#define SD_CFG_FIFO_PARAM 0x100
38#define SDCFG_GEN_PAD_CLK_ON (1<<6)
39#define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
40#define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
41
42#define SD_SPI_MODE 0x108
43#define SD_CE_ATA_1 0x10C
44
45#define SD_CE_ATA_2 0x10E
46#define SDCE_MISC_INT (1<<2)
47#define SDCE_MISC_INT_EN (1<<1)
48
Sebastian Hesselbarthcc9571e2014-10-21 11:22:35 +020049struct sdhci_pxa {
Sebastian Hesselbarth8afdc9c2014-10-21 11:22:40 +020050 struct clk *clk_core;
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +020051 struct clk *clk_io;
Sebastian Hesselbarthcc9571e2014-10-21 11:22:35 +020052 u8 power_mode;
Marcin Wojtas11400112015-01-29 12:36:27 +010053 void __iomem *sdio3_conf_reg;
Sebastian Hesselbarthcc9571e2014-10-21 11:22:35 +020054};
55
Marcin Wojtas5491ce32014-02-18 16:08:29 +010056/*
57 * These registers are relative to the second register region, for the
58 * MBus bridge.
59 */
60#define SDHCI_WINDOW_CTRL(i) (0x80 + ((i) << 3))
61#define SDHCI_WINDOW_BASE(i) (0x84 + ((i) << 3))
62#define SDHCI_MAX_WIN_NUM 8
63
Marcin Wojtas11400112015-01-29 12:36:27 +010064/*
65 * Fields below belong to SDIO3 Configuration Register (third register
66 * region for the Armada 38x flavor)
67 */
68
69#define SDIO3_CONF_CLK_INV BIT(0)
70#define SDIO3_CONF_SD_FB_CLK BIT(2)
71
Marcin Wojtas5491ce32014-02-18 16:08:29 +010072static int mv_conf_mbus_windows(struct platform_device *pdev,
73 const struct mbus_dram_target_info *dram)
74{
75 int i;
76 void __iomem *regs;
77 struct resource *res;
78
79 if (!dram) {
80 dev_err(&pdev->dev, "no mbus dram info\n");
81 return -EINVAL;
82 }
83
84 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
85 if (!res) {
86 dev_err(&pdev->dev, "cannot get mbus registers\n");
87 return -EINVAL;
88 }
89
90 regs = ioremap(res->start, resource_size(res));
91 if (!regs) {
92 dev_err(&pdev->dev, "cannot map mbus registers\n");
93 return -ENOMEM;
94 }
95
96 for (i = 0; i < SDHCI_MAX_WIN_NUM; i++) {
97 writel(0, regs + SDHCI_WINDOW_CTRL(i));
98 writel(0, regs + SDHCI_WINDOW_BASE(i));
99 }
100
101 for (i = 0; i < dram->num_cs; i++) {
102 const struct mbus_dram_window *cs = dram->cs + i;
103
104 /* Write size, attributes and target id to control register */
105 writel(((cs->size - 1) & 0xffff0000) |
106 (cs->mbus_attr << 8) |
107 (dram->mbus_dram_target_id << 4) | 1,
108 regs + SDHCI_WINDOW_CTRL(i));
109 /* Write base address to base register */
110 writel(cs->base, regs + SDHCI_WINDOW_BASE(i));
111 }
112
113 iounmap(regs);
114
115 return 0;
116}
117
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100118static int armada_38x_quirks(struct platform_device *pdev,
119 struct sdhci_host *host)
Gregory CLEMENTd4b803c2015-01-29 12:36:24 +0100120{
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100121 struct device_node *np = pdev->dev.of_node;
Marcin Wojtas11400112015-01-29 12:36:27 +0100122 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800123 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Marcin Wojtas11400112015-01-29 12:36:27 +0100124 struct resource *res;
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100125
Nadav Haklai5de76bf2015-10-06 03:22:35 +0200126 host->quirks &= ~SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
Gregory CLEMENTd4b803c2015-01-29 12:36:24 +0100127 host->quirks |= SDHCI_QUIRK_MISSING_CAPS;
Russell King0ca33b42016-01-26 13:40:47 +0000128
129 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
130 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
131
Marcin Wojtas11400112015-01-29 12:36:27 +0100132 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
133 "conf-sdio3");
134 if (res) {
135 pxa->sdio3_conf_reg = devm_ioremap_resource(&pdev->dev, res);
136 if (IS_ERR(pxa->sdio3_conf_reg))
137 return PTR_ERR(pxa->sdio3_conf_reg);
138 } else {
139 /*
140 * According to erratum 'FE-2946959' both SDR50 and DDR50
141 * modes require specific clock adjustments in SDIO3
142 * Configuration register, if the adjustment is not done,
143 * remove them from the capabilities.
144 */
Marcin Wojtas11400112015-01-29 12:36:27 +0100145 host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
146
147 dev_warn(&pdev->dev, "conf-sdio3 register not found: disabling SDR50 and DDR50 modes.\nConsider updating your dtb\n");
148 }
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100149
150 /*
151 * According to erratum 'ERR-7878951' Armada 38x SDHCI
152 * controller has different capabilities than the ones shown
153 * in its registers
154 */
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100155 if (of_property_read_bool(np, "no-1-8-v")) {
156 host->caps &= ~SDHCI_CAN_VDD_180;
157 host->mmc->caps &= ~MMC_CAP_1_8V_DDR;
158 } else {
159 host->caps &= ~SDHCI_CAN_VDD_330;
160 }
161 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_USE_SDR50_TUNING);
162
Gregory CLEMENTd4b803c2015-01-29 12:36:24 +0100163 return 0;
164}
165
Russell King03231f92014-04-25 12:57:12 +0100166static void pxav3_reset(struct sdhci_host *host, u8 mask)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800167{
168 struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
169 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
170
Russell King03231f92014-04-25 12:57:12 +0100171 sdhci_reset(host, mask);
172
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800173 if (mask == SDHCI_RESET_ALL) {
174 /*
175 * tune timing of read data/command when crc error happen
176 * no performance impact
177 */
178 if (pdata && 0 != pdata->clk_delay_cycles) {
179 u16 tmp;
180
181 tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
182 tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
183 << SDCLK_DELAY_SHIFT;
184 tmp |= SDCLK_SEL;
185 writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
186 }
187 }
188}
189
190#define MAX_WAIT_COUNT 5
191static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
192{
193 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800194 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800195 u16 tmp;
196 int count;
197
198 if (pxa->power_mode == MMC_POWER_UP
199 && power_mode == MMC_POWER_ON) {
200
201 dev_dbg(mmc_dev(host->mmc),
202 "%s: slot->power_mode = %d,"
203 "ios->power_mode = %d\n",
204 __func__,
205 pxa->power_mode,
206 power_mode);
207
208 /* set we want notice of when 74 clocks are sent */
209 tmp = readw(host->ioaddr + SD_CE_ATA_2);
210 tmp |= SDCE_MISC_INT_EN;
211 writew(tmp, host->ioaddr + SD_CE_ATA_2);
212
213 /* start sending the 74 clocks */
214 tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
215 tmp |= SDCFG_GEN_PAD_CLK_ON;
216 writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
217
218 /* slowest speed is about 100KHz or 10usec per clock */
219 udelay(740);
220 count = 0;
221
222 while (count++ < MAX_WAIT_COUNT) {
223 if ((readw(host->ioaddr + SD_CE_ATA_2)
224 & SDCE_MISC_INT) == 0)
225 break;
226 udelay(10);
227 }
228
229 if (count == MAX_WAIT_COUNT)
230 dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
231
232 /* clear the interrupt bit if posted */
233 tmp = readw(host->ioaddr + SD_CE_ATA_2);
234 tmp |= SDCE_MISC_INT;
235 writew(tmp, host->ioaddr + SD_CE_ATA_2);
236 }
237 pxa->power_mode = power_mode;
238}
239
Russell King13e64502014-04-25 12:59:20 +0100240static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800241{
Marcin Wojtas11400112015-01-29 12:36:27 +0100242 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800243 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800244 u16 ctrl_2;
245
246 /*
247 * Set V18_EN -- UHS modes do not work without this.
248 * does not change signaling voltage
249 */
250 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
251
252 /* Select Bus Speed Mode for host */
253 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
254 switch (uhs) {
255 case MMC_TIMING_UHS_SDR12:
256 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
257 break;
258 case MMC_TIMING_UHS_SDR25:
259 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
260 break;
261 case MMC_TIMING_UHS_SDR50:
262 ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
263 break;
264 case MMC_TIMING_UHS_SDR104:
265 ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
266 break;
Sebastian Hesselbarth668e84b2014-10-21 11:22:34 +0200267 case MMC_TIMING_MMC_DDR52:
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800268 case MMC_TIMING_UHS_DDR50:
269 ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
270 break;
271 }
272
Marcin Wojtas11400112015-01-29 12:36:27 +0100273 /*
274 * Update SDIO3 Configuration register according to erratum
275 * FE-2946959
276 */
277 if (pxa->sdio3_conf_reg) {
278 u8 reg_val = readb(pxa->sdio3_conf_reg);
279
280 if (uhs == MMC_TIMING_UHS_SDR50 ||
281 uhs == MMC_TIMING_UHS_DDR50) {
282 reg_val &= ~SDIO3_CONF_CLK_INV;
283 reg_val |= SDIO3_CONF_SD_FB_CLK;
Nadav Haklaifa796412015-10-06 03:22:36 +0200284 } else if (uhs == MMC_TIMING_MMC_HS) {
285 reg_val &= ~SDIO3_CONF_CLK_INV;
286 reg_val &= ~SDIO3_CONF_SD_FB_CLK;
Marcin Wojtas11400112015-01-29 12:36:27 +0100287 } else {
288 reg_val |= SDIO3_CONF_CLK_INV;
289 reg_val &= ~SDIO3_CONF_SD_FB_CLK;
290 }
291 writeb(reg_val, pxa->sdio3_conf_reg);
292 }
293
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800294 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
295 dev_dbg(mmc_dev(host->mmc),
296 "%s uhs = %d, ctrl_2 = %04X\n",
297 __func__, uhs, ctrl_2);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800298}
299
Adrian Hunter1dceb042016-03-29 12:45:43 +0300300static void pxav3_set_power(struct sdhci_host *host, unsigned char mode,
301 unsigned short vdd)
302{
303 struct mmc_host *mmc = host->mmc;
304 u8 pwr = host->pwr;
305
Adrian Hunter606d3132016-10-05 12:11:22 +0300306 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +0300307
308 if (host->pwr == pwr)
309 return;
310
311 if (host->pwr == 0)
312 vdd = 0;
313
Adrian Hunterd1e4f742017-03-20 19:50:37 +0200314 if (!IS_ERR(mmc->supply.vmmc))
Adrian Hunter1dceb042016-03-29 12:45:43 +0300315 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +0300316}
317
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100318static const struct sdhci_ops pxav3_sdhci_ops = {
Russell King17710592014-04-25 12:58:55 +0100319 .set_clock = sdhci_set_clock,
Adrian Hunter1dceb042016-03-29 12:45:43 +0300320 .set_power = pxav3_set_power,
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800321 .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
Lars-Peter Clausend005d942013-01-28 19:27:12 +0100322 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
Russell King2317f562014-04-25 12:57:07 +0100323 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100324 .reset = pxav3_reset,
Peter Griffinb3153762014-08-15 14:02:15 +0100325 .set_uhs_signaling = pxav3_set_uhs_signaling,
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800326};
327
Julia Lawalld35ade82017-08-07 11:50:40 +0200328static const struct sdhci_pltfm_data sdhci_pxav3_pdata = {
Kevin Liue0651622013-03-25 17:42:59 +0800329 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
Kevin Liu73b7afb2013-03-25 17:42:56 +0800330 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
331 | SDHCI_QUIRK_32BIT_ADMA_SIZE
332 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
333 .ops = &pxav3_sdhci_ops,
334};
335
Chris Ballb6503522012-04-10 22:34:33 -0400336#ifdef CONFIG_OF
337static const struct of_device_id sdhci_pxav3_of_match[] = {
338 {
339 .compatible = "mrvl,pxav3-mmc",
340 },
Marcin Wojtas5491ce32014-02-18 16:08:29 +0100341 {
342 .compatible = "marvell,armada-380-sdhci",
343 },
Chris Ballb6503522012-04-10 22:34:33 -0400344 {},
345};
346MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
347
348static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
349{
350 struct sdhci_pxa_platdata *pdata;
351 struct device_node *np = dev->of_node;
Chris Ballb6503522012-04-10 22:34:33 -0400352 u32 clk_delay_cycles;
353
354 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
355 if (!pdata)
356 return NULL;
357
Jisheng Zhang14460db2015-01-28 19:54:12 +0800358 if (!of_property_read_u32(np, "mrvl,clk-delay-cycles",
359 &clk_delay_cycles))
Chris Ballb6503522012-04-10 22:34:33 -0400360 pdata->clk_delay_cycles = clk_delay_cycles;
361
362 return pdata;
363}
364#else
365static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
366{
367 return NULL;
368}
369#endif
370
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500371static int sdhci_pxav3_probe(struct platform_device *pdev)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800372{
373 struct sdhci_pltfm_host *pltfm_host;
374 struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
375 struct device *dev = &pdev->dev;
Marcin Wojtas5491ce32014-02-18 16:08:29 +0100376 struct device_node *np = pdev->dev.of_node;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800377 struct sdhci_host *host = NULL;
378 struct sdhci_pxa *pxa = NULL;
Chris Ballb6503522012-04-10 22:34:33 -0400379 const struct of_device_id *match;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800380 int ret;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800381
Jisheng Zhangf599da42016-02-16 21:08:27 +0800382 host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, sizeof(*pxa));
Laurent Pinchart3df5b282014-07-16 11:53:42 +0200383 if (IS_ERR(host))
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800384 return PTR_ERR(host);
Marcin Wojtas5491ce32014-02-18 16:08:29 +0100385
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800386 pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800387 pxa = sdhci_pltfm_priv(pltfm_host);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800388
Sebastian Hesselbarth01ae1072014-10-21 11:22:39 +0200389 pxa->clk_io = devm_clk_get(dev, "io");
390 if (IS_ERR(pxa->clk_io))
391 pxa->clk_io = devm_clk_get(dev, NULL);
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200392 if (IS_ERR(pxa->clk_io)) {
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800393 dev_err(dev, "failed to get io clock\n");
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200394 ret = PTR_ERR(pxa->clk_io);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800395 goto err_clk_get;
396 }
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200397 pltfm_host->clk = pxa->clk_io;
398 clk_prepare_enable(pxa->clk_io);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800399
Sebastian Hesselbarth8afdc9c2014-10-21 11:22:40 +0200400 pxa->clk_core = devm_clk_get(dev, "core");
401 if (!IS_ERR(pxa->clk_core))
402 clk_prepare_enable(pxa->clk_core);
403
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100404 /* enable 1/8V DDR capable */
405 host->mmc->caps |= MMC_CAP_1_8V_DDR;
406
Thomas Petazzoniaa8165f2014-12-31 11:54:10 +0100407 if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) {
Marcin Wojtasa39128b2015-01-29 12:36:25 +0100408 ret = armada_38x_quirks(pdev, host);
Gregory CLEMENTd4b803c2015-01-29 12:36:24 +0100409 if (ret < 0)
Marcin Wojtas2162d9f42015-10-06 03:22:37 +0200410 goto err_mbus_win;
Thomas Petazzoniaa8165f2014-12-31 11:54:10 +0100411 ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info());
412 if (ret < 0)
413 goto err_mbus_win;
414 }
415
Chris Ballb6503522012-04-10 22:34:33 -0400416 match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
Kevin Liu943647f2013-03-25 17:42:58 +0800417 if (match) {
Simon Baatzd2cf6072013-06-09 22:14:15 +0200418 ret = mmc_of_parse(host->mmc);
419 if (ret)
420 goto err_of_parse;
Kevin Liu943647f2013-03-25 17:42:58 +0800421 sdhci_get_of_property(pdev);
Chris Ballb6503522012-04-10 22:34:33 -0400422 pdata = pxav3_get_mmc_pdata(dev);
Jingju Hou9cd76042015-07-23 17:56:23 +0800423 pdev->dev.platform_data = pdata;
Kevin Liu943647f2013-03-25 17:42:58 +0800424 } else if (pdata) {
Kevin Liuc844a462013-03-25 17:42:57 +0800425 /* on-chip device */
426 if (pdata->flags & PXA_FLAG_CARD_PERMANENT)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800427 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800428
429 /* If slot design supports 8 bit data, indicate this to MMC. */
430 if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
431 host->mmc->caps |= MMC_CAP_8_BIT_DATA;
432
433 if (pdata->quirks)
434 host->quirks |= pdata->quirks;
Kevin Liu7c52d7bb2012-10-17 19:04:48 +0800435 if (pdata->quirks2)
436 host->quirks2 |= pdata->quirks2;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800437 if (pdata->host_caps)
438 host->mmc->caps |= pdata->host_caps;
Chris Ball8f637952012-09-19 16:29:12 +0800439 if (pdata->host_caps2)
440 host->mmc->caps2 |= pdata->host_caps2;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800441 if (pdata->pm_caps)
442 host->mmc->pm_caps |= pdata->pm_caps;
443 }
444
Jisheng Zhang62cf9832015-01-04 23:15:47 +0800445 pm_runtime_get_noresume(&pdev->dev);
446 pm_runtime_set_active(&pdev->dev);
Kevin Liubb691ae2013-02-01 17:48:30 +0800447 pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS);
448 pm_runtime_use_autosuspend(&pdev->dev);
Jisheng Zhang62cf9832015-01-04 23:15:47 +0800449 pm_runtime_enable(&pdev->dev);
Kevin Liubb691ae2013-02-01 17:48:30 +0800450 pm_suspend_ignore_children(&pdev->dev, 1);
Kevin Liubb691ae2013-02-01 17:48:30 +0800451
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800452 ret = sdhci_add_host(host);
Jisheng Zhangfb8617e2018-05-25 15:15:09 +0800453 if (ret)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800454 goto err_add_host;
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800455
Jisheng Zhang83dc9fe2015-06-02 18:38:35 +0800456 if (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ)
Kevin Liu740b7a42013-01-14 14:38:53 -0500457 device_init_wakeup(&pdev->dev, 1);
Kevin Liu740b7a42013-01-14 14:38:53 -0500458
Kevin Liubb691ae2013-02-01 17:48:30 +0800459 pm_runtime_put_autosuspend(&pdev->dev);
460
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800461 return 0;
462
463err_add_host:
Daniel Drake0dcaa242013-06-27 11:46:29 -0400464 pm_runtime_disable(&pdev->dev);
Jisheng Zhang62cf9832015-01-04 23:15:47 +0800465 pm_runtime_put_noidle(&pdev->dev);
Xiang Wang87d21632014-07-16 15:50:09 +0800466err_of_parse:
Thomas Petazzoniaa8165f2014-12-31 11:54:10 +0100467err_mbus_win:
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200468 clk_disable_unprepare(pxa->clk_io);
Jisheng Zhangc25d9e12015-01-05 15:59:19 +0800469 clk_disable_unprepare(pxa->clk_core);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800470err_clk_get:
471 sdhci_pltfm_free(pdev);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800472 return ret;
473}
474
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500475static int sdhci_pxav3_remove(struct platform_device *pdev)
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800476{
477 struct sdhci_host *host = platform_get_drvdata(pdev);
478 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800479 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800480
Kevin Liubb691ae2013-02-01 17:48:30 +0800481 pm_runtime_get_sync(&pdev->dev);
Kevin Liubb691ae2013-02-01 17:48:30 +0800482 pm_runtime_disable(&pdev->dev);
Jisheng Zhang20f1f2d2015-01-04 23:15:48 +0800483 pm_runtime_put_noidle(&pdev->dev);
484
485 sdhci_remove_host(host, 1);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800486
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200487 clk_disable_unprepare(pxa->clk_io);
Jisheng Zhangc25d9e12015-01-05 15:59:19 +0800488 clk_disable_unprepare(pxa->clk_core);
Chris Ball8f637952012-09-19 16:29:12 +0800489
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800490 sdhci_pltfm_free(pdev);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800491
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800492 return 0;
493}
494
Kevin Liubb691ae2013-02-01 17:48:30 +0800495#ifdef CONFIG_PM_SLEEP
496static int sdhci_pxav3_suspend(struct device *dev)
497{
498 int ret;
499 struct sdhci_host *host = dev_get_drvdata(dev);
500
501 pm_runtime_get_sync(dev);
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200502 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
503 mmc_retune_needed(host->mmc);
Kevin Liubb691ae2013-02-01 17:48:30 +0800504 ret = sdhci_suspend_host(host);
505 pm_runtime_mark_last_busy(dev);
506 pm_runtime_put_autosuspend(dev);
507
508 return ret;
509}
510
511static int sdhci_pxav3_resume(struct device *dev)
512{
513 int ret;
514 struct sdhci_host *host = dev_get_drvdata(dev);
515
516 pm_runtime_get_sync(dev);
517 ret = sdhci_resume_host(host);
518 pm_runtime_mark_last_busy(dev);
519 pm_runtime_put_autosuspend(dev);
520
521 return ret;
522}
523#endif
524
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100525#ifdef CONFIG_PM
Kevin Liubb691ae2013-02-01 17:48:30 +0800526static int sdhci_pxav3_runtime_suspend(struct device *dev)
527{
528 struct sdhci_host *host = dev_get_drvdata(dev);
529 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800530 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Jisheng Zhang3bb10f62015-01-23 18:08:21 +0800531 int ret;
Kevin Liubb691ae2013-02-01 17:48:30 +0800532
Jisheng Zhang3bb10f62015-01-23 18:08:21 +0800533 ret = sdhci_runtime_suspend_host(host);
534 if (ret)
535 return ret;
Kevin Liubb691ae2013-02-01 17:48:30 +0800536
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200537 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
538 mmc_retune_needed(host->mmc);
539
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200540 clk_disable_unprepare(pxa->clk_io);
Sebastian Hesselbarth8afdc9c2014-10-21 11:22:40 +0200541 if (!IS_ERR(pxa->clk_core))
542 clk_disable_unprepare(pxa->clk_core);
Kevin Liubb691ae2013-02-01 17:48:30 +0800543
544 return 0;
545}
546
547static int sdhci_pxav3_runtime_resume(struct device *dev)
548{
549 struct sdhci_host *host = dev_get_drvdata(dev);
550 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhangf599da42016-02-16 21:08:27 +0800551 struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
Kevin Liubb691ae2013-02-01 17:48:30 +0800552
Sebastian Hesselbarth8c96a7a2014-10-21 11:22:38 +0200553 clk_prepare_enable(pxa->clk_io);
Sebastian Hesselbarth8afdc9c2014-10-21 11:22:40 +0200554 if (!IS_ERR(pxa->clk_core))
555 clk_prepare_enable(pxa->clk_core);
Kevin Liubb691ae2013-02-01 17:48:30 +0800556
Baolin Wangc6303c52019-07-25 11:14:22 +0800557 return sdhci_runtime_resume_host(host, 0);
Kevin Liubb691ae2013-02-01 17:48:30 +0800558}
559#endif
560
Kevin Liubb691ae2013-02-01 17:48:30 +0800561static const struct dev_pm_ops sdhci_pxav3_pmops = {
562 SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume)
563 SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend,
564 sdhci_pxav3_runtime_resume, NULL)
565};
566
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800567static struct platform_driver sdhci_pxav3_driver = {
568 .driver = {
569 .name = "sdhci-pxav3",
Axel Lin59d22302015-05-05 17:11:54 +0800570 .of_match_table = of_match_ptr(sdhci_pxav3_of_match),
Ulf Hanssona81ce772016-07-27 11:08:41 +0200571 .pm = &sdhci_pxav3_pmops,
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800572 },
573 .probe = sdhci_pxav3_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500574 .remove = sdhci_pxav3_remove,
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800575};
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800576
Axel Lind1f81a62011-11-26 12:55:43 +0800577module_platform_driver(sdhci_pxav3_driver);
Zhangfei Gaoa702c8a2011-06-08 17:41:57 +0800578
579MODULE_DESCRIPTION("SDHCI driver for pxav3");
580MODULE_AUTHOR("Marvell International Ltd.");
581MODULE_LICENSE("GPL v2");
582