summaryrefslogtreecommitdiff
path: root/chip/npcx/clock_chip.h
blob: 702b55c52a55480ac2b718490f2d9b0a4d19de33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* NPCX-specific clock module for Chrome EC */

#ifndef __CROS_EC_CLOCK_CHIP_H
#define __CROS_EC_CLOCK_CHIP_H

/*
 * EC clock tree plan: (Default OSC_CLK is 40MHz.)
 *
 * Target OSC_CLK for NPCX7 is 90MHz, FMCLK is 45MHz, CPU and APBs is 15MHz.
 * Target OSC_CLK for NPCX5 is 30MHz, FMCLK is 30MHz, CPU and APBs is 15MHz.
 */
#if defined(CHIP_FAMILY_NPCX5)
/*
 * NPCX5 clock tree: (Please refer Figure 55. for more information.)
 *
 * Suggestion:
 * - OSC_CLK >= 30MHz, FPRED should be 1, else 0.
 *   (Keep FMCLK in 30-50 MHz possibly which is tested strictly.)
 */
/* Target OSC_CLK freq */
#define OSC_CLK 30000000
/* Core clock prescaler */
#if (OSC_CLK >= 30000000)
#define FPRED 1 /* CORE_CLK = OSC_CLK(FMCLK)/2 */
#else
#define FPRED 0 /* CORE_CLK = OSC_CLK(FMCLK) */
#endif
/* Core domain clock */
#define CORE_CLK (OSC_CLK / (FPRED + 1))
/* FMUL clock */
#define FMCLK OSC_CLK
/* APBs source clock */
#define APBSRC_CLK CORE_CLK
/* APB1 clock divider */
#define APB1DIV 3 /* Default APB1 clock = CORE_CLK/4 */
/* APB2 clock divider */
#define APB2DIV 0 /* Let APB2 = CORE_CLK since UART baudrate tolerance */
#elif NPCX_FAMILY_VERSION >= NPCX_FAMILY_NPCX7
/*
 * NPCX7 clock tree: (Please refer Figure 58. for more information.)
 *
 * Suggestion:
 * - OSC_CLK >= 80MHz, XF_RANGE should be 1, else 0.
 * - CORE_CLK > 66MHz, AHB6DIV should be 1, else 0.
 * - CORE_CLK > 50MHz, FIUDIV should be 1, else 0.
 */
/* Target OSC_CLK freq */
#define OSC_CLK 90000000
/* Core clock prescaler */
#define FPRED 5 /* CORE_CLK = OSC_CLK/6 */
/* Core domain clock */
#define CORE_CLK (OSC_CLK / (FPRED + 1))
/* FMUL clock */
#if (OSC_CLK >= 80000000)
#define FMCLK (OSC_CLK / 2) /* FMUL clock = OSC_CLK/2 if OSC_CLK >= 80MHz */
#else
#define FMCLK OSC_CLK /* FMUL clock = OSC_CLK */
#endif
/* AHB6 clock */
#if (CORE_CLK > 66000000)
#define AHB6DIV 1 /* AHB6_CLK = CORE_CLK/2 if CORE_CLK > 66MHz */
#else
#define AHB6DIV 0 /* AHB6_CLK = CORE_CLK */
#endif
/* FIU clock divider */
#if (CORE_CLK > 50000000)
#define FIUDIV 1 /* FIU_CLK = CORE_CLK/2 */
#else
#define FIUDIV 0 /* FIU_CLK = CORE_CLK */
#endif
/* APBs source clock */
#define APBSRC_CLK OSC_CLK
/* APB1 clock divider */
#define APB1DIV 5 /* APB1 clock = OSC_CLK/6 */
/* APB2 clock divider */
#define APB2DIV 5 /* APB2 clock = OSC_CLK/6 */
/* APB3 clock divider */
#define APB3DIV 5 /* APB3 clock = OSC_CLK/6 */
#if NPCX_FAMILY_VERSION >= NPCX_FAMILY_NPCX9
/* APB4 clock divider */
#define APB4DIV 5 /* APB4 clock = OSC_CLK/6 */
#endif
#endif

/* Get APB clock freq */
#define NPCX_APB_CLOCK(no) (APBSRC_CLK / (APB##no##DIV + 1))

/*
 * Frequency multiplier M/N value definitions according to the requested
 * OSC_CLK (Unit:Hz).
 */
#if (OSC_CLK > 80000000)
#define HFCGN    0x82 /* Set XF_RANGE as 1 if OSC_CLK >= 80MHz */
#else
#define HFCGN    0x02
#endif
#if   (OSC_CLK == 100000000)
#define HFCGMH   0x0B
#define HFCGML   0xEC
#elif (OSC_CLK == 90000000)
#define HFCGMH   0x0A
#define HFCGML   0xBA
#elif (OSC_CLK == 80000000)
#define HFCGMH   0x09
#define HFCGML   0x89
#elif (OSC_CLK == 66000000)
#define HFCGMH   0x0F
#define HFCGML   0xBC
#elif (OSC_CLK == 50000000)
#define HFCGMH   0x0B
#define HFCGML   0xEC
#elif (OSC_CLK == 48000000)
#define HFCGMH   0x0B
#define HFCGML   0x72
#elif (OSC_CLK == 40000000)
#define HFCGMH   0x09
#define HFCGML   0x89
#elif (OSC_CLK == 33000000)
#define HFCGMH   0x07
#define HFCGML   0xDE
#elif (OSC_CLK == 30000000)
#define HFCGMH   0x07
#define HFCGML   0x27
#elif (OSC_CLK == 26000000)
#define HFCGMH   0x06
#define HFCGML   0x33
#else
#error "Unsupported OSC_CLK Frequency"
#endif

#if defined(CHIP_FAMILY_NPCX5)
#if (OSC_CLK > 50000000)
#error "Unsupported OSC_CLK on NPCX5 series!"
#endif
#elif NPCX_FAMILY_VERSION >= NPCX_FAMILY_NPCX7
#if (OSC_CLK > 100000000)
#error "Unsupported OSC_CLK on NPCX series!"
#endif
#endif

/**
 * Return the current FMUL clock frequency in Hz.
 */
int clock_get_fm_freq(void);

/**
 * Return the current APB1 clock frequency in Hz.
 */
int clock_get_apb1_freq(void);

/**
 * Return the current APB2 clock frequency in Hz.
 */
int clock_get_apb2_freq(void);

/**
 * Return the current APB3 clock frequency in Hz.
 */
int clock_get_apb3_freq(void);

/**
 * Set the CPU clock to maximum freq for better performance.
 */
void clock_turbo(void);

/**
 * Set the CPU clock back to normal freq.
 */
void clock_turbo_disable(void);

#endif /* __CROS_EC_CLOCK_CHIP_H */