rtc-au1xxx.c 3.1 KB

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