xref: /openbmc/linux/drivers/rtc/rtc-spear.c (revision 63dc02bd)
1 /*
2  * drivers/rtc/rtc-spear.c
3  *
4  * Copyright (C) 2010 ST Microelectronics
5  * Rajeev Kumar<rajeev-dlh.kumar@st.com>
6  *
7  * This file is licensed under the terms of the GNU General Public
8  * License version 2. This program is licensed "as is" without any
9  * warranty of any kind, whether express or implied.
10  */
11 
12 #include <linux/bcd.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/rtc.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 
24 /* RTC registers */
25 #define TIME_REG		0x00
26 #define DATE_REG		0x04
27 #define ALARM_TIME_REG		0x08
28 #define ALARM_DATE_REG		0x0C
29 #define CTRL_REG		0x10
30 #define STATUS_REG		0x14
31 
32 /* TIME_REG & ALARM_TIME_REG */
33 #define SECONDS_UNITS		(0xf<<0)	/* seconds units position */
34 #define SECONDS_TENS		(0x7<<4)	/* seconds tens position */
35 #define MINUTES_UNITS		(0xf<<8)	/* minutes units position */
36 #define MINUTES_TENS		(0x7<<12)	/* minutes tens position */
37 #define HOURS_UNITS		(0xf<<16)	/* hours units position */
38 #define HOURS_TENS		(0x3<<20)	/* hours tens position */
39 
40 /* DATE_REG & ALARM_DATE_REG */
41 #define DAYS_UNITS		(0xf<<0)	/* days units position */
42 #define DAYS_TENS		(0x3<<4)	/* days tens position */
43 #define MONTHS_UNITS		(0xf<<8)	/* months units position */
44 #define MONTHS_TENS		(0x1<<12)	/* months tens position */
45 #define YEARS_UNITS		(0xf<<16)	/* years units position */
46 #define YEARS_TENS		(0xf<<20)	/* years tens position */
47 #define YEARS_HUNDREDS		(0xf<<24)	/* years hundereds position */
48 #define YEARS_MILLENIUMS	(0xf<<28)	/* years millenium position */
49 
50 /* MASK SHIFT TIME_REG & ALARM_TIME_REG*/
51 #define SECOND_SHIFT		0x00		/* seconds units */
52 #define MINUTE_SHIFT		0x08		/* minutes units position */
53 #define HOUR_SHIFT		0x10		/* hours units position */
54 #define MDAY_SHIFT		0x00		/* Month day shift */
55 #define MONTH_SHIFT		0x08		/* Month shift */
56 #define YEAR_SHIFT		0x10		/* Year shift */
57 
58 #define SECOND_MASK		0x7F
59 #define MIN_MASK		0x7F
60 #define HOUR_MASK		0x3F
61 #define DAY_MASK		0x3F
62 #define MONTH_MASK		0x7F
63 #define YEAR_MASK		0xFFFF
64 
65 /* date reg equal to time reg, for debug only */
66 #define TIME_BYP		(1<<9)
67 #define INT_ENABLE		(1<<31)		/* interrupt enable */
68 
69 /* STATUS_REG */
70 #define CLK_UNCONNECTED		(1<<0)
71 #define PEND_WR_TIME		(1<<2)
72 #define PEND_WR_DATE		(1<<3)
73 #define LOST_WR_TIME		(1<<4)
74 #define LOST_WR_DATE		(1<<5)
75 #define RTC_INT_MASK		(1<<31)
76 #define STATUS_BUSY		(PEND_WR_TIME | PEND_WR_DATE)
77 #define STATUS_FAIL		(LOST_WR_TIME | LOST_WR_DATE)
78 
79 struct spear_rtc_config {
80 	struct rtc_device *rtc;
81 	struct clk *clk;
82 	spinlock_t lock;
83 	void __iomem *ioaddr;
84 	unsigned int irq_wake;
85 };
86 
87 static inline void spear_rtc_clear_interrupt(struct spear_rtc_config *config)
88 {
89 	unsigned int val;
90 	unsigned long flags;
91 
92 	spin_lock_irqsave(&config->lock, flags);
93 	val = readl(config->ioaddr + STATUS_REG);
94 	val |= RTC_INT_MASK;
95 	writel(val, config->ioaddr + STATUS_REG);
96 	spin_unlock_irqrestore(&config->lock, flags);
97 }
98 
99 static inline void spear_rtc_enable_interrupt(struct spear_rtc_config *config)
100 {
101 	unsigned int val;
102 
103 	val = readl(config->ioaddr + CTRL_REG);
104 	if (!(val & INT_ENABLE)) {
105 		spear_rtc_clear_interrupt(config);
106 		val |= INT_ENABLE;
107 		writel(val, config->ioaddr + CTRL_REG);
108 	}
109 }
110 
111 static inline void spear_rtc_disable_interrupt(struct spear_rtc_config *config)
112 {
113 	unsigned int val;
114 
115 	val = readl(config->ioaddr + CTRL_REG);
116 	if (val & INT_ENABLE) {
117 		val &= ~INT_ENABLE;
118 		writel(val, config->ioaddr + CTRL_REG);
119 	}
120 }
121 
122 static inline int is_write_complete(struct spear_rtc_config *config)
123 {
124 	int ret = 0;
125 	unsigned long flags;
126 
127 	spin_lock_irqsave(&config->lock, flags);
128 	if ((readl(config->ioaddr + STATUS_REG)) & STATUS_FAIL)
129 		ret = -EIO;
130 	spin_unlock_irqrestore(&config->lock, flags);
131 
132 	return ret;
133 }
134 
135 static void rtc_wait_not_busy(struct spear_rtc_config *config)
136 {
137 	int status, count = 0;
138 	unsigned long flags;
139 
140 	/* Assuming BUSY may stay active for 80 msec) */
141 	for (count = 0; count < 80; count++) {
142 		spin_lock_irqsave(&config->lock, flags);
143 		status = readl(config->ioaddr + STATUS_REG);
144 		spin_unlock_irqrestore(&config->lock, flags);
145 		if ((status & STATUS_BUSY) == 0)
146 			break;
147 		/* check status busy, after each msec */
148 		msleep(1);
149 	}
150 }
151 
152 static irqreturn_t spear_rtc_irq(int irq, void *dev_id)
153 {
154 	struct spear_rtc_config *config = dev_id;
155 	unsigned long flags, events = 0;
156 	unsigned int irq_data;
157 
158 	spin_lock_irqsave(&config->lock, flags);
159 	irq_data = readl(config->ioaddr + STATUS_REG);
160 	spin_unlock_irqrestore(&config->lock, flags);
161 
162 	if ((irq_data & RTC_INT_MASK)) {
163 		spear_rtc_clear_interrupt(config);
164 		events = RTC_IRQF | RTC_AF;
165 		rtc_update_irq(config->rtc, 1, events);
166 		return IRQ_HANDLED;
167 	} else
168 		return IRQ_NONE;
169 
170 }
171 
172 static int tm2bcd(struct rtc_time *tm)
173 {
174 	if (rtc_valid_tm(tm) != 0)
175 		return -EINVAL;
176 	tm->tm_sec = bin2bcd(tm->tm_sec);
177 	tm->tm_min = bin2bcd(tm->tm_min);
178 	tm->tm_hour = bin2bcd(tm->tm_hour);
179 	tm->tm_mday = bin2bcd(tm->tm_mday);
180 	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
181 	tm->tm_year = bin2bcd(tm->tm_year);
182 
183 	return 0;
184 }
185 
186 static void bcd2tm(struct rtc_time *tm)
187 {
188 	tm->tm_sec = bcd2bin(tm->tm_sec);
189 	tm->tm_min = bcd2bin(tm->tm_min);
190 	tm->tm_hour = bcd2bin(tm->tm_hour);
191 	tm->tm_mday = bcd2bin(tm->tm_mday);
192 	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
193 	/* epoch == 1900 */
194 	tm->tm_year = bcd2bin(tm->tm_year);
195 }
196 
197 /*
198  * spear_rtc_read_time - set the time
199  * @dev: rtc device in use
200  * @tm: holds date and time
201  *
202  * This function read time and date. On success it will return 0
203  * otherwise -ve error is returned.
204  */
205 static int spear_rtc_read_time(struct device *dev, struct rtc_time *tm)
206 {
207 	struct spear_rtc_config *config = dev_get_drvdata(dev);
208 	unsigned int time, date;
209 
210 	/* we don't report wday/yday/isdst ... */
211 	rtc_wait_not_busy(config);
212 
213 	time = readl(config->ioaddr + TIME_REG);
214 	date = readl(config->ioaddr + DATE_REG);
215 	tm->tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
216 	tm->tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
217 	tm->tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
218 	tm->tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
219 	tm->tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
220 	tm->tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
221 
222 	bcd2tm(tm);
223 	return 0;
224 }
225 
226 /*
227  * spear_rtc_set_time - set the time
228  * @dev: rtc device in use
229  * @tm: holds date and time
230  *
231  * This function set time and date. On success it will return 0
232  * otherwise -ve error is returned.
233  */
234 static int spear_rtc_set_time(struct device *dev, struct rtc_time *tm)
235 {
236 	struct spear_rtc_config *config = dev_get_drvdata(dev);
237 	unsigned int time, date, err = 0;
238 
239 	if (tm2bcd(tm) < 0)
240 		return -EINVAL;
241 
242 	rtc_wait_not_busy(config);
243 	time = (tm->tm_sec << SECOND_SHIFT) | (tm->tm_min << MINUTE_SHIFT) |
244 		(tm->tm_hour << HOUR_SHIFT);
245 	date = (tm->tm_mday << MDAY_SHIFT) | (tm->tm_mon << MONTH_SHIFT) |
246 		(tm->tm_year << YEAR_SHIFT);
247 	writel(time, config->ioaddr + TIME_REG);
248 	writel(date, config->ioaddr + DATE_REG);
249 	err = is_write_complete(config);
250 	if (err < 0)
251 		return err;
252 
253 	return 0;
254 }
255 
256 /*
257  * spear_rtc_read_alarm - read the alarm time
258  * @dev: rtc device in use
259  * @alm: holds alarm date and time
260  *
261  * This function read alarm time and date. On success it will return 0
262  * otherwise -ve error is returned.
263  */
264 static int spear_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
265 {
266 	struct spear_rtc_config *config = dev_get_drvdata(dev);
267 	unsigned int time, date;
268 
269 	rtc_wait_not_busy(config);
270 
271 	time = readl(config->ioaddr + ALARM_TIME_REG);
272 	date = readl(config->ioaddr + ALARM_DATE_REG);
273 	alm->time.tm_sec = (time >> SECOND_SHIFT) & SECOND_MASK;
274 	alm->time.tm_min = (time >> MINUTE_SHIFT) & MIN_MASK;
275 	alm->time.tm_hour = (time >> HOUR_SHIFT) & HOUR_MASK;
276 	alm->time.tm_mday = (date >> MDAY_SHIFT) & DAY_MASK;
277 	alm->time.tm_mon = (date >> MONTH_SHIFT) & MONTH_MASK;
278 	alm->time.tm_year = (date >> YEAR_SHIFT) & YEAR_MASK;
279 
280 	bcd2tm(&alm->time);
281 	alm->enabled = readl(config->ioaddr + CTRL_REG) & INT_ENABLE;
282 
283 	return 0;
284 }
285 
286 /*
287  * spear_rtc_set_alarm - set the alarm time
288  * @dev: rtc device in use
289  * @alm: holds alarm date and time
290  *
291  * This function set alarm time and date. On success it will return 0
292  * otherwise -ve error is returned.
293  */
294 static int spear_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
295 {
296 	struct spear_rtc_config *config = dev_get_drvdata(dev);
297 	unsigned int time, date, err = 0;
298 
299 	if (tm2bcd(&alm->time) < 0)
300 		return -EINVAL;
301 
302 	rtc_wait_not_busy(config);
303 
304 	time = (alm->time.tm_sec << SECOND_SHIFT) | (alm->time.tm_min <<
305 			MINUTE_SHIFT) |	(alm->time.tm_hour << HOUR_SHIFT);
306 	date = (alm->time.tm_mday << MDAY_SHIFT) | (alm->time.tm_mon <<
307 			MONTH_SHIFT) | (alm->time.tm_year << YEAR_SHIFT);
308 
309 	writel(time, config->ioaddr + ALARM_TIME_REG);
310 	writel(date, config->ioaddr + ALARM_DATE_REG);
311 	err = is_write_complete(config);
312 	if (err < 0)
313 		return err;
314 
315 	if (alm->enabled)
316 		spear_rtc_enable_interrupt(config);
317 	else
318 		spear_rtc_disable_interrupt(config);
319 
320 	return 0;
321 }
322 
323 static int spear_alarm_irq_enable(struct device *dev, unsigned int enabled)
324 {
325 	struct spear_rtc_config *config = dev_get_drvdata(dev);
326 	int ret = 0;
327 
328 	spear_rtc_clear_interrupt(config);
329 
330 	switch (enabled) {
331 	case 0:
332 		/* alarm off */
333 		spear_rtc_disable_interrupt(config);
334 		break;
335 	case 1:
336 		/* alarm on */
337 		spear_rtc_enable_interrupt(config);
338 		break;
339 	default:
340 		ret = -EINVAL;
341 		break;
342 	}
343 
344 	return ret;
345 }
346 
347 static struct rtc_class_ops spear_rtc_ops = {
348 	.read_time = spear_rtc_read_time,
349 	.set_time = spear_rtc_set_time,
350 	.read_alarm = spear_rtc_read_alarm,
351 	.set_alarm = spear_rtc_set_alarm,
352 	.alarm_irq_enable = spear_alarm_irq_enable,
353 };
354 
355 static int __devinit spear_rtc_probe(struct platform_device *pdev)
356 {
357 	struct resource *res;
358 	struct spear_rtc_config *config;
359 	unsigned int status = 0;
360 	int irq;
361 
362 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 	if (!res) {
364 		dev_err(&pdev->dev, "no resource defined\n");
365 		return -EBUSY;
366 	}
367 	if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
368 		dev_err(&pdev->dev, "rtc region already claimed\n");
369 		return -EBUSY;
370 	}
371 
372 	config = kzalloc(sizeof(*config), GFP_KERNEL);
373 	if (!config) {
374 		dev_err(&pdev->dev, "out of memory\n");
375 		status = -ENOMEM;
376 		goto err_release_region;
377 	}
378 
379 	config->clk = clk_get(&pdev->dev, NULL);
380 	if (IS_ERR(config->clk)) {
381 		status = PTR_ERR(config->clk);
382 		goto err_kfree;
383 	}
384 
385 	status = clk_enable(config->clk);
386 	if (status < 0)
387 		goto err_clk_put;
388 
389 	config->ioaddr = ioremap(res->start, resource_size(res));
390 	if (!config->ioaddr) {
391 		dev_err(&pdev->dev, "ioremap fail\n");
392 		status = -ENOMEM;
393 		goto err_disable_clock;
394 	}
395 
396 	spin_lock_init(&config->lock);
397 	platform_set_drvdata(pdev, config);
398 
399 	config->rtc = rtc_device_register(pdev->name, &pdev->dev,
400 			&spear_rtc_ops, THIS_MODULE);
401 	if (IS_ERR(config->rtc)) {
402 		dev_err(&pdev->dev, "can't register RTC device, err %ld\n",
403 				PTR_ERR(config->rtc));
404 		status = PTR_ERR(config->rtc);
405 		goto err_iounmap;
406 	}
407 
408 	/* alarm irqs */
409 	irq = platform_get_irq(pdev, 0);
410 	if (irq < 0) {
411 		dev_err(&pdev->dev, "no update irq?\n");
412 		status = irq;
413 		goto err_clear_platdata;
414 	}
415 
416 	status = request_irq(irq, spear_rtc_irq, 0, pdev->name, config);
417 	if (status) {
418 		dev_err(&pdev->dev, "Alarm interrupt IRQ%d already \
419 				claimed\n", irq);
420 		goto err_clear_platdata;
421 	}
422 
423 	if (!device_can_wakeup(&pdev->dev))
424 		device_init_wakeup(&pdev->dev, 1);
425 
426 	return 0;
427 
428 err_clear_platdata:
429 	platform_set_drvdata(pdev, NULL);
430 	rtc_device_unregister(config->rtc);
431 err_iounmap:
432 	iounmap(config->ioaddr);
433 err_disable_clock:
434 	clk_disable(config->clk);
435 err_clk_put:
436 	clk_put(config->clk);
437 err_kfree:
438 	kfree(config);
439 err_release_region:
440 	release_mem_region(res->start, resource_size(res));
441 
442 	return status;
443 }
444 
445 static int __devexit spear_rtc_remove(struct platform_device *pdev)
446 {
447 	struct spear_rtc_config *config = platform_get_drvdata(pdev);
448 	int irq;
449 	struct resource *res;
450 
451 	/* leave rtc running, but disable irqs */
452 	spear_rtc_disable_interrupt(config);
453 	device_init_wakeup(&pdev->dev, 0);
454 	irq = platform_get_irq(pdev, 0);
455 	if (irq)
456 		free_irq(irq, pdev);
457 	clk_disable(config->clk);
458 	clk_put(config->clk);
459 	iounmap(config->ioaddr);
460 	kfree(config);
461 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
462 	if (res)
463 		release_mem_region(res->start, resource_size(res));
464 	platform_set_drvdata(pdev, NULL);
465 	rtc_device_unregister(config->rtc);
466 
467 	return 0;
468 }
469 
470 #ifdef CONFIG_PM
471 
472 static int spear_rtc_suspend(struct platform_device *pdev, pm_message_t state)
473 {
474 	struct spear_rtc_config *config = platform_get_drvdata(pdev);
475 	int irq;
476 
477 	irq = platform_get_irq(pdev, 0);
478 	if (device_may_wakeup(&pdev->dev)) {
479 		if (!enable_irq_wake(irq))
480 			config->irq_wake = 1;
481 	} else {
482 		spear_rtc_disable_interrupt(config);
483 		clk_disable(config->clk);
484 	}
485 
486 	return 0;
487 }
488 
489 static int spear_rtc_resume(struct platform_device *pdev)
490 {
491 	struct spear_rtc_config *config = platform_get_drvdata(pdev);
492 	int irq;
493 
494 	irq = platform_get_irq(pdev, 0);
495 
496 	if (device_may_wakeup(&pdev->dev)) {
497 		if (config->irq_wake) {
498 			disable_irq_wake(irq);
499 			config->irq_wake = 0;
500 		}
501 	} else {
502 		clk_enable(config->clk);
503 		spear_rtc_enable_interrupt(config);
504 	}
505 
506 	return 0;
507 }
508 
509 #else
510 #define spear_rtc_suspend	NULL
511 #define spear_rtc_resume	NULL
512 #endif
513 
514 static void spear_rtc_shutdown(struct platform_device *pdev)
515 {
516 	struct spear_rtc_config *config = platform_get_drvdata(pdev);
517 
518 	spear_rtc_disable_interrupt(config);
519 	clk_disable(config->clk);
520 }
521 
522 static struct platform_driver spear_rtc_driver = {
523 	.probe = spear_rtc_probe,
524 	.remove = __devexit_p(spear_rtc_remove),
525 	.suspend = spear_rtc_suspend,
526 	.resume = spear_rtc_resume,
527 	.shutdown = spear_rtc_shutdown,
528 	.driver = {
529 		.name = "rtc-spear",
530 	},
531 };
532 
533 module_platform_driver(spear_rtc_driver);
534 
535 MODULE_ALIAS("platform:rtc-spear");
536 MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
537 MODULE_DESCRIPTION("ST SPEAr Realtime Clock Driver (RTC)");
538 MODULE_LICENSE("GPL");
539