blob: c9f251713611512b9f4c572c1537f71f9316fc69 [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)
86#define CFG_AUTO_CLK BIT(23)
87
88#define SD_EMMC_STATUS 0x48
89#define STATUS_BUSY BIT(31)
90
91#define SD_EMMC_IRQ_EN 0x4c
92#define IRQ_EN_MASK 0x3fff
93#define IRQ_RXD_ERR_SHIFT 0
94#define IRQ_RXD_ERR_MASK 0xff
95#define IRQ_TXD_ERR BIT(8)
96#define IRQ_DESC_ERR BIT(9)
97#define IRQ_RESP_ERR BIT(10)
98#define IRQ_RESP_TIMEOUT BIT(11)
99#define IRQ_DESC_TIMEOUT BIT(12)
100#define IRQ_END_OF_CHAIN BIT(13)
101#define IRQ_RESP_STATUS BIT(14)
102#define IRQ_SDIO BIT(15)
103
104#define SD_EMMC_CMD_CFG 0x50
105#define SD_EMMC_CMD_ARG 0x54
106#define SD_EMMC_CMD_DAT 0x58
107#define SD_EMMC_CMD_RSP 0x5c
108#define SD_EMMC_CMD_RSP1 0x60
109#define SD_EMMC_CMD_RSP2 0x64
110#define SD_EMMC_CMD_RSP3 0x68
111
112#define SD_EMMC_RXD 0x94
113#define SD_EMMC_TXD 0x94
114#define SD_EMMC_LAST_REG SD_EMMC_TXD
115
116#define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
117#define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
118#define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
119#define MUX_CLK_NUM_PARENTS 2
120
121struct meson_host {
122 struct device *dev;
123 struct mmc_host *mmc;
124 struct mmc_request *mrq;
125 struct mmc_command *cmd;
126
127 spinlock_t lock;
128 void __iomem *regs;
129 int irq;
130 u32 ocr_mask;
131 struct clk *core_clk;
132 struct clk_mux mux;
133 struct clk *mux_clk;
134 struct clk *mux_parent[MUX_CLK_NUM_PARENTS];
Heiner Kallweit5da86882017-02-07 22:34:32 +0100135 unsigned long current_clock;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700136
137 struct clk_divider cfg_div;
138 struct clk *cfg_div_clk;
139
140 unsigned int bounce_buf_size;
141 void *bounce_buf;
142 dma_addr_t bounce_dma_addr;
143
144 bool vqmmc_enabled;
145};
146
147struct sd_emmc_desc {
148 u32 cmd_cfg;
149 u32 cmd_arg;
150 u32 cmd_data;
151 u32 cmd_resp;
152};
153#define CMD_CFG_LENGTH_SHIFT 0
154#define CMD_CFG_LENGTH_MASK 0x1ff
155#define CMD_CFG_BLOCK_MODE BIT(9)
156#define CMD_CFG_R1B BIT(10)
157#define CMD_CFG_END_OF_CHAIN BIT(11)
158#define CMD_CFG_TIMEOUT_SHIFT 12
159#define CMD_CFG_TIMEOUT_MASK 0xf
160#define CMD_CFG_NO_RESP BIT(16)
161#define CMD_CFG_NO_CMD BIT(17)
162#define CMD_CFG_DATA_IO BIT(18)
163#define CMD_CFG_DATA_WR BIT(19)
164#define CMD_CFG_RESP_NOCRC BIT(20)
165#define CMD_CFG_RESP_128 BIT(21)
166#define CMD_CFG_RESP_NUM BIT(22)
167#define CMD_CFG_DATA_NUM BIT(23)
168#define CMD_CFG_CMD_INDEX_SHIFT 24
169#define CMD_CFG_CMD_INDEX_MASK 0x3f
170#define CMD_CFG_ERROR BIT(30)
171#define CMD_CFG_OWNER BIT(31)
172
173#define CMD_DATA_MASK (~0x3)
174#define CMD_DATA_BIG_ENDIAN BIT(1)
175#define CMD_DATA_SRAM BIT(0)
176#define CMD_RESP_MASK (~0x1)
177#define CMD_RESP_SRAM BIT(0)
178
179static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
180{
181 struct mmc_host *mmc = host->mmc;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100182 int ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700183 u32 cfg;
184
185 if (clk_rate) {
186 if (WARN_ON(clk_rate > mmc->f_max))
187 clk_rate = mmc->f_max;
188 else if (WARN_ON(clk_rate < mmc->f_min))
189 clk_rate = mmc->f_min;
190 }
191
Heiner Kallweit5da86882017-02-07 22:34:32 +0100192 if (clk_rate == host->current_clock)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700193 return 0;
194
195 /* stop clock */
196 cfg = readl(host->regs + SD_EMMC_CFG);
197 if (!(cfg & CFG_STOP_CLOCK)) {
198 cfg |= CFG_STOP_CLOCK;
199 writel(cfg, host->regs + SD_EMMC_CFG);
200 }
201
202 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
203 mmc->actual_clock, clk_rate);
204
Heiner Kallweit5da86882017-02-07 22:34:32 +0100205 if (!clk_rate) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700206 mmc->actual_clock = 0;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100207 host->current_clock = 0;
208 /* return with clock being stopped */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700209 return 0;
210 }
211
212 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
Heiner Kallweit5da86882017-02-07 22:34:32 +0100213 if (ret) {
214 dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
215 clk_rate, ret);
216 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700217 }
218
Heiner Kallweit5da86882017-02-07 22:34:32 +0100219 mmc->actual_clock = clk_get_rate(host->cfg_div_clk);
220 host->current_clock = clk_rate;
221
222 if (clk_rate != mmc->actual_clock)
223 dev_dbg(host->dev,
224 "divider requested rate %lu != actual rate %u\n",
225 clk_rate, mmc->actual_clock);
226
227 /* (re)start clock */
228 cfg = readl(host->regs + SD_EMMC_CFG);
229 cfg &= ~CFG_STOP_CLOCK;
230 writel(cfg, host->regs + SD_EMMC_CFG);
231
232 return 0;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700233}
234
235/*
236 * The SD/eMMC IP block has an internal mux and divider used for
237 * generating the MMC clock. Use the clock framework to create and
238 * manage these clocks.
239 */
240static int meson_mmc_clk_init(struct meson_host *host)
241{
242 struct clk_init_data init;
243 char clk_name[32];
244 int i, ret = 0;
245 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
246 unsigned int mux_parent_count = 0;
247 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700248 u32 clk_reg, cfg;
249
250 /* get the mux parents */
251 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
252 char name[16];
253
254 snprintf(name, sizeof(name), "clkin%d", i);
255 host->mux_parent[i] = devm_clk_get(host->dev, name);
256 if (IS_ERR(host->mux_parent[i])) {
257 ret = PTR_ERR(host->mux_parent[i]);
258 if (PTR_ERR(host->mux_parent[i]) != -EPROBE_DEFER)
259 dev_err(host->dev, "Missing clock %s\n", name);
260 host->mux_parent[i] = NULL;
261 return ret;
262 }
263
Kevin Hilman51c5d842016-10-19 11:18:24 -0700264 mux_parent_names[i] = __clk_get_name(host->mux_parent[i]);
265 mux_parent_count++;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700266 }
267
Kevin Hilman51c5d842016-10-19 11:18:24 -0700268 /* create the mux */
269 snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
270 init.name = clk_name;
271 init.ops = &clk_mux_ops;
272 init.flags = 0;
273 init.parent_names = mux_parent_names;
274 init.num_parents = mux_parent_count;
275
276 host->mux.reg = host->regs + SD_EMMC_CLOCK;
277 host->mux.shift = CLK_SRC_SHIFT;
278 host->mux.mask = CLK_SRC_MASK;
279 host->mux.flags = 0;
280 host->mux.table = NULL;
281 host->mux.hw.init = &init;
282
283 host->mux_clk = devm_clk_register(host->dev, &host->mux.hw);
284 if (WARN_ON(IS_ERR(host->mux_clk)))
285 return PTR_ERR(host->mux_clk);
286
287 /* create the divider */
288 snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
289 init.name = devm_kstrdup(host->dev, clk_name, GFP_KERNEL);
290 init.ops = &clk_divider_ops;
291 init.flags = CLK_SET_RATE_PARENT;
292 clk_div_parents[0] = __clk_get_name(host->mux_clk);
293 init.parent_names = clk_div_parents;
294 init.num_parents = ARRAY_SIZE(clk_div_parents);
295
296 host->cfg_div.reg = host->regs + SD_EMMC_CLOCK;
297 host->cfg_div.shift = CLK_DIV_SHIFT;
298 host->cfg_div.width = CLK_DIV_WIDTH;
299 host->cfg_div.hw.init = &init;
300 host->cfg_div.flags = CLK_DIVIDER_ONE_BASED |
301 CLK_DIVIDER_ROUND_CLOSEST | CLK_DIVIDER_ALLOW_ZERO;
302
303 host->cfg_div_clk = devm_clk_register(host->dev, &host->cfg_div.hw);
304 if (WARN_ON(PTR_ERR_OR_ZERO(host->cfg_div_clk)))
305 return PTR_ERR(host->cfg_div_clk);
306
307 /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
308 clk_reg = 0;
309 clk_reg |= CLK_PHASE_180 << CLK_PHASE_SHIFT;
310 clk_reg |= CLK_SRC_XTAL << CLK_SRC_SHIFT;
311 clk_reg |= CLK_DIV_MAX << CLK_DIV_SHIFT;
312 clk_reg &= ~CLK_ALWAYS_ON;
313 writel(clk_reg, host->regs + SD_EMMC_CLOCK);
314
315 /* Ensure clock starts in "auto" mode, not "always on" */
316 cfg = readl(host->regs + SD_EMMC_CFG);
317 cfg &= ~CFG_CLK_ALWAYS_ON;
318 cfg |= CFG_AUTO_CLK;
319 writel(cfg, host->regs + SD_EMMC_CFG);
320
321 ret = clk_prepare_enable(host->cfg_div_clk);
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100322 if (ret)
323 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700324
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100325 /* Get the nearest minimum clock to 400KHz */
326 host->mmc->f_min = clk_round_rate(host->cfg_div_clk, 400000);
327
328 ret = meson_mmc_clk_set(host, host->mmc->f_min);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700329 if (!ret)
330 clk_disable_unprepare(host->cfg_div_clk);
331
332 return ret;
333}
334
335static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
336{
337 struct meson_host *host = mmc_priv(mmc);
338 u32 bus_width;
339 u32 val, orig;
340
341 /*
342 * GPIO regulator, only controls switching between 1v8 and
343 * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
344 */
345 switch (ios->power_mode) {
346 case MMC_POWER_OFF:
347 if (!IS_ERR(mmc->supply.vmmc))
348 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
349
350 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
351 regulator_disable(mmc->supply.vqmmc);
352 host->vqmmc_enabled = false;
353 }
354
355 break;
356
357 case MMC_POWER_UP:
358 if (!IS_ERR(mmc->supply.vmmc))
359 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
360 break;
361
362 case MMC_POWER_ON:
363 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
364 int ret = regulator_enable(mmc->supply.vqmmc);
365
366 if (ret < 0)
367 dev_err(mmc_dev(mmc),
368 "failed to enable vqmmc regulator\n");
369 else
370 host->vqmmc_enabled = true;
371 }
372
373 break;
374 }
375
376
377 meson_mmc_clk_set(host, ios->clock);
378
379 /* Bus width */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700380 switch (ios->bus_width) {
381 case MMC_BUS_WIDTH_1:
382 bus_width = CFG_BUS_WIDTH_1;
383 break;
384 case MMC_BUS_WIDTH_4:
385 bus_width = CFG_BUS_WIDTH_4;
386 break;
387 case MMC_BUS_WIDTH_8:
388 bus_width = CFG_BUS_WIDTH_8;
389 break;
390 default:
391 dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
392 ios->bus_width);
393 bus_width = CFG_BUS_WIDTH_4;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700394 }
395
396 val = readl(host->regs + SD_EMMC_CFG);
397 orig = val;
398
399 val &= ~(CFG_BUS_WIDTH_MASK << CFG_BUS_WIDTH_SHIFT);
400 val |= bus_width << CFG_BUS_WIDTH_SHIFT;
401
402 val &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
403 val |= ilog2(SD_EMMC_CFG_BLK_SIZE) << CFG_BLK_LEN_SHIFT;
404
405 val &= ~(CFG_RESP_TIMEOUT_MASK << CFG_RESP_TIMEOUT_SHIFT);
406 val |= ilog2(SD_EMMC_CFG_RESP_TIMEOUT) << CFG_RESP_TIMEOUT_SHIFT;
407
408 val &= ~(CFG_RC_CC_MASK << CFG_RC_CC_SHIFT);
409 val |= ilog2(SD_EMMC_CFG_CMD_GAP) << CFG_RC_CC_SHIFT;
410
411 writel(val, host->regs + SD_EMMC_CFG);
412
413 if (val != orig)
414 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
415 __func__, orig, val);
416}
417
418static int meson_mmc_request_done(struct mmc_host *mmc, struct mmc_request *mrq)
419{
420 struct meson_host *host = mmc_priv(mmc);
421
422 WARN_ON(host->mrq != mrq);
423
424 host->mrq = NULL;
425 host->cmd = NULL;
426 mmc_request_done(host->mmc, mrq);
427
428 return 0;
429}
430
431static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
432{
433 struct meson_host *host = mmc_priv(mmc);
434 struct sd_emmc_desc *desc, desc_tmp;
435 u32 cfg;
436 u8 blk_len, cmd_cfg_timeout;
437 unsigned int xfer_bytes = 0;
438
439 /* Setup descriptors */
440 dma_rmb();
441 desc = &desc_tmp;
442 memset(desc, 0, sizeof(struct sd_emmc_desc));
443
444 desc->cmd_cfg |= (cmd->opcode & CMD_CFG_CMD_INDEX_MASK) <<
445 CMD_CFG_CMD_INDEX_SHIFT;
446 desc->cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
447 desc->cmd_arg = cmd->arg;
448
449 /* Response */
450 if (cmd->flags & MMC_RSP_PRESENT) {
451 desc->cmd_cfg &= ~CMD_CFG_NO_RESP;
452 if (cmd->flags & MMC_RSP_136)
453 desc->cmd_cfg |= CMD_CFG_RESP_128;
454 desc->cmd_cfg |= CMD_CFG_RESP_NUM;
455 desc->cmd_resp = 0;
456
457 if (!(cmd->flags & MMC_RSP_CRC))
458 desc->cmd_cfg |= CMD_CFG_RESP_NOCRC;
459
460 if (cmd->flags & MMC_RSP_BUSY)
461 desc->cmd_cfg |= CMD_CFG_R1B;
462 } else {
463 desc->cmd_cfg |= CMD_CFG_NO_RESP;
464 }
465
466 /* data? */
467 if (cmd->data) {
468 desc->cmd_cfg |= CMD_CFG_DATA_IO;
469 if (cmd->data->blocks > 1) {
470 desc->cmd_cfg |= CMD_CFG_BLOCK_MODE;
471 desc->cmd_cfg |=
472 (cmd->data->blocks & CMD_CFG_LENGTH_MASK) <<
473 CMD_CFG_LENGTH_SHIFT;
474
475 /* check if block-size matches, if not update */
476 cfg = readl(host->regs + SD_EMMC_CFG);
477 blk_len = cfg & (CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
478 blk_len >>= CFG_BLK_LEN_SHIFT;
479 if (blk_len != ilog2(cmd->data->blksz)) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800480 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n",
Kevin Hilman51c5d842016-10-19 11:18:24 -0700481 __func__, blk_len,
Kevin Hilmandc012052017-01-25 16:01:39 -0800482 ilog2(cmd->data->blksz));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700483 blk_len = ilog2(cmd->data->blksz);
484 cfg &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
485 cfg |= blk_len << CFG_BLK_LEN_SHIFT;
486 writel(cfg, host->regs + SD_EMMC_CFG);
487 }
488 } else {
489 desc->cmd_cfg &= ~CMD_CFG_BLOCK_MODE;
490 desc->cmd_cfg |=
491 (cmd->data->blksz & CMD_CFG_LENGTH_MASK) <<
492 CMD_CFG_LENGTH_SHIFT;
493 }
494
495 cmd->data->bytes_xfered = 0;
496 xfer_bytes = cmd->data->blksz * cmd->data->blocks;
497 if (cmd->data->flags & MMC_DATA_WRITE) {
498 desc->cmd_cfg |= CMD_CFG_DATA_WR;
499 WARN_ON(xfer_bytes > host->bounce_buf_size);
500 sg_copy_to_buffer(cmd->data->sg, cmd->data->sg_len,
501 host->bounce_buf, xfer_bytes);
502 cmd->data->bytes_xfered = xfer_bytes;
503 dma_wmb();
504 } else {
505 desc->cmd_cfg &= ~CMD_CFG_DATA_WR;
506 }
507
508 if (xfer_bytes > 0) {
509 desc->cmd_cfg &= ~CMD_CFG_DATA_NUM;
510 desc->cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
511 } else {
512 /* write data to data_addr */
513 desc->cmd_cfg |= CMD_CFG_DATA_NUM;
514 desc->cmd_data = 0;
515 }
516
517 cmd_cfg_timeout = 12;
518 } else {
519 desc->cmd_cfg &= ~CMD_CFG_DATA_IO;
520 cmd_cfg_timeout = 10;
521 }
522 desc->cmd_cfg |= (cmd_cfg_timeout & CMD_CFG_TIMEOUT_MASK) <<
523 CMD_CFG_TIMEOUT_SHIFT;
524
525 host->cmd = cmd;
526
527 /* Last descriptor */
528 desc->cmd_cfg |= CMD_CFG_END_OF_CHAIN;
529 writel(desc->cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
530 writel(desc->cmd_data, host->regs + SD_EMMC_CMD_DAT);
531 writel(desc->cmd_resp, host->regs + SD_EMMC_CMD_RSP);
532 wmb(); /* ensure descriptor is written before kicked */
533 writel(desc->cmd_arg, host->regs + SD_EMMC_CMD_ARG);
534}
535
536static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
537{
538 struct meson_host *host = mmc_priv(mmc);
539
540 WARN_ON(host->mrq != NULL);
541
542 /* Stop execution */
543 writel(0, host->regs + SD_EMMC_START);
544
Kevin Hilman51c5d842016-10-19 11:18:24 -0700545 host->mrq = mrq;
546
547 if (mrq->sbc)
548 meson_mmc_start_cmd(mmc, mrq->sbc);
549 else
550 meson_mmc_start_cmd(mmc, mrq->cmd);
551}
552
553static int meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
554{
555 struct meson_host *host = mmc_priv(mmc);
556
557 if (cmd->flags & MMC_RSP_136) {
558 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
559 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
560 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
561 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
562 } else if (cmd->flags & MMC_RSP_PRESENT) {
563 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
564 }
565
566 return 0;
567}
568
569static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
570{
571 struct meson_host *host = dev_id;
572 struct mmc_request *mrq;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100573 struct mmc_command *cmd;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700574 u32 irq_en, status, raw_status;
575 irqreturn_t ret = IRQ_HANDLED;
576
577 if (WARN_ON(!host))
578 return IRQ_NONE;
579
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100580 cmd = host->cmd;
581
Kevin Hilman51c5d842016-10-19 11:18:24 -0700582 mrq = host->mrq;
583
584 if (WARN_ON(!mrq))
585 return IRQ_NONE;
586
587 if (WARN_ON(!cmd))
588 return IRQ_NONE;
589
590 spin_lock(&host->lock);
591 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
592 raw_status = readl(host->regs + SD_EMMC_STATUS);
593 status = raw_status & irq_en;
594
595 if (!status) {
596 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
597 raw_status, irq_en);
598 ret = IRQ_NONE;
599 goto out;
600 }
601
602 cmd->error = 0;
603 if (status & IRQ_RXD_ERR_MASK) {
604 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
605 cmd->error = -EILSEQ;
606 }
607 if (status & IRQ_TXD_ERR) {
608 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
609 cmd->error = -EILSEQ;
610 }
611 if (status & IRQ_DESC_ERR)
612 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
613 if (status & IRQ_RESP_ERR) {
614 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
615 cmd->error = -EILSEQ;
616 }
617 if (status & IRQ_RESP_TIMEOUT) {
618 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
619 cmd->error = -ETIMEDOUT;
620 }
621 if (status & IRQ_DESC_TIMEOUT) {
622 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
623 cmd->error = -ETIMEDOUT;
624 }
625 if (status & IRQ_SDIO)
626 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
627
628 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS))
629 ret = IRQ_WAKE_THREAD;
630 else {
631 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
632 status, cmd->opcode, cmd->arg,
633 cmd->flags, mrq->stop ? 1 : 0);
634 if (cmd->data) {
635 struct mmc_data *data = cmd->data;
636
637 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
638 data->blksz, data->blocks, data->flags,
639 data->flags & MMC_DATA_WRITE ? "write" : "",
640 data->flags & MMC_DATA_READ ? "read" : "");
641 }
642 }
643
644out:
645 /* ack all (enabled) interrupts */
646 writel(status, host->regs + SD_EMMC_STATUS);
647
648 if (ret == IRQ_HANDLED) {
649 meson_mmc_read_resp(host->mmc, cmd);
650 meson_mmc_request_done(host->mmc, cmd->mrq);
651 }
652
653 spin_unlock(&host->lock);
654 return ret;
655}
656
657static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
658{
659 struct meson_host *host = dev_id;
660 struct mmc_request *mrq = host->mrq;
661 struct mmc_command *cmd = host->cmd;
662 struct mmc_data *data;
663 unsigned int xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700664
665 if (WARN_ON(!mrq))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100666 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700667
668 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100669 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700670
671 data = cmd->data;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100672 if (data && data->flags & MMC_DATA_READ) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700673 xfer_bytes = data->blksz * data->blocks;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100674 WARN_ON(xfer_bytes > host->bounce_buf_size);
675 sg_copy_from_buffer(data->sg, data->sg_len,
676 host->bounce_buf, xfer_bytes);
677 data->bytes_xfered = xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700678 }
679
680 meson_mmc_read_resp(host->mmc, cmd);
681 if (!data || !data->stop || mrq->sbc)
682 meson_mmc_request_done(host->mmc, mrq);
683 else
684 meson_mmc_start_cmd(host->mmc, data->stop);
685
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100686 return IRQ_HANDLED;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700687}
688
689/*
690 * NOTE: we only need this until the GPIO/pinctrl driver can handle
691 * interrupts. For now, the MMC core will use this for polling.
692 */
693static int meson_mmc_get_cd(struct mmc_host *mmc)
694{
695 int status = mmc_gpio_get_cd(mmc);
696
697 if (status == -ENOSYS)
698 return 1; /* assume present */
699
700 return status;
701}
702
703static const struct mmc_host_ops meson_mmc_ops = {
704 .request = meson_mmc_request,
705 .set_ios = meson_mmc_set_ios,
706 .get_cd = meson_mmc_get_cd,
707};
708
709static int meson_mmc_probe(struct platform_device *pdev)
710{
711 struct resource *res;
712 struct meson_host *host;
713 struct mmc_host *mmc;
714 int ret;
715
716 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
717 if (!mmc)
718 return -ENOMEM;
719 host = mmc_priv(mmc);
720 host->mmc = mmc;
721 host->dev = &pdev->dev;
722 dev_set_drvdata(&pdev->dev, host);
723
724 spin_lock_init(&host->lock);
725
726 /* Get regulators and the supported OCR mask */
727 host->vqmmc_enabled = false;
728 ret = mmc_regulator_get_supply(mmc);
729 if (ret == -EPROBE_DEFER)
730 goto free_host;
731
732 ret = mmc_of_parse(mmc);
733 if (ret) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800734 if (ret != -EPROBE_DEFER)
735 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700736 goto free_host;
737 }
738
739 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
740 host->regs = devm_ioremap_resource(&pdev->dev, res);
741 if (IS_ERR(host->regs)) {
742 ret = PTR_ERR(host->regs);
743 goto free_host;
744 }
745
746 host->irq = platform_get_irq(pdev, 0);
747 if (host->irq == 0) {
748 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
749 ret = -EINVAL;
750 goto free_host;
751 }
752
753 host->core_clk = devm_clk_get(&pdev->dev, "core");
754 if (IS_ERR(host->core_clk)) {
755 ret = PTR_ERR(host->core_clk);
756 goto free_host;
757 }
758
759 ret = clk_prepare_enable(host->core_clk);
760 if (ret)
761 goto free_host;
762
763 ret = meson_mmc_clk_init(host);
764 if (ret)
765 goto free_host;
766
767 /* Stop execution */
768 writel(0, host->regs + SD_EMMC_START);
769
770 /* clear, ack, enable all interrupts */
771 writel(0, host->regs + SD_EMMC_IRQ_EN);
772 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
Heiner Kallweit92763b92017-02-07 22:34:51 +0100773 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700774
775 ret = devm_request_threaded_irq(&pdev->dev, host->irq,
776 meson_mmc_irq, meson_mmc_irq_thread,
777 IRQF_SHARED, DRIVER_NAME, host);
778 if (ret)
779 goto free_host;
780
Heiner Kallweitefe0b662017-02-07 22:34:58 +0100781 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
782 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
783
Kevin Hilman51c5d842016-10-19 11:18:24 -0700784 /* data bounce buffer */
785 host->bounce_buf_size = SZ_512K;
786 host->bounce_buf =
787 dma_alloc_coherent(host->dev, host->bounce_buf_size,
788 &host->bounce_dma_addr, GFP_KERNEL);
789 if (host->bounce_buf == NULL) {
790 dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
791 ret = -ENOMEM;
792 goto free_host;
793 }
794
795 mmc->ops = &meson_mmc_ops;
796 mmc_add_host(mmc);
797
798 return 0;
799
800free_host:
801 clk_disable_unprepare(host->cfg_div_clk);
802 clk_disable_unprepare(host->core_clk);
803 mmc_free_host(mmc);
804 return ret;
805}
806
807static int meson_mmc_remove(struct platform_device *pdev)
808{
809 struct meson_host *host = dev_get_drvdata(&pdev->dev);
810
811 if (WARN_ON(!host))
812 return 0;
813
Heiner Kallweit92763b92017-02-07 22:34:51 +0100814 /* disable interrupts */
815 writel(0, host->regs + SD_EMMC_IRQ_EN);
816
Kevin Hilman51c5d842016-10-19 11:18:24 -0700817 if (host->bounce_buf)
818 dma_free_coherent(host->dev, host->bounce_buf_size,
819 host->bounce_buf, host->bounce_dma_addr);
820
821 clk_disable_unprepare(host->cfg_div_clk);
822 clk_disable_unprepare(host->core_clk);
823
824 mmc_free_host(host->mmc);
825 return 0;
826}
827
828static const struct of_device_id meson_mmc_of_match[] = {
829 { .compatible = "amlogic,meson-gx-mmc", },
830 { .compatible = "amlogic,meson-gxbb-mmc", },
831 { .compatible = "amlogic,meson-gxl-mmc", },
832 { .compatible = "amlogic,meson-gxm-mmc", },
833 {}
834};
835MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
836
837static struct platform_driver meson_mmc_driver = {
838 .probe = meson_mmc_probe,
839 .remove = meson_mmc_remove,
840 .driver = {
841 .name = DRIVER_NAME,
842 .of_match_table = of_match_ptr(meson_mmc_of_match),
843 },
844};
845
846module_platform_driver(meson_mmc_driver);
847
848MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
849MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
850MODULE_LICENSE("GPL v2");