Chinaunix首页 | 论坛 | 博客
  • 博客访问: 921010
  • 博文数量: 96
  • 博客积分: 10071
  • 博客等级: 上将
  • 技术积分: 1118
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-20 17:54
文章分类

全部博文(96)

文章存档

2011年(3)

2010年(3)

2009年(29)

2008年(54)

2007年(7)

分类: WINDOWS

2009-04-10 18:00:04

SD_SDIO_SPEC.PDF

SDIO features

l         Targeted for portable and stationary applications

l         Minimal or no modification to SD Physical bus is required

l         Minimal change to memory driver software

l         Extended physical form factor available for specialized applications

l         Plug and play (PnP) support

l         Multi-function support including multiple I/O and combined I/O and memory

l         Up to 7 I/O functions plus one memory supported on one card.

l         Allows card to interrupt host

l         Initialization Voltage: 2.0 to 3.6V

l         Operational Voltage range: 3.1 to 3.5V

 

 

  

 

SD Stack

       一共可以分为3个部分:a bus driver, host controller driver  client drivers,现分别介绍:

1 Bus Driver

       顾名思义,总线,连接client driverhost controller driver之间的一个管理层。这部分代码微软已经写好了,也就是说定义好了clienthost之间的通讯接口。(是不是可以这么说:编写SD WIFI的程序员可以不用知道是什么SDHC,从而达到驱动的跨硬件性)

参考代码:

D:\WINCE500\PUBLIC\COMMON\OAK\DRIVERS\SDCARD\SDBUSDRIVER\

 

2 Host Controller

       控制硬件并且通过上述的BUS driver来和client进行通讯。

参考代码:

D:\WINCE500\PLATFORM\Mx31\Src\Drivers\Sdhc

D:\WINCE500\PUBLIC\COMMON\OAK\CSP\ARM\FREESCALE\Mxarm11\Drivers\Sdhc

 

3 Client Driver

       通过BUS driverSD设备进行通讯。

 

Windows CE 5.0中的SDIO支持如下:

l         动态插入和拔出

l         DMA方式 (平台相关)

l         SDIO 中断

l         动态的时钟控制

l         错误恢复

l         唤醒

l         v1.0 多功能和组合设备

l         CE Power Manager来处理电源管理

l         MMC

 

下图可以清晰的表达基于WINCE5.0的一个SD STACK模型:

 

安全性

SDA 中的SD Memory specification 有种机制可以在特定机器上锁定卡里面的内容。WINCE 5里并没有显示的支持该机制。但是可以自己来做,比如一个文件系统的FILTER等。

 

主要功能如下:

l         枚举板上的卡,并决定他们的类型(MMC, SD Memory or SDIO

l         配置合适的电流给卡。

l         根据注册表的值加载clients

l         把总线要求入队列

l         把来自host controller的异步通知入队列

l         总线要求完成,SDIO 中断,设备插入\拔出

l         出错时重试

 

参考代码:

D:\WINCE500\PUBLIC\COMMON\OAK\DRIVERS\SDCARD\SDBUSDRIVER

貌似此部分代码非微软原创:

// Copyright (c) 2002 BSQUARE Corporation.  All rights reserved.

// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE

 

主要功能:

l         HCD通知总线驱动卡的插入和拔出

l         给卡上电,SD定义了可接受的初时电压范围。

l         在总线驱动何客户端设置完时钟速度后,打开客户端卡的时钟(80个周期)

l         初始,把总线宽度设置成1。如果有需要的话(4-bit mode),把总线宽度设成4bit

l         传输SD 命令和数据到/来自卡上。

l         负责给槽上电,关电。

l         IST 在这里存在

l         可选的)唤醒支持(插入,拔出,SDIO中断)

 

初始化

1.         调用HCD’s XXX_Init

2.         调用 SDHCDAllocateContext() 来分配一段HC的上下文

a)         Context 是总线驱动和HCD共享的

3.         HCD 使用SDHCDSetXxx宏来填充这个上下文结构

a)         这个步骤是把HC向总线驱动描述一下

b)        包括函数指针,支持的电流,最大时钟,槽数目,SDIO的支持等等。

4.         调用 SDHCDRegisterHostController() 来把自己向总线驱动注册一下

