xref: /linux/drivers/mmc/host/meson-mx-sdhc-mmc.c (revision 854ff7923753009189a9e1f80d23ae9d407c2fb2)
1e4bf1b09SMartin Blumenstingl // SPDX-License-Identifier: GPL-2.0+
2e4bf1b09SMartin Blumenstingl /*
3e4bf1b09SMartin Blumenstingl  * Amlogic Meson6/Meson8/Meson8b/Meson8m2 SDHC MMC host controller driver.
4e4bf1b09SMartin Blumenstingl  *
5e4bf1b09SMartin Blumenstingl  * Copyright (C) 2020 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6e4bf1b09SMartin Blumenstingl  */
7e4bf1b09SMartin Blumenstingl 
8e4bf1b09SMartin Blumenstingl #include <linux/clk.h>
9e4bf1b09SMartin Blumenstingl #include <linux/device.h>
10e4bf1b09SMartin Blumenstingl #include <linux/dma-mapping.h>
11e4bf1b09SMartin Blumenstingl #include <linux/interrupt.h>
12e4bf1b09SMartin Blumenstingl #include <linux/iopoll.h>
13e4bf1b09SMartin Blumenstingl #include <linux/module.h>
14e4bf1b09SMartin Blumenstingl #include <linux/of.h>
15e4bf1b09SMartin Blumenstingl #include <linux/platform_device.h>
16e4bf1b09SMartin Blumenstingl #include <linux/property.h>
17e4bf1b09SMartin Blumenstingl #include <linux/regmap.h>
18e4bf1b09SMartin Blumenstingl #include <linux/regulator/consumer.h>
19e4bf1b09SMartin Blumenstingl #include <linux/types.h>
20e4bf1b09SMartin Blumenstingl 
21e4bf1b09SMartin Blumenstingl #include <linux/mmc/host.h>
22e4bf1b09SMartin Blumenstingl #include <linux/mmc/mmc.h>
23e4bf1b09SMartin Blumenstingl #include <linux/mmc/sdio.h>
24e4bf1b09SMartin Blumenstingl #include <linux/mmc/slot-gpio.h>
25e4bf1b09SMartin Blumenstingl 
26e4bf1b09SMartin Blumenstingl #include "meson-mx-sdhc.h"
27e4bf1b09SMartin Blumenstingl 
28e4bf1b09SMartin Blumenstingl #define MESON_SDHC_NUM_BULK_CLKS				4
29e4bf1b09SMartin Blumenstingl #define MESON_SDHC_MAX_BLK_SIZE					512
30e4bf1b09SMartin Blumenstingl #define MESON_SDHC_NUM_TUNING_TRIES				10
31e4bf1b09SMartin Blumenstingl 
32e4bf1b09SMartin Blumenstingl #define MESON_SDHC_WAIT_CMD_READY_SLEEP_US			1
33e4bf1b09SMartin Blumenstingl #define MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US			100000
34e4bf1b09SMartin Blumenstingl #define MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US			1
35e4bf1b09SMartin Blumenstingl #define MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US			200
36e4bf1b09SMartin Blumenstingl 
37e4bf1b09SMartin Blumenstingl struct meson_mx_sdhc_data {
38e4bf1b09SMartin Blumenstingl 	void		(*init_hw)(struct mmc_host *mmc);
39e4bf1b09SMartin Blumenstingl 	void		(*set_pdma)(struct mmc_host *mmc);
40e4bf1b09SMartin Blumenstingl 	void		(*wait_before_send)(struct mmc_host *mmc);
41e4bf1b09SMartin Blumenstingl 	bool		hardware_flush_all_cmds;
42e4bf1b09SMartin Blumenstingl };
43e4bf1b09SMartin Blumenstingl 
44e4bf1b09SMartin Blumenstingl struct meson_mx_sdhc_host {
45e4bf1b09SMartin Blumenstingl 	struct mmc_host			*mmc;
46e4bf1b09SMartin Blumenstingl 
47e4bf1b09SMartin Blumenstingl 	struct mmc_request		*mrq;
48e4bf1b09SMartin Blumenstingl 	struct mmc_command		*cmd;
49e4bf1b09SMartin Blumenstingl 	int				error;
50e4bf1b09SMartin Blumenstingl 
51e4bf1b09SMartin Blumenstingl 	struct regmap			*regmap;
52e4bf1b09SMartin Blumenstingl 
53e4bf1b09SMartin Blumenstingl 	struct clk			*pclk;
54e4bf1b09SMartin Blumenstingl 	struct clk			*sd_clk;
55e4bf1b09SMartin Blumenstingl 	struct clk_bulk_data		bulk_clks[MESON_SDHC_NUM_BULK_CLKS];
56e4bf1b09SMartin Blumenstingl 	bool				bulk_clks_enabled;
57e4bf1b09SMartin Blumenstingl 
58e4bf1b09SMartin Blumenstingl 	const struct meson_mx_sdhc_data	*platform;
59e4bf1b09SMartin Blumenstingl };
60e4bf1b09SMartin Blumenstingl 
61e4bf1b09SMartin Blumenstingl static const struct regmap_config meson_mx_sdhc_regmap_config = {
62e4bf1b09SMartin Blumenstingl 	.reg_bits = 8,
63e4bf1b09SMartin Blumenstingl 	.val_bits = 32,
64e4bf1b09SMartin Blumenstingl 	.reg_stride = 4,
65e4bf1b09SMartin Blumenstingl 	.max_register = MESON_SDHC_CLK2,
66e4bf1b09SMartin Blumenstingl };
67e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_reset(struct meson_mx_sdhc_host * host)68c0200efaSMartin Blumenstingl static void meson_mx_sdhc_reset(struct meson_mx_sdhc_host *host)
69e4bf1b09SMartin Blumenstingl {
70e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_MAIN_CTRL |
71e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_SRST_RXFIFO | MESON_SDHC_SRST_TXFIFO |
72e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_SRST_DPHY_RX | MESON_SDHC_SRST_DPHY_TX |
73e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_SRST_DMA_IF);
74e4bf1b09SMartin Blumenstingl 	usleep_range(10, 100);
75e4bf1b09SMartin Blumenstingl 
76e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_SRST, 0);
77e4bf1b09SMartin Blumenstingl 	usleep_range(10, 100);
78e4bf1b09SMartin Blumenstingl }
79e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_clear_fifo(struct mmc_host * mmc)80e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_clear_fifo(struct mmc_host *mmc)
81e4bf1b09SMartin Blumenstingl {
82e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
83e4bf1b09SMartin Blumenstingl 	u32 stat;
84e4bf1b09SMartin Blumenstingl 
85e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
86e4bf1b09SMartin Blumenstingl 	if (!FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat) &&
87e4bf1b09SMartin Blumenstingl 	    !FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat))
88e4bf1b09SMartin Blumenstingl 		return;
89e4bf1b09SMartin Blumenstingl 
90e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_SRST, MESON_SDHC_SRST_RXFIFO |
91e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_SRST_TXFIFO | MESON_SDHC_SRST_MAIN_CTRL);
92e4bf1b09SMartin Blumenstingl 	udelay(5);
93e4bf1b09SMartin Blumenstingl 
94e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
95e4bf1b09SMartin Blumenstingl 	if (FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat) ||
96e4bf1b09SMartin Blumenstingl 	    FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat))
97e4bf1b09SMartin Blumenstingl 		dev_warn(mmc_dev(host->mmc),
98e4bf1b09SMartin Blumenstingl 			 "Failed to clear FIFOs, RX: %lu, TX: %lu\n",
99e4bf1b09SMartin Blumenstingl 			 FIELD_GET(MESON_SDHC_STAT_RXFIFO_CNT, stat),
100e4bf1b09SMartin Blumenstingl 			 FIELD_GET(MESON_SDHC_STAT_TXFIFO_CNT, stat));
101e4bf1b09SMartin Blumenstingl }
102e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_wait_cmd_ready(struct mmc_host * mmc)103e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_wait_cmd_ready(struct mmc_host *mmc)
104e4bf1b09SMartin Blumenstingl {
105e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
106e4bf1b09SMartin Blumenstingl 	u32 stat, esta;
107e4bf1b09SMartin Blumenstingl 	int ret;
108e4bf1b09SMartin Blumenstingl 
109e4bf1b09SMartin Blumenstingl 	ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT, stat,
110e4bf1b09SMartin Blumenstingl 				       !(stat & MESON_SDHC_STAT_CMD_BUSY),
111e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
112e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
113e4bf1b09SMartin Blumenstingl 	if (ret) {
114e4bf1b09SMartin Blumenstingl 		dev_warn(mmc_dev(mmc),
115e4bf1b09SMartin Blumenstingl 			 "Failed to poll for CMD_BUSY while processing CMD%d\n",
116e4bf1b09SMartin Blumenstingl 			 host->cmd->opcode);
117c0200efaSMartin Blumenstingl 		meson_mx_sdhc_reset(host);
118e4bf1b09SMartin Blumenstingl 	}
119e4bf1b09SMartin Blumenstingl 
120e4bf1b09SMartin Blumenstingl 	ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, esta,
121e4bf1b09SMartin Blumenstingl 				       !(esta & MESON_SDHC_ESTA_11_13),
122e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_CMD_READY_SLEEP_US,
123e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_CMD_READY_TIMEOUT_US);
124e4bf1b09SMartin Blumenstingl 	if (ret) {
125e4bf1b09SMartin Blumenstingl 		dev_warn(mmc_dev(mmc),
126e4bf1b09SMartin Blumenstingl 			 "Failed to poll for ESTA[13:11] while processing CMD%d\n",
127e4bf1b09SMartin Blumenstingl 			 host->cmd->opcode);
128c0200efaSMartin Blumenstingl 		meson_mx_sdhc_reset(host);
129e4bf1b09SMartin Blumenstingl 	}
130e4bf1b09SMartin Blumenstingl }
131e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_start_cmd(struct mmc_host * mmc,struct mmc_command * cmd)132e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_start_cmd(struct mmc_host *mmc,
133e4bf1b09SMartin Blumenstingl 				    struct mmc_command *cmd)
134e4bf1b09SMartin Blumenstingl {
135e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
136f89b548cSMartin Blumenstingl 	bool manual_stop = false;
137e4bf1b09SMartin Blumenstingl 	u32 ictl, send;
138e4bf1b09SMartin Blumenstingl 	int pack_len;
139e4bf1b09SMartin Blumenstingl 
140e4bf1b09SMartin Blumenstingl 	host->cmd = cmd;
141e4bf1b09SMartin Blumenstingl 
142e4bf1b09SMartin Blumenstingl 	ictl = MESON_SDHC_ICTL_DATA_TIMEOUT | MESON_SDHC_ICTL_DATA_ERR_CRC |
143e4bf1b09SMartin Blumenstingl 	       MESON_SDHC_ICTL_RXFIFO_FULL | MESON_SDHC_ICTL_TXFIFO_EMPTY |
144e4bf1b09SMartin Blumenstingl 	       MESON_SDHC_ICTL_RESP_TIMEOUT | MESON_SDHC_ICTL_RESP_ERR_CRC;
145e4bf1b09SMartin Blumenstingl 
146e4bf1b09SMartin Blumenstingl 	send = FIELD_PREP(MESON_SDHC_SEND_CMD_INDEX, cmd->opcode);
147e4bf1b09SMartin Blumenstingl 
148e4bf1b09SMartin Blumenstingl 	if (cmd->data) {
149e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_CMD_HAS_DATA;
150e4bf1b09SMartin Blumenstingl 		send |= FIELD_PREP(MESON_SDHC_SEND_TOTAL_PACK,
151e4bf1b09SMartin Blumenstingl 				   cmd->data->blocks - 1);
152e4bf1b09SMartin Blumenstingl 
153e4bf1b09SMartin Blumenstingl 		if (cmd->data->blksz < MESON_SDHC_MAX_BLK_SIZE)
154e4bf1b09SMartin Blumenstingl 			pack_len = cmd->data->blksz;
155e4bf1b09SMartin Blumenstingl 		else
156e4bf1b09SMartin Blumenstingl 			pack_len = 0;
157e4bf1b09SMartin Blumenstingl 
158e4bf1b09SMartin Blumenstingl 		if (cmd->data->flags & MMC_DATA_WRITE)
159e4bf1b09SMartin Blumenstingl 			send |= MESON_SDHC_SEND_DATA_DIR;
160e4bf1b09SMartin Blumenstingl 
161e4bf1b09SMartin Blumenstingl 		/*
162e4bf1b09SMartin Blumenstingl 		 * If command with no data, just wait response done
163e4bf1b09SMartin Blumenstingl 		 * interrupt(int[0]), and if command with data transfer, just
164e4bf1b09SMartin Blumenstingl 		 * wait dma done interrupt(int[11]), don't need care about
165e4bf1b09SMartin Blumenstingl 		 * dat0 busy or not.
166e4bf1b09SMartin Blumenstingl 		 */
167e4bf1b09SMartin Blumenstingl 		if (host->platform->hardware_flush_all_cmds ||
168e4bf1b09SMartin Blumenstingl 		    cmd->data->flags & MMC_DATA_WRITE)
169e4bf1b09SMartin Blumenstingl 			/* hardware flush: */
170e4bf1b09SMartin Blumenstingl 			ictl |= MESON_SDHC_ICTL_DMA_DONE;
171e4bf1b09SMartin Blumenstingl 		else
172e4bf1b09SMartin Blumenstingl 			/* software flush: */
173e4bf1b09SMartin Blumenstingl 			ictl |= MESON_SDHC_ICTL_DATA_XFER_OK;
174f89b548cSMartin Blumenstingl 
175f89b548cSMartin Blumenstingl 		/*
176f89b548cSMartin Blumenstingl 		 * Mimic the logic from the vendor driver where (only)
177f89b548cSMartin Blumenstingl 		 * SD_IO_RW_EXTENDED commands with more than one block set the
178f89b548cSMartin Blumenstingl 		 * MESON_SDHC_MISC_MANUAL_STOP bit. This fixes the firmware
179f89b548cSMartin Blumenstingl 		 * download in the brcmfmac driver for a BCM43362/1 card.
180f89b548cSMartin Blumenstingl 		 * Without this sdio_memcpy_toio() (with a size of 219557
181f89b548cSMartin Blumenstingl 		 * bytes) times out if MESON_SDHC_MISC_MANUAL_STOP is not set.
182f89b548cSMartin Blumenstingl 		 */
183f89b548cSMartin Blumenstingl 		manual_stop = cmd->data->blocks > 1 &&
184f89b548cSMartin Blumenstingl 			      cmd->opcode == SD_IO_RW_EXTENDED;
185e4bf1b09SMartin Blumenstingl 	} else {
186e4bf1b09SMartin Blumenstingl 		pack_len = 0;
187e4bf1b09SMartin Blumenstingl 
188e4bf1b09SMartin Blumenstingl 		ictl |= MESON_SDHC_ICTL_RESP_OK;
189e4bf1b09SMartin Blumenstingl 	}
190e4bf1b09SMartin Blumenstingl 
191f89b548cSMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_MISC,
192f89b548cSMartin Blumenstingl 			   MESON_SDHC_MISC_MANUAL_STOP,
193f89b548cSMartin Blumenstingl 			   manual_stop ? MESON_SDHC_MISC_MANUAL_STOP : 0);
194f89b548cSMartin Blumenstingl 
195e4bf1b09SMartin Blumenstingl 	if (cmd->opcode == MMC_STOP_TRANSMISSION)
196e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_DATA_STOP;
197e4bf1b09SMartin Blumenstingl 
198e4bf1b09SMartin Blumenstingl 	if (cmd->flags & MMC_RSP_PRESENT)
199e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_CMD_HAS_RESP;
200e4bf1b09SMartin Blumenstingl 
201e4bf1b09SMartin Blumenstingl 	if (cmd->flags & MMC_RSP_136) {
202e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_RESP_LEN;
203e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_RESP_NO_CRC;
204e4bf1b09SMartin Blumenstingl 	}
205e4bf1b09SMartin Blumenstingl 
206e4bf1b09SMartin Blumenstingl 	if (!(cmd->flags & MMC_RSP_CRC))
207e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_RESP_NO_CRC;
208e4bf1b09SMartin Blumenstingl 
209e4bf1b09SMartin Blumenstingl 	if (cmd->flags & MMC_RSP_BUSY)
210e4bf1b09SMartin Blumenstingl 		send |= MESON_SDHC_SEND_R1B;
211e4bf1b09SMartin Blumenstingl 
212e4bf1b09SMartin Blumenstingl 	/* enable the new IRQs and mask all pending ones */
213e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ICTL, ictl);
214e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS);
215e4bf1b09SMartin Blumenstingl 
216e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ARGU, cmd->arg);
217e4bf1b09SMartin Blumenstingl 
218e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
219e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_CTRL_PACK_LEN,
220e4bf1b09SMartin Blumenstingl 			   FIELD_PREP(MESON_SDHC_CTRL_PACK_LEN, pack_len));
221e4bf1b09SMartin Blumenstingl 
222e4bf1b09SMartin Blumenstingl 	if (cmd->data)
223e4bf1b09SMartin Blumenstingl 		regmap_write(host->regmap, MESON_SDHC_ADDR,
224e4bf1b09SMartin Blumenstingl 			     sg_dma_address(cmd->data->sg));
225e4bf1b09SMartin Blumenstingl 
226e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_wait_cmd_ready(mmc);
227e4bf1b09SMartin Blumenstingl 
228e4bf1b09SMartin Blumenstingl 	if (cmd->data)
229e4bf1b09SMartin Blumenstingl 		host->platform->set_pdma(mmc);
230e4bf1b09SMartin Blumenstingl 
231e4bf1b09SMartin Blumenstingl 	if (host->platform->wait_before_send)
232e4bf1b09SMartin Blumenstingl 		host->platform->wait_before_send(mmc);
233e4bf1b09SMartin Blumenstingl 
234e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_SEND, send);
235e4bf1b09SMartin Blumenstingl }
236e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_disable_clks(struct mmc_host * mmc)237e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_disable_clks(struct mmc_host *mmc)
238e4bf1b09SMartin Blumenstingl {
239e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
240e4bf1b09SMartin Blumenstingl 
241e4bf1b09SMartin Blumenstingl 	if (!host->bulk_clks_enabled)
242e4bf1b09SMartin Blumenstingl 		return;
243e4bf1b09SMartin Blumenstingl 
244e4bf1b09SMartin Blumenstingl 	clk_bulk_disable_unprepare(MESON_SDHC_NUM_BULK_CLKS, host->bulk_clks);
245e4bf1b09SMartin Blumenstingl 
246e4bf1b09SMartin Blumenstingl 	host->bulk_clks_enabled = false;
247e4bf1b09SMartin Blumenstingl }
248e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_enable_clks(struct mmc_host * mmc)249e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_enable_clks(struct mmc_host *mmc)
250e4bf1b09SMartin Blumenstingl {
251e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
252e4bf1b09SMartin Blumenstingl 	int ret;
253e4bf1b09SMartin Blumenstingl 
254e4bf1b09SMartin Blumenstingl 	if (host->bulk_clks_enabled)
255e4bf1b09SMartin Blumenstingl 		return 0;
256e4bf1b09SMartin Blumenstingl 
257e4bf1b09SMartin Blumenstingl 	ret = clk_bulk_prepare_enable(MESON_SDHC_NUM_BULK_CLKS,
258e4bf1b09SMartin Blumenstingl 				      host->bulk_clks);
259e4bf1b09SMartin Blumenstingl 	if (ret)
260e4bf1b09SMartin Blumenstingl 		return ret;
261e4bf1b09SMartin Blumenstingl 
262e4bf1b09SMartin Blumenstingl 	host->bulk_clks_enabled = true;
263e4bf1b09SMartin Blumenstingl 
264e4bf1b09SMartin Blumenstingl 	return 0;
265e4bf1b09SMartin Blumenstingl }
266e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_set_clk(struct mmc_host * mmc,struct mmc_ios * ios)267e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_set_clk(struct mmc_host *mmc, struct mmc_ios *ios)
268e4bf1b09SMartin Blumenstingl {
269e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
2708c124d99SZiyang Huang 	u32 val, rx_clk_phase;
271e4bf1b09SMartin Blumenstingl 	int ret;
272e4bf1b09SMartin Blumenstingl 
273e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_disable_clks(mmc);
274e4bf1b09SMartin Blumenstingl 
275e4bf1b09SMartin Blumenstingl 	if (ios->clock) {
276e4bf1b09SMartin Blumenstingl 		ret = clk_set_rate(host->sd_clk, ios->clock);
277e4bf1b09SMartin Blumenstingl 		if (ret) {
278e4bf1b09SMartin Blumenstingl 			dev_warn(mmc_dev(mmc),
279e4bf1b09SMartin Blumenstingl 				 "Failed to set MMC clock to %uHz: %d\n",
280e4bf1b09SMartin Blumenstingl 				 ios->clock, host->error);
281e4bf1b09SMartin Blumenstingl 			return ret;
282e4bf1b09SMartin Blumenstingl 		}
283e4bf1b09SMartin Blumenstingl 
284e4bf1b09SMartin Blumenstingl 		ret = meson_mx_sdhc_enable_clks(mmc);
285e4bf1b09SMartin Blumenstingl 		if (ret)
286e4bf1b09SMartin Blumenstingl 			return ret;
287e4bf1b09SMartin Blumenstingl 
288e4bf1b09SMartin Blumenstingl 		mmc->actual_clock = clk_get_rate(host->sd_clk);
289e4bf1b09SMartin Blumenstingl 
290e4bf1b09SMartin Blumenstingl 		/*
2918c124d99SZiyang Huang 		 * Phase 90 should work in most cases. For data transmission,
2928c124d99SZiyang Huang 		 * meson_mx_sdhc_execute_tuning() will find a accurate value
293e4bf1b09SMartin Blumenstingl 		 */
2948c124d99SZiyang Huang 		regmap_read(host->regmap, MESON_SDHC_CLKC, &val);
2958c124d99SZiyang Huang 		rx_clk_phase = FIELD_GET(MESON_SDHC_CLKC_CLK_DIV, val) / 4;
296e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
297e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_CLK2_RX_CLK_PHASE,
298e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
299e4bf1b09SMartin Blumenstingl 					      rx_clk_phase));
300e4bf1b09SMartin Blumenstingl 	} else {
301e4bf1b09SMartin Blumenstingl 		mmc->actual_clock = 0;
302e4bf1b09SMartin Blumenstingl 	}
303e4bf1b09SMartin Blumenstingl 
304e4bf1b09SMartin Blumenstingl 	return 0;
305e4bf1b09SMartin Blumenstingl }
306e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)307e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
308e4bf1b09SMartin Blumenstingl {
309e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
310e4bf1b09SMartin Blumenstingl 	unsigned short vdd = ios->vdd;
311e4bf1b09SMartin Blumenstingl 
312e4bf1b09SMartin Blumenstingl 	switch (ios->power_mode) {
313e4bf1b09SMartin Blumenstingl 	case MMC_POWER_OFF:
314e4bf1b09SMartin Blumenstingl 		vdd = 0;
315e4bf1b09SMartin Blumenstingl 		fallthrough;
316e4bf1b09SMartin Blumenstingl 
317e4bf1b09SMartin Blumenstingl 	case MMC_POWER_UP:
318e4bf1b09SMartin Blumenstingl 		if (!IS_ERR(mmc->supply.vmmc)) {
319e4bf1b09SMartin Blumenstingl 			host->error = mmc_regulator_set_ocr(mmc,
320e4bf1b09SMartin Blumenstingl 							    mmc->supply.vmmc,
321e4bf1b09SMartin Blumenstingl 							    vdd);
322e4bf1b09SMartin Blumenstingl 			if (host->error)
323e4bf1b09SMartin Blumenstingl 				return;
324e4bf1b09SMartin Blumenstingl 		}
325e4bf1b09SMartin Blumenstingl 
326e4bf1b09SMartin Blumenstingl 		break;
327e4bf1b09SMartin Blumenstingl 
328e4bf1b09SMartin Blumenstingl 	case MMC_POWER_ON:
329e4bf1b09SMartin Blumenstingl 		break;
330e4bf1b09SMartin Blumenstingl 	}
331e4bf1b09SMartin Blumenstingl 
332e4bf1b09SMartin Blumenstingl 	host->error = meson_mx_sdhc_set_clk(mmc, ios);
333e4bf1b09SMartin Blumenstingl 	if (host->error)
334e4bf1b09SMartin Blumenstingl 		return;
335e4bf1b09SMartin Blumenstingl 
336e4bf1b09SMartin Blumenstingl 	switch (ios->bus_width) {
337e4bf1b09SMartin Blumenstingl 	case MMC_BUS_WIDTH_1:
338e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
339e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_CTRL_DAT_TYPE,
340e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 0));
341e4bf1b09SMartin Blumenstingl 		break;
342e4bf1b09SMartin Blumenstingl 
343e4bf1b09SMartin Blumenstingl 	case MMC_BUS_WIDTH_4:
344e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
345e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_CTRL_DAT_TYPE,
346e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 1));
347e4bf1b09SMartin Blumenstingl 		break;
348e4bf1b09SMartin Blumenstingl 
349e4bf1b09SMartin Blumenstingl 	case MMC_BUS_WIDTH_8:
350e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_CTRL,
351e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_CTRL_DAT_TYPE,
352e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_CTRL_DAT_TYPE, 2));
353e4bf1b09SMartin Blumenstingl 		break;
354e4bf1b09SMartin Blumenstingl 
355e4bf1b09SMartin Blumenstingl 	default:
356e4bf1b09SMartin Blumenstingl 		dev_err(mmc_dev(mmc), "unsupported bus width: %d\n",
357e4bf1b09SMartin Blumenstingl 			ios->bus_width);
358e4bf1b09SMartin Blumenstingl 		host->error = -EINVAL;
359e4bf1b09SMartin Blumenstingl 		return;
360e4bf1b09SMartin Blumenstingl 	}
361e4bf1b09SMartin Blumenstingl }
362e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_map_dma(struct mmc_host * mmc,struct mmc_request * mrq)363e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_map_dma(struct mmc_host *mmc, struct mmc_request *mrq)
364e4bf1b09SMartin Blumenstingl {
365e4bf1b09SMartin Blumenstingl 	struct mmc_data *data = mrq->data;
366f7865ad8SJack Wang 	unsigned int dma_len;
367e4bf1b09SMartin Blumenstingl 
368e4bf1b09SMartin Blumenstingl 	if (!data)
369e4bf1b09SMartin Blumenstingl 		return 0;
370e4bf1b09SMartin Blumenstingl 
371e4bf1b09SMartin Blumenstingl 	dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
372e4bf1b09SMartin Blumenstingl 			     mmc_get_dma_dir(data));
373f7865ad8SJack Wang 	if (!dma_len) {
374e4bf1b09SMartin Blumenstingl 		dev_err(mmc_dev(mmc), "dma_map_sg failed\n");
375e4bf1b09SMartin Blumenstingl 		return -ENOMEM;
376e4bf1b09SMartin Blumenstingl 	}
377e4bf1b09SMartin Blumenstingl 
378e4bf1b09SMartin Blumenstingl 	return 0;
379e4bf1b09SMartin Blumenstingl }
380e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_request(struct mmc_host * mmc,struct mmc_request * mrq)381e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_request(struct mmc_host *mmc, struct mmc_request *mrq)
382e4bf1b09SMartin Blumenstingl {
383e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
384e4bf1b09SMartin Blumenstingl 	struct mmc_command *cmd = mrq->cmd;
385e4bf1b09SMartin Blumenstingl 
386e4bf1b09SMartin Blumenstingl 	if (!host->error)
387e4bf1b09SMartin Blumenstingl 		host->error = meson_mx_sdhc_map_dma(mmc, mrq);
388e4bf1b09SMartin Blumenstingl 
389e4bf1b09SMartin Blumenstingl 	if (host->error) {
390e4bf1b09SMartin Blumenstingl 		cmd->error = host->error;
391e4bf1b09SMartin Blumenstingl 		mmc_request_done(mmc, mrq);
392e4bf1b09SMartin Blumenstingl 		return;
393e4bf1b09SMartin Blumenstingl 	}
394e4bf1b09SMartin Blumenstingl 
395e4bf1b09SMartin Blumenstingl 	host->mrq = mrq;
396e4bf1b09SMartin Blumenstingl 
397e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_start_cmd(mmc, mrq->cmd);
398e4bf1b09SMartin Blumenstingl }
399e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_card_busy(struct mmc_host * mmc)400e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_card_busy(struct mmc_host *mmc)
401e4bf1b09SMartin Blumenstingl {
402e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
403e4bf1b09SMartin Blumenstingl 	u32 stat;
404e4bf1b09SMartin Blumenstingl 
405e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_STAT, &stat);
406e4bf1b09SMartin Blumenstingl 	return FIELD_GET(MESON_SDHC_STAT_DAT3_0, stat) == 0;
407e4bf1b09SMartin Blumenstingl }
408e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_tuning_point_matches(struct mmc_host * mmc,u32 opcode)409e4bf1b09SMartin Blumenstingl static bool meson_mx_sdhc_tuning_point_matches(struct mmc_host *mmc,
410e4bf1b09SMartin Blumenstingl 					       u32 opcode)
411e4bf1b09SMartin Blumenstingl {
412e4bf1b09SMartin Blumenstingl 	unsigned int i, num_matches = 0;
413e4bf1b09SMartin Blumenstingl 	int ret;
414e4bf1b09SMartin Blumenstingl 
415e4bf1b09SMartin Blumenstingl 	for (i = 0; i < MESON_SDHC_NUM_TUNING_TRIES; i++) {
416e4bf1b09SMartin Blumenstingl 		ret = mmc_send_tuning(mmc, opcode, NULL);
417e4bf1b09SMartin Blumenstingl 		if (!ret)
418e4bf1b09SMartin Blumenstingl 			num_matches++;
419e4bf1b09SMartin Blumenstingl 	}
420e4bf1b09SMartin Blumenstingl 
421e4bf1b09SMartin Blumenstingl 	return num_matches == MESON_SDHC_NUM_TUNING_TRIES;
422e4bf1b09SMartin Blumenstingl }
423e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_execute_tuning(struct mmc_host * mmc,u32 opcode)424e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_execute_tuning(struct mmc_host *mmc, u32 opcode)
425e4bf1b09SMartin Blumenstingl {
426e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
427e4bf1b09SMartin Blumenstingl 	int div, start, len, best_start, best_len;
428e4bf1b09SMartin Blumenstingl 	int curr_phase, old_phase, new_phase;
429e4bf1b09SMartin Blumenstingl 	u32 val;
430e4bf1b09SMartin Blumenstingl 
431e4bf1b09SMartin Blumenstingl 	len = 0;
432e4bf1b09SMartin Blumenstingl 	start = 0;
433e4bf1b09SMartin Blumenstingl 	best_len = 0;
434e4bf1b09SMartin Blumenstingl 
435e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_CLK2, &val);
436e4bf1b09SMartin Blumenstingl 	old_phase = FIELD_GET(MESON_SDHC_CLK2_RX_CLK_PHASE, val);
437e4bf1b09SMartin Blumenstingl 
438e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_CLKC, &val);
439e4bf1b09SMartin Blumenstingl 	div = FIELD_GET(MESON_SDHC_CLKC_CLK_DIV, val);
440e4bf1b09SMartin Blumenstingl 
441e4bf1b09SMartin Blumenstingl 	for (curr_phase = 0; curr_phase <= div; curr_phase++) {
442e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
443e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_CLK2_RX_CLK_PHASE,
444e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
445e4bf1b09SMartin Blumenstingl 					      curr_phase));
446e4bf1b09SMartin Blumenstingl 
447e4bf1b09SMartin Blumenstingl 		if (meson_mx_sdhc_tuning_point_matches(mmc, opcode)) {
448e4bf1b09SMartin Blumenstingl 			if (!len) {
449e4bf1b09SMartin Blumenstingl 				start = curr_phase;
450e4bf1b09SMartin Blumenstingl 
451e4bf1b09SMartin Blumenstingl 				dev_dbg(mmc_dev(mmc),
452e4bf1b09SMartin Blumenstingl 					"New RX phase window starts at %u\n",
453e4bf1b09SMartin Blumenstingl 					start);
454e4bf1b09SMartin Blumenstingl 			}
455e4bf1b09SMartin Blumenstingl 
456e4bf1b09SMartin Blumenstingl 			len++;
457e4bf1b09SMartin Blumenstingl 		} else {
458e4bf1b09SMartin Blumenstingl 			if (len > best_len) {
459e4bf1b09SMartin Blumenstingl 				best_start = start;
460e4bf1b09SMartin Blumenstingl 				best_len = len;
461e4bf1b09SMartin Blumenstingl 
462e4bf1b09SMartin Blumenstingl 				dev_dbg(mmc_dev(mmc),
463e4bf1b09SMartin Blumenstingl 					"New best RX phase window: %u - %u\n",
464e4bf1b09SMartin Blumenstingl 					best_start, best_start + best_len);
465e4bf1b09SMartin Blumenstingl 			}
466e4bf1b09SMartin Blumenstingl 
467e4bf1b09SMartin Blumenstingl 			/* reset the current window */
468e4bf1b09SMartin Blumenstingl 			len = 0;
469e4bf1b09SMartin Blumenstingl 		}
470e4bf1b09SMartin Blumenstingl 	}
471e4bf1b09SMartin Blumenstingl 
472e4bf1b09SMartin Blumenstingl 	if (len > best_len)
473e4bf1b09SMartin Blumenstingl 		/* the last window is the best (or possibly only) window */
474e4bf1b09SMartin Blumenstingl 		new_phase = start + (len / 2);
475e4bf1b09SMartin Blumenstingl 	else if (best_len)
476e4bf1b09SMartin Blumenstingl 		/* there was a better window than the last */
477e4bf1b09SMartin Blumenstingl 		new_phase = best_start + (best_len / 2);
478e4bf1b09SMartin Blumenstingl 	else
479e4bf1b09SMartin Blumenstingl 		/* no window was found at all, reset to the original phase */
480e4bf1b09SMartin Blumenstingl 		new_phase = old_phase;
481e4bf1b09SMartin Blumenstingl 
482e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_CLK2,
483e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_CLK2_RX_CLK_PHASE,
484e4bf1b09SMartin Blumenstingl 			   FIELD_PREP(MESON_SDHC_CLK2_RX_CLK_PHASE,
485e4bf1b09SMartin Blumenstingl 				      new_phase));
486e4bf1b09SMartin Blumenstingl 
487e4bf1b09SMartin Blumenstingl 	if (!len && !best_len)
488e4bf1b09SMartin Blumenstingl 		return -EIO;
489e4bf1b09SMartin Blumenstingl 
490e4bf1b09SMartin Blumenstingl 	dev_dbg(mmc_dev(mmc), "Tuned RX clock phase to %u\n", new_phase);
491e4bf1b09SMartin Blumenstingl 
492e4bf1b09SMartin Blumenstingl 	return 0;
493e4bf1b09SMartin Blumenstingl }
494e4bf1b09SMartin Blumenstingl 
495e4bf1b09SMartin Blumenstingl static const struct mmc_host_ops meson_mx_sdhc_ops = {
496e4bf1b09SMartin Blumenstingl 	.request			= meson_mx_sdhc_request,
497e4bf1b09SMartin Blumenstingl 	.set_ios			= meson_mx_sdhc_set_ios,
498e4bf1b09SMartin Blumenstingl 	.card_busy			= meson_mx_sdhc_card_busy,
499e4bf1b09SMartin Blumenstingl 	.execute_tuning			= meson_mx_sdhc_execute_tuning,
500e4bf1b09SMartin Blumenstingl 	.get_cd				= mmc_gpio_get_cd,
501e4bf1b09SMartin Blumenstingl 	.get_ro				= mmc_gpio_get_ro,
502e4bf1b09SMartin Blumenstingl };
503e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_request_done(struct meson_mx_sdhc_host * host)504e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_request_done(struct meson_mx_sdhc_host *host)
505e4bf1b09SMartin Blumenstingl {
506e4bf1b09SMartin Blumenstingl 	struct mmc_request *mrq = host->mrq;
507e4bf1b09SMartin Blumenstingl 	struct mmc_host *mmc = host->mmc;
508e4bf1b09SMartin Blumenstingl 
509e4bf1b09SMartin Blumenstingl 	/* disable interrupts and mask all pending ones */
510e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_ICTL,
511e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_ICTL_ALL_IRQS, 0);
512e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_ISTA,
513e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_ISTA_ALL_IRQS, MESON_SDHC_ISTA_ALL_IRQS);
514e4bf1b09SMartin Blumenstingl 
515e4bf1b09SMartin Blumenstingl 	host->mrq = NULL;
516e4bf1b09SMartin Blumenstingl 	host->cmd = NULL;
517e4bf1b09SMartin Blumenstingl 
518e4bf1b09SMartin Blumenstingl 	mmc_request_done(mmc, mrq);
519e4bf1b09SMartin Blumenstingl }
520e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_read_response(struct meson_mx_sdhc_host * host,u8 idx)521e4bf1b09SMartin Blumenstingl static u32 meson_mx_sdhc_read_response(struct meson_mx_sdhc_host *host, u8 idx)
522e4bf1b09SMartin Blumenstingl {
523e4bf1b09SMartin Blumenstingl 	u32 val;
524e4bf1b09SMartin Blumenstingl 
525e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
526e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_PDMA_DMA_MODE, 0);
527e4bf1b09SMartin Blumenstingl 
528e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
529e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_PDMA_PIO_RDRESP,
530e4bf1b09SMartin Blumenstingl 			   FIELD_PREP(MESON_SDHC_PDMA_PIO_RDRESP, idx));
531e4bf1b09SMartin Blumenstingl 
532e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_ARGU, &val);
533e4bf1b09SMartin Blumenstingl 
534e4bf1b09SMartin Blumenstingl 	return val;
535e4bf1b09SMartin Blumenstingl }
536e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_irq(int irq,void * data)537e4bf1b09SMartin Blumenstingl static irqreturn_t meson_mx_sdhc_irq(int irq, void *data)
538e4bf1b09SMartin Blumenstingl {
539e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = data;
540e4bf1b09SMartin Blumenstingl 	struct mmc_command *cmd = host->cmd;
541e4bf1b09SMartin Blumenstingl 	u32 ictl, ista;
542e4bf1b09SMartin Blumenstingl 
543e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_ICTL, &ictl);
544e4bf1b09SMartin Blumenstingl 	regmap_read(host->regmap, MESON_SDHC_ISTA, &ista);
545e4bf1b09SMartin Blumenstingl 
546e4bf1b09SMartin Blumenstingl 	if (!(ictl & ista))
547e4bf1b09SMartin Blumenstingl 		return IRQ_NONE;
548e4bf1b09SMartin Blumenstingl 
549e4bf1b09SMartin Blumenstingl 	if (ista & MESON_SDHC_ISTA_RXFIFO_FULL ||
550e4bf1b09SMartin Blumenstingl 	    ista & MESON_SDHC_ISTA_TXFIFO_EMPTY)
551e4bf1b09SMartin Blumenstingl 		cmd->error = -EIO;
552e4bf1b09SMartin Blumenstingl 	else if (ista & MESON_SDHC_ISTA_RESP_ERR_CRC)
553e4bf1b09SMartin Blumenstingl 		cmd->error = -EILSEQ;
554e4bf1b09SMartin Blumenstingl 	else if (ista & MESON_SDHC_ISTA_RESP_TIMEOUT)
555e4bf1b09SMartin Blumenstingl 		cmd->error = -ETIMEDOUT;
556e4bf1b09SMartin Blumenstingl 
557e4bf1b09SMartin Blumenstingl 	if (cmd->data) {
558e4bf1b09SMartin Blumenstingl 		if (ista & MESON_SDHC_ISTA_DATA_ERR_CRC)
559e4bf1b09SMartin Blumenstingl 			cmd->data->error = -EILSEQ;
560e4bf1b09SMartin Blumenstingl 		else if (ista & MESON_SDHC_ISTA_DATA_TIMEOUT)
561e4bf1b09SMartin Blumenstingl 			cmd->data->error = -ETIMEDOUT;
562e4bf1b09SMartin Blumenstingl 	}
563e4bf1b09SMartin Blumenstingl 
564e4bf1b09SMartin Blumenstingl 	if (cmd->error || (cmd->data && cmd->data->error))
565e4bf1b09SMartin Blumenstingl 		dev_dbg(mmc_dev(host->mmc), "CMD%d error, ISTA: 0x%08x\n",
566e4bf1b09SMartin Blumenstingl 			cmd->opcode, ista);
567e4bf1b09SMartin Blumenstingl 
568e4bf1b09SMartin Blumenstingl 	return IRQ_WAKE_THREAD;
569e4bf1b09SMartin Blumenstingl }
570e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_irq_thread(int irq,void * irq_data)571e4bf1b09SMartin Blumenstingl static irqreturn_t meson_mx_sdhc_irq_thread(int irq, void *irq_data)
572e4bf1b09SMartin Blumenstingl {
573e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = irq_data;
574e4bf1b09SMartin Blumenstingl 	struct mmc_command *cmd;
575e4bf1b09SMartin Blumenstingl 	u32 val;
576e4bf1b09SMartin Blumenstingl 
577e4bf1b09SMartin Blumenstingl 	cmd = host->cmd;
578e4bf1b09SMartin Blumenstingl 	if (WARN_ON(!cmd))
579e4bf1b09SMartin Blumenstingl 		return IRQ_HANDLED;
580e4bf1b09SMartin Blumenstingl 
581e4bf1b09SMartin Blumenstingl 	if (cmd->data && !cmd->data->error) {
582e4bf1b09SMartin Blumenstingl 		if (!host->platform->hardware_flush_all_cmds &&
583e4bf1b09SMartin Blumenstingl 		    cmd->data->flags & MMC_DATA_READ) {
584e4bf1b09SMartin Blumenstingl 			meson_mx_sdhc_wait_cmd_ready(host->mmc);
585e4bf1b09SMartin Blumenstingl 
586c70805dcSMartin Blumenstingl 			/*
587c70805dcSMartin Blumenstingl 			 * If MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH was
588c70805dcSMartin Blumenstingl 			 * previously 0x1 then it has to be set to 0x3. If it
589c70805dcSMartin Blumenstingl 			 * was 0x0 before then it has to be set to 0x2. Without
590c70805dcSMartin Blumenstingl 			 * this reading SD cards sometimes transfers garbage,
591c70805dcSMartin Blumenstingl 			 * which results in cards not being detected due to:
592c70805dcSMartin Blumenstingl 			 *   unrecognised SCR structure version <random number>
593c70805dcSMartin Blumenstingl 			 */
594e4bf1b09SMartin Blumenstingl 			val = FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
595e4bf1b09SMartin Blumenstingl 					 2);
596c70805dcSMartin Blumenstingl 			regmap_update_bits(host->regmap, MESON_SDHC_PDMA, val,
597e4bf1b09SMartin Blumenstingl 					   val);
598e4bf1b09SMartin Blumenstingl 		}
599e4bf1b09SMartin Blumenstingl 
600e4bf1b09SMartin Blumenstingl 		dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg,
601e4bf1b09SMartin Blumenstingl 			     cmd->data->sg_len, mmc_get_dma_dir(cmd->data));
602e4bf1b09SMartin Blumenstingl 
603e4bf1b09SMartin Blumenstingl 		cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks;
604e4bf1b09SMartin Blumenstingl 	}
605e4bf1b09SMartin Blumenstingl 
606e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_wait_cmd_ready(host->mmc);
607e4bf1b09SMartin Blumenstingl 
608e4bf1b09SMartin Blumenstingl 	if (cmd->flags & MMC_RSP_136) {
609e4bf1b09SMartin Blumenstingl 		cmd->resp[0] = meson_mx_sdhc_read_response(host, 4);
610e4bf1b09SMartin Blumenstingl 		cmd->resp[1] = meson_mx_sdhc_read_response(host, 3);
611e4bf1b09SMartin Blumenstingl 		cmd->resp[2] = meson_mx_sdhc_read_response(host, 2);
612e4bf1b09SMartin Blumenstingl 		cmd->resp[3] = meson_mx_sdhc_read_response(host, 1);
613e4bf1b09SMartin Blumenstingl 	} else {
614e4bf1b09SMartin Blumenstingl 		cmd->resp[0] = meson_mx_sdhc_read_response(host, 0);
615e4bf1b09SMartin Blumenstingl 	}
616e4bf1b09SMartin Blumenstingl 
617e4bf1b09SMartin Blumenstingl 	if (cmd->error == -EIO || cmd->error == -ETIMEDOUT)
618c0200efaSMartin Blumenstingl 		meson_mx_sdhc_reset(host);
619e4bf1b09SMartin Blumenstingl 	else if (cmd->data)
620e4bf1b09SMartin Blumenstingl 		/*
621e4bf1b09SMartin Blumenstingl 		 * Clear the FIFOs after completing data transfers to prevent
622e4bf1b09SMartin Blumenstingl 		 * corrupting data on write access. It's not clear why this is
623e4bf1b09SMartin Blumenstingl 		 * needed (for reads and writes), but it mimics what the BSP
624e4bf1b09SMartin Blumenstingl 		 * kernel did.
625e4bf1b09SMartin Blumenstingl 		 */
626e4bf1b09SMartin Blumenstingl 		meson_mx_sdhc_clear_fifo(host->mmc);
627e4bf1b09SMartin Blumenstingl 
628e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_request_done(host);
629e4bf1b09SMartin Blumenstingl 
630e4bf1b09SMartin Blumenstingl 	return IRQ_HANDLED;
631e4bf1b09SMartin Blumenstingl }
632e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_init_hw_meson8(struct mmc_host * mmc)633e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_init_hw_meson8(struct mmc_host *mmc)
634e4bf1b09SMartin Blumenstingl {
635e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
636e4bf1b09SMartin Blumenstingl 
637e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_MISC,
638e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_TXSTART_THRES, 7) |
639e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_WCRC_ERR_PATT, 5) |
640e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_WCRC_OK_PATT, 2));
641e4bf1b09SMartin Blumenstingl 
642e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ENHC,
643e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_RXFIFO_TH, 63) |
644e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_ENHC_MESON6_DMA_WR_RESP |
645e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_MESON6_RX_TIMEOUT, 255) |
646e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_SDIO_IRQ_PERIOD, 12));
647e4bf1b09SMartin Blumenstingl };
648e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_set_pdma_meson8(struct mmc_host * mmc)649e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_set_pdma_meson8(struct mmc_host *mmc)
650e4bf1b09SMartin Blumenstingl {
651e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
652e4bf1b09SMartin Blumenstingl 
653e4bf1b09SMartin Blumenstingl 	if (host->cmd->data->flags & MMC_DATA_WRITE)
654e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
655e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_DMA_MODE |
656e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_RD_BURST |
657e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_TXFIFO_FILL,
658e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_DMA_MODE |
659e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 31) |
660e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_TXFIFO_FILL);
661e4bf1b09SMartin Blumenstingl 	else
662e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
663e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_DMA_MODE |
664e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
665e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_DMA_MODE |
666e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_MANUAL_FLUSH,
667e4bf1b09SMartin Blumenstingl 					      1));
668e4bf1b09SMartin Blumenstingl 
669e4bf1b09SMartin Blumenstingl 	if (host->cmd->data->flags & MMC_DATA_WRITE)
670e4bf1b09SMartin Blumenstingl 		regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
671e4bf1b09SMartin Blumenstingl 				   MESON_SDHC_PDMA_RD_BURST,
672e4bf1b09SMartin Blumenstingl 				   FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 15));
673e4bf1b09SMartin Blumenstingl }
674e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_wait_before_send_meson8(struct mmc_host * mmc)675e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_wait_before_send_meson8(struct mmc_host *mmc)
676e4bf1b09SMartin Blumenstingl {
677e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
678e4bf1b09SMartin Blumenstingl 	u32 val;
679e4bf1b09SMartin Blumenstingl 	int ret;
680e4bf1b09SMartin Blumenstingl 
681e4bf1b09SMartin Blumenstingl 	ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_ESTA, val,
682e4bf1b09SMartin Blumenstingl 				       val == 0,
683e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US,
684e4bf1b09SMartin Blumenstingl 				       MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US);
685e4bf1b09SMartin Blumenstingl 	if (ret)
686e4bf1b09SMartin Blumenstingl 		dev_warn(mmc_dev(mmc),
687e4bf1b09SMartin Blumenstingl 			 "Failed to wait for ESTA to clear: 0x%08x\n", val);
688e4bf1b09SMartin Blumenstingl 
689e4bf1b09SMartin Blumenstingl 	if (host->cmd->data && host->cmd->data->flags & MMC_DATA_WRITE) {
690e4bf1b09SMartin Blumenstingl 		ret = regmap_read_poll_timeout(host->regmap, MESON_SDHC_STAT,
691e4bf1b09SMartin Blumenstingl 					val, val & MESON_SDHC_STAT_TXFIFO_CNT,
692e4bf1b09SMartin Blumenstingl 					MESON_SDHC_WAIT_BEFORE_SEND_SLEEP_US,
693e4bf1b09SMartin Blumenstingl 					MESON_SDHC_WAIT_BEFORE_SEND_TIMEOUT_US);
694e4bf1b09SMartin Blumenstingl 		if (ret)
695e4bf1b09SMartin Blumenstingl 			dev_warn(mmc_dev(mmc),
696e4bf1b09SMartin Blumenstingl 				 "Failed to wait for TX FIFO to fill\n");
697e4bf1b09SMartin Blumenstingl 	}
698e4bf1b09SMartin Blumenstingl }
699e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_init_hw_meson8m2(struct mmc_host * mmc)700e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_init_hw_meson8m2(struct mmc_host *mmc)
701e4bf1b09SMartin Blumenstingl {
702e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
703e4bf1b09SMartin Blumenstingl 
704e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_MISC,
705e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_TXSTART_THRES, 6) |
706e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_WCRC_ERR_PATT, 5) |
707e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_MISC_WCRC_OK_PATT, 2));
708e4bf1b09SMartin Blumenstingl 
709e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ENHC,
710e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_RXFIFO_TH, 64) |
711e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_MESON8M2_DEBUG, 1) |
712e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_ENHC_MESON8M2_WRRSP_MODE |
713e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_ENHC_SDIO_IRQ_PERIOD, 12));
714e4bf1b09SMartin Blumenstingl }
715e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_set_pdma_meson8m2(struct mmc_host * mmc)716e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_set_pdma_meson8m2(struct mmc_host *mmc)
717e4bf1b09SMartin Blumenstingl {
718e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
719e4bf1b09SMartin Blumenstingl 
720e4bf1b09SMartin Blumenstingl 	regmap_update_bits(host->regmap, MESON_SDHC_PDMA,
721e4bf1b09SMartin Blumenstingl 			   MESON_SDHC_PDMA_DMA_MODE, MESON_SDHC_PDMA_DMA_MODE);
722e4bf1b09SMartin Blumenstingl }
723e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_init_hw(struct mmc_host * mmc)724e4bf1b09SMartin Blumenstingl static void meson_mx_sdhc_init_hw(struct mmc_host *mmc)
725e4bf1b09SMartin Blumenstingl {
726e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = mmc_priv(mmc);
727e4bf1b09SMartin Blumenstingl 
728c0200efaSMartin Blumenstingl 	meson_mx_sdhc_reset(host);
729e4bf1b09SMartin Blumenstingl 
730e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_CTRL,
731e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_CTRL_RX_PERIOD, 0xf) |
732e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_CTRL_RX_TIMEOUT, 0x7f) |
733e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_CTRL_RX_ENDIAN, 0x7) |
734e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_CTRL_TX_ENDIAN, 0x7));
735e4bf1b09SMartin Blumenstingl 
736e4bf1b09SMartin Blumenstingl 	/*
737e4bf1b09SMartin Blumenstingl 	 * start with a valid divider and enable the memory (un-setting
738e4bf1b09SMartin Blumenstingl 	 * MESON_SDHC_CLKC_MEM_PWR_OFF).
739e4bf1b09SMartin Blumenstingl 	 */
740e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_CLKC, MESON_SDHC_CLKC_CLK_DIV);
741e4bf1b09SMartin Blumenstingl 
742e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_CLK2,
743e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_CLK2_SD_CLK_PHASE, 1));
744e4bf1b09SMartin Blumenstingl 
745e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_PDMA,
746e4bf1b09SMartin Blumenstingl 		     MESON_SDHC_PDMA_DMA_URGENT |
747e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_PDMA_WR_BURST, 7) |
748e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_PDMA_TXFIFO_TH, 49) |
749e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_PDMA_RD_BURST, 15) |
750e4bf1b09SMartin Blumenstingl 		     FIELD_PREP(MESON_SDHC_PDMA_RXFIFO_TH, 7));
751e4bf1b09SMartin Blumenstingl 
752e4bf1b09SMartin Blumenstingl 	/* some initialization bits depend on the SoC: */
753e4bf1b09SMartin Blumenstingl 	host->platform->init_hw(mmc);
754e4bf1b09SMartin Blumenstingl 
755e4bf1b09SMartin Blumenstingl 	/* disable and mask all interrupts: */
756e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ICTL, 0);
757e4bf1b09SMartin Blumenstingl 	regmap_write(host->regmap, MESON_SDHC_ISTA, MESON_SDHC_ISTA_ALL_IRQS);
758e4bf1b09SMartin Blumenstingl }
759e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_probe(struct platform_device * pdev)760e4bf1b09SMartin Blumenstingl static int meson_mx_sdhc_probe(struct platform_device *pdev)
761e4bf1b09SMartin Blumenstingl {
762e4bf1b09SMartin Blumenstingl 	struct device *dev = &pdev->dev;
763e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host;
764e4bf1b09SMartin Blumenstingl 	struct mmc_host *mmc;
765e4bf1b09SMartin Blumenstingl 	void __iomem *base;
766e4bf1b09SMartin Blumenstingl 	int ret, irq;
767e4bf1b09SMartin Blumenstingl 
768*8f4ab5abSBinbin Zhou 	mmc = devm_mmc_alloc_host(dev, sizeof(*host));
769e4bf1b09SMartin Blumenstingl 	if (!mmc)
770e4bf1b09SMartin Blumenstingl 		return -ENOMEM;
771e4bf1b09SMartin Blumenstingl 
772e4bf1b09SMartin Blumenstingl 	host = mmc_priv(mmc);
773e4bf1b09SMartin Blumenstingl 	host->mmc = mmc;
774e4bf1b09SMartin Blumenstingl 
775e4bf1b09SMartin Blumenstingl 	platform_set_drvdata(pdev, host);
776e4bf1b09SMartin Blumenstingl 
777e4bf1b09SMartin Blumenstingl 	host->platform = device_get_match_data(dev);
778e4bf1b09SMartin Blumenstingl 	if (!host->platform)
779e4bf1b09SMartin Blumenstingl 		return -EINVAL;
780e4bf1b09SMartin Blumenstingl 
781e4bf1b09SMartin Blumenstingl 	base = devm_platform_ioremap_resource(pdev, 0);
782e4bf1b09SMartin Blumenstingl 	if (IS_ERR(base))
783e4bf1b09SMartin Blumenstingl 		return PTR_ERR(base);
784e4bf1b09SMartin Blumenstingl 
785e4bf1b09SMartin Blumenstingl 	host->regmap = devm_regmap_init_mmio(dev, base,
786e4bf1b09SMartin Blumenstingl 					     &meson_mx_sdhc_regmap_config);
787e4bf1b09SMartin Blumenstingl 	if (IS_ERR(host->regmap))
788e4bf1b09SMartin Blumenstingl 		return PTR_ERR(host->regmap);
789e4bf1b09SMartin Blumenstingl 
790e4bf1b09SMartin Blumenstingl 	host->pclk = devm_clk_get(dev, "pclk");
791e4bf1b09SMartin Blumenstingl 	if (IS_ERR(host->pclk))
792e4bf1b09SMartin Blumenstingl 		return PTR_ERR(host->pclk);
793e4bf1b09SMartin Blumenstingl 
794e4bf1b09SMartin Blumenstingl 	/* accessing any register requires the module clock to be enabled: */
795e4bf1b09SMartin Blumenstingl 	ret = clk_prepare_enable(host->pclk);
796e4bf1b09SMartin Blumenstingl 	if (ret) {
797e4bf1b09SMartin Blumenstingl 		dev_err(dev, "Failed to enable 'pclk' clock\n");
798e4bf1b09SMartin Blumenstingl 		return ret;
799e4bf1b09SMartin Blumenstingl 	}
800e4bf1b09SMartin Blumenstingl 
801e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_init_hw(mmc);
802e4bf1b09SMartin Blumenstingl 
803e4bf1b09SMartin Blumenstingl 	ret = meson_mx_sdhc_register_clkc(dev, base, host->bulk_clks);
804e4bf1b09SMartin Blumenstingl 	if (ret)
805e4bf1b09SMartin Blumenstingl 		goto err_disable_pclk;
806e4bf1b09SMartin Blumenstingl 
807e4bf1b09SMartin Blumenstingl 	host->sd_clk = host->bulk_clks[1].clk;
808e4bf1b09SMartin Blumenstingl 
809e4bf1b09SMartin Blumenstingl 	/* Get regulators and the supported OCR mask */
810e4bf1b09SMartin Blumenstingl 	ret = mmc_regulator_get_supply(mmc);
811e4bf1b09SMartin Blumenstingl 	if (ret)
812e4bf1b09SMartin Blumenstingl 		goto err_disable_pclk;
813e4bf1b09SMartin Blumenstingl 
814e4bf1b09SMartin Blumenstingl 	mmc->max_req_size = SZ_128K;
815e4bf1b09SMartin Blumenstingl 	mmc->max_seg_size = mmc->max_req_size;
816e4bf1b09SMartin Blumenstingl 	mmc->max_blk_count = FIELD_GET(MESON_SDHC_SEND_TOTAL_PACK, ~0);
817e4bf1b09SMartin Blumenstingl 	mmc->max_blk_size = MESON_SDHC_MAX_BLK_SIZE;
818e4bf1b09SMartin Blumenstingl 	mmc->max_busy_timeout = 30 * MSEC_PER_SEC;
819e4bf1b09SMartin Blumenstingl 	mmc->f_min = clk_round_rate(host->sd_clk, 1);
820e4bf1b09SMartin Blumenstingl 	mmc->f_max = clk_round_rate(host->sd_clk, ULONG_MAX);
821e4bf1b09SMartin Blumenstingl 	mmc->max_current_180 = 300;
822e4bf1b09SMartin Blumenstingl 	mmc->max_current_330 = 300;
823e4bf1b09SMartin Blumenstingl 	mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_HW_RESET;
824e4bf1b09SMartin Blumenstingl 	mmc->ops = &meson_mx_sdhc_ops;
825e4bf1b09SMartin Blumenstingl 
826e4bf1b09SMartin Blumenstingl 	ret = mmc_of_parse(mmc);
827e4bf1b09SMartin Blumenstingl 	if (ret)
828e4bf1b09SMartin Blumenstingl 		goto err_disable_pclk;
829e4bf1b09SMartin Blumenstingl 
830e4bf1b09SMartin Blumenstingl 	irq = platform_get_irq(pdev, 0);
83177bed755SSergey Shtylyov 	if (irq < 0) {
83277bed755SSergey Shtylyov 		ret = irq;
83377bed755SSergey Shtylyov 		goto err_disable_pclk;
83477bed755SSergey Shtylyov 	}
83577bed755SSergey Shtylyov 
836e4bf1b09SMartin Blumenstingl 	ret = devm_request_threaded_irq(dev, irq, meson_mx_sdhc_irq,
837e4bf1b09SMartin Blumenstingl 					meson_mx_sdhc_irq_thread, IRQF_ONESHOT,
838e4bf1b09SMartin Blumenstingl 					NULL, host);
839e4bf1b09SMartin Blumenstingl 	if (ret)
840e4bf1b09SMartin Blumenstingl 		goto err_disable_pclk;
841e4bf1b09SMartin Blumenstingl 
842e4bf1b09SMartin Blumenstingl 	ret = mmc_add_host(mmc);
843e4bf1b09SMartin Blumenstingl 	if (ret)
844e4bf1b09SMartin Blumenstingl 		goto err_disable_pclk;
845e4bf1b09SMartin Blumenstingl 
846e4bf1b09SMartin Blumenstingl 	return 0;
847e4bf1b09SMartin Blumenstingl 
848e4bf1b09SMartin Blumenstingl err_disable_pclk:
849e4bf1b09SMartin Blumenstingl 	clk_disable_unprepare(host->pclk);
850e4bf1b09SMartin Blumenstingl 	return ret;
851e4bf1b09SMartin Blumenstingl }
852e4bf1b09SMartin Blumenstingl 
meson_mx_sdhc_remove(struct platform_device * pdev)8533372487aSYangtao Li static void meson_mx_sdhc_remove(struct platform_device *pdev)
854e4bf1b09SMartin Blumenstingl {
855e4bf1b09SMartin Blumenstingl 	struct meson_mx_sdhc_host *host = platform_get_drvdata(pdev);
856e4bf1b09SMartin Blumenstingl 
857e4bf1b09SMartin Blumenstingl 	mmc_remove_host(host->mmc);
858e4bf1b09SMartin Blumenstingl 
859e4bf1b09SMartin Blumenstingl 	meson_mx_sdhc_disable_clks(host->mmc);
860e4bf1b09SMartin Blumenstingl 
861e4bf1b09SMartin Blumenstingl 	clk_disable_unprepare(host->pclk);
862e4bf1b09SMartin Blumenstingl }
863e4bf1b09SMartin Blumenstingl 
864e4bf1b09SMartin Blumenstingl static const struct meson_mx_sdhc_data meson_mx_sdhc_data_meson8 = {
865e4bf1b09SMartin Blumenstingl 	.init_hw			= meson_mx_sdhc_init_hw_meson8,
866e4bf1b09SMartin Blumenstingl 	.set_pdma			= meson_mx_sdhc_set_pdma_meson8,
867e4bf1b09SMartin Blumenstingl 	.wait_before_send		= meson_mx_sdhc_wait_before_send_meson8,
868e4bf1b09SMartin Blumenstingl 	.hardware_flush_all_cmds	= false,
869e4bf1b09SMartin Blumenstingl };
870e4bf1b09SMartin Blumenstingl 
871e4bf1b09SMartin Blumenstingl static const struct meson_mx_sdhc_data meson_mx_sdhc_data_meson8m2 = {
872e4bf1b09SMartin Blumenstingl 	.init_hw			= meson_mx_sdhc_init_hw_meson8m2,
873e4bf1b09SMartin Blumenstingl 	.set_pdma			= meson_mx_sdhc_set_pdma_meson8m2,
874e4bf1b09SMartin Blumenstingl 	.hardware_flush_all_cmds	= true,
875e4bf1b09SMartin Blumenstingl };
876e4bf1b09SMartin Blumenstingl 
877e4bf1b09SMartin Blumenstingl static const struct of_device_id meson_mx_sdhc_of_match[] = {
878e4bf1b09SMartin Blumenstingl 	{
879e4bf1b09SMartin Blumenstingl 		.compatible = "amlogic,meson8-sdhc",
880e4bf1b09SMartin Blumenstingl 		.data = &meson_mx_sdhc_data_meson8
881e4bf1b09SMartin Blumenstingl 	},
882e4bf1b09SMartin Blumenstingl 	{
883e4bf1b09SMartin Blumenstingl 		.compatible = "amlogic,meson8b-sdhc",
884e4bf1b09SMartin Blumenstingl 		.data = &meson_mx_sdhc_data_meson8
885e4bf1b09SMartin Blumenstingl 	},
886e4bf1b09SMartin Blumenstingl 	{
887e4bf1b09SMartin Blumenstingl 		.compatible = "amlogic,meson8m2-sdhc",
888e4bf1b09SMartin Blumenstingl 		.data = &meson_mx_sdhc_data_meson8m2
889e4bf1b09SMartin Blumenstingl 	},
890e4bf1b09SMartin Blumenstingl 	{ /* sentinel */ }
891e4bf1b09SMartin Blumenstingl };
892e4bf1b09SMartin Blumenstingl MODULE_DEVICE_TABLE(of, meson_mx_sdhc_of_match);
893e4bf1b09SMartin Blumenstingl 
894e4bf1b09SMartin Blumenstingl static struct platform_driver meson_mx_sdhc_driver = {
895e4bf1b09SMartin Blumenstingl 	.probe   = meson_mx_sdhc_probe,
896078e548aSUwe Kleine-König 	.remove = meson_mx_sdhc_remove,
897e4bf1b09SMartin Blumenstingl 	.driver  = {
898e4bf1b09SMartin Blumenstingl 		.name = "meson-mx-sdhc",
89931ae4035SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
900e4bf1b09SMartin Blumenstingl 		.of_match_table = of_match_ptr(meson_mx_sdhc_of_match),
901e4bf1b09SMartin Blumenstingl 	},
902e4bf1b09SMartin Blumenstingl };
903e4bf1b09SMartin Blumenstingl 
904e4bf1b09SMartin Blumenstingl module_platform_driver(meson_mx_sdhc_driver);
905e4bf1b09SMartin Blumenstingl 
906e4bf1b09SMartin Blumenstingl MODULE_DESCRIPTION("Meson6, Meson8, Meson8b and Meson8m2 SDHC Host Driver");
907e4bf1b09SMartin Blumenstingl MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
908e4bf1b09SMartin Blumenstingl MODULE_LICENSE("GPL v2");
909