有什么办法从Linux用户空间向SD卡发送命令?

时间:2014-04-26 15:28:42

标签: c linux kernel sd-card

我有一个Debian 7.0 Linux 3.2嵌入式ARM TI AM335x系统。这是我们开发的定制板,但SD卡部分至少与开发板相同。有一些供应商特定的SD卡命令我想发卡,即使用CMD56读取一些SMART数据。

有没有办法向SD卡控制器发送命令并从用户空间读取响应?

3 个答案:

答案 0 :(得分:10)

根据omap_hsmmc http://processors.wiki.ti.com/index.php/AM335x_MMC/SD_Driver%27s_Guide

中的一些信息,您的驱动程序为https://www.kernel.org/doc/Documentation/devicetree/bindings/mmc/ti-omap-hsmmc.txt

在某些 web 搜索SD卡中的SMART监控支持后,我收到了搜索查询mmc smartctl(因为smartctl是Linux中* ATA的SMART监控实用程序的名称,mmc是实现MMC,SD,SDHC卡和控制器的内核子系统。我发现Gwendal Grignou的某些移动PC OS https://code.google.com/p/chromium/issues/detail?id=315380填充了错误

  

如果根设备是SATA设备:

     
      
  • 添加hdparm -I / dev / sda
  • 的输出   
  • 添加smartctl -a / dev / sda
  • 的输出   
     

如果根设备是eMMC设备:

     
      
  • 当mmc-utils成为图像的一部分时,添加类似的命令输出。
  •   

听起来mmc-utils它是实施SMART SD卡的首选工具。 kernel.org上有mmc-utils的主页git:http://git.kernel.org/cgit/linux/kernel/git/cjb/mmc-utils.git/tree/

我看不到" SMART"在这里,mmc-utils/mmc_cmds.c有代码通过使用ioctl(fd, MMC_IOC_CMD, (struct mmc_ioc_cmd*) &ioctl_data)将fd指向正确的mmcblkX设备发送到卡上的自定义命令(我希望这适用于大多数SD控制器)。代码:Johan RUDHOLM(来自st-ericsson,2012,GPLv2):

   int read_extcsd(int fd, __u8 *ext_csd)
   {
       struct mmc_ioc_cmd idata;
       memset(&idata, 0, sizeof(idata));
       memset(ext_csd, 0, sizeof(__u8) * 512);
       idata.write_flag = 0;
       idata.opcode = MMC_SEND_EXT_CSD;
       idata.arg = 0;
       idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
       idata.blksz = 512;
       idata.blocks = 1;
       mmc_ioc_cmd_set_data(idata, ext_csd);

       return  ioctl(fd, MMC_IOC_CMD, &idata);
   }

   int write_extcsd_value(int fd, __u8 index, __u8 value)
   {
       struct mmc_ioc_cmd idata;

       memset(&idata, 0, sizeof(idata));
       idata.write_flag = 1;
       idata.opcode = MMC_SWITCH;
       idata.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
            (index << 16) |
            (value << 8) |
            EXT_CSD_CMD_SET_NORMAL;
       idata.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;

       return ioctl(fd, MMC_IOC_CMD, &idata);
   }

MMC_IOC_CMD的一些文档和示例由Shashidhar Hiremath于2011年12月20日14:54发布在LKML中"[PATCH 1/1] mmc: User Application for testing SD/MMC Commands and extra IOCTL Command for MMC card reset"

struct mmc_ioc_cmd的官方用户API(uapi)位于linux源代码树include/uapi/linux/mmc/ioctl.h中:

  6 struct mmc_ioc_cmd {
...
 10         /* Application-specific command.  true = precede with CMD55 */
 11         int is_acmd;
...
 51  * Since this ioctl is only meant to enhance (and not replace) normal access
 52  * to the mmc bus device...

答案 1 :(得分:-1)

最简单的方法是mmap SD控制器的存储空间,然后将sd控制器数据表的命令写入控制器。

答案 2 :(得分:-1)

是的,有供应商特定的SD卡命令,您可以使用CMD56从SD卡读取SMART数据到您的设备。

主机必须能够支持供应商命令“CMD61”和“CMD56”。并且在问题CMD61和CMD56之间的FW繁忙时间需要5ms延迟。

保罗。