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