stm32_sdmmc2.c 18.1 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
/*
2
 * Copyright (c) 2018-2020, STMicroelectronics - All Rights Reserved
Yann Gautier's avatar
Yann Gautier committed
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
8
9
10
#include <assert.h>
#include <errno.h>
#include <string.h>

11
12
13
14
#include <libfdt.h>

#include <platform_def.h>

Yann Gautier's avatar
Yann Gautier committed
15
16
#include <arch.h>
#include <arch_helpers.h>
17
18
19
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/mmc.h>
20
#include <drivers/st/stm32_gpio.h>
21
#include <drivers/st/stm32_sdmmc2.h>
22
#include <drivers/st/stm32mp_reset.h>
23
24
25
26
#include <lib/mmio.h>
#include <lib/utils.h>
#include <plat/common/platform.h>

Yann Gautier's avatar
Yann Gautier committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Registers offsets */
#define SDMMC_POWER			0x00U
#define SDMMC_CLKCR			0x04U
#define SDMMC_ARGR			0x08U
#define SDMMC_CMDR			0x0CU
#define SDMMC_RESPCMDR			0x10U
#define SDMMC_RESP1R			0x14U
#define SDMMC_RESP2R			0x18U
#define SDMMC_RESP3R			0x1CU
#define SDMMC_RESP4R			0x20U
#define SDMMC_DTIMER			0x24U
#define SDMMC_DLENR			0x28U
#define SDMMC_DCTRLR			0x2CU
#define SDMMC_DCNTR			0x30U
#define SDMMC_STAR			0x34U
#define SDMMC_ICR			0x38U
#define SDMMC_MASKR			0x3CU
#define SDMMC_ACKTIMER			0x40U
#define SDMMC_IDMACTRLR			0x50U
#define SDMMC_IDMABSIZER		0x54U
#define SDMMC_IDMABASE0R		0x58U
#define SDMMC_IDMABASE1R		0x5CU
#define SDMMC_FIFOR			0x80U

/* SDMMC power control register */
#define SDMMC_POWER_PWRCTRL		GENMASK(1, 0)
#define SDMMC_POWER_DIRPOL		BIT(4)

/* SDMMC clock control register */
#define SDMMC_CLKCR_WIDBUS_4		BIT(14)
#define SDMMC_CLKCR_WIDBUS_8		BIT(15)
#define SDMMC_CLKCR_NEGEDGE		BIT(16)
#define SDMMC_CLKCR_HWFC_EN		BIT(17)
#define SDMMC_CLKCR_SELCLKRX_0		BIT(20)

/* SDMMC command register */
#define SDMMC_CMDR_CMDTRANS		BIT(6)
#define SDMMC_CMDR_CMDSTOP		BIT(7)
#define SDMMC_CMDR_WAITRESP		GENMASK(9, 8)
#define SDMMC_CMDR_WAITRESP_SHORT	BIT(8)
#define SDMMC_CMDR_WAITRESP_SHORT_NOCRC	BIT(9)
#define SDMMC_CMDR_CPSMEN		BIT(12)

/* SDMMC data control register */
#define SDMMC_DCTRLR_DTEN		BIT(0)
#define SDMMC_DCTRLR_DTDIR		BIT(1)
#define SDMMC_DCTRLR_DTMODE		GENMASK(3, 2)
#define SDMMC_DCTRLR_DBLOCKSIZE		GENMASK(7, 4)
75
#define SDMMC_DCTRLR_DBLOCKSIZE_SHIFT	4
Yann Gautier's avatar
Yann Gautier committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define SDMMC_DCTRLR_FIFORST		BIT(13)

#define SDMMC_DCTRLR_CLEAR_MASK		(SDMMC_DCTRLR_DTEN | \
					 SDMMC_DCTRLR_DTDIR | \
					 SDMMC_DCTRLR_DTMODE | \
					 SDMMC_DCTRLR_DBLOCKSIZE)

