mc146818.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Denis Peter MPL AG Switzerland. d.peter@mpl.ch
  5. */
  6. /*
  7. * Date & Time support for the MC146818 (PIXX4) RTC
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <dm.h>
  12. #include <rtc.h>
  13. #if defined(CONFIG_X86) || defined(CONFIG_MALTA)
  14. #include <asm/io.h>
  15. #define in8(p) inb(p)
  16. #define out8(p, v) outb(v, p)
  17. #endif
  18. #if defined(CONFIG_CMD_DATE)
  19. /* Set this to 1 to clear the CMOS RAM */
  20. #define CLEAR_CMOS 0
  21. #define RTC_PORT_MC146818 CONFIG_SYS_ISA_IO_BASE_ADDRESS + 0x70
  22. #define RTC_SECONDS 0x00
  23. #define RTC_SECONDS_ALARM 0x01
  24. #define RTC_MINUTES 0x02
  25. #define RTC_MINUTES_ALARM 0x03
  26. #define RTC_HOURS 0x04
  27. #define RTC_HOURS_ALARM 0x05
  28. #define RTC_DAY_OF_WEEK 0x06
  29. #define RTC_DATE_OF_MONTH 0x07
  30. #define RTC_MONTH 0x08
  31. #define RTC_YEAR 0x09
  32. #define RTC_CONFIG_A 0x0a
  33. #define RTC_CONFIG_B 0x0b
  34. #define RTC_CONFIG_C 0x0c
  35. #define RTC_CONFIG_D 0x0d
  36. #define RTC_REG_SIZE 0x80
  37. #define RTC_CONFIG_A_REF_CLCK_32KHZ (1 << 5)
  38. #define RTC_CONFIG_A_RATE_1024HZ 6
  39. #define RTC_CONFIG_B_24H (1 << 1)
  40. #define RTC_CONFIG_D_VALID_RAM_AND_TIME 0x80
  41. static int mc146818_read8(int reg)
  42. {
  43. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  44. return in8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg);
  45. #else
  46. int ofs = 0;
  47. if (reg >= 128) {
  48. ofs = 2;
  49. reg -= 128;
  50. }
  51. out8(RTC_PORT_MC146818 + ofs, reg);
  52. return in8(RTC_PORT_MC146818 + ofs + 1);
  53. #endif
  54. }
  55. static void mc146818_write8(int reg, uchar val)
  56. {
  57. #ifdef CONFIG_SYS_RTC_REG_BASE_ADDR
  58. out8(CONFIG_SYS_RTC_REG_BASE_ADDR + reg, val);
  59. #else
  60. int ofs = 0;
  61. if (reg >= 128) {
  62. ofs = 2;
  63. reg -= 128;
  64. }
  65. out8(RTC_PORT_MC146818 + ofs, reg);
  66. out8(RTC_PORT_MC146818 + ofs + 1, val);
  67. #endif
  68. }
  69. static int mc146818_get(struct rtc_time *tmp)
  70. {
  71. uchar sec, min, hour, mday, wday __attribute__((unused)),mon, year;
  72. /* here check if rtc can be accessed */
  73. while ((mc146818_read8(RTC_CONFIG_A) & 0x80) == 0x80)
  74. ;
  75. sec = mc146818_read8(RTC_SECONDS);
  76. min = mc146818_read8(RTC_MINUTES);
  77. hour = mc146818_read8(RTC_HOURS);
  78. mday = mc146818_read8(RTC_DATE_OF_MONTH);
  79. wday = mc146818_read8(RTC_DAY_OF_WEEK);
  80. mon = mc146818_read8(RTC_MONTH);
  81. year = mc146818_read8(RTC_YEAR);
  82. #ifdef RTC_DEBUG
  83. printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x hr: %02x min: %02x sec: %02x\n",
  84. year, mon, mday, wday, hour, min, sec);
  85. printf("Alarms: mday: %02x hour: %02x min: %02x sec: %02x\n",
  86. mc146818_read8(RTC_CONFIG_D) & 0x3f,
  87. mc146818_read8(RTC_HOURS_ALARM),
  88. mc146818_read8(RTC_MINUTES_ALARM),
  89. mc146818_read8(RTC_SECONDS_ALARM));
  90. #endif
  91. tmp->tm_sec = bcd2bin(sec & 0x7f);
  92. tmp->tm_min = bcd2bin(min & 0x7f);
  93. tmp->tm_hour = bcd2bin(hour & 0x3f);
  94. tmp->tm_mday = bcd2bin(mday & 0x3f);
  95. tmp->tm_mon = bcd2bin(mon & 0x1f);
  96. tmp->tm_year = bcd2bin(year);
  97. if (tmp->tm_year < 70)
  98. tmp->tm_year += 2000;
  99. else
  100. tmp->tm_year += 1900;
  101. tmp->tm_yday = 0;
  102. tmp->tm_isdst = 0;
  103. /*
  104. * The mc146818 only updates wday if it is non-zero, sunday is 1
  105. * saturday is 7. So let's use our library routine.
  106. */
  107. rtc_calc_weekday(tmp);
  108. #ifdef RTC_DEBUG
  109. printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  110. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  111. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  112. #endif
  113. return 0;
  114. }
  115. static int mc146818_set(struct rtc_time *tmp)
  116. {
  117. #ifdef RTC_DEBUG
  118. printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  119. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  120. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  121. #endif
  122. /* Disable the RTC to update the regs */
  123. mc146818_write8(RTC_CONFIG_B, 0x82);
  124. mc146818_write8(RTC_YEAR, bin2bcd(tmp->tm_year % 100));
  125. mc146818_write8(RTC_MONTH, bin2bcd(tmp->tm_mon));
  126. mc146818_write8(RTC_DAY_OF_WEEK, bin2bcd(tmp->tm_wday));
  127. mc146818_write8(RTC_DATE_OF_MONTH, bin2bcd(tmp->tm_mday));
  128. mc146818_write8(RTC_HOURS, bin2bcd(tmp->tm_hour));
  129. mc146818_write8(RTC_MINUTES, bin2bcd(tmp->tm_min));
  130. mc146818_write8(RTC_SECONDS, bin2bcd(tmp->tm_sec));
  131. /* Enable the RTC to update the regs */
  132. mc146818_write8(RTC_CONFIG_B, 0x02);
  133. return 0;
  134. }
  135. static void mc146818_reset(void)
  136. {
  137. /* Disable the RTC to update the regs */
  138. mc146818_write8(RTC_CONFIG_B, 0x82);
  139. /* Normal OP */
  140. mc146818_write8(RTC_CONFIG_A, 0x20);
  141. mc146818_write8(RTC_CONFIG_B, 0x00);
  142. mc146818_write8(RTC_CONFIG_B, 0x00);
  143. /* Enable the RTC to update the regs */
  144. mc146818_write8(RTC_CONFIG_B, 0x02);
  145. }
  146. static void mc146818_init(void)
  147. {
  148. #if CLEAR_CMOS
  149. int i;
  150. rtc_write8(RTC_SECONDS_ALARM, 0);
  151. rtc_write8(RTC_MINUTES_ALARM, 0);
  152. rtc_write8(RTC_HOURS_ALARM, 0);
  153. for (i = RTC_CONFIG_A; i < RTC_REG_SIZE; i++)
  154. rtc_write8(i, 0);
  155. printf("RTC: zeroing CMOS RAM\n");
  156. #endif
  157. /* Setup the real time clock */
  158. mc146818_write8(RTC_CONFIG_B, RTC_CONFIG_B_24H);
  159. /* Setup the frequency it operates at */
  160. mc146818_write8(RTC_CONFIG_A, RTC_CONFIG_A_REF_CLCK_32KHZ |
  161. RTC_CONFIG_A_RATE_1024HZ);
  162. /* Ensure all reserved bits are 0 in register D */
  163. mc146818_write8(RTC_CONFIG_D, RTC_CONFIG_D_VALID_RAM_AND_TIME);
  164. /* Clear any pending interrupts */
  165. mc146818_read8(RTC_CONFIG_C);
  166. }
  167. #endif /* CONFIG_CMD_DATE */
  168. #ifdef CONFIG_DM_RTC
  169. static int rtc_mc146818_get(struct udevice *dev, struct rtc_time *time)
  170. {
  171. return mc146818_get(time);
  172. }
  173. static int rtc_mc146818_set(struct udevice *dev, const struct rtc_time *time)
  174. {
  175. return mc146818_set((struct rtc_time *)time);
  176. }
  177. static int rtc_mc146818_reset(struct udevice *dev)
  178. {
  179. mc146818_reset();
  180. return 0;
  181. }
  182. static int rtc_mc146818_read8(struct udevice *dev, unsigned int reg)
  183. {
  184. return mc146818_read8(reg);
  185. }
  186. static int rtc_mc146818_write8(struct udevice *dev, unsigned int reg, int val)
  187. {
  188. mc146818_write8(reg, val);
  189. return 0;
  190. }
  191. static int rtc_mc146818_probe(struct udevice *dev)
  192. {
  193. mc146818_init();
  194. return 0;
  195. }
  196. static const struct rtc_ops rtc_mc146818_ops = {
  197. .get = rtc_mc146818_get,
  198. .set = rtc_mc146818_set,
  199. .reset = rtc_mc146818_reset,
  200. .read8 = rtc_mc146818_read8,
  201. .write8 = rtc_mc146818_write8,
  202. };
  203. static const struct udevice_id rtc_mc146818_ids[] = {
  204. { .compatible = "motorola,mc146818" },
  205. { }
  206. };
  207. U_BOOT_DRIVER(rtc_mc146818) = {
  208. .name = "rtc_mc146818",
  209. .id = UCLASS_RTC,
  210. .of_match = rtc_mc146818_ids,
  211. .probe = rtc_mc146818_probe,
  212. .ops = &rtc_mc146818_ops,
  213. };
  214. #else /* !CONFIG_DM_RTC */
  215. int rtc_get(struct rtc_time *tmp)
  216. {
  217. return mc146818_get(tmp);
  218. }
  219. int rtc_set(struct rtc_time *tmp)
  220. {
  221. return mc146818_set(tmp);
  222. }
  223. void rtc_reset(void)
  224. {
  225. mc146818_reset();
  226. }
  227. int rtc_read8(int reg)
  228. {
  229. return mc146818_read8(reg);
  230. }
  231. void rtc_write8(int reg, uchar val)
  232. {
  233. mc146818_write8(reg, val);
  234. }
  235. u32 rtc_read32(int reg)
  236. {
  237. u32 value = 0;
  238. int i;
  239. for (i = 0; i < sizeof(value); i++)
  240. value |= rtc_read8(reg + i) << (i << 3);
  241. return value;
  242. }
  243. void rtc_write32(int reg, u32 value)
  244. {
  245. int i;
  246. for (i = 0; i < sizeof(value); i++)
  247. rtc_write8(reg + i, (value >> (i << 3)) & 0xff);
  248. }
  249. void rtc_init(void)
  250. {
  251. mc146818_init();
  252. }
  253. #endif /* CONFIG_DM_RTC */