1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic SD/eMMC driver for the GX/S905 family SoCs
5 * Copyright (c) 2016 BayLibre, SAS.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/iopoll.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/ioport.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/mmc.h>
20 #include <linux/mmc/sdio.h>
21 #include <linux/mmc/slot-gpio.h>
23 #include <linux/clk.h>
24 #include <linux/clk-provider.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/reset.h>
27 #include <linux/interrupt.h>
28 #include <linux/bitfield.h>
29 #include <linux/pinctrl/consumer.h>
31 #define DRIVER_NAME "meson-gx-mmc"
33 #define SD_EMMC_CLOCK 0x0
34 #define CLK_DIV_MASK GENMASK(5, 0)
35 #define CLK_SRC_MASK GENMASK(7, 6)
36 #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
37 #define CLK_TX_PHASE_MASK GENMASK(11, 10)
38 #define CLK_RX_PHASE_MASK GENMASK(13, 12)
40 #define CLK_PHASE_180 2
41 #define CLK_V2_TX_DELAY_MASK GENMASK(19, 16)
42 #define CLK_V2_RX_DELAY_MASK GENMASK(23, 20)
43 #define CLK_V2_ALWAYS_ON BIT(24)
44 #define CLK_V2_IRQ_SDIO_SLEEP BIT(25)
46 #define CLK_V3_TX_DELAY_MASK GENMASK(21, 16)
47 #define CLK_V3_RX_DELAY_MASK GENMASK(27, 22)
48 #define CLK_V3_ALWAYS_ON BIT(28)
49 #define CLK_V3_IRQ_SDIO_SLEEP BIT(29)
51 #define CLK_TX_DELAY_MASK(h) (h->data->tx_delay_mask)
52 #define CLK_RX_DELAY_MASK(h) (h->data->rx_delay_mask)
53 #define CLK_ALWAYS_ON(h) (h->data->always_on)
54 #define CLK_IRQ_SDIO_SLEEP(h) (h->data->irq_sdio_sleep)
56 #define SD_EMMC_DELAY 0x4
57 #define SD_EMMC_ADJUST 0x8
58 #define ADJUST_ADJ_DELAY_MASK GENMASK(21, 16)
59 #define ADJUST_DS_EN BIT(15)
60 #define ADJUST_ADJ_EN BIT(13)
62 #define SD_EMMC_DELAY1 0x4
63 #define SD_EMMC_DELAY2 0x8
64 #define SD_EMMC_V3_ADJUST 0xc
66 #define SD_EMMC_CALOUT 0x10
67 #define SD_EMMC_START 0x40
68 #define START_DESC_INIT BIT(0)
69 #define START_DESC_BUSY BIT(1)
70 #define START_DESC_ADDR_MASK GENMASK(31, 2)
72 #define SD_EMMC_CFG 0x44
73 #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
74 #define CFG_BUS_WIDTH_1 0x0
75 #define CFG_BUS_WIDTH_4 0x1
76 #define CFG_BUS_WIDTH_8 0x2
77 #define CFG_DDR BIT(2)
78 #define CFG_BLK_LEN_MASK GENMASK(7, 4)
79 #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
80 #define CFG_RC_CC_MASK GENMASK(15, 12)
81 #define CFG_STOP_CLOCK BIT(22)
82 #define CFG_CLK_ALWAYS_ON BIT(18)
83 #define CFG_CHK_DS BIT(20)
84 #define CFG_AUTO_CLK BIT(23)
85 #define CFG_ERR_ABORT BIT(27)
87 #define SD_EMMC_STATUS 0x48
88 #define STATUS_BUSY BIT(31)
89 #define STATUS_DESC_BUSY BIT(30)
90 #define STATUS_DATI GENMASK(23, 16)
92 #define SD_EMMC_IRQ_EN 0x4c
93 #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
94 #define IRQ_TXD_ERR BIT(8)
95 #define IRQ_DESC_ERR BIT(9)
96 #define IRQ_RESP_ERR BIT(10)
98 (IRQ_RXD_ERR_MASK | IRQ_TXD_ERR | IRQ_DESC_ERR | IRQ_RESP_ERR)
99 #define IRQ_RESP_TIMEOUT BIT(11)
100 #define IRQ_DESC_TIMEOUT BIT(12)
101 #define IRQ_TIMEOUTS \
102 (IRQ_RESP_TIMEOUT | IRQ_DESC_TIMEOUT)
103 #define IRQ_END_OF_CHAIN BIT(13)
104 #define IRQ_RESP_STATUS BIT(14)
105 #define IRQ_SDIO BIT(15)
106 #define IRQ_EN_MASK \
107 (IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN)
109 #define SD_EMMC_CMD_CFG 0x50
110 #define SD_EMMC_CMD_ARG 0x54
111 #define SD_EMMC_CMD_DAT 0x58
112 #define SD_EMMC_CMD_RSP 0x5c
113 #define SD_EMMC_CMD_RSP1 0x60
114 #define SD_EMMC_CMD_RSP2 0x64
115 #define SD_EMMC_CMD_RSP3 0x68
117 #define SD_EMMC_RXD 0x94
118 #define SD_EMMC_TXD 0x94
119 #define SD_EMMC_LAST_REG SD_EMMC_TXD
121 #define SD_EMMC_SRAM_DATA_BUF_LEN 1536
122 #define SD_EMMC_SRAM_DATA_BUF_OFF 0x200
124 #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
125 #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
126 #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
127 #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
128 #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
129 #define SD_EMMC_DESC_BUF_LEN PAGE_SIZE
131 #define SD_EMMC_PRE_REQ_DONE BIT(0)
132 #define SD_EMMC_DESC_CHAIN_MODE BIT(1)
134 #define MUX_CLK_NUM_PARENTS 2
136 struct meson_mmc_data {
137 unsigned int tx_delay_mask;
138 unsigned int rx_delay_mask;
139 unsigned int always_on;
141 unsigned int irq_sdio_sleep;
144 struct sd_emmc_desc {
153 const struct meson_mmc_data *data;
154 struct mmc_host *mmc;
155 struct mmc_command *cmd;
160 unsigned long req_rate;
163 bool dram_access_quirk;
165 struct pinctrl *pinctrl;
166 struct pinctrl_state *pins_clk_gate;
168 unsigned int bounce_buf_size;
170 void __iomem *bounce_iomem_buf;
171 dma_addr_t bounce_dma_addr;
172 struct sd_emmc_desc *descs;
173 dma_addr_t descs_dma_addr;
177 bool needs_pre_post_req;
182 #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
183 #define CMD_CFG_BLOCK_MODE BIT(9)
184 #define CMD_CFG_R1B BIT(10)
185 #define CMD_CFG_END_OF_CHAIN BIT(11)
186 #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
187 #define CMD_CFG_NO_RESP BIT(16)
188 #define CMD_CFG_NO_CMD BIT(17)
189 #define CMD_CFG_DATA_IO BIT(18)
190 #define CMD_CFG_DATA_WR BIT(19)
191 #define CMD_CFG_RESP_NOCRC BIT(20)
192 #define CMD_CFG_RESP_128 BIT(21)
193 #define CMD_CFG_RESP_NUM BIT(22)
194 #define CMD_CFG_DATA_NUM BIT(23)
195 #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
196 #define CMD_CFG_ERROR BIT(30)
197 #define CMD_CFG_OWNER BIT(31)
199 #define CMD_DATA_MASK GENMASK(31, 2)
200 #define CMD_DATA_BIG_ENDIAN BIT(1)
201 #define CMD_DATA_SRAM BIT(0)
202 #define CMD_RESP_MASK GENMASK(31, 1)
203 #define CMD_RESP_SRAM BIT(0)
205 static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data)
207 unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC;
210 return SD_EMMC_CMD_TIMEOUT_DATA;
212 timeout = roundup_pow_of_two(timeout);
214 return min(timeout, 32768U); /* max. 2^15 ms */
217 static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd)
219 if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
220 return cmd->mrq->cmd;
221 else if (mmc_op_multi(cmd->opcode) &&
222 (!cmd->mrq->sbc || cmd->error || cmd->data->error))
223 return cmd->mrq->stop;
228 static void meson_mmc_get_transfer_mode(struct mmc_host *mmc,
229 struct mmc_request *mrq)
231 struct meson_host *host = mmc_priv(mmc);
232 struct mmc_data *data = mrq->data;
233 struct scatterlist *sg;
237 * When Controller DMA cannot directly access DDR memory, disable
238 * support for Chain Mode to directly use the internal SRAM using
239 * the bounce buffer mode.
241 if (host->dram_access_quirk)
244 /* SD_IO_RW_EXTENDED (CMD53) can also use block mode under the hood */
245 if (data->blocks > 1 || mrq->cmd->opcode == SD_IO_RW_EXTENDED) {
247 * In block mode DMA descriptor format, "length" field indicates
248 * number of blocks and there is no way to pass DMA size that
249 * is not multiple of SDIO block size, making it impossible to
250 * tie more than one memory buffer with single SDIO block.
251 * Block mode sg buffer size should be aligned with SDIO block
252 * size, otherwise chain mode could not be used.
254 for_each_sg(data->sg, sg, data->sg_len, i) {
255 if (sg->length % data->blksz) {
256 dev_warn_once(mmc_dev(mmc),
257 "unaligned sg len %u blksize %u, disabling descriptor DMA for transfer\n",
258 sg->length, data->blksz);
264 for_each_sg(data->sg, sg, data->sg_len, i) {
265 /* check for 8 byte alignment */
266 if (sg->offset % 8) {
267 dev_warn_once(mmc_dev(mmc),
268 "unaligned sg offset %u, disabling descriptor DMA for transfer\n",
274 data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE;
277 static inline bool meson_mmc_desc_chain_mode(const struct mmc_data *data)
279 return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE;
282 static inline bool meson_mmc_bounce_buf_read(const struct mmc_data *data)
284 return data && data->flags & MMC_DATA_READ &&
285 !meson_mmc_desc_chain_mode(data);
288 static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
290 struct mmc_data *data = mrq->data;
295 meson_mmc_get_transfer_mode(mmc, mrq);
296 data->host_cookie |= SD_EMMC_PRE_REQ_DONE;
298 if (!meson_mmc_desc_chain_mode(data))
301 data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
302 mmc_get_dma_dir(data));
304 dev_err(mmc_dev(mmc), "dma_map_sg failed");
307 static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
310 struct mmc_data *data = mrq->data;
312 if (data && meson_mmc_desc_chain_mode(data) && data->sg_count)
313 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
314 mmc_get_dma_dir(data));
318 * Gating the clock on this controller is tricky. It seems the mmc clock
319 * is also used by the controller. It may crash during some operation if the
320 * clock is stopped. The safest thing to do, whenever possible, is to keep
321 * clock running at stop it at the pad using the pinmux.
323 static void meson_mmc_clk_gate(struct meson_host *host)
327 if (host->pins_clk_gate) {
328 pinctrl_select_state(host->pinctrl, host->pins_clk_gate);
331 * If the pinmux is not provided - default to the classic and
334 cfg = readl(host->regs + SD_EMMC_CFG);
335 cfg |= CFG_STOP_CLOCK;
336 writel(cfg, host->regs + SD_EMMC_CFG);
340 static void meson_mmc_clk_ungate(struct meson_host *host)
344 if (host->pins_clk_gate)
345 pinctrl_select_default_state(host->dev);
347 /* Make sure the clock is not stopped in the controller */
348 cfg = readl(host->regs + SD_EMMC_CFG);
349 cfg &= ~CFG_STOP_CLOCK;
350 writel(cfg, host->regs + SD_EMMC_CFG);
353 static int meson_mmc_clk_set(struct meson_host *host, unsigned long rate,
356 struct mmc_host *mmc = host->mmc;
360 /* Same request - bail-out */
361 if (host->ddr == ddr && host->req_rate == rate)
365 meson_mmc_clk_gate(host);
367 mmc->actual_clock = 0;
369 /* return with clock being stopped */
373 /* Stop the clock during rate change to avoid glitches */
374 cfg = readl(host->regs + SD_EMMC_CFG);
375 cfg |= CFG_STOP_CLOCK;
376 writel(cfg, host->regs + SD_EMMC_CFG);
379 /* DDR modes require higher module clock */
385 writel(cfg, host->regs + SD_EMMC_CFG);
388 ret = clk_set_rate(host->mmc_clk, rate);
390 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
395 host->req_rate = rate;
396 mmc->actual_clock = clk_get_rate(host->mmc_clk);
398 /* We should report the real output frequency of the controller */
400 host->req_rate >>= 1;
401 mmc->actual_clock >>= 1;
404 dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock);
405 if (rate != mmc->actual_clock)
406 dev_dbg(host->dev, "requested rate was %lu\n", rate);
408 /* (re)start clock */
409 meson_mmc_clk_ungate(host);
415 * The SD/eMMC IP block has an internal mux and divider used for
416 * generating the MMC clock. Use the clock framework to create and
417 * manage these clocks.
419 static int meson_mmc_clk_init(struct meson_host *host)
421 struct clk_init_data init;
423 struct clk_divider *div;
426 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
427 const char *clk_parent[1];
430 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
431 clk_reg = CLK_ALWAYS_ON(host);
432 clk_reg |= CLK_DIV_MASK;
433 clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, CLK_PHASE_180);
434 clk_reg |= FIELD_PREP(CLK_TX_PHASE_MASK, CLK_PHASE_0);
435 clk_reg |= FIELD_PREP(CLK_RX_PHASE_MASK, CLK_PHASE_0);
436 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
437 clk_reg |= CLK_IRQ_SDIO_SLEEP(host);
438 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
440 /* get the mux parents */
441 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
445 snprintf(name, sizeof(name), "clkin%d", i);
446 clk = devm_clk_get(host->dev, name);
448 return dev_err_probe(host->dev, PTR_ERR(clk),
449 "Missing clock %s\n", name);
451 mux_parent_names[i] = __clk_get_name(clk);
455 mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL);
459 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
460 init.name = clk_name;
461 init.ops = &clk_mux_ops;
463 init.parent_names = mux_parent_names;
464 init.num_parents = MUX_CLK_NUM_PARENTS;
466 mux->reg = host->regs + SD_EMMC_CLOCK;
467 mux->shift = __ffs(CLK_SRC_MASK);
468 mux->mask = CLK_SRC_MASK >> mux->shift;
469 mux->hw.init = &init;
471 host->mux_clk = devm_clk_register(host->dev, &mux->hw);
472 if (WARN_ON(IS_ERR(host->mux_clk)))
473 return PTR_ERR(host->mux_clk);
475 /* create the divider */
476 div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL);
480 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
481 init.name = clk_name;
482 init.ops = &clk_divider_ops;
483 init.flags = CLK_SET_RATE_PARENT;
484 clk_parent[0] = __clk_get_name(host->mux_clk);
485 init.parent_names = clk_parent;
486 init.num_parents = 1;
488 div->reg = host->regs + SD_EMMC_CLOCK;
489 div->shift = __ffs(CLK_DIV_MASK);
490 div->width = __builtin_popcountl(CLK_DIV_MASK);
491 div->hw.init = &init;
492 div->flags = CLK_DIVIDER_ONE_BASED;
494 host->mmc_clk = devm_clk_register(host->dev, &div->hw);
495 if (WARN_ON(IS_ERR(host->mmc_clk)))
496 return PTR_ERR(host->mmc_clk);
498 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
499 host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000);
500 ret = clk_set_rate(host->mmc_clk, host->mmc->f_min);
504 return clk_prepare_enable(host->mmc_clk);
507 static void meson_mmc_disable_resampling(struct meson_host *host)
509 unsigned int val = readl(host->regs + host->data->adjust);
511 val &= ~ADJUST_ADJ_EN;
512 writel(val, host->regs + host->data->adjust);
515 static void meson_mmc_reset_resampling(struct meson_host *host)
519 meson_mmc_disable_resampling(host);
521 val = readl(host->regs + host->data->adjust);
522 val &= ~ADJUST_ADJ_DELAY_MASK;
523 writel(val, host->regs + host->data->adjust);
526 static int meson_mmc_resampling_tuning(struct mmc_host *mmc, u32 opcode)
528 struct meson_host *host = mmc_priv(mmc);
529 unsigned int val, dly, max_dly, i;
532 /* Resampling is done using the source clock */
533 max_dly = DIV_ROUND_UP(clk_get_rate(host->mux_clk),
534 clk_get_rate(host->mmc_clk));
536 val = readl(host->regs + host->data->adjust);
537 val |= ADJUST_ADJ_EN;
538 writel(val, host->regs + host->data->adjust);
540 if (mmc_doing_retune(mmc))
541 dly = FIELD_GET(ADJUST_ADJ_DELAY_MASK, val) + 1;
545 for (i = 0; i < max_dly; i++) {
546 val &= ~ADJUST_ADJ_DELAY_MASK;
547 val |= FIELD_PREP(ADJUST_ADJ_DELAY_MASK, (dly + i) % max_dly);
548 writel(val, host->regs + host->data->adjust);
550 ret = mmc_send_tuning(mmc, opcode, NULL);
552 dev_dbg(mmc_dev(mmc), "resampling delay: %u\n",
553 (dly + i) % max_dly);
558 meson_mmc_reset_resampling(host);
562 static int meson_mmc_prepare_ios_clock(struct meson_host *host,
567 switch (ios->timing) {
568 case MMC_TIMING_MMC_DDR52:
569 case MMC_TIMING_UHS_DDR50:
578 return meson_mmc_clk_set(host, ios->clock, ddr);
581 static void meson_mmc_check_resampling(struct meson_host *host,
584 switch (ios->timing) {
585 case MMC_TIMING_LEGACY:
586 case MMC_TIMING_MMC_HS:
587 case MMC_TIMING_SD_HS:
588 case MMC_TIMING_MMC_DDR52:
589 meson_mmc_disable_resampling(host);
594 static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
596 struct meson_host *host = mmc_priv(mmc);
601 * GPIO regulator, only controls switching between 1v8 and
602 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
604 switch (ios->power_mode) {
606 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
607 mmc_regulator_disable_vqmmc(mmc);
612 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
617 mmc_regulator_enable_vqmmc(mmc);
623 switch (ios->bus_width) {
624 case MMC_BUS_WIDTH_1:
625 bus_width = CFG_BUS_WIDTH_1;
627 case MMC_BUS_WIDTH_4:
628 bus_width = CFG_BUS_WIDTH_4;
630 case MMC_BUS_WIDTH_8:
631 bus_width = CFG_BUS_WIDTH_8;
634 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
636 bus_width = CFG_BUS_WIDTH_4;
639 val = readl(host->regs + SD_EMMC_CFG);
640 val &= ~CFG_BUS_WIDTH_MASK;
641 val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
642 writel(val, host->regs + SD_EMMC_CFG);
644 meson_mmc_check_resampling(host, ios);
645 err = meson_mmc_prepare_ios_clock(host, ios);
647 dev_err(host->dev, "Failed to set clock: %d\n,", err);
649 dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val);
652 static void meson_mmc_request_done(struct mmc_host *mmc,
653 struct mmc_request *mrq)
655 struct meson_host *host = mmc_priv(mmc);
658 if (host->needs_pre_post_req)
659 meson_mmc_post_req(mmc, mrq, 0);
660 mmc_request_done(host->mmc, mrq);
663 static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz)
665 struct meson_host *host = mmc_priv(mmc);
668 cfg = readl(host->regs + SD_EMMC_CFG);
669 blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
671 if (!is_power_of_2(blksz))
672 dev_err(host->dev, "blksz %u is not a power of 2\n", blksz);
674 blksz = ilog2(blksz);
676 /* check if block-size matches, if not update */
677 if (blksz == blksz_old)
680 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__,
683 cfg &= ~CFG_BLK_LEN_MASK;
684 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz);
685 writel(cfg, host->regs + SD_EMMC_CFG);
688 static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg)
690 if (cmd->flags & MMC_RSP_PRESENT) {
691 if (cmd->flags & MMC_RSP_136)
692 *cmd_cfg |= CMD_CFG_RESP_128;
693 *cmd_cfg |= CMD_CFG_RESP_NUM;
695 if (!(cmd->flags & MMC_RSP_CRC))
696 *cmd_cfg |= CMD_CFG_RESP_NOCRC;
698 if (cmd->flags & MMC_RSP_BUSY)
699 *cmd_cfg |= CMD_CFG_R1B;
701 *cmd_cfg |= CMD_CFG_NO_RESP;
705 static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
707 struct meson_host *host = mmc_priv(mmc);
708 struct sd_emmc_desc *desc = host->descs;
709 struct mmc_data *data = host->cmd->data;
710 struct scatterlist *sg;
714 if (data->flags & MMC_DATA_WRITE)
715 cmd_cfg |= CMD_CFG_DATA_WR;
717 if (data->blocks > 1) {
718 cmd_cfg |= CMD_CFG_BLOCK_MODE;
719 meson_mmc_set_blksz(mmc, data->blksz);
722 for_each_sg(data->sg, sg, data->sg_count, i) {
723 unsigned int len = sg_dma_len(sg);
725 if (data->blocks > 1)
728 desc[i].cmd_cfg = cmd_cfg;
729 desc[i].cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, len);
731 desc[i].cmd_cfg |= CMD_CFG_NO_CMD;
732 desc[i].cmd_arg = host->cmd->arg;
733 desc[i].cmd_resp = 0;
734 desc[i].cmd_data = sg_dma_address(sg);
736 desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN;
738 dma_wmb(); /* ensure descriptor is written before kicked */
739 start = host->descs_dma_addr | START_DESC_BUSY;
740 writel(start, host->regs + SD_EMMC_START);
743 /* local sg copy for dram_access_quirk */
744 static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data,
745 size_t buflen, bool to_buffer)
747 unsigned int sg_flags = SG_MITER_ATOMIC;
748 struct scatterlist *sgl = data->sg;
749 unsigned int nents = data->sg_len;
750 struct sg_mapping_iter miter;
751 unsigned int offset = 0;
754 sg_flags |= SG_MITER_FROM_SG;
756 sg_flags |= SG_MITER_TO_SG;
758 sg_miter_start(&miter, sgl, nents, sg_flags);
760 while ((offset < buflen) && sg_miter_next(&miter)) {
761 unsigned int buf_offset = 0;
762 unsigned int len, left;
763 u32 *buf = miter.addr;
765 len = min(miter.length, buflen - offset);
770 writel(*buf++, host->bounce_iomem_buf + offset + buf_offset);
777 *buf++ = readl(host->bounce_iomem_buf + offset + buf_offset);
787 sg_miter_stop(&miter);
790 static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
792 struct meson_host *host = mmc_priv(mmc);
793 struct mmc_data *data = cmd->data;
794 u32 cmd_cfg = 0, cmd_data = 0;
795 unsigned int xfer_bytes = 0;
797 /* Setup descriptors */
802 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
803 cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
804 cmd_cfg |= CMD_CFG_ERROR; /* stop in case of error */
806 meson_mmc_set_response_bits(cmd, &cmd_cfg);
810 data->bytes_xfered = 0;
811 cmd_cfg |= CMD_CFG_DATA_IO;
812 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
813 ilog2(meson_mmc_get_timeout_msecs(data)));
815 if (meson_mmc_desc_chain_mode(data)) {
816 meson_mmc_desc_chain_transfer(mmc, cmd_cfg);
820 if (data->blocks > 1) {
821 cmd_cfg |= CMD_CFG_BLOCK_MODE;
822 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
824 meson_mmc_set_blksz(mmc, data->blksz);
826 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
829 xfer_bytes = data->blksz * data->blocks;
830 if (data->flags & MMC_DATA_WRITE) {
831 cmd_cfg |= CMD_CFG_DATA_WR;
832 WARN_ON(xfer_bytes > host->bounce_buf_size);
833 if (host->dram_access_quirk)
834 meson_mmc_copy_buffer(host, data, xfer_bytes, true);
836 sg_copy_to_buffer(data->sg, data->sg_len,
837 host->bounce_buf, xfer_bytes);
841 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
843 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
844 ilog2(SD_EMMC_CMD_TIMEOUT));
847 /* Last descriptor */
848 cmd_cfg |= CMD_CFG_END_OF_CHAIN;
849 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
850 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
851 writel(0, host->regs + SD_EMMC_CMD_RSP);
852 wmb(); /* ensure descriptor is written before kicked */
853 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
856 static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data)
858 struct scatterlist *sg;
861 /* Reject request if any element offset or size is not 32bit aligned */
862 for_each_sg(data->sg, sg, data->sg_len, i) {
863 if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
864 !IS_ALIGNED(sg->length, sizeof(u32))) {
865 dev_err(mmc_dev(mmc), "unaligned sg offset %u len %u\n",
866 data->sg->offset, data->sg->length);
874 static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
876 struct meson_host *host = mmc_priv(mmc);
877 host->needs_pre_post_req = mrq->data &&
878 !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);
881 * The memory at the end of the controller used as bounce buffer for
882 * the dram_access_quirk only accepts 32bit read/write access,
883 * check the aligment and length of the data before starting the request.
885 if (host->dram_access_quirk && mrq->data) {
886 mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data);
887 if (mrq->cmd->error) {
888 mmc_request_done(mmc, mrq);
893 if (host->needs_pre_post_req) {
894 meson_mmc_get_transfer_mode(mmc, mrq);
895 if (!meson_mmc_desc_chain_mode(mrq->data))
896 host->needs_pre_post_req = false;
899 if (host->needs_pre_post_req)
900 meson_mmc_pre_req(mmc, mrq);
903 writel(0, host->regs + SD_EMMC_START);
905 meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd);
908 static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
910 struct meson_host *host = mmc_priv(mmc);
912 if (cmd->flags & MMC_RSP_136) {
913 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
914 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
915 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
916 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
917 } else if (cmd->flags & MMC_RSP_PRESENT) {
918 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
922 static void __meson_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
924 struct meson_host *host = mmc_priv(mmc);
925 u32 reg_irqen = IRQ_EN_MASK;
928 reg_irqen |= IRQ_SDIO;
929 writel(reg_irqen, host->regs + SD_EMMC_IRQ_EN);
932 static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
934 struct meson_host *host = dev_id;
935 struct mmc_command *cmd;
936 u32 status, raw_status, irq_mask = IRQ_EN_MASK;
937 irqreturn_t ret = IRQ_NONE;
939 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
940 irq_mask |= IRQ_SDIO;
941 raw_status = readl(host->regs + SD_EMMC_STATUS);
942 status = raw_status & irq_mask;
946 "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n",
947 irq_mask, raw_status);
954 /* ack all raised interrupts */
955 writel(status, host->regs + SD_EMMC_STATUS);
959 if (status & IRQ_SDIO) {
960 spin_lock(&host->lock);
961 __meson_mmc_enable_sdio_irq(host->mmc, 0);
962 sdio_signal_irq(host->mmc);
963 spin_unlock(&host->lock);
973 if (status & IRQ_CRC_ERR) {
974 dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status);
975 cmd->error = -EILSEQ;
976 ret = IRQ_WAKE_THREAD;
980 if (status & IRQ_TIMEOUTS) {
981 dev_dbg(host->dev, "Timeout - status 0x%08x\n", status);
982 cmd->error = -ETIMEDOUT;
983 ret = IRQ_WAKE_THREAD;
987 meson_mmc_read_resp(host->mmc, cmd);
989 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
990 struct mmc_data *data = cmd->data;
992 if (data && !cmd->error)
993 data->bytes_xfered = data->blksz * data->blocks;
994 if (meson_mmc_bounce_buf_read(data) ||
995 meson_mmc_get_next_command(cmd))
996 ret = IRQ_WAKE_THREAD;
1003 /* Stop desc in case of errors */
1004 u32 start = readl(host->regs + SD_EMMC_START);
1006 start &= ~START_DESC_BUSY;
1007 writel(start, host->regs + SD_EMMC_START);
1010 if (ret == IRQ_HANDLED)
1011 meson_mmc_request_done(host->mmc, cmd->mrq);
1016 static int meson_mmc_wait_desc_stop(struct meson_host *host)
1021 * It may sometimes take a while for it to actually halt. Here, we
1022 * are giving it 5ms to comply
1024 * If we don't confirm the descriptor is stopped, it might raise new
1025 * IRQs after we have called mmc_request_done() which is bad.
1028 return readl_poll_timeout(host->regs + SD_EMMC_STATUS, status,
1029 !(status & (STATUS_BUSY | STATUS_DESC_BUSY)),
1033 static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
1035 struct meson_host *host = dev_id;
1036 struct mmc_command *next_cmd, *cmd = host->cmd;
1037 struct mmc_data *data;
1038 unsigned int xfer_bytes;
1044 meson_mmc_wait_desc_stop(host);
1045 meson_mmc_request_done(host->mmc, cmd->mrq);
1051 if (meson_mmc_bounce_buf_read(data)) {
1052 xfer_bytes = data->blksz * data->blocks;
1053 WARN_ON(xfer_bytes > host->bounce_buf_size);
1054 if (host->dram_access_quirk)
1055 meson_mmc_copy_buffer(host, data, xfer_bytes, false);
1057 sg_copy_from_buffer(data->sg, data->sg_len,
1058 host->bounce_buf, xfer_bytes);
1061 next_cmd = meson_mmc_get_next_command(cmd);
1063 meson_mmc_start_cmd(host->mmc, next_cmd);
1065 meson_mmc_request_done(host->mmc, cmd->mrq);
1070 static void meson_mmc_cfg_init(struct meson_host *host)
1074 cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
1075 ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
1076 cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
1077 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
1079 /* abort chain on R/W errors */
1080 cfg |= CFG_ERR_ABORT;
1082 writel(cfg, host->regs + SD_EMMC_CFG);
1085 static int meson_mmc_card_busy(struct mmc_host *mmc)
1087 struct meson_host *host = mmc_priv(mmc);
1090 regval = readl(host->regs + SD_EMMC_STATUS);
1092 /* We are only interrested in lines 0 to 3, so mask the other ones */
1093 return !(FIELD_GET(STATUS_DATI, regval) & 0xf);
1096 static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1100 /* vqmmc regulator is available */
1101 if (!IS_ERR(mmc->supply.vqmmc)) {
1103 * The usual amlogic setup uses a GPIO to switch from one
1104 * regulator to the other. While the voltage ramp up is
1105 * pretty fast, care must be taken when switching from 3.3v
1106 * to 1.8v. Please make sure the regulator framework is aware
1107 * of your own regulator constraints
1109 ret = mmc_regulator_set_vqmmc(mmc, ios);
1110 return ret < 0 ? ret : 0;
1113 /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
1114 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1120 static void meson_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1122 struct meson_host *host = mmc_priv(mmc);
1123 unsigned long flags;
1125 spin_lock_irqsave(&host->lock, flags);
1126 __meson_mmc_enable_sdio_irq(mmc, enable);
1127 spin_unlock_irqrestore(&host->lock, flags);
1130 static void meson_mmc_ack_sdio_irq(struct mmc_host *mmc)
1132 meson_mmc_enable_sdio_irq(mmc, 1);
1135 static const struct mmc_host_ops meson_mmc_ops = {
1136 .request = meson_mmc_request,
1137 .set_ios = meson_mmc_set_ios,
1138 .get_cd = mmc_gpio_get_cd,
1139 .pre_req = meson_mmc_pre_req,
1140 .post_req = meson_mmc_post_req,
1141 .execute_tuning = meson_mmc_resampling_tuning,
1142 .card_busy = meson_mmc_card_busy,
1143 .start_signal_voltage_switch = meson_mmc_voltage_switch,
1144 .enable_sdio_irq = meson_mmc_enable_sdio_irq,
1145 .ack_sdio_irq = meson_mmc_ack_sdio_irq,
1148 static int meson_mmc_probe(struct platform_device *pdev)
1150 struct resource *res;
1151 struct meson_host *host;
1152 struct mmc_host *mmc;
1153 struct clk *core_clk;
1156 mmc = devm_mmc_alloc_host(&pdev->dev, sizeof(struct meson_host));
1159 host = mmc_priv(mmc);
1161 host->dev = &pdev->dev;
1162 dev_set_drvdata(&pdev->dev, host);
1164 /* The G12A SDIO Controller needs an SRAM bounce buffer */
1165 host->dram_access_quirk = device_property_read_bool(&pdev->dev,
1166 "amlogic,dram-access-quirk");
1168 /* Get regulators and the supported OCR mask */
1169 ret = mmc_regulator_get_supply(mmc);
1173 ret = mmc_of_parse(mmc);
1175 return dev_err_probe(&pdev->dev, ret, "error parsing DT\n");
1177 mmc->caps |= MMC_CAP_CMD23;
1179 if (mmc->caps & MMC_CAP_SDIO_IRQ)
1180 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
1182 host->data = of_device_get_match_data(&pdev->dev);
1186 ret = device_reset_optional(&pdev->dev);
1188 return dev_err_probe(&pdev->dev, ret, "device reset failed\n");
1190 host->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1191 if (IS_ERR(host->regs))
1192 return PTR_ERR(host->regs);
1194 host->irq = platform_get_irq(pdev, 0);
1198 cd_irq = platform_get_irq_optional(pdev, 1);
1199 mmc_gpio_set_cd_irq(mmc, cd_irq);
1201 host->pinctrl = devm_pinctrl_get(&pdev->dev);
1202 if (IS_ERR(host->pinctrl))
1203 return PTR_ERR(host->pinctrl);
1205 host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl,
1207 if (IS_ERR(host->pins_clk_gate)) {
1208 dev_warn(&pdev->dev,
1209 "can't get clk-gate pinctrl, using clk_stop bit\n");
1210 host->pins_clk_gate = NULL;
1213 core_clk = devm_clk_get_enabled(&pdev->dev, "core");
1214 if (IS_ERR(core_clk))
1215 return PTR_ERR(core_clk);
1217 ret = meson_mmc_clk_init(host);
1221 /* set config to sane default */
1222 meson_mmc_cfg_init(host);
1224 /* Stop execution */
1225 writel(0, host->regs + SD_EMMC_START);
1227 /* clear, ack and enable interrupts */
1228 writel(0, host->regs + SD_EMMC_IRQ_EN);
1229 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
1230 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
1232 ret = request_threaded_irq(host->irq, meson_mmc_irq,
1233 meson_mmc_irq_thread, IRQF_ONESHOT,
1234 dev_name(&pdev->dev), host);
1238 spin_lock_init(&host->lock);
1240 if (host->dram_access_quirk) {
1241 /* Limit segments to 1 due to low available sram memory */
1243 /* Limit to the available sram memory */
1244 mmc->max_blk_count = SD_EMMC_SRAM_DATA_BUF_LEN /
1247 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
1248 mmc->max_segs = SD_EMMC_DESC_BUF_LEN /
1249 sizeof(struct sd_emmc_desc);
1251 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
1252 mmc->max_seg_size = mmc->max_req_size;
1255 * At the moment, we don't know how to reliably enable HS400.
1256 * From the different datasheets, it is not even clear if this mode
1257 * is officially supported by any of the SoCs
1259 mmc->caps2 &= ~MMC_CAP2_HS400;
1261 if (host->dram_access_quirk) {
1263 * The MMC Controller embeds 1,5KiB of internal SRAM
1264 * that can be used to be used as bounce buffer.
1265 * In the case of the G12A SDIO controller, use these
1266 * instead of the DDR memory
1268 host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN;
1269 host->bounce_iomem_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF;
1270 host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF;
1272 /* data bounce buffer */
1273 host->bounce_buf_size = mmc->max_req_size;
1275 dmam_alloc_coherent(host->dev, host->bounce_buf_size,
1276 &host->bounce_dma_addr, GFP_KERNEL);
1277 if (host->bounce_buf == NULL) {
1278 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
1284 host->descs = dmam_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
1285 &host->descs_dma_addr, GFP_KERNEL);
1287 dev_err(host->dev, "Allocating descriptor DMA buffer failed\n");
1292 mmc->ops = &meson_mmc_ops;
1293 ret = mmc_add_host(mmc);
1300 free_irq(host->irq, host);
1302 clk_disable_unprepare(host->mmc_clk);
1306 static int meson_mmc_remove(struct platform_device *pdev)
1308 struct meson_host *host = dev_get_drvdata(&pdev->dev);
1310 mmc_remove_host(host->mmc);
1312 /* disable interrupts */
1313 writel(0, host->regs + SD_EMMC_IRQ_EN);
1314 free_irq(host->irq, host);
1316 clk_disable_unprepare(host->mmc_clk);
1321 static const struct meson_mmc_data meson_gx_data = {
1322 .tx_delay_mask = CLK_V2_TX_DELAY_MASK,
1323 .rx_delay_mask = CLK_V2_RX_DELAY_MASK,
1324 .always_on = CLK_V2_ALWAYS_ON,
1325 .adjust = SD_EMMC_ADJUST,
1326 .irq_sdio_sleep = CLK_V2_IRQ_SDIO_SLEEP,
1329 static const struct meson_mmc_data meson_axg_data = {
1330 .tx_delay_mask = CLK_V3_TX_DELAY_MASK,
1331 .rx_delay_mask = CLK_V3_RX_DELAY_MASK,
1332 .always_on = CLK_V3_ALWAYS_ON,
1333 .adjust = SD_EMMC_V3_ADJUST,
1334 .irq_sdio_sleep = CLK_V3_IRQ_SDIO_SLEEP,
1337 static const struct of_device_id meson_mmc_of_match[] = {
1338 { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data },
1339 { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data },
1340 { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data },
1341 { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data },
1342 { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data },
1345 MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
1347 static struct platform_driver meson_mmc_driver = {
1348 .probe = meson_mmc_probe,
1349 .remove = meson_mmc_remove,
1351 .name = DRIVER_NAME,
1352 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1353 .of_match_table = meson_mmc_of_match,
1357 module_platform_driver(meson_mmc_driver);
1359 MODULE_DESCRIPTION("Amlogic S905*/GX*/AXG SD/eMMC driver");
1361 MODULE_LICENSE("GPL v2");