blob: 0a0f5008940b5c7e05d8aa13debc19cff83166c5 [file] [log] [blame]
Kevin Hilman51c5d842016-10-19 11:18:24 -07001/*
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 Hanssonb8789ec2016-12-30 13:47:23 +010038#include <linux/interrupt.h>
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010039#include <linux/bitfield.h>
Kevin Hilman51c5d842016-10-19 11:18:24 -070040
41#define DRIVER_NAME "meson-gx-mmc"
42
43#define SD_EMMC_CLOCK 0x0
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010044#define CLK_DIV_MASK GENMASK(5, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070045#define CLK_DIV_MAX 63
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010046#define CLK_SRC_MASK GENMASK(7, 6)
Kevin Hilman51c5d842016-10-19 11:18:24 -070047#define CLK_SRC_XTAL 0 /* external crystal */
48#define CLK_SRC_XTAL_RATE 24000000
49#define CLK_SRC_PLL 1 /* FCLK_DIV2 */
50#define CLK_SRC_PLL_RATE 1000000000
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010051#define CLK_CORE_PHASE_MASK GENMASK(9, 8)
Kevin Hilman51c5d842016-10-19 11:18:24 -070052#define CLK_PHASE_0 0
53#define CLK_PHASE_90 1
54#define CLK_PHASE_180 2
55#define CLK_PHASE_270 3
56#define CLK_ALWAYS_ON BIT(24)
57
58#define SD_EMMC_DElAY 0x4
59#define SD_EMMC_ADJUST 0x8
60#define SD_EMMC_CALOUT 0x10
61#define SD_EMMC_START 0x40
62#define START_DESC_INIT BIT(0)
63#define START_DESC_BUSY BIT(1)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010064#define START_DESC_ADDR_MASK GENMASK(31, 2)
Kevin Hilman51c5d842016-10-19 11:18:24 -070065
66#define SD_EMMC_CFG 0x44
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010067#define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070068#define CFG_BUS_WIDTH_1 0x0
69#define CFG_BUS_WIDTH_4 0x1
70#define CFG_BUS_WIDTH_8 0x2
71#define CFG_DDR BIT(2)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010072#define CFG_BLK_LEN_MASK GENMASK(7, 4)
73#define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
74#define CFG_RC_CC_MASK GENMASK(15, 12)
Kevin Hilman51c5d842016-10-19 11:18:24 -070075#define CFG_STOP_CLOCK BIT(22)
76#define CFG_CLK_ALWAYS_ON BIT(18)
Heiner Kallweite21e6fd2017-02-07 22:35:59 +010077#define CFG_CHK_DS BIT(20)
Kevin Hilman51c5d842016-10-19 11:18:24 -070078#define CFG_AUTO_CLK BIT(23)
79
80#define SD_EMMC_STATUS 0x48
81#define STATUS_BUSY BIT(31)
82
83#define SD_EMMC_IRQ_EN 0x4c
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010084#define IRQ_EN_MASK GENMASK(13, 0)
85#define IRQ_RXD_ERR_MASK GENMASK(7, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070086#define IRQ_TXD_ERR BIT(8)
87#define IRQ_DESC_ERR BIT(9)
88#define IRQ_RESP_ERR BIT(10)
89#define IRQ_RESP_TIMEOUT BIT(11)
90#define IRQ_DESC_TIMEOUT BIT(12)
91#define IRQ_END_OF_CHAIN BIT(13)
92#define IRQ_RESP_STATUS BIT(14)
93#define IRQ_SDIO BIT(15)
94
95#define SD_EMMC_CMD_CFG 0x50
96#define SD_EMMC_CMD_ARG 0x54
97#define SD_EMMC_CMD_DAT 0x58
98#define SD_EMMC_CMD_RSP 0x5c
99#define SD_EMMC_CMD_RSP1 0x60
100#define SD_EMMC_CMD_RSP2 0x64
101#define SD_EMMC_CMD_RSP3 0x68
102
103#define SD_EMMC_RXD 0x94
104#define SD_EMMC_TXD 0x94
105#define SD_EMMC_LAST_REG SD_EMMC_TXD
106
107#define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
108#define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
Heiner Kallweitbb11eff2017-03-04 13:37:46 +0100109#define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
110#define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700111#define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
112#define MUX_CLK_NUM_PARENTS 2
113
114struct meson_host {
115 struct device *dev;
116 struct mmc_host *mmc;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700117 struct mmc_command *cmd;
118
119 spinlock_t lock;
120 void __iomem *regs;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700121 struct clk *core_clk;
122 struct clk_mux mux;
123 struct clk *mux_clk;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100124 unsigned long current_clock;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700125
126 struct clk_divider cfg_div;
127 struct clk *cfg_div_clk;
128
129 unsigned int bounce_buf_size;
130 void *bounce_buf;
131 dma_addr_t bounce_dma_addr;
132
133 bool vqmmc_enabled;
134};
135
136struct sd_emmc_desc {
137 u32 cmd_cfg;
138 u32 cmd_arg;
139 u32 cmd_data;
140 u32 cmd_resp;
141};
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100142
143#define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700144#define CMD_CFG_BLOCK_MODE BIT(9)
145#define CMD_CFG_R1B BIT(10)
146#define CMD_CFG_END_OF_CHAIN BIT(11)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100147#define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700148#define CMD_CFG_NO_RESP BIT(16)
149#define CMD_CFG_NO_CMD BIT(17)
150#define CMD_CFG_DATA_IO BIT(18)
151#define CMD_CFG_DATA_WR BIT(19)
152#define CMD_CFG_RESP_NOCRC BIT(20)
153#define CMD_CFG_RESP_128 BIT(21)
154#define CMD_CFG_RESP_NUM BIT(22)
155#define CMD_CFG_DATA_NUM BIT(23)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100156#define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700157#define CMD_CFG_ERROR BIT(30)
158#define CMD_CFG_OWNER BIT(31)
159
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100160#define CMD_DATA_MASK GENMASK(31, 2)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700161#define CMD_DATA_BIG_ENDIAN BIT(1)
162#define CMD_DATA_SRAM BIT(0)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100163#define CMD_RESP_MASK GENMASK(31, 1)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700164#define CMD_RESP_SRAM BIT(0)
165
166static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
167{
168 struct mmc_host *mmc = host->mmc;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100169 int ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700170 u32 cfg;
171
172 if (clk_rate) {
173 if (WARN_ON(clk_rate > mmc->f_max))
174 clk_rate = mmc->f_max;
175 else if (WARN_ON(clk_rate < mmc->f_min))
176 clk_rate = mmc->f_min;
177 }
178
Heiner Kallweit5da86882017-02-07 22:34:32 +0100179 if (clk_rate == host->current_clock)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700180 return 0;
181
182 /* stop clock */
183 cfg = readl(host->regs + SD_EMMC_CFG);
184 if (!(cfg & CFG_STOP_CLOCK)) {
185 cfg |= CFG_STOP_CLOCK;
186 writel(cfg, host->regs + SD_EMMC_CFG);
187 }
188
189 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
190 mmc->actual_clock, clk_rate);
191
Heiner Kallweit5da86882017-02-07 22:34:32 +0100192 if (!clk_rate) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700193 mmc->actual_clock = 0;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100194 host->current_clock = 0;
195 /* return with clock being stopped */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700196 return 0;
197 }
198
199 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
Heiner Kallweit5da86882017-02-07 22:34:32 +0100200 if (ret) {
201 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
202 clk_rate, ret);
203 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700204 }
205
Heiner Kallweit5da86882017-02-07 22:34:32 +0100206 mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
207 host->current_clock = clk_rate;
208
209 if (clk_rate != mmc->actual_clock)
210 dev_dbg(host->dev,
211 "divider requested rate %lu != actual rate %u\n",
212 clk_rate, mmc->actual_clock);
213
214 /* (re)start clock */
215 cfg = readl(host->regs + SD_EMMC_CFG);
216 cfg &= ~CFG_STOP_CLOCK;
217 writel(cfg, host->regs + SD_EMMC_CFG);
218
219 return 0;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700220}
221
222/*
223 * The SD/eMMC IP block has an internal mux and divider used for
224 * generating the MMC clock. Use the clock framework to create and
225 * manage these clocks.
226 */
227static int meson_mmc_clk_init(struct meson_host *host)
228{
229 struct clk_init_data init;
230 char clk_name[32];
231 int i, ret = 0;
232 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700233 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700234 u32 clk_reg, cfg;
235
236 /* get the mux parents */
237 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100238 struct clk *clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700239 char name[16];
240
241 snprintf(name, sizeof(name), "clkin%d", i);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100242 clk = devm_clk_get(host->dev, name);
243 if (IS_ERR(clk)) {
244 if (clk != ERR_PTR(-EPROBE_DEFER))
Kevin Hilman51c5d842016-10-19 11:18:24 -0700245 dev_err(host->dev, "Missing clock %s\n", name);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100246 return PTR_ERR(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700247 }
248
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100249 mux_parent_names[i] = __clk_get_name(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700250 }
251
Kevin Hilman51c5d842016-10-19 11:18:24 -0700252 /* create the mux */
253 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
254 init.name = clk_name;
255 init.ops = &clk_mux_ops;
256 init.flags = 0;
257 init.parent_names = mux_parent_names;
Heiner Kallweit7558c112017-03-04 13:22:57 +0100258 init.num_parents = MUX_CLK_NUM_PARENTS;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700259 host->mux.reg = host->regs + SD_EMMC_CLOCK;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100260 host->mux.shift = __bf_shf(CLK_SRC_MASK);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700261 host->mux.mask = CLK_SRC_MASK;
262 host->mux.flags = 0;
263 host->mux.table = NULL;
264 host->mux.hw.init = &init;
265
266 host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
267 if (WARN_ON(IS_ERR(host->mux_clk)))
268 return PTR_ERR(host->mux_clk);
269
270 /* create the divider */
271 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
Heiner Kallweit7b9ebad2017-03-04 13:26:24 +0100272 init.name = clk_name;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700273 init.ops = &clk_divider_ops;
274 init.flags = CLK_SET_RATE_PARENT;
275 clk_div_parents[0] = __clk_get_name(host->mux_clk);
276 init.parent_names = clk_div_parents;
277 init.num_parents = ARRAY_SIZE(clk_div_parents);
278
279 host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100280 host->cfg_div.shift = __bf_shf(CLK_DIV_MASK);
281 host->cfg_div.width = __builtin_popcountl(CLK_DIV_MASK);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700282 host->cfg_div.hw.init = &init;
283 host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
284 CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO;
285
286 host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
287 if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
288 return PTR_ERR(host->cfg_div_clk);
289
290 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
291 clk_reg = 0;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100292 clk_reg |= FIELD_PREP(CLK_CORE_PHASE_MASK, CLK_PHASE_180);
293 clk_reg |= FIELD_PREP(CLK_SRC_MASK, CLK_SRC_XTAL);
294 clk_reg |= FIELD_PREP(CLK_DIV_MASK, CLK_DIV_MAX);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700295 clk_reg &= ~CLK_ALWAYS_ON;
296 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
297
298 /* Ensure clock starts in "auto" mode, not "always on" */
299 cfg = readl(host->regs + SD_EMMC_CFG);
300 cfg &= ~CFG_CLK_ALWAYS_ON;
301 cfg |= CFG_AUTO_CLK;
302 writel(cfg, host->regs + SD_EMMC_CFG);
303
304 ret = clk_prepare_enable(host->cfg_div_clk);
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100305 if (ret)
306 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700307
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100308 /* Get the nearest minimum clock to 400KHz */
309 host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
310
311 ret = meson_mmc_clk_set(host, host->mmc->f_min);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100312 if (ret)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700313 clk_disable_unprepare(host->cfg_div_clk);
314
315 return ret;
316}
317
318static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
319{
320 struct meson_host *host = mmc_priv(mmc);
321 u32 bus_width;
322 u32 val, orig;
323
324 /*
325 * GPIO regulator, only controls switching between 1v8 and
326 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
327 */
328 switch (ios->power_mode) {
329 case MMC_POWER_OFF:
330 if (!IS_ERR(mmc->supply.vmmc))
331 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
332
333 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
334 regulator_disable(mmc->supply.vqmmc);
335 host->vqmmc_enabled = false;
336 }
337
338 break;
339
340 case MMC_POWER_UP:
341 if (!IS_ERR(mmc->supply.vmmc))
342 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
343 break;
344
345 case MMC_POWER_ON:
346 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
347 int ret = regulator_enable(mmc->supply.vqmmc);
348
349 if (ret < 0)
350 dev_err(mmc_dev(mmc),
351 "failed to enable vqmmc regulator\n");
352 else
353 host->vqmmc_enabled = true;
354 }
355
356 break;
357 }
358
359
360 meson_mmc_clk_set(host, ios->clock);
361
362 /* Bus width */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700363 switch (ios->bus_width) {
364 case MMC_BUS_WIDTH_1:
365 bus_width = CFG_BUS_WIDTH_1;
366 break;
367 case MMC_BUS_WIDTH_4:
368 bus_width = CFG_BUS_WIDTH_4;
369 break;
370 case MMC_BUS_WIDTH_8:
371 bus_width = CFG_BUS_WIDTH_8;
372 break;
373 default:
374 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
375 ios->bus_width);
376 bus_width = CFG_BUS_WIDTH_4;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700377 }
378
379 val = readl(host->regs + SD_EMMC_CFG);
380 orig = val;
381
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100382 val &= ~CFG_BUS_WIDTH_MASK;
383 val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700384
Heiner Kallweite21e6fd2017-02-07 22:35:59 +0100385 val &= ~CFG_DDR;
386 if (ios->timing == MMC_TIMING_UHS_DDR50 ||
387 ios->timing == MMC_TIMING_MMC_DDR52 ||
388 ios->timing == MMC_TIMING_MMC_HS400)
389 val |= CFG_DDR;
390
391 val &= ~CFG_CHK_DS;
392 if (ios->timing == MMC_TIMING_MMC_HS400)
393 val |= CFG_CHK_DS;
394
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100395 if (val != orig) {
396 writel(val, host->regs + SD_EMMC_CFG);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700397 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
398 __func__, orig, val);
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100399 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700400}
401
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100402static void meson_mmc_request_done(struct mmc_host *mmc,
403 struct mmc_request *mrq)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700404{
405 struct meson_host *host = mmc_priv(mmc);
406
Kevin Hilman51c5d842016-10-19 11:18:24 -0700407 host->cmd = NULL;
408 mmc_request_done(host->mmc, mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700409}
410
411static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
412{
413 struct meson_host *host = mmc_priv(mmc);
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100414 struct mmc_data *data = cmd->data;
Heiner Kallweita322feb2017-03-22 22:33:47 +0100415 u32 cfg, cmd_cfg = 0, cmd_data = 0;
Heiner Kallweita744c6f2017-03-22 22:34:01 +0100416 u8 blk_len;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700417 unsigned int xfer_bytes = 0;
418
419 /* Setup descriptors */
420 dma_rmb();
Kevin Hilman51c5d842016-10-19 11:18:24 -0700421
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100422 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
Heiner Kallweita322feb2017-03-22 22:33:47 +0100423 cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700424
425 /* Response */
426 if (cmd->flags & MMC_RSP_PRESENT) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700427 if (cmd->flags & MMC_RSP_136)
Heiner Kallweita322feb2017-03-22 22:33:47 +0100428 cmd_cfg |= CMD_CFG_RESP_128;
429 cmd_cfg |= CMD_CFG_RESP_NUM;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700430
431 if (!(cmd->flags & MMC_RSP_CRC))
Heiner Kallweita322feb2017-03-22 22:33:47 +0100432 cmd_cfg |= CMD_CFG_RESP_NOCRC;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700433
434 if (cmd->flags & MMC_RSP_BUSY)
Heiner Kallweita322feb2017-03-22 22:33:47 +0100435 cmd_cfg |= CMD_CFG_R1B;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700436 } else {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100437 cmd_cfg |= CMD_CFG_NO_RESP;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700438 }
439
440 /* data? */
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100441 if (data) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100442 cmd_cfg |= CMD_CFG_DATA_IO;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100443 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
444 ilog2(SD_EMMC_CMD_TIMEOUT_DATA));
Heiner Kallweita744c6f2017-03-22 22:34:01 +0100445
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100446 if (data->blocks > 1) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100447 cmd_cfg |= CMD_CFG_BLOCK_MODE;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100448 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
449 data->blocks);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700450
451 /* check if block-size matches, if not update */
452 cfg = readl(host->regs + SD_EMMC_CFG);
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100453 blk_len = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100454 if (blk_len != ilog2(data->blksz)) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800455 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n",
Kevin Hilman51c5d842016-10-19 11:18:24 -0700456 __func__, blk_len,
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100457 ilog2(data->blksz));
458 blk_len = ilog2(data->blksz);
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100459 cfg &= ~CFG_BLK_LEN_MASK;
460 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blk_len);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700461 writel(cfg, host->regs + SD_EMMC_CFG);
462 }
463 } else {
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100464 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700465 }
466
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100467 data->bytes_xfered = 0;
468 xfer_bytes = data->blksz * data->blocks;
469 if (data->flags & MMC_DATA_WRITE) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100470 cmd_cfg |= CMD_CFG_DATA_WR;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700471 WARN_ON(xfer_bytes > host->bounce_buf_size);
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100472 sg_copy_to_buffer(data->sg, data->sg_len,
Kevin Hilman51c5d842016-10-19 11:18:24 -0700473 host->bounce_buf, xfer_bytes);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700474 dma_wmb();
Kevin Hilman51c5d842016-10-19 11:18:24 -0700475 }
476
Heiner Kallweita322feb2017-03-22 22:33:47 +0100477 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700478 } else {
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100479 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
480 ilog2(SD_EMMC_CMD_TIMEOUT));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700481 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700482
483 host->cmd = cmd;
484
485 /* Last descriptor */
Heiner Kallweita322feb2017-03-22 22:33:47 +0100486 cmd_cfg |= CMD_CFG_END_OF_CHAIN;
487 writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
488 writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
489 writel(0, host->regs + SD_EMMC_CMD_RSP);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700490 wmb(); /* ensure descriptor is written before kicked */
Heiner Kallweita322feb2017-03-22 22:33:47 +0100491 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700492}
493
494static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
495{
496 struct meson_host *host = mmc_priv(mmc);
497
Kevin Hilman51c5d842016-10-19 11:18:24 -0700498 /* Stop execution */
499 writel(0, host->regs + SD_EMMC_START);
500
Kevin Hilman51c5d842016-10-19 11:18:24 -0700501 if (mrq->sbc)
502 meson_mmc_start_cmd(mmc, mrq->sbc);
503 else
504 meson_mmc_start_cmd(mmc, mrq->cmd);
505}
506
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100507static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700508{
509 struct meson_host *host = mmc_priv(mmc);
510
511 if (cmd->flags & MMC_RSP_136) {
512 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
513 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
514 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
515 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
516 } else if (cmd->flags & MMC_RSP_PRESENT) {
517 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
518 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700519}
520
521static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
522{
523 struct meson_host *host = dev_id;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100524 struct mmc_command *cmd;
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100525 struct mmc_data *data;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700526 u32 irq_en, status, raw_status;
527 irqreturn_t ret = IRQ_HANDLED;
528
529 if (WARN_ON(!host))
530 return IRQ_NONE;
531
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100532 cmd = host->cmd;
533
Kevin Hilman51c5d842016-10-19 11:18:24 -0700534 if (WARN_ON(!cmd))
535 return IRQ_NONE;
536
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100537 data = cmd->data;
538
Kevin Hilman51c5d842016-10-19 11:18:24 -0700539 spin_lock(&host->lock);
540 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
541 raw_status = readl(host->regs + SD_EMMC_STATUS);
542 status = raw_status & irq_en;
543
544 if (!status) {
545 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
546 raw_status, irq_en);
547 ret = IRQ_NONE;
548 goto out;
549 }
550
Heiner Kallweit1f8066d2017-03-22 22:33:50 +0100551 meson_mmc_read_resp(host->mmc, cmd);
552
Kevin Hilman51c5d842016-10-19 11:18:24 -0700553 cmd->error = 0;
554 if (status & IRQ_RXD_ERR_MASK) {
555 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
556 cmd->error = -EILSEQ;
557 }
558 if (status & IRQ_TXD_ERR) {
559 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
560 cmd->error = -EILSEQ;
561 }
562 if (status & IRQ_DESC_ERR)
563 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
564 if (status & IRQ_RESP_ERR) {
565 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
566 cmd->error = -EILSEQ;
567 }
568 if (status & IRQ_RESP_TIMEOUT) {
569 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
570 cmd->error = -ETIMEDOUT;
571 }
572 if (status & IRQ_DESC_TIMEOUT) {
573 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
574 cmd->error = -ETIMEDOUT;
575 }
576 if (status & IRQ_SDIO)
577 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
578
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100579 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
580 if (data && !cmd->error)
581 data->bytes_xfered = data->blksz * data->blocks;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700582 ret = IRQ_WAKE_THREAD;
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100583 } else {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700584 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
585 status, cmd->opcode, cmd->arg,
Heiner Kallweit7cdcc482017-03-04 13:36:45 +0100586 cmd->flags, cmd->mrq->stop ? 1 : 0);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700587 if (cmd->data) {
588 struct mmc_data *data = cmd->data;
589
590 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
591 data->blksz, data->blocks, data->flags,
592 data->flags & MMC_DATA_WRITE ? "write" : "",
593 data->flags & MMC_DATA_READ ? "read" : "");
594 }
595 }
596
597out:
598 /* ack all (enabled) interrupts */
599 writel(status, host->regs + SD_EMMC_STATUS);
600
Heiner Kallweit1f8066d2017-03-22 22:33:50 +0100601 if (ret == IRQ_HANDLED)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700602 meson_mmc_request_done(host->mmc, cmd->mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700603
604 spin_unlock(&host->lock);
605 return ret;
606}
607
608static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
609{
610 struct meson_host *host = dev_id;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700611 struct mmc_command *cmd = host->cmd;
612 struct mmc_data *data;
613 unsigned int xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700614
Kevin Hilman51c5d842016-10-19 11:18:24 -0700615 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100616 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700617
618 data = cmd->data;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100619 if (data && data->flags & MMC_DATA_READ) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700620 xfer_bytes = data->blksz * data->blocks;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100621 WARN_ON(xfer_bytes > host->bounce_buf_size);
622 sg_copy_from_buffer(data->sg, data->sg_len,
623 host->bounce_buf, xfer_bytes);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700624 }
625
Heiner Kallweit7cdcc482017-03-04 13:36:45 +0100626 if (!data || !data->stop || cmd->mrq->sbc)
627 meson_mmc_request_done(host->mmc, cmd->mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700628 else
629 meson_mmc_start_cmd(host->mmc, data->stop);
630
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100631 return IRQ_HANDLED;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700632}
633
634/*
635 * NOTE: we only need this until the GPIO/pinctrl driver can handle
636 * interrupts. For now, the MMC core will use this for polling.
637 */
638static int meson_mmc_get_cd(struct mmc_host *mmc)
639{
640 int status = mmc_gpio_get_cd(mmc);
641
642 if (status == -ENOSYS)
643 return 1; /* assume present */
644
645 return status;
646}
647
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100648static void meson_mmc_cfg_init(struct meson_host *host)
649{
650 u32 cfg = 0;
651
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100652 cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
653 ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
654 cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
655 cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100656
657 writel(cfg, host->regs + SD_EMMC_CFG);
658}
659
Kevin Hilman51c5d842016-10-19 11:18:24 -0700660static const struct mmc_host_ops meson_mmc_ops = {
661 .request = meson_mmc_request,
662 .set_ios = meson_mmc_set_ios,
663 .get_cd = meson_mmc_get_cd,
664};
665
666static int meson_mmc_probe(struct platform_device *pdev)
667{
668 struct resource *res;
669 struct meson_host *host;
670 struct mmc_host *mmc;
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100671 int ret, irq;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700672
673 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
674 if (!mmc)
675 return -ENOMEM;
676 host = mmc_priv(mmc);
677 host->mmc = mmc;
678 host->dev = &pdev->dev;
679 dev_set_drvdata(&pdev->dev, host);
680
681 spin_lock_init(&host->lock);
682
683 /* Get regulators and the supported OCR mask */
684 host->vqmmc_enabled = false;
685 ret = mmc_regulator_get_supply(mmc);
686 if (ret == -EPROBE_DEFER)
687 goto free_host;
688
689 ret = mmc_of_parse(mmc);
690 if (ret) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800691 if (ret != -EPROBE_DEFER)
692 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700693 goto free_host;
694 }
695
696 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
697 host->regs = devm_ioremap_resource(&pdev->dev, res);
698 if (IS_ERR(host->regs)) {
699 ret = PTR_ERR(host->regs);
700 goto free_host;
701 }
702
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100703 irq = platform_get_irq(pdev, 0);
704 if (!irq) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700705 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
706 ret = -EINVAL;
707 goto free_host;
708 }
709
710 host->core_clk = devm_clk_get(&pdev->dev, "core");
711 if (IS_ERR(host->core_clk)) {
712 ret = PTR_ERR(host->core_clk);
713 goto free_host;
714 }
715
716 ret = clk_prepare_enable(host->core_clk);
717 if (ret)
718 goto free_host;
719
720 ret = meson_mmc_clk_init(host);
721 if (ret)
Michał Zegance473d52017-03-14 21:05:20 +0100722 goto err_core_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700723
724 /* Stop execution */
725 writel(0, host->regs + SD_EMMC_START);
726
727 /* clear, ack, enable all interrupts */
728 writel(0, host->regs + SD_EMMC_IRQ_EN);
729 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
Heiner Kallweit92763b92017-02-07 22:34:51 +0100730 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700731
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100732 /* set config to sane default */
733 meson_mmc_cfg_init(host);
734
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100735 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
736 meson_mmc_irq_thread, IRQF_SHARED,
Heiner Kallweitf016c672017-03-25 11:24:41 +0100737 NULL, host);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700738 if (ret)
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100739 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700740
Heiner Kallweitefe0b662017-02-07 22:34:58 +0100741 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
742 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
743
Kevin Hilman51c5d842016-10-19 11:18:24 -0700744 /* data bounce buffer */
Heiner Kallweit4136fcb2017-02-07 22:35:02 +0100745 host->bounce_buf_size = mmc->max_req_size;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700746 host->bounce_buf =
747 dma_alloc_coherent(host->dev, host->bounce_buf_size,
748 &host->bounce_dma_addr, GFP_KERNEL);
749 if (host->bounce_buf == NULL) {
750 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
751 ret = -ENOMEM;
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100752 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700753 }
754
755 mmc->ops = &meson_mmc_ops;
756 mmc_add_host(mmc);
757
758 return 0;
759
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100760err_div_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700761 clk_disable_unprepare(host->cfg_div_clk);
Michał Zegance473d52017-03-14 21:05:20 +0100762err_core_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700763 clk_disable_unprepare(host->core_clk);
Michał Zegance473d52017-03-14 21:05:20 +0100764free_host:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700765 mmc_free_host(mmc);
766 return ret;
767}
768
769static int meson_mmc_remove(struct platform_device *pdev)
770{
771 struct meson_host *host = dev_get_drvdata(&pdev->dev);
772
Michał Zegana01fc2a2017-02-18 18:06:47 +0100773 mmc_remove_host(host->mmc);
774
Heiner Kallweit92763b92017-02-07 22:34:51 +0100775 /* disable interrupts */
776 writel(0, host->regs + SD_EMMC_IRQ_EN);
777
Heiner Kallweit62d721a2017-02-07 22:35:40 +0100778 dma_free_coherent(host->dev, host->bounce_buf_size,
779 host->bounce_buf, host->bounce_dma_addr);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700780
781 clk_disable_unprepare(host->cfg_div_clk);
782 clk_disable_unprepare(host->core_clk);
783
784 mmc_free_host(host->mmc);
785 return 0;
786}
787
788static const struct of_device_id meson_mmc_of_match[] = {
789 { .compatible = "amlogic,meson-gx-mmc", },
790 { .compatible = "amlogic,meson-gxbb-mmc", },
791 { .compatible = "amlogic,meson-gxl-mmc", },
792 { .compatible = "amlogic,meson-gxm-mmc", },
793 {}
794};
795MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
796
797static struct platform_driver meson_mmc_driver = {
798 .probe = meson_mmc_probe,
799 .remove = meson_mmc_remove,
800 .driver = {
801 .name = DRIVER_NAME,
802 .of_match_table = of_match_ptr(meson_mmc_of_match),
803 },
804};
805
806module_platform_driver(meson_mmc_driver);
807
808MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
809MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
810MODULE_LICENSE("GPL v2");