diff options
Diffstat (limited to 'drivers/rtc/rtc-at91rm9200.c')
-rw-r--r-- | drivers/rtc/rtc-at91rm9200.c | 131 |
1 files changed, 111 insertions, 20 deletions
diff --git a/drivers/rtc/rtc-at91rm9200.c b/drivers/rtc/rtc-at91rm9200.c index 0eab77b22340..f296f3f7db9b 100644 --- a/drivers/rtc/rtc-at91rm9200.c +++ b/drivers/rtc/rtc-at91rm9200.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <linux/rtc.h> | 25 | #include <linux/rtc.h> |
26 | #include <linux/bcd.h> | 26 | #include <linux/bcd.h> |
27 | #include <linux/interrupt.h> | 27 | #include <linux/interrupt.h> |
28 | #include <linux/spinlock.h> | ||
28 | #include <linux/ioctl.h> | 29 | #include <linux/ioctl.h> |
29 | #include <linux/completion.h> | 30 | #include <linux/completion.h> |
30 | #include <linux/io.h> | 31 | #include <linux/io.h> |
@@ -42,10 +43,65 @@ | |||
42 | 43 | ||
43 | #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */ | 44 | #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */ |
44 | 45 | ||
46 | struct at91_rtc_config { | ||
47 | bool use_shadow_imr; | ||
48 | }; | ||
49 | |||
50 | static const struct at91_rtc_config *at91_rtc_config; | ||
45 | static DECLARE_COMPLETION(at91_rtc_updated); | 51 | static DECLARE_COMPLETION(at91_rtc_updated); |
46 | static unsigned int at91_alarm_year = AT91_RTC_EPOCH; | 52 | static unsigned int at91_alarm_year = AT91_RTC_EPOCH; |
47 | static void __iomem *at91_rtc_regs; | 53 | static void __iomem *at91_rtc_regs; |
48 | static int irq; | 54 | static int irq; |
55 | static DEFINE_SPINLOCK(at91_rtc_lock); | ||
56 | static u32 at91_rtc_shadow_imr; | ||
57 | |||
58 | static void at91_rtc_write_ier(u32 mask) | ||
59 | { | ||
60 | unsigned long flags; | ||
61 | |||
62 | spin_lock_irqsave(&at91_rtc_lock, flags); | ||
63 | at91_rtc_shadow_imr |= mask; | ||
64 | at91_rtc_write(AT91_RTC_IER, mask); | ||
65 | spin_unlock_irqrestore(&at91_rtc_lock, flags); | ||
66 | } | ||
67 | |||
68 | static void at91_rtc_write_idr(u32 mask) | ||
69 | { | ||
70 | unsigned long flags; | ||
71 | |||
72 | spin_lock_irqsave(&at91_rtc_lock, flags); | ||
73 | at91_rtc_write(AT91_RTC_IDR, mask); | ||
74 | /* | ||
75 | * Register read back (of any RTC-register) needed to make sure | ||
76 | * IDR-register write has reached the peripheral before updating | ||
77 | * shadow mask. | ||
78 | * | ||
79 | * Note that there is still a possibility that the mask is updated | ||
80 | * before interrupts have actually been disabled in hardware. The only | ||
81 | * way to be certain would be to poll the IMR-register, which is is | ||
82 | * the very register we are trying to emulate. The register read back | ||
83 | * is a reasonable heuristic. | ||
84 | */ | ||
85 | at91_rtc_read(AT91_RTC_SR); | ||
86 | at91_rtc_shadow_imr &= ~mask; | ||
87 | spin_unlock_irqrestore(&at91_rtc_lock, flags); | ||
88 | } | ||
89 | |||
90 | static u32 at91_rtc_read_imr(void) | ||
91 | { | ||
92 | unsigned long flags; | ||
93 | u32 mask; | ||
94 | |||
95 | if (at91_rtc_config->use_shadow_imr) { | ||
96 | spin_lock_irqsave(&at91_rtc_lock, flags); | ||
97 | mask = at91_rtc_shadow_imr; | ||
98 | spin_unlock_irqrestore(&at91_rtc_lock, flags); | ||
99 | } else { | ||
100 | mask = at91_rtc_read(AT91_RTC_IMR); | ||
101 | } | ||
102 | |||
103 | return mask; | ||
104 | } | ||
49 | 105 | ||
50 | /* | 106 | /* |
51 | * Decode time/date into rtc_time structure | 107 | * Decode time/date into rtc_time structure |
@@ -110,9 +166,9 @@ static int at91_rtc_settime(struct device *dev, struct rtc_time *tm) | |||
110 | cr = at91_rtc_read(AT91_RTC_CR); | 166 | cr = at91_rtc_read(AT91_RTC_CR); |
111 | at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM); | 167 | at91_rtc_write(AT91_RTC_CR, cr | AT91_RTC_UPDCAL | AT91_RTC_UPDTIM); |
112 | 168 | ||
113 | at91_rtc_write(AT91_RTC_IER, AT91_RTC_ACKUPD); | 169 | at91_rtc_write_ier(AT91_RTC_ACKUPD); |
114 | wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */ | 170 | wait_for_completion(&at91_rtc_updated); /* wait for ACKUPD interrupt */ |
115 | at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD); | 171 | at91_rtc_write_idr(AT91_RTC_ACKUPD); |
116 | 172 | ||
117 | at91_rtc_write(AT91_RTC_TIMR, | 173 | at91_rtc_write(AT91_RTC_TIMR, |
118 | bin2bcd(tm->tm_sec) << 0 | 174 | bin2bcd(tm->tm_sec) << 0 |
@@ -144,7 +200,7 @@ static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) | |||
144 | tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); | 200 | tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); |
145 | tm->tm_year = at91_alarm_year - 1900; | 201 | tm->tm_year = at91_alarm_year - 1900; |
146 | 202 | ||
147 | alrm->enabled = (at91_rtc_read(AT91_RTC_IMR) & AT91_RTC_ALARM) | 203 | alrm->enabled = (at91_rtc_read_imr() & AT91_RTC_ALARM) |
148 | ? 1 : 0; | 204 | ? 1 : 0; |
149 | 205 | ||
150 | dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__, | 206 | dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__, |
@@ -169,7 +225,7 @@ static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) | |||
169 | tm.tm_min = alrm->time.tm_min; | 225 | tm.tm_min = alrm->time.tm_min; |
170 | tm.tm_sec = alrm->time.tm_sec; | 226 | tm.tm_sec = alrm->time.tm_sec; |
171 | 227 | ||
172 | at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ALARM); | 228 | at91_rtc_write_idr(AT91_RTC_ALARM); |
173 | at91_rtc_write(AT91_RTC_TIMALR, | 229 | at91_rtc_write(AT91_RTC_TIMALR, |
174 | bin2bcd(tm.tm_sec) << 0 | 230 | bin2bcd(tm.tm_sec) << 0 |
175 | | bin2bcd(tm.tm_min) << 8 | 231 | | bin2bcd(tm.tm_min) << 8 |
@@ -182,7 +238,7 @@ static int at91_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) | |||
182 | 238 | ||
183 | if (alrm->enabled) { | 239 | if (alrm->enabled) { |
184 | at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM); | 240 | at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM); |
185 | at91_rtc_write(AT91_RTC_IER, AT91_RTC_ALARM); | 241 | at91_rtc_write_ier(AT91_RTC_ALARM); |
186 | } | 242 | } |
187 | 243 | ||
188 | dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__, | 244 | dev_dbg(dev, "%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __func__, |
@@ -198,9 +254,9 @@ static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) | |||
198 | 254 | ||
199 | if (enabled) { | 255 | if (enabled) { |
200 | at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM); | 256 | at91_rtc_write(AT91_RTC_SCCR, AT91_RTC_ALARM); |
201 | at91_rtc_write(AT91_RTC_IER, AT91_RTC_ALARM); | 257 | at91_rtc_write_ier(AT91_RTC_ALARM); |
202 | } else | 258 | } else |
203 | at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ALARM); | 259 | at91_rtc_write_idr(AT91_RTC_ALARM); |
204 | 260 | ||
205 | return 0; | 261 | return 0; |
206 | } | 262 | } |
@@ -209,7 +265,7 @@ static int at91_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) | |||
209 | */ | 265 | */ |
210 | static int at91_rtc_proc(struct device *dev, struct seq_file *seq) | 266 | static int at91_rtc_proc(struct device *dev, struct seq_file *seq) |
211 | { | 267 | { |
212 | unsigned long imr = at91_rtc_read(AT91_RTC_IMR); | 268 | unsigned long imr = at91_rtc_read_imr(); |
213 | 269 | ||
214 | seq_printf(seq, "update_IRQ\t: %s\n", | 270 | seq_printf(seq, "update_IRQ\t: %s\n", |
215 | (imr & AT91_RTC_ACKUPD) ? "yes" : "no"); | 271 | (imr & AT91_RTC_ACKUPD) ? "yes" : "no"); |
@@ -229,7 +285,7 @@ static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id) | |||
229 | unsigned int rtsr; | 285 | unsigned int rtsr; |
230 | unsigned long events = 0; | 286 | unsigned long events = 0; |
231 | 287 | ||
232 | rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read(AT91_RTC_IMR); | 288 | rtsr = at91_rtc_read(AT91_RTC_SR) & at91_rtc_read_imr(); |
233 | if (rtsr) { /* this interrupt is shared! Is it ours? */ | 289 | if (rtsr) { /* this interrupt is shared! Is it ours? */ |
234 | if (rtsr & AT91_RTC_ALARM) | 290 | if (rtsr & AT91_RTC_ALARM) |
235 | events |= (RTC_AF | RTC_IRQF); | 291 | events |= (RTC_AF | RTC_IRQF); |
@@ -250,6 +306,43 @@ static irqreturn_t at91_rtc_interrupt(int irq, void *dev_id) | |||
250 | return IRQ_NONE; /* not handled */ | 306 | return IRQ_NONE; /* not handled */ |
251 | } | 307 | } |
252 | 308 | ||
309 | static const struct at91_rtc_config at91rm9200_config = { | ||
310 | }; | ||
311 | |||
312 | static const struct at91_rtc_config at91sam9x5_config = { | ||
313 | .use_shadow_imr = true, | ||
314 | }; | ||
315 | |||
316 | #ifdef CONFIG_OF | ||
317 | static const struct of_device_id at91_rtc_dt_ids[] = { | ||
318 | { | ||
319 | .compatible = "atmel,at91rm9200-rtc", | ||
320 | .data = &at91rm9200_config, | ||
321 | }, { | ||
322 | .compatible = "atmel,at91sam9x5-rtc", | ||
323 | .data = &at91sam9x5_config, | ||
324 | }, { | ||
325 | /* sentinel */ | ||
326 | } | ||
327 | }; | ||
328 | MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids); | ||
329 | #endif | ||
330 | |||
331 | static const struct at91_rtc_config * | ||
332 | at91_rtc_get_config(struct platform_device *pdev) | ||
333 | { | ||
334 | const struct of_device_id *match; | ||
335 | |||
336 | if (pdev->dev.of_node) { | ||
337 | match = of_match_node(at91_rtc_dt_ids, pdev->dev.of_node); | ||
338 | if (!match) | ||
339 | return NULL; | ||
340 | return (const struct at91_rtc_config *)match->data; | ||
341 | } | ||
342 | |||
343 | return &at91rm9200_config; | ||
344 | } | ||
345 | |||
253 | static const struct rtc_class_ops at91_rtc_ops = { | 346 | static const struct rtc_class_ops at91_rtc_ops = { |
254 | .read_time = at91_rtc_readtime, | 347 | .read_time = at91_rtc_readtime, |
255 | .set_time = at91_rtc_settime, | 348 | .set_time = at91_rtc_settime, |
@@ -268,6 +361,10 @@ static int __init at91_rtc_probe(struct platform_device *pdev) | |||
268 | struct resource *regs; | 361 | struct resource *regs; |
269 | int ret = 0; | 362 | int ret = 0; |
270 | 363 | ||
364 | at91_rtc_config = at91_rtc_get_config(pdev); | ||
365 | if (!at91_rtc_config) | ||
366 | return -ENODEV; | ||
367 | |||
271 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 368 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
272 | if (!regs) { | 369 | if (!regs) { |
273 | dev_err(&pdev->dev, "no mmio resource defined\n"); | 370 | dev_err(&pdev->dev, "no mmio resource defined\n"); |
@@ -290,7 +387,7 @@ static int __init at91_rtc_probe(struct platform_device *pdev) | |||
290 | at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */ | 387 | at91_rtc_write(AT91_RTC_MR, 0); /* 24 hour mode */ |
291 | 388 | ||
292 | /* Disable all interrupts */ | 389 | /* Disable all interrupts */ |
293 | at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM | | 390 | at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM | |
294 | AT91_RTC_SECEV | AT91_RTC_TIMEV | | 391 | AT91_RTC_SECEV | AT91_RTC_TIMEV | |
295 | AT91_RTC_CALEV); | 392 | AT91_RTC_CALEV); |
296 | 393 | ||
@@ -335,7 +432,7 @@ static int __exit at91_rtc_remove(struct platform_device *pdev) | |||
335 | struct rtc_device *rtc = platform_get_drvdata(pdev); | 432 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
336 | 433 | ||
337 | /* Disable all interrupts */ | 434 | /* Disable all interrupts */ |
338 | at91_rtc_write(AT91_RTC_IDR, AT91_RTC_ACKUPD | AT91_RTC_ALARM | | 435 | at91_rtc_write_idr(AT91_RTC_ACKUPD | AT91_RTC_ALARM | |
339 | AT91_RTC_SECEV | AT91_RTC_TIMEV | | 436 | AT91_RTC_SECEV | AT91_RTC_TIMEV | |
340 | AT91_RTC_CALEV); | 437 | AT91_RTC_CALEV); |
341 | free_irq(irq, pdev); | 438 | free_irq(irq, pdev); |
@@ -358,13 +455,13 @@ static int at91_rtc_suspend(struct device *dev) | |||
358 | /* this IRQ is shared with DBGU and other hardware which isn't | 455 | /* this IRQ is shared with DBGU and other hardware which isn't |
359 | * necessarily doing PM like we are... | 456 | * necessarily doing PM like we are... |
360 | */ | 457 | */ |
361 | at91_rtc_imr = at91_rtc_read(AT91_RTC_IMR) | 458 | at91_rtc_imr = at91_rtc_read_imr() |
362 | & (AT91_RTC_ALARM|AT91_RTC_SECEV); | 459 | & (AT91_RTC_ALARM|AT91_RTC_SECEV); |
363 | if (at91_rtc_imr) { | 460 | if (at91_rtc_imr) { |
364 | if (device_may_wakeup(dev)) | 461 | if (device_may_wakeup(dev)) |
365 | enable_irq_wake(irq); | 462 | enable_irq_wake(irq); |
366 | else | 463 | else |
367 | at91_rtc_write(AT91_RTC_IDR, at91_rtc_imr); | 464 | at91_rtc_write_idr(at91_rtc_imr); |
368 | } | 465 | } |
369 | return 0; | 466 | return 0; |
370 | } | 467 | } |
@@ -375,7 +472,7 @@ static int at91_rtc_resume(struct device *dev) | |||
375 | if (device_may_wakeup(dev)) | 472 | if (device_may_wakeup(dev)) |
376 | disable_irq_wake(irq); | 473 | disable_irq_wake(irq); |
377 | else | 474 | else |
378 | at91_rtc_write(AT91_RTC_IER, at91_rtc_imr); | 475 | at91_rtc_write_ier(at91_rtc_imr); |
379 | } | 476 | } |
380 | return 0; | 477 | return 0; |
381 | } | 478 | } |
@@ -383,12 +480,6 @@ static int at91_rtc_resume(struct device *dev) | |||
383 | 480 | ||
384 | static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume); | 481 | static SIMPLE_DEV_PM_OPS(at91_rtc_pm_ops, at91_rtc_suspend, at91_rtc_resume); |
385 | 482 | ||
386 | static const struct of_device_id at91_rtc_dt_ids[] = { | ||
387 | { .compatible = "atmel,at91rm9200-rtc" }, | ||
388 | { /* sentinel */ } | ||
389 | }; | ||
390 | MODULE_DEVICE_TABLE(of, at91_rtc_dt_ids); | ||
391 | |||
392 | static struct platform_driver at91_rtc_driver = { | 483 | static struct platform_driver at91_rtc_driver = { |
393 | .remove = __exit_p(at91_rtc_remove), | 484 | .remove = __exit_p(at91_rtc_remove), |
394 | .driver = { | 485 | .driver = { |