blob: 5e8fb9952895421bcb6156e88889dc0346d86e78 [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>
12#include <dt-bindings/clock/microchip-mpfs-clock.h>
13#include <linux/err.h>
14
15#include "mpfs_clk.h"
16
17#define MPFS_CFG_CLOCK "mpfs_cfg_clock"
18
19#define REG_CLOCK_CONFIG_CR 0x08
20
21/* CPU and AXI clock divisors */
22static const struct clk_div_table mpfs_div_cpu_axi_table[] = {
23 { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 },
24 { 0, 0 }
25};
26
27/* AHB clock divisors */
28static const struct clk_div_table mpfs_div_ahb_table[] = {
29 { 1, 2 }, { 2, 4}, { 3, 8 },
30 { 0, 0 }
31};
32
33/**
34 * struct mpfs_cfg_clock - per instance of configuration clock
35 * @id: index of a configuration clock
36 * @name: name of a configuration clock
37 * @shift: shift to the divider bit field of a configuration clock
38 * @width: width of the divider bit field of a configation clock
39 * @table: clock divider table instance
40 * @flags: common clock framework flags
41 */
42struct mpfs_cfg_clock {
43 unsigned int id;
44 const char *name;
45 u8 shift;
46 u8 width;
47 const struct clk_div_table *table;
48 unsigned long flags;
49};
50
51/**
52 * struct mpfs_cfg_hw_clock - hardware configuration clock (cpu, axi, ahb)
53 * @cfg: configuration clock instance
54 * @sys_base: base address of the mpfs system register
55 * @prate: the pll clock rate
56 * @hw: clock instance
57 */
58struct mpfs_cfg_hw_clock {
59 struct mpfs_cfg_clock cfg;
60 void __iomem *sys_base;
61 u32 prate;
62 struct clk hw;
63};
64
65#define to_mpfs_cfg_clk(_hw) container_of(_hw, struct mpfs_cfg_hw_clock, hw)
66
67static ulong mpfs_cfg_clk_recalc_rate(struct clk *hw)
68{
69 struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
70 struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
71 void __iomem *base_addr = cfg_hw->sys_base;
72 unsigned long rate;
73 u32 val;
74
75 val = readl(base_addr + REG_CLOCK_CONFIG_CR) >> cfg->shift;
76 val &= clk_div_mask(cfg->width);
77 rate = cfg_hw->prate / (1u << val);
78 hw->rate = rate;
79
80 return rate;
81}
82
83static ulong mpfs_cfg_clk_set_rate(struct clk *hw, ulong rate)
84{
85 struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw);
86 struct mpfs_cfg_clock *cfg = &cfg_hw->cfg;
87 void __iomem *base_addr = cfg_hw->sys_base;
88 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
96 val = readl(base_addr + REG_CLOCK_CONFIG_CR);
97 val &= ~(clk_div_mask(cfg->width) << cfg_hw->cfg.shift);
98 val |= divider_setting << cfg->shift;
99 writel(val, base_addr + REG_CLOCK_CONFIG_CR);
100
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 Dooley4a182e02022-10-25 08:58:45 +0100119int mpfs_clk_register_cfgs(void __iomem *base, struct clk *parent)
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;
129 mpfs_cfg_clks[i].sys_base = base;
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};