rtc-pic32.c 11 KB

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