5.         当总线驱动准备处理SD事件时,它会调用 HCDinit 函数(pContext->pInitHandler(见SDHCDRegisterHostController__X函数)

6.         在初始化里,HCD还应该完成硬件和资源的初始化(IST等)

 

下面代码抄自MX31 BSPSDHC驱动的SDH_Init函数中:

DWORD SDH_Init(DWORD dwContext)

{

// allocate the context

 status = SDHCDAllocateContext(SDH_SLOTS,  &pHostContext);

。。。。。。

    //Set a unique name for each host controller

    if (pController->ControllerIndex == 1)

    {

        SDHCDSetHCName(pHostContext, TEXT("MXARM11_1"));

    }

    else

    {

        SDHCDSetHCName(pHostContext, TEXT("MXARM11_2"));

    }

// set init handler

    SDHCDSetControllerInitHandler(pHostContext,SDInitialize); 

    // set deinit handler   

    SDHCDSetControllerDeinitHandler(pHostContext, SDDeinitialize);

    // set the bus request handler

    SDHCDSetBusRequestHandler(pHostContext,SDHBusRequestHandler);  

    // set the cancel I/O handler

    SDHCDSetCancelIOHandler(pHostContext, SDHCancelIoHandler);  

    // set the slot option handler

    SDHCDSetSlotOptionHandler(pHostContext, SDHSlotOptionHandler);

    // now register the host controller

    status = SDHCDRegisterHostController(pHostContext);

 

Slot Option Handler

l         总线驱动调用HCD SlotOptionHandler 

n         SDHCDSetSlotPower –设置电压

n         SDHCDSetSlotInterface – 设置时钟和总线宽度

n         SDHCDEnableSDIOInterrupts

n         SDHCDAckSDIOInterrupt

n         SDHCDDisableSDIOInterrupts

l         SDHCDGetWriteProtectStatus – HCD 必须查看SD存储卡是否开启写保护

l         SDHCDQueryBlockCapability – 返回HCD的最大和最小块长度

 

总线要求

l         总线驱动把总线要求放入队列然后把它们传入HCD BusRequestHandler 函数

l         HCD根据要求来做相应的动作—command/read/write, multi-block, .

l         HCD 使用某种方式(DMA, PIO, busy-waiting等)来发送命令和数据

l         HCD 调用SDHCDIndicateBusRequestComplete() 来通知总线驱动完成

l         总线驱动把完成事件入队列并提交下一个要求给HCD

l         总线驱动的调度线程将会通知产生要求的起始源事件完成了。

 

标准的WINCE SDHC驱动支持

l         Standard Host Controller v1.0

n         Tokyo Electron Devices – Ellen

n         Toshiba - Pegasus

n         TI PCI 7x21

n         Ricoh R5C811 and R5C841

l         Non-standard hosts

n         Intel PXA-27x SOC (Mainstone II)

n         Samsung SMDK-2410

n         Freescale MX series

关于Standard Host Controller

SDA Host Working Group (MSFT executive member)

Defined Standard Host Register Specification to standardize the hardware interface from bus to controller

Currently ratified to v1.0 by SDA executive committee

MSFT strongly advocating this standard to all IHVs, ODMs, OEMs and Silicons

 

中断

l         HCD包括IST

l         IST 决定哪个槽中断

l         IST evaluates interrupt

比如现在拔出SD

IST 会调用Bus Driver’s SDHCIndicateSlotStateChange() with DeviceEjected parameter

l         Bus Driver calls client’s SlotEventCallBack routine with SDCardEjected.  (Provides async notification about changes in the slot state.)

l         Client performs its deinitialization

 

如何让镜像支持SD STACK

SYSGEN_SDBUS – SD bus driver (sdbus.dll)

SYSGEN_SDHC_STANDARD – Standard host controller (sdhc.dll)

SYSGEN_SD_MEMORY – SD Memory client (sdmemory.dll)

SYSGEN_BTH or SYSGEN_BTH_SDIO_ONLY – SDIO Bluetooth client (bthsdio.dll)

BSP variables will be added for CSP-specific host controllers

 

目前,WINCE5.0所支持的client driver如下:

l         SD Memory Class

l         SDIO Bluetooth Class - Type A

l         SDIO WiFi (vendor specific)

如果需要自己写client驱动的话可以参考D:\WINCE500\PUBLIC\COMMON\OAK\DRIVERS\SDCARD\SDCLIENTDRIVERS目录下的微软自带的SD client驱动来写。

 

example1: SD MEMORY DRIVER UNDER WINCE 5.0

Wince自带的SD存储卡client驱动,代码路径为:

D:\WINCE500\PUBLIC\COMMON\OAK\DRIVERS\SDCARD\SDCLIENTDRIVERS\SDMEMORY

 

一个SD存储卡在WINCE里的驱动架构如下图所示:
[图无法插入]

 

1注册表和启动

在注册表里告诉Storage Manager

[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\SDMemory]

    "Name"="SD Memory Card"

    "Folder"="Storage Card"

    ;"PartitionDriver"=""          ; removable storage cannot have partitions

 

[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\MMC]

    "Name"="MMC Card"

    "Folder"="Storage Card"

    ;"PartitionDriver"=""          ; removable storage cannot have partitions

 

; SD Memory Storage class driver

[HKEY_LOCAL_MACHINE\Drivers\SDCARD\ClientDrivers\Class\SDMemory_Class]

   "Dll"="SDMemory.dll"

   "Prefix"="DSK"

   "BlockTransferSize"=dword:40  ; send no more than 64 blocks of data per bus transfer

   ;"SingleBlockWrites"=dword:1  ; alternatively force the driver to use single block access

   ;"IdleTimeout"=dword:7D0      ; 2000 milliseconds

   ;"IdlePowerState"=dword:2     ; 0 == D0, 1 == D1, etc.

   ;"DisablePowerManagement"=""  ; if value present, then disable (remove value to enable)

 

   "Profile"="SDMemory"

   "IClass"=multi_sz:"{A4E7EDDA-E575-4252-9D6B-4195D48BB865}",

                     "{A32942B7-920C-486b-B0E6-92A702A99B35}"

 

; MMC Storage Class Driver

[HKEY_LOCAL_MACHINE\Drivers\SDCARD\ClientDrivers\Class\MMC_Class]

   "Dll"="SDMemory.dll"

   "Prefix"="DSK"

   "BlockTransferSize"=dword:40  ; send no more than 64 blocks of data per bus transfer

   ;"SingleBlockWrites"=dword:1  ; alternatively force the driver to use single block access

   ;"IdleTimeout"=dword:7D0      ; milliseconds

   ;"IdlePowerState"=dword:2     ; 0 == D0, 1 == D1, etc.

   ;"DisablePowerManagement"=""  ; if value present, then disable (remove value to enable)

 

   "Profile"="MMC"

   "IClass"=multi_sz:"{A4E7EDDA-E575-4252-9D6B-4195D48BB865}",

                     "{A32942B7-920C-486b-B0E6-92A702A99B35}"

 

2 SD命令和总线要求

将所有磁盘的操作转换成SD的命令和总线要求

NAME SDMEMORY.DLL

 

EXPORTS

DSK_Close=SMC_Close

DSK_Deinit=SMC_Deinit

DSK_Init=SMC_Init

DSK_IOControl=SMC_IOControl

DSK_Open=SMC_Open

DSK_PowerDown=SMC_PowerDown

DSK_PowerUp=SMC_PowerUp

DSK_Read=SMC_Read

DSK_Seek=SMC_Seek

DSK_Write=SMC_Write

下面为具体的DISK I/O操作函数和CARD I/O操作函数,详细实现见sdmemdiskio.cppsdmemcardio.cpp

//

// SDDiskIO

//

//  SDMemCardConfig    - Initialise the memcard structure and card itself

DWORD SDMemCardConfig( PSD_MEMCARD_INFO pMemCard );

 

    //  SDMemRead          - Read data from card into pSG scatter gather buffers

DWORD SDMemRead( PSD_MEMCARD_INFO pMemCard, PSG_REQ pSG );

 

    //  SDMemWrite         - Write data to card from pSG scatter gather buffers

DWORD SDMemWrite( PSD_MEMCARD_INFO pMemCard, PSG_REQ pSG );

 

    //  SDMemErase         - Erase a contiguous set of blocks

DWORD SDMemErase( PSD_MEMCARD_INFO pMemCard, PDELETE_SECTOR_INFO pDSI );

 

    //  SDMemEraseAll      - Erase all blocks

DWORD SDMemEraseAll( PSD_MEMCARD_INFO pMemCard );

 

//

// SDCardIO

//

 

    //  SDMemDoBusRequest  - Perform a bus request, returns Windows Status

DWORD SDMemDoBusRequest( PSD_MEMCARD_INFO  pMemcard,

                         UCHAR             Command,

                         DWORD             Argument,

                         SD_TRANSFER_CLASS TransferClass,

                         SD_RESPONSE_TYPE  ResponseType,

                         ULONG             NumBlocks,

                         ULONG             BlockSize,

                         PUCHAR            pBuffer,

                         DWORD             Flags);

 

    //  SDMemSetBlockLen   - Sets read/write block length for SD memory card

DWORD SDMemSetBlockLen( PSD_MEMCARD_INFO pMemcard,

                        DWORD            BlockLen );

 

    //  SDMemReadMultiple  - Read multiple 512 byte blocks of data from card

DWORD SDMemReadMultiple( PSD_MEMCARD_INFO pHandle,

                         ULONG            StartBlock,

                         ULONG            NumBlocks,

                         PUCHAR           pBuffer );

 

    //  SDMemWriteMultiple - Write multiple 512 byte blocks of data to card

DWORD SDMemWriteMultiple( PSD_MEMCARD_INFO pHandle,

                          LONG             StartBlock,

                          LONG             NumBlocks,

                          PUCHAR           pBuffer );

 

    //  SDMemWriteUsingSingleBlocks - Write using single block writes

DWORD SDMemWriteUsingSingleBlocks( PSD_MEMCARD_INFO pHandle,

                                   LONG             StartBlock,

                                   LONG             NumBlocks,

                                   PUCHAR           pBuffer );

 

    //  SDMemDoErase - Erase a contiguous set of blocks

DWORD SDMemDoErase( PSD_MEMCARD_INFO pHandle,

                    LONG             StartBlock,

                    LONG             NumBlocks );

 

DWORD SDAPIStatusToErrorCode( SD_API_STATUS Status );

 

DWORD SDGetCardStatus(PSD_MEMCARD_INFO pMemCard , SD_CARD_STATUS *pCardStatus);

 

VOID HandleIoctlPowerSet(PSD_MEMCARD_INFO       pMemCard,

                         PCEDEVICE_POWER_STATE  pDevicePowerState);

VOID InitializePowerManagement(PSD_MEMCARD_INFO pMemCard);

VOID DeinitializePowerManagement(PSD_MEMCARD_INFO pMemCard);

SD_API_STATUS IssueCardSelectDeSelect(PSD_MEMCARD_INFO pMemCard, BOOL Select);

VOID RequestEnd(PSD_MEMCARD_INFO pMemCard);

 

SD_API_STATUS RequestPrologue(PSD_MEMCARD_INFO pMemCard, DWORD DeviceIoControl);

 

 

 

Secure Digital Host Controller

The Secure Digital Host Controller (SDHC) 模块支持MMCSDSecure Digital I/O and Combo Cards (SDIO)三种。MX31一共有2SDHC硬件模块。一个host controller 只支持连接上的一个卡。

SDHC.DLL由以下源代码组成

『――――

微软代码

    c:\macallan\private\winceos\coreos\ceosutil\utiltree.cxx

    c:\macallan\public\common\sdk\inc\svsutil.hxx

    c:\macallan\private\winceos\coreos\ceosutil\utilmemf.cxx

    c:\macallan\public\common\sdk\inc\svsutil.hxx

    c:\macallan\private\winceos\coreos\ceosutil\utilmem.cxx

    c:\macallan\private\winceos\coreos\ceosutil\svsutil.cxx

    c:\macallan\public\common\oak\drivers\sdcard\sdcardlib\sdcardapistubs.cpp

    c:\macallan\public\common\oak\drivers\sdcard\sdcardlib\sddebug.cpp

    c:\macallan\public\common\oak\drivers\sdcard\sdcardlib\sdmemapi.cpp

    c:\macallan\public\common\oak\inc\block_allocator.hxx

c:\macallan\public\common\oak\drivers\sdcard\sdhclib\sdhclib.cpp

―――――』

d:\wince500\platform\mx31\src\drivers\sdhc\bspsdhc.c

    d:\wince500\public\common\oak\csp\arm\freescale\mxarm11\drivers\sdhc\main.cpp

    d:\wince500\public\common\oak\csp\arm\freescale\mxarm11\drivers\sdhc\sdcontrol.cpp

d:\wince500\public\common\sdk\inc\kfuncs.h

 

 

SDHC的注册表设置

#if (defined BSP_SDHC1 || defined BSP_SDHC2)

[HKEY_LOCAL_MACHINE\Drivers\SDCARD\ClientDrivers\Class\SDMemory_Class]

"BlockTransferSize"=dword:100 ; Overwrite from default 64 blocks.

; "SingleBlockWrites"=dword:1 ; alternatively force the driver to use single block

access

[HKEY_LOCAL_MACHINE\Drivers\SDCARD\ClientDrivers\Class\MMC_Class]

"BlockTransferSize"=dword:100 ; Overwrite from default 64 blocks.

; "SingleBlockWrites"=dword:1 ; alternatively force the driver to use single block

access

[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\MMC]

"Name"="MMC Card"

"Folder"="MMC"

[HKEY_LOCAL_MACHINE\System\StorageManager\Profiles\SDMemory]

"Name"="SD Memory Card"

"Folder"="SD Memory"

#endif

IF BSP_SDHC1

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SDHC_ARM11_1]

"Order"=dword:21

"Dll"="sdhc.dll"

"Prefix"="SDH"

"ControllerISTPriority"=dword:64

"Index"=dword:1

ENDIF ;BSP_SDHC1

IF BSP_SDHC2

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\SDHC_ARM11_2]

