1 From 6462d09dad154b69ea6180c0acb2d009627ff1be Mon Sep 17 00:00:00 2001
2 From: Alison Wang <b18965@freescale.com>
3 Date: Thu, 4 Aug 2011 09:59:46 +0800
4 Subject: [PATCH 27/52] Add RTC driver support on MCF5441x platform
6 Support on-chip robust-RTC module on MCF5441x platform.
8 Signed-off-by: Alison Wang <b18965@freescale.com>
10 drivers/rtc/Kconfig | 9 +
11 drivers/rtc/Makefile | 1 +
12 drivers/rtc/rtc-m5441x.c | 638 ++++++++++++++++++++++++++++++++++++++++++++++
13 3 files changed, 648 insertions(+), 0 deletions(-)
14 create mode 100644 drivers/rtc/rtc-m5441x.c
16 --- a/drivers/rtc/Kconfig
17 +++ b/drivers/rtc/Kconfig
18 @@ -928,6 +928,15 @@ config RTC_MCF
20 If you build it as a module it will be call mcf-rtc.
23 + tristate "Freescale Coldfire M5441X platform Real Time Clock"
26 + If you say yes here you will get support for the on-chip Coldfire
27 + Real-Time Clock for mcf5441x platform.
29 + If you build it as a module it will be call rtc-m5441x.
34 --- a/drivers/rtc/Makefile
35 +++ b/drivers/rtc/Makefile
36 @@ -103,3 +103,4 @@ obj-$(CONFIG_RTC_DRV_WM831X) += rtc-wm83
37 obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o
38 obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o
39 obj-$(CONFIG_RTC_MCF) += rtc-mcf.o
40 +obj-$(CONFIG_RTC_M5441X) += rtc-m5441x.o
42 +++ b/drivers/rtc/rtc-m5441x.c
45 + * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. All Rights Reserved.
47 + * Lanttor.Guo@freescale.com
49 + * The code contained herein is licensed under the GNU General Public
50 + * License. You may obtain a copy of the GNU General Public License
51 + * Version 2 or later at the following locations:
53 + * http://www.opensource.org/licenses/gpl-license.html
54 + * http://www.gnu.org/copyleft/gpl.html
58 + * Implementation based on rtc-mcf.c
62 + * RTC Real Time Clock (RTC) Driver
64 + * @file rtc-m5441x.c
65 + * @brief Real Time Clock interface
67 + * This file contains Real Time Clock interface for Linux.
70 +#include <linux/rtc.h>
71 +#include <linux/module.h>
72 +#include <linux/fs.h>
73 +#include <linux/init.h>
74 +#include <linux/interrupt.h>
75 +#include <linux/platform_device.h>
76 +#include <linux/clk.h>
77 +#include <linux/uaccess.h>
78 +#include <asm/mcfsim.h>
79 +#include <linux/slab.h>
80 +#include <linux/io.h>
90 +#define readw(addr) in_be16(addr)
91 +#define writew(val, addr) out_be16((addr), (val))
94 +#define PIT_ALL_ON (MCF_RTC_ISR_2HZ | MCF_RTC_ISR_SAM0 | MCF_RTC_ISR_SAM1 | \
95 + MCF_RTC_ISR_SAM2 | MCF_RTC_ISR_SAM3 | MCF_RTC_ISR_SAM4 | \
96 + MCF_RTC_ISR_SAM5 | MCF_RTC_ISR_SAM6 | MCF_RTC_ISR_SAM7)
98 +#define MAX_PIE_NUM 9
99 +#define MAX_PIE_FREQ 512
100 +const u32 PIE_BIT_DEF[MAX_PIE_NUM][2] = {
101 + {2, MCF_RTC_ISR_2HZ},
102 + {4, MCF_RTC_ISR_SAM0},
103 + {8, MCF_RTC_ISR_SAM1},
104 + {16, MCF_RTC_ISR_SAM2},
105 + {32, MCF_RTC_ISR_SAM3},
106 + {64, MCF_RTC_ISR_SAM4},
107 + {128, MCF_RTC_ISR_SAM5},
108 + {256, MCF_RTC_ISR_SAM6},
109 + {MAX_PIE_FREQ, MCF_RTC_ISR_SAM7},
112 +/* Those are the bits from a classic RTC we want to mimic */
113 +#define RTC_IRQF 0x80 /* any of the following 3 is active */
114 +#define RTC_PF 0x40 /* Periodic interrupt */
115 +#define RTC_AF 0x20 /* Alarm interrupt */
116 +#define RTC_UF 0x10 /* Update interrupt for 1Hz RTC */
118 +#define MCF_RTC_TIME 0
119 +#define MCF_RTC_ALARM 1
121 +struct rtc_plat_data {
122 + struct rtc_device *rtc;
124 + unsigned int irqen;
131 +static const int year_cal_basic = 2112;
133 +#define RTC_VERSION "0.1"
135 +static u32 rtc_freq = 2; /* minimun value for PIE */
136 +static unsigned long rtc_status;
138 +static struct rtc_time g_rtc_alarm = {
147 +static DEFINE_SPINLOCK(rtc_lock);
151 + * This funciton is used to disable RTC register write protection
153 +static void disable_register_write_protection(void)
155 + if (readw(MCF_RTC_SR) & MCF_RTC_SR_WPE) {
156 + writew(0x0000, MCF_RTC_CR);
157 + writew(0x0001, MCF_RTC_CR);
158 + writew(0x0003, MCF_RTC_CR);
159 + writew(0x0002, MCF_RTC_CR);
165 + * This function is used to obtain the RTC time or the alarm value in
168 + * @param time_alarm use MCF_RTC_TIME for RTC time value;
169 + * MCF_RTC_ALARM for alarm value
171 + * @return The RTC time or alarm time in second.
173 +static u32 get_alarm_or_time(struct device *dev, int time_alarm,
174 + struct rtc_time *tm)
176 + dev_dbg(dev, "debug function %s()!\n", __func__);
178 + if (time_alarm == MCF_RTC_TIME) {
179 + /*check register information */
180 + dev_dbg(dev, "RTC_YEARMON:0x%x,RTC_DAYS:0x%x,RTC_HOURMIN:0x%x,"
181 + "RTC_SECONDS:0x%x\n", readw(MCF_RTC_YEARMON),
182 + readw(MCF_RTC_DAYS), readw(MCF_RTC_HOURMIN),
183 + readw(MCF_RTC_SECONDS));
186 + tm->tm_year = year_cal_basic +
187 + (char)(MCF_RTC_YEARMON_YEAR_RD(readw(MCF_RTC_YEARMON)));
190 + MCF_RTC_YEARMON_MON_RD(readw(MCF_RTC_YEARMON)) - 1;
191 + /* get month day */
192 + tm->tm_mday = MCF_RTC_DAYS_DAY_RD(readw(MCF_RTC_DAYS));
194 + tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
196 + /* year minus 1900 */
197 + tm->tm_year = tm->tm_year - 1900;
199 + tm->tm_wday = MCF_RTC_DAYS_DAYWEEK_RD(readw(MCF_RTC_DAYS));
202 + MCF_RTC_HOURMIN_HOURS_RD(readw(MCF_RTC_HOURMIN));
205 + MCF_RTC_HOURMIN_MINUTES_RD(readw(MCF_RTC_HOURMIN));
208 + MCF_RTC_SECONDS_SECONDS_RD(readw(MCF_RTC_SECONDS));
209 + /* no day saving time */
212 + /* check rtc_tm fileds information */
213 + dev_dbg(dev, "RTC TIME --> year:%d,yday:%d,mon:%d,mday:%d,"
214 + "wday:%d,hour:%d,min:%d,sec:%d\n", tm->tm_year,
215 + tm->tm_yday, tm->tm_mon, tm->tm_mday, tm->tm_wday,
216 + tm->tm_hour, tm->tm_min, tm->tm_sec);
218 + } else if (time_alarm == MCF_RTC_ALARM) {
219 + tm->tm_year = year_cal_basic +
220 + (char)MCF_RTC_YEARMON_YEAR_RD
221 + (readw(MCF_RTC_ALRM_YRMON));
223 + MCF_RTC_YEARMON_MON_RD(readw(MCF_RTC_ALRM_YRMON)) - 1;
224 + tm->tm_mday = MCF_RTC_DAYS_DAY_RD(readw(MCF_RTC_ALRM_DAYS));
225 + tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon,
227 + tm->tm_year = tm->tm_year - 1900;
229 + MCF_RTC_DAYS_DAYWEEK_RD(readw(MCF_RTC_ALRM_DAYS));
231 + MCF_RTC_HOURMIN_HOURS_RD(readw(MCF_RTC_ALRM_HM));
233 + MCF_RTC_HOURMIN_MINUTES_RD(readw(MCF_RTC_ALRM_HM));
235 + MCF_RTC_SECONDS_SECONDS_RD(readw(MCF_RTC_ALRM_SEC));
238 + /* debug information */
239 + dev_dbg(dev, "RTC ALARM --> year:%d,yday:%d,mon:%d,mday:%d,"
240 + "wday:%d,hour:%d,min:%d,sec:%d\n", tm->tm_year,
241 + tm->tm_yday, tm->tm_mon, tm->tm_mday, tm->tm_wday,
242 + tm->tm_hour, tm->tm_min, tm->tm_sec);
245 + panic("wrong value for time_alarm=%d\n", time_alarm);
252 + * This function sets the RTC alarm value or the time value.
254 + * @param time_alarm the new alarm value to be updated in the RTC
255 + * @param time use MCF_RTC_TIME for RTC time value;
256 + * MCF_RTC_ALARM for alarm value
258 +static void set_alarm_or_time(struct device *dev, int time_alarm,
259 + struct rtc_time *tm)
263 + dev_dbg(dev, "debug function %s()!\n", __func__);
265 + /* wirte enable setting */
266 + disable_register_write_protection();
268 + if (time_alarm == MCF_RTC_TIME) {
269 + /* check rtc_time fields information */
270 + dev_dbg(dev, "RTC TIME --> year:%d,yday:%d,mon:%d,mday:%d,"
271 + "wday:%d,hour:%d,min:%d,sec:%d\n", tm->tm_year,
272 + tm->tm_yday, tm->tm_mon, tm->tm_mday, tm->tm_wday,
273 + tm->tm_hour, tm->tm_min, tm->tm_sec);
275 + year = ((tm->tm_year + 1900) - year_cal_basic);
276 + /* write RTC_YEARMON register */
277 + writew((year << 8) | (tm->tm_mon + 1), MCF_RTC_YEARMON);
279 + /* write RTC_DAYS register */
280 + writew(MCF_RTC_DAYS_DAYWEEK_SET(tm->tm_wday) |
281 + MCF_RTC_DAYS_DAY_SET(tm->tm_mday), MCF_RTC_DAYS);
283 + /* write RTC_HOURMIN register */
284 + writew(MCF_RTC_HOURMIN_HOURS_SET(tm->tm_hour) |
285 + MCF_RTC_HOURMIN_MINUTES_SET(tm->tm_min),
288 + /* write RTC_SECONDS register */
289 + writew(MCF_RTC_SECONDS_SECONDS_SET
290 + (tm->tm_sec), MCF_RTC_SECONDS);
292 + /* debug information */
293 + dev_dbg(dev, "RTC_YEARMON:0x%x, RTC_DAYS:0x%x, "
294 + "RTC_HOURMIN:0x%x, RTC_SECONDS:0x%x\n",
295 + readw(MCF_RTC_YEARMON), readw(MCF_RTC_DAYS),
296 + readw(MCF_RTC_HOURMIN), readw(MCF_RTC_SECONDS));
297 + } else if (time_alarm == MCF_RTC_ALARM) {
299 + year = ((tm->tm_year + 1900) - year_cal_basic);
300 + /* write RTC_YEARMON register */
301 + writew((year << 8) | (tm->tm_mon + 1), MCF_RTC_ALRM_YRMON);
303 + /* write RTC_DAYS register */
304 + writew(MCF_RTC_DAYS_DAYWEEK_SET(tm->tm_wday) |
305 + MCF_RTC_DAYS_DAY_SET(tm->tm_mday), MCF_RTC_ALRM_DAYS);
307 + /* write RTC_HOURMIN register */
308 + writew(MCF_RTC_HOURMIN_HOURS_SET(tm->tm_hour) |
309 + MCF_RTC_HOURMIN_MINUTES_SET(tm->tm_min),
312 + /* write RTC_SECONDS register */
313 + writew(MCF_RTC_SECONDS_SECONDS_SET
314 + (tm->tm_sec), MCF_RTC_ALRM_SEC);
316 + /* debug information */
317 + dev_dbg(dev, "ALRM_YRMON:0x%x,ALRM_DAYS:0x%x,ALRM_HM:0x%x,"
318 + "ALRM_SEC:0x%x\n", readw(MCF_RTC_ALRM_YRMON),
319 + readw(MCF_RTC_ALRM_DAYS), readw(MCF_RTC_ALRM_HM),
320 + readw(MCF_RTC_ALRM_SEC));
322 + panic("wrong value for time_alarm=%d\n", time_alarm);
327 + * This function updates the RTC alarm registers and then clears all the
328 + * interrupt status bits.
330 + * @param alrm the new alarm value to be updated in the RTC
332 + * @return 0 if successful; non-zero otherwise.
334 +static int rtc_update_alarm(struct device *dev, struct rtc_time *alrm)
336 + /* clear all the interrupt status bits */
337 + disable_register_write_protection();
339 + writew(readw(MCF_RTC_ISR), MCF_RTC_ISR);
341 + set_alarm_or_time(dev, MCF_RTC_ALARM, alrm);
347 + * This function is the RTC interrupt service routine.
349 + * @param irq RTC IRQ number
350 + * @param dev_id device ID which is not used
352 + * @return IRQ_HANDLED as defined in the include/linux/interrupt.h file.
354 +static irqreturn_t mcf_rtc_interrupt(int irq, void *dev_id)
356 + struct platform_device *pdev = dev_id;
357 + struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
361 + spin_lock(&rtc_lock);
363 + /* clear interrupt sources */
364 + status = readw(MCF_RTC_ISR) & readw(MCF_RTC_IER);
366 + disable_register_write_protection();
368 + writew(status, MCF_RTC_ISR);
370 + /* clear alarm interrupt if it has occurred */
371 + if (status & MCF_RTC_ISR_ALM)
372 + status &= ~MCF_RTC_ISR_ALM;
374 + /* update irq data & counter */
375 + if (status & MCF_RTC_ISR_ALM)
376 + events |= (RTC_AF | RTC_IRQF);
377 + if (status & MCF_RTC_ISR_1HZ)
378 + events |= (RTC_UF | RTC_IRQF);
379 + if (status & PIT_ALL_ON)
380 + events |= (RTC_PF | RTC_IRQF);
382 + if ((status & MCF_RTC_ISR_ALM) && rtc_valid_tm(&g_rtc_alarm))
383 + rtc_update_alarm(&pdev->dev, &g_rtc_alarm);
385 + spin_unlock(&rtc_lock);
386 + rtc_update_irq(pdata->rtc, 1, events);
387 + return IRQ_HANDLED;
391 + * clear all interrupts and release the IRQ
393 +static void mcf_rtc_release(struct device *dev)
395 + spin_lock_irq(&rtc_lock);
397 + disable_register_write_protection();
399 + writew(0, MCF_RTC_IER); /* Disable all rtc interrupts */
400 + writew(readw(MCF_RTC_ISR), MCF_RTC_ISR);
401 + spin_unlock_irq(&rtc_lock);
406 + * This function is used to support some ioctl calls directly.
407 + * Other ioctl calls are supported indirectly through the
408 + * arm/common/rtctime.c file.
410 + * @param cmd ioctl command as defined in include/linux/rtc.h
411 + * @param arg value for the ioctl command
413 + * @return 0 if successful or negative value otherwise.
415 +static int mcf_rtc_ioctl(struct device *dev, unsigned int cmd,
420 + disable_register_write_protection();
424 + writew((readw(MCF_RTC_IER) & ~PIT_ALL_ON), MCF_RTC_IER);
427 + if (arg < 2 || arg > MAX_PIE_FREQ || (arg % 2) != 0)
428 + return -EINVAL; /* Also make sure a power of 2Hz */
429 + if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
433 + case RTC_IRQP_READ:
434 + return put_user(rtc_freq, (u32 *) arg);
436 + for (i = 0; i < MAX_PIE_NUM; i++) {
437 + if (PIE_BIT_DEF[i][0] == rtc_freq)
440 + if (i == MAX_PIE_NUM)
442 + spin_lock_irq(&rtc_lock);
443 + writew((readw(MCF_RTC_IER) | PIE_BIT_DEF[i][1]), MCF_RTC_IER);
444 + spin_unlock_irq(&rtc_lock);
447 + spin_lock_irq(&rtc_lock);
448 + writew((readw(MCF_RTC_IER) & ~MCF_RTC_ISR_ALM), MCF_RTC_IER);
449 + spin_unlock_irq(&rtc_lock);
453 + spin_lock_irq(&rtc_lock);
454 + writew((readw(MCF_RTC_IER) | MCF_RTC_ISR_ALM), MCF_RTC_IER);
455 + spin_unlock_irq(&rtc_lock);
458 + case RTC_UIE_OFF: /* UIE is for the 1Hz interrupt */
459 + spin_lock_irq(&rtc_lock);
460 + writew((readw(MCF_RTC_IER) & ~MCF_RTC_ISR_1HZ), MCF_RTC_IER);
461 + spin_unlock_irq(&rtc_lock);
465 + spin_lock_irq(&rtc_lock);
466 + writew((readw(MCF_RTC_IER) | MCF_RTC_ISR_1HZ), MCF_RTC_IER);
467 + spin_unlock_irq(&rtc_lock);
470 + return -ENOIOCTLCMD;
474 + * This function reads the current RTC time into tm in Gregorian date.
476 + * @param tm contains the RTC time value upon return
478 + * @return 0 if successful; non-zero otherwise.
480 +static int mcf_rtc_read_time(struct device *dev, struct rtc_time *tm)
483 + get_alarm_or_time(dev, MCF_RTC_TIME, tm);
490 + * This function sets the internal RTC time based on tm in Gregorian date.
492 + * @param tm the time value to be set in the RTC
494 + * @return 0 if successful; non-zero otherwise.
496 +static int mcf_rtc_set_time(struct device *dev, struct rtc_time *tm)
499 + set_alarm_or_time(dev, MCF_RTC_TIME, tm);
506 + * This function reads the current alarm value into the passed in \b alrm
507 + * argument. It updates the \b alrm's pending field value based on the whether
508 + * an alarm interrupt occurs or not.
510 + * @param alrm contains the RTC alarm value upon return
512 + * @return 0 if successful; non-zero otherwise.
514 +static int mcf_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
517 + get_alarm_or_time(dev, MCF_RTC_ALARM, &alrm->time);
520 + alrm->pending = ((readw(MCF_RTC_ISR) & MCF_RTC_ISR_ALM) != 0) ? 1 : 0;
526 + * This function sets the RTC alarm based on passed in alrm.
528 + * @param alrm the alarm value to be set in the RTC
530 + * @return 0 if successful; non-zero otherwise.
532 +static int mcf_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
536 + spin_lock_irq(&rtc_lock);
538 + disable_register_write_protection();
540 + if (rtc_valid_tm(&alrm->time)) {
541 + if (alrm->time.tm_sec > 59 ||
542 + alrm->time.tm_hour > 23 || alrm->time.tm_min > 59) {
546 + ret = rtc_update_alarm(dev, &alrm->time);
548 + ret = rtc_valid_tm(&alrm->time);
551 + ret = rtc_update_alarm(dev, &alrm->time);
555 + memcpy(&g_rtc_alarm, &alrm->time, sizeof(struct rtc_time));
557 + if (alrm->enabled) {
558 + writew((readw(MCF_RTC_IER) | MCF_RTC_ISR_ALM),
561 + writew((readw(MCF_RTC_IER) & ~MCF_RTC_ISR_ALM),
566 + spin_unlock_irq(&rtc_lock);
572 + * The RTC driver structure
574 +static struct rtc_class_ops mcf_rtc_ops = {
575 + .ioctl = mcf_rtc_ioctl,
576 + .read_time = mcf_rtc_read_time,
577 + .set_time = mcf_rtc_set_time,
578 + .read_alarm = mcf_rtc_read_alarm,
579 + .set_alarm = mcf_rtc_set_alarm,
582 +static int __devinit mcf_rtc_probe(struct platform_device *pdev)
584 + struct rtc_device *rtc;
585 + struct rtc_plat_data *pdata = NULL;
588 + disable_register_write_protection();
589 + /* Clear interrupt before request irq */
590 + writew(0x0100, MCF_RTC_CR);
591 + writew(0x0001, MCF_RTC_IER);
593 + if (!(readw(MCF_RTC_CFG_DATA) & MCF_RTC_CFG_DATA_OSCEN))
594 + writew(MCF_RTC_CFG_DATA_OSCEN, MCF_RTC_CFG_DATA);
596 + pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
600 + pdata->irq = MCFINT_VECBASE + MCFINT_RTC;
601 + if (request_irq(pdata->irq, mcf_rtc_interrupt, IRQF_DISABLED,
602 + pdev->name, pdev) < 0) {
603 + dev_warn(&pdev->dev, "interrupt not available.\n");
607 + if (test_and_set_bit(1, &rtc_status))
610 + rtc = rtc_device_register(pdev->name, &pdev->dev, &mcf_rtc_ops,
613 + ret = PTR_ERR(rtc);
614 + if (pdata->irq >= 0)
615 + free_irq(pdata->irq, pdev);
620 + platform_set_drvdata(pdev, pdata);
622 + dev_dbg(&pdev->dev, "RTC_CR:0x%x, RTC_SR:0x%x\n", readw(MCF_RTC_CR),
623 + readw(MCF_RTC_SR));
625 + printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
629 +static int __devexit mcf_rtc_remove(struct platform_device *pdev)
631 + struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
633 + rtc_device_unregister(pdata->rtc);
634 + if (pdata->irq >= 0)
635 + free_irq(pdata->irq, pdev);
637 + mcf_rtc_release(NULL);
642 + * Contains pointers to the power management callback functions.
644 +MODULE_ALIAS("rtc-m5441x");
645 +static struct platform_driver mcf_rtc_driver = {
647 + .name = "rtc-m5441x",
648 + .owner = THIS_MODULE,
650 + .probe = mcf_rtc_probe,
651 + .remove = __devexit_p(mcf_rtc_remove),
655 + * This function creates the /proc/driver/rtc file and registers the device RTC
656 + * in the /dev/misc directory. It also reads the RTC value from external source
657 + * and setup the internal RTC properly.
659 + * @return -1 if RTC is failed to initialize; 0 is successful.
661 +static int __init mcf_rtc_init(void)
663 + return platform_driver_register(&mcf_rtc_driver);
667 + * This function removes the /proc/driver/rtc file and un-registers the
668 + * device RTC from the /dev/misc directory.
670 +static void __exit mcf_rtc_exit(void)
672 + platform_driver_unregister(&mcf_rtc_driver);
676 +module_init(mcf_rtc_init);
677 +module_exit(mcf_rtc_exit);
679 +MODULE_AUTHOR("Freescale Semiconductor, Inc.");
680 +MODULE_DESCRIPTION("Real Time Clock Driver (MCF)");
681 +MODULE_LICENSE("GPL V2");