time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * linux/arch/arm/mach-integrator/time.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/kernel.h>
  12. #include <linux/time.h>
  13. #include <linux/mc146818rtc.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/amba/bus.h>
  18. #include <asm/hardware.h>
  19. #include <asm/io.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/rtc.h>
  22. #include <asm/mach/time.h>
  23. #define RTC_DR (0)
  24. #define RTC_MR (4)
  25. #define RTC_STAT (8)
  26. #define RTC_EOI (8)
  27. #define RTC_LR (12)
  28. #define RTC_CR (16)
  29. #define RTC_CR_MIE (1 << 0)
  30. extern int (*set_rtc)(void);
  31. static void __iomem *rtc_base;
  32. static int integrator_set_rtc(void)
  33. {
  34. __raw_writel(xtime.tv_sec, rtc_base + RTC_LR);
  35. return 1;
  36. }
  37. static int rtc_read_alarm(struct rtc_wkalrm *alrm)
  38. {
  39. rtc_time_to_tm(readl(rtc_base + RTC_MR), &alrm->time);
  40. return 0;
  41. }
  42. static inline int rtc_set_alarm(struct rtc_wkalrm *alrm)
  43. {
  44. unsigned long time;
  45. int ret;
  46. /*
  47. * At the moment, we can only deal with non-wildcarded alarm times.
  48. */
  49. ret = rtc_valid_tm(&alrm->time);
  50. if (ret == 0)
  51. ret = rtc_tm_to_time(&alrm->time, &time);
  52. if (ret == 0)
  53. writel(time, rtc_base + RTC_MR);
  54. return ret;
  55. }
  56. static int rtc_read_time(struct rtc_time *tm)
  57. {
  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 inline int rtc_set_time(struct rtc_time *tm)
  70. {
  71. unsigned long time;
  72. int ret;
  73. ret = rtc_tm_to_time(tm, &time);
  74. if (ret == 0)
  75. writel(time + 1, rtc_base + RTC_LR);
  76. return ret;
  77. }
  78. static struct rtc_ops rtc_ops = {
  79. .owner = THIS_MODULE,
  80. .read_time = rtc_read_time,
  81. .set_time = rtc_set_time,
  82. .read_alarm = rtc_read_alarm,
  83. .set_alarm = rtc_set_alarm,
  84. };
  85. static irqreturn_t arm_rtc_interrupt(int irq, void *dev_id,
  86. struct pt_regs *regs)
  87. {
  88. writel(0, rtc_base + RTC_EOI);
  89. return IRQ_HANDLED;
  90. }
  91. static int rtc_probe(struct amba_device *dev, void *id)
  92. {
  93. int ret;
  94. if (rtc_base)
  95. return -EBUSY;
  96. ret = amba_request_regions(dev, NULL);
  97. if (ret)
  98. goto out;
  99. rtc_base = ioremap(dev->res.start, SZ_4K);
  100. if (!rtc_base) {
  101. ret = -ENOMEM;
  102. goto res_out;
  103. }
  104. __raw_writel(0, rtc_base + RTC_CR);
  105. __raw_writel(0, rtc_base + RTC_EOI);
  106. xtime.tv_sec = __raw_readl(rtc_base + RTC_DR);
  107. ret = request_irq(dev->irq[0], arm_rtc_interrupt, SA_INTERRUPT,
  108. "rtc-pl030", dev);
  109. if (ret)
  110. goto map_out;
  111. ret = register_rtc(&rtc_ops);
  112. if (ret)
  113. goto irq_out;
  114. set_rtc = integrator_set_rtc;
  115. return 0;
  116. irq_out:
  117. free_irq(dev->irq[0], dev);
  118. map_out:
  119. iounmap(rtc_base);
  120. rtc_base = NULL;
  121. res_out:
  122. amba_release_regions(dev);
  123. out:
  124. return ret;
  125. }
  126. static int rtc_remove(struct amba_device *dev)
  127. {
  128. set_rtc = NULL;
  129. writel(0, rtc_base + RTC_CR);
  130. free_irq(dev->irq[0], dev);
  131. unregister_rtc(&rtc_ops);
  132. iounmap(rtc_base);
  133. rtc_base = NULL;
  134. amba_release_regions(dev);
  135. return 0;
  136. }
  137. static struct timespec rtc_delta;
  138. static int rtc_suspend(struct amba_device *dev, pm_message_t state)
  139. {
  140. struct timespec rtc;
  141. rtc.tv_sec = readl(rtc_base + RTC_DR);
  142. rtc.tv_nsec = 0;
  143. save_time_delta(&rtc_delta, &rtc);
  144. return 0;
  145. }
  146. static int rtc_resume(struct amba_device *dev)
  147. {
  148. struct timespec rtc;
  149. rtc.tv_sec = readl(rtc_base + RTC_DR);
  150. rtc.tv_nsec = 0;
  151. restore_time_delta(&rtc_delta, &rtc);
  152. return 0;
  153. }
  154. static struct amba_id rtc_ids[] = {
  155. {
  156. .id = 0x00041030,
  157. .mask = 0x000fffff,
  158. },
  159. { 0, 0 },
  160. };
  161. static struct amba_driver rtc_driver = {
  162. .drv = {
  163. .name = "rtc-pl030",
  164. },
  165. .probe = rtc_probe,
  166. .remove = rtc_remove,
  167. .suspend = rtc_suspend,
  168. .resume = rtc_resume,
  169. .id_table = rtc_ids,
  170. };
  171. static int __init integrator_rtc_init(void)
  172. {
  173. return amba_driver_register(&rtc_driver);
  174. }
  175. static void __exit integrator_rtc_exit(void)
  176. {
  177. amba_driver_unregister(&rtc_driver);
  178. }
  179. module_init(integrator_rtc_init);
  180. module_exit(integrator_rtc_exit);