sd.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All rights reserved.
  3. Module Name:
  4. sd.h
  5. Abstract:
  6. This header contains definitions for the SD/MMC driver library.
  7. Author:
  8. Evan Green 27-Feb-2014
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. #include <minoca/sd/sdstd.h>
  14. //
  15. // --------------------------------------------------------------------- Macros
  16. //
  17. //
  18. // This macro determines if the given card is an SD card. It returns non-zero
  19. // if it is an SD card, or 0 if it is an MMC card.
  20. //
  21. #define SD_IS_CARD_SD(_Controller) \
  22. ((_Controller)->Version < SdVersionMaximum)
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. //
  27. // Define the API decorator.
  28. //
  29. #ifndef SD_API
  30. #define SD_API __DLLIMPORT
  31. #endif
  32. #define SD_ALLOCATION_TAG 0x636D6453 // 'cMdS'
  33. //
  34. // Define the device ID for an SD bus slot.
  35. //
  36. #define SD_SLOT_DEVICE_ID "SdSlot"
  37. //
  38. // Define the device ID for an SD Card.
  39. //
  40. #define SD_CARD_DEVICE_ID "SdCard"
  41. #define SD_MMC_DEVICE_ID "MmcDisk"
  42. //
  43. // Define software-only capability flags (ie these bits don't show up in the
  44. // hardware).
  45. //
  46. #define SD_MODE_HIGH_SPEED 0x0001
  47. #define SD_MODE_HIGH_SPEED_52MHZ 0x0002
  48. #define SD_MODE_4BIT 0x0004
  49. #define SD_MODE_8BIT 0x0008
  50. #define SD_MODE_SPI 0x0010
  51. #define SD_MODE_HIGH_CAPACITY 0x0020
  52. #define SD_MODE_AUTO_CMD12 0x0040
  53. #define SD_MODE_ADMA2 0x0080
  54. #define SD_MODE_RESPONSE136_SHIFTED 0x0100
  55. #define SD_MODE_SDMA 0x0200
  56. #define SD_MODE_SYSTEM_DMA 0x0400
  57. #define SD_MODE_CMD23 0x0800
  58. //
  59. // Define the software only reset flags.
  60. //
  61. #define SD_RESET_FLAG_ALL 0x00000001
  62. #define SD_RESET_FLAG_COMMAND_LINE 0x00000002
  63. #define SD_RESET_FLAG_DATA_LINE 0x00000004
  64. //
  65. // Define the bitmask of SD controller flags.
  66. //
  67. #define SD_CONTROLLER_FLAG_HIGH_CAPACITY 0x00000001
  68. #define SD_CONTROLLER_FLAG_MEDIA_PRESENT 0x00000002
  69. #define SD_CONTROLLER_FLAG_DMA_ENABLED 0x00000004
  70. #define SD_CONTROLLER_FLAG_CRITICAL_MODE 0x00000008
  71. #define SD_CONTROLLER_FLAG_DMA_COMMAND_ENABLED 0x00000010
  72. #define SD_CONTROLLER_FLAG_MEDIA_CHANGED 0x00000020
  73. #define SD_CONTROLLER_FLAG_REMOVAL_PENDING 0x00000040
  74. #define SD_CONTROLLER_FLAG_INSERTION_PENDING 0x00000080
  75. //
  76. // Define the maximum number of times to retry IO.
  77. //
  78. #define SD_MAX_IO_RETRIES 5
  79. //
  80. // ------------------------------------------------------ Data Type Definitions
  81. //
  82. typedef enum _SD_VOLTAGE {
  83. SdVoltage0V = 0,
  84. SdVoltage1V8 = 1800,
  85. SdVoltage3V0 = 3000,
  86. SdVoltage3V3 = 3300,
  87. } SD_VOLTAGE, *PSD_VOLTAGE;
  88. typedef struct _SD_CONTROLLER SD_CONTROLLER, *PSD_CONTROLLER;
  89. /*++
  90. Structure Description:
  91. This structure stores information about an SD card command.
  92. Members:
  93. Command - Stores the command number.
  94. ResponseType - Stores the response class expected from this command.
  95. CommandArgument - Stores the argument to the command.
  96. Response - Stores the response data from the executed command.
  97. BufferSize - Stores the size of the data buffer in bytes.
  98. BufferVirtual - Stores the virtual address of the data buffer.
  99. BufferPhysical - Stores the physical address of the data buffer.
  100. Write - Stores a boolean indicating if this is a data read or write. This
  101. is only used if the buffer size is non-zero.
  102. Dma - Stores a boolean indicating if this is a DMA or non-DMA operation.
  103. --*/
  104. typedef struct _SD_COMMAND {
  105. SD_COMMAND_VALUE Command;
  106. ULONG ResponseType;
  107. ULONG CommandArgument;
  108. ULONG Response[4];
  109. ULONG BufferSize;
  110. PVOID BufferVirtual;
  111. PHYSICAL_ADDRESS BufferPhysical;
  112. BOOL Write;
  113. BOOL Dma;
  114. } SD_COMMAND, *PSD_COMMAND;
  115. typedef
  116. KSTATUS
  117. (*PSD_INITIALIZE_CONTROLLER) (
  118. PSD_CONTROLLER Controller,
  119. PVOID Context,
  120. ULONG Phase
  121. );
  122. /*++
  123. Routine Description:
  124. This routine performs any controller specific initialization steps.
  125. Arguments:
  126. Controller - Supplies a pointer to the controller.
  127. Context - Supplies a context pointer passed to the SD/MMC library upon
  128. creation of the controller.
  129. Phase - Supplies the phase of initialization. Phase 0 happens after the
  130. initial software reset and Phase 1 happens after the bus width has been
  131. set to 1 and the speed to 400KHz.
  132. Return Value:
  133. Status code.
  134. --*/
  135. typedef
  136. KSTATUS
  137. (*PSD_RESET_CONTROLLER) (
  138. PSD_CONTROLLER Controller,
  139. PVOID Context,
  140. ULONG Flags
  141. );
  142. /*++
  143. Routine Description:
  144. This routine performs a soft reset of the SD controller.
  145. Arguments:
  146. Controller - Supplies a pointer to the controller.
  147. Context - Supplies a context pointer passed to the SD/MMC library upon
  148. creation of the controller.
  149. Flags - Supplies a bitmask of reset flags. See SD_RESET_FLAG_* for
  150. definitions.
  151. Return Value:
  152. Status code.
  153. --*/
  154. typedef
  155. KSTATUS
  156. (*PSD_SEND_COMMAND) (
  157. PSD_CONTROLLER Controller,
  158. PVOID Context,
  159. PSD_COMMAND Command
  160. );
  161. /*++
  162. Routine Description:
  163. This routine sends the given command to the card.
  164. Arguments:
  165. Controller - Supplies a pointer to the controller.
  166. Context - Supplies a context pointer passed to the SD/MMC library upon
  167. creation of the controller.
  168. Command - Supplies a pointer to the command parameters.
  169. Return Value:
  170. Status code.
  171. --*/
  172. typedef
  173. KSTATUS
  174. (*PSD_GET_SET_BUS_WIDTH) (
  175. PSD_CONTROLLER Controller,
  176. PVOID Context,
  177. BOOL Set
  178. );
  179. /*++
  180. Routine Description:
  181. This routine gets or sets the controller's bus width. The bus width is
  182. stored in the controller structure.
  183. Arguments:
  184. Controller - Supplies a pointer to the controller.
  185. Context - Supplies a context pointer passed to the SD/MMC library upon
  186. creation of the controller.
  187. Set - Supplies a boolean indicating whether the bus width should be queried
  188. or set.
  189. Return Value:
  190. Status code.
  191. --*/
  192. typedef
  193. KSTATUS
  194. (*PSD_GET_SET_CLOCK_SPEED) (
  195. PSD_CONTROLLER Controller,
  196. PVOID Context,
  197. BOOL Set
  198. );
  199. /*++
  200. Routine Description:
  201. This routine gets or sets the controller's clock speed. The clock speed is
  202. stored in the controller structure.
  203. Arguments:
  204. Controller - Supplies a pointer to the controller.
  205. Context - Supplies a context pointer passed to the SD/MMC library upon
  206. creation of the controller.
  207. Set - Supplies a boolean indicating whether the clock speed should be
  208. queried (FALSE) or set (TRUE).
  209. Return Value:
  210. Status code.
  211. --*/
  212. typedef
  213. KSTATUS
  214. (*PSD_GET_SET_VOLTAGE) (
  215. PSD_CONTROLLER Controller,
  216. PVOID Context,
  217. BOOL Set
  218. );
  219. /*++
  220. Routine Description:
  221. This routine gets or sets the current bus voltage. The current voltage is
  222. stored in the controller structure.
  223. Arguments:
  224. Controller - Supplies a pointer to the controller.
  225. Context - Supplies a context pointer passed to the SD/MMC library upon
  226. creation of the controller.
  227. Set - Supplies a boolean indicating whether the bus voltage should be
  228. queried (FALSE) or set (TRUE).
  229. Return Value:
  230. Status code.
  231. --*/
  232. typedef
  233. VOID
  234. (*PSD_STOP_DATA_TRANSFER) (
  235. PSD_CONTROLLER Controller,
  236. PVOID Context
  237. );
  238. /*++
  239. Routine Description:
  240. This routine stops any current data transfer on the controller.
  241. Arguments:
  242. Controller - Supplies a pointer to the controller.
  243. Context - Supplies a context pointer passed to the SD/MMC library upon
  244. creation of the controller.
  245. Return Value:
  246. None.
  247. --*/
  248. typedef
  249. KSTATUS
  250. (*PSD_GET_CARD_DETECT_STATUS) (
  251. PSD_CONTROLLER Controller,
  252. PVOID Context,
  253. PBOOL CardPresent
  254. );
  255. /*++
  256. Routine Description:
  257. This routine determines if there is currently a card in the given SD/MMC
  258. controller.
  259. Arguments:
  260. Controller - Supplies a pointer to the controller.
  261. Context - Supplies a context pointer passed to the SD/MMC library upon
  262. creation of the controller.
  263. CardPresent - Supplies a pointer where a boolean will be returned
  264. indicating if a card is present or not.
  265. Return Value:
  266. Status code.
  267. --*/
  268. typedef
  269. KSTATUS
  270. (*PSD_GET_WRITE_PROTECT_STATUS) (
  271. PSD_CONTROLLER Controller,
  272. PVOID Context,
  273. PBOOL WriteProtect
  274. );
  275. /*++
  276. Routine Description:
  277. This routine determines the state of the write protect switch on the
  278. SD/MMC card.
  279. Arguments:
  280. Controller - Supplies a pointer to the controller.
  281. Context - Supplies a context pointer passed to the SD/MMC library upon
  282. creation of the controller.
  283. WriteProtect - Supplies a pointer where a boolean will be returned
  284. indicating if writes are disallowed (TRUE) or if writing is allowed
  285. (FALSE).
  286. Return Value:
  287. Status code.
  288. --*/
  289. typedef
  290. VOID
  291. (*PSD_MEDIA_CHANGE_CALLBACK) (
  292. PSD_CONTROLLER Controller,
  293. PVOID Context,
  294. BOOL Removal,
  295. BOOL Insertion
  296. );
  297. /*++
  298. Routine Description:
  299. This routine is called by the SD library to notify the user of the SD
  300. library that media has been removed, inserted, or both. This routine is
  301. called from a DPC and, as a result, can get called back at dispatch level.
  302. Arguments:
  303. Controller - Supplies a pointer to the controller.
  304. Context - Supplies a context pointer passed to the SD/MMC library upon
  305. creation of the controller.
  306. Removal - Supplies a boolean indicating if a removal event has occurred.
  307. Insertion - Supplies a boolean indicating if an insertion event has
  308. occurred.
  309. Return Value:
  310. None.
  311. --*/
  312. /*++
  313. Structure Description:
  314. This structure defines the set of SD functions that may need to be supplied
  315. to the base SD driver in case the host controller is not standard.
  316. Members:
  317. InitializeController - Store a pointer to a function used to initialize the
  318. controller.
  319. ResetController - Stores a pointer to a function used to reset the
  320. controller.
  321. SendCommand - Stores a pointer to a function used to send commands to the
  322. SD/MMC device.
  323. GetSetBusWidth - Store a pointer to a function used to get or set the
  324. controller's bus width.
  325. GetSetClockSpeed - Stores a pointer to a function used to get or set the
  326. controller's clock speed.
  327. GetSetVoltage - Stores a pointer to a function used to get or set the
  328. bus voltage.
  329. StopDataTransfer - Stores a pointer to a function that stops any active
  330. data transfers before returning.
  331. GetCardDetectStatus - Stores an optional pointer to a function used to
  332. determine if there is a card in the slot.
  333. GetWriteProtectStatus - Stores an optional pointer to a function used to
  334. determine the state of the physical write protect switch on the card.
  335. MediaChangeCallback - Stores an optional pointer to a function called when
  336. media is inserted or removed.
  337. --*/
  338. typedef struct _SD_FUNCTION_TABLE {
  339. PSD_INITIALIZE_CONTROLLER InitializeController;
  340. PSD_RESET_CONTROLLER ResetController;
  341. PSD_SEND_COMMAND SendCommand;
  342. PSD_GET_SET_BUS_WIDTH GetSetBusWidth;
  343. PSD_GET_SET_CLOCK_SPEED GetSetClockSpeed;
  344. PSD_GET_SET_VOLTAGE GetSetVoltage;
  345. PSD_STOP_DATA_TRANSFER StopDataTransfer;
  346. PSD_GET_CARD_DETECT_STATUS GetCardDetectStatus;
  347. PSD_GET_WRITE_PROTECT_STATUS GetWriteProtectStatus;
  348. PSD_MEDIA_CHANGE_CALLBACK MediaChangeCallback;
  349. } SD_FUNCTION_TABLE, *PSD_FUNCTION_TABLE;
  350. /*++
  351. Structure Description:
  352. This structure defines the initialization parameters passed upon creation
  353. of a new SD controller.
  354. Members:
  355. StandardControllerBase - Stores an optional pointer to the base address of
  356. the standard SD host controller registers. If this is not supplied,
  357. then a function table must be supplied.
  358. ConsumerContext - Stores a context pointer passed to the function pointers
  359. contained in this structure.
  360. FunctionTable - Stores a table of functions used to override the standard
  361. SD behavior.
  362. Voltages - Stores a bitmask of supported voltages. See SD_VOLTAGE_*
  363. definitions.
  364. FundamentalClock - Stores the fundamental clock speed in Hertz.
  365. HostCapabilities - Stores the host controller capability bits See SD_MODE_*
  366. definitions.
  367. OsDevice - Stores a pointer to the OS device.
  368. --*/
  369. typedef struct _SD_INITIALIZATION_BLOCK {
  370. PVOID StandardControllerBase;
  371. PVOID ConsumerContext;
  372. SD_FUNCTION_TABLE FunctionTable;
  373. ULONG Voltages;
  374. ULONG FundamentalClock;
  375. ULONG HostCapabilities;
  376. PDEVICE OsDevice;
  377. } SD_INITIALIZATION_BLOCK, *PSD_INITIALIZATION_BLOCK;
  378. typedef
  379. VOID
  380. (*PSD_IO_COMPLETION_ROUTINE) (
  381. PSD_CONTROLLER Controller,
  382. PVOID Context,
  383. UINTN BytesCompleted,
  384. KSTATUS Status
  385. );
  386. /*++
  387. Routine Description:
  388. This routine is called by the SD library when a DMA transfer completes.
  389. This routine is called from a DPC and, as a result, can get called back
  390. at dispatch level.
  391. Arguments:
  392. Controller - Supplies a pointer to the controller.
  393. Context - Supplies a context pointer passed to the library when the DMA
  394. request was issued.
  395. BytesCompleted - Supplies the number of bytes successfully read or written.
  396. Status - Supplies the status code representing the completion of the I/O.
  397. Return Value:
  398. None.
  399. --*/
  400. /*++
  401. Structure Description:
  402. This structure defines the context for an SD/MMC controller instance.
  403. Members:
  404. ControllerBase - Stores a pointer to the base address of the host
  405. controller registers.
  406. InterruptHandle - Stores the interrupt handle of the controller.
  407. ConsumerContext - Stores a context pointer passed to the function pointers
  408. contained in this structure.
  409. FunctionTable - Stores a table of routines used to implement controller
  410. specific
  411. Voltages - Stores a bitmask of supported voltages.
  412. CurrentVoltage - Stores the current voltage, in millivolts.
  413. Version - Stores the specification revision of the card.
  414. HostVersion - Stores the version of the host controller interface.
  415. Flags - Stores a bitmask of SD controller flags. See SD_CONTROLLER_FLAG_*
  416. for definitions.
  417. CardAddress - Stores the card address.
  418. BusWidth - Stores the width of the bus. Valid values are 1, 4 and 8.
  419. ClockSpeed - Stores the bus clock speed. This must start at the lowest
  420. setting (400kHz) until it's known how fast the card can go.
  421. FundamentalClock - Stores the fundamental clock speed in Hertz.
  422. ReadBlockLength - Stores the block length when reading blocks from the
  423. card.
  424. WriteBlockLength - Stores the block length when writing blocks to the
  425. card.
  426. UserCapacity - Stores the primary capacity of the controller, in bytes.
  427. BootCapacity - Stores the capacity of the boot partition, in bytes.
  428. RpmbCapacity - Stores the capacity of the Replay Protected Memory Block, in
  429. bytes.
  430. GeneralPartitionCapacity - Stores the capacity of the general partitions,
  431. in bytes.
  432. EraseGroupSize - Stores the erase group size of the card, in blocks.
  433. CardSpecificData - Stores the card specific data.
  434. PartitionConfiguration - Stores the partition configuration of this device.
  435. HostCapabilities - Stores the host controller capability bits.
  436. CardCapabilities - Stores the card capability bits.
  437. MaxBlocksPerTransfer - Stores the maximum number of blocks that can occur
  438. in a single transfer. The default is SD_MAX_BLOCK_COUNT.
  439. EnabledInterrupts - Stores a shadow copy of the bitmask of flags set in
  440. the interrupt enable register (not the interrupt status enable
  441. register).
  442. DmaDescriptorTable - Stores a pointer to the I/O buffer of the DMA
  443. descriptor table.
  444. IoCompletionRoutine - Stores a pointer to a routine called when DMA I/O
  445. completes.
  446. IoCompletionContext - Stores the I/O completion context associated with the
  447. DMA transfer.
  448. IoRequestSize - Stores the request size of the pending DMA operation.
  449. PendingStatusBits - Stores the mask of pending interrupt status bits.
  450. Timeout - Stores the timeout duration, in time counter ticks.
  451. SendStop - Stores a boolean indicating whether a stop CMD12 needs to be
  452. sent after the data transfer or not.
  453. Try - Stores the number of times the current I/O has been attempted.
  454. OsDevice - Stores a pointer to the OS device.
  455. --*/
  456. struct _SD_CONTROLLER {
  457. PVOID ControllerBase;
  458. HANDLE InterruptHandle;
  459. PVOID ConsumerContext;
  460. SD_FUNCTION_TABLE FunctionTable;
  461. ULONG Voltages;
  462. SD_VOLTAGE CurrentVoltage;
  463. SD_VERSION Version;
  464. SD_HOST_VERSION HostVersion;
  465. volatile ULONG Flags;
  466. USHORT CardAddress;
  467. USHORT BusWidth;
  468. SD_CLOCK_SPEED ClockSpeed;
  469. ULONG FundamentalClock;
  470. ULONG ReadBlockLength;
  471. ULONG WriteBlockLength;
  472. ULONGLONG UserCapacity;
  473. ULONGLONG BootCapacity;
  474. ULONGLONG RpmbCapacity;
  475. ULONGLONG GeneralPartitionCapacity[SD_MMC_GENERAL_PARTITION_COUNT];
  476. ULONG EraseGroupSize;
  477. ULONG CardSpecificData[SD_MMC_CSD_WORDS];
  478. ULONG PartitionConfiguration;
  479. ULONG HostCapabilities;
  480. ULONG CardCapabilities;
  481. ULONG MaxBlocksPerTransfer;
  482. ULONG EnabledInterrupts;
  483. PIO_BUFFER DmaDescriptorTable;
  484. PSD_IO_COMPLETION_ROUTINE IoCompletionRoutine;
  485. PVOID IoCompletionContext;
  486. UINTN IoRequestSize;
  487. volatile ULONG PendingStatusBits;
  488. ULONGLONG Timeout;
  489. BOOL SendStop;
  490. LONG Try;
  491. PDEVICE OsDevice;
  492. };
  493. /*++
  494. Structure Description:
  495. This structure describes the card identification data from the card.
  496. Members:
  497. Crc7 - Stores the CRC7, shifted by 1. The lowest bit is always 1.
  498. ManufacturingDate - Stores a binary coded decimal date, in the form yym,
  499. where year is offset from 2000. For example, April 2001 is 0x014.
  500. SerialNumber - Stores the product serial number.
  501. ProductRevision - Stores the product revision code.
  502. ProductName - Stores the product name string in ASCII.
  503. OemId - Stores the Original Equipment Manufacturer identifier.
  504. ManufacturerId - Stores the manufacturer identification number.
  505. --*/
  506. typedef struct _SD_CARD_IDENTIFICATION {
  507. UCHAR Crc7;
  508. UCHAR ManufacturingDate[2];
  509. UCHAR SerialNumber[4];
  510. UCHAR ProductRevision;
  511. UCHAR ProductName[5];
  512. UCHAR OemId[2];
  513. UCHAR ManufacturerId;
  514. } PACKED SD_CARD_IDENTIFICATION, *PSD_CARD_IDENTIFICATION;
  515. /*++
  516. Structure Description:
  517. This structure describes the card identification data from the card.
  518. Members:
  519. Attributes - Stores the attributes and length of this descriptor. See
  520. SD_ADMA2_* definitions.
  521. Address - Stores the 32-bit physical address of the data buffer this
  522. transfer descriptor refers to.
  523. --*/
  524. typedef struct _SD_ADMA2_DESCRIPTOR {
  525. ULONG Attributes;
  526. ULONG Address;
  527. } PACKED SD_ADMA2_DESCRIPTOR, *PSD_ADMA2_DESCRIPTOR;
  528. //
  529. // -------------------------------------------------------------------- Globals
  530. //
  531. //
  532. // -------------------------------------------------------- Function Prototypes
  533. //
  534. SD_API
  535. PSD_CONTROLLER
  536. SdCreateController (
  537. PSD_INITIALIZATION_BLOCK Parameters
  538. );
  539. /*++
  540. Routine Description:
  541. This routine creates a new SD controller object.
  542. Arguments:
  543. Parameters - Supplies a pointer to the parameters to use when creating the
  544. controller. This can be stack allocated, as the SD library won't use
  545. this memory after this routine returns.
  546. Return Value:
  547. Returns a pointer to the controller structure on success.
  548. NULL on allocation failure or if a required parameter was not filled in.
  549. --*/
  550. SD_API
  551. VOID
  552. SdDestroyController (
  553. PSD_CONTROLLER Controller
  554. );
  555. /*++
  556. Routine Description:
  557. This routine destroys an SD controller object.
  558. Arguments:
  559. Controller - Supplies a pointer to the controller to destroy.
  560. Return Value:
  561. None.
  562. --*/
  563. SD_API
  564. KSTATUS
  565. SdInitializeController (
  566. PSD_CONTROLLER Controller,
  567. BOOL ResetController
  568. );
  569. /*++
  570. Routine Description:
  571. This routine resets and initializes the SD host controller.
  572. Arguments:
  573. Controller - Supplies a pointer to the controller to initialize.
  574. ResetController - Supplies a boolean indicating whether or not to reset
  575. the controller.
  576. Return Value:
  577. Status code.
  578. --*/
  579. SD_API
  580. KSTATUS
  581. SdBlockIoPolled (
  582. PSD_CONTROLLER Controller,
  583. ULONGLONG BlockOffset,
  584. UINTN BlockCount,
  585. PVOID BufferVirtual,
  586. BOOL Write
  587. );
  588. /*++
  589. Routine Description:
  590. This routine performs a block I/O read or write using the CPU and not
  591. DMA.
  592. Arguments:
  593. Controller - Supplies a pointer to the controller.
  594. BlockOffset - Supplies the logical block address of the I/O.
  595. BlockCount - Supplies the number of blocks to read or write.
  596. BufferVirtual - Supplies the virtual address of the I/O buffer.
  597. Write - Supplies a boolean indicating if this is a read operation (FALSE)
  598. or a write operation.
  599. Return Value:
  600. Status code.
  601. --*/
  602. SD_API
  603. KSTATUS
  604. SdGetMediaParameters (
  605. PSD_CONTROLLER Controller,
  606. PULONGLONG BlockCount,
  607. PULONG BlockSize
  608. );
  609. /*++
  610. Routine Description:
  611. This routine returns information about the media card.
  612. Arguments:
  613. Controller - Supplies a pointer to the controller.
  614. BlockCount - Supplies a pointer where the number of blocks in the user
  615. area of the medium will be returned.
  616. BlockSize - Supplies a pointer where the block size of the medium will be
  617. returned.
  618. Return Value:
  619. STATUS_SUCCESS on success.
  620. STATUS_NO_MEDIA if there is no card in the slot.
  621. --*/
  622. SD_API
  623. KSTATUS
  624. SdAbortTransaction (
  625. PSD_CONTROLLER Controller,
  626. BOOL UseR1bResponse
  627. );
  628. /*++
  629. Routine Description:
  630. This routine aborts the current SD transaction on the controller.
  631. Arguments:
  632. Controller - Supplies a pointer to the controller.
  633. UseR1bResponse - Supplies a boolean indicating whether to use the R1
  634. (FALSE) or R1b (TRUE) response for the STOP (CMD12) command.
  635. Return Value:
  636. Status code.
  637. --*/
  638. SD_API
  639. VOID
  640. SdSetCriticalMode (
  641. PSD_CONTROLLER Controller,
  642. BOOL Enable
  643. );
  644. /*++
  645. Routine Description:
  646. This routine sets the SD controller into and out of critical execution
  647. mode. Critical execution mode is necessary for crash dump scenarios in
  648. which timeouts must be calculated by querying the hardware time counter
  649. directly, as the clock is not running to update the kernel's time counter.
  650. Arguments:
  651. Controller - Supplies a pointer to the controller.
  652. Enable - Supplies a boolean indicating if critical mode should be enabled
  653. or disabled.
  654. Return Value:
  655. None.
  656. --*/
  657. SD_API
  658. KSTATUS
  659. SdErrorRecovery (
  660. PSD_CONTROLLER Controller
  661. );
  662. /*++
  663. Routine Description:
  664. This routine attempts to perform recovery after an error.
  665. Arguments:
  666. Controller - Supplies a pointer to the controller.
  667. Return Value:
  668. Status code.
  669. --*/
  670. SD_API
  671. KSTATUS
  672. SdSendBlockCount (
  673. PSD_CONTROLLER Controller,
  674. ULONG BlockCount,
  675. BOOL Write,
  676. BOOL InterruptCompletion
  677. );
  678. /*++
  679. Routine Description:
  680. This routine sends a CMD23 to pre-specify the block count.
  681. Arguments:
  682. Controller - Supplies a pointer to the controller.
  683. BlockCount - Supplies the block count to set.
  684. Write - Supplies a boolean indicating if this is a write (TRUE) or read
  685. (FALSE).
  686. InterruptCompletion - Supplies a boolean indicating whether to poll on
  687. completion of the command (FALSE) or wait for a transfer done interrupt
  688. (TRUE).
  689. Return Value:
  690. STATUS_SUCCESS if the command has been queued.
  691. STATUS_NOT_SUPPORTED if the card or controller does not support ACMD23.
  692. --*/
  693. SD_API
  694. KSTATUS
  695. SdSendStop (
  696. PSD_CONTROLLER Controller,
  697. BOOL UseR1bResponse,
  698. BOOL InterruptCompletion
  699. );
  700. /*++
  701. Routine Description:
  702. This routine sends a CMD12 to stop the current transfer.
  703. Arguments:
  704. Controller - Supplies a pointer to the controller.
  705. UseR1bResponse - Supplies a boolean indicating whether to use an R1b
  706. response (TRUE) or just R1 (FALSE) for more asynchronous aborts.
  707. InterruptCompletion - Supplies a boolean indicating whether to poll on
  708. completion of the command (FALSE) or wait for a transfer done interrupt
  709. (TRUE).
  710. Return Value:
  711. Status code.
  712. --*/
  713. SD_API
  714. ULONGLONG
  715. SdQueryTimeCounter (
  716. PSD_CONTROLLER Controller
  717. );
  718. /*++
  719. Routine Description:
  720. This routine returns a snap of the time counter. Depending on the mode of
  721. the SD controller, this may be just a recent snap of the time counter or
  722. the current value in the hardware.
  723. Arguments:
  724. Controller - Supplies a pointer to the controller.
  725. Return Value:
  726. Returns the number of ticks that have elapsed since the system was booted,
  727. or a recent tick value.
  728. --*/
  729. //
  730. // Standard SD host controller functions
  731. //
  732. SD_API
  733. INTERRUPT_STATUS
  734. SdStandardInterruptService (
  735. PSD_CONTROLLER Controller
  736. );
  737. /*++
  738. Routine Description:
  739. This routine implements the interrupt service routine for a standard SD
  740. controller.
  741. Arguments:
  742. Controller - Supplies a pointer to the controller.
  743. Return Value:
  744. Returns whether or not the SD controller caused the interrupt.
  745. --*/
  746. SD_API
  747. INTERRUPT_STATUS
  748. SdStandardInterruptServiceDispatch (
  749. PVOID Context
  750. );
  751. /*++
  752. Routine Description:
  753. This routine implements the interrupt handler that is called at dispatch
  754. level.
  755. Arguments:
  756. Context - Supplies a context pointer, which in this case is a pointer to
  757. the SD controller.
  758. Return Value:
  759. None.
  760. --*/
  761. SD_API
  762. KSTATUS
  763. SdStandardInitializeDma (
  764. PSD_CONTROLLER Controller
  765. );
  766. /*++
  767. Routine Description:
  768. This routine initializes standard DMA support in the host controller.
  769. Arguments:
  770. Controller - Supplies a pointer to the controller.
  771. Return Value:
  772. STATUS_SUCCESS on success.
  773. STATUS_NOT_SUPPORTED if the host controller does not support ADMA2.
  774. STATUS_INSUFFICIENT_RESOURCES on allocation failure.
  775. STATUS_NO_MEDIA if there is no card in the slot.
  776. --*/
  777. SD_API
  778. VOID
  779. SdStandardBlockIoDma (
  780. PSD_CONTROLLER Controller,
  781. ULONGLONG BlockOffset,
  782. UINTN BlockCount,
  783. PIO_BUFFER IoBuffer,
  784. UINTN IoBufferOffset,
  785. BOOL Write,
  786. PSD_IO_COMPLETION_ROUTINE CompletionRoutine,
  787. PVOID CompletionContext
  788. );
  789. /*++
  790. Routine Description:
  791. This routine performs a block I/O read or write using standard ADMA2.
  792. Arguments:
  793. Controller - Supplies a pointer to the controller.
  794. BlockOffset - Supplies the logical block address of the I/O.
  795. BlockCount - Supplies the number of blocks to read or write.
  796. IoBuffer - Supplies a pointer to the buffer containing the data to write
  797. or where the read data should be returned.
  798. IoBufferOffset - Supplies the offset from the beginning of the I/O buffer
  799. where this I/O should begin. This is relative to the I/O buffer's
  800. current offset.
  801. Write - Supplies a boolean indicating if this is a read operation (FALSE)
  802. or a write operation.
  803. CompletionRoutine - Supplies a pointer to a function to call when the I/O
  804. completes.
  805. CompletionContext - Supplies a context pointer to pass as a parameter to
  806. the completion routine.
  807. Return Value:
  808. None. The status of the operation is returned when the completion routine
  809. is called, which may be during the execution of this function in the case
  810. of an early failure.
  811. --*/
  812. SD_API
  813. KSTATUS
  814. SdStandardInitializeController (
  815. PSD_CONTROLLER Controller,
  816. PVOID Context,
  817. ULONG Phase
  818. );
  819. /*++
  820. Routine Description:
  821. This routine performs any controller specific initialization steps.
  822. Arguments:
  823. Controller - Supplies a pointer to the controller.
  824. Context - Supplies a context pointer passed to the SD/MMC library upon
  825. creation of the controller.
  826. Phase - Supplies the phase of initialization. Phase 0 happens after the
  827. initial software reset and Phase 1 happens after the bus width has been
  828. set to 1 and the speed to 400KHz.
  829. Return Value:
  830. Status code.
  831. --*/
  832. SD_API
  833. KSTATUS
  834. SdStandardResetController (
  835. PSD_CONTROLLER Controller,
  836. PVOID Context,
  837. ULONG Flags
  838. );
  839. /*++
  840. Routine Description:
  841. This routine performs a soft reset of the SD controller.
  842. Arguments:
  843. Controller - Supplies a pointer to the controller.
  844. Context - Supplies a context pointer passed to the SD/MMC library upon
  845. creation of the controller.
  846. Flags - Supplies a bitmask of reset flags. See SD_RESET_FLAG_* for
  847. definitions.
  848. Return Value:
  849. Status code.
  850. --*/
  851. SD_API
  852. KSTATUS
  853. SdStandardSendCommand (
  854. PSD_CONTROLLER Controller,
  855. PVOID Context,
  856. PSD_COMMAND Command
  857. );
  858. /*++
  859. Routine Description:
  860. This routine sends the given command to the card.
  861. Arguments:
  862. Controller - Supplies a pointer to the controller.
  863. Context - Supplies a context pointer passed to the SD/MMC library upon
  864. creation of the controller.
  865. Command - Supplies a pointer to the command parameters.
  866. Return Value:
  867. Status code.
  868. --*/
  869. SD_API
  870. KSTATUS
  871. SdStandardGetSetBusWidth (
  872. PSD_CONTROLLER Controller,
  873. PVOID Context,
  874. BOOL Set
  875. );
  876. /*++
  877. Routine Description:
  878. This routine gets or sets the controller's bus width. The bus width is
  879. stored in the controller structure.
  880. Arguments:
  881. Controller - Supplies a pointer to the controller.
  882. Context - Supplies a context pointer passed to the SD/MMC library upon
  883. creation of the controller.
  884. Set - Supplies a boolean indicating whether the bus width should be queried
  885. or set.
  886. Return Value:
  887. Status code.
  888. --*/
  889. SD_API
  890. KSTATUS
  891. SdStandardGetSetClockSpeed (
  892. PSD_CONTROLLER Controller,
  893. PVOID Context,
  894. BOOL Set
  895. );
  896. /*++
  897. Routine Description:
  898. This routine gets or sets the controller's clock speed. The clock speed is
  899. stored in the controller structure.
  900. Arguments:
  901. Controller - Supplies a pointer to the controller.
  902. Context - Supplies a context pointer passed to the SD/MMC library upon
  903. creation of the controller.
  904. Set - Supplies a boolean indicating whether the clock speed should be
  905. queried (FALSE) or set (TRUE).
  906. Return Value:
  907. Status code.
  908. --*/
  909. SD_API
  910. KSTATUS
  911. SdStandardGetSetVoltage (
  912. PSD_CONTROLLER Controller,
  913. PVOID Context,
  914. BOOL Set
  915. );
  916. /*++
  917. Routine Description:
  918. This routine gets or sets the bus voltage. The bus voltage is
  919. stored in the controller structure.
  920. Arguments:
  921. Controller - Supplies a pointer to the controller.
  922. Context - Supplies a context pointer passed to the SD/MMC library upon
  923. creation of the controller.
  924. Set - Supplies a boolean indicating whether the bus voltage should be
  925. queried (FALSE) or set (TRUE).
  926. Return Value:
  927. Status code.
  928. --*/
  929. SD_API
  930. VOID
  931. SdStandardStopDataTransfer (
  932. PSD_CONTROLLER Controller,
  933. PVOID Context
  934. );
  935. /*++
  936. Routine Description:
  937. This routine stops any current data transfer on the controller.
  938. Arguments:
  939. Controller - Supplies a pointer to the controller.
  940. Context - Supplies a context pointer passed to the SD/MMC library upon
  941. creation of the controller.
  942. Return Value:
  943. None.
  944. --*/
  945. SD_API
  946. VOID
  947. SdStandardMediaChangeCallback (
  948. PSD_CONTROLLER Controller,
  949. PVOID Context,
  950. BOOL Removal,
  951. BOOL Insertion
  952. );
  953. /*++
  954. Routine Description:
  955. This routine is called by the SD library to notify the user of the SD
  956. library that media has been removed, inserted, or both. This routine is
  957. called from a DPC and, as a result, can get called back at dispatch level.
  958. Arguments:
  959. Controller - Supplies a pointer to the controller.
  960. Context - Supplies a context pointer passed to the SD/MMC library upon
  961. creation of the controller.
  962. Removal - Supplies a boolean indicating if a removal event has occurred.
  963. Insertion - Supplies a boolean indicating if an insertion event has
  964. occurred.
  965. Return Value:
  966. None.
  967. --*/