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