1 // SPDX-License-Identifier: GPL-2.0
3 * sdhci_am654.c - SDHCI driver for TI's AM654 SOCs
5 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/property.h>
13 #include <linux/regmap.h>
15 #include "sdhci-pltfm.h"
17 /* CTL_CFG Registers */
18 #define CTL_CFG_2 0x14
20 #define SLOTTYPE_MASK GENMASK(31, 30)
21 #define SLOTTYPE_EMBEDDED BIT(30)
24 #define PHY_CTRL1 0x100
25 #define PHY_CTRL2 0x104
26 #define PHY_CTRL3 0x108
27 #define PHY_CTRL4 0x10C
28 #define PHY_CTRL5 0x110
29 #define PHY_CTRL6 0x114
30 #define PHY_STAT1 0x130
31 #define PHY_STAT2 0x134
33 #define IOMUX_ENABLE_SHIFT 31
34 #define IOMUX_ENABLE_MASK BIT(IOMUX_ENABLE_SHIFT)
35 #define OTAPDLYENA_SHIFT 20
36 #define OTAPDLYENA_MASK BIT(OTAPDLYENA_SHIFT)
37 #define OTAPDLYSEL_SHIFT 12
38 #define OTAPDLYSEL_MASK GENMASK(15, 12)
39 #define STRBSEL_SHIFT 24
40 #define STRBSEL_4BIT_MASK GENMASK(27, 24)
41 #define STRBSEL_8BIT_MASK GENMASK(31, 24)
43 #define SEL50_MASK BIT(SEL50_SHIFT)
44 #define SEL100_SHIFT 9
45 #define SEL100_MASK BIT(SEL100_SHIFT)
46 #define FREQSEL_SHIFT 8
47 #define FREQSEL_MASK GENMASK(10, 8)
48 #define DLL_TRIM_ICP_SHIFT 4
49 #define DLL_TRIM_ICP_MASK GENMASK(7, 4)
50 #define DR_TY_SHIFT 20
51 #define DR_TY_MASK GENMASK(22, 20)
53 #define ENDLL_MASK BIT(ENDLL_SHIFT)
54 #define DLLRDY_SHIFT 0
55 #define DLLRDY_MASK BIT(DLLRDY_SHIFT)
57 #define PDB_MASK BIT(PDB_SHIFT)
58 #define CALDONE_SHIFT 1
59 #define CALDONE_MASK BIT(CALDONE_SHIFT)
60 #define RETRIM_SHIFT 17
61 #define RETRIM_MASK BIT(RETRIM_SHIFT)
63 #define DRIVER_STRENGTH_50_OHM 0x0
64 #define DRIVER_STRENGTH_33_OHM 0x1
65 #define DRIVER_STRENGTH_66_OHM 0x2
66 #define DRIVER_STRENGTH_100_OHM 0x3
67 #define DRIVER_STRENGTH_40_OHM 0x4
69 #define CLOCK_TOO_SLOW_HZ 400000
71 static struct regmap_config sdhci_am654_regmap_config = {
78 struct sdhci_am654_data {
88 struct sdhci_am654_driver_data {
89 const struct sdhci_pltfm_data *pdata;
91 #define IOMUX_PRESENT (1 << 0)
92 #define FREQSEL_2_BIT (1 << 1)
93 #define STRBSEL_4_BIT (1 << 2)
94 #define DLL_PRESENT (1 << 3)
97 static void sdhci_am654_set_clock(struct sdhci_host *host, unsigned int clock)
99 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
100 struct sdhci_am654_data *sdhci_am654 = sdhci_pltfm_priv(pltfm_host);
101 int sel50, sel100, freqsel;
105 if (sdhci_am654->dll_on) {
106 regmap_update_bits(sdhci_am654->base, PHY_CTRL1, ENDLL_MASK, 0);
108 sdhci_am654->dll_on = false;
111 sdhci_set_clock(host, clock);
113 if (clock > CLOCK_TOO_SLOW_HZ) {
114 /* Setup DLL Output TAP delay */
115 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
116 val = (1 << OTAPDLYENA_SHIFT) |
117 (sdhci_am654->otap_del_sel << OTAPDLYSEL_SHIFT);
118 regmap_update_bits(sdhci_am654->base, PHY_CTRL4, mask, val);
119 /* Write to STRBSEL for HS400 speed mode */
120 if (host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
121 if (sdhci_am654->flags & STRBSEL_4_BIT)
122 mask = STRBSEL_4BIT_MASK;
124 mask = STRBSEL_8BIT_MASK;
126 regmap_update_bits(sdhci_am654->base, PHY_CTRL4, mask,
127 sdhci_am654->strb_sel <<
131 if (sdhci_am654->flags & FREQSEL_2_BIT) {
146 /* Configure PHY DLL frequency */
147 mask = SEL50_MASK | SEL100_MASK;
148 val = (sel50 << SEL50_SHIFT) | (sel100 << SEL100_SHIFT);
149 regmap_update_bits(sdhci_am654->base, PHY_CTRL5, mask,
160 regmap_update_bits(sdhci_am654->base, PHY_CTRL5,
162 freqsel << FREQSEL_SHIFT);
165 /* Configure DLL TRIM */
166 mask = DLL_TRIM_ICP_MASK;
167 val = sdhci_am654->trm_icp << DLL_TRIM_ICP_SHIFT;
169 /* Configure DLL driver strength */
171 val |= sdhci_am654->drv_strength << DR_TY_SHIFT;
172 regmap_update_bits(sdhci_am654->base, PHY_CTRL1, mask, val);
174 regmap_update_bits(sdhci_am654->base, PHY_CTRL1, ENDLL_MASK,
177 * Poll for DLL ready. Use a one second timeout.
178 * Works in all experiments done so far
180 ret = regmap_read_poll_timeout(sdhci_am654->base, PHY_STAT1,
181 val, val & DLLRDY_MASK, 1000,
184 dev_err(mmc_dev(host->mmc), "DLL failed to relock\n");
188 sdhci_am654->dll_on = true;
192 static void sdhci_j721e_4bit_set_clock(struct sdhci_host *host,
195 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
196 struct sdhci_am654_data *sdhci_am654 = sdhci_pltfm_priv(pltfm_host);
199 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
200 val = (1 << OTAPDLYENA_SHIFT) |
201 (sdhci_am654->otap_del_sel << OTAPDLYSEL_SHIFT);
202 regmap_update_bits(sdhci_am654->base, PHY_CTRL4, mask, val);
204 sdhci_set_clock(host, clock);
207 static void sdhci_am654_set_power(struct sdhci_host *host, unsigned char mode,
210 if (!IS_ERR(host->mmc->supply.vmmc)) {
211 struct mmc_host *mmc = host->mmc;
213 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
215 sdhci_set_power_noreg(host, mode, vdd);
218 static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
220 unsigned char timing = host->mmc->ios.timing;
222 if (reg == SDHCI_HOST_CONTROL) {
225 * According to the data manual, HISPD bit
226 * should not be set in these speed modes.
228 case MMC_TIMING_SD_HS:
229 case MMC_TIMING_MMC_HS:
230 case MMC_TIMING_UHS_SDR12:
231 case MMC_TIMING_UHS_SDR25:
232 val &= ~SDHCI_CTRL_HISPD;
236 writeb(val, host->ioaddr + reg);
239 static struct sdhci_ops sdhci_am654_ops = {
240 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
241 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
242 .set_uhs_signaling = sdhci_set_uhs_signaling,
243 .set_bus_width = sdhci_set_bus_width,
244 .set_power = sdhci_am654_set_power,
245 .set_clock = sdhci_am654_set_clock,
246 .write_b = sdhci_am654_write_b,
247 .reset = sdhci_reset,
250 static const struct sdhci_pltfm_data sdhci_am654_pdata = {
251 .ops = &sdhci_am654_ops,
252 .quirks = SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
253 SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
254 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
257 static const struct sdhci_am654_driver_data sdhci_am654_drvdata = {
258 .pdata = &sdhci_am654_pdata,
259 .flags = IOMUX_PRESENT | FREQSEL_2_BIT | STRBSEL_4_BIT | DLL_PRESENT,
262 static struct sdhci_ops sdhci_j721e_8bit_ops = {
263 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
264 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
265 .set_uhs_signaling = sdhci_set_uhs_signaling,
266 .set_bus_width = sdhci_set_bus_width,
267 .set_power = sdhci_am654_set_power,
268 .set_clock = sdhci_am654_set_clock,
269 .write_b = sdhci_am654_write_b,
270 .reset = sdhci_reset,
273 static const struct sdhci_pltfm_data sdhci_j721e_8bit_pdata = {
274 .ops = &sdhci_j721e_8bit_ops,
275 .quirks = SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
276 SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
277 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
280 static const struct sdhci_am654_driver_data sdhci_j721e_8bit_drvdata = {
281 .pdata = &sdhci_j721e_8bit_pdata,
282 .flags = DLL_PRESENT,
285 static struct sdhci_ops sdhci_j721e_4bit_ops = {
286 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
287 .get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
288 .set_uhs_signaling = sdhci_set_uhs_signaling,
289 .set_bus_width = sdhci_set_bus_width,
290 .set_power = sdhci_am654_set_power,
291 .set_clock = sdhci_j721e_4bit_set_clock,
292 .write_b = sdhci_am654_write_b,
293 .reset = sdhci_reset,
296 static const struct sdhci_pltfm_data sdhci_j721e_4bit_pdata = {
297 .ops = &sdhci_j721e_4bit_ops,
298 .quirks = SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
299 SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
300 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
303 static const struct sdhci_am654_driver_data sdhci_j721e_4bit_drvdata = {
304 .pdata = &sdhci_j721e_4bit_pdata,
305 .flags = IOMUX_PRESENT,
307 static int sdhci_am654_init(struct sdhci_host *host)
309 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
310 struct sdhci_am654_data *sdhci_am654 = sdhci_pltfm_priv(pltfm_host);
316 /* Reset OTAP to default value */
317 mask = OTAPDLYENA_MASK | OTAPDLYSEL_MASK;
318 regmap_update_bits(sdhci_am654->base, PHY_CTRL4, mask, 0x0);
320 if (sdhci_am654->flags & DLL_PRESENT) {
321 regmap_read(sdhci_am654->base, PHY_STAT1, &val);
322 if (~val & CALDONE_MASK) {
323 /* Calibrate IO lines */
324 regmap_update_bits(sdhci_am654->base, PHY_CTRL1,
326 ret = regmap_read_poll_timeout(sdhci_am654->base,
335 /* Enable pins by setting IO mux to 0 */
336 if (sdhci_am654->flags & IOMUX_PRESENT)
337 regmap_update_bits(sdhci_am654->base, PHY_CTRL1,
338 IOMUX_ENABLE_MASK, 0);
340 /* Set slot type based on SD or eMMC */
341 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
342 ctl_cfg_2 = SLOTTYPE_EMBEDDED;
344 regmap_update_bits(sdhci_am654->base, CTL_CFG_2, SLOTTYPE_MASK,
347 return sdhci_add_host(host);
350 static int sdhci_am654_get_of_property(struct platform_device *pdev,
351 struct sdhci_am654_data *sdhci_am654)
353 struct device *dev = &pdev->dev;
357 ret = device_property_read_u32(dev, "ti,otap-del-sel",
358 &sdhci_am654->otap_del_sel);
362 if (sdhci_am654->flags & DLL_PRESENT) {
363 ret = device_property_read_u32(dev, "ti,trm-icp",
364 &sdhci_am654->trm_icp);
368 ret = device_property_read_u32(dev, "ti,driver-strength-ohm",
373 switch (drv_strength) {
375 sdhci_am654->drv_strength = DRIVER_STRENGTH_50_OHM;
378 sdhci_am654->drv_strength = DRIVER_STRENGTH_33_OHM;
381 sdhci_am654->drv_strength = DRIVER_STRENGTH_66_OHM;
384 sdhci_am654->drv_strength = DRIVER_STRENGTH_100_OHM;
387 sdhci_am654->drv_strength = DRIVER_STRENGTH_40_OHM;
390 dev_err(dev, "Invalid driver strength\n");
395 device_property_read_u32(dev, "ti,strobe-sel", &sdhci_am654->strb_sel);
397 sdhci_get_of_property(pdev);
402 static const struct of_device_id sdhci_am654_of_match[] = {
404 .compatible = "ti,am654-sdhci-5.1",
405 .data = &sdhci_am654_drvdata,
408 .compatible = "ti,j721e-sdhci-8bit",
409 .data = &sdhci_j721e_8bit_drvdata,
412 .compatible = "ti,j721e-sdhci-4bit",
413 .data = &sdhci_j721e_4bit_drvdata,
418 static int sdhci_am654_probe(struct platform_device *pdev)
420 const struct sdhci_am654_driver_data *drvdata;
421 struct sdhci_pltfm_host *pltfm_host;
422 struct sdhci_am654_data *sdhci_am654;
423 const struct of_device_id *match;
424 struct sdhci_host *host;
425 struct resource *res;
427 struct device *dev = &pdev->dev;
431 match = of_match_node(sdhci_am654_of_match, pdev->dev.of_node);
432 drvdata = match->data;
433 host = sdhci_pltfm_init(pdev, drvdata->pdata, sizeof(*sdhci_am654));
435 return PTR_ERR(host);
437 pltfm_host = sdhci_priv(host);
438 sdhci_am654 = sdhci_pltfm_priv(pltfm_host);
439 sdhci_am654->flags = drvdata->flags;
441 clk_xin = devm_clk_get(dev, "clk_xin");
442 if (IS_ERR(clk_xin)) {
443 dev_err(dev, "clk_xin clock not found.\n");
444 ret = PTR_ERR(clk_xin);
448 pltfm_host->clk = clk_xin;
450 /* Clocks are enabled using pm_runtime */
451 pm_runtime_enable(dev);
452 ret = pm_runtime_get_sync(dev);
454 pm_runtime_put_noidle(dev);
455 goto pm_runtime_disable;
458 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
459 base = devm_ioremap_resource(dev, res);
465 sdhci_am654->base = devm_regmap_init_mmio(dev, base,
466 &sdhci_am654_regmap_config);
467 if (IS_ERR(sdhci_am654->base)) {
468 dev_err(dev, "Failed to initialize regmap\n");
469 ret = PTR_ERR(sdhci_am654->base);
473 ret = sdhci_am654_get_of_property(pdev, sdhci_am654);
477 ret = mmc_of_parse(host->mmc);
479 dev_err(dev, "parsing dt failed (%d)\n", ret);
483 ret = sdhci_am654_init(host);
490 pm_runtime_put_sync(dev);
492 pm_runtime_disable(dev);
494 sdhci_pltfm_free(pdev);
498 static int sdhci_am654_remove(struct platform_device *pdev)
500 struct sdhci_host *host = platform_get_drvdata(pdev);
503 sdhci_remove_host(host, true);
504 ret = pm_runtime_put_sync(&pdev->dev);
508 pm_runtime_disable(&pdev->dev);
509 sdhci_pltfm_free(pdev);
514 static struct platform_driver sdhci_am654_driver = {
516 .name = "sdhci-am654",
517 .of_match_table = sdhci_am654_of_match,
519 .probe = sdhci_am654_probe,
520 .remove = sdhci_am654_remove,
523 module_platform_driver(sdhci_am654_driver);
525 MODULE_DESCRIPTION("Driver for SDHCI Controller on TI's AM654 devices");
526 MODULE_AUTHOR("Faiz Abbas <faiz_abbas@ti.com>");
527 MODULE_LICENSE("GPL");