blob: 8efb451dc133b8659873cc84d3f4b7324e90eec9 [file] [log] [blame]
TsiChung Liew8e585f02007-06-18 13:50:13 -05001/*
2 *
3 * (C) Copyright 2000-2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
Alison Wangaa0d99f2012-03-26 21:49:05 +00006 * Copyright (C) 2004-2008, 2012 Freescale Semiconductor, Inc.
TsiChung Liew8e585f02007-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
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050031#include <asm/immap.h>
Alison Wangaa0d99f2012-03-26 21:49:05 +000032#include <asm/io.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050033
Wolfgang Denk1218abf2007-09-15 20:48:41 +020034DECLARE_GLOBAL_DATA_PTR;
35
TsiChung Liew8e585f02007-06-18 13:50:13 -050036/* PLL min/max specifications */
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050037#define MAX_FVCO 500000 /* KHz */
38#define MAX_FSYS 80000 /* KHz */
39#define MIN_FSYS 58333 /* KHz */
TsiChung Liew536e7da2008-10-22 11:38:21 +000040
41#ifdef CONFIG_MCF5301x
42#define FREF 20000 /* KHz */
43#define MAX_MFD 63 /* Multiplier */
44#define MIN_MFD 0 /* Multiplier */
45#define USBDIV 8
46
47/* Low Power Divider specifications */
48#define MIN_LPD (0) /* Divider (not encoded) */
49#define MAX_LPD (15) /* Divider (not encoded) */
50#define DEFAULT_LPD (0) /* Divider (not encoded) */
51#endif
52
53#ifdef CONFIG_MCF532x
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050054#define FREF 16000 /* KHz */
55#define MAX_MFD 135 /* Multiplier */
56#define MIN_MFD 88 /* Multiplier */
TsiChung Liew536e7da2008-10-22 11:38:21 +000057
58/* Low Power Divider specifications */
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050059#define MIN_LPD (1 << 0) /* Divider (not encoded) */
60#define MAX_LPD (1 << 15) /* Divider (not encoded) */
61#define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */
TsiChung Liew536e7da2008-10-22 11:38:21 +000062#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050063
TsiChung Liew536e7da2008-10-22 11:38:21 +000064#define BUSDIV 6 /* Divider */
65
66/* Get the value of the current system clock */
TsiChung Liew8e585f02007-06-18 13:50:13 -050067int get_sys_clock(void)
68{
Alison Wangaa0d99f2012-03-26 21:49:05 +000069 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
70 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liew8e585f02007-06-18 13:50:13 -050071 int divider;
72
73 /* Test to see if device is in LIMP mode */
Alison Wangaa0d99f2012-03-26 21:49:05 +000074 if (in_be16(&ccm->misccr) & CCM_MISCCR_LIMP) {
75 divider = in_be16(&ccm->cdr) & CCM_CDR_LPDIV(0xF);
TsiChung Liew536e7da2008-10-22 11:38:21 +000076#ifdef CONFIG_MCF5301x
77 return (FREF / (3 * (1 << divider)));
78#endif
79#ifdef CONFIG_MCF532x
TsiChung Liew8e585f02007-06-18 13:50:13 -050080 return (FREF / (2 << divider));
TsiChung Liew536e7da2008-10-22 11:38:21 +000081#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050082 } else {
TsiChung Liew536e7da2008-10-22 11:38:21 +000083#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-03-26 21:49:05 +000084 u32 pfdr = (in_be32(&pll->pcr) & 0x3F) + 1;
85 u32 refdiv = (1 << ((in_be32(&pll->pcr) & PLL_PCR_REFDIV(7)) >> 8));
86 u32 busdiv = ((in_be32(&pll->pdr) & 0x00F0) >> 4) + 1;
TsiChung Liew536e7da2008-10-22 11:38:21 +000087
88 return (((FREF * pfdr) / refdiv) / busdiv);
89#endif
90#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +000091 return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4);
TsiChung Liew536e7da2008-10-22 11:38:21 +000092#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050093 }
94}
95
96/*
97 * Initialize the Low Power Divider circuit
98 *
99 * Parameters:
100 * div Desired system frequency divider
101 *
102 * Return Value:
103 * The resulting output system frequency
104 */
105int clock_limp(int div)
106{
Alison Wangaa0d99f2012-03-26 21:49:05 +0000107 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500108 u32 temp;
109
110 /* Check bounds of divider */
111 if (div < MIN_LPD)
112 div = MIN_LPD;
113 if (div > MAX_LPD)
114 div = MAX_LPD;
115
116 /* Save of the current value of the SSIDIV so we don't overwrite the value */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000117 temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500118
119 /* Apply the divider to the system clock */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000120 out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500121
Alison Wangaa0d99f2012-03-26 21:49:05 +0000122 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500123
124 return (FREF / (3 * (1 << div)));
125}
126
TsiChung Liew536e7da2008-10-22 11:38:21 +0000127/* Exit low power LIMP mode */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500128int clock_exit_limp(void)
129{
Alison Wangaa0d99f2012-03-26 21:49:05 +0000130 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500131 int fout;
132
133 /* Exit LIMP mode */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000134 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500135
136 /* Wait for PLL to lock */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000137 while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK))
138 ;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500139
140 fout = get_sys_clock();
141
142 return fout;
143}
144
145/* Initialize the PLL
146 *
147 * Parameters:
148 * fref PLL reference clock frequency in KHz
149 * fsys Desired PLL output frequency in KHz
150 * flags Operating parameters
151 *
152 * Return Value:
153 * The resulting output system frequency
154 */
155int clock_pll(int fsys, int flags)
156{
TsiChung Liew536e7da2008-10-22 11:38:21 +0000157#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000158 u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000159#endif
Alison Wangaa0d99f2012-03-26 21:49:05 +0000160 sdram_t *sdram = (sdram_t *)(MMAP_SDRAM);
161 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500162 int fref, temp, fout, mfd;
163 u32 i;
164
165 fref = FREF;
166
167 if (fsys == 0) {
168 /* Return current PLL output */
TsiChung Liew536e7da2008-10-22 11:38:21 +0000169#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000170 u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1;
171 mfd = (in_be32(&pll->pcr) & 0x3F) + 1;
TsiChung Liew536e7da2008-10-22 11:38:21 +0000172
173 return (fref * mfd) / busdiv;
174#endif
175#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000176 mfd = in_8(&pll->pfdr);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500177
178 return (fref * mfd / (BUSDIV * 4));
TsiChung Liew536e7da2008-10-22 11:38:21 +0000179#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500180 }
181
182 /* Check bounds of requested system clock */
183 if (fsys > MAX_FSYS)
184 fsys = MAX_FSYS;
185
186 if (fsys < MIN_FSYS)
187 fsys = MIN_FSYS;
188
TsiChung Liew536e7da2008-10-22 11:38:21 +0000189 /*
190 * Multiplying by 100 when calculating the temp value,
191 * and then dividing by 100 to calculate the mfd allows
192 * for exact values without needing to include floating
193 * point libraries.
194 */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500195 temp = (100 * fsys) / fref;
TsiChung Liew536e7da2008-10-22 11:38:21 +0000196#ifdef CONFIG_MCF5301x
197 mfd = (BUSDIV * temp) / 100;
198
199 /* Determine the output frequency for selected values */
200 fout = ((fref * mfd) / BUSDIV);
201#endif
202#ifdef CONFIG_MCF532x
TsiChung Liew8e585f02007-06-18 13:50:13 -0500203 mfd = (4 * BUSDIV * temp) / 100;
204
205 /* Determine the output frequency for selected values */
206 fout = ((fref * mfd) / (BUSDIV * 4));
TsiChung Liew536e7da2008-10-22 11:38:21 +0000207#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500208
Wolfgang Wegnerc7de8102010-03-02 10:59:20 +0100209/* must not tamper with SDRAMC if running from SDRAM */
210#if !defined(CONFIG_MONITOR_IS_IN_RAM)
TsiChung Liew8e585f02007-06-18 13:50:13 -0500211 /*
212 * Check to see if the SDRAM has already been initialized.
213 * If it has then the SDRAM needs to be put into self refresh
214 * mode before reprogramming the PLL.
215 */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000216 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
217 clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500218
219 /*
220 * Initialize the PLL to generate the new system clock frequency.
221 * The device must be put into LIMP mode to reprogram the PLL.
222 */
223
224 /* Enter LIMP mode */
225 clock_limp(DEFAULT_LPD);
226
TsiChung Liew536e7da2008-10-22 11:38:21 +0000227#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000228 out_be32(&pll->pdr,
229 PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) |
230 PLL_PDR_OUTDIV2(BUSDIV - 1) |
231 PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) |
232 PLL_PDR_OUTDIV4(USBDIV - 1));
TsiChung Liew536e7da2008-10-22 11:38:21 +0000233
Alison Wangaa0d99f2012-03-26 21:49:05 +0000234 clrbits_be32(&pll->pcr, ~PLL_PCR_FBDIV_UNMASK);
235 setbits_be32(&pll->pcr, PLL_PCR_FBDIV(mfd - 1));
TsiChung Liew536e7da2008-10-22 11:38:21 +0000236#endif
237#ifdef CONFIG_MCF532x
TsiChung Liew8e585f02007-06-18 13:50:13 -0500238 /* Reprogram PLL for desired fsys */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000239 out_8(&pll->podr,
240 PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500241
Alison Wangaa0d99f2012-03-26 21:49:05 +0000242 out_8(&pll->pfdr, mfd);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000243#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500244
245 /* Exit LIMP mode */
246 clock_exit_limp();
247
TsiChung Liew536e7da2008-10-22 11:38:21 +0000248 /* Return the SDRAM to normal operation if it is in use. */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000249 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
250 setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500251
TsiChung Liew536e7da2008-10-22 11:38:21 +0000252#ifdef CONFIG_MCF532x
253 /*
254 * software workaround for SDRAM opeartion after exiting LIMP
255 * mode errata
256 */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000257 out_be32(sdram_workaround, CONFIG_SYS_SDRAM_BASE);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000258#endif
TsiChungLiewb9bf3de2007-07-05 23:05:31 -0500259
TsiChung Liew8e585f02007-06-18 13:50:13 -0500260 /* wait for DQS logic to relock */
261 for (i = 0; i < 0x200; i++) ;
Wolfgang Wegnerc7de8102010-03-02 10:59:20 +0100262#endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500263
264 return fout;
265}
266
TsiChung Liew536e7da2008-10-22 11:38:21 +0000267/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500268int get_clocks(void)
269{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200270 gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500271 gd->cpu_clk = (gd->bus_clk * 3);
TsiChung Lieweec567a2008-08-19 03:01:19 +0600272
273#ifdef CONFIG_FSL_I2C
Simon Glass609e6ec2012-12-13 20:48:49 +0000274 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Lieweec567a2008-08-19 03:01:19 +0600275#endif
276
TsiChung Liew8e585f02007-06-18 13:50:13 -0500277 return (0);
278}