/* SDMMC status register */
#define SDMMC_STAR_CCRCFAIL		BIT(0)
#define SDMMC_STAR_DCRCFAIL		BIT(1)
#define SDMMC_STAR_CTIMEOUT		BIT(2)
#define SDMMC_STAR_DTIMEOUT		BIT(3)
#define SDMMC_STAR_TXUNDERR		BIT(4)
#define SDMMC_STAR_RXOVERR		BIT(5)
#define SDMMC_STAR_CMDREND		BIT(6)
#define SDMMC_STAR_CMDSENT		BIT(7)
#define SDMMC_STAR_DATAEND		BIT(8)
#define SDMMC_STAR_DBCKEND		BIT(10)
94
#define SDMMC_STAR_DPSMACT		BIT(12)
Yann Gautier's avatar
Yann Gautier committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#define SDMMC_STAR_RXFIFOHF		BIT(15)
#define SDMMC_STAR_RXFIFOE		BIT(19)
#define SDMMC_STAR_IDMATE		BIT(27)
#define SDMMC_STAR_IDMABTC		BIT(28)

/* SDMMC DMA control register */
#define SDMMC_IDMACTRLR_IDMAEN		BIT(0)

#define SDMMC_STATIC_FLAGS		(SDMMC_STAR_CCRCFAIL | \
					 SDMMC_STAR_DCRCFAIL | \
					 SDMMC_STAR_CTIMEOUT | \
					 SDMMC_STAR_DTIMEOUT | \
					 SDMMC_STAR_TXUNDERR | \
					 SDMMC_STAR_RXOVERR  | \
					 SDMMC_STAR_CMDREND  | \
					 SDMMC_STAR_CMDSENT  | \
					 SDMMC_STAR_DATAEND  | \
					 SDMMC_STAR_DBCKEND  | \
					 SDMMC_STAR_IDMATE   | \
					 SDMMC_STAR_IDMABTC)

116
#define TIMEOUT_US_1_MS			1000U
117
118
#define TIMEOUT_US_10_MS		10000U
#define TIMEOUT_US_1_S			1000000U
Yann Gautier's avatar
Yann Gautier committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#define DT_SDMMC2_COMPAT		"st,stm32-sdmmc2"

static void stm32_sdmmc2_init(void);
static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd);
static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd);
static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width);
static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size);
static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size);
static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size);

static const struct mmc_ops stm32_sdmmc2_ops = {
	.init		= stm32_sdmmc2_init,
	.send_cmd	= stm32_sdmmc2_send_cmd,
	.set_ios	= stm32_sdmmc2_set_ios,
	.prepare	= stm32_sdmmc2_prepare,
	.read		= stm32_sdmmc2_read,
	.write		= stm32_sdmmc2_write,
};

static struct stm32_sdmmc2_params sdmmc2_params;

#pragma weak plat_sdmmc2_use_dma
bool plat_sdmmc2_use_dma(unsigned int instance, unsigned int memory)
{
	return false;
}

static void stm32_sdmmc2_init(void)
{
	uint32_t clock_div;
150
	uint32_t freq = STM32MP_MMC_INIT_FREQ;
Yann Gautier's avatar
Yann Gautier committed
151
152
	uintptr_t base = sdmmc2_params.reg_base;

153
154
155
156
157
	if (sdmmc2_params.max_freq != 0U) {
		freq = MIN(sdmmc2_params.max_freq, freq);
	}

	clock_div = div_round_up(sdmmc2_params.clk_rate, freq * 2U);
Yann Gautier's avatar
Yann Gautier committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

	mmio_write_32(base + SDMMC_CLKCR, SDMMC_CLKCR_HWFC_EN | clock_div |
		      sdmmc2_params.negedge |
		      sdmmc2_params.pin_ckin);

	mmio_write_32(base + SDMMC_POWER,
		      SDMMC_POWER_PWRCTRL | sdmmc2_params.dirpol);

	mdelay(1);
}

static int stm32_sdmmc2_stop_transfer(void)
{
	struct mmc_cmd cmd_stop;

	zeromem(&cmd_stop, sizeof(struct mmc_cmd));

	cmd_stop.cmd_idx = MMC_CMD(12);
	cmd_stop.resp_type = MMC_RESPONSE_R1B;

	return stm32_sdmmc2_send_cmd(&cmd_stop);
}

