Jim Liu | 29cf22c | 2022-05-24 16:55:33 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2022 Nuvoton Technology Corp. |
| 4 | */ |
| 5 | |
Jim Liu | 29cf22c | 2022-05-24 16:55:33 +0800 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <sdhci.h> |
| 8 | #include <clk.h> |
| 9 | #include <power/regulator.h> |
| 10 | |
| 11 | #define NPCM_SDHC_MIN_FREQ 400000 |
| 12 | |
| 13 | struct npcm_sdhci_plat { |
| 14 | struct mmc_config cfg; |
| 15 | struct mmc mmc; |
| 16 | }; |
| 17 | |
| 18 | static int npcm_sdhci_probe(struct udevice *dev) |
| 19 | { |
| 20 | struct npcm_sdhci_plat *plat = dev_get_plat(dev); |
| 21 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 22 | struct sdhci_host *host = dev_get_priv(dev); |
| 23 | struct udevice *vqmmc_supply; |
| 24 | int vqmmc_uv, ret; |
| 25 | struct clk clk; |
| 26 | |
| 27 | host->name = dev->name; |
| 28 | host->ioaddr = dev_read_addr_ptr(dev); |
| 29 | host->max_clk = dev_read_u32_default(dev, "clock-frequency", 0); |
| 30 | |
| 31 | ret = clk_get_by_index(dev, 0, &clk); |
| 32 | if (!ret && host->max_clk) { |
| 33 | ret = clk_set_rate(&clk, host->max_clk); |
| 34 | if (ret < 0) |
| 35 | return ret; |
| 36 | } |
| 37 | |
Marek Vasut | 3301e2e | 2023-02-28 02:55:19 +0100 | [diff] [blame] | 38 | if (CONFIG_IS_ENABLED(DM_REGULATOR)) { |
Jim Liu | 29cf22c | 2022-05-24 16:55:33 +0800 | [diff] [blame] | 39 | device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_supply); |
| 40 | vqmmc_uv = dev_read_u32_default(dev, "vqmmc-microvolt", 0); |
| 41 | /* Set IO voltage */ |
| 42 | if (vqmmc_supply && vqmmc_uv) |
| 43 | regulator_set_value(vqmmc_supply, vqmmc_uv); |
| 44 | } |
| 45 | |
| 46 | host->index = dev_read_u32_default(dev, "index", 0); |
| 47 | ret = mmc_of_parse(dev, &plat->cfg); |
| 48 | if (ret) |
| 49 | return ret; |
| 50 | |
| 51 | host->mmc = &plat->mmc; |
| 52 | host->mmc->priv = host; |
| 53 | host->mmc->dev = dev; |
| 54 | upriv->mmc = host->mmc; |
| 55 | |
| 56 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, NPCM_SDHC_MIN_FREQ); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | return sdhci_probe(dev); |
| 61 | } |
| 62 | |
| 63 | static int npcm_sdhci_bind(struct udevice *dev) |
| 64 | { |
| 65 | struct npcm_sdhci_plat *plat = dev_get_plat(dev); |
| 66 | |
| 67 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 68 | } |
| 69 | |
| 70 | static const struct udevice_id npcm_mmc_ids[] = { |
| 71 | { .compatible = "nuvoton,npcm750-sdhci" }, |
| 72 | { .compatible = "nuvoton,npcm845-sdhci" }, |
| 73 | { } |
| 74 | }; |
| 75 | |
| 76 | U_BOOT_DRIVER(npcm_sdhci_drv) = { |
| 77 | .name = "npcm_sdhci", |
| 78 | .id = UCLASS_MMC, |
| 79 | .of_match = npcm_mmc_ids, |
| 80 | .ops = &sdhci_ops, |
| 81 | .bind = npcm_sdhci_bind, |
| 82 | .probe = npcm_sdhci_probe, |
| 83 | .priv_auto = sizeof(struct sdhci_host), |
| 84 | .plat_auto = sizeof(struct npcm_sdhci_plat), |
| 85 | }; |