sdstd.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All rights reserved.
  3. Module Name:
  4. sdstd.c
  5. Abstract:
  6. This module implements the library functionality for the standard SD/MMC
  7. device.
  8. Author:
  9. Chris Stevens 16-Jul-2015
  10. Environment:
  11. Firmware
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <uefifw.h>
  17. #include "sdp.h"
  18. //
  19. // --------------------------------------------------------------------- Macros
  20. //
  21. //
  22. // These macros read and write SD controller registers.
  23. //
  24. #define SD_READ_REGISTER(_Controller, _Register) \
  25. EfiReadRegister32((_Controller)->ControllerBase + (_Register))
  26. #define SD_WRITE_REGISTER(_Controller, _Register, _Value) \
  27. EfiWriteRegister32((_Controller)->ControllerBase + (_Register), (_Value))
  28. //
  29. // ---------------------------------------------------------------- Definitions
  30. //
  31. //
  32. // ------------------------------------------------------ Data Type Definitions
  33. //
  34. //
  35. // ----------------------------------------------- Internal Function Prototypes
  36. //
  37. EFI_STATUS
  38. EfipSdInitializeController (
  39. PEFI_SD_CONTROLLER Controller,
  40. VOID *Context,
  41. UINT32 Phase
  42. );
  43. EFI_STATUS
  44. EfipSdResetController (
  45. PEFI_SD_CONTROLLER Controller,
  46. VOID *Context,
  47. UINT32 Flags
  48. );
  49. EFI_STATUS
  50. EfipSdSendCommand (
  51. PEFI_SD_CONTROLLER Controller,
  52. VOID *Context,
  53. PSD_COMMAND Command
  54. );
  55. EFI_STATUS
  56. EfipSdGetSetBusWidth (
  57. PEFI_SD_CONTROLLER Controller,
  58. VOID *Context,
  59. UINT16 *BusWidth,
  60. BOOLEAN Set
  61. );
  62. EFI_STATUS
  63. EfipSdGetSetClockSpeed (
  64. PEFI_SD_CONTROLLER Controller,
  65. VOID *Context,
  66. UINT32 *ClockSpeed,
  67. BOOLEAN Set
  68. );
  69. EFI_STATUS
  70. EfipSdReadData (
  71. PEFI_SD_CONTROLLER Controller,
  72. VOID *Data,
  73. UINT32 Size
  74. );
  75. EFI_STATUS
  76. EfipSdWriteData (
  77. PEFI_SD_CONTROLLER Controller,
  78. VOID *Data,
  79. UINT32 Size
  80. );
  81. //
  82. // -------------------------------------------------------------------- Globals
  83. //
  84. SD_FUNCTION_TABLE EfiSdStdFunctionTable = {
  85. EfipSdInitializeController,
  86. EfipSdResetController,
  87. EfipSdSendCommand,
  88. EfipSdGetSetBusWidth,
  89. EfipSdGetSetClockSpeed
  90. };
  91. //
  92. // ------------------------------------------------------------------ Functions
  93. //
  94. EFI_STATUS
  95. EfipSdInitializeController (
  96. PEFI_SD_CONTROLLER Controller,
  97. VOID *Context,
  98. UINT32 Phase
  99. )
  100. /*++
  101. Routine Description:
  102. This routine performs any controller specific initialization steps.
  103. Arguments:
  104. Controller - Supplies a pointer to the controller.
  105. Context - Supplies a context pointer passed to the SD/MMC library upon
  106. creation of the controller.
  107. Phase - Supplies the phase of initialization. Phase 0 happens after the
  108. initial software reset and Phase 1 happens after the bus width has been
  109. set to 1 and the speed to 400KHz.
  110. Return Value:
  111. Status code.
  112. --*/
  113. {
  114. UINT32 Capabilities;
  115. UINT32 HostControl;
  116. SD_HOST_VERSION HostVersion;
  117. EFI_STATUS Status;
  118. UINT32 Value;
  119. //
  120. // Phase 0 is an early initialization phase that happens after the
  121. // controller has been set. It is used to gather capabilities and set
  122. // certain parameters in the hardware.
  123. //
  124. if (Phase == 0) {
  125. Capabilities = SD_READ_REGISTER(Controller, SdRegisterCapabilities);
  126. if ((Capabilities & SD_CAPABILITY_ADMA2) != 0) {
  127. Controller->HostCapabilities |= SD_MODE_ADMA2;
  128. }
  129. if ((Capabilities & SD_CAPABILITY_HIGH_SPEED) != 0) {
  130. Controller->HostCapabilities |= SD_MODE_HIGH_SPEED |
  131. SD_MODE_HIGH_SPEED_52MHZ;
  132. }
  133. //
  134. // Setup the voltage support if not supplied on creation.
  135. //
  136. if (Controller->Voltages == 0) {
  137. if ((Capabilities & SD_CAPABILITY_VOLTAGE_1V8) != 0) {
  138. Controller->Voltages |= SD_VOLTAGE_165_195 | SD_VOLTAGE_18;
  139. }
  140. if ((Capabilities & SD_CAPABILITY_VOLTAGE_3V0) != 0) {
  141. Controller->Voltages |= SD_VOLTAGE_29_30 | SD_VOLTAGE_30_31;
  142. }
  143. if ((Capabilities & SD_CAPABILITY_VOLTAGE_3V3) != 0) {
  144. Controller->Voltages |= SD_VOLTAGE_32_33 | SD_VOLTAGE_33_34;
  145. }
  146. }
  147. if (Controller->Voltages == 0) {
  148. Status = EFI_DEVICE_ERROR;
  149. goto InitializeControllerEnd;
  150. }
  151. //
  152. // Get the host control power settings from the controller voltages.
  153. // Some devices do not have a capabilities register.
  154. //
  155. if ((Controller->Voltages & (SD_VOLTAGE_32_33 | SD_VOLTAGE_33_34)) ==
  156. (SD_VOLTAGE_32_33 | SD_VOLTAGE_33_34)) {
  157. HostControl = SD_HOST_CONTROL_POWER_3V3;
  158. } else if ((Controller->Voltages &
  159. (SD_VOLTAGE_29_30 | SD_VOLTAGE_30_31)) ==
  160. (SD_VOLTAGE_29_30 | SD_VOLTAGE_30_31)) {
  161. HostControl = SD_HOST_CONTROL_POWER_3V0;
  162. } else if ((Controller->Voltages &
  163. (SD_VOLTAGE_165_195 | SD_VOLTAGE_18)) != 0) {
  164. HostControl = SD_HOST_CONTROL_POWER_1V8;
  165. } else {
  166. Status = EFI_DEVICE_ERROR;
  167. goto InitializeControllerEnd;
  168. }
  169. SD_WRITE_REGISTER(Controller, SdRegisterHostControl, HostControl);
  170. //
  171. // Set the base clock frequency if not supplied on creation.
  172. //
  173. if (Controller->FundamentalClock == 0) {
  174. Value = SD_READ_REGISTER(Controller, SdRegisterSlotStatusVersion);
  175. HostVersion = (Value >> 16) & SD_HOST_VERSION_MASK;
  176. if (HostVersion >= SdHostVersion3) {
  177. Controller->FundamentalClock =
  178. ((Capabilities >> SD_CAPABILITY_BASE_CLOCK_FREQUENCY_SHIFT) &
  179. SD_CAPABILITY_V3_BASE_CLOCK_FREQUENCY_MASK) * 1000000;
  180. } else {
  181. Controller->FundamentalClock =
  182. ((Capabilities >> SD_CAPABILITY_BASE_CLOCK_FREQUENCY_SHIFT) &
  183. SD_CAPABILITY_BASE_CLOCK_FREQUENCY_MASK) * 1000000;
  184. }
  185. }
  186. if (Controller->FundamentalClock == 0) {
  187. Status = EFI_DEVICE_ERROR;
  188. goto InitializeControllerEnd;
  189. }
  190. //
  191. // Phase 1 happens right before the initialization command sequence is
  192. // about to begin. The clock and bus width have been program and the device
  193. // is just about read to go.
  194. //
  195. } else if (Phase == 1) {
  196. HostControl = SD_READ_REGISTER(Controller, SdRegisterHostControl);
  197. HostControl |= SD_HOST_CONTROL_POWER_ENABLE;
  198. SD_WRITE_REGISTER(Controller, SdRegisterHostControl, HostControl);
  199. Value = SD_INTERRUPT_STATUS_ENABLE_DEFAULT_MASK;
  200. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatusEnable, Value);
  201. SD_WRITE_REGISTER(Controller, SdRegisterInterruptSignalEnable, 0);
  202. }
  203. Status = EFI_SUCCESS;
  204. InitializeControllerEnd:
  205. return Status;
  206. }
  207. EFI_STATUS
  208. EfipSdResetController (
  209. PEFI_SD_CONTROLLER Controller,
  210. VOID *Context,
  211. UINT32 Flags
  212. )
  213. /*++
  214. Routine Description:
  215. This routine performs a soft reset of the SD controller.
  216. Arguments:
  217. Controller - Supplies a pointer to the controller.
  218. Context - Supplies a context pointer passed to the SD/MMC library upon
  219. creation of the controller.
  220. Flags - Supplies a bitmask of reset flags. See SD_RESET_FLAG_* for
  221. definitions.
  222. Return Value:
  223. Status code.
  224. --*/
  225. {
  226. UINT32 ResetBits;
  227. EFI_STATUS Status;
  228. UINT64 Time;
  229. UINT64 Timeout;
  230. UINT32 Value;
  231. ResetBits = 0;
  232. if ((Flags & SD_RESET_FLAG_ALL) != 0) {
  233. ResetBits |= SD_CLOCK_CONTROL_RESET_ALL;
  234. }
  235. if ((Flags & SD_RESET_FLAG_COMMAND_LINE) != 0) {
  236. ResetBits |= SD_CLOCK_CONTROL_RESET_COMMAND_LINE;
  237. }
  238. if ((Flags & SD_RESET_FLAG_DATA_LINE) != 0) {
  239. ResetBits |= SD_CLOCK_CONTROL_RESET_DATA_LINE;
  240. }
  241. Value = SD_READ_REGISTER(Controller, SdRegisterClockControl);
  242. SD_WRITE_REGISTER(Controller, SdRegisterClockControl, Value | ResetBits);
  243. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  244. Time = 0;
  245. Status = EFI_TIMEOUT;
  246. do {
  247. Value = SD_READ_REGISTER(Controller, SdRegisterClockControl);
  248. if ((Value & ResetBits) == 0) {
  249. Status = EFI_SUCCESS;
  250. break;
  251. }
  252. EfiStall(50);
  253. Time += 50;
  254. } while (Time <= Timeout);
  255. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatusEnable, 0xFFFFFFFF);
  256. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatus, 0xFFFFFFFF);
  257. return Status;
  258. }
  259. EFI_STATUS
  260. EfipSdSendCommand (
  261. PEFI_SD_CONTROLLER Controller,
  262. VOID *Context,
  263. PSD_COMMAND Command
  264. )
  265. /*++
  266. Routine Description:
  267. This routine sends the given command to the card.
  268. Arguments:
  269. Controller - Supplies a pointer to the controller.
  270. Context - Supplies a context pointer passed to the SD/MMC library upon
  271. creation of the controller.
  272. Command - Supplies a pointer to the command parameters.
  273. Return Value:
  274. Status code.
  275. --*/
  276. {
  277. UINT32 Flags;
  278. UINT32 InhibitMask;
  279. EFI_STATUS Status;
  280. UINT64 Time;
  281. UINT64 Timeout;
  282. UINT32 Value;
  283. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  284. Time = 0;
  285. Status = EFI_TIMEOUT;
  286. //
  287. // Don't wait for the data inhibit flag if this is the abort command.
  288. //
  289. InhibitMask = SD_STATE_DATA_INHIBIT | SD_STATE_COMMAND_INHIBIT;
  290. if ((Command->Command == SdCommandStopTransmission) &&
  291. (Command->ResponseType != SD_RESPONSE_R1B)) {
  292. InhibitMask = SD_STATE_COMMAND_INHIBIT;
  293. }
  294. do {
  295. Value = SD_READ_REGISTER(Controller, SdRegisterPresentState);
  296. if ((Value & InhibitMask) == 0) {
  297. Status = EFI_SUCCESS;
  298. break;
  299. }
  300. EfiStall(5);
  301. Time += 5;
  302. } while (Time <= Timeout);
  303. if (EFI_ERROR(Status)) {
  304. goto SendCommandEnd;
  305. }
  306. //
  307. // Clear interrupts from the previous command.
  308. //
  309. SD_WRITE_REGISTER(Controller,
  310. SdRegisterInterruptStatus,
  311. SD_INTERRUPT_STATUS_ALL_MASK);
  312. //
  313. // Set up the expected response flags.
  314. //
  315. Flags = 0;
  316. if ((Command->ResponseType & SD_RESPONSE_PRESENT) != 0) {
  317. if ((Command->ResponseType & SD_RESPONSE_136_BIT) != 0) {
  318. Flags |= SD_COMMAND_RESPONSE_136;
  319. } else if ((Command->ResponseType & SD_RESPONSE_BUSY) != 0) {
  320. Flags |= SD_COMMAND_RESPONSE_48_BUSY;
  321. } else {
  322. Flags |= SD_COMMAND_RESPONSE_48;
  323. }
  324. }
  325. //
  326. // Set up the remainder of the command flags.
  327. //
  328. if ((Command->ResponseType & SD_RESPONSE_VALID_CRC) != 0) {
  329. Flags |= SD_COMMAND_CRC_CHECK_ENABLE;
  330. }
  331. if ((Command->ResponseType & SD_RESPONSE_OPCODE) != 0) {
  332. Flags |= SD_COMMAND_COMMAND_INDEX_CHECK_ENABLE;
  333. }
  334. //
  335. // If there's a data buffer, program the block count.
  336. //
  337. if (Command->BufferSize != 0) {
  338. if ((Command->Command == SdCommandReadMultipleBlocks) ||
  339. (Command->Command == SdCommandWriteMultipleBlocks)) {
  340. Flags |= SD_COMMAND_MULTIPLE_BLOCKS |
  341. SD_COMMAND_BLOCK_COUNT_ENABLE;
  342. if ((Controller->HostCapabilities & SD_MODE_AUTO_CMD12) != 0) {
  343. Flags |= SD_COMMAND_AUTO_COMMAND12_ENABLE;
  344. }
  345. Value = SD_BLOCK_SIZE |
  346. ((Command->BufferSize / SD_BLOCK_SIZE) << 16);
  347. SD_WRITE_REGISTER(Controller, SdRegisterBlockSizeCount, Value);
  348. } else {
  349. Value = Command->BufferSize;
  350. SD_WRITE_REGISTER(Controller, SdRegisterBlockSizeCount, Value);
  351. }
  352. Flags |= SD_COMMAND_DATA_PRESENT;
  353. if (Command->Write != FALSE) {
  354. Flags |= SD_COMMAND_TRANSFER_WRITE;
  355. } else {
  356. Flags |= SD_COMMAND_TRANSFER_READ;
  357. }
  358. }
  359. SD_WRITE_REGISTER(Controller,
  360. SdRegisterArgument1,
  361. Command->CommandArgument);
  362. Value = (Command->Command << SD_COMMAND_INDEX_SHIFT) | Flags;
  363. SD_WRITE_REGISTER(Controller, SdRegisterCommand, Value);
  364. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  365. Time = 0;
  366. Status = EFI_TIMEOUT;
  367. do {
  368. Value = SD_READ_REGISTER(Controller, SdRegisterInterruptStatus);
  369. if (Value != 0) {
  370. Status = EFI_SUCCESS;
  371. break;
  372. }
  373. EfiStall(5);
  374. Time += 5;
  375. } while (Time <= Timeout);
  376. if (EFI_ERROR(Status)) {
  377. goto SendCommandEnd;
  378. }
  379. if ((Value & SD_INTERRUPT_STATUS_COMMAND_TIMEOUT_ERROR) != 0) {
  380. EfipSdResetController(Controller,
  381. Controller->ConsumerContext,
  382. SD_RESET_FLAG_COMMAND_LINE);
  383. Status = EFI_TIMEOUT;
  384. goto SendCommandEnd;
  385. } else if ((Value & SD_INTERRUPT_STATUS_ERROR_INTERRUPT) != 0) {
  386. Status = EFI_DEVICE_ERROR;
  387. goto SendCommandEnd;
  388. }
  389. if ((Value & SD_INTERRUPT_STATUS_COMMAND_COMPLETE) != 0) {
  390. SD_WRITE_REGISTER(Controller,
  391. SdRegisterInterruptStatus,
  392. SD_INTERRUPT_STATUS_COMMAND_COMPLETE);
  393. //
  394. // Get the response if there is one.
  395. //
  396. if ((Command->ResponseType & SD_RESPONSE_PRESENT) != 0) {
  397. if ((Command->ResponseType & SD_RESPONSE_136_BIT) != 0) {
  398. Command->Response[3] = SD_READ_REGISTER(Controller,
  399. SdRegisterResponse10);
  400. Command->Response[2] = SD_READ_REGISTER(Controller,
  401. SdRegisterResponse32);
  402. Command->Response[1] = SD_READ_REGISTER(Controller,
  403. SdRegisterResponse54);
  404. Command->Response[0] = SD_READ_REGISTER(Controller,
  405. SdRegisterResponse76);
  406. if ((Controller->HostCapabilities &
  407. SD_MODE_RESPONSE136_SHIFTED) != 0) {
  408. Command->Response[0] =
  409. (Command->Response[0] << 8) |
  410. ((Command->Response[1] >> 24) & 0xFF);
  411. Command->Response[1] =
  412. (Command->Response[1] << 8) |
  413. ((Command->Response[2] >> 24) & 0xFF);
  414. Command->Response[2] =
  415. (Command->Response[2] << 8) |
  416. ((Command->Response[3] >> 24) & 0xFF);
  417. Command->Response[3] = Command->Response[3] << 8;
  418. }
  419. } else {
  420. Command->Response[0] = SD_READ_REGISTER(Controller,
  421. SdRegisterResponse10);
  422. }
  423. }
  424. }
  425. if (Command->BufferSize != 0) {
  426. if (Command->Write != FALSE) {
  427. Status = EfipSdWriteData(Controller,
  428. Command->Buffer,
  429. Command->BufferSize);
  430. } else {
  431. Status = EfipSdReadData(Controller,
  432. Command->Buffer,
  433. Command->BufferSize);
  434. }
  435. if (EFI_ERROR(Status)) {
  436. goto SendCommandEnd;
  437. }
  438. }
  439. Status = EFI_SUCCESS;
  440. SendCommandEnd:
  441. return Status;
  442. }
  443. EFI_STATUS
  444. EfipSdGetSetBusWidth (
  445. PEFI_SD_CONTROLLER Controller,
  446. VOID *Context,
  447. UINT16 *BusWidth,
  448. BOOLEAN Set
  449. )
  450. /*++
  451. Routine Description:
  452. This routine gets or sets the controller's bus width.
  453. Arguments:
  454. Controller - Supplies a pointer to the controller.
  455. Context - Supplies a context pointer passed to the SD/MMC library upon
  456. creation of the controller.
  457. BusWidth - Supplies a pointer that receives bus width information on get
  458. and contains bus width information on set.
  459. Set - Supplies a boolean indicating whether the bus width should be queried
  460. or set.
  461. Return Value:
  462. Status code.
  463. --*/
  464. {
  465. UINT32 Value;
  466. Value = SD_READ_REGISTER(Controller, SdRegisterHostControl);
  467. if (Set != FALSE) {
  468. Value &= ~SD_HOST_CONTROL_BUS_WIDTH_MASK;
  469. switch (*BusWidth) {
  470. case 1:
  471. Value |= SD_HOST_CONTROL_DATA_1BIT;
  472. break;
  473. case 4:
  474. Value |= SD_HOST_CONTROL_DATA_4BIT;
  475. break;
  476. case 8:
  477. Value |= SD_HOST_CONTROL_DATA_8BIT;
  478. break;
  479. default:
  480. return EFI_INVALID_PARAMETER;
  481. }
  482. SD_WRITE_REGISTER(Controller, SdRegisterHostControl, Value);
  483. } else {
  484. if ((Value & SD_HOST_CONTROL_DATA_8BIT) != 0) {
  485. *BusWidth = 8;
  486. } else if ((Value & SD_HOST_CONTROL_DATA_4BIT) != 0) {
  487. *BusWidth = 4;
  488. } else {
  489. *BusWidth = 1;
  490. }
  491. }
  492. return EFI_SUCCESS;
  493. }
  494. EFI_STATUS
  495. EfipSdGetSetClockSpeed (
  496. PEFI_SD_CONTROLLER Controller,
  497. VOID *Context,
  498. UINT32 *ClockSpeed,
  499. BOOLEAN Set
  500. )
  501. /*++
  502. Routine Description:
  503. This routine gets or sets the controller's clock speed.
  504. Arguments:
  505. Controller - Supplies a pointer to the controller.
  506. Context - Supplies a context pointer passed to the SD/MMC library upon
  507. creation of the controller.
  508. ClockSpeed - Supplies a pointer that receives the current clock speed on
  509. get and contains the desired clock speed on set.
  510. Set - Supplies a boolean indicating whether the bus width should be queried
  511. or set.
  512. Return Value:
  513. Status code.
  514. --*/
  515. {
  516. UINT32 ClockControl;
  517. UINT32 Divisor;
  518. SD_HOST_VERSION HostVersion;
  519. UINT32 Result;
  520. EFI_STATUS Status;
  521. UINT64 Time;
  522. UINT64 Timeout;
  523. UINT32 Value;
  524. if (Controller->FundamentalClock == 0) {
  525. return EFI_INVALID_PARAMETER;
  526. }
  527. //
  528. // Getting the clock speed is not implemented as the divisor math might not
  529. // work out precisely in reverse.
  530. //
  531. if (Set == FALSE) {
  532. return EFI_UNSUPPORTED;
  533. }
  534. //
  535. // Find the right divisor, the highest frequency less than the desired
  536. // clock. Older controllers must be a power of 2.
  537. //
  538. Value = SD_READ_REGISTER(Controller, SdRegisterSlotStatusVersion) >> 16;
  539. HostVersion = Value & SD_HOST_VERSION_MASK;
  540. if (HostVersion < SdHostVersion3) {
  541. Result = Controller->FundamentalClock;
  542. Divisor = 1;
  543. while (Divisor < SD_V2_MAX_DIVISOR) {
  544. if (Result <= *ClockSpeed) {
  545. break;
  546. }
  547. Divisor <<= 1;
  548. Result >>= 1;
  549. }
  550. Divisor >>= 1;
  551. //
  552. // Version 3 divisors are a multiple of 2.
  553. //
  554. } else {
  555. if (*ClockSpeed >= Controller->FundamentalClock) {
  556. Divisor = 0;
  557. } else {
  558. Divisor = 2;
  559. while (Divisor < SD_V3_MAX_DIVISOR) {
  560. if ((Controller->FundamentalClock / Divisor) <= *ClockSpeed) {
  561. break;
  562. }
  563. Divisor += 2;
  564. }
  565. Divisor >>= 1;
  566. }
  567. }
  568. ClockControl = SD_CLOCK_CONTROL_DEFAULT_TIMEOUT <<
  569. SD_CLOCK_CONTROL_TIMEOUT_SHIFT;
  570. SD_WRITE_REGISTER(Controller, SdRegisterClockControl, ClockControl);
  571. ClockControl |= (Divisor & SD_CLOCK_CONTROL_DIVISOR_MASK) <<
  572. SD_CLOCK_CONTROL_DIVISOR_SHIFT;
  573. ClockControl |= (Divisor & SD_CLOCK_CONTROL_DIVISOR_HIGH_MASK) >>
  574. SD_CLOCK_CONTROL_DIVISOR_HIGH_SHIFT;
  575. ClockControl |= SD_CLOCK_CONTROL_INTERNAL_CLOCK_ENABLE;
  576. SD_WRITE_REGISTER(Controller, SdRegisterClockControl, ClockControl);
  577. SD_WRITE_REGISTER(Controller, SdRegisterClockControl, ClockControl);
  578. Status = EFI_TIMEOUT;
  579. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  580. Time = 0;
  581. do {
  582. Value = SD_READ_REGISTER(Controller, SdRegisterClockControl);
  583. if ((Value & SD_CLOCK_CONTROL_CLOCK_STABLE) != 0) {
  584. Status = EFI_SUCCESS;
  585. break;
  586. }
  587. EfiStall(50);
  588. Time += 50;
  589. } while (Time <= Timeout);
  590. if (EFI_ERROR(Status)) {
  591. return Status;
  592. }
  593. ClockControl |= SD_CLOCK_CONTROL_SD_CLOCK_ENABLE;
  594. SD_WRITE_REGISTER(Controller, SdRegisterClockControl, ClockControl);
  595. return EFI_SUCCESS;
  596. }
  597. //
  598. // --------------------------------------------------------- Internal Functions
  599. //
  600. EFI_STATUS
  601. EfipSdReadData (
  602. PEFI_SD_CONTROLLER Controller,
  603. VOID *Data,
  604. UINT32 Size
  605. )
  606. /*++
  607. Routine Description:
  608. This routine reads polled data from the SD controller.
  609. Arguments:
  610. Controller - Supplies a pointer to the controller.
  611. Data - Supplies a pointer to the buffer where the data will be read into.
  612. Size - Supplies the size in bytes. This must be a multiple of four bytes.
  613. Return Value:
  614. Status code.
  615. --*/
  616. {
  617. UINT32 *Buffer32;
  618. UINT32 Count;
  619. UINT32 IoIndex;
  620. UINT32 Mask;
  621. EFI_STATUS Status;
  622. UINT64 Time;
  623. UINT64 Timeout;
  624. UINT32 Value;
  625. Buffer32 = (UINT32 *)Data;
  626. Count = Size;
  627. if (Count > SD_BLOCK_SIZE) {
  628. Count = SD_BLOCK_SIZE;
  629. }
  630. Count /= sizeof(UINT32);
  631. while (Size != 0) {
  632. //
  633. // Get the interrupt status register.
  634. //
  635. Time = 0;
  636. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  637. Status = EFI_TIMEOUT;
  638. do {
  639. Value = SD_READ_REGISTER(Controller, SdRegisterInterruptStatus);
  640. if (Value != 0) {
  641. Status = EFI_SUCCESS;
  642. break;
  643. }
  644. EfiStall(5);
  645. Time += 5;
  646. } while (Time <= Timeout);
  647. if (EFI_ERROR(Status)) {
  648. return Status;
  649. }
  650. Mask = SD_INTERRUPT_STATUS_DATA_TIMEOUT_ERROR |
  651. SD_INTERRUPT_STATUS_DATA_CRC_ERROR |
  652. SD_INTERRUPT_STATUS_DATA_END_BIT_ERROR;
  653. if ((Value & Mask) != 0) {
  654. EfipSdResetController(Controller,
  655. Controller->ConsumerContext,
  656. SD_RESET_FLAG_DATA_LINE);
  657. }
  658. if ((Value & SD_INTERRUPT_STATUS_ERROR_INTERRUPT) != 0) {
  659. return EFI_DEVICE_ERROR;
  660. }
  661. if ((Value & SD_INTERRUPT_STATUS_BUFFER_READ_READY) != 0) {
  662. //
  663. // Acknowledge this batch of interrupts.
  664. //
  665. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatus, Value);
  666. Value = 0;
  667. for (IoIndex = 0; IoIndex < Count; IoIndex += 1) {
  668. *Buffer32 = SD_READ_REGISTER(Controller,
  669. SdRegisterBufferDataPort);
  670. Buffer32 += 1;
  671. }
  672. Size -= Count * sizeof(UINT32);
  673. }
  674. }
  675. Value = SD_READ_REGISTER(Controller, SdRegisterInterruptStatus);
  676. Mask = SD_INTERRUPT_STATUS_BUFFER_WRITE_READY |
  677. SD_INTERRUPT_STATUS_TRANSFER_COMPLETE;
  678. if ((Value & Mask) != 0) {
  679. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatus, Value);
  680. }
  681. return EFI_SUCCESS;
  682. }
  683. EFI_STATUS
  684. EfipSdWriteData (
  685. PEFI_SD_CONTROLLER Controller,
  686. VOID *Data,
  687. UINT32 Size
  688. )
  689. /*++
  690. Routine Description:
  691. This routine writes polled data to the SD controller.
  692. Arguments:
  693. Controller - Supplies a pointer to the controller.
  694. Data - Supplies a pointer to the buffer containing the data to write.
  695. Size - Supplies the size in bytes. This must be a multiple of 4 bytes.
  696. Return Value:
  697. Status code.
  698. --*/
  699. {
  700. UINT32 *Buffer32;
  701. UINT32 Count;
  702. UINT32 IoIndex;
  703. UINT32 Mask;
  704. EFI_STATUS Status;
  705. UINT64 Time;
  706. UINT64 Timeout;
  707. UINT32 Value;
  708. Buffer32 = (UINT32 *)Data;
  709. Count = Size;
  710. if (Count > SD_BLOCK_SIZE) {
  711. Count = SD_BLOCK_SIZE;
  712. }
  713. Count /= sizeof(UINT32);
  714. while (Size != 0) {
  715. //
  716. // Get the interrupt status register.
  717. //
  718. Time = 0;
  719. Timeout = EFI_SD_CONTROLLER_TIMEOUT;
  720. Status = EFI_TIMEOUT;
  721. do {
  722. Value = SD_READ_REGISTER(Controller, SdRegisterInterruptStatus);
  723. if (Value != 0) {
  724. Status = EFI_SUCCESS;
  725. break;
  726. }
  727. Time += 5;
  728. EfiStall(5);
  729. } while (Time <= Timeout);
  730. if (EFI_ERROR(Status)) {
  731. return Status;
  732. }
  733. Mask = SD_INTERRUPT_STATUS_DATA_TIMEOUT_ERROR |
  734. SD_INTERRUPT_STATUS_DATA_CRC_ERROR |
  735. SD_INTERRUPT_STATUS_DATA_END_BIT_ERROR;
  736. if ((Value & Mask) != 0) {
  737. EfipSdResetController(Controller,
  738. Controller->ConsumerContext,
  739. SD_RESET_FLAG_DATA_LINE);
  740. }
  741. if ((Value & SD_INTERRUPT_STATUS_ERROR_INTERRUPT) != 0) {
  742. return EFI_DEVICE_ERROR;
  743. }
  744. if ((Value & SD_INTERRUPT_STATUS_BUFFER_WRITE_READY) != 0) {
  745. //
  746. // Acknowledge this batch of interrupts.
  747. //
  748. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatus, Value);
  749. Value = 0;
  750. for (IoIndex = 0; IoIndex < Count; IoIndex += 1) {
  751. SD_WRITE_REGISTER(Controller,
  752. SdRegisterBufferDataPort,
  753. *Buffer32);
  754. Buffer32 += 1;
  755. }
  756. Size -= Count * sizeof(UINT32);
  757. }
  758. }
  759. Mask = SD_INTERRUPT_STATUS_BUFFER_READ_READY |
  760. SD_INTERRUPT_STATUS_TRANSFER_COMPLETE;
  761. if ((Value & Mask) != 0) {
  762. SD_WRITE_REGISTER(Controller, SdRegisterInterruptStatus, Value);
  763. }
  764. return EFI_SUCCESS;
  765. }