Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 4 | * |
| 5 | * Texas Instruments' K3 SD Host Controller Interface |
| 6 | */ |
| 7 | |
| 8 | #include <clk.h> |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <malloc.h> |
| 12 | #include <power-domain.h> |
| 13 | #include <sdhci.h> |
| 14 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 15 | #define AM654_SDHCI_MIN_FREQ 400000 |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 16 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 17 | struct am654_sdhci_plat { |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 18 | struct mmc_config cfg; |
| 19 | struct mmc mmc; |
| 20 | unsigned int f_max; |
| 21 | }; |
| 22 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 23 | static int am654_sdhci_probe(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 24 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 25 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 26 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 27 | struct sdhci_host *host = dev_get_priv(dev); |
| 28 | struct power_domain sdhci_pwrdmn; |
| 29 | struct clk clk; |
| 30 | unsigned long clock; |
| 31 | int ret; |
| 32 | |
| 33 | ret = power_domain_get_by_index(dev, &sdhci_pwrdmn, 0); |
| 34 | if (ret) { |
| 35 | dev_err(dev, "failed to get power domain\n"); |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | ret = power_domain_on(&sdhci_pwrdmn); |
| 40 | if (ret) { |
| 41 | dev_err(dev, "Power domain on failed\n"); |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | ret = clk_get_by_index(dev, 0, &clk); |
| 46 | if (ret) { |
| 47 | dev_err(dev, "failed to get clock\n"); |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | clock = clk_get_rate(&clk); |
| 52 | if (IS_ERR_VALUE(clock)) { |
| 53 | dev_err(dev, "failed to get rate\n"); |
| 54 | return clock; |
| 55 | } |
| 56 | |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 57 | host->max_clk = clock; |
| 58 | |
| 59 | ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max, |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 60 | AM654_SDHCI_MIN_FREQ); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 61 | host->mmc = &plat->mmc; |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | host->mmc->priv = host; |
| 65 | host->mmc->dev = dev; |
| 66 | upriv->mmc = host->mmc; |
| 67 | |
| 68 | return sdhci_probe(dev); |
| 69 | } |
| 70 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 71 | static int am654_sdhci_ofdata_to_platdata(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 72 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 73 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 74 | struct sdhci_host *host = dev_get_priv(dev); |
| 75 | |
| 76 | host->name = dev->name; |
| 77 | host->ioaddr = (void *)dev_read_addr(dev); |
| 78 | host->bus_width = dev_read_u32_default(dev, "bus-width", 4); |
| 79 | plat->f_max = dev_read_u32_default(dev, "max-frequency", 0); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 84 | static int am654_sdhci_bind(struct udevice *dev) |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 85 | { |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 86 | struct am654_sdhci_plat *plat = dev_get_platdata(dev); |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 87 | |
| 88 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 89 | } |
| 90 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 91 | static const struct udevice_id am654_sdhci_ids[] = { |
| 92 | { .compatible = "ti,am654-sdhci-5.1" }, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 93 | { } |
| 94 | }; |
| 95 | |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 96 | U_BOOT_DRIVER(am654_sdhci_drv) = { |
| 97 | .name = "am654_sdhci", |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 98 | .id = UCLASS_MMC, |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 99 | .of_match = am654_sdhci_ids, |
| 100 | .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 101 | .ops = &sdhci_ops, |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 102 | .bind = am654_sdhci_bind, |
| 103 | .probe = am654_sdhci_probe, |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 104 | .priv_auto_alloc_size = sizeof(struct sdhci_host), |
Faiz Abbas | 3a1a0df | 2019-06-11 00:43:31 +0530 | [diff] [blame] | 105 | .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat), |
Lokesh Vutla | aaa449f | 2018-08-27 15:57:54 +0530 | [diff] [blame] | 106 | }; |