Received: from mail.onstor.com (66.201.51.107) by exch1.onstor.net
 (10.0.0.225) with Microsoft SMTP Server id 8.1.311.2; Thu, 20 Nov 2008
 07:28:30 -0800
Received: from dalesmta2-2.messageone.com ([129.41.169.249]) by
 mail.onstor.com with Microsoft SMTPSVC(6.0.3790.3959);	 Thu, 20 Nov 2008
 07:28:29 -0800
Received: from ftp.linux-mips.org (ftp.linux-mips.org [213.58.128.207])	by
 dalesmta2-2.messageone.com (8.13.8/8.13.8) with ESMTP id mAKFSPJp023373	for
 <andy.sharp@onstor.com>; Thu, 20 Nov 2008 09:28:28 -0600
Received: from localhost.localdomain ([127.0.0.1]:6350 "EHLO
	ftp.linux-mips.org") by ftp.linux-mips.org with ESMTP	id S23792111AbYKTP2O
 (ORCPT <rfc822;andy.sharp@onstor.com>);	Thu, 20 Nov 2008 15:28:14 +0000
Received: with ECARTIS (v1.0.0; list linux-mips); Thu, 20 Nov 2008 15:27:58
 +0000 (GMT)
Received: from mba.ocn.ne.jp ([122.1.235.107]:49387 "HELO smtp.mba.ocn.ne.jp")
	by ftp.linux-mips.org with SMTP id S23792142AbYKTP0z (ORCPT
	<rfc822;linux-mips@linux-mips.org>); Thu, 20 Nov 2008 15:26:55 +0000
Received: from localhost.localdomain (p2225-ipad206funabasi.chiba.ocn.ne.jp
 [222.145.76.225])	by smtp.mba.ocn.ne.jp (Postfix) with ESMTP	id 1A0DE3F12;
 Fri, 21 Nov 2008 00:26:50 +0900 (JST)
From: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
To: "linux-mips@linux-mips.org" <linux-mips@linux-mips.org>
CC: "ralf@linux-mips.org" <ralf@linux-mips.org>, "rtc-linux@googlegroups.com"
	<rtc-linux@googlegroups.com>, "a.zummo@towertech.it" <a.zummo@towertech.it>
Sender: "linux-mips-bounce@linux-mips.org" <linux-mips-bounce@linux-mips.org>
Date: Thu, 20 Nov 2008 07:26:54 -0800
Subject: [PATCH 3/4] rtc: Add rtc-tx4939 driver
Thread-Topic: [PATCH 3/4] rtc: Add rtc-tx4939 driver
Thread-Index: AclLJKRGfuhQrd51TYGayHFicT+RDg==
Message-ID: <1227194815-16200-1-git-send-email-anemo@mba.ocn.ne.jp>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 0b
X-MS-Exchange-Organization-AuthSource: exch1.onstor.net
X-MS-Has-Attach:
X-Auto-Response-Suppress: All
X-MS-TNEF-Correlator:
x-originalarrivaltime: 20 Nov 2008 15:28:29.0834 (UTC)
 FILETIME=[A3F3CEA0:01C94B24]
errors-to: linux-mips-bounce@linux-mips.org
x-ems-proccessed: 2K3Xl1OQTInXD6xxuA8z3Q==
x-ems-stamp: 2cWeruXG8RVv42uOfY3i6w==
x-messageone-virus-version: vendor=fsecure
 engine=4.65.7400:2.4.4,1.2.40,4.0.164
 definitions=2008-11-20_11:2008-11-19,2008-11-20,2008-11-20 signatures=0
x-messageone-virus-scanned: Clean
x-messageone-envelope-sender: linux-mips-bounce@linux-mips.org
x-messageone-spam-details: rule=m773emszm_notspam policy=m773emszm score=0
 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam
 adjust=0 reason=mlx engine=3.1.0-0810130000 definitions=main-0811200051
x-messageone-spam-score: 0
x-messageone-spam-bar:
x-list: linux-mips
x-archive-position: 21343
x-ecartis-version: Ecartis v1.0.0
x-original-sender: anemo@mba.ocn.ne.jp
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

Add support for RTC in TX4939 SoC.

Signed-off-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
---
 drivers/rtc/Kconfig      |    7 +
 drivers/rtc/Makefile     |    1 +
 drivers/rtc/rtc-tx4939.c |  338 ++++++++++++++++++++++++++++++++++++++++++=
++++
 3 files changed, 346 insertions(+), 0 deletions(-)
 create mode 100644 drivers/rtc/rtc-tx4939.c

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 123092d..80da08f 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -679,4 +679,11 @@ config RTC_DRV_STARFIRE
 	  If you say Y here you will get support for the RTC found on
 	  Starfire systems.
=20
+config RTC_DRV_TX4939
+	tristate "TX4939 SoC"
+	depends on SOC_TX4939
+	help
+	  Driver for the internal RTC (Realtime Clock) module found on
+	  Toshiba TX4939 SoC.
+
 endif # RTC_CLASS
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 6e79c91..84dbffd 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -66,6 +66,7 @@ obj-$(CONFIG_RTC_DRV_SH)	+=3D rtc-sh.o
 obj-$(CONFIG_RTC_DRV_STK17TA8)	+=3D rtc-stk17ta8.o
 obj-$(CONFIG_RTC_DRV_TEST)	+=3D rtc-test.o
 obj-$(CONFIG_RTC_DRV_TWL4030)	+=3D rtc-twl4030.o
+obj-$(CONFIG_RTC_DRV_TX4939)	+=3D rtc-tx4939.o
 obj-$(CONFIG_RTC_DRV_V3020)	+=3D rtc-v3020.o
 obj-$(CONFIG_RTC_DRV_VR41XX)	+=3D rtc-vr41xx.o
 obj-$(CONFIG_RTC_DRV_WM8350)	+=3D rtc-wm8350.o
diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c
new file mode 100644
index 0000000..405790c
--- /dev/null
+++ b/drivers/rtc/rtc-tx4939.c
@@ -0,0 +1,338 @@
+/*
+ * TX4939 internal RTC driver
+ * Based on RBTX49xx patch from CELF patch archive.
+ *
+ * This file is subject to the terms and conditions of the GNU General Pub=
lic
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * (C) Copyright TOSHIBA CORPORATION 2005-2007
+ */
+#include <linux/rtc.h>
+#include <linux/platform_device.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <asm/txx9/tx4939.h>
+
+struct tx4939rtc_plat_data {
+	struct rtc_device *rtc;
+	struct tx4939_rtc_reg __iomem *rtcreg;
+	spinlock_t lock;
+};
+
+static struct tx4939rtc_plat_data *get_tx4939rtc_plat_data(struct device *=
dev)
+{
+	return platform_get_drvdata(to_platform_device(dev));
+}
+
+static int tx4939_rtc_cmd(struct tx4939_rtc_reg __iomem *rtcreg, int cmd)
+{
+	int i =3D 0;
+
+	__raw_writel(cmd, &rtcreg->ctl);
+	/* This might take 30us (next 32.768KHz clock) */
+	while (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_BUSY) {
+		/* timeout on approx. 100us (@ GBUS200MHz) */
+		if (i++ > 200 * 100)
+			return -EBUSY;
+		cpu_relax();
+	}
+	return 0;
+}
+
+static int tx4939_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	int i, ret;
+	unsigned long sec;
+	unsigned char buf[6];
+
+	rtc_tm_to_time(tm, &sec);
+	buf[0] =3D 0;
+	buf[1] =3D 0;
+	buf[2] =3D sec;
+	buf[3] =3D sec >> 8;
+	buf[4] =3D sec >> 16;
+	buf[5] =3D sec >> 24;
+	spin_lock_irq(&pdata->lock);
+	__raw_writel(0, &rtcreg->adr);
+	for (i =3D 0; i < 6; i++)
+		__raw_writel(buf[i], &rtcreg->dat);
+	ret =3D tx4939_rtc_cmd(rtcreg,
+			     TX4939_RTCCTL_COMMAND_SETTIME |
+			     (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
+	spin_unlock_irq(&pdata->lock);
+	return ret;
+}
+
+static int tx4939_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	int i, ret;
+	unsigned long sec;
+	unsigned char buf[6];
+
+	spin_lock_irq(&pdata->lock);
+	ret =3D tx4939_rtc_cmd(rtcreg,
+			     TX4939_RTCCTL_COMMAND_GETTIME |
+			     (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
+	if (ret) {
+		spin_unlock_irq(&pdata->lock);
+		return ret;
+	}
+	__raw_writel(2, &rtcreg->adr);
+	for (i =3D 2; i < 6; i++)
+		buf[i] =3D __raw_readl(&rtcreg->dat);
+	spin_unlock_irq(&pdata->lock);
+	sec =3D (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
+	rtc_time_to_tm(sec, tm);
+	return 0;
+}
+
+static int tx4939_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alr=
m)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	int i, ret;
+	unsigned long sec;
+	unsigned char buf[6];
+
+	if (alrm->time.tm_sec < 0 ||
+	    alrm->time.tm_min < 0 ||
+	    alrm->time.tm_hour < 0 ||
+	    alrm->time.tm_mday < 0 ||
+	    alrm->time.tm_mon < 0 ||
+	    alrm->time.tm_year < 0)
+		return -EINVAL;
+	rtc_tm_to_time(&alrm->time, &sec);
+	buf[0] =3D 0;
+	buf[1] =3D 0;
+	buf[2] =3D sec;
+	buf[3] =3D sec >> 8;
+	buf[4] =3D sec >> 16;
+	buf[5] =3D sec >> 24;
+	spin_lock_irq(&pdata->lock);
+	__raw_writel(0, &rtcreg->adr);
+	for (i =3D 0; i < 6; i++)
+		__raw_writel(buf[i], &rtcreg->dat);
+	ret =3D tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_SETALARM |
+			     (alrm->enabled ? TX4939_RTCCTL_ALME : 0));
+	spin_unlock_irq(&pdata->lock);
+	return ret;
+}
+
+static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *al=
rm)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	int i, ret;
+	unsigned long sec;
+	unsigned char buf[6];
+	u32 ctl;
+
+	spin_lock_irq(&pdata->lock);
+	ret =3D tx4939_rtc_cmd(rtcreg,
+			     TX4939_RTCCTL_COMMAND_GETALARM |
+			     (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALME));
+	if (ret) {
+		spin_unlock_irq(&pdata->lock);
+		return ret;
+	}
+	__raw_writel(2, &rtcreg->adr);
+	for (i =3D 2; i < 6; i++)
+		buf[i] =3D __raw_readl(&rtcreg->dat);
+	ctl =3D __raw_readl(&rtcreg->ctl);
+	alrm->enabled =3D (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
+	alrm->pending =3D (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
+	spin_unlock_irq(&pdata->lock);
+	sec =3D (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
+	rtc_time_to_tm(sec, &alrm->time);
+	return 0;
+}
+
+static irqreturn_t tx4939_rtc_interrupt(int irq, void *dev_id)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev_id);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	unsigned long events =3D RTC_IRQF;
+
+	spin_lock(&pdata->lock);
+	if (__raw_readl(&rtcreg->ctl) & TX4939_RTCCTL_ALMD) {
+		events |=3D RTC_AF;
+		tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
+	}
+	spin_unlock(&pdata->lock);
+	rtc_update_irq(pdata->rtc, 1, events);
+	return IRQ_HANDLED;
+}
+
+static int tx4939_rtc_ioctl(struct device *dev,
+			    unsigned int cmd, unsigned long arg)
+{
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	struct rtc_time tm;
+	struct rtc_wkalrm alarm;
+	void __user *uarg =3D (void __user *)arg;
+
+	switch (cmd) {
+	case RTC_ALM_SET:
+		if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
+			return -EFAULT;
+		alarm.enabled =3D 0;
+		alarm.pending =3D 0;
+		/* keep all date/time in alarm.time */
+		return rtc_set_alarm(pdata->rtc, &alarm);
+	case RTC_AIE_OFF:
+		spin_lock_irq(&pdata->lock);
+		tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP);
+		spin_unlock_irq(&pdata->lock);
+		break;
+	case RTC_AIE_ON:
+		spin_lock_irq(&pdata->lock);
+		tx4939_rtc_cmd(rtcreg, TX4939_RTCCTL_COMMAND_NOP |
+			       TX4939_RTCCTL_ALME);
+		spin_unlock_irq(&pdata->lock);
+		break;
+	default:
+		return -ENOIOCTLCMD;
+	}
+	return 0;
+}
+
+static const struct rtc_class_ops tx4939_rtc_ops =3D {
+	.read_time	=3D tx4939_rtc_read_time,
+	.set_time	=3D tx4939_rtc_set_time,
+	.read_alarm	=3D tx4939_rtc_read_alarm,
+	.set_alarm	=3D tx4939_rtc_set_alarm,
+	.ioctl		=3D tx4939_rtc_ioctl,
+};
+
+static ssize_t tx4939_rtc_nvram_read(struct kobject *kobj,
+				     struct bin_attribute *bin_attr,
+				     char *buf, loff_t pos, size_t size)
+{
+	struct device *dev =3D container_of(kobj, struct device, kobj);
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	ssize_t count;
+
+	spin_lock_irq(&pdata->lock);
+	for (count =3D 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
+	     count++, size--) {
+		__raw_writel(pos++, &rtcreg->adr);
+		*buf++ =3D __raw_readl(&rtcreg->dat);
+	}
+	spin_unlock_irq(&pdata->lock);
+	return count;
+}
+
+static ssize_t tx4939_rtc_nvram_write(struct kobject *kobj,
+				      struct bin_attribute *bin_attr,
+				      char *buf, loff_t pos, size_t size)
+{
+	struct device *dev =3D container_of(kobj, struct device, kobj);
+	struct tx4939rtc_plat_data *pdata =3D get_tx4939rtc_plat_data(dev);
+	struct tx4939_rtc_reg __iomem *rtcreg =3D pdata->rtcreg;
+	ssize_t count;
+
+	spin_lock_irq(&pdata->lock);
+	for (count =3D 0; size > 0 && pos < TX4939_RTC_REG_RAMSIZE;
+	     count++, size--) {
+		__raw_writel(pos++, &rtcreg->adr);
+		__raw_writel(*buf++, &rtcreg->dat);
+	}
+	spin_unlock_irq(&pdata->lock);
+	return count;
+}
+
+static struct bin_attribute tx4939_rtc_nvram_attr =3D {
+	.attr =3D {
+		.name =3D "nvram",
+		.mode =3D S_IRUGO | S_IWUSR,
+	},
+	.size =3D TX4939_RTC_REG_RAMSIZE,
+	.read =3D tx4939_rtc_nvram_read,
+	.write =3D tx4939_rtc_nvram_write,
+};
+
+static int __init tx4939_rtc_probe(struct platform_device *pdev)
+{
+	struct rtc_device *rtc;
+	struct tx4939rtc_plat_data *pdata;
+	struct resource *res;
+	int irq, ret;
+
+	res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -ENODEV;
+	irq =3D platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return -ENODEV;
+	pdata =3D devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return -ENOMEM;
+
+	if (!devm_request_mem_region(&pdev->dev, res->start,
+				     res->end - res->start + 1, pdev->name))
+		return -EBUSY;
+	pdata->rtcreg =3D devm_ioremap(&pdev->dev, res->start,
+				     res->end - res->start + 1);
+	if (!pdata->rtcreg)
+		return -EBUSY;
+
+	spin_lock_init(&pdata->lock);
+	tx4939_rtc_cmd(pdata->rtcreg, TX4939_RTCCTL_COMMAND_NOP);
+	if (devm_request_irq(&pdev->dev, irq, tx4939_rtc_interrupt,
+			     IRQF_DISABLED | IRQF_SHARED,
+			     pdev->name, &pdev->dev) < 0)
+		return -EBUSY;
+	rtc =3D rtc_device_register(pdev->name, &pdev->dev,
+				  &tx4939_rtc_ops, THIS_MODULE);
+	if (IS_ERR(rtc))
+		return PTR_ERR(rtc);
+	pdata->rtc =3D rtc;
+	platform_set_drvdata(pdev, pdata);
+	ret =3D sysfs_create_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
+	if (ret)
+		rtc_device_unregister(rtc);
+	return ret;
+}
+
+static int __exit tx4939_rtc_remove(struct platform_device *pdev)
+{
+	struct tx4939rtc_plat_data *pdata =3D platform_get_drvdata(pdev);
+	struct rtc_device *rtc =3D pdata->rtc;
+
+	sysfs_remove_bin_file(&pdev->dev.kobj, &tx4939_rtc_nvram_attr);
+	rtc_device_unregister(rtc);
+	platform_set_drvdata(pdev, NULL);
+	return 0;
+}
+
+static struct platform_driver tx4939_rtc_driver =3D {
+	.remove		=3D __exit_p(tx4939_rtc_remove),
+	.driver		=3D {
+		.name	=3D "tx4939rtc",
+		.owner	=3D THIS_MODULE,
+	},
+};
+
+static int __init tx4939rtc_init(void)
+{
+	return platform_driver_probe(&tx4939_rtc_driver, tx4939_rtc_probe);
+}
+
+static void __exit tx4939rtc_exit(void)
+{
+	platform_driver_unregister(&tx4939_rtc_driver);
+}
+
+module_init(tx4939rtc_init);
+module_exit(tx4939rtc_exit);
+
+MODULE_DESCRIPTION("TX4939 internal RTC driver");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:tx4939rtc");
--=20
1.5.6.3


