Lines Matching +full:mmci +full:- +full:gpio +full:- +full:supply

1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
24 #include <linux/dma-mapping.h>
35 #include <linux/mmc/slot-gpio.h>
39 #include <linux/platform_data/mmc-mxcmmc.h>
41 #include <linux/dma/imx-dma.h>
43 #define DRIVER_NAME "mxc-mmc"
161 .compatible = "fsl,imx21-mmc",
164 .compatible = "fsl,imx31-mmc",
167 .compatible = "fsl,mpc5121-sdhc",
177 return host->devtype == IMX31_MMC; in is_imx31_mmc()
182 return host->devtype == MPC512X_MMC; in is_mpc512x_mmc()
188 return ioread32be(host->base + reg); in mxcmci_readl()
190 return readl(host->base + reg); in mxcmci_readl()
196 iowrite32be(val, host->base + reg); in mxcmci_writel()
198 writel(val, host->base + reg); in mxcmci_writel()
204 return ioread32be(host->base + reg); in mxcmci_readw()
206 return readw(host->base + reg); in mxcmci_readw()
212 iowrite32be(val, host->base + reg); in mxcmci_writew()
214 writew(val, host->base + reg); in mxcmci_writew()
221 if (!IS_ERR(host->mmc->supply.vmmc)) { in mxcmci_set_power()
222 if (host->power_mode == MMC_POWER_UP) in mxcmci_set_power()
223 mmc_regulator_set_ocr(host->mmc, in mxcmci_set_power()
224 host->mmc->supply.vmmc, vdd); in mxcmci_set_power()
225 else if (host->power_mode == MMC_POWER_OFF) in mxcmci_set_power()
226 mmc_regulator_set_ocr(host->mmc, in mxcmci_set_power()
227 host->mmc->supply.vmmc, 0); in mxcmci_set_power()
230 if (host->pdata && host->pdata->setpower) in mxcmci_set_power()
231 host->pdata->setpower(mmc_dev(host->mmc), vdd); in mxcmci_set_power()
236 return host->do_dma; in mxcmci_use_dma()
243 dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n"); in mxcmci_softreset()
272 for_each_sg(data->sg, sg, data->sg_len, i) in mxcmci_swap_buffers()
273 buffer_swap32(sg_virt(sg), sg->length); in mxcmci_swap_buffers()
281 unsigned int nob = data->blocks; in mxcmci_setup_data()
282 unsigned int blksz = data->blksz; in mxcmci_setup_data()
288 host->data = data; in mxcmci_setup_data()
289 data->bytes_xfered = 0; in mxcmci_setup_data()
293 host->datasize = datasize; in mxcmci_setup_data()
298 for_each_sg(data->sg, sg, data->sg_len, i) { in mxcmci_setup_data()
299 if (sg->offset & 3 || sg->length & 3 || sg->length < 512) { in mxcmci_setup_data()
300 host->do_dma = 0; in mxcmci_setup_data()
305 if (data->flags & MMC_DATA_READ) { in mxcmci_setup_data()
306 host->dma_dir = DMA_FROM_DEVICE; in mxcmci_setup_data()
309 host->dma_dir = DMA_TO_DEVICE; in mxcmci_setup_data()
315 nents = dma_map_sg(host->dma->device->dev, data->sg, in mxcmci_setup_data()
316 data->sg_len, host->dma_dir); in mxcmci_setup_data()
317 if (nents != data->sg_len) in mxcmci_setup_data()
318 return -EINVAL; in mxcmci_setup_data()
320 host->desc = dmaengine_prep_slave_sg(host->dma, in mxcmci_setup_data()
321 data->sg, data->sg_len, slave_dirn, in mxcmci_setup_data()
324 if (!host->desc) { in mxcmci_setup_data()
325 dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, in mxcmci_setup_data()
326 host->dma_dir); in mxcmci_setup_data()
327 host->do_dma = 0; in mxcmci_setup_data()
332 dmaengine_submit(host->desc); in mxcmci_setup_data()
333 dma_async_issue_pending(host->dma); in mxcmci_setup_data()
335 mod_timer(&host->watchdog, jiffies + msecs_to_jiffies(MXCMCI_TIMEOUT_MS)); in mxcmci_setup_data()
348 del_timer(&host->watchdog); in mxcmci_dma_callback()
352 dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat); in mxcmci_dma_callback()
360 u32 int_cntr = host->default_irq_mask; in mxcmci_start_cmd()
363 WARN_ON(host->cmd != NULL); in mxcmci_start_cmd()
364 host->cmd = cmd; in mxcmci_start_cmd()
380 dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n", in mxcmci_start_cmd()
382 cmd->error = -EINVAL; in mxcmci_start_cmd()
383 return -EINVAL; in mxcmci_start_cmd()
389 if (host->dma_dir == DMA_FROM_DEVICE) { in mxcmci_start_cmd()
390 host->desc->callback = mxcmci_dma_callback; in mxcmci_start_cmd()
391 host->desc->callback_param = host; in mxcmci_start_cmd()
397 spin_lock_irqsave(&host->lock, flags); in mxcmci_start_cmd()
398 if (host->use_sdio) in mxcmci_start_cmd()
401 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_start_cmd()
403 mxcmci_writew(host, cmd->opcode, MMC_REG_CMD); in mxcmci_start_cmd()
404 mxcmci_writel(host, cmd->arg, MMC_REG_ARG); in mxcmci_start_cmd()
413 u32 int_cntr = host->default_irq_mask; in mxcmci_finish_request()
416 spin_lock_irqsave(&host->lock, flags); in mxcmci_finish_request()
417 if (host->use_sdio) in mxcmci_finish_request()
420 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_finish_request()
422 host->req = NULL; in mxcmci_finish_request()
423 host->cmd = NULL; in mxcmci_finish_request()
424 host->data = NULL; in mxcmci_finish_request()
426 mmc_request_done(host->mmc, req); in mxcmci_finish_request()
431 struct mmc_data *data = host->data; in mxcmci_finish_data()
435 dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len, in mxcmci_finish_data()
436 host->dma_dir); in mxcmci_finish_data()
441 dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n", in mxcmci_finish_data()
444 dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__); in mxcmci_finish_data()
445 data->error = -EILSEQ; in mxcmci_finish_data()
449 dev_err(mmc_dev(host->mmc), in mxcmci_finish_data()
450 "%s: No CRC -ETIMEDOUT\n", __func__); in mxcmci_finish_data()
451 data->error = -ETIMEDOUT; in mxcmci_finish_data()
453 dev_err(mmc_dev(host->mmc), in mxcmci_finish_data()
454 "%s: -EILSEQ\n", __func__); in mxcmci_finish_data()
455 data->error = -EILSEQ; in mxcmci_finish_data()
458 dev_err(mmc_dev(host->mmc), in mxcmci_finish_data()
459 "%s: read -ETIMEDOUT\n", __func__); in mxcmci_finish_data()
460 data->error = -ETIMEDOUT; in mxcmci_finish_data()
462 dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__); in mxcmci_finish_data()
463 data->error = -EIO; in mxcmci_finish_data()
466 data->bytes_xfered = host->datasize; in mxcmci_finish_data()
469 data_error = data->error; in mxcmci_finish_data()
471 host->data = NULL; in mxcmci_finish_data()
478 struct mmc_command *cmd = host->cmd; in mxcmci_read_response()
486 dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n"); in mxcmci_read_response()
487 cmd->error = -ETIMEDOUT; in mxcmci_read_response()
488 } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) { in mxcmci_read_response()
489 dev_dbg(mmc_dev(host->mmc), "cmd crc error\n"); in mxcmci_read_response()
490 cmd->error = -EILSEQ; in mxcmci_read_response()
493 if (cmd->flags & MMC_RSP_PRESENT) { in mxcmci_read_response()
494 if (cmd->flags & MMC_RSP_136) { in mxcmci_read_response()
498 cmd->resp[i] = a << 16 | b; in mxcmci_read_response()
504 cmd->resp[0] = a << 24 | b << 8 | c >> 8; in mxcmci_read_response()
520 mxcmci_set_clk_rate(host, host->clock); in mxcmci_poll_status()
540 bytes -= 4; in mxcmci_pull()
568 bytes -= 4; in mxcmci_push()
588 struct mmc_data *data = host->req->data; in mxcmci_transfer_data()
592 host->data = data; in mxcmci_transfer_data()
593 host->datasize = 0; in mxcmci_transfer_data()
595 if (data->flags & MMC_DATA_READ) { in mxcmci_transfer_data()
596 for_each_sg(data->sg, sg, data->sg_len, i) { in mxcmci_transfer_data()
597 stat = mxcmci_pull(host, sg_virt(sg), sg->length); in mxcmci_transfer_data()
600 host->datasize += sg->length; in mxcmci_transfer_data()
603 for_each_sg(data->sg, sg, data->sg_len, i) { in mxcmci_transfer_data()
604 stat = mxcmci_push(host, sg_virt(sg), sg->length); in mxcmci_transfer_data()
607 host->datasize += sg->length; in mxcmci_transfer_data()
626 if (host->req->stop) { in mxcmci_datawork()
627 if (mxcmci_start_cmd(host, host->req->stop, 0)) { in mxcmci_datawork()
628 mxcmci_finish_request(host, host->req); in mxcmci_datawork()
632 mxcmci_finish_request(host, host->req); in mxcmci_datawork()
642 spin_lock_irqsave(&host->lock, flags); in mxcmci_data_done()
644 if (!host->data) { in mxcmci_data_done()
645 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_data_done()
649 if (!host->req) { in mxcmci_data_done()
650 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_data_done()
654 req = host->req; in mxcmci_data_done()
655 if (!req->stop) in mxcmci_data_done()
656 host->req = NULL; /* we will handle finish req below */ in mxcmci_data_done()
660 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_data_done()
666 host->cmd = NULL; in mxcmci_data_done()
668 if (req->stop) { in mxcmci_data_done()
669 if (mxcmci_start_cmd(host, req->stop, 0)) { in mxcmci_data_done()
681 host->cmd = NULL; in mxcmci_cmd_done()
683 if (!host->data && host->req) { in mxcmci_cmd_done()
684 mxcmci_finish_request(host, host->req); in mxcmci_cmd_done()
692 if (!mxcmci_use_dma(host) && host->data) in mxcmci_cmd_done()
693 schedule_work(&host->datawork); in mxcmci_cmd_done()
709 dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat); in mxcmci_irq()
711 spin_lock(&host->lock); in mxcmci_irq()
712 sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio; in mxcmci_irq()
713 spin_unlock(&host->lock); in mxcmci_irq()
720 mmc_signal_sdio_irq(host->mmc); in mxcmci_irq()
727 del_timer(&host->watchdog); in mxcmci_irq()
731 if (host->default_irq_mask && in mxcmci_irq()
733 mmc_detect_change(host->mmc, msecs_to_jiffies(200)); in mxcmci_irq()
741 unsigned int cmdat = host->cmdat; in mxcmci_request()
744 WARN_ON(host->req != NULL); in mxcmci_request()
746 host->req = req; in mxcmci_request()
747 host->cmdat &= ~CMD_DAT_CONT_INIT; in mxcmci_request()
749 if (host->dma) in mxcmci_request()
750 host->do_dma = 1; in mxcmci_request()
752 if (req->data) { in mxcmci_request()
753 error = mxcmci_setup_data(host, req->data); in mxcmci_request()
755 req->cmd->error = error; in mxcmci_request()
762 if (req->data->flags & MMC_DATA_WRITE) in mxcmci_request()
766 error = mxcmci_start_cmd(host, req->cmd, cmdat); in mxcmci_request()
777 unsigned int clk_in = clk_get_rate(host->clk_per); in mxcmci_set_clk_rate()
802 dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n", in mxcmci_set_clk_rate()
809 struct dma_slave_config *config = &host->dma_slave_config; in mxcmci_setup_dma()
811 config->dst_addr = host->phys_base + MMC_REG_BUFFER_ACCESS; in mxcmci_setup_dma()
812 config->src_addr = host->phys_base + MMC_REG_BUFFER_ACCESS; in mxcmci_setup_dma()
813 config->dst_addr_width = 4; in mxcmci_setup_dma()
814 config->src_addr_width = 4; in mxcmci_setup_dma()
815 config->dst_maxburst = host->burstlen; in mxcmci_setup_dma()
816 config->src_maxburst = host->burstlen; in mxcmci_setup_dma()
817 config->device_fc = false; in mxcmci_setup_dma()
819 return dmaengine_slave_config(host->dma, config); in mxcmci_setup_dma()
828 * use burstlen of 64 (16 words) in 4 bit mode (--> reg value 0) in mxcmci_set_ios()
829 * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16) in mxcmci_set_ios()
831 if (ios->bus_width == MMC_BUS_WIDTH_4) in mxcmci_set_ios()
836 if (mxcmci_use_dma(host) && burstlen != host->burstlen) { in mxcmci_set_ios()
837 host->burstlen = burstlen; in mxcmci_set_ios()
840 dev_err(mmc_dev(host->mmc), in mxcmci_set_ios()
842 dma_release_channel(host->dma); in mxcmci_set_ios()
843 host->do_dma = 0; in mxcmci_set_ios()
844 host->dma = NULL; in mxcmci_set_ios()
848 if (ios->bus_width == MMC_BUS_WIDTH_4) in mxcmci_set_ios()
849 host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4; in mxcmci_set_ios()
851 host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4; in mxcmci_set_ios()
853 if (host->power_mode != ios->power_mode) { in mxcmci_set_ios()
854 host->power_mode = ios->power_mode; in mxcmci_set_ios()
855 mxcmci_set_power(host, ios->vdd); in mxcmci_set_ios()
857 if (ios->power_mode == MMC_POWER_ON) in mxcmci_set_ios()
858 host->cmdat |= CMD_DAT_CONT_INIT; in mxcmci_set_ios()
861 if (ios->clock) { in mxcmci_set_ios()
862 mxcmci_set_clk_rate(host, ios->clock); in mxcmci_set_ios()
868 host->clock = ios->clock; in mxcmci_set_ios()
885 if (host->pdata && host->pdata->get_ro) in mxcmci_get_ro()
886 return !!host->pdata->get_ro(mmc_dev(mmc)); in mxcmci_get_ro()
889 * context or gpio is invalid), then let the mmc core decide in mxcmci_get_ro()
901 spin_lock_irqsave(&host->lock, flags); in mxcmci_enable_sdio_irq()
902 host->use_sdio = enable; in mxcmci_enable_sdio_irq()
911 spin_unlock_irqrestore(&host->lock, flags); in mxcmci_enable_sdio_irq()
920 * multi-block transfers when connected SDIO peripheral doesn't in mxcmci_init_card()
922 * One way to prevent this is to only allow 1-bit transfers. in mxcmci_init_card()
926 host->caps &= ~MMC_CAP_4_BIT_DATA; in mxcmci_init_card()
928 host->caps |= MMC_CAP_4_BIT_DATA; in mxcmci_init_card()
938 chan->private = &host->dma_data; in filter()
946 struct mmc_request *req = host->req; in mxcmci_watchdog()
949 if (host->dma_dir == DMA_FROM_DEVICE) { in mxcmci_watchdog()
950 dmaengine_terminate_all(host->dma); in mxcmci_watchdog()
951 dev_err(mmc_dev(host->mmc), in mxcmci_watchdog()
955 dev_err(mmc_dev(host->mmc), in mxcmci_watchdog()
963 if (host->data) in mxcmci_watchdog()
964 host->data->error = -ETIMEDOUT; in mxcmci_watchdog()
965 host->req = NULL; in mxcmci_watchdog()
966 host->cmd = NULL; in mxcmci_watchdog()
967 host->data = NULL; in mxcmci_watchdog()
968 mmc_request_done(host->mmc, req); in mxcmci_watchdog()
987 struct imxmmc_platform_data *pdata = pdev->dev.platform_data; in mxcmci_probe()
995 mmc = mmc_alloc_host(sizeof(*host), &pdev->dev); in mxcmci_probe()
997 return -ENOMEM; in mxcmci_probe()
1001 host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in mxcmci_probe()
1002 if (IS_ERR(host->base)) { in mxcmci_probe()
1003 ret = PTR_ERR(host->base); in mxcmci_probe()
1007 host->phys_base = res->start; in mxcmci_probe()
1012 mmc->ops = &mxcmci_ops; in mxcmci_probe()
1016 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ; in mxcmci_probe()
1018 mmc->caps |= MMC_CAP_SDIO_IRQ; in mxcmci_probe()
1021 mmc->max_blk_size = 2048; in mxcmci_probe()
1022 mmc->max_blk_count = 65535; in mxcmci_probe()
1023 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; in mxcmci_probe()
1024 mmc->max_seg_size = mmc->max_req_size; in mxcmci_probe()
1026 host->devtype = (uintptr_t)of_device_get_match_data(&pdev->dev); in mxcmci_probe()
1030 mmc->max_segs = 64; in mxcmci_probe()
1032 host->mmc = mmc; in mxcmci_probe()
1033 host->pdata = pdata; in mxcmci_probe()
1034 spin_lock_init(&host->lock); in mxcmci_probe()
1037 dat3_card_detect = pdata->dat3_card_detect; in mxcmci_probe()
1039 && !of_property_read_bool(pdev->dev.of_node, "cd-gpios")) in mxcmci_probe()
1046 if (!mmc->ocr_avail) { in mxcmci_probe()
1047 if (pdata && pdata->ocr_avail) in mxcmci_probe()
1048 mmc->ocr_avail = pdata->ocr_avail; in mxcmci_probe()
1050 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; in mxcmci_probe()
1054 host->default_irq_mask = in mxcmci_probe()
1057 host->default_irq_mask = 0; in mxcmci_probe()
1059 host->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); in mxcmci_probe()
1060 if (IS_ERR(host->clk_ipg)) { in mxcmci_probe()
1061 ret = PTR_ERR(host->clk_ipg); in mxcmci_probe()
1065 host->clk_per = devm_clk_get(&pdev->dev, "per"); in mxcmci_probe()
1066 if (IS_ERR(host->clk_per)) { in mxcmci_probe()
1067 ret = PTR_ERR(host->clk_per); in mxcmci_probe()
1071 ret = clk_prepare_enable(host->clk_per); in mxcmci_probe()
1075 ret = clk_prepare_enable(host->clk_ipg); in mxcmci_probe()
1081 host->rev_no = mxcmci_readw(host, MMC_REG_REV_NO); in mxcmci_probe()
1082 if (host->rev_no != 0x400) { in mxcmci_probe()
1083 ret = -ENODEV; in mxcmci_probe()
1084 dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n", in mxcmci_probe()
1085 host->rev_no); in mxcmci_probe()
1089 mmc->f_min = clk_get_rate(host->clk_per) >> 16; in mxcmci_probe()
1090 mmc->f_max = clk_get_rate(host->clk_per) >> 1; in mxcmci_probe()
1095 mxcmci_writel(host, host->default_irq_mask, MMC_REG_INT_CNTR); in mxcmci_probe()
1097 if (!host->pdata) { in mxcmci_probe()
1098 host->dma = dma_request_chan(&pdev->dev, "rx-tx"); in mxcmci_probe()
1099 if (IS_ERR(host->dma)) { in mxcmci_probe()
1100 if (PTR_ERR(host->dma) == -EPROBE_DEFER) { in mxcmci_probe()
1101 ret = -EPROBE_DEFER; in mxcmci_probe()
1106 host->dma = NULL; in mxcmci_probe()
1111 host->dmareq = res->start; in mxcmci_probe()
1112 host->dma_data.peripheral_type = IMX_DMATYPE_SDHC; in mxcmci_probe()
1113 host->dma_data.priority = DMA_PRIO_LOW; in mxcmci_probe()
1114 host->dma_data.dma_request = host->dmareq; in mxcmci_probe()
1117 host->dma = dma_request_channel(mask, filter, host); in mxcmci_probe()
1120 if (host->dma) in mxcmci_probe()
1121 mmc->max_seg_size = dma_get_max_seg_size( in mxcmci_probe()
1122 host->dma->device->dev); in mxcmci_probe()
1124 dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n"); in mxcmci_probe()
1126 INIT_WORK(&host->datawork, mxcmci_datawork); in mxcmci_probe()
1128 ret = devm_request_irq(&pdev->dev, irq, mxcmci_irq, 0, in mxcmci_probe()
1129 dev_name(&pdev->dev), host); in mxcmci_probe()
1135 if (host->pdata && host->pdata->init) { in mxcmci_probe()
1136 ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq, in mxcmci_probe()
1137 host->mmc); in mxcmci_probe()
1142 timer_setup(&host->watchdog, mxcmci_watchdog, 0); in mxcmci_probe()
1151 if (host->dma) in mxcmci_probe()
1152 dma_release_channel(host->dma); in mxcmci_probe()
1155 clk_disable_unprepare(host->clk_ipg); in mxcmci_probe()
1157 clk_disable_unprepare(host->clk_per); in mxcmci_probe()
1172 if (host->pdata && host->pdata->exit) in mxcmci_remove()
1173 host->pdata->exit(&pdev->dev, mmc); in mxcmci_remove()
1175 if (host->dma) in mxcmci_remove()
1176 dma_release_channel(host->dma); in mxcmci_remove()
1178 clk_disable_unprepare(host->clk_per); in mxcmci_remove()
1179 clk_disable_unprepare(host->clk_ipg); in mxcmci_remove()
1189 clk_disable_unprepare(host->clk_per); in mxcmci_suspend()
1190 clk_disable_unprepare(host->clk_ipg); in mxcmci_suspend()
1200 ret = clk_prepare_enable(host->clk_per); in mxcmci_resume()
1204 ret = clk_prepare_enable(host->clk_ipg); in mxcmci_resume()
1206 clk_disable_unprepare(host->clk_per); in mxcmci_resume()
1229 MODULE_ALIAS("platform:mxc-mmc");