blob: 661abfa8877d6abbe7779469ffd4569b51a15887 [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
11#include <common.h>
12#include <asm/processor.h>
13
TsiChungLiew2ce14b72007-07-05 23:05:31 -050014#include <asm/immap.h>
Alison Wang35d23df2012-03-26 21:49:05 +000015#include <asm/io.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050016
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020017DECLARE_GLOBAL_DATA_PTR;
18
TsiChung Liewf6afe722007-06-18 13:50:13 -050019/* PLL min/max specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050020#define MAX_FVCO 500000 /* KHz */
21#define MAX_FSYS 80000 /* KHz */
22#define MIN_FSYS 58333 /* KHz */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000023
24#ifdef CONFIG_MCF5301x
25#define FREF 20000 /* KHz */
26#define MAX_MFD 63 /* Multiplier */
27#define MIN_MFD 0 /* Multiplier */
28#define USBDIV 8
29
30/* Low Power Divider specifications */
31#define MIN_LPD (0) /* Divider (not encoded) */
32#define MAX_LPD (15) /* Divider (not encoded) */
33#define DEFAULT_LPD (0) /* Divider (not encoded) */
34#endif
35
36#ifdef CONFIG_MCF532x
TsiChungLiew2ce14b72007-07-05 23:05:31 -050037#define FREF 16000 /* KHz */
38#define MAX_MFD 135 /* Multiplier */
39#define MIN_MFD 88 /* Multiplier */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000040
41/* Low Power Divider specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050042#define MIN_LPD (1 << 0) /* Divider (not encoded) */
43#define MAX_LPD (1 << 15) /* Divider (not encoded) */
44#define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000045#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050046
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000047#define BUSDIV 6 /* Divider */
48
49/* Get the value of the current system clock */
TsiChung Liewf6afe722007-06-18 13:50:13 -050050int get_sys_clock(void)
51{
Alison Wang35d23df2012-03-26 21:49:05 +000052 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
53 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liewf6afe722007-06-18 13:50:13 -050054 int divider;
55
56 /* Test to see if device is in LIMP mode */
Alison Wang35d23df2012-03-26 21:49:05 +000057 if (in_be16(&ccm->misccr) & CCM_MISCCR_LIMP) {
58 divider = in_be16(&ccm->cdr) & CCM_CDR_LPDIV(0xF);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000059#ifdef CONFIG_MCF5301x
60 return (FREF / (3 * (1 << divider)));
61#endif
62#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -050063 return (FREF / (2 << divider));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000064#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050065 } else {
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000066#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +000067 u32 pfdr = (in_be32(&pll->pcr) & 0x3F) + 1;
68 u32 refdiv = (1 << ((in_be32(&pll->pcr) & PLL_PCR_REFDIV(7)) >> 8));
69 u32 busdiv = ((in_be32(&pll->pdr) & 0x00F0) >> 4) + 1;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000070
71 return (((FREF * pfdr) / refdiv) / busdiv);
72#endif
73#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +000074 return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000075#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050076 }
77}
78
79/*
80 * Initialize the Low Power Divider circuit
81 *
82 * Parameters:
83 * div Desired system frequency divider
84 *
85 * Return Value:
86 * The resulting output system frequency
87 */
88int clock_limp(int div)
89{
Alison Wang35d23df2012-03-26 21:49:05 +000090 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liewf6afe722007-06-18 13:50:13 -050091 u32 temp;
92
93 /* Check bounds of divider */
94 if (div < MIN_LPD)
95 div = MIN_LPD;
96 if (div > MAX_LPD)
97 div = MAX_LPD;
98
99 /* Save of the current value of the SSIDIV so we don't overwrite the value */
Alison Wang35d23df2012-03-26 21:49:05 +0000100 temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500101
102 /* Apply the divider to the system clock */
Alison Wang35d23df2012-03-26 21:49:05 +0000103 out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500104
Alison Wang35d23df2012-03-26 21:49:05 +0000105 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500106
107 return (FREF / (3 * (1 << div)));
108}
109
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000110/* Exit low power LIMP mode */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500111int clock_exit_limp(void)
112{
Alison Wang35d23df2012-03-26 21:49:05 +0000113 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500114 int fout;
115
116 /* Exit LIMP mode */
Alison Wang35d23df2012-03-26 21:49:05 +0000117 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500118
119 /* Wait for PLL to lock */
Alison Wang35d23df2012-03-26 21:49:05 +0000120 while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK))
121 ;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500122
123 fout = get_sys_clock();
124
125 return fout;
126}
127
128/* Initialize the PLL
129 *
130 * Parameters:
131 * fref PLL reference clock frequency in KHz
132 * fsys Desired PLL output frequency in KHz
133 * flags Operating parameters
134 *
135 * Return Value:
136 * The resulting output system frequency
137 */
138int clock_pll(int fsys, int flags)
139{
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000140#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +0000141 u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000142#endif
Alison Wang35d23df2012-03-26 21:49:05 +0000143 sdram_t *sdram = (sdram_t *)(MMAP_SDRAM);
144 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500145 int fref, temp, fout, mfd;
146 u32 i;
147
148 fref = FREF;
149
150 if (fsys == 0) {
151 /* Return current PLL output */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000152#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +0000153 u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1;
154 mfd = (in_be32(&pll->pcr) & 0x3F) + 1;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000155
156 return (fref * mfd) / busdiv;
157#endif
158#ifdef CONFIG_MCF532x
Alison Wang35d23df2012-03-26 21:49:05 +0000159 mfd = in_8(&pll->pfdr);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500160
161 return (fref * mfd / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000162#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500163 }
164
165 /* Check bounds of requested system clock */
166 if (fsys > MAX_FSYS)
167 fsys = MAX_FSYS;
168
169 if (fsys < MIN_FSYS)
170 fsys = MIN_FSYS;
171
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000172 /*
173 * Multiplying by 100 when calculating the temp value,
174 * and then dividing by 100 to calculate the mfd allows
175 * for exact values without needing to include floating
176 * point libraries.
177 */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500178 temp = (100 * fsys) / fref;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000179#ifdef CONFIG_MCF5301x
180 mfd = (BUSDIV * temp) / 100;
181
182 /* Determine the output frequency for selected values */
183 fout = ((fref * mfd) / BUSDIV);
184#endif
185#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500186 mfd = (4 * BUSDIV * temp) / 100;
187
188 /* Determine the output frequency for selected values */
189 fout = ((fref * mfd) / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000190#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500191
Wolfgang Wegnerea32ab22010-03-02 10:59:20 +0100192/* must not tamper with SDRAMC if running from SDRAM */
193#if !defined(CONFIG_MONITOR_IS_IN_RAM)
TsiChung Liewf6afe722007-06-18 13:50:13 -0500194 /*
195 * Check to see if the SDRAM has already been initialized.
196 * If it has then the SDRAM needs to be put into self refresh
197 * mode before reprogramming the PLL.
198 */
Alison Wang35d23df2012-03-26 21:49:05 +0000199 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
200 clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500201
202 /*
203 * Initialize the PLL to generate the new system clock frequency.
204 * The device must be put into LIMP mode to reprogram the PLL.
205 */
206
207 /* Enter LIMP mode */
208 clock_limp(DEFAULT_LPD);
209
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000210#ifdef CONFIG_MCF5301x
Alison Wang35d23df2012-03-26 21:49:05 +0000211 out_be32(&pll->pdr,
212 PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
213 PLL_PDR_OUTDIV2(BUSDIV - 1) |
214 PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
215 PLL_PDR_OUTDIV4(USBDIV - 1));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000216
Alison Wang35d23df2012-03-26 21:49:05 +0000217 clrbits_be32(&pll->pcr, ~PLL_PCR_FBDIV_UNMASK);
218 setbits_be32(&pll->pcr, PLL_PCR_FBDIV(mfd - 1));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000219#endif
220#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500221 /* Reprogram PLL for desired fsys */
Alison Wang35d23df2012-03-26 21:49:05 +0000222 out_8(&pll->podr,
223 PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500224
Alison Wang35d23df2012-03-26 21:49:05 +0000225 out_8(&pll->pfdr, mfd);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000226#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500227
228 /* Exit LIMP mode */
229 clock_exit_limp();
230
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000231 /* Return the SDRAM to normal operation if it is in use. */
Alison Wang35d23df2012-03-26 21:49:05 +0000232 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
233 setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000234
235#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500236 /*
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000237 * software workaround for SDRAM opeartion after exiting LIMP
238 * mode errata
TsiChung Liewf6afe722007-06-18 13:50:13 -0500239 */
Alison Wang35d23df2012-03-26 21:49:05 +0000240 out_be32(sdram_workaround, CONFIG_SYS_SDRAM_BASE);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000241#endif
TsiChungLiew2ce14b72007-07-05 23:05:31 -0500242
TsiChung Liewf6afe722007-06-18 13:50:13 -0500243 /* wait for DQS logic to relock */
244 for (i = 0; i < 0x200; i++) ;
Wolfgang Wegnerea32ab22010-03-02 10:59:20 +0100245#endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500246
247 return fout;
248}
249
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000250/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500251int get_clocks(void)
252{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200253 gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500254 gd->cpu_clk = (gd->bus_clk * 3);
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600255
Heiko Schocherf2850742012-10-24 13:48:22 +0200256#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000257 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600258#endif
259
TsiChung Liewf6afe722007-06-18 13:50:13 -0500260 return (0);
261}