Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Amlogic SD/eMMC driver for the GX/S905 family SoCs |
| 3 | * |
| 4 | * Copyright (c) 2016 BayLibre, SAS. |
| 5 | * Author: Kevin Hilman <khilman@baylibre.com> |
| 6 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | */ |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/ioport.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | #include <linux/mmc/host.h> |
| 31 | #include <linux/mmc/mmc.h> |
| 32 | #include <linux/mmc/sdio.h> |
| 33 | #include <linux/mmc/slot-gpio.h> |
| 34 | #include <linux/io.h> |
| 35 | #include <linux/clk.h> |
| 36 | #include <linux/clk-provider.h> |
| 37 | #include <linux/regulator/consumer.h> |
Ulf Hansson | b8789ec | 2016-12-30 13:47:23 +0100 | [diff] [blame] | 38 | #include <linux/interrupt.h> |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_NAME "meson-gx-mmc" |
| 41 | |
| 42 | #define SD_EMMC_CLOCK 0x0 |
| 43 | #define CLK_DIV_SHIFT 0 |
| 44 | #define CLK_DIV_WIDTH 6 |
| 45 | #define CLK_DIV_MASK 0x3f |
| 46 | #define CLK_DIV_MAX 63 |
| 47 | #define CLK_SRC_SHIFT 6 |
| 48 | #define CLK_SRC_WIDTH 2 |
| 49 | #define CLK_SRC_MASK 0x3 |
| 50 | #define CLK_SRC_XTAL 0 /* external crystal */ |
| 51 | #define CLK_SRC_XTAL_RATE 24000000 |
| 52 | #define CLK_SRC_PLL 1 /* FCLK_DIV2 */ |
| 53 | #define CLK_SRC_PLL_RATE 1000000000 |
| 54 | #define CLK_PHASE_SHIFT 8 |
| 55 | #define CLK_PHASE_MASK 0x3 |
| 56 | #define CLK_PHASE_0 0 |
| 57 | #define CLK_PHASE_90 1 |
| 58 | #define CLK_PHASE_180 2 |
| 59 | #define CLK_PHASE_270 3 |
| 60 | #define CLK_ALWAYS_ON BIT(24) |
| 61 | |
| 62 | #define SD_EMMC_DElAY 0x4 |
| 63 | #define SD_EMMC_ADJUST 0x8 |
| 64 | #define SD_EMMC_CALOUT 0x10 |
| 65 | #define SD_EMMC_START 0x40 |
| 66 | #define START_DESC_INIT BIT(0) |
| 67 | #define START_DESC_BUSY BIT(1) |
| 68 | #define START_DESC_ADDR_SHIFT 2 |
| 69 | #define START_DESC_ADDR_MASK (~0x3) |
| 70 | |
| 71 | #define SD_EMMC_CFG 0x44 |
| 72 | #define CFG_BUS_WIDTH_SHIFT 0 |
| 73 | #define CFG_BUS_WIDTH_MASK 0x3 |
| 74 | #define CFG_BUS_WIDTH_1 0x0 |
| 75 | #define CFG_BUS_WIDTH_4 0x1 |
| 76 | #define CFG_BUS_WIDTH_8 0x2 |
| 77 | #define CFG_DDR BIT(2) |
| 78 | #define CFG_BLK_LEN_SHIFT 4 |
| 79 | #define CFG_BLK_LEN_MASK 0xf |
| 80 | #define CFG_RESP_TIMEOUT_SHIFT 8 |
| 81 | #define CFG_RESP_TIMEOUT_MASK 0xf |
| 82 | #define CFG_RC_CC_SHIFT 12 |
| 83 | #define CFG_RC_CC_MASK 0xf |
| 84 | #define CFG_STOP_CLOCK BIT(22) |
| 85 | #define CFG_CLK_ALWAYS_ON BIT(18) |
Heiner Kallweit | e21e6fd | 2017-02-07 22:35:59 +0100 | [diff] [blame] | 86 | #define CFG_CHK_DS BIT(20) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 87 | #define CFG_AUTO_CLK BIT(23) |
| 88 | |
| 89 | #define SD_EMMC_STATUS 0x48 |
| 90 | #define STATUS_BUSY BIT(31) |
| 91 | |
| 92 | #define SD_EMMC_IRQ_EN 0x4c |
| 93 | #define IRQ_EN_MASK 0x3fff |
| 94 | #define IRQ_RXD_ERR_SHIFT 0 |
| 95 | #define IRQ_RXD_ERR_MASK 0xff |
| 96 | #define IRQ_TXD_ERR BIT(8) |
| 97 | #define IRQ_DESC_ERR BIT(9) |
| 98 | #define IRQ_RESP_ERR BIT(10) |
| 99 | #define IRQ_RESP_TIMEOUT BIT(11) |
| 100 | #define IRQ_DESC_TIMEOUT BIT(12) |
| 101 | #define IRQ_END_OF_CHAIN BIT(13) |
| 102 | #define IRQ_RESP_STATUS BIT(14) |
| 103 | #define IRQ_SDIO BIT(15) |
| 104 | |
| 105 | #define SD_EMMC_CMD_CFG 0x50 |
| 106 | #define SD_EMMC_CMD_ARG 0x54 |
| 107 | #define SD_EMMC_CMD_DAT 0x58 |
| 108 | #define SD_EMMC_CMD_RSP 0x5c |
| 109 | #define SD_EMMC_CMD_RSP1 0x60 |
| 110 | #define SD_EMMC_CMD_RSP2 0x64 |
| 111 | #define SD_EMMC_CMD_RSP3 0x68 |
| 112 | |
| 113 | #define SD_EMMC_RXD 0x94 |
| 114 | #define SD_EMMC_TXD 0x94 |
| 115 | #define SD_EMMC_LAST_REG SD_EMMC_TXD |
| 116 | |
| 117 | #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */ |
| 118 | #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */ |
Heiner Kallweit | bb11eff | 2017-03-04 13:37:46 +0100 | [diff] [blame] | 119 | #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */ |
| 120 | #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */ |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 121 | #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */ |
| 122 | #define MUX_CLK_NUM_PARENTS 2 |
| 123 | |
| 124 | struct meson_host { |
| 125 | struct device *dev; |
| 126 | struct mmc_host *mmc; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 127 | struct mmc_command *cmd; |
| 128 | |
| 129 | spinlock_t lock; |
| 130 | void __iomem *regs; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 131 | struct clk *core_clk; |
| 132 | struct clk_mux mux; |
| 133 | struct clk *mux_clk; |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 134 | unsigned long current_clock; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 135 | |
| 136 | struct clk_divider cfg_div; |
| 137 | struct clk *cfg_div_clk; |
| 138 | |
| 139 | unsigned int bounce_buf_size; |
| 140 | void *bounce_buf; |
| 141 | dma_addr_t bounce_dma_addr; |
| 142 | |
| 143 | bool vqmmc_enabled; |
| 144 | }; |
| 145 | |
| 146 | struct sd_emmc_desc { |
| 147 | u32 cmd_cfg; |
| 148 | u32 cmd_arg; |
| 149 | u32 cmd_data; |
| 150 | u32 cmd_resp; |
| 151 | }; |
| 152 | #define CMD_CFG_LENGTH_SHIFT 0 |
| 153 | #define CMD_CFG_LENGTH_MASK 0x1ff |
| 154 | #define CMD_CFG_BLOCK_MODE BIT(9) |
| 155 | #define CMD_CFG_R1B BIT(10) |
| 156 | #define CMD_CFG_END_OF_CHAIN BIT(11) |
| 157 | #define CMD_CFG_TIMEOUT_SHIFT 12 |
| 158 | #define CMD_CFG_TIMEOUT_MASK 0xf |
| 159 | #define CMD_CFG_NO_RESP BIT(16) |
| 160 | #define CMD_CFG_NO_CMD BIT(17) |
| 161 | #define CMD_CFG_DATA_IO BIT(18) |
| 162 | #define CMD_CFG_DATA_WR BIT(19) |
| 163 | #define CMD_CFG_RESP_NOCRC BIT(20) |
| 164 | #define CMD_CFG_RESP_128 BIT(21) |
| 165 | #define CMD_CFG_RESP_NUM BIT(22) |
| 166 | #define CMD_CFG_DATA_NUM BIT(23) |
| 167 | #define CMD_CFG_CMD_INDEX_SHIFT 24 |
| 168 | #define CMD_CFG_CMD_INDEX_MASK 0x3f |
| 169 | #define CMD_CFG_ERROR BIT(30) |
| 170 | #define CMD_CFG_OWNER BIT(31) |
| 171 | |
| 172 | #define CMD_DATA_MASK (~0x3) |
| 173 | #define CMD_DATA_BIG_ENDIAN BIT(1) |
| 174 | #define CMD_DATA_SRAM BIT(0) |
| 175 | #define CMD_RESP_MASK (~0x1) |
| 176 | #define CMD_RESP_SRAM BIT(0) |
| 177 | |
| 178 | static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate) |
| 179 | { |
| 180 | struct mmc_host *mmc = host->mmc; |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 181 | int ret; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 182 | u32 cfg; |
| 183 | |
| 184 | if (clk_rate) { |
| 185 | if (WARN_ON(clk_rate > mmc->f_max)) |
| 186 | clk_rate = mmc->f_max; |
| 187 | else if (WARN_ON(clk_rate < mmc->f_min)) |
| 188 | clk_rate = mmc->f_min; |
| 189 | } |
| 190 | |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 191 | if (clk_rate == host->current_clock) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 192 | return 0; |
| 193 | |
| 194 | /* stop clock */ |
| 195 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 196 | if (!(cfg & CFG_STOP_CLOCK)) { |
| 197 | cfg |= CFG_STOP_CLOCK; |
| 198 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 199 | } |
| 200 | |
| 201 | dev_dbg(host->dev, "change clock rate %u -> %lu\n", |
| 202 | mmc->actual_clock, clk_rate); |
| 203 | |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 204 | if (!clk_rate) { |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 205 | mmc->actual_clock = 0; |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 206 | host->current_clock = 0; |
| 207 | /* return with clock being stopped */ |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | ret = clk_set_rate(host->cfg_div_clk, clk_rate); |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 212 | if (ret) { |
| 213 | dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n", |
| 214 | clk_rate, ret); |
| 215 | return ret; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Heiner Kallweit | 5da8688 | 2017-02-07 22:34:32 +0100 | [diff] [blame] | 218 | mmc->actual_clock = clk_get_rate(host->cfg_div_clk); |
| 219 | host->current_clock = clk_rate; |
| 220 | |
| 221 | if (clk_rate != mmc->actual_clock) |
| 222 | dev_dbg(host->dev, |
| 223 | "divider requested rate %lu != actual rate %u\n", |
| 224 | clk_rate, mmc->actual_clock); |
| 225 | |
| 226 | /* (re)start clock */ |
| 227 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 228 | cfg &= ~CFG_STOP_CLOCK; |
| 229 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 230 | |
| 231 | return 0; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * The SD/eMMC IP block has an internal mux and divider used for |
| 236 | * generating the MMC clock. Use the clock framework to create and |
| 237 | * manage these clocks. |
| 238 | */ |
| 239 | static int meson_mmc_clk_init(struct meson_host *host) |
| 240 | { |
| 241 | struct clk_init_data init; |
| 242 | char clk_name[32]; |
| 243 | int i, ret = 0; |
| 244 | const char *mux_parent_names[MUX_CLK_NUM_PARENTS]; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 245 | const char *clk_div_parents[1]; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 246 | u32 clk_reg, cfg; |
| 247 | |
| 248 | /* get the mux parents */ |
| 249 | for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) { |
Heiner Kallweit | e9883ef | 2017-03-04 13:24:09 +0100 | [diff] [blame] | 250 | struct clk *clk; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 251 | char name[16]; |
| 252 | |
| 253 | snprintf(name, sizeof(name), "clkin%d", i); |
Heiner Kallweit | e9883ef | 2017-03-04 13:24:09 +0100 | [diff] [blame] | 254 | clk = devm_clk_get(host->dev, name); |
| 255 | if (IS_ERR(clk)) { |
| 256 | if (clk != ERR_PTR(-EPROBE_DEFER)) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 257 | dev_err(host->dev, "Missing clock %s\n", name); |
Heiner Kallweit | e9883ef | 2017-03-04 13:24:09 +0100 | [diff] [blame] | 258 | return PTR_ERR(clk); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Heiner Kallweit | e9883ef | 2017-03-04 13:24:09 +0100 | [diff] [blame] | 261 | mux_parent_names[i] = __clk_get_name(clk); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 264 | /* create the mux */ |
| 265 | snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev)); |
| 266 | init.name = clk_name; |
| 267 | init.ops = &clk_mux_ops; |
| 268 | init.flags = 0; |
| 269 | init.parent_names = mux_parent_names; |
Heiner Kallweit | 7558c11 | 2017-03-04 13:22:57 +0100 | [diff] [blame] | 270 | init.num_parents = MUX_CLK_NUM_PARENTS; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 271 | |
| 272 | host->mux.reg = host->regs + SD_EMMC_CLOCK; |
| 273 | host->mux.shift = CLK_SRC_SHIFT; |
| 274 | host->mux.mask = CLK_SRC_MASK; |
| 275 | host->mux.flags = 0; |
| 276 | host->mux.table = NULL; |
| 277 | host->mux.hw.init = &init; |
| 278 | |
| 279 | host->mux_clk = devm_clk_register(host->dev, &host->mux.hw); |
| 280 | if (WARN_ON(IS_ERR(host->mux_clk))) |
| 281 | return PTR_ERR(host->mux_clk); |
| 282 | |
| 283 | /* create the divider */ |
| 284 | snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev)); |
Heiner Kallweit | 7b9ebad | 2017-03-04 13:26:24 +0100 | [diff] [blame] | 285 | init.name = clk_name; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 286 | init.ops = &clk_divider_ops; |
| 287 | init.flags = CLK_SET_RATE_PARENT; |
| 288 | clk_div_parents[0] = __clk_get_name(host->mux_clk); |
| 289 | init.parent_names = clk_div_parents; |
| 290 | init.num_parents = ARRAY_SIZE(clk_div_parents); |
| 291 | |
| 292 | host->cfg_div.reg = host->regs + SD_EMMC_CLOCK; |
| 293 | host->cfg_div.shift = CLK_DIV_SHIFT; |
| 294 | host->cfg_div.width = CLK_DIV_WIDTH; |
| 295 | host->cfg_div.hw.init = &init; |
| 296 | host->cfg_div.flags = CLK_DIVIDER_ONE_BASED | |
| 297 | CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO; |
| 298 | |
| 299 | host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw); |
| 300 | if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk))) |
| 301 | return PTR_ERR(host->cfg_div_clk); |
| 302 | |
| 303 | /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */ |
| 304 | clk_reg = 0; |
| 305 | clk_reg |= CLK_PHASE_180 << CLK_PHASE_SHIFT; |
| 306 | clk_reg |= CLK_SRC_XTAL << CLK_SRC_SHIFT; |
| 307 | clk_reg |= CLK_DIV_MAX << CLK_DIV_SHIFT; |
| 308 | clk_reg &= ~CLK_ALWAYS_ON; |
| 309 | writel(clk_reg, host->regs + SD_EMMC_CLOCK); |
| 310 | |
| 311 | /* Ensure clock starts in "auto" mode, not "always on" */ |
| 312 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 313 | cfg &= ~CFG_CLK_ALWAYS_ON; |
| 314 | cfg |= CFG_AUTO_CLK; |
| 315 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 316 | |
| 317 | ret = clk_prepare_enable(host->cfg_div_clk); |
Ulf Hansson | a4c38c8 | 2017-02-08 12:36:20 +0100 | [diff] [blame] | 318 | if (ret) |
| 319 | return ret; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 320 | |
Ulf Hansson | a4c38c8 | 2017-02-08 12:36:20 +0100 | [diff] [blame] | 321 | /* Get the nearest minimum clock to 400KHz */ |
| 322 | host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000); |
| 323 | |
| 324 | ret = meson_mmc_clk_set(host, host->mmc->f_min); |
Heiner Kallweit | cac3a47 | 2017-03-04 13:25:14 +0100 | [diff] [blame] | 325 | if (ret) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 326 | clk_disable_unprepare(host->cfg_div_clk); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 332 | { |
| 333 | struct meson_host *host = mmc_priv(mmc); |
| 334 | u32 bus_width; |
| 335 | u32 val, orig; |
| 336 | |
| 337 | /* |
| 338 | * GPIO regulator, only controls switching between 1v8 and |
| 339 | * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON. |
| 340 | */ |
| 341 | switch (ios->power_mode) { |
| 342 | case MMC_POWER_OFF: |
| 343 | if (!IS_ERR(mmc->supply.vmmc)) |
| 344 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 345 | |
| 346 | if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { |
| 347 | regulator_disable(mmc->supply.vqmmc); |
| 348 | host->vqmmc_enabled = false; |
| 349 | } |
| 350 | |
| 351 | break; |
| 352 | |
| 353 | case MMC_POWER_UP: |
| 354 | if (!IS_ERR(mmc->supply.vmmc)) |
| 355 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); |
| 356 | break; |
| 357 | |
| 358 | case MMC_POWER_ON: |
| 359 | if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { |
| 360 | int ret = regulator_enable(mmc->supply.vqmmc); |
| 361 | |
| 362 | if (ret < 0) |
| 363 | dev_err(mmc_dev(mmc), |
| 364 | "failed to enable vqmmc regulator\n"); |
| 365 | else |
| 366 | host->vqmmc_enabled = true; |
| 367 | } |
| 368 | |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | meson_mmc_clk_set(host, ios->clock); |
| 374 | |
| 375 | /* Bus width */ |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 376 | switch (ios->bus_width) { |
| 377 | case MMC_BUS_WIDTH_1: |
| 378 | bus_width = CFG_BUS_WIDTH_1; |
| 379 | break; |
| 380 | case MMC_BUS_WIDTH_4: |
| 381 | bus_width = CFG_BUS_WIDTH_4; |
| 382 | break; |
| 383 | case MMC_BUS_WIDTH_8: |
| 384 | bus_width = CFG_BUS_WIDTH_8; |
| 385 | break; |
| 386 | default: |
| 387 | dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n", |
| 388 | ios->bus_width); |
| 389 | bus_width = CFG_BUS_WIDTH_4; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | val = readl(host->regs + SD_EMMC_CFG); |
| 393 | orig = val; |
| 394 | |
| 395 | val &= ~(CFG_BUS_WIDTH_MASK << CFG_BUS_WIDTH_SHIFT); |
| 396 | val |= bus_width << CFG_BUS_WIDTH_SHIFT; |
| 397 | |
Heiner Kallweit | e21e6fd | 2017-02-07 22:35:59 +0100 | [diff] [blame] | 398 | val &= ~CFG_DDR; |
| 399 | if (ios->timing == MMC_TIMING_UHS_DDR50 || |
| 400 | ios->timing == MMC_TIMING_MMC_DDR52 || |
| 401 | ios->timing == MMC_TIMING_MMC_HS400) |
| 402 | val |= CFG_DDR; |
| 403 | |
| 404 | val &= ~CFG_CHK_DS; |
| 405 | if (ios->timing == MMC_TIMING_MMC_HS400) |
| 406 | val |= CFG_CHK_DS; |
| 407 | |
Heiner Kallweit | c01d121 | 2017-03-04 13:35:13 +0100 | [diff] [blame] | 408 | if (val != orig) { |
| 409 | writel(val, host->regs + SD_EMMC_CFG); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 410 | dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n", |
| 411 | __func__, orig, val); |
Heiner Kallweit | c01d121 | 2017-03-04 13:35:13 +0100 | [diff] [blame] | 412 | } |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Heiner Kallweit | 3d6c991 | 2017-03-04 13:20:44 +0100 | [diff] [blame] | 415 | static void meson_mmc_request_done(struct mmc_host *mmc, |
| 416 | struct mmc_request *mrq) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 417 | { |
| 418 | struct meson_host *host = mmc_priv(mmc); |
| 419 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 420 | host->cmd = NULL; |
| 421 | mmc_request_done(host->mmc, mrq); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd) |
| 425 | { |
| 426 | struct meson_host *host = mmc_priv(mmc); |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 427 | struct mmc_data *data = cmd->data; |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 428 | u32 cfg, cmd_cfg = 0, cmd_data = 0; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 429 | u8 blk_len, cmd_cfg_timeout; |
| 430 | unsigned int xfer_bytes = 0; |
| 431 | |
| 432 | /* Setup descriptors */ |
| 433 | dma_rmb(); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 434 | |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 435 | cmd_cfg |= (cmd->opcode & CMD_CFG_CMD_INDEX_MASK) << |
| 436 | CMD_CFG_CMD_INDEX_SHIFT; |
| 437 | cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */ |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 438 | |
| 439 | /* Response */ |
| 440 | if (cmd->flags & MMC_RSP_PRESENT) { |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 441 | if (cmd->flags & MMC_RSP_136) |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 442 | cmd_cfg |= CMD_CFG_RESP_128; |
| 443 | cmd_cfg |= CMD_CFG_RESP_NUM; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 444 | |
| 445 | if (!(cmd->flags & MMC_RSP_CRC)) |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 446 | cmd_cfg |= CMD_CFG_RESP_NOCRC; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 447 | |
| 448 | if (cmd->flags & MMC_RSP_BUSY) |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 449 | cmd_cfg |= CMD_CFG_R1B; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 450 | } else { |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 451 | cmd_cfg |= CMD_CFG_NO_RESP; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* data? */ |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 455 | if (data) { |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 456 | cmd_cfg |= CMD_CFG_DATA_IO; |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 457 | if (data->blocks > 1) { |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 458 | cmd_cfg |= CMD_CFG_BLOCK_MODE; |
| 459 | cmd_cfg |= (data->blocks & CMD_CFG_LENGTH_MASK) << |
| 460 | CMD_CFG_LENGTH_SHIFT; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 461 | |
| 462 | /* check if block-size matches, if not update */ |
| 463 | cfg = readl(host->regs + SD_EMMC_CFG); |
| 464 | blk_len = cfg & (CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT); |
| 465 | blk_len >>= CFG_BLK_LEN_SHIFT; |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 466 | if (blk_len != ilog2(data->blksz)) { |
Kevin Hilman | dc01205 | 2017-01-25 16:01:39 -0800 | [diff] [blame] | 467 | dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 468 | __func__, blk_len, |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 469 | ilog2(data->blksz)); |
| 470 | blk_len = ilog2(data->blksz); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 471 | cfg &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT); |
| 472 | cfg |= blk_len << CFG_BLK_LEN_SHIFT; |
| 473 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 474 | } |
| 475 | } else { |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 476 | cmd_cfg |= (data->blksz & CMD_CFG_LENGTH_MASK) << |
| 477 | CMD_CFG_LENGTH_SHIFT; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 480 | data->bytes_xfered = 0; |
| 481 | xfer_bytes = data->blksz * data->blocks; |
| 482 | if (data->flags & MMC_DATA_WRITE) { |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 483 | cmd_cfg |= CMD_CFG_DATA_WR; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 484 | WARN_ON(xfer_bytes > host->bounce_buf_size); |
Heiner Kallweit | 00412dd | 2017-03-22 22:33:44 +0100 | [diff] [blame] | 485 | sg_copy_to_buffer(data->sg, data->sg_len, |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 486 | host->bounce_buf, xfer_bytes); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 487 | dma_wmb(); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 490 | cmd_data = host->bounce_dma_addr & CMD_DATA_MASK; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 491 | |
Heiner Kallweit | bb11eff | 2017-03-04 13:37:46 +0100 | [diff] [blame] | 492 | cmd_cfg_timeout = ilog2(SD_EMMC_CMD_TIMEOUT_DATA); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 493 | } else { |
Heiner Kallweit | bb11eff | 2017-03-04 13:37:46 +0100 | [diff] [blame] | 494 | cmd_cfg_timeout = ilog2(SD_EMMC_CMD_TIMEOUT); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 495 | } |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 496 | cmd_cfg |= (cmd_cfg_timeout & CMD_CFG_TIMEOUT_MASK) << |
| 497 | CMD_CFG_TIMEOUT_SHIFT; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 498 | |
| 499 | host->cmd = cmd; |
| 500 | |
| 501 | /* Last descriptor */ |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 502 | cmd_cfg |= CMD_CFG_END_OF_CHAIN; |
| 503 | writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG); |
| 504 | writel(cmd_data, host->regs + SD_EMMC_CMD_DAT); |
| 505 | writel(0, host->regs + SD_EMMC_CMD_RSP); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 506 | wmb(); /* ensure descriptor is written before kicked */ |
Heiner Kallweit | a322feb | 2017-03-22 22:33:47 +0100 | [diff] [blame] | 507 | writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 511 | { |
| 512 | struct meson_host *host = mmc_priv(mmc); |
| 513 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 514 | /* Stop execution */ |
| 515 | writel(0, host->regs + SD_EMMC_START); |
| 516 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 517 | if (mrq->sbc) |
| 518 | meson_mmc_start_cmd(mmc, mrq->sbc); |
| 519 | else |
| 520 | meson_mmc_start_cmd(mmc, mrq->cmd); |
| 521 | } |
| 522 | |
Heiner Kallweit | 3d6c991 | 2017-03-04 13:20:44 +0100 | [diff] [blame] | 523 | static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 524 | { |
| 525 | struct meson_host *host = mmc_priv(mmc); |
| 526 | |
| 527 | if (cmd->flags & MMC_RSP_136) { |
| 528 | cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3); |
| 529 | cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2); |
| 530 | cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1); |
| 531 | cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP); |
| 532 | } else if (cmd->flags & MMC_RSP_PRESENT) { |
| 533 | cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP); |
| 534 | } |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static irqreturn_t meson_mmc_irq(int irq, void *dev_id) |
| 538 | { |
| 539 | struct meson_host *host = dev_id; |
Heinrich Schuchardt | 19a91dd | 2016-12-23 16:01:08 +0100 | [diff] [blame] | 540 | struct mmc_command *cmd; |
Heiner Kallweit | 2c8d96a | 2017-03-22 22:33:53 +0100 | [diff] [blame^] | 541 | struct mmc_data *data; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 542 | u32 irq_en, status, raw_status; |
| 543 | irqreturn_t ret = IRQ_HANDLED; |
| 544 | |
| 545 | if (WARN_ON(!host)) |
| 546 | return IRQ_NONE; |
| 547 | |
Heinrich Schuchardt | 19a91dd | 2016-12-23 16:01:08 +0100 | [diff] [blame] | 548 | cmd = host->cmd; |
| 549 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 550 | if (WARN_ON(!cmd)) |
| 551 | return IRQ_NONE; |
| 552 | |
Heiner Kallweit | 2c8d96a | 2017-03-22 22:33:53 +0100 | [diff] [blame^] | 553 | data = cmd->data; |
| 554 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 555 | spin_lock(&host->lock); |
| 556 | irq_en = readl(host->regs + SD_EMMC_IRQ_EN); |
| 557 | raw_status = readl(host->regs + SD_EMMC_STATUS); |
| 558 | status = raw_status & irq_en; |
| 559 | |
| 560 | if (!status) { |
| 561 | dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n", |
| 562 | raw_status, irq_en); |
| 563 | ret = IRQ_NONE; |
| 564 | goto out; |
| 565 | } |
| 566 | |
Heiner Kallweit | 1f8066d | 2017-03-22 22:33:50 +0100 | [diff] [blame] | 567 | meson_mmc_read_resp(host->mmc, cmd); |
| 568 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 569 | cmd->error = 0; |
| 570 | if (status & IRQ_RXD_ERR_MASK) { |
| 571 | dev_dbg(host->dev, "Unhandled IRQ: RXD error\n"); |
| 572 | cmd->error = -EILSEQ; |
| 573 | } |
| 574 | if (status & IRQ_TXD_ERR) { |
| 575 | dev_dbg(host->dev, "Unhandled IRQ: TXD error\n"); |
| 576 | cmd->error = -EILSEQ; |
| 577 | } |
| 578 | if (status & IRQ_DESC_ERR) |
| 579 | dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n"); |
| 580 | if (status & IRQ_RESP_ERR) { |
| 581 | dev_dbg(host->dev, "Unhandled IRQ: Response error\n"); |
| 582 | cmd->error = -EILSEQ; |
| 583 | } |
| 584 | if (status & IRQ_RESP_TIMEOUT) { |
| 585 | dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n"); |
| 586 | cmd->error = -ETIMEDOUT; |
| 587 | } |
| 588 | if (status & IRQ_DESC_TIMEOUT) { |
| 589 | dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n"); |
| 590 | cmd->error = -ETIMEDOUT; |
| 591 | } |
| 592 | if (status & IRQ_SDIO) |
| 593 | dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n"); |
| 594 | |
Heiner Kallweit | 2c8d96a | 2017-03-22 22:33:53 +0100 | [diff] [blame^] | 595 | if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) { |
| 596 | if (data && !cmd->error) |
| 597 | data->bytes_xfered = data->blksz * data->blocks; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 598 | ret = IRQ_WAKE_THREAD; |
Heiner Kallweit | 2c8d96a | 2017-03-22 22:33:53 +0100 | [diff] [blame^] | 599 | } else { |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 600 | dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n", |
| 601 | status, cmd->opcode, cmd->arg, |
Heiner Kallweit | 7cdcc48 | 2017-03-04 13:36:45 +0100 | [diff] [blame] | 602 | cmd->flags, cmd->mrq->stop ? 1 : 0); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 603 | if (cmd->data) { |
| 604 | struct mmc_data *data = cmd->data; |
| 605 | |
| 606 | dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)", |
| 607 | data->blksz, data->blocks, data->flags, |
| 608 | data->flags & MMC_DATA_WRITE ? "write" : "", |
| 609 | data->flags & MMC_DATA_READ ? "read" : ""); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | out: |
| 614 | /* ack all (enabled) interrupts */ |
| 615 | writel(status, host->regs + SD_EMMC_STATUS); |
| 616 | |
Heiner Kallweit | 1f8066d | 2017-03-22 22:33:50 +0100 | [diff] [blame] | 617 | if (ret == IRQ_HANDLED) |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 618 | meson_mmc_request_done(host->mmc, cmd->mrq); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 619 | |
| 620 | spin_unlock(&host->lock); |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id) |
| 625 | { |
| 626 | struct meson_host *host = dev_id; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 627 | struct mmc_command *cmd = host->cmd; |
| 628 | struct mmc_data *data; |
| 629 | unsigned int xfer_bytes; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 630 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 631 | if (WARN_ON(!cmd)) |
Heinrich Schuchardt | 19a91dd | 2016-12-23 16:01:08 +0100 | [diff] [blame] | 632 | return IRQ_NONE; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 633 | |
| 634 | data = cmd->data; |
Heiner Kallweit | 690f90b | 2017-02-07 22:34:41 +0100 | [diff] [blame] | 635 | if (data && data->flags & MMC_DATA_READ) { |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 636 | xfer_bytes = data->blksz * data->blocks; |
Heiner Kallweit | 690f90b | 2017-02-07 22:34:41 +0100 | [diff] [blame] | 637 | WARN_ON(xfer_bytes > host->bounce_buf_size); |
| 638 | sg_copy_from_buffer(data->sg, data->sg_len, |
| 639 | host->bounce_buf, xfer_bytes); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Heiner Kallweit | 7cdcc48 | 2017-03-04 13:36:45 +0100 | [diff] [blame] | 642 | if (!data || !data->stop || cmd->mrq->sbc) |
| 643 | meson_mmc_request_done(host->mmc, cmd->mrq); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 644 | else |
| 645 | meson_mmc_start_cmd(host->mmc, data->stop); |
| 646 | |
Heiner Kallweit | 690f90b | 2017-02-07 22:34:41 +0100 | [diff] [blame] | 647 | return IRQ_HANDLED; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | * NOTE: we only need this until the GPIO/pinctrl driver can handle |
| 652 | * interrupts. For now, the MMC core will use this for polling. |
| 653 | */ |
| 654 | static int meson_mmc_get_cd(struct mmc_host *mmc) |
| 655 | { |
| 656 | int status = mmc_gpio_get_cd(mmc); |
| 657 | |
| 658 | if (status == -ENOSYS) |
| 659 | return 1; /* assume present */ |
| 660 | |
| 661 | return status; |
| 662 | } |
| 663 | |
Heiner Kallweit | c01d121 | 2017-03-04 13:35:13 +0100 | [diff] [blame] | 664 | static void meson_mmc_cfg_init(struct meson_host *host) |
| 665 | { |
| 666 | u32 cfg = 0; |
| 667 | |
| 668 | cfg |= ilog2(SD_EMMC_CFG_RESP_TIMEOUT) << CFG_RESP_TIMEOUT_SHIFT; |
| 669 | cfg |= ilog2(SD_EMMC_CFG_CMD_GAP) << CFG_RC_CC_SHIFT; |
| 670 | cfg |= ilog2(SD_EMMC_CFG_BLK_SIZE) << CFG_BLK_LEN_SHIFT; |
| 671 | |
| 672 | writel(cfg, host->regs + SD_EMMC_CFG); |
| 673 | } |
| 674 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 675 | static const struct mmc_host_ops meson_mmc_ops = { |
| 676 | .request = meson_mmc_request, |
| 677 | .set_ios = meson_mmc_set_ios, |
| 678 | .get_cd = meson_mmc_get_cd, |
| 679 | }; |
| 680 | |
| 681 | static int meson_mmc_probe(struct platform_device *pdev) |
| 682 | { |
| 683 | struct resource *res; |
| 684 | struct meson_host *host; |
| 685 | struct mmc_host *mmc; |
Heiner Kallweit | 9a1da4d | 2017-03-04 13:21:54 +0100 | [diff] [blame] | 686 | int ret, irq; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 687 | |
| 688 | mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev); |
| 689 | if (!mmc) |
| 690 | return -ENOMEM; |
| 691 | host = mmc_priv(mmc); |
| 692 | host->mmc = mmc; |
| 693 | host->dev = &pdev->dev; |
| 694 | dev_set_drvdata(&pdev->dev, host); |
| 695 | |
| 696 | spin_lock_init(&host->lock); |
| 697 | |
| 698 | /* Get regulators and the supported OCR mask */ |
| 699 | host->vqmmc_enabled = false; |
| 700 | ret = mmc_regulator_get_supply(mmc); |
| 701 | if (ret == -EPROBE_DEFER) |
| 702 | goto free_host; |
| 703 | |
| 704 | ret = mmc_of_parse(mmc); |
| 705 | if (ret) { |
Kevin Hilman | dc01205 | 2017-01-25 16:01:39 -0800 | [diff] [blame] | 706 | if (ret != -EPROBE_DEFER) |
| 707 | dev_warn(&pdev->dev, "error parsing DT: %d\n", ret); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 708 | goto free_host; |
| 709 | } |
| 710 | |
| 711 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 712 | host->regs = devm_ioremap_resource(&pdev->dev, res); |
| 713 | if (IS_ERR(host->regs)) { |
| 714 | ret = PTR_ERR(host->regs); |
| 715 | goto free_host; |
| 716 | } |
| 717 | |
Heiner Kallweit | 9a1da4d | 2017-03-04 13:21:54 +0100 | [diff] [blame] | 718 | irq = platform_get_irq(pdev, 0); |
| 719 | if (!irq) { |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 720 | dev_err(&pdev->dev, "failed to get interrupt resource.\n"); |
| 721 | ret = -EINVAL; |
| 722 | goto free_host; |
| 723 | } |
| 724 | |
| 725 | host->core_clk = devm_clk_get(&pdev->dev, "core"); |
| 726 | if (IS_ERR(host->core_clk)) { |
| 727 | ret = PTR_ERR(host->core_clk); |
| 728 | goto free_host; |
| 729 | } |
| 730 | |
| 731 | ret = clk_prepare_enable(host->core_clk); |
| 732 | if (ret) |
| 733 | goto free_host; |
| 734 | |
| 735 | ret = meson_mmc_clk_init(host); |
| 736 | if (ret) |
Michał Zegan | ce473d5 | 2017-03-14 21:05:20 +0100 | [diff] [blame] | 737 | goto err_core_clk; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 738 | |
| 739 | /* Stop execution */ |
| 740 | writel(0, host->regs + SD_EMMC_START); |
| 741 | |
| 742 | /* clear, ack, enable all interrupts */ |
| 743 | writel(0, host->regs + SD_EMMC_IRQ_EN); |
| 744 | writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS); |
Heiner Kallweit | 92763b9 | 2017-02-07 22:34:51 +0100 | [diff] [blame] | 745 | writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 746 | |
Heiner Kallweit | c01d121 | 2017-03-04 13:35:13 +0100 | [diff] [blame] | 747 | /* set config to sane default */ |
| 748 | meson_mmc_cfg_init(host); |
| 749 | |
Heiner Kallweit | 9a1da4d | 2017-03-04 13:21:54 +0100 | [diff] [blame] | 750 | ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq, |
| 751 | meson_mmc_irq_thread, IRQF_SHARED, |
| 752 | DRIVER_NAME, host); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 753 | if (ret) |
Heiner Kallweit | cac3a47 | 2017-03-04 13:25:14 +0100 | [diff] [blame] | 754 | goto err_div_clk; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 755 | |
Heiner Kallweit | efe0b66 | 2017-02-07 22:34:58 +0100 | [diff] [blame] | 756 | mmc->max_blk_count = CMD_CFG_LENGTH_MASK; |
| 757 | mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size; |
| 758 | |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 759 | /* data bounce buffer */ |
Heiner Kallweit | 4136fcb | 2017-02-07 22:35:02 +0100 | [diff] [blame] | 760 | host->bounce_buf_size = mmc->max_req_size; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 761 | host->bounce_buf = |
| 762 | dma_alloc_coherent(host->dev, host->bounce_buf_size, |
| 763 | &host->bounce_dma_addr, GFP_KERNEL); |
| 764 | if (host->bounce_buf == NULL) { |
| 765 | dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n"); |
| 766 | ret = -ENOMEM; |
Heiner Kallweit | cac3a47 | 2017-03-04 13:25:14 +0100 | [diff] [blame] | 767 | goto err_div_clk; |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | mmc->ops = &meson_mmc_ops; |
| 771 | mmc_add_host(mmc); |
| 772 | |
| 773 | return 0; |
| 774 | |
Heiner Kallweit | cac3a47 | 2017-03-04 13:25:14 +0100 | [diff] [blame] | 775 | err_div_clk: |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 776 | clk_disable_unprepare(host->cfg_div_clk); |
Michał Zegan | ce473d5 | 2017-03-14 21:05:20 +0100 | [diff] [blame] | 777 | err_core_clk: |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 778 | clk_disable_unprepare(host->core_clk); |
Michał Zegan | ce473d5 | 2017-03-14 21:05:20 +0100 | [diff] [blame] | 779 | free_host: |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 780 | mmc_free_host(mmc); |
| 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | static int meson_mmc_remove(struct platform_device *pdev) |
| 785 | { |
| 786 | struct meson_host *host = dev_get_drvdata(&pdev->dev); |
| 787 | |
Michał Zegan | a01fc2a | 2017-02-18 18:06:47 +0100 | [diff] [blame] | 788 | mmc_remove_host(host->mmc); |
| 789 | |
Heiner Kallweit | 92763b9 | 2017-02-07 22:34:51 +0100 | [diff] [blame] | 790 | /* disable interrupts */ |
| 791 | writel(0, host->regs + SD_EMMC_IRQ_EN); |
| 792 | |
Heiner Kallweit | 62d721a | 2017-02-07 22:35:40 +0100 | [diff] [blame] | 793 | dma_free_coherent(host->dev, host->bounce_buf_size, |
| 794 | host->bounce_buf, host->bounce_dma_addr); |
Kevin Hilman | 51c5d84 | 2016-10-19 11:18:24 -0700 | [diff] [blame] | 795 | |
| 796 | clk_disable_unprepare(host->cfg_div_clk); |
| 797 | clk_disable_unprepare(host->core_clk); |
| 798 | |
| 799 | mmc_free_host(host->mmc); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static const struct of_device_id meson_mmc_of_match[] = { |
| 804 | { .compatible = "amlogic,meson-gx-mmc", }, |
| 805 | { .compatible = "amlogic,meson-gxbb-mmc", }, |
| 806 | { .compatible = "amlogic,meson-gxl-mmc", }, |
| 807 | { .compatible = "amlogic,meson-gxm-mmc", }, |
| 808 | {} |
| 809 | }; |
| 810 | MODULE_DEVICE_TABLE(of, meson_mmc_of_match); |
| 811 | |
| 812 | static struct platform_driver meson_mmc_driver = { |
| 813 | .probe = meson_mmc_probe, |
| 814 | .remove = meson_mmc_remove, |
| 815 | .driver = { |
| 816 | .name = DRIVER_NAME, |
| 817 | .of_match_table = of_match_ptr(meson_mmc_of_match), |
| 818 | }, |
| 819 | }; |
| 820 | |
| 821 | module_platform_driver(meson_mmc_driver); |
| 822 | |
| 823 | MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver"); |
| 824 | MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>"); |
| 825 | MODULE_LICENSE("GPL v2"); |