RE: [PATCH v2 2/3] riscv: timer: add timer_get_boot_us for BOOTSTAGE

Chanho Park
2023-09-04T07:41:19Z

Hi,

> -----Original Message-----
> From: Leo Liang <ycliang@andestech.com>
> Sent: Monday, September 4, 2023 4:01 PM
> To: Chanho Park <chanho61.park@samsung.com>
> Cc: Rick Chen <rick@andestech.com>; Simon Glass <sjg@chromium.org>; u-
> boot@lists.denx.de
> Subject: Re: [PATCH v2 2/3] riscv: timer: add timer_get_boot_us for
> BOOTSTAGE
>
> Hi Chanho,
>
> On Mon, Aug 28, 2023 at 06:49:37PM +0900, Chanho Park wrote:
> > timer_get_boot_us function is required to record the boot stages as
> > us-based timestamp.
> >
> > Signed-off-by: Chanho Park <chanho61.park@samsung.com>
> > ---
> > drivers/timer/riscv_timer.c | 21 +++++++++++++++++++++
> > 1 file changed, 21 insertions(+)
> >
> > diff --git a/drivers/timer/riscv_timer.c b/drivers/timer/riscv_timer.c
> > index 3627ed79b819..6cb589fcdc45 100644
> > --- a/drivers/timer/riscv_timer.c
> > +++ b/drivers/timer/riscv_timer.c
> > @@ -11,6 +11,7 @@
> > */
> >
> > #include <common.h>
> > +#include <div64.h>
> > #include <dm.h>
> > #include <errno.h>
> > #include <timer.h>
> > @@ -50,6 +51,26 @@ u64 notrace timer_early_get_count(void) } #endif
> >
> > +#if CONFIG_IS_ENABLED(RISCV_SMODE) && CONFIG_IS_ENABLED(BOOTSTAGE)
> > +ulong timer_get_boot_us(void) {
> > + int ret;
> > + u64 ticks = 0;
> > + u32 rate;
> > +
> > + ret = dm_timer_init();
> > + if (!ret) {
> > + rate = timer_get_rate(gd->timer);
> > + timer_get_count(gd->timer, &ticks);
> > + } else {
> > + rate = RISCV_SMODE_TIMER_FREQ;
> > + ticks = riscv_timer_get_count(NULL);
> > + }
> > +
> > + return lldiv(ticks * 1000, (rate / 1000));
>
> Could you elaborate a little how this formula is derived?

Sure. Below is the original formula. To avoid zero of (tick/rate), I split
the calculation to ticks and rate, respectively.

Time(us) = (ticks / rate) * 1000000
Or (ticks * 1000000) / rate
Or tick / (rate / 10000000)

Best Regards,
Chanho Park