2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2010-08-13 08:31:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010
|
|
|
|
* Rob Emanuele <rob@emanuele.us>
|
|
|
|
* Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
|
|
|
|
*
|
|
|
|
* Original Driver:
|
|
|
|
* Copyright (C) 2004-2006 Atmel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-04-13 02:29:22 +00:00
|
|
|
#include <clk.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2010-08-13 08:31:06 +00:00
|
|
|
#include <mmc.h>
|
|
|
|
#include <part.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/io.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2010-08-13 08:31:06 +00:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <asm/arch/clk.h>
|
2010-11-03 15:32:56 +00:00
|
|
|
#include <asm/arch/hardware.h>
|
2010-08-13 08:31:06 +00:00
|
|
|
#include "atmel_mci.h"
|
|
|
|
|
|
|
|
#ifndef CONFIG_SYS_MMC_CLK_OD
|
|
|
|
# define CONFIG_SYS_MMC_CLK_OD 150000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MMC_DEFAULT_BLKLEN 512
|
|
|
|
|
|
|
|
#if defined(CONFIG_ATMEL_MCI_PORTB)
|
|
|
|
# define MCI_BUS 1
|
|
|
|
#else
|
|
|
|
# define MCI_BUS 0
|
|
|
|
#endif
|
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
|
|
|
struct atmel_mci_plat {
|
|
|
|
struct mmc mmc;
|
|
|
|
struct mmc_config cfg;
|
|
|
|
struct atmel_mci *mci;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv {
|
2017-07-26 06:35:42 +00:00
|
|
|
#ifndef CONFIG_DM_MMC
|
2015-10-23 18:46:30 +00:00
|
|
|
struct mmc_config cfg;
|
|
|
|
struct atmel_mci *mci;
|
2017-07-26 06:35:42 +00:00
|
|
|
#endif
|
2015-10-23 18:46:31 +00:00
|
|
|
unsigned int initialized:1;
|
2015-11-05 19:58:30 +00:00
|
|
|
unsigned int curr_clk;
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
|
|
|
ulong bus_clk_rate;
|
|
|
|
#endif
|
2015-10-23 18:46:30 +00:00
|
|
|
};
|
|
|
|
|
2013-04-26 00:27:06 +00:00
|
|
|
/* Read Atmel MCI IP version */
|
|
|
|
static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
|
|
|
|
{
|
|
|
|
return readl(&mci->version) & 0x00000fff;
|
|
|
|
}
|
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/*
|
|
|
|
* Print command and status:
|
|
|
|
*
|
|
|
|
* - always when DEBUG is defined
|
|
|
|
* - on command errors
|
|
|
|
*/
|
|
|
|
static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
|
|
|
|
{
|
2015-10-23 18:46:28 +00:00
|
|
|
debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
|
|
|
|
cmdr, cmdr & 0x3F, arg, status, msg);
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 14:23:29 +00:00
|
|
|
static inline void mci_set_blklen(atmel_mci_t *mci, int blklen)
|
|
|
|
{
|
|
|
|
unsigned int version = atmel_mci_get_version(mci);
|
|
|
|
|
|
|
|
blklen &= 0xfffc;
|
|
|
|
|
|
|
|
/* MCI IP version >= 0x200 has blkr */
|
|
|
|
if (version >= 0x200)
|
|
|
|
writel(MMCI_BFINS(BLKLEN, blklen, readl(&mci->blkr)),
|
|
|
|
&mci->blkr);
|
|
|
|
else
|
|
|
|
writel(MMCI_BFINS(BLKLEN, blklen, readl(&mci->mr)), &mci->mr);
|
|
|
|
}
|
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/* Setup for MCI Clock and Block Size */
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2017-07-26 06:35:42 +00:00
|
|
|
static void mci_set_mode(struct udevice *dev, u32 hz, u32 blklen)
|
2017-04-13 02:29:22 +00:00
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
|
|
|
struct atmel_mci_priv *priv = dev_get_priv(dev);
|
|
|
|
struct mmc *mmc = &plat->mmc;
|
2017-04-13 02:29:22 +00:00
|
|
|
u32 bus_hz = priv->bus_clk_rate;
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_t *mci = plat->mci;
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
|
|
|
|
{
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv *priv = mmc->priv;
|
2010-08-13 08:31:06 +00:00
|
|
|
u32 bus_hz = get_mci_clk_rate();
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_t *mci = priv->mci;
|
2017-04-13 02:29:22 +00:00
|
|
|
#endif
|
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
u32 clkdiv = 255;
|
2014-07-31 06:39:30 +00:00
|
|
|
unsigned int version = atmel_mci_get_version(mci);
|
|
|
|
u32 clkodd = 0;
|
|
|
|
u32 mr;
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
|
|
|
|
bus_hz, hz, blklen);
|
|
|
|
if (hz > 0) {
|
2014-07-31 06:39:30 +00:00
|
|
|
if (version >= 0x500) {
|
|
|
|
clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
|
|
|
|
if (clkdiv > 511)
|
|
|
|
clkdiv = 511;
|
|
|
|
|
|
|
|
clkodd = clkdiv & 1;
|
|
|
|
clkdiv >>= 1;
|
|
|
|
|
2015-10-23 18:46:28 +00:00
|
|
|
debug("mci: setting clock %u Hz, block size %u\n",
|
|
|
|
bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
|
2014-07-31 06:39:30 +00:00
|
|
|
} else {
|
|
|
|
/* find clkdiv yielding a rate <= than requested */
|
|
|
|
for (clkdiv = 0; clkdiv < 255; clkdiv++) {
|
|
|
|
if ((bus_hz / (clkdiv + 1) / 2) <= hz)
|
|
|
|
break;
|
|
|
|
}
|
2015-10-23 18:46:28 +00:00
|
|
|
debug("mci: setting clock %u Hz, block size %u\n",
|
|
|
|
(bus_hz / (clkdiv + 1)) / 2, blklen);
|
2014-07-31 06:39:30 +00:00
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 19:58:30 +00:00
|
|
|
if (version >= 0x500)
|
|
|
|
priv->curr_clk = bus_hz / (clkdiv * 2 + clkodd + 2);
|
|
|
|
else
|
|
|
|
priv->curr_clk = (bus_hz / (clkdiv + 1)) / 2;
|
2014-07-31 06:39:30 +00:00
|
|
|
|
|
|
|
mr = MMCI_BF(CLKDIV, clkdiv);
|
|
|
|
|
|
|
|
/* MCI IP version >= 0x200 has R/WPROOF */
|
|
|
|
if (version >= 0x200)
|
|
|
|
mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
|
|
|
|
|
2012-09-13 22:22:04 +00:00
|
|
|
/*
|
2014-07-31 06:39:30 +00:00
|
|
|
* MCI IP version >= 0x500 use bit 16 as clkodd.
|
|
|
|
* MCI IP version < 0x500 use upper 16 bits for blklen.
|
2012-09-13 22:22:04 +00:00
|
|
|
*/
|
2014-07-31 06:39:30 +00:00
|
|
|
if (version >= 0x500)
|
|
|
|
mr |= MMCI_BF(CLKODD, clkodd);
|
|
|
|
|
|
|
|
writel(mr, &mci->mr);
|
|
|
|
|
2018-01-04 14:23:29 +00:00
|
|
|
mci_set_blklen(mci, blklen);
|
2014-07-31 06:39:30 +00:00
|
|
|
|
2014-07-31 06:39:32 +00:00
|
|
|
if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
|
|
|
|
writel(MMCI_BIT(HSMODE), &mci->cfg);
|
|
|
|
|
2015-10-23 18:46:31 +00:00
|
|
|
priv->initialized = 1;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the CMDR with flags for a given command and data packet */
|
|
|
|
static u32 mci_encode_cmd(
|
|
|
|
struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
|
|
|
|
{
|
|
|
|
u32 cmdr = 0;
|
|
|
|
|
|
|
|
/* Default Flags for Errors */
|
|
|
|
*error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
|
|
|
|
MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
|
|
|
|
|
|
|
|
/* Default Flags for the Command */
|
|
|
|
cmdr |= MMCI_BIT(MAXLAT);
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
cmdr |= MMCI_BF(TRCMD, 1);
|
|
|
|
if (data->blocks > 1)
|
|
|
|
cmdr |= MMCI_BF(TRTYP, 1);
|
|
|
|
if (data->flags & MMC_DATA_READ)
|
|
|
|
cmdr |= MMCI_BIT(TRDIR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->resp_type & MMC_RSP_CRC)
|
|
|
|
*error_flags |= MMCI_BIT(RCRCE);
|
|
|
|
if (cmd->resp_type & MMC_RSP_136)
|
|
|
|
cmdr |= MMCI_BF(RSPTYP, 2);
|
|
|
|
else if (cmd->resp_type & MMC_RSP_BUSY)
|
|
|
|
cmdr |= MMCI_BF(RSPTYP, 3);
|
|
|
|
else if (cmd->resp_type & MMC_RSP_PRESENT)
|
|
|
|
cmdr |= MMCI_BF(RSPTYP, 1);
|
|
|
|
|
|
|
|
return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entered into function pointer in mci_send_cmd */
|
|
|
|
static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
|
|
|
|
{
|
|
|
|
u32 status;
|
|
|
|
|
|
|
|
do {
|
|
|
|
status = readl(&mci->sr);
|
|
|
|
if (status & (error_flags | MMCI_BIT(OVRE)))
|
|
|
|
goto io_fail;
|
|
|
|
} while (!(status & MMCI_BIT(RXRDY)));
|
|
|
|
|
|
|
|
if (status & MMCI_BIT(RXRDY)) {
|
|
|
|
*data = readl(&mci->rdr);
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
io_fail:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entered into function pointer in mci_send_cmd */
|
|
|
|
static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
|
|
|
|
{
|
|
|
|
u32 status;
|
|
|
|
|
|
|
|
do {
|
|
|
|
status = readl(&mci->sr);
|
|
|
|
if (status & (error_flags | MMCI_BIT(UNRE)))
|
|
|
|
goto io_fail;
|
|
|
|
} while (!(status & MMCI_BIT(TXRDY)));
|
|
|
|
|
|
|
|
if (status & MMCI_BIT(TXRDY)) {
|
|
|
|
writel(*data, &mci->tdr);
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
io_fail:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entered into mmc structure during driver init
|
|
|
|
*
|
|
|
|
* Sends a command out on the bus and deals with the block data.
|
|
|
|
* Takes the mmc pointer, a command pointer, and an optional data pointer.
|
|
|
|
*/
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
|
|
|
static int atmel_mci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
|
|
|
|
struct mmc_data *data)
|
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
struct atmel_mci_priv *priv = dev_get_priv(dev);
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_t *mci = plat->mci;
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
static int
|
|
|
|
mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
|
|
|
|
{
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv *priv = mmc->priv;
|
|
|
|
atmel_mci_t *mci = priv->mci;
|
2017-07-26 06:35:42 +00:00
|
|
|
#endif
|
2010-08-13 08:31:06 +00:00
|
|
|
u32 cmdr;
|
|
|
|
u32 error_flags = 0;
|
|
|
|
u32 status;
|
|
|
|
|
2015-10-23 18:46:31 +00:00
|
|
|
if (!priv->initialized) {
|
2010-08-13 08:31:06 +00:00
|
|
|
puts ("MCI not initialized!\n");
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ECOMM;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out the transfer arguments */
|
|
|
|
cmdr = mci_encode_cmd(cmd, data, &error_flags);
|
|
|
|
|
2018-01-04 14:23:29 +00:00
|
|
|
mci_set_blklen(mci, data->blocksize);
|
|
|
|
|
2012-09-13 22:22:04 +00:00
|
|
|
/* For multi blocks read/write, set the block register */
|
|
|
|
if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
|
|
|
|
|| (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
|
2018-01-04 14:23:29 +00:00
|
|
|
writel(data->blocks | MMCI_BF(BLKLEN, data->blocksize),
|
|
|
|
&mci->blkr);
|
2012-09-13 22:22:04 +00:00
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/* Send the command */
|
|
|
|
writel(cmd->cmdarg, &mci->argr);
|
|
|
|
writel(cmdr, &mci->cmdr);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Wait for the command to complete */
|
|
|
|
while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
|
|
|
|
|
2013-04-26 00:27:07 +00:00
|
|
|
if ((status & error_flags) & MMCI_BIT(RTOE)) {
|
|
|
|
dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ETIMEDOUT;
|
2013-04-26 00:27:07 +00:00
|
|
|
} else if (status & error_flags) {
|
2010-08-13 08:31:06 +00:00
|
|
|
dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ECOMM;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the response to the response buffer */
|
|
|
|
if (cmd->resp_type & MMC_RSP_136) {
|
|
|
|
cmd->response[0] = readl(&mci->rspr);
|
|
|
|
cmd->response[1] = readl(&mci->rspr1);
|
|
|
|
cmd->response[2] = readl(&mci->rspr2);
|
|
|
|
cmd->response[3] = readl(&mci->rspr3);
|
|
|
|
} else
|
|
|
|
cmd->response[0] = readl(&mci->rspr);
|
|
|
|
|
|
|
|
/* transfer all of the blocks */
|
|
|
|
if (data) {
|
|
|
|
u32 word_count, block_count;
|
|
|
|
u32* ioptr;
|
2018-01-04 14:23:29 +00:00
|
|
|
u32 i;
|
2010-08-13 08:31:06 +00:00
|
|
|
u32 (*mci_data_op)
|
|
|
|
(atmel_mci_t *mci, u32* data, u32 error_flags);
|
|
|
|
|
|
|
|
if (data->flags & MMC_DATA_READ) {
|
|
|
|
mci_data_op = mci_data_read;
|
|
|
|
ioptr = (u32*)data->dest;
|
|
|
|
} else {
|
|
|
|
mci_data_op = mci_data_write;
|
|
|
|
ioptr = (u32*)data->src;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
for (block_count = 0;
|
|
|
|
block_count < data->blocks && !status;
|
|
|
|
block_count++) {
|
|
|
|
word_count = 0;
|
|
|
|
do {
|
|
|
|
status = mci_data_op(mci, ioptr, error_flags);
|
|
|
|
word_count++;
|
|
|
|
ioptr++;
|
|
|
|
} while (!status && word_count < (data->blocksize/4));
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (data->flags & MMC_DATA_READ)
|
|
|
|
{
|
2014-05-07 09:06:08 +00:00
|
|
|
u32 cnt = word_count * 4;
|
2010-08-13 08:31:06 +00:00
|
|
|
printf("Read Data:\n");
|
2014-05-07 09:06:08 +00:00
|
|
|
print_buffer(0, data->dest + cnt * block_count,
|
|
|
|
1, cnt, 0);
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (status) {
|
|
|
|
dump_cmd(cmdr, cmd->cmdarg, status,
|
|
|
|
"Data Transfer Failed");
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ECOMM;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for Transfer End */
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
status = readl(&mci->sr);
|
|
|
|
|
|
|
|
if (status & error_flags) {
|
|
|
|
dump_cmd(cmdr, cmd->cmdarg, status,
|
|
|
|
"DTIP Wait Failed");
|
2016-07-19 07:33:36 +00:00
|
|
|
return -ECOMM;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while ((status & MMCI_BIT(DTIP)) && i < 10000);
|
|
|
|
if (status & MMCI_BIT(DTIP)) {
|
|
|
|
dump_cmd(cmdr, cmd->cmdarg, status,
|
|
|
|
"XFER DTIP never unset, ignoring");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 19:58:30 +00:00
|
|
|
/*
|
|
|
|
* After the switch command, wait for 8 clocks before the next
|
|
|
|
* command
|
|
|
|
*/
|
|
|
|
if (cmd->cmdidx == MMC_CMD_SWITCH)
|
|
|
|
udelay(8*1000000 / priv->curr_clk); /* 8 clk in us */
|
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
|
|
|
static int atmel_mci_set_ios(struct udevice *dev)
|
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
struct mmc *mmc = mmc_get_mmc_dev(dev);
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_t *mci = plat->mci;
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
/* Entered into mmc structure during driver init */
|
2016-12-30 06:30:16 +00:00
|
|
|
static int mci_set_ios(struct mmc *mmc)
|
2010-08-13 08:31:06 +00:00
|
|
|
{
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv *priv = mmc->priv;
|
|
|
|
atmel_mci_t *mci = priv->mci;
|
2017-07-26 06:35:42 +00:00
|
|
|
#endif
|
2013-04-26 00:27:06 +00:00
|
|
|
int bus_width = mmc->bus_width;
|
|
|
|
unsigned int version = atmel_mci_get_version(mci);
|
|
|
|
int busw;
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
/* Set the clock speed */
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2017-07-26 06:35:42 +00:00
|
|
|
mci_set_mode(dev, mmc->clock, MMC_DEFAULT_BLKLEN);
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
|
2017-04-13 02:29:22 +00:00
|
|
|
#endif
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set the bus width and select slot for this interface
|
|
|
|
* there is no capability for multiple slots on the same interface yet
|
|
|
|
*/
|
2013-04-26 00:27:06 +00:00
|
|
|
if ((version & 0xf00) >= 0x300) {
|
|
|
|
switch (bus_width) {
|
|
|
|
case 8:
|
|
|
|
busw = 3;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
busw = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
busw = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
|
|
|
|
} else {
|
|
|
|
busw = (bus_width == 4) ? 1 : 0;
|
|
|
|
|
|
|
|
writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
|
|
|
|
}
|
2016-12-30 06:30:16 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-08-13 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2017-07-26 06:35:42 +00:00
|
|
|
static int atmel_mci_hw_init(struct udevice *dev)
|
2017-04-13 02:29:22 +00:00
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
|
|
|
atmel_mci_t *mci = plat->mci;
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
/* Entered into mmc structure during driver init */
|
|
|
|
static int mci_init(struct mmc *mmc)
|
|
|
|
{
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv *priv = mmc->priv;
|
|
|
|
atmel_mci_t *mci = priv->mci;
|
2017-07-26 06:35:42 +00:00
|
|
|
#endif
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
/* Initialize controller */
|
|
|
|
writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
|
|
|
|
writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
|
|
|
|
writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
|
2010-11-16 08:24:41 +00:00
|
|
|
writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
|
2010-08-13 08:31:06 +00:00
|
|
|
|
2012-09-13 22:22:06 +00:00
|
|
|
/* This delay can be optimized, but stick with max value */
|
|
|
|
writel(0x7f, &mci->dtor);
|
2010-08-13 08:31:06 +00:00
|
|
|
/* Disable Interrupts */
|
|
|
|
writel(~0UL, &mci->idr);
|
|
|
|
|
|
|
|
/* Set default clocks and blocklen */
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2017-07-26 06:35:42 +00:00
|
|
|
mci_set_mode(dev, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
|
2017-04-13 02:29:22 +00:00
|
|
|
#else
|
2010-08-13 08:31:06 +00:00
|
|
|
mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
|
2017-04-13 02:29:22 +00:00
|
|
|
#endif
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-13 02:29:22 +00:00
|
|
|
#ifndef CONFIG_DM_MMC
|
2014-02-26 17:28:45 +00:00
|
|
|
static const struct mmc_ops atmel_mci_ops = {
|
|
|
|
.send_cmd = mci_send_cmd,
|
|
|
|
.set_ios = mci_set_ios,
|
|
|
|
.init = mci_init,
|
|
|
|
};
|
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/*
|
|
|
|
* This is the only exported function
|
|
|
|
*
|
|
|
|
* Call it with the MCI register base address
|
|
|
|
*/
|
|
|
|
int atmel_mci_init(void *regs)
|
|
|
|
{
|
2014-03-11 17:34:20 +00:00
|
|
|
struct mmc *mmc;
|
|
|
|
struct mmc_config *cfg;
|
2015-10-23 18:46:30 +00:00
|
|
|
struct atmel_mci_priv *priv;
|
2013-04-26 00:27:06 +00:00
|
|
|
unsigned int version;
|
2010-08-13 08:31:06 +00:00
|
|
|
|
2015-10-23 18:46:30 +00:00
|
|
|
priv = calloc(1, sizeof(*priv));
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
2013-04-26 00:27:06 +00:00
|
|
|
|
2015-10-23 18:46:30 +00:00
|
|
|
cfg = &priv->cfg;
|
2014-03-11 17:34:20 +00:00
|
|
|
|
|
|
|
cfg->name = "mci";
|
|
|
|
cfg->ops = &atmel_mci_ops;
|
2010-08-13 08:31:06 +00:00
|
|
|
|
2015-10-23 18:46:30 +00:00
|
|
|
priv->mci = (struct atmel_mci *)regs;
|
2015-10-23 18:46:31 +00:00
|
|
|
priv->initialized = 0;
|
2015-10-23 18:46:30 +00:00
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/* need to be able to pass these in on a board by board basis */
|
2014-03-11 17:34:20 +00:00
|
|
|
cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
|
2015-10-23 18:46:30 +00:00
|
|
|
version = atmel_mci_get_version(priv->mci);
|
2014-07-31 06:39:32 +00:00
|
|
|
if ((version & 0xf00) >= 0x300) {
|
2014-03-11 17:34:20 +00:00
|
|
|
cfg->host_caps = MMC_MODE_8BIT;
|
2014-07-31 06:39:32 +00:00
|
|
|
cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
|
|
|
|
}
|
2013-04-26 00:27:06 +00:00
|
|
|
|
2014-03-11 17:34:20 +00:00
|
|
|
cfg->host_caps |= MMC_MODE_4BIT;
|
2013-04-26 00:27:06 +00:00
|
|
|
|
2010-08-13 08:31:06 +00:00
|
|
|
/*
|
|
|
|
* min and max frequencies determined by
|
|
|
|
* max and min of clock divider
|
|
|
|
*/
|
2014-03-11 17:34:20 +00:00
|
|
|
cfg->f_min = get_mci_clk_rate() / (2*256);
|
|
|
|
cfg->f_max = get_mci_clk_rate() / (2*1);
|
|
|
|
|
|
|
|
cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
|
2010-08-13 08:31:06 +00:00
|
|
|
|
2015-10-23 18:46:30 +00:00
|
|
|
mmc = mmc_create(cfg, priv);
|
2011-04-18 05:50:08 +00:00
|
|
|
|
2014-03-11 17:34:20 +00:00
|
|
|
if (mmc == NULL) {
|
2015-10-23 18:46:30 +00:00
|
|
|
free(priv);
|
|
|
|
return -ENODEV;
|
2014-03-11 17:34:20 +00:00
|
|
|
}
|
2015-10-23 18:46:30 +00:00
|
|
|
/* NOTE: possibly leaking the priv structure */
|
2010-08-13 08:31:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-13 02:29:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DM_MMC
|
|
|
|
static const struct dm_mmc_ops atmel_mci_mmc_ops = {
|
|
|
|
.send_cmd = atmel_mci_send_cmd,
|
|
|
|
.set_ios = atmel_mci_set_ios,
|
|
|
|
};
|
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
static void atmel_mci_setup_cfg(struct udevice *dev)
|
2017-04-13 02:29:22 +00:00
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
|
|
|
struct atmel_mci_priv *priv = dev_get_priv(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
struct mmc_config *cfg;
|
|
|
|
u32 version;
|
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
cfg = &plat->cfg;
|
2017-04-13 02:29:22 +00:00
|
|
|
cfg->name = "Atmel mci";
|
|
|
|
cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the version is above 3.0, the capabilities of the 8-bit
|
|
|
|
* bus width and high speed are supported.
|
|
|
|
*/
|
2017-07-26 06:35:42 +00:00
|
|
|
version = atmel_mci_get_version(plat->mci);
|
2017-04-13 02:29:22 +00:00
|
|
|
if ((version & 0xf00) >= 0x300) {
|
|
|
|
cfg->host_caps = MMC_MODE_8BIT |
|
|
|
|
MMC_MODE_HS | MMC_MODE_HS_52MHz;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->host_caps |= MMC_MODE_4BIT;
|
|
|
|
cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
|
|
|
|
cfg->f_min = priv->bus_clk_rate / (2 * 256);
|
|
|
|
cfg->f_max = priv->bus_clk_rate / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_mci_enable_clk(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct atmel_mci_priv *priv = dev_get_priv(dev);
|
|
|
|
struct clk clk;
|
|
|
|
ulong clk_rate;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_enable(&clk);
|
|
|
|
if (ret)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
clk_rate = clk_get_rate(&clk);
|
|
|
|
if (!clk_rate) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->bus_clk_rate = clk_rate;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
clk_free(&clk);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_mci_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
struct mmc *mmc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = atmel_mci_enable_clk(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-08-04 05:14:43 +00:00
|
|
|
plat->mci = dev_read_addr_ptr(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_setup_cfg(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
mmc = &plat->mmc;
|
|
|
|
mmc->cfg = &plat->cfg;
|
2017-04-13 02:29:22 +00:00
|
|
|
mmc->dev = dev;
|
|
|
|
upriv->mmc = mmc;
|
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
atmel_mci_hw_init(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int atmel_mci_bind(struct udevice *dev)
|
|
|
|
{
|
2017-07-26 06:35:42 +00:00
|
|
|
struct atmel_mci_plat *plat = dev_get_platdata(dev);
|
2017-04-13 02:29:22 +00:00
|
|
|
|
2017-07-26 06:35:42 +00:00
|
|
|
return mmc_bind(dev, &plat->mmc, &plat->cfg);
|
2017-04-13 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct udevice_id atmel_mci_ids[] = {
|
|
|
|
{ .compatible = "atmel,hsmci" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(atmel_mci) = {
|
|
|
|
.name = "atmel-mci",
|
|
|
|
.id = UCLASS_MMC,
|
|
|
|
.of_match = atmel_mci_ids,
|
|
|
|
.bind = atmel_mci_bind,
|
|
|
|
.probe = atmel_mci_probe,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct atmel_mci_plat),
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct atmel_mci_priv),
|
2017-04-13 02:29:22 +00:00
|
|
|
.ops = &atmel_mci_mmc_ops,
|
|
|
|
};
|
|
|
|
#endif
|