Add Goldfish RTC DT enabled device driver
authorMiodrag Dinic <miodrag.dinic@imgtec.com>
Wed, 3 Dec 2014 10:53:17 +0000 (11:53 +0100)
committerMiodrag Dinic <miodrag.dinic@imgtec.com>
Fri, 5 Dec 2014 10:40:30 +0000 (11:40 +0100)
Goldfish real-time clock (RTC) device

Taken from
<AOSP>/external/qemu/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT

Relevant files:
$QEMU/hw/timer/goldfish_timer.c
$KERNEL/drivers/rtc/rtc-goldfish.c

Device properties:
Name: goldfish_rtc
Id: -1
IrqCount: 1
I/O Registers:

0x00 R TIME_LOW        - Get current time low-order 32-bits.
0x04 R TIME_HIGH       - Return current time high 32-bits.
0x08 W ALARM_LOW       - Set low 32-bit value or alarm and arm it.
0x0c W ALARM_HIGH      - Set high 32-bit value of alarm.
0x10 W CLEAR_INTERRUPT - Lower device's irq level.

This device is _very_ similar to the Goldfish timer one,
with the following important differences:

- Values reported are still 64-bit nanoseconds, but
they have a granularity of 1 second, and represent
host-specific values (really 'time() * 1e9')

- The alarm is non-functioning, i.e. writing to
ALARM_LOW / ALARM_HIGH will work, but will never
arm any alarm.

Device tree initialisation example:

  goldfish_rtc@1f006000 {
    interrupts = <0xe>;
    reg = <0x1f006000 0x1000>;
    compatible = "generic,goldfish-rtc";
  };

Signed-off-by: Miodrag Dinic <miodrag.dinic@imgtec.com>
Change-Id: I724c35508ba65dae526c3b05229629cb9f81559f

drivers/rtc/Kconfig
drivers/rtc/Makefile
drivers/rtc/rtc-goldfish.c [new file with mode: 0644]

index b9838130a7b0da42a3bc0f35724c9b53b553096f..88174271f03105a3a0d2c7401e8513ac6d48cd7f 100644 (file)
@@ -1026,6 +1026,12 @@ 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_GOLDFISH
+        tristate "GOLDFISH"
+        depends on GOLDFISH
+        help
+          RTC driver for Goldfish Virtual Platform
+
 config RTC_DRV_RS5C313
        tristate "Ricoh RS5C313"
        depends on SH_LANDISK
index c33f86f1a69b313f84f7c0e5ceb492474a59f892..14f1c956731ffa5ccfce17e8a2e666e96bdb79f6 100644 (file)
@@ -53,6 +53,7 @@ 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_GENERIC)  += rtc-generic.o
+obj-$(CONFIG_RTC_DRV_GOLDFISH)  += rtc-goldfish.o
 obj-$(CONFIG_RTC_DRV_HID_SENSOR_TIME) += rtc-hid-sensor-time.o
 obj-$(CONFIG_RTC_DRV_IMXDI)    += rtc-imxdi.o
 obj-$(CONFIG_RTC_DRV_ISL1208)  += rtc-isl1208.o
