Lines Matching +full:mmc +full:-
1 // SPDX-License-Identifier: GPL-2.0-only
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>
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)
151 struct mmc_host *mmc; member
202 unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC; in meson_mmc_get_timeout_msecs()
214 if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error) in meson_mmc_get_next_command()
215 return cmd->mrq->cmd; in meson_mmc_get_next_command()
216 else if (mmc_op_multi(cmd->opcode) && in meson_mmc_get_next_command()
217 (!cmd->mrq->sbc || cmd->error || cmd->data->error)) in meson_mmc_get_next_command()
218 return cmd->mrq->stop; in meson_mmc_get_next_command()
223 static void meson_mmc_get_transfer_mode(struct mmc_host *mmc, in meson_mmc_get_transfer_mode() argument
226 struct meson_host *host = mmc_priv(mmc); in meson_mmc_get_transfer_mode()
227 struct mmc_data *data = mrq->data; in meson_mmc_get_transfer_mode()
237 if (host->dram_access_quirk) in meson_mmc_get_transfer_mode()
241 * Broken SDIO with AP6255-based WiFi on Khadas VIM Pro has been in meson_mmc_get_transfer_mode()
246 if (mrq->cmd->opcode == SD_IO_RW_EXTENDED) in meson_mmc_get_transfer_mode()
249 for_each_sg(data->sg, sg, data->sg_len, i) in meson_mmc_get_transfer_mode()
251 if (sg->offset & 7) { in meson_mmc_get_transfer_mode()
258 data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE; in meson_mmc_get_transfer_mode()
263 return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE; in meson_mmc_desc_chain_mode()
268 return data && data->flags & MMC_DATA_READ && in meson_mmc_bounce_buf_read()
272 static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) in meson_mmc_pre_req() argument
274 struct mmc_data *data = mrq->data; in meson_mmc_pre_req()
279 meson_mmc_get_transfer_mode(mmc, mrq); in meson_mmc_pre_req()
280 data->host_cookie |= SD_EMMC_PRE_REQ_DONE; in meson_mmc_pre_req()
285 data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, in meson_mmc_pre_req()
287 if (!data->sg_count) in meson_mmc_pre_req()
288 dev_err(mmc_dev(mmc), "dma_map_sg failed"); in meson_mmc_pre_req()
291 static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, in meson_mmc_post_req() argument
294 struct mmc_data *data = mrq->data; in meson_mmc_post_req()
296 if (data && meson_mmc_desc_chain_mode(data) && data->sg_count) in meson_mmc_post_req()
297 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, in meson_mmc_post_req()
302 * Gating the clock on this controller is tricky. It seems the mmc clock
311 if (host->pins_clk_gate) { in meson_mmc_clk_gate()
312 pinctrl_select_state(host->pinctrl, host->pins_clk_gate); in meson_mmc_clk_gate()
315 * If the pinmux is not provided - default to the classic and in meson_mmc_clk_gate()
318 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_gate()
320 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_gate()
328 if (host->pins_clk_gate) in meson_mmc_clk_ungate()
329 pinctrl_select_default_state(host->dev); in meson_mmc_clk_ungate()
332 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_ungate()
334 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_ungate()
340 struct mmc_host *mmc = host->mmc; in meson_mmc_clk_set() local
344 /* Same request - bail-out */ in meson_mmc_clk_set()
345 if (host->ddr == ddr && host->req_rate == rate) in meson_mmc_clk_set()
350 host->req_rate = 0; in meson_mmc_clk_set()
351 mmc->actual_clock = 0; in meson_mmc_clk_set()
358 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
360 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
369 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_clk_set()
370 host->ddr = ddr; in meson_mmc_clk_set()
372 ret = clk_set_rate(host->mmc_clk, rate); in meson_mmc_clk_set()
374 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n", in meson_mmc_clk_set()
379 host->req_rate = rate; in meson_mmc_clk_set()
380 mmc->actual_clock = clk_get_rate(host->mmc_clk); in meson_mmc_clk_set()
384 host->req_rate >>= 1; in meson_mmc_clk_set()
385 mmc->actual_clock >>= 1; in meson_mmc_clk_set()
388 dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock); in meson_mmc_clk_set()
389 if (rate != mmc->actual_clock) in meson_mmc_clk_set()
390 dev_dbg(host->dev, "requested rate was %lu\n", rate); in meson_mmc_clk_set()
400 * generating the MMC clock. Use the clock framework to create and
420 writel(clk_reg, host->regs + SD_EMMC_CLOCK); in meson_mmc_clk_init()
428 clk = devm_clk_get(host->dev, name); in meson_mmc_clk_init()
430 return dev_err_probe(host->dev, PTR_ERR(clk), in meson_mmc_clk_init()
437 mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL); in meson_mmc_clk_init()
439 return -ENOMEM; in meson_mmc_clk_init()
441 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev)); in meson_mmc_clk_init()
448 mux->reg = host->regs + SD_EMMC_CLOCK; in meson_mmc_clk_init()
449 mux->shift = __ffs(CLK_SRC_MASK); in meson_mmc_clk_init()
450 mux->mask = CLK_SRC_MASK >> mux->shift; in meson_mmc_clk_init()
451 mux->hw.init = &init; in meson_mmc_clk_init()
453 host->mux_clk = devm_clk_register(host->dev, &mux->hw); in meson_mmc_clk_init()
454 if (WARN_ON(IS_ERR(host->mux_clk))) in meson_mmc_clk_init()
455 return PTR_ERR(host->mux_clk); in meson_mmc_clk_init()
458 div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL); in meson_mmc_clk_init()
460 return -ENOMEM; in meson_mmc_clk_init()
462 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev)); in meson_mmc_clk_init()
466 clk_parent[0] = __clk_get_name(host->mux_clk); in meson_mmc_clk_init()
470 div->reg = host->regs + SD_EMMC_CLOCK; in meson_mmc_clk_init()
471 div->shift = __ffs(CLK_DIV_MASK); in meson_mmc_clk_init()
472 div->width = __builtin_popcountl(CLK_DIV_MASK); in meson_mmc_clk_init()
473 div->hw.init = &init; in meson_mmc_clk_init()
474 div->flags = CLK_DIVIDER_ONE_BASED; in meson_mmc_clk_init()
476 host->mmc_clk = devm_clk_register(host->dev, &div->hw); in meson_mmc_clk_init()
477 if (WARN_ON(IS_ERR(host->mmc_clk))) in meson_mmc_clk_init()
478 return PTR_ERR(host->mmc_clk); in meson_mmc_clk_init()
481 host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000); in meson_mmc_clk_init()
482 ret = clk_set_rate(host->mmc_clk, host->mmc->f_min); in meson_mmc_clk_init()
486 return clk_prepare_enable(host->mmc_clk); in meson_mmc_clk_init()
491 unsigned int val = readl(host->regs + host->data->adjust); in meson_mmc_disable_resampling()
494 writel(val, host->regs + host->data->adjust); in meson_mmc_disable_resampling()
503 val = readl(host->regs + host->data->adjust); in meson_mmc_reset_resampling()
505 writel(val, host->regs + host->data->adjust); in meson_mmc_reset_resampling()
508 static int meson_mmc_resampling_tuning(struct mmc_host *mmc, u32 opcode) in meson_mmc_resampling_tuning() argument
510 struct meson_host *host = mmc_priv(mmc); in meson_mmc_resampling_tuning()
515 max_dly = DIV_ROUND_UP(clk_get_rate(host->mux_clk), in meson_mmc_resampling_tuning()
516 clk_get_rate(host->mmc_clk)); in meson_mmc_resampling_tuning()
518 val = readl(host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
520 writel(val, host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
522 if (mmc_doing_retune(mmc)) in meson_mmc_resampling_tuning()
530 writel(val, host->regs + host->data->adjust); in meson_mmc_resampling_tuning()
532 ret = mmc_send_tuning(mmc, opcode, NULL); in meson_mmc_resampling_tuning()
534 dev_dbg(mmc_dev(mmc), "resampling delay: %u\n", in meson_mmc_resampling_tuning()
541 return -EIO; in meson_mmc_resampling_tuning()
549 switch (ios->timing) { in meson_mmc_prepare_ios_clock()
560 return meson_mmc_clk_set(host, ios->clock, ddr); in meson_mmc_prepare_ios_clock()
566 switch (ios->timing) { in meson_mmc_check_resampling()
576 static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) in meson_mmc_set_ios() argument
578 struct meson_host *host = mmc_priv(mmc); in meson_mmc_set_ios()
586 switch (ios->power_mode) { in meson_mmc_set_ios()
588 if (!IS_ERR(mmc->supply.vmmc)) in meson_mmc_set_ios()
589 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); in meson_mmc_set_ios()
591 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { in meson_mmc_set_ios()
592 regulator_disable(mmc->supply.vqmmc); in meson_mmc_set_ios()
593 host->vqmmc_enabled = false; in meson_mmc_set_ios()
599 if (!IS_ERR(mmc->supply.vmmc)) in meson_mmc_set_ios()
600 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); in meson_mmc_set_ios()
605 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { in meson_mmc_set_ios()
606 int ret = regulator_enable(mmc->supply.vqmmc); in meson_mmc_set_ios()
609 dev_err(host->dev, in meson_mmc_set_ios()
612 host->vqmmc_enabled = true; in meson_mmc_set_ios()
619 switch (ios->bus_width) { in meson_mmc_set_ios()
630 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n", in meson_mmc_set_ios()
631 ios->bus_width); in meson_mmc_set_ios()
635 val = readl(host->regs + SD_EMMC_CFG); in meson_mmc_set_ios()
638 writel(val, host->regs + SD_EMMC_CFG); in meson_mmc_set_ios()
643 dev_err(host->dev, "Failed to set clock: %d\n,", err); in meson_mmc_set_ios()
645 dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val); in meson_mmc_set_ios()
648 static void meson_mmc_request_done(struct mmc_host *mmc, in meson_mmc_request_done() argument
651 struct meson_host *host = mmc_priv(mmc); in meson_mmc_request_done()
653 host->cmd = NULL; in meson_mmc_request_done()
654 mmc_request_done(host->mmc, mrq); in meson_mmc_request_done()
657 static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz) in meson_mmc_set_blksz() argument
659 struct meson_host *host = mmc_priv(mmc); in meson_mmc_set_blksz()
662 cfg = readl(host->regs + SD_EMMC_CFG); in meson_mmc_set_blksz()
666 dev_err(host->dev, "blksz %u is not a power of 2\n", blksz); in meson_mmc_set_blksz()
670 /* check if block-size matches, if not update */ in meson_mmc_set_blksz()
674 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__, in meson_mmc_set_blksz()
679 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_set_blksz()
684 if (cmd->flags & MMC_RSP_PRESENT) { in meson_mmc_set_response_bits()
685 if (cmd->flags & MMC_RSP_136) in meson_mmc_set_response_bits()
689 if (!(cmd->flags & MMC_RSP_CRC)) in meson_mmc_set_response_bits()
692 if (cmd->flags & MMC_RSP_BUSY) in meson_mmc_set_response_bits()
699 static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg) in meson_mmc_desc_chain_transfer() argument
701 struct meson_host *host = mmc_priv(mmc); in meson_mmc_desc_chain_transfer()
702 struct sd_emmc_desc *desc = host->descs; in meson_mmc_desc_chain_transfer()
703 struct mmc_data *data = host->cmd->data; in meson_mmc_desc_chain_transfer()
708 if (data->flags & MMC_DATA_WRITE) in meson_mmc_desc_chain_transfer()
711 if (data->blocks > 1) { in meson_mmc_desc_chain_transfer()
713 meson_mmc_set_blksz(mmc, data->blksz); in meson_mmc_desc_chain_transfer()
716 for_each_sg(data->sg, sg, data->sg_count, i) { in meson_mmc_desc_chain_transfer()
719 if (data->blocks > 1) in meson_mmc_desc_chain_transfer()
720 len /= data->blksz; in meson_mmc_desc_chain_transfer()
726 desc[i].cmd_arg = host->cmd->arg; in meson_mmc_desc_chain_transfer()
730 desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN; in meson_mmc_desc_chain_transfer()
733 start = host->descs_dma_addr | START_DESC_BUSY; in meson_mmc_desc_chain_transfer()
734 writel(start, host->regs + SD_EMMC_START); in meson_mmc_desc_chain_transfer()
737 static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd) in meson_mmc_start_cmd() argument
739 struct meson_host *host = mmc_priv(mmc); in meson_mmc_start_cmd()
740 struct mmc_data *data = cmd->data; in meson_mmc_start_cmd()
747 host->cmd = cmd; in meson_mmc_start_cmd()
749 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode); in meson_mmc_start_cmd()
757 data->bytes_xfered = 0; in meson_mmc_start_cmd()
763 meson_mmc_desc_chain_transfer(mmc, cmd_cfg); in meson_mmc_start_cmd()
767 if (data->blocks > 1) { in meson_mmc_start_cmd()
770 data->blocks); in meson_mmc_start_cmd()
771 meson_mmc_set_blksz(mmc, data->blksz); in meson_mmc_start_cmd()
773 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz); in meson_mmc_start_cmd()
776 xfer_bytes = data->blksz * data->blocks; in meson_mmc_start_cmd()
777 if (data->flags & MMC_DATA_WRITE) { in meson_mmc_start_cmd()
779 WARN_ON(xfer_bytes > host->bounce_buf_size); in meson_mmc_start_cmd()
780 sg_copy_to_buffer(data->sg, data->sg_len, in meson_mmc_start_cmd()
781 host->bounce_buf, xfer_bytes); in meson_mmc_start_cmd()
785 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK; in meson_mmc_start_cmd()
793 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG); in meson_mmc_start_cmd()
794 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT); in meson_mmc_start_cmd()
795 writel(0, host->regs + SD_EMMC_CMD_RSP); in meson_mmc_start_cmd()
797 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG); in meson_mmc_start_cmd()
800 static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) in meson_mmc_request() argument
802 struct meson_host *host = mmc_priv(mmc); in meson_mmc_request()
803 bool needs_pre_post_req = mrq->data && in meson_mmc_request()
804 !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE); in meson_mmc_request()
807 meson_mmc_get_transfer_mode(mmc, mrq); in meson_mmc_request()
808 if (!meson_mmc_desc_chain_mode(mrq->data)) in meson_mmc_request()
813 meson_mmc_pre_req(mmc, mrq); in meson_mmc_request()
816 writel(0, host->regs + SD_EMMC_START); in meson_mmc_request()
818 meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd); in meson_mmc_request()
821 meson_mmc_post_req(mmc, mrq, 0); in meson_mmc_request()
824 static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd) in meson_mmc_read_resp() argument
826 struct meson_host *host = mmc_priv(mmc); in meson_mmc_read_resp()
828 if (cmd->flags & MMC_RSP_136) { in meson_mmc_read_resp()
829 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3); in meson_mmc_read_resp()
830 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2); in meson_mmc_read_resp()
831 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1); in meson_mmc_read_resp()
832 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP); in meson_mmc_read_resp()
833 } else if (cmd->flags & MMC_RSP_PRESENT) { in meson_mmc_read_resp()
834 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP); in meson_mmc_read_resp()
846 irq_en = readl(host->regs + SD_EMMC_IRQ_EN); in meson_mmc_irq()
847 raw_status = readl(host->regs + SD_EMMC_STATUS); in meson_mmc_irq()
851 dev_dbg(host->dev, in meson_mmc_irq()
852 "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n", in meson_mmc_irq()
857 if (WARN_ON(!host) || WARN_ON(!host->cmd)) in meson_mmc_irq()
861 writel(status, host->regs + SD_EMMC_STATUS); in meson_mmc_irq()
863 cmd = host->cmd; in meson_mmc_irq()
864 data = cmd->data; in meson_mmc_irq()
865 cmd->error = 0; in meson_mmc_irq()
867 dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status); in meson_mmc_irq()
868 cmd->error = -EILSEQ; in meson_mmc_irq()
874 dev_dbg(host->dev, "Timeout - status 0x%08x\n", status); in meson_mmc_irq()
875 cmd->error = -ETIMEDOUT; in meson_mmc_irq()
880 meson_mmc_read_resp(host->mmc, cmd); in meson_mmc_irq()
883 dev_dbg(host->dev, "IRQ: SDIO TODO.\n"); in meson_mmc_irq()
888 if (data && !cmd->error) in meson_mmc_irq()
889 data->bytes_xfered = data->blksz * data->blocks; in meson_mmc_irq()
898 if (cmd->error) { in meson_mmc_irq()
900 u32 start = readl(host->regs + SD_EMMC_START); in meson_mmc_irq()
903 writel(start, host->regs + SD_EMMC_START); in meson_mmc_irq()
907 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq()
924 return readl_poll_timeout(host->regs + SD_EMMC_STATUS, status, in meson_mmc_wait_desc_stop()
932 struct mmc_command *next_cmd, *cmd = host->cmd; in meson_mmc_irq_thread()
939 if (cmd->error) { in meson_mmc_irq_thread()
941 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq_thread()
946 data = cmd->data; in meson_mmc_irq_thread()
948 xfer_bytes = data->blksz * data->blocks; in meson_mmc_irq_thread()
949 WARN_ON(xfer_bytes > host->bounce_buf_size); in meson_mmc_irq_thread()
950 sg_copy_from_buffer(data->sg, data->sg_len, in meson_mmc_irq_thread()
951 host->bounce_buf, xfer_bytes); in meson_mmc_irq_thread()
956 meson_mmc_start_cmd(host->mmc, next_cmd); in meson_mmc_irq_thread()
958 meson_mmc_request_done(host->mmc, cmd->mrq); in meson_mmc_irq_thread()
965 * interrupts. For now, the MMC core will use this for polling.
967 static int meson_mmc_get_cd(struct mmc_host *mmc) in meson_mmc_get_cd() argument
969 int status = mmc_gpio_get_cd(mmc); in meson_mmc_get_cd()
971 if (status == -ENOSYS) in meson_mmc_get_cd()
989 writel(cfg, host->regs + SD_EMMC_CFG); in meson_mmc_cfg_init()
992 static int meson_mmc_card_busy(struct mmc_host *mmc) in meson_mmc_card_busy() argument
994 struct meson_host *host = mmc_priv(mmc); in meson_mmc_card_busy()
997 regval = readl(host->regs + SD_EMMC_STATUS); in meson_mmc_card_busy()
1003 static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios) in meson_mmc_voltage_switch() argument
1008 if (!IS_ERR(mmc->supply.vqmmc)) { in meson_mmc_voltage_switch()
1016 ret = mmc_regulator_set_vqmmc(mmc, ios); in meson_mmc_voltage_switch()
1021 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) in meson_mmc_voltage_switch()
1024 return -EINVAL; in meson_mmc_voltage_switch()
1042 struct mmc_host *mmc; in meson_mmc_probe() local
1045 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev); in meson_mmc_probe()
1046 if (!mmc) in meson_mmc_probe()
1047 return -ENOMEM; in meson_mmc_probe()
1048 host = mmc_priv(mmc); in meson_mmc_probe()
1049 host->mmc = mmc; in meson_mmc_probe()
1050 host->dev = &pdev->dev; in meson_mmc_probe()
1051 dev_set_drvdata(&pdev->dev, host); in meson_mmc_probe()
1054 host->dram_access_quirk = device_property_read_bool(&pdev->dev, in meson_mmc_probe()
1055 "amlogic,dram-access-quirk"); in meson_mmc_probe()
1058 host->vqmmc_enabled = false; in meson_mmc_probe()
1059 ret = mmc_regulator_get_supply(mmc); in meson_mmc_probe()
1063 ret = mmc_of_parse(mmc); in meson_mmc_probe()
1065 if (ret != -EPROBE_DEFER) in meson_mmc_probe()
1066 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret); in meson_mmc_probe()
1070 host->data = (struct meson_mmc_data *) in meson_mmc_probe()
1071 of_device_get_match_data(&pdev->dev); in meson_mmc_probe()
1072 if (!host->data) { in meson_mmc_probe()
1073 ret = -EINVAL; in meson_mmc_probe()
1077 ret = device_reset_optional(&pdev->dev); in meson_mmc_probe()
1079 return dev_err_probe(&pdev->dev, ret, "device reset failed\n"); in meson_mmc_probe()
1082 host->regs = devm_ioremap_resource(&pdev->dev, res); in meson_mmc_probe()
1083 if (IS_ERR(host->regs)) { in meson_mmc_probe()
1084 ret = PTR_ERR(host->regs); in meson_mmc_probe()
1088 host->irq = platform_get_irq(pdev, 0); in meson_mmc_probe()
1089 if (host->irq <= 0) { in meson_mmc_probe()
1090 ret = -EINVAL; in meson_mmc_probe()
1094 host->pinctrl = devm_pinctrl_get(&pdev->dev); in meson_mmc_probe()
1095 if (IS_ERR(host->pinctrl)) { in meson_mmc_probe()
1096 ret = PTR_ERR(host->pinctrl); in meson_mmc_probe()
1100 host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl, in meson_mmc_probe()
1101 "clk-gate"); in meson_mmc_probe()
1102 if (IS_ERR(host->pins_clk_gate)) { in meson_mmc_probe()
1103 dev_warn(&pdev->dev, in meson_mmc_probe()
1104 "can't get clk-gate pinctrl, using clk_stop bit\n"); in meson_mmc_probe()
1105 host->pins_clk_gate = NULL; in meson_mmc_probe()
1108 host->core_clk = devm_clk_get(&pdev->dev, "core"); in meson_mmc_probe()
1109 if (IS_ERR(host->core_clk)) { in meson_mmc_probe()
1110 ret = PTR_ERR(host->core_clk); in meson_mmc_probe()
1114 ret = clk_prepare_enable(host->core_clk); in meson_mmc_probe()
1126 writel(0, host->regs + SD_EMMC_START); in meson_mmc_probe()
1129 writel(0, host->regs + SD_EMMC_IRQ_EN); in meson_mmc_probe()
1131 host->regs + SD_EMMC_STATUS); in meson_mmc_probe()
1133 host->regs + SD_EMMC_IRQ_EN); in meson_mmc_probe()
1135 ret = request_threaded_irq(host->irq, meson_mmc_irq, in meson_mmc_probe()
1137 dev_name(&pdev->dev), host); in meson_mmc_probe()
1141 mmc->caps |= MMC_CAP_CMD23; in meson_mmc_probe()
1142 if (host->dram_access_quirk) { in meson_mmc_probe()
1144 mmc->max_segs = 1; in meson_mmc_probe()
1146 mmc->max_blk_count = SD_EMMC_SRAM_DATA_BUF_LEN / in meson_mmc_probe()
1147 mmc->max_blk_size; in meson_mmc_probe()
1149 mmc->max_blk_count = CMD_CFG_LENGTH_MASK; in meson_mmc_probe()
1150 mmc->max_segs = SD_EMMC_DESC_BUF_LEN / in meson_mmc_probe()
1153 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size; in meson_mmc_probe()
1154 mmc->max_seg_size = mmc->max_req_size; in meson_mmc_probe()
1161 mmc->caps2 &= ~MMC_CAP2_HS400; in meson_mmc_probe()
1163 if (host->dram_access_quirk) { in meson_mmc_probe()
1165 * The MMC Controller embeds 1,5KiB of internal SRAM in meson_mmc_probe()
1170 host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN; in meson_mmc_probe()
1171 host->bounce_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF; in meson_mmc_probe()
1172 host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF; in meson_mmc_probe()
1175 host->bounce_buf_size = mmc->max_req_size; in meson_mmc_probe()
1176 host->bounce_buf = in meson_mmc_probe()
1177 dma_alloc_coherent(host->dev, host->bounce_buf_size, in meson_mmc_probe()
1178 &host->bounce_dma_addr, GFP_KERNEL); in meson_mmc_probe()
1179 if (host->bounce_buf == NULL) { in meson_mmc_probe()
1180 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n"); in meson_mmc_probe()
1181 ret = -ENOMEM; in meson_mmc_probe()
1186 host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, in meson_mmc_probe()
1187 &host->descs_dma_addr, GFP_KERNEL); in meson_mmc_probe()
1188 if (!host->descs) { in meson_mmc_probe()
1189 dev_err(host->dev, "Allocating descriptor DMA buffer failed\n"); in meson_mmc_probe()
1190 ret = -ENOMEM; in meson_mmc_probe()
1194 mmc->ops = &meson_mmc_ops; in meson_mmc_probe()
1195 mmc_add_host(mmc); in meson_mmc_probe()
1200 if (!host->dram_access_quirk) in meson_mmc_probe()
1201 dma_free_coherent(host->dev, host->bounce_buf_size, in meson_mmc_probe()
1202 host->bounce_buf, host->bounce_dma_addr); in meson_mmc_probe()
1204 free_irq(host->irq, host); in meson_mmc_probe()
1206 clk_disable_unprepare(host->mmc_clk); in meson_mmc_probe()
1208 clk_disable_unprepare(host->core_clk); in meson_mmc_probe()
1210 mmc_free_host(mmc); in meson_mmc_probe()
1216 struct meson_host *host = dev_get_drvdata(&pdev->dev); in meson_mmc_remove()
1218 mmc_remove_host(host->mmc); in meson_mmc_remove()
1221 writel(0, host->regs + SD_EMMC_IRQ_EN); in meson_mmc_remove()
1222 free_irq(host->irq, host); in meson_mmc_remove()
1224 dma_free_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, in meson_mmc_remove()
1225 host->descs, host->descs_dma_addr); in meson_mmc_remove()
1227 if (!host->dram_access_quirk) in meson_mmc_remove()
1228 dma_free_coherent(host->dev, host->bounce_buf_size, in meson_mmc_remove()
1229 host->bounce_buf, host->bounce_dma_addr); in meson_mmc_remove()
1231 clk_disable_unprepare(host->mmc_clk); in meson_mmc_remove()
1232 clk_disable_unprepare(host->core_clk); in meson_mmc_remove()
1234 mmc_free_host(host->mmc); in meson_mmc_remove()
1253 { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data },
1254 { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data },
1255 { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data },
1256 { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data },
1257 { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data },