static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd)
{
183
	uint64_t timeout;
Yann Gautier's avatar
Yann Gautier committed
184
185
186
187
	uint32_t flags_cmd, status;
	uint32_t flags_data = 0;
	int err = 0;
	uintptr_t base = sdmmc2_params.reg_base;
188
	unsigned int cmd_reg, arg_reg;
Yann Gautier's avatar
Yann Gautier committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

	if (cmd == NULL) {
		return -EINVAL;
	}

	flags_cmd = SDMMC_STAR_CTIMEOUT;
	arg_reg = cmd->cmd_arg;

	if ((mmio_read_32(base + SDMMC_CMDR) & SDMMC_CMDR_CPSMEN) != 0U) {
		mmio_write_32(base + SDMMC_CMDR, 0);
	}

	cmd_reg = cmd->cmd_idx | SDMMC_CMDR_CPSMEN;

	if (cmd->resp_type == 0U) {
		flags_cmd |= SDMMC_STAR_CMDSENT;
	}

	if ((cmd->resp_type & MMC_RSP_48) != 0U) {
		if ((cmd->resp_type & MMC_RSP_136) != 0U) {
			flags_cmd |= SDMMC_STAR_CMDREND;
			cmd_reg |= SDMMC_CMDR_WAITRESP;
		} else if ((cmd->resp_type & MMC_RSP_CRC) != 0U) {
			flags_cmd |= SDMMC_STAR_CMDREND | SDMMC_STAR_CCRCFAIL;
			cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT;
		} else {
			flags_cmd |= SDMMC_STAR_CMDREND;
			cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT_NOCRC;
		}
	}

	switch (cmd->cmd_idx) {
	case MMC_CMD(1):
		arg_reg |= OCR_POWERUP;
		break;
	case MMC_CMD(8):
		if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
			cmd_reg |= SDMMC_CMDR_CMDTRANS;
		}
		break;
	case MMC_CMD(12):
		cmd_reg |= SDMMC_CMDR_CMDSTOP;
		break;
	case MMC_CMD(17):
	case MMC_CMD(18):
		cmd_reg |= SDMMC_CMDR_CMDTRANS;
		if (sdmmc2_params.use_dma) {
			flags_data |= SDMMC_STAR_DCRCFAIL |
				      SDMMC_STAR_DTIMEOUT |
				      SDMMC_STAR_DATAEND |
				      SDMMC_STAR_RXOVERR |
				      SDMMC_STAR_IDMATE;
		}
		break;
	case MMC_ACMD(41):
		arg_reg |= OCR_3_2_3_3 | OCR_3_3_3_4;
		break;
	case MMC_ACMD(51):
		cmd_reg |= SDMMC_CMDR_CMDTRANS;
		if (sdmmc2_params.use_dma) {
			flags_data |= SDMMC_STAR_DCRCFAIL |
				      SDMMC_STAR_DTIMEOUT |
				      SDMMC_STAR_DATAEND |
				      SDMMC_STAR_RXOVERR |
				      SDMMC_STAR_IDMATE |
				      SDMMC_STAR_DBCKEND;
		}
		break;
	default:
		break;
	}

261
262
263
264
265
266
267
268
269
270
271
272
	mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);

	/*
	 * Clear the SDMMC_DCTRLR if the command does not await data.
	 * Skip CMD55 as the next command could be data related, and
	 * the register could have been set in prepare function.
	 */
	if (((cmd_reg & SDMMC_CMDR_CMDTRANS) == 0U) &&
	    (cmd->cmd_idx != MMC_CMD(55))) {
		mmio_write_32(base + SDMMC_DCTRLR, 0U);
	}

Yann Gautier's avatar
Yann Gautier committed
273
274
275
276
277
278
279
280
	if ((cmd->resp_type & MMC_RSP_BUSY) != 0U) {
		mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
	}

	mmio_write_32(base + SDMMC_ARGR, arg_reg);

	mmio_write_32(base + SDMMC_CMDR, cmd_reg);

281
	status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier's avatar
Yann Gautier committed
282

283
	timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier's avatar
Yann Gautier committed
284

285
	while ((status & flags_cmd) == 0U) {
286
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
287
288
289
			err = -ETIMEDOUT;
			ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
			      __func__, cmd->cmd_idx, status);
290
			goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
291
292
		}

