rtc-pl030.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/rtc/rtc-pl030.c
  4. *
  5. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/rtc.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/amba/bus.h>
  12. #include <linux/io.h>
  13. #include <linux/slab.h>
  14. #define RTC_DR (0)
  15. #define RTC_MR (4)
  16. #define RTC_STAT (8)
  17. #define RTC_EOI (8)
  18. #define RTC_LR (12)
  19. #define RTC_CR (16)
  20. #define RTC_CR_MIE (1 << 0)
  21. struct pl030_rtc {
  22. struct rtc_device *rtc;
  23. void __iomem *base;
  24. };
  25. static irqreturn_t pl030_interrupt(int irq, void *dev_id)
  26. {
  27. struct pl030_rtc *rtc = dev_id;
  28. writel(0, rtc->base + RTC_EOI);
  29. return IRQ_HANDLED;
  30. }
  31. static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  32. {
  33. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  34. rtc_time64_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
  35. return 0;
  36. }
  37. static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  38. {
  39. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  40. writel(rtc_tm_to_time64(&alrm->time), rtc->base + RTC_MR);
  41. return 0;
  42. }
  43. static int pl030_read_time(struct device *dev, struct rtc_time *tm)
  44. {
  45. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  46. rtc_time64_to_tm(readl(rtc->base + RTC_DR), tm);
  47. return 0;
  48. }
  49. /*
  50. * Set the RTC time. Unfortunately, we can't accurately set
  51. * the point at which the counter updates.
  52. *
  53. * Also, since RTC_LR is transferred to RTC_CR on next rising
  54. * edge of the 1Hz clock, we must write the time one second
  55. * in advance.
  56. */
  57. static int pl030_set_time(struct device *dev, struct rtc_time *tm)
  58. {
  59. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  60. writel(rtc_tm_to_time64(tm) + 1, rtc->base + RTC_LR);
  61. return 0;
  62. }
  63. static const struct rtc_class_ops pl030_ops = {
  64. .read_time = pl030_read_time,
  65. .set_time = pl030_set_time,
  66. .read_alarm = pl030_read_alarm,
  67. .set_alarm = pl030_set_alarm,
  68. };
  69. static int pl030_probe(struct amba_device *dev, const struct amba_id *id)
  70. {
  71. struct pl030_rtc *rtc;
  72. int ret;
  73. ret = amba_request_regions(dev, NULL);
  74. if (ret)
  75. goto err_req;
  76. rtc = devm_kzalloc(&dev->dev, sizeof(*rtc), GFP_KERNEL);
  77. if (!rtc) {
  78. ret = -ENOMEM;
  79. goto err_rtc;
  80. }
  81. rtc->rtc = devm_rtc_allocate_device(&dev->dev);
  82. if (IS_ERR(rtc->rtc)) {
  83. ret = PTR_ERR(rtc->rtc);
  84. goto err_rtc;
  85. }
  86. rtc->rtc->ops = &pl030_ops;
  87. rtc->rtc->range_max = U32_MAX;
  88. rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
  89. if (!rtc->base) {
  90. ret = -ENOMEM;
  91. goto err_rtc;
  92. }
  93. __raw_writel(0, rtc->base + RTC_CR);
  94. __raw_writel(0, rtc->base + RTC_EOI);
  95. amba_set_drvdata(dev, rtc);
  96. ret = request_irq(dev->irq[0], pl030_interrupt, 0,
  97. "rtc-pl030", rtc);
  98. if (ret)
  99. goto err_irq;
  100. ret = rtc_register_device(rtc->rtc);
  101. if (ret)
  102. goto err_reg;
  103. return 0;
  104. err_reg:
  105. free_irq(dev->irq[0], rtc);
  106. err_irq:
  107. iounmap(rtc->base);
  108. err_rtc:
  109. amba_release_regions(dev);
  110. err_req:
  111. return ret;
  112. }
  113. static void pl030_remove(struct amba_device *dev)
  114. {
  115. struct pl030_rtc *rtc = amba_get_drvdata(dev);
  116. writel(0, rtc->base + RTC_CR);
  117. free_irq(dev->irq[0], rtc);
  118. iounmap(rtc->base);
  119. amba_release_regions(dev);
  120. }
  121. static struct amba_id pl030_ids[] = {
  122. {
  123. .id = 0x00041030,
  124. .mask = 0x000fffff,
  125. },
  126. { 0, 0 },
  127. };
  128. MODULE_DEVICE_TABLE(amba, pl030_ids);
  129. static struct amba_driver pl030_driver = {
  130. .drv = {
  131. .name = "rtc-pl030",
  132. },
  133. .probe = pl030_probe,
  134. .remove = pl030_remove,
  135. .id_table = pl030_ids,
  136. };
  137. module_amba_driver(pl030_driver);
  138. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  139. MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
  140. MODULE_LICENSE("GPL");