rtc-sirfsoc.c 11 KB

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