2 * Faraday MMC/SD Host Controller
4 * (C) Copyright 2010 Faraday Technology
5 * Dante Su <dantesu@faraday-tech.com>
7 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/errno.h>
17 #include <asm/byteorder.h>
18 #include <faraday/ftsdc010.h>
19 #include "ftsdc010_mci.h"
21 #define CFG_CMD_TIMEOUT (CONFIG_SYS_HZ >> 4) /* 250 ms */
22 #define CFG_RST_TIMEOUT CONFIG_SYS_HZ /* 1 sec reset timeout */
24 static inline int ftsdc010_send_cmd(struct mmc *mmc, struct mmc_cmd *mmc_cmd)
26 struct ftsdc010_chip *chip = mmc->priv;
27 struct ftsdc010_mmc __iomem *regs = chip->regs;
30 uint32_t cmd = FTSDC010_CMD_IDX(mmc_cmd->cmdidx);
31 uint32_t arg = mmc_cmd->cmdarg;
32 uint32_t flags = mmc_cmd->resp_type;
34 cmd |= FTSDC010_CMD_CMD_EN;
37 cmd |= FTSDC010_CMD_APP_CMD;
41 if (flags & MMC_RSP_PRESENT)
42 cmd |= FTSDC010_CMD_NEED_RSP;
44 if (flags & MMC_RSP_136)
45 cmd |= FTSDC010_CMD_LONG_RSP;
47 writel(FTSDC010_STATUS_RSP_MASK | FTSDC010_STATUS_CMD_SEND,
49 writel(arg, ®s->argu);
50 writel(cmd, ®s->cmd);
52 if (!(flags & (MMC_RSP_PRESENT | MMC_RSP_136))) {
53 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
54 if (readl(®s->status) & FTSDC010_STATUS_CMD_SEND) {
55 writel(FTSDC010_STATUS_CMD_SEND, ®s->clr);
62 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
63 st = readl(®s->status);
64 writel(st & FTSDC010_STATUS_RSP_MASK, ®s->clr);
65 if (st & FTSDC010_STATUS_RSP_MASK)
68 if (st & FTSDC010_STATUS_RSP_CRC_OK) {
69 if (flags & MMC_RSP_136) {
70 mmc_cmd->response[0] = readl(®s->rsp3);
71 mmc_cmd->response[1] = readl(®s->rsp2);
72 mmc_cmd->response[2] = readl(®s->rsp1);
73 mmc_cmd->response[3] = readl(®s->rsp0);
75 mmc_cmd->response[0] = readl(®s->rsp0);
79 debug("ftsdc010: rsp err (cmd=%d, st=0x%x)\n",
85 debug("ftsdc010: cmd timeout (op code=%d)\n",
87 } else if (mmc_cmd->cmdidx == MMC_CMD_APP_CMD) {
94 static void ftsdc010_clkset(struct mmc *mmc, uint32_t rate)
96 struct ftsdc010_chip *chip = mmc->priv;
97 struct ftsdc010_mmc __iomem *regs = chip->regs;
100 for (div = 0; div < 0x7f; ++div) {
101 if (rate >= chip->sclk / (2 * (div + 1)))
104 chip->rate = chip->sclk / (2 * (div + 1));
106 writel(FTSDC010_CCR_CLK_DIV(div), ®s->ccr);
109 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_SD);
111 if (chip->rate > 25000000)
112 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
114 clrbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
118 static int ftsdc010_wait(struct ftsdc010_mmc __iomem *regs, uint32_t mask)
120 int ret = -ETIMEDOUT;
121 uint32_t st, timeout = 10000000;
123 st = readl(®s->status);
126 writel(st & mask, ®s->clr);
132 debug("ftsdc010: wait st(0x%x) timeout\n", mask);
141 static int ftsdc010_request(struct udevice *dev, struct mmc_cmd *cmd,
142 struct mmc_data *data)
144 struct mmc *mmc = mmc_get_mmc_dev(dev);
145 int ret = -EOPNOTSUPP;
147 struct ftsdc010_chip *chip = mmc->priv;
148 struct ftsdc010_mmc __iomem *regs = chip->regs;
150 if (data && (data->flags & MMC_DATA_WRITE) && chip->wprot) {
151 printf("ftsdc010: the card is write protected!\n");
158 len = data->blocksize * data->blocks;
160 /* 1. data disable + fifo reset */
162 #ifdef CONFIG_FTSDC010_SDIO
163 dcr |= FTSDC010_DCR_FIFO_RST;
165 writel(dcr, ®s->dcr);
167 /* 2. clear status register */
168 writel(FTSDC010_STATUS_DATA_MASK | FTSDC010_STATUS_FIFO_URUN
169 | FTSDC010_STATUS_FIFO_ORUN, ®s->clr);
171 /* 3. data timeout (1 sec) */
172 writel(chip->rate, ®s->dtr);
174 /* 4. data length (bytes) */
175 writel(len, ®s->dlr);
178 dcr = (ffs(data->blocksize) - 1) | FTSDC010_DCR_DATA_EN;
179 if (data->flags & MMC_DATA_WRITE)
180 dcr |= FTSDC010_DCR_DATA_WRITE;
181 writel(dcr, ®s->dcr);
184 ret = ftsdc010_send_cmd(mmc, cmd);
186 printf("ftsdc010: CMD%d failed\n", cmd->cmdidx);
193 if (data->flags & MMC_DATA_WRITE) {
194 const uint8_t *buf = (const uint8_t *)data->src;
199 /* wait for tx ready */
200 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_URUN);
204 /* write bytes to ftsdc010 */
205 for (wlen = 0; wlen < len && wlen < chip->fifo; ) {
206 writel(*(uint32_t *)buf, ®s->dwr);
215 uint8_t *buf = (uint8_t *)data->dest;
220 /* wait for rx ready */
221 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_ORUN);
225 /* fetch bytes from ftsdc010 */
226 for (rlen = 0; rlen < len && rlen < chip->fifo; ) {
227 *(uint32_t *)buf = readl(®s->dwr);
238 ret = ftsdc010_wait(regs,
239 FTSDC010_STATUS_DATA_END | FTSDC010_STATUS_DATA_CRC_OK);
245 static int ftsdc010_set_ios(struct udevice *dev)
247 struct mmc *mmc = mmc_get_mmc_dev(dev);
248 struct ftsdc010_chip *chip = mmc->priv;
249 struct ftsdc010_mmc __iomem *regs = chip->regs;
251 ftsdc010_clkset(mmc, mmc->clock);
253 clrbits_le32(®s->bwr, FTSDC010_BWR_MODE_MASK);
254 switch (mmc->bus_width) {
256 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_4BIT);
259 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_8BIT);
262 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_1BIT);
269 static int ftsdc010_get_cd(struct udevice *dev)
271 struct mmc *mmc = mmc_get_mmc_dev(dev);
272 struct ftsdc010_chip *chip = mmc->priv;
273 struct ftsdc010_mmc __iomem *regs = chip->regs;
274 return !(readl(®s->status) & FTSDC010_STATUS_CARD_DETECT);
277 static int ftsdc010_get_wp(struct udevice *dev)
279 struct mmc *mmc = mmc_get_mmc_dev(dev);
280 struct ftsdc010_chip *chip = mmc->priv;
281 struct ftsdc010_mmc __iomem *regs = chip->regs;
282 if (readl(®s->status) & FTSDC010_STATUS_WRITE_PROT) {
283 printf("ftsdc010: write protected\n");
290 static int ftsdc010_init(struct mmc *mmc)
292 struct ftsdc010_chip *chip = mmc->priv;
293 struct ftsdc010_mmc __iomem *regs = chip->regs;
296 chip->fifo = (readl(®s->feature) & 0xff) << 2;
299 writel(FTSDC010_CMD_SDC_RST, ®s->cmd);
300 for (ts = get_timer(0); get_timer(ts) < CFG_RST_TIMEOUT; ) {
301 if (readl(®s->cmd) & FTSDC010_CMD_SDC_RST)
305 if (readl(®s->cmd) & FTSDC010_CMD_SDC_RST) {
306 printf("ftsdc010: reset failed\n");
310 /* 2. enter low speed mode (400k card detection) */
311 ftsdc010_clkset(mmc, 400000);
313 /* 3. interrupt disabled */
314 writel(0, ®s->int_mask);
319 int ftsdc010_probe(struct udevice *dev)
321 struct mmc *mmc = mmc_get_mmc_dev(dev);
322 return ftsdc010_init(mmc);
325 const struct dm_mmc_ops dm_ftsdc010_ops = {
326 .send_cmd = ftsdc010_request,
327 .set_ios = ftsdc010_set_ios,
328 .get_cd = ftsdc010_get_cd,
329 .get_wp = ftsdc010_get_wp,
332 void ftsdc_setup_cfg(struct mmc_config *cfg, const char *name, int buswidth,
333 uint caps, u32 max_clk, u32 min_clk)
336 cfg->f_min = min_clk;
337 cfg->f_max = max_clk;
338 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
339 cfg->host_caps = caps;
341 cfg->host_caps |= MMC_MODE_8BIT;
342 cfg->host_caps &= ~MMC_MODE_4BIT;
344 cfg->host_caps |= MMC_MODE_4BIT;
345 cfg->host_caps &= ~MMC_MODE_8BIT;
347 cfg->part_type = PART_TYPE_DOS;
348 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
351 int ftsdc010_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
353 return mmc_bind(dev, mmc, cfg);