blob: fa9d5cb7887c3419be1839bb2ef14d6af081e8c3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiewae831cd2008-01-14 17:46:19 -06002/*
3 *
Alison Wang8bce3ec2012-03-26 21:49:03 +00004 * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiewae831cd2008-01-14 17:46:19 -06005 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
TsiChungLiewae831cd2008-01-14 17:46:19 -06006 */
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>
TsiChungLiewae831cd2008-01-14 17:46:19 -060011#include <asm/processor.h>
12
13#include <asm/immap.h>
Alison Wang8bce3ec2012-03-26 21:49:03 +000014#include <asm/io.h>
TsiChungLiewae831cd2008-01-14 17:46:19 -060015
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 Wang8bce3ec2012-03-26 21:49:03 +000033 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewae831cd2008-01-14 17:46:19 -060034 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
45 /* Apply the divider to the system clock */
Alison Wang8bce3ec2012-03-26 21:49:03 +000046 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
TsiChungLiewae831cd2008-01-14 17:46:19 -060047
48 /* Enable Limp Mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000049 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewae831cd2008-01-14 17:46:19 -060050}
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 Wang8bce3ec2012-03-26 21:49:03 +000058 ccm_t *ccm = (ccm_t *)MMAP_CCM;
59 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewae831cd2008-01-14 17:46:19 -060060
61 /* Exit Limp mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000062 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewae831cd2008-01-14 17:46:19 -060063
64 /* Wait for the PLL to lock */
Alison Wang8bce3ec2012-03-26 21:49:03 +000065 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
66 ;
TsiChungLiewae831cd2008-01-14 17:46:19 -060067}
68
69/*
70 * get_clocks() fills in gd->cpu_clock and gd->bus_clk
71 */
72int get_clocks(void)
73{
74
Alison Wang8bce3ec2012-03-26 21:49:03 +000075 ccm_t *ccm = (ccm_t *)MMAP_CCM;
76 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewae831cd2008-01-14 17:46:19 -060077 int vco, temp, pcrvalue, pfdr;
78 u8 bootmode;
79
Alison Wang8bce3ec2012-03-26 21:49:03 +000080 pcrvalue = in_be32(&pll->pcr) & 0xFF0F0FFF;
TsiChungLiewae831cd2008-01-14 17:46:19 -060081 pfdr = pcrvalue >> 24;
82
TsiChung Liew39966e32008-10-21 15:37:02 +000083 if (pfdr == 0x1E)
84 bootmode = 0; /* Normal Mode */
85
86#ifdef CONFIG_CF_SBF
87 bootmode = 3; /* Serial Mode */
88#endif
89
90 if (bootmode == 0) {
91 /* Normal mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000092 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew39966e32008-10-21 15:37:02 +000093 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
94 /* Default value */
Alison Wang8bce3ec2012-03-26 21:49:03 +000095 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
TsiChung Liew39966e32008-10-21 15:37:02 +000096 pcrvalue |= 0x1E << 24;
Alison Wang8bce3ec2012-03-26 21:49:03 +000097 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew39966e32008-10-21 15:37:02 +000098 vco =
Alison Wang8bce3ec2012-03-26 21:49:03 +000099 ((in_be32(&pll->pcr) & 0xFF000000) >> 24) *
TsiChung Liew39966e32008-10-21 15:37:02 +0000100 CONFIG_SYS_INPUT_CLKSRC;
101 }
Simon Glass568a7b62012-12-13 20:49:07 +0000102 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew39966e32008-10-21 15:37:02 +0000103 } else if (bootmode == 3) {
TsiChungLiewae831cd2008-01-14 17:46:19 -0600104 /* serial mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +0000105 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass568a7b62012-12-13 20:49:07 +0000106 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiewae831cd2008-01-14 17:46:19 -0600107 }
108
Alison Wang8bce3ec2012-03-26 21:49:03 +0000109 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewae831cd2008-01-14 17:46:19 -0600110 /* Limp mode */
111 } else {
Simon Glass568a7b62012-12-13 20:49:07 +0000112 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewae831cd2008-01-14 17:46:19 -0600113
Alison Wang8bce3ec2012-03-26 21:49:03 +0000114 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewae831cd2008-01-14 17:46:19 -0600115 gd->cpu_clk = vco / temp; /* cpu clock */
116
Alison Wang8bce3ec2012-03-26 21:49:03 +0000117 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
Simon Glass568a7b62012-12-13 20:49:07 +0000118 gd->arch.flb_clk = vco / temp; /* flexbus clock */
119 gd->bus_clk = gd->arch.flb_clk;
TsiChungLiewae831cd2008-01-14 17:46:19 -0600120 }
121
Heiko Schocherf2850742012-10-24 13:48:22 +0200122#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000123 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600124#endif
125
TsiChungLiewae831cd2008-01-14 17:46:19 -0600126 return (0);
127}