time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 void rtc_read_alarm(struct rtc_wkalrm *alrm)
  38. {
  39. rtc_time_to_tm(readl(rtc_base + RTC_MR), &alrm->time);
  40. }
  41. static int rtc_set_alarm(struct rtc_wkalrm *alrm)
  42. {
  43. unsigned long time;
  44. int ret;
  45. ret = rtc_tm_to_time(&alrm->time, &time);
  46. if (ret == 0)
  47. writel(time, rtc_base + RTC_MR);
  48. return ret;
  49. }
  50. static void rtc_read_time(struct rtc_time *tm)
  51. {
  52. rtc_time_to_tm(readl(rtc_base + RTC_DR), tm);
  53. }
  54. /*
  55. * Set the RTC time. Unfortunately, we can't accurately set
  56. * the point at which the counter updates.
  57. *
  58. * Also, since RTC_LR is transferred to RTC_CR on next rising
  59. * edge of the 1Hz clock, we must write the time one second
  60. * in advance.
  61. */
  62. static int rtc_set_time(struct rtc_time *tm)
  63. {
  64. unsigned long time;
  65. int ret;
  66. ret = rtc_tm_to_time(tm, &time);
  67. if (ret == 0)
  68. writel(time + 1, rtc_base + RTC_LR);
  69. return ret;
  70. }
  71. static struct rtc_ops rtc_ops = {
  72. .owner = THIS_MODULE,
  73. .read_time = rtc_read_time,
  74. .set_time = rtc_set_time,
  75. .read_alarm = rtc_read_alarm,
  76. .set_alarm = rtc_set_alarm,
  77. };
  78. static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  79. {
  80. writel(0, rtc_base + RTC_EOI);
  81. return IRQ_HANDLED;
  82. }
  83. static int rtc_probe(struct amba_device *dev, void *id)
  84. {
  85. int ret;
  86. if (rtc_base)
  87. return -EBUSY;
  88. ret = amba_request_regions(dev, NULL);
  89. if (ret)
  90. goto out;
  91. rtc_base = ioremap(dev->res.start, SZ_4K);
  92. if (!rtc_base) {
  93. ret = -ENOMEM;
  94. goto res_out;
  95. }
  96. __raw_writel(0, rtc_base + RTC_CR);
  97. __raw_writel(0, rtc_base + RTC_EOI);
  98. xtime.tv_sec = __raw_readl(rtc_base + RTC_DR);
  99. ret = request_irq(dev->irq[0], rtc_interrupt, SA_INTERRUPT,
  100. "rtc-pl030", dev);
  101. if (ret)
  102. goto map_out;
  103. ret = register_rtc(&rtc_ops);
  104. if (ret)
  105. goto irq_out;
  106. set_rtc = integrator_set_rtc;
  107. return 0;
  108. irq_out:
  109. free_irq(dev->irq[0], dev);
  110. map_out:
  111. iounmap(rtc_base);
  112. rtc_base = NULL;
  113. res_out:
  114. amba_release_regions(dev);
  115. out:
  116. return ret;
  117. }
  118. static int rtc_remove(struct amba_device *dev)
  119. {
  120. set_rtc = NULL;
  121. writel(0, rtc_base + RTC_CR);
  122. free_irq(dev->irq[0], dev);
  123. unregister_rtc(&rtc_ops);
  124. iounmap(rtc_base);
  125. rtc_base = NULL;
  126. amba_release_regions(dev);
  127. return 0;
  128. }
  129. static struct timespec rtc_delta;
  130. static int rtc_suspend(struct amba_device *dev, pm_message_t state)
  131. {
  132. struct timespec rtc;
  133. rtc.tv_sec = readl(rtc_base + RTC_DR);
  134. rtc.tv_nsec = 0;
  135. save_time_delta(&rtc_delta, &rtc);
  136. return 0;
  137. }
  138. static int rtc_resume(struct amba_device *dev)
  139. {
  140. struct timespec rtc;
  141. rtc.tv_sec = readl(rtc_base + RTC_DR);
  142. rtc.tv_nsec = 0;
  143. restore_time_delta(&rtc_delta, &rtc);
  144. return 0;
  145. }
  146. static struct amba_id rtc_ids[] = {
  147. {
  148. .id = 0x00041030,
  149. .mask = 0x000fffff,
  150. },
  151. { 0, 0 },
  152. };
  153. static struct amba_driver rtc_driver = {
  154. .drv = {
  155. .name = "rtc-pl030",
  156. },
  157. .probe = rtc_probe,
  158. .remove = rtc_remove,
  159. .suspend = rtc_suspend,
  160. .resume = rtc_resume,
  161. .id_table = rtc_ids,
  162. };
  163. static int __init integrator_rtc_init(void)
  164. {
  165. return amba_driver_register(&rtc_driver);
  166. }
  167. static void __exit integrator_rtc_exit(void)
  168. {
  169. amba_driver_unregister(&rtc_driver);
  170. }
  171. module_init(integrator_rtc_init);
  172. module_exit(integrator_rtc_exit);