blob: b5f5122b1b7bb91220bd0010c4f3b5a0cb95648d [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
6#include <common.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>
Carlo Caione20cab782017-04-12 20:30:42 +020015#include <asm/arch/sd_emmc.h>
Carlo Caione20cab782017-04-12 20:30:42 +020016#include <linux/log2.h>
17
18static inline void *get_regbase(const struct mmc *mmc)
19{
20 struct meson_mmc_platdata *pdata = mmc->priv;
21
22 return pdata->regbase;
23}
24
25static inline uint32_t meson_read(struct mmc *mmc, int offset)
26{
27 return readl(get_regbase(mmc) + offset);
28}
29
30static inline void meson_write(struct mmc *mmc, uint32_t val, int offset)
31{
32 writel(val, get_regbase(mmc) + offset);
33}
34
35static void meson_mmc_config_clock(struct mmc *mmc)
36{
37 uint32_t meson_mmc_clk = 0;
38 unsigned int clk, clk_src, clk_div;
39
Heinrich Schuchardt127c8b12018-03-17 22:49:36 +000040 if (!mmc->clock)
41 return;
42
Carlo Caione20cab782017-04-12 20:30:42 +020043 /* 1GHz / CLK_MAX_DIV = 15,9 MHz */
44 if (mmc->clock > 16000000) {
45 clk = SD_EMMC_CLKSRC_DIV2;
46 clk_src = CLK_SRC_DIV2;
47 } else {
48 clk = SD_EMMC_CLKSRC_24M;
49 clk_src = CLK_SRC_24M;
50 }
51 clk_div = DIV_ROUND_UP(clk, mmc->clock);
52
53 /* 180 phase core clock */
54 meson_mmc_clk |= CLK_CO_PHASE_180;
55
56 /* 180 phase tx clock */
57 meson_mmc_clk |= CLK_TX_PHASE_000;
58
59 /* clock settings */
60 meson_mmc_clk |= clk_src;
61 meson_mmc_clk |= clk_div;
62
63 meson_write(mmc, meson_mmc_clk, MESON_SD_EMMC_CLOCK);
64}
65
66static int meson_dm_mmc_set_ios(struct udevice *dev)
67{
68 struct mmc *mmc = mmc_get_mmc_dev(dev);
69 uint32_t meson_mmc_cfg;
70
71 meson_mmc_config_clock(mmc);
72
73 meson_mmc_cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
74
75 meson_mmc_cfg &= ~CFG_BUS_WIDTH_MASK;
76 if (mmc->bus_width == 1)
77 meson_mmc_cfg |= CFG_BUS_WIDTH_1;
78 else if (mmc->bus_width == 4)
79 meson_mmc_cfg |= CFG_BUS_WIDTH_4;
80 else if (mmc->bus_width == 8)
81 meson_mmc_cfg |= CFG_BUS_WIDTH_8;
82 else
83 return -EINVAL;
84
85 /* 512 bytes block length */
86 meson_mmc_cfg &= ~CFG_BL_LEN_MASK;
87 meson_mmc_cfg |= CFG_BL_LEN_512;
88
89 /* Response timeout 256 clk */
90 meson_mmc_cfg &= ~CFG_RESP_TIMEOUT_MASK;
91 meson_mmc_cfg |= CFG_RESP_TIMEOUT_256;
92
93 /* Command-command gap 16 clk */
94 meson_mmc_cfg &= ~CFG_RC_CC_MASK;
95 meson_mmc_cfg |= CFG_RC_CC_16;
96
97 meson_write(mmc, meson_mmc_cfg, MESON_SD_EMMC_CFG);
98
99 return 0;
100}
101
102static void meson_mmc_setup_cmd(struct mmc *mmc, struct mmc_data *data,
103 struct mmc_cmd *cmd)
104{
105 uint32_t meson_mmc_cmd = 0, cfg;
106
107 meson_mmc_cmd |= cmd->cmdidx << CMD_CFG_CMD_INDEX_SHIFT;
108
109 if (cmd->resp_type & MMC_RSP_PRESENT) {
110 if (cmd->resp_type & MMC_RSP_136)
111 meson_mmc_cmd |= CMD_CFG_RESP_128;
112
113 if (cmd->resp_type & MMC_RSP_BUSY)
114 meson_mmc_cmd |= CMD_CFG_R1B;
115
116 if (!(cmd->resp_type & MMC_RSP_CRC))
117 meson_mmc_cmd |= CMD_CFG_RESP_NOCRC;
118 } else {
119 meson_mmc_cmd |= CMD_CFG_NO_RESP;
120 }
121
122 if (data) {
123 cfg = meson_read(mmc, MESON_SD_EMMC_CFG);
124 cfg &= ~CFG_BL_LEN_MASK;
125 cfg |= ilog2(data->blocksize) << CFG_BL_LEN_SHIFT;
126 meson_write(mmc, cfg, MESON_SD_EMMC_CFG);
127
128 if (data->flags == MMC_DATA_WRITE)
129 meson_mmc_cmd |= CMD_CFG_DATA_WR;
130
131 meson_mmc_cmd |= CMD_CFG_DATA_IO | CMD_CFG_BLOCK_MODE |
132 data->blocks;
133 }
134
135 meson_mmc_cmd |= CMD_CFG_TIMEOUT_4S | CMD_CFG_OWNER |
136 CMD_CFG_END_OF_CHAIN;
137
138 meson_write(mmc, meson_mmc_cmd, MESON_SD_EMMC_CMD_CFG);
139}
140
141static void meson_mmc_setup_addr(struct mmc *mmc, struct mmc_data *data)
142{
143 struct meson_mmc_platdata *pdata = mmc->priv;
144 unsigned int data_size;
145 uint32_t data_addr = 0;
146
147 if (data) {
148 data_size = data->blocks * data->blocksize;
149
150 if (data->flags == MMC_DATA_READ) {
151 data_addr = (ulong) data->dest;
152 invalidate_dcache_range(data_addr,
153 data_addr + data_size);
154 } else {
155 pdata->w_buf = calloc(data_size, sizeof(char));
156 data_addr = (ulong) pdata->w_buf;
157 memcpy(pdata->w_buf, data->src, data_size);
158 flush_dcache_range(data_addr, data_addr + data_size);
159 }
160 }
161
162 meson_write(mmc, data_addr, MESON_SD_EMMC_CMD_DAT);
163}
164
165static void meson_mmc_read_response(struct mmc *mmc, struct mmc_cmd *cmd)
166{
167 if (cmd->resp_type & MMC_RSP_136) {
168 cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP3);
169 cmd->response[1] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP2);
170 cmd->response[2] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP1);
171 cmd->response[3] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
172 } else {
173 cmd->response[0] = meson_read(mmc, MESON_SD_EMMC_CMD_RSP);
174 }
175}
176
177static int meson_dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
178 struct mmc_data *data)
179{
180 struct mmc *mmc = mmc_get_mmc_dev(dev);
181 struct meson_mmc_platdata *pdata = mmc->priv;
182 uint32_t status;
183 ulong start;
184 int ret = 0;
185
186 /* max block size supported by chip is 512 byte */
187 if (data && data->blocksize > 512)
188 return -EINVAL;
189
190 meson_mmc_setup_cmd(mmc, data, cmd);
191 meson_mmc_setup_addr(mmc, data);
192
193 meson_write(mmc, cmd->cmdarg, MESON_SD_EMMC_CMD_ARG);
194
195 /* use 10s timeout */
196 start = get_timer(0);
197 do {
198 status = meson_read(mmc, MESON_SD_EMMC_STATUS);
199 } while(!(status & STATUS_END_OF_CHAIN) && get_timer(start) < 10000);
200
201 if (!(status & STATUS_END_OF_CHAIN))
202 ret = -ETIMEDOUT;
203 else if (status & STATUS_RESP_TIMEOUT)
204 ret = -ETIMEDOUT;
205 else if (status & STATUS_ERR_MASK)
206 ret = -EIO;
207
208 meson_mmc_read_response(mmc, cmd);
209
210 if (data && data->flags == MMC_DATA_WRITE)
211 free(pdata->w_buf);
212
213 /* reset status bits */
214 meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
215
216 return ret;
217}
218
219static const struct dm_mmc_ops meson_dm_mmc_ops = {
220 .send_cmd = meson_dm_mmc_send_cmd,
221 .set_ios = meson_dm_mmc_set_ios,
222};
223
224static int meson_mmc_ofdata_to_platdata(struct udevice *dev)
225{
226 struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
227 fdt_addr_t addr;
228
Simon Glassba1dea42017-05-17 17:18:05 -0600229 addr = devfdt_get_addr(dev);
Carlo Caione20cab782017-04-12 20:30:42 +0200230 if (addr == FDT_ADDR_T_NONE)
231 return -EINVAL;
232
233 pdata->regbase = (void *)addr;
234
235 return 0;
236}
237
238static int meson_mmc_probe(struct udevice *dev)
239{
240 struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
241 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
242 struct mmc *mmc = &pdata->mmc;
243 struct mmc_config *cfg = &pdata->cfg;
244 uint32_t val;
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200245#ifdef CONFIG_PWRSEQ
246 struct udevice *pwr_dev;
247 int ret;
248#endif
Carlo Caione20cab782017-04-12 20:30:42 +0200249
250 cfg->voltages = MMC_VDD_33_34 | MMC_VDD_32_33 |
251 MMC_VDD_31_32 | MMC_VDD_165_195;
252 cfg->host_caps = MMC_MODE_8BIT | MMC_MODE_4BIT |
253 MMC_MODE_HS_52MHz | MMC_MODE_HS;
254 cfg->f_min = DIV_ROUND_UP(SD_EMMC_CLKSRC_24M, CLK_MAX_DIV);
255 cfg->f_max = 100000000; /* 100 MHz */
Heiner Kallweit3515c172017-04-14 10:10:19 +0200256 cfg->b_max = 511; /* max 512 - 1 blocks */
Carlo Caione20cab782017-04-12 20:30:42 +0200257 cfg->name = dev->name;
258
259 mmc->priv = pdata;
260 upriv->mmc = mmc;
261
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900262 mmc_set_clock(mmc, cfg->f_min, MMC_CLK_ENABLE);
Carlo Caione20cab782017-04-12 20:30:42 +0200263
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200264#ifdef CONFIG_PWRSEQ
265 /* Enable power if needed */
266 ret = uclass_get_device_by_phandle(UCLASS_PWRSEQ, dev, "mmc-pwrseq",
267 &pwr_dev);
268 if (!ret) {
269 ret = pwrseq_set_power(pwr_dev, true);
270 if (ret)
271 return ret;
272 }
273#endif
274
Carlo Caione20cab782017-04-12 20:30:42 +0200275 /* reset all status bits */
276 meson_write(mmc, STATUS_MASK, MESON_SD_EMMC_STATUS);
277
278 /* disable interrupts */
279 meson_write(mmc, 0, MESON_SD_EMMC_IRQ_EN);
280
281 /* enable auto clock mode */
282 val = meson_read(mmc, MESON_SD_EMMC_CFG);
283 val &= ~CFG_SDCLK_ALWAYS_ON;
284 val |= CFG_AUTO_CLK;
285 meson_write(mmc, val, MESON_SD_EMMC_CFG);
286
287 return 0;
288}
289
290int meson_mmc_bind(struct udevice *dev)
291{
292 struct meson_mmc_platdata *pdata = dev_get_platdata(dev);
293
294 return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
295}
296
297static const struct udevice_id meson_mmc_match[] = {
298 { .compatible = "amlogic,meson-gx-mmc" },
Neil Armstrongbd373e92018-09-10 16:43:46 +0200299 { .compatible = "amlogic,meson-axg-mmc" },
Carlo Caione20cab782017-04-12 20:30:42 +0200300 { /* sentinel */ }
301};
302
303U_BOOT_DRIVER(meson_mmc) = {
304 .name = "meson_gx_mmc",
305 .id = UCLASS_MMC,
306 .of_match = meson_mmc_match,
307 .ops = &meson_dm_mmc_ops,
308 .probe = meson_mmc_probe,
309 .bind = meson_mmc_bind,
310 .ofdata_to_platdata = meson_mmc_ofdata_to_platdata,
311 .platdata_auto_alloc_size = sizeof(struct meson_mmc_platdata),
312};
Neil Armstrong1c0ca202019-10-11 17:33:52 +0200313
314#ifdef CONFIG_PWRSEQ
315static int meson_mmc_pwrseq_set_power(struct udevice *dev, bool enable)
316{
317 struct gpio_desc reset;
318 int ret;
319
320 ret = gpio_request_by_name(dev, "reset-gpios", 0, &reset, GPIOD_IS_OUT);
321 if (ret)
322 return ret;
323 dm_gpio_set_value(&reset, 1);
324 udelay(1);
325 dm_gpio_set_value(&reset, 0);
326 udelay(200);
327
328 return 0;
329}
330
331static const struct pwrseq_ops meson_mmc_pwrseq_ops = {
332 .set_power = meson_mmc_pwrseq_set_power,
333};
334
335static const struct udevice_id meson_mmc_pwrseq_ids[] = {
336 { .compatible = "mmc-pwrseq-emmc" },
337 { }
338};
339
340U_BOOT_DRIVER(meson_mmc_pwrseq_drv) = {
341 .name = "mmc_pwrseq_emmc",
342 .id = UCLASS_PWRSEQ,
343 .of_match = meson_mmc_pwrseq_ids,
344 .ops = &meson_mmc_pwrseq_ops,
345};
346#endif