blob: f36a97e993c069c2a5344d82231e26f3fd21ff97 [file] [log] [blame]
Kelvin Cheung5175cb52012-08-20 18:05:35 +08001/*
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +08002 * Copyright (c) 2012-2016 Zhang, Keguang <keguang.zhang@gmail.com>
Kelvin Cheung5175cb52012-08-20 18:05:35 +08003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 */
9
10#include <linux/clkdev.h>
11#include <linux/clk-provider.h>
12#include <linux/io.h>
Kelvin Cheung5175cb52012-08-20 18:05:35 +080013#include <linux/err.h>
14
15#include <loongson1.h>
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +080016#include "clk.h"
Kelvin Cheung5175cb52012-08-20 18:05:35 +080017
Kelvin Cheung3526f742014-10-10 11:42:51 +080018#define OSC (33 * 1000000)
19#define DIV_APB 2
Kelvin Cheung5175cb52012-08-20 18:05:35 +080020
21static DEFINE_SPINLOCK(_lock);
22
Kelvin Cheung5175cb52012-08-20 18:05:35 +080023static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw,
Kelvin Cheung3526f742014-10-10 11:42:51 +080024 unsigned long parent_rate)
Kelvin Cheung5175cb52012-08-20 18:05:35 +080025{
26 u32 pll, rate;
27
28 pll = __raw_readl(LS1X_CLK_PLL_FREQ);
Kelvin Cheungf0ffaf12016-09-19 12:38:56 +080029 rate = 12 + (pll & GENMASK(5, 0));
Kelvin Cheung5175cb52012-08-20 18:05:35 +080030 rate *= OSC;
31 rate >>= 1;
32
33 return rate;
34}
35
36static const struct clk_ops ls1x_pll_clk_ops = {
Kelvin Cheung5175cb52012-08-20 18:05:35 +080037 .recalc_rate = ls1x_pll_recalc_rate,
38};
39
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080040static const char *const cpu_parents[] = { "cpu_clk_div", "osc_clk", };
41static const char *const ahb_parents[] = { "ahb_clk_div", "osc_clk", };
42static const char *const dc_parents[] = { "dc_clk_div", "osc_clk", };
Kelvin Cheung3526f742014-10-10 11:42:51 +080043
Kelvin Cheung5175cb52012-08-20 18:05:35 +080044void __init ls1x_clk_init(void)
45{
Stephen Boyd944b9a42016-06-01 16:15:15 -070046 struct clk_hw *hw;
Kelvin Cheung5175cb52012-08-20 18:05:35 +080047
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080048 hw = clk_hw_register_fixed_rate(NULL, "osc_clk", NULL, 0, OSC);
49 clk_hw_register_clkdev(hw, "osc_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080050
Kelvin Cheung3526f742014-10-10 11:42:51 +080051 /* clock derived from 33 MHz OSC clk */
Kelvin Cheungc99c7a92016-09-19 12:38:55 +080052 hw = clk_hw_register_pll(NULL, "pll_clk", "osc_clk",
Kelvin Cheunga8e3ced2016-09-19 12:38:54 +080053 &ls1x_pll_clk_ops, 0);
Stephen Boyd944b9a42016-06-01 16:15:15 -070054 clk_hw_register_clkdev(hw, "pll_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080055
Kelvin Cheung3526f742014-10-10 11:42:51 +080056 /* clock derived from PLL clk */
57 /* _____
58 * _______________________| |
59 * OSC ___/ | MUX |___ CPU CLK
60 * \___ PLL ___ CPU DIV ___| |
61 * |_____|
62 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070063 hw = clk_hw_register_divider(NULL, "cpu_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080064 CLK_GET_RATE_NOCACHE, LS1X_CLK_PLL_DIV,
65 DIV_CPU_SHIFT, DIV_CPU_WIDTH,
66 CLK_DIVIDER_ONE_BASED |
67 CLK_DIVIDER_ROUND_CLOSEST, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070068 clk_hw_register_clkdev(hw, "cpu_clk_div", NULL);
69 hw = clk_hw_register_mux(NULL, "cpu_clk", cpu_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +080070 ARRAY_SIZE(cpu_parents),
71 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
72 BYPASS_CPU_SHIFT, BYPASS_CPU_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070073 clk_hw_register_clkdev(hw, "cpu_clk", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +080074
Kelvin Cheung3526f742014-10-10 11:42:51 +080075 /* _____
76 * _______________________| |
77 * OSC ___/ | MUX |___ DC CLK
78 * \___ PLL ___ DC DIV ___| |
79 * |_____|
80 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070081 hw = clk_hw_register_divider(NULL, "dc_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080082 0, LS1X_CLK_PLL_DIV, DIV_DC_SHIFT,
83 DIV_DC_WIDTH, CLK_DIVIDER_ONE_BASED, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070084 clk_hw_register_clkdev(hw, "dc_clk_div", NULL);
85 hw = clk_hw_register_mux(NULL, "dc_clk", dc_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +080086 ARRAY_SIZE(dc_parents),
87 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
88 BYPASS_DC_SHIFT, BYPASS_DC_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -070089 clk_hw_register_clkdev(hw, "dc_clk", NULL);
Kelvin Cheung3526f742014-10-10 11:42:51 +080090
91 /* _____
92 * _______________________| |
93 * OSC ___/ | MUX |___ DDR CLK
94 * \___ PLL ___ DDR DIV ___| |
95 * |_____|
96 */
Stephen Boyd944b9a42016-06-01 16:15:15 -070097 hw = clk_hw_register_divider(NULL, "ahb_clk_div", "pll_clk",
Kelvin Cheung3526f742014-10-10 11:42:51 +080098 0, LS1X_CLK_PLL_DIV, DIV_DDR_SHIFT,
99 DIV_DDR_WIDTH, CLK_DIVIDER_ONE_BASED,
100 &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700101 clk_hw_register_clkdev(hw, "ahb_clk_div", NULL);
102 hw = clk_hw_register_mux(NULL, "ahb_clk", ahb_parents,
Kelvin Cheung3526f742014-10-10 11:42:51 +0800103 ARRAY_SIZE(ahb_parents),
104 CLK_SET_RATE_NO_REPARENT, LS1X_CLK_PLL_DIV,
105 BYPASS_DDR_SHIFT, BYPASS_DDR_WIDTH, 0, &_lock);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700106 clk_hw_register_clkdev(hw, "ahb_clk", NULL);
Kelvin Cheungc99c7a92016-09-19 12:38:55 +0800107 clk_hw_register_clkdev(hw, "ls1x-dma", NULL);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700108 clk_hw_register_clkdev(hw, "stmmaceth", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +0800109
Kelvin Cheung3526f742014-10-10 11:42:51 +0800110 /* clock derived from AHB clk */
111 /* APB clk is always half of the AHB clk */
Stephen Boyd944b9a42016-06-01 16:15:15 -0700112 hw = clk_hw_register_fixed_factor(NULL, "apb_clk", "ahb_clk", 0, 1,
Kelvin Cheung3526f742014-10-10 11:42:51 +0800113 DIV_APB);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700114 clk_hw_register_clkdev(hw, "apb_clk", NULL);
Kelvin Cheungc99c7a92016-09-19 12:38:55 +0800115 clk_hw_register_clkdev(hw, "ls1x-ac97", NULL);
116 clk_hw_register_clkdev(hw, "ls1x-i2c", NULL);
117 clk_hw_register_clkdev(hw, "ls1x-nand", NULL);
118 clk_hw_register_clkdev(hw, "ls1x-pwmtimer", NULL);
119 clk_hw_register_clkdev(hw, "ls1x-spi", NULL);
120 clk_hw_register_clkdev(hw, "ls1x-wdt", NULL);
Stephen Boyd944b9a42016-06-01 16:15:15 -0700121 clk_hw_register_clkdev(hw, "serial8250", NULL);
Kelvin Cheung5175cb52012-08-20 18:05:35 +0800122}