1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Freescale eSDHC i.MX controller driver for the platform bus.
4 *
5 * derived from the OF-version.
6 *
7 * Copyright (c) 2010 Pengutronix e.K.
8 * Author: Wolfram Sang <kernel@pengutronix.de>
9 */
10
11 #include <linux/bitfield.h>
12 #include <linux/io.h>
13 #include <linux/iopoll.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/pm_qos.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sdio.h>
23 #include <linux/mmc/slot-gpio.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/pinctrl/consumer.h>
27 #include <linux/pm_runtime.h>
28 #include "sdhci-cqhci.h"
29 #include "sdhci-pltfm.h"
30 #include "sdhci-esdhc.h"
31 #include "cqhci.h"
32
33 #define ESDHC_SYS_CTRL_DTOCV_MASK 0x0f
34 #define ESDHC_CTRL_D3CD 0x08
35 #define ESDHC_BURST_LEN_EN_INCR (1 << 27)
36 /* VENDOR SPEC register */
37 #define ESDHC_VENDOR_SPEC 0xc0
38 #define ESDHC_VENDOR_SPEC_SDIO_QUIRK (1 << 1)
39 #define ESDHC_VENDOR_SPEC_VSELECT (1 << 1)
40 #define ESDHC_VENDOR_SPEC_FRC_SDCLK_ON (1 << 8)
41 #define ESDHC_DEBUG_SEL_AND_STATUS_REG 0xc2
42 #define ESDHC_DEBUG_SEL_REG 0xc3
43 #define ESDHC_DEBUG_SEL_MASK 0xf
44 #define ESDHC_DEBUG_SEL_CMD_STATE 1
45 #define ESDHC_DEBUG_SEL_DATA_STATE 2
46 #define ESDHC_DEBUG_SEL_TRANS_STATE 3
47 #define ESDHC_DEBUG_SEL_DMA_STATE 4
48 #define ESDHC_DEBUG_SEL_ADMA_STATE 5
49 #define ESDHC_DEBUG_SEL_FIFO_STATE 6
50 #define ESDHC_DEBUG_SEL_ASYNC_FIFO_STATE 7
51 #define ESDHC_WTMK_LVL 0x44
52 #define ESDHC_WTMK_DEFAULT_VAL 0x10401040
53 #define ESDHC_WTMK_LVL_RD_WML_MASK 0x000000FF
54 #define ESDHC_WTMK_LVL_RD_WML_SHIFT 0
55 #define ESDHC_WTMK_LVL_WR_WML_MASK 0x00FF0000
56 #define ESDHC_WTMK_LVL_WR_WML_SHIFT 16
57 #define ESDHC_WTMK_LVL_WML_VAL_DEF 64
58 #define ESDHC_WTMK_LVL_WML_VAL_MAX 128
59 #define ESDHC_MIX_CTRL 0x48
60 #define ESDHC_MIX_CTRL_DDREN (1 << 3)
61 #define ESDHC_MIX_CTRL_AC23EN (1 << 7)
62 #define ESDHC_MIX_CTRL_EXE_TUNE (1 << 22)
63 #define ESDHC_MIX_CTRL_SMPCLK_SEL (1 << 23)
64 #define ESDHC_MIX_CTRL_AUTO_TUNE_EN (1 << 24)
65 #define ESDHC_MIX_CTRL_FBCLK_SEL (1 << 25)
66 #define ESDHC_MIX_CTRL_HS400_EN (1 << 26)
67 #define ESDHC_MIX_CTRL_HS400_ES_EN (1 << 27)
68 /* Bits 3 and 6 are not SDHCI standard definitions */
69 #define ESDHC_MIX_CTRL_SDHCI_MASK 0xb7
70 /* Tuning bits */
71 #define ESDHC_MIX_CTRL_TUNING_MASK 0x03c00000
72
73 /* dll control register */
74 #define ESDHC_DLL_CTRL 0x60
75 #define ESDHC_DLL_OVERRIDE_VAL_SHIFT 9
76 #define ESDHC_DLL_OVERRIDE_EN_SHIFT 8
77
78 /* tune control register */
79 #define ESDHC_TUNE_CTRL_STATUS 0x68
80 #define ESDHC_TUNE_CTRL_STEP 1
81 #define ESDHC_TUNE_CTRL_MIN 0
82 #define ESDHC_TUNE_CTRL_MAX ((1 << 7) - 1)
83
84 /* strobe dll register */
85 #define ESDHC_STROBE_DLL_CTRL 0x70
86 #define ESDHC_STROBE_DLL_CTRL_ENABLE (1 << 0)
87 #define ESDHC_STROBE_DLL_CTRL_RESET (1 << 1)
88 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT 0x7
89 #define ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT 3
90 #define ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT (4 << 20)
91
92 #define ESDHC_STROBE_DLL_STATUS 0x74
93 #define ESDHC_STROBE_DLL_STS_REF_LOCK (1 << 1)
94 #define ESDHC_STROBE_DLL_STS_SLV_LOCK 0x1
95
96 #define ESDHC_VEND_SPEC2 0xc8
97 #define ESDHC_VEND_SPEC2_EN_BUSY_IRQ (1 << 8)
98 #define ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN (1 << 4)
99 #define ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN (0 << 4)
100 #define ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN (2 << 4)
101 #define ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN (1 << 6)
102 #define ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK (7 << 4)
103
104 #define ESDHC_TUNING_CTRL 0xcc
105 #define ESDHC_STD_TUNING_EN (1 << 24)
106 /* NOTE: the minimum valid tuning start tap for mx6sl is 1 */
107 #define ESDHC_TUNING_START_TAP_DEFAULT 0x1
108 #define ESDHC_TUNING_START_TAP_MASK 0x7f
109 #define ESDHC_TUNING_CMD_CRC_CHECK_DISABLE (1 << 7)
110 #define ESDHC_TUNING_STEP_MASK 0x00070000
111 #define ESDHC_TUNING_STEP_SHIFT 16
112
113 /* pinctrl state */
114 #define ESDHC_PINCTRL_STATE_100MHZ "state_100mhz"
115 #define ESDHC_PINCTRL_STATE_200MHZ "state_200mhz"
116
117 /*
118 * Our interpretation of the SDHCI_HOST_CONTROL register
119 */
120 #define ESDHC_CTRL_4BITBUS (0x1 << 1)
121 #define ESDHC_CTRL_8BITBUS (0x2 << 1)
122 #define ESDHC_CTRL_BUSWIDTH_MASK (0x3 << 1)
123 #define USDHC_GET_BUSWIDTH(c) (c & ESDHC_CTRL_BUSWIDTH_MASK)
124
125 /*
126 * There is an INT DMA ERR mismatch between eSDHC and STD SDHC SPEC:
127 * Bit25 is used in STD SPEC, and is reserved in fsl eSDHC design,
128 * but bit28 is used as the INT DMA ERR in fsl eSDHC design.
129 * Define this macro DMA error INT for fsl eSDHC
130 */
131 #define ESDHC_INT_VENDOR_SPEC_DMA_ERR (1 << 28)
132
133 /* the address offset of CQHCI */
134 #define ESDHC_CQHCI_ADDR_OFFSET 0x100
135
136 /*
137 * The CMDTYPE of the CMD register (offset 0xE) should be set to
138 * "11" when the STOP CMD12 is issued on imx53 to abort one
139 * open ended multi-blk IO. Otherwise the TC INT wouldn't
140 * be generated.
141 * In exact block transfer, the controller doesn't complete the
142 * operations automatically as required at the end of the
143 * transfer and remains on hold if the abort command is not sent.
144 * As a result, the TC flag is not asserted and SW received timeout
145 * exception. Bit1 of Vendor Spec register is used to fix it.
146 */
147 #define ESDHC_FLAG_MULTIBLK_NO_INT BIT(1)
148 /*
149 * The flag tells that the ESDHC controller is an USDHC block that is
150 * integrated on the i.MX6 series.
151 */
152 #define ESDHC_FLAG_USDHC BIT(3)
153 /* The IP supports manual tuning process */
154 #define ESDHC_FLAG_MAN_TUNING BIT(4)
155 /* The IP supports standard tuning process */
156 #define ESDHC_FLAG_STD_TUNING BIT(5)
157 /* The IP has SDHCI_CAPABILITIES_1 register */
158 #define ESDHC_FLAG_HAVE_CAP1 BIT(6)
159 /*
160 * The IP has erratum ERR004536
161 * uSDHC: ADMA Length Mismatch Error occurs if the AHB read access is slow,
162 * when reading data from the card
163 * This flag is also set for i.MX25 and i.MX35 in order to get
164 * SDHCI_QUIRK_BROKEN_ADMA, but for different reasons (ADMA capability bits).
165 */
166 #define ESDHC_FLAG_ERR004536 BIT(7)
167 /* The IP supports HS200 mode */
168 #define ESDHC_FLAG_HS200 BIT(8)
169 /* The IP supports HS400 mode */
170 #define ESDHC_FLAG_HS400 BIT(9)
171 /*
172 * The IP has errata ERR010450
173 * uSDHC: Due to the I/O timing limit, for SDR mode, SD card clock can't
174 * exceed 150MHz, for DDR mode, SD card clock can't exceed 45MHz.
175 */
176 #define ESDHC_FLAG_ERR010450 BIT(10)
177 /* The IP supports HS400ES mode */
178 #define ESDHC_FLAG_HS400_ES BIT(11)
179 /* The IP has Host Controller Interface for Command Queuing */
180 #define ESDHC_FLAG_CQHCI BIT(12)
181 /* need request pmqos during low power */
182 #define ESDHC_FLAG_PMQOS BIT(13)
183 /* The IP state got lost in low power mode */
184 #define ESDHC_FLAG_STATE_LOST_IN_LPMODE BIT(14)
185 /* The IP lost clock rate in PM_RUNTIME */
186 #define ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME BIT(15)
187 /*
188 * The IP do not support the ACMD23 feature completely when use ADMA mode.
189 * In ADMA mode, it only use the 16 bit block count of the register 0x4
190 * (BLOCK_ATT) as the CMD23's argument for ACMD23 mode, which means it will
191 * ignore the upper 16 bit of the CMD23's argument. This will block the reliable
192 * write operation in RPMB, because RPMB reliable write need to set the bit31
193 * of the CMD23's argument.
194 * imx6qpdl/imx6sx/imx6sl/imx7d has this limitation only for ADMA mode, SDMA
195 * do not has this limitation. so when these SoC use ADMA mode, it need to
196 * disable the ACMD23 feature.
197 */
198 #define ESDHC_FLAG_BROKEN_AUTO_CMD23 BIT(16)
199
200 /* ERR004536 is not applicable for the IP */
201 #define ESDHC_FLAG_SKIP_ERR004536 BIT(17)
202
203 enum wp_types {
204 ESDHC_WP_NONE, /* no WP, neither controller nor gpio */
205 ESDHC_WP_CONTROLLER, /* mmc controller internal WP */
206 ESDHC_WP_GPIO, /* external gpio pin for WP */
207 };
208
209 enum cd_types {
210 ESDHC_CD_NONE, /* no CD, neither controller nor gpio */
211 ESDHC_CD_CONTROLLER, /* mmc controller internal CD */
212 ESDHC_CD_GPIO, /* external gpio pin for CD */
213 ESDHC_CD_PERMANENT, /* no CD, card permanently wired to host */
214 };
215
216 /*
217 * struct esdhc_platform_data - platform data for esdhc on i.MX
218 *
219 * ESDHC_WP(CD)_CONTROLLER type is not available on i.MX25/35.
220 *
221 * @wp_type: type of write_protect method (see wp_types enum above)
222 * @cd_type: type of card_detect method (see cd_types enum above)
223 */
224
225 struct esdhc_platform_data {
226 enum wp_types wp_type;
227 enum cd_types cd_type;
228 int max_bus_width;
229 unsigned int delay_line;
230 unsigned int tuning_step; /* The delay cell steps in tuning procedure */
231 unsigned int tuning_start_tap; /* The start delay cell point in tuning procedure */
232 unsigned int strobe_dll_delay_target; /* The delay cell for strobe pad (read clock) */
233 };
234
235 struct esdhc_soc_data {
236 u32 flags;
237 };
238
239 static const struct esdhc_soc_data esdhc_imx25_data = {
240 .flags = ESDHC_FLAG_ERR004536,
241 };
242
243 static const struct esdhc_soc_data esdhc_imx35_data = {
244 .flags = ESDHC_FLAG_ERR004536,
245 };
246
247 static const struct esdhc_soc_data esdhc_imx51_data = {
248 .flags = 0,
249 };
250
251 static const struct esdhc_soc_data esdhc_imx53_data = {
252 .flags = ESDHC_FLAG_MULTIBLK_NO_INT,
253 };
254
255 static const struct esdhc_soc_data usdhc_imx6q_data = {
256 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
257 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
258 };
259
260 static const struct esdhc_soc_data usdhc_imx6sl_data = {
261 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
262 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_ERR004536
263 | ESDHC_FLAG_HS200
264 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
265 };
266
267 static const struct esdhc_soc_data usdhc_imx6sll_data = {
268 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
269 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
270 | ESDHC_FLAG_HS400
271 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
272 };
273
274 static const struct esdhc_soc_data usdhc_imx6sx_data = {
275 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
276 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
277 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
278 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
279 };
280
281 static const struct esdhc_soc_data usdhc_imx6ull_data = {
282 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
283 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
284 | ESDHC_FLAG_ERR010450
285 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
286 };
287
288 static const struct esdhc_soc_data usdhc_imx7d_data = {
289 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
290 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
291 | ESDHC_FLAG_HS400
292 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
293 | ESDHC_FLAG_BROKEN_AUTO_CMD23,
294 };
295
296 static struct esdhc_soc_data usdhc_s32g2_data = {
297 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_MAN_TUNING
298 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
299 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
300 | ESDHC_FLAG_SKIP_ERR004536,
301 };
302
303 static struct esdhc_soc_data usdhc_imx7ulp_data = {
304 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
305 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
306 | ESDHC_FLAG_PMQOS | ESDHC_FLAG_HS400
307 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
308 };
309 static struct esdhc_soc_data usdhc_imxrt1050_data = {
310 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_HS200 | ESDHC_FLAG_ERR004536,
311 };
312
313 static struct esdhc_soc_data usdhc_imx8qxp_data = {
314 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
315 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
316 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
317 | ESDHC_FLAG_STATE_LOST_IN_LPMODE
318 | ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME,
319 };
320
321 static struct esdhc_soc_data usdhc_imx8mm_data = {
322 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
323 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
324 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES
325 | ESDHC_FLAG_STATE_LOST_IN_LPMODE,
326 };
327
328 struct pltfm_imx_data {
329 u32 scratchpad;
330 struct pinctrl *pinctrl;
331 struct pinctrl_state *pins_100mhz;
332 struct pinctrl_state *pins_200mhz;
333 const struct esdhc_soc_data *socdata;
334 struct esdhc_platform_data boarddata;
335 struct clk *clk_ipg;
336 struct clk *clk_ahb;
337 struct clk *clk_per;
338 unsigned int actual_clock;
339 enum {
340 NO_CMD_PENDING, /* no multiblock command pending */
341 MULTIBLK_IN_PROCESS, /* exact multiblock cmd in process */
342 WAIT_FOR_INT, /* sent CMD12, waiting for response INT */
343 } multiblock_status;
344 u32 is_ddr;
345 struct pm_qos_request pm_qos_req;
346 };
347
348 static const struct of_device_id imx_esdhc_dt_ids[] = {
349 { .compatible = "fsl,imx25-esdhc", .data = &esdhc_imx25_data, },
350 { .compatible = "fsl,imx35-esdhc", .data = &esdhc_imx35_data, },
351 { .compatible = "fsl,imx51-esdhc", .data = &esdhc_imx51_data, },
352 { .compatible = "fsl,imx53-esdhc", .data = &esdhc_imx53_data, },
353 { .compatible = "fsl,imx6sx-usdhc", .data = &usdhc_imx6sx_data, },
354 { .compatible = "fsl,imx6sl-usdhc", .data = &usdhc_imx6sl_data, },
355 { .compatible = "fsl,imx6sll-usdhc", .data = &usdhc_imx6sll_data, },
356 { .compatible = "fsl,imx6q-usdhc", .data = &usdhc_imx6q_data, },
357 { .compatible = "fsl,imx6ull-usdhc", .data = &usdhc_imx6ull_data, },
358 { .compatible = "fsl,imx7d-usdhc", .data = &usdhc_imx7d_data, },
359 { .compatible = "fsl,imx7ulp-usdhc", .data = &usdhc_imx7ulp_data, },
360 { .compatible = "fsl,imx8qxp-usdhc", .data = &usdhc_imx8qxp_data, },
361 { .compatible = "fsl,imx8mm-usdhc", .data = &usdhc_imx8mm_data, },
362 { .compatible = "fsl,imxrt1050-usdhc", .data = &usdhc_imxrt1050_data, },
363 { .compatible = "nxp,s32g2-usdhc", .data = &usdhc_s32g2_data, },
364 { /* sentinel */ }
365 };
366 MODULE_DEVICE_TABLE(of, imx_esdhc_dt_ids);
367
is_imx25_esdhc(struct pltfm_imx_data * data)368 static inline int is_imx25_esdhc(struct pltfm_imx_data *data)
369 {
370 return data->socdata == &esdhc_imx25_data;
371 }
372
is_imx53_esdhc(struct pltfm_imx_data * data)373 static inline int is_imx53_esdhc(struct pltfm_imx_data *data)
374 {
375 return data->socdata == &esdhc_imx53_data;
376 }
377
esdhc_is_usdhc(struct pltfm_imx_data * data)378 static inline int esdhc_is_usdhc(struct pltfm_imx_data *data)
379 {
380 return !!(data->socdata->flags & ESDHC_FLAG_USDHC);
381 }
382
esdhc_clrset_le(struct sdhci_host * host,u32 mask,u32 val,int reg)383 static inline void esdhc_clrset_le(struct sdhci_host *host, u32 mask, u32 val, int reg)
384 {
385 void __iomem *base = host->ioaddr + (reg & ~0x3);
386 u32 shift = (reg & 0x3) * 8;
387
388 writel(((readl(base) & ~(mask << shift)) | (val << shift)), base);
389 }
390
391 #define DRIVER_NAME "sdhci-esdhc-imx"
392 #define ESDHC_IMX_DUMP(f, x...) \
393 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
esdhc_dump_debug_regs(struct sdhci_host * host)394 static void esdhc_dump_debug_regs(struct sdhci_host *host)
395 {
396 int i;
397 char *debug_status[7] = {
398 "cmd debug status",
399 "data debug status",
400 "trans debug status",
401 "dma debug status",
402 "adma debug status",
403 "fifo debug status",
404 "async fifo debug status"
405 };
406
407 ESDHC_IMX_DUMP("========= ESDHC IMX DEBUG STATUS DUMP =========\n");
408 for (i = 0; i < 7; i++) {
409 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK,
410 ESDHC_DEBUG_SEL_CMD_STATE + i, ESDHC_DEBUG_SEL_REG);
411 ESDHC_IMX_DUMP("%s: 0x%04x\n", debug_status[i],
412 readw(host->ioaddr + ESDHC_DEBUG_SEL_AND_STATUS_REG));
413 }
414
415 esdhc_clrset_le(host, ESDHC_DEBUG_SEL_MASK, 0, ESDHC_DEBUG_SEL_REG);
416
417 }
418
esdhc_wait_for_card_clock_gate_off(struct sdhci_host * host)419 static inline void esdhc_wait_for_card_clock_gate_off(struct sdhci_host *host)
420 {
421 u32 present_state;
422 int ret;
423
424 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, present_state,
425 (present_state & ESDHC_CLOCK_GATE_OFF), 2, 100);
426 if (ret == -ETIMEDOUT)
427 dev_warn(mmc_dev(host->mmc), "%s: card clock still not gate off in 100us!.\n", __func__);
428 }
429
430 /* Enable the auto tuning circuit to check the CMD line and BUS line */
usdhc_auto_tuning_mode_sel(struct sdhci_host * host)431 static inline void usdhc_auto_tuning_mode_sel(struct sdhci_host *host)
432 {
433 u32 buswidth, auto_tune_buswidth;
434
435 buswidth = USDHC_GET_BUSWIDTH(readl(host->ioaddr + SDHCI_HOST_CONTROL));
436
437 switch (buswidth) {
438 case ESDHC_CTRL_8BITBUS:
439 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_8BIT_EN;
440 break;
441 case ESDHC_CTRL_4BITBUS:
442 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_4BIT_EN;
443 break;
444 default: /* 1BITBUS */
445 auto_tune_buswidth = ESDHC_VEND_SPEC2_AUTO_TUNE_1BIT_EN;
446 break;
447 }
448
449 esdhc_clrset_le(host, ESDHC_VEND_SPEC2_AUTO_TUNE_MODE_MASK,
450 auto_tune_buswidth | ESDHC_VEND_SPEC2_AUTO_TUNE_CMD_EN,
451 ESDHC_VEND_SPEC2);
452 }
453
esdhc_readl_le(struct sdhci_host * host,int reg)454 static u32 esdhc_readl_le(struct sdhci_host *host, int reg)
455 {
456 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
457 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
458 u32 val = readl(host->ioaddr + reg);
459
460 if (unlikely(reg == SDHCI_PRESENT_STATE)) {
461 u32 fsl_prss = val;
462 /* save the least 20 bits */
463 val = fsl_prss & 0x000FFFFF;
464 /* move dat[0-3] bits */
465 val |= (fsl_prss & 0x0F000000) >> 4;
466 /* move cmd line bit */
467 val |= (fsl_prss & 0x00800000) << 1;
468 }
469
470 if (unlikely(reg == SDHCI_CAPABILITIES)) {
471 /* ignore bit[0-15] as it stores cap_1 register val for mx6sl */
472 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
473 val &= 0xffff0000;
474
475 /* In FSL esdhc IC module, only bit20 is used to indicate the
476 * ADMA2 capability of esdhc, but this bit is messed up on
477 * some SOCs (e.g. on MX25, MX35 this bit is set, but they
478 * don't actually support ADMA2). So set the BROKEN_ADMA
479 * quirk on MX25/35 platforms.
480 */
481
482 if (val & SDHCI_CAN_DO_ADMA1) {
483 val &= ~SDHCI_CAN_DO_ADMA1;
484 val |= SDHCI_CAN_DO_ADMA2;
485 }
486 }
487
488 if (unlikely(reg == SDHCI_CAPABILITIES_1)) {
489 if (esdhc_is_usdhc(imx_data)) {
490 if (imx_data->socdata->flags & ESDHC_FLAG_HAVE_CAP1)
491 val = readl(host->ioaddr + SDHCI_CAPABILITIES) & 0xFFFF;
492 else
493 /* imx6q/dl does not have cap_1 register, fake one */
494 val = SDHCI_SUPPORT_DDR50 | SDHCI_SUPPORT_SDR104
495 | SDHCI_SUPPORT_SDR50
496 | SDHCI_USE_SDR50_TUNING
497 | FIELD_PREP(SDHCI_RETUNING_MODE_MASK,
498 SDHCI_TUNING_MODE_3);
499
500 /*
501 * Do not advertise faster UHS modes if there are no
502 * pinctrl states for 100MHz/200MHz.
503 */
504 if (IS_ERR_OR_NULL(imx_data->pins_100mhz))
505 val &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_DDR50);
506 if (IS_ERR_OR_NULL(imx_data->pins_200mhz))
507 val &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_HS400);
508 }
509 }
510
511 if (unlikely(reg == SDHCI_MAX_CURRENT) && esdhc_is_usdhc(imx_data)) {
512 val = 0;
513 val |= FIELD_PREP(SDHCI_MAX_CURRENT_330_MASK, 0xFF);
514 val |= FIELD_PREP(SDHCI_MAX_CURRENT_300_MASK, 0xFF);
515 val |= FIELD_PREP(SDHCI_MAX_CURRENT_180_MASK, 0xFF);
516 }
517
518 if (unlikely(reg == SDHCI_INT_STATUS)) {
519 if (val & ESDHC_INT_VENDOR_SPEC_DMA_ERR) {
520 val &= ~ESDHC_INT_VENDOR_SPEC_DMA_ERR;
521 val |= SDHCI_INT_ADMA_ERROR;
522 }
523
524 /*
525 * mask off the interrupt we get in response to the manually
526 * sent CMD12
527 */
528 if ((imx_data->multiblock_status == WAIT_FOR_INT) &&
529 ((val & SDHCI_INT_RESPONSE) == SDHCI_INT_RESPONSE)) {
530 val &= ~SDHCI_INT_RESPONSE;
531 writel(SDHCI_INT_RESPONSE, host->ioaddr +
532 SDHCI_INT_STATUS);
533 imx_data->multiblock_status = NO_CMD_PENDING;
534 }
535 }
536
537 return val;
538 }
539
esdhc_writel_le(struct sdhci_host * host,u32 val,int reg)540 static void esdhc_writel_le(struct sdhci_host *host, u32 val, int reg)
541 {
542 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
543 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
544 u32 data;
545
546 if (unlikely(reg == SDHCI_INT_ENABLE || reg == SDHCI_SIGNAL_ENABLE ||
547 reg == SDHCI_INT_STATUS)) {
548 if ((val & SDHCI_INT_CARD_INT) && !esdhc_is_usdhc(imx_data)) {
549 /*
550 * Clear and then set D3CD bit to avoid missing the
551 * card interrupt. This is an eSDHC controller problem
552 * so we need to apply the following workaround: clear
553 * and set D3CD bit will make eSDHC re-sample the card
554 * interrupt. In case a card interrupt was lost,
555 * re-sample it by the following steps.
556 */
557 data = readl(host->ioaddr + SDHCI_HOST_CONTROL);
558 data &= ~ESDHC_CTRL_D3CD;
559 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
560 data |= ESDHC_CTRL_D3CD;
561 writel(data, host->ioaddr + SDHCI_HOST_CONTROL);
562 }
563
564 if (val & SDHCI_INT_ADMA_ERROR) {
565 val &= ~SDHCI_INT_ADMA_ERROR;
566 val |= ESDHC_INT_VENDOR_SPEC_DMA_ERR;
567 }
568 }
569
570 if (unlikely((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
571 && (reg == SDHCI_INT_STATUS)
572 && (val & SDHCI_INT_DATA_END))) {
573 u32 v;
574 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
575 v &= ~ESDHC_VENDOR_SPEC_SDIO_QUIRK;
576 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
577
578 if (imx_data->multiblock_status == MULTIBLK_IN_PROCESS)
579 {
580 /* send a manual CMD12 with RESPTYP=none */
581 data = MMC_STOP_TRANSMISSION << 24 |
582 SDHCI_CMD_ABORTCMD << 16;
583 writel(data, host->ioaddr + SDHCI_TRANSFER_MODE);
584 imx_data->multiblock_status = WAIT_FOR_INT;
585 }
586 }
587
588 writel(val, host->ioaddr + reg);
589 }
590
esdhc_readw_le(struct sdhci_host * host,int reg)591 static u16 esdhc_readw_le(struct sdhci_host *host, int reg)
592 {
593 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
594 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
595 u16 ret = 0;
596 u32 val;
597
598 if (unlikely(reg == SDHCI_HOST_VERSION)) {
599 reg ^= 2;
600 if (esdhc_is_usdhc(imx_data)) {
601 /*
602 * The usdhc register returns a wrong host version.
603 * Correct it here.
604 */
605 return SDHCI_SPEC_300;
606 }
607 }
608
609 if (unlikely(reg == SDHCI_HOST_CONTROL2)) {
610 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
611 if (val & ESDHC_VENDOR_SPEC_VSELECT)
612 ret |= SDHCI_CTRL_VDD_180;
613
614 if (esdhc_is_usdhc(imx_data)) {
615 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
616 val = readl(host->ioaddr + ESDHC_MIX_CTRL);
617 else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING)
618 /* the std tuning bits is in ACMD12_ERR for imx6sl */
619 val = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
620 }
621
622 if (val & ESDHC_MIX_CTRL_EXE_TUNE)
623 ret |= SDHCI_CTRL_EXEC_TUNING;
624 if (val & ESDHC_MIX_CTRL_SMPCLK_SEL)
625 ret |= SDHCI_CTRL_TUNED_CLK;
626
627 ret &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
628
629 return ret;
630 }
631
632 if (unlikely(reg == SDHCI_TRANSFER_MODE)) {
633 if (esdhc_is_usdhc(imx_data)) {
634 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
635 ret = m & ESDHC_MIX_CTRL_SDHCI_MASK;
636 /* Swap AC23 bit */
637 if (m & ESDHC_MIX_CTRL_AC23EN) {
638 ret &= ~ESDHC_MIX_CTRL_AC23EN;
639 ret |= SDHCI_TRNS_AUTO_CMD23;
640 }
641 } else {
642 ret = readw(host->ioaddr + SDHCI_TRANSFER_MODE);
643 }
644
645 return ret;
646 }
647
648 return readw(host->ioaddr + reg);
649 }
650
esdhc_writew_le(struct sdhci_host * host,u16 val,int reg)651 static void esdhc_writew_le(struct sdhci_host *host, u16 val, int reg)
652 {
653 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
654 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
655 u32 new_val = 0;
656
657 switch (reg) {
658 case SDHCI_CLOCK_CONTROL:
659 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
660 if (val & SDHCI_CLOCK_CARD_EN)
661 new_val |= ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
662 else
663 new_val &= ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON;
664 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
665 if (!(new_val & ESDHC_VENDOR_SPEC_FRC_SDCLK_ON))
666 esdhc_wait_for_card_clock_gate_off(host);
667 return;
668 case SDHCI_HOST_CONTROL2:
669 new_val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
670 if (val & SDHCI_CTRL_VDD_180)
671 new_val |= ESDHC_VENDOR_SPEC_VSELECT;
672 else
673 new_val &= ~ESDHC_VENDOR_SPEC_VSELECT;
674 writel(new_val, host->ioaddr + ESDHC_VENDOR_SPEC);
675 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
676 u32 v = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
677 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
678 if (val & SDHCI_CTRL_TUNED_CLK) {
679 v |= ESDHC_MIX_CTRL_SMPCLK_SEL;
680 } else {
681 v &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
682 m &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
683 m &= ~ESDHC_MIX_CTRL_AUTO_TUNE_EN;
684 }
685
686 if (val & SDHCI_CTRL_EXEC_TUNING) {
687 v |= ESDHC_MIX_CTRL_EXE_TUNE;
688 m |= ESDHC_MIX_CTRL_FBCLK_SEL;
689 m |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
690 usdhc_auto_tuning_mode_sel(host);
691 } else {
692 v &= ~ESDHC_MIX_CTRL_EXE_TUNE;
693 }
694
695 writel(v, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
696 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
697 }
698 return;
699 case SDHCI_TRANSFER_MODE:
700 if ((imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT)
701 && (host->cmd->opcode == SD_IO_RW_EXTENDED)
702 && (host->cmd->data->blocks > 1)
703 && (host->cmd->data->flags & MMC_DATA_READ)) {
704 u32 v;
705 v = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
706 v |= ESDHC_VENDOR_SPEC_SDIO_QUIRK;
707 writel(v, host->ioaddr + ESDHC_VENDOR_SPEC);
708 }
709
710 if (esdhc_is_usdhc(imx_data)) {
711 u32 wml;
712 u32 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
713 /* Swap AC23 bit */
714 if (val & SDHCI_TRNS_AUTO_CMD23) {
715 val &= ~SDHCI_TRNS_AUTO_CMD23;
716 val |= ESDHC_MIX_CTRL_AC23EN;
717 }
718 m = val | (m & ~ESDHC_MIX_CTRL_SDHCI_MASK);
719 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
720
721 /* Set watermark levels for PIO access to maximum value
722 * (128 words) to accommodate full 512 bytes buffer.
723 * For DMA access restore the levels to default value.
724 */
725 m = readl(host->ioaddr + ESDHC_WTMK_LVL);
726 if (val & SDHCI_TRNS_DMA) {
727 wml = ESDHC_WTMK_LVL_WML_VAL_DEF;
728 } else {
729 u8 ctrl;
730 wml = ESDHC_WTMK_LVL_WML_VAL_MAX;
731
732 /*
733 * Since already disable DMA mode, so also need
734 * to clear the DMASEL. Otherwise, for standard
735 * tuning, when send tuning command, usdhc will
736 * still prefetch the ADMA script from wrong
737 * DMA address, then we will see IOMMU report
738 * some error which show lack of TLB mapping.
739 */
740 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
741 ctrl &= ~SDHCI_CTRL_DMA_MASK;
742 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
743 }
744 m &= ~(ESDHC_WTMK_LVL_RD_WML_MASK |
745 ESDHC_WTMK_LVL_WR_WML_MASK);
746 m |= (wml << ESDHC_WTMK_LVL_RD_WML_SHIFT) |
747 (wml << ESDHC_WTMK_LVL_WR_WML_SHIFT);
748 writel(m, host->ioaddr + ESDHC_WTMK_LVL);
749 } else {
750 /*
751 * Postpone this write, we must do it together with a
752 * command write that is down below.
753 */
754 imx_data->scratchpad = val;
755 }
756 return;
757 case SDHCI_COMMAND:
758 if (host->cmd->opcode == MMC_STOP_TRANSMISSION)
759 val |= SDHCI_CMD_ABORTCMD;
760
761 if ((host->cmd->opcode == MMC_SET_BLOCK_COUNT) &&
762 (imx_data->socdata->flags & ESDHC_FLAG_MULTIBLK_NO_INT))
763 imx_data->multiblock_status = MULTIBLK_IN_PROCESS;
764
765 if (esdhc_is_usdhc(imx_data))
766 writel(val << 16,
767 host->ioaddr + SDHCI_TRANSFER_MODE);
768 else
769 writel(val << 16 | imx_data->scratchpad,
770 host->ioaddr + SDHCI_TRANSFER_MODE);
771 return;
772 case SDHCI_BLOCK_SIZE:
773 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
774 break;
775 }
776 esdhc_clrset_le(host, 0xffff, val, reg);
777 }
778
esdhc_readb_le(struct sdhci_host * host,int reg)779 static u8 esdhc_readb_le(struct sdhci_host *host, int reg)
780 {
781 u8 ret;
782 u32 val;
783
784 switch (reg) {
785 case SDHCI_HOST_CONTROL:
786 val = readl(host->ioaddr + reg);
787
788 ret = val & SDHCI_CTRL_LED;
789 ret |= (val >> 5) & SDHCI_CTRL_DMA_MASK;
790 ret |= (val & ESDHC_CTRL_4BITBUS);
791 ret |= (val & ESDHC_CTRL_8BITBUS) << 3;
792 return ret;
793 }
794
795 return readb(host->ioaddr + reg);
796 }
797
esdhc_writeb_le(struct sdhci_host * host,u8 val,int reg)798 static void esdhc_writeb_le(struct sdhci_host *host, u8 val, int reg)
799 {
800 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
801 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
802 u32 new_val = 0;
803 u32 mask;
804
805 switch (reg) {
806 case SDHCI_POWER_CONTROL:
807 /*
808 * FSL put some DMA bits here
809 * If your board has a regulator, code should be here
810 */
811 return;
812 case SDHCI_HOST_CONTROL:
813 /* FSL messed up here, so we need to manually compose it. */
814 new_val = val & SDHCI_CTRL_LED;
815 /* ensure the endianness */
816 new_val |= ESDHC_HOST_CONTROL_LE;
817 /* bits 8&9 are reserved on mx25 */
818 if (!is_imx25_esdhc(imx_data)) {
819 /* DMA mode bits are shifted */
820 new_val |= (val & SDHCI_CTRL_DMA_MASK) << 5;
821 }
822
823 /*
824 * Do not touch buswidth bits here. This is done in
825 * esdhc_pltfm_bus_width.
826 * Do not touch the D3CD bit either which is used for the
827 * SDIO interrupt erratum workaround.
828 */
829 mask = 0xffff & ~(ESDHC_CTRL_BUSWIDTH_MASK | ESDHC_CTRL_D3CD);
830
831 esdhc_clrset_le(host, mask, new_val, reg);
832 return;
833 case SDHCI_SOFTWARE_RESET:
834 if (val & SDHCI_RESET_DATA)
835 new_val = readl(host->ioaddr + SDHCI_HOST_CONTROL);
836 break;
837 }
838 esdhc_clrset_le(host, 0xff, val, reg);
839
840 if (reg == SDHCI_SOFTWARE_RESET) {
841 if (val & SDHCI_RESET_ALL) {
842 /*
843 * The esdhc has a design violation to SDHC spec which
844 * tells that software reset should not affect card
845 * detection circuit. But esdhc clears its SYSCTL
846 * register bits [0..2] during the software reset. This
847 * will stop those clocks that card detection circuit
848 * relies on. To work around it, we turn the clocks on
849 * back to keep card detection circuit functional.
850 */
851 esdhc_clrset_le(host, 0x7, 0x7, ESDHC_SYSTEM_CONTROL);
852 /*
853 * The reset on usdhc fails to clear MIX_CTRL register.
854 * Do it manually here.
855 */
856 if (esdhc_is_usdhc(imx_data)) {
857 /*
858 * the tuning bits should be kept during reset
859 */
860 new_val = readl(host->ioaddr + ESDHC_MIX_CTRL);
861 writel(new_val & ESDHC_MIX_CTRL_TUNING_MASK,
862 host->ioaddr + ESDHC_MIX_CTRL);
863 imx_data->is_ddr = 0;
864 }
865 } else if (val & SDHCI_RESET_DATA) {
866 /*
867 * The eSDHC DAT line software reset clears at least the
868 * data transfer width on i.MX25, so make sure that the
869 * Host Control register is unaffected.
870 */
871 esdhc_clrset_le(host, 0xff, new_val,
872 SDHCI_HOST_CONTROL);
873 }
874 }
875 }
876
esdhc_pltfm_get_max_clock(struct sdhci_host * host)877 static unsigned int esdhc_pltfm_get_max_clock(struct sdhci_host *host)
878 {
879 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
880
881 return pltfm_host->clock;
882 }
883
esdhc_pltfm_get_min_clock(struct sdhci_host * host)884 static unsigned int esdhc_pltfm_get_min_clock(struct sdhci_host *host)
885 {
886 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
887
888 return pltfm_host->clock / 256 / 16;
889 }
890
esdhc_pltfm_set_clock(struct sdhci_host * host,unsigned int clock)891 static inline void esdhc_pltfm_set_clock(struct sdhci_host *host,
892 unsigned int clock)
893 {
894 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
895 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
896 unsigned int host_clock = pltfm_host->clock;
897 int ddr_pre_div = imx_data->is_ddr ? 2 : 1;
898 int pre_div = 1;
899 int div = 1;
900 int ret;
901 u32 temp, val;
902
903 if (esdhc_is_usdhc(imx_data)) {
904 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
905 writel(val & ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
906 host->ioaddr + ESDHC_VENDOR_SPEC);
907 esdhc_wait_for_card_clock_gate_off(host);
908 }
909
910 if (clock == 0) {
911 host->mmc->actual_clock = 0;
912 return;
913 }
914
915 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
916 if (is_imx53_esdhc(imx_data)) {
917 /*
918 * According to the i.MX53 reference manual, if DLLCTRL[10] can
919 * be set, then the controller is eSDHCv3, else it is eSDHCv2.
920 */
921 val = readl(host->ioaddr + ESDHC_DLL_CTRL);
922 writel(val | BIT(10), host->ioaddr + ESDHC_DLL_CTRL);
923 temp = readl(host->ioaddr + ESDHC_DLL_CTRL);
924 writel(val, host->ioaddr + ESDHC_DLL_CTRL);
925 if (temp & BIT(10))
926 pre_div = 2;
927 }
928
929 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
930 temp &= ~(ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
931 | ESDHC_CLOCK_MASK);
932 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
933
934 if (imx_data->socdata->flags & ESDHC_FLAG_ERR010450) {
935 unsigned int max_clock;
936
937 max_clock = imx_data->is_ddr ? 45000000 : 150000000;
938
939 clock = min(clock, max_clock);
940 }
941
942 while (host_clock / (16 * pre_div * ddr_pre_div) > clock &&
943 pre_div < 256)
944 pre_div *= 2;
945
946 while (host_clock / (div * pre_div * ddr_pre_div) > clock && div < 16)
947 div++;
948
949 host->mmc->actual_clock = host_clock / (div * pre_div * ddr_pre_div);
950 dev_dbg(mmc_dev(host->mmc), "desired SD clock: %d, actual: %d\n",
951 clock, host->mmc->actual_clock);
952
953 pre_div >>= 1;
954 div--;
955
956 temp = sdhci_readl(host, ESDHC_SYSTEM_CONTROL);
957 temp |= (ESDHC_CLOCK_IPGEN | ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN
958 | (div << ESDHC_DIVIDER_SHIFT)
959 | (pre_div << ESDHC_PREDIV_SHIFT));
960 sdhci_writel(host, temp, ESDHC_SYSTEM_CONTROL);
961
962 /* need to wait the bit 3 of the PRSSTAT to be set, make sure card clock is stable */
963 ret = readl_poll_timeout(host->ioaddr + ESDHC_PRSSTAT, temp,
964 (temp & ESDHC_CLOCK_STABLE), 2, 100);
965 if (ret == -ETIMEDOUT)
966 dev_warn(mmc_dev(host->mmc), "card clock still not stable in 100us!.\n");
967
968 if (esdhc_is_usdhc(imx_data)) {
969 val = readl(host->ioaddr + ESDHC_VENDOR_SPEC);
970 writel(val | ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
971 host->ioaddr + ESDHC_VENDOR_SPEC);
972 }
973
974 }
975
esdhc_pltfm_get_ro(struct sdhci_host * host)976 static unsigned int esdhc_pltfm_get_ro(struct sdhci_host *host)
977 {
978 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
979 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
980 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
981
982 switch (boarddata->wp_type) {
983 case ESDHC_WP_GPIO:
984 return mmc_gpio_get_ro(host->mmc);
985 case ESDHC_WP_CONTROLLER:
986 return !(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
987 SDHCI_WRITE_PROTECT);
988 case ESDHC_WP_NONE:
989 break;
990 }
991
992 return -ENOSYS;
993 }
994
esdhc_pltfm_set_bus_width(struct sdhci_host * host,int width)995 static void esdhc_pltfm_set_bus_width(struct sdhci_host *host, int width)
996 {
997 u32 ctrl;
998
999 switch (width) {
1000 case MMC_BUS_WIDTH_8:
1001 ctrl = ESDHC_CTRL_8BITBUS;
1002 break;
1003 case MMC_BUS_WIDTH_4:
1004 ctrl = ESDHC_CTRL_4BITBUS;
1005 break;
1006 default:
1007 ctrl = 0;
1008 break;
1009 }
1010
1011 esdhc_clrset_le(host, ESDHC_CTRL_BUSWIDTH_MASK, ctrl,
1012 SDHCI_HOST_CONTROL);
1013 }
1014
usdhc_execute_tuning(struct mmc_host * mmc,u32 opcode)1015 static int usdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1016 {
1017 struct sdhci_host *host = mmc_priv(mmc);
1018
1019 /*
1020 * i.MX uSDHC internally already uses a fixed optimized timing for
1021 * DDR50, normally does not require tuning for DDR50 mode.
1022 */
1023 if (host->timing == MMC_TIMING_UHS_DDR50)
1024 return 0;
1025
1026 return sdhci_execute_tuning(mmc, opcode);
1027 }
1028
esdhc_prepare_tuning(struct sdhci_host * host,u32 val)1029 static void esdhc_prepare_tuning(struct sdhci_host *host, u32 val)
1030 {
1031 u32 reg;
1032 u8 sw_rst;
1033 int ret;
1034
1035 /* FIXME: delay a bit for card to be ready for next tuning due to errors */
1036 mdelay(1);
1037
1038 /* IC suggest to reset USDHC before every tuning command */
1039 esdhc_clrset_le(host, 0xff, SDHCI_RESET_ALL, SDHCI_SOFTWARE_RESET);
1040 ret = readb_poll_timeout(host->ioaddr + SDHCI_SOFTWARE_RESET, sw_rst,
1041 !(sw_rst & SDHCI_RESET_ALL), 10, 100);
1042 if (ret == -ETIMEDOUT)
1043 dev_warn(mmc_dev(host->mmc),
1044 "warning! RESET_ALL never complete before sending tuning command\n");
1045
1046 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1047 reg |= ESDHC_MIX_CTRL_EXE_TUNE | ESDHC_MIX_CTRL_SMPCLK_SEL |
1048 ESDHC_MIX_CTRL_FBCLK_SEL;
1049 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1050 writel(val << 8, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1051 dev_dbg(mmc_dev(host->mmc),
1052 "tuning with delay 0x%x ESDHC_TUNE_CTRL_STATUS 0x%x\n",
1053 val, readl(host->ioaddr + ESDHC_TUNE_CTRL_STATUS));
1054 }
1055
esdhc_post_tuning(struct sdhci_host * host)1056 static void esdhc_post_tuning(struct sdhci_host *host)
1057 {
1058 u32 reg;
1059
1060 usdhc_auto_tuning_mode_sel(host);
1061
1062 reg = readl(host->ioaddr + ESDHC_MIX_CTRL);
1063 reg &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1064 reg |= ESDHC_MIX_CTRL_AUTO_TUNE_EN;
1065 writel(reg, host->ioaddr + ESDHC_MIX_CTRL);
1066 }
1067
esdhc_executing_tuning(struct sdhci_host * host,u32 opcode)1068 static int esdhc_executing_tuning(struct sdhci_host *host, u32 opcode)
1069 {
1070 int min, max, avg, ret;
1071
1072 /* find the mininum delay first which can pass tuning */
1073 min = ESDHC_TUNE_CTRL_MIN;
1074 while (min < ESDHC_TUNE_CTRL_MAX) {
1075 esdhc_prepare_tuning(host, min);
1076 if (!mmc_send_tuning(host->mmc, opcode, NULL))
1077 break;
1078 min += ESDHC_TUNE_CTRL_STEP;
1079 }
1080
1081 /* find the maxinum delay which can not pass tuning */
1082 max = min + ESDHC_TUNE_CTRL_STEP;
1083 while (max < ESDHC_TUNE_CTRL_MAX) {
1084 esdhc_prepare_tuning(host, max);
1085 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
1086 max -= ESDHC_TUNE_CTRL_STEP;
1087 break;
1088 }
1089 max += ESDHC_TUNE_CTRL_STEP;
1090 }
1091
1092 /* use average delay to get the best timing */
1093 avg = (min + max) / 2;
1094 esdhc_prepare_tuning(host, avg);
1095 ret = mmc_send_tuning(host->mmc, opcode, NULL);
1096 esdhc_post_tuning(host);
1097
1098 dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1099 ret ? "failed" : "passed", avg, ret);
1100
1101 return ret;
1102 }
1103
esdhc_hs400_enhanced_strobe(struct mmc_host * mmc,struct mmc_ios * ios)1104 static void esdhc_hs400_enhanced_strobe(struct mmc_host *mmc, struct mmc_ios *ios)
1105 {
1106 struct sdhci_host *host = mmc_priv(mmc);
1107 u32 m;
1108
1109 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1110 if (ios->enhanced_strobe)
1111 m |= ESDHC_MIX_CTRL_HS400_ES_EN;
1112 else
1113 m &= ~ESDHC_MIX_CTRL_HS400_ES_EN;
1114 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1115 }
1116
esdhc_change_pinstate(struct sdhci_host * host,unsigned int uhs)1117 static int esdhc_change_pinstate(struct sdhci_host *host,
1118 unsigned int uhs)
1119 {
1120 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1121 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1122 struct pinctrl_state *pinctrl;
1123
1124 dev_dbg(mmc_dev(host->mmc), "change pinctrl state for uhs %d\n", uhs);
1125
1126 if (IS_ERR(imx_data->pinctrl) ||
1127 IS_ERR(imx_data->pins_100mhz) ||
1128 IS_ERR(imx_data->pins_200mhz))
1129 return -EINVAL;
1130
1131 switch (uhs) {
1132 case MMC_TIMING_UHS_SDR50:
1133 case MMC_TIMING_UHS_DDR50:
1134 pinctrl = imx_data->pins_100mhz;
1135 break;
1136 case MMC_TIMING_UHS_SDR104:
1137 case MMC_TIMING_MMC_HS200:
1138 case MMC_TIMING_MMC_HS400:
1139 pinctrl = imx_data->pins_200mhz;
1140 break;
1141 default:
1142 /* back to default state for other legacy timing */
1143 return pinctrl_select_default_state(mmc_dev(host->mmc));
1144 }
1145
1146 return pinctrl_select_state(imx_data->pinctrl, pinctrl);
1147 }
1148
1149 /*
1150 * For HS400 eMMC, there is a data_strobe line. This signal is generated
1151 * by the device and used for data output and CRC status response output
1152 * in HS400 mode. The frequency of this signal follows the frequency of
1153 * CLK generated by host. The host receives the data which is aligned to the
1154 * edge of data_strobe line. Due to the time delay between CLK line and
1155 * data_strobe line, if the delay time is larger than one clock cycle,
1156 * then CLK and data_strobe line will be misaligned, read error shows up.
1157 */
esdhc_set_strobe_dll(struct sdhci_host * host)1158 static void esdhc_set_strobe_dll(struct sdhci_host *host)
1159 {
1160 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1161 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1162 u32 strobe_delay;
1163 u32 v;
1164 int ret;
1165
1166 /* disable clock before enabling strobe dll */
1167 writel(readl(host->ioaddr + ESDHC_VENDOR_SPEC) &
1168 ~ESDHC_VENDOR_SPEC_FRC_SDCLK_ON,
1169 host->ioaddr + ESDHC_VENDOR_SPEC);
1170 esdhc_wait_for_card_clock_gate_off(host);
1171
1172 /* force a reset on strobe dll */
1173 writel(ESDHC_STROBE_DLL_CTRL_RESET,
1174 host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1175 /* clear the reset bit on strobe dll before any setting */
1176 writel(0, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1177
1178 /*
1179 * enable strobe dll ctrl and adjust the delay target
1180 * for the uSDHC loopback read clock
1181 */
1182 if (imx_data->boarddata.strobe_dll_delay_target)
1183 strobe_delay = imx_data->boarddata.strobe_dll_delay_target;
1184 else
1185 strobe_delay = ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT;
1186 v = ESDHC_STROBE_DLL_CTRL_ENABLE |
1187 ESDHC_STROBE_DLL_CTRL_SLV_UPDATE_INT_DEFAULT |
1188 (strobe_delay << ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
1189 writel(v, host->ioaddr + ESDHC_STROBE_DLL_CTRL);
1190
1191 /* wait max 50us to get the REF/SLV lock */
1192 ret = readl_poll_timeout(host->ioaddr + ESDHC_STROBE_DLL_STATUS, v,
1193 ((v & ESDHC_STROBE_DLL_STS_REF_LOCK) && (v & ESDHC_STROBE_DLL_STS_SLV_LOCK)), 1, 50);
1194 if (ret == -ETIMEDOUT)
1195 dev_warn(mmc_dev(host->mmc),
1196 "warning! HS400 strobe DLL status REF/SLV not lock in 50us, STROBE DLL status is %x!\n", v);
1197 }
1198
esdhc_reset_tuning(struct sdhci_host * host)1199 static void esdhc_reset_tuning(struct sdhci_host *host)
1200 {
1201 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1202 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1203 u32 ctrl;
1204 int ret;
1205
1206 /* Reset the tuning circuit */
1207 if (esdhc_is_usdhc(imx_data)) {
1208 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1209 ctrl = readl(host->ioaddr + ESDHC_MIX_CTRL);
1210 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1211 ctrl &= ~ESDHC_MIX_CTRL_FBCLK_SEL;
1212 writel(ctrl, host->ioaddr + ESDHC_MIX_CTRL);
1213 writel(0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1214 } else if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1215 ctrl = readl(host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1216 ctrl &= ~ESDHC_MIX_CTRL_SMPCLK_SEL;
1217 ctrl &= ~ESDHC_MIX_CTRL_EXE_TUNE;
1218 writel(ctrl, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1219 /* Make sure ESDHC_MIX_CTRL_EXE_TUNE cleared */
1220 ret = readl_poll_timeout(host->ioaddr + SDHCI_AUTO_CMD_STATUS,
1221 ctrl, !(ctrl & ESDHC_MIX_CTRL_EXE_TUNE), 1, 50);
1222 if (ret == -ETIMEDOUT)
1223 dev_warn(mmc_dev(host->mmc),
1224 "Warning! clear execute tuning bit failed\n");
1225 /*
1226 * SDHCI_INT_DATA_AVAIL is W1C bit, set this bit will clear the
1227 * usdhc IP internal logic flag execute_tuning_with_clr_buf, which
1228 * will finally make sure the normal data transfer logic correct.
1229 */
1230 ctrl = readl(host->ioaddr + SDHCI_INT_STATUS);
1231 ctrl |= SDHCI_INT_DATA_AVAIL;
1232 writel(ctrl, host->ioaddr + SDHCI_INT_STATUS);
1233 }
1234 }
1235 }
1236
esdhc_set_uhs_signaling(struct sdhci_host * host,unsigned timing)1237 static void esdhc_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1238 {
1239 u32 m;
1240 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1241 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1242 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1243
1244 /* disable ddr mode and disable HS400 mode */
1245 m = readl(host->ioaddr + ESDHC_MIX_CTRL);
1246 m &= ~(ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN);
1247 imx_data->is_ddr = 0;
1248
1249 switch (timing) {
1250 case MMC_TIMING_UHS_SDR12:
1251 case MMC_TIMING_UHS_SDR25:
1252 case MMC_TIMING_UHS_SDR50:
1253 case MMC_TIMING_UHS_SDR104:
1254 case MMC_TIMING_MMC_HS:
1255 case MMC_TIMING_MMC_HS200:
1256 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1257 break;
1258 case MMC_TIMING_UHS_DDR50:
1259 case MMC_TIMING_MMC_DDR52:
1260 m |= ESDHC_MIX_CTRL_DDREN;
1261 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1262 imx_data->is_ddr = 1;
1263 if (boarddata->delay_line) {
1264 u32 v;
1265 v = boarddata->delay_line <<
1266 ESDHC_DLL_OVERRIDE_VAL_SHIFT |
1267 (1 << ESDHC_DLL_OVERRIDE_EN_SHIFT);
1268 if (is_imx53_esdhc(imx_data))
1269 v <<= 1;
1270 writel(v, host->ioaddr + ESDHC_DLL_CTRL);
1271 }
1272 break;
1273 case MMC_TIMING_MMC_HS400:
1274 m |= ESDHC_MIX_CTRL_DDREN | ESDHC_MIX_CTRL_HS400_EN;
1275 writel(m, host->ioaddr + ESDHC_MIX_CTRL);
1276 imx_data->is_ddr = 1;
1277 /* update clock after enable DDR for strobe DLL lock */
1278 host->ops->set_clock(host, host->clock);
1279 esdhc_set_strobe_dll(host);
1280 break;
1281 case MMC_TIMING_LEGACY:
1282 default:
1283 esdhc_reset_tuning(host);
1284 break;
1285 }
1286
1287 esdhc_change_pinstate(host, timing);
1288 }
1289
esdhc_reset(struct sdhci_host * host,u8 mask)1290 static void esdhc_reset(struct sdhci_host *host, u8 mask)
1291 {
1292 sdhci_and_cqhci_reset(host, mask);
1293
1294 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1295 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1296 }
1297
esdhc_get_max_timeout_count(struct sdhci_host * host)1298 static unsigned int esdhc_get_max_timeout_count(struct sdhci_host *host)
1299 {
1300 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1301 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1302
1303 /* Doc Erratum: the uSDHC actual maximum timeout count is 1 << 29 */
1304 return esdhc_is_usdhc(imx_data) ? 1 << 29 : 1 << 27;
1305 }
1306
esdhc_set_timeout(struct sdhci_host * host,struct mmc_command * cmd)1307 static void esdhc_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
1308 {
1309 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1310 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1311
1312 /* use maximum timeout counter */
1313 esdhc_clrset_le(host, ESDHC_SYS_CTRL_DTOCV_MASK,
1314 esdhc_is_usdhc(imx_data) ? 0xF : 0xE,
1315 SDHCI_TIMEOUT_CONTROL);
1316 }
1317
esdhc_cqhci_irq(struct sdhci_host * host,u32 intmask)1318 static u32 esdhc_cqhci_irq(struct sdhci_host *host, u32 intmask)
1319 {
1320 int cmd_error = 0;
1321 int data_error = 0;
1322
1323 if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
1324 return intmask;
1325
1326 cqhci_irq(host->mmc, intmask, cmd_error, data_error);
1327
1328 return 0;
1329 }
1330
1331 static struct sdhci_ops sdhci_esdhc_ops = {
1332 .read_l = esdhc_readl_le,
1333 .read_w = esdhc_readw_le,
1334 .read_b = esdhc_readb_le,
1335 .write_l = esdhc_writel_le,
1336 .write_w = esdhc_writew_le,
1337 .write_b = esdhc_writeb_le,
1338 .set_clock = esdhc_pltfm_set_clock,
1339 .get_max_clock = esdhc_pltfm_get_max_clock,
1340 .get_min_clock = esdhc_pltfm_get_min_clock,
1341 .get_max_timeout_count = esdhc_get_max_timeout_count,
1342 .get_ro = esdhc_pltfm_get_ro,
1343 .set_timeout = esdhc_set_timeout,
1344 .set_bus_width = esdhc_pltfm_set_bus_width,
1345 .set_uhs_signaling = esdhc_set_uhs_signaling,
1346 .reset = esdhc_reset,
1347 .irq = esdhc_cqhci_irq,
1348 .dump_vendor_regs = esdhc_dump_debug_regs,
1349 };
1350
1351 static const struct sdhci_pltfm_data sdhci_esdhc_imx_pdata = {
1352 .quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_NO_HISPD_BIT
1353 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC
1354 | SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC
1355 | SDHCI_QUIRK_BROKEN_CARD_DETECTION,
1356 .ops = &sdhci_esdhc_ops,
1357 };
1358
sdhci_esdhc_imx_hwinit(struct sdhci_host * host)1359 static void sdhci_esdhc_imx_hwinit(struct sdhci_host *host)
1360 {
1361 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1362 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1363 struct cqhci_host *cq_host = host->mmc->cqe_private;
1364 int tmp;
1365
1366 if (esdhc_is_usdhc(imx_data)) {
1367 /*
1368 * The imx6q ROM code will change the default watermark
1369 * level setting to something insane. Change it back here.
1370 */
1371 writel(ESDHC_WTMK_DEFAULT_VAL, host->ioaddr + ESDHC_WTMK_LVL);
1372
1373 /*
1374 * ROM code will change the bit burst_length_enable setting
1375 * to zero if this usdhc is chosen to boot system. Change
1376 * it back here, otherwise it will impact the performance a
1377 * lot. This bit is used to enable/disable the burst length
1378 * for the external AHB2AXI bridge. It's useful especially
1379 * for INCR transfer because without burst length indicator,
1380 * the AHB2AXI bridge does not know the burst length in
1381 * advance. And without burst length indicator, AHB INCR
1382 * transfer can only be converted to singles on the AXI side.
1383 */
1384 writel(readl(host->ioaddr + SDHCI_HOST_CONTROL)
1385 | ESDHC_BURST_LEN_EN_INCR,
1386 host->ioaddr + SDHCI_HOST_CONTROL);
1387
1388 /*
1389 * erratum ESDHC_FLAG_ERR004536 fix for MX6Q TO1.2 and MX6DL
1390 * TO1.1, it's harmless for MX6SL
1391 */
1392 if (!(imx_data->socdata->flags & ESDHC_FLAG_SKIP_ERR004536)) {
1393 writel(readl(host->ioaddr + 0x6c) & ~BIT(7),
1394 host->ioaddr + 0x6c);
1395 }
1396
1397 /* disable DLL_CTRL delay line settings */
1398 writel(0x0, host->ioaddr + ESDHC_DLL_CTRL);
1399
1400 /*
1401 * For the case of command with busy, if set the bit
1402 * ESDHC_VEND_SPEC2_EN_BUSY_IRQ, USDHC will generate a
1403 * transfer complete interrupt when busy is deasserted.
1404 * When CQHCI use DCMD to send a CMD need R1b respons,
1405 * CQHCI require to set ESDHC_VEND_SPEC2_EN_BUSY_IRQ,
1406 * otherwise DCMD will always meet timeout waiting for
1407 * hardware interrupt issue.
1408 */
1409 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1410 tmp = readl(host->ioaddr + ESDHC_VEND_SPEC2);
1411 tmp |= ESDHC_VEND_SPEC2_EN_BUSY_IRQ;
1412 writel(tmp, host->ioaddr + ESDHC_VEND_SPEC2);
1413
1414 host->quirks &= ~SDHCI_QUIRK_NO_BUSY_IRQ;
1415 }
1416
1417 if (imx_data->socdata->flags & ESDHC_FLAG_STD_TUNING) {
1418 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1419 tmp |= ESDHC_STD_TUNING_EN |
1420 ESDHC_TUNING_START_TAP_DEFAULT;
1421 if (imx_data->boarddata.tuning_start_tap) {
1422 tmp &= ~ESDHC_TUNING_START_TAP_MASK;
1423 tmp |= imx_data->boarddata.tuning_start_tap;
1424 }
1425
1426 if (imx_data->boarddata.tuning_step) {
1427 tmp &= ~ESDHC_TUNING_STEP_MASK;
1428 tmp |= imx_data->boarddata.tuning_step
1429 << ESDHC_TUNING_STEP_SHIFT;
1430 }
1431
1432 /* Disable the CMD CRC check for tuning, if not, need to
1433 * add some delay after every tuning command, because
1434 * hardware standard tuning logic will directly go to next
1435 * step once it detect the CMD CRC error, will not wait for
1436 * the card side to finally send out the tuning data, trigger
1437 * the buffer read ready interrupt immediately. If usdhc send
1438 * the next tuning command some eMMC card will stuck, can't
1439 * response, block the tuning procedure or the first command
1440 * after the whole tuning procedure always can't get any response.
1441 */
1442 tmp |= ESDHC_TUNING_CMD_CRC_CHECK_DISABLE;
1443 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1444 } else if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING) {
1445 /*
1446 * ESDHC_STD_TUNING_EN may be configed in bootloader
1447 * or ROM code, so clear this bit here to make sure
1448 * the manual tuning can work.
1449 */
1450 tmp = readl(host->ioaddr + ESDHC_TUNING_CTRL);
1451 tmp &= ~ESDHC_STD_TUNING_EN;
1452 writel(tmp, host->ioaddr + ESDHC_TUNING_CTRL);
1453 }
1454
1455 /*
1456 * On i.MX8MM, we are running Dual Linux OS, with 1st Linux using SD Card
1457 * as rootfs storage, 2nd Linux using eMMC as rootfs storage. We let the
1458 * the 1st linux configure power/clock for the 2nd Linux.
1459 *
1460 * When the 2nd Linux is booting into rootfs stage, we let the 1st Linux
1461 * to destroy the 2nd linux, then restart the 2nd linux, we met SDHCI dump.
1462 * After we clear the pending interrupt and halt CQCTL, issue gone.
1463 */
1464 if (cq_host) {
1465 tmp = cqhci_readl(cq_host, CQHCI_IS);
1466 cqhci_writel(cq_host, tmp, CQHCI_IS);
1467 cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
1468 }
1469 }
1470 }
1471
esdhc_cqe_enable(struct mmc_host * mmc)1472 static void esdhc_cqe_enable(struct mmc_host *mmc)
1473 {
1474 struct sdhci_host *host = mmc_priv(mmc);
1475 struct cqhci_host *cq_host = mmc->cqe_private;
1476 u32 reg;
1477 u16 mode;
1478 int count = 10;
1479
1480 /*
1481 * CQE gets stuck if it sees Buffer Read Enable bit set, which can be
1482 * the case after tuning, so ensure the buffer is drained.
1483 */
1484 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1485 while (reg & SDHCI_DATA_AVAILABLE) {
1486 sdhci_readl(host, SDHCI_BUFFER);
1487 reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
1488 if (count-- == 0) {
1489 dev_warn(mmc_dev(host->mmc),
1490 "CQE may get stuck because the Buffer Read Enable bit is set\n");
1491 break;
1492 }
1493 mdelay(1);
1494 }
1495
1496 /*
1497 * Runtime resume will reset the entire host controller, which
1498 * will also clear the DMAEN/BCEN of register ESDHC_MIX_CTRL.
1499 * Here set DMAEN and BCEN when enable CMDQ.
1500 */
1501 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1502 if (host->flags & SDHCI_REQ_USE_DMA)
1503 mode |= SDHCI_TRNS_DMA;
1504 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1505 mode |= SDHCI_TRNS_BLK_CNT_EN;
1506 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
1507
1508 /*
1509 * Though Runtime resume reset the entire host controller,
1510 * but do not impact the CQHCI side, need to clear the
1511 * HALT bit, avoid CQHCI stuck in the first request when
1512 * system resume back.
1513 */
1514 cqhci_writel(cq_host, 0, CQHCI_CTL);
1515 if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT)
1516 dev_err(mmc_dev(host->mmc),
1517 "failed to exit halt state when enable CQE\n");
1518
1519
1520 sdhci_cqe_enable(mmc);
1521 }
1522
esdhc_sdhci_dumpregs(struct mmc_host * mmc)1523 static void esdhc_sdhci_dumpregs(struct mmc_host *mmc)
1524 {
1525 sdhci_dumpregs(mmc_priv(mmc));
1526 }
1527
1528 static const struct cqhci_host_ops esdhc_cqhci_ops = {
1529 .enable = esdhc_cqe_enable,
1530 .disable = sdhci_cqe_disable,
1531 .dumpregs = esdhc_sdhci_dumpregs,
1532 };
1533
1534 static int
sdhci_esdhc_imx_probe_dt(struct platform_device * pdev,struct sdhci_host * host,struct pltfm_imx_data * imx_data)1535 sdhci_esdhc_imx_probe_dt(struct platform_device *pdev,
1536 struct sdhci_host *host,
1537 struct pltfm_imx_data *imx_data)
1538 {
1539 struct device_node *np = pdev->dev.of_node;
1540 struct esdhc_platform_data *boarddata = &imx_data->boarddata;
1541 int ret;
1542
1543 if (of_get_property(np, "fsl,wp-controller", NULL))
1544 boarddata->wp_type = ESDHC_WP_CONTROLLER;
1545
1546 /*
1547 * If we have this property, then activate WP check.
1548 * Retrieveing and requesting the actual WP GPIO will happen
1549 * in the call to mmc_of_parse().
1550 */
1551 if (of_property_read_bool(np, "wp-gpios"))
1552 boarddata->wp_type = ESDHC_WP_GPIO;
1553
1554 of_property_read_u32(np, "fsl,tuning-step", &boarddata->tuning_step);
1555 of_property_read_u32(np, "fsl,tuning-start-tap",
1556 &boarddata->tuning_start_tap);
1557
1558 of_property_read_u32(np, "fsl,strobe-dll-delay-target",
1559 &boarddata->strobe_dll_delay_target);
1560 if (of_find_property(np, "no-1-8-v", NULL))
1561 host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1562
1563 if (of_property_read_u32(np, "fsl,delay-line", &boarddata->delay_line))
1564 boarddata->delay_line = 0;
1565
1566 mmc_of_parse_voltage(host->mmc, &host->ocr_mask);
1567
1568 if (esdhc_is_usdhc(imx_data) && !IS_ERR(imx_data->pinctrl)) {
1569 imx_data->pins_100mhz = pinctrl_lookup_state(imx_data->pinctrl,
1570 ESDHC_PINCTRL_STATE_100MHZ);
1571 imx_data->pins_200mhz = pinctrl_lookup_state(imx_data->pinctrl,
1572 ESDHC_PINCTRL_STATE_200MHZ);
1573 }
1574
1575 /* call to generic mmc_of_parse to support additional capabilities */
1576 ret = mmc_of_parse(host->mmc);
1577 if (ret)
1578 return ret;
1579
1580 if (mmc_gpio_get_cd(host->mmc) >= 0)
1581 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
1582
1583 return 0;
1584 }
1585
sdhci_esdhc_imx_probe(struct platform_device * pdev)1586 static int sdhci_esdhc_imx_probe(struct platform_device *pdev)
1587 {
1588 struct sdhci_pltfm_host *pltfm_host;
1589 struct sdhci_host *host;
1590 struct cqhci_host *cq_host;
1591 int err;
1592 struct pltfm_imx_data *imx_data;
1593
1594 host = sdhci_pltfm_init(pdev, &sdhci_esdhc_imx_pdata,
1595 sizeof(*imx_data));
1596 if (IS_ERR(host))
1597 return PTR_ERR(host);
1598
1599 pltfm_host = sdhci_priv(host);
1600
1601 imx_data = sdhci_pltfm_priv(pltfm_host);
1602
1603 imx_data->socdata = device_get_match_data(&pdev->dev);
1604
1605 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1606 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1607
1608 imx_data->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1609 if (IS_ERR(imx_data->clk_ipg)) {
1610 err = PTR_ERR(imx_data->clk_ipg);
1611 goto free_sdhci;
1612 }
1613
1614 imx_data->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1615 if (IS_ERR(imx_data->clk_ahb)) {
1616 err = PTR_ERR(imx_data->clk_ahb);
1617 goto free_sdhci;
1618 }
1619
1620 imx_data->clk_per = devm_clk_get(&pdev->dev, "per");
1621 if (IS_ERR(imx_data->clk_per)) {
1622 err = PTR_ERR(imx_data->clk_per);
1623 goto free_sdhci;
1624 }
1625
1626 pltfm_host->clk = imx_data->clk_per;
1627 pltfm_host->clock = clk_get_rate(pltfm_host->clk);
1628 err = clk_prepare_enable(imx_data->clk_per);
1629 if (err)
1630 goto free_sdhci;
1631 err = clk_prepare_enable(imx_data->clk_ipg);
1632 if (err)
1633 goto disable_per_clk;
1634 err = clk_prepare_enable(imx_data->clk_ahb);
1635 if (err)
1636 goto disable_ipg_clk;
1637
1638 imx_data->pinctrl = devm_pinctrl_get(&pdev->dev);
1639 if (IS_ERR(imx_data->pinctrl))
1640 dev_warn(mmc_dev(host->mmc), "could not get pinctrl\n");
1641
1642 if (esdhc_is_usdhc(imx_data)) {
1643 host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
1644 host->mmc->caps |= MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR;
1645
1646 /* GPIO CD can be set as a wakeup source */
1647 host->mmc->caps |= MMC_CAP_CD_WAKE;
1648
1649 if (!(imx_data->socdata->flags & ESDHC_FLAG_HS200))
1650 host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
1651
1652 /* clear tuning bits in case ROM has set it already */
1653 writel(0x0, host->ioaddr + ESDHC_MIX_CTRL);
1654 writel(0x0, host->ioaddr + SDHCI_AUTO_CMD_STATUS);
1655 writel(0x0, host->ioaddr + ESDHC_TUNE_CTRL_STATUS);
1656
1657 /*
1658 * Link usdhc specific mmc_host_ops execute_tuning function,
1659 * to replace the standard one in sdhci_ops.
1660 */
1661 host->mmc_host_ops.execute_tuning = usdhc_execute_tuning;
1662 }
1663
1664 err = sdhci_esdhc_imx_probe_dt(pdev, host, imx_data);
1665 if (err)
1666 goto disable_ahb_clk;
1667
1668 if (imx_data->socdata->flags & ESDHC_FLAG_MAN_TUNING)
1669 sdhci_esdhc_ops.platform_execute_tuning =
1670 esdhc_executing_tuning;
1671
1672 if (imx_data->socdata->flags & ESDHC_FLAG_ERR004536)
1673 host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
1674
1675 if (host->mmc->caps & MMC_CAP_8_BIT_DATA &&
1676 imx_data->socdata->flags & ESDHC_FLAG_HS400)
1677 host->mmc->caps2 |= MMC_CAP2_HS400;
1678
1679 if (imx_data->socdata->flags & ESDHC_FLAG_BROKEN_AUTO_CMD23)
1680 host->quirks2 |= SDHCI_QUIRK2_ACMD23_BROKEN;
1681
1682 if (host->mmc->caps & MMC_CAP_8_BIT_DATA &&
1683 imx_data->socdata->flags & ESDHC_FLAG_HS400_ES) {
1684 host->mmc->caps2 |= MMC_CAP2_HS400_ES;
1685 host->mmc_host_ops.hs400_enhanced_strobe =
1686 esdhc_hs400_enhanced_strobe;
1687 }
1688
1689 if (imx_data->socdata->flags & ESDHC_FLAG_CQHCI) {
1690 host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
1691 cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1692 if (!cq_host) {
1693 err = -ENOMEM;
1694 goto disable_ahb_clk;
1695 }
1696
1697 cq_host->mmio = host->ioaddr + ESDHC_CQHCI_ADDR_OFFSET;
1698 cq_host->ops = &esdhc_cqhci_ops;
1699
1700 err = cqhci_init(cq_host, host->mmc, false);
1701 if (err)
1702 goto disable_ahb_clk;
1703 }
1704
1705 sdhci_esdhc_imx_hwinit(host);
1706
1707 err = sdhci_add_host(host);
1708 if (err)
1709 goto disable_ahb_clk;
1710
1711 /*
1712 * Setup the wakeup capability here, let user to decide
1713 * whether need to enable this wakeup through sysfs interface.
1714 */
1715 if ((host->mmc->pm_caps & MMC_PM_KEEP_POWER) &&
1716 (host->mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ))
1717 device_set_wakeup_capable(&pdev->dev, true);
1718
1719 pm_runtime_set_active(&pdev->dev);
1720 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1721 pm_runtime_use_autosuspend(&pdev->dev);
1722 pm_suspend_ignore_children(&pdev->dev, 1);
1723 pm_runtime_enable(&pdev->dev);
1724
1725 return 0;
1726
1727 disable_ahb_clk:
1728 clk_disable_unprepare(imx_data->clk_ahb);
1729 disable_ipg_clk:
1730 clk_disable_unprepare(imx_data->clk_ipg);
1731 disable_per_clk:
1732 clk_disable_unprepare(imx_data->clk_per);
1733 free_sdhci:
1734 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1735 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1736 sdhci_pltfm_free(pdev);
1737 return err;
1738 }
1739
sdhci_esdhc_imx_remove(struct platform_device * pdev)1740 static int sdhci_esdhc_imx_remove(struct platform_device *pdev)
1741 {
1742 struct sdhci_host *host = platform_get_drvdata(pdev);
1743 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1744 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1745 int dead;
1746
1747 pm_runtime_get_sync(&pdev->dev);
1748 dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
1749 pm_runtime_disable(&pdev->dev);
1750 pm_runtime_put_noidle(&pdev->dev);
1751
1752 sdhci_remove_host(host, dead);
1753
1754 clk_disable_unprepare(imx_data->clk_per);
1755 clk_disable_unprepare(imx_data->clk_ipg);
1756 clk_disable_unprepare(imx_data->clk_ahb);
1757
1758 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1759 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1760
1761 sdhci_pltfm_free(pdev);
1762
1763 return 0;
1764 }
1765
1766 #ifdef CONFIG_PM_SLEEP
sdhci_esdhc_suspend(struct device * dev)1767 static int sdhci_esdhc_suspend(struct device *dev)
1768 {
1769 struct sdhci_host *host = dev_get_drvdata(dev);
1770 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1771 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1772 int ret;
1773
1774 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1775 ret = cqhci_suspend(host->mmc);
1776 if (ret)
1777 return ret;
1778 }
1779
1780 if ((imx_data->socdata->flags & ESDHC_FLAG_STATE_LOST_IN_LPMODE) &&
1781 (host->tuning_mode != SDHCI_TUNING_MODE_1)) {
1782 mmc_retune_timer_stop(host->mmc);
1783 mmc_retune_needed(host->mmc);
1784 }
1785
1786 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1787 mmc_retune_needed(host->mmc);
1788
1789 ret = sdhci_suspend_host(host);
1790 if (ret)
1791 return ret;
1792
1793 ret = pinctrl_pm_select_sleep_state(dev);
1794 if (ret)
1795 return ret;
1796
1797 ret = mmc_gpio_set_cd_wake(host->mmc, true);
1798
1799 return ret;
1800 }
1801
sdhci_esdhc_resume(struct device * dev)1802 static int sdhci_esdhc_resume(struct device *dev)
1803 {
1804 struct sdhci_host *host = dev_get_drvdata(dev);
1805 int ret;
1806
1807 ret = pinctrl_pm_select_default_state(dev);
1808 if (ret)
1809 return ret;
1810
1811 /* re-initialize hw state in case it's lost in low power mode */
1812 sdhci_esdhc_imx_hwinit(host);
1813
1814 ret = sdhci_resume_host(host);
1815 if (ret)
1816 return ret;
1817
1818 if (host->mmc->caps2 & MMC_CAP2_CQE)
1819 ret = cqhci_resume(host->mmc);
1820
1821 if (!ret)
1822 ret = mmc_gpio_set_cd_wake(host->mmc, false);
1823
1824 return ret;
1825 }
1826 #endif
1827
1828 #ifdef CONFIG_PM
sdhci_esdhc_runtime_suspend(struct device * dev)1829 static int sdhci_esdhc_runtime_suspend(struct device *dev)
1830 {
1831 struct sdhci_host *host = dev_get_drvdata(dev);
1832 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1833 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1834 int ret;
1835
1836 if (host->mmc->caps2 & MMC_CAP2_CQE) {
1837 ret = cqhci_suspend(host->mmc);
1838 if (ret)
1839 return ret;
1840 }
1841
1842 ret = sdhci_runtime_suspend_host(host);
1843 if (ret)
1844 return ret;
1845
1846 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
1847 mmc_retune_needed(host->mmc);
1848
1849 imx_data->actual_clock = host->mmc->actual_clock;
1850 esdhc_pltfm_set_clock(host, 0);
1851 clk_disable_unprepare(imx_data->clk_per);
1852 clk_disable_unprepare(imx_data->clk_ipg);
1853 clk_disable_unprepare(imx_data->clk_ahb);
1854
1855 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1856 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1857
1858 return ret;
1859 }
1860
sdhci_esdhc_runtime_resume(struct device * dev)1861 static int sdhci_esdhc_runtime_resume(struct device *dev)
1862 {
1863 struct sdhci_host *host = dev_get_drvdata(dev);
1864 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1865 struct pltfm_imx_data *imx_data = sdhci_pltfm_priv(pltfm_host);
1866 int err;
1867
1868 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1869 cpu_latency_qos_add_request(&imx_data->pm_qos_req, 0);
1870
1871 if (imx_data->socdata->flags & ESDHC_FLAG_CLK_RATE_LOST_IN_PM_RUNTIME)
1872 clk_set_rate(imx_data->clk_per, pltfm_host->clock);
1873
1874 err = clk_prepare_enable(imx_data->clk_ahb);
1875 if (err)
1876 goto remove_pm_qos_request;
1877
1878 err = clk_prepare_enable(imx_data->clk_per);
1879 if (err)
1880 goto disable_ahb_clk;
1881
1882 err = clk_prepare_enable(imx_data->clk_ipg);
1883 if (err)
1884 goto disable_per_clk;
1885
1886 esdhc_pltfm_set_clock(host, imx_data->actual_clock);
1887
1888 err = sdhci_runtime_resume_host(host, 0);
1889 if (err)
1890 goto disable_ipg_clk;
1891
1892 if (host->mmc->caps2 & MMC_CAP2_CQE)
1893 err = cqhci_resume(host->mmc);
1894
1895 return err;
1896
1897 disable_ipg_clk:
1898 clk_disable_unprepare(imx_data->clk_ipg);
1899 disable_per_clk:
1900 clk_disable_unprepare(imx_data->clk_per);
1901 disable_ahb_clk:
1902 clk_disable_unprepare(imx_data->clk_ahb);
1903 remove_pm_qos_request:
1904 if (imx_data->socdata->flags & ESDHC_FLAG_PMQOS)
1905 cpu_latency_qos_remove_request(&imx_data->pm_qos_req);
1906 return err;
1907 }
1908 #endif
1909
1910 static const struct dev_pm_ops sdhci_esdhc_pmops = {
1911 SET_SYSTEM_SLEEP_PM_OPS(sdhci_esdhc_suspend, sdhci_esdhc_resume)
1912 SET_RUNTIME_PM_OPS(sdhci_esdhc_runtime_suspend,
1913 sdhci_esdhc_runtime_resume, NULL)
1914 };
1915
1916 static struct platform_driver sdhci_esdhc_imx_driver = {
1917 .driver = {
1918 .name = "sdhci-esdhc-imx",
1919 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1920 .of_match_table = imx_esdhc_dt_ids,
1921 .pm = &sdhci_esdhc_pmops,
1922 },
1923 .probe = sdhci_esdhc_imx_probe,
1924 .remove = sdhci_esdhc_imx_remove,
1925 };
1926
1927 module_platform_driver(sdhci_esdhc_imx_driver);
1928
1929 MODULE_DESCRIPTION("SDHCI driver for Freescale i.MX eSDHC");
1930 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
1931 MODULE_LICENSE("GPL v2");
1932