u-boot-2009.08在mini2440上的移植(九)---增加SD卡功能
移植環(huán)境
1,主機(jī)環(huán)境:VMare下CentOS 5.5 ,1G內(nèi)存。
2,集成開發(fā)環(huán)境:Elipse IDE
3,編譯編譯環(huán)境:arm-linux-gcc v4.4.3,arm-none-eabi-gcc v4.5.1。
4,開發(fā)板:mini2440,2M nor flash,128M nand flash。
5,u-boot版本:u-boot-2009.08
6,參考文章:
http://blogimg.chinaunix.net/blog/upfile2/100811115954.pdf
9.1,實(shí)現(xiàn)u-boot的SD卡功能
SD卡的支持參考了buserror的Git代碼倉庫中的源碼,他也是為mini2440 移植的。它使用的代碼也是Openmoko的GTA2 源碼。因?yàn)镚TA2 可以在U-boot中使用SD卡更新系統(tǒng)。將其SD卡底層驅(qū)動代碼搬過來,經(jīng)過簡單的修改就可以使用了。
這個(gè)功能需要修改 5 個(gè)文件,添加3 個(gè)驅(qū)動代碼文件。
【1】打開/common/cmd_mem.c,定位到35行,加入下面代碼:
*
* Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
*/
#include
#include
#ifdef CONFIG_HAS_DATAFLASH
#include
#endif
#if defined(CONFIG_CMD_MMC)
#include
#endif
#include
定位到357行,修改如下:
#if defined(CONFIG_CMD_MMC)
extern int mmc2info(ulong addr);
extern int mmc_write(uchar *src, ulong dst, int size);
#endif
int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
定位到411行附近,加入下面代碼:
puts ("Copy to Flash... ");
rc = flash_write ((char *)addr, dest, count*size);
if (rc != 0) {
flash_perror (rc);
return (1);
}
puts ("donen");
return 0;
}
#endif
#if defined(CONFIG_CMD_MMC)
if (mmc2info(dest)) {
int rc;
puts ("Copy to MMC... ");
switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
case 0:
putc ('n');
return 1;
case -1:
puts ("failedn");
return 1;
default:
printf ("%s[%d] FIXME: rc=%dn",__FILE__,__LINE__,rc);
return 1;
}
puts ("donen");
return 0;
}
if (mmc2info(addr)) {
int rc;
puts ("Copy from MMC... ");
switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
case 0:
putc ('n');
return 1;
case -1:
puts ("failedn");
return 1;
default:
printf ("%s[%d] FIXME: rc=%dn",__FILE__,__LINE__,rc);
return 1;
}
puts ("donen");
return 0;
}
#endif
#ifdef CONFIG_HAS_DATAFLASH
/* Check if we are copying from RAM or Flash to DataFlash */
if (addr_dataflash(dest) && !addr_dataflash(addr)){
int rc;
puts ("Copy to DataFlash... ");
【2】打開//common/cmd_mmc.c,定位到53行,修改如下:
cmd_usage(cmdtp);
return 1;
}
if (mmc_init(dev) != 0) {//if (mmc_legacy_init(dev) != 0) {
puts("No MMC card foundn");
return 1;
}
【3】打開/cpu/arm920t/s3c24x0/Makefile,定位到33行,如入下面代碼:
COBJS-$(CONFIG_USE_IRQ) += interrupts.o
COBJS-y+= speed.o
COBJS-y+= timer.o
COBJS-y+= usb.o
COBJS-y+= usb_ohci.o
COBJS-y+= mmc.o
SRCS:= $(SOBJS:.o=.S) $(COBJS-y:.o=.c)
【4】打開/include/mmc.h,定位到172行,修改如下:
#define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
#define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
#if 0
struct mmc_cid {
unsigned long psn;
unsigned short oid;
unsigned char mid;
unsigned char prv;
unsigned char mdt;
char pnm[7];
};
struct mmc_csd
{
u8csd_structure:2,
spec_vers:4,
... ...
u8one:1;
};
#endif
struct mmc_cmd {
ushort cmdidx;
uint resp_type;
uint cmdarg;
uint response[4];
uint flags;
};
定位到271行,修改如下:
int mmc_register(struct mmc *mmc);
int mmc_initialize(bd_t *bis);
//int mmc_init(struct mmc *mmc);
//int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size);
int mmc_init(int verbose);
int mmc_read(ulong src, uchar *dst, int size);
struct mmc *find_mmc_device(int dev_num);
void print_mmc_devices(char separator);
【5】打開/include/part.h,定位到65行,加入下面代碼:
#define IF_TYPE_USB4
#define IF_TYPE_DOC5
#define IF_TYPE_MMC6
#define IF_TYPE_SD7
#define IF_TYPE_SATA8
#define IF_TYPE_SDHC9
/* Part types */
#define PART_TYPE_UNKNOWN0x00
【6】添加的驅(qū)動代碼文件/cpu/arm920t/s3c24x0/mmc.c:
/*
* u-boot S3C2410 MMC/SD card driver
* (C) Copyright 2006 by OpenMoko, Inc.
* Author: Harald Welte
*
* based on u-boot pxa MMC driver and linux/drivers/mmc/s3c2410mci.c
* (C) 2005-2005 Thomas Kleffel
*
* 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
#include
#include
#include
#include
#include
#include
#include
#include
#if defined(CONFIG_MMC) && defined(CONFIG_MMC_S3C)
#ifdef DEBUG
#define pr_debug(fmt, args...) printf(fmt, ##args)
#else
#define pr_debug(...) do { } while(0)
#endif
#define CONFIG_MMC_WIDE
static S3C2410_SDI *sdi;
static block_dev_desc_t mmc_dev;
block_dev_desc_t * mmc_get_dev(int dev)
{
return ((block_dev_desc_t *)&mmc_dev);
}
/*
* FIXME needs to read cid and csd info to determine block size
* and other parameters
*/
static uchar mmc_buf[MMC_BLOCK_SIZE];
static mmc_csd_t mmc_csd;
static int mmc_ready = 0;
static int wide = 0;
#define CMD_F_RESP0x01
#define CMD_F_RESP_LONG0x02
#define CMD_F_RESP_R7 CMD_F_RESP
static u_int32_t *mmc_cmd(ushort cmd, ulong arg, ushort flags)
{
static u_int32_t resp[5];
u_int32_t ccon, csta;
u_int32_t csta_rdy_bit = S3C2410_SDICMDSTAT_CMDSENT;
memset(resp, 0, sizeof(resp));
debug("mmc_cmd CMD%d arg=0x%08x flags=%xn", cmd, arg, flags);
sdi->SDICSTA = 0xffffffff;
sdi->SDIDSTA = 0xffffffff;
sdi->SDIFSTA = 0xffffffff;
sdi->SDICARG = arg;
ccon = cmd & S3C2410_SDICMDCON_INDEX;
ccon |= S3C2410_SDICMDCON_SENDERHOST|S3C2410_SDICMDCON_CMDSTART;
if (flags & CMD_F_RESP) {
ccon |= S3C2410_SDICMDCON_WAITRSP;
csta_rdy_bit = S3C2410_SDICMDSTAT_RSPFIN; /* 1 << 9 */
}
if (flags & CMD_F_RESP_LONG)
ccon |= S3C2410_SDICMDCON_LONGRSP;
sdi->SDICCON = ccon;
while (1) {
csta = sdi->SDICSTA;
if (csta & csta_rdy_bit)
break;
if (csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
printf("===============> MMC CMD Timeoutn");
sdi->SDICSTA |= S3C2410_SDICMDSTAT_CMDTIMEOUT;
break;
}
}
debug("final MMC CMD status 0x%xn", csta);
sdi->SDICSTA |= csta_rdy_bit;
if (flags & CMD_F_RESP) {
resp[0] = sdi->SDIRSP0;
resp[1] = sdi->SDIRSP1;
resp[2] = sdi->SDIRSP2;
resp[3] = sdi->SDIRSP3;
}
return resp;
}
#define FIFO_FILL(host) ((host->SDIFSTA & S3C2410_SDIFSTA_COUNTMASK) >> 2)
static int mmc_block_read(uchar *dst, ulong src, ulong len)
{
u_int32_t dcon, fifo;
u_int32_t *dst_u32 = (u_int32_t *)dst;
u_int32_t *resp;
if (len == 0)
return 0;
debug("mmc_block_rd dst %lx src %lx len %dn", (ulong)dst, src, len);
/* set block len */
resp = mmc_cmd(MMC_CMD_SET_BLOCKLEN, len, CMD_F_RESP);
sdi->SDIBSIZE = len;
//sdi->SDIPRE = 0xff;
/* setup data */
dcon = (len >> 9) & S3C2410_SDIDCON_BLKNUM;
dcon |= S3C2410_SDIDCON_BLOCKMODE;
dcon |= S3C2410_SDIDCON_RXAFTERCMD|S3C2410_SDIDCON_XFER_RXSTART;
if (wide)
dcon |= S3C2410_SDIDCON_WIDEBUS;
#if defined(CONFIG_S3C2440) || defined(CONFIG_S3C2442)
dcon |= S3C2440_SDIDCON_DS_WORD | S3C2440_SDIDCON_DATSTART;
#endif
sdi->SDIDCON = dcon;
/* send read command */
resp = mmc_cmd(MMC_CMD_READ_BLOCK, (mmc_dev.if_type == IF_TYPE_SDHC) ? (src >> 9) : src, CMD_F_RESP);
while (len > 0) {
u_int32_t sdidsta = sdi->SDIDSTA;
fifo = FIFO_FILL(sdi);
if (sdidsta & (S3C2410_SDIDSTA_FIFOFAIL|
S3C2410_SDIDSTA_CRCFAIL|
S3C2410_SDIDSTA_RXCRCFAIL|
S3C2410_SDIDSTA_DATATIMEOUT)) {
printf("mmc_block_read: err SDIDSTA=0x%08xn", sdidsta);
return -EIO;
}
while (fifo--) {
//debug("dst_u32 = 0x%08xn", dst_u32);
*(dst_u32++) = sdi->SDIDAT;
if (len >= 4)
len -= 4;
else {
len = 0;
break;
}
}
}