blob: dc0210402bd21fad9c8a17ca2eb67d959b01be74 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peter Griffinaf0c1be2015-07-30 18:55:22 +01002/*
3 * (C) Copyright 2015 Linaro
4 * peter.griffin <peter.griffin@linaro.org>
Peter Griffinaf0c1be2015-07-30 18:55:22 +01005 */
6
7#include <common.h>
Yang Xiwen04259472024-02-01 22:05:42 +08008#include <clk.h>
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +05309#include <dm.h>
Peter Griffinaf0c1be2015-07-30 18:55:22 +010010#include <dwmmc.h>
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053011#include <errno.h>
12#include <fdtdec.h>
Peter Griffinaf0c1be2015-07-30 18:55:22 +010013#include <malloc.h>
Yang Xiwen04259472024-02-01 22:05:42 +080014#include <reset.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Yang Xiwen04259472024-02-01 22:05:42 +080016#include <dm/device_compat.h>
Peter Griffinaf0c1be2015-07-30 18:55:22 +010017
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053018DECLARE_GLOBAL_DATA_PTR;
Peter Griffinaf0c1be2015-07-30 18:55:22 +010019
Yang Xiwen04259472024-02-01 22:05:42 +080020enum hi6220_dwmmc_clk_type {
21 HI6220_DWMMC_CLK_BIU,
22 HI6220_DWMMC_CLK_CIU,
23 HI6220_DWMMC_CLK_CNT,
24};
25
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053026struct hi6220_dwmmc_plat {
27 struct mmc_config cfg;
28 struct mmc mmc;
29};
Peter Griffinaf0c1be2015-07-30 18:55:22 +010030
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053031struct hi6220_dwmmc_priv_data {
32 struct dwmci_host host;
Yang Xiwen04259472024-02-01 22:05:42 +080033 struct clk *clks[HI6220_DWMMC_CLK_CNT];
34 struct reset_ctl_bulk rsts;
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053035};
Peter Griffinaf0c1be2015-07-30 18:55:22 +010036
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +053037struct hisi_mmc_data {
38 unsigned int clock;
39 bool use_fifo;
Yang Xiwene52d3c02024-02-01 22:05:44 +080040 u32 fifoth_val;
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +053041};
42
Simon Glassaad29ae2020-12-03 16:55:21 -070043static int hi6220_dwmmc_of_to_plat(struct udevice *dev)
Peter Griffinaf0c1be2015-07-30 18:55:22 +010044{
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053045 struct hi6220_dwmmc_priv_data *priv = dev_get_priv(dev);
46 struct dwmci_host *host = &priv->host;
Yang Xiwen04259472024-02-01 22:05:42 +080047 int ret;
Peter Griffinaf0c1be2015-07-30 18:55:22 +010048
Yang Xiwen04259472024-02-01 22:05:42 +080049 if (CONFIG_IS_ENABLED(CLK) && CONFIG_IS_ENABLED(DM_RESET)) {
50 priv->clks[HI6220_DWMMC_CLK_BIU] = devm_clk_get(dev, "biu");
51 if (IS_ERR(priv->clks[HI6220_DWMMC_CLK_BIU])) {
52 ret = PTR_ERR(priv->clks[HI6220_DWMMC_CLK_BIU]);
53 dev_err(dev, "Failed to get BIU clock(ret = %d).\n", ret);
54 return log_msg_ret("clk", ret);
55 }
56
57 priv->clks[HI6220_DWMMC_CLK_CIU] = devm_clk_get(dev, "ciu");
58 if (IS_ERR(priv->clks[HI6220_DWMMC_CLK_CIU])) {
59 ret = PTR_ERR(priv->clks[HI6220_DWMMC_CLK_CIU]);
60 dev_err(dev, "Failed to get CIU clock(ret = %d).\n", ret);
61 return log_msg_ret("clk", ret);
62 }
63
64 ret = reset_get_bulk(dev, &priv->rsts);
65 if (ret) {
66 dev_err(dev, "Failed to get resets(ret = %d)", ret);
67 return log_msg_ret("rst", ret);
68 }
69 }
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053070 host->name = dev->name;
Masahiro Yamada1096ae12020-07-17 14:36:46 +090071 host->ioaddr = dev_read_addr_ptr(dev);
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053072 host->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
73 "bus-width", 4);
Peter Griffinaf0c1be2015-07-30 18:55:22 +010074
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053075 /* use non-removable property for differentiating SD card and eMMC */
76 if (dev_read_bool(dev, "non-removable"))
77 host->dev_index = 0;
78 else
79 host->dev_index = 1;
80
81 host->priv = priv;
82
Peter Griffinaf0c1be2015-07-30 18:55:22 +010083 return 0;
84}
85
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053086static int hi6220_dwmmc_probe(struct udevice *dev)
Peter Griffinaf0c1be2015-07-30 18:55:22 +010087{
Simon Glassfa20e932020-12-03 16:55:20 -070088 struct hi6220_dwmmc_plat *plat = dev_get_plat(dev);
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053089 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
90 struct hi6220_dwmmc_priv_data *priv = dev_get_priv(dev);
91 struct dwmci_host *host = &priv->host;
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +053092 struct hisi_mmc_data *mmc_data;
Yang Xiwen04259472024-02-01 22:05:42 +080093 int ret;
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +053094
95 mmc_data = (struct hisi_mmc_data *)dev_get_driver_data(dev);
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +053096
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +053097 host->bus_hz = mmc_data->clock;
Yang Xiwen04259472024-02-01 22:05:42 +080098 if (CONFIG_IS_ENABLED(CLK) && CONFIG_IS_ENABLED(DM_RESET)) {
99 ret = clk_prepare_enable(priv->clks[HI6220_DWMMC_CLK_BIU]);
100 if (ret) {
101 dev_err(dev, "Failed to enable biu clock(ret = %d).\n", ret);
102 return log_msg_ret("clk", ret);
103 }
104
105 ret = clk_prepare_enable(priv->clks[HI6220_DWMMC_CLK_CIU]);
106 if (ret) {
107 dev_err(dev, "Failed to enable ciu clock(ret = %d).\n", ret);
108 return log_msg_ret("clk", ret);
109 }
110
111 ret = reset_deassert_bulk(&priv->rsts);
112 if (ret) {
113 dev_err(dev, "Failed to deassert resets(ret = %d).\n", ret);
114 return log_msg_ret("rst", ret);
115 }
116
117 host->bus_hz = clk_get_rate(priv->clks[HI6220_DWMMC_CLK_CIU]);
118 if (host->bus_hz <= 0) {
119 dev_err(dev, "Failed to get ciu clock rate(ret = %d).\n", ret);
120 return log_msg_ret("clk", ret);
121 }
122 }
123 dev_dbg(dev, "bus clock rate: %d.\n", host->bus_hz);
Peter Griffinaf0c1be2015-07-30 18:55:22 +0100124
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530125 dwmci_setup_cfg(&plat->cfg, host, host->bus_hz, 400000);
126 host->mmc = &plat->mmc;
Peter Griffinaf0c1be2015-07-30 18:55:22 +0100127
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +0530128 host->fifo_mode = mmc_data->use_fifo;
Yang Xiwene52d3c02024-02-01 22:05:44 +0800129 host->fifoth_val = mmc_data->fifoth_val;
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530130 host->mmc->priv = &priv->host;
131 upriv->mmc = host->mmc;
132 host->mmc->dev = dev;
Peter Griffinaf0c1be2015-07-30 18:55:22 +0100133
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530134 return dwmci_probe(dev);
Peter Griffinaf0c1be2015-07-30 18:55:22 +0100135}
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530136
137static int hi6220_dwmmc_bind(struct udevice *dev)
138{
Simon Glassfa20e932020-12-03 16:55:20 -0700139 struct hi6220_dwmmc_plat *plat = dev_get_plat(dev);
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530140 int ret;
141
142 ret = dwmci_bind(dev, &plat->mmc, &plat->cfg);
143 if (ret)
144 return ret;
145
146 return 0;
147}
148
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +0530149static const struct hisi_mmc_data hi3660_mmc_data = {
150 .clock = 3200000,
151 .use_fifo = true,
152};
153
154static const struct hisi_mmc_data hi6220_mmc_data = {
155 .clock = 50000000,
156 .use_fifo = false,
157};
158
Yang Xiwene52d3c02024-02-01 22:05:44 +0800159static const struct hisi_mmc_data hi3798mv2x_mmc_data = {
160 .clock = 50000000,
161 .use_fifo = false,
162 // FIFO depth is 256
163 .fifoth_val = MSIZE(4) | RX_WMARK(0x7f) | TX_WMARK(0x80),
164};
165
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530166static const struct udevice_id hi6220_dwmmc_ids[] = {
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +0530167 { .compatible = "hisilicon,hi6220-dw-mshc",
168 .data = (ulong)&hi6220_mmc_data },
169 { .compatible = "hisilicon,hi3798cv200-dw-mshc",
170 .data = (ulong)&hi6220_mmc_data },
Yang Xiwen464c2be2023-04-01 19:17:34 +0800171 { .compatible = "hisilicon,hi3798mv200-dw-mshc",
Yang Xiwene52d3c02024-02-01 22:05:44 +0800172 .data = (ulong)&hi3798mv2x_mmc_data },
Manivannan Sadhasivam759c45a2019-08-02 20:40:10 +0530173 { .compatible = "hisilicon,hi3660-dw-mshc",
174 .data = (ulong)&hi3660_mmc_data },
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530175 { }
176};
177
178U_BOOT_DRIVER(hi6220_dwmmc_drv) = {
179 .name = "hi6220_dwmmc",
180 .id = UCLASS_MMC,
181 .of_match = hi6220_dwmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700182 .of_to_plat = hi6220_dwmmc_of_to_plat,
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530183 .ops = &dm_dwmci_ops,
184 .bind = hi6220_dwmmc_bind,
185 .probe = hi6220_dwmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700186 .priv_auto = sizeof(struct hi6220_dwmmc_priv_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700187 .plat_auto = sizeof(struct hi6220_dwmmc_plat),
Manivannan Sadhasivam6ad4f8c2018-12-27 19:04:04 +0530188};