sd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. sd.c
  5. Abstract:
  6. This module implements BCM2709 SD support in UEFI.
  7. Author:
  8. Chris Stevens 5-Jan-2015
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include <dev/sd.h>
  17. #include <dev/bcm2709.h>
  18. #include <minoca/uefi/protocol/blockio.h>
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. //
  23. // This macro returns a pointer to the disk I/O data given a pointer to the
  24. // block I/O protocol instance.
  25. //
  26. #define EFI_SD_BCM2709_FROM_THIS(_BlockIo) \
  27. (EFI_SD_BCM2709_CONTEXT *)((VOID *)(_BlockIo) - \
  28. ((VOID *)(&(((EFI_SD_BCM2709_CONTEXT *)0)->BlockIo))))
  29. //
  30. // ---------------------------------------------------------------- Definitions
  31. //
  32. #define EFI_SD_BCM2709_MAGIC 0x32426453 // '2BdS'
  33. #define EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH_GUID \
  34. { \
  35. 0xFCA216DE, 0x950E, 0x11E4, \
  36. {0xBD, 0x11, 0x04, 0x01, 0x0F, 0xDD, 0x74, 0x01} \
  37. }
  38. //
  39. // ------------------------------------------------------ Data Type Definitions
  40. //
  41. /*++
  42. Structure Description:
  43. This structure defines the data necessary to enable the eMMC.
  44. Members:
  45. Header - Stores a header that defines the total size of the messages being
  46. sent to and received from the mailbox.
  47. DeviceState - Stores a request to set the state for a particular device.
  48. EndTag - Stores the tag to denote the end of the mailbox message.
  49. --*/
  50. typedef struct _EFI_SD_BCM2709_ENABLE_EMMC {
  51. BCM2709_MAILBOX_HEADER Header;
  52. BCM2709_MAILBOX_DEVICE_STATE DeviceState;
  53. UINT32 EndTag;
  54. } EFI_SD_BCM2709_ENABLE_EMMC, *PEFI_SD_BCM2709_ENABLE_EMMC;
  55. /*++
  56. Structure Description:
  57. This structure defines the data necessary to get the eMMC clock rate in Hz.
  58. Members:
  59. Header - Stores a header that defines the total size of the messages being
  60. sent to and received from the mailbox.
  61. ClockRate - Stores a request to get the rate for a particular clock.
  62. EndTag - Stores the tag to denote the end of the mailbox message.
  63. --*/
  64. typedef struct _EFI_SD_BCM2709_GET_EMMC_CLOCK {
  65. BCM2709_MAILBOX_HEADER Header;
  66. BCM2709_MAILBOX_GET_CLOCK_RATE ClockRate;
  67. UINT32 EndTag;
  68. } EFI_SD_BCM2709_GET_EMMC_CLOCK, *PEFI_SD_BCM2709_GET_EMMC_CLOCK;
  69. /*++
  70. Structure Description:
  71. This structure describes the SD BCM2709 device context.
  72. Members:
  73. Magic - Stores the magic constand EFI_SD_BCM2709_MAGIC.
  74. Handle - Stores the handle to the block I/O device.
  75. DevicePath - Stores a pointer to the device path.
  76. Controller - Stores an pointer to the controller structure.
  77. ControllerBase - Stores a pointer to the virtual address of the HSMMC
  78. registers.
  79. MediaPresent - Stores a boolean indicating whether or not there is a card
  80. in the slot.
  81. BlockSize - Stores the cached block size of the media.
  82. BlockCount - Stores the cached block count of the media.
  83. BlockIo - Stores the block I/O protocol.
  84. Media - Stores the block I/O media information.
  85. --*/
  86. typedef struct _EFI_SD_BCM2709_CONTEXT {
  87. UINT32 Magic;
  88. EFI_HANDLE Handle;
  89. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  90. PEFI_SD_CONTROLLER Controller;
  91. BOOLEAN MediaPresent;
  92. UINT32 BlockSize;
  93. UINT64 BlockCount;
  94. EFI_BLOCK_IO_PROTOCOL BlockIo;
  95. EFI_BLOCK_IO_MEDIA Media;
  96. } EFI_SD_BCM2709_CONTEXT, *PEFI_SD_BCM2709_CONTEXT;
  97. /*++
  98. Structure Description:
  99. This structure defines the SD BCM2709 block I/O device path.
  100. Members:
  101. DevicePath - Stores the standard vendor-specific device path.
  102. ControllerBase - Stores the controller number.
  103. --*/
  104. typedef struct _EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH {
  105. VENDOR_DEVICE_PATH DevicePath;
  106. UINT32 ControllerBase;
  107. } EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH, *PEFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH;
  108. /*++
  109. Structure Description:
  110. This structure defines the BCM2709 SD block I/O device path.
  111. Members:
  112. Disk - Stores the disk device path node.
  113. End - Stores the end device path node.
  114. --*/
  115. typedef struct _EFI_SD_BCM2709_DEVICE_PATH {
  116. EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH Disk;
  117. EFI_DEVICE_PATH_PROTOCOL End;
  118. } PACKED EFI_SD_BCM2709_DEVICE_PATH, *PEFI_SD_BCM2709_DEVICE_PATH;
  119. //
  120. // ----------------------------------------------- Internal Function Prototypes
  121. //
  122. EFIAPI
  123. EFI_STATUS
  124. EfipSdBcm2709Reset (
  125. EFI_BLOCK_IO_PROTOCOL *This,
  126. BOOLEAN ExtendedVerification
  127. );
  128. EFIAPI
  129. EFI_STATUS
  130. EfipSdBcm2709ReadBlocks (
  131. EFI_BLOCK_IO_PROTOCOL *This,
  132. UINT32 MediaId,
  133. EFI_LBA Lba,
  134. UINTN BufferSize,
  135. VOID *Buffer
  136. );
  137. EFIAPI
  138. EFI_STATUS
  139. EfipSdBcm2709WriteBlocks (
  140. EFI_BLOCK_IO_PROTOCOL *This,
  141. UINT32 MediaId,
  142. EFI_LBA Lba,
  143. UINTN BufferSize,
  144. VOID *Buffer
  145. );
  146. EFIAPI
  147. EFI_STATUS
  148. EfipSdBcm2709FlushBlocks (
  149. EFI_BLOCK_IO_PROTOCOL *This
  150. );
  151. EFI_STATUS
  152. EfipSdBcm2709ResetController (
  153. PEFI_SD_BCM2709_CONTEXT Device
  154. );
  155. //
  156. // -------------------------------------------------------------------- Globals
  157. //
  158. //
  159. // Define the private data template.
  160. //
  161. EFI_SD_BCM2709_CONTEXT EfiSdBcm2709DiskTemplate = {
  162. EFI_SD_BCM2709_MAGIC,
  163. NULL,
  164. NULL,
  165. NULL,
  166. FALSE,
  167. 0,
  168. 0,
  169. {
  170. EFI_BLOCK_IO_PROTOCOL_REVISION3,
  171. NULL,
  172. EfipSdBcm2709Reset,
  173. EfipSdBcm2709ReadBlocks,
  174. EfipSdBcm2709WriteBlocks,
  175. EfipSdBcm2709FlushBlocks
  176. },
  177. {0},
  178. };
  179. //
  180. // Define the device path template.
  181. //
  182. EFI_SD_BCM2709_DEVICE_PATH EfiSdBcm2709DevicePathTemplate = {
  183. {
  184. {
  185. {
  186. HARDWARE_DEVICE_PATH,
  187. HW_VENDOR_DP,
  188. sizeof(EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH)
  189. },
  190. EFI_SD_BCM2709_BLOCK_IO_DEVICE_PATH_GUID,
  191. },
  192. BCM2709_EMMC_OFFSET
  193. },
  194. {
  195. END_DEVICE_PATH_TYPE,
  196. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  197. END_DEVICE_PATH_LENGTH
  198. }
  199. };
  200. //
  201. // Define a template for the command to enable the eMMC power.
  202. //
  203. EFI_SD_BCM2709_ENABLE_EMMC EfiBcm2709EmmcPowerCommand = {
  204. {
  205. sizeof(EFI_SD_BCM2709_ENABLE_EMMC),
  206. 0
  207. },
  208. {
  209. {
  210. BCM2709_MAILBOX_TAG_SET_POWER_STATE,
  211. sizeof(UINT32) + sizeof(UINT32),
  212. sizeof(UINT32) + sizeof(UINT32)
  213. },
  214. BCM2709_MAILBOX_DEVICE_SDHCI,
  215. BCM2709_MAILBOX_POWER_STATE_ON
  216. },
  217. 0
  218. };
  219. //
  220. // Define a template for the command to get the eMMC clock rate.
  221. //
  222. EFI_SD_BCM2709_GET_EMMC_CLOCK EfiBcm2709EmmcGetClockRateCommand = {
  223. {
  224. sizeof(EFI_SD_BCM2709_GET_EMMC_CLOCK),
  225. 0
  226. },
  227. {
  228. {
  229. BCM2709_MAILBOX_TAG_GET_CLOCK_RATE,
  230. sizeof(UINT32) + sizeof(UINT32),
  231. sizeof(UINT32)
  232. },
  233. BCM2709_MAILBOX_CLOCK_ID_EMMC,
  234. 0
  235. },
  236. 0
  237. };
  238. //
  239. // ------------------------------------------------------------------ Functions
  240. //
  241. EFI_STATUS
  242. EfipBcm2709EnumerateSd (
  243. VOID
  244. )
  245. /*++
  246. Routine Description:
  247. This routine enumerates the SD card on the BCM2709.
  248. Arguments:
  249. None.
  250. Return Value:
  251. EFI status code.
  252. --*/
  253. {
  254. UINT64 BlockCount;
  255. UINT32 BlockSize;
  256. PEFI_SD_BCM2709_DEVICE_PATH DevicePath;
  257. PEFI_SD_BCM2709_CONTEXT Disk;
  258. UINT32 ExpectedLength;
  259. UINT32 Frequency;
  260. EFI_SD_BCM2709_GET_EMMC_CLOCK GetClockRate;
  261. UINT32 Length;
  262. EFI_SD_INITIALIZATION_BLOCK SdParameters;
  263. EFI_STATUS Status;
  264. //
  265. // The BCM2709 device library must be initialized to enumerate SD.
  266. //
  267. if (EfiBcm2709Initialized == FALSE) {
  268. return EFI_NOT_READY;
  269. }
  270. //
  271. // Allocate and initialize the context structure.
  272. //
  273. Status = EfiAllocatePool(EfiBootServicesData,
  274. sizeof(EFI_SD_BCM2709_CONTEXT),
  275. (VOID **)&Disk);
  276. if (EFI_ERROR(Status)) {
  277. return Status;
  278. }
  279. EfiCopyMem(Disk, &EfiSdBcm2709DiskTemplate, sizeof(EFI_SD_BCM2709_CONTEXT));
  280. Disk->BlockIo.Media = &(Disk->Media);
  281. Disk->Media.RemovableMedia = TRUE;
  282. //
  283. // Create the device path.
  284. //
  285. Status = EfiAllocatePool(EfiBootServicesData,
  286. sizeof(EFI_SD_BCM2709_DEVICE_PATH),
  287. (VOID **)&DevicePath);
  288. if (EFI_ERROR(Status)) {
  289. goto Bcm2709EnumerateSdEnd;
  290. }
  291. //
  292. // Update the device path template now that the BCM2709 device has a base
  293. // address. The global array only had the offset.
  294. //
  295. EfiSdBcm2709DevicePathTemplate.Disk.ControllerBase =
  296. (UINTN)BCM2709_EMMC_BASE;
  297. EfiCopyMem(DevicePath,
  298. &EfiSdBcm2709DevicePathTemplate,
  299. sizeof(EFI_SD_BCM2709_DEVICE_PATH));
  300. Disk->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  301. //
  302. // Initialize the eMMC's power.
  303. //
  304. Status = EfipBcm2709MailboxSendCommand(BCM2709_MAILBOX_PROPERTIES_CHANNEL,
  305. &EfiBcm2709EmmcPowerCommand,
  306. sizeof(EFI_SD_BCM2709_ENABLE_EMMC),
  307. TRUE);
  308. if (EFI_ERROR(Status)) {
  309. goto Bcm2709EnumerateSdEnd;
  310. }
  311. //
  312. // Get the eMMC's clock frequency.
  313. //
  314. EfiCopyMem(&GetClockRate,
  315. &EfiBcm2709EmmcGetClockRateCommand,
  316. sizeof(EFI_SD_BCM2709_GET_EMMC_CLOCK));
  317. Status = EfipBcm2709MailboxSendCommand(
  318. BCM2709_MAILBOX_PROPERTIES_CHANNEL,
  319. &GetClockRate,
  320. sizeof(EFI_SD_BCM2709_GET_EMMC_CLOCK),
  321. FALSE);
  322. if (EFI_ERROR(Status)) {
  323. goto Bcm2709EnumerateSdEnd;
  324. }
  325. Length = GetClockRate.ClockRate.TagHeader.Length;
  326. ExpectedLength = sizeof(BCM2709_MAILBOX_GET_CLOCK_RATE) -
  327. sizeof(BCM2709_MAILBOX_TAG);
  328. if (BCM2709_MAILBOX_CHECK_TAG_LENGTH(Length, ExpectedLength) == FALSE) {
  329. Status = EFI_DEVICE_ERROR;
  330. goto Bcm2709EnumerateSdEnd;
  331. }
  332. Frequency = GetClockRate.ClockRate.Rate;
  333. //
  334. // Create the SD controller.
  335. //
  336. EfiSetMem(&SdParameters, sizeof(EFI_SD_INITIALIZATION_BLOCK), 0);
  337. SdParameters.StandardControllerBase =
  338. (VOID *)DevicePath->Disk.ControllerBase;
  339. SdParameters.Voltages = SD_VOLTAGE_32_33 | SD_VOLTAGE_33_34;
  340. SdParameters.HostCapabilities = SD_MODE_4BIT |
  341. SD_MODE_RESPONSE136_SHIFTED |
  342. SD_MODE_HIGH_SPEED |
  343. SD_MODE_HIGH_SPEED_52MHZ |
  344. SD_MODE_AUTO_CMD12;
  345. SdParameters.FundamentalClock = Frequency;
  346. Disk->Controller = EfiSdCreateController(&SdParameters);
  347. if (Disk->Controller == NULL) {
  348. Status = EFI_OUT_OF_RESOURCES;
  349. goto Bcm2709EnumerateSdEnd;
  350. }
  351. //
  352. // Perform some initialization to see if the card is there.
  353. //
  354. Status = EfiSdInitializeController(Disk->Controller, TRUE);
  355. if (!EFI_ERROR(Status)) {
  356. Status = EfiSdGetMediaParameters(Disk->Controller,
  357. &BlockCount,
  358. &BlockSize);
  359. if (!EFI_ERROR(Status)) {
  360. Disk->MediaPresent = TRUE;
  361. Disk->BlockSize = BlockSize;
  362. Disk->BlockCount = BlockCount;
  363. Disk->Media.MediaPresent = TRUE;
  364. Disk->Media.BlockSize = BlockSize;
  365. Disk->Media.LastBlock = BlockCount - 1;
  366. }
  367. }
  368. Status = EfiInstallMultipleProtocolInterfaces(&(Disk->Handle),
  369. &EfiDevicePathProtocolGuid,
  370. Disk->DevicePath,
  371. &EfiBlockIoProtocolGuid,
  372. &(Disk->BlockIo),
  373. NULL);
  374. Bcm2709EnumerateSdEnd:
  375. if (EFI_ERROR(Status)) {
  376. if (Disk != NULL) {
  377. if (Disk->DevicePath != NULL) {
  378. EfiFreePool(DevicePath);
  379. }
  380. if (Disk->Controller != NULL) {
  381. EfiSdDestroyController(Disk->Controller);
  382. }
  383. EfiFreePool(Disk);
  384. }
  385. }
  386. return Status;
  387. }
  388. //
  389. // --------------------------------------------------------- Internal Functions
  390. //
  391. EFIAPI
  392. EFI_STATUS
  393. EfipSdBcm2709Reset (
  394. EFI_BLOCK_IO_PROTOCOL *This,
  395. BOOLEAN ExtendedVerification
  396. )
  397. /*++
  398. Routine Description:
  399. This routine resets the block device.
  400. Arguments:
  401. This - Supplies a pointer to the protocol instance.
  402. ExtendedVerification - Supplies a boolean indicating whether or not the
  403. driver should perform diagnostics on reset.
  404. Return Value:
  405. EFI_SUCCESS on success.
  406. EFI_DEVICE_ERROR if the device had an error and could not complete the
  407. request.
  408. --*/
  409. {
  410. PEFI_SD_BCM2709_CONTEXT Disk;
  411. EFI_STATUS Status;
  412. Disk = EFI_SD_BCM2709_FROM_THIS(This);
  413. Status = EfiSdInitializeController(Disk->Controller, TRUE);
  414. if (EFI_ERROR(Status)) {
  415. Disk->MediaPresent = FALSE;
  416. Disk->Media.MediaPresent = FALSE;
  417. } else {
  418. Disk->Media.MediaId += 1;
  419. Disk->Media.MediaPresent = TRUE;
  420. Disk->MediaPresent = TRUE;
  421. }
  422. return Status;
  423. }
  424. EFIAPI
  425. EFI_STATUS
  426. EfipSdBcm2709ReadBlocks (
  427. EFI_BLOCK_IO_PROTOCOL *This,
  428. UINT32 MediaId,
  429. EFI_LBA Lba,
  430. UINTN BufferSize,
  431. VOID *Buffer
  432. )
  433. /*++
  434. Routine Description:
  435. This routine performs a block I/O read from the device.
  436. Arguments:
  437. This - Supplies a pointer to the protocol instance.
  438. MediaId - Supplies the media identifier, which changes each time the media
  439. is replaced.
  440. Lba - Supplies the logical block address of the read.
  441. BufferSize - Supplies the size of the buffer in bytes.
  442. Buffer - Supplies the buffer where the read data will be returned.
  443. Return Value:
  444. EFI_SUCCESS on success.
  445. EFI_DEVICE_ERROR if the device had an error and could not complete the
  446. request.
  447. EFI_NO_MEDIA if there is no media in the device.
  448. EFI_MEDIA_CHANGED if the media ID does not match the current device.
  449. EFI_BAD_BUFFER_SIZE if the buffer was not a multiple of the device block
  450. size.
  451. EFI_INVALID_PARAMETER if the read request contains LBAs that are not valid,
  452. or the buffer is not properly aligned.
  453. --*/
  454. {
  455. PEFI_SD_BCM2709_CONTEXT Disk;
  456. EFI_STATUS Status;
  457. Disk = EFI_SD_BCM2709_FROM_THIS(This);
  458. if (MediaId != Disk->Media.MediaId) {
  459. return EFI_MEDIA_CHANGED;
  460. }
  461. if ((Disk->MediaPresent == FALSE) || (Disk->BlockSize == 0)) {
  462. return EFI_NO_MEDIA;
  463. }
  464. Status = EfiSdBlockIoPolled(Disk->Controller,
  465. Lba,
  466. BufferSize / Disk->BlockSize,
  467. Buffer,
  468. FALSE);
  469. return Status;
  470. }
  471. EFIAPI
  472. EFI_STATUS
  473. EfipSdBcm2709WriteBlocks (
  474. EFI_BLOCK_IO_PROTOCOL *This,
  475. UINT32 MediaId,
  476. EFI_LBA Lba,
  477. UINTN BufferSize,
  478. VOID *Buffer
  479. )
  480. /*++
  481. Routine Description:
  482. This routine performs a block I/O write to the device.
  483. Arguments:
  484. This - Supplies a pointer to the protocol instance.
  485. MediaId - Supplies the media identifier, which changes each time the media
  486. is replaced.
  487. Lba - Supplies the logical block address of the write.
  488. BufferSize - Supplies the size of the buffer in bytes.
  489. Buffer - Supplies the buffer containing the data to write.
  490. Return Value:
  491. EFI_SUCCESS on success.
  492. EFI_WRITE_PROTECTED if the device cannot be written to.
  493. EFI_DEVICE_ERROR if the device had an error and could not complete the
  494. request.
  495. EFI_NO_MEDIA if there is no media in the device.
  496. EFI_MEDIA_CHANGED if the media ID does not match the current device.
  497. EFI_BAD_BUFFER_SIZE if the buffer was not a multiple of the device block
  498. size.
  499. EFI_INVALID_PARAMETER if the read request contains LBAs that are not valid,
  500. or the buffer is not properly aligned.
  501. --*/
  502. {
  503. PEFI_SD_BCM2709_CONTEXT Disk;
  504. EFI_STATUS Status;
  505. Disk = EFI_SD_BCM2709_FROM_THIS(This);
  506. if (MediaId != Disk->Media.MediaId) {
  507. return EFI_MEDIA_CHANGED;
  508. }
  509. if ((Disk->MediaPresent == FALSE) || (Disk->BlockSize == 0)) {
  510. return EFI_NO_MEDIA;
  511. }
  512. Status = EfiSdBlockIoPolled(Disk->Controller,
  513. Lba,
  514. BufferSize / Disk->BlockSize,
  515. Buffer,
  516. TRUE);
  517. return Status;
  518. }
  519. EFIAPI
  520. EFI_STATUS
  521. EfipSdBcm2709FlushBlocks (
  522. EFI_BLOCK_IO_PROTOCOL *This
  523. )
  524. /*++
  525. Routine Description:
  526. This routine flushes the block device.
  527. Arguments:
  528. This - Supplies a pointer to the protocol instance.
  529. Return Value:
  530. EFI_SUCCESS on success.
  531. EFI_DEVICE_ERROR if the device had an error and could not complete the
  532. request.
  533. EFI_NO_MEDIA if there is no media in the device.
  534. --*/
  535. {
  536. return EFI_SUCCESS;
  537. }