1a702c8abSZhangfei Gao /* 2a702c8abSZhangfei Gao * Copyright (C) 2010 Marvell International Ltd. 3a702c8abSZhangfei Gao * Zhangfei Gao <zhangfei.gao@marvell.com> 4a702c8abSZhangfei Gao * Kevin Wang <dwang4@marvell.com> 5a702c8abSZhangfei Gao * Mingwei Wang <mwwang@marvell.com> 6a702c8abSZhangfei Gao * Philip Rakity <prakity@marvell.com> 7a702c8abSZhangfei Gao * Mark Brown <markb@marvell.com> 8a702c8abSZhangfei Gao * 9a702c8abSZhangfei Gao * This software is licensed under the terms of the GNU General Public 10a702c8abSZhangfei Gao * License version 2, as published by the Free Software Foundation, and 11a702c8abSZhangfei Gao * may be copied, distributed, and modified under those terms. 12a702c8abSZhangfei Gao * 13a702c8abSZhangfei Gao * This program is distributed in the hope that it will be useful, 14a702c8abSZhangfei Gao * but WITHOUT ANY WARRANTY; without even the implied warranty of 15a702c8abSZhangfei Gao * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a702c8abSZhangfei Gao * GNU General Public License for more details. 17a702c8abSZhangfei Gao * 18a702c8abSZhangfei Gao */ 19a702c8abSZhangfei Gao #include <linux/err.h> 20a702c8abSZhangfei Gao #include <linux/init.h> 21a702c8abSZhangfei Gao #include <linux/platform_device.h> 22a702c8abSZhangfei Gao #include <linux/clk.h> 23a702c8abSZhangfei Gao #include <linux/io.h> 24a702c8abSZhangfei Gao #include <linux/gpio.h> 25a702c8abSZhangfei Gao #include <linux/mmc/card.h> 26a702c8abSZhangfei Gao #include <linux/mmc/host.h> 278f63795cSChris Ball #include <linux/mmc/slot-gpio.h> 28bfed345eSZhangfei Gao #include <linux/platform_data/pxa_sdhci.h> 29a702c8abSZhangfei Gao #include <linux/slab.h> 30a702c8abSZhangfei Gao #include <linux/delay.h> 3188b47679SPaul Gortmaker #include <linux/module.h> 32b650352dSChris Ball #include <linux/of.h> 33b650352dSChris Ball #include <linux/of_device.h> 348f63795cSChris Ball #include <linux/of_gpio.h> 35bb691ae4SKevin Liu #include <linux/pm.h> 36bb691ae4SKevin Liu #include <linux/pm_runtime.h> 375491ce3fSMarcin Wojtas #include <linux/mbus.h> 38b650352dSChris Ball 39a702c8abSZhangfei Gao #include "sdhci.h" 40a702c8abSZhangfei Gao #include "sdhci-pltfm.h" 41a702c8abSZhangfei Gao 42bb691ae4SKevin Liu #define PXAV3_RPM_DELAY_MS 50 43bb691ae4SKevin Liu 44a702c8abSZhangfei Gao #define SD_CLOCK_BURST_SIZE_SETUP 0x10A 45a702c8abSZhangfei Gao #define SDCLK_SEL 0x100 46a702c8abSZhangfei Gao #define SDCLK_DELAY_SHIFT 9 47a702c8abSZhangfei Gao #define SDCLK_DELAY_MASK 0x1f 48a702c8abSZhangfei Gao 49a702c8abSZhangfei Gao #define SD_CFG_FIFO_PARAM 0x100 50a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_ON (1<<6) 51a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_CNT_MASK 0xFF 52a702c8abSZhangfei Gao #define SDCFG_GEN_PAD_CLK_CNT_SHIFT 24 53a702c8abSZhangfei Gao 54a702c8abSZhangfei Gao #define SD_SPI_MODE 0x108 55a702c8abSZhangfei Gao #define SD_CE_ATA_1 0x10C 56a702c8abSZhangfei Gao 57a702c8abSZhangfei Gao #define SD_CE_ATA_2 0x10E 58a702c8abSZhangfei Gao #define SDCE_MISC_INT (1<<2) 59a702c8abSZhangfei Gao #define SDCE_MISC_INT_EN (1<<1) 60a702c8abSZhangfei Gao 615491ce3fSMarcin Wojtas /* 625491ce3fSMarcin Wojtas * These registers are relative to the second register region, for the 635491ce3fSMarcin Wojtas * MBus bridge. 645491ce3fSMarcin Wojtas */ 655491ce3fSMarcin Wojtas #define SDHCI_WINDOW_CTRL(i) (0x80 + ((i) << 3)) 665491ce3fSMarcin Wojtas #define SDHCI_WINDOW_BASE(i) (0x84 + ((i) << 3)) 675491ce3fSMarcin Wojtas #define SDHCI_MAX_WIN_NUM 8 685491ce3fSMarcin Wojtas 695491ce3fSMarcin Wojtas static int mv_conf_mbus_windows(struct platform_device *pdev, 705491ce3fSMarcin Wojtas const struct mbus_dram_target_info *dram) 715491ce3fSMarcin Wojtas { 725491ce3fSMarcin Wojtas int i; 735491ce3fSMarcin Wojtas void __iomem *regs; 745491ce3fSMarcin Wojtas struct resource *res; 755491ce3fSMarcin Wojtas 765491ce3fSMarcin Wojtas if (!dram) { 775491ce3fSMarcin Wojtas dev_err(&pdev->dev, "no mbus dram info\n"); 785491ce3fSMarcin Wojtas return -EINVAL; 795491ce3fSMarcin Wojtas } 805491ce3fSMarcin Wojtas 815491ce3fSMarcin Wojtas res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 825491ce3fSMarcin Wojtas if (!res) { 835491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot get mbus registers\n"); 845491ce3fSMarcin Wojtas return -EINVAL; 855491ce3fSMarcin Wojtas } 865491ce3fSMarcin Wojtas 875491ce3fSMarcin Wojtas regs = ioremap(res->start, resource_size(res)); 885491ce3fSMarcin Wojtas if (!regs) { 895491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot map mbus registers\n"); 905491ce3fSMarcin Wojtas return -ENOMEM; 915491ce3fSMarcin Wojtas } 925491ce3fSMarcin Wojtas 935491ce3fSMarcin Wojtas for (i = 0; i < SDHCI_MAX_WIN_NUM; i++) { 945491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_CTRL(i)); 955491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_BASE(i)); 965491ce3fSMarcin Wojtas } 975491ce3fSMarcin Wojtas 985491ce3fSMarcin Wojtas for (i = 0; i < dram->num_cs; i++) { 995491ce3fSMarcin Wojtas const struct mbus_dram_window *cs = dram->cs + i; 1005491ce3fSMarcin Wojtas 1015491ce3fSMarcin Wojtas /* Write size, attributes and target id to control register */ 1025491ce3fSMarcin Wojtas writel(((cs->size - 1) & 0xffff0000) | 1035491ce3fSMarcin Wojtas (cs->mbus_attr << 8) | 1045491ce3fSMarcin Wojtas (dram->mbus_dram_target_id << 4) | 1, 1055491ce3fSMarcin Wojtas regs + SDHCI_WINDOW_CTRL(i)); 1065491ce3fSMarcin Wojtas /* Write base address to base register */ 1075491ce3fSMarcin Wojtas writel(cs->base, regs + SDHCI_WINDOW_BASE(i)); 1085491ce3fSMarcin Wojtas } 1095491ce3fSMarcin Wojtas 1105491ce3fSMarcin Wojtas iounmap(regs); 1115491ce3fSMarcin Wojtas 1125491ce3fSMarcin Wojtas return 0; 1135491ce3fSMarcin Wojtas } 1145491ce3fSMarcin Wojtas 11503231f9bSRussell King static void pxav3_reset(struct sdhci_host *host, u8 mask) 116a702c8abSZhangfei Gao { 117a702c8abSZhangfei Gao struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); 118a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; 119a702c8abSZhangfei Gao 12003231f9bSRussell King sdhci_reset(host, mask); 12103231f9bSRussell King 122a702c8abSZhangfei Gao if (mask == SDHCI_RESET_ALL) { 123a702c8abSZhangfei Gao /* 124a702c8abSZhangfei Gao * tune timing of read data/command when crc error happen 125a702c8abSZhangfei Gao * no performance impact 126a702c8abSZhangfei Gao */ 127a702c8abSZhangfei Gao if (pdata && 0 != pdata->clk_delay_cycles) { 128a702c8abSZhangfei Gao u16 tmp; 129a702c8abSZhangfei Gao 130a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); 131a702c8abSZhangfei Gao tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK) 132a702c8abSZhangfei Gao << SDCLK_DELAY_SHIFT; 133a702c8abSZhangfei Gao tmp |= SDCLK_SEL; 134a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); 135a702c8abSZhangfei Gao } 136a702c8abSZhangfei Gao } 137a702c8abSZhangfei Gao } 138a702c8abSZhangfei Gao 139a702c8abSZhangfei Gao #define MAX_WAIT_COUNT 5 140a702c8abSZhangfei Gao static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode) 141a702c8abSZhangfei Gao { 142a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 143a702c8abSZhangfei Gao struct sdhci_pxa *pxa = pltfm_host->priv; 144a702c8abSZhangfei Gao u16 tmp; 145a702c8abSZhangfei Gao int count; 146a702c8abSZhangfei Gao 147a702c8abSZhangfei Gao if (pxa->power_mode == MMC_POWER_UP 148a702c8abSZhangfei Gao && power_mode == MMC_POWER_ON) { 149a702c8abSZhangfei Gao 150a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc), 151a702c8abSZhangfei Gao "%s: slot->power_mode = %d," 152a702c8abSZhangfei Gao "ios->power_mode = %d\n", 153a702c8abSZhangfei Gao __func__, 154a702c8abSZhangfei Gao pxa->power_mode, 155a702c8abSZhangfei Gao power_mode); 156a702c8abSZhangfei Gao 157a702c8abSZhangfei Gao /* set we want notice of when 74 clocks are sent */ 158a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2); 159a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT_EN; 160a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2); 161a702c8abSZhangfei Gao 162a702c8abSZhangfei Gao /* start sending the 74 clocks */ 163a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM); 164a702c8abSZhangfei Gao tmp |= SDCFG_GEN_PAD_CLK_ON; 165a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM); 166a702c8abSZhangfei Gao 167a702c8abSZhangfei Gao /* slowest speed is about 100KHz or 10usec per clock */ 168a702c8abSZhangfei Gao udelay(740); 169a702c8abSZhangfei Gao count = 0; 170a702c8abSZhangfei Gao 171a702c8abSZhangfei Gao while (count++ < MAX_WAIT_COUNT) { 172a702c8abSZhangfei Gao if ((readw(host->ioaddr + SD_CE_ATA_2) 173a702c8abSZhangfei Gao & SDCE_MISC_INT) == 0) 174a702c8abSZhangfei Gao break; 175a702c8abSZhangfei Gao udelay(10); 176a702c8abSZhangfei Gao } 177a702c8abSZhangfei Gao 178a702c8abSZhangfei Gao if (count == MAX_WAIT_COUNT) 179a702c8abSZhangfei Gao dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n"); 180a702c8abSZhangfei Gao 181a702c8abSZhangfei Gao /* clear the interrupt bit if posted */ 182a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2); 183a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT; 184a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2); 185a702c8abSZhangfei Gao } 186a702c8abSZhangfei Gao pxa->power_mode = power_mode; 187a702c8abSZhangfei Gao } 188a702c8abSZhangfei Gao 18913e64501SRussell King static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs) 190a702c8abSZhangfei Gao { 191a702c8abSZhangfei Gao u16 ctrl_2; 192a702c8abSZhangfei Gao 193a702c8abSZhangfei Gao /* 194a702c8abSZhangfei Gao * Set V18_EN -- UHS modes do not work without this. 195a702c8abSZhangfei Gao * does not change signaling voltage 196a702c8abSZhangfei Gao */ 197a702c8abSZhangfei Gao ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); 198a702c8abSZhangfei Gao 199a702c8abSZhangfei Gao /* Select Bus Speed Mode for host */ 200a702c8abSZhangfei Gao ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; 201a702c8abSZhangfei Gao switch (uhs) { 202a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR12: 203a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR12; 204a702c8abSZhangfei Gao break; 205a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR25: 206a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR25; 207a702c8abSZhangfei Gao break; 208a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR50: 209a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180; 210a702c8abSZhangfei Gao break; 211a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR104: 212a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180; 213a702c8abSZhangfei Gao break; 214*668e84b2SSebastian Hesselbarth case MMC_TIMING_MMC_DDR52: 215a702c8abSZhangfei Gao case MMC_TIMING_UHS_DDR50: 216a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180; 217a702c8abSZhangfei Gao break; 218a702c8abSZhangfei Gao } 219a702c8abSZhangfei Gao 220a702c8abSZhangfei Gao sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); 221a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc), 222a702c8abSZhangfei Gao "%s uhs = %d, ctrl_2 = %04X\n", 223a702c8abSZhangfei Gao __func__, uhs, ctrl_2); 224a702c8abSZhangfei Gao } 225a702c8abSZhangfei Gao 226c915568dSLars-Peter Clausen static const struct sdhci_ops pxav3_sdhci_ops = { 2271771059cSRussell King .set_clock = sdhci_set_clock, 228a702c8abSZhangfei Gao .platform_send_init_74_clocks = pxav3_gen_init_74_clocks, 229d005d943SLars-Peter Clausen .get_max_clock = sdhci_pltfm_clk_get_max_clock, 2302317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 23103231f9bSRussell King .reset = pxav3_reset, 232b3153765SPeter Griffin .set_uhs_signaling = pxav3_set_uhs_signaling, 233a702c8abSZhangfei Gao }; 234a702c8abSZhangfei Gao 23573b7afb9SKevin Liu static struct sdhci_pltfm_data sdhci_pxav3_pdata = { 236e065162aSKevin Liu .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK 23773b7afb9SKevin Liu | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC 23873b7afb9SKevin Liu | SDHCI_QUIRK_32BIT_ADMA_SIZE 23973b7afb9SKevin Liu | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 24073b7afb9SKevin Liu .ops = &pxav3_sdhci_ops, 24173b7afb9SKevin Liu }; 24273b7afb9SKevin Liu 243b650352dSChris Ball #ifdef CONFIG_OF 244b650352dSChris Ball static const struct of_device_id sdhci_pxav3_of_match[] = { 245b650352dSChris Ball { 246b650352dSChris Ball .compatible = "mrvl,pxav3-mmc", 247b650352dSChris Ball }, 2485491ce3fSMarcin Wojtas { 2495491ce3fSMarcin Wojtas .compatible = "marvell,armada-380-sdhci", 2505491ce3fSMarcin Wojtas }, 251b650352dSChris Ball {}, 252b650352dSChris Ball }; 253b650352dSChris Ball MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match); 254b650352dSChris Ball 255b650352dSChris Ball static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev) 256b650352dSChris Ball { 257b650352dSChris Ball struct sdhci_pxa_platdata *pdata; 258b650352dSChris Ball struct device_node *np = dev->of_node; 259b650352dSChris Ball u32 clk_delay_cycles; 260b650352dSChris Ball 261b650352dSChris Ball pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 262b650352dSChris Ball if (!pdata) 263b650352dSChris Ball return NULL; 264b650352dSChris Ball 265b650352dSChris Ball of_property_read_u32(np, "mrvl,clk-delay-cycles", &clk_delay_cycles); 266b650352dSChris Ball if (clk_delay_cycles > 0) 267b650352dSChris Ball pdata->clk_delay_cycles = clk_delay_cycles; 268b650352dSChris Ball 269b650352dSChris Ball return pdata; 270b650352dSChris Ball } 271b650352dSChris Ball #else 272b650352dSChris Ball static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev) 273b650352dSChris Ball { 274b650352dSChris Ball return NULL; 275b650352dSChris Ball } 276b650352dSChris Ball #endif 277b650352dSChris Ball 278c3be1efdSBill Pemberton static int sdhci_pxav3_probe(struct platform_device *pdev) 279a702c8abSZhangfei Gao { 280a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host; 281a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; 282a702c8abSZhangfei Gao struct device *dev = &pdev->dev; 2835491ce3fSMarcin Wojtas struct device_node *np = pdev->dev.of_node; 284a702c8abSZhangfei Gao struct sdhci_host *host = NULL; 285a702c8abSZhangfei Gao struct sdhci_pxa *pxa = NULL; 286b650352dSChris Ball const struct of_device_id *match; 287b650352dSChris Ball 288a702c8abSZhangfei Gao int ret; 289a702c8abSZhangfei Gao struct clk *clk; 290a702c8abSZhangfei Gao 2913df5b281SLaurent Pinchart pxa = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_pxa), GFP_KERNEL); 292a702c8abSZhangfei Gao if (!pxa) 293a702c8abSZhangfei Gao return -ENOMEM; 294a702c8abSZhangfei Gao 2950e748234SChristian Daudt host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, 0); 2963df5b281SLaurent Pinchart if (IS_ERR(host)) 297a702c8abSZhangfei Gao return PTR_ERR(host); 2985491ce3fSMarcin Wojtas 2995491ce3fSMarcin Wojtas if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) { 3005491ce3fSMarcin Wojtas ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info()); 3015491ce3fSMarcin Wojtas if (ret < 0) 3025491ce3fSMarcin Wojtas goto err_mbus_win; 3035491ce3fSMarcin Wojtas } 3045491ce3fSMarcin Wojtas 3055491ce3fSMarcin Wojtas 306a702c8abSZhangfei Gao pltfm_host = sdhci_priv(host); 307a702c8abSZhangfei Gao pltfm_host->priv = pxa; 308a702c8abSZhangfei Gao 3093df5b281SLaurent Pinchart clk = devm_clk_get(dev, NULL); 310a702c8abSZhangfei Gao if (IS_ERR(clk)) { 311a702c8abSZhangfei Gao dev_err(dev, "failed to get io clock\n"); 312a702c8abSZhangfei Gao ret = PTR_ERR(clk); 313a702c8abSZhangfei Gao goto err_clk_get; 314a702c8abSZhangfei Gao } 315a702c8abSZhangfei Gao pltfm_host->clk = clk; 316164378efSChao Xie clk_prepare_enable(clk); 317a702c8abSZhangfei Gao 318a702c8abSZhangfei Gao /* enable 1/8V DDR capable */ 319a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_1_8V_DDR; 320a702c8abSZhangfei Gao 321b650352dSChris Ball match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev); 322943647f6SKevin Liu if (match) { 323d2cf6071SSimon Baatz ret = mmc_of_parse(host->mmc); 324d2cf6071SSimon Baatz if (ret) 325d2cf6071SSimon Baatz goto err_of_parse; 326943647f6SKevin Liu sdhci_get_of_property(pdev); 327b650352dSChris Ball pdata = pxav3_get_mmc_pdata(dev); 328943647f6SKevin Liu } else if (pdata) { 329a702c8abSZhangfei Gao /* on-chip device */ 330c844a46fSKevin Liu if (pdata->flags & PXA_FLAG_CARD_PERMANENT) 331a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_NONREMOVABLE; 332a702c8abSZhangfei Gao 333a702c8abSZhangfei Gao /* If slot design supports 8 bit data, indicate this to MMC. */ 334a702c8abSZhangfei Gao if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT) 335a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_8_BIT_DATA; 336a702c8abSZhangfei Gao 337a702c8abSZhangfei Gao if (pdata->quirks) 338a702c8abSZhangfei Gao host->quirks |= pdata->quirks; 3397c52d7bbSKevin Liu if (pdata->quirks2) 3407c52d7bbSKevin Liu host->quirks2 |= pdata->quirks2; 341a702c8abSZhangfei Gao if (pdata->host_caps) 342a702c8abSZhangfei Gao host->mmc->caps |= pdata->host_caps; 3438f63795cSChris Ball if (pdata->host_caps2) 3448f63795cSChris Ball host->mmc->caps2 |= pdata->host_caps2; 345a702c8abSZhangfei Gao if (pdata->pm_caps) 346a702c8abSZhangfei Gao host->mmc->pm_caps |= pdata->pm_caps; 3478f63795cSChris Ball 3488f63795cSChris Ball if (gpio_is_valid(pdata->ext_cd_gpio)) { 349214fc309SLaurent Pinchart ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio, 350214fc309SLaurent Pinchart 0); 3518f63795cSChris Ball if (ret) { 3528f63795cSChris Ball dev_err(mmc_dev(host->mmc), 3538f63795cSChris Ball "failed to allocate card detect gpio\n"); 3548f63795cSChris Ball goto err_cd_req; 3558f63795cSChris Ball } 3568f63795cSChris Ball } 357a702c8abSZhangfei Gao } 358a702c8abSZhangfei Gao 359bb691ae4SKevin Liu pm_runtime_enable(&pdev->dev); 3600dcaa249SDaniel Drake pm_runtime_get_sync(&pdev->dev); 361bb691ae4SKevin Liu pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS); 362bb691ae4SKevin Liu pm_runtime_use_autosuspend(&pdev->dev); 363bb691ae4SKevin Liu pm_suspend_ignore_children(&pdev->dev, 1); 364bb691ae4SKevin Liu 365a702c8abSZhangfei Gao ret = sdhci_add_host(host); 366a702c8abSZhangfei Gao if (ret) { 367a702c8abSZhangfei Gao dev_err(&pdev->dev, "failed to add host\n"); 368a702c8abSZhangfei Gao goto err_add_host; 369a702c8abSZhangfei Gao } 370a702c8abSZhangfei Gao 371a702c8abSZhangfei Gao platform_set_drvdata(pdev, host); 372a702c8abSZhangfei Gao 373943647f6SKevin Liu if (host->mmc->pm_caps & MMC_PM_KEEP_POWER) { 374740b7a44SKevin Liu device_init_wakeup(&pdev->dev, 1); 375740b7a44SKevin Liu host->mmc->pm_flags |= MMC_PM_WAKE_SDIO_IRQ; 376740b7a44SKevin Liu } else { 377740b7a44SKevin Liu device_init_wakeup(&pdev->dev, 0); 378740b7a44SKevin Liu } 379740b7a44SKevin Liu 380bb691ae4SKevin Liu pm_runtime_put_autosuspend(&pdev->dev); 381bb691ae4SKevin Liu 382a702c8abSZhangfei Gao return 0; 383a702c8abSZhangfei Gao 384a702c8abSZhangfei Gao err_add_host: 3850dcaa249SDaniel Drake pm_runtime_put_sync(&pdev->dev); 3860dcaa249SDaniel Drake pm_runtime_disable(&pdev->dev); 38787d2163dSXiang Wang err_of_parse: 38887d2163dSXiang Wang err_cd_req: 389164378efSChao Xie clk_disable_unprepare(clk); 390a702c8abSZhangfei Gao err_clk_get: 3915491ce3fSMarcin Wojtas err_mbus_win: 392a702c8abSZhangfei Gao sdhci_pltfm_free(pdev); 393a702c8abSZhangfei Gao return ret; 394a702c8abSZhangfei Gao } 395a702c8abSZhangfei Gao 3966e0ee714SBill Pemberton static int sdhci_pxav3_remove(struct platform_device *pdev) 397a702c8abSZhangfei Gao { 398a702c8abSZhangfei Gao struct sdhci_host *host = platform_get_drvdata(pdev); 399a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 400a702c8abSZhangfei Gao 401bb691ae4SKevin Liu pm_runtime_get_sync(&pdev->dev); 402a702c8abSZhangfei Gao sdhci_remove_host(host, 1); 403bb691ae4SKevin Liu pm_runtime_disable(&pdev->dev); 404a702c8abSZhangfei Gao 405164378efSChao Xie clk_disable_unprepare(pltfm_host->clk); 4068f63795cSChris Ball 407a702c8abSZhangfei Gao sdhci_pltfm_free(pdev); 408a702c8abSZhangfei Gao 409a702c8abSZhangfei Gao return 0; 410a702c8abSZhangfei Gao } 411a702c8abSZhangfei Gao 412bb691ae4SKevin Liu #ifdef CONFIG_PM_SLEEP 413bb691ae4SKevin Liu static int sdhci_pxav3_suspend(struct device *dev) 414bb691ae4SKevin Liu { 415bb691ae4SKevin Liu int ret; 416bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 417bb691ae4SKevin Liu 418bb691ae4SKevin Liu pm_runtime_get_sync(dev); 419bb691ae4SKevin Liu ret = sdhci_suspend_host(host); 420bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev); 421bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev); 422bb691ae4SKevin Liu 423bb691ae4SKevin Liu return ret; 424bb691ae4SKevin Liu } 425bb691ae4SKevin Liu 426bb691ae4SKevin Liu static int sdhci_pxav3_resume(struct device *dev) 427bb691ae4SKevin Liu { 428bb691ae4SKevin Liu int ret; 429bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 430bb691ae4SKevin Liu 431bb691ae4SKevin Liu pm_runtime_get_sync(dev); 432bb691ae4SKevin Liu ret = sdhci_resume_host(host); 433bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev); 434bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev); 435bb691ae4SKevin Liu 436bb691ae4SKevin Liu return ret; 437bb691ae4SKevin Liu } 438bb691ae4SKevin Liu #endif 439bb691ae4SKevin Liu 440bb691ae4SKevin Liu #ifdef CONFIG_PM_RUNTIME 441bb691ae4SKevin Liu static int sdhci_pxav3_runtime_suspend(struct device *dev) 442bb691ae4SKevin Liu { 443bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 444bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 445bb691ae4SKevin Liu unsigned long flags; 446bb691ae4SKevin Liu 447bb691ae4SKevin Liu if (pltfm_host->clk) { 448bb691ae4SKevin Liu spin_lock_irqsave(&host->lock, flags); 449bb691ae4SKevin Liu host->runtime_suspended = true; 450bb691ae4SKevin Liu spin_unlock_irqrestore(&host->lock, flags); 451bb691ae4SKevin Liu 452bb691ae4SKevin Liu clk_disable_unprepare(pltfm_host->clk); 453bb691ae4SKevin Liu } 454bb691ae4SKevin Liu 455bb691ae4SKevin Liu return 0; 456bb691ae4SKevin Liu } 457bb691ae4SKevin Liu 458bb691ae4SKevin Liu static int sdhci_pxav3_runtime_resume(struct device *dev) 459bb691ae4SKevin Liu { 460bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 461bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 462bb691ae4SKevin Liu unsigned long flags; 463bb691ae4SKevin Liu 464bb691ae4SKevin Liu if (pltfm_host->clk) { 465bb691ae4SKevin Liu clk_prepare_enable(pltfm_host->clk); 466bb691ae4SKevin Liu 467bb691ae4SKevin Liu spin_lock_irqsave(&host->lock, flags); 468bb691ae4SKevin Liu host->runtime_suspended = false; 469bb691ae4SKevin Liu spin_unlock_irqrestore(&host->lock, flags); 470bb691ae4SKevin Liu } 471bb691ae4SKevin Liu 472bb691ae4SKevin Liu return 0; 473bb691ae4SKevin Liu } 474bb691ae4SKevin Liu #endif 475bb691ae4SKevin Liu 476bb691ae4SKevin Liu #ifdef CONFIG_PM 477bb691ae4SKevin Liu static const struct dev_pm_ops sdhci_pxav3_pmops = { 478bb691ae4SKevin Liu SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume) 479bb691ae4SKevin Liu SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend, 480bb691ae4SKevin Liu sdhci_pxav3_runtime_resume, NULL) 481bb691ae4SKevin Liu }; 482bb691ae4SKevin Liu 483bb691ae4SKevin Liu #define SDHCI_PXAV3_PMOPS (&sdhci_pxav3_pmops) 484bb691ae4SKevin Liu 485bb691ae4SKevin Liu #else 486bb691ae4SKevin Liu #define SDHCI_PXAV3_PMOPS NULL 487bb691ae4SKevin Liu #endif 488bb691ae4SKevin Liu 489a702c8abSZhangfei Gao static struct platform_driver sdhci_pxav3_driver = { 490a702c8abSZhangfei Gao .driver = { 491a702c8abSZhangfei Gao .name = "sdhci-pxav3", 492b650352dSChris Ball #ifdef CONFIG_OF 493b650352dSChris Ball .of_match_table = sdhci_pxav3_of_match, 494b650352dSChris Ball #endif 495bb691ae4SKevin Liu .pm = SDHCI_PXAV3_PMOPS, 496a702c8abSZhangfei Gao }, 497a702c8abSZhangfei Gao .probe = sdhci_pxav3_probe, 4980433c143SBill Pemberton .remove = sdhci_pxav3_remove, 499a702c8abSZhangfei Gao }; 500a702c8abSZhangfei Gao 501d1f81a64SAxel Lin module_platform_driver(sdhci_pxav3_driver); 502a702c8abSZhangfei Gao 503a702c8abSZhangfei Gao MODULE_DESCRIPTION("SDHCI driver for pxav3"); 504a702c8abSZhangfei Gao MODULE_AUTHOR("Marvell International Ltd."); 505a702c8abSZhangfei Gao MODULE_LICENSE("GPL v2"); 506a702c8abSZhangfei Gao 507