rtc-gemini.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #define DRV_VERSION "0.2"
  30. MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
  31. MODULE_DESCRIPTION("RTC driver for Gemini SoC");
  32. MODULE_LICENSE("GPL");
  33. MODULE_ALIAS("platform:" DRV_NAME);
  34. struct gemini_rtc {
  35. struct rtc_device *rtc_dev;
  36. void __iomem *rtc_base;
  37. int rtc_irq;
  38. };
  39. enum gemini_rtc_offsets {
  40. GEMINI_RTC_SECOND = 0x00,
  41. GEMINI_RTC_MINUTE = 0x04,
  42. GEMINI_RTC_HOUR = 0x08,
  43. GEMINI_RTC_DAYS = 0x0C,
  44. GEMINI_RTC_ALARM_SECOND = 0x10,
  45. GEMINI_RTC_ALARM_MINUTE = 0x14,
  46. GEMINI_RTC_ALARM_HOUR = 0x18,
  47. GEMINI_RTC_RECORD = 0x1C,
  48. GEMINI_RTC_CR = 0x20
  49. };
  50. static irqreturn_t gemini_rtc_interrupt(int irq, void *dev)
  51. {
  52. return IRQ_HANDLED;
  53. }
  54. /*
  55. * Looks like the RTC in the Gemini SoC is (totaly) broken
  56. * We can't read/write directly the time from RTC registers.
  57. * We must do some "offset" calculation to get the real time
  58. *
  59. * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
  60. * the same thing, without the rtc-lib.c calls.
  61. */
  62. static int gemini_rtc_read_time(struct device *dev, struct rtc_time *tm)
  63. {
  64. struct gemini_rtc *rtc = dev_get_drvdata(dev);
  65. unsigned int days, hour, min, sec;
  66. unsigned long offset, time;
  67. sec = readl(rtc->rtc_base + GEMINI_RTC_SECOND);
  68. min = readl(rtc->rtc_base + GEMINI_RTC_MINUTE);
  69. hour = readl(rtc->rtc_base + GEMINI_RTC_HOUR);
  70. days = readl(rtc->rtc_base + GEMINI_RTC_DAYS);
  71. offset = readl(rtc->rtc_base + GEMINI_RTC_RECORD);
  72. time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
  73. rtc_time_to_tm(time, tm);
  74. return 0;
  75. }
  76. static int gemini_rtc_set_time(struct device *dev, struct rtc_time *tm)
  77. {
  78. struct gemini_rtc *rtc = dev_get_drvdata(dev);
  79. unsigned int sec, min, hour, day;
  80. unsigned long offset, time;
  81. if (tm->tm_year >= 2148) /* EPOCH Year + 179 */
  82. return -EINVAL;
  83. rtc_tm_to_time(tm, &time);
  84. sec = readl(rtc->rtc_base + GEMINI_RTC_SECOND);
  85. min = readl(rtc->rtc_base + GEMINI_RTC_MINUTE);
  86. hour = readl(rtc->rtc_base + GEMINI_RTC_HOUR);
  87. day = readl(rtc->rtc_base + GEMINI_RTC_DAYS);
  88. offset = time - (day * 86400 + hour * 3600 + min * 60 + sec);
  89. writel(offset, rtc->rtc_base + GEMINI_RTC_RECORD);
  90. writel(0x01, rtc->rtc_base + GEMINI_RTC_CR);
  91. return 0;
  92. }
  93. static struct rtc_class_ops gemini_rtc_ops = {
  94. .read_time = gemini_rtc_read_time,
  95. .set_time = gemini_rtc_set_time,
  96. };
  97. static int gemini_rtc_probe(struct platform_device *pdev)
  98. {
  99. struct gemini_rtc *rtc;
  100. struct device *dev = &pdev->dev;
  101. struct resource *res;
  102. int ret;
  103. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  104. if (unlikely(!rtc))
  105. return -ENOMEM;
  106. platform_set_drvdata(pdev, rtc);
  107. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  108. if (!res)
  109. return -ENODEV;
  110. rtc->rtc_irq = res->start;
  111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. if (!res)
  113. return -ENODEV;
  114. rtc->rtc_base = devm_ioremap(dev, res->start,
  115. resource_size(res));
  116. ret = devm_request_irq(dev, rtc->rtc_irq, gemini_rtc_interrupt,
  117. IRQF_SHARED, pdev->name, dev);
  118. if (unlikely(ret))
  119. return ret;
  120. rtc->rtc_dev = rtc_device_register(pdev->name, dev,
  121. &gemini_rtc_ops, THIS_MODULE);
  122. return PTR_ERR_OR_ZERO(rtc->rtc_dev);
  123. }
  124. static int gemini_rtc_remove(struct platform_device *pdev)
  125. {
  126. struct gemini_rtc *rtc = platform_get_drvdata(pdev);
  127. rtc_device_unregister(rtc->rtc_dev);
  128. platform_set_drvdata(pdev, NULL);
  129. return 0;
  130. }
  131. static struct platform_driver gemini_rtc_driver = {
  132. .driver = {
  133. .name = DRV_NAME,
  134. },
  135. .probe = gemini_rtc_probe,
  136. .remove = gemini_rtc_remove,
  137. };
  138. module_platform_driver_probe(gemini_rtc_driver, gemini_rtc_probe);