blob: 5852b24c6d2a5a9d1b1e8a62f8c0ec325b339182 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Carlo Caione20cab782017-04-12 20:30:42 +02002/*
3 * (C) Copyright 2016 Carlo Caione <carlo@caione.org>
Carlo Caione20cab782017-04-12 20:30:42 +02004 */
5
Jerome Brunete6acfa72020-03-05 12:12:36 +01006#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Simon Glass11c89f32017-05-17 17:18:03 -06008#include <dm.h>
Carlo Caione20cab782017-04-12 20:30:42 +02009#include <fdtdec.h>
10#include <malloc.h>
Neil Armstrong1c0ca202019-10-11 17:33:52 +020011#include <pwrseq.h>
Carlo Caione20cab782017-04-12 20:30:42 +020012#include <mmc.h>
13#include <asm/io.h>
Neil Armstrong1c0ca202019-10-11 17:33:52 +020014#include <asm/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Carlo Caione20cab782017-04-12 20:30:42 +020016#include <linux/log2.h>
Neil Armstrong70556d02020-11-11 08:22:09 +090017#include "meson_gx_mmc.h"
Carlo Caione20cab782017-04-12 20:30:42 +020018
Neil Armstrong380fcdd2020-11-11 08:22:10 +090019bool meson_gx_mmc_is_compatible(struct udevice *dev,
20 enum meson_gx_mmc_compatible family)
21{
22 enum meson_gx_mmc_compatible compat = dev_get_driver_data(dev);
23
24 return compat == family;
25}
26
Carlo Caione20cab782017-04-12 20:30:42 +020027static inline void *get_regbase(const struct mmc *mmc)
28{
Simon Glassb75b15b2020-12-03 16:55:23 -070029 struct meson_mmc_plat *pdata = mmc->priv;
Carlo Caione20cab782017-04-12 20:30:42 +020030
31 return pdata->regbase;
32}
33
34static inline uint32_t meson_read(struct mmc *mmc, int offset)
35{
36 return readl(get_regbase(mmc) + offset);
37}
38
39static inline void meson_write(struct mmc *mmc, uint32_t val, int offset)
40{
41 writel(val, get_regbase(mmc) + offset);
42}
43
44static void meson_mmc_config_clock(struct mmc *mmc)
45{
46 uint32_t meson_mmc_clk = 0;
47 unsigned int clk, clk_src, clk_div;
48
Heinrich Schuchardt127c8b12018-03-17 22:49:36 +000049 if (!mmc->clock)
50 return;
51
Neil Armstrong380fcdd2020-11-11 08:22:10 +090052 /* TOFIX This should use the proper clock taken from DT */
53
Carlo Caione20cab782017-04-12 20:30:42 +020054 /* 1GHz / CLK_MAX_DIV = 15,9 MHz */
55 if (mmc->clock > 16000000) {
56 clk = SD_EMMC_CLKSRC_DIV2;
57 clk_src = CLK_SRC_DIV2;
58 } else {
59 clk = SD_EMMC_CLKSRC_24M;
60 clk_src = CLK_SRC_24M;
61 }
62 clk_div = DIV_ROUND_UP(clk, mmc->clock);
63
Neil Armstrong380fcdd2020-11-11 08:22:10 +090064 /*
65 * SM1 SoCs doesn't work fine over 50MHz with CLK_CO_PHASE_180
66 * If CLK_CO_PHASE_270 is used, it's more stable than other.
67 * Other SoCs use CLK_CO_PHASE_180 by default.
68 * It needs to find what is a proper value about each SoCs.
69 */
70 if (meson_gx_mmc_is_compatible(mmc->dev, MMC_COMPATIBLE_SM1))
71 meson_mmc_clk |= CLK_CO_PHASE_270;
72 else
73 meson_mmc_clk |= CLK_CO_PHASE_180;
Carlo Caione20cab782017-04-12 20:30:42 +020074
75 /* 180 phase tx clock */
76 meson_mmc_clk |= CLK_TX_PHASE_000;
77
78 /* clock settings */
79 meson_mmc_clk |= clk_src;
80 meson_mmc_clk |= clk_div;
81
82 meson_write(mmc, meson_mmc_clk, MESON_SD_EMMC_CLOCK);
83}
84
85static int meson_dm_mmc_set_ios(struct udevice *dev)
86{
87 struct mmc *mmc = mmc_get_mmc_dev(dev);
88 uint32_t meson_mmc_cfg;
89
90 meson_mmc_config_clock(mmc);
91
92 meson_mmc_cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
93
94 meson_mmc_cfg &= ~CFG_BUS_WIDTH_MASK;
95 if (mmc->bus_width == 1)
96 meson_mmc_cfg |= CFG_BUS_WIDTH_1;
97 else if (mmc->bus_width == 4)
98 meson_mmc_cfg |= CFG_BUS_WIDTH_4;
99 else if (mmc->bus_width == 8)
100 meson_mmc_cfg |= CFG_BUS_WIDTH_8;
101 else
102 return -EINVAL;
103
104 /* 512 bytes block length */
105 meson_mmc_cfg &= ~CFG_BL_LEN_MASK;
106 meson_mmc_cfg |= CFG_BL_LEN_512;
107
108 /* Response timeout 256 clk */
109 meson_mmc_cfg &= ~CFG_RESP_TIMEOUT_MASK;
110 meson_mmc_cfg |= CFG_RESP_TIMEOUT_256;
111
112 /* Command-command gap 16 clk */
113 meson_mmc_cfg &= ~CFG_RC_CC_MASK;
114 meson_mmc_cfg |= CFG_RC_CC_16;
115
116 meson_write(mmc, meson_mmc_cfg, MESON_SD_EMMC_CFG);
117
118 return 0;
119}
120
121static void meson_mmc_setup_cmd(struct mmc *mmc, struct mmc_data *data,
122 struct mmc_cmd *cmd)
123{
124 uint32_t meson_mmc_cmd = 0, cfg;
125
126 meson_mmc_cmd |= cmd->cmdidx << CMD_CFG_CMD_INDEX_SHIFT;
127
128 if (cmd->resp_type & MMC_RSP_PRESENT) {
129 if (cmd->resp_type & MMC_RSP_136)
130 meson_mmc_cmd |= CMD_CFG_RESP_128;
131
132 if (cmd->resp_type & MMC_RSP_BUSY)
133 meson_mmc_cmd |= CMD_CFG_R1B;
134
135 if (!(cmd->resp_type & MMC_RSP_CRC))
136 meson_mmc_cmd |= CMD_CFG_RESP_NOCRC;
137 } else {
138 meson_mmc_cmd |= CMD_CFG_NO_RESP;
139 }
140
141 if (data) {
142 cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
143 cfg &= ~CFG_BL_LEN_MASK;
144 cfg |= ilog2(data->blocksize) << CFG_BL_LEN_SHIFT;
145 meson_write(mmc, cfg, MESON_SD_EMMC_CFG);
146
147 if (data->flags == MMC_DATA_WRITE)
148 meson_mmc_cmd |= CMD_CFG_DATA_WR;
149
150 meson_mmc_cmd |= CMD_CFG_DATA_IO | CMD_CFG_BLOCK_MODE |
151 data->blocks;
152 }
153
154 meson_mmc_cmd |= CMD_CFG_TIMEOUT_4S | CMD_CFG_OWNER |
155 CMD_CFG_END_OF_CHAIN;
156
157 meson_write(mmc, meson_mmc_cmd, MESON_SD_EMMC_CMD_CFG);
158}
159
160static void meson_mmc_setup_addr(struct mmc *mmc, struct mmc_data *data)
161{
Simon Glassb75b15b2020-12-03 16:55:23 -0700162 struct meson_mmc_plat *pdata = mmc->priv;
Carlo Caione20cab782017-04-12 20:30:42 +0200163 unsigned int data_size;
164 uint32_t data_addr = 0;
165
166 if (data) {
167 data_size = data->blocks * data->blocksize;
168
169 if (data->flags == MMC_DATA_READ) {
170 data_addr = (ulong) data->dest;
171 invalidate_dcache_range(data_addr,
172 data_addr + data_size);
173 } else {
174 pdata->w_buf = calloc(data_size, sizeof(char));
175 data_addr = (ulong) pdata->w_buf;
176 memcpy(pdata->w_buf, data->src, data_size);
177 flush_dcache_range(data_addr, data_addr + data_size);
178 }
179 }
180
181 meson_write(mmc, data_addr, MESON_SD_EMMC_CMD_DAT);
182}
183
184static void meson_mmc_read_response(struct mmc *mmc, struct mmc_cmd *cmd)
185{
186 if (cmd->resp_type & MMC_RSP_136) {
187 cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP3);
188 cmd->response[1] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP2);
189 cmd->response[2] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP1);
190 cmd->response[3] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
191 } else {
192 cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
193 }
194}
195
196static int meson_dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
197 struct mmc_data *data)
198{
199 struct mmc *mmc = mmc_get_mmc_dev(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700200 struct meson_mmc_plat *pdata = mmc->priv;
Carlo Caione20cab782017-04-12 20:30:42 +0200201 uint32_t status;
202 ulong start;
203 int ret = 0;
204
205 /* max block size supported by chip is 512 byte */
206 if (data && data->blocksize > 512)
207 return -EINVAL;
208
209 meson_mmc_setup_cmd(mmc, data, cmd);
210 meson_mmc_setup_addr(mmc, data);
211
212 meson_write(mmc, cmd->cmdarg, MESON_SD_EMMC_CMD_ARG);
213
214 /* use 10s timeout */
215 start = get_timer(0);
216 do {
217 status = meson_read(mmc, MESON_SD_EMMC_STATUS);
218 } while(!(status & STATUS_END_OF_CHAIN) && get_timer(start) < 10000);
219
220 if (!(status & STATUS_END_OF_CHAIN))
221 ret = -ETIMEDOUT;
222 else if (status & STATUS_RESP_TIMEOUT)
223 ret = -ETIMEDOUT;
224 else if (status & STATUS_ERR_MASK)
225 ret = -EIO;
226
227 meson_mmc_read_response(mmc, cmd);
228
229 if (data && data->flags == MMC_DATA_WRITE)
230 free(pdata->w_buf);
231
232 /* reset status bits */
233 meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
234
235 return ret;
236}
237
238static const struct dm_mmc_ops meson_dm_mmc_ops = {
239 .send_cmd = meson_dm_mmc_send_cmd,
240 .set_ios = meson_dm_mmc_set_ios,
241};
242
Simon Glassaad29ae2020-12-03 16:55:21 -0700243static int meson_mmc_of_to_plat(struct udevice *dev)
Carlo Caione20cab782017-04-12 20:30:42 +0200244{
Simon Glassb75b15b2020-12-03 16:55:23 -0700245 struct meson_mmc_plat *pdata = dev_get_plat(dev);
Carlo Caione20cab782017-04-12 20:30:42 +0200246 fdt_addr_t addr;
247
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900248 addr = dev_read_addr(dev);
Carlo Caione20cab782017-04-12 20:30:42 +0200249 if (addr == FDT_ADDR_T_NONE)
250 return -EINVAL;
251
252 pdata->regbase = (void *)addr;
253
254 return 0;
255}
256
257static int meson_mmc_probe(struct udevice *dev)
258{
Simon Glassb75b15b2020-12-03 16:55:23 -0700259 struct meson_mmc_plat *pdata = dev_get_plat(dev);
Carlo Caione20cab782017-04-12 20:30:42 +0200260 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
261 struct mmc *mmc = &pdata->mmc;
262 struct mmc_config *cfg = &pdata->cfg;
Jerome Brunete6acfa72020-03-05 12:12:36 +0100263 struct clk_bulk clocks;
Carlo Caione20cab782017-04-12 20:30:42 +0200264 uint32_t val;
Jerome Brunete6acfa72020-03-05 12:12:36 +0100265 int ret;
266
Jerome Brunete6acfa72020-03-05 12:12:36 +0100267 /* Enable the clocks feeding the MMC controller */
268 ret = clk_get_bulk(dev, &clocks);
269 if (ret)
270 return ret;
271
272 ret = clk_enable_bulk(&clocks);
273 if (ret)
274 return ret;
275
Carlo Caione20cab782017-04-12 20:30:42 +0200276 cfg->voltages = MMC_VDD_33_34 | MMC_VDD_32_33 |
277 MMC_VDD_31_32 | MMC_VDD_165_195;
278 cfg->host_caps = MMC_MODE_8BIT | MMC_MODE_4BIT |
279 MMC_MODE_HS_52MHz | MMC_MODE_HS;
280 cfg->f_min = DIV_ROUND_UP(SD_EMMC_CLKSRC_24M, CLK_MAX_DIV);
281 cfg->f_max = 100000000; /* 100 MHz */
Heiner Kallweit3515c172017-04-14 10:10:19 +0200282 cfg->b_max = 511; /* max 512 - 1 blocks */
Carlo Caione20cab782017-04-12 20:30:42 +0200283 cfg->name = dev->name;
284
285 mmc->priv = pdata;
286 upriv->mmc = mmc;
287
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900288 mmc_set_clock(mmc, cfg->f_min, MMC_CLK_ENABLE);
Carlo Caione20cab782017-04-12 20:30:42 +0200289
Jonas Karlmanf2ceb752024-01-27 17:12:35 +0000290#if CONFIG_IS_ENABLED(MMC_PWRSEQ)
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200291 /* Enable power if needed */
Jaehoon Chungf780e132021-02-16 10:16:53 +0900292 ret = mmc_pwrseq_get_power(dev, cfg);
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200293 if (!ret) {
Jaehoon Chungf780e132021-02-16 10:16:53 +0900294 ret = pwrseq_set_power(cfg->pwr_dev, true);
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200295 if (ret)
296 return ret;
297 }
298#endif
299
Carlo Caione20cab782017-04-12 20:30:42 +0200300 /* reset all status bits */
301 meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
302
303 /* disable interrupts */
304 meson_write(mmc, 0, MESON_SD_EMMC_IRQ_EN);
305
306 /* enable auto clock mode */
307 val = meson_read(mmc, MESON_SD_EMMC_CFG);
308 val &= ~CFG_SDCLK_ALWAYS_ON;
309 val |= CFG_AUTO_CLK;
310 meson_write(mmc, val, MESON_SD_EMMC_CFG);
311
312 return 0;
313}
314
315int meson_mmc_bind(struct udevice *dev)
316{
Simon Glassb75b15b2020-12-03 16:55:23 -0700317 struct meson_mmc_plat *pdata = dev_get_plat(dev);
Carlo Caione20cab782017-04-12 20:30:42 +0200318
319 return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
320}
321
322static const struct udevice_id meson_mmc_match[] = {
Neil Armstrong380fcdd2020-11-11 08:22:10 +0900323 { .compatible = "amlogic,meson-gx-mmc", .data = MMC_COMPATIBLE_GX },
324 { .compatible = "amlogic,meson-axg-mmc", .data = MMC_COMPATIBLE_GX },
325 { .compatible = "amlogic,meson-sm1-mmc", .data = MMC_COMPATIBLE_SM1 },
Carlo Caione20cab782017-04-12 20:30:42 +0200326 { /* sentinel */ }
327};
328
329U_BOOT_DRIVER(meson_mmc) = {
330 .name = "meson_gx_mmc",
331 .id = UCLASS_MMC,
332 .of_match = meson_mmc_match,
333 .ops = &meson_dm_mmc_ops,
334 .probe = meson_mmc_probe,
335 .bind = meson_mmc_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700336 .of_to_plat = meson_mmc_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700337 .plat_auto = sizeof(struct meson_mmc_plat),
Carlo Caione20cab782017-04-12 20:30:42 +0200338};