rtc-pxa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Real Time Clock interface for XScale PXA27x and PXA3xx
  3. *
  4. * Copyright (C) 2008 Robert Jarzmik
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/rtc.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <mach/hardware.h>
  32. #include "rtc-sa1100.h"
  33. #define RTC_DEF_DIVIDER (32768 - 1)
  34. #define RTC_DEF_TRIM 0
  35. #define MAXFREQ_PERIODIC 1000
  36. /*
  37. * PXA Registers and bits definitions
  38. */
  39. #define RTSR_PICE (1 << 15) /* Periodic interrupt count enable */
  40. #define RTSR_PIALE (1 << 14) /* Periodic interrupt Alarm enable */
  41. #define RTSR_PIAL (1 << 13) /* Periodic interrupt detected */
  42. #define RTSR_SWALE2 (1 << 11) /* RTC stopwatch alarm2 enable */
  43. #define RTSR_SWAL2 (1 << 10) /* RTC stopwatch alarm2 detected */
  44. #define RTSR_SWALE1 (1 << 9) /* RTC stopwatch alarm1 enable */
  45. #define RTSR_SWAL1 (1 << 8) /* RTC stopwatch alarm1 detected */
  46. #define RTSR_RDALE2 (1 << 7) /* RTC alarm2 enable */
  47. #define RTSR_RDAL2 (1 << 6) /* RTC alarm2 detected */
  48. #define RTSR_RDALE1 (1 << 5) /* RTC alarm1 enable */
  49. #define RTSR_RDAL1 (1 << 4) /* RTC alarm1 detected */
  50. #define RTSR_HZE (1 << 3) /* HZ interrupt enable */
  51. #define RTSR_ALE (1 << 2) /* RTC alarm interrupt enable */
  52. #define RTSR_HZ (1 << 1) /* HZ rising-edge detected */
  53. #define RTSR_AL (1 << 0) /* RTC alarm detected */
  54. #define RTSR_TRIG_MASK (RTSR_AL | RTSR_HZ | RTSR_RDAL1 | RTSR_RDAL2\
  55. | RTSR_SWAL1 | RTSR_SWAL2)
  56. #define RYxR_YEAR_S 9
  57. #define RYxR_YEAR_MASK (0xfff << RYxR_YEAR_S)
  58. #define RYxR_MONTH_S 5
  59. #define RYxR_MONTH_MASK (0xf << RYxR_MONTH_S)
  60. #define RYxR_DAY_MASK 0x1f
  61. #define RDxR_WOM_S 20
  62. #define RDxR_WOM_MASK (0x7 << RDxR_WOM_S)
  63. #define RDxR_DOW_S 17
  64. #define RDxR_DOW_MASK (0x7 << RDxR_DOW_S)
  65. #define RDxR_HOUR_S 12
  66. #define RDxR_HOUR_MASK (0x1f << RDxR_HOUR_S)
  67. #define RDxR_MIN_S 6
  68. #define RDxR_MIN_MASK (0x3f << RDxR_MIN_S)
  69. #define RDxR_SEC_MASK 0x3f
  70. #define RTSR 0x08
  71. #define RTTR 0x0c
  72. #define RDCR 0x10
  73. #define RYCR 0x14
  74. #define RDAR1 0x18
  75. #define RYAR1 0x1c
  76. #define RTCPICR 0x34
  77. #define PIAR 0x38
  78. #define rtc_readl(pxa_rtc, reg) \
  79. __raw_readl((pxa_rtc)->base + (reg))
  80. #define rtc_writel(pxa_rtc, reg, value) \
  81. __raw_writel((value), (pxa_rtc)->base + (reg))
  82. struct pxa_rtc {
  83. struct sa1100_rtc sa1100_rtc;
  84. struct resource *ress;
  85. void __iomem *base;
  86. struct rtc_device *rtc;
  87. spinlock_t lock; /* Protects this structure */
  88. };
  89. static u32 ryxr_calc(struct rtc_time *tm)
  90. {
  91. return ((tm->tm_year + 1900) << RYxR_YEAR_S)
  92. | ((tm->tm_mon + 1) << RYxR_MONTH_S)
  93. | tm->tm_mday;
  94. }
  95. static u32 rdxr_calc(struct rtc_time *tm)
  96. {
  97. return ((((tm->tm_mday + 6) / 7) << RDxR_WOM_S) & RDxR_WOM_MASK)
  98. | (((tm->tm_wday + 1) << RDxR_DOW_S) & RDxR_DOW_MASK)
  99. | (tm->tm_hour << RDxR_HOUR_S)
  100. | (tm->tm_min << RDxR_MIN_S)
  101. | tm->tm_sec;
  102. }
  103. static void tm_calc(u32 rycr, u32 rdcr, struct rtc_time *tm)
  104. {
  105. tm->tm_year = ((rycr & RYxR_YEAR_MASK) >> RYxR_YEAR_S) - 1900;
  106. tm->tm_mon = (((rycr & RYxR_MONTH_MASK) >> RYxR_MONTH_S)) - 1;
  107. tm->tm_mday = (rycr & RYxR_DAY_MASK);
  108. tm->tm_wday = ((rycr & RDxR_DOW_MASK) >> RDxR_DOW_S) - 1;
  109. tm->tm_hour = (rdcr & RDxR_HOUR_MASK) >> RDxR_HOUR_S;
  110. tm->tm_min = (rdcr & RDxR_MIN_MASK) >> RDxR_MIN_S;
  111. tm->tm_sec = rdcr & RDxR_SEC_MASK;
  112. }
  113. static void rtsr_clear_bits(struct pxa_rtc *pxa_rtc, u32 mask)
  114. {
  115. u32 rtsr;
  116. rtsr = rtc_readl(pxa_rtc, RTSR);
  117. rtsr &= ~RTSR_TRIG_MASK;
  118. rtsr &= ~mask;
  119. rtc_writel(pxa_rtc, RTSR, rtsr);
  120. }
  121. static void rtsr_set_bits(struct pxa_rtc *pxa_rtc, u32 mask)
  122. {
  123. u32 rtsr;
  124. rtsr = rtc_readl(pxa_rtc, RTSR);
  125. rtsr &= ~RTSR_TRIG_MASK;
  126. rtsr |= mask;
  127. rtc_writel(pxa_rtc, RTSR, rtsr);
  128. }
  129. static irqreturn_t pxa_rtc_irq(int irq, void *dev_id)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev_id);
  132. struct pxa_rtc *pxa_rtc = platform_get_drvdata(pdev);
  133. u32 rtsr;
  134. unsigned long events = 0;
  135. spin_lock(&pxa_rtc->lock);
  136. /* clear interrupt sources */
  137. rtsr = rtc_readl(pxa_rtc, RTSR);
  138. rtc_writel(pxa_rtc, RTSR, rtsr);
  139. /* temporary disable rtc interrupts */
  140. rtsr_clear_bits(pxa_rtc, RTSR_RDALE1 | RTSR_PIALE | RTSR_HZE);
  141. /* clear alarm interrupt if it has occurred */
  142. if (rtsr & RTSR_RDAL1)
  143. rtsr &= ~RTSR_RDALE1;
  144. /* update irq data & counter */
  145. if (rtsr & RTSR_RDAL1)
  146. events |= RTC_AF | RTC_IRQF;
  147. if (rtsr & RTSR_HZ)
  148. events |= RTC_UF | RTC_IRQF;
  149. if (rtsr & RTSR_PIAL)
  150. events |= RTC_PF | RTC_IRQF;
  151. rtc_update_irq(pxa_rtc->rtc, 1, events);
  152. /* enable back rtc interrupts */
  153. rtc_writel(pxa_rtc, RTSR, rtsr & ~RTSR_TRIG_MASK);
  154. spin_unlock(&pxa_rtc->lock);
  155. return IRQ_HANDLED;
  156. }
  157. static int pxa_rtc_open(struct device *dev)
  158. {
  159. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  160. int ret;
  161. ret = request_irq(pxa_rtc->sa1100_rtc.irq_1hz, pxa_rtc_irq, 0,
  162. "rtc 1Hz", dev);
  163. if (ret < 0) {
  164. dev_err(dev, "can't get irq %i, err %d\n",
  165. pxa_rtc->sa1100_rtc.irq_1hz, ret);
  166. goto err_irq_1Hz;
  167. }
  168. ret = request_irq(pxa_rtc->sa1100_rtc.irq_alarm, pxa_rtc_irq, 0,
  169. "rtc Alrm", dev);
  170. if (ret < 0) {
  171. dev_err(dev, "can't get irq %i, err %d\n",
  172. pxa_rtc->sa1100_rtc.irq_alarm, ret);
  173. goto err_irq_Alrm;
  174. }
  175. return 0;
  176. err_irq_Alrm:
  177. free_irq(pxa_rtc->sa1100_rtc.irq_1hz, dev);
  178. err_irq_1Hz:
  179. return ret;
  180. }
  181. static void pxa_rtc_release(struct device *dev)
  182. {
  183. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  184. spin_lock_irq(&pxa_rtc->lock);
  185. rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE);
  186. spin_unlock_irq(&pxa_rtc->lock);
  187. free_irq(pxa_rtc->sa1100_rtc.irq_1hz, dev);
  188. free_irq(pxa_rtc->sa1100_rtc.irq_alarm, dev);
  189. }
  190. static int pxa_alarm_irq_enable(struct device *dev, unsigned int enabled)
  191. {
  192. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  193. spin_lock_irq(&pxa_rtc->lock);
  194. if (enabled)
  195. rtsr_set_bits(pxa_rtc, RTSR_RDALE1);
  196. else
  197. rtsr_clear_bits(pxa_rtc, RTSR_RDALE1);
  198. spin_unlock_irq(&pxa_rtc->lock);
  199. return 0;
  200. }
  201. static int pxa_rtc_read_time(struct device *dev, struct rtc_time *tm)
  202. {
  203. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  204. u32 rycr, rdcr;
  205. rycr = rtc_readl(pxa_rtc, RYCR);
  206. rdcr = rtc_readl(pxa_rtc, RDCR);
  207. tm_calc(rycr, rdcr, tm);
  208. return 0;
  209. }
  210. static int pxa_rtc_set_time(struct device *dev, struct rtc_time *tm)
  211. {
  212. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  213. rtc_writel(pxa_rtc, RYCR, ryxr_calc(tm));
  214. rtc_writel(pxa_rtc, RDCR, rdxr_calc(tm));
  215. return 0;
  216. }
  217. static int pxa_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  218. {
  219. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  220. u32 rtsr, ryar, rdar;
  221. ryar = rtc_readl(pxa_rtc, RYAR1);
  222. rdar = rtc_readl(pxa_rtc, RDAR1);
  223. tm_calc(ryar, rdar, &alrm->time);
  224. rtsr = rtc_readl(pxa_rtc, RTSR);
  225. alrm->enabled = (rtsr & RTSR_RDALE1) ? 1 : 0;
  226. alrm->pending = (rtsr & RTSR_RDAL1) ? 1 : 0;
  227. return 0;
  228. }
  229. static int pxa_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  230. {
  231. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  232. u32 rtsr;
  233. spin_lock_irq(&pxa_rtc->lock);
  234. rtc_writel(pxa_rtc, RYAR1, ryxr_calc(&alrm->time));
  235. rtc_writel(pxa_rtc, RDAR1, rdxr_calc(&alrm->time));
  236. rtsr = rtc_readl(pxa_rtc, RTSR);
  237. if (alrm->enabled)
  238. rtsr |= RTSR_RDALE1;
  239. else
  240. rtsr &= ~RTSR_RDALE1;
  241. rtc_writel(pxa_rtc, RTSR, rtsr);
  242. spin_unlock_irq(&pxa_rtc->lock);
  243. return 0;
  244. }
  245. static int pxa_rtc_proc(struct device *dev, struct seq_file *seq)
  246. {
  247. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  248. seq_printf(seq, "trim/divider\t: 0x%08x\n", rtc_readl(pxa_rtc, RTTR));
  249. seq_printf(seq, "update_IRQ\t: %s\n",
  250. (rtc_readl(pxa_rtc, RTSR) & RTSR_HZE) ? "yes" : "no");
  251. seq_printf(seq, "periodic_IRQ\t: %s\n",
  252. (rtc_readl(pxa_rtc, RTSR) & RTSR_PIALE) ? "yes" : "no");
  253. seq_printf(seq, "periodic_freq\t: %u\n", rtc_readl(pxa_rtc, PIAR));
  254. return 0;
  255. }
  256. static const struct rtc_class_ops pxa_rtc_ops = {
  257. .read_time = pxa_rtc_read_time,
  258. .set_time = pxa_rtc_set_time,
  259. .read_alarm = pxa_rtc_read_alarm,
  260. .set_alarm = pxa_rtc_set_alarm,
  261. .alarm_irq_enable = pxa_alarm_irq_enable,
  262. .proc = pxa_rtc_proc,
  263. };
  264. static int __init pxa_rtc_probe(struct platform_device *pdev)
  265. {
  266. struct device *dev = &pdev->dev;
  267. struct pxa_rtc *pxa_rtc;
  268. struct sa1100_rtc *sa1100_rtc;
  269. int ret;
  270. pxa_rtc = devm_kzalloc(dev, sizeof(*pxa_rtc), GFP_KERNEL);
  271. if (!pxa_rtc)
  272. return -ENOMEM;
  273. sa1100_rtc = &pxa_rtc->sa1100_rtc;
  274. spin_lock_init(&pxa_rtc->lock);
  275. platform_set_drvdata(pdev, pxa_rtc);
  276. pxa_rtc->ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. if (!pxa_rtc->ress) {
  278. dev_err(dev, "No I/O memory resource defined\n");
  279. return -ENXIO;
  280. }
  281. sa1100_rtc->irq_1hz = platform_get_irq(pdev, 0);
  282. if (sa1100_rtc->irq_1hz < 0) {
  283. dev_err(dev, "No 1Hz IRQ resource defined\n");
  284. return -ENXIO;
  285. }
  286. sa1100_rtc->irq_alarm = platform_get_irq(pdev, 1);
  287. if (sa1100_rtc->irq_alarm < 0) {
  288. dev_err(dev, "No alarm IRQ resource defined\n");
  289. return -ENXIO;
  290. }
  291. pxa_rtc_open(dev);
  292. pxa_rtc->base = devm_ioremap(dev, pxa_rtc->ress->start,
  293. resource_size(pxa_rtc->ress));
  294. if (!pxa_rtc->base) {
  295. dev_err(dev, "Unable to map pxa RTC I/O memory\n");
  296. return -ENOMEM;
  297. }
  298. sa1100_rtc->rcnr = pxa_rtc->base + 0x0;
  299. sa1100_rtc->rtsr = pxa_rtc->base + 0x8;
  300. sa1100_rtc->rtar = pxa_rtc->base + 0x4;
  301. sa1100_rtc->rttr = pxa_rtc->base + 0xc;
  302. ret = sa1100_rtc_init(pdev, sa1100_rtc);
  303. if (!ret) {
  304. dev_err(dev, "Unable to init SA1100 RTC sub-device\n");
  305. return ret;
  306. }
  307. rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE);
  308. pxa_rtc->rtc = devm_rtc_device_register(&pdev->dev, "pxa-rtc",
  309. &pxa_rtc_ops, THIS_MODULE);
  310. if (IS_ERR(pxa_rtc->rtc)) {
  311. ret = PTR_ERR(pxa_rtc->rtc);
  312. dev_err(dev, "Failed to register RTC device -> %d\n", ret);
  313. return ret;
  314. }
  315. device_init_wakeup(dev, 1);
  316. return 0;
  317. }
  318. static int __exit pxa_rtc_remove(struct platform_device *pdev)
  319. {
  320. struct device *dev = &pdev->dev;
  321. pxa_rtc_release(dev);
  322. return 0;
  323. }
  324. #ifdef CONFIG_OF
  325. static const struct of_device_id pxa_rtc_dt_ids[] = {
  326. { .compatible = "marvell,pxa-rtc" },
  327. {}
  328. };
  329. MODULE_DEVICE_TABLE(of, pxa_rtc_dt_ids);
  330. #endif
  331. #ifdef CONFIG_PM_SLEEP
  332. static int pxa_rtc_suspend(struct device *dev)
  333. {
  334. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  335. if (device_may_wakeup(dev))
  336. enable_irq_wake(pxa_rtc->sa1100_rtc.irq_alarm);
  337. return 0;
  338. }
  339. static int pxa_rtc_resume(struct device *dev)
  340. {
  341. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  342. if (device_may_wakeup(dev))
  343. disable_irq_wake(pxa_rtc->sa1100_rtc.irq_alarm);
  344. return 0;
  345. }
  346. #endif
  347. static SIMPLE_DEV_PM_OPS(pxa_rtc_pm_ops, pxa_rtc_suspend, pxa_rtc_resume);
  348. static struct platform_driver pxa_rtc_driver = {
  349. .remove = __exit_p(pxa_rtc_remove),
  350. .driver = {
  351. .name = "pxa-rtc",
  352. .of_match_table = of_match_ptr(pxa_rtc_dt_ids),
  353. .pm = &pxa_rtc_pm_ops,
  354. },
  355. };
  356. module_platform_driver_probe(pxa_rtc_driver, pxa_rtc_probe);
  357. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  358. MODULE_DESCRIPTION("PXA27x/PXA3xx Realtime Clock Driver (RTC)");
  359. MODULE_LICENSE("GPL");
  360. MODULE_ALIAS("platform:pxa-rtc");