time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <asm/hardware/amba.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 rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  86. {
  87. writel(0, rtc_base + RTC_EOI);
  88. return IRQ_HANDLED;
  89. }
  90. static int rtc_probe(struct amba_device *dev, void *id)
  91. {
  92. int ret;
  93. if (rtc_base)
  94. return -EBUSY;
  95. ret = amba_request_regions(dev, NULL);
  96. if (ret)
  97. goto out;
  98. rtc_base = ioremap(dev->res.start, SZ_4K);
  99. if (!rtc_base) {
  100. ret = -ENOMEM;
  101. goto res_out;
  102. }
  103. __raw_writel(0, rtc_base + RTC_CR);
  104. __raw_writel(0, rtc_base + RTC_EOI);
  105. xtime.tv_sec = __raw_readl(rtc_base + RTC_DR);
  106. ret = request_irq(dev->irq[0], rtc_interrupt, SA_INTERRUPT,
  107. "rtc-pl030", dev);
  108. if (ret)
  109. goto map_out;
  110. ret = register_rtc(&rtc_ops);
  111. if (ret)
  112. goto irq_out;
  113. set_rtc = integrator_set_rtc;
  114. return 0;
  115. irq_out:
  116. free_irq(dev->irq[0], dev);
  117. map_out:
  118. iounmap(rtc_base);
  119. rtc_base = NULL;
  120. res_out:
  121. amba_release_regions(dev);
  122. out:
  123. return ret;
  124. }
  125. static int rtc_remove(struct amba_device *dev)
  126. {
  127. set_rtc = NULL;
  128. writel(0, rtc_base + RTC_CR);
  129. free_irq(dev->irq[0], dev);
  130. unregister_rtc(&rtc_ops);
  131. iounmap(rtc_base);
  132. rtc_base = NULL;
  133. amba_release_regions(dev);
  134. return 0;
  135. }
  136. static struct timespec rtc_delta;
  137. static int rtc_suspend(struct amba_device *dev, pm_message_t state)
  138. {
  139. struct timespec rtc;
  140. rtc.tv_sec = readl(rtc_base + RTC_DR);
  141. rtc.tv_nsec = 0;
  142. save_time_delta(&rtc_delta, &rtc);
  143. return 0;
  144. }
  145. static int rtc_resume(struct amba_device *dev)
  146. {
  147. struct timespec rtc;
  148. rtc.tv_sec = readl(rtc_base + RTC_DR);
  149. rtc.tv_nsec = 0;
  150. restore_time_delta(&rtc_delta, &rtc);
  151. return 0;
  152. }
  153. static struct amba_id rtc_ids[] = {
  154. {
  155. .id = 0x00041030,
  156. .mask = 0x000fffff,
  157. },
  158. { 0, 0 },
  159. };
  160. static struct amba_driver rtc_driver = {
  161. .drv = {
  162. .name = "rtc-pl030",
  163. },
  164. .probe = rtc_probe,
  165. .remove = rtc_remove,
  166. .suspend = rtc_suspend,
  167. .resume = rtc_resume,
  168. .id_table = rtc_ids,
  169. };
  170. static int __init integrator_rtc_init(void)
  171. {
  172. return amba_driver_register(&rtc_driver);
  173. }
  174. static void __exit integrator_rtc_exit(void)
  175. {
  176. amba_driver_unregister(&rtc_driver);
  177. }
  178. module_init(integrator_rtc_init);
  179. module_exit(integrator_rtc_exit);