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 <asm/errno.h>
17 #include <asm/byteorder.h>
18 #include <faraday/ftsdc010.h>
20 #define CFG_CMD_TIMEOUT (CONFIG_SYS_HZ >> 4) /* 250 ms */
21 #define CFG_RST_TIMEOUT CONFIG_SYS_HZ /* 1 sec reset timeout */
23 struct ftsdc010_chip {
25 uint32_t wprot; /* write protected (locked) */
26 uint32_t rate; /* actual SD clock in Hz */
27 uint32_t sclk; /* FTSDC010 source clock in Hz */
28 uint32_t fifo; /* fifo depth in bytes */
32 static inline int ftsdc010_send_cmd(struct mmc *mmc, struct mmc_cmd *mmc_cmd)
34 struct ftsdc010_chip *chip = mmc->priv;
35 struct ftsdc010_mmc __iomem *regs = chip->regs;
38 uint32_t cmd = FTSDC010_CMD_IDX(mmc_cmd->cmdidx);
39 uint32_t arg = mmc_cmd->cmdarg;
40 uint32_t flags = mmc_cmd->resp_type;
42 cmd |= FTSDC010_CMD_CMD_EN;
45 cmd |= FTSDC010_CMD_APP_CMD;
49 if (flags & MMC_RSP_PRESENT)
50 cmd |= FTSDC010_CMD_NEED_RSP;
52 if (flags & MMC_RSP_136)
53 cmd |= FTSDC010_CMD_LONG_RSP;
55 writel(FTSDC010_STATUS_RSP_MASK | FTSDC010_STATUS_CMD_SEND,
57 writel(arg, ®s->argu);
58 writel(cmd, ®s->cmd);
60 if (!(flags & (MMC_RSP_PRESENT | MMC_RSP_136))) {
61 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
62 if (readl(®s->status) & FTSDC010_STATUS_CMD_SEND) {
63 writel(FTSDC010_STATUS_CMD_SEND, ®s->clr);
70 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
71 st = readl(®s->status);
72 writel(st & FTSDC010_STATUS_RSP_MASK, ®s->clr);
73 if (st & FTSDC010_STATUS_RSP_MASK)
76 if (st & FTSDC010_STATUS_RSP_CRC_OK) {
77 if (flags & MMC_RSP_136) {
78 mmc_cmd->response[0] = readl(®s->rsp3);
79 mmc_cmd->response[1] = readl(®s->rsp2);
80 mmc_cmd->response[2] = readl(®s->rsp1);
81 mmc_cmd->response[3] = readl(®s->rsp0);
83 mmc_cmd->response[0] = readl(®s->rsp0);
87 debug("ftsdc010: rsp err (cmd=%d, st=0x%x)\n",
93 debug("ftsdc010: cmd timeout (op code=%d)\n",
95 } else if (mmc_cmd->cmdidx == MMC_CMD_APP_CMD) {
102 static void ftsdc010_clkset(struct mmc *mmc, uint32_t rate)
104 struct ftsdc010_chip *chip = mmc->priv;
105 struct ftsdc010_mmc __iomem *regs = chip->regs;
108 for (div = 0; div < 0x7f; ++div) {
109 if (rate >= chip->sclk / (2 * (div + 1)))
112 chip->rate = chip->sclk / (2 * (div + 1));
114 writel(FTSDC010_CCR_CLK_DIV(div), ®s->ccr);
117 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_SD);
119 if (chip->rate > 25000000)
120 setbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
122 clrbits_le32(®s->ccr, FTSDC010_CCR_CLK_HISPD);
126 static inline int ftsdc010_is_ro(struct mmc *mmc)
128 struct ftsdc010_chip *chip = mmc->priv;
129 const uint8_t *csd = (const uint8_t *)mmc->csd;
131 return chip->wprot || (csd[1] & 0x30);
134 static int ftsdc010_wait(struct ftsdc010_mmc __iomem *regs, uint32_t mask)
139 for (ts = get_timer(0); get_timer(ts) < CFG_CMD_TIMEOUT; ) {
140 st = readl(®s->status);
143 writel(st & mask, ®s->clr);
149 debug("ftsdc010: wait st(0x%x) timeout\n", mask);
158 static int ftsdc010_request(struct mmc *mmc, struct mmc_cmd *cmd,
159 struct mmc_data *data)
161 int ret = UNUSABLE_ERR;
163 struct ftsdc010_chip *chip = mmc->priv;
164 struct ftsdc010_mmc __iomem *regs = chip->regs;
166 if (data && (data->flags & MMC_DATA_WRITE) && chip->wprot) {
167 printf("ftsdc010: the card is write protected!\n");
174 len = data->blocksize * data->blocks;
176 /* 1. data disable + fifo reset */
178 #ifdef CONFIG_FTSDC010_SDIO
179 dcr |= FTSDC010_DCR_FIFO_RST;
181 writel(dcr, ®s->dcr);
183 /* 2. clear status register */
184 writel(FTSDC010_STATUS_DATA_MASK | FTSDC010_STATUS_FIFO_URUN
185 | FTSDC010_STATUS_FIFO_ORUN, ®s->clr);
187 /* 3. data timeout (1 sec) */
188 writel(chip->rate, ®s->dtr);
190 /* 4. data length (bytes) */
191 writel(len, ®s->dlr);
194 dcr = (ffs(data->blocksize) - 1) | FTSDC010_DCR_DATA_EN;
195 if (data->flags & MMC_DATA_WRITE)
196 dcr |= FTSDC010_DCR_DATA_WRITE;
197 writel(dcr, ®s->dcr);
200 ret = ftsdc010_send_cmd(mmc, cmd);
202 printf("ftsdc010: CMD%d failed\n", cmd->cmdidx);
209 if (data->flags & MMC_DATA_WRITE) {
210 const uint8_t *buf = (const uint8_t *)data->src;
215 /* wait for tx ready */
216 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_URUN);
220 /* write bytes to ftsdc010 */
221 for (wlen = 0; wlen < len && wlen < chip->fifo; ) {
222 writel(*(uint32_t *)buf, ®s->dwr);
231 uint8_t *buf = (uint8_t *)data->dest;
236 /* wait for rx ready */
237 ret = ftsdc010_wait(regs, FTSDC010_STATUS_FIFO_ORUN);
241 /* fetch bytes from ftsdc010 */
242 for (rlen = 0; rlen < len && rlen < chip->fifo; ) {
243 *(uint32_t *)buf = readl(®s->dwr);
254 ret = ftsdc010_wait(regs,
255 FTSDC010_STATUS_DATA_END | FTSDC010_STATUS_DATA_ERROR);
261 static void ftsdc010_set_ios(struct mmc *mmc)
263 struct ftsdc010_chip *chip = mmc->priv;
264 struct ftsdc010_mmc __iomem *regs = chip->regs;
266 ftsdc010_clkset(mmc, mmc->clock);
268 clrbits_le32(®s->bwr, FTSDC010_BWR_MODE_MASK);
269 switch (mmc->bus_width) {
271 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_4BIT);
274 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_8BIT);
277 setbits_le32(®s->bwr, FTSDC010_BWR_MODE_1BIT);
282 static int ftsdc010_init(struct mmc *mmc)
284 struct ftsdc010_chip *chip = mmc->priv;
285 struct ftsdc010_mmc __iomem *regs = chip->regs;
288 if (readl(®s->status) & FTSDC010_STATUS_CARD_DETECT)
291 if (readl(®s->status) & FTSDC010_STATUS_WRITE_PROT) {
292 printf("ftsdc010: write protected\n");
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_mmc_init(int devid)
322 struct ftsdc010_chip *chip;
323 struct ftsdc010_mmc __iomem *regs;
324 #ifdef CONFIG_FTSDC010_BASE_LIST
325 uint32_t base_list[] = CONFIG_FTSDC010_BASE_LIST;
327 if (devid < 0 || devid >= ARRAY_SIZE(base_list))
329 regs = (void __iomem *)base_list[devid];
331 regs = (void __iomem *)(CONFIG_FTSDC010_BASE + (devid << 20));
334 mmc = malloc(sizeof(struct mmc));
337 memset(mmc, 0, sizeof(struct mmc));
339 chip = malloc(sizeof(struct ftsdc010_chip));
344 memset(chip, 0, sizeof(struct ftsdc010_chip));
349 sprintf(mmc->name, "ftsdc010");
350 mmc->send_cmd = ftsdc010_request;
351 mmc->set_ios = ftsdc010_set_ios;
352 mmc->init = ftsdc010_init;
354 mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz;
355 switch (readl(®s->bwr) & FTSDC010_BWR_CAPS_MASK) {
356 case FTSDC010_BWR_CAPS_4BIT:
357 mmc->host_caps |= MMC_MODE_4BIT;
359 case FTSDC010_BWR_CAPS_8BIT:
360 mmc->host_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
366 #ifdef CONFIG_SYS_CLK_FREQ
367 chip->sclk = CONFIG_SYS_CLK_FREQ;
369 chip->sclk = clk_get_rate("SDC");
372 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
373 mmc->f_max = chip->sclk / 2;
374 mmc->f_min = chip->sclk / 0x100;
375 mmc->block_dev.part_type = PART_TYPE_DOS;