8c95229bf77aceb9d17c21f2c99e359d57eaae2c
[oweals/u-boot.git] / drivers / mmc / arm_pl180_mmci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
10  */
11
12 /* #define DEBUG */
13
14 #include "common.h"
15 #include <clk.h>
16 #include <errno.h>
17 #include <log.h>
18 #include <malloc.h>
19 #include <mmc.h>
20 #include <dm/device_compat.h>
21
22 #include <asm/io.h>
23 #include <asm-generic/gpio.h>
24
25 #include "arm_pl180_mmci.h"
26
27 #ifdef CONFIG_DM_MMC
28 #include <dm.h>
29 #define MMC_CLOCK_MAX   48000000
30 #define MMC_CLOCK_MIN   400000
31
32 struct arm_pl180_mmc_plat {
33         struct mmc_config cfg;
34         struct mmc mmc;
35 };
36 #endif
37
38 static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
39 {
40         u32 hoststatus, statusmask;
41         struct pl180_mmc_host *host = dev->priv;
42
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) {
55                 debug("CMD%d time out\n", cmd->cmdidx);
56                 return -ETIMEDOUT;
57         } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
58                    (cmd->resp_type & MMC_RSP_CRC)) {
59                 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 */
78 static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
79 {
80         int result;
81         u32 sdi_cmd = 0;
82         struct pl180_mmc_host *host = dev->priv;
83
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
110 static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
111 {
112         u32 *tempbuff = dest;
113         u64 xfercount = blkcount * blksize;
114         struct pl180_mmc_host *host = dev->priv;
115         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);
122         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
165 static 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;
170         struct pl180_mmc_host *host = dev->priv;
171         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
224 static int do_data_transfer(struct mmc *dev,
225                             struct mmc_cmd *cmd,
226                             struct mmc_data *data)
227 {
228         int error = -ETIMEDOUT;
229         struct pl180_mmc_host *host = dev->priv;
230         u32 blksz = 0;
231         u32 data_ctrl = 0;
232         u32 data_len = (u32) (data->blocks * data->blocksize);
233
234         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;
242
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,
264                                                         (u32)data->blocksize);
265         }
266
267         return error;
268 }
269
270 static 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
284 static int  host_set_ios(struct mmc *dev)
285 {
286         struct pl180_mmc_host *host = dev->priv;
287         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;
294                 u32 tmp_clock;
295
296                 if (dev->clock >= dev->cfg->f_max) {
297                         clkdiv = 0;
298                         dev->clock = dev->cfg->f_max;
299                 } else {
300                         clkdiv = (host->clock_in / dev->clock) - 2;
301                 }
302
303                 tmp_clock = host->clock_in / (clkdiv + 2);
304                 while (tmp_clock > dev->clock) {
305                         clkdiv++;
306                         tmp_clock = host->clock_in / (clkdiv + 2);
307                 }
308
309                 if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
310                         clkdiv = SDI_CLKCR_CLKDIV_MASK;
311
312                 tmp_clock = host->clock_in / (clkdiv + 2);
313                 dev->clock = tmp_clock;
314                 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;
329                 case 8:
330                         buswidth |= SDI_CLKCR_WIDBUS_8;
331                         break;
332                 default:
333                         printf("Invalid bus width: %d\n", dev->bus_width);
334                         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);
342
343         return 0;
344 }
345
346 #ifndef CONFIG_DM_MMC
347 /* MMC uses open drain drivers in the enumeration phase */
348 static 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
357 static 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
363 /*
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  */
368
369 int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
370 {
371         u32 sdi_u32;
372
373         writel(host->pwr_init, &host->base->power);
374         writel(host->clkdiv_init, &host->base->clock);
375         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);
380
381         host->cfg.name = host->name;
382         host->cfg.ops = &arm_pl180_mmci_ops;
383
384         /* 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
394         *mmc = mmc_create(&host->cfg, host);
395         if (!*mmc)
396                 return -1;
397         debug("registered mmc interface number is:%d\n",
398               (*mmc)->block_dev.devnum);
399
400         return 0;
401 }
402 #endif
403
404 #ifdef CONFIG_DM_MMC
405 static 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
418 static 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;
423         struct pl180_mmc_host *host = dev->priv;
424         struct mmc_config *cfg = &pdata->cfg;
425         struct clk clk;
426         u32 bus_width;
427         u32 periphid;
428         int ret;
429
430         ret = clk_get_by_index(dev, 0, &clk);
431         if (ret < 0)
432                 return ret;
433
434         ret = clk_enable(&clk);
435         if (ret) {
436                 clk_free(&clk);
437                 dev_err(dev, "failed to enable clock\n");
438                 return ret;
439         }
440
441         host->pwr_init = INIT_PWR;
442         host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
443                             SDI_CLKCR_HWFC_EN;
444         host->clock_in = clk_get_rate(&clk);
445
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         }
454
455         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
462         gpio_request_by_name(dev, "cd-gpios", 0, &host->cd_gpio, GPIOD_IS_IN);
463
464         bus_width = dev_read_u32_default(dev, "bus-width", 1);
465         switch (bus_width) {
466         case 8:
467                 cfg->host_caps |= MMC_MODE_8BIT;
468                 /* Hosts capable of 8-bit transfers can also do 4 bits */
469         case 4:
470                 cfg->host_caps |= MMC_MODE_4BIT;
471                 break;
472         case 1:
473                 break;
474         default:
475                 dev_err(dev, "Invalid bus-width value %u\n", bus_width);
476         }
477
478         arm_pl180_mmc_init(host);
479         mmc->priv = host;
480         mmc->dev = dev;
481         upriv->mmc = mmc;
482
483         return 0;
484 }
485
486 int 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
493 static 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
501 static 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
508 static int dm_mmc_getcd(struct udevice *dev)
509 {
510         struct pl180_mmc_host *host = dev->priv;
511         int value = 1;
512
513         if (dm_gpio_is_valid(&host->cd_gpio))
514                 value = dm_gpio_get_value(&host->cd_gpio);
515
516         return value;
517 }
518
519 static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
520         .send_cmd = dm_host_request,
521         .set_ios = dm_host_set_ios,
522         .get_cd = dm_mmc_getcd,
523 };
524
525 static int arm_pl180_mmc_ofdata_to_platdata(struct udevice *dev)
526 {
527         struct pl180_mmc_host *host = dev->priv;
528         fdt_addr_t addr;
529
530         addr = dev_read_addr(dev);
531         if (addr == FDT_ADDR_T_NONE)
532                 return -EINVAL;
533
534         host->base = (void *)addr;
535
536         return 0;
537 }
538
539 static const struct udevice_id arm_pl180_mmc_match[] = {
540         { .compatible = "arm,pl180" },
541         { .compatible = "arm,primecell" },
542         { /* sentinel */ }
543 };
544
545 U_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,
552         .bind = arm_pl180_mmc_bind,
553         .priv_auto_alloc_size = sizeof(struct pl180_mmc_host),
554         .platdata_auto_alloc_size = sizeof(struct arm_pl180_mmc_plat),
555 };
556 #endif