blob: 3de6b12b0c52524614b159e159a9eee3c45558a5 [file] [log] [blame]
Matt Waddel17eb4972011-04-16 11:54:07 +00001/*
2 * ARM PrimeCell MultiMedia Card Interface - PL180
3 *
4 * Copyright (C) ST-Ericsson SA 2010
5 *
6 * Author: Ulf Hansson <ulf.hansson@stericsson.com>
7 * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com>
8 * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org>
9 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Matt Waddel17eb4972011-04-16 11:54:07 +000011 */
12
13/* #define DEBUG */
14
Matt Waddel17eb4972011-04-16 11:54:07 +000015#include "common.h"
Patrice Chotard879dbab2017-10-23 10:57:33 +020016#include <clk.h>
Matt Waddel17eb4972011-04-16 11:54:07 +000017#include <errno.h>
Patrice Chotardfcce4202017-10-23 10:57:31 +020018#include <malloc.h>
Matt Waddel17eb4972011-04-16 11:54:07 +000019#include <mmc.h>
Patrice Chotardfcce4202017-10-23 10:57:31 +020020
Matt Waddel17eb4972011-04-16 11:54:07 +000021#include "arm_pl180_mmci.h"
Patrice Chotardfcce4202017-10-23 10:57:31 +020022
23#include <asm/io.h>
24
25#ifdef CONFIG_DM_MMC
26#include <dm.h>
27DECLARE_GLOBAL_DATA_PTR;
28
29#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 Waddel17eb4972011-04-16 11:54:07 +000037
Matt Waddel17eb4972011-04-16 11:54:07 +000038static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
39{
40 u32 hoststatus, statusmask;
John Rigby03f609b2012-07-31 08:59:31 +000041 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +000055 debug("CMD%d time out\n", cmd->cmdidx);
Jaehoon Chung7825d202016-07-19 16:33:36 +090056 return -ETIMEDOUT;
Matt Waddel17eb4972011-04-16 11:54:07 +000057 } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
Andy Fleming611a3472012-09-06 15:23:13 -050058 (cmd->resp_type & MMC_RSP_CRC)) {
Matt Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +000082 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Waddel17eb4972011-04-16 11:54:07 +0000113 u64 xfercount = blkcount * blksize;
John Rigby03f609b2012-07-31 08:59:31 +0000114 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +0000170 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +0000229 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Rigby03f609b2012-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 Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +0000264 (u32)data->blocksize);
Matt Waddel17eb4972011-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 Chungb6cd1d32016-12-30 15:30:16 +0900284static int host_set_ios(struct mmc *dev)
Matt Waddel17eb4972011-04-16 11:54:07 +0000285{
John Rigby03f609b2012-07-31 08:59:31 +0000286 struct pl180_mmc_host *host = dev->priv;
Matt Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +0000294 u32 tmp_clock;
Matt Waddel17eb4972011-04-16 11:54:07 +0000295
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200296 if (dev->clock >= dev->cfg->f_max) {
John Rigby03f609b2012-07-31 08:59:31 +0000297 clkdiv = 0;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200298 dev->clock = dev->cfg->f_max;
John Rigby03f609b2012-07-31 08:59:31 +0000299 } else {
300 clkdiv = (host->clock_in / dev->clock) - 2;
301 }
Matt Waddel17eb4972011-04-16 11:54:07 +0000302
John Rigby03f609b2012-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 Waddel17eb4972011-04-16 11:54:07 +0000308
309 if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
310 clkdiv = SDI_CLKCR_CLKDIV_MASK;
311
John Rigby03f609b2012-07-31 08:59:31 +0000312 tmp_clock = host->clock_in / (clkdiv + 2);
313 dev->clock = tmp_clock;
Matt Waddel17eb4972011-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 Rigby03f609b2012-07-31 08:59:31 +0000329 case 8:
330 buswidth |= SDI_CLKCR_WIDBUS_8;
331 break;
Matt Waddel17eb4972011-04-16 11:54:07 +0000332 default:
John Rigby03f609b2012-07-31 08:59:31 +0000333 printf("Invalid bus width: %d\n", dev->bus_width);
Matt Waddel17eb4972011-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 Chungb6cd1d32016-12-30 15:30:16 +0900342
343 return 0;
Matt Waddel17eb4972011-04-16 11:54:07 +0000344}
345
Patrice Chotardfcce4202017-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 Antoniouc9e75912014-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};
Patrice Chotardfcce4202017-10-23 10:57:31 +0200362#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200363
Matt Waddel17eb4972011-04-16 11:54:07 +0000364/*
365 * mmc_host_init - initialize the mmc controller.
366 * Set initial clock and power for mmc slot.
367 * Initialize mmc struct and register with mmc framework.
368 */
Patrice Chotard2a392fe2017-10-23 10:57:30 +0200369int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
Matt Waddel17eb4972011-04-16 11:54:07 +0000370{
Matt Waddel17eb4972011-04-16 11:54:07 +0000371 u32 sdi_u32;
372
John Rigby03f609b2012-07-31 08:59:31 +0000373 writel(host->pwr_init, &host->base->power);
374 writel(host->clkdiv_init, &host->base->clock);
Matt Waddel17eb4972011-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 Antoniou2c850462014-03-11 19:34:20 +0200380
381 host->cfg.name = host->name;
Patrice Chotardfcce4202017-10-23 10:57:31 +0200382#ifndef CONFIG_DM_MMC
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200383 host->cfg.ops = &arm_pl180_mmci_ops;
Patrice Chotardfcce4202017-10-23 10:57:31 +0200384#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200385 /* TODO remove the duplicates */
386 host->cfg.host_caps = host->caps;
387 host->cfg.voltages = host->voltages;
388 host->cfg.f_min = host->clock_min;
389 host->cfg.f_max = host->clock_max;
390 if (host->b_max != 0)
391 host->cfg.b_max = host->b_max;
392 else
393 host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
394
Patrice Chotard2a392fe2017-10-23 10:57:30 +0200395 *mmc = mmc_create(&host->cfg, host);
396 if (!*mmc)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200397 return -1;
398
Patrice Chotard2a392fe2017-10-23 10:57:30 +0200399 debug("registered mmc interface number is:%d\n",
400 (*mmc)->block_dev.devnum);
Matt Waddel17eb4972011-04-16 11:54:07 +0000401
402 return 0;
403}
Patrice Chotardfcce4202017-10-23 10:57:31 +0200404
405#ifdef CONFIG_DM_MMC
406static int arm_pl180_mmc_probe(struct udevice *dev)
407{
408 struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev);
409 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
410 struct mmc *mmc = &pdata->mmc;
411 struct pl180_mmc_host *host = mmc->priv;
Patrice Chotard879dbab2017-10-23 10:57:33 +0200412 struct clk clk;
Patrice Chotard45fc9e62017-10-23 10:57:32 +0200413 u32 bus_width;
Patrice Chotardfcce4202017-10-23 10:57:31 +0200414 int ret;
415
Patrice Chotard879dbab2017-10-23 10:57:33 +0200416 ret = clk_get_by_index(dev, 0, &clk);
417 if (ret < 0)
418 return ret;
419
420 ret = clk_enable(&clk);
421 if (ret) {
422 dev_err(dev, "failed to enable clock\n");
423 return ret;
424 }
425
Patrice Chotardfcce4202017-10-23 10:57:31 +0200426 strcpy(host->name, "MMC");
427 host->pwr_init = INIT_PWR;
428 host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
429 SDI_CLKCR_HWFC_EN;
430 host->voltages = VOLTAGE_WINDOW_SD;
431 host->caps = 0;
Patrice Chotard879dbab2017-10-23 10:57:33 +0200432 host->clock_in = clk_get_rate(&clk);
433 host->clock_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1));
Patrice Chotardfcce4202017-10-23 10:57:31 +0200434 host->clock_max = dev_read_u32_default(dev, "max-frequency",
435 MMC_CLOCK_MAX);
436 host->version2 = dev_get_driver_data(dev);
Patrice Chotard45fc9e62017-10-23 10:57:32 +0200437
438 bus_width = dev_read_u32_default(dev, "bus-width", 1);
439 switch (bus_width) {
440 case 8:
441 host->caps |= MMC_MODE_8BIT;
442 /* Hosts capable of 8-bit transfers can also do 4 bits */
443 case 4:
444 host->caps |= MMC_MODE_4BIT;
445 break;
446 case 1:
447 break;
448 default:
449 dev_err(dev, "Invalid bus-width value %u\n", bus_width);
450 }
451
Patrice Chotardfcce4202017-10-23 10:57:31 +0200452 ret = arm_pl180_mmci_init(host, &mmc);
453 if (ret) {
454 dev_err(dev, "arm_pl180_mmci init failed\n");
455 return ret;
456 }
457
458 mmc->dev = dev;
459 dev->priv = host;
460 upriv->mmc = mmc;
461
462 return 0;
463}
464
465static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd,
466 struct mmc_data *data)
467{
468 struct mmc *mmc = mmc_get_mmc_dev(dev);
469
470 return host_request(mmc, cmd, data);
471}
472
473static int dm_host_set_ios(struct udevice *dev)
474{
475 struct mmc *mmc = mmc_get_mmc_dev(dev);
476
477 return host_set_ios(mmc);
478}
479
480static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
481 .send_cmd = dm_host_request,
482 .set_ios = dm_host_set_ios,
483};
484
485static int arm_pl180_mmc_ofdata_to_platdata(struct udevice *dev)
486{
487 struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev);
488 struct mmc *mmc = &pdata->mmc;
489 struct pl180_mmc_host *host = mmc->priv;
490 fdt_addr_t addr;
491
492 addr = devfdt_get_addr(dev);
493 if (addr == FDT_ADDR_T_NONE)
494 return -EINVAL;
495
496 host->base = (void *)addr;
497
498 return 0;
499}
500
501static const struct udevice_id arm_pl180_mmc_match[] = {
502 { .compatible = "st,stm32f4xx-sdio", .data = VERSION1 },
503 { /* sentinel */ }
504};
505
506U_BOOT_DRIVER(arm_pl180_mmc) = {
507 .name = "arm_pl180_mmc",
508 .id = UCLASS_MMC,
509 .of_match = arm_pl180_mmc_match,
510 .ops = &arm_pl180_dm_mmc_ops,
511 .probe = arm_pl180_mmc_probe,
512 .ofdata_to_platdata = arm_pl180_mmc_ofdata_to_platdata,
513 .priv_auto_alloc_size = sizeof(struct pl180_mmc_host),
514 .platdata_auto_alloc_size = sizeof(struct arm_pl180_mmc_plat),
515};
516#endif