sd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. sd.c
  5. Abstract:
  6. This module implements PandaBoard SD support in UEFI.
  7. Author:
  8. Evan Green 20-Mar-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include <dev/sd.h>
  17. #include <minoca/uefi/protocol/blockio.h>
  18. #include "pandafw.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_OMAP4_FROM_THIS(_BlockIo) \
  27. (EFI_SD_OMAP4_CONTEXT *)((VOID *)(_BlockIo) - \
  28. ((VOID *)(&(((EFI_SD_OMAP4_CONTEXT *)0)->BlockIo))))
  29. //
  30. // These macros read and write SD controller registers.
  31. //
  32. #define SD_OMAP4_READ_REGISTER(_Device, _Register) \
  33. *(volatile UINT32 *)((_Device)->ControllerBase + (_Register))
  34. #define SD_OMAP4_WRITE_REGISTER(_Device, _Register, _Value) \
  35. *(volatile UINT32 *)((_Device)->ControllerBase + (_Register)) = (_Value)
  36. //
  37. // ---------------------------------------------------------------- Definitions
  38. //
  39. #define EFI_SD_OMAP4_MAGIC 0x344F6453 // '4OdS'
  40. #define EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH_GUID \
  41. { \
  42. 0xCF31FAC5, 0xC24E, 0x11D2, \
  43. {0x85, 0xF3, 0x00, 0xA0, 0xC9, 0x3E, 0xA7, 0x39} \
  44. }
  45. //
  46. // Define the offset into the HSMMC block where the SD standard registers
  47. // start.
  48. //
  49. #define SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET 0x200
  50. //
  51. // Define the fundamental frequency of the HSMMC clock. An initial divisor of
  52. // 0x80 (divide by 256) gets a base frequency of 375000, just under the 400kHz
  53. // limit.
  54. //
  55. #define SD_OMAP4_FUNDAMENTAL_CLOCK_SPEED 96000000
  56. #define SD_OMAP4_INITIAL_DIVISOR 0x80
  57. #define SD_OMAP4_SYSCONFIG_REGISTER 0x10
  58. #define SD_OMAP4_SYSSTATUS_REGISTER 0x114
  59. #define SD_OMAP4_CON_REGISTER 0x12C
  60. //
  61. // Sysconfig register definitions
  62. //
  63. #define SD_OMAP4_SYSCONFIG_SOFT_RESET 0x00000001
  64. //
  65. // Sysstatus register definitions
  66. //
  67. #define SD_OMAP4_SYSSTATUS_RESET_DONE 0x00000001
  68. //
  69. // Con (control) register definitions.
  70. //
  71. #define SD_OMAP4_CON_INIT (1 << 1)
  72. #define SD_OMAP4_CON_DEBOUNCE_MASK (0x3 << 9)
  73. #define SD_OMAP4_CON_DMA_MASTER (1 << 20)
  74. //
  75. // Define the OMAP4 SD timeout in microseconds.
  76. //
  77. #define EFI_SD_OMAP4_TIMEOUT 1000000
  78. //
  79. // ------------------------------------------------------ Data Type Definitions
  80. //
  81. /*++
  82. Structure Description:
  83. This structure describes the SD OMAP4 device context.
  84. Members:
  85. Magic - Stores the magic constand EFI_SD_OMAP4_MAGIC.
  86. Handle - Stores the handle to the block I/O device.
  87. DevicePath - Stores a pointer to the device path.
  88. Controller - Stores an pointer to the controller structure.
  89. ControllerBase - Stores a pointer to the virtual address of the HSMMC
  90. registers.
  91. MediaPresent - Stores a boolean indicating whether or not there is a card
  92. in the slot.
  93. BlockSize - Stores the cached block size of the media.
  94. BlockCount - Stores the cached block count of the media.
  95. BlockIo - Stores the block I/O protocol.
  96. Media - Stores the block I/O media information.
  97. --*/
  98. typedef struct _EFI_SD_OMAP4_CONTEXT {
  99. UINT32 Magic;
  100. EFI_HANDLE Handle;
  101. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  102. PEFI_SD_CONTROLLER Controller;
  103. VOID *ControllerBase;
  104. BOOLEAN MediaPresent;
  105. UINT32 BlockSize;
  106. UINT64 BlockCount;
  107. EFI_BLOCK_IO_PROTOCOL BlockIo;
  108. EFI_BLOCK_IO_MEDIA Media;
  109. } EFI_SD_OMAP4_CONTEXT, *PEFI_SD_OMAP4_CONTEXT;
  110. /*++
  111. Structure Description:
  112. This structure defines the SD OMAP4 block I/O device path.
  113. Members:
  114. DevicePath - Stores the standard vendor-specific device path.
  115. ControllerBase - Stores the controller number.
  116. --*/
  117. typedef struct _EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH {
  118. VENDOR_DEVICE_PATH DevicePath;
  119. UINT32 ControllerBase;
  120. } EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH, *PEFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH;
  121. /*++
  122. Structure Description:
  123. This structure defines the OMAP4 SD block I/O device path.
  124. Members:
  125. Disk - Stores the disk device path node.
  126. End - Stores the end device path node.
  127. --*/
  128. typedef struct _EFI_SD_OMAP4_DEVICE_PATH {
  129. EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH Disk;
  130. EFI_DEVICE_PATH_PROTOCOL End;
  131. } PACKED EFI_SD_OMAP4_DEVICE_PATH, *PEFI_SD_OMAP4_DEVICE_PATH;
  132. //
  133. // ----------------------------------------------- Internal Function Prototypes
  134. //
  135. EFIAPI
  136. EFI_STATUS
  137. EfipSdOmap4Reset (
  138. EFI_BLOCK_IO_PROTOCOL *This,
  139. BOOLEAN ExtendedVerification
  140. );
  141. EFIAPI
  142. EFI_STATUS
  143. EfipSdOmap4ReadBlocks (
  144. EFI_BLOCK_IO_PROTOCOL *This,
  145. UINT32 MediaId,
  146. EFI_LBA Lba,
  147. UINTN BufferSize,
  148. VOID *Buffer
  149. );
  150. EFIAPI
  151. EFI_STATUS
  152. EfipSdOmap4WriteBlocks (
  153. EFI_BLOCK_IO_PROTOCOL *This,
  154. UINT32 MediaId,
  155. EFI_LBA Lba,
  156. UINTN BufferSize,
  157. VOID *Buffer
  158. );
  159. EFIAPI
  160. EFI_STATUS
  161. EfipSdOmap4FlushBlocks (
  162. EFI_BLOCK_IO_PROTOCOL *This
  163. );
  164. EFI_STATUS
  165. EfipSdOmap4ResetController (
  166. PEFI_SD_OMAP4_CONTEXT Device
  167. );
  168. //
  169. // -------------------------------------------------------------------- Globals
  170. //
  171. //
  172. // Define the private data template.
  173. //
  174. EFI_SD_OMAP4_CONTEXT EfiSdOmap4DiskTemplate = {
  175. EFI_SD_OMAP4_MAGIC,
  176. NULL,
  177. NULL,
  178. NULL,
  179. NULL,
  180. FALSE,
  181. 0,
  182. 0,
  183. {
  184. EFI_BLOCK_IO_PROTOCOL_REVISION3,
  185. NULL,
  186. EfipSdOmap4Reset,
  187. EfipSdOmap4ReadBlocks,
  188. EfipSdOmap4WriteBlocks,
  189. EfipSdOmap4FlushBlocks
  190. }
  191. };
  192. //
  193. // Define the device path template.
  194. //
  195. EFI_SD_OMAP4_DEVICE_PATH EfiSdOmap4DevicePathTemplate = {
  196. {
  197. {
  198. {
  199. HARDWARE_DEVICE_PATH,
  200. HW_VENDOR_DP,
  201. sizeof(EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH)
  202. },
  203. EFI_SD_OMAP4_BLOCK_IO_DEVICE_PATH_GUID,
  204. },
  205. 0xFFFFFFFF
  206. },
  207. {
  208. END_DEVICE_PATH_TYPE,
  209. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  210. END_DEVICE_PATH_LENGTH
  211. }
  212. };
  213. //
  214. // ------------------------------------------------------------------ Functions
  215. //
  216. EFI_STATUS
  217. EfipPandaEnumerateSd (
  218. VOID
  219. )
  220. /*++
  221. Routine Description:
  222. This routine enumerates the SD card on the PandaBoard.
  223. Arguments:
  224. None.
  225. Return Value:
  226. EFI status code.
  227. --*/
  228. {
  229. UINT64 BlockCount;
  230. UINT32 BlockSize;
  231. UINT32 ControllerBase;
  232. PEFI_SD_OMAP4_DEVICE_PATH DevicePath;
  233. PEFI_SD_OMAP4_CONTEXT Disk;
  234. EFI_SD_INITIALIZATION_BLOCK SdParameters;
  235. EFI_STATUS Status;
  236. ControllerBase = OMAP4430_HSMMC1_BASE;
  237. //
  238. // Allocate and initialize the context structure.
  239. //
  240. Status = EfiAllocatePool(EfiBootServicesData,
  241. sizeof(EFI_SD_OMAP4_CONTEXT),
  242. (VOID **)&Disk);
  243. if (EFI_ERROR(Status)) {
  244. return Status;
  245. }
  246. EfiCopyMem(Disk, &EfiSdOmap4DiskTemplate, sizeof(EFI_SD_OMAP4_CONTEXT));
  247. Disk->Handle = NULL;
  248. Disk->ControllerBase = (VOID *)(UINTN)ControllerBase;
  249. Disk->BlockIo.Media = &(Disk->Media);
  250. Disk->Media.RemovableMedia = TRUE;
  251. //
  252. // Create the device path.
  253. //
  254. Status = EfiAllocatePool(EfiBootServicesData,
  255. sizeof(EFI_SD_OMAP4_DEVICE_PATH),
  256. (VOID **)&DevicePath);
  257. if (EFI_ERROR(Status)) {
  258. goto PandaEnumerateSdEnd;
  259. }
  260. EfiCopyMem(DevicePath,
  261. &EfiSdOmap4DevicePathTemplate,
  262. sizeof(EFI_SD_OMAP4_DEVICE_PATH));
  263. DevicePath->Disk.ControllerBase = ControllerBase;
  264. Disk->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  265. //
  266. // Create the SD controller.
  267. //
  268. EfiSetMem(&SdParameters, sizeof(EFI_SD_INITIALIZATION_BLOCK), 0);
  269. SdParameters.StandardControllerBase =
  270. Disk->ControllerBase + SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET;
  271. SdParameters.Voltages = SD_VOLTAGE_29_30 | SD_VOLTAGE_30_31;
  272. SdParameters.HostCapabilities = SD_MODE_4BIT |
  273. SD_MODE_HIGH_SPEED |
  274. SD_MODE_AUTO_CMD12;
  275. SdParameters.FundamentalClock = SD_OMAP4_FUNDAMENTAL_CLOCK_SPEED;
  276. Disk->Controller = EfiSdCreateController(&SdParameters);
  277. if (Disk->Controller == NULL) {
  278. Status = EFI_OUT_OF_RESOURCES;
  279. goto PandaEnumerateSdEnd;
  280. }
  281. //
  282. // Perform some initialization to see if the card is there.
  283. //
  284. Status = EfipSdOmap4ResetController(Disk);
  285. if (!EFI_ERROR(Status)) {
  286. Status = EfiSdInitializeController(Disk->Controller, FALSE);
  287. if (!EFI_ERROR(Status)) {
  288. Status = EfiSdGetMediaParameters(Disk->Controller,
  289. &BlockCount,
  290. &BlockSize);
  291. if (!EFI_ERROR(Status)) {
  292. Disk->MediaPresent = TRUE;
  293. Disk->BlockSize = BlockSize;
  294. Disk->BlockCount = BlockCount;
  295. Disk->Media.MediaPresent = TRUE;
  296. Disk->Media.BlockSize = BlockSize;
  297. Disk->Media.LastBlock = BlockCount - 1;
  298. }
  299. }
  300. }
  301. Status = EfiInstallMultipleProtocolInterfaces(&(Disk->Handle),
  302. &EfiDevicePathProtocolGuid,
  303. Disk->DevicePath,
  304. &EfiBlockIoProtocolGuid,
  305. &(Disk->BlockIo),
  306. NULL);
  307. PandaEnumerateSdEnd:
  308. if (EFI_ERROR(Status)) {
  309. if (Disk != NULL) {
  310. if (Disk->DevicePath != NULL) {
  311. EfiFreePool(DevicePath);
  312. }
  313. if (Disk->Controller != NULL) {
  314. EfiSdDestroyController(Disk->Controller);
  315. }
  316. EfiFreePool(Disk);
  317. }
  318. }
  319. return Status;
  320. }
  321. //
  322. // --------------------------------------------------------- Internal Functions
  323. //
  324. EFIAPI
  325. EFI_STATUS
  326. EfipSdOmap4Reset (
  327. EFI_BLOCK_IO_PROTOCOL *This,
  328. BOOLEAN ExtendedVerification
  329. )
  330. /*++
  331. Routine Description:
  332. This routine resets the block device.
  333. Arguments:
  334. This - Supplies a pointer to the protocol instance.
  335. ExtendedVerification - Supplies a boolean indicating whether or not the
  336. driver should perform diagnostics on reset.
  337. Return Value:
  338. EFI_SUCCESS on success.
  339. EFI_DEVICE_ERROR if the device had an error and could not complete the
  340. request.
  341. --*/
  342. {
  343. PEFI_SD_OMAP4_CONTEXT Disk;
  344. EFI_STATUS Status;
  345. Disk = EFI_SD_OMAP4_FROM_THIS(This);
  346. Status = EfipSdOmap4ResetController(Disk);
  347. if (!EFI_ERROR(Status)) {
  348. Status = EfiSdInitializeController(Disk->Controller, FALSE);
  349. }
  350. if (EFI_ERROR(Status)) {
  351. Disk->MediaPresent = FALSE;
  352. Disk->Media.MediaPresent = FALSE;
  353. } else {
  354. Disk->Media.MediaId += 1;
  355. Disk->Media.MediaPresent = TRUE;
  356. Disk->MediaPresent = TRUE;
  357. }
  358. return Status;
  359. }
  360. EFIAPI
  361. EFI_STATUS
  362. EfipSdOmap4ReadBlocks (
  363. EFI_BLOCK_IO_PROTOCOL *This,
  364. UINT32 MediaId,
  365. EFI_LBA Lba,
  366. UINTN BufferSize,
  367. VOID *Buffer
  368. )
  369. /*++
  370. Routine Description:
  371. This routine performs a block I/O read from the device.
  372. Arguments:
  373. This - Supplies a pointer to the protocol instance.
  374. MediaId - Supplies the media identifier, which changes each time the media
  375. is replaced.
  376. Lba - Supplies the logical block address of the read.
  377. BufferSize - Supplies the size of the buffer in bytes.
  378. Buffer - Supplies the buffer where the read data will be returned.
  379. Return Value:
  380. EFI_SUCCESS on success.
  381. EFI_DEVICE_ERROR if the device had an error and could not complete the
  382. request.
  383. EFI_NO_MEDIA if there is no media in the device.
  384. EFI_MEDIA_CHANGED if the media ID does not match the current device.
  385. EFI_BAD_BUFFER_SIZE if the buffer was not a multiple of the device block
  386. size.
  387. EFI_INVALID_PARAMETER if the read request contains LBAs that are not valid,
  388. or the buffer is not properly aligned.
  389. --*/
  390. {
  391. PEFI_SD_OMAP4_CONTEXT Disk;
  392. EFI_STATUS Status;
  393. Disk = EFI_SD_OMAP4_FROM_THIS(This);
  394. if (MediaId != Disk->Media.MediaId) {
  395. return EFI_MEDIA_CHANGED;
  396. }
  397. if ((Disk->MediaPresent == FALSE) || (Disk->BlockSize == 0)) {
  398. return EFI_NO_MEDIA;
  399. }
  400. Status = EfiSdBlockIoPolled(Disk->Controller,
  401. Lba,
  402. BufferSize / Disk->BlockSize,
  403. Buffer,
  404. FALSE);
  405. return Status;
  406. }
  407. EFIAPI
  408. EFI_STATUS
  409. EfipSdOmap4WriteBlocks (
  410. EFI_BLOCK_IO_PROTOCOL *This,
  411. UINT32 MediaId,
  412. EFI_LBA Lba,
  413. UINTN BufferSize,
  414. VOID *Buffer
  415. )
  416. /*++
  417. Routine Description:
  418. This routine performs a block I/O write to the device.
  419. Arguments:
  420. This - Supplies a pointer to the protocol instance.
  421. MediaId - Supplies the media identifier, which changes each time the media
  422. is replaced.
  423. Lba - Supplies the logical block address of the write.
  424. BufferSize - Supplies the size of the buffer in bytes.
  425. Buffer - Supplies the buffer containing the data to write.
  426. Return Value:
  427. EFI_SUCCESS on success.
  428. EFI_WRITE_PROTECTED if the device cannot be written to.
  429. EFI_DEVICE_ERROR if the device had an error and could not complete the
  430. request.
  431. EFI_NO_MEDIA if there is no media in the device.
  432. EFI_MEDIA_CHANGED if the media ID does not match the current device.
  433. EFI_BAD_BUFFER_SIZE if the buffer was not a multiple of the device block
  434. size.
  435. EFI_INVALID_PARAMETER if the read request contains LBAs that are not valid,
  436. or the buffer is not properly aligned.
  437. --*/
  438. {
  439. PEFI_SD_OMAP4_CONTEXT Disk;
  440. EFI_STATUS Status;
  441. Disk = EFI_SD_OMAP4_FROM_THIS(This);
  442. if (MediaId != Disk->Media.MediaId) {
  443. return EFI_MEDIA_CHANGED;
  444. }
  445. if ((Disk->MediaPresent == FALSE) || (Disk->BlockSize == 0)) {
  446. return EFI_NO_MEDIA;
  447. }
  448. Status = EfiSdBlockIoPolled(Disk->Controller,
  449. Lba,
  450. BufferSize / Disk->BlockSize,
  451. Buffer,
  452. TRUE);
  453. return Status;
  454. }
  455. EFIAPI
  456. EFI_STATUS
  457. EfipSdOmap4FlushBlocks (
  458. EFI_BLOCK_IO_PROTOCOL *This
  459. )
  460. /*++
  461. Routine Description:
  462. This routine flushes the block device.
  463. Arguments:
  464. This - Supplies a pointer to the protocol instance.
  465. Return Value:
  466. EFI_SUCCESS on success.
  467. EFI_DEVICE_ERROR if the device had an error and could not complete the
  468. request.
  469. EFI_NO_MEDIA if there is no media in the device.
  470. --*/
  471. {
  472. return EFI_SUCCESS;
  473. }
  474. EFI_STATUS
  475. EfipSdOmap4ResetController (
  476. PEFI_SD_OMAP4_CONTEXT Device
  477. )
  478. /*++
  479. Routine Description:
  480. This routine resets the OMAP4 SD controller and card.
  481. Arguments:
  482. Device - Supplies a pointer to this SD OMAP4 device.
  483. Return Value:
  484. Status code.
  485. --*/
  486. {
  487. UINT32 ClockControl;
  488. UINT32 Divisor;
  489. UINT32 Register;
  490. EFI_STATUS Status;
  491. UINT64 Time;
  492. UINT64 Timeout;
  493. UINT32 Value;
  494. //
  495. // Perform a soft reset on the HSMMC part.
  496. //
  497. SD_OMAP4_WRITE_REGISTER(Device,
  498. SD_OMAP4_SYSCONFIG_REGISTER,
  499. SD_OMAP4_SYSCONFIG_SOFT_RESET);
  500. Status = EFI_TIMEOUT;
  501. Time = 0;
  502. Timeout = EFI_SD_OMAP4_TIMEOUT;
  503. do {
  504. if ((SD_OMAP4_READ_REGISTER(Device, SD_OMAP4_SYSSTATUS_REGISTER) &
  505. SD_OMAP4_SYSSTATUS_RESET_DONE) != 0) {
  506. Status = EFI_SUCCESS;
  507. break;
  508. }
  509. EfiStall(50);
  510. Time += 50;
  511. } while (Time <= Timeout);
  512. if (EFI_ERROR(Status)) {
  513. return Status;
  514. }
  515. //
  516. // Perform a reset on the SD controller.
  517. //
  518. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterClockControl;
  519. Value = SD_OMAP4_READ_REGISTER(Device, Register);
  520. Value |= SD_CLOCK_CONTROL_RESET_ALL;
  521. Status = EFI_TIMEOUT;
  522. Time = 0;
  523. Timeout = EFI_SD_OMAP4_TIMEOUT;
  524. do {
  525. if ((SD_OMAP4_READ_REGISTER(Device, Register) &
  526. SD_CLOCK_CONTROL_RESET_ALL) == 0) {
  527. Status = EFI_SUCCESS;
  528. break;
  529. }
  530. EfiStall(50);
  531. Time += 50;
  532. } while (Time <= Timeout);
  533. if (EFI_ERROR(Status)) {
  534. return Status;
  535. }
  536. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET +
  537. SdRegisterInterruptStatus;
  538. SD_OMAP4_WRITE_REGISTER(Device, Register, 0xFFFFFFFF);
  539. //
  540. // Set up the host control register for 3 Volts.
  541. //
  542. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterHostControl;
  543. Value = SD_HOST_CONTROL_POWER_3V0;
  544. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  545. //
  546. // Add the 3.0V and 1.8V capabilities to the capability register.
  547. //
  548. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterCapabilities;
  549. Value = SD_OMAP4_READ_REGISTER(Device, Register);
  550. Value |= SD_CAPABILITY_VOLTAGE_3V0 | SD_CAPABILITY_VOLTAGE_1V8;
  551. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  552. //
  553. // Initialize the HSMMC control register.
  554. //
  555. Register = SD_OMAP4_CON_REGISTER;
  556. Value = SD_OMAP4_READ_REGISTER(Device, Register) &
  557. SD_OMAP4_CON_DEBOUNCE_MASK;
  558. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  559. //
  560. // Set up the clock control register for 400kHz in preparation for sending
  561. // CMD0 with INIT held.
  562. //
  563. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterClockControl;
  564. ClockControl = SD_CLOCK_CONTROL_DEFAULT_TIMEOUT <<
  565. SD_CLOCK_CONTROL_TIMEOUT_SHIFT;
  566. SD_OMAP4_WRITE_REGISTER(Device, Register, ClockControl);
  567. Divisor = SD_OMAP4_INITIAL_DIVISOR;
  568. ClockControl |= (Divisor & SD_CLOCK_CONTROL_DIVISOR_MASK) <<
  569. SD_CLOCK_CONTROL_DIVISOR_SHIFT;
  570. ClockControl |= (Divisor & SD_CLOCK_CONTROL_DIVISOR_HIGH_MASK) >>
  571. SD_CLOCK_CONTROL_DIVISOR_HIGH_SHIFT;
  572. ClockControl |= SD_CLOCK_CONTROL_INTERNAL_CLOCK_ENABLE;
  573. SD_OMAP4_WRITE_REGISTER(Device, Register, ClockControl);
  574. Status = EFI_TIMEOUT;
  575. Time = 0;
  576. Timeout = EFI_SD_OMAP4_TIMEOUT;
  577. do {
  578. Value = SD_OMAP4_READ_REGISTER(Device, Register);
  579. if ((Value & SD_CLOCK_CONTROL_CLOCK_STABLE) != 0) {
  580. Status = EFI_SUCCESS;
  581. break;
  582. }
  583. EfiStall(50);
  584. Time += 50;
  585. } while (Time <= Timeout);
  586. if (EFI_ERROR(Status)) {
  587. return Status;
  588. }
  589. ClockControl |= SD_CLOCK_CONTROL_SD_CLOCK_ENABLE;
  590. SD_OMAP4_WRITE_REGISTER(Device, Register, ClockControl);
  591. SD_OMAP4_WRITE_REGISTER(Device, Register, ClockControl);
  592. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterHostControl;
  593. Value = SD_OMAP4_READ_REGISTER(Device, Register);
  594. Value |= SD_HOST_CONTROL_POWER_ENABLE;
  595. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  596. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET +
  597. SdRegisterInterruptStatusEnable;
  598. Value = SD_INTERRUPT_STATUS_ENABLE_DEFAULT_MASK;
  599. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  600. //
  601. // Reset the card by setting the init flag and issuing the card reset (go
  602. // idle, command 0) command.
  603. //
  604. Register = SD_OMAP4_CON_REGISTER;
  605. Value = SD_OMAP4_READ_REGISTER(Device, Register) | SD_OMAP4_CON_INIT |
  606. SD_OMAP4_CON_DMA_MASTER;
  607. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  608. //
  609. // Write a 0 to the command register to issue the command.
  610. //
  611. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET + SdRegisterCommand;
  612. SD_OMAP4_WRITE_REGISTER(Device, Register, 0);
  613. //
  614. // Wait for the command to complete.
  615. //
  616. Register = SD_OMAP4_CONTROLLER_SD_REGISTER_OFFSET +
  617. SdRegisterInterruptStatus;
  618. Status = EFI_TIMEOUT;
  619. Time = 0;
  620. Timeout = EFI_SD_OMAP4_TIMEOUT;
  621. do {
  622. Value = SD_OMAP4_READ_REGISTER(Device, Register);
  623. if (Value != 0) {
  624. if ((Value & SD_INTERRUPT_STATUS_COMMAND_COMPLETE) != 0) {
  625. Status = EFI_SUCCESS;
  626. } else if ((Value &
  627. SD_INTERRUPT_STATUS_COMMAND_TIMEOUT_ERROR) != 0) {
  628. Status = EFI_NO_MEDIA;
  629. } else {
  630. Status = EFI_DEVICE_ERROR;
  631. }
  632. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  633. break;
  634. }
  635. EfiStall(50);
  636. Time += 50;
  637. } while (Time <= Timeout);
  638. //
  639. // Disable the INIT line.
  640. //
  641. Register = SD_OMAP4_CON_REGISTER;
  642. Value = SD_OMAP4_READ_REGISTER(Device, Register) & (~SD_OMAP4_CON_INIT);
  643. SD_OMAP4_WRITE_REGISTER(Device, Register, Value);
  644. if (EFI_ERROR(Status)) {
  645. return Status;
  646. }
  647. Status = EFI_SUCCESS;
  648. return Status;
  649. }