12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ed80a13bSCarlo Caione /*
3ed80a13bSCarlo Caione * meson-mx-sdio.c - Meson6, Meson8 and Meson8b SDIO/MMC Host Controller
4ed80a13bSCarlo Caione *
5ed80a13bSCarlo Caione * Copyright (C) 2015 Endless Mobile, Inc.
6ed80a13bSCarlo Caione * Author: Carlo Caione <carlo@endlessm.com>
7ed80a13bSCarlo Caione * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
8ed80a13bSCarlo Caione */
9ed80a13bSCarlo Caione
10ed80a13bSCarlo Caione #include <linux/bitfield.h>
11ed80a13bSCarlo Caione #include <linux/clk.h>
12ed80a13bSCarlo Caione #include <linux/clk-provider.h>
13ed80a13bSCarlo Caione #include <linux/delay.h>
14ed80a13bSCarlo Caione #include <linux/device.h>
15ed80a13bSCarlo Caione #include <linux/dma-mapping.h>
16ed80a13bSCarlo Caione #include <linux/module.h>
17ed80a13bSCarlo Caione #include <linux/interrupt.h>
1862e59c4eSStephen Boyd #include <linux/io.h>
19ed80a13bSCarlo Caione #include <linux/ioport.h>
20ed80a13bSCarlo Caione #include <linux/platform_device.h>
21ed80a13bSCarlo Caione #include <linux/of_platform.h>
22ed80a13bSCarlo Caione #include <linux/timer.h>
23ed80a13bSCarlo Caione #include <linux/types.h>
24ed80a13bSCarlo Caione
25ed80a13bSCarlo Caione #include <linux/mmc/host.h>
26ed80a13bSCarlo Caione #include <linux/mmc/mmc.h>
27ed80a13bSCarlo Caione #include <linux/mmc/sdio.h>
28ed80a13bSCarlo Caione #include <linux/mmc/slot-gpio.h>
29ed80a13bSCarlo Caione
30ed80a13bSCarlo Caione #define MESON_MX_SDIO_ARGU 0x00
31ed80a13bSCarlo Caione
32ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND 0x04
33ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_COMMAND_INDEX_MASK GENMASK(7, 0)
34ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK GENMASK(15, 8)
35ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7 BIT(16)
36ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_HAS_DATA BIT(17)
37ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_CRC7_FROM_8 BIT(18)
38ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_CHECK_DAT0_BUSY BIT(19)
39ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_DATA BIT(20)
40ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_USE_INT_WINDOW BIT(21)
41ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK GENMASK(31, 24)
42ed80a13bSCarlo Caione
43ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF 0x08
44ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_CLK_DIV_SHIFT 0
45ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_CLK_DIV_WIDTH 10
46ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_DISABLE_CRC BIT(10)
47ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_OUT_AT_POSITIVE_EDGE BIT(11)
48ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_ARGUMENT_BITS_MASK GENMASK(17, 12)
49ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_RESP_LATCH_AT_NEGATIVE_EDGE BIT(18)
50ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_DATA_LATCH_AT_NEGATIVE_EDGE BIT(19)
51ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_BUS_WIDTH BIT(20)
52ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_M_ENDIAN_MASK GENMASK(22, 21)
53ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_WRITE_NWR_MASK GENMASK(28, 23)
54ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_WRITE_CRC_OK_STATUS_MASK GENMASK(31, 29)
55ed80a13bSCarlo Caione
56ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS 0x0c
57ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_STATUS_STATE_MACHINE_MASK GENMASK(3, 0)
58ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_CMD_BUSY BIT(4)
59ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_RESP_CRC7_OK BIT(5)
60ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_DATA_READ_CRC16_OK BIT(6)
61ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_DATA_WRITE_CRC16_OK BIT(7)
62ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_IF_INT BIT(8)
63ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_CMD_INT BIT(9)
64ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_STATUS_INFO_MASK GENMASK(15, 12)
65ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_TIMING_OUT_INT BIT(16)
66ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_AMRISC_TIMING_OUT_INT_EN BIT(17)
67ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_ARC_TIMING_OUT_INT_EN BIT(18)
68ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_TIMING_OUT_COUNT_MASK GENMASK(31, 19)
69ed80a13bSCarlo Caione
70ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC 0x10
71ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_ARC_IF_INT_EN BIT(3)
72ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN BIT(4)
73ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_IF_CONFIG_MASK GENMASK(7, 6)
74ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_DATA_CLK BIT(8)
75ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_DATA_CMD BIT(9)
76665e985cSJoe Perches #define MESON_MX_SDIO_IRQC_FORCE_DATA_DAT_MASK GENMASK(13, 10)
77ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_SOFT_RESET BIT(15)
78ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_HALT BIT(30)
79ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_HALT_HOLE BIT(31)
80ed80a13bSCarlo Caione
81ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT 0x14
82ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_PORT_SEL_MASK GENMASK(1, 0)
83ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_MEMORY_STICK_ENABLE BIT(2)
84ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_MEMORY_STICK_SCLK_ALWAYS BIT(3)
85ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_STREAM_ENABLE BIT(4)
86ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_STREAM_8BITS_MODE BIT(5)
87ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_WR_RD_OUT_INDEX BIT(8)
88ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_DAT0_DAT1_SWAPPED BIT(10)
89ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_DAT1_DAT0_SWAPPED BIT(11)
90ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK GENMASK(15, 12)
91ed80a13bSCarlo Caione
92ed80a13bSCarlo Caione #define MESON_MX_SDIO_ADDR 0x18
93ed80a13bSCarlo Caione
94ed80a13bSCarlo Caione #define MESON_MX_SDIO_EXT 0x1c
95ed80a13bSCarlo Caione #define MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK GENMASK(29, 16)
96ed80a13bSCarlo Caione
97ed80a13bSCarlo Caione #define MESON_MX_SDIO_BOUNCE_REQ_SIZE (128 * 1024)
98ed80a13bSCarlo Caione #define MESON_MX_SDIO_RESPONSE_CRC16_BITS (16 - 1)
99ed80a13bSCarlo Caione #define MESON_MX_SDIO_MAX_SLOTS 3
100ed80a13bSCarlo Caione
101ed80a13bSCarlo Caione struct meson_mx_mmc_host {
102ed80a13bSCarlo Caione struct device *controller_dev;
103ed80a13bSCarlo Caione
104ed80a13bSCarlo Caione struct clk *parent_clk;
105ed80a13bSCarlo Caione struct clk *core_clk;
106ed80a13bSCarlo Caione struct clk_divider cfg_div;
107ed80a13bSCarlo Caione struct clk *cfg_div_clk;
108ed80a13bSCarlo Caione struct clk_fixed_factor fixed_factor;
109ed80a13bSCarlo Caione struct clk *fixed_factor_clk;
110ed80a13bSCarlo Caione
111ed80a13bSCarlo Caione void __iomem *base;
112ed80a13bSCarlo Caione int irq;
113ed80a13bSCarlo Caione spinlock_t irq_lock;
114ed80a13bSCarlo Caione
115ed80a13bSCarlo Caione struct timer_list cmd_timeout;
116ed80a13bSCarlo Caione
117ed80a13bSCarlo Caione unsigned int slot_id;
118ed80a13bSCarlo Caione struct mmc_host *mmc;
119ed80a13bSCarlo Caione
120ed80a13bSCarlo Caione struct mmc_request *mrq;
121ed80a13bSCarlo Caione struct mmc_command *cmd;
122ed80a13bSCarlo Caione int error;
123ed80a13bSCarlo Caione };
124ed80a13bSCarlo Caione
meson_mx_mmc_mask_bits(struct mmc_host * mmc,char reg,u32 mask,u32 val)125ed80a13bSCarlo Caione static void meson_mx_mmc_mask_bits(struct mmc_host *mmc, char reg, u32 mask,
126ed80a13bSCarlo Caione u32 val)
127ed80a13bSCarlo Caione {
128ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc);
129ed80a13bSCarlo Caione u32 regval;
130ed80a13bSCarlo Caione
131ed80a13bSCarlo Caione regval = readl(host->base + reg);
132ed80a13bSCarlo Caione regval &= ~mask;
133ed80a13bSCarlo Caione regval |= (val & mask);
134ed80a13bSCarlo Caione
135ed80a13bSCarlo Caione writel(regval, host->base + reg);
136ed80a13bSCarlo Caione }
137ed80a13bSCarlo Caione
meson_mx_mmc_soft_reset(struct meson_mx_mmc_host * host)138ed80a13bSCarlo Caione static void meson_mx_mmc_soft_reset(struct meson_mx_mmc_host *host)
139ed80a13bSCarlo Caione {
140ed80a13bSCarlo Caione writel(MESON_MX_SDIO_IRQC_SOFT_RESET, host->base + MESON_MX_SDIO_IRQC);
141ed80a13bSCarlo Caione udelay(2);
142ed80a13bSCarlo Caione }
143ed80a13bSCarlo Caione
meson_mx_mmc_get_next_cmd(struct mmc_command * cmd)144ed80a13bSCarlo Caione static struct mmc_command *meson_mx_mmc_get_next_cmd(struct mmc_command *cmd)
145ed80a13bSCarlo Caione {
146ed80a13bSCarlo Caione if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
147ed80a13bSCarlo Caione return cmd->mrq->cmd;
148ed80a13bSCarlo Caione else if (mmc_op_multi(cmd->opcode) &&
149ed80a13bSCarlo Caione (!cmd->mrq->sbc || cmd->error || cmd->data->error))
150ed80a13bSCarlo Caione return cmd->mrq->stop;
151ed80a13bSCarlo Caione else
152ed80a13bSCarlo Caione return NULL;
153ed80a13bSCarlo Caione }
154ed80a13bSCarlo Caione
meson_mx_mmc_start_cmd(struct mmc_host * mmc,struct mmc_command * cmd)155ed80a13bSCarlo Caione static void meson_mx_mmc_start_cmd(struct mmc_host *mmc,
156ed80a13bSCarlo Caione struct mmc_command *cmd)
157ed80a13bSCarlo Caione {
158ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc);
159ed80a13bSCarlo Caione unsigned int pack_size;
160ed80a13bSCarlo Caione unsigned long irqflags, timeout;
161ed80a13bSCarlo Caione u32 mult, send = 0, ext = 0;
162ed80a13bSCarlo Caione
163ed80a13bSCarlo Caione host->cmd = cmd;
164ed80a13bSCarlo Caione
165ed80a13bSCarlo Caione if (cmd->busy_timeout)
166ed80a13bSCarlo Caione timeout = msecs_to_jiffies(cmd->busy_timeout);
167ed80a13bSCarlo Caione else
168ed80a13bSCarlo Caione timeout = msecs_to_jiffies(1000);
169ed80a13bSCarlo Caione
170ed80a13bSCarlo Caione switch (mmc_resp_type(cmd)) {
171ed80a13bSCarlo Caione case MMC_RSP_R1:
172ed80a13bSCarlo Caione case MMC_RSP_R1B:
173ed80a13bSCarlo Caione case MMC_RSP_R3:
174ed80a13bSCarlo Caione /* 7 (CMD) + 32 (response) + 7 (CRC) -1 */
175ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK, 45);
176ed80a13bSCarlo Caione break;
177ed80a13bSCarlo Caione case MMC_RSP_R2:
178ed80a13bSCarlo Caione /* 7 (CMD) + 120 (response) + 7 (CRC) -1 */
179ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK, 133);
180ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_CRC7_FROM_8;
181ed80a13bSCarlo Caione break;
182ed80a13bSCarlo Caione default:
183ed80a13bSCarlo Caione break;
184ed80a13bSCarlo Caione }
185ed80a13bSCarlo Caione
186ed80a13bSCarlo Caione if (!(cmd->flags & MMC_RSP_CRC))
187ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7;
188ed80a13bSCarlo Caione
189ed80a13bSCarlo Caione if (cmd->flags & MMC_RSP_BUSY)
190ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_CHECK_DAT0_BUSY;
191ed80a13bSCarlo Caione
192ed80a13bSCarlo Caione if (cmd->data) {
193ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK,
194ed80a13bSCarlo Caione (cmd->data->blocks - 1));
195ed80a13bSCarlo Caione
196ed80a13bSCarlo Caione pack_size = cmd->data->blksz * BITS_PER_BYTE;
197ed80a13bSCarlo Caione if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
198ed80a13bSCarlo Caione pack_size += MESON_MX_SDIO_RESPONSE_CRC16_BITS * 4;
199ed80a13bSCarlo Caione else
200ed80a13bSCarlo Caione pack_size += MESON_MX_SDIO_RESPONSE_CRC16_BITS * 1;
201ed80a13bSCarlo Caione
202ed80a13bSCarlo Caione ext |= FIELD_PREP(MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK,
203ed80a13bSCarlo Caione pack_size);
204ed80a13bSCarlo Caione
205ed80a13bSCarlo Caione if (cmd->data->flags & MMC_DATA_WRITE)
206ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_DATA;
207ed80a13bSCarlo Caione else
208ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_HAS_DATA;
209ed80a13bSCarlo Caione
210ed80a13bSCarlo Caione cmd->data->bytes_xfered = 0;
211ed80a13bSCarlo Caione }
212ed80a13bSCarlo Caione
213ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_COMMAND_INDEX_MASK,
214ed80a13bSCarlo Caione (0x40 | cmd->opcode));
215ed80a13bSCarlo Caione
216ed80a13bSCarlo Caione spin_lock_irqsave(&host->irq_lock, irqflags);
217ed80a13bSCarlo Caione
218ed80a13bSCarlo Caione mult = readl(host->base + MESON_MX_SDIO_MULT);
219ed80a13bSCarlo Caione mult &= ~MESON_MX_SDIO_MULT_PORT_SEL_MASK;
220ed80a13bSCarlo Caione mult |= FIELD_PREP(MESON_MX_SDIO_MULT_PORT_SEL_MASK, host->slot_id);
221ed80a13bSCarlo Caione mult |= BIT(31);
222ed80a13bSCarlo Caione writel(mult, host->base + MESON_MX_SDIO_MULT);
223ed80a13bSCarlo Caione
224ed80a13bSCarlo Caione /* enable the CMD done interrupt */
225ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_IRQC,
226ed80a13bSCarlo Caione MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN,
227ed80a13bSCarlo Caione MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN);
228ed80a13bSCarlo Caione
229ed80a13bSCarlo Caione /* clear pending interrupts */
230ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_IRQS,
231ed80a13bSCarlo Caione MESON_MX_SDIO_IRQS_CMD_INT,
232ed80a13bSCarlo Caione MESON_MX_SDIO_IRQS_CMD_INT);
233ed80a13bSCarlo Caione
234ed80a13bSCarlo Caione writel(cmd->arg, host->base + MESON_MX_SDIO_ARGU);
235ed80a13bSCarlo Caione writel(ext, host->base + MESON_MX_SDIO_EXT);
236ed80a13bSCarlo Caione writel(send, host->base + MESON_MX_SDIO_SEND);
237ed80a13bSCarlo Caione
238ed80a13bSCarlo Caione spin_unlock_irqrestore(&host->irq_lock, irqflags);
239ed80a13bSCarlo Caione
240ed80a13bSCarlo Caione mod_timer(&host->cmd_timeout, jiffies + timeout);
241ed80a13bSCarlo Caione }
242ed80a13bSCarlo Caione
meson_mx_mmc_request_done(struct meson_mx_mmc_host * host)243ed80a13bSCarlo Caione static void meson_mx_mmc_request_done(struct meson_mx_mmc_host *host)
244ed80a13bSCarlo Caione {
245ed80a13bSCarlo Caione struct mmc_request *mrq;
246ed80a13bSCarlo Caione
247ed80a13bSCarlo Caione mrq = host->mrq;
248ed80a13bSCarlo Caione
24991995b90SMartin Blumenstingl if (host->cmd->error)
25091995b90SMartin Blumenstingl meson_mx_mmc_soft_reset(host);
25191995b90SMartin Blumenstingl
252ed80a13bSCarlo Caione host->mrq = NULL;
253ed80a13bSCarlo Caione host->cmd = NULL;
254ed80a13bSCarlo Caione
255ed80a13bSCarlo Caione mmc_request_done(host->mmc, mrq);
256ed80a13bSCarlo Caione }
257ed80a13bSCarlo Caione
meson_mx_mmc_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)258ed80a13bSCarlo Caione static void meson_mx_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
259ed80a13bSCarlo Caione {
260ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc);
261ed80a13bSCarlo Caione unsigned short vdd = ios->vdd;
262ed80a13bSCarlo Caione unsigned long clk_rate = ios->clock;
263ed80a13bSCarlo Caione
264ed80a13bSCarlo Caione switch (ios->bus_width) {
265ed80a13bSCarlo Caione case MMC_BUS_WIDTH_1:
266ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_CONF,
267ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH, 0);
268ed80a13bSCarlo Caione break;
269ed80a13bSCarlo Caione
270ed80a13bSCarlo Caione case MMC_BUS_WIDTH_4:
271ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_CONF,
272ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH,
273ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH);
274ed80a13bSCarlo Caione break;
275ed80a13bSCarlo Caione
276ed80a13bSCarlo Caione case MMC_BUS_WIDTH_8:
277ed80a13bSCarlo Caione default:
278ed80a13bSCarlo Caione dev_err(mmc_dev(mmc), "unsupported bus width: %d\n",
279ed80a13bSCarlo Caione ios->bus_width);
280ed80a13bSCarlo Caione host->error = -EINVAL;
281ed80a13bSCarlo Caione return;
282ed80a13bSCarlo Caione }
283ed80a13bSCarlo Caione
284ed80a13bSCarlo Caione host->error = clk_set_rate(host->cfg_div_clk, ios->clock);
285ed80a13bSCarlo Caione if (host->error) {
286ed80a13bSCarlo Caione dev_warn(mmc_dev(mmc),
287ed80a13bSCarlo Caione "failed to set MMC clock to %lu: %d\n",
288ed80a13bSCarlo Caione clk_rate, host->error);
289ed80a13bSCarlo Caione return;
290ed80a13bSCarlo Caione }
291ed80a13bSCarlo Caione
292ed80a13bSCarlo Caione mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
293ed80a13bSCarlo Caione
294ed80a13bSCarlo Caione switch (ios->power_mode) {
295ed80a13bSCarlo Caione case MMC_POWER_OFF:
296ed80a13bSCarlo Caione vdd = 0;
297df561f66SGustavo A. R. Silva fallthrough;
298ed80a13bSCarlo Caione case MMC_POWER_UP:
299ed80a13bSCarlo Caione if (!IS_ERR(mmc->supply.vmmc)) {
300ed80a13bSCarlo Caione host->error = mmc_regulator_set_ocr(mmc,
301ed80a13bSCarlo Caione mmc->supply.vmmc,
302ed80a13bSCarlo Caione vdd);
303ed80a13bSCarlo Caione if (host->error)
304ed80a13bSCarlo Caione return;
305ed80a13bSCarlo Caione }
306ed80a13bSCarlo Caione break;
307ed80a13bSCarlo Caione }
308ed80a13bSCarlo Caione }
309ed80a13bSCarlo Caione
meson_mx_mmc_map_dma(struct mmc_host * mmc,struct mmc_request * mrq)310ed80a13bSCarlo Caione static int meson_mx_mmc_map_dma(struct mmc_host *mmc, struct mmc_request *mrq)
311ed80a13bSCarlo Caione {
312ed80a13bSCarlo Caione struct mmc_data *data = mrq->data;
313ed80a13bSCarlo Caione int dma_len;
314ed80a13bSCarlo Caione struct scatterlist *sg;
315ed80a13bSCarlo Caione
316ed80a13bSCarlo Caione if (!data)
317ed80a13bSCarlo Caione return 0;
318ed80a13bSCarlo Caione
319ed80a13bSCarlo Caione sg = data->sg;
320ed80a13bSCarlo Caione if (sg->offset & 3 || sg->length & 3) {
321ed80a13bSCarlo Caione dev_err(mmc_dev(mmc),
322ed80a13bSCarlo Caione "unaligned scatterlist: offset %x length %d\n",
323ed80a13bSCarlo Caione sg->offset, sg->length);
324ed80a13bSCarlo Caione return -EINVAL;
325ed80a13bSCarlo Caione }
326ed80a13bSCarlo Caione
327ed80a13bSCarlo Caione dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
328ed80a13bSCarlo Caione mmc_get_dma_dir(data));
329ed80a13bSCarlo Caione if (dma_len <= 0) {
330ed80a13bSCarlo Caione dev_err(mmc_dev(mmc), "dma_map_sg failed\n");
331ed80a13bSCarlo Caione return -ENOMEM;
332ed80a13bSCarlo Caione }
333ed80a13bSCarlo Caione
334ed80a13bSCarlo Caione return 0;
335ed80a13bSCarlo Caione }
336ed80a13bSCarlo Caione
meson_mx_mmc_request(struct mmc_host * mmc,struct mmc_request * mrq)337ed80a13bSCarlo Caione static void meson_mx_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
338ed80a13bSCarlo Caione {
339ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc);
340ed80a13bSCarlo Caione struct mmc_command *cmd = mrq->cmd;
341ed80a13bSCarlo Caione
342ed80a13bSCarlo Caione if (!host->error)
343ed80a13bSCarlo Caione host->error = meson_mx_mmc_map_dma(mmc, mrq);
344ed80a13bSCarlo Caione
345ed80a13bSCarlo Caione if (host->error) {
346ed80a13bSCarlo Caione cmd->error = host->error;
347ed80a13bSCarlo Caione mmc_request_done(mmc, mrq);
348ed80a13bSCarlo Caione return;
349ed80a13bSCarlo Caione }
350ed80a13bSCarlo Caione
351ed80a13bSCarlo Caione host->mrq = mrq;
352ed80a13bSCarlo Caione
353ed80a13bSCarlo Caione if (mrq->data)
354ed80a13bSCarlo Caione writel(sg_dma_address(mrq->data->sg),
355ed80a13bSCarlo Caione host->base + MESON_MX_SDIO_ADDR);
356ed80a13bSCarlo Caione
357ed80a13bSCarlo Caione if (mrq->sbc)
358ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(mmc, mrq->sbc);
359ed80a13bSCarlo Caione else
360ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(mmc, mrq->cmd);
361ed80a13bSCarlo Caione }
362ed80a13bSCarlo Caione
meson_mx_mmc_read_response(struct mmc_host * mmc,struct mmc_command * cmd)363ed80a13bSCarlo Caione static void meson_mx_mmc_read_response(struct mmc_host *mmc,
364ed80a13bSCarlo Caione struct mmc_command *cmd)
365ed80a13bSCarlo Caione {
366ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc);
367ed80a13bSCarlo Caione u32 mult;
368ed80a13bSCarlo Caione int i, resp[4];
369ed80a13bSCarlo Caione
370ed80a13bSCarlo Caione mult = readl(host->base + MESON_MX_SDIO_MULT);
371ed80a13bSCarlo Caione mult |= MESON_MX_SDIO_MULT_WR_RD_OUT_INDEX;
372ed80a13bSCarlo Caione mult &= ~MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK;
373ed80a13bSCarlo Caione mult |= FIELD_PREP(MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK, 0);
374ed80a13bSCarlo Caione writel(mult, host->base + MESON_MX_SDIO_MULT);
375ed80a13bSCarlo Caione
376ed80a13bSCarlo Caione if (cmd->flags & MMC_RSP_136) {
377ed80a13bSCarlo Caione for (i = 0; i <= 3; i++)
378ed80a13bSCarlo Caione resp[3 - i] = readl(host->base + MESON_MX_SDIO_ARGU);
379ed80a13bSCarlo Caione cmd->resp[0] = (resp[0] << 8) | ((resp[1] >> 24) & 0xff);
380ed80a13bSCarlo Caione cmd->resp[1] = (resp[1] << 8) | ((resp[2] >> 24) & 0xff);
381ed80a13bSCarlo Caione cmd->resp[2] = (resp[2] << 8) | ((resp[3] >> 24) & 0xff);
382ed80a13bSCarlo Caione cmd->resp[3] = (resp[3] << 8);
383ed80a13bSCarlo Caione } else if (cmd->flags & MMC_RSP_PRESENT) {
384ed80a13bSCarlo Caione cmd->resp[0] = readl(host->base + MESON_MX_SDIO_ARGU);
385ed80a13bSCarlo Caione }
386ed80a13bSCarlo Caione }
387ed80a13bSCarlo Caione
meson_mx_mmc_process_cmd_irq(struct meson_mx_mmc_host * host,u32 irqs,u32 send)388ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_process_cmd_irq(struct meson_mx_mmc_host *host,
389ed80a13bSCarlo Caione u32 irqs, u32 send)
390ed80a13bSCarlo Caione {
391ed80a13bSCarlo Caione struct mmc_command *cmd = host->cmd;
392ed80a13bSCarlo Caione
393ed80a13bSCarlo Caione /*
394ed80a13bSCarlo Caione * NOTE: even though it shouldn't happen we sometimes get command
395ed80a13bSCarlo Caione * interrupts twice (at least this is what it looks like). Ideally
396ed80a13bSCarlo Caione * we find out why this happens and warn here as soon as it occurs.
397ed80a13bSCarlo Caione */
398ed80a13bSCarlo Caione if (!cmd)
399ed80a13bSCarlo Caione return IRQ_HANDLED;
400ed80a13bSCarlo Caione
401ed80a13bSCarlo Caione cmd->error = 0;
402ed80a13bSCarlo Caione meson_mx_mmc_read_response(host->mmc, cmd);
403ed80a13bSCarlo Caione
404ed80a13bSCarlo Caione if (cmd->data) {
405ed80a13bSCarlo Caione if (!((irqs & MESON_MX_SDIO_IRQS_DATA_READ_CRC16_OK) ||
406ed80a13bSCarlo Caione (irqs & MESON_MX_SDIO_IRQS_DATA_WRITE_CRC16_OK)))
407ed80a13bSCarlo Caione cmd->error = -EILSEQ;
408ed80a13bSCarlo Caione } else {
409ed80a13bSCarlo Caione if (!((irqs & MESON_MX_SDIO_IRQS_RESP_CRC7_OK) ||
410ed80a13bSCarlo Caione (send & MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7)))
411ed80a13bSCarlo Caione cmd->error = -EILSEQ;
412ed80a13bSCarlo Caione }
413ed80a13bSCarlo Caione
414ed80a13bSCarlo Caione return IRQ_WAKE_THREAD;
415ed80a13bSCarlo Caione }
416ed80a13bSCarlo Caione
meson_mx_mmc_irq(int irq,void * data)417ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_irq(int irq, void *data)
418ed80a13bSCarlo Caione {
419ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = (void *) data;
420ed80a13bSCarlo Caione u32 irqs, send;
421ed80a13bSCarlo Caione irqreturn_t ret;
422ed80a13bSCarlo Caione
423291a81c3STian Tao spin_lock(&host->irq_lock);
424ed80a13bSCarlo Caione
425ed80a13bSCarlo Caione irqs = readl(host->base + MESON_MX_SDIO_IRQS);
426ed80a13bSCarlo Caione send = readl(host->base + MESON_MX_SDIO_SEND);
427ed80a13bSCarlo Caione
428ed80a13bSCarlo Caione if (irqs & MESON_MX_SDIO_IRQS_CMD_INT)
429ed80a13bSCarlo Caione ret = meson_mx_mmc_process_cmd_irq(host, irqs, send);
430ed80a13bSCarlo Caione else
431ed80a13bSCarlo Caione ret = IRQ_HANDLED;
432ed80a13bSCarlo Caione
433ed80a13bSCarlo Caione /* finally ACK all pending interrupts */
434ed80a13bSCarlo Caione writel(irqs, host->base + MESON_MX_SDIO_IRQS);
435ed80a13bSCarlo Caione
436291a81c3STian Tao spin_unlock(&host->irq_lock);
437ed80a13bSCarlo Caione
438ed80a13bSCarlo Caione return ret;
439ed80a13bSCarlo Caione }
440ed80a13bSCarlo Caione
meson_mx_mmc_irq_thread(int irq,void * irq_data)441ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_irq_thread(int irq, void *irq_data)
442ed80a13bSCarlo Caione {
443ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = (void *) irq_data;
444ed80a13bSCarlo Caione struct mmc_command *cmd = host->cmd, *next_cmd;
445ed80a13bSCarlo Caione
446ed80a13bSCarlo Caione if (WARN_ON(!cmd))
447ed80a13bSCarlo Caione return IRQ_HANDLED;
448ed80a13bSCarlo Caione
449ed80a13bSCarlo Caione del_timer_sync(&host->cmd_timeout);
450ed80a13bSCarlo Caione
451ed80a13bSCarlo Caione if (cmd->data) {
452ed80a13bSCarlo Caione dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg,
453ed80a13bSCarlo Caione cmd->data->sg_len,
454ed80a13bSCarlo Caione mmc_get_dma_dir(cmd->data));
455ed80a13bSCarlo Caione
456ed80a13bSCarlo Caione cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks;
457ed80a13bSCarlo Caione }
458ed80a13bSCarlo Caione
459ed80a13bSCarlo Caione next_cmd = meson_mx_mmc_get_next_cmd(cmd);
460ed80a13bSCarlo Caione if (next_cmd)
461ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(host->mmc, next_cmd);
462ed80a13bSCarlo Caione else
463ed80a13bSCarlo Caione meson_mx_mmc_request_done(host);
464ed80a13bSCarlo Caione
465ed80a13bSCarlo Caione return IRQ_HANDLED;
466ed80a13bSCarlo Caione }
467ed80a13bSCarlo Caione
meson_mx_mmc_timeout(struct timer_list * t)4682ee4f620SKees Cook static void meson_mx_mmc_timeout(struct timer_list *t)
469ed80a13bSCarlo Caione {
4702ee4f620SKees Cook struct meson_mx_mmc_host *host = from_timer(host, t, cmd_timeout);
471ed80a13bSCarlo Caione unsigned long irqflags;
472ed80a13bSCarlo Caione u32 irqc;
473ed80a13bSCarlo Caione
474ed80a13bSCarlo Caione spin_lock_irqsave(&host->irq_lock, irqflags);
475ed80a13bSCarlo Caione
476ed80a13bSCarlo Caione /* disable the CMD interrupt */
477ed80a13bSCarlo Caione irqc = readl(host->base + MESON_MX_SDIO_IRQC);
478ed80a13bSCarlo Caione irqc &= ~MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN;
479ed80a13bSCarlo Caione writel(irqc, host->base + MESON_MX_SDIO_IRQC);
480ed80a13bSCarlo Caione
481ed80a13bSCarlo Caione spin_unlock_irqrestore(&host->irq_lock, irqflags);
482ed80a13bSCarlo Caione
483ed80a13bSCarlo Caione /*
484ed80a13bSCarlo Caione * skip the timeout handling if the interrupt handler already processed
485ed80a13bSCarlo Caione * the command.
486ed80a13bSCarlo Caione */
487ed80a13bSCarlo Caione if (!host->cmd)
488ed80a13bSCarlo Caione return;
489ed80a13bSCarlo Caione
490ed80a13bSCarlo Caione dev_dbg(mmc_dev(host->mmc),
491ed80a13bSCarlo Caione "Timeout on CMD%u (IRQS = 0x%08x, ARGU = 0x%08x)\n",
492ed80a13bSCarlo Caione host->cmd->opcode, readl(host->base + MESON_MX_SDIO_IRQS),
493ed80a13bSCarlo Caione readl(host->base + MESON_MX_SDIO_ARGU));
494ed80a13bSCarlo Caione
495ed80a13bSCarlo Caione host->cmd->error = -ETIMEDOUT;
496ed80a13bSCarlo Caione
497ed80a13bSCarlo Caione meson_mx_mmc_request_done(host);
498ed80a13bSCarlo Caione }
499ed80a13bSCarlo Caione
500ed80a13bSCarlo Caione static struct mmc_host_ops meson_mx_mmc_ops = {
501ed80a13bSCarlo Caione .request = meson_mx_mmc_request,
502ed80a13bSCarlo Caione .set_ios = meson_mx_mmc_set_ios,
503ed80a13bSCarlo Caione .get_cd = mmc_gpio_get_cd,
504ed80a13bSCarlo Caione .get_ro = mmc_gpio_get_ro,
505ed80a13bSCarlo Caione };
506ed80a13bSCarlo Caione
meson_mx_mmc_slot_pdev(struct device * parent)507ed80a13bSCarlo Caione static struct platform_device *meson_mx_mmc_slot_pdev(struct device *parent)
508ed80a13bSCarlo Caione {
509ed80a13bSCarlo Caione struct device_node *slot_node;
510c483a5ccSJohan Hovold struct platform_device *pdev;
511ed80a13bSCarlo Caione
512ed80a13bSCarlo Caione /*
513ed80a13bSCarlo Caione * TODO: the MMC core framework currently does not support
514ed80a13bSCarlo Caione * controllers with multiple slots properly. So we only register
515ed80a13bSCarlo Caione * the first slot for now
516ed80a13bSCarlo Caione */
517c483a5ccSJohan Hovold slot_node = of_get_compatible_child(parent->of_node, "mmc-slot");
518ed80a13bSCarlo Caione if (!slot_node) {
519ed80a13bSCarlo Caione dev_warn(parent, "no 'mmc-slot' sub-node found\n");
520ed80a13bSCarlo Caione return ERR_PTR(-ENOENT);
521ed80a13bSCarlo Caione }
522ed80a13bSCarlo Caione
523c483a5ccSJohan Hovold pdev = of_platform_device_create(slot_node, NULL, parent);
524c483a5ccSJohan Hovold of_node_put(slot_node);
525c483a5ccSJohan Hovold
526c483a5ccSJohan Hovold return pdev;
527ed80a13bSCarlo Caione }
528ed80a13bSCarlo Caione
meson_mx_mmc_add_host(struct meson_mx_mmc_host * host)529ed80a13bSCarlo Caione static int meson_mx_mmc_add_host(struct meson_mx_mmc_host *host)
530ed80a13bSCarlo Caione {
531ed80a13bSCarlo Caione struct mmc_host *mmc = host->mmc;
532ed80a13bSCarlo Caione struct device *slot_dev = mmc_dev(mmc);
533ed80a13bSCarlo Caione int ret;
534ed80a13bSCarlo Caione
535ed80a13bSCarlo Caione if (of_property_read_u32(slot_dev->of_node, "reg", &host->slot_id)) {
536ed80a13bSCarlo Caione dev_err(slot_dev, "missing 'reg' property\n");
537ed80a13bSCarlo Caione return -EINVAL;
538ed80a13bSCarlo Caione }
539ed80a13bSCarlo Caione
540ed80a13bSCarlo Caione if (host->slot_id >= MESON_MX_SDIO_MAX_SLOTS) {
541ed80a13bSCarlo Caione dev_err(slot_dev, "invalid 'reg' property value %d\n",
542ed80a13bSCarlo Caione host->slot_id);
543ed80a13bSCarlo Caione return -EINVAL;
544ed80a13bSCarlo Caione }
545ed80a13bSCarlo Caione
546ed80a13bSCarlo Caione /* Get regulators and the supported OCR mask */
547ed80a13bSCarlo Caione ret = mmc_regulator_get_supply(mmc);
548aa5754c7SWolfram Sang if (ret)
549ed80a13bSCarlo Caione return ret;
550ed80a13bSCarlo Caione
551ed80a13bSCarlo Caione mmc->max_req_size = MESON_MX_SDIO_BOUNCE_REQ_SIZE;
552ed80a13bSCarlo Caione mmc->max_seg_size = mmc->max_req_size;
553ed80a13bSCarlo Caione mmc->max_blk_count =
554ed80a13bSCarlo Caione FIELD_GET(MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK,
555ed80a13bSCarlo Caione 0xffffffff);
556ed80a13bSCarlo Caione mmc->max_blk_size = FIELD_GET(MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK,
557ed80a13bSCarlo Caione 0xffffffff);
558ed80a13bSCarlo Caione mmc->max_blk_size -= (4 * MESON_MX_SDIO_RESPONSE_CRC16_BITS);
559ed80a13bSCarlo Caione mmc->max_blk_size /= BITS_PER_BYTE;
560ed80a13bSCarlo Caione
561ed80a13bSCarlo Caione /* Get the min and max supported clock rates */
562ed80a13bSCarlo Caione mmc->f_min = clk_round_rate(host->cfg_div_clk, 1);
563ed80a13bSCarlo Caione mmc->f_max = clk_round_rate(host->cfg_div_clk,
564ed80a13bSCarlo Caione clk_get_rate(host->parent_clk));
565ed80a13bSCarlo Caione
5661be64c79SUlf Hansson mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY;
567ed80a13bSCarlo Caione mmc->ops = &meson_mx_mmc_ops;
568ed80a13bSCarlo Caione
569ed80a13bSCarlo Caione ret = mmc_of_parse(mmc);
570ed80a13bSCarlo Caione if (ret)
571ed80a13bSCarlo Caione return ret;
572ed80a13bSCarlo Caione
573ed80a13bSCarlo Caione ret = mmc_add_host(mmc);
574ed80a13bSCarlo Caione if (ret)
575ed80a13bSCarlo Caione return ret;
576ed80a13bSCarlo Caione
577ed80a13bSCarlo Caione return 0;
578ed80a13bSCarlo Caione }
579ed80a13bSCarlo Caione
meson_mx_mmc_register_clks(struct meson_mx_mmc_host * host)580ed80a13bSCarlo Caione static int meson_mx_mmc_register_clks(struct meson_mx_mmc_host *host)
581ed80a13bSCarlo Caione {
582ed80a13bSCarlo Caione struct clk_init_data init;
583ed80a13bSCarlo Caione const char *clk_div_parent, *clk_fixed_factor_parent;
584ed80a13bSCarlo Caione
585ed80a13bSCarlo Caione clk_fixed_factor_parent = __clk_get_name(host->parent_clk);
586ed80a13bSCarlo Caione init.name = devm_kasprintf(host->controller_dev, GFP_KERNEL,
587ed80a13bSCarlo Caione "%s#fixed_factor",
588ed80a13bSCarlo Caione dev_name(host->controller_dev));
589b0d06f1cSNicholas Mc Guire if (!init.name)
590b0d06f1cSNicholas Mc Guire return -ENOMEM;
591b0d06f1cSNicholas Mc Guire
592ed80a13bSCarlo Caione init.ops = &clk_fixed_factor_ops;
593ed80a13bSCarlo Caione init.flags = 0;
594ed80a13bSCarlo Caione init.parent_names = &clk_fixed_factor_parent;
595ed80a13bSCarlo Caione init.num_parents = 1;
596ed80a13bSCarlo Caione host->fixed_factor.div = 2;
597ed80a13bSCarlo Caione host->fixed_factor.mult = 1;
598ed80a13bSCarlo Caione host->fixed_factor.hw.init = &init;
599ed80a13bSCarlo Caione
600ed80a13bSCarlo Caione host->fixed_factor_clk = devm_clk_register(host->controller_dev,
601ed80a13bSCarlo Caione &host->fixed_factor.hw);
6022f129d39SDan Carpenter if (WARN_ON(IS_ERR(host->fixed_factor_clk)))
603ed80a13bSCarlo Caione return PTR_ERR(host->fixed_factor_clk);
604ed80a13bSCarlo Caione
605ed80a13bSCarlo Caione clk_div_parent = __clk_get_name(host->fixed_factor_clk);
606ed80a13bSCarlo Caione init.name = devm_kasprintf(host->controller_dev, GFP_KERNEL,
607ed80a13bSCarlo Caione "%s#div", dev_name(host->controller_dev));
608b0d06f1cSNicholas Mc Guire if (!init.name)
609b0d06f1cSNicholas Mc Guire return -ENOMEM;
610b0d06f1cSNicholas Mc Guire
611ed80a13bSCarlo Caione init.ops = &clk_divider_ops;
612ed80a13bSCarlo Caione init.flags = CLK_SET_RATE_PARENT;
613ed80a13bSCarlo Caione init.parent_names = &clk_div_parent;
614ed80a13bSCarlo Caione init.num_parents = 1;
615ed80a13bSCarlo Caione host->cfg_div.reg = host->base + MESON_MX_SDIO_CONF;
616ed80a13bSCarlo Caione host->cfg_div.shift = MESON_MX_SDIO_CONF_CMD_CLK_DIV_SHIFT;
617ed80a13bSCarlo Caione host->cfg_div.width = MESON_MX_SDIO_CONF_CMD_CLK_DIV_WIDTH;
618ed80a13bSCarlo Caione host->cfg_div.hw.init = &init;
619ed80a13bSCarlo Caione host->cfg_div.flags = CLK_DIVIDER_ALLOW_ZERO;
620ed80a13bSCarlo Caione
621ed80a13bSCarlo Caione host->cfg_div_clk = devm_clk_register(host->controller_dev,
622ed80a13bSCarlo Caione &host->cfg_div.hw);
6232f129d39SDan Carpenter if (WARN_ON(IS_ERR(host->cfg_div_clk)))
6247599b849SDan Carpenter return PTR_ERR(host->cfg_div_clk);
625ed80a13bSCarlo Caione
626ed80a13bSCarlo Caione return 0;
627ed80a13bSCarlo Caione }
628ed80a13bSCarlo Caione
meson_mx_mmc_probe(struct platform_device * pdev)629ed80a13bSCarlo Caione static int meson_mx_mmc_probe(struct platform_device *pdev)
630ed80a13bSCarlo Caione {
631ed80a13bSCarlo Caione struct platform_device *slot_pdev;
632ed80a13bSCarlo Caione struct mmc_host *mmc;
633ed80a13bSCarlo Caione struct meson_mx_mmc_host *host;
634ed80a13bSCarlo Caione int ret, irq;
635ed80a13bSCarlo Caione u32 conf;
636ed80a13bSCarlo Caione
637ed80a13bSCarlo Caione slot_pdev = meson_mx_mmc_slot_pdev(&pdev->dev);
638ed80a13bSCarlo Caione if (!slot_pdev)
639ed80a13bSCarlo Caione return -ENODEV;
640ed80a13bSCarlo Caione else if (IS_ERR(slot_pdev))
641ed80a13bSCarlo Caione return PTR_ERR(slot_pdev);
642ed80a13bSCarlo Caione
643ed80a13bSCarlo Caione mmc = mmc_alloc_host(sizeof(*host), &slot_pdev->dev);
644ed80a13bSCarlo Caione if (!mmc) {
645ed80a13bSCarlo Caione ret = -ENOMEM;
646ed80a13bSCarlo Caione goto error_unregister_slot_pdev;
647ed80a13bSCarlo Caione }
648ed80a13bSCarlo Caione
649ed80a13bSCarlo Caione host = mmc_priv(mmc);
650ed80a13bSCarlo Caione host->mmc = mmc;
651ed80a13bSCarlo Caione host->controller_dev = &pdev->dev;
652ed80a13bSCarlo Caione
653ed80a13bSCarlo Caione spin_lock_init(&host->irq_lock);
6542ee4f620SKees Cook timer_setup(&host->cmd_timeout, meson_mx_mmc_timeout, 0);
655ed80a13bSCarlo Caione
656ed80a13bSCarlo Caione platform_set_drvdata(pdev, host);
657ed80a13bSCarlo Caione
658e10e54a6SYangtao Li host->base = devm_platform_ioremap_resource(pdev, 0);
659ed80a13bSCarlo Caione if (IS_ERR(host->base)) {
660ed80a13bSCarlo Caione ret = PTR_ERR(host->base);
661ed80a13bSCarlo Caione goto error_free_mmc;
662ed80a13bSCarlo Caione }
663ed80a13bSCarlo Caione
664ed80a13bSCarlo Caione irq = platform_get_irq(pdev, 0);
6658fc9a77bSSergey Shtylyov if (irq < 0) {
6668fc9a77bSSergey Shtylyov ret = irq;
6678fc9a77bSSergey Shtylyov goto error_free_mmc;
6688fc9a77bSSergey Shtylyov }
6698fc9a77bSSergey Shtylyov
670ed80a13bSCarlo Caione ret = devm_request_threaded_irq(host->controller_dev, irq,
671ed80a13bSCarlo Caione meson_mx_mmc_irq,
672ed80a13bSCarlo Caione meson_mx_mmc_irq_thread, IRQF_ONESHOT,
673ed80a13bSCarlo Caione NULL, host);
674ed80a13bSCarlo Caione if (ret)
675ed80a13bSCarlo Caione goto error_free_mmc;
676ed80a13bSCarlo Caione
677ed80a13bSCarlo Caione host->core_clk = devm_clk_get(host->controller_dev, "core");
678ed80a13bSCarlo Caione if (IS_ERR(host->core_clk)) {
679ed80a13bSCarlo Caione ret = PTR_ERR(host->core_clk);
680ed80a13bSCarlo Caione goto error_free_mmc;
681ed80a13bSCarlo Caione }
682ed80a13bSCarlo Caione
683ed80a13bSCarlo Caione host->parent_clk = devm_clk_get(host->controller_dev, "clkin");
684ed80a13bSCarlo Caione if (IS_ERR(host->parent_clk)) {
685ed80a13bSCarlo Caione ret = PTR_ERR(host->parent_clk);
686ed80a13bSCarlo Caione goto error_free_mmc;
687ed80a13bSCarlo Caione }
688ed80a13bSCarlo Caione
689ed80a13bSCarlo Caione ret = meson_mx_mmc_register_clks(host);
690ed80a13bSCarlo Caione if (ret)
691ed80a13bSCarlo Caione goto error_free_mmc;
692ed80a13bSCarlo Caione
693ed80a13bSCarlo Caione ret = clk_prepare_enable(host->core_clk);
694ed80a13bSCarlo Caione if (ret) {
695ed80a13bSCarlo Caione dev_err(host->controller_dev, "Failed to enable core clock\n");
696ed80a13bSCarlo Caione goto error_free_mmc;
697ed80a13bSCarlo Caione }
698ed80a13bSCarlo Caione
699ed80a13bSCarlo Caione ret = clk_prepare_enable(host->cfg_div_clk);
700ed80a13bSCarlo Caione if (ret) {
701ed80a13bSCarlo Caione dev_err(host->controller_dev, "Failed to enable MMC clock\n");
702ed80a13bSCarlo Caione goto error_disable_core_clk;
703ed80a13bSCarlo Caione }
704ed80a13bSCarlo Caione
705ed80a13bSCarlo Caione conf = 0;
706ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_CMD_ARGUMENT_BITS_MASK, 39);
707ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_M_ENDIAN_MASK, 0x3);
708ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_WRITE_NWR_MASK, 0x2);
709ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_WRITE_CRC_OK_STATUS_MASK, 0x2);
710ed80a13bSCarlo Caione writel(conf, host->base + MESON_MX_SDIO_CONF);
711ed80a13bSCarlo Caione
712ed80a13bSCarlo Caione meson_mx_mmc_soft_reset(host);
713ed80a13bSCarlo Caione
714ed80a13bSCarlo Caione ret = meson_mx_mmc_add_host(host);
715ed80a13bSCarlo Caione if (ret)
716ed80a13bSCarlo Caione goto error_disable_clks;
717ed80a13bSCarlo Caione
718ed80a13bSCarlo Caione return 0;
719ed80a13bSCarlo Caione
720ed80a13bSCarlo Caione error_disable_clks:
721ed80a13bSCarlo Caione clk_disable_unprepare(host->cfg_div_clk);
722ed80a13bSCarlo Caione error_disable_core_clk:
723ed80a13bSCarlo Caione clk_disable_unprepare(host->core_clk);
724ed80a13bSCarlo Caione error_free_mmc:
725ed80a13bSCarlo Caione mmc_free_host(mmc);
726ed80a13bSCarlo Caione error_unregister_slot_pdev:
727ed80a13bSCarlo Caione of_platform_device_destroy(&slot_pdev->dev, NULL);
728ed80a13bSCarlo Caione return ret;
729ed80a13bSCarlo Caione }
730ed80a13bSCarlo Caione
meson_mx_mmc_remove(struct platform_device * pdev)731*20c57c3cSYangtao Li static void meson_mx_mmc_remove(struct platform_device *pdev)
732ed80a13bSCarlo Caione {
733ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = platform_get_drvdata(pdev);
734ed80a13bSCarlo Caione struct device *slot_dev = mmc_dev(host->mmc);
735ed80a13bSCarlo Caione
736ed80a13bSCarlo Caione del_timer_sync(&host->cmd_timeout);
737ed80a13bSCarlo Caione
738ed80a13bSCarlo Caione mmc_remove_host(host->mmc);
739ed80a13bSCarlo Caione
740ed80a13bSCarlo Caione of_platform_device_destroy(slot_dev, NULL);
741ed80a13bSCarlo Caione
742ed80a13bSCarlo Caione clk_disable_unprepare(host->cfg_div_clk);
743ed80a13bSCarlo Caione clk_disable_unprepare(host->core_clk);
744ed80a13bSCarlo Caione
745ed80a13bSCarlo Caione mmc_free_host(host->mmc);
746ed80a13bSCarlo Caione }
747ed80a13bSCarlo Caione
748ed80a13bSCarlo Caione static const struct of_device_id meson_mx_mmc_of_match[] = {
749ed80a13bSCarlo Caione { .compatible = "amlogic,meson8-sdio", },
750ed80a13bSCarlo Caione { .compatible = "amlogic,meson8b-sdio", },
751ed80a13bSCarlo Caione { /* sentinel */ }
752ed80a13bSCarlo Caione };
753ed80a13bSCarlo Caione MODULE_DEVICE_TABLE(of, meson_mx_mmc_of_match);
754ed80a13bSCarlo Caione
755ed80a13bSCarlo Caione static struct platform_driver meson_mx_mmc_driver = {
756ed80a13bSCarlo Caione .probe = meson_mx_mmc_probe,
757*20c57c3cSYangtao Li .remove_new = meson_mx_mmc_remove,
758ed80a13bSCarlo Caione .driver = {
759ed80a13bSCarlo Caione .name = "meson-mx-sdio",
760a1a48919SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
761ed80a13bSCarlo Caione .of_match_table = of_match_ptr(meson_mx_mmc_of_match),
762ed80a13bSCarlo Caione },
763ed80a13bSCarlo Caione };
764ed80a13bSCarlo Caione
765ed80a13bSCarlo Caione module_platform_driver(meson_mx_mmc_driver);
766ed80a13bSCarlo Caione
767ed80a13bSCarlo Caione MODULE_DESCRIPTION("Meson6, Meson8 and Meson8b SDIO/MMC Host Driver");
768ed80a13bSCarlo Caione MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>");
769ed80a13bSCarlo Caione MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
770ed80a13bSCarlo Caione MODULE_LICENSE("GPL v2");
771