rtc-pl030.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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->rtc = devm_rtc_allocate_device(&dev->dev);
  98. if (IS_ERR(rtc->rtc)) {
  99. ret = PTR_ERR(rtc->rtc);
  100. goto err_rtc;
  101. }
  102. rtc->rtc->ops = &pl030_ops;
  103. rtc->base = ioremap(dev->res.start, resource_size(&dev->res));
  104. if (!rtc->base) {
  105. ret = -ENOMEM;
  106. goto err_rtc;
  107. }
  108. __raw_writel(0, rtc->base + RTC_CR);
  109. __raw_writel(0, rtc->base + RTC_EOI);
  110. amba_set_drvdata(dev, rtc);
  111. ret = request_irq(dev->irq[0], pl030_interrupt, 0,
  112. "rtc-pl030", rtc);
  113. if (ret)
  114. goto err_irq;
  115. ret = rtc_register_device(rtc->rtc);
  116. if (ret)
  117. goto err_reg;
  118. return 0;
  119. err_reg:
  120. free_irq(dev->irq[0], rtc);
  121. err_irq:
  122. iounmap(rtc->base);
  123. err_rtc:
  124. amba_release_regions(dev);
  125. err_req:
  126. return ret;
  127. }
  128. static int pl030_remove(struct amba_device *dev)
  129. {
  130. struct pl030_rtc *rtc = amba_get_drvdata(dev);
  131. writel(0, rtc->base + RTC_CR);
  132. free_irq(dev->irq[0], rtc);
  133. iounmap(rtc->base);
  134. amba_release_regions(dev);
  135. return 0;
  136. }
  137. static struct amba_id pl030_ids[] = {
  138. {
  139. .id = 0x00041030,
  140. .mask = 0x000fffff,
  141. },
  142. { 0, 0 },
  143. };
  144. MODULE_DEVICE_TABLE(amba, pl030_ids);
  145. static struct amba_driver pl030_driver = {
  146. .drv = {
  147. .name = "rtc-pl030",
  148. },
  149. .probe = pl030_probe,
  150. .remove = pl030_remove,
  151. .id_table = pl030_ids,
  152. };
  153. module_amba_driver(pl030_driver);
  154. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  155. MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
  156. MODULE_LICENSE("GPL");