blob: 166916526eb930cc47fcbf337b69d2e0055d50e8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChung Liewf6afe722007-06-18 13:50:13 -05002/*
3 *
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Alison Wang35d23df2012-03-26 21:49:05 +00007 * Copyright (C) 2004-2008, 2012 Freescale Semiconductor, Inc.
TsiChung Liewf6afe722007-06-18 13:50:13 -05008 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChung Liewf6afe722007-06-18 13:50:13 -05009 */
10
Simon Glass85d65312019-12-28 10:44:58 -070011#include <clock_legacy.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050013#include <asm/processor.h>
14
TsiChungLiew2ce14b72007-07-05 23:05:31 -050015#include <asm/immap.h>
Alison Wang35d23df2012-03-26 21:49:05 +000016#include <asm/io.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050017
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020018DECLARE_GLOBAL_DATA_PTR;
19
TsiChung Liewf6afe722007-06-18 13:50:13 -050020/* PLL min/max specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050021#define MAX_FVCO 500000 /* KHz */
22#define MAX_FSYS 80000 /* KHz */
23#define MIN_FSYS 58333 /* KHz */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000024
25#ifdef CONFIG_MCF5301x
26#define FREF 20000 /* KHz */
27#define MAX_MFD 63 /* Multiplier */
28#define MIN_MFD 0 /* Multiplier */
29#define USBDIV 8
30
31/* Low Power Divider specifications */
32#define MIN_LPD (0) /* Divider (not encoded) */
33#define MAX_LPD (15) /* Divider (not encoded) */
34#define DEFAULT_LPD (0) /* Divider (not encoded) */
35#endif
36
37#ifdef CONFIG_MCF532x
TsiChungLiew2ce14b72007-07-05 23:05:31 -050038#define FREF 16000 /* KHz */
39#define MAX_MFD 135 /* Multiplier */
40#define MIN_MFD 88 /* Multiplier */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000041
42/* Low Power Divider specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050043#define MIN_LPD (1 << 0) /* Divider (not encoded) */
44#define MAX_LPD (1 << 15) /* Divider (not encoded) */
45#define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000046#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050047
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000048#define BUSDIV 6 /* Divider */
49
50/* Get the value of the current system clock */
TsiChung Liewf6afe722007-06-18 13:50:13 -050051int get_sys_clock(void)
52{
Alison Wang35d23df2012-03-26 21:49:05 +000053 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
54 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liewf6afe722007-06-18 13:50:13 -050055 int divider;
56
57 /* Test to see if device is in LIMP mode */
Alison Wang35d23df2012-03-26 21:49:05 +000058 if (in_be16(&ccm->misccr) & CCM_MISCCR_LIMP) {
59 divider = in_be16(&ccm->cdr) & CCM_CDR_LPDIV(0xF);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000060#ifdef CONFIG_MCF5301x
61 return (FREF / (3 * (1 << divider)));
62#endif
63#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -050064 return (FREF / (2 << divider));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000065#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050066 } else {
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000067#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +000068 u32 pfdr = (in_be32(&pll->pcr) & 0x3F) + 1;
69 u32 refdiv = (1 << ((in_be32(&pll->pcr) & PLL_PCR_REFDIV(7)) >> 8));
70 u32 busdiv = ((in_be32(&pll->pdr) & 0x00F0) >> 4) + 1;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000071
72 return (((FREF * pfdr) / refdiv) / busdiv);
73#endif
74#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +000075 return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000076#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050077 }
78}
79
80/*
81 * Initialize the Low Power Divider circuit
82 *
83 * Parameters:
84 * div Desired system frequency divider
85 *
86 * Return Value:
87 * The resulting output system frequency
88 */
89int clock_limp(int div)
90{
Alison Wang35d23df2012-03-26 21:49:05 +000091 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liewf6afe722007-06-18 13:50:13 -050092 u32 temp;
93
94 /* Check bounds of divider */
95 if (div < MIN_LPD)
96 div = MIN_LPD;
97 if (div > MAX_LPD)
98 div = MAX_LPD;
99
100 /* Save of the current value of the SSIDIV so we don't overwrite the value */
Alison Wang35d23df2012-03-26 21:49:05 +0000101 temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500102
103 /* Apply the divider to the system clock */
Alison Wang35d23df2012-03-26 21:49:05 +0000104 out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500105
Alison Wang35d23df2012-03-26 21:49:05 +0000106 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500107
108 return (FREF / (3 * (1 << div)));
109}
110
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000111/* Exit low power LIMP mode */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500112int clock_exit_limp(void)
113{
Alison Wang35d23df2012-03-26 21:49:05 +0000114 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500115 int fout;
116
117 /* Exit LIMP mode */
Alison Wang35d23df2012-03-26 21:49:05 +0000118 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500119
120 /* Wait for PLL to lock */
Alison Wang35d23df2012-03-26 21:49:05 +0000121 while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK))
122 ;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500123
124 fout = get_sys_clock();
125
126 return fout;
127}
128
129/* Initialize the PLL
130 *
131 * Parameters:
132 * fref PLL reference clock frequency in KHz
133 * fsys Desired PLL output frequency in KHz
134 * flags Operating parameters
135 *
136 * Return Value:
137 * The resulting output system frequency
138 */
139int clock_pll(int fsys, int flags)
140{
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000141#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +0000142 u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000143#endif
Alison Wang35d23df2012-03-26 21:49:05 +0000144 sdram_t *sdram = (sdram_t *)(MMAP_SDRAM);
145 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500146 int fref, temp, fout, mfd;
147 u32 i;
148
149 fref = FREF;
150
151 if (fsys == 0) {
152 /* Return current PLL output */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000153#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +0000154 u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1;
155 mfd = (in_be32(&pll->pcr) & 0x3F) + 1;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000156
157 return (fref * mfd) / busdiv;
158#endif
159#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +0000160 mfd = in_8(&pll->pfdr);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500161
162 return (fref * mfd / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000163#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500164 }
165
166 /* Check bounds of requested system clock */
167 if (fsys > MAX_FSYS)
168 fsys = MAX_FSYS;
169
170 if (fsys < MIN_FSYS)
171 fsys = MIN_FSYS;
172
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000173 /*
174 * Multiplying by 100 when calculating the temp value,
175 * and then dividing by 100 to calculate the mfd allows
176 * for exact values without needing to include floating
177 * point libraries.
178 */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500179 temp = (100 * fsys) / fref;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000180#ifdef CONFIG_MCF5301x
181 mfd = (BUSDIV * temp) / 100;
182
183 /* Determine the output frequency for selected values */
184 fout = ((fref * mfd) / BUSDIV);
185#endif
186#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500187 mfd = (4 * BUSDIV * temp) / 100;
188
189 /* Determine the output frequency for selected values */
190 fout = ((fref * mfd) / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000191#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500192
Wolfgang Wegnerea32ab22010-03-02 10:59:20 +0100193/* must not tamper with SDRAMC if running from SDRAM */
194#if !defined(CONFIG_MONITOR_IS_IN_RAM)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500195 /*
196 * Check to see if the SDRAM has already been initialized.
197 * If it has then the SDRAM needs to be put into self refresh
198 * mode before reprogramming the PLL.
199 */
Alison Wang35d23df2012-03-26 21:49:05 +0000200 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
201 clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500202
203 /*
204 * Initialize the PLL to generate the new system clock frequency.
205 * The device must be put into LIMP mode to reprogram the PLL.
206 */
207
208 /* Enter LIMP mode */
209 clock_limp(DEFAULT_LPD);
210
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000211#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +0000212 out_be32(&pll->pdr,
213 PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
214 PLL_PDR_OUTDIV2(BUSDIV - 1) |
215 PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
216 PLL_PDR_OUTDIV4(USBDIV - 1));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000217
Alison Wang35d23df2012-03-26 21:49:05 +0000218 clrbits_be32(&pll->pcr, ~PLL_PCR_FBDIV_UNMASK);
219 setbits_be32(&pll->pcr, PLL_PCR_FBDIV(mfd - 1));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000220#endif
221#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500222 /* Reprogram PLL for desired fsys */
Alison Wang35d23df2012-03-26 21:49:05 +0000223 out_8(&pll->podr,
224 PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500225
Alison Wang35d23df2012-03-26 21:49:05 +0000226 out_8(&pll->pfdr, mfd);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000227#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500228
229 /* Exit LIMP mode */
230 clock_exit_limp();
231
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000232 /* Return the SDRAM to normal operation if it is in use. */
Alison Wang35d23df2012-03-26 21:49:05 +0000233 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
234 setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000235
236#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500237 /*
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000238 * software workaround for SDRAM opeartion after exiting LIMP
239 * mode errata
TsiChung Liewf6afe722007-06-18 13:50:13 -0500240 */
Tom Rinibb4dd962022-11-16 13:10:37 -0500241 out_be32(sdram_workaround, CFG_SYS_SDRAM_BASE);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000242#endif
TsiChungLiew2ce14b72007-07-05 23:05:31 -0500243
TsiChung Liewf6afe722007-06-18 13:50:13 -0500244 /* wait for DQS logic to relock */
245 for (i = 0; i < 0x200; i++) ;
Wolfgang Wegnerea32ab22010-03-02 10:59:20 +0100246#endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500247
248 return fout;
249}
250
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000251/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500252int get_clocks(void)
253{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500254 gd->bus_clk = clock_pll(CFG_SYS_CLK / 1000, 0) * 1000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500255 gd->cpu_clk = (gd->bus_clk * 3);
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600256
Heiko Schocherf2850742012-10-24 13:48:22 +0200257#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000258 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600259#endif
260
TsiChung Liewf6afe722007-06-18 13:50:13 -0500261 return (0);
262}