hctosys.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/rtc.h>
  13. /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
  14. * whether it stores the most close value or the value with partial
  15. * seconds truncated. However, it is important that we use it to store
  16. * the truncated value. This is because otherwise it is necessary,
  17. * in an rtc sync function, to read both xtime.tv_sec and
  18. * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
  19. * of >32bits is not possible. So storing the most close value would
  20. * slow down the sync API. So here we have the truncated value and
  21. * the best guess is to add 0.5s.
  22. */
  23. static int __init rtc_hctosys(void)
  24. {
  25. int err = -ENODEV;
  26. struct rtc_time tm;
  27. struct timespec64 tv64 = {
  28. .tv_nsec = NSEC_PER_SEC >> 1,
  29. };
  30. struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
  31. if (rtc == NULL) {
  32. pr_info("unable to open rtc device (%s)\n",
  33. CONFIG_RTC_HCTOSYS_DEVICE);
  34. goto err_open;
  35. }
  36. err = rtc_read_time(rtc, &tm);
  37. if (err) {
  38. dev_err(rtc->dev.parent,
  39. "hctosys: unable to read the hardware clock\n");
  40. goto err_read;
  41. }
  42. tv64.tv_sec = rtc_tm_to_time64(&tm);
  43. #if BITS_PER_LONG == 32
  44. if (tv64.tv_sec > INT_MAX) {
  45. err = -ERANGE;
  46. goto err_read;
  47. }
  48. #endif
  49. err = do_settimeofday64(&tv64);
  50. dev_info(rtc->dev.parent,
  51. "setting system clock to "
  52. "%d-%02d-%02d %02d:%02d:%02d UTC (%lld)\n",
  53. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  54. tm.tm_hour, tm.tm_min, tm.tm_sec,
  55. (long long) tv64.tv_sec);
  56. err_read:
  57. rtc_class_close(rtc);
  58. err_open:
  59. rtc_hctosys_ret = err;
  60. return err;
  61. }
  62. late_initcall(rtc_hctosys);