rtc-pl030.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define RTC_DR (0)
  17. #define RTC_MR (4)
  18. #define RTC_STAT (8)
  19. #define RTC_EOI (8)
  20. #define RTC_LR (12)
  21. #define RTC_CR (16)
  22. #define RTC_CR_MIE (1 << 0)
  23. struct pl030_rtc {
  24. struct rtc_device *rtc;
  25. void __iomem *base;
  26. };
  27. static irqreturn_t pl030_interrupt(int irq, void *dev_id)
  28. {
  29. struct pl030_rtc *rtc = dev_id;
  30. writel(0, rtc->base + RTC_EOI);
  31. return IRQ_HANDLED;
  32. }
  33. static int pl030_open(struct device *dev)
  34. {
  35. return 0;
  36. }
  37. static void pl030_release(struct device *dev)
  38. {
  39. }
  40. static int pl030_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  41. {
  42. return -ENOIOCTLCMD;
  43. }
  44. static int pl030_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  45. {
  46. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  47. rtc_time_to_tm(readl(rtc->base + RTC_MR), &alrm->time);
  48. return 0;
  49. }
  50. static int pl030_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  51. {
  52. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  53. unsigned long time;
  54. int ret;
  55. /*
  56. * At the moment, we can only deal with non-wildcarded alarm times.
  57. */
  58. ret = rtc_valid_tm(&alrm->time);
  59. if (ret == 0)
  60. ret = rtc_tm_to_time(&alrm->time, &time);
  61. if (ret == 0)
  62. writel(time, rtc->base + RTC_MR);
  63. return ret;
  64. }
  65. static int pl030_read_time(struct device *dev, struct rtc_time *tm)
  66. {
  67. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  68. rtc_time_to_tm(readl(rtc->base + RTC_DR), tm);
  69. return 0;
  70. }
  71. /*
  72. * Set the RTC time. Unfortunately, we can't accurately set
  73. * the point at which the counter updates.
  74. *
  75. * Also, since RTC_LR is transferred to RTC_CR on next rising
  76. * edge of the 1Hz clock, we must write the time one second
  77. * in advance.
  78. */
  79. static int pl030_set_time(struct device *dev, struct rtc_time *tm)
  80. {
  81. struct pl030_rtc *rtc = dev_get_drvdata(dev);
  82. unsigned long time;
  83. int ret;
  84. ret = rtc_tm_to_time(tm, &time);
  85. if (ret == 0)
  86. writel(time + 1, rtc->base + RTC_LR);
  87. return ret;
  88. }
  89. static const struct rtc_class_ops pl030_ops = {
  90. .open = pl030_open,
  91. .release = pl030_release,
  92. .ioctl = pl030_ioctl,
  93. .read_time = pl030_read_time,
  94. .set_time = pl030_set_time,
  95. .read_alarm = pl030_read_alarm,
  96. .set_alarm = pl030_set_alarm,
  97. };
  98. static int pl030_probe(struct amba_device *dev, void *id)
  99. {
  100. struct pl030_rtc *rtc;
  101. int ret;
  102. ret = amba_request_regions(dev, NULL);
  103. if (ret)
  104. goto err_req;
  105. rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
  106. if (!rtc) {
  107. ret = -ENOMEM;
  108. goto err_rtc;
  109. }
  110. rtc->base = ioremap(dev->res.start, SZ_4K);
  111. if (!rtc->base) {
  112. ret = -ENOMEM;
  113. goto err_map;
  114. }
  115. __raw_writel(0, rtc->base + RTC_CR);
  116. __raw_writel(0, rtc->base + RTC_EOI);
  117. amba_set_drvdata(dev, rtc);
  118. ret = request_irq(dev->irq[0], pl030_interrupt, IRQF_DISABLED,
  119. "rtc-pl030", rtc);
  120. if (ret)
  121. goto err_irq;
  122. rtc->rtc = rtc_device_register("pl030", &dev->dev, &pl030_ops,
  123. THIS_MODULE);
  124. if (IS_ERR(rtc->rtc)) {
  125. ret = PTR_ERR(rtc->rtc);
  126. goto err_reg;
  127. }
  128. return 0;
  129. err_reg:
  130. free_irq(dev->irq[0], rtc);
  131. err_irq:
  132. iounmap(rtc->base);
  133. err_map:
  134. kfree(rtc);
  135. err_rtc:
  136. amba_release_regions(dev);
  137. err_req:
  138. return ret;
  139. }
  140. static int pl030_remove(struct amba_device *dev)
  141. {
  142. struct pl030_rtc *rtc = amba_get_drvdata(dev);
  143. amba_set_drvdata(dev, NULL);
  144. writel(0, rtc->base + RTC_CR);
  145. free_irq(dev->irq[0], rtc);
  146. rtc_device_unregister(rtc->rtc);
  147. iounmap(rtc->base);
  148. kfree(rtc);
  149. amba_release_regions(dev);
  150. return 0;
  151. }
  152. static struct amba_id pl030_ids[] = {
  153. {
  154. .id = 0x00041030,
  155. .mask = 0x000fffff,
  156. },
  157. { 0, 0 },
  158. };
  159. static struct amba_driver pl030_driver = {
  160. .drv = {
  161. .name = "rtc-pl030",
  162. },
  163. .probe = pl030_probe,
  164. .remove = pl030_remove,
  165. .id_table = pl030_ids,
  166. };
  167. static int __init pl030_init(void)
  168. {
  169. return amba_driver_register(&pl030_driver);
  170. }
  171. static void __exit pl030_exit(void)
  172. {
  173. amba_driver_unregister(&pl030_driver);
  174. }
  175. module_init(pl030_init);
  176. module_exit(pl030_exit);
  177. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  178. MODULE_DESCRIPTION("ARM AMBA PL030 RTC Driver");
  179. MODULE_LICENSE("GPL");