rtc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. rtc.c
  5. Abstract:
  6. This module implements support for speaking to the RTC module on the
  7. AM335x SoC.
  8. Author:
  9. Evan Green 23-Sep-2015
  10. Environment:
  11. Firmware
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <uefifw.h>
  17. #include <minoca/soc/am335x.h>
  18. #include "../bbonefw.h"
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. #define AM3_READ_RTC(_Register) \
  23. *(volatile UINT32 *)(EfiAm335RtcBase + (_Register))
  24. #define AM3_WRITE_RTC(_Register, _Value) \
  25. *((volatile UINT32 *)(EfiAm335RtcBase + (_Register))) = (_Value)
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. //
  30. // Define the default cutoff year guess between the twentieth and twenty first
  31. // century.
  32. //
  33. #define AM3_CENTURY_CUTOFF_YEAR 70
  34. //
  35. // Define some constants to stuff into scratch 0 that indicate the time zone
  36. // minutes are being stored there.
  37. //
  38. #define AM3_SCRATCH0_MAGIC 0x5F4A0000
  39. #define AM3_SCRATCH0_MAGIC_MASK 0xFFFF0000
  40. #define AM3_SCRATCH0_TIME_ZONE_MASK 0x0000FFFF
  41. //
  42. // Define some constants to stuff into scratch 1 that indicate the daylight bit
  43. // is stored there.
  44. //
  45. #define AM3_SCRATCH1_MAGIC 0xB13C0000
  46. #define AM3_SCRATCH1_MAGIC_MASK 0xFFFF0000
  47. #define AM3_SCRATCH1_DAYLIGHT 0x00008000
  48. #define AM3_SCRATCH1_CENTURY_MASK 0x000000FF
  49. //
  50. // ------------------------------------------------------ Data Type Definitions
  51. //
  52. //
  53. // ----------------------------------------------- Internal Function Prototypes
  54. //
  55. VOID
  56. EfipAm335WaitForNonBusyEdge (
  57. VOID
  58. );
  59. VOID
  60. EfipAm335LockRtc (
  61. BOOLEAN Lock
  62. );
  63. //
  64. // -------------------------------------------------------------------- Globals
  65. //
  66. //
  67. // Define the pointer to the RTC base, which will get virtualized when going
  68. // to runtime.
  69. //
  70. VOID *EfiAm335RtcBase = (VOID *)AM335_RTC_BASE;
  71. //
  72. // ------------------------------------------------------------------ Functions
  73. //
  74. EFIAPI
  75. EFI_STATUS
  76. EfipAm335GetTime (
  77. EFI_TIME *Time,
  78. EFI_TIME_CAPABILITIES *Capabilities
  79. )
  80. /*++
  81. Routine Description:
  82. This routine returns the current time and dat information, and
  83. timekeeping capabilities of the hardware platform.
  84. Arguments:
  85. Time - Supplies a pointer where the current time will be returned.
  86. Capabilities - Supplies an optional pointer where the capabilities will be
  87. returned on success.
  88. Return Value:
  89. EFI_SUCCESS on success.
  90. EFI_INVALID_PARAMETER if the time parameter was NULL.
  91. EFI_DEVICE_ERROR if there was a hardware error accessing the device.
  92. --*/
  93. {
  94. UINT32 Control;
  95. UINT32 Value;
  96. UINT32 Year;
  97. if (Capabilities != NULL) {
  98. Capabilities->Resolution = 1;
  99. Capabilities->Accuracy = 0;
  100. Capabilities->SetsToZero = FALSE;
  101. }
  102. Control = AM3_READ_RTC(Am335RtcControl);
  103. //
  104. // The RTC cannot be turned back on once it's off.
  105. //
  106. if ((Control & AM335_RTC_CONTROL_RTC_DISABLE) != 0) {
  107. return EFI_DEVICE_ERROR;
  108. }
  109. //
  110. // Values are in BCD, and all values snap as soon as the seconds register
  111. // is read, so there's no need to worry about tearing.
  112. //
  113. Value = AM3_READ_RTC(Am335RtcSeconds);
  114. Time->Second = EFI_BCD_TO_BINARY(Value);
  115. Value = AM3_READ_RTC(Am335RtcMinutes);
  116. Time->Minute = EFI_BCD_TO_BINARY(Value);
  117. //
  118. // Handle post meridiem, which have value in the range 1-12, or 24-hour
  119. // mode, which will just be 0-23.
  120. //
  121. Value = AM3_READ_RTC(Am335RtcHours);
  122. Time->Hour = EFI_BCD_TO_BINARY(Value & ~AM335_RTC_HOURS_PM);
  123. if ((Control & AM335_RTC_CONTROL_12_HOUR_MODE) != 0) {
  124. if (Time->Hour == 12) {
  125. Time->Hour = 0;
  126. }
  127. if ((Value & AM335_RTC_HOURS_PM) != 0) {
  128. Time->Hour += 12;
  129. }
  130. }
  131. Value = AM3_READ_RTC(Am335RtcDays);
  132. Time->Day = EFI_BCD_TO_BINARY(Value);
  133. Value = AM3_READ_RTC(Am335RtcMonths);
  134. Time->Month = EFI_BCD_TO_BINARY(Value);
  135. Value = AM3_READ_RTC(Am335RtcYears);
  136. Year = EFI_BCD_TO_BINARY(Value);
  137. //
  138. // The time zone might be stored in scratch 0 if it was this firmware that
  139. // wrote it last.
  140. //
  141. Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  142. Value = AM3_READ_RTC(Am335RtcScratch0);
  143. if ((Value & AM3_SCRATCH0_MAGIC_MASK) == AM3_SCRATCH0_MAGIC) {
  144. Time->TimeZone = (INT16)(Value & AM3_SCRATCH0_TIME_ZONE_MASK);
  145. }
  146. //
  147. // The daylight bit and century might be stored in scratch 1.
  148. //
  149. Time->Daylight = 0;
  150. Value = AM3_READ_RTC(Am335RtcScratch1);
  151. if ((Value & AM3_SCRATCH1_MAGIC_MASK) == AM3_SCRATCH1_MAGIC) {
  152. if ((Value & AM3_SCRATCH1_DAYLIGHT) != 0) {
  153. Time->Daylight = 1;
  154. }
  155. Time->Year = ((Value & AM3_SCRATCH1_CENTURY_MASK) * 100) + Year;
  156. //
  157. // Scratch 1 doesn't have any known data in it, so take a guess.
  158. //
  159. } else {
  160. if (Year >= AM3_CENTURY_CUTOFF_YEAR) {
  161. Time->Year = 1900 + Year;
  162. } else {
  163. Time->Year = 2000 + Year;
  164. }
  165. }
  166. Time->Nanosecond = 0;
  167. return EFI_SUCCESS;
  168. }
  169. EFIAPI
  170. EFI_STATUS
  171. EfipAm335SetTime (
  172. EFI_TIME *Time
  173. )
  174. /*++
  175. Routine Description:
  176. This routine sets the current local time and date information.
  177. Arguments:
  178. Time - Supplies a pointer to the time to set.
  179. Return Value:
  180. EFI_SUCCESS on success.
  181. EFI_INVALID_PARAMETER if a time field is out of range.
  182. EFI_DEVICE_ERROR if there was a hardware error accessing the device.
  183. --*/
  184. {
  185. UINT32 Century;
  186. UINT32 Control;
  187. UINT32 Status;
  188. UINT32 Value;
  189. UINT32 Year;
  190. Control = AM3_READ_RTC(Am335RtcControl);
  191. if ((Control & AM335_RTC_CONTROL_RTC_DISABLE) != 0) {
  192. return EFI_DEVICE_ERROR;
  193. }
  194. EfipAm335LockRtc(FALSE);
  195. //
  196. // Stop the clock, wait for it to really stop, program the time,
  197. // start the clock.
  198. //
  199. Control = 0;
  200. AM3_WRITE_RTC(Am335RtcControl, 0);
  201. do {
  202. Status = AM3_READ_RTC(Am335RtcStatus);
  203. } while ((Status & AM335_RTC_STATUS_RUN) != 0);
  204. Century = 19;
  205. Year = Time->Year - 1900;
  206. while (Year >= 100) {
  207. Century += 1;
  208. Year -= 100;
  209. }
  210. Value = AM3_SCRATCH0_MAGIC | (Time->TimeZone & AM3_SCRATCH0_TIME_ZONE_MASK);
  211. AM3_WRITE_RTC(Am335RtcScratch0, Value);
  212. Value = AM3_SCRATCH1_MAGIC | (Century & AM3_SCRATCH1_CENTURY_MASK);
  213. if (Time->Daylight != 0) {
  214. Value |= AM3_SCRATCH1_DAYLIGHT;
  215. }
  216. AM3_WRITE_RTC(Am335RtcScratch1, Value);
  217. AM3_WRITE_RTC(Am335RtcYears, EFI_BINARY_TO_BCD(Year));
  218. AM3_WRITE_RTC(Am335RtcMonths, EFI_BINARY_TO_BCD(Time->Month));
  219. AM3_WRITE_RTC(Am335RtcDays, EFI_BINARY_TO_BCD(Time->Day));
  220. AM3_WRITE_RTC(Am335RtcHours, EFI_BINARY_TO_BCD(Time->Hour));
  221. AM3_WRITE_RTC(Am335RtcMinutes, EFI_BINARY_TO_BCD(Time->Minute));
  222. AM3_WRITE_RTC(Am335RtcSeconds, EFI_BINARY_TO_BCD(Time->Second));
  223. Control = AM335_RTC_CONTROL_RUN;
  224. AM3_WRITE_RTC(Am335RtcControl, Control);
  225. do {
  226. Status = AM3_READ_RTC(Am335RtcStatus);
  227. } while ((Status & AM335_RTC_STATUS_RUN) == 0);
  228. EfipAm335LockRtc(TRUE);
  229. return EFI_SUCCESS;
  230. }
  231. EFIAPI
  232. EFI_STATUS
  233. EfipAm335GetWakeupTime (
  234. BOOLEAN *Enabled,
  235. BOOLEAN *Pending,
  236. EFI_TIME *Time
  237. )
  238. /*++
  239. Routine Description:
  240. This routine gets the current wake alarm setting.
  241. Arguments:
  242. Enabled - Supplies a pointer that receives a boolean indicating if the
  243. alarm is currently enabled or disabled.
  244. Pending - Supplies a pointer that receives a boolean indicating if the
  245. alarm signal is pending and requires acknowledgement.
  246. Time - Supplies a pointer that receives the current wake time.
  247. Return Value:
  248. EFI_SUCCESS on success.
  249. EFI_INVALID_PARAMETER if any parameter is NULL.
  250. EFI_DEVICE_ERROR if there was a hardware error accessing the device.
  251. EFI_UNSUPPORTED if the wakeup timer is not supported on this platform.
  252. --*/
  253. {
  254. UINT32 Control;
  255. UINT32 Interrupts;
  256. UINT32 Status;
  257. UINT32 Value;
  258. UINT32 Year;
  259. Control = AM3_READ_RTC(Am335RtcControl);
  260. if ((Control & AM335_RTC_CONTROL_RTC_DISABLE) != 0) {
  261. return EFI_DEVICE_ERROR;
  262. }
  263. //
  264. // Unlock the RTC for that one potential access to clear the status bit.
  265. //
  266. EfipAm335LockRtc(FALSE);
  267. EfipAm335WaitForNonBusyEdge();
  268. //
  269. // Values are in BCD, and all values snap as soon as the seconds register
  270. // is read, so there's no need to worry about tearing.
  271. //
  272. Value = AM3_READ_RTC(Am335RtcAlarmSeconds);
  273. Time->Second = EFI_BCD_TO_BINARY(Value);
  274. Value = AM3_READ_RTC(Am335RtcAlarmMinutes);
  275. Time->Minute = EFI_BCD_TO_BINARY(Value);
  276. //
  277. // Handle post meridiem, which have value in the range 1-12, or 24-hour
  278. // mode, which will just be 0-23.
  279. //
  280. Value = AM3_READ_RTC(Am335RtcAlarmHours);
  281. Time->Hour = EFI_BCD_TO_BINARY(Value & ~AM335_RTC_HOURS_PM);
  282. if ((Control & AM335_RTC_CONTROL_12_HOUR_MODE) != 0) {
  283. if (Time->Hour == 12) {
  284. Time->Hour = 0;
  285. }
  286. if ((Value & AM335_RTC_HOURS_PM) != 0) {
  287. Time->Hour += 12;
  288. }
  289. }
  290. Value = AM3_READ_RTC(Am335RtcAlarmDays);
  291. Time->Day = EFI_BCD_TO_BINARY(Value);
  292. Value = AM3_READ_RTC(Am335RtcAlarmMonths);
  293. Time->Month = EFI_BCD_TO_BINARY(Value);
  294. Value = AM3_READ_RTC(Am335RtcAlarmYears);
  295. Year = EFI_BCD_TO_BINARY(Value);
  296. Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
  297. Time->Daylight = 0;
  298. if (Year >= AM3_CENTURY_CUTOFF_YEAR) {
  299. Time->Year = 1900 + Year;
  300. } else {
  301. Time->Year = 2000 + Year;
  302. }
  303. Time->Nanosecond = 0;
  304. Interrupts = AM3_READ_RTC(Am335RtcInterruptEnable);
  305. *Enabled = FALSE;
  306. if ((Interrupts & AM335_RTC_INTERRUPT_ALARM) != 0) {
  307. *Enabled = TRUE;
  308. }
  309. Status = AM3_READ_RTC(Am335RtcStatus);
  310. *Pending = FALSE;
  311. if ((Status & AM335_RTC_STATUS_ALARM) != 0) {
  312. *Pending = TRUE;
  313. Status &= ~AM335_RTC_STATUS_ALARM;
  314. AM3_WRITE_RTC(Am335RtcStatus, Status);
  315. }
  316. EfipAm335LockRtc(FALSE);
  317. return EFI_SUCCESS;
  318. }
  319. EFIAPI
  320. EFI_STATUS
  321. EfipAm335SetWakeupTime (
  322. BOOLEAN Enable,
  323. EFI_TIME *Time
  324. )
  325. /*++
  326. Routine Description:
  327. This routine sets the current wake alarm setting.
  328. Arguments:
  329. Enable - Supplies a boolean enabling or disabling the wakeup timer.
  330. Time - Supplies an optional pointer to the time to set. This parameter is
  331. only optional if the enable parameter is FALSE.
  332. Return Value:
  333. EFI_SUCCESS on success.
  334. EFI_INVALID_PARAMETER if a time field is out of range.
  335. EFI_DEVICE_ERROR if there was a hardware error accessing the device.
  336. EFI_UNSUPPORTED if the wakeup timer is not supported on this platform.
  337. --*/
  338. {
  339. UINT32 Control;
  340. UINT32 Hour;
  341. UINT32 Interrupts;
  342. UINT32 Year;
  343. Control = AM3_READ_RTC(Am335RtcControl);
  344. if ((Control & AM335_RTC_CONTROL_RTC_DISABLE) != 0) {
  345. return EFI_DEVICE_ERROR;
  346. }
  347. if ((Control & AM335_RTC_CONTROL_12_HOUR_MODE) != 0) {
  348. Hour = Time->Hour;
  349. if (Hour == 0) {
  350. Hour = EFI_BINARY_TO_BCD(12);
  351. } else if (Hour >= 12) {
  352. Hour = EFI_BINARY_TO_BCD(Hour - 12);
  353. Hour |= AM335_RTC_HOURS_PM;
  354. }
  355. } else {
  356. Hour = EFI_BINARY_TO_BCD(Time->Hour);
  357. }
  358. Year = Time->Year - 1900;
  359. while (Year >= 100) {
  360. Year -= 100;
  361. }
  362. EfipAm335LockRtc(FALSE);
  363. EfipAm335WaitForNonBusyEdge();
  364. AM3_WRITE_RTC(Am335RtcAlarmYears, EFI_BINARY_TO_BCD(Year));
  365. AM3_WRITE_RTC(Am335RtcAlarmMonths, EFI_BINARY_TO_BCD(Time->Month));
  366. AM3_WRITE_RTC(Am335RtcAlarmDays, EFI_BINARY_TO_BCD(Time->Day));
  367. AM3_WRITE_RTC(Am335RtcAlarmHours, Hour);
  368. AM3_WRITE_RTC(Am335RtcAlarmMinutes, EFI_BINARY_TO_BCD(Time->Minute));
  369. AM3_WRITE_RTC(Am335RtcAlarmSeconds, EFI_BINARY_TO_BCD(Time->Second));
  370. Interrupts = AM3_READ_RTC(Am335RtcInterruptEnable);
  371. Interrupts &= ~AM335_RTC_INTERRUPT_ALARM;
  372. if (Enable != FALSE) {
  373. Interrupts |= AM335_RTC_INTERRUPT_ALARM;
  374. }
  375. AM3_WRITE_RTC(Am335RtcInterruptEnable, Interrupts);
  376. EfipAm335LockRtc(TRUE);
  377. return EFI_SUCCESS;
  378. }
  379. //
  380. // --------------------------------------------------------- Internal Functions
  381. //
  382. VOID
  383. EfipAm335WaitForNonBusyEdge (
  384. VOID
  385. )
  386. /*++
  387. Routine Description:
  388. This routine waits for the falling edge of a the busy bit in the RTC. This
  389. could take up to two seconds if a falling edge was just missed.
  390. Arguments:
  391. None.
  392. Return Value:
  393. None.
  394. --*/
  395. {
  396. UINT32 Status;
  397. //
  398. // The busy bit will never go up if it's not running.
  399. //
  400. Status = AM3_READ_RTC(Am335RtcStatus);
  401. if ((Status & AM335_RTC_STATUS_RUN) == 0) {
  402. return;
  403. }
  404. //
  405. // Wait for a the busy bit to go high so that the start of a falling edge
  406. // can be observed.
  407. //
  408. do {
  409. Status = AM3_READ_RTC(Am335RtcStatus);
  410. } while ((Status & AM335_RTC_STATUS_BUSY) == 0);
  411. //
  412. // Now wait for a falling edge.
  413. //
  414. do {
  415. Status = AM3_READ_RTC(Am335RtcStatus);
  416. } while ((Status & AM335_RTC_STATUS_BUSY) != 0);
  417. return;
  418. }
  419. VOID
  420. EfipAm335LockRtc (
  421. BOOLEAN Lock
  422. )
  423. /*++
  424. Routine Description:
  425. This routine locks or unlocks write access to the RTC.
  426. Arguments:
  427. Lock - Supplies a boolean indicating whether to prevent write access (TRUE)
  428. or allow it (FALSE).
  429. Return Value:
  430. None.
  431. --*/
  432. {
  433. //
  434. // To lock it, write the correct kick 0 value, but the incorrect kick 1
  435. // value. According to the state machine diagram, that's the best way to
  436. // get to locked, even if the current state is somehow unlocked.
  437. //
  438. AM3_WRITE_RTC(Am335RtcKick0, AM335_RTC_KICK0_KEY);
  439. if (Lock != FALSE) {
  440. AM3_WRITE_RTC(Am335RtcKick1, 0xFFFFFFFF);
  441. //
  442. // Write the correct kick value to unlock the RTC.
  443. //
  444. } else {
  445. AM3_WRITE_RTC(Am335RtcKick1, AM335_RTC_KICK1_KEY);
  446. }
  447. return;
  448. }