blob: f453ce9a5788c604f361863b32a096e3a51705c7 [file] [log] [blame]
Yann Gautier5380b0d2018-10-15 09:36:04 +02001/*
Yann Gautier7b7e4bf2019-01-17 19:16:03 +01002 * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved
Yann Gautier5380b0d2018-10-15 09:36:04 +02003 *
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>
Yann Gautier038bff22019-01-17 19:17:47 +010020#include <drivers/st/stm32_gpio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <drivers/st/stm32_sdmmc2.h>
Yann Gautiera2e2a302019-02-14 11:13:39 +010022#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <lib/mmio.h>
24#include <lib/utils.h>
25#include <plat/common/platform.h>
26
Yann Gautier5380b0d2018-10-15 09:36:04 +020027/* Registers offsets */
28#define SDMMC_POWER 0x00U
29#define SDMMC_CLKCR 0x04U
30#define SDMMC_ARGR 0x08U
31#define SDMMC_CMDR 0x0CU
32#define SDMMC_RESPCMDR 0x10U
33#define SDMMC_RESP1R 0x14U
34#define SDMMC_RESP2R 0x18U
35#define SDMMC_RESP3R 0x1CU
36#define SDMMC_RESP4R 0x20U
37#define SDMMC_DTIMER 0x24U
38#define SDMMC_DLENR 0x28U
39#define SDMMC_DCTRLR 0x2CU
40#define SDMMC_DCNTR 0x30U
41#define SDMMC_STAR 0x34U
42#define SDMMC_ICR 0x38U
43#define SDMMC_MASKR 0x3CU
44#define SDMMC_ACKTIMER 0x40U
45#define SDMMC_IDMACTRLR 0x50U
46#define SDMMC_IDMABSIZER 0x54U
47#define SDMMC_IDMABASE0R 0x58U
48#define SDMMC_IDMABASE1R 0x5CU
49#define SDMMC_FIFOR 0x80U
50
51/* SDMMC power control register */
52#define SDMMC_POWER_PWRCTRL GENMASK(1, 0)
53#define SDMMC_POWER_DIRPOL BIT(4)
54
55/* SDMMC clock control register */
56#define SDMMC_CLKCR_WIDBUS_4 BIT(14)
57#define SDMMC_CLKCR_WIDBUS_8 BIT(15)
58#define SDMMC_CLKCR_NEGEDGE BIT(16)
59#define SDMMC_CLKCR_HWFC_EN BIT(17)
60#define SDMMC_CLKCR_SELCLKRX_0 BIT(20)
61
62/* SDMMC command register */
63#define SDMMC_CMDR_CMDTRANS BIT(6)
64#define SDMMC_CMDR_CMDSTOP BIT(7)
65#define SDMMC_CMDR_WAITRESP GENMASK(9, 8)
66#define SDMMC_CMDR_WAITRESP_SHORT BIT(8)
67#define SDMMC_CMDR_WAITRESP_SHORT_NOCRC BIT(9)
68#define SDMMC_CMDR_CPSMEN BIT(12)
69
70/* SDMMC data control register */
71#define SDMMC_DCTRLR_DTEN BIT(0)
72#define SDMMC_DCTRLR_DTDIR BIT(1)
73#define SDMMC_DCTRLR_DTMODE GENMASK(3, 2)
74#define SDMMC_DCTRLR_DBLOCKSIZE_0 BIT(4)
75#define SDMMC_DCTRLR_DBLOCKSIZE_1 BIT(5)
76#define SDMMC_DCTRLR_DBLOCKSIZE_3 BIT(7)
77#define SDMMC_DCTRLR_DBLOCKSIZE GENMASK(7, 4)
78#define SDMMC_DCTRLR_FIFORST BIT(13)
79
80#define SDMMC_DCTRLR_CLEAR_MASK (SDMMC_DCTRLR_DTEN | \
81 SDMMC_DCTRLR_DTDIR | \
82 SDMMC_DCTRLR_DTMODE | \
83 SDMMC_DCTRLR_DBLOCKSIZE)
84#define SDMMC_DBLOCKSIZE_8 (SDMMC_DCTRLR_DBLOCKSIZE_0 | \
85 SDMMC_DCTRLR_DBLOCKSIZE_1)
86#define SDMMC_DBLOCKSIZE_512 (SDMMC_DCTRLR_DBLOCKSIZE_0 | \
87 SDMMC_DCTRLR_DBLOCKSIZE_3)
88
89/* SDMMC status register */
90#define SDMMC_STAR_CCRCFAIL BIT(0)
91#define SDMMC_STAR_DCRCFAIL BIT(1)
92#define SDMMC_STAR_CTIMEOUT BIT(2)
93#define SDMMC_STAR_DTIMEOUT BIT(3)
94#define SDMMC_STAR_TXUNDERR BIT(4)
95#define SDMMC_STAR_RXOVERR BIT(5)
96#define SDMMC_STAR_CMDREND BIT(6)
97#define SDMMC_STAR_CMDSENT BIT(7)
98#define SDMMC_STAR_DATAEND BIT(8)
99#define SDMMC_STAR_DBCKEND BIT(10)
Yann Gautiere88fdd72018-11-30 15:22:11 +0100100#define SDMMC_STAR_DPSMACT BIT(12)
Yann Gautier5380b0d2018-10-15 09:36:04 +0200101#define SDMMC_STAR_RXFIFOHF BIT(15)
102#define SDMMC_STAR_RXFIFOE BIT(19)
103#define SDMMC_STAR_IDMATE BIT(27)
104#define SDMMC_STAR_IDMABTC BIT(28)
105
106/* SDMMC DMA control register */
107#define SDMMC_IDMACTRLR_IDMAEN BIT(0)
108
109#define SDMMC_STATIC_FLAGS (SDMMC_STAR_CCRCFAIL | \
110 SDMMC_STAR_DCRCFAIL | \
111 SDMMC_STAR_CTIMEOUT | \
112 SDMMC_STAR_DTIMEOUT | \
113 SDMMC_STAR_TXUNDERR | \
114 SDMMC_STAR_RXOVERR | \
115 SDMMC_STAR_CMDREND | \
116 SDMMC_STAR_CMDSENT | \
117 SDMMC_STAR_DATAEND | \
118 SDMMC_STAR_DBCKEND | \
119 SDMMC_STAR_IDMATE | \
120 SDMMC_STAR_IDMABTC)
121
Yann Gautier2299d572019-02-14 11:14:39 +0100122#define TIMEOUT_US_10_MS 10000U
123#define TIMEOUT_US_1_S 1000000U
Yann Gautier5380b0d2018-10-15 09:36:04 +0200124
125#define DT_SDMMC2_COMPAT "st,stm32-sdmmc2"
126
127static void stm32_sdmmc2_init(void);
128static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd);
129static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd);
130static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width);
131static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size);
132static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size);
133static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size);
134
135static const struct mmc_ops stm32_sdmmc2_ops = {
136 .init = stm32_sdmmc2_init,
137 .send_cmd = stm32_sdmmc2_send_cmd,
138 .set_ios = stm32_sdmmc2_set_ios,
139 .prepare = stm32_sdmmc2_prepare,
140 .read = stm32_sdmmc2_read,
141 .write = stm32_sdmmc2_write,
142};
143
144static struct stm32_sdmmc2_params sdmmc2_params;
145
146#pragma weak plat_sdmmc2_use_dma
147bool plat_sdmmc2_use_dma(unsigned int instance, unsigned int memory)
148{
149 return false;
150}
151
152static void stm32_sdmmc2_init(void)
153{
154 uint32_t clock_div;
155 uintptr_t base = sdmmc2_params.reg_base;
156
157 clock_div = div_round_up(sdmmc2_params.clk_rate,
Yann Gautiera2e2a302019-02-14 11:13:39 +0100158 STM32MP_MMC_INIT_FREQ * 2);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200159
160 mmio_write_32(base + SDMMC_CLKCR, SDMMC_CLKCR_HWFC_EN | clock_div |
161 sdmmc2_params.negedge |
162 sdmmc2_params.pin_ckin);
163
164 mmio_write_32(base + SDMMC_POWER,
165 SDMMC_POWER_PWRCTRL | sdmmc2_params.dirpol);
166
167 mdelay(1);
168}
169
170static int stm32_sdmmc2_stop_transfer(void)
171{
172 struct mmc_cmd cmd_stop;
173
174 zeromem(&cmd_stop, sizeof(struct mmc_cmd));
175
176 cmd_stop.cmd_idx = MMC_CMD(12);
177 cmd_stop.resp_type = MMC_RESPONSE_R1B;
178
179 return stm32_sdmmc2_send_cmd(&cmd_stop);
180}
181
182static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd)
183{
Yann Gautier2299d572019-02-14 11:14:39 +0100184 uint64_t timeout;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200185 uint32_t flags_cmd, status;
186 uint32_t flags_data = 0;
187 int err = 0;
188 uintptr_t base = sdmmc2_params.reg_base;
Yann Gautier2299d572019-02-14 11:14:39 +0100189 unsigned int cmd_reg, arg_reg;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200190
191 if (cmd == NULL) {
192 return -EINVAL;
193 }
194
195 flags_cmd = SDMMC_STAR_CTIMEOUT;
196 arg_reg = cmd->cmd_arg;
197
198 if ((mmio_read_32(base + SDMMC_CMDR) & SDMMC_CMDR_CPSMEN) != 0U) {
199 mmio_write_32(base + SDMMC_CMDR, 0);
200 }
201
202 cmd_reg = cmd->cmd_idx | SDMMC_CMDR_CPSMEN;
203
204 if (cmd->resp_type == 0U) {
205 flags_cmd |= SDMMC_STAR_CMDSENT;
206 }
207
208 if ((cmd->resp_type & MMC_RSP_48) != 0U) {
209 if ((cmd->resp_type & MMC_RSP_136) != 0U) {
210 flags_cmd |= SDMMC_STAR_CMDREND;
211 cmd_reg |= SDMMC_CMDR_WAITRESP;
212 } else if ((cmd->resp_type & MMC_RSP_CRC) != 0U) {
213 flags_cmd |= SDMMC_STAR_CMDREND | SDMMC_STAR_CCRCFAIL;
214 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT;
215 } else {
216 flags_cmd |= SDMMC_STAR_CMDREND;
217 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT_NOCRC;
218 }
219 }
220
221 switch (cmd->cmd_idx) {
222 case MMC_CMD(1):
223 arg_reg |= OCR_POWERUP;
224 break;
225 case MMC_CMD(8):
226 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
227 cmd_reg |= SDMMC_CMDR_CMDTRANS;
228 }
229 break;
230 case MMC_CMD(12):
231 cmd_reg |= SDMMC_CMDR_CMDSTOP;
232 break;
233 case MMC_CMD(17):
234 case MMC_CMD(18):
235 cmd_reg |= SDMMC_CMDR_CMDTRANS;
236 if (sdmmc2_params.use_dma) {
237 flags_data |= SDMMC_STAR_DCRCFAIL |
238 SDMMC_STAR_DTIMEOUT |
239 SDMMC_STAR_DATAEND |
240 SDMMC_STAR_RXOVERR |
241 SDMMC_STAR_IDMATE;
242 }
243 break;
244 case MMC_ACMD(41):
245 arg_reg |= OCR_3_2_3_3 | OCR_3_3_3_4;
246 break;
247 case MMC_ACMD(51):
248 cmd_reg |= SDMMC_CMDR_CMDTRANS;
249 if (sdmmc2_params.use_dma) {
250 flags_data |= SDMMC_STAR_DCRCFAIL |
251 SDMMC_STAR_DTIMEOUT |
252 SDMMC_STAR_DATAEND |
253 SDMMC_STAR_RXOVERR |
254 SDMMC_STAR_IDMATE |
255 SDMMC_STAR_DBCKEND;
256 }
257 break;
258 default:
259 break;
260 }
261
262 if ((cmd->resp_type & MMC_RSP_BUSY) != 0U) {
263 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
264 }
265
266 mmio_write_32(base + SDMMC_ARGR, arg_reg);
267
268 mmio_write_32(base + SDMMC_CMDR, cmd_reg);
269
Yann Gautiere88fdd72018-11-30 15:22:11 +0100270 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200271
Yann Gautier2299d572019-02-14 11:14:39 +0100272 timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200273
Yann Gautiere88fdd72018-11-30 15:22:11 +0100274 while ((status & flags_cmd) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +0100275 if (timeout_elapsed(timeout)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200276 err = -ETIMEDOUT;
277 ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
278 __func__, cmd->cmd_idx, status);
Yann Gautiere88fdd72018-11-30 15:22:11 +0100279 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200280 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200281
Yann Gautiere88fdd72018-11-30 15:22:11 +0100282 status = mmio_read_32(base + SDMMC_STAR);
283 }
284
285 if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200286 if ((status & SDMMC_STAR_CTIMEOUT) != 0U) {
287 err = -ETIMEDOUT;
288 /*
289 * Those timeouts can occur, and framework will handle
290 * the retries. CMD8 is expected to return this timeout
291 * for eMMC
292 */
293 if (!((cmd->cmd_idx == MMC_CMD(1)) ||
294 (cmd->cmd_idx == MMC_CMD(13)) ||
295 ((cmd->cmd_idx == MMC_CMD(8)) &&
296 (cmd->resp_type == MMC_RESPONSE_R7)))) {
297 ERROR("%s: CTIMEOUT (cmd = %d,status = %x)\n",
298 __func__, cmd->cmd_idx, status);
299 }
300 } else {
301 err = -EIO;
302 ERROR("%s: CRCFAIL (cmd = %d,status = %x)\n",
303 __func__, cmd->cmd_idx, status);
304 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100305
306 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200307 }
308
Yann Gautiere88fdd72018-11-30 15:22:11 +0100309 if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200310 if ((cmd->cmd_idx == MMC_CMD(9)) &&
311 ((cmd_reg & SDMMC_CMDR_WAITRESP) == SDMMC_CMDR_WAITRESP)) {
312 /* Need to invert response to match CSD structure */
313 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP4R);
314 cmd->resp_data[1] = mmio_read_32(base + SDMMC_RESP3R);
315 cmd->resp_data[2] = mmio_read_32(base + SDMMC_RESP2R);
316 cmd->resp_data[3] = mmio_read_32(base + SDMMC_RESP1R);
317 } else {
318 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP1R);
319 if ((cmd_reg & SDMMC_CMDR_WAITRESP) ==
320 SDMMC_CMDR_WAITRESP) {
321 cmd->resp_data[1] = mmio_read_32(base +
322 SDMMC_RESP2R);
323 cmd->resp_data[2] = mmio_read_32(base +
324 SDMMC_RESP3R);
325 cmd->resp_data[3] = mmio_read_32(base +
326 SDMMC_RESP4R);
327 }
328 }
329 }
330
Yann Gautiere88fdd72018-11-30 15:22:11 +0100331 if (flags_data == 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200332 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
333
Yann Gautiere88fdd72018-11-30 15:22:11 +0100334 return 0;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200335 }
336
Yann Gautiere88fdd72018-11-30 15:22:11 +0100337 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200338
Yann Gautier2299d572019-02-14 11:14:39 +0100339 timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200340
Yann Gautiere88fdd72018-11-30 15:22:11 +0100341 while ((status & flags_data) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +0100342 if (timeout_elapsed(timeout)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200343 ERROR("%s: timeout 10ms (cmd = %d,status = %x)\n",
344 __func__, cmd->cmd_idx, status);
345 err = -ETIMEDOUT;
Yann Gautiere88fdd72018-11-30 15:22:11 +0100346 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200347 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100348
349 status = mmio_read_32(base + SDMMC_STAR);
350 };
Yann Gautier5380b0d2018-10-15 09:36:04 +0200351
352 if ((status & (SDMMC_STAR_DTIMEOUT | SDMMC_STAR_DCRCFAIL |
353 SDMMC_STAR_TXUNDERR | SDMMC_STAR_RXOVERR |
354 SDMMC_STAR_IDMATE)) != 0U) {
355 ERROR("%s: Error flag (cmd = %d,status = %x)\n", __func__,
356 cmd->cmd_idx, status);
357 err = -EIO;
358 }
359
Yann Gautiere88fdd72018-11-30 15:22:11 +0100360err_exit:
Yann Gautier5380b0d2018-10-15 09:36:04 +0200361 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
362 mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);
363
Yann Gautier2299d572019-02-14 11:14:39 +0100364 if ((err != 0) && ((status & SDMMC_STAR_DPSMACT) != 0U)) {
Yann Gautiere88fdd72018-11-30 15:22:11 +0100365 int ret_stop = stm32_sdmmc2_stop_transfer();
366
367 if (ret_stop != 0) {
368 return ret_stop;
369 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200370 }
371
372 return err;
373}
374
375static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
376{
377 int8_t retry;
378 int err = 0;
379
380 assert(cmd != NULL);
381
382 for (retry = 0; retry <= 3; retry++) {
383 err = stm32_sdmmc2_send_cmd_req(cmd);
384 if (err == 0) {
385 return err;
386 }
387
388 if ((cmd->cmd_idx == MMC_CMD(1)) ||
389 (cmd->cmd_idx == MMC_CMD(13))) {
390 return 0; /* Retry managed by framework */
391 }
392
393 /* Command 8 is expected to fail for eMMC */
394 if (!(cmd->cmd_idx == MMC_CMD(8))) {
395 WARN(" CMD%d, Retry: %d, Error: %d\n",
396 cmd->cmd_idx, retry, err);
397 }
398
399 udelay(10);
400 }
401
402 return err;
403}
404
405static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width)
406{
407 uintptr_t base = sdmmc2_params.reg_base;
408 uint32_t bus_cfg = 0;
409 uint32_t clock_div, max_freq;
410 uint32_t clk_rate = sdmmc2_params.clk_rate;
411 uint32_t max_bus_freq = sdmmc2_params.device_info->max_bus_freq;
412
413 switch (width) {
414 case MMC_BUS_WIDTH_1:
415 break;
416 case MMC_BUS_WIDTH_4:
417 bus_cfg |= SDMMC_CLKCR_WIDBUS_4;
418 break;
419 case MMC_BUS_WIDTH_8:
420 bus_cfg |= SDMMC_CLKCR_WIDBUS_8;
421 break;
422 default:
423 panic();
424 break;
425 }
426
427 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
428 if (max_bus_freq >= 52000000U) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100429 max_freq = STM32MP_EMMC_HIGH_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200430 } else {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100431 max_freq = STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200432 }
433 } else {
434 if (max_bus_freq >= 50000000U) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100435 max_freq = STM32MP_SD_HIGH_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200436 } else {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100437 max_freq = STM32MP_SD_NORMAL_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200438 }
439 }
440
441 clock_div = div_round_up(clk_rate, max_freq * 2);
442
443 mmio_write_32(base + SDMMC_CLKCR,
444 SDMMC_CLKCR_HWFC_EN | clock_div | bus_cfg |
445 sdmmc2_params.negedge |
446 sdmmc2_params.pin_ckin);
447
448 return 0;
449}
450
451static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size)
452{
453 struct mmc_cmd cmd;
454 int ret;
455 uintptr_t base = sdmmc2_params.reg_base;
456 uint32_t data_ctrl = SDMMC_DCTRLR_DTDIR;
457
458 if (size == 8U) {
459 data_ctrl |= SDMMC_DBLOCKSIZE_8;
460 } else {
461 data_ctrl |= SDMMC_DBLOCKSIZE_512;
462 }
463
464 sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);
465
466 if (sdmmc2_params.use_dma) {
467 inv_dcache_range(buf, size);
468 }
469
470 /* Prepare CMD 16*/
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100471 mmio_write_32(base + SDMMC_DTIMER, 0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200472
473 mmio_write_32(base + SDMMC_DLENR, 0);
474
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100475 mmio_write_32(base + SDMMC_DCTRLR, 0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200476
477 zeromem(&cmd, sizeof(struct mmc_cmd));
478
479 cmd.cmd_idx = MMC_CMD(16);
480 if (size > MMC_BLOCK_SIZE) {
481 cmd.cmd_arg = MMC_BLOCK_SIZE;
482 } else {
483 cmd.cmd_arg = size;
484 }
485
486 cmd.resp_type = MMC_RESPONSE_R1;
487
488 ret = stm32_sdmmc2_send_cmd(&cmd);
489 if (ret != 0) {
490 ERROR("CMD16 failed\n");
491 return ret;
492 }
493
494 /* Prepare data command */
495 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
496
497 mmio_write_32(base + SDMMC_DLENR, size);
498
499 if (sdmmc2_params.use_dma) {
500 mmio_write_32(base + SDMMC_IDMACTRLR,
501 SDMMC_IDMACTRLR_IDMAEN);
502 mmio_write_32(base + SDMMC_IDMABASE0R, buf);
503
504 flush_dcache_range(buf, size);
505 }
506
507 mmio_clrsetbits_32(base + SDMMC_DCTRLR,
508 SDMMC_DCTRLR_CLEAR_MASK,
509 data_ctrl);
510
511 return 0;
512}
513
514static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size)
515{
516 uint32_t error_flags = SDMMC_STAR_RXOVERR | SDMMC_STAR_DCRCFAIL |
517 SDMMC_STAR_DTIMEOUT;
518 uint32_t flags = error_flags | SDMMC_STAR_DATAEND;
519 uint32_t status;
520 uint32_t *buffer;
521 uintptr_t base = sdmmc2_params.reg_base;
522 uintptr_t fifo_reg = base + SDMMC_FIFOR;
Yann Gautier2299d572019-02-14 11:14:39 +0100523 uint64_t timeout;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200524 int ret;
525
526 /* Assert buf is 4 bytes aligned */
527 assert((buf & GENMASK(1, 0)) == 0U);
528
529 buffer = (uint32_t *)buf;
530
531 if (sdmmc2_params.use_dma) {
532 inv_dcache_range(buf, size);
533
534 return 0;
535 }
536
537 if (size <= MMC_BLOCK_SIZE) {
538 flags |= SDMMC_STAR_DBCKEND;
539 }
540
Yann Gautier2299d572019-02-14 11:14:39 +0100541 timeout = timeout_init_us(TIMEOUT_US_1_S);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200542
543 do {
544 status = mmio_read_32(base + SDMMC_STAR);
545
546 if ((status & error_flags) != 0U) {
547 ERROR("%s: Read error (status = %x)\n", __func__,
548 status);
549 mmio_write_32(base + SDMMC_DCTRLR,
550 SDMMC_DCTRLR_FIFORST);
551
552 mmio_write_32(base + SDMMC_ICR,
553 SDMMC_STATIC_FLAGS);
554
555 ret = stm32_sdmmc2_stop_transfer();
556 if (ret != 0) {
557 return ret;
558 }
559
560 return -EIO;
561 }
562
Yann Gautier2299d572019-02-14 11:14:39 +0100563 if (timeout_elapsed(timeout)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200564 ERROR("%s: timeout 1s (status = %x)\n",
565 __func__, status);
566 mmio_write_32(base + SDMMC_ICR,
567 SDMMC_STATIC_FLAGS);
568
569 ret = stm32_sdmmc2_stop_transfer();
570 if (ret != 0) {
571 return ret;
572 }
573
574 return -ETIMEDOUT;
575 }
576
577 if (size < (8U * sizeof(uint32_t))) {
578 if ((mmio_read_32(base + SDMMC_DCNTR) > 0U) &&
579 ((status & SDMMC_STAR_RXFIFOE) == 0U)) {
580 *buffer = mmio_read_32(fifo_reg);
581 buffer++;
582 }
583 } else if ((status & SDMMC_STAR_RXFIFOHF) != 0U) {
584 uint32_t count;
585
586 /* Read data from SDMMC Rx FIFO */
587 for (count = 0; count < 8U; count++) {
588 *buffer = mmio_read_32(fifo_reg);
589 buffer++;
590 }
591 }
592 } while ((status & flags) == 0U);
593
594 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
595
596 if ((status & SDMMC_STAR_DPSMACT) != 0U) {
597 WARN("%s: DPSMACT=1, send stop\n", __func__);
598 return stm32_sdmmc2_stop_transfer();
599 }
600
601 return 0;
602}
603
604static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size)
605{
606 return 0;
607}
608
609static int stm32_sdmmc2_dt_get_config(void)
610{
611 int sdmmc_node;
612 void *fdt = NULL;
613 const fdt32_t *cuint;
614
615 if (fdt_get_address(&fdt) == 0) {
616 return -FDT_ERR_NOTFOUND;
617 }
618
619 if (fdt == NULL) {
620 return -FDT_ERR_NOTFOUND;
621 }
622
623 sdmmc_node = fdt_node_offset_by_compatible(fdt, -1, DT_SDMMC2_COMPAT);
624
625 while (sdmmc_node != -FDT_ERR_NOTFOUND) {
626 cuint = fdt_getprop(fdt, sdmmc_node, "reg", NULL);
627 if (cuint == NULL) {
628 continue;
629 }
630
631 if (fdt32_to_cpu(*cuint) == sdmmc2_params.reg_base) {
632 break;
633 }
634
635 sdmmc_node = fdt_node_offset_by_compatible(fdt, sdmmc_node,
636 DT_SDMMC2_COMPAT);
637 }
638
639 if (sdmmc_node == -FDT_ERR_NOTFOUND) {
640 return -FDT_ERR_NOTFOUND;
641 }
642
Yann Gautier038bff22019-01-17 19:17:47 +0100643 if (fdt_get_status(sdmmc_node) == DT_DISABLED) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200644 return -FDT_ERR_NOTFOUND;
645 }
646
647 if (dt_set_pinctrl_config(sdmmc_node) != 0) {
648 return -FDT_ERR_BADVALUE;
649 }
650
651 cuint = fdt_getprop(fdt, sdmmc_node, "clocks", NULL);
652 if (cuint == NULL) {
653 return -FDT_ERR_NOTFOUND;
654 }
655
656 cuint++;
657 sdmmc2_params.clock_id = fdt32_to_cpu(*cuint);
658
659 cuint = fdt_getprop(fdt, sdmmc_node, "resets", NULL);
660 if (cuint == NULL) {
661 return -FDT_ERR_NOTFOUND;
662 }
663
664 cuint++;
665 sdmmc2_params.reset_id = fdt32_to_cpu(*cuint);
666
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100667 if ((fdt_getprop(fdt, sdmmc_node, "st,use-ckin", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200668 sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
669 }
670
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100671 if ((fdt_getprop(fdt, sdmmc_node, "st,sig-dir", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200672 sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
673 }
674
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100675 if ((fdt_getprop(fdt, sdmmc_node, "st,neg-edge", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200676 sdmmc2_params.negedge = SDMMC_CLKCR_NEGEDGE;
677 }
678
679 cuint = fdt_getprop(fdt, sdmmc_node, "bus-width", NULL);
680 if (cuint != NULL) {
681 switch (fdt32_to_cpu(*cuint)) {
682 case 4:
683 sdmmc2_params.bus_width = MMC_BUS_WIDTH_4;
684 break;
685
686 case 8:
687 sdmmc2_params.bus_width = MMC_BUS_WIDTH_8;
688 break;
689
690 default:
691 break;
692 }
693 }
694
695 return 0;
696}
697
698unsigned long long stm32_sdmmc2_mmc_get_device_size(void)
699{
700 return sdmmc2_params.device_info->device_size;
701}
702
703int stm32_sdmmc2_mmc_init(struct stm32_sdmmc2_params *params)
704{
Yann Gautier5380b0d2018-10-15 09:36:04 +0200705 assert((params != NULL) &&
706 ((params->reg_base & MMC_BLOCK_MASK) == 0U) &&
707 ((params->bus_width == MMC_BUS_WIDTH_1) ||
708 (params->bus_width == MMC_BUS_WIDTH_4) ||
709 (params->bus_width == MMC_BUS_WIDTH_8)));
710
711 memcpy(&sdmmc2_params, params, sizeof(struct stm32_sdmmc2_params));
712
713 if (stm32_sdmmc2_dt_get_config() != 0) {
714 ERROR("%s: DT error\n", __func__);
715 return -ENOMEM;
716 }
717
Yann Gautiere4a3c352019-02-14 10:53:33 +0100718 stm32mp_clk_enable(sdmmc2_params.clock_id);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200719
Yann Gautiera2e2a302019-02-14 11:13:39 +0100720 stm32mp_reset_assert(sdmmc2_params.reset_id);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200721 udelay(2);
Yann Gautiera2e2a302019-02-14 11:13:39 +0100722 stm32mp_reset_deassert(sdmmc2_params.reset_id);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200723 mdelay(1);
724
Yann Gautiera2e2a302019-02-14 11:13:39 +0100725 sdmmc2_params.clk_rate = stm32mp_clk_get_rate(sdmmc2_params.clock_id);
Yann Gautierc8fa1aa2019-03-08 10:59:00 +0100726 sdmmc2_params.device_info->ocr_voltage = OCR_3_2_3_3 | OCR_3_3_3_4;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200727
728 return mmc_init(&stm32_sdmmc2_ops, sdmmc2_params.clk_rate,
729 sdmmc2_params.bus_width, sdmmc2_params.flags,
730 sdmmc2_params.device_info);
731}