rtc-sa1100.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
  3. *
  4. * Copyright (c) 2000 Nils Faerber
  5. *
  6. * Based on rtc.c by Paul Gortmaker
  7. *
  8. * Original Driver by Nils Faerber <nils@kernelconcepts.de>
  9. *
  10. * Modifications from:
  11. * CIH <cih@coventive.com>
  12. * Nicolas Pitre <nico@fluxnic.net>
  13. * Andrew Christian <andrew.christian@hp.com>
  14. *
  15. * Converted to the RTC subsystem and Driver Model
  16. * by Richard Purdie <rpurdie@rpsys.net>
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/platform_device.h>
  24. #include <linux/module.h>
  25. #include <linux/clk.h>
  26. #include <linux/rtc.h>
  27. #include <linux/init.h>
  28. #include <linux/fs.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <linux/of.h>
  33. #include <linux/pm.h>
  34. #include <linux/bitops.h>
  35. #include <linux/io.h>
  36. #include <mach/hardware.h>
  37. #include <mach/irqs.h>
  38. #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
  39. #include <mach/regs-rtc.h>
  40. #endif
  41. #define RTC_DEF_DIVIDER (32768 - 1)
  42. #define RTC_DEF_TRIM 0
  43. #define RTC_FREQ 1024
  44. struct sa1100_rtc {
  45. spinlock_t lock;
  46. int irq_1hz;
  47. int irq_alarm;
  48. struct rtc_device *rtc;
  49. struct clk *clk;
  50. };
  51. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  52. {
  53. struct sa1100_rtc *info = dev_get_drvdata(dev_id);
  54. struct rtc_device *rtc = info->rtc;
  55. unsigned int rtsr;
  56. unsigned long events = 0;
  57. spin_lock(&info->lock);
  58. rtsr = RTSR;
  59. /* clear interrupt sources */
  60. RTSR = 0;
  61. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  62. * See also the comments in sa1100_rtc_probe(). */
  63. if (rtsr & (RTSR_ALE | RTSR_HZE)) {
  64. /* This is the original code, before there was the if test
  65. * above. This code does not clear interrupts that were not
  66. * enabled. */
  67. RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
  68. } else {
  69. /* For some reason, it is possible to enter this routine
  70. * without interruptions enabled, it has been tested with
  71. * several units (Bug in SA11xx chip?).
  72. *
  73. * This situation leads to an infinite "loop" of interrupt
  74. * routine calling and as a result the processor seems to
  75. * lock on its first call to open(). */
  76. RTSR = RTSR_AL | RTSR_HZ;
  77. }
  78. /* clear alarm interrupt if it has occurred */
  79. if (rtsr & RTSR_AL)
  80. rtsr &= ~RTSR_ALE;
  81. RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
  82. /* update irq data & counter */
  83. if (rtsr & RTSR_AL)
  84. events |= RTC_AF | RTC_IRQF;
  85. if (rtsr & RTSR_HZ)
  86. events |= RTC_UF | RTC_IRQF;
  87. rtc_update_irq(rtc, 1, events);
  88. spin_unlock(&info->lock);
  89. return IRQ_HANDLED;
  90. }
  91. static int sa1100_rtc_open(struct device *dev)
  92. {
  93. struct sa1100_rtc *info = dev_get_drvdata(dev);
  94. struct rtc_device *rtc = info->rtc;
  95. int ret;
  96. ret = request_irq(info->irq_1hz, sa1100_rtc_interrupt, 0, "rtc 1Hz", dev);
  97. if (ret) {
  98. dev_err(dev, "IRQ %d already in use.\n", info->irq_1hz);
  99. goto fail_ui;
  100. }
  101. ret = request_irq(info->irq_alarm, sa1100_rtc_interrupt, 0, "rtc Alrm", dev);
  102. if (ret) {
  103. dev_err(dev, "IRQ %d already in use.\n", info->irq_alarm);
  104. goto fail_ai;
  105. }
  106. rtc->max_user_freq = RTC_FREQ;
  107. rtc_irq_set_freq(rtc, NULL, RTC_FREQ);
  108. return 0;
  109. fail_ai:
  110. free_irq(info->irq_1hz, dev);
  111. fail_ui:
  112. clk_disable_unprepare(info->clk);
  113. return ret;
  114. }
  115. static void sa1100_rtc_release(struct device *dev)
  116. {
  117. struct sa1100_rtc *info = dev_get_drvdata(dev);
  118. spin_lock_irq(&info->lock);
  119. RTSR = 0;
  120. spin_unlock_irq(&info->lock);
  121. free_irq(info->irq_alarm, dev);
  122. free_irq(info->irq_1hz, dev);
  123. }
  124. static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  125. {
  126. struct sa1100_rtc *info = dev_get_drvdata(dev);
  127. spin_lock_irq(&info->lock);
  128. if (enabled)
  129. RTSR |= RTSR_ALE;
  130. else
  131. RTSR &= ~RTSR_ALE;
  132. spin_unlock_irq(&info->lock);
  133. return 0;
  134. }
  135. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  136. {
  137. rtc_time_to_tm(RCNR, tm);
  138. return 0;
  139. }
  140. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  141. {
  142. unsigned long time;
  143. int ret;
  144. ret = rtc_tm_to_time(tm, &time);
  145. if (ret == 0)
  146. RCNR = time;
  147. return ret;
  148. }
  149. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  150. {
  151. u32 rtsr;
  152. rtsr = RTSR;
  153. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  154. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  155. return 0;
  156. }
  157. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  158. {
  159. struct sa1100_rtc *info = dev_get_drvdata(dev);
  160. unsigned long time;
  161. int ret;
  162. spin_lock_irq(&info->lock);
  163. ret = rtc_tm_to_time(&alrm->time, &time);
  164. if (ret != 0)
  165. goto out;
  166. RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
  167. RTAR = time;
  168. if (alrm->enabled)
  169. RTSR |= RTSR_ALE;
  170. else
  171. RTSR &= ~RTSR_ALE;
  172. out:
  173. spin_unlock_irq(&info->lock);
  174. return ret;
  175. }
  176. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  177. {
  178. seq_printf(seq, "trim/divider\t\t: 0x%08x\n", (u32) RTTR);
  179. seq_printf(seq, "RTSR\t\t\t: 0x%08x\n", (u32)RTSR);
  180. return 0;
  181. }
  182. static const struct rtc_class_ops sa1100_rtc_ops = {
  183. .open = sa1100_rtc_open,
  184. .release = sa1100_rtc_release,
  185. .read_time = sa1100_rtc_read_time,
  186. .set_time = sa1100_rtc_set_time,
  187. .read_alarm = sa1100_rtc_read_alarm,
  188. .set_alarm = sa1100_rtc_set_alarm,
  189. .proc = sa1100_rtc_proc,
  190. .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
  191. };
  192. static int sa1100_rtc_probe(struct platform_device *pdev)
  193. {
  194. struct rtc_device *rtc;
  195. struct sa1100_rtc *info;
  196. int irq_1hz, irq_alarm, ret = 0;
  197. irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
  198. irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
  199. if (irq_1hz < 0 || irq_alarm < 0)
  200. return -ENODEV;
  201. info = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL);
  202. if (!info)
  203. return -ENOMEM;
  204. info->clk = clk_get(&pdev->dev, NULL);
  205. if (IS_ERR(info->clk)) {
  206. dev_err(&pdev->dev, "failed to find rtc clock source\n");
  207. ret = PTR_ERR(info->clk);
  208. goto err_clk;
  209. }
  210. info->irq_1hz = irq_1hz;
  211. info->irq_alarm = irq_alarm;
  212. spin_lock_init(&info->lock);
  213. platform_set_drvdata(pdev, info);
  214. ret = clk_prepare_enable(info->clk);
  215. if (ret)
  216. goto err_enable_clk;
  217. /*
  218. * According to the manual we should be able to let RTTR be zero
  219. * and then a default diviser for a 32.768KHz clock is used.
  220. * Apparently this doesn't work, at least for my SA1110 rev 5.
  221. * If the clock divider is uninitialized then reset it to the
  222. * default value to get the 1Hz clock.
  223. */
  224. if (RTTR == 0) {
  225. RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  226. dev_warn(&pdev->dev, "warning: "
  227. "initializing default clock divider/trim value\n");
  228. /* The current RTC value probably doesn't make sense either */
  229. RCNR = 0;
  230. }
  231. device_init_wakeup(&pdev->dev, 1);
  232. rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
  233. THIS_MODULE);
  234. if (IS_ERR(rtc)) {
  235. ret = PTR_ERR(rtc);
  236. goto err_dev;
  237. }
  238. info->rtc = rtc;
  239. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  240. * See also the comments in sa1100_rtc_interrupt().
  241. *
  242. * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
  243. * interrupt pending, even though interrupts were never enabled.
  244. * In this case, this bit it must be reset before enabling
  245. * interruptions to avoid a nonexistent interrupt to occur.
  246. *
  247. * In principle, the same problem would apply to bit 0, although it has
  248. * never been observed to happen.
  249. *
  250. * This issue is addressed both here and in sa1100_rtc_interrupt().
  251. * If the issue is not addressed here, in the times when the processor
  252. * wakes up with the bit set there will be one spurious interrupt.
  253. *
  254. * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
  255. * safe side, once the condition that lead to this strange
  256. * initialization is unknown and could in principle happen during
  257. * normal processing.
  258. *
  259. * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
  260. * the corresponding bits in RTSR. */
  261. RTSR = RTSR_AL | RTSR_HZ;
  262. return 0;
  263. err_dev:
  264. clk_disable_unprepare(info->clk);
  265. err_enable_clk:
  266. platform_set_drvdata(pdev, NULL);
  267. clk_put(info->clk);
  268. err_clk:
  269. kfree(info);
  270. return ret;
  271. }
  272. static int sa1100_rtc_remove(struct platform_device *pdev)
  273. {
  274. struct sa1100_rtc *info = platform_get_drvdata(pdev);
  275. if (info) {
  276. rtc_device_unregister(info->rtc);
  277. clk_disable_unprepare(info->clk);
  278. clk_put(info->clk);
  279. platform_set_drvdata(pdev, NULL);
  280. kfree(info);
  281. }
  282. return 0;
  283. }
  284. #ifdef CONFIG_PM
  285. static int sa1100_rtc_suspend(struct device *dev)
  286. {
  287. struct sa1100_rtc *info = dev_get_drvdata(dev);
  288. if (device_may_wakeup(dev))
  289. enable_irq_wake(info->irq_alarm);
  290. return 0;
  291. }
  292. static int sa1100_rtc_resume(struct device *dev)
  293. {
  294. struct sa1100_rtc *info = dev_get_drvdata(dev);
  295. if (device_may_wakeup(dev))
  296. disable_irq_wake(info->irq_alarm);
  297. return 0;
  298. }
  299. static const struct dev_pm_ops sa1100_rtc_pm_ops = {
  300. .suspend = sa1100_rtc_suspend,
  301. .resume = sa1100_rtc_resume,
  302. };
  303. #endif
  304. #ifdef CONFIG_OF
  305. static struct of_device_id sa1100_rtc_dt_ids[] = {
  306. { .compatible = "mrvl,sa1100-rtc", },
  307. { .compatible = "mrvl,mmp-rtc", },
  308. {}
  309. };
  310. MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
  311. #endif
  312. static struct platform_driver sa1100_rtc_driver = {
  313. .probe = sa1100_rtc_probe,
  314. .remove = sa1100_rtc_remove,
  315. .driver = {
  316. .name = "sa1100-rtc",
  317. #ifdef CONFIG_PM
  318. .pm = &sa1100_rtc_pm_ops,
  319. #endif
  320. .of_match_table = of_match_ptr(sa1100_rtc_dt_ids),
  321. },
  322. };
  323. module_platform_driver(sa1100_rtc_driver);
  324. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  325. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  326. MODULE_LICENSE("GPL");
  327. MODULE_ALIAS("platform:sa1100-rtc");