rtc-sunxi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * An RTC driver for Allwinner A10/A20
  4. *
  5. * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/fs.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/rtc.h>
  18. #include <linux/types.h>
  19. #define SUNXI_LOSC_CTRL 0x0000
  20. #define SUNXI_LOSC_CTRL_RTC_HMS_ACC BIT(8)
  21. #define SUNXI_LOSC_CTRL_RTC_YMD_ACC BIT(7)
  22. #define SUNXI_RTC_YMD 0x0004
  23. #define SUNXI_RTC_HMS 0x0008
  24. #define SUNXI_ALRM_DHMS 0x000c
  25. #define SUNXI_ALRM_EN 0x0014
  26. #define SUNXI_ALRM_EN_CNT_EN BIT(8)
  27. #define SUNXI_ALRM_IRQ_EN 0x0018
  28. #define SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
  29. #define SUNXI_ALRM_IRQ_STA 0x001c
  30. #define SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
  31. #define SUNXI_MASK_DH 0x0000001f
  32. #define SUNXI_MASK_SM 0x0000003f
  33. #define SUNXI_MASK_M 0x0000000f
  34. #define SUNXI_MASK_LY 0x00000001
  35. #define SUNXI_MASK_D 0x00000ffe
  36. #define SUNXI_MASK_M 0x0000000f
  37. #define SUNXI_GET(x, mask, shift) (((x) & ((mask) << (shift))) \
  38. >> (shift))
  39. #define SUNXI_SET(x, mask, shift) (((x) & (mask)) << (shift))
  40. /*
  41. * Get date values
  42. */
  43. #define SUNXI_DATE_GET_DAY_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 0)
  44. #define SUNXI_DATE_GET_MON_VALUE(x) SUNXI_GET(x, SUNXI_MASK_M, 8)
  45. #define SUNXI_DATE_GET_YEAR_VALUE(x, mask) SUNXI_GET(x, mask, 16)
  46. /*
  47. * Get time values
  48. */
  49. #define SUNXI_TIME_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
  50. #define SUNXI_TIME_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
  51. #define SUNXI_TIME_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
  52. /*
  53. * Get alarm values
  54. */
  55. #define SUNXI_ALRM_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
  56. #define SUNXI_ALRM_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
  57. #define SUNXI_ALRM_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
  58. /*
  59. * Set date values
  60. */
  61. #define SUNXI_DATE_SET_DAY_VALUE(x) SUNXI_DATE_GET_DAY_VALUE(x)
  62. #define SUNXI_DATE_SET_MON_VALUE(x) SUNXI_SET(x, SUNXI_MASK_M, 8)
  63. #define SUNXI_DATE_SET_YEAR_VALUE(x, mask) SUNXI_SET(x, mask, 16)
  64. #define SUNXI_LEAP_SET_VALUE(x, shift) SUNXI_SET(x, SUNXI_MASK_LY, shift)
  65. /*
  66. * Set time values
  67. */
  68. #define SUNXI_TIME_SET_SEC_VALUE(x) SUNXI_TIME_GET_SEC_VALUE(x)
  69. #define SUNXI_TIME_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
  70. #define SUNXI_TIME_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
  71. /*
  72. * Set alarm values
  73. */
  74. #define SUNXI_ALRM_SET_SEC_VALUE(x) SUNXI_ALRM_GET_SEC_VALUE(x)
  75. #define SUNXI_ALRM_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
  76. #define SUNXI_ALRM_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
  77. #define SUNXI_ALRM_SET_DAY_VALUE(x) SUNXI_SET(x, SUNXI_MASK_D, 21)
  78. /*
  79. * Time unit conversions
  80. */
  81. #define SEC_IN_MIN 60
  82. #define SEC_IN_HOUR (60 * SEC_IN_MIN)
  83. #define SEC_IN_DAY (24 * SEC_IN_HOUR)
  84. /*
  85. * The year parameter passed to the driver is usually an offset relative to
  86. * the year 1900. This macro is used to convert this offset to another one
  87. * relative to the minimum year allowed by the hardware.
  88. */
  89. #define SUNXI_YEAR_OFF(x) ((x)->min - 1900)
  90. /*
  91. * min and max year are arbitrary set considering the limited range of the
  92. * hardware register field
  93. */
  94. struct sunxi_rtc_data_year {
  95. unsigned int min; /* min year allowed */
  96. unsigned int max; /* max year allowed */
  97. unsigned int mask; /* mask for the year field */
  98. unsigned char leap_shift; /* bit shift to get the leap year */
  99. };
  100. static const struct sunxi_rtc_data_year data_year_param[] = {
  101. [0] = {
  102. .min = 2010,
  103. .max = 2073,
  104. .mask = 0x3f,
  105. .leap_shift = 22,
  106. },
  107. [1] = {
  108. .min = 1970,
  109. .max = 2225,
  110. .mask = 0xff,
  111. .leap_shift = 24,
  112. },
  113. };
  114. struct sunxi_rtc_dev {
  115. struct rtc_device *rtc;
  116. struct device *dev;
  117. const struct sunxi_rtc_data_year *data_year;
  118. void __iomem *base;
  119. int irq;
  120. };
  121. static irqreturn_t sunxi_rtc_alarmirq(int irq, void *id)
  122. {
  123. struct sunxi_rtc_dev *chip = (struct sunxi_rtc_dev *) id;
  124. u32 val;
  125. val = readl(chip->base + SUNXI_ALRM_IRQ_STA);
  126. if (val & SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND) {
  127. val |= SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND;
  128. writel(val, chip->base + SUNXI_ALRM_IRQ_STA);
  129. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  130. return IRQ_HANDLED;
  131. }
  132. return IRQ_NONE;
  133. }
  134. static void sunxi_rtc_setaie(unsigned int to, struct sunxi_rtc_dev *chip)
  135. {
  136. u32 alrm_val = 0;
  137. u32 alrm_irq_val = 0;
  138. if (to) {
  139. alrm_val = readl(chip->base + SUNXI_ALRM_EN);
  140. alrm_val |= SUNXI_ALRM_EN_CNT_EN;
  141. alrm_irq_val = readl(chip->base + SUNXI_ALRM_IRQ_EN);
  142. alrm_irq_val |= SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN;
  143. } else {
  144. writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND,
  145. chip->base + SUNXI_ALRM_IRQ_STA);
  146. }
  147. writel(alrm_val, chip->base + SUNXI_ALRM_EN);
  148. writel(alrm_irq_val, chip->base + SUNXI_ALRM_IRQ_EN);
  149. }
  150. static int sunxi_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  151. {
  152. struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
  153. struct rtc_time *alrm_tm = &wkalrm->time;
  154. u32 alrm;
  155. u32 alrm_en;
  156. u32 date;
  157. alrm = readl(chip->base + SUNXI_ALRM_DHMS);
  158. date = readl(chip->base + SUNXI_RTC_YMD);
  159. alrm_tm->tm_sec = SUNXI_ALRM_GET_SEC_VALUE(alrm);
  160. alrm_tm->tm_min = SUNXI_ALRM_GET_MIN_VALUE(alrm);
  161. alrm_tm->tm_hour = SUNXI_ALRM_GET_HOUR_VALUE(alrm);
  162. alrm_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
  163. alrm_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
  164. alrm_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
  165. chip->data_year->mask);
  166. alrm_tm->tm_mon -= 1;
  167. /*
  168. * switch from (data_year->min)-relative offset to
  169. * a (1900)-relative one
  170. */
  171. alrm_tm->tm_year += SUNXI_YEAR_OFF(chip->data_year);
  172. alrm_en = readl(chip->base + SUNXI_ALRM_IRQ_EN);
  173. if (alrm_en & SUNXI_ALRM_EN_CNT_EN)
  174. wkalrm->enabled = 1;
  175. return 0;
  176. }
  177. static int sunxi_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  178. {
  179. struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
  180. u32 date, time;
  181. /*
  182. * read again in case it changes
  183. */
  184. do {
  185. date = readl(chip->base + SUNXI_RTC_YMD);
  186. time = readl(chip->base + SUNXI_RTC_HMS);
  187. } while ((date != readl(chip->base + SUNXI_RTC_YMD)) ||
  188. (time != readl(chip->base + SUNXI_RTC_HMS)));
  189. rtc_tm->tm_sec = SUNXI_TIME_GET_SEC_VALUE(time);
  190. rtc_tm->tm_min = SUNXI_TIME_GET_MIN_VALUE(time);
  191. rtc_tm->tm_hour = SUNXI_TIME_GET_HOUR_VALUE(time);
  192. rtc_tm->tm_mday = SUNXI_DATE_GET_DAY_VALUE(date);
  193. rtc_tm->tm_mon = SUNXI_DATE_GET_MON_VALUE(date);
  194. rtc_tm->tm_year = SUNXI_DATE_GET_YEAR_VALUE(date,
  195. chip->data_year->mask);
  196. rtc_tm->tm_mon -= 1;
  197. /*
  198. * switch from (data_year->min)-relative offset to
  199. * a (1900)-relative one
  200. */
  201. rtc_tm->tm_year += SUNXI_YEAR_OFF(chip->data_year);
  202. return 0;
  203. }
  204. static int sunxi_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  205. {
  206. struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
  207. struct rtc_time *alrm_tm = &wkalrm->time;
  208. struct rtc_time tm_now;
  209. u32 alrm;
  210. time64_t diff;
  211. unsigned long time_gap;
  212. unsigned long time_gap_day;
  213. unsigned long time_gap_hour;
  214. unsigned long time_gap_min;
  215. int ret;
  216. ret = sunxi_rtc_gettime(dev, &tm_now);
  217. if (ret < 0) {
  218. dev_err(dev, "Error in getting time\n");
  219. return -EINVAL;
  220. }
  221. diff = rtc_tm_sub(alrm_tm, &tm_now);
  222. if (diff <= 0) {
  223. dev_err(dev, "Date to set in the past\n");
  224. return -EINVAL;
  225. }
  226. if (diff > 255 * SEC_IN_DAY) {
  227. dev_err(dev, "Day must be in the range 0 - 255\n");
  228. return -EINVAL;
  229. }
  230. time_gap = diff;
  231. time_gap_day = time_gap / SEC_IN_DAY;
  232. time_gap -= time_gap_day * SEC_IN_DAY;
  233. time_gap_hour = time_gap / SEC_IN_HOUR;
  234. time_gap -= time_gap_hour * SEC_IN_HOUR;
  235. time_gap_min = time_gap / SEC_IN_MIN;
  236. time_gap -= time_gap_min * SEC_IN_MIN;
  237. sunxi_rtc_setaie(0, chip);
  238. writel(0, chip->base + SUNXI_ALRM_DHMS);
  239. usleep_range(100, 300);
  240. alrm = SUNXI_ALRM_SET_SEC_VALUE(time_gap) |
  241. SUNXI_ALRM_SET_MIN_VALUE(time_gap_min) |
  242. SUNXI_ALRM_SET_HOUR_VALUE(time_gap_hour) |
  243. SUNXI_ALRM_SET_DAY_VALUE(time_gap_day);
  244. writel(alrm, chip->base + SUNXI_ALRM_DHMS);
  245. writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
  246. writel(SUNXI_ALRM_IRQ_EN_CNT_IRQ_EN, chip->base + SUNXI_ALRM_IRQ_EN);
  247. sunxi_rtc_setaie(wkalrm->enabled, chip);
  248. return 0;
  249. }
  250. static int sunxi_rtc_wait(struct sunxi_rtc_dev *chip, int offset,
  251. unsigned int mask, unsigned int ms_timeout)
  252. {
  253. const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
  254. u32 reg;
  255. do {
  256. reg = readl(chip->base + offset);
  257. reg &= mask;
  258. if (reg == mask)
  259. return 0;
  260. } while (time_before(jiffies, timeout));
  261. return -ETIMEDOUT;
  262. }
  263. static int sunxi_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
  264. {
  265. struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
  266. u32 date = 0;
  267. u32 time = 0;
  268. unsigned int year;
  269. /*
  270. * the input rtc_tm->tm_year is the offset relative to 1900. We use
  271. * the SUNXI_YEAR_OFF macro to rebase it with respect to the min year
  272. * allowed by the hardware
  273. */
  274. year = rtc_tm->tm_year + 1900;
  275. if (year < chip->data_year->min || year > chip->data_year->max) {
  276. dev_err(dev, "rtc only supports year in range %u - %u\n",
  277. chip->data_year->min, chip->data_year->max);
  278. return -EINVAL;
  279. }
  280. rtc_tm->tm_year -= SUNXI_YEAR_OFF(chip->data_year);
  281. rtc_tm->tm_mon += 1;
  282. date = SUNXI_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
  283. SUNXI_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
  284. SUNXI_DATE_SET_YEAR_VALUE(rtc_tm->tm_year,
  285. chip->data_year->mask);
  286. if (is_leap_year(year))
  287. date |= SUNXI_LEAP_SET_VALUE(1, chip->data_year->leap_shift);
  288. time = SUNXI_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
  289. SUNXI_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
  290. SUNXI_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
  291. writel(0, chip->base + SUNXI_RTC_HMS);
  292. writel(0, chip->base + SUNXI_RTC_YMD);
  293. writel(time, chip->base + SUNXI_RTC_HMS);
  294. /*
  295. * After writing the RTC HH-MM-SS register, the
  296. * SUNXI_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
  297. * be cleared until the real writing operation is finished
  298. */
  299. if (sunxi_rtc_wait(chip, SUNXI_LOSC_CTRL,
  300. SUNXI_LOSC_CTRL_RTC_HMS_ACC, 50)) {
  301. dev_err(dev, "Failed to set rtc time.\n");
  302. return -1;
  303. }
  304. writel(date, chip->base + SUNXI_RTC_YMD);
  305. /*
  306. * After writing the RTC YY-MM-DD register, the
  307. * SUNXI_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
  308. * be cleared until the real writing operation is finished
  309. */
  310. if (sunxi_rtc_wait(chip, SUNXI_LOSC_CTRL,
  311. SUNXI_LOSC_CTRL_RTC_YMD_ACC, 50)) {
  312. dev_err(dev, "Failed to set rtc time.\n");
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. static int sunxi_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  318. {
  319. struct sunxi_rtc_dev *chip = dev_get_drvdata(dev);
  320. if (!enabled)
  321. sunxi_rtc_setaie(enabled, chip);
  322. return 0;
  323. }
  324. static const struct rtc_class_ops sunxi_rtc_ops = {
  325. .read_time = sunxi_rtc_gettime,
  326. .set_time = sunxi_rtc_settime,
  327. .read_alarm = sunxi_rtc_getalarm,
  328. .set_alarm = sunxi_rtc_setalarm,
  329. .alarm_irq_enable = sunxi_rtc_alarm_irq_enable
  330. };
  331. static const struct of_device_id sunxi_rtc_dt_ids[] = {
  332. { .compatible = "allwinner,sun4i-a10-rtc", .data = &data_year_param[0] },
  333. { .compatible = "allwinner,sun7i-a20-rtc", .data = &data_year_param[1] },
  334. { /* sentinel */ },
  335. };
  336. MODULE_DEVICE_TABLE(of, sunxi_rtc_dt_ids);
  337. static int sunxi_rtc_probe(struct platform_device *pdev)
  338. {
  339. struct sunxi_rtc_dev *chip;
  340. int ret;
  341. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  342. if (!chip)
  343. return -ENOMEM;
  344. platform_set_drvdata(pdev, chip);
  345. chip->dev = &pdev->dev;
  346. chip->rtc = devm_rtc_allocate_device(&pdev->dev);
  347. if (IS_ERR(chip->rtc))
  348. return PTR_ERR(chip->rtc);
  349. chip->base = devm_platform_ioremap_resource(pdev, 0);
  350. if (IS_ERR(chip->base))
  351. return PTR_ERR(chip->base);
  352. chip->irq = platform_get_irq(pdev, 0);
  353. if (chip->irq < 0)
  354. return chip->irq;
  355. ret = devm_request_irq(&pdev->dev, chip->irq, sunxi_rtc_alarmirq,
  356. 0, dev_name(&pdev->dev), chip);
  357. if (ret) {
  358. dev_err(&pdev->dev, "Could not request IRQ\n");
  359. return ret;
  360. }
  361. chip->data_year = of_device_get_match_data(&pdev->dev);
  362. if (!chip->data_year) {
  363. dev_err(&pdev->dev, "Unable to setup RTC data\n");
  364. return -ENODEV;
  365. }
  366. /* clear the alarm count value */
  367. writel(0, chip->base + SUNXI_ALRM_DHMS);
  368. /* disable alarm, not generate irq pending */
  369. writel(0, chip->base + SUNXI_ALRM_EN);
  370. /* disable alarm week/cnt irq, unset to cpu */
  371. writel(0, chip->base + SUNXI_ALRM_IRQ_EN);
  372. /* clear alarm week/cnt irq pending */
  373. writel(SUNXI_ALRM_IRQ_STA_CNT_IRQ_PEND, chip->base +
  374. SUNXI_ALRM_IRQ_STA);
  375. chip->rtc->ops = &sunxi_rtc_ops;
  376. return devm_rtc_register_device(chip->rtc);
  377. }
  378. static struct platform_driver sunxi_rtc_driver = {
  379. .probe = sunxi_rtc_probe,
  380. .driver = {
  381. .name = "sunxi-rtc",
  382. .of_match_table = sunxi_rtc_dt_ids,
  383. },
  384. };
  385. module_platform_driver(sunxi_rtc_driver);
  386. MODULE_DESCRIPTION("sunxi RTC driver");
  387. MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
  388. MODULE_LICENSE("GPL");