s5p4418-linux MMC驱动子系统分析

平台说明:

s5p4418

SD/MMC控制器驱动:Synopsys DesignWare Dw_mmc-pltfm.c (drivers\mmc\host) 和Dw_mmc.c (drivers\mmc\host)

linux 版本:3.4.29


驱动原理:

1.SD/MMC控制器的的接口是SDIO接口,工作方式有三种,单线,四线,SPI。

2.在MMC的设备模型中,用struct mmc_host来代表一个mmc控制器,用struct mmc_card表示一个卡,如SD卡,TF卡,emmc,wifi模块等。struct sdio_func
    表示具有SDIO功能的卡。设备struct mmc_card对应驱动struct mmc_card,总线名:"mmc";设备struct sdio_func对应驱动struct sdio_driver,总线名:"sdio";

    后者是前者的一种容器,或者是继承。

3.在drivers/mmc/*下面的三个文件夹分别是card,core,host。代表,一个块设备驱动,mmc的核心,控制器驱动。

如下图:


4.在Dw_mmc.c (drivers\mmc\host)中,控制器驱动的probe函数里,执行如下代码:

for (i = 0; i < host->num_slots; i++) {
		ret = dw_mci_init_slot(host, i);
		if (ret) {
			ret = -ENODEV;
			goto err_init_slot;
		}
	}

表示申请了三个struct mmc_host,代表三个卡槽,三个mmc控制器。在dw_mci_init_slot函数中调用了:

       struct mmc_host *mmc;
	struct dw_mci_slot *slot;

	mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->dev);
…………………………
if(host->pdata->mode == DMA_MODE)	
		mmc->ops = &dw_mci_ops;
	else 
		mmc->ops = &dw_mci_nodma_ops;
……………………………
mmc_add_host(mmc);
……………………………

首先是申请struct mmc_host,然后添加struct mmc_host的操作方法,最后是添加host到设备模型中,调用device_add函数。类似与申请一个IIC的adapter。

在申请struct mmc_host的阶段还调用了一个函数:INIT_DELAYED_WORK(&host->detect, mmc_rescan);当有卡插入时候调用mmc_rescan函数。这个函数的主要内容是完成对插入卡就行判断,选择不同的驱动。

/* Order's important: probe SDIO, then SD, then MMC */
	if (!mmc_attach_sdio(host))
		return 0;
	if (!mmc_attach_sd(host))
		return 0;
	if (!mmc_attach_mmc(host))
		return 0;

首先执行mmc_attach_sdio(host)

        /*
	 * Detect and init the card.
	 */
	err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
……
err = sdio_init_func(host->card, i + 1);
……
err = sdio_add_func(host->card->sdio_func[i]);
……


这里重要的结构也是有三个,第一个是如果存在则申请一个struct mmc_card,调用了

/*
	 * Allocate card structure.
	 */
	card = mmc_alloc_card(host, NULL);

--------------------------------
/*
 * Allocate and initialise a new MMC card structure.
 */
struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
{
    struct mmc_card *card;

    card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
    if (!card)
        return ERR_PTR(-ENOMEM);

    card->host = host;

    device_initialize(&card->dev);

    card->dev.parent = mmc_classdev(host);
    card->dev.bus = &mmc_bus_type;
    card->dev.release = mmc_release_card;
    card->dev.type = type;

    return card;
}
第二个函数是申请了struct sdio_func,调用:

	struct sdio_func *func;
	func = sdio_alloc_func(card);
----------------------------------------
/*
 * Allocate and initialise a new SDIO function structure.
 */
struct sdio_func *sdio_alloc_func(struct mmc_card *card)
{
	struct sdio_func *func;

	func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
	if (!func)
		return ERR_PTR(-ENOMEM);

	func->card = card;

	device_initialize(&func->dev);

	func->dev.parent = &card->dev;
	func->dev.bus = &sdio_bus_type;
	func->dev.release = sdio_release_func;

	return func;
}

第三个创建设备模型,调用device_add(&func->dev);函数,创建SDIO设备。

	dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
ret = device_add(&func->dev);
	if (ret == 0)
		sdio_func_set_present(func);

注意:MMC设备和驱动匹配是不需要名称匹配的。只要总线名称一致就可以probe。

总的过程,就是host mmc设备的建立,等待卡的插入,自动判断卡的类型,创建不同类型的设备,驱动程序则由总线名称匹配。SDIO设备在文件Sdio_bus.c (drivers\mmc\core)   定义了其设备模型。mmc设备在Bus.c (drivers\mmc\core)  定义了设备模型。



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
s5p6818-serial是一个基于S5P6818处理器的串口通信程序。该程序的主要功能是通过串口与外设进行数据交互,具体包括以下几个方面: 1. 引入头文件 ``` #include <stdio.h> #include <fcntl.h> #include <string.h> #include <unistd.h> #include <termios.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> ``` 这些头文件包含了本程序所需的各种系统级别的函数和宏定义。 2. 定义串口参数结构体 ``` struct termios options; ``` 该结构体用于设置串口的各种参数,例如波特率、数据位数、停止位数等。 3. 打开串口设备文件 ``` int fd = open("/dev/ttySAC1", O_RDWR|O_NOCTTY|O_NDELAY); ``` 该函数打开/dev/ttySAC1设备文件,并且设置了一些选项: - O_RDWR:以读写方式打开串口设备文件 - O_NOCTTY:不将该串口设备设置为进程的控制终端 - O_NDELAY:非阻塞方式打开串口设备文件 4. 配置串口参数 ``` tcgetattr(fd, &options); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &options); ``` 该函数配置了串口的各种参数,具体包括波特率、数据位数、停止位数、校验位等。其中,函数cfsetispeed和cfsetospeed设置了串口的输入输出速率,options.c_cflag设置了串口的控制标志位,options.c_lflag设置了串口的本地标志位,options.c_oflag设置了串口的输出标志位。 5. 读写数据 ``` int len = write(fd, buf, count); len = read(fd, buf, count); ``` 该函数通过调用系统API函数write和read进行串口数据的读写。其中,write函数将buf中的count个字节写入串口设备文件,read函数从串口设备文件中读取count个字节到buf中。这里需要注意的是,串口数据的读写需要按照一定的协议进行,例如起始位、停止位、校验位等。 6. 关闭串口 ``` close(fd); ``` 该函数关闭串口设备文件,释放资源。 综上所述,s5p6818-serial是一个基于S5P6818处理器的串口通信程序,用于与外设进行数据交互。该程序通过打开串口设备文件、配置串口参数、读写数据等步骤实现数据的传输。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值