rtc-au1xxx.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Au1xxx counter0 (aka Time-Of-Year counter) RTC interface driver.
  4. *
  5. * Copyright (C) 2008 Manuel Lauss <[email protected]>
  6. */
  7. /* All current Au1xxx SoCs have 2 counters fed by an external 32.768 kHz
  8. * crystal. Counter 0, which keeps counting during sleep/powerdown, is
  9. * used to count seconds since the beginning of the unix epoch.
  10. *
  11. * The counters must be configured and enabled by bootloader/board code;
  12. * no checks as to whether they really get a proper 32.768kHz clock are
  13. * made as this would take far too long.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/rtc.h>
  18. #include <linux/init.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <asm/mach-au1x00/au1000.h>
  22. /* 32kHz clock enabled and detected */
  23. #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  24. static int au1xtoy_rtc_read_time(struct device *dev, struct rtc_time *tm)
  25. {
  26. unsigned long t;
  27. t = alchemy_rdsys(AU1000_SYS_TOYREAD);
  28. rtc_time64_to_tm(t, tm);
  29. return 0;
  30. }
  31. static int au1xtoy_rtc_set_time(struct device *dev, struct rtc_time *tm)
  32. {
  33. unsigned long t;
  34. t = rtc_tm_to_time64(tm);
  35. alchemy_wrsys(t, AU1000_SYS_TOYWRITE);
  36. /* wait for the pending register write to succeed. This can
  37. * take up to 6 seconds...
  38. */
  39. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
  40. msleep(1);
  41. return 0;
  42. }
  43. static const struct rtc_class_ops au1xtoy_rtc_ops = {
  44. .read_time = au1xtoy_rtc_read_time,
  45. .set_time = au1xtoy_rtc_set_time,
  46. };
  47. static int au1xtoy_rtc_probe(struct platform_device *pdev)
  48. {
  49. struct rtc_device *rtcdev;
  50. unsigned long t;
  51. t = alchemy_rdsys(AU1000_SYS_CNTRCTRL);
  52. if (!(t & CNTR_OK)) {
  53. dev_err(&pdev->dev, "counters not working; aborting.\n");
  54. return -ENODEV;
  55. }
  56. /* set counter0 tickrate to 1Hz if necessary */
  57. if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767) {
  58. /* wait until hardware gives access to TRIM register */
  59. t = 0x00100000;
  60. while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T0S) && --t)
  61. msleep(1);
  62. if (!t) {
  63. /* timed out waiting for register access; assume
  64. * counters are unusable.
  65. */
  66. dev_err(&pdev->dev, "timeout waiting for access\n");
  67. return -ETIMEDOUT;
  68. }
  69. /* set 1Hz TOY tick rate */
  70. alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
  71. }
  72. /* wait until the hardware allows writes to the counter reg */
  73. while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
  74. msleep(1);
  75. rtcdev = devm_rtc_allocate_device(&pdev->dev);
  76. if (IS_ERR(rtcdev))
  77. return PTR_ERR(rtcdev);
  78. rtcdev->ops = &au1xtoy_rtc_ops;
  79. rtcdev->range_max = U32_MAX;
  80. platform_set_drvdata(pdev, rtcdev);
  81. return devm_rtc_register_device(rtcdev);
  82. }
  83. static struct platform_driver au1xrtc_driver = {
  84. .driver = {
  85. .name = "rtc-au1xxx",
  86. },
  87. };
  88. module_platform_driver_probe(au1xrtc_driver, au1xtoy_rtc_probe);
  89. MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
  90. MODULE_AUTHOR("Manuel Lauss <[email protected]>");
  91. MODULE_LICENSE("GPL");
  92. MODULE_ALIAS("platform:rtc-au1xxx");