293
294
295
296
		status = mmio_read_32(base + SDMMC_STAR);
	}

	if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
		if ((status & SDMMC_STAR_CTIMEOUT) != 0U) {
			err = -ETIMEDOUT;
			/*
			 * Those timeouts can occur, and framework will handle
			 * the retries. CMD8 is expected to return this timeout
			 * for eMMC
			 */
			if (!((cmd->cmd_idx == MMC_CMD(1)) ||
			      (cmd->cmd_idx == MMC_CMD(13)) ||
			      ((cmd->cmd_idx == MMC_CMD(8)) &&
			       (cmd->resp_type == MMC_RESPONSE_R7)))) {
				ERROR("%s: CTIMEOUT (cmd = %d,status = %x)\n",
				      __func__, cmd->cmd_idx, status);
			}
		} else {
			err = -EIO;
			ERROR("%s: CRCFAIL (cmd = %d,status = %x)\n",
			      __func__, cmd->cmd_idx, status);
		}
316
317

		goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
318
319
	}

320
	if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
		if ((cmd->cmd_idx == MMC_CMD(9)) &&
		    ((cmd_reg & SDMMC_CMDR_WAITRESP) == SDMMC_CMDR_WAITRESP)) {
			/* Need to invert response to match CSD structure */
			cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP4R);
			cmd->resp_data[1] = mmio_read_32(base + SDMMC_RESP3R);
			cmd->resp_data[2] = mmio_read_32(base + SDMMC_RESP2R);
			cmd->resp_data[3] = mmio_read_32(base + SDMMC_RESP1R);
		} else {
			cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP1R);
			if ((cmd_reg & SDMMC_CMDR_WAITRESP) ==
			    SDMMC_CMDR_WAITRESP) {
				cmd->resp_data[1] = mmio_read_32(base +
								 SDMMC_RESP2R);
				cmd->resp_data[2] = mmio_read_32(base +
								 SDMMC_RESP3R);
				cmd->resp_data[3] = mmio_read_32(base +
								 SDMMC_RESP4R);
			}
		}
	}

342
	if (flags_data == 0U) {
Yann Gautier's avatar
Yann Gautier committed
343
344
		mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);

345
		return 0;
Yann Gautier's avatar
Yann Gautier committed
346
347
	}

348
	status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier's avatar
Yann Gautier committed
349

350
	timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier's avatar
Yann Gautier committed
351

352
	while ((status & flags_data) == 0U) {
353
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
354
355
356
			ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
			      __func__, cmd->cmd_idx, status);
			err = -ETIMEDOUT;
357
			goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
358
		}
359
360
361

		status = mmio_read_32(base + SDMMC_STAR);
	};
Yann Gautier's avatar
Yann Gautier committed
362
363
364
365
366
367
368
369
370

	if ((status & (SDMMC_STAR_DTIMEOUT | SDMMC_STAR_DCRCFAIL |
		       SDMMC_STAR_TXUNDERR | SDMMC_STAR_RXOVERR |
		       SDMMC_STAR_IDMATE)) != 0U) {
		ERROR("%s: Error flag (cmd = %d,status = %x)\n", __func__,
		      cmd->cmd_idx, status);
		err = -EIO;
	}

371
err_exit:
Yann Gautier's avatar
Yann Gautier committed
372
373
374
	mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
	mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);

375
	if ((err != 0) && ((status & SDMMC_STAR_DPSMACT) != 0U)) {
376
377
378
379
380
		int ret_stop = stm32_sdmmc2_stop_transfer();

		if (ret_stop != 0) {
			return ret_stop;
		}
Yann Gautier's avatar
Yann Gautier committed
381
382
383
384
385
386
387
	}

	return err;
}

static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
{
388
389
	uint8_t retry;
	int err;
Yann Gautier's avatar
Yann Gautier committed
390
391
392

	assert(cmd != NULL);

393
	for (retry = 0U; retry < 3U; retry++) {
Yann Gautier's avatar
Yann Gautier committed
394
395
		err = stm32_sdmmc2_send_cmd_req(cmd);
		if (err == 0) {
396
			return 0;
Yann Gautier's avatar
Yann Gautier committed
397
398
399
400
401
402
403
404
		}

		if ((cmd->cmd_idx == MMC_CMD(1)) ||
		    (cmd->cmd_idx == MMC_CMD(13))) {
			return 0; /* Retry managed by framework */
		}

		/* Command 8 is expected to fail for eMMC */
405
406
407
		if (cmd->cmd_idx != MMC_CMD(8)) {
			WARN(" CMD%u, Retry: %u, Error: %d\n",
			     cmd->cmd_idx, retry + 1U, err);
Yann Gautier's avatar
Yann Gautier committed
408
409
		}

410
		udelay(10U);
Yann Gautier's avatar
Yann Gautier committed
411
412
413
414
415
416
417
418
419
	}

	return err;
}

