Minkyu Kang | b1b2468 | 2011-01-24 15:22:23 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Samsung Electronics |
| 3 | * Minkyu Kang <mk7.kang@samsung.com> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 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 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/arch/clock.h> |
| 27 | #include <asm/arch/clk.h> |
| 28 | |
| 29 | #ifndef CONFIG_SYS_CLK_FREQ_C210 |
| 30 | #define CONFIG_SYS_CLK_FREQ_C210 24000000 |
| 31 | #endif |
| 32 | |
| 33 | /* s5pc210: return pll clock frequency */ |
| 34 | static unsigned long s5pc210_get_pll_clk(int pllreg) |
| 35 | { |
| 36 | struct s5pc210_clock *clk = |
| 37 | (struct s5pc210_clock *)samsung_get_base_clock(); |
| 38 | unsigned long r, m, p, s, k = 0, mask, fout; |
| 39 | unsigned int freq; |
| 40 | |
| 41 | switch (pllreg) { |
| 42 | case APLL: |
| 43 | r = readl(&clk->apll_con0); |
| 44 | break; |
| 45 | case MPLL: |
| 46 | r = readl(&clk->mpll_con0); |
| 47 | break; |
| 48 | case EPLL: |
| 49 | r = readl(&clk->epll_con0); |
| 50 | k = readl(&clk->epll_con1); |
| 51 | break; |
| 52 | case VPLL: |
| 53 | r = readl(&clk->vpll_con0); |
| 54 | k = readl(&clk->vpll_con1); |
| 55 | break; |
| 56 | default: |
| 57 | printf("Unsupported PLL (%d)\n", pllreg); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * APLL_CON: MIDV [25:16] |
| 63 | * MPLL_CON: MIDV [25:16] |
| 64 | * EPLL_CON: MIDV [24:16] |
| 65 | * VPLL_CON: MIDV [24:16] |
| 66 | */ |
| 67 | if (pllreg == APLL || pllreg == MPLL) |
| 68 | mask = 0x3ff; |
| 69 | else |
| 70 | mask = 0x1ff; |
| 71 | |
| 72 | m = (r >> 16) & mask; |
| 73 | |
| 74 | /* PDIV [13:8] */ |
| 75 | p = (r >> 8) & 0x3f; |
| 76 | /* SDIV [2:0] */ |
| 77 | s = r & 0x7; |
| 78 | |
| 79 | freq = CONFIG_SYS_CLK_FREQ_C210; |
| 80 | |
| 81 | if (pllreg == EPLL) { |
| 82 | k = k & 0xffff; |
| 83 | /* FOUT = (MDIV + K / 65536) * FIN / (PDIV * 2^SDIV) */ |
| 84 | fout = (m + k / 65536) * (freq / (p * (1 << s))); |
| 85 | } else if (pllreg == VPLL) { |
| 86 | k = k & 0xfff; |
| 87 | /* FOUT = (MDIV + K / 1024) * FIN / (PDIV * 2^SDIV) */ |
| 88 | fout = (m + k / 1024) * (freq / (p * (1 << s))); |
| 89 | } else { |
| 90 | if (s < 1) |
| 91 | s = 1; |
| 92 | /* FOUT = MDIV * FIN / (PDIV * 2^(SDIV - 1)) */ |
| 93 | fout = m * (freq / (p * (1 << (s - 1)))); |
| 94 | } |
| 95 | |
| 96 | return fout; |
| 97 | } |
| 98 | |
| 99 | /* s5pc210: return ARM clock frequency */ |
| 100 | static unsigned long s5pc210_get_arm_clk(void) |
| 101 | { |
| 102 | struct s5pc210_clock *clk = |
| 103 | (struct s5pc210_clock *)samsung_get_base_clock(); |
| 104 | unsigned long div; |
| 105 | unsigned long dout_apll; |
| 106 | unsigned int apll_ratio; |
| 107 | |
| 108 | div = readl(&clk->div_cpu0); |
| 109 | |
| 110 | /* APLL_RATIO: [26:24] */ |
| 111 | apll_ratio = (div >> 24) & 0x7; |
| 112 | |
| 113 | dout_apll = get_pll_clk(APLL) / (apll_ratio + 1); |
| 114 | |
| 115 | return dout_apll; |
| 116 | } |
| 117 | |
| 118 | /* s5pc210: return pwm clock frequency */ |
| 119 | static unsigned long s5pc210_get_pwm_clk(void) |
| 120 | { |
| 121 | struct s5pc210_clock *clk = |
| 122 | (struct s5pc210_clock *)samsung_get_base_clock(); |
| 123 | unsigned long pclk, sclk; |
| 124 | unsigned int sel; |
| 125 | unsigned int ratio; |
| 126 | |
| 127 | /* |
| 128 | * CLK_SRC_PERIL0 |
| 129 | * PWM_SEL [27:24] |
| 130 | */ |
| 131 | sel = readl(&clk->src_peril0); |
| 132 | sel = (sel >> 24) & 0xf; |
| 133 | |
| 134 | if (sel == 0x6) |
| 135 | sclk = get_pll_clk(MPLL); |
| 136 | else if (sel == 0x7) |
| 137 | sclk = get_pll_clk(EPLL); |
| 138 | else if (sel == 0x8) |
| 139 | sclk = get_pll_clk(VPLL); |
| 140 | else |
| 141 | return 0; |
| 142 | |
| 143 | /* |
| 144 | * CLK_DIV_PERIL3 |
| 145 | * PWM_RATIO [3:0] |
| 146 | */ |
| 147 | ratio = readl(&clk->div_peril3); |
| 148 | ratio = ratio & 0xf; |
| 149 | |
| 150 | pclk = sclk / (ratio + 1); |
| 151 | |
| 152 | return pclk; |
| 153 | } |
| 154 | |
| 155 | /* s5pc210: return uart clock frequency */ |
| 156 | static unsigned long s5pc210_get_uart_clk(int dev_index) |
| 157 | { |
| 158 | struct s5pc210_clock *clk = |
| 159 | (struct s5pc210_clock *)samsung_get_base_clock(); |
| 160 | unsigned long uclk, sclk; |
| 161 | unsigned int sel; |
| 162 | unsigned int ratio; |
| 163 | |
| 164 | /* |
| 165 | * CLK_SRC_PERIL0 |
| 166 | * UART0_SEL [3:0] |
| 167 | * UART1_SEL [7:4] |
| 168 | * UART2_SEL [8:11] |
| 169 | * UART3_SEL [12:15] |
| 170 | * UART4_SEL [16:19] |
| 171 | * UART5_SEL [23:20] |
| 172 | */ |
| 173 | sel = readl(&clk->src_peril0); |
| 174 | sel = (sel >> (dev_index << 2)) & 0xf; |
| 175 | |
| 176 | if (sel == 0x6) |
| 177 | sclk = get_pll_clk(MPLL); |
| 178 | else if (sel == 0x7) |
| 179 | sclk = get_pll_clk(EPLL); |
| 180 | else if (sel == 0x8) |
| 181 | sclk = get_pll_clk(VPLL); |
| 182 | else |
| 183 | return 0; |
| 184 | |
| 185 | /* |
| 186 | * CLK_DIV_PERIL0 |
| 187 | * UART0_RATIO [3:0] |
| 188 | * UART1_RATIO [7:4] |
| 189 | * UART2_RATIO [8:11] |
| 190 | * UART3_RATIO [12:15] |
| 191 | * UART4_RATIO [16:19] |
| 192 | * UART5_RATIO [23:20] |
| 193 | */ |
| 194 | ratio = readl(&clk->div_peril0); |
| 195 | ratio = (ratio >> (dev_index << 2)) & 0xf; |
| 196 | |
| 197 | uclk = sclk / (ratio + 1); |
| 198 | |
| 199 | return uclk; |
| 200 | } |
| 201 | |
Jaehoon Chung | 9a77221 | 2011-05-17 21:19:17 +0000 | [diff] [blame^] | 202 | /* s5pc210: set the mmc clock */ |
| 203 | static void s5pc210_set_mmc_clk(int dev_index, unsigned int div) |
| 204 | { |
| 205 | struct s5pc210_clock *clk = |
| 206 | (struct s5pc210_clock *)samsung_get_base_clock(); |
| 207 | unsigned int addr; |
| 208 | unsigned int val; |
| 209 | |
| 210 | /* |
| 211 | * CLK_DIV_FSYS1 |
| 212 | * MMC0_PRE_RATIO [15:8], MMC1_PRE_RATIO [31:24] |
| 213 | * CLK_DIV_FSYS2 |
| 214 | * MMC2_PRE_RATIO [15:8], MMC3_PRE_RATIO [31:24] |
| 215 | */ |
| 216 | if (dev_index < 2) { |
| 217 | addr = (unsigned int)&clk->div_fsys1; |
| 218 | } else { |
| 219 | addr = (unsigned int)&clk->div_fsys2; |
| 220 | dev_index -= 2; |
| 221 | } |
| 222 | |
| 223 | val = readl(addr); |
| 224 | val &= ~(0xff << ((dev_index << 4) + 8)); |
| 225 | val |= (div & 0xff) << ((dev_index << 4) + 8); |
| 226 | writel(val, addr); |
| 227 | } |
| 228 | |
Minkyu Kang | b1b2468 | 2011-01-24 15:22:23 +0900 | [diff] [blame] | 229 | unsigned long get_pll_clk(int pllreg) |
| 230 | { |
| 231 | return s5pc210_get_pll_clk(pllreg); |
| 232 | } |
| 233 | |
| 234 | unsigned long get_arm_clk(void) |
| 235 | { |
| 236 | return s5pc210_get_arm_clk(); |
| 237 | } |
| 238 | |
| 239 | unsigned long get_pwm_clk(void) |
| 240 | { |
| 241 | return s5pc210_get_pwm_clk(); |
| 242 | } |
| 243 | |
| 244 | unsigned long get_uart_clk(int dev_index) |
| 245 | { |
| 246 | return s5pc210_get_uart_clk(dev_index); |
| 247 | } |
Jaehoon Chung | 9a77221 | 2011-05-17 21:19:17 +0000 | [diff] [blame^] | 248 | |
| 249 | void set_mmc_clk(int dev_index, unsigned int div) |
| 250 | { |
| 251 | s5pc210_set_mmc_clk(dev_index, div); |
| 252 | } |