sd.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. sd.c
  5. Abstract:
  6. This module implements RK3288 SD support in UEFI.
  7. Author:
  8. Chris Stevens 14-Jul-2015
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <uefifw.h>
  16. #include <dev/sddwc.h>
  17. #include <minoca/uefi/protocol/blockio.h>
  18. #include "veyronfw.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_RK32_FROM_THIS(_BlockIo) \
  27. (EFI_SD_RK32_CONTEXT *)((VOID *)(_BlockIo) - \
  28. ((VOID *)(&(((EFI_SD_RK32_CONTEXT *)0)->BlockIo))))
  29. //
  30. // These macros read and write SD controller registers.
  31. //
  32. #define SD_RK32_READ_REGISTER(_Device, _Register) \
  33. EfiReadRegister32((_Device)->ControllerBase + (_Register))
  34. #define SD_RK32_WRITE_REGISTER(_Device, _Register, _Value) \
  35. EfiWriteRegister32((_Device)->ControllerBase + (_Register), (_Value))
  36. //
  37. // ---------------------------------------------------------------- Definitions
  38. //
  39. #define EFI_SD_RK32_MAGIC 0x6B526453 // 'kRdS'
  40. #define EFI_SD_RK32_BLOCK_IO_DEVICE_PATH_GUID \
  41. { \
  42. 0xCF31FAC5, 0xC24E, 0x11D2, \
  43. {0x85, 0xF3, 0x00, 0xA0, 0xC9, 0x3E, 0xA7, 0x39} \
  44. }
  45. //
  46. // Define the amount of time to wait in microseconds for the controller to
  47. // respond.
  48. //
  49. #define EFI_SD_RK32_TIMEOUT 1000000
  50. //
  51. // Define the speed of the SD fundamental clock. This is based on the general
  52. // PLL, which is set up by the previous loader to be 594MHz.
  53. //
  54. #define EFI_SD_RK32_CLOCK_SPEED 594000000
  55. //
  56. // ------------------------------------------------------ Data Type Definitions
  57. //
  58. /*++
  59. Structure Description:
  60. This structure describes the SD RK32 device context.
  61. Members:
  62. Magic - Stores the magic constand EFI_SD_RK32_MAGIC.
  63. Handle - Stores the handle to the block I/O device.
  64. DevicePath - Stores a pointer to the device path.
  65. Controller - Stores an pointer to the controller structure.
  66. ControllerBase - Stores a pointer to the virtual address of the HSMMC
  67. registers.
  68. FundamentalClock - Stores the fundamental clock for the HSMMC device in
  69. Hertz.
  70. MediaPresent - Stores a boolean indicating whether or not there is a card
  71. in the slot.
  72. BlockSize - Stores the cached block size of the media.
  73. BlockCount - Stores the cached block count of the media.
  74. BlockIo - Stores the block I/O protocol.
  75. Media - Stores the block I/O media information.
  76. --*/
  77. typedef struct _EFI_SD_RK32_CONTEXT {
  78. UINT32 Magic;
  79. EFI_HANDLE Handle;
  80. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  81. PEFI_SD_DWC_CONTROLLER Controller;
  82. VOID *ControllerBase;
  83. UINT32 FundamentalClock;
  84. BOOLEAN MediaPresent;
  85. UINT32 BlockSize;
  86. UINT64 BlockCount;
  87. EFI_BLOCK_IO_PROTOCOL BlockIo;
  88. EFI_BLOCK_IO_MEDIA Media;
  89. } EFI_SD_RK32_CONTEXT, *PEFI_SD_RK32_CONTEXT;
  90. /*++
  91. Structure Description:
  92. This structure defines the SD RK32 block I/O device path.
  93. Members:
  94. DevicePath - Stores the standard vendor-specific device path.
  95. ControllerBase - Stores the controller number.
  96. --*/
  97. typedef struct _EFI_SD_RK32_BLOCK_IO_DEVICE_PATH {
  98. VENDOR_DEVICE_PATH DevicePath;
  99. UINT32 ControllerBase;
  100. } EFI_SD_RK32_BLOCK_IO_DEVICE_PATH, *PEFI_SD_RK32_BLOCK_IO_DEVICE_PATH;
  101. /*++
  102. Structure Description:
  103. This structure defines the RK32 SD block I/O device path.
  104. Members:
  105. Disk - Stores the disk device path node.
  106. End - Stores the end device path node.
  107. --*/
  108. typedef struct _EFI_SD_RK32_DEVICE_PATH {
  109. EFI_SD_RK32_BLOCK_IO_DEVICE_PATH Disk;
  110. EFI_DEVICE_PATH_PROTOCOL End;
  111. } PACKED EFI_SD_RK32_DEVICE_PATH, *PEFI_SD_RK32_DEVICE_PATH;
  112. //
  113. // ----------------------------------------------- Internal Function Prototypes
  114. //
  115. EFI_STATUS
  116. EfipVeyronEnumerateSdController (
  117. UINT32 ControllerBase,
  118. BOOLEAN RemovableMedia
  119. );
  120. EFIAPI
  121. EFI_STATUS
  122. EfipSdRk32Reset (
  123. EFI_BLOCK_IO_PROTOCOL *This,
  124. BOOLEAN ExtendedVerification
  125. );
  126. EFIAPI
  127. EFI_STATUS
  128. EfipSdRk32ReadBlocks (
  129. EFI_BLOCK_IO_PROTOCOL *This,
  130. UINT32 MediaId,
  131. EFI_LBA Lba,
  132. UINTN BufferSize,
  133. VOID *Buffer
  134. );
  135. EFIAPI
  136. EFI_STATUS
  137. EfipSdRk32WriteBlocks (
  138. EFI_BLOCK_IO_PROTOCOL *This,
  139. UINT32 MediaId,
  140. EFI_LBA Lba,
  141. UINTN BufferSize,
  142. VOID *Buffer
  143. );
  144. EFIAPI
  145. EFI_STATUS
  146. EfipSdRk32FlushBlocks (
  147. EFI_BLOCK_IO_PROTOCOL *This
  148. );
  149. EFI_STATUS
  150. EfipSdRk32GetFundamentalClock (
  151. PEFI_SD_RK32_CONTEXT Device,
  152. UINT32 *FundamentalClock
  153. );
  154. EFI_STATUS
  155. EfipSdRk32HardResetController (
  156. PEFI_SD_RK32_CONTEXT Device
  157. );
  158. EFI_STATUS
  159. EfipSdRk32GetSetClockSpeed (
  160. PEFI_SD_CONTROLLER Controller,
  161. VOID *Context,
  162. UINT32 *ClockSpeed,
  163. BOOLEAN Set
  164. );
  165. EFI_STATUS
  166. EfipSdRk32SetClockSpeed (
  167. PEFI_SD_RK32_CONTEXT Disk,
  168. UINT32 ClockSpeed
  169. );
  170. //
  171. // -------------------------------------------------------------------- Globals
  172. //
  173. //
  174. // Define the private data template.
  175. //
  176. EFI_SD_RK32_CONTEXT EfiSdRk32DiskTemplate = {
  177. EFI_SD_RK32_MAGIC,
  178. NULL,
  179. NULL,
  180. NULL,
  181. NULL,
  182. 0,
  183. FALSE,
  184. 0,
  185. 0,
  186. {
  187. EFI_BLOCK_IO_PROTOCOL_REVISION3,
  188. NULL,
  189. EfipSdRk32Reset,
  190. EfipSdRk32ReadBlocks,
  191. EfipSdRk32WriteBlocks,
  192. EfipSdRk32FlushBlocks
  193. }
  194. };
  195. //
  196. // Define the device path template.
  197. //
  198. EFI_SD_RK32_DEVICE_PATH EfiSdRk32DevicePathTemplate = {
  199. {
  200. {
  201. {
  202. HARDWARE_DEVICE_PATH,
  203. HW_VENDOR_DP,
  204. sizeof(EFI_SD_RK32_BLOCK_IO_DEVICE_PATH)
  205. },
  206. EFI_SD_RK32_BLOCK_IO_DEVICE_PATH_GUID,
  207. },
  208. 0xFFFFFFFF
  209. },
  210. {
  211. END_DEVICE_PATH_TYPE,
  212. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  213. END_DEVICE_PATH_LENGTH
  214. }
  215. };
  216. SD_FUNCTION_TABLE EfiSdRk32FunctionTable = {
  217. NULL,
  218. NULL,
  219. NULL,
  220. NULL,
  221. EfipSdRk32GetSetClockSpeed
  222. };
  223. //
  224. // ------------------------------------------------------------------ Functions
  225. //
  226. EFI_STATUS
  227. EfipVeyronEnumerateSd (
  228. VOID
  229. )
  230. /*++
  231. Routine Description:
  232. This routine enumerates the SD card on the Veyron SoC.
  233. Arguments:
  234. None.
  235. Return Value:
  236. EFI status code.
  237. --*/
  238. {
  239. EFI_STATUS Status;
  240. Status = EfipVeyronEnumerateSdController(RK32_SD_BASE, TRUE);
  241. if (EFI_ERROR(Status)) {
  242. return Status;
  243. }
  244. //
  245. // Only enumerate eMMC if the firmware was not loaded from SD. Enumerating
  246. // eMMC will cause NV variables to be loaded from there, which will specify
  247. // a BootOrder of eMMC first. The user likely didn't go to all the trouble
  248. // of booting via SD only to have this firmware launch the eMMC boot option.
  249. //
  250. if (EfiBootedViaSd == FALSE) {
  251. Status = EfipVeyronEnumerateSdController(RK32_EMMC_BASE, FALSE);
  252. }
  253. return Status;
  254. }
  255. //
  256. // --------------------------------------------------------- Internal Functions
  257. //
  258. EFI_STATUS
  259. EfipVeyronEnumerateSdController (
  260. UINT32 ControllerBase,
  261. BOOLEAN RemovableMedia
  262. )
  263. /*++
  264. Routine Description:
  265. This routine enumerates the an SD or eMMC controller on the Veyron.
  266. Arguments:
  267. ControllerBase - Supplies the physical address of the controller to
  268. enumerate.
  269. RemovableMedia - Supplies a boolean whether or not the controller is
  270. connected to removable media.
  271. Return Value:
  272. EFI status code.
  273. --*/
  274. {
  275. UINT64 BlockCount;
  276. UINT32 BlockSize;
  277. PEFI_SD_RK32_DEVICE_PATH DevicePath;
  278. PEFI_SD_RK32_CONTEXT Disk;
  279. EFI_SD_DWC_INITIALIZATION_BLOCK SdDwcParameters;
  280. EFI_STATUS Status;
  281. //
  282. // Allocate and initialize the context structure.
  283. //
  284. Status = EfiAllocatePool(EfiBootServicesData,
  285. sizeof(EFI_SD_RK32_CONTEXT),
  286. (VOID **)&Disk);
  287. if (EFI_ERROR(Status)) {
  288. return Status;
  289. }
  290. EfiCopyMem(Disk, &EfiSdRk32DiskTemplate, sizeof(EFI_SD_RK32_CONTEXT));
  291. Disk->Handle = NULL;
  292. Disk->ControllerBase = (VOID *)(UINTN)ControllerBase;
  293. Disk->BlockIo.Media = &(Disk->Media);
  294. Disk->Media.RemovableMedia = RemovableMedia;
  295. //
  296. // Create the device path.
  297. //
  298. Status = EfiAllocatePool(EfiBootServicesData,
  299. sizeof(EFI_SD_RK32_DEVICE_PATH),
  300. (VOID **)&DevicePath);
  301. if (EFI_ERROR(Status)) {
  302. goto VeyronEnumerateSdControllerEnd;
  303. }
  304. EfiCopyMem(DevicePath,
  305. &EfiSdRk32DevicePathTemplate,
  306. sizeof(EFI_SD_RK32_DEVICE_PATH));
  307. DevicePath->Disk.ControllerBase = ControllerBase;
  308. DevicePath->Disk.DevicePath.Guid.Data4[0] += RemovableMedia;
  309. Disk->DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  310. Status = EfipSdRk32GetFundamentalClock(Disk, &(Disk->FundamentalClock));
  311. if (EFI_ERROR(Status)) {
  312. goto VeyronEnumerateSdControllerEnd;
  313. }
  314. //
  315. // Create the SD controller.
  316. //
  317. EfiSetMem(&SdDwcParameters, sizeof(EFI_SD_DWC_INITIALIZATION_BLOCK), 0);
  318. SdDwcParameters.ControllerBase = Disk->ControllerBase;
  319. SdDwcParameters.Voltages = SD_VOLTAGE_32_33 | SD_VOLTAGE_33_34;
  320. SdDwcParameters.HostCapabilities = SD_MODE_4BIT |
  321. SD_MODE_HIGH_SPEED |
  322. SD_MODE_AUTO_CMD12;
  323. SdDwcParameters.FundamentalClock = Disk->FundamentalClock;
  324. SdDwcParameters.OverrideFunctionTable = &EfiSdRk32FunctionTable;
  325. SdDwcParameters.OverrideContext = Disk;
  326. Disk->Controller = EfiSdDwcCreateController(&SdDwcParameters);
  327. if (Disk->Controller == NULL) {
  328. Status = EFI_OUT_OF_RESOURCES;
  329. goto VeyronEnumerateSdControllerEnd;
  330. }
  331. //
  332. // Reset the controller.
  333. //
  334. Status = EfipSdRk32HardResetController(Disk);
  335. if (EFI_ERROR(Status)) {
  336. goto VeyronEnumerateSdControllerEnd;
  337. }
  338. //
  339. // Perform some initialization to see if the card is there.
  340. //
  341. Status = EfiSdDwcInitializeController(Disk->Controller, FALSE);
  342. if (!EFI_ERROR(Status)) {
  343. Status = EfiSdDwcGetMediaParameters(Disk->Controller,
  344. &BlockCount,
  345. &BlockSize);
  346. if (!EFI_ERROR(Status)) {
  347. Disk->MediaPresent = TRUE;
  348. Disk->BlockSize = BlockSize;
  349. Disk->BlockCount = BlockCount;
  350. Disk->Media.MediaPresent = TRUE;
  351. Disk->Media.BlockSize = BlockSize;
  352. Disk->Media.LastBlock = BlockCount - 1;
  353. }
  354. }
  355. Status = EfiInstallMultipleProtocolInterfaces(&(Disk->Handle),
  356. &EfiDevicePathProtocolGuid,
  357. Disk->DevicePath,
  358. &EfiBlockIoProtocolGuid,
  359. &(Disk->BlockIo),
  360. NULL);
  361. VeyronEnumerateSdControllerEnd:
  362. if (EFI_ERROR(Status)) {
  363. if (Disk != NULL) {
  364. if (Disk->DevicePath != NULL) {
  365. EfiFreePool(DevicePath);
  366. }
  367. if (Disk->Controller != NULL) {
  368. EfiSdDwcDestroyController(Disk->Controller);
  369. }
  370. EfiFreePool(Disk);
  371. }
  372. }
  373. return Status;
  374. }
  375. EFIAPI
  376. EFI_STATUS
  377. EfipSdRk32Reset (
  378. EFI_BLOCK_IO_PROTOCOL *This,
  379. BOOLEAN ExtendedVerification
  380. )
  381. /*++
  382. Routine Description:
  383. This routine resets the block device.
  384. Arguments:
  385. This - Supplies a pointer to the protocol instance.
  386. ExtendedVerification - Supplies a boolean indicating whether or not the
  387. driver should perform diagnostics on reset.
  388. Return Value:
  389. EFI_SUCCESS on success.
  390. EFI_DEVICE_ERROR if the device had an error and could not complete the
  391. request.
  392. --*/
  393. {
  394. PEFI_SD_RK32_CONTEXT Disk;
  395. EFI_STATUS Status;
  396. Disk = EFI_SD_RK32_FROM_THIS(This);
  397. Status = EfiSdDwcInitializeController(Disk->Controller, TRUE);
  398. if (EFI_ERROR(Status)) {
  399. Disk->MediaPresent = FALSE;
  400. Disk->Media.MediaPresent = FALSE;
  401. } else {
  402. Disk->Media.MediaId += 1;
  403. Disk->Media.MediaPresent = TRUE;
  404. Disk->MediaPresent = TRUE;
  405. }
  406. return Status;
  407. }
  408. EFIAPI
  409. EFI_STATUS
  410. EfipSdRk32ReadBlocks (
  411. EFI_BLOCK_IO_PROTOCOL *This,
  412. UINT32 MediaId,
  413. EFI_LBA Lba,
  414. UINTN BufferSize,
  415. VOID *Buffer
  416. )
  417. /*++
  418. Routine Description:
  419. This routine performs a block I/O read from the device.
  420. Arguments:
  421. This - Supplies a pointer to the protocol instance.
  422. MediaId - Supplies the media identifier, which changes each time the media
  423. is replaced.
  424. Lba - Supplies the logical block address of the read.
  425. BufferSize - Supplies the size of the buffer in bytes.
  426. Buffer - Supplies the buffer where the read data will be returned.
  427. Return Value:
  428. EFI_SUCCESS on success.
  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_RK32_CONTEXT Disk;
  440. EFI_STATUS Status;
  441. Disk = EFI_SD_RK32_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 = EfiSdDwcBlockIoPolled(Disk->Controller,
  449. Lba,
  450. BufferSize / Disk->BlockSize,
  451. Buffer,
  452. FALSE);
  453. return Status;
  454. }
  455. EFIAPI
  456. EFI_STATUS
  457. EfipSdRk32WriteBlocks (
  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_RK32_CONTEXT Disk;
  488. EFI_STATUS Status;
  489. Disk = EFI_SD_RK32_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. Status = EfiSdDwcBlockIoPolled(Disk->Controller,
  497. Lba,
  498. BufferSize / Disk->BlockSize,
  499. Buffer,
  500. TRUE);
  501. return Status;
  502. }
  503. EFIAPI
  504. EFI_STATUS
  505. EfipSdRk32FlushBlocks (
  506. EFI_BLOCK_IO_PROTOCOL *This
  507. )
  508. /*++
  509. Routine Description:
  510. This routine flushes the block device.
  511. Arguments:
  512. This - Supplies a pointer to the protocol instance.
  513. Return Value:
  514. EFI_SUCCESS on success.
  515. EFI_DEVICE_ERROR if the device had an error and could not complete the
  516. request.
  517. EFI_NO_MEDIA if there is no media in the device.
  518. --*/
  519. {
  520. return EFI_SUCCESS;
  521. }
  522. EFI_STATUS
  523. EfipSdRk32GetFundamentalClock (
  524. PEFI_SD_RK32_CONTEXT Device,
  525. UINT32 *FundamentalClock
  526. )
  527. /*++
  528. Routine Description:
  529. This routine gets the fundamental clock frequency to use for the SD
  530. controller.
  531. Arguments:
  532. Device - Supplies a pointer to this SD RK32 device.
  533. FundamentalClock - Supplies a pointer that receives the frequency of the
  534. fundamental clock, in Hertz.
  535. Return Value:
  536. Status code.
  537. --*/
  538. {
  539. *FundamentalClock = EFI_SD_RK32_CLOCK_SPEED;
  540. return EFI_SUCCESS;
  541. }
  542. EFI_STATUS
  543. EfipSdRk32HardResetController (
  544. PEFI_SD_RK32_CONTEXT Device
  545. )
  546. /*++
  547. Routine Description:
  548. This routine resets the DesignWare SD controller and card.
  549. Arguments:
  550. Device - Supplies a pointer to this SD RK32 device.
  551. Return Value:
  552. Status code.
  553. --*/
  554. {
  555. UINT32 ResetMask;
  556. EFI_STATUS Status;
  557. UINT64 Time;
  558. UINT64 Timeout;
  559. UINT32 Value;
  560. //
  561. // First perform a hardware reset on the SD card.
  562. //
  563. SD_RK32_WRITE_REGISTER(Device, SdDwcPower, SD_DWC_POWER_DISABLE);
  564. SD_RK32_WRITE_REGISTER(Device, SdDwcResetN, SD_DWC_RESET_ENABLE);
  565. EfiStall(5000);
  566. SD_RK32_WRITE_REGISTER(Device, SdDwcPower, SD_DWC_POWER_ENABLE);
  567. SD_RK32_WRITE_REGISTER(Device, SdDwcResetN, 0);
  568. EfiStall(1000);
  569. //
  570. // Perform a complete controller reset and wait for it to complete.
  571. //
  572. ResetMask = SD_DWC_CONTROL_FIFO_RESET |
  573. SD_DWC_CONTROL_CONTROLLER_RESET;
  574. SD_RK32_WRITE_REGISTER(Device, SdDwcControl, ResetMask);
  575. Status = EFI_TIMEOUT;
  576. Timeout = EFI_SD_RK32_TIMEOUT;
  577. Time = 0;
  578. do {
  579. Value = SD_RK32_READ_REGISTER(Device, SdDwcControl);
  580. if ((Value & ResetMask) == 0) {
  581. Status = EFI_SUCCESS;
  582. break;
  583. }
  584. EfiStall(50);
  585. Time += 50;
  586. } while (Time <= Timeout);
  587. if (EFI_ERROR(Status)) {
  588. return Status;
  589. }
  590. //
  591. // Clear interrupts.
  592. //
  593. SD_RK32_WRITE_REGISTER(Device,
  594. SdDwcInterruptStatus,
  595. SD_DWC_INTERRUPT_STATUS_ALL_MASK);
  596. //
  597. // Set 3v3 volts in the UHS register.
  598. //
  599. SD_RK32_WRITE_REGISTER(Device, SdDwcUhs, SD_DWC_UHS_VOLTAGE_3V3);
  600. //
  601. // Set the clock to 400kHz in preparation for sending CMD0 with the
  602. // initialization bit set.
  603. //
  604. Status = EfipSdRk32SetClockSpeed(Device, 400000);
  605. if (EFI_ERROR(Status)) {
  606. return Status;
  607. }
  608. //
  609. // Reset the card by sending the CMD0 reset command with the initialization
  610. // bit set.
  611. //
  612. Value = SD_DWC_COMMAND_START |
  613. SD_DWC_COMMAND_USE_HOLD_REGISTER |
  614. SD_DWC_COMMAND_SEND_INITIALIZATION;
  615. SD_RK32_WRITE_REGISTER(Device, SdDwcCommand, Value);
  616. //
  617. // Wait for the command to complete.
  618. //
  619. Status = EFI_TIMEOUT;
  620. Timeout = EFI_SD_RK32_TIMEOUT;
  621. Time = 0;
  622. do {
  623. Value = SD_RK32_READ_REGISTER(Device, SdDwcCommand);
  624. if ((Value & SD_DWC_COMMAND_START) == 0) {
  625. Status = EFI_SUCCESS;
  626. break;
  627. }
  628. EfiStall(50);
  629. Time += 50;
  630. } while (Time <= Timeout);
  631. if (EFI_ERROR(Status)) {
  632. return Status;
  633. }
  634. Timeout = EFI_SD_RK32_TIMEOUT;
  635. Time = 0;
  636. Status = EFI_TIMEOUT;
  637. do {
  638. Value = SD_RK32_READ_REGISTER(Device, SdDwcInterruptStatus);
  639. if (Value != 0) {
  640. if ((Value & SD_DWC_INTERRUPT_STATUS_COMMAND_DONE) != 0) {
  641. Status = EFI_SUCCESS;
  642. } else if ((Value &
  643. SD_DWC_INTERRUPT_STATUS_ERROR_RESPONSE_TIMEOUT) != 0) {
  644. Status = EFI_NO_MEDIA;
  645. } else {
  646. Status = EFI_DEVICE_ERROR;
  647. }
  648. SD_RK32_WRITE_REGISTER(Device, SdDwcInterruptStatus, Value);
  649. break;
  650. }
  651. EfiStall(50);
  652. Time += 50;
  653. } while (Time <= Timeout);
  654. if (EFI_ERROR(Status)) {
  655. return Status;
  656. }
  657. return EFI_SUCCESS;
  658. }
  659. EFI_STATUS
  660. EfipSdRk32GetSetClockSpeed (
  661. PEFI_SD_CONTROLLER Controller,
  662. VOID *Context,
  663. UINT32 *ClockSpeed,
  664. BOOLEAN Set
  665. )
  666. /*++
  667. Routine Description:
  668. This routine gets or sets the controller's clock speed.
  669. Arguments:
  670. Controller - Supplies a pointer to the controller.
  671. Context - Supplies a context pointer passed to the SD/MMC library upon
  672. creation of the controller.
  673. ClockSpeed - Supplies a pointer that receives the current clock speed on
  674. get and contains the desired clock speed on set.
  675. Set - Supplies a boolean indicating whether the bus width should be queried
  676. or set.
  677. Return Value:
  678. Status code.
  679. --*/
  680. {
  681. PEFI_SD_RK32_CONTEXT Disk;
  682. Disk = Context;
  683. //
  684. // Getting the clock speed is not implemented as the divisor math might not
  685. // work out precisely in reverse.
  686. //
  687. if (Set == FALSE) {
  688. return EFI_UNSUPPORTED;
  689. }
  690. return EfipSdRk32SetClockSpeed(Disk, *ClockSpeed);
  691. }
  692. EFI_STATUS
  693. EfipSdRk32SetClockSpeed (
  694. PEFI_SD_RK32_CONTEXT Disk,
  695. UINT32 ClockSpeed
  696. )
  697. /*++
  698. Routine Description:
  699. This routine sets the controller's clock speed.
  700. Arguments:
  701. Disk - Supplies a pointer to the disk context.
  702. ClockSpeed - Supplies the desired clock speed in Hertz.
  703. Return Value:
  704. Status code.
  705. --*/
  706. {
  707. UINT32 Divisor;
  708. PEFI_SD_DWC_CONTROLLER DwcController;
  709. UINT32 InputClock;
  710. EFI_STATUS Status;
  711. UINT64 Time;
  712. UINT64 Timeout;
  713. UINT32 Value;
  714. DwcController = Disk->Controller;
  715. if (DwcController->FundamentalClock == 0) {
  716. return EFI_INVALID_PARAMETER;
  717. }
  718. //
  719. // Wait for the card to not be busy.
  720. //
  721. Timeout = EFI_SD_DWC_CONTROLLER_TIMEOUT;
  722. Time = 0;
  723. Status = EFI_TIMEOUT;
  724. do {
  725. Value = SD_DWC_READ_REGISTER(DwcController, SdDwcStatus);
  726. if ((Value & SD_DWC_STATUS_DATA_BUSY) == 0) {
  727. Status = EFI_SUCCESS;
  728. break;
  729. }
  730. EfiStall(50);
  731. Time += 50;
  732. } while (Time <= Timeout);
  733. if (EFI_ERROR(Status)) {
  734. return Status;
  735. }
  736. //
  737. // Disable all clocks.
  738. //
  739. SD_DWC_WRITE_REGISTER(DwcController, SdDwcClockEnable, 0);
  740. //
  741. // Send the command to indicate that the clock enable register is being
  742. // updated.
  743. //
  744. Value = SD_DWC_COMMAND_START |
  745. SD_DWC_COMMAND_UPDATE_CLOCK_REGISTERS |
  746. SD_DWC_COMMAND_WAIT_PREVIOUS_DATA_COMPLETE;
  747. SD_DWC_WRITE_REGISTER(DwcController, SdDwcCommand, Value);
  748. Timeout = EFI_SD_DWC_CONTROLLER_TIMEOUT;
  749. Time = 0;
  750. Status = EFI_TIMEOUT;
  751. do {
  752. Value = SD_DWC_READ_REGISTER(DwcController, SdDwcCommand);
  753. if ((Value & SD_DWC_COMMAND_START) == 0) {
  754. Status = EFI_SUCCESS;
  755. break;
  756. }
  757. EfiStall(50);
  758. Time += 50;
  759. } while (Time <= Timeout);
  760. if (EFI_ERROR(Status)) {
  761. return Status;
  762. }
  763. //
  764. // Use the 24MHz clock if a really slow speed is desired.
  765. //
  766. InputClock = DwcController->FundamentalClock;
  767. if (ClockSpeed < (InputClock / (RK32_CRU_MAX_MMC_DIVISOR + 1))) {
  768. //
  769. // Select the raw 24MHz source, and set the DesignWare divider to 1 to
  770. // divide by 2.
  771. //
  772. InputClock = RK32_SDMMC_FREQUENCY_24MHZ / 2;
  773. SD_DWC_WRITE_REGISTER(DwcController, SdDwcClockDivider, 1);
  774. Value = (RK32_CRU_CLOCK_SELECT_24MHZ <<
  775. RK32_CRU_CLOCK_SELECT_CLOCK_SHIFT);
  776. //
  777. // Use the general PLL.
  778. //
  779. } else {
  780. SD_DWC_WRITE_REGISTER(DwcController, SdDwcClockDivider, 0);
  781. Value = (RK32_CRU_CLOCK_SELECT_GENERAL_PLL <<
  782. RK32_CRU_CLOCK_SELECT_CLOCK_SHIFT);
  783. }
  784. Divisor = InputClock / ClockSpeed;
  785. if (InputClock / Divisor > ClockSpeed) {
  786. Divisor += 1;
  787. }
  788. //
  789. // Bits 16 and up must be set for the write to take effect. This is also
  790. // why read-modify-write is not needed.
  791. //
  792. Value |= (RK32_CRU_CLOCK_SELECT_CLOCK_MASK |
  793. RK32_CRU_CLOCK_SELECT_DIVIDER_MASK) <<
  794. RK32_CRU_CLOCK_SELECT_PROTECT_SHIFT;
  795. Value |= Divisor;
  796. if (Disk->ControllerBase == (VOID *)RK32_SD_BASE) {
  797. RK32_WRITE_CRU(Rk32CruClockSelect11, Value);
  798. } else if (Disk->ControllerBase == (VOID *)RK32_EMMC_BASE) {
  799. Value <<= RK32_CRU_CLOCK_SELECT12_EMMC_DIVIDER_SHIFT;
  800. RK32_WRITE_CRU(Rk32CruClockSelect12, Value);
  801. } else {
  802. return EFI_UNSUPPORTED;
  803. }
  804. SD_DWC_WRITE_REGISTER(DwcController,
  805. SdDwcClockSource,
  806. SD_DWC_CLOCK_SOURCE_DIVIDER_0);
  807. //
  808. // Send the command to indicate that the clock source and divider are is
  809. // being updated.
  810. //
  811. Value = SD_DWC_COMMAND_START |
  812. SD_DWC_COMMAND_UPDATE_CLOCK_REGISTERS |
  813. SD_DWC_COMMAND_WAIT_PREVIOUS_DATA_COMPLETE;
  814. SD_DWC_WRITE_REGISTER(DwcController, SdDwcCommand, Value);
  815. Timeout = EFI_SD_DWC_CONTROLLER_TIMEOUT;
  816. Time = 0;
  817. Status = EFI_TIMEOUT;
  818. do {
  819. Value = SD_DWC_READ_REGISTER(DwcController, SdDwcCommand);
  820. if ((Value & SD_DWC_COMMAND_START) == 0) {
  821. Status = EFI_SUCCESS;
  822. break;
  823. }
  824. EfiStall(50);
  825. Time += 50;
  826. } while (Time <= Timeout);
  827. if (EFI_ERROR(Status)) {
  828. return Status;
  829. }
  830. //
  831. // Enable the clocks in lower power mode.
  832. //
  833. SD_DWC_WRITE_REGISTER(DwcController,
  834. SdDwcClockEnable,
  835. (SD_DWC_CLOCK_ENABLE_LOW_POWER |
  836. SD_DWC_CLOCK_ENABLE_ON));
  837. //
  838. // Send the command to indicate that the clock is enable register being
  839. // updated.
  840. //
  841. Value = SD_DWC_COMMAND_START |
  842. SD_DWC_COMMAND_UPDATE_CLOCK_REGISTERS |
  843. SD_DWC_COMMAND_WAIT_PREVIOUS_DATA_COMPLETE;
  844. SD_DWC_WRITE_REGISTER(DwcController, SdDwcCommand, Value);
  845. Timeout = EFI_SD_DWC_CONTROLLER_TIMEOUT;
  846. Time = 0;
  847. Status = EFI_TIMEOUT;
  848. do {
  849. Value = SD_DWC_READ_REGISTER(DwcController, SdDwcCommand);
  850. if ((Value & SD_DWC_COMMAND_START) == 0) {
  851. Status = EFI_SUCCESS;
  852. break;
  853. }
  854. EfiStall(50);
  855. Time += 50;
  856. } while (Time <= Timeout);
  857. if (EFI_ERROR(Status)) {
  858. return Status;
  859. }
  860. return EFI_SUCCESS;
  861. }