rtc-stm32.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics 2017
  4. * Author: Amelie Delaunay <amelie.delaunay@st.com>
  5. */
  6. #include <linux/bcd.h>
  7. #include <linux/clk.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/ioport.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pm_wakeirq.h>
  14. #include <linux/regmap.h>
  15. #include <linux/rtc.h>
  16. #define DRIVER_NAME "stm32_rtc"
  17. /* STM32_RTC_TR bit fields */
  18. #define STM32_RTC_TR_SEC_SHIFT 0
  19. #define STM32_RTC_TR_SEC GENMASK(6, 0)
  20. #define STM32_RTC_TR_MIN_SHIFT 8
  21. #define STM32_RTC_TR_MIN GENMASK(14, 8)
  22. #define STM32_RTC_TR_HOUR_SHIFT 16
  23. #define STM32_RTC_TR_HOUR GENMASK(21, 16)
  24. /* STM32_RTC_DR bit fields */
  25. #define STM32_RTC_DR_DATE_SHIFT 0
  26. #define STM32_RTC_DR_DATE GENMASK(5, 0)
  27. #define STM32_RTC_DR_MONTH_SHIFT 8
  28. #define STM32_RTC_DR_MONTH GENMASK(12, 8)
  29. #define STM32_RTC_DR_WDAY_SHIFT 13
  30. #define STM32_RTC_DR_WDAY GENMASK(15, 13)
  31. #define STM32_RTC_DR_YEAR_SHIFT 16
  32. #define STM32_RTC_DR_YEAR GENMASK(23, 16)
  33. /* STM32_RTC_CR bit fields */
  34. #define STM32_RTC_CR_FMT BIT(6)
  35. #define STM32_RTC_CR_ALRAE BIT(8)
  36. #define STM32_RTC_CR_ALRAIE BIT(12)
  37. /* STM32_RTC_ISR/STM32_RTC_ICSR bit fields */
  38. #define STM32_RTC_ISR_ALRAWF BIT(0)
  39. #define STM32_RTC_ISR_INITS BIT(4)
  40. #define STM32_RTC_ISR_RSF BIT(5)
  41. #define STM32_RTC_ISR_INITF BIT(6)
  42. #define STM32_RTC_ISR_INIT BIT(7)
  43. #define STM32_RTC_ISR_ALRAF BIT(8)
  44. /* STM32_RTC_PRER bit fields */
  45. #define STM32_RTC_PRER_PRED_S_SHIFT 0
  46. #define STM32_RTC_PRER_PRED_S GENMASK(14, 0)
  47. #define STM32_RTC_PRER_PRED_A_SHIFT 16
  48. #define STM32_RTC_PRER_PRED_A GENMASK(22, 16)
  49. /* STM32_RTC_ALRMAR and STM32_RTC_ALRMBR bit fields */
  50. #define STM32_RTC_ALRMXR_SEC_SHIFT 0
  51. #define STM32_RTC_ALRMXR_SEC GENMASK(6, 0)
  52. #define STM32_RTC_ALRMXR_SEC_MASK BIT(7)
  53. #define STM32_RTC_ALRMXR_MIN_SHIFT 8
  54. #define STM32_RTC_ALRMXR_MIN GENMASK(14, 8)
  55. #define STM32_RTC_ALRMXR_MIN_MASK BIT(15)
  56. #define STM32_RTC_ALRMXR_HOUR_SHIFT 16
  57. #define STM32_RTC_ALRMXR_HOUR GENMASK(21, 16)
  58. #define STM32_RTC_ALRMXR_PM BIT(22)
  59. #define STM32_RTC_ALRMXR_HOUR_MASK BIT(23)
  60. #define STM32_RTC_ALRMXR_DATE_SHIFT 24
  61. #define STM32_RTC_ALRMXR_DATE GENMASK(29, 24)
  62. #define STM32_RTC_ALRMXR_WDSEL BIT(30)
  63. #define STM32_RTC_ALRMXR_WDAY_SHIFT 24
  64. #define STM32_RTC_ALRMXR_WDAY GENMASK(27, 24)
  65. #define STM32_RTC_ALRMXR_DATE_MASK BIT(31)
  66. /* STM32_RTC_SR/_SCR bit fields */
  67. #define STM32_RTC_SR_ALRA BIT(0)
  68. /* STM32_RTC_VERR bit fields */
  69. #define STM32_RTC_VERR_MINREV_SHIFT 0
  70. #define STM32_RTC_VERR_MINREV GENMASK(3, 0)
  71. #define STM32_RTC_VERR_MAJREV_SHIFT 4
  72. #define STM32_RTC_VERR_MAJREV GENMASK(7, 4)
  73. /* STM32_RTC_WPR key constants */
  74. #define RTC_WPR_1ST_KEY 0xCA
  75. #define RTC_WPR_2ND_KEY 0x53
  76. #define RTC_WPR_WRONG_KEY 0xFF
  77. /* Max STM32 RTC register offset is 0x3FC */
  78. #define UNDEF_REG 0xFFFF
  79. struct stm32_rtc;
  80. struct stm32_rtc_registers {
  81. u16 tr;
  82. u16 dr;
  83. u16 cr;
  84. u16 isr;
  85. u16 prer;
  86. u16 alrmar;
  87. u16 wpr;
  88. u16 sr;
  89. u16 scr;
  90. u16 verr;
  91. };
  92. struct stm32_rtc_events {
  93. u32 alra;
  94. };
  95. struct stm32_rtc_data {
  96. const struct stm32_rtc_registers regs;
  97. const struct stm32_rtc_events events;
  98. void (*clear_events)(struct stm32_rtc *rtc, unsigned int flags);
  99. bool has_pclk;
  100. bool need_dbp;
  101. bool has_wakeirq;
  102. };
  103. struct stm32_rtc {
  104. struct rtc_device *rtc_dev;
  105. void __iomem *base;
  106. struct regmap *dbp;
  107. unsigned int dbp_reg;
  108. unsigned int dbp_mask;
  109. struct clk *pclk;
  110. struct clk *rtc_ck;
  111. const struct stm32_rtc_data *data;
  112. int irq_alarm;
  113. int wakeirq_alarm;
  114. };
  115. static void stm32_rtc_wpr_unlock(struct stm32_rtc *rtc)
  116. {
  117. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  118. writel_relaxed(RTC_WPR_1ST_KEY, rtc->base + regs->wpr);
  119. writel_relaxed(RTC_WPR_2ND_KEY, rtc->base + regs->wpr);
  120. }
  121. static void stm32_rtc_wpr_lock(struct stm32_rtc *rtc)
  122. {
  123. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  124. writel_relaxed(RTC_WPR_WRONG_KEY, rtc->base + regs->wpr);
  125. }
  126. static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc)
  127. {
  128. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  129. unsigned int isr = readl_relaxed(rtc->base + regs->isr);
  130. if (!(isr & STM32_RTC_ISR_INITF)) {
  131. isr |= STM32_RTC_ISR_INIT;
  132. writel_relaxed(isr, rtc->base + regs->isr);
  133. /*
  134. * It takes around 2 rtc_ck clock cycles to enter in
  135. * initialization phase mode (and have INITF flag set). As
  136. * slowest rtc_ck frequency may be 32kHz and highest should be
  137. * 1MHz, we poll every 10 us with a timeout of 100ms.
  138. */
  139. return readl_relaxed_poll_timeout_atomic(
  140. rtc->base + regs->isr,
  141. isr, (isr & STM32_RTC_ISR_INITF),
  142. 10, 100000);
  143. }
  144. return 0;
  145. }
  146. static void stm32_rtc_exit_init_mode(struct stm32_rtc *rtc)
  147. {
  148. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  149. unsigned int isr = readl_relaxed(rtc->base + regs->isr);
  150. isr &= ~STM32_RTC_ISR_INIT;
  151. writel_relaxed(isr, rtc->base + regs->isr);
  152. }
  153. static int stm32_rtc_wait_sync(struct stm32_rtc *rtc)
  154. {
  155. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  156. unsigned int isr = readl_relaxed(rtc->base + regs->isr);
  157. isr &= ~STM32_RTC_ISR_RSF;
  158. writel_relaxed(isr, rtc->base + regs->isr);
  159. /*
  160. * Wait for RSF to be set to ensure the calendar registers are
  161. * synchronised, it takes around 2 rtc_ck clock cycles
  162. */
  163. return readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr,
  164. isr,
  165. (isr & STM32_RTC_ISR_RSF),
  166. 10, 100000);
  167. }
  168. static void stm32_rtc_clear_event_flags(struct stm32_rtc *rtc,
  169. unsigned int flags)
  170. {
  171. rtc->data->clear_events(rtc, flags);
  172. }
  173. static irqreturn_t stm32_rtc_alarm_irq(int irq, void *dev_id)
  174. {
  175. struct stm32_rtc *rtc = (struct stm32_rtc *)dev_id;
  176. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  177. const struct stm32_rtc_events *evts = &rtc->data->events;
  178. unsigned int status, cr;
  179. mutex_lock(&rtc->rtc_dev->ops_lock);
  180. status = readl_relaxed(rtc->base + regs->sr);
  181. cr = readl_relaxed(rtc->base + regs->cr);
  182. if ((status & evts->alra) &&
  183. (cr & STM32_RTC_CR_ALRAIE)) {
  184. /* Alarm A flag - Alarm interrupt */
  185. dev_dbg(&rtc->rtc_dev->dev, "Alarm occurred\n");
  186. /* Pass event to the kernel */
  187. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  188. /* Clear event flags, otherwise new events won't be received */
  189. stm32_rtc_clear_event_flags(rtc, evts->alra);
  190. }
  191. mutex_unlock(&rtc->rtc_dev->ops_lock);
  192. return IRQ_HANDLED;
  193. }
  194. /* Convert rtc_time structure from bin to bcd format */
  195. static void tm2bcd(struct rtc_time *tm)
  196. {
  197. tm->tm_sec = bin2bcd(tm->tm_sec);
  198. tm->tm_min = bin2bcd(tm->tm_min);
  199. tm->tm_hour = bin2bcd(tm->tm_hour);
  200. tm->tm_mday = bin2bcd(tm->tm_mday);
  201. tm->tm_mon = bin2bcd(tm->tm_mon + 1);
  202. tm->tm_year = bin2bcd(tm->tm_year - 100);
  203. /*
  204. * Number of days since Sunday
  205. * - on kernel side, 0=Sunday...6=Saturday
  206. * - on rtc side, 0=invalid,1=Monday...7=Sunday
  207. */
  208. tm->tm_wday = (!tm->tm_wday) ? 7 : tm->tm_wday;
  209. }
  210. /* Convert rtc_time structure from bcd to bin format */
  211. static void bcd2tm(struct rtc_time *tm)
  212. {
  213. tm->tm_sec = bcd2bin(tm->tm_sec);
  214. tm->tm_min = bcd2bin(tm->tm_min);
  215. tm->tm_hour = bcd2bin(tm->tm_hour);
  216. tm->tm_mday = bcd2bin(tm->tm_mday);
  217. tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
  218. tm->tm_year = bcd2bin(tm->tm_year) + 100;
  219. /*
  220. * Number of days since Sunday
  221. * - on kernel side, 0=Sunday...6=Saturday
  222. * - on rtc side, 0=invalid,1=Monday...7=Sunday
  223. */
  224. tm->tm_wday %= 7;
  225. }
  226. static int stm32_rtc_read_time(struct device *dev, struct rtc_time *tm)
  227. {
  228. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  229. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  230. unsigned int tr, dr;
  231. /* Time and Date in BCD format */
  232. tr = readl_relaxed(rtc->base + regs->tr);
  233. dr = readl_relaxed(rtc->base + regs->dr);
  234. tm->tm_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
  235. tm->tm_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
  236. tm->tm_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
  237. tm->tm_mday = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
  238. tm->tm_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
  239. tm->tm_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
  240. tm->tm_wday = (dr & STM32_RTC_DR_WDAY) >> STM32_RTC_DR_WDAY_SHIFT;
  241. /* We don't report tm_yday and tm_isdst */
  242. bcd2tm(tm);
  243. return 0;
  244. }
  245. static int stm32_rtc_set_time(struct device *dev, struct rtc_time *tm)
  246. {
  247. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  248. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  249. unsigned int tr, dr;
  250. int ret = 0;
  251. tm2bcd(tm);
  252. /* Time in BCD format */
  253. tr = ((tm->tm_sec << STM32_RTC_TR_SEC_SHIFT) & STM32_RTC_TR_SEC) |
  254. ((tm->tm_min << STM32_RTC_TR_MIN_SHIFT) & STM32_RTC_TR_MIN) |
  255. ((tm->tm_hour << STM32_RTC_TR_HOUR_SHIFT) & STM32_RTC_TR_HOUR);
  256. /* Date in BCD format */
  257. dr = ((tm->tm_mday << STM32_RTC_DR_DATE_SHIFT) & STM32_RTC_DR_DATE) |
  258. ((tm->tm_mon << STM32_RTC_DR_MONTH_SHIFT) & STM32_RTC_DR_MONTH) |
  259. ((tm->tm_year << STM32_RTC_DR_YEAR_SHIFT) & STM32_RTC_DR_YEAR) |
  260. ((tm->tm_wday << STM32_RTC_DR_WDAY_SHIFT) & STM32_RTC_DR_WDAY);
  261. stm32_rtc_wpr_unlock(rtc);
  262. ret = stm32_rtc_enter_init_mode(rtc);
  263. if (ret) {
  264. dev_err(dev, "Can't enter in init mode. Set time aborted.\n");
  265. goto end;
  266. }
  267. writel_relaxed(tr, rtc->base + regs->tr);
  268. writel_relaxed(dr, rtc->base + regs->dr);
  269. stm32_rtc_exit_init_mode(rtc);
  270. ret = stm32_rtc_wait_sync(rtc);
  271. end:
  272. stm32_rtc_wpr_lock(rtc);
  273. return ret;
  274. }
  275. static int stm32_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  276. {
  277. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  278. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  279. const struct stm32_rtc_events *evts = &rtc->data->events;
  280. struct rtc_time *tm = &alrm->time;
  281. unsigned int alrmar, cr, status;
  282. alrmar = readl_relaxed(rtc->base + regs->alrmar);
  283. cr = readl_relaxed(rtc->base + regs->cr);
  284. status = readl_relaxed(rtc->base + regs->sr);
  285. if (alrmar & STM32_RTC_ALRMXR_DATE_MASK) {
  286. /*
  287. * Date/day doesn't matter in Alarm comparison so alarm
  288. * triggers every day
  289. */
  290. tm->tm_mday = -1;
  291. tm->tm_wday = -1;
  292. } else {
  293. if (alrmar & STM32_RTC_ALRMXR_WDSEL) {
  294. /* Alarm is set to a day of week */
  295. tm->tm_mday = -1;
  296. tm->tm_wday = (alrmar & STM32_RTC_ALRMXR_WDAY) >>
  297. STM32_RTC_ALRMXR_WDAY_SHIFT;
  298. tm->tm_wday %= 7;
  299. } else {
  300. /* Alarm is set to a day of month */
  301. tm->tm_wday = -1;
  302. tm->tm_mday = (alrmar & STM32_RTC_ALRMXR_DATE) >>
  303. STM32_RTC_ALRMXR_DATE_SHIFT;
  304. }
  305. }
  306. if (alrmar & STM32_RTC_ALRMXR_HOUR_MASK) {
  307. /* Hours don't matter in Alarm comparison */
  308. tm->tm_hour = -1;
  309. } else {
  310. tm->tm_hour = (alrmar & STM32_RTC_ALRMXR_HOUR) >>
  311. STM32_RTC_ALRMXR_HOUR_SHIFT;
  312. if (alrmar & STM32_RTC_ALRMXR_PM)
  313. tm->tm_hour += 12;
  314. }
  315. if (alrmar & STM32_RTC_ALRMXR_MIN_MASK) {
  316. /* Minutes don't matter in Alarm comparison */
  317. tm->tm_min = -1;
  318. } else {
  319. tm->tm_min = (alrmar & STM32_RTC_ALRMXR_MIN) >>
  320. STM32_RTC_ALRMXR_MIN_SHIFT;
  321. }
  322. if (alrmar & STM32_RTC_ALRMXR_SEC_MASK) {
  323. /* Seconds don't matter in Alarm comparison */
  324. tm->tm_sec = -1;
  325. } else {
  326. tm->tm_sec = (alrmar & STM32_RTC_ALRMXR_SEC) >>
  327. STM32_RTC_ALRMXR_SEC_SHIFT;
  328. }
  329. bcd2tm(tm);
  330. alrm->enabled = (cr & STM32_RTC_CR_ALRAE) ? 1 : 0;
  331. alrm->pending = (status & evts->alra) ? 1 : 0;
  332. return 0;
  333. }
  334. static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  335. {
  336. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  337. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  338. const struct stm32_rtc_events *evts = &rtc->data->events;
  339. unsigned int cr;
  340. cr = readl_relaxed(rtc->base + regs->cr);
  341. stm32_rtc_wpr_unlock(rtc);
  342. /* We expose Alarm A to the kernel */
  343. if (enabled)
  344. cr |= (STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
  345. else
  346. cr &= ~(STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
  347. writel_relaxed(cr, rtc->base + regs->cr);
  348. /* Clear event flags, otherwise new events won't be received */
  349. stm32_rtc_clear_event_flags(rtc, evts->alra);
  350. stm32_rtc_wpr_lock(rtc);
  351. return 0;
  352. }
  353. static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm)
  354. {
  355. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  356. int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec;
  357. unsigned int dr = readl_relaxed(rtc->base + regs->dr);
  358. unsigned int tr = readl_relaxed(rtc->base + regs->tr);
  359. cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
  360. cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
  361. cur_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
  362. cur_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
  363. cur_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
  364. cur_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
  365. /*
  366. * Assuming current date is M-D-Y H:M:S.
  367. * RTC alarm can't be set on a specific month and year.
  368. * So the valid alarm range is:
  369. * M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S
  370. * with a specific case for December...
  371. */
  372. if ((((tm->tm_year > cur_year) &&
  373. (tm->tm_mon == 0x1) && (cur_mon == 0x12)) ||
  374. ((tm->tm_year == cur_year) &&
  375. (tm->tm_mon <= cur_mon + 1))) &&
  376. ((tm->tm_mday > cur_day) ||
  377. ((tm->tm_mday == cur_day) &&
  378. ((tm->tm_hour > cur_hour) ||
  379. ((tm->tm_hour == cur_hour) && (tm->tm_min > cur_min)) ||
  380. ((tm->tm_hour == cur_hour) && (tm->tm_min == cur_min) &&
  381. (tm->tm_sec >= cur_sec))))))
  382. return 0;
  383. return -EINVAL;
  384. }
  385. static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  386. {
  387. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  388. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  389. struct rtc_time *tm = &alrm->time;
  390. unsigned int cr, isr, alrmar;
  391. int ret = 0;
  392. tm2bcd(tm);
  393. /*
  394. * RTC alarm can't be set on a specific date, unless this date is
  395. * up to the same day of month next month.
  396. */
  397. if (stm32_rtc_valid_alrm(rtc, tm) < 0) {
  398. dev_err(dev, "Alarm can be set only on upcoming month.\n");
  399. return -EINVAL;
  400. }
  401. alrmar = 0;
  402. /* tm_year and tm_mon are not used because not supported by RTC */
  403. alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &
  404. STM32_RTC_ALRMXR_DATE;
  405. /* 24-hour format */
  406. alrmar &= ~STM32_RTC_ALRMXR_PM;
  407. alrmar |= (tm->tm_hour << STM32_RTC_ALRMXR_HOUR_SHIFT) &
  408. STM32_RTC_ALRMXR_HOUR;
  409. alrmar |= (tm->tm_min << STM32_RTC_ALRMXR_MIN_SHIFT) &
  410. STM32_RTC_ALRMXR_MIN;
  411. alrmar |= (tm->tm_sec << STM32_RTC_ALRMXR_SEC_SHIFT) &
  412. STM32_RTC_ALRMXR_SEC;
  413. stm32_rtc_wpr_unlock(rtc);
  414. /* Disable Alarm */
  415. cr = readl_relaxed(rtc->base + regs->cr);
  416. cr &= ~STM32_RTC_CR_ALRAE;
  417. writel_relaxed(cr, rtc->base + regs->cr);
  418. /*
  419. * Poll Alarm write flag to be sure that Alarm update is allowed: it
  420. * takes around 2 rtc_ck clock cycles
  421. */
  422. ret = readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr,
  423. isr,
  424. (isr & STM32_RTC_ISR_ALRAWF),
  425. 10, 100000);
  426. if (ret) {
  427. dev_err(dev, "Alarm update not allowed\n");
  428. goto end;
  429. }
  430. /* Write to Alarm register */
  431. writel_relaxed(alrmar, rtc->base + regs->alrmar);
  432. if (alrm->enabled)
  433. stm32_rtc_alarm_irq_enable(dev, 1);
  434. else
  435. stm32_rtc_alarm_irq_enable(dev, 0);
  436. end:
  437. stm32_rtc_wpr_lock(rtc);
  438. return ret;
  439. }
  440. static const struct rtc_class_ops stm32_rtc_ops = {
  441. .read_time = stm32_rtc_read_time,
  442. .set_time = stm32_rtc_set_time,
  443. .read_alarm = stm32_rtc_read_alarm,
  444. .set_alarm = stm32_rtc_set_alarm,
  445. .alarm_irq_enable = stm32_rtc_alarm_irq_enable,
  446. };
  447. static void stm32_rtc_clear_events(struct stm32_rtc *rtc,
  448. unsigned int flags)
  449. {
  450. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  451. /* Flags are cleared by writing 0 in RTC_ISR */
  452. writel_relaxed(readl_relaxed(rtc->base + regs->isr) & ~flags,
  453. rtc->base + regs->isr);
  454. }
  455. static const struct stm32_rtc_data stm32_rtc_data = {
  456. .has_pclk = false,
  457. .need_dbp = true,
  458. .has_wakeirq = false,
  459. .regs = {
  460. .tr = 0x00,
  461. .dr = 0x04,
  462. .cr = 0x08,
  463. .isr = 0x0C,
  464. .prer = 0x10,
  465. .alrmar = 0x1C,
  466. .wpr = 0x24,
  467. .sr = 0x0C, /* set to ISR offset to ease alarm management */
  468. .scr = UNDEF_REG,
  469. .verr = UNDEF_REG,
  470. },
  471. .events = {
  472. .alra = STM32_RTC_ISR_ALRAF,
  473. },
  474. .clear_events = stm32_rtc_clear_events,
  475. };
  476. static const struct stm32_rtc_data stm32h7_rtc_data = {
  477. .has_pclk = true,
  478. .need_dbp = true,
  479. .has_wakeirq = false,
  480. .regs = {
  481. .tr = 0x00,
  482. .dr = 0x04,
  483. .cr = 0x08,
  484. .isr = 0x0C,
  485. .prer = 0x10,
  486. .alrmar = 0x1C,
  487. .wpr = 0x24,
  488. .sr = 0x0C, /* set to ISR offset to ease alarm management */
  489. .scr = UNDEF_REG,
  490. .verr = UNDEF_REG,
  491. },
  492. .events = {
  493. .alra = STM32_RTC_ISR_ALRAF,
  494. },
  495. .clear_events = stm32_rtc_clear_events,
  496. };
  497. static void stm32mp1_rtc_clear_events(struct stm32_rtc *rtc,
  498. unsigned int flags)
  499. {
  500. struct stm32_rtc_registers regs = rtc->data->regs;
  501. /* Flags are cleared by writing 1 in RTC_SCR */
  502. writel_relaxed(flags, rtc->base + regs.scr);
  503. }
  504. static const struct stm32_rtc_data stm32mp1_data = {
  505. .has_pclk = true,
  506. .need_dbp = false,
  507. .has_wakeirq = true,
  508. .regs = {
  509. .tr = 0x00,
  510. .dr = 0x04,
  511. .cr = 0x18,
  512. .isr = 0x0C, /* named RTC_ICSR on stm32mp1 */
  513. .prer = 0x10,
  514. .alrmar = 0x40,
  515. .wpr = 0x24,
  516. .sr = 0x50,
  517. .scr = 0x5C,
  518. .verr = 0x3F4,
  519. },
  520. .events = {
  521. .alra = STM32_RTC_SR_ALRA,
  522. },
  523. .clear_events = stm32mp1_rtc_clear_events,
  524. };
  525. static const struct of_device_id stm32_rtc_of_match[] = {
  526. { .compatible = "st,stm32-rtc", .data = &stm32_rtc_data },
  527. { .compatible = "st,stm32h7-rtc", .data = &stm32h7_rtc_data },
  528. { .compatible = "st,stm32mp1-rtc", .data = &stm32mp1_data },
  529. {}
  530. };
  531. MODULE_DEVICE_TABLE(of, stm32_rtc_of_match);
  532. static int stm32_rtc_init(struct platform_device *pdev,
  533. struct stm32_rtc *rtc)
  534. {
  535. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  536. unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr;
  537. unsigned int rate;
  538. int ret = 0;
  539. rate = clk_get_rate(rtc->rtc_ck);
  540. /* Find prediv_a and prediv_s to obtain the 1Hz calendar clock */
  541. pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT;
  542. pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT;
  543. for (pred_a = pred_a_max; pred_a + 1 > 0; pred_a--) {
  544. pred_s = (rate / (pred_a + 1)) - 1;
  545. if (((pred_s + 1) * (pred_a + 1)) == rate)
  546. break;
  547. }
  548. /*
  549. * Can't find a 1Hz, so give priority to RTC power consumption
  550. * by choosing the higher possible value for prediv_a
  551. */
  552. if ((pred_s > pred_s_max) || (pred_a > pred_a_max)) {
  553. pred_a = pred_a_max;
  554. pred_s = (rate / (pred_a + 1)) - 1;
  555. dev_warn(&pdev->dev, "rtc_ck is %s\n",
  556. (rate < ((pred_a + 1) * (pred_s + 1))) ?
  557. "fast" : "slow");
  558. }
  559. stm32_rtc_wpr_unlock(rtc);
  560. ret = stm32_rtc_enter_init_mode(rtc);
  561. if (ret) {
  562. dev_err(&pdev->dev,
  563. "Can't enter in init mode. Prescaler config failed.\n");
  564. goto end;
  565. }
  566. prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & STM32_RTC_PRER_PRED_S;
  567. writel_relaxed(prer, rtc->base + regs->prer);
  568. prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & STM32_RTC_PRER_PRED_A;
  569. writel_relaxed(prer, rtc->base + regs->prer);
  570. /* Force 24h time format */
  571. cr = readl_relaxed(rtc->base + regs->cr);
  572. cr &= ~STM32_RTC_CR_FMT;
  573. writel_relaxed(cr, rtc->base + regs->cr);
  574. stm32_rtc_exit_init_mode(rtc);
  575. ret = stm32_rtc_wait_sync(rtc);
  576. end:
  577. stm32_rtc_wpr_lock(rtc);
  578. return ret;
  579. }
  580. static int stm32_rtc_probe(struct platform_device *pdev)
  581. {
  582. struct stm32_rtc *rtc;
  583. const struct stm32_rtc_registers *regs;
  584. struct resource *res;
  585. int ret;
  586. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  587. if (!rtc)
  588. return -ENOMEM;
  589. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  590. rtc->base = devm_ioremap_resource(&pdev->dev, res);
  591. if (IS_ERR(rtc->base))
  592. return PTR_ERR(rtc->base);
  593. rtc->data = (struct stm32_rtc_data *)
  594. of_device_get_match_data(&pdev->dev);
  595. regs = &rtc->data->regs;
  596. if (rtc->data->need_dbp) {
  597. rtc->dbp = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  598. "st,syscfg");
  599. if (IS_ERR(rtc->dbp)) {
  600. dev_err(&pdev->dev, "no st,syscfg\n");
  601. return PTR_ERR(rtc->dbp);
  602. }
  603. ret = of_property_read_u32_index(pdev->dev.of_node, "st,syscfg",
  604. 1, &rtc->dbp_reg);
  605. if (ret) {
  606. dev_err(&pdev->dev, "can't read DBP register offset\n");
  607. return ret;
  608. }
  609. ret = of_property_read_u32_index(pdev->dev.of_node, "st,syscfg",
  610. 2, &rtc->dbp_mask);
  611. if (ret) {
  612. dev_err(&pdev->dev, "can't read DBP register mask\n");
  613. return ret;
  614. }
  615. }
  616. if (!rtc->data->has_pclk) {
  617. rtc->pclk = NULL;
  618. rtc->rtc_ck = devm_clk_get(&pdev->dev, NULL);
  619. } else {
  620. rtc->pclk = devm_clk_get(&pdev->dev, "pclk");
  621. if (IS_ERR(rtc->pclk)) {
  622. dev_err(&pdev->dev, "no pclk clock");
  623. return PTR_ERR(rtc->pclk);
  624. }
  625. rtc->rtc_ck = devm_clk_get(&pdev->dev, "rtc_ck");
  626. }
  627. if (IS_ERR(rtc->rtc_ck)) {
  628. dev_err(&pdev->dev, "no rtc_ck clock");
  629. return PTR_ERR(rtc->rtc_ck);
  630. }
  631. if (rtc->data->has_pclk) {
  632. ret = clk_prepare_enable(rtc->pclk);
  633. if (ret)
  634. return ret;
  635. }
  636. ret = clk_prepare_enable(rtc->rtc_ck);
  637. if (ret)
  638. goto err;
  639. if (rtc->data->need_dbp)
  640. regmap_update_bits(rtc->dbp, rtc->dbp_reg,
  641. rtc->dbp_mask, rtc->dbp_mask);
  642. /*
  643. * After a system reset, RTC_ISR.INITS flag can be read to check if
  644. * the calendar has been initialized or not. INITS flag is reset by a
  645. * power-on reset (no vbat, no power-supply). It is not reset if
  646. * rtc_ck parent clock has changed (so RTC prescalers need to be
  647. * changed). That's why we cannot rely on this flag to know if RTC
  648. * init has to be done.
  649. */
  650. ret = stm32_rtc_init(pdev, rtc);
  651. if (ret)
  652. goto err;
  653. rtc->irq_alarm = platform_get_irq(pdev, 0);
  654. if (rtc->irq_alarm <= 0) {
  655. dev_err(&pdev->dev, "no alarm irq\n");
  656. ret = rtc->irq_alarm;
  657. goto err;
  658. }
  659. ret = device_init_wakeup(&pdev->dev, true);
  660. if (rtc->data->has_wakeirq) {
  661. rtc->wakeirq_alarm = platform_get_irq(pdev, 1);
  662. if (rtc->wakeirq_alarm > 0) {
  663. ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  664. rtc->wakeirq_alarm);
  665. } else {
  666. ret = rtc->wakeirq_alarm;
  667. if (rtc->wakeirq_alarm == -EPROBE_DEFER)
  668. goto err;
  669. }
  670. }
  671. if (ret)
  672. dev_warn(&pdev->dev, "alarm can't wake up the system: %d", ret);
  673. platform_set_drvdata(pdev, rtc);
  674. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
  675. &stm32_rtc_ops, THIS_MODULE);
  676. if (IS_ERR(rtc->rtc_dev)) {
  677. ret = PTR_ERR(rtc->rtc_dev);
  678. dev_err(&pdev->dev, "rtc device registration failed, err=%d\n",
  679. ret);
  680. goto err;
  681. }
  682. /* Handle RTC alarm interrupts */
  683. ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_alarm, NULL,
  684. stm32_rtc_alarm_irq, IRQF_ONESHOT,
  685. pdev->name, rtc);
  686. if (ret) {
  687. dev_err(&pdev->dev, "IRQ%d (alarm interrupt) already claimed\n",
  688. rtc->irq_alarm);
  689. goto err;
  690. }
  691. /*
  692. * If INITS flag is reset (calendar year field set to 0x00), calendar
  693. * must be initialized
  694. */
  695. if (!(readl_relaxed(rtc->base + regs->isr) & STM32_RTC_ISR_INITS))
  696. dev_warn(&pdev->dev, "Date/Time must be initialized\n");
  697. if (regs->verr != UNDEF_REG) {
  698. u32 ver = readl_relaxed(rtc->base + regs->verr);
  699. dev_info(&pdev->dev, "registered rev:%d.%d\n",
  700. (ver >> STM32_RTC_VERR_MAJREV_SHIFT) & 0xF,
  701. (ver >> STM32_RTC_VERR_MINREV_SHIFT) & 0xF);
  702. }
  703. return 0;
  704. err:
  705. if (rtc->data->has_pclk)
  706. clk_disable_unprepare(rtc->pclk);
  707. clk_disable_unprepare(rtc->rtc_ck);
  708. if (rtc->data->need_dbp)
  709. regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0);
  710. dev_pm_clear_wake_irq(&pdev->dev);
  711. device_init_wakeup(&pdev->dev, false);
  712. return ret;
  713. }
  714. static int stm32_rtc_remove(struct platform_device *pdev)
  715. {
  716. struct stm32_rtc *rtc = platform_get_drvdata(pdev);
  717. const struct stm32_rtc_registers *regs = &rtc->data->regs;
  718. unsigned int cr;
  719. /* Disable interrupts */
  720. stm32_rtc_wpr_unlock(rtc);
  721. cr = readl_relaxed(rtc->base + regs->cr);
  722. cr &= ~STM32_RTC_CR_ALRAIE;
  723. writel_relaxed(cr, rtc->base + regs->cr);
  724. stm32_rtc_wpr_lock(rtc);
  725. clk_disable_unprepare(rtc->rtc_ck);
  726. if (rtc->data->has_pclk)
  727. clk_disable_unprepare(rtc->pclk);
  728. /* Enable backup domain write protection if needed */
  729. if (rtc->data->need_dbp)
  730. regmap_update_bits(rtc->dbp, rtc->dbp_reg, rtc->dbp_mask, 0);
  731. dev_pm_clear_wake_irq(&pdev->dev);
  732. device_init_wakeup(&pdev->dev, false);
  733. return 0;
  734. }
  735. #ifdef CONFIG_PM_SLEEP
  736. static int stm32_rtc_suspend(struct device *dev)
  737. {
  738. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  739. if (rtc->data->has_pclk)
  740. clk_disable_unprepare(rtc->pclk);
  741. if (device_may_wakeup(dev))
  742. return enable_irq_wake(rtc->irq_alarm);
  743. return 0;
  744. }
  745. static int stm32_rtc_resume(struct device *dev)
  746. {
  747. struct stm32_rtc *rtc = dev_get_drvdata(dev);
  748. int ret = 0;
  749. if (rtc->data->has_pclk) {
  750. ret = clk_prepare_enable(rtc->pclk);
  751. if (ret)
  752. return ret;
  753. }
  754. ret = stm32_rtc_wait_sync(rtc);
  755. if (ret < 0)
  756. return ret;
  757. if (device_may_wakeup(dev))
  758. return disable_irq_wake(rtc->irq_alarm);
  759. return ret;
  760. }
  761. #endif
  762. static SIMPLE_DEV_PM_OPS(stm32_rtc_pm_ops,
  763. stm32_rtc_suspend, stm32_rtc_resume);
  764. static struct platform_driver stm32_rtc_driver = {
  765. .probe = stm32_rtc_probe,
  766. .remove = stm32_rtc_remove,
  767. .driver = {
  768. .name = DRIVER_NAME,
  769. .pm = &stm32_rtc_pm_ops,
  770. .of_match_table = stm32_rtc_of_match,
  771. },
  772. };
  773. module_platform_driver(stm32_rtc_driver);
  774. MODULE_ALIAS("platform:" DRIVER_NAME);
  775. MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  776. MODULE_DESCRIPTION("STMicroelectronics STM32 Real Time Clock driver");
  777. MODULE_LICENSE("GPL v2");