2011-06-06 17:32:06 +00:00
|
|
|
/*
|
2013-01-25 15:17:37 +00:00
|
|
|
* Atmel MCI driver
|
2011-06-06 17:32:06 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Hubert Feurstein <h.feurstein@gmail.com>
|
|
|
|
*
|
|
|
|
* based on imx.c by:
|
|
|
|
* Copyright (C) 2009 Ilya Yanok, <yanok@emcraft.com>
|
|
|
|
* Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
|
|
|
|
* Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <mci.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <clock.h>
|
|
|
|
#include <gpio.h>
|
2011-09-22 17:02:57 +00:00
|
|
|
#include <io.h>
|
2011-06-06 17:32:06 +00:00
|
|
|
#include <mach/board.h>
|
|
|
|
#include <linux/clk.h>
|
2011-09-09 11:46:48 +00:00
|
|
|
#include <linux/err.h>
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
#include "atmel-mci-regs.h"
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:39 +00:00
|
|
|
struct atmel_mci_caps {
|
2013-01-25 15:17:44 +00:00
|
|
|
bool has_cfg_reg;
|
|
|
|
bool has_highspeed;
|
2013-01-25 15:17:41 +00:00
|
|
|
bool has_rwproof;
|
2013-01-25 15:17:39 +00:00
|
|
|
bool has_odd_clk_div;
|
2013-01-25 15:17:42 +00:00
|
|
|
bool need_reset_after_xfer;
|
2013-01-25 15:17:39 +00:00
|
|
|
};
|
|
|
|
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci {
|
2011-06-06 17:32:06 +00:00
|
|
|
struct mci_host mci;
|
2013-01-25 15:17:37 +00:00
|
|
|
void __iomem *regs;
|
2011-06-06 17:32:06 +00:00
|
|
|
struct device_d *hw_dev;
|
|
|
|
struct clk *clk;
|
|
|
|
|
|
|
|
u32 datasize;
|
|
|
|
struct mci_cmd *cmd;
|
|
|
|
struct mci_data *data;
|
2012-01-13 15:55:24 +00:00
|
|
|
unsigned slot_b;
|
2013-01-25 15:17:38 +00:00
|
|
|
int version;
|
2013-01-25 15:17:39 +00:00
|
|
|
struct atmel_mci_caps caps;
|
|
|
|
|
|
|
|
unsigned long bus_hz;
|
|
|
|
u32 mode_reg;
|
2013-01-25 15:17:44 +00:00
|
|
|
u32 cfg_reg;
|
2013-01-25 15:17:45 +00:00
|
|
|
u32 sdc_reg;
|
2013-01-25 15:17:43 +00:00
|
|
|
bool need_reset;
|
2011-06-06 17:32:06 +00:00
|
|
|
};
|
|
|
|
|
2013-01-25 15:17:34 +00:00
|
|
|
#define to_mci_host(mci) container_of(mci, struct atmel_mci, mci)
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
#define STATUS_ERROR_MASK (ATMCI_RINDE \
|
|
|
|
| ATMCI_RDIRE \
|
|
|
|
| ATMCI_RCRCE \
|
|
|
|
| ATMCI_RENDE \
|
|
|
|
| ATMCI_RTOE \
|
|
|
|
| ATMCI_DCRCE \
|
|
|
|
| ATMCI_DTOE \
|
|
|
|
| ATMCI_OVRE \
|
|
|
|
| ATMCI_UNRE)
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static void atmci_set_clk_rate(struct atmel_mci *host,
|
2013-01-25 15:17:39 +00:00
|
|
|
unsigned int clock_min)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:39 +00:00
|
|
|
unsigned int clkdiv;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:39 +00:00
|
|
|
if (!host->mode_reg) {
|
|
|
|
clk_enable(host->clk);
|
|
|
|
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
|
2013-01-25 15:17:44 +00:00
|
|
|
if (host->caps.has_cfg_reg)
|
|
|
|
atmci_writel(host, ATMCI_CFG, host->cfg_reg);
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:39 +00:00
|
|
|
if (host->caps.has_odd_clk_div) {
|
|
|
|
clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
|
|
|
|
if (clkdiv > 511) {
|
|
|
|
dev_dbg(host->hw_dev,
|
|
|
|
"clock %u too slow; using %lu\n",
|
|
|
|
clock_min, host->bus_hz / (511 + 2));
|
|
|
|
clkdiv = 511;
|
|
|
|
}
|
|
|
|
host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
|
|
|
|
| ATMCI_MR_CLKODD(clkdiv & 1);
|
|
|
|
} else {
|
|
|
|
clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
|
|
|
|
if (clkdiv > 255) {
|
|
|
|
dev_dbg(host->hw_dev,
|
|
|
|
"clock %u too slow; using %lu\n",
|
|
|
|
clock_min, host->bus_hz / (2 * 256));
|
|
|
|
clkdiv = 255;
|
|
|
|
}
|
|
|
|
host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(host->hw_dev, "atmel_set_clk_rate: clkIn=%ld clkIos=%d divider=%d\n",
|
|
|
|
host->bus_hz, clock_min, clkdiv);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:41 +00:00
|
|
|
/*
|
|
|
|
* WRPROOF and RDPROOF prevent overruns/underruns by
|
|
|
|
* stopping the clock when the FIFO is full/empty.
|
|
|
|
* This state is not expected to last for long.
|
|
|
|
*/
|
|
|
|
if (host->caps.has_rwproof)
|
|
|
|
host->mode_reg |= ATMCI_MR_RDPROOF | ATMCI_MR_WRPROOF;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:39 +00:00
|
|
|
atmci_writel(host, ATMCI_MR, host->mode_reg);
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_poll_status(struct atmel_mci *host, u32 mask)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
u32 stat;
|
|
|
|
uint64_t start = get_time_ns();
|
|
|
|
|
|
|
|
do {
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_readl(host, ATMCI_SR);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat & STATUS_ERROR_MASK)
|
|
|
|
return stat;
|
|
|
|
if (is_timeout(start, SECOND)) {
|
|
|
|
dev_err(host->hw_dev, "timeout\n");
|
2013-01-25 15:17:43 +00:00
|
|
|
host->need_reset = true;
|
2013-01-25 15:17:37 +00:00
|
|
|
return ATMCI_RTOE | stat;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
if (stat & mask)
|
|
|
|
return 0;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_pull(struct atmel_mci *host, void *_buf, int bytes)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
unsigned int stat;
|
|
|
|
u32 *buf = _buf;
|
|
|
|
|
|
|
|
while (bytes > 3) {
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_RXRDY);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
*buf++ = atmci_readl(host, ATMCI_RDR);
|
2011-06-06 17:32:06 +00:00
|
|
|
bytes -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(bytes))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_MCI_WRITE
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_push(struct atmel_mci *host, const void *_buf, int bytes)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
unsigned int stat;
|
|
|
|
const u32 *buf = _buf;
|
|
|
|
|
|
|
|
while (bytes > 3) {
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_TXRDY);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
atmci_writel(host, ATMCI_TDR, *buf++);
|
2011-06-06 17:32:06 +00:00
|
|
|
bytes -= 4;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_TXRDY);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
if (WARN_ON(bytes))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MCI_WRITE */
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_transfer_data(struct atmel_mci *host)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
struct mci_data *data = host->data;
|
|
|
|
int stat;
|
|
|
|
unsigned long length;
|
|
|
|
|
|
|
|
length = data->blocks * data->blocksize;
|
|
|
|
host->datasize = 0;
|
|
|
|
|
|
|
|
if (data->flags & MMC_DATA_READ) {
|
2013-01-25 15:17:36 +00:00
|
|
|
stat = atmci_pull(host, data->dest, length);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_NOTBUSY);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
host->datasize += length;
|
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_MCI_WRITE
|
2013-01-25 15:17:36 +00:00
|
|
|
stat = atmci_push(host, (const void *)(data->src), length);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
host->datasize += length;
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_NOTBUSY);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (stat)
|
|
|
|
return stat;
|
|
|
|
#endif /* CONFIG_MCI_WRITE */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static void atmci_finish_request(struct atmel_mci *host)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
host->cmd = NULL;
|
|
|
|
host->data = NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_finish_data(struct atmel_mci *host, unsigned int stat)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
int data_error = 0;
|
|
|
|
|
|
|
|
if (stat & STATUS_ERROR_MASK) {
|
|
|
|
dev_err(host->hw_dev, "request failed (status=0x%08x)\n", stat);
|
2013-01-25 15:17:37 +00:00
|
|
|
if (stat & ATMCI_DCRCE)
|
2011-06-06 17:32:06 +00:00
|
|
|
data_error = -EILSEQ;
|
2013-01-25 15:17:37 +00:00
|
|
|
else if (stat & (ATMCI_RTOE | ATMCI_DTOE))
|
2011-06-06 17:32:06 +00:00
|
|
|
data_error = -ETIMEDOUT;
|
|
|
|
else
|
|
|
|
data_error = -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
host->data = NULL;
|
|
|
|
|
|
|
|
return data_error;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static void atmci_setup_data(struct atmel_mci *host, struct mci_data *data)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
unsigned int nob = data->blocks;
|
|
|
|
unsigned int blksz = data->blocksize;
|
|
|
|
unsigned int datasize = nob * blksz;
|
|
|
|
|
|
|
|
BUG_ON(data->blocksize & 3);
|
|
|
|
BUG_ON(nob == 0);
|
|
|
|
|
|
|
|
host->data = data;
|
|
|
|
|
|
|
|
dev_dbg(host->hw_dev, "atmel_setup_data: nob=%d blksz=%d\n",
|
|
|
|
nob, blksz);
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(nob)
|
|
|
|
| ATMCI_BLKLEN(blksz));
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
host->datasize = datasize;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_read_response(struct atmel_mci *host, unsigned int stat)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
struct mci_cmd *cmd = host->cmd;
|
|
|
|
int i;
|
2013-04-26 16:41:09 +00:00
|
|
|
u32 *resp;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
return 0;
|
|
|
|
|
2013-04-26 16:41:09 +00:00
|
|
|
resp = (u32 *)cmd->response;
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
if (stat & (ATMCI_RTOE | ATMCI_DTOE)) {
|
2011-06-06 17:32:06 +00:00
|
|
|
dev_err(host->hw_dev, "command/data timeout\n");
|
|
|
|
return -ETIMEDOUT;
|
2013-01-25 15:17:37 +00:00
|
|
|
} else if ((stat & ATMCI_RCRCE) && (cmd->resp_type & MMC_RSP_CRC)) {
|
2011-06-06 17:32:06 +00:00
|
|
|
dev_err(host->hw_dev, "cmd crc error\n");
|
|
|
|
return -EILSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->resp_type & MMC_RSP_PRESENT) {
|
|
|
|
if (cmd->resp_type & MMC_RSP_136) {
|
|
|
|
for (i = 0; i < 4; i++)
|
2013-01-25 15:17:37 +00:00
|
|
|
resp[i] = atmci_readl(host, ATMCI_RSPR);
|
2011-06-06 17:32:06 +00:00
|
|
|
} else {
|
2013-01-25 15:17:37 +00:00
|
|
|
resp[0] = atmci_readl(host, ATMCI_RSPR);
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_cmd_done(struct atmel_mci *host, unsigned int stat)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
|
|
|
int datastat;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
ret = atmci_read_response(host, stat);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
if (ret) {
|
2013-01-25 15:17:36 +00:00
|
|
|
atmci_finish_request(host);
|
2011-06-06 17:32:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!host->data) {
|
2013-01-25 15:17:36 +00:00
|
|
|
atmci_finish_request(host);
|
2011-06-06 17:32:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
datastat = atmci_transfer_data(host);
|
|
|
|
ret = atmci_finish_data(host, datastat);
|
|
|
|
atmci_finish_request(host);
|
2011-06-06 17:32:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_start_cmd(struct atmel_mci *host, struct mci_cmd *cmd,
|
2011-06-06 17:32:06 +00:00
|
|
|
unsigned int cmdat)
|
|
|
|
{
|
|
|
|
unsigned flags = 0;
|
|
|
|
unsigned cmdval = 0;
|
|
|
|
|
|
|
|
if (host->cmd != NULL)
|
|
|
|
dev_err(host->hw_dev, "error!\n");
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
if ((atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY) == 0) {
|
2011-06-06 17:32:06 +00:00
|
|
|
dev_err(host->hw_dev, "mci not ready!\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
host->cmd = cmd;
|
2013-01-25 15:17:37 +00:00
|
|
|
cmdval = ATMCI_CMDR_CMDNB_MASK & cmd->cmdidx;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
switch (cmd->resp_type) {
|
|
|
|
case MMC_RSP_R1: /* short CRC, OPCODE */
|
|
|
|
case MMC_RSP_R1b:/* short CRC, OPCODE, BUSY */
|
2013-01-25 15:17:37 +00:00
|
|
|
flags |= ATMCI_CMDR_RSPTYP_48BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
|
|
|
case MMC_RSP_R2: /* long 136 bit + CRC */
|
2013-01-25 15:17:37 +00:00
|
|
|
flags |= ATMCI_CMDR_RSPTYP_136BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
|
|
|
case MMC_RSP_R3: /* short */
|
2013-01-25 15:17:37 +00:00
|
|
|
flags |= ATMCI_CMDR_RSPTYP_48BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
|
|
|
case MMC_RSP_NONE:
|
2013-01-25 15:17:37 +00:00
|
|
|
flags |= ATMCI_CMDR_RSPTYP_NONE;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(host->hw_dev, "unhandled response type 0x%x\n",
|
|
|
|
cmd->resp_type);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-01-25 15:17:37 +00:00
|
|
|
cmdval |= ATMCI_CMDR_RSPTYP & flags;
|
|
|
|
cmdval |= cmdat & ~(ATMCI_CMDR_CMDNB_MASK | ATMCI_CMDR_RSPTYP);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
atmci_writel(host, ATMCI_ARGR, cmd->cmdarg);
|
|
|
|
atmci_writel(host, ATMCI_CMDR, cmdval);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-04 14:48:58 +00:00
|
|
|
static int atmci_card_present(struct mci_host *mci)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci *host = to_mci_host(mci);
|
2011-06-06 17:32:06 +00:00
|
|
|
struct atmel_mci_platform_data *pd = host->hw_dev->platform_data;
|
2013-01-18 11:52:29 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* No gpio, assume card is present */
|
|
|
|
if (!gpio_is_valid(pd->detect_pin))
|
|
|
|
return 1;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
ret = gpio_get_value(pd->detect_pin);
|
|
|
|
|
2013-01-18 11:52:29 +00:00
|
|
|
return ret == 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** init the host interface */
|
2013-02-04 14:48:58 +00:00
|
|
|
static int atmci_reset(struct mci_host *mci, struct device_d *mci_dev)
|
2013-01-18 11:52:29 +00:00
|
|
|
{
|
2013-02-04 14:48:58 +00:00
|
|
|
struct atmel_mci *host = to_mci_host(mci);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
clk_enable(host->clk);
|
2013-01-25 15:17:40 +00:00
|
|
|
atmci_writel(host, ATMCI_DTOR, 0x7f);
|
|
|
|
clk_disable(host->clk);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** change host interface settings */
|
2013-01-25 15:17:36 +00:00
|
|
|
static void atmci_set_ios(struct mci_host *mci, struct mci_ios *ios)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci *host = to_mci_host(mci);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
dev_dbg(host->hw_dev, "atmel_mci_set_ios: bus_width=%d clk=%d\n",
|
2012-02-08 14:49:14 +00:00
|
|
|
ios->bus_width, ios->clock);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:45 +00:00
|
|
|
host->sdc_reg &= ~ATMCI_SDCBUS_MASK;
|
2012-02-08 14:49:14 +00:00
|
|
|
switch (ios->bus_width) {
|
|
|
|
case MMC_BUS_WIDTH_4:
|
2013-01-25 15:17:45 +00:00
|
|
|
host->sdc_reg |= ATMCI_SDCBUS_4BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
2012-02-08 14:49:14 +00:00
|
|
|
case MMC_BUS_WIDTH_8:
|
2013-01-25 15:17:45 +00:00
|
|
|
host->sdc_reg |= ATMCI_SDCBUS_8BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
2012-02-08 14:49:14 +00:00
|
|
|
case MMC_BUS_WIDTH_1:
|
2013-01-25 15:17:45 +00:00
|
|
|
host->sdc_reg |= ATMCI_SDCBUS_1BIT;
|
2011-06-06 17:32:06 +00:00
|
|
|
break;
|
2012-02-08 14:49:14 +00:00
|
|
|
default:
|
|
|
|
return;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 14:49:14 +00:00
|
|
|
if (ios->clock) {
|
2013-01-25 15:17:36 +00:00
|
|
|
atmci_set_clk_rate(host, ios->clock);
|
2013-01-25 15:17:44 +00:00
|
|
|
|
|
|
|
if (host->caps.has_cfg_reg) {
|
|
|
|
/* setup High Speed mode in relation with card capacity */
|
|
|
|
if (ios->timing == MMC_TIMING_SD_HS)
|
|
|
|
host->cfg_reg |= ATMCI_CFG_HSMODE;
|
|
|
|
else
|
|
|
|
host->cfg_reg &= ~ATMCI_CFG_HSMODE;
|
|
|
|
|
|
|
|
atmci_writel(host, ATMCI_CFG, host->cfg_reg);
|
|
|
|
}
|
2011-06-06 17:32:06 +00:00
|
|
|
} else {
|
2013-01-25 15:17:37 +00:00
|
|
|
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
|
2013-01-25 15:17:39 +00:00
|
|
|
if (host->mode_reg) {
|
|
|
|
atmci_readl(host, ATMCI_MR);
|
|
|
|
clk_disable(host->clk);
|
|
|
|
}
|
|
|
|
host->mode_reg = 0;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** handle a command */
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_request(struct mci_host *mci, struct mci_cmd *cmd, struct mci_data *data)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci *host = to_mci_host(mci);
|
2011-06-06 17:32:06 +00:00
|
|
|
u32 stat, cmdat = 0;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-25 15:17:43 +00:00
|
|
|
if (host->need_reset || host->caps.need_reset_after_xfer) {
|
2013-01-25 15:17:42 +00:00
|
|
|
atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
|
|
|
|
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
|
|
|
|
atmci_writel(host, ATMCI_MR, host->mode_reg);
|
2013-01-25 15:17:44 +00:00
|
|
|
if (host->caps.has_cfg_reg)
|
|
|
|
atmci_writel(host, ATMCI_CFG, host->cfg_reg);
|
2013-01-25 15:17:43 +00:00
|
|
|
host->need_reset = false;
|
2013-01-25 15:17:42 +00:00
|
|
|
}
|
2013-01-25 15:17:45 +00:00
|
|
|
atmci_writel(host, ATMCI_SDCR, host->sdc_reg);
|
2013-01-25 15:17:42 +00:00
|
|
|
|
2011-06-06 17:32:06 +00:00
|
|
|
if (cmd->resp_type != MMC_RSP_NONE)
|
2013-01-25 15:17:37 +00:00
|
|
|
cmdat |= ATMCI_CMDR_MAXLAT_64CYC;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
if (data) {
|
2013-01-25 15:17:36 +00:00
|
|
|
atmci_setup_data(host, data);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
cmdat |= ATMCI_CMDR_START_XFER | ATMCI_CMDR_MULTI_BLOCK;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
if (data->flags & MMC_DATA_READ)
|
2013-01-25 15:17:37 +00:00
|
|
|
cmdat |= ATMCI_CMDR_TRDIR_READ;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
ret = atmci_start_cmd(host, cmd, cmdat);
|
2011-06-06 17:32:06 +00:00
|
|
|
if (ret) {
|
2013-01-25 15:17:36 +00:00
|
|
|
atmci_finish_request(host);
|
2011-06-06 17:32:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
stat = atmci_poll_status(host, ATMCI_CMDRDY);
|
2013-01-25 15:17:36 +00:00
|
|
|
return atmci_cmd_done(host, stat);
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static void atmci_info(struct device_d *mci_dev)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci *host = mci_dev->priv;
|
2011-06-06 17:32:06 +00:00
|
|
|
struct atmel_mci_platform_data *pd = host->hw_dev->platform_data;
|
|
|
|
|
|
|
|
printf(" Bus data width: %d bit\n", host->mci.bus_width);
|
|
|
|
|
|
|
|
printf(" Bus frequency: %u Hz\n", host->mci.clock);
|
|
|
|
printf(" Frequency limits: ");
|
|
|
|
if (host->mci.f_min == 0)
|
|
|
|
printf("no lower limit ");
|
|
|
|
else
|
|
|
|
printf("%u Hz lower limit ", host->mci.f_min);
|
|
|
|
if (host->mci.f_max == 0)
|
|
|
|
printf("- no upper limit");
|
|
|
|
else
|
|
|
|
printf("- %u Hz upper limit", host->mci.f_max);
|
|
|
|
|
|
|
|
printf("\n Card detection support: %s\n",
|
2013-01-25 15:17:47 +00:00
|
|
|
gpio_is_valid(pd->detect_pin) ? "yes" : "no");
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
}
|
2013-01-25 15:17:38 +00:00
|
|
|
/*
|
|
|
|
* HSMCI (High Speed MCI) module is not fully compatible with MCI module.
|
|
|
|
* HSMCI provides DMA support and a new config register but no more supports
|
|
|
|
* PDC.
|
|
|
|
*/
|
|
|
|
static void atmci_get_cap(struct atmel_mci *host)
|
|
|
|
{
|
|
|
|
unsigned int version;
|
|
|
|
|
|
|
|
version = atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
|
|
|
|
host->version = version;
|
|
|
|
|
|
|
|
dev_info(host->hw_dev, "version: 0x%x\n", version);
|
|
|
|
|
2013-01-25 15:17:44 +00:00
|
|
|
host->caps.has_cfg_reg = 0;
|
|
|
|
host->caps.has_highspeed = 0;
|
2013-01-25 15:17:42 +00:00
|
|
|
host->caps.need_reset_after_xfer = 1;
|
|
|
|
|
2013-01-25 15:17:38 +00:00
|
|
|
switch (version & 0xf00) {
|
|
|
|
case 0x500:
|
2013-01-25 15:17:39 +00:00
|
|
|
host->caps.has_odd_clk_div = 1;
|
2013-01-25 15:17:38 +00:00
|
|
|
case 0x400:
|
|
|
|
case 0x300:
|
2013-01-25 15:17:44 +00:00
|
|
|
host->caps.has_cfg_reg = 1;
|
|
|
|
host->caps.has_highspeed = 1;
|
2013-01-25 15:17:38 +00:00
|
|
|
case 0x200:
|
2013-01-25 15:17:41 +00:00
|
|
|
host->caps.has_rwproof = 1;
|
2013-01-25 15:17:38 +00:00
|
|
|
case 0x100:
|
2013-01-25 15:17:42 +00:00
|
|
|
host->caps.need_reset_after_xfer = 0;
|
2013-01-25 15:17:38 +00:00
|
|
|
case 0x0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_warn(host->hw_dev,
|
|
|
|
"Unmanaged mci version, set minimum capabilities\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static int atmci_probe(struct device_d *hw_dev)
|
2011-06-06 17:32:06 +00:00
|
|
|
{
|
2013-01-25 15:17:34 +00:00
|
|
|
struct atmel_mci *host;
|
2011-06-06 17:32:06 +00:00
|
|
|
struct atmel_mci_platform_data *pd = hw_dev->platform_data;
|
2013-01-25 15:17:47 +00:00
|
|
|
int ret;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
if (!pd) {
|
|
|
|
dev_err(hw_dev, "missing platform data\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:47 +00:00
|
|
|
if (gpio_is_valid(pd->detect_pin)) {
|
|
|
|
ret = gpio_request(pd->detect_pin, "mci_cd");
|
|
|
|
if (ret) {
|
|
|
|
dev_err(hw_dev, "Impossible to request CD gpio %d (%d)\n",
|
|
|
|
ret, pd->detect_pin);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_direction_input(pd->detect_pin);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(hw_dev, "Impossible to configure CD gpio %d as input (%d)\n",
|
|
|
|
ret, pd->detect_pin);
|
|
|
|
goto err_gpio_cd_request;
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 15:17:38 +00:00
|
|
|
|
2011-06-06 17:32:06 +00:00
|
|
|
host = xzalloc(sizeof(*host));
|
2013-01-25 15:17:36 +00:00
|
|
|
host->mci.send_cmd = atmci_request;
|
|
|
|
host->mci.set_ios = atmci_set_ios;
|
|
|
|
host->mci.init = atmci_reset;
|
2013-02-04 14:48:58 +00:00
|
|
|
host->mci.card_present = atmci_card_present;
|
2011-08-15 07:44:04 +00:00
|
|
|
host->mci.hw_dev = hw_dev;
|
2013-09-17 07:50:45 +00:00
|
|
|
host->mci.devname = pd->devname;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2011-06-21 22:10:16 +00:00
|
|
|
if (pd->bus_width >= 4)
|
2013-06-03 08:59:35 +00:00
|
|
|
host->mci.host_caps |= MMC_CAP_4_BIT_DATA;
|
2011-06-21 22:10:16 +00:00
|
|
|
if (pd->bus_width == 8)
|
2013-06-03 08:59:35 +00:00
|
|
|
host->mci.host_caps |= MMC_CAP_8_BIT_DATA;
|
2012-01-13 15:55:24 +00:00
|
|
|
host->slot_b = pd->slot_b;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:37 +00:00
|
|
|
host->regs = dev_request_mem_region(hw_dev, 0);
|
2011-06-06 17:32:06 +00:00
|
|
|
host->hw_dev = hw_dev;
|
|
|
|
hw_dev->priv = host;
|
|
|
|
host->clk = clk_get(hw_dev, "mci_clk");
|
2011-09-09 11:46:48 +00:00
|
|
|
if (IS_ERR(host->clk)) {
|
2011-06-06 17:32:06 +00:00
|
|
|
dev_err(hw_dev, "no mci_clk\n");
|
2013-01-25 15:17:47 +00:00
|
|
|
ret = PTR_ERR(host->clk);
|
|
|
|
goto err_gpio_cd_request;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:40 +00:00
|
|
|
clk_enable(host->clk);
|
|
|
|
atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
|
|
|
|
atmci_writel(host, ATMCI_IDR, ~0UL);
|
2013-01-25 15:17:39 +00:00
|
|
|
host->bus_hz = clk_get_rate(host->clk);
|
2013-01-25 15:17:40 +00:00
|
|
|
clk_disable(host->clk);
|
2011-06-06 17:32:06 +00:00
|
|
|
|
|
|
|
host->mci.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
|
|
|
|
|
2013-01-25 15:17:39 +00:00
|
|
|
host->mci.f_min = DIV_ROUND_UP(host->bus_hz, 512);
|
|
|
|
host->mci.f_max = host->bus_hz >> 1;
|
2011-06-06 17:32:06 +00:00
|
|
|
|
2013-01-25 15:17:38 +00:00
|
|
|
atmci_get_cap(host);
|
|
|
|
|
2013-01-25 15:17:44 +00:00
|
|
|
if (host->caps.has_highspeed)
|
2013-06-03 08:59:35 +00:00
|
|
|
host->mci.host_caps |= MMC_CAP_SD_HIGHSPEED;
|
2013-01-25 15:17:44 +00:00
|
|
|
|
2013-01-25 15:17:45 +00:00
|
|
|
if (host->slot_b)
|
|
|
|
host->sdc_reg = ATMCI_SDCSEL_SLOT_B;
|
|
|
|
else
|
|
|
|
host->sdc_reg = ATMCI_SDCSEL_SLOT_A;
|
|
|
|
|
2013-05-26 13:06:53 +00:00
|
|
|
if (IS_ENABLED(CONFIG_MCI_INFO))
|
|
|
|
hw_dev->info = atmci_info;
|
|
|
|
|
2011-06-06 17:32:06 +00:00
|
|
|
mci_register(&host->mci);
|
|
|
|
|
|
|
|
return 0;
|
2013-01-25 15:17:47 +00:00
|
|
|
|
|
|
|
err_gpio_cd_request:
|
|
|
|
if (gpio_is_valid(pd->detect_pin))
|
|
|
|
gpio_free(pd->detect_pin);
|
|
|
|
|
|
|
|
return ret;
|
2011-06-06 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 15:17:36 +00:00
|
|
|
static struct driver_d atmci_driver = {
|
2011-06-06 17:32:06 +00:00
|
|
|
.name = "atmel_mci",
|
2013-01-25 15:17:36 +00:00
|
|
|
.probe = atmci_probe,
|
2011-06-06 17:32:06 +00:00
|
|
|
};
|
2013-02-12 18:08:57 +00:00
|
|
|
device_platform_driver(atmci_driver);
|