rtc-pl031.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * drivers/rtc/rtc-pl031.c
  3. *
  4. * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC
  5. *
  6. * Author: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright 2006 (c) MontaVista Software, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/platform_device.h>
  16. #include <linux/module.h>
  17. #include <linux/rtc.h>
  18. #include <linux/init.h>
  19. #include <linux/fs.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/string.h>
  22. #include <linux/pm.h>
  23. #include <linux/amba/bus.h>
  24. #include <asm/io.h>
  25. #include <asm/bitops.h>
  26. #include <asm/hardware.h>
  27. #include <asm/irq.h>
  28. #include <asm/rtc.h>
  29. /*
  30. * Register definitions
  31. */
  32. #define RTC_DR 0x00 /* Data read register */
  33. #define RTC_MR 0x04 /* Match register */
  34. #define RTC_LR 0x08 /* Data load register */
  35. #define RTC_CR 0x0c /* Control register */
  36. #define RTC_IMSC 0x10 /* Interrupt mask and set register */
  37. #define RTC_RIS 0x14 /* Raw interrupt status register */
  38. #define RTC_MIS 0x18 /* Masked interrupt status register */
  39. #define RTC_ICR 0x1c /* Interrupt clear register */
  40. struct pl031_local {
  41. struct rtc_device *rtc;
  42. void __iomem *base;
  43. };
  44. static irqreturn_t pl031_interrupt(int irq, void *dev_id)
  45. {
  46. struct rtc_device *rtc = dev_id;
  47. rtc_update_irq(&rtc->class_dev, 1, RTC_AF);
  48. return IRQ_HANDLED;
  49. }
  50. static int pl031_open(struct device *dev)
  51. {
  52. /*
  53. * We request IRQ in pl031_probe, so nothing to do here...
  54. */
  55. return 0;
  56. }
  57. static void pl031_release(struct device *dev)
  58. {
  59. }
  60. static int pl031_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  61. {
  62. struct pl031_local *ldata = dev_get_drvdata(dev);
  63. switch (cmd) {
  64. case RTC_AIE_OFF:
  65. __raw_writel(1, ldata->base + RTC_MIS);
  66. return 0;
  67. case RTC_AIE_ON:
  68. __raw_writel(0, ldata->base + RTC_MIS);
  69. return 0;
  70. }
  71. return -ENOIOCTLCMD;
  72. }
  73. static int pl031_read_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. struct pl031_local *ldata = dev_get_drvdata(dev);
  76. rtc_time_to_tm(__raw_readl(ldata->base + RTC_DR), tm);
  77. return 0;
  78. }
  79. static int pl031_set_time(struct device *dev, struct rtc_time *tm)
  80. {
  81. unsigned long time;
  82. struct pl031_local *ldata = dev_get_drvdata(dev);
  83. rtc_tm_to_time(tm, &time);
  84. __raw_writel(time, ldata->base + RTC_LR);
  85. return 0;
  86. }
  87. static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  88. {
  89. struct pl031_local *ldata = dev_get_drvdata(dev);
  90. rtc_time_to_tm(__raw_readl(ldata->base + RTC_MR), &alarm->time);
  91. alarm->pending = __raw_readl(ldata->base + RTC_RIS);
  92. alarm->enabled = __raw_readl(ldata->base + RTC_IMSC);
  93. return 0;
  94. }
  95. static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  96. {
  97. struct pl031_local *ldata = dev_get_drvdata(dev);
  98. unsigned long time;
  99. rtc_tm_to_time(&alarm->time, &time);
  100. __raw_writel(time, ldata->base + RTC_MR);
  101. __raw_writel(!alarm->enabled, ldata->base + RTC_MIS);
  102. return 0;
  103. }
  104. static const struct rtc_class_ops pl031_ops = {
  105. .open = pl031_open,
  106. .release = pl031_release,
  107. .ioctl = pl031_ioctl,
  108. .read_time = pl031_read_time,
  109. .set_time = pl031_set_time,
  110. .read_alarm = pl031_read_alarm,
  111. .set_alarm = pl031_set_alarm,
  112. };
  113. static int pl031_remove(struct amba_device *adev)
  114. {
  115. struct pl031_local *ldata = dev_get_drvdata(&adev->dev);
  116. if (ldata) {
  117. dev_set_drvdata(&adev->dev, NULL);
  118. free_irq(adev->irq[0], ldata->rtc);
  119. rtc_device_unregister(ldata->rtc);
  120. iounmap(ldata->base);
  121. kfree(ldata);
  122. }
  123. return 0;
  124. }
  125. static int pl031_probe(struct amba_device *adev, void *id)
  126. {
  127. int ret;
  128. struct pl031_local *ldata;
  129. ldata = kmalloc(sizeof(struct pl031_local), GFP_KERNEL);
  130. if (!ldata) {
  131. ret = -ENOMEM;
  132. goto out;
  133. }
  134. dev_set_drvdata(&adev->dev, ldata);
  135. ldata->base = ioremap(adev->res.start,
  136. adev->res.end - adev->res.start + 1);
  137. if (!ldata->base) {
  138. ret = -ENOMEM;
  139. goto out_no_remap;
  140. }
  141. if (request_irq(adev->irq[0], pl031_interrupt, IRQF_DISABLED,
  142. "rtc-pl031", ldata->rtc)) {
  143. ret = -EIO;
  144. goto out_no_irq;
  145. }
  146. ldata->rtc = rtc_device_register("pl031", &adev->dev, &pl031_ops,
  147. THIS_MODULE);
  148. if (IS_ERR(ldata->rtc)) {
  149. ret = PTR_ERR(ldata->rtc);
  150. goto out_no_rtc;
  151. }
  152. return 0;
  153. out_no_rtc:
  154. free_irq(adev->irq[0], ldata->rtc);
  155. out_no_irq:
  156. iounmap(ldata->base);
  157. out_no_remap:
  158. dev_set_drvdata(&adev->dev, NULL);
  159. kfree(ldata);
  160. out:
  161. return ret;
  162. }
  163. static struct amba_id pl031_ids[] __initdata = {
  164. {
  165. .id = 0x00041031,
  166. .mask = 0x000fffff, },
  167. {0, 0},
  168. };
  169. static struct amba_driver pl031_driver = {
  170. .drv = {
  171. .name = "rtc-pl031",
  172. },
  173. .id_table = pl031_ids,
  174. .probe = pl031_probe,
  175. .remove = pl031_remove,
  176. };
  177. static int __init pl031_init(void)
  178. {
  179. return amba_driver_register(&pl031_driver);
  180. }
  181. static void __exit pl031_exit(void)
  182. {
  183. amba_driver_unregister(&pl031_driver);
  184. }
  185. module_init(pl031_init);
  186. module_exit(pl031_exit);
  187. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net");
  188. MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
  189. MODULE_LICENSE("GPL");