blob: eb5ee68dbd469fd5f8f1eef9a8bc05a930bf3b22 [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>
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010039#include <linux/bitfield.h>
Kevin Hilman51c5d842016-10-19 11:18:24 -070040
41#define DRIVER_NAME "meson-gx-mmc"
42
43#define SD_EMMC_CLOCK 0x0
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010044#define CLK_DIV_MASK GENMASK(5, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070045#define CLK_DIV_MAX 63
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010046#define CLK_SRC_MASK GENMASK(7, 6)
Kevin Hilman51c5d842016-10-19 11:18:24 -070047#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
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010051#define CLK_CORE_PHASE_MASK GENMASK(9, 8)
Heiner Kallweitc08bcb62017-04-04 21:03:22 +020052#define CLK_TX_PHASE_MASK GENMASK(11, 10)
53#define CLK_RX_PHASE_MASK GENMASK(13, 12)
Kevin Hilman51c5d842016-10-19 11:18:24 -070054#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)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010066#define START_DESC_ADDR_MASK GENMASK(31, 2)
Kevin Hilman51c5d842016-10-19 11:18:24 -070067
68#define SD_EMMC_CFG 0x44
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010069#define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070070#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)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010074#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)
Kevin Hilman51c5d842016-10-19 11:18:24 -070077#define CFG_STOP_CLOCK BIT(22)
78#define CFG_CLK_ALWAYS_ON BIT(18)
Heiner Kallweite21e6fd2017-02-07 22:35:59 +010079#define CFG_CHK_DS BIT(20)
Kevin Hilman51c5d842016-10-19 11:18:24 -070080#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
Heiner Kallweit1231e7e2017-03-25 11:23:24 +010086#define IRQ_EN_MASK GENMASK(13, 0)
87#define IRQ_RXD_ERR_MASK GENMASK(7, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -070088#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 */
Heiner Kallweitbb11eff2017-03-04 13:37:46 +0100111#define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
112#define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700113#define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
114#define MUX_CLK_NUM_PARENTS 2
115
Heiner Kallweitc08bcb62017-04-04 21:03:22 +0200116struct meson_tuning_params {
117 u8 core_phase;
118 u8 tx_phase;
119 u8 rx_phase;
120};
121
Kevin Hilman51c5d842016-10-19 11:18:24 -0700122struct meson_host {
123 struct device *dev;
124 struct mmc_host *mmc;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700125 struct mmc_command *cmd;
126
127 spinlock_t lock;
128 void __iomem *regs;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700129 struct clk *core_clk;
130 struct clk_mux mux;
131 struct clk *mux_clk;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100132 unsigned long current_clock;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700133
134 struct clk_divider cfg_div;
135 struct clk *cfg_div_clk;
136
137 unsigned int bounce_buf_size;
138 void *bounce_buf;
139 dma_addr_t bounce_dma_addr;
140
Heiner Kallweitc08bcb62017-04-04 21:03:22 +0200141 struct meson_tuning_params tp;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700142 bool vqmmc_enabled;
143};
144
145struct sd_emmc_desc {
146 u32 cmd_cfg;
147 u32 cmd_arg;
148 u32 cmd_data;
149 u32 cmd_resp;
150};
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100151
152#define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700153#define CMD_CFG_BLOCK_MODE BIT(9)
154#define CMD_CFG_R1B BIT(10)
155#define CMD_CFG_END_OF_CHAIN BIT(11)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100156#define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700157#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)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100165#define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700166#define CMD_CFG_ERROR BIT(30)
167#define CMD_CFG_OWNER BIT(31)
168
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100169#define CMD_DATA_MASK GENMASK(31, 2)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700170#define CMD_DATA_BIG_ENDIAN BIT(1)
171#define CMD_DATA_SRAM BIT(0)
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100172#define CMD_RESP_MASK GENMASK(31, 1)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700173#define CMD_RESP_SRAM BIT(0)
174
Heiner Kallweit4eee86c2017-03-25 11:26:18 +0100175static 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
Heiner Kallweite5e4a3e2017-03-25 11:28:13 +0100187static 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
Kevin Hilman51c5d842016-10-19 11:18:24 -0700198static int meson_mmc_clk_set(struct meson_host *host, unsigned long clk_rate)
199{
200 struct mmc_host *mmc = host->mmc;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100201 int ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700202 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
Heiner Kallweit5da86882017-02-07 22:34:32 +0100211 if (clk_rate == host->current_clock)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700212 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
Heiner Kallweit5da86882017-02-07 22:34:32 +0100224 if (!clk_rate) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700225 mmc->actual_clock = 0;
Heiner Kallweit5da86882017-02-07 22:34:32 +0100226 host->current_clock = 0;
227 /* return with clock being stopped */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700228 return 0;
229 }
230
231 ret = clk_set_rate(host->cfg_div_clk, clk_rate);
Heiner Kallweit5da86882017-02-07 22:34:32 +0100232 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;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700236 }
237
Heiner Kallweit5da86882017-02-07 22:34:32 +0100238 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;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700252}
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 */
259static 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];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700265 const char *clk_div_parents[1];
Kevin Hilman51c5d842016-10-19 11:18:24 -0700266 u32 clk_reg, cfg;
267
268 /* get the mux parents */
269 for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100270 struct clk *clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700271 char name[16];
272
273 snprintf(name, sizeof(name), "clkin%d", i);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100274 clk = devm_clk_get(host->dev, name);
275 if (IS_ERR(clk)) {
276 if (clk != ERR_PTR(-EPROBE_DEFER))
Kevin Hilman51c5d842016-10-19 11:18:24 -0700277 dev_err(host->dev, "Missing clock %s\n", name);
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100278 return PTR_ERR(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700279 }
280
Heiner Kallweite9883ef2017-03-04 13:24:09 +0100281 mux_parent_names[i] = __clk_get_name(clk);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700282 }
283
Kevin Hilman51c5d842016-10-19 11:18:24 -0700284 /* 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;
Heiner Kallweit7558c112017-03-04 13:22:57 +0100290 init.num_parents = MUX_CLK_NUM_PARENTS;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700291 host->mux.reg = host->regs + SD_EMMC_CLOCK;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100292 host->mux.shift = __bf_shf(CLK_SRC_MASK);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700293 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));
Heiner Kallweit7b9ebad2017-03-04 13:26:24 +0100304 init.name = clk_name;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700305 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;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100312 host->cfg_div.shift = __bf_shf(CLK_DIV_MASK);
313 host->cfg_div.width = __builtin_popcountl(CLK_DIV_MASK);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700314 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;
Heiner Kallweitc08bcb62017-04-04 21:03:22 +0200324 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);
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100327 clk_reg |= FIELD_PREP(CLK_SRC_MASK, CLK_SRC_XTAL);
328 clk_reg |= FIELD_PREP(CLK_DIV_MASK, CLK_DIV_MAX);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700329 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);
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100339 if (ret)
340 return ret;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700341
Ulf Hanssona4c38c82017-02-08 12:36:20 +0100342 /* 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);
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100346 if (ret)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700347 clk_disable_unprepare(host->cfg_div_clk);
348
349 return ret;
350}
351
Heiner Kallweit0b6ed712017-04-04 21:11:17 +0200352static 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
Kevin Hilman51c5d842016-10-19 11:18:24 -0700377static 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 */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700422 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;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700436 }
437
438 val = readl(host->regs + SD_EMMC_CFG);
439 orig = val;
440
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100441 val &= ~CFG_BUS_WIDTH_MASK;
442 val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700443
Heiner Kallweite21e6fd2017-02-07 22:35:59 +0100444 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
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100454 if (val != orig) {
455 writel(val, host->regs + SD_EMMC_CFG);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700456 dev_dbg(host->dev, "%s: SD_EMMC_CFG: 0x%08x -> 0x%08x\n",
457 __func__, orig, val);
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100458 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700459}
460
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100461static void meson_mmc_request_done(struct mmc_host *mmc,
462 struct mmc_request *mrq)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700463{
464 struct meson_host *host = mmc_priv(mmc);
465
Kevin Hilman51c5d842016-10-19 11:18:24 -0700466 host->cmd = NULL;
467 mmc_request_done(host->mmc, mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700468}
469
Heiner Kallweit3d03f6a2017-03-27 21:57:11 +0200470static 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
Heiner Kallweit75c7fd92017-03-27 22:02:32 +0200495static 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
Kevin Hilman51c5d842016-10-19 11:18:24 -0700512static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
513{
514 struct meson_host *host = mmc_priv(mmc);
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100515 struct mmc_data *data = cmd->data;
Heiner Kallweit3d03f6a2017-03-27 21:57:11 +0200516 u32 cmd_cfg = 0, cmd_data = 0;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700517 unsigned int xfer_bytes = 0;
518
519 /* Setup descriptors */
520 dma_rmb();
Kevin Hilman51c5d842016-10-19 11:18:24 -0700521
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100522 cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
Heiner Kallweita322feb2017-03-22 22:33:47 +0100523 cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
Kevin Hilman51c5d842016-10-19 11:18:24 -0700524
Heiner Kallweit75c7fd92017-03-27 22:02:32 +0200525 meson_mmc_set_response_bits(cmd, &cmd_cfg);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700526
527 /* data? */
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100528 if (data) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100529 cmd_cfg |= CMD_CFG_DATA_IO;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100530 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
Heiner Kallweit4eee86c2017-03-25 11:26:18 +0100531 ilog2(meson_mmc_get_timeout_msecs(data)));
Heiner Kallweita744c6f2017-03-22 22:34:01 +0100532
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100533 if (data->blocks > 1) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100534 cmd_cfg |= CMD_CFG_BLOCK_MODE;
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100535 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
536 data->blocks);
Heiner Kallweit3d03f6a2017-03-27 21:57:11 +0200537 meson_mmc_set_blksz(mmc, data->blksz);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700538 } else {
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100539 cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700540 }
541
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100542 data->bytes_xfered = 0;
543 xfer_bytes = data->blksz * data->blocks;
544 if (data->flags & MMC_DATA_WRITE) {
Heiner Kallweita322feb2017-03-22 22:33:47 +0100545 cmd_cfg |= CMD_CFG_DATA_WR;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700546 WARN_ON(xfer_bytes > host->bounce_buf_size);
Heiner Kallweit00412dd2017-03-22 22:33:44 +0100547 sg_copy_to_buffer(data->sg, data->sg_len,
Kevin Hilman51c5d842016-10-19 11:18:24 -0700548 host->bounce_buf, xfer_bytes);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700549 dma_wmb();
Kevin Hilman51c5d842016-10-19 11:18:24 -0700550 }
551
Heiner Kallweita322feb2017-03-22 22:33:47 +0100552 cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700553 } else {
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100554 cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
555 ilog2(SD_EMMC_CMD_TIMEOUT));
Kevin Hilman51c5d842016-10-19 11:18:24 -0700556 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700557
558 host->cmd = cmd;
559
560 /* Last descriptor */
Heiner Kallweita322feb2017-03-22 22:33:47 +0100561 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);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700565 wmb(); /* ensure descriptor is written before kicked */
Heiner Kallweita322feb2017-03-22 22:33:47 +0100566 writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700567}
568
569static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
570{
571 struct meson_host *host = mmc_priv(mmc);
572
Kevin Hilman51c5d842016-10-19 11:18:24 -0700573 /* Stop execution */
574 writel(0, host->regs + SD_EMMC_START);
575
Kevin Hilman51c5d842016-10-19 11:18:24 -0700576 if (mrq->sbc)
577 meson_mmc_start_cmd(mmc, mrq->sbc);
578 else
579 meson_mmc_start_cmd(mmc, mrq->cmd);
580}
581
Heiner Kallweit3d6c9912017-03-04 13:20:44 +0100582static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700583{
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 }
Kevin Hilman51c5d842016-10-19 11:18:24 -0700594}
595
596static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
597{
598 struct meson_host *host = dev_id;
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100599 struct mmc_command *cmd;
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100600 struct mmc_data *data;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700601 u32 irq_en, status, raw_status;
602 irqreturn_t ret = IRQ_HANDLED;
603
604 if (WARN_ON(!host))
605 return IRQ_NONE;
606
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100607 cmd = host->cmd;
608
Kevin Hilman51c5d842016-10-19 11:18:24 -0700609 if (WARN_ON(!cmd))
610 return IRQ_NONE;
611
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100612 data = cmd->data;
613
Kevin Hilman51c5d842016-10-19 11:18:24 -0700614 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
Heiner Kallweit1f8066d2017-03-22 22:33:50 +0100626 meson_mmc_read_resp(host->mmc, cmd);
627
Kevin Hilman51c5d842016-10-19 11:18:24 -0700628 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
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100654 if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
655 if (data && !cmd->error)
656 data->bytes_xfered = data->blksz * data->blocks;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700657 ret = IRQ_WAKE_THREAD;
Heiner Kallweit2c8d96a2017-03-22 22:33:53 +0100658 } else {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700659 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,
Heiner Kallweit7cdcc482017-03-04 13:36:45 +0100661 cmd->flags, cmd->mrq->stop ? 1 : 0);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700662 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
672out:
673 /* ack all (enabled) interrupts */
674 writel(status, host->regs + SD_EMMC_STATUS);
675
Heiner Kallweit1f8066d2017-03-22 22:33:50 +0100676 if (ret == IRQ_HANDLED)
Kevin Hilman51c5d842016-10-19 11:18:24 -0700677 meson_mmc_request_done(host->mmc, cmd->mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700678
679 spin_unlock(&host->lock);
680 return ret;
681}
682
683static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
684{
685 struct meson_host *host = dev_id;
Heiner Kallweite5e4a3e2017-03-25 11:28:13 +0100686 struct mmc_command *next_cmd, *cmd = host->cmd;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700687 struct mmc_data *data;
688 unsigned int xfer_bytes;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700689
Kevin Hilman51c5d842016-10-19 11:18:24 -0700690 if (WARN_ON(!cmd))
Heinrich Schuchardt19a91dd2016-12-23 16:01:08 +0100691 return IRQ_NONE;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700692
693 data = cmd->data;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100694 if (data && data->flags & MMC_DATA_READ) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700695 xfer_bytes = data->blksz * data->blocks;
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100696 WARN_ON(xfer_bytes > host->bounce_buf_size);
697 sg_copy_from_buffer(data->sg, data->sg_len,
698 host->bounce_buf, xfer_bytes);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700699 }
700
Heiner Kallweite5e4a3e2017-03-25 11:28:13 +0100701 next_cmd = meson_mmc_get_next_command(cmd);
702 if (next_cmd)
703 meson_mmc_start_cmd(host->mmc, next_cmd);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700704 else
Heiner Kallweite5e4a3e2017-03-25 11:28:13 +0100705 meson_mmc_request_done(host->mmc, cmd->mrq);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700706
Heiner Kallweit690f90b2017-02-07 22:34:41 +0100707 return IRQ_HANDLED;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700708}
709
Heiner Kallweit0b6ed712017-04-04 21:11:17 +0200710static 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
Kevin Hilman51c5d842016-10-19 11:18:24 -0700733/*
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 */
737static 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
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100747static void meson_mmc_cfg_init(struct meson_host *host)
748{
749 u32 cfg = 0;
750
Heiner Kallweit1231e7e2017-03-25 11:23:24 +0100751 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));
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100755
756 writel(cfg, host->regs + SD_EMMC_CFG);
757}
758
Kevin Hilman51c5d842016-10-19 11:18:24 -0700759static 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,
Heiner Kallweit0b6ed712017-04-04 21:11:17 +0200763 .execute_tuning = meson_mmc_execute_tuning,
Kevin Hilman51c5d842016-10-19 11:18:24 -0700764};
765
766static int meson_mmc_probe(struct platform_device *pdev)
767{
768 struct resource *res;
769 struct meson_host *host;
770 struct mmc_host *mmc;
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100771 int ret, irq;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700772
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) {
Kevin Hilmandc012052017-01-25 16:01:39 -0800791 if (ret != -EPROBE_DEFER)
792 dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700793 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
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100803 irq = platform_get_irq(pdev, 0);
804 if (!irq) {
Kevin Hilman51c5d842016-10-19 11:18:24 -0700805 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
Heiner Kallweitc08bcb62017-04-04 21:03:22 +0200820 host->tp.core_phase = CLK_PHASE_180;
821 host->tp.tx_phase = CLK_PHASE_0;
822 host->tp.rx_phase = CLK_PHASE_0;
823
Kevin Hilman51c5d842016-10-19 11:18:24 -0700824 ret = meson_mmc_clk_init(host);
825 if (ret)
Michał Zegance473d52017-03-14 21:05:20 +0100826 goto err_core_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700827
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);
Heiner Kallweit92763b92017-02-07 22:34:51 +0100834 writel(IRQ_EN_MASK, host->regs + SD_EMMC_IRQ_EN);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700835
Heiner Kallweitc01d1212017-03-04 13:35:13 +0100836 /* set config to sane default */
837 meson_mmc_cfg_init(host);
838
Heiner Kallweit9a1da4d2017-03-04 13:21:54 +0100839 ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
840 meson_mmc_irq_thread, IRQF_SHARED,
Heiner Kallweitf016c672017-03-25 11:24:41 +0100841 NULL, host);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700842 if (ret)
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100843 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700844
Heiner Kallweite5e4a3e2017-03-25 11:28:13 +0100845 mmc->caps |= MMC_CAP_CMD23;
Heiner Kallweitefe0b662017-02-07 22:34:58 +0100846 mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
847 mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
848
Kevin Hilman51c5d842016-10-19 11:18:24 -0700849 /* data bounce buffer */
Heiner Kallweit4136fcb2017-02-07 22:35:02 +0100850 host->bounce_buf_size = mmc->max_req_size;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700851 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;
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100857 goto err_div_clk;
Kevin Hilman51c5d842016-10-19 11:18:24 -0700858 }
859
860 mmc->ops = &meson_mmc_ops;
861 mmc_add_host(mmc);
862
863 return 0;
864
Heiner Kallweitcac3a472017-03-04 13:25:14 +0100865err_div_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700866 clk_disable_unprepare(host->cfg_div_clk);
Michał Zegance473d52017-03-14 21:05:20 +0100867err_core_clk:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700868 clk_disable_unprepare(host->core_clk);
Michał Zegance473d52017-03-14 21:05:20 +0100869free_host:
Kevin Hilman51c5d842016-10-19 11:18:24 -0700870 mmc_free_host(mmc);
871 return ret;
872}
873
874static int meson_mmc_remove(struct platform_device *pdev)
875{
876 struct meson_host *host = dev_get_drvdata(&pdev->dev);
877
Michał Zegana01fc2a2017-02-18 18:06:47 +0100878 mmc_remove_host(host->mmc);
879
Heiner Kallweit92763b92017-02-07 22:34:51 +0100880 /* disable interrupts */
881 writel(0, host->regs + SD_EMMC_IRQ_EN);
882
Heiner Kallweit62d721a2017-02-07 22:35:40 +0100883 dma_free_coherent(host->dev, host->bounce_buf_size,
884 host->bounce_buf, host->bounce_dma_addr);
Kevin Hilman51c5d842016-10-19 11:18:24 -0700885
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
893static 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};
900MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
901
902static 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
911module_platform_driver(meson_mmc_driver);
912
913MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
914MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
915MODULE_LICENSE("GPL v2");