1 // SPDX-License-Identifier: GPL-2.0+
3 * Amlogic Meson6/Meson8/Meson8b/Meson8m2 SDHC MMC host controller driver.
5 * Copyright (C) 2020 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/types.h>
21 #include <linux/mmc/host.h>
22 #include <linux/mmc/mmc.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/mmc/slot-gpio.h>
26 #include "meson-mx-sdhc.h"
28 #define MESON_SDHC_NUM_BULK_CLKS 4
29 #define MESON_SDHC_MAX_BLK_SIZE 512
30 #define MESON_SDHC_NUM_TUNING_TRIES 10
32 #define MESON_SDHC_WAIT_CMD_READY_SLEEP_US 1
33 #define MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US 100000
34 #define MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US 1
35 #define MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US 200
37 struct meson_mx_sdhc_data {
38 void (*init_hw)(struct mmc_host *mmc);
39 void (*set_pdma)(struct mmc_host *mmc);
40 void (*wait_before_send)(struct mmc_host *mmc);
41 bool hardware_flush_all_cmds;
44 struct meson_mx_sdhc_host {
47 struct mmc_request *mrq;
48 struct mmc_command *cmd;
51 struct regmap *regmap;
55 struct clk_bulk_data bulk_clks[MESON_SDHC_NUM_BULK_CLKS];
56 bool bulk_clks_enabled;
58 const struct meson_mx_sdhc_data *platform;
61 static const struct regmap_config meson_mx_sdhc_regmap_config = {
65 .max_register = MESON_SDHC_CLK2,
68 static void meson_mx_sdhc_hw_reset(struct mmc_host *mmc)
70 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
72 regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_MAIN_CTRL |
73 MESON_SDHC_SRST_RXFIFO | MESON_SDHC_SRST_TXFIFO |
74 MESON_SDHC_SRST_DPHY_RX | MESON_SDHC_SRST_DPHY_TX |
75 MESON_SDHC_SRST_DMA_IF);
76 usleep_range(10, 100);
78 regmap_write(host->regmap, MESON_SDHC_SRST, 0);
79 usleep_range(10, 100);
82 static void meson_mx_sdhc_clear_fifo(struct mmc_host *mmc)
84 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
87 regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
88 if (!FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat) &&
89 !FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat))
92 regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_RXFIFO |
93 MESON_SDHC_SRST_TXFIFO | MESON_SDHC_SRST_MAIN_CTRL);
96 regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
97 if (FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat) ||
98 FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat))
99 dev_warn(mmc_dev(host->mmc),
100 "Failed to clear FIFOs, RX: %lu, TX: %lu\n",
101 FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat),
102 FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat));
105 static void meson_mx_sdhc_wait_cmd_ready(struct mmc_host *mmc)
107 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
111 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT, stat,
112 !(stat & MESON_SDHC_STAT_CMD_BUSY),
113 MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
114 MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
116 dev_warn(mmc_dev(mmc),
117 "Failed to poll for CMD_BUSY while processing CMD%d\n",
119 meson_mx_sdhc_hw_reset(mmc);
122 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, esta,
123 !(esta & MESON_SDHC_ESTA_11_13),
124 MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
125 MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
127 dev_warn(mmc_dev(mmc),
128 "Failed to poll for ESTA[13:11] while processing CMD%d\n",
130 meson_mx_sdhc_hw_reset(mmc);
134 static void meson_mx_sdhc_start_cmd(struct mmc_host *mmc,
135 struct mmc_command *cmd)
137 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
138 bool manual_stop = false;
144 ictl = MESON_SDHC_ICTL_DATA_TIMEOUT | MESON_SDHC_ICTL_DATA_ERR_CRC |
145 MESON_SDHC_ICTL_RXFIFO_FULL | MESON_SDHC_ICTL_TXFIFO_EMPTY |
146 MESON_SDHC_ICTL_RESP_TIMEOUT | MESON_SDHC_ICTL_RESP_ERR_CRC;
148 send = FIELD_PREP(MESON_SDHC_SEND_CMD_INDEX, cmd->opcode);
151 send |= MESON_SDHC_SEND_CMD_HAS_DATA;
152 send |= FIELD_PREP(MESON_SDHC_SEND_TOTAL_PACK,
153 cmd->data->blocks - 1);
155 if (cmd->data->blksz < MESON_SDHC_MAX_BLK_SIZE)
156 pack_len = cmd->data->blksz;
160 if (cmd->data->flags & MMC_DATA_WRITE)
161 send |= MESON_SDHC_SEND_DATA_DIR;
164 * If command with no data, just wait response done
165 * interrupt(int[0]), and if command with data transfer, just
166 * wait dma done interrupt(int[11]), don't need care about
169 if (host->platform->hardware_flush_all_cmds ||
170 cmd->data->flags & MMC_DATA_WRITE)
171 /* hardware flush: */
172 ictl |= MESON_SDHC_ICTL_DMA_DONE;
174 /* software flush: */
175 ictl |= MESON_SDHC_ICTL_DATA_XFER_OK;
178 * Mimic the logic from the vendor driver where (only)
179 * SD_IO_RW_EXTENDED commands with more than one block set the
180 * MESON_SDHC_MISC_MANUAL_STOP bit. This fixes the firmware
181 * download in the brcmfmac driver for a BCM43362/1 card.
182 * Without this sdio_memcpy_toio() (with a size of 219557
183 * bytes) times out if MESON_SDHC_MISC_MANUAL_STOP is not set.
185 manual_stop = cmd->data->blocks > 1 &&
186 cmd->opcode == SD_IO_RW_EXTENDED;
190 ictl |= MESON_SDHC_ICTL_RESP_OK;
193 regmap_update_bits(host->regmap, MESON_SDHC_MISC,
194 MESON_SDHC_MISC_MANUAL_STOP,
195 manual_stop ? MESON_SDHC_MISC_MANUAL_STOP : 0);
197 if (cmd->opcode == MMC_STOP_TRANSMISSION)
198 send |= MESON_SDHC_SEND_DATA_STOP;
200 if (cmd->flags & MMC_RSP_PRESENT)
201 send |= MESON_SDHC_SEND_CMD_HAS_RESP;
203 if (cmd->flags & MMC_RSP_136) {
204 send |= MESON_SDHC_SEND_RESP_LEN;
205 send |= MESON_SDHC_SEND_RESP_NO_CRC;
208 if (!(cmd->flags & MMC_RSP_CRC))
209 send |= MESON_SDHC_SEND_RESP_NO_CRC;
211 if (cmd->flags & MMC_RSP_BUSY)
212 send |= MESON_SDHC_SEND_R1B;
214 /* enable the new IRQs and mask all pending ones */
215 regmap_write(host->regmap, MESON_SDHC_ICTL, ictl);
216 regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS);
218 regmap_write(host->regmap, MESON_SDHC_ARGU, cmd->arg);
220 regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
221 MESON_SDHC_CTRL_PACK_LEN,
222 FIELD_PREP(MESON_SDHC_CTRL_PACK_LEN, pack_len));
225 regmap_write(host->regmap, MESON_SDHC_ADDR,
226 sg_dma_address(cmd->data->sg));
228 meson_mx_sdhc_wait_cmd_ready(mmc);
231 host->platform->set_pdma(mmc);
233 if (host->platform->wait_before_send)
234 host->platform->wait_before_send(mmc);
236 regmap_write(host->regmap, MESON_SDHC_SEND, send);
239 static void meson_mx_sdhc_disable_clks(struct mmc_host *mmc)
241 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
243 if (!host->bulk_clks_enabled)
246 clk_bulk_disable_unprepare(MESON_SDHC_NUM_BULK_CLKS, host->bulk_clks);
248 host->bulk_clks_enabled = false;
251 static int meson_mx_sdhc_enable_clks(struct mmc_host *mmc)
253 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
256 if (host->bulk_clks_enabled)
259 ret = clk_bulk_prepare_enable(MESON_SDHC_NUM_BULK_CLKS,
264 host->bulk_clks_enabled = true;
269 static int meson_mx_sdhc_set_clk(struct mmc_host *mmc, struct mmc_ios *ios)
271 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
272 u32 val, rx_clk_phase;
275 meson_mx_sdhc_disable_clks(mmc);
278 ret = clk_set_rate(host->sd_clk, ios->clock);
280 dev_warn(mmc_dev(mmc),
281 "Failed to set MMC clock to %uHz: %d\n",
282 ios->clock, host->error);
286 ret = meson_mx_sdhc_enable_clks(mmc);
290 mmc->actual_clock = clk_get_rate(host->sd_clk);
293 * Phase 90 should work in most cases. For data transmission,
294 * meson_mx_sdhc_execute_tuning() will find a accurate value
296 regmap_read(host->regmap, MESON_SDHC_CLKC, &val);
297 rx_clk_phase = FIELD_GET(MESON_SDHC_CLKC_CLK_DIV, val) / 4;
298 regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
299 MESON_SDHC_CLK2_RX_CLK_PHASE,
300 FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
303 mmc->actual_clock = 0;
309 static void meson_mx_sdhc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
311 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
312 unsigned short vdd = ios->vdd;
314 switch (ios->power_mode) {
320 if (!IS_ERR(mmc->supply.vmmc)) {
321 host->error = mmc_regulator_set_ocr(mmc,
334 host->error = meson_mx_sdhc_set_clk(mmc, ios);
338 switch (ios->bus_width) {
339 case MMC_BUS_WIDTH_1:
340 regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
341 MESON_SDHC_CTRL_DAT_TYPE,
342 FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 0));
345 case MMC_BUS_WIDTH_4:
346 regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
347 MESON_SDHC_CTRL_DAT_TYPE,
348 FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 1));
351 case MMC_BUS_WIDTH_8:
352 regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
353 MESON_SDHC_CTRL_DAT_TYPE,
354 FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 2));
358 dev_err(mmc_dev(mmc), "unsupported bus width: %d\n",
360 host->error = -EINVAL;
365 static int meson_mx_sdhc_map_dma(struct mmc_host *mmc, struct mmc_request *mrq)
367 struct mmc_data *data = mrq->data;
373 dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
374 mmc_get_dma_dir(data));
376 dev_err(mmc_dev(mmc), "dma_map_sg failed\n");
383 static void meson_mx_sdhc_request(struct mmc_host *mmc, struct mmc_request *mrq)
385 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
386 struct mmc_command *cmd = mrq->cmd;
389 host->error = meson_mx_sdhc_map_dma(mmc, mrq);
392 cmd->error = host->error;
393 mmc_request_done(mmc, mrq);
399 meson_mx_sdhc_start_cmd(mmc, mrq->cmd);
402 static int meson_mx_sdhc_card_busy(struct mmc_host *mmc)
404 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
407 regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
408 return FIELD_GET(MESON_SDHC_STAT_DAT3_0, stat) == 0;
411 static bool meson_mx_sdhc_tuning_point_matches(struct mmc_host *mmc,
414 unsigned int i, num_matches = 0;
417 for (i = 0; i < MESON_SDHC_NUM_TUNING_TRIES; i++) {
418 ret = mmc_send_tuning(mmc, opcode, NULL);
423 return num_matches == MESON_SDHC_NUM_TUNING_TRIES;
426 static int meson_mx_sdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
428 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
429 int div, start, len, best_start, best_len;
430 int curr_phase, old_phase, new_phase;
437 regmap_read(host->regmap, MESON_SDHC_CLK2, &val);
438 old_phase = FIELD_GET(MESON_SDHC_CLK2_RX_CLK_PHASE, val);
440 regmap_read(host->regmap, MESON_SDHC_CLKC, &val);
441 div = FIELD_GET(MESON_SDHC_CLKC_CLK_DIV, val);
443 for (curr_phase = 0; curr_phase <= div; curr_phase++) {
444 regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
445 MESON_SDHC_CLK2_RX_CLK_PHASE,
446 FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
449 if (meson_mx_sdhc_tuning_point_matches(mmc, opcode)) {
453 dev_dbg(mmc_dev(mmc),
454 "New RX phase window starts at %u\n",
460 if (len > best_len) {
464 dev_dbg(mmc_dev(mmc),
465 "New best RX phase window: %u - %u\n",
466 best_start, best_start + best_len);
469 /* reset the current window */
475 /* the last window is the best (or possibly only) window */
476 new_phase = start + (len / 2);
478 /* there was a better window than the last */
479 new_phase = best_start + (best_len / 2);
481 /* no window was found at all, reset to the original phase */
482 new_phase = old_phase;
484 regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
485 MESON_SDHC_CLK2_RX_CLK_PHASE,
486 FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
489 if (!len && !best_len)
492 dev_dbg(mmc_dev(mmc), "Tuned RX clock phase to %u\n", new_phase);
497 static const struct mmc_host_ops meson_mx_sdhc_ops = {
498 .hw_reset = meson_mx_sdhc_hw_reset,
499 .request = meson_mx_sdhc_request,
500 .set_ios = meson_mx_sdhc_set_ios,
501 .card_busy = meson_mx_sdhc_card_busy,
502 .execute_tuning = meson_mx_sdhc_execute_tuning,
503 .get_cd = mmc_gpio_get_cd,
504 .get_ro = mmc_gpio_get_ro,
507 static void meson_mx_sdhc_request_done(struct meson_mx_sdhc_host *host)
509 struct mmc_request *mrq = host->mrq;
510 struct mmc_host *mmc = host->mmc;
512 /* disable interrupts and mask all pending ones */
513 regmap_update_bits(host->regmap, MESON_SDHC_ICTL,
514 MESON_SDHC_ICTL_ALL_IRQS, 0);
515 regmap_update_bits(host->regmap, MESON_SDHC_ISTA,
516 MESON_SDHC_ISTA_ALL_IRQS, MESON_SDHC_ISTA_ALL_IRQS);
521 mmc_request_done(mmc, mrq);
524 static u32 meson_mx_sdhc_read_response(struct meson_mx_sdhc_host *host, u8 idx)
528 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
529 MESON_SDHC_PDMA_DMA_MODE, 0);
531 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
532 MESON_SDHC_PDMA_PIO_RDRESP,
533 FIELD_PREP(MESON_SDHC_PDMA_PIO_RDRESP, idx));
535 regmap_read(host->regmap, MESON_SDHC_ARGU, &val);
540 static irqreturn_t meson_mx_sdhc_irq(int irq, void *data)
542 struct meson_mx_sdhc_host *host = data;
543 struct mmc_command *cmd = host->cmd;
546 regmap_read(host->regmap, MESON_SDHC_ICTL, &ictl);
547 regmap_read(host->regmap, MESON_SDHC_ISTA, &ista);
552 if (ista & MESON_SDHC_ISTA_RXFIFO_FULL ||
553 ista & MESON_SDHC_ISTA_TXFIFO_EMPTY)
555 else if (ista & MESON_SDHC_ISTA_RESP_ERR_CRC)
556 cmd->error = -EILSEQ;
557 else if (ista & MESON_SDHC_ISTA_RESP_TIMEOUT)
558 cmd->error = -ETIMEDOUT;
561 if (ista & MESON_SDHC_ISTA_DATA_ERR_CRC)
562 cmd->data->error = -EILSEQ;
563 else if (ista & MESON_SDHC_ISTA_DATA_TIMEOUT)
564 cmd->data->error = -ETIMEDOUT;
567 if (cmd->error || (cmd->data && cmd->data->error))
568 dev_dbg(mmc_dev(host->mmc), "CMD%d error, ISTA: 0x%08x\n",
571 return IRQ_WAKE_THREAD;
574 static irqreturn_t meson_mx_sdhc_irq_thread(int irq, void *irq_data)
576 struct meson_mx_sdhc_host *host = irq_data;
577 struct mmc_command *cmd;
584 if (cmd->data && !cmd->data->error) {
585 if (!host->platform->hardware_flush_all_cmds &&
586 cmd->data->flags & MMC_DATA_READ) {
587 meson_mx_sdhc_wait_cmd_ready(host->mmc);
590 * If MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH was
591 * previously 0x1 then it has to be set to 0x3. If it
592 * was 0x0 before then it has to be set to 0x2. Without
593 * this reading SD cards sometimes transfers garbage,
594 * which results in cards not being detected due to:
595 * unrecognised SCR structure version <random number>
597 val = FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
599 regmap_update_bits(host->regmap, MESON_SDHC_PDMA, val,
603 dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg,
604 cmd->data->sg_len, mmc_get_dma_dir(cmd->data));
606 cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks;
609 meson_mx_sdhc_wait_cmd_ready(host->mmc);
611 if (cmd->flags & MMC_RSP_136) {
612 cmd->resp[0] = meson_mx_sdhc_read_response(host, 4);
613 cmd->resp[1] = meson_mx_sdhc_read_response(host, 3);
614 cmd->resp[2] = meson_mx_sdhc_read_response(host, 2);
615 cmd->resp[3] = meson_mx_sdhc_read_response(host, 1);
617 cmd->resp[0] = meson_mx_sdhc_read_response(host, 0);
620 if (cmd->error == -EIO || cmd->error == -ETIMEDOUT)
621 meson_mx_sdhc_hw_reset(host->mmc);
624 * Clear the FIFOs after completing data transfers to prevent
625 * corrupting data on write access. It's not clear why this is
626 * needed (for reads and writes), but it mimics what the BSP
629 meson_mx_sdhc_clear_fifo(host->mmc);
631 meson_mx_sdhc_request_done(host);
636 static void meson_mx_sdhc_init_hw_meson8(struct mmc_host *mmc)
638 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
640 regmap_write(host->regmap, MESON_SDHC_MISC,
641 FIELD_PREP(MESON_SDHC_MISC_TXSTART_THRES, 7) |
642 FIELD_PREP(MESON_SDHC_MISC_WCRC_ERR_PATT, 5) |
643 FIELD_PREP(MESON_SDHC_MISC_WCRC_OK_PATT, 2));
645 regmap_write(host->regmap, MESON_SDHC_ENHC,
646 FIELD_PREP(MESON_SDHC_ENHC_RXFIFO_TH, 63) |
647 MESON_SDHC_ENHC_MESON6_DMA_WR_RESP |
648 FIELD_PREP(MESON_SDHC_ENHC_MESON6_RX_TIMEOUT, 255) |
649 FIELD_PREP(MESON_SDHC_ENHC_SDIO_IRQ_PERIOD, 12));
652 static void meson_mx_sdhc_set_pdma_meson8(struct mmc_host *mmc)
654 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
656 if (host->cmd->data->flags & MMC_DATA_WRITE)
657 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
658 MESON_SDHC_PDMA_DMA_MODE |
659 MESON_SDHC_PDMA_RD_BURST |
660 MESON_SDHC_PDMA_TXFIFO_FILL,
661 MESON_SDHC_PDMA_DMA_MODE |
662 FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 31) |
663 MESON_SDHC_PDMA_TXFIFO_FILL);
665 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
666 MESON_SDHC_PDMA_DMA_MODE |
667 MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
668 MESON_SDHC_PDMA_DMA_MODE |
669 FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
672 if (host->cmd->data->flags & MMC_DATA_WRITE)
673 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
674 MESON_SDHC_PDMA_RD_BURST,
675 FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 15));
678 static void meson_mx_sdhc_wait_before_send_meson8(struct mmc_host *mmc)
680 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
684 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, val,
686 MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US,
687 MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US);
689 dev_warn(mmc_dev(mmc),
690 "Failed to wait for ESTA to clear: 0x%08x\n", val);
692 if (host->cmd->data && host->cmd->data->flags & MMC_DATA_WRITE) {
693 ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT,
694 val, val & MESON_SDHC_STAT_TXFIFO_CNT,
695 MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US,
696 MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US);
698 dev_warn(mmc_dev(mmc),
699 "Failed to wait for TX FIFO to fill\n");
703 static void meson_mx_sdhc_init_hw_meson8m2(struct mmc_host *mmc)
705 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
707 regmap_write(host->regmap, MESON_SDHC_MISC,
708 FIELD_PREP(MESON_SDHC_MISC_TXSTART_THRES, 6) |
709 FIELD_PREP(MESON_SDHC_MISC_WCRC_ERR_PATT, 5) |
710 FIELD_PREP(MESON_SDHC_MISC_WCRC_OK_PATT, 2));
712 regmap_write(host->regmap, MESON_SDHC_ENHC,
713 FIELD_PREP(MESON_SDHC_ENHC_RXFIFO_TH, 64) |
714 FIELD_PREP(MESON_SDHC_ENHC_MESON8M2_DEBUG, 1) |
715 MESON_SDHC_ENHC_MESON8M2_WRRSP_MODE |
716 FIELD_PREP(MESON_SDHC_ENHC_SDIO_IRQ_PERIOD, 12));
719 static void meson_mx_sdhc_set_pdma_meson8m2(struct mmc_host *mmc)
721 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
723 regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
724 MESON_SDHC_PDMA_DMA_MODE, MESON_SDHC_PDMA_DMA_MODE);
727 static void meson_mx_sdhc_init_hw(struct mmc_host *mmc)
729 struct meson_mx_sdhc_host *host = mmc_priv(mmc);
731 meson_mx_sdhc_hw_reset(mmc);
733 regmap_write(host->regmap, MESON_SDHC_CTRL,
734 FIELD_PREP(MESON_SDHC_CTRL_RX_PERIOD, 0xf) |
735 FIELD_PREP(MESON_SDHC_CTRL_RX_TIMEOUT, 0x7f) |
736 FIELD_PREP(MESON_SDHC_CTRL_RX_ENDIAN, 0x7) |
737 FIELD_PREP(MESON_SDHC_CTRL_TX_ENDIAN, 0x7));
740 * start with a valid divider and enable the memory (un-setting
741 * MESON_SDHC_CLKC_MEM_PWR_OFF).
743 regmap_write(host->regmap, MESON_SDHC_CLKC, MESON_SDHC_CLKC_CLK_DIV);
745 regmap_write(host->regmap, MESON_SDHC_CLK2,
746 FIELD_PREP(MESON_SDHC_CLK2_SD_CLK_PHASE, 1));
748 regmap_write(host->regmap, MESON_SDHC_PDMA,
749 MESON_SDHC_PDMA_DMA_URGENT |
750 FIELD_PREP(MESON_SDHC_PDMA_WR_BURST, 7) |
751 FIELD_PREP(MESON_SDHC_PDMA_TXFIFO_TH, 49) |
752 FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 15) |
753 FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_TH, 7));
755 /* some initialization bits depend on the SoC: */
756 host->platform->init_hw(mmc);
758 /* disable and mask all interrupts: */
759 regmap_write(host->regmap, MESON_SDHC_ICTL, 0);
760 regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS);
763 static int meson_mx_sdhc_probe(struct platform_device *pdev)
765 struct device *dev = &pdev->dev;
766 struct meson_mx_sdhc_host *host;
767 struct mmc_host *mmc;
771 mmc = mmc_alloc_host(sizeof(*host), dev);
775 ret = devm_add_action_or_reset(dev, (void(*)(void *))mmc_free_host,
778 dev_err(dev, "Failed to register mmc_free_host action\n");
782 host = mmc_priv(mmc);
785 platform_set_drvdata(pdev, host);
787 host->platform = device_get_match_data(dev);
791 base = devm_platform_ioremap_resource(pdev, 0);
793 return PTR_ERR(base);
795 host->regmap = devm_regmap_init_mmio(dev, base,
796 &meson_mx_sdhc_regmap_config);
797 if (IS_ERR(host->regmap))
798 return PTR_ERR(host->regmap);
800 host->pclk = devm_clk_get(dev, "pclk");
801 if (IS_ERR(host->pclk))
802 return PTR_ERR(host->pclk);
804 /* accessing any register requires the module clock to be enabled: */
805 ret = clk_prepare_enable(host->pclk);
807 dev_err(dev, "Failed to enable 'pclk' clock\n");
811 meson_mx_sdhc_init_hw(mmc);
813 ret = meson_mx_sdhc_register_clkc(dev, base, host->bulk_clks);
815 goto err_disable_pclk;
817 host->sd_clk = host->bulk_clks[1].clk;
819 /* Get regulators and the supported OCR mask */
820 ret = mmc_regulator_get_supply(mmc);
822 goto err_disable_pclk;
824 mmc->max_req_size = SZ_128K;
825 mmc->max_seg_size = mmc->max_req_size;
826 mmc->max_blk_count = FIELD_GET(MESON_SDHC_SEND_TOTAL_PACK, ~0);
827 mmc->max_blk_size = MESON_SDHC_MAX_BLK_SIZE;
828 mmc->max_busy_timeout = 30 * MSEC_PER_SEC;
829 mmc->f_min = clk_round_rate(host->sd_clk, 1);
830 mmc->f_max = clk_round_rate(host->sd_clk, ULONG_MAX);
831 mmc->max_current_180 = 300;
832 mmc->max_current_330 = 300;
833 mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_HW_RESET;
834 mmc->ops = &meson_mx_sdhc_ops;
836 ret = mmc_of_parse(mmc);
838 goto err_disable_pclk;
840 irq = platform_get_irq(pdev, 0);
843 goto err_disable_pclk;
846 ret = devm_request_threaded_irq(dev, irq, meson_mx_sdhc_irq,
847 meson_mx_sdhc_irq_thread, IRQF_ONESHOT,
850 goto err_disable_pclk;
852 ret = mmc_add_host(mmc);
854 goto err_disable_pclk;
859 clk_disable_unprepare(host->pclk);
863 static int meson_mx_sdhc_remove(struct platform_device *pdev)
865 struct meson_mx_sdhc_host *host = platform_get_drvdata(pdev);
867 mmc_remove_host(host->mmc);
869 meson_mx_sdhc_disable_clks(host->mmc);
871 clk_disable_unprepare(host->pclk);
876 static const struct meson_mx_sdhc_data meson_mx_sdhc_data_meson8 = {
877 .init_hw = meson_mx_sdhc_init_hw_meson8,
878 .set_pdma = meson_mx_sdhc_set_pdma_meson8,
879 .wait_before_send = meson_mx_sdhc_wait_before_send_meson8,
880 .hardware_flush_all_cmds = false,
883 static const struct meson_mx_sdhc_data meson_mx_sdhc_data_meson8m2 = {
884 .init_hw = meson_mx_sdhc_init_hw_meson8m2,
885 .set_pdma = meson_mx_sdhc_set_pdma_meson8m2,
886 .hardware_flush_all_cmds = true,
889 static const struct of_device_id meson_mx_sdhc_of_match[] = {
891 .compatible = "amlogic,meson8-sdhc",
892 .data = &meson_mx_sdhc_data_meson8
895 .compatible = "amlogic,meson8b-sdhc",
896 .data = &meson_mx_sdhc_data_meson8
899 .compatible = "amlogic,meson8m2-sdhc",
900 .data = &meson_mx_sdhc_data_meson8m2
904 MODULE_DEVICE_TABLE(of, meson_mx_sdhc_of_match);
906 static struct platform_driver meson_mx_sdhc_driver = {
907 .probe = meson_mx_sdhc_probe,
908 .remove = meson_mx_sdhc_remove,
910 .name = "meson-mx-sdhc",
911 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
912 .of_match_table = of_match_ptr(meson_mx_sdhc_of_match),
916 module_platform_driver(meson_mx_sdhc_driver);
918 MODULE_DESCRIPTION("Meson6, Meson8, Meson8b and Meson8m2 SDHC Host Driver");
919 MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
920 MODULE_LICENSE("GPL v2");