blob: b734f49d81aa2f19d5b142d74de5bb8644d5e52b [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_PERIPH_CLOCK "mpfs_periph_clock"
19
20#define REG_CLOCK_CONFIG_CR 0x08
21#define REG_SUBBLK_CLOCK_CR 0x84
22#define REG_SUBBLK_RESET_CR 0x88
23
24#define CFG_CPU_SHIFT 0x0
25#define CFG_AXI_SHIFT 0x2
26#define CFG_AHB_SHIFT 0x4
27#define CFG_WIDTH 0x2
28
29/**
30 * struct mpfs_periph_clock - per instance of peripheral clock
31 * @id: index of a peripheral clock
Conor Dooley73a1d602022-10-25 08:58:47 +010032 * @parent_id: index of the parent clock
Padmarao Begari0c4ae802021-01-15 08:20:38 +053033 * @name: name of a peripheral clock
34 * @shift: shift to a peripheral clock bit field
35 * @flags: common clock framework flags
36 */
37struct mpfs_periph_clock {
38 unsigned int id;
Conor Dooley73a1d602022-10-25 08:58:47 +010039 unsigned int parent_id;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053040 const char *name;
41 u8 shift;
42 unsigned long flags;
43};
44
45/**
46 * struct mpfs_periph_hw_clock - hardware peripheral clock
47 * @periph: peripheral clock instance
48 * @sys_base: base address of the mpfs system register
49 * @prate: the pll clock rate
50 * @hw: clock instance
51 */
52struct mpfs_periph_hw_clock {
53 struct mpfs_periph_clock periph;
Conor Dooley54713d62024-10-23 11:17:52 +010054 struct regmap *regmap;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053055 u32 prate;
56 struct clk hw;
57};
58
59#define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw)
60
61static int mpfs_periph_clk_enable(struct clk *hw)
62{
63 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
64 struct mpfs_periph_clock *periph = &periph_hw->periph;
Conor Dooley54713d62024-10-23 11:17:52 +010065 u32 reg;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053066
67 if (periph->flags != CLK_IS_CRITICAL) {
Conor Dooley54713d62024-10-23 11:17:52 +010068 regmap_read(periph_hw->regmap, REG_SUBBLK_RESET_CR, &reg);
69 reg &= ~(1u << periph->shift);
70 regmap_write(periph_hw->regmap, REG_SUBBLK_RESET_CR, reg);
Padmarao Begari0c4ae802021-01-15 08:20:38 +053071
Conor Dooley54713d62024-10-23 11:17:52 +010072 regmap_read(periph_hw->regmap, REG_SUBBLK_CLOCK_CR, &reg);
73 reg |= (1u << periph->shift);
74 regmap_write(periph_hw->regmap, REG_SUBBLK_CLOCK_CR, reg);
Padmarao Begari0c4ae802021-01-15 08:20:38 +053075 }
76
77 return 0;
78}
79
80static int mpfs_periph_clk_disable(struct clk *hw)
81{
82 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
83 struct mpfs_periph_clock *periph = &periph_hw->periph;
Conor Dooley54713d62024-10-23 11:17:52 +010084 u32 reg;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053085
86 if (periph->flags != CLK_IS_CRITICAL) {
Conor Dooley54713d62024-10-23 11:17:52 +010087 regmap_read(periph_hw->regmap, REG_SUBBLK_RESET_CR, &reg);
88 reg |= (1u << periph->shift);
89 regmap_write(periph_hw->regmap, REG_SUBBLK_RESET_CR, reg);
Padmarao Begari0c4ae802021-01-15 08:20:38 +053090
Conor Dooley54713d62024-10-23 11:17:52 +010091 regmap_read(periph_hw->regmap, REG_SUBBLK_CLOCK_CR, &reg);
92 reg &= ~(1u << periph->shift);
93 regmap_write(periph_hw->regmap, REG_SUBBLK_CLOCK_CR, reg);
Padmarao Begari0c4ae802021-01-15 08:20:38 +053094 }
95
96 return 0;
97}
98
99static ulong mpfs_periph_clk_recalc_rate(struct clk *hw)
100{
101 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530102
Conor Dooley4a182e02022-10-25 08:58:45 +0100103 return periph_hw->prate;
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530104
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530105}
106
Conor Dooley73a1d602022-10-25 08:58:47 +0100107#define CLK_PERIPH(_id, _name, _parent_id, _shift, _flags) { \
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530108 .periph.id = _id, \
Conor Dooley73a1d602022-10-25 08:58:47 +0100109 .periph.parent_id = _parent_id, \
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530110 .periph.name = _name, \
111 .periph.shift = _shift, \
112 .periph.flags = _flags, \
113 }
114
Conor Dooley7bde2d22022-10-25 08:58:48 +0100115/*
116 * Critical clocks:
117 * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt
118 * trap handler
119 * - CLK_MMUART0: reserved by the hss
120 * - CLK_DDRC: provides clock to the ddr subsystem
121 * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop
122 * if the AHB interface clock is disabled
123 * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect)
124 * clock domain crossers which provide the interface to the FPGA fabric. Disabling them
125 * causes the FPGA fabric to go into reset.
126 * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire.
127 */
128
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530129static struct mpfs_periph_hw_clock mpfs_periph_clks[] = {
Conor Dooley73a1d602022-10-25 08:58:47 +0100130 CLK_PERIPH(CLK_ENVM, "clk_periph_envm", CLK_AHB, 0, CLK_IS_CRITICAL),
131 CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", CLK_AHB, 1, 0),
132 CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", CLK_AHB, 2, 0),
133 CLK_PERIPH(CLK_MMC, "clk_periph_mmc", CLK_AHB, 3, 0),
134 CLK_PERIPH(CLK_TIMER, "clk_periph_timer", CLK_RTCREF, 4, 0),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100135 CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", CLK_AHB, 5, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100136 CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", CLK_AHB, 6, 0),
137 CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", CLK_AHB, 7, 0),
138 CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", CLK_AHB, 8, 0),
139 CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", CLK_AHB, 9, 0),
140 CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", CLK_AHB, 10, 0),
141 CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", CLK_AHB, 11, 0),
142 CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", CLK_AHB, 12, 0),
143 CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", CLK_AHB, 13, 0),
144 CLK_PERIPH(CLK_CAN0, "clk_periph_can0", CLK_AHB, 14, 0),
145 CLK_PERIPH(CLK_CAN1, "clk_periph_can1", CLK_AHB, 15, 0),
146 CLK_PERIPH(CLK_USB, "clk_periph_usb", CLK_AHB, 16, 0),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100147 CLK_PERIPH(CLK_RTC, "clk_periph_rtc", CLK_AHB, 18, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100148 CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", CLK_AHB, 19, 0),
149 CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", CLK_AHB, 20, 0),
150 CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", CLK_AHB, 21, 0),
151 CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", CLK_AHB, 22, 0),
152 CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", CLK_AHB, 23, CLK_IS_CRITICAL),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100153 CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", CLK_AXI, 24, CLK_IS_CRITICAL),
154 CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", CLK_AXI, 25, CLK_IS_CRITICAL),
155 CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", CLK_AXI, 26, CLK_IS_CRITICAL),
156 CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", CLK_AXI, 27, CLK_IS_CRITICAL),
157 CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", CLK_AXI, 28, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100158 CLK_PERIPH(CLK_CFM, "clk_periph_cfm", CLK_AHB, 29, 0),
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530159};
160
Conor Dooley54713d62024-10-23 11:17:52 +0100161int mpfs_clk_register_periphs(struct udevice *dev, struct regmap *regmap)
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530162{
163 int ret;
164 int i, id, num_clks;
165 const char *name;
166 struct clk *hw;
167
168 num_clks = ARRAY_SIZE(mpfs_periph_clks);
169 for (i = 0; i < num_clks; i++) {
Conor Dooley73a1d602022-10-25 08:58:47 +0100170 struct clk parent = { .id = mpfs_periph_clks[i].periph.parent_id };
171
172 clk_request(dev, &parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530173 hw = &mpfs_periph_clks[i].hw;
Conor Dooley54713d62024-10-23 11:17:52 +0100174 mpfs_periph_clks[i].regmap = regmap;
Conor Dooley73a1d602022-10-25 08:58:47 +0100175 mpfs_periph_clks[i].prate = clk_get_rate(&parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530176 name = mpfs_periph_clks[i].periph.name;
Conor Dooley73a1d602022-10-25 08:58:47 +0100177 ret = clk_register(hw, MPFS_PERIPH_CLOCK, name, parent.dev->name);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530178 if (ret)
179 ERR_PTR(ret);
180 id = mpfs_periph_clks[i].periph.id;
181 clk_dm(id, hw);
182 }
183
184 return 0;
185}
186
187const struct clk_ops mpfs_periph_clk_ops = {
188 .enable = mpfs_periph_clk_enable,
189 .disable = mpfs_periph_clk_disable,
190 .get_rate = mpfs_periph_clk_recalc_rate,
191};
192
193U_BOOT_DRIVER(mpfs_periph_clock) = {
194 .name = MPFS_PERIPH_CLOCK,
195 .id = UCLASS_CLK,
196 .ops = &mpfs_periph_clk_ops,
197};