blob: d1826cf87a76ab9af057ad2a932a2c4581f42094 [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;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700125 struct mmc_command *cmd;
126
127 spinlock_t lock;
128 void __iomem *regs;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700129 struct clk *core_clk;
130 struct clk_mux mux;
131 struct clk *mux_clk;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100132 unsigned long current_clock;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700133
134 struct clk_divider cfg_div;
135 struct clk *cfg_div_clk;
136
137 unsigned int bounce_buf_size;
138 void *bounce_buf;
139 dma_addr_t bounce_dma_addr;
140
141 bool vqmmc_enabled;
142};
143
144struct sd_emmc_desc {
145 u32 cmd_cfg;
146 u32 cmd_arg;
147 u32 cmd_data;
148 u32 cmd_resp;
149};
150#define CMD_CFG_LENGTH_SHIFT 0
151#define CMD_CFG_LENGTH_MASK 0x1ff
152#define CMD_CFG_BLOCK_MODE BIT(9)
153#define CMD_CFG_R1B BIT(10)
154#define CMD_CFG_END_OF_CHAIN BIT(11)
155#define CMD_CFG_TIMEOUT_SHIFT 12
156#define CMD_CFG_TIMEOUT_MASK 0xf
157#define CMD_CFG_NO_RESP BIT(16)
158#define CMD_CFG_NO_CMD BIT(17)
159#define CMD_CFG_DATA_IO BIT(18)
160#define CMD_CFG_DATA_WR BIT(19)
161#define CMD_CFG_RESP_NOCRC BIT(20)
162#define CMD_CFG_RESP_128 BIT(21)
163#define CMD_CFG_RESP_NUM BIT(22)
164#define CMD_CFG_DATA_NUM BIT(23)
165#define CMD_CFG_CMD_INDEX_SHIFT 24
166#define CMD_CFG_CMD_INDEX_MASK 0x3f
167#define CMD_CFG_ERROR BIT(30)
168#define CMD_CFG_OWNER BIT(31)
169
170#define CMD_DATA_MASK (~0x3)
171#define CMD_DATA_BIG_ENDIAN BIT(1)
172#define CMD_DATA_SRAM BIT(0)
173#define CMD_RESP_MASK (~0x1)
174#define CMD_RESP_SRAM BIT(0)
175
176static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
177{
178 struct mmc_host *mmc = host->mmc;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100179 int ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700180 u32 cfg;
181
182 if (clk_rate) {
183 if (WARN_ON(clk_rate > mmc->f_max))
184 clk_rate = mmc->f_max;
185 else if (WARN_ON(clk_rate < mmc->f_min))
186 clk_rate = mmc->f_min;
187 }
188
Heiner Kallweit5da86882017-02-07 22:34:32 +0100189 if (clk_rate == host->current_clock)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700190 return 0;
191
192 /* stop clock */
193 cfg = readl(host->regs + SD_EMMC_CFG);
194 if (!(cfg & CFG_STOP_CLOCK)) {
195 cfg |= CFG_STOP_CLOCK;
196 writel(cfg, host->regs + SD_EMMC_CFG);
197 }
198
199 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
200 mmc->actual_clock, clk_rate);
201
Heiner Kallweit5da86882017-02-07 22:34:32 +0100202 if (!clk_rate) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700203 mmc->actual_clock = 0;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100204 host->current_clock = 0;
205 /* return with clock being stopped */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700206 return 0;
207 }
208
209 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
Heiner Kallweit5da86882017-02-07 22:34:32 +0100210 if (ret) {
211 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
212 clk_rate, ret);
213 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700214 }
215
Heiner Kallweit5da86882017-02-07 22:34:32 +0100216 mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
217 host->current_clock = clk_rate;
218
219 if (clk_rate != mmc->actual_clock)
220 dev_dbg(host->dev,
221 "divider requested rate %lu != actual rate %u\n",
222 clk_rate, mmc->actual_clock);
223
224 /* (re)start clock */
225 cfg = readl(host->regs + SD_EMMC_CFG);
226 cfg &= ~CFG_STOP_CLOCK;
227 writel(cfg, host->regs + SD_EMMC_CFG);
228
229 return 0;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700230}
231
232/*
233 * The SD/eMMC IP block has an internal mux and divider used for
234 * generating the MMC clock. Use the clock framework to create and
235 * manage these clocks.
236 */
237static int meson_mmc_clk_init(struct meson_host *host)
238{
239 struct clk_init_data init;
240 char clk_name[32];
241 int i, ret = 0;
242 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700243 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700244 u32 clk_reg, cfg;
245
246 /* get the mux parents */
247 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100248 struct clk *clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700249 char name[16];
250
251 snprintf(name, sizeof(name), "clkin%d", i);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100252 clk = devm_clk_get(host->dev, name);
253 if (IS_ERR(clk)) {
254 if (clk != ERR_PTR(-EPROBE_DEFER))
Kevin Hilman51c5d842016-10-19 11:18:24 -0700255 dev_err(host->dev, "Missing clock %s\n", name);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100256 return PTR_ERR(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700257 }
258
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100259 mux_parent_names[i] = __clk_get_name(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700260 }
261
Kevin Hilman51c5d842016-10-19 11:18:24 -0700262 /* create the mux */
263 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
264 init.name = clk_name;
265 init.ops = &clk_mux_ops;
266 init.flags = 0;
267 init.parent_names = mux_parent_names;
Heiner Kallweit7558c112017-03-04 13:22:57 +0100268 init.num_parents = MUX_CLK_NUM_PARENTS;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700269
270 host->mux.reg = host->regs + SD_EMMC_CLOCK;
271 host->mux.shift = CLK_SRC_SHIFT;
272 host->mux.mask = CLK_SRC_MASK;
273 host->mux.flags = 0;
274 host->mux.table = NULL;
275 host->mux.hw.init = &init;
276
277 host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
278 if (WARN_ON(IS_ERR(host->mux_clk)))
279 return PTR_ERR(host->mux_clk);
280
281 /* create the divider */
282 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
Heiner Kallweit7b9ebad2017-03-04 13:26:24 +0100283 init.name = clk_name;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700284 init.ops = &clk_divider_ops;
285 init.flags = CLK_SET_RATE_PARENT;
286 clk_div_parents[0] = __clk_get_name(host->mux_clk);
287 init.parent_names = clk_div_parents;
288 init.num_parents = ARRAY_SIZE(clk_div_parents);
289
290 host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
291 host->cfg_div.shift = CLK_DIV_SHIFT;
292 host->cfg_div.width = CLK_DIV_WIDTH;
293 host->cfg_div.hw.init = &init;
294 host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
295 CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO;
296
297 host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
298 if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
299 return PTR_ERR(host->cfg_div_clk);
300
301 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
302 clk_reg = 0;
303 clk_reg |= CLK_PHASE_180 << CLK_PHASE_SHIFT;
304 clk_reg |= CLK_SRC_XTAL << CLK_SRC_SHIFT;
305 clk_reg |= CLK_DIV_MAX << CLK_DIV_SHIFT;
306 clk_reg &= ~CLK_ALWAYS_ON;
307 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
308
309 /* Ensure clock starts in "auto" mode, not "always on" */
310 cfg = readl(host->regs + SD_EMMC_CFG);
311 cfg &= ~CFG_CLK_ALWAYS_ON;
312 cfg |= CFG_AUTO_CLK;
313 writel(cfg, host->regs + SD_EMMC_CFG);
314
315 ret = clk_prepare_enable(host->cfg_div_clk);
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100316 if (ret)
317 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700318
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100319 /* Get the nearest minimum clock to 400KHz */
320 host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
321
322 ret = meson_mmc_clk_set(host, host->mmc->f_min);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100323 if (ret)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700324 clk_disable_unprepare(host->cfg_div_clk);
325
326 return ret;
327}
328
329static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
330{
331 struct meson_host *host = mmc_priv(mmc);
332 u32 bus_width;
333 u32 val, orig;
334
335 /*
336 * GPIO regulator, only controls switching between 1v8 and
337 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
338 */
339 switch (ios->power_mode) {
340 case MMC_POWER_OFF:
341 if (!IS_ERR(mmc->supply.vmmc))
342 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
343
344 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
345 regulator_disable(mmc->supply.vqmmc);
346 host->vqmmc_enabled = false;
347 }
348
349 break;
350
351 case MMC_POWER_UP:
352 if (!IS_ERR(mmc->supply.vmmc))
353 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
354 break;
355
356 case MMC_POWER_ON:
357 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
358 int ret = regulator_enable(mmc->supply.vqmmc);
359
360 if (ret < 0)
361 dev_err(mmc_dev(mmc),
362 "failed to enable vqmmc regulator\n");
363 else
364 host->vqmmc_enabled = true;
365 }
366
367 break;
368 }
369
370
371 meson_mmc_clk_set(host, ios->clock);
372
373 /* Bus width */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700374 switch (ios->bus_width) {
375 case MMC_BUS_WIDTH_1:
376 bus_width = CFG_BUS_WIDTH_1;
377 break;
378 case MMC_BUS_WIDTH_4:
379 bus_width = CFG_BUS_WIDTH_4;
380 break;
381 case MMC_BUS_WIDTH_8:
382 bus_width = CFG_BUS_WIDTH_8;
383 break;
384 default:
385 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
386 ios->bus_width);
387 bus_width = CFG_BUS_WIDTH_4;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700388 }
389
390 val = readl(host->regs + SD_EMMC_CFG);
391 orig = val;
392
393 val &= ~(CFG_BUS_WIDTH_MASK << CFG_BUS_WIDTH_SHIFT);
394 val |= bus_width << CFG_BUS_WIDTH_SHIFT;
395
Heiner Kallweite21e6fd2017-02-07 22:35:59 +0100396 val &= ~CFG_DDR;
397 if (ios->timing == MMC_TIMING_UHS_DDR50 ||
398 ios->timing == MMC_TIMING_MMC_DDR52 ||
399 ios->timing == MMC_TIMING_MMC_HS400)
400 val |= CFG_DDR;
401
402 val &= ~CFG_CHK_DS;
403 if (ios->timing == MMC_TIMING_MMC_HS400)
404 val |= CFG_CHK_DS;
405
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100406 if (val != orig) {
407 writel(val, host->regs + SD_EMMC_CFG);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700408 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
409 __func__, orig, val);
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100410 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700411}
412
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100413static void meson_mmc_request_done(struct mmc_host *mmc,
414 struct mmc_request *mrq)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700415{
416 struct meson_host *host = mmc_priv(mmc);
417
Kevin Hilman51c5d842016-10-19 11:18:24 -0700418 host->cmd = NULL;
419 mmc_request_done(host->mmc, mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700420}
421
422static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
423{
424 struct meson_host *host = mmc_priv(mmc);
425 struct sd_emmc_desc *desc, desc_tmp;
426 u32 cfg;
427 u8 blk_len, cmd_cfg_timeout;
428 unsigned int xfer_bytes = 0;
429
430 /* Setup descriptors */
431 dma_rmb();
432 desc = &desc_tmp;
433 memset(desc, 0, sizeof(struct sd_emmc_desc));
434
435 desc->cmd_cfg |= (cmd->opcode & CMD_CFG_CMD_INDEX_MASK) <<
436 CMD_CFG_CMD_INDEX_SHIFT;
437 desc->cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
438 desc->cmd_arg = cmd->arg;
439
440 /* Response */
441 if (cmd->flags & MMC_RSP_PRESENT) {
442 desc->cmd_cfg &= ~CMD_CFG_NO_RESP;
443 if (cmd->flags & MMC_RSP_136)
444 desc->cmd_cfg |= CMD_CFG_RESP_128;
445 desc->cmd_cfg |= CMD_CFG_RESP_NUM;
446 desc->cmd_resp = 0;
447
448 if (!(cmd->flags & MMC_RSP_CRC))
449 desc->cmd_cfg |= CMD_CFG_RESP_NOCRC;
450
451 if (cmd->flags & MMC_RSP_BUSY)
452 desc->cmd_cfg |= CMD_CFG_R1B;
453 } else {
454 desc->cmd_cfg |= CMD_CFG_NO_RESP;
455 }
456
457 /* data? */
458 if (cmd->data) {
459 desc->cmd_cfg |= CMD_CFG_DATA_IO;
460 if (cmd->data->blocks > 1) {
461 desc->cmd_cfg |= CMD_CFG_BLOCK_MODE;
462 desc->cmd_cfg |=
463 (cmd->data->blocks & CMD_CFG_LENGTH_MASK) <<
464 CMD_CFG_LENGTH_SHIFT;
465
466 /* check if block-size matches, if not update */
467 cfg = readl(host->regs + SD_EMMC_CFG);
468 blk_len = cfg & (CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
469 blk_len >>= CFG_BLK_LEN_SHIFT;
470 if (blk_len != ilog2(cmd->data->blksz)) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800471 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n",
Kevin Hilman51c5d842016-10-19 11:18:24 -0700472 __func__, blk_len,
Kevin Hilmandc012052017-01-25 16:01:39 -0800473 ilog2(cmd->data->blksz));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700474 blk_len = ilog2(cmd->data->blksz);
475 cfg &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
476 cfg |= blk_len << CFG_BLK_LEN_SHIFT;
477 writel(cfg, host->regs + SD_EMMC_CFG);
478 }
479 } else {
480 desc->cmd_cfg &= ~CMD_CFG_BLOCK_MODE;
481 desc->cmd_cfg |=
482 (cmd->data->blksz & CMD_CFG_LENGTH_MASK) <<
483 CMD_CFG_LENGTH_SHIFT;
484 }
485
486 cmd->data->bytes_xfered = 0;
487 xfer_bytes = cmd->data->blksz * cmd->data->blocks;
488 if (cmd->data->flags & MMC_DATA_WRITE) {
489 desc->cmd_cfg |= CMD_CFG_DATA_WR;
490 WARN_ON(xfer_bytes > host->bounce_buf_size);
491 sg_copy_to_buffer(cmd->data->sg, cmd->data->sg_len,
492 host->bounce_buf, xfer_bytes);
493 cmd->data->bytes_xfered = xfer_bytes;
494 dma_wmb();
495 } else {
496 desc->cmd_cfg &= ~CMD_CFG_DATA_WR;
497 }
498
Heiner Kallweit94d765b2017-03-04 13:19:23 +0100499 desc->cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700500
501 cmd_cfg_timeout = 12;
502 } else {
503 desc->cmd_cfg &= ~CMD_CFG_DATA_IO;
504 cmd_cfg_timeout = 10;
505 }
506 desc->cmd_cfg |= (cmd_cfg_timeout & CMD_CFG_TIMEOUT_MASK) <<
507 CMD_CFG_TIMEOUT_SHIFT;
508
509 host->cmd = cmd;
510
511 /* Last descriptor */
512 desc->cmd_cfg |= CMD_CFG_END_OF_CHAIN;
513 writel(desc->cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
514 writel(desc->cmd_data, host->regs + SD_EMMC_CMD_DAT);
515 writel(desc->cmd_resp, host->regs + SD_EMMC_CMD_RSP);
516 wmb(); /* ensure descriptor is written before kicked */
517 writel(desc->cmd_arg, host->regs + SD_EMMC_CMD_ARG);
518}
519
520static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
521{
522 struct meson_host *host = mmc_priv(mmc);
523
Kevin Hilman51c5d842016-10-19 11:18:24 -0700524 /* Stop execution */
525 writel(0, host->regs + SD_EMMC_START);
526
Kevin Hilman51c5d842016-10-19 11:18:24 -0700527 if (mrq->sbc)
528 meson_mmc_start_cmd(mmc, mrq->sbc);
529 else
530 meson_mmc_start_cmd(mmc, mrq->cmd);
531}
532
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100533static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700534{
535 struct meson_host *host = mmc_priv(mmc);
536
537 if (cmd->flags & MMC_RSP_136) {
538 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
539 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
540 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
541 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
542 } else if (cmd->flags & MMC_RSP_PRESENT) {
543 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
544 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700545}
546
547static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
548{
549 struct meson_host *host = dev_id;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100550 struct mmc_command *cmd;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700551 u32 irq_en, status, raw_status;
552 irqreturn_t ret = IRQ_HANDLED;
553
554 if (WARN_ON(!host))
555 return IRQ_NONE;
556
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100557 cmd = host->cmd;
558
Kevin Hilman51c5d842016-10-19 11:18:24 -0700559 if (WARN_ON(!cmd))
560 return IRQ_NONE;
561
562 spin_lock(&host->lock);
563 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
564 raw_status = readl(host->regs + SD_EMMC_STATUS);
565 status = raw_status & irq_en;
566
567 if (!status) {
568 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
569 raw_status, irq_en);
570 ret = IRQ_NONE;
571 goto out;
572 }
573
574 cmd->error = 0;
575 if (status & IRQ_RXD_ERR_MASK) {
576 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
577 cmd->error = -EILSEQ;
578 }
579 if (status & IRQ_TXD_ERR) {
580 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
581 cmd->error = -EILSEQ;
582 }
583 if (status & IRQ_DESC_ERR)
584 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
585 if (status & IRQ_RESP_ERR) {
586 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
587 cmd->error = -EILSEQ;
588 }
589 if (status & IRQ_RESP_TIMEOUT) {
590 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
591 cmd->error = -ETIMEDOUT;
592 }
593 if (status & IRQ_DESC_TIMEOUT) {
594 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
595 cmd->error = -ETIMEDOUT;
596 }
597 if (status & IRQ_SDIO)
598 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
599
600 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS))
601 ret = IRQ_WAKE_THREAD;
602 else {
603 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
604 status, cmd->opcode, cmd->arg,
Heiner Kallweit7cdcc482017-03-04 13:36:45 +0100605 cmd->flags, cmd->mrq->stop ? 1 : 0);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700606 if (cmd->data) {
607 struct mmc_data *data = cmd->data;
608
609 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
610 data->blksz, data->blocks, data->flags,
611 data->flags & MMC_DATA_WRITE ? "write" : "",
612 data->flags & MMC_DATA_READ ? "read" : "");
613 }
614 }
615
616out:
617 /* ack all (enabled) interrupts */
618 writel(status, host->regs + SD_EMMC_STATUS);
619
620 if (ret == IRQ_HANDLED) {
621 meson_mmc_read_resp(host->mmc, cmd);
622 meson_mmc_request_done(host->mmc, cmd->mrq);
623 }
624
625 spin_unlock(&host->lock);
626 return ret;
627}
628
629static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
630{
631 struct meson_host *host = dev_id;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700632 struct mmc_command *cmd = host->cmd;
633 struct mmc_data *data;
634 unsigned int xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700635
Kevin Hilman51c5d842016-10-19 11:18:24 -0700636 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100637 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700638
639 data = cmd->data;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100640 if (data && data->flags & MMC_DATA_READ) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700641 xfer_bytes = data->blksz * data->blocks;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100642 WARN_ON(xfer_bytes > host->bounce_buf_size);
643 sg_copy_from_buffer(data->sg, data->sg_len,
644 host->bounce_buf, xfer_bytes);
645 data->bytes_xfered = xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700646 }
647
648 meson_mmc_read_resp(host->mmc, cmd);
Heiner Kallweit7cdcc482017-03-04 13:36:45 +0100649 if (!data || !data->stop || cmd->mrq->sbc)
650 meson_mmc_request_done(host->mmc, cmd->mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700651 else
652 meson_mmc_start_cmd(host->mmc, data->stop);
653
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100654 return IRQ_HANDLED;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700655}
656
657/*
658 * NOTE: we only need this until the GPIO/pinctrl driver can handle
659 * interrupts. For now, the MMC core will use this for polling.
660 */
661static int meson_mmc_get_cd(struct mmc_host *mmc)
662{
663 int status = mmc_gpio_get_cd(mmc);
664
665 if (status == -ENOSYS)
666 return 1; /* assume present */
667
668 return status;
669}
670
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100671static void meson_mmc_cfg_init(struct meson_host *host)
672{
673 u32 cfg = 0;
674
675 cfg |= ilog2(SD_EMMC_CFG_RESP_TIMEOUT) << CFG_RESP_TIMEOUT_SHIFT;
676 cfg |= ilog2(SD_EMMC_CFG_CMD_GAP) << CFG_RC_CC_SHIFT;
677 cfg |= ilog2(SD_EMMC_CFG_BLK_SIZE) << CFG_BLK_LEN_SHIFT;
678
679 writel(cfg, host->regs + SD_EMMC_CFG);
680}
681
Kevin Hilman51c5d842016-10-19 11:18:24 -0700682static const struct mmc_host_ops meson_mmc_ops = {
683 .request = meson_mmc_request,
684 .set_ios = meson_mmc_set_ios,
685 .get_cd = meson_mmc_get_cd,
686};
687
688static int meson_mmc_probe(struct platform_device *pdev)
689{
690 struct resource *res;
691 struct meson_host *host;
692 struct mmc_host *mmc;
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100693 int ret, irq;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700694
695 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
696 if (!mmc)
697 return -ENOMEM;
698 host = mmc_priv(mmc);
699 host->mmc = mmc;
700 host->dev = &pdev->dev;
701 dev_set_drvdata(&pdev->dev, host);
702
703 spin_lock_init(&host->lock);
704
705 /* Get regulators and the supported OCR mask */
706 host->vqmmc_enabled = false;
707 ret = mmc_regulator_get_supply(mmc);
708 if (ret == -EPROBE_DEFER)
709 goto free_host;
710
711 ret = mmc_of_parse(mmc);
712 if (ret) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800713 if (ret != -EPROBE_DEFER)
714 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700715 goto free_host;
716 }
717
718 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
719 host->regs = devm_ioremap_resource(&pdev->dev, res);
720 if (IS_ERR(host->regs)) {
721 ret = PTR_ERR(host->regs);
722 goto free_host;
723 }
724
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100725 irq = platform_get_irq(pdev, 0);
726 if (!irq) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700727 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
728 ret = -EINVAL;
729 goto free_host;
730 }
731
732 host->core_clk = devm_clk_get(&pdev->dev, "core");
733 if (IS_ERR(host->core_clk)) {
734 ret = PTR_ERR(host->core_clk);
735 goto free_host;
736 }
737
738 ret = clk_prepare_enable(host->core_clk);
739 if (ret)
740 goto free_host;
741
742 ret = meson_mmc_clk_init(host);
743 if (ret)
744 goto free_host;
745
746 /* Stop execution */
747 writel(0, host->regs + SD_EMMC_START);
748
749 /* clear, ack, enable all interrupts */
750 writel(0, host->regs + SD_EMMC_IRQ_EN);
751 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
Heiner Kallweit92763b92017-02-07 22:34:51 +0100752 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700753
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100754 /* set config to sane default */
755 meson_mmc_cfg_init(host);
756
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100757 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
758 meson_mmc_irq_thread, IRQF_SHARED,
759 DRIVER_NAME, host);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700760 if (ret)
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100761 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700762
Heiner Kallweitefe0b662017-02-07 22:34:58 +0100763 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
764 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
765
Kevin Hilman51c5d842016-10-19 11:18:24 -0700766 /* data bounce buffer */
Heiner Kallweit4136fcb2017-02-07 22:35:02 +0100767 host->bounce_buf_size = mmc->max_req_size;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700768 host->bounce_buf =
769 dma_alloc_coherent(host->dev, host->bounce_buf_size,
770 &host->bounce_dma_addr, GFP_KERNEL);
771 if (host->bounce_buf == NULL) {
772 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
773 ret = -ENOMEM;
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100774 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700775 }
776
777 mmc->ops = &meson_mmc_ops;
778 mmc_add_host(mmc);
779
780 return 0;
781
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100782err_div_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700783 clk_disable_unprepare(host->cfg_div_clk);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100784free_host:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700785 clk_disable_unprepare(host->core_clk);
786 mmc_free_host(mmc);
787 return ret;
788}
789
790static int meson_mmc_remove(struct platform_device *pdev)
791{
792 struct meson_host *host = dev_get_drvdata(&pdev->dev);
793
Heiner Kallweit92763b92017-02-07 22:34:51 +0100794 /* disable interrupts */
795 writel(0, host->regs + SD_EMMC_IRQ_EN);
796
Heiner Kallweit62d721a2017-02-07 22:35:40 +0100797 dma_free_coherent(host->dev, host->bounce_buf_size,
798 host->bounce_buf, host->bounce_dma_addr);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700799
800 clk_disable_unprepare(host->cfg_div_clk);
801 clk_disable_unprepare(host->core_clk);
802
803 mmc_free_host(host->mmc);
804 return 0;
805}
806
807static const struct of_device_id meson_mmc_of_match[] = {
808 { .compatible = "amlogic,meson-gx-mmc", },
809 { .compatible = "amlogic,meson-gxbb-mmc", },
810 { .compatible = "amlogic,meson-gxl-mmc", },
811 { .compatible = "amlogic,meson-gxm-mmc", },
812 {}
813};
814MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
815
816static struct platform_driver meson_mmc_driver = {
817 .probe = meson_mmc_probe,
818 .remove = meson_mmc_remove,
819 .driver = {
820 .name = DRIVER_NAME,
821 .of_match_table = of_match_ptr(meson_mmc_of_match),
822 },
823};
824
825module_platform_driver(meson_mmc_driver);
826
827MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
828MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
829MODULE_LICENSE("GPL v2");