rtc-pl030.c 4.1 KB

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