sd.c 22 KB

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