1 #ifndef __CB_BDK_CLOCK_H__
2 #define __CB_BDK_CLOCK_H__
3 /***********************license start***********************************
4 * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights
5 * reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * * Neither the name of Cavium Inc. nor the names of
21 * its contributors may be used to endorse or promote products
22 * derived from this software without specific prior written
23 * permission.
24 *
25 * This Software, including technical data, may be subject to U.S. export
26 * control laws, including the U.S. Export Administration Act and its
27 * associated regulations, and may be subject to export or import
28 * regulations in other countries.
29 *
30 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
31 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
32 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
33 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
34 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
35 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
36 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
37 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
38 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK
39 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
40 ***********************license end**************************************/
41
42 /* FIXME(dhendrix): added */
43 #include <libbdk-arch/bdk-asm.h>
44 #include <libbdk-arch/bdk-numa.h>
45 /* FIXME(prudolph): added */
46
47 #include <soc/clock.h>
48
49 /**
50 * @file
51 *
52 * Interface to Core, IO and DDR Clock.
53 *
54 * <hr>$Revision: 45089 $<hr>
55 *
56 * @addtogroup hal
57 * @{
58 */
59
60 #define BDK_GTI_RATE 1000000ull
61
62 /**
63 * Enumeration of different Clocks.
64 */
65 typedef enum{
66 BDK_CLOCK_TIME, /**< Clock for telling time with fast access. Uses GTI in core */
67 BDK_CLOCK_MAIN_REF, /**< Main reference clock */
68 BDK_CLOCK_RCLK, /**< Clock used by cores, coherent bus and L2 cache. */
69 BDK_CLOCK_SCLK, /**< Clock used by IO blocks. */
70 } bdk_clock_t;
71
clock_get_rate_slow(bdk_clock_t clock)72 static inline uint64_t clock_get_rate_slow(bdk_clock_t clock)
73 {
74 const uint64_t REF_CLOCK = 50000000;
75
76 switch (clock) {
77 case BDK_CLOCK_TIME:
78 return BDK_GTI_RATE; /* Programed as part of setup */
79 case BDK_CLOCK_MAIN_REF:
80 return REF_CLOCK;
81 case BDK_CLOCK_RCLK:
82 return thunderx_get_core_clock();
83 case BDK_CLOCK_SCLK:
84 return thunderx_get_io_clock();
85 }
86 return 0;
87 }
88
89 /**
90 * Called in __bdk_init to setup the global timer
91 */
92 extern void bdk_clock_setup(bdk_node_t node); /* FIXME(dhendrix): added arg */
93
94 extern uint64_t __bdk_clock_get_count_slow(bdk_clock_t clock);
95 extern uint64_t __bdk_clock_get_rate_slow(bdk_node_t node, bdk_clock_t clock);
96
97 /**
98 * Get cycle count based on the clock type.
99 *
100 * @param clock - Enumeration of the clock type.
101 * @return - Get the number of cycles executed so far.
102 */
103 static inline uint64_t bdk_clock_get_count(bdk_clock_t clock) __attribute__ ((always_inline));
bdk_clock_get_count(bdk_clock_t clock)104 static inline uint64_t bdk_clock_get_count(bdk_clock_t clock)
105 {
106 if (clock == BDK_CLOCK_TIME)
107 {
108 uint64_t clk;
109 BDK_MRS(CNTPCT_EL0, clk);
110 return clk;
111 }
112 else
113 return __bdk_clock_get_count_slow(clock);
114 }
115
116 /**
117 * Get clock rate based on the clock type.
118 *
119 * @param node Node to use in a Numa setup. Can be an exact ID or a special value.
120 * @param clock - Enumeration of the clock type.
121 * @return - return the clock rate.
122 */
123 static inline uint64_t bdk_clock_get_rate(bdk_node_t node, bdk_clock_t clock) __attribute__ ((always_inline, pure));
bdk_clock_get_rate(bdk_node_t node,bdk_clock_t clock)124 static inline uint64_t bdk_clock_get_rate(bdk_node_t node, bdk_clock_t clock)
125 {
126 if (clock == BDK_CLOCK_TIME)
127 return BDK_GTI_RATE; /* Programed as part of setup */
128 else
129 return clock_get_rate_slow(clock);
130 }
131
132 /** @} */
133 #endif /* !__CB_BDK_CLOCK_H__ */
134