blob: 05f5ae17c9d802da9f0ad0edbb1b7cc62585992b [file] [log] [blame]
Yann Gautier5380b0d2018-10-15 09:36:04 +02001/*
2 * Copyright (c) 2018, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <errno.h>
9#include <string.h>
10
Yann Gautier57e282b2019-01-07 11:17:24 +010011#include <libfdt.h>
12
13#include <platform_def.h>
14
Yann Gautier5380b0d2018-10-15 09:36:04 +020015#include <arch.h>
16#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <common/debug.h>
18#include <drivers/delay_timer.h>
19#include <drivers/mmc.h>
20#include <drivers/st/stm32_sdmmc2.h>
21#include <drivers/st/stm32mp1_clk.h>
22#include <drivers/st/stm32mp1_rcc.h>
23#include <drivers/st/stm32mp1_reset.h>
Yann Gautier5380b0d2018-10-15 09:36:04 +020024#include <dt-bindings/clock/stm32mp1-clks.h>
25#include <dt-bindings/reset/stm32mp1-resets.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <lib/mmio.h>
27#include <lib/utils.h>
28#include <plat/common/platform.h>
29
Yann Gautier5380b0d2018-10-15 09:36:04 +020030/* Registers offsets */
31#define SDMMC_POWER 0x00U
32#define SDMMC_CLKCR 0x04U
33#define SDMMC_ARGR 0x08U
34#define SDMMC_CMDR 0x0CU
35#define SDMMC_RESPCMDR 0x10U
36#define SDMMC_RESP1R 0x14U
37#define SDMMC_RESP2R 0x18U
38#define SDMMC_RESP3R 0x1CU
39#define SDMMC_RESP4R 0x20U
40#define SDMMC_DTIMER 0x24U
41#define SDMMC_DLENR 0x28U
42#define SDMMC_DCTRLR 0x2CU
43#define SDMMC_DCNTR 0x30U
44#define SDMMC_STAR 0x34U
45#define SDMMC_ICR 0x38U
46#define SDMMC_MASKR 0x3CU
47#define SDMMC_ACKTIMER 0x40U
48#define SDMMC_IDMACTRLR 0x50U
49#define SDMMC_IDMABSIZER 0x54U
50#define SDMMC_IDMABASE0R 0x58U
51#define SDMMC_IDMABASE1R 0x5CU
52#define SDMMC_FIFOR 0x80U
53
54/* SDMMC power control register */
55#define SDMMC_POWER_PWRCTRL GENMASK(1, 0)
56#define SDMMC_POWER_DIRPOL BIT(4)
57
58/* SDMMC clock control register */
59#define SDMMC_CLKCR_WIDBUS_4 BIT(14)
60#define SDMMC_CLKCR_WIDBUS_8 BIT(15)
61#define SDMMC_CLKCR_NEGEDGE BIT(16)
62#define SDMMC_CLKCR_HWFC_EN BIT(17)
63#define SDMMC_CLKCR_SELCLKRX_0 BIT(20)
64
65/* SDMMC command register */
66#define SDMMC_CMDR_CMDTRANS BIT(6)
67#define SDMMC_CMDR_CMDSTOP BIT(7)
68#define SDMMC_CMDR_WAITRESP GENMASK(9, 8)
69#define SDMMC_CMDR_WAITRESP_SHORT BIT(8)
70#define SDMMC_CMDR_WAITRESP_SHORT_NOCRC BIT(9)
71#define SDMMC_CMDR_CPSMEN BIT(12)
72
73/* SDMMC data control register */
74#define SDMMC_DCTRLR_DTEN BIT(0)
75#define SDMMC_DCTRLR_DTDIR BIT(1)
76#define SDMMC_DCTRLR_DTMODE GENMASK(3, 2)
77#define SDMMC_DCTRLR_DBLOCKSIZE_0 BIT(4)
78#define SDMMC_DCTRLR_DBLOCKSIZE_1 BIT(5)
79#define SDMMC_DCTRLR_DBLOCKSIZE_3 BIT(7)
80#define SDMMC_DCTRLR_DBLOCKSIZE GENMASK(7, 4)
81#define SDMMC_DCTRLR_FIFORST BIT(13)
82
83#define SDMMC_DCTRLR_CLEAR_MASK (SDMMC_DCTRLR_DTEN | \
84 SDMMC_DCTRLR_DTDIR | \
85 SDMMC_DCTRLR_DTMODE | \
86 SDMMC_DCTRLR_DBLOCKSIZE)
87#define SDMMC_DBLOCKSIZE_8 (SDMMC_DCTRLR_DBLOCKSIZE_0 | \
88 SDMMC_DCTRLR_DBLOCKSIZE_1)
89#define SDMMC_DBLOCKSIZE_512 (SDMMC_DCTRLR_DBLOCKSIZE_0 | \
90 SDMMC_DCTRLR_DBLOCKSIZE_3)
91
92/* SDMMC status register */
93#define SDMMC_STAR_CCRCFAIL BIT(0)
94#define SDMMC_STAR_DCRCFAIL BIT(1)
95#define SDMMC_STAR_CTIMEOUT BIT(2)
96#define SDMMC_STAR_DTIMEOUT BIT(3)
97#define SDMMC_STAR_TXUNDERR BIT(4)
98#define SDMMC_STAR_RXOVERR BIT(5)
99#define SDMMC_STAR_CMDREND BIT(6)
100#define SDMMC_STAR_CMDSENT BIT(7)
101#define SDMMC_STAR_DATAEND BIT(8)
102#define SDMMC_STAR_DBCKEND BIT(10)
Yann Gautiere88fdd72018-11-30 15:22:11 +0100103#define SDMMC_STAR_DPSMACT BIT(12)
Yann Gautier5380b0d2018-10-15 09:36:04 +0200104#define SDMMC_STAR_RXFIFOHF BIT(15)
105#define SDMMC_STAR_RXFIFOE BIT(19)
106#define SDMMC_STAR_IDMATE BIT(27)
107#define SDMMC_STAR_IDMABTC BIT(28)
108
109/* SDMMC DMA control register */
110#define SDMMC_IDMACTRLR_IDMAEN BIT(0)
111
112#define SDMMC_STATIC_FLAGS (SDMMC_STAR_CCRCFAIL | \
113 SDMMC_STAR_DCRCFAIL | \
114 SDMMC_STAR_CTIMEOUT | \
115 SDMMC_STAR_DTIMEOUT | \
116 SDMMC_STAR_TXUNDERR | \
117 SDMMC_STAR_RXOVERR | \
118 SDMMC_STAR_CMDREND | \
119 SDMMC_STAR_CMDSENT | \
120 SDMMC_STAR_DATAEND | \
121 SDMMC_STAR_DBCKEND | \
122 SDMMC_STAR_IDMATE | \
123 SDMMC_STAR_IDMABTC)
124
125#define TIMEOUT_10_MS (plat_get_syscnt_freq2() / 100U)
126#define TIMEOUT_1_S plat_get_syscnt_freq2()
127
128#define DT_SDMMC2_COMPAT "st,stm32-sdmmc2"
129
130static void stm32_sdmmc2_init(void);
131static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd);
132static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd);
133static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width);
134static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size);
135static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size);
136static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size);
137
138static const struct mmc_ops stm32_sdmmc2_ops = {
139 .init = stm32_sdmmc2_init,
140 .send_cmd = stm32_sdmmc2_send_cmd,
141 .set_ios = stm32_sdmmc2_set_ios,
142 .prepare = stm32_sdmmc2_prepare,
143 .read = stm32_sdmmc2_read,
144 .write = stm32_sdmmc2_write,
145};
146
147static struct stm32_sdmmc2_params sdmmc2_params;
148
149#pragma weak plat_sdmmc2_use_dma
150bool plat_sdmmc2_use_dma(unsigned int instance, unsigned int memory)
151{
152 return false;
153}
154
155static void stm32_sdmmc2_init(void)
156{
157 uint32_t clock_div;
158 uintptr_t base = sdmmc2_params.reg_base;
159
160 clock_div = div_round_up(sdmmc2_params.clk_rate,
161 STM32MP1_MMC_INIT_FREQ * 2);
162
163 mmio_write_32(base + SDMMC_CLKCR, SDMMC_CLKCR_HWFC_EN | clock_div |
164 sdmmc2_params.negedge |
165 sdmmc2_params.pin_ckin);
166
167 mmio_write_32(base + SDMMC_POWER,
168 SDMMC_POWER_PWRCTRL | sdmmc2_params.dirpol);
169
170 mdelay(1);
171}
172
173static int stm32_sdmmc2_stop_transfer(void)
174{
175 struct mmc_cmd cmd_stop;
176
177 zeromem(&cmd_stop, sizeof(struct mmc_cmd));
178
179 cmd_stop.cmd_idx = MMC_CMD(12);
180 cmd_stop.resp_type = MMC_RESPONSE_R1B;
181
182 return stm32_sdmmc2_send_cmd(&cmd_stop);
183}
184
185static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd)
186{
187 uint32_t flags_cmd, status;
188 uint32_t flags_data = 0;
189 int err = 0;
190 uintptr_t base = sdmmc2_params.reg_base;
191 unsigned int cmd_reg, arg_reg, start;
192
193 if (cmd == NULL) {
194 return -EINVAL;
195 }
196
197 flags_cmd = SDMMC_STAR_CTIMEOUT;
198 arg_reg = cmd->cmd_arg;
199
200 if ((mmio_read_32(base + SDMMC_CMDR) & SDMMC_CMDR_CPSMEN) != 0U) {
201 mmio_write_32(base + SDMMC_CMDR, 0);
202 }
203
204 cmd_reg = cmd->cmd_idx | SDMMC_CMDR_CPSMEN;
205
206 if (cmd->resp_type == 0U) {
207 flags_cmd |= SDMMC_STAR_CMDSENT;
208 }
209
210 if ((cmd->resp_type & MMC_RSP_48) != 0U) {
211 if ((cmd->resp_type & MMC_RSP_136) != 0U) {
212 flags_cmd |= SDMMC_STAR_CMDREND;
213 cmd_reg |= SDMMC_CMDR_WAITRESP;
214 } else if ((cmd->resp_type & MMC_RSP_CRC) != 0U) {
215 flags_cmd |= SDMMC_STAR_CMDREND | SDMMC_STAR_CCRCFAIL;
216 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT;
217 } else {
218 flags_cmd |= SDMMC_STAR_CMDREND;
219 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT_NOCRC;
220 }
221 }
222
223 switch (cmd->cmd_idx) {
224 case MMC_CMD(1):
225 arg_reg |= OCR_POWERUP;
226 break;
227 case MMC_CMD(8):
228 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
229 cmd_reg |= SDMMC_CMDR_CMDTRANS;
230 }
231 break;
232 case MMC_CMD(12):
233 cmd_reg |= SDMMC_CMDR_CMDSTOP;
234 break;
235 case MMC_CMD(17):
236 case MMC_CMD(18):
237 cmd_reg |= SDMMC_CMDR_CMDTRANS;
238 if (sdmmc2_params.use_dma) {
239 flags_data |= SDMMC_STAR_DCRCFAIL |
240 SDMMC_STAR_DTIMEOUT |
241 SDMMC_STAR_DATAEND |
242 SDMMC_STAR_RXOVERR |
243 SDMMC_STAR_IDMATE;
244 }
245 break;
246 case MMC_ACMD(41):
247 arg_reg |= OCR_3_2_3_3 | OCR_3_3_3_4;
248 break;
249 case MMC_ACMD(51):
250 cmd_reg |= SDMMC_CMDR_CMDTRANS;
251 if (sdmmc2_params.use_dma) {
252 flags_data |= SDMMC_STAR_DCRCFAIL |
253 SDMMC_STAR_DTIMEOUT |
254 SDMMC_STAR_DATAEND |
255 SDMMC_STAR_RXOVERR |
256 SDMMC_STAR_IDMATE |
257 SDMMC_STAR_DBCKEND;
258 }
259 break;
260 default:
261 break;
262 }
263
264 if ((cmd->resp_type & MMC_RSP_BUSY) != 0U) {
265 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
266 }
267
268 mmio_write_32(base + SDMMC_ARGR, arg_reg);
269
270 mmio_write_32(base + SDMMC_CMDR, cmd_reg);
271
Yann Gautiere88fdd72018-11-30 15:22:11 +0100272 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200273
Yann Gautiere88fdd72018-11-30 15:22:11 +0100274 start = get_timer(0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200275
Yann Gautiere88fdd72018-11-30 15:22:11 +0100276 while ((status & flags_cmd) == 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200277 if (get_timer(start) > TIMEOUT_10_MS) {
278 err = -ETIMEDOUT;
279 ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
280 __func__, cmd->cmd_idx, status);
Yann Gautiere88fdd72018-11-30 15:22:11 +0100281 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200282 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200283
Yann Gautiere88fdd72018-11-30 15:22:11 +0100284 status = mmio_read_32(base + SDMMC_STAR);
285 }
286
287 if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200288 if ((status & SDMMC_STAR_CTIMEOUT) != 0U) {
289 err = -ETIMEDOUT;
290 /*
291 * Those timeouts can occur, and framework will handle
292 * the retries. CMD8 is expected to return this timeout
293 * for eMMC
294 */
295 if (!((cmd->cmd_idx == MMC_CMD(1)) ||
296 (cmd->cmd_idx == MMC_CMD(13)) ||
297 ((cmd->cmd_idx == MMC_CMD(8)) &&
298 (cmd->resp_type == MMC_RESPONSE_R7)))) {
299 ERROR("%s: CTIMEOUT (cmd = %d,status = %x)\n",
300 __func__, cmd->cmd_idx, status);
301 }
302 } else {
303 err = -EIO;
304 ERROR("%s: CRCFAIL (cmd = %d,status = %x)\n",
305 __func__, cmd->cmd_idx, status);
306 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100307
308 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200309 }
310
Yann Gautiere88fdd72018-11-30 15:22:11 +0100311 if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200312 if ((cmd->cmd_idx == MMC_CMD(9)) &&
313 ((cmd_reg & SDMMC_CMDR_WAITRESP) == SDMMC_CMDR_WAITRESP)) {
314 /* Need to invert response to match CSD structure */
315 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP4R);
316 cmd->resp_data[1] = mmio_read_32(base + SDMMC_RESP3R);
317 cmd->resp_data[2] = mmio_read_32(base + SDMMC_RESP2R);
318 cmd->resp_data[3] = mmio_read_32(base + SDMMC_RESP1R);
319 } else {
320 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP1R);
321 if ((cmd_reg & SDMMC_CMDR_WAITRESP) ==
322 SDMMC_CMDR_WAITRESP) {
323 cmd->resp_data[1] = mmio_read_32(base +
324 SDMMC_RESP2R);
325 cmd->resp_data[2] = mmio_read_32(base +
326 SDMMC_RESP3R);
327 cmd->resp_data[3] = mmio_read_32(base +
328 SDMMC_RESP4R);
329 }
330 }
331 }
332
Yann Gautiere88fdd72018-11-30 15:22:11 +0100333 if (flags_data == 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200334 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
335
Yann Gautiere88fdd72018-11-30 15:22:11 +0100336 return 0;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200337 }
338
Yann Gautiere88fdd72018-11-30 15:22:11 +0100339 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200340
Yann Gautiere88fdd72018-11-30 15:22:11 +0100341 start = get_timer(0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200342
Yann Gautiere88fdd72018-11-30 15:22:11 +0100343 while ((status & flags_data) == 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200344 if (get_timer(start) > TIMEOUT_10_MS) {
345 ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
346 __func__, cmd->cmd_idx, status);
347 err = -ETIMEDOUT;
Yann Gautiere88fdd72018-11-30 15:22:11 +0100348 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200349 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100350
351 status = mmio_read_32(base + SDMMC_STAR);
352 };
Yann Gautier5380b0d2018-10-15 09:36:04 +0200353
354 if ((status & (SDMMC_STAR_DTIMEOUT | SDMMC_STAR_DCRCFAIL |
355 SDMMC_STAR_TXUNDERR | SDMMC_STAR_RXOVERR |
356 SDMMC_STAR_IDMATE)) != 0U) {
357 ERROR("%s: Error flag (cmd = %d,status = %x)\n", __func__,
358 cmd->cmd_idx, status);
359 err = -EIO;
360 }
361
Yann Gautiere88fdd72018-11-30 15:22:11 +0100362err_exit:
Yann Gautier5380b0d2018-10-15 09:36:04 +0200363 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
364 mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);
365
366 if (err != 0) {
Yann Gautiere88fdd72018-11-30 15:22:11 +0100367 int ret_stop = stm32_sdmmc2_stop_transfer();
368
369 if (ret_stop != 0) {
370 return ret_stop;
371 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200372 }
373
374 return err;
375}
376
377static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
378{
379 int8_t retry;
380 int err = 0;
381
382 assert(cmd != NULL);
383
384 for (retry = 0; retry <= 3; retry++) {
385 err = stm32_sdmmc2_send_cmd_req(cmd);
386 if (err == 0) {
387 return err;
388 }
389
390 if ((cmd->cmd_idx == MMC_CMD(1)) ||
391 (cmd->cmd_idx == MMC_CMD(13))) {
392 return 0; /* Retry managed by framework */
393 }
394
395 /* Command 8 is expected to fail for eMMC */
396 if (!(cmd->cmd_idx == MMC_CMD(8))) {
397 WARN(" CMD%d, Retry: %d, Error: %d\n",
398 cmd->cmd_idx, retry, err);
399 }
400
401 udelay(10);
402 }
403
404 return err;
405}
406
407static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width)
408{
409 uintptr_t base = sdmmc2_params.reg_base;
410 uint32_t bus_cfg = 0;
411 uint32_t clock_div, max_freq;
412 uint32_t clk_rate = sdmmc2_params.clk_rate;
413 uint32_t max_bus_freq = sdmmc2_params.device_info->max_bus_freq;
414
415 switch (width) {
416 case MMC_BUS_WIDTH_1:
417 break;
418 case MMC_BUS_WIDTH_4:
419 bus_cfg |= SDMMC_CLKCR_WIDBUS_4;
420 break;
421 case MMC_BUS_WIDTH_8:
422 bus_cfg |= SDMMC_CLKCR_WIDBUS_8;
423 break;
424 default:
425 panic();
426 break;
427 }
428
429 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
430 if (max_bus_freq >= 52000000U) {
431 max_freq = STM32MP1_EMMC_HIGH_SPEED_MAX_FREQ;
432 } else {
433 max_freq = STM32MP1_EMMC_NORMAL_SPEED_MAX_FREQ;
434 }
435 } else {
436 if (max_bus_freq >= 50000000U) {
437 max_freq = STM32MP1_SD_HIGH_SPEED_MAX_FREQ;
438 } else {
439 max_freq = STM32MP1_SD_NORMAL_SPEED_MAX_FREQ;
440 }
441 }
442
443 clock_div = div_round_up(clk_rate, max_freq * 2);
444
445 mmio_write_32(base + SDMMC_CLKCR,
446 SDMMC_CLKCR_HWFC_EN | clock_div | bus_cfg |
447 sdmmc2_params.negedge |
448 sdmmc2_params.pin_ckin);
449
450 return 0;
451}
452
453static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size)
454{
455 struct mmc_cmd cmd;
456 int ret;
457 uintptr_t base = sdmmc2_params.reg_base;
458 uint32_t data_ctrl = SDMMC_DCTRLR_DTDIR;
459
460 if (size == 8U) {
461 data_ctrl |= SDMMC_DBLOCKSIZE_8;
462 } else {
463 data_ctrl |= SDMMC_DBLOCKSIZE_512;
464 }
465
466 sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);
467
468 if (sdmmc2_params.use_dma) {
469 inv_dcache_range(buf, size);
470 }
471
472 /* Prepare CMD 16*/
473 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
474
475 mmio_write_32(base + SDMMC_DLENR, 0);
476
477 mmio_clrsetbits_32(base + SDMMC_DCTRLR,
478 SDMMC_DCTRLR_CLEAR_MASK, SDMMC_DCTRLR_DTDIR);
479
480 zeromem(&cmd, sizeof(struct mmc_cmd));
481
482 cmd.cmd_idx = MMC_CMD(16);
483 if (size > MMC_BLOCK_SIZE) {
484 cmd.cmd_arg = MMC_BLOCK_SIZE;
485 } else {
486 cmd.cmd_arg = size;
487 }
488
489 cmd.resp_type = MMC_RESPONSE_R1;
490
491 ret = stm32_sdmmc2_send_cmd(&cmd);
492 if (ret != 0) {
493 ERROR("CMD16 failed\n");
494 return ret;
495 }
496
497 /* Prepare data command */
498 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
499
500 mmio_write_32(base + SDMMC_DLENR, size);
501
502 if (sdmmc2_params.use_dma) {
503 mmio_write_32(base + SDMMC_IDMACTRLR,
504 SDMMC_IDMACTRLR_IDMAEN);
505 mmio_write_32(base + SDMMC_IDMABASE0R, buf);
506
507 flush_dcache_range(buf, size);
508 }
509
510 mmio_clrsetbits_32(base + SDMMC_DCTRLR,
511 SDMMC_DCTRLR_CLEAR_MASK,
512 data_ctrl);
513
514 return 0;
515}
516
517static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size)
518{
519 uint32_t error_flags = SDMMC_STAR_RXOVERR | SDMMC_STAR_DCRCFAIL |
520 SDMMC_STAR_DTIMEOUT;
521 uint32_t flags = error_flags | SDMMC_STAR_DATAEND;
522 uint32_t status;
523 uint32_t *buffer;
524 uintptr_t base = sdmmc2_params.reg_base;
525 uintptr_t fifo_reg = base + SDMMC_FIFOR;
526 unsigned int start;
527 int ret;
528
529 /* Assert buf is 4 bytes aligned */
530 assert((buf & GENMASK(1, 0)) == 0U);
531
532 buffer = (uint32_t *)buf;
533
534 if (sdmmc2_params.use_dma) {
535 inv_dcache_range(buf, size);
536
537 return 0;
538 }
539
540 if (size <= MMC_BLOCK_SIZE) {
541 flags |= SDMMC_STAR_DBCKEND;
542 }
543
544 start = get_timer(0);
545
546 do {
547 status = mmio_read_32(base + SDMMC_STAR);
548
549 if ((status & error_flags) != 0U) {
550 ERROR("%s: Read error (status = %x)\n", __func__,
551 status);
552 mmio_write_32(base + SDMMC_DCTRLR,
553 SDMMC_DCTRLR_FIFORST);
554
555 mmio_write_32(base + SDMMC_ICR,
556 SDMMC_STATIC_FLAGS);
557
558 ret = stm32_sdmmc2_stop_transfer();
559 if (ret != 0) {
560 return ret;
561 }
562
563 return -EIO;
564 }
565
566 if (get_timer(start) > TIMEOUT_1_S) {
567 ERROR("%s: timeout 1s (status = %x)\n",
568 __func__, status);
569 mmio_write_32(base + SDMMC_ICR,
570 SDMMC_STATIC_FLAGS);
571
572 ret = stm32_sdmmc2_stop_transfer();
573 if (ret != 0) {
574 return ret;
575 }
576
577 return -ETIMEDOUT;
578 }
579
580 if (size < (8U * sizeof(uint32_t))) {
581 if ((mmio_read_32(base + SDMMC_DCNTR) > 0U) &&
582 ((status & SDMMC_STAR_RXFIFOE) == 0U)) {
583 *buffer = mmio_read_32(fifo_reg);
584 buffer++;
585 }
586 } else if ((status & SDMMC_STAR_RXFIFOHF) != 0U) {
587 uint32_t count;
588
589 /* Read data from SDMMC Rx FIFO */
590 for (count = 0; count < 8U; count++) {
591 *buffer = mmio_read_32(fifo_reg);
592 buffer++;
593 }
594 }
595 } while ((status & flags) == 0U);
596
597 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
598
599 if ((status & SDMMC_STAR_DPSMACT) != 0U) {
600 WARN("%s: DPSMACT=1, send stop\n", __func__);
601 return stm32_sdmmc2_stop_transfer();
602 }
603
604 return 0;
605}
606
607static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size)
608{
609 return 0;
610}
611
612static int stm32_sdmmc2_dt_get_config(void)
613{
614 int sdmmc_node;
615 void *fdt = NULL;
616 const fdt32_t *cuint;
617
618 if (fdt_get_address(&fdt) == 0) {
619 return -FDT_ERR_NOTFOUND;
620 }
621
622 if (fdt == NULL) {
623 return -FDT_ERR_NOTFOUND;
624 }
625
626 sdmmc_node = fdt_node_offset_by_compatible(fdt, -1, DT_SDMMC2_COMPAT);
627
628 while (sdmmc_node != -FDT_ERR_NOTFOUND) {
629 cuint = fdt_getprop(fdt, sdmmc_node, "reg", NULL);
630 if (cuint == NULL) {
631 continue;
632 }
633
634 if (fdt32_to_cpu(*cuint) == sdmmc2_params.reg_base) {
635 break;
636 }
637
638 sdmmc_node = fdt_node_offset_by_compatible(fdt, sdmmc_node,
639 DT_SDMMC2_COMPAT);
640 }
641
642 if (sdmmc_node == -FDT_ERR_NOTFOUND) {
643 return -FDT_ERR_NOTFOUND;
644 }
645
646 if (fdt_check_status(sdmmc_node) == 0) {
647 return -FDT_ERR_NOTFOUND;
648 }
649
650 if (dt_set_pinctrl_config(sdmmc_node) != 0) {
651 return -FDT_ERR_BADVALUE;
652 }
653
654 cuint = fdt_getprop(fdt, sdmmc_node, "clocks", NULL);
655 if (cuint == NULL) {
656 return -FDT_ERR_NOTFOUND;
657 }
658
659 cuint++;
660 sdmmc2_params.clock_id = fdt32_to_cpu(*cuint);
661
662 cuint = fdt_getprop(fdt, sdmmc_node, "resets", NULL);
663 if (cuint == NULL) {
664 return -FDT_ERR_NOTFOUND;
665 }
666
667 cuint++;
668 sdmmc2_params.reset_id = fdt32_to_cpu(*cuint);
669
670 if ((fdt_getprop(fdt, sdmmc_node, "st,pin-ckin", NULL)) != NULL) {
671 sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
672 }
673
674 if ((fdt_getprop(fdt, sdmmc_node, "st,dirpol", NULL)) != NULL) {
675 sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
676 }
677
678 if ((fdt_getprop(fdt, sdmmc_node, "st,negedge", NULL)) != NULL) {
679 sdmmc2_params.negedge = SDMMC_CLKCR_NEGEDGE;
680 }
681
682 cuint = fdt_getprop(fdt, sdmmc_node, "bus-width", NULL);
683 if (cuint != NULL) {
684 switch (fdt32_to_cpu(*cuint)) {
685 case 4:
686 sdmmc2_params.bus_width = MMC_BUS_WIDTH_4;
687 break;
688
689 case 8:
690 sdmmc2_params.bus_width = MMC_BUS_WIDTH_8;
691 break;
692
693 default:
694 break;
695 }
696 }
697
698 return 0;
699}
700
701unsigned long long stm32_sdmmc2_mmc_get_device_size(void)
702{
703 return sdmmc2_params.device_info->device_size;
704}
705
706int stm32_sdmmc2_mmc_init(struct stm32_sdmmc2_params *params)
707{
708 int ret;
709
710 assert((params != NULL) &&
711 ((params->reg_base & MMC_BLOCK_MASK) == 0U) &&
712 ((params->bus_width == MMC_BUS_WIDTH_1) ||
713 (params->bus_width == MMC_BUS_WIDTH_4) ||
714 (params->bus_width == MMC_BUS_WIDTH_8)));
715
716 memcpy(&sdmmc2_params, params, sizeof(struct stm32_sdmmc2_params));
717
718 if (stm32_sdmmc2_dt_get_config() != 0) {
719 ERROR("%s: DT error\n", __func__);
720 return -ENOMEM;
721 }
722
723 ret = stm32mp1_clk_enable(sdmmc2_params.clock_id);
724 if (ret != 0) {
725 ERROR("%s: clock %d failed\n", __func__,
726 sdmmc2_params.clock_id);
727 return ret;
728 }
729
730 stm32mp1_reset_assert(sdmmc2_params.reset_id);
731 udelay(2);
732 stm32mp1_reset_deassert(sdmmc2_params.reset_id);
733 mdelay(1);
734
735 sdmmc2_params.clk_rate = stm32mp1_clk_get_rate(sdmmc2_params.clock_id);
736
737 return mmc_init(&stm32_sdmmc2_ops, sdmmc2_params.clk_rate,
738 sdmmc2_params.bus_width, sdmmc2_params.flags,
739 sdmmc2_params.device_info);
740}