1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <linux/io.h>
33#include <linux/clk.h>
34#include <linux/delay.h>
35#include <linux/module.h>
36#include <linux/platform_device.h>
37#include <linux/rtc.h>
38#include <linux/sched.h>
39#include <linux/workqueue.h>
40
41
42
43#define DTCMR 0x00
44#define DTCLR 0x04
45
46#define DCAMR 0x08
47#define DCALR 0x0c
48#define DCAMR_UNSET 0xFFFFFFFF
49
50#define DCR 0x10
51#define DCR_TCE (1 << 3)
52
53#define DSR 0x14
54#define DSR_WBF (1 << 10)
55#define DSR_WNF (1 << 9)
56#define DSR_WCF (1 << 8)
57#define DSR_WEF (1 << 7)
58#define DSR_CAF (1 << 4)
59#define DSR_NVF (1 << 1)
60#define DSR_SVF (1 << 0)
61
62#define DIER 0x18
63#define DIER_WNIE (1 << 9)
64#define DIER_WCIE (1 << 8)
65#define DIER_WEIE (1 << 7)
66#define DIER_CAIE (1 << 4)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81struct imxdi_dev {
82 struct platform_device *pdev;
83 struct rtc_device *rtc;
84 void __iomem *ioaddr;
85 int irq;
86 struct clk *clk;
87 u32 dsr;
88 spinlock_t irq_lock;
89 wait_queue_head_t write_wait;
90 struct mutex write_mutex;
91 struct work_struct work;
92};
93
94
95
96
97static void di_int_enable(struct imxdi_dev *imxdi, u32 intr)
98{
99 unsigned long flags;
100
101 spin_lock_irqsave(&imxdi->irq_lock, flags);
102 __raw_writel(__raw_readl(imxdi->ioaddr + DIER) | intr,
103 imxdi->ioaddr + DIER);
104 spin_unlock_irqrestore(&imxdi->irq_lock, flags);
105}
106
107
108
109
110static void di_int_disable(struct imxdi_dev *imxdi, u32 intr)
111{
112 unsigned long flags;
113
114 spin_lock_irqsave(&imxdi->irq_lock, flags);
115 __raw_writel(__raw_readl(imxdi->ioaddr + DIER) & ~intr,
116 imxdi->ioaddr + DIER);
117 spin_unlock_irqrestore(&imxdi->irq_lock, flags);
118}
119
120
121
122
123
124
125
126
127static void clear_write_error(struct imxdi_dev *imxdi)
128{
129 int cnt;
130
131 dev_warn(&imxdi->pdev->dev, "WARNING: Register write error!\n");
132
133
134 __raw_writel(DSR_WEF, imxdi->ioaddr + DSR);
135
136
137 for (cnt = 0; cnt < 1000; cnt++) {
138 if ((__raw_readl(imxdi->ioaddr + DSR) & DSR_WEF) == 0)
139 return;
140 udelay(10);
141 }
142 dev_err(&imxdi->pdev->dev,
143 "ERROR: Cannot clear write-error flag!\n");
144}
145
146
147
148
149
150
151
152static int di_write_wait(struct imxdi_dev *imxdi, u32 val, int reg)
153{
154 int ret;
155 int rc = 0;
156
157
158 mutex_lock(&imxdi->write_mutex);
159
160
161 di_int_enable(imxdi, DIER_WCIE);
162
163 imxdi->dsr = 0;
164
165
166 __raw_writel(val, imxdi->ioaddr + reg);
167
168
169 ret = wait_event_interruptible_timeout(imxdi->write_wait,
170 imxdi->dsr & (DSR_WCF | DSR_WEF), msecs_to_jiffies(1));
171 if (ret < 0) {
172 rc = ret;
173 goto out;
174 } else if (ret == 0) {
175 dev_warn(&imxdi->pdev->dev,
176 "Write-wait timeout "
177 "val = 0x%08x reg = 0x%08x\n", val, reg);
178 }
179
180
181 if (imxdi->dsr & DSR_WEF) {
182 clear_write_error(imxdi);
183 rc = -EIO;
184 }
185
186out:
187 mutex_unlock(&imxdi->write_mutex);
188
189 return rc;
190}
191
192
193
194
195static int dryice_rtc_read_time(struct device *dev, struct rtc_time *tm)
196{
197 struct imxdi_dev *imxdi = dev_get_drvdata(dev);
198 unsigned long now;
199
200 now = __raw_readl(imxdi->ioaddr + DTCMR);
201 rtc_time_to_tm(now, tm);
202
203 return 0;
204}
205
206
207
208
209
210static int dryice_rtc_set_mmss(struct device *dev, unsigned long secs)
211{
212 struct imxdi_dev *imxdi = dev_get_drvdata(dev);
213 int rc;
214
215
216 rc = di_write_wait(imxdi, 0, DTCLR);
217 if (rc == 0)
218 rc = di_write_wait(imxdi, secs, DTCMR);
219
220 return rc;
221}
222
223static int dryice_rtc_alarm_irq_enable(struct device *dev,
224 unsigned int enabled)
225{
226 struct imxdi_dev *imxdi = dev_get_drvdata(dev);
227
228 if (enabled)
229 di_int_enable(imxdi, DIER_CAIE);
230 else
231 di_int_disable(imxdi, DIER_CAIE);
232
233 return 0;
234}
235
236
237
238
239
240static int dryice_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
241{
242 struct imxdi_dev *imxdi = dev_get_drvdata(dev);
243 u32 dcamr;
244
245 dcamr = __raw_readl(imxdi->ioaddr + DCAMR);
246 rtc_time_to_tm(dcamr, &alarm->time);
247
248
249 alarm->enabled = (__raw_readl(imxdi->ioaddr + DIER) & DIER_CAIE) != 0;
250
251
252 mutex_lock(&imxdi->write_mutex);
253
254
255 alarm->pending = (__raw_readl(imxdi->ioaddr + DSR) & DSR_CAF) != 0;
256
257 mutex_unlock(&imxdi->write_mutex);
258
259 return 0;
260}
261
262
263
264
265static int dryice_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
266{
267 struct imxdi_dev *imxdi = dev_get_drvdata(dev);
268 unsigned long now;
269 unsigned long alarm_time;
270 int rc;
271
272 rc = rtc_tm_to_time(&alarm->time, &alarm_time);
273 if (rc)
274 return rc;
275
276
277 now = __raw_readl(imxdi->ioaddr + DTCMR);
278 if (alarm_time < now)
279 return -EINVAL;
280
281
282 rc = di_write_wait(imxdi, (u32)alarm_time, DCAMR);
283 if (rc)
284 return rc;
285
286 if (alarm->enabled)
287 di_int_enable(imxdi, DIER_CAIE);
288 else
289 di_int_disable(imxdi, DIER_CAIE);
290
291 return 0;
292}
293
294static struct rtc_class_ops dryice_rtc_ops = {
295 .read_time = dryice_rtc_read_time,
296 .set_mmss = dryice_rtc_set_mmss,
297 .alarm_irq_enable = dryice_rtc_alarm_irq_enable,
298 .read_alarm = dryice_rtc_read_alarm,
299 .set_alarm = dryice_rtc_set_alarm,
300};
301
302
303
304
305static irqreturn_t dryice_norm_irq(int irq, void *dev_id)
306{
307 struct imxdi_dev *imxdi = dev_id;
308 u32 dsr, dier;
309 irqreturn_t rc = IRQ_NONE;
310
311 dier = __raw_readl(imxdi->ioaddr + DIER);
312
313
314 if ((dier & DIER_WCIE)) {
315
316
317
318 if (list_empty_careful(&imxdi->write_wait.task_list))
319 return rc;
320
321
322 dsr = __raw_readl(imxdi->ioaddr + DSR);
323 if ((dsr & (DSR_WCF | DSR_WEF))) {
324
325 di_int_disable(imxdi, DIER_WCIE);
326
327
328 imxdi->dsr |= dsr;
329
330 wake_up_interruptible(&imxdi->write_wait);
331 rc = IRQ_HANDLED;
332 }
333 }
334
335
336 if ((dier & DIER_CAIE)) {
337
338 dsr = __raw_readl(imxdi->ioaddr + DSR);
339 if (dsr & DSR_CAF) {
340
341 di_int_disable(imxdi, DIER_CAIE);
342
343
344 schedule_work(&imxdi->work);
345 rc = IRQ_HANDLED;
346 }
347 }
348 return rc;
349}
350
351
352
353
354
355static void dryice_work(struct work_struct *work)
356{
357 struct imxdi_dev *imxdi = container_of(work,
358 struct imxdi_dev, work);
359
360
361 di_write_wait(imxdi, DSR_CAF, DSR);
362
363
364 rtc_update_irq(imxdi->rtc, 1, RTC_AF | RTC_IRQF);
365}
366
367
368
369
370static int dryice_rtc_probe(struct platform_device *pdev)
371{
372 struct resource *res;
373 struct imxdi_dev *imxdi;
374 int rc;
375
376 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
377 if (!res)
378 return -ENODEV;
379
380 imxdi = devm_kzalloc(&pdev->dev, sizeof(*imxdi), GFP_KERNEL);
381 if (!imxdi)
382 return -ENOMEM;
383
384 imxdi->pdev = pdev;
385
386 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
387 pdev->name))
388 return -EBUSY;
389
390 imxdi->ioaddr = devm_ioremap(&pdev->dev, res->start,
391 resource_size(res));
392 if (imxdi->ioaddr == NULL)
393 return -ENOMEM;
394
395 imxdi->irq = platform_get_irq(pdev, 0);
396 if (imxdi->irq < 0)
397 return imxdi->irq;
398
399 init_waitqueue_head(&imxdi->write_wait);
400
401 INIT_WORK(&imxdi->work, dryice_work);
402
403 mutex_init(&imxdi->write_mutex);
404
405 imxdi->clk = clk_get(&pdev->dev, NULL);
406 if (IS_ERR(imxdi->clk))
407 return PTR_ERR(imxdi->clk);
408 clk_enable(imxdi->clk);
409
410
411
412
413
414
415 __raw_writel(0, imxdi->ioaddr + DIER);
416
417 rc = devm_request_irq(&pdev->dev, imxdi->irq, dryice_norm_irq,
418 IRQF_SHARED, pdev->name, imxdi);
419 if (rc) {
420 dev_warn(&pdev->dev, "interrupt not available.\n");
421 goto err;
422 }
423
424
425 if (__raw_readl(imxdi->ioaddr + DSR) & DSR_NVF) {
426 rc = di_write_wait(imxdi, DSR_NVF | DSR_SVF, DSR);
427 if (rc)
428 goto err;
429 }
430
431
432 rc = di_write_wait(imxdi, DCAMR_UNSET, DCAMR);
433 if (rc)
434 goto err;
435 rc = di_write_wait(imxdi, 0, DCALR);
436 if (rc)
437 goto err;
438
439
440 if (__raw_readl(imxdi->ioaddr + DSR) & DSR_CAF) {
441 rc = di_write_wait(imxdi, DSR_CAF, DSR);
442 if (rc)
443 goto err;
444 }
445
446
447 if (__raw_readl(imxdi->ioaddr + DTCMR) == 0) {
448 rc = di_write_wait(imxdi, 0, DTCMR);
449 if (rc)
450 goto err;
451 }
452
453
454 if (!(__raw_readl(imxdi->ioaddr + DCR) & DCR_TCE)) {
455 rc = di_write_wait(imxdi,
456 __raw_readl(imxdi->ioaddr + DCR) | DCR_TCE,
457 DCR);
458 if (rc)
459 goto err;
460 }
461
462 platform_set_drvdata(pdev, imxdi);
463 imxdi->rtc = rtc_device_register(pdev->name, &pdev->dev,
464 &dryice_rtc_ops, THIS_MODULE);
465 if (IS_ERR(imxdi->rtc)) {
466 rc = PTR_ERR(imxdi->rtc);
467 goto err;
468 }
469
470 return 0;
471
472err:
473 clk_disable(imxdi->clk);
474 clk_put(imxdi->clk);
475
476 return rc;
477}
478
479static int __devexit dryice_rtc_remove(struct platform_device *pdev)
480{
481 struct imxdi_dev *imxdi = platform_get_drvdata(pdev);
482
483 flush_work(&imxdi->work);
484
485
486 __raw_writel(0, imxdi->ioaddr + DIER);
487
488 rtc_device_unregister(imxdi->rtc);
489
490 clk_disable(imxdi->clk);
491 clk_put(imxdi->clk);
492
493 return 0;
494}
495
496static struct platform_driver dryice_rtc_driver = {
497 .driver = {
498 .name = "imxdi_rtc",
499 .owner = THIS_MODULE,
500 },
501 .remove = __devexit_p(dryice_rtc_remove),
502};
503
504static int __init dryice_rtc_init(void)
505{
506 return platform_driver_probe(&dryice_rtc_driver, dryice_rtc_probe);
507}
508
509static void __exit dryice_rtc_exit(void)
510{
511 platform_driver_unregister(&dryice_rtc_driver);
512}
513
514module_init(dryice_rtc_init);
515module_exit(dryice_rtc_exit);
516
517MODULE_AUTHOR("Freescale Semiconductor, Inc.");
518MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
519MODULE_DESCRIPTION("IMX DryIce Realtime Clock Driver (RTC)");
520MODULE_LICENSE("GPL");
521