static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width)
{
	uintptr_t base = sdmmc2_params.reg_base;
	uint32_t bus_cfg = 0;
420
	uint32_t clock_div, max_freq, freq;
Yann Gautier's avatar
Yann Gautier committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
	uint32_t clk_rate = sdmmc2_params.clk_rate;
	uint32_t max_bus_freq = sdmmc2_params.device_info->max_bus_freq;

	switch (width) {
	case MMC_BUS_WIDTH_1:
		break;
	case MMC_BUS_WIDTH_4:
		bus_cfg |= SDMMC_CLKCR_WIDBUS_4;
		break;
	case MMC_BUS_WIDTH_8:
		bus_cfg |= SDMMC_CLKCR_WIDBUS_8;
		break;
	default:
		panic();
		break;
	}

	if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
		if (max_bus_freq >= 52000000U) {
440
			max_freq = STM32MP_EMMC_HIGH_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
441
		} else {
442
			max_freq = STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
443
444
445
		}
	} else {
		if (max_bus_freq >= 50000000U) {
446
			max_freq = STM32MP_SD_HIGH_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
447
		} else {
448
			max_freq = STM32MP_SD_NORMAL_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
449
450
451
		}
	}

452
453
454
455
456
457
458
	if (sdmmc2_params.max_freq != 0U) {
		freq = MIN(sdmmc2_params.max_freq, max_freq);
	} else {
		freq = max_freq;
	}

	clock_div = div_round_up(clk_rate, freq * 2U);
Yann Gautier's avatar
Yann Gautier committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

	mmio_write_32(base + SDMMC_CLKCR,
		      SDMMC_CLKCR_HWFC_EN | clock_div | bus_cfg |
		      sdmmc2_params.negedge |
		      sdmmc2_params.pin_ckin);

	return 0;
}

static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size)
{
	struct mmc_cmd cmd;
	int ret;
	uintptr_t base = sdmmc2_params.reg_base;
	uint32_t data_ctrl = SDMMC_DCTRLR_DTDIR;
474
	uint32_t arg_size;
Yann Gautier's avatar
Yann Gautier committed
475

476
477
478
479
	assert(size != 0U);

	if (size > MMC_BLOCK_SIZE) {
		arg_size = MMC_BLOCK_SIZE;
Yann Gautier's avatar
Yann Gautier committed
480
	} else {
481
		arg_size = size;
Yann Gautier's avatar
Yann Gautier committed
482
483
484
485
486
487
488
489
490
	}

	sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);

	if (sdmmc2_params.use_dma) {
		inv_dcache_range(buf, size);
	}

	/* Prepare CMD 16*/
491
	mmio_write_32(base + SDMMC_DTIMER, 0);
Yann Gautier's avatar
Yann Gautier committed
492
493
494

	mmio_write_32(base + SDMMC_DLENR, 0);

495
	mmio_write_32(base + SDMMC_DCTRLR, 0);
Yann Gautier's avatar
Yann Gautier committed
496
497
498
499

	zeromem(&cmd, sizeof(struct mmc_cmd));

	cmd.cmd_idx = MMC_CMD(16);
500
	cmd.cmd_arg = arg_size;
Yann Gautier's avatar
Yann Gautier committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
	cmd.resp_type = MMC_RESPONSE_R1;

	ret = stm32_sdmmc2_send_cmd(&cmd);
	if (ret != 0) {
		ERROR("CMD16 failed\n");
		return ret;
	}

	/* Prepare data command */
	mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);

	mmio_write_32(base + SDMMC_DLENR, size);

	if (sdmmc2_params.use_dma) {
		mmio_write_32(base + SDMMC_IDMACTRLR,
			      SDMMC_IDMACTRLR_IDMAEN);
		mmio_write_32(base + SDMMC_IDMABASE0R, buf);

		flush_dcache_range(buf, size);
	}

