b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Amlogic SD/eMMC driver for the GX/S905 family SoCs |
| 4 | * |
| 5 | * Copyright (c) 2016 BayLibre, SAS. |
| 6 | * Author: Kevin Hilman <khilman@baylibre.com> |
| 7 | */ |
| 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> |
| 22 | #include <linux/io.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> |
| 30 | |
| 31 | #define DRIVER_NAME "meson-gx-mmc" |
| 32 | |
| 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) |
| 39 | #define CLK_PHASE_0 0 |
| 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) |
| 44 | |
| 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) |
| 48 | |
| 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) |
| 52 | |
| 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) |
| 58 | |
| 59 | #define SD_EMMC_DELAY1 0x4 |
| 60 | #define SD_EMMC_DELAY2 0x8 |
| 61 | #define SD_EMMC_V3_ADJUST 0xc |
| 62 | |
| 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) |
| 68 | |
| 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) |
| 83 | |
| 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) |
| 88 | |
| 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) |
| 94 | #define IRQ_CRC_ERR \ |
| 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 |\ |
| 105 | IRQ_SDIO) |
| 106 | |
| 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 |
| 114 | |
| 115 | #define SD_EMMC_RXD 0x94 |
| 116 | #define SD_EMMC_TXD 0x94 |
| 117 | #define SD_EMMC_LAST_REG SD_EMMC_TXD |
| 118 | |
| 119 | #define SD_EMMC_SRAM_DATA_BUF_LEN 1536 |
| 120 | #define SD_EMMC_SRAM_DATA_BUF_OFF 0x200 |
| 121 | |
| 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 |
| 128 | |
| 129 | #define SD_EMMC_PRE_REQ_DONE BIT(0) |
| 130 | #define SD_EMMC_DESC_CHAIN_MODE BIT(1) |
| 131 | |
| 132 | #define MUX_CLK_NUM_PARENTS 2 |
| 133 | |
| 134 | struct meson_mmc_data { |
| 135 | unsigned int tx_delay_mask; |
| 136 | unsigned int rx_delay_mask; |
| 137 | unsigned int always_on; |
| 138 | unsigned int adjust; |
| 139 | }; |
| 140 | |
| 141 | struct sd_emmc_desc { |
| 142 | u32 cmd_cfg; |
| 143 | u32 cmd_arg; |
| 144 | u32 cmd_data; |
| 145 | u32 cmd_resp; |
| 146 | }; |
| 147 | |
| 148 | struct meson_host { |
| 149 | struct device *dev; |
| 150 | struct meson_mmc_data *data; |
| 151 | struct mmc_host *mmc; |
| 152 | struct mmc_command *cmd; |
| 153 | |
| 154 | void __iomem *regs; |
| 155 | struct clk *core_clk; |
| 156 | struct clk *mux_clk; |
| 157 | struct clk *mmc_clk; |
| 158 | unsigned long req_rate; |
| 159 | bool ddr; |
| 160 | |
| 161 | bool dram_access_quirk; |
| 162 | |
| 163 | struct pinctrl *pinctrl; |
| 164 | struct pinctrl_state *pins_default; |
| 165 | struct pinctrl_state *pins_clk_gate; |
| 166 | |
| 167 | unsigned int bounce_buf_size; |
| 168 | void *bounce_buf; |
| 169 | void __iomem *bounce_iomem_buf; |
| 170 | dma_addr_t bounce_dma_addr; |
| 171 | struct sd_emmc_desc *descs; |
| 172 | dma_addr_t descs_dma_addr; |
| 173 | |
| 174 | int irq; |
| 175 | |
| 176 | bool vqmmc_enabled; |
| 177 | bool needs_pre_post_req; |
| 178 | |
| 179 | }; |
| 180 | |
| 181 | #define CMD_CFG_LENGTH_MASK GENMASK(8, 0) |
| 182 | #define CMD_CFG_BLOCK_MODE BIT(9) |
| 183 | #define CMD_CFG_R1B BIT(10) |
| 184 | #define CMD_CFG_END_OF_CHAIN BIT(11) |
| 185 | #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12) |
| 186 | #define CMD_CFG_NO_RESP BIT(16) |
| 187 | #define CMD_CFG_NO_CMD BIT(17) |
| 188 | #define CMD_CFG_DATA_IO BIT(18) |
| 189 | #define CMD_CFG_DATA_WR BIT(19) |
| 190 | #define CMD_CFG_RESP_NOCRC BIT(20) |
| 191 | #define CMD_CFG_RESP_128 BIT(21) |
| 192 | #define CMD_CFG_RESP_NUM BIT(22) |
| 193 | #define CMD_CFG_DATA_NUM BIT(23) |
| 194 | #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24) |
| 195 | #define CMD_CFG_ERROR BIT(30) |
| 196 | #define CMD_CFG_OWNER BIT(31) |
| 197 | |
| 198 | #define CMD_DATA_MASK GENMASK(31, 2) |
| 199 | #define CMD_DATA_BIG_ENDIAN BIT(1) |
| 200 | #define CMD_DATA_SRAM BIT(0) |
| 201 | #define CMD_RESP_MASK GENMASK(31, 1) |
| 202 | #define CMD_RESP_SRAM BIT(0) |
| 203 | |
| 204 | static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data) |
| 205 | { |
| 206 | unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC; |
| 207 | |
| 208 | if (!timeout) |
| 209 | return SD_EMMC_CMD_TIMEOUT_DATA; |
| 210 | |
| 211 | timeout = roundup_pow_of_two(timeout); |
| 212 | |
| 213 | return min(timeout, 32768U); /* max. 2^15 ms */ |
| 214 | } |
| 215 | |
| 216 | static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd) |
| 217 | { |
| 218 | if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error) |
| 219 | return cmd->mrq->cmd; |
| 220 | else if (mmc_op_multi(cmd->opcode) && |
| 221 | (!cmd->mrq->sbc || cmd->error || cmd->data->error)) |
| 222 | return cmd->mrq->stop; |
| 223 | else |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | static void meson_mmc_get_transfer_mode(struct mmc_host *mmc, |
| 228 | struct mmc_request *mrq) |
| 229 | { |
| 230 | struct meson_host *host = mmc_priv(mmc); |
| 231 | struct mmc_data *data = mrq->data; |
| 232 | struct scatterlist *sg; |
| 233 | int i; |
| 234 | bool use_desc_chain_mode = true; |
| 235 | |
| 236 | /* |
| 237 | * When Controller DMA cannot directly access DDR memory, disable |
| 238 | * support for Chain Mode to directly use the internal SRAM using |
| 239 | * the bounce buffer mode. |
| 240 | */ |
| 241 | if (host->dram_access_quirk) |
| 242 | return; |
| 243 | |
| 244 | /* |
| 245 | * Broken SDIO with AP6255-based WiFi on Khadas VIM Pro has been |
| 246 | * reported. For some strange reason this occurs in descriptor |
| 247 | * chain mode only. So let's fall back to bounce buffer mode |
| 248 | * for command SD_IO_RW_EXTENDED. |
| 249 | */ |
| 250 | if (mrq->cmd->opcode == SD_IO_RW_EXTENDED) |
| 251 | return; |
| 252 | |
| 253 | for_each_sg(data->sg, sg, data->sg_len, i) |
| 254 | /* check for 8 byte alignment */ |
| 255 | if (sg->offset & 7) { |
| 256 | WARN_ONCE(1, "unaligned scatterlist buffer\n"); |
| 257 | use_desc_chain_mode = false; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | if (use_desc_chain_mode) |
| 262 | data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE; |
| 263 | } |
| 264 | |
| 265 | static inline bool meson_mmc_desc_chain_mode(const struct mmc_data *data) |
| 266 | { |
| 267 | return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE; |
| 268 | } |
| 269 | |
| 270 | static inline bool meson_mmc_bounce_buf_read(const struct mmc_data *data) |
| 271 | { |
| 272 | return data && data->flags & MMC_DATA_READ && |
| 273 | !meson_mmc_desc_chain_mode(data); |
| 274 | } |
| 275 | |
| 276 | static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) |
| 277 | { |
| 278 | struct mmc_data *data = mrq->data; |
| 279 | |
| 280 | if (!data) |
| 281 | return; |
| 282 | |
| 283 | meson_mmc_get_transfer_mode(mmc, mrq); |
| 284 | data->host_cookie |= SD_EMMC_PRE_REQ_DONE; |
| 285 | |
| 286 | if (!meson_mmc_desc_chain_mode(data)) |
| 287 | return; |
| 288 | |
| 289 | data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, |
| 290 | mmc_get_dma_dir(data)); |
| 291 | if (!data->sg_count) |
| 292 | dev_err(mmc_dev(mmc), "dma_map_sg failed"); |
| 293 | } |
| 294 | |
| 295 | static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 296 | int err) |
| 297 | { |
| 298 | struct mmc_data *data = mrq->data; |
| 299 | |
| 300 | if (data && meson_mmc_desc_chain_mode(data) && data->sg_count) |
| 301 | dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, |
| 302 | mmc_get_dma_dir(data)); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Gating the clock on this controller is tricky. It seems the mmc clock |
| 307 | * is also used by the controller. It may crash during some operation if the |
| 308 | * clock is stopped. The safest thing to do, whenever possible, is to keep |
| 309 | * clock running at stop it at the pad using the pinmux. |
| 310 | */ |
| 311 | static void meson_mmc_clk_gate(struct meson_host *host) |
| 312 | { |
| 313 | u32 cfg; |
| 314 | |
| 315 | if (host->pins_clk_gate) { |
| 316 | pinctrl_select_state(host->pinctrl, host->pins_clk_gate); |
| 317 | } else { |
| 318 | /* |
| 319 | * If the pinmux is not provided - default to the classic and |
| 320 | * unsafe method |
| 321 | */ |
| 322 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 323 | cfg |= CFG_STOP_CLOCK; |
| 324 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static void meson_mmc_clk_ungate(struct meson_host *host) |
| 329 | { |
| 330 | u32 cfg; |
| 331 | |
| 332 | if (host->pins_clk_gate) |
| 333 | pinctrl_select_state(host->pinctrl, host->pins_default); |
| 334 | |
| 335 | /* Make sure the clock is not stopped in the controller */ |
| 336 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 337 | cfg &= ~CFG_STOP_CLOCK; |
| 338 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 339 | } |
| 340 | |
| 341 | static int meson_mmc_clk_set(struct meson_host *host, unsigned long rate, |
| 342 | bool ddr) |
| 343 | { |
| 344 | struct mmc_host *mmc = host->mmc; |
| 345 | int ret; |
| 346 | u32 cfg; |
| 347 | |
| 348 | /* Same request - bail-out */ |
| 349 | if (host->ddr == ddr && host->req_rate == rate) |
| 350 | return 0; |
| 351 | |
| 352 | /* stop clock */ |
| 353 | meson_mmc_clk_gate(host); |
| 354 | host->req_rate = 0; |
| 355 | mmc->actual_clock = 0; |
| 356 | |
| 357 | /* return with clock being stopped */ |
| 358 | if (!rate) |
| 359 | return 0; |
| 360 | |
| 361 | /* Stop the clock during rate change to avoid glitches */ |
| 362 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 363 | cfg |= CFG_STOP_CLOCK; |
| 364 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 365 | |
| 366 | if (ddr) { |
| 367 | /* DDR modes require higher module clock */ |
| 368 | rate <<= 1; |
| 369 | cfg |= CFG_DDR; |
| 370 | } else { |
| 371 | cfg &= ~CFG_DDR; |
| 372 | } |
| 373 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 374 | host->ddr = ddr; |
| 375 | |
| 376 | ret = clk_set_rate(host->mmc_clk, rate); |
| 377 | if (ret) { |
| 378 | dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n", |
| 379 | rate, ret); |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | host->req_rate = rate; |
| 384 | mmc->actual_clock = clk_get_rate(host->mmc_clk); |
| 385 | |
| 386 | /* We should report the real output frequency of the controller */ |
| 387 | if (ddr) { |
| 388 | host->req_rate >>= 1; |
| 389 | mmc->actual_clock >>= 1; |
| 390 | } |
| 391 | |
| 392 | dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock); |
| 393 | if (rate != mmc->actual_clock) |
| 394 | dev_dbg(host->dev, "requested rate was %lu\n", rate); |
| 395 | |
| 396 | /* (re)start clock */ |
| 397 | meson_mmc_clk_ungate(host); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * The SD/eMMC IP block has an internal mux and divider used for |
| 404 | * generating the MMC clock. Use the clock framework to create and |
| 405 | * manage these clocks. |
| 406 | */ |
| 407 | static int meson_mmc_clk_init(struct meson_host *host) |
| 408 | { |
| 409 | struct clk_init_data init; |
| 410 | struct clk_mux *mux; |
| 411 | struct clk_divider *div; |
| 412 | char clk_name[32]; |
| 413 | int i, ret = 0; |
| 414 | const char *mux_parent_names[MUX_CLK_NUM_PARENTS]; |
| 415 | const char *clk_parent[1]; |
| 416 | u32 clk_reg; |
| 417 | |
| 418 | /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */ |
| 419 | clk_reg = CLK_ALWAYS_ON(host); |
| 420 | clk_reg |= CLK_DIV_MASK; |
| 421 | clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, CLK_PHASE_180); |
| 422 | clk_reg |= FIELD_PREP(CLK_TX_PHASE_MASK, CLK_PHASE_0); |
| 423 | clk_reg |= FIELD_PREP(CLK_RX_PHASE_MASK, CLK_PHASE_0); |
| 424 | writel(clk_reg, host->regs + SD_EMMC_CLOCK); |
| 425 | |
| 426 | /* get the mux parents */ |
| 427 | for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) { |
| 428 | struct clk *clk; |
| 429 | char name[16]; |
| 430 | |
| 431 | snprintf(name, sizeof(name), "clkin%d", i); |
| 432 | clk = devm_clk_get(host->dev, name); |
| 433 | if (IS_ERR(clk)) { |
| 434 | if (clk != ERR_PTR(-EPROBE_DEFER)) |
| 435 | dev_err(host->dev, "Missing clock %s\n", name); |
| 436 | return PTR_ERR(clk); |
| 437 | } |
| 438 | |
| 439 | mux_parent_names[i] = __clk_get_name(clk); |
| 440 | } |
| 441 | |
| 442 | /* create the mux */ |
| 443 | mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL); |
| 444 | if (!mux) |
| 445 | return -ENOMEM; |
| 446 | |
| 447 | snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev)); |
| 448 | init.name = clk_name; |
| 449 | init.ops = &clk_mux_ops; |
| 450 | init.flags = 0; |
| 451 | init.parent_names = mux_parent_names; |
| 452 | init.num_parents = MUX_CLK_NUM_PARENTS; |
| 453 | |
| 454 | mux->reg = host->regs + SD_EMMC_CLOCK; |
| 455 | mux->shift = __ffs(CLK_SRC_MASK); |
| 456 | mux->mask = CLK_SRC_MASK >> mux->shift; |
| 457 | mux->hw.init = &init; |
| 458 | |
| 459 | host->mux_clk = devm_clk_register(host->dev, &mux->hw); |
| 460 | if (WARN_ON(IS_ERR(host->mux_clk))) |
| 461 | return PTR_ERR(host->mux_clk); |
| 462 | |
| 463 | /* create the divider */ |
| 464 | div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL); |
| 465 | if (!div) |
| 466 | return -ENOMEM; |
| 467 | |
| 468 | snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev)); |
| 469 | init.name = clk_name; |
| 470 | init.ops = &clk_divider_ops; |
| 471 | init.flags = CLK_SET_RATE_PARENT; |
| 472 | clk_parent[0] = __clk_get_name(host->mux_clk); |
| 473 | init.parent_names = clk_parent; |
| 474 | init.num_parents = 1; |
| 475 | |
| 476 | div->reg = host->regs + SD_EMMC_CLOCK; |
| 477 | div->shift = __ffs(CLK_DIV_MASK); |
| 478 | div->width = __builtin_popcountl(CLK_DIV_MASK); |
| 479 | div->hw.init = &init; |
| 480 | div->flags = CLK_DIVIDER_ONE_BASED; |
| 481 | |
| 482 | host->mmc_clk = devm_clk_register(host->dev, &div->hw); |
| 483 | if (WARN_ON(IS_ERR(host->mmc_clk))) |
| 484 | return PTR_ERR(host->mmc_clk); |
| 485 | |
| 486 | /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */ |
| 487 | host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000); |
| 488 | ret = clk_set_rate(host->mmc_clk, host->mmc->f_min); |
| 489 | if (ret) |
| 490 | return ret; |
| 491 | |
| 492 | return clk_prepare_enable(host->mmc_clk); |
| 493 | } |
| 494 | |
| 495 | static void meson_mmc_disable_resampling(struct meson_host *host) |
| 496 | { |
| 497 | unsigned int val = readl(host->regs + host->data->adjust); |
| 498 | |
| 499 | val &= ~ADJUST_ADJ_EN; |
| 500 | writel(val, host->regs + host->data->adjust); |
| 501 | } |
| 502 | |
| 503 | static void meson_mmc_reset_resampling(struct meson_host *host) |
| 504 | { |
| 505 | unsigned int val; |
| 506 | |
| 507 | meson_mmc_disable_resampling(host); |
| 508 | |
| 509 | val = readl(host->regs + host->data->adjust); |
| 510 | val &= ~ADJUST_ADJ_DELAY_MASK; |
| 511 | writel(val, host->regs + host->data->adjust); |
| 512 | } |
| 513 | |
| 514 | static int meson_mmc_resampling_tuning(struct mmc_host *mmc, u32 opcode) |
| 515 | { |
| 516 | struct meson_host *host = mmc_priv(mmc); |
| 517 | unsigned int val, dly, max_dly, i; |
| 518 | int ret; |
| 519 | |
| 520 | /* Resampling is done using the source clock */ |
| 521 | max_dly = DIV_ROUND_UP(clk_get_rate(host->mux_clk), |
| 522 | clk_get_rate(host->mmc_clk)); |
| 523 | |
| 524 | val = readl(host->regs + host->data->adjust); |
| 525 | val |= ADJUST_ADJ_EN; |
| 526 | writel(val, host->regs + host->data->adjust); |
| 527 | |
| 528 | if (mmc->doing_retune) |
| 529 | dly = FIELD_GET(ADJUST_ADJ_DELAY_MASK, val) + 1; |
| 530 | else |
| 531 | dly = 0; |
| 532 | |
| 533 | for (i = 0; i < max_dly; i++) { |
| 534 | val &= ~ADJUST_ADJ_DELAY_MASK; |
| 535 | val |= FIELD_PREP(ADJUST_ADJ_DELAY_MASK, (dly + i) % max_dly); |
| 536 | writel(val, host->regs + host->data->adjust); |
| 537 | |
| 538 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 539 | if (!ret) { |
| 540 | dev_dbg(mmc_dev(mmc), "resampling delay: %u\n", |
| 541 | (dly + i) % max_dly); |
| 542 | return 0; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | meson_mmc_reset_resampling(host); |
| 547 | return -EIO; |
| 548 | } |
| 549 | |
| 550 | static int meson_mmc_prepare_ios_clock(struct meson_host *host, |
| 551 | struct mmc_ios *ios) |
| 552 | { |
| 553 | bool ddr; |
| 554 | |
| 555 | switch (ios->timing) { |
| 556 | case MMC_TIMING_MMC_DDR52: |
| 557 | case MMC_TIMING_UHS_DDR50: |
| 558 | ddr = true; |
| 559 | break; |
| 560 | |
| 561 | default: |
| 562 | ddr = false; |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | return meson_mmc_clk_set(host, ios->clock, ddr); |
| 567 | } |
| 568 | |
| 569 | static void meson_mmc_check_resampling(struct meson_host *host, |
| 570 | struct mmc_ios *ios) |
| 571 | { |
| 572 | switch (ios->timing) { |
| 573 | case MMC_TIMING_LEGACY: |
| 574 | case MMC_TIMING_MMC_HS: |
| 575 | case MMC_TIMING_SD_HS: |
| 576 | case MMC_TIMING_MMC_DDR52: |
| 577 | meson_mmc_disable_resampling(host); |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 583 | { |
| 584 | struct meson_host *host = mmc_priv(mmc); |
| 585 | u32 bus_width, val; |
| 586 | int err; |
| 587 | |
| 588 | /* |
| 589 | * GPIO regulator, only controls switching between 1v8 and |
| 590 | * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON. |
| 591 | */ |
| 592 | switch (ios->power_mode) { |
| 593 | case MMC_POWER_OFF: |
| 594 | if (!IS_ERR(mmc->supply.vmmc)) |
| 595 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 596 | |
| 597 | if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { |
| 598 | regulator_disable(mmc->supply.vqmmc); |
| 599 | host->vqmmc_enabled = false; |
| 600 | } |
| 601 | |
| 602 | break; |
| 603 | |
| 604 | case MMC_POWER_UP: |
| 605 | if (!IS_ERR(mmc->supply.vmmc)) |
| 606 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); |
| 607 | |
| 608 | break; |
| 609 | |
| 610 | case MMC_POWER_ON: |
| 611 | if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { |
| 612 | int ret = regulator_enable(mmc->supply.vqmmc); |
| 613 | |
| 614 | if (ret < 0) |
| 615 | dev_err(host->dev, |
| 616 | "failed to enable vqmmc regulator\n"); |
| 617 | else |
| 618 | host->vqmmc_enabled = true; |
| 619 | } |
| 620 | |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | /* Bus width */ |
| 625 | switch (ios->bus_width) { |
| 626 | case MMC_BUS_WIDTH_1: |
| 627 | bus_width = CFG_BUS_WIDTH_1; |
| 628 | break; |
| 629 | case MMC_BUS_WIDTH_4: |
| 630 | bus_width = CFG_BUS_WIDTH_4; |
| 631 | break; |
| 632 | case MMC_BUS_WIDTH_8: |
| 633 | bus_width = CFG_BUS_WIDTH_8; |
| 634 | break; |
| 635 | default: |
| 636 | dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n", |
| 637 | ios->bus_width); |
| 638 | bus_width = CFG_BUS_WIDTH_4; |
| 639 | } |
| 640 | |
| 641 | val = readl(host->regs + SD_EMMC_CFG); |
| 642 | val &= ~CFG_BUS_WIDTH_MASK; |
| 643 | val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width); |
| 644 | writel(val, host->regs + SD_EMMC_CFG); |
| 645 | |
| 646 | meson_mmc_check_resampling(host, ios); |
| 647 | err = meson_mmc_prepare_ios_clock(host, ios); |
| 648 | if (err) |
| 649 | dev_err(host->dev, "Failed to set clock: %d\n,", err); |
| 650 | |
| 651 | dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val); |
| 652 | } |
| 653 | |
| 654 | static void meson_mmc_request_done(struct mmc_host *mmc, |
| 655 | struct mmc_request *mrq) |
| 656 | { |
| 657 | struct meson_host *host = mmc_priv(mmc); |
| 658 | |
| 659 | host->cmd = NULL; |
| 660 | if (host->needs_pre_post_req) |
| 661 | meson_mmc_post_req(mmc, mrq, 0); |
| 662 | mmc_request_done(host->mmc, mrq); |
| 663 | } |
| 664 | |
| 665 | static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz) |
| 666 | { |
| 667 | struct meson_host *host = mmc_priv(mmc); |
| 668 | u32 cfg, blksz_old; |
| 669 | |
| 670 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 671 | blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg); |
| 672 | |
| 673 | if (!is_power_of_2(blksz)) |
| 674 | dev_err(host->dev, "blksz %u is not a power of 2\n", blksz); |
| 675 | |
| 676 | blksz = ilog2(blksz); |
| 677 | |
| 678 | /* check if block-size matches, if not update */ |
| 679 | if (blksz == blksz_old) |
| 680 | return; |
| 681 | |
| 682 | dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__, |
| 683 | blksz_old, blksz); |
| 684 | |
| 685 | cfg &= ~CFG_BLK_LEN_MASK; |
| 686 | cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz); |
| 687 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 688 | } |
| 689 | |
| 690 | static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg) |
| 691 | { |
| 692 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 693 | if (cmd->flags & MMC_RSP_136) |
| 694 | *cmd_cfg |= CMD_CFG_RESP_128; |
| 695 | *cmd_cfg |= CMD_CFG_RESP_NUM; |
| 696 | |
| 697 | if (!(cmd->flags & MMC_RSP_CRC)) |
| 698 | *cmd_cfg |= CMD_CFG_RESP_NOCRC; |
| 699 | |
| 700 | if (cmd->flags & MMC_RSP_BUSY) |
| 701 | *cmd_cfg |= CMD_CFG_R1B; |
| 702 | } else { |
| 703 | *cmd_cfg |= CMD_CFG_NO_RESP; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg) |
| 708 | { |
| 709 | struct meson_host *host = mmc_priv(mmc); |
| 710 | struct sd_emmc_desc *desc = host->descs; |
| 711 | struct mmc_data *data = host->cmd->data; |
| 712 | struct scatterlist *sg; |
| 713 | u32 start; |
| 714 | int i; |
| 715 | |
| 716 | if (data->flags & MMC_DATA_WRITE) |
| 717 | cmd_cfg |= CMD_CFG_DATA_WR; |
| 718 | |
| 719 | if (data->blocks > 1) { |
| 720 | cmd_cfg |= CMD_CFG_BLOCK_MODE; |
| 721 | meson_mmc_set_blksz(mmc, data->blksz); |
| 722 | } |
| 723 | |
| 724 | for_each_sg(data->sg, sg, data->sg_count, i) { |
| 725 | unsigned int len = sg_dma_len(sg); |
| 726 | |
| 727 | if (data->blocks > 1) |
| 728 | len /= data->blksz; |
| 729 | |
| 730 | desc[i].cmd_cfg = cmd_cfg; |
| 731 | desc[i].cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, len); |
| 732 | if (i > 0) |
| 733 | desc[i].cmd_cfg |= CMD_CFG_NO_CMD; |
| 734 | desc[i].cmd_arg = host->cmd->arg; |
| 735 | desc[i].cmd_resp = 0; |
| 736 | desc[i].cmd_data = sg_dma_address(sg); |
| 737 | } |
| 738 | desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN; |
| 739 | |
| 740 | dma_wmb(); /* ensure descriptor is written before kicked */ |
| 741 | start = host->descs_dma_addr | START_DESC_BUSY; |
| 742 | writel(start, host->regs + SD_EMMC_START); |
| 743 | } |
| 744 | |
| 745 | /* local sg copy for dram_access_quirk */ |
| 746 | static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data, |
| 747 | size_t buflen, bool to_buffer) |
| 748 | { |
| 749 | unsigned int sg_flags = SG_MITER_ATOMIC; |
| 750 | struct scatterlist *sgl = data->sg; |
| 751 | unsigned int nents = data->sg_len; |
| 752 | struct sg_mapping_iter miter; |
| 753 | unsigned int offset = 0; |
| 754 | |
| 755 | if (to_buffer) |
| 756 | sg_flags |= SG_MITER_FROM_SG; |
| 757 | else |
| 758 | sg_flags |= SG_MITER_TO_SG; |
| 759 | |
| 760 | sg_miter_start(&miter, sgl, nents, sg_flags); |
| 761 | |
| 762 | while ((offset < buflen) && sg_miter_next(&miter)) { |
| 763 | unsigned int buf_offset = 0; |
| 764 | unsigned int len, left; |
| 765 | u32 *buf = miter.addr; |
| 766 | |
| 767 | len = min(miter.length, buflen - offset); |
| 768 | left = len; |
| 769 | |
| 770 | if (to_buffer) { |
| 771 | do { |
| 772 | writel(*buf++, host->bounce_iomem_buf + offset + buf_offset); |
| 773 | |
| 774 | buf_offset += 4; |
| 775 | left -= 4; |
| 776 | } while (left); |
| 777 | } else { |
| 778 | do { |
| 779 | *buf++ = readl(host->bounce_iomem_buf + offset + buf_offset); |
| 780 | |
| 781 | buf_offset += 4; |
| 782 | left -= 4; |
| 783 | } while (left); |
| 784 | } |
| 785 | |
| 786 | offset += len; |
| 787 | } |
| 788 | |
| 789 | sg_miter_stop(&miter); |
| 790 | } |
| 791 | |
| 792 | static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd) |
| 793 | { |
| 794 | struct meson_host *host = mmc_priv(mmc); |
| 795 | struct mmc_data *data = cmd->data; |
| 796 | u32 cmd_cfg = 0, cmd_data = 0; |
| 797 | unsigned int xfer_bytes = 0; |
| 798 | |
| 799 | /* Setup descriptors */ |
| 800 | dma_rmb(); |
| 801 | |
| 802 | host->cmd = cmd; |
| 803 | |
| 804 | cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode); |
| 805 | cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */ |
| 806 | |
| 807 | meson_mmc_set_response_bits(cmd, &cmd_cfg); |
| 808 | |
| 809 | /* data? */ |
| 810 | if (data) { |
| 811 | data->bytes_xfered = 0; |
| 812 | cmd_cfg |= CMD_CFG_DATA_IO; |
| 813 | cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK, |
| 814 | ilog2(meson_mmc_get_timeout_msecs(data))); |
| 815 | |
| 816 | if (meson_mmc_desc_chain_mode(data)) { |
| 817 | meson_mmc_desc_chain_transfer(mmc, cmd_cfg); |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | if (data->blocks > 1) { |
| 822 | cmd_cfg |= CMD_CFG_BLOCK_MODE; |
| 823 | cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, |
| 824 | data->blocks); |
| 825 | meson_mmc_set_blksz(mmc, data->blksz); |
| 826 | } else { |
| 827 | cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz); |
| 828 | } |
| 829 | |
| 830 | xfer_bytes = data->blksz * data->blocks; |
| 831 | if (data->flags & MMC_DATA_WRITE) { |
| 832 | cmd_cfg |= CMD_CFG_DATA_WR; |
| 833 | WARN_ON(xfer_bytes > host->bounce_buf_size); |
| 834 | if (host->dram_access_quirk) |
| 835 | meson_mmc_copy_buffer(host, data, xfer_bytes, true); |
| 836 | else |
| 837 | sg_copy_to_buffer(data->sg, data->sg_len, |
| 838 | host->bounce_buf, xfer_bytes); |
| 839 | dma_wmb(); |
| 840 | } |
| 841 | |
| 842 | cmd_data = host->bounce_dma_addr & CMD_DATA_MASK; |
| 843 | } else { |
| 844 | cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK, |
| 845 | ilog2(SD_EMMC_CMD_TIMEOUT)); |
| 846 | } |
| 847 | |
| 848 | /* Last descriptor */ |
| 849 | cmd_cfg |= CMD_CFG_END_OF_CHAIN; |
| 850 | writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG); |
| 851 | writel(cmd_data, host->regs + SD_EMMC_CMD_DAT); |
| 852 | writel(0, host->regs + SD_EMMC_CMD_RSP); |
| 853 | wmb(); /* ensure descriptor is written before kicked */ |
| 854 | writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG); |
| 855 | } |
| 856 | |
| 857 | static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data) |
| 858 | { |
| 859 | struct scatterlist *sg; |
| 860 | int i; |
| 861 | |
| 862 | /* Reject request if any element offset or size is not 32bit aligned */ |
| 863 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 864 | if (!IS_ALIGNED(sg->offset, sizeof(u32)) || |
| 865 | !IS_ALIGNED(sg->length, sizeof(u32))) { |
| 866 | dev_err(mmc_dev(mmc), "unaligned sg offset %u len %u\n", |
| 867 | data->sg->offset, data->sg->length); |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 876 | { |
| 877 | struct meson_host *host = mmc_priv(mmc); |
| 878 | host->needs_pre_post_req = mrq->data && |
| 879 | !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE); |
| 880 | |
| 881 | /* |
| 882 | * The memory at the end of the controller used as bounce buffer for |
| 883 | * the dram_access_quirk only accepts 32bit read/write access, |
| 884 | * check the aligment and length of the data before starting the request. |
| 885 | */ |
| 886 | if (host->dram_access_quirk && mrq->data) { |
| 887 | mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data); |
| 888 | if (mrq->cmd->error) { |
| 889 | mmc_request_done(mmc, mrq); |
| 890 | return; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | if (host->needs_pre_post_req) { |
| 895 | meson_mmc_get_transfer_mode(mmc, mrq); |
| 896 | if (!meson_mmc_desc_chain_mode(mrq->data)) |
| 897 | host->needs_pre_post_req = false; |
| 898 | } |
| 899 | |
| 900 | if (host->needs_pre_post_req) |
| 901 | meson_mmc_pre_req(mmc, mrq); |
| 902 | |
| 903 | /* Stop execution */ |
| 904 | writel(0, host->regs + SD_EMMC_START); |
| 905 | |
| 906 | meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd); |
| 907 | } |
| 908 | |
| 909 | static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd) |
| 910 | { |
| 911 | struct meson_host *host = mmc_priv(mmc); |
| 912 | |
| 913 | if (cmd->flags & MMC_RSP_136) { |
| 914 | cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3); |
| 915 | cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2); |
| 916 | cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1); |
| 917 | cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP); |
| 918 | } else if (cmd->flags & MMC_RSP_PRESENT) { |
| 919 | cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | static irqreturn_t meson_mmc_irq(int irq, void *dev_id) |
| 924 | { |
| 925 | struct meson_host *host = dev_id; |
| 926 | struct mmc_command *cmd; |
| 927 | struct mmc_data *data; |
| 928 | u32 irq_en, status, raw_status; |
| 929 | irqreturn_t ret = IRQ_NONE; |
| 930 | |
| 931 | irq_en = readl(host->regs + SD_EMMC_IRQ_EN); |
| 932 | raw_status = readl(host->regs + SD_EMMC_STATUS); |
| 933 | status = raw_status & irq_en; |
| 934 | |
| 935 | if (!status) { |
| 936 | dev_dbg(host->dev, |
| 937 | "Unexpected IRQ! irq_en 0x%08x - status 0x%08x\n", |
| 938 | irq_en, raw_status); |
| 939 | return IRQ_NONE; |
| 940 | } |
| 941 | |
| 942 | if (WARN_ON(!host) || WARN_ON(!host->cmd)) |
| 943 | return IRQ_NONE; |
| 944 | |
| 945 | /* ack all raised interrupts */ |
| 946 | writel(status, host->regs + SD_EMMC_STATUS); |
| 947 | |
| 948 | cmd = host->cmd; |
| 949 | data = cmd->data; |
| 950 | cmd->error = 0; |
| 951 | if (status & IRQ_CRC_ERR) { |
| 952 | dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status); |
| 953 | cmd->error = -EILSEQ; |
| 954 | ret = IRQ_WAKE_THREAD; |
| 955 | goto out; |
| 956 | } |
| 957 | |
| 958 | if (status & IRQ_TIMEOUTS) { |
| 959 | dev_dbg(host->dev, "Timeout - status 0x%08x\n", status); |
| 960 | cmd->error = -ETIMEDOUT; |
| 961 | ret = IRQ_WAKE_THREAD; |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | meson_mmc_read_resp(host->mmc, cmd); |
| 966 | |
| 967 | if (status & IRQ_SDIO) { |
| 968 | dev_dbg(host->dev, "IRQ: SDIO TODO.\n"); |
| 969 | ret = IRQ_HANDLED; |
| 970 | } |
| 971 | |
| 972 | if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) { |
| 973 | if (data && !cmd->error) |
| 974 | data->bytes_xfered = data->blksz * data->blocks; |
| 975 | |
| 976 | return IRQ_WAKE_THREAD; |
| 977 | } |
| 978 | |
| 979 | out: |
| 980 | if (cmd->error) { |
| 981 | /* Stop desc in case of errors */ |
| 982 | u32 start = readl(host->regs + SD_EMMC_START); |
| 983 | |
| 984 | start &= ~START_DESC_BUSY; |
| 985 | writel(start, host->regs + SD_EMMC_START); |
| 986 | } |
| 987 | |
| 988 | return ret; |
| 989 | } |
| 990 | |
| 991 | static int meson_mmc_wait_desc_stop(struct meson_host *host) |
| 992 | { |
| 993 | u32 status; |
| 994 | |
| 995 | /* |
| 996 | * It may sometimes take a while for it to actually halt. Here, we |
| 997 | * are giving it 5ms to comply |
| 998 | * |
| 999 | * If we don't confirm the descriptor is stopped, it might raise new |
| 1000 | * IRQs after we have called mmc_request_done() which is bad. |
| 1001 | */ |
| 1002 | |
| 1003 | return readl_poll_timeout(host->regs + SD_EMMC_STATUS, status, |
| 1004 | !(status & (STATUS_BUSY | STATUS_DESC_BUSY)), |
| 1005 | 100, 5000); |
| 1006 | } |
| 1007 | |
| 1008 | static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id) |
| 1009 | { |
| 1010 | struct meson_host *host = dev_id; |
| 1011 | struct mmc_command *next_cmd, *cmd = host->cmd; |
| 1012 | struct mmc_data *data; |
| 1013 | unsigned int xfer_bytes; |
| 1014 | |
| 1015 | if (WARN_ON(!cmd)) |
| 1016 | return IRQ_NONE; |
| 1017 | |
| 1018 | if (cmd->error) { |
| 1019 | meson_mmc_wait_desc_stop(host); |
| 1020 | meson_mmc_request_done(host->mmc, cmd->mrq); |
| 1021 | |
| 1022 | return IRQ_HANDLED; |
| 1023 | } |
| 1024 | |
| 1025 | data = cmd->data; |
| 1026 | if (meson_mmc_bounce_buf_read(data)) { |
| 1027 | xfer_bytes = data->blksz * data->blocks; |
| 1028 | WARN_ON(xfer_bytes > host->bounce_buf_size); |
| 1029 | if (host->dram_access_quirk) |
| 1030 | meson_mmc_copy_buffer(host, data, xfer_bytes, false); |
| 1031 | else |
| 1032 | sg_copy_from_buffer(data->sg, data->sg_len, |
| 1033 | host->bounce_buf, xfer_bytes); |
| 1034 | } |
| 1035 | |
| 1036 | next_cmd = meson_mmc_get_next_command(cmd); |
| 1037 | if (next_cmd) |
| 1038 | meson_mmc_start_cmd(host->mmc, next_cmd); |
| 1039 | else |
| 1040 | meson_mmc_request_done(host->mmc, cmd->mrq); |
| 1041 | |
| 1042 | return IRQ_HANDLED; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * NOTE: we only need this until the GPIO/pinctrl driver can handle |
| 1047 | * interrupts. For now, the MMC core will use this for polling. |
| 1048 | */ |
| 1049 | static int meson_mmc_get_cd(struct mmc_host *mmc) |
| 1050 | { |
| 1051 | int status = mmc_gpio_get_cd(mmc); |
| 1052 | |
| 1053 | if (status == -ENOSYS) |
| 1054 | return 1; /* assume present */ |
| 1055 | |
| 1056 | return status; |
| 1057 | } |
| 1058 | |
| 1059 | static void meson_mmc_cfg_init(struct meson_host *host) |
| 1060 | { |
| 1061 | u32 cfg = 0; |
| 1062 | |
| 1063 | cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK, |
| 1064 | ilog2(SD_EMMC_CFG_RESP_TIMEOUT)); |
| 1065 | cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP)); |
| 1066 | cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE)); |
| 1067 | |
| 1068 | /* abort chain on R/W errors */ |
| 1069 | cfg |= CFG_ERR_ABORT; |
| 1070 | |
| 1071 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 1072 | } |
| 1073 | |
| 1074 | static int meson_mmc_card_busy(struct mmc_host *mmc) |
| 1075 | { |
| 1076 | struct meson_host *host = mmc_priv(mmc); |
| 1077 | u32 regval; |
| 1078 | |
| 1079 | regval = readl(host->regs + SD_EMMC_STATUS); |
| 1080 | |
| 1081 | /* We are only interrested in lines 0 to 3, so mask the other ones */ |
| 1082 | return !(FIELD_GET(STATUS_DATI, regval) & 0xf); |
| 1083 | } |
| 1084 | |
| 1085 | static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1086 | { |
| 1087 | /* vqmmc regulator is available */ |
| 1088 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 1089 | /* |
| 1090 | * The usual amlogic setup uses a GPIO to switch from one |
| 1091 | * regulator to the other. While the voltage ramp up is |
| 1092 | * pretty fast, care must be taken when switching from 3.3v |
| 1093 | * to 1.8v. Please make sure the regulator framework is aware |
| 1094 | * of your own regulator constraints |
| 1095 | */ |
| 1096 | return mmc_regulator_set_vqmmc(mmc, ios); |
| 1097 | } |
| 1098 | |
| 1099 | /* no vqmmc regulator, assume fixed regulator at 3/3.3V */ |
| 1100 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
| 1101 | return 0; |
| 1102 | |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | |
| 1106 | static const struct mmc_host_ops meson_mmc_ops = { |
| 1107 | .request = meson_mmc_request, |
| 1108 | .set_ios = meson_mmc_set_ios, |
| 1109 | .get_cd = meson_mmc_get_cd, |
| 1110 | .pre_req = meson_mmc_pre_req, |
| 1111 | .post_req = meson_mmc_post_req, |
| 1112 | .execute_tuning = meson_mmc_resampling_tuning, |
| 1113 | .card_busy = meson_mmc_card_busy, |
| 1114 | .start_signal_voltage_switch = meson_mmc_voltage_switch, |
| 1115 | }; |
| 1116 | |
| 1117 | static int meson_mmc_probe(struct platform_device *pdev) |
| 1118 | { |
| 1119 | struct resource *res; |
| 1120 | struct meson_host *host; |
| 1121 | struct mmc_host *mmc; |
| 1122 | int ret; |
| 1123 | |
| 1124 | mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev); |
| 1125 | if (!mmc) |
| 1126 | return -ENOMEM; |
| 1127 | host = mmc_priv(mmc); |
| 1128 | host->mmc = mmc; |
| 1129 | host->dev = &pdev->dev; |
| 1130 | dev_set_drvdata(&pdev->dev, host); |
| 1131 | |
| 1132 | /* The G12A SDIO Controller needs an SRAM bounce buffer */ |
| 1133 | host->dram_access_quirk = device_property_read_bool(&pdev->dev, |
| 1134 | "amlogic,dram-access-quirk"); |
| 1135 | |
| 1136 | /* Get regulators and the supported OCR mask */ |
| 1137 | host->vqmmc_enabled = false; |
| 1138 | ret = mmc_regulator_get_supply(mmc); |
| 1139 | if (ret) |
| 1140 | goto free_host; |
| 1141 | |
| 1142 | ret = mmc_of_parse(mmc); |
| 1143 | if (ret) { |
| 1144 | if (ret != -EPROBE_DEFER) |
| 1145 | dev_warn(&pdev->dev, "error parsing DT: %d\n", ret); |
| 1146 | goto free_host; |
| 1147 | } |
| 1148 | |
| 1149 | host->data = (struct meson_mmc_data *) |
| 1150 | of_device_get_match_data(&pdev->dev); |
| 1151 | if (!host->data) { |
| 1152 | ret = -EINVAL; |
| 1153 | goto free_host; |
| 1154 | } |
| 1155 | |
| 1156 | ret = device_reset_optional(&pdev->dev); |
| 1157 | if (ret) { |
| 1158 | if (ret != -EPROBE_DEFER) |
| 1159 | dev_err(&pdev->dev, "device reset failed: %d\n", ret); |
| 1160 | |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1165 | host->regs = devm_ioremap_resource(&pdev->dev, res); |
| 1166 | if (IS_ERR(host->regs)) { |
| 1167 | ret = PTR_ERR(host->regs); |
| 1168 | goto free_host; |
| 1169 | } |
| 1170 | |
| 1171 | host->irq = platform_get_irq(pdev, 0); |
| 1172 | if (host->irq <= 0) { |
| 1173 | ret = -EINVAL; |
| 1174 | goto free_host; |
| 1175 | } |
| 1176 | |
| 1177 | host->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 1178 | if (IS_ERR(host->pinctrl)) { |
| 1179 | ret = PTR_ERR(host->pinctrl); |
| 1180 | goto free_host; |
| 1181 | } |
| 1182 | |
| 1183 | host->pins_default = pinctrl_lookup_state(host->pinctrl, |
| 1184 | PINCTRL_STATE_DEFAULT); |
| 1185 | if (IS_ERR(host->pins_default)) { |
| 1186 | ret = PTR_ERR(host->pins_default); |
| 1187 | goto free_host; |
| 1188 | } |
| 1189 | |
| 1190 | host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl, |
| 1191 | "clk-gate"); |
| 1192 | if (IS_ERR(host->pins_clk_gate)) { |
| 1193 | dev_warn(&pdev->dev, |
| 1194 | "can't get clk-gate pinctrl, using clk_stop bit\n"); |
| 1195 | host->pins_clk_gate = NULL; |
| 1196 | } |
| 1197 | |
| 1198 | host->core_clk = devm_clk_get(&pdev->dev, "core"); |
| 1199 | if (IS_ERR(host->core_clk)) { |
| 1200 | ret = PTR_ERR(host->core_clk); |
| 1201 | goto free_host; |
| 1202 | } |
| 1203 | |
| 1204 | ret = clk_prepare_enable(host->core_clk); |
| 1205 | if (ret) |
| 1206 | goto free_host; |
| 1207 | |
| 1208 | ret = meson_mmc_clk_init(host); |
| 1209 | if (ret) |
| 1210 | goto err_core_clk; |
| 1211 | |
| 1212 | /* set config to sane default */ |
| 1213 | meson_mmc_cfg_init(host); |
| 1214 | |
| 1215 | /* Stop execution */ |
| 1216 | writel(0, host->regs + SD_EMMC_START); |
| 1217 | |
| 1218 | /* clear, ack and enable interrupts */ |
| 1219 | writel(0, host->regs + SD_EMMC_IRQ_EN); |
| 1220 | writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN, |
| 1221 | host->regs + SD_EMMC_STATUS); |
| 1222 | writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN, |
| 1223 | host->regs + SD_EMMC_IRQ_EN); |
| 1224 | |
| 1225 | ret = request_threaded_irq(host->irq, meson_mmc_irq, |
| 1226 | meson_mmc_irq_thread, IRQF_ONESHOT, |
| 1227 | dev_name(&pdev->dev), host); |
| 1228 | if (ret) |
| 1229 | goto err_init_clk; |
| 1230 | |
| 1231 | mmc->caps |= MMC_CAP_CMD23; |
| 1232 | if (host->dram_access_quirk) { |
| 1233 | /* Limit segments to 1 due to low available sram memory */ |
| 1234 | mmc->max_segs = 1; |
| 1235 | /* Limit to the available sram memory */ |
| 1236 | mmc->max_blk_count = SD_EMMC_SRAM_DATA_BUF_LEN / |
| 1237 | mmc->max_blk_size; |
| 1238 | } else { |
| 1239 | mmc->max_blk_count = CMD_CFG_LENGTH_MASK; |
| 1240 | mmc->max_segs = SD_EMMC_DESC_BUF_LEN / |
| 1241 | sizeof(struct sd_emmc_desc); |
| 1242 | } |
| 1243 | mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size; |
| 1244 | mmc->max_seg_size = mmc->max_req_size; |
| 1245 | |
| 1246 | /* |
| 1247 | * At the moment, we don't know how to reliably enable HS400. |
| 1248 | * From the different datasheets, it is not even clear if this mode |
| 1249 | * is officially supported by any of the SoCs |
| 1250 | */ |
| 1251 | mmc->caps2 &= ~MMC_CAP2_HS400; |
| 1252 | |
| 1253 | if (host->dram_access_quirk) { |
| 1254 | /* |
| 1255 | * The MMC Controller embeds 1,5KiB of internal SRAM |
| 1256 | * that can be used to be used as bounce buffer. |
| 1257 | * In the case of the G12A SDIO controller, use these |
| 1258 | * instead of the DDR memory |
| 1259 | */ |
| 1260 | host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN; |
| 1261 | host->bounce_iomem_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF; |
| 1262 | host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF; |
| 1263 | } else { |
| 1264 | /* data bounce buffer */ |
| 1265 | host->bounce_buf_size = mmc->max_req_size; |
| 1266 | host->bounce_buf = |
| 1267 | dma_alloc_coherent(host->dev, host->bounce_buf_size, |
| 1268 | &host->bounce_dma_addr, GFP_KERNEL); |
| 1269 | if (host->bounce_buf == NULL) { |
| 1270 | dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n"); |
| 1271 | ret = -ENOMEM; |
| 1272 | goto err_free_irq; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, |
| 1277 | &host->descs_dma_addr, GFP_KERNEL); |
| 1278 | if (!host->descs) { |
| 1279 | dev_err(host->dev, "Allocating descriptor DMA buffer failed\n"); |
| 1280 | ret = -ENOMEM; |
| 1281 | goto err_bounce_buf; |
| 1282 | } |
| 1283 | |
| 1284 | mmc->ops = &meson_mmc_ops; |
| 1285 | ret = mmc_add_host(mmc); |
| 1286 | if (ret) |
| 1287 | goto err_free_irq; |
| 1288 | |
| 1289 | return 0; |
| 1290 | |
| 1291 | err_bounce_buf: |
| 1292 | if (!host->dram_access_quirk) |
| 1293 | dma_free_coherent(host->dev, host->bounce_buf_size, |
| 1294 | host->bounce_buf, host->bounce_dma_addr); |
| 1295 | err_free_irq: |
| 1296 | free_irq(host->irq, host); |
| 1297 | err_init_clk: |
| 1298 | clk_disable_unprepare(host->mmc_clk); |
| 1299 | err_core_clk: |
| 1300 | clk_disable_unprepare(host->core_clk); |
| 1301 | free_host: |
| 1302 | mmc_free_host(mmc); |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | static int meson_mmc_remove(struct platform_device *pdev) |
| 1307 | { |
| 1308 | struct meson_host *host = dev_get_drvdata(&pdev->dev); |
| 1309 | |
| 1310 | mmc_remove_host(host->mmc); |
| 1311 | |
| 1312 | /* disable interrupts */ |
| 1313 | writel(0, host->regs + SD_EMMC_IRQ_EN); |
| 1314 | free_irq(host->irq, host); |
| 1315 | |
| 1316 | dma_free_coherent(host->dev, SD_EMMC_DESC_BUF_LEN, |
| 1317 | host->descs, host->descs_dma_addr); |
| 1318 | |
| 1319 | if (!host->dram_access_quirk) |
| 1320 | dma_free_coherent(host->dev, host->bounce_buf_size, |
| 1321 | host->bounce_buf, host->bounce_dma_addr); |
| 1322 | |
| 1323 | clk_disable_unprepare(host->mmc_clk); |
| 1324 | clk_disable_unprepare(host->core_clk); |
| 1325 | |
| 1326 | mmc_free_host(host->mmc); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | static const struct meson_mmc_data meson_gx_data = { |
| 1331 | .tx_delay_mask = CLK_V2_TX_DELAY_MASK, |
| 1332 | .rx_delay_mask = CLK_V2_RX_DELAY_MASK, |
| 1333 | .always_on = CLK_V2_ALWAYS_ON, |
| 1334 | .adjust = SD_EMMC_ADJUST, |
| 1335 | }; |
| 1336 | |
| 1337 | static const struct meson_mmc_data meson_axg_data = { |
| 1338 | .tx_delay_mask = CLK_V3_TX_DELAY_MASK, |
| 1339 | .rx_delay_mask = CLK_V3_RX_DELAY_MASK, |
| 1340 | .always_on = CLK_V3_ALWAYS_ON, |
| 1341 | .adjust = SD_EMMC_V3_ADJUST, |
| 1342 | }; |
| 1343 | |
| 1344 | static const struct of_device_id meson_mmc_of_match[] = { |
| 1345 | { .compatible = "amlogic,meson-gx-mmc", .data = &meson_gx_data }, |
| 1346 | { .compatible = "amlogic,meson-gxbb-mmc", .data = &meson_gx_data }, |
| 1347 | { .compatible = "amlogic,meson-gxl-mmc", .data = &meson_gx_data }, |
| 1348 | { .compatible = "amlogic,meson-gxm-mmc", .data = &meson_gx_data }, |
| 1349 | { .compatible = "amlogic,meson-axg-mmc", .data = &meson_axg_data }, |
| 1350 | {} |
| 1351 | }; |
| 1352 | MODULE_DEVICE_TABLE(of, meson_mmc_of_match); |
| 1353 | |
| 1354 | static struct platform_driver meson_mmc_driver = { |
| 1355 | .probe = meson_mmc_probe, |
| 1356 | .remove = meson_mmc_remove, |
| 1357 | .driver = { |
| 1358 | .name = DRIVER_NAME, |
| 1359 | .of_match_table = of_match_ptr(meson_mmc_of_match), |
| 1360 | }, |
| 1361 | }; |
| 1362 | |
| 1363 | module_platform_driver(meson_mmc_driver); |
| 1364 | |
| 1365 | MODULE_DESCRIPTION("Amlogic S905*/GX*/AXG SD/eMMC driver"); |
| 1366 | MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>"); |
| 1367 | MODULE_LICENSE("GPL v2"); |