"Order"=dword:21

"Dll"="sdhc.dll"

"Prefix"="SDH"

"ControllerISTPriority"=dword:64

"Index"=dword:2

ENDIF ;BSP_SDHC

 

SDHC和DMA

SDHC驱动支持DMA 和非DMA 2种数据传输模式,默认是DMAFor every request submitted to it, the driver attempts to build a DMA Scatter Gather Buffer Descriptor list for the buffer passed to it by the upper layer. For cases where this list cannot be built, the driver falls back to the non-DMA mode of transfer. The default configuration is maintained in the file bsp_cfg.h using the parameters BSP_SDMA_SUPPORT_SDHC1 and BSP_SDMA_SUPPORT_SDHC2. A value of TRUE means DMA is the default mode, and for cases where DMA cannot be used, the driver falls back to a non-DMA mode. A value of FALSE means non-DMA mode is the default and DMA mode will not be attempted. For the driver to attempt to build the Scatter Gather DMA Buffer Descriptors, the upper layer should ensure that the buffer meets the following criteria.

 Start of the buffer should be a word aligned address.

 Number of bytes to transfer should be word aligned.

Due to cache coherency issues arising due to processor and SDMA access of the memory, the above criteria is further stringent for the read or receive operation (it is not applicable for write or transmit):

 Start of the buffer should be a cache line size (32 bytes) aligned address.

 Number of bytes to transfer should be cache line size (32 bytes) aligned.

 

