Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 2 | /* |
Kelvin Cheung | a8e3ced | 2016-09-19 12:38:54 +0800 | [diff] [blame] | 3 | * Copyright (c) 2012-2016 Zhang, Keguang <keguang.zhang@gmail.com> |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clkdev.h> |
| 7 | #include <linux/clk-provider.h> |
| 8 | #include <linux/io.h> |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 9 | #include <linux/err.h> |
| 10 | |
| 11 | #include <loongson1.h> |
Kelvin Cheung | a8e3ced | 2016-09-19 12:38:54 +0800 | [diff] [blame] | 12 | #include "clk.h" |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 13 | |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 14 | #define OSC (33 * 1000000) |
| 15 | #define DIV_APB 2 |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 16 | |
| 17 | static DEFINE_SPINLOCK(_lock); |
| 18 | |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 19 | static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw, |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 20 | unsigned long parent_rate) |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 21 | { |
| 22 | u32 pll, rate; |
| 23 | |
| 24 | pll = __raw_readl(LS1X_CLK_PLL_FREQ); |
Kelvin Cheung | f0ffaf1 | 2016-09-19 12:38:56 +0800 | [diff] [blame] | 25 | rate = 12 + (pll & GENMASK(5, 0)); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 26 | rate *= OSC; |
| 27 | rate >>= 1; |
| 28 | |
| 29 | return rate; |
| 30 | } |
| 31 | |
| 32 | static const struct clk_ops ls1x_pll_clk_ops = { |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 33 | .recalc_rate = ls1x_pll_recalc_rate, |
| 34 | }; |
| 35 | |
Kelvin Cheung | c99c7a9 | 2016-09-19 12:38:55 +0800 | [diff] [blame] | 36 | static const char *const cpu_parents[] = { "cpu_clk_div", "osc_clk", }; |
| 37 | static const char *const ahb_parents[] = { "ahb_clk_div", "osc_clk", }; |
| 38 | static const char *const dc_parents[] = { "dc_clk_div", "osc_clk", }; |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 39 | |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 40 | void __init ls1x_clk_init(void) |
| 41 | { |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 42 | struct clk_hw *hw; |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 43 | |
Kelvin Cheung | c99c7a9 | 2016-09-19 12:38:55 +0800 | [diff] [blame] | 44 | hw = clk_hw_register_fixed_rate(NULL, "osc_clk", NULL, 0, OSC); |
| 45 | clk_hw_register_clkdev(hw, "osc_clk", NULL); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 46 | |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 47 | /* clock derived from 33 MHz OSC clk */ |
Kelvin Cheung | c99c7a9 | 2016-09-19 12:38:55 +0800 | [diff] [blame] | 48 | hw = clk_hw_register_pll(NULL, "pll_clk", "osc_clk", |
Kelvin Cheung | a8e3ced | 2016-09-19 12:38:54 +0800 | [diff] [blame] | 49 | &ls1x_pll_clk_ops, 0); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 50 | clk_hw_register_clkdev(hw, "pll_clk", NULL); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 51 | |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 52 | /* clock derived from PLL clk */ |
| 53 | /* _____ |
| 54 | * _______________________| | |
| 55 | * OSC ___/ | MUX |___ CPU CLK |
| 56 | * \___ PLL ___ CPU DIV ___| | |
| 57 | * |_____| |
| 58 | */ |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 59 | hw = clk_hw_register_divider(NULL, "cpu_clk_div", "pll_clk", |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 60 | CLK_GET_RATE_NOCACHE, LS1X_CLK_PLL_DIV, |
| 61 | DIV_CPU_SHIFT, DIV_CPU_WIDTH, |
| 62 | CLK_DIVIDER_ONE_BASED | |
| 63 | CLK_DIVIDER_ROUND_CLOSEST, &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 64 | clk_hw_register_clkdev(hw, "cpu_clk_div", NULL); |
| 65 | hw = clk_hw_register_mux(NULL, "cpu_clk", cpu_parents, |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 66 | ARRAY_SIZE(cpu_parents), |
| 67 | CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV, |
| 68 | BYPASS_CPU_SHIFT, BYPASS_CPU_WIDTH, 0, &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 69 | clk_hw_register_clkdev(hw, "cpu_clk", NULL); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 70 | |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 71 | /* _____ |
| 72 | * _______________________| | |
| 73 | * OSC ___/ | MUX |___ DC CLK |
| 74 | * \___ PLL ___ DC DIV ___| | |
| 75 | * |_____| |
| 76 | */ |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 77 | hw = clk_hw_register_divider(NULL, "dc_clk_div", "pll_clk", |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 78 | 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT, |
| 79 | DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 80 | clk_hw_register_clkdev(hw, "dc_clk_div", NULL); |
| 81 | hw = clk_hw_register_mux(NULL, "dc_clk", dc_parents, |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 82 | ARRAY_SIZE(dc_parents), |
| 83 | CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV, |
| 84 | BYPASS_DC_SHIFT, BYPASS_DC_WIDTH, 0, &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 85 | clk_hw_register_clkdev(hw, "dc_clk", NULL); |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 86 | |
| 87 | /* _____ |
| 88 | * _______________________| | |
| 89 | * OSC ___/ | MUX |___ DDR CLK |
| 90 | * \___ PLL ___ DDR DIV ___| | |
| 91 | * |_____| |
| 92 | */ |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 93 | hw = clk_hw_register_divider(NULL, "ahb_clk_div", "pll_clk", |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 94 | 0, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT, |
| 95 | DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED, |
| 96 | &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 97 | clk_hw_register_clkdev(hw, "ahb_clk_div", NULL); |
| 98 | hw = clk_hw_register_mux(NULL, "ahb_clk", ahb_parents, |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 99 | ARRAY_SIZE(ahb_parents), |
| 100 | CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV, |
| 101 | BYPASS_DDR_SHIFT, BYPASS_DDR_WIDTH, 0, &_lock); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 102 | clk_hw_register_clkdev(hw, "ahb_clk", NULL); |
Kelvin Cheung | c99c7a9 | 2016-09-19 12:38:55 +0800 | [diff] [blame] | 103 | clk_hw_register_clkdev(hw, "ls1x-dma", NULL); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 104 | clk_hw_register_clkdev(hw, "stmmaceth", NULL); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 105 | |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 106 | /* clock derived from AHB clk */ |
| 107 | /* APB clk is always half of the AHB clk */ |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 108 | hw = clk_hw_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1, |
Kelvin Cheung | 3526f74 | 2014-10-10 11:42:51 +0800 | [diff] [blame] | 109 | DIV_APB); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 110 | clk_hw_register_clkdev(hw, "apb_clk", NULL); |
Kelvin Cheung | c99c7a9 | 2016-09-19 12:38:55 +0800 | [diff] [blame] | 111 | clk_hw_register_clkdev(hw, "ls1x-ac97", NULL); |
| 112 | clk_hw_register_clkdev(hw, "ls1x-i2c", NULL); |
| 113 | clk_hw_register_clkdev(hw, "ls1x-nand", NULL); |
| 114 | clk_hw_register_clkdev(hw, "ls1x-pwmtimer", NULL); |
| 115 | clk_hw_register_clkdev(hw, "ls1x-spi", NULL); |
| 116 | clk_hw_register_clkdev(hw, "ls1x-wdt", NULL); |
Stephen Boyd | 944b9a4 | 2016-06-01 16:15:15 -0700 | [diff] [blame] | 117 | clk_hw_register_clkdev(hw, "serial8250", NULL); |
Kelvin Cheung | 5175cb5 | 2012-08-20 18:05:35 +0800 | [diff] [blame] | 118 | } |