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> |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 13 | |
| 14 | struct aspeed_sdhci_plat { |
| 15 | struct mmc_config cfg; |
| 16 | struct mmc mmc; |
| 17 | }; |
| 18 | |
| 19 | static int aspeed_sdhci_probe(struct udevice *dev) |
| 20 | { |
| 21 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 22 | struct aspeed_sdhci_plat *plat = dev_get_plat(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 23 | struct sdhci_host *host = dev_get_priv(dev); |
| 24 | u32 max_clk; |
| 25 | struct clk clk; |
| 26 | int ret; |
| 27 | |
| 28 | ret = clk_get_by_index(dev, 0, &clk); |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame^] | 29 | if (ret) { |
| 30 | debug("%s: clock get failed %d\n", __func__, ret); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 31 | return ret; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame^] | 32 | } |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 33 | |
| 34 | ret = clk_enable(&clk); |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame^] | 35 | if (ret) { |
| 36 | debug("%s: clock enable failed %d\n", __func__, ret); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 37 | goto free; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame^] | 38 | } |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 39 | |
| 40 | host->name = dev->name; |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 41 | host->ioaddr = dev_read_addr_ptr(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 42 | |
| 43 | max_clk = clk_get_rate(&clk); |
| 44 | if (IS_ERR_VALUE(max_clk)) { |
| 45 | ret = max_clk; |
Joel Stanley | 7dab5c1 | 2022-06-23 18:35:33 +0930 | [diff] [blame^] | 46 | debug("%s: clock rate get failed %d\n", __func__, ret); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 47 | goto err; |
| 48 | } |
| 49 | |
| 50 | host->max_clk = max_clk; |
| 51 | host->mmc = &plat->mmc; |
| 52 | host->mmc->dev = dev; |
| 53 | host->mmc->priv = host; |
| 54 | upriv->mmc = host->mmc; |
| 55 | |
| 56 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 57 | if (ret) |
| 58 | goto err; |
| 59 | |
| 60 | ret = sdhci_probe(dev); |
| 61 | if (ret) |
| 62 | goto err; |
| 63 | |
| 64 | return 0; |
| 65 | |
| 66 | err: |
| 67 | clk_disable(&clk); |
| 68 | free: |
| 69 | clk_free(&clk); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static int aspeed_sdhci_bind(struct udevice *dev) |
| 74 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 75 | struct aspeed_sdhci_plat *plat = dev_get_plat(dev); |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 76 | |
| 77 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 78 | } |
| 79 | |
| 80 | static const struct udevice_id aspeed_sdhci_ids[] = { |
| 81 | { .compatible = "aspeed,ast2400-sdhci" }, |
| 82 | { .compatible = "aspeed,ast2500-sdhci" }, |
| 83 | { .compatible = "aspeed,ast2600-sdhci" }, |
| 84 | { } |
| 85 | }; |
| 86 | |
| 87 | U_BOOT_DRIVER(aspeed_sdhci_drv) = { |
| 88 | .name = "aspeed_sdhci", |
| 89 | .id = UCLASS_MMC, |
| 90 | .of_match = aspeed_sdhci_ids, |
| 91 | .ops = &sdhci_ops, |
| 92 | .bind = aspeed_sdhci_bind, |
| 93 | .probe = aspeed_sdhci_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 94 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 95 | .plat_auto = sizeof(struct aspeed_sdhci_plat), |
Eddie James | c8a6581 | 2019-08-27 09:48:03 -0500 | [diff] [blame] | 96 | }; |