sd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. sd.h
  9. Abstract:
  10. This header contains definitions for the SD/MMC device library and
  11. definitions common to controllers that follow the SD specification.
  12. Author:
  13. Evan Green 27-Feb-2014
  14. --*/
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <minoca/sd/sdstd.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. //
  23. // Define software-only capability flags (ie these bits don't show up in the
  24. // hardware).
  25. //
  26. #define SD_MODE_HIGH_SPEED 0x0001
  27. #define SD_MODE_HIGH_SPEED_52MHZ 0x0002
  28. #define SD_MODE_4BIT 0x0004
  29. #define SD_MODE_8BIT 0x0008
  30. #define SD_MODE_SPI 0x0010
  31. #define SD_MODE_HIGH_CAPACITY 0x0020
  32. #define SD_MODE_AUTO_CMD12 0x0040
  33. #define SD_MODE_ADMA2 0x0080
  34. #define SD_MODE_RESPONSE136_SHIFTED 0x0100
  35. //
  36. // Define the software only reset flags.
  37. //
  38. #define SD_RESET_FLAG_ALL 0x00000001
  39. #define SD_RESET_FLAG_COMMAND_LINE 0x00000002
  40. #define SD_RESET_FLAG_DATA_LINE 0x00000004
  41. //
  42. // ------------------------------------------------------ Data Type Definitions
  43. //
  44. typedef struct _EFI_SD_CONTROLLER EFI_SD_CONTROLLER, *PEFI_SD_CONTROLLER;
  45. /*++
  46. Structure Description:
  47. This structure stores information about an SD card command.
  48. Members:
  49. Command - Stores the command number.
  50. ResponseType - Stores the response class expected from this command.
  51. CommandArgument - Stores the argument to the command.
  52. Response - Stores the response data from the executed command.
  53. BufferSize - Stores the size of the data buffer in bytes.
  54. Buffer - Stores the physical address of the data buffer.
  55. Write - Stores a boolean indicating if this is a data read or write. This
  56. is only used if the buffer size is non-zero.
  57. --*/
  58. typedef struct _SD_COMMAND {
  59. SD_COMMAND_VALUE Command;
  60. UINT32 ResponseType;
  61. UINT32 CommandArgument;
  62. UINT32 Response[4];
  63. UINT32 BufferSize;
  64. VOID *Buffer;
  65. BOOLEAN Write;
  66. } SD_COMMAND, *PSD_COMMAND;
  67. typedef
  68. EFI_STATUS
  69. (*PSD_INITIALIZE_CONTROLLER) (
  70. PEFI_SD_CONTROLLER Controller,
  71. VOID *Context,
  72. UINT32 Phase
  73. );
  74. /*++
  75. Routine Description:
  76. This routine performs any controller specific initialization steps.
  77. Arguments:
  78. Controller - Supplies a pointer to the controller.
  79. Context - Supplies a context pointer passed to the SD/MMC library upon
  80. creation of the controller.
  81. Phase - Supplies the phase of initialization. Phase 0 happens after the
  82. initial software reset and Phase 1 happens after the bus width has been
  83. set to 1 and the speed to 400KHz.
  84. Return Value:
  85. Status code.
  86. --*/
  87. typedef
  88. EFI_STATUS
  89. (*PSD_RESET_CONTROLLER) (
  90. PEFI_SD_CONTROLLER Controller,
  91. VOID *Context,
  92. UINT32 Flags
  93. );
  94. /*++
  95. Routine Description:
  96. This routine performs a soft reset of the SD controller.
  97. Arguments:
  98. Controller - Supplies a pointer to the controller.
  99. Context - Supplies a context pointer passed to the SD/MMC library upon
  100. creation of the controller.
  101. Flags - Supplies a bitmask of reset flags. See SD_RESET_FLAG_* for
  102. definitions.
  103. Return Value:
  104. Status code.
  105. --*/
  106. typedef
  107. EFI_STATUS
  108. (*PSD_SEND_COMMAND) (
  109. PEFI_SD_CONTROLLER Controller,
  110. VOID *Context,
  111. PSD_COMMAND Command
  112. );
  113. /*++
  114. Routine Description:
  115. This routine sends the given command to the card.
  116. Arguments:
  117. Controller - Supplies a pointer to the controller.
  118. Context - Supplies a context pointer passed to the SD/MMC library upon
  119. creation of the controller.
  120. Command - Supplies a pointer to the command parameters.
  121. Return Value:
  122. Status code.
  123. --*/
  124. typedef
  125. EFI_STATUS
  126. (*PSD_GET_SET_BUS_WIDTH) (
  127. PEFI_SD_CONTROLLER Controller,
  128. VOID *Context,
  129. UINT16 *BusWidth,
  130. BOOLEAN Set
  131. );
  132. /*++
  133. Routine Description:
  134. This routine gets or sets the controller's bus width.
  135. Arguments:
  136. Controller - Supplies a pointer to the controller.
  137. Context - Supplies a context pointer passed to the SD/MMC library upon
  138. creation of the controller.
  139. BusWidth - Supplies a pointer that receives bus width information on get
  140. and contains bus width information on set.
  141. Set - Supplies a boolean indicating whether the bus width should be queried
  142. or set.
  143. Return Value:
  144. Status code.
  145. --*/
  146. typedef
  147. EFI_STATUS
  148. (*PSD_GET_SET_CLOCK_SPEED) (
  149. PEFI_SD_CONTROLLER Controller,
  150. VOID *Context,
  151. UINT32 *ClockSpeed,
  152. BOOLEAN Set
  153. );
  154. /*++
  155. Routine Description:
  156. This routine gets or sets the controller's clock speed.
  157. Arguments:
  158. Controller - Supplies a pointer to the controller.
  159. Context - Supplies a context pointer passed to the SD/MMC library upon
  160. creation of the controller.
  161. ClockSpeed - Supplies a pointer that receives the current clock speed on
  162. get and contains the desired clock speed on set.
  163. Set - Supplies a boolean indicating whether the bus width should be queried
  164. or set.
  165. Return Value:
  166. Status code.
  167. --*/
  168. /*++
  169. Structure Description:
  170. This structure defines the set of SD functions that may need to be supplied
  171. to the base SD driver in case the host controller is not standard.
  172. Members:
  173. InitializeController - Store a pointer to a function used to initialize the
  174. controller.
  175. ResetController - Stores a pointer to a function used to reset the
  176. controller.
  177. SendCommand - Stores a pointer to a function used to send commands to the
  178. SD/MMC device.
  179. GetSetBusWidth - Store a pointer to a function used to get or set the
  180. controller's bus width.
  181. GetSetClockSpeed - Stores a pointer to a function used to get or set the
  182. controller's clock speed.
  183. --*/
  184. typedef struct _SD_FUNCTION_TABLE {
  185. PSD_INITIALIZE_CONTROLLER InitializeController;
  186. PSD_RESET_CONTROLLER ResetController;
  187. PSD_SEND_COMMAND SendCommand;
  188. PSD_GET_SET_BUS_WIDTH GetSetBusWidth;
  189. PSD_GET_SET_CLOCK_SPEED GetSetClockSpeed;
  190. } SD_FUNCTION_TABLE, *PSD_FUNCTION_TABLE;
  191. typedef
  192. EFI_STATUS
  193. (*PSD_GET_CARD_DETECT_STATUS) (
  194. PEFI_SD_CONTROLLER Controller,
  195. VOID *Context,
  196. BOOLEAN *CardPresent
  197. );
  198. /*++
  199. Routine Description:
  200. This routine determines if there is currently a card in the given SD/MMC
  201. controller.
  202. Arguments:
  203. Controller - Supplies a pointer to the controller.
  204. Context - Supplies a context pointer passed to the SD/MMC library upon
  205. creation of the controller.
  206. CardPresent - Supplies a pointer where a boolean will be returned
  207. indicating if a card is present or not.
  208. Return Value:
  209. Status code.
  210. --*/
  211. typedef
  212. EFI_STATUS
  213. (*PSD_GET_WRITE_PROTECT_STATUS) (
  214. PEFI_SD_CONTROLLER Controller,
  215. VOID *Context,
  216. BOOLEAN *WriteProtect
  217. );
  218. /*++
  219. Routine Description:
  220. This routine determines the state of the write protect switch on the
  221. SD/MMC card.
  222. Arguments:
  223. Controller - Supplies a pointer to the controller.
  224. Context - Supplies a context pointer passed to the SD/MMC library upon
  225. creation of the controller.
  226. WriteProtect - Supplies a pointer where a boolean will be returned
  227. indicating if writes are disallowed (TRUE) or if writing is allowed
  228. (FALSE).
  229. Return Value:
  230. Status code.
  231. --*/
  232. /*++
  233. Structure Description:
  234. This structure defines the initialization parameters passed upon creation
  235. of a new SD controller.
  236. Members:
  237. StandardControllerBase - Stores an optional pointer to the base address of
  238. the standard SD host controller registers. If this is not supplied,
  239. then the override function table must be supplied.
  240. OverrideFunctionTable - Stores an optional pointer to a table of functions
  241. used to override the standard SD behavior. This should be used when a
  242. device follows the SD specification but has a non-standard register
  243. set.
  244. ConsumerContext - Stores a context pointer passed to the function pointers
  245. contained in this structure.
  246. GetCardDetectStatus - Stores an optional pointer to a function used to
  247. determine if there is a card in the slot.
  248. GetWriteProtectStatus - Stores an optional pointer to a function used to
  249. determine the state of the physical write protect switch on the card.
  250. Voltages - Stores a bitmask of supported voltages. See SD_VOLTAGE_*
  251. definitions.
  252. FundamentalClock - Stores the fundamental clock speed in Hertz.
  253. HostCapabilities - Stores the host controller capability bits See SD_MODE_*
  254. definitions.
  255. --*/
  256. typedef struct _EFI_SD_INITIALIZATION_BLOCK {
  257. VOID *StandardControllerBase;
  258. PSD_FUNCTION_TABLE OverrideFunctionTable;
  259. VOID *ConsumerContext;
  260. PSD_GET_CARD_DETECT_STATUS GetCardDetectStatus;
  261. PSD_GET_WRITE_PROTECT_STATUS GetWriteProtectStatus;
  262. UINT32 Voltages;
  263. UINT32 FundamentalClock;
  264. UINT32 HostCapabilities;
  265. } EFI_SD_INITIALIZATION_BLOCK, *PEFI_SD_INITIALIZATION_BLOCK;
  266. /*++
  267. Structure Description:
  268. This structure describes the card identification data from the card.
  269. Members:
  270. Crc7 - Stores the CRC7, shifted by 1. The lowest bit is always 1.
  271. ManufacturingDate - Stores a binary coded decimal date, in the form yym,
  272. where year is offset from 2000. For example, April 2001 is 0x014.
  273. SerialNumber - Stores the product serial number.
  274. ProductRevision - Stores the product revision code.
  275. ProductName - Stores the product name string in ASCII.
  276. OemId - Stores the Original Equipment Manufacturer identifier.
  277. ManufacturerId - Stores the manufacturer identification number.
  278. --*/
  279. #pragma pack(push, 1)
  280. typedef struct _SD_CARD_IDENTIFICATION {
  281. UINT8 Crc7;
  282. UINT8 ManufacturingDate[2];
  283. UINT8 SerialNumber[4];
  284. UINT8 ProductRevision;
  285. UINT8 ProductName[5];
  286. UINT8 OemId[2];
  287. UINT8 ManufacturerId;
  288. } PACKED SD_CARD_IDENTIFICATION, *PSD_CARD_IDENTIFICATION;
  289. #pragma pack(pop)
  290. //
  291. // -------------------------------------------------------------------- Globals
  292. //
  293. //
  294. // -------------------------------------------------------- Function Prototypes
  295. //
  296. PEFI_SD_CONTROLLER
  297. EfiSdCreateController (
  298. PEFI_SD_INITIALIZATION_BLOCK Parameters
  299. );
  300. /*++
  301. Routine Description:
  302. This routine creates a new SD controller object.
  303. Arguments:
  304. Parameters - Supplies a pointer to the parameters to use when creating the
  305. controller. This can be stack allocated, as the SD library won't use
  306. this memory after this routine returns.
  307. Return Value:
  308. Returns a pointer to the controller structure on success.
  309. NULL on allocation failure or if a required parameter was not filled in.
  310. --*/
  311. VOID
  312. EfiSdDestroyController (
  313. PEFI_SD_CONTROLLER Controller
  314. );
  315. /*++
  316. Routine Description:
  317. This routine destroys an SD controller object.
  318. Arguments:
  319. Controller - Supplies a pointer to the controller to destroy.
  320. Return Value:
  321. None.
  322. --*/
  323. EFI_STATUS
  324. EfiSdInitializeController (
  325. PEFI_SD_CONTROLLER Controller,
  326. BOOLEAN ResetController
  327. );
  328. /*++
  329. Routine Description:
  330. This routine resets and initializes the SD host controller.
  331. Arguments:
  332. Controller - Supplies a pointer to the controller to initialize.
  333. ResetController - Supplies a boolean indicating whether or not to reset
  334. the controller.
  335. Return Value:
  336. Status code.
  337. --*/
  338. EFI_STATUS
  339. EfiSdBlockIoPolled (
  340. PEFI_SD_CONTROLLER Controller,
  341. UINT64 BlockOffset,
  342. UINTN BlockCount,
  343. VOID *BufferVirtual,
  344. BOOLEAN Write
  345. );
  346. /*++
  347. Routine Description:
  348. This routine performs a block I/O read or write using the CPU and not
  349. DMA.
  350. Arguments:
  351. Controller - Supplies a pointer to the controller.
  352. BlockOffset - Supplies the logical block address of the I/O.
  353. BlockCount - Supplies the number of blocks to read or write.
  354. BufferVirtual - Supplies the virtual address of the I/O buffer.
  355. Write - Supplies a boolean indicating if this is a read operation (FALSE)
  356. or a write operation.
  357. Return Value:
  358. Status code.
  359. --*/
  360. EFI_STATUS
  361. EfiSdGetMediaParameters (
  362. PEFI_SD_CONTROLLER Controller,
  363. UINT64 *BlockCount,
  364. UINT32 *BlockSize
  365. );
  366. /*++
  367. Routine Description:
  368. This routine returns information about the media card.
  369. Arguments:
  370. Controller - Supplies a pointer to the controller.
  371. BlockCount - Supplies a pointer where the number of blocks in the user
  372. area of the medium will be returned.
  373. BlockSize - Supplies a pointer where the block size of the medium will be
  374. returned.
  375. Return Value:
  376. EFI_SUCCESS on success.
  377. EFI_NO_MEDIA if there is no card in the slot.
  378. --*/