rtc-pic32.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PIC32 RTC driver
  4. *
  5. * Joshua Henderson <[email protected]>
  6. * Copyright (C) 2016 Microchip Technology Inc. All rights reserved.
  7. *
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include <linux/clk.h>
  16. #include <linux/rtc.h>
  17. #include <linux/bcd.h>
  18. #include <asm/mach-pic32/pic32.h>
  19. #define PIC32_RTCCON 0x00
  20. #define PIC32_RTCCON_ON BIT(15)
  21. #define PIC32_RTCCON_SIDL BIT(13)
  22. #define PIC32_RTCCON_RTCCLKSEL (3 << 9)
  23. #define PIC32_RTCCON_RTCCLKON BIT(6)
  24. #define PIC32_RTCCON_RTCWREN BIT(3)
  25. #define PIC32_RTCCON_RTCSYNC BIT(2)
  26. #define PIC32_RTCCON_HALFSEC BIT(1)
  27. #define PIC32_RTCCON_RTCOE BIT(0)
  28. #define PIC32_RTCALRM 0x10
  29. #define PIC32_RTCALRM_ALRMEN BIT(15)
  30. #define PIC32_RTCALRM_CHIME BIT(14)
  31. #define PIC32_RTCALRM_PIV BIT(13)
  32. #define PIC32_RTCALRM_ALARMSYNC BIT(12)
  33. #define PIC32_RTCALRM_AMASK 0x0F00
  34. #define PIC32_RTCALRM_ARPT 0xFF
  35. #define PIC32_RTCHOUR 0x23
  36. #define PIC32_RTCMIN 0x22
  37. #define PIC32_RTCSEC 0x21
  38. #define PIC32_RTCYEAR 0x33
  39. #define PIC32_RTCMON 0x32
  40. #define PIC32_RTCDAY 0x31
  41. #define PIC32_ALRMTIME 0x40
  42. #define PIC32_ALRMDATE 0x50
  43. #define PIC32_ALRMHOUR 0x43
  44. #define PIC32_ALRMMIN 0x42
  45. #define PIC32_ALRMSEC 0x41
  46. #define PIC32_ALRMYEAR 0x53
  47. #define PIC32_ALRMMON 0x52
  48. #define PIC32_ALRMDAY 0x51
  49. struct pic32_rtc_dev {
  50. struct rtc_device *rtc;
  51. void __iomem *reg_base;
  52. struct clk *clk;
  53. spinlock_t alarm_lock;
  54. int alarm_irq;
  55. bool alarm_clk_enabled;
  56. };
  57. static void pic32_rtc_alarm_clk_enable(struct pic32_rtc_dev *pdata,
  58. bool enable)
  59. {
  60. unsigned long flags;
  61. spin_lock_irqsave(&pdata->alarm_lock, flags);
  62. if (enable) {
  63. if (!pdata->alarm_clk_enabled) {
  64. clk_enable(pdata->clk);
  65. pdata->alarm_clk_enabled = true;
  66. }
  67. } else {
  68. if (pdata->alarm_clk_enabled) {
  69. clk_disable(pdata->clk);
  70. pdata->alarm_clk_enabled = false;
  71. }
  72. }
  73. spin_unlock_irqrestore(&pdata->alarm_lock, flags);
  74. }
  75. static irqreturn_t pic32_rtc_alarmirq(int irq, void *id)
  76. {
  77. struct pic32_rtc_dev *pdata = (struct pic32_rtc_dev *)id;
  78. clk_enable(pdata->clk);
  79. rtc_update_irq(pdata->rtc, 1, RTC_AF | RTC_IRQF);
  80. clk_disable(pdata->clk);
  81. pic32_rtc_alarm_clk_enable(pdata, false);
  82. return IRQ_HANDLED;
  83. }
  84. static int pic32_rtc_setaie(struct device *dev, unsigned int enabled)
  85. {
  86. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  87. void __iomem *base = pdata->reg_base;
  88. clk_enable(pdata->clk);
  89. writel(PIC32_RTCALRM_ALRMEN,
  90. base + (enabled ? PIC32_SET(PIC32_RTCALRM) :
  91. PIC32_CLR(PIC32_RTCALRM)));
  92. clk_disable(pdata->clk);
  93. pic32_rtc_alarm_clk_enable(pdata, enabled);
  94. return 0;
  95. }
  96. static int pic32_rtc_setfreq(struct device *dev, int freq)
  97. {
  98. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  99. void __iomem *base = pdata->reg_base;
  100. clk_enable(pdata->clk);
  101. writel(PIC32_RTCALRM_AMASK, base + PIC32_CLR(PIC32_RTCALRM));
  102. writel(freq << 8, base + PIC32_SET(PIC32_RTCALRM));
  103. writel(PIC32_RTCALRM_CHIME, base + PIC32_SET(PIC32_RTCALRM));
  104. clk_disable(pdata->clk);
  105. return 0;
  106. }
  107. static int pic32_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  108. {
  109. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  110. void __iomem *base = pdata->reg_base;
  111. unsigned int tries = 0;
  112. clk_enable(pdata->clk);
  113. do {
  114. rtc_tm->tm_hour = readb(base + PIC32_RTCHOUR);
  115. rtc_tm->tm_min = readb(base + PIC32_RTCMIN);
  116. rtc_tm->tm_mon = readb(base + PIC32_RTCMON);
  117. rtc_tm->tm_mday = readb(base + PIC32_RTCDAY);
  118. rtc_tm->tm_year = readb(base + PIC32_RTCYEAR);
  119. rtc_tm->tm_sec = readb(base + PIC32_RTCSEC);
  120. /*
  121. * The only way to work out whether the system was mid-update
  122. * when we read it is to check the second counter, and if it
  123. * is zero, then we re-try the entire read.
  124. */
  125. tries += 1;
  126. } while (rtc_tm->tm_sec == 0 && tries < 2);
  127. rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
  128. rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
  129. rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
  130. rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
  131. rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon) - 1;
  132. rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
  133. rtc_tm->tm_year += 100;
  134. dev_dbg(dev, "read time %ptR\n", rtc_tm);
  135. clk_disable(pdata->clk);
  136. return 0;
  137. }
  138. static int pic32_rtc_settime(struct device *dev, struct rtc_time *tm)
  139. {
  140. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  141. void __iomem *base = pdata->reg_base;
  142. dev_dbg(dev, "set time %ptR\n", tm);
  143. clk_enable(pdata->clk);
  144. writeb(bin2bcd(tm->tm_sec), base + PIC32_RTCSEC);
  145. writeb(bin2bcd(tm->tm_min), base + PIC32_RTCMIN);
  146. writeb(bin2bcd(tm->tm_hour), base + PIC32_RTCHOUR);
  147. writeb(bin2bcd(tm->tm_mday), base + PIC32_RTCDAY);
  148. writeb(bin2bcd(tm->tm_mon + 1), base + PIC32_RTCMON);
  149. writeb(bin2bcd(tm->tm_year - 100), base + PIC32_RTCYEAR);
  150. clk_disable(pdata->clk);
  151. return 0;
  152. }
  153. static int pic32_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
  154. {
  155. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  156. struct rtc_time *alm_tm = &alrm->time;
  157. void __iomem *base = pdata->reg_base;
  158. unsigned int alm_en;
  159. clk_enable(pdata->clk);
  160. alm_tm->tm_sec = readb(base + PIC32_ALRMSEC);
  161. alm_tm->tm_min = readb(base + PIC32_ALRMMIN);
  162. alm_tm->tm_hour = readb(base + PIC32_ALRMHOUR);
  163. alm_tm->tm_mon = readb(base + PIC32_ALRMMON);
  164. alm_tm->tm_mday = readb(base + PIC32_ALRMDAY);
  165. alm_tm->tm_year = readb(base + PIC32_ALRMYEAR);
  166. alm_en = readb(base + PIC32_RTCALRM);
  167. alrm->enabled = (alm_en & PIC32_RTCALRM_ALRMEN) ? 1 : 0;
  168. dev_dbg(dev, "getalarm: %d, %ptR\n", alm_en, alm_tm);
  169. alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
  170. alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
  171. alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
  172. alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
  173. alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon) - 1;
  174. alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
  175. clk_disable(pdata->clk);
  176. return 0;
  177. }
  178. static int pic32_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
  179. {
  180. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  181. struct rtc_time *tm = &alrm->time;
  182. void __iomem *base = pdata->reg_base;
  183. clk_enable(pdata->clk);
  184. dev_dbg(dev, "setalarm: %d, %ptR\n", alrm->enabled, tm);
  185. writel(0x00, base + PIC32_ALRMTIME);
  186. writel(0x00, base + PIC32_ALRMDATE);
  187. pic32_rtc_setaie(dev, alrm->enabled);
  188. clk_disable(pdata->clk);
  189. return 0;
  190. }
  191. static int pic32_rtc_proc(struct device *dev, struct seq_file *seq)
  192. {
  193. struct pic32_rtc_dev *pdata = dev_get_drvdata(dev);
  194. void __iomem *base = pdata->reg_base;
  195. unsigned int repeat;
  196. clk_enable(pdata->clk);
  197. repeat = readw(base + PIC32_RTCALRM);
  198. repeat &= PIC32_RTCALRM_ARPT;
  199. seq_printf(seq, "periodic_IRQ\t: %s\n", repeat ? "yes" : "no");
  200. clk_disable(pdata->clk);
  201. return 0;
  202. }
  203. static const struct rtc_class_ops pic32_rtcops = {
  204. .read_time = pic32_rtc_gettime,
  205. .set_time = pic32_rtc_settime,
  206. .read_alarm = pic32_rtc_getalarm,
  207. .set_alarm = pic32_rtc_setalarm,
  208. .proc = pic32_rtc_proc,
  209. .alarm_irq_enable = pic32_rtc_setaie,
  210. };
  211. static void pic32_rtc_enable(struct pic32_rtc_dev *pdata, int en)
  212. {
  213. void __iomem *base = pdata->reg_base;
  214. if (!base)
  215. return;
  216. clk_enable(pdata->clk);
  217. if (!en) {
  218. writel(PIC32_RTCCON_ON, base + PIC32_CLR(PIC32_RTCCON));
  219. } else {
  220. pic32_syskey_unlock();
  221. writel(PIC32_RTCCON_RTCWREN, base + PIC32_SET(PIC32_RTCCON));
  222. writel(3 << 9, base + PIC32_CLR(PIC32_RTCCON));
  223. if (!(readl(base + PIC32_RTCCON) & PIC32_RTCCON_ON))
  224. writel(PIC32_RTCCON_ON, base + PIC32_SET(PIC32_RTCCON));
  225. }
  226. clk_disable(pdata->clk);
  227. }
  228. static int pic32_rtc_remove(struct platform_device *pdev)
  229. {
  230. struct pic32_rtc_dev *pdata = platform_get_drvdata(pdev);
  231. pic32_rtc_setaie(&pdev->dev, 0);
  232. clk_unprepare(pdata->clk);
  233. pdata->clk = NULL;
  234. return 0;
  235. }
  236. static int pic32_rtc_probe(struct platform_device *pdev)
  237. {
  238. struct pic32_rtc_dev *pdata;
  239. int ret;
  240. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  241. if (!pdata)
  242. return -ENOMEM;
  243. platform_set_drvdata(pdev, pdata);
  244. pdata->alarm_irq = platform_get_irq(pdev, 0);
  245. if (pdata->alarm_irq < 0)
  246. return pdata->alarm_irq;
  247. pdata->reg_base = devm_platform_ioremap_resource(pdev, 0);
  248. if (IS_ERR(pdata->reg_base))
  249. return PTR_ERR(pdata->reg_base);
  250. pdata->clk = devm_clk_get(&pdev->dev, NULL);
  251. if (IS_ERR(pdata->clk)) {
  252. dev_err(&pdev->dev, "failed to find rtc clock source\n");
  253. ret = PTR_ERR(pdata->clk);
  254. pdata->clk = NULL;
  255. return ret;
  256. }
  257. spin_lock_init(&pdata->alarm_lock);
  258. pdata->rtc = devm_rtc_allocate_device(&pdev->dev);
  259. if (IS_ERR(pdata->rtc))
  260. return PTR_ERR(pdata->rtc);
  261. clk_prepare_enable(pdata->clk);
  262. pic32_rtc_enable(pdata, 1);
  263. device_init_wakeup(&pdev->dev, 1);
  264. pdata->rtc->ops = &pic32_rtcops;
  265. pdata->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
  266. pdata->rtc->range_max = RTC_TIMESTAMP_END_2099;
  267. ret = devm_rtc_register_device(pdata->rtc);
  268. if (ret)
  269. goto err_nortc;
  270. pdata->rtc->max_user_freq = 128;
  271. pic32_rtc_setfreq(&pdev->dev, 1);
  272. ret = devm_request_irq(&pdev->dev, pdata->alarm_irq,
  273. pic32_rtc_alarmirq, 0,
  274. dev_name(&pdev->dev), pdata);
  275. if (ret) {
  276. dev_err(&pdev->dev,
  277. "IRQ %d error %d\n", pdata->alarm_irq, ret);
  278. goto err_nortc;
  279. }
  280. clk_disable(pdata->clk);
  281. return 0;
  282. err_nortc:
  283. pic32_rtc_enable(pdata, 0);
  284. clk_disable_unprepare(pdata->clk);
  285. return ret;
  286. }
  287. static const struct of_device_id pic32_rtc_dt_ids[] = {
  288. { .compatible = "microchip,pic32mzda-rtc" },
  289. { /* sentinel */ }
  290. };
  291. MODULE_DEVICE_TABLE(of, pic32_rtc_dt_ids);
  292. static struct platform_driver pic32_rtc_driver = {
  293. .probe = pic32_rtc_probe,
  294. .remove = pic32_rtc_remove,
  295. .driver = {
  296. .name = "pic32-rtc",
  297. .of_match_table = of_match_ptr(pic32_rtc_dt_ids),
  298. },
  299. };
  300. module_platform_driver(pic32_rtc_driver);
  301. MODULE_DESCRIPTION("Microchip PIC32 RTC Driver");
  302. MODULE_AUTHOR("Joshua Henderson <[email protected]>");
  303. MODULE_LICENSE("GPL");