rtc-gemini.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Gemini OnChip RTC
  3. *
  4. * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Original code for older kernel 2.6.15 are from Stormlinksemi
  17. * first update from Janos Laube for > 2.6.29 kernels
  18. *
  19. * checkpatch fixes and usage of rtc-lib code
  20. * Hans Ulli Kroll <ulli.kroll@googlemail.com>
  21. */
  22. #include <linux/rtc.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #define DRV_NAME "rtc-gemini"
  29. MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
  30. MODULE_DESCRIPTION("RTC driver for Gemini SoC");
  31. MODULE_LICENSE("GPL");
  32. MODULE_ALIAS("platform:" DRV_NAME);
  33. struct gemini_rtc {
  34. struct rtc_device *rtc_dev;
  35. void __iomem *rtc_base;
  36. int rtc_irq;
  37. };
  38. enum gemini_rtc_offsets {
  39. GEMINI_RTC_SECOND = 0x00,
  40. GEMINI_RTC_MINUTE = 0x04,
  41. GEMINI_RTC_HOUR = 0x08,
  42. GEMINI_RTC_DAYS = 0x0C,
  43. GEMINI_RTC_ALARM_SECOND = 0x10,
  44. GEMINI_RTC_ALARM_MINUTE = 0x14,
  45. GEMINI_RTC_ALARM_HOUR = 0x18,
  46. GEMINI_RTC_RECORD = 0x1C,
  47. GEMINI_RTC_CR = 0x20
  48. };
  49. static irqreturn_t gemini_rtc_interrupt(int irq, void *dev)
  50. {
  51. return IRQ_HANDLED;
  52. }
  53. /*
  54. * Looks like the RTC in the Gemini SoC is (totaly) broken
  55. * We can't read/write directly the time from RTC registers.
  56. * We must do some "offset" calculation to get the real time
  57. *
  58. * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
  59. * the same thing, without the rtc-lib.c calls.
  60. */
  61. static int gemini_rtc_read_time(struct device *dev, struct rtc_time *tm)
  62. {
  63. struct gemini_rtc *rtc = dev_get_drvdata(dev);
  64. unsigned int days, hour, min, sec;
  65. unsigned long offset, time;
  66. sec = readl(rtc->rtc_base + GEMINI_RTC_SECOND);
  67. min = readl(rtc->rtc_base + GEMINI_RTC_MINUTE);
  68. hour = readl(rtc->rtc_base + GEMINI_RTC_HOUR);
  69. days = readl(rtc->rtc_base + GEMINI_RTC_DAYS);
  70. offset = readl(rtc->rtc_base + GEMINI_RTC_RECORD);
  71. time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
  72. rtc_time_to_tm(time, tm);
  73. return 0;
  74. }
  75. static int gemini_rtc_set_time(struct device *dev, struct rtc_time *tm)
  76. {
  77. struct gemini_rtc *rtc = dev_get_drvdata(dev);
  78. unsigned int sec, min, hour, day;
  79. unsigned long offset, time;
  80. if (tm->tm_year >= 2148) /* EPOCH Year + 179 */
  81. return -EINVAL;
  82. rtc_tm_to_time(tm, &time);
  83. sec = readl(rtc->rtc_base + GEMINI_RTC_SECOND);
  84. min = readl(rtc->rtc_base + GEMINI_RTC_MINUTE);
  85. hour = readl(rtc->rtc_base + GEMINI_RTC_HOUR);
  86. day = readl(rtc->rtc_base + GEMINI_RTC_DAYS);
  87. offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
  88. writel(offset, rtc->rtc_base + GEMINI_RTC_RECORD);
  89. writel(0x01, rtc->rtc_base + GEMINI_RTC_CR);
  90. return 0;
  91. }
  92. static const struct rtc_class_ops gemini_rtc_ops = {
  93. .read_time = gemini_rtc_read_time,
  94. .set_time = gemini_rtc_set_time,
  95. };
  96. static int gemini_rtc_probe(struct platform_device *pdev)
  97. {
  98. struct gemini_rtc *rtc;
  99. struct device *dev = &pdev->dev;
  100. struct resource *res;
  101. int ret;
  102. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  103. if (unlikely(!rtc))
  104. return -ENOMEM;
  105. platform_set_drvdata(pdev, rtc);
  106. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  107. if (!res)
  108. return -ENODEV;
  109. rtc->rtc_irq = res->start;
  110. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. if (!res)
  112. return -ENODEV;
  113. rtc->rtc_base = devm_ioremap(dev, res->start,
  114. resource_size(res));
  115. ret = devm_request_irq(dev, rtc->rtc_irq, gemini_rtc_interrupt,
  116. IRQF_SHARED, pdev->name, dev);
  117. if (unlikely(ret))
  118. return ret;
  119. rtc->rtc_dev = rtc_device_register(pdev->name, dev,
  120. &gemini_rtc_ops, THIS_MODULE);
  121. return PTR_ERR_OR_ZERO(rtc->rtc_dev);
  122. }
  123. static int gemini_rtc_remove(struct platform_device *pdev)
  124. {
  125. struct gemini_rtc *rtc = platform_get_drvdata(pdev);
  126. rtc_device_unregister(rtc->rtc_dev);
  127. return 0;
  128. }
  129. static struct platform_driver gemini_rtc_driver = {
  130. .driver = {
  131. .name = DRV_NAME,
  132. },
  133. .probe = gemini_rtc_probe,
  134. .remove = gemini_rtc_remove,
  135. };
  136. module_platform_driver_probe(gemini_rtc_driver, gemini_rtc_probe);