15eda5d79SJoel Stanley // SPDX-License-Identifier: GPL-2.0+ 25eda5d79SJoel Stanley 35eda5d79SJoel Stanley #define pr_fmt(fmt) "clk-aspeed: " fmt 45eda5d79SJoel Stanley 55eda5d79SJoel Stanley #include <linux/clk-provider.h> 65eda5d79SJoel Stanley #include <linux/mfd/syscon.h> 75eda5d79SJoel Stanley #include <linux/of_address.h> 898f3118dSJoel Stanley #include <linux/of_device.h> 998f3118dSJoel Stanley #include <linux/platform_device.h> 105eda5d79SJoel Stanley #include <linux/regmap.h> 115eda5d79SJoel Stanley #include <linux/slab.h> 125eda5d79SJoel Stanley #include <linux/spinlock.h> 135eda5d79SJoel Stanley 145eda5d79SJoel Stanley #include <dt-bindings/clock/aspeed-clock.h> 155eda5d79SJoel Stanley 165eda5d79SJoel Stanley #define ASPEED_NUM_CLKS 35 175eda5d79SJoel Stanley 1899d01e0eSJoel Stanley #define ASPEED_RESET_CTRL 0x04 1999d01e0eSJoel Stanley #define ASPEED_CLK_SELECTION 0x08 2099d01e0eSJoel Stanley #define ASPEED_CLK_STOP_CTRL 0x0c 2199d01e0eSJoel Stanley #define ASPEED_MPLL_PARAM 0x20 2299d01e0eSJoel Stanley #define ASPEED_HPLL_PARAM 0x24 2399d01e0eSJoel Stanley #define AST2500_HPLL_BYPASS_EN BIT(20) 2499d01e0eSJoel Stanley #define AST2400_HPLL_STRAPPED BIT(18) 2599d01e0eSJoel Stanley #define AST2400_HPLL_BYPASS_EN BIT(17) 2699d01e0eSJoel Stanley #define ASPEED_MISC_CTRL 0x2c 2799d01e0eSJoel Stanley #define UART_DIV13_EN BIT(12) 285eda5d79SJoel Stanley #define ASPEED_STRAP 0x70 2999d01e0eSJoel Stanley #define CLKIN_25MHZ_EN BIT(23) 3099d01e0eSJoel Stanley #define AST2400_CLK_SOURCE_SEL BIT(18) 3199d01e0eSJoel Stanley #define ASPEED_CLK_SELECTION_2 0xd8 3299d01e0eSJoel Stanley 3399d01e0eSJoel Stanley /* Globally visible clocks */ 3499d01e0eSJoel Stanley static DEFINE_SPINLOCK(aspeed_clk_lock); 355eda5d79SJoel Stanley 365eda5d79SJoel Stanley /* Keeps track of all clocks */ 375eda5d79SJoel Stanley static struct clk_hw_onecell_data *aspeed_clk_data; 385eda5d79SJoel Stanley 395eda5d79SJoel Stanley static void __iomem *scu_base; 405eda5d79SJoel Stanley 415eda5d79SJoel Stanley /** 425eda5d79SJoel Stanley * struct aspeed_gate_data - Aspeed gated clocks 435eda5d79SJoel Stanley * @clock_idx: bit used to gate this clock in the clock register 445eda5d79SJoel Stanley * @reset_idx: bit used to reset this IP in the reset register. -1 if no 455eda5d79SJoel Stanley * reset is required when enabling the clock 465eda5d79SJoel Stanley * @name: the clock name 475eda5d79SJoel Stanley * @parent_name: the name of the parent clock 485eda5d79SJoel Stanley * @flags: standard clock framework flags 495eda5d79SJoel Stanley */ 505eda5d79SJoel Stanley struct aspeed_gate_data { 515eda5d79SJoel Stanley u8 clock_idx; 525eda5d79SJoel Stanley s8 reset_idx; 535eda5d79SJoel Stanley const char *name; 545eda5d79SJoel Stanley const char *parent_name; 555eda5d79SJoel Stanley unsigned long flags; 565eda5d79SJoel Stanley }; 575eda5d79SJoel Stanley 585eda5d79SJoel Stanley /** 595eda5d79SJoel Stanley * struct aspeed_clk_gate - Aspeed specific clk_gate structure 605eda5d79SJoel Stanley * @hw: handle between common and hardware-specific interfaces 615eda5d79SJoel Stanley * @reg: register controlling gate 625eda5d79SJoel Stanley * @clock_idx: bit used to gate this clock in the clock register 635eda5d79SJoel Stanley * @reset_idx: bit used to reset this IP in the reset register. -1 if no 645eda5d79SJoel Stanley * reset is required when enabling the clock 655eda5d79SJoel Stanley * @flags: hardware-specific flags 665eda5d79SJoel Stanley * @lock: register lock 675eda5d79SJoel Stanley * 685eda5d79SJoel Stanley * Some of the clocks in the Aspeed SoC must be put in reset before enabling. 695eda5d79SJoel Stanley * This modified version of clk_gate allows an optional reset bit to be 705eda5d79SJoel Stanley * specified. 715eda5d79SJoel Stanley */ 725eda5d79SJoel Stanley struct aspeed_clk_gate { 735eda5d79SJoel Stanley struct clk_hw hw; 745eda5d79SJoel Stanley struct regmap *map; 755eda5d79SJoel Stanley u8 clock_idx; 765eda5d79SJoel Stanley s8 reset_idx; 775eda5d79SJoel Stanley u8 flags; 785eda5d79SJoel Stanley spinlock_t *lock; 795eda5d79SJoel Stanley }; 805eda5d79SJoel Stanley 815eda5d79SJoel Stanley #define to_aspeed_clk_gate(_hw) container_of(_hw, struct aspeed_clk_gate, hw) 825eda5d79SJoel Stanley 835eda5d79SJoel Stanley /* TODO: ask Aspeed about the actual parent data */ 845eda5d79SJoel Stanley static const struct aspeed_gate_data aspeed_gates[] = { 855eda5d79SJoel Stanley /* clk rst name parent flags */ 865eda5d79SJoel Stanley [ASPEED_CLK_GATE_ECLK] = { 0, -1, "eclk-gate", "eclk", 0 }, /* Video Engine */ 875eda5d79SJoel Stanley [ASPEED_CLK_GATE_GCLK] = { 1, 7, "gclk-gate", NULL, 0 }, /* 2D engine */ 885eda5d79SJoel Stanley [ASPEED_CLK_GATE_MCLK] = { 2, -1, "mclk-gate", "mpll", CLK_IS_CRITICAL }, /* SDRAM */ 895eda5d79SJoel Stanley [ASPEED_CLK_GATE_VCLK] = { 3, 6, "vclk-gate", NULL, 0 }, /* Video Capture */ 905eda5d79SJoel Stanley [ASPEED_CLK_GATE_BCLK] = { 4, 10, "bclk-gate", "bclk", 0 }, /* PCIe/PCI */ 915eda5d79SJoel Stanley [ASPEED_CLK_GATE_DCLK] = { 5, -1, "dclk-gate", NULL, 0 }, /* DAC */ 925eda5d79SJoel Stanley [ASPEED_CLK_GATE_REFCLK] = { 6, -1, "refclk-gate", "clkin", CLK_IS_CRITICAL }, 935eda5d79SJoel Stanley [ASPEED_CLK_GATE_USBPORT2CLK] = { 7, 3, "usb-port2-gate", NULL, 0 }, /* USB2.0 Host port 2 */ 945eda5d79SJoel Stanley [ASPEED_CLK_GATE_LCLK] = { 8, 5, "lclk-gate", NULL, 0 }, /* LPC */ 955eda5d79SJoel Stanley [ASPEED_CLK_GATE_USBUHCICLK] = { 9, 15, "usb-uhci-gate", NULL, 0 }, /* USB1.1 (requires port 2 enabled) */ 965eda5d79SJoel Stanley [ASPEED_CLK_GATE_D1CLK] = { 10, 13, "d1clk-gate", NULL, 0 }, /* GFX CRT */ 975eda5d79SJoel Stanley [ASPEED_CLK_GATE_YCLK] = { 13, 4, "yclk-gate", NULL, 0 }, /* HAC */ 985eda5d79SJoel Stanley [ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate", NULL, 0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */ 995eda5d79SJoel Stanley [ASPEED_CLK_GATE_UART1CLK] = { 15, -1, "uart1clk-gate", "uart", 0 }, /* UART1 */ 1005eda5d79SJoel Stanley [ASPEED_CLK_GATE_UART2CLK] = { 16, -1, "uart2clk-gate", "uart", 0 }, /* UART2 */ 1015eda5d79SJoel Stanley [ASPEED_CLK_GATE_UART5CLK] = { 17, -1, "uart5clk-gate", "uart", 0 }, /* UART5 */ 1025eda5d79SJoel Stanley [ASPEED_CLK_GATE_ESPICLK] = { 19, -1, "espiclk-gate", NULL, 0 }, /* eSPI */ 1035eda5d79SJoel Stanley [ASPEED_CLK_GATE_MAC1CLK] = { 20, 11, "mac1clk-gate", "mac", 0 }, /* MAC1 */ 1045eda5d79SJoel Stanley [ASPEED_CLK_GATE_MAC2CLK] = { 21, 12, "mac2clk-gate", "mac", 0 }, /* MAC2 */ 1055eda5d79SJoel Stanley [ASPEED_CLK_GATE_RSACLK] = { 24, -1, "rsaclk-gate", NULL, 0 }, /* RSA */ 1065eda5d79SJoel Stanley [ASPEED_CLK_GATE_UART3CLK] = { 25, -1, "uart3clk-gate", "uart", 0 }, /* UART3 */ 1075eda5d79SJoel Stanley [ASPEED_CLK_GATE_UART4CLK] = { 26, -1, "uart4clk-gate", "uart", 0 }, /* UART4 */ 1085eda5d79SJoel Stanley [ASPEED_CLK_GATE_SDCLKCLK] = { 27, 16, "sdclk-gate", NULL, 0 }, /* SDIO/SD */ 1095eda5d79SJoel Stanley [ASPEED_CLK_GATE_LHCCLK] = { 28, -1, "lhclk-gate", "lhclk", 0 }, /* LPC master/LPC+ */ 1105eda5d79SJoel Stanley }; 1115eda5d79SJoel Stanley 11298f3118dSJoel Stanley static const struct clk_div_table ast2500_mac_div_table[] = { 11398f3118dSJoel Stanley { 0x0, 4 }, /* Yep, really. Aspeed confirmed this is correct */ 11498f3118dSJoel Stanley { 0x1, 4 }, 11598f3118dSJoel Stanley { 0x2, 6 }, 11698f3118dSJoel Stanley { 0x3, 8 }, 11798f3118dSJoel Stanley { 0x4, 10 }, 11898f3118dSJoel Stanley { 0x5, 12 }, 11998f3118dSJoel Stanley { 0x6, 14 }, 12098f3118dSJoel Stanley { 0x7, 16 }, 12198f3118dSJoel Stanley { 0 } 12298f3118dSJoel Stanley }; 12398f3118dSJoel Stanley 12499d01e0eSJoel Stanley static const struct clk_div_table ast2400_div_table[] = { 12599d01e0eSJoel Stanley { 0x0, 2 }, 12699d01e0eSJoel Stanley { 0x1, 4 }, 12799d01e0eSJoel Stanley { 0x2, 6 }, 12899d01e0eSJoel Stanley { 0x3, 8 }, 12999d01e0eSJoel Stanley { 0x4, 10 }, 13099d01e0eSJoel Stanley { 0x5, 12 }, 13199d01e0eSJoel Stanley { 0x6, 14 }, 13299d01e0eSJoel Stanley { 0x7, 16 }, 13399d01e0eSJoel Stanley { 0 } 13499d01e0eSJoel Stanley }; 13599d01e0eSJoel Stanley 13699d01e0eSJoel Stanley static const struct clk_div_table ast2500_div_table[] = { 13799d01e0eSJoel Stanley { 0x0, 4 }, 13899d01e0eSJoel Stanley { 0x1, 8 }, 13999d01e0eSJoel Stanley { 0x2, 12 }, 14099d01e0eSJoel Stanley { 0x3, 16 }, 14199d01e0eSJoel Stanley { 0x4, 20 }, 14299d01e0eSJoel Stanley { 0x5, 24 }, 14399d01e0eSJoel Stanley { 0x6, 28 }, 14499d01e0eSJoel Stanley { 0x7, 32 }, 14599d01e0eSJoel Stanley { 0 } 14699d01e0eSJoel Stanley }; 14799d01e0eSJoel Stanley 14899d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2400_calc_pll(const char *name, u32 val) 14999d01e0eSJoel Stanley { 15099d01e0eSJoel Stanley unsigned int mult, div; 15199d01e0eSJoel Stanley 15299d01e0eSJoel Stanley if (val & AST2400_HPLL_BYPASS_EN) { 15399d01e0eSJoel Stanley /* Pass through mode */ 15499d01e0eSJoel Stanley mult = div = 1; 15599d01e0eSJoel Stanley } else { 15699d01e0eSJoel Stanley /* F = 24Mhz * (2-OD) * [(N + 2) / (D + 1)] */ 15799d01e0eSJoel Stanley u32 n = (val >> 5) & 0x3f; 15899d01e0eSJoel Stanley u32 od = (val >> 4) & 0x1; 15999d01e0eSJoel Stanley u32 d = val & 0xf; 16099d01e0eSJoel Stanley 16199d01e0eSJoel Stanley mult = (2 - od) * (n + 2); 16299d01e0eSJoel Stanley div = d + 1; 16399d01e0eSJoel Stanley } 16499d01e0eSJoel Stanley return clk_hw_register_fixed_factor(NULL, name, "clkin", 0, 16599d01e0eSJoel Stanley mult, div); 16699d01e0eSJoel Stanley }; 16799d01e0eSJoel Stanley 16899d01e0eSJoel Stanley static struct clk_hw *aspeed_ast2500_calc_pll(const char *name, u32 val) 16999d01e0eSJoel Stanley { 17099d01e0eSJoel Stanley unsigned int mult, div; 17199d01e0eSJoel Stanley 17299d01e0eSJoel Stanley if (val & AST2500_HPLL_BYPASS_EN) { 17399d01e0eSJoel Stanley /* Pass through mode */ 17499d01e0eSJoel Stanley mult = div = 1; 17599d01e0eSJoel Stanley } else { 17699d01e0eSJoel Stanley /* F = clkin * [(M+1) / (N+1)] / (P + 1) */ 17799d01e0eSJoel Stanley u32 p = (val >> 13) & 0x3f; 17899d01e0eSJoel Stanley u32 m = (val >> 5) & 0xff; 17999d01e0eSJoel Stanley u32 n = val & 0x1f; 18099d01e0eSJoel Stanley 18199d01e0eSJoel Stanley mult = (m + 1) / (n + 1); 18299d01e0eSJoel Stanley div = p + 1; 18399d01e0eSJoel Stanley } 18499d01e0eSJoel Stanley 18599d01e0eSJoel Stanley return clk_hw_register_fixed_factor(NULL, name, "clkin", 0, 18699d01e0eSJoel Stanley mult, div); 18799d01e0eSJoel Stanley } 18899d01e0eSJoel Stanley 18998f3118dSJoel Stanley struct aspeed_clk_soc_data { 19098f3118dSJoel Stanley const struct clk_div_table *div_table; 19198f3118dSJoel Stanley const struct clk_div_table *mac_div_table; 19298f3118dSJoel Stanley struct clk_hw *(*calc_pll)(const char *name, u32 val); 19398f3118dSJoel Stanley }; 19498f3118dSJoel Stanley 19598f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2500_data = { 19698f3118dSJoel Stanley .div_table = ast2500_div_table, 19798f3118dSJoel Stanley .mac_div_table = ast2500_mac_div_table, 19898f3118dSJoel Stanley .calc_pll = aspeed_ast2500_calc_pll, 19998f3118dSJoel Stanley }; 20098f3118dSJoel Stanley 20198f3118dSJoel Stanley static const struct aspeed_clk_soc_data ast2400_data = { 20298f3118dSJoel Stanley .div_table = ast2400_div_table, 20398f3118dSJoel Stanley .mac_div_table = ast2400_div_table, 20498f3118dSJoel Stanley .calc_pll = aspeed_ast2400_calc_pll, 20598f3118dSJoel Stanley }; 20698f3118dSJoel Stanley 207*15ed8ce5SJoel Stanley static int aspeed_clk_enable(struct clk_hw *hw) 208*15ed8ce5SJoel Stanley { 209*15ed8ce5SJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw); 210*15ed8ce5SJoel Stanley unsigned long flags; 211*15ed8ce5SJoel Stanley u32 clk = BIT(gate->clock_idx); 212*15ed8ce5SJoel Stanley u32 rst = BIT(gate->reset_idx); 213*15ed8ce5SJoel Stanley 214*15ed8ce5SJoel Stanley spin_lock_irqsave(gate->lock, flags); 215*15ed8ce5SJoel Stanley 216*15ed8ce5SJoel Stanley if (gate->reset_idx >= 0) { 217*15ed8ce5SJoel Stanley /* Put IP in reset */ 218*15ed8ce5SJoel Stanley regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst); 219*15ed8ce5SJoel Stanley 220*15ed8ce5SJoel Stanley /* Delay 100us */ 221*15ed8ce5SJoel Stanley udelay(100); 222*15ed8ce5SJoel Stanley } 223*15ed8ce5SJoel Stanley 224*15ed8ce5SJoel Stanley /* Enable clock */ 225*15ed8ce5SJoel Stanley regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, 0); 226*15ed8ce5SJoel Stanley 227*15ed8ce5SJoel Stanley if (gate->reset_idx >= 0) { 228*15ed8ce5SJoel Stanley /* A delay of 10ms is specified by the ASPEED docs */ 229*15ed8ce5SJoel Stanley mdelay(10); 230*15ed8ce5SJoel Stanley 231*15ed8ce5SJoel Stanley /* Take IP out of reset */ 232*15ed8ce5SJoel Stanley regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, 0); 233*15ed8ce5SJoel Stanley } 234*15ed8ce5SJoel Stanley 235*15ed8ce5SJoel Stanley spin_unlock_irqrestore(gate->lock, flags); 236*15ed8ce5SJoel Stanley 237*15ed8ce5SJoel Stanley return 0; 238*15ed8ce5SJoel Stanley } 239*15ed8ce5SJoel Stanley 240*15ed8ce5SJoel Stanley static void aspeed_clk_disable(struct clk_hw *hw) 241*15ed8ce5SJoel Stanley { 242*15ed8ce5SJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw); 243*15ed8ce5SJoel Stanley unsigned long flags; 244*15ed8ce5SJoel Stanley u32 clk = BIT(gate->clock_idx); 245*15ed8ce5SJoel Stanley 246*15ed8ce5SJoel Stanley spin_lock_irqsave(gate->lock, flags); 247*15ed8ce5SJoel Stanley 248*15ed8ce5SJoel Stanley regmap_update_bits(gate->map, ASPEED_CLK_STOP_CTRL, clk, clk); 249*15ed8ce5SJoel Stanley 250*15ed8ce5SJoel Stanley spin_unlock_irqrestore(gate->lock, flags); 251*15ed8ce5SJoel Stanley } 252*15ed8ce5SJoel Stanley 253*15ed8ce5SJoel Stanley static int aspeed_clk_is_enabled(struct clk_hw *hw) 254*15ed8ce5SJoel Stanley { 255*15ed8ce5SJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw); 256*15ed8ce5SJoel Stanley u32 clk = BIT(gate->clock_idx); 257*15ed8ce5SJoel Stanley u32 reg; 258*15ed8ce5SJoel Stanley 259*15ed8ce5SJoel Stanley regmap_read(gate->map, ASPEED_CLK_STOP_CTRL, ®); 260*15ed8ce5SJoel Stanley 261*15ed8ce5SJoel Stanley return (reg & clk) ? 0 : 1; 262*15ed8ce5SJoel Stanley } 263*15ed8ce5SJoel Stanley 264*15ed8ce5SJoel Stanley static const struct clk_ops aspeed_clk_gate_ops = { 265*15ed8ce5SJoel Stanley .enable = aspeed_clk_enable, 266*15ed8ce5SJoel Stanley .disable = aspeed_clk_disable, 267*15ed8ce5SJoel Stanley .is_enabled = aspeed_clk_is_enabled, 268*15ed8ce5SJoel Stanley }; 269*15ed8ce5SJoel Stanley 270*15ed8ce5SJoel Stanley static struct clk_hw *aspeed_clk_hw_register_gate(struct device *dev, 271*15ed8ce5SJoel Stanley const char *name, const char *parent_name, unsigned long flags, 272*15ed8ce5SJoel Stanley struct regmap *map, u8 clock_idx, u8 reset_idx, 273*15ed8ce5SJoel Stanley u8 clk_gate_flags, spinlock_t *lock) 274*15ed8ce5SJoel Stanley { 275*15ed8ce5SJoel Stanley struct aspeed_clk_gate *gate; 276*15ed8ce5SJoel Stanley struct clk_init_data init; 277*15ed8ce5SJoel Stanley struct clk_hw *hw; 278*15ed8ce5SJoel Stanley int ret; 279*15ed8ce5SJoel Stanley 280*15ed8ce5SJoel Stanley gate = kzalloc(sizeof(*gate), GFP_KERNEL); 281*15ed8ce5SJoel Stanley if (!gate) 282*15ed8ce5SJoel Stanley return ERR_PTR(-ENOMEM); 283*15ed8ce5SJoel Stanley 284*15ed8ce5SJoel Stanley init.name = name; 285*15ed8ce5SJoel Stanley init.ops = &aspeed_clk_gate_ops; 286*15ed8ce5SJoel Stanley init.flags = flags; 287*15ed8ce5SJoel Stanley init.parent_names = parent_name ? &parent_name : NULL; 288*15ed8ce5SJoel Stanley init.num_parents = parent_name ? 1 : 0; 289*15ed8ce5SJoel Stanley 290*15ed8ce5SJoel Stanley gate->map = map; 291*15ed8ce5SJoel Stanley gate->clock_idx = clock_idx; 292*15ed8ce5SJoel Stanley gate->reset_idx = reset_idx; 293*15ed8ce5SJoel Stanley gate->flags = clk_gate_flags; 294*15ed8ce5SJoel Stanley gate->lock = lock; 295*15ed8ce5SJoel Stanley gate->hw.init = &init; 296*15ed8ce5SJoel Stanley 297*15ed8ce5SJoel Stanley hw = &gate->hw; 298*15ed8ce5SJoel Stanley ret = clk_hw_register(dev, hw); 299*15ed8ce5SJoel Stanley if (ret) { 300*15ed8ce5SJoel Stanley kfree(gate); 301*15ed8ce5SJoel Stanley hw = ERR_PTR(ret); 302*15ed8ce5SJoel Stanley } 303*15ed8ce5SJoel Stanley 304*15ed8ce5SJoel Stanley return hw; 305*15ed8ce5SJoel Stanley } 306*15ed8ce5SJoel Stanley 30798f3118dSJoel Stanley static int aspeed_clk_probe(struct platform_device *pdev) 30898f3118dSJoel Stanley { 30998f3118dSJoel Stanley const struct aspeed_clk_soc_data *soc_data; 31098f3118dSJoel Stanley struct device *dev = &pdev->dev; 31198f3118dSJoel Stanley struct regmap *map; 31298f3118dSJoel Stanley struct clk_hw *hw; 31398f3118dSJoel Stanley u32 val, rate; 314*15ed8ce5SJoel Stanley int i; 31598f3118dSJoel Stanley 31698f3118dSJoel Stanley map = syscon_node_to_regmap(dev->of_node); 31798f3118dSJoel Stanley if (IS_ERR(map)) { 31898f3118dSJoel Stanley dev_err(dev, "no syscon regmap\n"); 31998f3118dSJoel Stanley return PTR_ERR(map); 32098f3118dSJoel Stanley } 32198f3118dSJoel Stanley 32298f3118dSJoel Stanley /* SoC generations share common layouts but have different divisors */ 32398f3118dSJoel Stanley soc_data = of_device_get_match_data(dev); 32498f3118dSJoel Stanley if (!soc_data) { 32598f3118dSJoel Stanley dev_err(dev, "no match data for platform\n"); 32698f3118dSJoel Stanley return -EINVAL; 32798f3118dSJoel Stanley } 32898f3118dSJoel Stanley 32998f3118dSJoel Stanley /* UART clock div13 setting */ 33098f3118dSJoel Stanley regmap_read(map, ASPEED_MISC_CTRL, &val); 33198f3118dSJoel Stanley if (val & UART_DIV13_EN) 33298f3118dSJoel Stanley rate = 24000000 / 13; 33398f3118dSJoel Stanley else 33498f3118dSJoel Stanley rate = 24000000; 33598f3118dSJoel Stanley /* TODO: Find the parent data for the uart clock */ 33698f3118dSJoel Stanley hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate); 33798f3118dSJoel Stanley if (IS_ERR(hw)) 33898f3118dSJoel Stanley return PTR_ERR(hw); 33998f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_UART] = hw; 34098f3118dSJoel Stanley 34198f3118dSJoel Stanley /* 34298f3118dSJoel Stanley * Memory controller (M-PLL) PLL. This clock is configured by the 34398f3118dSJoel Stanley * bootloader, and is exposed to Linux as a read-only clock rate. 34498f3118dSJoel Stanley */ 34598f3118dSJoel Stanley regmap_read(map, ASPEED_MPLL_PARAM, &val); 34698f3118dSJoel Stanley hw = soc_data->calc_pll("mpll", val); 34798f3118dSJoel Stanley if (IS_ERR(hw)) 34898f3118dSJoel Stanley return PTR_ERR(hw); 34998f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_MPLL] = hw; 35098f3118dSJoel Stanley 35198f3118dSJoel Stanley /* SD/SDIO clock divider (TODO: There's a gate too) */ 35298f3118dSJoel Stanley hw = clk_hw_register_divider_table(dev, "sdio", "hpll", 0, 35398f3118dSJoel Stanley scu_base + ASPEED_CLK_SELECTION, 12, 3, 0, 35498f3118dSJoel Stanley soc_data->div_table, 35598f3118dSJoel Stanley &aspeed_clk_lock); 35698f3118dSJoel Stanley if (IS_ERR(hw)) 35798f3118dSJoel Stanley return PTR_ERR(hw); 35898f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_SDIO] = hw; 35998f3118dSJoel Stanley 36098f3118dSJoel Stanley /* MAC AHB bus clock divider */ 36198f3118dSJoel Stanley hw = clk_hw_register_divider_table(dev, "mac", "hpll", 0, 36298f3118dSJoel Stanley scu_base + ASPEED_CLK_SELECTION, 16, 3, 0, 36398f3118dSJoel Stanley soc_data->mac_div_table, 36498f3118dSJoel Stanley &aspeed_clk_lock); 36598f3118dSJoel Stanley if (IS_ERR(hw)) 36698f3118dSJoel Stanley return PTR_ERR(hw); 36798f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_MAC] = hw; 36898f3118dSJoel Stanley 36998f3118dSJoel Stanley /* LPC Host (LHCLK) clock divider */ 37098f3118dSJoel Stanley hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0, 37198f3118dSJoel Stanley scu_base + ASPEED_CLK_SELECTION, 20, 3, 0, 37298f3118dSJoel Stanley soc_data->div_table, 37398f3118dSJoel Stanley &aspeed_clk_lock); 37498f3118dSJoel Stanley if (IS_ERR(hw)) 37598f3118dSJoel Stanley return PTR_ERR(hw); 37698f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_LHCLK] = hw; 37798f3118dSJoel Stanley 37898f3118dSJoel Stanley /* P-Bus (BCLK) clock divider */ 37998f3118dSJoel Stanley hw = clk_hw_register_divider_table(dev, "bclk", "hpll", 0, 38098f3118dSJoel Stanley scu_base + ASPEED_CLK_SELECTION_2, 0, 2, 0, 38198f3118dSJoel Stanley soc_data->div_table, 38298f3118dSJoel Stanley &aspeed_clk_lock); 38398f3118dSJoel Stanley if (IS_ERR(hw)) 38498f3118dSJoel Stanley return PTR_ERR(hw); 38598f3118dSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_BCLK] = hw; 38698f3118dSJoel Stanley 387*15ed8ce5SJoel Stanley /* 388*15ed8ce5SJoel Stanley * TODO: There are a number of clocks that not included in this driver 389*15ed8ce5SJoel Stanley * as more information is required: 390*15ed8ce5SJoel Stanley * D2-PLL 391*15ed8ce5SJoel Stanley * D-PLL 392*15ed8ce5SJoel Stanley * YCLK 393*15ed8ce5SJoel Stanley * RGMII 394*15ed8ce5SJoel Stanley * RMII 395*15ed8ce5SJoel Stanley * UART[1..5] clock source mux 396*15ed8ce5SJoel Stanley * Video Engine (ECLK) mux and clock divider 397*15ed8ce5SJoel Stanley */ 398*15ed8ce5SJoel Stanley 399*15ed8ce5SJoel Stanley for (i = 0; i < ARRAY_SIZE(aspeed_gates); i++) { 400*15ed8ce5SJoel Stanley const struct aspeed_gate_data *gd = &aspeed_gates[i]; 401*15ed8ce5SJoel Stanley 402*15ed8ce5SJoel Stanley hw = aspeed_clk_hw_register_gate(dev, 403*15ed8ce5SJoel Stanley gd->name, 404*15ed8ce5SJoel Stanley gd->parent_name, 405*15ed8ce5SJoel Stanley gd->flags, 406*15ed8ce5SJoel Stanley map, 407*15ed8ce5SJoel Stanley gd->clock_idx, 408*15ed8ce5SJoel Stanley gd->reset_idx, 409*15ed8ce5SJoel Stanley CLK_GATE_SET_TO_DISABLE, 410*15ed8ce5SJoel Stanley &aspeed_clk_lock); 411*15ed8ce5SJoel Stanley if (IS_ERR(hw)) 412*15ed8ce5SJoel Stanley return PTR_ERR(hw); 413*15ed8ce5SJoel Stanley aspeed_clk_data->hws[i] = hw; 414*15ed8ce5SJoel Stanley } 415*15ed8ce5SJoel Stanley 41698f3118dSJoel Stanley return 0; 41798f3118dSJoel Stanley }; 41898f3118dSJoel Stanley 41998f3118dSJoel Stanley static const struct of_device_id aspeed_clk_dt_ids[] = { 42098f3118dSJoel Stanley { .compatible = "aspeed,ast2400-scu", .data = &ast2400_data }, 42198f3118dSJoel Stanley { .compatible = "aspeed,ast2500-scu", .data = &ast2500_data }, 42298f3118dSJoel Stanley { } 42398f3118dSJoel Stanley }; 42498f3118dSJoel Stanley 42598f3118dSJoel Stanley static struct platform_driver aspeed_clk_driver = { 42698f3118dSJoel Stanley .probe = aspeed_clk_probe, 42798f3118dSJoel Stanley .driver = { 42898f3118dSJoel Stanley .name = "aspeed-clk", 42998f3118dSJoel Stanley .of_match_table = aspeed_clk_dt_ids, 43098f3118dSJoel Stanley .suppress_bind_attrs = true, 43198f3118dSJoel Stanley }, 43298f3118dSJoel Stanley }; 43398f3118dSJoel Stanley builtin_platform_driver(aspeed_clk_driver); 43498f3118dSJoel Stanley 43599d01e0eSJoel Stanley static void __init aspeed_ast2400_cc(struct regmap *map) 43699d01e0eSJoel Stanley { 43799d01e0eSJoel Stanley struct clk_hw *hw; 43899d01e0eSJoel Stanley u32 val, freq, div; 43999d01e0eSJoel Stanley 44099d01e0eSJoel Stanley /* 44199d01e0eSJoel Stanley * CLKIN is the crystal oscillator, 24, 48 or 25MHz selected by 44299d01e0eSJoel Stanley * strapping 44399d01e0eSJoel Stanley */ 44499d01e0eSJoel Stanley regmap_read(map, ASPEED_STRAP, &val); 44599d01e0eSJoel Stanley if (val & CLKIN_25MHZ_EN) 44699d01e0eSJoel Stanley freq = 25000000; 44799d01e0eSJoel Stanley else if (val & AST2400_CLK_SOURCE_SEL) 44899d01e0eSJoel Stanley freq = 48000000; 44999d01e0eSJoel Stanley else 45099d01e0eSJoel Stanley freq = 24000000; 45199d01e0eSJoel Stanley hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq); 45299d01e0eSJoel Stanley pr_debug("clkin @%u MHz\n", freq / 1000000); 45399d01e0eSJoel Stanley 45499d01e0eSJoel Stanley /* 45599d01e0eSJoel Stanley * High-speed PLL clock derived from the crystal. This the CPU clock, 45699d01e0eSJoel Stanley * and we assume that it is enabled 45799d01e0eSJoel Stanley */ 45899d01e0eSJoel Stanley regmap_read(map, ASPEED_HPLL_PARAM, &val); 45999d01e0eSJoel Stanley WARN(val & AST2400_HPLL_STRAPPED, "hpll is strapped not configured"); 46099d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2400_calc_pll("hpll", val); 46199d01e0eSJoel Stanley 46299d01e0eSJoel Stanley /* 46399d01e0eSJoel Stanley * Strap bits 11:10 define the CPU/AHB clock frequency ratio (aka HCLK) 46499d01e0eSJoel Stanley * 00: Select CPU:AHB = 1:1 46599d01e0eSJoel Stanley * 01: Select CPU:AHB = 2:1 46699d01e0eSJoel Stanley * 10: Select CPU:AHB = 4:1 46799d01e0eSJoel Stanley * 11: Select CPU:AHB = 3:1 46899d01e0eSJoel Stanley */ 46999d01e0eSJoel Stanley regmap_read(map, ASPEED_STRAP, &val); 47099d01e0eSJoel Stanley val = (val >> 10) & 0x3; 47199d01e0eSJoel Stanley div = val + 1; 47299d01e0eSJoel Stanley if (div == 3) 47399d01e0eSJoel Stanley div = 4; 47499d01e0eSJoel Stanley else if (div == 4) 47599d01e0eSJoel Stanley div = 3; 47699d01e0eSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div); 47799d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw; 47899d01e0eSJoel Stanley 47999d01e0eSJoel Stanley /* APB clock clock selection register SCU08 (aka PCLK) */ 48099d01e0eSJoel Stanley hw = clk_hw_register_divider_table(NULL, "apb", "hpll", 0, 48199d01e0eSJoel Stanley scu_base + ASPEED_CLK_SELECTION, 23, 3, 0, 48299d01e0eSJoel Stanley ast2400_div_table, 48399d01e0eSJoel Stanley &aspeed_clk_lock); 48499d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_APB] = hw; 48599d01e0eSJoel Stanley } 48699d01e0eSJoel Stanley 48799d01e0eSJoel Stanley static void __init aspeed_ast2500_cc(struct regmap *map) 48899d01e0eSJoel Stanley { 48999d01e0eSJoel Stanley struct clk_hw *hw; 49099d01e0eSJoel Stanley u32 val, freq, div; 49199d01e0eSJoel Stanley 49299d01e0eSJoel Stanley /* CLKIN is the crystal oscillator, 24 or 25MHz selected by strapping */ 49399d01e0eSJoel Stanley regmap_read(map, ASPEED_STRAP, &val); 49499d01e0eSJoel Stanley if (val & CLKIN_25MHZ_EN) 49599d01e0eSJoel Stanley freq = 25000000; 49699d01e0eSJoel Stanley else 49799d01e0eSJoel Stanley freq = 24000000; 49899d01e0eSJoel Stanley hw = clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, freq); 49999d01e0eSJoel Stanley pr_debug("clkin @%u MHz\n", freq / 1000000); 50099d01e0eSJoel Stanley 50199d01e0eSJoel Stanley /* 50299d01e0eSJoel Stanley * High-speed PLL clock derived from the crystal. This the CPU clock, 50399d01e0eSJoel Stanley * and we assume that it is enabled 50499d01e0eSJoel Stanley */ 50599d01e0eSJoel Stanley regmap_read(map, ASPEED_HPLL_PARAM, &val); 50699d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_HPLL] = aspeed_ast2500_calc_pll("hpll", val); 50799d01e0eSJoel Stanley 50899d01e0eSJoel Stanley /* Strap bits 11:9 define the AXI/AHB clock frequency ratio (aka HCLK)*/ 50999d01e0eSJoel Stanley regmap_read(map, ASPEED_STRAP, &val); 51099d01e0eSJoel Stanley val = (val >> 9) & 0x7; 51199d01e0eSJoel Stanley WARN(val == 0, "strapping is zero: cannot determine ahb clock"); 51299d01e0eSJoel Stanley div = 2 * (val + 1); 51399d01e0eSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, div); 51499d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_AHB] = hw; 51599d01e0eSJoel Stanley 51699d01e0eSJoel Stanley /* APB clock clock selection register SCU08 (aka PCLK) */ 51799d01e0eSJoel Stanley regmap_read(map, ASPEED_CLK_SELECTION, &val); 51899d01e0eSJoel Stanley val = (val >> 23) & 0x7; 51999d01e0eSJoel Stanley div = 4 * (val + 1); 52099d01e0eSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "apb", "hpll", 0, 1, div); 52199d01e0eSJoel Stanley aspeed_clk_data->hws[ASPEED_CLK_APB] = hw; 52299d01e0eSJoel Stanley }; 52399d01e0eSJoel Stanley 5245eda5d79SJoel Stanley static void __init aspeed_cc_init(struct device_node *np) 5255eda5d79SJoel Stanley { 5265eda5d79SJoel Stanley struct regmap *map; 5275eda5d79SJoel Stanley u32 val; 5285eda5d79SJoel Stanley int ret; 5295eda5d79SJoel Stanley int i; 5305eda5d79SJoel Stanley 5315eda5d79SJoel Stanley scu_base = of_iomap(np, 0); 5325eda5d79SJoel Stanley if (IS_ERR(scu_base)) 5335eda5d79SJoel Stanley return; 5345eda5d79SJoel Stanley 5355eda5d79SJoel Stanley aspeed_clk_data = kzalloc(sizeof(*aspeed_clk_data) + 5365eda5d79SJoel Stanley sizeof(*aspeed_clk_data->hws) * ASPEED_NUM_CLKS, 5375eda5d79SJoel Stanley GFP_KERNEL); 5385eda5d79SJoel Stanley if (!aspeed_clk_data) 5395eda5d79SJoel Stanley return; 5405eda5d79SJoel Stanley 5415eda5d79SJoel Stanley /* 5425eda5d79SJoel Stanley * This way all clocks fetched before the platform device probes, 5435eda5d79SJoel Stanley * except those we assign here for early use, will be deferred. 5445eda5d79SJoel Stanley */ 5455eda5d79SJoel Stanley for (i = 0; i < ASPEED_NUM_CLKS; i++) 5465eda5d79SJoel Stanley aspeed_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER); 5475eda5d79SJoel Stanley 5485eda5d79SJoel Stanley map = syscon_node_to_regmap(np); 5495eda5d79SJoel Stanley if (IS_ERR(map)) { 5505eda5d79SJoel Stanley pr_err("no syscon regmap\n"); 5515eda5d79SJoel Stanley return; 5525eda5d79SJoel Stanley } 5535eda5d79SJoel Stanley /* 5545eda5d79SJoel Stanley * We check that the regmap works on this very first access, 5555eda5d79SJoel Stanley * but as this is an MMIO-backed regmap, subsequent regmap 5565eda5d79SJoel Stanley * access is not going to fail and we skip error checks from 5575eda5d79SJoel Stanley * this point. 5585eda5d79SJoel Stanley */ 5595eda5d79SJoel Stanley ret = regmap_read(map, ASPEED_STRAP, &val); 5605eda5d79SJoel Stanley if (ret) { 5615eda5d79SJoel Stanley pr_err("failed to read strapping register\n"); 5625eda5d79SJoel Stanley return; 5635eda5d79SJoel Stanley } 5645eda5d79SJoel Stanley 56599d01e0eSJoel Stanley if (of_device_is_compatible(np, "aspeed,ast2400-scu")) 56699d01e0eSJoel Stanley aspeed_ast2400_cc(map); 56799d01e0eSJoel Stanley else if (of_device_is_compatible(np, "aspeed,ast2500-scu")) 56899d01e0eSJoel Stanley aspeed_ast2500_cc(map); 56999d01e0eSJoel Stanley else 57099d01e0eSJoel Stanley pr_err("unknown platform, failed to add clocks\n"); 57199d01e0eSJoel Stanley 5725eda5d79SJoel Stanley aspeed_clk_data->num = ASPEED_NUM_CLKS; 5735eda5d79SJoel Stanley ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_clk_data); 5745eda5d79SJoel Stanley if (ret) 5755eda5d79SJoel Stanley pr_err("failed to add DT provider: %d\n", ret); 5765eda5d79SJoel Stanley }; 5775eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g5, "aspeed,ast2500-scu", aspeed_cc_init); 5785eda5d79SJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g4, "aspeed,ast2400-scu", aspeed_cc_init); 579