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