blob: 5214730a0fea09641b19ca73c1e85608ef99c06b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiew8ae158c2007-08-16 15:05:11 -05002/*
3 *
Alison Wang198cafb2012-03-26 21:49:08 +00004 * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiew8ae158c2007-08-16 15:05:11 -05005 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChungLiew8ae158c2007-08-16 15:05:11 -05006 */
7
8#include <common.h>
9#include <asm/processor.h>
10
11#include <asm/immap.h>
Alison Wang198cafb2012-03-26 21:49:08 +000012#include <asm/io.h>
TsiChungLiew8ae158c2007-08-16 15:05:11 -050013
14DECLARE_GLOBAL_DATA_PTR;
15
16/*
17 * Low Power Divider specifications
18 */
19#define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */
20#define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */
21
22#define CLOCK_PLL_FVCO_MAX 540000000
23#define CLOCK_PLL_FVCO_MIN 300000000
24
25#define CLOCK_PLL_FSYS_MAX 266666666
26#define CLOCK_PLL_FSYS_MIN 100000000
27#define MHZ 1000000
28
29void clock_enter_limp(int lpdiv)
30{
Alison Wang198cafb2012-03-26 21:49:08 +000031 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiew8ae158c2007-08-16 15:05:11 -050032 int i, j;
33
34 /* Check bounds of divider */
35 if (lpdiv < CLOCK_LPD_MIN)
36 lpdiv = CLOCK_LPD_MIN;
37 if (lpdiv > CLOCK_LPD_MAX)
38 lpdiv = CLOCK_LPD_MAX;
39
40 /* Round divider down to nearest power of two */
41 for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ;
42
Alison Wang45370e12012-10-18 19:25:51 +000043#ifdef CONFIG_MCF5445x
TsiChungLiew8ae158c2007-08-16 15:05:11 -050044 /* Apply the divider to the system clock */
Alison Wang198cafb2012-03-26 21:49:08 +000045 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
Alison Wang45370e12012-10-18 19:25:51 +000046#endif
TsiChungLiew8ae158c2007-08-16 15:05:11 -050047
48 /* Enable Limp Mode */
Alison Wang198cafb2012-03-26 21:49:08 +000049 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiew8ae158c2007-08-16 15:05:11 -050050}
51
52/*
53 * brief Exit Limp mode
54 * warning The PLL should be set and locked prior to exiting Limp mode
55 */
56void clock_exit_limp(void)
57{
Alison Wang198cafb2012-03-26 21:49:08 +000058 ccm_t *ccm = (ccm_t *)MMAP_CCM;
59 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiew8ae158c2007-08-16 15:05:11 -050060
61 /* Exit Limp mode */
Alison Wang198cafb2012-03-26 21:49:08 +000062 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiew8ae158c2007-08-16 15:05:11 -050063
64 /* Wait for the PLL to lock */
Alison Wang198cafb2012-03-26 21:49:08 +000065 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
66 ;
TsiChungLiew8ae158c2007-08-16 15:05:11 -050067}
68
Alison Wang45370e12012-10-18 19:25:51 +000069#ifdef CONFIG_MCF5441x
70void setup_5441x_clocks(void)
TsiChungLiew8ae158c2007-08-16 15:05:11 -050071{
Alison Wang45370e12012-10-18 19:25:51 +000072 ccm_t *ccm = (ccm_t *)MMAP_CCM;
73 pll_t *pll = (pll_t *)MMAP_PLL;
74 int temp, vco = 0, bootmod_ccr, pdr;
TsiChung Liew9f751552008-07-23 20:38:53 -050075
Alison Wang45370e12012-10-18 19:25:51 +000076 bootmod_ccr = (in_be16(&ccm->ccr) & CCM_CCR_BOOTMOD) >> 14;
77
78 switch (bootmod_ccr) {
79 case 0:
80 out_be32(&pll->pcr, 0x00000013);
81 out_be32(&pll->pdr, 0x00e70c61);
82 clock_exit_limp();
83 break;
84 case 2:
85 break;
86 case 3:
87 break;
88 }
89
90 /*Change frequency for Modelo SER1 USB host*/
91#ifdef CONFIG_LOW_MCFCLK
92 temp = in_be32(&pll->pcr);
93 temp &= ~0x3f;
94 temp |= 5;
95 out_be32(&pll->pcr, temp);
96
97 temp = in_be32(&pll->pdr);
98 temp &= ~0x001f0000;
99 temp |= 0x00040000;
100 out_be32(&pll->pdr, temp);
101 __asm__("tpf");
102#endif
103
104 setbits_be16(&ccm->misccr2, 0x02);
105
106 vco = ((in_be32(&pll->pcr) & PLL_CR_FBKDIV_BITS) + 1) *
107 CONFIG_SYS_INPUT_CLKSRC;
Jason Jin1b9591c2013-06-26 10:21:31 +0800108 gd->arch.vco_clk = vco;
Alison Wang45370e12012-10-18 19:25:51 +0000109
Jason Jin1b9591c2013-06-26 10:21:31 +0800110 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
Alison Wang45370e12012-10-18 19:25:51 +0000111
112 pdr = in_be32(&pll->pdr);
113 temp = (pdr & PLL_DR_OUTDIV1_BITS) + 1;
114 gd->cpu_clk = vco / temp; /* cpu clock */
Jason Jin1b9591c2013-06-26 10:21:31 +0800115 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
116 gd->arch.flb_clk >>= 1;
Vasili Galka6b02d062014-06-30 12:59:06 +0300117 if (in_be16(&ccm->misccr2) & 2) /* fsys/4 */
Jason Jin1b9591c2013-06-26 10:21:31 +0800118 gd->arch.flb_clk >>= 1;
Alison Wang45370e12012-10-18 19:25:51 +0000119
120 temp = ((pdr & PLL_DR_OUTDIV2_BITS) >> 5) + 1;
121 gd->bus_clk = vco / temp; /* bus clock */
122
123}
124#endif
125
126#ifdef CONFIG_MCF5445x
127void setup_5445x_clocks(void)
128{
Alison Wang198cafb2012-03-26 21:49:08 +0000129 ccm_t *ccm = (ccm_t *)MMAP_CCM;
130 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500131 int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 };
132 int pllmult_pci[] = { 12, 6, 16, 8 };
Marek Vasutc1568ca52012-10-03 13:28:45 +0000133 int vco = 0, temp, fbtemp, pcrvalue;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500134 int *pPllmult = NULL;
135 u16 fbpll_mask;
Marek Vasutc1568ca52012-10-03 13:28:45 +0000136#ifdef CONFIG_PCI
137 int bPci;
138#endif
TsiChung Liew9f751552008-07-23 20:38:53 -0500139
140#ifdef CONFIG_M54455EVB
Alison Wang198cafb2012-03-26 21:49:08 +0000141 u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3);
TsiChung Liew9f751552008-07-23 20:38:53 -0500142#endif
143 u8 bootmode;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500144
145 /* To determine PCI is present or not */
Alison Wang198cafb2012-03-26 21:49:08 +0000146 if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) ||
147 ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) {
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500148 pPllmult = &pllmult_pci[0];
TsiChung Liew9f751552008-07-23 20:38:53 -0500149 fbpll_mask = 3; /* 11b */
Marek Vasutc1568ca52012-10-03 13:28:45 +0000150#ifdef CONFIG_PCI
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500151 bPci = 1;
Marek Vasutc1568ca52012-10-03 13:28:45 +0000152#endif
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500153 } else {
154 pPllmult = &pllmult_nopci[0];
TsiChung Liew9f751552008-07-23 20:38:53 -0500155 fbpll_mask = 7; /* 111b */
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500156#ifdef CONFIG_PCI
157 gd->pci_clk = 0;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500158 bPci = 0;
Marek Vasutc1568ca52012-10-03 13:28:45 +0000159#endif
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500160 }
161
162#ifdef CONFIG_M54455EVB
Alison Wang198cafb2012-03-26 21:49:08 +0000163 bootmode = (in_8(cpld) & 0x03);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500164
TsiChung Liew9f751552008-07-23 20:38:53 -0500165 if (bootmode != 3) {
166 /* Temporary read from CCR- fixed fb issue, must be the same clock
167 as pci or input clock, causing cpld/fpga read inconsistancy */
168 fbtemp = pPllmult[ccm->ccr & fbpll_mask];
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500169
TsiChung Liew9f751552008-07-23 20:38:53 -0500170 /* Break down into small pieces, code still in flex bus */
Alison Wang198cafb2012-03-26 21:49:08 +0000171 pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF;
TsiChung Liew9f751552008-07-23 20:38:53 -0500172 temp = fbtemp - 1;
173 pcrvalue |= PLL_PCR_OUTDIV3(temp);
174
Alison Wang198cafb2012-03-26 21:49:08 +0000175 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew9f751552008-07-23 20:38:53 -0500176 }
177#endif
178#ifdef CONFIG_M54451EVB
179 /* No external logic to read the bootmode, hard coded from built */
180#ifdef CONFIG_CF_SBF
181 bootmode = 3;
182#else
183 bootmode = 2;
184
185 /* default value is 16 mul, set to 20 mul */
Alison Wang198cafb2012-03-26 21:49:08 +0000186 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000;
187 out_be32(&pll->pcr, pcrvalue);
188 while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK)
189 ;
TsiChung Liew9f751552008-07-23 20:38:53 -0500190#endif
191#endif
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500192
TsiChung Liew9f751552008-07-23 20:38:53 -0500193 if (bootmode == 0) {
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500194 /* RCON mode */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200195 vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500196
197 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
198 /* invaild range, re-set in PCR */
Alison Wang198cafb2012-03-26 21:49:08 +0000199 int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500200 int i, j, bus;
201
Alison Wang198cafb2012-03-26 21:49:08 +0000202 j = (in_be32(&pll->pcr) & 0xFF000000) >> 24;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500203 for (i = j; i < 0xFF; i++) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200204 vco = i * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500205 if (vco >= CLOCK_PLL_FVCO_MIN) {
206 bus = vco / temp;
207 if (bus <= CLOCK_PLL_FSYS_MIN - MHZ)
208 continue;
209 else
210 break;
211 }
212 }
Alison Wang198cafb2012-03-26 21:49:08 +0000213 pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500214 fbtemp = ((i - 1) << 8) | ((i - 1) << 12);
215 pcrvalue |= ((i << 24) | fbtemp);
216
Alison Wang198cafb2012-03-26 21:49:08 +0000217 out_be32(&pll->pcr, pcrvalue);
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500218 }
Simon Glass7e2592f2012-12-13 20:49:07 +0000219 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew9f751552008-07-23 20:38:53 -0500220 } else if (bootmode == 2) {
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500221 /* Normal mode */
Alison Wang198cafb2012-03-26 21:49:08 +0000222 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew9f751552008-07-23 20:38:53 -0500223 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
224 /* Default value */
Alison Wang198cafb2012-03-26 21:49:08 +0000225 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
226 pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24;
227 out_be32(&pll->pcr, pcrvalue);
228 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew9f751552008-07-23 20:38:53 -0500229 }
Simon Glass7e2592f2012-12-13 20:49:07 +0000230 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew9f751552008-07-23 20:38:53 -0500231 } else if (bootmode == 3) {
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500232 /* serial mode */
Alison Wang198cafb2012-03-26 21:49:08 +0000233 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass7e2592f2012-12-13 20:49:07 +0000234 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500235 }
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500236
Alison Wang198cafb2012-03-26 21:49:08 +0000237 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500238 /* Limp mode */
239 } else {
Simon Glass7e2592f2012-12-13 20:49:07 +0000240 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500241
Alison Wang198cafb2012-03-26 21:49:08 +0000242 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500243 gd->cpu_clk = vco / temp; /* cpu clock */
244
Alison Wang198cafb2012-03-26 21:49:08 +0000245 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500246 gd->bus_clk = vco / temp; /* bus clock */
247
Alison Wang198cafb2012-03-26 21:49:08 +0000248 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1;
Simon Glass7e2592f2012-12-13 20:49:07 +0000249 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500250
251#ifdef CONFIG_PCI
252 if (bPci) {
Alison Wang198cafb2012-03-26 21:49:08 +0000253 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1;
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500254 gd->pci_clk = vco / temp; /* PCI clock */
255 }
256#endif
257 }
258
Heiko Schocher00f792e2012-10-24 13:48:22 +0200259#ifdef CONFIG_SYS_I2C_FSL
Simon Glass609e6ec2012-12-13 20:48:49 +0000260 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Lieweec567a2008-08-19 03:01:19 +0600261#endif
Alison Wang45370e12012-10-18 19:25:51 +0000262}
263#endif
264
265/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
266int get_clocks(void)
267{
268#ifdef CONFIG_MCF5441x
269 setup_5441x_clocks();
270#endif
271#ifdef CONFIG_MCF5445x
272 setup_5445x_clocks();
273#endif
274
Heiko Schocher00f792e2012-10-24 13:48:22 +0200275#ifdef CONFIG_SYS_FSL_I2C
Simon Glass609e6ec2012-12-13 20:48:49 +0000276 gd->arch.i2c1_clk = gd->bus_clk;
Alison Wang45370e12012-10-18 19:25:51 +0000277#endif
TsiChung Lieweec567a2008-08-19 03:01:19 +0600278
TsiChungLiew8ae158c2007-08-16 15:05:11 -0500279 return (0);
280}