Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp417534pxb; Tue, 1 Feb 2022 02:49:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxx0Sq9gD9O5WslkEF8LzUb0RKPIW3P8prRTisvVrXoHQpsNnFq3uNtHA+q3MmUl9o9J9Xg X-Received: by 2002:a05:6402:2790:: with SMTP id b16mr14915696ede.171.1643712585454; Tue, 01 Feb 2022 02:49:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643712585; cv=none; d=google.com; s=arc-20160816; b=Uzwp2wr3QRtH0GyNEbIa7KfFPoJufqaB6J/vXp5nynF0j+ciSNBRzAeBrwhMrpcB3q 4JYaNVX/PcoKs4BEp3TZUmN8vwvWu96XYEhnw4zBXZuqw3az2G06hbL16UI77O15grVI PHnAbYHGW2iFWLK6RzHRdzcAi9sWZCrLBEKn+Lp2WhmckrgFfaQSbFTPU35Hg99a6Jxf g+qKidaXdQCvNxND4li5dSr8lkpz12LbqoIAEfqpVxrVMdTOGv0AsIxDOg3Hz4r9TfcU c1zT4MUxSOoWNIo6sqz9OGFBlL+RpHQY4o1ehuSQRESOuOBO2fEkM0cMfLl+95RW/9fC od5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Pd246VfumB/5dbDCArTu2zWuPqcLscKLZo9bjD6WnVs=; b=zOUvYIN6btbg3PHH7GzcvGzdWRBPNH42hbd6MWoqFPe0BE8cu4Yr8bIGjB7N883JDZ fTmfSguvUBrxkP9JelcC4DvW427Rrq6JUXhxI6GgLRMcZwzzjWxg6a4r+QrJQyz/GDsx 9eNS5EfDvKFp9kFK3FhUt3vHu38n0WswQ6ud2jI7lZdoKeIsB6i7zZhk5bgCX4si0RbL IUSeTC2mrR89cHLpBj9cicGXUsWX8B8Z30xkhZyu7G6lHv0AjVaPzJY1WgOrayYq/edg YJzPN8/zve8qyW8MMrFVV8oQNymOf7MLsE5hU4nD1JL3/HcXiW/UytO9ikluLMHk6ZL8 erSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hp3si10229167ejc.847.2022.02.01.02.49.20; Tue, 01 Feb 2022 02:49:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355091AbiA3OeP (ORCPT + 99 others); Sun, 30 Jan 2022 09:34:15 -0500 Received: from mout.kundenserver.de ([217.72.192.74]:48419 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355014AbiA3Odx (ORCPT ); Sun, 30 Jan 2022 09:33:53 -0500 Received: from quad ([82.142.10.94]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MXYAj-1mkH0506cp-00Z1EE; Sun, 30 Jan 2022 15:33:42 +0100 From: Laurent Vivier To: linux-kernel@vger.kernel.org Cc: Arnd Bergmann , linux-rtc@vger.kernel.org, Jiaxun Yang , Stephen Boyd , John Stultz , Alexandre Belloni , Alessandro Zummo , Geert Uytterhoeven , linux-m68k@lists.linux-m68k.org, Daniel Lezcano , Thomas Gleixner , Laurent Vivier Subject: [PATCH v14 4/5] clocksource/drivers: Add a goldfish-timer clocksource Date: Sun, 30 Jan 2022 15:33:32 +0100 Message-Id: <20220130143333.552646-5-laurent@vivier.eu> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220130143333.552646-1-laurent@vivier.eu> References: <20220130143333.552646-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:p3tR+/ZFOHSLnDfQfT/zjcHdbQVriftPGoKYJblwNrP8Vlu7Fvr SRgCbY9e0lFICfsnb3Ky5/RFYh0IsLmHDJLYaMIlzRPLZhcv+ZQgst4pQWBattJc1Gl+LwY JbOaSeyBwRv7T4rA66FPoJbJv9fgtzKOqRizAwJPWbtvFsmlJh+RqXlT7CDLEySgr0D7vHZ 0HRj9tonpRPKn34gZ/TtA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:aky6AykgxXo=:4aNApessfYbQERa+Fu+Nfl suhDiIIFFJmgfeGFLpcGeYhKe9GjZ13xJItIM+qoAehn8MavadytAxNZeUbDm5UbBraW7D0qF VVlKxp96S9O3ZrW+9xABjcNCieC/5zB3/BLd+6HKFL9hjV/ewdumNMzi25HFrRJv2/mVgYS7u 6Ont7PXAkxJYawghkFRMBG+3yNTMDjRCJV6lmrCfZVxztHdRvF70s7sanaqRcG74TTO9Vv4Zv 0cjUoz+gvTbaUyStSncyIypINzEkOUuqUsqM/cnIfZxRr+0RGJz4c4sxXbohpFbbzoTofNv3z z2txDPEZ9IXTzJRW9FI/j9j3NSoN+TLVNm18xw0vdD7vM86DXfZoBVSMurvxJ+ngbKZj7x4DH m+hD0XpJWRwuIRl4HAjy1nQtzsLBensXbOaPtpUKDsryxDoFKrYEA6T3GUozcme89vfCkJKag arORU0cfeQvtHi8ohy//XA7++Z+XLVfLM9m9Ght8Jv5fEP05LptW1D7spQ3SnLKYXrSplud53 IDtoWJ/HIq/JNkbvlVdTMZA28U6nG4laqJvGFmjyYZxnYesNZEZN2josNSXGVaODN+gNcqzhy WsHzF3hmylF5diaXWDjdd72kbAGGCSMYgbdFsvm2g42xKMbkylada7nAtc/1lLnmBYWD3GVkS 8e1Q3LjyP4PQX490CJS+PhlI2er4ayywvHDa0/4GpuOeybrmoklQHeqm+YvbF7Vjk+CA= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a clocksource based on the goldfish-rtc device. Move the timer register definition to This kernel implementation is based on the QEMU upstream implementation: https://git.qemu.org/?p=qemu.git;a=blob_plain;f=hw/rtc/goldfish_rtc.c Details related to Goldfish devices can be found in: https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT Signed-off-by: Laurent Vivier --- drivers/clocksource/Kconfig | 7 ++ drivers/clocksource/Makefile | 1 + drivers/clocksource/timer-goldfish.c | 153 +++++++++++++++++++++++++++ drivers/rtc/rtc-goldfish.c | 13 +-- include/clocksource/timer-goldfish.h | 31 ++++++ 5 files changed, 193 insertions(+), 12 deletions(-) create mode 100644 drivers/clocksource/timer-goldfish.c create mode 100644 include/clocksource/timer-goldfish.h diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index cfb8ea0df3b1..94f00374cebb 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -721,4 +721,11 @@ config MICROCHIP_PIT64B modes and high resolution. It is used as a clocksource and a clockevent. +config GOLDFISH_TIMER + bool "Clocksource using goldfish-rtc" + depends on M68K || COMPILE_TEST + depends on RTC_DRV_GOLDFISH + help + Support for the timer/counter of goldfish-rtc + endmenu diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index fa5f624eadb6..12f5d7e8cc2d 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -89,3 +89,4 @@ obj-$(CONFIG_GX6605S_TIMER) += timer-gx6605s.o obj-$(CONFIG_HYPERV_TIMER) += hyperv_timer.o obj-$(CONFIG_MICROCHIP_PIT64B) += timer-microchip-pit64b.o obj-$(CONFIG_MSC313E_TIMER) += timer-msc313e.o +obj-$(CONFIG_GOLDFISH_TIMER) += timer-goldfish.o diff --git a/drivers/clocksource/timer-goldfish.c b/drivers/clocksource/timer-goldfish.c new file mode 100644 index 000000000000..0512d5eabc82 --- /dev/null +++ b/drivers/clocksource/timer-goldfish.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include + +struct goldfish_timer { + struct clocksource cs; + struct clock_event_device ced; + struct resource res; + void __iomem *base; +}; + +static struct goldfish_timer *ced_to_gf(struct clock_event_device *ced) +{ + return container_of(ced, struct goldfish_timer, ced); +} + +static struct goldfish_timer *cs_to_gf(struct clocksource *cs) +{ + return container_of(cs, struct goldfish_timer, cs); +} + +static u64 goldfish_timer_read(struct clocksource *cs) +{ + struct goldfish_timer *timerdrv = cs_to_gf(cs); + void __iomem *base = timerdrv->base; + u32 time_low, time_high; + u64 ticks; + + /* + * time_low: get low bits of current time and update time_high + * time_high: get high bits of time at last time_low read + */ + time_low = gf_ioread32(base + TIMER_TIME_LOW); + time_high = gf_ioread32(base + TIMER_TIME_HIGH); + + ticks = ((u64)time_high << 32) | time_low; + + return ticks; +} + +static int goldfish_timer_set_oneshot(struct clock_event_device *evt) +{ + struct goldfish_timer *timerdrv = ced_to_gf(evt); + void __iomem *base = timerdrv->base; + + gf_iowrite32(0, base + TIMER_ALARM_HIGH); + gf_iowrite32(0, base + TIMER_ALARM_LOW); + gf_iowrite32(1, base + TIMER_IRQ_ENABLED); + + return 0; +} + +static int goldfish_timer_shutdown(struct clock_event_device *evt) +{ + struct goldfish_timer *timerdrv = ced_to_gf(evt); + void __iomem *base = timerdrv->base; + + gf_iowrite32(0, base + TIMER_IRQ_ENABLED); + + return 0; +} + +static int goldfish_timer_next_event(unsigned long delta, + struct clock_event_device *evt) +{ + struct goldfish_timer *timerdrv = ced_to_gf(evt); + void __iomem *base = timerdrv->base; + u64 now; + + now = goldfish_timer_read(&timerdrv->cs); + + now += delta; + + gf_iowrite32(upper_32_bits(now), base + TIMER_ALARM_HIGH); + gf_iowrite32(lower_32_bits(now), base + TIMER_ALARM_LOW); + + return 0; +} + +static irqreturn_t goldfish_timer_irq(int irq, void *dev_id) +{ + struct goldfish_timer *timerdrv = dev_id; + struct clock_event_device *evt = &timerdrv->ced; + void __iomem *base = timerdrv->base; + + gf_iowrite32(1, base + TIMER_CLEAR_INTERRUPT); + + evt->event_handler(evt); + + return IRQ_HANDLED; +} + +int __init goldfish_timer_init(int irq, void __iomem *base) +{ + struct goldfish_timer *timerdrv; + int ret; + + timerdrv = kzalloc(sizeof(*timerdrv), GFP_KERNEL); + if (!timerdrv) + return -ENOMEM; + + timerdrv->base = base; + + timerdrv->ced = (struct clock_event_device){ + .name = "goldfish_timer", + .features = CLOCK_EVT_FEAT_ONESHOT, + .set_state_shutdown = goldfish_timer_shutdown, + .set_state_oneshot = goldfish_timer_set_oneshot, + .set_next_event = goldfish_timer_next_event, + }; + + timerdrv->res = (struct resource){ + .name = "goldfish_timer", + .start = (unsigned long)base, + .end = (unsigned long)base + 0xfff, + }; + + ret = request_resource(&iomem_resource, &timerdrv->res); + if (ret) { + pr_err("Cannot allocate '%s' resource\n", timerdrv->res.name); + return ret; + } + + timerdrv->cs = (struct clocksource){ + .name = "goldfish_timer", + .rating = 400, + .read = goldfish_timer_read, + .mask = CLOCKSOURCE_MASK(64), + .flags = 0, + .max_idle_ns = LONG_MAX, + }; + + clocksource_register_hz(&timerdrv->cs, NSEC_PER_SEC); + + ret = request_irq(irq, goldfish_timer_irq, IRQF_TIMER, + "goldfish_timer", timerdrv); + if (ret) { + pr_err("Couldn't register goldfish-timer interrupt\n"); + return ret; + } + + clockevents_config_and_register(&timerdrv->ced, NSEC_PER_SEC, + 1, 0xffffffff); + + return 0; +} diff --git a/drivers/rtc/rtc-goldfish.c b/drivers/rtc/rtc-goldfish.c index eb1929b0cbb6..59c0f38cc08d 100644 --- a/drivers/rtc/rtc-goldfish.c +++ b/drivers/rtc/rtc-goldfish.c @@ -11,18 +11,7 @@ #include #include #include - -#define TIMER_TIME_LOW 0x00 /* get low bits of current time */ - /* and update TIMER_TIME_HIGH */ -#define TIMER_TIME_HIGH 0x04 /* get high bits of time at last */ - /* TIMER_TIME_LOW read */ -#define TIMER_ALARM_LOW 0x08 /* set low bits of alarm and */ - /* activate it */ -#define TIMER_ALARM_HIGH 0x0c /* set high bits of next alarm */ -#define TIMER_IRQ_ENABLED 0x10 -#define TIMER_CLEAR_ALARM 0x14 -#define TIMER_ALARM_STATUS 0x18 -#define TIMER_CLEAR_INTERRUPT 0x1c +#include struct goldfish_rtc { void __iomem *base; diff --git a/include/clocksource/timer-goldfish.h b/include/clocksource/timer-goldfish.h new file mode 100644 index 000000000000..d39097729b1d --- /dev/null +++ b/include/clocksource/timer-goldfish.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * goldfish-timer clocksource + * Registers definition for the goldfish-timer device + */ + +#ifndef _CLOCKSOURCE_TIMER_GOLDFISH_H +#define _CLOCKSOURCE_TIMER_GOLDFISH_H + +/* + * TIMER_TIME_LOW get low bits of current time and update TIMER_TIME_HIGH + * TIMER_TIME_HIGH get high bits of time at last TIMER_TIME_LOW read + * TIMER_ALARM_LOW set low bits of alarm and activate it + * TIMER_ALARM_HIGH set high bits of next alarm + * TIMER_IRQ_ENABLED enable alarm interrupt + * TIMER_CLEAR_ALARM disarm an existin alarm + * TIMER_ALARM_STATUS alarm status (running or not) + * TIMER_CLEAR_INTERRUPT clear interrupt + */ +#define TIMER_TIME_LOW 0x00 +#define TIMER_TIME_HIGH 0x04 +#define TIMER_ALARM_LOW 0x08 +#define TIMER_ALARM_HIGH 0x0c +#define TIMER_IRQ_ENABLED 0x10 +#define TIMER_CLEAR_ALARM 0x14 +#define TIMER_ALARM_STATUS 0x18 +#define TIMER_CLEAR_INTERRUPT 0x1c + +extern int goldfish_timer_init(int irq, void __iomem *base); + +#endif /* _CLOCKSOURCE_TIMER_GOLDFISH_H */ -- 2.34.1