rtc-pl030.c 3.8 KB

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