blob: a8c92277f7a92665d4c785bcc7ac45b4dcf1ae37 [file] [log] [blame]
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +05301// 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 Abbas3a1a0df2019-06-11 00:43:31 +053015#define AM654_SDHCI_MIN_FREQ 400000
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053016
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053017struct am654_sdhci_plat {
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053018 struct mmc_config cfg;
19 struct mmc mmc;
20 unsigned int f_max;
21};
22
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053023static int am654_sdhci_probe(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053024{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053025 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053026 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 Vutlaaaa449f2018-08-27 15:57:54 +053057 host->max_clk = clock;
58
59 ret = sdhci_setup_cfg(&plat->cfg, host, plat->f_max,
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053060 AM654_SDHCI_MIN_FREQ);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053061 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 Abbas3a1a0df2019-06-11 00:43:31 +053071static int am654_sdhci_ofdata_to_platdata(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053072{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053073 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053074 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 Abbas3a1a0df2019-06-11 00:43:31 +053084static int am654_sdhci_bind(struct udevice *dev)
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053085{
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053086 struct am654_sdhci_plat *plat = dev_get_platdata(dev);
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053087
88 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
89}
90
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053091static const struct udevice_id am654_sdhci_ids[] = {
92 { .compatible = "ti,am654-sdhci-5.1" },
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053093 { }
94};
95
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053096U_BOOT_DRIVER(am654_sdhci_drv) = {
97 .name = "am654_sdhci",
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +053098 .id = UCLASS_MMC,
Faiz Abbas3a1a0df2019-06-11 00:43:31 +053099 .of_match = am654_sdhci_ids,
100 .ofdata_to_platdata = am654_sdhci_ofdata_to_platdata,
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530101 .ops = &sdhci_ops,
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530102 .bind = am654_sdhci_bind,
103 .probe = am654_sdhci_probe,
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530104 .priv_auto_alloc_size = sizeof(struct sdhci_host),
Faiz Abbas3a1a0df2019-06-11 00:43:31 +0530105 .platdata_auto_alloc_size = sizeof(struct am654_sdhci_plat),
Lokesh Vutlaaaa449f2018-08-27 15:57:54 +0530106};