blob: eaa3b39bec6937fb2d339bfa407374126090f5fe [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>
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050010#include <asm/processor.h>
11
12#include <asm/immap.h>
Alison Wang8d8dac92012-03-26 21:49:08 +000013#include <asm/io.h>
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050014
15DECLARE_GLOBAL_DATA_PTR;
16
17/*
18 * Low Power Divider specifications
19 */
20#define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */
21#define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */
22
23#define CLOCK_PLL_FVCO_MAX 540000000
24#define CLOCK_PLL_FVCO_MIN 300000000
25
26#define CLOCK_PLL_FSYS_MAX 266666666
27#define CLOCK_PLL_FSYS_MIN 100000000
28#define MHZ 1000000
29
30void clock_enter_limp(int lpdiv)
31{
Alison Wang8d8dac92012-03-26 21:49:08 +000032 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050033 int i, j;
34
35 /* Check bounds of divider */
36 if (lpdiv < CLOCK_LPD_MIN)
37 lpdiv = CLOCK_LPD_MIN;
38 if (lpdiv > CLOCK_LPD_MAX)
39 lpdiv = CLOCK_LPD_MAX;
40
41 /* Round divider down to nearest power of two */
42 for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ;
43
Alison Wangfdc2fb12012-10-18 19:25:51 +000044#ifdef CONFIG_MCF5445x
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050045 /* Apply the divider to the system clock */
Alison Wang8d8dac92012-03-26 21:49:08 +000046 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
Alison Wangfdc2fb12012-10-18 19:25:51 +000047#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050048
49 /* Enable Limp Mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000050 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050051}
52
53/*
54 * brief Exit Limp mode
55 * warning The PLL should be set and locked prior to exiting Limp mode
56 */
57void clock_exit_limp(void)
58{
Alison Wang8d8dac92012-03-26 21:49:08 +000059 ccm_t *ccm = (ccm_t *)MMAP_CCM;
60 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050061
62 /* Exit Limp mode */
Alison Wang8d8dac92012-03-26 21:49:08 +000063 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050064
65 /* Wait for the PLL to lock */
Alison Wang8d8dac92012-03-26 21:49:08 +000066 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
67 ;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050068}
69
Alison Wangfdc2fb12012-10-18 19:25:51 +000070#ifdef CONFIG_MCF5441x
71void setup_5441x_clocks(void)
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -050072{
Alison Wangfdc2fb12012-10-18 19:25:51 +000073 ccm_t *ccm = (ccm_t *)MMAP_CCM;
74 pll_t *pll = (pll_t *)MMAP_PLL;
75 int temp, vco = 0, bootmod_ccr, pdr;
76
77 bootmod_ccr = (in_be16(&ccm->ccr) & CCM_CCR_BOOTMOD) >> 14;
78
79 switch (bootmod_ccr) {
80 case 0:
81 out_be32(&pll->pcr, 0x00000013);
82 out_be32(&pll->pdr, 0x00e70c61);
83 clock_exit_limp();
84 break;
85 case 2:
86 break;
87 case 3:
88 break;
89 }
90
91 /*Change frequency for Modelo SER1 USB host*/
92#ifdef CONFIG_LOW_MCFCLK
93 temp = in_be32(&pll->pcr);
94 temp &= ~0x3f;
95 temp |= 5;
96 out_be32(&pll->pcr, temp);
97
98 temp = in_be32(&pll->pdr);
99 temp &= ~0x001f0000;
100 temp |= 0x00040000;
101 out_be32(&pll->pdr, temp);
102 __asm__("tpf");
103#endif
104
105 setbits_be16(&ccm->misccr2, 0x02);
106
107 vco = ((in_be32(&pll->pcr) & PLL_CR_FBKDIV_BITS) + 1) *
108 CONFIG_SYS_INPUT_CLKSRC;
Jason Jin2ecfc782013-06-26 10:21:31 +0800109 gd->arch.vco_clk = vco;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000110
Jason Jin2ecfc782013-06-26 10:21:31 +0800111 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500112
Alison Wangfdc2fb12012-10-18 19:25:51 +0000113 pdr = in_be32(&pll->pdr);
114 temp = (pdr & PLL_DR_OUTDIV1_BITS) + 1;
115 gd->cpu_clk = vco / temp; /* cpu clock */
Jason Jin2ecfc782013-06-26 10:21:31 +0800116 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
117 gd->arch.flb_clk >>= 1;
Vasili Galka24f413b2014-06-30 12:59:06 +0300118 if (in_be16(&ccm->misccr2) & 2) /* fsys/4 */
Jason Jin2ecfc782013-06-26 10:21:31 +0800119 gd->arch.flb_clk >>= 1;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000120
121 temp = ((pdr & PLL_DR_OUTDIV2_BITS) >> 5) + 1;
122 gd->bus_clk = vco / temp; /* bus clock */
123
Angelo Dureghello95a69982018-01-25 22:42:52 +0100124 temp = ((pdr & PLL_DR_OUTDIV3_BITS) >> 10) + 1;
125 gd->arch.sdhc_clk = vco / temp;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000126}
127#endif
128
129#ifdef CONFIG_MCF5445x
130void setup_5445x_clocks(void)
131{
Alison Wang8d8dac92012-03-26 21:49:08 +0000132 ccm_t *ccm = (ccm_t *)MMAP_CCM;
133 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500134 int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 };
135 int pllmult_pci[] = { 12, 6, 16, 8 };
Marek Vasute946a882012-10-03 13:28:45 +0000136 int vco = 0, temp, fbtemp, pcrvalue;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500137 int *pPllmult = NULL;
138 u16 fbpll_mask;
Marek Vasute946a882012-10-03 13:28:45 +0000139#ifdef CONFIG_PCI
140 int bPci;
141#endif
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500142
143#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000144 u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500145#endif
146 u8 bootmode;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500147
148 /* To determine PCI is present or not */
Alison Wang8d8dac92012-03-26 21:49:08 +0000149 if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) ||
150 ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500151 pPllmult = &pllmult_pci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500152 fbpll_mask = 3; /* 11b */
Marek Vasute946a882012-10-03 13:28:45 +0000153#ifdef CONFIG_PCI
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500154 bPci = 1;
Marek Vasute946a882012-10-03 13:28:45 +0000155#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500156 } else {
157 pPllmult = &pllmult_nopci[0];
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500158 fbpll_mask = 7; /* 111b */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500159#ifdef CONFIG_PCI
160 gd->pci_clk = 0;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500161 bPci = 0;
Marek Vasute946a882012-10-03 13:28:45 +0000162#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500163 }
164
165#ifdef CONFIG_M54455EVB
Alison Wang8d8dac92012-03-26 21:49:08 +0000166 bootmode = (in_8(cpld) & 0x03);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500167
168 if (bootmode != 3) {
169 /* Temporary read from CCR- fixed fb issue, must be the same clock
170 as pci or input clock, causing cpld/fpga read inconsistancy */
171 fbtemp = pPllmult[ccm->ccr & fbpll_mask];
172
173 /* Break down into small pieces, code still in flex bus */
Alison Wang8d8dac92012-03-26 21:49:08 +0000174 pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500175 temp = fbtemp - 1;
176 pcrvalue |= PLL_PCR_OUTDIV3(temp);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500177
Alison Wang8d8dac92012-03-26 21:49:08 +0000178 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500179 }
180#endif
181#ifdef CONFIG_M54451EVB
182 /* No external logic to read the bootmode, hard coded from built */
183#ifdef CONFIG_CF_SBF
184 bootmode = 3;
185#else
186 bootmode = 2;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500187
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500188 /* default value is 16 mul, set to 20 mul */
Alison Wang8d8dac92012-03-26 21:49:08 +0000189 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000;
190 out_be32(&pll->pcr, pcrvalue);
191 while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK)
192 ;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500193#endif
194#endif
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500195
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500196 if (bootmode == 0) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500197 /* RCON mode */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200198 vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500199
200 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
201 /* invaild range, re-set in PCR */
Alison Wang8d8dac92012-03-26 21:49:08 +0000202 int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500203 int i, j, bus;
204
Alison Wang8d8dac92012-03-26 21:49:08 +0000205 j = (in_be32(&pll->pcr) & 0xFF000000) >> 24;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500206 for (i = j; i < 0xFF; i++) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200207 vco = i * CONFIG_SYS_INPUT_CLKSRC;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500208 if (vco >= CLOCK_PLL_FVCO_MIN) {
209 bus = vco / temp;
210 if (bus <= CLOCK_PLL_FSYS_MIN - MHZ)
211 continue;
212 else
213 break;
214 }
215 }
Alison Wang8d8dac92012-03-26 21:49:08 +0000216 pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500217 fbtemp = ((i - 1) << 8) | ((i - 1) << 12);
218 pcrvalue |= ((i << 24) | fbtemp);
219
Alison Wang8d8dac92012-03-26 21:49:08 +0000220 out_be32(&pll->pcr, pcrvalue);
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500221 }
Simon Glass568a7b62012-12-13 20:49:07 +0000222 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500223 } else if (bootmode == 2) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500224 /* Normal mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000225 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500226 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
227 /* Default value */
Alison Wang8d8dac92012-03-26 21:49:08 +0000228 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
229 pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24;
230 out_be32(&pll->pcr, pcrvalue);
231 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500232 }
Simon Glass568a7b62012-12-13 20:49:07 +0000233 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew23cf8fd2008-07-23 20:38:53 -0500234 } else if (bootmode == 3) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500235 /* serial mode */
Alison Wang8d8dac92012-03-26 21:49:08 +0000236 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass568a7b62012-12-13 20:49:07 +0000237 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500238 }
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500239
Alison Wang8d8dac92012-03-26 21:49:08 +0000240 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500241 /* Limp mode */
242 } else {
Simon Glass568a7b62012-12-13 20:49:07 +0000243 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500244
Alison Wang8d8dac92012-03-26 21:49:08 +0000245 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500246 gd->cpu_clk = vco / temp; /* cpu clock */
247
Alison Wang8d8dac92012-03-26 21:49:08 +0000248 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500249 gd->bus_clk = vco / temp; /* bus clock */
250
Alison Wang8d8dac92012-03-26 21:49:08 +0000251 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1;
Simon Glass568a7b62012-12-13 20:49:07 +0000252 gd->arch.flb_clk = vco / temp; /* FlexBus clock */
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500253
254#ifdef CONFIG_PCI
255 if (bPci) {
Alison Wang8d8dac92012-03-26 21:49:08 +0000256 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1;
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500257 gd->pci_clk = vco / temp; /* PCI clock */
258 }
259#endif
260 }
261
Heiko Schocherf2850742012-10-24 13:48:22 +0200262#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000263 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600264#endif
Alison Wangfdc2fb12012-10-18 19:25:51 +0000265}
266#endif
267
268/* get_clocks() fills in gd->cpu_clock and gd->bus_clk */
269int get_clocks(void)
270{
271#ifdef CONFIG_MCF5441x
272 setup_5441x_clocks();
273#endif
274#ifdef CONFIG_MCF5445x
275 setup_5445x_clocks();
276#endif
277
Heiko Schocherf2850742012-10-24 13:48:22 +0200278#ifdef CONFIG_SYS_FSL_I2C
Simon Glassc2baaec2012-12-13 20:48:49 +0000279 gd->arch.i2c1_clk = gd->bus_clk;
Alison Wangfdc2fb12012-10-18 19:25:51 +0000280#endif
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600281
TsiChungLiewfc3ca3b62007-08-16 15:05:11 -0500282 return (0);
283}