diff --git a/drivers/rtc/rtc-goldfish.c b/drivers/rtc/rtc-goldfish.c
new file mode 100644 (file)
index 0000000..829d1c7
--- /dev/null
@@ -0,0 +1,156 @@
+/* drivers/rtc/rtc-goldfish.c
+**
+** Copyright (C) 2007 Google, Inc.
+**
+** This software is licensed under the terms of the GNU General Public
+** License version 2, as published by the Free Software Foundation, and
+** may be copied, distributed, and modified under those terms.
+**
+** 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.
+**
+*/
+
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/export.h>
+#include <linux/slab.h>
+#include <asm/io.h>
+
+enum {
+       TIMER_TIME_LOW          = 0x00, // get low bits of current time and update TIMER_TIME_HIGH
+       TIMER_TIME_HIGH         = 0x04, // get high bits of time at last TIMER_TIME_LOW read
+       TIMER_ALARM_LOW         = 0x08, // set low bits of alarm and activate it
+       TIMER_ALARM_HIGH        = 0x0c, // set high bits of next alarm
+       TIMER_CLEAR_INTERRUPT   = 0x10,
+       TIMER_CLEAR_ALARM       = 0x14
+};
+
+struct goldfish_rtc {
+       void __iomem *base;
+       uint32_t irq;
+       struct rtc_device *rtc;
+};
+
+static irqreturn_t
+goldfish_rtc_interrupt(int irq, void *dev_id)
+{
+       struct goldfish_rtc     *qrtc = dev_id;
+       unsigned long           events = 0;
+       void __iomem *base = qrtc->base;
+
+       writel(1, base + TIMER_CLEAR_INTERRUPT);
+       events = RTC_IRQF | RTC_AF;
+
+       rtc_update_irq(qrtc->rtc, 1, events);
+
+       return IRQ_HANDLED;
+}
+
+static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+       int64_t time;
+       struct goldfish_rtc     *qrtc = platform_get_drvdata(to_platform_device(dev));
+       void __iomem *base = qrtc->base;
+
+       time = readl(base + TIMER_TIME_LOW);
+       time |= (int64_t)readl(base + TIMER_TIME_HIGH) << 32;
+       do_div(time, NSEC_PER_SEC);
+
+       rtc_time_to_tm(time, tm);
+       return 0;
+}
+
+static struct rtc_class_ops goldfish_rtc_ops = {
+//     .ioctl          = goldfish_rtc_ioctl,
+       .read_time      = goldfish_rtc_read_time,
+//     .set_time       = goldfish_rtc_set_time,
+//     .read_alarm     = goldfish_rtc_read_alarm,
+//     .set_alarm      = goldfish_rtc_set_alarm,
+};
+
+
+static int goldfish_rtc_probe(struct platform_device *pdev)
+{
+       int ret;
+       struct resource *r;
+       struct goldfish_rtc *qrtc;
+       unsigned long rtc_dev_len;
+       unsigned long rtc_dev_addr;
+
+       qrtc = kzalloc(sizeof(*qrtc), GFP_KERNEL);
+       if(qrtc == NULL) {
+               ret = -ENOMEM;
+               goto err_qrtc_alloc_failed;
+       }
+       platform_set_drvdata(pdev, qrtc);
+
+       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if(r == NULL) {
+               ret = -ENODEV;
+               goto err_no_io_base;
+       }
+
+       rtc_dev_addr = r->start;
+       rtc_dev_len = resource_size(r);
+       qrtc->base = ioremap(rtc_dev_addr, rtc_dev_len);
+       qrtc->irq = platform_get_irq(pdev, 0);
+       if(qrtc->irq < 0) {
+               ret = -ENODEV;
+               goto err_no_irq;
+       }
+       qrtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
+                                       &goldfish_rtc_ops, THIS_MODULE);
+       if (IS_ERR(qrtc->rtc)) {
+               ret = PTR_ERR(qrtc->rtc);
+               goto err_rtc_device_register_failed;
+       }
+
+       ret = request_irq(qrtc->irq, goldfish_rtc_interrupt, 0, pdev->name, qrtc);
+       if(ret)
+               goto request_irq;
+
+       return 0;
+
+       free_irq(qrtc->irq, qrtc);
+request_irq:
+       rtc_device_unregister(qrtc->rtc);
+err_rtc_device_register_failed:
+err_no_irq:
+err_no_io_base:
+       kfree(qrtc);
+err_qrtc_alloc_failed:
+       return ret;
+}
+
+static int goldfish_rtc_remove(struct platform_device *pdev)
+{
+       struct goldfish_rtc     *qrtc = platform_get_drvdata(pdev);
+       free_irq(qrtc->irq, qrtc);
+       rtc_device_unregister(qrtc->rtc);
+       kfree(qrtc);
+       return 0;
+}
+
+static const struct of_device_id goldfish_rtc_of_match[] = {
+       { .compatible = "generic,goldfish-rtc", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
+
+static struct platform_driver goldfish_rtc = {
+       .probe = goldfish_rtc_probe,
+       .remove = goldfish_rtc_remove,
+       .driver = {
+               .name = "goldfish_rtc",
+               .of_match_table = goldfish_rtc_of_match,
+       }
+};
+
+module_platform_driver(goldfish_rtc);
+