[OpenWrt-Devel] [PATCH 1/2] gemini: add rtc-gemini driver
Roman Yeryomin
leroi.lists at gmail.com
Sat Dec 20 18:56:41 EST 2014
Signed-off-by: Roman Yeryomin <roman at advem.lv>
---
target/linux/gemini/Makefile | 2 +-
target/linux/gemini/config-3.10 | 2 +
target/linux/gemini/files/drivers/rtc/rtc-gemini.c | 220 +++++++++++++++++++++
...emini-fix-platform_register_rtc-prototype.patch | 23 ---
.../linux/gemini/patches-3.10/002-gemini-rtc.patch | 51 +++++
5 files changed, 274 insertions(+), 24 deletions(-)
create mode 100644 target/linux/gemini/files/drivers/rtc/rtc-gemini.c
delete mode 100644 target/linux/gemini/patches-3.10/002-arm-gemini-fix-platform_register_rtc-prototype.patch
create mode 100644 target/linux/gemini/patches-3.10/002-gemini-rtc.patch
diff --git a/target/linux/gemini/Makefile b/target/linux/gemini/Makefile
index 82338f4..9228bf2 100644
--- a/target/linux/gemini/Makefile
+++ b/target/linux/gemini/Makefile
@@ -10,7 +10,7 @@ ARCH:=arm
BOARD:=gemini
BOARDNAME:=Cortina Systems CS351x
SUBTARGETS:=raidsonic wiligear
-FEATURES:=squashfs pci
+FEATURES:=squashfs pci rtc
CPU_TYPE:=fa526
MAINTAINER:=Imre Kaloz <kaloz at openwrt.org>
diff --git a/target/linux/gemini/config-3.10 b/target/linux/gemini/config-3.10
index a0ff34c..fdddd10 100644
--- a/target/linux/gemini/config-3.10
+++ b/target/linux/gemini/config-3.10
@@ -134,6 +134,8 @@ CONFIG_PERF_USE_VMALLOC=y
CONFIG_PHYLIB=y
# CONFIG_PREEMPT_RCU is not set
# CONFIG_RCU_STALL_COMMON is not set
+CONFIG_RTC_CLASS=y
+CONFIG_RTC_DRV_GEMINI=y
# CONFIG_SCHED_HRTICK is not set
# CONFIG_SCSI_DMA is not set
CONFIG_SPLIT_PTLOCK_CPUS=999999
diff --git a/target/linux/gemini/files/drivers/rtc/rtc-gemini.c b/target/linux/gemini/files/drivers/rtc/rtc-gemini.c
new file mode 100644
index 0000000..587d812
--- /dev/null
+++ b/target/linux/gemini/files/drivers/rtc/rtc-gemini.c
@@ -0,0 +1,220 @@
+/*
+ * Gemini OnChip RTC
+ *
+ * Copyright (C) 2009 Janos Laube <janos.dev at gmail.com>
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Original code for older kernel 2.6.15 are form Stormlinksemi
+ * first update from Janos Laube for > 2.6.29 kernels
+ *
+ * checkpatch fixes and usage off rtc-lib code
+ * Hans Ulli Kroll <ulli.kroll at googlemail.com>
+ */
+
+#include <linux/rtc.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+
+#include <mach/hardware.h>
+
+#define DRV_NAME "rtc-gemini"
+
+MODULE_DESCRIPTION("RTC driver for Gemini SoC");
+MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll at googlemail.com>");
+MODULE_LICENSE("GPL");
+
+struct gemini_rtc {
+ struct rtc_device *dev;
+ void __iomem *base;
+ int irq;
+};
+
+enum gemini_rtc_offsets {
+ GEMINI_RTC_SECOND = 0x00,
+ GEMINI_RTC_MINUTE = 0x04,
+ GEMINI_RTC_HOUR = 0x08,
+ GEMINI_RTC_DAYS = 0x0C,
+ GEMINI_RTC_ALARM_SECOND = 0x10,
+ GEMINI_RTC_ALARM_MINUTE = 0x14,
+ GEMINI_RTC_ALARM_HOUR = 0x18,
+ GEMINI_RTC_RECORD = 0x1C,
+ GEMINI_RTC_CR = 0x20
+};
+
+static irqreturn_t gemini_rtc_interrupt(int irq, void *dev)
+{
+ return IRQ_HANDLED;
+}
+
+/*
+ * Looks like the RTC in the Gemini SoC is (totaly) broken
+ * We can't read/write directly the time from RTC registers.
+ * We must do some "offset" calculation to get the real time
+ *
+ * The register "day" seams to be fixed, and the register "hour"
+ * has his own mind.
+ *
+ * Maybe we can write directly the hour and days since EPOCH
+ * but in this case the RTC will recalucate to some (other) strange values.
+ * If you write time to the registers you will not read the same values.
+ *
+ * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
+ * the same thing, without the rtc-lib.c calls.
+ */
+
+static int gemini_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+ struct gemini_rtc *rtc = dev_get_drvdata(dev);
+
+ unsigned int days, hour, min, sec;
+ unsigned long offset, time;
+
+ sec = readl(rtc->base + GEMINI_RTC_SECOND);
+ min = readl(rtc->base + GEMINI_RTC_MINUTE);
+ hour = readl(rtc->base + GEMINI_RTC_HOUR);
+ days = readl(rtc->base + GEMINI_RTC_DAYS);
+ offset = readl(rtc->base + GEMINI_RTC_RECORD);
+
+ time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
+
+ rtc_time_to_tm(time, tm);
+ return 0;
+}
+
+/*
+ * Maybe there is some hidden register to care ?
+ * looks like register GEMINI_RTC_DAY can count
+ * 365 days * 179 years >= 65535 (uint16)
+ */
+
+static int gemini_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+ struct gemini_rtc *rtc = dev_get_drvdata(dev);
+ unsigned int sec, min, hour, day;
+ unsigned long offset, time;
+
+ if (tm->tm_year >= 2148) /* EPOCH Year + 179 */
+ return -EINVAL;
+
+ rtc_tm_to_time(tm , &time);
+
+ sec = readl(rtc->base + GEMINI_RTC_SECOND);
+ min = readl(rtc->base + GEMINI_RTC_MINUTE);
+ hour = readl(rtc->base + GEMINI_RTC_HOUR);
+ day = readl(rtc->base + GEMINI_RTC_DAYS);
+
+ offset = time - (day*86400 + hour*3600 + min*60 + sec);
+
+ writel(offset, rtc->base + GEMINI_RTC_RECORD);
+ writel(0x01, rtc->base + GEMINI_RTC_CR);
+ return 0;
+}
+
+static struct rtc_class_ops gemini_rtc_ops = {
+ .read_time = gemini_rtc_read_time,
+ .set_time = gemini_rtc_set_time,
+};
+
+static int gemini_rtc_probe(struct platform_device *pdev)
+{
+ struct gemini_rtc *rtc;
+ struct device *dev = &pdev->dev;
+ struct resource *res;
+ int ret;
+
+ rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
+ if (unlikely(!rtc))
+ return -ENOMEM;
+ platform_set_drvdata(pdev, rtc);
+
+ rtc->irq = platform_get_irq(pdev, 0);
+ if (rtc->irq < 0) {
+ ret = -rtc->irq;
+ goto err_mem;
+ }
+
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res) {
+ ret = -ENODEV;
+ goto err_mem;
+ }
+ rtc->base = devm_ioremap(&pdev->dev, res->start,
+ res->end - res->start + 1);
+
+ ret = request_irq(rtc->irq, gemini_rtc_interrupt,
+ IRQF_SHARED, pdev->name, dev);
+ if (unlikely(ret))
+ goto err_mem;
+
+ rtc->dev = rtc_device_register(pdev->name, dev,
+ &gemini_rtc_ops, THIS_MODULE);
+ if (unlikely(IS_ERR(rtc->dev))) {
+ ret = PTR_ERR(rtc->dev);
+ goto err_irq;
+ }
+ return 0;
+
+err_irq:
+ free_irq(rtc->irq, dev);
+
+err_mem:
+ kfree(rtc);
+ return ret;
+}
+
+static int gemini_rtc_remove(struct platform_device *pdev)
+{
+ struct gemini_rtc *rtc = platform_get_drvdata(pdev);
+ struct device *dev = &pdev->dev;
+
+ free_irq(rtc->irq, dev);
+ rtc_device_unregister(rtc->dev);
+ platform_set_drvdata(pdev, NULL);
+ kfree(rtc);
+
+ return 0;
+}
+
+static struct platform_driver gemini_rtc_driver = {
+ .driver = {
+ .name = DRV_NAME,
+ .owner = THIS_MODULE,
+ },
+ .probe = gemini_rtc_probe,
+ .remove = gemini_rtc_remove,
+};
+
+static int __init gemini_rtc_init(void)
+{
+ int retval;
+
+ retval = platform_driver_register(&gemini_rtc_driver);
+ if (retval == 0)
+ pr_info(DRV_NAME ": registered successfully");
+ return retval;
+}
+
+static void __exit gemini_rtc_exit(void)
+{
+ platform_driver_unregister(&gemini_rtc_driver);
+}
+
+module_init(gemini_rtc_init);
+module_exit(gemini_rtc_exit);
diff --git a/target/linux/gemini/patches-3.10/002-arm-gemini-fix-platform_register_rtc-prototype.patch b/target/linux/gemini/patches-3.10/002-arm-gemini-fix-platform_register_rtc-prototype.patch
deleted file mode 100644
index f789f77..0000000
--- a/target/linux/gemini/patches-3.10/002-arm-gemini-fix-platform_register_rtc-prototype.patch
+++ /dev/null
@@ -1,23 +0,0 @@
---- a/arch/arm/mach-gemini/common.h
-+++ b/arch/arm/mach-gemini/common.h
-@@ -18,9 +18,9 @@ extern void gemini_map_io(void);
- extern void gemini_init_irq(void);
- extern void gemini_timer_init(void);
- extern void gemini_gpio_init(void);
--extern void platform_register_rtc(void);
-
- /* Common platform devices registration functions */
-+extern int platform_register_rtc(void);
- extern int platform_register_uart(void);
- extern int platform_register_pflash(unsigned int size,
- struct mtd_partition *parts,
---- a/arch/arm/mach-gemini/devices.c
-+++ b/arch/arm/mach-gemini/devices.c
-@@ -17,6 +17,7 @@
- #include <mach/irqs.h>
- #include <mach/hardware.h>
- #include <mach/global_reg.h>
-+#include "common.h"
-
- static struct plat_serial8250_port serial_platform_data[] = {
- {
diff --git a/target/linux/gemini/patches-3.10/002-gemini-rtc.patch b/target/linux/gemini/patches-3.10/002-gemini-rtc.patch
new file mode 100644
index 0000000..60ef167
--- /dev/null
+++ b/target/linux/gemini/patches-3.10/002-gemini-rtc.patch
@@ -0,0 +1,51 @@
+--- a/drivers/rtc/Kconfig
++++ b/drivers/rtc/Kconfig
+@@ -984,6 +984,15 @@ config RTC_DRV_BFIN
+ This driver can also be built as a module. If so, the module
+ will be called rtc-bfin.
+
++config RTC_DRV_GEMINI
++ tristate "Gemini SoC RTC"
++ help
++ If you say Y here you will get support for the
++ RTC found on Gemini SoC's.
++
++ This driver can also be built as a module. If so, the module
++ will be called rtc-gemini.
++
+ config RTC_DRV_RS5C313
+ tristate "Ricoh RS5C313"
+ depends on SH_LANDISK
+--- a/drivers/rtc/Makefile
++++ b/drivers/rtc/Makefile
+@@ -51,6 +51,7 @@ obj-$(CONFIG_RTC_DRV_DS3234) += rtc-ds3234.o
+ obj-$(CONFIG_RTC_DRV_EM3027) += rtc-em3027.o
+ obj-$(CONFIG_RTC_DRV_EP93XX) += rtc-ep93xx.o
+ obj-$(CONFIG_RTC_DRV_FM3130) += rtc-fm3130.o
++obj-$(CONFIG_RTC_DRV_GEMINI) += rtc-gemini.o
+ obj-$(CONFIG_RTC_DRV_GENERIC) += rtc-generic.o
+ obj-$(CONFIG_RTC_DRV_IMXDI) += rtc-imxdi.o
+ obj-$(CONFIG_RTC_DRV_ISL1208) += rtc-isl1208.o
+--- a/arch/arm/mach-gemini/common.h
++++ b/arch/arm/mach-gemini/common.h
+@@ -18,9 +18,9 @@ extern void gemini_map_io(void);
+ extern void gemini_init_irq(void);
+ extern void gemini_timer_init(void);
+ extern void gemini_gpio_init(void);
+-extern void platform_register_rtc(void);
+
+ /* Common platform devices registration functions */
++extern int platform_register_rtc(void);
+ extern int platform_register_uart(void);
+ extern int platform_register_pflash(unsigned int size,
+ struct mtd_partition *parts,
+--- a/arch/arm/mach-gemini/devices.c
++++ b/arch/arm/mach-gemini/devices.c
+@@ -17,6 +17,7 @@
+ #include <mach/irqs.h>
+ #include <mach/hardware.h>
+ #include <mach/global_reg.h>
++#include "common.h"
+
+ static struct plat_serial8250_port serial_platform_data[] = {
+ {
--
1.9.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list