rtc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* RTC (CMOS) date/time read and write. */
  2. #include "rtc.h"
  3. #include "acpi.h"
  4. #define CMOS_ADDR 0x70
  5. #define CMOS_DATA 0x71
  6. #define RTC_SEC 0x00
  7. #define RTC_MIN 0x02
  8. #define RTC_HOUR 0x04
  9. #define RTC_WDAY 0x06
  10. #define RTC_DAY 0x07
  11. #define RTC_MON 0x08
  12. #define RTC_YEAR 0x09
  13. #define RTC_REGA 0x0A
  14. #define RTC_REGB 0x0B
  15. /* Last good value, used when a read straddles the update cycle and no retry
  16. * produces a complete, valid set. Initialised to 2000-01-01 (a Saturday). */
  17. static struct sys_datetime last_dt = { 2000, 1, 1, 0, 0, 0, 6 };
  18. static inline unsigned char cmos_read(unsigned char reg)
  19. {
  20. unsigned char v;
  21. /* Bit 7 on 0x70 disables NMI during access. */
  22. __asm__ volatile ("outb %b1, %w0" : : "Nd"(CMOS_ADDR), "a"((unsigned char)(reg | 0x80)));
  23. __asm__ volatile ("inb %w1, %0" : "=a"(v) : "Nd"(CMOS_DATA));
  24. return v;
  25. }
  26. static inline void cmos_write(unsigned char reg, unsigned char val)
  27. {
  28. __asm__ volatile ("outb %b1, %w0" : : "Nd"(CMOS_ADDR), "a"((unsigned char)(reg | 0x80)));
  29. __asm__ volatile ("outb %b1, %w0" : : "Nd"(CMOS_DATA), "a"(val));
  30. }
  31. static int bcd_digits(unsigned char bcd)
  32. {
  33. return (bcd & 0x0F) <= 9 && ((bcd >> 4) & 0x0F) <= 9;
  34. }
  35. static unsigned int rtc_decode(unsigned char v, int bcd)
  36. {
  37. if (!bcd)
  38. return v;
  39. return (unsigned int)((v >> 4) * 10 + (v & 0x0F));
  40. }
  41. static unsigned char rtc_encode(unsigned int v, int bcd)
  42. {
  43. if (!bcd)
  44. return (unsigned char)v;
  45. return (unsigned char)(((v / 10) << 4) | (v % 10));
  46. }
  47. static int digits_ok(unsigned char v, int bcd)
  48. {
  49. return !bcd || bcd_digits(v);
  50. }
  51. /* The RTC's update-in-progress bit stays set for about 2 ms. */
  52. static int rtc_wait_ready(void)
  53. {
  54. unsigned int n = 0;
  55. while ((cmos_read(RTC_REGA) & 0x80) && ++n < 10000)
  56. ;
  57. return (cmos_read(RTC_REGA) & 0x80) ? -1 : 0;
  58. }
  59. static unsigned int days_in_month(unsigned int y, unsigned int m)
  60. {
  61. static const unsigned char dm[12] = {
  62. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  63. };
  64. if (m < 1 || m > 12)
  65. return 0;
  66. if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
  67. return 29;
  68. return dm[m - 1];
  69. }
  70. /* Sakamoto's algorithm; 0 = Sunday. */
  71. static unsigned int weekday_of(unsigned int y, unsigned int m, unsigned int d)
  72. {
  73. static const unsigned char t[12] = {
  74. 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
  75. };
  76. if (m < 3)
  77. y -= 1;
  78. return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
  79. }
  80. /* The RTC only stores two digits. A dead CMOS battery commonly leaves 1980
  81. * or 1990, so decode with the usual 1970-2069 window instead of assuming
  82. * 20xx: 70..99 -> 19xx, 00..69 -> 20xx. This keeps pre-2000 defaults in the
  83. * past and still maps the current date correctly. */
  84. static unsigned int expand_year(unsigned int yy)
  85. {
  86. return (yy >= 70) ? 1900 + yy : 2000 + yy;
  87. }
  88. struct rtc_raw {
  89. unsigned char sec, min, hour, day, mon, year;
  90. };
  91. static void rtc_read_raw(struct rtc_raw *r)
  92. {
  93. r->sec = cmos_read(RTC_SEC);
  94. r->min = cmos_read(RTC_MIN);
  95. r->hour = cmos_read(RTC_HOUR);
  96. r->day = cmos_read(RTC_DAY);
  97. r->mon = cmos_read(RTC_MON);
  98. r->year = cmos_read(RTC_YEAR);
  99. }
  100. /* Read all fields twice and require the raw bytes to agree, so a read that
  101. * straddles an update is retried rather than decoded as a torn value.
  102. * Register B selects BCD/binary and 12/24-hour mode; both are honoured. */
  103. static int rtc_read_fields(struct sys_datetime *out)
  104. {
  105. struct rtc_raw a, b;
  106. unsigned char regb;
  107. unsigned int tries;
  108. regb = cmos_read(RTC_REGB);
  109. for (tries = 0; tries < 4; tries++) {
  110. int is_bcd = !(regb & 0x04);
  111. /* Register B bit 1 is the 24-hour select: 1 = 24-hour,
  112. * 0 = 12-hour with bit 7 of the hour as the PM flag. */
  113. int is_12h = (regb & 0x02) == 0;
  114. unsigned int sec, min, hour, day, mon, yy, year;
  115. unsigned int pm;
  116. if (rtc_wait_ready() != 0)
  117. continue;
  118. rtc_read_raw(&a);
  119. if (rtc_wait_ready() != 0)
  120. continue;
  121. rtc_read_raw(&b);
  122. if (a.sec != b.sec || a.min != b.min || a.hour != b.hour ||
  123. a.day != b.day || a.mon != b.mon || a.year != b.year)
  124. continue;
  125. if (!digits_ok(a.sec, is_bcd) ||
  126. !digits_ok(a.min, is_bcd) ||
  127. !digits_ok((unsigned char)(a.hour & 0x7F), is_bcd) ||
  128. !digits_ok(a.day, is_bcd) ||
  129. !digits_ok(a.mon, is_bcd) ||
  130. !digits_ok(a.year, is_bcd))
  131. continue;
  132. sec = rtc_decode(a.sec, is_bcd);
  133. min = rtc_decode(a.min, is_bcd);
  134. pm = (is_12h && (a.hour & 0x80)) ? 1 : 0;
  135. hour = rtc_decode((unsigned char)(a.hour & 0x7F), is_bcd);
  136. if (is_12h) {
  137. if (hour == 12)
  138. hour = pm ? 12 : 0;
  139. else if (pm)
  140. hour += 12;
  141. }
  142. day = rtc_decode(a.day, is_bcd);
  143. mon = rtc_decode(a.mon, is_bcd);
  144. yy = rtc_decode(a.year, is_bcd);
  145. year = expand_year(yy);
  146. if (sec > 59 || min > 59 || hour > 23 ||
  147. mon < 1 || mon > 12 || yy > 99 ||
  148. day < 1 || day > days_in_month(year, mon))
  149. continue;
  150. out->year = year;
  151. out->month = mon;
  152. out->day = day;
  153. out->hour = hour;
  154. out->minute = min;
  155. out->second = sec;
  156. out->wday = weekday_of(year, mon, day);
  157. return 0;
  158. }
  159. return -1;
  160. }
  161. void rtc_read_datetime_full(struct sys_datetime *out)
  162. {
  163. if (!out)
  164. return;
  165. if (rtc_read_fields(out) == 0) {
  166. last_dt = *out;
  167. return;
  168. }
  169. *out = last_dt;
  170. }
  171. void rtc_read_datetime(char *buf)
  172. {
  173. struct sys_datetime dt;
  174. unsigned int y, mo, d, h, m, s;
  175. rtc_read_datetime_full(&dt);
  176. y = dt.year;
  177. mo = dt.month;
  178. d = dt.day;
  179. h = dt.hour;
  180. m = dt.minute;
  181. s = dt.second;
  182. buf[0] = (char)('0' + (y / 1000));
  183. buf[1] = (char)('0' + ((y / 100) % 10));
  184. buf[2] = (char)('0' + ((y / 10) % 10));
  185. buf[3] = (char)('0' + (y % 10));
  186. buf[4] = '-';
  187. buf[5] = (char)('0' + (mo / 10));
  188. buf[6] = (char)('0' + (mo % 10));
  189. buf[7] = '-';
  190. buf[8] = (char)('0' + (d / 10));
  191. buf[9] = (char)('0' + (d % 10));
  192. buf[10] = ' ';
  193. buf[11] = (char)('0' + (h / 10));
  194. buf[12] = (char)('0' + (h % 10));
  195. buf[13] = ':';
  196. buf[14] = (char)('0' + (m / 10));
  197. buf[15] = (char)('0' + (m % 10));
  198. buf[16] = ':';
  199. buf[17] = (char)('0' + (s / 10));
  200. buf[18] = (char)('0' + (s % 10));
  201. buf[19] = '\0';
  202. }
  203. void rtc_read_hms(unsigned int *hour, unsigned int *minute, unsigned int *second)
  204. {
  205. struct sys_datetime dt;
  206. rtc_read_datetime_full(&dt);
  207. if (hour)
  208. *hour = dt.hour;
  209. if (minute)
  210. *minute = dt.minute;
  211. if (second)
  212. *second = dt.second;
  213. }
  214. int rtc_set_datetime(const struct sys_datetime *in)
  215. {
  216. unsigned int year, mon, day, hour, min, sec, wday, yy, century_reg;
  217. unsigned char regb, hour_raw;
  218. int is_bcd, is_12h;
  219. if (!in)
  220. return -1;
  221. year = in->year;
  222. mon = in->month;
  223. day = in->day;
  224. hour = in->hour;
  225. min = in->minute;
  226. sec = in->second;
  227. if (year < 1970 || year > 2069 ||
  228. mon < 1 || mon > 12 ||
  229. day < 1 || day > days_in_month(year, mon) ||
  230. hour > 23 || min > 59 || sec > 59)
  231. return -1;
  232. /* Read register B before the SET sequence so the RTC's BCD/binary and
  233. * 12/24-hour mode survive the write. */
  234. regb = cmos_read(RTC_REGB);
  235. if (rtc_wait_ready() != 0)
  236. return -1;
  237. is_bcd = !(regb & 0x04);
  238. is_12h = (regb & 0x02) == 0;
  239. if (is_12h) {
  240. unsigned int h12, pm = 0;
  241. if (hour == 0) {
  242. h12 = 12;
  243. } else if (hour < 12) {
  244. h12 = hour;
  245. } else if (hour == 12) {
  246. h12 = 12;
  247. pm = 1;
  248. } else {
  249. h12 = hour - 12;
  250. pm = 1;
  251. }
  252. hour_raw = (unsigned char)(rtc_encode(h12, is_bcd) |
  253. (pm ? 0x80 : 0));
  254. } else {
  255. hour_raw = rtc_encode(hour, is_bcd);
  256. }
  257. wday = weekday_of(year, mon, day);
  258. yy = year % 100;
  259. /* The FADT points at the RTC century byte on ACPI machines. Writing it
  260. * keeps the two-digit year in the intended century for the firmware and
  261. * any other OS; old non-ACPI boards do not get an arbitrary NVRAM write,
  262. * and the kernel's 1970-2069 read window still gives 19xx years. */
  263. century_reg = acpi_rtc_century_reg();
  264. cmos_write(RTC_REGB, (unsigned char)(regb | 0x80));
  265. cmos_write(RTC_SEC, rtc_encode(sec, is_bcd));
  266. cmos_write(RTC_MIN, rtc_encode(min, is_bcd));
  267. cmos_write(RTC_HOUR, hour_raw);
  268. cmos_write(RTC_WDAY, rtc_encode(wday + 1, is_bcd));
  269. cmos_write(RTC_DAY, rtc_encode(day, is_bcd));
  270. cmos_write(RTC_MON, rtc_encode(mon, is_bcd));
  271. cmos_write(RTC_YEAR, rtc_encode(yy, is_bcd));
  272. if (century_reg)
  273. cmos_write((unsigned char)century_reg,
  274. rtc_encode(year / 100, is_bcd));
  275. cmos_write(RTC_REGB, (unsigned char)(regb & 0x7F));
  276. last_dt.year = year;
  277. last_dt.month = mon;
  278. last_dt.day = day;
  279. last_dt.hour = hour;
  280. last_dt.minute = min;
  281. last_dt.second = sec;
  282. last_dt.wday = wday;
  283. return 0;
  284. }