Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2019 IBM Corp. |
| 4 | * Eddie James <eajames@linux.ibm.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <malloc.h> |
| 11 | #include <sdhci.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Joel Stanley | 818a6b5 | 2022-06-23 18:35:34 +0930 | [diff] [blame] | 13 | #include <dm/lists.h> |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 14 | |
| 15 | struct aspeed_sdhci_plat { |
| 16 | struct mmc_config cfg; |
| 17 | struct mmc mmc; |
| 18 | }; |
| 19 | |
| 20 | static int aspeed_sdhci_probe(struct udevice *dev) |
| 21 | { |
| 22 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 23 | struct aspeed_sdhci_plat *plat = dev_get_plat(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 24 | struct sdhci_host *host = dev_get_priv(dev); |
| 25 | u32 max_clk; |
| 26 | struct clk clk; |
| 27 | int ret; |
| 28 | |
| 29 | ret = clk_get_by_index(dev, 0, &clk); |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame] | 30 | if (ret) { |
| 31 | debug("%s: clock get failed %d\n", __func__, ret); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 32 | return ret; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame] | 33 | } |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 34 | |
| 35 | ret = clk_enable(&clk); |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame] | 36 | if (ret) { |
| 37 | debug("%s: clock enable failed %d\n", __func__, ret); |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 38 | return ret; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame] | 39 | } |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 40 | |
| 41 | host->name = dev->name; |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 42 | host->ioaddr = dev_read_addr_ptr(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 43 | |
| 44 | max_clk = clk_get_rate(&clk); |
| 45 | if (IS_ERR_VALUE(max_clk)) { |
| 46 | ret = max_clk; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame] | 47 | debug("%s: clock rate get failed %d\n", __func__, ret); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 48 | goto err; |
| 49 | } |
| 50 | |
| 51 | host->max_clk = max_clk; |
| 52 | host->mmc = &plat->mmc; |
| 53 | host->mmc->dev = dev; |
| 54 | host->mmc->priv = host; |
| 55 | upriv->mmc = host->mmc; |
| 56 | |
| 57 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 58 | if (ret) |
| 59 | goto err; |
| 60 | |
| 61 | ret = sdhci_probe(dev); |
| 62 | if (ret) |
| 63 | goto err; |
| 64 | |
| 65 | return 0; |
| 66 | |
| 67 | err: |
| 68 | clk_disable(&clk); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int aspeed_sdhci_bind(struct udevice *dev) |
| 73 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 74 | struct aspeed_sdhci_plat *plat = dev_get_plat(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 75 | |
| 76 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 77 | } |
| 78 | |
| 79 | static const struct udevice_id aspeed_sdhci_ids[] = { |
| 80 | { .compatible = "aspeed,ast2400-sdhci" }, |
| 81 | { .compatible = "aspeed,ast2500-sdhci" }, |
| 82 | { .compatible = "aspeed,ast2600-sdhci" }, |
| 83 | { } |
| 84 | }; |
| 85 | |
| 86 | U_BOOT_DRIVER(aspeed_sdhci_drv) = { |
| 87 | .name = "aspeed_sdhci", |
| 88 | .id = UCLASS_MMC, |
| 89 | .of_match = aspeed_sdhci_ids, |
| 90 | .ops = &sdhci_ops, |
| 91 | .bind = aspeed_sdhci_bind, |
| 92 | .probe = aspeed_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 93 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 94 | .plat_auto = sizeof(struct aspeed_sdhci_plat), |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 95 | }; |
Joel Stanley | 818a6b5 | 2022-06-23 18:35:34 +0930 | [diff] [blame] | 96 | |
| 97 | |
| 98 | static int aspeed_sdc_probe(struct udevice *parent) |
| 99 | { |
Joel Stanley | 8d2fb02 | 2022-06-23 18:35:35 +0930 | [diff] [blame] | 100 | struct clk clk; |
| 101 | int ret; |
| 102 | |
| 103 | ret = clk_get_by_index(parent, 0, &clk); |
| 104 | if (ret) { |
| 105 | debug("%s: clock get failed %d\n", __func__, ret); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | ret = clk_enable(&clk); |
| 110 | if (ret) { |
| 111 | debug("%s: clock enable failed %d\n", __func__, ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
Joel Stanley | 818a6b5 | 2022-06-23 18:35:34 +0930 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static const struct udevice_id aspeed_sdc_ids[] = { |
| 119 | { .compatible = "aspeed,ast2400-sd-controller" }, |
| 120 | { .compatible = "aspeed,ast2500-sd-controller" }, |
| 121 | { .compatible = "aspeed,ast2600-sd-controller" }, |
| 122 | { } |
| 123 | }; |
| 124 | |
| 125 | U_BOOT_DRIVER(aspeed_sdc_drv) = { |
| 126 | .name = "aspeed_sdc", |
| 127 | .id = UCLASS_MISC, |
| 128 | .of_match = aspeed_sdc_ids, |
| 129 | .probe = aspeed_sdc_probe, |
| 130 | }; |