19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a702c8abSZhangfei Gao /*
3a702c8abSZhangfei Gao * Copyright (C) 2010 Marvell International Ltd.
4a702c8abSZhangfei Gao * Zhangfei Gao <zhangfei.gao@marvell.com>
5a702c8abSZhangfei Gao * Kevin Wang <dwang4@marvell.com>
6a702c8abSZhangfei Gao * Mingwei Wang <mwwang@marvell.com>
7a702c8abSZhangfei Gao * Philip Rakity <prakity@marvell.com>
8a702c8abSZhangfei Gao * Mark Brown <markb@marvell.com>
9a702c8abSZhangfei Gao */
10a702c8abSZhangfei Gao #include <linux/err.h>
11a702c8abSZhangfei Gao #include <linux/init.h>
12a702c8abSZhangfei Gao #include <linux/platform_device.h>
13a702c8abSZhangfei Gao #include <linux/clk.h>
14a702c8abSZhangfei Gao #include <linux/io.h>
15a702c8abSZhangfei Gao #include <linux/mmc/card.h>
16a702c8abSZhangfei Gao #include <linux/mmc/host.h>
17bfed345eSZhangfei Gao #include <linux/platform_data/pxa_sdhci.h>
18a702c8abSZhangfei Gao #include <linux/slab.h>
19a702c8abSZhangfei Gao #include <linux/delay.h>
2088b47679SPaul Gortmaker #include <linux/module.h>
21b650352dSChris Ball #include <linux/of.h>
22b650352dSChris Ball #include <linux/of_device.h>
23bb691ae4SKevin Liu #include <linux/pm.h>
24bb691ae4SKevin Liu #include <linux/pm_runtime.h>
255491ce3fSMarcin Wojtas #include <linux/mbus.h>
26b650352dSChris Ball
27a702c8abSZhangfei Gao #include "sdhci.h"
28a702c8abSZhangfei Gao #include "sdhci-pltfm.h"
29a702c8abSZhangfei Gao
30bb691ae4SKevin Liu #define PXAV3_RPM_DELAY_MS 50
31bb691ae4SKevin Liu
32a702c8abSZhangfei Gao #define SD_CLOCK_BURST_SIZE_SETUP 0x10A
33a702c8abSZhangfei Gao #define SDCLK_SEL 0x100
34a702c8abSZhangfei Gao #define SDCLK_DELAY_SHIFT 9
35a702c8abSZhangfei Gao #define SDCLK_DELAY_MASK 0x1f
36a702c8abSZhangfei Gao
37a702c8abSZhangfei Gao #define SD_CFG_FIFO_PARAM 0x100
38a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_ON (1<<6)
39a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF
40a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24
41a702c8abSZhangfei Gao
42a702c8abSZhangfei Gao #define SD_SPI_MODE 0x108
43a702c8abSZhangfei Gao #define SD_CE_ATA_1 0x10C
44a702c8abSZhangfei Gao
45a702c8abSZhangfei Gao #define SD_CE_ATA_2 0x10E
46a702c8abSZhangfei Gao #define SDCE_MISC_INT (1<<2)
47a702c8abSZhangfei Gao #define SDCE_MISC_INT_EN (1<<1)
48a702c8abSZhangfei Gao
49cc9571e8SSebastian Hesselbarth struct sdhci_pxa {
508afdc9ccSSebastian Hesselbarth struct clk *clk_core;
518c96a7a3SSebastian Hesselbarth struct clk *clk_io;
52cc9571e8SSebastian Hesselbarth u8 power_mode;
531140011eSMarcin Wojtas void __iomem *sdio3_conf_reg;
54cc9571e8SSebastian Hesselbarth };
55cc9571e8SSebastian Hesselbarth
565491ce3fSMarcin Wojtas /*
575491ce3fSMarcin Wojtas * These registers are relative to the second register region, for the
585491ce3fSMarcin Wojtas * MBus bridge.
595491ce3fSMarcin Wojtas */
605491ce3fSMarcin Wojtas #define SDHCI_WINDOW_CTRL(i) (0x80 + ((i) << 3))
615491ce3fSMarcin Wojtas #define SDHCI_WINDOW_BASE(i) (0x84 + ((i) << 3))
625491ce3fSMarcin Wojtas #define SDHCI_MAX_WIN_NUM 8
635491ce3fSMarcin Wojtas
641140011eSMarcin Wojtas /*
651140011eSMarcin Wojtas * Fields below belong to SDIO3 Configuration Register (third register
661140011eSMarcin Wojtas * region for the Armada 38x flavor)
671140011eSMarcin Wojtas */
681140011eSMarcin Wojtas
691140011eSMarcin Wojtas #define SDIO3_CONF_CLK_INV BIT(0)
701140011eSMarcin Wojtas #define SDIO3_CONF_SD_FB_CLK BIT(2)
711140011eSMarcin Wojtas
mv_conf_mbus_windows(struct platform_device * pdev,const struct mbus_dram_target_info * dram)725491ce3fSMarcin Wojtas static int mv_conf_mbus_windows(struct platform_device *pdev,
735491ce3fSMarcin Wojtas const struct mbus_dram_target_info *dram)
745491ce3fSMarcin Wojtas {
755491ce3fSMarcin Wojtas int i;
765491ce3fSMarcin Wojtas void __iomem *regs;
775491ce3fSMarcin Wojtas struct resource *res;
785491ce3fSMarcin Wojtas
795491ce3fSMarcin Wojtas if (!dram) {
805491ce3fSMarcin Wojtas dev_err(&pdev->dev, "no mbus dram info\n");
815491ce3fSMarcin Wojtas return -EINVAL;
825491ce3fSMarcin Wojtas }
835491ce3fSMarcin Wojtas
845491ce3fSMarcin Wojtas res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
855491ce3fSMarcin Wojtas if (!res) {
865491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot get mbus registers\n");
875491ce3fSMarcin Wojtas return -EINVAL;
885491ce3fSMarcin Wojtas }
895491ce3fSMarcin Wojtas
905491ce3fSMarcin Wojtas regs = ioremap(res->start, resource_size(res));
915491ce3fSMarcin Wojtas if (!regs) {
925491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot map mbus registers\n");
935491ce3fSMarcin Wojtas return -ENOMEM;
945491ce3fSMarcin Wojtas }
955491ce3fSMarcin Wojtas
965491ce3fSMarcin Wojtas for (i = 0; i < SDHCI_MAX_WIN_NUM; i++) {
975491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_CTRL(i));
985491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_BASE(i));
995491ce3fSMarcin Wojtas }
1005491ce3fSMarcin Wojtas
1015491ce3fSMarcin Wojtas for (i = 0; i < dram->num_cs; i++) {
1025491ce3fSMarcin Wojtas const struct mbus_dram_window *cs = dram->cs + i;
1035491ce3fSMarcin Wojtas
1045491ce3fSMarcin Wojtas /* Write size, attributes and target id to control register */
1055491ce3fSMarcin Wojtas writel(((cs->size - 1) & 0xffff0000) |
1065491ce3fSMarcin Wojtas (cs->mbus_attr << 8) |
1075491ce3fSMarcin Wojtas (dram->mbus_dram_target_id << 4) | 1,
1085491ce3fSMarcin Wojtas regs + SDHCI_WINDOW_CTRL(i));
1095491ce3fSMarcin Wojtas /* Write base address to base register */
1105491ce3fSMarcin Wojtas writel(cs->base, regs + SDHCI_WINDOW_BASE(i));
1115491ce3fSMarcin Wojtas }
1125491ce3fSMarcin Wojtas
1135491ce3fSMarcin Wojtas iounmap(regs);
1145491ce3fSMarcin Wojtas
1155491ce3fSMarcin Wojtas return 0;
1165491ce3fSMarcin Wojtas }
1175491ce3fSMarcin Wojtas
armada_38x_quirks(struct platform_device * pdev,struct sdhci_host * host)118a39128bcSMarcin Wojtas static int armada_38x_quirks(struct platform_device *pdev,
119a39128bcSMarcin Wojtas struct sdhci_host *host)
120d4b803c5SGregory CLEMENT {
121a39128bcSMarcin Wojtas struct device_node *np = pdev->dev.of_node;
1221140011eSMarcin Wojtas struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
123f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
1241140011eSMarcin Wojtas struct resource *res;
125a39128bcSMarcin Wojtas
1265de76bfcSNadav Haklai host->quirks &= ~SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
1270ca33b4aSRussell King
1284f1896ddSAdrian Hunter sdhci_read_caps(host);
1290ca33b4aSRussell King
1301140011eSMarcin Wojtas res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1311140011eSMarcin Wojtas "conf-sdio3");
1321140011eSMarcin Wojtas if (res) {
1331140011eSMarcin Wojtas pxa->sdio3_conf_reg = devm_ioremap_resource(&pdev->dev, res);
1341140011eSMarcin Wojtas if (IS_ERR(pxa->sdio3_conf_reg))
1351140011eSMarcin Wojtas return PTR_ERR(pxa->sdio3_conf_reg);
1361140011eSMarcin Wojtas } else {
137d4b803c5SGregory CLEMENT /*
138d4b803c5SGregory CLEMENT * According to erratum 'FE-2946959' both SDR50 and DDR50
139d4b803c5SGregory CLEMENT * modes require specific clock adjustments in SDIO3
140d4b803c5SGregory CLEMENT * Configuration register, if the adjustment is not done,
141d4b803c5SGregory CLEMENT * remove them from the capabilities.
142d4b803c5SGregory CLEMENT */
143d4b803c5SGregory CLEMENT host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
144a39128bcSMarcin Wojtas
1451140011eSMarcin Wojtas dev_warn(&pdev->dev, "conf-sdio3 register not found: disabling SDR50 and DDR50 modes.\nConsider updating your dtb\n");
1461140011eSMarcin Wojtas }
1471140011eSMarcin Wojtas
148a39128bcSMarcin Wojtas /*
149a39128bcSMarcin Wojtas * According to erratum 'ERR-7878951' Armada 38x SDHCI
150a39128bcSMarcin Wojtas * controller has different capabilities than the ones shown
151a39128bcSMarcin Wojtas * in its registers
152a39128bcSMarcin Wojtas */
153a39128bcSMarcin Wojtas if (of_property_read_bool(np, "no-1-8-v")) {
154a39128bcSMarcin Wojtas host->caps &= ~SDHCI_CAN_VDD_180;
155a39128bcSMarcin Wojtas host->mmc->caps &= ~MMC_CAP_1_8V_DDR;
156a39128bcSMarcin Wojtas } else {
157a39128bcSMarcin Wojtas host->caps &= ~SDHCI_CAN_VDD_330;
158a39128bcSMarcin Wojtas }
159a39128bcSMarcin Wojtas host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_USE_SDR50_TUNING);
160a39128bcSMarcin Wojtas
161d4b803c5SGregory CLEMENT return 0;
162d4b803c5SGregory CLEMENT }
163d4b803c5SGregory CLEMENT
pxav3_reset(struct sdhci_host * host,u8 mask)16403231f9bSRussell King static void pxav3_reset(struct sdhci_host *host, u8 mask)
165a702c8abSZhangfei Gao {
166a702c8abSZhangfei Gao struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc));
167a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
168a702c8abSZhangfei Gao
16903231f9bSRussell King sdhci_reset(host, mask);
17003231f9bSRussell King
171a702c8abSZhangfei Gao if (mask == SDHCI_RESET_ALL) {
172a702c8abSZhangfei Gao /*
173a702c8abSZhangfei Gao * tune timing of read data/command when crc error happen
174a702c8abSZhangfei Gao * no performance impact
175a702c8abSZhangfei Gao */
176a702c8abSZhangfei Gao if (pdata && 0 != pdata->clk_delay_cycles) {
177a702c8abSZhangfei Gao u16 tmp;
178a702c8abSZhangfei Gao
179a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
180a702c8abSZhangfei Gao tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK)
181a702c8abSZhangfei Gao << SDCLK_DELAY_SHIFT;
182a702c8abSZhangfei Gao tmp |= SDCLK_SEL;
183a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP);
184a702c8abSZhangfei Gao }
185a702c8abSZhangfei Gao }
186a702c8abSZhangfei Gao }
187a702c8abSZhangfei Gao
188a702c8abSZhangfei Gao #define MAX_WAIT_COUNT 5
pxav3_gen_init_74_clocks(struct sdhci_host * host,u8 power_mode)189a702c8abSZhangfei Gao static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode)
190a702c8abSZhangfei Gao {
191a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
192f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
193a702c8abSZhangfei Gao u16 tmp;
194a702c8abSZhangfei Gao int count;
195a702c8abSZhangfei Gao
196a702c8abSZhangfei Gao if (pxa->power_mode == MMC_POWER_UP
197a702c8abSZhangfei Gao && power_mode == MMC_POWER_ON) {
198a702c8abSZhangfei Gao
199a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc),
200a702c8abSZhangfei Gao "%s: slot->power_mode = %d,"
201a702c8abSZhangfei Gao "ios->power_mode = %d\n",
202a702c8abSZhangfei Gao __func__,
203a702c8abSZhangfei Gao pxa->power_mode,
204a702c8abSZhangfei Gao power_mode);
205a702c8abSZhangfei Gao
206a702c8abSZhangfei Gao /* set we want notice of when 74 clocks are sent */
207a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2);
208a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT_EN;
209a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2);
210a702c8abSZhangfei Gao
211a702c8abSZhangfei Gao /* start sending the 74 clocks */
212a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM);
213a702c8abSZhangfei Gao tmp |= SDCFG_GEN_PAD_CLK_ON;
214a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM);
215a702c8abSZhangfei Gao
216a702c8abSZhangfei Gao /* slowest speed is about 100KHz or 10usec per clock */
217a702c8abSZhangfei Gao udelay(740);
218a702c8abSZhangfei Gao count = 0;
219a702c8abSZhangfei Gao
220a702c8abSZhangfei Gao while (count++ < MAX_WAIT_COUNT) {
221a702c8abSZhangfei Gao if ((readw(host->ioaddr + SD_CE_ATA_2)
222a702c8abSZhangfei Gao & SDCE_MISC_INT) == 0)
223a702c8abSZhangfei Gao break;
224a702c8abSZhangfei Gao udelay(10);
225a702c8abSZhangfei Gao }
226a702c8abSZhangfei Gao
227a702c8abSZhangfei Gao if (count == MAX_WAIT_COUNT)
228a702c8abSZhangfei Gao dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n");
229a702c8abSZhangfei Gao
230a702c8abSZhangfei Gao /* clear the interrupt bit if posted */
231a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2);
232a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT;
233a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2);
234a702c8abSZhangfei Gao }
235a702c8abSZhangfei Gao pxa->power_mode = power_mode;
236a702c8abSZhangfei Gao }
237a702c8abSZhangfei Gao
pxav3_set_uhs_signaling(struct sdhci_host * host,unsigned int uhs)23813e64501SRussell King static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs)
239a702c8abSZhangfei Gao {
2401140011eSMarcin Wojtas struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
241f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
242a702c8abSZhangfei Gao u16 ctrl_2;
243a702c8abSZhangfei Gao
244a702c8abSZhangfei Gao /*
245a702c8abSZhangfei Gao * Set V18_EN -- UHS modes do not work without this.
246a702c8abSZhangfei Gao * does not change signaling voltage
247a702c8abSZhangfei Gao */
248a702c8abSZhangfei Gao ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
249a702c8abSZhangfei Gao
250a702c8abSZhangfei Gao /* Select Bus Speed Mode for host */
251a702c8abSZhangfei Gao ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
252a702c8abSZhangfei Gao switch (uhs) {
253a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR12:
254a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
255a702c8abSZhangfei Gao break;
256a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR25:
257a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
258a702c8abSZhangfei Gao break;
259a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR50:
260a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180;
261a702c8abSZhangfei Gao break;
262a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR104:
263a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180;
264a702c8abSZhangfei Gao break;
265668e84b2SSebastian Hesselbarth case MMC_TIMING_MMC_DDR52:
266a702c8abSZhangfei Gao case MMC_TIMING_UHS_DDR50:
267a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180;
268a702c8abSZhangfei Gao break;
269a702c8abSZhangfei Gao }
270a702c8abSZhangfei Gao
2711140011eSMarcin Wojtas /*
2721140011eSMarcin Wojtas * Update SDIO3 Configuration register according to erratum
2731140011eSMarcin Wojtas * FE-2946959
2741140011eSMarcin Wojtas */
2751140011eSMarcin Wojtas if (pxa->sdio3_conf_reg) {
2761140011eSMarcin Wojtas u8 reg_val = readb(pxa->sdio3_conf_reg);
2771140011eSMarcin Wojtas
2781140011eSMarcin Wojtas if (uhs == MMC_TIMING_UHS_SDR50 ||
2791140011eSMarcin Wojtas uhs == MMC_TIMING_UHS_DDR50) {
2801140011eSMarcin Wojtas reg_val &= ~SDIO3_CONF_CLK_INV;
2811140011eSMarcin Wojtas reg_val |= SDIO3_CONF_SD_FB_CLK;
282fa796414SNadav Haklai } else if (uhs == MMC_TIMING_MMC_HS) {
283fa796414SNadav Haklai reg_val &= ~SDIO3_CONF_CLK_INV;
284fa796414SNadav Haklai reg_val &= ~SDIO3_CONF_SD_FB_CLK;
2851140011eSMarcin Wojtas } else {
2861140011eSMarcin Wojtas reg_val |= SDIO3_CONF_CLK_INV;
2871140011eSMarcin Wojtas reg_val &= ~SDIO3_CONF_SD_FB_CLK;
2881140011eSMarcin Wojtas }
2891140011eSMarcin Wojtas writeb(reg_val, pxa->sdio3_conf_reg);
2901140011eSMarcin Wojtas }
2911140011eSMarcin Wojtas
292a702c8abSZhangfei Gao sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
293a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc),
294a702c8abSZhangfei Gao "%s uhs = %d, ctrl_2 = %04X\n",
295a702c8abSZhangfei Gao __func__, uhs, ctrl_2);
296a702c8abSZhangfei Gao }
297a702c8abSZhangfei Gao
pxav3_set_power(struct sdhci_host * host,unsigned char mode,unsigned short vdd)2981dceb041SAdrian Hunter static void pxav3_set_power(struct sdhci_host *host, unsigned char mode,
2991dceb041SAdrian Hunter unsigned short vdd)
3001dceb041SAdrian Hunter {
3011dceb041SAdrian Hunter struct mmc_host *mmc = host->mmc;
3021dceb041SAdrian Hunter u8 pwr = host->pwr;
3031dceb041SAdrian Hunter
304606d3131SAdrian Hunter sdhci_set_power_noreg(host, mode, vdd);
3051dceb041SAdrian Hunter
3061dceb041SAdrian Hunter if (host->pwr == pwr)
3071dceb041SAdrian Hunter return;
3081dceb041SAdrian Hunter
3091dceb041SAdrian Hunter if (host->pwr == 0)
3101dceb041SAdrian Hunter vdd = 0;
3111dceb041SAdrian Hunter
312d1e4f74fSAdrian Hunter if (!IS_ERR(mmc->supply.vmmc))
3131dceb041SAdrian Hunter mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
3141dceb041SAdrian Hunter }
3151dceb041SAdrian Hunter
316c915568dSLars-Peter Clausen static const struct sdhci_ops pxav3_sdhci_ops = {
3171771059cSRussell King .set_clock = sdhci_set_clock,
3181dceb041SAdrian Hunter .set_power = pxav3_set_power,
319a702c8abSZhangfei Gao .platform_send_init_74_clocks = pxav3_gen_init_74_clocks,
320d005d943SLars-Peter Clausen .get_max_clock = sdhci_pltfm_clk_get_max_clock,
3212317f56cSRussell King .set_bus_width = sdhci_set_bus_width,
32203231f9bSRussell King .reset = pxav3_reset,
323b3153765SPeter Griffin .set_uhs_signaling = pxav3_set_uhs_signaling,
324a702c8abSZhangfei Gao };
325a702c8abSZhangfei Gao
326d35ade8fSJulia Lawall static const struct sdhci_pltfm_data sdhci_pxav3_pdata = {
327e065162aSKevin Liu .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK
32873b7afb9SKevin Liu | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
32973b7afb9SKevin Liu | SDHCI_QUIRK_32BIT_ADMA_SIZE
33073b7afb9SKevin Liu | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
33173b7afb9SKevin Liu .ops = &pxav3_sdhci_ops,
33273b7afb9SKevin Liu };
33373b7afb9SKevin Liu
334b650352dSChris Ball #ifdef CONFIG_OF
335b650352dSChris Ball static const struct of_device_id sdhci_pxav3_of_match[] = {
336b650352dSChris Ball {
337b650352dSChris Ball .compatible = "mrvl,pxav3-mmc",
338b650352dSChris Ball },
3395491ce3fSMarcin Wojtas {
3405491ce3fSMarcin Wojtas .compatible = "marvell,armada-380-sdhci",
3415491ce3fSMarcin Wojtas },
342b650352dSChris Ball {},
343b650352dSChris Ball };
344b650352dSChris Ball MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match);
345b650352dSChris Ball
pxav3_get_mmc_pdata(struct device * dev)346b650352dSChris Ball static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
347b650352dSChris Ball {
348b650352dSChris Ball struct sdhci_pxa_platdata *pdata;
349b650352dSChris Ball struct device_node *np = dev->of_node;
350b650352dSChris Ball u32 clk_delay_cycles;
351b650352dSChris Ball
352b650352dSChris Ball pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
353b650352dSChris Ball if (!pdata)
354b650352dSChris Ball return NULL;
355b650352dSChris Ball
35614460dbaSJisheng Zhang if (!of_property_read_u32(np, "mrvl,clk-delay-cycles",
35714460dbaSJisheng Zhang &clk_delay_cycles))
358b650352dSChris Ball pdata->clk_delay_cycles = clk_delay_cycles;
359b650352dSChris Ball
360b650352dSChris Ball return pdata;
361b650352dSChris Ball }
362b650352dSChris Ball #else
pxav3_get_mmc_pdata(struct device * dev)363b650352dSChris Ball static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev)
364b650352dSChris Ball {
365b650352dSChris Ball return NULL;
366b650352dSChris Ball }
367b650352dSChris Ball #endif
368b650352dSChris Ball
sdhci_pxav3_probe(struct platform_device * pdev)369c3be1efdSBill Pemberton static int sdhci_pxav3_probe(struct platform_device *pdev)
370a702c8abSZhangfei Gao {
371a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host;
372a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data;
373a702c8abSZhangfei Gao struct device *dev = &pdev->dev;
3745491ce3fSMarcin Wojtas struct device_node *np = pdev->dev.of_node;
375a702c8abSZhangfei Gao struct sdhci_host *host = NULL;
376a702c8abSZhangfei Gao struct sdhci_pxa *pxa = NULL;
377b650352dSChris Ball const struct of_device_id *match;
378a702c8abSZhangfei Gao int ret;
379a702c8abSZhangfei Gao
380f599da40SJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, sizeof(*pxa));
3813df5b281SLaurent Pinchart if (IS_ERR(host))
382a702c8abSZhangfei Gao return PTR_ERR(host);
3835491ce3fSMarcin Wojtas
384a702c8abSZhangfei Gao pltfm_host = sdhci_priv(host);
385f599da40SJisheng Zhang pxa = sdhci_pltfm_priv(pltfm_host);
386a702c8abSZhangfei Gao
38701ae1070SSebastian Hesselbarth pxa->clk_io = devm_clk_get(dev, "io");
38801ae1070SSebastian Hesselbarth if (IS_ERR(pxa->clk_io))
3898c96a7a3SSebastian Hesselbarth pxa->clk_io = devm_clk_get(dev, NULL);
3908c96a7a3SSebastian Hesselbarth if (IS_ERR(pxa->clk_io)) {
391a702c8abSZhangfei Gao dev_err(dev, "failed to get io clock\n");
3928c96a7a3SSebastian Hesselbarth ret = PTR_ERR(pxa->clk_io);
393a702c8abSZhangfei Gao goto err_clk_get;
394a702c8abSZhangfei Gao }
3958c96a7a3SSebastian Hesselbarth pltfm_host->clk = pxa->clk_io;
3968c96a7a3SSebastian Hesselbarth clk_prepare_enable(pxa->clk_io);
397a702c8abSZhangfei Gao
3988afdc9ccSSebastian Hesselbarth pxa->clk_core = devm_clk_get(dev, "core");
3998afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core))
4008afdc9ccSSebastian Hesselbarth clk_prepare_enable(pxa->clk_core);
4018afdc9ccSSebastian Hesselbarth
402a39128bcSMarcin Wojtas /* enable 1/8V DDR capable */
403a39128bcSMarcin Wojtas host->mmc->caps |= MMC_CAP_1_8V_DDR;
404a39128bcSMarcin Wojtas
405aa8165f9SThomas Petazzoni if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) {
406a39128bcSMarcin Wojtas ret = armada_38x_quirks(pdev, host);
407d4b803c5SGregory CLEMENT if (ret < 0)
4082162d9f4SMarcin Wojtas goto err_mbus_win;
409aa8165f9SThomas Petazzoni ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info());
410aa8165f9SThomas Petazzoni if (ret < 0)
411aa8165f9SThomas Petazzoni goto err_mbus_win;
412aa8165f9SThomas Petazzoni }
413aa8165f9SThomas Petazzoni
414b650352dSChris Ball match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev);
415943647f6SKevin Liu if (match) {
416d2cf6071SSimon Baatz ret = mmc_of_parse(host->mmc);
417d2cf6071SSimon Baatz if (ret)
418d2cf6071SSimon Baatz goto err_of_parse;
419943647f6SKevin Liu sdhci_get_of_property(pdev);
420b650352dSChris Ball pdata = pxav3_get_mmc_pdata(dev);
4219cd76049SJingju Hou pdev->dev.platform_data = pdata;
422943647f6SKevin Liu } else if (pdata) {
423a702c8abSZhangfei Gao /* on-chip device */
424c844a46fSKevin Liu if (pdata->flags & PXA_FLAG_CARD_PERMANENT)
425a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_NONREMOVABLE;
426a702c8abSZhangfei Gao
427a702c8abSZhangfei Gao /* If slot design supports 8 bit data, indicate this to MMC. */
428a702c8abSZhangfei Gao if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT)
429a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_8_BIT_DATA;
430a702c8abSZhangfei Gao
431a702c8abSZhangfei Gao if (pdata->quirks)
432a702c8abSZhangfei Gao host->quirks |= pdata->quirks;
4337c52d7bbSKevin Liu if (pdata->quirks2)
4347c52d7bbSKevin Liu host->quirks2 |= pdata->quirks2;
435a702c8abSZhangfei Gao if (pdata->host_caps)
436a702c8abSZhangfei Gao host->mmc->caps |= pdata->host_caps;
4378f63795cSChris Ball if (pdata->host_caps2)
4388f63795cSChris Ball host->mmc->caps2 |= pdata->host_caps2;
439a702c8abSZhangfei Gao if (pdata->pm_caps)
440a702c8abSZhangfei Gao host->mmc->pm_caps |= pdata->pm_caps;
441a702c8abSZhangfei Gao }
442a702c8abSZhangfei Gao
44362cf983aSJisheng Zhang pm_runtime_get_noresume(&pdev->dev);
44462cf983aSJisheng Zhang pm_runtime_set_active(&pdev->dev);
445bb691ae4SKevin Liu pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS);
446bb691ae4SKevin Liu pm_runtime_use_autosuspend(&pdev->dev);
44762cf983aSJisheng Zhang pm_runtime_enable(&pdev->dev);
448bb691ae4SKevin Liu pm_suspend_ignore_children(&pdev->dev, 1);
449bb691ae4SKevin Liu
450a702c8abSZhangfei Gao ret = sdhci_add_host(host);
451fb8617e1SJisheng Zhang if (ret)
452a702c8abSZhangfei Gao goto err_add_host;
453a702c8abSZhangfei Gao
45483dc9fecSJisheng Zhang if (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ)
455740b7a44SKevin Liu device_init_wakeup(&pdev->dev, 1);
456740b7a44SKevin Liu
457bb691ae4SKevin Liu pm_runtime_put_autosuspend(&pdev->dev);
458bb691ae4SKevin Liu
459a702c8abSZhangfei Gao return 0;
460a702c8abSZhangfei Gao
461a702c8abSZhangfei Gao err_add_host:
4620dcaa249SDaniel Drake pm_runtime_disable(&pdev->dev);
46362cf983aSJisheng Zhang pm_runtime_put_noidle(&pdev->dev);
46487d2163dSXiang Wang err_of_parse:
465aa8165f9SThomas Petazzoni err_mbus_win:
4668c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io);
4678afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core);
468a702c8abSZhangfei Gao err_clk_get:
469a702c8abSZhangfei Gao sdhci_pltfm_free(pdev);
470a702c8abSZhangfei Gao return ret;
471a702c8abSZhangfei Gao }
472a702c8abSZhangfei Gao
sdhci_pxav3_remove(struct platform_device * pdev)473*c61394aaSYangtao Li static void sdhci_pxav3_remove(struct platform_device *pdev)
474a702c8abSZhangfei Gao {
475a702c8abSZhangfei Gao struct sdhci_host *host = platform_get_drvdata(pdev);
476a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
477f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
478a702c8abSZhangfei Gao
479bb691ae4SKevin Liu pm_runtime_get_sync(&pdev->dev);
480bb691ae4SKevin Liu pm_runtime_disable(&pdev->dev);
48120f1f2d7SJisheng Zhang pm_runtime_put_noidle(&pdev->dev);
48220f1f2d7SJisheng Zhang
48320f1f2d7SJisheng Zhang sdhci_remove_host(host, 1);
484a702c8abSZhangfei Gao
4858c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io);
4868afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core);
4878f63795cSChris Ball
488a702c8abSZhangfei Gao sdhci_pltfm_free(pdev);
489a702c8abSZhangfei Gao }
490a702c8abSZhangfei Gao
491bb691ae4SKevin Liu #ifdef CONFIG_PM_SLEEP
sdhci_pxav3_suspend(struct device * dev)492bb691ae4SKevin Liu static int sdhci_pxav3_suspend(struct device *dev)
493bb691ae4SKevin Liu {
494bb691ae4SKevin Liu int ret;
495bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev);
496bb691ae4SKevin Liu
497bb691ae4SKevin Liu pm_runtime_get_sync(dev);
498d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3)
499d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc);
500bb691ae4SKevin Liu ret = sdhci_suspend_host(host);
501bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev);
502bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev);
503bb691ae4SKevin Liu
504bb691ae4SKevin Liu return ret;
505bb691ae4SKevin Liu }
506bb691ae4SKevin Liu
sdhci_pxav3_resume(struct device * dev)507bb691ae4SKevin Liu static int sdhci_pxav3_resume(struct device *dev)
508bb691ae4SKevin Liu {
509bb691ae4SKevin Liu int ret;
510bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev);
511bb691ae4SKevin Liu
512bb691ae4SKevin Liu pm_runtime_get_sync(dev);
513bb691ae4SKevin Liu ret = sdhci_resume_host(host);
514bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev);
515bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev);
516bb691ae4SKevin Liu
517bb691ae4SKevin Liu return ret;
518bb691ae4SKevin Liu }
519bb691ae4SKevin Liu #endif
520bb691ae4SKevin Liu
521162d6f98SRafael J. Wysocki #ifdef CONFIG_PM
sdhci_pxav3_runtime_suspend(struct device * dev)522bb691ae4SKevin Liu static int sdhci_pxav3_runtime_suspend(struct device *dev)
523bb691ae4SKevin Liu {
524bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev);
525bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
526f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
5273bb10f60SJisheng Zhang int ret;
528bb691ae4SKevin Liu
5293bb10f60SJisheng Zhang ret = sdhci_runtime_suspend_host(host);
5303bb10f60SJisheng Zhang if (ret)
5313bb10f60SJisheng Zhang return ret;
532bb691ae4SKevin Liu
533d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3)
534d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc);
535d38dcad4SAdrian Hunter
5368c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io);
5378afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core))
5388afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core);
539bb691ae4SKevin Liu
540bb691ae4SKevin Liu return 0;
541bb691ae4SKevin Liu }
542bb691ae4SKevin Liu
sdhci_pxav3_runtime_resume(struct device * dev)543bb691ae4SKevin Liu static int sdhci_pxav3_runtime_resume(struct device *dev)
544bb691ae4SKevin Liu {
545bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev);
546bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
547f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host);
548bb691ae4SKevin Liu
5498c96a7a3SSebastian Hesselbarth clk_prepare_enable(pxa->clk_io);
5508afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core))
5518afdc9ccSSebastian Hesselbarth clk_prepare_enable(pxa->clk_core);
552bb691ae4SKevin Liu
553c6303c5dSBaolin Wang return sdhci_runtime_resume_host(host, 0);
554bb691ae4SKevin Liu }
555bb691ae4SKevin Liu #endif
556bb691ae4SKevin Liu
557bb691ae4SKevin Liu static const struct dev_pm_ops sdhci_pxav3_pmops = {
558bb691ae4SKevin Liu SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume)
559bb691ae4SKevin Liu SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend,
560bb691ae4SKevin Liu sdhci_pxav3_runtime_resume, NULL)
561bb691ae4SKevin Liu };
562bb691ae4SKevin Liu
563a702c8abSZhangfei Gao static struct platform_driver sdhci_pxav3_driver = {
564a702c8abSZhangfei Gao .driver = {
565a702c8abSZhangfei Gao .name = "sdhci-pxav3",
56621b2cec6SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
56759d22309SAxel Lin .of_match_table = of_match_ptr(sdhci_pxav3_of_match),
568a81ce772SUlf Hansson .pm = &sdhci_pxav3_pmops,
569a702c8abSZhangfei Gao },
570a702c8abSZhangfei Gao .probe = sdhci_pxav3_probe,
571*c61394aaSYangtao Li .remove_new = sdhci_pxav3_remove,
572a702c8abSZhangfei Gao };
573a702c8abSZhangfei Gao
574d1f81a64SAxel Lin module_platform_driver(sdhci_pxav3_driver);
575a702c8abSZhangfei Gao
576a702c8abSZhangfei Gao MODULE_DESCRIPTION("SDHCI driver for pxav3");
577a702c8abSZhangfei Gao MODULE_AUTHOR("Marvell International Ltd.");
578a702c8abSZhangfei Gao MODULE_LICENSE("GPL v2");
579a702c8abSZhangfei Gao
580