rtc-puv3.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * RTC driver code specific to PKUnity SoC and UniCore ISA
  3. *
  4. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  5. * Copyright (C) 2001-2010 Guan Xuetao
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/string.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/rtc.h>
  18. #include <linux/bcd.h>
  19. #include <linux/clk.h>
  20. #include <linux/log2.h>
  21. #include <linux/slab.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/io.h>
  24. #include <asm/irq.h>
  25. #include <mach/hardware.h>
  26. static struct resource *puv3_rtc_mem;
  27. static int puv3_rtc_alarmno = IRQ_RTCAlarm;
  28. static int puv3_rtc_tickno = IRQ_RTC;
  29. static DEFINE_SPINLOCK(puv3_rtc_pie_lock);
  30. /* IRQ Handlers */
  31. static irqreturn_t puv3_rtc_alarmirq(int irq, void *id)
  32. {
  33. struct rtc_device *rdev = id;
  34. writel(readl(RTC_RTSR) | RTC_RTSR_AL, RTC_RTSR);
  35. rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
  36. return IRQ_HANDLED;
  37. }
  38. static irqreturn_t puv3_rtc_tickirq(int irq, void *id)
  39. {
  40. struct rtc_device *rdev = id;
  41. writel(readl(RTC_RTSR) | RTC_RTSR_HZ, RTC_RTSR);
  42. rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
  43. return IRQ_HANDLED;
  44. }
  45. /* Update control registers */
  46. static void puv3_rtc_setaie(struct device *dev, int to)
  47. {
  48. unsigned int tmp;
  49. dev_dbg(dev, "%s: aie=%d\n", __func__, to);
  50. tmp = readl(RTC_RTSR) & ~RTC_RTSR_ALE;
  51. if (to)
  52. tmp |= RTC_RTSR_ALE;
  53. writel(tmp, RTC_RTSR);
  54. }
  55. static int puv3_rtc_setpie(struct device *dev, int enabled)
  56. {
  57. unsigned int tmp;
  58. dev_dbg(dev, "%s: pie=%d\n", __func__, enabled);
  59. spin_lock_irq(&puv3_rtc_pie_lock);
  60. tmp = readl(RTC_RTSR) & ~RTC_RTSR_HZE;
  61. if (enabled)
  62. tmp |= RTC_RTSR_HZE;
  63. writel(tmp, RTC_RTSR);
  64. spin_unlock_irq(&puv3_rtc_pie_lock);
  65. return 0;
  66. }
  67. /* Time read/write */
  68. static int puv3_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  69. {
  70. rtc_time_to_tm(readl(RTC_RCNR), rtc_tm);
  71. dev_dbg(dev, "read time %02x.%02x.%02x %02x/%02x/%02x\n",
  72. rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
  73. rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
  74. return 0;
  75. }
  76. static int puv3_rtc_settime(struct device *dev, struct rtc_time *tm)
  77. {
  78. unsigned long rtc_count = 0;
  79. dev_dbg(dev, "set time %02d.%02d.%02d %02d/%02d/%02d\n",
  80. tm->tm_year, tm->tm_mon, tm->tm_mday,
  81. tm->tm_hour, tm->tm_min, tm->tm_sec);
  82. rtc_tm_to_time(tm, &rtc_count);
  83. writel(rtc_count, RTC_RCNR);
  84. return 0;
  85. }
  86. static int puv3_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  87. {
  88. struct rtc_time *alm_tm = &alrm->time;
  89. rtc_time_to_tm(readl(RTC_RTAR), alm_tm);
  90. alrm->enabled = readl(RTC_RTSR) & RTC_RTSR_ALE;
  91. dev_dbg(dev, "read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
  92. alrm->enabled,
  93. alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
  94. alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
  95. return 0;
  96. }
  97. static int puv3_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  98. {
  99. struct rtc_time *tm = &alrm->time;
  100. unsigned long rtcalarm_count = 0;
  101. dev_dbg(dev, "puv3_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
  102. alrm->enabled,
  103. tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
  104. tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
  105. rtc_tm_to_time(tm, &rtcalarm_count);
  106. writel(rtcalarm_count, RTC_RTAR);
  107. puv3_rtc_setaie(dev, alrm->enabled);
  108. if (alrm->enabled)
  109. enable_irq_wake(puv3_rtc_alarmno);
  110. else
  111. disable_irq_wake(puv3_rtc_alarmno);
  112. return 0;
  113. }
  114. static int puv3_rtc_proc(struct device *dev, struct seq_file *seq)
  115. {
  116. seq_printf(seq, "periodic_IRQ\t: %s\n",
  117. (readl(RTC_RTSR) & RTC_RTSR_HZE) ? "yes" : "no");
  118. return 0;
  119. }
  120. static const struct rtc_class_ops puv3_rtcops = {
  121. .read_time = puv3_rtc_gettime,
  122. .set_time = puv3_rtc_settime,
  123. .read_alarm = puv3_rtc_getalarm,
  124. .set_alarm = puv3_rtc_setalarm,
  125. .proc = puv3_rtc_proc,
  126. };
  127. static void puv3_rtc_enable(struct device *dev, int en)
  128. {
  129. if (!en) {
  130. writel(readl(RTC_RTSR) & ~RTC_RTSR_HZE, RTC_RTSR);
  131. } else {
  132. /* re-enable the device, and check it is ok */
  133. if ((readl(RTC_RTSR) & RTC_RTSR_HZE) == 0) {
  134. dev_info(dev, "rtc disabled, re-enabling\n");
  135. writel(readl(RTC_RTSR) | RTC_RTSR_HZE, RTC_RTSR);
  136. }
  137. }
  138. }
  139. static int puv3_rtc_remove(struct platform_device *dev)
  140. {
  141. puv3_rtc_setpie(&dev->dev, 0);
  142. puv3_rtc_setaie(&dev->dev, 0);
  143. release_resource(puv3_rtc_mem);
  144. kfree(puv3_rtc_mem);
  145. return 0;
  146. }
  147. static int puv3_rtc_probe(struct platform_device *pdev)
  148. {
  149. struct rtc_device *rtc;
  150. struct resource *res;
  151. int ret;
  152. dev_dbg(&pdev->dev, "%s: probe=%p\n", __func__, pdev);
  153. /* find the IRQs */
  154. puv3_rtc_tickno = platform_get_irq(pdev, 1);
  155. if (puv3_rtc_tickno < 0) {
  156. dev_err(&pdev->dev, "no irq for rtc tick\n");
  157. return -ENOENT;
  158. }
  159. puv3_rtc_alarmno = platform_get_irq(pdev, 0);
  160. if (puv3_rtc_alarmno < 0) {
  161. dev_err(&pdev->dev, "no irq for alarm\n");
  162. return -ENOENT;
  163. }
  164. dev_dbg(&pdev->dev, "PKUnity_rtc: tick irq %d, alarm irq %d\n",
  165. puv3_rtc_tickno, puv3_rtc_alarmno);
  166. rtc = devm_rtc_allocate_device(&pdev->dev);
  167. if (IS_ERR(rtc))
  168. return PTR_ERR(rtc);
  169. ret = devm_request_irq(&pdev->dev, puv3_rtc_alarmno, puv3_rtc_alarmirq,
  170. 0, "pkunity-rtc alarm", rtc);
  171. if (ret) {
  172. dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_alarmno, ret);
  173. return ret;
  174. }
  175. ret = devm_request_irq(&pdev->dev, puv3_rtc_tickno, puv3_rtc_tickirq,
  176. 0, "pkunity-rtc tick", rtc);
  177. if (ret) {
  178. dev_err(&pdev->dev, "IRQ%d error %d\n", puv3_rtc_tickno, ret);
  179. return ret;
  180. }
  181. /* get the memory region */
  182. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  183. if (res == NULL) {
  184. dev_err(&pdev->dev, "failed to get memory region resource\n");
  185. return -ENOENT;
  186. }
  187. puv3_rtc_mem = request_mem_region(res->start, resource_size(res),
  188. pdev->name);
  189. if (puv3_rtc_mem == NULL) {
  190. dev_err(&pdev->dev, "failed to reserve memory region\n");
  191. ret = -ENOENT;
  192. goto err_nores;
  193. }
  194. puv3_rtc_enable(&pdev->dev, 1);
  195. /* register RTC and exit */
  196. rtc->ops = &puv3_rtcops;
  197. ret = rtc_register_device(rtc);
  198. if (ret) {
  199. dev_err(&pdev->dev, "cannot attach rtc\n");
  200. goto err_nortc;
  201. }
  202. /* platform setup code should have handled this; sigh */
  203. if (!device_can_wakeup(&pdev->dev))
  204. device_init_wakeup(&pdev->dev, 1);
  205. platform_set_drvdata(pdev, rtc);
  206. return 0;
  207. err_nortc:
  208. puv3_rtc_enable(&pdev->dev, 0);
  209. release_resource(puv3_rtc_mem);
  210. err_nores:
  211. return ret;
  212. }
  213. #ifdef CONFIG_PM_SLEEP
  214. static int ticnt_save;
  215. static int puv3_rtc_suspend(struct device *dev)
  216. {
  217. /* save RTAR for anyone using periodic interrupts */
  218. ticnt_save = readl(RTC_RTAR);
  219. puv3_rtc_enable(dev, 0);
  220. return 0;
  221. }
  222. static int puv3_rtc_resume(struct device *dev)
  223. {
  224. puv3_rtc_enable(dev, 1);
  225. writel(ticnt_save, RTC_RTAR);
  226. return 0;
  227. }
  228. #endif
  229. static SIMPLE_DEV_PM_OPS(puv3_rtc_pm_ops, puv3_rtc_suspend, puv3_rtc_resume);
  230. static struct platform_driver puv3_rtc_driver = {
  231. .probe = puv3_rtc_probe,
  232. .remove = puv3_rtc_remove,
  233. .driver = {
  234. .name = "PKUnity-v3-RTC",
  235. .pm = &puv3_rtc_pm_ops,
  236. }
  237. };
  238. module_platform_driver(puv3_rtc_driver);
  239. MODULE_DESCRIPTION("RTC Driver for the PKUnity v3 chip");
  240. MODULE_AUTHOR("Hu Dongliang");
  241. MODULE_LICENSE("GPL v2");