1e438cf49SJisheng Zhang // SPDX-License-Identifier: GPL-2.0
2e438cf49SJisheng Zhang /*
3e438cf49SJisheng Zhang * Driver for Synopsys DesignWare Cores Mobile Storage Host Controller
4e438cf49SJisheng Zhang *
5e438cf49SJisheng Zhang * Copyright (C) 2018 Synaptics Incorporated
6e438cf49SJisheng Zhang *
7e438cf49SJisheng Zhang * Author: Jisheng Zhang <jszhang@kernel.org>
8e438cf49SJisheng Zhang */
9e438cf49SJisheng Zhang
10eb81ed51SLiming Sun #include <linux/acpi.h>
11e438cf49SJisheng Zhang #include <linux/clk.h>
12b85c997dSJisheng Zhang #include <linux/dma-mapping.h>
1308f3dff7SShawn Lin #include <linux/iopoll.h>
14b85c997dSJisheng Zhang #include <linux/kernel.h>
15e438cf49SJisheng Zhang #include <linux/module.h>
16e438cf49SJisheng Zhang #include <linux/of.h>
17c62da8a8SRob Herring #include <linux/platform_device.h>
1848fe8fadSLiming Sun #include <linux/pm_runtime.h>
1970f83220SYifeng Zhao #include <linux/reset.h>
20b85c997dSJisheng Zhang #include <linux/sizes.h>
21e438cf49SJisheng Zhang
22e438cf49SJisheng Zhang #include "sdhci-pltfm.h"
23e438cf49SJisheng Zhang
24ca1219c0SJisheng Zhang #define SDHCI_DWCMSHC_ARG2_STUFF GENMASK(31, 16)
25ca1219c0SJisheng Zhang
26554232e8SJisheng Zhang /* DWCMSHC specific Mode Select value */
27554232e8SJisheng Zhang #define DWCMSHC_CTRL_HS400 0x7
28554232e8SJisheng Zhang
2908f3dff7SShawn Lin /* DWC IP vendor area 1 pointer */
3008f3dff7SShawn Lin #define DWCMSHC_P_VENDOR_AREA1 0xe8
3108f3dff7SShawn Lin #define DWCMSHC_AREA1_MASK GENMASK(11, 0)
3208f3dff7SShawn Lin /* Offset inside the vendor area 1 */
3308f3dff7SShawn Lin #define DWCMSHC_HOST_CTRL3 0x8
3408f3dff7SShawn Lin #define DWCMSHC_EMMC_CONTROL 0x2c
35c6f361cbSYifeng Zhao #define DWCMSHC_CARD_IS_EMMC BIT(0)
3608f3dff7SShawn Lin #define DWCMSHC_ENHANCED_STROBE BIT(8)
3708f3dff7SShawn Lin #define DWCMSHC_EMMC_ATCTRL 0x40
3808f3dff7SShawn Lin
3908f3dff7SShawn Lin /* Rockchip specific Registers */
4008f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_CTRL 0x800
4108f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_RXCLK 0x804
4208f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_TXCLK 0x808
4308f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_STRBIN 0x80c
44c6f361cbSYifeng Zhao #define DECMSHC_EMMC_DLL_CMDOUT 0x810
4508f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_STATUS0 0x840
4608f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_START BIT(0)
4708f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
4808f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
4908f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL 29
5008f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_START_POINT 16
5108f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_INC 8
52b75a52b0SShawn Lin #define DWCMSHC_EMMC_DLL_BYPASS BIT(24)
5308f3dff7SShawn Lin #define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
54c6f361cbSYifeng Zhao #define DLL_TXCLK_TAPNUM_DEFAULT 0x10
55c6f361cbSYifeng Zhao #define DLL_TXCLK_TAPNUM_90_DEGREES 0xA
5608f3dff7SShawn Lin #define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
57c6f361cbSYifeng Zhao #define DLL_STRBIN_TAPNUM_DEFAULT 0x8
58c6f361cbSYifeng Zhao #define DLL_STRBIN_TAPNUM_FROM_SW BIT(24)
59c6f361cbSYifeng Zhao #define DLL_STRBIN_DELAY_NUM_SEL BIT(26)
60c6f361cbSYifeng Zhao #define DLL_STRBIN_DELAY_NUM_OFFSET 16
61c6f361cbSYifeng Zhao #define DLL_STRBIN_DELAY_NUM_DEFAULT 0x16
6208f3dff7SShawn Lin #define DLL_RXCLK_NO_INVERTER 1
6308f3dff7SShawn Lin #define DLL_RXCLK_INVERTER 0
64c6f361cbSYifeng Zhao #define DLL_CMDOUT_TAPNUM_90_DEGREES 0x8
65b75a52b0SShawn Lin #define DLL_RXCLK_ORI_GATE BIT(31)
66c6f361cbSYifeng Zhao #define DLL_CMDOUT_TAPNUM_FROM_SW BIT(24)
67c6f361cbSYifeng Zhao #define DLL_CMDOUT_SRC_CLK_NEG BIT(28)
68c6f361cbSYifeng Zhao #define DLL_CMDOUT_EN_SRC_CLK_NEG BIT(29)
69c6f361cbSYifeng Zhao
7008f3dff7SShawn Lin #define DLL_LOCK_WO_TMOUT(x) \
7108f3dff7SShawn Lin ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
7208f3dff7SShawn Lin (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
7386e1a8e1SSebastian Reichel #define RK35xx_MAX_CLKS 3
7408f3dff7SShawn Lin
75b85c997dSJisheng Zhang #define BOUNDARY_OK(addr, len) \
76b85c997dSJisheng Zhang ((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1)))
77b85c997dSJisheng Zhang
78c6f361cbSYifeng Zhao enum dwcmshc_rk_type {
79c6f361cbSYifeng Zhao DWCMSHC_RK3568,
80c6f361cbSYifeng Zhao DWCMSHC_RK3588,
81c6f361cbSYifeng Zhao };
82c6f361cbSYifeng Zhao
8386e1a8e1SSebastian Reichel struct rk35xx_priv {
8408f3dff7SShawn Lin /* Rockchip specified optional clocks */
8586e1a8e1SSebastian Reichel struct clk_bulk_data rockchip_clks[RK35xx_MAX_CLKS];
8670f83220SYifeng Zhao struct reset_control *reset;
87c6f361cbSYifeng Zhao enum dwcmshc_rk_type devtype;
8808f3dff7SShawn Lin u8 txclk_tapnum;
8908f3dff7SShawn Lin };
9008f3dff7SShawn Lin
91e438cf49SJisheng Zhang struct dwcmshc_priv {
92e438cf49SJisheng Zhang struct clk *bus_clk;
9308f3dff7SShawn Lin int vendor_specific_area1; /* P_VENDOR_SPECIFIC_AREA reg */
9408f3dff7SShawn Lin void *priv; /* pointer to SoC private stuff */
95e438cf49SJisheng Zhang };
96e438cf49SJisheng Zhang
97b85c997dSJisheng Zhang /*
98b85c997dSJisheng Zhang * If DMA addr spans 128MB boundary, we split the DMA transfer into two
99b85c997dSJisheng Zhang * so that each DMA transfer doesn't exceed the boundary.
100b85c997dSJisheng Zhang */
dwcmshc_adma_write_desc(struct sdhci_host * host,void ** desc,dma_addr_t addr,int len,unsigned int cmd)101b85c997dSJisheng Zhang static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc,
102b85c997dSJisheng Zhang dma_addr_t addr, int len, unsigned int cmd)
103b85c997dSJisheng Zhang {
104b85c997dSJisheng Zhang int tmplen, offset;
105b85c997dSJisheng Zhang
106b85c997dSJisheng Zhang if (likely(!len || BOUNDARY_OK(addr, len))) {
107b85c997dSJisheng Zhang sdhci_adma_write_desc(host, desc, addr, len, cmd);
108b85c997dSJisheng Zhang return;
109b85c997dSJisheng Zhang }
110b85c997dSJisheng Zhang
111b85c997dSJisheng Zhang offset = addr & (SZ_128M - 1);
112b85c997dSJisheng Zhang tmplen = SZ_128M - offset;
113b85c997dSJisheng Zhang sdhci_adma_write_desc(host, desc, addr, tmplen, cmd);
114b85c997dSJisheng Zhang
115b85c997dSJisheng Zhang addr += tmplen;
116b85c997dSJisheng Zhang len -= tmplen;
117b85c997dSJisheng Zhang sdhci_adma_write_desc(host, desc, addr, len, cmd);
118b85c997dSJisheng Zhang }
119b85c997dSJisheng Zhang
dwcmshc_get_max_clock(struct sdhci_host * host)120eb81ed51SLiming Sun static unsigned int dwcmshc_get_max_clock(struct sdhci_host *host)
121eb81ed51SLiming Sun {
122eb81ed51SLiming Sun struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
123eb81ed51SLiming Sun
124eb81ed51SLiming Sun if (pltfm_host->clk)
125eb81ed51SLiming Sun return sdhci_pltfm_clk_get_max_clock(host);
126eb81ed51SLiming Sun else
127eb81ed51SLiming Sun return pltfm_host->clock;
128eb81ed51SLiming Sun }
129eb81ed51SLiming Sun
rk35xx_get_max_clock(struct sdhci_host * host)13049502408SVasily Khoruzhick static unsigned int rk35xx_get_max_clock(struct sdhci_host *host)
13149502408SVasily Khoruzhick {
13249502408SVasily Khoruzhick struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
13349502408SVasily Khoruzhick
13449502408SVasily Khoruzhick return clk_round_rate(pltfm_host->clk, ULONG_MAX);
13549502408SVasily Khoruzhick }
13649502408SVasily Khoruzhick
dwcmshc_check_auto_cmd23(struct mmc_host * mmc,struct mmc_request * mrq)137ca1219c0SJisheng Zhang static void dwcmshc_check_auto_cmd23(struct mmc_host *mmc,
138ca1219c0SJisheng Zhang struct mmc_request *mrq)
139ca1219c0SJisheng Zhang {
140ca1219c0SJisheng Zhang struct sdhci_host *host = mmc_priv(mmc);
141ca1219c0SJisheng Zhang
142ca1219c0SJisheng Zhang /*
143ca1219c0SJisheng Zhang * No matter V4 is enabled or not, ARGUMENT2 register is 32-bit
144ca1219c0SJisheng Zhang * block count register which doesn't support stuff bits of
145ca1219c0SJisheng Zhang * CMD23 argument on dwcmsch host controller.
146ca1219c0SJisheng Zhang */
147ca1219c0SJisheng Zhang if (mrq->sbc && (mrq->sbc->arg & SDHCI_DWCMSHC_ARG2_STUFF))
148ca1219c0SJisheng Zhang host->flags &= ~SDHCI_AUTO_CMD23;
149ca1219c0SJisheng Zhang else
150ca1219c0SJisheng Zhang host->flags |= SDHCI_AUTO_CMD23;
151ca1219c0SJisheng Zhang }
152ca1219c0SJisheng Zhang
dwcmshc_request(struct mmc_host * mmc,struct mmc_request * mrq)153ca1219c0SJisheng Zhang static void dwcmshc_request(struct mmc_host *mmc, struct mmc_request *mrq)
154ca1219c0SJisheng Zhang {
155ca1219c0SJisheng Zhang dwcmshc_check_auto_cmd23(mmc, mrq);
156ca1219c0SJisheng Zhang
157ca1219c0SJisheng Zhang sdhci_request(mmc, mrq);
158ca1219c0SJisheng Zhang }
159ca1219c0SJisheng Zhang
dwcmshc_set_uhs_signaling(struct sdhci_host * host,unsigned int timing)160554232e8SJisheng Zhang static void dwcmshc_set_uhs_signaling(struct sdhci_host *host,
161554232e8SJisheng Zhang unsigned int timing)
162554232e8SJisheng Zhang {
163c6f361cbSYifeng Zhao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
164c6f361cbSYifeng Zhao struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
165c6f361cbSYifeng Zhao u16 ctrl, ctrl_2;
166554232e8SJisheng Zhang
167554232e8SJisheng Zhang ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
168554232e8SJisheng Zhang /* Select Bus Speed Mode for host */
169554232e8SJisheng Zhang ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
170554232e8SJisheng Zhang if ((timing == MMC_TIMING_MMC_HS200) ||
171554232e8SJisheng Zhang (timing == MMC_TIMING_UHS_SDR104))
172554232e8SJisheng Zhang ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
173554232e8SJisheng Zhang else if (timing == MMC_TIMING_UHS_SDR12)
174554232e8SJisheng Zhang ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
175554232e8SJisheng Zhang else if ((timing == MMC_TIMING_UHS_SDR25) ||
176554232e8SJisheng Zhang (timing == MMC_TIMING_MMC_HS))
177554232e8SJisheng Zhang ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
178554232e8SJisheng Zhang else if (timing == MMC_TIMING_UHS_SDR50)
179554232e8SJisheng Zhang ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
180554232e8SJisheng Zhang else if ((timing == MMC_TIMING_UHS_DDR50) ||
181554232e8SJisheng Zhang (timing == MMC_TIMING_MMC_DDR52))
182554232e8SJisheng Zhang ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
183c6f361cbSYifeng Zhao else if (timing == MMC_TIMING_MMC_HS400) {
184c6f361cbSYifeng Zhao /* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */
185c6f361cbSYifeng Zhao ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
186c6f361cbSYifeng Zhao ctrl |= DWCMSHC_CARD_IS_EMMC;
187c6f361cbSYifeng Zhao sdhci_writew(host, ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
188c6f361cbSYifeng Zhao
189554232e8SJisheng Zhang ctrl_2 |= DWCMSHC_CTRL_HS400;
190c6f361cbSYifeng Zhao }
191c6f361cbSYifeng Zhao
192554232e8SJisheng Zhang sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
193554232e8SJisheng Zhang }
194554232e8SJisheng Zhang
dwcmshc_hs400_enhanced_strobe(struct mmc_host * mmc,struct mmc_ios * ios)19508f3dff7SShawn Lin static void dwcmshc_hs400_enhanced_strobe(struct mmc_host *mmc,
19608f3dff7SShawn Lin struct mmc_ios *ios)
19708f3dff7SShawn Lin {
19808f3dff7SShawn Lin u32 vendor;
19908f3dff7SShawn Lin struct sdhci_host *host = mmc_priv(mmc);
20008f3dff7SShawn Lin struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
20108f3dff7SShawn Lin struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
20208f3dff7SShawn Lin int reg = priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL;
20308f3dff7SShawn Lin
20408f3dff7SShawn Lin vendor = sdhci_readl(host, reg);
20508f3dff7SShawn Lin if (ios->enhanced_strobe)
20608f3dff7SShawn Lin vendor |= DWCMSHC_ENHANCED_STROBE;
20708f3dff7SShawn Lin else
20808f3dff7SShawn Lin vendor &= ~DWCMSHC_ENHANCED_STROBE;
20908f3dff7SShawn Lin
21008f3dff7SShawn Lin sdhci_writel(host, vendor, reg);
21108f3dff7SShawn Lin }
21208f3dff7SShawn Lin
dwcmshc_rk3568_set_clock(struct sdhci_host * host,unsigned int clock)21308f3dff7SShawn Lin static void dwcmshc_rk3568_set_clock(struct sdhci_host *host, unsigned int clock)
21408f3dff7SShawn Lin {
21508f3dff7SShawn Lin struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
21608f3dff7SShawn Lin struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
21786e1a8e1SSebastian Reichel struct rk35xx_priv *priv = dwc_priv->priv;
21808f3dff7SShawn Lin u8 txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
21908f3dff7SShawn Lin u32 extra, reg;
22008f3dff7SShawn Lin int err;
22108f3dff7SShawn Lin
22208f3dff7SShawn Lin host->mmc->actual_clock = 0;
22308f3dff7SShawn Lin
224c6f361cbSYifeng Zhao if (clock == 0) {
225c6f361cbSYifeng Zhao /* Disable interface clock at initial state. */
226c6f361cbSYifeng Zhao sdhci_set_clock(host, clock);
22708f3dff7SShawn Lin return;
228c6f361cbSYifeng Zhao }
22908f3dff7SShawn Lin
23008f3dff7SShawn Lin /* Rockchip platform only support 375KHz for identify mode */
23108f3dff7SShawn Lin if (clock <= 400000)
23208f3dff7SShawn Lin clock = 375000;
23308f3dff7SShawn Lin
23408f3dff7SShawn Lin err = clk_set_rate(pltfm_host->clk, clock);
23508f3dff7SShawn Lin if (err)
23608f3dff7SShawn Lin dev_err(mmc_dev(host->mmc), "fail to set clock %d", clock);
23708f3dff7SShawn Lin
23808f3dff7SShawn Lin sdhci_set_clock(host, clock);
23908f3dff7SShawn Lin
24008f3dff7SShawn Lin /* Disable cmd conflict check */
24108f3dff7SShawn Lin reg = dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3;
24208f3dff7SShawn Lin extra = sdhci_readl(host, reg);
24308f3dff7SShawn Lin extra &= ~BIT(0);
24408f3dff7SShawn Lin sdhci_writel(host, extra, reg);
24508f3dff7SShawn Lin
246c6f361cbSYifeng Zhao if (clock <= 52000000) {
247b75a52b0SShawn Lin /*
248b75a52b0SShawn Lin * Disable DLL and reset both of sample and drive clock.
249b75a52b0SShawn Lin * The bypass bit and start bit need to be set if DLL is not locked.
250b75a52b0SShawn Lin */
251b75a52b0SShawn Lin sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START, DWCMSHC_EMMC_DLL_CTRL);
252b75a52b0SShawn Lin sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
253c6f361cbSYifeng Zhao sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
254c6f361cbSYifeng Zhao sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
255c6f361cbSYifeng Zhao /*
256c6f361cbSYifeng Zhao * Before switching to hs400es mode, the driver will enable
257c6f361cbSYifeng Zhao * enhanced strobe first. PHY needs to configure the parameters
258c6f361cbSYifeng Zhao * of enhanced strobe first.
259c6f361cbSYifeng Zhao */
260c6f361cbSYifeng Zhao extra = DWCMSHC_EMMC_DLL_DLYENA |
261c6f361cbSYifeng Zhao DLL_STRBIN_DELAY_NUM_SEL |
262c6f361cbSYifeng Zhao DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
263c6f361cbSYifeng Zhao sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
26408f3dff7SShawn Lin return;
26508f3dff7SShawn Lin }
26608f3dff7SShawn Lin
26708f3dff7SShawn Lin /* Reset DLL */
26808f3dff7SShawn Lin sdhci_writel(host, BIT(1), DWCMSHC_EMMC_DLL_CTRL);
26908f3dff7SShawn Lin udelay(1);
27008f3dff7SShawn Lin sdhci_writel(host, 0x0, DWCMSHC_EMMC_DLL_CTRL);
27108f3dff7SShawn Lin
272c6f361cbSYifeng Zhao /*
273c6f361cbSYifeng Zhao * We shouldn't set DLL_RXCLK_NO_INVERTER for identify mode but
274c6f361cbSYifeng Zhao * we must set it in higher speed mode.
275c6f361cbSYifeng Zhao */
276c6f361cbSYifeng Zhao extra = DWCMSHC_EMMC_DLL_DLYENA;
277c6f361cbSYifeng Zhao if (priv->devtype == DWCMSHC_RK3568)
278c6f361cbSYifeng Zhao extra |= DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
279c6f361cbSYifeng Zhao sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
280c6f361cbSYifeng Zhao
28108f3dff7SShawn Lin /* Init DLL settings */
28208f3dff7SShawn Lin extra = 0x5 << DWCMSHC_EMMC_DLL_START_POINT |
28308f3dff7SShawn Lin 0x2 << DWCMSHC_EMMC_DLL_INC |
28408f3dff7SShawn Lin DWCMSHC_EMMC_DLL_START;
28508f3dff7SShawn Lin sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
28608f3dff7SShawn Lin err = readl_poll_timeout(host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
28708f3dff7SShawn Lin extra, DLL_LOCK_WO_TMOUT(extra), 1,
28808f3dff7SShawn Lin 500 * USEC_PER_MSEC);
28908f3dff7SShawn Lin if (err) {
29008f3dff7SShawn Lin dev_err(mmc_dev(host->mmc), "DLL lock timeout!\n");
29108f3dff7SShawn Lin return;
29208f3dff7SShawn Lin }
29308f3dff7SShawn Lin
29408f3dff7SShawn Lin extra = 0x1 << 16 | /* tune clock stop en */
295b75a52b0SShawn Lin 0x3 << 17 | /* pre-change delay */
29608f3dff7SShawn Lin 0x3 << 19; /* post-change delay */
29708f3dff7SShawn Lin sdhci_writel(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
29808f3dff7SShawn Lin
29908f3dff7SShawn Lin if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
30008f3dff7SShawn Lin host->mmc->ios.timing == MMC_TIMING_MMC_HS400)
30108f3dff7SShawn Lin txclk_tapnum = priv->txclk_tapnum;
30208f3dff7SShawn Lin
303c6f361cbSYifeng Zhao if ((priv->devtype == DWCMSHC_RK3588) && host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
304c6f361cbSYifeng Zhao txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES;
305c6f361cbSYifeng Zhao
306c6f361cbSYifeng Zhao extra = DLL_CMDOUT_SRC_CLK_NEG |
307c6f361cbSYifeng Zhao DLL_CMDOUT_EN_SRC_CLK_NEG |
308c6f361cbSYifeng Zhao DWCMSHC_EMMC_DLL_DLYENA |
309c6f361cbSYifeng Zhao DLL_CMDOUT_TAPNUM_90_DEGREES |
310c6f361cbSYifeng Zhao DLL_CMDOUT_TAPNUM_FROM_SW;
311c6f361cbSYifeng Zhao sdhci_writel(host, extra, DECMSHC_EMMC_DLL_CMDOUT);
312c6f361cbSYifeng Zhao }
313c6f361cbSYifeng Zhao
31408f3dff7SShawn Lin extra = DWCMSHC_EMMC_DLL_DLYENA |
31508f3dff7SShawn Lin DLL_TXCLK_TAPNUM_FROM_SW |
316c6f361cbSYifeng Zhao DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL |
31708f3dff7SShawn Lin txclk_tapnum;
31808f3dff7SShawn Lin sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
31908f3dff7SShawn Lin
32008f3dff7SShawn Lin extra = DWCMSHC_EMMC_DLL_DLYENA |
32108f3dff7SShawn Lin DLL_STRBIN_TAPNUM_DEFAULT |
32208f3dff7SShawn Lin DLL_STRBIN_TAPNUM_FROM_SW;
32308f3dff7SShawn Lin sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
32408f3dff7SShawn Lin }
32508f3dff7SShawn Lin
rk35xx_sdhci_reset(struct sdhci_host * host,u8 mask)32670f83220SYifeng Zhao static void rk35xx_sdhci_reset(struct sdhci_host *host, u8 mask)
32770f83220SYifeng Zhao {
32870f83220SYifeng Zhao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
32970f83220SYifeng Zhao struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
33070f83220SYifeng Zhao struct rk35xx_priv *priv = dwc_priv->priv;
33170f83220SYifeng Zhao
33270f83220SYifeng Zhao if (mask & SDHCI_RESET_ALL && priv->reset) {
33370f83220SYifeng Zhao reset_control_assert(priv->reset);
33470f83220SYifeng Zhao udelay(1);
33570f83220SYifeng Zhao reset_control_deassert(priv->reset);
33670f83220SYifeng Zhao }
33770f83220SYifeng Zhao
33870f83220SYifeng Zhao sdhci_reset(host, mask);
33970f83220SYifeng Zhao }
34070f83220SYifeng Zhao
341e438cf49SJisheng Zhang static const struct sdhci_ops sdhci_dwcmshc_ops = {
342e438cf49SJisheng Zhang .set_clock = sdhci_set_clock,
343e438cf49SJisheng Zhang .set_bus_width = sdhci_set_bus_width,
344554232e8SJisheng Zhang .set_uhs_signaling = dwcmshc_set_uhs_signaling,
345eb81ed51SLiming Sun .get_max_clock = dwcmshc_get_max_clock,
346e438cf49SJisheng Zhang .reset = sdhci_reset,
347b85c997dSJisheng Zhang .adma_write_desc = dwcmshc_adma_write_desc,
348e438cf49SJisheng Zhang };
349e438cf49SJisheng Zhang
35086e1a8e1SSebastian Reichel static const struct sdhci_ops sdhci_dwcmshc_rk35xx_ops = {
35108f3dff7SShawn Lin .set_clock = dwcmshc_rk3568_set_clock,
35208f3dff7SShawn Lin .set_bus_width = sdhci_set_bus_width,
35308f3dff7SShawn Lin .set_uhs_signaling = dwcmshc_set_uhs_signaling,
35449502408SVasily Khoruzhick .get_max_clock = rk35xx_get_max_clock,
35570f83220SYifeng Zhao .reset = rk35xx_sdhci_reset,
35608f3dff7SShawn Lin .adma_write_desc = dwcmshc_adma_write_desc,
35708f3dff7SShawn Lin };
35808f3dff7SShawn Lin
359e438cf49SJisheng Zhang static const struct sdhci_pltfm_data sdhci_dwcmshc_pdata = {
360e438cf49SJisheng Zhang .ops = &sdhci_dwcmshc_ops,
361e438cf49SJisheng Zhang .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
3625f7dfda4SJisheng Zhang .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
363e438cf49SJisheng Zhang };
364e438cf49SJisheng Zhang
365a0753ef6SLiming Sun #ifdef CONFIG_ACPI
366a0753ef6SLiming Sun static const struct sdhci_pltfm_data sdhci_dwcmshc_bf3_pdata = {
367a0753ef6SLiming Sun .ops = &sdhci_dwcmshc_ops,
368a0753ef6SLiming Sun .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
369a0753ef6SLiming Sun .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
370a0753ef6SLiming Sun SDHCI_QUIRK2_ACMD23_BROKEN,
371a0753ef6SLiming Sun };
372a0753ef6SLiming Sun #endif
373a0753ef6SLiming Sun
37486e1a8e1SSebastian Reichel static const struct sdhci_pltfm_data sdhci_dwcmshc_rk35xx_pdata = {
37586e1a8e1SSebastian Reichel .ops = &sdhci_dwcmshc_rk35xx_ops,
37608f3dff7SShawn Lin .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
37708f3dff7SShawn Lin SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
37808f3dff7SShawn Lin .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
37908f3dff7SShawn Lin SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
38008f3dff7SShawn Lin };
38108f3dff7SShawn Lin
dwcmshc_rk35xx_init(struct sdhci_host * host,struct dwcmshc_priv * dwc_priv)38286e1a8e1SSebastian Reichel static int dwcmshc_rk35xx_init(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
38308f3dff7SShawn Lin {
38408f3dff7SShawn Lin int err;
38586e1a8e1SSebastian Reichel struct rk35xx_priv *priv = dwc_priv->priv;
38608f3dff7SShawn Lin
38770f83220SYifeng Zhao priv->reset = devm_reset_control_array_get_optional_exclusive(mmc_dev(host->mmc));
38870f83220SYifeng Zhao if (IS_ERR(priv->reset)) {
38970f83220SYifeng Zhao err = PTR_ERR(priv->reset);
39070f83220SYifeng Zhao dev_err(mmc_dev(host->mmc), "failed to get reset control %d\n", err);
39170f83220SYifeng Zhao return err;
39270f83220SYifeng Zhao }
39370f83220SYifeng Zhao
39408f3dff7SShawn Lin priv->rockchip_clks[0].id = "axi";
39508f3dff7SShawn Lin priv->rockchip_clks[1].id = "block";
39608f3dff7SShawn Lin priv->rockchip_clks[2].id = "timer";
39786e1a8e1SSebastian Reichel err = devm_clk_bulk_get_optional(mmc_dev(host->mmc), RK35xx_MAX_CLKS,
39808f3dff7SShawn Lin priv->rockchip_clks);
39908f3dff7SShawn Lin if (err) {
40008f3dff7SShawn Lin dev_err(mmc_dev(host->mmc), "failed to get clocks %d\n", err);
40108f3dff7SShawn Lin return err;
40208f3dff7SShawn Lin }
40308f3dff7SShawn Lin
40486e1a8e1SSebastian Reichel err = clk_bulk_prepare_enable(RK35xx_MAX_CLKS, priv->rockchip_clks);
40508f3dff7SShawn Lin if (err) {
40608f3dff7SShawn Lin dev_err(mmc_dev(host->mmc), "failed to enable clocks %d\n", err);
40708f3dff7SShawn Lin return err;
40808f3dff7SShawn Lin }
40908f3dff7SShawn Lin
41008f3dff7SShawn Lin if (of_property_read_u8(mmc_dev(host->mmc)->of_node, "rockchip,txclk-tapnum",
41108f3dff7SShawn Lin &priv->txclk_tapnum))
41208f3dff7SShawn Lin priv->txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
41308f3dff7SShawn Lin
41408f3dff7SShawn Lin /* Disable cmd conflict check */
41508f3dff7SShawn Lin sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
41608f3dff7SShawn Lin /* Reset previous settings */
41708f3dff7SShawn Lin sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
41808f3dff7SShawn Lin sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
41908f3dff7SShawn Lin
42008f3dff7SShawn Lin return 0;
42108f3dff7SShawn Lin }
42208f3dff7SShawn Lin
dwcmshc_rk35xx_postinit(struct sdhci_host * host,struct dwcmshc_priv * dwc_priv)423c6f361cbSYifeng Zhao static void dwcmshc_rk35xx_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
424c6f361cbSYifeng Zhao {
425c6f361cbSYifeng Zhao /*
426c6f361cbSYifeng Zhao * Don't support highspeed bus mode with low clk speed as we
427c6f361cbSYifeng Zhao * cannot use DLL for this condition.
428c6f361cbSYifeng Zhao */
429c6f361cbSYifeng Zhao if (host->mmc->f_max <= 52000000) {
430c6f361cbSYifeng Zhao dev_info(mmc_dev(host->mmc), "Disabling HS200/HS400, frequency too low (%d)\n",
431c6f361cbSYifeng Zhao host->mmc->f_max);
432c6f361cbSYifeng Zhao host->mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400);
433c6f361cbSYifeng Zhao host->mmc->caps &= ~(MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR);
434c6f361cbSYifeng Zhao }
435c6f361cbSYifeng Zhao }
436c6f361cbSYifeng Zhao
43708f3dff7SShawn Lin static const struct of_device_id sdhci_dwcmshc_dt_ids[] = {
43808f3dff7SShawn Lin {
439c6f361cbSYifeng Zhao .compatible = "rockchip,rk3588-dwcmshc",
440c6f361cbSYifeng Zhao .data = &sdhci_dwcmshc_rk35xx_pdata,
441c6f361cbSYifeng Zhao },
442c6f361cbSYifeng Zhao {
44308f3dff7SShawn Lin .compatible = "rockchip,rk3568-dwcmshc",
44486e1a8e1SSebastian Reichel .data = &sdhci_dwcmshc_rk35xx_pdata,
44508f3dff7SShawn Lin },
44608f3dff7SShawn Lin {
44708f3dff7SShawn Lin .compatible = "snps,dwcmshc-sdhci",
44808f3dff7SShawn Lin .data = &sdhci_dwcmshc_pdata,
44908f3dff7SShawn Lin },
45008f3dff7SShawn Lin {},
45108f3dff7SShawn Lin };
45208f3dff7SShawn Lin MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids);
45308f3dff7SShawn Lin
454eb81ed51SLiming Sun #ifdef CONFIG_ACPI
455eb81ed51SLiming Sun static const struct acpi_device_id sdhci_dwcmshc_acpi_ids[] = {
456a0753ef6SLiming Sun {
457a0753ef6SLiming Sun .id = "MLNXBF30",
458a0753ef6SLiming Sun .driver_data = (kernel_ulong_t)&sdhci_dwcmshc_bf3_pdata,
459a0753ef6SLiming Sun },
460eb81ed51SLiming Sun {}
461eb81ed51SLiming Sun };
462cfd4ea48SLiming Sun MODULE_DEVICE_TABLE(acpi, sdhci_dwcmshc_acpi_ids);
463eb81ed51SLiming Sun #endif
464eb81ed51SLiming Sun
dwcmshc_probe(struct platform_device * pdev)465e438cf49SJisheng Zhang static int dwcmshc_probe(struct platform_device *pdev)
466e438cf49SJisheng Zhang {
467eb81ed51SLiming Sun struct device *dev = &pdev->dev;
468e438cf49SJisheng Zhang struct sdhci_pltfm_host *pltfm_host;
469e438cf49SJisheng Zhang struct sdhci_host *host;
470e438cf49SJisheng Zhang struct dwcmshc_priv *priv;
47186e1a8e1SSebastian Reichel struct rk35xx_priv *rk_priv = NULL;
47208f3dff7SShawn Lin const struct sdhci_pltfm_data *pltfm_data;
473e438cf49SJisheng Zhang int err;
474b85c997dSJisheng Zhang u32 extra;
475e438cf49SJisheng Zhang
476a0753ef6SLiming Sun pltfm_data = device_get_match_data(&pdev->dev);
47708f3dff7SShawn Lin if (!pltfm_data) {
47808f3dff7SShawn Lin dev_err(&pdev->dev, "Error: No device match data found\n");
47908f3dff7SShawn Lin return -ENODEV;
48008f3dff7SShawn Lin }
48108f3dff7SShawn Lin
48208f3dff7SShawn Lin host = sdhci_pltfm_init(pdev, pltfm_data,
483e438cf49SJisheng Zhang sizeof(struct dwcmshc_priv));
484e438cf49SJisheng Zhang if (IS_ERR(host))
485e438cf49SJisheng Zhang return PTR_ERR(host);
486e438cf49SJisheng Zhang
487b85c997dSJisheng Zhang /*
488b85c997dSJisheng Zhang * extra adma table cnt for cross 128M boundary handling.
489b85c997dSJisheng Zhang */
490eb81ed51SLiming Sun extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M);
491b85c997dSJisheng Zhang if (extra > SDHCI_MAX_SEGS)
492b85c997dSJisheng Zhang extra = SDHCI_MAX_SEGS;
493b85c997dSJisheng Zhang host->adma_table_cnt += extra;
494b85c997dSJisheng Zhang
495e438cf49SJisheng Zhang pltfm_host = sdhci_priv(host);
496e438cf49SJisheng Zhang priv = sdhci_pltfm_priv(pltfm_host);
497e438cf49SJisheng Zhang
498eb81ed51SLiming Sun if (dev->of_node) {
499eb81ed51SLiming Sun pltfm_host->clk = devm_clk_get(dev, "core");
500e438cf49SJisheng Zhang if (IS_ERR(pltfm_host->clk)) {
501e438cf49SJisheng Zhang err = PTR_ERR(pltfm_host->clk);
502eb81ed51SLiming Sun dev_err(dev, "failed to get core clk: %d\n", err);
503e438cf49SJisheng Zhang goto free_pltfm;
504e438cf49SJisheng Zhang }
505e438cf49SJisheng Zhang err = clk_prepare_enable(pltfm_host->clk);
506e438cf49SJisheng Zhang if (err)
507e438cf49SJisheng Zhang goto free_pltfm;
508e438cf49SJisheng Zhang
509eb81ed51SLiming Sun priv->bus_clk = devm_clk_get(dev, "bus");
510e438cf49SJisheng Zhang if (!IS_ERR(priv->bus_clk))
511e438cf49SJisheng Zhang clk_prepare_enable(priv->bus_clk);
512eb81ed51SLiming Sun }
513e438cf49SJisheng Zhang
514e438cf49SJisheng Zhang err = mmc_of_parse(host->mmc);
515e438cf49SJisheng Zhang if (err)
516e438cf49SJisheng Zhang goto err_clk;
517e438cf49SJisheng Zhang
518e438cf49SJisheng Zhang sdhci_get_of_property(pdev);
519e438cf49SJisheng Zhang
52008f3dff7SShawn Lin priv->vendor_specific_area1 =
52108f3dff7SShawn Lin sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK;
52208f3dff7SShawn Lin
523ca1219c0SJisheng Zhang host->mmc_host_ops.request = dwcmshc_request;
52408f3dff7SShawn Lin host->mmc_host_ops.hs400_enhanced_strobe = dwcmshc_hs400_enhanced_strobe;
52508f3dff7SShawn Lin
52686e1a8e1SSebastian Reichel if (pltfm_data == &sdhci_dwcmshc_rk35xx_pdata) {
52786e1a8e1SSebastian Reichel rk_priv = devm_kzalloc(&pdev->dev, sizeof(struct rk35xx_priv), GFP_KERNEL);
52834884c4fSWei Yongjun if (!rk_priv) {
52934884c4fSWei Yongjun err = -ENOMEM;
53008f3dff7SShawn Lin goto err_clk;
53134884c4fSWei Yongjun }
53208f3dff7SShawn Lin
533c6f361cbSYifeng Zhao if (of_device_is_compatible(pdev->dev.of_node, "rockchip,rk3588-dwcmshc"))
534c6f361cbSYifeng Zhao rk_priv->devtype = DWCMSHC_RK3588;
535c6f361cbSYifeng Zhao else
536c6f361cbSYifeng Zhao rk_priv->devtype = DWCMSHC_RK3568;
537c6f361cbSYifeng Zhao
53808f3dff7SShawn Lin priv->priv = rk_priv;
53908f3dff7SShawn Lin
54086e1a8e1SSebastian Reichel err = dwcmshc_rk35xx_init(host, priv);
54108f3dff7SShawn Lin if (err)
54208f3dff7SShawn Lin goto err_clk;
54308f3dff7SShawn Lin }
544ca1219c0SJisheng Zhang
54595921151SLiming Sun #ifdef CONFIG_ACPI
54695921151SLiming Sun if (pltfm_data == &sdhci_dwcmshc_bf3_pdata)
54795921151SLiming Sun sdhci_enable_v4_mode(host);
54895921151SLiming Sun #endif
54995921151SLiming Sun
55057ac3084SJisheng Zhang host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
55157ac3084SJisheng Zhang
55248fe8fadSLiming Sun pm_runtime_get_noresume(dev);
55348fe8fadSLiming Sun pm_runtime_set_active(dev);
55448fe8fadSLiming Sun pm_runtime_enable(dev);
55548fe8fadSLiming Sun
556c6f361cbSYifeng Zhao err = sdhci_setup_host(host);
557e438cf49SJisheng Zhang if (err)
55848fe8fadSLiming Sun goto err_rpm;
559e438cf49SJisheng Zhang
560c6f361cbSYifeng Zhao if (rk_priv)
561c6f361cbSYifeng Zhao dwcmshc_rk35xx_postinit(host, priv);
562c6f361cbSYifeng Zhao
563c6f361cbSYifeng Zhao err = __sdhci_add_host(host);
564c6f361cbSYifeng Zhao if (err)
565c6f361cbSYifeng Zhao goto err_setup_host;
566c6f361cbSYifeng Zhao
56748fe8fadSLiming Sun pm_runtime_put(dev);
56848fe8fadSLiming Sun
569e438cf49SJisheng Zhang return 0;
570e438cf49SJisheng Zhang
571c6f361cbSYifeng Zhao err_setup_host:
572c6f361cbSYifeng Zhao sdhci_cleanup_host(host);
57348fe8fadSLiming Sun err_rpm:
57448fe8fadSLiming Sun pm_runtime_disable(dev);
57548fe8fadSLiming Sun pm_runtime_put_noidle(dev);
576e438cf49SJisheng Zhang err_clk:
577e438cf49SJisheng Zhang clk_disable_unprepare(pltfm_host->clk);
578e438cf49SJisheng Zhang clk_disable_unprepare(priv->bus_clk);
57908f3dff7SShawn Lin if (rk_priv)
58086e1a8e1SSebastian Reichel clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
58108f3dff7SShawn Lin rk_priv->rockchip_clks);
582e438cf49SJisheng Zhang free_pltfm:
583e438cf49SJisheng Zhang sdhci_pltfm_free(pdev);
584e438cf49SJisheng Zhang return err;
585e438cf49SJisheng Zhang }
586e438cf49SJisheng Zhang
dwcmshc_disable_card_clk(struct sdhci_host * host)587*15279912SLiming Sun static void dwcmshc_disable_card_clk(struct sdhci_host *host)
588*15279912SLiming Sun {
589*15279912SLiming Sun u16 ctrl;
590*15279912SLiming Sun
591*15279912SLiming Sun ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
592*15279912SLiming Sun if (ctrl & SDHCI_CLOCK_CARD_EN) {
593*15279912SLiming Sun ctrl &= ~SDHCI_CLOCK_CARD_EN;
594*15279912SLiming Sun sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
595*15279912SLiming Sun }
596*15279912SLiming Sun }
597*15279912SLiming Sun
dwcmshc_remove(struct platform_device * pdev)5985905a1f1SYangtao Li static void dwcmshc_remove(struct platform_device *pdev)
599e438cf49SJisheng Zhang {
600e438cf49SJisheng Zhang struct sdhci_host *host = platform_get_drvdata(pdev);
601e438cf49SJisheng Zhang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
602e438cf49SJisheng Zhang struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
60386e1a8e1SSebastian Reichel struct rk35xx_priv *rk_priv = priv->priv;
604e438cf49SJisheng Zhang
605*15279912SLiming Sun pm_runtime_get_sync(&pdev->dev);
606*15279912SLiming Sun pm_runtime_disable(&pdev->dev);
607*15279912SLiming Sun pm_runtime_put_noidle(&pdev->dev);
608*15279912SLiming Sun
609e438cf49SJisheng Zhang sdhci_remove_host(host, 0);
610e438cf49SJisheng Zhang
611*15279912SLiming Sun dwcmshc_disable_card_clk(host);
612*15279912SLiming Sun
613e438cf49SJisheng Zhang clk_disable_unprepare(pltfm_host->clk);
614e438cf49SJisheng Zhang clk_disable_unprepare(priv->bus_clk);
61508f3dff7SShawn Lin if (rk_priv)
61686e1a8e1SSebastian Reichel clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
61708f3dff7SShawn Lin rk_priv->rockchip_clks);
618e438cf49SJisheng Zhang sdhci_pltfm_free(pdev);
619e438cf49SJisheng Zhang }
620e438cf49SJisheng Zhang
621bccce2ecSJisheng Zhang #ifdef CONFIG_PM_SLEEP
dwcmshc_suspend(struct device * dev)622bccce2ecSJisheng Zhang static int dwcmshc_suspend(struct device *dev)
623bccce2ecSJisheng Zhang {
624bccce2ecSJisheng Zhang struct sdhci_host *host = dev_get_drvdata(dev);
625bccce2ecSJisheng Zhang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
626bccce2ecSJisheng Zhang struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
62786e1a8e1SSebastian Reichel struct rk35xx_priv *rk_priv = priv->priv;
628bccce2ecSJisheng Zhang int ret;
629bccce2ecSJisheng Zhang
63048fe8fadSLiming Sun pm_runtime_resume(dev);
63148fe8fadSLiming Sun
632bccce2ecSJisheng Zhang ret = sdhci_suspend_host(host);
633bccce2ecSJisheng Zhang if (ret)
634bccce2ecSJisheng Zhang return ret;
635bccce2ecSJisheng Zhang
636bccce2ecSJisheng Zhang clk_disable_unprepare(pltfm_host->clk);
637bccce2ecSJisheng Zhang if (!IS_ERR(priv->bus_clk))
638bccce2ecSJisheng Zhang clk_disable_unprepare(priv->bus_clk);
639bccce2ecSJisheng Zhang
64008f3dff7SShawn Lin if (rk_priv)
64186e1a8e1SSebastian Reichel clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
64208f3dff7SShawn Lin rk_priv->rockchip_clks);
64308f3dff7SShawn Lin
644bccce2ecSJisheng Zhang return ret;
645bccce2ecSJisheng Zhang }
646bccce2ecSJisheng Zhang
dwcmshc_resume(struct device * dev)647bccce2ecSJisheng Zhang static int dwcmshc_resume(struct device *dev)
648bccce2ecSJisheng Zhang {
649bccce2ecSJisheng Zhang struct sdhci_host *host = dev_get_drvdata(dev);
650bccce2ecSJisheng Zhang struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
651bccce2ecSJisheng Zhang struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
65286e1a8e1SSebastian Reichel struct rk35xx_priv *rk_priv = priv->priv;
653bccce2ecSJisheng Zhang int ret;
654bccce2ecSJisheng Zhang
655bccce2ecSJisheng Zhang ret = clk_prepare_enable(pltfm_host->clk);
656bccce2ecSJisheng Zhang if (ret)
657bccce2ecSJisheng Zhang return ret;
658bccce2ecSJisheng Zhang
659bccce2ecSJisheng Zhang if (!IS_ERR(priv->bus_clk)) {
660bccce2ecSJisheng Zhang ret = clk_prepare_enable(priv->bus_clk);
661bccce2ecSJisheng Zhang if (ret)
662a11937b3SLiming Sun goto disable_clk;
663bccce2ecSJisheng Zhang }
664bccce2ecSJisheng Zhang
66508f3dff7SShawn Lin if (rk_priv) {
66686e1a8e1SSebastian Reichel ret = clk_bulk_prepare_enable(RK35xx_MAX_CLKS,
66708f3dff7SShawn Lin rk_priv->rockchip_clks);
66808f3dff7SShawn Lin if (ret)
669a11937b3SLiming Sun goto disable_bus_clk;
67008f3dff7SShawn Lin }
67108f3dff7SShawn Lin
672a11937b3SLiming Sun ret = sdhci_resume_host(host);
673a11937b3SLiming Sun if (ret)
674a11937b3SLiming Sun goto disable_rockchip_clks;
675a11937b3SLiming Sun
676a11937b3SLiming Sun return 0;
677a11937b3SLiming Sun
678a11937b3SLiming Sun disable_rockchip_clks:
679a11937b3SLiming Sun if (rk_priv)
680a11937b3SLiming Sun clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
681a11937b3SLiming Sun rk_priv->rockchip_clks);
682a11937b3SLiming Sun disable_bus_clk:
683a11937b3SLiming Sun if (!IS_ERR(priv->bus_clk))
684a11937b3SLiming Sun clk_disable_unprepare(priv->bus_clk);
685a11937b3SLiming Sun disable_clk:
686a11937b3SLiming Sun clk_disable_unprepare(pltfm_host->clk);
687a11937b3SLiming Sun return ret;
688bccce2ecSJisheng Zhang }
689bccce2ecSJisheng Zhang #endif
690bccce2ecSJisheng Zhang
69148fe8fadSLiming Sun #ifdef CONFIG_PM
69248fe8fadSLiming Sun
dwcmshc_enable_card_clk(struct sdhci_host * host)69348fe8fadSLiming Sun static void dwcmshc_enable_card_clk(struct sdhci_host *host)
69448fe8fadSLiming Sun {
69548fe8fadSLiming Sun u16 ctrl;
69648fe8fadSLiming Sun
69748fe8fadSLiming Sun ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
69848fe8fadSLiming Sun if ((ctrl & SDHCI_CLOCK_INT_EN) && !(ctrl & SDHCI_CLOCK_CARD_EN)) {
69948fe8fadSLiming Sun ctrl |= SDHCI_CLOCK_CARD_EN;
70048fe8fadSLiming Sun sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
70148fe8fadSLiming Sun }
70248fe8fadSLiming Sun }
70348fe8fadSLiming Sun
dwcmshc_runtime_suspend(struct device * dev)70448fe8fadSLiming Sun static int dwcmshc_runtime_suspend(struct device *dev)
70548fe8fadSLiming Sun {
70648fe8fadSLiming Sun struct sdhci_host *host = dev_get_drvdata(dev);
70748fe8fadSLiming Sun
70848fe8fadSLiming Sun dwcmshc_disable_card_clk(host);
70948fe8fadSLiming Sun
71048fe8fadSLiming Sun return 0;
71148fe8fadSLiming Sun }
71248fe8fadSLiming Sun
dwcmshc_runtime_resume(struct device * dev)71348fe8fadSLiming Sun static int dwcmshc_runtime_resume(struct device *dev)
71448fe8fadSLiming Sun {
71548fe8fadSLiming Sun struct sdhci_host *host = dev_get_drvdata(dev);
71648fe8fadSLiming Sun
71748fe8fadSLiming Sun dwcmshc_enable_card_clk(host);
71848fe8fadSLiming Sun
71948fe8fadSLiming Sun return 0;
72048fe8fadSLiming Sun }
72148fe8fadSLiming Sun
72248fe8fadSLiming Sun #endif
72348fe8fadSLiming Sun
72448fe8fadSLiming Sun static const struct dev_pm_ops dwcmshc_pmops = {
72548fe8fadSLiming Sun SET_SYSTEM_SLEEP_PM_OPS(dwcmshc_suspend, dwcmshc_resume)
72648fe8fadSLiming Sun SET_RUNTIME_PM_OPS(dwcmshc_runtime_suspend,
72748fe8fadSLiming Sun dwcmshc_runtime_resume, NULL)
72848fe8fadSLiming Sun };
729bccce2ecSJisheng Zhang
730e438cf49SJisheng Zhang static struct platform_driver sdhci_dwcmshc_driver = {
731e438cf49SJisheng Zhang .driver = {
732e438cf49SJisheng Zhang .name = "sdhci-dwcmshc",
733a1a48919SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
734e438cf49SJisheng Zhang .of_match_table = sdhci_dwcmshc_dt_ids,
735eb81ed51SLiming Sun .acpi_match_table = ACPI_PTR(sdhci_dwcmshc_acpi_ids),
736bccce2ecSJisheng Zhang .pm = &dwcmshc_pmops,
737e438cf49SJisheng Zhang },
738e438cf49SJisheng Zhang .probe = dwcmshc_probe,
7395905a1f1SYangtao Li .remove_new = dwcmshc_remove,
740e438cf49SJisheng Zhang };
741e438cf49SJisheng Zhang module_platform_driver(sdhci_dwcmshc_driver);
742e438cf49SJisheng Zhang
743e438cf49SJisheng Zhang MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC");
744e438cf49SJisheng Zhang MODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
745e438cf49SJisheng Zhang MODULE_LICENSE("GPL v2");
746