stm32_sdmmc2.c 17.8 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
261
262
263
264
265
266
267
268

	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;
	}

	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);

269
	status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier's avatar
Yann Gautier committed
270

271
	timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier's avatar
Yann Gautier committed
272

273
	while ((status & flags_cmd) == 0U) {
274
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
275
276
277
			err = -ETIMEDOUT;
			ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
			      __func__, cmd->cmd_idx, status);
278
			goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
279
280
		}

281
282
283
284
		status = mmio_read_32(base + SDMMC_STAR);
	}

	if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		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);
		}
304
305

		goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
306
307
	}

308
	if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
Yann Gautier's avatar
Yann Gautier committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
		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);
			}
		}
	}

330
	if (flags_data == 0U) {
Yann Gautier's avatar
Yann Gautier committed
331
332
		mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);

333
		return 0;
Yann Gautier's avatar
Yann Gautier committed
334
335
	}

336
	status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier's avatar
Yann Gautier committed
337

338
	timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier's avatar
Yann Gautier committed
339

340
	while ((status & flags_data) == 0U) {
341
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
342
343
344
			ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
			      __func__, cmd->cmd_idx, status);
			err = -ETIMEDOUT;
345
			goto err_exit;
Yann Gautier's avatar
Yann Gautier committed
346
		}
347
348
349

		status = mmio_read_32(base + SDMMC_STAR);
	};
Yann Gautier's avatar
Yann Gautier committed
350
351
352
353
354
355
356
357
358

	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;
	}

359
err_exit:
Yann Gautier's avatar
Yann Gautier committed
360
361
362
	mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
	mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);

363
	if ((err != 0) && ((status & SDMMC_STAR_DPSMACT) != 0U)) {
364
365
366
367
368
		int ret_stop = stm32_sdmmc2_stop_transfer();

		if (ret_stop != 0) {
			return ret_stop;
		}
Yann Gautier's avatar
Yann Gautier committed
369
370
371
372
373
374
375
	}

	return err;
}

static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
{
376
377
	uint8_t retry;
	int err;
Yann Gautier's avatar
Yann Gautier committed
378
379
380

	assert(cmd != NULL);

381
	for (retry = 0U; retry < 3U; retry++) {
Yann Gautier's avatar
Yann Gautier committed
382
383
		err = stm32_sdmmc2_send_cmd_req(cmd);
		if (err == 0) {
384
			return 0;
Yann Gautier's avatar
Yann Gautier committed
385
386
387
388
389
390
391
392
		}

		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 */
393
394
395
		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
396
397
		}

398
		udelay(10U);
Yann Gautier's avatar
Yann Gautier committed
399
400
401
402
403
404
405
406
407
	}

	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;
408
	uint32_t clock_div, max_freq, freq;
Yann Gautier's avatar
Yann Gautier committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
	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) {
428
			max_freq = STM32MP_EMMC_HIGH_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
429
		} else {
430
			max_freq = STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
431
432
433
		}
	} else {
		if (max_bus_freq >= 50000000U) {
434
			max_freq = STM32MP_SD_HIGH_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
435
		} else {
436
			max_freq = STM32MP_SD_NORMAL_SPEED_MAX_FREQ;
Yann Gautier's avatar
Yann Gautier committed
437
438
439
		}
	}

440
441
442
443
444
445
446
	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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

	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;
462
	uint32_t arg_size;
Yann Gautier's avatar
Yann Gautier committed
463

464
465
466
467
	assert(size != 0U);

	if (size > MMC_BLOCK_SIZE) {
		arg_size = MMC_BLOCK_SIZE;
Yann Gautier's avatar
Yann Gautier committed
468
	} else {
469
		arg_size = size;
Yann Gautier's avatar
Yann Gautier committed
470
471
472
473
474
475
476
477
478
	}

	sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);

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

	/* Prepare CMD 16*/
479
	mmio_write_32(base + SDMMC_DTIMER, 0);
Yann Gautier's avatar
Yann Gautier committed
480
481
482

	mmio_write_32(base + SDMMC_DLENR, 0);

483
	mmio_write_32(base + SDMMC_DCTRLR, 0);
Yann Gautier's avatar
Yann Gautier committed
484
485
486
487

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

	cmd.cmd_idx = MMC_CMD(16);
488
	cmd.cmd_arg = arg_size;
Yann Gautier's avatar
Yann Gautier committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
	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);
	}

510
511
	data_ctrl |= __builtin_ctz(arg_size) << SDMMC_DCTRLR_DBLOCKSIZE_SHIFT;

Yann Gautier's avatar
Yann Gautier committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
	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;
528
	uint64_t timeout;
Yann Gautier's avatar
Yann Gautier committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
	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;
	}

546
	timeout = timeout_init_us(TIMEOUT_US_1_S);
Yann Gautier's avatar
Yann Gautier committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

	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;
		}

568
		if (timeout_elapsed(timeout)) {
Yann Gautier's avatar
Yann Gautier committed
569
570
571
572
573
574
575
576
577
578
579
580
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
			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;
	}

648
	if (fdt_get_status(sdmmc_node) == DT_DISABLED) {
Yann Gautier's avatar
Yann Gautier committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
		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);

672
	if ((fdt_getprop(fdt, sdmmc_node, "st,use-ckin", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
673
674
675
		sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
	}

676
	if ((fdt_getprop(fdt, sdmmc_node, "st,sig-dir", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
677
678
679
		sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
	}

680
	if ((fdt_getprop(fdt, sdmmc_node, "st,neg-edge", NULL)) != NULL) {
Yann Gautier's avatar
Yann Gautier committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
		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;
		}
	}

700
701
702
703
704
	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
705
706
707
708
709
710
711
712
713
714
	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)
{
715
716
	int rc;

Yann Gautier's avatar
Yann Gautier committed
717
718
719
720
721
722
723
724
725
726
727
728
729
	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
730
	stm32mp_clk_enable(sdmmc2_params.clock_id);
Yann Gautier's avatar
Yann Gautier committed
731

732
733
734
735
	rc = stm32mp_reset_assert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
	if (rc != 0) {
		panic();
	}
Yann Gautier's avatar
Yann Gautier committed
736
	udelay(2);
737
738
739
740
	rc = stm32mp_reset_deassert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
	if (rc != 0) {
		panic();
	}
Yann Gautier's avatar
Yann Gautier committed
741
742
	mdelay(1);

743
	sdmmc2_params.clk_rate = stm32mp_clk_get_rate(sdmmc2_params.clock_id);
744
	sdmmc2_params.device_info->ocr_voltage = OCR_3_2_3_3 | OCR_3_3_3_4;
Yann Gautier's avatar
Yann Gautier committed
745
746
747
748
749

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