blob: f6a27dc61362fb55b8df614cbbb4a61f0e51267c [file] [log] [blame]
Jun Nie988e3b62018-06-28 16:38:02 +08001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +02003 * Copyright 2025 NXP
Jun Nie988e3b62018-06-28 16:38:02 +08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Jun Nie988e3b62018-06-28 16:38:02 +08008#include <assert.h>
Jun Nie988e3b62018-06-28 16:38:02 +08009#include <errno.h>
Jun Nie988e3b62018-06-28 16:38:02 +080010#include <string.h>
11
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <arch.h>
13#include <arch_helpers.h>
14#include <common/debug.h>
15#include <drivers/delay_timer.h>
16#include <drivers/mmc.h>
17#include <lib/mmio.h>
Ghennadi Procopciucc2ed5cf2025-03-28 08:58:39 +020018#include <lib/xlat_tables/xlat_tables_v2.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019
20#include <imx_usdhc.h>
21
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +020022/* These masks represent the commands which involve a data transfer. */
23#define ADTC_MASK_SD (BIT_32(6U) | BIT_32(17U) | BIT_32(18U) |\
24 BIT_32(24U) | BIT_32(25U))
25#define ADTC_MASK_ACMD (BIT_64(51U))
26
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +020027struct imx_usdhc_device_data {
28 uint32_t addr;
29 uint32_t blk_size;
30 uint32_t blks;
31 bool valid;
32};
33
Jun Nie988e3b62018-06-28 16:38:02 +080034static void imx_usdhc_initialize(void);
35static int imx_usdhc_send_cmd(struct mmc_cmd *cmd);
36static int imx_usdhc_set_ios(unsigned int clk, unsigned int width);
37static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size);
38static int imx_usdhc_read(int lba, uintptr_t buf, size_t size);
39static int imx_usdhc_write(int lba, uintptr_t buf, size_t size);
40
41static const struct mmc_ops imx_usdhc_ops = {
42 .init = imx_usdhc_initialize,
43 .send_cmd = imx_usdhc_send_cmd,
44 .set_ios = imx_usdhc_set_ios,
45 .prepare = imx_usdhc_prepare,
46 .read = imx_usdhc_read,
47 .write = imx_usdhc_write,
48};
49
50static imx_usdhc_params_t imx_usdhc_params;
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +020051static struct imx_usdhc_device_data imx_usdhc_data;
52
53static bool imx_usdhc_is_buf_valid(void)
54{
55 return imx_usdhc_data.valid;
56}
57
58static bool imx_usdhc_is_buf_multiblk(void)
59{
60 return imx_usdhc_data.blks > 1U;
61}
62
63static void imx_usdhc_inval_buf_data(void)
64{
65 imx_usdhc_data.valid = false;
66}
67
68static int imx_usdhc_save_buf_data(uintptr_t buf, size_t size)
69{
70 uint32_t block_size;
71 uint64_t blks;
72
73 if (size <= MMC_BLOCK_SIZE) {
74 block_size = (uint32_t)size;
75 } else {
76 block_size = MMC_BLOCK_SIZE;
77 }
78
79 if (buf > UINT32_MAX) {
80 return -EOVERFLOW;
81 }
82
83 imx_usdhc_data.addr = (uint32_t)buf;
84 imx_usdhc_data.blk_size = block_size;
85 blks = size / block_size;
86 imx_usdhc_data.blks = (uint32_t)blks;
87
88 imx_usdhc_data.valid = true;
89
90 return 0;
91}
92
93static void imx_usdhc_write_buf_data(void)
94{
95 uintptr_t reg_base = imx_usdhc_params.reg_base;
96 uint32_t addr, blks, blk_size;
97
98 addr = imx_usdhc_data.addr;
99 blks = imx_usdhc_data.blks;
100 blk_size = imx_usdhc_data.blk_size;
101
102 mmio_write_32(reg_base + DSADDR, addr);
103 mmio_write_32(reg_base + BLKATT, BLKATT_BLKCNT(blks) |
104 BLKATT_BLKSIZE(blk_size));
105}
Jun Nie988e3b62018-06-28 16:38:02 +0800106
107#define IMX7_MMC_SRC_CLK_RATE (200 * 1000 * 1000)
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200108static void imx_usdhc_set_clk(unsigned int clk)
Jun Nie988e3b62018-06-28 16:38:02 +0800109{
Jun Nie988e3b62018-06-28 16:38:02 +0800110 unsigned int sdhc_clk = IMX7_MMC_SRC_CLK_RATE;
111 uintptr_t reg_base = imx_usdhc_params.reg_base;
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200112 unsigned int pre_div = 1U, div = 1U;
Jun Nie988e3b62018-06-28 16:38:02 +0800113
114 assert(clk > 0);
115
116 while (sdhc_clk / (16 * pre_div) > clk && pre_div < 256)
117 pre_div *= 2;
118
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200119 while (((sdhc_clk / (div * pre_div)) > clk) && (div < 16U)) {
Jun Nie988e3b62018-06-28 16:38:02 +0800120 div++;
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200121 }
Jun Nie988e3b62018-06-28 16:38:02 +0800122
123 pre_div >>= 1;
124 div -= 1;
125 clk = (pre_div << 8) | (div << 4);
126
Ghennadi Procopciuc8bad0c02025-03-28 08:44:47 +0200127 while ((mmio_read_32(reg_base + PSTATE) & PSTATE_SDSTB) == 0U) {
128 }
129
Jun Nie988e3b62018-06-28 16:38:02 +0800130 mmio_clrbits32(reg_base + VENDSPEC, VENDSPEC_CARD_CLKEN);
131 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_CLOCK_MASK, clk);
132 udelay(10000);
133
134 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_PER_CLKEN | VENDSPEC_CARD_CLKEN);
135}
136
137static void imx_usdhc_initialize(void)
138{
139 unsigned int timeout = 10000;
140 uintptr_t reg_base = imx_usdhc_params.reg_base;
141
142 assert((imx_usdhc_params.reg_base & MMC_BLOCK_MASK) == 0);
143
144 /* reset the controller */
145 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTA);
146
147 /* wait for reset done */
148 while ((mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTA)) {
149 if (!timeout)
150 ERROR("IMX MMC reset timeout.\n");
151 timeout--;
152 }
153
154 mmio_write_32(reg_base + MMCBOOT, 0);
155 mmio_write_32(reg_base + MIXCTRL, 0);
156 mmio_write_32(reg_base + CLKTUNECTRLSTS, 0);
157
158 mmio_write_32(reg_base + VENDSPEC, VENDSPEC_INIT);
159 mmio_write_32(reg_base + DLLCTRL, 0);
160 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_IPG_CLKEN | VENDSPEC_PER_CLKEN);
161
162 /* Set the initial boot clock rate */
163 imx_usdhc_set_clk(MMC_BOOT_CLK_RATE);
164 udelay(100);
165
166 /* Clear read/write ready status */
167 mmio_clrbits32(reg_base + INTSTATEN, INTSTATEN_BRR | INTSTATEN_BWR);
168
169 /* configure as little endian */
170 mmio_write_32(reg_base + PROTCTRL, PROTCTRL_LE);
171
172 /* Set timeout to the maximum value */
173 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_TIMEOUT_MASK,
174 SYSCTRL_TIMEOUT(15));
175
176 /* set wartermark level as 16 for safe for MMC */
177 mmio_clrsetbits32(reg_base + WATERMARKLEV, WMKLV_MASK, 16 | (16 << 16));
178}
179
180#define FSL_CMD_RETRIES 1000
181
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200182static bool is_data_transfer_to_card(const struct mmc_cmd *cmd)
183{
184 unsigned int cmd_idx = cmd->cmd_idx;
185
186 return (cmd_idx == MMC_CMD(24)) || (cmd_idx == MMC_CMD(25));
187}
188
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200189static bool is_data_transfer_cmd(const struct mmc_cmd *cmd)
190{
191 uintptr_t reg_base = imx_usdhc_params.reg_base;
192 unsigned int cmd_idx = cmd->cmd_idx;
193 uint32_t xfer_type;
194
195 xfer_type = mmio_read_32(reg_base + XFERTYPE);
196
197 if (XFERTYPE_GET_CMD(xfer_type) == MMC_CMD(55)) {
198 return (ADTC_MASK_ACMD & BIT_64(cmd_idx)) != 0ULL;
199 }
200
201 if ((ADTC_MASK_SD & BIT_32(cmd->cmd_idx)) != 0U) {
202 return true;
203 }
204
205 return false;
206}
207
208static int get_xfr_type(const struct mmc_cmd *cmd, bool data, uint32_t *xfertype)
209{
210 *xfertype = XFERTYPE_CMD(cmd->cmd_idx);
211
212 switch (cmd->resp_type) {
213 case MMC_RESPONSE_R2:
214 *xfertype |= XFERTYPE_RSPTYP_136;
215 *xfertype |= XFERTYPE_CCCEN;
216 break;
217 case MMC_RESPONSE_R4:
218 *xfertype |= XFERTYPE_RSPTYP_48;
219 break;
220 case MMC_RESPONSE_R6:
221 *xfertype |= XFERTYPE_RSPTYP_48;
222 *xfertype |= XFERTYPE_CICEN;
223 *xfertype |= XFERTYPE_CCCEN;
224 break;
225 case MMC_RESPONSE_R1B:
226 *xfertype |= XFERTYPE_RSPTYP_48_BUSY;
227 *xfertype |= XFERTYPE_CICEN;
228 *xfertype |= XFERTYPE_CCCEN;
229 break;
230 default:
231 ERROR("Invalid CMD response: %u\n", cmd->resp_type);
232 return -EINVAL;
233 }
234
235 if (data) {
236 *xfertype |= XFERTYPE_DPSEL;
237 }
238
239 return 0;
240}
241
Jun Nie988e3b62018-06-28 16:38:02 +0800242static int imx_usdhc_send_cmd(struct mmc_cmd *cmd)
243{
244 uintptr_t reg_base = imx_usdhc_params.reg_base;
Jun Nie988e3b62018-06-28 16:38:02 +0800245 unsigned int state, flags = INTSTATEN_CC | INTSTATEN_CTOE;
Ghennadi Procopciucbb0f9a02025-03-17 22:17:16 +0200246 unsigned int mixctl = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800247 unsigned int cmd_retries = 0;
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200248 uint32_t xfertype;
249 bool data;
250 int err = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800251
252 assert(cmd);
253
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200254 data = is_data_transfer_cmd(cmd);
255
256 err = get_xfr_type(cmd, data, &xfertype);
257 if (err != 0) {
258 return err;
259 }
260
Jun Nie988e3b62018-06-28 16:38:02 +0800261 /* clear all irq status */
262 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
263
264 /* Wait for the bus to be idle */
265 do {
266 state = mmio_read_32(reg_base + PSTATE);
267 } while (state & (PSTATE_CDIHB | PSTATE_CIHB));
268
269 while (mmio_read_32(reg_base + PSTATE) & PSTATE_DLA)
270 ;
271
272 mmio_write_32(reg_base + INTSIGEN, 0);
Jun Nie988e3b62018-06-28 16:38:02 +0800273
Jun Nie988e3b62018-06-28 16:38:02 +0800274 if (data) {
Jun Nie988e3b62018-06-28 16:38:02 +0800275 mixctl |= MIXCTRL_DMAEN;
276 }
277
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200278 if (!is_data_transfer_to_card(cmd)) {
279 mixctl |= MIXCTRL_DTDSEL;
280 }
281
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +0200282 if ((cmd->cmd_idx != MMC_CMD(55)) && imx_usdhc_is_buf_valid()) {
283 if (imx_usdhc_is_buf_multiblk()) {
284 mixctl |= MIXCTRL_MSBSEL | MIXCTRL_BCEN;
285 }
286
287 imx_usdhc_write_buf_data();
288 imx_usdhc_inval_buf_data();
289 }
290
Jun Nie988e3b62018-06-28 16:38:02 +0800291 /* Send the command */
292 mmio_write_32(reg_base + CMDARG, cmd->cmd_arg);
293 mmio_clrsetbits32(reg_base + MIXCTRL, MIXCTRL_DATMASK, mixctl);
294 mmio_write_32(reg_base + XFERTYPE, xfertype);
295
296 /* Wait for the command done */
297 do {
298 state = mmio_read_32(reg_base + INTSTAT);
299 if (cmd_retries)
300 udelay(1);
301 } while ((!(state & flags)) && ++cmd_retries < FSL_CMD_RETRIES);
302
303 if ((state & (INTSTATEN_CTOE | CMD_ERR)) || cmd_retries == FSL_CMD_RETRIES) {
304 if (cmd_retries == FSL_CMD_RETRIES)
305 err = -ETIMEDOUT;
306 else
307 err = -EIO;
308 ERROR("imx_usdhc mmc cmd %d state 0x%x errno=%d\n",
309 cmd->cmd_idx, state, err);
310 goto out;
311 }
312
313 /* Copy the response to the response buffer */
314 if (cmd->resp_type & MMC_RSP_136) {
315 unsigned int cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
316
317 cmdrsp3 = mmio_read_32(reg_base + CMDRSP3);
318 cmdrsp2 = mmio_read_32(reg_base + CMDRSP2);
319 cmdrsp1 = mmio_read_32(reg_base + CMDRSP1);
320 cmdrsp0 = mmio_read_32(reg_base + CMDRSP0);
321 cmd->resp_data[3] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
322 cmd->resp_data[2] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
323 cmd->resp_data[1] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
324 cmd->resp_data[0] = (cmdrsp0 << 8);
325 } else {
326 cmd->resp_data[0] = mmio_read_32(reg_base + CMDRSP0);
327 }
328
329 /* Wait until all of the blocks are transferred */
330 if (data) {
331 flags = DATA_COMPLETE;
332 do {
333 state = mmio_read_32(reg_base + INTSTAT);
334
335 if (state & (INTSTATEN_DTOE | DATA_ERR)) {
336 err = -EIO;
337 ERROR("imx_usdhc mmc data state 0x%x\n", state);
338 goto out;
339 }
340 } while ((state & flags) != flags);
341 }
342
343out:
344 /* Reset CMD and DATA on error */
345 if (err) {
346 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTC);
347 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTC)
348 ;
349
350 if (data) {
351 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTD);
352 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTD)
353 ;
354 }
355 }
356
357 /* clear all irq status */
358 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
359
360 return err;
361}
362
363static int imx_usdhc_set_ios(unsigned int clk, unsigned int width)
364{
365 uintptr_t reg_base = imx_usdhc_params.reg_base;
366
367 imx_usdhc_set_clk(clk);
368
369 if (width == MMC_BUS_WIDTH_4)
370 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
371 PROTCTRL_WIDTH_4);
372 else if (width == MMC_BUS_WIDTH_8)
373 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
374 PROTCTRL_WIDTH_8);
375
376 return 0;
377}
378
379static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size)
380{
Ghennadi Procopciuc92ede2c2025-03-17 23:07:23 +0200381 flush_dcache_range(buf, size);
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +0200382 return imx_usdhc_save_buf_data(buf, size);
Jun Nie988e3b62018-06-28 16:38:02 +0800383}
384
385static int imx_usdhc_read(int lba, uintptr_t buf, size_t size)
386{
Ghennadi Procopciuc92ede2c2025-03-17 23:07:23 +0200387 inv_dcache_range(buf, size);
Jun Nie988e3b62018-06-28 16:38:02 +0800388 return 0;
389}
390
391static int imx_usdhc_write(int lba, uintptr_t buf, size_t size)
392{
393 return 0;
394}
395
396void imx_usdhc_init(imx_usdhc_params_t *params,
397 struct mmc_device_info *mmc_dev_info)
398{
Ghennadi Procopciucc2ed5cf2025-03-28 08:58:39 +0200399 int ret __maybe_unused;
400
Jun Nie988e3b62018-06-28 16:38:02 +0800401 assert((params != 0) &&
402 ((params->reg_base & MMC_BLOCK_MASK) == 0) &&
Jun Nie988e3b62018-06-28 16:38:02 +0800403 ((params->bus_width == MMC_BUS_WIDTH_1) ||
404 (params->bus_width == MMC_BUS_WIDTH_4) ||
405 (params->bus_width == MMC_BUS_WIDTH_8)));
406
Ghennadi Procopciucc2ed5cf2025-03-28 08:58:39 +0200407#if PLAT_XLAT_TABLES_DYNAMIC
408 ret = mmap_add_dynamic_region(params->reg_base, params->reg_base,
409 PAGE_SIZE,
410 MT_DEVICE | MT_RW | MT_SECURE);
411 if (ret != 0) {
412 ERROR("Failed to map the uSDHC registers\n");
413 panic();
414 }
415#endif
416
Jun Nie988e3b62018-06-28 16:38:02 +0800417 memcpy(&imx_usdhc_params, params, sizeof(imx_usdhc_params_t));
418 mmc_init(&imx_usdhc_ops, params->clk_rate, params->bus_width,
419 params->flags, mmc_dev_info);
420}