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