电源管理

The primary methods for limiting power in SDHC module is to gate off all clocks to the controllers and to

cut off power to the card slot when no cards are inserted. When a card is inserted to any of the slots, that

slot alone is powered and the clocks to that controller alone are gated on. While using memory cards, the

clock to the host controller and the clock to memory cards are gated off when ever the controller is idle.

For SDIO cards, both the clocks stay on all the time.

SDHC driver supports the full power on and full power off states. In full power off state, the clocks to the

controllers and the power to the inserted cards are turned off. When powered on, all cards inserted before

and after the power down will be detected and mounted.

PowerUp

This function is implemented to support resuming a memory card operation that was previously terminated

by calling PowerDown() API. Power to the card is restored, clocks to the pertaining controller is restarted.

SDHC driver is notified of a device status change. This results in signaling the SD bus driver of a card

removal followed by a card insertion. The card is re-initialized and is mounted so that the all operations

scheduled during a power down resumes. SDIO cards will be initialized on resume.

The details of this architecture and its operation can be found in the Platform Builder Help under the

heading “Power On and Off Notifications for Secure Digital Card Drivers”, or in the online Microsoft

documentation at the following URL:

ificationsforsecuredigitalcarddrivers.asp

Note that this function is intended to be called only by the Power Manager.

PowerDown

This function has been implemented to support suspending all currently active SD operations just before

the entire system enters the low power state. Note that this function is intended to be called only by the

Power Manager. This function gates off all clocks to the controllers and powers down all the card slots.

 

阅读(4392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~