TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2007, 2012 Freescale Semiconductor, Inc. |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 4 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <asm/processor.h> |
| 27 | |
| 28 | #include <asm/immap.h> |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 29 | #include <asm/io.h> |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
| 33 | /* |
| 34 | * Low Power Divider specifications |
| 35 | */ |
| 36 | #define CLOCK_LPD_MIN (1 << 0) /* Divider (decoded) */ |
| 37 | #define CLOCK_LPD_MAX (1 << 15) /* Divider (decoded) */ |
| 38 | |
| 39 | #define CLOCK_PLL_FVCO_MAX 540000000 |
| 40 | #define CLOCK_PLL_FVCO_MIN 300000000 |
| 41 | |
| 42 | #define CLOCK_PLL_FSYS_MAX 266666666 |
| 43 | #define CLOCK_PLL_FSYS_MIN 100000000 |
| 44 | #define MHZ 1000000 |
| 45 | |
| 46 | void clock_enter_limp(int lpdiv) |
| 47 | { |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 48 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 49 | int i, j; |
| 50 | |
| 51 | /* Check bounds of divider */ |
| 52 | if (lpdiv < CLOCK_LPD_MIN) |
| 53 | lpdiv = CLOCK_LPD_MIN; |
| 54 | if (lpdiv > CLOCK_LPD_MAX) |
| 55 | lpdiv = CLOCK_LPD_MAX; |
| 56 | |
| 57 | /* Round divider down to nearest power of two */ |
| 58 | for (i = 0, j = lpdiv; j != 1; j >>= 1, i++) ; |
| 59 | |
| 60 | /* Apply the divider to the system clock */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 61 | clrsetbits_be16(&ccm->cdr, 0x0f00, CCM_CDR_LPDIV(i)); |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 62 | |
| 63 | /* Enable Limp Mode */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 64 | setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * brief Exit Limp mode |
| 69 | * warning The PLL should be set and locked prior to exiting Limp mode |
| 70 | */ |
| 71 | void clock_exit_limp(void) |
| 72 | { |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 73 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
| 74 | pll_t *pll = (pll_t *)MMAP_PLL; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 75 | |
| 76 | /* Exit Limp mode */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 77 | clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 78 | |
| 79 | /* Wait for the PLL to lock */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 80 | while (!(in_be32(&pll->psr) & PLL_PSR_LOCK)) |
| 81 | ; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * get_clocks() fills in gd->cpu_clock and gd->bus_clk |
| 86 | */ |
| 87 | int get_clocks(void) |
| 88 | { |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 89 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 90 | ccm_t *ccm = (ccm_t *)MMAP_CCM; |
| 91 | pll_t *pll = (pll_t *)MMAP_PLL; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 92 | int pllmult_nopci[] = { 20, 10, 24, 18, 12, 6, 16, 8 }; |
| 93 | int pllmult_pci[] = { 12, 6, 16, 8 }; |
Marek Vasut | e946a88 | 2012-10-03 13:28:45 +0000 | [diff] [blame] | 94 | int vco = 0, temp, fbtemp, pcrvalue; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 95 | int *pPllmult = NULL; |
| 96 | u16 fbpll_mask; |
Marek Vasut | e946a88 | 2012-10-03 13:28:45 +0000 | [diff] [blame] | 97 | #ifdef CONFIG_PCI |
| 98 | int bPci; |
| 99 | #endif |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 100 | |
| 101 | #ifdef CONFIG_M54455EVB |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 102 | u8 *cpld = (u8 *)(CONFIG_SYS_CS2_BASE + 3); |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 103 | #endif |
| 104 | u8 bootmode; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 105 | |
| 106 | /* To determine PCI is present or not */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 107 | if (((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x00e0) || |
| 108 | ((in_be16(&ccm->ccr) & CCM_CCR_360_FBCONFIG_MASK) == 0x0060)) { |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 109 | pPllmult = &pllmult_pci[0]; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 110 | fbpll_mask = 3; /* 11b */ |
Marek Vasut | e946a88 | 2012-10-03 13:28:45 +0000 | [diff] [blame] | 111 | #ifdef CONFIG_PCI |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 112 | bPci = 1; |
Marek Vasut | e946a88 | 2012-10-03 13:28:45 +0000 | [diff] [blame] | 113 | #endif |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 114 | } else { |
| 115 | pPllmult = &pllmult_nopci[0]; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 116 | fbpll_mask = 7; /* 111b */ |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 117 | #ifdef CONFIG_PCI |
| 118 | gd->pci_clk = 0; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 119 | bPci = 0; |
Marek Vasut | e946a88 | 2012-10-03 13:28:45 +0000 | [diff] [blame] | 120 | #endif |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #ifdef CONFIG_M54455EVB |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 124 | bootmode = (in_8(cpld) & 0x03); |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 125 | |
| 126 | if (bootmode != 3) { |
| 127 | /* Temporary read from CCR- fixed fb issue, must be the same clock |
| 128 | as pci or input clock, causing cpld/fpga read inconsistancy */ |
| 129 | fbtemp = pPllmult[ccm->ccr & fbpll_mask]; |
| 130 | |
| 131 | /* Break down into small pieces, code still in flex bus */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 132 | pcrvalue = in_be32(&pll->pcr) & 0xFFFFF0FF; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 133 | temp = fbtemp - 1; |
| 134 | pcrvalue |= PLL_PCR_OUTDIV3(temp); |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 135 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 136 | out_be32(&pll->pcr, pcrvalue); |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 137 | } |
| 138 | #endif |
| 139 | #ifdef CONFIG_M54451EVB |
| 140 | /* No external logic to read the bootmode, hard coded from built */ |
| 141 | #ifdef CONFIG_CF_SBF |
| 142 | bootmode = 3; |
| 143 | #else |
| 144 | bootmode = 2; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 145 | |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 146 | /* default value is 16 mul, set to 20 mul */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 147 | pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF) | 0x14000000; |
| 148 | out_be32(&pll->pcr, pcrvalue); |
| 149 | while ((in_be32(&pll->psr) & PLL_PSR_LOCK) != PLL_PSR_LOCK) |
| 150 | ; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 151 | #endif |
| 152 | #endif |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 153 | |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 154 | if (bootmode == 0) { |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 155 | /* RCON mode */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 156 | vco = pPllmult[ccm->rcon & fbpll_mask] * CONFIG_SYS_INPUT_CLKSRC; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 157 | |
| 158 | if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) { |
| 159 | /* invaild range, re-set in PCR */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 160 | int temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 161 | int i, j, bus; |
| 162 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 163 | j = (in_be32(&pll->pcr) & 0xFF000000) >> 24; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 164 | for (i = j; i < 0xFF; i++) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 165 | vco = i * CONFIG_SYS_INPUT_CLKSRC; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 166 | if (vco >= CLOCK_PLL_FVCO_MIN) { |
| 167 | bus = vco / temp; |
| 168 | if (bus <= CLOCK_PLL_FSYS_MIN - MHZ) |
| 169 | continue; |
| 170 | else |
| 171 | break; |
| 172 | } |
| 173 | } |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 174 | pcrvalue = in_be32(&pll->pcr) & 0x00FF00FF; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 175 | fbtemp = ((i - 1) << 8) | ((i - 1) << 12); |
| 176 | pcrvalue |= ((i << 24) | fbtemp); |
| 177 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 178 | out_be32(&pll->pcr, pcrvalue); |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 179 | } |
| 180 | gd->vco_clk = vco; /* Vco clock */ |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 181 | } else if (bootmode == 2) { |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 182 | /* Normal mode */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 183 | vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 184 | if ((vco < CLOCK_PLL_FVCO_MIN) || (vco > CLOCK_PLL_FVCO_MAX)) { |
| 185 | /* Default value */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 186 | pcrvalue = (in_be32(&pll->pcr) & 0x00FFFFFF); |
| 187 | pcrvalue |= pPllmult[in_be16(&ccm->ccr) & fbpll_mask] << 24; |
| 188 | out_be32(&pll->pcr, pcrvalue); |
| 189 | vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 190 | } |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 191 | gd->vco_clk = vco; /* Vco clock */ |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 192 | } else if (bootmode == 3) { |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 193 | /* serial mode */ |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 194 | vco = ((in_be32(&pll->pcr) & 0xFF000000) >> 24) * CONFIG_SYS_INPUT_CLKSRC; |
TsiChung Liew | 23cf8fd | 2008-07-23 20:38:53 -0500 | [diff] [blame] | 195 | gd->vco_clk = vco; /* Vco clock */ |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 196 | } |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 197 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 198 | if ((in_be16(&ccm->ccr) & CCM_MISCCR_LIMP) == CCM_MISCCR_LIMP) { |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 199 | /* Limp mode */ |
| 200 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 201 | gd->inp_clk = CONFIG_SYS_INPUT_CLKSRC; /* Input clock */ |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 202 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 203 | temp = (in_be32(&pll->pcr) & PLL_PCR_OUTDIV1_MASK) + 1; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 204 | gd->cpu_clk = vco / temp; /* cpu clock */ |
| 205 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 206 | temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV2_MASK) >> 4) + 1; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 207 | gd->bus_clk = vco / temp; /* bus clock */ |
| 208 | |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 209 | temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV3_MASK) >> 8) + 1; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 210 | gd->flb_clk = vco / temp; /* FlexBus clock */ |
| 211 | |
| 212 | #ifdef CONFIG_PCI |
| 213 | if (bPci) { |
Alison Wang | 8d8dac9 | 2012-03-26 21:49:08 +0000 | [diff] [blame] | 214 | temp = ((in_be32(&pll->pcr) & PLL_PCR_OUTDIV4_MASK) >> 12) + 1; |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 215 | gd->pci_clk = vco / temp; /* PCI clock */ |
| 216 | } |
| 217 | #endif |
| 218 | } |
| 219 | |
TsiChung Liew | 0c1e325 | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 220 | #ifdef CONFIG_FSL_I2C |
| 221 | gd->i2c1_clk = gd->bus_clk; |
| 222 | #endif |
| 223 | |
TsiChungLiew | fc3ca3b6 | 2007-08-16 15:05:11 -0500 | [diff] [blame] | 224 | return (0); |
| 225 | } |