blob: 720127468d373499d04d4f27aeee4d573a8d8533 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Alexander Graf044725f2018-01-23 18:05:22 +01002/*
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
Alexander Graf044725f2018-01-23 18:05:22 +010031 */
32#include <clk.h>
Alexander Graf044725f2018-01-23 18:05:22 +010033#include <dm.h>
34#include <mmc.h>
35#include <asm/arch/msg.h>
Jonathan Grayf98c4852018-03-17 16:15:48 +110036#include <asm/arch/mbox.h>
Alexander Graf044725f2018-01-23 18:05:22 +010037#include <asm/unaligned.h>
Simon Glass9bc15642020-02-03 07:36:16 -070038#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060039#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060040#include <linux/bug.h>
Alexander Graf044725f2018-01-23 18:05:22 +010041#include <linux/compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060042#include <linux/delay.h>
Alexander Graf044725f2018-01-23 18:05:22 +010043#include <linux/io.h>
44#include <linux/iopoll.h>
45#include <linux/sizes.h>
46#include <mach/gpio.h>
47#include <power/regulator.h>
48
Alexander Graf044725f2018-01-23 18:05:22 +010049#define msleep(a) udelay(a * 1000)
50
51#define SDCMD 0x00 /* Command to SD card - 16 R/W */
52#define SDARG 0x04 /* Argument to SD card - 32 R/W */
53#define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
54#define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
55#define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
56#define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
57#define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
58#define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
59#define SDHSTS 0x20 /* SD host status - 11 R/W */
60#define SDVDD 0x30 /* SD card power control - 1 R/W */
61#define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
62#define SDHCFG 0x38 /* Host configuration - 2 R/W */
63#define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
64#define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
65#define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
66
67#define SDCMD_NEW_FLAG 0x8000
68#define SDCMD_FAIL_FLAG 0x4000
69#define SDCMD_BUSYWAIT 0x800
70#define SDCMD_NO_RESPONSE 0x400
71#define SDCMD_LONG_RESPONSE 0x200
72#define SDCMD_WRITE_CMD 0x80
73#define SDCMD_READ_CMD 0x40
74#define SDCMD_CMD_MASK 0x3f
75
76#define SDCDIV_MAX_CDIV 0x7ff
77
78#define SDHSTS_BUSY_IRPT 0x400
79#define SDHSTS_BLOCK_IRPT 0x200
80#define SDHSTS_SDIO_IRPT 0x100
81#define SDHSTS_REW_TIME_OUT 0x80
82#define SDHSTS_CMD_TIME_OUT 0x40
83#define SDHSTS_CRC16_ERROR 0x20
84#define SDHSTS_CRC7_ERROR 0x10
85#define SDHSTS_FIFO_ERROR 0x08
86#define SDHSTS_DATA_FLAG 0x01
87
88#define SDHSTS_CLEAR_MASK (SDHSTS_BUSY_IRPT | \
89 SDHSTS_BLOCK_IRPT | \
90 SDHSTS_SDIO_IRPT | \
91 SDHSTS_REW_TIME_OUT | \
92 SDHSTS_CMD_TIME_OUT | \
93 SDHSTS_CRC16_ERROR | \
94 SDHSTS_CRC7_ERROR | \
95 SDHSTS_FIFO_ERROR)
96
97#define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR | \
98 SDHSTS_CRC16_ERROR | \
99 SDHSTS_REW_TIME_OUT | \
100 SDHSTS_FIFO_ERROR)
101
102#define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT | \
103 SDHSTS_TRANSFER_ERROR_MASK)
104
105#define SDHCFG_BUSY_IRPT_EN BIT(10)
106#define SDHCFG_BLOCK_IRPT_EN BIT(8)
107#define SDHCFG_SDIO_IRPT_EN BIT(5)
108#define SDHCFG_DATA_IRPT_EN BIT(4)
109#define SDHCFG_SLOW_CARD BIT(3)
110#define SDHCFG_WIDE_EXT_BUS BIT(2)
111#define SDHCFG_WIDE_INT_BUS BIT(1)
112#define SDHCFG_REL_CMD_LINE BIT(0)
113
114#define SDVDD_POWER_OFF 0
115#define SDVDD_POWER_ON 1
116
117#define SDEDM_FORCE_DATA_MODE BIT(19)
118#define SDEDM_CLOCK_PULSE BIT(20)
119#define SDEDM_BYPASS BIT(21)
120
121#define SDEDM_FIFO_FILL_SHIFT 4
122#define SDEDM_FIFO_FILL_MASK 0x1f
123static u32 edm_fifo_fill(u32 edm)
124{
125 return (edm >> SDEDM_FIFO_FILL_SHIFT) & SDEDM_FIFO_FILL_MASK;
126}
127
128#define SDEDM_WRITE_THRESHOLD_SHIFT 9
129#define SDEDM_READ_THRESHOLD_SHIFT 14
130#define SDEDM_THRESHOLD_MASK 0x1f
131
132#define SDEDM_FSM_MASK 0xf
133#define SDEDM_FSM_IDENTMODE 0x0
134#define SDEDM_FSM_DATAMODE 0x1
135#define SDEDM_FSM_READDATA 0x2
136#define SDEDM_FSM_WRITEDATA 0x3
137#define SDEDM_FSM_READWAIT 0x4
138#define SDEDM_FSM_READCRC 0x5
139#define SDEDM_FSM_WRITECRC 0x6
140#define SDEDM_FSM_WRITEWAIT1 0x7
141#define SDEDM_FSM_POWERDOWN 0x8
142#define SDEDM_FSM_POWERUP 0x9
143#define SDEDM_FSM_WRITESTART1 0xa
144#define SDEDM_FSM_WRITESTART2 0xb
145#define SDEDM_FSM_GENPULSES 0xc
146#define SDEDM_FSM_WRITEWAIT2 0xd
147#define SDEDM_FSM_STARTPOWDOWN 0xf
148
149#define SDDATA_FIFO_WORDS 16
150
151#define FIFO_READ_THRESHOLD 4
152#define FIFO_WRITE_THRESHOLD 4
153#define SDDATA_FIFO_PIO_BURST 8
154
155#define SDHST_TIMEOUT_MAX_USEC 100000
156
157struct bcm2835_plat {
158 struct mmc_config cfg;
159 struct mmc mmc;
160};
161
162struct bcm2835_host {
163 void __iomem *ioaddr;
164 u32 phys_addr;
165
166 int clock; /* Current clock speed */
167 unsigned int max_clk; /* Max possible freq */
168 unsigned int blocks; /* remaining PIO blocks */
Alexander Graf044725f2018-01-23 18:05:22 +0100169
170 u32 ns_per_fifo_word;
171
172 /* cached registers */
173 u32 hcfg;
174 u32 cdiv;
175
176 struct mmc_cmd *cmd; /* Current command */
177 struct mmc_data *data; /* Current data request */
Alexander Graf044725f2018-01-23 18:05:22 +0100178 bool use_busy:1; /* Wait for busy interrupt */
Alexander Graf044725f2018-01-23 18:05:22 +0100179
180 struct udevice *dev;
181 struct mmc *mmc;
182 struct bcm2835_plat *plat;
Vincent Fazioc9c95392021-09-14 13:19:19 -0500183 unsigned int firmware_sets_cdiv:1;
Alexander Graf044725f2018-01-23 18:05:22 +0100184};
185
186static void bcm2835_dumpregs(struct bcm2835_host *host)
187{
Sean Anderson988e42c2020-09-15 10:44:46 -0400188 dev_dbg(host->dev, "=========== REGISTER DUMP ===========\n");
189 dev_dbg(host->dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD));
190 dev_dbg(host->dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG));
191 dev_dbg(host->dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
192 dev_dbg(host->dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
193 dev_dbg(host->dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
194 dev_dbg(host->dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
195 dev_dbg(host->dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
196 dev_dbg(host->dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
197 dev_dbg(host->dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
198 dev_dbg(host->dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD));
199 dev_dbg(host->dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM));
200 dev_dbg(host->dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
201 dev_dbg(host->dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
202 dev_dbg(host->dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
203 dev_dbg(host->dev, "===========================================\n");
Alexander Graf044725f2018-01-23 18:05:22 +0100204}
205
206static void bcm2835_reset_internal(struct bcm2835_host *host)
207{
208 u32 temp;
209
210 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
211 writel(0, host->ioaddr + SDCMD);
212 writel(0, host->ioaddr + SDARG);
213 /* Set timeout to a big enough value so we don't hit it */
214 writel(0xf00000, host->ioaddr + SDTOUT);
215 writel(0, host->ioaddr + SDCDIV);
216 /* Clear status register */
217 writel(SDHSTS_CLEAR_MASK, host->ioaddr + SDHSTS);
218 writel(0, host->ioaddr + SDHCFG);
219 writel(0, host->ioaddr + SDHBCT);
220 writel(0, host->ioaddr + SDHBLC);
221
222 /* Limit fifo usage due to silicon bug */
223 temp = readl(host->ioaddr + SDEDM);
224 temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) |
225 (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT));
226 temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
227 (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
228 writel(temp, host->ioaddr + SDEDM);
229 /* Wait for FIFO threshold to populate */
230 msleep(20);
231 writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
232 /* Wait for all components to go through power on cycle */
233 msleep(20);
234 host->clock = 0;
235 writel(host->hcfg, host->ioaddr + SDHCFG);
Vincent Fazioc9c95392021-09-14 13:19:19 -0500236 writel(SDCDIV_MAX_CDIV, host->ioaddr + SDCDIV);
Alexander Graf044725f2018-01-23 18:05:22 +0100237}
238
Alexander Graf331a0662018-05-23 22:24:51 +0200239static int bcm2835_wait_transfer_complete(struct bcm2835_host *host)
Alexander Graf044725f2018-01-23 18:05:22 +0100240{
Raul Benet18bd0612019-06-13 14:59:57 +0100241 ulong tstart_ms = get_timer(0);
Alexander Graf044725f2018-01-23 18:05:22 +0100242
243 while (1) {
244 u32 edm, fsm;
245
246 edm = readl(host->ioaddr + SDEDM);
247 fsm = edm & SDEDM_FSM_MASK;
248
249 if ((fsm == SDEDM_FSM_IDENTMODE) ||
250 (fsm == SDEDM_FSM_DATAMODE))
251 break;
Alexander Graf331a0662018-05-23 22:24:51 +0200252
253 if ((fsm == SDEDM_FSM_READWAIT) ||
254 (fsm == SDEDM_FSM_WRITESTART1) ||
255 (fsm == SDEDM_FSM_READDATA)) {
Alexander Graf044725f2018-01-23 18:05:22 +0100256 writel(edm | SDEDM_FORCE_DATA_MODE,
257 host->ioaddr + SDEDM);
258 break;
259 }
260
Raul Benet18bd0612019-06-13 14:59:57 +0100261 /* Error out after ~1s */
262 ulong tlapse_ms = get_timer(tstart_ms);
263 if ( tlapse_ms > 1000 /* ms */ ) {
264
Alexander Graf044725f2018-01-23 18:05:22 +0100265 dev_err(host->dev,
Raul Benet18bd0612019-06-13 14:59:57 +0100266 "wait_transfer_complete - still waiting after %lu ms\n",
267 tlapse_ms);
Alexander Graf044725f2018-01-23 18:05:22 +0100268 bcm2835_dumpregs(host);
Alexander Graf331a0662018-05-23 22:24:51 +0200269 return -ETIMEDOUT;
Alexander Graf044725f2018-01-23 18:05:22 +0100270 }
271 }
Alexander Graf331a0662018-05-23 22:24:51 +0200272
273 return 0;
Alexander Graf044725f2018-01-23 18:05:22 +0100274}
275
276static int bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
277{
278 struct mmc_data *data = host->data;
279 size_t blksize = data->blocksize;
280 int copy_words;
281 u32 hsts = 0;
282 u32 *buf;
283
284 if (blksize % sizeof(u32))
285 return -EINVAL;
286
287 buf = is_read ? (u32 *)data->dest : (u32 *)data->src;
288
289 if (is_read)
290 data->dest += blksize;
291 else
292 data->src += blksize;
293
294 copy_words = blksize / sizeof(u32);
295
296 /*
297 * Copy all contents from/to the FIFO as far as it reaches,
298 * then wait for it to fill/empty again and rewind.
299 */
300 while (copy_words) {
301 int burst_words, words;
302 u32 edm;
303
304 burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words);
305 edm = readl(host->ioaddr + SDEDM);
306 if (is_read)
307 words = edm_fifo_fill(edm);
308 else
309 words = SDDATA_FIFO_WORDS - edm_fifo_fill(edm);
310
311 if (words < burst_words) {
312 int fsm_state = (edm & SDEDM_FSM_MASK);
313
314 if ((is_read &&
315 (fsm_state != SDEDM_FSM_READDATA &&
316 fsm_state != SDEDM_FSM_READWAIT &&
317 fsm_state != SDEDM_FSM_READCRC)) ||
318 (!is_read &&
319 (fsm_state != SDEDM_FSM_WRITEDATA &&
Alexander Graf331a0662018-05-23 22:24:51 +0200320 fsm_state != SDEDM_FSM_WRITEWAIT1 &&
321 fsm_state != SDEDM_FSM_WRITEWAIT2 &&
322 fsm_state != SDEDM_FSM_WRITECRC &&
Alexander Graf044725f2018-01-23 18:05:22 +0100323 fsm_state != SDEDM_FSM_WRITESTART1 &&
324 fsm_state != SDEDM_FSM_WRITESTART2))) {
325 hsts = readl(host->ioaddr + SDHSTS);
326 printf("fsm %x, hsts %08x\n", fsm_state, hsts);
327 if (hsts & SDHSTS_ERROR_MASK)
328 break;
329 }
330
331 continue;
332 } else if (words > copy_words) {
333 words = copy_words;
334 }
335
336 copy_words -= words;
337
338 /* Copy current chunk to/from the FIFO */
339 while (words) {
340 if (is_read)
341 *(buf++) = readl(host->ioaddr + SDDATA);
342 else
343 writel(*(buf++), host->ioaddr + SDDATA);
344 words--;
345 }
346 }
347
348 return 0;
349}
350
351static int bcm2835_transfer_pio(struct bcm2835_host *host)
352{
353 u32 sdhsts;
354 bool is_read;
355 int ret = 0;
356
357 is_read = (host->data->flags & MMC_DATA_READ) != 0;
358 ret = bcm2835_transfer_block_pio(host, is_read);
Alexander Graf331a0662018-05-23 22:24:51 +0200359 if (ret)
360 return ret;
Alexander Graf044725f2018-01-23 18:05:22 +0100361
362 sdhsts = readl(host->ioaddr + SDHSTS);
363 if (sdhsts & (SDHSTS_CRC16_ERROR |
364 SDHSTS_CRC7_ERROR |
365 SDHSTS_FIFO_ERROR)) {
366 printf("%s transfer error - HSTS %08x\n",
367 is_read ? "read" : "write", sdhsts);
368 ret = -EILSEQ;
369 } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
370 SDHSTS_REW_TIME_OUT))) {
371 printf("%s timeout error - HSTS %08x\n",
372 is_read ? "read" : "write", sdhsts);
373 ret = -ETIMEDOUT;
374 }
375
376 return ret;
377}
378
Alexander Graf331a0662018-05-23 22:24:51 +0200379static void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_cmd *cmd,
380 struct mmc_data *data)
Alexander Graf044725f2018-01-23 18:05:22 +0100381{
382 WARN_ON(host->data);
383
384 host->data = data;
385 if (!data)
386 return;
387
Alexander Graf044725f2018-01-23 18:05:22 +0100388 /* Use PIO */
389 host->blocks = data->blocks;
390
Alexander Graf044725f2018-01-23 18:05:22 +0100391 writel(data->blocksize, host->ioaddr + SDHBCT);
392 writel(data->blocks, host->ioaddr + SDHBLC);
393}
394
395static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host)
396{
397 u32 value;
398 int ret;
399 int timeout_us = SDHST_TIMEOUT_MAX_USEC;
400
401 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
402 !(value & SDCMD_NEW_FLAG), timeout_us);
403 if (ret == -ETIMEDOUT)
404 printf("%s: timeout (%d us)\n", __func__, timeout_us);
405
406 return value;
407}
408
409static int bcm2835_send_command(struct bcm2835_host *host, struct mmc_cmd *cmd,
410 struct mmc_data *data)
411{
412 u32 sdcmd, sdhsts;
413
414 WARN_ON(host->cmd);
415
416 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) {
417 printf("unsupported response type!\n");
418 return -EINVAL;
419 }
420
421 sdcmd = bcm2835_read_wait_sdcmd(host);
422 if (sdcmd & SDCMD_NEW_FLAG) {
423 printf("previous command never completed.\n");
424 bcm2835_dumpregs(host);
425 return -EBUSY;
426 }
427
428 host->cmd = cmd;
429
430 /* Clear any error flags */
431 sdhsts = readl(host->ioaddr + SDHSTS);
432 if (sdhsts & SDHSTS_ERROR_MASK)
433 writel(sdhsts, host->ioaddr + SDHSTS);
434
435 bcm2835_prepare_data(host, cmd, data);
436
437 writel(cmd->cmdarg, host->ioaddr + SDARG);
438
439 sdcmd = cmd->cmdidx & SDCMD_CMD_MASK;
440
441 host->use_busy = false;
442 if (!(cmd->resp_type & MMC_RSP_PRESENT)) {
443 sdcmd |= SDCMD_NO_RESPONSE;
444 } else {
445 if (cmd->resp_type & MMC_RSP_136)
446 sdcmd |= SDCMD_LONG_RESPONSE;
447 if (cmd->resp_type & MMC_RSP_BUSY) {
448 sdcmd |= SDCMD_BUSYWAIT;
449 host->use_busy = true;
450 }
451 }
452
453 if (data) {
454 if (data->flags & MMC_DATA_WRITE)
455 sdcmd |= SDCMD_WRITE_CMD;
456 if (data->flags & MMC_DATA_READ)
457 sdcmd |= SDCMD_READ_CMD;
458 }
459
460 writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
461
462 return 0;
463}
464
Alexander Graf044725f2018-01-23 18:05:22 +0100465static int bcm2835_finish_command(struct bcm2835_host *host)
466{
467 struct mmc_cmd *cmd = host->cmd;
468 u32 sdcmd;
469 int ret = 0;
470
471 sdcmd = bcm2835_read_wait_sdcmd(host);
472
473 /* Check for errors */
474 if (sdcmd & SDCMD_NEW_FLAG) {
475 printf("command never completed.\n");
476 bcm2835_dumpregs(host);
477 return -EIO;
478 } else if (sdcmd & SDCMD_FAIL_FLAG) {
479 u32 sdhsts = readl(host->ioaddr + SDHSTS);
480
481 /* Clear the errors */
482 writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
483
484 if (!(sdhsts & SDHSTS_CRC7_ERROR) ||
485 (host->cmd->cmdidx != MMC_CMD_SEND_OP_COND)) {
486 if (sdhsts & SDHSTS_CMD_TIME_OUT) {
487 ret = -ETIMEDOUT;
488 } else {
489 printf("unexpected command %d error\n",
490 host->cmd->cmdidx);
491 bcm2835_dumpregs(host);
492 ret = -EILSEQ;
493 }
494
495 return ret;
496 }
497 }
498
499 if (cmd->resp_type & MMC_RSP_PRESENT) {
500 if (cmd->resp_type & MMC_RSP_136) {
501 int i;
502
503 for (i = 0; i < 4; i++) {
504 cmd->response[3 - i] =
505 readl(host->ioaddr + SDRSP0 + i * 4);
506 }
507 } else {
508 cmd->response[0] = readl(host->ioaddr + SDRSP0);
509 }
510 }
511
512 /* Processed actual command. */
513 host->cmd = NULL;
Alexander Graf044725f2018-01-23 18:05:22 +0100514
515 return ret;
516}
517
518static int bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
519{
520 int ret = -EINVAL;
521
522 if (!(intmask & SDHSTS_ERROR_MASK))
523 return 0;
524
525 if (!host->cmd)
526 return -EINVAL;
527
528 printf("sdhost_busy_irq: intmask %08x\n", intmask);
529 if (intmask & SDHSTS_CRC7_ERROR) {
530 ret = -EILSEQ;
531 } else if (intmask & (SDHSTS_CRC16_ERROR |
532 SDHSTS_FIFO_ERROR)) {
533 ret = -EILSEQ;
534 } else if (intmask & (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT)) {
535 ret = -ETIMEDOUT;
536 }
537 bcm2835_dumpregs(host);
538 return ret;
539}
540
541static int bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
542{
543 int ret = 0;
544
545 if (!host->data)
546 return 0;
547 if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR))
548 ret = -EILSEQ;
549 if (intmask & SDHSTS_REW_TIME_OUT)
550 ret = -ETIMEDOUT;
551
552 if (ret)
553 printf("%s:%d %d\n", __func__, __LINE__, ret);
554
555 return ret;
556}
557
Alexander Graf331a0662018-05-23 22:24:51 +0200558static int bcm2835_transmit(struct bcm2835_host *host)
Alexander Graf044725f2018-01-23 18:05:22 +0100559{
Alexander Graf331a0662018-05-23 22:24:51 +0200560 u32 intmask = readl(host->ioaddr + SDHSTS);
Alexander Graf044725f2018-01-23 18:05:22 +0100561 int ret;
562
Alexander Graf331a0662018-05-23 22:24:51 +0200563 /* Check for errors */
Alexander Graf044725f2018-01-23 18:05:22 +0100564 ret = bcm2835_check_data_error(host, intmask);
565 if (ret)
Alexander Graf331a0662018-05-23 22:24:51 +0200566 return ret;
Alexander Graf044725f2018-01-23 18:05:22 +0100567
Alexander Graf331a0662018-05-23 22:24:51 +0200568 ret = bcm2835_check_cmd_error(host, intmask);
569 if (ret)
570 return ret;
Alexander Graf044725f2018-01-23 18:05:22 +0100571
Alexander Graf331a0662018-05-23 22:24:51 +0200572 /* Handle wait for busy end */
573 if (host->use_busy && (intmask & SDHSTS_BUSY_IRPT)) {
574 writel(SDHSTS_BUSY_IRPT, host->ioaddr + SDHSTS);
575 host->use_busy = false;
576 bcm2835_finish_command(host);
Alexander Graf044725f2018-01-23 18:05:22 +0100577 }
578
Alexander Graf331a0662018-05-23 22:24:51 +0200579 /* Handle PIO data transfer */
580 if (host->data) {
581 ret = bcm2835_transfer_pio(host);
582 if (ret)
583 return ret;
584 host->blocks--;
585 if (host->blocks == 0) {
586 /* Wait for command to complete for real */
587 ret = bcm2835_wait_transfer_complete(host);
588 if (ret)
589 return ret;
590 /* Transfer complete */
591 host->data = NULL;
Alexander Graf044725f2018-01-23 18:05:22 +0100592 }
593 }
594
Alexander Graf331a0662018-05-23 22:24:51 +0200595 return 0;
Alexander Graf044725f2018-01-23 18:05:22 +0100596}
597
598static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
599{
600 int div;
Vincent Fazioc9c95392021-09-14 13:19:19 -0500601 u32 clock_rate[2] = { 0 };
Alexander Graf044725f2018-01-23 18:05:22 +0100602
603 /* The SDCDIV register has 11 bits, and holds (div - 2). But
604 * in data mode the max is 50MHz wihout a minimum, and only
605 * the bottom 3 bits are used. Since the switch over is
606 * automatic (unless we have marked the card as slow...),
607 * chosen values have to make sense in both modes. Ident mode
608 * must be 100-400KHz, so can range check the requested
609 * clock. CMD15 must be used to return to data mode, so this
610 * can be monitored.
611 *
612 * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
613 * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
614 *
615 * 623->400KHz/27.8MHz
616 * reset value (507)->491159/50MHz
617 *
618 * BUT, the 3-bit clock divisor in data mode is too small if
619 * the core clock is higher than 250MHz, so instead use the
620 * SLOW_CARD configuration bit to force the use of the ident
621 * clock divisor at all times.
622 */
623
Vincent Fazioc9c95392021-09-14 13:19:19 -0500624 if (host->firmware_sets_cdiv) {
625 bcm2835_set_sdhost_clock(clock, &clock_rate[0], &clock_rate[1]);
626 clock = max(clock_rate[0], clock_rate[1]);
627 } else {
628 if (clock < 100000) {
629 /* Can't stop the clock, but make it as slow as possible
630 * to show willing
631 */
632 host->cdiv = SDCDIV_MAX_CDIV;
633 writel(host->cdiv, host->ioaddr + SDCDIV);
634 return;
635 }
Alexander Graf044725f2018-01-23 18:05:22 +0100636
Vincent Fazioc9c95392021-09-14 13:19:19 -0500637 div = host->max_clk / clock;
638 if (div < 2)
639 div = 2;
640 if ((host->max_clk / div) > clock)
641 div++;
642 div -= 2;
Alexander Graf044725f2018-01-23 18:05:22 +0100643
Vincent Fazioc9c95392021-09-14 13:19:19 -0500644 if (div > SDCDIV_MAX_CDIV)
645 div = SDCDIV_MAX_CDIV;
Alexander Graf044725f2018-01-23 18:05:22 +0100646
Vincent Fazioc9c95392021-09-14 13:19:19 -0500647 clock = host->max_clk / (div + 2);
648 host->cdiv = div;
649 writel(host->cdiv, host->ioaddr + SDCDIV);
650 }
651
Alexander Graf044725f2018-01-23 18:05:22 +0100652 host->mmc->clock = clock;
653
654 /* Calibrate some delays */
655
656 host->ns_per_fifo_word = (1000000000 / clock) *
657 ((host->mmc->card_caps & MMC_MODE_4BIT) ? 8 : 32);
658
Alexander Graf044725f2018-01-23 18:05:22 +0100659 /* Set the timeout to 500ms */
660 writel(host->mmc->clock / 2, host->ioaddr + SDTOUT);
661}
662
663static inline int is_power_of_2(u64 x)
664{
665 return !(x & (x - 1));
666}
667
668static int bcm2835_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
669 struct mmc_data *data)
670{
671 struct bcm2835_host *host = dev_get_priv(dev);
672 u32 edm, fsm;
673 int ret = 0;
674
675 if (data && !is_power_of_2(data->blocksize)) {
676 printf("unsupported block size (%d bytes)\n", data->blocksize);
677
678 if (cmd)
679 return -EINVAL;
680 }
681
682 edm = readl(host->ioaddr + SDEDM);
683 fsm = edm & SDEDM_FSM_MASK;
684
685 if ((fsm != SDEDM_FSM_IDENTMODE) &&
686 (fsm != SDEDM_FSM_DATAMODE) &&
687 (cmd && cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) {
688 printf("previous command (%d) not complete (EDM %08x)\n",
689 readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK, edm);
690 bcm2835_dumpregs(host);
691
692 if (cmd)
693 return -EILSEQ;
694
695 return 0;
696 }
697
698 if (cmd) {
699 ret = bcm2835_send_command(host, cmd, data);
700 if (!ret && !host->use_busy)
701 ret = bcm2835_finish_command(host);
702 }
703
704 /* Wait for completion of busy signal or data transfer */
Alexander Graf331a0662018-05-23 22:24:51 +0200705 while (host->use_busy || host->data) {
706 ret = bcm2835_transmit(host);
707 if (ret)
708 break;
709 }
Alexander Graf044725f2018-01-23 18:05:22 +0100710
711 return ret;
712}
713
714static int bcm2835_set_ios(struct udevice *dev)
715{
716 struct bcm2835_host *host = dev_get_priv(dev);
717 struct mmc *mmc = mmc_get_mmc_dev(dev);
718
719 if (!mmc->clock || mmc->clock != host->clock) {
720 bcm2835_set_clock(host, mmc->clock);
721 host->clock = mmc->clock;
722 }
723
724 /* set bus width */
725 host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
726 if (mmc->bus_width == 4)
727 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
728
729 host->hcfg |= SDHCFG_WIDE_INT_BUS;
730
731 /* Disable clever clock switching, to cope with fast core clocks */
732 host->hcfg |= SDHCFG_SLOW_CARD;
733
734 writel(host->hcfg, host->ioaddr + SDHCFG);
735
736 return 0;
737}
738
739static void bcm2835_add_host(struct bcm2835_host *host)
740{
741 struct mmc_config *cfg = &host->plat->cfg;
742
743 cfg->f_max = host->max_clk;
744 cfg->f_min = host->max_clk / SDCDIV_MAX_CDIV;
745 cfg->b_max = 65535;
746
Sean Anderson988e42c2020-09-15 10:44:46 -0400747 dev_dbg(host->dev, "f_max %d, f_min %d\n",
Alexander Graf044725f2018-01-23 18:05:22 +0100748 cfg->f_max, cfg->f_min);
749
750 /* host controller capabilities */
751 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
752
753 /* report supported voltage ranges */
754 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
755
756 /* Set interrupt enables */
757 host->hcfg = SDHCFG_BUSY_IRPT_EN;
758
759 bcm2835_reset_internal(host);
760}
761
762static int bcm2835_probe(struct udevice *dev)
763{
Simon Glassfa20e932020-12-03 16:55:20 -0700764 struct bcm2835_plat *plat = dev_get_plat(dev);
Alexander Graf044725f2018-01-23 18:05:22 +0100765 struct bcm2835_host *host = dev_get_priv(dev);
766 struct mmc *mmc = mmc_get_mmc_dev(dev);
767 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Vincent Fazioc9c95392021-09-14 13:19:19 -0500768 u32 clock_rate[2] = { ~0 };
Alexander Graf044725f2018-01-23 18:05:22 +0100769
770 host->dev = dev;
771 host->mmc = mmc;
772 host->plat = plat;
773 upriv->mmc = &plat->mmc;
774 plat->cfg.name = dev->name;
775
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900776 host->phys_addr = dev_read_addr(dev);
Alexander Graf044725f2018-01-23 18:05:22 +0100777 if (host->phys_addr == FDT_ADDR_T_NONE)
778 return -EINVAL;
779
780 host->ioaddr = devm_ioremap(dev, host->phys_addr, SZ_256);
781 if (!host->ioaddr)
782 return -ENOMEM;
783
Jonathan Grayf98c4852018-03-17 16:15:48 +1100784 host->max_clk = bcm2835_get_mmc_clock(BCM2835_MBOX_CLOCK_ID_CORE);
Alexander Graf044725f2018-01-23 18:05:22 +0100785
Vincent Fazioc9c95392021-09-14 13:19:19 -0500786 bcm2835_set_sdhost_clock(0, &clock_rate[0], &clock_rate[1]);
787 host->firmware_sets_cdiv = (clock_rate[0] != ~0);
788
Alexander Graf044725f2018-01-23 18:05:22 +0100789 bcm2835_add_host(host);
790
791 dev_dbg(dev, "%s -> OK\n", __func__);
792
793 return 0;
794}
795
796static const struct udevice_id bcm2835_match[] = {
797 { .compatible = "brcm,bcm2835-sdhost" },
798 { }
799};
800
801static const struct dm_mmc_ops bcm2835_ops = {
802 .send_cmd = bcm2835_send_cmd,
803 .set_ios = bcm2835_set_ios,
804};
805
806static int bcm2835_bind(struct udevice *dev)
807{
Simon Glassfa20e932020-12-03 16:55:20 -0700808 struct bcm2835_plat *plat = dev_get_plat(dev);
Alexander Graf044725f2018-01-23 18:05:22 +0100809
810 return mmc_bind(dev, &plat->mmc, &plat->cfg);
811}
812
813U_BOOT_DRIVER(bcm2835_sdhost) = {
814 .name = "bcm2835-sdhost",
815 .id = UCLASS_MMC,
816 .of_match = bcm2835_match,
817 .bind = bcm2835_bind,
818 .probe = bcm2835_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700819 .priv_auto = sizeof(struct bcm2835_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700820 .plat_auto = sizeof(struct bcm2835_plat),
Alexander Graf044725f2018-01-23 18:05:22 +0100821 .ops = &bcm2835_ops,
822};