Commit 272cc70b authored by Andy Fleming's avatar Andy Fleming
Browse files

Add MMC Framework



Here's a new framework (based roughly off the linux one) for managing
MMC controllers.  It handles all of the standard SD/MMC transactions,
leaving the host drivers to implement only what is necessary to
deal with their specific hardware.

This also hooks the infrastructure into the PowerPC board code
(similar to how the ethernet infrastructure now hooks in)

Some of this code was contributed by Dave Liu <daveliu@freescale.com>
Signed-off-by: default avatarAndy Fleming <afleming@freescale.com>
parent 1de97f98
......@@ -25,6 +25,7 @@
#include <command.h>
#include <mmc.h>
#ifndef CONFIG_GENERIC_MMC
int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
if (mmc_legacy_init (1) != 0) {
......@@ -39,3 +40,134 @@ U_BOOT_CMD(
"init mmc card",
NULL
);
#endif /* !CONFIG_GENERIC_MMC */
static void print_mmcinfo(struct mmc *mmc)
{
printf("Device: %s\n", mmc->name);
printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
printf("Tran Speed: %d\n", mmc->tran_speed);
printf("Rd Block Len: %d\n", mmc->read_bl_len);
printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
(mmc->version >> 4) & 0xf, mmc->version & 0xf);
printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
printf("Capacity: %lld\n", mmc->capacity);
printf("Bus Width: %d-bit\n", mmc->bus_width);
}
int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
struct mmc *mmc;
int dev_num;
if (argc < 2)
dev_num = 0;
else
dev_num = simple_strtoul(argv[1], NULL, 0);
mmc = find_mmc_device(dev_num);
if (mmc) {
mmc_init(mmc);
print_mmcinfo(mmc);
}
return 0;
}
U_BOOT_CMD(mmcinfo, 2, 0, do_mmcinfo, "mmcinfo <dev num>-- display MMC info\n",
NULL);
int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int rc = 0;
switch (argc) {
case 3:
if (strcmp(argv[1], "rescan") == 0) {
int dev = simple_strtoul(argv[2], NULL, 10);
struct mmc *mmc = find_mmc_device(dev);
mmc_init(mmc);
return 0;
}
case 0:
case 1:
case 4:
printf("Usage:\n%s\n", cmdtp->usage);
return 1;
case 2:
if (!strcmp(argv[1], "list")) {
print_mmc_devices('\n');
return 0;
}
return 1;
default: /* at least 5 args */
if (strcmp(argv[1], "read") == 0) {
int dev = simple_strtoul(argv[2], NULL, 10);
void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
u32 cnt = simple_strtoul(argv[5], NULL, 16);
u32 n;
u32 blk = simple_strtoul(argv[4], NULL, 16);
struct mmc *mmc = find_mmc_device(dev);
printf("\nMMC read: dev # %d, block # %d, count %d ... ",
dev, blk, cnt);
mmc_init(mmc);
n = mmc->block_dev.block_read(dev, blk, cnt, addr);
/* flush cache after read */
flush_cache((ulong)addr, cnt * 512); //FIXME
printf("%d blocks read: %s\n",
n, (n==cnt) ? "OK" : "ERROR");
return (n == cnt) ? 0 : 1;
} else if (strcmp(argv[1], "write") == 0) {
int dev = simple_strtoul(argv[2], NULL, 10);
void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
u32 cnt = simple_strtoul(argv[5], NULL, 16);
u32 n;
struct mmc *mmc = find_mmc_device(dev);
int blk = simple_strtoul(argv[4], NULL, 16);
printf("\nMMC write: dev # %d, block # %d, count %d ... ",
dev, blk, cnt);
mmc_init(mmc);
n = mmc->block_dev.block_write(dev, blk, cnt, addr);
printf("%d blocks written: %s\n",
n, (n == cnt) ? "OK" : "ERROR");
return (n == cnt) ? 0 : 1;
} else {
printf("Usage:\n%s\n", cmdtp->usage);
rc = 1;
}
return rc;
}
}
U_BOOT_CMD(
mmc, 6, 1, do_mmcops,
"mmc - MMC sub system\n",
"mmc read <device num> addr blk# cnt\n"
"mmc write <device num> addr blk# cnt\n"
"mmc rescan <device num>\n"
"mmc list - lists available devices\n");
......@@ -25,6 +25,7 @@ include $(TOPDIR)/config.mk
LIB := $(obj)libmmc.a
COBJS-$(CONFIG_GENERIC_MMC) += mmc.o
COBJS-$(CONFIG_ATMEL_MCI) += atmel_mci.o
COBJS-$(CONFIG_BFIN_SDH) += bfin_sdh.o
COBJS-$(CONFIG_OMAP3_MMC) += omap3_mmc.o
......
/*
* Copyright 2008, Freescale Semiconductor, Inc
* Andy Fleming
*
* Based vaguely on the Linux code
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <config.h>
#include <common.h>
#include <command.h>
#include <mmc.h>
#include <part.h>
#include <malloc.h>
#include <linux/list.h>
#include <mmc.h>
static struct list_head mmc_devices;
static int cur_dev_num = -1;
int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
{
return mmc->send_cmd(mmc, cmd, data);
}
int mmc_set_blocklen(struct mmc *mmc, int len)
{
struct mmc_cmd cmd;
cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = len;
cmd.flags = 0;
return mmc_send_cmd(mmc, &cmd, NULL);
}
struct mmc *find_mmc_device(int dev_num)
{
struct mmc *m;
struct list_head *entry;
list_for_each(entry, &mmc_devices) {
m = list_entry(entry, struct mmc, link);
if (m->block_dev.dev == dev_num)
return m;
}
printf("MMC Device %d not found\n", dev_num);
return NULL;
}
static ulong
mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
{
struct mmc_cmd cmd;
struct mmc_data data;
int err;
int stoperr = 0;
struct mmc *mmc = find_mmc_device(dev_num);
int blklen;
if (!mmc)
return -1;
blklen = mmc->write_bl_len;
err = mmc_set_blocklen(mmc, mmc->write_bl_len);
if (err) {
printf("set write bl len failed\n\r");
return err;
}
if (blkcnt > 1)
cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
else
cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
if (mmc->high_capacity)
cmd.cmdarg = start;
else
cmd.cmdarg = start * blklen;
cmd.resp_type = MMC_RSP_R1;
cmd.flags = 0;
data.src = src;
data.blocks = blkcnt;
data.blocksize = blklen;
data.flags = MMC_DATA_WRITE;
err = mmc_send_cmd(mmc, &cmd, &data);
if (err) {
printf("mmc write failed\n\r");
return err;
}
if (blkcnt > 1) {
cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
cmd.cmdarg = 0;
cmd.resp_type = MMC_RSP_R1b;
cmd.flags = 0;
stoperr = mmc_send_cmd(mmc, &cmd, NULL);
}
return blkcnt;
}
int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
{
struct mmc_cmd cmd;
struct mmc_data data;
cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
if (mmc->high_capacity)
cmd.cmdarg = blocknum;
else
cmd.cmdarg = blocknum * mmc->read_bl_len;
cmd.resp_type = MMC_RSP_R1;
cmd.flags = 0;
data.dest = dst;
data.blocks = 1;
data.blocksize = mmc->read_bl_len;
data.flags = MMC_DATA_READ;
return mmc_send_cmd(mmc, &cmd, &data);
}
int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
{
char *buffer;
int i;
int blklen = mmc->read_bl_len;
int startblock = src / blklen;
int endblock = (src + size - 1) / blklen;
int err = 0;
/* Make a buffer big enough to hold all the blocks we might read */
buffer = malloc(blklen);
if (!buffer) {
printf("Could not allocate buffer for MMC read!\n");
return -1;
}
/* We always do full block reads from the card */
err = mmc_set_blocklen(mmc, mmc->read_bl_len);
if (err)
return err;
for (i = startblock; i <= endblock; i++) {
int segment_size;
int offset;
err = mmc_read_block(mmc, buffer, i);
if (err)
goto free_buffer;
/*
* The first block may not be aligned, so we
* copy from the desired point in the block
*/
offset = (src & (blklen - 1));
segment_size = MIN(blklen - offset, size);
memcpy(dst, buffer + offset, segment_size);
dst += segment_size;
src += segment_size;
size -= segment_size;
}
free_buffer:
free(buffer);
return err;
}
static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
{
int err;
int i;
struct mmc *mmc = find_mmc_device(dev_num);
if (!mmc)
return 0;
/* We always do full block reads from the card */
err = mmc_set_blocklen(mmc, mmc->read_bl_len);
if (err) {
return 0;
}
for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
err = mmc_read_block(mmc, dst, i);
if (err) {
printf("block read failed: %d\n", err);
return i - start;
}
}
return blkcnt;
}
int mmc_go_idle(struct mmc* mmc)
{
struct mmc_cmd cmd;
int err;
udelay(1000);
cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
cmd.cmdarg = 0;
cmd.resp_type = MMC_RSP_NONE;
cmd.flags = 0;
err = mmc_send_cmd(mmc, &cmd, NULL);
if (err)
return err;
udelay(2000);
return 0;
}
int
sd_send_op_cond(struct mmc *mmc)
{
int timeout = 1000;
int err;
struct mmc_cmd cmd;
do {
cmd.cmdidx = MMC_CMD_APP_CMD;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = 0;
cmd.flags = 0;
err = mmc_send_cmd(mmc, &cmd, NULL);
if (err)
return err;
cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
cmd.resp_type = MMC_RSP_R3;
cmd.cmdarg = mmc->voltages;
if (mmc->version == SD_VERSION_2)
cmd.cmdarg |= OCR_HCS;
err = mmc_send_cmd(mmc, &cmd, NULL);
if (err)
return err;
udelay(1000);
} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
if (timeout <= 0)
return UNUSABLE_ERR;
if (mmc->version != SD_VERSION_2)
mmc->version = SD_VERSION_1_0;
mmc->ocr = ((uint *)(cmd.response))[0];
mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
mmc->rca = 0;
return 0;
}
int mmc_send_op_cond(struct mmc *mmc)
{
int timeout = 1000;
struct mmc_cmd cmd;
int err;
/* Some cards seem to need this */
mmc_go_idle(mmc);
do {
cmd.cmdidx = MMC_CMD_SEND_OP_COND;
cmd.resp_type = MMC_RSP_R3;
cmd.cmdarg = OCR_HCS | mmc->voltages;
cmd.flags = 0;
err = mmc_send_cmd(mmc, &cmd, NULL);
if (err)
return err;
udelay(1000);
} while (!(cmd.response[0] & OCR_BUSY) && timeout--);
if (timeout <= 0)
return UNUSABLE_ERR;
mmc->version = MMC_VERSION_UNKNOWN;
mmc->ocr = ((uint *)(cmd.response))[0];
mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
mmc->rca = 0;
return 0;
}
int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
{
struct mmc_cmd cmd;
struct mmc_data data;
int err;
/* Get the Card Status Register */
cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = 0;
cmd.flags = 0;
data.dest = ext_csd;
data.blocks = 1;
data.blocksize = 512;
data.flags = MMC_DATA_READ;
err = mmc_send_cmd(mmc, &cmd, &data);
return err;
}
int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
{
struct mmc_cmd cmd;
cmd.cmdidx = MMC_CMD_SWITCH;
cmd.resp_type = MMC_RSP_R1b;
cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
(index << 16) |
(value << 8);
cmd.flags = 0;
return mmc_send_cmd(mmc, &cmd, NULL);
}
int mmc_change_freq(struct mmc *mmc)
{
char ext_csd[512];
char cardtype;
int err;
mmc->card_caps = 0;
/* Only version 4 supports high-speed */
if (mmc->version < MMC_VERSION_4)
return 0;
mmc->card_caps |= MMC_MODE_4BIT;
err = mmc_send_ext_csd(mmc, ext_csd);
if (err)
return err;
if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
mmc->high_capacity = 1;
cardtype = ext_csd[196] & 0xf;
err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
if (err)
return err;
/* Now check to see that it worked */
err = mmc_send_ext_csd(mmc, ext_csd);
if (err)
return err;
/* No high-speed support */
if (!ext_csd[185])
return 0;
/* High Speed is set, there are two types: 52MHz and 26MHz */
if (cardtype & MMC_HS_52MHZ)
mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
else
mmc->card_caps |= MMC_MODE_HS;
return 0;
}
int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
{
struct mmc_cmd cmd;
struct mmc_data data;
/* Switch the frequency */
cmd.cmdidx = SD_CMD_SWITCH_FUNC;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = (mode << 31) | 0xffffff;
cmd.cmdarg &= ~(0xf << (group * 4));
cmd.cmdarg |= value << (group * 4);
cmd.flags = 0;
data.dest = (char *)resp;
data.blocksize = 64;
data.blocks = 1;
data.flags = MMC_DATA_READ;
return mmc_send_cmd(mmc, &cmd, &data);
}
int sd_change_freq(struct mmc *mmc)
{
int err;
struct mmc_cmd cmd;
uint scr[2];
uint switch_status[16];
struct mmc_data data;
int timeout;
mmc->card_caps = 0;
/* Read the SCR to find out if this card supports higher speeds */
cmd.cmdidx = MMC_CMD_APP_CMD;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = mmc->rca << 16;
cmd.flags = 0;
err = mmc_send_cmd(mmc, &cmd, NULL);
if (err)
return err;
cmd.cmdidx = SD_CMD_APP_SEND_SCR;
cmd.resp_type = MMC_RSP_R1;
cmd.cmdarg = 0;
cmd.flags = 0;
timeout = 3;
retry_scr:
data.dest = (char *)&scr;
data.blocksize = 8;
data.blocks = 1;