rtc-pxa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #define TIMER_FREQ CLOCK_TICK_RATE
  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_HOUR_S 12
  62. #define RDxR_HOUR_MASK (0x1f << RDxR_HOUR_S)
  63. #define RDxR_MIN_S 6
  64. #define RDxR_MIN_MASK (0x3f << RDxR_MIN_S)
  65. #define RDxR_SEC_MASK 0x3f
  66. #define RTSR 0x08
  67. #define RTTR 0x0c
  68. #define RDCR 0x10
  69. #define RYCR 0x14
  70. #define RDAR1 0x18
  71. #define RYAR1 0x1c
  72. #define RTCPICR 0x34
  73. #define PIAR 0x38
  74. #define rtc_readl(pxa_rtc, reg) \
  75. __raw_readl((pxa_rtc)->base + (reg))
  76. #define rtc_writel(pxa_rtc, reg, value) \
  77. __raw_writel((value), (pxa_rtc)->base + (reg))
  78. struct pxa_rtc {
  79. struct resource *ress;
  80. void __iomem *base;
  81. int irq_1Hz;
  82. int irq_Alrm;
  83. struct rtc_device *rtc;
  84. spinlock_t lock; /* Protects this structure */
  85. };
  86. static u32 ryxr_calc(struct rtc_time *tm)
  87. {
  88. return ((tm->tm_year + 1900) << RYxR_YEAR_S)
  89. | ((tm->tm_mon + 1) << RYxR_MONTH_S)
  90. | tm->tm_mday;
  91. }
  92. static u32 rdxr_calc(struct rtc_time *tm)
  93. {
  94. return (tm->tm_hour << RDxR_HOUR_S) | (tm->tm_min << RDxR_MIN_S)
  95. | tm->tm_sec;
  96. }
  97. static void tm_calc(u32 rycr, u32 rdcr, struct rtc_time *tm)
  98. {
  99. tm->tm_year = ((rycr & RYxR_YEAR_MASK) >> RYxR_YEAR_S) - 1900;
  100. tm->tm_mon = (((rycr & RYxR_MONTH_MASK) >> RYxR_MONTH_S)) - 1;
  101. tm->tm_mday = (rycr & RYxR_DAY_MASK);
  102. tm->tm_hour = (rdcr & RDxR_HOUR_MASK) >> RDxR_HOUR_S;
  103. tm->tm_min = (rdcr & RDxR_MIN_MASK) >> RDxR_MIN_S;
  104. tm->tm_sec = rdcr & RDxR_SEC_MASK;
  105. }
  106. static void rtsr_clear_bits(struct pxa_rtc *pxa_rtc, u32 mask)
  107. {
  108. u32 rtsr;
  109. rtsr = rtc_readl(pxa_rtc, RTSR);
  110. rtsr &= ~RTSR_TRIG_MASK;
  111. rtsr &= ~mask;
  112. rtc_writel(pxa_rtc, RTSR, rtsr);
  113. }
  114. static void rtsr_set_bits(struct pxa_rtc *pxa_rtc, u32 mask)
  115. {
  116. u32 rtsr;
  117. rtsr = rtc_readl(pxa_rtc, RTSR);
  118. rtsr &= ~RTSR_TRIG_MASK;
  119. rtsr |= mask;
  120. rtc_writel(pxa_rtc, RTSR, rtsr);
  121. }
  122. static irqreturn_t pxa_rtc_irq(int irq, void *dev_id)
  123. {
  124. struct platform_device *pdev = to_platform_device(dev_id);
  125. struct pxa_rtc *pxa_rtc = platform_get_drvdata(pdev);
  126. u32 rtsr;
  127. unsigned long events = 0;
  128. spin_lock(&pxa_rtc->lock);
  129. /* clear interrupt sources */
  130. rtsr = rtc_readl(pxa_rtc, RTSR);
  131. rtc_writel(pxa_rtc, RTSR, rtsr);
  132. /* temporary disable rtc interrupts */
  133. rtsr_clear_bits(pxa_rtc, RTSR_RDALE1 | RTSR_PIALE | RTSR_HZE);
  134. /* clear alarm interrupt if it has occurred */
  135. if (rtsr & RTSR_RDAL1)
  136. rtsr &= ~RTSR_RDALE1;
  137. /* update irq data & counter */
  138. if (rtsr & RTSR_RDAL1)
  139. events |= RTC_AF | RTC_IRQF;
  140. if (rtsr & RTSR_HZ)
  141. events |= RTC_UF | RTC_IRQF;
  142. if (rtsr & RTSR_PIAL)
  143. events |= RTC_PF | RTC_IRQF;
  144. rtc_update_irq(pxa_rtc->rtc, 1, events);
  145. /* enable back rtc interrupts */
  146. rtc_writel(pxa_rtc, RTSR, rtsr & ~RTSR_TRIG_MASK);
  147. spin_unlock(&pxa_rtc->lock);
  148. return IRQ_HANDLED;
  149. }
  150. static int pxa_rtc_open(struct device *dev)
  151. {
  152. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  153. int ret;
  154. ret = request_irq(pxa_rtc->irq_1Hz, pxa_rtc_irq, 0,
  155. "rtc 1Hz", dev);
  156. if (ret < 0) {
  157. dev_err(dev, "can't get irq %i, err %d\n", pxa_rtc->irq_1Hz,
  158. ret);
  159. goto err_irq_1Hz;
  160. }
  161. ret = request_irq(pxa_rtc->irq_Alrm, pxa_rtc_irq, 0,
  162. "rtc Alrm", dev);
  163. if (ret < 0) {
  164. dev_err(dev, "can't get irq %i, err %d\n", pxa_rtc->irq_Alrm,
  165. ret);
  166. goto err_irq_Alrm;
  167. }
  168. return 0;
  169. err_irq_Alrm:
  170. free_irq(pxa_rtc->irq_1Hz, dev);
  171. err_irq_1Hz:
  172. return ret;
  173. }
  174. static void pxa_rtc_release(struct device *dev)
  175. {
  176. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  177. spin_lock_irq(&pxa_rtc->lock);
  178. rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE);
  179. spin_unlock_irq(&pxa_rtc->lock);
  180. free_irq(pxa_rtc->irq_Alrm, dev);
  181. free_irq(pxa_rtc->irq_1Hz, dev);
  182. }
  183. static int pxa_alarm_irq_enable(struct device *dev, unsigned int enabled)
  184. {
  185. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  186. spin_lock_irq(&pxa_rtc->lock);
  187. if (enabled)
  188. rtsr_set_bits(pxa_rtc, RTSR_RDALE1);
  189. else
  190. rtsr_clear_bits(pxa_rtc, RTSR_RDALE1);
  191. spin_unlock_irq(&pxa_rtc->lock);
  192. return 0;
  193. }
  194. static int pxa_rtc_read_time(struct device *dev, struct rtc_time *tm)
  195. {
  196. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  197. u32 rycr, rdcr;
  198. rycr = rtc_readl(pxa_rtc, RYCR);
  199. rdcr = rtc_readl(pxa_rtc, RDCR);
  200. tm_calc(rycr, rdcr, tm);
  201. return 0;
  202. }
  203. static int pxa_rtc_set_time(struct device *dev, struct rtc_time *tm)
  204. {
  205. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  206. rtc_writel(pxa_rtc, RYCR, ryxr_calc(tm));
  207. rtc_writel(pxa_rtc, RDCR, rdxr_calc(tm));
  208. return 0;
  209. }
  210. static int pxa_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  211. {
  212. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  213. u32 rtsr, ryar, rdar;
  214. ryar = rtc_readl(pxa_rtc, RYAR1);
  215. rdar = rtc_readl(pxa_rtc, RDAR1);
  216. tm_calc(ryar, rdar, &alrm->time);
  217. rtsr = rtc_readl(pxa_rtc, RTSR);
  218. alrm->enabled = (rtsr & RTSR_RDALE1) ? 1 : 0;
  219. alrm->pending = (rtsr & RTSR_RDAL1) ? 1 : 0;
  220. return 0;
  221. }
  222. static int pxa_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  223. {
  224. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  225. u32 rtsr;
  226. spin_lock_irq(&pxa_rtc->lock);
  227. rtc_writel(pxa_rtc, RYAR1, ryxr_calc(&alrm->time));
  228. rtc_writel(pxa_rtc, RDAR1, rdxr_calc(&alrm->time));
  229. rtsr = rtc_readl(pxa_rtc, RTSR);
  230. if (alrm->enabled)
  231. rtsr |= RTSR_RDALE1;
  232. else
  233. rtsr &= ~RTSR_RDALE1;
  234. rtc_writel(pxa_rtc, RTSR, rtsr);
  235. spin_unlock_irq(&pxa_rtc->lock);
  236. return 0;
  237. }
  238. static int pxa_rtc_proc(struct device *dev, struct seq_file *seq)
  239. {
  240. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  241. seq_printf(seq, "trim/divider\t: 0x%08x\n", rtc_readl(pxa_rtc, RTTR));
  242. seq_printf(seq, "update_IRQ\t: %s\n",
  243. (rtc_readl(pxa_rtc, RTSR) & RTSR_HZE) ? "yes" : "no");
  244. seq_printf(seq, "periodic_IRQ\t: %s\n",
  245. (rtc_readl(pxa_rtc, RTSR) & RTSR_PIALE) ? "yes" : "no");
  246. seq_printf(seq, "periodic_freq\t: %u\n", rtc_readl(pxa_rtc, PIAR));
  247. return 0;
  248. }
  249. static const struct rtc_class_ops pxa_rtc_ops = {
  250. .open = pxa_rtc_open,
  251. .release = pxa_rtc_release,
  252. .read_time = pxa_rtc_read_time,
  253. .set_time = pxa_rtc_set_time,
  254. .read_alarm = pxa_rtc_read_alarm,
  255. .set_alarm = pxa_rtc_set_alarm,
  256. .alarm_irq_enable = pxa_alarm_irq_enable,
  257. .proc = pxa_rtc_proc,
  258. };
  259. static int __init pxa_rtc_probe(struct platform_device *pdev)
  260. {
  261. struct device *dev = &pdev->dev;
  262. struct pxa_rtc *pxa_rtc;
  263. int ret;
  264. u32 rttr;
  265. pxa_rtc = kzalloc(sizeof(struct pxa_rtc), GFP_KERNEL);
  266. if (!pxa_rtc)
  267. return -ENOMEM;
  268. spin_lock_init(&pxa_rtc->lock);
  269. platform_set_drvdata(pdev, pxa_rtc);
  270. ret = -ENXIO;
  271. pxa_rtc->ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  272. if (!pxa_rtc->ress) {
  273. dev_err(dev, "No I/O memory resource defined\n");
  274. goto err_ress;
  275. }
  276. pxa_rtc->irq_1Hz = platform_get_irq(pdev, 0);
  277. if (pxa_rtc->irq_1Hz < 0) {
  278. dev_err(dev, "No 1Hz IRQ resource defined\n");
  279. goto err_ress;
  280. }
  281. pxa_rtc->irq_Alrm = platform_get_irq(pdev, 1);
  282. if (pxa_rtc->irq_Alrm < 0) {
  283. dev_err(dev, "No alarm IRQ resource defined\n");
  284. goto err_ress;
  285. }
  286. ret = -ENOMEM;
  287. pxa_rtc->base = ioremap(pxa_rtc->ress->start,
  288. resource_size(pxa_rtc->ress));
  289. if (!pxa_rtc->base) {
  290. dev_err(&pdev->dev, "Unable to map pxa RTC I/O memory\n");
  291. goto err_map;
  292. }
  293. /*
  294. * If the clock divider is uninitialized then reset it to the
  295. * default value to get the 1Hz clock.
  296. */
  297. if (rtc_readl(pxa_rtc, RTTR) == 0) {
  298. rttr = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  299. rtc_writel(pxa_rtc, RTTR, rttr);
  300. dev_warn(dev, "warning: initializing default clock"
  301. " divider/trim value\n");
  302. }
  303. rtsr_clear_bits(pxa_rtc, RTSR_PIALE | RTSR_RDALE1 | RTSR_HZE);
  304. pxa_rtc->rtc = rtc_device_register("pxa-rtc", &pdev->dev, &pxa_rtc_ops,
  305. THIS_MODULE);
  306. ret = PTR_ERR(pxa_rtc->rtc);
  307. if (IS_ERR(pxa_rtc->rtc)) {
  308. dev_err(dev, "Failed to register RTC device -> %d\n", ret);
  309. goto err_rtc_reg;
  310. }
  311. device_init_wakeup(dev, 1);
  312. return 0;
  313. err_rtc_reg:
  314. iounmap(pxa_rtc->base);
  315. err_ress:
  316. err_map:
  317. kfree(pxa_rtc);
  318. return ret;
  319. }
  320. static int __exit pxa_rtc_remove(struct platform_device *pdev)
  321. {
  322. struct pxa_rtc *pxa_rtc = platform_get_drvdata(pdev);
  323. rtc_device_unregister(pxa_rtc->rtc);
  324. spin_lock_irq(&pxa_rtc->lock);
  325. iounmap(pxa_rtc->base);
  326. spin_unlock_irq(&pxa_rtc->lock);
  327. kfree(pxa_rtc);
  328. return 0;
  329. }
  330. #ifdef CONFIG_OF
  331. static struct of_device_id pxa_rtc_dt_ids[] = {
  332. { .compatible = "marvell,pxa-rtc" },
  333. {}
  334. };
  335. MODULE_DEVICE_TABLE(of, pxa_rtc_dt_ids);
  336. #endif
  337. #ifdef CONFIG_PM
  338. static int pxa_rtc_suspend(struct device *dev)
  339. {
  340. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  341. if (device_may_wakeup(dev))
  342. enable_irq_wake(pxa_rtc->irq_Alrm);
  343. return 0;
  344. }
  345. static int pxa_rtc_resume(struct device *dev)
  346. {
  347. struct pxa_rtc *pxa_rtc = dev_get_drvdata(dev);
  348. if (device_may_wakeup(dev))
  349. disable_irq_wake(pxa_rtc->irq_Alrm);
  350. return 0;
  351. }
  352. static const struct dev_pm_ops pxa_rtc_pm_ops = {
  353. .suspend = pxa_rtc_suspend,
  354. .resume = pxa_rtc_resume,
  355. };
  356. #endif
  357. static struct platform_driver pxa_rtc_driver = {
  358. .remove = __exit_p(pxa_rtc_remove),
  359. .driver = {
  360. .name = "pxa-rtc",
  361. .of_match_table = of_match_ptr(pxa_rtc_dt_ids),
  362. #ifdef CONFIG_PM
  363. .pm = &pxa_rtc_pm_ops,
  364. #endif
  365. },
  366. };
  367. static int __init pxa_rtc_init(void)
  368. {
  369. if (cpu_is_pxa27x() || cpu_is_pxa3xx())
  370. return platform_driver_probe(&pxa_rtc_driver, pxa_rtc_probe);
  371. return -ENODEV;
  372. }
  373. static void __exit pxa_rtc_exit(void)
  374. {
  375. platform_driver_unregister(&pxa_rtc_driver);
  376. }
  377. module_init(pxa_rtc_init);
  378. module_exit(pxa_rtc_exit);
  379. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  380. MODULE_DESCRIPTION("PXA27x/PXA3xx Realtime Clock Driver (RTC)");
  381. MODULE_LICENSE("GPL");
  382. MODULE_ALIAS("platform:pxa-rtc");