Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Emilio López |
| 3 | * |
| 4 | * Emilio López <emilio@elopez.com.ar> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/clk-provider.h> |
| 18 | #include <linux/clkdev.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 21 | #include <linux/reset-controller.h> |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 23 | |
| 24 | #include "clk-factors.h" |
| 25 | |
| 26 | static DEFINE_SPINLOCK(clk_lock); |
| 27 | |
Emilio López | 40a5dcb | 2013-12-23 00:32:32 -0300 | [diff] [blame] | 28 | /* Maximum number of parents our clocks have */ |
| 29 | #define SUNXI_MAX_PARENTS 5 |
| 30 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 31 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 32 | * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1 |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 33 | * PLL1 rate is calculated as follows |
| 34 | * rate = (parent_rate * n * (k + 1) >> p) / (m + 1); |
| 35 | * parent_rate is always 24Mhz |
| 36 | */ |
| 37 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 38 | static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 39 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 40 | { |
| 41 | u8 div; |
| 42 | |
| 43 | /* Normalize value to a 6M multiple */ |
| 44 | div = *freq / 6000000; |
| 45 | *freq = 6000000 * div; |
| 46 | |
| 47 | /* we were called to round the frequency, we can now return */ |
| 48 | if (n == NULL) |
| 49 | return; |
| 50 | |
| 51 | /* m is always zero for pll1 */ |
| 52 | *m = 0; |
| 53 | |
| 54 | /* k is 1 only on these cases */ |
| 55 | if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000) |
| 56 | *k = 1; |
| 57 | else |
| 58 | *k = 0; |
| 59 | |
| 60 | /* p will be 3 for divs under 10 */ |
| 61 | if (div < 10) |
| 62 | *p = 3; |
| 63 | |
| 64 | /* p will be 2 for divs between 10 - 20 and odd divs under 32 */ |
| 65 | else if (div < 20 || (div < 32 && (div & 1))) |
| 66 | *p = 2; |
| 67 | |
| 68 | /* p will be 1 for even divs under 32, divs under 40 and odd pairs |
| 69 | * of divs between 40-62 */ |
| 70 | else if (div < 40 || (div < 64 && (div & 2))) |
| 71 | *p = 1; |
| 72 | |
| 73 | /* any other entries have p = 0 */ |
| 74 | else |
| 75 | *p = 0; |
| 76 | |
| 77 | /* calculate a suitable n based on k and p */ |
| 78 | div <<= *p; |
| 79 | div /= (*k + 1); |
| 80 | *n = div / 4; |
| 81 | } |
| 82 | |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 83 | /** |
| 84 | * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1 |
| 85 | * PLL1 rate is calculated as follows |
| 86 | * rate = parent_rate * (n + 1) * (k + 1) / (m + 1); |
| 87 | * parent_rate should always be 24MHz |
| 88 | */ |
| 89 | static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate, |
| 90 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 91 | { |
| 92 | /* |
| 93 | * We can operate only on MHz, this will make our life easier |
| 94 | * later. |
| 95 | */ |
| 96 | u32 freq_mhz = *freq / 1000000; |
| 97 | u32 parent_freq_mhz = parent_rate / 1000000; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 98 | |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Round down the frequency to the closest multiple of either |
| 101 | * 6 or 16 |
| 102 | */ |
| 103 | u32 round_freq_6 = round_down(freq_mhz, 6); |
| 104 | u32 round_freq_16 = round_down(freq_mhz, 16); |
| 105 | |
| 106 | if (round_freq_6 > round_freq_16) |
| 107 | freq_mhz = round_freq_6; |
| 108 | else |
| 109 | freq_mhz = round_freq_16; |
| 110 | |
| 111 | *freq = freq_mhz * 1000000; |
| 112 | |
| 113 | /* |
| 114 | * If the factors pointer are null, we were just called to |
| 115 | * round down the frequency. |
| 116 | * Exit. |
| 117 | */ |
| 118 | if (n == NULL) |
| 119 | return; |
| 120 | |
| 121 | /* If the frequency is a multiple of 32 MHz, k is always 3 */ |
| 122 | if (!(freq_mhz % 32)) |
| 123 | *k = 3; |
| 124 | /* If the frequency is a multiple of 9 MHz, k is always 2 */ |
| 125 | else if (!(freq_mhz % 9)) |
| 126 | *k = 2; |
| 127 | /* If the frequency is a multiple of 8 MHz, k is always 1 */ |
| 128 | else if (!(freq_mhz % 8)) |
| 129 | *k = 1; |
| 130 | /* Otherwise, we don't use the k factor */ |
| 131 | else |
| 132 | *k = 0; |
| 133 | |
| 134 | /* |
| 135 | * If the frequency is a multiple of 2 but not a multiple of |
| 136 | * 3, m is 3. This is the first time we use 6 here, yet we |
| 137 | * will use it on several other places. |
| 138 | * We use this number because it's the lowest frequency we can |
| 139 | * generate (with n = 0, k = 0, m = 3), so every other frequency |
| 140 | * somehow relates to this frequency. |
| 141 | */ |
| 142 | if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4) |
| 143 | *m = 2; |
| 144 | /* |
| 145 | * If the frequency is a multiple of 6MHz, but the factor is |
| 146 | * odd, m will be 3 |
| 147 | */ |
| 148 | else if ((freq_mhz / 6) & 1) |
| 149 | *m = 3; |
| 150 | /* Otherwise, we end up with m = 1 */ |
| 151 | else |
| 152 | *m = 1; |
| 153 | |
| 154 | /* Calculate n thanks to the above factors we already got */ |
| 155 | *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1; |
| 156 | |
| 157 | /* |
| 158 | * If n end up being outbound, and that we can still decrease |
| 159 | * m, do it. |
| 160 | */ |
| 161 | if ((*n + 1) > 31 && (*m + 1) > 1) { |
| 162 | *n = (*n + 1) / 2 - 1; |
| 163 | *m = (*m + 1) / 2 - 1; |
| 164 | } |
| 165 | } |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 166 | |
| 167 | /** |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 168 | * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1 |
| 169 | * PLL1 rate is calculated as follows |
| 170 | * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1); |
| 171 | * parent_rate is always 24Mhz |
| 172 | */ |
| 173 | |
| 174 | static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate, |
| 175 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 176 | { |
| 177 | u8 div; |
| 178 | |
| 179 | /* Normalize value to a 6M multiple */ |
| 180 | div = *freq / 6000000; |
| 181 | *freq = 6000000 * div; |
| 182 | |
| 183 | /* we were called to round the frequency, we can now return */ |
| 184 | if (n == NULL) |
| 185 | return; |
| 186 | |
| 187 | /* m is always zero for pll1 */ |
| 188 | *m = 0; |
| 189 | |
| 190 | /* k is 1 only on these cases */ |
| 191 | if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000) |
| 192 | *k = 1; |
| 193 | else |
| 194 | *k = 0; |
| 195 | |
| 196 | /* p will be 2 for divs under 20 and odd divs under 32 */ |
| 197 | if (div < 20 || (div < 32 && (div & 1))) |
| 198 | *p = 2; |
| 199 | |
| 200 | /* p will be 1 for even divs under 32, divs under 40 and odd pairs |
| 201 | * of divs between 40-62 */ |
| 202 | else if (div < 40 || (div < 64 && (div & 2))) |
| 203 | *p = 1; |
| 204 | |
| 205 | /* any other entries have p = 0 */ |
| 206 | else |
| 207 | *p = 0; |
| 208 | |
| 209 | /* calculate a suitable n based on k and p */ |
| 210 | div <<= *p; |
| 211 | div /= (*k + 1); |
| 212 | *n = div / 4 - 1; |
| 213 | } |
| 214 | |
| 215 | /** |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 216 | * sun4i_get_pll5_factors() - calculates n, k factors for PLL5 |
| 217 | * PLL5 rate is calculated as follows |
| 218 | * rate = parent_rate * n * (k + 1) |
| 219 | * parent_rate is always 24Mhz |
| 220 | */ |
| 221 | |
| 222 | static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate, |
| 223 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 224 | { |
| 225 | u8 div; |
| 226 | |
| 227 | /* Normalize value to a parent_rate multiple (24M) */ |
| 228 | div = *freq / parent_rate; |
| 229 | *freq = parent_rate * div; |
| 230 | |
| 231 | /* we were called to round the frequency, we can now return */ |
| 232 | if (n == NULL) |
| 233 | return; |
| 234 | |
| 235 | if (div < 31) |
| 236 | *k = 0; |
| 237 | else if (div / 2 < 31) |
| 238 | *k = 1; |
| 239 | else if (div / 3 < 31) |
| 240 | *k = 2; |
| 241 | else |
| 242 | *k = 3; |
| 243 | |
| 244 | *n = DIV_ROUND_UP(div, (*k+1)); |
| 245 | } |
| 246 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 247 | /** |
| 248 | * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6 |
| 249 | * PLL6 rate is calculated as follows |
| 250 | * rate = parent_rate * n * (k + 1) / 2 |
| 251 | * parent_rate is always 24Mhz |
| 252 | */ |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 253 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 254 | static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate, |
| 255 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 256 | { |
| 257 | u8 div; |
| 258 | |
| 259 | /* |
| 260 | * We always have 24MHz / 2, so we can just say that our |
| 261 | * parent clock is 12MHz. |
| 262 | */ |
| 263 | parent_rate = parent_rate / 2; |
| 264 | |
| 265 | /* Normalize value to a parent_rate multiple (24M / 2) */ |
| 266 | div = *freq / parent_rate; |
| 267 | *freq = parent_rate * div; |
| 268 | |
| 269 | /* we were called to round the frequency, we can now return */ |
| 270 | if (n == NULL) |
| 271 | return; |
| 272 | |
| 273 | *k = div / 32; |
| 274 | if (*k > 3) |
| 275 | *k = 3; |
| 276 | |
| 277 | *n = DIV_ROUND_UP(div, (*k+1)); |
| 278 | } |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 279 | |
| 280 | /** |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 281 | * sun4i_get_apb1_factors() - calculates m, p factors for APB1 |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 282 | * APB1 rate is calculated as follows |
| 283 | * rate = (parent_rate >> p) / (m + 1); |
| 284 | */ |
| 285 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 286 | static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 287 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 288 | { |
| 289 | u8 calcm, calcp; |
| 290 | |
| 291 | if (parent_rate < *freq) |
| 292 | *freq = parent_rate; |
| 293 | |
Emilio López | 2226013 | 2014-03-19 15:19:32 -0300 | [diff] [blame] | 294 | parent_rate = DIV_ROUND_UP(parent_rate, *freq); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 295 | |
| 296 | /* Invalid rate! */ |
| 297 | if (parent_rate > 32) |
| 298 | return; |
| 299 | |
| 300 | if (parent_rate <= 4) |
| 301 | calcp = 0; |
| 302 | else if (parent_rate <= 8) |
| 303 | calcp = 1; |
| 304 | else if (parent_rate <= 16) |
| 305 | calcp = 2; |
| 306 | else |
| 307 | calcp = 3; |
| 308 | |
| 309 | calcm = (parent_rate >> calcp) - 1; |
| 310 | |
| 311 | *freq = (parent_rate >> calcp) / (calcm + 1); |
| 312 | |
| 313 | /* we were called to round the frequency, we can now return */ |
| 314 | if (n == NULL) |
| 315 | return; |
| 316 | |
| 317 | *m = calcm; |
| 318 | *p = calcp; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | /** |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 324 | * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks |
Emilio López | 9ce71ca | 2014-03-19 15:19:33 -0300 | [diff] [blame] | 325 | * MOD0 rate is calculated as follows |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 326 | * rate = (parent_rate >> p) / (m + 1); |
| 327 | */ |
| 328 | |
| 329 | static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate, |
| 330 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 331 | { |
| 332 | u8 div, calcm, calcp; |
| 333 | |
| 334 | /* These clocks can only divide, so we will never be able to achieve |
| 335 | * frequencies higher than the parent frequency */ |
| 336 | if (*freq > parent_rate) |
| 337 | *freq = parent_rate; |
| 338 | |
Emilio López | 2226013 | 2014-03-19 15:19:32 -0300 | [diff] [blame] | 339 | div = DIV_ROUND_UP(parent_rate, *freq); |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 340 | |
| 341 | if (div < 16) |
| 342 | calcp = 0; |
| 343 | else if (div / 2 < 16) |
| 344 | calcp = 1; |
| 345 | else if (div / 4 < 16) |
| 346 | calcp = 2; |
| 347 | else |
| 348 | calcp = 3; |
| 349 | |
| 350 | calcm = DIV_ROUND_UP(div, 1 << calcp); |
| 351 | |
| 352 | *freq = (parent_rate >> calcp) / calcm; |
| 353 | |
| 354 | /* we were called to round the frequency, we can now return */ |
| 355 | if (n == NULL) |
| 356 | return; |
| 357 | |
| 358 | *m = calcm - 1; |
| 359 | *p = calcp; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | /** |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 365 | * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B |
| 366 | * CLK_OUT rate is calculated as follows |
| 367 | * rate = (parent_rate >> p) / (m + 1); |
| 368 | */ |
| 369 | |
| 370 | static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate, |
| 371 | u8 *n, u8 *k, u8 *m, u8 *p) |
| 372 | { |
| 373 | u8 div, calcm, calcp; |
| 374 | |
| 375 | /* These clocks can only divide, so we will never be able to achieve |
| 376 | * frequencies higher than the parent frequency */ |
| 377 | if (*freq > parent_rate) |
| 378 | *freq = parent_rate; |
| 379 | |
Emilio López | 2226013 | 2014-03-19 15:19:32 -0300 | [diff] [blame] | 380 | div = DIV_ROUND_UP(parent_rate, *freq); |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 381 | |
| 382 | if (div < 32) |
| 383 | calcp = 0; |
| 384 | else if (div / 2 < 32) |
| 385 | calcp = 1; |
| 386 | else if (div / 4 < 32) |
| 387 | calcp = 2; |
| 388 | else |
| 389 | calcp = 3; |
| 390 | |
| 391 | calcm = DIV_ROUND_UP(div, 1 << calcp); |
| 392 | |
| 393 | *freq = (parent_rate >> calcp) / calcm; |
| 394 | |
| 395 | /* we were called to round the frequency, we can now return */ |
| 396 | if (n == NULL) |
| 397 | return; |
| 398 | |
| 399 | *m = calcm - 1; |
| 400 | *p = calcp; |
| 401 | } |
| 402 | |
Chen-Yu Tsai | e4c6d6c | 2014-02-10 18:35:47 +0800 | [diff] [blame] | 403 | /** |
Emilio López | 9571397 | 2014-05-02 17:57:16 +0200 | [diff] [blame] | 404 | * clk_sunxi_mmc_phase_control() - configures MMC clock phase control |
| 405 | */ |
| 406 | |
Hans de Goede | a97181a | 2014-05-12 14:04:47 +0200 | [diff] [blame] | 407 | void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output) |
Emilio López | 9571397 | 2014-05-02 17:57:16 +0200 | [diff] [blame] | 408 | { |
| 409 | #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw) |
| 410 | #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw) |
| 411 | |
Hans de Goede | a97181a | 2014-05-12 14:04:47 +0200 | [diff] [blame] | 412 | struct clk_hw *hw = __clk_get_hw(clk); |
Emilio López | 9571397 | 2014-05-02 17:57:16 +0200 | [diff] [blame] | 413 | struct clk_composite *composite = to_clk_composite(hw); |
| 414 | struct clk_hw *rate_hw = composite->rate_hw; |
| 415 | struct clk_factors *factors = to_clk_factors(rate_hw); |
| 416 | unsigned long flags = 0; |
| 417 | u32 reg; |
| 418 | |
| 419 | if (factors->lock) |
| 420 | spin_lock_irqsave(factors->lock, flags); |
| 421 | |
| 422 | reg = readl(factors->reg); |
| 423 | |
| 424 | /* set sample clock phase control */ |
| 425 | reg &= ~(0x7 << 20); |
| 426 | reg |= ((sample & 0x7) << 20); |
| 427 | |
| 428 | /* set output clock phase control */ |
| 429 | reg &= ~(0x7 << 8); |
| 430 | reg |= ((output & 0x7) << 8); |
| 431 | |
| 432 | writel(reg, factors->reg); |
| 433 | |
| 434 | if (factors->lock) |
| 435 | spin_unlock_irqrestore(factors->lock, flags); |
| 436 | } |
| 437 | EXPORT_SYMBOL(clk_sunxi_mmc_phase_control); |
| 438 | |
| 439 | |
| 440 | /** |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 441 | * sunxi_factors_clk_setup() - Setup function for factor clocks |
| 442 | */ |
| 443 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 444 | static struct clk_factors_config sun4i_pll1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 445 | .nshift = 8, |
| 446 | .nwidth = 5, |
| 447 | .kshift = 4, |
| 448 | .kwidth = 2, |
| 449 | .mshift = 0, |
| 450 | .mwidth = 2, |
| 451 | .pshift = 16, |
| 452 | .pwidth = 2, |
| 453 | }; |
| 454 | |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 455 | static struct clk_factors_config sun6i_a31_pll1_config = { |
| 456 | .nshift = 8, |
| 457 | .nwidth = 5, |
| 458 | .kshift = 4, |
| 459 | .kwidth = 2, |
| 460 | .mshift = 0, |
| 461 | .mwidth = 2, |
| 462 | }; |
| 463 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 464 | static struct clk_factors_config sun8i_a23_pll1_config = { |
| 465 | .nshift = 8, |
| 466 | .nwidth = 5, |
| 467 | .kshift = 4, |
| 468 | .kwidth = 2, |
| 469 | .mshift = 0, |
| 470 | .mwidth = 2, |
| 471 | .pshift = 16, |
| 472 | .pwidth = 2, |
| 473 | .n_start = 1, |
| 474 | }; |
| 475 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 476 | static struct clk_factors_config sun4i_pll5_config = { |
| 477 | .nshift = 8, |
| 478 | .nwidth = 5, |
| 479 | .kshift = 4, |
| 480 | .kwidth = 2, |
| 481 | }; |
| 482 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 483 | static struct clk_factors_config sun6i_a31_pll6_config = { |
| 484 | .nshift = 8, |
| 485 | .nwidth = 5, |
| 486 | .kshift = 4, |
| 487 | .kwidth = 2, |
| 488 | }; |
| 489 | |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 490 | static struct clk_factors_config sun4i_apb1_config = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 491 | .mshift = 0, |
| 492 | .mwidth = 5, |
| 493 | .pshift = 16, |
| 494 | .pwidth = 2, |
| 495 | }; |
| 496 | |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 497 | /* user manual says "n" but it's really "p" */ |
| 498 | static struct clk_factors_config sun4i_mod0_config = { |
| 499 | .mshift = 0, |
| 500 | .mwidth = 4, |
| 501 | .pshift = 16, |
| 502 | .pwidth = 2, |
| 503 | }; |
| 504 | |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 505 | /* user manual says "n" but it's really "p" */ |
| 506 | static struct clk_factors_config sun7i_a20_out_config = { |
| 507 | .mshift = 8, |
| 508 | .mwidth = 5, |
| 509 | .pshift = 20, |
| 510 | .pwidth = 2, |
| 511 | }; |
| 512 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 513 | static const struct factors_data sun4i_pll1_data __initconst = { |
Emilio López | d838ff3 | 2013-12-23 00:32:34 -0300 | [diff] [blame] | 514 | .enable = 31, |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 515 | .table = &sun4i_pll1_config, |
| 516 | .getter = sun4i_get_pll1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 517 | }; |
| 518 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 519 | static const struct factors_data sun6i_a31_pll1_data __initconst = { |
Emilio López | d838ff3 | 2013-12-23 00:32:34 -0300 | [diff] [blame] | 520 | .enable = 31, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 521 | .table = &sun6i_a31_pll1_config, |
| 522 | .getter = sun6i_a31_get_pll1_factors, |
| 523 | }; |
| 524 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 525 | static const struct factors_data sun8i_a23_pll1_data __initconst = { |
| 526 | .enable = 31, |
| 527 | .table = &sun8i_a23_pll1_config, |
| 528 | .getter = sun8i_a23_get_pll1_factors, |
| 529 | }; |
| 530 | |
Emilio López | 5a8ddf2 | 2014-03-19 15:19:30 -0300 | [diff] [blame] | 531 | static const struct factors_data sun7i_a20_pll4_data __initconst = { |
| 532 | .enable = 31, |
| 533 | .table = &sun4i_pll5_config, |
| 534 | .getter = sun4i_get_pll5_factors, |
| 535 | }; |
| 536 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 537 | static const struct factors_data sun4i_pll5_data __initconst = { |
| 538 | .enable = 31, |
| 539 | .table = &sun4i_pll5_config, |
| 540 | .getter = sun4i_get_pll5_factors, |
Chen-Yu Tsai | 667f542 | 2014-02-03 09:51:39 +0800 | [diff] [blame] | 541 | .name = "pll5", |
| 542 | }; |
| 543 | |
| 544 | static const struct factors_data sun4i_pll6_data __initconst = { |
| 545 | .enable = 31, |
| 546 | .table = &sun4i_pll5_config, |
| 547 | .getter = sun4i_get_pll5_factors, |
| 548 | .name = "pll6", |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 549 | }; |
| 550 | |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 551 | static const struct factors_data sun6i_a31_pll6_data __initconst = { |
| 552 | .enable = 31, |
| 553 | .table = &sun6i_a31_pll6_config, |
| 554 | .getter = sun6i_a31_get_pll6_factors, |
| 555 | }; |
| 556 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 557 | static const struct factors_data sun4i_apb1_data __initconst = { |
Maxime Ripard | 81ba6c5 | 2013-07-22 18:21:32 +0200 | [diff] [blame] | 558 | .table = &sun4i_apb1_config, |
| 559 | .getter = sun4i_get_apb1_factors, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 560 | }; |
| 561 | |
Emilio López | 7551769 | 2013-12-23 00:32:39 -0300 | [diff] [blame] | 562 | static const struct factors_data sun4i_mod0_data __initconst = { |
| 563 | .enable = 31, |
| 564 | .mux = 24, |
| 565 | .table = &sun4i_mod0_config, |
| 566 | .getter = sun4i_get_mod0_factors, |
| 567 | }; |
| 568 | |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 569 | static const struct factors_data sun7i_a20_out_data __initconst = { |
| 570 | .enable = 31, |
| 571 | .mux = 24, |
| 572 | .table = &sun7i_a20_out_config, |
| 573 | .getter = sun7i_a20_get_out_factors, |
| 574 | }; |
| 575 | |
Emilio López | 5f4e0be | 2013-12-23 00:32:36 -0300 | [diff] [blame] | 576 | static struct clk * __init sunxi_factors_clk_setup(struct device_node *node, |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 577 | const struct factors_data *data) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 578 | { |
Maxime Ripard | 601da9d | 2014-07-04 22:24:52 +0200 | [diff] [blame] | 579 | return sunxi_factors_register(node, data, &clk_lock); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | /** |
| 585 | * sunxi_mux_clk_setup() - Setup function for muxes |
| 586 | */ |
| 587 | |
| 588 | #define SUNXI_MUX_GATE_WIDTH 2 |
| 589 | |
| 590 | struct mux_data { |
| 591 | u8 shift; |
| 592 | }; |
| 593 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 594 | static const struct mux_data sun4i_cpu_mux_data __initconst = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 595 | .shift = 16, |
| 596 | }; |
| 597 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 598 | static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 599 | .shift = 12, |
| 600 | }; |
| 601 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 602 | static const struct mux_data sun4i_apb1_mux_data __initconst = { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 603 | .shift = 24, |
| 604 | }; |
| 605 | |
| 606 | static void __init sunxi_mux_clk_setup(struct device_node *node, |
| 607 | struct mux_data *data) |
| 608 | { |
| 609 | struct clk *clk; |
| 610 | const char *clk_name = node->name; |
Emilio López | edaf3fb | 2013-12-23 00:32:33 -0300 | [diff] [blame] | 611 | const char *parents[SUNXI_MAX_PARENTS]; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 612 | void __iomem *reg; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 613 | int i = 0; |
| 614 | |
| 615 | reg = of_iomap(node, 0); |
| 616 | |
Emilio López | edaf3fb | 2013-12-23 00:32:33 -0300 | [diff] [blame] | 617 | while (i < SUNXI_MAX_PARENTS && |
| 618 | (parents[i] = of_clk_get_parent_name(node, i)) != NULL) |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 619 | i++; |
| 620 | |
Chen-Yu Tsai | f64111e | 2014-02-03 09:51:37 +0800 | [diff] [blame] | 621 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 622 | |
James Hogan | 819c1de | 2013-07-29 12:25:01 +0100 | [diff] [blame] | 623 | clk = clk_register_mux(NULL, clk_name, parents, i, |
| 624 | CLK_SET_RATE_NO_REPARENT, reg, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 625 | data->shift, SUNXI_MUX_GATE_WIDTH, |
| 626 | 0, &clk_lock); |
| 627 | |
| 628 | if (clk) { |
| 629 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 630 | clk_register_clkdev(clk, clk_name, NULL); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | /** |
| 637 | * sunxi_divider_clk_setup() - Setup function for simple divider clocks |
| 638 | */ |
| 639 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 640 | struct div_data { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 641 | u8 shift; |
| 642 | u8 pow; |
| 643 | u8 width; |
Chen-Yu Tsai | ea5671b | 2014-06-26 23:55:42 +0800 | [diff] [blame] | 644 | const struct clk_div_table *table; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 645 | }; |
| 646 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 647 | static const struct div_data sun4i_axi_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 648 | .shift = 0, |
| 649 | .pow = 0, |
| 650 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 651 | }; |
| 652 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 653 | static const struct clk_div_table sun8i_a23_axi_table[] __initconst = { |
| 654 | { .val = 0, .div = 1 }, |
| 655 | { .val = 1, .div = 2 }, |
| 656 | { .val = 2, .div = 3 }, |
| 657 | { .val = 3, .div = 4 }, |
| 658 | { .val = 4, .div = 4 }, |
| 659 | { .val = 5, .div = 4 }, |
| 660 | { .val = 6, .div = 4 }, |
| 661 | { .val = 7, .div = 4 }, |
| 662 | { } /* sentinel */ |
| 663 | }; |
| 664 | |
| 665 | static const struct div_data sun8i_a23_axi_data __initconst = { |
| 666 | .width = 3, |
| 667 | .table = sun8i_a23_axi_table, |
| 668 | }; |
| 669 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 670 | static const struct div_data sun4i_ahb_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 671 | .shift = 4, |
| 672 | .pow = 1, |
| 673 | .width = 2, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 674 | }; |
| 675 | |
Chen-Yu Tsai | cfe4c93 | 2014-09-06 14:45:10 +0800 | [diff] [blame] | 676 | static const struct clk_div_table sun4i_apb0_table[] __initconst = { |
| 677 | { .val = 0, .div = 2 }, |
| 678 | { .val = 1, .div = 2 }, |
| 679 | { .val = 2, .div = 4 }, |
| 680 | { .val = 3, .div = 8 }, |
| 681 | { } /* sentinel */ |
| 682 | }; |
| 683 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 684 | static const struct div_data sun4i_apb0_data __initconst = { |
Maxime Ripard | 70855bb | 2013-07-23 09:25:56 +0200 | [diff] [blame] | 685 | .shift = 8, |
| 686 | .pow = 1, |
| 687 | .width = 2, |
Chen-Yu Tsai | cfe4c93 | 2014-09-06 14:45:10 +0800 | [diff] [blame] | 688 | .table = sun4i_apb0_table, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 689 | }; |
| 690 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 691 | static const struct div_data sun6i_a31_apb2_div_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 692 | .shift = 0, |
| 693 | .pow = 0, |
| 694 | .width = 4, |
| 695 | }; |
| 696 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 697 | static void __init sunxi_divider_clk_setup(struct device_node *node, |
| 698 | struct div_data *data) |
| 699 | { |
| 700 | struct clk *clk; |
| 701 | const char *clk_name = node->name; |
| 702 | const char *clk_parent; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 703 | void __iomem *reg; |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 704 | |
| 705 | reg = of_iomap(node, 0); |
| 706 | |
| 707 | clk_parent = of_clk_get_parent_name(node, 0); |
| 708 | |
Chen-Yu Tsai | f64111e | 2014-02-03 09:51:37 +0800 | [diff] [blame] | 709 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 710 | |
Chen-Yu Tsai | ea5671b | 2014-06-26 23:55:42 +0800 | [diff] [blame] | 711 | clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0, |
| 712 | reg, data->shift, data->width, |
| 713 | data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0, |
| 714 | data->table, &clk_lock); |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 715 | if (clk) { |
| 716 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 717 | clk_register_clkdev(clk, clk_name, NULL); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 722 | |
| 723 | /** |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 724 | * sunxi_gates_reset... - reset bits in leaf gate clk registers handling |
| 725 | */ |
| 726 | |
| 727 | struct gates_reset_data { |
| 728 | void __iomem *reg; |
| 729 | spinlock_t *lock; |
| 730 | struct reset_controller_dev rcdev; |
| 731 | }; |
| 732 | |
| 733 | static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev, |
| 734 | unsigned long id) |
| 735 | { |
| 736 | struct gates_reset_data *data = container_of(rcdev, |
| 737 | struct gates_reset_data, |
| 738 | rcdev); |
| 739 | unsigned long flags; |
| 740 | u32 reg; |
| 741 | |
| 742 | spin_lock_irqsave(data->lock, flags); |
| 743 | |
| 744 | reg = readl(data->reg); |
| 745 | writel(reg & ~BIT(id), data->reg); |
| 746 | |
| 747 | spin_unlock_irqrestore(data->lock, flags); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev, |
| 753 | unsigned long id) |
| 754 | { |
| 755 | struct gates_reset_data *data = container_of(rcdev, |
| 756 | struct gates_reset_data, |
| 757 | rcdev); |
| 758 | unsigned long flags; |
| 759 | u32 reg; |
| 760 | |
| 761 | spin_lock_irqsave(data->lock, flags); |
| 762 | |
| 763 | reg = readl(data->reg); |
| 764 | writel(reg | BIT(id), data->reg); |
| 765 | |
| 766 | spin_unlock_irqrestore(data->lock, flags); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static struct reset_control_ops sunxi_gates_reset_ops = { |
| 772 | .assert = sunxi_gates_reset_assert, |
| 773 | .deassert = sunxi_gates_reset_deassert, |
| 774 | }; |
| 775 | |
| 776 | /** |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 777 | * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks |
| 778 | */ |
| 779 | |
| 780 | #define SUNXI_GATES_MAX_SIZE 64 |
| 781 | |
| 782 | struct gates_data { |
| 783 | DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE); |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 784 | u32 reset_mask; |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 785 | }; |
| 786 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 787 | static const struct gates_data sun4i_axi_gates_data __initconst = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 788 | .mask = {1}, |
| 789 | }; |
| 790 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 791 | static const struct gates_data sun4i_ahb_gates_data __initconst = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 792 | .mask = {0x7F77FFF, 0x14FB3F}, |
| 793 | }; |
| 794 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 795 | static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = { |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 796 | .mask = {0x147667e7, 0x185915}, |
| 797 | }; |
| 798 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 799 | static const struct gates_data sun5i_a13_ahb_gates_data __initconst = { |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 800 | .mask = {0x107067e7, 0x185111}, |
| 801 | }; |
| 802 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 803 | static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 804 | .mask = {0xEDFE7F62, 0x794F931}, |
| 805 | }; |
| 806 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 807 | static const struct gates_data sun7i_a20_ahb_gates_data __initconst = { |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 808 | .mask = { 0x12f77fff, 0x16ff3f }, |
| 809 | }; |
| 810 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 811 | static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = { |
| 812 | .mask = {0x25386742, 0x2505111}, |
| 813 | }; |
| 814 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 815 | static const struct gates_data sun4i_apb0_gates_data __initconst = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 816 | .mask = {0x4EF}, |
| 817 | }; |
| 818 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 819 | static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = { |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 820 | .mask = {0x469}, |
| 821 | }; |
| 822 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 823 | static const struct gates_data sun5i_a13_apb0_gates_data __initconst = { |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 824 | .mask = {0x61}, |
| 825 | }; |
| 826 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 827 | static const struct gates_data sun7i_a20_apb0_gates_data __initconst = { |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 828 | .mask = { 0x4ff }, |
| 829 | }; |
| 830 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 831 | static const struct gates_data sun4i_apb1_gates_data __initconst = { |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 832 | .mask = {0xFF00F7}, |
| 833 | }; |
| 834 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 835 | static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = { |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 836 | .mask = {0xf0007}, |
| 837 | }; |
| 838 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 839 | static const struct gates_data sun5i_a13_apb1_gates_data __initconst = { |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 840 | .mask = {0xa0007}, |
| 841 | }; |
| 842 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 843 | static const struct gates_data sun6i_a31_apb1_gates_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 844 | .mask = {0x3031}, |
| 845 | }; |
| 846 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 847 | static const struct gates_data sun8i_a23_apb1_gates_data __initconst = { |
| 848 | .mask = {0x3021}, |
| 849 | }; |
| 850 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 851 | static const struct gates_data sun6i_a31_apb2_gates_data __initconst = { |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 852 | .mask = {0x3F000F}, |
| 853 | }; |
| 854 | |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 855 | static const struct gates_data sun7i_a20_apb1_gates_data __initconst = { |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 856 | .mask = { 0xff80ff }, |
| 857 | }; |
| 858 | |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 859 | static const struct gates_data sun8i_a23_apb2_gates_data __initconst = { |
| 860 | .mask = {0x1F0007}, |
| 861 | }; |
| 862 | |
Roman Byshko | 5abdbf2 | 2014-02-07 16:21:50 +0100 | [diff] [blame] | 863 | static const struct gates_data sun4i_a10_usb_gates_data __initconst = { |
| 864 | .mask = {0x1C0}, |
| 865 | .reset_mask = 0x07, |
| 866 | }; |
| 867 | |
| 868 | static const struct gates_data sun5i_a13_usb_gates_data __initconst = { |
| 869 | .mask = {0x140}, |
| 870 | .reset_mask = 0x03, |
| 871 | }; |
| 872 | |
Maxime Ripard | e0e7943 | 2014-05-13 17:44:15 +0200 | [diff] [blame] | 873 | static const struct gates_data sun6i_a31_usb_gates_data __initconst = { |
| 874 | .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) }, |
| 875 | .reset_mask = BIT(2) | BIT(1) | BIT(0), |
| 876 | }; |
| 877 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 878 | static void __init sunxi_gates_clk_setup(struct device_node *node, |
| 879 | struct gates_data *data) |
| 880 | { |
| 881 | struct clk_onecell_data *clk_data; |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 882 | struct gates_reset_data *reset_data; |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 883 | const char *clk_parent; |
| 884 | const char *clk_name; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 885 | void __iomem *reg; |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 886 | int qty; |
| 887 | int i = 0; |
| 888 | int j = 0; |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 889 | |
| 890 | reg = of_iomap(node, 0); |
| 891 | |
| 892 | clk_parent = of_clk_get_parent_name(node, 0); |
| 893 | |
| 894 | /* Worst-case size approximation and memory allocation */ |
| 895 | qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE); |
| 896 | clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL); |
| 897 | if (!clk_data) |
| 898 | return; |
| 899 | clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL); |
| 900 | if (!clk_data->clks) { |
| 901 | kfree(clk_data); |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) { |
| 906 | of_property_read_string_index(node, "clock-output-names", |
| 907 | j, &clk_name); |
| 908 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 909 | clk_data->clks[i] = clk_register_gate(NULL, clk_name, |
Chen-Yu Tsai | 70eab19 | 2014-06-26 23:55:40 +0800 | [diff] [blame] | 910 | clk_parent, 0, |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 911 | reg + 4 * (i/32), i % 32, |
| 912 | 0, &clk_lock); |
| 913 | WARN_ON(IS_ERR(clk_data->clks[i])); |
Chen-Yu Tsai | d14e470 | 2014-06-26 23:55:39 +0800 | [diff] [blame] | 914 | clk_register_clkdev(clk_data->clks[i], clk_name, NULL); |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 915 | |
| 916 | j++; |
| 917 | } |
| 918 | |
| 919 | /* Adjust to the real max */ |
| 920 | clk_data->clk_num = i; |
| 921 | |
| 922 | of_clk_add_provider(node, of_clk_src_onecell_get, clk_data); |
Hans de Goede | cfb0086 | 2014-02-07 16:21:49 +0100 | [diff] [blame] | 923 | |
| 924 | /* Register a reset controler for gates with reset bits */ |
| 925 | if (data->reset_mask == 0) |
| 926 | return; |
| 927 | |
| 928 | reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL); |
| 929 | if (!reset_data) |
| 930 | return; |
| 931 | |
| 932 | reset_data->reg = reg; |
| 933 | reset_data->lock = &clk_lock; |
| 934 | reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1; |
| 935 | reset_data->rcdev.ops = &sunxi_gates_reset_ops; |
| 936 | reset_data->rcdev.of_node = node; |
| 937 | reset_controller_register(&reset_data->rcdev); |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 938 | } |
| 939 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 940 | |
| 941 | |
| 942 | /** |
| 943 | * sunxi_divs_clk_setup() helper data |
| 944 | */ |
| 945 | |
| 946 | #define SUNXI_DIVS_MAX_QTY 2 |
| 947 | #define SUNXI_DIVISOR_WIDTH 2 |
| 948 | |
| 949 | struct divs_data { |
| 950 | const struct factors_data *factors; /* data for the factor clock */ |
| 951 | struct { |
| 952 | u8 fixed; /* is it a fixed divisor? if not... */ |
| 953 | struct clk_div_table *table; /* is it a table based divisor? */ |
| 954 | u8 shift; /* otherwise it's a normal divisor with this shift */ |
| 955 | u8 pow; /* is it power-of-two based? */ |
| 956 | u8 gate; /* is it independently gateable? */ |
| 957 | } div[SUNXI_DIVS_MAX_QTY]; |
| 958 | }; |
| 959 | |
| 960 | static struct clk_div_table pll6_sata_tbl[] = { |
| 961 | { .val = 0, .div = 6, }, |
| 962 | { .val = 1, .div = 12, }, |
| 963 | { .val = 2, .div = 18, }, |
| 964 | { .val = 3, .div = 24, }, |
| 965 | { } /* sentinel */ |
| 966 | }; |
| 967 | |
| 968 | static const struct divs_data pll5_divs_data __initconst = { |
| 969 | .factors = &sun4i_pll5_data, |
| 970 | .div = { |
| 971 | { .shift = 0, .pow = 0, }, /* M, DDR */ |
| 972 | { .shift = 16, .pow = 1, }, /* P, other */ |
| 973 | } |
| 974 | }; |
| 975 | |
| 976 | static const struct divs_data pll6_divs_data __initconst = { |
Chen-Yu Tsai | 667f542 | 2014-02-03 09:51:39 +0800 | [diff] [blame] | 977 | .factors = &sun4i_pll6_data, |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 978 | .div = { |
| 979 | { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */ |
| 980 | { .fixed = 2 }, /* P, other */ |
| 981 | } |
| 982 | }; |
| 983 | |
| 984 | /** |
| 985 | * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks |
| 986 | * |
| 987 | * These clocks look something like this |
| 988 | * ________________________ |
| 989 | * | ___divisor 1---|----> to consumer |
| 990 | * parent >--| pll___/___divisor 2---|----> to consumer |
| 991 | * | \_______________|____> to consumer |
| 992 | * |________________________| |
| 993 | */ |
| 994 | |
| 995 | static void __init sunxi_divs_clk_setup(struct device_node *node, |
| 996 | struct divs_data *data) |
| 997 | { |
| 998 | struct clk_onecell_data *clk_data; |
Chen-Yu Tsai | 97e36b3 | 2014-02-03 09:51:40 +0800 | [diff] [blame] | 999 | const char *parent; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1000 | const char *clk_name; |
| 1001 | struct clk **clks, *pclk; |
| 1002 | struct clk_hw *gate_hw, *rate_hw; |
| 1003 | const struct clk_ops *rate_ops; |
| 1004 | struct clk_gate *gate = NULL; |
| 1005 | struct clk_fixed_factor *fix_factor; |
| 1006 | struct clk_divider *divider; |
Emilio López | 89a9456 | 2014-07-28 00:49:42 -0300 | [diff] [blame] | 1007 | void __iomem *reg; |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1008 | int i = 0; |
| 1009 | int flags, clkflags; |
| 1010 | |
| 1011 | /* Set up factor clock that we will be dividing */ |
| 1012 | pclk = sunxi_factors_clk_setup(node, data->factors); |
Chen-Yu Tsai | 97e36b3 | 2014-02-03 09:51:40 +0800 | [diff] [blame] | 1013 | parent = __clk_get_name(pclk); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1014 | |
| 1015 | reg = of_iomap(node, 0); |
| 1016 | |
| 1017 | clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL); |
| 1018 | if (!clk_data) |
| 1019 | return; |
| 1020 | |
Emilio López | d193368 | 2014-01-24 22:32:41 -0300 | [diff] [blame] | 1021 | clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL); |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1022 | if (!clks) |
| 1023 | goto free_clkdata; |
| 1024 | |
| 1025 | clk_data->clks = clks; |
| 1026 | |
| 1027 | /* It's not a good idea to have automatic reparenting changing |
| 1028 | * our RAM clock! */ |
| 1029 | clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT; |
| 1030 | |
| 1031 | for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) { |
| 1032 | if (of_property_read_string_index(node, "clock-output-names", |
| 1033 | i, &clk_name) != 0) |
| 1034 | break; |
| 1035 | |
| 1036 | gate_hw = NULL; |
| 1037 | rate_hw = NULL; |
| 1038 | rate_ops = NULL; |
| 1039 | |
| 1040 | /* If this leaf clock can be gated, create a gate */ |
| 1041 | if (data->div[i].gate) { |
| 1042 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 1043 | if (!gate) |
| 1044 | goto free_clks; |
| 1045 | |
| 1046 | gate->reg = reg; |
| 1047 | gate->bit_idx = data->div[i].gate; |
| 1048 | gate->lock = &clk_lock; |
| 1049 | |
| 1050 | gate_hw = &gate->hw; |
| 1051 | } |
| 1052 | |
| 1053 | /* Leaves can be fixed or configurable divisors */ |
| 1054 | if (data->div[i].fixed) { |
| 1055 | fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL); |
| 1056 | if (!fix_factor) |
| 1057 | goto free_gate; |
| 1058 | |
| 1059 | fix_factor->mult = 1; |
| 1060 | fix_factor->div = data->div[i].fixed; |
| 1061 | |
| 1062 | rate_hw = &fix_factor->hw; |
| 1063 | rate_ops = &clk_fixed_factor_ops; |
| 1064 | } else { |
| 1065 | divider = kzalloc(sizeof(*divider), GFP_KERNEL); |
| 1066 | if (!divider) |
| 1067 | goto free_gate; |
| 1068 | |
| 1069 | flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0; |
| 1070 | |
| 1071 | divider->reg = reg; |
| 1072 | divider->shift = data->div[i].shift; |
| 1073 | divider->width = SUNXI_DIVISOR_WIDTH; |
| 1074 | divider->flags = flags; |
| 1075 | divider->lock = &clk_lock; |
| 1076 | divider->table = data->div[i].table; |
| 1077 | |
| 1078 | rate_hw = ÷r->hw; |
| 1079 | rate_ops = &clk_divider_ops; |
| 1080 | } |
| 1081 | |
| 1082 | /* Wrap the (potential) gate and the divisor on a composite |
| 1083 | * clock to unify them */ |
| 1084 | clks[i] = clk_register_composite(NULL, clk_name, &parent, 1, |
| 1085 | NULL, NULL, |
| 1086 | rate_hw, rate_ops, |
| 1087 | gate_hw, &clk_gate_ops, |
| 1088 | clkflags); |
| 1089 | |
| 1090 | WARN_ON(IS_ERR(clk_data->clks[i])); |
| 1091 | clk_register_clkdev(clks[i], clk_name, NULL); |
| 1092 | } |
| 1093 | |
| 1094 | /* The last clock available on the getter is the parent */ |
| 1095 | clks[i++] = pclk; |
| 1096 | |
| 1097 | /* Adjust to the real max */ |
| 1098 | clk_data->clk_num = i; |
| 1099 | |
| 1100 | of_clk_add_provider(node, of_clk_src_onecell_get, clk_data); |
| 1101 | |
| 1102 | return; |
| 1103 | |
| 1104 | free_gate: |
| 1105 | kfree(gate); |
| 1106 | free_clks: |
| 1107 | kfree(clks); |
| 1108 | free_clkdata: |
| 1109 | kfree(clk_data); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1114 | /* Matches for factors clocks */ |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 1115 | static const struct of_device_id clk_factors_match[] __initconst = { |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1116 | {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1117 | {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,}, |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1118 | {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,}, |
Emilio López | 5a8ddf2 | 2014-03-19 15:19:30 -0300 | [diff] [blame] | 1119 | {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,}, |
Maxime Ripard | 92ef67c | 2014-02-05 14:05:03 +0100 | [diff] [blame] | 1120 | {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,}, |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1121 | {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,}, |
Maxime Ripard | 03e29bb | 2014-07-10 23:53:40 +0200 | [diff] [blame^] | 1122 | {.compatible = "allwinner,sun5i-a13-mbus-clk", .data = &sun4i_mod0_data,}, |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1123 | {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,}, |
Chen-Yu Tsai | 6f86341 | 2013-12-24 21:26:17 +0800 | [diff] [blame] | 1124 | {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1125 | {} |
| 1126 | }; |
| 1127 | |
| 1128 | /* Matches for divider clocks */ |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 1129 | static const struct of_device_id clk_div_match[] __initconst = { |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1130 | {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,}, |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1131 | {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,}, |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1132 | {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,}, |
| 1133 | {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1134 | {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1135 | {} |
| 1136 | }; |
| 1137 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1138 | /* Matches for divided outputs */ |
| 1139 | static const struct of_device_id clk_divs_match[] __initconst = { |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1140 | {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,}, |
| 1141 | {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,}, |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1142 | {} |
| 1143 | }; |
| 1144 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1145 | /* Matches for mux clocks */ |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 1146 | static const struct of_device_id clk_mux_match[] __initconst = { |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1147 | {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,}, |
| 1148 | {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1149 | {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,}, |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1150 | {} |
| 1151 | }; |
| 1152 | |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 1153 | /* Matches for gate clocks */ |
Sachin Kamat | 52be7cc | 2013-08-12 14:44:06 +0530 | [diff] [blame] | 1154 | static const struct of_device_id clk_gates_match[] __initconst = { |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1155 | {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,}, |
| 1156 | {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 1157 | {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 1158 | {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1159 | {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,}, |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 1160 | {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,}, |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1161 | {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,}, |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1162 | {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 1163 | {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 1164 | {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,}, |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 1165 | {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,}, |
Maxime Ripard | fd1b22f | 2014-02-06 09:55:57 +0100 | [diff] [blame] | 1166 | {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,}, |
Maxime Ripard | 2371dd8 | 2013-07-16 11:21:59 +0200 | [diff] [blame] | 1167 | {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,}, |
Maxime Ripard | 4f985b4 | 2013-04-30 11:56:22 +0200 | [diff] [blame] | 1168 | {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1169 | {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,}, |
Maxime Ripard | 1fb2e4a | 2013-07-25 21:06:56 +0200 | [diff] [blame] | 1170 | {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,}, |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1171 | {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,}, |
Maxime Ripard | 6a721db | 2013-07-23 23:34:10 +0200 | [diff] [blame] | 1172 | {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,}, |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1173 | {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,}, |
Roman Byshko | 5abdbf2 | 2014-02-07 16:21:50 +0100 | [diff] [blame] | 1174 | {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,}, |
| 1175 | {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,}, |
Maxime Ripard | e0e7943 | 2014-05-13 17:44:15 +0200 | [diff] [blame] | 1176 | {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,}, |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 1177 | {} |
| 1178 | }; |
| 1179 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1180 | static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match, |
| 1181 | void *function) |
| 1182 | { |
| 1183 | struct device_node *np; |
| 1184 | const struct div_data *data; |
| 1185 | const struct of_device_id *match; |
| 1186 | void (*setup_function)(struct device_node *, const void *) = function; |
| 1187 | |
Rob Herring | cb7d5f4 | 2014-05-12 11:24:31 -0500 | [diff] [blame] | 1188 | for_each_matching_node_and_match(np, clk_match, &match) { |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1189 | data = match->data; |
| 1190 | setup_function(np, data); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1194 | static void __init sunxi_init_clocks(const char *clocks[], int nclocks) |
Emilio López | 8e6a4c4 | 2013-09-20 22:03:12 -0300 | [diff] [blame] | 1195 | { |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1196 | unsigned int i; |
Emilio López | 8e6a4c4 | 2013-09-20 22:03:12 -0300 | [diff] [blame] | 1197 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1198 | /* Register factor clocks */ |
| 1199 | of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup); |
| 1200 | |
| 1201 | /* Register divider clocks */ |
| 1202 | of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup); |
| 1203 | |
Emilio López | d584c13 | 2013-12-23 00:32:37 -0300 | [diff] [blame] | 1204 | /* Register divided output clocks */ |
| 1205 | of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup); |
| 1206 | |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1207 | /* Register mux clocks */ |
| 1208 | of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup); |
Emilio López | 13569a7 | 2013-03-27 18:20:37 -0300 | [diff] [blame] | 1209 | |
| 1210 | /* Register gate clocks */ |
| 1211 | of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup); |
Emilio López | 8e6a4c4 | 2013-09-20 22:03:12 -0300 | [diff] [blame] | 1212 | |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1213 | /* Protect the clocks that needs to stay on */ |
| 1214 | for (i = 0; i < nclocks; i++) { |
| 1215 | struct clk *clk = clk_get(NULL, clocks[i]); |
| 1216 | |
| 1217 | if (!IS_ERR(clk)) |
| 1218 | clk_prepare_enable(clk); |
| 1219 | } |
Emilio López | e874a66 | 2013-02-25 11:44:26 -0300 | [diff] [blame] | 1220 | } |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1221 | |
| 1222 | static const char *sun4i_a10_critical_clocks[] __initdata = { |
| 1223 | "pll5_ddr", |
Chen-Yu Tsai | 70eab19 | 2014-06-26 23:55:40 +0800 | [diff] [blame] | 1224 | "ahb_sdram", |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1225 | }; |
| 1226 | |
| 1227 | static void __init sun4i_a10_init_clocks(struct device_node *node) |
| 1228 | { |
| 1229 | sunxi_init_clocks(sun4i_a10_critical_clocks, |
| 1230 | ARRAY_SIZE(sun4i_a10_critical_clocks)); |
| 1231 | } |
| 1232 | CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks); |
| 1233 | |
| 1234 | static const char *sun5i_critical_clocks[] __initdata = { |
| 1235 | "mbus", |
| 1236 | "pll5_ddr", |
Chen-Yu Tsai | 70eab19 | 2014-06-26 23:55:40 +0800 | [diff] [blame] | 1237 | "ahb_sdram", |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1238 | }; |
| 1239 | |
| 1240 | static void __init sun5i_init_clocks(struct device_node *node) |
| 1241 | { |
| 1242 | sunxi_init_clocks(sun5i_critical_clocks, |
| 1243 | ARRAY_SIZE(sun5i_critical_clocks)); |
| 1244 | } |
| 1245 | CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks); |
| 1246 | CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks); |
| 1247 | CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks); |
| 1248 | |
| 1249 | static const char *sun6i_critical_clocks[] __initdata = { |
Maxime Ripard | 2df73f4 | 2014-05-09 22:33:40 -0500 | [diff] [blame] | 1250 | "cpu", |
Maxime Ripard | efb3184 | 2014-05-09 22:33:41 -0500 | [diff] [blame] | 1251 | "ahb1_sdram", |
Maxime Ripard | 134a669 | 2014-05-09 22:33:39 -0500 | [diff] [blame] | 1252 | }; |
| 1253 | |
| 1254 | static void __init sun6i_init_clocks(struct device_node *node) |
| 1255 | { |
| 1256 | sunxi_init_clocks(sun6i_critical_clocks, |
| 1257 | ARRAY_SIZE(sun6i_critical_clocks)); |
| 1258 | } |
| 1259 | CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks); |
Chen-Yu Tsai | 515c1a4 | 2014-06-26 23:55:43 +0800 | [diff] [blame] | 1260 | CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks); |