Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Analog Devices Inc. |
| 3 | * Licensed under the GPL-2 or later. |
| 4 | */ |
| 5 | |
| 6 | #ifndef __CLOCK_H__ |
| 7 | #define __CLOCK_H__ |
| 8 | |
| 9 | #include <asm/blackfin.h> |
| 10 | #ifdef PLL_CTL |
| 11 | #include <asm/mach-common/bits/pll.h> |
Sonic Zhang | ab80b65 | 2012-11-30 17:39:32 +0800 | [diff] [blame] | 12 | # define pll_is_bypassed() (bfin_read_PLL_CTL() & BYPASS) |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 13 | #else |
| 14 | #include <asm/mach-common/bits/cgu.h> |
| 15 | # define pll_is_bypassed() (bfin_read_CGU_STAT() & PLLBP) |
| 16 | # define bfin_read_PLL_CTL() bfin_read_CGU_CTL() |
| 17 | # define bfin_read_PLL_DIV() bfin_read_CGU_DIV() |
| 18 | # define SSEL SYSSEL |
| 19 | # define SSEL_P SYSSEL_P |
| 20 | #endif |
| 21 | |
| 22 | __attribute__((always_inline)) |
| 23 | static inline uint32_t early_division(uint32_t dividend, uint32_t divisor) |
| 24 | { |
| 25 | uint32_t quotient; |
| 26 | uint32_t i, j; |
| 27 | |
| 28 | for (quotient = 1, i = 1; dividend > divisor; ++i) { |
| 29 | j = divisor << i; |
| 30 | if (j > dividend || (j & 0x80000000)) { |
| 31 | --i; |
| 32 | quotient += (1 << i); |
| 33 | dividend -= (divisor << i); |
| 34 | i = 0; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return quotient; |
| 39 | } |
| 40 | |
| 41 | __attribute__((always_inline)) |
| 42 | static inline uint32_t early_get_uart_clk(void) |
| 43 | { |
| 44 | uint32_t msel, pll_ctl, vco; |
| 45 | uint32_t div, ssel, sclk, uclk; |
| 46 | |
| 47 | pll_ctl = bfin_read_PLL_CTL(); |
| 48 | msel = (pll_ctl & MSEL) >> MSEL_P; |
| 49 | if (msel == 0) |
| 50 | msel = (MSEL >> MSEL_P) + 1; |
| 51 | |
| 52 | vco = (CONFIG_CLKIN_HZ >> (pll_ctl & DF)) * msel; |
| 53 | sclk = vco; |
| 54 | if (!pll_is_bypassed()) { |
| 55 | div = bfin_read_PLL_DIV(); |
| 56 | ssel = (div & SSEL) >> SSEL_P; |
Sonic Zhang | ab80b65 | 2012-11-30 17:39:32 +0800 | [diff] [blame] | 57 | #if CONFIG_BFIN_BOOT_MODE == BFIN_BOOT_BYPASS |
| 58 | sclk = vco/ssel; |
| 59 | #else |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 60 | sclk = early_division(vco, ssel); |
Sonic Zhang | ab80b65 | 2012-11-30 17:39:32 +0800 | [diff] [blame] | 61 | #endif |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 62 | } |
| 63 | uclk = sclk; |
| 64 | #ifdef CGU_DIV |
| 65 | ssel = (div & S0SEL) >> S0SEL_P; |
| 66 | uclk = early_division(sclk, ssel); |
| 67 | #endif |
| 68 | return uclk; |
| 69 | } |
| 70 | |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame^] | 71 | extern u_long get_vco(void); |
| 72 | extern u_long get_cclk(void); |
| 73 | extern u_long get_sclk(void); |
| 74 | |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 75 | #ifdef CGU_DIV |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame^] | 76 | extern u_long get_sclk0(void); |
| 77 | extern u_long get_sclk1(void); |
| 78 | extern u_long get_dclk(void); |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 79 | # define get_uart_clk get_sclk0 |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame^] | 80 | # define get_i2c_clk get_sclk0 |
| 81 | # define get_spi_clk get_sclk0 |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 82 | #else |
| 83 | # define get_uart_clk get_sclk |
Sonic Zhang | d6a320d | 2014-01-28 13:53:34 +0800 | [diff] [blame^] | 84 | # define get_i2c_clk get_sclk |
| 85 | # define get_spi_clk get_sclk |
Sonic Zhang | 79f2b39 | 2013-02-05 19:10:34 +0800 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | #endif |