blob: dc052ce6e63a8af6525cd067d0748927298c0c41 [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>
Saeed Bisharadefb4512009-01-06 14:42:24 -080016
17
18#define RTC_TIME_REG_OFFS 0
19#define RTC_SECONDS_OFFS 0
20#define RTC_MINUTES_OFFS 8
21#define RTC_HOURS_OFFS 16
22#define RTC_WDAY_OFFS 24
23#define RTC_HOURS_12H_MODE (1 << 22) /* 12 hours mode */
24
25#define RTC_DATE_REG_OFFS 4
26#define RTC_MDAY_OFFS 0
27#define RTC_MONTH_OFFS 8
28#define RTC_YEAR_OFFS 16
29
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080030#define RTC_ALARM_TIME_REG_OFFS 8
31#define RTC_ALARM_DATE_REG_OFFS 0xc
32#define RTC_ALARM_VALID (1 << 7)
33
34#define RTC_ALARM_INTERRUPT_MASK_REG_OFFS 0x10
35#define RTC_ALARM_INTERRUPT_CASUE_REG_OFFS 0x14
Saeed Bisharadefb4512009-01-06 14:42:24 -080036
37struct rtc_plat_data {
38 struct rtc_device *rtc;
39 void __iomem *ioaddr;
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080040 int irq;
Saeed Bisharadefb4512009-01-06 14:42:24 -080041};
42
43static int mv_rtc_set_time(struct device *dev, struct rtc_time *tm)
44{
45 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
46 void __iomem *ioaddr = pdata->ioaddr;
47 u32 rtc_reg;
48
49 rtc_reg = (bin2bcd(tm->tm_sec) << RTC_SECONDS_OFFS) |
50 (bin2bcd(tm->tm_min) << RTC_MINUTES_OFFS) |
51 (bin2bcd(tm->tm_hour) << RTC_HOURS_OFFS) |
52 (bin2bcd(tm->tm_wday) << RTC_WDAY_OFFS);
53 writel(rtc_reg, ioaddr + RTC_TIME_REG_OFFS);
54
55 rtc_reg = (bin2bcd(tm->tm_mday) << RTC_MDAY_OFFS) |
56 (bin2bcd(tm->tm_mon + 1) << RTC_MONTH_OFFS) |
57 (bin2bcd(tm->tm_year % 100) << RTC_YEAR_OFFS);
58 writel(rtc_reg, ioaddr + RTC_DATE_REG_OFFS);
59
60 return 0;
61}
62
63static int mv_rtc_read_time(struct device *dev, struct rtc_time *tm)
64{
65 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
66 void __iomem *ioaddr = pdata->ioaddr;
67 u32 rtc_time, rtc_date;
68 unsigned int year, month, day, hour, minute, second, wday;
69
70 rtc_time = readl(ioaddr + RTC_TIME_REG_OFFS);
71 rtc_date = readl(ioaddr + RTC_DATE_REG_OFFS);
72
73 second = rtc_time & 0x7f;
74 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
75 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
76 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
77
78 day = rtc_date & 0x3f;
79 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
80 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
81
82 tm->tm_sec = bcd2bin(second);
83 tm->tm_min = bcd2bin(minute);
84 tm->tm_hour = bcd2bin(hour);
85 tm->tm_mday = bcd2bin(day);
86 tm->tm_wday = bcd2bin(wday);
87 tm->tm_mon = bcd2bin(month) - 1;
88 /* hw counts from year 2000, but tm_year is relative to 1900 */
89 tm->tm_year = bcd2bin(year) + 100;
90
91 return rtc_valid_tm(tm);
92}
93
Saeed Bisharaaeedaca2009-12-15 16:46:08 -080094static int mv_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
95{
96 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
97 void __iomem *ioaddr = pdata->ioaddr;
98 u32 rtc_time, rtc_date;
99 unsigned int year, month, day, hour, minute, second, wday;
100
101 rtc_time = readl(ioaddr + RTC_ALARM_TIME_REG_OFFS);
102 rtc_date = readl(ioaddr + RTC_ALARM_DATE_REG_OFFS);
103
104 second = rtc_time & 0x7f;
105 minute = (rtc_time >> RTC_MINUTES_OFFS) & 0x7f;
106 hour = (rtc_time >> RTC_HOURS_OFFS) & 0x3f; /* assume 24 hours mode */
107 wday = (rtc_time >> RTC_WDAY_OFFS) & 0x7;
108
109 day = rtc_date & 0x3f;
110 month = (rtc_date >> RTC_MONTH_OFFS) & 0x3f;
111 year = (rtc_date >> RTC_YEAR_OFFS) & 0xff;
112
113 alm->time.tm_sec = bcd2bin(second);
114 alm->time.tm_min = bcd2bin(minute);
115 alm->time.tm_hour = bcd2bin(hour);
116 alm->time.tm_mday = bcd2bin(day);
117 alm->time.tm_wday = bcd2bin(wday);
118 alm->time.tm_mon = bcd2bin(month) - 1;
119 /* hw counts from year 2000, but tm_year is relative to 1900 */
120 alm->time.tm_year = bcd2bin(year) + 100;
121
122 if (rtc_valid_tm(&alm->time) < 0) {
123 dev_err(dev, "retrieved alarm date/time is not valid.\n");
124 rtc_time_to_tm(0, &alm->time);
125 }
126
127 alm->enabled = !!readl(ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
128 return 0;
129}
130
131static int mv_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
132{
133 struct rtc_plat_data *pdata = dev_get_drvdata(dev);
134 void __iomem *ioaddr = pdata->ioaddr;
135 u32 rtc_reg = 0;
136
137 if (alm->time.tm_sec >= 0)
138 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_sec))
139 << RTC_SECONDS_OFFS;
140 if (alm->time.tm_min >= 0)
141 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_min))
142 << RTC_MINUTES_OFFS;
143 if (alm->time.tm_hour >= 0)
144 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_hour))
145 << RTC_HOURS_OFFS;
146
147 writel(rtc_reg, ioaddr + RTC_ALARM_TIME_REG_OFFS);
148
149 if (alm->time.tm_mday >= 0)
150 rtc_reg = (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mday))
151 << RTC_MDAY_OFFS;
152 else
153 rtc_reg = 0;
154
155 if (alm->time.tm_mon >= 0)
156 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_mon + 1))
157 << RTC_MONTH_OFFS;
158
159 if (alm->time.tm_year >= 0)
160 rtc_reg |= (RTC_ALARM_VALID | bin2bcd(alm->time.tm_year % 100))
161 << RTC_YEAR_OFFS;
162
163 writel(rtc_reg, ioaddr + RTC_ALARM_DATE_REG_OFFS);
164 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
165 writel(alm->enabled ? 1 : 0,
166 ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
167
168 return 0;
169}
170
171static int mv_rtc_ioctl(struct device *dev, unsigned int cmd,
172 unsigned long arg)
173{
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)
179 return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
180 switch (cmd) {
181 case RTC_AIE_OFF:
182 writel(0, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
183 break;
184 case RTC_AIE_ON:
185 writel(1, ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
186 break;
187 default:
188 return -ENOIOCTLCMD;
189 }
190 return 0;
191}
192
193static irqreturn_t mv_rtc_interrupt(int irq, void *data)
194{
195 struct rtc_plat_data *pdata = data;
196 void __iomem *ioaddr = pdata->ioaddr;
197
198 /* alarm irq? */
199 if (!readl(ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS))
200 return IRQ_NONE;
201
202 /* clear interrupt */
203 writel(0, ioaddr + RTC_ALARM_INTERRUPT_CASUE_REG_OFFS);
204 rtc_update_irq(pdata->rtc, 1, RTC_IRQF | RTC_AF);
205 return IRQ_HANDLED;
206}
207
Saeed Bisharadefb4512009-01-06 14:42:24 -0800208static const struct rtc_class_ops mv_rtc_ops = {
209 .read_time = mv_rtc_read_time,
210 .set_time = mv_rtc_set_time,
211};
212
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800213static 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 .ioctl = mv_rtc_ioctl,
219};
220
221static int __devinit mv_rtc_probe(struct platform_device *pdev)
Saeed Bisharadefb4512009-01-06 14:42:24 -0800222{
223 struct resource *res;
224 struct rtc_plat_data *pdata;
225 resource_size_t size;
226 u32 rtc_time;
227
228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 if (!res)
230 return -ENODEV;
231
232 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
233 if (!pdata)
234 return -ENOMEM;
235
236 size = resource_size(res);
237 if (!devm_request_mem_region(&pdev->dev, res->start, size,
238 pdev->name))
239 return -EBUSY;
240
241 pdata->ioaddr = devm_ioremap(&pdev->dev, res->start, size);
242 if (!pdata->ioaddr)
243 return -ENOMEM;
244
245 /* make sure the 24 hours mode is enabled */
246 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
247 if (rtc_time & RTC_HOURS_12H_MODE) {
248 dev_err(&pdev->dev, "24 Hours mode not supported.\n");
249 return -EINVAL;
250 }
251
Nicolas Pitre9d1d4f92009-03-23 20:42:29 -0400252 /* make sure it is actually functional */
253 if (rtc_time == 0x01000000) {
254 ssleep(1);
255 rtc_time = readl(pdata->ioaddr + RTC_TIME_REG_OFFS);
256 if (rtc_time == 0x01000000) {
257 dev_err(&pdev->dev, "internal RTC not ticking\n");
258 return -ENODEV;
259 }
260 }
261
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800262 pdata->irq = platform_get_irq(pdev, 0);
263
Saeed Bisharadefb4512009-01-06 14:42:24 -0800264 platform_set_drvdata(pdev, pdata);
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800265
266 if (pdata->irq >= 0) {
267 device_init_wakeup(&pdev->dev, 1);
268 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
269 &mv_rtc_alarm_ops,
270 THIS_MODULE);
271 } else
272 pdata->rtc = rtc_device_register(pdev->name, &pdev->dev,
273 &mv_rtc_ops, THIS_MODULE);
Saeed Bisharadefb4512009-01-06 14:42:24 -0800274 if (IS_ERR(pdata->rtc))
275 return PTR_ERR(pdata->rtc);
276
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800277 if (pdata->irq >= 0) {
278 writel(0, pdata->ioaddr + RTC_ALARM_INTERRUPT_MASK_REG_OFFS);
279 if (devm_request_irq(&pdev->dev, pdata->irq, mv_rtc_interrupt,
280 IRQF_DISABLED | IRQF_SHARED,
281 pdev->name, pdata) < 0) {
282 dev_warn(&pdev->dev, "interrupt not available.\n");
283 pdata->irq = -1;
284 }
285 }
286
Saeed Bisharadefb4512009-01-06 14:42:24 -0800287 return 0;
288}
289
290static int __exit mv_rtc_remove(struct platform_device *pdev)
291{
292 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
293
Saeed Bisharaaeedaca2009-12-15 16:46:08 -0800294 if (pdata->irq >= 0)
295 device_init_wakeup(&pdev->dev, 0);
296
Saeed Bisharadefb4512009-01-06 14:42:24 -0800297 rtc_device_unregister(pdata->rtc);
298 return 0;
299}
300
301static struct platform_driver mv_rtc_driver = {
302 .remove = __exit_p(mv_rtc_remove),
303 .driver = {
304 .name = "rtc-mv",
305 .owner = THIS_MODULE,
306 },
307};
308
309static __init int mv_init(void)
310{
311 return platform_driver_probe(&mv_rtc_driver, mv_rtc_probe);
312}
313
314static __exit void mv_exit(void)
315{
316 platform_driver_unregister(&mv_rtc_driver);
317}
318
319module_init(mv_init);
320module_exit(mv_exit);
321
322MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
323MODULE_DESCRIPTION("Marvell RTC driver");
324MODULE_LICENSE("GPL");
325MODULE_ALIAS("platform:rtc-mv");