rtc-pxa.c 12 KB

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