Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * clock.c |
| 3 | * |
| 4 | * clocks for AM33XX based boards |
| 5 | * |
| 6 | * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <asm/arch/cpu.h> |
| 21 | #include <asm/arch/clock.h> |
| 22 | #include <asm/arch/hardware.h> |
| 23 | #include <asm/io.h> |
| 24 | |
| 25 | #define PRCM_MOD_EN 0x2 |
| 26 | #define PRCM_FORCE_WAKEUP 0x2 |
Chandan Nath | 2015c38 | 2012-07-24 12:22:17 +0000 | [diff] [blame] | 27 | #define PRCM_FUNCTL 0x0 |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 28 | |
| 29 | #define PRCM_EMIF_CLK_ACTIVITY BIT(2) |
| 30 | #define PRCM_L3_GCLK_ACTIVITY BIT(4) |
| 31 | |
| 32 | #define PLL_BYPASS_MODE 0x4 |
| 33 | #define ST_MN_BYPASS 0x00000100 |
| 34 | #define ST_DPLL_CLK 0x00000001 |
| 35 | #define CLK_SEL_MASK 0x7ffff |
| 36 | #define CLK_DIV_MASK 0x1f |
| 37 | #define CLK_DIV2_MASK 0x7f |
| 38 | #define CLK_SEL_SHIFT 0x8 |
| 39 | #define CLK_MODE_SEL 0x7 |
| 40 | #define CLK_MODE_MASK 0xfffffff8 |
| 41 | #define CLK_DIV_SEL 0xFFFFFFE0 |
Chandan Nath | 2015c38 | 2012-07-24 12:22:17 +0000 | [diff] [blame] | 42 | #define CPGMAC0_IDLE 0x30000 |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 43 | |
| 44 | const struct cm_perpll *cmper = (struct cm_perpll *)CM_PER; |
| 45 | const struct cm_wkuppll *cmwkup = (struct cm_wkuppll *)CM_WKUP; |
| 46 | const struct cm_dpll *cmdpll = (struct cm_dpll *)CM_DPLL; |
| 47 | |
| 48 | static void enable_interface_clocks(void) |
| 49 | { |
| 50 | /* Enable all the Interconnect Modules */ |
| 51 | writel(PRCM_MOD_EN, &cmper->l3clkctrl); |
| 52 | while (readl(&cmper->l3clkctrl) != PRCM_MOD_EN) |
| 53 | ; |
| 54 | |
| 55 | writel(PRCM_MOD_EN, &cmper->l4lsclkctrl); |
| 56 | while (readl(&cmper->l4lsclkctrl) != PRCM_MOD_EN) |
| 57 | ; |
| 58 | |
| 59 | writel(PRCM_MOD_EN, &cmper->l4fwclkctrl); |
| 60 | while (readl(&cmper->l4fwclkctrl) != PRCM_MOD_EN) |
| 61 | ; |
| 62 | |
| 63 | writel(PRCM_MOD_EN, &cmwkup->wkl4wkclkctrl); |
| 64 | while (readl(&cmwkup->wkl4wkclkctrl) != PRCM_MOD_EN) |
| 65 | ; |
| 66 | |
| 67 | writel(PRCM_MOD_EN, &cmper->l3instrclkctrl); |
| 68 | while (readl(&cmper->l3instrclkctrl) != PRCM_MOD_EN) |
| 69 | ; |
| 70 | |
| 71 | writel(PRCM_MOD_EN, &cmper->l4hsclkctrl); |
| 72 | while (readl(&cmper->l4hsclkctrl) != PRCM_MOD_EN) |
| 73 | ; |
Tom Rini | f8bf6b2 | 2012-07-31 07:22:47 -0700 | [diff] [blame] | 74 | |
| 75 | writel(PRCM_MOD_EN, &cmwkup->wkgpio0clkctrl); |
| 76 | while (readl(&cmwkup->wkgpio0clkctrl) != PRCM_MOD_EN) |
| 77 | ; |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Force power domain wake up transition |
| 82 | * Ensure that the corresponding interface clock is active before |
| 83 | * using the peripheral |
| 84 | */ |
| 85 | static void power_domain_wkup_transition(void) |
| 86 | { |
| 87 | writel(PRCM_FORCE_WAKEUP, &cmper->l3clkstctrl); |
| 88 | writel(PRCM_FORCE_WAKEUP, &cmper->l4lsclkstctrl); |
| 89 | writel(PRCM_FORCE_WAKEUP, &cmwkup->wkclkstctrl); |
| 90 | writel(PRCM_FORCE_WAKEUP, &cmper->l4fwclkstctrl); |
| 91 | writel(PRCM_FORCE_WAKEUP, &cmper->l3sclkstctrl); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Enable the peripheral clock for required peripherals |
| 96 | */ |
| 97 | static void enable_per_clocks(void) |
| 98 | { |
| 99 | /* Enable the control module though RBL would have done it*/ |
| 100 | writel(PRCM_MOD_EN, &cmwkup->wkctrlclkctrl); |
| 101 | while (readl(&cmwkup->wkctrlclkctrl) != PRCM_MOD_EN) |
| 102 | ; |
| 103 | |
| 104 | /* Enable the module clock */ |
| 105 | writel(PRCM_MOD_EN, &cmper->timer2clkctrl); |
| 106 | while (readl(&cmper->timer2clkctrl) != PRCM_MOD_EN) |
| 107 | ; |
| 108 | |
Chandan Nath | 5b5c212 | 2012-01-09 20:38:56 +0000 | [diff] [blame] | 109 | /* Select the Master osc 24 MHZ as Timer2 clock source */ |
| 110 | writel(0x1, &cmdpll->clktimer2clk); |
| 111 | |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 112 | /* UART0 */ |
| 113 | writel(PRCM_MOD_EN, &cmwkup->wkup_uart0ctrl); |
| 114 | while (readl(&cmwkup->wkup_uart0ctrl) != PRCM_MOD_EN) |
| 115 | ; |
Chandan Nath | d6e97f8 | 2012-01-09 20:38:58 +0000 | [diff] [blame] | 116 | |
| 117 | /* MMC0*/ |
| 118 | writel(PRCM_MOD_EN, &cmper->mmc0clkctrl); |
| 119 | while (readl(&cmper->mmc0clkctrl) != PRCM_MOD_EN) |
| 120 | ; |
Patil, Rachna | 5f70c51 | 2012-01-22 23:47:01 +0000 | [diff] [blame] | 121 | |
| 122 | /* i2c0 */ |
| 123 | writel(PRCM_MOD_EN, &cmwkup->wkup_i2c0ctrl); |
| 124 | while (readl(&cmwkup->wkup_i2c0ctrl) != PRCM_MOD_EN) |
| 125 | ; |
Steve Sakoman | 6229e33 | 2012-06-04 05:35:34 +0000 | [diff] [blame] | 126 | |
| 127 | /* gpio1 module */ |
| 128 | writel(PRCM_MOD_EN, &cmper->gpio1clkctrl); |
| 129 | while (readl(&cmper->gpio1clkctrl) != PRCM_MOD_EN) |
| 130 | ; |
| 131 | |
| 132 | /* gpio2 module */ |
| 133 | writel(PRCM_MOD_EN, &cmper->gpio2clkctrl); |
| 134 | while (readl(&cmper->gpio2clkctrl) != PRCM_MOD_EN) |
| 135 | ; |
| 136 | |
| 137 | /* gpio3 module */ |
| 138 | writel(PRCM_MOD_EN, &cmper->gpio3clkctrl); |
| 139 | while (readl(&cmper->gpio3clkctrl) != PRCM_MOD_EN) |
| 140 | ; |
Steve Sakoman | 695b1fe | 2012-06-22 07:45:57 +0000 | [diff] [blame] | 141 | |
| 142 | /* i2c1 */ |
| 143 | writel(PRCM_MOD_EN, &cmper->i2c1clkctrl); |
| 144 | while (readl(&cmper->i2c1clkctrl) != PRCM_MOD_EN) |
| 145 | ; |
Chandan Nath | 2015c38 | 2012-07-24 12:22:17 +0000 | [diff] [blame] | 146 | |
| 147 | /* Ethernet */ |
| 148 | writel(PRCM_MOD_EN, &cmper->cpgmac0clkctrl); |
| 149 | while ((readl(&cmper->cpgmac0clkctrl) & CPGMAC0_IDLE) != PRCM_FUNCTL) |
| 150 | ; |
Tom Rini | 470e2a2 | 2012-08-08 14:29:51 -0700 | [diff] [blame^] | 151 | |
| 152 | /* spi0 */ |
| 153 | writel(PRCM_MOD_EN, &cmper->spi0clkctrl); |
| 154 | while (readl(&cmper->spi0clkctrl) != PRCM_MOD_EN) |
| 155 | ; |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void mpu_pll_config(void) |
| 159 | { |
| 160 | u32 clkmode, clksel, div_m2; |
| 161 | |
| 162 | clkmode = readl(&cmwkup->clkmoddpllmpu); |
| 163 | clksel = readl(&cmwkup->clkseldpllmpu); |
| 164 | div_m2 = readl(&cmwkup->divm2dpllmpu); |
| 165 | |
| 166 | /* Set the PLL to bypass Mode */ |
| 167 | writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllmpu); |
| 168 | while (readl(&cmwkup->idlestdpllmpu) != ST_MN_BYPASS) |
| 169 | ; |
| 170 | |
| 171 | clksel = clksel & (~CLK_SEL_MASK); |
| 172 | clksel = clksel | ((MPUPLL_M << CLK_SEL_SHIFT) | MPUPLL_N); |
| 173 | writel(clksel, &cmwkup->clkseldpllmpu); |
| 174 | |
| 175 | div_m2 = div_m2 & ~CLK_DIV_MASK; |
| 176 | div_m2 = div_m2 | MPUPLL_M2; |
| 177 | writel(div_m2, &cmwkup->divm2dpllmpu); |
| 178 | |
| 179 | clkmode = clkmode | CLK_MODE_SEL; |
| 180 | writel(clkmode, &cmwkup->clkmoddpllmpu); |
| 181 | |
| 182 | while (readl(&cmwkup->idlestdpllmpu) != ST_DPLL_CLK) |
| 183 | ; |
| 184 | } |
| 185 | |
| 186 | static void core_pll_config(void) |
| 187 | { |
| 188 | u32 clkmode, clksel, div_m4, div_m5, div_m6; |
| 189 | |
| 190 | clkmode = readl(&cmwkup->clkmoddpllcore); |
| 191 | clksel = readl(&cmwkup->clkseldpllcore); |
| 192 | div_m4 = readl(&cmwkup->divm4dpllcore); |
| 193 | div_m5 = readl(&cmwkup->divm5dpllcore); |
| 194 | div_m6 = readl(&cmwkup->divm6dpllcore); |
| 195 | |
| 196 | /* Set the PLL to bypass Mode */ |
| 197 | writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllcore); |
| 198 | |
| 199 | while (readl(&cmwkup->idlestdpllcore) != ST_MN_BYPASS) |
| 200 | ; |
| 201 | |
| 202 | clksel = clksel & (~CLK_SEL_MASK); |
| 203 | clksel = clksel | ((COREPLL_M << CLK_SEL_SHIFT) | COREPLL_N); |
| 204 | writel(clksel, &cmwkup->clkseldpllcore); |
| 205 | |
| 206 | div_m4 = div_m4 & ~CLK_DIV_MASK; |
| 207 | div_m4 = div_m4 | COREPLL_M4; |
| 208 | writel(div_m4, &cmwkup->divm4dpllcore); |
| 209 | |
| 210 | div_m5 = div_m5 & ~CLK_DIV_MASK; |
| 211 | div_m5 = div_m5 | COREPLL_M5; |
| 212 | writel(div_m5, &cmwkup->divm5dpllcore); |
| 213 | |
| 214 | div_m6 = div_m6 & ~CLK_DIV_MASK; |
| 215 | div_m6 = div_m6 | COREPLL_M6; |
| 216 | writel(div_m6, &cmwkup->divm6dpllcore); |
| 217 | |
| 218 | clkmode = clkmode | CLK_MODE_SEL; |
| 219 | writel(clkmode, &cmwkup->clkmoddpllcore); |
| 220 | |
| 221 | while (readl(&cmwkup->idlestdpllcore) != ST_DPLL_CLK) |
| 222 | ; |
| 223 | } |
| 224 | |
| 225 | static void per_pll_config(void) |
| 226 | { |
| 227 | u32 clkmode, clksel, div_m2; |
| 228 | |
| 229 | clkmode = readl(&cmwkup->clkmoddpllper); |
| 230 | clksel = readl(&cmwkup->clkseldpllper); |
| 231 | div_m2 = readl(&cmwkup->divm2dpllper); |
| 232 | |
| 233 | /* Set the PLL to bypass Mode */ |
| 234 | writel(PLL_BYPASS_MODE, &cmwkup->clkmoddpllper); |
| 235 | |
| 236 | while (readl(&cmwkup->idlestdpllper) != ST_MN_BYPASS) |
| 237 | ; |
| 238 | |
| 239 | clksel = clksel & (~CLK_SEL_MASK); |
| 240 | clksel = clksel | ((PERPLL_M << CLK_SEL_SHIFT) | PERPLL_N); |
| 241 | writel(clksel, &cmwkup->clkseldpllper); |
| 242 | |
| 243 | div_m2 = div_m2 & ~CLK_DIV2_MASK; |
| 244 | div_m2 = div_m2 | PERPLL_M2; |
| 245 | writel(div_m2, &cmwkup->divm2dpllper); |
| 246 | |
| 247 | clkmode = clkmode | CLK_MODE_SEL; |
| 248 | writel(clkmode, &cmwkup->clkmoddpllper); |
| 249 | |
| 250 | while (readl(&cmwkup->idlestdpllper) != ST_DPLL_CLK) |
| 251 | ; |
| 252 | } |
| 253 | |
Tom Rini | 034aba7 | 2012-07-03 09:20:06 -0700 | [diff] [blame] | 254 | void ddr_pll_config(unsigned int ddrpll_m) |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 255 | { |
| 256 | u32 clkmode, clksel, div_m2; |
| 257 | |
| 258 | clkmode = readl(&cmwkup->clkmoddpllddr); |
| 259 | clksel = readl(&cmwkup->clkseldpllddr); |
| 260 | div_m2 = readl(&cmwkup->divm2dpllddr); |
| 261 | |
| 262 | /* Set the PLL to bypass Mode */ |
| 263 | clkmode = (clkmode & CLK_MODE_MASK) | PLL_BYPASS_MODE; |
| 264 | writel(clkmode, &cmwkup->clkmoddpllddr); |
| 265 | |
| 266 | /* Wait till bypass mode is enabled */ |
| 267 | while ((readl(&cmwkup->idlestdpllddr) & ST_MN_BYPASS) |
| 268 | != ST_MN_BYPASS) |
| 269 | ; |
| 270 | |
| 271 | clksel = clksel & (~CLK_SEL_MASK); |
Tom Rini | 034aba7 | 2012-07-03 09:20:06 -0700 | [diff] [blame] | 272 | clksel = clksel | ((ddrpll_m << CLK_SEL_SHIFT) | DDRPLL_N); |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 273 | writel(clksel, &cmwkup->clkseldpllddr); |
| 274 | |
| 275 | div_m2 = div_m2 & CLK_DIV_SEL; |
| 276 | div_m2 = div_m2 | DDRPLL_M2; |
| 277 | writel(div_m2, &cmwkup->divm2dpllddr); |
| 278 | |
| 279 | clkmode = (clkmode & CLK_MODE_MASK) | CLK_MODE_SEL; |
| 280 | writel(clkmode, &cmwkup->clkmoddpllddr); |
| 281 | |
| 282 | /* Wait till dpll is locked */ |
| 283 | while ((readl(&cmwkup->idlestdpllddr) & ST_DPLL_CLK) != ST_DPLL_CLK) |
| 284 | ; |
| 285 | } |
| 286 | |
| 287 | void enable_emif_clocks(void) |
| 288 | { |
| 289 | /* Enable the EMIF_FW Functional clock */ |
| 290 | writel(PRCM_MOD_EN, &cmper->emiffwclkctrl); |
| 291 | /* Enable EMIF0 Clock */ |
| 292 | writel(PRCM_MOD_EN, &cmper->emifclkctrl); |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 293 | /* Poll if module is functional */ |
| 294 | while ((readl(&cmper->emifclkctrl)) != PRCM_MOD_EN) |
| 295 | ; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Configure the PLL/PRCM for necessary peripherals |
| 300 | */ |
| 301 | void pll_init() |
| 302 | { |
| 303 | mpu_pll_config(); |
| 304 | core_pll_config(); |
| 305 | per_pll_config(); |
Chandan Nath | 4ba3345 | 2011-10-14 02:58:23 +0000 | [diff] [blame] | 306 | |
| 307 | /* Enable the required interconnect clocks */ |
| 308 | enable_interface_clocks(); |
| 309 | |
| 310 | /* Power domain wake up transition */ |
| 311 | power_domain_wkup_transition(); |
| 312 | |
| 313 | /* Enable the required peripherals */ |
| 314 | enable_per_clocks(); |
| 315 | } |