blob: be722f32243f483d267687dfa4273fe2dbf3f07f [file] [log] [blame]
Yann Gautier5380b0d2018-10-15 09:36:04 +02001/*
Yann Gautier619d2a52023-06-30 16:01:30 +02002 * Copyright (c) 2018-2023, 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 Gautier5380b0d2018-10-15 09:36:04 +020011#include <arch.h>
12#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <common/debug.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020014#include <drivers/clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <drivers/delay_timer.h>
16#include <drivers/mmc.h>
Yann Gautier038bff22019-01-17 19:17:47 +010017#include <drivers/st/stm32_gpio.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <drivers/st/stm32_sdmmc2.h>
Yann Gautiera2e2a302019-02-14 11:13:39 +010019#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <lib/mmio.h>
21#include <lib/utils.h>
Yann Gautierc14205d2019-05-10 16:01:34 +020022#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <plat/common/platform.h>
24
Yann Gautierc14205d2019-05-10 16:01:34 +020025#include <platform_def.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)
Yann Gautierc14205d2019-05-10 16:01:34 +020053#define SDMMC_POWER_PWRCTRL_PWR_CYCLE BIT(1)
Yann Gautier5380b0d2018-10-15 09:36:04 +020054#define SDMMC_POWER_DIRPOL BIT(4)
55
56/* SDMMC clock control register */
57#define SDMMC_CLKCR_WIDBUS_4 BIT(14)
58#define SDMMC_CLKCR_WIDBUS_8 BIT(15)
59#define SDMMC_CLKCR_NEGEDGE BIT(16)
60#define SDMMC_CLKCR_HWFC_EN BIT(17)
61#define SDMMC_CLKCR_SELCLKRX_0 BIT(20)
62
63/* SDMMC command register */
64#define SDMMC_CMDR_CMDTRANS BIT(6)
65#define SDMMC_CMDR_CMDSTOP BIT(7)
66#define SDMMC_CMDR_WAITRESP GENMASK(9, 8)
67#define SDMMC_CMDR_WAITRESP_SHORT BIT(8)
68#define SDMMC_CMDR_WAITRESP_SHORT_NOCRC BIT(9)
69#define SDMMC_CMDR_CPSMEN BIT(12)
70
71/* SDMMC data control register */
72#define SDMMC_DCTRLR_DTEN BIT(0)
73#define SDMMC_DCTRLR_DTDIR BIT(1)
74#define SDMMC_DCTRLR_DTMODE GENMASK(3, 2)
Yann Gautier5380b0d2018-10-15 09:36:04 +020075#define SDMMC_DCTRLR_DBLOCKSIZE GENMASK(7, 4)
Yann Gautier6d9e6a02019-06-11 20:03:07 +020076#define SDMMC_DCTRLR_DBLOCKSIZE_SHIFT 4
Yann Gautier5380b0d2018-10-15 09:36:04 +020077#define SDMMC_DCTRLR_FIFORST BIT(13)
78
79#define SDMMC_DCTRLR_CLEAR_MASK (SDMMC_DCTRLR_DTEN | \
80 SDMMC_DCTRLR_DTDIR | \
81 SDMMC_DCTRLR_DTMODE | \
82 SDMMC_DCTRLR_DBLOCKSIZE)
Yann Gautier5380b0d2018-10-15 09:36:04 +020083
84/* SDMMC status register */
85#define SDMMC_STAR_CCRCFAIL BIT(0)
86#define SDMMC_STAR_DCRCFAIL BIT(1)
87#define SDMMC_STAR_CTIMEOUT BIT(2)
88#define SDMMC_STAR_DTIMEOUT BIT(3)
89#define SDMMC_STAR_TXUNDERR BIT(4)
90#define SDMMC_STAR_RXOVERR BIT(5)
91#define SDMMC_STAR_CMDREND BIT(6)
92#define SDMMC_STAR_CMDSENT BIT(7)
93#define SDMMC_STAR_DATAEND BIT(8)
94#define SDMMC_STAR_DBCKEND BIT(10)
Yann Gautiere88fdd72018-11-30 15:22:11 +010095#define SDMMC_STAR_DPSMACT BIT(12)
Yann Gautier5380b0d2018-10-15 09:36:04 +020096#define SDMMC_STAR_RXFIFOHF BIT(15)
97#define SDMMC_STAR_RXFIFOE BIT(19)
98#define SDMMC_STAR_IDMATE BIT(27)
99#define SDMMC_STAR_IDMABTC BIT(28)
100
101/* SDMMC DMA control register */
102#define SDMMC_IDMACTRLR_IDMAEN BIT(0)
103
104#define SDMMC_STATIC_FLAGS (SDMMC_STAR_CCRCFAIL | \
105 SDMMC_STAR_DCRCFAIL | \
106 SDMMC_STAR_CTIMEOUT | \
107 SDMMC_STAR_DTIMEOUT | \
108 SDMMC_STAR_TXUNDERR | \
109 SDMMC_STAR_RXOVERR | \
110 SDMMC_STAR_CMDREND | \
111 SDMMC_STAR_CMDSENT | \
112 SDMMC_STAR_DATAEND | \
113 SDMMC_STAR_DBCKEND | \
114 SDMMC_STAR_IDMATE | \
115 SDMMC_STAR_IDMABTC)
116
Etienne Carrieref02647a2019-12-08 08:14:40 +0100117#define TIMEOUT_US_1_MS 1000U
Yann Gautier2299d572019-02-14 11:14:39 +0100118#define TIMEOUT_US_10_MS 10000U
119#define TIMEOUT_US_1_S 1000000U
Yann Gautier5380b0d2018-10-15 09:36:04 +0200120
Yann Gautierc14205d2019-05-10 16:01:34 +0200121/* Power cycle delays in ms */
122#define VCC_POWER_OFF_DELAY 2
123#define VCC_POWER_ON_DELAY 2
124#define POWER_CYCLE_DELAY 2
125#define POWER_OFF_DELAY 2
126#define POWER_ON_DELAY 1
127
Yann Gautier22c3dbf2021-01-20 14:08:32 +0100128#ifndef DT_SDMMC2_COMPAT
Yann Gautier5380b0d2018-10-15 09:36:04 +0200129#define DT_SDMMC2_COMPAT "st,stm32-sdmmc2"
Yann Gautier22c3dbf2021-01-20 14:08:32 +0100130#endif
Yann Gautier5380b0d2018-10-15 09:36:04 +0200131
Yann Gautier2da3c972021-05-05 13:47:56 +0200132#define SDMMC_FIFO_SIZE 64U
133
Yann Gautier619d2a52023-06-30 16:01:30 +0200134#define STM32MP_MMC_INIT_FREQ U(400000) /*400 KHz*/
135#define STM32MP_SD_NORMAL_SPEED_MAX_FREQ U(25000000) /*25 MHz*/
136#define STM32MP_SD_HIGH_SPEED_MAX_FREQ U(50000000) /*50 MHz*/
137#define STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ U(26000000) /*26 MHz*/
138#define STM32MP_EMMC_HIGH_SPEED_MAX_FREQ U(52000000) /*52 MHz*/
139
Yann Gautier5380b0d2018-10-15 09:36:04 +0200140static void stm32_sdmmc2_init(void);
141static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd);
142static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd);
143static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width);
144static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size);
145static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size);
146static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size);
147
148static const struct mmc_ops stm32_sdmmc2_ops = {
149 .init = stm32_sdmmc2_init,
150 .send_cmd = stm32_sdmmc2_send_cmd,
151 .set_ios = stm32_sdmmc2_set_ios,
152 .prepare = stm32_sdmmc2_prepare,
153 .read = stm32_sdmmc2_read,
154 .write = stm32_sdmmc2_write,
155};
156
157static struct stm32_sdmmc2_params sdmmc2_params;
158
Yann Gautier726a7982019-06-12 15:48:05 +0200159static bool next_cmd_is_acmd;
160
Yann Gautier5380b0d2018-10-15 09:36:04 +0200161#pragma weak plat_sdmmc2_use_dma
162bool plat_sdmmc2_use_dma(unsigned int instance, unsigned int memory)
163{
164 return false;
165}
166
167static void stm32_sdmmc2_init(void)
168{
169 uint32_t clock_div;
Yann Gautier3194afe2019-05-28 11:54:50 +0200170 uint32_t freq = STM32MP_MMC_INIT_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200171 uintptr_t base = sdmmc2_params.reg_base;
Yann Gautier68899ce2022-01-04 15:25:04 +0100172 int ret;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200173
Yann Gautier3194afe2019-05-28 11:54:50 +0200174 if (sdmmc2_params.max_freq != 0U) {
175 freq = MIN(sdmmc2_params.max_freq, freq);
176 }
177
Yann Gautierc14205d2019-05-10 16:01:34 +0200178 if (sdmmc2_params.vmmc_regu != NULL) {
Yann Gautier68899ce2022-01-04 15:25:04 +0100179 ret = regulator_disable(sdmmc2_params.vmmc_regu);
180 if (ret < 0) {
181 panic();
182 }
Yann Gautierc14205d2019-05-10 16:01:34 +0200183 }
184
185 mdelay(VCC_POWER_OFF_DELAY);
186
187 mmio_write_32(base + SDMMC_POWER,
188 SDMMC_POWER_PWRCTRL_PWR_CYCLE | sdmmc2_params.dirpol);
189 mdelay(POWER_CYCLE_DELAY);
190
191 if (sdmmc2_params.vmmc_regu != NULL) {
Yann Gautier68899ce2022-01-04 15:25:04 +0100192 ret = regulator_enable(sdmmc2_params.vmmc_regu);
193 if (ret < 0) {
194 panic();
195 }
Yann Gautierc14205d2019-05-10 16:01:34 +0200196 }
197
198 mdelay(VCC_POWER_ON_DELAY);
199
200 mmio_write_32(base + SDMMC_POWER, sdmmc2_params.dirpol);
201 mdelay(POWER_OFF_DELAY);
202
Yann Gautier3194afe2019-05-28 11:54:50 +0200203 clock_div = div_round_up(sdmmc2_params.clk_rate, freq * 2U);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200204
205 mmio_write_32(base + SDMMC_CLKCR, SDMMC_CLKCR_HWFC_EN | clock_div |
206 sdmmc2_params.negedge |
207 sdmmc2_params.pin_ckin);
208
209 mmio_write_32(base + SDMMC_POWER,
210 SDMMC_POWER_PWRCTRL | sdmmc2_params.dirpol);
211
Yann Gautierc14205d2019-05-10 16:01:34 +0200212 mdelay(POWER_ON_DELAY);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200213}
214
215static int stm32_sdmmc2_stop_transfer(void)
216{
217 struct mmc_cmd cmd_stop;
218
219 zeromem(&cmd_stop, sizeof(struct mmc_cmd));
220
221 cmd_stop.cmd_idx = MMC_CMD(12);
222 cmd_stop.resp_type = MMC_RESPONSE_R1B;
223
224 return stm32_sdmmc2_send_cmd(&cmd_stop);
225}
226
227static int stm32_sdmmc2_send_cmd_req(struct mmc_cmd *cmd)
228{
Yann Gautier2299d572019-02-14 11:14:39 +0100229 uint64_t timeout;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200230 uint32_t flags_cmd, status;
231 uint32_t flags_data = 0;
232 int err = 0;
233 uintptr_t base = sdmmc2_params.reg_base;
Yann Gautier2299d572019-02-14 11:14:39 +0100234 unsigned int cmd_reg, arg_reg;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200235
236 if (cmd == NULL) {
237 return -EINVAL;
238 }
239
240 flags_cmd = SDMMC_STAR_CTIMEOUT;
241 arg_reg = cmd->cmd_arg;
242
243 if ((mmio_read_32(base + SDMMC_CMDR) & SDMMC_CMDR_CPSMEN) != 0U) {
244 mmio_write_32(base + SDMMC_CMDR, 0);
245 }
246
247 cmd_reg = cmd->cmd_idx | SDMMC_CMDR_CPSMEN;
248
249 if (cmd->resp_type == 0U) {
250 flags_cmd |= SDMMC_STAR_CMDSENT;
251 }
252
253 if ((cmd->resp_type & MMC_RSP_48) != 0U) {
254 if ((cmd->resp_type & MMC_RSP_136) != 0U) {
255 flags_cmd |= SDMMC_STAR_CMDREND;
256 cmd_reg |= SDMMC_CMDR_WAITRESP;
257 } else if ((cmd->resp_type & MMC_RSP_CRC) != 0U) {
258 flags_cmd |= SDMMC_STAR_CMDREND | SDMMC_STAR_CCRCFAIL;
259 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT;
260 } else {
261 flags_cmd |= SDMMC_STAR_CMDREND;
262 cmd_reg |= SDMMC_CMDR_WAITRESP_SHORT_NOCRC;
263 }
264 }
265
266 switch (cmd->cmd_idx) {
267 case MMC_CMD(1):
268 arg_reg |= OCR_POWERUP;
269 break;
Yann Gautier726a7982019-06-12 15:48:05 +0200270 case MMC_CMD(6):
271 if ((sdmmc2_params.device_info->mmc_dev_type == MMC_IS_SD_HC) &&
272 (!next_cmd_is_acmd)) {
273 cmd_reg |= SDMMC_CMDR_CMDTRANS;
274 if (sdmmc2_params.use_dma) {
275 flags_data |= SDMMC_STAR_DCRCFAIL |
276 SDMMC_STAR_DTIMEOUT |
277 SDMMC_STAR_DATAEND |
278 SDMMC_STAR_RXOVERR |
279 SDMMC_STAR_IDMATE |
280 SDMMC_STAR_DBCKEND;
281 }
282 }
283 break;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200284 case MMC_CMD(8):
285 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
286 cmd_reg |= SDMMC_CMDR_CMDTRANS;
287 }
288 break;
289 case MMC_CMD(12):
290 cmd_reg |= SDMMC_CMDR_CMDSTOP;
291 break;
292 case MMC_CMD(17):
293 case MMC_CMD(18):
294 cmd_reg |= SDMMC_CMDR_CMDTRANS;
295 if (sdmmc2_params.use_dma) {
296 flags_data |= SDMMC_STAR_DCRCFAIL |
297 SDMMC_STAR_DTIMEOUT |
298 SDMMC_STAR_DATAEND |
299 SDMMC_STAR_RXOVERR |
300 SDMMC_STAR_IDMATE;
301 }
302 break;
303 case MMC_ACMD(41):
304 arg_reg |= OCR_3_2_3_3 | OCR_3_3_3_4;
305 break;
306 case MMC_ACMD(51):
307 cmd_reg |= SDMMC_CMDR_CMDTRANS;
308 if (sdmmc2_params.use_dma) {
309 flags_data |= SDMMC_STAR_DCRCFAIL |
310 SDMMC_STAR_DTIMEOUT |
311 SDMMC_STAR_DATAEND |
312 SDMMC_STAR_RXOVERR |
313 SDMMC_STAR_IDMATE |
314 SDMMC_STAR_DBCKEND;
315 }
316 break;
317 default:
318 break;
319 }
320
Yann Gautier726a7982019-06-12 15:48:05 +0200321 next_cmd_is_acmd = (cmd->cmd_idx == MMC_CMD(55));
322
Yann Gautier10454222020-06-12 14:14:26 +0200323 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
324
325 /*
326 * Clear the SDMMC_DCTRLR if the command does not await data.
327 * Skip CMD55 as the next command could be data related, and
328 * the register could have been set in prepare function.
329 */
Yann Gautier726a7982019-06-12 15:48:05 +0200330 if (((cmd_reg & SDMMC_CMDR_CMDTRANS) == 0U) && !next_cmd_is_acmd) {
Yann Gautier10454222020-06-12 14:14:26 +0200331 mmio_write_32(base + SDMMC_DCTRLR, 0U);
332 }
333
Yann Gautier5380b0d2018-10-15 09:36:04 +0200334 if ((cmd->resp_type & MMC_RSP_BUSY) != 0U) {
335 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
336 }
337
338 mmio_write_32(base + SDMMC_ARGR, arg_reg);
339
340 mmio_write_32(base + SDMMC_CMDR, cmd_reg);
341
Yann Gautiere88fdd72018-11-30 15:22:11 +0100342 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200343
Yann Gautier2299d572019-02-14 11:14:39 +0100344 timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200345
Yann Gautiere88fdd72018-11-30 15:22:11 +0100346 while ((status & flags_cmd) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +0100347 if (timeout_elapsed(timeout)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200348 err = -ETIMEDOUT;
Yann Gautierfaef9022022-02-14 09:58:11 +0100349 ERROR("%s: timeout 10ms (cmd = %u,status = %x)\n",
Yann Gautier5380b0d2018-10-15 09:36:04 +0200350 __func__, cmd->cmd_idx, status);
Yann Gautiere88fdd72018-11-30 15:22:11 +0100351 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200352 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200353
Yann Gautiere88fdd72018-11-30 15:22:11 +0100354 status = mmio_read_32(base + SDMMC_STAR);
355 }
356
357 if ((status & (SDMMC_STAR_CTIMEOUT | SDMMC_STAR_CCRCFAIL)) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200358 if ((status & SDMMC_STAR_CTIMEOUT) != 0U) {
359 err = -ETIMEDOUT;
360 /*
361 * Those timeouts can occur, and framework will handle
362 * the retries. CMD8 is expected to return this timeout
363 * for eMMC
364 */
365 if (!((cmd->cmd_idx == MMC_CMD(1)) ||
366 (cmd->cmd_idx == MMC_CMD(13)) ||
367 ((cmd->cmd_idx == MMC_CMD(8)) &&
368 (cmd->resp_type == MMC_RESPONSE_R7)))) {
Yann Gautierfaef9022022-02-14 09:58:11 +0100369 ERROR("%s: CTIMEOUT (cmd = %u,status = %x)\n",
Yann Gautier5380b0d2018-10-15 09:36:04 +0200370 __func__, cmd->cmd_idx, status);
371 }
372 } else {
373 err = -EIO;
Yann Gautierfaef9022022-02-14 09:58:11 +0100374 ERROR("%s: CRCFAIL (cmd = %u,status = %x)\n",
Yann Gautier5380b0d2018-10-15 09:36:04 +0200375 __func__, cmd->cmd_idx, status);
376 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100377
378 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200379 }
380
Yann Gautiere88fdd72018-11-30 15:22:11 +0100381 if ((cmd_reg & SDMMC_CMDR_WAITRESP) != 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200382 if ((cmd->cmd_idx == MMC_CMD(9)) &&
383 ((cmd_reg & SDMMC_CMDR_WAITRESP) == SDMMC_CMDR_WAITRESP)) {
384 /* Need to invert response to match CSD structure */
385 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP4R);
386 cmd->resp_data[1] = mmio_read_32(base + SDMMC_RESP3R);
387 cmd->resp_data[2] = mmio_read_32(base + SDMMC_RESP2R);
388 cmd->resp_data[3] = mmio_read_32(base + SDMMC_RESP1R);
389 } else {
390 cmd->resp_data[0] = mmio_read_32(base + SDMMC_RESP1R);
391 if ((cmd_reg & SDMMC_CMDR_WAITRESP) ==
392 SDMMC_CMDR_WAITRESP) {
393 cmd->resp_data[1] = mmio_read_32(base +
394 SDMMC_RESP2R);
395 cmd->resp_data[2] = mmio_read_32(base +
396 SDMMC_RESP3R);
397 cmd->resp_data[3] = mmio_read_32(base +
398 SDMMC_RESP4R);
399 }
400 }
401 }
402
Yann Gautiere88fdd72018-11-30 15:22:11 +0100403 if (flags_data == 0U) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200404 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
405
Yann Gautiere88fdd72018-11-30 15:22:11 +0100406 return 0;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200407 }
408
Yann Gautiere88fdd72018-11-30 15:22:11 +0100409 status = mmio_read_32(base + SDMMC_STAR);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200410
Yann Gautier2299d572019-02-14 11:14:39 +0100411 timeout = timeout_init_us(TIMEOUT_US_10_MS);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200412
Yann Gautiere88fdd72018-11-30 15:22:11 +0100413 while ((status & flags_data) == 0U) {
Yann Gautier2299d572019-02-14 11:14:39 +0100414 if (timeout_elapsed(timeout)) {
Yann Gautierfaef9022022-02-14 09:58:11 +0100415 ERROR("%s: timeout 10ms (cmd = %u,status = %x)\n",
Yann Gautier5380b0d2018-10-15 09:36:04 +0200416 __func__, cmd->cmd_idx, status);
417 err = -ETIMEDOUT;
Yann Gautiere88fdd72018-11-30 15:22:11 +0100418 goto err_exit;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200419 }
Yann Gautiere88fdd72018-11-30 15:22:11 +0100420
421 status = mmio_read_32(base + SDMMC_STAR);
422 };
Yann Gautier5380b0d2018-10-15 09:36:04 +0200423
424 if ((status & (SDMMC_STAR_DTIMEOUT | SDMMC_STAR_DCRCFAIL |
425 SDMMC_STAR_TXUNDERR | SDMMC_STAR_RXOVERR |
426 SDMMC_STAR_IDMATE)) != 0U) {
Yann Gautierfaef9022022-02-14 09:58:11 +0100427 ERROR("%s: Error flag (cmd = %u,status = %x)\n", __func__,
Yann Gautier5380b0d2018-10-15 09:36:04 +0200428 cmd->cmd_idx, status);
429 err = -EIO;
430 }
431
Yann Gautiere88fdd72018-11-30 15:22:11 +0100432err_exit:
Yann Gautier5380b0d2018-10-15 09:36:04 +0200433 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
434 mmio_clrbits_32(base + SDMMC_CMDR, SDMMC_CMDR_CMDTRANS);
435
Yann Gautier2299d572019-02-14 11:14:39 +0100436 if ((err != 0) && ((status & SDMMC_STAR_DPSMACT) != 0U)) {
Yann Gautiere88fdd72018-11-30 15:22:11 +0100437 int ret_stop = stm32_sdmmc2_stop_transfer();
438
439 if (ret_stop != 0) {
440 return ret_stop;
441 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200442 }
443
444 return err;
445}
446
447static int stm32_sdmmc2_send_cmd(struct mmc_cmd *cmd)
448{
Yann Gautierdbb9f572020-06-12 12:17:17 +0200449 uint8_t retry;
450 int err;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200451
452 assert(cmd != NULL);
453
Yann Gautierdbb9f572020-06-12 12:17:17 +0200454 for (retry = 0U; retry < 3U; retry++) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200455 err = stm32_sdmmc2_send_cmd_req(cmd);
456 if (err == 0) {
Yann Gautierdbb9f572020-06-12 12:17:17 +0200457 return 0;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200458 }
459
460 if ((cmd->cmd_idx == MMC_CMD(1)) ||
461 (cmd->cmd_idx == MMC_CMD(13))) {
462 return 0; /* Retry managed by framework */
463 }
464
465 /* Command 8 is expected to fail for eMMC */
Yann Gautierdbb9f572020-06-12 12:17:17 +0200466 if (cmd->cmd_idx != MMC_CMD(8)) {
467 WARN(" CMD%u, Retry: %u, Error: %d\n",
468 cmd->cmd_idx, retry + 1U, err);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200469 }
470
Yann Gautierdbb9f572020-06-12 12:17:17 +0200471 udelay(10U);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200472 }
473
474 return err;
475}
476
477static int stm32_sdmmc2_set_ios(unsigned int clk, unsigned int width)
478{
479 uintptr_t base = sdmmc2_params.reg_base;
480 uint32_t bus_cfg = 0;
Yann Gautier3194afe2019-05-28 11:54:50 +0200481 uint32_t clock_div, max_freq, freq;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200482 uint32_t clk_rate = sdmmc2_params.clk_rate;
483 uint32_t max_bus_freq = sdmmc2_params.device_info->max_bus_freq;
484
485 switch (width) {
486 case MMC_BUS_WIDTH_1:
487 break;
488 case MMC_BUS_WIDTH_4:
489 bus_cfg |= SDMMC_CLKCR_WIDBUS_4;
490 break;
491 case MMC_BUS_WIDTH_8:
492 bus_cfg |= SDMMC_CLKCR_WIDBUS_8;
493 break;
494 default:
495 panic();
496 break;
497 }
498
499 if (sdmmc2_params.device_info->mmc_dev_type == MMC_IS_EMMC) {
500 if (max_bus_freq >= 52000000U) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100501 max_freq = STM32MP_EMMC_HIGH_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200502 } else {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100503 max_freq = STM32MP_EMMC_NORMAL_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200504 }
505 } else {
506 if (max_bus_freq >= 50000000U) {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100507 max_freq = STM32MP_SD_HIGH_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200508 } else {
Yann Gautiera2e2a302019-02-14 11:13:39 +0100509 max_freq = STM32MP_SD_NORMAL_SPEED_MAX_FREQ;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200510 }
511 }
512
Yann Gautier3194afe2019-05-28 11:54:50 +0200513 if (sdmmc2_params.max_freq != 0U) {
514 freq = MIN(sdmmc2_params.max_freq, max_freq);
515 } else {
516 freq = max_freq;
517 }
518
519 clock_div = div_round_up(clk_rate, freq * 2U);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200520
521 mmio_write_32(base + SDMMC_CLKCR,
522 SDMMC_CLKCR_HWFC_EN | clock_div | bus_cfg |
523 sdmmc2_params.negedge |
524 sdmmc2_params.pin_ckin);
525
526 return 0;
527}
528
529static int stm32_sdmmc2_prepare(int lba, uintptr_t buf, size_t size)
530{
531 struct mmc_cmd cmd;
532 int ret;
533 uintptr_t base = sdmmc2_params.reg_base;
534 uint32_t data_ctrl = SDMMC_DCTRLR_DTDIR;
Yann Gautier6d9e6a02019-06-11 20:03:07 +0200535 uint32_t arg_size;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200536
Yann Gautierad631cc2022-11-21 13:36:53 +0100537 assert((size != 0U) && (size <= UINT32_MAX));
Yann Gautier6d9e6a02019-06-11 20:03:07 +0200538
539 if (size > MMC_BLOCK_SIZE) {
540 arg_size = MMC_BLOCK_SIZE;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200541 } else {
Yann Gautierad631cc2022-11-21 13:36:53 +0100542 arg_size = (uint32_t)size;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200543 }
544
545 sdmmc2_params.use_dma = plat_sdmmc2_use_dma(base, buf);
546
547 if (sdmmc2_params.use_dma) {
548 inv_dcache_range(buf, size);
549 }
550
551 /* Prepare CMD 16*/
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100552 mmio_write_32(base + SDMMC_DTIMER, 0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200553
554 mmio_write_32(base + SDMMC_DLENR, 0);
555
Yann Gautier1a3fc9f2019-01-17 14:35:22 +0100556 mmio_write_32(base + SDMMC_DCTRLR, 0);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200557
558 zeromem(&cmd, sizeof(struct mmc_cmd));
559
560 cmd.cmd_idx = MMC_CMD(16);
Yann Gautier6d9e6a02019-06-11 20:03:07 +0200561 cmd.cmd_arg = arg_size;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200562 cmd.resp_type = MMC_RESPONSE_R1;
563
564 ret = stm32_sdmmc2_send_cmd(&cmd);
565 if (ret != 0) {
566 ERROR("CMD16 failed\n");
567 return ret;
568 }
569
570 /* Prepare data command */
571 mmio_write_32(base + SDMMC_DTIMER, UINT32_MAX);
572
573 mmio_write_32(base + SDMMC_DLENR, size);
574
575 if (sdmmc2_params.use_dma) {
576 mmio_write_32(base + SDMMC_IDMACTRLR,
577 SDMMC_IDMACTRLR_IDMAEN);
578 mmio_write_32(base + SDMMC_IDMABASE0R, buf);
579
580 flush_dcache_range(buf, size);
581 }
582
Yann Gautier6d9e6a02019-06-11 20:03:07 +0200583 data_ctrl |= __builtin_ctz(arg_size) << SDMMC_DCTRLR_DBLOCKSIZE_SHIFT;
584
Yann Gautier5380b0d2018-10-15 09:36:04 +0200585 mmio_clrsetbits_32(base + SDMMC_DCTRLR,
586 SDMMC_DCTRLR_CLEAR_MASK,
587 data_ctrl);
588
589 return 0;
590}
591
592static int stm32_sdmmc2_read(int lba, uintptr_t buf, size_t size)
593{
594 uint32_t error_flags = SDMMC_STAR_RXOVERR | SDMMC_STAR_DCRCFAIL |
595 SDMMC_STAR_DTIMEOUT;
596 uint32_t flags = error_flags | SDMMC_STAR_DATAEND;
597 uint32_t status;
598 uint32_t *buffer;
599 uintptr_t base = sdmmc2_params.reg_base;
600 uintptr_t fifo_reg = base + SDMMC_FIFOR;
Yann Gautier2299d572019-02-14 11:14:39 +0100601 uint64_t timeout;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200602 int ret;
603
604 /* Assert buf is 4 bytes aligned */
605 assert((buf & GENMASK(1, 0)) == 0U);
606
607 buffer = (uint32_t *)buf;
608
609 if (sdmmc2_params.use_dma) {
610 inv_dcache_range(buf, size);
611
612 return 0;
613 }
614
615 if (size <= MMC_BLOCK_SIZE) {
616 flags |= SDMMC_STAR_DBCKEND;
617 }
618
Yann Gautier2299d572019-02-14 11:14:39 +0100619 timeout = timeout_init_us(TIMEOUT_US_1_S);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200620
621 do {
622 status = mmio_read_32(base + SDMMC_STAR);
623
624 if ((status & error_flags) != 0U) {
625 ERROR("%s: Read error (status = %x)\n", __func__,
626 status);
627 mmio_write_32(base + SDMMC_DCTRLR,
628 SDMMC_DCTRLR_FIFORST);
629
630 mmio_write_32(base + SDMMC_ICR,
631 SDMMC_STATIC_FLAGS);
632
633 ret = stm32_sdmmc2_stop_transfer();
634 if (ret != 0) {
635 return ret;
636 }
637
638 return -EIO;
639 }
640
Yann Gautier2299d572019-02-14 11:14:39 +0100641 if (timeout_elapsed(timeout)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200642 ERROR("%s: timeout 1s (status = %x)\n",
643 __func__, status);
644 mmio_write_32(base + SDMMC_ICR,
645 SDMMC_STATIC_FLAGS);
646
647 ret = stm32_sdmmc2_stop_transfer();
648 if (ret != 0) {
649 return ret;
650 }
651
652 return -ETIMEDOUT;
653 }
654
Yann Gautier2da3c972021-05-05 13:47:56 +0200655 if (size < (SDMMC_FIFO_SIZE / 2U)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200656 if ((mmio_read_32(base + SDMMC_DCNTR) > 0U) &&
657 ((status & SDMMC_STAR_RXFIFOE) == 0U)) {
658 *buffer = mmio_read_32(fifo_reg);
659 buffer++;
660 }
661 } else if ((status & SDMMC_STAR_RXFIFOHF) != 0U) {
662 uint32_t count;
663
664 /* Read data from SDMMC Rx FIFO */
Yann Gautier2da3c972021-05-05 13:47:56 +0200665 for (count = 0; count < (SDMMC_FIFO_SIZE / 2U);
666 count += sizeof(uint32_t)) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200667 *buffer = mmio_read_32(fifo_reg);
668 buffer++;
669 }
670 }
671 } while ((status & flags) == 0U);
672
673 mmio_write_32(base + SDMMC_ICR, SDMMC_STATIC_FLAGS);
674
675 if ((status & SDMMC_STAR_DPSMACT) != 0U) {
676 WARN("%s: DPSMACT=1, send stop\n", __func__);
677 return stm32_sdmmc2_stop_transfer();
678 }
679
680 return 0;
681}
682
683static int stm32_sdmmc2_write(int lba, uintptr_t buf, size_t size)
684{
685 return 0;
686}
687
688static int stm32_sdmmc2_dt_get_config(void)
689{
690 int sdmmc_node;
691 void *fdt = NULL;
692 const fdt32_t *cuint;
Yann Gautiere289cb02019-11-04 14:27:23 +0100693 struct dt_node_info dt_info;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200694
695 if (fdt_get_address(&fdt) == 0) {
696 return -FDT_ERR_NOTFOUND;
697 }
698
699 if (fdt == NULL) {
700 return -FDT_ERR_NOTFOUND;
701 }
702
Yann Gautiere289cb02019-11-04 14:27:23 +0100703 sdmmc_node = dt_match_instance_by_compatible(DT_SDMMC2_COMPAT,
704 sdmmc2_params.reg_base);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200705 if (sdmmc_node == -FDT_ERR_NOTFOUND) {
706 return -FDT_ERR_NOTFOUND;
707 }
708
Yann Gautiere289cb02019-11-04 14:27:23 +0100709 dt_fill_device_info(&dt_info, sdmmc_node);
710 if (dt_info.status == DT_DISABLED) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200711 return -FDT_ERR_NOTFOUND;
712 }
713
714 if (dt_set_pinctrl_config(sdmmc_node) != 0) {
715 return -FDT_ERR_BADVALUE;
716 }
717
Yann Gautiere289cb02019-11-04 14:27:23 +0100718 sdmmc2_params.clock_id = dt_info.clock;
719 sdmmc2_params.reset_id = dt_info.reset;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200720
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100721 if ((fdt_getprop(fdt, sdmmc_node, "st,use-ckin", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200722 sdmmc2_params.pin_ckin = SDMMC_CLKCR_SELCLKRX_0;
723 }
724
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100725 if ((fdt_getprop(fdt, sdmmc_node, "st,sig-dir", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200726 sdmmc2_params.dirpol = SDMMC_POWER_DIRPOL;
727 }
728
Yann Gautier7b7e4bf2019-01-17 19:16:03 +0100729 if ((fdt_getprop(fdt, sdmmc_node, "st,neg-edge", NULL)) != NULL) {
Yann Gautier5380b0d2018-10-15 09:36:04 +0200730 sdmmc2_params.negedge = SDMMC_CLKCR_NEGEDGE;
731 }
732
733 cuint = fdt_getprop(fdt, sdmmc_node, "bus-width", NULL);
734 if (cuint != NULL) {
735 switch (fdt32_to_cpu(*cuint)) {
736 case 4:
737 sdmmc2_params.bus_width = MMC_BUS_WIDTH_4;
738 break;
739
740 case 8:
741 sdmmc2_params.bus_width = MMC_BUS_WIDTH_8;
742 break;
743
744 default:
745 break;
746 }
747 }
748
Yann Gautier3194afe2019-05-28 11:54:50 +0200749 cuint = fdt_getprop(fdt, sdmmc_node, "max-frequency", NULL);
750 if (cuint != NULL) {
751 sdmmc2_params.max_freq = fdt32_to_cpu(*cuint);
752 }
753
Yann Gautierc14205d2019-05-10 16:01:34 +0200754 sdmmc2_params.vmmc_regu = regulator_get_by_supply_name(fdt, sdmmc_node, "vmmc");
755
Yann Gautier5380b0d2018-10-15 09:36:04 +0200756 return 0;
757}
758
759unsigned long long stm32_sdmmc2_mmc_get_device_size(void)
760{
761 return sdmmc2_params.device_info->device_size;
762}
763
764int stm32_sdmmc2_mmc_init(struct stm32_sdmmc2_params *params)
765{
Yann Gautier5380b0d2018-10-15 09:36:04 +0200766 assert((params != NULL) &&
767 ((params->reg_base & MMC_BLOCK_MASK) == 0U) &&
768 ((params->bus_width == MMC_BUS_WIDTH_1) ||
769 (params->bus_width == MMC_BUS_WIDTH_4) ||
770 (params->bus_width == MMC_BUS_WIDTH_8)));
771
772 memcpy(&sdmmc2_params, params, sizeof(struct stm32_sdmmc2_params));
773
Yann Gautierc14205d2019-05-10 16:01:34 +0200774 sdmmc2_params.vmmc_regu = NULL;
775
Yann Gautier5380b0d2018-10-15 09:36:04 +0200776 if (stm32_sdmmc2_dt_get_config() != 0) {
777 ERROR("%s: DT error\n", __func__);
778 return -ENOMEM;
779 }
780
Yann Gautiera205a5c2021-08-30 15:06:54 +0200781 clk_enable(sdmmc2_params.clock_id);
Yann Gautier5380b0d2018-10-15 09:36:04 +0200782
Yann Gautierfda489e2022-05-03 15:37:54 +0200783 if ((int)sdmmc2_params.reset_id >= 0) {
784 int rc;
785
786 rc = stm32mp_reset_assert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
787 if (rc != 0) {
788 panic();
789 }
790 udelay(2);
791 rc = stm32mp_reset_deassert(sdmmc2_params.reset_id, TIMEOUT_US_1_MS);
792 if (rc != 0) {
793 panic();
794 }
795 mdelay(1);
Etienne Carrieref02647a2019-12-08 08:14:40 +0100796 }
Yann Gautier5380b0d2018-10-15 09:36:04 +0200797
Yann Gautiera205a5c2021-08-30 15:06:54 +0200798 sdmmc2_params.clk_rate = clk_get_rate(sdmmc2_params.clock_id);
Yann Gautierc8fa1aa2019-03-08 10:59:00 +0100799 sdmmc2_params.device_info->ocr_voltage = OCR_3_2_3_3 | OCR_3_3_3_4;
Yann Gautier5380b0d2018-10-15 09:36:04 +0200800
801 return mmc_init(&stm32_sdmmc2_ops, sdmmc2_params.clk_rate,
802 sdmmc2_params.bus_width, sdmmc2_params.flags,
803 sdmmc2_params.device_info);
804}