rtc-sirfsoc.c 11 KB

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