Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 1 | // 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 |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 32 | * @parent_id: index of the parent clock |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 33 | * @name: name of a peripheral clock |
| 34 | * @shift: shift to a peripheral clock bit field |
| 35 | * @flags: common clock framework flags |
| 36 | */ |
| 37 | struct mpfs_periph_clock { |
| 38 | unsigned int id; |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 39 | unsigned int parent_id; |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 40 | 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 | */ |
| 52 | struct mpfs_periph_hw_clock { |
| 53 | struct mpfs_periph_clock periph; |
| 54 | void __iomem *sys_base; |
| 55 | 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 | |
| 61 | static 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; |
| 65 | void __iomem *base_addr = periph_hw->sys_base; |
| 66 | u32 reg, val; |
| 67 | |
| 68 | if (periph->flags != CLK_IS_CRITICAL) { |
| 69 | reg = readl(base_addr + REG_SUBBLK_RESET_CR); |
| 70 | val = reg & ~(1u << periph->shift); |
| 71 | writel(val, base_addr + REG_SUBBLK_RESET_CR); |
| 72 | |
| 73 | reg = readl(base_addr + REG_SUBBLK_CLOCK_CR); |
| 74 | val = reg | (1u << periph->shift); |
| 75 | writel(val, base_addr + REG_SUBBLK_CLOCK_CR); |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int mpfs_periph_clk_disable(struct clk *hw) |
| 82 | { |
| 83 | struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw); |
| 84 | struct mpfs_periph_clock *periph = &periph_hw->periph; |
| 85 | void __iomem *base_addr = periph_hw->sys_base; |
| 86 | u32 reg, val; |
| 87 | |
| 88 | if (periph->flags != CLK_IS_CRITICAL) { |
| 89 | reg = readl(base_addr + REG_SUBBLK_RESET_CR); |
| 90 | val = reg | (1u << periph->shift); |
| 91 | writel(val, base_addr + REG_SUBBLK_RESET_CR); |
| 92 | |
| 93 | reg = readl(base_addr + REG_SUBBLK_CLOCK_CR); |
| 94 | val = reg & ~(1u << periph->shift); |
| 95 | writel(val, base_addr + REG_SUBBLK_CLOCK_CR); |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static ulong mpfs_periph_clk_recalc_rate(struct clk *hw) |
| 102 | { |
| 103 | struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 104 | |
Conor Dooley | 4a182e0 | 2022-10-25 08:58:45 +0100 | [diff] [blame] | 105 | return periph_hw->prate; |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 106 | |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 107 | } |
| 108 | |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 109 | #define CLK_PERIPH(_id, _name, _parent_id, _shift, _flags) { \ |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 110 | .periph.id = _id, \ |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 111 | .periph.parent_id = _parent_id, \ |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 112 | .periph.name = _name, \ |
| 113 | .periph.shift = _shift, \ |
| 114 | .periph.flags = _flags, \ |
| 115 | } |
| 116 | |
Conor Dooley | 7bde2d2 | 2022-10-25 08:58:48 +0100 | [diff] [blame] | 117 | /* |
| 118 | * Critical clocks: |
| 119 | * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt |
| 120 | * trap handler |
| 121 | * - CLK_MMUART0: reserved by the hss |
| 122 | * - CLK_DDRC: provides clock to the ddr subsystem |
| 123 | * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop |
| 124 | * if the AHB interface clock is disabled |
| 125 | * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect) |
| 126 | * clock domain crossers which provide the interface to the FPGA fabric. Disabling them |
| 127 | * causes the FPGA fabric to go into reset. |
| 128 | * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire. |
| 129 | */ |
| 130 | |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 131 | static struct mpfs_periph_hw_clock mpfs_periph_clks[] = { |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 132 | CLK_PERIPH(CLK_ENVM, "clk_periph_envm", CLK_AHB, 0, CLK_IS_CRITICAL), |
| 133 | CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", CLK_AHB, 1, 0), |
| 134 | CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", CLK_AHB, 2, 0), |
| 135 | CLK_PERIPH(CLK_MMC, "clk_periph_mmc", CLK_AHB, 3, 0), |
| 136 | CLK_PERIPH(CLK_TIMER, "clk_periph_timer", CLK_RTCREF, 4, 0), |
Conor Dooley | 7bde2d2 | 2022-10-25 08:58:48 +0100 | [diff] [blame] | 137 | CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", CLK_AHB, 5, CLK_IS_CRITICAL), |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 138 | CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", CLK_AHB, 6, 0), |
| 139 | CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", CLK_AHB, 7, 0), |
| 140 | CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", CLK_AHB, 8, 0), |
| 141 | CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", CLK_AHB, 9, 0), |
| 142 | CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", CLK_AHB, 10, 0), |
| 143 | CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", CLK_AHB, 11, 0), |
| 144 | CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", CLK_AHB, 12, 0), |
| 145 | CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", CLK_AHB, 13, 0), |
| 146 | CLK_PERIPH(CLK_CAN0, "clk_periph_can0", CLK_AHB, 14, 0), |
| 147 | CLK_PERIPH(CLK_CAN1, "clk_periph_can1", CLK_AHB, 15, 0), |
| 148 | CLK_PERIPH(CLK_USB, "clk_periph_usb", CLK_AHB, 16, 0), |
Conor Dooley | 7bde2d2 | 2022-10-25 08:58:48 +0100 | [diff] [blame] | 149 | CLK_PERIPH(CLK_RTC, "clk_periph_rtc", CLK_AHB, 18, CLK_IS_CRITICAL), |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 150 | CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", CLK_AHB, 19, 0), |
| 151 | CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", CLK_AHB, 20, 0), |
| 152 | CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", CLK_AHB, 21, 0), |
| 153 | CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", CLK_AHB, 22, 0), |
| 154 | CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", CLK_AHB, 23, CLK_IS_CRITICAL), |
Conor Dooley | 7bde2d2 | 2022-10-25 08:58:48 +0100 | [diff] [blame] | 155 | CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", CLK_AXI, 24, CLK_IS_CRITICAL), |
| 156 | CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", CLK_AXI, 25, CLK_IS_CRITICAL), |
| 157 | CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", CLK_AXI, 26, CLK_IS_CRITICAL), |
| 158 | CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", CLK_AXI, 27, CLK_IS_CRITICAL), |
| 159 | CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", CLK_AXI, 28, CLK_IS_CRITICAL), |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 160 | CLK_PERIPH(CLK_CFM, "clk_periph_cfm", CLK_AHB, 29, 0), |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 161 | }; |
| 162 | |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 163 | int mpfs_clk_register_periphs(void __iomem *base, struct udevice *dev) |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 164 | { |
| 165 | int ret; |
| 166 | int i, id, num_clks; |
| 167 | const char *name; |
| 168 | struct clk *hw; |
| 169 | |
| 170 | num_clks = ARRAY_SIZE(mpfs_periph_clks); |
| 171 | for (i = 0; i < num_clks; i++) { |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 172 | struct clk parent = { .id = mpfs_periph_clks[i].periph.parent_id }; |
| 173 | |
| 174 | clk_request(dev, &parent); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 175 | hw = &mpfs_periph_clks[i].hw; |
| 176 | mpfs_periph_clks[i].sys_base = base; |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 177 | mpfs_periph_clks[i].prate = clk_get_rate(&parent); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 178 | name = mpfs_periph_clks[i].periph.name; |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame] | 179 | ret = clk_register(hw, MPFS_PERIPH_CLOCK, name, parent.dev->name); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 180 | if (ret) |
| 181 | ERR_PTR(ret); |
| 182 | id = mpfs_periph_clks[i].periph.id; |
| 183 | clk_dm(id, hw); |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | const struct clk_ops mpfs_periph_clk_ops = { |
| 190 | .enable = mpfs_periph_clk_enable, |
| 191 | .disable = mpfs_periph_clk_disable, |
| 192 | .get_rate = mpfs_periph_clk_recalc_rate, |
| 193 | }; |
| 194 | |
| 195 | U_BOOT_DRIVER(mpfs_periph_clock) = { |
| 196 | .name = MPFS_PERIPH_CLOCK, |
| 197 | .id = UCLASS_CLK, |
| 198 | .ops = &mpfs_periph_clk_ops, |
| 199 | }; |