2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-10-15 19:10:29 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2012 SAMSUNG Electronics
|
|
|
|
* Jaehoon Chung <jh80.chung@samsung.com>
|
|
|
|
* Rajeshawari Shinde <rajeshwari.s@samsung.com>
|
|
|
|
*/
|
|
|
|
|
2013-12-26 11:29:07 +00:00
|
|
|
#include <bouncebuf.h>
|
2012-10-15 19:10:29 +00:00
|
|
|
#include <common.h>
|
2019-11-14 19:57:39 +00:00
|
|
|
#include <cpu_func.h>
|
2015-08-07 02:16:27 +00:00
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2012-10-15 19:10:29 +00:00
|
|
|
#include <malloc.h>
|
2015-09-02 23:24:58 +00:00
|
|
|
#include <memalign.h>
|
2012-10-15 19:10:29 +00:00
|
|
|
#include <mmc.h>
|
|
|
|
#include <dwmmc.h>
|
2018-12-20 09:55:41 +00:00
|
|
|
#include <wait_bit.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2019-05-13 13:25:27 +00:00
|
|
|
#include <power/regulator.h>
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
#define PAGE_SIZE 4096
|
|
|
|
|
|
|
|
static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
|
|
|
|
{
|
|
|
|
unsigned long timeout = 1000;
|
|
|
|
u32 ctrl;
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CTRL, value);
|
|
|
|
|
|
|
|
while (timeout--) {
|
|
|
|
ctrl = dwmci_readl(host, DWMCI_CTRL);
|
|
|
|
if (!(ctrl & DWMCI_RESET_ALL))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
|
|
|
|
u32 desc0, u32 desc1, u32 desc2)
|
|
|
|
{
|
|
|
|
struct dwmci_idmac *desc = idmac;
|
|
|
|
|
|
|
|
desc->flags = desc0;
|
|
|
|
desc->cnt = desc1;
|
|
|
|
desc->addr = desc2;
|
2015-10-25 07:48:25 +00:00
|
|
|
desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void dwmci_prepare_data(struct dwmci_host *host,
|
2013-12-26 11:29:07 +00:00
|
|
|
struct mmc_data *data,
|
|
|
|
struct dwmci_idmac *cur_idmac,
|
|
|
|
void *bounce_buffer)
|
2012-10-15 19:10:29 +00:00
|
|
|
{
|
|
|
|
unsigned long ctrl;
|
|
|
|
unsigned int i = 0, flags, cnt, blk_cnt;
|
2013-12-26 11:29:07 +00:00
|
|
|
ulong data_start, data_end;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
blk_cnt = data->blocks;
|
|
|
|
|
|
|
|
dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
|
|
|
|
|
2018-12-20 09:55:41 +00:00
|
|
|
/* Clear IDMAC interrupt */
|
|
|
|
dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF);
|
|
|
|
|
2012-10-15 19:10:29 +00:00
|
|
|
data_start = (ulong)cur_idmac;
|
2015-10-25 07:48:25 +00:00
|
|
|
dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
|
|
|
|
flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
|
|
|
|
if (blk_cnt <= 8) {
|
|
|
|
flags |= DWMCI_IDMAC_LD;
|
|
|
|
cnt = data->blocksize * blk_cnt;
|
|
|
|
} else
|
|
|
|
cnt = data->blocksize * 8;
|
|
|
|
|
|
|
|
dwmci_set_idma_desc(cur_idmac, flags, cnt,
|
2015-10-25 07:48:25 +00:00
|
|
|
(ulong)bounce_buffer + (i * PAGE_SIZE));
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2019-02-13 19:16:20 +00:00
|
|
|
cur_idmac++;
|
2013-07-26 14:18:40 +00:00
|
|
|
if (blk_cnt <= 8)
|
2012-10-15 19:10:29 +00:00
|
|
|
break;
|
|
|
|
blk_cnt -= 8;
|
|
|
|
i++;
|
|
|
|
} while(1);
|
|
|
|
|
|
|
|
data_end = (ulong)cur_idmac;
|
2019-02-13 19:16:20 +00:00
|
|
|
flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
ctrl = dwmci_readl(host, DWMCI_CTRL);
|
|
|
|
ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
|
|
|
|
dwmci_writel(host, DWMCI_CTRL, ctrl);
|
|
|
|
|
|
|
|
ctrl = dwmci_readl(host, DWMCI_BMOD);
|
|
|
|
ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
|
|
|
|
dwmci_writel(host, DWMCI_BMOD, ctrl);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
|
|
|
|
dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
|
|
|
|
}
|
|
|
|
|
2018-09-21 08:59:45 +00:00
|
|
|
static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
|
|
|
|
{
|
|
|
|
u32 timeout = 20000;
|
|
|
|
|
|
|
|
*len = dwmci_readl(host, DWMCI_STATUS);
|
|
|
|
while (--timeout && (*len & bit)) {
|
|
|
|
udelay(200);
|
|
|
|
*len = dwmci_readl(host, DWMCI_STATUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timeout) {
|
|
|
|
debug("%s: FIFO underflow timeout\n", __func__);
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:32:24 +00:00
|
|
|
static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int timeout;
|
|
|
|
|
2019-08-29 07:42:41 +00:00
|
|
|
timeout = size * 8; /* counting in bits */
|
|
|
|
timeout *= 10; /* wait 10 times as long */
|
2019-03-23 02:32:24 +00:00
|
|
|
timeout /= mmc->clock;
|
|
|
|
timeout /= mmc->bus_width;
|
|
|
|
timeout /= mmc->ddr_mode ? 2 : 1;
|
2019-08-29 07:42:41 +00:00
|
|
|
timeout *= 1000; /* counting in msec */
|
2019-03-23 02:32:24 +00:00
|
|
|
timeout = (timeout < 1000) ? 1000 : timeout;
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
|
2015-11-17 06:20:21 +00:00
|
|
|
{
|
2019-03-23 02:32:24 +00:00
|
|
|
struct mmc *mmc = host->mmc;
|
2015-11-17 06:20:21 +00:00
|
|
|
int ret = 0;
|
2019-03-23 02:32:24 +00:00
|
|
|
u32 timeout, mask, size, i, len = 0;
|
2015-11-17 06:20:22 +00:00
|
|
|
u32 *buf = NULL;
|
2015-11-17 06:20:21 +00:00
|
|
|
ulong start = get_timer(0);
|
2015-11-17 06:20:22 +00:00
|
|
|
u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
|
|
|
|
RX_WMARK_SHIFT) + 1) * 2;
|
|
|
|
|
2019-03-23 02:32:24 +00:00
|
|
|
size = data->blocksize * data->blocks;
|
2015-11-17 06:20:22 +00:00
|
|
|
if (data->flags == MMC_DATA_READ)
|
|
|
|
buf = (unsigned int *)data->dest;
|
|
|
|
else
|
|
|
|
buf = (unsigned int *)data->src;
|
2015-11-17 06:20:21 +00:00
|
|
|
|
2019-03-23 02:32:24 +00:00
|
|
|
timeout = dwmci_get_timeout(mmc, size);
|
|
|
|
|
|
|
|
size /= 4;
|
|
|
|
|
2015-11-17 06:20:21 +00:00
|
|
|
for (;;) {
|
|
|
|
mask = dwmci_readl(host, DWMCI_RINTSTS);
|
|
|
|
/* Error during data transfer. */
|
|
|
|
if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
|
|
|
|
debug("%s: DATA ERROR!\n", __func__);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
if (host->fifo_mode && size) {
|
2016-07-28 02:25:48 +00:00
|
|
|
len = 0;
|
2016-09-19 02:16:50 +00:00
|
|
|
if (data->flags == MMC_DATA_READ &&
|
2021-04-26 03:35:05 +00:00
|
|
|
(mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO))) {
|
|
|
|
dwmci_writel(host, DWMCI_RINTSTS,
|
2022-09-15 17:56:56 +00:00
|
|
|
mask & (DWMCI_INTMSK_RXDR |
|
|
|
|
DWMCI_INTMSK_DTO));
|
2016-09-19 02:16:50 +00:00
|
|
|
while (size) {
|
2018-09-21 08:59:45 +00:00
|
|
|
ret = dwmci_fifo_ready(host,
|
|
|
|
DWMCI_FIFO_EMPTY,
|
|
|
|
&len);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
len = (len >> DWMCI_FIFO_SHIFT) &
|
|
|
|
DWMCI_FIFO_MASK;
|
2016-07-28 02:25:47 +00:00
|
|
|
len = min(size, len);
|
2015-11-17 06:20:22 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
*buf++ =
|
|
|
|
dwmci_readl(host, DWMCI_DATA);
|
2016-09-19 02:16:50 +00:00
|
|
|
size = size > len ? (size - len) : 0;
|
2015-11-17 06:20:22 +00:00
|
|
|
}
|
2016-09-19 02:16:50 +00:00
|
|
|
} else if (data->flags == MMC_DATA_WRITE &&
|
|
|
|
(mask & DWMCI_INTMSK_TXDR)) {
|
|
|
|
while (size) {
|
2018-09-21 08:59:45 +00:00
|
|
|
ret = dwmci_fifo_ready(host,
|
|
|
|
DWMCI_FIFO_FULL,
|
|
|
|
&len);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
len = fifo_depth - ((len >>
|
|
|
|
DWMCI_FIFO_SHIFT) &
|
|
|
|
DWMCI_FIFO_MASK);
|
2016-07-28 02:25:47 +00:00
|
|
|
len = min(size, len);
|
2015-11-17 06:20:22 +00:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
dwmci_writel(host, DWMCI_DATA,
|
|
|
|
*buf++);
|
2016-09-19 02:16:50 +00:00
|
|
|
size = size > len ? (size - len) : 0;
|
2015-11-17 06:20:22 +00:00
|
|
|
}
|
2016-09-19 02:16:50 +00:00
|
|
|
dwmci_writel(host, DWMCI_RINTSTS,
|
|
|
|
DWMCI_INTMSK_TXDR);
|
2015-11-17 06:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 06:20:21 +00:00
|
|
|
/* Data arrived correctly. */
|
|
|
|
if (mask & DWMCI_INTMSK_DTO) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for timeout. */
|
|
|
|
if (get_timer(start) > timeout) {
|
|
|
|
debug("%s: Timeout waiting for data!\n",
|
|
|
|
__func__);
|
2016-07-19 07:33:36 +00:00
|
|
|
ret = -ETIMEDOUT;
|
2015-11-17 06:20:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_RINTSTS, mask);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-15 19:10:29 +00:00
|
|
|
static int dwmci_set_transfer_mode(struct dwmci_host *host,
|
|
|
|
struct mmc_data *data)
|
|
|
|
{
|
|
|
|
unsigned long mode;
|
|
|
|
|
|
|
|
mode = DWMCI_CMD_DATA_EXP;
|
|
|
|
if (data->flags & MMC_DATA_WRITE)
|
|
|
|
mode |= DWMCI_CMD_RW;
|
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2017-07-29 17:35:31 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2016-06-28 06:52:21 +00:00
|
|
|
static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
|
2016-06-13 05:30:23 +00:00
|
|
|
struct mmc_data *data)
|
|
|
|
{
|
|
|
|
struct mmc *mmc = mmc_get_mmc_dev(dev);
|
|
|
|
#else
|
2012-10-15 19:10:29 +00:00
|
|
|
static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
|
|
|
|
struct mmc_data *data)
|
|
|
|
{
|
2016-06-13 05:30:23 +00:00
|
|
|
#endif
|
2014-03-11 17:34:20 +00:00
|
|
|
struct dwmci_host *host = mmc->priv;
|
2013-07-26 12:08:14 +00:00
|
|
|
ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
|
2013-07-26 14:18:40 +00:00
|
|
|
data ? DIV_ROUND_UP(data->blocks, 8) : 0);
|
2015-07-27 20:39:38 +00:00
|
|
|
int ret = 0, flags = 0, i;
|
2016-07-19 01:38:22 +00:00
|
|
|
unsigned int timeout = 500;
|
2016-03-04 00:09:52 +00:00
|
|
|
u32 retry = 100000;
|
2012-10-15 19:10:29 +00:00
|
|
|
u32 mask, ctrl;
|
2013-04-27 06:12:54 +00:00
|
|
|
ulong start = get_timer(0);
|
2013-12-26 11:29:07 +00:00
|
|
|
struct bounce_buffer bbstate;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
|
2013-04-27 06:12:54 +00:00
|
|
|
if (get_timer(start) > timeout) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Timeout on data busy\n", __func__);
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ETIMEDOUT;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
|
|
|
|
|
2013-12-26 11:29:07 +00:00
|
|
|
if (data) {
|
2015-11-17 06:20:22 +00:00
|
|
|
if (host->fifo_mode) {
|
|
|
|
dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
|
|
|
|
dwmci_writel(host, DWMCI_BYTCNT,
|
|
|
|
data->blocksize * data->blocks);
|
|
|
|
dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
|
2013-12-26 11:29:07 +00:00
|
|
|
} else {
|
2015-11-17 06:20:22 +00:00
|
|
|
if (data->flags == MMC_DATA_READ) {
|
2019-03-23 17:45:27 +00:00
|
|
|
ret = bounce_buffer_start(&bbstate,
|
|
|
|
(void*)data->dest,
|
2015-11-17 06:20:22 +00:00
|
|
|
data->blocksize *
|
|
|
|
data->blocks, GEN_BB_WRITE);
|
|
|
|
} else {
|
2019-03-23 17:45:27 +00:00
|
|
|
ret = bounce_buffer_start(&bbstate,
|
|
|
|
(void*)data->src,
|
2015-11-17 06:20:22 +00:00
|
|
|
data->blocksize *
|
|
|
|
data->blocks, GEN_BB_READ);
|
|
|
|
}
|
2019-03-23 17:45:27 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
dwmci_prepare_data(host, data, cur_idmac,
|
|
|
|
bbstate.bounce_buffer);
|
2013-12-26 11:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
flags = dwmci_set_transfer_mode(host, data);
|
|
|
|
|
|
|
|
if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
|
2021-12-07 16:09:35 +00:00
|
|
|
return -EBUSY;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
|
|
|
|
flags |= DWMCI_CMD_ABORT_STOP;
|
|
|
|
else
|
|
|
|
flags |= DWMCI_CMD_PRV_DAT_WAIT;
|
|
|
|
|
|
|
|
if (cmd->resp_type & MMC_RSP_PRESENT) {
|
|
|
|
flags |= DWMCI_CMD_RESP_EXP;
|
|
|
|
if (cmd->resp_type & MMC_RSP_136)
|
|
|
|
flags |= DWMCI_CMD_RESP_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->resp_type & MMC_RSP_CRC)
|
|
|
|
flags |= DWMCI_CMD_CHECK_CRC;
|
|
|
|
|
|
|
|
flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
|
|
|
|
|
|
|
|
debug("Sending CMD%d\n",cmd->cmdidx);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CMD, flags);
|
|
|
|
|
|
|
|
for (i = 0; i < retry; i++) {
|
|
|
|
mask = dwmci_readl(host, DWMCI_RINTSTS);
|
|
|
|
if (mask & DWMCI_INTMSK_CDONE) {
|
|
|
|
if (!data)
|
|
|
|
dwmci_writel(host, DWMCI_RINTSTS, mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 10:49:48 +00:00
|
|
|
if (i == retry) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Timeout.\n", __func__);
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ETIMEDOUT;
|
2014-09-05 10:49:48 +00:00
|
|
|
}
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
if (mask & DWMCI_INTMSK_RTO) {
|
2014-09-05 10:49:48 +00:00
|
|
|
/*
|
|
|
|
* Timeout here is not necessarily fatal. (e)MMC cards
|
|
|
|
* will splat here when they receive CMD55 as they do
|
|
|
|
* not support this command and that is exactly the way
|
|
|
|
* to tell them apart from SD cards. Thus, this output
|
|
|
|
* below shall be debug(). eMMC cards also do not favor
|
|
|
|
* CMD8, please keep that in mind.
|
|
|
|
*/
|
|
|
|
debug("%s: Response Timeout.\n", __func__);
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ETIMEDOUT;
|
2012-10-15 19:10:29 +00:00
|
|
|
} else if (mask & DWMCI_INTMSK_RE) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Response Error.\n", __func__);
|
|
|
|
return -EIO;
|
2018-11-06 22:42:11 +00:00
|
|
|
} else if ((cmd->resp_type & MMC_RSP_CRC) &&
|
|
|
|
(mask & DWMCI_INTMSK_RCRC)) {
|
|
|
|
debug("%s: Response CRC Error.\n", __func__);
|
|
|
|
return -EIO;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cmd->resp_type & MMC_RSP_PRESENT) {
|
|
|
|
if (cmd->resp_type & MMC_RSP_136) {
|
|
|
|
cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
|
|
|
|
cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
|
|
|
|
cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
|
|
|
|
cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
|
|
|
|
} else {
|
|
|
|
cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2015-11-17 06:20:22 +00:00
|
|
|
ret = dwmci_data_transfer(host, data);
|
|
|
|
|
|
|
|
/* only dma mode need it */
|
|
|
|
if (!host->fifo_mode) {
|
2018-12-20 09:55:41 +00:00
|
|
|
if (data->flags == MMC_DATA_READ)
|
|
|
|
mask = DWMCI_IDINTEN_RI;
|
|
|
|
else
|
|
|
|
mask = DWMCI_IDINTEN_TI;
|
|
|
|
ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS,
|
|
|
|
mask, true, 1000, false);
|
|
|
|
if (ret)
|
|
|
|
debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n",
|
|
|
|
__func__, mask);
|
|
|
|
/* clear interrupts */
|
|
|
|
dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK);
|
|
|
|
|
2015-11-17 06:20:22 +00:00
|
|
|
ctrl = dwmci_readl(host, DWMCI_CTRL);
|
|
|
|
ctrl &= ~(DWMCI_DMA_EN);
|
|
|
|
dwmci_writel(host, DWMCI_CTRL, ctrl);
|
|
|
|
bounce_buffer_stop(&bbstate);
|
|
|
|
}
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udelay(100);
|
|
|
|
|
2015-07-27 20:39:38 +00:00
|
|
|
return ret;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
|
|
|
|
{
|
|
|
|
u32 div, status;
|
|
|
|
int timeout = 10000;
|
|
|
|
unsigned long sclk;
|
|
|
|
|
2013-04-27 06:12:54 +00:00
|
|
|
if ((freq == host->clock) || (freq == 0))
|
2012-10-15 19:10:29 +00:00
|
|
|
return 0;
|
|
|
|
/*
|
2014-09-05 10:49:48 +00:00
|
|
|
* If host->get_mmc_clk isn't defined,
|
2012-10-15 19:10:29 +00:00
|
|
|
* then assume that host->bus_hz is source clock value.
|
2014-09-05 10:49:48 +00:00
|
|
|
* host->bus_hz should be set by user.
|
2012-10-15 19:10:29 +00:00
|
|
|
*/
|
2013-10-06 09:59:31 +00:00
|
|
|
if (host->get_mmc_clk)
|
2015-08-30 22:55:15 +00:00
|
|
|
sclk = host->get_mmc_clk(host, freq);
|
2012-10-15 19:10:29 +00:00
|
|
|
else if (host->bus_hz)
|
|
|
|
sclk = host->bus_hz;
|
|
|
|
else {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Didn't get source clock value.\n", __func__);
|
2012-10-15 19:10:29 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-06-10 06:26:52 +00:00
|
|
|
if (sclk == freq)
|
|
|
|
div = 0; /* bypass mode */
|
|
|
|
else
|
|
|
|
div = DIV_ROUND_UP(sclk, 2 * freq);
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CLKENA, 0);
|
|
|
|
dwmci_writel(host, DWMCI_CLKSRC, 0);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CLKDIV, div);
|
|
|
|
dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
|
|
|
|
DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
|
|
|
|
|
|
|
|
do {
|
|
|
|
status = dwmci_readl(host, DWMCI_CMD);
|
|
|
|
if (timeout-- < 0) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Timeout!\n", __func__);
|
2012-10-15 19:10:29 +00:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
} while (status & DWMCI_CMD_START);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
|
|
|
|
DWMCI_CLKEN_LOW_PWR);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
|
|
|
|
DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
|
|
|
|
|
|
|
|
timeout = 10000;
|
|
|
|
do {
|
|
|
|
status = dwmci_readl(host, DWMCI_CMD);
|
|
|
|
if (timeout-- < 0) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s: Timeout!\n", __func__);
|
2012-10-15 19:10:29 +00:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
} while (status & DWMCI_CMD_START);
|
|
|
|
|
|
|
|
host->clock = freq;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-29 17:35:31 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2016-06-28 06:52:21 +00:00
|
|
|
static int dwmci_set_ios(struct udevice *dev)
|
2016-06-13 05:30:23 +00:00
|
|
|
{
|
|
|
|
struct mmc *mmc = mmc_get_mmc_dev(dev);
|
|
|
|
#else
|
2016-12-30 06:30:16 +00:00
|
|
|
static int dwmci_set_ios(struct mmc *mmc)
|
2012-10-15 19:10:29 +00:00
|
|
|
{
|
2016-06-13 05:30:23 +00:00
|
|
|
#endif
|
2014-05-16 04:59:55 +00:00
|
|
|
struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
|
|
|
|
u32 ctype, regs;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2014-09-05 10:49:48 +00:00
|
|
|
debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
dwmci_setup_bus(host, mmc->clock);
|
|
|
|
switch (mmc->bus_width) {
|
|
|
|
case 8:
|
|
|
|
ctype = DWMCI_CTYPE_8BIT;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
ctype = DWMCI_CTYPE_4BIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ctype = DWMCI_CTYPE_1BIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CTYPE, ctype);
|
|
|
|
|
2014-05-16 04:59:55 +00:00
|
|
|
regs = dwmci_readl(host, DWMCI_UHS_REG);
|
2014-12-01 12:59:12 +00:00
|
|
|
if (mmc->ddr_mode)
|
2014-05-16 04:59:55 +00:00
|
|
|
regs |= DWMCI_DDR_MODE;
|
|
|
|
else
|
2015-01-14 08:37:53 +00:00
|
|
|
regs &= ~DWMCI_DDR_MODE;
|
2014-05-16 04:59:55 +00:00
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_UHS_REG, regs);
|
|
|
|
|
2020-12-24 10:21:03 +00:00
|
|
|
if (host->clksel) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = host->clksel(host);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-12-30 06:30:16 +00:00
|
|
|
|
2019-05-13 13:25:27 +00:00
|
|
|
#if CONFIG_IS_ENABLED(DM_REGULATOR)
|
|
|
|
if (mmc->vqmmc_supply) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
|
|
|
|
regulator_set_value(mmc->vqmmc_supply, 1800000);
|
|
|
|
else
|
|
|
|
regulator_set_value(mmc->vqmmc_supply, 3300000);
|
|
|
|
|
|
|
|
ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-13 05:30:23 +00:00
|
|
|
return 0;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dwmci_init(struct mmc *mmc)
|
|
|
|
{
|
2014-03-11 17:34:20 +00:00
|
|
|
struct dwmci_host *host = mmc->priv;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2013-11-29 11:08:57 +00:00
|
|
|
if (host->board_init)
|
|
|
|
host->board_init(host);
|
2013-10-29 07:23:13 +00:00
|
|
|
|
2012-10-15 19:10:29 +00:00
|
|
|
dwmci_writel(host, DWMCI_PWREN, 1);
|
|
|
|
|
|
|
|
if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
|
2015-08-07 02:16:27 +00:00
|
|
|
debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
|
|
|
|
return -EIO;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 06:12:54 +00:00
|
|
|
/* Enumerate at 400KHz */
|
2014-03-11 17:34:20 +00:00
|
|
|
dwmci_setup_bus(host, mmc->cfg->f_min);
|
2013-04-27 06:12:54 +00:00
|
|
|
|
2012-10-15 19:10:29 +00:00
|
|
|
dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
|
|
|
|
dwmci_writel(host, DWMCI_INTMASK, 0);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
|
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_IDINTEN, 0);
|
|
|
|
dwmci_writel(host, DWMCI_BMOD, 1);
|
|
|
|
|
2015-08-07 02:16:29 +00:00
|
|
|
if (!host->fifoth_val) {
|
|
|
|
uint32_t fifo_size;
|
|
|
|
|
|
|
|
fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
|
|
|
|
fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
|
|
|
|
host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
|
|
|
|
TX_WMARK(fifo_size / 2);
|
2013-04-27 06:12:54 +00:00
|
|
|
}
|
2015-08-07 02:16:29 +00:00
|
|
|
dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
|
2012-10-15 19:10:29 +00:00
|
|
|
|
|
|
|
dwmci_writel(host, DWMCI_CLKENA, 0);
|
|
|
|
dwmci_writel(host, DWMCI_CLKSRC, 0);
|
|
|
|
|
2018-12-20 09:55:41 +00:00
|
|
|
if (!host->fifo_mode)
|
|
|
|
dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK);
|
|
|
|
|
2012-10-15 19:10:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-29 17:35:31 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2016-06-13 05:30:23 +00:00
|
|
|
int dwmci_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct mmc *mmc = mmc_get_mmc_dev(dev);
|
|
|
|
|
|
|
|
return dwmci_init(mmc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct dm_mmc_ops dm_dwmci_ops = {
|
|
|
|
.send_cmd = dwmci_send_cmd,
|
|
|
|
.set_ios = dwmci_set_ios,
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
2014-02-26 17:28:45 +00:00
|
|
|
static const struct mmc_ops dwmci_ops = {
|
|
|
|
.send_cmd = dwmci_send_cmd,
|
|
|
|
.set_ios = dwmci_set_ios,
|
|
|
|
.init = dwmci_init,
|
|
|
|
};
|
2016-06-13 05:30:23 +00:00
|
|
|
#endif
|
2014-02-26 17:28:45 +00:00
|
|
|
|
2016-09-23 10:13:16 +00:00
|
|
|
void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
|
|
|
|
u32 max_clk, u32 min_clk)
|
2012-10-15 19:10:29 +00:00
|
|
|
{
|
2016-09-23 10:13:16 +00:00
|
|
|
cfg->name = host->name;
|
2017-07-29 17:35:31 +00:00
|
|
|
#ifndef CONFIG_DM_MMC
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->ops = &dwmci_ops;
|
2016-06-13 05:30:23 +00:00
|
|
|
#endif
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->f_min = min_clk;
|
|
|
|
cfg->f_max = max_clk;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2016-09-23 10:13:16 +00:00
|
|
|
cfg->host_caps = host->caps;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2016-09-23 10:13:16 +00:00
|
|
|
if (host->buswidth == 8) {
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->host_caps |= MMC_MODE_8BIT;
|
|
|
|
cfg->host_caps &= ~MMC_MODE_4BIT;
|
2012-10-15 19:10:29 +00:00
|
|
|
} else {
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->host_caps |= MMC_MODE_4BIT;
|
|
|
|
cfg->host_caps &= ~MMC_MODE_8BIT;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
2016-05-14 20:03:07 +00:00
|
|
|
cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
|
|
|
|
|
|
|
|
cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
|
|
|
|
}
|
2014-03-11 17:34:20 +00:00
|
|
|
|
2016-05-14 20:03:07 +00:00
|
|
|
#ifdef CONFIG_BLK
|
|
|
|
int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
|
|
|
|
{
|
|
|
|
return mmc_bind(dev, mmc, cfg);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
|
|
|
|
{
|
2016-09-23 10:13:16 +00:00
|
|
|
dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2014-03-11 17:34:20 +00:00
|
|
|
host->mmc = mmc_create(&host->cfg, host);
|
|
|
|
if (host->mmc == NULL)
|
|
|
|
return -1;
|
2012-10-15 19:10:29 +00:00
|
|
|
|
2014-03-11 17:34:20 +00:00
|
|
|
return 0;
|
2012-10-15 19:10:29 +00:00
|
|
|
}
|
2016-05-14 20:03:07 +00:00
|
|
|
#endif
|