blob: 60627a7645143721459cd78eb97f5addc885856e [file] [log] [blame]
Saeed Bisharadefb4512009-01-06 14:42:24 -08001/*
2 * Driver for the RTC in Marvell SoCs.
3 *
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.
7 */
8
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/rtc.h>
12#include <linux/bcd.h>
13#include <linux/io.h>
14#include <linux/platform_device.h>
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -040015#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Saeed Bisharadefb4512009-01-06 14:42:24 -080017
18
19#define RTC_TIME_REG_OFFS 0
20#define RTC_SECONDS_OFFS 0
21#define RTC_MINUTES_OFFS 8
22#define RTC_HOURS_OFFS 16
23#define RTC_WDAY_OFFS 24
24#define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
25
26#define RTC_DATE_REG_OFFS 4
27#define RTC_MDAY_OFFS 0
28#define RTC_MONTH_OFFS 8
29#define RTC_YEAR_OFFS 16
30
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080031#define RTC_ALARM_TIME_REG_OFFS 8
32#define RTC_ALARM_DATE_REG_OFFS 0xc
33#define RTC_ALARM_VALID (1 << 7)
34
35#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
36#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
Saeed Bisharadefb4512009-01-06 14:42:24 -080037
38struct rtc_plat_data {
39 struct rtc_device *rtc;
40 void __iomem *ioaddr;
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080041 int irq;
Saeed Bisharadefb4512009-01-06 14:42:24 -080042};
43
44static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
45{
46 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
47 void __iomem *ioaddr = pdata->ioaddr;
48 u32 rtc_reg;
49
50 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
51 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
52 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
53 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
54 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
55
56 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
57 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
58 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
59 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
60
61 return 0;
62}
63
64static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
65{
66 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
67 void __iomem *ioaddr = pdata->ioaddr;
68 u32 rtc_time, rtc_date;
69 unsigned int year, month, day, hour, minute, second, wday;
70
71 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
72 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
73
74 second = rtc_time & 0x7f;
75 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
76 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
77 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
78
79 day = rtc_date & 0x3f;
80 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
81 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
82
83 tm->tm_sec = bcd2bin(second);
84 tm->tm_min = bcd2bin(minute);
85 tm->tm_hour = bcd2bin(hour);
86 tm->tm_mday = bcd2bin(day);
87 tm->tm_wday = bcd2bin(wday);
88 tm->tm_mon = bcd2bin(month) - 1;
89 /* hw counts from year 2000, but tm_year is relative to 1900 */
90 tm->tm_year = bcd2bin(year) + 100;
91
92 return rtc_valid_tm(tm);
93}
94
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080095static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
96{
97 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
98 void __iomem *ioaddr = pdata->ioaddr;
99 u32 rtc_time, rtc_date;
100 unsigned int year, month, day, hour, minute, second, wday;
101
102 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
103 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
104
105 second = rtc_time & 0x7f;
106 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
107 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
108 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
109
110 day = rtc_date & 0x3f;
111 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
112 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
113
114 alm->time.tm_sec = bcd2bin(second);
115 alm->time.tm_min = bcd2bin(minute);
116 alm->time.tm_hour = bcd2bin(hour);
117 alm->time.tm_mday = bcd2bin(day);
118 alm->time.tm_wday = bcd2bin(wday);
119 alm->time.tm_mon = bcd2bin(month) - 1;
120 /* hw counts from year 2000, but tm_year is relative to 1900 */
121 alm->time.tm_year = bcd2bin(year) + 100;
122
123 if (rtc_valid_tm(&alm->time) < 0) {
124 dev_err(dev, "retrieved alarm date/time is not valid.\n");
125 rtc_time_to_tm(0, &alm->time);
126 }
127
128 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
129 return 0;
130}
131
132static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
133{
134 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
135 void __iomem *ioaddr = pdata->ioaddr;
136 u32 rtc_reg = 0;
137
138 if (alm->time.tm_sec >= 0)
139 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
140 << RTC_SECONDS_OFFS;
141 if (alm->time.tm_min >= 0)
142 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
143 << RTC_MINUTES_OFFS;
144 if (alm->time.tm_hour >= 0)
145 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
146 << RTC_HOURS_OFFS;
147
148 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
149
150 if (alm->time.tm_mday >= 0)
151 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
152 << RTC_MDAY_OFFS;
153 else
154 rtc_reg = 0;
155
156 if (alm->time.tm_mon >= 0)
157 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
158 << RTC_MONTH_OFFS;
159
160 if (alm->time.tm_year >= 0)
161 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
162 << RTC_YEAR_OFFS;
163
164 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
165 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
166 writel(alm->enabled ? 1 : 0,
167 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
168
169 return 0;
170}
171
John Stultz16380c12011-02-02 17:02:41 -0800172static int mv_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800173{
174 struct platform_device *pdev = to_platform_device(dev);
175 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
176 void __iomem *ioaddr = pdata->ioaddr;
177
178 if (pdata->irq < 0)
John Stultz16380c12011-02-02 17:02:41 -0800179 return -EINVAL; /* fall back into rtc-dev's emulation */
180
181 if (enabled)
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800182 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
John Stultz16380c12011-02-02 17:02:41 -0800183 else
184 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800185 return 0;
186}
187
188static irqreturn_t mv_rtc_interrupt(int irq, void *data)
189{
190 struct rtc_plat_data *pdata = data;
191 void __iomem *ioaddr = pdata->ioaddr;
192
193 /* alarm irq? */
194 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
195 return IRQ_NONE;
196
197 /* clear interrupt */
198 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
199 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
200 return IRQ_HANDLED;
201}
202
Saeed Bisharadefb4512009-01-06 14:42:24 -0800203static const struct rtc_class_ops mv_rtc_ops = {
204 .read_time = mv_rtc_read_time,
205 .set_time = mv_rtc_set_time,
206};
207
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800208static const struct rtc_class_ops mv_rtc_alarm_ops = {
209 .read_time = mv_rtc_read_time,
210 .set_time = mv_rtc_set_time,
211 .read_alarm = mv_rtc_read_alarm,
212 .set_alarm = mv_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800213 .alarm_irq_enable = mv_rtc_alarm_irq_enable,
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800214};
215
216static int __devinit mv_rtc_probe(struct platform_device *pdev)
Saeed Bisharadefb4512009-01-06 14:42:24 -0800217{
218 struct resource *res;
219 struct rtc_plat_data *pdata;
220 resource_size_t size;
221 u32 rtc_time;
222
223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
224 if (!res)
225 return -ENODEV;
226
227 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
228 if (!pdata)
229 return -ENOMEM;
230
231 size = resource_size(res);
232 if (!devm_request_mem_region(&pdev->dev, res->start, size,
233 pdev->name))
234 return -EBUSY;
235
236 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
237 if (!pdata->ioaddr)
238 return -ENOMEM;
239
240 /* make sure the 24 hours mode is enabled */
241 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
242 if (rtc_time & RTC_HOURS_12H_MODE) {
243 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
244 return -EINVAL;
245 }
246
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400247 /* make sure it is actually functional */
248 if (rtc_time == 0x01000000) {
249 ssleep(1);
250 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
251 if (rtc_time == 0x01000000) {
252 dev_err(&pdev->dev, "internal RTC not ticking\n");
253 return -ENODEV;
254 }
255 }
256
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800257 pdata->irq = platform_get_irq(pdev, 0);
258
Saeed Bisharadefb4512009-01-06 14:42:24 -0800259 platform_set_drvdata(pdev, pdata);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800260
261 if (pdata->irq >= 0) {
262 device_init_wakeup(&pdev->dev, 1);
263 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
264 &mv_rtc_alarm_ops,
265 THIS_MODULE);
266 } else
267 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
268 &mv_rtc_ops, THIS_MODULE);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800269 if (IS_ERR(pdata->rtc))
270 return PTR_ERR(pdata->rtc);
271
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800272 if (pdata->irq >= 0) {
273 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
274 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
275 IRQF_DISABLED | IRQF_SHARED,
276 pdev->name, pdata) < 0) {
277 dev_warn(&pdev->dev, "interrupt not available.\n");
278 pdata->irq = -1;
279 }
280 }
281
Saeed Bisharadefb4512009-01-06 14:42:24 -0800282 return 0;
283}
284
285static int __exit mv_rtc_remove(struct platform_device *pdev)
286{
287 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
288
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800289 if (pdata->irq >= 0)
290 device_init_wakeup(&pdev->dev, 0);
291
Saeed Bisharadefb4512009-01-06 14:42:24 -0800292 rtc_device_unregister(pdata->rtc);
293 return 0;
294}
295
296static struct platform_driver mv_rtc_driver = {
297 .remove = __exit_p(mv_rtc_remove),
298 .driver = {
299 .name = "rtc-mv",
300 .owner = THIS_MODULE,
301 },
302};
303
304static __init int mv_init(void)
305{
306 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
307}
308
309static __exit void mv_exit(void)
310{
311 platform_driver_unregister(&mv_rtc_driver);
312}
313
314module_init(mv_init);
315module_exit(mv_exit);
316
317MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
318MODULE_DESCRIPTION("Marvell RTC driver");
319MODULE_LICENSE("GPL");
320MODULE_ALIAS("platform:rtc-mv");