2 * Driver for the RTC in Marvell SoCs.
4 * This file is licensed under the terms of the GNU General Public
5 * License version 2. This program is licensed "as is" without any
6 * warranty of any kind, whether express or implied.
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/rtc.h>
12 #include <linux/bcd.h>
13 #include <linux/bitops.h>
15 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/clk.h>
19 #include <linux/gfp.h>
20 #include <linux/module.h>
23 #define RTC_TIME_REG_OFFS 0
24 #define RTC_SECONDS_OFFS 0
25 #define RTC_MINUTES_OFFS 8
26 #define RTC_HOURS_OFFS 16
27 #define RTC_WDAY_OFFS 24
28 #define RTC_HOURS_12H_MODE BIT(22) /* 12 hour mode */
30 #define RTC_DATE_REG_OFFS 4
31 #define RTC_MDAY_OFFS 0
32 #define RTC_MONTH_OFFS 8
33 #define RTC_YEAR_OFFS 16
35 #define RTC_ALARM_TIME_REG_OFFS 8
36 #define RTC_ALARM_DATE_REG_OFFS 0xc
37 #define RTC_ALARM_VALID BIT(7)
39 #define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
40 #define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
42 struct rtc_plat_data {
43 struct rtc_device *rtc;
49 static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
51 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
52 void __iomem *ioaddr = pdata->ioaddr;
55 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
56 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
57 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
58 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
59 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
61 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
62 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
63 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
64 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
69 static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
71 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
72 void __iomem *ioaddr = pdata->ioaddr;
73 u32 rtc_time, rtc_date;
74 unsigned int year, month, day, hour, minute, second, wday;
76 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
77 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
79 second = rtc_time & 0x7f;
80 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
81 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
82 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
84 day = rtc_date & 0x3f;
85 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
86 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
88 tm->tm_sec = bcd2bin(second);
89 tm->tm_min = bcd2bin(minute);
90 tm->tm_hour = bcd2bin(hour);
91 tm->tm_mday = bcd2bin(day);
92 tm->tm_wday = bcd2bin(wday);
93 tm->tm_mon = bcd2bin(month) - 1;
94 /* hw counts from year 2000, but tm_year is relative to 1900 */
95 tm->tm_year = bcd2bin(year) + 100;
100 static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
102 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
103 void __iomem *ioaddr = pdata->ioaddr;
104 u32 rtc_time, rtc_date;
105 unsigned int year, month, day, hour, minute, second, wday;
107 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
108 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
110 second = rtc_time & 0x7f;
111 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
112 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hour mode */
113 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
115 day = rtc_date & 0x3f;
116 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
117 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
119 alm->time.tm_sec = bcd2bin(second);
120 alm->time.tm_min = bcd2bin(minute);
121 alm->time.tm_hour = bcd2bin(hour);
122 alm->time.tm_mday = bcd2bin(day);
123 alm->time.tm_wday = bcd2bin(wday);
124 alm->time.tm_mon = bcd2bin(month) - 1;
125 /* hw counts from year 2000, but tm_year is relative to 1900 */
126 alm->time.tm_year = bcd2bin(year) + 100;
128 if (rtc_valid_tm(&alm->time) < 0) {
129 dev_err(dev, "retrieved alarm date/time is not valid.\n");
130 rtc_time_to_tm(0, &alm->time);
133 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
137 static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
139 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
140 void __iomem *ioaddr = pdata->ioaddr;
143 if (alm->time.tm_sec >= 0)
144 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
146 if (alm->time.tm_min >= 0)
147 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
149 if (alm->time.tm_hour >= 0)
150 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
153 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
155 if (alm->time.tm_mday >= 0)
156 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
161 if (alm->time.tm_mon >= 0)
162 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
165 if (alm->time.tm_year >= 0)
166 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
169 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
170 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
171 writel(alm->enabled ? 1 : 0,
172 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
177 static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
179 struct platform_device *pdev = to_platform_device(dev);
180 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
181 void __iomem *ioaddr = pdata->ioaddr;
184 return -EINVAL; /* fall back into rtc-dev's emulation */
187 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
189 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
193 static irqreturn_t mv_rtc_interrupt(int irq, void *data)
195 struct rtc_plat_data *pdata = data;
196 void __iomem *ioaddr = pdata->ioaddr;
199 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
202 /* clear interrupt */
203 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
204 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
208 static const struct rtc_class_ops mv_rtc_ops = {
209 .read_time = mv_rtc_read_time,
210 .set_time = mv_rtc_set_time,
213 static const struct rtc_class_ops mv_rtc_alarm_ops = {
214 .read_time = mv_rtc_read_time,
215 .set_time = mv_rtc_set_time,
216 .read_alarm = mv_rtc_read_alarm,
217 .set_alarm = mv_rtc_set_alarm,
218 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
221 static int __init mv_rtc_probe(struct platform_device *pdev)
223 struct resource *res;
224 struct rtc_plat_data *pdata;
228 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
232 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
233 pdata->ioaddr = devm_ioremap_resource(&pdev->dev, res);
234 if (IS_ERR(pdata->ioaddr))
235 return PTR_ERR(pdata->ioaddr);
237 pdata->clk = devm_clk_get(&pdev->dev, NULL);
238 /* Not all SoCs require a clock.*/
239 if (!IS_ERR(pdata->clk))
240 clk_prepare_enable(pdata->clk);
242 /* make sure the 24 hour mode is enabled */
243 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
244 if (rtc_time & RTC_HOURS_12H_MODE) {
245 dev_err(&pdev->dev, "12 Hour mode is enabled but not supported.\n");
250 /* make sure it is actually functional */
251 if (rtc_time == 0x01000000) {
253 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
254 if (rtc_time == 0x01000000) {
255 dev_err(&pdev->dev, "internal RTC not ticking\n");
261 pdata->irq = platform_get_irq(pdev, 0);
263 platform_set_drvdata(pdev, pdata);
265 if (pdata->irq >= 0) {
266 device_init_wakeup(&pdev->dev, 1);
267 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
271 pdata->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
272 &mv_rtc_ops, THIS_MODULE);
274 if (IS_ERR(pdata->rtc)) {
275 ret = PTR_ERR(pdata->rtc);
279 if (pdata->irq >= 0) {
280 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
281 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
283 pdev->name, pdata) < 0) {
284 dev_warn(&pdev->dev, "interrupt not available.\n");
291 if (!IS_ERR(pdata->clk))
292 clk_disable_unprepare(pdata->clk);
297 static int __exit mv_rtc_remove(struct platform_device *pdev)
299 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
302 device_init_wakeup(&pdev->dev, 0);
304 if (!IS_ERR(pdata->clk))
305 clk_disable_unprepare(pdata->clk);
311 static const struct of_device_id rtc_mv_of_match_table[] = {
312 { .compatible = "marvell,orion-rtc", },
315 MODULE_DEVICE_TABLE(of, rtc_mv_of_match_table);
318 static struct platform_driver mv_rtc_driver = {
319 .remove = __exit_p(mv_rtc_remove),
322 .of_match_table = of_match_ptr(rtc_mv_of_match_table),
326 module_platform_driver_probe(mv_rtc_driver, mv_rtc_probe);
329 MODULE_DESCRIPTION("Marvell RTC driver");
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS("platform:rtc-mv");