Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2013 Freescale Semiconductor, Inc. |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 7 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 8 | #include <time.h> |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <div64.h> |
| 11 | #include <asm/arch/imx-regs.h> |
| 12 | #include <asm/arch/clock.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 14 | |
| 15 | static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR; |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #define TIMER_LOAD_VAL 0xffffffff |
| 20 | |
| 21 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 22 | { |
| 23 | tick *= CONFIG_SYS_HZ; |
| 24 | do_div(tick, mxc_get_clock(MXC_IPG_CLK)); |
| 25 | |
| 26 | return tick; |
| 27 | } |
| 28 | |
| 29 | static inline unsigned long long us_to_tick(unsigned long long usec) |
| 30 | { |
| 31 | usec = usec * mxc_get_clock(MXC_IPG_CLK) + 999999; |
| 32 | do_div(usec, 1000000); |
| 33 | |
| 34 | return usec; |
| 35 | } |
| 36 | |
| 37 | int timer_init(void) |
| 38 | { |
| 39 | __raw_writel(0, &cur_pit->mcr); |
| 40 | |
| 41 | __raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1); |
| 42 | __raw_writel(0, &cur_pit->tctrl1); |
| 43 | __raw_writel(1, &cur_pit->tctrl1); |
| 44 | |
| 45 | gd->arch.tbl = 0; |
| 46 | gd->arch.tbu = 0; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | unsigned long long get_ticks(void) |
| 52 | { |
| 53 | ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1); |
| 54 | |
| 55 | /* increment tbu if tbl has rolled over */ |
| 56 | if (now < gd->arch.tbl) |
| 57 | gd->arch.tbu++; |
| 58 | gd->arch.tbl = now; |
| 59 | |
| 60 | return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; |
| 61 | } |
| 62 | |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 63 | ulong get_timer(ulong base) |
| 64 | { |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 65 | return tick_to_time(get_ticks()) - base; |
Alison Wang | 035260a | 2013-05-27 22:55:42 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* delay x useconds AND preserve advance timstamp value */ |
| 69 | void __udelay(unsigned long usec) |
| 70 | { |
| 71 | unsigned long long start; |
| 72 | ulong tmo; |
| 73 | |
| 74 | start = get_ticks(); /* get current timestamp */ |
| 75 | tmo = us_to_tick(usec); /* convert usecs to ticks */ |
| 76 | while ((get_ticks() - start) < tmo) |
| 77 | ; /* loop till time has passed */ |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * This function is derived from PowerPC code (timebase clock frequency). |
| 82 | * On ARM it returns the number of timer ticks per second. |
| 83 | */ |
| 84 | ulong get_tbclk(void) |
| 85 | { |
| 86 | return mxc_get_clock(MXC_IPG_CLK); |
| 87 | } |