rtc-pl030.c 4.2 KB

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