1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4 * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
5 * JZ4740 SoC RTC driver
9 #include <linux/clk-provider.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_wakeirq.h>
17 #include <linux/property.h>
18 #include <linux/reboot.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
23 #define JZ_REG_RTC_CTRL 0x00
24 #define JZ_REG_RTC_SEC 0x04
25 #define JZ_REG_RTC_SEC_ALARM 0x08
26 #define JZ_REG_RTC_REGULATOR 0x0C
27 #define JZ_REG_RTC_HIBERNATE 0x20
28 #define JZ_REG_RTC_WAKEUP_FILTER 0x24
29 #define JZ_REG_RTC_RESET_COUNTER 0x28
30 #define JZ_REG_RTC_SCRATCHPAD 0x34
31 #define JZ_REG_RTC_CKPCR 0x40
33 /* The following are present on the jz4780 */
34 #define JZ_REG_RTC_WENR 0x3C
35 #define JZ_RTC_WENR_WEN BIT(31)
37 #define JZ_RTC_CTRL_WRDY BIT(7)
38 #define JZ_RTC_CTRL_1HZ BIT(6)
39 #define JZ_RTC_CTRL_1HZ_IRQ BIT(5)
40 #define JZ_RTC_CTRL_AF BIT(4)
41 #define JZ_RTC_CTRL_AF_IRQ BIT(3)
42 #define JZ_RTC_CTRL_AE BIT(2)
43 #define JZ_RTC_CTRL_ENABLE BIT(0)
45 /* Magic value to enable writes on jz4780 */
46 #define JZ_RTC_WENR_MAGIC 0xA55A
48 #define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0
49 #define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0
51 #define JZ_RTC_CKPCR_CK32PULL_DIS BIT(4)
52 #define JZ_RTC_CKPCR_CK32CTL_EN (BIT(2) | BIT(1))
54 enum jz4740_rtc_type
{
62 enum jz4740_rtc_type type
;
64 struct rtc_device
*rtc
;
71 static struct device
*dev_for_power_off
;
73 static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc
*rtc
, size_t reg
)
75 return readl(rtc
->base
+ reg
);
78 static int jz4740_rtc_wait_write_ready(struct jz4740_rtc
*rtc
)
82 return readl_poll_timeout(rtc
->base
+ JZ_REG_RTC_CTRL
, ctrl
,
83 ctrl
& JZ_RTC_CTRL_WRDY
, 0, 1000);
86 static inline int jz4780_rtc_enable_write(struct jz4740_rtc
*rtc
)
91 ret
= jz4740_rtc_wait_write_ready(rtc
);
95 writel(JZ_RTC_WENR_MAGIC
, rtc
->base
+ JZ_REG_RTC_WENR
);
97 return readl_poll_timeout(rtc
->base
+ JZ_REG_RTC_WENR
, ctrl
,
98 ctrl
& JZ_RTC_WENR_WEN
, 0, 1000);
101 static inline int jz4740_rtc_reg_write(struct jz4740_rtc
*rtc
, size_t reg
,
106 if (rtc
->type
>= ID_JZ4760
)
107 ret
= jz4780_rtc_enable_write(rtc
);
109 ret
= jz4740_rtc_wait_write_ready(rtc
);
111 writel(val
, rtc
->base
+ reg
);
116 static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc
*rtc
, uint32_t mask
,
123 spin_lock_irqsave(&rtc
->lock
, flags
);
125 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
127 /* Don't clear interrupt flags by accident */
128 ctrl
|= JZ_RTC_CTRL_1HZ
| JZ_RTC_CTRL_AF
;
135 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_CTRL
, ctrl
);
137 spin_unlock_irqrestore(&rtc
->lock
, flags
);
142 static int jz4740_rtc_read_time(struct device
*dev
, struct rtc_time
*time
)
144 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
145 uint32_t secs
, secs2
;
148 if (jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SCRATCHPAD
) != 0x12345678)
151 /* If the seconds register is read while it is updated, it can contain a
152 * bogus value. This can be avoided by making sure that two consecutive
153 * reads have the same value.
155 secs
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
156 secs2
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
158 while (secs
!= secs2
&& --timeout
) {
160 secs2
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC
);
166 rtc_time64_to_tm(secs
, time
);
171 static int jz4740_rtc_set_time(struct device
*dev
, struct rtc_time
*time
)
173 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
176 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SEC
, rtc_tm_to_time64(time
));
180 return jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SCRATCHPAD
, 0x12345678);
183 static int jz4740_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
185 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
189 secs
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_SEC_ALARM
);
191 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
193 alrm
->enabled
= !!(ctrl
& JZ_RTC_CTRL_AE
);
194 alrm
->pending
= !!(ctrl
& JZ_RTC_CTRL_AF
);
196 rtc_time64_to_tm(secs
, &alrm
->time
);
201 static int jz4740_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
204 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
205 uint32_t secs
= lower_32_bits(rtc_tm_to_time64(&alrm
->time
));
207 ret
= jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_SEC_ALARM
, secs
);
209 ret
= jz4740_rtc_ctrl_set_bits(rtc
,
210 JZ_RTC_CTRL_AE
| JZ_RTC_CTRL_AF_IRQ
, alrm
->enabled
);
215 static int jz4740_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
217 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
218 return jz4740_rtc_ctrl_set_bits(rtc
, JZ_RTC_CTRL_AF_IRQ
, enable
);
221 static const struct rtc_class_ops jz4740_rtc_ops
= {
222 .read_time
= jz4740_rtc_read_time
,
223 .set_time
= jz4740_rtc_set_time
,
224 .read_alarm
= jz4740_rtc_read_alarm
,
225 .set_alarm
= jz4740_rtc_set_alarm
,
226 .alarm_irq_enable
= jz4740_rtc_alarm_irq_enable
,
229 static irqreturn_t
jz4740_rtc_irq(int irq
, void *data
)
231 struct jz4740_rtc
*rtc
= data
;
233 unsigned long events
= 0;
235 ctrl
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CTRL
);
237 if (ctrl
& JZ_RTC_CTRL_1HZ
)
238 events
|= (RTC_UF
| RTC_IRQF
);
240 if (ctrl
& JZ_RTC_CTRL_AF
)
241 events
|= (RTC_AF
| RTC_IRQF
);
243 rtc_update_irq(rtc
->rtc
, 1, events
);
245 jz4740_rtc_ctrl_set_bits(rtc
, JZ_RTC_CTRL_1HZ
| JZ_RTC_CTRL_AF
, false);
250 static void jz4740_rtc_poweroff(struct device
*dev
)
252 struct jz4740_rtc
*rtc
= dev_get_drvdata(dev
);
253 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_HIBERNATE
, 1);
256 static void jz4740_rtc_power_off(void)
258 jz4740_rtc_poweroff(dev_for_power_off
);
262 static const struct of_device_id jz4740_rtc_of_match
[] = {
263 { .compatible
= "ingenic,jz4740-rtc", .data
= (void *)ID_JZ4740
},
264 { .compatible
= "ingenic,jz4760-rtc", .data
= (void *)ID_JZ4760
},
265 { .compatible
= "ingenic,jz4770-rtc", .data
= (void *)ID_JZ4780
},
266 { .compatible
= "ingenic,jz4780-rtc", .data
= (void *)ID_JZ4780
},
269 MODULE_DEVICE_TABLE(of
, jz4740_rtc_of_match
);
271 static void jz4740_rtc_set_wakeup_params(struct jz4740_rtc
*rtc
,
272 struct device_node
*np
,
275 unsigned long wakeup_ticks
, reset_ticks
;
276 unsigned int min_wakeup_pin_assert_time
= 60; /* Default: 60ms */
277 unsigned int reset_pin_assert_time
= 100; /* Default: 100ms */
279 of_property_read_u32(np
, "ingenic,reset-pin-assert-time-ms",
280 &reset_pin_assert_time
);
281 of_property_read_u32(np
, "ingenic,min-wakeup-pin-assert-time-ms",
282 &min_wakeup_pin_assert_time
);
285 * Set minimum wakeup pin assertion time: 100 ms.
286 * Range is 0 to 2 sec if RTC is clocked at 32 kHz.
288 wakeup_ticks
= (min_wakeup_pin_assert_time
* rate
) / 1000;
289 if (wakeup_ticks
< JZ_RTC_WAKEUP_FILTER_MASK
)
290 wakeup_ticks
&= JZ_RTC_WAKEUP_FILTER_MASK
;
292 wakeup_ticks
= JZ_RTC_WAKEUP_FILTER_MASK
;
293 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_WAKEUP_FILTER
, wakeup_ticks
);
296 * Set reset pin low-level assertion time after wakeup: 60 ms.
297 * Range is 0 to 125 ms if RTC is clocked at 32 kHz.
299 reset_ticks
= (reset_pin_assert_time
* rate
) / 1000;
300 if (reset_ticks
< JZ_RTC_RESET_COUNTER_MASK
)
301 reset_ticks
&= JZ_RTC_RESET_COUNTER_MASK
;
303 reset_ticks
= JZ_RTC_RESET_COUNTER_MASK
;
304 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_RESET_COUNTER
, reset_ticks
);
307 static int jz4740_rtc_clk32k_enable(struct clk_hw
*hw
)
309 struct jz4740_rtc
*rtc
= container_of(hw
, struct jz4740_rtc
, clk32k
);
311 return jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_CKPCR
,
312 JZ_RTC_CKPCR_CK32PULL_DIS
|
313 JZ_RTC_CKPCR_CK32CTL_EN
);
316 static void jz4740_rtc_clk32k_disable(struct clk_hw
*hw
)
318 struct jz4740_rtc
*rtc
= container_of(hw
, struct jz4740_rtc
, clk32k
);
320 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_CKPCR
, 0);
323 static int jz4740_rtc_clk32k_is_enabled(struct clk_hw
*hw
)
325 struct jz4740_rtc
*rtc
= container_of(hw
, struct jz4740_rtc
, clk32k
);
328 ckpcr
= jz4740_rtc_reg_read(rtc
, JZ_REG_RTC_CKPCR
);
330 return !!(ckpcr
& JZ_RTC_CKPCR_CK32CTL_EN
);
333 static const struct clk_ops jz4740_rtc_clk32k_ops
= {
334 .enable
= jz4740_rtc_clk32k_enable
,
335 .disable
= jz4740_rtc_clk32k_disable
,
336 .is_enabled
= jz4740_rtc_clk32k_is_enabled
,
339 static int jz4740_rtc_probe(struct platform_device
*pdev
)
341 struct device
*dev
= &pdev
->dev
;
342 struct device_node
*np
= dev
->of_node
;
343 struct jz4740_rtc
*rtc
;
348 rtc
= devm_kzalloc(dev
, sizeof(*rtc
), GFP_KERNEL
);
352 rtc
->type
= (uintptr_t)device_get_match_data(dev
);
354 irq
= platform_get_irq(pdev
, 0);
358 rtc
->base
= devm_platform_ioremap_resource(pdev
, 0);
359 if (IS_ERR(rtc
->base
))
360 return PTR_ERR(rtc
->base
);
362 clk
= devm_clk_get_enabled(dev
, "rtc");
364 return dev_err_probe(dev
, PTR_ERR(clk
), "Failed to get RTC clock\n");
366 spin_lock_init(&rtc
->lock
);
368 platform_set_drvdata(pdev
, rtc
);
370 device_init_wakeup(dev
, 1);
372 ret
= dev_pm_set_wake_irq(dev
, irq
);
374 return dev_err_probe(dev
, ret
, "Failed to set wake irq\n");
376 rtc
->rtc
= devm_rtc_allocate_device(dev
);
377 if (IS_ERR(rtc
->rtc
))
378 return dev_err_probe(dev
, PTR_ERR(rtc
->rtc
),
379 "Failed to allocate rtc device\n");
381 rtc
->rtc
->ops
= &jz4740_rtc_ops
;
382 rtc
->rtc
->range_max
= U32_MAX
;
384 rate
= clk_get_rate(clk
);
385 jz4740_rtc_set_wakeup_params(rtc
, np
, rate
);
387 /* Each 1 Hz pulse should happen after (rate) ticks */
388 jz4740_rtc_reg_write(rtc
, JZ_REG_RTC_REGULATOR
, rate
- 1);
390 ret
= devm_rtc_register_device(rtc
->rtc
);
394 ret
= devm_request_irq(dev
, irq
, jz4740_rtc_irq
, 0,
397 return dev_err_probe(dev
, ret
, "Failed to request rtc irq\n");
399 if (of_device_is_system_power_controller(np
)) {
400 dev_for_power_off
= dev
;
403 pm_power_off
= jz4740_rtc_power_off
;
405 dev_warn(dev
, "Poweroff handler already present!\n");
408 if (device_property_present(dev
, "#clock-cells")) {
409 rtc
->clk32k
.init
= CLK_HW_INIT_HW("clk32k", __clk_get_hw(clk
),
410 &jz4740_rtc_clk32k_ops
, 0);
412 ret
= devm_clk_hw_register(dev
, &rtc
->clk32k
);
414 return dev_err_probe(dev
, ret
,
415 "Unable to register clk32k clock\n");
417 ret
= devm_of_clk_add_hw_provider(dev
, of_clk_hw_simple_get
,
420 return dev_err_probe(dev
, ret
,
421 "Unable to register clk32k clock provider\n");
427 static struct platform_driver jz4740_rtc_driver
= {
428 .probe
= jz4740_rtc_probe
,
430 .name
= "jz4740-rtc",
431 .of_match_table
= jz4740_rtc_of_match
,
435 module_platform_driver(jz4740_rtc_driver
);
437 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
438 MODULE_LICENSE("GPL");
439 MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
440 MODULE_ALIAS("platform:jz4740-rtc");