rtc-asm9260.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (C) 2016 Oleksij Rempel <linux@rempel-privat.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rtc.h>
  16. /* Miscellaneous registers */
  17. /* Interrupt Location Register */
  18. #define HW_ILR 0x00
  19. #define BM_RTCALF BIT(1)
  20. #define BM_RTCCIF BIT(0)
  21. /* Clock Control Register */
  22. #define HW_CCR 0x08
  23. /* Calibration counter disable */
  24. #define BM_CCALOFF BIT(4)
  25. /* Reset internal oscillator divider */
  26. #define BM_CTCRST BIT(1)
  27. /* Clock Enable */
  28. #define BM_CLKEN BIT(0)
  29. /* Counter Increment Interrupt Register */
  30. #define HW_CIIR 0x0C
  31. #define BM_CIIR_IMYEAR BIT(7)
  32. #define BM_CIIR_IMMON BIT(6)
  33. #define BM_CIIR_IMDOY BIT(5)
  34. #define BM_CIIR_IMDOW BIT(4)
  35. #define BM_CIIR_IMDOM BIT(3)
  36. #define BM_CIIR_IMHOUR BIT(2)
  37. #define BM_CIIR_IMMIN BIT(1)
  38. #define BM_CIIR_IMSEC BIT(0)
  39. /* Alarm Mask Register */
  40. #define HW_AMR 0x10
  41. #define BM_AMR_IMYEAR BIT(7)
  42. #define BM_AMR_IMMON BIT(6)
  43. #define BM_AMR_IMDOY BIT(5)
  44. #define BM_AMR_IMDOW BIT(4)
  45. #define BM_AMR_IMDOM BIT(3)
  46. #define BM_AMR_IMHOUR BIT(2)
  47. #define BM_AMR_IMMIN BIT(1)
  48. #define BM_AMR_IMSEC BIT(0)
  49. #define BM_AMR_OFF 0xff
  50. /* Consolidated time registers */
  51. #define HW_CTIME0 0x14
  52. #define BM_CTIME0_DOW_S 24
  53. #define BM_CTIME0_DOW_M 0x7
  54. #define BM_CTIME0_HOUR_S 16
  55. #define BM_CTIME0_HOUR_M 0x1f
  56. #define BM_CTIME0_MIN_S 8
  57. #define BM_CTIME0_MIN_M 0x3f
  58. #define BM_CTIME0_SEC_S 0
  59. #define BM_CTIME0_SEC_M 0x3f
  60. #define HW_CTIME1 0x18
  61. #define BM_CTIME1_YEAR_S 16
  62. #define BM_CTIME1_YEAR_M 0xfff
  63. #define BM_CTIME1_MON_S 8
  64. #define BM_CTIME1_MON_M 0xf
  65. #define BM_CTIME1_DOM_S 0
  66. #define BM_CTIME1_DOM_M 0x1f
  67. #define HW_CTIME2 0x1C
  68. #define BM_CTIME2_DOY_S 0
  69. #define BM_CTIME2_DOY_M 0xfff
  70. /* Time counter registers */
  71. #define HW_SEC 0x20
  72. #define HW_MIN 0x24
  73. #define HW_HOUR 0x28
  74. #define HW_DOM 0x2C
  75. #define HW_DOW 0x30
  76. #define HW_DOY 0x34
  77. #define HW_MONTH 0x38
  78. #define HW_YEAR 0x3C
  79. #define HW_CALIBRATION 0x40
  80. #define BM_CALDIR_BACK BIT(17)
  81. #define BM_CALVAL_M 0x1ffff
  82. /* General purpose registers */
  83. #define HW_GPREG0 0x44
  84. #define HW_GPREG1 0x48
  85. #define HW_GPREG2 0x4C
  86. #define HW_GPREG3 0x50
  87. #define HW_GPREG4 0x54
  88. /* Alarm register group */
  89. #define HW_ALSEC 0x60
  90. #define HW_ALMIN 0x64
  91. #define HW_ALHOUR 0x68
  92. #define HW_ALDOM 0x6C
  93. #define HW_ALDOW 0x70
  94. #define HW_ALDOY 0x74
  95. #define HW_ALMON 0x78
  96. #define HW_ALYEAR 0x7C
  97. struct asm9260_rtc_priv {
  98. struct device *dev;
  99. void __iomem *iobase;
  100. struct rtc_device *rtc;
  101. struct clk *clk;
  102. };
  103. static irqreturn_t asm9260_rtc_irq(int irq, void *dev_id)
  104. {
  105. struct asm9260_rtc_priv *priv = dev_id;
  106. u32 isr;
  107. unsigned long events = 0;
  108. mutex_lock(&priv->rtc->ops_lock);
  109. isr = ioread32(priv->iobase + HW_CIIR);
  110. if (!isr) {
  111. mutex_unlock(&priv->rtc->ops_lock);
  112. return IRQ_NONE;
  113. }
  114. iowrite32(0, priv->iobase + HW_CIIR);
  115. mutex_unlock(&priv->rtc->ops_lock);
  116. events |= RTC_AF | RTC_IRQF;
  117. rtc_update_irq(priv->rtc, 1, events);
  118. return IRQ_HANDLED;
  119. }
  120. static int asm9260_rtc_read_time(struct device *dev, struct rtc_time *tm)
  121. {
  122. struct asm9260_rtc_priv *priv = dev_get_drvdata(dev);
  123. u32 ctime0, ctime1, ctime2;
  124. ctime0 = ioread32(priv->iobase + HW_CTIME0);
  125. ctime1 = ioread32(priv->iobase + HW_CTIME1);
  126. ctime2 = ioread32(priv->iobase + HW_CTIME2);
  127. if (ctime1 != ioread32(priv->iobase + HW_CTIME1)) {
  128. /*
  129. * woops, counter flipped right now. Now we are safe
  130. * to reread.
  131. */
  132. ctime0 = ioread32(priv->iobase + HW_CTIME0);
  133. ctime1 = ioread32(priv->iobase + HW_CTIME1);
  134. ctime2 = ioread32(priv->iobase + HW_CTIME2);
  135. }
  136. tm->tm_sec = (ctime0 >> BM_CTIME0_SEC_S) & BM_CTIME0_SEC_M;
  137. tm->tm_min = (ctime0 >> BM_CTIME0_MIN_S) & BM_CTIME0_MIN_M;
  138. tm->tm_hour = (ctime0 >> BM_CTIME0_HOUR_S) & BM_CTIME0_HOUR_M;
  139. tm->tm_wday = (ctime0 >> BM_CTIME0_DOW_S) & BM_CTIME0_DOW_M;
  140. tm->tm_mday = (ctime1 >> BM_CTIME1_DOM_S) & BM_CTIME1_DOM_M;
  141. tm->tm_mon = (ctime1 >> BM_CTIME1_MON_S) & BM_CTIME1_MON_M;
  142. tm->tm_year = (ctime1 >> BM_CTIME1_YEAR_S) & BM_CTIME1_YEAR_M;
  143. tm->tm_yday = (ctime2 >> BM_CTIME2_DOY_S) & BM_CTIME2_DOY_M;
  144. return 0;
  145. }
  146. static int asm9260_rtc_set_time(struct device *dev, struct rtc_time *tm)
  147. {
  148. struct asm9260_rtc_priv *priv = dev_get_drvdata(dev);
  149. /*
  150. * make sure SEC counter will not flip other counter on write time,
  151. * real value will be written at the enf of sequence.
  152. */
  153. iowrite32(0, priv->iobase + HW_SEC);
  154. iowrite32(tm->tm_year, priv->iobase + HW_YEAR);
  155. iowrite32(tm->tm_mon, priv->iobase + HW_MONTH);
  156. iowrite32(tm->tm_mday, priv->iobase + HW_DOM);
  157. iowrite32(tm->tm_wday, priv->iobase + HW_DOW);
  158. iowrite32(tm->tm_yday, priv->iobase + HW_DOY);
  159. iowrite32(tm->tm_hour, priv->iobase + HW_HOUR);
  160. iowrite32(tm->tm_min, priv->iobase + HW_MIN);
  161. iowrite32(tm->tm_sec, priv->iobase + HW_SEC);
  162. return 0;
  163. }
  164. static int asm9260_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  165. {
  166. struct asm9260_rtc_priv *priv = dev_get_drvdata(dev);
  167. alrm->time.tm_year = ioread32(priv->iobase + HW_ALYEAR);
  168. alrm->time.tm_mon = ioread32(priv->iobase + HW_ALMON);
  169. alrm->time.tm_mday = ioread32(priv->iobase + HW_ALDOM);
  170. alrm->time.tm_wday = ioread32(priv->iobase + HW_ALDOW);
  171. alrm->time.tm_yday = ioread32(priv->iobase + HW_ALDOY);
  172. alrm->time.tm_hour = ioread32(priv->iobase + HW_ALHOUR);
  173. alrm->time.tm_min = ioread32(priv->iobase + HW_ALMIN);
  174. alrm->time.tm_sec = ioread32(priv->iobase + HW_ALSEC);
  175. alrm->enabled = ioread32(priv->iobase + HW_AMR) ? 1 : 0;
  176. alrm->pending = ioread32(priv->iobase + HW_CIIR) ? 1 : 0;
  177. return rtc_valid_tm(&alrm->time);
  178. }
  179. static int asm9260_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  180. {
  181. struct asm9260_rtc_priv *priv = dev_get_drvdata(dev);
  182. iowrite32(alrm->time.tm_year, priv->iobase + HW_ALYEAR);
  183. iowrite32(alrm->time.tm_mon, priv->iobase + HW_ALMON);
  184. iowrite32(alrm->time.tm_mday, priv->iobase + HW_ALDOM);
  185. iowrite32(alrm->time.tm_wday, priv->iobase + HW_ALDOW);
  186. iowrite32(alrm->time.tm_yday, priv->iobase + HW_ALDOY);
  187. iowrite32(alrm->time.tm_hour, priv->iobase + HW_ALHOUR);
  188. iowrite32(alrm->time.tm_min, priv->iobase + HW_ALMIN);
  189. iowrite32(alrm->time.tm_sec, priv->iobase + HW_ALSEC);
  190. iowrite32(alrm->enabled ? 0 : BM_AMR_OFF, priv->iobase + HW_AMR);
  191. return 0;
  192. }
  193. static int asm9260_alarm_irq_enable(struct device *dev, unsigned int enabled)
  194. {
  195. struct asm9260_rtc_priv *priv = dev_get_drvdata(dev);
  196. iowrite32(enabled ? 0 : BM_AMR_OFF, priv->iobase + HW_AMR);
  197. return 0;
  198. }
  199. static const struct rtc_class_ops asm9260_rtc_ops = {
  200. .read_time = asm9260_rtc_read_time,
  201. .set_time = asm9260_rtc_set_time,
  202. .read_alarm = asm9260_rtc_read_alarm,
  203. .set_alarm = asm9260_rtc_set_alarm,
  204. .alarm_irq_enable = asm9260_alarm_irq_enable,
  205. };
  206. static int asm9260_rtc_probe(struct platform_device *pdev)
  207. {
  208. struct asm9260_rtc_priv *priv;
  209. struct device *dev = &pdev->dev;
  210. struct resource *res;
  211. int irq_alarm, ret;
  212. u32 ccr;
  213. priv = devm_kzalloc(dev, sizeof(struct asm9260_rtc_priv), GFP_KERNEL);
  214. if (!priv)
  215. return -ENOMEM;
  216. priv->dev = &pdev->dev;
  217. platform_set_drvdata(pdev, priv);
  218. irq_alarm = platform_get_irq(pdev, 0);
  219. if (irq_alarm < 0) {
  220. dev_err(dev, "No alarm IRQ resource defined\n");
  221. return irq_alarm;
  222. }
  223. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  224. priv->iobase = devm_ioremap_resource(dev, res);
  225. if (IS_ERR(priv->iobase))
  226. return PTR_ERR(priv->iobase);
  227. priv->clk = devm_clk_get(dev, "ahb");
  228. ret = clk_prepare_enable(priv->clk);
  229. if (ret) {
  230. dev_err(dev, "Failed to enable clk!\n");
  231. return ret;
  232. }
  233. ccr = ioread32(priv->iobase + HW_CCR);
  234. /* if dev is not enabled, reset it */
  235. if ((ccr & (BM_CLKEN | BM_CTCRST)) != BM_CLKEN) {
  236. iowrite32(BM_CTCRST, priv->iobase + HW_CCR);
  237. ccr = 0;
  238. }
  239. iowrite32(BM_CLKEN | ccr, priv->iobase + HW_CCR);
  240. iowrite32(0, priv->iobase + HW_CIIR);
  241. iowrite32(BM_AMR_OFF, priv->iobase + HW_AMR);
  242. priv->rtc = devm_rtc_device_register(dev, dev_name(dev),
  243. &asm9260_rtc_ops, THIS_MODULE);
  244. if (IS_ERR(priv->rtc)) {
  245. ret = PTR_ERR(priv->rtc);
  246. dev_err(dev, "Failed to register RTC device: %d\n", ret);
  247. goto err_return;
  248. }
  249. ret = devm_request_threaded_irq(dev, irq_alarm, NULL,
  250. asm9260_rtc_irq, IRQF_ONESHOT,
  251. dev_name(dev), priv);
  252. if (ret < 0) {
  253. dev_err(dev, "can't get irq %i, err %d\n",
  254. irq_alarm, ret);
  255. goto err_return;
  256. }
  257. return 0;
  258. err_return:
  259. clk_disable_unprepare(priv->clk);
  260. return ret;
  261. }
  262. static int asm9260_rtc_remove(struct platform_device *pdev)
  263. {
  264. struct asm9260_rtc_priv *priv = platform_get_drvdata(pdev);
  265. /* Disable alarm matching */
  266. iowrite32(BM_AMR_OFF, priv->iobase + HW_AMR);
  267. clk_disable_unprepare(priv->clk);
  268. return 0;
  269. }
  270. static const struct of_device_id asm9260_dt_ids[] = {
  271. { .compatible = "alphascale,asm9260-rtc", },
  272. {}
  273. };
  274. MODULE_DEVICE_TABLE(of, asm9260_dt_ids);
  275. static struct platform_driver asm9260_rtc_driver = {
  276. .probe = asm9260_rtc_probe,
  277. .remove = asm9260_rtc_remove,
  278. .driver = {
  279. .name = "asm9260-rtc",
  280. .of_match_table = asm9260_dt_ids,
  281. },
  282. };
  283. module_platform_driver(asm9260_rtc_driver);
  284. MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
  285. MODULE_DESCRIPTION("Alphascale asm9260 SoC Realtime Clock Driver (RTC)");
  286. MODULE_LICENSE("GPL");