i2c: aspeed: Fix initial values of master and slave state
[linux/fpc-iii.git] / drivers / mmc / host / meson-gx-mmc.c
blobc201c378537e4f1a8a601e8d857481a7e6af9f0c
1 /*
2 * Amlogic SD/eMMC driver for the GX/S905 family SoCs
4 * Copyright (c) 2016 BayLibre, SAS.
5 * Author: Kevin Hilman <khilman@baylibre.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * The full GNU General Public License is included in this distribution
19 * in the file called COPYING.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/ioport.h>
28 #include <linux/spinlock.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/mmc/host.h>
31 #include <linux/mmc/mmc.h>
32 #include <linux/mmc/sdio.h>
33 #include <linux/mmc/slot-gpio.h>
34 #include <linux/io.h>
35 #include <linux/clk.h>
36 #include <linux/clk-provider.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/reset.h>
39 #include <linux/interrupt.h>
40 #include <linux/bitfield.h>
41 #include <linux/pinctrl/consumer.h>
43 #define DRIVER_NAME "meson-gx-mmc"
45 #define SD_EMMC_CLOCK 0x0
46 #define CLK_DIV_MASK GENMASK(5, 0)
47 #define CLK_SRC_MASK GENMASK(7, 6)
48 #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
49 #define CLK_TX_PHASE_MASK GENMASK(11, 10)
50 #define CLK_RX_PHASE_MASK GENMASK(13, 12)
51 #define CLK_V2_TX_DELAY_MASK GENMASK(19, 16)
52 #define CLK_V2_RX_DELAY_MASK GENMASK(23, 20)
53 #define CLK_V2_ALWAYS_ON BIT(24)
55 #define CLK_V3_TX_DELAY_MASK GENMASK(21, 16)
56 #define CLK_V3_RX_DELAY_MASK GENMASK(27, 22)
57 #define CLK_V3_ALWAYS_ON BIT(28)
59 #define CLK_DELAY_STEP_PS 200
60 #define CLK_PHASE_STEP 30
61 #define CLK_PHASE_POINT_NUM (360 / CLK_PHASE_STEP)
63 #define CLK_TX_DELAY_MASK(h) (h->data->tx_delay_mask)
64 #define CLK_RX_DELAY_MASK(h) (h->data->rx_delay_mask)
65 #define CLK_ALWAYS_ON(h) (h->data->always_on)
67 #define SD_EMMC_DELAY 0x4
68 #define SD_EMMC_ADJUST 0x8
70 #define SD_EMMC_DELAY1 0x4
71 #define SD_EMMC_DELAY2 0x8
72 #define SD_EMMC_V3_ADJUST 0xc
74 #define SD_EMMC_CALOUT 0x10
75 #define SD_EMMC_START 0x40
76 #define START_DESC_INIT BIT(0)
77 #define START_DESC_BUSY BIT(1)
78 #define START_DESC_ADDR_MASK GENMASK(31, 2)
80 #define SD_EMMC_CFG 0x44
81 #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
82 #define CFG_BUS_WIDTH_1 0x0
83 #define CFG_BUS_WIDTH_4 0x1
84 #define CFG_BUS_WIDTH_8 0x2
85 #define CFG_DDR BIT(2)
86 #define CFG_BLK_LEN_MASK GENMASK(7, 4)
87 #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
88 #define CFG_RC_CC_MASK GENMASK(15, 12)
89 #define CFG_STOP_CLOCK BIT(22)
90 #define CFG_CLK_ALWAYS_ON BIT(18)
91 #define CFG_CHK_DS BIT(20)
92 #define CFG_AUTO_CLK BIT(23)
94 #define SD_EMMC_STATUS 0x48
95 #define STATUS_BUSY BIT(31)
96 #define STATUS_DATI GENMASK(23, 16)
98 #define SD_EMMC_IRQ_EN 0x4c
99 #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
100 #define IRQ_TXD_ERR BIT(8)
101 #define IRQ_DESC_ERR BIT(9)
102 #define IRQ_RESP_ERR BIT(10)
103 #define IRQ_CRC_ERR \
104 (IRQ_RXD_ERR_MASK | IRQ_TXD_ERR | IRQ_DESC_ERR | IRQ_RESP_ERR)
105 #define IRQ_RESP_TIMEOUT BIT(11)
106 #define IRQ_DESC_TIMEOUT BIT(12)
107 #define IRQ_TIMEOUTS \
108 (IRQ_RESP_TIMEOUT | IRQ_DESC_TIMEOUT)
109 #define IRQ_END_OF_CHAIN BIT(13)
110 #define IRQ_RESP_STATUS BIT(14)
111 #define IRQ_SDIO BIT(15)
112 #define IRQ_EN_MASK \
113 (IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN | IRQ_RESP_STATUS |\
114 IRQ_SDIO)
116 #define SD_EMMC_CMD_CFG 0x50
117 #define SD_EMMC_CMD_ARG 0x54
118 #define SD_EMMC_CMD_DAT 0x58
119 #define SD_EMMC_CMD_RSP 0x5c
120 #define SD_EMMC_CMD_RSP1 0x60
121 #define SD_EMMC_CMD_RSP2 0x64
122 #define SD_EMMC_CMD_RSP3 0x68
124 #define SD_EMMC_RXD 0x94
125 #define SD_EMMC_TXD 0x94
126 #define SD_EMMC_LAST_REG SD_EMMC_TXD
128 #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
129 #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
130 #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
131 #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
132 #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
133 #define SD_EMMC_DESC_BUF_LEN PAGE_SIZE
135 #define SD_EMMC_PRE_REQ_DONE BIT(0)
136 #define SD_EMMC_DESC_CHAIN_MODE BIT(1)
138 #define MUX_CLK_NUM_PARENTS 2
140 struct meson_mmc_data {
141 unsigned int tx_delay_mask;
142 unsigned int rx_delay_mask;
143 unsigned int always_on;
146 struct sd_emmc_desc {
147 u32 cmd_cfg;
148 u32 cmd_arg;
149 u32 cmd_data;
150 u32 cmd_resp;
153 struct meson_host {
154 struct device *dev;
155 struct meson_mmc_data *data;
156 struct mmc_host *mmc;
157 struct mmc_command *cmd;
159 spinlock_t lock;
160 void __iomem *regs;
161 struct clk *core_clk;
162 struct clk *mmc_clk;
163 struct clk *rx_clk;
164 struct clk *tx_clk;
165 unsigned long req_rate;
167 struct pinctrl *pinctrl;
168 struct pinctrl_state *pins_default;
169 struct pinctrl_state *pins_clk_gate;
171 unsigned int bounce_buf_size;
172 void *bounce_buf;
173 dma_addr_t bounce_dma_addr;
174 struct sd_emmc_desc *descs;
175 dma_addr_t descs_dma_addr;
177 bool vqmmc_enabled;
180 #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
181 #define CMD_CFG_BLOCK_MODE BIT(9)
182 #define CMD_CFG_R1B BIT(10)
183 #define CMD_CFG_END_OF_CHAIN BIT(11)
184 #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
185 #define CMD_CFG_NO_RESP BIT(16)
186 #define CMD_CFG_NO_CMD BIT(17)
187 #define CMD_CFG_DATA_IO BIT(18)
188 #define CMD_CFG_DATA_WR BIT(19)
189 #define CMD_CFG_RESP_NOCRC BIT(20)
190 #define CMD_CFG_RESP_128 BIT(21)
191 #define CMD_CFG_RESP_NUM BIT(22)
192 #define CMD_CFG_DATA_NUM BIT(23)
193 #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
194 #define CMD_CFG_ERROR BIT(30)
195 #define CMD_CFG_OWNER BIT(31)
197 #define CMD_DATA_MASK GENMASK(31, 2)
198 #define CMD_DATA_BIG_ENDIAN BIT(1)
199 #define CMD_DATA_SRAM BIT(0)
200 #define CMD_RESP_MASK GENMASK(31, 1)
201 #define CMD_RESP_SRAM BIT(0)
203 struct meson_mmc_phase {
204 struct clk_hw hw;
205 void __iomem *reg;
206 unsigned long phase_mask;
207 unsigned long delay_mask;
208 unsigned int delay_step_ps;
211 #define to_meson_mmc_phase(_hw) container_of(_hw, struct meson_mmc_phase, hw)
213 static int meson_mmc_clk_get_phase(struct clk_hw *hw)
215 struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw);
216 unsigned int phase_num = 1 << hweight_long(mmc->phase_mask);
217 unsigned long period_ps, p, d;
218 int degrees;
219 u32 val;
221 val = readl(mmc->reg);
222 p = (val & mmc->phase_mask) >> __ffs(mmc->phase_mask);
223 degrees = p * 360 / phase_num;
225 if (mmc->delay_mask) {
226 period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000,
227 clk_get_rate(hw->clk));
228 d = (val & mmc->delay_mask) >> __ffs(mmc->delay_mask);
229 degrees += d * mmc->delay_step_ps * 360 / period_ps;
230 degrees %= 360;
233 return degrees;
236 static void meson_mmc_apply_phase_delay(struct meson_mmc_phase *mmc,
237 unsigned int phase,
238 unsigned int delay)
240 u32 val;
242 val = readl(mmc->reg);
243 val &= ~mmc->phase_mask;
244 val |= phase << __ffs(mmc->phase_mask);
246 if (mmc->delay_mask) {
247 val &= ~mmc->delay_mask;
248 val |= delay << __ffs(mmc->delay_mask);
251 writel(val, mmc->reg);
254 static int meson_mmc_clk_set_phase(struct clk_hw *hw, int degrees)
256 struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw);
257 unsigned int phase_num = 1 << hweight_long(mmc->phase_mask);
258 unsigned long period_ps, d = 0, r;
259 uint64_t p;
261 p = degrees % 360;
263 if (!mmc->delay_mask) {
264 p = DIV_ROUND_CLOSEST_ULL(p, 360 / phase_num);
265 } else {
266 period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000,
267 clk_get_rate(hw->clk));
269 /* First compute the phase index (p), the remainder (r) is the
270 * part we'll try to acheive using the delays (d).
272 r = do_div(p, 360 / phase_num);
273 d = DIV_ROUND_CLOSEST(r * period_ps,
274 360 * mmc->delay_step_ps);
275 d = min(d, mmc->delay_mask >> __ffs(mmc->delay_mask));
278 meson_mmc_apply_phase_delay(mmc, p, d);
279 return 0;
282 static const struct clk_ops meson_mmc_clk_phase_ops = {
283 .get_phase = meson_mmc_clk_get_phase,
284 .set_phase = meson_mmc_clk_set_phase,
287 static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data)
289 unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC;
291 if (!timeout)
292 return SD_EMMC_CMD_TIMEOUT_DATA;
294 timeout = roundup_pow_of_two(timeout);
296 return min(timeout, 32768U); /* max. 2^15 ms */
299 static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd)
301 if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
302 return cmd->mrq->cmd;
303 else if (mmc_op_multi(cmd->opcode) &&
304 (!cmd->mrq->sbc || cmd->error || cmd->data->error))
305 return cmd->mrq->stop;
306 else
307 return NULL;
310 static void meson_mmc_get_transfer_mode(struct mmc_host *mmc,
311 struct mmc_request *mrq)
313 struct mmc_data *data = mrq->data;
314 struct scatterlist *sg;
315 int i;
316 bool use_desc_chain_mode = true;
319 * Broken SDIO with AP6255-based WiFi on Khadas VIM Pro has been
320 * reported. For some strange reason this occurs in descriptor
321 * chain mode only. So let's fall back to bounce buffer mode
322 * for command SD_IO_RW_EXTENDED.
324 if (mrq->cmd->opcode == SD_IO_RW_EXTENDED)
325 return;
327 for_each_sg(data->sg, sg, data->sg_len, i)
328 /* check for 8 byte alignment */
329 if (sg->offset & 7) {
330 WARN_ONCE(1, "unaligned scatterlist buffer\n");
331 use_desc_chain_mode = false;
332 break;
335 if (use_desc_chain_mode)
336 data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE;
339 static inline bool meson_mmc_desc_chain_mode(const struct mmc_data *data)
341 return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE;
344 static inline bool meson_mmc_bounce_buf_read(const struct mmc_data *data)
346 return data && data->flags & MMC_DATA_READ &&
347 !meson_mmc_desc_chain_mode(data);
350 static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
352 struct mmc_data *data = mrq->data;
354 if (!data)
355 return;
357 meson_mmc_get_transfer_mode(mmc, mrq);
358 data->host_cookie |= SD_EMMC_PRE_REQ_DONE;
360 if (!meson_mmc_desc_chain_mode(data))
361 return;
363 data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
364 mmc_get_dma_dir(data));
365 if (!data->sg_count)
366 dev_err(mmc_dev(mmc), "dma_map_sg failed");
369 static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
370 int err)
372 struct mmc_data *data = mrq->data;
374 if (data && meson_mmc_desc_chain_mode(data) && data->sg_count)
375 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
376 mmc_get_dma_dir(data));
379 static bool meson_mmc_timing_is_ddr(struct mmc_ios *ios)
381 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
382 ios->timing == MMC_TIMING_UHS_DDR50 ||
383 ios->timing == MMC_TIMING_MMC_HS400)
384 return true;
386 return false;
390 * Gating the clock on this controller is tricky. It seems the mmc clock
391 * is also used by the controller. It may crash during some operation if the
392 * clock is stopped. The safest thing to do, whenever possible, is to keep
393 * clock running at stop it at the pad using the pinmux.
395 static void meson_mmc_clk_gate(struct meson_host *host)
397 u32 cfg;
399 if (host->pins_clk_gate) {
400 pinctrl_select_state(host->pinctrl, host->pins_clk_gate);
401 } else {
403 * If the pinmux is not provided - default to the classic and
404 * unsafe method
406 cfg = readl(host->regs + SD_EMMC_CFG);
407 cfg |= CFG_STOP_CLOCK;
408 writel(cfg, host->regs + SD_EMMC_CFG);
412 static void meson_mmc_clk_ungate(struct meson_host *host)
414 u32 cfg;
416 if (host->pins_clk_gate)
417 pinctrl_select_state(host->pinctrl, host->pins_default);
419 /* Make sure the clock is not stopped in the controller */
420 cfg = readl(host->regs + SD_EMMC_CFG);
421 cfg &= ~CFG_STOP_CLOCK;
422 writel(cfg, host->regs + SD_EMMC_CFG);
425 static int meson_mmc_clk_set(struct meson_host *host, struct mmc_ios *ios)
427 struct mmc_host *mmc = host->mmc;
428 unsigned long rate = ios->clock;
429 int ret;
430 u32 cfg;
432 /* DDR modes require higher module clock */
433 if (meson_mmc_timing_is_ddr(ios))
434 rate <<= 1;
436 /* Same request - bail-out */
437 if (host->req_rate == rate)
438 return 0;
440 /* stop clock */
441 meson_mmc_clk_gate(host);
442 host->req_rate = 0;
444 if (!rate) {
445 mmc->actual_clock = 0;
446 /* return with clock being stopped */
447 return 0;
450 /* Stop the clock during rate change to avoid glitches */
451 cfg = readl(host->regs + SD_EMMC_CFG);
452 cfg |= CFG_STOP_CLOCK;
453 writel(cfg, host->regs + SD_EMMC_CFG);
455 ret = clk_set_rate(host->mmc_clk, rate);
456 if (ret) {
457 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
458 rate, ret);
459 return ret;
462 host->req_rate = rate;
463 mmc->actual_clock = clk_get_rate(host->mmc_clk);
465 /* We should report the real output frequency of the controller */
466 if (meson_mmc_timing_is_ddr(ios))
467 mmc->actual_clock >>= 1;
469 dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock);
470 if (ios->clock != mmc->actual_clock)
471 dev_dbg(host->dev, "requested rate was %u\n", ios->clock);
473 /* (re)start clock */
474 meson_mmc_clk_ungate(host);
476 return 0;
480 * The SD/eMMC IP block has an internal mux and divider used for
481 * generating the MMC clock. Use the clock framework to create and
482 * manage these clocks.
484 static int meson_mmc_clk_init(struct meson_host *host)
486 struct clk_init_data init;
487 struct clk_mux *mux;
488 struct clk_divider *div;
489 struct meson_mmc_phase *core, *tx, *rx;
490 struct clk *clk;
491 char clk_name[32];
492 int i, ret = 0;
493 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
494 const char *clk_parent[1];
495 u32 clk_reg;
497 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
498 clk_reg = 0;
499 clk_reg |= CLK_ALWAYS_ON(host);
500 clk_reg |= CLK_DIV_MASK;
501 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
503 /* get the mux parents */
504 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
505 struct clk *clk;
506 char name[16];
508 snprintf(name, sizeof(name), "clkin%d", i);
509 clk = devm_clk_get(host->dev, name);
510 if (IS_ERR(clk)) {
511 if (clk != ERR_PTR(-EPROBE_DEFER))
512 dev_err(host->dev, "Missing clock %s\n", name);
513 return PTR_ERR(clk);
516 mux_parent_names[i] = __clk_get_name(clk);
519 /* create the mux */
520 mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL);
521 if (!mux)
522 return -ENOMEM;
524 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
525 init.name = clk_name;
526 init.ops = &clk_mux_ops;
527 init.flags = 0;
528 init.parent_names = mux_parent_names;
529 init.num_parents = MUX_CLK_NUM_PARENTS;
531 mux->reg = host->regs + SD_EMMC_CLOCK;
532 mux->shift = __ffs(CLK_SRC_MASK);
533 mux->mask = CLK_SRC_MASK >> mux->shift;
534 mux->hw.init = &init;
536 clk = devm_clk_register(host->dev, &mux->hw);
537 if (WARN_ON(IS_ERR(clk)))
538 return PTR_ERR(clk);
540 /* create the divider */
541 div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL);
542 if (!div)
543 return -ENOMEM;
545 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
546 init.name = clk_name;
547 init.ops = &clk_divider_ops;
548 init.flags = CLK_SET_RATE_PARENT;
549 clk_parent[0] = __clk_get_name(clk);
550 init.parent_names = clk_parent;
551 init.num_parents = 1;
553 div->reg = host->regs + SD_EMMC_CLOCK;
554 div->shift = __ffs(CLK_DIV_MASK);
555 div->width = __builtin_popcountl(CLK_DIV_MASK);
556 div->hw.init = &init;
557 div->flags = CLK_DIVIDER_ONE_BASED;
559 clk = devm_clk_register(host->dev, &div->hw);
560 if (WARN_ON(IS_ERR(clk)))
561 return PTR_ERR(clk);
563 /* create the mmc core clock */
564 core = devm_kzalloc(host->dev, sizeof(*core), GFP_KERNEL);
565 if (!core)
566 return -ENOMEM;
568 snprintf(clk_name, sizeof(clk_name), "%s#core", dev_name(host->dev));
569 init.name = clk_name;
570 init.ops = &meson_mmc_clk_phase_ops;
571 init.flags = CLK_SET_RATE_PARENT;
572 clk_parent[0] = __clk_get_name(clk);
573 init.parent_names = clk_parent;
574 init.num_parents = 1;
576 core->reg = host->regs + SD_EMMC_CLOCK;
577 core->phase_mask = CLK_CORE_PHASE_MASK;
578 core->hw.init = &init;
580 host->mmc_clk = devm_clk_register(host->dev, &core->hw);
581 if (WARN_ON(PTR_ERR_OR_ZERO(host->mmc_clk)))
582 return PTR_ERR(host->mmc_clk);
584 /* create the mmc tx clock */
585 tx = devm_kzalloc(host->dev, sizeof(*tx), GFP_KERNEL);
586 if (!tx)
587 return -ENOMEM;
589 snprintf(clk_name, sizeof(clk_name), "%s#tx", dev_name(host->dev));
590 init.name = clk_name;
591 init.ops = &meson_mmc_clk_phase_ops;
592 init.flags = 0;
593 clk_parent[0] = __clk_get_name(host->mmc_clk);
594 init.parent_names = clk_parent;
595 init.num_parents = 1;
597 tx->reg = host->regs + SD_EMMC_CLOCK;
598 tx->phase_mask = CLK_TX_PHASE_MASK;
599 tx->delay_mask = CLK_TX_DELAY_MASK(host);
600 tx->delay_step_ps = CLK_DELAY_STEP_PS;
601 tx->hw.init = &init;
603 host->tx_clk = devm_clk_register(host->dev, &tx->hw);
604 if (WARN_ON(PTR_ERR_OR_ZERO(host->tx_clk)))
605 return PTR_ERR(host->tx_clk);
607 /* create the mmc rx clock */
608 rx = devm_kzalloc(host->dev, sizeof(*rx), GFP_KERNEL);
609 if (!rx)
610 return -ENOMEM;
612 snprintf(clk_name, sizeof(clk_name), "%s#rx", dev_name(host->dev));
613 init.name = clk_name;
614 init.ops = &meson_mmc_clk_phase_ops;
615 init.flags = 0;
616 clk_parent[0] = __clk_get_name(host->mmc_clk);
617 init.parent_names = clk_parent;
618 init.num_parents = 1;
620 rx->reg = host->regs + SD_EMMC_CLOCK;
621 rx->phase_mask = CLK_RX_PHASE_MASK;
622 rx->delay_mask = CLK_RX_DELAY_MASK(host);
623 rx->delay_step_ps = CLK_DELAY_STEP_PS;
624 rx->hw.init = &init;
626 host->rx_clk = devm_clk_register(host->dev, &rx->hw);
627 if (WARN_ON(PTR_ERR_OR_ZERO(host->rx_clk)))
628 return PTR_ERR(host->rx_clk);
630 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
631 host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000);
632 ret = clk_set_rate(host->mmc_clk, host->mmc->f_min);
633 if (ret)
634 return ret;
637 * Set phases : These values are mostly the datasheet recommended ones
638 * except for the Tx phase. Datasheet recommends 180 but some cards
639 * fail at initialisation with it. 270 works just fine, it fixes these
640 * initialisation issues and enable eMMC DDR52 mode.
642 clk_set_phase(host->mmc_clk, 180);
643 clk_set_phase(host->tx_clk, 270);
644 clk_set_phase(host->rx_clk, 0);
646 return clk_prepare_enable(host->mmc_clk);
649 static void meson_mmc_shift_map(unsigned long *map, unsigned long shift)
651 DECLARE_BITMAP(left, CLK_PHASE_POINT_NUM);
652 DECLARE_BITMAP(right, CLK_PHASE_POINT_NUM);
655 * shift the bitmap right and reintroduce the dropped bits on the left
656 * of the bitmap
658 bitmap_shift_right(right, map, shift, CLK_PHASE_POINT_NUM);
659 bitmap_shift_left(left, map, CLK_PHASE_POINT_NUM - shift,
660 CLK_PHASE_POINT_NUM);
661 bitmap_or(map, left, right, CLK_PHASE_POINT_NUM);
664 static void meson_mmc_find_next_region(unsigned long *map,
665 unsigned long *start,
666 unsigned long *stop)
668 *start = find_next_bit(map, CLK_PHASE_POINT_NUM, *start);
669 *stop = find_next_zero_bit(map, CLK_PHASE_POINT_NUM, *start);
672 static int meson_mmc_find_tuning_point(unsigned long *test)
674 unsigned long shift, stop, offset = 0, start = 0, size = 0;
676 /* Get the all good/all bad situation out the way */
677 if (bitmap_full(test, CLK_PHASE_POINT_NUM))
678 return 0; /* All points are good so point 0 will do */
679 else if (bitmap_empty(test, CLK_PHASE_POINT_NUM))
680 return -EIO; /* No successful tuning point */
683 * Now we know there is a least one region find. Make sure it does
684 * not wrap by the shifting the bitmap if necessary
686 shift = find_first_zero_bit(test, CLK_PHASE_POINT_NUM);
687 if (shift != 0)
688 meson_mmc_shift_map(test, shift);
690 while (start < CLK_PHASE_POINT_NUM) {
691 meson_mmc_find_next_region(test, &start, &stop);
693 if ((stop - start) > size) {
694 offset = start;
695 size = stop - start;
698 start = stop;
701 /* Get the center point of the region */
702 offset += (size / 2);
704 /* Shift the result back */
705 offset = (offset + shift) % CLK_PHASE_POINT_NUM;
707 return offset;
710 static int meson_mmc_clk_phase_tuning(struct mmc_host *mmc, u32 opcode,
711 struct clk *clk)
713 int point, ret;
714 DECLARE_BITMAP(test, CLK_PHASE_POINT_NUM);
716 dev_dbg(mmc_dev(mmc), "%s phase/delay tunning...\n",
717 __clk_get_name(clk));
718 bitmap_zero(test, CLK_PHASE_POINT_NUM);
720 /* Explore tuning points */
721 for (point = 0; point < CLK_PHASE_POINT_NUM; point++) {
722 clk_set_phase(clk, point * CLK_PHASE_STEP);
723 ret = mmc_send_tuning(mmc, opcode, NULL);
724 if (!ret)
725 set_bit(point, test);
728 /* Find the optimal tuning point and apply it */
729 point = meson_mmc_find_tuning_point(test);
730 if (point < 0)
731 return point; /* tuning failed */
733 clk_set_phase(clk, point * CLK_PHASE_STEP);
734 dev_dbg(mmc_dev(mmc), "success with phase: %d\n",
735 clk_get_phase(clk));
736 return 0;
739 static int meson_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
741 struct meson_host *host = mmc_priv(mmc);
743 return meson_mmc_clk_phase_tuning(mmc, opcode, host->rx_clk);
746 static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
748 struct meson_host *host = mmc_priv(mmc);
749 u32 bus_width, val;
750 int err;
753 * GPIO regulator, only controls switching between 1v8 and
754 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
756 switch (ios->power_mode) {
757 case MMC_POWER_OFF:
758 if (!IS_ERR(mmc->supply.vmmc))
759 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
761 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
762 regulator_disable(mmc->supply.vqmmc);
763 host->vqmmc_enabled = false;
766 break;
768 case MMC_POWER_UP:
769 if (!IS_ERR(mmc->supply.vmmc))
770 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
772 /* Reset rx phase */
773 clk_set_phase(host->rx_clk, 0);
775 break;
777 case MMC_POWER_ON:
778 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
779 int ret = regulator_enable(mmc->supply.vqmmc);
781 if (ret < 0)
782 dev_err(host->dev,
783 "failed to enable vqmmc regulator\n");
784 else
785 host->vqmmc_enabled = true;
788 break;
791 /* Bus width */
792 switch (ios->bus_width) {
793 case MMC_BUS_WIDTH_1:
794 bus_width = CFG_BUS_WIDTH_1;
795 break;
796 case MMC_BUS_WIDTH_4:
797 bus_width = CFG_BUS_WIDTH_4;
798 break;
799 case MMC_BUS_WIDTH_8:
800 bus_width = CFG_BUS_WIDTH_8;
801 break;
802 default:
803 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
804 ios->bus_width);
805 bus_width = CFG_BUS_WIDTH_4;
808 val = readl(host->regs + SD_EMMC_CFG);
809 val &= ~CFG_BUS_WIDTH_MASK;
810 val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
812 val &= ~CFG_DDR;
813 if (meson_mmc_timing_is_ddr(ios))
814 val |= CFG_DDR;
816 val &= ~CFG_CHK_DS;
817 if (ios->timing == MMC_TIMING_MMC_HS400)
818 val |= CFG_CHK_DS;
820 err = meson_mmc_clk_set(host, ios);
821 if (err)
822 dev_err(host->dev, "Failed to set clock: %d\n,", err);
824 writel(val, host->regs + SD_EMMC_CFG);
825 dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val);
828 static void meson_mmc_request_done(struct mmc_host *mmc,
829 struct mmc_request *mrq)
831 struct meson_host *host = mmc_priv(mmc);
833 host->cmd = NULL;
834 mmc_request_done(host->mmc, mrq);
837 static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz)
839 struct meson_host *host = mmc_priv(mmc);
840 u32 cfg, blksz_old;
842 cfg = readl(host->regs + SD_EMMC_CFG);
843 blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
845 if (!is_power_of_2(blksz))
846 dev_err(host->dev, "blksz %u is not a power of 2\n", blksz);
848 blksz = ilog2(blksz);
850 /* check if block-size matches, if not update */
851 if (blksz == blksz_old)
852 return;
854 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__,
855 blksz_old, blksz);
857 cfg &= ~CFG_BLK_LEN_MASK;
858 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz);
859 writel(cfg, host->regs + SD_EMMC_CFG);
862 static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg)
864 if (cmd->flags & MMC_RSP_PRESENT) {
865 if (cmd->flags & MMC_RSP_136)
866 *cmd_cfg |= CMD_CFG_RESP_128;
867 *cmd_cfg |= CMD_CFG_RESP_NUM;
869 if (!(cmd->flags & MMC_RSP_CRC))
870 *cmd_cfg |= CMD_CFG_RESP_NOCRC;
872 if (cmd->flags & MMC_RSP_BUSY)
873 *cmd_cfg |= CMD_CFG_R1B;
874 } else {
875 *cmd_cfg |= CMD_CFG_NO_RESP;
879 static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
881 struct meson_host *host = mmc_priv(mmc);
882 struct sd_emmc_desc *desc = host->descs;
883 struct mmc_data *data = host->cmd->data;
884 struct scatterlist *sg;
885 u32 start;
886 int i;
888 if (data->flags & MMC_DATA_WRITE)
889 cmd_cfg |= CMD_CFG_DATA_WR;
891 if (data->blocks > 1) {
892 cmd_cfg |= CMD_CFG_BLOCK_MODE;
893 meson_mmc_set_blksz(mmc, data->blksz);
896 for_each_sg(data->sg, sg, data->sg_count, i) {
897 unsigned int len = sg_dma_len(sg);
899 if (data->blocks > 1)
900 len /= data->blksz;
902 desc[i].cmd_cfg = cmd_cfg;
903 desc[i].cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, len);
904 if (i > 0)
905 desc[i].cmd_cfg |= CMD_CFG_NO_CMD;
906 desc[i].cmd_arg = host->cmd->arg;
907 desc[i].cmd_resp = 0;
908 desc[i].cmd_data = sg_dma_address(sg);
910 desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN;
912 dma_wmb(); /* ensure descriptor is written before kicked */
913 start = host->descs_dma_addr | START_DESC_BUSY;
914 writel(start, host->regs + SD_EMMC_START);
917 static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
919 struct meson_host *host = mmc_priv(mmc);
920 struct mmc_data *data = cmd->data;
921 u32 cmd_cfg = 0, cmd_data = 0;
922 unsigned int xfer_bytes = 0;
924 /* Setup descriptors */
925 dma_rmb();
927 host->cmd = cmd;
929 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
930 cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
932 meson_mmc_set_response_bits(cmd, &cmd_cfg);
934 /* data? */
935 if (data) {
936 data->bytes_xfered = 0;
937 cmd_cfg |= CMD_CFG_DATA_IO;
938 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
939 ilog2(meson_mmc_get_timeout_msecs(data)));
941 if (meson_mmc_desc_chain_mode(data)) {
942 meson_mmc_desc_chain_transfer(mmc, cmd_cfg);
943 return;
946 if (data->blocks > 1) {
947 cmd_cfg |= CMD_CFG_BLOCK_MODE;
948 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
949 data->blocks);
950 meson_mmc_set_blksz(mmc, data->blksz);
951 } else {
952 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
955 xfer_bytes = data->blksz * data->blocks;
956 if (data->flags & MMC_DATA_WRITE) {
957 cmd_cfg |= CMD_CFG_DATA_WR;
958 WARN_ON(xfer_bytes > host->bounce_buf_size);
959 sg_copy_to_buffer(data->sg, data->sg_len,
960 host->bounce_buf, xfer_bytes);
961 dma_wmb();
964 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
965 } else {
966 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
967 ilog2(SD_EMMC_CMD_TIMEOUT));
970 /* Last descriptor */
971 cmd_cfg |= CMD_CFG_END_OF_CHAIN;
972 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
973 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
974 writel(0, host->regs + SD_EMMC_CMD_RSP);
975 wmb(); /* ensure descriptor is written before kicked */
976 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
979 static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
981 struct meson_host *host = mmc_priv(mmc);
982 bool needs_pre_post_req = mrq->data &&
983 !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);
985 if (needs_pre_post_req) {
986 meson_mmc_get_transfer_mode(mmc, mrq);
987 if (!meson_mmc_desc_chain_mode(mrq->data))
988 needs_pre_post_req = false;
991 if (needs_pre_post_req)
992 meson_mmc_pre_req(mmc, mrq);
994 /* Stop execution */
995 writel(0, host->regs + SD_EMMC_START);
997 meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd);
999 if (needs_pre_post_req)
1000 meson_mmc_post_req(mmc, mrq, 0);
1003 static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
1005 struct meson_host *host = mmc_priv(mmc);
1007 if (cmd->flags & MMC_RSP_136) {
1008 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
1009 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
1010 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
1011 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
1012 } else if (cmd->flags & MMC_RSP_PRESENT) {
1013 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
1017 static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
1019 struct meson_host *host = dev_id;
1020 struct mmc_command *cmd;
1021 struct mmc_data *data;
1022 u32 irq_en, status, raw_status;
1023 irqreturn_t ret = IRQ_NONE;
1025 if (WARN_ON(!host) || WARN_ON(!host->cmd))
1026 return IRQ_NONE;
1028 spin_lock(&host->lock);
1030 cmd = host->cmd;
1031 data = cmd->data;
1032 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
1033 raw_status = readl(host->regs + SD_EMMC_STATUS);
1034 status = raw_status & irq_en;
1036 cmd->error = 0;
1037 if (status & IRQ_CRC_ERR) {
1038 dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status);
1039 cmd->error = -EILSEQ;
1040 ret = IRQ_HANDLED;
1041 goto out;
1044 if (status & IRQ_TIMEOUTS) {
1045 dev_dbg(host->dev, "Timeout - status 0x%08x\n", status);
1046 cmd->error = -ETIMEDOUT;
1047 ret = IRQ_HANDLED;
1048 goto out;
1051 meson_mmc_read_resp(host->mmc, cmd);
1053 if (status & IRQ_SDIO) {
1054 dev_dbg(host->dev, "IRQ: SDIO TODO.\n");
1055 ret = IRQ_HANDLED;
1058 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
1059 if (data && !cmd->error)
1060 data->bytes_xfered = data->blksz * data->blocks;
1061 if (meson_mmc_bounce_buf_read(data) ||
1062 meson_mmc_get_next_command(cmd))
1063 ret = IRQ_WAKE_THREAD;
1064 else
1065 ret = IRQ_HANDLED;
1068 out:
1069 /* ack all enabled interrupts */
1070 writel(irq_en, host->regs + SD_EMMC_STATUS);
1072 if (ret == IRQ_HANDLED)
1073 meson_mmc_request_done(host->mmc, cmd->mrq);
1074 else if (ret == IRQ_NONE)
1075 dev_warn(host->dev,
1076 "Unexpected IRQ! status=0x%08x, irq_en=0x%08x\n",
1077 raw_status, irq_en);
1079 spin_unlock(&host->lock);
1080 return ret;
1083 static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
1085 struct meson_host *host = dev_id;
1086 struct mmc_command *next_cmd, *cmd = host->cmd;
1087 struct mmc_data *data;
1088 unsigned int xfer_bytes;
1090 if (WARN_ON(!cmd))
1091 return IRQ_NONE;
1093 data = cmd->data;
1094 if (meson_mmc_bounce_buf_read(data)) {
1095 xfer_bytes = data->blksz * data->blocks;
1096 WARN_ON(xfer_bytes > host->bounce_buf_size);
1097 sg_copy_from_buffer(data->sg, data->sg_len,
1098 host->bounce_buf, xfer_bytes);
1101 next_cmd = meson_mmc_get_next_command(cmd);
1102 if (next_cmd)
1103 meson_mmc_start_cmd(host->mmc, next_cmd);
1104 else
1105 meson_mmc_request_done(host->mmc, cmd->mrq);
1107 return IRQ_HANDLED;
1111 * NOTE: we only need this until the GPIO/pinctrl driver can handle
1112 * interrupts. For now, the MMC core will use this for polling.
1114 static int meson_mmc_get_cd(struct mmc_host *mmc)
1116 int status = mmc_gpio_get_cd(mmc);
1118 if (status == -ENOSYS)
1119 return 1; /* assume present */
1121 return status;
1124 static void meson_mmc_cfg_init(struct meson_host *host)
1126 u32 cfg = 0;
1128 cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
1129 ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
1130 cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
1131 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
1133 writel(cfg, host->regs + SD_EMMC_CFG);
1136 static int meson_mmc_card_busy(struct mmc_host *mmc)
1138 struct meson_host *host = mmc_priv(mmc);
1139 u32 regval;
1141 regval = readl(host->regs + SD_EMMC_STATUS);
1143 /* We are only interrested in lines 0 to 3, so mask the other ones */
1144 return !(FIELD_GET(STATUS_DATI, regval) & 0xf);
1147 static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
1149 /* vqmmc regulator is available */
1150 if (!IS_ERR(mmc->supply.vqmmc)) {
1152 * The usual amlogic setup uses a GPIO to switch from one
1153 * regulator to the other. While the voltage ramp up is
1154 * pretty fast, care must be taken when switching from 3.3v
1155 * to 1.8v. Please make sure the regulator framework is aware
1156 * of your own regulator constraints
1158 return mmc_regulator_set_vqmmc(mmc, ios);
1161 /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
1162 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1163 return 0;
1165 return -EINVAL;
1168 static const struct mmc_host_ops meson_mmc_ops = {
1169 .request = meson_mmc_request,
1170 .set_ios = meson_mmc_set_ios,
1171 .get_cd = meson_mmc_get_cd,
1172 .pre_req = meson_mmc_pre_req,
1173 .post_req = meson_mmc_post_req,
1174 .execute_tuning = meson_mmc_execute_tuning,
1175 .card_busy = meson_mmc_card_busy,
1176 .start_signal_voltage_switch = meson_mmc_voltage_switch,
1179 static int meson_mmc_probe(struct platform_device *pdev)
1181 struct resource *res;
1182 struct meson_host *host;
1183 struct mmc_host *mmc;
1184 int ret, irq;
1186 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
1187 if (!mmc)
1188 return -ENOMEM;
1189 host = mmc_priv(mmc);
1190 host->mmc = mmc;
1191 host->dev = &pdev->dev;
1192 dev_set_drvdata(&pdev->dev, host);
1194 spin_lock_init(&host->lock);
1196 /* Get regulators and the supported OCR mask */
1197 host->vqmmc_enabled = false;
1198 ret = mmc_regulator_get_supply(mmc);
1199 if (ret)
1200 goto free_host;
1202 ret = mmc_of_parse(mmc);
1203 if (ret) {
1204 if (ret != -EPROBE_DEFER)
1205 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
1206 goto free_host;
1209 host->data = (struct meson_mmc_data *)
1210 of_device_get_match_data(&pdev->dev);
1211 if (!host->data) {
1212 ret = -EINVAL;
1213 goto free_host;
1216 ret = device_reset_optional(&pdev->dev);
1217 if (ret) {
1218 if (ret != -EPROBE_DEFER)
1219 dev_err(&pdev->dev, "device reset failed: %d\n", ret);
1221 return ret;
1224 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1225 host->regs = devm_ioremap_resource(&pdev->dev, res);
1226 if (IS_ERR(host->regs)) {
1227 ret = PTR_ERR(host->regs);
1228 goto free_host;
1231 irq = platform_get_irq(pdev, 0);
1232 if (irq <= 0) {
1233 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
1234 ret = -EINVAL;
1235 goto free_host;
1238 host->pinctrl = devm_pinctrl_get(&pdev->dev);
1239 if (IS_ERR(host->pinctrl)) {
1240 ret = PTR_ERR(host->pinctrl);
1241 goto free_host;
1244 host->pins_default = pinctrl_lookup_state(host->pinctrl,
1245 PINCTRL_STATE_DEFAULT);
1246 if (IS_ERR(host->pins_default)) {
1247 ret = PTR_ERR(host->pins_default);
1248 goto free_host;
1251 host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl,
1252 "clk-gate");
1253 if (IS_ERR(host->pins_clk_gate)) {
1254 dev_warn(&pdev->dev,
1255 "can't get clk-gate pinctrl, using clk_stop bit\n");
1256 host->pins_clk_gate = NULL;
1259 host->core_clk = devm_clk_get(&pdev->dev, "core");
1260 if (IS_ERR(host->core_clk)) {
1261 ret = PTR_ERR(host->core_clk);
1262 goto free_host;
1265 ret = clk_prepare_enable(host->core_clk);
1266 if (ret)
1267 goto free_host;
1269 ret = meson_mmc_clk_init(host);
1270 if (ret)
1271 goto err_core_clk;
1273 /* set config to sane default */
1274 meson_mmc_cfg_init(host);
1276 /* Stop execution */
1277 writel(0, host->regs + SD_EMMC_START);
1279 /* clear, ack and enable interrupts */
1280 writel(0, host->regs + SD_EMMC_IRQ_EN);
1281 writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN,
1282 host->regs + SD_EMMC_STATUS);
1283 writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN,
1284 host->regs + SD_EMMC_IRQ_EN);
1286 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
1287 meson_mmc_irq_thread, IRQF_SHARED,
1288 NULL, host);
1289 if (ret)
1290 goto err_init_clk;
1292 mmc->caps |= MMC_CAP_CMD23;
1293 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
1294 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
1295 mmc->max_segs = SD_EMMC_DESC_BUF_LEN / sizeof(struct sd_emmc_desc);
1296 mmc->max_seg_size = mmc->max_req_size;
1298 /* data bounce buffer */
1299 host->bounce_buf_size = mmc->max_req_size;
1300 host->bounce_buf =
1301 dma_alloc_coherent(host->dev, host->bounce_buf_size,
1302 &host->bounce_dma_addr, GFP_KERNEL);
1303 if (host->bounce_buf == NULL) {
1304 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
1305 ret = -ENOMEM;
1306 goto err_init_clk;
1309 host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
1310 &host->descs_dma_addr, GFP_KERNEL);
1311 if (!host->descs) {
1312 dev_err(host->dev, "Allocating descriptor DMA buffer failed\n");
1313 ret = -ENOMEM;
1314 goto err_bounce_buf;
1317 mmc->ops = &meson_mmc_ops;
1318 mmc_add_host(mmc);
1320 return 0;
1322 err_bounce_buf:
1323 dma_free_coherent(host->dev, host->bounce_buf_size,
1324 host->bounce_buf, host->bounce_dma_addr);
1325 err_init_clk:
1326 clk_disable_unprepare(host->mmc_clk);
1327 err_core_clk:
1328 clk_disable_unprepare(host->core_clk);
1329 free_host:
1330 mmc_free_host(mmc);
1331 return ret;
1334 static int meson_mmc_remove(struct platform_device *pdev)
1336 struct meson_host *host = dev_get_drvdata(&pdev->dev);
1338 mmc_remove_host(host->mmc);
1340 /* disable interrupts */
1341 writel(0, host->regs + SD_EMMC_IRQ_EN);
1343 dma_free_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
1344 host->descs, host->descs_dma_addr);
1345 dma_free_coherent(host->dev, host->bounce_buf_size,
1346 host->bounce_buf, host->bounce_dma_addr);
1348 clk_disable_unprepare(host->mmc_clk);
1349 clk_disable_unprepare(host->core_clk);
1351 mmc_free_host(host->mmc);
1352 return 0;
1355 static const struct meson_mmc_data meson_gx_data = {
1356 .tx_delay_mask = CLK_V2_TX_DELAY_MASK,
1357 .rx_delay_mask = CLK_V2_RX_DELAY_MASK,
1358 .always_on = CLK_V2_ALWAYS_ON,
1361 static const struct meson_mmc_data meson_axg_data = {
1362 .tx_delay_mask = CLK_V3_TX_DELAY_MASK,
1363 .rx_delay_mask = CLK_V3_RX_DELAY_MASK,
1364 .always_on = CLK_V3_ALWAYS_ON,
1367 static const struct of_device_id meson_mmc_of_match[] = {
1368 { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data },
1369 { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data },
1370 { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data },
1371 { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data },
1372 { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data },
1375 MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
1377 static struct platform_driver meson_mmc_driver = {
1378 .probe = meson_mmc_probe,
1379 .remove = meson_mmc_remove,
1380 .driver = {
1381 .name = DRIVER_NAME,
1382 .of_match_table = of_match_ptr(meson_mmc_of_match),
1386 module_platform_driver(meson_mmc_driver);
1388 MODULE_DESCRIPTION("Amlogic S905*/GX*/AXG SD/eMMC driver");
1389 MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
1390 MODULE_LICENSE("GPL v2");