blob: a440bbb3b648ed6c29833572a5e928201383580e [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
TsiChung Liew8e585f02007-06-18 13:50:13 -050010 */
11
12#include <common.h>
13#include <asm/processor.h>
14
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050015#include <asm/immap.h>
Alison Wangaa0d99f2012-03-26 21:49:05 +000016#include <asm/io.h>
TsiChung Liew8e585f02007-06-18 13:50:13 -050017
Wolfgang Denk1218abf2007-09-15 20:48:41 +020018DECLARE_GLOBAL_DATA_PTR;
19
TsiChung Liew8e585f02007-06-18 13:50:13 -050020/* PLL min/max specifications */
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050021#define MAX_FVCO 500000 /* KHz */
22#define MAX_FSYS 80000 /* KHz */
23#define MIN_FSYS 58333 /* KHz */
TsiChung Liew536e7da2008-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
TsiChungLiewb9bf3de2007-07-05 23:05:31 -050038#define FREF 16000 /* KHz */
39#define MAX_MFD 135 /* Multiplier */
40#define MIN_MFD 88 /* Multiplier */
TsiChung Liew536e7da2008-10-22 11:38:21 +000041
42/* Low Power Divider specifications */
TsiChungLiewb9bf3de2007-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 Liew536e7da2008-10-22 11:38:21 +000046#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050047
TsiChung Liew536e7da2008-10-22 11:38:21 +000048#define BUSDIV 6 /* Divider */
49
50/* Get the value of the current system clock */
TsiChung Liew8e585f02007-06-18 13:50:13 -050051int get_sys_clock(void)
52{
Alison Wangaa0d99f2012-03-26 21:49:05 +000053 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
54 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liew8e585f02007-06-18 13:50:13 -050055 int divider;
56
57 /* Test to see if device is in LIMP mode */
Alison Wangaa0d99f2012-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 Liew536e7da2008-10-22 11:38:21 +000060#ifdef CONFIG_MCF5301x
61 return (FREF / (3 * (1 << divider)));
62#endif
63#ifdef CONFIG_MCF532x
TsiChung Liew8e585f02007-06-18 13:50:13 -050064 return (FREF / (2 << divider));
TsiChung Liew536e7da2008-10-22 11:38:21 +000065#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -050066 } else {
TsiChung Liew536e7da2008-10-22 11:38:21 +000067#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-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 Liew536e7da2008-10-22 11:38:21 +000071
72 return (((FREF * pfdr) / refdiv) / busdiv);
73#endif
74#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +000075 return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4);
TsiChung Liew536e7da2008-10-22 11:38:21 +000076#endif
TsiChung Liew8e585f02007-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 Wangaa0d99f2012-03-26 21:49:05 +000091 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liew8e585f02007-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 Wangaa0d99f2012-03-26 21:49:05 +0000101 temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500102
103 /* Apply the divider to the system clock */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000104 out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500105
Alison Wangaa0d99f2012-03-26 21:49:05 +0000106 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500107
108 return (FREF / (3 * (1 << div)));
109}
110
TsiChung Liew536e7da2008-10-22 11:38:21 +0000111/* Exit low power LIMP mode */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500112int clock_exit_limp(void)
113{
Alison Wangaa0d99f2012-03-26 21:49:05 +0000114 ccm_t *ccm = (ccm_t *)(MMAP_CCM);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500115 int fout;
116
117 /* Exit LIMP mode */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000118 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500119
120 /* Wait for PLL to lock */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000121 while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK))
122 ;
TsiChung Liew8e585f02007-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 Liew536e7da2008-10-22 11:38:21 +0000141#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000142 u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000143#endif
Alison Wangaa0d99f2012-03-26 21:49:05 +0000144 sdram_t *sdram = (sdram_t *)(MMAP_SDRAM);
145 pll_t *pll = (pll_t *)(MMAP_PLL);
TsiChung Liew8e585f02007-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 Liew536e7da2008-10-22 11:38:21 +0000153#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000154 u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1;
155 mfd = (in_be32(&pll->pcr) & 0x3F) + 1;
TsiChung Liew536e7da2008-10-22 11:38:21 +0000156
157 return (fref * mfd) / busdiv;
158#endif
159#ifdef CONFIG_MCF532x
Alison Wangaa0d99f2012-03-26 21:49:05 +0000160 mfd = in_8(&pll->pfdr);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500161
162 return (fref * mfd / (BUSDIV * 4));
TsiChung Liew536e7da2008-10-22 11:38:21 +0000163#endif
TsiChung Liew8e585f02007-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 Liew536e7da2008-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 Liew8e585f02007-06-18 13:50:13 -0500179 temp = (100 * fsys) / fref;
TsiChung Liew536e7da2008-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 Liew8e585f02007-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 Liew536e7da2008-10-22 11:38:21 +0000191#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500192
Wolfgang Wegnerc7de8102010-03-02 10:59:20 +0100193/* must not tamper with SDRAMC if running from SDRAM */
194#if !defined(CONFIG_MONITOR_IS_IN_RAM)
TsiChung Liew8e585f02007-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 Wangaa0d99f2012-03-26 21:49:05 +0000200 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
201 clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liew8e585f02007-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 Liew536e7da2008-10-22 11:38:21 +0000211#ifdef CONFIG_MCF5301x
Alison Wangaa0d99f2012-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 Liew536e7da2008-10-22 11:38:21 +0000217
Alison Wangaa0d99f2012-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 Liew536e7da2008-10-22 11:38:21 +0000220#endif
221#ifdef CONFIG_MCF532x
TsiChung Liew8e585f02007-06-18 13:50:13 -0500222 /* Reprogram PLL for desired fsys */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000223 out_8(&pll->podr,
224 PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV));
TsiChung Liew8e585f02007-06-18 13:50:13 -0500225
Alison Wangaa0d99f2012-03-26 21:49:05 +0000226 out_8(&pll->pfdr, mfd);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000227#endif
TsiChung Liew8e585f02007-06-18 13:50:13 -0500228
229 /* Exit LIMP mode */
230 clock_exit_limp();
231
TsiChung Liew536e7da2008-10-22 11:38:21 +0000232 /* Return the SDRAM to normal operation if it is in use. */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000233 if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF)
234 setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE);
TsiChung Liew8e585f02007-06-18 13:50:13 -0500235
TsiChung Liew536e7da2008-10-22 11:38:21 +0000236#ifdef CONFIG_MCF532x
237 /*
238 * software workaround for SDRAM opeartion after exiting LIMP
239 * mode errata
240 */
Alison Wangaa0d99f2012-03-26 21:49:05 +0000241 out_be32(sdram_workaround, CONFIG_SYS_SDRAM_BASE);
TsiChung Liew536e7da2008-10-22 11:38:21 +0000242#endif
TsiChungLiewb9bf3de2007-07-05 23:05:31 -0500243
TsiChung Liew8e585f02007-06-18 13:50:13 -0500244 /* wait for DQS logic to relock */
245 for (i = 0; i < 0x200; i++) ;
Wolfgang Wegnerc7de8102010-03-02 10:59:20 +0100246#endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500247
248 return fout;
249}
250
TsiChung Liew536e7da2008-10-22 11:38:21 +0000251/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
TsiChung Liew8e585f02007-06-18 13:50:13 -0500252int get_clocks(void)
253{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200254 gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000;
TsiChung Liew8e585f02007-06-18 13:50:13 -0500255 gd->cpu_clk = (gd->bus_clk * 3);
TsiChung Lieweec567a2008-08-19 03:01:19 +0600256
Heiko Schocher00f792e2012-10-24 13:48:22 +0200257#ifdef CONFIG_SYS_I2C_FSL
Simon Glass609e6ec2012-12-13 20:48:49 +0000258 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Lieweec567a2008-08-19 03:01:19 +0600259#endif
260
TsiChung Liew8e585f02007-06-18 13:50:13 -0500261 return (0);
262}