522
523
	data_ctrl |= __builtin_ctz(arg_size) << SDMMC_DCTRLR_DBLOCKSIZE_SHIFT;

Yann Gautier's avatar
Yann Gautier committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	mmio_clrsetbits_32(base + SDMMC_DCTRLR,
			   SDMMC_DCTRLR_CLEAR_MASK,
			   data_ctrl);

	return 0;
}

static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size)
{
	uint32_t error_flags = SDMMC_STAR_RXOVERR | SDMMC_STAR_DCRCFAIL |
			       SDMMC_STAR_DTIMEOUT;
	uint32_t flags = error_flags | SDMMC_STAR_DATAEND;
	uint32_t status;
	uint32_t *buffer;
	uintptr_t base = sdmmc2_params.reg_base;
	uintptr_t fifo_reg = base + SDMMC_FIFOR;
540
	uint64_t timeout;
Yann Gautier's avatar
Yann Gautier committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	int ret;

	/* Assert buf is 4 bytes aligned */
	assert((buf & GENMASK(1, 0)) == 0U);

	buffer = (uint32_t *)buf;

	if (sdmmc2_params.use_dma) {
		inv_dcache_range(buf, size);

		return 0;
	}

	if (size <= MMC_BLOCK_SIZE) {
		flags |= SDMMC_STAR_DBCKEND;
	}

558
	timeout = timeout_init_us(TIMEOUT_US_1_S);
Yann Gautier's avatar
Yann Gautier committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

	do {
		status = mmio_read_32(base + SDMMC_STAR);

		if ((status & error_flags) != 0U) {
			ERROR("%s: Read error (status = %x)\n", __func__,
			      status);
			mmio_write_32(base + SDMMC_DCTRLR,
				      SDMMC_DCTRLR_FIFORST);

			mmio_write_32(base + SDMMC_ICR,
				      SDMMC_STATIC_FLAGS);

			ret = stm32_sdmmc2_stop_transfer();
			if (ret != 0) {
				return ret;
			}

			return -EIO;
		}

580
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
			ERROR("%s: timeout 1s (status = %x)\n",
			      __func__, status);
			mmio_write_32(base + SDMMC_ICR,
				      SDMMC_STATIC_FLAGS);

			ret = stm32_sdmmc2_stop_transfer();
			if (ret != 0) {
				return ret;
			}

			return -ETIMEDOUT;
		}

		if (size < (8U * sizeof(uint32_t))) {
			if ((mmio_read_32(base + SDMMC_DCNTR) > 0U) &&
			    ((status & SDMMC_STAR_RXFIFOE) == 0U)) {
				*buffer = mmio_read_32(fifo_reg);
				buffer++;
			}
		} else if ((status & SDMMC_STAR_RXFIFOHF) != 0U) {
			uint32_t count;

			/* Read data from SDMMC Rx FIFO */
			for (count = 0; count < 8U; count++) {
				*buffer = mmio_read_32(fifo_reg);
				buffer++;
			}
		}
	} while ((status & flags) == 0U);

	mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);

	if ((status & SDMMC_STAR_DPSMACT) != 0U) {
		WARN("%s: DPSMACT=1, send stop\n", __func__);
		return stm32_sdmmc2_stop_transfer();
	}

	return 0;
}

static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size)
{
	return 0;
}

