blob: 44de4a6701ac4ce6700218623338c8582af9be2b [file] [log] [blame]
TsiChungLiewc8758102008-01-14 17:46:19 -06001/*
2 *
Alison Wang849fc422012-03-26 21:49:03 +00003 * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc.
TsiChungLiewc8758102008-01-14 17:46:19 -06004 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
TsiChungLiewc8758102008-01-14 17:46:19 -06007 */
8
9#include <common.h>
10#include <asm/processor.h>
11
12#include <asm/immap.h>
Alison Wang849fc422012-03-26 21:49:03 +000013#include <asm/io.h>
TsiChungLiewc8758102008-01-14 17:46:19 -060014
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 Wang849fc422012-03-26 21:49:03 +000032 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewc8758102008-01-14 17:46:19 -060033 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
44 /* Apply the divider to the system clock */
Alison Wang849fc422012-03-26 21:49:03 +000045 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
TsiChungLiewc8758102008-01-14 17:46:19 -060046
47 /* Enable Limp Mode */
Alison Wang849fc422012-03-26 21:49:03 +000048 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewc8758102008-01-14 17:46:19 -060049}
50
51/*
52 * brief Exit Limp mode
53 * warning The PLL should be set and locked prior to exiting Limp mode
54 */
55void clock_exit_limp(void)
56{
Alison Wang849fc422012-03-26 21:49:03 +000057 ccm_t *ccm = (ccm_t *)MMAP_CCM;
58 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewc8758102008-01-14 17:46:19 -060059
60 /* Exit Limp mode */
Alison Wang849fc422012-03-26 21:49:03 +000061 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewc8758102008-01-14 17:46:19 -060062
63 /* Wait for the PLL to lock */
Alison Wang849fc422012-03-26 21:49:03 +000064 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
65 ;
TsiChungLiewc8758102008-01-14 17:46:19 -060066}
67
68/*
69 * get_clocks() fills in gd->cpu_clock and gd->bus_clk
70 */
71int get_clocks(void)
72{
73
Alison Wang849fc422012-03-26 21:49:03 +000074 ccm_t *ccm = (ccm_t *)MMAP_CCM;
75 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewc8758102008-01-14 17:46:19 -060076 int vco, temp, pcrvalue, pfdr;
77 u8 bootmode;
78
Alison Wang849fc422012-03-26 21:49:03 +000079 pcrvalue = in_be32(&pll->pcr) & 0xFF0F0FFF;
TsiChungLiewc8758102008-01-14 17:46:19 -060080 pfdr = pcrvalue >> 24;
81
TsiChung Liewa21d0c22008-10-21 15:37:02 +000082 if (pfdr == 0x1E)
83 bootmode = 0; /* Normal Mode */
84
85#ifdef CONFIG_CF_SBF
86 bootmode = 3; /* Serial Mode */
87#endif
88
89 if (bootmode == 0) {
90 /* Normal mode */
Alison Wang849fc422012-03-26 21:49:03 +000091 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liewa21d0c22008-10-21 15:37:02 +000092 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
93 /* Default value */
Alison Wang849fc422012-03-26 21:49:03 +000094 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
TsiChung Liewa21d0c22008-10-21 15:37:02 +000095 pcrvalue |= 0x1E << 24;
Alison Wang849fc422012-03-26 21:49:03 +000096 out_be32(&pll->pcr, pcrvalue);
TsiChung Liewa21d0c22008-10-21 15:37:02 +000097 vco =
Alison Wang849fc422012-03-26 21:49:03 +000098 ((in_be32(&pll->pcr) & 0xFF000000) >> 24) *
TsiChung Liewa21d0c22008-10-21 15:37:02 +000099 CONFIG_SYS_INPUT_CLKSRC;
100 }
Simon Glass7e2592f2012-12-13 20:49:07 +0000101 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liewa21d0c22008-10-21 15:37:02 +0000102 } else if (bootmode == 3) {
TsiChungLiewc8758102008-01-14 17:46:19 -0600103 /* serial mode */
Alison Wang849fc422012-03-26 21:49:03 +0000104 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass7e2592f2012-12-13 20:49:07 +0000105 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiewc8758102008-01-14 17:46:19 -0600106 }
107
Alison Wang849fc422012-03-26 21:49:03 +0000108 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewc8758102008-01-14 17:46:19 -0600109 /* Limp mode */
110 } else {
Simon Glass7e2592f2012-12-13 20:49:07 +0000111 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewc8758102008-01-14 17:46:19 -0600112
Alison Wang849fc422012-03-26 21:49:03 +0000113 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewc8758102008-01-14 17:46:19 -0600114 gd->cpu_clk = vco / temp; /* cpu clock */
115
Alison Wang849fc422012-03-26 21:49:03 +0000116 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
Simon Glass7e2592f2012-12-13 20:49:07 +0000117 gd->arch.flb_clk = vco / temp; /* flexbus clock */
118 gd->bus_clk = gd->arch.flb_clk;
TsiChungLiewc8758102008-01-14 17:46:19 -0600119 }
120
Heiko Schocher00f792e2012-10-24 13:48:22 +0200121#ifdef CONFIG_SYS_I2C_FSL
Simon Glass609e6ec2012-12-13 20:48:49 +0000122 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Lieweec567a2008-08-19 03:01:19 +0600123#endif
124
TsiChungLiewc8758102008-01-14 17:46:19 -0600125 return (0);
126}