From patchwork Fri Oct 4 15:22:41 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Herring X-Patchwork-Id: 280648 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 66C0D2C00A2 for ; Sat, 5 Oct 2013 01:24:28 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 77CFF4A09D; Fri, 4 Oct 2013 17:24:10 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GPSywu7F+bMA; Fri, 4 Oct 2013 17:24:10 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 075DA4A09F; Fri, 4 Oct 2013 17:23:39 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 540F74A090 for ; Fri, 4 Oct 2013 17:23:30 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ebn9gG+UZWk6 for ; Fri, 4 Oct 2013 17:23:23 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 BL_NJABL=ERR(-1.5) (only DNSBL check requested) Received: from mail-ie0-f172.google.com (mail-ie0-f172.google.com [209.85.223.172]) by theia.denx.de (Postfix) with ESMTPS id 1C5964A07A for ; Fri, 4 Oct 2013 17:23:13 +0200 (CEST) Received: by mail-ie0-f172.google.com with SMTP id x13so9672063ief.3 for ; Fri, 04 Oct 2013 08:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=l/LJ5M/rVkXJ3N3w5TKhF/DmEaXZJ2qBm9FoNOHN7jk=; b=Gr22qVNBaCMJsKelzYWcUeWux3k5irFsx8Xre1rxp2XIDSV0jQ2I79fbRHYB9oc9oh LUJbdi+zAbXR3sfbRG7qK0XNR0YuX/LO+iD44eWHmPi1W1xEcAM9gx69plj1O3MFNuhq cYSE/ldayuiTt5HqOwjwKZ2Z/xptCyn1D7Gu6J05axD0hKEBRkMqgtuDtVTRVx0xyPE7 wS1aCKAFTxH9ZmRSFjNWhRsBAIQWirA+R9XuoQ+bR/uVRS9ZBBvY9nXtMD0U4gj8Zo8E bl7nsvVZg2hc+z0O5g2wPdB03dSv0u4aG+Y6lOrUH15afA12X+8O/LM/7VIA/u9nyOPo Tgiw== X-Received: by 10.42.98.76 with SMTP id r12mr1203760icn.59.1380900192019; Fri, 04 Oct 2013 08:23:12 -0700 (PDT) Received: from rob-laptop.calxeda.com ([173.226.190.126]) by mx.google.com with ESMTPSA id f5sm7421998igc.4.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 04 Oct 2013 08:23:11 -0700 (PDT) From: Rob Herring To: u-boot@lists.denx.de Date: Fri, 4 Oct 2013 10:22:41 -0500 Message-Id: <1380900168-23791-3-git-send-email-robherring2@gmail.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1380900168-23791-1-git-send-email-robherring2@gmail.com> References: <1378671174-18535-1-git-send-email-robherring2@gmail.com> <1380900168-23791-1-git-send-email-robherring2@gmail.com> Cc: Rob Herring Subject: [U-Boot] [PATCH v2 2/9] Introduce common timer functions X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Rob Herring Many platforms duplicate pretty much the same timer code yet they all have a 32-bit freerunning counter register. Create a common implementation that minimally requires 2 or 3 defines to add timer support: CONFIG_SYS_TIMER_RATE - Clock rate of the timer counter CONFIG_SYS_TIMER_COUNTER - Address of 32-bit counter CONFIG_SYS_TIMER_COUNTS_DOWN - Define if counter counts down All functions are weak or ifdef'ed so they can still be overriden by any platform. Signed-off-by: Rob Herring --- v2: - Move to common location for all arches - Add extern timer_read_counter declaration - Add new global data timebase_h and timebase_l include/asm-generic/global_data.h | 2 ++ lib/time.c | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h index 77e06fb..0de0bea 100644 --- a/include/asm-generic/global_data.h +++ b/include/asm-generic/global_data.h @@ -72,6 +72,8 @@ typedef struct global_data { #if defined(CONFIG_SYS_I2C) int cur_i2c_bus; /* current used i2c bus */ #endif + unsigned long timebase_h; + unsigned long timebase_l; struct arch_global_data arch; /* architecture-specific data */ } gd_t; #endif diff --git a/lib/time.c b/lib/time.c index 68b8ff4..761272a 100644 --- a/lib/time.c +++ b/lib/time.c @@ -7,11 +7,84 @@ #include #include +#include +#include #ifndef CONFIG_WD_PERIOD # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/ #endif +DECLARE_GLOBAL_DATA_PTR; + +#ifdef CONFIG_SYS_TIMER_RATE +ulong notrace get_tbclk(void) +{ + return CONFIG_SYS_TIMER_RATE; +} +#endif + +#ifdef CONFIG_SYS_TIMER_COUNTER +unsigned long notrace timer_read_counter(void) +{ +#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN + return ~readl(CONFIG_SYS_TIMER_COUNTER); +#else + return readl(CONFIG_SYS_TIMER_COUNTER); +#endif +} +#else +extern unsigned long timer_read_counter(void); +#endif + +unsigned long long __weak notrace get_ticks(void) +{ + unsigned long now = timer_read_counter(); + + /* increment tbu if tbl has rolled over */ + if (now < gd->timebase_l) + gd->timebase_h++; + gd->timebase_l = now; + return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l; +} + +static unsigned long long notrace tick_to_time(unsigned long long tick) +{ + unsigned int div = get_tbclk(); + + tick *= CONFIG_SYS_HZ; + do_div(tick, div); + return tick; +} + +ulong __weak get_timer(ulong base) +{ + return tick_to_time(get_ticks()) - base; +} + +unsigned long __weak notrace timer_get_us(void) +{ + return tick_to_time(get_ticks() * 1000); +} +static unsigned long long usec_to_tick(unsigned long usec) +{ + unsigned long long tick = usec * get_tbclk(); + usec *= get_tbclk(); + do_div(tick, 1000000); + return tick; +} + +void __weak __udelay(unsigned long usec) +{ + unsigned long long tmp; + ulong tmo; + + tmo = usec_to_tick(usec); + tmp = get_ticks() + tmo; /* get current timestamp */ + + while (get_ticks() < tmp) /* loop till event */ + /*NOP*/; +} + /* ------------------------------------------------------------------------- */ void udelay(unsigned long usec)