blob: 610c3e614b0e9724882512e676149e9e382edf62 [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
Jun Nie988e3b62018-06-28 16:38:02 +080026static void imx_usdhc_initialize(void);
27static int imx_usdhc_send_cmd(struct mmc_cmd *cmd);
28static int imx_usdhc_set_ios(unsigned int clk, unsigned int width);
29static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size);
30static int imx_usdhc_read(int lba, uintptr_t buf, size_t size);
31static int imx_usdhc_write(int lba, uintptr_t buf, size_t size);
32
33static const struct mmc_ops imx_usdhc_ops = {
34 .init = imx_usdhc_initialize,
35 .send_cmd = imx_usdhc_send_cmd,
36 .set_ios = imx_usdhc_set_ios,
37 .prepare = imx_usdhc_prepare,
38 .read = imx_usdhc_read,
39 .write = imx_usdhc_write,
40};
41
42static imx_usdhc_params_t imx_usdhc_params;
43
44#define IMX7_MMC_SRC_CLK_RATE (200 * 1000 * 1000)
45static void imx_usdhc_set_clk(int clk)
46{
47 int div = 1;
48 int pre_div = 1;
49 unsigned int sdhc_clk = IMX7_MMC_SRC_CLK_RATE;
50 uintptr_t reg_base = imx_usdhc_params.reg_base;
51
52 assert(clk > 0);
53
54 while (sdhc_clk / (16 * pre_div) > clk && pre_div < 256)
55 pre_div *= 2;
56
57 while (sdhc_clk / div > clk && div < 16)
58 div++;
59
60 pre_div >>= 1;
61 div -= 1;
62 clk = (pre_div << 8) | (div << 4);
63
64 mmio_clrbits32(reg_base + VENDSPEC, VENDSPEC_CARD_CLKEN);
65 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_CLOCK_MASK, clk);
66 udelay(10000);
67
68 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_PER_CLKEN | VENDSPEC_CARD_CLKEN);
69}
70
71static void imx_usdhc_initialize(void)
72{
73 unsigned int timeout = 10000;
74 uintptr_t reg_base = imx_usdhc_params.reg_base;
75
76 assert((imx_usdhc_params.reg_base & MMC_BLOCK_MASK) == 0);
77
78 /* reset the controller */
79 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTA);
80
81 /* wait for reset done */
82 while ((mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTA)) {
83 if (!timeout)
84 ERROR("IMX MMC reset timeout.\n");
85 timeout--;
86 }
87
88 mmio_write_32(reg_base + MMCBOOT, 0);
89 mmio_write_32(reg_base + MIXCTRL, 0);
90 mmio_write_32(reg_base + CLKTUNECTRLSTS, 0);
91
92 mmio_write_32(reg_base + VENDSPEC, VENDSPEC_INIT);
93 mmio_write_32(reg_base + DLLCTRL, 0);
94 mmio_setbits32(reg_base + VENDSPEC, VENDSPEC_IPG_CLKEN | VENDSPEC_PER_CLKEN);
95
96 /* Set the initial boot clock rate */
97 imx_usdhc_set_clk(MMC_BOOT_CLK_RATE);
98 udelay(100);
99
100 /* Clear read/write ready status */
101 mmio_clrbits32(reg_base + INTSTATEN, INTSTATEN_BRR | INTSTATEN_BWR);
102
103 /* configure as little endian */
104 mmio_write_32(reg_base + PROTCTRL, PROTCTRL_LE);
105
106 /* Set timeout to the maximum value */
107 mmio_clrsetbits32(reg_base + SYSCTRL, SYSCTRL_TIMEOUT_MASK,
108 SYSCTRL_TIMEOUT(15));
109
110 /* set wartermark level as 16 for safe for MMC */
111 mmio_clrsetbits32(reg_base + WATERMARKLEV, WMKLV_MASK, 16 | (16 << 16));
112}
113
114#define FSL_CMD_RETRIES 1000
115
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200116static bool is_data_transfer_to_card(const struct mmc_cmd *cmd)
117{
118 unsigned int cmd_idx = cmd->cmd_idx;
119
120 return (cmd_idx == MMC_CMD(24)) || (cmd_idx == MMC_CMD(25));
121}
122
Ghennadi Procopciucbb0f9a02025-03-17 22:17:16 +0200123static bool is_multiple_block_transfer(const struct mmc_cmd *cmd)
124{
125 unsigned int cmd_idx = cmd->cmd_idx;
126
127 return cmd_idx == MMC_CMD(18) || cmd_idx == MMC_CMD(25);
128}
129
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200130static bool is_data_transfer_cmd(const struct mmc_cmd *cmd)
131{
132 uintptr_t reg_base = imx_usdhc_params.reg_base;
133 unsigned int cmd_idx = cmd->cmd_idx;
134 uint32_t xfer_type;
135
136 xfer_type = mmio_read_32(reg_base + XFERTYPE);
137
138 if (XFERTYPE_GET_CMD(xfer_type) == MMC_CMD(55)) {
139 return (ADTC_MASK_ACMD & BIT_64(cmd_idx)) != 0ULL;
140 }
141
142 if ((ADTC_MASK_SD & BIT_32(cmd->cmd_idx)) != 0U) {
143 return true;
144 }
145
146 return false;
147}
148
149static int get_xfr_type(const struct mmc_cmd *cmd, bool data, uint32_t *xfertype)
150{
151 *xfertype = XFERTYPE_CMD(cmd->cmd_idx);
152
153 switch (cmd->resp_type) {
154 case MMC_RESPONSE_R2:
155 *xfertype |= XFERTYPE_RSPTYP_136;
156 *xfertype |= XFERTYPE_CCCEN;
157 break;
158 case MMC_RESPONSE_R4:
159 *xfertype |= XFERTYPE_RSPTYP_48;
160 break;
161 case MMC_RESPONSE_R6:
162 *xfertype |= XFERTYPE_RSPTYP_48;
163 *xfertype |= XFERTYPE_CICEN;
164 *xfertype |= XFERTYPE_CCCEN;
165 break;
166 case MMC_RESPONSE_R1B:
167 *xfertype |= XFERTYPE_RSPTYP_48_BUSY;
168 *xfertype |= XFERTYPE_CICEN;
169 *xfertype |= XFERTYPE_CCCEN;
170 break;
171 default:
172 ERROR("Invalid CMD response: %u\n", cmd->resp_type);
173 return -EINVAL;
174 }
175
176 if (data) {
177 *xfertype |= XFERTYPE_DPSEL;
178 }
179
180 return 0;
181}
182
Jun Nie988e3b62018-06-28 16:38:02 +0800183static int imx_usdhc_send_cmd(struct mmc_cmd *cmd)
184{
185 uintptr_t reg_base = imx_usdhc_params.reg_base;
Jun Nie988e3b62018-06-28 16:38:02 +0800186 unsigned int state, flags = INTSTATEN_CC | INTSTATEN_CTOE;
Ghennadi Procopciucbb0f9a02025-03-17 22:17:16 +0200187 unsigned int mixctl = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800188 unsigned int cmd_retries = 0;
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200189 uint32_t xfertype;
190 bool data;
191 int err = 0;
Jun Nie988e3b62018-06-28 16:38:02 +0800192
193 assert(cmd);
194
Ghennadi Procopciuc3f642e82025-03-17 12:21:13 +0200195 data = is_data_transfer_cmd(cmd);
196
197 err = get_xfr_type(cmd, data, &xfertype);
198 if (err != 0) {
199 return err;
200 }
201
Jun Nie988e3b62018-06-28 16:38:02 +0800202 /* clear all irq status */
203 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
204
205 /* Wait for the bus to be idle */
206 do {
207 state = mmio_read_32(reg_base + PSTATE);
208 } while (state & (PSTATE_CDIHB | PSTATE_CIHB));
209
210 while (mmio_read_32(reg_base + PSTATE) & PSTATE_DLA)
211 ;
212
213 mmio_write_32(reg_base + INTSIGEN, 0);
214 udelay(1000);
215
Ghennadi Procopciucbb0f9a02025-03-17 22:17:16 +0200216 if (is_multiple_block_transfer(cmd)) {
Jun Nie988e3b62018-06-28 16:38:02 +0800217 mixctl |= MIXCTRL_MSBSEL;
218 mixctl |= MIXCTRL_BCEN;
219 }
220
221 if (data) {
Jun Nie988e3b62018-06-28 16:38:02 +0800222 mixctl |= MIXCTRL_DMAEN;
223 }
224
Ghennadi Procopciuc96e6ccc2025-03-17 15:17:54 +0200225 if (!is_data_transfer_to_card(cmd)) {
226 mixctl |= MIXCTRL_DTDSEL;
227 }
228
Jun Nie988e3b62018-06-28 16:38:02 +0800229 /* Send the command */
230 mmio_write_32(reg_base + CMDARG, cmd->cmd_arg);
231 mmio_clrsetbits32(reg_base + MIXCTRL, MIXCTRL_DATMASK, mixctl);
232 mmio_write_32(reg_base + XFERTYPE, xfertype);
233
234 /* Wait for the command done */
235 do {
236 state = mmio_read_32(reg_base + INTSTAT);
237 if (cmd_retries)
238 udelay(1);
239 } while ((!(state & flags)) && ++cmd_retries < FSL_CMD_RETRIES);
240
241 if ((state & (INTSTATEN_CTOE | CMD_ERR)) || cmd_retries == FSL_CMD_RETRIES) {
242 if (cmd_retries == FSL_CMD_RETRIES)
243 err = -ETIMEDOUT;
244 else
245 err = -EIO;
246 ERROR("imx_usdhc mmc cmd %d state 0x%x errno=%d\n",
247 cmd->cmd_idx, state, err);
248 goto out;
249 }
250
251 /* Copy the response to the response buffer */
252 if (cmd->resp_type & MMC_RSP_136) {
253 unsigned int cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
254
255 cmdrsp3 = mmio_read_32(reg_base + CMDRSP3);
256 cmdrsp2 = mmio_read_32(reg_base + CMDRSP2);
257 cmdrsp1 = mmio_read_32(reg_base + CMDRSP1);
258 cmdrsp0 = mmio_read_32(reg_base + CMDRSP0);
259 cmd->resp_data[3] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
260 cmd->resp_data[2] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
261 cmd->resp_data[1] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
262 cmd->resp_data[0] = (cmdrsp0 << 8);
263 } else {
264 cmd->resp_data[0] = mmio_read_32(reg_base + CMDRSP0);
265 }
266
267 /* Wait until all of the blocks are transferred */
268 if (data) {
269 flags = DATA_COMPLETE;
270 do {
271 state = mmio_read_32(reg_base + INTSTAT);
272
273 if (state & (INTSTATEN_DTOE | DATA_ERR)) {
274 err = -EIO;
275 ERROR("imx_usdhc mmc data state 0x%x\n", state);
276 goto out;
277 }
278 } while ((state & flags) != flags);
279 }
280
281out:
282 /* Reset CMD and DATA on error */
283 if (err) {
284 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTC);
285 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTC)
286 ;
287
288 if (data) {
289 mmio_setbits32(reg_base + SYSCTRL, SYSCTRL_RSTD);
290 while (mmio_read_32(reg_base + SYSCTRL) & SYSCTRL_RSTD)
291 ;
292 }
293 }
294
295 /* clear all irq status */
296 mmio_write_32(reg_base + INTSTAT, 0xffffffff);
297
298 return err;
299}
300
301static int imx_usdhc_set_ios(unsigned int clk, unsigned int width)
302{
303 uintptr_t reg_base = imx_usdhc_params.reg_base;
304
305 imx_usdhc_set_clk(clk);
306
307 if (width == MMC_BUS_WIDTH_4)
308 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
309 PROTCTRL_WIDTH_4);
310 else if (width == MMC_BUS_WIDTH_8)
311 mmio_clrsetbits32(reg_base + PROTCTRL, PROTCTRL_WIDTH_MASK,
312 PROTCTRL_WIDTH_8);
313
314 return 0;
315}
316
317static int imx_usdhc_prepare(int lba, uintptr_t buf, size_t size)
318{
319 uintptr_t reg_base = imx_usdhc_params.reg_base;
320
321 mmio_write_32(reg_base + DSADDR, buf);
322 mmio_write_32(reg_base + BLKATT,
323 (size / MMC_BLOCK_SIZE) << 16 | MMC_BLOCK_SIZE);
324
325 return 0;
326}
327
328static int imx_usdhc_read(int lba, uintptr_t buf, size_t size)
329{
330 return 0;
331}
332
333static int imx_usdhc_write(int lba, uintptr_t buf, size_t size)
334{
335 return 0;
336}
337
338void imx_usdhc_init(imx_usdhc_params_t *params,
339 struct mmc_device_info *mmc_dev_info)
340{
341 assert((params != 0) &&
342 ((params->reg_base & MMC_BLOCK_MASK) == 0) &&
Jun Nie988e3b62018-06-28 16:38:02 +0800343 ((params->bus_width == MMC_BUS_WIDTH_1) ||
344 (params->bus_width == MMC_BUS_WIDTH_4) ||
345 (params->bus_width == MMC_BUS_WIDTH_8)));
346
347 memcpy(&imx_usdhc_params, params, sizeof(imx_usdhc_params_t));
348 mmc_init(&imx_usdhc_ops, params->clk_rate, params->bus_width,
349 params->flags, mmc_dev_info);
350}