Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 4 | * Copyright (C) 2008 Renesas Solutions Corp. |
| 5 | * |
| 6 | * (C) Copyright 2003 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 11 | #include <time.h> |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/processor.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 15 | |
Jean-Christophe PLAGNIOL-VILLARD | 9f37e8f | 2008-12-20 15:27:45 +0100 | [diff] [blame] | 16 | #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */ |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 17 | #define CMT_CMCSR_CALIB 0x0000 |
| 18 | #define CMT_MAX_COUNTER (0xFFFFFFFF) |
| 19 | #define CMT_TIMER_RESET (0xFFFF) |
| 20 | |
| 21 | static vu_long cmt0_timer; |
| 22 | |
| 23 | static void cmt_timer_start(unsigned int timer) |
| 24 | { |
| 25 | writew(readw(CMSTR) | 0x01, CMSTR); |
| 26 | } |
| 27 | |
| 28 | static void cmt_timer_stop(unsigned int timer) |
| 29 | { |
| 30 | writew(readw(CMSTR) & ~0x01, CMSTR); |
| 31 | } |
| 32 | |
| 33 | int timer_init(void) |
| 34 | { |
| 35 | cmt0_timer = 0; |
| 36 | /* Divide clock by 32 */ |
| 37 | readw(CMCSR_0); |
| 38 | writew(CMT_CMCSR_INIT, CMCSR_0); |
| 39 | |
| 40 | /* User Device 0 only */ |
| 41 | cmt_timer_stop(0); |
Graeme Russ | 5e669ff | 2011-07-15 02:18:12 +0000 | [diff] [blame] | 42 | writew(CMT_TIMER_RESET, CMCOR_0); |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 43 | cmt_timer_start(0); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | unsigned long long get_ticks(void) |
| 49 | { |
| 50 | return cmt0_timer; |
| 51 | } |
| 52 | |
Nobuhiro Iwamatsu | 6743808 | 2008-12-09 11:32:46 +0900 | [diff] [blame] | 53 | static vu_long cmcnt = 0; |
| 54 | static unsigned long get_usec (void) |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 55 | { |
| 56 | ulong data = readw(CMCNT_0); |
| 57 | |
| 58 | if (data >= cmcnt) |
| 59 | cmcnt = data - cmcnt; |
| 60 | else |
| 61 | cmcnt = (CMT_TIMER_RESET - cmcnt) + data; |
| 62 | |
| 63 | if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER) |
| 64 | cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER); |
| 65 | else |
| 66 | cmt0_timer += cmcnt; |
| 67 | |
| 68 | cmcnt = data; |
Nobuhiro Iwamatsu | 6743808 | 2008-12-09 11:32:46 +0900 | [diff] [blame] | 69 | return cmt0_timer; |
| 70 | } |
| 71 | |
| 72 | /* return msec */ |
| 73 | ulong get_timer(ulong base) |
| 74 | { |
Jean-Christophe PLAGNIOL-VILLARD | 9f37e8f | 2008-12-20 15:27:45 +0100 | [diff] [blame] | 75 | return (get_usec() / 1000) - base; |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 76 | } |
| 77 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 78 | void __udelay(unsigned long usec) |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 79 | { |
Nobuhiro Iwamatsu | 6743808 | 2008-12-09 11:32:46 +0900 | [diff] [blame] | 80 | unsigned long end = get_usec() + usec; |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 81 | |
Nobuhiro Iwamatsu | 6743808 | 2008-12-09 11:32:46 +0900 | [diff] [blame] | 82 | while (get_usec() < end) |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | |
| 86 | unsigned long get_tbclk(void) |
| 87 | { |
Nobuhiro Iwamatsu | befb5cc | 2014-01-08 14:57:30 +0900 | [diff] [blame] | 88 | return CONFIG_SH_CMT_CLK_FREQ; |
Nobuhiro Iwamatsu | 5b6918e | 2008-08-31 22:48:33 +0900 | [diff] [blame] | 89 | } |