1635e5e73SDaire McNamara // SPDX-License-Identifier: GPL-2.0-only 2635e5e73SDaire McNamara /* 3635e5e73SDaire McNamara * Daire McNamara,<daire.mcnamara@microchip.com> 4635e5e73SDaire McNamara * Copyright (C) 2020 Microchip Technology Inc. All rights reserved. 5635e5e73SDaire McNamara */ 6b56bae2dSConor Dooley #include <linux/auxiliary_bus.h> 7635e5e73SDaire McNamara #include <linux/clk-provider.h> 8635e5e73SDaire McNamara #include <linux/io.h> 9635e5e73SDaire McNamara #include <linux/module.h> 10635e5e73SDaire McNamara #include <linux/platform_device.h> 11635e5e73SDaire McNamara #include <linux/slab.h> 12635e5e73SDaire McNamara #include <dt-bindings/clock/microchip,mpfs-clock.h> 13b56bae2dSConor Dooley #include <soc/microchip/mpfs.h> 14635e5e73SDaire McNamara 15635e5e73SDaire McNamara /* address offset of control registers */ 16445c2da8SConor Dooley #define REG_MSSPLL_REF_CR 0x08u 17445c2da8SConor Dooley #define REG_MSSPLL_POSTDIV_CR 0x10u 18445c2da8SConor Dooley #define REG_MSSPLL_SSCG_2_CR 0x2Cu 19635e5e73SDaire McNamara #define REG_CLOCK_CONFIG_CR 0x08u 201c6a7ea3SConor Dooley #define REG_RTC_CLOCK_CR 0x0Cu 21635e5e73SDaire McNamara #define REG_SUBBLK_CLOCK_CR 0x84u 22635e5e73SDaire McNamara #define REG_SUBBLK_RESET_CR 0x88u 23635e5e73SDaire McNamara 24445c2da8SConor Dooley #define MSSPLL_FBDIV_SHIFT 0x00u 25445c2da8SConor Dooley #define MSSPLL_FBDIV_WIDTH 0x0Cu 26445c2da8SConor Dooley #define MSSPLL_REFDIV_SHIFT 0x08u 27445c2da8SConor Dooley #define MSSPLL_REFDIV_WIDTH 0x06u 28445c2da8SConor Dooley #define MSSPLL_POSTDIV_SHIFT 0x08u 29445c2da8SConor Dooley #define MSSPLL_POSTDIV_WIDTH 0x07u 30445c2da8SConor Dooley #define MSSPLL_FIXED_DIV 4u 31445c2da8SConor Dooley 32635e5e73SDaire McNamara struct mpfs_clock_data { 33b56bae2dSConor Dooley struct device *dev; 34635e5e73SDaire McNamara void __iomem *base; 35445c2da8SConor Dooley void __iomem *msspll_base; 36635e5e73SDaire McNamara struct clk_hw_onecell_data hw_data; 37635e5e73SDaire McNamara }; 38635e5e73SDaire McNamara 39445c2da8SConor Dooley struct mpfs_msspll_hw_clock { 40445c2da8SConor Dooley void __iomem *base; 41445c2da8SConor Dooley unsigned int id; 42445c2da8SConor Dooley u32 reg_offset; 43445c2da8SConor Dooley u32 shift; 44445c2da8SConor Dooley u32 width; 45445c2da8SConor Dooley u32 flags; 46445c2da8SConor Dooley struct clk_hw hw; 47445c2da8SConor Dooley struct clk_init_data init; 48445c2da8SConor Dooley }; 49445c2da8SConor Dooley 50445c2da8SConor Dooley #define to_mpfs_msspll_clk(_hw) container_of(_hw, struct mpfs_msspll_hw_clock, hw) 51445c2da8SConor Dooley 52635e5e73SDaire McNamara struct mpfs_cfg_clock { 535fa27b77SConor Dooley void __iomem *reg; 54635e5e73SDaire McNamara const struct clk_div_table *table; 55635e5e73SDaire McNamara u8 shift; 56635e5e73SDaire McNamara u8 width; 57445c2da8SConor Dooley u8 flags; 58635e5e73SDaire McNamara }; 59635e5e73SDaire McNamara 60635e5e73SDaire McNamara struct mpfs_cfg_hw_clock { 61635e5e73SDaire McNamara struct mpfs_cfg_clock cfg; 62635e5e73SDaire McNamara struct clk_hw hw; 63635e5e73SDaire McNamara struct clk_init_data init; 6452fe6b52SConor Dooley unsigned int id; 6552fe6b52SConor Dooley u32 reg_offset; 66635e5e73SDaire McNamara }; 67635e5e73SDaire McNamara 68635e5e73SDaire McNamara #define to_mpfs_cfg_clk(_hw) container_of(_hw, struct mpfs_cfg_hw_clock, hw) 69635e5e73SDaire McNamara 70635e5e73SDaire McNamara struct mpfs_periph_clock { 715fa27b77SConor Dooley void __iomem *reg; 72635e5e73SDaire McNamara u8 shift; 73635e5e73SDaire McNamara }; 74635e5e73SDaire McNamara 75635e5e73SDaire McNamara struct mpfs_periph_hw_clock { 76635e5e73SDaire McNamara struct mpfs_periph_clock periph; 77635e5e73SDaire McNamara struct clk_hw hw; 7852fe6b52SConor Dooley unsigned int id; 79635e5e73SDaire McNamara }; 80635e5e73SDaire McNamara 81635e5e73SDaire McNamara #define to_mpfs_periph_clk(_hw) container_of(_hw, struct mpfs_periph_hw_clock, hw) 82635e5e73SDaire McNamara 83635e5e73SDaire McNamara /* 84635e5e73SDaire McNamara * mpfs_clk_lock prevents anything else from writing to the 85635e5e73SDaire McNamara * mpfs clk block while a software locked register is being written. 86635e5e73SDaire McNamara */ 87635e5e73SDaire McNamara static DEFINE_SPINLOCK(mpfs_clk_lock); 88635e5e73SDaire McNamara 89445c2da8SConor Dooley static const struct clk_parent_data mpfs_ext_ref[] = { 90635e5e73SDaire McNamara { .index = 0 }, 91635e5e73SDaire McNamara }; 92635e5e73SDaire McNamara 93635e5e73SDaire McNamara static const struct clk_div_table mpfs_div_cpu_axi_table[] = { 94635e5e73SDaire McNamara { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, 95635e5e73SDaire McNamara { 0, 0 } 96635e5e73SDaire McNamara }; 97635e5e73SDaire McNamara 98635e5e73SDaire McNamara static const struct clk_div_table mpfs_div_ahb_table[] = { 99635e5e73SDaire McNamara { 1, 2 }, { 2, 4}, { 3, 8 }, 100635e5e73SDaire McNamara { 0, 0 } 101635e5e73SDaire McNamara }; 102635e5e73SDaire McNamara 1031c6a7ea3SConor Dooley /* 1041c6a7ea3SConor Dooley * The only two supported reference clock frequencies for the PolarFire SoC are 1051c6a7ea3SConor Dooley * 100 and 125 MHz, as the rtc reference is required to be 1 MHz. 1061c6a7ea3SConor Dooley * It therefore only needs to have divider table entries corresponding to 1071c6a7ea3SConor Dooley * divide by 100 and 125. 1081c6a7ea3SConor Dooley */ 1091c6a7ea3SConor Dooley static const struct clk_div_table mpfs_div_rtcref_table[] = { 1101c6a7ea3SConor Dooley { 100, 100 }, { 125, 125 }, 1111c6a7ea3SConor Dooley { 0, 0 } 1121c6a7ea3SConor Dooley }; 1131c6a7ea3SConor Dooley 114445c2da8SConor Dooley static unsigned long mpfs_clk_msspll_recalc_rate(struct clk_hw *hw, unsigned long prate) 115445c2da8SConor Dooley { 116445c2da8SConor Dooley struct mpfs_msspll_hw_clock *msspll_hw = to_mpfs_msspll_clk(hw); 117445c2da8SConor Dooley void __iomem *mult_addr = msspll_hw->base + msspll_hw->reg_offset; 118445c2da8SConor Dooley void __iomem *ref_div_addr = msspll_hw->base + REG_MSSPLL_REF_CR; 119445c2da8SConor Dooley void __iomem *postdiv_addr = msspll_hw->base + REG_MSSPLL_POSTDIV_CR; 120445c2da8SConor Dooley u32 mult, ref_div, postdiv; 121445c2da8SConor Dooley 122445c2da8SConor Dooley mult = readl_relaxed(mult_addr) >> MSSPLL_FBDIV_SHIFT; 123445c2da8SConor Dooley mult &= clk_div_mask(MSSPLL_FBDIV_WIDTH); 124445c2da8SConor Dooley ref_div = readl_relaxed(ref_div_addr) >> MSSPLL_REFDIV_SHIFT; 125445c2da8SConor Dooley ref_div &= clk_div_mask(MSSPLL_REFDIV_WIDTH); 126445c2da8SConor Dooley postdiv = readl_relaxed(postdiv_addr) >> MSSPLL_POSTDIV_SHIFT; 127445c2da8SConor Dooley postdiv &= clk_div_mask(MSSPLL_POSTDIV_WIDTH); 128445c2da8SConor Dooley 129445c2da8SConor Dooley return prate * mult / (ref_div * MSSPLL_FIXED_DIV * postdiv); 130445c2da8SConor Dooley } 131445c2da8SConor Dooley 13214016e4aSConor Dooley static long mpfs_clk_msspll_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate) 13314016e4aSConor Dooley { 13414016e4aSConor Dooley struct mpfs_msspll_hw_clock *msspll_hw = to_mpfs_msspll_clk(hw); 13514016e4aSConor Dooley void __iomem *mult_addr = msspll_hw->base + msspll_hw->reg_offset; 13614016e4aSConor Dooley void __iomem *ref_div_addr = msspll_hw->base + REG_MSSPLL_REF_CR; 13714016e4aSConor Dooley u32 mult, ref_div; 13814016e4aSConor Dooley unsigned long rate_before_ctrl; 13914016e4aSConor Dooley 14014016e4aSConor Dooley mult = readl_relaxed(mult_addr) >> MSSPLL_FBDIV_SHIFT; 14114016e4aSConor Dooley mult &= clk_div_mask(MSSPLL_FBDIV_WIDTH); 14214016e4aSConor Dooley ref_div = readl_relaxed(ref_div_addr) >> MSSPLL_REFDIV_SHIFT; 14314016e4aSConor Dooley ref_div &= clk_div_mask(MSSPLL_REFDIV_WIDTH); 14414016e4aSConor Dooley 14514016e4aSConor Dooley rate_before_ctrl = rate * (ref_div * MSSPLL_FIXED_DIV) / mult; 14614016e4aSConor Dooley 14714016e4aSConor Dooley return divider_round_rate(hw, rate_before_ctrl, prate, NULL, MSSPLL_POSTDIV_WIDTH, 14814016e4aSConor Dooley msspll_hw->flags); 14914016e4aSConor Dooley } 15014016e4aSConor Dooley 15114016e4aSConor Dooley static int mpfs_clk_msspll_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate) 15214016e4aSConor Dooley { 15314016e4aSConor Dooley struct mpfs_msspll_hw_clock *msspll_hw = to_mpfs_msspll_clk(hw); 15414016e4aSConor Dooley void __iomem *mult_addr = msspll_hw->base + msspll_hw->reg_offset; 15514016e4aSConor Dooley void __iomem *ref_div_addr = msspll_hw->base + REG_MSSPLL_REF_CR; 15614016e4aSConor Dooley void __iomem *postdiv_addr = msspll_hw->base + REG_MSSPLL_POSTDIV_CR; 15714016e4aSConor Dooley u32 mult, ref_div, postdiv; 15814016e4aSConor Dooley int divider_setting; 15914016e4aSConor Dooley unsigned long rate_before_ctrl, flags; 16014016e4aSConor Dooley 16114016e4aSConor Dooley mult = readl_relaxed(mult_addr) >> MSSPLL_FBDIV_SHIFT; 16214016e4aSConor Dooley mult &= clk_div_mask(MSSPLL_FBDIV_WIDTH); 16314016e4aSConor Dooley ref_div = readl_relaxed(ref_div_addr) >> MSSPLL_REFDIV_SHIFT; 16414016e4aSConor Dooley ref_div &= clk_div_mask(MSSPLL_REFDIV_WIDTH); 16514016e4aSConor Dooley 16614016e4aSConor Dooley rate_before_ctrl = rate * (ref_div * MSSPLL_FIXED_DIV) / mult; 16714016e4aSConor Dooley divider_setting = divider_get_val(rate_before_ctrl, prate, NULL, MSSPLL_POSTDIV_WIDTH, 16814016e4aSConor Dooley msspll_hw->flags); 16914016e4aSConor Dooley 17014016e4aSConor Dooley if (divider_setting < 0) 17114016e4aSConor Dooley return divider_setting; 17214016e4aSConor Dooley 17314016e4aSConor Dooley spin_lock_irqsave(&mpfs_clk_lock, flags); 17414016e4aSConor Dooley 17514016e4aSConor Dooley postdiv = readl_relaxed(postdiv_addr); 17614016e4aSConor Dooley postdiv &= ~(clk_div_mask(MSSPLL_POSTDIV_WIDTH) << MSSPLL_POSTDIV_SHIFT); 17714016e4aSConor Dooley writel_relaxed(postdiv, postdiv_addr); 17814016e4aSConor Dooley 17914016e4aSConor Dooley spin_unlock_irqrestore(&mpfs_clk_lock, flags); 18014016e4aSConor Dooley 18114016e4aSConor Dooley return 0; 18214016e4aSConor Dooley } 18314016e4aSConor Dooley 184445c2da8SConor Dooley static const struct clk_ops mpfs_clk_msspll_ops = { 185445c2da8SConor Dooley .recalc_rate = mpfs_clk_msspll_recalc_rate, 18614016e4aSConor Dooley .round_rate = mpfs_clk_msspll_round_rate, 18714016e4aSConor Dooley .set_rate = mpfs_clk_msspll_set_rate, 188445c2da8SConor Dooley }; 189445c2da8SConor Dooley 190445c2da8SConor Dooley #define CLK_PLL(_id, _name, _parent, _shift, _width, _flags, _offset) { \ 191445c2da8SConor Dooley .id = _id, \ 192445c2da8SConor Dooley .shift = _shift, \ 193445c2da8SConor Dooley .width = _width, \ 194445c2da8SConor Dooley .reg_offset = _offset, \ 195445c2da8SConor Dooley .flags = _flags, \ 196445c2da8SConor Dooley .hw.init = CLK_HW_INIT_PARENTS_DATA(_name, _parent, &mpfs_clk_msspll_ops, 0), \ 197445c2da8SConor Dooley } 198445c2da8SConor Dooley 199445c2da8SConor Dooley static struct mpfs_msspll_hw_clock mpfs_msspll_clks[] = { 200445c2da8SConor Dooley CLK_PLL(CLK_MSSPLL, "clk_msspll", mpfs_ext_ref, MSSPLL_FBDIV_SHIFT, 201445c2da8SConor Dooley MSSPLL_FBDIV_WIDTH, 0, REG_MSSPLL_SSCG_2_CR), 202445c2da8SConor Dooley }; 203445c2da8SConor Dooley 204445c2da8SConor Dooley static int mpfs_clk_register_mssplls(struct device *dev, struct mpfs_msspll_hw_clock *msspll_hws, 205445c2da8SConor Dooley unsigned int num_clks, struct mpfs_clock_data *data) 206445c2da8SConor Dooley { 207445c2da8SConor Dooley unsigned int i; 208445c2da8SConor Dooley int ret; 209445c2da8SConor Dooley 210445c2da8SConor Dooley for (i = 0; i < num_clks; i++) { 211445c2da8SConor Dooley struct mpfs_msspll_hw_clock *msspll_hw = &msspll_hws[i]; 212445c2da8SConor Dooley 213*e7df7ba0SConor Dooley msspll_hw->base = data->msspll_base; 214*e7df7ba0SConor Dooley ret = devm_clk_hw_register(dev, &msspll_hw->hw); 215445c2da8SConor Dooley if (ret) 216445c2da8SConor Dooley return dev_err_probe(dev, ret, "failed to register msspll id: %d\n", 217445c2da8SConor Dooley CLK_MSSPLL); 218445c2da8SConor Dooley 219445c2da8SConor Dooley data->hw_data.hws[msspll_hw->id] = &msspll_hw->hw; 220445c2da8SConor Dooley } 221445c2da8SConor Dooley 222445c2da8SConor Dooley return 0; 223445c2da8SConor Dooley } 224445c2da8SConor Dooley 225445c2da8SConor Dooley /* 226445c2da8SConor Dooley * "CFG" clocks 227445c2da8SConor Dooley */ 228445c2da8SConor Dooley 229635e5e73SDaire McNamara static unsigned long mpfs_cfg_clk_recalc_rate(struct clk_hw *hw, unsigned long prate) 230635e5e73SDaire McNamara { 231635e5e73SDaire McNamara struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw); 232635e5e73SDaire McNamara struct mpfs_cfg_clock *cfg = &cfg_hw->cfg; 233635e5e73SDaire McNamara u32 val; 234635e5e73SDaire McNamara 2355fa27b77SConor Dooley val = readl_relaxed(cfg->reg) >> cfg->shift; 236635e5e73SDaire McNamara val &= clk_div_mask(cfg->width); 237635e5e73SDaire McNamara 238445c2da8SConor Dooley return divider_recalc_rate(hw, prate, val, cfg->table, cfg->flags, cfg->width); 239635e5e73SDaire McNamara } 240635e5e73SDaire McNamara 241635e5e73SDaire McNamara static long mpfs_cfg_clk_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate) 242635e5e73SDaire McNamara { 243635e5e73SDaire McNamara struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw); 244635e5e73SDaire McNamara struct mpfs_cfg_clock *cfg = &cfg_hw->cfg; 245635e5e73SDaire McNamara 246635e5e73SDaire McNamara return divider_round_rate(hw, rate, prate, cfg->table, cfg->width, 0); 247635e5e73SDaire McNamara } 248635e5e73SDaire McNamara 249635e5e73SDaire McNamara static int mpfs_cfg_clk_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate) 250635e5e73SDaire McNamara { 251635e5e73SDaire McNamara struct mpfs_cfg_hw_clock *cfg_hw = to_mpfs_cfg_clk(hw); 252635e5e73SDaire McNamara struct mpfs_cfg_clock *cfg = &cfg_hw->cfg; 253635e5e73SDaire McNamara unsigned long flags; 254635e5e73SDaire McNamara u32 val; 255635e5e73SDaire McNamara int divider_setting; 256635e5e73SDaire McNamara 257635e5e73SDaire McNamara divider_setting = divider_get_val(rate, prate, cfg->table, cfg->width, 0); 258635e5e73SDaire McNamara 259635e5e73SDaire McNamara if (divider_setting < 0) 260635e5e73SDaire McNamara return divider_setting; 261635e5e73SDaire McNamara 262635e5e73SDaire McNamara spin_lock_irqsave(&mpfs_clk_lock, flags); 2635fa27b77SConor Dooley val = readl_relaxed(cfg->reg); 264635e5e73SDaire McNamara val &= ~(clk_div_mask(cfg->width) << cfg_hw->cfg.shift); 265635e5e73SDaire McNamara val |= divider_setting << cfg->shift; 2665fa27b77SConor Dooley writel_relaxed(val, cfg->reg); 267635e5e73SDaire McNamara 268635e5e73SDaire McNamara spin_unlock_irqrestore(&mpfs_clk_lock, flags); 269635e5e73SDaire McNamara 270635e5e73SDaire McNamara return 0; 271635e5e73SDaire McNamara } 272635e5e73SDaire McNamara 273635e5e73SDaire McNamara static const struct clk_ops mpfs_clk_cfg_ops = { 274635e5e73SDaire McNamara .recalc_rate = mpfs_cfg_clk_recalc_rate, 275635e5e73SDaire McNamara .round_rate = mpfs_cfg_clk_round_rate, 276635e5e73SDaire McNamara .set_rate = mpfs_cfg_clk_set_rate, 277635e5e73SDaire McNamara }; 278635e5e73SDaire McNamara 279445c2da8SConor Dooley #define CLK_CFG(_id, _name, _parent, _shift, _width, _table, _flags, _offset) { \ 28052fe6b52SConor Dooley .id = _id, \ 281635e5e73SDaire McNamara .cfg.shift = _shift, \ 282635e5e73SDaire McNamara .cfg.width = _width, \ 283635e5e73SDaire McNamara .cfg.table = _table, \ 28452fe6b52SConor Dooley .reg_offset = _offset, \ 285445c2da8SConor Dooley .cfg.flags = _flags, \ 286445c2da8SConor Dooley .hw.init = CLK_HW_INIT(_name, _parent, &mpfs_clk_cfg_ops, 0), \ 287635e5e73SDaire McNamara } 288635e5e73SDaire McNamara 2895da39ac5SConor Dooley #define CLK_CPU_OFFSET 0u 2905da39ac5SConor Dooley #define CLK_AXI_OFFSET 1u 2915da39ac5SConor Dooley #define CLK_AHB_OFFSET 2u 2925da39ac5SConor Dooley #define CLK_RTCREF_OFFSET 3u 2935da39ac5SConor Dooley 294635e5e73SDaire McNamara static struct mpfs_cfg_hw_clock mpfs_cfg_clks[] = { 295445c2da8SConor Dooley CLK_CFG(CLK_CPU, "clk_cpu", "clk_msspll", 0, 2, mpfs_div_cpu_axi_table, 0, 296445c2da8SConor Dooley REG_CLOCK_CONFIG_CR), 297445c2da8SConor Dooley CLK_CFG(CLK_AXI, "clk_axi", "clk_msspll", 2, 2, mpfs_div_cpu_axi_table, 0, 298445c2da8SConor Dooley REG_CLOCK_CONFIG_CR), 299445c2da8SConor Dooley CLK_CFG(CLK_AHB, "clk_ahb", "clk_msspll", 4, 2, mpfs_div_ahb_table, 0, 300445c2da8SConor Dooley REG_CLOCK_CONFIG_CR), 3011c6a7ea3SConor Dooley { 30252fe6b52SConor Dooley .id = CLK_RTCREF, 3031c6a7ea3SConor Dooley .cfg.shift = 0, 3041c6a7ea3SConor Dooley .cfg.width = 12, 3051c6a7ea3SConor Dooley .cfg.table = mpfs_div_rtcref_table, 30652fe6b52SConor Dooley .reg_offset = REG_RTC_CLOCK_CR, 3071c6a7ea3SConor Dooley .cfg.flags = CLK_DIVIDER_ONE_BASED, 3081c6a7ea3SConor Dooley .hw.init = 3091c6a7ea3SConor Dooley CLK_HW_INIT_PARENTS_DATA("clk_rtcref", mpfs_ext_ref, &mpfs_clk_cfg_ops, 0), 3101c6a7ea3SConor Dooley } 311635e5e73SDaire McNamara }; 312635e5e73SDaire McNamara 313635e5e73SDaire McNamara static int mpfs_clk_register_cfgs(struct device *dev, struct mpfs_cfg_hw_clock *cfg_hws, 314635e5e73SDaire McNamara unsigned int num_clks, struct mpfs_clock_data *data) 315635e5e73SDaire McNamara { 316635e5e73SDaire McNamara unsigned int i, id; 317635e5e73SDaire McNamara int ret; 318635e5e73SDaire McNamara 319635e5e73SDaire McNamara for (i = 0; i < num_clks; i++) { 320635e5e73SDaire McNamara struct mpfs_cfg_hw_clock *cfg_hw = &cfg_hws[i]; 321635e5e73SDaire McNamara 322*e7df7ba0SConor Dooley cfg_hw->cfg.reg = data->base + cfg_hw->reg_offset; 323*e7df7ba0SConor Dooley ret = devm_clk_hw_register(dev, &cfg_hw->hw); 324635e5e73SDaire McNamara if (ret) 325635e5e73SDaire McNamara return dev_err_probe(dev, ret, "failed to register clock id: %d\n", 32652fe6b52SConor Dooley cfg_hw->id); 327635e5e73SDaire McNamara 32852fe6b52SConor Dooley id = cfg_hw->id; 329635e5e73SDaire McNamara data->hw_data.hws[id] = &cfg_hw->hw; 330635e5e73SDaire McNamara } 331635e5e73SDaire McNamara 332635e5e73SDaire McNamara return 0; 333635e5e73SDaire McNamara } 334635e5e73SDaire McNamara 335445c2da8SConor Dooley /* 336445c2da8SConor Dooley * peripheral clocks - devices connected to axi or ahb buses. 337445c2da8SConor Dooley */ 338445c2da8SConor Dooley 339635e5e73SDaire McNamara static int mpfs_periph_clk_enable(struct clk_hw *hw) 340635e5e73SDaire McNamara { 341635e5e73SDaire McNamara struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw); 342635e5e73SDaire McNamara struct mpfs_periph_clock *periph = &periph_hw->periph; 343635e5e73SDaire McNamara u32 reg, val; 344635e5e73SDaire McNamara unsigned long flags; 345635e5e73SDaire McNamara 346635e5e73SDaire McNamara spin_lock_irqsave(&mpfs_clk_lock, flags); 347635e5e73SDaire McNamara 3485fa27b77SConor Dooley reg = readl_relaxed(periph->reg); 349635e5e73SDaire McNamara val = reg | (1u << periph->shift); 3505fa27b77SConor Dooley writel_relaxed(val, periph->reg); 351635e5e73SDaire McNamara 352635e5e73SDaire McNamara spin_unlock_irqrestore(&mpfs_clk_lock, flags); 353635e5e73SDaire McNamara 354635e5e73SDaire McNamara return 0; 355635e5e73SDaire McNamara } 356635e5e73SDaire McNamara 357635e5e73SDaire McNamara static void mpfs_periph_clk_disable(struct clk_hw *hw) 358635e5e73SDaire McNamara { 359635e5e73SDaire McNamara struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw); 360635e5e73SDaire McNamara struct mpfs_periph_clock *periph = &periph_hw->periph; 361635e5e73SDaire McNamara u32 reg, val; 362635e5e73SDaire McNamara unsigned long flags; 363635e5e73SDaire McNamara 364635e5e73SDaire McNamara spin_lock_irqsave(&mpfs_clk_lock, flags); 365635e5e73SDaire McNamara 3665fa27b77SConor Dooley reg = readl_relaxed(periph->reg); 367635e5e73SDaire McNamara val = reg & ~(1u << periph->shift); 3685fa27b77SConor Dooley writel_relaxed(val, periph->reg); 369635e5e73SDaire McNamara 370635e5e73SDaire McNamara spin_unlock_irqrestore(&mpfs_clk_lock, flags); 371635e5e73SDaire McNamara } 372635e5e73SDaire McNamara 373635e5e73SDaire McNamara static int mpfs_periph_clk_is_enabled(struct clk_hw *hw) 374635e5e73SDaire McNamara { 375635e5e73SDaire McNamara struct mpfs_periph_hw_clock *periph_hw = to_mpfs_periph_clk(hw); 376635e5e73SDaire McNamara struct mpfs_periph_clock *periph = &periph_hw->periph; 377635e5e73SDaire McNamara u32 reg; 378635e5e73SDaire McNamara 3795fa27b77SConor Dooley reg = readl_relaxed(periph->reg); 380635e5e73SDaire McNamara if (reg & (1u << periph->shift)) 381635e5e73SDaire McNamara return 1; 382635e5e73SDaire McNamara 383635e5e73SDaire McNamara return 0; 384635e5e73SDaire McNamara } 385635e5e73SDaire McNamara 386635e5e73SDaire McNamara static const struct clk_ops mpfs_periph_clk_ops = { 387635e5e73SDaire McNamara .enable = mpfs_periph_clk_enable, 388635e5e73SDaire McNamara .disable = mpfs_periph_clk_disable, 389635e5e73SDaire McNamara .is_enabled = mpfs_periph_clk_is_enabled, 390635e5e73SDaire McNamara }; 391635e5e73SDaire McNamara 392635e5e73SDaire McNamara #define CLK_PERIPH(_id, _name, _parent, _shift, _flags) { \ 39352fe6b52SConor Dooley .id = _id, \ 394635e5e73SDaire McNamara .periph.shift = _shift, \ 395635e5e73SDaire McNamara .hw.init = CLK_HW_INIT_HW(_name, _parent, &mpfs_periph_clk_ops, \ 396635e5e73SDaire McNamara _flags), \ 397635e5e73SDaire McNamara } 398635e5e73SDaire McNamara 3995da39ac5SConor Dooley #define PARENT_CLK(PARENT) (&mpfs_cfg_clks[CLK_##PARENT##_OFFSET].hw) 400635e5e73SDaire McNamara 401635e5e73SDaire McNamara /* 402635e5e73SDaire McNamara * Critical clocks: 403635e5e73SDaire McNamara * - CLK_ENVM: reserved by hart software services (hss) superloop monitor/m mode interrupt 404635e5e73SDaire McNamara * trap handler 405635e5e73SDaire McNamara * - CLK_MMUART0: reserved by the hss 406635e5e73SDaire McNamara * - CLK_DDRC: provides clock to the ddr subsystem 40705d27090SConor Dooley * - CLK_RTC: the onboard RTC's AHB bus clock must be kept running as the rtc will stop 40805d27090SConor Dooley * if the AHB interface clock is disabled 409a2438f82SConor Dooley * - CLK_FICx: these provide the processor side clocks to the "FIC" (Fabric InterConnect) 410a2438f82SConor Dooley * clock domain crossers which provide the interface to the FPGA fabric. Disabling them 411a2438f82SConor Dooley * causes the FPGA fabric to go into reset. 412a2438f82SConor Dooley * - CLK_ATHENA: The athena clock is FIC4, which is reserved for the Athena TeraFire. 413635e5e73SDaire McNamara */ 414635e5e73SDaire McNamara 415635e5e73SDaire McNamara static struct mpfs_periph_hw_clock mpfs_periph_clks[] = { 416635e5e73SDaire McNamara CLK_PERIPH(CLK_ENVM, "clk_periph_envm", PARENT_CLK(AHB), 0, CLK_IS_CRITICAL), 417635e5e73SDaire McNamara CLK_PERIPH(CLK_MAC0, "clk_periph_mac0", PARENT_CLK(AHB), 1, 0), 418635e5e73SDaire McNamara CLK_PERIPH(CLK_MAC1, "clk_periph_mac1", PARENT_CLK(AHB), 2, 0), 419635e5e73SDaire McNamara CLK_PERIPH(CLK_MMC, "clk_periph_mmc", PARENT_CLK(AHB), 3, 0), 4201c6a7ea3SConor Dooley CLK_PERIPH(CLK_TIMER, "clk_periph_timer", PARENT_CLK(RTCREF), 4, 0), 421635e5e73SDaire McNamara CLK_PERIPH(CLK_MMUART0, "clk_periph_mmuart0", PARENT_CLK(AHB), 5, CLK_IS_CRITICAL), 422635e5e73SDaire McNamara CLK_PERIPH(CLK_MMUART1, "clk_periph_mmuart1", PARENT_CLK(AHB), 6, 0), 423635e5e73SDaire McNamara CLK_PERIPH(CLK_MMUART2, "clk_periph_mmuart2", PARENT_CLK(AHB), 7, 0), 424635e5e73SDaire McNamara CLK_PERIPH(CLK_MMUART3, "clk_periph_mmuart3", PARENT_CLK(AHB), 8, 0), 425635e5e73SDaire McNamara CLK_PERIPH(CLK_MMUART4, "clk_periph_mmuart4", PARENT_CLK(AHB), 9, 0), 426635e5e73SDaire McNamara CLK_PERIPH(CLK_SPI0, "clk_periph_spi0", PARENT_CLK(AHB), 10, 0), 427635e5e73SDaire McNamara CLK_PERIPH(CLK_SPI1, "clk_periph_spi1", PARENT_CLK(AHB), 11, 0), 428635e5e73SDaire McNamara CLK_PERIPH(CLK_I2C0, "clk_periph_i2c0", PARENT_CLK(AHB), 12, 0), 429635e5e73SDaire McNamara CLK_PERIPH(CLK_I2C1, "clk_periph_i2c1", PARENT_CLK(AHB), 13, 0), 430635e5e73SDaire McNamara CLK_PERIPH(CLK_CAN0, "clk_periph_can0", PARENT_CLK(AHB), 14, 0), 431635e5e73SDaire McNamara CLK_PERIPH(CLK_CAN1, "clk_periph_can1", PARENT_CLK(AHB), 15, 0), 432635e5e73SDaire McNamara CLK_PERIPH(CLK_USB, "clk_periph_usb", PARENT_CLK(AHB), 16, 0), 43305d27090SConor Dooley CLK_PERIPH(CLK_RTC, "clk_periph_rtc", PARENT_CLK(AHB), 18, CLK_IS_CRITICAL), 434635e5e73SDaire McNamara CLK_PERIPH(CLK_QSPI, "clk_periph_qspi", PARENT_CLK(AHB), 19, 0), 435635e5e73SDaire McNamara CLK_PERIPH(CLK_GPIO0, "clk_periph_gpio0", PARENT_CLK(AHB), 20, 0), 436635e5e73SDaire McNamara CLK_PERIPH(CLK_GPIO1, "clk_periph_gpio1", PARENT_CLK(AHB), 21, 0), 437635e5e73SDaire McNamara CLK_PERIPH(CLK_GPIO2, "clk_periph_gpio2", PARENT_CLK(AHB), 22, 0), 438635e5e73SDaire McNamara CLK_PERIPH(CLK_DDRC, "clk_periph_ddrc", PARENT_CLK(AHB), 23, CLK_IS_CRITICAL), 4398f9fb2abSConor Dooley CLK_PERIPH(CLK_FIC0, "clk_periph_fic0", PARENT_CLK(AXI), 24, CLK_IS_CRITICAL), 4408f9fb2abSConor Dooley CLK_PERIPH(CLK_FIC1, "clk_periph_fic1", PARENT_CLK(AXI), 25, CLK_IS_CRITICAL), 4418f9fb2abSConor Dooley CLK_PERIPH(CLK_FIC2, "clk_periph_fic2", PARENT_CLK(AXI), 26, CLK_IS_CRITICAL), 4428f9fb2abSConor Dooley CLK_PERIPH(CLK_FIC3, "clk_periph_fic3", PARENT_CLK(AXI), 27, CLK_IS_CRITICAL), 443a2438f82SConor Dooley CLK_PERIPH(CLK_ATHENA, "clk_periph_athena", PARENT_CLK(AXI), 28, CLK_IS_CRITICAL), 444635e5e73SDaire McNamara CLK_PERIPH(CLK_CFM, "clk_periph_cfm", PARENT_CLK(AHB), 29, 0), 445635e5e73SDaire McNamara }; 446635e5e73SDaire McNamara 447635e5e73SDaire McNamara static int mpfs_clk_register_periphs(struct device *dev, struct mpfs_periph_hw_clock *periph_hws, 448635e5e73SDaire McNamara int num_clks, struct mpfs_clock_data *data) 449635e5e73SDaire McNamara { 450635e5e73SDaire McNamara unsigned int i, id; 451635e5e73SDaire McNamara int ret; 452635e5e73SDaire McNamara 453635e5e73SDaire McNamara for (i = 0; i < num_clks; i++) { 454635e5e73SDaire McNamara struct mpfs_periph_hw_clock *periph_hw = &periph_hws[i]; 455635e5e73SDaire McNamara 456*e7df7ba0SConor Dooley periph_hw->periph.reg = data->base + REG_SUBBLK_CLOCK_CR; 457*e7df7ba0SConor Dooley ret = devm_clk_hw_register(dev, &periph_hw->hw); 458635e5e73SDaire McNamara if (ret) 459635e5e73SDaire McNamara return dev_err_probe(dev, ret, "failed to register clock id: %d\n", 46052fe6b52SConor Dooley periph_hw->id); 461635e5e73SDaire McNamara 46252fe6b52SConor Dooley id = periph_hws[i].id; 463635e5e73SDaire McNamara data->hw_data.hws[id] = &periph_hw->hw; 464635e5e73SDaire McNamara } 465635e5e73SDaire McNamara 466635e5e73SDaire McNamara return 0; 467635e5e73SDaire McNamara } 468635e5e73SDaire McNamara 469b56bae2dSConor Dooley /* 470b56bae2dSConor Dooley * Peripheral clock resets 471b56bae2dSConor Dooley */ 472b56bae2dSConor Dooley 473b56bae2dSConor Dooley #if IS_ENABLED(CONFIG_RESET_CONTROLLER) 474b56bae2dSConor Dooley 475b56bae2dSConor Dooley u32 mpfs_reset_read(struct device *dev) 476b56bae2dSConor Dooley { 477b56bae2dSConor Dooley struct mpfs_clock_data *clock_data = dev_get_drvdata(dev->parent); 478b56bae2dSConor Dooley 479b56bae2dSConor Dooley return readl_relaxed(clock_data->base + REG_SUBBLK_RESET_CR); 480b56bae2dSConor Dooley } 481b56bae2dSConor Dooley EXPORT_SYMBOL_NS_GPL(mpfs_reset_read, MCHP_CLK_MPFS); 482b56bae2dSConor Dooley 483b56bae2dSConor Dooley void mpfs_reset_write(struct device *dev, u32 val) 484b56bae2dSConor Dooley { 485b56bae2dSConor Dooley struct mpfs_clock_data *clock_data = dev_get_drvdata(dev->parent); 486b56bae2dSConor Dooley 487b56bae2dSConor Dooley writel_relaxed(val, clock_data->base + REG_SUBBLK_RESET_CR); 488b56bae2dSConor Dooley } 489b56bae2dSConor Dooley EXPORT_SYMBOL_NS_GPL(mpfs_reset_write, MCHP_CLK_MPFS); 490b56bae2dSConor Dooley 491b56bae2dSConor Dooley static void mpfs_reset_unregister_adev(void *_adev) 492b56bae2dSConor Dooley { 493b56bae2dSConor Dooley struct auxiliary_device *adev = _adev; 494b56bae2dSConor Dooley 495b56bae2dSConor Dooley auxiliary_device_delete(adev); 496b56bae2dSConor Dooley } 497b56bae2dSConor Dooley 498b56bae2dSConor Dooley static void mpfs_reset_adev_release(struct device *dev) 499b56bae2dSConor Dooley { 500b56bae2dSConor Dooley struct auxiliary_device *adev = to_auxiliary_dev(dev); 501b56bae2dSConor Dooley 502b56bae2dSConor Dooley auxiliary_device_uninit(adev); 503b56bae2dSConor Dooley 504b56bae2dSConor Dooley kfree(adev); 505b56bae2dSConor Dooley } 506b56bae2dSConor Dooley 507b56bae2dSConor Dooley static struct auxiliary_device *mpfs_reset_adev_alloc(struct mpfs_clock_data *clk_data) 508b56bae2dSConor Dooley { 509b56bae2dSConor Dooley struct auxiliary_device *adev; 510b56bae2dSConor Dooley int ret; 511b56bae2dSConor Dooley 512b56bae2dSConor Dooley adev = kzalloc(sizeof(*adev), GFP_KERNEL); 513b56bae2dSConor Dooley if (!adev) 514b56bae2dSConor Dooley return ERR_PTR(-ENOMEM); 515b56bae2dSConor Dooley 516b56bae2dSConor Dooley adev->name = "reset-mpfs"; 517b56bae2dSConor Dooley adev->dev.parent = clk_data->dev; 518b56bae2dSConor Dooley adev->dev.release = mpfs_reset_adev_release; 519b56bae2dSConor Dooley adev->id = 666u; 520b56bae2dSConor Dooley 521b56bae2dSConor Dooley ret = auxiliary_device_init(adev); 522b56bae2dSConor Dooley if (ret) { 523b56bae2dSConor Dooley kfree(adev); 524b56bae2dSConor Dooley return ERR_PTR(ret); 525b56bae2dSConor Dooley } 526b56bae2dSConor Dooley 527b56bae2dSConor Dooley return adev; 528b56bae2dSConor Dooley } 529b56bae2dSConor Dooley 530b56bae2dSConor Dooley static int mpfs_reset_controller_register(struct mpfs_clock_data *clk_data) 531b56bae2dSConor Dooley { 532b56bae2dSConor Dooley struct auxiliary_device *adev; 533b56bae2dSConor Dooley int ret; 534b56bae2dSConor Dooley 535b56bae2dSConor Dooley adev = mpfs_reset_adev_alloc(clk_data); 536b56bae2dSConor Dooley if (IS_ERR(adev)) 537b56bae2dSConor Dooley return PTR_ERR(adev); 538b56bae2dSConor Dooley 539b56bae2dSConor Dooley ret = auxiliary_device_add(adev); 540b56bae2dSConor Dooley if (ret) { 541b56bae2dSConor Dooley auxiliary_device_uninit(adev); 542b56bae2dSConor Dooley return ret; 543b56bae2dSConor Dooley } 544b56bae2dSConor Dooley 545b56bae2dSConor Dooley return devm_add_action_or_reset(clk_data->dev, mpfs_reset_unregister_adev, adev); 546b56bae2dSConor Dooley } 547b56bae2dSConor Dooley 548b56bae2dSConor Dooley #else /* !CONFIG_RESET_CONTROLLER */ 549b56bae2dSConor Dooley 550b56bae2dSConor Dooley static int mpfs_reset_controller_register(struct mpfs_clock_data *clk_data) 551b56bae2dSConor Dooley { 552b56bae2dSConor Dooley return 0; 553b56bae2dSConor Dooley } 554b56bae2dSConor Dooley 555b56bae2dSConor Dooley #endif /* !CONFIG_RESET_CONTROLLER */ 556b56bae2dSConor Dooley 557635e5e73SDaire McNamara static int mpfs_clk_probe(struct platform_device *pdev) 558635e5e73SDaire McNamara { 559635e5e73SDaire McNamara struct device *dev = &pdev->dev; 560635e5e73SDaire McNamara struct mpfs_clock_data *clk_data; 561635e5e73SDaire McNamara unsigned int num_clks; 562635e5e73SDaire McNamara int ret; 563635e5e73SDaire McNamara 564445c2da8SConor Dooley /* CLK_RESERVED is not part of clock arrays, so add 1 */ 565445c2da8SConor Dooley num_clks = ARRAY_SIZE(mpfs_msspll_clks) + ARRAY_SIZE(mpfs_cfg_clks) 566445c2da8SConor Dooley + ARRAY_SIZE(mpfs_periph_clks) + 1; 567635e5e73SDaire McNamara 568635e5e73SDaire McNamara clk_data = devm_kzalloc(dev, struct_size(clk_data, hw_data.hws, num_clks), GFP_KERNEL); 569635e5e73SDaire McNamara if (!clk_data) 570635e5e73SDaire McNamara return -ENOMEM; 571635e5e73SDaire McNamara 572635e5e73SDaire McNamara clk_data->base = devm_platform_ioremap_resource(pdev, 0); 573635e5e73SDaire McNamara if (IS_ERR(clk_data->base)) 574635e5e73SDaire McNamara return PTR_ERR(clk_data->base); 575635e5e73SDaire McNamara 576445c2da8SConor Dooley clk_data->msspll_base = devm_platform_ioremap_resource(pdev, 1); 577445c2da8SConor Dooley if (IS_ERR(clk_data->msspll_base)) 578445c2da8SConor Dooley return PTR_ERR(clk_data->msspll_base); 579445c2da8SConor Dooley 580635e5e73SDaire McNamara clk_data->hw_data.num = num_clks; 581b56bae2dSConor Dooley clk_data->dev = dev; 582b56bae2dSConor Dooley dev_set_drvdata(dev, clk_data); 583635e5e73SDaire McNamara 584445c2da8SConor Dooley ret = mpfs_clk_register_mssplls(dev, mpfs_msspll_clks, ARRAY_SIZE(mpfs_msspll_clks), 585445c2da8SConor Dooley clk_data); 586445c2da8SConor Dooley if (ret) 587445c2da8SConor Dooley return ret; 588445c2da8SConor Dooley 589635e5e73SDaire McNamara ret = mpfs_clk_register_cfgs(dev, mpfs_cfg_clks, ARRAY_SIZE(mpfs_cfg_clks), clk_data); 590635e5e73SDaire McNamara if (ret) 591635e5e73SDaire McNamara return ret; 592635e5e73SDaire McNamara 593635e5e73SDaire McNamara ret = mpfs_clk_register_periphs(dev, mpfs_periph_clks, ARRAY_SIZE(mpfs_periph_clks), 594635e5e73SDaire McNamara clk_data); 595635e5e73SDaire McNamara if (ret) 596635e5e73SDaire McNamara return ret; 597635e5e73SDaire McNamara 598635e5e73SDaire McNamara ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, &clk_data->hw_data); 599635e5e73SDaire McNamara if (ret) 600635e5e73SDaire McNamara return ret; 601635e5e73SDaire McNamara 602b56bae2dSConor Dooley return mpfs_reset_controller_register(clk_data); 603635e5e73SDaire McNamara } 604635e5e73SDaire McNamara 605635e5e73SDaire McNamara static const struct of_device_id mpfs_clk_of_match_table[] = { 606635e5e73SDaire McNamara { .compatible = "microchip,mpfs-clkcfg", }, 607635e5e73SDaire McNamara {} 608635e5e73SDaire McNamara }; 609b56bae2dSConor Dooley MODULE_DEVICE_TABLE(of, mpfs_clk_of_match_table); 610635e5e73SDaire McNamara 611635e5e73SDaire McNamara static struct platform_driver mpfs_clk_driver = { 612635e5e73SDaire McNamara .probe = mpfs_clk_probe, 613635e5e73SDaire McNamara .driver = { 614635e5e73SDaire McNamara .name = "microchip-mpfs-clkcfg", 615635e5e73SDaire McNamara .of_match_table = mpfs_clk_of_match_table, 616635e5e73SDaire McNamara }, 617635e5e73SDaire McNamara }; 618635e5e73SDaire McNamara 619635e5e73SDaire McNamara static int __init clk_mpfs_init(void) 620635e5e73SDaire McNamara { 621635e5e73SDaire McNamara return platform_driver_register(&mpfs_clk_driver); 622635e5e73SDaire McNamara } 623635e5e73SDaire McNamara core_initcall(clk_mpfs_init); 624635e5e73SDaire McNamara 625635e5e73SDaire McNamara static void __exit clk_mpfs_exit(void) 626635e5e73SDaire McNamara { 627635e5e73SDaire McNamara platform_driver_unregister(&mpfs_clk_driver); 628635e5e73SDaire McNamara } 629635e5e73SDaire McNamara module_exit(clk_mpfs_exit); 630635e5e73SDaire McNamara 631635e5e73SDaire McNamara MODULE_DESCRIPTION("Microchip PolarFire SoC Clock Driver"); 632635e5e73SDaire McNamara MODULE_LICENSE("GPL v2"); 633