rtc-pxa.c 12 KB

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