1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic SD/eMMC driver for the GX/S905 family SoCs
5 * Copyright (c) 2016 BayLibre, SAS.
6 * Author: Kevin Hilman <khilman@baylibre.com>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/iopoll.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/ioport.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/mmc.h>
20 #include <linux/mmc/sdio.h>
21 #include <linux/mmc/slot-gpio.h>
23 #include <linux/clk.h>
24 #include <linux/clk-provider.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/reset.h>
27 #include <linux/interrupt.h>
28 #include <linux/bitfield.h>
29 #include <linux/pinctrl/consumer.h>
31 #define DRIVER_NAME "meson-gx-mmc"
33 #define SD_EMMC_CLOCK 0x0
34 #define CLK_DIV_MASK GENMASK(5, 0)
35 #define CLK_SRC_MASK GENMASK(7, 6)
36 #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
37 #define CLK_TX_PHASE_MASK GENMASK(11, 10)
38 #define CLK_RX_PHASE_MASK GENMASK(13, 12)
40 #define CLK_PHASE_180 2
41 #define CLK_V2_TX_DELAY_MASK GENMASK(19, 16)
42 #define CLK_V2_RX_DELAY_MASK GENMASK(23, 20)
43 #define CLK_V2_ALWAYS_ON BIT(24)
45 #define CLK_V3_TX_DELAY_MASK GENMASK(21, 16)
46 #define CLK_V3_RX_DELAY_MASK GENMASK(27, 22)
47 #define CLK_V3_ALWAYS_ON BIT(28)
49 #define CLK_TX_DELAY_MASK(h) (h->data->tx_delay_mask)
50 #define CLK_RX_DELAY_MASK(h) (h->data->rx_delay_mask)
51 #define CLK_ALWAYS_ON(h) (h->data->always_on)
53 #define SD_EMMC_DELAY 0x4
54 #define SD_EMMC_ADJUST 0x8
55 #define ADJUST_ADJ_DELAY_MASK GENMASK(21, 16)
56 #define ADJUST_DS_EN BIT(15)
57 #define ADJUST_ADJ_EN BIT(13)
59 #define SD_EMMC_DELAY1 0x4
60 #define SD_EMMC_DELAY2 0x8
61 #define SD_EMMC_V3_ADJUST 0xc
63 #define SD_EMMC_CALOUT 0x10
64 #define SD_EMMC_START 0x40
65 #define START_DESC_INIT BIT(0)
66 #define START_DESC_BUSY BIT(1)
67 #define START_DESC_ADDR_MASK GENMASK(31, 2)
69 #define SD_EMMC_CFG 0x44
70 #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
71 #define CFG_BUS_WIDTH_1 0x0
72 #define CFG_BUS_WIDTH_4 0x1
73 #define CFG_BUS_WIDTH_8 0x2
74 #define CFG_DDR BIT(2)
75 #define CFG_BLK_LEN_MASK GENMASK(7, 4)
76 #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
77 #define CFG_RC_CC_MASK GENMASK(15, 12)
78 #define CFG_STOP_CLOCK BIT(22)
79 #define CFG_CLK_ALWAYS_ON BIT(18)
80 #define CFG_CHK_DS BIT(20)
81 #define CFG_AUTO_CLK BIT(23)
82 #define CFG_ERR_ABORT BIT(27)
84 #define SD_EMMC_STATUS 0x48
85 #define STATUS_BUSY BIT(31)
86 #define STATUS_DESC_BUSY BIT(30)
87 #define STATUS_DATI GENMASK(23, 16)
89 #define SD_EMMC_IRQ_EN 0x4c
90 #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
91 #define IRQ_TXD_ERR BIT(8)
92 #define IRQ_DESC_ERR BIT(9)
93 #define IRQ_RESP_ERR BIT(10)
95 (IRQ_RXD_ERR_MASK | IRQ_TXD_ERR | IRQ_DESC_ERR | IRQ_RESP_ERR)
96 #define IRQ_RESP_TIMEOUT BIT(11)
97 #define IRQ_DESC_TIMEOUT BIT(12)
98 #define IRQ_TIMEOUTS \
99 (IRQ_RESP_TIMEOUT | IRQ_DESC_TIMEOUT)
100 #define IRQ_END_OF_CHAIN BIT(13)
101 #define IRQ_RESP_STATUS BIT(14)
102 #define IRQ_SDIO BIT(15)
103 #define IRQ_EN_MASK \
104 (IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN | IRQ_RESP_STATUS |\
107 #define SD_EMMC_CMD_CFG 0x50
108 #define SD_EMMC_CMD_ARG 0x54
109 #define SD_EMMC_CMD_DAT 0x58
110 #define SD_EMMC_CMD_RSP 0x5c
111 #define SD_EMMC_CMD_RSP1 0x60
112 #define SD_EMMC_CMD_RSP2 0x64
113 #define SD_EMMC_CMD_RSP3 0x68
115 #define SD_EMMC_RXD 0x94
116 #define SD_EMMC_TXD 0x94
117 #define SD_EMMC_LAST_REG SD_EMMC_TXD
119 #define SD_EMMC_SRAM_DATA_BUF_LEN 1536
120 #define SD_EMMC_SRAM_DATA_BUF_OFF 0x200
122 #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
123 #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
124 #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
125 #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
126 #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
127 #define SD_EMMC_DESC_BUF_LEN PAGE_SIZE
129 #define SD_EMMC_PRE_REQ_DONE BIT(0)
130 #define SD_EMMC_DESC_CHAIN_MODE BIT(1)
132 #define MUX_CLK_NUM_PARENTS 2
134 struct meson_mmc_data
{
135 unsigned int tx_delay_mask
;
136 unsigned int rx_delay_mask
;
137 unsigned int always_on
;
141 struct sd_emmc_desc
{
150 struct meson_mmc_data
*data
;
151 struct mmc_host
*mmc
;
152 struct mmc_command
*cmd
;
155 struct clk
*core_clk
;
158 unsigned long req_rate
;
161 bool dram_access_quirk
;
163 struct pinctrl
*pinctrl
;
164 struct pinctrl_state
*pins_default
;
165 struct pinctrl_state
*pins_clk_gate
;
167 unsigned int bounce_buf_size
;
169 dma_addr_t bounce_dma_addr
;
170 struct sd_emmc_desc
*descs
;
171 dma_addr_t descs_dma_addr
;
178 #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
179 #define CMD_CFG_BLOCK_MODE BIT(9)
180 #define CMD_CFG_R1B BIT(10)
181 #define CMD_CFG_END_OF_CHAIN BIT(11)
182 #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
183 #define CMD_CFG_NO_RESP BIT(16)
184 #define CMD_CFG_NO_CMD BIT(17)
185 #define CMD_CFG_DATA_IO BIT(18)
186 #define CMD_CFG_DATA_WR BIT(19)
187 #define CMD_CFG_RESP_NOCRC BIT(20)
188 #define CMD_CFG_RESP_128 BIT(21)
189 #define CMD_CFG_RESP_NUM BIT(22)
190 #define CMD_CFG_DATA_NUM BIT(23)
191 #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
192 #define CMD_CFG_ERROR BIT(30)
193 #define CMD_CFG_OWNER BIT(31)
195 #define CMD_DATA_MASK GENMASK(31, 2)
196 #define CMD_DATA_BIG_ENDIAN BIT(1)
197 #define CMD_DATA_SRAM BIT(0)
198 #define CMD_RESP_MASK GENMASK(31, 1)
199 #define CMD_RESP_SRAM BIT(0)
201 static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data
*data
)
203 unsigned int timeout
= data
->timeout_ns
/ NSEC_PER_MSEC
;
206 return SD_EMMC_CMD_TIMEOUT_DATA
;
208 timeout
= roundup_pow_of_two(timeout
);
210 return min(timeout
, 32768U); /* max. 2^15 ms */
213 static struct mmc_command
*meson_mmc_get_next_command(struct mmc_command
*cmd
)
215 if (cmd
->opcode
== MMC_SET_BLOCK_COUNT
&& !cmd
->error
)
216 return cmd
->mrq
->cmd
;
217 else if (mmc_op_multi(cmd
->opcode
) &&
218 (!cmd
->mrq
->sbc
|| cmd
->error
|| cmd
->data
->error
))
219 return cmd
->mrq
->stop
;
224 static void meson_mmc_get_transfer_mode(struct mmc_host
*mmc
,
225 struct mmc_request
*mrq
)
227 struct meson_host
*host
= mmc_priv(mmc
);
228 struct mmc_data
*data
= mrq
->data
;
229 struct scatterlist
*sg
;
231 bool use_desc_chain_mode
= true;
234 * When Controller DMA cannot directly access DDR memory, disable
235 * support for Chain Mode to directly use the internal SRAM using
236 * the bounce buffer mode.
238 if (host
->dram_access_quirk
)
242 * Broken SDIO with AP6255-based WiFi on Khadas VIM Pro has been
243 * reported. For some strange reason this occurs in descriptor
244 * chain mode only. So let's fall back to bounce buffer mode
245 * for command SD_IO_RW_EXTENDED.
247 if (mrq
->cmd
->opcode
== SD_IO_RW_EXTENDED
)
250 for_each_sg(data
->sg
, sg
, data
->sg_len
, i
)
251 /* check for 8 byte alignment */
252 if (sg
->offset
& 7) {
253 WARN_ONCE(1, "unaligned scatterlist buffer\n");
254 use_desc_chain_mode
= false;
258 if (use_desc_chain_mode
)
259 data
->host_cookie
|= SD_EMMC_DESC_CHAIN_MODE
;
262 static inline bool meson_mmc_desc_chain_mode(const struct mmc_data
*data
)
264 return data
->host_cookie
& SD_EMMC_DESC_CHAIN_MODE
;
267 static inline bool meson_mmc_bounce_buf_read(const struct mmc_data
*data
)
269 return data
&& data
->flags
& MMC_DATA_READ
&&
270 !meson_mmc_desc_chain_mode(data
);
273 static void meson_mmc_pre_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
275 struct mmc_data
*data
= mrq
->data
;
280 meson_mmc_get_transfer_mode(mmc
, mrq
);
281 data
->host_cookie
|= SD_EMMC_PRE_REQ_DONE
;
283 if (!meson_mmc_desc_chain_mode(data
))
286 data
->sg_count
= dma_map_sg(mmc_dev(mmc
), data
->sg
, data
->sg_len
,
287 mmc_get_dma_dir(data
));
289 dev_err(mmc_dev(mmc
), "dma_map_sg failed");
292 static void meson_mmc_post_req(struct mmc_host
*mmc
, struct mmc_request
*mrq
,
295 struct mmc_data
*data
= mrq
->data
;
297 if (data
&& meson_mmc_desc_chain_mode(data
) && data
->sg_count
)
298 dma_unmap_sg(mmc_dev(mmc
), data
->sg
, data
->sg_len
,
299 mmc_get_dma_dir(data
));
303 * Gating the clock on this controller is tricky. It seems the mmc clock
304 * is also used by the controller. It may crash during some operation if the
305 * clock is stopped. The safest thing to do, whenever possible, is to keep
306 * clock running at stop it at the pad using the pinmux.
308 static void meson_mmc_clk_gate(struct meson_host
*host
)
312 if (host
->pins_clk_gate
) {
313 pinctrl_select_state(host
->pinctrl
, host
->pins_clk_gate
);
316 * If the pinmux is not provided - default to the classic and
319 cfg
= readl(host
->regs
+ SD_EMMC_CFG
);
320 cfg
|= CFG_STOP_CLOCK
;
321 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
325 static void meson_mmc_clk_ungate(struct meson_host
*host
)
329 if (host
->pins_clk_gate
)
330 pinctrl_select_state(host
->pinctrl
, host
->pins_default
);
332 /* Make sure the clock is not stopped in the controller */
333 cfg
= readl(host
->regs
+ SD_EMMC_CFG
);
334 cfg
&= ~CFG_STOP_CLOCK
;
335 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
338 static int meson_mmc_clk_set(struct meson_host
*host
, unsigned long rate
,
341 struct mmc_host
*mmc
= host
->mmc
;
345 /* Same request - bail-out */
346 if (host
->ddr
== ddr
&& host
->req_rate
== rate
)
350 meson_mmc_clk_gate(host
);
352 mmc
->actual_clock
= 0;
354 /* return with clock being stopped */
358 /* Stop the clock during rate change to avoid glitches */
359 cfg
= readl(host
->regs
+ SD_EMMC_CFG
);
360 cfg
|= CFG_STOP_CLOCK
;
361 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
364 /* DDR modes require higher module clock */
370 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
373 ret
= clk_set_rate(host
->mmc_clk
, rate
);
375 dev_err(host
->dev
, "Unable to set cfg_div_clk to %lu. ret=%d\n",
380 host
->req_rate
= rate
;
381 mmc
->actual_clock
= clk_get_rate(host
->mmc_clk
);
383 /* We should report the real output frequency of the controller */
385 host
->req_rate
>>= 1;
386 mmc
->actual_clock
>>= 1;
389 dev_dbg(host
->dev
, "clk rate: %u Hz\n", mmc
->actual_clock
);
390 if (rate
!= mmc
->actual_clock
)
391 dev_dbg(host
->dev
, "requested rate was %lu\n", rate
);
393 /* (re)start clock */
394 meson_mmc_clk_ungate(host
);
400 * The SD/eMMC IP block has an internal mux and divider used for
401 * generating the MMC clock. Use the clock framework to create and
402 * manage these clocks.
404 static int meson_mmc_clk_init(struct meson_host
*host
)
406 struct clk_init_data init
;
408 struct clk_divider
*div
;
411 const char *mux_parent_names
[MUX_CLK_NUM_PARENTS
];
412 const char *clk_parent
[1];
415 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
416 clk_reg
= CLK_ALWAYS_ON(host
);
417 clk_reg
|= CLK_DIV_MASK
;
418 clk_reg
|= FIELD_PREP(CLK_CORE_PHASE_MASK
, CLK_PHASE_180
);
419 clk_reg
|= FIELD_PREP(CLK_TX_PHASE_MASK
, CLK_PHASE_0
);
420 clk_reg
|= FIELD_PREP(CLK_RX_PHASE_MASK
, CLK_PHASE_0
);
421 writel(clk_reg
, host
->regs
+ SD_EMMC_CLOCK
);
423 /* get the mux parents */
424 for (i
= 0; i
< MUX_CLK_NUM_PARENTS
; i
++) {
428 snprintf(name
, sizeof(name
), "clkin%d", i
);
429 clk
= devm_clk_get(host
->dev
, name
);
431 if (clk
!= ERR_PTR(-EPROBE_DEFER
))
432 dev_err(host
->dev
, "Missing clock %s\n", name
);
436 mux_parent_names
[i
] = __clk_get_name(clk
);
440 mux
= devm_kzalloc(host
->dev
, sizeof(*mux
), GFP_KERNEL
);
444 snprintf(clk_name
, sizeof(clk_name
), "%s#mux", dev_name(host
->dev
));
445 init
.name
= clk_name
;
446 init
.ops
= &clk_mux_ops
;
448 init
.parent_names
= mux_parent_names
;
449 init
.num_parents
= MUX_CLK_NUM_PARENTS
;
451 mux
->reg
= host
->regs
+ SD_EMMC_CLOCK
;
452 mux
->shift
= __ffs(CLK_SRC_MASK
);
453 mux
->mask
= CLK_SRC_MASK
>> mux
->shift
;
454 mux
->hw
.init
= &init
;
456 host
->mux_clk
= devm_clk_register(host
->dev
, &mux
->hw
);
457 if (WARN_ON(IS_ERR(host
->mux_clk
)))
458 return PTR_ERR(host
->mux_clk
);
460 /* create the divider */
461 div
= devm_kzalloc(host
->dev
, sizeof(*div
), GFP_KERNEL
);
465 snprintf(clk_name
, sizeof(clk_name
), "%s#div", dev_name(host
->dev
));
466 init
.name
= clk_name
;
467 init
.ops
= &clk_divider_ops
;
468 init
.flags
= CLK_SET_RATE_PARENT
;
469 clk_parent
[0] = __clk_get_name(host
->mux_clk
);
470 init
.parent_names
= clk_parent
;
471 init
.num_parents
= 1;
473 div
->reg
= host
->regs
+ SD_EMMC_CLOCK
;
474 div
->shift
= __ffs(CLK_DIV_MASK
);
475 div
->width
= __builtin_popcountl(CLK_DIV_MASK
);
476 div
->hw
.init
= &init
;
477 div
->flags
= CLK_DIVIDER_ONE_BASED
;
479 host
->mmc_clk
= devm_clk_register(host
->dev
, &div
->hw
);
480 if (WARN_ON(IS_ERR(host
->mmc_clk
)))
481 return PTR_ERR(host
->mmc_clk
);
483 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
484 host
->mmc
->f_min
= clk_round_rate(host
->mmc_clk
, 400000);
485 ret
= clk_set_rate(host
->mmc_clk
, host
->mmc
->f_min
);
489 return clk_prepare_enable(host
->mmc_clk
);
492 static void meson_mmc_disable_resampling(struct meson_host
*host
)
494 unsigned int val
= readl(host
->regs
+ host
->data
->adjust
);
496 val
&= ~ADJUST_ADJ_EN
;
497 writel(val
, host
->regs
+ host
->data
->adjust
);
500 static void meson_mmc_reset_resampling(struct meson_host
*host
)
504 meson_mmc_disable_resampling(host
);
506 val
= readl(host
->regs
+ host
->data
->adjust
);
507 val
&= ~ADJUST_ADJ_DELAY_MASK
;
508 writel(val
, host
->regs
+ host
->data
->adjust
);
511 static int meson_mmc_resampling_tuning(struct mmc_host
*mmc
, u32 opcode
)
513 struct meson_host
*host
= mmc_priv(mmc
);
514 unsigned int val
, dly
, max_dly
, i
;
517 /* Resampling is done using the source clock */
518 max_dly
= DIV_ROUND_UP(clk_get_rate(host
->mux_clk
),
519 clk_get_rate(host
->mmc_clk
));
521 val
= readl(host
->regs
+ host
->data
->adjust
);
522 val
|= ADJUST_ADJ_EN
;
523 writel(val
, host
->regs
+ host
->data
->adjust
);
525 if (mmc
->doing_retune
)
526 dly
= FIELD_GET(ADJUST_ADJ_DELAY_MASK
, val
) + 1;
530 for (i
= 0; i
< max_dly
; i
++) {
531 val
&= ~ADJUST_ADJ_DELAY_MASK
;
532 val
|= FIELD_PREP(ADJUST_ADJ_DELAY_MASK
, (dly
+ i
) % max_dly
);
533 writel(val
, host
->regs
+ host
->data
->adjust
);
535 ret
= mmc_send_tuning(mmc
, opcode
, NULL
);
537 dev_dbg(mmc_dev(mmc
), "resampling delay: %u\n",
538 (dly
+ i
) % max_dly
);
543 meson_mmc_reset_resampling(host
);
547 static int meson_mmc_prepare_ios_clock(struct meson_host
*host
,
552 switch (ios
->timing
) {
553 case MMC_TIMING_MMC_DDR52
:
554 case MMC_TIMING_UHS_DDR50
:
563 return meson_mmc_clk_set(host
, ios
->clock
, ddr
);
566 static void meson_mmc_check_resampling(struct meson_host
*host
,
569 switch (ios
->timing
) {
570 case MMC_TIMING_LEGACY
:
571 case MMC_TIMING_MMC_HS
:
572 case MMC_TIMING_SD_HS
:
573 case MMC_TIMING_MMC_DDR52
:
574 meson_mmc_disable_resampling(host
);
579 static void meson_mmc_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
581 struct meson_host
*host
= mmc_priv(mmc
);
586 * GPIO regulator, only controls switching between 1v8 and
587 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
589 switch (ios
->power_mode
) {
591 if (!IS_ERR(mmc
->supply
.vmmc
))
592 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, 0);
594 if (!IS_ERR(mmc
->supply
.vqmmc
) && host
->vqmmc_enabled
) {
595 regulator_disable(mmc
->supply
.vqmmc
);
596 host
->vqmmc_enabled
= false;
602 if (!IS_ERR(mmc
->supply
.vmmc
))
603 mmc_regulator_set_ocr(mmc
, mmc
->supply
.vmmc
, ios
->vdd
);
608 if (!IS_ERR(mmc
->supply
.vqmmc
) && !host
->vqmmc_enabled
) {
609 int ret
= regulator_enable(mmc
->supply
.vqmmc
);
613 "failed to enable vqmmc regulator\n");
615 host
->vqmmc_enabled
= true;
622 switch (ios
->bus_width
) {
623 case MMC_BUS_WIDTH_1
:
624 bus_width
= CFG_BUS_WIDTH_1
;
626 case MMC_BUS_WIDTH_4
:
627 bus_width
= CFG_BUS_WIDTH_4
;
629 case MMC_BUS_WIDTH_8
:
630 bus_width
= CFG_BUS_WIDTH_8
;
633 dev_err(host
->dev
, "Invalid ios->bus_width: %u. Setting to 4.\n",
635 bus_width
= CFG_BUS_WIDTH_4
;
638 val
= readl(host
->regs
+ SD_EMMC_CFG
);
639 val
&= ~CFG_BUS_WIDTH_MASK
;
640 val
|= FIELD_PREP(CFG_BUS_WIDTH_MASK
, bus_width
);
641 writel(val
, host
->regs
+ SD_EMMC_CFG
);
643 meson_mmc_check_resampling(host
, ios
);
644 err
= meson_mmc_prepare_ios_clock(host
, ios
);
646 dev_err(host
->dev
, "Failed to set clock: %d\n,", err
);
648 dev_dbg(host
->dev
, "SD_EMMC_CFG: 0x%08x\n", val
);
651 static void meson_mmc_request_done(struct mmc_host
*mmc
,
652 struct mmc_request
*mrq
)
654 struct meson_host
*host
= mmc_priv(mmc
);
657 mmc_request_done(host
->mmc
, mrq
);
660 static void meson_mmc_set_blksz(struct mmc_host
*mmc
, unsigned int blksz
)
662 struct meson_host
*host
= mmc_priv(mmc
);
665 cfg
= readl(host
->regs
+ SD_EMMC_CFG
);
666 blksz_old
= FIELD_GET(CFG_BLK_LEN_MASK
, cfg
);
668 if (!is_power_of_2(blksz
))
669 dev_err(host
->dev
, "blksz %u is not a power of 2\n", blksz
);
671 blksz
= ilog2(blksz
);
673 /* check if block-size matches, if not update */
674 if (blksz
== blksz_old
)
677 dev_dbg(host
->dev
, "%s: update blk_len %d -> %d\n", __func__
,
680 cfg
&= ~CFG_BLK_LEN_MASK
;
681 cfg
|= FIELD_PREP(CFG_BLK_LEN_MASK
, blksz
);
682 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
685 static void meson_mmc_set_response_bits(struct mmc_command
*cmd
, u32
*cmd_cfg
)
687 if (cmd
->flags
& MMC_RSP_PRESENT
) {
688 if (cmd
->flags
& MMC_RSP_136
)
689 *cmd_cfg
|= CMD_CFG_RESP_128
;
690 *cmd_cfg
|= CMD_CFG_RESP_NUM
;
692 if (!(cmd
->flags
& MMC_RSP_CRC
))
693 *cmd_cfg
|= CMD_CFG_RESP_NOCRC
;
695 if (cmd
->flags
& MMC_RSP_BUSY
)
696 *cmd_cfg
|= CMD_CFG_R1B
;
698 *cmd_cfg
|= CMD_CFG_NO_RESP
;
702 static void meson_mmc_desc_chain_transfer(struct mmc_host
*mmc
, u32 cmd_cfg
)
704 struct meson_host
*host
= mmc_priv(mmc
);
705 struct sd_emmc_desc
*desc
= host
->descs
;
706 struct mmc_data
*data
= host
->cmd
->data
;
707 struct scatterlist
*sg
;
711 if (data
->flags
& MMC_DATA_WRITE
)
712 cmd_cfg
|= CMD_CFG_DATA_WR
;
714 if (data
->blocks
> 1) {
715 cmd_cfg
|= CMD_CFG_BLOCK_MODE
;
716 meson_mmc_set_blksz(mmc
, data
->blksz
);
719 for_each_sg(data
->sg
, sg
, data
->sg_count
, i
) {
720 unsigned int len
= sg_dma_len(sg
);
722 if (data
->blocks
> 1)
725 desc
[i
].cmd_cfg
= cmd_cfg
;
726 desc
[i
].cmd_cfg
|= FIELD_PREP(CMD_CFG_LENGTH_MASK
, len
);
728 desc
[i
].cmd_cfg
|= CMD_CFG_NO_CMD
;
729 desc
[i
].cmd_arg
= host
->cmd
->arg
;
730 desc
[i
].cmd_resp
= 0;
731 desc
[i
].cmd_data
= sg_dma_address(sg
);
733 desc
[data
->sg_count
- 1].cmd_cfg
|= CMD_CFG_END_OF_CHAIN
;
735 dma_wmb(); /* ensure descriptor is written before kicked */
736 start
= host
->descs_dma_addr
| START_DESC_BUSY
;
737 writel(start
, host
->regs
+ SD_EMMC_START
);
740 static void meson_mmc_start_cmd(struct mmc_host
*mmc
, struct mmc_command
*cmd
)
742 struct meson_host
*host
= mmc_priv(mmc
);
743 struct mmc_data
*data
= cmd
->data
;
744 u32 cmd_cfg
= 0, cmd_data
= 0;
745 unsigned int xfer_bytes
= 0;
747 /* Setup descriptors */
752 cmd_cfg
|= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK
, cmd
->opcode
);
753 cmd_cfg
|= CMD_CFG_OWNER
; /* owned by CPU */
754 cmd_cfg
|= CMD_CFG_ERROR
; /* stop in case of error */
756 meson_mmc_set_response_bits(cmd
, &cmd_cfg
);
760 data
->bytes_xfered
= 0;
761 cmd_cfg
|= CMD_CFG_DATA_IO
;
762 cmd_cfg
|= FIELD_PREP(CMD_CFG_TIMEOUT_MASK
,
763 ilog2(meson_mmc_get_timeout_msecs(data
)));
765 if (meson_mmc_desc_chain_mode(data
)) {
766 meson_mmc_desc_chain_transfer(mmc
, cmd_cfg
);
770 if (data
->blocks
> 1) {
771 cmd_cfg
|= CMD_CFG_BLOCK_MODE
;
772 cmd_cfg
|= FIELD_PREP(CMD_CFG_LENGTH_MASK
,
774 meson_mmc_set_blksz(mmc
, data
->blksz
);
776 cmd_cfg
|= FIELD_PREP(CMD_CFG_LENGTH_MASK
, data
->blksz
);
779 xfer_bytes
= data
->blksz
* data
->blocks
;
780 if (data
->flags
& MMC_DATA_WRITE
) {
781 cmd_cfg
|= CMD_CFG_DATA_WR
;
782 WARN_ON(xfer_bytes
> host
->bounce_buf_size
);
783 sg_copy_to_buffer(data
->sg
, data
->sg_len
,
784 host
->bounce_buf
, xfer_bytes
);
788 cmd_data
= host
->bounce_dma_addr
& CMD_DATA_MASK
;
790 cmd_cfg
|= FIELD_PREP(CMD_CFG_TIMEOUT_MASK
,
791 ilog2(SD_EMMC_CMD_TIMEOUT
));
794 /* Last descriptor */
795 cmd_cfg
|= CMD_CFG_END_OF_CHAIN
;
796 writel(cmd_cfg
, host
->regs
+ SD_EMMC_CMD_CFG
);
797 writel(cmd_data
, host
->regs
+ SD_EMMC_CMD_DAT
);
798 writel(0, host
->regs
+ SD_EMMC_CMD_RSP
);
799 wmb(); /* ensure descriptor is written before kicked */
800 writel(cmd
->arg
, host
->regs
+ SD_EMMC_CMD_ARG
);
803 static void meson_mmc_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
805 struct meson_host
*host
= mmc_priv(mmc
);
806 bool needs_pre_post_req
= mrq
->data
&&
807 !(mrq
->data
->host_cookie
& SD_EMMC_PRE_REQ_DONE
);
809 if (needs_pre_post_req
) {
810 meson_mmc_get_transfer_mode(mmc
, mrq
);
811 if (!meson_mmc_desc_chain_mode(mrq
->data
))
812 needs_pre_post_req
= false;
815 if (needs_pre_post_req
)
816 meson_mmc_pre_req(mmc
, mrq
);
819 writel(0, host
->regs
+ SD_EMMC_START
);
821 meson_mmc_start_cmd(mmc
, mrq
->sbc
?: mrq
->cmd
);
823 if (needs_pre_post_req
)
824 meson_mmc_post_req(mmc
, mrq
, 0);
827 static void meson_mmc_read_resp(struct mmc_host
*mmc
, struct mmc_command
*cmd
)
829 struct meson_host
*host
= mmc_priv(mmc
);
831 if (cmd
->flags
& MMC_RSP_136
) {
832 cmd
->resp
[0] = readl(host
->regs
+ SD_EMMC_CMD_RSP3
);
833 cmd
->resp
[1] = readl(host
->regs
+ SD_EMMC_CMD_RSP2
);
834 cmd
->resp
[2] = readl(host
->regs
+ SD_EMMC_CMD_RSP1
);
835 cmd
->resp
[3] = readl(host
->regs
+ SD_EMMC_CMD_RSP
);
836 } else if (cmd
->flags
& MMC_RSP_PRESENT
) {
837 cmd
->resp
[0] = readl(host
->regs
+ SD_EMMC_CMD_RSP
);
841 static irqreturn_t
meson_mmc_irq(int irq
, void *dev_id
)
843 struct meson_host
*host
= dev_id
;
844 struct mmc_command
*cmd
;
845 struct mmc_data
*data
;
846 u32 irq_en
, status
, raw_status
;
847 irqreturn_t ret
= IRQ_NONE
;
849 irq_en
= readl(host
->regs
+ SD_EMMC_IRQ_EN
);
850 raw_status
= readl(host
->regs
+ SD_EMMC_STATUS
);
851 status
= raw_status
& irq_en
;
855 "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n",
860 if (WARN_ON(!host
) || WARN_ON(!host
->cmd
))
863 /* ack all raised interrupts */
864 writel(status
, host
->regs
+ SD_EMMC_STATUS
);
869 if (status
& IRQ_CRC_ERR
) {
870 dev_dbg(host
->dev
, "CRC Error - status 0x%08x\n", status
);
871 cmd
->error
= -EILSEQ
;
872 ret
= IRQ_WAKE_THREAD
;
876 if (status
& IRQ_TIMEOUTS
) {
877 dev_dbg(host
->dev
, "Timeout - status 0x%08x\n", status
);
878 cmd
->error
= -ETIMEDOUT
;
879 ret
= IRQ_WAKE_THREAD
;
883 meson_mmc_read_resp(host
->mmc
, cmd
);
885 if (status
& IRQ_SDIO
) {
886 dev_dbg(host
->dev
, "IRQ: SDIO TODO.\n");
890 if (status
& (IRQ_END_OF_CHAIN
| IRQ_RESP_STATUS
)) {
891 if (data
&& !cmd
->error
)
892 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
893 if (meson_mmc_bounce_buf_read(data
) ||
894 meson_mmc_get_next_command(cmd
))
895 ret
= IRQ_WAKE_THREAD
;
902 /* Stop desc in case of errors */
903 u32 start
= readl(host
->regs
+ SD_EMMC_START
);
905 start
&= ~START_DESC_BUSY
;
906 writel(start
, host
->regs
+ SD_EMMC_START
);
909 if (ret
== IRQ_HANDLED
)
910 meson_mmc_request_done(host
->mmc
, cmd
->mrq
);
915 static int meson_mmc_wait_desc_stop(struct meson_host
*host
)
920 * It may sometimes take a while for it to actually halt. Here, we
921 * are giving it 5ms to comply
923 * If we don't confirm the descriptor is stopped, it might raise new
924 * IRQs after we have called mmc_request_done() which is bad.
927 return readl_poll_timeout(host
->regs
+ SD_EMMC_STATUS
, status
,
928 !(status
& (STATUS_BUSY
| STATUS_DESC_BUSY
)),
932 static irqreturn_t
meson_mmc_irq_thread(int irq
, void *dev_id
)
934 struct meson_host
*host
= dev_id
;
935 struct mmc_command
*next_cmd
, *cmd
= host
->cmd
;
936 struct mmc_data
*data
;
937 unsigned int xfer_bytes
;
943 meson_mmc_wait_desc_stop(host
);
944 meson_mmc_request_done(host
->mmc
, cmd
->mrq
);
950 if (meson_mmc_bounce_buf_read(data
)) {
951 xfer_bytes
= data
->blksz
* data
->blocks
;
952 WARN_ON(xfer_bytes
> host
->bounce_buf_size
);
953 sg_copy_from_buffer(data
->sg
, data
->sg_len
,
954 host
->bounce_buf
, xfer_bytes
);
957 next_cmd
= meson_mmc_get_next_command(cmd
);
959 meson_mmc_start_cmd(host
->mmc
, next_cmd
);
961 meson_mmc_request_done(host
->mmc
, cmd
->mrq
);
967 * NOTE: we only need this until the GPIO/pinctrl driver can handle
968 * interrupts. For now, the MMC core will use this for polling.
970 static int meson_mmc_get_cd(struct mmc_host
*mmc
)
972 int status
= mmc_gpio_get_cd(mmc
);
974 if (status
== -ENOSYS
)
975 return 1; /* assume present */
980 static void meson_mmc_cfg_init(struct meson_host
*host
)
984 cfg
|= FIELD_PREP(CFG_RESP_TIMEOUT_MASK
,
985 ilog2(SD_EMMC_CFG_RESP_TIMEOUT
));
986 cfg
|= FIELD_PREP(CFG_RC_CC_MASK
, ilog2(SD_EMMC_CFG_CMD_GAP
));
987 cfg
|= FIELD_PREP(CFG_BLK_LEN_MASK
, ilog2(SD_EMMC_CFG_BLK_SIZE
));
989 /* abort chain on R/W errors */
990 cfg
|= CFG_ERR_ABORT
;
992 writel(cfg
, host
->regs
+ SD_EMMC_CFG
);
995 static int meson_mmc_card_busy(struct mmc_host
*mmc
)
997 struct meson_host
*host
= mmc_priv(mmc
);
1000 regval
= readl(host
->regs
+ SD_EMMC_STATUS
);
1002 /* We are only interrested in lines 0 to 3, so mask the other ones */
1003 return !(FIELD_GET(STATUS_DATI
, regval
) & 0xf);
1006 static int meson_mmc_voltage_switch(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
1008 /* vqmmc regulator is available */
1009 if (!IS_ERR(mmc
->supply
.vqmmc
)) {
1011 * The usual amlogic setup uses a GPIO to switch from one
1012 * regulator to the other. While the voltage ramp up is
1013 * pretty fast, care must be taken when switching from 3.3v
1014 * to 1.8v. Please make sure the regulator framework is aware
1015 * of your own regulator constraints
1017 return mmc_regulator_set_vqmmc(mmc
, ios
);
1020 /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
1021 if (ios
->signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1027 static const struct mmc_host_ops meson_mmc_ops
= {
1028 .request
= meson_mmc_request
,
1029 .set_ios
= meson_mmc_set_ios
,
1030 .get_cd
= meson_mmc_get_cd
,
1031 .pre_req
= meson_mmc_pre_req
,
1032 .post_req
= meson_mmc_post_req
,
1033 .execute_tuning
= meson_mmc_resampling_tuning
,
1034 .card_busy
= meson_mmc_card_busy
,
1035 .start_signal_voltage_switch
= meson_mmc_voltage_switch
,
1038 static int meson_mmc_probe(struct platform_device
*pdev
)
1040 struct resource
*res
;
1041 struct meson_host
*host
;
1042 struct mmc_host
*mmc
;
1045 mmc
= mmc_alloc_host(sizeof(struct meson_host
), &pdev
->dev
);
1048 host
= mmc_priv(mmc
);
1050 host
->dev
= &pdev
->dev
;
1051 dev_set_drvdata(&pdev
->dev
, host
);
1053 /* The G12A SDIO Controller needs an SRAM bounce buffer */
1054 host
->dram_access_quirk
= device_property_read_bool(&pdev
->dev
,
1055 "amlogic,dram-access-quirk");
1057 /* Get regulators and the supported OCR mask */
1058 host
->vqmmc_enabled
= false;
1059 ret
= mmc_regulator_get_supply(mmc
);
1063 ret
= mmc_of_parse(mmc
);
1065 if (ret
!= -EPROBE_DEFER
)
1066 dev_warn(&pdev
->dev
, "error parsing DT: %d\n", ret
);
1070 host
->data
= (struct meson_mmc_data
*)
1071 of_device_get_match_data(&pdev
->dev
);
1077 ret
= device_reset_optional(&pdev
->dev
);
1079 if (ret
!= -EPROBE_DEFER
)
1080 dev_err(&pdev
->dev
, "device reset failed: %d\n", ret
);
1085 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1086 host
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
1087 if (IS_ERR(host
->regs
)) {
1088 ret
= PTR_ERR(host
->regs
);
1092 host
->irq
= platform_get_irq(pdev
, 0);
1093 if (host
->irq
<= 0) {
1094 dev_err(&pdev
->dev
, "failed to get interrupt resource.\n");
1099 host
->pinctrl
= devm_pinctrl_get(&pdev
->dev
);
1100 if (IS_ERR(host
->pinctrl
)) {
1101 ret
= PTR_ERR(host
->pinctrl
);
1105 host
->pins_default
= pinctrl_lookup_state(host
->pinctrl
,
1106 PINCTRL_STATE_DEFAULT
);
1107 if (IS_ERR(host
->pins_default
)) {
1108 ret
= PTR_ERR(host
->pins_default
);
1112 host
->pins_clk_gate
= pinctrl_lookup_state(host
->pinctrl
,
1114 if (IS_ERR(host
->pins_clk_gate
)) {
1115 dev_warn(&pdev
->dev
,
1116 "can't get clk-gate pinctrl, using clk_stop bit\n");
1117 host
->pins_clk_gate
= NULL
;
1120 host
->core_clk
= devm_clk_get(&pdev
->dev
, "core");
1121 if (IS_ERR(host
->core_clk
)) {
1122 ret
= PTR_ERR(host
->core_clk
);
1126 ret
= clk_prepare_enable(host
->core_clk
);
1130 ret
= meson_mmc_clk_init(host
);
1134 /* set config to sane default */
1135 meson_mmc_cfg_init(host
);
1137 /* Stop execution */
1138 writel(0, host
->regs
+ SD_EMMC_START
);
1140 /* clear, ack and enable interrupts */
1141 writel(0, host
->regs
+ SD_EMMC_IRQ_EN
);
1142 writel(IRQ_CRC_ERR
| IRQ_TIMEOUTS
| IRQ_END_OF_CHAIN
,
1143 host
->regs
+ SD_EMMC_STATUS
);
1144 writel(IRQ_CRC_ERR
| IRQ_TIMEOUTS
| IRQ_END_OF_CHAIN
,
1145 host
->regs
+ SD_EMMC_IRQ_EN
);
1147 ret
= request_threaded_irq(host
->irq
, meson_mmc_irq
,
1148 meson_mmc_irq_thread
, IRQF_ONESHOT
,
1149 dev_name(&pdev
->dev
), host
);
1153 mmc
->caps
|= MMC_CAP_CMD23
;
1154 if (host
->dram_access_quirk
) {
1155 /* Limit to the available sram memory */
1156 mmc
->max_segs
= SD_EMMC_SRAM_DATA_BUF_LEN
/ mmc
->max_blk_size
;
1157 mmc
->max_blk_count
= mmc
->max_segs
;
1159 mmc
->max_blk_count
= CMD_CFG_LENGTH_MASK
;
1160 mmc
->max_segs
= SD_EMMC_DESC_BUF_LEN
/
1161 sizeof(struct sd_emmc_desc
);
1163 mmc
->max_req_size
= mmc
->max_blk_count
* mmc
->max_blk_size
;
1164 mmc
->max_seg_size
= mmc
->max_req_size
;
1167 * At the moment, we don't know how to reliably enable HS400.
1168 * From the different datasheets, it is not even clear if this mode
1169 * is officially supported by any of the SoCs
1171 mmc
->caps2
&= ~MMC_CAP2_HS400
;
1173 if (host
->dram_access_quirk
) {
1175 * The MMC Controller embeds 1,5KiB of internal SRAM
1176 * that can be used to be used as bounce buffer.
1177 * In the case of the G12A SDIO controller, use these
1178 * instead of the DDR memory
1180 host
->bounce_buf_size
= SD_EMMC_SRAM_DATA_BUF_LEN
;
1181 host
->bounce_buf
= host
->regs
+ SD_EMMC_SRAM_DATA_BUF_OFF
;
1182 host
->bounce_dma_addr
= res
->start
+ SD_EMMC_SRAM_DATA_BUF_OFF
;
1184 /* data bounce buffer */
1185 host
->bounce_buf_size
= mmc
->max_req_size
;
1187 dma_alloc_coherent(host
->dev
, host
->bounce_buf_size
,
1188 &host
->bounce_dma_addr
, GFP_KERNEL
);
1189 if (host
->bounce_buf
== NULL
) {
1190 dev_err(host
->dev
, "Unable to map allocate DMA bounce buffer.\n");
1196 host
->descs
= dma_alloc_coherent(host
->dev
, SD_EMMC_DESC_BUF_LEN
,
1197 &host
->descs_dma_addr
, GFP_KERNEL
);
1199 dev_err(host
->dev
, "Allocating descriptor DMA buffer failed\n");
1201 goto err_bounce_buf
;
1204 mmc
->ops
= &meson_mmc_ops
;
1210 if (!host
->dram_access_quirk
)
1211 dma_free_coherent(host
->dev
, host
->bounce_buf_size
,
1212 host
->bounce_buf
, host
->bounce_dma_addr
);
1214 free_irq(host
->irq
, host
);
1216 clk_disable_unprepare(host
->mmc_clk
);
1218 clk_disable_unprepare(host
->core_clk
);
1224 static int meson_mmc_remove(struct platform_device
*pdev
)
1226 struct meson_host
*host
= dev_get_drvdata(&pdev
->dev
);
1228 mmc_remove_host(host
->mmc
);
1230 /* disable interrupts */
1231 writel(0, host
->regs
+ SD_EMMC_IRQ_EN
);
1232 free_irq(host
->irq
, host
);
1234 dma_free_coherent(host
->dev
, SD_EMMC_DESC_BUF_LEN
,
1235 host
->descs
, host
->descs_dma_addr
);
1237 if (!host
->dram_access_quirk
)
1238 dma_free_coherent(host
->dev
, host
->bounce_buf_size
,
1239 host
->bounce_buf
, host
->bounce_dma_addr
);
1241 clk_disable_unprepare(host
->mmc_clk
);
1242 clk_disable_unprepare(host
->core_clk
);
1244 mmc_free_host(host
->mmc
);
1248 static const struct meson_mmc_data meson_gx_data
= {
1249 .tx_delay_mask
= CLK_V2_TX_DELAY_MASK
,
1250 .rx_delay_mask
= CLK_V2_RX_DELAY_MASK
,
1251 .always_on
= CLK_V2_ALWAYS_ON
,
1252 .adjust
= SD_EMMC_ADJUST
,
1255 static const struct meson_mmc_data meson_axg_data
= {
1256 .tx_delay_mask
= CLK_V3_TX_DELAY_MASK
,
1257 .rx_delay_mask
= CLK_V3_RX_DELAY_MASK
,
1258 .always_on
= CLK_V3_ALWAYS_ON
,
1259 .adjust
= SD_EMMC_V3_ADJUST
,
1262 static const struct of_device_id meson_mmc_of_match
[] = {
1263 { .compatible
= "amlogic,meson-gx-mmc", .data
= &meson_gx_data
},
1264 { .compatible
= "amlogic,meson-gxbb-mmc", .data
= &meson_gx_data
},
1265 { .compatible
= "amlogic,meson-gxl-mmc", .data
= &meson_gx_data
},
1266 { .compatible
= "amlogic,meson-gxm-mmc", .data
= &meson_gx_data
},
1267 { .compatible
= "amlogic,meson-axg-mmc", .data
= &meson_axg_data
},
1270 MODULE_DEVICE_TABLE(of
, meson_mmc_of_match
);
1272 static struct platform_driver meson_mmc_driver
= {
1273 .probe
= meson_mmc_probe
,
1274 .remove
= meson_mmc_remove
,
1276 .name
= DRIVER_NAME
,
1277 .of_match_table
= of_match_ptr(meson_mmc_of_match
),
1281 module_platform_driver(meson_mmc_driver
);
1283 MODULE_DESCRIPTION("Amlogic S905*/GX*/AXG SD/eMMC driver");
1284 MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
1285 MODULE_LICENSE("GPL v2");