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 61cc9571e8SSebastian Hesselbarth struct sdhci_pxa { 628afdc9ccSSebastian Hesselbarth struct clk *clk_core; 638c96a7a3SSebastian Hesselbarth struct clk *clk_io; 64cc9571e8SSebastian Hesselbarth u8 power_mode; 651140011eSMarcin Wojtas void __iomem *sdio3_conf_reg; 66cc9571e8SSebastian Hesselbarth }; 67cc9571e8SSebastian Hesselbarth 685491ce3fSMarcin Wojtas /* 695491ce3fSMarcin Wojtas * These registers are relative to the second register region, for the 705491ce3fSMarcin Wojtas * MBus bridge. 715491ce3fSMarcin Wojtas */ 725491ce3fSMarcin Wojtas #define SDHCI_WINDOW_CTRL(i) (0x80 + ((i) << 3)) 735491ce3fSMarcin Wojtas #define SDHCI_WINDOW_BASE(i) (0x84 + ((i) << 3)) 745491ce3fSMarcin Wojtas #define SDHCI_MAX_WIN_NUM 8 755491ce3fSMarcin Wojtas 761140011eSMarcin Wojtas /* 771140011eSMarcin Wojtas * Fields below belong to SDIO3 Configuration Register (third register 781140011eSMarcin Wojtas * region for the Armada 38x flavor) 791140011eSMarcin Wojtas */ 801140011eSMarcin Wojtas 811140011eSMarcin Wojtas #define SDIO3_CONF_CLK_INV BIT(0) 821140011eSMarcin Wojtas #define SDIO3_CONF_SD_FB_CLK BIT(2) 831140011eSMarcin Wojtas 845491ce3fSMarcin Wojtas static int mv_conf_mbus_windows(struct platform_device *pdev, 855491ce3fSMarcin Wojtas const struct mbus_dram_target_info *dram) 865491ce3fSMarcin Wojtas { 875491ce3fSMarcin Wojtas int i; 885491ce3fSMarcin Wojtas void __iomem *regs; 895491ce3fSMarcin Wojtas struct resource *res; 905491ce3fSMarcin Wojtas 915491ce3fSMarcin Wojtas if (!dram) { 925491ce3fSMarcin Wojtas dev_err(&pdev->dev, "no mbus dram info\n"); 935491ce3fSMarcin Wojtas return -EINVAL; 945491ce3fSMarcin Wojtas } 955491ce3fSMarcin Wojtas 965491ce3fSMarcin Wojtas res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 975491ce3fSMarcin Wojtas if (!res) { 985491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot get mbus registers\n"); 995491ce3fSMarcin Wojtas return -EINVAL; 1005491ce3fSMarcin Wojtas } 1015491ce3fSMarcin Wojtas 1025491ce3fSMarcin Wojtas regs = ioremap(res->start, resource_size(res)); 1035491ce3fSMarcin Wojtas if (!regs) { 1045491ce3fSMarcin Wojtas dev_err(&pdev->dev, "cannot map mbus registers\n"); 1055491ce3fSMarcin Wojtas return -ENOMEM; 1065491ce3fSMarcin Wojtas } 1075491ce3fSMarcin Wojtas 1085491ce3fSMarcin Wojtas for (i = 0; i < SDHCI_MAX_WIN_NUM; i++) { 1095491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_CTRL(i)); 1105491ce3fSMarcin Wojtas writel(0, regs + SDHCI_WINDOW_BASE(i)); 1115491ce3fSMarcin Wojtas } 1125491ce3fSMarcin Wojtas 1135491ce3fSMarcin Wojtas for (i = 0; i < dram->num_cs; i++) { 1145491ce3fSMarcin Wojtas const struct mbus_dram_window *cs = dram->cs + i; 1155491ce3fSMarcin Wojtas 1165491ce3fSMarcin Wojtas /* Write size, attributes and target id to control register */ 1175491ce3fSMarcin Wojtas writel(((cs->size - 1) & 0xffff0000) | 1185491ce3fSMarcin Wojtas (cs->mbus_attr << 8) | 1195491ce3fSMarcin Wojtas (dram->mbus_dram_target_id << 4) | 1, 1205491ce3fSMarcin Wojtas regs + SDHCI_WINDOW_CTRL(i)); 1215491ce3fSMarcin Wojtas /* Write base address to base register */ 1225491ce3fSMarcin Wojtas writel(cs->base, regs + SDHCI_WINDOW_BASE(i)); 1235491ce3fSMarcin Wojtas } 1245491ce3fSMarcin Wojtas 1255491ce3fSMarcin Wojtas iounmap(regs); 1265491ce3fSMarcin Wojtas 1275491ce3fSMarcin Wojtas return 0; 1285491ce3fSMarcin Wojtas } 1295491ce3fSMarcin Wojtas 130a39128bcSMarcin Wojtas static int armada_38x_quirks(struct platform_device *pdev, 131a39128bcSMarcin Wojtas struct sdhci_host *host) 132d4b803c5SGregory CLEMENT { 133a39128bcSMarcin Wojtas struct device_node *np = pdev->dev.of_node; 1341140011eSMarcin Wojtas struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 135f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 1361140011eSMarcin Wojtas struct resource *res; 137a39128bcSMarcin Wojtas 1385de76bfcSNadav Haklai host->quirks &= ~SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN; 139d4b803c5SGregory CLEMENT host->quirks |= SDHCI_QUIRK_MISSING_CAPS; 1400ca33b4aSRussell King 1410ca33b4aSRussell King host->caps = sdhci_readl(host, SDHCI_CAPABILITIES); 1420ca33b4aSRussell King host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1); 1430ca33b4aSRussell King 1441140011eSMarcin Wojtas res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 1451140011eSMarcin Wojtas "conf-sdio3"); 1461140011eSMarcin Wojtas if (res) { 1471140011eSMarcin Wojtas pxa->sdio3_conf_reg = devm_ioremap_resource(&pdev->dev, res); 1481140011eSMarcin Wojtas if (IS_ERR(pxa->sdio3_conf_reg)) 1491140011eSMarcin Wojtas return PTR_ERR(pxa->sdio3_conf_reg); 1501140011eSMarcin Wojtas } else { 151d4b803c5SGregory CLEMENT /* 152d4b803c5SGregory CLEMENT * According to erratum 'FE-2946959' both SDR50 and DDR50 153d4b803c5SGregory CLEMENT * modes require specific clock adjustments in SDIO3 154d4b803c5SGregory CLEMENT * Configuration register, if the adjustment is not done, 155d4b803c5SGregory CLEMENT * remove them from the capabilities. 156d4b803c5SGregory CLEMENT */ 157d4b803c5SGregory CLEMENT host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50); 158a39128bcSMarcin Wojtas 1591140011eSMarcin Wojtas dev_warn(&pdev->dev, "conf-sdio3 register not found: disabling SDR50 and DDR50 modes.\nConsider updating your dtb\n"); 1601140011eSMarcin Wojtas } 1611140011eSMarcin Wojtas 162a39128bcSMarcin Wojtas /* 163a39128bcSMarcin Wojtas * According to erratum 'ERR-7878951' Armada 38x SDHCI 164a39128bcSMarcin Wojtas * controller has different capabilities than the ones shown 165a39128bcSMarcin Wojtas * in its registers 166a39128bcSMarcin Wojtas */ 167a39128bcSMarcin Wojtas if (of_property_read_bool(np, "no-1-8-v")) { 168a39128bcSMarcin Wojtas host->caps &= ~SDHCI_CAN_VDD_180; 169a39128bcSMarcin Wojtas host->mmc->caps &= ~MMC_CAP_1_8V_DDR; 170a39128bcSMarcin Wojtas } else { 171a39128bcSMarcin Wojtas host->caps &= ~SDHCI_CAN_VDD_330; 172a39128bcSMarcin Wojtas } 173a39128bcSMarcin Wojtas host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_USE_SDR50_TUNING); 174a39128bcSMarcin Wojtas 175d4b803c5SGregory CLEMENT return 0; 176d4b803c5SGregory CLEMENT } 177d4b803c5SGregory CLEMENT 17803231f9bSRussell King static void pxav3_reset(struct sdhci_host *host, u8 mask) 179a702c8abSZhangfei Gao { 180a702c8abSZhangfei Gao struct platform_device *pdev = to_platform_device(mmc_dev(host->mmc)); 181a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; 182a702c8abSZhangfei Gao 18303231f9bSRussell King sdhci_reset(host, mask); 18403231f9bSRussell King 185a702c8abSZhangfei Gao if (mask == SDHCI_RESET_ALL) { 186a702c8abSZhangfei Gao /* 187a702c8abSZhangfei Gao * tune timing of read data/command when crc error happen 188a702c8abSZhangfei Gao * no performance impact 189a702c8abSZhangfei Gao */ 190a702c8abSZhangfei Gao if (pdata && 0 != pdata->clk_delay_cycles) { 191a702c8abSZhangfei Gao u16 tmp; 192a702c8abSZhangfei Gao 193a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); 194a702c8abSZhangfei Gao tmp |= (pdata->clk_delay_cycles & SDCLK_DELAY_MASK) 195a702c8abSZhangfei Gao << SDCLK_DELAY_SHIFT; 196a702c8abSZhangfei Gao tmp |= SDCLK_SEL; 197a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CLOCK_BURST_SIZE_SETUP); 198a702c8abSZhangfei Gao } 199a702c8abSZhangfei Gao } 200a702c8abSZhangfei Gao } 201a702c8abSZhangfei Gao 202a702c8abSZhangfei Gao #define MAX_WAIT_COUNT 5 203a702c8abSZhangfei Gao static void pxav3_gen_init_74_clocks(struct sdhci_host *host, u8 power_mode) 204a702c8abSZhangfei Gao { 205a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 206f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 207a702c8abSZhangfei Gao u16 tmp; 208a702c8abSZhangfei Gao int count; 209a702c8abSZhangfei Gao 210a702c8abSZhangfei Gao if (pxa->power_mode == MMC_POWER_UP 211a702c8abSZhangfei Gao && power_mode == MMC_POWER_ON) { 212a702c8abSZhangfei Gao 213a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc), 214a702c8abSZhangfei Gao "%s: slot->power_mode = %d," 215a702c8abSZhangfei Gao "ios->power_mode = %d\n", 216a702c8abSZhangfei Gao __func__, 217a702c8abSZhangfei Gao pxa->power_mode, 218a702c8abSZhangfei Gao power_mode); 219a702c8abSZhangfei Gao 220a702c8abSZhangfei Gao /* set we want notice of when 74 clocks are sent */ 221a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2); 222a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT_EN; 223a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2); 224a702c8abSZhangfei Gao 225a702c8abSZhangfei Gao /* start sending the 74 clocks */ 226a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CFG_FIFO_PARAM); 227a702c8abSZhangfei Gao tmp |= SDCFG_GEN_PAD_CLK_ON; 228a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CFG_FIFO_PARAM); 229a702c8abSZhangfei Gao 230a702c8abSZhangfei Gao /* slowest speed is about 100KHz or 10usec per clock */ 231a702c8abSZhangfei Gao udelay(740); 232a702c8abSZhangfei Gao count = 0; 233a702c8abSZhangfei Gao 234a702c8abSZhangfei Gao while (count++ < MAX_WAIT_COUNT) { 235a702c8abSZhangfei Gao if ((readw(host->ioaddr + SD_CE_ATA_2) 236a702c8abSZhangfei Gao & SDCE_MISC_INT) == 0) 237a702c8abSZhangfei Gao break; 238a702c8abSZhangfei Gao udelay(10); 239a702c8abSZhangfei Gao } 240a702c8abSZhangfei Gao 241a702c8abSZhangfei Gao if (count == MAX_WAIT_COUNT) 242a702c8abSZhangfei Gao dev_warn(mmc_dev(host->mmc), "74 clock interrupt not cleared\n"); 243a702c8abSZhangfei Gao 244a702c8abSZhangfei Gao /* clear the interrupt bit if posted */ 245a702c8abSZhangfei Gao tmp = readw(host->ioaddr + SD_CE_ATA_2); 246a702c8abSZhangfei Gao tmp |= SDCE_MISC_INT; 247a702c8abSZhangfei Gao writew(tmp, host->ioaddr + SD_CE_ATA_2); 248a702c8abSZhangfei Gao } 249a702c8abSZhangfei Gao pxa->power_mode = power_mode; 250a702c8abSZhangfei Gao } 251a702c8abSZhangfei Gao 25213e64501SRussell King static void pxav3_set_uhs_signaling(struct sdhci_host *host, unsigned int uhs) 253a702c8abSZhangfei Gao { 2541140011eSMarcin Wojtas struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 255f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 256a702c8abSZhangfei Gao u16 ctrl_2; 257a702c8abSZhangfei Gao 258a702c8abSZhangfei Gao /* 259a702c8abSZhangfei Gao * Set V18_EN -- UHS modes do not work without this. 260a702c8abSZhangfei Gao * does not change signaling voltage 261a702c8abSZhangfei Gao */ 262a702c8abSZhangfei Gao ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); 263a702c8abSZhangfei Gao 264a702c8abSZhangfei Gao /* Select Bus Speed Mode for host */ 265a702c8abSZhangfei Gao ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; 266a702c8abSZhangfei Gao switch (uhs) { 267a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR12: 268a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR12; 269a702c8abSZhangfei Gao break; 270a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR25: 271a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR25; 272a702c8abSZhangfei Gao break; 273a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR50: 274a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR50 | SDHCI_CTRL_VDD_180; 275a702c8abSZhangfei Gao break; 276a702c8abSZhangfei Gao case MMC_TIMING_UHS_SDR104: 277a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_SDR104 | SDHCI_CTRL_VDD_180; 278a702c8abSZhangfei Gao break; 279668e84b2SSebastian Hesselbarth case MMC_TIMING_MMC_DDR52: 280a702c8abSZhangfei Gao case MMC_TIMING_UHS_DDR50: 281a702c8abSZhangfei Gao ctrl_2 |= SDHCI_CTRL_UHS_DDR50 | SDHCI_CTRL_VDD_180; 282a702c8abSZhangfei Gao break; 283a702c8abSZhangfei Gao } 284a702c8abSZhangfei Gao 2851140011eSMarcin Wojtas /* 2861140011eSMarcin Wojtas * Update SDIO3 Configuration register according to erratum 2871140011eSMarcin Wojtas * FE-2946959 2881140011eSMarcin Wojtas */ 2891140011eSMarcin Wojtas if (pxa->sdio3_conf_reg) { 2901140011eSMarcin Wojtas u8 reg_val = readb(pxa->sdio3_conf_reg); 2911140011eSMarcin Wojtas 2921140011eSMarcin Wojtas if (uhs == MMC_TIMING_UHS_SDR50 || 2931140011eSMarcin Wojtas uhs == MMC_TIMING_UHS_DDR50) { 2941140011eSMarcin Wojtas reg_val &= ~SDIO3_CONF_CLK_INV; 2951140011eSMarcin Wojtas reg_val |= SDIO3_CONF_SD_FB_CLK; 296fa796414SNadav Haklai } else if (uhs == MMC_TIMING_MMC_HS) { 297fa796414SNadav Haklai reg_val &= ~SDIO3_CONF_CLK_INV; 298fa796414SNadav Haklai reg_val &= ~SDIO3_CONF_SD_FB_CLK; 2991140011eSMarcin Wojtas } else { 3001140011eSMarcin Wojtas reg_val |= SDIO3_CONF_CLK_INV; 3011140011eSMarcin Wojtas reg_val &= ~SDIO3_CONF_SD_FB_CLK; 3021140011eSMarcin Wojtas } 3031140011eSMarcin Wojtas writeb(reg_val, pxa->sdio3_conf_reg); 3041140011eSMarcin Wojtas } 3051140011eSMarcin Wojtas 306a702c8abSZhangfei Gao sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); 307a702c8abSZhangfei Gao dev_dbg(mmc_dev(host->mmc), 308a702c8abSZhangfei Gao "%s uhs = %d, ctrl_2 = %04X\n", 309a702c8abSZhangfei Gao __func__, uhs, ctrl_2); 310a702c8abSZhangfei Gao } 311a702c8abSZhangfei Gao 3121dceb041SAdrian Hunter static void pxav3_set_power(struct sdhci_host *host, unsigned char mode, 3131dceb041SAdrian Hunter unsigned short vdd) 3141dceb041SAdrian Hunter { 3151dceb041SAdrian Hunter struct mmc_host *mmc = host->mmc; 3161dceb041SAdrian Hunter u8 pwr = host->pwr; 3171dceb041SAdrian Hunter 318606d3131SAdrian Hunter sdhci_set_power_noreg(host, mode, vdd); 3191dceb041SAdrian Hunter 3201dceb041SAdrian Hunter if (host->pwr == pwr) 3211dceb041SAdrian Hunter return; 3221dceb041SAdrian Hunter 3231dceb041SAdrian Hunter if (host->pwr == 0) 3241dceb041SAdrian Hunter vdd = 0; 3251dceb041SAdrian Hunter 326*d1e4f74fSAdrian Hunter if (!IS_ERR(mmc->supply.vmmc)) 3271dceb041SAdrian Hunter mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); 3281dceb041SAdrian Hunter } 3291dceb041SAdrian Hunter 330c915568dSLars-Peter Clausen static const struct sdhci_ops pxav3_sdhci_ops = { 3311771059cSRussell King .set_clock = sdhci_set_clock, 3321dceb041SAdrian Hunter .set_power = pxav3_set_power, 333a702c8abSZhangfei Gao .platform_send_init_74_clocks = pxav3_gen_init_74_clocks, 334d005d943SLars-Peter Clausen .get_max_clock = sdhci_pltfm_clk_get_max_clock, 3352317f56cSRussell King .set_bus_width = sdhci_set_bus_width, 33603231f9bSRussell King .reset = pxav3_reset, 337b3153765SPeter Griffin .set_uhs_signaling = pxav3_set_uhs_signaling, 338a702c8abSZhangfei Gao }; 339a702c8abSZhangfei Gao 34073b7afb9SKevin Liu static struct sdhci_pltfm_data sdhci_pxav3_pdata = { 341e065162aSKevin Liu .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK 34273b7afb9SKevin Liu | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC 34373b7afb9SKevin Liu | SDHCI_QUIRK_32BIT_ADMA_SIZE 34473b7afb9SKevin Liu | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, 34573b7afb9SKevin Liu .ops = &pxav3_sdhci_ops, 34673b7afb9SKevin Liu }; 34773b7afb9SKevin Liu 348b650352dSChris Ball #ifdef CONFIG_OF 349b650352dSChris Ball static const struct of_device_id sdhci_pxav3_of_match[] = { 350b650352dSChris Ball { 351b650352dSChris Ball .compatible = "mrvl,pxav3-mmc", 352b650352dSChris Ball }, 3535491ce3fSMarcin Wojtas { 3545491ce3fSMarcin Wojtas .compatible = "marvell,armada-380-sdhci", 3555491ce3fSMarcin Wojtas }, 356b650352dSChris Ball {}, 357b650352dSChris Ball }; 358b650352dSChris Ball MODULE_DEVICE_TABLE(of, sdhci_pxav3_of_match); 359b650352dSChris Ball 360b650352dSChris Ball static struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev) 361b650352dSChris Ball { 362b650352dSChris Ball struct sdhci_pxa_platdata *pdata; 363b650352dSChris Ball struct device_node *np = dev->of_node; 364b650352dSChris Ball u32 clk_delay_cycles; 365b650352dSChris Ball 366b650352dSChris Ball pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 367b650352dSChris Ball if (!pdata) 368b650352dSChris Ball return NULL; 369b650352dSChris Ball 37014460dbaSJisheng Zhang if (!of_property_read_u32(np, "mrvl,clk-delay-cycles", 37114460dbaSJisheng Zhang &clk_delay_cycles)) 372b650352dSChris Ball pdata->clk_delay_cycles = clk_delay_cycles; 373b650352dSChris Ball 374b650352dSChris Ball return pdata; 375b650352dSChris Ball } 376b650352dSChris Ball #else 377b650352dSChris Ball static inline struct sdhci_pxa_platdata *pxav3_get_mmc_pdata(struct device *dev) 378b650352dSChris Ball { 379b650352dSChris Ball return NULL; 380b650352dSChris Ball } 381b650352dSChris Ball #endif 382b650352dSChris Ball 383c3be1efdSBill Pemberton static int sdhci_pxav3_probe(struct platform_device *pdev) 384a702c8abSZhangfei Gao { 385a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host; 386a702c8abSZhangfei Gao struct sdhci_pxa_platdata *pdata = pdev->dev.platform_data; 387a702c8abSZhangfei Gao struct device *dev = &pdev->dev; 3885491ce3fSMarcin Wojtas struct device_node *np = pdev->dev.of_node; 389a702c8abSZhangfei Gao struct sdhci_host *host = NULL; 390a702c8abSZhangfei Gao struct sdhci_pxa *pxa = NULL; 391b650352dSChris Ball const struct of_device_id *match; 392a702c8abSZhangfei Gao int ret; 393a702c8abSZhangfei Gao 394f599da40SJisheng Zhang host = sdhci_pltfm_init(pdev, &sdhci_pxav3_pdata, sizeof(*pxa)); 3953df5b281SLaurent Pinchart if (IS_ERR(host)) 396a702c8abSZhangfei Gao return PTR_ERR(host); 3975491ce3fSMarcin Wojtas 398a702c8abSZhangfei Gao pltfm_host = sdhci_priv(host); 399f599da40SJisheng Zhang pxa = sdhci_pltfm_priv(pltfm_host); 400a702c8abSZhangfei Gao 40101ae1070SSebastian Hesselbarth pxa->clk_io = devm_clk_get(dev, "io"); 40201ae1070SSebastian Hesselbarth if (IS_ERR(pxa->clk_io)) 4038c96a7a3SSebastian Hesselbarth pxa->clk_io = devm_clk_get(dev, NULL); 4048c96a7a3SSebastian Hesselbarth if (IS_ERR(pxa->clk_io)) { 405a702c8abSZhangfei Gao dev_err(dev, "failed to get io clock\n"); 4068c96a7a3SSebastian Hesselbarth ret = PTR_ERR(pxa->clk_io); 407a702c8abSZhangfei Gao goto err_clk_get; 408a702c8abSZhangfei Gao } 4098c96a7a3SSebastian Hesselbarth pltfm_host->clk = pxa->clk_io; 4108c96a7a3SSebastian Hesselbarth clk_prepare_enable(pxa->clk_io); 411a702c8abSZhangfei Gao 4128afdc9ccSSebastian Hesselbarth pxa->clk_core = devm_clk_get(dev, "core"); 4138afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core)) 4148afdc9ccSSebastian Hesselbarth clk_prepare_enable(pxa->clk_core); 4158afdc9ccSSebastian Hesselbarth 416a39128bcSMarcin Wojtas /* enable 1/8V DDR capable */ 417a39128bcSMarcin Wojtas host->mmc->caps |= MMC_CAP_1_8V_DDR; 418a39128bcSMarcin Wojtas 419aa8165f9SThomas Petazzoni if (of_device_is_compatible(np, "marvell,armada-380-sdhci")) { 420a39128bcSMarcin Wojtas ret = armada_38x_quirks(pdev, host); 421d4b803c5SGregory CLEMENT if (ret < 0) 4222162d9f4SMarcin Wojtas goto err_mbus_win; 423aa8165f9SThomas Petazzoni ret = mv_conf_mbus_windows(pdev, mv_mbus_dram_info()); 424aa8165f9SThomas Petazzoni if (ret < 0) 425aa8165f9SThomas Petazzoni goto err_mbus_win; 426aa8165f9SThomas Petazzoni } 427aa8165f9SThomas Petazzoni 428b650352dSChris Ball match = of_match_device(of_match_ptr(sdhci_pxav3_of_match), &pdev->dev); 429943647f6SKevin Liu if (match) { 430d2cf6071SSimon Baatz ret = mmc_of_parse(host->mmc); 431d2cf6071SSimon Baatz if (ret) 432d2cf6071SSimon Baatz goto err_of_parse; 433943647f6SKevin Liu sdhci_get_of_property(pdev); 434b650352dSChris Ball pdata = pxav3_get_mmc_pdata(dev); 4359cd76049SJingju Hou pdev->dev.platform_data = pdata; 436943647f6SKevin Liu } else if (pdata) { 437a702c8abSZhangfei Gao /* on-chip device */ 438c844a46fSKevin Liu if (pdata->flags & PXA_FLAG_CARD_PERMANENT) 439a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_NONREMOVABLE; 440a702c8abSZhangfei Gao 441a702c8abSZhangfei Gao /* If slot design supports 8 bit data, indicate this to MMC. */ 442a702c8abSZhangfei Gao if (pdata->flags & PXA_FLAG_SD_8_BIT_CAPABLE_SLOT) 443a702c8abSZhangfei Gao host->mmc->caps |= MMC_CAP_8_BIT_DATA; 444a702c8abSZhangfei Gao 445a702c8abSZhangfei Gao if (pdata->quirks) 446a702c8abSZhangfei Gao host->quirks |= pdata->quirks; 4477c52d7bbSKevin Liu if (pdata->quirks2) 4487c52d7bbSKevin Liu host->quirks2 |= pdata->quirks2; 449a702c8abSZhangfei Gao if (pdata->host_caps) 450a702c8abSZhangfei Gao host->mmc->caps |= pdata->host_caps; 4518f63795cSChris Ball if (pdata->host_caps2) 4528f63795cSChris Ball host->mmc->caps2 |= pdata->host_caps2; 453a702c8abSZhangfei Gao if (pdata->pm_caps) 454a702c8abSZhangfei Gao host->mmc->pm_caps |= pdata->pm_caps; 4558f63795cSChris Ball 4568f63795cSChris Ball if (gpio_is_valid(pdata->ext_cd_gpio)) { 457214fc309SLaurent Pinchart ret = mmc_gpio_request_cd(host->mmc, pdata->ext_cd_gpio, 458214fc309SLaurent Pinchart 0); 4598f63795cSChris Ball if (ret) { 4608f63795cSChris Ball dev_err(mmc_dev(host->mmc), 4618f63795cSChris Ball "failed to allocate card detect gpio\n"); 4628f63795cSChris Ball goto err_cd_req; 4638f63795cSChris Ball } 4648f63795cSChris Ball } 465a702c8abSZhangfei Gao } 466a702c8abSZhangfei Gao 46762cf983aSJisheng Zhang pm_runtime_get_noresume(&pdev->dev); 46862cf983aSJisheng Zhang pm_runtime_set_active(&pdev->dev); 469bb691ae4SKevin Liu pm_runtime_set_autosuspend_delay(&pdev->dev, PXAV3_RPM_DELAY_MS); 470bb691ae4SKevin Liu pm_runtime_use_autosuspend(&pdev->dev); 47162cf983aSJisheng Zhang pm_runtime_enable(&pdev->dev); 472bb691ae4SKevin Liu pm_suspend_ignore_children(&pdev->dev, 1); 473bb691ae4SKevin Liu 474a702c8abSZhangfei Gao ret = sdhci_add_host(host); 475a702c8abSZhangfei Gao if (ret) { 476a702c8abSZhangfei Gao dev_err(&pdev->dev, "failed to add host\n"); 477a702c8abSZhangfei Gao goto err_add_host; 478a702c8abSZhangfei Gao } 479a702c8abSZhangfei Gao 48083dc9fecSJisheng Zhang if (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ) 481740b7a44SKevin Liu device_init_wakeup(&pdev->dev, 1); 482740b7a44SKevin Liu 483bb691ae4SKevin Liu pm_runtime_put_autosuspend(&pdev->dev); 484bb691ae4SKevin Liu 485a702c8abSZhangfei Gao return 0; 486a702c8abSZhangfei Gao 487a702c8abSZhangfei Gao err_add_host: 4880dcaa249SDaniel Drake pm_runtime_disable(&pdev->dev); 48962cf983aSJisheng Zhang pm_runtime_put_noidle(&pdev->dev); 49087d2163dSXiang Wang err_of_parse: 49187d2163dSXiang Wang err_cd_req: 492aa8165f9SThomas Petazzoni err_mbus_win: 4938c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io); 4948afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core); 495a702c8abSZhangfei Gao err_clk_get: 496a702c8abSZhangfei Gao sdhci_pltfm_free(pdev); 497a702c8abSZhangfei Gao return ret; 498a702c8abSZhangfei Gao } 499a702c8abSZhangfei Gao 5006e0ee714SBill Pemberton static int sdhci_pxav3_remove(struct platform_device *pdev) 501a702c8abSZhangfei Gao { 502a702c8abSZhangfei Gao struct sdhci_host *host = platform_get_drvdata(pdev); 503a702c8abSZhangfei Gao struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 504f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 505a702c8abSZhangfei Gao 506bb691ae4SKevin Liu pm_runtime_get_sync(&pdev->dev); 507bb691ae4SKevin Liu pm_runtime_disable(&pdev->dev); 50820f1f2d7SJisheng Zhang pm_runtime_put_noidle(&pdev->dev); 50920f1f2d7SJisheng Zhang 51020f1f2d7SJisheng Zhang sdhci_remove_host(host, 1); 511a702c8abSZhangfei Gao 5128c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io); 5138afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core); 5148f63795cSChris Ball 515a702c8abSZhangfei Gao sdhci_pltfm_free(pdev); 516a702c8abSZhangfei Gao 517a702c8abSZhangfei Gao return 0; 518a702c8abSZhangfei Gao } 519a702c8abSZhangfei Gao 520bb691ae4SKevin Liu #ifdef CONFIG_PM_SLEEP 521bb691ae4SKevin Liu static int sdhci_pxav3_suspend(struct device *dev) 522bb691ae4SKevin Liu { 523bb691ae4SKevin Liu int ret; 524bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 525bb691ae4SKevin Liu 526bb691ae4SKevin Liu pm_runtime_get_sync(dev); 527d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 528d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 529bb691ae4SKevin Liu ret = sdhci_suspend_host(host); 530bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev); 531bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev); 532bb691ae4SKevin Liu 533bb691ae4SKevin Liu return ret; 534bb691ae4SKevin Liu } 535bb691ae4SKevin Liu 536bb691ae4SKevin Liu static int sdhci_pxav3_resume(struct device *dev) 537bb691ae4SKevin Liu { 538bb691ae4SKevin Liu int ret; 539bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 540bb691ae4SKevin Liu 541bb691ae4SKevin Liu pm_runtime_get_sync(dev); 542bb691ae4SKevin Liu ret = sdhci_resume_host(host); 543bb691ae4SKevin Liu pm_runtime_mark_last_busy(dev); 544bb691ae4SKevin Liu pm_runtime_put_autosuspend(dev); 545bb691ae4SKevin Liu 546bb691ae4SKevin Liu return ret; 547bb691ae4SKevin Liu } 548bb691ae4SKevin Liu #endif 549bb691ae4SKevin Liu 550162d6f98SRafael J. Wysocki #ifdef CONFIG_PM 551bb691ae4SKevin Liu static int sdhci_pxav3_runtime_suspend(struct device *dev) 552bb691ae4SKevin Liu { 553bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 554bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 555f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 5563bb10f60SJisheng Zhang int ret; 557bb691ae4SKevin Liu 5583bb10f60SJisheng Zhang ret = sdhci_runtime_suspend_host(host); 5593bb10f60SJisheng Zhang if (ret) 5603bb10f60SJisheng Zhang return ret; 561bb691ae4SKevin Liu 562d38dcad4SAdrian Hunter if (host->tuning_mode != SDHCI_TUNING_MODE_3) 563d38dcad4SAdrian Hunter mmc_retune_needed(host->mmc); 564d38dcad4SAdrian Hunter 5658c96a7a3SSebastian Hesselbarth clk_disable_unprepare(pxa->clk_io); 5668afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core)) 5678afdc9ccSSebastian Hesselbarth clk_disable_unprepare(pxa->clk_core); 568bb691ae4SKevin Liu 569bb691ae4SKevin Liu return 0; 570bb691ae4SKevin Liu } 571bb691ae4SKevin Liu 572bb691ae4SKevin Liu static int sdhci_pxav3_runtime_resume(struct device *dev) 573bb691ae4SKevin Liu { 574bb691ae4SKevin Liu struct sdhci_host *host = dev_get_drvdata(dev); 575bb691ae4SKevin Liu struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 576f599da40SJisheng Zhang struct sdhci_pxa *pxa = sdhci_pltfm_priv(pltfm_host); 577bb691ae4SKevin Liu 5788c96a7a3SSebastian Hesselbarth clk_prepare_enable(pxa->clk_io); 5798afdc9ccSSebastian Hesselbarth if (!IS_ERR(pxa->clk_core)) 5808afdc9ccSSebastian Hesselbarth clk_prepare_enable(pxa->clk_core); 581bb691ae4SKevin Liu 5823bb10f60SJisheng Zhang return sdhci_runtime_resume_host(host); 583bb691ae4SKevin Liu } 584bb691ae4SKevin Liu #endif 585bb691ae4SKevin Liu 586bb691ae4SKevin Liu static const struct dev_pm_ops sdhci_pxav3_pmops = { 587bb691ae4SKevin Liu SET_SYSTEM_SLEEP_PM_OPS(sdhci_pxav3_suspend, sdhci_pxav3_resume) 588bb691ae4SKevin Liu SET_RUNTIME_PM_OPS(sdhci_pxav3_runtime_suspend, 589bb691ae4SKevin Liu sdhci_pxav3_runtime_resume, NULL) 590bb691ae4SKevin Liu }; 591bb691ae4SKevin Liu 592a702c8abSZhangfei Gao static struct platform_driver sdhci_pxav3_driver = { 593a702c8abSZhangfei Gao .driver = { 594a702c8abSZhangfei Gao .name = "sdhci-pxav3", 59559d22309SAxel Lin .of_match_table = of_match_ptr(sdhci_pxav3_of_match), 596a81ce772SUlf Hansson .pm = &sdhci_pxav3_pmops, 597a702c8abSZhangfei Gao }, 598a702c8abSZhangfei Gao .probe = sdhci_pxav3_probe, 5990433c143SBill Pemberton .remove = sdhci_pxav3_remove, 600a702c8abSZhangfei Gao }; 601a702c8abSZhangfei Gao 602d1f81a64SAxel Lin module_platform_driver(sdhci_pxav3_driver); 603a702c8abSZhangfei Gao 604a702c8abSZhangfei Gao MODULE_DESCRIPTION("SDHCI driver for pxav3"); 605a702c8abSZhangfei Gao MODULE_AUTHOR("Marvell International Ltd."); 606a702c8abSZhangfei Gao MODULE_LICENSE("GPL v2"); 607a702c8abSZhangfei Gao 608