hctosys.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * RTC subsystem, initialize system time on startup
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/rtc.h>
  12. /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
  13. * whether it stores the most close value or the value with partial
  14. * seconds truncated. However, it is important that we use it to store
  15. * the truncated value. This is because otherwise it is necessary,
  16. * in an rtc sync function, to read both xtime.tv_sec and
  17. * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
  18. * of >32bits is not possible. So storing the most close value would
  19. * slow down the sync API. So here we have the truncated value and
  20. * the best guess is to add 0.5s.
  21. */
  22. static int __init rtc_hctosys(void)
  23. {
  24. int err;
  25. struct rtc_time tm;
  26. struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
  27. if (rtc == NULL) {
  28. printk("%s: unable to open rtc device (%s)\n",
  29. __FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
  30. return -ENODEV;
  31. }
  32. err = rtc_read_time(rtc, &tm);
  33. if (err == 0) {
  34. err = rtc_valid_tm(&tm);
  35. if (err == 0) {
  36. struct timespec tv;
  37. tv.tv_nsec = NSEC_PER_SEC >> 1;
  38. rtc_tm_to_time(&tm, &tv.tv_sec);
  39. do_settimeofday(&tv);
  40. dev_info(rtc->dev.parent,
  41. "setting system clock to "
  42. "%d-%02d-%02d %02d:%02d:%02d UTC (%u)\n",
  43. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  44. tm.tm_hour, tm.tm_min, tm.tm_sec,
  45. (unsigned int) tv.tv_sec);
  46. }
  47. else
  48. dev_err(rtc->dev.parent,
  49. "hctosys: invalid date/time\n");
  50. }
  51. else
  52. dev_err(rtc->dev.parent,
  53. "hctosys: unable to read the hardware clock\n");
  54. rtc_class_close(rtc);
  55. return 0;
  56. }
  57. late_initcall(rtc_hctosys);