Lines Matching +full:hs400 +full:- +full:cmd +full:- +full:int +full:- +full:delay
1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
21 #include <linux/mmc/slot-gpio.h>
24 #include <linux/clk-provider.h>
31 #define DRIVER_NAME "meson-gx-mmc"
49 #define CLK_TX_DELAY_MASK(h) (h->data->tx_delay_mask)
50 #define CLK_RX_DELAY_MASK(h) (h->data->rx_delay_mask)
51 #define CLK_ALWAYS_ON(h) (h->data->always_on)
135 unsigned int tx_delay_mask;
136 unsigned int rx_delay_mask;
137 unsigned int always_on;
138 unsigned int adjust;
152 struct mmc_command *cmd; member
166 unsigned int bounce_buf_size;
173 int irq;
201 static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data) in meson_mmc_get_timeout_msecs()
203 unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC; in meson_mmc_get_timeout_msecs()
213 static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd) in meson_mmc_get_next_command() argument
215 if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error) in meson_mmc_get_next_command()
216 return cmd->mrq->cmd; in meson_mmc_get_next_command()
217 else if (mmc_op_multi(cmd->opcode) && in meson_mmc_get_next_command()
218 (!cmd->mrq->sbc || cmd->error || cmd->data->error)) in meson_mmc_get_next_command()
219 return cmd->mrq->stop; in meson_mmc_get_next_command()
228 struct mmc_data *data = mrq->data; in meson_mmc_get_transfer_mode()
230 int i; in meson_mmc_get_transfer_mode()
237 if (host->dram_access_quirk) in meson_mmc_get_transfer_mode()
241 if (data->blocks > 1 || mrq->cmd->opcode == SD_IO_RW_EXTENDED) { in meson_mmc_get_transfer_mode()
250 for_each_sg(data->sg, sg, data->sg_len, i) { in meson_mmc_get_transfer_mode()
251 if (sg->length % data->blksz) { in meson_mmc_get_transfer_mode()
254 sg->length, data->blksz); in meson_mmc_get_transfer_mode()
260 for_each_sg(data->sg, sg, data->sg_len, i) { in meson_mmc_get_transfer_mode()
262 if (sg->offset % 8) { in meson_mmc_get_transfer_mode()
265 sg->offset); in meson_mmc_get_transfer_mode()
270 data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE; in meson_mmc_get_transfer_mode()
275 return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE; in meson_mmc_desc_chain_mode()
280 return data && data->flags & MMC_DATA_READ && in meson_mmc_bounce_buf_read()
286 struct mmc_data *data = mrq->data; in meson_mmc_pre_req()
292 data->host_cookie |= SD_EMMC_PRE_REQ_DONE; in meson_mmc_pre_req()
297 data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, in meson_mmc_pre_req()
299 if (!data->sg_count) in meson_mmc_pre_req()
304 int err) in meson_mmc_post_req()
306 struct mmc_data *data = mrq->data; in meson_mmc_post_req()
308 if (data && meson_mmc_desc_chain_mode(data) && data->sg_count) in meson_mmc_post_req()
309 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, in meson_mmc_post_req()
323 if (host->pins_clk_gate) { in meson_mmc_clk_gate()
324 pinctrl_select_state(host->pinctrl, host->pins_clk_gate); in meson_mmc_clk_gate()
327 * If the pinmux is not provided - default to the classic and in meson_mmc_clk_gate()
330 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_gate()
332 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_gate()
340 if (host->pins_clk_gate) in meson_mmc_clk_ungate()
341 pinctrl_select_default_state(host->dev); in meson_mmc_clk_ungate()
344 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_ungate()
346 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_ungate()
349 static int meson_mmc_clk_set(struct meson_host *host, unsigned long rate, in meson_mmc_clk_set()
352 struct mmc_host *mmc = host->mmc; in meson_mmc_clk_set()
353 int ret; in meson_mmc_clk_set()
356 /* Same request - bail-out */ in meson_mmc_clk_set()
357 if (host->ddr == ddr && host->req_rate == rate) in meson_mmc_clk_set()
362 host->req_rate = 0; in meson_mmc_clk_set()
363 mmc->actual_clock = 0; in meson_mmc_clk_set()
370 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
372 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
381 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
382 host->ddr = ddr; in meson_mmc_clk_set()
384 ret = clk_set_rate(host->mmc_clk, rate); in meson_mmc_clk_set()
386 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n", in meson_mmc_clk_set()
391 host->req_rate = rate; in meson_mmc_clk_set()
392 mmc->actual_clock = clk_get_rate(host->mmc_clk); in meson_mmc_clk_set()
396 host->req_rate >>= 1; in meson_mmc_clk_set()
397 mmc->actual_clock >>= 1; in meson_mmc_clk_set()
400 dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock); in meson_mmc_clk_set()
401 if (rate != mmc->actual_clock) in meson_mmc_clk_set()
402 dev_dbg(host->dev, "requested rate was %lu\n", rate); in meson_mmc_clk_set()
415 static int meson_mmc_clk_init(struct meson_host *host) in meson_mmc_clk_init()
421 int i, ret = 0; in meson_mmc_clk_init()
432 writel(clk_reg, host->regs + SD_EMMC_CLOCK); in meson_mmc_clk_init()
440 clk = devm_clk_get(host->dev, name); in meson_mmc_clk_init()
442 return dev_err_probe(host->dev, PTR_ERR(clk), in meson_mmc_clk_init()
449 mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL); in meson_mmc_clk_init()
451 return -ENOMEM; in meson_mmc_clk_init()
453 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev)); in meson_mmc_clk_init()
460 mux->reg = host->regs + SD_EMMC_CLOCK; in meson_mmc_clk_init()
461 mux->shift = __ffs(CLK_SRC_MASK); in meson_mmc_clk_init()
462 mux->mask = CLK_SRC_MASK >> mux->shift; in meson_mmc_clk_init()
463 mux->hw.init = &init; in meson_mmc_clk_init()
465 host->mux_clk = devm_clk_register(host->dev, &mux->hw); in meson_mmc_clk_init()
466 if (WARN_ON(IS_ERR(host->mux_clk))) in meson_mmc_clk_init()
467 return PTR_ERR(host->mux_clk); in meson_mmc_clk_init()
470 div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL); in meson_mmc_clk_init()
472 return -ENOMEM; in meson_mmc_clk_init()
474 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev)); in meson_mmc_clk_init()
478 clk_parent[0] = __clk_get_name(host->mux_clk); in meson_mmc_clk_init()
482 div->reg = host->regs + SD_EMMC_CLOCK; in meson_mmc_clk_init()
483 div->shift = __ffs(CLK_DIV_MASK); in meson_mmc_clk_init()
484 div->width = __builtin_popcountl(CLK_DIV_MASK); in meson_mmc_clk_init()
485 div->hw.init = &init; in meson_mmc_clk_init()
486 div->flags = CLK_DIVIDER_ONE_BASED; in meson_mmc_clk_init()
488 host->mmc_clk = devm_clk_register(host->dev, &div->hw); in meson_mmc_clk_init()
489 if (WARN_ON(IS_ERR(host->mmc_clk))) in meson_mmc_clk_init()
490 return PTR_ERR(host->mmc_clk); in meson_mmc_clk_init()
493 host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000); in meson_mmc_clk_init()
494 ret = clk_set_rate(host->mmc_clk, host->mmc->f_min); in meson_mmc_clk_init()
498 return clk_prepare_enable(host->mmc_clk); in meson_mmc_clk_init()
503 unsigned int val = readl(host->regs + host->data->adjust); in meson_mmc_disable_resampling()
506 writel(val, host->regs + host->data->adjust); in meson_mmc_disable_resampling()
511 unsigned int val; in meson_mmc_reset_resampling()
515 val = readl(host->regs + host->data->adjust); in meson_mmc_reset_resampling()
517 writel(val, host->regs + host->data->adjust); in meson_mmc_reset_resampling()
520 static int meson_mmc_resampling_tuning(struct mmc_host *mmc, u32 opcode) in meson_mmc_resampling_tuning()
523 unsigned int val, dly, max_dly, i; in meson_mmc_resampling_tuning()
524 int ret; in meson_mmc_resampling_tuning()
527 max_dly = DIV_ROUND_UP(clk_get_rate(host->mux_clk), in meson_mmc_resampling_tuning()
528 clk_get_rate(host->mmc_clk)); in meson_mmc_resampling_tuning()
530 val = readl(host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
532 writel(val, host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
542 writel(val, host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
546 dev_dbg(mmc_dev(mmc), "resampling delay: %u\n", in meson_mmc_resampling_tuning()
553 return -EIO; in meson_mmc_resampling_tuning()
556 static int meson_mmc_prepare_ios_clock(struct meson_host *host, in meson_mmc_prepare_ios_clock()
561 switch (ios->timing) { in meson_mmc_prepare_ios_clock()
572 return meson_mmc_clk_set(host, ios->clock, ddr); in meson_mmc_prepare_ios_clock()
578 switch (ios->timing) { in meson_mmc_check_resampling()
592 int err; in meson_mmc_set_ios()
598 switch (ios->power_mode) { in meson_mmc_set_ios()
600 if (!IS_ERR(mmc->supply.vmmc)) in meson_mmc_set_ios()
601 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); in meson_mmc_set_ios()
603 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { in meson_mmc_set_ios()
604 regulator_disable(mmc->supply.vqmmc); in meson_mmc_set_ios()
605 host->vqmmc_enabled = false; in meson_mmc_set_ios()
611 if (!IS_ERR(mmc->supply.vmmc)) in meson_mmc_set_ios()
612 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); in meson_mmc_set_ios()
617 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { in meson_mmc_set_ios()
618 int ret = regulator_enable(mmc->supply.vqmmc); in meson_mmc_set_ios()
621 dev_err(host->dev, in meson_mmc_set_ios()
624 host->vqmmc_enabled = true; in meson_mmc_set_ios()
631 switch (ios->bus_width) { in meson_mmc_set_ios()
642 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n", in meson_mmc_set_ios()
643 ios->bus_width); in meson_mmc_set_ios()
647 val = readl(host->regs + SD_EMMC_CFG); in meson_mmc_set_ios()
650 writel(val, host->regs + SD_EMMC_CFG); in meson_mmc_set_ios()
655 dev_err(host->dev, "Failed to set clock: %d\n,", err); in meson_mmc_set_ios()
657 dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val); in meson_mmc_set_ios()
665 host->cmd = NULL; in meson_mmc_request_done()
666 mmc_request_done(host->mmc, mrq); in meson_mmc_request_done()
669 static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz) in meson_mmc_set_blksz()
674 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_set_blksz()
678 dev_err(host->dev, "blksz %u is not a power of 2\n", blksz); in meson_mmc_set_blksz()
682 /* check if block-size matches, if not update */ in meson_mmc_set_blksz()
686 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__, in meson_mmc_set_blksz()
691 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_set_blksz()
694 static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg) in meson_mmc_set_response_bits() argument
696 if (cmd->flags & MMC_RSP_PRESENT) { in meson_mmc_set_response_bits()
697 if (cmd->flags & MMC_RSP_136) in meson_mmc_set_response_bits()
701 if (!(cmd->flags & MMC_RSP_CRC)) in meson_mmc_set_response_bits()
704 if (cmd->flags & MMC_RSP_BUSY) in meson_mmc_set_response_bits()
714 struct sd_emmc_desc *desc = host->descs; in meson_mmc_desc_chain_transfer()
715 struct mmc_data *data = host->cmd->data; in meson_mmc_desc_chain_transfer()
718 int i; in meson_mmc_desc_chain_transfer()
720 if (data->flags & MMC_DATA_WRITE) in meson_mmc_desc_chain_transfer()
723 if (data->blocks > 1) { in meson_mmc_desc_chain_transfer()
725 meson_mmc_set_blksz(mmc, data->blksz); in meson_mmc_desc_chain_transfer()
728 for_each_sg(data->sg, sg, data->sg_count, i) { in meson_mmc_desc_chain_transfer()
729 unsigned int len = sg_dma_len(sg); in meson_mmc_desc_chain_transfer()
731 if (data->blocks > 1) in meson_mmc_desc_chain_transfer()
732 len /= data->blksz; in meson_mmc_desc_chain_transfer()
738 desc[i].cmd_arg = host->cmd->arg; in meson_mmc_desc_chain_transfer()
742 desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN; in meson_mmc_desc_chain_transfer()
745 start = host->descs_dma_addr | START_DESC_BUSY; in meson_mmc_desc_chain_transfer()
746 writel(start, host->regs + SD_EMMC_START); in meson_mmc_desc_chain_transfer()
753 unsigned int sg_flags = SG_MITER_ATOMIC; in meson_mmc_copy_buffer()
754 struct scatterlist *sgl = data->sg; in meson_mmc_copy_buffer()
755 unsigned int nents = data->sg_len; in meson_mmc_copy_buffer()
757 unsigned int offset = 0; in meson_mmc_copy_buffer()
767 unsigned int buf_offset = 0; in meson_mmc_copy_buffer()
768 unsigned int len, left; in meson_mmc_copy_buffer()
771 len = min(miter.length, buflen - offset); in meson_mmc_copy_buffer()
776 writel(*buf++, host->bounce_iomem_buf + offset + buf_offset); in meson_mmc_copy_buffer()
779 left -= 4; in meson_mmc_copy_buffer()
783 *buf++ = readl(host->bounce_iomem_buf + offset + buf_offset); in meson_mmc_copy_buffer()
786 left -= 4; in meson_mmc_copy_buffer()
796 static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd) in meson_mmc_start_cmd() argument
799 struct mmc_data *data = cmd->data; in meson_mmc_start_cmd()
801 unsigned int xfer_bytes = 0; in meson_mmc_start_cmd()
806 host->cmd = cmd; in meson_mmc_start_cmd()
808 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode); in meson_mmc_start_cmd()
812 meson_mmc_set_response_bits(cmd, &cmd_cfg); in meson_mmc_start_cmd()
816 data->bytes_xfered = 0; in meson_mmc_start_cmd()
826 if (data->blocks > 1) { in meson_mmc_start_cmd()
829 data->blocks); in meson_mmc_start_cmd()
830 meson_mmc_set_blksz(mmc, data->blksz); in meson_mmc_start_cmd()
832 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz); in meson_mmc_start_cmd()
835 xfer_bytes = data->blksz * data->blocks; in meson_mmc_start_cmd()
836 if (data->flags & MMC_DATA_WRITE) { in meson_mmc_start_cmd()
838 WARN_ON(xfer_bytes > host->bounce_buf_size); in meson_mmc_start_cmd()
839 if (host->dram_access_quirk) in meson_mmc_start_cmd()
842 sg_copy_to_buffer(data->sg, data->sg_len, in meson_mmc_start_cmd()
843 host->bounce_buf, xfer_bytes); in meson_mmc_start_cmd()
847 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK; in meson_mmc_start_cmd()
855 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG); in meson_mmc_start_cmd()
856 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT); in meson_mmc_start_cmd()
857 writel(0, host->regs + SD_EMMC_CMD_RSP); in meson_mmc_start_cmd()
859 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG); in meson_mmc_start_cmd()
862 static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data) in meson_mmc_validate_dram_access()
865 int i; in meson_mmc_validate_dram_access()
868 for_each_sg(data->sg, sg, data->sg_len, i) { in meson_mmc_validate_dram_access()
869 if (!IS_ALIGNED(sg->offset, sizeof(u32)) || in meson_mmc_validate_dram_access()
870 !IS_ALIGNED(sg->length, sizeof(u32))) { in meson_mmc_validate_dram_access()
872 data->sg->offset, data->sg->length); in meson_mmc_validate_dram_access()
873 return -EINVAL; in meson_mmc_validate_dram_access()
883 bool needs_pre_post_req = mrq->data && in meson_mmc_request()
884 !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE); in meson_mmc_request()
891 if (host->dram_access_quirk && mrq->data) { in meson_mmc_request()
892 mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data); in meson_mmc_request()
893 if (mrq->cmd->error) { in meson_mmc_request()
901 if (!meson_mmc_desc_chain_mode(mrq->data)) in meson_mmc_request()
909 writel(0, host->regs + SD_EMMC_START); in meson_mmc_request()
911 meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd); in meson_mmc_request()
917 static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd) in meson_mmc_read_resp() argument
921 if (cmd->flags & MMC_RSP_136) { in meson_mmc_read_resp()
922 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3); in meson_mmc_read_resp()
923 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2); in meson_mmc_read_resp()
924 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1); in meson_mmc_read_resp()
925 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP); in meson_mmc_read_resp()
926 } else if (cmd->flags & MMC_RSP_PRESENT) { in meson_mmc_read_resp()
927 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP); in meson_mmc_read_resp()
931 static irqreturn_t meson_mmc_irq(int irq, void *dev_id) in meson_mmc_irq()
934 struct mmc_command *cmd; in meson_mmc_irq() local
939 irq_en = readl(host->regs + SD_EMMC_IRQ_EN); in meson_mmc_irq()
940 raw_status = readl(host->regs + SD_EMMC_STATUS); in meson_mmc_irq()
944 dev_dbg(host->dev, in meson_mmc_irq()
945 "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n", in meson_mmc_irq()
950 if (WARN_ON(!host) || WARN_ON(!host->cmd)) in meson_mmc_irq()
954 writel(status, host->regs + SD_EMMC_STATUS); in meson_mmc_irq()
956 cmd = host->cmd; in meson_mmc_irq()
957 data = cmd->data; in meson_mmc_irq()
958 cmd->error = 0; in meson_mmc_irq()
960 dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status); in meson_mmc_irq()
961 cmd->error = -EILSEQ; in meson_mmc_irq()
967 dev_dbg(host->dev, "Timeout - status 0x%08x\n", status); in meson_mmc_irq()
968 cmd->error = -ETIMEDOUT; in meson_mmc_irq()
973 meson_mmc_read_resp(host->mmc, cmd); in meson_mmc_irq()
976 dev_dbg(host->dev, "IRQ: SDIO TODO.\n"); in meson_mmc_irq()
981 if (data && !cmd->error) in meson_mmc_irq()
982 data->bytes_xfered = data->blksz * data->blocks; in meson_mmc_irq()
984 meson_mmc_get_next_command(cmd)) in meson_mmc_irq()
991 if (cmd->error) { in meson_mmc_irq()
993 u32 start = readl(host->regs + SD_EMMC_START); in meson_mmc_irq()
996 writel(start, host->regs + SD_EMMC_START); in meson_mmc_irq()
1000 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq()
1005 static int meson_mmc_wait_desc_stop(struct meson_host *host) in meson_mmc_wait_desc_stop()
1017 return readl_poll_timeout(host->regs + SD_EMMC_STATUS, status, in meson_mmc_wait_desc_stop()
1022 static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id) in meson_mmc_irq_thread()
1025 struct mmc_command *next_cmd, *cmd = host->cmd; in meson_mmc_irq_thread() local
1027 unsigned int xfer_bytes; in meson_mmc_irq_thread()
1029 if (WARN_ON(!cmd)) in meson_mmc_irq_thread()
1032 if (cmd->error) { in meson_mmc_irq_thread()
1034 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq_thread()
1039 data = cmd->data; in meson_mmc_irq_thread()
1041 xfer_bytes = data->blksz * data->blocks; in meson_mmc_irq_thread()
1042 WARN_ON(xfer_bytes > host->bounce_buf_size); in meson_mmc_irq_thread()
1043 if (host->dram_access_quirk) in meson_mmc_irq_thread()
1046 sg_copy_from_buffer(data->sg, data->sg_len, in meson_mmc_irq_thread()
1047 host->bounce_buf, xfer_bytes); in meson_mmc_irq_thread()
1050 next_cmd = meson_mmc_get_next_command(cmd); in meson_mmc_irq_thread()
1052 meson_mmc_start_cmd(host->mmc, next_cmd); in meson_mmc_irq_thread()
1054 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq_thread()
1063 static int meson_mmc_get_cd(struct mmc_host *mmc) in meson_mmc_get_cd()
1065 int status = mmc_gpio_get_cd(mmc); in meson_mmc_get_cd()
1067 if (status == -ENOSYS) in meson_mmc_get_cd()
1085 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_cfg_init()
1088 static int meson_mmc_card_busy(struct mmc_host *mmc) in meson_mmc_card_busy()
1093 regval = readl(host->regs + SD_EMMC_STATUS); in meson_mmc_card_busy()
1099 static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios) in meson_mmc_voltage_switch()
1101 int ret; in meson_mmc_voltage_switch()
1104 if (!IS_ERR(mmc->supply.vqmmc)) { in meson_mmc_voltage_switch()
1117 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) in meson_mmc_voltage_switch()
1120 return -EINVAL; in meson_mmc_voltage_switch()
1134 static int meson_mmc_probe(struct platform_device *pdev) in meson_mmc_probe()
1139 int ret; in meson_mmc_probe()
1141 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev); in meson_mmc_probe()
1143 return -ENOMEM; in meson_mmc_probe()
1145 host->mmc = mmc; in meson_mmc_probe()
1146 host->dev = &pdev->dev; in meson_mmc_probe()
1147 dev_set_drvdata(&pdev->dev, host); in meson_mmc_probe()
1150 host->dram_access_quirk = device_property_read_bool(&pdev->dev, in meson_mmc_probe()
1151 "amlogic,dram-access-quirk"); in meson_mmc_probe()
1154 host->vqmmc_enabled = false; in meson_mmc_probe()
1161 if (ret != -EPROBE_DEFER) in meson_mmc_probe()
1162 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret); in meson_mmc_probe()
1166 host->data = (struct meson_mmc_data *) in meson_mmc_probe()
1167 of_device_get_match_data(&pdev->dev); in meson_mmc_probe()
1168 if (!host->data) { in meson_mmc_probe()
1169 ret = -EINVAL; in meson_mmc_probe()
1173 ret = device_reset_optional(&pdev->dev); in meson_mmc_probe()
1175 return dev_err_probe(&pdev->dev, ret, "device reset failed\n"); in meson_mmc_probe()
1178 host->regs = devm_ioremap_resource(&pdev->dev, res); in meson_mmc_probe()
1179 if (IS_ERR(host->regs)) { in meson_mmc_probe()
1180 ret = PTR_ERR(host->regs); in meson_mmc_probe()
1184 host->irq = platform_get_irq(pdev, 0); in meson_mmc_probe()
1185 if (host->irq <= 0) { in meson_mmc_probe()
1186 ret = -EINVAL; in meson_mmc_probe()
1190 host->pinctrl = devm_pinctrl_get(&pdev->dev); in meson_mmc_probe()
1191 if (IS_ERR(host->pinctrl)) { in meson_mmc_probe()
1192 ret = PTR_ERR(host->pinctrl); in meson_mmc_probe()
1196 host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl, in meson_mmc_probe()
1197 "clk-gate"); in meson_mmc_probe()
1198 if (IS_ERR(host->pins_clk_gate)) { in meson_mmc_probe()
1199 dev_warn(&pdev->dev, in meson_mmc_probe()
1200 "can't get clk-gate pinctrl, using clk_stop bit\n"); in meson_mmc_probe()
1201 host->pins_clk_gate = NULL; in meson_mmc_probe()
1204 host->core_clk = devm_clk_get(&pdev->dev, "core"); in meson_mmc_probe()
1205 if (IS_ERR(host->core_clk)) { in meson_mmc_probe()
1206 ret = PTR_ERR(host->core_clk); in meson_mmc_probe()
1210 ret = clk_prepare_enable(host->core_clk); in meson_mmc_probe()
1222 writel(0, host->regs + SD_EMMC_START); in meson_mmc_probe()
1225 writel(0, host->regs + SD_EMMC_IRQ_EN); in meson_mmc_probe()
1227 host->regs + SD_EMMC_STATUS); in meson_mmc_probe()
1229 host->regs + SD_EMMC_IRQ_EN); in meson_mmc_probe()
1231 ret = request_threaded_irq(host->irq, meson_mmc_irq, in meson_mmc_probe()
1233 dev_name(&pdev->dev), host); in meson_mmc_probe()
1237 mmc->caps |= MMC_CAP_CMD23; in meson_mmc_probe()
1238 if (host->dram_access_quirk) { in meson_mmc_probe()
1240 mmc->max_segs = 1; in meson_mmc_probe()
1242 mmc->max_blk_count = SD_EMMC_SRAM_DATA_BUF_LEN / in meson_mmc_probe()
1243 mmc->max_blk_size; in meson_mmc_probe()
1245 mmc->max_blk_count = CMD_CFG_LENGTH_MASK; in meson_mmc_probe()
1246 mmc->max_segs = SD_EMMC_DESC_BUF_LEN / in meson_mmc_probe()
1249 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size; in meson_mmc_probe()
1250 mmc->max_seg_size = mmc->max_req_size; in meson_mmc_probe()
1253 * At the moment, we don't know how to reliably enable HS400. in meson_mmc_probe()
1257 mmc->caps2 &= ~MMC_CAP2_HS400; in meson_mmc_probe()
1259 if (host->dram_access_quirk) { in meson_mmc_probe()
1266 host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN; in meson_mmc_probe()
1267 host->bounce_iomem_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF; in meson_mmc_probe()
1268 host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF; in meson_mmc_probe()
1271 host->bounce_buf_size = mmc->max_req_size; in meson_mmc_probe()
1272 host->bounce_buf = in meson_mmc_probe()
1273 dma_alloc_coherent(host->dev, host->bounce_buf_size, in meson_mmc_probe()
1274 &host->bounce_dma_addr, GFP_KERNEL); in meson_mmc_probe()
1275 if (host->bounce_buf == NULL) { in meson_mmc_probe()
1276 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n"); in meson_mmc_probe()
1277 ret = -ENOMEM; in meson_mmc_probe()
1282 host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, in meson_mmc_probe()
1283 &host->descs_dma_addr, GFP_KERNEL); in meson_mmc_probe()
1284 if (!host->descs) { in meson_mmc_probe()
1285 dev_err(host->dev, "Allocating descriptor DMA buffer failed\n"); in meson_mmc_probe()
1286 ret = -ENOMEM; in meson_mmc_probe()
1290 mmc->ops = &meson_mmc_ops; in meson_mmc_probe()
1296 if (!host->dram_access_quirk) in meson_mmc_probe()
1297 dma_free_coherent(host->dev, host->bounce_buf_size, in meson_mmc_probe()
1298 host->bounce_buf, host->bounce_dma_addr); in meson_mmc_probe()
1300 free_irq(host->irq, host); in meson_mmc_probe()
1302 clk_disable_unprepare(host->mmc_clk); in meson_mmc_probe()
1304 clk_disable_unprepare(host->core_clk); in meson_mmc_probe()
1310 static int meson_mmc_remove(struct platform_device *pdev) in meson_mmc_remove()
1312 struct meson_host *host = dev_get_drvdata(&pdev->dev); in meson_mmc_remove()
1314 mmc_remove_host(host->mmc); in meson_mmc_remove()
1317 writel(0, host->regs + SD_EMMC_IRQ_EN); in meson_mmc_remove()
1318 free_irq(host->irq, host); in meson_mmc_remove()
1320 dma_free_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, in meson_mmc_remove()
1321 host->descs, host->descs_dma_addr); in meson_mmc_remove()
1323 if (!host->dram_access_quirk) in meson_mmc_remove()
1324 dma_free_coherent(host->dev, host->bounce_buf_size, in meson_mmc_remove()
1325 host->bounce_buf, host->bounce_dma_addr); in meson_mmc_remove()
1327 clk_disable_unprepare(host->mmc_clk); in meson_mmc_remove()
1328 clk_disable_unprepare(host->core_clk); in meson_mmc_remove()
1330 mmc_free_host(host->mmc); in meson_mmc_remove()
1349 { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data },
1350 { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data },
1351 { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data },
1352 { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data },
1353 { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data },