1 // SPDX-License-Identifier: GPL-2.0+
3 * Faraday MMC/SD Host Controller
5 * (C) Copyright 2010 Faraday Technology
6 * Dante Su <dantesu@faraday-tech.com>
8 * Copyright 2018 Andes Technology, Inc.
9 * Author: Rick Chen (rick@andestech.com)
18 #include <linux/errno.h>
19 #include <asm/byteorder.h>
20 #include <faraday/ftsdc010.h>
21 #include "ftsdc010_mci.h"
23 #include <dt-structs.h>
28 #include <linux/err.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 #define CFG_CMD_TIMEOUT (CONFIG_SYS_HZ >> 4) /* 250 ms */
33 #define CFG_RST_TIMEOUT CONFIG_SYS_HZ /* 1 sec reset timeout */
35 #if CONFIG_IS_ENABLED(OF_PLATDATA)
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];
47 struct ftsdc010_plat {
48 #if CONFIG_IS_ENABLED(OF_PLATDATA)
49 struct ftsdc010 dtplat;
51 struct mmc_config cfg;
57 struct ftsdc010_chip chip;
63 static inline int ftsdc010_send_cmd(struct mmc *mmc, struct mmc_cmd *mmc_cmd)
65 struct ftsdc010_chip *chip = mmc->priv;
66 struct ftsdc010_mmc __iomem *regs = chip->regs;
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;
73 cmd |= FTSDC010_CMD_CMD_EN;
76 cmd |= FTSDC010_CMD_APP_CMD;
80 if (flags & MMC_RSP_PRESENT)
81 cmd |= FTSDC010_CMD_NEED_RSP;
83 if (flags & MMC_RSP_136)
84 cmd |= FTSDC010_CMD_LONG_RSP;
86 writel(FTSDC010_STATUS_RSP_MASK | FTSDC010_STATUS_CMD_SEND,
88 writel(arg, ®s->argu);
89 writel(cmd, ®s->cmd);
91 if (!(flags & (MMC_RSP_PRESENT | MMC_RSP_136))) {
92 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
93 if (readl(®s->status) & FTSDC010_STATUS_CMD_SEND) {
94 writel(FTSDC010_STATUS_CMD_SEND, ®s->clr);
101 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
102 st = readl(®s->status);
103 writel(st & FTSDC010_STATUS_RSP_MASK, ®s->clr);
104 if (st & FTSDC010_STATUS_RSP_MASK)
107 if (st & FTSDC010_STATUS_RSP_CRC_OK) {
108 if (flags & MMC_RSP_136) {
109 mmc_cmd->response[0] = readl(®s->rsp3);
110 mmc_cmd->response[1] = readl(®s->rsp2);
111 mmc_cmd->response[2] = readl(®s->rsp1);
112 mmc_cmd->response[3] = readl(®s->rsp0);
114 mmc_cmd->response[0] = readl(®s->rsp0);
118 debug("ftsdc010: rsp err (cmd=%d, st=0x%x)\n",
119 mmc_cmd->cmdidx, st);
124 debug("ftsdc010: cmd timeout (op code=%d)\n",
126 } else if (mmc_cmd->cmdidx == MMC_CMD_APP_CMD) {
133 static void ftsdc010_clkset(struct mmc *mmc, uint32_t rate)
135 struct ftsdc010_chip *chip = mmc->priv;
136 struct ftsdc010_mmc __iomem *regs = chip->regs;
139 for (div = 0; div < 0x7f; ++div) {
140 if (rate >= chip->sclk / (2 * (div + 1)))
143 chip->rate = chip->sclk / (2 * (div + 1));
145 writel(FTSDC010_CCR_CLK_DIV(div), ®s->ccr);
148 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_SD);
150 if (chip->rate > 25000000)
151 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
153 clrbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
157 static int ftsdc010_wait(struct ftsdc010_mmc __iomem *regs, uint32_t mask)
159 int ret = -ETIMEDOUT;
160 uint32_t st, timeout = 10000000;
162 st = readl(®s->status);
165 writel(st & mask, ®s->clr);
171 debug("ftsdc010: wait st(0x%x) timeout\n", mask);
180 static int ftsdc010_request(struct udevice *dev, struct mmc_cmd *cmd,
181 struct mmc_data *data)
183 struct mmc *mmc = mmc_get_mmc_dev(dev);
184 int ret = -EOPNOTSUPP;
186 struct ftsdc010_chip *chip = mmc->priv;
187 struct ftsdc010_mmc __iomem *regs = chip->regs;
189 if (data && (data->flags & MMC_DATA_WRITE) && chip->wprot) {
190 printf("ftsdc010: the card is write protected!\n");
197 len = data->blocksize * data->blocks;
199 /* 1. data disable + fifo reset */
201 #ifdef CONFIG_FTSDC010_SDIO
202 dcr |= FTSDC010_DCR_FIFO_RST;
204 writel(dcr, ®s->dcr);
206 /* 2. clear status register */
207 writel(FTSDC010_STATUS_DATA_MASK | FTSDC010_STATUS_FIFO_URUN
208 | FTSDC010_STATUS_FIFO_ORUN, ®s->clr);
210 /* 3. data timeout (1 sec) */
211 writel(chip->rate, ®s->dtr);
213 /* 4. data length (bytes) */
214 writel(len, ®s->dlr);
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, ®s->dcr);
223 ret = ftsdc010_send_cmd(mmc, cmd);
225 printf("ftsdc010: CMD%d failed\n", cmd->cmdidx);
232 if (data->flags & MMC_DATA_WRITE) {
233 const uint8_t *buf = (const uint8_t *)data->src;
238 /* wait for tx ready */
239 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_URUN);
243 /* write bytes to ftsdc010 */
244 for (wlen = 0; wlen < len && wlen < chip->fifo; ) {
245 writel(*(uint32_t *)buf, ®s->dwr);
254 uint8_t *buf = (uint8_t *)data->dest;
259 /* wait for rx ready */
260 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_ORUN);
264 /* fetch bytes from ftsdc010 */
265 for (rlen = 0; rlen < len && rlen < chip->fifo; ) {
266 *(uint32_t *)buf = readl(®s->dwr);
277 ret = ftsdc010_wait(regs,
278 FTSDC010_STATUS_DATA_END | FTSDC010_STATUS_DATA_CRC_OK);
284 static int ftsdc010_set_ios(struct udevice *dev)
286 struct mmc *mmc = mmc_get_mmc_dev(dev);
287 struct ftsdc010_chip *chip = mmc->priv;
288 struct ftsdc010_mmc __iomem *regs = chip->regs;
290 ftsdc010_clkset(mmc, mmc->clock);
292 clrbits_le32(®s->bwr, FTSDC010_BWR_MODE_MASK);
293 switch (mmc->bus_width) {
295 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_4BIT);
298 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_8BIT);
301 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_1BIT);
308 static int ftsdc010_get_cd(struct udevice *dev)
310 struct mmc *mmc = mmc_get_mmc_dev(dev);
311 struct ftsdc010_chip *chip = mmc->priv;
312 struct ftsdc010_mmc __iomem *regs = chip->regs;
313 return !(readl(®s->status) & FTSDC010_STATUS_CARD_DETECT);
316 static int ftsdc010_get_wp(struct udevice *dev)
318 struct mmc *mmc = mmc_get_mmc_dev(dev);
319 struct ftsdc010_chip *chip = mmc->priv;
320 struct ftsdc010_mmc __iomem *regs = chip->regs;
321 if (readl(®s->status) & FTSDC010_STATUS_WRITE_PROT) {
322 printf("ftsdc010: write protected\n");
329 static int ftsdc010_init(struct mmc *mmc)
331 struct ftsdc010_chip *chip = mmc->priv;
332 struct ftsdc010_mmc __iomem *regs = chip->regs;
335 chip->fifo = (readl(®s->feature) & 0xff) << 2;
338 writel(FTSDC010_CMD_SDC_RST, ®s->cmd);
339 for (ts = get_timer(0); get_timer(ts) < CFG_RST_TIMEOUT; ) {
340 if (readl(®s->cmd) & FTSDC010_CMD_SDC_RST)
344 if (readl(®s->cmd) & FTSDC010_CMD_SDC_RST) {
345 printf("ftsdc010: reset failed\n");
349 /* 2. enter low speed mode (400k card detection) */
350 ftsdc010_clkset(mmc, 400000);
352 /* 3. interrupt disabled */
353 writel(0, ®s->int_mask);
358 static int ftsdc010_probe(struct udevice *dev)
360 struct mmc *mmc = mmc_get_mmc_dev(dev);
361 return ftsdc010_init(mmc);
364 const struct dm_mmc_ops dm_ftsdc010_mmc_ops = {
365 .send_cmd = ftsdc010_request,
366 .set_ios = ftsdc010_set_ios,
367 .get_cd = ftsdc010_get_cd,
368 .get_wp = ftsdc010_get_wp,
371 static void ftsdc_setup_cfg(struct mmc_config *cfg, const char *name, int buswidth,
372 uint caps, u32 max_clk, u32 min_clk)
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;
380 cfg->host_caps |= MMC_MODE_8BIT;
381 cfg->host_caps &= ~MMC_MODE_4BIT;
383 cfg->host_caps |= MMC_MODE_4BIT;
384 cfg->host_caps &= ~MMC_MODE_8BIT;
386 cfg->part_type = PART_TYPE_DOS;
387 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
390 static int ftsdc010_mmc_ofdata_to_platdata(struct udevice *dev)
392 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
393 struct ftsdc_priv *priv = dev_get_priv(dev);
394 struct ftsdc010_chip *chip = &priv->chip;
395 chip->name = dev->name;
396 chip->ioaddr = (void *)devfdt_get_addr(dev);
397 chip->buswidth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
400 priv->fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
402 priv->fifo_mode = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
404 if (fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
405 "clock-freq-min-max", priv->minmax, 2)) {
406 int val = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
407 "max-frequency", -EINVAL);
411 priv->minmax[0] = 400000; /* 400 kHz */
412 priv->minmax[1] = val;
414 debug("%s: 'clock-freq-min-max' property was deprecated.\n",
418 chip->sclk = priv->minmax[1];
419 chip->regs = chip->ioaddr;
423 static int ftsdc010_mmc_probe(struct udevice *dev)
425 struct ftsdc010_plat *plat = dev_get_platdata(dev);
426 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
427 struct ftsdc_priv *priv = dev_get_priv(dev);
428 struct ftsdc010_chip *chip = &priv->chip;
429 struct udevice *pwr_dev __maybe_unused;
431 #if CONFIG_IS_ENABLED(OF_PLATDATA)
433 struct ftsdc010 *dtplat = &plat->dtplat;
434 chip->name = dev->name;
435 chip->ioaddr = map_sysmem(dtplat->reg[0], dtplat->reg[1]);
436 chip->buswidth = dtplat->bus_width;
439 memcpy(priv->minmax, dtplat->clock_freq_min_max, sizeof(priv->minmax));
440 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
445 if (dev_read_bool(dev, "cap-mmc-highspeed") || \
446 dev_read_bool(dev, "cap-sd-highspeed"))
447 chip->caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
449 ftsdc_setup_cfg(&plat->cfg, dev->name, chip->buswidth, chip->caps,
450 priv->minmax[1] , priv->minmax[0]);
451 chip->mmc = &plat->mmc;
452 chip->mmc->priv = &priv->chip;
453 chip->mmc->dev = dev;
454 upriv->mmc = chip->mmc;
455 return ftsdc010_probe(dev);
458 int ftsdc010_mmc_bind(struct udevice *dev)
460 struct ftsdc010_plat *plat = dev_get_platdata(dev);
462 return mmc_bind(dev, &plat->mmc, &plat->cfg);
465 static const struct udevice_id ftsdc010_mmc_ids[] = {
466 { .compatible = "andestech,atfsdc010" },
470 U_BOOT_DRIVER(ftsdc010_mmc) = {
471 .name = "ftsdc010_mmc",
473 .of_match = ftsdc010_mmc_ids,
474 .ofdata_to_platdata = ftsdc010_mmc_ofdata_to_platdata,
475 .ops = &dm_ftsdc010_mmc_ops,
476 .bind = ftsdc010_mmc_bind,
477 .probe = ftsdc010_mmc_probe,
478 .priv_auto_alloc_size = sizeof(struct ftsdc_priv),
479 .platdata_auto_alloc_size = sizeof(struct ftsdc010_plat),