blob: 1488ef503ed039e6c78dd08d59f0362651deac2c [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 */
6#include <common.h>
7#include <clk.h>
8#include <clk-uclass.h>
9#include <asm/io.h>
10#include <dm/device.h>
11#include <dm/devres.h>
12#include <dm/uclass.h>
13#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
32 * @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;
38 const char *name;
39 u8 shift;
40 unsigned long flags;
41};
42
43/**
44 * struct mpfs_periph_hw_clock - hardware peripheral clock
45 * @periph: peripheral clock instance
46 * @sys_base: base address of the mpfs system register
47 * @prate: the pll clock rate
48 * @hw: clock instance
49 */
50struct mpfs_periph_hw_clock {
51 struct mpfs_periph_clock periph;
52 void __iomem *sys_base;
53 u32 prate;
54 struct clk hw;
55};
56
57#define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw)
58
59static int mpfs_periph_clk_enable(struct clk *hw)
60{
61 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
62 struct mpfs_periph_clock *periph = &periph_hw->periph;
63 void __iomem *base_addr = periph_hw->sys_base;
64 u32 reg, val;
65
66 if (periph->flags != CLK_IS_CRITICAL) {
67 reg = readl(base_addr + REG_SUBBLK_RESET_CR);
68 val = reg & ~(1u << periph->shift);
69 writel(val, base_addr + REG_SUBBLK_RESET_CR);
70
71 reg = readl(base_addr + REG_SUBBLK_CLOCK_CR);
72 val = reg | (1u << periph->shift);
73 writel(val, base_addr + REG_SUBBLK_CLOCK_CR);
74 }
75
76 return 0;
77}
78
79static int mpfs_periph_clk_disable(struct clk *hw)
80{
81 struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw);
82 struct mpfs_periph_clock *periph = &periph_hw->periph;
83 void __iomem *base_addr = periph_hw->sys_base;
84 u32 reg, val;
85
86 if (periph->flags != CLK_IS_CRITICAL) {
87 reg = readl(base_addr + REG_SUBBLK_RESET_CR);
88 val = reg | (1u << periph->shift);
89 writel(val, base_addr + REG_SUBBLK_RESET_CR);
90
91 reg = readl(base_addr + REG_SUBBLK_CLOCK_CR);
92 val = reg & ~(1u << periph->shift);
93 writel(val, base_addr + REG_SUBBLK_CLOCK_CR);
94 }
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
107#define CLK_PERIPH(_id, _name, _shift, _flags) { \
108 .periph.id = _id, \
109 .periph.name = _name, \
110 .periph.shift = _shift, \
111 .periph.flags = _flags, \
112 }
113
114static struct mpfs_periph_hw_clock mpfs_periph_clks[] = {
115 CLK_PERIPH(CLK_ENVM, "clk_periph_envm", 0, CLK_IS_CRITICAL),
116 CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", 1, 0),
117 CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", 2, 0),
118 CLK_PERIPH(CLK_MMC, "clk_periph_mmc", 3, 0),
119 CLK_PERIPH(CLK_TIMER, "clk_periph_timer", 4, 0),
120 CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", 5, 0),
121 CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", 6, 0),
122 CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", 7, 0),
123 CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", 8, 0),
124 CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", 9, 0),
125 CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", 10, 0),
126 CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", 11, 0),
127 CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", 12, 0),
128 CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", 13, 0),
129 CLK_PERIPH(CLK_CAN0, "clk_periph_can0", 14, 0),
130 CLK_PERIPH(CLK_CAN1, "clk_periph_can1", 15, 0),
131 CLK_PERIPH(CLK_USB, "clk_periph_usb", 16, 0),
132 CLK_PERIPH(CLK_RTC, "clk_periph_rtc", 18, 0),
133 CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", 19, 0),
134 CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", 20, 0),
135 CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", 21, 0),
136 CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", 22, 0),
137 CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", 23, CLK_IS_CRITICAL),
138 CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", 24, 0),
139 CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", 25, 0),
140 CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", 26, 0),
141 CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", 27, 0),
142 CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", 28, 0),
143 CLK_PERIPH(CLK_CFM, "clk_periph_cfm", 29, 0),
144};
145
Conor Dooley4a182e02022-10-25 08:58:45 +0100146int mpfs_clk_register_periphs(void __iomem *base, struct clk *parent)
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530147{
148 int ret;
149 int i, id, num_clks;
150 const char *name;
151 struct clk *hw;
152
153 num_clks = ARRAY_SIZE(mpfs_periph_clks);
154 for (i = 0; i < num_clks; i++) {
155 hw = &mpfs_periph_clks[i].hw;
156 mpfs_periph_clks[i].sys_base = base;
Conor Dooley4a182e02022-10-25 08:58:45 +0100157 mpfs_periph_clks[i].prate = clk_get_rate(parent);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530158 name = mpfs_periph_clks[i].periph.name;
Conor Dooley4a182e02022-10-25 08:58:45 +0100159 ret = clk_register(hw, MPFS_PERIPH_CLOCK, name, parent->dev->name);
Padmarao Begari0c4ae802021-01-15 08:20:38 +0530160 if (ret)
161 ERR_PTR(ret);
162 id = mpfs_periph_clks[i].periph.id;
163 clk_dm(id, hw);
164 }
165
166 return 0;
167}
168
169const struct clk_ops mpfs_periph_clk_ops = {
170 .enable = mpfs_periph_clk_enable,
171 .disable = mpfs_periph_clk_disable,
172 .get_rate = mpfs_periph_clk_recalc_rate,
173};
174
175U_BOOT_DRIVER(mpfs_periph_clock) = {
176 .name = MPFS_PERIPH_CLOCK,
177 .id = UCLASS_CLK,
178 .ops = &mpfs_periph_clk_ops,
179};