TsiChung Liew | f6afe72 | 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 | * |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2004-2008 Freescale Semiconductor, Inc. |
TsiChung Liew | f6afe72 | 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 | 2ce14b7 | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 31 | #include <asm/immap.h> |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 32 | |
Wolfgang Denk | d112a2c | 2007-09-15 20:48:41 +0200 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 35 | /* PLL min/max specifications */ |
TsiChungLiew | 2ce14b7 | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 36 | #define MAX_FVCO 500000 /* KHz */ |
| 37 | #define MAX_FSYS 80000 /* KHz */ |
| 38 | #define MIN_FSYS 58333 /* KHz */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 39 | |
| 40 | #ifdef CONFIG_MCF5301x |
| 41 | #define FREF 20000 /* KHz */ |
| 42 | #define MAX_MFD 63 /* Multiplier */ |
| 43 | #define MIN_MFD 0 /* Multiplier */ |
| 44 | #define USBDIV 8 |
| 45 | |
| 46 | /* Low Power Divider specifications */ |
| 47 | #define MIN_LPD (0) /* Divider (not encoded) */ |
| 48 | #define MAX_LPD (15) /* Divider (not encoded) */ |
| 49 | #define DEFAULT_LPD (0) /* Divider (not encoded) */ |
| 50 | #endif |
| 51 | |
| 52 | #ifdef CONFIG_MCF532x |
TsiChungLiew | 2ce14b7 | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 53 | #define FREF 16000 /* KHz */ |
| 54 | #define MAX_MFD 135 /* Multiplier */ |
| 55 | #define MIN_MFD 88 /* Multiplier */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 56 | |
| 57 | /* Low Power Divider specifications */ |
TsiChungLiew | 2ce14b7 | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 58 | #define MIN_LPD (1 << 0) /* Divider (not encoded) */ |
| 59 | #define MAX_LPD (1 << 15) /* Divider (not encoded) */ |
| 60 | #define DEFAULT_LPD (1 << 1) /* Divider (not encoded) */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 61 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 62 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 63 | #define BUSDIV 6 /* Divider */ |
| 64 | |
| 65 | /* Get the value of the current system clock */ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 66 | int get_sys_clock(void) |
| 67 | { |
| 68 | volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM); |
| 69 | volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL); |
| 70 | int divider; |
| 71 | |
| 72 | /* Test to see if device is in LIMP mode */ |
| 73 | if (ccm->misccr & CCM_MISCCR_LIMP) { |
| 74 | divider = ccm->cdr & CCM_CDR_LPDIV(0xF); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_MCF5301x |
| 76 | return (FREF / (3 * (1 << divider))); |
| 77 | #endif |
| 78 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 79 | return (FREF / (2 << divider)); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 80 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 81 | } else { |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_MCF5301x |
| 83 | u32 pfdr = (pll->pcr & 0x3F) + 1; |
| 84 | u32 refdiv = (1 << ((pll->pcr & PLL_PCR_REFDIV(7)) >> 8)); |
| 85 | u32 busdiv = ((pll->pdr & 0x00F0) >> 4) + 1; |
| 86 | |
| 87 | return (((FREF * pfdr) / refdiv) / busdiv); |
| 88 | #endif |
| 89 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 90 | return ((FREF * pll->pfdr) / (BUSDIV * 4)); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 91 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Initialize the Low Power Divider circuit |
| 97 | * |
| 98 | * Parameters: |
| 99 | * div Desired system frequency divider |
| 100 | * |
| 101 | * Return Value: |
| 102 | * The resulting output system frequency |
| 103 | */ |
| 104 | int clock_limp(int div) |
| 105 | { |
| 106 | volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM); |
| 107 | u32 temp; |
| 108 | |
| 109 | /* Check bounds of divider */ |
| 110 | if (div < MIN_LPD) |
| 111 | div = MIN_LPD; |
| 112 | if (div > MAX_LPD) |
| 113 | div = MAX_LPD; |
| 114 | |
| 115 | /* Save of the current value of the SSIDIV so we don't overwrite the value */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 116 | temp = (ccm->cdr & CCM_CDR_SSIDIV(0xFF)); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 117 | |
| 118 | /* Apply the divider to the system clock */ |
| 119 | ccm->cdr = (CCM_CDR_LPDIV(div) | CCM_CDR_SSIDIV(temp)); |
| 120 | |
| 121 | ccm->misccr |= CCM_MISCCR_LIMP; |
| 122 | |
| 123 | return (FREF / (3 * (1 << div))); |
| 124 | } |
| 125 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 126 | /* Exit low power LIMP mode */ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 127 | int clock_exit_limp(void) |
| 128 | { |
| 129 | volatile ccm_t *ccm = (volatile ccm_t *)(MMAP_CCM); |
| 130 | int fout; |
| 131 | |
| 132 | /* Exit LIMP mode */ |
| 133 | ccm->misccr &= (~CCM_MISCCR_LIMP); |
| 134 | |
| 135 | /* Wait for PLL to lock */ |
| 136 | while (!(ccm->misccr & CCM_MISCCR_PLL_LOCK)) ; |
| 137 | |
| 138 | fout = get_sys_clock(); |
| 139 | |
| 140 | return fout; |
| 141 | } |
| 142 | |
| 143 | /* Initialize the PLL |
| 144 | * |
| 145 | * Parameters: |
| 146 | * fref PLL reference clock frequency in KHz |
| 147 | * fsys Desired PLL output frequency in KHz |
| 148 | * flags Operating parameters |
| 149 | * |
| 150 | * Return Value: |
| 151 | * The resulting output system frequency |
| 152 | */ |
| 153 | int clock_pll(int fsys, int flags) |
| 154 | { |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 156 | volatile u32 *sdram_workaround = (volatile u32 *)(MMAP_SDRAM + 0x80); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 157 | #endif |
| 158 | volatile sdram_t *sdram = (volatile sdram_t *)(MMAP_SDRAM); |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 159 | volatile pll_t *pll = (volatile pll_t *)(MMAP_PLL); |
| 160 | int fref, temp, fout, mfd; |
| 161 | u32 i; |
| 162 | |
| 163 | fref = FREF; |
| 164 | |
| 165 | if (fsys == 0) { |
| 166 | /* Return current PLL output */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 167 | #ifdef CONFIG_MCF5301x |
| 168 | u32 busdiv = ((pll->pdr >> 4) & 0x0F) + 1; |
| 169 | mfd = (pll->pcr & 0x3F) + 1; |
| 170 | |
| 171 | return (fref * mfd) / busdiv; |
| 172 | #endif |
| 173 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 174 | mfd = pll->pfdr; |
| 175 | |
| 176 | return (fref * mfd / (BUSDIV * 4)); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 177 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Check bounds of requested system clock */ |
| 181 | if (fsys > MAX_FSYS) |
| 182 | fsys = MAX_FSYS; |
| 183 | |
| 184 | if (fsys < MIN_FSYS) |
| 185 | fsys = MIN_FSYS; |
| 186 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Multiplying by 100 when calculating the temp value, |
| 189 | * and then dividing by 100 to calculate the mfd allows |
| 190 | * for exact values without needing to include floating |
| 191 | * point libraries. |
| 192 | */ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 193 | temp = (100 * fsys) / fref; |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 194 | #ifdef CONFIG_MCF5301x |
| 195 | mfd = (BUSDIV * temp) / 100; |
| 196 | |
| 197 | /* Determine the output frequency for selected values */ |
| 198 | fout = ((fref * mfd) / BUSDIV); |
| 199 | #endif |
| 200 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 201 | mfd = (4 * BUSDIV * temp) / 100; |
| 202 | |
| 203 | /* Determine the output frequency for selected values */ |
| 204 | fout = ((fref * mfd) / (BUSDIV * 4)); |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 205 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 206 | |
Wolfgang Wegner | ea32ab2 | 2010-03-02 10:59:20 +0100 | [diff] [blame] | 207 | /* must not tamper with SDRAMC if running from SDRAM */ |
| 208 | #if !defined(CONFIG_MONITOR_IS_IN_RAM) |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 209 | /* |
| 210 | * Check to see if the SDRAM has already been initialized. |
| 211 | * If it has then the SDRAM needs to be put into self refresh |
| 212 | * mode before reprogramming the PLL. |
| 213 | */ |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 214 | if (sdram->ctrl & SDRAMC_SDCR_REF) |
| 215 | sdram->ctrl &= ~SDRAMC_SDCR_CKE; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Initialize the PLL to generate the new system clock frequency. |
| 219 | * The device must be put into LIMP mode to reprogram the PLL. |
| 220 | */ |
| 221 | |
| 222 | /* Enter LIMP mode */ |
| 223 | clock_limp(DEFAULT_LPD); |
| 224 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 225 | #ifdef CONFIG_MCF5301x |
| 226 | pll->pdr = |
| 227 | PLL_PDR_OUTDIV1((BUSDIV / 3) - 1) | |
| 228 | PLL_PDR_OUTDIV2(BUSDIV - 1) | |
| 229 | PLL_PDR_OUTDIV3((BUSDIV / 2) - 1) | |
| 230 | PLL_PDR_OUTDIV4(USBDIV - 1); |
| 231 | |
TsiChung Liew | bbb8ddc | 2010-03-09 18:32:16 -0600 | [diff] [blame] | 232 | pll->pcr &= PLL_PCR_FBDIV_UNMASK; |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 233 | pll->pcr |= PLL_PCR_FBDIV(mfd - 1); |
| 234 | #endif |
| 235 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 236 | /* Reprogram PLL for desired fsys */ |
| 237 | pll->podr = (PLL_PODR_CPUDIV(BUSDIV / 3) | PLL_PODR_BUSDIV(BUSDIV)); |
| 238 | |
| 239 | pll->pfdr = mfd; |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 240 | #endif |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 241 | |
| 242 | /* Exit LIMP mode */ |
| 243 | clock_exit_limp(); |
| 244 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 245 | /* Return the SDRAM to normal operation if it is in use. */ |
| 246 | if (sdram->ctrl & SDRAMC_SDCR_REF) |
| 247 | sdram->ctrl |= SDRAMC_SDCR_CKE; |
| 248 | |
| 249 | #ifdef CONFIG_MCF532x |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 250 | /* |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 251 | * software workaround for SDRAM opeartion after exiting LIMP |
| 252 | * mode errata |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 253 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 254 | *sdram_workaround = CONFIG_SYS_SDRAM_BASE; |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 255 | #endif |
TsiChungLiew | 2ce14b7 | 2007-07-05 23:05:31 -0500 | [diff] [blame] | 256 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 257 | /* wait for DQS logic to relock */ |
| 258 | for (i = 0; i < 0x200; i++) ; |
Wolfgang Wegner | ea32ab2 | 2010-03-02 10:59:20 +0100 | [diff] [blame] | 259 | #endif /* !defined(CONFIG_MONITOR_IS_IN_RAM) */ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 260 | |
| 261 | return fout; |
| 262 | } |
| 263 | |
TsiChung Liew | e7e4fc8 | 2008-10-22 11:38:21 +0000 | [diff] [blame] | 264 | /* get_clocks() fills in gd->cpu_clock and gd->bus_clk */ |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 265 | int get_clocks(void) |
| 266 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 267 | gd->bus_clk = clock_pll(CONFIG_SYS_CLK / 1000, 0) * 1000; |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 268 | gd->cpu_clk = (gd->bus_clk * 3); |
TsiChung Liew | 0c1e325 | 2008-08-19 03:01:19 +0600 | [diff] [blame] | 269 | |
| 270 | #ifdef CONFIG_FSL_I2C |
| 271 | gd->i2c1_clk = gd->bus_clk; |
| 272 | #endif |
| 273 | |
TsiChung Liew | f6afe72 | 2007-06-18 13:50:13 -0500 | [diff] [blame] | 274 | return (0); |
| 275 | } |