Lines Matching +full:meson +full:- +full:mx +full:- +full:sdhc
1 // SPDX-License-Identifier: GPL-2.0+
3 * Amlogic Meson6/Meson8/Meson8b/Meson8m2 SDHC MMC host controller driver.
10 #include <linux/dma-mapping.h>
24 #include <linux/mmc/slot-gpio.h>
26 #include "meson-mx-sdhc.h"
72 regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_MAIN_CTRL | in meson_mx_sdhc_hw_reset()
78 regmap_write(host->regmap, MESON_SDHC_SRST, 0); in meson_mx_sdhc_hw_reset()
87 regmap_read(host->regmap, MESON_SDHC_STAT, &stat); in meson_mx_sdhc_clear_fifo()
92 regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_RXFIFO | in meson_mx_sdhc_clear_fifo()
96 regmap_read(host->regmap, MESON_SDHC_STAT, &stat); in meson_mx_sdhc_clear_fifo()
99 dev_warn(mmc_dev(host->mmc), in meson_mx_sdhc_clear_fifo()
111 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT, stat, in meson_mx_sdhc_wait_cmd_ready()
118 host->cmd->opcode); in meson_mx_sdhc_wait_cmd_ready()
122 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, esta, in meson_mx_sdhc_wait_cmd_ready()
129 host->cmd->opcode); in meson_mx_sdhc_wait_cmd_ready()
142 host->cmd = cmd; in meson_mx_sdhc_start_cmd()
148 send = FIELD_PREP(MESON_SDHC_SEND_CMD_INDEX, cmd->opcode); in meson_mx_sdhc_start_cmd()
150 if (cmd->data) { in meson_mx_sdhc_start_cmd()
153 cmd->data->blocks - 1); in meson_mx_sdhc_start_cmd()
155 if (cmd->data->blksz < MESON_SDHC_MAX_BLK_SIZE) in meson_mx_sdhc_start_cmd()
156 pack_len = cmd->data->blksz; in meson_mx_sdhc_start_cmd()
160 if (cmd->data->flags & MMC_DATA_WRITE) in meson_mx_sdhc_start_cmd()
169 if (host->platform->hardware_flush_all_cmds || in meson_mx_sdhc_start_cmd()
170 cmd->data->flags & MMC_DATA_WRITE) in meson_mx_sdhc_start_cmd()
185 manual_stop = cmd->data->blocks > 1 && in meson_mx_sdhc_start_cmd()
186 cmd->opcode == SD_IO_RW_EXTENDED; in meson_mx_sdhc_start_cmd()
193 regmap_update_bits(host->regmap, MESON_SDHC_MISC, in meson_mx_sdhc_start_cmd()
197 if (cmd->opcode == MMC_STOP_TRANSMISSION) in meson_mx_sdhc_start_cmd()
200 if (cmd->flags & MMC_RSP_PRESENT) in meson_mx_sdhc_start_cmd()
203 if (cmd->flags & MMC_RSP_136) { in meson_mx_sdhc_start_cmd()
208 if (!(cmd->flags & MMC_RSP_CRC)) in meson_mx_sdhc_start_cmd()
211 if (cmd->flags & MMC_RSP_BUSY) in meson_mx_sdhc_start_cmd()
215 regmap_write(host->regmap, MESON_SDHC_ICTL, ictl); in meson_mx_sdhc_start_cmd()
216 regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS); in meson_mx_sdhc_start_cmd()
218 regmap_write(host->regmap, MESON_SDHC_ARGU, cmd->arg); in meson_mx_sdhc_start_cmd()
220 regmap_update_bits(host->regmap, MESON_SDHC_CTRL, in meson_mx_sdhc_start_cmd()
224 if (cmd->data) in meson_mx_sdhc_start_cmd()
225 regmap_write(host->regmap, MESON_SDHC_ADDR, in meson_mx_sdhc_start_cmd()
226 sg_dma_address(cmd->data->sg)); in meson_mx_sdhc_start_cmd()
230 if (cmd->data) in meson_mx_sdhc_start_cmd()
231 host->platform->set_pdma(mmc); in meson_mx_sdhc_start_cmd()
233 if (host->platform->wait_before_send) in meson_mx_sdhc_start_cmd()
234 host->platform->wait_before_send(mmc); in meson_mx_sdhc_start_cmd()
236 regmap_write(host->regmap, MESON_SDHC_SEND, send); in meson_mx_sdhc_start_cmd()
243 if (!host->bulk_clks_enabled) in meson_mx_sdhc_disable_clks()
246 clk_bulk_disable_unprepare(MESON_SDHC_NUM_BULK_CLKS, host->bulk_clks); in meson_mx_sdhc_disable_clks()
248 host->bulk_clks_enabled = false; in meson_mx_sdhc_disable_clks()
256 if (host->bulk_clks_enabled) in meson_mx_sdhc_enable_clks()
260 host->bulk_clks); in meson_mx_sdhc_enable_clks()
264 host->bulk_clks_enabled = true; in meson_mx_sdhc_enable_clks()
277 if (ios->clock) { in meson_mx_sdhc_set_clk()
278 ret = clk_set_rate(host->sd_clk, ios->clock); in meson_mx_sdhc_set_clk()
282 ios->clock, host->error); in meson_mx_sdhc_set_clk()
290 mmc->actual_clock = clk_get_rate(host->sd_clk); in meson_mx_sdhc_set_clk()
296 regmap_read(host->regmap, MESON_SDHC_CLKC, &val); in meson_mx_sdhc_set_clk()
298 regmap_update_bits(host->regmap, MESON_SDHC_CLK2, in meson_mx_sdhc_set_clk()
303 mmc->actual_clock = 0; in meson_mx_sdhc_set_clk()
312 unsigned short vdd = ios->vdd; in meson_mx_sdhc_set_ios()
314 switch (ios->power_mode) { in meson_mx_sdhc_set_ios()
320 if (!IS_ERR(mmc->supply.vmmc)) { in meson_mx_sdhc_set_ios()
321 host->error = mmc_regulator_set_ocr(mmc, in meson_mx_sdhc_set_ios()
322 mmc->supply.vmmc, in meson_mx_sdhc_set_ios()
324 if (host->error) in meson_mx_sdhc_set_ios()
334 host->error = meson_mx_sdhc_set_clk(mmc, ios); in meson_mx_sdhc_set_ios()
335 if (host->error) in meson_mx_sdhc_set_ios()
338 switch (ios->bus_width) { in meson_mx_sdhc_set_ios()
340 regmap_update_bits(host->regmap, MESON_SDHC_CTRL, in meson_mx_sdhc_set_ios()
346 regmap_update_bits(host->regmap, MESON_SDHC_CTRL, in meson_mx_sdhc_set_ios()
352 regmap_update_bits(host->regmap, MESON_SDHC_CTRL, in meson_mx_sdhc_set_ios()
359 ios->bus_width); in meson_mx_sdhc_set_ios()
360 host->error = -EINVAL; in meson_mx_sdhc_set_ios()
367 struct mmc_data *data = mrq->data; in meson_mx_sdhc_map_dma()
373 dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, in meson_mx_sdhc_map_dma()
377 return -ENOMEM; in meson_mx_sdhc_map_dma()
386 struct mmc_command *cmd = mrq->cmd; in meson_mx_sdhc_request()
388 if (!host->error) in meson_mx_sdhc_request()
389 host->error = meson_mx_sdhc_map_dma(mmc, mrq); in meson_mx_sdhc_request()
391 if (host->error) { in meson_mx_sdhc_request()
392 cmd->error = host->error; in meson_mx_sdhc_request()
397 host->mrq = mrq; in meson_mx_sdhc_request()
399 meson_mx_sdhc_start_cmd(mmc, mrq->cmd); in meson_mx_sdhc_request()
407 regmap_read(host->regmap, MESON_SDHC_STAT, &stat); in meson_mx_sdhc_card_busy()
437 regmap_read(host->regmap, MESON_SDHC_CLK2, &val); in meson_mx_sdhc_execute_tuning()
440 regmap_read(host->regmap, MESON_SDHC_CLKC, &val); in meson_mx_sdhc_execute_tuning()
444 regmap_update_bits(host->regmap, MESON_SDHC_CLK2, in meson_mx_sdhc_execute_tuning()
465 "New best RX phase window: %u - %u\n", in meson_mx_sdhc_execute_tuning()
484 regmap_update_bits(host->regmap, MESON_SDHC_CLK2, in meson_mx_sdhc_execute_tuning()
490 return -EIO; in meson_mx_sdhc_execute_tuning()
509 struct mmc_request *mrq = host->mrq; in meson_mx_sdhc_request_done()
510 struct mmc_host *mmc = host->mmc; in meson_mx_sdhc_request_done()
513 regmap_update_bits(host->regmap, MESON_SDHC_ICTL, in meson_mx_sdhc_request_done()
515 regmap_update_bits(host->regmap, MESON_SDHC_ISTA, in meson_mx_sdhc_request_done()
518 host->mrq = NULL; in meson_mx_sdhc_request_done()
519 host->cmd = NULL; in meson_mx_sdhc_request_done()
528 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_read_response()
531 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_read_response()
535 regmap_read(host->regmap, MESON_SDHC_ARGU, &val); in meson_mx_sdhc_read_response()
543 struct mmc_command *cmd = host->cmd; in meson_mx_sdhc_irq()
546 regmap_read(host->regmap, MESON_SDHC_ICTL, &ictl); in meson_mx_sdhc_irq()
547 regmap_read(host->regmap, MESON_SDHC_ISTA, &ista); in meson_mx_sdhc_irq()
554 cmd->error = -EIO; in meson_mx_sdhc_irq()
556 cmd->error = -EILSEQ; in meson_mx_sdhc_irq()
558 cmd->error = -ETIMEDOUT; in meson_mx_sdhc_irq()
560 if (cmd->data) { in meson_mx_sdhc_irq()
562 cmd->data->error = -EILSEQ; in meson_mx_sdhc_irq()
564 cmd->data->error = -ETIMEDOUT; in meson_mx_sdhc_irq()
567 if (cmd->error || (cmd->data && cmd->data->error)) in meson_mx_sdhc_irq()
568 dev_dbg(mmc_dev(host->mmc), "CMD%d error, ISTA: 0x%08x\n", in meson_mx_sdhc_irq()
569 cmd->opcode, ista); in meson_mx_sdhc_irq()
580 cmd = host->cmd; in meson_mx_sdhc_irq_thread()
584 if (cmd->data && !cmd->data->error) { in meson_mx_sdhc_irq_thread()
585 if (!host->platform->hardware_flush_all_cmds && in meson_mx_sdhc_irq_thread()
586 cmd->data->flags & MMC_DATA_READ) { in meson_mx_sdhc_irq_thread()
587 meson_mx_sdhc_wait_cmd_ready(host->mmc); in meson_mx_sdhc_irq_thread()
599 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, val, in meson_mx_sdhc_irq_thread()
603 dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg, in meson_mx_sdhc_irq_thread()
604 cmd->data->sg_len, mmc_get_dma_dir(cmd->data)); in meson_mx_sdhc_irq_thread()
606 cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks; in meson_mx_sdhc_irq_thread()
609 meson_mx_sdhc_wait_cmd_ready(host->mmc); in meson_mx_sdhc_irq_thread()
611 if (cmd->flags & MMC_RSP_136) { in meson_mx_sdhc_irq_thread()
612 cmd->resp[0] = meson_mx_sdhc_read_response(host, 4); in meson_mx_sdhc_irq_thread()
613 cmd->resp[1] = meson_mx_sdhc_read_response(host, 3); in meson_mx_sdhc_irq_thread()
614 cmd->resp[2] = meson_mx_sdhc_read_response(host, 2); in meson_mx_sdhc_irq_thread()
615 cmd->resp[3] = meson_mx_sdhc_read_response(host, 1); in meson_mx_sdhc_irq_thread()
617 cmd->resp[0] = meson_mx_sdhc_read_response(host, 0); in meson_mx_sdhc_irq_thread()
620 if (cmd->error == -EIO || cmd->error == -ETIMEDOUT) in meson_mx_sdhc_irq_thread()
621 meson_mx_sdhc_hw_reset(host->mmc); in meson_mx_sdhc_irq_thread()
622 else if (cmd->data) in meson_mx_sdhc_irq_thread()
629 meson_mx_sdhc_clear_fifo(host->mmc); in meson_mx_sdhc_irq_thread()
640 regmap_write(host->regmap, MESON_SDHC_MISC, in meson_mx_sdhc_init_hw_meson8()
645 regmap_write(host->regmap, MESON_SDHC_ENHC, in meson_mx_sdhc_init_hw_meson8()
656 if (host->cmd->data->flags & MMC_DATA_WRITE) in meson_mx_sdhc_set_pdma_meson8()
657 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_set_pdma_meson8()
665 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_set_pdma_meson8()
672 if (host->cmd->data->flags & MMC_DATA_WRITE) in meson_mx_sdhc_set_pdma_meson8()
673 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_set_pdma_meson8()
684 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, val, in meson_mx_sdhc_wait_before_send_meson8()
692 if (host->cmd->data && host->cmd->data->flags & MMC_DATA_WRITE) { in meson_mx_sdhc_wait_before_send_meson8()
693 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT, in meson_mx_sdhc_wait_before_send_meson8()
707 regmap_write(host->regmap, MESON_SDHC_MISC, in meson_mx_sdhc_init_hw_meson8m2()
712 regmap_write(host->regmap, MESON_SDHC_ENHC, in meson_mx_sdhc_init_hw_meson8m2()
723 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_set_pdma_meson8m2()
733 regmap_write(host->regmap, MESON_SDHC_CTRL, in meson_mx_sdhc_init_hw()
740 * start with a valid divider and enable the memory (un-setting in meson_mx_sdhc_init_hw()
743 regmap_write(host->regmap, MESON_SDHC_CLKC, MESON_SDHC_CLKC_CLK_DIV); in meson_mx_sdhc_init_hw()
745 regmap_write(host->regmap, MESON_SDHC_CLK2, in meson_mx_sdhc_init_hw()
748 regmap_write(host->regmap, MESON_SDHC_PDMA, in meson_mx_sdhc_init_hw()
756 host->platform->init_hw(mmc); in meson_mx_sdhc_init_hw()
759 regmap_write(host->regmap, MESON_SDHC_ICTL, 0); in meson_mx_sdhc_init_hw()
760 regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS); in meson_mx_sdhc_init_hw()
765 struct device *dev = &pdev->dev; in meson_mx_sdhc_probe()
773 return -ENOMEM; in meson_mx_sdhc_probe()
783 host->mmc = mmc; in meson_mx_sdhc_probe()
787 host->platform = device_get_match_data(dev); in meson_mx_sdhc_probe()
788 if (!host->platform) in meson_mx_sdhc_probe()
789 return -EINVAL; in meson_mx_sdhc_probe()
795 host->regmap = devm_regmap_init_mmio(dev, base, in meson_mx_sdhc_probe()
797 if (IS_ERR(host->regmap)) in meson_mx_sdhc_probe()
798 return PTR_ERR(host->regmap); in meson_mx_sdhc_probe()
800 host->pclk = devm_clk_get(dev, "pclk"); in meson_mx_sdhc_probe()
801 if (IS_ERR(host->pclk)) in meson_mx_sdhc_probe()
802 return PTR_ERR(host->pclk); in meson_mx_sdhc_probe()
805 ret = clk_prepare_enable(host->pclk); in meson_mx_sdhc_probe()
813 ret = meson_mx_sdhc_register_clkc(dev, base, host->bulk_clks); in meson_mx_sdhc_probe()
817 host->sd_clk = host->bulk_clks[1].clk; in meson_mx_sdhc_probe()
824 mmc->max_req_size = SZ_128K; in meson_mx_sdhc_probe()
825 mmc->max_seg_size = mmc->max_req_size; in meson_mx_sdhc_probe()
826 mmc->max_blk_count = FIELD_GET(MESON_SDHC_SEND_TOTAL_PACK, ~0); in meson_mx_sdhc_probe()
827 mmc->max_blk_size = MESON_SDHC_MAX_BLK_SIZE; in meson_mx_sdhc_probe()
828 mmc->max_busy_timeout = 30 * MSEC_PER_SEC; in meson_mx_sdhc_probe()
829 mmc->f_min = clk_round_rate(host->sd_clk, 1); in meson_mx_sdhc_probe()
830 mmc->f_max = clk_round_rate(host->sd_clk, ULONG_MAX); in meson_mx_sdhc_probe()
831 mmc->max_current_180 = 300; in meson_mx_sdhc_probe()
832 mmc->max_current_330 = 300; in meson_mx_sdhc_probe()
833 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_HW_RESET; in meson_mx_sdhc_probe()
834 mmc->ops = &meson_mx_sdhc_ops; in meson_mx_sdhc_probe()
859 clk_disable_unprepare(host->pclk); in meson_mx_sdhc_probe()
867 mmc_remove_host(host->mmc); in meson_mx_sdhc_remove()
869 meson_mx_sdhc_disable_clks(host->mmc); in meson_mx_sdhc_remove()
871 clk_disable_unprepare(host->pclk); in meson_mx_sdhc_remove()
891 .compatible = "amlogic,meson8-sdhc",
895 .compatible = "amlogic,meson8b-sdhc",
899 .compatible = "amlogic,meson8m2-sdhc",
910 .name = "meson-mx-sdhc",
918 MODULE_DESCRIPTION("Meson6, Meson8, Meson8b and Meson8m2 SDHC Host Driver");