1c3665006SThomas Abraham /* 2c3665006SThomas Abraham * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver 3c3665006SThomas Abraham * 4c3665006SThomas Abraham * Copyright (C) 2012, Samsung Electronics Co., Ltd. 5c3665006SThomas Abraham * 6c3665006SThomas Abraham * This program is free software; you can redistribute it and/or modify 7c3665006SThomas Abraham * it under the terms of the GNU General Public License as published by 8c3665006SThomas Abraham * the Free Software Foundation; either version 2 of the License, or 9c3665006SThomas Abraham * (at your option) any later version. 10c3665006SThomas Abraham */ 11c3665006SThomas Abraham 12c3665006SThomas Abraham #include <linux/module.h> 13c3665006SThomas Abraham #include <linux/platform_device.h> 14c3665006SThomas Abraham #include <linux/clk.h> 15c3665006SThomas Abraham #include <linux/mmc/host.h> 16c3665006SThomas Abraham #include <linux/mmc/dw_mmc.h> 17c3665006SThomas Abraham #include <linux/of.h> 18c3665006SThomas Abraham #include <linux/of_gpio.h> 19c3665006SThomas Abraham 20c3665006SThomas Abraham #include "dw_mmc.h" 21c3665006SThomas Abraham #include "dw_mmc-pltfm.h" 22c3665006SThomas Abraham 23c3665006SThomas Abraham #define NUM_PINS(x) (x + 2) 24c3665006SThomas Abraham 25c3665006SThomas Abraham #define SDMMC_CLKSEL 0x09C 26c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_SAMPLE(x) (((x) & 7) << 0) 27c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DRIVE(x) (((x) & 7) << 16) 28c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DIVIDER(x) (((x) & 7) << 24) 29c3665006SThomas Abraham #define SDMMC_CLKSEL_GET_DRV_WD3(x) (((x) >> 16) & 0x7) 30c3665006SThomas Abraham #define SDMMC_CLKSEL_TIMING(x, y, z) (SDMMC_CLKSEL_CCLK_SAMPLE(x) | \ 31c3665006SThomas Abraham SDMMC_CLKSEL_CCLK_DRIVE(y) | \ 32c3665006SThomas Abraham SDMMC_CLKSEL_CCLK_DIVIDER(z)) 33e2c63599SDoug Anderson #define SDMMC_CLKSEL_WAKEUP_INT BIT(11) 34c3665006SThomas Abraham 35c3665006SThomas Abraham #define EXYNOS4210_FIXED_CIU_CLK_DIV 2 36c3665006SThomas Abraham #define EXYNOS4412_FIXED_CIU_CLK_DIV 4 37c3665006SThomas Abraham 38*6bce431cSYuvaraj Kumar C D /* Block number in eMMC */ 39*6bce431cSYuvaraj Kumar C D #define DWMCI_BLOCK_NUM 0xFFFFFFFF 40*6bce431cSYuvaraj Kumar C D 41*6bce431cSYuvaraj Kumar C D #define SDMMC_EMMCP_BASE 0x1000 42*6bce431cSYuvaraj Kumar C D #define SDMMC_MPSECURITY (SDMMC_EMMCP_BASE + 0x0010) 43*6bce431cSYuvaraj Kumar C D #define SDMMC_MPSBEGIN0 (SDMMC_EMMCP_BASE + 0x0200) 44*6bce431cSYuvaraj Kumar C D #define SDMMC_MPSEND0 (SDMMC_EMMCP_BASE + 0x0204) 45*6bce431cSYuvaraj Kumar C D #define SDMMC_MPSCTRL0 (SDMMC_EMMCP_BASE + 0x020C) 46*6bce431cSYuvaraj Kumar C D 47*6bce431cSYuvaraj Kumar C D /* SMU control bits */ 48*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_SECURE_READ_BIT BIT(7) 49*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_SECURE_WRITE_BIT BIT(6) 50*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_NON_SECURE_READ_BIT BIT(5) 51*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT BIT(4) 52*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_USE_FUSE_KEY BIT(3) 53*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_ECB_MODE BIT(2) 54*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_ENCRYPTION BIT(1) 55*6bce431cSYuvaraj Kumar C D #define DWMCI_MPSCTRL_VALID BIT(0) 56*6bce431cSYuvaraj Kumar C D 57c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */ 58c3665006SThomas Abraham enum dw_mci_exynos_type { 59c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS4210, 60c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS4412, 61c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS5250, 6200fd041bSYuvaraj Kumar C D DW_MCI_TYPE_EXYNOS5420, 63*6bce431cSYuvaraj Kumar C D DW_MCI_TYPE_EXYNOS5420_SMU, 64c3665006SThomas Abraham }; 65c3665006SThomas Abraham 66c3665006SThomas Abraham /* Exynos implementation specific driver private data */ 67c3665006SThomas Abraham struct dw_mci_exynos_priv_data { 68c3665006SThomas Abraham enum dw_mci_exynos_type ctrl_type; 69c3665006SThomas Abraham u8 ciu_div; 70c3665006SThomas Abraham u32 sdr_timing; 71c3665006SThomas Abraham u32 ddr_timing; 72c3665006SThomas Abraham }; 73c3665006SThomas Abraham 74c3665006SThomas Abraham static struct dw_mci_exynos_compatible { 75c3665006SThomas Abraham char *compatible; 76c3665006SThomas Abraham enum dw_mci_exynos_type ctrl_type; 77c3665006SThomas Abraham } exynos_compat[] = { 78c3665006SThomas Abraham { 79c3665006SThomas Abraham .compatible = "samsung,exynos4210-dw-mshc", 80c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS4210, 81c3665006SThomas Abraham }, { 82c3665006SThomas Abraham .compatible = "samsung,exynos4412-dw-mshc", 83c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS4412, 84c3665006SThomas Abraham }, { 85c3665006SThomas Abraham .compatible = "samsung,exynos5250-dw-mshc", 86c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS5250, 8700fd041bSYuvaraj Kumar C D }, { 8800fd041bSYuvaraj Kumar C D .compatible = "samsung,exynos5420-dw-mshc", 8900fd041bSYuvaraj Kumar C D .ctrl_type = DW_MCI_TYPE_EXYNOS5420, 90*6bce431cSYuvaraj Kumar C D }, { 91*6bce431cSYuvaraj Kumar C D .compatible = "samsung,exynos5420-dw-mshc-smu", 92*6bce431cSYuvaraj Kumar C D .ctrl_type = DW_MCI_TYPE_EXYNOS5420_SMU, 93c3665006SThomas Abraham }, 94c3665006SThomas Abraham }; 95c3665006SThomas Abraham 96c3665006SThomas Abraham static int dw_mci_exynos_priv_init(struct dw_mci *host) 97c3665006SThomas Abraham { 98e6c784edSYuvaraj Kumar C D struct dw_mci_exynos_priv_data *priv = host->priv; 99c3665006SThomas Abraham 100*6bce431cSYuvaraj Kumar C D if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU) { 101*6bce431cSYuvaraj Kumar C D mci_writel(host, MPSBEGIN0, 0); 102*6bce431cSYuvaraj Kumar C D mci_writel(host, MPSEND0, DWMCI_BLOCK_NUM); 103*6bce431cSYuvaraj Kumar C D mci_writel(host, MPSCTRL0, DWMCI_MPSCTRL_SECURE_WRITE_BIT | 104*6bce431cSYuvaraj Kumar C D DWMCI_MPSCTRL_NON_SECURE_READ_BIT | 105*6bce431cSYuvaraj Kumar C D DWMCI_MPSCTRL_VALID | 106*6bce431cSYuvaraj Kumar C D DWMCI_MPSCTRL_NON_SECURE_WRITE_BIT); 107*6bce431cSYuvaraj Kumar C D } 108*6bce431cSYuvaraj Kumar C D 109c3665006SThomas Abraham return 0; 110c3665006SThomas Abraham } 111c3665006SThomas Abraham 112c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host) 113c3665006SThomas Abraham { 114c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv = host->priv; 115c3665006SThomas Abraham 11600fd041bSYuvaraj Kumar C D if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5250 || 117*6bce431cSYuvaraj Kumar C D priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420 || 118*6bce431cSYuvaraj Kumar C D priv->ctrl_type == DW_MCI_TYPE_EXYNOS5420_SMU) 119c3665006SThomas Abraham host->bus_hz /= (priv->ciu_div + 1); 120c3665006SThomas Abraham else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412) 121c3665006SThomas Abraham host->bus_hz /= EXYNOS4412_FIXED_CIU_CLK_DIV; 122c3665006SThomas Abraham else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210) 123c3665006SThomas Abraham host->bus_hz /= EXYNOS4210_FIXED_CIU_CLK_DIV; 124c3665006SThomas Abraham 125c3665006SThomas Abraham return 0; 126c3665006SThomas Abraham } 127c3665006SThomas Abraham 128e2c63599SDoug Anderson #ifdef CONFIG_PM_SLEEP 129e2c63599SDoug Anderson static int dw_mci_exynos_suspend(struct device *dev) 130e2c63599SDoug Anderson { 131e2c63599SDoug Anderson struct dw_mci *host = dev_get_drvdata(dev); 132e2c63599SDoug Anderson 133e2c63599SDoug Anderson return dw_mci_suspend(host); 134e2c63599SDoug Anderson } 135e2c63599SDoug Anderson 136e2c63599SDoug Anderson static int dw_mci_exynos_resume(struct device *dev) 137e2c63599SDoug Anderson { 138e2c63599SDoug Anderson struct dw_mci *host = dev_get_drvdata(dev); 139e2c63599SDoug Anderson 140*6bce431cSYuvaraj Kumar C D dw_mci_exynos_priv_init(host); 141e2c63599SDoug Anderson return dw_mci_resume(host); 142e2c63599SDoug Anderson } 143e2c63599SDoug Anderson 144e2c63599SDoug Anderson /** 145e2c63599SDoug Anderson * dw_mci_exynos_resume_noirq - Exynos-specific resume code 146e2c63599SDoug Anderson * 147e2c63599SDoug Anderson * On exynos5420 there is a silicon errata that will sometimes leave the 148e2c63599SDoug Anderson * WAKEUP_INT bit in the CLKSEL register asserted. This bit is 1 to indicate 149e2c63599SDoug Anderson * that it fired and we can clear it by writing a 1 back. Clear it to prevent 150e2c63599SDoug Anderson * interrupts from going off constantly. 151e2c63599SDoug Anderson * 152e2c63599SDoug Anderson * We run this code on all exynos variants because it doesn't hurt. 153e2c63599SDoug Anderson */ 154e2c63599SDoug Anderson 155e2c63599SDoug Anderson static int dw_mci_exynos_resume_noirq(struct device *dev) 156e2c63599SDoug Anderson { 157e2c63599SDoug Anderson struct dw_mci *host = dev_get_drvdata(dev); 158e2c63599SDoug Anderson u32 clksel; 159e2c63599SDoug Anderson 160e2c63599SDoug Anderson clksel = mci_readl(host, CLKSEL); 161e2c63599SDoug Anderson if (clksel & SDMMC_CLKSEL_WAKEUP_INT) 162e2c63599SDoug Anderson mci_writel(host, CLKSEL, clksel); 163e2c63599SDoug Anderson 164e2c63599SDoug Anderson return 0; 165e2c63599SDoug Anderson } 166e2c63599SDoug Anderson #else 167e2c63599SDoug Anderson #define dw_mci_exynos_suspend NULL 168e2c63599SDoug Anderson #define dw_mci_exynos_resume NULL 169e2c63599SDoug Anderson #define dw_mci_exynos_resume_noirq NULL 170e2c63599SDoug Anderson #endif /* CONFIG_PM_SLEEP */ 171e2c63599SDoug Anderson 172c3665006SThomas Abraham static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr) 173c3665006SThomas Abraham { 174c3665006SThomas Abraham /* 175c3665006SThomas Abraham * Exynos4412 and Exynos5250 extends the use of CMD register with the 176c3665006SThomas Abraham * use of bit 29 (which is reserved on standard MSHC controllers) for 177c3665006SThomas Abraham * optionally bypassing the HOLD register for command and data. The 178c3665006SThomas Abraham * HOLD register should be bypassed in case there is no phase shift 179c3665006SThomas Abraham * applied on CMD/DATA that is sent to the card. 180c3665006SThomas Abraham */ 181c3665006SThomas Abraham if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL))) 182c3665006SThomas Abraham *cmdr |= SDMMC_CMD_USE_HOLD_REG; 183c3665006SThomas Abraham } 184c3665006SThomas Abraham 185c3665006SThomas Abraham static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios) 186c3665006SThomas Abraham { 187c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv = host->priv; 188c3665006SThomas Abraham 189c3665006SThomas Abraham if (ios->timing == MMC_TIMING_UHS_DDR50) 190c3665006SThomas Abraham mci_writel(host, CLKSEL, priv->ddr_timing); 191c3665006SThomas Abraham else 192c3665006SThomas Abraham mci_writel(host, CLKSEL, priv->sdr_timing); 193c3665006SThomas Abraham } 194c3665006SThomas Abraham 195c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host) 196c3665006SThomas Abraham { 197e6c784edSYuvaraj Kumar C D struct dw_mci_exynos_priv_data *priv; 198c3665006SThomas Abraham struct device_node *np = host->dev->of_node; 199c3665006SThomas Abraham u32 timing[2]; 200c3665006SThomas Abraham u32 div = 0; 201e6c784edSYuvaraj Kumar C D int idx; 202c3665006SThomas Abraham int ret; 203c3665006SThomas Abraham 204e6c784edSYuvaraj Kumar C D priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL); 205e6c784edSYuvaraj Kumar C D if (!priv) { 206e6c784edSYuvaraj Kumar C D dev_err(host->dev, "mem alloc failed for private data\n"); 207e6c784edSYuvaraj Kumar C D return -ENOMEM; 208e6c784edSYuvaraj Kumar C D } 209e6c784edSYuvaraj Kumar C D 210e6c784edSYuvaraj Kumar C D for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) { 211e6c784edSYuvaraj Kumar C D if (of_device_is_compatible(np, exynos_compat[idx].compatible)) 212e6c784edSYuvaraj Kumar C D priv->ctrl_type = exynos_compat[idx].ctrl_type; 213e6c784edSYuvaraj Kumar C D } 214e6c784edSYuvaraj Kumar C D 215c3665006SThomas Abraham of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div); 216c3665006SThomas Abraham priv->ciu_div = div; 217c3665006SThomas Abraham 218c3665006SThomas Abraham ret = of_property_read_u32_array(np, 219c3665006SThomas Abraham "samsung,dw-mshc-sdr-timing", timing, 2); 220c3665006SThomas Abraham if (ret) 221c3665006SThomas Abraham return ret; 222c3665006SThomas Abraham 223c3665006SThomas Abraham ret = of_property_read_u32_array(np, 224c3665006SThomas Abraham "samsung,dw-mshc-ddr-timing", timing, 2); 225c3665006SThomas Abraham if (ret) 226c3665006SThomas Abraham return ret; 227c3665006SThomas Abraham 228e6c784edSYuvaraj Kumar C D priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div); 229c3665006SThomas Abraham priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div); 230e6c784edSYuvaraj Kumar C D host->priv = priv; 231c3665006SThomas Abraham return 0; 232c3665006SThomas Abraham } 233c3665006SThomas Abraham 2340f6e73d0SDongjin Kim /* Common capabilities of Exynos4/Exynos5 SoC */ 2350f6e73d0SDongjin Kim static unsigned long exynos_dwmmc_caps[4] = { 236c3665006SThomas Abraham MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR | 237c3665006SThomas Abraham MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23, 238c3665006SThomas Abraham MMC_CAP_CMD23, 239c3665006SThomas Abraham MMC_CAP_CMD23, 240c3665006SThomas Abraham MMC_CAP_CMD23, 241c3665006SThomas Abraham }; 242c3665006SThomas Abraham 2430f6e73d0SDongjin Kim static const struct dw_mci_drv_data exynos_drv_data = { 2440f6e73d0SDongjin Kim .caps = exynos_dwmmc_caps, 245c3665006SThomas Abraham .init = dw_mci_exynos_priv_init, 246c3665006SThomas Abraham .setup_clock = dw_mci_exynos_setup_clock, 247c3665006SThomas Abraham .prepare_command = dw_mci_exynos_prepare_command, 248c3665006SThomas Abraham .set_ios = dw_mci_exynos_set_ios, 249c3665006SThomas Abraham .parse_dt = dw_mci_exynos_parse_dt, 250c3665006SThomas Abraham }; 251c3665006SThomas Abraham 252c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = { 2530f6e73d0SDongjin Kim { .compatible = "samsung,exynos4412-dw-mshc", 2540f6e73d0SDongjin Kim .data = &exynos_drv_data, }, 255c3665006SThomas Abraham { .compatible = "samsung,exynos5250-dw-mshc", 2560f6e73d0SDongjin Kim .data = &exynos_drv_data, }, 25700fd041bSYuvaraj Kumar C D { .compatible = "samsung,exynos5420-dw-mshc", 25800fd041bSYuvaraj Kumar C D .data = &exynos_drv_data, }, 259*6bce431cSYuvaraj Kumar C D { .compatible = "samsung,exynos5420-dw-mshc-smu", 260*6bce431cSYuvaraj Kumar C D .data = &exynos_drv_data, }, 261c3665006SThomas Abraham {}, 262c3665006SThomas Abraham }; 263517cb9f1SArnd Bergmann MODULE_DEVICE_TABLE(of, dw_mci_exynos_match); 264c3665006SThomas Abraham 2659665f7f2SSachin Kamat static int dw_mci_exynos_probe(struct platform_device *pdev) 266c3665006SThomas Abraham { 2678e2b36eaSArnd Bergmann const struct dw_mci_drv_data *drv_data; 268c3665006SThomas Abraham const struct of_device_id *match; 269c3665006SThomas Abraham 270c3665006SThomas Abraham match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node); 271c3665006SThomas Abraham drv_data = match->data; 272c3665006SThomas Abraham return dw_mci_pltfm_register(pdev, drv_data); 273c3665006SThomas Abraham } 274c3665006SThomas Abraham 275e2c63599SDoug Anderson const struct dev_pm_ops dw_mci_exynos_pmops = { 276e2c63599SDoug Anderson SET_SYSTEM_SLEEP_PM_OPS(dw_mci_exynos_suspend, dw_mci_exynos_resume) 277e2c63599SDoug Anderson .resume_noirq = dw_mci_exynos_resume_noirq, 278e2c63599SDoug Anderson .thaw_noirq = dw_mci_exynos_resume_noirq, 279e2c63599SDoug Anderson .restore_noirq = dw_mci_exynos_resume_noirq, 280e2c63599SDoug Anderson }; 281e2c63599SDoug Anderson 282c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = { 283c3665006SThomas Abraham .probe = dw_mci_exynos_probe, 284c3665006SThomas Abraham .remove = __exit_p(dw_mci_pltfm_remove), 285c3665006SThomas Abraham .driver = { 286c3665006SThomas Abraham .name = "dwmmc_exynos", 28720183d50SSachin Kamat .of_match_table = dw_mci_exynos_match, 288e2c63599SDoug Anderson .pm = &dw_mci_exynos_pmops, 289c3665006SThomas Abraham }, 290c3665006SThomas Abraham }; 291c3665006SThomas Abraham 292c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver); 293c3665006SThomas Abraham 294c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension"); 295c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com"); 296c3665006SThomas Abraham MODULE_LICENSE("GPL v2"); 297c3665006SThomas Abraham MODULE_ALIAS("platform:dwmmc-exynos"); 298