aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-sa1100.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/rtc-sa1100.c')
-rw-r--r--drivers/rtc/rtc-sa1100.c161
1 files changed, 127 insertions, 34 deletions
diff --git a/drivers/rtc/rtc-sa1100.c b/drivers/rtc/rtc-sa1100.c
index e4a44b641702..88ea52b8647a 100644
--- a/drivers/rtc/rtc-sa1100.c
+++ b/drivers/rtc/rtc-sa1100.c
@@ -39,10 +39,10 @@
39#include <mach/regs-ost.h> 39#include <mach/regs-ost.h>
40#endif 40#endif
41 41
42#define RTC_DEF_DIVIDER 32768 - 1 42#define RTC_DEF_DIVIDER (32768 - 1)
43#define RTC_DEF_TRIM 0 43#define RTC_DEF_TRIM 0
44 44
45static unsigned long rtc_freq = 1024; 45static const unsigned long RTC_FREQ = 1024;
46static unsigned long timer_freq; 46static unsigned long timer_freq;
47static struct rtc_time rtc_alarm; 47static struct rtc_time rtc_alarm;
48static DEFINE_SPINLOCK(sa1100_rtc_lock); 48static DEFINE_SPINLOCK(sa1100_rtc_lock);
@@ -61,7 +61,8 @@ static inline int rtc_periodic_alarm(struct rtc_time *tm)
61 * Calculate the next alarm time given the requested alarm time mask 61 * Calculate the next alarm time given the requested alarm time mask
62 * and the current time. 62 * and the current time.
63 */ 63 */
64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm) 64static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
65 struct rtc_time *alrm)
65{ 66{
66 unsigned long next_time; 67 unsigned long next_time;
67 unsigned long now_time; 68 unsigned long now_time;
@@ -116,7 +117,23 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
116 rtsr = RTSR; 117 rtsr = RTSR;
117 /* clear interrupt sources */ 118 /* clear interrupt sources */
118 RTSR = 0; 119 RTSR = 0;
119 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2); 120 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
121 * See also the comments in sa1100_rtc_probe(). */
122 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
123 /* This is the original code, before there was the if test
124 * above. This code does not clear interrupts that were not
125 * enabled. */
126 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
127 } else {
128 /* For some reason, it is possible to enter this routine
129 * without interruptions enabled, it has been tested with
130 * several units (Bug in SA11xx chip?).
131 *
132 * This situation leads to an infinite "loop" of interrupt
133 * routine calling and as a result the processor seems to
134 * lock on its first call to open(). */
135 RTSR = RTSR_AL | RTSR_HZ;
136 }
120 137
121 /* clear alarm interrupt if it has occurred */ 138 /* clear alarm interrupt if it has occurred */
122 if (rtsr & RTSR_AL) 139 if (rtsr & RTSR_AL)
@@ -139,8 +156,58 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
139 return IRQ_HANDLED; 156 return IRQ_HANDLED;
140} 157}
141 158
159static int sa1100_irq_set_freq(struct device *dev, int freq)
160{
161 if (freq < 1 || freq > timer_freq) {
162 return -EINVAL;
163 } else {
164 struct rtc_device *rtc = (struct rtc_device *)dev;
165
166 rtc->irq_freq = freq;
167
168 return 0;
169 }
170}
171
142static int rtc_timer1_count; 172static int rtc_timer1_count;
143 173
174static int sa1100_irq_set_state(struct device *dev, int enabled)
175{
176 spin_lock_irq(&sa1100_rtc_lock);
177 if (enabled) {
178 struct rtc_device *rtc = (struct rtc_device *)dev;
179
180 OSMR1 = timer_freq / rtc->irq_freq + OSCR;
181 OIER |= OIER_E1;
182 rtc_timer1_count = 1;
183 } else {
184 OIER &= ~OIER_E1;
185 }
186 spin_unlock_irq(&sa1100_rtc_lock);
187
188 return 0;
189}
190
191static inline int sa1100_timer1_retrigger(struct rtc_device *rtc)
192{
193 unsigned long diff;
194 unsigned long period = timer_freq / rtc->irq_freq;
195
196 spin_lock_irq(&sa1100_rtc_lock);
197
198 do {
199 OSMR1 += period;
200 diff = OSMR1 - OSCR;
201 /* If OSCR > OSMR1, diff is a very large number (unsigned
202 * math). This means we have a lost interrupt. */
203 } while (diff > period);
204 OIER |= OIER_E1;
205
206 spin_unlock_irq(&sa1100_rtc_lock);
207
208 return 0;
209}
210
144static irqreturn_t timer1_interrupt(int irq, void *dev_id) 211static irqreturn_t timer1_interrupt(int irq, void *dev_id)
145{ 212{
146 struct platform_device *pdev = to_platform_device(dev_id); 213 struct platform_device *pdev = to_platform_device(dev_id);
@@ -158,7 +225,11 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
158 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF); 225 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
159 226
160 if (rtc_timer1_count == 1) 227 if (rtc_timer1_count == 1)
161 rtc_timer1_count = (rtc_freq * ((1 << 30) / (timer_freq >> 2))); 228 rtc_timer1_count =
229 (rtc->irq_freq * ((1 << 30) / (timer_freq >> 2)));
230
231 /* retrigger. */
232 sa1100_timer1_retrigger(rtc);
162 233
163 return IRQ_HANDLED; 234 return IRQ_HANDLED;
164} 235}
@@ -166,8 +237,10 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id)
166static int sa1100_rtc_read_callback(struct device *dev, int data) 237static int sa1100_rtc_read_callback(struct device *dev, int data)
167{ 238{
168 if (data & RTC_PF) { 239 if (data & RTC_PF) {
240 struct rtc_device *rtc = (struct rtc_device *)dev;
241
169 /* interpolate missed periods and set match for the next */ 242 /* interpolate missed periods and set match for the next */
170 unsigned long period = timer_freq / rtc_freq; 243 unsigned long period = timer_freq / rtc->irq_freq;
171 unsigned long oscr = OSCR; 244 unsigned long oscr = OSCR;
172 unsigned long osmr1 = OSMR1; 245 unsigned long osmr1 = OSMR1;
173 unsigned long missed = (oscr - osmr1)/period; 246 unsigned long missed = (oscr - osmr1)/period;
@@ -178,7 +251,7 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
178 * Here we compare (match - OSCR) 8 instead of 0 -- 251 * Here we compare (match - OSCR) 8 instead of 0 --
179 * see comment in pxa_timer_interrupt() for explanation. 252 * see comment in pxa_timer_interrupt() for explanation.
180 */ 253 */
181 while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) { 254 while ((signed long)((osmr1 = OSMR1) - OSCR) <= 8) {
182 data += 0x100; 255 data += 0x100;
183 OSSR = OSSR_M1; /* clear match on timer 1 */ 256 OSSR = OSSR_M1; /* clear match on timer 1 */
184 OSMR1 = osmr1 + period; 257 OSMR1 = osmr1 + period;
@@ -190,25 +263,29 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
190static int sa1100_rtc_open(struct device *dev) 263static int sa1100_rtc_open(struct device *dev)
191{ 264{
192 int ret; 265 int ret;
266 struct rtc_device *rtc = (struct rtc_device *)dev;
193 267
194 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED, 268 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
195 "rtc 1Hz", dev); 269 "rtc 1Hz", dev);
196 if (ret) { 270 if (ret) {
197 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz); 271 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
198 goto fail_ui; 272 goto fail_ui;
199 } 273 }
200 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED, 274 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
201 "rtc Alrm", dev); 275 "rtc Alrm", dev);
202 if (ret) { 276 if (ret) {
203 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm); 277 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
204 goto fail_ai; 278 goto fail_ai;
205 } 279 }
206 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED, 280 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
207 "rtc timer", dev); 281 "rtc timer", dev);
208 if (ret) { 282 if (ret) {
209 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1); 283 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
210 goto fail_pi; 284 goto fail_pi;
211 } 285 }
286 rtc->max_user_freq = RTC_FREQ;
287 sa1100_irq_set_freq(dev, RTC_FREQ);
288
212 return 0; 289 return 0;
213 290
214 fail_pi: 291 fail_pi:
@@ -236,7 +313,7 @@ static void sa1100_rtc_release(struct device *dev)
236static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd, 313static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
237 unsigned long arg) 314 unsigned long arg)
238{ 315{
239 switch(cmd) { 316 switch (cmd) {
240 case RTC_AIE_OFF: 317 case RTC_AIE_OFF:
241 spin_lock_irq(&sa1100_rtc_lock); 318 spin_lock_irq(&sa1100_rtc_lock);
242 RTSR &= ~RTSR_ALE; 319 RTSR &= ~RTSR_ALE;
@@ -257,25 +334,6 @@ static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
257 RTSR |= RTSR_HZE; 334 RTSR |= RTSR_HZE;
258 spin_unlock_irq(&sa1100_rtc_lock); 335 spin_unlock_irq(&sa1100_rtc_lock);
259 return 0; 336 return 0;
260 case RTC_PIE_OFF:
261 spin_lock_irq(&sa1100_rtc_lock);
262 OIER &= ~OIER_E1;
263 spin_unlock_irq(&sa1100_rtc_lock);
264 return 0;
265 case RTC_PIE_ON:
266 spin_lock_irq(&sa1100_rtc_lock);
267 OSMR1 = timer_freq / rtc_freq + OSCR;
268 OIER |= OIER_E1;
269 rtc_timer1_count = 1;
270 spin_unlock_irq(&sa1100_rtc_lock);
271 return 0;
272 case RTC_IRQP_READ:
273 return put_user(rtc_freq, (unsigned long *)arg);
274 case RTC_IRQP_SET:
275 if (arg < 1 || arg > timer_freq)
276 return -EINVAL;
277 rtc_freq = arg;
278 return 0;
279 } 337 }
280 return -ENOIOCTLCMD; 338 return -ENOIOCTLCMD;
281} 339}
@@ -327,12 +385,15 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
327 385
328static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) 386static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
329{ 387{
388 struct rtc_device *rtc = (struct rtc_device *)dev;
389
330 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR); 390 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
331 seq_printf(seq, "update_IRQ\t: %s\n", 391 seq_printf(seq, "update_IRQ\t: %s\n",
332 (RTSR & RTSR_HZE) ? "yes" : "no"); 392 (RTSR & RTSR_HZE) ? "yes" : "no");
333 seq_printf(seq, "periodic_IRQ\t: %s\n", 393 seq_printf(seq, "periodic_IRQ\t: %s\n",
334 (OIER & OIER_E1) ? "yes" : "no"); 394 (OIER & OIER_E1) ? "yes" : "no");
335 seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq); 395 seq_printf(seq, "periodic_freq\t: %d\n", rtc->irq_freq);
396 seq_printf(seq, "RTSR\t\t: 0x%08x\n", (u32)RTSR);
336 397
337 return 0; 398 return 0;
338} 399}
@@ -347,6 +408,8 @@ static const struct rtc_class_ops sa1100_rtc_ops = {
347 .read_alarm = sa1100_rtc_read_alarm, 408 .read_alarm = sa1100_rtc_read_alarm,
348 .set_alarm = sa1100_rtc_set_alarm, 409 .set_alarm = sa1100_rtc_set_alarm,
349 .proc = sa1100_rtc_proc, 410 .proc = sa1100_rtc_proc,
411 .irq_set_freq = sa1100_irq_set_freq,
412 .irq_set_state = sa1100_irq_set_state,
350}; 413};
351 414
352static int sa1100_rtc_probe(struct platform_device *pdev) 415static int sa1100_rtc_probe(struct platform_device *pdev)
@@ -364,7 +427,8 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
364 */ 427 */
365 if (RTTR == 0) { 428 if (RTTR == 0) {
366 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16); 429 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
367 dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n"); 430 dev_warn(&pdev->dev, "warning: "
431 "initializing default clock divider/trim value\n");
368 /* The current RTC value probably doesn't make sense either */ 432 /* The current RTC value probably doesn't make sense either */
369 RCNR = 0; 433 RCNR = 0;
370 } 434 }
@@ -372,13 +436,42 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
372 device_init_wakeup(&pdev->dev, 1); 436 device_init_wakeup(&pdev->dev, 1);
373 437
374 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops, 438 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
375 THIS_MODULE); 439 THIS_MODULE);
376 440
377 if (IS_ERR(rtc)) 441 if (IS_ERR(rtc))
378 return PTR_ERR(rtc); 442 return PTR_ERR(rtc);
379 443
380 platform_set_drvdata(pdev, rtc); 444 platform_set_drvdata(pdev, rtc);
381 445
446 /* Set the irq_freq */
447 /*TODO: Find out who is messing with this value after we initialize
448 * it here.*/
449 rtc->irq_freq = RTC_FREQ;
450
451 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
452 * See also the comments in sa1100_rtc_interrupt().
453 *
454 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
455 * interrupt pending, even though interrupts were never enabled.
456 * In this case, this bit it must be reset before enabling
457 * interruptions to avoid a nonexistent interrupt to occur.
458 *
459 * In principle, the same problem would apply to bit 0, although it has
460 * never been observed to happen.
461 *
462 * This issue is addressed both here and in sa1100_rtc_interrupt().
463 * If the issue is not addressed here, in the times when the processor
464 * wakes up with the bit set there will be one spurious interrupt.
465 *
466 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
467 * safe side, once the condition that lead to this strange
468 * initialization is unknown and could in principle happen during
469 * normal processing.
470 *
471 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
472 * the corresponding bits in RTSR. */
473 RTSR = RTSR_AL | RTSR_HZ;
474
382 return 0; 475 return 0;
383} 476}
384 477
@@ -386,7 +479,7 @@ static int sa1100_rtc_remove(struct platform_device *pdev)
386{ 479{
387 struct rtc_device *rtc = platform_get_drvdata(pdev); 480 struct rtc_device *rtc = platform_get_drvdata(pdev);
388 481
389 if (rtc) 482 if (rtc)
390 rtc_device_unregister(rtc); 483 rtc_device_unregister(rtc);
391 484
392 return 0; 485 return 0;