blob: f3eccb2b03c54c85c8ddb58c196ea02a4ccdda19 [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>
18
19#include <imx_usdhc.h>
20
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +020021/* These masks represent the commands which involve a data transfer. */
22#define ADTC_MASK_SD (BIT_32(6U) | BIT_32(17U) | BIT_32(18U) |\
23 BIT_32(24U) | BIT_32(25U))
24#define ADTC_MASK_ACMD (BIT_64(51U))
25
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +020026struct imx_usdhc_device_data {
27 uint32_t addr;
28 uint32_t blk_size;
29 uint32_t blks;
30 bool valid;
31};
32
Jun Nie988e3b62018-06-28 16:38:02 +080033static void imx_usdhc_initialize(void);
34static int imx_usdhc_send_cmd(struct mmc_cmd *cmd);
35static int imx_usdhc_set_ios(unsigned int clk, unsigned int width);
36static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size);
37static int imx_usdhc_read(int lba, uintptr_t buf, size_t size);
38static int imx_usdhc_write(int lba, uintptr_t buf, size_t size);
39
40static const struct mmc_ops imx_usdhc_ops = {
41 .init = imx_usdhc_initialize,
42 .send_cmd = imx_usdhc_send_cmd,
43 .set_ios = imx_usdhc_set_ios,
44 .prepare = imx_usdhc_prepare,
45 .read = imx_usdhc_read,
46 .write = imx_usdhc_write,
47};
48
49static imx_usdhc_params_t imx_usdhc_params;
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +020050static struct imx_usdhc_device_data imx_usdhc_data;
51
52static bool imx_usdhc_is_buf_valid(void)
53{
54 return imx_usdhc_data.valid;
55}
56
57static bool imx_usdhc_is_buf_multiblk(void)
58{
59 return imx_usdhc_data.blks > 1U;
60}
61
62static void imx_usdhc_inval_buf_data(void)
63{
64 imx_usdhc_data.valid = false;
65}
66
67static int imx_usdhc_save_buf_data(uintptr_t buf, size_t size)
68{
69 uint32_t block_size;
70 uint64_t blks;
71
72 if (size <= MMC_BLOCK_SIZE) {
73 block_size = (uint32_t)size;
74 } else {
75 block_size = MMC_BLOCK_SIZE;
76 }
77
78 if (buf > UINT32_MAX) {
79 return -EOVERFLOW;
80 }
81
82 imx_usdhc_data.addr = (uint32_t)buf;
83 imx_usdhc_data.blk_size = block_size;
84 blks = size / block_size;
85 imx_usdhc_data.blks = (uint32_t)blks;
86
87 imx_usdhc_data.valid = true;
88
89 return 0;
90}
91
92static void imx_usdhc_write_buf_data(void)
93{
94 uintptr_t reg_base = imx_usdhc_params.reg_base;
95 uint32_t addr, blks, blk_size;
96
97 addr = imx_usdhc_data.addr;
98 blks = imx_usdhc_data.blks;
99 blk_size = imx_usdhc_data.blk_size;
100
101 mmio_write_32(reg_base + DSADDR, addr);
102 mmio_write_32(reg_base + BLKATT, BLKATT_BLKCNT(blks) |
103 BLKATT_BLKSIZE(blk_size));
104}
Jun Nie988e3b62018-06-28 16:38:02 +0800105
106#define IMX7_MMC_SRC_CLK_RATE (200 * 1000 * 1000)
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200107static void imx_usdhc_set_clk(unsigned int clk)
Jun Nie988e3b62018-06-28 16:38:02 +0800108{
Jun Nie988e3b62018-06-28 16:38:02 +0800109 unsigned int sdhc_clk = IMX7_MMC_SRC_CLK_RATE;
110 uintptr_t reg_base = imx_usdhc_params.reg_base;
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200111 unsigned int pre_div = 1U, div = 1U;
Jun Nie988e3b62018-06-28 16:38:02 +0800112
113 assert(clk > 0);
114
115 while (sdhc_clk / (16 * pre_div) > clk && pre_div < 256)
116 pre_div *= 2;
117
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200118 while (((sdhc_clk / (div * pre_div)) > clk) && (div < 16U)) {
Jun Nie988e3b62018-06-28 16:38:02 +0800119 div++;
Ghennadi Procopciuc69459f72025-03-28 08:37:33 +0200120 }
Jun Nie988e3b62018-06-28 16:38:02 +0800121
122 pre_div >>= 1;
123 div -= 1;
124 clk = (pre_div << 8) | (div << 4);
125
Ghennadi Procopciuc8bad0c02025-03-28 08:44:47 +0200126 while ((mmio_read_32(reg_base + PSTATE) & PSTATE_SDSTB) == 0U) {
127 }
128
Jun Nie988e3b62018-06-28 16:38:02 +0800129 mmio_clrbits32(reg_base + VENDSPEC, VENDSPEC_CARD_CLKEN);
130 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_CLOCK_MASK, clk);
131 udelay(10000);
132
133 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_PER_CLKEN | VENDSPEC_CARD_CLKEN);
134}
135
136static void imx_usdhc_initialize(void)
137{
138 unsigned int timeout = 10000;
139 uintptr_t reg_base = imx_usdhc_params.reg_base;
140
141 assert((imx_usdhc_params.reg_base & MMC_BLOCK_MASK) == 0);
142
143 /* reset the controller */
144 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTA);
145
146 /* wait for reset done */
147 while ((mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTA)) {
148 if (!timeout)
149 ERROR("IMX MMC reset timeout.\n");
150 timeout--;
151 }
152
153 mmio_write_32(reg_base + MMCBOOT, 0);
154 mmio_write_32(reg_base + MIXCTRL, 0);
155 mmio_write_32(reg_base + CLKTUNECTRLSTS, 0);
156
157 mmio_write_32(reg_base + VENDSPEC, VENDSPEC_INIT);
158 mmio_write_32(reg_base + DLLCTRL, 0);
159 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_IPG_CLKEN | VENDSPEC_PER_CLKEN);
160
161 /* Set the initial boot clock rate */
162 imx_usdhc_set_clk(MMC_BOOT_CLK_RATE);
163 udelay(100);
164
165 /* Clear read/write ready status */
166 mmio_clrbits32(reg_base + INTSTATEN, INTSTATEN_BRR | INTSTATEN_BWR);
167
168 /* configure as little endian */
169 mmio_write_32(reg_base + PROTCTRL, PROTCTRL_LE);
170
171 /* Set timeout to the maximum value */
172 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_TIMEOUT_MASK,
173 SYSCTRL_TIMEOUT(15));
174
175 /* set wartermark level as 16 for safe for MMC */
176 mmio_clrsetbits32(reg_base + WATERMARKLEV, WMKLV_MASK, 16 | (16 << 16));
177}
178
179#define FSL_CMD_RETRIES 1000
180
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200181static bool is_data_transfer_to_card(const struct mmc_cmd *cmd)
182{
183 unsigned int cmd_idx = cmd->cmd_idx;
184
185 return (cmd_idx == MMC_CMD(24)) || (cmd_idx == MMC_CMD(25));
186}
187
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200188static bool is_data_transfer_cmd(const struct mmc_cmd *cmd)
189{
190 uintptr_t reg_base = imx_usdhc_params.reg_base;
191 unsigned int cmd_idx = cmd->cmd_idx;
192 uint32_t xfer_type;
193
194 xfer_type = mmio_read_32(reg_base + XFERTYPE);
195
196 if (XFERTYPE_GET_CMD(xfer_type) == MMC_CMD(55)) {
197 return (ADTC_MASK_ACMD & BIT_64(cmd_idx)) != 0ULL;
198 }
199
200 if ((ADTC_MASK_SD & BIT_32(cmd->cmd_idx)) != 0U) {
201 return true;
202 }
203
204 return false;
205}
206
207static int get_xfr_type(const struct mmc_cmd *cmd, bool data, uint32_t *xfertype)
208{
209 *xfertype = XFERTYPE_CMD(cmd->cmd_idx);
210
211 switch (cmd->resp_type) {
212 case MMC_RESPONSE_R2:
213 *xfertype |= XFERTYPE_RSPTYP_136;
214 *xfertype |= XFERTYPE_CCCEN;
215 break;
216 case MMC_RESPONSE_R4:
217 *xfertype |= XFERTYPE_RSPTYP_48;
218 break;
219 case MMC_RESPONSE_R6:
220 *xfertype |= XFERTYPE_RSPTYP_48;
221 *xfertype |= XFERTYPE_CICEN;
222 *xfertype |= XFERTYPE_CCCEN;
223 break;
224 case MMC_RESPONSE_R1B:
225 *xfertype |= XFERTYPE_RSPTYP_48_BUSY;
226 *xfertype |= XFERTYPE_CICEN;
227 *xfertype |= XFERTYPE_CCCEN;
228 break;
229 default:
230 ERROR("Invalid CMD response: %u\n", cmd->resp_type);
231 return -EINVAL;
232 }
233
234 if (data) {
235 *xfertype |= XFERTYPE_DPSEL;
236 }
237
238 return 0;
239}
240
Jun Nie988e3b62018-06-28 16:38:02 +0800241static int imx_usdhc_send_cmd(struct mmc_cmd *cmd)
242{
243 uintptr_t reg_base = imx_usdhc_params.reg_base;
Jun Nie988e3b62018-06-28 16:38:02 +0800244 unsigned int state, flags = INTSTATEN_CC | INTSTATEN_CTOE;
Ghennadi Procopciucbb0f9a02025-03-17 22:17:16 +0200245 unsigned int mixctl = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800246 unsigned int cmd_retries = 0;
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200247 uint32_t xfertype;
248 bool data;
249 int err = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800250
251 assert(cmd);
252
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200253 data = is_data_transfer_cmd(cmd);
254
255 err = get_xfr_type(cmd, data, &xfertype);
256 if (err != 0) {
257 return err;
258 }
259
Jun Nie988e3b62018-06-28 16:38:02 +0800260 /* clear all irq status */
261 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
262
263 /* Wait for the bus to be idle */
264 do {
265 state = mmio_read_32(reg_base + PSTATE);
266 } while (state & (PSTATE_CDIHB | PSTATE_CIHB));
267
268 while (mmio_read_32(reg_base + PSTATE) & PSTATE_DLA)
269 ;
270
271 mmio_write_32(reg_base + INTSIGEN, 0);
Jun Nie988e3b62018-06-28 16:38:02 +0800272
Jun Nie988e3b62018-06-28 16:38:02 +0800273 if (data) {
Jun Nie988e3b62018-06-28 16:38:02 +0800274 mixctl |= MIXCTRL_DMAEN;
275 }
276
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200277 if (!is_data_transfer_to_card(cmd)) {
278 mixctl |= MIXCTRL_DTDSEL;
279 }
280
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +0200281 if ((cmd->cmd_idx != MMC_CMD(55)) && imx_usdhc_is_buf_valid()) {
282 if (imx_usdhc_is_buf_multiblk()) {
283 mixctl |= MIXCTRL_MSBSEL | MIXCTRL_BCEN;
284 }
285
286 imx_usdhc_write_buf_data();
287 imx_usdhc_inval_buf_data();
288 }
289
Jun Nie988e3b62018-06-28 16:38:02 +0800290 /* Send the command */
291 mmio_write_32(reg_base + CMDARG, cmd->cmd_arg);
292 mmio_clrsetbits32(reg_base + MIXCTRL, MIXCTRL_DATMASK, mixctl);
293 mmio_write_32(reg_base + XFERTYPE, xfertype);
294
295 /* Wait for the command done */
296 do {
297 state = mmio_read_32(reg_base + INTSTAT);
298 if (cmd_retries)
299 udelay(1);
300 } while ((!(state & flags)) && ++cmd_retries < FSL_CMD_RETRIES);
301
302 if ((state & (INTSTATEN_CTOE | CMD_ERR)) || cmd_retries == FSL_CMD_RETRIES) {
303 if (cmd_retries == FSL_CMD_RETRIES)
304 err = -ETIMEDOUT;
305 else
306 err = -EIO;
307 ERROR("imx_usdhc mmc cmd %d state 0x%x errno=%d\n",
308 cmd->cmd_idx, state, err);
309 goto out;
310 }
311
312 /* Copy the response to the response buffer */
313 if (cmd->resp_type & MMC_RSP_136) {
314 unsigned int cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
315
316 cmdrsp3 = mmio_read_32(reg_base + CMDRSP3);
317 cmdrsp2 = mmio_read_32(reg_base + CMDRSP2);
318 cmdrsp1 = mmio_read_32(reg_base + CMDRSP1);
319 cmdrsp0 = mmio_read_32(reg_base + CMDRSP0);
320 cmd->resp_data[3] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
321 cmd->resp_data[2] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
322 cmd->resp_data[1] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
323 cmd->resp_data[0] = (cmdrsp0 << 8);
324 } else {
325 cmd->resp_data[0] = mmio_read_32(reg_base + CMDRSP0);
326 }
327
328 /* Wait until all of the blocks are transferred */
329 if (data) {
330 flags = DATA_COMPLETE;
331 do {
332 state = mmio_read_32(reg_base + INTSTAT);
333
334 if (state & (INTSTATEN_DTOE | DATA_ERR)) {
335 err = -EIO;
336 ERROR("imx_usdhc mmc data state 0x%x\n", state);
337 goto out;
338 }
339 } while ((state & flags) != flags);
340 }
341
342out:
343 /* Reset CMD and DATA on error */
344 if (err) {
345 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTC);
346 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTC)
347 ;
348
349 if (data) {
350 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTD);
351 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTD)
352 ;
353 }
354 }
355
356 /* clear all irq status */
357 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
358
359 return err;
360}
361
362static int imx_usdhc_set_ios(unsigned int clk, unsigned int width)
363{
364 uintptr_t reg_base = imx_usdhc_params.reg_base;
365
366 imx_usdhc_set_clk(clk);
367
368 if (width == MMC_BUS_WIDTH_4)
369 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
370 PROTCTRL_WIDTH_4);
371 else if (width == MMC_BUS_WIDTH_8)
372 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
373 PROTCTRL_WIDTH_8);
374
375 return 0;
376}
377
378static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size)
379{
Ghennadi Procopciuc92ede2c2025-03-17 23:07:23 +0200380 flush_dcache_range(buf, size);
Ghennadi Procopciuc8da3b432025-03-17 22:54:41 +0200381 return imx_usdhc_save_buf_data(buf, size);
Jun Nie988e3b62018-06-28 16:38:02 +0800382}
383
384static int imx_usdhc_read(int lba, uintptr_t buf, size_t size)
385{
Ghennadi Procopciuc92ede2c2025-03-17 23:07:23 +0200386 inv_dcache_range(buf, size);
Jun Nie988e3b62018-06-28 16:38:02 +0800387 return 0;
388}
389
390static int imx_usdhc_write(int lba, uintptr_t buf, size_t size)
391{
392 return 0;
393}
394
395void imx_usdhc_init(imx_usdhc_params_t *params,
396 struct mmc_device_info *mmc_dev_info)
397{
398 assert((params != 0) &&
399 ((params->reg_base & MMC_BLOCK_MASK) == 0) &&
Jun Nie988e3b62018-06-28 16:38:02 +0800400 ((params->bus_width == MMC_BUS_WIDTH_1) ||
401 (params->bus_width == MMC_BUS_WIDTH_4) ||
402 (params->bus_width == MMC_BUS_WIDTH_8)));
403
404 memcpy(&imx_usdhc_params, params, sizeof(imx_usdhc_params_t));
405 mmc_init(&imx_usdhc_ops, params->clk_rate, params->bus_width,
406 params->flags, mmc_dev_info);
407}