sd.c 26 KB

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