blob: 13a2ca23a15970b7e9042c3e0f322203c1dedde1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Kelvin Cheung5175cb52012-08-20 18:05:35 +08002/*
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +08003 * Copyright (c) 2012-2016 Zhang, Keguang <keguang.zhang@gmail.com>
Kelvin Cheung5175cb52012-08-20 18:05:35 +08004 */
5
6#include <linux/clkdev.h>
7#include <linux/clk-provider.h>
8#include <linux/io.h>
Kelvin Cheung5175cb52012-08-20 18:05:35 +08009#include <linux/err.h>
10
11#include <loongson1.h>
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +080012#include "clk.h"
Kelvin Cheung5175cb52012-08-20 18:05:35 +080013
Kelvin Cheung3526f742014-10-10 11:42:51 +080014#define OSC (33 * 1000000)
15#define DIV_APB 2
Kelvin Cheung5175cb52012-08-20 18:05:35 +080016
17static DEFINE_SPINLOCK(_lock);
18
Kelvin Cheung5175cb52012-08-20 18:05:35 +080019static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
Kelvin Cheung3526f742014-10-10 11:42:51 +080020 unsigned long parent_rate)
Kelvin Cheung5175cb52012-08-20 18:05:35 +080021{
22 u32 pll, rate;
23
24 pll = __raw_readl(LS1X_CLK_PLL_FREQ);
Kelvin Cheungf0ffaf12016-09-19 12:38:56 +080025 rate = 12 + (pll & GENMASK(5, 0));
Kelvin Cheung5175cb52012-08-20 18:05:35 +080026 rate *= OSC;
27 rate >>= 1;
28
29 return rate;
30}
31
32static const struct clk_ops ls1x_pll_clk_ops = {
Kelvin Cheung5175cb52012-08-20 18:05:35 +080033 .recalc_rate = ls1x_pll_recalc_rate,
34};
35
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080036static const char *const cpu_parents[] = { "cpu_clk_div", "osc_clk", };
37static const char *const ahb_parents[] = { "ahb_clk_div", "osc_clk", };
38static const char *const dc_parents[] = { "dc_clk_div", "osc_clk", };
Kelvin Cheung3526f742014-10-10 11:42:51 +080039
Kelvin Cheung5175cb52012-08-20 18:05:35 +080040void __init ls1x_clk_init(void)
41{
Stephen Boyd944b9a42016-06-01 16:15:15 -070042 struct clk_hw *hw;
Kelvin Cheung5175cb52012-08-20 18:05:35 +080043
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080044 hw = clk_hw_register_fixed_rate(NULL, "osc_clk", NULL, 0, OSC);
45 clk_hw_register_clkdev(hw, "osc_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080046
Kelvin Cheung3526f742014-10-10 11:42:51 +080047 /* clock derived from 33 MHz OSC clk */
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080048 hw = clk_hw_register_pll(NULL, "pll_clk", "osc_clk",
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +080049 &ls1x_pll_clk_ops, 0);
Stephen Boyd944b9a42016-06-01 16:15:15 -070050 clk_hw_register_clkdev(hw, "pll_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080051
Kelvin Cheung3526f742014-10-10 11:42:51 +080052 /* clock derived from PLL clk */
53 /* _____
54 * _______________________| |
55 * OSC ___/ | MUX |___ CPU CLK
56 * \___ PLL ___ CPU DIV ___| |
57 * |_____|
58 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070059 hw = clk_hw_register_divider(NULL, "cpu_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080060 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 Boyd944b9a42016-06-01 16:15:15 -070064 clk_hw_register_clkdev(hw, "cpu_clk_div", NULL);
65 hw = clk_hw_register_mux(NULL, "cpu_clk", cpu_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +080066 ARRAY_SIZE(cpu_parents),
67 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
68 BYPASS_CPU_SHIFT, BYPASS_CPU_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070069 clk_hw_register_clkdev(hw, "cpu_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080070
Kelvin Cheung3526f742014-10-10 11:42:51 +080071 /* _____
72 * _______________________| |
73 * OSC ___/ | MUX |___ DC CLK
74 * \___ PLL ___ DC DIV ___| |
75 * |_____|
76 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070077 hw = clk_hw_register_divider(NULL, "dc_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080078 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
79 DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070080 clk_hw_register_clkdev(hw, "dc_clk_div", NULL);
81 hw = clk_hw_register_mux(NULL, "dc_clk", dc_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +080082 ARRAY_SIZE(dc_parents),
83 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
84 BYPASS_DC_SHIFT, BYPASS_DC_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070085 clk_hw_register_clkdev(hw, "dc_clk", NULL);
Kelvin Cheung3526f742014-10-10 11:42:51 +080086
87 /* _____
88 * _______________________| |
89 * OSC ___/ | MUX |___ DDR CLK
90 * \___ PLL ___ DDR DIV ___| |
91 * |_____|
92 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070093 hw = clk_hw_register_divider(NULL, "ahb_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080094 0, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT,
95 DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED,
96 &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070097 clk_hw_register_clkdev(hw, "ahb_clk_div", NULL);
98 hw = clk_hw_register_mux(NULL, "ahb_clk", ahb_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +080099 ARRAY_SIZE(ahb_parents),
100 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
101 BYPASS_DDR_SHIFT, BYPASS_DDR_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700102 clk_hw_register_clkdev(hw, "ahb_clk", NULL);
Kelvin Cheungc99c7a92016-09-19 12:38:55 +0800103 clk_hw_register_clkdev(hw, "ls1x-dma", NULL);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700104 clk_hw_register_clkdev(hw, "stmmaceth", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +0800105
Kelvin Cheung3526f742014-10-10 11:42:51 +0800106 /* clock derived from AHB clk */
107 /* APB clk is always half of the AHB clk */
Stephen Boyd944b9a42016-06-01 16:15:15 -0700108 hw = clk_hw_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1,
Kelvin Cheung3526f742014-10-10 11:42:51 +0800109 DIV_APB);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700110 clk_hw_register_clkdev(hw, "apb_clk", NULL);
Kelvin Cheungc99c7a92016-09-19 12:38:55 +0800111 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 Boyd944b9a42016-06-01 16:15:15 -0700117 clk_hw_register_clkdev(hw, "serial8250", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +0800118}