1*bb5f8ea4Sludovic.desroches@atmel.com /* 2*bb5f8ea4Sludovic.desroches@atmel.com * Atmel SDMMC controller driver. 3*bb5f8ea4Sludovic.desroches@atmel.com * 4*bb5f8ea4Sludovic.desroches@atmel.com * Copyright (C) 2015 Atmel, 5*bb5f8ea4Sludovic.desroches@atmel.com * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> 6*bb5f8ea4Sludovic.desroches@atmel.com * 7*bb5f8ea4Sludovic.desroches@atmel.com * This software is licensed under the terms of the GNU General Public 8*bb5f8ea4Sludovic.desroches@atmel.com * License version 2, as published by the Free Software Foundation, and 9*bb5f8ea4Sludovic.desroches@atmel.com * may be copied, distributed, and modified under those terms. 10*bb5f8ea4Sludovic.desroches@atmel.com * 11*bb5f8ea4Sludovic.desroches@atmel.com * This program is distributed in the hope that it will be useful, 12*bb5f8ea4Sludovic.desroches@atmel.com * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bb5f8ea4Sludovic.desroches@atmel.com * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bb5f8ea4Sludovic.desroches@atmel.com * GNU General Public License for more details. 15*bb5f8ea4Sludovic.desroches@atmel.com */ 16*bb5f8ea4Sludovic.desroches@atmel.com 17*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/clk.h> 18*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/err.h> 19*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/io.h> 20*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/mmc/host.h> 21*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/module.h> 22*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/of.h> 23*bb5f8ea4Sludovic.desroches@atmel.com #include <linux/of_device.h> 24*bb5f8ea4Sludovic.desroches@atmel.com 25*bb5f8ea4Sludovic.desroches@atmel.com #include "sdhci-pltfm.h" 26*bb5f8ea4Sludovic.desroches@atmel.com 27*bb5f8ea4Sludovic.desroches@atmel.com #define SDMMC_CACR 0x230 28*bb5f8ea4Sludovic.desroches@atmel.com #define SDMMC_CACR_CAPWREN BIT(0) 29*bb5f8ea4Sludovic.desroches@atmel.com #define SDMMC_CACR_KEY (0x46 << 8) 30*bb5f8ea4Sludovic.desroches@atmel.com 31*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_at91_priv { 32*bb5f8ea4Sludovic.desroches@atmel.com struct clk *hclock; 33*bb5f8ea4Sludovic.desroches@atmel.com struct clk *gck; 34*bb5f8ea4Sludovic.desroches@atmel.com struct clk *mainck; 35*bb5f8ea4Sludovic.desroches@atmel.com }; 36*bb5f8ea4Sludovic.desroches@atmel.com 37*bb5f8ea4Sludovic.desroches@atmel.com static const struct sdhci_ops sdhci_at91_sama5d2_ops = { 38*bb5f8ea4Sludovic.desroches@atmel.com .set_clock = sdhci_set_clock, 39*bb5f8ea4Sludovic.desroches@atmel.com .set_bus_width = sdhci_set_bus_width, 40*bb5f8ea4Sludovic.desroches@atmel.com .reset = sdhci_reset, 41*bb5f8ea4Sludovic.desroches@atmel.com .set_uhs_signaling = sdhci_set_uhs_signaling, 42*bb5f8ea4Sludovic.desroches@atmel.com }; 43*bb5f8ea4Sludovic.desroches@atmel.com 44*bb5f8ea4Sludovic.desroches@atmel.com static const struct sdhci_pltfm_data soc_data_sama5d2 = { 45*bb5f8ea4Sludovic.desroches@atmel.com .ops = &sdhci_at91_sama5d2_ops, 46*bb5f8ea4Sludovic.desroches@atmel.com }; 47*bb5f8ea4Sludovic.desroches@atmel.com 48*bb5f8ea4Sludovic.desroches@atmel.com static const struct of_device_id sdhci_at91_dt_match[] = { 49*bb5f8ea4Sludovic.desroches@atmel.com { .compatible = "atmel,sama5d2-sdhci", .data = &soc_data_sama5d2 }, 50*bb5f8ea4Sludovic.desroches@atmel.com {} 51*bb5f8ea4Sludovic.desroches@atmel.com }; 52*bb5f8ea4Sludovic.desroches@atmel.com 53*bb5f8ea4Sludovic.desroches@atmel.com static int sdhci_at91_probe(struct platform_device *pdev) 54*bb5f8ea4Sludovic.desroches@atmel.com { 55*bb5f8ea4Sludovic.desroches@atmel.com const struct of_device_id *match; 56*bb5f8ea4Sludovic.desroches@atmel.com const struct sdhci_pltfm_data *soc_data; 57*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_host *host; 58*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_pltfm_host *pltfm_host; 59*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_at91_priv *priv; 60*bb5f8ea4Sludovic.desroches@atmel.com unsigned int caps0, caps1; 61*bb5f8ea4Sludovic.desroches@atmel.com unsigned int clk_base, clk_mul; 62*bb5f8ea4Sludovic.desroches@atmel.com unsigned int gck_rate, real_gck_rate; 63*bb5f8ea4Sludovic.desroches@atmel.com int ret; 64*bb5f8ea4Sludovic.desroches@atmel.com 65*bb5f8ea4Sludovic.desroches@atmel.com match = of_match_device(sdhci_at91_dt_match, &pdev->dev); 66*bb5f8ea4Sludovic.desroches@atmel.com if (!match) 67*bb5f8ea4Sludovic.desroches@atmel.com return -EINVAL; 68*bb5f8ea4Sludovic.desroches@atmel.com soc_data = match->data; 69*bb5f8ea4Sludovic.desroches@atmel.com 70*bb5f8ea4Sludovic.desroches@atmel.com priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 71*bb5f8ea4Sludovic.desroches@atmel.com if (!priv) { 72*bb5f8ea4Sludovic.desroches@atmel.com dev_err(&pdev->dev, "unable to allocate private data\n"); 73*bb5f8ea4Sludovic.desroches@atmel.com return -ENOMEM; 74*bb5f8ea4Sludovic.desroches@atmel.com } 75*bb5f8ea4Sludovic.desroches@atmel.com 76*bb5f8ea4Sludovic.desroches@atmel.com priv->mainck = devm_clk_get(&pdev->dev, "baseclk"); 77*bb5f8ea4Sludovic.desroches@atmel.com if (IS_ERR(priv->mainck)) { 78*bb5f8ea4Sludovic.desroches@atmel.com dev_err(&pdev->dev, "failed to get baseclk\n"); 79*bb5f8ea4Sludovic.desroches@atmel.com return PTR_ERR(priv->mainck); 80*bb5f8ea4Sludovic.desroches@atmel.com } 81*bb5f8ea4Sludovic.desroches@atmel.com 82*bb5f8ea4Sludovic.desroches@atmel.com priv->hclock = devm_clk_get(&pdev->dev, "hclock"); 83*bb5f8ea4Sludovic.desroches@atmel.com if (IS_ERR(priv->hclock)) { 84*bb5f8ea4Sludovic.desroches@atmel.com dev_err(&pdev->dev, "failed to get hclock\n"); 85*bb5f8ea4Sludovic.desroches@atmel.com return PTR_ERR(priv->hclock); 86*bb5f8ea4Sludovic.desroches@atmel.com } 87*bb5f8ea4Sludovic.desroches@atmel.com 88*bb5f8ea4Sludovic.desroches@atmel.com priv->gck = devm_clk_get(&pdev->dev, "multclk"); 89*bb5f8ea4Sludovic.desroches@atmel.com if (IS_ERR(priv->gck)) { 90*bb5f8ea4Sludovic.desroches@atmel.com dev_err(&pdev->dev, "failed to get multclk\n"); 91*bb5f8ea4Sludovic.desroches@atmel.com return PTR_ERR(priv->gck); 92*bb5f8ea4Sludovic.desroches@atmel.com } 93*bb5f8ea4Sludovic.desroches@atmel.com 94*bb5f8ea4Sludovic.desroches@atmel.com host = sdhci_pltfm_init(pdev, soc_data, 0); 95*bb5f8ea4Sludovic.desroches@atmel.com if (IS_ERR(host)) 96*bb5f8ea4Sludovic.desroches@atmel.com return PTR_ERR(host); 97*bb5f8ea4Sludovic.desroches@atmel.com 98*bb5f8ea4Sludovic.desroches@atmel.com /* 99*bb5f8ea4Sludovic.desroches@atmel.com * The mult clock is provided by as a generated clock by the PMC 100*bb5f8ea4Sludovic.desroches@atmel.com * controller. In order to set the rate of gck, we have to get the 101*bb5f8ea4Sludovic.desroches@atmel.com * base clock rate and the clock mult from capabilities. 102*bb5f8ea4Sludovic.desroches@atmel.com */ 103*bb5f8ea4Sludovic.desroches@atmel.com clk_prepare_enable(priv->hclock); 104*bb5f8ea4Sludovic.desroches@atmel.com caps0 = readl(host->ioaddr + SDHCI_CAPABILITIES); 105*bb5f8ea4Sludovic.desroches@atmel.com caps1 = readl(host->ioaddr + SDHCI_CAPABILITIES_1); 106*bb5f8ea4Sludovic.desroches@atmel.com clk_base = (caps0 & SDHCI_CLOCK_V3_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; 107*bb5f8ea4Sludovic.desroches@atmel.com clk_mul = (caps1 & SDHCI_CLOCK_MUL_MASK) >> SDHCI_CLOCK_MUL_SHIFT; 108*bb5f8ea4Sludovic.desroches@atmel.com gck_rate = clk_base * 1000000 * (clk_mul + 1); 109*bb5f8ea4Sludovic.desroches@atmel.com ret = clk_set_rate(priv->gck, gck_rate); 110*bb5f8ea4Sludovic.desroches@atmel.com if (ret < 0) { 111*bb5f8ea4Sludovic.desroches@atmel.com dev_err(&pdev->dev, "failed to set gck"); 112*bb5f8ea4Sludovic.desroches@atmel.com goto hclock_disable_unprepare; 113*bb5f8ea4Sludovic.desroches@atmel.com return -EINVAL; 114*bb5f8ea4Sludovic.desroches@atmel.com } 115*bb5f8ea4Sludovic.desroches@atmel.com /* 116*bb5f8ea4Sludovic.desroches@atmel.com * We need to check if we have the requested rate for gck because in 117*bb5f8ea4Sludovic.desroches@atmel.com * some cases this rate could be not supported. If it happens, the rate 118*bb5f8ea4Sludovic.desroches@atmel.com * is the closest one gck can provide. We have to update the value 119*bb5f8ea4Sludovic.desroches@atmel.com * of clk mul. 120*bb5f8ea4Sludovic.desroches@atmel.com */ 121*bb5f8ea4Sludovic.desroches@atmel.com real_gck_rate = clk_get_rate(priv->gck); 122*bb5f8ea4Sludovic.desroches@atmel.com if (real_gck_rate != gck_rate) { 123*bb5f8ea4Sludovic.desroches@atmel.com clk_mul = real_gck_rate / (clk_base * 1000000) - 1; 124*bb5f8ea4Sludovic.desroches@atmel.com caps1 &= (~SDHCI_CLOCK_MUL_MASK); 125*bb5f8ea4Sludovic.desroches@atmel.com caps1 |= ((clk_mul << SDHCI_CLOCK_MUL_SHIFT) & SDHCI_CLOCK_MUL_MASK); 126*bb5f8ea4Sludovic.desroches@atmel.com /* Set capabilities in r/w mode. */ 127*bb5f8ea4Sludovic.desroches@atmel.com writel(SDMMC_CACR_KEY | SDMMC_CACR_CAPWREN, host->ioaddr + SDMMC_CACR); 128*bb5f8ea4Sludovic.desroches@atmel.com writel(caps1, host->ioaddr + SDHCI_CAPABILITIES_1); 129*bb5f8ea4Sludovic.desroches@atmel.com /* Set capabilities in ro mode. */ 130*bb5f8ea4Sludovic.desroches@atmel.com writel(0, host->ioaddr + SDMMC_CACR); 131*bb5f8ea4Sludovic.desroches@atmel.com dev_info(&pdev->dev, "update clk mul to %u as gck rate is %u Hz\n", 132*bb5f8ea4Sludovic.desroches@atmel.com clk_mul, real_gck_rate); 133*bb5f8ea4Sludovic.desroches@atmel.com } 134*bb5f8ea4Sludovic.desroches@atmel.com 135*bb5f8ea4Sludovic.desroches@atmel.com clk_prepare_enable(priv->mainck); 136*bb5f8ea4Sludovic.desroches@atmel.com clk_prepare_enable(priv->gck); 137*bb5f8ea4Sludovic.desroches@atmel.com 138*bb5f8ea4Sludovic.desroches@atmel.com pltfm_host = sdhci_priv(host); 139*bb5f8ea4Sludovic.desroches@atmel.com pltfm_host->priv = priv; 140*bb5f8ea4Sludovic.desroches@atmel.com 141*bb5f8ea4Sludovic.desroches@atmel.com ret = mmc_of_parse(host->mmc); 142*bb5f8ea4Sludovic.desroches@atmel.com if (ret) 143*bb5f8ea4Sludovic.desroches@atmel.com goto clocks_disable_unprepare; 144*bb5f8ea4Sludovic.desroches@atmel.com 145*bb5f8ea4Sludovic.desroches@atmel.com sdhci_get_of_property(pdev); 146*bb5f8ea4Sludovic.desroches@atmel.com 147*bb5f8ea4Sludovic.desroches@atmel.com ret = sdhci_add_host(host); 148*bb5f8ea4Sludovic.desroches@atmel.com if (ret) 149*bb5f8ea4Sludovic.desroches@atmel.com goto clocks_disable_unprepare; 150*bb5f8ea4Sludovic.desroches@atmel.com 151*bb5f8ea4Sludovic.desroches@atmel.com return 0; 152*bb5f8ea4Sludovic.desroches@atmel.com 153*bb5f8ea4Sludovic.desroches@atmel.com clocks_disable_unprepare: 154*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->gck); 155*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->mainck); 156*bb5f8ea4Sludovic.desroches@atmel.com hclock_disable_unprepare: 157*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->hclock); 158*bb5f8ea4Sludovic.desroches@atmel.com sdhci_pltfm_free(pdev); 159*bb5f8ea4Sludovic.desroches@atmel.com return ret; 160*bb5f8ea4Sludovic.desroches@atmel.com } 161*bb5f8ea4Sludovic.desroches@atmel.com 162*bb5f8ea4Sludovic.desroches@atmel.com static int sdhci_at91_remove(struct platform_device *pdev) 163*bb5f8ea4Sludovic.desroches@atmel.com { 164*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_host *host = platform_get_drvdata(pdev); 165*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); 166*bb5f8ea4Sludovic.desroches@atmel.com struct sdhci_at91_priv *priv = pltfm_host->priv; 167*bb5f8ea4Sludovic.desroches@atmel.com 168*bb5f8ea4Sludovic.desroches@atmel.com sdhci_pltfm_unregister(pdev); 169*bb5f8ea4Sludovic.desroches@atmel.com 170*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->gck); 171*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->hclock); 172*bb5f8ea4Sludovic.desroches@atmel.com clk_disable_unprepare(priv->mainck); 173*bb5f8ea4Sludovic.desroches@atmel.com 174*bb5f8ea4Sludovic.desroches@atmel.com return 0; 175*bb5f8ea4Sludovic.desroches@atmel.com } 176*bb5f8ea4Sludovic.desroches@atmel.com 177*bb5f8ea4Sludovic.desroches@atmel.com static struct platform_driver sdhci_at91_driver = { 178*bb5f8ea4Sludovic.desroches@atmel.com .driver = { 179*bb5f8ea4Sludovic.desroches@atmel.com .name = "sdhci-at91", 180*bb5f8ea4Sludovic.desroches@atmel.com .owner = THIS_MODULE, 181*bb5f8ea4Sludovic.desroches@atmel.com .of_match_table = sdhci_at91_dt_match, 182*bb5f8ea4Sludovic.desroches@atmel.com .pm = SDHCI_PLTFM_PMOPS, 183*bb5f8ea4Sludovic.desroches@atmel.com }, 184*bb5f8ea4Sludovic.desroches@atmel.com .probe = sdhci_at91_probe, 185*bb5f8ea4Sludovic.desroches@atmel.com .remove = sdhci_at91_remove, 186*bb5f8ea4Sludovic.desroches@atmel.com }; 187*bb5f8ea4Sludovic.desroches@atmel.com 188*bb5f8ea4Sludovic.desroches@atmel.com module_platform_driver(sdhci_at91_driver); 189*bb5f8ea4Sludovic.desroches@atmel.com 190*bb5f8ea4Sludovic.desroches@atmel.com MODULE_DESCRIPTION("SDHCI driver for at91"); 191*bb5f8ea4Sludovic.desroches@atmel.com MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>"); 192*bb5f8ea4Sludovic.desroches@atmel.com MODULE_LICENSE("GPL v2"); 193