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