rtc-sirfsoc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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_ioctl(struct device *dev, unsigned int cmd,
  172. unsigned long arg)
  173. {
  174. switch (cmd) {
  175. case RTC_PIE_ON:
  176. case RTC_PIE_OFF:
  177. case RTC_UIE_ON:
  178. case RTC_UIE_OFF:
  179. case RTC_AIE_ON:
  180. case RTC_AIE_OFF:
  181. return 0;
  182. default:
  183. return -ENOIOCTLCMD;
  184. }
  185. }
  186. static int sirfsoc_rtc_alarm_irq_enable(struct device *dev,
  187. unsigned int enabled)
  188. {
  189. unsigned long rtc_status_reg = 0x0;
  190. struct sirfsoc_rtc_drv *rtcdrv;
  191. rtcdrv = dev_get_drvdata(dev);
  192. spin_lock_irq(&rtcdrv->lock);
  193. rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
  194. if (enabled)
  195. rtc_status_reg |= SIRFSOC_RTC_AL0E;
  196. else
  197. rtc_status_reg &= ~SIRFSOC_RTC_AL0E;
  198. sirfsoc_rtc_writel(rtcdrv, RTC_STATUS, rtc_status_reg);
  199. spin_unlock_irq(&rtcdrv->lock);
  200. return 0;
  201. }
  202. static const struct rtc_class_ops sirfsoc_rtc_ops = {
  203. .read_time = sirfsoc_rtc_read_time,
  204. .set_time = sirfsoc_rtc_set_time,
  205. .read_alarm = sirfsoc_rtc_read_alarm,
  206. .set_alarm = sirfsoc_rtc_set_alarm,
  207. .ioctl = sirfsoc_rtc_ioctl,
  208. .alarm_irq_enable = sirfsoc_rtc_alarm_irq_enable
  209. };
  210. static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
  211. {
  212. struct sirfsoc_rtc_drv *rtcdrv = pdata;
  213. unsigned long rtc_status_reg = 0x0;
  214. unsigned long events = 0x0;
  215. spin_lock(&rtcdrv->lock);
  216. rtc_status_reg = sirfsoc_rtc_readl(rtcdrv, RTC_STATUS);
  217. /* this bit will be set ONLY if an alarm was active
  218. * and it expired NOW
  219. * So this is being used as an ASSERT
  220. */
  221. if (rtc_status_reg & SIRFSOC_RTC_AL0) {
  222. /*
  223. * clear the RTC status register's alarm bit
  224. * mask out the lower status bits
  225. */
  226. rtc_status_reg &= ~0x07;
  227. /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
  228. rtc_status_reg |= (SIRFSOC_RTC_AL0);
  229. /* Clear the Alarm enable bit */
  230. rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
  231. }
  232. sirfsoc_rtc_writel(rtcdrv, RTC_STATUS, rtc_status_reg);
  233. spin_unlock(&rtcdrv->lock);
  234. /* this should wake up any apps polling/waiting on the read
  235. * after setting the alarm
  236. */
  237. events |= RTC_IRQF | RTC_AF;
  238. rtc_update_irq(rtcdrv->rtc, 1, events);
  239. return IRQ_HANDLED;
  240. }
  241. static const struct of_device_id sirfsoc_rtc_of_match[] = {
  242. { .compatible = "sirf,prima2-sysrtc"},
  243. {},
  244. };
  245. const struct regmap_config sysrtc_regmap_config = {
  246. .reg_bits = 32,
  247. .val_bits = 32,
  248. .fast_io = true,
  249. };
  250. MODULE_DEVICE_TABLE(of, sirfsoc_rtc_of_match);
  251. static int sirfsoc_rtc_probe(struct platform_device *pdev)
  252. {
  253. int err;
  254. unsigned long rtc_div;
  255. struct sirfsoc_rtc_drv *rtcdrv;
  256. struct device_node *np = pdev->dev.of_node;
  257. rtcdrv = devm_kzalloc(&pdev->dev,
  258. sizeof(struct sirfsoc_rtc_drv), GFP_KERNEL);
  259. if (rtcdrv == NULL)
  260. return -ENOMEM;
  261. spin_lock_init(&rtcdrv->lock);
  262. err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
  263. if (err) {
  264. dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
  265. return err;
  266. }
  267. platform_set_drvdata(pdev, rtcdrv);
  268. /* Register rtc alarm as a wakeup source */
  269. device_init_wakeup(&pdev->dev, 1);
  270. rtcdrv->regmap = devm_regmap_init_iobg(&pdev->dev,
  271. &sysrtc_regmap_config);
  272. if (IS_ERR(rtcdrv->regmap)) {
  273. err = PTR_ERR(rtcdrv->regmap);
  274. dev_err(&pdev->dev, "Failed to allocate register map: %d\n",
  275. err);
  276. return err;
  277. }
  278. /*
  279. * Set SYS_RTC counter in RTC_HZ HZ Units
  280. * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
  281. * If 16HZ, therefore RTC_DIV = 1023;
  282. */
  283. rtc_div = ((32768 / RTC_HZ) / 2) - 1;
  284. sirfsoc_rtc_writel(rtcdrv, RTC_DIV, rtc_div);
  285. /* 0x3 -> RTC_CLK */
  286. sirfsoc_rtc_writel(rtcdrv, RTC_CLOCK_SWITCH, SIRFSOC_RTC_CLK);
  287. /* reset SYS RTC ALARM0 */
  288. sirfsoc_rtc_writel(rtcdrv, RTC_ALARM0, 0x0);
  289. /* reset SYS RTC ALARM1 */
  290. sirfsoc_rtc_writel(rtcdrv, RTC_ALARM1, 0x0);
  291. /* Restore RTC Overflow From Register After Command Reboot */
  292. rtcdrv->overflow_rtc =
  293. sirfsoc_rtc_readl(rtcdrv, RTC_SW_VALUE);
  294. rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  295. &sirfsoc_rtc_ops, THIS_MODULE);
  296. if (IS_ERR(rtcdrv->rtc)) {
  297. err = PTR_ERR(rtcdrv->rtc);
  298. dev_err(&pdev->dev, "can't register RTC device\n");
  299. return err;
  300. }
  301. rtcdrv->irq = platform_get_irq(pdev, 0);
  302. err = devm_request_irq(
  303. &pdev->dev,
  304. rtcdrv->irq,
  305. sirfsoc_rtc_irq_handler,
  306. IRQF_SHARED,
  307. pdev->name,
  308. rtcdrv);
  309. if (err) {
  310. dev_err(&pdev->dev, "Unable to register for the SiRF SOC RTC IRQ\n");
  311. return err;
  312. }
  313. return 0;
  314. }
  315. static int sirfsoc_rtc_remove(struct platform_device *pdev)
  316. {
  317. device_init_wakeup(&pdev->dev, 0);
  318. return 0;
  319. }
  320. #ifdef CONFIG_PM_SLEEP
  321. static int sirfsoc_rtc_suspend(struct device *dev)
  322. {
  323. struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
  324. rtcdrv->overflow_rtc =
  325. sirfsoc_rtc_readl(rtcdrv, RTC_SW_VALUE);
  326. rtcdrv->saved_counter =
  327. sirfsoc_rtc_readl(rtcdrv, RTC_CN);
  328. rtcdrv->saved_overflow_rtc = rtcdrv->overflow_rtc;
  329. if (device_may_wakeup(dev) && !enable_irq_wake(rtcdrv->irq))
  330. rtcdrv->irq_wake = 1;
  331. return 0;
  332. }
  333. static int sirfsoc_rtc_resume(struct device *dev)
  334. {
  335. u32 tmp;
  336. struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
  337. /*
  338. * if resume from snapshot and the rtc power is lost,
  339. * restroe the rtc settings
  340. */
  341. if (SIRFSOC_RTC_CLK != sirfsoc_rtc_readl(rtcdrv, RTC_CLOCK_SWITCH)) {
  342. u32 rtc_div;
  343. /* 0x3 -> RTC_CLK */
  344. sirfsoc_rtc_writel(rtcdrv, RTC_CLOCK_SWITCH, SIRFSOC_RTC_CLK);
  345. /*
  346. * Set SYS_RTC counter in RTC_HZ HZ Units
  347. * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
  348. * If 16HZ, therefore RTC_DIV = 1023;
  349. */
  350. rtc_div = ((32768 / RTC_HZ) / 2) - 1;
  351. sirfsoc_rtc_writel(rtcdrv, RTC_DIV, rtc_div);
  352. /* reset SYS RTC ALARM0 */
  353. sirfsoc_rtc_writel(rtcdrv, RTC_ALARM0, 0x0);
  354. /* reset SYS RTC ALARM1 */
  355. sirfsoc_rtc_writel(rtcdrv, RTC_ALARM1, 0x0);
  356. }
  357. rtcdrv->overflow_rtc = rtcdrv->saved_overflow_rtc;
  358. /*
  359. * if current counter is small than previous,
  360. * it means overflow in sleep
  361. */
  362. tmp = sirfsoc_rtc_readl(rtcdrv, RTC_CN);
  363. if (tmp <= rtcdrv->saved_counter)
  364. rtcdrv->overflow_rtc++;
  365. /*
  366. *PWRC Value Be Changed When Suspend, Restore Overflow
  367. * In Memory To Register
  368. */
  369. sirfsoc_rtc_writel(rtcdrv, RTC_SW_VALUE, rtcdrv->overflow_rtc);
  370. if (device_may_wakeup(dev) && rtcdrv->irq_wake) {
  371. disable_irq_wake(rtcdrv->irq);
  372. rtcdrv->irq_wake = 0;
  373. }
  374. return 0;
  375. }
  376. #endif
  377. static SIMPLE_DEV_PM_OPS(sirfsoc_rtc_pm_ops,
  378. sirfsoc_rtc_suspend, sirfsoc_rtc_resume);
  379. static struct platform_driver sirfsoc_rtc_driver = {
  380. .driver = {
  381. .name = "sirfsoc-rtc",
  382. .pm = &sirfsoc_rtc_pm_ops,
  383. .of_match_table = sirfsoc_rtc_of_match,
  384. },
  385. .probe = sirfsoc_rtc_probe,
  386. .remove = sirfsoc_rtc_remove,
  387. };
  388. module_platform_driver(sirfsoc_rtc_driver);
  389. MODULE_DESCRIPTION("SiRF SoC rtc driver");
  390. MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
  391. MODULE_LICENSE("GPL v2");
  392. MODULE_ALIAS("platform:sirfsoc-rtc");