blob: f4e53bc82bbefd424d16c9f4db6a45f36fabae07 [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>
9#include <asm/processor.h>
10
11#include <asm/immap.h>
Alison Wang8bce3ec2012-03-26 21:49:03 +000012#include <asm/io.h>
TsiChungLiewae831cd2008-01-14 17:46:19 -060013
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 Wang8bce3ec2012-03-26 21:49:03 +000031 ccm_t *ccm = (ccm_t *)MMAP_CCM;
TsiChungLiewae831cd2008-01-14 17:46:19 -060032 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
43 /* Apply the divider to the system clock */
Alison Wang8bce3ec2012-03-26 21:49:03 +000044 clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i));
TsiChungLiewae831cd2008-01-14 17:46:19 -060045
46 /* Enable Limp Mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000047 setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewae831cd2008-01-14 17:46:19 -060048}
49
50/*
51 * brief Exit Limp mode
52 * warning The PLL should be set and locked prior to exiting Limp mode
53 */
54void clock_exit_limp(void)
55{
Alison Wang8bce3ec2012-03-26 21:49:03 +000056 ccm_t *ccm = (ccm_t *)MMAP_CCM;
57 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewae831cd2008-01-14 17:46:19 -060058
59 /* Exit Limp mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000060 clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP);
TsiChungLiewae831cd2008-01-14 17:46:19 -060061
62 /* Wait for the PLL to lock */
Alison Wang8bce3ec2012-03-26 21:49:03 +000063 while (!(in_be32(&pll->psr) & PLL_PSR_LOCK))
64 ;
TsiChungLiewae831cd2008-01-14 17:46:19 -060065}
66
67/*
68 * get_clocks() fills in gd->cpu_clock and gd->bus_clk
69 */
70int get_clocks(void)
71{
72
Alison Wang8bce3ec2012-03-26 21:49:03 +000073 ccm_t *ccm = (ccm_t *)MMAP_CCM;
74 pll_t *pll = (pll_t *)MMAP_PLL;
TsiChungLiewae831cd2008-01-14 17:46:19 -060075 int vco, temp, pcrvalue, pfdr;
76 u8 bootmode;
77
Alison Wang8bce3ec2012-03-26 21:49:03 +000078 pcrvalue = in_be32(&pll->pcr) & 0xFF0F0FFF;
TsiChungLiewae831cd2008-01-14 17:46:19 -060079 pfdr = pcrvalue >> 24;
80
TsiChung Liew39966e32008-10-21 15:37:02 +000081 if (pfdr == 0x1E)
82 bootmode = 0; /* Normal Mode */
83
84#ifdef CONFIG_CF_SBF
85 bootmode = 3; /* Serial Mode */
86#endif
87
88 if (bootmode == 0) {
89 /* Normal mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +000090 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
TsiChung Liew39966e32008-10-21 15:37:02 +000091 if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) {
92 /* Default value */
Alison Wang8bce3ec2012-03-26 21:49:03 +000093 pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF);
TsiChung Liew39966e32008-10-21 15:37:02 +000094 pcrvalue |= 0x1E << 24;
Alison Wang8bce3ec2012-03-26 21:49:03 +000095 out_be32(&pll->pcr, pcrvalue);
TsiChung Liew39966e32008-10-21 15:37:02 +000096 vco =
Alison Wang8bce3ec2012-03-26 21:49:03 +000097 ((in_be32(&pll->pcr) & 0xFF000000) >> 24) *
TsiChung Liew39966e32008-10-21 15:37:02 +000098 CONFIG_SYS_INPUT_CLKSRC;
99 }
Simon Glass568a7b62012-12-13 20:49:07 +0000100 gd->arch.vco_clk = vco; /* Vco clock */
TsiChung Liew39966e32008-10-21 15:37:02 +0000101 } else if (bootmode == 3) {
TsiChungLiewae831cd2008-01-14 17:46:19 -0600102 /* serial mode */
Alison Wang8bce3ec2012-03-26 21:49:03 +0000103 vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC;
Simon Glass568a7b62012-12-13 20:49:07 +0000104 gd->arch.vco_clk = vco; /* Vco clock */
TsiChungLiewae831cd2008-01-14 17:46:19 -0600105 }
106
Alison Wang8bce3ec2012-03-26 21:49:03 +0000107 if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) {
TsiChungLiewae831cd2008-01-14 17:46:19 -0600108 /* Limp mode */
109 } else {
Simon Glass568a7b62012-12-13 20:49:07 +0000110 gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */
TsiChungLiewae831cd2008-01-14 17:46:19 -0600111
Alison Wang8bce3ec2012-03-26 21:49:03 +0000112 temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1;
TsiChungLiewae831cd2008-01-14 17:46:19 -0600113 gd->cpu_clk = vco / temp; /* cpu clock */
114
Alison Wang8bce3ec2012-03-26 21:49:03 +0000115 temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1;
Simon Glass568a7b62012-12-13 20:49:07 +0000116 gd->arch.flb_clk = vco / temp; /* flexbus clock */
117 gd->bus_clk = gd->arch.flb_clk;
TsiChungLiewae831cd2008-01-14 17:46:19 -0600118 }
119
Heiko Schocherf2850742012-10-24 13:48:22 +0200120#ifdef CONFIG_SYS_I2C_FSL
Simon Glassc2baaec2012-12-13 20:48:49 +0000121 gd->arch.i2c1_clk = gd->bus_clk;
TsiChung Liew0c1e3252008-08-19 03:01:19 +0600122#endif
123
TsiChungLiewae831cd2008-01-14 17:46:19 -0600124 return (0);
125}