rtc-sun6i.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 <[email protected]>
  6. *
  7. * based on rtc-sunxi.c
  8. *
  9. * An RTC driver for Allwinner A10/A20
  10. *
  11. * Copyright (c) 2013, Carlo Caione <[email protected]>
  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/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/rtc.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. /* Control register */
  32. #define SUN6I_LOSC_CTRL 0x0000
  33. #define SUN6I_LOSC_CTRL_KEY (0x16aa << 16)
  34. #define SUN6I_LOSC_CTRL_AUTO_SWT_BYPASS BIT(15)
  35. #define SUN6I_LOSC_CTRL_ALM_DHMS_ACC BIT(9)
  36. #define SUN6I_LOSC_CTRL_RTC_HMS_ACC BIT(8)
  37. #define SUN6I_LOSC_CTRL_RTC_YMD_ACC BIT(7)
  38. #define SUN6I_LOSC_CTRL_EXT_LOSC_EN BIT(4)
  39. #define SUN6I_LOSC_CTRL_EXT_OSC BIT(0)
  40. #define SUN6I_LOSC_CTRL_ACC_MASK GENMASK(9, 7)
  41. #define SUN6I_LOSC_CLK_PRESCAL 0x0008
  42. /* RTC */
  43. #define SUN6I_RTC_YMD 0x0010
  44. #define SUN6I_RTC_HMS 0x0014
  45. /* Alarm 0 (counter) */
  46. #define SUN6I_ALRM_COUNTER 0x0020
  47. /* This holds the remaining alarm seconds on older SoCs (current value) */
  48. #define SUN6I_ALRM_COUNTER_HMS 0x0024
  49. #define SUN6I_ALRM_EN 0x0028
  50. #define SUN6I_ALRM_EN_CNT_EN BIT(0)
  51. #define SUN6I_ALRM_IRQ_EN 0x002c
  52. #define SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN BIT(0)
  53. #define SUN6I_ALRM_IRQ_STA 0x0030
  54. #define SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND BIT(0)
  55. /* Alarm 1 (wall clock) */
  56. #define SUN6I_ALRM1_EN 0x0044
  57. #define SUN6I_ALRM1_IRQ_EN 0x0048
  58. #define SUN6I_ALRM1_IRQ_STA 0x004c
  59. #define SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND BIT(0)
  60. /* Alarm config */
  61. #define SUN6I_ALARM_CONFIG 0x0050
  62. #define SUN6I_ALARM_CONFIG_WAKEUP BIT(0)
  63. #define SUN6I_LOSC_OUT_GATING 0x0060
  64. #define SUN6I_LOSC_OUT_GATING_EN_OFFSET 0
  65. /* General-purpose data */
  66. #define SUN6I_GP_DATA 0x0100
  67. #define SUN6I_GP_DATA_SIZE 0x20
  68. /*
  69. * Get date values
  70. */
  71. #define SUN6I_DATE_GET_DAY_VALUE(x) ((x) & 0x0000001f)
  72. #define SUN6I_DATE_GET_MON_VALUE(x) (((x) & 0x00000f00) >> 8)
  73. #define SUN6I_DATE_GET_YEAR_VALUE(x) (((x) & 0x003f0000) >> 16)
  74. #define SUN6I_LEAP_GET_VALUE(x) (((x) & 0x00400000) >> 22)
  75. /*
  76. * Get time values
  77. */
  78. #define SUN6I_TIME_GET_SEC_VALUE(x) ((x) & 0x0000003f)
  79. #define SUN6I_TIME_GET_MIN_VALUE(x) (((x) & 0x00003f00) >> 8)
  80. #define SUN6I_TIME_GET_HOUR_VALUE(x) (((x) & 0x001f0000) >> 16)
  81. /*
  82. * Set date values
  83. */
  84. #define SUN6I_DATE_SET_DAY_VALUE(x) ((x) & 0x0000001f)
  85. #define SUN6I_DATE_SET_MON_VALUE(x) ((x) << 8 & 0x00000f00)
  86. #define SUN6I_DATE_SET_YEAR_VALUE(x) ((x) << 16 & 0x003f0000)
  87. #define SUN6I_LEAP_SET_VALUE(x) ((x) << 22 & 0x00400000)
  88. /*
  89. * Set time values
  90. */
  91. #define SUN6I_TIME_SET_SEC_VALUE(x) ((x) & 0x0000003f)
  92. #define SUN6I_TIME_SET_MIN_VALUE(x) ((x) << 8 & 0x00003f00)
  93. #define SUN6I_TIME_SET_HOUR_VALUE(x) ((x) << 16 & 0x001f0000)
  94. /*
  95. * The year parameter passed to the driver is usually an offset relative to
  96. * the year 1900. This macro is used to convert this offset to another one
  97. * relative to the minimum year allowed by the hardware.
  98. *
  99. * The year range is 1970 - 2033. This range is selected to match Allwinner's
  100. * driver, even though it is somewhat limited.
  101. */
  102. #define SUN6I_YEAR_MIN 1970
  103. #define SUN6I_YEAR_OFF (SUN6I_YEAR_MIN - 1900)
  104. #define SECS_PER_DAY (24 * 3600ULL)
  105. /*
  106. * There are other differences between models, including:
  107. *
  108. * - number of GPIO pins that can be configured to hold a certain level
  109. * - crypto-key related registers (H5, H6)
  110. * - boot process related (super standby, secondary processor entry address)
  111. * registers (R40, H6)
  112. * - SYS power domain controls (R40)
  113. * - DCXO controls (H6)
  114. * - RC oscillator calibration (H6)
  115. *
  116. * These functions are not covered by this driver.
  117. */
  118. struct sun6i_rtc_clk_data {
  119. unsigned long rc_osc_rate;
  120. unsigned int fixed_prescaler : 16;
  121. unsigned int has_prescaler : 1;
  122. unsigned int has_out_clk : 1;
  123. unsigned int has_losc_en : 1;
  124. unsigned int has_auto_swt : 1;
  125. };
  126. #define RTC_LINEAR_DAY BIT(0)
  127. struct sun6i_rtc_dev {
  128. struct rtc_device *rtc;
  129. const struct sun6i_rtc_clk_data *data;
  130. void __iomem *base;
  131. int irq;
  132. time64_t alarm;
  133. unsigned long flags;
  134. struct clk_hw hw;
  135. struct clk_hw *int_osc;
  136. struct clk *losc;
  137. struct clk *ext_losc;
  138. spinlock_t lock;
  139. };
  140. static struct sun6i_rtc_dev *sun6i_rtc;
  141. static unsigned long sun6i_rtc_osc_recalc_rate(struct clk_hw *hw,
  142. unsigned long parent_rate)
  143. {
  144. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  145. u32 val = 0;
  146. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  147. if (val & SUN6I_LOSC_CTRL_EXT_OSC)
  148. return parent_rate;
  149. if (rtc->data->fixed_prescaler)
  150. parent_rate /= rtc->data->fixed_prescaler;
  151. if (rtc->data->has_prescaler) {
  152. val = readl(rtc->base + SUN6I_LOSC_CLK_PRESCAL);
  153. val &= GENMASK(4, 0);
  154. }
  155. return parent_rate / (val + 1);
  156. }
  157. static u8 sun6i_rtc_osc_get_parent(struct clk_hw *hw)
  158. {
  159. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  160. return readl(rtc->base + SUN6I_LOSC_CTRL) & SUN6I_LOSC_CTRL_EXT_OSC;
  161. }
  162. static int sun6i_rtc_osc_set_parent(struct clk_hw *hw, u8 index)
  163. {
  164. struct sun6i_rtc_dev *rtc = container_of(hw, struct sun6i_rtc_dev, hw);
  165. unsigned long flags;
  166. u32 val;
  167. if (index > 1)
  168. return -EINVAL;
  169. spin_lock_irqsave(&rtc->lock, flags);
  170. val = readl(rtc->base + SUN6I_LOSC_CTRL);
  171. val &= ~SUN6I_LOSC_CTRL_EXT_OSC;
  172. val |= SUN6I_LOSC_CTRL_KEY;
  173. val |= index ? SUN6I_LOSC_CTRL_EXT_OSC : 0;
  174. if (rtc->data->has_losc_en) {
  175. val &= ~SUN6I_LOSC_CTRL_EXT_LOSC_EN;
  176. val |= index ? SUN6I_LOSC_CTRL_EXT_LOSC_EN : 0;
  177. }
  178. writel(val, rtc->base + SUN6I_LOSC_CTRL);
  179. spin_unlock_irqrestore(&rtc->lock, flags);
  180. return 0;
  181. }
  182. static const struct clk_ops sun6i_rtc_osc_ops = {
  183. .recalc_rate = sun6i_rtc_osc_recalc_rate,
  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_get_property(node, "clocks", NULL)) {
  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. };
  345. static void __init sun8i_v3_rtc_clk_init(struct device_node *node)
  346. {
  347. sun6i_rtc_clk_init(node, &sun8i_v3_rtc_data);
  348. }
  349. CLK_OF_DECLARE_DRIVER(sun8i_v3_rtc_clk, "allwinner,sun8i-v3-rtc",
  350. sun8i_v3_rtc_clk_init);
  351. static irqreturn_t sun6i_rtc_alarmirq(int irq, void *id)
  352. {
  353. struct sun6i_rtc_dev *chip = (struct sun6i_rtc_dev *) id;
  354. irqreturn_t ret = IRQ_NONE;
  355. u32 val;
  356. spin_lock(&chip->lock);
  357. val = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  358. if (val & SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND) {
  359. val |= SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND;
  360. writel(val, chip->base + SUN6I_ALRM_IRQ_STA);
  361. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  362. ret = IRQ_HANDLED;
  363. }
  364. spin_unlock(&chip->lock);
  365. return ret;
  366. }
  367. static void sun6i_rtc_setaie(int to, struct sun6i_rtc_dev *chip)
  368. {
  369. u32 alrm_val = 0;
  370. u32 alrm_irq_val = 0;
  371. u32 alrm_wake_val = 0;
  372. unsigned long flags;
  373. if (to) {
  374. alrm_val = SUN6I_ALRM_EN_CNT_EN;
  375. alrm_irq_val = SUN6I_ALRM_IRQ_EN_CNT_IRQ_EN;
  376. alrm_wake_val = SUN6I_ALARM_CONFIG_WAKEUP;
  377. } else {
  378. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  379. chip->base + SUN6I_ALRM_IRQ_STA);
  380. }
  381. spin_lock_irqsave(&chip->lock, flags);
  382. writel(alrm_val, chip->base + SUN6I_ALRM_EN);
  383. writel(alrm_irq_val, chip->base + SUN6I_ALRM_IRQ_EN);
  384. writel(alrm_wake_val, chip->base + SUN6I_ALARM_CONFIG);
  385. spin_unlock_irqrestore(&chip->lock, flags);
  386. }
  387. static int sun6i_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
  388. {
  389. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  390. u32 date, time;
  391. /*
  392. * read again in case it changes
  393. */
  394. do {
  395. date = readl(chip->base + SUN6I_RTC_YMD);
  396. time = readl(chip->base + SUN6I_RTC_HMS);
  397. } while ((date != readl(chip->base + SUN6I_RTC_YMD)) ||
  398. (time != readl(chip->base + SUN6I_RTC_HMS)));
  399. if (chip->flags & RTC_LINEAR_DAY) {
  400. /*
  401. * Newer chips store a linear day number, the manual
  402. * does not mandate any epoch base. The BSP driver uses
  403. * the UNIX epoch, let's just copy that, as it's the
  404. * easiest anyway.
  405. */
  406. rtc_time64_to_tm((date & 0xffff) * SECS_PER_DAY, rtc_tm);
  407. } else {
  408. rtc_tm->tm_mday = SUN6I_DATE_GET_DAY_VALUE(date);
  409. rtc_tm->tm_mon = SUN6I_DATE_GET_MON_VALUE(date) - 1;
  410. rtc_tm->tm_year = SUN6I_DATE_GET_YEAR_VALUE(date);
  411. /*
  412. * switch from (data_year->min)-relative offset to
  413. * a (1900)-relative one
  414. */
  415. rtc_tm->tm_year += SUN6I_YEAR_OFF;
  416. }
  417. rtc_tm->tm_sec = SUN6I_TIME_GET_SEC_VALUE(time);
  418. rtc_tm->tm_min = SUN6I_TIME_GET_MIN_VALUE(time);
  419. rtc_tm->tm_hour = SUN6I_TIME_GET_HOUR_VALUE(time);
  420. return 0;
  421. }
  422. static int sun6i_rtc_getalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  423. {
  424. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  425. unsigned long flags;
  426. u32 alrm_st;
  427. u32 alrm_en;
  428. spin_lock_irqsave(&chip->lock, flags);
  429. alrm_en = readl(chip->base + SUN6I_ALRM_IRQ_EN);
  430. alrm_st = readl(chip->base + SUN6I_ALRM_IRQ_STA);
  431. spin_unlock_irqrestore(&chip->lock, flags);
  432. wkalrm->enabled = !!(alrm_en & SUN6I_ALRM_EN_CNT_EN);
  433. wkalrm->pending = !!(alrm_st & SUN6I_ALRM_EN_CNT_EN);
  434. rtc_time64_to_tm(chip->alarm, &wkalrm->time);
  435. return 0;
  436. }
  437. static int sun6i_rtc_setalarm(struct device *dev, struct rtc_wkalrm *wkalrm)
  438. {
  439. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  440. struct rtc_time *alrm_tm = &wkalrm->time;
  441. struct rtc_time tm_now;
  442. time64_t time_set;
  443. u32 counter_val, counter_val_hms;
  444. int ret;
  445. time_set = rtc_tm_to_time64(alrm_tm);
  446. if (chip->flags & RTC_LINEAR_DAY) {
  447. /*
  448. * The alarm registers hold the actual alarm time, encoded
  449. * in the same way (linear day + HMS) as the current time.
  450. */
  451. counter_val_hms = SUN6I_TIME_SET_SEC_VALUE(alrm_tm->tm_sec) |
  452. SUN6I_TIME_SET_MIN_VALUE(alrm_tm->tm_min) |
  453. SUN6I_TIME_SET_HOUR_VALUE(alrm_tm->tm_hour);
  454. /* The division will cut off the H:M:S part of alrm_tm. */
  455. counter_val = div_u64(rtc_tm_to_time64(alrm_tm), SECS_PER_DAY);
  456. } else {
  457. /* The alarm register holds the number of seconds left. */
  458. time64_t time_now;
  459. ret = sun6i_rtc_gettime(dev, &tm_now);
  460. if (ret < 0) {
  461. dev_err(dev, "Error in getting time\n");
  462. return -EINVAL;
  463. }
  464. time_now = rtc_tm_to_time64(&tm_now);
  465. if (time_set <= time_now) {
  466. dev_err(dev, "Date to set in the past\n");
  467. return -EINVAL;
  468. }
  469. if ((time_set - time_now) > U32_MAX) {
  470. dev_err(dev, "Date too far in the future\n");
  471. return -EINVAL;
  472. }
  473. counter_val = time_set - time_now;
  474. }
  475. sun6i_rtc_setaie(0, chip);
  476. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  477. if (chip->flags & RTC_LINEAR_DAY)
  478. writel(0, chip->base + SUN6I_ALRM_COUNTER_HMS);
  479. usleep_range(100, 300);
  480. writel(counter_val, chip->base + SUN6I_ALRM_COUNTER);
  481. if (chip->flags & RTC_LINEAR_DAY)
  482. writel(counter_val_hms, chip->base + SUN6I_ALRM_COUNTER_HMS);
  483. chip->alarm = time_set;
  484. sun6i_rtc_setaie(wkalrm->enabled, chip);
  485. return 0;
  486. }
  487. static int sun6i_rtc_wait(struct sun6i_rtc_dev *chip, int offset,
  488. unsigned int mask, unsigned int ms_timeout)
  489. {
  490. const unsigned long timeout = jiffies + msecs_to_jiffies(ms_timeout);
  491. u32 reg;
  492. do {
  493. reg = readl(chip->base + offset);
  494. reg &= mask;
  495. if (!reg)
  496. return 0;
  497. } while (time_before(jiffies, timeout));
  498. return -ETIMEDOUT;
  499. }
  500. static int sun6i_rtc_settime(struct device *dev, struct rtc_time *rtc_tm)
  501. {
  502. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  503. u32 date = 0;
  504. u32 time = 0;
  505. time = SUN6I_TIME_SET_SEC_VALUE(rtc_tm->tm_sec) |
  506. SUN6I_TIME_SET_MIN_VALUE(rtc_tm->tm_min) |
  507. SUN6I_TIME_SET_HOUR_VALUE(rtc_tm->tm_hour);
  508. if (chip->flags & RTC_LINEAR_DAY) {
  509. /* The division will cut off the H:M:S part of rtc_tm. */
  510. date = div_u64(rtc_tm_to_time64(rtc_tm), SECS_PER_DAY);
  511. } else {
  512. rtc_tm->tm_year -= SUN6I_YEAR_OFF;
  513. rtc_tm->tm_mon += 1;
  514. date = SUN6I_DATE_SET_DAY_VALUE(rtc_tm->tm_mday) |
  515. SUN6I_DATE_SET_MON_VALUE(rtc_tm->tm_mon) |
  516. SUN6I_DATE_SET_YEAR_VALUE(rtc_tm->tm_year);
  517. if (is_leap_year(rtc_tm->tm_year + SUN6I_YEAR_MIN))
  518. date |= SUN6I_LEAP_SET_VALUE(1);
  519. }
  520. /* Check whether registers are writable */
  521. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  522. SUN6I_LOSC_CTRL_ACC_MASK, 50)) {
  523. dev_err(dev, "rtc is still busy.\n");
  524. return -EBUSY;
  525. }
  526. writel(time, chip->base + SUN6I_RTC_HMS);
  527. /*
  528. * After writing the RTC HH-MM-SS register, the
  529. * SUN6I_LOSC_CTRL_RTC_HMS_ACC bit is set and it will not
  530. * be cleared until the real writing operation is finished
  531. */
  532. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  533. SUN6I_LOSC_CTRL_RTC_HMS_ACC, 50)) {
  534. dev_err(dev, "Failed to set rtc time.\n");
  535. return -ETIMEDOUT;
  536. }
  537. writel(date, chip->base + SUN6I_RTC_YMD);
  538. /*
  539. * After writing the RTC YY-MM-DD register, the
  540. * SUN6I_LOSC_CTRL_RTC_YMD_ACC bit is set and it will not
  541. * be cleared until the real writing operation is finished
  542. */
  543. if (sun6i_rtc_wait(chip, SUN6I_LOSC_CTRL,
  544. SUN6I_LOSC_CTRL_RTC_YMD_ACC, 50)) {
  545. dev_err(dev, "Failed to set rtc time.\n");
  546. return -ETIMEDOUT;
  547. }
  548. return 0;
  549. }
  550. static int sun6i_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  551. {
  552. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  553. if (!enabled)
  554. sun6i_rtc_setaie(enabled, chip);
  555. return 0;
  556. }
  557. static const struct rtc_class_ops sun6i_rtc_ops = {
  558. .read_time = sun6i_rtc_gettime,
  559. .set_time = sun6i_rtc_settime,
  560. .read_alarm = sun6i_rtc_getalarm,
  561. .set_alarm = sun6i_rtc_setalarm,
  562. .alarm_irq_enable = sun6i_rtc_alarm_irq_enable
  563. };
  564. static int sun6i_rtc_nvmem_read(void *priv, unsigned int offset, void *_val, size_t bytes)
  565. {
  566. struct sun6i_rtc_dev *chip = priv;
  567. u32 *val = _val;
  568. int i;
  569. for (i = 0; i < bytes / 4; ++i)
  570. val[i] = readl(chip->base + SUN6I_GP_DATA + offset + 4 * i);
  571. return 0;
  572. }
  573. static int sun6i_rtc_nvmem_write(void *priv, unsigned int offset, void *_val, size_t bytes)
  574. {
  575. struct sun6i_rtc_dev *chip = priv;
  576. u32 *val = _val;
  577. int i;
  578. for (i = 0; i < bytes / 4; ++i)
  579. writel(val[i], chip->base + SUN6I_GP_DATA + offset + 4 * i);
  580. return 0;
  581. }
  582. static struct nvmem_config sun6i_rtc_nvmem_cfg = {
  583. .type = NVMEM_TYPE_BATTERY_BACKED,
  584. .reg_read = sun6i_rtc_nvmem_read,
  585. .reg_write = sun6i_rtc_nvmem_write,
  586. .size = SUN6I_GP_DATA_SIZE,
  587. .word_size = 4,
  588. .stride = 4,
  589. };
  590. #ifdef CONFIG_PM_SLEEP
  591. /* Enable IRQ wake on suspend, to wake up from RTC. */
  592. static int sun6i_rtc_suspend(struct device *dev)
  593. {
  594. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  595. if (device_may_wakeup(dev))
  596. enable_irq_wake(chip->irq);
  597. return 0;
  598. }
  599. /* Disable IRQ wake on resume. */
  600. static int sun6i_rtc_resume(struct device *dev)
  601. {
  602. struct sun6i_rtc_dev *chip = dev_get_drvdata(dev);
  603. if (device_may_wakeup(dev))
  604. disable_irq_wake(chip->irq);
  605. return 0;
  606. }
  607. #endif
  608. static SIMPLE_DEV_PM_OPS(sun6i_rtc_pm_ops,
  609. sun6i_rtc_suspend, sun6i_rtc_resume);
  610. static void sun6i_rtc_bus_clk_cleanup(void *data)
  611. {
  612. struct clk *bus_clk = data;
  613. clk_disable_unprepare(bus_clk);
  614. }
  615. static int sun6i_rtc_probe(struct platform_device *pdev)
  616. {
  617. struct sun6i_rtc_dev *chip = sun6i_rtc;
  618. struct device *dev = &pdev->dev;
  619. struct clk *bus_clk;
  620. int ret;
  621. bus_clk = devm_clk_get_optional(dev, "bus");
  622. if (IS_ERR(bus_clk))
  623. return PTR_ERR(bus_clk);
  624. if (bus_clk) {
  625. ret = clk_prepare_enable(bus_clk);
  626. if (ret)
  627. return ret;
  628. ret = devm_add_action_or_reset(dev, sun6i_rtc_bus_clk_cleanup,
  629. bus_clk);
  630. if (ret)
  631. return ret;
  632. }
  633. if (!chip) {
  634. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  635. if (!chip)
  636. return -ENOMEM;
  637. spin_lock_init(&chip->lock);
  638. chip->base = devm_platform_ioremap_resource(pdev, 0);
  639. if (IS_ERR(chip->base))
  640. return PTR_ERR(chip->base);
  641. if (IS_REACHABLE(CONFIG_SUN6I_RTC_CCU)) {
  642. ret = sun6i_rtc_ccu_probe(dev, chip->base);
  643. if (ret)
  644. return ret;
  645. }
  646. }
  647. platform_set_drvdata(pdev, chip);
  648. chip->flags = (unsigned long)of_device_get_match_data(&pdev->dev);
  649. chip->irq = platform_get_irq(pdev, 0);
  650. if (chip->irq < 0)
  651. return chip->irq;
  652. ret = devm_request_irq(&pdev->dev, chip->irq, sun6i_rtc_alarmirq,
  653. 0, dev_name(&pdev->dev), chip);
  654. if (ret) {
  655. dev_err(&pdev->dev, "Could not request IRQ\n");
  656. return ret;
  657. }
  658. /* clear the alarm counter value */
  659. writel(0, chip->base + SUN6I_ALRM_COUNTER);
  660. /* disable counter alarm */
  661. writel(0, chip->base + SUN6I_ALRM_EN);
  662. /* disable counter alarm interrupt */
  663. writel(0, chip->base + SUN6I_ALRM_IRQ_EN);
  664. /* disable week alarm */
  665. writel(0, chip->base + SUN6I_ALRM1_EN);
  666. /* disable week alarm interrupt */
  667. writel(0, chip->base + SUN6I_ALRM1_IRQ_EN);
  668. /* clear counter alarm pending interrupts */
  669. writel(SUN6I_ALRM_IRQ_STA_CNT_IRQ_PEND,
  670. chip->base + SUN6I_ALRM_IRQ_STA);
  671. /* clear week alarm pending interrupts */
  672. writel(SUN6I_ALRM1_IRQ_STA_WEEK_IRQ_PEND,
  673. chip->base + SUN6I_ALRM1_IRQ_STA);
  674. /* disable alarm wakeup */
  675. writel(0, chip->base + SUN6I_ALARM_CONFIG);
  676. clk_prepare_enable(chip->losc);
  677. device_init_wakeup(&pdev->dev, 1);
  678. chip->rtc = devm_rtc_allocate_device(&pdev->dev);
  679. if (IS_ERR(chip->rtc))
  680. return PTR_ERR(chip->rtc);
  681. chip->rtc->ops = &sun6i_rtc_ops;
  682. if (chip->flags & RTC_LINEAR_DAY)
  683. chip->rtc->range_max = (65536 * SECS_PER_DAY) - 1;
  684. else
  685. chip->rtc->range_max = 2019686399LL; /* 2033-12-31 23:59:59 */
  686. ret = devm_rtc_register_device(chip->rtc);
  687. if (ret)
  688. return ret;
  689. sun6i_rtc_nvmem_cfg.priv = chip;
  690. ret = devm_rtc_nvmem_register(chip->rtc, &sun6i_rtc_nvmem_cfg);
  691. if (ret)
  692. return ret;
  693. dev_info(&pdev->dev, "RTC enabled\n");
  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);