SDIO驱动学习

在分析了sdio,emmc驱动后,把剩余的sdio部分也学习下。

代码很多不仔细看了,看关键的

int mmc_attach_sdio(struct mmc_host *host)
{
	
	err = mmc_send_io_op_cond(host, 0, &ocr);
	mmc_attach_bus(host, &mmc_sdio_ops);
	rocr = mmc_select_voltage(host, ocr);

	
	err = mmc_sdio_init_card(host, rocr, NULL, 0);
	
	funcs = (ocr & 0x70000000) >> 28;
	card->sdio_funcs = 0;
	for (i = 0; i < funcs; i++, card->sdio_funcs++) {
		err = sdio_init_func(host->card, i + 1);
	}

	mmc_release_host(host);
	for (i = 0;i < funcs;i++) {
		err = sdio_add_func(host->card->sdio_func[i]);
	}

	
	mmc_claim_host(host);
}

最终在/sys/bus/sdio/devices/下生成对于的设备

看下mmc_send_io_op_cond函数

#define SD_IO_SEND_OP_COND          5 /* bcr  [23:0] OCR         R4  */
int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
{
	struct mmc_command cmd = {};
	int i, err = 0;

	cmd.opcode = SD_IO_SEND_OP_COND;
	cmd.arg = ocr;
	cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;

	for (i = 100; i; i--) {
		err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
			if (cmd.resp[0] & MMC_CARD_BUSY)
				break;
	}

	*rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
}

发送CMD5命令,接收R4返回的状态。 

 SDIO协议文档位置https://www.sdcard.org/downloads/pls/index.html,下载这一篇SDIO Simplified Specification

 

看下sdio总线设备和驱动的匹配

static struct bus_type sdio_bus_type = {
	.match		= sdio_bus_match,
};

static int sdio_bus_match(struct device *dev, struct device_driver *drv)
{
	struct sdio_func *func = dev_to_sdio_func(dev);
	struct sdio_driver *sdrv = to_sdio_driver(drv);

	if (sdio_match_device(func, sdrv))
		return 1;

	return 0;
}

static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
	struct sdio_driver *sdrv)
{
	const struct sdio_device_id *ids;

	ids = sdrv->id_table;

	if (ids) {
		while (ids->class || ids->vendor || ids->device) {
			if (sdio_match_one(func, ids))
				return ids;
			ids++;
		}
	}

	return NULL;
}

static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
	const struct sdio_device_id *id)
{
	if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
		return NULL;
	if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
		return NULL;
	if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
		return NULL;
	return id;
}

也就是根据sdio的id跟驱动的id进行匹配,匹配后跑probe函数。

所以我们看sdio驱动时,有类似的注册方法

static const struct sdio_device_id b43_sdio_ids[] = {
	{ SDIO_DEVICE(0x02d0, 0x044b) }, /* Nintendo Wii WLAN daughter card */
	{ SDIO_DEVICE(0x0092, 0x0004) }, /* C-guys, Inc. EW-CG1102GC */
	{ },
};

static struct sdio_driver b43_sdio_driver = {
	.name		= "b43-sdio",
	.id_table	= b43_sdio_ids,
	.probe		= b43_sdio_probe,
	.remove		= b43_sdio_remove,
};

int b43_sdio_init(void)
{
	return sdio_register_driver(&b43_sdio_driver);
}

void b43_sdio_exit(void)
{
	sdio_unregister_driver(&b43_sdio_driver);
}

 

`struct sdio_func`是Linux内核中表示SDIO设备的结构体,它包含了SDIO设备的各种信息。在Linux内核中,每个SDIO设备都对应着一个`struct sdio_func`结构体,通过该结构体可以访问SDIO设备的寄存器、执行SDIO命令、发送和接收数据等。 `struct sdio_func`结构体定义如下: ``` struct sdio_func { u32 num; // SDIO设备的编号 struct mmc_card *card; // 对应的MMC卡 struct mmc_host *host; // 所在的MMC主机 unsigned int max_blksize; // 最大块大小 unsigned int cur_blksize; // 当前块大小 unsigned int enable_timeout_ms; // 使能超时时间 unsigned int io_abort; // 是否中断IO操作 void *class_dev; // 对应的设备结构体 struct sdio_func_tuple *tuples; // SDIO Tuple列表 unsigned int num_info; // Tuple的数量 unsigned int state; // SDIO设备状态 unsigned int irq_handler; // 中断处理函数 void *irq_data; // 中断处理函数的参数 unsigned int card_busy; // 卡忙标志 unsigned int ocr; // OCR寄存器的值 unsigned int cccr; // CCCR寄存器的值 unsigned int cisd; // CISD寄存器的值 unsigned int sdio_irqs; // SDIO中断标志 unsigned int num_funcs; // SDIO设备的函数数 struct sdio_func_tuple *common_cis; // 公共CIS struct sdio_func_tuple *cis; // CIS struct sdio_func_tuple *cccr_func; // CCCR函数 struct sdio_func_tuple *tuples_buf; // Tuple缓存 unsigned int max_busy_timeout; // 最大卡忙超时时间 unsigned int max_timeout; // 最大超时时间 unsigned int max_seg_size; // 最大段大小 unsigned int multi_block; // 是否支持多块传输 unsigned int no_preblock; // 不需要块预处理 unsigned int sdio_ver; // SDIO版本 unsigned int sd_ver; // SD版本 unsigned int sdio_funcs[8]; // SDIO设备的函数信息 struct device dev; // 对应的设备结构体 }; ``` `struct sdio_func`结构体中包含了SDIO设备的编号、对应的MMC卡、所在的MMC主机、最大块大小、当前块大小、使能超时时间、中断处理函数、设备结构体等信息。在使用`struct sdio_func`结构体时,可以根据SDIO设备的编号、MMC卡和MMC主机等信息来获取相应的`struct sdio_func`结构体,然后通过该结构体访问SDIO设备的寄存器、执行SDIO命令、发送和接收数据等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值