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