TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 1 | /* |
| 2 | * |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc. |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 4 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <asm/processor.h> |
| 11 | |
| 12 | #include <asm/immap.h> |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 14 | |
| 15 | DECLARE_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 | |
| 30 | void clock_enter_limp(int lpdiv) |
| 31 | { |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 32 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 33 | 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 Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 45 | clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i)); |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 46 | |
| 47 | /* Enable Limp Mode */ |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 48 | setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
| 52 | * brief Exit Limp mode |
| 53 | * warning The PLL should be set and locked prior to exiting Limp mode |
| 54 | */ |
| 55 | void clock_exit_limp(void) |
| 56 | { |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 57 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
| 58 | pll_t *pll = (pll_t *)MMAP_PLL; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 59 | |
| 60 | /* Exit Limp mode */ |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 61 | clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 62 | |
| 63 | /* Wait for the PLL to lock */ |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 64 | while (!(in_be32(&pll->psr) & PLL_PSR_LOCK)) |
| 65 | ; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * get_clocks() fills in gd->cpu_clock and gd->bus_clk |
| 70 | */ |
| 71 | int get_clocks(void) |
| 72 | { |
| 73 | |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 74 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
| 75 | pll_t *pll = (pll_t *)MMAP_PLL; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 76 | int vco, temp, pcrvalue, pfdr; |
| 77 | u8 bootmode; |
| 78 | |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 79 | pcrvalue = in_be32(&pll->pcr) & 0xFF0F0FFF; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 80 | pfdr = pcrvalue >> 24; |
| 81 | |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 82 | 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 Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 91 | vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC; |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 92 | if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) { |
| 93 | /* Default value */ |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 94 | pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF); |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 95 | pcrvalue |= 0x1E << 24; |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 96 | out_be32(&pll->pcr, pcrvalue); |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 97 | vco = |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 98 | ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 99 | CONFIG_SYS_INPUT_CLKSRC; |
| 100 | } |
Simon Glass | 568a7b6 | 2012-12-13 20:49:07 +0000 | [diff] [blame] | 101 | gd->arch.vco_clk = vco; /* Vco clock */ |
TsiChung Liew | 39966e3 | 2008-10-21 15:37:02 +0000 | [diff] [blame] | 102 | } else if (bootmode == 3) { |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 103 | /* serial mode */ |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 104 | vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC; |
Simon Glass | 568a7b6 | 2012-12-13 20:49:07 +0000 | [diff] [blame] | 105 | gd->arch.vco_clk = vco; /* Vco clock */ |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 108 | if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) { |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 109 | /* Limp mode */ |
| 110 | } else { |
Simon Glass | 568a7b6 | 2012-12-13 20:49:07 +0000 | [diff] [blame] | 111 | gd->arch.inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */ |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 112 | |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 113 | temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 114 | gd->cpu_clk = vco / temp; /* cpu clock */ |
| 115 | |
Alison Wang | 8bce3ec | 2012-03-26 21:49:03 +0000 | [diff] [blame] | 116 | temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1; |
Simon Glass | 568a7b6 | 2012-12-13 20:49:07 +0000 | [diff] [blame] | 117 | gd->arch.flb_clk = vco / temp; /* flexbus clock */ |
| 118 | gd->bus_clk = gd->arch.flb_clk; |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 119 | } |
| 120 | |
TsiChung Liew | 0c1e325 | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 121 | #ifdef CONFIG_FSL_I2C |
Simon Glass | c2baaec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 122 | gd->arch.i2c1_clk = gd->bus_clk; |
TsiChung Liew | 0c1e325 | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 123 | #endif |
| 124 | |
TsiChungLiew | ae831cd | 2008-01-14 17:46:19 -0600 | [diff] [blame] | 125 | return (0); |
| 126 | } |