rtc-pxa.c 12 KB

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