blob: 41c6df4fb9795faee0ae2461c0599763b7e259b1 [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_PERIPH_CLOCK "mpfs_periph_clock"
18
19#define REG_CLOCK_CONFIG_CR 0x08
20#define REG_SUBBLK_CLOCK_CR 0x84
21#define REG_SUBBLK_RESET_CR 0x88
22
23#define CFG_CPU_SHIFT 0x0
24#define CFG_AXI_SHIFT 0x2
25#define CFG_AHB_SHIFT 0x4
26#define CFG_WIDTH 0x2
27
28/**
29 * struct mpfs_periph_clock - per instance of peripheral clock
30 * @id: index of a peripheral clock
Conor Dooley73a1d602022-10-25 08:58:47 +010031 * @parent_id: index of the parent clock
Padmarao Begari0c4ae802021-01-15 08:20:38 +053032 * @name: name of a peripheral clock
33 * @shift: shift to a peripheral clock bit field
34 * @flags: common clock framework flags
35 */
36struct mpfs_periph_clock {
37 unsigned int id;
Conor Dooley73a1d602022-10-25 08:58:47 +010038 unsigned int parent_id;
Padmarao Begari0c4ae802021-01-15 08:20:38 +053039 const char *name;
40 u8 shift;
41 unsigned long flags;
42};
43
44/**
45 * struct mpfs_periph_hw_clock - hardware peripheral clock
46 * @periph: peripheral clock instance
47 * @sys_base: base address of the mpfs system register
48 * @prate: the pll clock rate
49 * @hw: clock instance
50 */
51struct mpfs_periph_hw_clock {
52 struct mpfs_periph_clock periph;
53 void __iomem *sys_base;
54 u32 prate;
55 struct clk hw;
56};
57
58#define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw)
59
60static int mpfs_periph_clk_enable(struct clk *hw)
61{
62 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
63 struct mpfs_periph_clock *periph = &periph_hw->periph;
64 void __iomem *base_addr = periph_hw->sys_base;
65 u32 reg, val;
66
67 if (periph->flags != CLK_IS_CRITICAL) {
68 reg = readl(base_addr + REG_SUBBLK_RESET_CR);
69 val = reg & ~(1u << periph->shift);
70 writel(val, base_addr + REG_SUBBLK_RESET_CR);
71
72 reg = readl(base_addr + REG_SUBBLK_CLOCK_CR);
73 val = reg | (1u << periph->shift);
74 writel(val, base_addr + REG_SUBBLK_CLOCK_CR);
75 }
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;
84 void __iomem *base_addr = periph_hw->sys_base;
85 u32 reg, val;
86
87 if (periph->flags != CLK_IS_CRITICAL) {
88 reg = readl(base_addr + REG_SUBBLK_RESET_CR);
89 val = reg | (1u << periph->shift);
90 writel(val, base_addr + REG_SUBBLK_RESET_CR);
91
92 reg = readl(base_addr + REG_SUBBLK_CLOCK_CR);
93 val = reg & ~(1u << periph->shift);
94 writel(val, base_addr + REG_SUBBLK_CLOCK_CR);
95 }
96
97 return 0;
98}
99
100static ulong mpfs_periph_clk_recalc_rate(struct clk *hw)
101{
102 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530103
Conor Dooley4a182e02022-10-25 08:58:45 +0100104 return periph_hw->prate;
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530105
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530106}
107
Conor Dooley73a1d602022-10-25 08:58:47 +0100108#define CLK_PERIPH(_id, _name, _parent_id, _shift, _flags) { \
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530109 .periph.id = _id, \
Conor Dooley73a1d602022-10-25 08:58:47 +0100110 .periph.parent_id = _parent_id, \
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530111 .periph.name = _name, \
112 .periph.shift = _shift, \
113 .periph.flags = _flags, \
114 }
115
Conor Dooley7bde2d22022-10-25 08:58:48 +0100116/*
117 * Critical clocks:
118 * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt
119 * trap handler
120 * - CLK_MMUART0: reserved by the hss
121 * - CLK_DDRC: provides clock to the ddr subsystem
122 * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop
123 * if the AHB interface clock is disabled
124 * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect)
125 * clock domain crossers which provide the interface to the FPGA fabric. Disabling them
126 * causes the FPGA fabric to go into reset.
127 * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire.
128 */
129
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530130static struct mpfs_periph_hw_clock mpfs_periph_clks[] = {
Conor Dooley73a1d602022-10-25 08:58:47 +0100131 CLK_PERIPH(CLK_ENVM, "clk_periph_envm", CLK_AHB, 0, CLK_IS_CRITICAL),
132 CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", CLK_AHB, 1, 0),
133 CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", CLK_AHB, 2, 0),
134 CLK_PERIPH(CLK_MMC, "clk_periph_mmc", CLK_AHB, 3, 0),
135 CLK_PERIPH(CLK_TIMER, "clk_periph_timer", CLK_RTCREF, 4, 0),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100136 CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", CLK_AHB, 5, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100137 CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", CLK_AHB, 6, 0),
138 CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", CLK_AHB, 7, 0),
139 CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", CLK_AHB, 8, 0),
140 CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", CLK_AHB, 9, 0),
141 CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", CLK_AHB, 10, 0),
142 CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", CLK_AHB, 11, 0),
143 CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", CLK_AHB, 12, 0),
144 CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", CLK_AHB, 13, 0),
145 CLK_PERIPH(CLK_CAN0, "clk_periph_can0", CLK_AHB, 14, 0),
146 CLK_PERIPH(CLK_CAN1, "clk_periph_can1", CLK_AHB, 15, 0),
147 CLK_PERIPH(CLK_USB, "clk_periph_usb", CLK_AHB, 16, 0),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100148 CLK_PERIPH(CLK_RTC, "clk_periph_rtc", CLK_AHB, 18, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100149 CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", CLK_AHB, 19, 0),
150 CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", CLK_AHB, 20, 0),
151 CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", CLK_AHB, 21, 0),
152 CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", CLK_AHB, 22, 0),
153 CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", CLK_AHB, 23, CLK_IS_CRITICAL),
Conor Dooley7bde2d22022-10-25 08:58:48 +0100154 CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", CLK_AXI, 24, CLK_IS_CRITICAL),
155 CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", CLK_AXI, 25, CLK_IS_CRITICAL),
156 CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", CLK_AXI, 26, CLK_IS_CRITICAL),
157 CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", CLK_AXI, 27, CLK_IS_CRITICAL),
158 CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", CLK_AXI, 28, CLK_IS_CRITICAL),
Conor Dooley73a1d602022-10-25 08:58:47 +0100159 CLK_PERIPH(CLK_CFM, "clk_periph_cfm", CLK_AHB, 29, 0),
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530160};
161
Conor Dooley73a1d602022-10-25 08:58:47 +0100162int mpfs_clk_register_periphs(void __iomem *base, struct udevice *dev)
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530163{
164 int ret;
165 int i, id, num_clks;
166 const char *name;
167 struct clk *hw;
168
169 num_clks = ARRAY_SIZE(mpfs_periph_clks);
170 for (i = 0; i < num_clks; i++) {
Conor Dooley73a1d602022-10-25 08:58:47 +0100171 struct clk parent = { .id = mpfs_periph_clks[i].periph.parent_id };
172
173 clk_request(dev, &parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530174 hw = &mpfs_periph_clks[i].hw;
175 mpfs_periph_clks[i].sys_base = base;
Conor Dooley73a1d602022-10-25 08:58:47 +0100176 mpfs_periph_clks[i].prate = clk_get_rate(&parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530177 name = mpfs_periph_clks[i].periph.name;
Conor Dooley73a1d602022-10-25 08:58:47 +0100178 ret = clk_register(hw, MPFS_PERIPH_CLOCK, name, parent.dev->name);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530179 if (ret)
180 ERR_PTR(ret);
181 id = mpfs_periph_clks[i].periph.id;
182 clk_dm(id, hw);
183 }
184
185 return 0;
186}
187
188const struct clk_ops mpfs_periph_clk_ops = {
189 .enable = mpfs_periph_clk_enable,
190 .disable = mpfs_periph_clk_disable,
191 .get_rate = mpfs_periph_clk_recalc_rate,
192};
193
194U_BOOT_DRIVER(mpfs_periph_clock) = {
195 .name = MPFS_PERIPH_CLOCK,
196 .id = UCLASS_CLK,
197 .ops = &mpfs_periph_clk_ops,
198};