2 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
5 * Copyright (C) 2010 ST-Ericsson SA
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/highmem.h>
23 #include <linux/log2.h>
24 #include <linux/mmc/pm.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/slot-gpio.h>
28 #include <linux/amba/bus.h>
29 #include <linux/clk.h>
30 #include <linux/scatterlist.h>
31 #include <linux/gpio.h>
32 #include <linux/of_gpio.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/dmaengine.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/amba/mmci.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/types.h>
39 #include <linux/pinctrl/consumer.h>
41 #include <asm/div64.h>
45 #include "mmci_qcom_dml.h"
47 #define DRIVER_NAME "mmci-pl18x"
49 static unsigned int fmax = 515633;
52 * struct variant_data - MMCI variant-specific quirks
53 * @clkreg: default value for MCICLOCK register
54 * @clkreg_enable: enable value for MMCICLOCK register
55 * @clkreg_8bit_bus_enable: enable value for 8 bit bus
56 * @clkreg_neg_edge_enable: enable value for inverted data/cmd output
57 * @datalength_bits: number of bits in the MMCIDATALENGTH register
58 * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
59 * is asserted (likewise for RX)
60 * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
61 * is asserted (likewise for RX)
62 * @data_cmd_enable: enable value for data commands.
63 * @st_sdio: enable ST specific SDIO logic
64 * @st_clkdiv: true if using a ST-specific clock divider algorithm
65 * @datactrl_mask_ddrmode: ddr mode mask in datactrl register.
66 * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
67 * @blksz_datactrl4: true if Block size is at b4..b16 position in datactrl
69 * @datactrl_mask_sdio: SDIO enable mask in datactrl register
70 * @pwrreg_powerup: power up value for MMCIPOWER register
71 * @f_max: maximum clk frequency supported by the controller.
72 * @signal_direction: input/out direction of bus signals can be indicated
73 * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
74 * @busy_detect: true if the variant supports busy detection on DAT0.
75 * @busy_dpsm_flag: bitmask enabling busy detection in the DPSM
76 * @busy_detect_flag: bitmask identifying the bit in the MMCISTATUS register
77 * indicating that the card is busy
78 * @busy_detect_mask: bitmask identifying the bit in the MMCIMASK0 to mask for
79 * getting busy end detection interrupts
80 * @pwrreg_nopower: bits in MMCIPOWER don't controls ext. power supply
81 * @explicit_mclk_control: enable explicit mclk control in driver.
82 * @qcom_fifo: enables qcom specific fifo pio read logic.
83 * @qcom_dml: enables qcom specific dma glue for dma transfers.
84 * @reversed_irq_handling: handle data irq before cmd irq.
85 * @mmcimask1: true if variant have a MMCIMASK1 register.
89 unsigned int clkreg_enable;
90 unsigned int clkreg_8bit_bus_enable;
91 unsigned int clkreg_neg_edge_enable;
92 unsigned int datalength_bits;
93 unsigned int fifosize;
94 unsigned int fifohalfsize;
95 unsigned int data_cmd_enable;
96 unsigned int datactrl_mask_ddrmode;
97 unsigned int datactrl_mask_sdio;
100 bool blksz_datactrl16;
101 bool blksz_datactrl4;
104 bool signal_direction;
108 u32 busy_detect_flag;
109 u32 busy_detect_mask;
111 bool explicit_mclk_control;
114 bool reversed_irq_handling;
118 static struct variant_data variant_arm = {
120 .fifohalfsize = 8 * 4,
121 .datalength_bits = 16,
122 .pwrreg_powerup = MCI_PWR_UP,
124 .reversed_irq_handling = true,
128 static struct variant_data variant_arm_extended_fifo = {
130 .fifohalfsize = 64 * 4,
131 .datalength_bits = 16,
132 .pwrreg_powerup = MCI_PWR_UP,
137 static struct variant_data variant_arm_extended_fifo_hwfc = {
139 .fifohalfsize = 64 * 4,
140 .clkreg_enable = MCI_ARM_HWFCEN,
141 .datalength_bits = 16,
142 .pwrreg_powerup = MCI_PWR_UP,
147 static struct variant_data variant_u300 = {
149 .fifohalfsize = 8 * 4,
150 .clkreg_enable = MCI_ST_U300_HWFCEN,
151 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
152 .datalength_bits = 16,
153 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
155 .pwrreg_powerup = MCI_PWR_ON,
157 .signal_direction = true,
158 .pwrreg_clkgate = true,
159 .pwrreg_nopower = true,
163 static struct variant_data variant_nomadik = {
165 .fifohalfsize = 8 * 4,
166 .clkreg = MCI_CLK_ENABLE,
167 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
168 .datalength_bits = 24,
169 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
172 .pwrreg_powerup = MCI_PWR_ON,
174 .signal_direction = true,
175 .pwrreg_clkgate = true,
176 .pwrreg_nopower = true,
180 static struct variant_data variant_ux500 = {
182 .fifohalfsize = 8 * 4,
183 .clkreg = MCI_CLK_ENABLE,
184 .clkreg_enable = MCI_ST_UX500_HWFCEN,
185 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
186 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
187 .datalength_bits = 24,
188 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
191 .pwrreg_powerup = MCI_PWR_ON,
193 .signal_direction = true,
194 .pwrreg_clkgate = true,
196 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
197 .busy_detect_flag = MCI_ST_CARDBUSY,
198 .busy_detect_mask = MCI_ST_BUSYENDMASK,
199 .pwrreg_nopower = true,
203 static struct variant_data variant_ux500v2 = {
205 .fifohalfsize = 8 * 4,
206 .clkreg = MCI_CLK_ENABLE,
207 .clkreg_enable = MCI_ST_UX500_HWFCEN,
208 .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
209 .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
210 .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
211 .datalength_bits = 24,
212 .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
215 .blksz_datactrl16 = true,
216 .pwrreg_powerup = MCI_PWR_ON,
218 .signal_direction = true,
219 .pwrreg_clkgate = true,
221 .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
222 .busy_detect_flag = MCI_ST_CARDBUSY,
223 .busy_detect_mask = MCI_ST_BUSYENDMASK,
224 .pwrreg_nopower = true,
228 static struct variant_data variant_qcom = {
230 .fifohalfsize = 8 * 4,
231 .clkreg = MCI_CLK_ENABLE,
232 .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
233 MCI_QCOM_CLK_SELECT_IN_FBCLK,
234 .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
235 .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
236 .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
237 .blksz_datactrl4 = true,
238 .datalength_bits = 24,
239 .pwrreg_powerup = MCI_PWR_UP,
241 .explicit_mclk_control = true,
247 /* Busy detection for the ST Micro variant */
248 static int mmci_card_busy(struct mmc_host *mmc)
250 struct mmci_host *host = mmc_priv(mmc);
254 spin_lock_irqsave(&host->lock, flags);
255 if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
257 spin_unlock_irqrestore(&host->lock, flags);
263 * Validate mmc prerequisites
265 static int mmci_validate_data(struct mmci_host *host,
266 struct mmc_data *data)
271 if (!is_power_of_2(data->blksz)) {
272 dev_err(mmc_dev(host->mmc),
273 "unsupported block size (%d bytes)\n", data->blksz);
280 static void mmci_reg_delay(struct mmci_host *host)
283 * According to the spec, at least three feedback clock cycles
284 * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
285 * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
286 * Worst delay time during card init is at 100 kHz => 30 us.
287 * Worst delay time when up and running is at 25 MHz => 120 ns.
289 if (host->cclk < 25000000)
296 * This must be called with host->lock held
298 static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
300 if (host->clk_reg != clk) {
302 writel(clk, host->base + MMCICLOCK);
307 * This must be called with host->lock held
309 static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
311 if (host->pwr_reg != pwr) {
313 writel(pwr, host->base + MMCIPOWER);
318 * This must be called with host->lock held
320 static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
322 /* Keep busy mode in DPSM if enabled */
323 datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
325 if (host->datactrl_reg != datactrl) {
326 host->datactrl_reg = datactrl;
327 writel(datactrl, host->base + MMCIDATACTRL);
332 * This must be called with host->lock held
334 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
336 struct variant_data *variant = host->variant;
337 u32 clk = variant->clkreg;
339 /* Make sure cclk reflects the current calculated clock */
343 if (variant->explicit_mclk_control) {
344 host->cclk = host->mclk;
345 } else if (desired >= host->mclk) {
346 clk = MCI_CLK_BYPASS;
347 if (variant->st_clkdiv)
348 clk |= MCI_ST_UX500_NEG_EDGE;
349 host->cclk = host->mclk;
350 } else if (variant->st_clkdiv) {
352 * DB8500 TRM says f = mclk / (clkdiv + 2)
353 * => clkdiv = (mclk / f) - 2
354 * Round the divider up so we don't exceed the max
357 clk = DIV_ROUND_UP(host->mclk, desired) - 2;
360 host->cclk = host->mclk / (clk + 2);
363 * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
364 * => clkdiv = mclk / (2 * f) - 1
366 clk = host->mclk / (2 * desired) - 1;
369 host->cclk = host->mclk / (2 * (clk + 1));
372 clk |= variant->clkreg_enable;
373 clk |= MCI_CLK_ENABLE;
374 /* This hasn't proven to be worthwhile */
375 /* clk |= MCI_CLK_PWRSAVE; */
378 /* Set actual clock for debug */
379 host->mmc->actual_clock = host->cclk;
381 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
383 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
384 clk |= variant->clkreg_8bit_bus_enable;
386 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
387 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
388 clk |= variant->clkreg_neg_edge_enable;
390 mmci_write_clkreg(host, clk);
394 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
396 writel(0, host->base + MMCICOMMAND);
403 mmc_request_done(host->mmc, mrq);
406 static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
408 void __iomem *base = host->base;
409 struct variant_data *variant = host->variant;
411 if (host->singleirq) {
412 unsigned int mask0 = readl(base + MMCIMASK0);
414 mask0 &= ~MCI_IRQ1MASK;
417 writel(mask0, base + MMCIMASK0);
420 if (variant->mmcimask1)
421 writel(mask, base + MMCIMASK1);
423 host->mask1_reg = mask;
426 static void mmci_stop_data(struct mmci_host *host)
428 mmci_write_datactrlreg(host, 0);
429 mmci_set_mask1(host, 0);
433 static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
435 unsigned int flags = SG_MITER_ATOMIC;
437 if (data->flags & MMC_DATA_READ)
438 flags |= SG_MITER_TO_SG;
440 flags |= SG_MITER_FROM_SG;
442 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
446 * All the DMA operation mode stuff goes inside this ifdef.
447 * This assumes that you have a generic DMA device interface,
448 * no custom DMA interfaces are supported.
450 #ifdef CONFIG_DMA_ENGINE
451 static void mmci_dma_setup(struct mmci_host *host)
453 const char *rxname, *txname;
454 struct variant_data *variant = host->variant;
456 host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
457 host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
459 /* initialize pre request cookie */
460 host->next_data.cookie = 1;
463 * If only an RX channel is specified, the driver will
464 * attempt to use it bidirectionally, however if it is
465 * is specified but cannot be located, DMA will be disabled.
467 if (host->dma_rx_channel && !host->dma_tx_channel)
468 host->dma_tx_channel = host->dma_rx_channel;
470 if (host->dma_rx_channel)
471 rxname = dma_chan_name(host->dma_rx_channel);
475 if (host->dma_tx_channel)
476 txname = dma_chan_name(host->dma_tx_channel);
480 dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
484 * Limit the maximum segment size in any SG entry according to
485 * the parameters of the DMA engine device.
487 if (host->dma_tx_channel) {
488 struct device *dev = host->dma_tx_channel->device->dev;
489 unsigned int max_seg_size = dma_get_max_seg_size(dev);
491 if (max_seg_size < host->mmc->max_seg_size)
492 host->mmc->max_seg_size = max_seg_size;
494 if (host->dma_rx_channel) {
495 struct device *dev = host->dma_rx_channel->device->dev;
496 unsigned int max_seg_size = dma_get_max_seg_size(dev);
498 if (max_seg_size < host->mmc->max_seg_size)
499 host->mmc->max_seg_size = max_seg_size;
502 if (variant->qcom_dml && host->dma_rx_channel && host->dma_tx_channel)
503 if (dml_hw_init(host, host->mmc->parent->of_node))
504 variant->qcom_dml = false;
508 * This is used in or so inline it
509 * so it can be discarded.
511 static inline void mmci_dma_release(struct mmci_host *host)
513 if (host->dma_rx_channel)
514 dma_release_channel(host->dma_rx_channel);
515 if (host->dma_tx_channel)
516 dma_release_channel(host->dma_tx_channel);
517 host->dma_rx_channel = host->dma_tx_channel = NULL;
520 static void mmci_dma_data_error(struct mmci_host *host)
522 dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
523 dmaengine_terminate_all(host->dma_current);
524 host->dma_in_progress = false;
525 host->dma_current = NULL;
526 host->dma_desc_current = NULL;
527 host->data->host_cookie = 0;
530 static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
532 struct dma_chan *chan;
534 if (data->flags & MMC_DATA_READ)
535 chan = host->dma_rx_channel;
537 chan = host->dma_tx_channel;
539 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
540 mmc_get_dma_dir(data));
543 static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
548 /* Wait up to 1ms for the DMA to complete */
550 status = readl(host->base + MMCISTATUS);
551 if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
557 * Check to see whether we still have some data left in the FIFO -
558 * this catches DMA controllers which are unable to monitor the
559 * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
560 * contiguous buffers. On TX, we'll get a FIFO underrun error.
562 if (status & MCI_RXDATAAVLBLMASK) {
563 mmci_dma_data_error(host);
568 if (!data->host_cookie)
569 mmci_dma_unmap(host, data);
572 * Use of DMA with scatter-gather is impossible.
573 * Give up with DMA and switch back to PIO mode.
575 if (status & MCI_RXDATAAVLBLMASK) {
576 dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
577 mmci_dma_release(host);
580 host->dma_in_progress = false;
581 host->dma_current = NULL;
582 host->dma_desc_current = NULL;
585 /* prepares DMA channel and DMA descriptor, returns non-zero on failure */
586 static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
587 struct dma_chan **dma_chan,
588 struct dma_async_tx_descriptor **dma_desc)
590 struct variant_data *variant = host->variant;
591 struct dma_slave_config conf = {
592 .src_addr = host->phybase + MMCIFIFO,
593 .dst_addr = host->phybase + MMCIFIFO,
594 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
595 .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
596 .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
597 .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
600 struct dma_chan *chan;
601 struct dma_device *device;
602 struct dma_async_tx_descriptor *desc;
604 unsigned long flags = DMA_CTRL_ACK;
606 if (data->flags & MMC_DATA_READ) {
607 conf.direction = DMA_DEV_TO_MEM;
608 chan = host->dma_rx_channel;
610 conf.direction = DMA_MEM_TO_DEV;
611 chan = host->dma_tx_channel;
614 /* If there's no DMA channel, fall back to PIO */
618 /* If less than or equal to the fifo size, don't bother with DMA */
619 if (data->blksz * data->blocks <= variant->fifosize)
622 device = chan->device;
623 nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
624 mmc_get_dma_dir(data));
628 if (host->variant->qcom_dml)
629 flags |= DMA_PREP_INTERRUPT;
631 dmaengine_slave_config(chan, &conf);
632 desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
633 conf.direction, flags);
643 dma_unmap_sg(device->dev, data->sg, data->sg_len,
644 mmc_get_dma_dir(data));
648 static inline int mmci_dma_prep_data(struct mmci_host *host,
649 struct mmc_data *data)
651 /* Check if next job is already prepared. */
652 if (host->dma_current && host->dma_desc_current)
655 /* No job were prepared thus do it now. */
656 return __mmci_dma_prep_data(host, data, &host->dma_current,
657 &host->dma_desc_current);
660 static inline int mmci_dma_prep_next(struct mmci_host *host,
661 struct mmc_data *data)
663 struct mmci_host_next *nd = &host->next_data;
664 return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
667 static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
670 struct mmc_data *data = host->data;
672 ret = mmci_dma_prep_data(host, host->data);
676 /* Okay, go for it. */
677 dev_vdbg(mmc_dev(host->mmc),
678 "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
679 data->sg_len, data->blksz, data->blocks, data->flags);
680 host->dma_in_progress = true;
681 dmaengine_submit(host->dma_desc_current);
682 dma_async_issue_pending(host->dma_current);
684 if (host->variant->qcom_dml)
685 dml_start_xfer(host, data);
687 datactrl |= MCI_DPSM_DMAENABLE;
689 /* Trigger the DMA transfer */
690 mmci_write_datactrlreg(host, datactrl);
693 * Let the MMCI say when the data is ended and it's time
694 * to fire next DMA request. When that happens, MMCI will
695 * call mmci_data_end()
697 writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
698 host->base + MMCIMASK0);
702 static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
704 struct mmci_host_next *next = &host->next_data;
706 WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
707 WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
709 host->dma_desc_current = next->dma_desc;
710 host->dma_current = next->dma_chan;
711 next->dma_desc = NULL;
712 next->dma_chan = NULL;
715 static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
717 struct mmci_host *host = mmc_priv(mmc);
718 struct mmc_data *data = mrq->data;
719 struct mmci_host_next *nd = &host->next_data;
724 BUG_ON(data->host_cookie);
726 if (mmci_validate_data(host, data))
729 if (!mmci_dma_prep_next(host, data))
730 data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
733 static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
736 struct mmci_host *host = mmc_priv(mmc);
737 struct mmc_data *data = mrq->data;
739 if (!data || !data->host_cookie)
742 mmci_dma_unmap(host, data);
745 struct mmci_host_next *next = &host->next_data;
746 struct dma_chan *chan;
747 if (data->flags & MMC_DATA_READ)
748 chan = host->dma_rx_channel;
750 chan = host->dma_tx_channel;
751 dmaengine_terminate_all(chan);
753 if (host->dma_desc_current == next->dma_desc)
754 host->dma_desc_current = NULL;
756 if (host->dma_current == next->dma_chan) {
757 host->dma_in_progress = false;
758 host->dma_current = NULL;
761 next->dma_desc = NULL;
762 next->dma_chan = NULL;
763 data->host_cookie = 0;
768 /* Blank functions if the DMA engine is not available */
769 static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
772 static inline void mmci_dma_setup(struct mmci_host *host)
776 static inline void mmci_dma_release(struct mmci_host *host)
780 static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
784 static inline void mmci_dma_finalize(struct mmci_host *host,
785 struct mmc_data *data)
789 static inline void mmci_dma_data_error(struct mmci_host *host)
793 static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
798 #define mmci_pre_request NULL
799 #define mmci_post_request NULL
803 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
805 struct variant_data *variant = host->variant;
806 unsigned int datactrl, timeout, irqmask;
807 unsigned long long clks;
811 dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
812 data->blksz, data->blocks, data->flags);
815 host->size = data->blksz * data->blocks;
816 data->bytes_xfered = 0;
818 clks = (unsigned long long)data->timeout_ns * host->cclk;
819 do_div(clks, NSEC_PER_SEC);
821 timeout = data->timeout_clks + (unsigned int)clks;
824 writel(timeout, base + MMCIDATATIMER);
825 writel(host->size, base + MMCIDATALENGTH);
827 blksz_bits = ffs(data->blksz) - 1;
828 BUG_ON(1 << blksz_bits != data->blksz);
830 if (variant->blksz_datactrl16)
831 datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
832 else if (variant->blksz_datactrl4)
833 datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
835 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
837 if (data->flags & MMC_DATA_READ)
838 datactrl |= MCI_DPSM_DIRECTION;
840 if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
843 datactrl |= variant->datactrl_mask_sdio;
846 * The ST Micro variant for SDIO small write transfers
847 * needs to have clock H/W flow control disabled,
848 * otherwise the transfer will not start. The threshold
849 * depends on the rate of MCLK.
851 if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
853 (host->size <= 8 && host->mclk > 50000000)))
854 clk = host->clk_reg & ~variant->clkreg_enable;
856 clk = host->clk_reg | variant->clkreg_enable;
858 mmci_write_clkreg(host, clk);
861 if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
862 host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
863 datactrl |= variant->datactrl_mask_ddrmode;
866 * Attempt to use DMA operation mode, if this
867 * should fail, fall back to PIO mode
869 if (!mmci_dma_start_data(host, datactrl))
872 /* IRQ mode, map the SG list for CPU reading/writing */
873 mmci_init_sg(host, data);
875 if (data->flags & MMC_DATA_READ) {
876 irqmask = MCI_RXFIFOHALFFULLMASK;
879 * If we have less than the fifo 'half-full' threshold to
880 * transfer, trigger a PIO interrupt as soon as any data
883 if (host->size < variant->fifohalfsize)
884 irqmask |= MCI_RXDATAAVLBLMASK;
887 * We don't actually need to include "FIFO empty" here
888 * since its implicit in "FIFO half empty".
890 irqmask = MCI_TXFIFOHALFEMPTYMASK;
893 mmci_write_datactrlreg(host, datactrl);
894 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
895 mmci_set_mask1(host, irqmask);
899 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
901 void __iomem *base = host->base;
903 dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
904 cmd->opcode, cmd->arg, cmd->flags);
906 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
907 writel(0, base + MMCICOMMAND);
908 mmci_reg_delay(host);
911 c |= cmd->opcode | MCI_CPSM_ENABLE;
912 if (cmd->flags & MMC_RSP_PRESENT) {
913 if (cmd->flags & MMC_RSP_136)
914 c |= MCI_CPSM_LONGRSP;
915 c |= MCI_CPSM_RESPONSE;
918 c |= MCI_CPSM_INTERRUPT;
920 if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
921 c |= host->variant->data_cmd_enable;
925 writel(cmd->arg, base + MMCIARGUMENT);
926 writel(c, base + MMCICOMMAND);
930 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
933 /* Make sure we have data to handle */
937 /* First check for errors */
938 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_STARTBITERR|
939 MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
942 /* Terminate the DMA transfer */
943 if (dma_inprogress(host)) {
944 mmci_dma_data_error(host);
945 mmci_dma_unmap(host, data);
949 * Calculate how far we are into the transfer. Note that
950 * the data counter gives the number of bytes transferred
951 * on the MMC bus, not on the host side. On reads, this
952 * can be as much as a FIFO-worth of data ahead. This
953 * matters for FIFO overruns only.
955 remain = readl(host->base + MMCIDATACNT);
956 success = data->blksz * data->blocks - remain;
958 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
960 if (status & MCI_DATACRCFAIL) {
961 /* Last block was not successful */
963 data->error = -EILSEQ;
964 } else if (status & MCI_DATATIMEOUT) {
965 data->error = -ETIMEDOUT;
966 } else if (status & MCI_STARTBITERR) {
967 data->error = -ECOMM;
968 } else if (status & MCI_TXUNDERRUN) {
970 } else if (status & MCI_RXOVERRUN) {
971 if (success > host->variant->fifosize)
972 success -= host->variant->fifosize;
977 data->bytes_xfered = round_down(success, data->blksz);
980 if (status & MCI_DATABLOCKEND)
981 dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
983 if (status & MCI_DATAEND || data->error) {
984 if (dma_inprogress(host))
985 mmci_dma_finalize(host, data);
986 mmci_stop_data(host);
989 /* The error clause is handled above, success! */
990 data->bytes_xfered = data->blksz * data->blocks;
992 if (!data->stop || host->mrq->sbc) {
993 mmci_request_end(host, data->mrq);
995 mmci_start_command(host, data->stop, 0);
1001 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
1002 unsigned int status)
1004 void __iomem *base = host->base;
1010 sbc = (cmd == host->mrq->sbc);
1013 * We need to be one of these interrupts to be considered worth
1014 * handling. Note that we tag on any latent IRQs postponed
1015 * due to waiting for busy status.
1017 if (!((status|host->busy_status) &
1018 (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
1022 * ST Micro variant: handle busy detection.
1024 if (host->variant->busy_detect) {
1025 bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
1027 /* We are busy with a command, return */
1028 if (host->busy_status &&
1029 (status & host->variant->busy_detect_flag))
1033 * We were not busy, but we now got a busy response on
1034 * something that was not an error, and we double-check
1035 * that the special busy status bit is still set before
1038 if (!host->busy_status && busy_resp &&
1039 !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
1040 (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
1042 /* Clear the busy start IRQ */
1043 writel(host->variant->busy_detect_mask,
1044 host->base + MMCICLEAR);
1046 /* Unmask the busy end IRQ */
1047 writel(readl(base + MMCIMASK0) |
1048 host->variant->busy_detect_mask,
1051 * Now cache the last response status code (until
1052 * the busy bit goes low), and return.
1055 status & (MCI_CMDSENT|MCI_CMDRESPEND);
1060 * At this point we are not busy with a command, we have
1061 * not received a new busy request, clear and mask the busy
1062 * end IRQ and fall through to process the IRQ.
1064 if (host->busy_status) {
1066 writel(host->variant->busy_detect_mask,
1067 host->base + MMCICLEAR);
1069 writel(readl(base + MMCIMASK0) &
1070 ~host->variant->busy_detect_mask,
1072 host->busy_status = 0;
1078 if (status & MCI_CMDTIMEOUT) {
1079 cmd->error = -ETIMEDOUT;
1080 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
1081 cmd->error = -EILSEQ;
1083 cmd->resp[0] = readl(base + MMCIRESPONSE0);
1084 cmd->resp[1] = readl(base + MMCIRESPONSE1);
1085 cmd->resp[2] = readl(base + MMCIRESPONSE2);
1086 cmd->resp[3] = readl(base + MMCIRESPONSE3);
1089 if ((!sbc && !cmd->data) || cmd->error) {
1091 /* Terminate the DMA transfer */
1092 if (dma_inprogress(host)) {
1093 mmci_dma_data_error(host);
1094 mmci_dma_unmap(host, host->data);
1096 mmci_stop_data(host);
1098 mmci_request_end(host, host->mrq);
1100 mmci_start_command(host, host->mrq->cmd, 0);
1101 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
1102 mmci_start_data(host, cmd->data);
1106 static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
1108 return remain - (readl(host->base + MMCIFIFOCNT) << 2);
1111 static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
1114 * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
1115 * from the fifo range should be used
1117 if (status & MCI_RXFIFOHALFFULL)
1118 return host->variant->fifohalfsize;
1119 else if (status & MCI_RXDATAAVLBL)
1125 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
1127 void __iomem *base = host->base;
1129 u32 status = readl(host->base + MMCISTATUS);
1130 int host_remain = host->size;
1133 int count = host->get_rx_fifocnt(host, status, host_remain);
1142 * SDIO especially may want to send something that is
1143 * not divisible by 4 (as opposed to card sectors
1144 * etc). Therefore make sure to always read the last bytes
1145 * while only doing full 32-bit reads towards the FIFO.
1147 if (unlikely(count & 0x3)) {
1149 unsigned char buf[4];
1150 ioread32_rep(base + MMCIFIFO, buf, 1);
1151 memcpy(ptr, buf, count);
1153 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
1157 ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
1162 host_remain -= count;
1167 status = readl(base + MMCISTATUS);
1168 } while (status & MCI_RXDATAAVLBL);
1170 return ptr - buffer;
1173 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
1175 struct variant_data *variant = host->variant;
1176 void __iomem *base = host->base;
1180 unsigned int count, maxcnt;
1182 maxcnt = status & MCI_TXFIFOEMPTY ?
1183 variant->fifosize : variant->fifohalfsize;
1184 count = min(remain, maxcnt);
1187 * SDIO especially may want to send something that is
1188 * not divisible by 4 (as opposed to card sectors
1189 * etc), and the FIFO only accept full 32-bit writes.
1190 * So compensate by adding +3 on the count, a single
1191 * byte become a 32bit write, 7 bytes will be two
1194 iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
1202 status = readl(base + MMCISTATUS);
1203 } while (status & MCI_TXFIFOHALFEMPTY);
1205 return ptr - buffer;
1209 * PIO data transfer IRQ handler.
1211 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
1213 struct mmci_host *host = dev_id;
1214 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1215 struct variant_data *variant = host->variant;
1216 void __iomem *base = host->base;
1217 unsigned long flags;
1220 status = readl(base + MMCISTATUS);
1222 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
1224 local_irq_save(flags);
1227 unsigned int remain, len;
1231 * For write, we only need to test the half-empty flag
1232 * here - if the FIFO is completely empty, then by
1233 * definition it is more than half empty.
1235 * For read, check for data available.
1237 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
1240 if (!sg_miter_next(sg_miter))
1243 buffer = sg_miter->addr;
1244 remain = sg_miter->length;
1247 if (status & MCI_RXACTIVE)
1248 len = mmci_pio_read(host, buffer, remain);
1249 if (status & MCI_TXACTIVE)
1250 len = mmci_pio_write(host, buffer, remain, status);
1252 sg_miter->consumed = len;
1260 status = readl(base + MMCISTATUS);
1263 sg_miter_stop(sg_miter);
1265 local_irq_restore(flags);
1268 * If we have less than the fifo 'half-full' threshold to transfer,
1269 * trigger a PIO interrupt as soon as any data is available.
1271 if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
1272 mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
1275 * If we run out of data, disable the data IRQs; this
1276 * prevents a race where the FIFO becomes empty before
1277 * the chip itself has disabled the data path, and
1278 * stops us racing with our data end IRQ.
1280 if (host->size == 0) {
1281 mmci_set_mask1(host, 0);
1282 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
1289 * Handle completion of command and data transfers.
1291 static irqreturn_t mmci_irq(int irq, void *dev_id)
1293 struct mmci_host *host = dev_id;
1297 spin_lock(&host->lock);
1300 status = readl(host->base + MMCISTATUS);
1302 if (host->singleirq) {
1303 if (status & host->mask1_reg)
1304 mmci_pio_irq(irq, dev_id);
1306 status &= ~MCI_IRQ1MASK;
1310 * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
1311 * enabled) in mmci_cmd_irq() function where ST Micro busy
1312 * detection variant is handled. Considering the HW seems to be
1313 * triggering the IRQ on both edges while monitoring DAT0 for
1314 * busy completion and that same status bit is used to monitor
1315 * start and end of busy detection, special care must be taken
1316 * to make sure that both start and end interrupts are always
1317 * cleared one after the other.
1319 status &= readl(host->base + MMCIMASK0);
1320 if (host->variant->busy_detect)
1321 writel(status & ~host->variant->busy_detect_mask,
1322 host->base + MMCICLEAR);
1324 writel(status, host->base + MMCICLEAR);
1326 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
1328 if (host->variant->reversed_irq_handling) {
1329 mmci_data_irq(host, host->data, status);
1330 mmci_cmd_irq(host, host->cmd, status);
1332 mmci_cmd_irq(host, host->cmd, status);
1333 mmci_data_irq(host, host->data, status);
1337 * Don't poll for busy completion in irq context.
1339 if (host->variant->busy_detect && host->busy_status)
1340 status &= ~host->variant->busy_detect_flag;
1345 spin_unlock(&host->lock);
1347 return IRQ_RETVAL(ret);
1350 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1352 struct mmci_host *host = mmc_priv(mmc);
1353 unsigned long flags;
1355 WARN_ON(host->mrq != NULL);
1357 mrq->cmd->error = mmci_validate_data(host, mrq->data);
1358 if (mrq->cmd->error) {
1359 mmc_request_done(mmc, mrq);
1363 spin_lock_irqsave(&host->lock, flags);
1368 mmci_get_next_data(host, mrq->data);
1370 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
1371 mmci_start_data(host, mrq->data);
1374 mmci_start_command(host, mrq->sbc, 0);
1376 mmci_start_command(host, mrq->cmd, 0);
1378 spin_unlock_irqrestore(&host->lock, flags);
1381 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1383 struct mmci_host *host = mmc_priv(mmc);
1384 struct variant_data *variant = host->variant;
1386 unsigned long flags;
1389 if (host->plat->ios_handler &&
1390 host->plat->ios_handler(mmc_dev(mmc), ios))
1391 dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
1393 switch (ios->power_mode) {
1395 if (!IS_ERR(mmc->supply.vmmc))
1396 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1398 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
1399 regulator_disable(mmc->supply.vqmmc);
1400 host->vqmmc_enabled = false;
1405 if (!IS_ERR(mmc->supply.vmmc))
1406 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
1409 * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
1410 * and instead uses MCI_PWR_ON so apply whatever value is
1411 * configured in the variant data.
1413 pwr |= variant->pwrreg_powerup;
1417 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
1418 ret = regulator_enable(mmc->supply.vqmmc);
1420 dev_err(mmc_dev(mmc),
1421 "failed to enable vqmmc regulator\n");
1423 host->vqmmc_enabled = true;
1430 if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
1432 * The ST Micro variant has some additional bits
1433 * indicating signal direction for the signals in
1434 * the SD/MMC bus and feedback-clock usage.
1436 pwr |= host->pwr_reg_add;
1438 if (ios->bus_width == MMC_BUS_WIDTH_4)
1439 pwr &= ~MCI_ST_DATA74DIREN;
1440 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1441 pwr &= (~MCI_ST_DATA74DIREN &
1442 ~MCI_ST_DATA31DIREN &
1443 ~MCI_ST_DATA2DIREN);
1446 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
1447 if (host->hw_designer != AMBA_VENDOR_ST)
1451 * The ST Micro variant use the ROD bit for something
1452 * else and only has OD (Open Drain).
1459 * If clock = 0 and the variant requires the MMCIPOWER to be used for
1460 * gating the clock, the MCI_PWR_ON bit is cleared.
1462 if (!ios->clock && variant->pwrreg_clkgate)
1465 if (host->variant->explicit_mclk_control &&
1466 ios->clock != host->clock_cache) {
1467 ret = clk_set_rate(host->clk, ios->clock);
1469 dev_err(mmc_dev(host->mmc),
1470 "Error setting clock rate (%d)\n", ret);
1472 host->mclk = clk_get_rate(host->clk);
1474 host->clock_cache = ios->clock;
1476 spin_lock_irqsave(&host->lock, flags);
1478 mmci_set_clkreg(host, ios->clock);
1479 mmci_write_pwrreg(host, pwr);
1480 mmci_reg_delay(host);
1482 spin_unlock_irqrestore(&host->lock, flags);
1485 static int mmci_get_cd(struct mmc_host *mmc)
1487 struct mmci_host *host = mmc_priv(mmc);
1488 struct mmci_platform_data *plat = host->plat;
1489 unsigned int status = mmc_gpio_get_cd(mmc);
1491 if (status == -ENOSYS) {
1493 return 1; /* Assume always present */
1495 status = plat->status(mmc_dev(host->mmc));
1500 static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1504 if (!IS_ERR(mmc->supply.vqmmc)) {
1506 switch (ios->signal_voltage) {
1507 case MMC_SIGNAL_VOLTAGE_330:
1508 ret = regulator_set_voltage(mmc->supply.vqmmc,
1511 case MMC_SIGNAL_VOLTAGE_180:
1512 ret = regulator_set_voltage(mmc->supply.vqmmc,
1515 case MMC_SIGNAL_VOLTAGE_120:
1516 ret = regulator_set_voltage(mmc->supply.vqmmc,
1522 dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
1528 static struct mmc_host_ops mmci_ops = {
1529 .request = mmci_request,
1530 .pre_req = mmci_pre_request,
1531 .post_req = mmci_post_request,
1532 .set_ios = mmci_set_ios,
1533 .get_ro = mmc_gpio_get_ro,
1534 .get_cd = mmci_get_cd,
1535 .start_signal_voltage_switch = mmci_sig_volt_switch,
1538 static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
1540 struct mmci_host *host = mmc_priv(mmc);
1541 int ret = mmc_of_parse(mmc);
1546 if (of_get_property(np, "st,sig-dir-dat0", NULL))
1547 host->pwr_reg_add |= MCI_ST_DATA0DIREN;
1548 if (of_get_property(np, "st,sig-dir-dat2", NULL))
1549 host->pwr_reg_add |= MCI_ST_DATA2DIREN;
1550 if (of_get_property(np, "st,sig-dir-dat31", NULL))
1551 host->pwr_reg_add |= MCI_ST_DATA31DIREN;
1552 if (of_get_property(np, "st,sig-dir-dat74", NULL))
1553 host->pwr_reg_add |= MCI_ST_DATA74DIREN;
1554 if (of_get_property(np, "st,sig-dir-cmd", NULL))
1555 host->pwr_reg_add |= MCI_ST_CMDDIREN;
1556 if (of_get_property(np, "st,sig-pin-fbclk", NULL))
1557 host->pwr_reg_add |= MCI_ST_FBCLKEN;
1559 if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
1560 mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
1561 if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
1562 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1567 static int mmci_probe(struct amba_device *dev,
1568 const struct amba_id *id)
1570 struct mmci_platform_data *plat = dev->dev.platform_data;
1571 struct device_node *np = dev->dev.of_node;
1572 struct variant_data *variant = id->data;
1573 struct mmci_host *host;
1574 struct mmc_host *mmc;
1577 /* Must have platform data or Device Tree. */
1579 dev_err(&dev->dev, "No plat data or DT found\n");
1584 plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
1589 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
1593 ret = mmci_of_parse(np, mmc);
1597 host = mmc_priv(mmc);
1600 host->hw_designer = amba_manf(dev);
1601 host->hw_revision = amba_rev(dev);
1602 dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
1603 dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
1605 host->clk = devm_clk_get(&dev->dev, NULL);
1606 if (IS_ERR(host->clk)) {
1607 ret = PTR_ERR(host->clk);
1611 ret = clk_prepare_enable(host->clk);
1615 if (variant->qcom_fifo)
1616 host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
1618 host->get_rx_fifocnt = mmci_get_rx_fifocnt;
1621 host->variant = variant;
1622 host->mclk = clk_get_rate(host->clk);
1624 * According to the spec, mclk is max 100 MHz,
1625 * so we try to adjust the clock down to this,
1628 if (host->mclk > variant->f_max) {
1629 ret = clk_set_rate(host->clk, variant->f_max);
1632 host->mclk = clk_get_rate(host->clk);
1633 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
1637 host->phybase = dev->res.start;
1638 host->base = devm_ioremap_resource(&dev->dev, &dev->res);
1639 if (IS_ERR(host->base)) {
1640 ret = PTR_ERR(host->base);
1645 * The ARM and ST versions of the block have slightly different
1646 * clock divider equations which means that the minimum divider
1648 * on Qualcomm like controllers get the nearest minimum clock to 100Khz
1650 if (variant->st_clkdiv)
1651 mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
1652 else if (variant->explicit_mclk_control)
1653 mmc->f_min = clk_round_rate(host->clk, 100000);
1655 mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
1657 * If no maximum operating frequency is supplied, fall back to use
1658 * the module parameter, which has a (low) default value in case it
1659 * is not specified. Either value must not exceed the clock rate into
1660 * the block, of course.
1663 mmc->f_max = variant->explicit_mclk_control ?
1664 min(variant->f_max, mmc->f_max) :
1665 min(host->mclk, mmc->f_max);
1667 mmc->f_max = variant->explicit_mclk_control ?
1668 fmax : min(host->mclk, fmax);
1671 dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
1673 /* Get regulators and the supported OCR mask */
1674 ret = mmc_regulator_get_supply(mmc);
1678 if (!mmc->ocr_avail)
1679 mmc->ocr_avail = plat->ocr_mask;
1680 else if (plat->ocr_mask)
1681 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1683 /* DT takes precedence over platform data. */
1685 if (!plat->cd_invert)
1686 mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
1687 mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
1690 /* We support these capabilities. */
1691 mmc->caps |= MMC_CAP_CMD23;
1694 * Enable busy detection.
1696 if (variant->busy_detect) {
1697 mmci_ops.card_busy = mmci_card_busy;
1699 * Not all variants have a flag to enable busy detection
1700 * in the DPSM, but if they do, set it here.
1702 if (variant->busy_dpsm_flag)
1703 mmci_write_datactrlreg(host,
1704 host->variant->busy_dpsm_flag);
1705 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1706 mmc->max_busy_timeout = 0;
1709 mmc->ops = &mmci_ops;
1711 /* We support these PM capabilities. */
1712 mmc->pm_caps |= MMC_PM_KEEP_POWER;
1717 mmc->max_segs = NR_SG;
1720 * Since only a certain number of bits are valid in the data length
1721 * register, we must ensure that we don't exceed 2^num-1 bytes in a
1724 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
1727 * Set the maximum segment size. Since we aren't doing DMA
1728 * (yet) we are only limited by the data length register.
1730 mmc->max_seg_size = mmc->max_req_size;
1733 * Block size can be up to 2048 bytes, but must be a power of two.
1735 mmc->max_blk_size = 1 << 11;
1738 * Limit the number of blocks transferred so that we don't overflow
1739 * the maximum request size.
1741 mmc->max_blk_count = mmc->max_req_size >> 11;
1743 spin_lock_init(&host->lock);
1745 writel(0, host->base + MMCIMASK0);
1747 if (variant->mmcimask1)
1748 writel(0, host->base + MMCIMASK1);
1750 writel(0xfff, host->base + MMCICLEAR);
1754 * - not using DT but using a descriptor table, or
1755 * - using a table of descriptors ALONGSIDE DT, or
1756 * look up these descriptors named "cd" and "wp" right here, fail
1757 * silently of these do not exist and proceed to try platform data
1760 ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
1762 if (ret == -EPROBE_DEFER)
1764 else if (gpio_is_valid(plat->gpio_cd)) {
1765 ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
1771 ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
1773 if (ret == -EPROBE_DEFER)
1775 else if (gpio_is_valid(plat->gpio_wp)) {
1776 ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
1783 ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
1784 DRIVER_NAME " (cmd)", host);
1789 host->singleirq = true;
1791 ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
1792 IRQF_SHARED, DRIVER_NAME " (pio)", host);
1797 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1799 amba_set_drvdata(dev, mmc);
1801 dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
1802 mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
1803 amba_rev(dev), (unsigned long long)dev->res.start,
1804 dev->irq[0], dev->irq[1]);
1806 mmci_dma_setup(host);
1808 pm_runtime_set_autosuspend_delay(&dev->dev, 50);
1809 pm_runtime_use_autosuspend(&dev->dev);
1813 pm_runtime_put(&dev->dev);
1817 clk_disable_unprepare(host->clk);
1823 static int mmci_remove(struct amba_device *dev)
1825 struct mmc_host *mmc = amba_get_drvdata(dev);
1828 struct mmci_host *host = mmc_priv(mmc);
1829 struct variant_data *variant = host->variant;
1832 * Undo pm_runtime_put() in probe. We use the _sync
1833 * version here so that we can access the primecell.
1835 pm_runtime_get_sync(&dev->dev);
1837 mmc_remove_host(mmc);
1839 writel(0, host->base + MMCIMASK0);
1841 if (variant->mmcimask1)
1842 writel(0, host->base + MMCIMASK1);
1844 writel(0, host->base + MMCICOMMAND);
1845 writel(0, host->base + MMCIDATACTRL);
1847 mmci_dma_release(host);
1848 clk_disable_unprepare(host->clk);
1856 static void mmci_save(struct mmci_host *host)
1858 unsigned long flags;
1860 spin_lock_irqsave(&host->lock, flags);
1862 writel(0, host->base + MMCIMASK0);
1863 if (host->variant->pwrreg_nopower) {
1864 writel(0, host->base + MMCIDATACTRL);
1865 writel(0, host->base + MMCIPOWER);
1866 writel(0, host->base + MMCICLOCK);
1868 mmci_reg_delay(host);
1870 spin_unlock_irqrestore(&host->lock, flags);
1873 static void mmci_restore(struct mmci_host *host)
1875 unsigned long flags;
1877 spin_lock_irqsave(&host->lock, flags);
1879 if (host->variant->pwrreg_nopower) {
1880 writel(host->clk_reg, host->base + MMCICLOCK);
1881 writel(host->datactrl_reg, host->base + MMCIDATACTRL);
1882 writel(host->pwr_reg, host->base + MMCIPOWER);
1884 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
1885 mmci_reg_delay(host);
1887 spin_unlock_irqrestore(&host->lock, flags);
1890 static int mmci_runtime_suspend(struct device *dev)
1892 struct amba_device *adev = to_amba_device(dev);
1893 struct mmc_host *mmc = amba_get_drvdata(adev);
1896 struct mmci_host *host = mmc_priv(mmc);
1897 pinctrl_pm_select_sleep_state(dev);
1899 clk_disable_unprepare(host->clk);
1905 static int mmci_runtime_resume(struct device *dev)
1907 struct amba_device *adev = to_amba_device(dev);
1908 struct mmc_host *mmc = amba_get_drvdata(adev);
1911 struct mmci_host *host = mmc_priv(mmc);
1912 clk_prepare_enable(host->clk);
1914 pinctrl_pm_select_default_state(dev);
1921 static const struct dev_pm_ops mmci_dev_pm_ops = {
1922 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1923 pm_runtime_force_resume)
1924 SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
1927 static const struct amba_id mmci_ids[] = {
1931 .data = &variant_arm,
1936 .data = &variant_arm_extended_fifo,
1941 .data = &variant_arm_extended_fifo_hwfc,
1946 .data = &variant_arm,
1948 /* ST Micro variants */
1952 .data = &variant_u300,
1957 .data = &variant_nomadik,
1962 .data = &variant_nomadik,
1967 .data = &variant_ux500,
1972 .data = &variant_ux500v2,
1974 /* Qualcomm variants */
1978 .data = &variant_qcom,
1983 MODULE_DEVICE_TABLE(amba, mmci_ids);
1985 static struct amba_driver mmci_driver = {
1987 .name = DRIVER_NAME,
1988 .pm = &mmci_dev_pm_ops,
1990 .probe = mmci_probe,
1991 .remove = mmci_remove,
1992 .id_table = mmci_ids,
1995 module_amba_driver(mmci_driver);
1997 module_param(fmax, uint, 0444);
1999 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
2000 MODULE_LICENSE("GPL");