blob: ef2ce725c31d0c9914a219aa9e2f0cc396cd65b2 [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];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700135
136 struct clk_divider cfg_div;
137 struct clk *cfg_div_clk;
138
139 unsigned int bounce_buf_size;
140 void *bounce_buf;
141 dma_addr_t bounce_dma_addr;
142
143 bool vqmmc_enabled;
144};
145
146struct sd_emmc_desc {
147 u32 cmd_cfg;
148 u32 cmd_arg;
149 u32 cmd_data;
150 u32 cmd_resp;
151};
152#define CMD_CFG_LENGTH_SHIFT 0
153#define CMD_CFG_LENGTH_MASK 0x1ff
154#define CMD_CFG_BLOCK_MODE BIT(9)
155#define CMD_CFG_R1B BIT(10)
156#define CMD_CFG_END_OF_CHAIN BIT(11)
157#define CMD_CFG_TIMEOUT_SHIFT 12
158#define CMD_CFG_TIMEOUT_MASK 0xf
159#define CMD_CFG_NO_RESP BIT(16)
160#define CMD_CFG_NO_CMD BIT(17)
161#define CMD_CFG_DATA_IO BIT(18)
162#define CMD_CFG_DATA_WR BIT(19)
163#define CMD_CFG_RESP_NOCRC BIT(20)
164#define CMD_CFG_RESP_128 BIT(21)
165#define CMD_CFG_RESP_NUM BIT(22)
166#define CMD_CFG_DATA_NUM BIT(23)
167#define CMD_CFG_CMD_INDEX_SHIFT 24
168#define CMD_CFG_CMD_INDEX_MASK 0x3f
169#define CMD_CFG_ERROR BIT(30)
170#define CMD_CFG_OWNER BIT(31)
171
172#define CMD_DATA_MASK (~0x3)
173#define CMD_DATA_BIG_ENDIAN BIT(1)
174#define CMD_DATA_SRAM BIT(0)
175#define CMD_RESP_MASK (~0x1)
176#define CMD_RESP_SRAM BIT(0)
177
178static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
179{
180 struct mmc_host *mmc = host->mmc;
181 int ret = 0;
182 u32 cfg;
183
184 if (clk_rate) {
185 if (WARN_ON(clk_rate > mmc->f_max))
186 clk_rate = mmc->f_max;
187 else if (WARN_ON(clk_rate < mmc->f_min))
188 clk_rate = mmc->f_min;
189 }
190
191 if (clk_rate == mmc->actual_clock)
192 return 0;
193
194 /* stop clock */
195 cfg = readl(host->regs + SD_EMMC_CFG);
196 if (!(cfg & CFG_STOP_CLOCK)) {
197 cfg |= CFG_STOP_CLOCK;
198 writel(cfg, host->regs + SD_EMMC_CFG);
199 }
200
201 dev_dbg(host->dev, "change clock rate %u -> %lu\n",
202 mmc->actual_clock, clk_rate);
203
204 if (clk_rate == 0) {
205 mmc->actual_clock = 0;
206 return 0;
207 }
208
209 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
210 if (ret)
211 dev_warn(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
212 clk_rate, ret);
213 else if (clk_rate && clk_rate != clk_get_rate(host->cfg_div_clk))
214 dev_warn(host->dev, "divider requested rate %lu != actual rate %lu: ret=%d\n",
215 clk_rate, clk_get_rate(host->cfg_div_clk), ret);
216 else
217 mmc->actual_clock = clk_rate;
218
219 /* (re)start clock, if non-zero */
220 if (!ret && clk_rate) {
221 cfg = readl(host->regs + SD_EMMC_CFG);
222 cfg &= ~CFG_STOP_CLOCK;
223 writel(cfg, host->regs + SD_EMMC_CFG);
224 }
225
226 return ret;
227}
228
229/*
230 * The SD/eMMC IP block has an internal mux and divider used for
231 * generating the MMC clock. Use the clock framework to create and
232 * manage these clocks.
233 */
234static int meson_mmc_clk_init(struct meson_host *host)
235{
236 struct clk_init_data init;
237 char clk_name[32];
238 int i, ret = 0;
239 const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
240 unsigned int mux_parent_count = 0;
241 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700242 u32 clk_reg, cfg;
243
244 /* get the mux parents */
245 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
246 char name[16];
247
248 snprintf(name, sizeof(name), "clkin%d", i);
249 host->mux_parent[i] = devm_clk_get(host->dev, name);
250 if (IS_ERR(host->mux_parent[i])) {
251 ret = PTR_ERR(host->mux_parent[i]);
252 if (PTR_ERR(host->mux_parent[i]) != -EPROBE_DEFER)
253 dev_err(host->dev, "Missing clock %s\n", name);
254 host->mux_parent[i] = NULL;
255 return ret;
256 }
257
Kevin Hilman51c5d842016-10-19 11:18:24 -0700258 mux_parent_names[i] = __clk_get_name(host->mux_parent[i]);
259 mux_parent_count++;
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;
268 init.num_parents = mux_parent_count;
269
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));
283 init.name = devm_kstrdup(host->dev, clk_name, GFP_KERNEL);
284 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);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700323 if (!ret)
324 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 */
374 val = readl(host->regs + SD_EMMC_CFG);
375 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;
389 return;
390 }
391
392 val = readl(host->regs + SD_EMMC_CFG);
393 orig = val;
394
395 val &= ~(CFG_BUS_WIDTH_MASK << CFG_BUS_WIDTH_SHIFT);
396 val |= bus_width << CFG_BUS_WIDTH_SHIFT;
397
398 val &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
399 val |= ilog2(SD_EMMC_CFG_BLK_SIZE) << CFG_BLK_LEN_SHIFT;
400
401 val &= ~(CFG_RESP_TIMEOUT_MASK << CFG_RESP_TIMEOUT_SHIFT);
402 val |= ilog2(SD_EMMC_CFG_RESP_TIMEOUT) << CFG_RESP_TIMEOUT_SHIFT;
403
404 val &= ~(CFG_RC_CC_MASK << CFG_RC_CC_SHIFT);
405 val |= ilog2(SD_EMMC_CFG_CMD_GAP) << CFG_RC_CC_SHIFT;
406
407 writel(val, host->regs + SD_EMMC_CFG);
408
409 if (val != orig)
410 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
411 __func__, orig, val);
412}
413
414static int meson_mmc_request_done(struct mmc_host *mmc, struct mmc_request *mrq)
415{
416 struct meson_host *host = mmc_priv(mmc);
417
418 WARN_ON(host->mrq != mrq);
419
420 host->mrq = NULL;
421 host->cmd = NULL;
422 mmc_request_done(host->mmc, mrq);
423
424 return 0;
425}
426
427static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
428{
429 struct meson_host *host = mmc_priv(mmc);
430 struct sd_emmc_desc *desc, desc_tmp;
431 u32 cfg;
432 u8 blk_len, cmd_cfg_timeout;
433 unsigned int xfer_bytes = 0;
434
435 /* Setup descriptors */
436 dma_rmb();
437 desc = &desc_tmp;
438 memset(desc, 0, sizeof(struct sd_emmc_desc));
439
440 desc->cmd_cfg |= (cmd->opcode & CMD_CFG_CMD_INDEX_MASK) <<
441 CMD_CFG_CMD_INDEX_SHIFT;
442 desc->cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
443 desc->cmd_arg = cmd->arg;
444
445 /* Response */
446 if (cmd->flags & MMC_RSP_PRESENT) {
447 desc->cmd_cfg &= ~CMD_CFG_NO_RESP;
448 if (cmd->flags & MMC_RSP_136)
449 desc->cmd_cfg |= CMD_CFG_RESP_128;
450 desc->cmd_cfg |= CMD_CFG_RESP_NUM;
451 desc->cmd_resp = 0;
452
453 if (!(cmd->flags & MMC_RSP_CRC))
454 desc->cmd_cfg |= CMD_CFG_RESP_NOCRC;
455
456 if (cmd->flags & MMC_RSP_BUSY)
457 desc->cmd_cfg |= CMD_CFG_R1B;
458 } else {
459 desc->cmd_cfg |= CMD_CFG_NO_RESP;
460 }
461
462 /* data? */
463 if (cmd->data) {
464 desc->cmd_cfg |= CMD_CFG_DATA_IO;
465 if (cmd->data->blocks > 1) {
466 desc->cmd_cfg |= CMD_CFG_BLOCK_MODE;
467 desc->cmd_cfg |=
468 (cmd->data->blocks & CMD_CFG_LENGTH_MASK) <<
469 CMD_CFG_LENGTH_SHIFT;
470
471 /* check if block-size matches, if not update */
472 cfg = readl(host->regs + SD_EMMC_CFG);
473 blk_len = cfg & (CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
474 blk_len >>= CFG_BLK_LEN_SHIFT;
475 if (blk_len != ilog2(cmd->data->blksz)) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800476 dev_dbg(host->dev, "%s: update blk_len %d -> %d\n",
Kevin Hilman51c5d842016-10-19 11:18:24 -0700477 __func__, blk_len,
Kevin Hilmandc012052017-01-25 16:01:39 -0800478 ilog2(cmd->data->blksz));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700479 blk_len = ilog2(cmd->data->blksz);
480 cfg &= ~(CFG_BLK_LEN_MASK << CFG_BLK_LEN_SHIFT);
481 cfg |= blk_len << CFG_BLK_LEN_SHIFT;
482 writel(cfg, host->regs + SD_EMMC_CFG);
483 }
484 } else {
485 desc->cmd_cfg &= ~CMD_CFG_BLOCK_MODE;
486 desc->cmd_cfg |=
487 (cmd->data->blksz & CMD_CFG_LENGTH_MASK) <<
488 CMD_CFG_LENGTH_SHIFT;
489 }
490
491 cmd->data->bytes_xfered = 0;
492 xfer_bytes = cmd->data->blksz * cmd->data->blocks;
493 if (cmd->data->flags & MMC_DATA_WRITE) {
494 desc->cmd_cfg |= CMD_CFG_DATA_WR;
495 WARN_ON(xfer_bytes > host->bounce_buf_size);
496 sg_copy_to_buffer(cmd->data->sg, cmd->data->sg_len,
497 host->bounce_buf, xfer_bytes);
498 cmd->data->bytes_xfered = xfer_bytes;
499 dma_wmb();
500 } else {
501 desc->cmd_cfg &= ~CMD_CFG_DATA_WR;
502 }
503
504 if (xfer_bytes > 0) {
505 desc->cmd_cfg &= ~CMD_CFG_DATA_NUM;
506 desc->cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
507 } else {
508 /* write data to data_addr */
509 desc->cmd_cfg |= CMD_CFG_DATA_NUM;
510 desc->cmd_data = 0;
511 }
512
513 cmd_cfg_timeout = 12;
514 } else {
515 desc->cmd_cfg &= ~CMD_CFG_DATA_IO;
516 cmd_cfg_timeout = 10;
517 }
518 desc->cmd_cfg |= (cmd_cfg_timeout & CMD_CFG_TIMEOUT_MASK) <<
519 CMD_CFG_TIMEOUT_SHIFT;
520
521 host->cmd = cmd;
522
523 /* Last descriptor */
524 desc->cmd_cfg |= CMD_CFG_END_OF_CHAIN;
525 writel(desc->cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
526 writel(desc->cmd_data, host->regs + SD_EMMC_CMD_DAT);
527 writel(desc->cmd_resp, host->regs + SD_EMMC_CMD_RSP);
528 wmb(); /* ensure descriptor is written before kicked */
529 writel(desc->cmd_arg, host->regs + SD_EMMC_CMD_ARG);
530}
531
532static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
533{
534 struct meson_host *host = mmc_priv(mmc);
535
536 WARN_ON(host->mrq != NULL);
537
538 /* Stop execution */
539 writel(0, host->regs + SD_EMMC_START);
540
541 /* clear, ack, enable all interrupts */
542 writel(0, host->regs + SD_EMMC_IRQ_EN);
543 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
544 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
545
546 host->mrq = mrq;
547
548 if (mrq->sbc)
549 meson_mmc_start_cmd(mmc, mrq->sbc);
550 else
551 meson_mmc_start_cmd(mmc, mrq->cmd);
552}
553
554static int meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
555{
556 struct meson_host *host = mmc_priv(mmc);
557
558 if (cmd->flags & MMC_RSP_136) {
559 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
560 cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
561 cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
562 cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
563 } else if (cmd->flags & MMC_RSP_PRESENT) {
564 cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
565 }
566
567 return 0;
568}
569
570static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
571{
572 struct meson_host *host = dev_id;
573 struct mmc_request *mrq;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100574 struct mmc_command *cmd;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700575 u32 irq_en, status, raw_status;
576 irqreturn_t ret = IRQ_HANDLED;
577
578 if (WARN_ON(!host))
579 return IRQ_NONE;
580
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100581 cmd = host->cmd;
582
Kevin Hilman51c5d842016-10-19 11:18:24 -0700583 mrq = host->mrq;
584
585 if (WARN_ON(!mrq))
586 return IRQ_NONE;
587
588 if (WARN_ON(!cmd))
589 return IRQ_NONE;
590
591 spin_lock(&host->lock);
592 irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
593 raw_status = readl(host->regs + SD_EMMC_STATUS);
594 status = raw_status & irq_en;
595
596 if (!status) {
597 dev_warn(host->dev, "Spurious IRQ! status=0x%08x, irq_en=0x%08x\n",
598 raw_status, irq_en);
599 ret = IRQ_NONE;
600 goto out;
601 }
602
603 cmd->error = 0;
604 if (status & IRQ_RXD_ERR_MASK) {
605 dev_dbg(host->dev, "Unhandled IRQ: RXD error\n");
606 cmd->error = -EILSEQ;
607 }
608 if (status & IRQ_TXD_ERR) {
609 dev_dbg(host->dev, "Unhandled IRQ: TXD error\n");
610 cmd->error = -EILSEQ;
611 }
612 if (status & IRQ_DESC_ERR)
613 dev_dbg(host->dev, "Unhandled IRQ: Descriptor error\n");
614 if (status & IRQ_RESP_ERR) {
615 dev_dbg(host->dev, "Unhandled IRQ: Response error\n");
616 cmd->error = -EILSEQ;
617 }
618 if (status & IRQ_RESP_TIMEOUT) {
619 dev_dbg(host->dev, "Unhandled IRQ: Response timeout\n");
620 cmd->error = -ETIMEDOUT;
621 }
622 if (status & IRQ_DESC_TIMEOUT) {
623 dev_dbg(host->dev, "Unhandled IRQ: Descriptor timeout\n");
624 cmd->error = -ETIMEDOUT;
625 }
626 if (status & IRQ_SDIO)
627 dev_dbg(host->dev, "Unhandled IRQ: SDIO.\n");
628
629 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS))
630 ret = IRQ_WAKE_THREAD;
631 else {
632 dev_warn(host->dev, "Unknown IRQ! status=0x%04x: MMC CMD%u arg=0x%08x flags=0x%08x stop=%d\n",
633 status, cmd->opcode, cmd->arg,
634 cmd->flags, mrq->stop ? 1 : 0);
635 if (cmd->data) {
636 struct mmc_data *data = cmd->data;
637
638 dev_warn(host->dev, "\tblksz %u blocks %u flags 0x%08x (%s%s)",
639 data->blksz, data->blocks, data->flags,
640 data->flags & MMC_DATA_WRITE ? "write" : "",
641 data->flags & MMC_DATA_READ ? "read" : "");
642 }
643 }
644
645out:
646 /* ack all (enabled) interrupts */
647 writel(status, host->regs + SD_EMMC_STATUS);
648
649 if (ret == IRQ_HANDLED) {
650 meson_mmc_read_resp(host->mmc, cmd);
651 meson_mmc_request_done(host->mmc, cmd->mrq);
652 }
653
654 spin_unlock(&host->lock);
655 return ret;
656}
657
658static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
659{
660 struct meson_host *host = dev_id;
661 struct mmc_request *mrq = host->mrq;
662 struct mmc_command *cmd = host->cmd;
663 struct mmc_data *data;
664 unsigned int xfer_bytes;
665 int ret = IRQ_HANDLED;
666
667 if (WARN_ON(!mrq))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100668 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700669
670 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100671 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700672
673 data = cmd->data;
674 if (data) {
675 xfer_bytes = data->blksz * data->blocks;
676 if (data->flags & MMC_DATA_READ) {
677 WARN_ON(xfer_bytes > host->bounce_buf_size);
678 sg_copy_from_buffer(data->sg, data->sg_len,
679 host->bounce_buf, xfer_bytes);
680 data->bytes_xfered = xfer_bytes;
681 }
682 }
683
684 meson_mmc_read_resp(host->mmc, cmd);
685 if (!data || !data->stop || mrq->sbc)
686 meson_mmc_request_done(host->mmc, mrq);
687 else
688 meson_mmc_start_cmd(host->mmc, data->stop);
689
690 return ret;
691}
692
693/*
694 * NOTE: we only need this until the GPIO/pinctrl driver can handle
695 * interrupts. For now, the MMC core will use this for polling.
696 */
697static int meson_mmc_get_cd(struct mmc_host *mmc)
698{
699 int status = mmc_gpio_get_cd(mmc);
700
701 if (status == -ENOSYS)
702 return 1; /* assume present */
703
704 return status;
705}
706
707static const struct mmc_host_ops meson_mmc_ops = {
708 .request = meson_mmc_request,
709 .set_ios = meson_mmc_set_ios,
710 .get_cd = meson_mmc_get_cd,
711};
712
713static int meson_mmc_probe(struct platform_device *pdev)
714{
715 struct resource *res;
716 struct meson_host *host;
717 struct mmc_host *mmc;
718 int ret;
719
720 mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
721 if (!mmc)
722 return -ENOMEM;
723 host = mmc_priv(mmc);
724 host->mmc = mmc;
725 host->dev = &pdev->dev;
726 dev_set_drvdata(&pdev->dev, host);
727
728 spin_lock_init(&host->lock);
729
730 /* Get regulators and the supported OCR mask */
731 host->vqmmc_enabled = false;
732 ret = mmc_regulator_get_supply(mmc);
733 if (ret == -EPROBE_DEFER)
734 goto free_host;
735
736 ret = mmc_of_parse(mmc);
737 if (ret) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800738 if (ret != -EPROBE_DEFER)
739 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700740 goto free_host;
741 }
742
743 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
744 host->regs = devm_ioremap_resource(&pdev->dev, res);
745 if (IS_ERR(host->regs)) {
746 ret = PTR_ERR(host->regs);
747 goto free_host;
748 }
749
750 host->irq = platform_get_irq(pdev, 0);
751 if (host->irq == 0) {
752 dev_err(&pdev->dev, "failed to get interrupt resource.\n");
753 ret = -EINVAL;
754 goto free_host;
755 }
756
757 host->core_clk = devm_clk_get(&pdev->dev, "core");
758 if (IS_ERR(host->core_clk)) {
759 ret = PTR_ERR(host->core_clk);
760 goto free_host;
761 }
762
763 ret = clk_prepare_enable(host->core_clk);
764 if (ret)
765 goto free_host;
766
767 ret = meson_mmc_clk_init(host);
768 if (ret)
769 goto free_host;
770
771 /* Stop execution */
772 writel(0, host->regs + SD_EMMC_START);
773
774 /* clear, ack, enable all interrupts */
775 writel(0, host->regs + SD_EMMC_IRQ_EN);
776 writel(IRQ_EN_MASK, host->regs + SD_EMMC_STATUS);
777
778 ret = devm_request_threaded_irq(&pdev->dev, host->irq,
779 meson_mmc_irq, meson_mmc_irq_thread,
780 IRQF_SHARED, DRIVER_NAME, host);
781 if (ret)
782 goto free_host;
783
784 /* 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
814 if (host->bounce_buf)
815 dma_free_coherent(host->dev, host->bounce_buf_size,
816 host->bounce_buf, host->bounce_dma_addr);
817
818 clk_disable_unprepare(host->cfg_div_clk);
819 clk_disable_unprepare(host->core_clk);
820
821 mmc_free_host(host->mmc);
822 return 0;
823}
824
825static const struct of_device_id meson_mmc_of_match[] = {
826 { .compatible = "amlogic,meson-gx-mmc", },
827 { .compatible = "amlogic,meson-gxbb-mmc", },
828 { .compatible = "amlogic,meson-gxl-mmc", },
829 { .compatible = "amlogic,meson-gxm-mmc", },
830 {}
831};
832MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
833
834static struct platform_driver meson_mmc_driver = {
835 .probe = meson_mmc_probe,
836 .remove = meson_mmc_remove,
837 .driver = {
838 .name = DRIVER_NAME,
839 .of_match_table = of_match_ptr(meson_mmc_of_match),
840 },
841};
842
843module_platform_driver(meson_mmc_driver);
844
845MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
846MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
847MODULE_LICENSE("GPL v2");