blob: 11e44264e47daa2448a8981998cf8390794f6a17 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kuo-Jung Sud123ab82013-05-06 20:32:51 +00002/*
3 * Faraday MMC/SD Host Controller
4 *
5 * (C) Copyright 2010 Faraday Technology
6 * Dante Su <dantesu@faraday-tech.com>
7 *
Rick Chen47ec48b2018-03-20 15:52:58 +08008 * Copyright 2018 Andes Technology, Inc.
9 * Author: Rick Chen (rick@andestech.com)
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000010 */
11
Rick Chen47ec48b2018-03-20 15:52:58 +080012#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000014#include <malloc.h>
15#include <part.h>
16#include <mmc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Rick Chenf7a4adb2017-08-28 16:44:11 +080019#include <linux/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090020#include <linux/errno.h>
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000021#include <asm/byteorder.h>
22#include <faraday/ftsdc010.h>
Rick Chenf7a4adb2017-08-28 16:44:11 +080023#include "ftsdc010_mci.h"
Rick Chen47ec48b2018-03-20 15:52:58 +080024#include <dm.h>
25#include <dt-structs.h>
26#include <errno.h>
27#include <mapmem.h>
28#include <pwrseq.h>
29#include <syscon.h>
30#include <linux/err.h>
31
Sergei Antonov391d0f32022-09-02 10:40:10 +030032#define CFG_CMD_TIMEOUT (CONFIG_SYS_HZ >> 2) /* 250 ms */
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000033#define CFG_RST_TIMEOUT CONFIG_SYS_HZ /* 1 sec reset timeout */
34
Rick Chen47ec48b2018-03-20 15:52:58 +080035#if CONFIG_IS_ENABLED(OF_PLATDATA)
36struct ftsdc010 {
37 fdt32_t bus_width;
38 bool cap_mmc_highspeed;
39 bool cap_sd_highspeed;
40 fdt32_t clock_freq_min_max[2];
41 struct phandle_2_cell clocks[4];
42 fdt32_t fifo_depth;
43 fdt32_t reg[2];
44};
45#endif
46
47struct ftsdc010_plat {
48#if CONFIG_IS_ENABLED(OF_PLATDATA)
49 struct ftsdc010 dtplat;
50#endif
51 struct mmc_config cfg;
52 struct mmc mmc;
53};
54
55struct ftsdc_priv {
56 struct clk clk;
57 struct ftsdc010_chip chip;
58 int fifo_depth;
59 bool fifo_mode;
60 u32 minmax[2];
61};
62
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000063static inline int ftsdc010_send_cmd(struct mmc *mmc, struct mmc_cmd *mmc_cmd)
64{
65 struct ftsdc010_chip *chip = mmc->priv;
66 struct ftsdc010_mmc __iomem *regs = chip->regs;
Jaehoon Chung7825d202016-07-19 16:33:36 +090067 int ret = -ETIMEDOUT;
Kuo-Jung Sud123ab82013-05-06 20:32:51 +000068 uint32_t ts, st;
69 uint32_t cmd = FTSDC010_CMD_IDX(mmc_cmd->cmdidx);
70 uint32_t arg = mmc_cmd->cmdarg;
71 uint32_t flags = mmc_cmd->resp_type;
72
73 cmd |= FTSDC010_CMD_CMD_EN;
74
75 if (chip->acmd) {
76 cmd |= FTSDC010_CMD_APP_CMD;
77 chip->acmd = 0;
78 }
79
80 if (flags & MMC_RSP_PRESENT)
81 cmd |= FTSDC010_CMD_NEED_RSP;
82
83 if (flags & MMC_RSP_136)
84 cmd |= FTSDC010_CMD_LONG_RSP;
85
86 writel(FTSDC010_STATUS_RSP_MASK | FTSDC010_STATUS_CMD_SEND,
87 &regs->clr);
88 writel(arg, &regs->argu);
89 writel(cmd, &regs->cmd);
90
91 if (!(flags & (MMC_RSP_PRESENT | MMC_RSP_136))) {
92 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
93 if (readl(&regs->status) & FTSDC010_STATUS_CMD_SEND) {
94 writel(FTSDC010_STATUS_CMD_SEND, &regs->clr);
95 ret = 0;
96 break;
97 }
98 }
99 } else {
100 st = 0;
101 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
102 st = readl(&regs->status);
103 writel(st & FTSDC010_STATUS_RSP_MASK, &regs->clr);
104 if (st & FTSDC010_STATUS_RSP_MASK)
105 break;
106 }
107 if (st & FTSDC010_STATUS_RSP_CRC_OK) {
108 if (flags & MMC_RSP_136) {
109 mmc_cmd->response[0] = readl(&regs->rsp3);
110 mmc_cmd->response[1] = readl(&regs->rsp2);
111 mmc_cmd->response[2] = readl(&regs->rsp1);
112 mmc_cmd->response[3] = readl(&regs->rsp0);
113 } else {
114 mmc_cmd->response[0] = readl(&regs->rsp0);
115 }
116 ret = 0;
117 } else {
118 debug("ftsdc010: rsp err (cmd=%d, st=0x%x)\n",
119 mmc_cmd->cmdidx, st);
120 }
121 }
122
123 if (ret) {
124 debug("ftsdc010: cmd timeout (op code=%d)\n",
125 mmc_cmd->cmdidx);
126 } else if (mmc_cmd->cmdidx == MMC_CMD_APP_CMD) {
127 chip->acmd = 1;
128 }
129
130 return ret;
131}
132
133static void ftsdc010_clkset(struct mmc *mmc, uint32_t rate)
134{
135 struct ftsdc010_chip *chip = mmc->priv;
136 struct ftsdc010_mmc __iomem *regs = chip->regs;
137 uint32_t div;
138
139 for (div = 0; div < 0x7f; ++div) {
140 if (rate >= chip->sclk / (2 * (div + 1)))
141 break;
142 }
143 chip->rate = chip->sclk / (2 * (div + 1));
144
145 writel(FTSDC010_CCR_CLK_DIV(div), &regs->ccr);
146
147 if (IS_SD(mmc)) {
148 setbits_le32(&regs->ccr, FTSDC010_CCR_CLK_SD);
149
150 if (chip->rate > 25000000)
151 setbits_le32(&regs->ccr, FTSDC010_CCR_CLK_HISPD);
152 else
153 clrbits_le32(&regs->ccr, FTSDC010_CCR_CLK_HISPD);
154 }
155}
156
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000157static int ftsdc010_wait(struct ftsdc010_mmc __iomem *regs, uint32_t mask)
158{
Jaehoon Chung7825d202016-07-19 16:33:36 +0900159 int ret = -ETIMEDOUT;
Rick Chenf7a4adb2017-08-28 16:44:11 +0800160 uint32_t st, timeout = 10000000;
161 while (timeout--) {
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000162 st = readl(&regs->status);
163 if (!(st & mask))
164 continue;
165 writel(st & mask, &regs->clr);
166 ret = 0;
167 break;
168 }
169
Rick Chen22223f02017-08-25 14:02:13 +0800170 if (ret){
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000171 debug("ftsdc010: wait st(0x%x) timeout\n", mask);
Rick Chen22223f02017-08-25 14:02:13 +0800172 }
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000173
174 return ret;
175}
176
177/*
178 * u-boot mmc api
179 */
Rick Chenf7a4adb2017-08-28 16:44:11 +0800180static int ftsdc010_request(struct udevice *dev, struct mmc_cmd *cmd,
181 struct mmc_data *data)
182{
183 struct mmc *mmc = mmc_get_mmc_dev(dev);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900184 int ret = -EOPNOTSUPP;
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000185 uint32_t len = 0;
186 struct ftsdc010_chip *chip = mmc->priv;
187 struct ftsdc010_mmc __iomem *regs = chip->regs;
188
189 if (data && (data->flags & MMC_DATA_WRITE) && chip->wprot) {
190 printf("ftsdc010: the card is write protected!\n");
191 return ret;
192 }
193
194 if (data) {
195 uint32_t dcr;
196
197 len = data->blocksize * data->blocks;
198
199 /* 1. data disable + fifo reset */
Gabor Juhos67a38f12013-05-26 12:11:27 +0200200 dcr = 0;
201#ifdef CONFIG_FTSDC010_SDIO
202 dcr |= FTSDC010_DCR_FIFO_RST;
203#endif
204 writel(dcr, &regs->dcr);
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000205
206 /* 2. clear status register */
207 writel(FTSDC010_STATUS_DATA_MASK | FTSDC010_STATUS_FIFO_URUN
208 | FTSDC010_STATUS_FIFO_ORUN, &regs->clr);
209
210 /* 3. data timeout (1 sec) */
211 writel(chip->rate, &regs->dtr);
212
213 /* 4. data length (bytes) */
214 writel(len, &regs->dlr);
215
216 /* 5. data enable */
217 dcr = (ffs(data->blocksize) - 1) | FTSDC010_DCR_DATA_EN;
218 if (data->flags & MMC_DATA_WRITE)
219 dcr |= FTSDC010_DCR_DATA_WRITE;
220 writel(dcr, &regs->dcr);
221 }
222
223 ret = ftsdc010_send_cmd(mmc, cmd);
224 if (ret) {
225 printf("ftsdc010: CMD%d failed\n", cmd->cmdidx);
226 return ret;
227 }
228
229 if (!data)
230 return ret;
231
232 if (data->flags & MMC_DATA_WRITE) {
233 const uint8_t *buf = (const uint8_t *)data->src;
234
235 while (len > 0) {
236 int wlen;
237
238 /* wait for tx ready */
239 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_URUN);
240 if (ret)
241 break;
242
243 /* write bytes to ftsdc010 */
244 for (wlen = 0; wlen < len && wlen < chip->fifo; ) {
245 writel(*(uint32_t *)buf, &regs->dwr);
246 buf += 4;
247 wlen += 4;
248 }
249
250 len -= wlen;
251 }
252
253 } else {
254 uint8_t *buf = (uint8_t *)data->dest;
255
256 while (len > 0) {
257 int rlen;
258
259 /* wait for rx ready */
260 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_ORUN);
261 if (ret)
262 break;
263
264 /* fetch bytes from ftsdc010 */
265 for (rlen = 0; rlen < len && rlen < chip->fifo; ) {
266 *(uint32_t *)buf = readl(&regs->dwr);
267 buf += 4;
268 rlen += 4;
269 }
270
271 len -= rlen;
272 }
273
274 }
275
276 if (!ret) {
277 ret = ftsdc010_wait(regs,
Rick Chen22223f02017-08-25 14:02:13 +0800278 FTSDC010_STATUS_DATA_END | FTSDC010_STATUS_DATA_CRC_OK);
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000279 }
280
281 return ret;
282}
283
Rick Chenf7a4adb2017-08-28 16:44:11 +0800284static int ftsdc010_set_ios(struct udevice *dev)
285{
286 struct mmc *mmc = mmc_get_mmc_dev(dev);
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000287 struct ftsdc010_chip *chip = mmc->priv;
288 struct ftsdc010_mmc __iomem *regs = chip->regs;
289
290 ftsdc010_clkset(mmc, mmc->clock);
291
292 clrbits_le32(&regs->bwr, FTSDC010_BWR_MODE_MASK);
293 switch (mmc->bus_width) {
294 case 4:
295 setbits_le32(&regs->bwr, FTSDC010_BWR_MODE_4BIT);
296 break;
297 case 8:
298 setbits_le32(&regs->bwr, FTSDC010_BWR_MODE_8BIT);
299 break;
300 default:
301 setbits_le32(&regs->bwr, FTSDC010_BWR_MODE_1BIT);
302 break;
303 }
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900304
305 return 0;
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000306}
307
Rick Chenf7a4adb2017-08-28 16:44:11 +0800308static int ftsdc010_get_cd(struct udevice *dev)
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000309{
Rick Chenf7a4adb2017-08-28 16:44:11 +0800310 struct mmc *mmc = mmc_get_mmc_dev(dev);
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000311 struct ftsdc010_chip *chip = mmc->priv;
312 struct ftsdc010_mmc __iomem *regs = chip->regs;
Rick Chenf7a4adb2017-08-28 16:44:11 +0800313 return !(readl(&regs->status) & FTSDC010_STATUS_CARD_DETECT);
314}
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000315
Rick Chenf7a4adb2017-08-28 16:44:11 +0800316static int ftsdc010_get_wp(struct udevice *dev)
317{
318 struct mmc *mmc = mmc_get_mmc_dev(dev);
Rick Chenf7a4adb2017-08-28 16:44:11 +0800319 struct ftsdc010_chip *chip = mmc->priv;
320 struct ftsdc010_mmc __iomem *regs = chip->regs;
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000321 if (readl(&regs->status) & FTSDC010_STATUS_WRITE_PROT) {
322 printf("ftsdc010: write protected\n");
323 chip->wprot = 1;
324 }
325
Rick Chenf7a4adb2017-08-28 16:44:11 +0800326 return 0;
327}
328
329static int ftsdc010_init(struct mmc *mmc)
330{
331 struct ftsdc010_chip *chip = mmc->priv;
332 struct ftsdc010_mmc __iomem *regs = chip->regs;
333 uint32_t ts;
334
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000335 chip->fifo = (readl(&regs->feature) & 0xff) << 2;
336
337 /* 1. chip reset */
338 writel(FTSDC010_CMD_SDC_RST, &regs->cmd);
339 for (ts = get_timer(0); get_timer(ts) < CFG_RST_TIMEOUT; ) {
340 if (readl(&regs->cmd) & FTSDC010_CMD_SDC_RST)
341 continue;
342 break;
343 }
344 if (readl(&regs->cmd) & FTSDC010_CMD_SDC_RST) {
345 printf("ftsdc010: reset failed\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900346 return -EOPNOTSUPP;
Kuo-Jung Sud123ab82013-05-06 20:32:51 +0000347 }
348
349 /* 2. enter low speed mode (400k card detection) */
350 ftsdc010_clkset(mmc, 400000);
351
352 /* 3. interrupt disabled */
353 writel(0, &regs->int_mask);
354
355 return 0;
356}
357
Rick Chen47ec48b2018-03-20 15:52:58 +0800358static int ftsdc010_probe(struct udevice *dev)
Rick Chenf7a4adb2017-08-28 16:44:11 +0800359{
360 struct mmc *mmc = mmc_get_mmc_dev(dev);
361 return ftsdc010_init(mmc);
362}
363
Rick Chen47ec48b2018-03-20 15:52:58 +0800364const struct dm_mmc_ops dm_ftsdc010_mmc_ops = {
Rick Chenf7a4adb2017-08-28 16:44:11 +0800365 .send_cmd = ftsdc010_request,
366 .set_ios = ftsdc010_set_ios,
367 .get_cd = ftsdc010_get_cd,
368 .get_wp = ftsdc010_get_wp,
369};
Rick Chenf7a4adb2017-08-28 16:44:11 +0800370
Rick Chen47ec48b2018-03-20 15:52:58 +0800371static void ftsdc_setup_cfg(struct mmc_config *cfg, const char *name, int buswidth,
Rick Chenf7a4adb2017-08-28 16:44:11 +0800372 uint caps, u32 max_clk, u32 min_clk)
373{
374 cfg->name = name;
375 cfg->f_min = min_clk;
376 cfg->f_max = max_clk;
377 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
378 cfg->host_caps = caps;
379 if (buswidth == 8) {
380 cfg->host_caps |= MMC_MODE_8BIT;
381 cfg->host_caps &= ~MMC_MODE_4BIT;
382 } else {
383 cfg->host_caps |= MMC_MODE_4BIT;
384 cfg->host_caps &= ~MMC_MODE_8BIT;
385 }
Rick Chenf7a4adb2017-08-28 16:44:11 +0800386 cfg->part_type = PART_TYPE_DOS;
387 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
388}
389
Simon Glassaad29ae2020-12-03 16:55:21 -0700390static int ftsdc010_mmc_of_to_plat(struct udevice *dev)
Rick Chenf7a4adb2017-08-28 16:44:11 +0800391{
Rick Chen47ec48b2018-03-20 15:52:58 +0800392 struct ftsdc_priv *priv = dev_get_priv(dev);
393 struct ftsdc010_chip *chip = &priv->chip;
Simon Glass47f64692021-08-07 07:24:05 -0600394
Simon Glass6d70ba02021-08-07 07:24:06 -0600395 if (CONFIG_IS_ENABLED(OF_REAL)) {
396 chip->name = dev->name;
397 chip->ioaddr = dev_read_addr_ptr(dev);
398 chip->buswidth = dev_read_u32_default(dev, "bus-width", 4);
399 chip->priv = dev;
400 priv->fifo_depth = dev_read_u32_default(dev, "fifo-depth", 0);
401 priv->fifo_mode = dev_read_bool(dev, "fifo-mode");
402 if (dev_read_u32_array(dev, "clock-freq-min-max", priv->minmax, 2)) {
403 if (dev_read_u32(dev, "max-frequency", &priv->minmax[1]))
404 return -EINVAL;
Rick Chen47ec48b2018-03-20 15:52:58 +0800405
Simon Glass6d70ba02021-08-07 07:24:06 -0600406 priv->minmax[0] = 400000; /* 400 kHz */
407 } else {
408 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
409 __func__);
410 }
Rick Chen47ec48b2018-03-20 15:52:58 +0800411 }
Rick Chen47ec48b2018-03-20 15:52:58 +0800412 chip->sclk = priv->minmax[1];
413 chip->regs = chip->ioaddr;
Simon Glass6d70ba02021-08-07 07:24:06 -0600414
Rick Chen47ec48b2018-03-20 15:52:58 +0800415 return 0;
Rick Chenf7a4adb2017-08-28 16:44:11 +0800416}
Rick Chen47ec48b2018-03-20 15:52:58 +0800417
418static int ftsdc010_mmc_probe(struct udevice *dev)
419{
Simon Glassfa20e932020-12-03 16:55:20 -0700420 struct ftsdc010_plat *plat = dev_get_plat(dev);
Rick Chen47ec48b2018-03-20 15:52:58 +0800421 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
422 struct ftsdc_priv *priv = dev_get_priv(dev);
423 struct ftsdc010_chip *chip = &priv->chip;
424 struct udevice *pwr_dev __maybe_unused;
425
426#if CONFIG_IS_ENABLED(OF_PLATDATA)
427 int ret;
428 struct ftsdc010 *dtplat = &plat->dtplat;
429 chip->name = dev->name;
430 chip->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
431 chip->buswidth = dtplat->bus_width;
432 chip->priv = dev;
433 chip->dev_index = 1;
434 memcpy(priv->minmax, dtplat->clock_freq_min_max, sizeof(priv->minmax));
Simon Glass1257efc2021-08-07 07:24:09 -0600435 ret = clk_get_by_phandle(dev, dtplat->clocks, &priv->clk);
Rick Chen47ec48b2018-03-20 15:52:58 +0800436 if (ret < 0)
437 return ret;
438#endif
439
440 if (dev_read_bool(dev, "cap-mmc-highspeed") || \
441 dev_read_bool(dev, "cap-sd-highspeed"))
442 chip->caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
443
444 ftsdc_setup_cfg(&plat->cfg, dev->name, chip->buswidth, chip->caps,
445 priv->minmax[1] , priv->minmax[0]);
446 chip->mmc = &plat->mmc;
447 chip->mmc->priv = &priv->chip;
448 chip->mmc->dev = dev;
449 upriv->mmc = chip->mmc;
450 return ftsdc010_probe(dev);
451}
452
453int ftsdc010_mmc_bind(struct udevice *dev)
454{
Simon Glassfa20e932020-12-03 16:55:20 -0700455 struct ftsdc010_plat *plat = dev_get_plat(dev);
Rick Chen47ec48b2018-03-20 15:52:58 +0800456
457 return mmc_bind(dev, &plat->mmc, &plat->cfg);
458}
459
460static const struct udevice_id ftsdc010_mmc_ids[] = {
Rick Chen7e1861c2018-05-29 11:00:28 +0800461 { .compatible = "andestech,atfsdc010" },
Rick Chen47ec48b2018-03-20 15:52:58 +0800462 { }
463};
464
465U_BOOT_DRIVER(ftsdc010_mmc) = {
466 .name = "ftsdc010_mmc",
467 .id = UCLASS_MMC,
468 .of_match = ftsdc010_mmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700469 .of_to_plat = ftsdc010_mmc_of_to_plat,
Rick Chen47ec48b2018-03-20 15:52:58 +0800470 .ops = &dm_ftsdc010_mmc_ops,
471 .bind = ftsdc010_mmc_bind,
472 .probe = ftsdc010_mmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700473 .priv_auto = sizeof(struct ftsdc_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700474 .plat_auto = sizeof(struct ftsdc010_plat),
Rick Chen47ec48b2018-03-20 15:52:58 +0800475};