blob: 55d1c488a317f5e11e803fb2a2a2f7901062c93d [file] [log] [blame]
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -05001/*
2 *
Alison Wang8d8dac92012-03-26 21:49:08 +00003 * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -05004 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <asm/processor.h>
27
28#include <asm/immap.h>
Alison Wang8d8dac92012-03-26 21:49:08 +000029#include <asm/io.h>
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050030
31DECLARE_GLOBAL_DATA_PTR;
32
33/*
34 * Low Power Divider specifications
35 */
36#define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */
37#define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */
38
39#define CLOCK_PLL_FVCO_MAX 540000000
40#define CLOCK_PLL_FVCO_MIN 300000000
41
42#define CLOCK_PLL_FSYS_MAX 266666666
43#define CLOCK_PLL_FSYS_MIN 100000000
44#define MHZ 1000000
45
46void clock_enter_limp(int lpdiv)
47{
Alison Wang8d8dac92012-03-26 21:49:08 +000048 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050049 int i, j;
50
51 /* Check bounds of divider */
52 if (lpdiv < CLOCK_LPD_MIN)
53 lpdiv = CLOCK_LPD_MIN;
54 if (lpdiv > CLOCK_LPD_MAX)
55 lpdiv = CLOCK_LPD_MAX;
56
57 /* Round divider down to nearest power of two */
58 for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ;
59
Alison Wangfdc2fb12012-10-18 19:25:51 +000060#ifdef CONFIG_MCF5445x
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050061 /* Apply the divider to the system clock */
Alison Wang8d8dac92012-03-26 21:49:08 +000062 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
Alison Wangfdc2fb12012-10-18 19:25:51 +000063#endif
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050064
65 /* Enable Limp Mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000066 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050067}
68
69/*
70 * brief Exit Limp mode
71 * warning The PLL should be set and locked prior to exiting Limp mode
72 */
73void clock_exit_limp(void)
74{
Alison Wang8d8dac92012-03-26 21:49:08 +000075 ccm_t *ccm = (ccm_t *)MMAP_CCM;
76 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050077
78 /* Exit Limp mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000079 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050080
81 /* Wait for the PLL to lock */
Alison Wang8d8dac92012-03-26 21:49:08 +000082 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
83 ;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050084}
85
Alison Wangfdc2fb12012-10-18 19:25:51 +000086#ifdef CONFIG_MCF5441x
87void setup_5441x_clocks(void)
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050088{
Alison Wangfdc2fb12012-10-18 19:25:51 +000089 ccm_t *ccm = (ccm_t *)MMAP_CCM;
90 pll_t *pll = (pll_t *)MMAP_PLL;
91 int temp, vco = 0, bootmod_ccr, pdr;
92
93 bootmod_ccr = (in_be16(&ccm->ccr) & CCM_CCR_BOOTMOD) >> 14;
94
95 switch (bootmod_ccr) {
96 case 0:
97 out_be32(&pll->pcr, 0x00000013);
98 out_be32(&pll->pdr, 0x00e70c61);
99 clock_exit_limp();
100 break;
101 case 2:
102 break;
103 case 3:
104 break;
105 }
106
107 /*Change frequency for Modelo SER1 USB host*/
108#ifdef CONFIG_LOW_MCFCLK
109 temp = in_be32(&pll->pcr);
110 temp &= ~0x3f;
111 temp |= 5;
112 out_be32(&pll->pcr, temp);
113
114 temp = in_be32(&pll->pdr);
115 temp &= ~0x001f0000;
116 temp |= 0x00040000;
117 out_be32(&pll->pdr, temp);
118 __asm__("tpf");
119#endif
120
121 setbits_be16(&ccm->misccr2, 0x02);
122
123 vco = ((in_be32(&pll->pcr) & PLL_CR_FBKDIV_BITS) + 1) *
124 CONFIG_SYS_INPUT_CLKSRC;
125 gd->vco_clk = vco;
126
127 gd->inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500128
Alison Wangfdc2fb12012-10-18 19:25:51 +0000129 pdr = in_be32(&pll->pdr);
130 temp = (pdr & PLL_DR_OUTDIV1_BITS) + 1;
131 gd->cpu_clk = vco / temp; /* cpu clock */
132 gd->flb_clk = vco / temp; /* FlexBus clock */
133 gd->flb_clk >>= 1;
134 if (in_be16(ccm->misccr2) & 2) /* fsys/4 */
135 gd->flb_clk >>= 1;
136
137 temp = ((pdr & PLL_DR_OUTDIV2_BITS) >> 5) + 1;
138 gd->bus_clk = vco / temp; /* bus clock */
139
140}
141#endif
142
143#ifdef CONFIG_MCF5445x
144void setup_5445x_clocks(void)
145{
Alison Wang8d8dac92012-03-26 21:49:08 +0000146 ccm_t *ccm = (ccm_t *)MMAP_CCM;
147 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500148 int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 };
149 int pllmult_pci[] = { 12, 6, 16, 8 };
Marek Vasute946a882012-10-03 13:28:45 +0000150 int vco = 0, temp, fbtemp, pcrvalue;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500151 int *pPllmult = NULL;
152 u16 fbpll_mask;
Marek Vasute946a882012-10-03 13:28:45 +0000153#ifdef CONFIG_PCI
154 int bPci;
155#endif
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500156
157#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000158 u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500159#endif
160 u8 bootmode;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500161
162 /* To determine PCI is present or not */
Alison Wang8d8dac92012-03-26 21:49:08 +0000163 if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) ||
164 ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500165 pPllmult = &pllmult_pci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500166 fbpll_mask = 3; /* 11b */
Marek Vasute946a882012-10-03 13:28:45 +0000167#ifdef CONFIG_PCI
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500168 bPci = 1;
Marek Vasute946a882012-10-03 13:28:45 +0000169#endif
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500170 } else {
171 pPllmult = &pllmult_nopci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500172 fbpll_mask = 7; /* 111b */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500173#ifdef CONFIG_PCI
174 gd->pci_clk = 0;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500175 bPci = 0;
Marek Vasute946a882012-10-03 13:28:45 +0000176#endif
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500177 }
178
179#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000180 bootmode = (in_8(cpld) & 0x03);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500181
182 if (bootmode != 3) {
183 /* Temporary read from CCR- fixed fb issue, must be the same clock
184 as pci or input clock, causing cpld/fpga read inconsistancy */
185 fbtemp = pPllmult[ccm->ccr & fbpll_mask];
186
187 /* Break down into small pieces, code still in flex bus */
Alison Wang8d8dac92012-03-26 21:49:08 +0000188 pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500189 temp = fbtemp - 1;
190 pcrvalue |= PLL_PCR_OUTDIV3(temp);
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500191
Alison Wang8d8dac92012-03-26 21:49:08 +0000192 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500193 }
194#endif
195#ifdef CONFIG_M54451EVB
196 /* No external logic to read the bootmode, hard coded from built */
197#ifdef CONFIG_CF_SBF
198 bootmode = 3;
199#else
200 bootmode = 2;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500201
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500202 /* default value is 16 mul, set to 20 mul */
Alison Wang8d8dac92012-03-26 21:49:08 +0000203 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000;
204 out_be32(&pll->pcr, pcrvalue);
205 while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK)
206 ;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500207#endif
208#endif
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500209
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500210 if (bootmode == 0) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500211 /* RCON mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200212 vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500213
214 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
215 /* invaild range, re-set in PCR */
Alison Wang8d8dac92012-03-26 21:49:08 +0000216 int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500217 int i, j, bus;
218
Alison Wang8d8dac92012-03-26 21:49:08 +0000219 j = (in_be32(&pll->pcr) & 0xFF000000) >> 24;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500220 for (i = j; i < 0xFF; i++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200221 vco = i * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500222 if (vco >= CLOCK_PLL_FVCO_MIN) {
223 bus = vco / temp;
224 if (bus <= CLOCK_PLL_FSYS_MIN - MHZ)
225 continue;
226 else
227 break;
228 }
229 }
Alison Wang8d8dac92012-03-26 21:49:08 +0000230 pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500231 fbtemp = ((i - 1) << 8) | ((i - 1) << 12);
232 pcrvalue |= ((i << 24) | fbtemp);
233
Alison Wang8d8dac92012-03-26 21:49:08 +0000234 out_be32(&pll->pcr, pcrvalue);
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500235 }
236 gd->vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500237 } else if (bootmode == 2) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500238 /* Normal mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000239 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500240 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
241 /* Default value */
Alison Wang8d8dac92012-03-26 21:49:08 +0000242 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
243 pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24;
244 out_be32(&pll->pcr, pcrvalue);
245 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500246 }
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500247 gd->vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500248 } else if (bootmode == 3) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500249 /* serial mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000250 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500251 gd->vco_clk = vco; /* Vco clock */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500252 }
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500253
Alison Wang8d8dac92012-03-26 21:49:08 +0000254 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500255 /* Limp mode */
256 } else {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200257 gd->inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500258
Alison Wang8d8dac92012-03-26 21:49:08 +0000259 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500260 gd->cpu_clk = vco / temp; /* cpu clock */
261
Alison Wang8d8dac92012-03-26 21:49:08 +0000262 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500263 gd->bus_clk = vco / temp; /* bus clock */
264
Alison Wang8d8dac92012-03-26 21:49:08 +0000265 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500266 gd->flb_clk = vco / temp; /* FlexBus clock */
267
268#ifdef CONFIG_PCI
269 if (bPci) {
Alison Wang8d8dac92012-03-26 21:49:08 +0000270 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500271 gd->pci_clk = vco / temp; /* PCI clock */
272 }
273#endif
274 }
275
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600276#ifdef CONFIG_FSL_I2C
277 gd->i2c1_clk = gd->bus_clk;
278#endif
Alison Wangfdc2fb12012-10-18 19:25:51 +0000279}
280#endif
281
282/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
283int get_clocks(void)
284{
285#ifdef CONFIG_MCF5441x
286 setup_5441x_clocks();
287#endif
288#ifdef CONFIG_MCF5445x
289 setup_5445x_clocks();
290#endif
291
292#ifdef CONFIG_FSL_I2C
293 gd->i2c1_clk = gd->bus_clk;
294#endif
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600295
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500296 return (0);
297}