1*c3665006SThomas Abraham /* 2*c3665006SThomas Abraham * Exynos Specific Extensions for Synopsys DW Multimedia Card Interface driver 3*c3665006SThomas Abraham * 4*c3665006SThomas Abraham * Copyright (C) 2012, Samsung Electronics Co., Ltd. 5*c3665006SThomas Abraham * 6*c3665006SThomas Abraham * This program is free software; you can redistribute it and/or modify 7*c3665006SThomas Abraham * it under the terms of the GNU General Public License as published by 8*c3665006SThomas Abraham * the Free Software Foundation; either version 2 of the License, or 9*c3665006SThomas Abraham * (at your option) any later version. 10*c3665006SThomas Abraham */ 11*c3665006SThomas Abraham 12*c3665006SThomas Abraham #include <linux/module.h> 13*c3665006SThomas Abraham #include <linux/platform_device.h> 14*c3665006SThomas Abraham #include <linux/clk.h> 15*c3665006SThomas Abraham #include <linux/mmc/host.h> 16*c3665006SThomas Abraham #include <linux/mmc/dw_mmc.h> 17*c3665006SThomas Abraham #include <linux/of.h> 18*c3665006SThomas Abraham #include <linux/of_gpio.h> 19*c3665006SThomas Abraham 20*c3665006SThomas Abraham #include "dw_mmc.h" 21*c3665006SThomas Abraham #include "dw_mmc-pltfm.h" 22*c3665006SThomas Abraham 23*c3665006SThomas Abraham #define NUM_PINS(x) (x + 2) 24*c3665006SThomas Abraham 25*c3665006SThomas Abraham #define SDMMC_CLKSEL 0x09C 26*c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_SAMPLE(x) (((x) & 7) << 0) 27*c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DRIVE(x) (((x) & 7) << 16) 28*c3665006SThomas Abraham #define SDMMC_CLKSEL_CCLK_DIVIDER(x) (((x) & 7) << 24) 29*c3665006SThomas Abraham #define SDMMC_CLKSEL_GET_DRV_WD3(x) (((x) >> 16) & 0x7) 30*c3665006SThomas Abraham #define SDMMC_CLKSEL_TIMING(x, y, z) (SDMMC_CLKSEL_CCLK_SAMPLE(x) | \ 31*c3665006SThomas Abraham SDMMC_CLKSEL_CCLK_DRIVE(y) | \ 32*c3665006SThomas Abraham SDMMC_CLKSEL_CCLK_DIVIDER(z)) 33*c3665006SThomas Abraham 34*c3665006SThomas Abraham #define SDMMC_CMD_USE_HOLD_REG BIT(29) 35*c3665006SThomas Abraham 36*c3665006SThomas Abraham #define EXYNOS4210_FIXED_CIU_CLK_DIV 2 37*c3665006SThomas Abraham #define EXYNOS4412_FIXED_CIU_CLK_DIV 4 38*c3665006SThomas Abraham 39*c3665006SThomas Abraham /* Variations in Exynos specific dw-mshc controller */ 40*c3665006SThomas Abraham enum dw_mci_exynos_type { 41*c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS4210, 42*c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS4412, 43*c3665006SThomas Abraham DW_MCI_TYPE_EXYNOS5250, 44*c3665006SThomas Abraham }; 45*c3665006SThomas Abraham 46*c3665006SThomas Abraham /* Exynos implementation specific driver private data */ 47*c3665006SThomas Abraham struct dw_mci_exynos_priv_data { 48*c3665006SThomas Abraham enum dw_mci_exynos_type ctrl_type; 49*c3665006SThomas Abraham u8 ciu_div; 50*c3665006SThomas Abraham u32 sdr_timing; 51*c3665006SThomas Abraham u32 ddr_timing; 52*c3665006SThomas Abraham }; 53*c3665006SThomas Abraham 54*c3665006SThomas Abraham static struct dw_mci_exynos_compatible { 55*c3665006SThomas Abraham char *compatible; 56*c3665006SThomas Abraham enum dw_mci_exynos_type ctrl_type; 57*c3665006SThomas Abraham } exynos_compat[] = { 58*c3665006SThomas Abraham { 59*c3665006SThomas Abraham .compatible = "samsung,exynos4210-dw-mshc", 60*c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS4210, 61*c3665006SThomas Abraham }, { 62*c3665006SThomas Abraham .compatible = "samsung,exynos4412-dw-mshc", 63*c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS4412, 64*c3665006SThomas Abraham }, { 65*c3665006SThomas Abraham .compatible = "samsung,exynos5250-dw-mshc", 66*c3665006SThomas Abraham .ctrl_type = DW_MCI_TYPE_EXYNOS5250, 67*c3665006SThomas Abraham }, 68*c3665006SThomas Abraham }; 69*c3665006SThomas Abraham 70*c3665006SThomas Abraham static int dw_mci_exynos_priv_init(struct dw_mci *host) 71*c3665006SThomas Abraham { 72*c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv; 73*c3665006SThomas Abraham int idx; 74*c3665006SThomas Abraham 75*c3665006SThomas Abraham priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL); 76*c3665006SThomas Abraham if (!priv) { 77*c3665006SThomas Abraham dev_err(host->dev, "mem alloc failed for private data\n"); 78*c3665006SThomas Abraham return -ENOMEM; 79*c3665006SThomas Abraham } 80*c3665006SThomas Abraham 81*c3665006SThomas Abraham for (idx = 0; idx < ARRAY_SIZE(exynos_compat); idx++) { 82*c3665006SThomas Abraham if (of_device_is_compatible(host->dev->of_node, 83*c3665006SThomas Abraham exynos_compat[idx].compatible)) 84*c3665006SThomas Abraham priv->ctrl_type = exynos_compat[idx].ctrl_type; 85*c3665006SThomas Abraham } 86*c3665006SThomas Abraham 87*c3665006SThomas Abraham host->priv = priv; 88*c3665006SThomas Abraham return 0; 89*c3665006SThomas Abraham } 90*c3665006SThomas Abraham 91*c3665006SThomas Abraham static int dw_mci_exynos_setup_clock(struct dw_mci *host) 92*c3665006SThomas Abraham { 93*c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv = host->priv; 94*c3665006SThomas Abraham 95*c3665006SThomas Abraham if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS5250) 96*c3665006SThomas Abraham host->bus_hz /= (priv->ciu_div + 1); 97*c3665006SThomas Abraham else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4412) 98*c3665006SThomas Abraham host->bus_hz /= EXYNOS4412_FIXED_CIU_CLK_DIV; 99*c3665006SThomas Abraham else if (priv->ctrl_type == DW_MCI_TYPE_EXYNOS4210) 100*c3665006SThomas Abraham host->bus_hz /= EXYNOS4210_FIXED_CIU_CLK_DIV; 101*c3665006SThomas Abraham 102*c3665006SThomas Abraham return 0; 103*c3665006SThomas Abraham } 104*c3665006SThomas Abraham 105*c3665006SThomas Abraham static void dw_mci_exynos_prepare_command(struct dw_mci *host, u32 *cmdr) 106*c3665006SThomas Abraham { 107*c3665006SThomas Abraham /* 108*c3665006SThomas Abraham * Exynos4412 and Exynos5250 extends the use of CMD register with the 109*c3665006SThomas Abraham * use of bit 29 (which is reserved on standard MSHC controllers) for 110*c3665006SThomas Abraham * optionally bypassing the HOLD register for command and data. The 111*c3665006SThomas Abraham * HOLD register should be bypassed in case there is no phase shift 112*c3665006SThomas Abraham * applied on CMD/DATA that is sent to the card. 113*c3665006SThomas Abraham */ 114*c3665006SThomas Abraham if (SDMMC_CLKSEL_GET_DRV_WD3(mci_readl(host, CLKSEL))) 115*c3665006SThomas Abraham *cmdr |= SDMMC_CMD_USE_HOLD_REG; 116*c3665006SThomas Abraham } 117*c3665006SThomas Abraham 118*c3665006SThomas Abraham static void dw_mci_exynos_set_ios(struct dw_mci *host, struct mmc_ios *ios) 119*c3665006SThomas Abraham { 120*c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv = host->priv; 121*c3665006SThomas Abraham 122*c3665006SThomas Abraham if (ios->timing == MMC_TIMING_UHS_DDR50) 123*c3665006SThomas Abraham mci_writel(host, CLKSEL, priv->ddr_timing); 124*c3665006SThomas Abraham else 125*c3665006SThomas Abraham mci_writel(host, CLKSEL, priv->sdr_timing); 126*c3665006SThomas Abraham } 127*c3665006SThomas Abraham 128*c3665006SThomas Abraham static int dw_mci_exynos_parse_dt(struct dw_mci *host) 129*c3665006SThomas Abraham { 130*c3665006SThomas Abraham struct dw_mci_exynos_priv_data *priv = host->priv; 131*c3665006SThomas Abraham struct device_node *np = host->dev->of_node; 132*c3665006SThomas Abraham u32 timing[2]; 133*c3665006SThomas Abraham u32 div = 0; 134*c3665006SThomas Abraham int ret; 135*c3665006SThomas Abraham 136*c3665006SThomas Abraham of_property_read_u32(np, "samsung,dw-mshc-ciu-div", &div); 137*c3665006SThomas Abraham priv->ciu_div = div; 138*c3665006SThomas Abraham 139*c3665006SThomas Abraham ret = of_property_read_u32_array(np, 140*c3665006SThomas Abraham "samsung,dw-mshc-sdr-timing", timing, 2); 141*c3665006SThomas Abraham if (ret) 142*c3665006SThomas Abraham return ret; 143*c3665006SThomas Abraham 144*c3665006SThomas Abraham priv->sdr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div); 145*c3665006SThomas Abraham 146*c3665006SThomas Abraham ret = of_property_read_u32_array(np, 147*c3665006SThomas Abraham "samsung,dw-mshc-ddr-timing", timing, 2); 148*c3665006SThomas Abraham if (ret) 149*c3665006SThomas Abraham return ret; 150*c3665006SThomas Abraham 151*c3665006SThomas Abraham priv->ddr_timing = SDMMC_CLKSEL_TIMING(timing[0], timing[1], div); 152*c3665006SThomas Abraham return 0; 153*c3665006SThomas Abraham } 154*c3665006SThomas Abraham 155*c3665006SThomas Abraham static int dw_mci_exynos_setup_bus(struct dw_mci *host, 156*c3665006SThomas Abraham struct device_node *slot_np, u8 bus_width) 157*c3665006SThomas Abraham { 158*c3665006SThomas Abraham int idx, gpio, ret; 159*c3665006SThomas Abraham 160*c3665006SThomas Abraham if (!slot_np) 161*c3665006SThomas Abraham return -EINVAL; 162*c3665006SThomas Abraham 163*c3665006SThomas Abraham /* cmd + clock + bus-width pins */ 164*c3665006SThomas Abraham for (idx = 0; idx < NUM_PINS(bus_width); idx++) { 165*c3665006SThomas Abraham gpio = of_get_gpio(slot_np, idx); 166*c3665006SThomas Abraham if (!gpio_is_valid(gpio)) { 167*c3665006SThomas Abraham dev_err(host->dev, "invalid gpio: %d\n", gpio); 168*c3665006SThomas Abraham return -EINVAL; 169*c3665006SThomas Abraham } 170*c3665006SThomas Abraham 171*c3665006SThomas Abraham ret = devm_gpio_request(host->dev, gpio, "dw-mci-bus"); 172*c3665006SThomas Abraham if (ret) { 173*c3665006SThomas Abraham dev_err(host->dev, "gpio [%d] request failed\n", gpio); 174*c3665006SThomas Abraham return -EBUSY; 175*c3665006SThomas Abraham } 176*c3665006SThomas Abraham } 177*c3665006SThomas Abraham 178*c3665006SThomas Abraham gpio = of_get_named_gpio(slot_np, "wp-gpios", 0); 179*c3665006SThomas Abraham if (gpio_is_valid(gpio)) { 180*c3665006SThomas Abraham if (devm_gpio_request(host->dev, gpio, "dw-mci-wp")) 181*c3665006SThomas Abraham dev_info(host->dev, "gpio [%d] request failed\n", 182*c3665006SThomas Abraham gpio); 183*c3665006SThomas Abraham } else { 184*c3665006SThomas Abraham dev_info(host->dev, "wp gpio not available"); 185*c3665006SThomas Abraham host->pdata->quirks |= DW_MCI_QUIRK_NO_WRITE_PROTECT; 186*c3665006SThomas Abraham } 187*c3665006SThomas Abraham 188*c3665006SThomas Abraham if (host->pdata->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) 189*c3665006SThomas Abraham return 0; 190*c3665006SThomas Abraham 191*c3665006SThomas Abraham gpio = of_get_named_gpio(slot_np, "samsung,cd-pinmux-gpio", 0); 192*c3665006SThomas Abraham if (gpio_is_valid(gpio)) { 193*c3665006SThomas Abraham if (devm_gpio_request(host->dev, gpio, "dw-mci-cd")) 194*c3665006SThomas Abraham dev_err(host->dev, "gpio [%d] request failed\n", gpio); 195*c3665006SThomas Abraham } else { 196*c3665006SThomas Abraham dev_info(host->dev, "cd gpio not available"); 197*c3665006SThomas Abraham } 198*c3665006SThomas Abraham 199*c3665006SThomas Abraham return 0; 200*c3665006SThomas Abraham } 201*c3665006SThomas Abraham 202*c3665006SThomas Abraham /* Exynos5250 controller specific capabilities */ 203*c3665006SThomas Abraham static unsigned long exynos5250_dwmmc_caps[4] = { 204*c3665006SThomas Abraham MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR | 205*c3665006SThomas Abraham MMC_CAP_8_BIT_DATA | MMC_CAP_CMD23, 206*c3665006SThomas Abraham MMC_CAP_CMD23, 207*c3665006SThomas Abraham MMC_CAP_CMD23, 208*c3665006SThomas Abraham MMC_CAP_CMD23, 209*c3665006SThomas Abraham }; 210*c3665006SThomas Abraham 211*c3665006SThomas Abraham static struct dw_mci_drv_data exynos5250_drv_data = { 212*c3665006SThomas Abraham .caps = exynos5250_dwmmc_caps, 213*c3665006SThomas Abraham .init = dw_mci_exynos_priv_init, 214*c3665006SThomas Abraham .setup_clock = dw_mci_exynos_setup_clock, 215*c3665006SThomas Abraham .prepare_command = dw_mci_exynos_prepare_command, 216*c3665006SThomas Abraham .set_ios = dw_mci_exynos_set_ios, 217*c3665006SThomas Abraham .parse_dt = dw_mci_exynos_parse_dt, 218*c3665006SThomas Abraham .setup_bus = dw_mci_exynos_setup_bus, 219*c3665006SThomas Abraham }; 220*c3665006SThomas Abraham 221*c3665006SThomas Abraham static const struct of_device_id dw_mci_exynos_match[] = { 222*c3665006SThomas Abraham { .compatible = "samsung,exynos5250-dw-mshc", 223*c3665006SThomas Abraham .data = (void *)&exynos5250_drv_data, }, 224*c3665006SThomas Abraham {}, 225*c3665006SThomas Abraham }; 226*c3665006SThomas Abraham MODULE_DEVICE_TABLE(of, dw_mci_pltfm_match); 227*c3665006SThomas Abraham 228*c3665006SThomas Abraham int dw_mci_exynos_probe(struct platform_device *pdev) 229*c3665006SThomas Abraham { 230*c3665006SThomas Abraham struct dw_mci_drv_data *drv_data; 231*c3665006SThomas Abraham const struct of_device_id *match; 232*c3665006SThomas Abraham 233*c3665006SThomas Abraham match = of_match_node(dw_mci_exynos_match, pdev->dev.of_node); 234*c3665006SThomas Abraham drv_data = match->data; 235*c3665006SThomas Abraham return dw_mci_pltfm_register(pdev, drv_data); 236*c3665006SThomas Abraham } 237*c3665006SThomas Abraham 238*c3665006SThomas Abraham static struct platform_driver dw_mci_exynos_pltfm_driver = { 239*c3665006SThomas Abraham .probe = dw_mci_exynos_probe, 240*c3665006SThomas Abraham .remove = __exit_p(dw_mci_pltfm_remove), 241*c3665006SThomas Abraham .driver = { 242*c3665006SThomas Abraham .name = "dwmmc_exynos", 243*c3665006SThomas Abraham .of_match_table = of_match_ptr(dw_mci_exynos_match), 244*c3665006SThomas Abraham .pm = &dw_mci_pltfm_pmops, 245*c3665006SThomas Abraham }, 246*c3665006SThomas Abraham }; 247*c3665006SThomas Abraham 248*c3665006SThomas Abraham module_platform_driver(dw_mci_exynos_pltfm_driver); 249*c3665006SThomas Abraham 250*c3665006SThomas Abraham MODULE_DESCRIPTION("Samsung Specific DW-MSHC Driver Extension"); 251*c3665006SThomas Abraham MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com"); 252*c3665006SThomas Abraham MODULE_LICENSE("GPL v2"); 253*c3665006SThomas Abraham MODULE_ALIAS("platform:dwmmc-exynos"); 254