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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/************************************************************************************
* arch/arm/src/lpc313x/lpc313x_clkinit.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
************************************************************************************/
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <arch/board/board.h>
#include "lpc313x_cgu.h"
#include "lpc313x_cgudrvr.h"
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
/************************************************************************************
* Private Types
************************************************************************************/
/* This structure describes the configuration of one domain */
struct lpc313x_domainconfig_s
{
enum lpc313x_domainid_e dmnid; /* Domain ID */
uint32_t finsel; /* Frequency input selection */
uint32_t clk1; /* ID of first clock in the domain */
uint32_t nclks; /* Number of clocks in the domain */
uint32_t fdiv1; /* First frequency divider in the domain */
uint32_t nfdiv; /* Number of frequency dividers in the domain */
const struct lpc313x_subdomainconfig_s* sub; /* Sub=domain array */
};
/************************************************************************************
* Private Data
************************************************************************************/
/************************************************************************************
* Public Data
************************************************************************************/
/************************************************************************************
* Private Functions
************************************************************************************/
/************************************************************************************
* Name: lpc313x_domaininit
*
* Description:
* Initialize one clock domain based on board-specific clock configuration data
*
************************************************************************************/
static void lpc313x_domaininit(struct lpc313x_domainconfig_s* dmn)
{
const struct lpc313x_subdomainconfig_s * sub = dmn->sub;
uint32_t fdivcfg;
uint32_t regaddr;
uint32_t regval;
int fdndx;
int clkndx;
int bcrndx = lp313x_bcrndx(dmn->dmnid);
int esrndx;
if (bcrndx != BCRNDX_INVALID)
{
/* Disable BCR for domain */
regaddr = LPC313X_CGU_BCR(bcrndx);
putreg32(0, regaddr);
}
/* Configure the fractional dividers in this domain */
for (fdndx = 0; fdndx < dmn->nfdiv; fdndx++, sub++)
{
/* Set fractional divider confiruation but don't enable it yet */
fdivcfg = lpc313x_fdivinit(fdndx + dmn->fdiv1, &sub->fdiv, false);
/* Enable frac divider only if it has valid settings */
if (fdivcfg != 0)
{
/* Select the fractional dividir for each clock in this
* sub domain.
*/
for (clkndx = 0; clkndx <= dmn->nclks; clkndx++)
{
/* Does this clock have an ESR register? */
esrndx = lp313x_esrndx((enum lpc313x_clockid_e)(clkndx + dmn->clk1));
if (esrndx != ESRNDX_INVALID)
{
/* Yes.. Check if this clock belongs to this sub-domain */
if (sub->clkset & (1 << clkndx))
{
/* Yes.. configure the clock to use this fractional divider */
regaddr = LPC313X_CGU_ESR(esrndx);
putreg32((fdndx << CGU_ESR_ESRSEL_SHIFT) | CGU_ESR_ESREN, regaddr);
}
}
}
/* Enable the fractional divider */
regaddr = LPC313X_CGU_FDC(fdndx + dmn->fdiv1);
regval = getreg32(regaddr);
regval |= CGU_FDC_RUN;
putreg32(regval, regaddr);
}
}
if (bcrndx != BCRNDX_INVALID)
{
/* Enable the BCR for domain */
regaddr = LPC313X_CGU_BCR(bcrndx);
putreg32(CGU_BCR_FDRUN, regaddr);
}
/* Select input base clock for domain*/
lpc313x_selectfreqin(dmn->dmnid, dmn->finsel);
}
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: lpc313x_clkinit
*
* Description:
* Initialize all clock domains based on board-specific clock configuration data
*
************************************************************************************/
void lpc313x_clkinit(const struct lpc313x_clkinit_s* cfg)
{
struct lpc313x_domainconfig_s domain;
/* Reset all clocks and connect them to FFAST */
lpc313x_resetclks();
/* Initialize Domain0 = SYS_BASE clocks */
domain.dmnid = DOMAINID_SYS;
domain.finsel = cfg->domain0.finsel;
domain.clk1 = CLKID_SYSBASE_FIRST;
domain.nclks = (CLKID_SYSBASE_LAST - CLKID_SYSBASE_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE0_LOW;
domain.nfdiv = FRACDIV_BASE0_CNT;
domain.sub = cfg->domain0.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain1 = AHB0APB0_BASE clocks */
domain.dmnid = DOMAINID_AHB0APB0;
domain.finsel = cfg->domain1.finsel;
domain.clk1 = CLKID_AHB0APB0_FIRST;
domain.nclks = (CLKID_AHB0APB0_LAST - CLKID_AHB0APB0_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE1_LOW;
domain.nfdiv = FRACDIV_BASE1_CNT;
domain.sub = cfg->domain1.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain2 = AHB0APB1_BASE clocks */
domain.dmnid = DOMAINID_AHB0APB1;
domain.finsel = cfg->domain2.finsel;
domain.clk1 = CLKID_AHB0APB1_FIRST;
domain.nclks = (CLKID_AHB0APB1_LAST - CLKID_AHB0APB1_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE2_LOW;
domain.nfdiv = FRACDIV_BASE2_CNT;
domain.sub = cfg->domain2.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain3 = AHB0APB2_BASE clocks */
domain.dmnid = DOMAINID_AHB0APB2;
domain.finsel = cfg->domain3.finsel;
domain.clk1 = CLKID_AHB0APB2_FIRST;
domain.nclks = (CLKID_AHB0APB2_LAST - CLKID_AHB0APB2_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE3_LOW;
domain.nfdiv = FRACDIV_BASE3_CNT;
domain.sub = cfg->domain3.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain4 = AHB0APB3_BASE clocks */
domain.dmnid = DOMAINID_AHB0APB3;
domain.finsel = cfg->domain4.finsel;
domain.clk1 = CLKID_AHB0APB3_FIRST;
domain.nclks = (CLKID_AHB0APB3_LAST - CLKID_AHB0APB3_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE4_LOW;
domain.nfdiv = FRACDIV_BASE4_CNT;
domain.sub = cfg->domain4.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain5 = PCM_BASE clocks */
domain.dmnid = DOMAINID_PCM;
domain.finsel = cfg->domain5.finsel;
domain.clk1 = CLKID_PCM_FIRST;
domain.nclks = 1;
domain.fdiv1 = FRACDIV_BASE5_LOW;
domain.nfdiv = FRACDIV_BASE5_CNT;
domain.sub = cfg->domain5.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain6 = UART_BASE clocks */
domain.dmnid = DOMAINID_UART;
domain.finsel = cfg->domain6.finsel;
domain.clk1 = CLKID_UART_FIRST;
domain.nclks = 1;
domain.fdiv1 = FRACDIV_BASE6_LOW;
domain.nfdiv = FRACDIV_BASE6_CNT;
domain.sub = cfg->domain6.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain7 = CLK1024FS_BASE clocks */
domain.dmnid = DOMAINID_CLK1024FS;
domain.finsel = cfg->domain7.finsel;
domain.clk1 = CLKID_CLK1024FS_FIRST;
domain.nclks = (CLKID_CLK1024FS_LAST - CLKID_CLK1024FS_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE7_LOW;
domain.nfdiv = FRACDIV_BASE7_CNT;
domain.sub = cfg->domain7.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain8 = I2SRX_BCK0_BASE clocks */
lpc313x_selectfreqin(DOMAINID_BCK0, cfg->domain8.finsel);
/* Initialize Domain9 = I2SRX_BCK1_BASE clocks */
lpc313x_selectfreqin(DOMAINID_BCK1, cfg->domain9.finsel);
/* Initialize Domain10 = SPI_BASE clocks */
domain.dmnid = DOMAINID_SPI;
domain.finsel = cfg->domain10.finsel;
domain.clk1 = CLKID_SPI_FIRST;
domain.nclks = (CLKID_SPI_LAST - CLKID_SPI_FIRST) + 1;
domain.fdiv1 = FRACDIV_BASE10_LOW;
domain.nfdiv = FRACDIV_BASE10_CNT;
domain.sub = cfg->domain10.sub;
lpc313x_domaininit(&domain);
/* Initialize Domain11 = SYSCLK_O_BASE clocks */
lpc313x_selectfreqin(DOMAINID_SYSCLKO, cfg->domain11.finsel);
/* Initialize Dynamic fractional dividers -- to be provided */
}
|