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