rtc-sa1100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/rtc.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pm.h>
  30. #include <linux/slab.h>
  31. #include <linux/clk.h>
  32. #include <linux/io.h>
  33. #include <mach/hardware.h>
  34. #include <asm/irq.h>
  35. #define RTC_DEF_DIVIDER (32768 - 1)
  36. #define RTC_DEF_TRIM 0
  37. #define RTC_FREQ 1024
  38. #define RCNR 0x00 /* RTC Count Register */
  39. #define RTAR 0x04 /* RTC Alarm Register */
  40. #define RTSR 0x08 /* RTC Status Register */
  41. #define RTTR 0x0c /* RTC Timer Trim Register */
  42. #define RTSR_HZE (1 << 3) /* HZ interrupt enable */
  43. #define RTSR_ALE (1 << 2) /* RTC alarm interrupt enable */
  44. #define RTSR_HZ (1 << 1) /* HZ rising-edge detected */
  45. #define RTSR_AL (1 << 0) /* RTC alarm detected */
  46. #define rtc_readl(sa1100_rtc, reg) \
  47. readl_relaxed((sa1100_rtc)->base + (reg))
  48. #define rtc_writel(sa1100_rtc, reg, value) \
  49. writel_relaxed((value), (sa1100_rtc)->base + (reg))
  50. struct sa1100_rtc {
  51. struct resource *ress;
  52. void __iomem *base;
  53. struct clk *clk;
  54. int irq_1Hz;
  55. int irq_Alrm;
  56. struct rtc_device *rtc;
  57. spinlock_t lock; /* Protects this structure */
  58. };
  59. /*
  60. * Calculate the next alarm time given the requested alarm time mask
  61. * and the current time.
  62. */
  63. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  64. struct rtc_time *alrm)
  65. {
  66. unsigned long next_time;
  67. unsigned long now_time;
  68. next->tm_year = now->tm_year;
  69. next->tm_mon = now->tm_mon;
  70. next->tm_mday = now->tm_mday;
  71. next->tm_hour = alrm->tm_hour;
  72. next->tm_min = alrm->tm_min;
  73. next->tm_sec = alrm->tm_sec;
  74. rtc_tm_to_time(now, &now_time);
  75. rtc_tm_to_time(next, &next_time);
  76. if (next_time < now_time) {
  77. /* Advance one day */
  78. next_time += 60 * 60 * 24;
  79. rtc_time_to_tm(next_time, next);
  80. }
  81. }
  82. static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
  83. {
  84. struct platform_device *pdev = to_platform_device(dev_id);
  85. struct sa1100_rtc *sa1100_rtc = platform_get_drvdata(pdev);
  86. unsigned int rtsr;
  87. unsigned long events = 0;
  88. spin_lock(&sa1100_rtc->lock);
  89. /* clear interrupt sources */
  90. rtsr = rtc_readl(sa1100_rtc, RTSR);
  91. rtc_writel(sa1100_rtc, RTSR, 0);
  92. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  93. * See also the comments in sa1100_rtc_probe(). */
  94. if (rtsr & (RTSR_ALE | RTSR_HZE)) {
  95. /* This is the original code, before there was the if test
  96. * above. This code does not clear interrupts that were not
  97. * enabled. */
  98. rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ) & (rtsr >> 2));
  99. } else {
  100. /* For some reason, it is possible to enter this routine
  101. * without interruptions enabled, it has been tested with
  102. * several units (Bug in SA11xx chip?).
  103. *
  104. * This situation leads to an infinite "loop" of interrupt
  105. * routine calling and as a result the processor seems to
  106. * lock on its first call to open(). */
  107. rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ));
  108. }
  109. /* clear alarm interrupt if it has occurred */
  110. if (rtsr & RTSR_AL)
  111. rtsr &= ~RTSR_ALE;
  112. rtc_writel(sa1100_rtc, RTSR, rtsr & (RTSR_ALE | RTSR_HZE));
  113. /* update irq data & counter */
  114. if (rtsr & RTSR_AL)
  115. events |= RTC_AF | RTC_IRQF;
  116. if (rtsr & RTSR_HZ)
  117. events |= RTC_UF | RTC_IRQF;
  118. rtc_update_irq(sa1100_rtc->rtc, 1, events);
  119. spin_unlock(&sa1100_rtc->lock);
  120. return IRQ_HANDLED;
  121. }
  122. static int sa1100_rtc_open(struct device *dev)
  123. {
  124. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  125. int ret;
  126. ret = request_irq(sa1100_rtc->irq_1Hz, sa1100_rtc_interrupt,
  127. IRQF_DISABLED, "rtc 1Hz", dev);
  128. if (ret) {
  129. dev_err(dev, "IRQ %d already in use.\n", sa1100_rtc->irq_1Hz);
  130. goto fail_ui;
  131. }
  132. ret = request_irq(sa1100_rtc->irq_Alrm, sa1100_rtc_interrupt,
  133. IRQF_DISABLED, "rtc Alrm", dev);
  134. if (ret) {
  135. dev_err(dev, "IRQ %d already in use.\n", sa1100_rtc->irq_Alrm);
  136. goto fail_ai;
  137. }
  138. sa1100_rtc->rtc->max_user_freq = RTC_FREQ;
  139. rtc_irq_set_freq(sa1100_rtc->rtc, NULL, RTC_FREQ);
  140. return 0;
  141. fail_ai:
  142. free_irq(sa1100_rtc->irq_1Hz, dev);
  143. fail_ui:
  144. return ret;
  145. }
  146. static void sa1100_rtc_release(struct device *dev)
  147. {
  148. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  149. spin_lock_irq(&sa1100_rtc->lock);
  150. rtc_writel(sa1100_rtc, RTSR, 0);
  151. spin_unlock_irq(&sa1100_rtc->lock);
  152. free_irq(sa1100_rtc->irq_Alrm, dev);
  153. free_irq(sa1100_rtc->irq_1Hz, dev);
  154. }
  155. static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  156. {
  157. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  158. unsigned int rtsr;
  159. spin_lock_irq(&sa1100_rtc->lock);
  160. rtsr = rtc_readl(sa1100_rtc, RTSR);
  161. if (enabled)
  162. rtsr |= RTSR_ALE;
  163. else
  164. rtsr &= ~RTSR_ALE;
  165. rtc_writel(sa1100_rtc, RTSR, rtsr);
  166. spin_unlock_irq(&sa1100_rtc->lock);
  167. return 0;
  168. }
  169. static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
  170. {
  171. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  172. rtc_time_to_tm(rtc_readl(sa1100_rtc, RCNR), tm);
  173. return 0;
  174. }
  175. static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
  176. {
  177. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  178. unsigned long time;
  179. int ret;
  180. ret = rtc_tm_to_time(tm, &time);
  181. if (ret == 0)
  182. rtc_writel(sa1100_rtc, RCNR, time);
  183. return ret;
  184. }
  185. static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  186. {
  187. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  188. unsigned long time;
  189. unsigned int rtsr;
  190. time = rtc_readl(sa1100_rtc, RCNR);
  191. rtc_time_to_tm(time, &alrm->time);
  192. rtsr = rtc_readl(sa1100_rtc, RTSR);
  193. alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
  194. alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
  195. return 0;
  196. }
  197. static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  198. {
  199. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  200. struct rtc_time now_tm, alarm_tm;
  201. unsigned long time, alarm;
  202. unsigned int rtsr;
  203. spin_lock_irq(&sa1100_rtc->lock);
  204. time = rtc_readl(sa1100_rtc, RCNR);
  205. rtc_time_to_tm(time, &now_tm);
  206. rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
  207. rtc_tm_to_time(&alarm_tm, &alarm);
  208. rtc_writel(sa1100_rtc, RTAR, alarm);
  209. rtsr = rtc_readl(sa1100_rtc, RTSR);
  210. if (alrm->enabled)
  211. rtsr |= RTSR_ALE;
  212. else
  213. rtsr &= ~RTSR_ALE;
  214. rtc_writel(sa1100_rtc, RTSR, rtsr);
  215. spin_unlock_irq(&sa1100_rtc->lock);
  216. return 0;
  217. }
  218. static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
  219. {
  220. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  221. seq_printf(seq, "trim/divider\t\t: 0x%08x\n",
  222. rtc_readl(sa1100_rtc, RTTR));
  223. seq_printf(seq, "RTSR\t\t\t: 0x%08x\n",
  224. rtc_readl(sa1100_rtc, RTSR));
  225. return 0;
  226. }
  227. static const struct rtc_class_ops sa1100_rtc_ops = {
  228. .open = sa1100_rtc_open,
  229. .release = sa1100_rtc_release,
  230. .read_time = sa1100_rtc_read_time,
  231. .set_time = sa1100_rtc_set_time,
  232. .read_alarm = sa1100_rtc_read_alarm,
  233. .set_alarm = sa1100_rtc_set_alarm,
  234. .proc = sa1100_rtc_proc,
  235. .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
  236. };
  237. static int sa1100_rtc_probe(struct platform_device *pdev)
  238. {
  239. struct sa1100_rtc *sa1100_rtc;
  240. unsigned int rttr;
  241. int ret;
  242. sa1100_rtc = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL);
  243. if (!sa1100_rtc)
  244. return -ENOMEM;
  245. spin_lock_init(&sa1100_rtc->lock);
  246. platform_set_drvdata(pdev, sa1100_rtc);
  247. ret = -ENXIO;
  248. sa1100_rtc->ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. if (!sa1100_rtc->ress) {
  250. dev_err(&pdev->dev, "No I/O memory resource defined\n");
  251. goto err_ress;
  252. }
  253. sa1100_rtc->irq_1Hz = platform_get_irq(pdev, 0);
  254. if (sa1100_rtc->irq_1Hz < 0) {
  255. dev_err(&pdev->dev, "No 1Hz IRQ resource defined\n");
  256. goto err_ress;
  257. }
  258. sa1100_rtc->irq_Alrm = platform_get_irq(pdev, 1);
  259. if (sa1100_rtc->irq_Alrm < 0) {
  260. dev_err(&pdev->dev, "No alarm IRQ resource defined\n");
  261. goto err_ress;
  262. }
  263. ret = -ENOMEM;
  264. sa1100_rtc->base = ioremap(sa1100_rtc->ress->start,
  265. resource_size(sa1100_rtc->ress));
  266. if (!sa1100_rtc->base) {
  267. dev_err(&pdev->dev, "Unable to map pxa RTC I/O memory\n");
  268. goto err_map;
  269. }
  270. sa1100_rtc->clk = clk_get(&pdev->dev, NULL);
  271. if (IS_ERR(sa1100_rtc->clk)) {
  272. dev_err(&pdev->dev, "failed to find rtc clock source\n");
  273. ret = PTR_ERR(sa1100_rtc->clk);
  274. goto err_clk;
  275. }
  276. clk_prepare(sa1100_rtc->clk);
  277. clk_enable(sa1100_rtc->clk);
  278. /*
  279. * According to the manual we should be able to let RTTR be zero
  280. * and then a default diviser for a 32.768KHz clock is used.
  281. * Apparently this doesn't work, at least for my SA1110 rev 5.
  282. * If the clock divider is uninitialized then reset it to the
  283. * default value to get the 1Hz clock.
  284. */
  285. if (rtc_readl(sa1100_rtc, RTTR) == 0) {
  286. rttr = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  287. rtc_writel(sa1100_rtc, RTTR, rttr);
  288. dev_warn(&pdev->dev, "warning: initializing default clock"
  289. " divider/trim value\n");
  290. /* The current RTC value probably doesn't make sense either */
  291. rtc_writel(sa1100_rtc, RCNR, 0);
  292. }
  293. device_init_wakeup(&pdev->dev, 1);
  294. sa1100_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
  295. &sa1100_rtc_ops, THIS_MODULE);
  296. if (IS_ERR(sa1100_rtc->rtc)) {
  297. dev_err(&pdev->dev, "Failed to register RTC device -> %d\n",
  298. ret);
  299. goto err_rtc_reg;
  300. }
  301. /* Fix for a nasty initialization problem the in SA11xx RTSR register.
  302. * See also the comments in sa1100_rtc_interrupt().
  303. *
  304. * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
  305. * interrupt pending, even though interrupts were never enabled.
  306. * In this case, this bit it must be reset before enabling
  307. * interruptions to avoid a nonexistent interrupt to occur.
  308. *
  309. * In principle, the same problem would apply to bit 0, although it has
  310. * never been observed to happen.
  311. *
  312. * This issue is addressed both here and in sa1100_rtc_interrupt().
  313. * If the issue is not addressed here, in the times when the processor
  314. * wakes up with the bit set there will be one spurious interrupt.
  315. *
  316. * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
  317. * safe side, once the condition that lead to this strange
  318. * initialization is unknown and could in principle happen during
  319. * normal processing.
  320. *
  321. * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
  322. * the corresponding bits in RTSR. */
  323. rtc_writel(sa1100_rtc, RTSR, (RTSR_AL | RTSR_HZ));
  324. return 0;
  325. err_rtc_reg:
  326. err_clk:
  327. iounmap(sa1100_rtc->base);
  328. err_ress:
  329. err_map:
  330. kfree(sa1100_rtc);
  331. return ret;
  332. }
  333. static int sa1100_rtc_remove(struct platform_device *pdev)
  334. {
  335. struct sa1100_rtc *sa1100_rtc = platform_get_drvdata(pdev);
  336. rtc_device_unregister(sa1100_rtc->rtc);
  337. clk_disable(sa1100_rtc->clk);
  338. clk_unprepare(sa1100_rtc->clk);
  339. iounmap(sa1100_rtc->base);
  340. return 0;
  341. }
  342. #ifdef CONFIG_PM
  343. static int sa1100_rtc_suspend(struct device *dev)
  344. {
  345. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  346. if (device_may_wakeup(dev))
  347. enable_irq_wake(sa1100_rtc->irq_Alrm);
  348. return 0;
  349. }
  350. static int sa1100_rtc_resume(struct device *dev)
  351. {
  352. struct sa1100_rtc *sa1100_rtc = dev_get_drvdata(dev);
  353. if (device_may_wakeup(dev))
  354. disable_irq_wake(sa1100_rtc->irq_Alrm);
  355. return 0;
  356. }
  357. static const struct dev_pm_ops sa1100_rtc_pm_ops = {
  358. .suspend = sa1100_rtc_suspend,
  359. .resume = sa1100_rtc_resume,
  360. };
  361. #endif
  362. static struct platform_driver sa1100_rtc_driver = {
  363. .probe = sa1100_rtc_probe,
  364. .remove = sa1100_rtc_remove,
  365. .driver = {
  366. .name = "sa1100-rtc",
  367. #ifdef CONFIG_PM
  368. .pm = &sa1100_rtc_pm_ops,
  369. #endif
  370. },
  371. };
  372. module_platform_driver(sa1100_rtc_driver);
  373. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  374. MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
  375. MODULE_LICENSE("GPL");
  376. MODULE_ALIAS("platform:sa1100-rtc");