blob: a0b9af8866b3e86f6f051faa0298eddb82f01d2d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -05002/*
3 *
Alison Wang8d8dac92012-03-26 21:49:08 +00004 * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -05005 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -05006 */
7
8#include <common.h>
Simon Glass85d65312019-12-28 10:44:58 -07009#include <clock_legacy.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050011#include <asm/processor.h>
12
13#include <asm/immap.h>
Alison Wang8d8dac92012-03-26 21:49:08 +000014#include <asm/io.h>
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050015
16DECLARE_GLOBAL_DATA_PTR;
17
18/*
19 * Low Power Divider specifications
20 */
21#define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */
22#define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */
23
24#define CLOCK_PLL_FVCO_MAX 540000000
25#define CLOCK_PLL_FVCO_MIN 300000000
26
27#define CLOCK_PLL_FSYS_MAX 266666666
28#define CLOCK_PLL_FSYS_MIN 100000000
29#define MHZ 1000000
30
31void clock_enter_limp(int lpdiv)
32{
Alison Wang8d8dac92012-03-26 21:49:08 +000033 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050034 int i, j;
35
36 /* Check bounds of divider */
37 if (lpdiv < CLOCK_LPD_MIN)
38 lpdiv = CLOCK_LPD_MIN;
39 if (lpdiv > CLOCK_LPD_MAX)
40 lpdiv = CLOCK_LPD_MAX;
41
42 /* Round divider down to nearest power of two */
43 for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ;
44
Alison Wangfdc2fb12012-10-18 19:25:51 +000045#ifdef CONFIG_MCF5445x
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050046 /* Apply the divider to the system clock */
Alison Wang8d8dac92012-03-26 21:49:08 +000047 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
Alison Wangfdc2fb12012-10-18 19:25:51 +000048#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050049
50 /* Enable Limp Mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000051 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050052}
53
54/*
55 * brief Exit Limp mode
56 * warning The PLL should be set and locked prior to exiting Limp mode
57 */
58void clock_exit_limp(void)
59{
Alison Wang8d8dac92012-03-26 21:49:08 +000060 ccm_t *ccm = (ccm_t *)MMAP_CCM;
61 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050062
63 /* Exit Limp mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000064 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050065
66 /* Wait for the PLL to lock */
Alison Wang8d8dac92012-03-26 21:49:08 +000067 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
68 ;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050069}
70
Alison Wangfdc2fb12012-10-18 19:25:51 +000071#ifdef CONFIG_MCF5441x
72void setup_5441x_clocks(void)
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050073{
Alison Wangfdc2fb12012-10-18 19:25:51 +000074 ccm_t *ccm = (ccm_t *)MMAP_CCM;
75 pll_t *pll = (pll_t *)MMAP_PLL;
76 int temp, vco = 0, bootmod_ccr, pdr;
77
78 bootmod_ccr = (in_be16(&ccm->ccr) & CCM_CCR_BOOTMOD) >> 14;
79
80 switch (bootmod_ccr) {
81 case 0:
82 out_be32(&pll->pcr, 0x00000013);
83 out_be32(&pll->pdr, 0x00e70c61);
84 clock_exit_limp();
85 break;
86 case 2:
87 break;
88 case 3:
89 break;
90 }
91
92 /*Change frequency for Modelo SER1 USB host*/
93#ifdef CONFIG_LOW_MCFCLK
94 temp = in_be32(&pll->pcr);
95 temp &= ~0x3f;
96 temp |= 5;
97 out_be32(&pll->pcr, temp);
98
99 temp = in_be32(&pll->pdr);
100 temp &= ~0x001f0000;
101 temp |= 0x00040000;
102 out_be32(&pll->pdr, temp);
103 __asm__("tpf");
104#endif
105
106 setbits_be16(&ccm->misccr2, 0x02);
107
108 vco = ((in_be32(&pll->pcr) & PLL_CR_FBKDIV_BITS) + 1) *
109 CONFIG_SYS_INPUT_CLKSRC;
Jason Jin2ecfc782013-06-26 10:21:31 +0800110 gd->arch.vco_clk = vco;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000111
Jason Jin2ecfc782013-06-26 10:21:31 +0800112 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500113
Alison Wangfdc2fb12012-10-18 19:25:51 +0000114 pdr = in_be32(&pll->pdr);
115 temp = (pdr & PLL_DR_OUTDIV1_BITS) + 1;
116 gd->cpu_clk = vco / temp; /* cpu clock */
Jason Jin2ecfc782013-06-26 10:21:31 +0800117 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
118 gd->arch.flb_clk >>= 1;
Vasili Galka24f413b2014-06-30 12:59:06 +0300119 if (in_be16(&ccm->misccr2) & 2) /* fsys/4 */
Jason Jin2ecfc782013-06-26 10:21:31 +0800120 gd->arch.flb_clk >>= 1;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000121
122 temp = ((pdr & PLL_DR_OUTDIV2_BITS) >> 5) + 1;
123 gd->bus_clk = vco / temp; /* bus clock */
124
Angelo Dureghello95a69982018-01-25 22:42:52 +0100125 temp = ((pdr & PLL_DR_OUTDIV3_BITS) >> 10) + 1;
126 gd->arch.sdhc_clk = vco / temp;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000127}
128#endif
129
130#ifdef CONFIG_MCF5445x
131void setup_5445x_clocks(void)
132{
Alison Wang8d8dac92012-03-26 21:49:08 +0000133 ccm_t *ccm = (ccm_t *)MMAP_CCM;
134 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500135 int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 };
136 int pllmult_pci[] = { 12, 6, 16, 8 };
Marek Vasute946a882012-10-03 13:28:45 +0000137 int vco = 0, temp, fbtemp, pcrvalue;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500138 int *pPllmult = NULL;
139 u16 fbpll_mask;
Marek Vasute946a882012-10-03 13:28:45 +0000140#ifdef CONFIG_PCI
141 int bPci;
142#endif
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500143
144#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000145 u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500146#endif
147 u8 bootmode;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500148
149 /* To determine PCI is present or not */
Alison Wang8d8dac92012-03-26 21:49:08 +0000150 if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) ||
151 ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500152 pPllmult = &pllmult_pci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500153 fbpll_mask = 3; /* 11b */
Marek Vasute946a882012-10-03 13:28:45 +0000154#ifdef CONFIG_PCI
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500155 bPci = 1;
Marek Vasute946a882012-10-03 13:28:45 +0000156#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500157 } else {
158 pPllmult = &pllmult_nopci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500159 fbpll_mask = 7; /* 111b */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500160#ifdef CONFIG_PCI
161 gd->pci_clk = 0;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500162 bPci = 0;
Marek Vasute946a882012-10-03 13:28:45 +0000163#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500164 }
165
166#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000167 bootmode = (in_8(cpld) & 0x03);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500168
169 if (bootmode != 3) {
170 /* Temporary read from CCR- fixed fb issue, must be the same clock
171 as pci or input clock, causing cpld/fpga read inconsistancy */
172 fbtemp = pPllmult[ccm->ccr & fbpll_mask];
173
174 /* Break down into small pieces, code still in flex bus */
Alison Wang8d8dac92012-03-26 21:49:08 +0000175 pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500176 temp = fbtemp - 1;
177 pcrvalue |= PLL_PCR_OUTDIV3(temp);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500178
Alison Wang8d8dac92012-03-26 21:49:08 +0000179 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500180 }
181#endif
182#ifdef CONFIG_M54451EVB
183 /* No external logic to read the bootmode, hard coded from built */
184#ifdef CONFIG_CF_SBF
185 bootmode = 3;
186#else
187 bootmode = 2;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500188
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500189 /* default value is 16 mul, set to 20 mul */
Alison Wang8d8dac92012-03-26 21:49:08 +0000190 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000;
191 out_be32(&pll->pcr, pcrvalue);
192 while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK)
193 ;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500194#endif
195#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500196
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500197 if (bootmode == 0) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500198 /* RCON mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200199 vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500200
201 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
202 /* invaild range, re-set in PCR */
Alison Wang8d8dac92012-03-26 21:49:08 +0000203 int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500204 int i, j, bus;
205
Alison Wang8d8dac92012-03-26 21:49:08 +0000206 j = (in_be32(&pll->pcr) & 0xFF000000) >> 24;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500207 for (i = j; i < 0xFF; i++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200208 vco = i * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500209 if (vco >= CLOCK_PLL_FVCO_MIN) {
210 bus = vco / temp;
211 if (bus <= CLOCK_PLL_FSYS_MIN - MHZ)
212 continue;
213 else
214 break;
215 }
216 }
Alison Wang8d8dac92012-03-26 21:49:08 +0000217 pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500218 fbtemp = ((i - 1) << 8) | ((i - 1) << 12);
219 pcrvalue |= ((i << 24) | fbtemp);
220
Alison Wang8d8dac92012-03-26 21:49:08 +0000221 out_be32(&pll->pcr, pcrvalue);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500222 }
Simon Glass568a7b62012-12-13 20:49:07 +0000223 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500224 } else if (bootmode == 2) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500225 /* Normal mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000226 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500227 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
228 /* Default value */
Alison Wang8d8dac92012-03-26 21:49:08 +0000229 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
230 pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24;
231 out_be32(&pll->pcr, pcrvalue);
232 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500233 }
Simon Glass568a7b62012-12-13 20:49:07 +0000234 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500235 } else if (bootmode == 3) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500236 /* serial mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000237 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass568a7b62012-12-13 20:49:07 +0000238 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500239 }
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500240
Alison Wang8d8dac92012-03-26 21:49:08 +0000241 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500242 /* Limp mode */
243 } else {
Simon Glass568a7b62012-12-13 20:49:07 +0000244 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500245
Alison Wang8d8dac92012-03-26 21:49:08 +0000246 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500247 gd->cpu_clk = vco / temp; /* cpu clock */
248
Alison Wang8d8dac92012-03-26 21:49:08 +0000249 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500250 gd->bus_clk = vco / temp; /* bus clock */
251
Alison Wang8d8dac92012-03-26 21:49:08 +0000252 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1;
Simon Glass568a7b62012-12-13 20:49:07 +0000253 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500254
255#ifdef CONFIG_PCI
256 if (bPci) {
Alison Wang8d8dac92012-03-26 21:49:08 +0000257 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500258 gd->pci_clk = vco / temp; /* PCI clock */
259 }
260#endif
261 }
262
Heiko Schocherf2850742012-10-24 13:48:22 +0200263#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000264 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600265#endif
Alison Wangfdc2fb12012-10-18 19:25:51 +0000266}
267#endif
268
269/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
270int get_clocks(void)
271{
272#ifdef CONFIG_MCF5441x
273 setup_5441x_clocks();
274#endif
275#ifdef CONFIG_MCF5445x
276 setup_5445x_clocks();
277#endif
278
Heiko Schocherf2850742012-10-24 13:48:22 +0200279#ifdef CONFIG_SYS_FSL_I2C
Simon Glassc2baaec2012-12-13 20:48:49 +0000280 gd->arch.i2c1_clk = gd->bus_clk;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000281#endif
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600282
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500283 return (0);
284}