Re: [PATCH V3] ARM: NUC900: add RTC driver support for nuc910 and nuc920

From: Wan ZongShun
Date: Wed Nov 25 2009 - 22:11:29 EST


2009/11/25 Alessandro Zummo <a.zummo@xxxxxxxxxxxx>:
> On Wed, 25 Nov 2009 20:15:55 +0800
> Wan ZongShun <mcuos.com@xxxxxxxxx> wrote:
>
>> Dear Alessandro,
>>
>> This is a fixed patch which was tested via rtc test program located in Documentation/rtc.txt.
>>
>> Thanks for your help.
>>
>
> ÂHi, Âa few more notes below
>
>> Signed-off-by: Wan ZongShun <mcuos.com@xxxxxxxxx>
>> ---
>> Âdrivers/rtc/Kconfig   Â|  Â6 +
>> Âdrivers/rtc/Makefile   |  Â1 +
>> Âdrivers/rtc/rtc-nuc900.c | Â334 ++++++++++++++++++++++++++++++++++++++++++++++
>> Â3 files changed, 341 insertions(+), 0 deletions(-)
>> Âcreate mode 100644 drivers/rtc/rtc-nuc900.c
>>
>> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
>> index 3c20dae..266ce62 100644
>> --- a/drivers/rtc/Kconfig
>> +++ b/drivers/rtc/Kconfig
>> @@ -573,6 +573,12 @@ config RTC_DRV_AB3100
>> Â Â Â Â Select this to enable the ST-Ericsson AB3100 Mixed Signal IC RTC
>> Â Â Â Â support. This chip contains a battery- and capacitor-backed RTC.
>>
>> +config RTC_DRV_NUC900
>> + Â Â tristate "NUC910/NUC920 RTC driver"
>> + Â Â depends on RTC_CLASS && ARCH_W90X900
>> + Â Â help
>> + Â Â Â If you say yes here you get support for the RTC subsystem of the
>> + Â Â Â NUC910/NUC920 used in embedded systems.
>>
>> Âcomment "on-CPU RTC drivers"
>>
>> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
>> index aa3fbd5..d511774 100644
>> --- a/drivers/rtc/Makefile
>> +++ b/drivers/rtc/Makefile
>> @@ -53,6 +53,7 @@ obj-$(CONFIG_RTC_MXC) Â Â Â Â Â Â Â += rtc-mxc.o
>> Âobj-$(CONFIG_RTC_DRV_MAX6900) Â Â Â Â+= rtc-max6900.o
>> Âobj-$(CONFIG_RTC_DRV_MAX6902) Â Â Â Â+= rtc-max6902.o
>> Âobj-$(CONFIG_RTC_DRV_MV) Â Â += rtc-mv.o
>> +obj-$(CONFIG_RTC_DRV_NUC900) += rtc-nuc900.o
>> Âobj-$(CONFIG_RTC_DRV_OMAP) Â += rtc-omap.o
>> Âobj-$(CONFIG_RTC_DRV_PCAP) Â += rtc-pcap.o
>> Âobj-$(CONFIG_RTC_DRV_PCF8563) Â Â Â Â+= rtc-pcf8563.o
>> diff --git a/drivers/rtc/rtc-nuc900.c b/drivers/rtc/rtc-nuc900.c
>> new file mode 100644
>> index 0000000..09383e2
>> --- /dev/null
>> +++ b/drivers/rtc/rtc-nuc900.c
>> @@ -0,0 +1,334 @@
>> +/*
>> + * Copyright (c) 2008-2009 Nuvoton technology corporation.
>> + *
>> + * Wan ZongShun <mcuos.com@xxxxxxxxx>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation;version 2 of the License.
>> + *
>> + */
>> +
>> +#include <linux/module.h>
>> +#include <linux/init.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/rtc.h>
>> +#include <linux/delay.h>
>> +#include <linux/io.h>
>> +#include <linux/bcd.h>
>> +
>> +/* RTC Control Registers */
>> +#define REG_RTC_INIR Â Â Â Â 0x00
>> +#define REG_RTC_AER Â Â Â Â Â0x04
>> +#define REG_RTC_FCR Â Â Â Â Â0x08
>> +#define REG_RTC_TLR Â Â Â Â Â0x0C
>> +#define REG_RTC_CLR Â Â Â Â Â0x10
>> +#define REG_RTC_TSSR Â Â Â Â 0x14
>> +#define REG_RTC_DWR Â Â Â Â Â0x18
>> +#define REG_RTC_TAR Â Â Â Â Â0x1C
>> +#define REG_RTC_CAR Â Â Â Â Â0x20
>> +#define REG_RTC_LIR Â Â Â Â Â0x24
>> +#define REG_RTC_RIER Â Â Â Â 0x28
>> +#define REG_RTC_RIIR Â Â Â Â 0x2C
>> +#define REG_RTC_TTR Â Â Â Â Â0x30
>> +
>> +#define RTCSET Â Â Â Â Â Â Â Â Â Â Â 0x01
>> +#define AERRWENB Â Â Â Â Â Â 0x10000
>> +#define INIRRESET Â Â Â Â Â Â0xa5eb1357
>> +#define AERPOWERON Â Â Â Â Â 0xA965
>> +#define AERPOWEROFF Â Â Â Â Â0x0000
>> +#define LEAPYEAR Â Â Â Â Â Â 0x0001
>> +#define TICKENB Â Â Â Â Â Â Â Â Â Â Â0x80
>> +#define TICKINTENB Â Â Â Â Â 0x0002
>> +#define ALARMINTENB Â Â Â Â Â0x0001
>> +#define MODE24 Â Â Â Â Â Â Â Â Â Â Â 0x0001
>> +
>> +struct nuc900_rtc {
>> +   int           irq_num;
>> +   void __iomem      Â*rtc_reg;
>> +   struct rtc_device    *rtcdev;
>> +   spinlock_t       Âlock;
>
> Âused?
>
>> +};
>> +
>> +struct nuc900_bcd_time {
>> + Â Â int bcd_sec;
>> + Â Â int bcd_min;
>> + Â Â int bcd_hour;
>> + Â Â int bcd_mday;
>> + Â Â int bcd_mon;
>> + Â Â int bcd_year;
>> +};
>> +
>> +static irqreturn_t nuc900_rtc_interrupt(int irq, void *_rtc)
>> +{
>> + Â Â struct nuc900_rtc *rtc = _rtc;
>> + Â Â unsigned long events = 0, rtc_irq;
>> +
>> + Â Â rtc_irq = __raw_readl(rtc->rtc_reg + REG_RTC_RIIR);
>> +
>> + Â Â if (rtc_irq & ALARMINTENB) {
>> + Â Â Â Â Â Â rtc_irq &= ~ALARMINTENB;
>> + Â Â Â Â Â Â __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR);
>> + Â Â Â Â Â Â events |= RTC_AF | RTC_IRQF;
>> + Â Â }
>> +
>> + Â Â if (rtc_irq & TICKINTENB) {
>> + Â Â Â Â Â Â rtc_irq &= ~TICKINTENB;
>> + Â Â Â Â Â Â __raw_writel(rtc_irq, rtc->rtc_reg + REG_RTC_RIIR);
>> + Â Â Â Â Â Â events |= RTC_UF | RTC_IRQF;
>> + Â Â }
>> +
>> + Â Â rtc_update_irq(rtc->rtcdev, 1, events);
>> +
>> + Â Â return IRQ_HANDLED;
>> +}
>> +
>> +static void check_rtc_power(struct nuc900_rtc *nuc900_rtc)
>> +{
>> + Â Â unsigned int i;
>> + Â Â __raw_writel(INIRRESET, nuc900_rtc->rtc_reg + REG_RTC_INIR);
>> +
>> + Â Â mdelay(10);
>> +
>> + Â Â __raw_writel(AERPOWERON, nuc900_rtc->rtc_reg + REG_RTC_AER);
>> +
>> + Â Â for (i = 0; i < 1000000; i++) {
>> + Â Â Â Â Â Â if (__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_AER) & AERRWENB)
>> + Â Â Â Â Â Â Â Â Â Â break;
>> + Â Â }
>> +}
>> +
>> +static void nuc900_rtc_bcd2bin(unsigned int timereg,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int calreg, struct rtc_time *tm)
>> +{
>> +   tm->tm_mday   = bcd2bin(calreg >> 0);
>> +   tm->tm_mon   Â= bcd2bin(calreg >> 8);
>> +   tm->tm_year   = bcd2bin(calreg >> 16) + 100;
>> +
>> +   tm->tm_sec   Â= bcd2bin(timereg >> 0);
>> +   tm->tm_min   Â= bcd2bin(timereg >> 8);
>> +   tm->tm_hour   = bcd2bin(timereg >> 16);
>> +
>> + Â Â rtc_valid_tm(tm);
>> +}
>> +
>> +static void nuc900_rtc_bin2bcd(struct rtc_time *settm,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct nuc900_bcd_time *gettm)
>> +{
>> + Â Â gettm->bcd_mday = bin2bcd(settm->tm_mday) << 0;
>> + Â Â gettm->bcd_mon Â= bin2bcd(settm->tm_mon) << 8;
>> + Â Â gettm->bcd_year = bin2bcd(settm->tm_year - 100) << 16;
>> +
>> + Â Â gettm->bcd_sec Â= bin2bcd(settm->tm_sec) << 0;
>> + Â Â gettm->bcd_min Â= bin2bcd(settm->tm_min) << 8;
>> + Â Â gettm->bcd_hour = bin2bcd(settm->tm_hour) << 16;
>> +}
>> +
>> +static int nuc900_update_irq_enable(struct device *dev, unsigned int enabled)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> +
>> + Â Â if (enabled)
>> + Â Â Â Â Â Â __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)|
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â (TICKINTENB), rtc->rtc_reg + REG_RTC_RIER);
>> + Â Â else
>> + Â Â Â Â Â Â __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)&
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â (~TICKINTENB), rtc->rtc_reg + REG_RTC_RIER);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static int nuc900_alarm_irq_enable(struct device *dev, unsigned int enabled)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> +
>> + Â Â if (enabled)
>> + Â Â Â Â Â Â __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)|
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â (ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER);
>> + Â Â else
>> + Â Â Â Â Â Â __raw_writel(__raw_readl(rtc->rtc_reg + REG_RTC_RIER)&
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â (~ALARMINTENB), rtc->rtc_reg + REG_RTC_RIER);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static int nuc900_rtc_read_time(struct device *dev, struct rtc_time *tm)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> + Â Â unsigned int timeval, clrval;
>> +
>> + Â Â timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TLR);
>> + Â Â clrval Â= __raw_readl(rtc->rtc_reg + REG_RTC_CLR);
>> +
>> + Â Â nuc900_rtc_bcd2bin(timeval, clrval, tm);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static int nuc900_rtc_set_time(struct device *dev, struct rtc_time *tm)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> + Â Â struct nuc900_bcd_time gettm;
>> + Â Â unsigned long val;
>> +
>> + Â Â nuc900_rtc_bin2bcd(tm, &gettm);
>> +
>> + Â Â check_rtc_power(rtc);
>> +
>> + Â Â val = gettm.bcd_mday | gettm.bcd_mon | gettm.bcd_year;
>> + Â Â __raw_writel(val, rtc->rtc_reg + REG_RTC_CLR);
>> +
>> + Â Â val = gettm.bcd_sec | gettm.bcd_min | gettm.bcd_hour;
>> + Â Â __raw_writel(val, rtc->rtc_reg + REG_RTC_TLR);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static int nuc900_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> + Â Â unsigned int timeval, carval;
>> +
>> + Â Â timeval = __raw_readl(rtc->rtc_reg + REG_RTC_TAR);
>> + Â Â carval Â= __raw_readl(rtc->rtc_reg + REG_RTC_CAR);
>> +
>> + Â Â nuc900_rtc_bcd2bin(timeval, carval, &alrm->time);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static int nuc900_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>> +{
>> + Â Â struct nuc900_rtc *rtc = dev_get_drvdata(dev);
>> + Â Â struct nuc900_bcd_time tm;
>> + Â Â unsigned long val;
>> +
>> + Â Â nuc900_rtc_bin2bcd(&alrm->time, &tm);
>> +
>> + Â Â check_rtc_power(rtc);
>> +
>> + Â Â val = tm.bcd_mday | tm.bcd_mon | tm.bcd_year;
>> + Â Â __raw_writel(val, rtc->rtc_reg + REG_RTC_CAR);
>> +
>> + Â Â val = tm.bcd_sec | tm.bcd_min | tm.bcd_hour;
>> + Â Â __raw_writel(val, rtc->rtc_reg + REG_RTC_TAR);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static struct rtc_class_ops nuc900_rtc_ops = {
>> + Â Â .read_time = nuc900_rtc_read_time,
>> + Â Â .set_time = nuc900_rtc_set_time,
>> + Â Â .read_alarm = nuc900_rtc_read_alarm,
>> + Â Â .set_alarm = nuc900_rtc_set_alarm,
>> + Â Â .alarm_irq_enable = nuc900_alarm_irq_enable,
>> + Â Â .update_irq_enable = nuc900_update_irq_enable,
>> +};
>> +
>> +static int __devinit nuc900_rtc_probe(struct platform_device *pdev)
>> +{
>> + Â Â struct resource *res;
>> + Â Â struct nuc900_rtc *nuc900_rtc;
>> + Â Â int err = 0;
>> +
>> + Â Â nuc900_rtc = kzalloc(sizeof(struct nuc900_rtc), GFP_KERNEL);
>> + Â Â if (!nuc900_rtc) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "kzalloc nuc900_rtc failed\n");
>> + Â Â Â Â Â Â return -ENOMEM;
>> + Â Â }
>> + Â Â res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> + Â Â if (!res) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "platform_get_resource failed\n");
>> + Â Â Â Â Â Â err = -ENXIO;
>> + Â Â Â Â Â Â goto fail1;
>> + Â Â }
>> +
>> + Â Â if (!request_mem_region(res->start, resource_size(res),
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdev->name)) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "request_mem_region failed\n");
>> + Â Â Â Â Â Â err = -EBUSY;
>> + Â Â Â Â Â Â goto fail1;
>> + Â Â }
>> +
>> + Â Â nuc900_rtc->rtc_reg = ioremap(res->start, resource_size(res));
>> + Â Â if (!nuc900_rtc->rtc_reg) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "ioremap rtc_reg failed\n");
>> + Â Â Â Â Â Â err = -ENOMEM;
>> + Â Â Â Â Â Â goto fail2;
>> + Â Â }
>> +
>> + Â Â nuc900_rtc->irq_num = platform_get_irq(pdev, 0);
>> + Â Â if (request_irq(nuc900_rtc->irq_num, nuc900_rtc_interrupt,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_DISABLED, "nuc900rtc", nuc900_rtc)) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "NUC900 RTC request irq failed\n");
>> + Â Â Â Â Â Â err = -EBUSY;
>> + Â Â Â Â Â Â goto fail3;
>> + Â Â }
>> +
>> + Â Â nuc900_rtc->rtcdev = rtc_device_register(pdev->name, &pdev->dev,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &nuc900_rtc_ops, THIS_MODULE);
>> + Â Â if (IS_ERR(nuc900_rtc->rtcdev)) {
>> + Â Â Â Â Â Â dev_err(&pdev->dev, "rtc device register faild\n");
>> + Â Â Â Â Â Â err = PTR_ERR(nuc900_rtc->rtcdev);
>> + Â Â Â Â Â Â goto fail4;
>> + Â Â }
>> +
>> + Â Â platform_set_drvdata(pdev, nuc900_rtc);
>> + Â Â __raw_writel(__raw_readl(nuc900_rtc->rtc_reg + REG_RTC_TSSR) | MODE24,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nuc900_rtc->rtc_reg + REG_RTC_TSSR);
>> +
>> + Â Â dev_info(&pdev->dev, "NUC900 RTC device probe successfully\n");
>
> Âno banners, rtc framwork will announce the rtc
>
>> + Â Â return 0;
>> +
>> +fail4: Â Â Â free_irq(nuc900_rtc->irq_num, nuc900_rtc);
>> +fail3: Â Â Â iounmap(nuc900_rtc->rtc_reg);
>> +fail2: Â Â Â release_mem_region(res->start, resource_size(res));
>> +fail1: Â Â Â kfree(nuc900_rtc);
>> + Â Â return err;
>> +}
>> +
>> +static int __devexit nuc900_rtc_remove(struct platform_device *pdev)
>> +{
>> + Â Â struct nuc900_rtc *nuc900_rtc = platform_get_drvdata(pdev);
>> + Â Â struct resource *res;
>> +
>> + Â Â rtc_device_unregister(nuc900_rtc->rtcdev);
>> + Â Â free_irq(nuc900_rtc->irq_num, nuc900_rtc);
>> + Â Â iounmap(nuc900_rtc->rtc_reg);
>> +
>> + Â Â res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> + Â Â release_mem_region(res->start, resource_size(res));
>> +
>> + Â Â kfree(nuc900_rtc);
>> +
>> + Â Â platform_set_drvdata(pdev, NULL);
>> +
>> + Â Â return 0;
>> +}
>> +
>> +static struct platform_driver nuc900_rtc_driver = {
>> +   .remove     = __devexit_p(nuc900_rtc_remove),
>
> Â__devexit_p is incoherent with the definition below

Sir, I could not understand you regarding above comment ?
and why ? If so anything can instead of the __devexit_p ?

>> +   .driver     = {
>> +       .name  = "nuc900-rtc",
>> + Â Â Â Â Â Â .owner Â= THIS_MODULE,
>> + Â Â },
>> +};
>> +
>> +static int __init nuc900_rtc_init(void)
>> +{
>> + Â Â return platform_driver_probe(&nuc900_rtc_driver, nuc900_rtc_probe);
>> +}
>> +
>> +static void __exit nuc900_rtc_exit(void)
>> +{
>> + Â Â platform_driver_unregister(&nuc900_rtc_driver);
>> +}
>> +
>> +module_init(nuc900_rtc_init);
>> +module_exit(nuc900_rtc_exit);
>> +
>> +MODULE_AUTHOR("Wan ZongShun <mcuos.com@xxxxxxxxx>");
>> +MODULE_DESCRIPTION("nuc910/nuc920 RTC driver");
>> +MODULE_LICENSE("GPL");
>> +MODULE_ALIAS("platform:nuc900-rtc");
>> --
>> 1.5.6.3
>
>
> --
>
> ÂBest regards,
>
> ÂAlessandro Zummo,
> ÂTower Technologies - Torino, Italy
>
> Âhttp://www.towertech.it
>
>



--
linux-arm-kernel mailing list
linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/