rtc-sirfsoc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * SiRFSoC Real Time Clock interface for Linux
  3. *
  4. * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/err.h>
  10. #include <linux/rtc.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/rtc/sirfsoc_rtciobrg.h>
  16. #define RTC_CN 0x00
  17. #define RTC_ALARM0 0x04
  18. #define RTC_ALARM1 0x18
  19. #define RTC_STATUS 0x08
  20. #define RTC_SW_VALUE 0x40
  21. #define SIRFSOC_RTC_AL1E (1<<6)
  22. #define SIRFSOC_RTC_AL1 (1<<4)
  23. #define SIRFSOC_RTC_HZE (1<<3)
  24. #define SIRFSOC_RTC_AL0E (1<<2)
  25. #define SIRFSOC_RTC_HZ (1<<1)
  26. #define SIRFSOC_RTC_AL0 (1<<0)
  27. #define RTC_DIV 0x0c
  28. #define RTC_DEEP_CTRL 0x14
  29. #define RTC_CLOCK_SWITCH 0x1c
  30. #define SIRFSOC_RTC_CLK 0x03 /* others are reserved */
  31. /* Refer to RTC DIV switch */
  32. #define RTC_HZ 16
  33. /* This macro is also defined in arch/arm/plat-sirfsoc/cpu.c */
  34. #define RTC_SHIFT 4
  35. #define INTR_SYSRTC_CN 0x48
  36. struct sirfsoc_rtc_drv {
  37. struct rtc_device *rtc;
  38. u32 rtc_base;
  39. u32 irq;
  40. /* Overflow for every 8 years extra time */
  41. u32 overflow_rtc;
  42. #ifdef CONFIG_PM
  43. u32 saved_counter;
  44. u32 saved_overflow_rtc;
  45. #endif
  46. };
  47. static int sirfsoc_rtc_read_alarm(struct device *dev,
  48. struct rtc_wkalrm *alrm)
  49. {
  50. unsigned long rtc_alarm, rtc_count;
  51. struct sirfsoc_rtc_drv *rtcdrv;
  52. rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
  53. local_irq_disable();
  54. rtc_count = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
  55. rtc_alarm = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_ALARM0);
  56. memset(alrm, 0, sizeof(struct rtc_wkalrm));
  57. /*
  58. * assume alarm interval not beyond one round counter overflow_rtc:
  59. * 0->0xffffffff
  60. */
  61. /* if alarm is in next overflow cycle */
  62. if (rtc_count > rtc_alarm)
  63. rtc_time_to_tm((rtcdrv->overflow_rtc + 1)
  64. << (BITS_PER_LONG - RTC_SHIFT)
  65. | rtc_alarm >> RTC_SHIFT, &(alrm->time));
  66. else
  67. rtc_time_to_tm(rtcdrv->overflow_rtc
  68. << (BITS_PER_LONG - RTC_SHIFT)
  69. | rtc_alarm >> RTC_SHIFT, &(alrm->time));
  70. if (sirfsoc_rtc_iobrg_readl(
  71. rtcdrv->rtc_base + RTC_STATUS) & SIRFSOC_RTC_AL0E)
  72. alrm->enabled = 1;
  73. local_irq_enable();
  74. return 0;
  75. }
  76. static int sirfsoc_rtc_set_alarm(struct device *dev,
  77. struct rtc_wkalrm *alrm)
  78. {
  79. unsigned long rtc_status_reg, rtc_alarm;
  80. struct sirfsoc_rtc_drv *rtcdrv;
  81. rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
  82. if (alrm->enabled) {
  83. rtc_tm_to_time(&(alrm->time), &rtc_alarm);
  84. local_irq_disable();
  85. rtc_status_reg = sirfsoc_rtc_iobrg_readl(
  86. rtcdrv->rtc_base + RTC_STATUS);
  87. if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
  88. /*
  89. * An ongoing alarm in progress - ingore it and not
  90. * to return EBUSY
  91. */
  92. dev_info(dev, "An old alarm was set, will be replaced by a new one\n");
  93. }
  94. sirfsoc_rtc_iobrg_writel(
  95. rtc_alarm << RTC_SHIFT, rtcdrv->rtc_base + RTC_ALARM0);
  96. rtc_status_reg &= ~0x07; /* mask out the lower status bits */
  97. /*
  98. * This bit RTC_AL sets it as a wake-up source for Sleep Mode
  99. * Writing 1 into this bit will clear it
  100. */
  101. rtc_status_reg |= SIRFSOC_RTC_AL0;
  102. /* enable the RTC alarm interrupt */
  103. rtc_status_reg |= SIRFSOC_RTC_AL0E;
  104. sirfsoc_rtc_iobrg_writel(
  105. rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
  106. local_irq_enable();
  107. } else {
  108. /*
  109. * if this function was called with enabled=0
  110. * then it could mean that the application is
  111. * trying to cancel an ongoing alarm
  112. */
  113. local_irq_disable();
  114. rtc_status_reg = sirfsoc_rtc_iobrg_readl(
  115. rtcdrv->rtc_base + RTC_STATUS);
  116. if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
  117. /* clear the RTC status register's alarm bit */
  118. rtc_status_reg &= ~0x07;
  119. /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
  120. rtc_status_reg |= (SIRFSOC_RTC_AL0);
  121. /* Clear the Alarm enable bit */
  122. rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
  123. sirfsoc_rtc_iobrg_writel(rtc_status_reg,
  124. rtcdrv->rtc_base + RTC_STATUS);
  125. }
  126. local_irq_enable();
  127. }
  128. return 0;
  129. }
  130. static int sirfsoc_rtc_read_time(struct device *dev,
  131. struct rtc_time *tm)
  132. {
  133. unsigned long tmp_rtc = 0;
  134. struct sirfsoc_rtc_drv *rtcdrv;
  135. rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
  136. /*
  137. * This patch is taken from WinCE - Need to validate this for
  138. * correctness. To work around sirfsoc RTC counter double sync logic
  139. * fail, read several times to make sure get stable value.
  140. */
  141. do {
  142. tmp_rtc = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
  143. cpu_relax();
  144. } while (tmp_rtc != sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN));
  145. rtc_time_to_tm(rtcdrv->overflow_rtc << (BITS_PER_LONG - RTC_SHIFT) |
  146. tmp_rtc >> RTC_SHIFT, tm);
  147. return 0;
  148. }
  149. static int sirfsoc_rtc_set_time(struct device *dev,
  150. struct rtc_time *tm)
  151. {
  152. unsigned long rtc_time;
  153. struct sirfsoc_rtc_drv *rtcdrv;
  154. rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
  155. rtc_tm_to_time(tm, &rtc_time);
  156. rtcdrv->overflow_rtc = rtc_time >> (BITS_PER_LONG - RTC_SHIFT);
  157. sirfsoc_rtc_iobrg_writel(rtcdrv->overflow_rtc,
  158. rtcdrv->rtc_base + RTC_SW_VALUE);
  159. sirfsoc_rtc_iobrg_writel(
  160. rtc_time << RTC_SHIFT, rtcdrv->rtc_base + RTC_CN);
  161. return 0;
  162. }
  163. static int sirfsoc_rtc_ioctl(struct device *dev, unsigned int cmd,
  164. unsigned long arg)
  165. {
  166. switch (cmd) {
  167. case RTC_PIE_ON:
  168. case RTC_PIE_OFF:
  169. case RTC_UIE_ON:
  170. case RTC_UIE_OFF:
  171. case RTC_AIE_ON:
  172. case RTC_AIE_OFF:
  173. return 0;
  174. default:
  175. return -ENOIOCTLCMD;
  176. }
  177. }
  178. static const struct rtc_class_ops sirfsoc_rtc_ops = {
  179. .read_time = sirfsoc_rtc_read_time,
  180. .set_time = sirfsoc_rtc_set_time,
  181. .read_alarm = sirfsoc_rtc_read_alarm,
  182. .set_alarm = sirfsoc_rtc_set_alarm,
  183. .ioctl = sirfsoc_rtc_ioctl
  184. };
  185. static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
  186. {
  187. struct sirfsoc_rtc_drv *rtcdrv = pdata;
  188. unsigned long rtc_status_reg = 0x0;
  189. unsigned long events = 0x0;
  190. rtc_status_reg = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_STATUS);
  191. /* this bit will be set ONLY if an alarm was active
  192. * and it expired NOW
  193. * So this is being used as an ASSERT
  194. */
  195. if (rtc_status_reg & SIRFSOC_RTC_AL0) {
  196. /*
  197. * clear the RTC status register's alarm bit
  198. * mask out the lower status bits
  199. */
  200. rtc_status_reg &= ~0x07;
  201. /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
  202. rtc_status_reg |= (SIRFSOC_RTC_AL0);
  203. /* Clear the Alarm enable bit */
  204. rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
  205. }
  206. sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
  207. /* this should wake up any apps polling/waiting on the read
  208. * after setting the alarm
  209. */
  210. events |= RTC_IRQF | RTC_AF;
  211. rtc_update_irq(rtcdrv->rtc, 1, events);
  212. return IRQ_HANDLED;
  213. }
  214. static const struct of_device_id sirfsoc_rtc_of_match[] = {
  215. { .compatible = "sirf,prima2-sysrtc"},
  216. {},
  217. };
  218. MODULE_DEVICE_TABLE(of, sirfsoc_rtc_of_match);
  219. static int sirfsoc_rtc_probe(struct platform_device *pdev)
  220. {
  221. int err;
  222. unsigned long rtc_div;
  223. struct sirfsoc_rtc_drv *rtcdrv;
  224. struct device_node *np = pdev->dev.of_node;
  225. rtcdrv = devm_kzalloc(&pdev->dev,
  226. sizeof(struct sirfsoc_rtc_drv), GFP_KERNEL);
  227. if (rtcdrv == NULL) {
  228. dev_err(&pdev->dev,
  229. "%s: can't alloc mem for drv struct\n",
  230. pdev->name);
  231. return -ENOMEM;
  232. }
  233. err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
  234. if (err) {
  235. dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
  236. goto error;
  237. }
  238. platform_set_drvdata(pdev, rtcdrv);
  239. /* Register rtc alarm as a wakeup source */
  240. device_init_wakeup(&pdev->dev, 1);
  241. /*
  242. * Set SYS_RTC counter in RTC_HZ HZ Units
  243. * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
  244. * If 16HZ, therefore RTC_DIV = 1023;
  245. */
  246. rtc_div = ((32768 / RTC_HZ) / 2) - 1;
  247. sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
  248. rtcdrv->rtc = rtc_device_register(pdev->name, &(pdev->dev),
  249. &sirfsoc_rtc_ops, THIS_MODULE);
  250. if (IS_ERR(rtcdrv->rtc)) {
  251. err = PTR_ERR(rtcdrv->rtc);
  252. dev_err(&pdev->dev, "can't register RTC device\n");
  253. return err;
  254. }
  255. /* 0x3 -> RTC_CLK */
  256. sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
  257. rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
  258. /* reset SYS RTC ALARM0 */
  259. sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
  260. /* reset SYS RTC ALARM1 */
  261. sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
  262. /* Restore RTC Overflow From Register After Command Reboot */
  263. rtcdrv->overflow_rtc =
  264. sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
  265. rtcdrv->irq = platform_get_irq(pdev, 0);
  266. err = devm_request_irq(
  267. &pdev->dev,
  268. rtcdrv->irq,
  269. sirfsoc_rtc_irq_handler,
  270. IRQF_SHARED,
  271. pdev->name,
  272. rtcdrv);
  273. if (err) {
  274. dev_err(&pdev->dev, "Unable to register for the SiRF SOC RTC IRQ\n");
  275. goto error;
  276. }
  277. return 0;
  278. error:
  279. if (rtcdrv->rtc)
  280. rtc_device_unregister(rtcdrv->rtc);
  281. return err;
  282. }
  283. static int sirfsoc_rtc_remove(struct platform_device *pdev)
  284. {
  285. struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
  286. device_init_wakeup(&pdev->dev, 0);
  287. rtc_device_unregister(rtcdrv->rtc);
  288. return 0;
  289. }
  290. #ifdef CONFIG_PM
  291. static int sirfsoc_rtc_suspend(struct device *dev)
  292. {
  293. struct platform_device *pdev = to_platform_device(dev);
  294. struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
  295. rtcdrv->overflow_rtc =
  296. sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
  297. rtcdrv->saved_counter =
  298. sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
  299. rtcdrv->saved_overflow_rtc = rtcdrv->overflow_rtc;
  300. if (device_may_wakeup(&pdev->dev))
  301. enable_irq_wake(rtcdrv->irq);
  302. return 0;
  303. }
  304. static int sirfsoc_rtc_freeze(struct device *dev)
  305. {
  306. sirfsoc_rtc_suspend(dev);
  307. return 0;
  308. }
  309. static int sirfsoc_rtc_thaw(struct device *dev)
  310. {
  311. u32 tmp;
  312. struct sirfsoc_rtc_drv *rtcdrv;
  313. rtcdrv = (struct sirfsoc_rtc_drv *)dev_get_drvdata(dev);
  314. /*
  315. * if resume from snapshot and the rtc power is losed,
  316. * restroe the rtc settings
  317. */
  318. if (SIRFSOC_RTC_CLK != sirfsoc_rtc_iobrg_readl(
  319. rtcdrv->rtc_base + RTC_CLOCK_SWITCH)) {
  320. u32 rtc_div;
  321. /* 0x3 -> RTC_CLK */
  322. sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
  323. rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
  324. /*
  325. * Set SYS_RTC counter in RTC_HZ HZ Units
  326. * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
  327. * If 16HZ, therefore RTC_DIV = 1023;
  328. */
  329. rtc_div = ((32768 / RTC_HZ) / 2) - 1;
  330. sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
  331. /* reset SYS RTC ALARM0 */
  332. sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
  333. /* reset SYS RTC ALARM1 */
  334. sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
  335. }
  336. rtcdrv->overflow_rtc = rtcdrv->saved_overflow_rtc;
  337. /*
  338. * if current counter is small than previous,
  339. * it means overflow in sleep
  340. */
  341. tmp = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
  342. if (tmp <= rtcdrv->saved_counter)
  343. rtcdrv->overflow_rtc++;
  344. /*
  345. *PWRC Value Be Changed When Suspend, Restore Overflow
  346. * In Memory To Register
  347. */
  348. sirfsoc_rtc_iobrg_writel(rtcdrv->overflow_rtc,
  349. rtcdrv->rtc_base + RTC_SW_VALUE);
  350. return 0;
  351. }
  352. static int sirfsoc_rtc_resume(struct device *dev)
  353. {
  354. struct platform_device *pdev = to_platform_device(dev);
  355. struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
  356. sirfsoc_rtc_thaw(dev);
  357. if (device_may_wakeup(&pdev->dev))
  358. disable_irq_wake(rtcdrv->irq);
  359. return 0;
  360. }
  361. static int sirfsoc_rtc_restore(struct device *dev)
  362. {
  363. struct platform_device *pdev = to_platform_device(dev);
  364. struct sirfsoc_rtc_drv *rtcdrv = platform_get_drvdata(pdev);
  365. if (device_may_wakeup(&pdev->dev))
  366. disable_irq_wake(rtcdrv->irq);
  367. return 0;
  368. }
  369. #else
  370. #define sirfsoc_rtc_suspend NULL
  371. #define sirfsoc_rtc_resume NULL
  372. #define sirfsoc_rtc_freeze NULL
  373. #define sirfsoc_rtc_thaw NULL
  374. #define sirfsoc_rtc_restore NULL
  375. #endif
  376. static const struct dev_pm_ops sirfsoc_rtc_pm_ops = {
  377. .suspend = sirfsoc_rtc_suspend,
  378. .resume = sirfsoc_rtc_resume,
  379. .freeze = sirfsoc_rtc_freeze,
  380. .thaw = sirfsoc_rtc_thaw,
  381. .restore = sirfsoc_rtc_restore,
  382. };
  383. static struct platform_driver sirfsoc_rtc_driver = {
  384. .driver = {
  385. .name = "sirfsoc-rtc",
  386. .owner = THIS_MODULE,
  387. #ifdef CONFIG_PM
  388. .pm = &sirfsoc_rtc_pm_ops,
  389. #endif
  390. .of_match_table = of_match_ptr(sirfsoc_rtc_of_match),
  391. },
  392. .probe = sirfsoc_rtc_probe,
  393. .remove = sirfsoc_rtc_remove,
  394. };
  395. module_platform_driver(sirfsoc_rtc_driver);
  396. MODULE_DESCRIPTION("SiRF SoC rtc driver");
  397. MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
  398. MODULE_LICENSE("GPL v2");
  399. MODULE_ALIAS("platform:sirfsoc-rtc");