d44799b4af374475c9341b867bc9501f20855ca0
[oweals/u-boot.git] / drivers / mmc / bcm2835_sdhost.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcm2835 sdhost driver.
4  *
5  * The 2835 has two SD controllers: The Arasan sdhci controller
6  * (supported by the iproc driver) and a custom sdhost controller
7  * (supported by this driver).
8  *
9  * The sdhci controller supports both sdcard and sdio.  The sdhost
10  * controller supports the sdcard only, but has better performance.
11  * Also note that the rpi3 has sdio wifi, so driving the sdcard with
12  * the sdhost controller allows to use the sdhci controller for wifi
13  * support.
14  *
15  * The configuration is done by devicetree via pin muxing.  Both
16  * SD controller are available on the same pins (2 pin groups = pin 22
17  * to 27 + pin 48 to 53).  So it's possible to use both SD controllers
18  * at the same time with different pin groups.
19  *
20  * This code was ported to U-Boot by
21  *  Alexander Graf <agraf@suse.de>
22  * and is based on drivers/mmc/host/bcm2835.c in Linux which is written by
23  *  Phil Elwell <phil@raspberrypi.org>
24  *  Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
25  * which is based on
26  *  mmc-bcm2835.c by Gellert Weisz
27  * which is, in turn, based on
28  *  sdhci-bcm2708.c by Broadcom
29  *  sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
30  *  sdhci.c and sdhci-pci.c by Pierre Ossman
31  */
32 #include <clk.h>
33 #include <common.h>
34 #include <dm.h>
35 #include <mmc.h>
36 #include <asm/arch/msg.h>
37 #include <asm/arch/mbox.h>
38 #include <asm/unaligned.h>
39 #include <dm/device_compat.h>
40 #include <linux/bug.h>
41 #include <linux/compat.h>
42 #include <linux/io.h>
43 #include <linux/iopoll.h>
44 #include <linux/sizes.h>
45 #include <mach/gpio.h>
46 #include <power/regulator.h>
47
48 #define msleep(a) udelay(a * 1000)
49
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 */
65
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
74
75 #define SDCDIV_MAX_CDIV                 0x7ff
76
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
86
87 #define SDHSTS_CLEAR_MASK               (SDHSTS_BUSY_IRPT | \
88                                          SDHSTS_BLOCK_IRPT | \
89                                          SDHSTS_SDIO_IRPT | \
90                                          SDHSTS_REW_TIME_OUT | \
91                                          SDHSTS_CMD_TIME_OUT | \
92                                          SDHSTS_CRC16_ERROR | \
93                                          SDHSTS_CRC7_ERROR | \
94                                          SDHSTS_FIFO_ERROR)
95
96 #define SDHSTS_TRANSFER_ERROR_MASK      (SDHSTS_CRC7_ERROR | \
97                                          SDHSTS_CRC16_ERROR | \
98                                          SDHSTS_REW_TIME_OUT | \
99                                          SDHSTS_FIFO_ERROR)
100
101 #define SDHSTS_ERROR_MASK               (SDHSTS_CMD_TIME_OUT | \
102                                          SDHSTS_TRANSFER_ERROR_MASK)
103
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)
112
113 #define SDVDD_POWER_OFF         0
114 #define SDVDD_POWER_ON          1
115
116 #define SDEDM_FORCE_DATA_MODE   BIT(19)
117 #define SDEDM_CLOCK_PULSE       BIT(20)
118 #define SDEDM_BYPASS            BIT(21)
119
120 #define SDEDM_FIFO_FILL_SHIFT   4
121 #define SDEDM_FIFO_FILL_MASK    0x1f
122 static u32 edm_fifo_fill(u32 edm)
123 {
124         return (edm >> SDEDM_FIFO_FILL_SHIFT) & SDEDM_FIFO_FILL_MASK;
125 }
126
127 #define SDEDM_WRITE_THRESHOLD_SHIFT     9
128 #define SDEDM_READ_THRESHOLD_SHIFT      14
129 #define SDEDM_THRESHOLD_MASK            0x1f
130
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
147
148 #define SDDATA_FIFO_WORDS       16
149
150 #define FIFO_READ_THRESHOLD     4
151 #define FIFO_WRITE_THRESHOLD    4
152 #define SDDATA_FIFO_PIO_BURST   8
153
154 #define SDHST_TIMEOUT_MAX_USEC  100000
155
156 struct bcm2835_plat {
157         struct mmc_config cfg;
158         struct mmc mmc;
159 };
160
161 struct bcm2835_host {
162         void __iomem            *ioaddr;
163         u32                     phys_addr;
164
165         int                     clock;          /* Current clock speed */
166         unsigned int            max_clk;        /* Max possible freq */
167         unsigned int            blocks;         /* remaining PIO blocks */
168
169         u32                     ns_per_fifo_word;
170
171         /* cached registers */
172         u32                     hcfg;
173         u32                     cdiv;
174
175         struct mmc_cmd  *cmd;           /* Current command */
176         struct mmc_data         *data;          /* Current data request */
177         bool                    use_busy:1;     /* Wait for busy interrupt */
178
179         struct udevice          *dev;
180         struct mmc              *mmc;
181         struct bcm2835_plat     *plat;
182 };
183
184 static void bcm2835_dumpregs(struct bcm2835_host *host)
185 {
186         dev_dbg(dev, "=========== REGISTER DUMP ===========\n");
187         dev_dbg(dev, "SDCMD  0x%08x\n", readl(host->ioaddr + SDCMD));
188         dev_dbg(dev, "SDARG  0x%08x\n", readl(host->ioaddr + SDARG));
189         dev_dbg(dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
190         dev_dbg(dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
191         dev_dbg(dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
192         dev_dbg(dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
193         dev_dbg(dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
194         dev_dbg(dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
195         dev_dbg(dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
196         dev_dbg(dev, "SDVDD  0x%08x\n", readl(host->ioaddr + SDVDD));
197         dev_dbg(dev, "SDEDM  0x%08x\n", readl(host->ioaddr + SDEDM));
198         dev_dbg(dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
199         dev_dbg(dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
200         dev_dbg(dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
201         dev_dbg(dev, "===========================================\n");
202 }
203
204 static void bcm2835_reset_internal(struct bcm2835_host *host)
205 {
206         u32 temp;
207
208         writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
209         writel(0, host->ioaddr + SDCMD);
210         writel(0, host->ioaddr + SDARG);
211         /* Set timeout to a big enough value so we don't hit it */
212         writel(0xf00000, host->ioaddr + SDTOUT);
213         writel(0, host->ioaddr + SDCDIV);
214         /* Clear status register */
215         writel(SDHSTS_CLEAR_MASK, host->ioaddr + SDHSTS);
216         writel(0, host->ioaddr + SDHCFG);
217         writel(0, host->ioaddr + SDHBCT);
218         writel(0, host->ioaddr + SDHBLC);
219
220         /* Limit fifo usage due to silicon bug */
221         temp = readl(host->ioaddr + SDEDM);
222         temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) |
223                   (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT));
224         temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
225                 (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
226         writel(temp, host->ioaddr + SDEDM);
227         /* Wait for FIFO threshold to populate */
228         msleep(20);
229         writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
230         /* Wait for all components to go through power on cycle */
231         msleep(20);
232         host->clock = 0;
233         writel(host->hcfg, host->ioaddr + SDHCFG);
234         writel(host->cdiv, host->ioaddr + SDCDIV);
235 }
236
237 static int bcm2835_wait_transfer_complete(struct bcm2835_host *host)
238 {
239         ulong tstart_ms = get_timer(0);
240
241         while (1) {
242                 u32 edm, fsm;
243
244                 edm = readl(host->ioaddr + SDEDM);
245                 fsm = edm & SDEDM_FSM_MASK;
246
247                 if ((fsm == SDEDM_FSM_IDENTMODE) ||
248                     (fsm == SDEDM_FSM_DATAMODE))
249                         break;
250
251                 if ((fsm == SDEDM_FSM_READWAIT) ||
252                     (fsm == SDEDM_FSM_WRITESTART1) ||
253                     (fsm == SDEDM_FSM_READDATA)) {
254                         writel(edm | SDEDM_FORCE_DATA_MODE,
255                                host->ioaddr + SDEDM);
256                         break;
257                 }
258
259                 /* Error out after ~1s */
260                 ulong tlapse_ms = get_timer(tstart_ms);
261                 if ( tlapse_ms > 1000 /* ms */ ) {
262
263                         dev_err(host->dev,
264                                 "wait_transfer_complete - still waiting after %lu ms\n",
265                                 tlapse_ms);
266                         bcm2835_dumpregs(host);
267                         return -ETIMEDOUT;
268                 }
269         }
270
271         return 0;
272 }
273
274 static int bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
275 {
276         struct mmc_data *data = host->data;
277         size_t blksize = data->blocksize;
278         int copy_words;
279         u32 hsts = 0;
280         u32 *buf;
281
282         if (blksize % sizeof(u32))
283                 return -EINVAL;
284
285         buf = is_read ? (u32 *)data->dest : (u32 *)data->src;
286
287         if (is_read)
288                 data->dest += blksize;
289         else
290                 data->src += blksize;
291
292         copy_words = blksize / sizeof(u32);
293
294         /*
295          * Copy all contents from/to the FIFO as far as it reaches,
296          * then wait for it to fill/empty again and rewind.
297          */
298         while (copy_words) {
299                 int burst_words, words;
300                 u32 edm;
301
302                 burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words);
303                 edm = readl(host->ioaddr + SDEDM);
304                 if (is_read)
305                         words = edm_fifo_fill(edm);
306                 else
307                         words = SDDATA_FIFO_WORDS - edm_fifo_fill(edm);
308
309                 if (words < burst_words) {
310                         int fsm_state = (edm & SDEDM_FSM_MASK);
311
312                         if ((is_read &&
313                              (fsm_state != SDEDM_FSM_READDATA &&
314                               fsm_state != SDEDM_FSM_READWAIT &&
315                               fsm_state != SDEDM_FSM_READCRC)) ||
316                             (!is_read &&
317                              (fsm_state != SDEDM_FSM_WRITEDATA &&
318                               fsm_state != SDEDM_FSM_WRITEWAIT1 &&
319                               fsm_state != SDEDM_FSM_WRITEWAIT2 &&
320                               fsm_state != SDEDM_FSM_WRITECRC &&
321                               fsm_state != SDEDM_FSM_WRITESTART1 &&
322                               fsm_state != SDEDM_FSM_WRITESTART2))) {
323                                 hsts = readl(host->ioaddr + SDHSTS);
324                                 printf("fsm %x, hsts %08x\n", fsm_state, hsts);
325                                 if (hsts & SDHSTS_ERROR_MASK)
326                                         break;
327                         }
328
329                         continue;
330                 } else if (words > copy_words) {
331                         words = copy_words;
332                 }
333
334                 copy_words -= words;
335
336                 /* Copy current chunk to/from the FIFO */
337                 while (words) {
338                         if (is_read)
339                                 *(buf++) = readl(host->ioaddr + SDDATA);
340                         else
341                                 writel(*(buf++), host->ioaddr + SDDATA);
342                         words--;
343                 }
344         }
345
346         return 0;
347 }
348
349 static int bcm2835_transfer_pio(struct bcm2835_host *host)
350 {
351         u32 sdhsts;
352         bool is_read;
353         int ret = 0;
354
355         is_read = (host->data->flags & MMC_DATA_READ) != 0;
356         ret = bcm2835_transfer_block_pio(host, is_read);
357         if (ret)
358                 return ret;
359
360         sdhsts = readl(host->ioaddr + SDHSTS);
361         if (sdhsts & (SDHSTS_CRC16_ERROR |
362                       SDHSTS_CRC7_ERROR |
363                       SDHSTS_FIFO_ERROR)) {
364                 printf("%s transfer error - HSTS %08x\n",
365                        is_read ? "read" : "write", sdhsts);
366                 ret =  -EILSEQ;
367         } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
368                               SDHSTS_REW_TIME_OUT))) {
369                 printf("%s timeout error - HSTS %08x\n",
370                        is_read ? "read" : "write", sdhsts);
371                 ret = -ETIMEDOUT;
372         }
373
374         return ret;
375 }
376
377 static void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_cmd *cmd,
378                                  struct mmc_data *data)
379 {
380         WARN_ON(host->data);
381
382         host->data = data;
383         if (!data)
384                 return;
385
386         /* Use PIO */
387         host->blocks = data->blocks;
388
389         writel(data->blocksize, host->ioaddr + SDHBCT);
390         writel(data->blocks, host->ioaddr + SDHBLC);
391 }
392
393 static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host)
394 {
395         u32 value;
396         int ret;
397         int timeout_us = SDHST_TIMEOUT_MAX_USEC;
398
399         ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
400                                  !(value & SDCMD_NEW_FLAG), timeout_us);
401         if (ret == -ETIMEDOUT)
402                 printf("%s: timeout (%d us)\n", __func__, timeout_us);
403
404         return value;
405 }
406
407 static int bcm2835_send_command(struct bcm2835_host *host, struct mmc_cmd *cmd,
408                                 struct mmc_data *data)
409 {
410         u32 sdcmd, sdhsts;
411
412         WARN_ON(host->cmd);
413
414         if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) {
415                 printf("unsupported response type!\n");
416                 return -EINVAL;
417         }
418
419         sdcmd = bcm2835_read_wait_sdcmd(host);
420         if (sdcmd & SDCMD_NEW_FLAG) {
421                 printf("previous command never completed.\n");
422                 bcm2835_dumpregs(host);
423                 return -EBUSY;
424         }
425
426         host->cmd = cmd;
427
428         /* Clear any error flags */
429         sdhsts = readl(host->ioaddr + SDHSTS);
430         if (sdhsts & SDHSTS_ERROR_MASK)
431                 writel(sdhsts, host->ioaddr + SDHSTS);
432
433         bcm2835_prepare_data(host, cmd, data);
434
435         writel(cmd->cmdarg, host->ioaddr + SDARG);
436
437         sdcmd = cmd->cmdidx & SDCMD_CMD_MASK;
438
439         host->use_busy = false;
440         if (!(cmd->resp_type & MMC_RSP_PRESENT)) {
441                 sdcmd |= SDCMD_NO_RESPONSE;
442         } else {
443                 if (cmd->resp_type & MMC_RSP_136)
444                         sdcmd |= SDCMD_LONG_RESPONSE;
445                 if (cmd->resp_type & MMC_RSP_BUSY) {
446                         sdcmd |= SDCMD_BUSYWAIT;
447                         host->use_busy = true;
448                 }
449         }
450
451         if (data) {
452                 if (data->flags & MMC_DATA_WRITE)
453                         sdcmd |= SDCMD_WRITE_CMD;
454                 if (data->flags & MMC_DATA_READ)
455                         sdcmd |= SDCMD_READ_CMD;
456         }
457
458         writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
459
460         return 0;
461 }
462
463 static int bcm2835_finish_command(struct bcm2835_host *host)
464 {
465         struct mmc_cmd *cmd = host->cmd;
466         u32 sdcmd;
467         int ret = 0;
468
469         sdcmd = bcm2835_read_wait_sdcmd(host);
470
471         /* Check for errors */
472         if (sdcmd & SDCMD_NEW_FLAG) {
473                 printf("command never completed.\n");
474                 bcm2835_dumpregs(host);
475                 return -EIO;
476         } else if (sdcmd & SDCMD_FAIL_FLAG) {
477                 u32 sdhsts = readl(host->ioaddr + SDHSTS);
478
479                 /* Clear the errors */
480                 writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
481
482                 if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
483                     (host->cmd->cmdidx != MMC_CMD_SEND_OP_COND)) {
484                         if (sdhsts & SDHSTS_CMD_TIME_OUT) {
485                                 ret = -ETIMEDOUT;
486                         } else {
487                                 printf("unexpected command %d error\n",
488                                        host->cmd->cmdidx);
489                                 bcm2835_dumpregs(host);
490                                 ret = -EILSEQ;
491                         }
492
493                         return ret;
494                 }
495         }
496
497         if (cmd->resp_type & MMC_RSP_PRESENT) {
498                 if (cmd->resp_type & MMC_RSP_136) {
499                         int i;
500
501                         for (i = 0; i < 4; i++) {
502                                 cmd->response[3 - i] =
503                                         readl(host->ioaddr + SDRSP0 + i * 4);
504                         }
505                 } else {
506                         cmd->response[0] = readl(host->ioaddr + SDRSP0);
507                 }
508         }
509
510         /* Processed actual command. */
511         host->cmd = NULL;
512
513         return ret;
514 }
515
516 static int bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
517 {
518         int ret = -EINVAL;
519
520         if (!(intmask & SDHSTS_ERROR_MASK))
521                 return 0;
522
523         if (!host->cmd)
524                 return -EINVAL;
525
526         printf("sdhost_busy_irq: intmask %08x\n", intmask);
527         if (intmask & SDHSTS_CRC7_ERROR) {
528                 ret = -EILSEQ;
529         } else if (intmask & (SDHSTS_CRC16_ERROR |
530                               SDHSTS_FIFO_ERROR)) {
531                 ret = -EILSEQ;
532         } else if (intmask & (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT)) {
533                 ret = -ETIMEDOUT;
534         }
535         bcm2835_dumpregs(host);
536         return ret;
537 }
538
539 static int bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
540 {
541         int ret = 0;
542
543         if (!host->data)
544                 return 0;
545         if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
546                 ret = -EILSEQ;
547         if (intmask & SDHSTS_REW_TIME_OUT)
548                 ret = -ETIMEDOUT;
549
550         if (ret)
551                 printf("%s:%d %d\n", __func__, __LINE__, ret);
552
553         return ret;
554 }
555
556 static int bcm2835_transmit(struct bcm2835_host *host)
557 {
558         u32 intmask = readl(host->ioaddr + SDHSTS);
559         int ret;
560
561         /* Check for errors */
562         ret = bcm2835_check_data_error(host, intmask);
563         if (ret)
564                 return ret;
565
566         ret = bcm2835_check_cmd_error(host, intmask);
567         if (ret)
568                 return ret;
569
570         /* Handle wait for busy end */
571         if (host->use_busy && (intmask & SDHSTS_BUSY_IRPT)) {
572                 writel(SDHSTS_BUSY_IRPT, host->ioaddr + SDHSTS);
573                 host->use_busy = false;
574                 bcm2835_finish_command(host);
575         }
576
577         /* Handle PIO data transfer */
578         if (host->data) {
579                 ret = bcm2835_transfer_pio(host);
580                 if (ret)
581                         return ret;
582                 host->blocks--;
583                 if (host->blocks == 0) {
584                         /* Wait for command to complete for real */
585                         ret = bcm2835_wait_transfer_complete(host);
586                         if (ret)
587                                 return ret;
588                         /* Transfer complete */
589                         host->data = NULL;
590                 }
591         }
592
593         return 0;
594 }
595
596 static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
597 {
598         int div;
599
600         /* The SDCDIV register has 11 bits, and holds (div - 2).  But
601          * in data mode the max is 50MHz wihout a minimum, and only
602          * the bottom 3 bits are used. Since the switch over is
603          * automatic (unless we have marked the card as slow...),
604          * chosen values have to make sense in both modes.  Ident mode
605          * must be 100-400KHz, so can range check the requested
606          * clock. CMD15 must be used to return to data mode, so this
607          * can be monitored.
608          *
609          * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
610          *                 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
611          *
612          *               623->400KHz/27.8MHz
613          *               reset value (507)->491159/50MHz
614          *
615          * BUT, the 3-bit clock divisor in data mode is too small if
616          * the core clock is higher than 250MHz, so instead use the
617          * SLOW_CARD configuration bit to force the use of the ident
618          * clock divisor at all times.
619          */
620
621         if (clock < 100000) {
622                 /* Can't stop the clock, but make it as slow as possible
623                  * to show willing
624                  */
625                 host->cdiv = SDCDIV_MAX_CDIV;
626                 writel(host->cdiv, host->ioaddr + SDCDIV);
627                 return;
628         }
629
630         div = host->max_clk / clock;
631         if (div < 2)
632                 div = 2;
633         if ((host->max_clk / div) > clock)
634                 div++;
635         div -= 2;
636
637         if (div > SDCDIV_MAX_CDIV)
638                 div = SDCDIV_MAX_CDIV;
639
640         clock = host->max_clk / (div + 2);
641         host->mmc->clock = clock;
642
643         /* Calibrate some delays */
644
645         host->ns_per_fifo_word = (1000000000 / clock) *
646                 ((host->mmc->card_caps & MMC_MODE_4BIT) ? 8 : 32);
647
648         host->cdiv = div;
649         writel(host->cdiv, host->ioaddr + SDCDIV);
650
651         /* Set the timeout to 500ms */
652         writel(host->mmc->clock / 2, host->ioaddr + SDTOUT);
653 }
654
655 static inline int is_power_of_2(u64 x)
656 {
657         return !(x & (x - 1));
658 }
659
660 static int bcm2835_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
661                             struct mmc_data *data)
662 {
663         struct bcm2835_host *host = dev_get_priv(dev);
664         u32 edm, fsm;
665         int ret = 0;
666
667         if (data && !is_power_of_2(data->blocksize)) {
668                 printf("unsupported block size (%d bytes)\n", data->blocksize);
669
670                 if (cmd)
671                         return -EINVAL;
672         }
673
674         edm = readl(host->ioaddr + SDEDM);
675         fsm = edm & SDEDM_FSM_MASK;
676
677         if ((fsm != SDEDM_FSM_IDENTMODE) &&
678             (fsm != SDEDM_FSM_DATAMODE) &&
679             (cmd && cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) {
680                 printf("previous command (%d) not complete (EDM %08x)\n",
681                        readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK, edm);
682                 bcm2835_dumpregs(host);
683
684                 if (cmd)
685                         return -EILSEQ;
686
687                 return 0;
688         }
689
690         if (cmd) {
691                 ret = bcm2835_send_command(host, cmd, data);
692                 if (!ret && !host->use_busy)
693                         ret = bcm2835_finish_command(host);
694         }
695
696         /* Wait for completion of busy signal or data transfer */
697         while (host->use_busy || host->data) {
698                 ret = bcm2835_transmit(host);
699                 if (ret)
700                         break;
701         }
702
703         return ret;
704 }
705
706 static int bcm2835_set_ios(struct udevice *dev)
707 {
708         struct bcm2835_host *host = dev_get_priv(dev);
709         struct mmc *mmc = mmc_get_mmc_dev(dev);
710
711         if (!mmc->clock || mmc->clock != host->clock) {
712                 bcm2835_set_clock(host, mmc->clock);
713                 host->clock = mmc->clock;
714         }
715
716         /* set bus width */
717         host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
718         if (mmc->bus_width == 4)
719                 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
720
721         host->hcfg |= SDHCFG_WIDE_INT_BUS;
722
723         /* Disable clever clock switching, to cope with fast core clocks */
724         host->hcfg |= SDHCFG_SLOW_CARD;
725
726         writel(host->hcfg, host->ioaddr + SDHCFG);
727
728         return 0;
729 }
730
731 static void bcm2835_add_host(struct bcm2835_host *host)
732 {
733         struct mmc_config *cfg = &host->plat->cfg;
734
735         cfg->f_max = host->max_clk;
736         cfg->f_min = host->max_clk / SDCDIV_MAX_CDIV;
737         cfg->b_max = 65535;
738
739         dev_dbg(dev, "f_max %d, f_min %d\n",
740                 cfg->f_max, cfg->f_min);
741
742         /* host controller capabilities */
743         cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
744
745         /* report supported voltage ranges */
746         cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
747
748         /* Set interrupt enables */
749         host->hcfg = SDHCFG_BUSY_IRPT_EN;
750
751         bcm2835_reset_internal(host);
752 }
753
754 static int bcm2835_probe(struct udevice *dev)
755 {
756         struct bcm2835_plat *plat = dev_get_platdata(dev);
757         struct bcm2835_host *host = dev_get_priv(dev);
758         struct mmc *mmc = mmc_get_mmc_dev(dev);
759         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
760
761         host->dev = dev;
762         host->mmc = mmc;
763         host->plat = plat;
764         upriv->mmc = &plat->mmc;
765         plat->cfg.name = dev->name;
766
767         host->phys_addr = devfdt_get_addr(dev);
768         if (host->phys_addr == FDT_ADDR_T_NONE)
769                 return -EINVAL;
770
771         host->ioaddr = devm_ioremap(dev, host->phys_addr, SZ_256);
772         if (!host->ioaddr)
773                 return -ENOMEM;
774
775         host->max_clk = bcm2835_get_mmc_clock(BCM2835_MBOX_CLOCK_ID_CORE);
776
777         bcm2835_add_host(host);
778
779         dev_dbg(dev, "%s -> OK\n", __func__);
780
781         return 0;
782 }
783
784 static const struct udevice_id bcm2835_match[] = {
785         { .compatible = "brcm,bcm2835-sdhost" },
786         { }
787 };
788
789 static const struct dm_mmc_ops bcm2835_ops = {
790         .send_cmd = bcm2835_send_cmd,
791         .set_ios = bcm2835_set_ios,
792 };
793
794 static int bcm2835_bind(struct udevice *dev)
795 {
796         struct bcm2835_plat *plat = dev_get_platdata(dev);
797
798         return mmc_bind(dev, &plat->mmc, &plat->cfg);
799 }
800
801 U_BOOT_DRIVER(bcm2835_sdhost) = {
802         .name = "bcm2835-sdhost",
803         .id = UCLASS_MMC,
804         .of_match = bcm2835_match,
805         .bind = bcm2835_bind,
806         .probe = bcm2835_probe,
807         .priv_auto_alloc_size = sizeof(struct bcm2835_host),
808         .platdata_auto_alloc_size = sizeof(struct bcm2835_plat),
809         .ops = &bcm2835_ops,
810 };