blob: 7da1fc77120b63d569ee0490a72e1616dd6e8f74 [file] [log] [blame]
Padmarao Begari0c4ae802021-01-15 08:20:38 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Microchip Technology Inc.
4 * Padmarao Begari <padmarao.begari@microchip.com>
5 */
Padmarao Begari0c4ae802021-01-15 08:20:38 +05306#include <clk.h>
7#include <clk-uclass.h>
8#include <asm/io.h>
9#include <dm/device.h>
10#include <dm/devres.h>
11#include <dm/uclass.h>
Conor Dooley54713d62024-10-23 11:17:52 +010012#include <regmap.h>
Padmarao Begari0c4ae802021-01-15 08:20:38 +053013#include <dt-bindings/clock/microchip-mpfs-clock.h>
14#include <linux/err.h>
15
16#include "mpfs_clk.h"
17
18#define MPFS_CFG_CLOCK "mpfs_cfg_clock"
19
20#define REG_CLOCK_CONFIG_CR 0x08
21
22/* CPU and AXI clock divisors */
23static const struct clk_div_table mpfs_div_cpu_axi_table[] = {
24 { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
25 { 0, 0 }
26};
27
28/* AHB clock divisors */
29static const struct clk_div_table mpfs_div_ahb_table[] = {
30 { 1, 2 }, { 2, 4}, { 3, 8 },
31 { 0, 0 }
32};
33
34/**
35 * struct mpfs_cfg_clock - per instance of configuration clock
36 * @id: index of a configuration clock
37 * @name: name of a configuration clock
38 * @shift: shift to the divider bit field of a configuration clock
39 * @width: width of the divider bit field of a configation clock
40 * @table: clock divider table instance
41 * @flags: common clock framework flags
42 */
43struct mpfs_cfg_clock {
44 unsigned int id;
45 const char *name;
46 u8 shift;
47 u8 width;
48 const struct clk_div_table *table;
49 unsigned long flags;
50};
51
52/**
53 * struct mpfs_cfg_hw_clock - hardware configuration clock (cpu, axi, ahb)
54 * @cfg: configuration clock instance
55 * @sys_base: base address of the mpfs system register
56 * @prate: the pll clock rate
57 * @hw: clock instance
58 */
59struct mpfs_cfg_hw_clock {
60 struct mpfs_cfg_clock cfg;
Conor Dooley54713d62024-10-23 11:17:52 +010061 struct regmap *regmap;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053062 u32 prate;
63 struct clk hw;
64};
65
66#define to_mpfs_cfg_clk(_hw) container_of(_hw, struct mpfs_cfg_hw_clock, hw)
67
68static ulong mpfs_cfg_clk_recalc_rate(struct clk *hw)
69{
70 struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
71 struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053072 unsigned long rate;
73 u32 val;
74
Conor Dooley54713d62024-10-23 11:17:52 +010075 regmap_read(cfg_hw->regmap, REG_CLOCK_CONFIG_CR, &val);
76 val >>= cfg->shift;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053077 val &= clk_div_mask(cfg->width);
78 rate = cfg_hw->prate / (1u << val);
79 hw->rate = rate;
80
81 return rate;
82}
83
84static ulong mpfs_cfg_clk_set_rate(struct clk *hw, ulong rate)
85{
86 struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
87 struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053088 u32 val;
89 int divider_setting;
90
91 divider_setting = divider_get_val(rate, cfg_hw->prate, cfg->table, cfg->width, cfg->flags);
92
93 if (divider_setting < 0)
94 return divider_setting;
95
Conor Dooley54713d62024-10-23 11:17:52 +010096 regmap_read(cfg_hw->regmap, REG_CLOCK_CONFIG_CR, &val);
Padmarao Begari0c4ae802021-01-15 08:20:38 +053097 val &= ~(clk_div_mask(cfg->width) << cfg_hw->cfg.shift);
98 val |= divider_setting << cfg->shift;
Conor Dooley54713d62024-10-23 11:17:52 +010099 regmap_write(cfg_hw->regmap, REG_CLOCK_CONFIG_CR, val);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530100
101 return clk_get_rate(hw);
102}
103
104#define CLK_CFG(_id, _name, _shift, _width, _table, _flags) { \
105 .cfg.id = _id, \
106 .cfg.name = _name, \
107 .cfg.shift = _shift, \
108 .cfg.width = _width, \
109 .cfg.table = _table, \
110 .cfg.flags = _flags, \
111 }
112
113static struct mpfs_cfg_hw_clock mpfs_cfg_clks[] = {
114 CLK_CFG(CLK_CPU, "clk_cpu", 0, 2, mpfs_div_cpu_axi_table, 0),
115 CLK_CFG(CLK_AXI, "clk_axi", 2, 2, mpfs_div_cpu_axi_table, 0),
116 CLK_CFG(CLK_AHB, "clk_ahb", 4, 2, mpfs_div_ahb_table, 0),
117};
118
Conor Dooley54713d62024-10-23 11:17:52 +0100119int mpfs_clk_register_cfgs(struct clk *parent, struct regmap *regmap)
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530120{
121 int ret;
122 int i, id, num_clks;
123 const char *name;
124 struct clk *hw;
125
126 num_clks = ARRAY_SIZE(mpfs_cfg_clks);
127 for (i = 0; i < num_clks; i++) {
128 hw = &mpfs_cfg_clks[i].hw;
Conor Dooley54713d62024-10-23 11:17:52 +0100129 mpfs_cfg_clks[i].regmap = regmap;
Conor Dooley4a182e02022-10-25 08:58:45 +0100130 mpfs_cfg_clks[i].prate = clk_get_rate(parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530131 name = mpfs_cfg_clks[i].cfg.name;
Conor Dooley4a182e02022-10-25 08:58:45 +0100132 ret = clk_register(hw, MPFS_CFG_CLOCK, name, parent->dev->name);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530133 if (ret)
134 ERR_PTR(ret);
135 id = mpfs_cfg_clks[i].cfg.id;
136 clk_dm(id, hw);
137 }
138 return 0;
139}
140
141const struct clk_ops mpfs_cfg_clk_ops = {
142 .set_rate = mpfs_cfg_clk_set_rate,
143 .get_rate = mpfs_cfg_clk_recalc_rate,
144};
145
146U_BOOT_DRIVER(mpfs_cfg_clock) = {
147 .name = MPFS_CFG_CLOCK,
148 .id = UCLASS_CLK,
149 .ops = &mpfs_cfg_clk_ops,
150};