TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * (C) Copyright 2000-2003 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 6 | * Copyright (C) 2004-2008, 2012 Freescale Semiconductor, Inc. |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 7 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <asm/processor.h> |
| 30 | |
TsiChungLiew | b9bf3de | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 31 | #include <asm/immap.h> |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 32 | #include <asm/io.h> |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 33 | |
Wolfgang Denk | 1218abf | 2007-09-15 20:48:41 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 36 | /* PLL min/max specifications */ |
TsiChungLiew | b9bf3de | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 37 | #define MAX_FVCO 500000 /* KHz */ |
| 38 | #define MAX_FSYS 80000 /* KHz */ |
| 39 | #define MIN_FSYS 58333 /* KHz */ |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 40 | |
| 41 | #ifdef CONFIG_MCF5301x |
| 42 | #define FREF 20000 /* KHz */ |
| 43 | #define MAX_MFD 63 /* Multiplier */ |
| 44 | #define MIN_MFD 0 /* Multiplier */ |
| 45 | #define USBDIV 8 |
| 46 | |
| 47 | /* Low Power Divider specifications */ |
| 48 | #define MIN_LPD (0) /* Divider (not encoded) */ |
| 49 | #define MAX_LPD (15) /* Divider (not encoded) */ |
| 50 | #define DEFAULT_LPD (0) /* Divider (not encoded) */ |
| 51 | #endif |
| 52 | |
| 53 | #ifdef CONFIG_MCF532x |
TsiChungLiew | b9bf3de | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 54 | #define FREF 16000 /* KHz */ |
| 55 | #define MAX_MFD 135 /* Multiplier */ |
| 56 | #define MIN_MFD 88 /* Multiplier */ |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 57 | |
| 58 | /* Low Power Divider specifications */ |
TsiChungLiew | b9bf3de | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 59 | #define MIN_LPD (1 << 0) /* Divider (not encoded) */ |
| 60 | #define MAX_LPD (1 << 15) /* Divider (not encoded) */ |
| 61 | #define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */ |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 62 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 63 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 64 | #define BUSDIV 6 /* Divider */ |
| 65 | |
| 66 | /* Get the value of the current system clock */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 67 | int get_sys_clock(void) |
| 68 | { |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 69 | ccm_t *ccm = (ccm_t *)(MMAP_CCM); |
| 70 | pll_t *pll = (pll_t *)(MMAP_PLL); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 71 | int divider; |
| 72 | |
| 73 | /* Test to see if device is in LIMP mode */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 74 | if (in_be16(&ccm->misccr) & CCM_MISCCR_LIMP) { |
| 75 | divider = in_be16(&ccm->cdr) & CCM_CDR_LPDIV(0xF); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_MCF5301x |
| 77 | return (FREF / (3 * (1 << divider))); |
| 78 | #endif |
| 79 | #ifdef CONFIG_MCF532x |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 80 | return (FREF / (2 << divider)); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 81 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 82 | } else { |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_MCF5301x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 84 | u32 pfdr = (in_be32(&pll->pcr) & 0x3F) + 1; |
| 85 | u32 refdiv = (1 << ((in_be32(&pll->pcr) & PLL_PCR_REFDIV(7)) >> 8)); |
| 86 | u32 busdiv = ((in_be32(&pll->pdr) & 0x00F0) >> 4) + 1; |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 87 | |
| 88 | return (((FREF * pfdr) / refdiv) / busdiv); |
| 89 | #endif |
| 90 | #ifdef CONFIG_MCF532x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 91 | return (FREF * in_8(&pll->pfdr)) / (BUSDIV * 4); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 92 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Initialize the Low Power Divider circuit |
| 98 | * |
| 99 | * Parameters: |
| 100 | * div Desired system frequency divider |
| 101 | * |
| 102 | * Return Value: |
| 103 | * The resulting output system frequency |
| 104 | */ |
| 105 | int clock_limp(int div) |
| 106 | { |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 107 | ccm_t *ccm = (ccm_t *)(MMAP_CCM); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 108 | u32 temp; |
| 109 | |
| 110 | /* Check bounds of divider */ |
| 111 | if (div < MIN_LPD) |
| 112 | div = MIN_LPD; |
| 113 | if (div > MAX_LPD) |
| 114 | div = MAX_LPD; |
| 115 | |
| 116 | /* Save of the current value of the SSIDIV so we don't overwrite the value */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 117 | temp = (in_be16(&ccm->cdr) & CCM_CDR_SSIDIV(0xFF)); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 118 | |
| 119 | /* Apply the divider to the system clock */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 120 | out_be16(&ccm->cdr, CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp)); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 121 | |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 122 | setbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 123 | |
| 124 | return (FREF / (3 * (1 << div))); |
| 125 | } |
| 126 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 127 | /* Exit low power LIMP mode */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 128 | int clock_exit_limp(void) |
| 129 | { |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 130 | ccm_t *ccm = (ccm_t *)(MMAP_CCM); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 131 | int fout; |
| 132 | |
| 133 | /* Exit LIMP mode */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 134 | clrbits_be16(&ccm->misccr, CCM_MISCCR_LIMP); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 135 | |
| 136 | /* Wait for PLL to lock */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 137 | while (!(in_be16(&ccm->misccr) & CCM_MISCCR_PLL_LOCK)) |
| 138 | ; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 139 | |
| 140 | fout = get_sys_clock(); |
| 141 | |
| 142 | return fout; |
| 143 | } |
| 144 | |
| 145 | /* Initialize the PLL |
| 146 | * |
| 147 | * Parameters: |
| 148 | * fref PLL reference clock frequency in KHz |
| 149 | * fsys Desired PLL output frequency in KHz |
| 150 | * flags Operating parameters |
| 151 | * |
| 152 | * Return Value: |
| 153 | * The resulting output system frequency |
| 154 | */ |
| 155 | int clock_pll(int fsys, int flags) |
| 156 | { |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 157 | #ifdef CONFIG_MCF532x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 158 | u32 *sdram_workaround = (u32 *)(MMAP_SDRAM + 0x80); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 159 | #endif |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 160 | sdram_t *sdram = (sdram_t *)(MMAP_SDRAM); |
| 161 | pll_t *pll = (pll_t *)(MMAP_PLL); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 162 | int fref, temp, fout, mfd; |
| 163 | u32 i; |
| 164 | |
| 165 | fref = FREF; |
| 166 | |
| 167 | if (fsys == 0) { |
| 168 | /* Return current PLL output */ |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 169 | #ifdef CONFIG_MCF5301x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 170 | u32 busdiv = ((in_be32(&pll->pdr) >> 4) & 0x0F) + 1; |
| 171 | mfd = (in_be32(&pll->pcr) & 0x3F) + 1; |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 172 | |
| 173 | return (fref * mfd) / busdiv; |
| 174 | #endif |
| 175 | #ifdef CONFIG_MCF532x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 176 | mfd = in_8(&pll->pfdr); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 177 | |
| 178 | return (fref * mfd / (BUSDIV * 4)); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 179 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Check bounds of requested system clock */ |
| 183 | if (fsys > MAX_FSYS) |
| 184 | fsys = MAX_FSYS; |
| 185 | |
| 186 | if (fsys < MIN_FSYS) |
| 187 | fsys = MIN_FSYS; |
| 188 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Multiplying by 100 when calculating the temp value, |
| 191 | * and then dividing by 100 to calculate the mfd allows |
| 192 | * for exact values without needing to include floating |
| 193 | * point libraries. |
| 194 | */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 195 | temp = (100 * fsys) / fref; |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 196 | #ifdef CONFIG_MCF5301x |
| 197 | mfd = (BUSDIV * temp) / 100; |
| 198 | |
| 199 | /* Determine the output frequency for selected values */ |
| 200 | fout = ((fref * mfd) / BUSDIV); |
| 201 | #endif |
| 202 | #ifdef CONFIG_MCF532x |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 203 | mfd = (4 * BUSDIV * temp) / 100; |
| 204 | |
| 205 | /* Determine the output frequency for selected values */ |
| 206 | fout = ((fref * mfd) / (BUSDIV * 4)); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 207 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 208 | |
Wolfgang Wegner | c7de810 | 2010-03-02 10:59:20 +0100 | [diff] [blame] | 209 | /* must not tamper with SDRAMC if running from SDRAM */ |
| 210 | #if !defined(CONFIG_MONITOR_IS_IN_RAM) |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 211 | /* |
| 212 | * Check to see if the SDRAM has already been initialized. |
| 213 | * If it has then the SDRAM needs to be put into self refresh |
| 214 | * mode before reprogramming the PLL. |
| 215 | */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 216 | if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF) |
| 217 | clrbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Initialize the PLL to generate the new system clock frequency. |
| 221 | * The device must be put into LIMP mode to reprogram the PLL. |
| 222 | */ |
| 223 | |
| 224 | /* Enter LIMP mode */ |
| 225 | clock_limp(DEFAULT_LPD); |
| 226 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 227 | #ifdef CONFIG_MCF5301x |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 228 | out_be32(&pll->pdr, |
| 229 | PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) | |
| 230 | PLL_PDR_OUTDIV2(BUSDIV - 1) | |
| 231 | PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) | |
| 232 | PLL_PDR_OUTDIV4(USBDIV - 1)); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 233 | |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 234 | clrbits_be32(&pll->pcr, ~PLL_PCR_FBDIV_UNMASK); |
| 235 | setbits_be32(&pll->pcr, PLL_PCR_FBDIV(mfd - 1)); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 236 | #endif |
| 237 | #ifdef CONFIG_MCF532x |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 238 | /* Reprogram PLL for desired fsys */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 239 | out_8(&pll->podr, |
| 240 | PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV)); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 241 | |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 242 | out_8(&pll->pfdr, mfd); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 243 | #endif |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 244 | |
| 245 | /* Exit LIMP mode */ |
| 246 | clock_exit_limp(); |
| 247 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 248 | /* Return the SDRAM to normal operation if it is in use. */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 249 | if (in_be32(&sdram->ctrl) & SDRAMC_SDCR_REF) |
| 250 | setbits_be32(&sdram->ctrl, SDRAMC_SDCR_CKE); |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 251 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 252 | #ifdef CONFIG_MCF532x |
| 253 | /* |
| 254 | * software workaround for SDRAM opeartion after exiting LIMP |
| 255 | * mode errata |
| 256 | */ |
Alison Wang | aa0d99f | 2012-03-26 21:49:05 +0000 | [diff] [blame] | 257 | out_be32(sdram_workaround, CONFIG_SYS_SDRAM_BASE); |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 258 | #endif |
TsiChungLiew | b9bf3de | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 259 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 260 | /* wait for DQS logic to relock */ |
| 261 | for (i = 0; i < 0x200; i++) ; |
Wolfgang Wegner | c7de810 | 2010-03-02 10:59:20 +0100 | [diff] [blame] | 262 | #endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 263 | |
| 264 | return fout; |
| 265 | } |
| 266 | |
TsiChung Liew | 536e7da | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 267 | /* get_clocks() fills in gd->cpu_clock and gd->bus_clk */ |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 268 | int get_clocks(void) |
| 269 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 270 | gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000; |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 271 | gd->cpu_clk = (gd->bus_clk * 3); |
TsiChung Liew | eec567a | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 272 | |
| 273 | #ifdef CONFIG_FSL_I2C |
Simon Glass | 609e6ec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 274 | gd->arch.i2c1_clk = gd->bus_clk; |
TsiChung Liew | eec567a | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 275 | #endif |
| 276 | |
TsiChung Liew | 8e585f0 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 277 | return (0); |
| 278 | } |