static int stm32_sdmmc2_dt_get_config(void)
{
	int sdmmc_node;
	void *fdt = NULL;
	const fdt32_t *cuint;

	if (fdt_get_address(&fdt) == 0) {
		return -FDT_ERR_NOTFOUND;
	}

	if (fdt == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	sdmmc_node = fdt_node_offset_by_compatible(fdt, -1, DT_SDMMC2_COMPAT);

	while (sdmmc_node != -FDT_ERR_NOTFOUND) {
		cuint = fdt_getprop(fdt, sdmmc_node, "reg", NULL);
		if (cuint == NULL) {
			continue;
		}

		if (fdt32_to_cpu(*cuint) == sdmmc2_params.reg_base) {
			break;
		}

		sdmmc_node = fdt_node_offset_by_compatible(fdt, sdmmc_node,
							   DT_SDMMC2_COMPAT);
	}

	if (sdmmc_node == -FDT_ERR_NOTFOUND) {
		return -FDT_ERR_NOTFOUND;
	}

660
	if (fdt_get_status(sdmmc_node) == DT_DISABLED) {
Yann Gautier's avatar
Yann Gautier committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
		return -FDT_ERR_NOTFOUND;
	}

	if (dt_set_pinctrl_config(sdmmc_node) != 0) {
		return -FDT_ERR_BADVALUE;
	}

	cuint = fdt_getprop(fdt, sdmmc_node, "clocks", NULL);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	cuint++;
	sdmmc2_params.clock_id = fdt32_to_cpu(*cuint);

	cuint = fdt_getprop(fdt, sdmmc_node, "resets", NULL);
	if (cuint == NULL) {
		return -FDT_ERR_NOTFOUND;
	}

	cuint++;
	sdmmc2_params.reset_id = fdt32_to_cpu(*cuint);

684
	if ((fdt_getprop(fdt, sdmmc_node, "st,use-ckin", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
685
686
687
		sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
	}

688
	if ((fdt_getprop(fdt, sdmmc_node, "st,sig-dir", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
689
690
691
		sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
	}

692
	if ((fdt_getprop(fdt, sdmmc_node, "st,neg-edge", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
		sdmmc2_params.negedge = SDMMC_CLKCR_NEGEDGE;
	}

	cuint = fdt_getprop(fdt, sdmmc_node, "bus-width", NULL);
	if (cuint != NULL) {
		switch (fdt32_to_cpu(*cuint)) {
		case 4:
			sdmmc2_params.bus_width = MMC_BUS_WIDTH_4;
			break;

		case 8:
			sdmmc2_params.bus_width = MMC_BUS_WIDTH_8;
			break;

		default:
			break;
		}
	}

712
713
714
715
716
	cuint = fdt_getprop(fdt, sdmmc_node, "max-frequency", NULL);
	if (cuint != NULL) {
		sdmmc2_params.max_freq = fdt32_to_cpu(*cuint);
	}

Yann Gautier's avatar
Yann Gautier committed
717
718
719
720
721
722
723
724
725
726
	return 0;
}

unsigned long long stm32_sdmmc2_mmc_get_device_size(void)
{
	return sdmmc2_params.device_info->device_size;
}

int stm32_sdmmc2_mmc_init(struct stm32_sdmmc2_params *params)
{
727
728
	int rc;

Yann Gautier's avatar
Yann Gautier committed
729
730
731
732
733
734
735
736
737
738
739
740
741
	assert((params != NULL) &&
	       ((params->reg_base & MMC_BLOCK_MASK) == 0U) &&
	       ((params->bus_width == MMC_BUS_WIDTH_1) ||
		(params->bus_width == MMC_BUS_WIDTH_4) ||
		(params->bus_width == MMC_BUS_WIDTH_8)));

	memcpy(&sdmmc2_params, params, sizeof(struct stm32_sdmmc2_params));

	if (stm32_sdmmc2_dt_get_config() != 0) {
		ERROR("%s: DT error\n", __func__);
		return -ENOMEM;
	}

Yann Gautier's avatar
Yann Gautier committed
742
	stm32mp_clk_enable(sdmmc2_params.clock_id);
Yann Gautier's avatar
Yann Gautier committed
743

744
745
746
747
	rc = stm32mp_reset_assert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
	if (rc != 0) {
		panic();
	}
Yann Gautier's avatar
Yann Gautier committed
748
	udelay(2);
749
750
751
752
	rc = stm32mp_reset_deassert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
	if (rc != 0) {
		panic();
	}
Yann Gautier's avatar
Yann Gautier committed
753
754
	mdelay(1);

755
	sdmmc2_params.clk_rate = stm32mp_clk_get_rate(sdmmc2_params.clock_id);
756
	sdmmc2_params.device_info->ocr_voltage = OCR_3_2_3_3 | OCR_3_3_3_4;
Yann Gautier's avatar
Yann Gautier committed
757
758
759
760
761

	return mmc_init(&stm32_sdmmc2_ops, sdmmc2_params.clk_rate,
			sdmmc2_params.bus_width, sdmmc2_params.flags,
			sdmmc2_params.device_info);
}