From patchwork Thu Mar 17 23:12:57 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rich Felker X-Patchwork-Id: 8975501 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0A93A9F1D3 for ; Thu, 28 Apr 2016 21:59:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EFC8D20259 for ; Thu, 28 Apr 2016 21:59:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CDA9D20219 for ; Thu, 28 Apr 2016 21:59:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753330AbcD1V7A (ORCPT ); Thu, 28 Apr 2016 17:59:00 -0400 Received: from 216-12-86-13.cv.mvl.ntelos.net ([216.12.86.13]:57859 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752634AbcD1V67 (ORCPT ); Thu, 28 Apr 2016 17:58:59 -0400 Received: from dalias by brightrain.aerifal.cx with local (Exim 3.15 #2) id 1avtxZ-000806-00; Thu, 28 Apr 2016 21:58:57 +0000 Message-Id: <2a99114ee07dbedc6b686c0cdb9dc8542a2955fc.1461880061.git.dalias@libc.org> In-Reply-To: References: From: Rich Felker Date: Thu, 17 Mar 2016 23:12:57 +0000 Subject: [PATCH 4/7] clocksource: add J-Core PIT/RTC driver To: linux-sh@vger.kernel.org Cc: Yoshinori Sato Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00, DATE_IN_PAST_96_XX, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Rich Felker --- drivers/clocksource/Kconfig | 4 + drivers/clocksource/Makefile | 2 + drivers/clocksource/jcore-pit.c | 176 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 drivers/clocksource/jcore-pit.c diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index c346be6..f41f9ee 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -411,4 +411,8 @@ config CLKSRC_ST_LPC Enable this option to use the Low Power controller timer as clocksource. +config CLKSRC_JCORE_PIT + bool "J-Core integrated PIT/RTC" + depends on GENERIC_CLOCKEVENTS + endmenu diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index dc2b899..cf9f1d4 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -65,3 +65,5 @@ obj-$(CONFIG_H8300_TMR16) += h8300_timer16.o obj-$(CONFIG_H8300_TPU) += h8300_tpu.o obj-$(CONFIG_CLKSRC_ST_LPC) += clksrc_st_lpc.o obj-$(CONFIG_X86_NUMACHIP) += numachip.o + +obj-$(CONFIG_CLKSRC_JCORE_PIT) += jcore-pit.o diff --git a/drivers/clocksource/jcore-pit.c b/drivers/clocksource/jcore-pit.c new file mode 100644 index 0000000..a4fde51 --- /dev/null +++ b/drivers/clocksource/jcore-pit.c @@ -0,0 +1,176 @@ +/* + * J-Core SoC PIT/RTC driver + * + * Copyright (C) 2015-2016 Smart Energy Instruments, Inc. + * + * This file is subject to the terms and conditions of the GNU General Public + * License. See the file "COPYING" in the main directory of this archive + * for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static unsigned char __iomem *pit_base; +static int pit_irq; +static u32 percpu_offset; +static u32 enable_val; + +static struct clock_event_device __percpu *pit_percpu; + +#define REG_PITEN 0x00 +#define REG_THROT 0x10 +#define REG_COUNT 0x14 +#define REG_BUSPD 0x18 +#define REG_SECHI 0x20 +#define REG_SECLO 0x24 +#define REG_NSEC 0x28 + +static cycle_t rtc_read(struct clocksource *cs) +{ + u32 sechi, seclo, nsec, sechi0, seclo0; + + sechi = __raw_readl(pit_base + REG_SECHI); + seclo = __raw_readl(pit_base + REG_SECLO); + do { + sechi0 = sechi; + seclo0 = seclo; + nsec = __raw_readl(pit_base + REG_NSEC); + sechi = __raw_readl(pit_base + REG_SECHI); + seclo = __raw_readl(pit_base + REG_SECLO); + } while (sechi0 != sechi || seclo0 != seclo); + + return ((u64)sechi << 32 | seclo) * 1000000000 + nsec; +} + +struct clocksource rtc_csd = { + .name = "rtc", + .rating = 400, + .read = rtc_read, + .mult = 1, + .shift = 0, + .mask = CLOCKSOURCE_MASK(64), + .flags = CLOCK_SOURCE_IS_CONTINUOUS, +}; + +static int pit_disable(struct clock_event_device *ced) +{ + unsigned cpu = smp_processor_id(); + writel(0, pit_base + percpu_offset*cpu + REG_PITEN); + return 0; +} + +static int pit_set(unsigned long delta, struct clock_event_device *ced) +{ + unsigned cpu = smp_processor_id(); + + pit_disable(ced); + + writel(delta, pit_base + percpu_offset*cpu + REG_THROT); + writel(enable_val, pit_base + percpu_offset*cpu + REG_PITEN); + + return 0; +} + +static int pit_set_periodic(struct clock_event_device *ced) +{ + unsigned cpu = smp_processor_id(); + unsigned long per = readl(pit_base + percpu_offset*cpu + REG_BUSPD); + + return pit_set(DIV_ROUND_CLOSEST(1000000000, HZ*per), ced); +} + +static int pit_local_init(struct clock_event_device *ced) +{ + unsigned cpu = smp_processor_id(); + unsigned long per = readl(pit_base + percpu_offset*cpu + REG_BUSPD); + + pr_info("Local PIT init on cpu %u\n", cpu); + + ced->name = "pit"; + ced->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT + | CLOCK_EVT_FEAT_PERCPU; + ced->cpumask = cpumask_of(cpu); + ced->rating = 400; + ced->irq = pit_irq; + ced->set_state_shutdown = pit_disable; + ced->set_state_periodic = pit_set_periodic; + ced->set_state_oneshot = pit_disable; + ced->set_next_event = pit_set; + + clockevents_config_and_register(ced, DIV_ROUND_CLOSEST(1000000000, per), + 1, 0xffffffff); + + pit_set_periodic(ced); + + return 0; +} + +static int pit_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) +{ + switch (action & ~CPU_TASKS_FROZEN) { + case CPU_STARTING: + pit_local_init(this_cpu_ptr(pit_percpu)); + break; + } + return NOTIFY_OK; +} + +static struct notifier_block pit_cpu_nb = { + .notifier_call = pit_cpu_notify, +}; + +static irqreturn_t timer_interrupt(int irq, void *dev_id) +{ + struct clock_event_device *ced = this_cpu_ptr(dev_id); + + if (clockevent_state_oneshot(ced)) pit_disable(ced); + + ced->event_handler(ced); + + return IRQ_HANDLED; +} + +static void __init pit_init(struct device_node *node) +{ + unsigned long hwirq; + int err; + + pit_base = of_iomap(node, 0); + pit_irq = irq_of_parse_and_map(node, 0); + of_property_read_u32(node, "cpu-offset", &percpu_offset); + + pr_info("Initializing J-Core PIT at %p IRQ %d\n", pit_base, pit_irq); + + clocksource_register_hz(&rtc_csd, 1000000000); + + pit_percpu = alloc_percpu(struct clock_event_device); + register_cpu_notifier(&pit_cpu_nb); + + err = request_irq(pit_irq, timer_interrupt, + IRQF_TIMER | IRQF_PERCPU, "pit", pit_percpu); + if (err) pr_err("pit irq request failed: %d\n", err); + + hwirq = irq_get_irq_data(pit_irq)->hwirq; + enable_val = (1<<26) | ((hwirq&0x3c)<<18) | (hwirq<<12); + + pit_local_init(this_cpu_ptr(pit_percpu)); +} + +CLOCKSOURCE_OF_DECLARE(jcore_pit, "jcore,pit", pit_init);