blob: 684cc08813ac534362b86a307c69ff4bec416694 [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>
Kevin Hilman51c5d842016-10-19 11:18:24 -070039
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 Kallweite21e6fd2017-02-07 22:35:59 +010086#define CFG_CHK_DS BIT(20)
Kevin Hilman51c5d842016-10-19 11:18:24 -070087#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 */
119#define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
120#define MUX_CLK_NUM_PARENTS 2
121
122struct meson_host {
123 struct device *dev;
124 struct mmc_host *mmc;
125 struct mmc_request *mrq;
126 struct mmc_command *cmd;
127
128 spinlock_t lock;
129 void __iomem *regs;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700130 struct clk *core_clk;
131 struct clk_mux mux;
132 struct clk *mux_clk;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100133 unsigned long current_clock;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700134
135 struct clk_divider cfg_div;
136 struct clk *cfg_div_clk;
137
138 unsigned int bounce_buf_size;
139 void *bounce_buf;
140 dma_addr_t bounce_dma_addr;
141
142 bool vqmmc_enabled;
143};
144
145struct sd_emmc_desc {
146 u32 cmd_cfg;
147 u32 cmd_arg;
148 u32 cmd_data;
149 u32 cmd_resp;
150};
151#define CMD_CFG_LENGTH_SHIFT 0
152#define CMD_CFG_LENGTH_MASK 0x1ff
153#define CMD_CFG_BLOCK_MODE BIT(9)
154#define CMD_CFG_R1B BIT(10)
155#define CMD_CFG_END_OF_CHAIN BIT(11)
156#define CMD_CFG_TIMEOUT_SHIFT 12
157#define CMD_CFG_TIMEOUT_MASK 0xf
158#define CMD_CFG_NO_RESP BIT(16)
159#define CMD_CFG_NO_CMD BIT(17)
160#define CMD_CFG_DATA_IO BIT(18)
161#define CMD_CFG_DATA_WR BIT(19)
162#define CMD_CFG_RESP_NOCRC BIT(20)
163#define CMD_CFG_RESP_128 BIT(21)
164#define CMD_CFG_RESP_NUM BIT(22)
165#define CMD_CFG_DATA_NUM BIT(23)
166#define CMD_CFG_CMD_INDEX_SHIFT 24
167#define CMD_CFG_CMD_INDEX_MASK 0x3f
168#define CMD_CFG_ERROR BIT(30)
169#define CMD_CFG_OWNER BIT(31)
170
171#define CMD_DATA_MASK (~0x3)
172#define CMD_DATA_BIG_ENDIAN BIT(1)
173#define CMD_DATA_SRAM BIT(0)
174#define CMD_RESP_MASK (~0x1)
175#define CMD_RESP_SRAM BIT(0)
176
177static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
178{
179 struct mmc_host *mmc = host->mmc;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100180 int ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700181 u32 cfg;
182
183 if (clk_rate) {
184 if (WARN_ON(clk_rate > mmc->f_max))
185 clk_rate = mmc->f_max;
186 else if (WARN_ON(clk_rate < mmc->f_min))
187 clk_rate = mmc->f_min;
188 }
189
Heiner Kallweit5da86882017-02-07 22:34:32 +0100190 if (clk_rate == host->current_clock)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700191 return 0;
192
193 /* stop clock */
194 cfg = readl(host->regs + SD_EMMC_CFG);
195 if (!(cfg & CFG_STOP_CLOCK)) {
196 cfg |= CFG_STOP_CLOCK;
197 writel(cfg, host->regs + SD_EMMC_CFG);
198 }
199
200 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
201 mmc->actual_clock, clk_rate);
202
Heiner Kallweit5da86882017-02-07 22:34:32 +0100203 if (!clk_rate) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700204 mmc->actual_clock = 0;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100205 host->current_clock = 0;
206 /* return with clock being stopped */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700207 return 0;
208 }
209
210 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
Heiner Kallweit5da86882017-02-07 22:34:32 +0100211 if (ret) {
212 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
213 clk_rate, ret);
214 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700215 }
216
Heiner Kallweit5da86882017-02-07 22:34:32 +0100217 mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
218 host->current_clock = clk_rate;
219
220 if (clk_rate != mmc->actual_clock)
221 dev_dbg(host->dev,
222 "divider requested rate %lu != actual rate %u\n",
223 clk_rate, mmc->actual_clock);
224
225 /* (re)start clock */
226 cfg = readl(host->regs + SD_EMMC_CFG);
227 cfg &= ~CFG_STOP_CLOCK;
228 writel(cfg, host->regs + SD_EMMC_CFG);
229
230 return 0;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700231}
232
233/*
234 * The SD/eMMC IP block has an internal mux and divider used for
235 * generating the MMC clock. Use the clock framework to create and
236 * manage these clocks.
237 */
238static int meson_mmc_clk_init(struct meson_host *host)
239{
240 struct clk_init_data init;
241 char clk_name[32];
242 int i, ret = 0;
243 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700244 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700245 u32 clk_reg, cfg;
246
247 /* get the mux parents */
248 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100249 struct clk *clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700250 char name[16];
251
252 snprintf(name, sizeof(name), "clkin%d", i);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100253 clk = devm_clk_get(host->dev, name);
254 if (IS_ERR(clk)) {
255 if (clk != ERR_PTR(-EPROBE_DEFER))
Kevin Hilman51c5d842016-10-19 11:18:24 -0700256 dev_err(host->dev, "Missing clock %s\n", name);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100257 return PTR_ERR(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700258 }
259
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100260 mux_parent_names[i] = __clk_get_name(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700261 }
262
Kevin Hilman51c5d842016-10-19 11:18:24 -0700263 /* create the mux */
264 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
265 init.name = clk_name;
266 init.ops = &clk_mux_ops;
267 init.flags = 0;
268 init.parent_names = mux_parent_names;
Heiner Kallweit7558c112017-03-04 13:22:57 +0100269 init.num_parents = MUX_CLK_NUM_PARENTS;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700270
271 host->mux.reg = host->regs + SD_EMMC_CLOCK;
272 host->mux.shift = CLK_SRC_SHIFT;
273 host->mux.mask = CLK_SRC_MASK;
274 host->mux.flags = 0;
275 host->mux.table = NULL;
276 host->mux.hw.init = &init;
277
278 host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
279 if (WARN_ON(IS_ERR(host->mux_clk)))
280 return PTR_ERR(host->mux_clk);
281
282 /* create the divider */
283 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
Heiner Kallweit7b9ebad2017-03-04 13:26:24 +0100284 init.name = clk_name;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700285 init.ops = &clk_divider_ops;
286 init.flags = CLK_SET_RATE_PARENT;
287 clk_div_parents[0] = __clk_get_name(host->mux_clk);
288 init.parent_names = clk_div_parents;
289 init.num_parents = ARRAY_SIZE(clk_div_parents);
290
291 host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
292 host->cfg_div.shift = CLK_DIV_SHIFT;
293 host->cfg_div.width = CLK_DIV_WIDTH;
294 host->cfg_div.hw.init = &init;
295 host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
296 CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO;
297
298 host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
299 if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
300 return PTR_ERR(host->cfg_div_clk);
301
302 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
303 clk_reg = 0;
304 clk_reg |= CLK_PHASE_180 << CLK_PHASE_SHIFT;
305 clk_reg |= CLK_SRC_XTAL << CLK_SRC_SHIFT;
306 clk_reg |= CLK_DIV_MAX << CLK_DIV_SHIFT;
307 clk_reg &= ~CLK_ALWAYS_ON;
308 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
309
310 /* Ensure clock starts in "auto" mode, not "always on" */
311 cfg = readl(host->regs + SD_EMMC_CFG);
312 cfg &= ~CFG_CLK_ALWAYS_ON;
313 cfg |= CFG_AUTO_CLK;
314 writel(cfg, host->regs + SD_EMMC_CFG);
315
316 ret = clk_prepare_enable(host->cfg_div_clk);
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100317 if (ret)
318 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700319
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100320 /* Get the nearest minimum clock to 400KHz */
321 host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
322
323 ret = meson_mmc_clk_set(host, host->mmc->f_min);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100324 if (ret)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700325 clk_disable_unprepare(host->cfg_div_clk);
326
327 return ret;
328}
329
330static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
331{
332 struct meson_host *host = mmc_priv(mmc);
333 u32 bus_width;
334 u32 val, orig;
335
336 /*
337 * GPIO regulator, only controls switching between 1v8 and
338 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
339 */
340 switch (ios->power_mode) {
341 case MMC_POWER_OFF:
342 if (!IS_ERR(mmc->supply.vmmc))
343 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
344
345 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
346 regulator_disable(mmc->supply.vqmmc);
347 host->vqmmc_enabled = false;
348 }
349
350 break;
351
352 case MMC_POWER_UP:
353 if (!IS_ERR(mmc->supply.vmmc))
354 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
355 break;
356
357 case MMC_POWER_ON:
358 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
359 int ret = regulator_enable(mmc->supply.vqmmc);
360
361 if (ret < 0)
362 dev_err(mmc_dev(mmc),
363 "failed to enable vqmmc regulator\n");
364 else
365 host->vqmmc_enabled = true;
366 }
367
368 break;
369 }
370
371
372 meson_mmc_clk_set(host, ios->clock);
373
374 /* Bus width */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700375 switch (ios->bus_width) {
376 case MMC_BUS_WIDTH_1:
377 bus_width = CFG_BUS_WIDTH_1;
378 break;
379 case MMC_BUS_WIDTH_4:
380 bus_width = CFG_BUS_WIDTH_4;
381 break;
382 case MMC_BUS_WIDTH_8:
383 bus_width = CFG_BUS_WIDTH_8;
384 break;
385 default:
386 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
387 ios->bus_width);
388 bus_width = CFG_BUS_WIDTH_4;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700389 }
390
391 val = readl(host->regs + SD_EMMC_CFG);
392 orig = val;
393
394 val &= ~(CFG_BUS_WIDTH_MASK << CFG_BUS_WIDTH_SHIFT);
395 val |= bus_width << CFG_BUS_WIDTH_SHIFT;
396
397 val &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
398 val |= ilog2(SD_EMMC_CFG_BLK_SIZE) << CFG_BLK_LEN_SHIFT;
399
400 val &= ~(CFG_RESP_TIMEOUT_MASK << CFG_RESP_TIMEOUT_SHIFT);
401 val |= ilog2(SD_EMMC_CFG_RESP_TIMEOUT) << CFG_RESP_TIMEOUT_SHIFT;
402
403 val &= ~(CFG_RC_CC_MASK << CFG_RC_CC_SHIFT);
404 val |= ilog2(SD_EMMC_CFG_CMD_GAP) << CFG_RC_CC_SHIFT;
405
Heiner Kallweite21e6fd2017-02-07 22:35:59 +0100406 val &= ~CFG_DDR;
407 if (ios->timing == MMC_TIMING_UHS_DDR50 ||
408 ios->timing == MMC_TIMING_MMC_DDR52 ||
409 ios->timing == MMC_TIMING_MMC_HS400)
410 val |= CFG_DDR;
411
412 val &= ~CFG_CHK_DS;
413 if (ios->timing == MMC_TIMING_MMC_HS400)
414 val |= CFG_CHK_DS;
415
Kevin Hilman51c5d842016-10-19 11:18:24 -0700416 writel(val, host->regs + SD_EMMC_CFG);
417
418 if (val != orig)
419 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
420 __func__, orig, val);
421}
422
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100423static void meson_mmc_request_done(struct mmc_host *mmc,
424 struct mmc_request *mrq)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700425{
426 struct meson_host *host = mmc_priv(mmc);
427
428 WARN_ON(host->mrq != mrq);
429
430 host->mrq = NULL;
431 host->cmd = NULL;
432 mmc_request_done(host->mmc, mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700433}
434
435static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
436{
437 struct meson_host *host = mmc_priv(mmc);
438 struct sd_emmc_desc *desc, desc_tmp;
439 u32 cfg;
440 u8 blk_len, cmd_cfg_timeout;
441 unsigned int xfer_bytes = 0;
442
443 /* Setup descriptors */
444 dma_rmb();
445 desc = &desc_tmp;
446 memset(desc, 0, sizeof(struct sd_emmc_desc));
447
448 desc->cmd_cfg |= (cmd->opcode & CMD_CFG_CMD_INDEX_MASK) <<
449 CMD_CFG_CMD_INDEX_SHIFT;
450 desc->cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
451 desc->cmd_arg = cmd->arg;
452
453 /* Response */
454 if (cmd->flags & MMC_RSP_PRESENT) {
455 desc->cmd_cfg &= ~CMD_CFG_NO_RESP;
456 if (cmd->flags & MMC_RSP_136)
457 desc->cmd_cfg |= CMD_CFG_RESP_128;
458 desc->cmd_cfg |= CMD_CFG_RESP_NUM;
459 desc->cmd_resp = 0;
460
461 if (!(cmd->flags & MMC_RSP_CRC))
462 desc->cmd_cfg |= CMD_CFG_RESP_NOCRC;
463
464 if (cmd->flags & MMC_RSP_BUSY)
465 desc->cmd_cfg |= CMD_CFG_R1B;
466 } else {
467 desc->cmd_cfg |= CMD_CFG_NO_RESP;
468 }
469
470 /* data? */
471 if (cmd->data) {
472 desc->cmd_cfg |= CMD_CFG_DATA_IO;
473 if (cmd->data->blocks > 1) {
474 desc->cmd_cfg |= CMD_CFG_BLOCK_MODE;
475 desc->cmd_cfg |=
476 (cmd->data->blocks & CMD_CFG_LENGTH_MASK) <<
477 CMD_CFG_LENGTH_SHIFT;
478
479 /* check if block-size matches, if not update */
480 cfg = readl(host->regs + SD_EMMC_CFG);
481 blk_len = cfg & (CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
482 blk_len >>= CFG_BLK_LEN_SHIFT;
483 if (blk_len != ilog2(cmd->data->blksz)) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800484 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n",
Kevin Hilman51c5d842016-10-19 11:18:24 -0700485 __func__, blk_len,
Kevin Hilmandc012052017-01-25 16:01:39 -0800486 ilog2(cmd->data->blksz));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700487 blk_len = ilog2(cmd->data->blksz);
488 cfg &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
489 cfg |= blk_len << CFG_BLK_LEN_SHIFT;
490 writel(cfg, host->regs + SD_EMMC_CFG);
491 }
492 } else {
493 desc->cmd_cfg &= ~CMD_CFG_BLOCK_MODE;
494 desc->cmd_cfg |=
495 (cmd->data->blksz & CMD_CFG_LENGTH_MASK) <<
496 CMD_CFG_LENGTH_SHIFT;
497 }
498
499 cmd->data->bytes_xfered = 0;
500 xfer_bytes = cmd->data->blksz * cmd->data->blocks;
501 if (cmd->data->flags & MMC_DATA_WRITE) {
502 desc->cmd_cfg |= CMD_CFG_DATA_WR;
503 WARN_ON(xfer_bytes > host->bounce_buf_size);
504 sg_copy_to_buffer(cmd->data->sg, cmd->data->sg_len,
505 host->bounce_buf, xfer_bytes);
506 cmd->data->bytes_xfered = xfer_bytes;
507 dma_wmb();
508 } else {
509 desc->cmd_cfg &= ~CMD_CFG_DATA_WR;
510 }
511
Heiner Kallweit94d765b2017-03-04 13:19:23 +0100512 desc->cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700513
514 cmd_cfg_timeout = 12;
515 } else {
516 desc->cmd_cfg &= ~CMD_CFG_DATA_IO;
517 cmd_cfg_timeout = 10;
518 }
519 desc->cmd_cfg |= (cmd_cfg_timeout & CMD_CFG_TIMEOUT_MASK) <<
520 CMD_CFG_TIMEOUT_SHIFT;
521
522 host->cmd = cmd;
523
524 /* Last descriptor */
525 desc->cmd_cfg |= CMD_CFG_END_OF_CHAIN;
526 writel(desc->cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
527 writel(desc->cmd_data, host->regs + SD_EMMC_CMD_DAT);
528 writel(desc->cmd_resp, host->regs + SD_EMMC_CMD_RSP);
529 wmb(); /* ensure descriptor is written before kicked */
530 writel(desc->cmd_arg, host->regs + SD_EMMC_CMD_ARG);
531}
532
533static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
534{
535 struct meson_host *host = mmc_priv(mmc);
536
537 WARN_ON(host->mrq != NULL);
538
539 /* Stop execution */
540 writel(0, host->regs + SD_EMMC_START);
541
Kevin Hilman51c5d842016-10-19 11:18:24 -0700542 host->mrq = mrq;
543
544 if (mrq->sbc)
545 meson_mmc_start_cmd(mmc, mrq->sbc);
546 else
547 meson_mmc_start_cmd(mmc, mrq->cmd);
548}
549
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100550static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700551{
552 struct meson_host *host = mmc_priv(mmc);
553
554 if (cmd->flags & MMC_RSP_136) {
555 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
556 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
557 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
558 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
559 } else if (cmd->flags & MMC_RSP_PRESENT) {
560 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
561 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700562}
563
564static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
565{
566 struct meson_host *host = dev_id;
567 struct mmc_request *mrq;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100568 struct mmc_command *cmd;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700569 u32 irq_en, status, raw_status;
570 irqreturn_t ret = IRQ_HANDLED;
571
572 if (WARN_ON(!host))
573 return IRQ_NONE;
574
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100575 cmd = host->cmd;
576
Kevin Hilman51c5d842016-10-19 11:18:24 -0700577 mrq = host->mrq;
578
579 if (WARN_ON(!mrq))
580 return IRQ_NONE;
581
582 if (WARN_ON(!cmd))
583 return IRQ_NONE;
584
585 spin_lock(&host->lock);
586 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
587 raw_status = readl(host->regs + SD_EMMC_STATUS);
588 status = raw_status & irq_en;
589
590 if (!status) {
591 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
592 raw_status, irq_en);
593 ret = IRQ_NONE;
594 goto out;
595 }
596
597 cmd->error = 0;
598 if (status & IRQ_RXD_ERR_MASK) {
599 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
600 cmd->error = -EILSEQ;
601 }
602 if (status & IRQ_TXD_ERR) {
603 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
604 cmd->error = -EILSEQ;
605 }
606 if (status & IRQ_DESC_ERR)
607 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
608 if (status & IRQ_RESP_ERR) {
609 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
610 cmd->error = -EILSEQ;
611 }
612 if (status & IRQ_RESP_TIMEOUT) {
613 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
614 cmd->error = -ETIMEDOUT;
615 }
616 if (status & IRQ_DESC_TIMEOUT) {
617 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
618 cmd->error = -ETIMEDOUT;
619 }
620 if (status & IRQ_SDIO)
621 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
622
623 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS))
624 ret = IRQ_WAKE_THREAD;
625 else {
626 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
627 status, cmd->opcode, cmd->arg,
628 cmd->flags, mrq->stop ? 1 : 0);
629 if (cmd->data) {
630 struct mmc_data *data = cmd->data;
631
632 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
633 data->blksz, data->blocks, data->flags,
634 data->flags & MMC_DATA_WRITE ? "write" : "",
635 data->flags & MMC_DATA_READ ? "read" : "");
636 }
637 }
638
639out:
640 /* ack all (enabled) interrupts */
641 writel(status, host->regs + SD_EMMC_STATUS);
642
643 if (ret == IRQ_HANDLED) {
644 meson_mmc_read_resp(host->mmc, cmd);
645 meson_mmc_request_done(host->mmc, cmd->mrq);
646 }
647
648 spin_unlock(&host->lock);
649 return ret;
650}
651
652static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
653{
654 struct meson_host *host = dev_id;
655 struct mmc_request *mrq = host->mrq;
656 struct mmc_command *cmd = host->cmd;
657 struct mmc_data *data;
658 unsigned int xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700659
660 if (WARN_ON(!mrq))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100661 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700662
663 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100664 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700665
666 data = cmd->data;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100667 if (data && data->flags & MMC_DATA_READ) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700668 xfer_bytes = data->blksz * data->blocks;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100669 WARN_ON(xfer_bytes > host->bounce_buf_size);
670 sg_copy_from_buffer(data->sg, data->sg_len,
671 host->bounce_buf, xfer_bytes);
672 data->bytes_xfered = xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700673 }
674
675 meson_mmc_read_resp(host->mmc, cmd);
676 if (!data || !data->stop || mrq->sbc)
677 meson_mmc_request_done(host->mmc, mrq);
678 else
679 meson_mmc_start_cmd(host->mmc, data->stop);
680
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100681 return IRQ_HANDLED;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700682}
683
684/*
685 * NOTE: we only need this until the GPIO/pinctrl driver can handle
686 * interrupts. For now, the MMC core will use this for polling.
687 */
688static int meson_mmc_get_cd(struct mmc_host *mmc)
689{
690 int status = mmc_gpio_get_cd(mmc);
691
692 if (status == -ENOSYS)
693 return 1; /* assume present */
694
695 return status;
696}
697
698static const struct mmc_host_ops meson_mmc_ops = {
699 .request = meson_mmc_request,
700 .set_ios = meson_mmc_set_ios,
701 .get_cd = meson_mmc_get_cd,
702};
703
704static int meson_mmc_probe(struct platform_device *pdev)
705{
706 struct resource *res;
707 struct meson_host *host;
708 struct mmc_host *mmc;
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100709 int ret, irq;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700710
711 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
712 if (!mmc)
713 return -ENOMEM;
714 host = mmc_priv(mmc);
715 host->mmc = mmc;
716 host->dev = &pdev->dev;
717 dev_set_drvdata(&pdev->dev, host);
718
719 spin_lock_init(&host->lock);
720
721 /* Get regulators and the supported OCR mask */
722 host->vqmmc_enabled = false;
723 ret = mmc_regulator_get_supply(mmc);
724 if (ret == -EPROBE_DEFER)
725 goto free_host;
726
727 ret = mmc_of_parse(mmc);
728 if (ret) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800729 if (ret != -EPROBE_DEFER)
730 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700731 goto free_host;
732 }
733
734 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
735 host->regs = devm_ioremap_resource(&pdev->dev, res);
736 if (IS_ERR(host->regs)) {
737 ret = PTR_ERR(host->regs);
738 goto free_host;
739 }
740
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100741 irq = platform_get_irq(pdev, 0);
742 if (!irq) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700743 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
744 ret = -EINVAL;
745 goto free_host;
746 }
747
748 host->core_clk = devm_clk_get(&pdev->dev, "core");
749 if (IS_ERR(host->core_clk)) {
750 ret = PTR_ERR(host->core_clk);
751 goto free_host;
752 }
753
754 ret = clk_prepare_enable(host->core_clk);
755 if (ret)
756 goto free_host;
757
758 ret = meson_mmc_clk_init(host);
759 if (ret)
760 goto free_host;
761
762 /* Stop execution */
763 writel(0, host->regs + SD_EMMC_START);
764
765 /* clear, ack, enable all interrupts */
766 writel(0, host->regs + SD_EMMC_IRQ_EN);
767 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
Heiner Kallweit92763b92017-02-07 22:34:51 +0100768 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700769
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100770 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
771 meson_mmc_irq_thread, IRQF_SHARED,
772 DRIVER_NAME, host);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700773 if (ret)
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100774 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700775
Heiner Kallweitefe0b662017-02-07 22:34:58 +0100776 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
777 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
778
Kevin Hilman51c5d842016-10-19 11:18:24 -0700779 /* data bounce buffer */
Heiner Kallweit4136fcb2017-02-07 22:35:02 +0100780 host->bounce_buf_size = mmc->max_req_size;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700781 host->bounce_buf =
782 dma_alloc_coherent(host->dev, host->bounce_buf_size,
783 &host->bounce_dma_addr, GFP_KERNEL);
784 if (host->bounce_buf == NULL) {
785 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
786 ret = -ENOMEM;
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100787 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700788 }
789
790 mmc->ops = &meson_mmc_ops;
791 mmc_add_host(mmc);
792
793 return 0;
794
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100795err_div_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700796 clk_disable_unprepare(host->cfg_div_clk);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100797free_host:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700798 clk_disable_unprepare(host->core_clk);
799 mmc_free_host(mmc);
800 return ret;
801}
802
803static int meson_mmc_remove(struct platform_device *pdev)
804{
805 struct meson_host *host = dev_get_drvdata(&pdev->dev);
806
Heiner Kallweit92763b92017-02-07 22:34:51 +0100807 /* disable interrupts */
808 writel(0, host->regs + SD_EMMC_IRQ_EN);
809
Heiner Kallweit62d721a2017-02-07 22:35:40 +0100810 dma_free_coherent(host->dev, host->bounce_buf_size,
811 host->bounce_buf, host->bounce_dma_addr);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700812
813 clk_disable_unprepare(host->cfg_div_clk);
814 clk_disable_unprepare(host->core_clk);
815
816 mmc_free_host(host->mmc);
817 return 0;
818}
819
820static const struct of_device_id meson_mmc_of_match[] = {
821 { .compatible = "amlogic,meson-gx-mmc", },
822 { .compatible = "amlogic,meson-gxbb-mmc", },
823 { .compatible = "amlogic,meson-gxl-mmc", },
824 { .compatible = "amlogic,meson-gxm-mmc", },
825 {}
826};
827MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
828
829static struct platform_driver meson_mmc_driver = {
830 .probe = meson_mmc_probe,
831 .remove = meson_mmc_remove,
832 .driver = {
833 .name = DRIVER_NAME,
834 .of_match_table = of_match_ptr(meson_mmc_of_match),
835 },
836};
837
838module_platform_driver(meson_mmc_driver);
839
840MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
841MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
842MODULE_LICENSE("GPL v2");