blob: 575fbbf966db21798e82f72e087bc60f44e22e37 [file] [log] [blame]
Deepak Saxena8ae6e162006-06-25 05:47:38 -07001/*
2 * drivers/rtc/rtc-pl031.c
3 *
4 * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC
5 *
6 * Author: Deepak Saxena <dsaxena@plexity.net>
7 *
8 * Copyright 2006 (c) MontaVista Software, Inc.
9 *
Linus Walleijc72881e2010-02-04 12:50:13 +010010 * Author: Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
11 * Copyright 2010 (c) ST-Ericsson AB
12 *
Deepak Saxena8ae6e162006-06-25 05:47:38 -070013 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 */
Deepak Saxena8ae6e162006-06-25 05:47:38 -070018#include <linux/module.h>
19#include <linux/rtc.h>
20#include <linux/init.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070021#include <linux/interrupt.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070022#include <linux/amba/bus.h>
Russell King2dba8512008-04-20 12:08:04 +010023#include <linux/io.h>
Linus Walleijc72881e2010-02-04 12:50:13 +010024#include <linux/bcd.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Deepak Saxena8ae6e162006-06-25 05:47:38 -070027
28/*
29 * Register definitions
30 */
31#define RTC_DR 0x00 /* Data read register */
32#define RTC_MR 0x04 /* Match register */
33#define RTC_LR 0x08 /* Data load register */
34#define RTC_CR 0x0c /* Control register */
35#define RTC_IMSC 0x10 /* Interrupt mask and set register */
36#define RTC_RIS 0x14 /* Raw interrupt status register */
37#define RTC_MIS 0x18 /* Masked interrupt status register */
38#define RTC_ICR 0x1c /* Interrupt clear register */
Linus Walleijc72881e2010-02-04 12:50:13 +010039/* ST variants have additional timer functionality */
40#define RTC_TDR 0x20 /* Timer data read register */
41#define RTC_TLR 0x24 /* Timer data load register */
42#define RTC_TCR 0x28 /* Timer control register */
43#define RTC_YDR 0x30 /* Year data read register */
44#define RTC_YMR 0x34 /* Year match register */
45#define RTC_YLR 0x38 /* Year data load register */
46
47#define RTC_CR_CWEN (1 << 26) /* Clockwatch enable bit */
48
49#define RTC_TCR_EN (1 << 1) /* Periodic timer enable bit */
50
51/* Common bit definitions for Interrupt status and control registers */
52#define RTC_BIT_AI (1 << 0) /* Alarm interrupt bit */
53#define RTC_BIT_PI (1 << 1) /* Periodic interrupt bit. ST variants only. */
54
55/* Common bit definations for ST v2 for reading/writing time */
56#define RTC_SEC_SHIFT 0
57#define RTC_SEC_MASK (0x3F << RTC_SEC_SHIFT) /* Second [0-59] */
58#define RTC_MIN_SHIFT 6
59#define RTC_MIN_MASK (0x3F << RTC_MIN_SHIFT) /* Minute [0-59] */
60#define RTC_HOUR_SHIFT 12
61#define RTC_HOUR_MASK (0x1F << RTC_HOUR_SHIFT) /* Hour [0-23] */
62#define RTC_WDAY_SHIFT 17
63#define RTC_WDAY_MASK (0x7 << RTC_WDAY_SHIFT) /* Day of Week [1-7] 1=Sunday */
64#define RTC_MDAY_SHIFT 20
65#define RTC_MDAY_MASK (0x1F << RTC_MDAY_SHIFT) /* Day of Month [1-31] */
66#define RTC_MON_SHIFT 25
67#define RTC_MON_MASK (0xF << RTC_MON_SHIFT) /* Month [1-12] 1=January */
68
69#define RTC_TIMER_FREQ 32768
Deepak Saxena8ae6e162006-06-25 05:47:38 -070070
Linus Walleijaff05ed2012-07-30 14:41:34 -070071/**
72 * struct pl031_vendor_data - per-vendor variations
73 * @ops: the vendor-specific operations used on this silicon version
74 */
75struct pl031_vendor_data {
76 struct rtc_class_ops ops;
77};
78
Deepak Saxena8ae6e162006-06-25 05:47:38 -070079struct pl031_local {
Linus Walleijaff05ed2012-07-30 14:41:34 -070080 struct pl031_vendor_data *vendor;
Deepak Saxena8ae6e162006-06-25 05:47:38 -070081 struct rtc_device *rtc;
82 void __iomem *base;
Linus Walleijc72881e2010-02-04 12:50:13 +010083 u8 hw_designer;
84 u8 hw_revision:4;
Deepak Saxena8ae6e162006-06-25 05:47:38 -070085};
86
Linus Walleijc72881e2010-02-04 12:50:13 +010087static int pl031_alarm_irq_enable(struct device *dev,
88 unsigned int enabled)
Deepak Saxena8ae6e162006-06-25 05:47:38 -070089{
Linus Walleijc72881e2010-02-04 12:50:13 +010090 struct pl031_local *ldata = dev_get_drvdata(dev);
91 unsigned long imsc;
Deepak Saxena8ae6e162006-06-25 05:47:38 -070092
Linus Walleijc72881e2010-02-04 12:50:13 +010093 /* Clear any pending alarm interrupts. */
94 writel(RTC_BIT_AI, ldata->base + RTC_ICR);
Deepak Saxena8ae6e162006-06-25 05:47:38 -070095
Linus Walleijc72881e2010-02-04 12:50:13 +010096 imsc = readl(ldata->base + RTC_IMSC);
97
98 if (enabled == 1)
99 writel(imsc | RTC_BIT_AI, ldata->base + RTC_IMSC);
100 else
101 writel(imsc & ~RTC_BIT_AI, ldata->base + RTC_IMSC);
102
103 return 0;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700104}
105
Linus Walleijc72881e2010-02-04 12:50:13 +0100106/*
107 * Convert Gregorian date to ST v2 RTC format.
108 */
109static int pl031_stv2_tm_to_time(struct device *dev,
110 struct rtc_time *tm, unsigned long *st_time,
111 unsigned long *bcd_year)
112{
113 int year = tm->tm_year + 1900;
114 int wday = tm->tm_wday;
115
116 /* wday masking is not working in hardware so wday must be valid */
117 if (wday < -1 || wday > 6) {
118 dev_err(dev, "invalid wday value %d\n", tm->tm_wday);
119 return -EINVAL;
120 } else if (wday == -1) {
121 /* wday is not provided, calculate it here */
122 unsigned long time;
123 struct rtc_time calc_tm;
124
125 rtc_tm_to_time(tm, &time);
126 rtc_time_to_tm(time, &calc_tm);
127 wday = calc_tm.tm_wday;
128 }
129
130 *bcd_year = (bin2bcd(year % 100) | bin2bcd(year / 100) << 8);
131
132 *st_time = ((tm->tm_mon + 1) << RTC_MON_SHIFT)
133 | (tm->tm_mday << RTC_MDAY_SHIFT)
134 | ((wday + 1) << RTC_WDAY_SHIFT)
135 | (tm->tm_hour << RTC_HOUR_SHIFT)
136 | (tm->tm_min << RTC_MIN_SHIFT)
137 | (tm->tm_sec << RTC_SEC_SHIFT);
138
139 return 0;
140}
141
142/*
143 * Convert ST v2 RTC format to Gregorian date.
144 */
145static int pl031_stv2_time_to_tm(unsigned long st_time, unsigned long bcd_year,
146 struct rtc_time *tm)
147{
148 tm->tm_year = bcd2bin(bcd_year) + (bcd2bin(bcd_year >> 8) * 100);
149 tm->tm_mon = ((st_time & RTC_MON_MASK) >> RTC_MON_SHIFT) - 1;
150 tm->tm_mday = ((st_time & RTC_MDAY_MASK) >> RTC_MDAY_SHIFT);
151 tm->tm_wday = ((st_time & RTC_WDAY_MASK) >> RTC_WDAY_SHIFT) - 1;
152 tm->tm_hour = ((st_time & RTC_HOUR_MASK) >> RTC_HOUR_SHIFT);
153 tm->tm_min = ((st_time & RTC_MIN_MASK) >> RTC_MIN_SHIFT);
154 tm->tm_sec = ((st_time & RTC_SEC_MASK) >> RTC_SEC_SHIFT);
155
156 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
157 tm->tm_year -= 1900;
158
159 return 0;
160}
161
162static int pl031_stv2_read_time(struct device *dev, struct rtc_time *tm)
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700163{
164 struct pl031_local *ldata = dev_get_drvdata(dev);
165
Linus Walleijc72881e2010-02-04 12:50:13 +0100166 pl031_stv2_time_to_tm(readl(ldata->base + RTC_DR),
167 readl(ldata->base + RTC_YDR), tm);
168
169 return 0;
170}
171
172static int pl031_stv2_set_time(struct device *dev, struct rtc_time *tm)
173{
174 unsigned long time;
175 unsigned long bcd_year;
176 struct pl031_local *ldata = dev_get_drvdata(dev);
177 int ret;
178
179 ret = pl031_stv2_tm_to_time(dev, tm, &time, &bcd_year);
180 if (ret == 0) {
181 writel(bcd_year, ldata->base + RTC_YLR);
182 writel(time, ldata->base + RTC_LR);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700183 }
184
Linus Walleijc72881e2010-02-04 12:50:13 +0100185 return ret;
186}
187
188static int pl031_stv2_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
189{
190 struct pl031_local *ldata = dev_get_drvdata(dev);
191 int ret;
192
193 ret = pl031_stv2_time_to_tm(readl(ldata->base + RTC_MR),
194 readl(ldata->base + RTC_YMR), &alarm->time);
195
196 alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
197 alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
198
199 return ret;
200}
201
202static int pl031_stv2_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
203{
204 struct pl031_local *ldata = dev_get_drvdata(dev);
205 unsigned long time;
206 unsigned long bcd_year;
207 int ret;
208
209 /* At the moment, we can only deal with non-wildcarded alarm times. */
210 ret = rtc_valid_tm(&alarm->time);
211 if (ret == 0) {
212 ret = pl031_stv2_tm_to_time(dev, &alarm->time,
213 &time, &bcd_year);
214 if (ret == 0) {
215 writel(bcd_year, ldata->base + RTC_YMR);
216 writel(time, ldata->base + RTC_MR);
217
218 pl031_alarm_irq_enable(dev, alarm->enabled);
219 }
220 }
221
222 return ret;
223}
224
225static irqreturn_t pl031_interrupt(int irq, void *dev_id)
226{
227 struct pl031_local *ldata = dev_id;
228 unsigned long rtcmis;
229 unsigned long events = 0;
230
231 rtcmis = readl(ldata->base + RTC_MIS);
Rajkumar Kasirajanac2dee52012-05-29 15:07:40 -0700232 if (rtcmis & RTC_BIT_AI) {
233 writel(RTC_BIT_AI, ldata->base + RTC_ICR);
234 events |= (RTC_AF | RTC_IRQF);
Linus Walleijc72881e2010-02-04 12:50:13 +0100235 rtc_update_irq(ldata->rtc, 1, events);
236
237 return IRQ_HANDLED;
238 }
239
240 return IRQ_NONE;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700241}
242
243static int pl031_read_time(struct device *dev, struct rtc_time *tm)
244{
245 struct pl031_local *ldata = dev_get_drvdata(dev);
246
Linus Walleij2934d6a2009-12-15 16:46:13 -0800247 rtc_time_to_tm(readl(ldata->base + RTC_DR), tm);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700248
249 return 0;
250}
251
252static int pl031_set_time(struct device *dev, struct rtc_time *tm)
253{
254 unsigned long time;
255 struct pl031_local *ldata = dev_get_drvdata(dev);
Linus Walleijc72881e2010-02-04 12:50:13 +0100256 int ret;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700257
Linus Walleijc72881e2010-02-04 12:50:13 +0100258 ret = rtc_tm_to_time(tm, &time);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700259
Linus Walleijc72881e2010-02-04 12:50:13 +0100260 if (ret == 0)
261 writel(time, ldata->base + RTC_LR);
262
263 return ret;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700264}
265
266static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
267{
268 struct pl031_local *ldata = dev_get_drvdata(dev);
269
Linus Walleij2934d6a2009-12-15 16:46:13 -0800270 rtc_time_to_tm(readl(ldata->base + RTC_MR), &alarm->time);
Linus Walleijc72881e2010-02-04 12:50:13 +0100271
272 alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
273 alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700274
275 return 0;
276}
277
278static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
279{
280 struct pl031_local *ldata = dev_get_drvdata(dev);
281 unsigned long time;
Linus Walleijc72881e2010-02-04 12:50:13 +0100282 int ret;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700283
Linus Walleijc72881e2010-02-04 12:50:13 +0100284 /* At the moment, we can only deal with non-wildcarded alarm times. */
285 ret = rtc_valid_tm(&alarm->time);
286 if (ret == 0) {
287 ret = rtc_tm_to_time(&alarm->time, &time);
288 if (ret == 0) {
289 writel(time, ldata->base + RTC_MR);
290 pl031_alarm_irq_enable(dev, alarm->enabled);
291 }
292 }
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700293
Linus Walleijc72881e2010-02-04 12:50:13 +0100294 return ret;
295}
296
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700297static int pl031_remove(struct amba_device *adev)
298{
299 struct pl031_local *ldata = dev_get_drvdata(&adev->dev);
300
Russell King2dba8512008-04-20 12:08:04 +0100301 amba_set_drvdata(adev, NULL);
302 free_irq(adev->irq[0], ldata->rtc);
303 rtc_device_unregister(ldata->rtc);
304 iounmap(ldata->base);
305 kfree(ldata);
306 amba_release_regions(adev);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700307
308 return 0;
309}
310
Russell Kingaa25afa2011-02-19 15:55:00 +0000311static int pl031_probe(struct amba_device *adev, const struct amba_id *id)
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700312{
313 int ret;
314 struct pl031_local *ldata;
Linus Walleijaff05ed2012-07-30 14:41:34 -0700315 struct pl031_vendor_data *vendor = id->data;
316 struct rtc_class_ops *ops = &vendor->ops;
Rajkumar Kasirajanc0a5f4a2012-05-17 17:03:24 -0700317 unsigned long time;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700318
Russell King2dba8512008-04-20 12:08:04 +0100319 ret = amba_request_regions(adev, NULL);
320 if (ret)
321 goto err_req;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700322
Linus Walleijc72881e2010-02-04 12:50:13 +0100323 ldata = kzalloc(sizeof(struct pl031_local), GFP_KERNEL);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700324 if (!ldata) {
325 ret = -ENOMEM;
326 goto out;
327 }
Linus Walleijaff05ed2012-07-30 14:41:34 -0700328 ldata->vendor = vendor;
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700329
Linus Walleijdc890c22009-06-07 23:27:31 +0100330 ldata->base = ioremap(adev->res.start, resource_size(&adev->res));
Linus Walleijc72881e2010-02-04 12:50:13 +0100331
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700332 if (!ldata->base) {
333 ret = -ENOMEM;
334 goto out_no_remap;
335 }
336
Russell King2dba8512008-04-20 12:08:04 +0100337 amba_set_drvdata(adev, ldata);
338
Linus Walleijc72881e2010-02-04 12:50:13 +0100339 ldata->hw_designer = amba_manf(adev);
340 ldata->hw_revision = amba_rev(adev);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700341
Linus Walleijc72881e2010-02-04 12:50:13 +0100342 dev_dbg(&adev->dev, "designer ID = 0x%02x\n", ldata->hw_designer);
343 dev_dbg(&adev->dev, "revision = 0x%01x\n", ldata->hw_revision);
344
345 /* Enable the clockwatch on ST Variants */
Linus Walleij2f397212012-04-12 12:49:16 -0700346 if (ldata->hw_designer == AMBA_VENDOR_ST)
Linus Walleijc72881e2010-02-04 12:50:13 +0100347 writel(readl(ldata->base + RTC_CR) | RTC_CR_CWEN,
348 ldata->base + RTC_CR);
349
Rajkumar Kasirajanc0a5f4a2012-05-17 17:03:24 -0700350 /*
351 * On ST PL031 variants, the RTC reset value does not provide correct
352 * weekday for 2000-01-01. Correct the erroneous sunday to saturday.
353 */
354 if (ldata->hw_designer == AMBA_VENDOR_ST) {
355 if (readl(ldata->base + RTC_YDR) == 0x2000) {
356 time = readl(ldata->base + RTC_DR);
357 if ((time &
358 (RTC_MON_MASK | RTC_MDAY_MASK | RTC_WDAY_MASK))
359 == 0x02120000) {
360 time = time | (0x7 << RTC_WDAY_SHIFT);
361 writel(0x2000, ldata->base + RTC_YLR);
362 writel(time, ldata->base + RTC_LR);
363 }
364 }
365 }
366
Linus Walleijc72881e2010-02-04 12:50:13 +0100367 ldata->rtc = rtc_device_register("pl031", &adev->dev, ops,
368 THIS_MODULE);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700369 if (IS_ERR(ldata->rtc)) {
370 ret = PTR_ERR(ldata->rtc);
371 goto out_no_rtc;
372 }
373
Linus Walleijc72881e2010-02-04 12:50:13 +0100374 if (request_irq(adev->irq[0], pl031_interrupt,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700375 0, "rtc-pl031", ldata)) {
Linus Walleijc72881e2010-02-04 12:50:13 +0100376 ret = -EIO;
377 goto out_no_irq;
378 }
379
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700380 return 0;
381
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700382out_no_irq:
Linus Walleijc72881e2010-02-04 12:50:13 +0100383 rtc_device_unregister(ldata->rtc);
384out_no_rtc:
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700385 iounmap(ldata->base);
Russell King2dba8512008-04-20 12:08:04 +0100386 amba_set_drvdata(adev, NULL);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700387out_no_remap:
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700388 kfree(ldata);
389out:
Russell King2dba8512008-04-20 12:08:04 +0100390 amba_release_regions(adev);
391err_req:
Linus Walleijc72881e2010-02-04 12:50:13 +0100392
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700393 return ret;
394}
395
Linus Walleijc72881e2010-02-04 12:50:13 +0100396/* Operations for the original ARM version */
Linus Walleijaff05ed2012-07-30 14:41:34 -0700397static struct pl031_vendor_data arm_pl031 = {
398 .ops = {
399 .read_time = pl031_read_time,
400 .set_time = pl031_set_time,
401 .read_alarm = pl031_read_alarm,
402 .set_alarm = pl031_set_alarm,
403 .alarm_irq_enable = pl031_alarm_irq_enable,
404 },
Linus Walleijc72881e2010-02-04 12:50:13 +0100405};
406
407/* The First ST derivative */
Linus Walleijaff05ed2012-07-30 14:41:34 -0700408static struct pl031_vendor_data stv1_pl031 = {
409 .ops = {
410 .read_time = pl031_read_time,
411 .set_time = pl031_set_time,
412 .read_alarm = pl031_read_alarm,
413 .set_alarm = pl031_set_alarm,
414 .alarm_irq_enable = pl031_alarm_irq_enable,
415 },
Linus Walleijc72881e2010-02-04 12:50:13 +0100416};
417
418/* And the second ST derivative */
Linus Walleijaff05ed2012-07-30 14:41:34 -0700419static struct pl031_vendor_data stv2_pl031 = {
420 .ops = {
421 .read_time = pl031_stv2_read_time,
422 .set_time = pl031_stv2_set_time,
423 .read_alarm = pl031_stv2_read_alarm,
424 .set_alarm = pl031_stv2_set_alarm,
425 .alarm_irq_enable = pl031_alarm_irq_enable,
426 },
Linus Walleijc72881e2010-02-04 12:50:13 +0100427};
428
Russell King2c39c9e2010-07-27 08:50:16 +0100429static struct amba_id pl031_ids[] = {
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700430 {
Linus Walleij2934d6a2009-12-15 16:46:13 -0800431 .id = 0x00041031,
432 .mask = 0x000fffff,
Linus Walleijaff05ed2012-07-30 14:41:34 -0700433 .data = &arm_pl031,
Linus Walleijc72881e2010-02-04 12:50:13 +0100434 },
435 /* ST Micro variants */
436 {
437 .id = 0x00180031,
438 .mask = 0x00ffffff,
Linus Walleijaff05ed2012-07-30 14:41:34 -0700439 .data = &stv1_pl031,
Linus Walleijc72881e2010-02-04 12:50:13 +0100440 },
441 {
442 .id = 0x00280031,
443 .mask = 0x00ffffff,
Linus Walleijaff05ed2012-07-30 14:41:34 -0700444 .data = &stv2_pl031,
Linus Walleij2934d6a2009-12-15 16:46:13 -0800445 },
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700446 {0, 0},
447};
448
Dave Martinf5feac22011-10-05 15:15:22 +0100449MODULE_DEVICE_TABLE(amba, pl031_ids);
450
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700451static struct amba_driver pl031_driver = {
452 .drv = {
453 .name = "rtc-pl031",
454 },
455 .id_table = pl031_ids,
456 .probe = pl031_probe,
457 .remove = pl031_remove,
458};
459
viresh kumar9e5ed092012-03-15 10:40:38 +0100460module_amba_driver(pl031_driver);
Deepak Saxena8ae6e162006-06-25 05:47:38 -0700461
462MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net");
463MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
464MODULE_LICENSE("GPL");