rtc-au1xxx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = au_readl(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. au_writel(t, SYS_TOYWRITE);
  39. au_sync();
  40. /* wait for the pending register write to succeed. This can
  41. * take up to 6 seconds...
  42. */
  43. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S)
  44. msleep(1);
  45. return 0;
  46. }
  47. static struct rtc_class_ops au1xtoy_rtc_ops = {
  48. .read_time = au1xtoy_rtc_read_time,
  49. .set_time = au1xtoy_rtc_set_time,
  50. };
  51. static int __devinit au1xtoy_rtc_probe(struct platform_device *pdev)
  52. {
  53. struct rtc_device *rtcdev;
  54. unsigned long t;
  55. int ret;
  56. t = au_readl(SYS_COUNTER_CNTRL);
  57. if (!(t & CNTR_OK)) {
  58. dev_err(&pdev->dev, "counters not working; aborting.\n");
  59. ret = -ENODEV;
  60. goto out_err;
  61. }
  62. ret = -ETIMEDOUT;
  63. /* set counter0 tickrate to 1Hz if necessary */
  64. if (au_readl(SYS_TOYTRIM) != 32767) {
  65. /* wait until hardware gives access to TRIM register */
  66. t = 0x00100000;
  67. while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T0S) && --t)
  68. msleep(1);
  69. if (!t) {
  70. /* timed out waiting for register access; assume
  71. * counters are unusable.
  72. */
  73. dev_err(&pdev->dev, "timeout waiting for access\n");
  74. goto out_err;
  75. }
  76. /* set 1Hz TOY tick rate */
  77. au_writel(32767, SYS_TOYTRIM);
  78. au_sync();
  79. }
  80. /* wait until the hardware allows writes to the counter reg */
  81. while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S)
  82. msleep(1);
  83. rtcdev = rtc_device_register("rtc-au1xxx", &pdev->dev,
  84. &au1xtoy_rtc_ops, THIS_MODULE);
  85. if (IS_ERR(rtcdev)) {
  86. ret = PTR_ERR(rtcdev);
  87. goto out_err;
  88. }
  89. platform_set_drvdata(pdev, rtcdev);
  90. return 0;
  91. out_err:
  92. return ret;
  93. }
  94. static int __devexit au1xtoy_rtc_remove(struct platform_device *pdev)
  95. {
  96. struct rtc_device *rtcdev = platform_get_drvdata(pdev);
  97. rtc_device_unregister(rtcdev);
  98. platform_set_drvdata(pdev, NULL);
  99. return 0;
  100. }
  101. static struct platform_driver au1xrtc_driver = {
  102. .driver = {
  103. .name = "rtc-au1xxx",
  104. .owner = THIS_MODULE,
  105. },
  106. .remove = __devexit_p(au1xtoy_rtc_remove),
  107. };
  108. static int __init au1xtoy_rtc_init(void)
  109. {
  110. return platform_driver_probe(&au1xrtc_driver, au1xtoy_rtc_probe);
  111. }
  112. static void __exit au1xtoy_rtc_exit(void)
  113. {
  114. platform_driver_unregister(&au1xrtc_driver);
  115. }
  116. module_init(au1xtoy_rtc_init);
  117. module_exit(au1xtoy_rtc_exit);
  118. MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
  119. MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");
  120. MODULE_LICENSE("GPL");
  121. MODULE_ALIAS("platform:rtc-au1xxx");