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>
35 #include <linux/clk.h>
36 #include <linux/clk-provider.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/interrupt.h>
39 #include <linux/bitfield.h>
41 #define DRIVER_NAME "meson-gx-mmc"
43 #define SD_EMMC_CLOCK 0x0
44 #define CLK_DIV_MASK GENMASK(5, 0)
45 #define CLK_DIV_MAX 63
46 #define CLK_SRC_MASK GENMASK(7, 6)
47 #define CLK_SRC_XTAL 0 /* external crystal */
48 #define CLK_SRC_XTAL_RATE 24000000
49 #define CLK_SRC_PLL 1 /* FCLK_DIV2 */
50 #define CLK_SRC_PLL_RATE 1000000000
51 #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
52 #define CLK_TX_PHASE_MASK GENMASK(11, 10)
53 #define CLK_RX_PHASE_MASK GENMASK(13, 12)
55 #define CLK_PHASE_90 1
56 #define CLK_PHASE_180 2
57 #define CLK_PHASE_270 3
58 #define CLK_ALWAYS_ON BIT(24)
60 #define SD_EMMC_DElAY 0x4
61 #define SD_EMMC_ADJUST 0x8
62 #define SD_EMMC_CALOUT 0x10
63 #define SD_EMMC_START 0x40
64 #define START_DESC_INIT BIT(0)
65 #define START_DESC_BUSY BIT(1)
66 #define START_DESC_ADDR_MASK GENMASK(31, 2)
68 #define SD_EMMC_CFG 0x44
69 #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
70 #define CFG_BUS_WIDTH_1 0x0
71 #define CFG_BUS_WIDTH_4 0x1
72 #define CFG_BUS_WIDTH_8 0x2
73 #define CFG_DDR BIT(2)
74 #define CFG_BLK_LEN_MASK GENMASK(7, 4)
75 #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
76 #define CFG_RC_CC_MASK GENMASK(15, 12)
77 #define CFG_STOP_CLOCK BIT(22)
78 #define CFG_CLK_ALWAYS_ON BIT(18)
79 #define CFG_CHK_DS BIT(20)
80 #define CFG_AUTO_CLK BIT(23)
82 #define SD_EMMC_STATUS 0x48
83 #define STATUS_BUSY BIT(31)
85 #define SD_EMMC_IRQ_EN 0x4c
86 #define IRQ_EN_MASK GENMASK(13, 0)
87 #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
88 #define IRQ_TXD_ERR BIT(8)
89 #define IRQ_DESC_ERR BIT(9)
90 #define IRQ_RESP_ERR BIT(10)
91 #define IRQ_RESP_TIMEOUT BIT(11)
92 #define IRQ_DESC_TIMEOUT BIT(12)
93 #define IRQ_END_OF_CHAIN BIT(13)
94 #define IRQ_RESP_STATUS BIT(14)
95 #define IRQ_SDIO BIT(15)
97 #define SD_EMMC_CMD_CFG 0x50
98 #define SD_EMMC_CMD_ARG 0x54
99 #define SD_EMMC_CMD_DAT 0x58
100 #define SD_EMMC_CMD_RSP 0x5c
101 #define SD_EMMC_CMD_RSP1 0x60
102 #define SD_EMMC_CMD_RSP2 0x64
103 #define SD_EMMC_CMD_RSP3 0x68
105 #define SD_EMMC_RXD 0x94
106 #define SD_EMMC_TXD 0x94
107 #define SD_EMMC_LAST_REG SD_EMMC_TXD
109 #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
110 #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
111 #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
112 #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
113 #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
114 #define MUX_CLK_NUM_PARENTS 2
116 struct meson_tuning_params {
124 struct mmc_host *mmc;
125 struct mmc_command *cmd;
129 struct clk *core_clk;
132 unsigned long current_clock;
134 struct clk_divider cfg_div;
135 struct clk *cfg_div_clk;
137 unsigned int bounce_buf_size;
139 dma_addr_t bounce_dma_addr;
141 struct meson_tuning_params tp;
145 struct sd_emmc_desc {
152 #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
153 #define CMD_CFG_BLOCK_MODE BIT(9)
154 #define CMD_CFG_R1B BIT(10)
155 #define CMD_CFG_END_OF_CHAIN BIT(11)
156 #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
157 #define CMD_CFG_NO_RESP BIT(16)
158 #define CMD_CFG_NO_CMD BIT(17)
159 #define CMD_CFG_DATA_IO BIT(18)
160 #define CMD_CFG_DATA_WR BIT(19)
161 #define CMD_CFG_RESP_NOCRC BIT(20)
162 #define CMD_CFG_RESP_128 BIT(21)
163 #define CMD_CFG_RESP_NUM BIT(22)
164 #define CMD_CFG_DATA_NUM BIT(23)
165 #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
166 #define CMD_CFG_ERROR BIT(30)
167 #define CMD_CFG_OWNER BIT(31)
169 #define CMD_DATA_MASK GENMASK(31, 2)
170 #define CMD_DATA_BIG_ENDIAN BIT(1)
171 #define CMD_DATA_SRAM BIT(0)
172 #define CMD_RESP_MASK GENMASK(31, 1)
173 #define CMD_RESP_SRAM BIT(0)
175 static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data)
177 unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC;
180 return SD_EMMC_CMD_TIMEOUT_DATA;
182 timeout = roundup_pow_of_two(timeout);
184 return min(timeout, 32768U); /* max. 2^15 ms */
187 static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd)
189 if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
190 return cmd->mrq->cmd;
191 else if (mmc_op_multi(cmd->opcode) &&
192 (!cmd->mrq->sbc || cmd->error || cmd->data->error))
193 return cmd->mrq->stop;
198 static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
200 struct mmc_host *mmc = host->mmc;
205 if (WARN_ON(clk_rate > mmc->f_max))
206 clk_rate = mmc->f_max;
207 else if (WARN_ON(clk_rate < mmc->f_min))
208 clk_rate = mmc->f_min;
211 if (clk_rate == host->current_clock)
215 cfg = readl(host->regs + SD_EMMC_CFG);
216 if (!(cfg & CFG_STOP_CLOCK)) {
217 cfg |= CFG_STOP_CLOCK;
218 writel(cfg, host->regs + SD_EMMC_CFG);
221 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
222 mmc->actual_clock, clk_rate);
225 mmc->actual_clock = 0;
226 host->current_clock = 0;
227 /* return with clock being stopped */
231 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
233 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
238 mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
239 host->current_clock = clk_rate;
241 if (clk_rate != mmc->actual_clock)
243 "divider requested rate %lu != actual rate %u\n",
244 clk_rate, mmc->actual_clock);
246 /* (re)start clock */
247 cfg = readl(host->regs + SD_EMMC_CFG);
248 cfg &= ~CFG_STOP_CLOCK;
249 writel(cfg, host->regs + SD_EMMC_CFG);
255 * The SD/eMMC IP block has an internal mux and divider used for
256 * generating the MMC clock. Use the clock framework to create and
257 * manage these clocks.
259 static int meson_mmc_clk_init(struct meson_host *host)
261 struct clk_init_data init;
264 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
265 const char *clk_div_parents[1];
268 /* get the mux parents */
269 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
273 snprintf(name, sizeof(name), "clkin%d", i);
274 clk = devm_clk_get(host->dev, name);
276 if (clk != ERR_PTR(-EPROBE_DEFER))
277 dev_err(host->dev, "Missing clock %s\n", name);
281 mux_parent_names[i] = __clk_get_name(clk);
285 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
286 init.name = clk_name;
287 init.ops = &clk_mux_ops;
289 init.parent_names = mux_parent_names;
290 init.num_parents = MUX_CLK_NUM_PARENTS;
291 host->mux.reg = host->regs + SD_EMMC_CLOCK;
292 host->mux.shift = __bf_shf(CLK_SRC_MASK);
293 host->mux.mask = CLK_SRC_MASK;
295 host->mux.table = NULL;
296 host->mux.hw.init = &init;
298 host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
299 if (WARN_ON(IS_ERR(host->mux_clk)))
300 return PTR_ERR(host->mux_clk);
302 /* create the divider */
303 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
304 init.name = clk_name;
305 init.ops = &clk_divider_ops;
306 init.flags = CLK_SET_RATE_PARENT;
307 clk_div_parents[0] = __clk_get_name(host->mux_clk);
308 init.parent_names = clk_div_parents;
309 init.num_parents = ARRAY_SIZE(clk_div_parents);
311 host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
312 host->cfg_div.shift = __bf_shf(CLK_DIV_MASK);
313 host->cfg_div.width = __builtin_popcountl(CLK_DIV_MASK);
314 host->cfg_div.hw.init = &init;
315 host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
316 CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO;
318 host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
319 if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
320 return PTR_ERR(host->cfg_div_clk);
322 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
324 clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, host->tp.core_phase);
325 clk_reg |= FIELD_PREP(CLK_TX_PHASE_MASK, host->tp.tx_phase);
326 clk_reg |= FIELD_PREP(CLK_RX_PHASE_MASK, host->tp.rx_phase);
327 clk_reg |= FIELD_PREP(CLK_SRC_MASK, CLK_SRC_XTAL);
328 clk_reg |= FIELD_PREP(CLK_DIV_MASK, CLK_DIV_MAX);
329 clk_reg &= ~CLK_ALWAYS_ON;
330 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
332 /* Ensure clock starts in "auto" mode, not "always on" */
333 cfg = readl(host->regs + SD_EMMC_CFG);
334 cfg &= ~CFG_CLK_ALWAYS_ON;
336 writel(cfg, host->regs + SD_EMMC_CFG);
338 ret = clk_prepare_enable(host->cfg_div_clk);
342 /* Get the nearest minimum clock to 400KHz */
343 host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
345 ret = meson_mmc_clk_set(host, host->mmc->f_min);
347 clk_disable_unprepare(host->cfg_div_clk);
352 static void meson_mmc_set_tuning_params(struct mmc_host *mmc)
354 struct meson_host *host = mmc_priv(mmc);
358 regval = readl(host->regs + SD_EMMC_CFG);
359 regval |= CFG_STOP_CLOCK;
360 writel(regval, host->regs + SD_EMMC_CFG);
362 regval = readl(host->regs + SD_EMMC_CLOCK);
363 regval &= ~CLK_CORE_PHASE_MASK;
364 regval |= FIELD_PREP(CLK_CORE_PHASE_MASK, host->tp.core_phase);
365 regval &= ~CLK_TX_PHASE_MASK;
366 regval |= FIELD_PREP(CLK_TX_PHASE_MASK, host->tp.tx_phase);
367 regval &= ~CLK_RX_PHASE_MASK;
368 regval |= FIELD_PREP(CLK_RX_PHASE_MASK, host->tp.rx_phase);
369 writel(regval, host->regs + SD_EMMC_CLOCK);
372 regval = readl(host->regs + SD_EMMC_CFG);
373 regval &= ~CFG_STOP_CLOCK;
374 writel(regval, host->regs + SD_EMMC_CFG);
377 static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
379 struct meson_host *host = mmc_priv(mmc);
384 * GPIO regulator, only controls switching between 1v8 and
385 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
387 switch (ios->power_mode) {
389 if (!IS_ERR(mmc->supply.vmmc))
390 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
392 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
393 regulator_disable(mmc->supply.vqmmc);
394 host->vqmmc_enabled = false;
400 if (!IS_ERR(mmc->supply.vmmc))
401 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
405 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
406 int ret = regulator_enable(mmc->supply.vqmmc);
409 dev_err(mmc_dev(mmc),
410 "failed to enable vqmmc regulator\n");
412 host->vqmmc_enabled = true;
419 meson_mmc_clk_set(host, ios->clock);
422 switch (ios->bus_width) {
423 case MMC_BUS_WIDTH_1:
424 bus_width = CFG_BUS_WIDTH_1;
426 case MMC_BUS_WIDTH_4:
427 bus_width = CFG_BUS_WIDTH_4;
429 case MMC_BUS_WIDTH_8:
430 bus_width = CFG_BUS_WIDTH_8;
433 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
435 bus_width = CFG_BUS_WIDTH_4;
438 val = readl(host->regs + SD_EMMC_CFG);
441 val &= ~CFG_BUS_WIDTH_MASK;
442 val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
445 if (ios->timing == MMC_TIMING_UHS_DDR50 ||
446 ios->timing == MMC_TIMING_MMC_DDR52 ||
447 ios->timing == MMC_TIMING_MMC_HS400)
451 if (ios->timing == MMC_TIMING_MMC_HS400)
455 writel(val, host->regs + SD_EMMC_CFG);
456 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
457 __func__, orig, val);
461 static void meson_mmc_request_done(struct mmc_host *mmc,
462 struct mmc_request *mrq)
464 struct meson_host *host = mmc_priv(mmc);
467 mmc_request_done(host->mmc, mrq);
470 static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz)
472 struct meson_host *host = mmc_priv(mmc);
475 cfg = readl(host->regs + SD_EMMC_CFG);
476 blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
478 if (!is_power_of_2(blksz))
479 dev_err(host->dev, "blksz %u is not a power of 2\n", blksz);
481 blksz = ilog2(blksz);
483 /* check if block-size matches, if not update */
484 if (blksz == blksz_old)
487 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__,
490 cfg &= ~CFG_BLK_LEN_MASK;
491 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz);
492 writel(cfg, host->regs + SD_EMMC_CFG);
495 static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg)
497 if (cmd->flags & MMC_RSP_PRESENT) {
498 if (cmd->flags & MMC_RSP_136)
499 *cmd_cfg |= CMD_CFG_RESP_128;
500 *cmd_cfg |= CMD_CFG_RESP_NUM;
502 if (!(cmd->flags & MMC_RSP_CRC))
503 *cmd_cfg |= CMD_CFG_RESP_NOCRC;
505 if (cmd->flags & MMC_RSP_BUSY)
506 *cmd_cfg |= CMD_CFG_R1B;
508 *cmd_cfg |= CMD_CFG_NO_RESP;
512 static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
514 struct meson_host *host = mmc_priv(mmc);
515 struct mmc_data *data = cmd->data;
516 u32 cmd_cfg = 0, cmd_data = 0;
517 unsigned int xfer_bytes = 0;
519 /* Setup descriptors */
522 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
523 cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
525 meson_mmc_set_response_bits(cmd, &cmd_cfg);
529 cmd_cfg |= CMD_CFG_DATA_IO;
530 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
531 ilog2(meson_mmc_get_timeout_msecs(data)));
533 if (data->blocks > 1) {
534 cmd_cfg |= CMD_CFG_BLOCK_MODE;
535 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
537 meson_mmc_set_blksz(mmc, data->blksz);
539 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
542 data->bytes_xfered = 0;
543 xfer_bytes = data->blksz * data->blocks;
544 if (data->flags & MMC_DATA_WRITE) {
545 cmd_cfg |= CMD_CFG_DATA_WR;
546 WARN_ON(xfer_bytes > host->bounce_buf_size);
547 sg_copy_to_buffer(data->sg, data->sg_len,
548 host->bounce_buf, xfer_bytes);
552 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
554 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
555 ilog2(SD_EMMC_CMD_TIMEOUT));
560 /* Last descriptor */
561 cmd_cfg |= CMD_CFG_END_OF_CHAIN;
562 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
563 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
564 writel(0, host->regs + SD_EMMC_CMD_RSP);
565 wmb(); /* ensure descriptor is written before kicked */
566 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
569 static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
571 struct meson_host *host = mmc_priv(mmc);
574 writel(0, host->regs + SD_EMMC_START);
577 meson_mmc_start_cmd(mmc, mrq->sbc);
579 meson_mmc_start_cmd(mmc, mrq->cmd);
582 static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
584 struct meson_host *host = mmc_priv(mmc);
586 if (cmd->flags & MMC_RSP_136) {
587 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
588 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
589 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
590 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
591 } else if (cmd->flags & MMC_RSP_PRESENT) {
592 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
596 static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
598 struct meson_host *host = dev_id;
599 struct mmc_command *cmd;
600 struct mmc_data *data;
601 u32 irq_en, status, raw_status;
602 irqreturn_t ret = IRQ_HANDLED;
614 spin_lock(&host->lock);
615 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
616 raw_status = readl(host->regs + SD_EMMC_STATUS);
617 status = raw_status & irq_en;
620 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
626 meson_mmc_read_resp(host->mmc, cmd);
629 if (status & IRQ_RXD_ERR_MASK) {
630 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
631 cmd->error = -EILSEQ;
633 if (status & IRQ_TXD_ERR) {
634 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
635 cmd->error = -EILSEQ;
637 if (status & IRQ_DESC_ERR)
638 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
639 if (status & IRQ_RESP_ERR) {
640 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
641 cmd->error = -EILSEQ;
643 if (status & IRQ_RESP_TIMEOUT) {
644 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
645 cmd->error = -ETIMEDOUT;
647 if (status & IRQ_DESC_TIMEOUT) {
648 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
649 cmd->error = -ETIMEDOUT;
651 if (status & IRQ_SDIO)
652 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
654 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
655 if (data && !cmd->error)
656 data->bytes_xfered = data->blksz * data->blocks;
657 ret = IRQ_WAKE_THREAD;
659 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
660 status, cmd->opcode, cmd->arg,
661 cmd->flags, cmd->mrq->stop ? 1 : 0);
663 struct mmc_data *data = cmd->data;
665 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
666 data->blksz, data->blocks, data->flags,
667 data->flags & MMC_DATA_WRITE ? "write" : "",
668 data->flags & MMC_DATA_READ ? "read" : "");
673 /* ack all (enabled) interrupts */
674 writel(status, host->regs + SD_EMMC_STATUS);
676 if (ret == IRQ_HANDLED)
677 meson_mmc_request_done(host->mmc, cmd->mrq);
679 spin_unlock(&host->lock);
683 static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
685 struct meson_host *host = dev_id;
686 struct mmc_command *next_cmd, *cmd = host->cmd;
687 struct mmc_data *data;
688 unsigned int xfer_bytes;
694 if (data && data->flags & MMC_DATA_READ) {
695 xfer_bytes = data->blksz * data->blocks;
696 WARN_ON(xfer_bytes > host->bounce_buf_size);
697 sg_copy_from_buffer(data->sg, data->sg_len,
698 host->bounce_buf, xfer_bytes);
701 next_cmd = meson_mmc_get_next_command(cmd);
703 meson_mmc_start_cmd(host->mmc, next_cmd);
705 meson_mmc_request_done(host->mmc, cmd->mrq);
710 static int meson_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
712 struct meson_host *host = mmc_priv(mmc);
713 struct meson_tuning_params tp_old = host->tp;
714 int ret = -EINVAL, i, cmd_error;
716 dev_info(mmc_dev(mmc), "(re)tuning...\n");
718 for (i = CLK_PHASE_0; i <= CLK_PHASE_270; i++) {
719 host->tp.rx_phase = i;
720 /* exclude the active parameter set if retuning */
721 if (!memcmp(&tp_old, &host->tp, sizeof(tp_old)) &&
724 meson_mmc_set_tuning_params(mmc);
725 ret = mmc_send_tuning(mmc, opcode, &cmd_error);
734 * NOTE: we only need this until the GPIO/pinctrl driver can handle
735 * interrupts. For now, the MMC core will use this for polling.
737 static int meson_mmc_get_cd(struct mmc_host *mmc)
739 int status = mmc_gpio_get_cd(mmc);
741 if (status == -ENOSYS)
742 return 1; /* assume present */
747 static void meson_mmc_cfg_init(struct meson_host *host)
751 cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
752 ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
753 cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
754 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
756 writel(cfg, host->regs + SD_EMMC_CFG);
759 static const struct mmc_host_ops meson_mmc_ops = {
760 .request = meson_mmc_request,
761 .set_ios = meson_mmc_set_ios,
762 .get_cd = meson_mmc_get_cd,
763 .execute_tuning = meson_mmc_execute_tuning,
766 static int meson_mmc_probe(struct platform_device *pdev)
768 struct resource *res;
769 struct meson_host *host;
770 struct mmc_host *mmc;
773 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
776 host = mmc_priv(mmc);
778 host->dev = &pdev->dev;
779 dev_set_drvdata(&pdev->dev, host);
781 spin_lock_init(&host->lock);
783 /* Get regulators and the supported OCR mask */
784 host->vqmmc_enabled = false;
785 ret = mmc_regulator_get_supply(mmc);
786 if (ret == -EPROBE_DEFER)
789 ret = mmc_of_parse(mmc);
791 if (ret != -EPROBE_DEFER)
792 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
796 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
797 host->regs = devm_ioremap_resource(&pdev->dev, res);
798 if (IS_ERR(host->regs)) {
799 ret = PTR_ERR(host->regs);
803 irq = platform_get_irq(pdev, 0);
805 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
810 host->core_clk = devm_clk_get(&pdev->dev, "core");
811 if (IS_ERR(host->core_clk)) {
812 ret = PTR_ERR(host->core_clk);
816 ret = clk_prepare_enable(host->core_clk);
820 host->tp.core_phase = CLK_PHASE_180;
821 host->tp.tx_phase = CLK_PHASE_0;
822 host->tp.rx_phase = CLK_PHASE_0;
824 ret = meson_mmc_clk_init(host);
829 writel(0, host->regs + SD_EMMC_START);
831 /* clear, ack, enable all interrupts */
832 writel(0, host->regs + SD_EMMC_IRQ_EN);
833 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
834 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
836 /* set config to sane default */
837 meson_mmc_cfg_init(host);
839 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
840 meson_mmc_irq_thread, IRQF_SHARED,
845 mmc->caps |= MMC_CAP_CMD23;
846 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
847 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
849 /* data bounce buffer */
850 host->bounce_buf_size = mmc->max_req_size;
852 dma_alloc_coherent(host->dev, host->bounce_buf_size,
853 &host->bounce_dma_addr, GFP_KERNEL);
854 if (host->bounce_buf == NULL) {
855 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
860 mmc->ops = &meson_mmc_ops;
866 clk_disable_unprepare(host->cfg_div_clk);
868 clk_disable_unprepare(host->core_clk);
874 static int meson_mmc_remove(struct platform_device *pdev)
876 struct meson_host *host = dev_get_drvdata(&pdev->dev);
878 mmc_remove_host(host->mmc);
880 /* disable interrupts */
881 writel(0, host->regs + SD_EMMC_IRQ_EN);
883 dma_free_coherent(host->dev, host->bounce_buf_size,
884 host->bounce_buf, host->bounce_dma_addr);
886 clk_disable_unprepare(host->cfg_div_clk);
887 clk_disable_unprepare(host->core_clk);
889 mmc_free_host(host->mmc);
893 static const struct of_device_id meson_mmc_of_match[] = {
894 { .compatible = "amlogic,meson-gx-mmc", },
895 { .compatible = "amlogic,meson-gxbb-mmc", },
896 { .compatible = "amlogic,meson-gxl-mmc", },
897 { .compatible = "amlogic,meson-gxm-mmc", },
900 MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
902 static struct platform_driver meson_mmc_driver = {
903 .probe = meson_mmc_probe,
904 .remove = meson_mmc_remove,
907 .of_match_table = of_match_ptr(meson_mmc_of_match),
911 module_platform_driver(meson_mmc_driver);
913 MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
914 MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
915 MODULE_LICENSE("GPL v2");