rtc-sun6i.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * An RTC driver for Allwinner A31/A23
  4. *
  5. * Copyright (c) 2014, Chen-Yu Tsai <wens@csie.org>
  6. *
  7. * based on rtc-sunxi.c
  8. *
  9. * An RTC driver for Allwinner A10/A20
  10. *
  11. * Copyright (c) 2013, Carlo Caione <carlo.caione@gmail.com>
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/clk/sunxi-ng.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/rtc.h>
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. /* Control register */
  31. #define SUN6I_LOSC_CTRL 0x0000
  32. #define SUN6I_LOSC_CTRL_KEY (0x16aa << 16)
  33. #define SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS BIT(15)
  34. #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9)
  35. #define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8)
  36. #define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7)
  37. #define SUN6I_LOSC_CTRL_EXT_LOSC_EN BIT(4)
  38. #define SUN6I_LOSC_CTRL_EXT_OSC BIT(0)
  39. #define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7)
  40. #define SUN6I_LOSC_CLK_PRESCAL 0x0008
  41. /* RTC */
  42. #define SUN6I_RTC_YMD 0x0010
  43. #define SUN6I_RTC_HMS 0x0014
  44. /* Alarm 0 (counter) */
  45. #define SUN6I_ALRM_COUNTER 0x0020
  46. /* This holds the remaining alarm seconds on older SoCs (current value) */
  47. #define SUN6I_ALRM_COUNTER_HMS 0x0024
  48. #define SUN6I_ALRM_EN 0x0028
  49. #define SUN6I_ALRM_EN_CNT_EN BIT(0)
  50. #define SUN6I_ALRM_IRQ_EN 0x002c
  51. #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
  52. #define SUN6I_ALRM_IRQ_STA 0x0030
  53. #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
  54. /* Alarm 1 (wall clock) */
  55. #define SUN6I_ALRM1_EN 0x0044
  56. #define SUN6I_ALRM1_IRQ_EN 0x0048
  57. #define SUN6I_ALRM1_IRQ_STA 0x004c
  58. #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0)
  59. /* Alarm config */
  60. #define SUN6I_ALARM_CONFIG 0x0050
  61. #define SUN6I_ALARM_CONFIG_WAKEUP BIT(0)
  62. #define SUN6I_LOSC_OUT_GATING 0x0060
  63. #define SUN6I_LOSC_OUT_GATING_EN_OFFSET 0
  64. /* General-purpose data */
  65. #define SUN6I_GP_DATA 0x0100
  66. #define SUN6I_GP_DATA_SIZE 0x20
  67. /*
  68. * Get date values
  69. */
  70. #define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f)
  71. #define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8)
  72. #define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16)
  73. #define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22)
  74. /*
  75. * Get time values
  76. */
  77. #define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f)
  78. #define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8)
  79. #define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16)
  80. /*
  81. * Set date values
  82. */
  83. #define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f)
  84. #define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00)
  85. #define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000)
  86. #define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000)
  87. /*
  88. * Set time values
  89. */
  90. #define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f)
  91. #define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00)
  92. #define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000)
  93. /*
  94. * The year parameter passed to the driver is usually an offset relative to
  95. * the year 1900. This macro is used to convert this offset to another one
  96. * relative to the minimum year allowed by the hardware.
  97. *
  98. * The year range is 1970 - 2033. This range is selected to match Allwinner's
  99. * driver, even though it is somewhat limited.
  100. */
  101. #define SUN6I_YEAR_MIN 1970
  102. #define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900)
  103. #define SECS_PER_DAY (24 * 3600ULL)
  104. /*
  105. * There are other differences between models, including:
  106. *
  107. * - number of GPIO pins that can be configured to hold a certain level
  108. * - crypto-key related registers (H5, H6)
  109. * - boot process related (super standby, secondary processor entry address)
  110. * registers (R40, H6)
  111. * - SYS power domain controls (R40)
  112. * - DCXO controls (H6)
  113. * - RC oscillator calibration (H6)
  114. *
  115. * These functions are not covered by this driver.
  116. */
  117. struct sun6i_rtc_clk_data {
  118. unsigned long rc_osc_rate;
  119. unsigned int fixed_prescaler : 16;
  120. unsigned int has_prescaler : 1;
  121. unsigned int has_out_clk : 1;
  122. unsigned int has_losc_en : 1;
  123. unsigned int has_auto_swt : 1;
  124. };
  125. #define RTC_LINEAR_DAY BIT(0)
  126. struct sun6i_rtc_dev {
  127. struct rtc_device *rtc;
  128. const struct sun6i_rtc_clk_data *data;
  129. void __iomem *base;
  130. int irq;
  131. time64_t alarm;
  132. unsigned long flags;
  133. struct clk_hw hw;
  134. struct clk_hw *int_osc;
  135. struct clk *losc;
  136. struct clk *ext_losc;
  137. spinlock_t lock;
  138. };
  139. static struct sun6i_rtc_dev *sun6i_rtc;
  140. static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
  141. unsigned long parent_rate)
  142. {
  143. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  144. u32 val = 0;
  145. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  146. if (val & SUN6I_LOSC_CTRL_EXT_OSC)
  147. return parent_rate;
  148. if (rtc->data->fixed_prescaler)
  149. parent_rate /= rtc->data->fixed_prescaler;
  150. if (rtc->data->has_prescaler) {
  151. val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
  152. val &= GENMASK(4, 0);
  153. }
  154. return parent_rate / (val + 1);
  155. }
  156. static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
  157. {
  158. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  159. return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
  160. }
  161. static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
  162. {
  163. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  164. unsigned long flags;
  165. u32 val;
  166. if (index > 1)
  167. return -EINVAL;
  168. spin_lock_irqsave(&rtc->lock, flags);
  169. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  170. val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
  171. val |= SUN6I_LOSC_CTRL_KEY;
  172. val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
  173. if (rtc->data->has_losc_en) {
  174. val &= ~SUN6I_LOSC_CTRL_EXT_LOSC_EN;
  175. val |= index ? SUN6I_LOSC_CTRL_EXT_LOSC_EN : 0;
  176. }
  177. writel(val, rtc->base + SUN6I_LOSC_CTRL);
  178. spin_unlock_irqrestore(&rtc->lock, flags);
  179. return 0;
  180. }
  181. static const struct clk_ops sun6i_rtc_osc_ops = {
  182. .recalc_rate = sun6i_rtc_osc_recalc_rate,
  183. .determine_rate = clk_hw_determine_rate_no_reparent,
  184. .get_parent = sun6i_rtc_osc_get_parent,
  185. .set_parent = sun6i_rtc_osc_set_parent,
  186. };
  187. static void __init sun6i_rtc_clk_init(struct device_node *node,
  188. const struct sun6i_rtc_clk_data *data)
  189. {
  190. struct clk_hw_onecell_data *clk_data;
  191. struct sun6i_rtc_dev *rtc;
  192. struct clk_init_data init = {
  193. .ops = &sun6i_rtc_osc_ops,
  194. .name = "losc",
  195. };
  196. const char *iosc_name = "rtc-int-osc";
  197. const char *clkout_name = "osc32k-out";
  198. const char *parents[2];
  199. u32 reg;
  200. rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
  201. if (!rtc)
  202. return;
  203. rtc->data = data;
  204. clk_data = kzalloc(struct_size(clk_data, hws, 3), GFP_KERNEL);
  205. if (!clk_data) {
  206. kfree(rtc);
  207. return;
  208. }
  209. spin_lock_init(&rtc->lock);
  210. rtc->base = of_io_request_and_map(node, 0, of_node_full_name(node));
  211. if (IS_ERR(rtc->base)) {
  212. pr_crit("Can't map RTC registers");
  213. goto err;
  214. }
  215. reg = SUN6I_LOSC_CTRL_KEY;
  216. if (rtc->data->has_auto_swt) {
  217. /* Bypass auto-switch to int osc, on ext losc failure */
  218. reg |= SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS;
  219. writel(reg, rtc->base + SUN6I_LOSC_CTRL);
  220. }
  221. /* Switch to the external, more precise, oscillator, if present */
  222. if (of_property_present(node, "clocks")) {
  223. reg |= SUN6I_LOSC_CTRL_EXT_OSC;
  224. if (rtc->data->has_losc_en)
  225. reg |= SUN6I_LOSC_CTRL_EXT_LOSC_EN;
  226. }
  227. writel(reg, rtc->base + SUN6I_LOSC_CTRL);
  228. /* Yes, I know, this is ugly. */
  229. sun6i_rtc = rtc;
  230. of_property_read_string_index(node, "clock-output-names", 2,
  231. &iosc_name);
  232. rtc->int_osc = clk_hw_register_fixed_rate_with_accuracy(NULL,
  233. iosc_name,
  234. NULL, 0,
  235. rtc->data->rc_osc_rate,
  236. 300000000);
  237. if (IS_ERR(rtc->int_osc)) {
  238. pr_crit("Couldn't register the internal oscillator\n");
  239. goto err;
  240. }
  241. parents[0] = clk_hw_get_name(rtc->int_osc);
  242. /* If there is no external oscillator, this will be NULL and ... */
  243. parents[1] = of_clk_get_parent_name(node, 0);
  244. rtc->hw.init = &init;
  245. init.parent_names = parents;
  246. /* ... number of clock parents will be 1. */
  247. init.num_parents = of_clk_get_parent_count(node) + 1;
  248. of_property_read_string_index(node, "clock-output-names", 0,
  249. &init.name);
  250. rtc->losc = clk_register(NULL, &rtc->hw);
  251. if (IS_ERR(rtc->losc)) {
  252. pr_crit("Couldn't register the LOSC clock\n");
  253. goto err_register;
  254. }
  255. of_property_read_string_index(node, "clock-output-names", 1,
  256. &clkout_name);
  257. rtc->ext_losc = clk_register_gate(NULL, clkout_name, init.name,
  258. 0, rtc->base + SUN6I_LOSC_OUT_GATING,
  259. SUN6I_LOSC_OUT_GATING_EN_OFFSET, 0,
  260. &rtc->lock);
  261. if (IS_ERR(rtc->ext_losc)) {
  262. pr_crit("Couldn't register the LOSC external gate\n");
  263. goto err_register;
  264. }
  265. clk_data->num = 3;
  266. clk_data->hws[0] = &rtc->hw;
  267. clk_data->hws[1] = __clk_get_hw(rtc->ext_losc);
  268. clk_data->hws[2] = rtc->int_osc;
  269. of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  270. return;
  271. err_register:
  272. clk_hw_unregister_fixed_rate(rtc->int_osc);
  273. err:
  274. kfree(clk_data);
  275. }
  276. static const struct sun6i_rtc_clk_data sun6i_a31_rtc_data = {
  277. .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
  278. .has_prescaler = 1,
  279. };
  280. static void __init sun6i_a31_rtc_clk_init(struct device_node *node)
  281. {
  282. sun6i_rtc_clk_init(node, &sun6i_a31_rtc_data);
  283. }
  284. CLK_OF_DECLARE_DRIVER(sun6i_a31_rtc_clk, "allwinner,sun6i-a31-rtc",
  285. sun6i_a31_rtc_clk_init);
  286. static const struct sun6i_rtc_clk_data sun8i_a23_rtc_data = {
  287. .rc_osc_rate = 667000, /* datasheet says 600 ~ 700 KHz */
  288. .has_prescaler = 1,
  289. .has_out_clk = 1,
  290. };
  291. static void __init sun8i_a23_rtc_clk_init(struct device_node *node)
  292. {
  293. sun6i_rtc_clk_init(node, &sun8i_a23_rtc_data);
  294. }
  295. CLK_OF_DECLARE_DRIVER(sun8i_a23_rtc_clk, "allwinner,sun8i-a23-rtc",
  296. sun8i_a23_rtc_clk_init);
  297. static const struct sun6i_rtc_clk_data sun8i_h3_rtc_data = {
  298. .rc_osc_rate = 16000000,
  299. .fixed_prescaler = 32,
  300. .has_prescaler = 1,
  301. .has_out_clk = 1,
  302. };
  303. static void __init sun8i_h3_rtc_clk_init(struct device_node *node)
  304. {
  305. sun6i_rtc_clk_init(node, &sun8i_h3_rtc_data);
  306. }
  307. CLK_OF_DECLARE_DRIVER(sun8i_h3_rtc_clk, "allwinner,sun8i-h3-rtc",
  308. sun8i_h3_rtc_clk_init);
  309. /* As far as we are concerned, clocks for H5 are the same as H3 */
  310. CLK_OF_DECLARE_DRIVER(sun50i_h5_rtc_clk, "allwinner,sun50i-h5-rtc",
  311. sun8i_h3_rtc_clk_init);
  312. static const struct sun6i_rtc_clk_data sun50i_h6_rtc_data = {
  313. .rc_osc_rate = 16000000,
  314. .fixed_prescaler = 32,
  315. .has_prescaler = 1,
  316. .has_out_clk = 1,
  317. .has_losc_en = 1,
  318. .has_auto_swt = 1,
  319. };
  320. static void __init sun50i_h6_rtc_clk_init(struct device_node *node)
  321. {
  322. sun6i_rtc_clk_init(node, &sun50i_h6_rtc_data);
  323. }
  324. CLK_OF_DECLARE_DRIVER(sun50i_h6_rtc_clk, "allwinner,sun50i-h6-rtc",
  325. sun50i_h6_rtc_clk_init);
  326. /*
  327. * The R40 user manual is self-conflicting on whether the prescaler is
  328. * fixed or configurable. The clock diagram shows it as fixed, but there
  329. * is also a configurable divider in the RTC block.
  330. */
  331. static const struct sun6i_rtc_clk_data sun8i_r40_rtc_data = {
  332. .rc_osc_rate = 16000000,
  333. .fixed_prescaler = 512,
  334. };
  335. static void __init sun8i_r40_rtc_clk_init(struct device_node *node)
  336. {
  337. sun6i_rtc_clk_init(node, &sun8i_r40_rtc_data);
  338. }
  339. CLK_OF_DECLARE_DRIVER(sun8i_r40_rtc_clk, "allwinner,sun8i-r40-rtc",
  340. sun8i_r40_rtc_clk_init);
  341. static const struct sun6i_rtc_clk_data sun8i_v3_rtc_data = {
  342. .rc_osc_rate = 32000,
  343. .has_out_clk = 1,
  344. .has_auto_swt = 1,
  345. };
  346. static void __init sun8i_v3_rtc_clk_init(struct device_node *node)
  347. {
  348. sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data);
  349. }
  350. CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc",
  351. sun8i_v3_rtc_clk_init);
  352. static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
  353. {
  354. struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
  355. irqreturn_t ret = IRQ_NONE;
  356. u32 val;
  357. spin_lock(&chip->lock);
  358. val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  359. if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
  360. val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
  361. writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
  362. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  363. ret = IRQ_HANDLED;
  364. }
  365. spin_unlock(&chip->lock);
  366. return ret;
  367. }
  368. static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
  369. {
  370. u32 alrm_val = 0;
  371. u32 alrm_irq_val = 0;
  372. u32 alrm_wake_val = 0;
  373. unsigned long flags;
  374. if (to) {
  375. alrm_val = SUN6I_ALRM_EN_CNT_EN;
  376. alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
  377. alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
  378. } else {
  379. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  380. chip->base + SUN6I_ALRM_IRQ_STA);
  381. }
  382. spin_lock_irqsave(&chip->lock, flags);
  383. writel(alrm_val, chip->base + SUN6I_ALRM_EN);
  384. writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
  385. writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
  386. spin_unlock_irqrestore(&chip->lock, flags);
  387. }
  388. static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  389. {
  390. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  391. u32 date, time;
  392. /*
  393. * read again in case it changes
  394. */
  395. do {
  396. date = readl(chip->base + SUN6I_RTC_YMD);
  397. time = readl(chip->base + SUN6I_RTC_HMS);
  398. } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
  399. (time != readl(chip->base + SUN6I_RTC_HMS)));
  400. if (chip->flags & RTC_LINEAR_DAY) {
  401. /*
  402. * Newer chips store a linear day number, the manual
  403. * does not mandate any epoch base. The BSP driver uses
  404. * the UNIX epoch, let's just copy that, as it's the
  405. * easiest anyway.
  406. */
  407. rtc_time64_to_tm((date & 0xffff) * SECS_PER_DAY, rtc_tm);
  408. } else {
  409. rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
  410. rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date) - 1;
  411. rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
  412. /*
  413. * switch from (data_year->min)-relative offset to
  414. * a (1900)-relative one
  415. */
  416. rtc_tm->tm_year += SUN6I_YEAR_OFF;
  417. }
  418. rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
  419. rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
  420. rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
  421. return 0;
  422. }
  423. static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  424. {
  425. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  426. unsigned long flags;
  427. u32 alrm_st;
  428. u32 alrm_en;
  429. spin_lock_irqsave(&chip->lock, flags);
  430. alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
  431. alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  432. spin_unlock_irqrestore(&chip->lock, flags);
  433. wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
  434. wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
  435. rtc_time64_to_tm(chip->alarm, &wkalrm->time);
  436. return 0;
  437. }
  438. static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  439. {
  440. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  441. struct rtc_time *alrm_tm = &wkalrm->time;
  442. struct rtc_time tm_now;
  443. time64_t time_set;
  444. u32 counter_val, counter_val_hms;
  445. int ret;
  446. time_set = rtc_tm_to_time64(alrm_tm);
  447. if (chip->flags & RTC_LINEAR_DAY) {
  448. /*
  449. * The alarm registers hold the actual alarm time, encoded
  450. * in the same way (linear day + HMS) as the current time.
  451. */
  452. counter_val_hms = SUN6I_TIME_SET_SEC_VALUE(alrm_tm->tm_sec) |
  453. SUN6I_TIME_SET_MIN_VALUE(alrm_tm->tm_min) |
  454. SUN6I_TIME_SET_HOUR_VALUE(alrm_tm->tm_hour);
  455. /* The division will cut off the H:M:S part of alrm_tm. */
  456. counter_val = div_u64(rtc_tm_to_time64(alrm_tm), SECS_PER_DAY);
  457. } else {
  458. /* The alarm register holds the number of seconds left. */
  459. time64_t time_now;
  460. ret = sun6i_rtc_gettime(dev, &tm_now);
  461. if (ret < 0) {
  462. dev_err(dev, "Error in getting time\n");
  463. return -EINVAL;
  464. }
  465. time_now = rtc_tm_to_time64(&tm_now);
  466. if (time_set <= time_now) {
  467. dev_err(dev, "Date to set in the past\n");
  468. return -EINVAL;
  469. }
  470. if ((time_set - time_now) > U32_MAX) {
  471. dev_err(dev, "Date too far in the future\n");
  472. return -EINVAL;
  473. }
  474. counter_val = time_set - time_now;
  475. }
  476. sun6i_rtc_setaie(0, chip);
  477. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  478. if (chip->flags & RTC_LINEAR_DAY)
  479. writel(0, chip->base + SUN6I_ALRM_COUNTER_HMS);
  480. usleep_range(100, 300);
  481. writel(counter_val, chip->base + SUN6I_ALRM_COUNTER);
  482. if (chip->flags & RTC_LINEAR_DAY)
  483. writel(counter_val_hms, chip->base + SUN6I_ALRM_COUNTER_HMS);
  484. chip->alarm = time_set;
  485. sun6i_rtc_setaie(wkalrm->enabled, chip);
  486. return 0;
  487. }
  488. static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
  489. unsigned int mask, unsigned int ms_timeout)
  490. {
  491. const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
  492. u32 reg;
  493. do {
  494. reg = readl(chip->base + offset);
  495. reg &= mask;
  496. if (!reg)
  497. return 0;
  498. } while (time_before(jiffies, timeout));
  499. return -ETIMEDOUT;
  500. }
  501. static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
  502. {
  503. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  504. u32 date = 0;
  505. u32 time = 0;
  506. time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
  507. SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
  508. SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
  509. if (chip->flags & RTC_LINEAR_DAY) {
  510. /* The division will cut off the H:M:S part of rtc_tm. */
  511. date = div_u64(rtc_tm_to_time64(rtc_tm), SECS_PER_DAY);
  512. } else {
  513. rtc_tm->tm_year -= SUN6I_YEAR_OFF;
  514. rtc_tm->tm_mon += 1;
  515. date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
  516. SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
  517. SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
  518. if (is_leap_year(rtc_tm->tm_year + SUN6I_YEAR_MIN))
  519. date |= SUN6I_LEAP_SET_VALUE(1);
  520. }
  521. /* Check whether registers are writable */
  522. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  523. SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
  524. dev_err(dev, "rtc is still busy.\n");
  525. return -EBUSY;
  526. }
  527. writel(time, chip->base + SUN6I_RTC_HMS);
  528. /*
  529. * After writing the RTC HH-MM-SS register, the
  530. * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
  531. * be cleared until the real writing operation is finished
  532. */
  533. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  534. SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
  535. dev_err(dev, "Failed to set rtc time.\n");
  536. return -ETIMEDOUT;
  537. }
  538. writel(date, chip->base + SUN6I_RTC_YMD);
  539. /*
  540. * After writing the RTC YY-MM-DD register, the
  541. * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
  542. * be cleared until the real writing operation is finished
  543. */
  544. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  545. SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
  546. dev_err(dev, "Failed to set rtc time.\n");
  547. return -ETIMEDOUT;
  548. }
  549. return 0;
  550. }
  551. static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  552. {
  553. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  554. if (!enabled)
  555. sun6i_rtc_setaie(enabled, chip);
  556. return 0;
  557. }
  558. static const struct rtc_class_ops sun6i_rtc_ops = {
  559. .read_time = sun6i_rtc_gettime,
  560. .set_time = sun6i_rtc_settime,
  561. .read_alarm = sun6i_rtc_getalarm,
  562. .set_alarm = sun6i_rtc_setalarm,
  563. .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
  564. };
  565. static int sun6i_rtc_nvmem_read(void *priv, unsigned int offset, void *_val, size_t bytes)
  566. {
  567. struct sun6i_rtc_dev *chip = priv;
  568. u32 *val = _val;
  569. int i;
  570. for (i = 0; i < bytes / 4; ++i)
  571. val[i] = readl(chip->base + SUN6I_GP_DATA + offset + 4 * i);
  572. return 0;
  573. }
  574. static int sun6i_rtc_nvmem_write(void *priv, unsigned int offset, void *_val, size_t bytes)
  575. {
  576. struct sun6i_rtc_dev *chip = priv;
  577. u32 *val = _val;
  578. int i;
  579. for (i = 0; i < bytes / 4; ++i)
  580. writel(val[i], chip->base + SUN6I_GP_DATA + offset + 4 * i);
  581. return 0;
  582. }
  583. static struct nvmem_config sun6i_rtc_nvmem_cfg = {
  584. .type = NVMEM_TYPE_BATTERY_BACKED,
  585. .reg_read = sun6i_rtc_nvmem_read,
  586. .reg_write = sun6i_rtc_nvmem_write,
  587. .size = SUN6I_GP_DATA_SIZE,
  588. .word_size = 4,
  589. .stride = 4,
  590. };
  591. #ifdef CONFIG_PM_SLEEP
  592. /* Enable IRQ wake on suspend, to wake up from RTC. */
  593. static int sun6i_rtc_suspend(struct device *dev)
  594. {
  595. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  596. if (device_may_wakeup(dev))
  597. enable_irq_wake(chip->irq);
  598. return 0;
  599. }
  600. /* Disable IRQ wake on resume. */
  601. static int sun6i_rtc_resume(struct device *dev)
  602. {
  603. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  604. if (device_may_wakeup(dev))
  605. disable_irq_wake(chip->irq);
  606. return 0;
  607. }
  608. #endif
  609. static SIMPLE_DEV_PM_OPS(sun6i_rtc_pm_ops,
  610. sun6i_rtc_suspend, sun6i_rtc_resume);
  611. static void sun6i_rtc_bus_clk_cleanup(void *data)
  612. {
  613. struct clk *bus_clk = data;
  614. clk_disable_unprepare(bus_clk);
  615. }
  616. static int sun6i_rtc_probe(struct platform_device *pdev)
  617. {
  618. struct sun6i_rtc_dev *chip = sun6i_rtc;
  619. struct device *dev = &pdev->dev;
  620. struct clk *bus_clk;
  621. int ret;
  622. bus_clk = devm_clk_get_optional(dev, "bus");
  623. if (IS_ERR(bus_clk))
  624. return PTR_ERR(bus_clk);
  625. if (bus_clk) {
  626. ret = clk_prepare_enable(bus_clk);
  627. if (ret)
  628. return ret;
  629. ret = devm_add_action_or_reset(dev, sun6i_rtc_bus_clk_cleanup,
  630. bus_clk);
  631. if (ret)
  632. return ret;
  633. }
  634. if (!chip) {
  635. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  636. if (!chip)
  637. return -ENOMEM;
  638. spin_lock_init(&chip->lock);
  639. chip->base = devm_platform_ioremap_resource(pdev, 0);
  640. if (IS_ERR(chip->base))
  641. return PTR_ERR(chip->base);
  642. if (IS_REACHABLE(CONFIG_SUN6I_RTC_CCU)) {
  643. ret = sun6i_rtc_ccu_probe(dev, chip->base);
  644. if (ret)
  645. return ret;
  646. }
  647. }
  648. platform_set_drvdata(pdev, chip);
  649. chip->flags = (unsigned long)of_device_get_match_data(&pdev->dev);
  650. chip->irq = platform_get_irq(pdev, 0);
  651. if (chip->irq < 0)
  652. return chip->irq;
  653. ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
  654. 0, dev_name(&pdev->dev), chip);
  655. if (ret) {
  656. dev_err(&pdev->dev, "Could not request IRQ\n");
  657. return ret;
  658. }
  659. /* clear the alarm counter value */
  660. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  661. /* disable counter alarm */
  662. writel(0, chip->base + SUN6I_ALRM_EN);
  663. /* disable counter alarm interrupt */
  664. writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
  665. /* disable week alarm */
  666. writel(0, chip->base + SUN6I_ALRM1_EN);
  667. /* disable week alarm interrupt */
  668. writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
  669. /* clear counter alarm pending interrupts */
  670. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  671. chip->base + SUN6I_ALRM_IRQ_STA);
  672. /* clear week alarm pending interrupts */
  673. writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
  674. chip->base + SUN6I_ALRM1_IRQ_STA);
  675. /* disable alarm wakeup */
  676. writel(0, chip->base + SUN6I_ALARM_CONFIG);
  677. clk_prepare_enable(chip->losc);
  678. device_init_wakeup(&pdev->dev, 1);
  679. chip->rtc = devm_rtc_allocate_device(&pdev->dev);
  680. if (IS_ERR(chip->rtc))
  681. return PTR_ERR(chip->rtc);
  682. chip->rtc->ops = &sun6i_rtc_ops;
  683. if (chip->flags & RTC_LINEAR_DAY)
  684. chip->rtc->range_max = (65536 * SECS_PER_DAY) - 1;
  685. else
  686. chip->rtc->range_max = 2019686399LL; /* 2033-12-31 23:59:59 */
  687. ret = devm_rtc_register_device(chip->rtc);
  688. if (ret)
  689. return ret;
  690. sun6i_rtc_nvmem_cfg.priv = chip;
  691. ret = devm_rtc_nvmem_register(chip->rtc, &sun6i_rtc_nvmem_cfg);
  692. if (ret)
  693. return ret;
  694. return 0;
  695. }
  696. /*
  697. * As far as RTC functionality goes, all models are the same. The
  698. * datasheets claim that different models have different number of
  699. * registers available for non-volatile storage, but experiments show
  700. * that all SoCs have 16 registers available for this purpose.
  701. */
  702. static const struct of_device_id sun6i_rtc_dt_ids[] = {
  703. { .compatible = "allwinner,sun6i-a31-rtc" },
  704. { .compatible = "allwinner,sun8i-a23-rtc" },
  705. { .compatible = "allwinner,sun8i-h3-rtc" },
  706. { .compatible = "allwinner,sun8i-r40-rtc" },
  707. { .compatible = "allwinner,sun8i-v3-rtc" },
  708. { .compatible = "allwinner,sun50i-h5-rtc" },
  709. { .compatible = "allwinner,sun50i-h6-rtc" },
  710. { .compatible = "allwinner,sun50i-h616-rtc",
  711. .data = (void *)RTC_LINEAR_DAY },
  712. { .compatible = "allwinner,sun50i-r329-rtc",
  713. .data = (void *)RTC_LINEAR_DAY },
  714. { /* sentinel */ },
  715. };
  716. MODULE_DEVICE_TABLE(of, sun6i_rtc_dt_ids);
  717. static struct platform_driver sun6i_rtc_driver = {
  718. .probe = sun6i_rtc_probe,
  719. .driver = {
  720. .name = "sun6i-rtc",
  721. .of_match_table = sun6i_rtc_dt_ids,
  722. .pm = &sun6i_rtc_pm_ops,
  723. },
  724. };
  725. builtin_platform_driver(sun6i_rtc_driver);