调一款加密ic ET300时,发现通信不上,报错-110,就是超时出错,检查了gpio和sdcard配置,也检查了sd供电,发现sdio的供电域是1.8v,而根据协议初始化时默认是3.3v,然后切换到1.8v,通过飞线,使用一个ldo来供电。修改后,还是通信异常。
加打印调试,在mmc_send_cid函数报错了,就是读不到卡的cid信息。默认读不到cid,代码就退出了。 检查了再检查,实在找不出问题,于是修改代码,强制往下跑,看log,居然读到了卡的信息。
也就是只有CMD2命令跑不过,其他命令都跑过了。
发送CMD2命令前会先切换到1.8v的电压,对应平台的sdio驱动
也就是sdio host的名字是sdio_sd才会复位下平台的sdio host,而dts配置名字不是sdio_sd,导致没有复位平台的host,修改名字后,代码能正常跑过。
调是调完了,顺便把相关的代码逻辑整理下。
从mmc_rescan这个函数讲起,相关内容已在Linux下的eMMC_Android开发-优快云博客描述过。
//以不同频率去扫描卡,但实际上某些平台定义的最小频率是400k(只会以400k去扫)
void mmc_rescan(struct work_struct *work)
{
...
for (i = 0; i < ARRAY_SIZE(freqs); i++) {
if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
break;
if (freqs[i] <= host->f_min)
break;
}
...
}
static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
{
mmc_power_up(host, host->ocr_avail);
if (!(host->caps2 & MMC_CAP2_NO_SDIO))
sdio_reset(host);
mmc_go_idle(host);
mmc_attach_sd(host);
mmc_power_off(host);
return -EIO;
}
int mmc_attach_sd(struct mmc_host *host)
{
...
err = mmc_send_app_op_cond(host, 0, &ocr);
mmc_attach_bus(host, &mmc_sd_ops);
if (host->ocr_avail_sd)
host->ocr_avail = host->ocr_avail_sd;
rocr = mmc_select_voltage(host, ocr);
err = mmc_sd_init_card(host, rocr, NULL);
mmc_release_host(host);
err = mmc_add_card(host->card);
mmc_claim_host(host);
...
}
比较关键了几个函数mmc_send_app_op_cond,mmc_sd_init_card仔细看的话,实际上是sd协议的代码实现。如果扫描到了sd卡,会在/sys/bus/mmc/devices
生成相应的设备节点。
说到sd协议,比较权威的资料就是官网文档了,下载地址Simplified Specifications | SD Association,下这一篇(Physical Layer Simplified Specification)速度慢点,还是能下载的(自己也上传了一份Part1_Physical_Layer_Simplified_Specification_Ver8.00.pdf-Android文档类资源-优快云下载)。
比较关键的一张图,然后按照代码慢慢看吧。
补上sdcard热插拔部分
dts配置检测脚,如
&sdio0 {
cd-gpios = <&eic_sync 19 GPIO_ACTIVE_LOW>;
};
int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,unsigned int idx, bool override_active_level,unsigned int debounce, bool *gpio_invert)
{
desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
ctx->cd_gpio = desc;
}
void mmc_gpiod_request_cd_irq(struct mmc_host *host)
{
irq = gpiod_to_irq(ctx->cd_gpio);
ret = devm_request_threaded_irq(host->parent, irq,NULL, mmc_gpio_cd_irqt,IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,ctx->cd_label, host);
}
int mmc_of_parse(struct mmc_host *host){
{
mmc_gpiod_request_cd(host, "cd", 0, true,0, &cd_gpio_invert);//获取热插拔的gpio
}
int mmc_add_host(struct mmc_host *host)
{
mmc_start_host-->mmc_gpiod_request_cd_irq/申请中断
}
static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
{
/* Schedule a card detection after a debounce timeout */
struct mmc_host *host = dev_id;
host->trigger_card_event = true;
mmc_detect_change(host, msecs_to_jiffies(200));-->mmc_schedule_delayed_work(&host->detect, delay);
return IRQ_HANDLED;
}
INIT_DELAYED_WORK(&host->detect, mmc_rescan);//进行重新扫描