1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
5 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
6 * Copyright (C) 2010 ST-Ericsson SA
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/highmem.h>
20 #include <linux/log2.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/pm.h>
23 #include <linux/mmc/host.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/sd.h>
26 #include <linux/mmc/slot-gpio.h>
27 #include <linux/amba/bus.h>
28 #include <linux/clk.h>
29 #include <linux/scatterlist.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/dmaengine.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/amba/mmci.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/types.h>
37 #include <linux/pinctrl/consumer.h>
38 #include <linux/reset.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/workqueue.h>
42 #include <asm/div64.h>
47 #define DRIVER_NAME "mmci-pl18x"
49 static void mmci_variant_init(struct mmci_host *host);
50 static void ux500_variant_init(struct mmci_host *host);
51 static void ux500v2_variant_init(struct mmci_host *host);
53 static unsigned int fmax = 515633;
55 static struct variant_data variant_arm = {
57 .fifohalfsize = 8 * 4,
58 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
59 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
60 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
61 .cmdreg_srsp = MCI_CPSM_RESPONSE,
62 .datalength_bits = 16,
63 .datactrl_blocksz = 11,
64 .pwrreg_powerup = MCI_PWR_UP,
66 .reversed_irq_handling = true,
68 .irq_pio_mask = MCI_IRQ_PIO_MASK,
69 .start_err = MCI_STARTBITERR,
71 .init = mmci_variant_init,
74 static struct variant_data variant_arm_extended_fifo = {
76 .fifohalfsize = 64 * 4,
77 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
78 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
79 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
80 .cmdreg_srsp = MCI_CPSM_RESPONSE,
81 .datalength_bits = 16,
82 .datactrl_blocksz = 11,
83 .pwrreg_powerup = MCI_PWR_UP,
86 .irq_pio_mask = MCI_IRQ_PIO_MASK,
87 .start_err = MCI_STARTBITERR,
89 .init = mmci_variant_init,
92 static struct variant_data variant_arm_extended_fifo_hwfc = {
94 .fifohalfsize = 64 * 4,
95 .clkreg_enable = MCI_ARM_HWFCEN,
96 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
97 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
98 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
99 .cmdreg_srsp = MCI_CPSM_RESPONSE,
100 .datalength_bits = 16,
101 .datactrl_blocksz = 11,
102 .pwrreg_powerup = MCI_PWR_UP,
105 .irq_pio_mask = MCI_IRQ_PIO_MASK,
106 .start_err = MCI_STARTBITERR,
107 .opendrain = MCI_ROD,
108 .init = mmci_variant_init,
111 static struct variant_data variant_u300 = {
113 .fifohalfsize = 8 * 4,
114 .clkreg_enable = MCI_ST_U300_HWFCEN,
115 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
116 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
117 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
118 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
119 .cmdreg_srsp = MCI_CPSM_RESPONSE,
120 .datalength_bits = 16,
121 .datactrl_blocksz = 11,
122 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
124 .pwrreg_powerup = MCI_PWR_ON,
126 .signal_direction = true,
127 .pwrreg_clkgate = true,
128 .pwrreg_nopower = true,
130 .irq_pio_mask = MCI_IRQ_PIO_MASK,
131 .start_err = MCI_STARTBITERR,
133 .init = mmci_variant_init,
136 static struct variant_data variant_nomadik = {
138 .fifohalfsize = 8 * 4,
139 .clkreg = MCI_CLK_ENABLE,
140 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
141 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
142 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
143 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
144 .cmdreg_srsp = MCI_CPSM_RESPONSE,
145 .datalength_bits = 24,
146 .datactrl_blocksz = 11,
147 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
150 .pwrreg_powerup = MCI_PWR_ON,
152 .signal_direction = true,
153 .pwrreg_clkgate = true,
154 .pwrreg_nopower = true,
156 .irq_pio_mask = MCI_IRQ_PIO_MASK,
157 .start_err = MCI_STARTBITERR,
159 .init = mmci_variant_init,
162 static struct variant_data variant_ux500 = {
164 .fifohalfsize = 8 * 4,
165 .clkreg = MCI_CLK_ENABLE,
166 .clkreg_enable = MCI_ST_UX500_HWFCEN,
167 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
168 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
169 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
170 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
171 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
172 .cmdreg_srsp = MCI_CPSM_RESPONSE,
173 .datalength_bits = 24,
174 .datactrl_blocksz = 11,
175 .datactrl_any_blocksz = true,
176 .dma_power_of_2 = true,
177 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
180 .pwrreg_powerup = MCI_PWR_ON,
182 .signal_direction = true,
183 .pwrreg_clkgate = true,
185 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
186 .busy_detect_flag = MCI_ST_CARDBUSY,
187 .busy_detect_mask = MCI_ST_BUSYENDMASK,
188 .pwrreg_nopower = true,
190 .irq_pio_mask = MCI_IRQ_PIO_MASK,
191 .start_err = MCI_STARTBITERR,
193 .init = ux500_variant_init,
196 static struct variant_data variant_ux500v2 = {
198 .fifohalfsize = 8 * 4,
199 .clkreg = MCI_CLK_ENABLE,
200 .clkreg_enable = MCI_ST_UX500_HWFCEN,
201 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
202 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
203 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
204 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
205 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
206 .cmdreg_srsp = MCI_CPSM_RESPONSE,
207 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
208 .datalength_bits = 24,
209 .datactrl_blocksz = 11,
210 .datactrl_any_blocksz = true,
211 .dma_power_of_2 = true,
212 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
215 .pwrreg_powerup = MCI_PWR_ON,
217 .signal_direction = true,
218 .pwrreg_clkgate = true,
220 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
221 .busy_detect_flag = MCI_ST_CARDBUSY,
222 .busy_detect_mask = MCI_ST_BUSYENDMASK,
223 .pwrreg_nopower = true,
225 .irq_pio_mask = MCI_IRQ_PIO_MASK,
226 .start_err = MCI_STARTBITERR,
228 .init = ux500v2_variant_init,
231 static struct variant_data variant_stm32 = {
233 .fifohalfsize = 8 * 4,
234 .clkreg = MCI_CLK_ENABLE,
235 .clkreg_enable = MCI_ST_UX500_HWFCEN,
236 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
237 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
238 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
239 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
240 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
241 .cmdreg_srsp = MCI_CPSM_RESPONSE,
242 .irq_pio_mask = MCI_IRQ_PIO_MASK,
243 .datalength_bits = 24,
244 .datactrl_blocksz = 11,
245 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
248 .pwrreg_powerup = MCI_PWR_ON,
250 .pwrreg_clkgate = true,
251 .pwrreg_nopower = true,
252 .init = mmci_variant_init,
255 static struct variant_data variant_stm32_sdmmc = {
257 .fifohalfsize = 8 * 4,
259 .stm32_clkdiv = true,
260 .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
261 .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
262 .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
263 .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
264 .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
265 .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
266 .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
267 .datactrl_first = true,
268 .datacnt_useless = true,
269 .datalength_bits = 25,
270 .datactrl_blocksz = 14,
271 .datactrl_any_blocksz = true,
272 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
273 .stm32_idmabsize_mask = GENMASK(12, 5),
274 .stm32_idmabsize_align = BIT(5),
275 .busy_timeout = true,
277 .busy_detect_flag = MCI_STM32_BUSYD0,
278 .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK,
279 .init = sdmmc_variant_init,
282 static struct variant_data variant_stm32_sdmmcv2 = {
284 .fifohalfsize = 8 * 4,
286 .stm32_clkdiv = true,
287 .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
288 .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
289 .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
290 .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
291 .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
292 .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
293 .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
294 .datactrl_first = true,
295 .datacnt_useless = true,
296 .datalength_bits = 25,
297 .datactrl_blocksz = 14,
298 .datactrl_any_blocksz = true,
299 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
300 .stm32_idmabsize_mask = GENMASK(16, 5),
301 .stm32_idmabsize_align = BIT(5),
303 .busy_timeout = true,
305 .busy_detect_flag = MCI_STM32_BUSYD0,
306 .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK,
307 .init = sdmmc_variant_init,
310 static struct variant_data variant_stm32_sdmmcv3 = {
312 .fifohalfsize = 128 * 4,
314 .stm32_clkdiv = true,
315 .cmdreg_cpsm_enable = MCI_CPSM_STM32_ENABLE,
316 .cmdreg_lrsp_crc = MCI_CPSM_STM32_LRSP_CRC,
317 .cmdreg_srsp_crc = MCI_CPSM_STM32_SRSP_CRC,
318 .cmdreg_srsp = MCI_CPSM_STM32_SRSP,
319 .cmdreg_stop = MCI_CPSM_STM32_CMDSTOP,
320 .data_cmd_enable = MCI_CPSM_STM32_CMDTRANS,
321 .irq_pio_mask = MCI_IRQ_PIO_STM32_MASK,
322 .datactrl_first = true,
323 .datacnt_useless = true,
324 .datalength_bits = 25,
325 .datactrl_blocksz = 14,
326 .datactrl_any_blocksz = true,
327 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
328 .stm32_idmabsize_mask = GENMASK(16, 6),
329 .stm32_idmabsize_align = BIT(6),
331 .busy_timeout = true,
333 .busy_detect_flag = MCI_STM32_BUSYD0,
334 .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK,
335 .init = sdmmc_variant_init,
338 static struct variant_data variant_qcom = {
340 .fifohalfsize = 8 * 4,
341 .clkreg = MCI_CLK_ENABLE,
342 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
343 MCI_QCOM_CLK_SELECT_IN_FBCLK,
344 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
345 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
346 .cmdreg_cpsm_enable = MCI_CPSM_ENABLE,
347 .cmdreg_lrsp_crc = MCI_CPSM_RESPONSE | MCI_CPSM_LONGRSP,
348 .cmdreg_srsp_crc = MCI_CPSM_RESPONSE,
349 .cmdreg_srsp = MCI_CPSM_RESPONSE,
350 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
351 .datalength_bits = 24,
352 .datactrl_blocksz = 11,
353 .datactrl_any_blocksz = true,
354 .pwrreg_powerup = MCI_PWR_UP,
356 .explicit_mclk_control = true,
360 .irq_pio_mask = MCI_IRQ_PIO_MASK,
361 .start_err = MCI_STARTBITERR,
362 .opendrain = MCI_ROD,
363 .init = qcom_variant_init,
366 /* Busy detection for the ST Micro variant */
367 static int mmci_card_busy(struct mmc_host *mmc)
369 struct mmci_host *host = mmc_priv(mmc);
373 spin_lock_irqsave(&host->lock, flags);
374 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
376 spin_unlock_irqrestore(&host->lock, flags);
381 static void mmci_reg_delay(struct mmci_host *host)
384 * According to the spec, at least three feedback clock cycles
385 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
386 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
387 * Worst delay time during card init is at 100 kHz => 30 us.
388 * Worst delay time when up and running is at 25 MHz => 120 ns.
390 if (host->cclk < 25000000)
397 * This must be called with host->lock held
399 void mmci_write_clkreg(struct mmci_host *host, u32 clk)
401 if (host->clk_reg != clk) {
403 writel(clk, host->base + MMCICLOCK);
408 * This must be called with host->lock held
410 void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
412 if (host->pwr_reg != pwr) {
414 writel(pwr, host->base + MMCIPOWER);
419 * This must be called with host->lock held
421 static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
423 /* Keep busy mode in DPSM if enabled */
424 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
426 if (host->datactrl_reg != datactrl) {
427 host->datactrl_reg = datactrl;
428 writel(datactrl, host->base + MMCIDATACTRL);
433 * This must be called with host->lock held
435 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
437 struct variant_data *variant = host->variant;
438 u32 clk = variant->clkreg;
440 /* Make sure cclk reflects the current calculated clock */
444 if (variant->explicit_mclk_control) {
445 host->cclk = host->mclk;
446 } else if (desired >= host->mclk) {
447 clk = MCI_CLK_BYPASS;
448 if (variant->st_clkdiv)
449 clk |= MCI_ST_UX500_NEG_EDGE;
450 host->cclk = host->mclk;
451 } else if (variant->st_clkdiv) {
453 * DB8500 TRM says f = mclk / (clkdiv + 2)
454 * => clkdiv = (mclk / f) - 2
455 * Round the divider up so we don't exceed the max
458 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
461 host->cclk = host->mclk / (clk + 2);
464 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
465 * => clkdiv = mclk / (2 * f) - 1
467 clk = host->mclk / (2 * desired) - 1;
470 host->cclk = host->mclk / (2 * (clk + 1));
473 clk |= variant->clkreg_enable;
474 clk |= MCI_CLK_ENABLE;
475 /* This hasn't proven to be worthwhile */
476 /* clk |= MCI_CLK_PWRSAVE; */
479 /* Set actual clock for debug */
480 host->mmc->actual_clock = host->cclk;
482 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
484 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
485 clk |= variant->clkreg_8bit_bus_enable;
487 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
488 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
489 clk |= variant->clkreg_neg_edge_enable;
491 mmci_write_clkreg(host, clk);
494 static void mmci_dma_release(struct mmci_host *host)
496 if (host->ops && host->ops->dma_release)
497 host->ops->dma_release(host);
499 host->use_dma = false;
502 static void mmci_dma_setup(struct mmci_host *host)
504 if (!host->ops || !host->ops->dma_setup)
507 if (host->ops->dma_setup(host))
510 /* initialize pre request cookie */
511 host->next_cookie = 1;
513 host->use_dma = true;
517 * Validate mmc prerequisites
519 static int mmci_validate_data(struct mmci_host *host,
520 struct mmc_data *data)
522 struct variant_data *variant = host->variant;
526 if (!is_power_of_2(data->blksz) && !variant->datactrl_any_blocksz) {
527 dev_err(mmc_dev(host->mmc),
528 "unsupported block size (%d bytes)\n", data->blksz);
532 if (host->ops && host->ops->validate_data)
533 return host->ops->validate_data(host, data);
538 static int mmci_prep_data(struct mmci_host *host, struct mmc_data *data, bool next)
542 if (!host->ops || !host->ops->prep_data)
545 err = host->ops->prep_data(host, data, next);
548 data->host_cookie = ++host->next_cookie < 0 ?
549 1 : host->next_cookie;
554 static void mmci_unprep_data(struct mmci_host *host, struct mmc_data *data,
557 if (host->ops && host->ops->unprep_data)
558 host->ops->unprep_data(host, data, err);
560 data->host_cookie = 0;
563 static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
565 WARN_ON(data->host_cookie && data->host_cookie != host->next_cookie);
567 if (host->ops && host->ops->get_next_data)
568 host->ops->get_next_data(host, data);
571 static int mmci_dma_start(struct mmci_host *host, unsigned int datactrl)
573 struct mmc_data *data = host->data;
579 ret = mmci_prep_data(host, data, false);
583 if (!host->ops || !host->ops->dma_start)
586 /* Okay, go for it. */
587 dev_vdbg(mmc_dev(host->mmc),
588 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
589 data->sg_len, data->blksz, data->blocks, data->flags);
591 ret = host->ops->dma_start(host, &datactrl);
595 /* Trigger the DMA transfer */
596 mmci_write_datactrlreg(host, datactrl);
599 * Let the MMCI say when the data is ended and it's time
600 * to fire next DMA request. When that happens, MMCI will
601 * call mmci_data_end()
603 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
604 host->base + MMCIMASK0);
608 static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
613 if (host->ops && host->ops->dma_finalize)
614 host->ops->dma_finalize(host, data);
617 static void mmci_dma_error(struct mmci_host *host)
622 if (host->ops && host->ops->dma_error)
623 host->ops->dma_error(host);
627 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
629 writel(0, host->base + MMCICOMMAND);
636 mmc_request_done(host->mmc, mrq);
639 static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
641 void __iomem *base = host->base;
642 struct variant_data *variant = host->variant;
644 if (host->singleirq) {
645 unsigned int mask0 = readl(base + MMCIMASK0);
647 mask0 &= ~variant->irq_pio_mask;
650 writel(mask0, base + MMCIMASK0);
653 if (variant->mmcimask1)
654 writel(mask, base + MMCIMASK1);
656 host->mask1_reg = mask;
659 static void mmci_stop_data(struct mmci_host *host)
661 mmci_write_datactrlreg(host, 0);
662 mmci_set_mask1(host, 0);
666 static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
668 unsigned int flags = SG_MITER_ATOMIC;
670 if (data->flags & MMC_DATA_READ)
671 flags |= SG_MITER_TO_SG;
673 flags |= SG_MITER_FROM_SG;
675 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
678 static u32 mmci_get_dctrl_cfg(struct mmci_host *host)
680 return MCI_DPSM_ENABLE | mmci_dctrl_blksz(host);
683 static u32 ux500v2_get_dctrl_cfg(struct mmci_host *host)
685 return MCI_DPSM_ENABLE | (host->data->blksz << 16);
688 static void ux500_busy_clear_mask_done(struct mmci_host *host)
690 void __iomem *base = host->base;
692 writel(host->variant->busy_detect_mask, base + MMCICLEAR);
693 writel(readl(base + MMCIMASK0) &
694 ~host->variant->busy_detect_mask, base + MMCIMASK0);
695 host->busy_state = MMCI_BUSY_DONE;
696 host->busy_status = 0;
700 * ux500_busy_complete() - this will wait until the busy status
701 * goes off, saving any status that occur in the meantime into
702 * host->busy_status until we know the card is not busy any more.
703 * The function returns true when the busy detection is ended
704 * and we should continue processing the command.
706 * The Ux500 typically fires two IRQs over a busy cycle like this:
708 * DAT0 busy +-----------------+
710 * DAT0 not busy ----+ +--------
716 static bool ux500_busy_complete(struct mmci_host *host, struct mmc_command *cmd,
717 u32 status, u32 err_msk)
719 void __iomem *base = host->base;
722 if (status & err_msk) {
723 /* Stop any ongoing busy detection if an error occurs */
724 ux500_busy_clear_mask_done(host);
729 * The state transitions are encoded in a state machine crossing
730 * the edges in this switch statement.
732 switch (host->busy_state) {
735 * Before unmasking for the busy end IRQ, confirm that the
736 * command was sent successfully. To keep track of having a
737 * command in-progress, waiting for busy signaling to end,
738 * store the status in host->busy_status.
740 * Note that, the card may need a couple of clock cycles before
741 * it starts signaling busy on DAT0, hence re-read the
742 * MMCISTATUS register here, to allow the busy bit to be set.
746 * Save the first status register read to be sure to catch
747 * all bits that may be lost will retrying. If the command
748 * is still busy this will result in assigning 0 to
749 * host->busy_status, which is what it should be in IDLE.
751 host->busy_status = status & (MCI_CMDSENT | MCI_CMDRESPEND);
753 status = readl(base + MMCISTATUS);
754 /* Keep accumulating status bits */
755 host->busy_status |= status & (MCI_CMDSENT | MCI_CMDRESPEND);
756 if (status & host->variant->busy_detect_flag) {
757 writel(readl(base + MMCIMASK0) |
758 host->variant->busy_detect_mask,
760 host->busy_state = MMCI_BUSY_WAITING_FOR_START_IRQ;
761 schedule_delayed_work(&host->ux500_busy_timeout_work,
762 msecs_to_jiffies(cmd->busy_timeout));
767 dev_dbg(mmc_dev(host->mmc),
768 "no busy signalling in time CMD%02x\n", cmd->opcode);
769 ux500_busy_clear_mask_done(host);
773 * If there is a command in-progress that has been successfully
774 * sent, then bail out if busy status is set and wait for the
777 * Note that, the HW triggers an IRQ on both edges while
778 * monitoring DAT0 for busy completion, but there is only one
779 * status bit in MMCISTATUS for the busy state. Therefore
780 * both the start and the end interrupts needs to be cleared,
781 * one after the other. So, clear the busy start IRQ here.
783 case MMCI_BUSY_WAITING_FOR_START_IRQ:
784 if (status & host->variant->busy_detect_flag) {
785 host->busy_status |= status & (MCI_CMDSENT | MCI_CMDRESPEND);
786 writel(host->variant->busy_detect_mask, base + MMCICLEAR);
787 host->busy_state = MMCI_BUSY_WAITING_FOR_END_IRQ;
789 dev_dbg(mmc_dev(host->mmc),
790 "lost busy status when waiting for busy start IRQ CMD%02x\n",
792 cancel_delayed_work(&host->ux500_busy_timeout_work);
793 ux500_busy_clear_mask_done(host);
797 case MMCI_BUSY_WAITING_FOR_END_IRQ:
798 if (!(status & host->variant->busy_detect_flag)) {
799 host->busy_status |= status & (MCI_CMDSENT | MCI_CMDRESPEND);
800 writel(host->variant->busy_detect_mask, base + MMCICLEAR);
801 cancel_delayed_work(&host->ux500_busy_timeout_work);
802 ux500_busy_clear_mask_done(host);
804 dev_dbg(mmc_dev(host->mmc),
805 "busy status still asserted when handling busy end IRQ - will keep waiting CMD%02x\n",
811 dev_dbg(mmc_dev(host->mmc), "fell through on state %d, CMD%02x\n",
812 host->busy_state, cmd->opcode);
817 return (host->busy_state == MMCI_BUSY_DONE);
821 * All the DMA operation mode stuff goes inside this ifdef.
822 * This assumes that you have a generic DMA device interface,
823 * no custom DMA interfaces are supported.
825 #ifdef CONFIG_DMA_ENGINE
826 struct mmci_dmae_next {
827 struct dma_async_tx_descriptor *desc;
828 struct dma_chan *chan;
831 struct mmci_dmae_priv {
832 struct dma_chan *cur;
833 struct dma_chan *rx_channel;
834 struct dma_chan *tx_channel;
835 struct dma_async_tx_descriptor *desc_current;
836 struct mmci_dmae_next next_data;
839 int mmci_dmae_setup(struct mmci_host *host)
841 const char *rxname, *txname;
842 struct mmci_dmae_priv *dmae;
844 dmae = devm_kzalloc(mmc_dev(host->mmc), sizeof(*dmae), GFP_KERNEL);
848 host->dma_priv = dmae;
850 dmae->rx_channel = dma_request_chan(mmc_dev(host->mmc), "rx");
851 if (IS_ERR(dmae->rx_channel)) {
852 int ret = PTR_ERR(dmae->rx_channel);
853 dmae->rx_channel = NULL;
857 dmae->tx_channel = dma_request_chan(mmc_dev(host->mmc), "tx");
858 if (IS_ERR(dmae->tx_channel)) {
859 if (PTR_ERR(dmae->tx_channel) == -EPROBE_DEFER)
860 dev_warn(mmc_dev(host->mmc),
861 "Deferred probe for TX channel ignored\n");
862 dmae->tx_channel = NULL;
866 * If only an RX channel is specified, the driver will
867 * attempt to use it bidirectionally, however if it
868 * is specified but cannot be located, DMA will be disabled.
870 if (dmae->rx_channel && !dmae->tx_channel)
871 dmae->tx_channel = dmae->rx_channel;
873 if (dmae->rx_channel)
874 rxname = dma_chan_name(dmae->rx_channel);
878 if (dmae->tx_channel)
879 txname = dma_chan_name(dmae->tx_channel);
883 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
887 * Limit the maximum segment size in any SG entry according to
888 * the parameters of the DMA engine device.
890 if (dmae->tx_channel) {
891 struct device *dev = dmae->tx_channel->device->dev;
892 unsigned int max_seg_size = dma_get_max_seg_size(dev);
894 if (max_seg_size < host->mmc->max_seg_size)
895 host->mmc->max_seg_size = max_seg_size;
897 if (dmae->rx_channel) {
898 struct device *dev = dmae->rx_channel->device->dev;
899 unsigned int max_seg_size = dma_get_max_seg_size(dev);
901 if (max_seg_size < host->mmc->max_seg_size)
902 host->mmc->max_seg_size = max_seg_size;
905 if (!dmae->tx_channel || !dmae->rx_channel) {
906 mmci_dmae_release(host);
914 * This is used in or so inline it
915 * so it can be discarded.
917 void mmci_dmae_release(struct mmci_host *host)
919 struct mmci_dmae_priv *dmae = host->dma_priv;
921 if (dmae->rx_channel)
922 dma_release_channel(dmae->rx_channel);
923 if (dmae->tx_channel)
924 dma_release_channel(dmae->tx_channel);
925 dmae->rx_channel = dmae->tx_channel = NULL;
928 static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
930 struct mmci_dmae_priv *dmae = host->dma_priv;
931 struct dma_chan *chan;
933 if (data->flags & MMC_DATA_READ)
934 chan = dmae->rx_channel;
936 chan = dmae->tx_channel;
938 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
939 mmc_get_dma_dir(data));
942 void mmci_dmae_error(struct mmci_host *host)
944 struct mmci_dmae_priv *dmae = host->dma_priv;
946 if (!dma_inprogress(host))
949 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
950 dmaengine_terminate_all(dmae->cur);
951 host->dma_in_progress = false;
953 dmae->desc_current = NULL;
954 host->data->host_cookie = 0;
956 mmci_dma_unmap(host, host->data);
959 void mmci_dmae_finalize(struct mmci_host *host, struct mmc_data *data)
961 struct mmci_dmae_priv *dmae = host->dma_priv;
965 if (!dma_inprogress(host))
968 /* Wait up to 1ms for the DMA to complete */
970 status = readl(host->base + MMCISTATUS);
971 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
977 * Check to see whether we still have some data left in the FIFO -
978 * this catches DMA controllers which are unable to monitor the
979 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
980 * contiguous buffers. On TX, we'll get a FIFO underrun error.
982 if (status & MCI_RXDATAAVLBLMASK) {
983 mmci_dma_error(host);
986 } else if (!data->host_cookie) {
987 mmci_dma_unmap(host, data);
991 * Use of DMA with scatter-gather is impossible.
992 * Give up with DMA and switch back to PIO mode.
994 if (status & MCI_RXDATAAVLBLMASK) {
995 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
996 mmci_dma_release(host);
999 host->dma_in_progress = false;
1001 dmae->desc_current = NULL;
1004 /* prepares DMA channel and DMA descriptor, returns non-zero on failure */
1005 static int _mmci_dmae_prep_data(struct mmci_host *host, struct mmc_data *data,
1006 struct dma_chan **dma_chan,
1007 struct dma_async_tx_descriptor **dma_desc)
1009 struct mmci_dmae_priv *dmae = host->dma_priv;
1010 struct variant_data *variant = host->variant;
1011 struct dma_slave_config conf = {
1012 .src_addr = host->phybase + MMCIFIFO,
1013 .dst_addr = host->phybase + MMCIFIFO,
1014 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1015 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
1016 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
1017 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
1020 struct dma_chan *chan;
1021 struct dma_device *device;
1022 struct dma_async_tx_descriptor *desc;
1024 unsigned long flags = DMA_CTRL_ACK;
1026 if (data->flags & MMC_DATA_READ) {
1027 conf.direction = DMA_DEV_TO_MEM;
1028 chan = dmae->rx_channel;
1030 conf.direction = DMA_MEM_TO_DEV;
1031 chan = dmae->tx_channel;
1034 /* If there's no DMA channel, fall back to PIO */
1038 /* If less than or equal to the fifo size, don't bother with DMA */
1039 if (data->blksz * data->blocks <= variant->fifosize)
1043 * This is necessary to get SDIO working on the Ux500. We do not yet
1044 * know if this is a bug in:
1045 * - The Ux500 DMA controller (DMA40)
1046 * - The MMCI DMA interface on the Ux500
1047 * some power of two blocks (such as 64 bytes) are sent regularly
1048 * during SDIO traffic and those work fine so for these we enable DMA
1051 if (host->variant->dma_power_of_2 && !is_power_of_2(data->blksz))
1054 device = chan->device;
1055 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
1056 mmc_get_dma_dir(data));
1060 if (host->variant->qcom_dml)
1061 flags |= DMA_PREP_INTERRUPT;
1063 dmaengine_slave_config(chan, &conf);
1064 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
1065 conf.direction, flags);
1075 dma_unmap_sg(device->dev, data->sg, data->sg_len,
1076 mmc_get_dma_dir(data));
1080 int mmci_dmae_prep_data(struct mmci_host *host,
1081 struct mmc_data *data,
1084 struct mmci_dmae_priv *dmae = host->dma_priv;
1085 struct mmci_dmae_next *nd = &dmae->next_data;
1091 return _mmci_dmae_prep_data(host, data, &nd->chan, &nd->desc);
1092 /* Check if next job is already prepared. */
1093 if (dmae->cur && dmae->desc_current)
1096 /* No job were prepared thus do it now. */
1097 return _mmci_dmae_prep_data(host, data, &dmae->cur,
1098 &dmae->desc_current);
1101 int mmci_dmae_start(struct mmci_host *host, unsigned int *datactrl)
1103 struct mmci_dmae_priv *dmae = host->dma_priv;
1106 host->dma_in_progress = true;
1107 ret = dma_submit_error(dmaengine_submit(dmae->desc_current));
1109 host->dma_in_progress = false;
1112 dma_async_issue_pending(dmae->cur);
1114 *datactrl |= MCI_DPSM_DMAENABLE;
1119 void mmci_dmae_get_next_data(struct mmci_host *host, struct mmc_data *data)
1121 struct mmci_dmae_priv *dmae = host->dma_priv;
1122 struct mmci_dmae_next *next = &dmae->next_data;
1127 WARN_ON(!data->host_cookie && (next->desc || next->chan));
1129 dmae->desc_current = next->desc;
1130 dmae->cur = next->chan;
1135 void mmci_dmae_unprep_data(struct mmci_host *host,
1136 struct mmc_data *data, int err)
1139 struct mmci_dmae_priv *dmae = host->dma_priv;
1144 mmci_dma_unmap(host, data);
1147 struct mmci_dmae_next *next = &dmae->next_data;
1148 struct dma_chan *chan;
1149 if (data->flags & MMC_DATA_READ)
1150 chan = dmae->rx_channel;
1152 chan = dmae->tx_channel;
1153 dmaengine_terminate_all(chan);
1155 if (dmae->desc_current == next->desc)
1156 dmae->desc_current = NULL;
1158 if (dmae->cur == next->chan) {
1159 host->dma_in_progress = false;
1168 static struct mmci_host_ops mmci_variant_ops = {
1169 .prep_data = mmci_dmae_prep_data,
1170 .unprep_data = mmci_dmae_unprep_data,
1171 .get_datactrl_cfg = mmci_get_dctrl_cfg,
1172 .get_next_data = mmci_dmae_get_next_data,
1173 .dma_setup = mmci_dmae_setup,
1174 .dma_release = mmci_dmae_release,
1175 .dma_start = mmci_dmae_start,
1176 .dma_finalize = mmci_dmae_finalize,
1177 .dma_error = mmci_dmae_error,
1180 static struct mmci_host_ops mmci_variant_ops = {
1181 .get_datactrl_cfg = mmci_get_dctrl_cfg,
1185 static void mmci_variant_init(struct mmci_host *host)
1187 host->ops = &mmci_variant_ops;
1190 static void ux500_variant_init(struct mmci_host *host)
1192 host->ops = &mmci_variant_ops;
1193 host->ops->busy_complete = ux500_busy_complete;
1196 static void ux500v2_variant_init(struct mmci_host *host)
1198 host->ops = &mmci_variant_ops;
1199 host->ops->busy_complete = ux500_busy_complete;
1200 host->ops->get_datactrl_cfg = ux500v2_get_dctrl_cfg;
1203 static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
1205 struct mmci_host *host = mmc_priv(mmc);
1206 struct mmc_data *data = mrq->data;
1211 WARN_ON(data->host_cookie);
1213 if (mmci_validate_data(host, data))
1216 mmci_prep_data(host, data, true);
1219 static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
1222 struct mmci_host *host = mmc_priv(mmc);
1223 struct mmc_data *data = mrq->data;
1225 if (!data || !data->host_cookie)
1228 mmci_unprep_data(host, data, err);
1231 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
1233 struct variant_data *variant = host->variant;
1234 unsigned int datactrl, timeout, irqmask;
1235 unsigned long long clks;
1238 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
1239 data->blksz, data->blocks, data->flags);
1242 host->size = data->blksz * data->blocks;
1243 data->bytes_xfered = 0;
1245 clks = (unsigned long long)data->timeout_ns * host->cclk;
1246 do_div(clks, NSEC_PER_SEC);
1248 timeout = data->timeout_clks + (unsigned int)clks;
1251 writel(timeout, base + MMCIDATATIMER);
1252 writel(host->size, base + MMCIDATALENGTH);
1254 datactrl = host->ops->get_datactrl_cfg(host);
1255 datactrl |= host->data->flags & MMC_DATA_READ ? MCI_DPSM_DIRECTION : 0;
1257 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
1260 datactrl |= variant->datactrl_mask_sdio;
1263 * The ST Micro variant for SDIO small write transfers
1264 * needs to have clock H/W flow control disabled,
1265 * otherwise the transfer will not start. The threshold
1266 * depends on the rate of MCLK.
1268 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
1270 (host->size <= 8 && host->mclk > 50000000)))
1271 clk = host->clk_reg & ~variant->clkreg_enable;
1273 clk = host->clk_reg | variant->clkreg_enable;
1275 mmci_write_clkreg(host, clk);
1278 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
1279 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
1280 datactrl |= variant->datactrl_mask_ddrmode;
1283 * Attempt to use DMA operation mode, if this
1284 * should fail, fall back to PIO mode
1286 if (!mmci_dma_start(host, datactrl))
1289 /* IRQ mode, map the SG list for CPU reading/writing */
1290 mmci_init_sg(host, data);
1292 if (data->flags & MMC_DATA_READ) {
1293 irqmask = MCI_RXFIFOHALFFULLMASK;
1296 * If we have less than the fifo 'half-full' threshold to
1297 * transfer, trigger a PIO interrupt as soon as any data
1300 if (host->size < variant->fifohalfsize)
1301 irqmask |= MCI_RXDATAAVLBLMASK;
1304 * We don't actually need to include "FIFO empty" here
1305 * since its implicit in "FIFO half empty".
1307 irqmask = MCI_TXFIFOHALFEMPTYMASK;
1310 mmci_write_datactrlreg(host, datactrl);
1311 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
1312 mmci_set_mask1(host, irqmask);
1316 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
1318 void __iomem *base = host->base;
1319 bool busy_resp = cmd->flags & MMC_RSP_BUSY;
1320 unsigned long long clks;
1322 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
1323 cmd->opcode, cmd->arg, cmd->flags);
1325 if (readl(base + MMCICOMMAND) & host->variant->cmdreg_cpsm_enable) {
1326 writel(0, base + MMCICOMMAND);
1327 mmci_reg_delay(host);
1330 if (host->variant->cmdreg_stop &&
1331 cmd->opcode == MMC_STOP_TRANSMISSION)
1332 c |= host->variant->cmdreg_stop;
1334 c |= cmd->opcode | host->variant->cmdreg_cpsm_enable;
1335 if (cmd->flags & MMC_RSP_PRESENT) {
1336 if (cmd->flags & MMC_RSP_136)
1337 c |= host->variant->cmdreg_lrsp_crc;
1338 else if (cmd->flags & MMC_RSP_CRC)
1339 c |= host->variant->cmdreg_srsp_crc;
1341 c |= host->variant->cmdreg_srsp;
1344 host->busy_status = 0;
1345 host->busy_state = MMCI_BUSY_DONE;
1347 /* Assign a default timeout if the core does not provide one */
1348 if (busy_resp && !cmd->busy_timeout)
1349 cmd->busy_timeout = 10 * MSEC_PER_SEC;
1351 if (busy_resp && host->variant->busy_timeout) {
1352 if (cmd->busy_timeout > host->mmc->max_busy_timeout)
1353 clks = (unsigned long long)host->mmc->max_busy_timeout * host->cclk;
1355 clks = (unsigned long long)cmd->busy_timeout * host->cclk;
1357 do_div(clks, MSEC_PER_SEC);
1358 writel_relaxed(clks, host->base + MMCIDATATIMER);
1361 if (host->ops->pre_sig_volt_switch && cmd->opcode == SD_SWITCH_VOLTAGE)
1362 host->ops->pre_sig_volt_switch(host);
1365 c |= MCI_CPSM_INTERRUPT;
1367 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
1368 c |= host->variant->data_cmd_enable;
1372 writel(cmd->arg, base + MMCIARGUMENT);
1373 writel(c, base + MMCICOMMAND);
1376 static void mmci_stop_command(struct mmci_host *host)
1378 host->stop_abort.error = 0;
1379 mmci_start_command(host, &host->stop_abort, 0);
1383 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
1384 unsigned int status)
1386 unsigned int status_err;
1388 /* Make sure we have data to handle */
1392 /* First check for errors */
1393 status_err = status & (host->variant->start_err |
1394 MCI_DATACRCFAIL | MCI_DATATIMEOUT |
1395 MCI_TXUNDERRUN | MCI_RXOVERRUN);
1398 u32 remain, success;
1400 /* Terminate the DMA transfer */
1401 mmci_dma_error(host);
1404 * Calculate how far we are into the transfer. Note that
1405 * the data counter gives the number of bytes transferred
1406 * on the MMC bus, not on the host side. On reads, this
1407 * can be as much as a FIFO-worth of data ahead. This
1408 * matters for FIFO overruns only.
1410 if (!host->variant->datacnt_useless) {
1411 remain = readl(host->base + MMCIDATACNT);
1412 success = data->blksz * data->blocks - remain;
1417 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
1418 status_err, success);
1419 if (status_err & MCI_DATACRCFAIL) {
1420 /* Last block was not successful */
1422 data->error = -EILSEQ;
1423 } else if (status_err & MCI_DATATIMEOUT) {
1424 data->error = -ETIMEDOUT;
1425 } else if (status_err & MCI_STARTBITERR) {
1426 data->error = -ECOMM;
1427 } else if (status_err & MCI_TXUNDERRUN) {
1429 } else if (status_err & MCI_RXOVERRUN) {
1430 if (success > host->variant->fifosize)
1431 success -= host->variant->fifosize;
1436 data->bytes_xfered = round_down(success, data->blksz);
1439 if (status & MCI_DATABLOCKEND)
1440 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
1442 if (status & MCI_DATAEND || data->error) {
1443 mmci_dma_finalize(host, data);
1445 mmci_stop_data(host);
1448 /* The error clause is handled above, success! */
1449 data->bytes_xfered = data->blksz * data->blocks;
1452 if (host->variant->cmdreg_stop && data->error)
1453 mmci_stop_command(host);
1455 mmci_request_end(host, data->mrq);
1456 } else if (host->mrq->sbc && !data->error) {
1457 mmci_request_end(host, data->mrq);
1459 mmci_start_command(host, data->stop, 0);
1465 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1466 unsigned int status)
1468 u32 err_msk = MCI_CMDCRCFAIL | MCI_CMDTIMEOUT;
1469 void __iomem *base = host->base;
1470 bool sbc, busy_resp;
1475 sbc = (cmd == host->mrq->sbc);
1476 busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
1479 * We need to be one of these interrupts to be considered worth
1480 * handling. Note that we tag on any latent IRQs postponed
1481 * due to waiting for busy status.
1483 if (host->variant->busy_timeout && busy_resp)
1484 err_msk |= MCI_DATATIMEOUT;
1486 if (!((status | host->busy_status) &
1487 (err_msk | MCI_CMDSENT | MCI_CMDRESPEND)))
1490 /* Handle busy detection on DAT0 if the variant supports it. */
1491 if (busy_resp && host->variant->busy_detect)
1492 if (!host->ops->busy_complete(host, cmd, status, err_msk))
1497 if (status & MCI_CMDTIMEOUT) {
1498 cmd->error = -ETIMEDOUT;
1499 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
1500 cmd->error = -EILSEQ;
1501 } else if (host->variant->busy_timeout && busy_resp &&
1502 status & MCI_DATATIMEOUT) {
1503 cmd->error = -ETIMEDOUT;
1505 * This will wake up mmci_irq_thread() which will issue
1506 * a hardware reset of the MMCI block.
1508 host->irq_action = IRQ_WAKE_THREAD;
1510 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1511 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1512 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1513 cmd->resp[3] = readl(base + MMCIRESPONSE3);
1516 if ((!sbc && !cmd->data) || cmd->error) {
1518 /* Terminate the DMA transfer */
1519 mmci_dma_error(host);
1521 mmci_stop_data(host);
1522 if (host->variant->cmdreg_stop && cmd->error) {
1523 mmci_stop_command(host);
1528 if (host->irq_action != IRQ_WAKE_THREAD)
1529 mmci_request_end(host, host->mrq);
1532 mmci_start_command(host, host->mrq->cmd, 0);
1533 } else if (!host->variant->datactrl_first &&
1534 !(cmd->data->flags & MMC_DATA_READ)) {
1535 mmci_start_data(host, cmd->data);
1539 static char *ux500_state_str(struct mmci_host *host)
1541 switch (host->busy_state) {
1542 case MMCI_BUSY_WAITING_FOR_START_IRQ:
1543 return "waiting for start IRQ";
1544 case MMCI_BUSY_WAITING_FOR_END_IRQ:
1545 return "waiting for end IRQ";
1546 case MMCI_BUSY_DONE:
1547 return "not waiting for IRQs";
1554 * This busy timeout worker is used to "kick" the command IRQ if a
1555 * busy detect IRQ fails to appear in reasonable time. Only used on
1556 * variants with busy detection IRQ delivery.
1558 static void ux500_busy_timeout_work(struct work_struct *work)
1560 struct mmci_host *host = container_of(work, struct mmci_host,
1561 ux500_busy_timeout_work.work);
1562 unsigned long flags;
1565 spin_lock_irqsave(&host->lock, flags);
1568 /* If we are still busy let's tag on a cmd-timeout error. */
1569 status = readl(host->base + MMCISTATUS);
1570 if (status & host->variant->busy_detect_flag) {
1571 status |= MCI_CMDTIMEOUT;
1572 dev_err(mmc_dev(host->mmc),
1573 "timeout in state %s still busy with CMD%02x\n",
1574 ux500_state_str(host), host->cmd->opcode);
1576 dev_err(mmc_dev(host->mmc),
1577 "timeout in state %s waiting for busy CMD%02x\n",
1578 ux500_state_str(host), host->cmd->opcode);
1581 mmci_cmd_irq(host, host->cmd, status);
1584 spin_unlock_irqrestore(&host->lock, flags);
1587 static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1589 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1592 static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1595 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1596 * from the fifo range should be used
1598 if (status & MCI_RXFIFOHALFFULL)
1599 return host->variant->fifohalfsize;
1600 else if (status & MCI_RXDATAAVLBL)
1606 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1608 void __iomem *base = host->base;
1610 u32 status = readl(host->base + MMCISTATUS);
1611 int host_remain = host->size;
1614 int count = host->get_rx_fifocnt(host, status, host_remain);
1623 * SDIO especially may want to send something that is
1624 * not divisible by 4 (as opposed to card sectors
1625 * etc). Therefore make sure to always read the last bytes
1626 * while only doing full 32-bit reads towards the FIFO.
1628 if (unlikely(count & 0x3)) {
1630 unsigned char buf[4];
1631 ioread32_rep(base + MMCIFIFO, buf, 1);
1632 memcpy(ptr, buf, count);
1634 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
1638 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
1643 host_remain -= count;
1648 status = readl(base + MMCISTATUS);
1649 } while (status & MCI_RXDATAAVLBL);
1651 return ptr - buffer;
1654 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1656 struct variant_data *variant = host->variant;
1657 void __iomem *base = host->base;
1661 unsigned int count, maxcnt;
1663 maxcnt = status & MCI_TXFIFOEMPTY ?
1664 variant->fifosize : variant->fifohalfsize;
1665 count = min(remain, maxcnt);
1668 * SDIO especially may want to send something that is
1669 * not divisible by 4 (as opposed to card sectors
1670 * etc), and the FIFO only accept full 32-bit writes.
1671 * So compensate by adding +3 on the count, a single
1672 * byte become a 32bit write, 7 bytes will be two
1675 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
1683 status = readl(base + MMCISTATUS);
1684 } while (status & MCI_TXFIFOHALFEMPTY);
1686 return ptr - buffer;
1690 * PIO data transfer IRQ handler.
1692 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
1694 struct mmci_host *host = dev_id;
1695 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1696 struct variant_data *variant = host->variant;
1697 void __iomem *base = host->base;
1700 status = readl(base + MMCISTATUS);
1702 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
1705 unsigned int remain, len;
1709 * For write, we only need to test the half-empty flag
1710 * here - if the FIFO is completely empty, then by
1711 * definition it is more than half empty.
1713 * For read, check for data available.
1715 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1718 if (!sg_miter_next(sg_miter))
1721 buffer = sg_miter->addr;
1722 remain = sg_miter->length;
1725 if (status & MCI_RXACTIVE)
1726 len = mmci_pio_read(host, buffer, remain);
1727 if (status & MCI_TXACTIVE)
1728 len = mmci_pio_write(host, buffer, remain, status);
1730 sg_miter->consumed = len;
1738 status = readl(base + MMCISTATUS);
1741 sg_miter_stop(sg_miter);
1744 * If we have less than the fifo 'half-full' threshold to transfer,
1745 * trigger a PIO interrupt as soon as any data is available.
1747 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
1748 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
1751 * If we run out of data, disable the data IRQs; this
1752 * prevents a race where the FIFO becomes empty before
1753 * the chip itself has disabled the data path, and
1754 * stops us racing with our data end IRQ.
1756 if (host->size == 0) {
1757 mmci_set_mask1(host, 0);
1758 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1765 * Handle completion of command and data transfers.
1767 static irqreturn_t mmci_irq(int irq, void *dev_id)
1769 struct mmci_host *host = dev_id;
1772 spin_lock(&host->lock);
1773 host->irq_action = IRQ_HANDLED;
1776 status = readl(host->base + MMCISTATUS);
1780 if (host->singleirq) {
1781 if (status & host->mask1_reg)
1782 mmci_pio_irq(irq, dev_id);
1784 status &= ~host->variant->irq_pio_mask;
1788 * Busy detection is managed by mmci_cmd_irq(), including to
1789 * clear the corresponding IRQ.
1791 status &= readl(host->base + MMCIMASK0);
1792 if (host->variant->busy_detect)
1793 writel(status & ~host->variant->busy_detect_mask,
1794 host->base + MMCICLEAR);
1796 writel(status, host->base + MMCICLEAR);
1798 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
1800 if (host->variant->reversed_irq_handling) {
1801 mmci_data_irq(host, host->data, status);
1802 mmci_cmd_irq(host, host->cmd, status);
1804 mmci_cmd_irq(host, host->cmd, status);
1805 mmci_data_irq(host, host->data, status);
1809 * Busy detection has been handled by mmci_cmd_irq() above.
1810 * Clear the status bit to prevent polling in IRQ context.
1812 if (host->variant->busy_detect_flag)
1813 status &= ~host->variant->busy_detect_flag;
1817 spin_unlock(&host->lock);
1819 return host->irq_action;
1823 * mmci_irq_thread() - A threaded IRQ handler that manages a reset of the HW.
1825 * A reset is needed for some variants, where a datatimeout for a R1B request
1826 * causes the DPSM to stay busy (non-functional).
1828 static irqreturn_t mmci_irq_thread(int irq, void *dev_id)
1830 struct mmci_host *host = dev_id;
1831 unsigned long flags;
1834 reset_control_assert(host->rst);
1836 reset_control_deassert(host->rst);
1839 spin_lock_irqsave(&host->lock, flags);
1840 writel(host->clk_reg, host->base + MMCICLOCK);
1841 writel(host->pwr_reg, host->base + MMCIPOWER);
1842 writel(MCI_IRQENABLE | host->variant->start_err,
1843 host->base + MMCIMASK0);
1845 host->irq_action = IRQ_HANDLED;
1846 mmci_request_end(host, host->mrq);
1847 spin_unlock_irqrestore(&host->lock, flags);
1849 return host->irq_action;
1852 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1854 struct mmci_host *host = mmc_priv(mmc);
1855 unsigned long flags;
1857 WARN_ON(host->mrq != NULL);
1859 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1860 if (mrq->cmd->error) {
1861 mmc_request_done(mmc, mrq);
1865 spin_lock_irqsave(&host->lock, flags);
1870 mmci_get_next_data(host, mrq->data);
1873 (host->variant->datactrl_first || mrq->data->flags & MMC_DATA_READ))
1874 mmci_start_data(host, mrq->data);
1877 mmci_start_command(host, mrq->sbc, 0);
1879 mmci_start_command(host, mrq->cmd, 0);
1881 spin_unlock_irqrestore(&host->lock, flags);
1884 static void mmci_set_max_busy_timeout(struct mmc_host *mmc)
1886 struct mmci_host *host = mmc_priv(mmc);
1887 u32 max_busy_timeout = 0;
1889 if (!host->variant->busy_detect)
1892 if (host->variant->busy_timeout && mmc->actual_clock)
1893 max_busy_timeout = U32_MAX / DIV_ROUND_UP(mmc->actual_clock,
1896 mmc->max_busy_timeout = max_busy_timeout;
1899 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1901 struct mmci_host *host = mmc_priv(mmc);
1902 struct variant_data *variant = host->variant;
1904 unsigned long flags;
1907 switch (ios->power_mode) {
1909 if (!IS_ERR(mmc->supply.vmmc))
1910 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1912 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
1913 regulator_disable(mmc->supply.vqmmc);
1914 host->vqmmc_enabled = false;
1919 if (!IS_ERR(mmc->supply.vmmc))
1920 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1923 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1924 * and instead uses MCI_PWR_ON so apply whatever value is
1925 * configured in the variant data.
1927 pwr |= variant->pwrreg_powerup;
1931 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
1932 ret = regulator_enable(mmc->supply.vqmmc);
1934 dev_err(mmc_dev(mmc),
1935 "failed to enable vqmmc regulator\n");
1937 host->vqmmc_enabled = true;
1944 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1946 * The ST Micro variant has some additional bits
1947 * indicating signal direction for the signals in
1948 * the SD/MMC bus and feedback-clock usage.
1950 pwr |= host->pwr_reg_add;
1952 if (ios->bus_width == MMC_BUS_WIDTH_4)
1953 pwr &= ~MCI_ST_DATA74DIREN;
1954 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1955 pwr &= (~MCI_ST_DATA74DIREN &
1956 ~MCI_ST_DATA31DIREN &
1957 ~MCI_ST_DATA2DIREN);
1960 if (variant->opendrain) {
1961 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1962 pwr |= variant->opendrain;
1965 * If the variant cannot configure the pads by its own, then we
1966 * expect the pinctrl to be able to do that for us
1968 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
1969 pinctrl_select_state(host->pinctrl, host->pins_opendrain);
1971 pinctrl_select_default_state(mmc_dev(mmc));
1975 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1976 * gating the clock, the MCI_PWR_ON bit is cleared.
1978 if (!ios->clock && variant->pwrreg_clkgate)
1981 if (host->variant->explicit_mclk_control &&
1982 ios->clock != host->clock_cache) {
1983 ret = clk_set_rate(host->clk, ios->clock);
1985 dev_err(mmc_dev(host->mmc),
1986 "Error setting clock rate (%d)\n", ret);
1988 host->mclk = clk_get_rate(host->clk);
1990 host->clock_cache = ios->clock;
1992 spin_lock_irqsave(&host->lock, flags);
1994 if (host->ops && host->ops->set_clkreg)
1995 host->ops->set_clkreg(host, ios->clock);
1997 mmci_set_clkreg(host, ios->clock);
1999 mmci_set_max_busy_timeout(mmc);
2001 if (host->ops && host->ops->set_pwrreg)
2002 host->ops->set_pwrreg(host, pwr);
2004 mmci_write_pwrreg(host, pwr);
2006 mmci_reg_delay(host);
2008 spin_unlock_irqrestore(&host->lock, flags);
2011 static int mmci_get_cd(struct mmc_host *mmc)
2013 struct mmci_host *host = mmc_priv(mmc);
2014 struct mmci_platform_data *plat = host->plat;
2015 unsigned int status = mmc_gpio_get_cd(mmc);
2017 if (status == -ENOSYS) {
2019 return 1; /* Assume always present */
2021 status = plat->status(mmc_dev(host->mmc));
2026 static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
2028 struct mmci_host *host = mmc_priv(mmc);
2031 ret = mmc_regulator_set_vqmmc(mmc, ios);
2033 if (!ret && host->ops && host->ops->post_sig_volt_switch)
2034 ret = host->ops->post_sig_volt_switch(host, ios);
2039 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
2044 static struct mmc_host_ops mmci_ops = {
2045 .request = mmci_request,
2046 .pre_req = mmci_pre_request,
2047 .post_req = mmci_post_request,
2048 .set_ios = mmci_set_ios,
2049 .get_ro = mmc_gpio_get_ro,
2050 .get_cd = mmci_get_cd,
2051 .start_signal_voltage_switch = mmci_sig_volt_switch,
2054 static void mmci_probe_level_translator(struct mmc_host *mmc)
2056 struct device *dev = mmc_dev(mmc);
2057 struct mmci_host *host = mmc_priv(mmc);
2058 struct gpio_desc *cmd_gpio;
2059 struct gpio_desc *ck_gpio;
2060 struct gpio_desc *ckin_gpio;
2064 * Assume the level translator is present if st,use-ckin is set.
2065 * This is to cater for DTs which do not implement this test.
2067 host->clk_reg_add |= MCI_STM32_CLK_SELCKIN;
2069 cmd_gpio = gpiod_get(dev, "st,cmd", GPIOD_OUT_HIGH);
2070 if (IS_ERR(cmd_gpio))
2073 ck_gpio = gpiod_get(dev, "st,ck", GPIOD_OUT_HIGH);
2074 if (IS_ERR(ck_gpio))
2077 ckin_gpio = gpiod_get(dev, "st,ckin", GPIOD_IN);
2078 if (IS_ERR(ckin_gpio))
2081 /* All GPIOs are valid, test whether level translator works */
2084 clk_hi = !!gpiod_get_value(ckin_gpio);
2087 gpiod_set_value(ck_gpio, 0);
2090 clk_lo = !!gpiod_get_value(ckin_gpio);
2093 gpiod_direction_input(cmd_gpio);
2094 gpiod_direction_input(ck_gpio);
2096 /* Level translator is present if CK signal is propagated to CKIN */
2097 if (!clk_hi || clk_lo) {
2098 host->clk_reg_add &= ~MCI_STM32_CLK_SELCKIN;
2100 "Level translator inoperable, CK signal not detected on CKIN, disabling.\n");
2103 gpiod_put(ckin_gpio);
2108 gpiod_put(cmd_gpio);
2110 pinctrl_select_default_state(dev);
2113 static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
2115 struct mmci_host *host = mmc_priv(mmc);
2116 int ret = mmc_of_parse(mmc);
2121 if (of_property_read_bool(np, "st,sig-dir-dat0"))
2122 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
2123 if (of_property_read_bool(np, "st,sig-dir-dat2"))
2124 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
2125 if (of_property_read_bool(np, "st,sig-dir-dat31"))
2126 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
2127 if (of_property_read_bool(np, "st,sig-dir-dat74"))
2128 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
2129 if (of_property_read_bool(np, "st,sig-dir-cmd"))
2130 host->pwr_reg_add |= MCI_ST_CMDDIREN;
2131 if (of_property_read_bool(np, "st,sig-pin-fbclk"))
2132 host->pwr_reg_add |= MCI_ST_FBCLKEN;
2133 if (of_property_read_bool(np, "st,sig-dir"))
2134 host->pwr_reg_add |= MCI_STM32_DIRPOL;
2135 if (of_property_read_bool(np, "st,neg-edge"))
2136 host->clk_reg_add |= MCI_STM32_CLK_NEGEDGE;
2137 if (of_property_read_bool(np, "st,use-ckin"))
2138 mmci_probe_level_translator(mmc);
2140 if (of_property_read_bool(np, "mmc-cap-mmc-highspeed"))
2141 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
2142 if (of_property_read_bool(np, "mmc-cap-sd-highspeed"))
2143 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
2148 static int mmci_probe(struct amba_device *dev,
2149 const struct amba_id *id)
2151 struct mmci_platform_data *plat = dev->dev.platform_data;
2152 struct device_node *np = dev->dev.of_node;
2153 struct variant_data *variant = id->data;
2154 struct mmci_host *host;
2155 struct mmc_host *mmc;
2158 /* Must have platform data or Device Tree. */
2160 dev_err(&dev->dev, "No plat data or DT found\n");
2165 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
2170 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
2174 host = mmc_priv(mmc);
2176 host->mmc_ops = &mmci_ops;
2177 mmc->ops = &mmci_ops;
2179 ret = mmci_of_parse(np, mmc);
2184 * Some variant (STM32) doesn't have opendrain bit, nevertheless
2185 * pins can be set accordingly using pinctrl
2187 if (!variant->opendrain) {
2188 host->pinctrl = devm_pinctrl_get(&dev->dev);
2189 if (IS_ERR(host->pinctrl)) {
2190 dev_err(&dev->dev, "failed to get pinctrl");
2191 ret = PTR_ERR(host->pinctrl);
2195 host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
2196 MMCI_PINCTRL_STATE_OPENDRAIN);
2197 if (IS_ERR(host->pins_opendrain)) {
2198 dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
2199 ret = PTR_ERR(host->pins_opendrain);
2204 host->hw_designer = amba_manf(dev);
2205 host->hw_revision = amba_rev(dev);
2206 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
2207 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
2209 host->clk = devm_clk_get(&dev->dev, NULL);
2210 if (IS_ERR(host->clk)) {
2211 ret = PTR_ERR(host->clk);
2215 ret = clk_prepare_enable(host->clk);
2219 if (variant->qcom_fifo)
2220 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
2222 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
2225 host->variant = variant;
2226 host->mclk = clk_get_rate(host->clk);
2228 * According to the spec, mclk is max 100 MHz,
2229 * so we try to adjust the clock down to this,
2232 if (host->mclk > variant->f_max) {
2233 ret = clk_set_rate(host->clk, variant->f_max);
2236 host->mclk = clk_get_rate(host->clk);
2237 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
2241 host->phybase = dev->res.start;
2242 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
2243 if (IS_ERR(host->base)) {
2244 ret = PTR_ERR(host->base);
2249 variant->init(host);
2252 * The ARM and ST versions of the block have slightly different
2253 * clock divider equations which means that the minimum divider
2255 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
2257 if (variant->st_clkdiv)
2258 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
2259 else if (variant->stm32_clkdiv)
2260 mmc->f_min = DIV_ROUND_UP(host->mclk, 2046);
2261 else if (variant->explicit_mclk_control)
2262 mmc->f_min = clk_round_rate(host->clk, 100000);
2264 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
2266 * If no maximum operating frequency is supplied, fall back to use
2267 * the module parameter, which has a (low) default value in case it
2268 * is not specified. Either value must not exceed the clock rate into
2269 * the block, of course.
2272 mmc->f_max = variant->explicit_mclk_control ?
2273 min(variant->f_max, mmc->f_max) :
2274 min(host->mclk, mmc->f_max);
2276 mmc->f_max = variant->explicit_mclk_control ?
2277 fmax : min(host->mclk, fmax);
2280 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
2282 host->rst = devm_reset_control_get_optional_exclusive(&dev->dev, NULL);
2283 if (IS_ERR(host->rst)) {
2284 ret = PTR_ERR(host->rst);
2287 ret = reset_control_deassert(host->rst);
2289 dev_err(mmc_dev(mmc), "failed to de-assert reset\n");
2291 /* Get regulators and the supported OCR mask */
2292 ret = mmc_regulator_get_supply(mmc);
2296 if (!mmc->ocr_avail)
2297 mmc->ocr_avail = plat->ocr_mask;
2298 else if (plat->ocr_mask)
2299 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
2301 /* We support these capabilities. */
2302 mmc->caps |= MMC_CAP_CMD23;
2305 * Enable busy detection.
2307 if (variant->busy_detect) {
2308 mmci_ops.card_busy = mmci_card_busy;
2310 * Not all variants have a flag to enable busy detection
2311 * in the DPSM, but if they do, set it here.
2313 if (variant->busy_dpsm_flag)
2314 mmci_write_datactrlreg(host,
2315 host->variant->busy_dpsm_flag);
2316 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
2319 /* Variants with mandatory busy timeout in HW needs R1B responses. */
2320 if (variant->busy_timeout)
2321 mmc->caps |= MMC_CAP_NEED_RSP_BUSY;
2323 /* Prepare a CMD12 - needed to clear the DPSM on some variants. */
2324 host->stop_abort.opcode = MMC_STOP_TRANSMISSION;
2325 host->stop_abort.arg = 0;
2326 host->stop_abort.flags = MMC_RSP_R1B | MMC_CMD_AC;
2328 /* We support these PM capabilities. */
2329 mmc->pm_caps |= MMC_PM_KEEP_POWER;
2334 mmc->max_segs = NR_SG;
2337 * Since only a certain number of bits are valid in the data length
2338 * register, we must ensure that we don't exceed 2^num-1 bytes in a
2341 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
2344 * Set the maximum segment size. Since we aren't doing DMA
2345 * (yet) we are only limited by the data length register.
2347 mmc->max_seg_size = mmc->max_req_size;
2350 * Block size can be up to 2048 bytes, but must be a power of two.
2352 mmc->max_blk_size = 1 << variant->datactrl_blocksz;
2355 * Limit the number of blocks transferred so that we don't overflow
2356 * the maximum request size.
2358 mmc->max_blk_count = mmc->max_req_size >> variant->datactrl_blocksz;
2360 spin_lock_init(&host->lock);
2362 writel(0, host->base + MMCIMASK0);
2364 if (variant->mmcimask1)
2365 writel(0, host->base + MMCIMASK1);
2367 writel(0xfff, host->base + MMCICLEAR);
2371 * - not using DT but using a descriptor table, or
2372 * - using a table of descriptors ALONGSIDE DT, or
2373 * look up these descriptors named "cd" and "wp" right here, fail
2374 * silently of these do not exist
2377 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0);
2378 if (ret == -EPROBE_DEFER)
2381 ret = mmc_gpiod_request_ro(mmc, "wp", 0, 0);
2382 if (ret == -EPROBE_DEFER)
2386 ret = devm_request_threaded_irq(&dev->dev, dev->irq[0], mmci_irq,
2387 mmci_irq_thread, IRQF_SHARED,
2388 DRIVER_NAME " (cmd)", host);
2393 host->singleirq = true;
2395 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
2396 IRQF_SHARED, DRIVER_NAME " (pio)", host);
2401 if (host->variant->busy_detect)
2402 INIT_DELAYED_WORK(&host->ux500_busy_timeout_work,
2403 ux500_busy_timeout_work);
2405 writel(MCI_IRQENABLE | variant->start_err, host->base + MMCIMASK0);
2407 amba_set_drvdata(dev, mmc);
2409 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
2410 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
2411 amba_rev(dev), (unsigned long long)dev->res.start,
2412 dev->irq[0], dev->irq[1]);
2414 mmci_dma_setup(host);
2416 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
2417 pm_runtime_use_autosuspend(&dev->dev);
2419 ret = mmc_add_host(mmc);
2423 pm_runtime_put(&dev->dev);
2427 clk_disable_unprepare(host->clk);
2433 static void mmci_remove(struct amba_device *dev)
2435 struct mmc_host *mmc = amba_get_drvdata(dev);
2438 struct mmci_host *host = mmc_priv(mmc);
2439 struct variant_data *variant = host->variant;
2442 * Undo pm_runtime_put() in probe. We use the _sync
2443 * version here so that we can access the primecell.
2445 pm_runtime_get_sync(&dev->dev);
2447 mmc_remove_host(mmc);
2449 writel(0, host->base + MMCIMASK0);
2451 if (variant->mmcimask1)
2452 writel(0, host->base + MMCIMASK1);
2454 writel(0, host->base + MMCICOMMAND);
2455 writel(0, host->base + MMCIDATACTRL);
2457 mmci_dma_release(host);
2458 clk_disable_unprepare(host->clk);
2464 static void mmci_save(struct mmci_host *host)
2466 unsigned long flags;
2468 spin_lock_irqsave(&host->lock, flags);
2470 writel(0, host->base + MMCIMASK0);
2471 if (host->variant->pwrreg_nopower) {
2472 writel(0, host->base + MMCIDATACTRL);
2473 writel(0, host->base + MMCIPOWER);
2474 writel(0, host->base + MMCICLOCK);
2476 mmci_reg_delay(host);
2478 spin_unlock_irqrestore(&host->lock, flags);
2481 static void mmci_restore(struct mmci_host *host)
2483 unsigned long flags;
2485 spin_lock_irqsave(&host->lock, flags);
2487 if (host->variant->pwrreg_nopower) {
2488 writel(host->clk_reg, host->base + MMCICLOCK);
2489 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
2490 writel(host->pwr_reg, host->base + MMCIPOWER);
2492 writel(MCI_IRQENABLE | host->variant->start_err,
2493 host->base + MMCIMASK0);
2494 mmci_reg_delay(host);
2496 spin_unlock_irqrestore(&host->lock, flags);
2499 static int mmci_runtime_suspend(struct device *dev)
2501 struct amba_device *adev = to_amba_device(dev);
2502 struct mmc_host *mmc = amba_get_drvdata(adev);
2505 struct mmci_host *host = mmc_priv(mmc);
2506 pinctrl_pm_select_sleep_state(dev);
2508 clk_disable_unprepare(host->clk);
2514 static int mmci_runtime_resume(struct device *dev)
2516 struct amba_device *adev = to_amba_device(dev);
2517 struct mmc_host *mmc = amba_get_drvdata(adev);
2520 struct mmci_host *host = mmc_priv(mmc);
2521 clk_prepare_enable(host->clk);
2523 pinctrl_select_default_state(dev);
2530 static const struct dev_pm_ops mmci_dev_pm_ops = {
2531 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2532 pm_runtime_force_resume)
2533 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
2536 static const struct amba_id mmci_ids[] = {
2540 .data = &variant_arm,
2545 .data = &variant_arm_extended_fifo,
2550 .data = &variant_arm_extended_fifo_hwfc,
2555 .data = &variant_arm,
2557 /* ST Micro variants */
2561 .data = &variant_u300,
2566 .data = &variant_nomadik,
2571 .data = &variant_nomadik,
2576 .data = &variant_ux500,
2581 .data = &variant_ux500v2,
2586 .data = &variant_stm32,
2591 .data = &variant_stm32_sdmmc,
2596 .data = &variant_stm32_sdmmcv2,
2601 .data = &variant_stm32_sdmmcv2,
2606 .data = &variant_stm32_sdmmcv3,
2608 /* Qualcomm variants */
2612 .data = &variant_qcom,
2617 MODULE_DEVICE_TABLE(amba, mmci_ids);
2619 static struct amba_driver mmci_driver = {
2621 .name = DRIVER_NAME,
2622 .pm = &mmci_dev_pm_ops,
2623 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2625 .probe = mmci_probe,
2626 .remove = mmci_remove,
2627 .id_table = mmci_ids,
2630 module_amba_driver(mmci_driver);
2632 module_param(fmax, uint, 0444);
2634 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2635 MODULE_LICENSE("GPL");