public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: u-boot@lists.denx.de
Subject: [U-Boot-Users] [PATCH 04/10 v2] [ARM] TQMA31: add new board	with i.MX31 processor
Date: Thu, 10 Jul 2008 12:16:35 +0200	[thread overview]
Message-ID: <20080710101635.GS4595@pengutronix.de> (raw)
In-Reply-To: <20080710094645.C8049242FF@gemini.denx.de>

On Thu, Jul 10, 2008 at 11:46:45AM +0200, Wolfgang Denk wrote:
> In message <20080710093001.GR4595@pengutronix.de> you wrote:
> >
> > How about using the Linux clocksource framework for U-Boot? I did this
> 
> I have to admit that I don't know how big that code is?

Somewhere next to nothing:

/*
 * clock.c - generic clocksource implementation
 *
 * This file contains the clocksource implementation from the Linux
 * kernel originally by John Stultz
 *
 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul at us.ibm.com)
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <common.h>
#include <asm-generic/div64.h>
#include <clock.h>

static struct clocksource *current_clock;
static uint64_t time_ns;

/**
 * get_time_ns - get current timestamp in nanoseconds
 */
uint64_t get_time_ns(void)
{
	struct clocksource *cs = current_clock;
        uint64_t cycle_now, cycle_delta;
        uint64_t ns_offset;

        /* read clocksource: */
        cycle_now = cs->read();

        /* calculate the delta since the last call: */
        cycle_delta = (cycle_now - cs->cycle_last) & cs->mask;

        /* convert to nanoseconds: */
        ns_offset = cyc2ns(cs, cycle_delta);

	cs->cycle_last = cycle_now;

	time_ns += ns_offset;
        return time_ns;
}
EXPORT_SYMBOL(get_time_ns);

/**
 * clocksource_hz2mult - calculates mult from hz and shift
 * @hz:                 Clocksource frequency in Hz
 * @shift_constant:     Clocksource shift factor
 *
 * Helper functions that converts a hz counter
 * frequency to a timsource multiplier, given the
 * clocksource shift value
 */
uint32_t clocksource_hz2mult(uint32_t hz, uint32_t shift_constant)
{
        /*  hz = cyc/(Billion ns)
         *  mult/2^shift  = ns/cyc
         *  mult = ns/cyc * 2^shift
         *  mult = 1Billion/hz * 2^shift
         *  mult = 1000000000 * 2^shift / hz
         *  mult = (1000000000<<shift) / hz
         */
        uint64_t tmp = ((uint64_t)1000000000) << shift_constant;

        tmp += hz/2; /* round for do_div */
        do_div(tmp, hz);

        return (uint32_t)tmp;
}

int is_timeout(uint64_t start_ns, uint64_t time_offset_ns)
{

	if (start_ns + time_offset_ns < get_time_ns())
		return 1;
	else
		return 0;
}
EXPORT_SYMBOL(is_timeout);

void udelay(unsigned long usecs)
{
	uint64_t start = get_time_ns();

	while(!is_timeout(start, usecs * 1000));
}
EXPORT_SYMBOL(udelay);

void mdelay(unsigned long msecs)
{
	uint64_t start = get_time_ns();

	while(!is_timeout(start, msecs * 1000000));
}
EXPORT_SYMBOL(mdelay);

int init_clock(struct clocksource *cs)
{
	current_clock = cs;
	return 0;
}

The clocksource driver itself adds another 100 lines of code (for mx31).
The implementation of udelay shows how to implement timeouts, the main
usage of timing functions.

Regards,
  Sascha



-- 
-- 
 Pengutronix - Linux Solutions for Science and Industry
   Handelsregister:  Amtsgericht Hildesheim, HRA 2686
     Hannoversche Str. 2, 31134 Hildesheim, Germany
   Phone: +49-5121-206917-0 |  Fax: +49-5121-206917-9

  reply	other threads:[~2008-07-10 10:16 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-08  9:33 [U-Boot-Users] [PATCH 04/10 v2] [ARM] TQMA31: add new board with i.MX31 processor Jens Gehrlein
2008-07-08 10:32 ` Jean-Christophe PLAGNIOL-VILLARD
2008-07-08 12:09   ` Jens Gehrlein
2008-07-08 15:24     ` Jean-Christophe PLAGNIOL-VILLARD
2008-07-08 14:31 ` Wolfgang Denk
2008-07-08 15:14   ` Jens Gehrlein
2008-07-09 22:46     ` Wolfgang Denk
2008-07-10  6:22       ` Jens Gehrlein
2008-07-10  6:55         ` Wolfgang Denk
2008-07-10  7:29           ` [U-Boot-Users] [PATCH 04/10 v2] [ARM] TQMA31: add new boardwith " Martin Krause
2008-07-10 20:54         ` [U-Boot-Users] [PATCH 04/10 v2] [ARM] TQMA31: add new board with " Guennadi Liakhovetski
2008-07-10  8:20       ` Sascha Hauer
2008-07-10  8:17     ` Sascha Hauer
2008-07-10  8:49       ` Wolfgang Denk
2008-07-10  9:30         ` Sascha Hauer
2008-07-10  9:46           ` Wolfgang Denk
2008-07-10 10:16             ` Sascha Hauer [this message]
2008-07-08 22:55 ` Guennadi Liakhovetski
2008-07-08 23:16   ` Wolfgang Denk
2008-07-08 23:33     ` Guennadi Liakhovetski
2008-07-09  6:50       ` Wolfgang Denk
2008-07-09  6:32   ` Jens Gehrlein
2008-07-09  7:10     ` Wolfgang Denk
2008-07-09 12:09   ` Jerry Van Baren
     [not found] <mailman.707077.1215511129.5026.u-boot-users@lists.sourceforge.net>
2008-07-08 11:33 ` Heiko Schocher
2008-07-08 11:38   ` Jens Gehrlein
2008-07-08 13:48     ` Heiko Schocher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080710101635.GS4595@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox