blob: 8c95229bf77aceb9d17c21f2c99e359d57eaae2c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matt Waddel23b93e12011-04-16 11:54:07 +00002/*
3 * ARM PrimeCell MultiMedia Card Interface - PL180
4 *
5 * Copyright (C) ST-Ericsson SA 2010
6 *
7 * Author: Ulf Hansson <ulf.hansson@stericsson.com>
8 * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com>
9 * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org>
Matt Waddel23b93e12011-04-16 11:54:07 +000010 */
11
12/* #define DEBUG */
13
Matt Waddel23b93e12011-04-16 11:54:07 +000014#include "common.h"
Patrice Chotard5f256fe2017-10-23 10:57:33 +020015#include <clk.h>
Matt Waddel23b93e12011-04-16 11:54:07 +000016#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Matt Waddel23b93e12011-04-16 11:54:07 +000018#include <malloc.h>
Patrice Chotard3c0dbed2017-10-23 10:57:31 +020019#include <mmc.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Patrice Chotard3c0dbed2017-10-23 10:57:31 +020021
Patrice Chotard3c0dbed2017-10-23 10:57:31 +020022#include <asm/io.h>
Patrice Chotard5829fe22017-10-23 10:57:34 +020023#include <asm-generic/gpio.h>
24
25#include "arm_pl180_mmci.h"
Patrice Chotard3c0dbed2017-10-23 10:57:31 +020026
27#ifdef CONFIG_DM_MMC
28#include <dm.h>
Patrice Chotard3c0dbed2017-10-23 10:57:31 +020029#define MMC_CLOCK_MAX 48000000
30#define MMC_CLOCK_MIN 400000
31
32struct arm_pl180_mmc_plat {
33 struct mmc_config cfg;
34 struct mmc mmc;
35};
36#endif
Matt Waddel23b93e12011-04-16 11:54:07 +000037
Matt Waddel23b93e12011-04-16 11:54:07 +000038static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
39{
40 u32 hoststatus, statusmask;
John Rigby10ed93d2012-07-31 08:59:31 +000041 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +000042
43 statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL;
44 if ((cmd->resp_type & MMC_RSP_PRESENT))
45 statusmask |= SDI_STA_CMDREND;
46 else
47 statusmask |= SDI_STA_CMDSENT;
48
49 do
50 hoststatus = readl(&host->base->status) & statusmask;
51 while (!hoststatus);
52
53 writel(statusmask, &host->base->status_clear);
54 if (hoststatus & SDI_STA_CTIMEOUT) {
John Rigby10ed93d2012-07-31 08:59:31 +000055 debug("CMD%d time out\n", cmd->cmdidx);
Jaehoon Chung915ffa52016-07-19 16:33:36 +090056 return -ETIMEDOUT;
Matt Waddel23b93e12011-04-16 11:54:07 +000057 } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
Andy Fleming95b01c42012-09-06 15:23:13 -050058 (cmd->resp_type & MMC_RSP_CRC)) {
Matt Waddel23b93e12011-04-16 11:54:07 +000059 printf("CMD%d CRC error\n", cmd->cmdidx);
60 return -EILSEQ;
61 }
62
63 if (cmd->resp_type & MMC_RSP_PRESENT) {
64 cmd->response[0] = readl(&host->base->response0);
65 cmd->response[1] = readl(&host->base->response1);
66 cmd->response[2] = readl(&host->base->response2);
67 cmd->response[3] = readl(&host->base->response3);
68 debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, "
69 "response[2]:0x%08X, response[3]:0x%08X\n",
70 cmd->cmdidx, cmd->response[0], cmd->response[1],
71 cmd->response[2], cmd->response[3]);
72 }
73
74 return 0;
75}
76
77/* send command to the mmc card and wait for results */
78static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
79{
80 int result;
81 u32 sdi_cmd = 0;
John Rigby10ed93d2012-07-31 08:59:31 +000082 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +000083
84 sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN);
85
86 if (cmd->resp_type) {
87 sdi_cmd |= SDI_CMD_WAITRESP;
88 if (cmd->resp_type & MMC_RSP_136)
89 sdi_cmd |= SDI_CMD_LONGRESP;
90 }
91
92 writel((u32)cmd->cmdarg, &host->base->argument);
93 udelay(COMMAND_REG_DELAY);
94 writel(sdi_cmd, &host->base->command);
95 result = wait_for_command_end(dev, cmd);
96
97 /* After CMD2 set RCA to a none zero value. */
98 if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID))
99 dev->rca = 10;
100
101 /* After CMD3 open drain is switched off and push pull is used. */
102 if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) {
103 u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD;
104 writel(sdi_pwr, &host->base->power);
105 }
106
107 return result;
108}
109
110static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
111{
112 u32 *tempbuff = dest;
Matt Waddel23b93e12011-04-16 11:54:07 +0000113 u64 xfercount = blkcount * blksize;
John Rigby10ed93d2012-07-31 08:59:31 +0000114 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +0000115 u32 status, status_err;
116
117 debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
118
119 status = readl(&host->base->status);
120 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
121 SDI_STA_RXOVERR);
Matt Waddel23b93e12011-04-16 11:54:07 +0000122 while ((!status_err) && (xfercount >= sizeof(u32))) {
123 if (status & SDI_STA_RXDAVL) {
124 *(tempbuff) = readl(&host->base->fifo);
125 tempbuff++;
126 xfercount -= sizeof(u32);
127 }
128 status = readl(&host->base->status);
129 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
130 SDI_STA_RXOVERR);
131 }
132
133 status_err = status &
134 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
135 SDI_STA_RXOVERR);
136 while (!status_err) {
137 status = readl(&host->base->status);
138 status_err = status &
139 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
140 SDI_STA_RXOVERR);
141 }
142
143 if (status & SDI_STA_DTIMEOUT) {
144 printf("Read data timed out, xfercount: %llu, status: 0x%08X\n",
145 xfercount, status);
146 return -ETIMEDOUT;
147 } else if (status & SDI_STA_DCRCFAIL) {
148 printf("Read data bytes CRC error: 0x%x\n", status);
149 return -EILSEQ;
150 } else if (status & SDI_STA_RXOVERR) {
151 printf("Read data RX overflow error\n");
152 return -EIO;
153 }
154
155 writel(SDI_ICR_MASK, &host->base->status_clear);
156
157 if (xfercount) {
158 printf("Read data error, xfercount: %llu\n", xfercount);
159 return -ENOBUFS;
160 }
161
162 return 0;
163}
164
165static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize)
166{
167 u32 *tempbuff = src;
168 int i;
169 u64 xfercount = blkcount * blksize;
John Rigby10ed93d2012-07-31 08:59:31 +0000170 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +0000171 u32 status, status_err;
172
173 debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
174
175 status = readl(&host->base->status);
176 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
177 while (!status_err && xfercount) {
178 if (status & SDI_STA_TXFIFOBW) {
179 if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) {
180 for (i = 0; i < SDI_FIFO_BURST_SIZE; i++)
181 writel(*(tempbuff + i),
182 &host->base->fifo);
183 tempbuff += SDI_FIFO_BURST_SIZE;
184 xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32);
185 } else {
186 while (xfercount >= sizeof(u32)) {
187 writel(*(tempbuff), &host->base->fifo);
188 tempbuff++;
189 xfercount -= sizeof(u32);
190 }
191 }
192 }
193 status = readl(&host->base->status);
194 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
195 }
196
197 status_err = status &
198 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
199 while (!status_err) {
200 status = readl(&host->base->status);
201 status_err = status &
202 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
203 }
204
205 if (status & SDI_STA_DTIMEOUT) {
206 printf("Write data timed out, xfercount:%llu,status:0x%08X\n",
207 xfercount, status);
208 return -ETIMEDOUT;
209 } else if (status & SDI_STA_DCRCFAIL) {
210 printf("Write data CRC error\n");
211 return -EILSEQ;
212 }
213
214 writel(SDI_ICR_MASK, &host->base->status_clear);
215
216 if (xfercount) {
217 printf("Write data error, xfercount:%llu", xfercount);
218 return -ENOBUFS;
219 }
220
221 return 0;
222}
223
224static int do_data_transfer(struct mmc *dev,
225 struct mmc_cmd *cmd,
226 struct mmc_data *data)
227{
228 int error = -ETIMEDOUT;
John Rigby10ed93d2012-07-31 08:59:31 +0000229 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +0000230 u32 blksz = 0;
231 u32 data_ctrl = 0;
232 u32 data_len = (u32) (data->blocks * data->blocksize);
233
John Rigby10ed93d2012-07-31 08:59:31 +0000234 if (!host->version2) {
235 blksz = (ffs(data->blocksize) - 1);
236 data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK);
237 } else {
238 blksz = data->blocksize;
239 data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT);
240 }
241 data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE;
Matt Waddel23b93e12011-04-16 11:54:07 +0000242
243 writel(SDI_DTIMER_DEFAULT, &host->base->datatimer);
244 writel(data_len, &host->base->datalength);
245 udelay(DATA_REG_DELAY);
246
247 if (data->flags & MMC_DATA_READ) {
248 data_ctrl |= SDI_DCTRL_DTDIR_IN;
249 writel(data_ctrl, &host->base->datactrl);
250
251 error = do_command(dev, cmd);
252 if (error)
253 return error;
254
255 error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks,
256 (u32)data->blocksize);
257 } else if (data->flags & MMC_DATA_WRITE) {
258 error = do_command(dev, cmd);
259 if (error)
260 return error;
261
262 writel(data_ctrl, &host->base->datactrl);
263 error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks,
John Rigby10ed93d2012-07-31 08:59:31 +0000264 (u32)data->blocksize);
Matt Waddel23b93e12011-04-16 11:54:07 +0000265 }
266
267 return error;
268}
269
270static int host_request(struct mmc *dev,
271 struct mmc_cmd *cmd,
272 struct mmc_data *data)
273{
274 int result;
275
276 if (data)
277 result = do_data_transfer(dev, cmd, data);
278 else
279 result = do_command(dev, cmd);
280
281 return result;
282}
283
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900284static int host_set_ios(struct mmc *dev)
Matt Waddel23b93e12011-04-16 11:54:07 +0000285{
John Rigby10ed93d2012-07-31 08:59:31 +0000286 struct pl180_mmc_host *host = dev->priv;
Matt Waddel23b93e12011-04-16 11:54:07 +0000287 u32 sdi_clkcr;
288
289 sdi_clkcr = readl(&host->base->clock);
290
291 /* Ramp up the clock rate */
292 if (dev->clock) {
293 u32 clkdiv = 0;
John Rigby10ed93d2012-07-31 08:59:31 +0000294 u32 tmp_clock;
Matt Waddel23b93e12011-04-16 11:54:07 +0000295
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200296 if (dev->clock >= dev->cfg->f_max) {
John Rigby10ed93d2012-07-31 08:59:31 +0000297 clkdiv = 0;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200298 dev->clock = dev->cfg->f_max;
John Rigby10ed93d2012-07-31 08:59:31 +0000299 } else {
300 clkdiv = (host->clock_in / dev->clock) - 2;
301 }
Matt Waddel23b93e12011-04-16 11:54:07 +0000302
John Rigby10ed93d2012-07-31 08:59:31 +0000303 tmp_clock = host->clock_in / (clkdiv + 2);
304 while (tmp_clock > dev->clock) {
305 clkdiv++;
306 tmp_clock = host->clock_in / (clkdiv + 2);
307 }
Matt Waddel23b93e12011-04-16 11:54:07 +0000308
309 if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
310 clkdiv = SDI_CLKCR_CLKDIV_MASK;
311
John Rigby10ed93d2012-07-31 08:59:31 +0000312 tmp_clock = host->clock_in / (clkdiv + 2);
313 dev->clock = tmp_clock;
Matt Waddel23b93e12011-04-16 11:54:07 +0000314 sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK);
315 sdi_clkcr |= clkdiv;
316 }
317
318 /* Set the bus width */
319 if (dev->bus_width) {
320 u32 buswidth = 0;
321
322 switch (dev->bus_width) {
323 case 1:
324 buswidth |= SDI_CLKCR_WIDBUS_1;
325 break;
326 case 4:
327 buswidth |= SDI_CLKCR_WIDBUS_4;
328 break;
John Rigby10ed93d2012-07-31 08:59:31 +0000329 case 8:
330 buswidth |= SDI_CLKCR_WIDBUS_8;
331 break;
Matt Waddel23b93e12011-04-16 11:54:07 +0000332 default:
John Rigby10ed93d2012-07-31 08:59:31 +0000333 printf("Invalid bus width: %d\n", dev->bus_width);
Matt Waddel23b93e12011-04-16 11:54:07 +0000334 break;
335 }
336 sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK);
337 sdi_clkcr |= buswidth;
338 }
339
340 writel(sdi_clkcr, &host->base->clock);
341 udelay(CLK_CHANGE_DELAY);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900342
343 return 0;
Matt Waddel23b93e12011-04-16 11:54:07 +0000344}
345
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200346#ifndef CONFIG_DM_MMC
347/* MMC uses open drain drivers in the enumeration phase */
348static int mmc_host_reset(struct mmc *dev)
349{
350 struct pl180_mmc_host *host = dev->priv;
351
352 writel(host->pwr_init, &host->base->power);
353
354 return 0;
355}
356
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200357static const struct mmc_ops arm_pl180_mmci_ops = {
358 .send_cmd = host_request,
359 .set_ios = host_set_ios,
360 .init = mmc_host_reset,
361};
362
Matt Waddel23b93e12011-04-16 11:54:07 +0000363/*
364 * mmc_host_init - initialize the mmc controller.
365 * Set initial clock and power for mmc slot.
366 * Initialize mmc struct and register with mmc framework.
367 */
Patrice Chotard80150932018-07-25 17:49:07 +0200368
Patrice Chotardcb0060e2017-10-23 10:57:30 +0200369int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
Matt Waddel23b93e12011-04-16 11:54:07 +0000370{
Matt Waddel23b93e12011-04-16 11:54:07 +0000371 u32 sdi_u32;
372
John Rigby10ed93d2012-07-31 08:59:31 +0000373 writel(host->pwr_init, &host->base->power);
374 writel(host->clkdiv_init, &host->base->clock);
Matt Waddel23b93e12011-04-16 11:54:07 +0000375 udelay(CLK_CHANGE_DELAY);
376
377 /* Disable mmc interrupts */
378 sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
379 writel(sdi_u32, &host->base->mask0);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200380
381 host->cfg.name = host->name;
382 host->cfg.ops = &arm_pl180_mmci_ops;
Patrice Chotard80150932018-07-25 17:49:07 +0200383
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200384 /* TODO remove the duplicates */
385 host->cfg.host_caps = host->caps;
386 host->cfg.voltages = host->voltages;
387 host->cfg.f_min = host->clock_min;
388 host->cfg.f_max = host->clock_max;
389 if (host->b_max != 0)
390 host->cfg.b_max = host->b_max;
391 else
392 host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
393
Patrice Chotardcb0060e2017-10-23 10:57:30 +0200394 *mmc = mmc_create(&host->cfg, host);
395 if (!*mmc)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200396 return -1;
Patrice Chotardcb0060e2017-10-23 10:57:30 +0200397 debug("registered mmc interface number is:%d\n",
398 (*mmc)->block_dev.devnum);
Matt Waddel23b93e12011-04-16 11:54:07 +0000399
400 return 0;
401}
Patrice Chotard80150932018-07-25 17:49:07 +0200402#endif
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200403
404#ifdef CONFIG_DM_MMC
Patrice Chotard80150932018-07-25 17:49:07 +0200405static void arm_pl180_mmc_init(struct pl180_mmc_host *host)
406{
407 u32 sdi_u32;
408
409 writel(host->pwr_init, &host->base->power);
410 writel(host->clkdiv_init, &host->base->clock);
411 udelay(CLK_CHANGE_DELAY);
412
413 /* Disable mmc interrupts */
414 sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
415 writel(sdi_u32, &host->base->mask0);
416}
417
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200418static int arm_pl180_mmc_probe(struct udevice *dev)
419{
420 struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev);
421 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
422 struct mmc *mmc = &pdata->mmc;
Patrice Chotard80150932018-07-25 17:49:07 +0200423 struct pl180_mmc_host *host = dev->priv;
424 struct mmc_config *cfg = &pdata->cfg;
Patrice Chotard5f256fe2017-10-23 10:57:33 +0200425 struct clk clk;
Patrice Chotard9035bb72017-10-23 10:57:32 +0200426 u32 bus_width;
Patrice Chotard6f41d1a2018-12-05 14:04:32 +0100427 u32 periphid;
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200428 int ret;
429
Patrice Chotard5f256fe2017-10-23 10:57:33 +0200430 ret = clk_get_by_index(dev, 0, &clk);
431 if (ret < 0)
432 return ret;
433
434 ret = clk_enable(&clk);
435 if (ret) {
Patrice Chotard43d36a02018-07-25 17:49:08 +0200436 clk_free(&clk);
Patrice Chotard5f256fe2017-10-23 10:57:33 +0200437 dev_err(dev, "failed to enable clock\n");
438 return ret;
439 }
440
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200441 host->pwr_init = INIT_PWR;
442 host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
443 SDI_CLKCR_HWFC_EN;
Patrice Chotard5f256fe2017-10-23 10:57:33 +0200444 host->clock_in = clk_get_rate(&clk);
Patrice Chotard6f41d1a2018-12-05 14:04:32 +0100445
446 periphid = dev_read_u32_default(dev, "arm,primecell-periphid", 0);
447 switch (periphid) {
448 case STM32_MMCI_ID: /* stm32 variant */
449 host->version2 = false;
450 break;
451 default:
452 host->version2 = true;
453 }
Patrice Chotard9035bb72017-10-23 10:57:32 +0200454
Patrice Chotard80150932018-07-25 17:49:07 +0200455 cfg->name = dev->name;
456 cfg->voltages = VOLTAGE_WINDOW_SD;
457 cfg->host_caps = 0;
458 cfg->f_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1));
459 cfg->f_max = dev_read_u32_default(dev, "max-frequency", MMC_CLOCK_MAX);
460 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
461
Patrice Chotard5829fe22017-10-23 10:57:34 +0200462 gpio_request_by_name(dev, "cd-gpios", 0, &host->cd_gpio, GPIOD_IS_IN);
463
Patrice Chotard9035bb72017-10-23 10:57:32 +0200464 bus_width = dev_read_u32_default(dev, "bus-width", 1);
465 switch (bus_width) {
466 case 8:
Patrice Chotard80150932018-07-25 17:49:07 +0200467 cfg->host_caps |= MMC_MODE_8BIT;
Patrice Chotard9035bb72017-10-23 10:57:32 +0200468 /* Hosts capable of 8-bit transfers can also do 4 bits */
469 case 4:
Patrice Chotard80150932018-07-25 17:49:07 +0200470 cfg->host_caps |= MMC_MODE_4BIT;
Patrice Chotard9035bb72017-10-23 10:57:32 +0200471 break;
472 case 1:
473 break;
474 default:
475 dev_err(dev, "Invalid bus-width value %u\n", bus_width);
476 }
477
Patrice Chotard80150932018-07-25 17:49:07 +0200478 arm_pl180_mmc_init(host);
479 mmc->priv = host;
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200480 mmc->dev = dev;
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200481 upriv->mmc = mmc;
482
483 return 0;
484}
485
Patrice Chotard80150932018-07-25 17:49:07 +0200486int arm_pl180_mmc_bind(struct udevice *dev)
487{
488 struct arm_pl180_mmc_plat *plat = dev_get_platdata(dev);
489
490 return mmc_bind(dev, &plat->mmc, &plat->cfg);
491}
492
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200493static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd,
494 struct mmc_data *data)
495{
496 struct mmc *mmc = mmc_get_mmc_dev(dev);
497
498 return host_request(mmc, cmd, data);
499}
500
501static int dm_host_set_ios(struct udevice *dev)
502{
503 struct mmc *mmc = mmc_get_mmc_dev(dev);
504
505 return host_set_ios(mmc);
506}
507
Patrice Chotard5829fe22017-10-23 10:57:34 +0200508static int dm_mmc_getcd(struct udevice *dev)
509{
Patrice Chotard80150932018-07-25 17:49:07 +0200510 struct pl180_mmc_host *host = dev->priv;
Patrice Chotard5829fe22017-10-23 10:57:34 +0200511 int value = 1;
512
Patrice Chotardfa911562018-07-25 17:49:09 +0200513 if (dm_gpio_is_valid(&host->cd_gpio))
Patrice Chotard5829fe22017-10-23 10:57:34 +0200514 value = dm_gpio_get_value(&host->cd_gpio);
Patrice Chotard5829fe22017-10-23 10:57:34 +0200515
516 return value;
517}
518
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200519static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
520 .send_cmd = dm_host_request,
521 .set_ios = dm_host_set_ios,
Patrice Chotard5829fe22017-10-23 10:57:34 +0200522 .get_cd = dm_mmc_getcd,
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200523};
524
525static int arm_pl180_mmc_ofdata_to_platdata(struct udevice *dev)
526{
Patrice Chotard80150932018-07-25 17:49:07 +0200527 struct pl180_mmc_host *host = dev->priv;
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200528 fdt_addr_t addr;
529
Patrice Chotard80150932018-07-25 17:49:07 +0200530 addr = dev_read_addr(dev);
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200531 if (addr == FDT_ADDR_T_NONE)
532 return -EINVAL;
533
534 host->base = (void *)addr;
535
536 return 0;
537}
538
539static const struct udevice_id arm_pl180_mmc_match[] = {
Patrice Chotard6f41d1a2018-12-05 14:04:32 +0100540 { .compatible = "arm,pl180" },
541 { .compatible = "arm,primecell" },
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200542 { /* sentinel */ }
543};
544
545U_BOOT_DRIVER(arm_pl180_mmc) = {
546 .name = "arm_pl180_mmc",
547 .id = UCLASS_MMC,
548 .of_match = arm_pl180_mmc_match,
549 .ops = &arm_pl180_dm_mmc_ops,
550 .probe = arm_pl180_mmc_probe,
551 .ofdata_to_platdata = arm_pl180_mmc_ofdata_to_platdata,
Patrice Chotard80150932018-07-25 17:49:07 +0200552 .bind = arm_pl180_mmc_bind,
Patrice Chotard3c0dbed2017-10-23 10:57:31 +0200553 .priv_auto_alloc_size = sizeof(struct pl180_mmc_host),
554 .platdata_auto_alloc_size = sizeof(struct arm_pl180_mmc_plat),
555};
556#endif