2 * bcm2835 sdhost driver.
4 * The 2835 has two SD controllers: The Arasan sdhci controller
5 * (supported by the iproc driver) and a custom sdhost controller
6 * (supported by this driver).
8 * The sdhci controller supports both sdcard and sdio. The sdhost
9 * controller supports the sdcard only, but has better performance.
10 * Also note that the rpi3 has sdio wifi, so driving the sdcard with
11 * the sdhost controller allows to use the sdhci controller for wifi
14 * The configuration is done by devicetree via pin muxing. Both
15 * SD controller are available on the same pins (2 pin groups = pin 22
16 * to 27 + pin 48 to 53). So it's possible to use both SD controllers
17 * at the same time with different pin groups.
19 * This code was ported to U-Boot by
20 * Alexander Graf <agraf@suse.de>
21 * and is based on drivers/mmc/host/bcm2835.c in Linux which is written by
22 * Phil Elwell <phil@raspberrypi.org>
23 * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
25 * mmc-bcm2835.c by Gellert Weisz
26 * which is, in turn, based on
27 * sdhci-bcm2708.c by Broadcom
28 * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
29 * sdhci.c and sdhci-pci.c by Pierre Ossman
31 * SPDX-License-Identifier: GPL-2.0
37 #include <asm/arch/msg.h>
38 #include <asm/unaligned.h>
39 #include <linux/compat.h>
41 #include <linux/iopoll.h>
42 #include <linux/sizes.h>
43 #include <mach/gpio.h>
44 #include <power/regulator.h>
46 DECLARE_GLOBAL_DATA_PTR;
48 #define msleep(a) udelay(a * 1000)
50 #define SDCMD 0x00 /* Command to SD card - 16 R/W */
51 #define SDARG 0x04 /* Argument to SD card - 32 R/W */
52 #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
53 #define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
54 #define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
55 #define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
56 #define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
57 #define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
58 #define SDHSTS 0x20 /* SD host status - 11 R/W */
59 #define SDVDD 0x30 /* SD card power control - 1 R/W */
60 #define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
61 #define SDHCFG 0x38 /* Host configuration - 2 R/W */
62 #define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
63 #define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
64 #define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
66 #define SDCMD_NEW_FLAG 0x8000
67 #define SDCMD_FAIL_FLAG 0x4000
68 #define SDCMD_BUSYWAIT 0x800
69 #define SDCMD_NO_RESPONSE 0x400
70 #define SDCMD_LONG_RESPONSE 0x200
71 #define SDCMD_WRITE_CMD 0x80
72 #define SDCMD_READ_CMD 0x40
73 #define SDCMD_CMD_MASK 0x3f
75 #define SDCDIV_MAX_CDIV 0x7ff
77 #define SDHSTS_BUSY_IRPT 0x400
78 #define SDHSTS_BLOCK_IRPT 0x200
79 #define SDHSTS_SDIO_IRPT 0x100
80 #define SDHSTS_REW_TIME_OUT 0x80
81 #define SDHSTS_CMD_TIME_OUT 0x40
82 #define SDHSTS_CRC16_ERROR 0x20
83 #define SDHSTS_CRC7_ERROR 0x10
84 #define SDHSTS_FIFO_ERROR 0x08
85 #define SDHSTS_DATA_FLAG 0x01
87 #define SDHSTS_CLEAR_MASK (SDHSTS_BUSY_IRPT | \
90 SDHSTS_REW_TIME_OUT | \
91 SDHSTS_CMD_TIME_OUT | \
92 SDHSTS_CRC16_ERROR | \
96 #define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR | \
97 SDHSTS_CRC16_ERROR | \
98 SDHSTS_REW_TIME_OUT | \
101 #define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT | \
102 SDHSTS_TRANSFER_ERROR_MASK)
104 #define SDHCFG_BUSY_IRPT_EN BIT(10)
105 #define SDHCFG_BLOCK_IRPT_EN BIT(8)
106 #define SDHCFG_SDIO_IRPT_EN BIT(5)
107 #define SDHCFG_DATA_IRPT_EN BIT(4)
108 #define SDHCFG_SLOW_CARD BIT(3)
109 #define SDHCFG_WIDE_EXT_BUS BIT(2)
110 #define SDHCFG_WIDE_INT_BUS BIT(1)
111 #define SDHCFG_REL_CMD_LINE BIT(0)
113 #define SDVDD_POWER_OFF 0
114 #define SDVDD_POWER_ON 1
116 #define SDEDM_FORCE_DATA_MODE BIT(19)
117 #define SDEDM_CLOCK_PULSE BIT(20)
118 #define SDEDM_BYPASS BIT(21)
120 #define SDEDM_FIFO_FILL_SHIFT 4
121 #define SDEDM_FIFO_FILL_MASK 0x1f
122 static u32 edm_fifo_fill(u32 edm)
124 return (edm >> SDEDM_FIFO_FILL_SHIFT) & SDEDM_FIFO_FILL_MASK;
127 #define SDEDM_WRITE_THRESHOLD_SHIFT 9
128 #define SDEDM_READ_THRESHOLD_SHIFT 14
129 #define SDEDM_THRESHOLD_MASK 0x1f
131 #define SDEDM_FSM_MASK 0xf
132 #define SDEDM_FSM_IDENTMODE 0x0
133 #define SDEDM_FSM_DATAMODE 0x1
134 #define SDEDM_FSM_READDATA 0x2
135 #define SDEDM_FSM_WRITEDATA 0x3
136 #define SDEDM_FSM_READWAIT 0x4
137 #define SDEDM_FSM_READCRC 0x5
138 #define SDEDM_FSM_WRITECRC 0x6
139 #define SDEDM_FSM_WRITEWAIT1 0x7
140 #define SDEDM_FSM_POWERDOWN 0x8
141 #define SDEDM_FSM_POWERUP 0x9
142 #define SDEDM_FSM_WRITESTART1 0xa
143 #define SDEDM_FSM_WRITESTART2 0xb
144 #define SDEDM_FSM_GENPULSES 0xc
145 #define SDEDM_FSM_WRITEWAIT2 0xd
146 #define SDEDM_FSM_STARTPOWDOWN 0xf
148 #define SDDATA_FIFO_WORDS 16
150 #define FIFO_READ_THRESHOLD 4
151 #define FIFO_WRITE_THRESHOLD 4
152 #define SDDATA_FIFO_PIO_BURST 8
154 #define SDHST_TIMEOUT_MAX_USEC 100000
156 struct bcm2835_plat {
157 struct mmc_config cfg;
161 struct bcm2835_host {
162 void __iomem *ioaddr;
165 int clock; /* Current clock speed */
166 unsigned int max_clk; /* Max possible freq */
167 unsigned int blocks; /* remaining PIO blocks */
168 int irq; /* Device IRQ */
170 u32 ns_per_fifo_word;
172 /* cached registers */
176 struct mmc_cmd *cmd; /* Current command */
177 struct mmc_data *data; /* Current data request */
178 bool data_complete:1;/* Data finished before cmd */
179 bool use_busy:1; /* Wait for busy interrupt */
180 bool wait_data_complete:1; /* Wait for data */
182 /* for threaded irq handler */
189 struct bcm2835_plat *plat;
192 static void bcm2835_dumpregs(struct bcm2835_host *host)
194 dev_dbg(dev, "=========== REGISTER DUMP ===========\n");
195 dev_dbg(dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD));
196 dev_dbg(dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG));
197 dev_dbg(dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
198 dev_dbg(dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
199 dev_dbg(dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
200 dev_dbg(dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
201 dev_dbg(dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
202 dev_dbg(dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
203 dev_dbg(dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
204 dev_dbg(dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD));
205 dev_dbg(dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM));
206 dev_dbg(dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
207 dev_dbg(dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
208 dev_dbg(dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
209 dev_dbg(dev, "===========================================\n");
212 static void bcm2835_reset_internal(struct bcm2835_host *host)
216 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
217 writel(0, host->ioaddr + SDCMD);
218 writel(0, host->ioaddr + SDARG);
219 /* Set timeout to a big enough value so we don't hit it */
220 writel(0xf00000, host->ioaddr + SDTOUT);
221 writel(0, host->ioaddr + SDCDIV);
222 /* Clear status register */
223 writel(SDHSTS_CLEAR_MASK, host->ioaddr + SDHSTS);
224 writel(0, host->ioaddr + SDHCFG);
225 writel(0, host->ioaddr + SDHBCT);
226 writel(0, host->ioaddr + SDHBLC);
228 /* Limit fifo usage due to silicon bug */
229 temp = readl(host->ioaddr + SDEDM);
230 temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) |
231 (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT));
232 temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
233 (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
234 writel(temp, host->ioaddr + SDEDM);
235 /* Wait for FIFO threshold to populate */
237 writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
238 /* Wait for all components to go through power on cycle */
241 writel(host->hcfg, host->ioaddr + SDHCFG);
242 writel(host->cdiv, host->ioaddr + SDCDIV);
245 static int bcm2835_finish_command(struct bcm2835_host *host);
247 static void bcm2835_wait_transfer_complete(struct bcm2835_host *host)
252 alternate_idle = (host->data->flags & MMC_DATA_READ) ?
253 SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
260 edm = readl(host->ioaddr + SDEDM);
261 fsm = edm & SDEDM_FSM_MASK;
263 if ((fsm == SDEDM_FSM_IDENTMODE) ||
264 (fsm == SDEDM_FSM_DATAMODE))
266 if (fsm == alternate_idle) {
267 writel(edm | SDEDM_FORCE_DATA_MODE,
268 host->ioaddr + SDEDM);
272 /* Error out after 100000 register reads (~1s) */
273 if (timediff++ == 100000) {
275 "wait_transfer_complete - still waiting after %d retries\n",
277 bcm2835_dumpregs(host);
283 static int bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
285 struct mmc_data *data = host->data;
286 size_t blksize = data->blocksize;
291 if (blksize % sizeof(u32))
294 buf = is_read ? (u32 *)data->dest : (u32 *)data->src;
297 data->dest += blksize;
299 data->src += blksize;
301 copy_words = blksize / sizeof(u32);
304 * Copy all contents from/to the FIFO as far as it reaches,
305 * then wait for it to fill/empty again and rewind.
308 int burst_words, words;
311 burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words);
312 edm = readl(host->ioaddr + SDEDM);
314 words = edm_fifo_fill(edm);
316 words = SDDATA_FIFO_WORDS - edm_fifo_fill(edm);
318 if (words < burst_words) {
319 int fsm_state = (edm & SDEDM_FSM_MASK);
322 (fsm_state != SDEDM_FSM_READDATA &&
323 fsm_state != SDEDM_FSM_READWAIT &&
324 fsm_state != SDEDM_FSM_READCRC)) ||
326 (fsm_state != SDEDM_FSM_WRITEDATA &&
327 fsm_state != SDEDM_FSM_WRITESTART1 &&
328 fsm_state != SDEDM_FSM_WRITESTART2))) {
329 hsts = readl(host->ioaddr + SDHSTS);
330 printf("fsm %x, hsts %08x\n", fsm_state, hsts);
331 if (hsts & SDHSTS_ERROR_MASK)
336 } else if (words > copy_words) {
342 /* Copy current chunk to/from the FIFO */
345 *(buf++) = readl(host->ioaddr + SDDATA);
347 writel(*(buf++), host->ioaddr + SDDATA);
355 static int bcm2835_transfer_pio(struct bcm2835_host *host)
361 is_read = (host->data->flags & MMC_DATA_READ) != 0;
362 ret = bcm2835_transfer_block_pio(host, is_read);
364 if (host->wait_data_complete)
365 bcm2835_wait_transfer_complete(host);
367 sdhsts = readl(host->ioaddr + SDHSTS);
368 if (sdhsts & (SDHSTS_CRC16_ERROR |
370 SDHSTS_FIFO_ERROR)) {
371 printf("%s transfer error - HSTS %08x\n",
372 is_read ? "read" : "write", sdhsts);
374 } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
375 SDHSTS_REW_TIME_OUT))) {
376 printf("%s timeout error - HSTS %08x\n",
377 is_read ? "read" : "write", sdhsts);
384 static void bcm2835_set_transfer_irqs(struct bcm2835_host *host)
386 u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
389 host->hcfg = (host->hcfg & ~all_irqs) |
390 SDHCFG_DATA_IRPT_EN |
393 writel(host->hcfg, host->ioaddr + SDHCFG);
397 void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_cmd *cmd,
398 struct mmc_data *data)
406 host->wait_data_complete = cmd->cmdidx != MMC_CMD_READ_MULTIPLE_BLOCK;
407 host->data_complete = false;
410 host->blocks = data->blocks;
412 bcm2835_set_transfer_irqs(host);
414 writel(data->blocksize, host->ioaddr + SDHBCT);
415 writel(data->blocks, host->ioaddr + SDHBLC);
418 static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host)
422 int timeout_us = SDHST_TIMEOUT_MAX_USEC;
424 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
425 !(value & SDCMD_NEW_FLAG), timeout_us);
426 if (ret == -ETIMEDOUT)
427 printf("%s: timeout (%d us)\n", __func__, timeout_us);
432 static int bcm2835_send_command(struct bcm2835_host *host, struct mmc_cmd *cmd,
433 struct mmc_data *data)
439 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) {
440 printf("unsupported response type!\n");
444 sdcmd = bcm2835_read_wait_sdcmd(host);
445 if (sdcmd & SDCMD_NEW_FLAG) {
446 printf("previous command never completed.\n");
447 bcm2835_dumpregs(host);
453 /* Clear any error flags */
454 sdhsts = readl(host->ioaddr + SDHSTS);
455 if (sdhsts & SDHSTS_ERROR_MASK)
456 writel(sdhsts, host->ioaddr + SDHSTS);
458 bcm2835_prepare_data(host, cmd, data);
460 writel(cmd->cmdarg, host->ioaddr + SDARG);
462 sdcmd = cmd->cmdidx & SDCMD_CMD_MASK;
464 host->use_busy = false;
465 if (!(cmd->resp_type & MMC_RSP_PRESENT)) {
466 sdcmd |= SDCMD_NO_RESPONSE;
468 if (cmd->resp_type & MMC_RSP_136)
469 sdcmd |= SDCMD_LONG_RESPONSE;
470 if (cmd->resp_type & MMC_RSP_BUSY) {
471 sdcmd |= SDCMD_BUSYWAIT;
472 host->use_busy = true;
477 if (data->flags & MMC_DATA_WRITE)
478 sdcmd |= SDCMD_WRITE_CMD;
479 if (data->flags & MMC_DATA_READ)
480 sdcmd |= SDCMD_READ_CMD;
483 writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
488 static int bcm2835_transfer_complete(struct bcm2835_host *host)
492 WARN_ON(!host->data_complete);
499 static void bcm2835_finish_data(struct bcm2835_host *host)
501 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
502 writel(host->hcfg, host->ioaddr + SDHCFG);
504 host->data_complete = true;
507 /* Data managed to finish before the
508 * command completed. Make sure we do
509 * things in the proper order.
511 dev_dbg(dev, "Finished early - HSTS %08x\n",
512 readl(host->ioaddr + SDHSTS));
514 bcm2835_transfer_complete(host);
518 static int bcm2835_finish_command(struct bcm2835_host *host)
520 struct mmc_cmd *cmd = host->cmd;
524 sdcmd = bcm2835_read_wait_sdcmd(host);
526 /* Check for errors */
527 if (sdcmd & SDCMD_NEW_FLAG) {
528 printf("command never completed.\n");
529 bcm2835_dumpregs(host);
531 } else if (sdcmd & SDCMD_FAIL_FLAG) {
532 u32 sdhsts = readl(host->ioaddr + SDHSTS);
534 /* Clear the errors */
535 writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
537 if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
538 (host->cmd->cmdidx != MMC_CMD_SEND_OP_COND)) {
539 if (sdhsts & SDHSTS_CMD_TIME_OUT) {
542 printf("unexpected command %d error\n",
544 bcm2835_dumpregs(host);
552 if (cmd->resp_type & MMC_RSP_PRESENT) {
553 if (cmd->resp_type & MMC_RSP_136) {
556 for (i = 0; i < 4; i++) {
557 cmd->response[3 - i] =
558 readl(host->ioaddr + SDRSP0 + i * 4);
561 cmd->response[0] = readl(host->ioaddr + SDRSP0);
565 /* Processed actual command. */
567 if (host->data && host->data_complete)
568 ret = bcm2835_transfer_complete(host);
573 static int bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
577 if (!(intmask & SDHSTS_ERROR_MASK))
583 printf("sdhost_busy_irq: intmask %08x\n", intmask);
584 if (intmask & SDHSTS_CRC7_ERROR) {
586 } else if (intmask & (SDHSTS_CRC16_ERROR |
587 SDHSTS_FIFO_ERROR)) {
589 } else if (intmask & (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT)) {
592 bcm2835_dumpregs(host);
596 static int bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
602 if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
604 if (intmask & SDHSTS_REW_TIME_OUT)
608 printf("%s:%d %d\n", __func__, __LINE__, ret);
613 static void bcm2835_busy_irq(struct bcm2835_host *host)
615 if (WARN_ON(!host->cmd)) {
616 bcm2835_dumpregs(host);
620 if (WARN_ON(!host->use_busy)) {
621 bcm2835_dumpregs(host);
624 host->use_busy = false;
626 bcm2835_finish_command(host);
629 static void bcm2835_data_irq(struct bcm2835_host *host, u32 intmask)
634 * There are no dedicated data/space available interrupt
635 * status bits, so it is necessary to use the single shared
636 * data/space available FIFO status bits. It is therefore not
637 * an error to get here when there is no data transfer in
643 ret = bcm2835_check_data_error(host, intmask);
647 if (host->data->flags & MMC_DATA_WRITE) {
648 /* Use the block interrupt for writes after the first block */
649 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
650 host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
651 writel(host->hcfg, host->ioaddr + SDHCFG);
652 bcm2835_transfer_pio(host);
654 bcm2835_transfer_pio(host);
656 if ((host->blocks == 0))
662 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
663 writel(host->hcfg, host->ioaddr + SDHCFG);
666 static void bcm2835_data_threaded_irq(struct bcm2835_host *host)
670 if ((host->blocks == 0))
671 bcm2835_finish_data(host);
674 static void bcm2835_block_irq(struct bcm2835_host *host)
676 if (WARN_ON(!host->data)) {
677 bcm2835_dumpregs(host);
681 WARN_ON(!host->blocks);
682 if ((--host->blocks == 0))
683 bcm2835_finish_data(host);
685 bcm2835_transfer_pio(host);
688 static irqreturn_t bcm2835_irq(int irq, void *dev_id)
690 irqreturn_t result = IRQ_NONE;
691 struct bcm2835_host *host = dev_id;
694 intmask = readl(host->ioaddr + SDHSTS);
696 writel(SDHSTS_BUSY_IRPT |
700 host->ioaddr + SDHSTS);
702 if (intmask & SDHSTS_BLOCK_IRPT) {
703 bcm2835_check_data_error(host, intmask);
704 host->irq_block = true;
705 result = IRQ_WAKE_THREAD;
708 if (intmask & SDHSTS_BUSY_IRPT) {
709 if (!bcm2835_check_cmd_error(host, intmask)) {
710 host->irq_busy = true;
711 result = IRQ_WAKE_THREAD;
713 result = IRQ_HANDLED;
717 /* There is no true data interrupt status bit, so it is
718 * necessary to qualify the data flag with the interrupt
721 if ((intmask & SDHSTS_DATA_FLAG) &&
722 (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
723 bcm2835_data_irq(host, intmask);
724 host->irq_data = true;
725 result = IRQ_WAKE_THREAD;
731 static irqreturn_t bcm2835_threaded_irq(int irq, void *dev_id)
733 struct bcm2835_host *host = dev_id;
735 if (host->irq_block) {
736 host->irq_block = false;
737 bcm2835_block_irq(host);
740 if (host->irq_busy) {
741 host->irq_busy = false;
742 bcm2835_busy_irq(host);
745 if (host->irq_data) {
746 host->irq_data = false;
747 bcm2835_data_threaded_irq(host);
753 static void bcm2835_irq_poll(struct bcm2835_host *host)
758 intmask = readl(host->ioaddr + SDHSTS);
759 if (intmask & (SDHSTS_BUSY_IRPT | SDHSTS_BLOCK_IRPT |
760 SDHSTS_SDIO_IRPT | SDHSTS_DATA_FLAG)) {
761 bcm2835_irq(0, host);
762 bcm2835_threaded_irq(0, host);
768 static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
772 /* The SDCDIV register has 11 bits, and holds (div - 2). But
773 * in data mode the max is 50MHz wihout a minimum, and only
774 * the bottom 3 bits are used. Since the switch over is
775 * automatic (unless we have marked the card as slow...),
776 * chosen values have to make sense in both modes. Ident mode
777 * must be 100-400KHz, so can range check the requested
778 * clock. CMD15 must be used to return to data mode, so this
781 * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
782 * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
784 * 623->400KHz/27.8MHz
785 * reset value (507)->491159/50MHz
787 * BUT, the 3-bit clock divisor in data mode is too small if
788 * the core clock is higher than 250MHz, so instead use the
789 * SLOW_CARD configuration bit to force the use of the ident
790 * clock divisor at all times.
793 if (clock < 100000) {
794 /* Can't stop the clock, but make it as slow as possible
797 host->cdiv = SDCDIV_MAX_CDIV;
798 writel(host->cdiv, host->ioaddr + SDCDIV);
802 div = host->max_clk / clock;
805 if ((host->max_clk / div) > clock)
809 if (div > SDCDIV_MAX_CDIV)
810 div = SDCDIV_MAX_CDIV;
812 clock = host->max_clk / (div + 2);
813 host->mmc->clock = clock;
815 /* Calibrate some delays */
817 host->ns_per_fifo_word = (1000000000 / clock) *
818 ((host->mmc->card_caps & MMC_MODE_4BIT) ? 8 : 32);
821 writel(host->cdiv, host->ioaddr + SDCDIV);
823 /* Set the timeout to 500ms */
824 writel(host->mmc->clock / 2, host->ioaddr + SDTOUT);
827 static inline int is_power_of_2(u64 x)
829 return !(x & (x - 1));
832 static int bcm2835_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
833 struct mmc_data *data)
835 struct bcm2835_host *host = dev_get_priv(dev);
839 if (data && !is_power_of_2(data->blocksize)) {
840 printf("unsupported block size (%d bytes)\n", data->blocksize);
846 edm = readl(host->ioaddr + SDEDM);
847 fsm = edm & SDEDM_FSM_MASK;
849 if ((fsm != SDEDM_FSM_IDENTMODE) &&
850 (fsm != SDEDM_FSM_DATAMODE) &&
851 (cmd && cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) {
852 printf("previous command (%d) not complete (EDM %08x)\n",
853 readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK, edm);
854 bcm2835_dumpregs(host);
863 ret = bcm2835_send_command(host, cmd, data);
864 if (!ret && !host->use_busy)
865 ret = bcm2835_finish_command(host);
868 /* Wait for completion of busy signal or data transfer */
869 while (host->use_busy || host->data)
870 bcm2835_irq_poll(host);
875 static int bcm2835_set_ios(struct udevice *dev)
877 struct bcm2835_host *host = dev_get_priv(dev);
878 struct mmc *mmc = mmc_get_mmc_dev(dev);
880 if (!mmc->clock || mmc->clock != host->clock) {
881 bcm2835_set_clock(host, mmc->clock);
882 host->clock = mmc->clock;
886 host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
887 if (mmc->bus_width == 4)
888 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
890 host->hcfg |= SDHCFG_WIDE_INT_BUS;
892 /* Disable clever clock switching, to cope with fast core clocks */
893 host->hcfg |= SDHCFG_SLOW_CARD;
895 writel(host->hcfg, host->ioaddr + SDHCFG);
900 static void bcm2835_add_host(struct bcm2835_host *host)
902 struct mmc_config *cfg = &host->plat->cfg;
904 cfg->f_max = host->max_clk;
905 cfg->f_min = host->max_clk / SDCDIV_MAX_CDIV;
908 dev_dbg(dev, "f_max %d, f_min %d\n",
909 cfg->f_max, cfg->f_min);
911 /* host controller capabilities */
912 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
914 /* report supported voltage ranges */
915 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
917 /* Set interrupt enables */
918 host->hcfg = SDHCFG_BUSY_IRPT_EN;
920 bcm2835_reset_internal(host);
923 static int bcm2835_probe(struct udevice *dev)
925 struct bcm2835_plat *plat = dev_get_platdata(dev);
926 struct bcm2835_host *host = dev_get_priv(dev);
927 struct mmc *mmc = mmc_get_mmc_dev(dev);
928 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
933 upriv->mmc = &plat->mmc;
934 plat->cfg.name = dev->name;
936 host->phys_addr = devfdt_get_addr(dev);
937 if (host->phys_addr == FDT_ADDR_T_NONE)
940 host->ioaddr = devm_ioremap(dev, host->phys_addr, SZ_256);
944 host->max_clk = bcm2835_get_mmc_clock();
946 bcm2835_add_host(host);
948 dev_dbg(dev, "%s -> OK\n", __func__);
953 static const struct udevice_id bcm2835_match[] = {
954 { .compatible = "brcm,bcm2835-sdhost" },
958 static const struct dm_mmc_ops bcm2835_ops = {
959 .send_cmd = bcm2835_send_cmd,
960 .set_ios = bcm2835_set_ios,
963 static int bcm2835_bind(struct udevice *dev)
965 struct bcm2835_plat *plat = dev_get_platdata(dev);
967 return mmc_bind(dev, &plat->mmc, &plat->cfg);
970 U_BOOT_DRIVER(bcm2835_sdhost) = {
971 .name = "bcm2835-sdhost",
973 .of_match = bcm2835_match,
974 .bind = bcm2835_bind,
975 .probe = bcm2835_probe,
976 .priv_auto_alloc_size = sizeof(struct bcm2835_host),
977 .platdata_auto_alloc_size = sizeof(struct bcm2835_plat),