blob: 0d378e63d4f139d316c7e61ad1ffac82114cb1b9 [file] [log] [blame]
TsiChung Liewf6afe722007-06-18 13:50:13 -05001/*
2 *
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
TsiChung Liewe7e4fc82008-10-22 11:38:21 +00006 * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
TsiChung Liewf6afe722007-06-18 13:50:13 -05007 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <asm/processor.h>
30
TsiChungLiew2ce14b72007-07-05 23:05:31 -050031#include <asm/immap.h>
TsiChung Liewf6afe722007-06-18 13:50:13 -050032
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020033DECLARE_GLOBAL_DATA_PTR;
34
TsiChung Liewf6afe722007-06-18 13:50:13 -050035/* PLL min/max specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050036#define MAX_FVCO 500000 /* KHz */
37#define MAX_FSYS 80000 /* KHz */
38#define MIN_FSYS 58333 /* KHz */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000039
40#ifdef CONFIG_MCF5301x
41#define FREF 20000 /* KHz */
42#define MAX_MFD 63 /* Multiplier */
43#define MIN_MFD 0 /* Multiplier */
44#define USBDIV 8
45
46/* Low Power Divider specifications */
47#define MIN_LPD (0) /* Divider (not encoded) */
48#define MAX_LPD (15) /* Divider (not encoded) */
49#define DEFAULT_LPD (0) /* Divider (not encoded) */
50#endif
51
52#ifdef CONFIG_MCF532x
TsiChungLiew2ce14b72007-07-05 23:05:31 -050053#define FREF 16000 /* KHz */
54#define MAX_MFD 135 /* Multiplier */
55#define MIN_MFD 88 /* Multiplier */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000056
57/* Low Power Divider specifications */
TsiChungLiew2ce14b72007-07-05 23:05:31 -050058#define MIN_LPD (1 << 0) /* Divider (not encoded) */
59#define MAX_LPD (1 << 15) /* Divider (not encoded) */
60#define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000061#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050062
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000063#define BUSDIV 6 /* Divider */
64
65/* Get the value of the current system clock */
TsiChung Liewf6afe722007-06-18 13:50:13 -050066int get_sys_clock(void)
67{
68 volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
69 volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL);
70 int divider;
71
72 /* Test to see if device is in LIMP mode */
73 if (ccm->misccr & CCM_MISCCR_LIMP) {
74 divider = ccm->cdr & CCM_CDR_LPDIV(0xF);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000075#ifdef CONFIG_MCF5301x
76 return (FREF / (3 * (1 << divider)));
77#endif
78#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -050079 return (FREF / (2 << divider));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000080#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050081 } else {
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000082#ifdef CONFIG_MCF5301x
83 u32 pfdr = (pll->pcr & 0x3F) + 1;
84 u32 refdiv = (1 << ((pll->pcr & PLL_PCR_REFDIV(7)) >> 8));
85 u32 busdiv = ((pll->pdr & 0x00F0) >> 4) + 1;
86
87 return (((FREF * pfdr) / refdiv) / busdiv);
88#endif
89#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -050090 return ((FREF * pll->pfdr) / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +000091#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -050092 }
93}
94
95/*
96 * Initialize the Low Power Divider circuit
97 *
98 * Parameters:
99 * div Desired system frequency divider
100 *
101 * Return Value:
102 * The resulting output system frequency
103 */
104int clock_limp(int div)
105{
106 volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
107 u32 temp;
108
109 /* Check bounds of divider */
110 if (div < MIN_LPD)
111 div = MIN_LPD;
112 if (div > MAX_LPD)
113 div = MAX_LPD;
114
115 /* Save of the current value of the SSIDIV so we don't overwrite the value */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000116 temp = (ccm->cdr & CCM_CDR_SSIDIV(0xFF));
TsiChung Liewf6afe722007-06-18 13:50:13 -0500117
118 /* Apply the divider to the system clock */
119 ccm->cdr = (CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
120
121 ccm->misccr |= CCM_MISCCR_LIMP;
122
123 return (FREF / (3 * (1 << div)));
124}
125
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000126/* Exit low power LIMP mode */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500127int clock_exit_limp(void)
128{
129 volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM);
130 int fout;
131
132 /* Exit LIMP mode */
133 ccm->misccr &= (~CCM_MISCCR_LIMP);
134
135 /* Wait for PLL to lock */
136 while (!(ccm->misccr & CCM_MISCCR_PLL_LOCK)) ;
137
138 fout = get_sys_clock();
139
140 return fout;
141}
142
143/* Initialize the PLL
144 *
145 * Parameters:
146 * fref PLL reference clock frequency in KHz
147 * fsys Desired PLL output frequency in KHz
148 * flags Operating parameters
149 *
150 * Return Value:
151 * The resulting output system frequency
152 */
153int clock_pll(int fsys, int flags)
154{
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000155#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500156 volatile u32 *sdram_workaround = (volatile u32 *)(MMAP_SDRAM + 0x80);
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000157#endif
158 volatile sdram_t *sdram = (volatile sdram_t *)(MMAP_SDRAM);
TsiChung Liewf6afe722007-06-18 13:50:13 -0500159 volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL);
160 int fref, temp, fout, mfd;
161 u32 i;
162
163 fref = FREF;
164
165 if (fsys == 0) {
166 /* Return current PLL output */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000167#ifdef CONFIG_MCF5301x
168 u32 busdiv = ((pll->pdr >> 4) & 0x0F) + 1;
169 mfd = (pll->pcr & 0x3F) + 1;
170
171 return (fref * mfd) / busdiv;
172#endif
173#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500174 mfd = pll->pfdr;
175
176 return (fref * mfd / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000177#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500178 }
179
180 /* Check bounds of requested system clock */
181 if (fsys > MAX_FSYS)
182 fsys = MAX_FSYS;
183
184 if (fsys < MIN_FSYS)
185 fsys = MIN_FSYS;
186
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000187 /*
188 * Multiplying by 100 when calculating the temp value,
189 * and then dividing by 100 to calculate the mfd allows
190 * for exact values without needing to include floating
191 * point libraries.
192 */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500193 temp = (100 * fsys) / fref;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000194#ifdef CONFIG_MCF5301x
195 mfd = (BUSDIV * temp) / 100;
196
197 /* Determine the output frequency for selected values */
198 fout = ((fref * mfd) / BUSDIV);
199#endif
200#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500201 mfd = (4 * BUSDIV * temp) / 100;
202
203 /* Determine the output frequency for selected values */
204 fout = ((fref * mfd) / (BUSDIV * 4));
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000205#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500206
207 /*
208 * Check to see if the SDRAM has already been initialized.
209 * If it has then the SDRAM needs to be put into self refresh
210 * mode before reprogramming the PLL.
211 */
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000212 if (sdram->ctrl & SDRAMC_SDCR_REF)
213 sdram->ctrl &= ~SDRAMC_SDCR_CKE;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500214
215 /*
216 * Initialize the PLL to generate the new system clock frequency.
217 * The device must be put into LIMP mode to reprogram the PLL.
218 */
219
220 /* Enter LIMP mode */
221 clock_limp(DEFAULT_LPD);
222
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000223#ifdef CONFIG_MCF5301x
224 pll->pdr =
225 PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
226 PLL_PDR_OUTDIV2(BUSDIV - 1) |
227 PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
228 PLL_PDR_OUTDIV4(USBDIV - 1);
229
230 pll->pcr &= PLL_PCR_FBDIV_MASK;
231 pll->pcr |= PLL_PCR_FBDIV(mfd - 1);
232#endif
233#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500234 /* Reprogram PLL for desired fsys */
235 pll->podr = (PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
236
237 pll->pfdr = mfd;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000238#endif
TsiChung Liewf6afe722007-06-18 13:50:13 -0500239
240 /* Exit LIMP mode */
241 clock_exit_limp();
242
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000243 /* Return the SDRAM to normal operation if it is in use. */
244 if (sdram->ctrl & SDRAMC_SDCR_REF)
245 sdram->ctrl |= SDRAMC_SDCR_CKE;
246
247#ifdef CONFIG_MCF532x
TsiChung Liewf6afe722007-06-18 13:50:13 -0500248 /*
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000249 * software workaround for SDRAM opeartion after exiting LIMP
250 * mode errata
TsiChung Liewf6afe722007-06-18 13:50:13 -0500251 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200252 *sdram_workaround = CONFIG_SYS_SDRAM_BASE;
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000253#endif
TsiChungLiew2ce14b72007-07-05 23:05:31 -0500254
TsiChung Liewf6afe722007-06-18 13:50:13 -0500255 /* wait for DQS logic to relock */
256 for (i = 0; i < 0x200; i++) ;
257
258 return fout;
259}
260
TsiChung Liewe7e4fc82008-10-22 11:38:21 +0000261/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
TsiChung Liewf6afe722007-06-18 13:50:13 -0500262int get_clocks(void)
263{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200264 gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
TsiChung Liewf6afe722007-06-18 13:50:13 -0500265 gd->cpu_clk = (gd->bus_clk * 3);
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600266
267#ifdef CONFIG_FSL_I2C
268 gd->i2c1_clk = gd->bus_clk;
269#endif
270
TsiChung Liewf6afe722007-06-18 13:50:13 -0500271 return (0);
272}