Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F3A4EC433F5 for ; Tue, 15 Feb 2022 02:34:19 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 429CC8359C; Tue, 15 Feb 2022 03:34:17 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="E1js03of"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CCCDF8359C; Tue, 15 Feb 2022 03:34:14 +0100 (CET) Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4423E8198A for ; Tue, 15 Feb 2022 03:34:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=stanley.chuys@gmail.com Received: by mail-pl1-x62b.google.com with SMTP id x4so12001854plb.4 for ; Mon, 14 Feb 2022 18:34:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id; bh=5X1Q4v/LNB/Z+BC8pJwBvKFPoGCakBmapbUmBv9yuiI=; b=E1js03ofjzcdjFyMKPmCFpfCaTMBuiy+V4pNnZ3X069V+r/w2WRxokudffz8tSP41r M2WqYEBqz1qV5w9zN43P8I9fEAu1Nmb2l4yXqgeP2D4iTL8ILMg7FduxWiXNAIxe72zm 8sRU1vTIfbeM8m2KtRsntJAQolKhdkWrhCVJCQH1Kl+4k3IOAa7N0Q86ulLVgCO+0I2/ PDMGTEaLtDsPyvlWKc6wpn0mVnNwbsNLq5OhvaTQT5zaR1flmBVX1pKdEFB0Q/zpQXdM Uaqyn0JdlW9nokYlG/dEPYtpCc78K++41p/LC//dQJ7IsxJlDHn8hFeQ1HlXiqI027Vh g0YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=5X1Q4v/LNB/Z+BC8pJwBvKFPoGCakBmapbUmBv9yuiI=; b=ohYgvnYzNeZptVWpJ7UaJMO9w4k3z6brLYmBMwdRamaVn+ZM5TNeUlV+ePYkrS65nd /12Za/c5VoUgJ8vfJlG+LMECFuawXnSQDeavvQ0lyIVoXrVguAs0G/4dWUkP3Ih8ya8b oNd6Tw88q4nJsXvv5H+RBozljA7QaxVUnqt9SjRUXzpZxZfwYviopg5h8gADBomzRZq9 IaEmwB1J+YXxOZ9Snur/qT1s+cxQkzlBPN+nGqeFagXqBmyWk0jNA0rxC8Z3vS+qjnkD nzEBTLjg2T4p78or5bZUL2GtBlWJkNS76YvadCCXYIk4aDvLkXCkqpu08bl6odjH/PfG NJ8Q== X-Gm-Message-State: AOAM531SqNIEMXK9cWeDUu1S5ZMzD/nZ/OcyRTBTqt5ZfiJ2EBCwXgXU pBG6KHjoQoPt/6qFOK6NX08= X-Google-Smtp-Source: ABdhPJzwa9jQtHnTzhUMV3Yor12zv5jGrSdMLWuK/NSNbU+O7bCMUiKKBds41kWH85zDppOi53WSWg== X-Received: by 2002:a17:902:ab55:: with SMTP id ij21mr2033185plb.50.1644892449577; Mon, 14 Feb 2022 18:34:09 -0800 (PST) Received: from localhost.localdomain ([112.78.83.53]) by smtp.gmail.com with ESMTPSA id b13sm36981535pfm.27.2022.02.14.18.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 18:34:09 -0800 (PST) From: Stanley Chu X-Google-Original-From: Stanley Chu To: sjg@chromium.org Cc: yschu@nuvoton.com, kwliu@nuvoton.com, ctcchien@nuvoton.com, avifishman70@gmail.com, tmaimon77@gmail.com, u-boot@lists.denx.de Subject: [PATCH v3 1/1] timer: npcm: Add driver for Nuvoton NPCM SoCs Date: Tue, 15 Feb 2022 10:36:41 +0800 Message-Id: <20220215023641.26653-1-yschu@nuvoton.com> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Enable a periodic timer on NPCM SoCs and implement the get_count timer ops. Signed-off-by: Stanley Chu --- v3: - use driver data for clock setting on different chips - remove unnecessary code of zeroing priv variables - more help description in Kconfig v2: - calculate the prescale value, not hardcode --- drivers/timer/Kconfig | 9 +++ drivers/timer/Makefile | 1 + drivers/timer/npcm-timer.c | 115 +++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 drivers/timer/npcm-timer.c diff --git a/drivers/timer/Kconfig b/drivers/timer/Kconfig index 8913142654..abcc736492 100644 --- a/drivers/timer/Kconfig +++ b/drivers/timer/Kconfig @@ -143,6 +143,15 @@ config NOMADIK_MTU_TIMER The MTU provides 4 decrementing free-running timers. At the moment, only the first timer is used by the driver. +config NPCM_TIMER + bool "Nuvoton NPCM timer support" + depends on TIMER + help + Select this to enable a timer on Nuvoton NPCM SoCs. + NPCM timer module has 5 down-counting timers, only the first timer + is used to implement timer ops. No support for early timer and + boot timer. + config OMAP_TIMER bool "Omap timer support" depends on TIMER diff --git a/drivers/timer/Makefile b/drivers/timer/Makefile index e2bd530eb0..39bda1ea79 100644 --- a/drivers/timer/Makefile +++ b/drivers/timer/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_CADENCE_TTC_TIMER) += cadence-ttc.o obj-$(CONFIG_DESIGNWARE_APB_TIMER) += dw-apb-timer.o obj-$(CONFIG_MPC83XX_TIMER) += mpc83xx_timer.o obj-$(CONFIG_NOMADIK_MTU_TIMER) += nomadik-mtu-timer.o +obj-$(CONFIG_NPCM_TIMER) += npcm-timer.o obj-$(CONFIG_OMAP_TIMER) += omap-timer.o obj-$(CONFIG_RENESAS_OSTM_TIMER) += ostm_timer.o obj-$(CONFIG_RISCV_TIMER) += riscv_timer.o diff --git a/drivers/timer/npcm-timer.c b/drivers/timer/npcm-timer.c new file mode 100644 index 0000000000..4562a6f231 --- /dev/null +++ b/drivers/timer/npcm-timer.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2022 Nuvoton Technology Corp. + */ + +#include +#include +#include +#include +#include + +#define NPCM_TIMER_CLOCK_RATE 1000000UL /* 1MHz timer */ +#define NPCM_TIMER_INPUT_RATE 25000000UL /* Rate of input clock */ +#define NPCM_TIMER_TDR_MASK GENMASK(23, 0) +#define NPCM_TIMER_MAX_VAL NPCM_TIMER_TDR_MASK /* max counter value */ + +/* Register offsets */ +#define TCR0 0x0 /* Timer Control and Status Register */ +#define TICR0 0x8 /* Timer Initial Count Register */ +#define TDR0 0x10 /* Timer Data Register */ + +/* TCR fields */ +#define TCR_MODE_PERIODIC BIT(27) +#define TCR_EN BIT(30) +#define TCR_PRESCALE (NPCM_TIMER_INPUT_RATE / NPCM_TIMER_CLOCK_RATE - 1) + +enum input_clock_type { + INPUT_CLOCK_FIXED, /* input clock rate is fixed */ + INPUT_CLOCK_NON_FIXED +}; + +/** + * struct npcm_timer_priv - private data for npcm timer driver + * npcm timer is a 24-bits down-counting timer. + * + * @last_count: last hw counter value + * @counter: the value to be returned for get_count ops + */ +struct npcm_timer_priv { + void __iomem *base; + u32 last_count; + u64 counter; +}; + +static u64 npcm_timer_get_count(struct udevice *dev) +{ + struct npcm_timer_priv *priv = dev_get_priv(dev); + u32 val; + + /* The timer is counting down */ + val = readl(priv->base + TDR0) & NPCM_TIMER_TDR_MASK; + if (val <= priv->last_count) + priv->counter += priv->last_count - val; + else + priv->counter += priv->last_count + (NPCM_TIMER_MAX_VAL + 1 - val); + priv->last_count = val; + + return priv->counter; +} + +static int npcm_timer_probe(struct udevice *dev) +{ + struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); + struct npcm_timer_priv *priv = dev_get_priv(dev); + enum input_clock_type type = dev_get_driver_data(dev); + struct clk clk; + int ret; + + priv->base = dev_read_addr_ptr(dev); + if (!priv->base) + return -EINVAL; + uc_priv->clock_rate = NPCM_TIMER_CLOCK_RATE; + + if (type == INPUT_CLOCK_NON_FIXED) { + ret = clk_get_by_index(dev, 0, &clk); + if (ret < 0) + return ret; + + ret = clk_set_rate(&clk, NPCM_TIMER_INPUT_RATE); + if (ret < 0) + return ret; + } + + /* + * Configure timer and start + * periodic mode + * timer clock rate = input clock / prescale + */ + writel(0, priv->base + TCR0); + writel(NPCM_TIMER_MAX_VAL, priv->base + TICR0); + writel(TCR_EN | TCR_MODE_PERIODIC | TCR_PRESCALE, + priv->base + TCR0); + + return 0; +} + +static const struct timer_ops npcm_timer_ops = { + .get_count = npcm_timer_get_count, +}; + +static const struct udevice_id npcm_timer_ids[] = { + { .compatible = "nuvoton,npcm845-timer", .data = INPUT_CLOCK_FIXED}, + { .compatible = "nuvoton,npcm750-timer", .data = INPUT_CLOCK_NON_FIXED}, + {} +}; + +U_BOOT_DRIVER(npcm_timer) = { + .name = "npcm_timer", + .id = UCLASS_TIMER, + .of_match = npcm_timer_ids, + .priv_auto = sizeof(struct npcm_timer_priv), + .probe = npcm_timer_probe, + .ops = &npcm_timer_ops, + .flags = DM_FLAG_PRE_RELOC, +}; -- 2.17.1