1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Linaro
4  * peter.griffin <peter.griffin@linaro.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <dwmmc.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <malloc.h>
13 #include <asm/global_data.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 struct hi6220_dwmmc_plat {
18 	struct mmc_config cfg;
19 	struct mmc mmc;
20 };
21 
22 struct hi6220_dwmmc_priv_data {
23 	struct dwmci_host host;
24 };
25 
26 struct hisi_mmc_data {
27 	unsigned int clock;
28 	bool use_fifo;
29 };
30 
hi6220_dwmmc_of_to_plat(struct udevice * dev)31 static int hi6220_dwmmc_of_to_plat(struct udevice *dev)
32 {
33 	struct hi6220_dwmmc_priv_data *priv = dev_get_priv(dev);
34 	struct dwmci_host *host = &priv->host;
35 
36 	host->name = dev->name;
37 	host->ioaddr = dev_read_addr_ptr(dev);
38 	host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
39 					"bus-width", 4);
40 
41 	/* use non-removable property for differentiating SD card and eMMC */
42 	if (dev_read_bool(dev, "non-removable"))
43 		host->dev_index = 0;
44 	else
45 		host->dev_index = 1;
46 
47 	host->priv = priv;
48 
49 	return 0;
50 }
51 
hi6220_dwmmc_probe(struct udevice * dev)52 static int hi6220_dwmmc_probe(struct udevice *dev)
53 {
54 	struct hi6220_dwmmc_plat *plat = dev_get_plat(dev);
55 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
56 	struct hi6220_dwmmc_priv_data *priv = dev_get_priv(dev);
57 	struct dwmci_host *host = &priv->host;
58 	struct hisi_mmc_data *mmc_data;
59 
60 	mmc_data = (struct hisi_mmc_data *)dev_get_driver_data(dev);
61 
62 	/* Use default bus speed due to absence of clk driver */
63 	host->bus_hz = mmc_data->clock;
64 
65 	dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000);
66 	host->mmc = &plat->mmc;
67 
68 	host->fifo_mode = mmc_data->use_fifo;
69 	host->mmc->priv = &priv->host;
70 	upriv->mmc = host->mmc;
71 	host->mmc->dev = dev;
72 
73 	return dwmci_probe(dev);
74 }
75 
hi6220_dwmmc_bind(struct udevice * dev)76 static int hi6220_dwmmc_bind(struct udevice *dev)
77 {
78 	struct hi6220_dwmmc_plat *plat = dev_get_plat(dev);
79 	int ret;
80 
81 	ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
82 	if (ret)
83 		return ret;
84 
85 	return 0;
86 }
87 
88 static const struct hisi_mmc_data hi3660_mmc_data = {
89 	.clock = 3200000,
90 	.use_fifo = true,
91 };
92 
93 static const struct hisi_mmc_data hi6220_mmc_data = {
94 	.clock = 50000000,
95 	.use_fifo = false,
96 };
97 
98 static const struct udevice_id hi6220_dwmmc_ids[] = {
99 	{ .compatible = "hisilicon,hi6220-dw-mshc",
100 	  .data = (ulong)&hi6220_mmc_data },
101 	{ .compatible = "hisilicon,hi3798cv200-dw-mshc",
102 	  .data = (ulong)&hi6220_mmc_data },
103 	{ .compatible = "hisilicon,hi3660-dw-mshc",
104 	  .data = (ulong)&hi3660_mmc_data },
105 	{ }
106 };
107 
108 U_BOOT_DRIVER(hi6220_dwmmc_drv) = {
109 	.name = "hi6220_dwmmc",
110 	.id = UCLASS_MMC,
111 	.of_match = hi6220_dwmmc_ids,
112 	.of_to_plat = hi6220_dwmmc_of_to_plat,
113 	.ops = &dm_dwmci_ops,
114 	.bind = hi6220_dwmmc_bind,
115 	.probe = hi6220_dwmmc_probe,
116 	.priv_auto	= sizeof(struct hi6220_dwmmc_priv_data),
117 	.plat_auto	= sizeof(struct hi6220_dwmmc_plat),
118 };
119