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 | |
| 117 | static struct mpfs_periph_hw_clock mpfs_periph_clks[] = { |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame^] | 118 | CLK_PERIPH(CLK_ENVM, "clk_periph_envm", CLK_AHB, 0, CLK_IS_CRITICAL), |
| 119 | CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", CLK_AHB, 1, 0), |
| 120 | CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", CLK_AHB, 2, 0), |
| 121 | CLK_PERIPH(CLK_MMC, "clk_periph_mmc", CLK_AHB, 3, 0), |
| 122 | CLK_PERIPH(CLK_TIMER, "clk_periph_timer", CLK_RTCREF, 4, 0), |
| 123 | CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", CLK_AHB, 5, 0), |
| 124 | CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", CLK_AHB, 6, 0), |
| 125 | CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", CLK_AHB, 7, 0), |
| 126 | CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", CLK_AHB, 8, 0), |
| 127 | CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", CLK_AHB, 9, 0), |
| 128 | CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", CLK_AHB, 10, 0), |
| 129 | CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", CLK_AHB, 11, 0), |
| 130 | CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", CLK_AHB, 12, 0), |
| 131 | CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", CLK_AHB, 13, 0), |
| 132 | CLK_PERIPH(CLK_CAN0, "clk_periph_can0", CLK_AHB, 14, 0), |
| 133 | CLK_PERIPH(CLK_CAN1, "clk_periph_can1", CLK_AHB, 15, 0), |
| 134 | CLK_PERIPH(CLK_USB, "clk_periph_usb", CLK_AHB, 16, 0), |
| 135 | CLK_PERIPH(CLK_RTC, "clk_periph_rtc", CLK_AHB, 18, 0), |
| 136 | CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", CLK_AHB, 19, 0), |
| 137 | CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", CLK_AHB, 20, 0), |
| 138 | CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", CLK_AHB, 21, 0), |
| 139 | CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", CLK_AHB, 22, 0), |
| 140 | CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", CLK_AHB, 23, CLK_IS_CRITICAL), |
| 141 | CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", CLK_AXI, 24, 0), |
| 142 | CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", CLK_AXI, 25, 0), |
| 143 | CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", CLK_AXI, 26, 0), |
| 144 | CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", CLK_AXI, 27, 0), |
| 145 | CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", CLK_AXI, 28, 0), |
| 146 | CLK_PERIPH(CLK_CFM, "clk_periph_cfm", CLK_AHB, 29, 0), |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 147 | }; |
| 148 | |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame^] | 149 | int mpfs_clk_register_periphs(void __iomem *base, struct udevice *dev) |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 150 | { |
| 151 | int ret; |
| 152 | int i, id, num_clks; |
| 153 | const char *name; |
| 154 | struct clk *hw; |
| 155 | |
| 156 | num_clks = ARRAY_SIZE(mpfs_periph_clks); |
| 157 | for (i = 0; i < num_clks; i++) { |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame^] | 158 | struct clk parent = { .id = mpfs_periph_clks[i].periph.parent_id }; |
| 159 | |
| 160 | clk_request(dev, &parent); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 161 | hw = &mpfs_periph_clks[i].hw; |
| 162 | mpfs_periph_clks[i].sys_base = base; |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame^] | 163 | mpfs_periph_clks[i].prate = clk_get_rate(&parent); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 164 | name = mpfs_periph_clks[i].periph.name; |
Conor Dooley | 73a1d60 | 2022-10-25 08:58:47 +0100 | [diff] [blame^] | 165 | ret = clk_register(hw, MPFS_PERIPH_CLOCK, name, parent.dev->name); |
Padmarao Begari | 0c4ae80 | 2021-01-15 08:20:38 +0530 | [diff] [blame] | 166 | if (ret) |
| 167 | ERR_PTR(ret); |
| 168 | id = mpfs_periph_clks[i].periph.id; |
| 169 | clk_dm(id, hw); |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | const struct clk_ops mpfs_periph_clk_ops = { |
| 176 | .enable = mpfs_periph_clk_enable, |
| 177 | .disable = mpfs_periph_clk_disable, |
| 178 | .get_rate = mpfs_periph_clk_recalc_rate, |
| 179 | }; |
| 180 | |
| 181 | U_BOOT_DRIVER(mpfs_periph_clock) = { |
| 182 | .name = MPFS_PERIPH_CLOCK, |
| 183 | .id = UCLASS_CLK, |
| 184 | .ops = &mpfs_periph_clk_ops, |
| 185 | }; |