162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Hisilicon Hi3559A clock driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2019-2020, Huawei Tech. Co., Ltd. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Author: Dongjiu Geng <gengdongjiu@huawei.com> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/clk-provider.h> 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/of.h> 1362306a36Sopenharmony_ci#include <linux/platform_device.h> 1462306a36Sopenharmony_ci#include <linux/slab.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include <dt-bindings/clock/hi3559av100-clock.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#include "clk.h" 1962306a36Sopenharmony_ci#include "crg.h" 2062306a36Sopenharmony_ci#include "reset.h" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#define CRG_BASE_ADDR 0x18020000 2362306a36Sopenharmony_ci#define PLL_MASK_WIDTH 24 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_cistruct hi3559av100_pll_clock { 2662306a36Sopenharmony_ci u32 id; 2762306a36Sopenharmony_ci const char *name; 2862306a36Sopenharmony_ci const char *parent_name; 2962306a36Sopenharmony_ci const u32 ctrl_reg1; 3062306a36Sopenharmony_ci const u8 frac_shift; 3162306a36Sopenharmony_ci const u8 frac_width; 3262306a36Sopenharmony_ci const u8 postdiv1_shift; 3362306a36Sopenharmony_ci const u8 postdiv1_width; 3462306a36Sopenharmony_ci const u8 postdiv2_shift; 3562306a36Sopenharmony_ci const u8 postdiv2_width; 3662306a36Sopenharmony_ci const u32 ctrl_reg2; 3762306a36Sopenharmony_ci const u8 fbdiv_shift; 3862306a36Sopenharmony_ci const u8 fbdiv_width; 3962306a36Sopenharmony_ci const u8 refdiv_shift; 4062306a36Sopenharmony_ci const u8 refdiv_width; 4162306a36Sopenharmony_ci}; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistruct hi3559av100_clk_pll { 4462306a36Sopenharmony_ci struct clk_hw hw; 4562306a36Sopenharmony_ci u32 id; 4662306a36Sopenharmony_ci void __iomem *ctrl_reg1; 4762306a36Sopenharmony_ci u8 frac_shift; 4862306a36Sopenharmony_ci u8 frac_width; 4962306a36Sopenharmony_ci u8 postdiv1_shift; 5062306a36Sopenharmony_ci u8 postdiv1_width; 5162306a36Sopenharmony_ci u8 postdiv2_shift; 5262306a36Sopenharmony_ci u8 postdiv2_width; 5362306a36Sopenharmony_ci void __iomem *ctrl_reg2; 5462306a36Sopenharmony_ci u8 fbdiv_shift; 5562306a36Sopenharmony_ci u8 fbdiv_width; 5662306a36Sopenharmony_ci u8 refdiv_shift; 5762306a36Sopenharmony_ci u8 refdiv_width; 5862306a36Sopenharmony_ci}; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci/* soc clk config */ 6162306a36Sopenharmony_cistatic const struct hisi_fixed_rate_clock hi3559av100_fixed_rate_clks_crg[] = { 6262306a36Sopenharmony_ci { HI3559AV100_FIXED_1188M, "1188m", NULL, 0, 1188000000, }, 6362306a36Sopenharmony_ci { HI3559AV100_FIXED_1000M, "1000m", NULL, 0, 1000000000, }, 6462306a36Sopenharmony_ci { HI3559AV100_FIXED_842M, "842m", NULL, 0, 842000000, }, 6562306a36Sopenharmony_ci { HI3559AV100_FIXED_792M, "792m", NULL, 0, 792000000, }, 6662306a36Sopenharmony_ci { HI3559AV100_FIXED_750M, "750m", NULL, 0, 750000000, }, 6762306a36Sopenharmony_ci { HI3559AV100_FIXED_710M, "710m", NULL, 0, 710000000, }, 6862306a36Sopenharmony_ci { HI3559AV100_FIXED_680M, "680m", NULL, 0, 680000000, }, 6962306a36Sopenharmony_ci { HI3559AV100_FIXED_667M, "667m", NULL, 0, 667000000, }, 7062306a36Sopenharmony_ci { HI3559AV100_FIXED_631M, "631m", NULL, 0, 631000000, }, 7162306a36Sopenharmony_ci { HI3559AV100_FIXED_600M, "600m", NULL, 0, 600000000, }, 7262306a36Sopenharmony_ci { HI3559AV100_FIXED_568M, "568m", NULL, 0, 568000000, }, 7362306a36Sopenharmony_ci { HI3559AV100_FIXED_500M, "500m", NULL, 0, 500000000, }, 7462306a36Sopenharmony_ci { HI3559AV100_FIXED_475M, "475m", NULL, 0, 475000000, }, 7562306a36Sopenharmony_ci { HI3559AV100_FIXED_428M, "428m", NULL, 0, 428000000, }, 7662306a36Sopenharmony_ci { HI3559AV100_FIXED_400M, "400m", NULL, 0, 400000000, }, 7762306a36Sopenharmony_ci { HI3559AV100_FIXED_396M, "396m", NULL, 0, 396000000, }, 7862306a36Sopenharmony_ci { HI3559AV100_FIXED_300M, "300m", NULL, 0, 300000000, }, 7962306a36Sopenharmony_ci { HI3559AV100_FIXED_250M, "250m", NULL, 0, 250000000, }, 8062306a36Sopenharmony_ci { HI3559AV100_FIXED_200M, "200m", NULL, 0, 200000000, }, 8162306a36Sopenharmony_ci { HI3559AV100_FIXED_198M, "198m", NULL, 0, 198000000, }, 8262306a36Sopenharmony_ci { HI3559AV100_FIXED_187p5M, "187p5m", NULL, 0, 187500000, }, 8362306a36Sopenharmony_ci { HI3559AV100_FIXED_150M, "150m", NULL, 0, 150000000, }, 8462306a36Sopenharmony_ci { HI3559AV100_FIXED_148p5M, "148p5m", NULL, 0, 1485000000, }, 8562306a36Sopenharmony_ci { HI3559AV100_FIXED_125M, "125m", NULL, 0, 125000000, }, 8662306a36Sopenharmony_ci { HI3559AV100_FIXED_107M, "107m", NULL, 0, 107000000, }, 8762306a36Sopenharmony_ci { HI3559AV100_FIXED_100M, "100m", NULL, 0, 100000000, }, 8862306a36Sopenharmony_ci { HI3559AV100_FIXED_99M, "99m", NULL, 0, 99000000, }, 8962306a36Sopenharmony_ci { HI3559AV100_FIXED_75M, "75m", NULL, 0, 75000000, }, 9062306a36Sopenharmony_ci { HI3559AV100_FIXED_74p25M, "74p25m", NULL, 0, 74250000, }, 9162306a36Sopenharmony_ci { HI3559AV100_FIXED_72M, "72m", NULL, 0, 72000000, }, 9262306a36Sopenharmony_ci { HI3559AV100_FIXED_60M, "60m", NULL, 0, 60000000, }, 9362306a36Sopenharmony_ci { HI3559AV100_FIXED_54M, "54m", NULL, 0, 54000000, }, 9462306a36Sopenharmony_ci { HI3559AV100_FIXED_50M, "50m", NULL, 0, 50000000, }, 9562306a36Sopenharmony_ci { HI3559AV100_FIXED_49p5M, "49p5m", NULL, 0, 49500000, }, 9662306a36Sopenharmony_ci { HI3559AV100_FIXED_37p125M, "37p125m", NULL, 0, 37125000, }, 9762306a36Sopenharmony_ci { HI3559AV100_FIXED_36M, "36m", NULL, 0, 36000000, }, 9862306a36Sopenharmony_ci { HI3559AV100_FIXED_32p4M, "32p4m", NULL, 0, 32400000, }, 9962306a36Sopenharmony_ci { HI3559AV100_FIXED_27M, "27m", NULL, 0, 27000000, }, 10062306a36Sopenharmony_ci { HI3559AV100_FIXED_25M, "25m", NULL, 0, 25000000, }, 10162306a36Sopenharmony_ci { HI3559AV100_FIXED_24M, "24m", NULL, 0, 24000000, }, 10262306a36Sopenharmony_ci { HI3559AV100_FIXED_12M, "12m", NULL, 0, 12000000, }, 10362306a36Sopenharmony_ci { HI3559AV100_FIXED_3M, "3m", NULL, 0, 3000000, }, 10462306a36Sopenharmony_ci { HI3559AV100_FIXED_1p6M, "1p6m", NULL, 0, 1600000, }, 10562306a36Sopenharmony_ci { HI3559AV100_FIXED_400K, "400k", NULL, 0, 400000, }, 10662306a36Sopenharmony_ci { HI3559AV100_FIXED_100K, "100k", NULL, 0, 100000, }, 10762306a36Sopenharmony_ci}; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cistatic const char *fmc_mux_p[] = { 11162306a36Sopenharmony_ci "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m" 11262306a36Sopenharmony_ci}; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_cistatic const char *mmc_mux_p[] = { 11562306a36Sopenharmony_ci "100k", "25m", "49p5m", "99m", "187p5m", "150m", "198m", "400k" 11662306a36Sopenharmony_ci}; 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_cistatic const char *sysapb_mux_p[] = { 11962306a36Sopenharmony_ci "24m", "50m", 12062306a36Sopenharmony_ci}; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_cistatic const char *sysbus_mux_p[] = { 12362306a36Sopenharmony_ci "24m", "300m" 12462306a36Sopenharmony_ci}; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic const char *uart_mux_p[] = { "50m", "24m", "3m" }; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_cistatic const char *a73_clksel_mux_p[] = { 12962306a36Sopenharmony_ci "24m", "apll", "1000m" 13062306a36Sopenharmony_ci}; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_cistatic const u32 fmc_mux_table[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; 13362306a36Sopenharmony_cistatic const u32 mmc_mux_table[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; 13462306a36Sopenharmony_cistatic const u32 sysapb_mux_table[] = { 0, 1 }; 13562306a36Sopenharmony_cistatic const u32 sysbus_mux_table[] = { 0, 1 }; 13662306a36Sopenharmony_cistatic const u32 uart_mux_table[] = { 0, 1, 2 }; 13762306a36Sopenharmony_cistatic const u32 a73_clksel_mux_table[] = { 0, 1, 2 }; 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cistatic struct hisi_mux_clock hi3559av100_mux_clks_crg[] = { 14062306a36Sopenharmony_ci { 14162306a36Sopenharmony_ci HI3559AV100_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p), 14262306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x170, 2, 3, 0, fmc_mux_table, 14362306a36Sopenharmony_ci }, 14462306a36Sopenharmony_ci { 14562306a36Sopenharmony_ci HI3559AV100_MMC0_MUX, "mmc0_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p), 14662306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x1a8, 24, 3, 0, mmc_mux_table, 14762306a36Sopenharmony_ci }, 14862306a36Sopenharmony_ci { 14962306a36Sopenharmony_ci HI3559AV100_MMC1_MUX, "mmc1_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p), 15062306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x1ec, 24, 3, 0, mmc_mux_table, 15162306a36Sopenharmony_ci }, 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci { 15462306a36Sopenharmony_ci HI3559AV100_MMC2_MUX, "mmc2_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p), 15562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x214, 24, 3, 0, mmc_mux_table, 15662306a36Sopenharmony_ci }, 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci { 15962306a36Sopenharmony_ci HI3559AV100_MMC3_MUX, "mmc3_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p), 16062306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x23c, 24, 3, 0, mmc_mux_table, 16162306a36Sopenharmony_ci }, 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci { 16462306a36Sopenharmony_ci HI3559AV100_SYSAPB_MUX, "sysapb_mux", sysapb_mux_p, ARRAY_SIZE(sysapb_mux_p), 16562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0xe8, 3, 1, 0, sysapb_mux_table 16662306a36Sopenharmony_ci }, 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci { 16962306a36Sopenharmony_ci HI3559AV100_SYSBUS_MUX, "sysbus_mux", sysbus_mux_p, ARRAY_SIZE(sysbus_mux_p), 17062306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0xe8, 0, 1, 0, sysbus_mux_table 17162306a36Sopenharmony_ci }, 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci { 17462306a36Sopenharmony_ci HI3559AV100_UART_MUX, "uart_mux", uart_mux_p, ARRAY_SIZE(uart_mux_p), 17562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 28, 2, 0, uart_mux_table 17662306a36Sopenharmony_ci }, 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci { 17962306a36Sopenharmony_ci HI3559AV100_A73_MUX, "a73_mux", a73_clksel_mux_p, ARRAY_SIZE(a73_clksel_mux_p), 18062306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0xe4, 0, 2, 0, a73_clksel_mux_table 18162306a36Sopenharmony_ci }, 18262306a36Sopenharmony_ci}; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_cistatic struct hisi_gate_clock hi3559av100_gate_clks[] = { 18562306a36Sopenharmony_ci { 18662306a36Sopenharmony_ci HI3559AV100_FMC_CLK, "clk_fmc", "fmc_mux", 18762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x170, 1, 0, 18862306a36Sopenharmony_ci }, 18962306a36Sopenharmony_ci { 19062306a36Sopenharmony_ci HI3559AV100_MMC0_CLK, "clk_mmc0", "mmc0_mux", 19162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x1a8, 28, 0, 19262306a36Sopenharmony_ci }, 19362306a36Sopenharmony_ci { 19462306a36Sopenharmony_ci HI3559AV100_MMC1_CLK, "clk_mmc1", "mmc1_mux", 19562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x1ec, 28, 0, 19662306a36Sopenharmony_ci }, 19762306a36Sopenharmony_ci { 19862306a36Sopenharmony_ci HI3559AV100_MMC2_CLK, "clk_mmc2", "mmc2_mux", 19962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x214, 28, 0, 20062306a36Sopenharmony_ci }, 20162306a36Sopenharmony_ci { 20262306a36Sopenharmony_ci HI3559AV100_MMC3_CLK, "clk_mmc3", "mmc3_mux", 20362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x23c, 28, 0, 20462306a36Sopenharmony_ci }, 20562306a36Sopenharmony_ci { 20662306a36Sopenharmony_ci HI3559AV100_UART0_CLK, "clk_uart0", "uart_mux", 20762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 23, 0, 20862306a36Sopenharmony_ci }, 20962306a36Sopenharmony_ci { 21062306a36Sopenharmony_ci HI3559AV100_UART1_CLK, "clk_uart1", "uart_mux", 21162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 24, 0, 21262306a36Sopenharmony_ci }, 21362306a36Sopenharmony_ci { 21462306a36Sopenharmony_ci HI3559AV100_UART2_CLK, "clk_uart2", "uart_mux", 21562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 25, 0, 21662306a36Sopenharmony_ci }, 21762306a36Sopenharmony_ci { 21862306a36Sopenharmony_ci HI3559AV100_UART3_CLK, "clk_uart3", "uart_mux", 21962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 26, 0, 22062306a36Sopenharmony_ci }, 22162306a36Sopenharmony_ci { 22262306a36Sopenharmony_ci HI3559AV100_UART4_CLK, "clk_uart4", "uart_mux", 22362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x198, 27, 0, 22462306a36Sopenharmony_ci }, 22562306a36Sopenharmony_ci { 22662306a36Sopenharmony_ci HI3559AV100_ETH_CLK, "clk_eth", NULL, 22762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0174, 1, 0, 22862306a36Sopenharmony_ci }, 22962306a36Sopenharmony_ci { 23062306a36Sopenharmony_ci HI3559AV100_ETH_MACIF_CLK, "clk_eth_macif", NULL, 23162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0174, 5, 0, 23262306a36Sopenharmony_ci }, 23362306a36Sopenharmony_ci { 23462306a36Sopenharmony_ci HI3559AV100_ETH1_CLK, "clk_eth1", NULL, 23562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0174, 3, 0, 23662306a36Sopenharmony_ci }, 23762306a36Sopenharmony_ci { 23862306a36Sopenharmony_ci HI3559AV100_ETH1_MACIF_CLK, "clk_eth1_macif", NULL, 23962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0174, 7, 0, 24062306a36Sopenharmony_ci }, 24162306a36Sopenharmony_ci { 24262306a36Sopenharmony_ci HI3559AV100_I2C0_CLK, "clk_i2c0", "50m", 24362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 16, 0, 24462306a36Sopenharmony_ci }, 24562306a36Sopenharmony_ci { 24662306a36Sopenharmony_ci HI3559AV100_I2C1_CLK, "clk_i2c1", "50m", 24762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 17, 0, 24862306a36Sopenharmony_ci }, 24962306a36Sopenharmony_ci { 25062306a36Sopenharmony_ci HI3559AV100_I2C2_CLK, "clk_i2c2", "50m", 25162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 18, 0, 25262306a36Sopenharmony_ci }, 25362306a36Sopenharmony_ci { 25462306a36Sopenharmony_ci HI3559AV100_I2C3_CLK, "clk_i2c3", "50m", 25562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 19, 0, 25662306a36Sopenharmony_ci }, 25762306a36Sopenharmony_ci { 25862306a36Sopenharmony_ci HI3559AV100_I2C4_CLK, "clk_i2c4", "50m", 25962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 20, 0, 26062306a36Sopenharmony_ci }, 26162306a36Sopenharmony_ci { 26262306a36Sopenharmony_ci HI3559AV100_I2C5_CLK, "clk_i2c5", "50m", 26362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 21, 0, 26462306a36Sopenharmony_ci }, 26562306a36Sopenharmony_ci { 26662306a36Sopenharmony_ci HI3559AV100_I2C6_CLK, "clk_i2c6", "50m", 26762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 22, 0, 26862306a36Sopenharmony_ci }, 26962306a36Sopenharmony_ci { 27062306a36Sopenharmony_ci HI3559AV100_I2C7_CLK, "clk_i2c7", "50m", 27162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 23, 0, 27262306a36Sopenharmony_ci }, 27362306a36Sopenharmony_ci { 27462306a36Sopenharmony_ci HI3559AV100_I2C8_CLK, "clk_i2c8", "50m", 27562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 24, 0, 27662306a36Sopenharmony_ci }, 27762306a36Sopenharmony_ci { 27862306a36Sopenharmony_ci HI3559AV100_I2C9_CLK, "clk_i2c9", "50m", 27962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 25, 0, 28062306a36Sopenharmony_ci }, 28162306a36Sopenharmony_ci { 28262306a36Sopenharmony_ci HI3559AV100_I2C10_CLK, "clk_i2c10", "50m", 28362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 26, 0, 28462306a36Sopenharmony_ci }, 28562306a36Sopenharmony_ci { 28662306a36Sopenharmony_ci HI3559AV100_I2C11_CLK, "clk_i2c11", "50m", 28762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x01a0, 27, 0, 28862306a36Sopenharmony_ci }, 28962306a36Sopenharmony_ci { 29062306a36Sopenharmony_ci HI3559AV100_SPI0_CLK, "clk_spi0", "100m", 29162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 16, 0, 29262306a36Sopenharmony_ci }, 29362306a36Sopenharmony_ci { 29462306a36Sopenharmony_ci HI3559AV100_SPI1_CLK, "clk_spi1", "100m", 29562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 17, 0, 29662306a36Sopenharmony_ci }, 29762306a36Sopenharmony_ci { 29862306a36Sopenharmony_ci HI3559AV100_SPI2_CLK, "clk_spi2", "100m", 29962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 18, 0, 30062306a36Sopenharmony_ci }, 30162306a36Sopenharmony_ci { 30262306a36Sopenharmony_ci HI3559AV100_SPI3_CLK, "clk_spi3", "100m", 30362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 19, 0, 30462306a36Sopenharmony_ci }, 30562306a36Sopenharmony_ci { 30662306a36Sopenharmony_ci HI3559AV100_SPI4_CLK, "clk_spi4", "100m", 30762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 20, 0, 30862306a36Sopenharmony_ci }, 30962306a36Sopenharmony_ci { 31062306a36Sopenharmony_ci HI3559AV100_SPI5_CLK, "clk_spi5", "100m", 31162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 21, 0, 31262306a36Sopenharmony_ci }, 31362306a36Sopenharmony_ci { 31462306a36Sopenharmony_ci HI3559AV100_SPI6_CLK, "clk_spi6", "100m", 31562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x0198, 22, 0, 31662306a36Sopenharmony_ci }, 31762306a36Sopenharmony_ci { 31862306a36Sopenharmony_ci HI3559AV100_EDMAC_AXICLK, "axi_clk_edmac", NULL, 31962306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x16c, 6, 0, 32062306a36Sopenharmony_ci }, 32162306a36Sopenharmony_ci { 32262306a36Sopenharmony_ci HI3559AV100_EDMAC_CLK, "clk_edmac", NULL, 32362306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x16c, 5, 0, 32462306a36Sopenharmony_ci }, 32562306a36Sopenharmony_ci { 32662306a36Sopenharmony_ci HI3559AV100_EDMAC1_AXICLK, "axi_clk_edmac1", NULL, 32762306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x16c, 9, 0, 32862306a36Sopenharmony_ci }, 32962306a36Sopenharmony_ci { 33062306a36Sopenharmony_ci HI3559AV100_EDMAC1_CLK, "clk_edmac1", NULL, 33162306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x16c, 8, 0, 33262306a36Sopenharmony_ci }, 33362306a36Sopenharmony_ci { 33462306a36Sopenharmony_ci HI3559AV100_VDMAC_CLK, "clk_vdmac", NULL, 33562306a36Sopenharmony_ci CLK_SET_RATE_PARENT, 0x14c, 5, 0, 33662306a36Sopenharmony_ci }, 33762306a36Sopenharmony_ci}; 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_cistatic struct hi3559av100_pll_clock hi3559av100_pll_clks[] = { 34062306a36Sopenharmony_ci { 34162306a36Sopenharmony_ci HI3559AV100_APLL_CLK, "apll", NULL, 0x0, 0, 24, 24, 3, 28, 3, 34262306a36Sopenharmony_ci 0x4, 0, 12, 12, 6 34362306a36Sopenharmony_ci }, 34462306a36Sopenharmony_ci { 34562306a36Sopenharmony_ci HI3559AV100_GPLL_CLK, "gpll", NULL, 0x20, 0, 24, 24, 3, 28, 3, 34662306a36Sopenharmony_ci 0x24, 0, 12, 12, 6 34762306a36Sopenharmony_ci }, 34862306a36Sopenharmony_ci}; 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci#define to_pll_clk(_hw) container_of(_hw, struct hi3559av100_clk_pll, hw) 35162306a36Sopenharmony_cistatic void hi3559av100_calc_pll(u32 *frac_val, u32 *postdiv1_val, 35262306a36Sopenharmony_ci u32 *postdiv2_val, 35362306a36Sopenharmony_ci u32 *fbdiv_val, u32 *refdiv_val, u64 rate) 35462306a36Sopenharmony_ci{ 35562306a36Sopenharmony_ci u64 rem; 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci *postdiv1_val = 2; 35862306a36Sopenharmony_ci *postdiv2_val = 1; 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci rate = rate * ((*postdiv1_val) * (*postdiv2_val)); 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci *frac_val = 0; 36362306a36Sopenharmony_ci rem = do_div(rate, 1000000); 36462306a36Sopenharmony_ci rem = do_div(rate, PLL_MASK_WIDTH); 36562306a36Sopenharmony_ci *fbdiv_val = rate; 36662306a36Sopenharmony_ci *refdiv_val = 1; 36762306a36Sopenharmony_ci rem = rem * (1 << PLL_MASK_WIDTH); 36862306a36Sopenharmony_ci do_div(rem, PLL_MASK_WIDTH); 36962306a36Sopenharmony_ci *frac_val = rem; 37062306a36Sopenharmony_ci} 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_cistatic int clk_pll_set_rate(struct clk_hw *hw, 37362306a36Sopenharmony_ci unsigned long rate, 37462306a36Sopenharmony_ci unsigned long parent_rate) 37562306a36Sopenharmony_ci{ 37662306a36Sopenharmony_ci struct hi3559av100_clk_pll *clk = to_pll_clk(hw); 37762306a36Sopenharmony_ci u32 frac_val, postdiv1_val, postdiv2_val, fbdiv_val, refdiv_val; 37862306a36Sopenharmony_ci u32 val; 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci postdiv1_val = postdiv2_val = 0; 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci hi3559av100_calc_pll(&frac_val, &postdiv1_val, &postdiv2_val, 38362306a36Sopenharmony_ci &fbdiv_val, &refdiv_val, (u64)rate); 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg1); 38662306a36Sopenharmony_ci val &= ~(((1 << clk->frac_width) - 1) << clk->frac_shift); 38762306a36Sopenharmony_ci val &= ~(((1 << clk->postdiv1_width) - 1) << clk->postdiv1_shift); 38862306a36Sopenharmony_ci val &= ~(((1 << clk->postdiv2_width) - 1) << clk->postdiv2_shift); 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci val |= frac_val << clk->frac_shift; 39162306a36Sopenharmony_ci val |= postdiv1_val << clk->postdiv1_shift; 39262306a36Sopenharmony_ci val |= postdiv2_val << clk->postdiv2_shift; 39362306a36Sopenharmony_ci writel_relaxed(val, clk->ctrl_reg1); 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg2); 39662306a36Sopenharmony_ci val &= ~(((1 << clk->fbdiv_width) - 1) << clk->fbdiv_shift); 39762306a36Sopenharmony_ci val &= ~(((1 << clk->refdiv_width) - 1) << clk->refdiv_shift); 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci val |= fbdiv_val << clk->fbdiv_shift; 40062306a36Sopenharmony_ci val |= refdiv_val << clk->refdiv_shift; 40162306a36Sopenharmony_ci writel_relaxed(val, clk->ctrl_reg2); 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ci return 0; 40462306a36Sopenharmony_ci} 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_cistatic unsigned long clk_pll_recalc_rate(struct clk_hw *hw, 40762306a36Sopenharmony_ci unsigned long parent_rate) 40862306a36Sopenharmony_ci{ 40962306a36Sopenharmony_ci struct hi3559av100_clk_pll *clk = to_pll_clk(hw); 41062306a36Sopenharmony_ci u64 frac_val, fbdiv_val, refdiv_val; 41162306a36Sopenharmony_ci u32 postdiv1_val, postdiv2_val; 41262306a36Sopenharmony_ci u32 val; 41362306a36Sopenharmony_ci u64 tmp, rate; 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg1); 41662306a36Sopenharmony_ci val = val >> clk->frac_shift; 41762306a36Sopenharmony_ci val &= ((1 << clk->frac_width) - 1); 41862306a36Sopenharmony_ci frac_val = val; 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg1); 42162306a36Sopenharmony_ci val = val >> clk->postdiv1_shift; 42262306a36Sopenharmony_ci val &= ((1 << clk->postdiv1_width) - 1); 42362306a36Sopenharmony_ci postdiv1_val = val; 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg1); 42662306a36Sopenharmony_ci val = val >> clk->postdiv2_shift; 42762306a36Sopenharmony_ci val &= ((1 << clk->postdiv2_width) - 1); 42862306a36Sopenharmony_ci postdiv2_val = val; 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg2); 43162306a36Sopenharmony_ci val = val >> clk->fbdiv_shift; 43262306a36Sopenharmony_ci val &= ((1 << clk->fbdiv_width) - 1); 43362306a36Sopenharmony_ci fbdiv_val = val; 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci val = readl_relaxed(clk->ctrl_reg2); 43662306a36Sopenharmony_ci val = val >> clk->refdiv_shift; 43762306a36Sopenharmony_ci val &= ((1 << clk->refdiv_width) - 1); 43862306a36Sopenharmony_ci refdiv_val = val; 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci /* rate = 24000000 * (fbdiv + frac / (1<<24) ) / refdiv */ 44162306a36Sopenharmony_ci rate = 0; 44262306a36Sopenharmony_ci tmp = 24000000 * fbdiv_val + (24000000 * frac_val) / (1 << 24); 44362306a36Sopenharmony_ci rate += tmp; 44462306a36Sopenharmony_ci do_div(rate, refdiv_val); 44562306a36Sopenharmony_ci do_div(rate, postdiv1_val * postdiv2_val); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci return rate; 44862306a36Sopenharmony_ci} 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_cistatic const struct clk_ops hisi_clk_pll_ops = { 45162306a36Sopenharmony_ci .set_rate = clk_pll_set_rate, 45262306a36Sopenharmony_ci .recalc_rate = clk_pll_recalc_rate, 45362306a36Sopenharmony_ci}; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_cistatic void hisi_clk_register_pll(struct hi3559av100_pll_clock *clks, 45662306a36Sopenharmony_ci int nums, struct hisi_clock_data *data, struct device *dev) 45762306a36Sopenharmony_ci{ 45862306a36Sopenharmony_ci void __iomem *base = data->base; 45962306a36Sopenharmony_ci struct hi3559av100_clk_pll *p_clk = NULL; 46062306a36Sopenharmony_ci struct clk *clk = NULL; 46162306a36Sopenharmony_ci struct clk_init_data init; 46262306a36Sopenharmony_ci int i; 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ci p_clk = devm_kzalloc(dev, sizeof(*p_clk) * nums, GFP_KERNEL); 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci if (!p_clk) 46762306a36Sopenharmony_ci return; 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci for (i = 0; i < nums; i++) { 47062306a36Sopenharmony_ci init.name = clks[i].name; 47162306a36Sopenharmony_ci init.flags = 0; 47262306a36Sopenharmony_ci init.parent_names = 47362306a36Sopenharmony_ci (clks[i].parent_name ? &clks[i].parent_name : NULL); 47462306a36Sopenharmony_ci init.num_parents = (clks[i].parent_name ? 1 : 0); 47562306a36Sopenharmony_ci init.ops = &hisi_clk_pll_ops; 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci p_clk->ctrl_reg1 = base + clks[i].ctrl_reg1; 47862306a36Sopenharmony_ci p_clk->frac_shift = clks[i].frac_shift; 47962306a36Sopenharmony_ci p_clk->frac_width = clks[i].frac_width; 48062306a36Sopenharmony_ci p_clk->postdiv1_shift = clks[i].postdiv1_shift; 48162306a36Sopenharmony_ci p_clk->postdiv1_width = clks[i].postdiv1_width; 48262306a36Sopenharmony_ci p_clk->postdiv2_shift = clks[i].postdiv2_shift; 48362306a36Sopenharmony_ci p_clk->postdiv2_width = clks[i].postdiv2_width; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci p_clk->ctrl_reg2 = base + clks[i].ctrl_reg2; 48662306a36Sopenharmony_ci p_clk->fbdiv_shift = clks[i].fbdiv_shift; 48762306a36Sopenharmony_ci p_clk->fbdiv_width = clks[i].fbdiv_width; 48862306a36Sopenharmony_ci p_clk->refdiv_shift = clks[i].refdiv_shift; 48962306a36Sopenharmony_ci p_clk->refdiv_width = clks[i].refdiv_width; 49062306a36Sopenharmony_ci p_clk->hw.init = &init; 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci clk = clk_register(NULL, &p_clk->hw); 49362306a36Sopenharmony_ci if (IS_ERR(clk)) { 49462306a36Sopenharmony_ci dev_err(dev, "%s: failed to register clock %s\n", 49562306a36Sopenharmony_ci __func__, clks[i].name); 49662306a36Sopenharmony_ci continue; 49762306a36Sopenharmony_ci } 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci data->clk_data.clks[clks[i].id] = clk; 50062306a36Sopenharmony_ci p_clk++; 50162306a36Sopenharmony_ci } 50262306a36Sopenharmony_ci} 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_cistatic struct hisi_clock_data *hi3559av100_clk_register( 50562306a36Sopenharmony_ci struct platform_device *pdev) 50662306a36Sopenharmony_ci{ 50762306a36Sopenharmony_ci struct hisi_clock_data *clk_data; 50862306a36Sopenharmony_ci int ret; 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci clk_data = hisi_clk_alloc(pdev, HI3559AV100_CRG_NR_CLKS); 51162306a36Sopenharmony_ci if (!clk_data) 51262306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci ret = hisi_clk_register_fixed_rate(hi3559av100_fixed_rate_clks_crg, 51562306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), clk_data); 51662306a36Sopenharmony_ci if (ret) 51762306a36Sopenharmony_ci return ERR_PTR(ret); 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ci hisi_clk_register_pll(hi3559av100_pll_clks, 52062306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_pll_clks), clk_data, &pdev->dev); 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci ret = hisi_clk_register_mux(hi3559av100_mux_clks_crg, 52362306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_mux_clks_crg), clk_data); 52462306a36Sopenharmony_ci if (ret) 52562306a36Sopenharmony_ci goto unregister_fixed_rate; 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci ret = hisi_clk_register_gate(hi3559av100_gate_clks, 52862306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_gate_clks), clk_data); 52962306a36Sopenharmony_ci if (ret) 53062306a36Sopenharmony_ci goto unregister_mux; 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_ci ret = of_clk_add_provider(pdev->dev.of_node, 53362306a36Sopenharmony_ci of_clk_src_onecell_get, &clk_data->clk_data); 53462306a36Sopenharmony_ci if (ret) 53562306a36Sopenharmony_ci goto unregister_gate; 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ci return clk_data; 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ciunregister_gate: 54062306a36Sopenharmony_ci hisi_clk_unregister_gate(hi3559av100_gate_clks, 54162306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_gate_clks), clk_data); 54262306a36Sopenharmony_ciunregister_mux: 54362306a36Sopenharmony_ci hisi_clk_unregister_mux(hi3559av100_mux_clks_crg, 54462306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_mux_clks_crg), clk_data); 54562306a36Sopenharmony_ciunregister_fixed_rate: 54662306a36Sopenharmony_ci hisi_clk_unregister_fixed_rate(hi3559av100_fixed_rate_clks_crg, 54762306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), clk_data); 54862306a36Sopenharmony_ci return ERR_PTR(ret); 54962306a36Sopenharmony_ci} 55062306a36Sopenharmony_ci 55162306a36Sopenharmony_cistatic void hi3559av100_clk_unregister(struct platform_device *pdev) 55262306a36Sopenharmony_ci{ 55362306a36Sopenharmony_ci struct hisi_crg_dev *crg = platform_get_drvdata(pdev); 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ci of_clk_del_provider(pdev->dev.of_node); 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci hisi_clk_unregister_gate(hi3559av100_gate_clks, 55862306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_gate_clks), crg->clk_data); 55962306a36Sopenharmony_ci hisi_clk_unregister_mux(hi3559av100_mux_clks_crg, 56062306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_mux_clks_crg), crg->clk_data); 56162306a36Sopenharmony_ci hisi_clk_unregister_fixed_rate(hi3559av100_fixed_rate_clks_crg, 56262306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_fixed_rate_clks_crg), crg->clk_data); 56362306a36Sopenharmony_ci} 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_cistatic const struct hisi_crg_funcs hi3559av100_crg_funcs = { 56662306a36Sopenharmony_ci .register_clks = hi3559av100_clk_register, 56762306a36Sopenharmony_ci .unregister_clks = hi3559av100_clk_unregister, 56862306a36Sopenharmony_ci}; 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_cistatic struct hisi_fixed_rate_clock hi3559av100_shub_fixed_rate_clks[] = { 57162306a36Sopenharmony_ci { HI3559AV100_SHUB_SOURCE_SOC_24M, "clk_source_24M", NULL, 0, 24000000UL, }, 57262306a36Sopenharmony_ci { HI3559AV100_SHUB_SOURCE_SOC_200M, "clk_source_200M", NULL, 0, 200000000UL, }, 57362306a36Sopenharmony_ci { HI3559AV100_SHUB_SOURCE_SOC_300M, "clk_source_300M", NULL, 0, 300000000UL, }, 57462306a36Sopenharmony_ci { HI3559AV100_SHUB_SOURCE_PLL, "clk_source_PLL", NULL, 0, 192000000UL, }, 57562306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C0_CLK, "clk_shub_i2c0", NULL, 0, 48000000UL, }, 57662306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C1_CLK, "clk_shub_i2c1", NULL, 0, 48000000UL, }, 57762306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C2_CLK, "clk_shub_i2c2", NULL, 0, 48000000UL, }, 57862306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C3_CLK, "clk_shub_i2c3", NULL, 0, 48000000UL, }, 57962306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C4_CLK, "clk_shub_i2c4", NULL, 0, 48000000UL, }, 58062306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C5_CLK, "clk_shub_i2c5", NULL, 0, 48000000UL, }, 58162306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C6_CLK, "clk_shub_i2c6", NULL, 0, 48000000UL, }, 58262306a36Sopenharmony_ci { HI3559AV100_SHUB_I2C7_CLK, "clk_shub_i2c7", NULL, 0, 48000000UL, }, 58362306a36Sopenharmony_ci { HI3559AV100_SHUB_UART_CLK_32K, "clk_uart_32K", NULL, 0, 32000UL, }, 58462306a36Sopenharmony_ci}; 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci/* shub mux clk */ 58762306a36Sopenharmony_cistatic u32 shub_source_clk_mux_table[] = {0, 1, 2, 3}; 58862306a36Sopenharmony_cistatic const char *shub_source_clk_mux_p[] = { 58962306a36Sopenharmony_ci "clk_source_24M", "clk_source_200M", "clk_source_300M", "clk_source_PLL" 59062306a36Sopenharmony_ci}; 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_cistatic u32 shub_uart_source_clk_mux_table[] = {0, 1, 2, 3}; 59362306a36Sopenharmony_cistatic const char *shub_uart_source_clk_mux_p[] = { 59462306a36Sopenharmony_ci "clk_uart_32K", "clk_uart_div_clk", "clk_uart_div_clk", "clk_source_24M" 59562306a36Sopenharmony_ci}; 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_cistatic struct hisi_mux_clock hi3559av100_shub_mux_clks[] = { 59862306a36Sopenharmony_ci { 59962306a36Sopenharmony_ci HI3559AV100_SHUB_SOURCE_CLK, "shub_clk", shub_source_clk_mux_p, 60062306a36Sopenharmony_ci ARRAY_SIZE(shub_source_clk_mux_p), 60162306a36Sopenharmony_ci 0, 0x0, 0, 2, 0, shub_source_clk_mux_table, 60262306a36Sopenharmony_ci }, 60362306a36Sopenharmony_ci 60462306a36Sopenharmony_ci { 60562306a36Sopenharmony_ci HI3559AV100_SHUB_UART_SOURCE_CLK, "shub_uart_source_clk", 60662306a36Sopenharmony_ci shub_uart_source_clk_mux_p, ARRAY_SIZE(shub_uart_source_clk_mux_p), 60762306a36Sopenharmony_ci 0, 0x1c, 28, 2, 0, shub_uart_source_clk_mux_table, 60862306a36Sopenharmony_ci }, 60962306a36Sopenharmony_ci}; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci/* shub div clk */ 61362306a36Sopenharmony_cistatic struct clk_div_table shub_spi_clk_table[] = {{0, 8}, {1, 4}, {2, 2}, {/*sentinel*/}}; 61462306a36Sopenharmony_cistatic struct clk_div_table shub_uart_div_clk_table[] = {{1, 8}, {2, 4}, {/*sentinel*/}}; 61562306a36Sopenharmony_ci 61662306a36Sopenharmony_cistatic struct hisi_divider_clock hi3559av100_shub_div_clks[] = { 61762306a36Sopenharmony_ci { HI3559AV100_SHUB_SPI_SOURCE_CLK, "clk_spi_clk", "shub_clk", 0, 0x20, 24, 2, 61862306a36Sopenharmony_ci CLK_DIVIDER_ALLOW_ZERO, shub_spi_clk_table, 61962306a36Sopenharmony_ci }, 62062306a36Sopenharmony_ci { HI3559AV100_SHUB_UART_DIV_CLK, "clk_uart_div_clk", "shub_clk", 0, 0x1c, 28, 2, 62162306a36Sopenharmony_ci CLK_DIVIDER_ALLOW_ZERO, shub_uart_div_clk_table, 62262306a36Sopenharmony_ci }, 62362306a36Sopenharmony_ci}; 62462306a36Sopenharmony_ci 62562306a36Sopenharmony_ci/* shub gate clk */ 62662306a36Sopenharmony_cistatic struct hisi_gate_clock hi3559av100_shub_gate_clks[] = { 62762306a36Sopenharmony_ci { 62862306a36Sopenharmony_ci HI3559AV100_SHUB_SPI0_CLK, "clk_shub_spi0", "clk_spi_clk", 62962306a36Sopenharmony_ci 0, 0x20, 1, 0, 63062306a36Sopenharmony_ci }, 63162306a36Sopenharmony_ci { 63262306a36Sopenharmony_ci HI3559AV100_SHUB_SPI1_CLK, "clk_shub_spi1", "clk_spi_clk", 63362306a36Sopenharmony_ci 0, 0x20, 5, 0, 63462306a36Sopenharmony_ci }, 63562306a36Sopenharmony_ci { 63662306a36Sopenharmony_ci HI3559AV100_SHUB_SPI2_CLK, "clk_shub_spi2", "clk_spi_clk", 63762306a36Sopenharmony_ci 0, 0x20, 9, 0, 63862306a36Sopenharmony_ci }, 63962306a36Sopenharmony_ci 64062306a36Sopenharmony_ci { 64162306a36Sopenharmony_ci HI3559AV100_SHUB_UART0_CLK, "clk_shub_uart0", "shub_uart_source_clk", 64262306a36Sopenharmony_ci 0, 0x1c, 1, 0, 64362306a36Sopenharmony_ci }, 64462306a36Sopenharmony_ci { 64562306a36Sopenharmony_ci HI3559AV100_SHUB_UART1_CLK, "clk_shub_uart1", "shub_uart_source_clk", 64662306a36Sopenharmony_ci 0, 0x1c, 5, 0, 64762306a36Sopenharmony_ci }, 64862306a36Sopenharmony_ci { 64962306a36Sopenharmony_ci HI3559AV100_SHUB_UART2_CLK, "clk_shub_uart2", "shub_uart_source_clk", 65062306a36Sopenharmony_ci 0, 0x1c, 9, 0, 65162306a36Sopenharmony_ci }, 65262306a36Sopenharmony_ci { 65362306a36Sopenharmony_ci HI3559AV100_SHUB_UART3_CLK, "clk_shub_uart3", "shub_uart_source_clk", 65462306a36Sopenharmony_ci 0, 0x1c, 13, 0, 65562306a36Sopenharmony_ci }, 65662306a36Sopenharmony_ci { 65762306a36Sopenharmony_ci HI3559AV100_SHUB_UART4_CLK, "clk_shub_uart4", "shub_uart_source_clk", 65862306a36Sopenharmony_ci 0, 0x1c, 17, 0, 65962306a36Sopenharmony_ci }, 66062306a36Sopenharmony_ci { 66162306a36Sopenharmony_ci HI3559AV100_SHUB_UART5_CLK, "clk_shub_uart5", "shub_uart_source_clk", 66262306a36Sopenharmony_ci 0, 0x1c, 21, 0, 66362306a36Sopenharmony_ci }, 66462306a36Sopenharmony_ci { 66562306a36Sopenharmony_ci HI3559AV100_SHUB_UART6_CLK, "clk_shub_uart6", "shub_uart_source_clk", 66662306a36Sopenharmony_ci 0, 0x1c, 25, 0, 66762306a36Sopenharmony_ci }, 66862306a36Sopenharmony_ci 66962306a36Sopenharmony_ci { 67062306a36Sopenharmony_ci HI3559AV100_SHUB_EDMAC_CLK, "clk_shub_dmac", "shub_clk", 67162306a36Sopenharmony_ci 0, 0x24, 4, 0, 67262306a36Sopenharmony_ci }, 67362306a36Sopenharmony_ci}; 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_cistatic int hi3559av100_shub_default_clk_set(void) 67662306a36Sopenharmony_ci{ 67762306a36Sopenharmony_ci void __iomem *crg_base; 67862306a36Sopenharmony_ci unsigned int val; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci crg_base = ioremap(CRG_BASE_ADDR, SZ_4K); 68162306a36Sopenharmony_ci 68262306a36Sopenharmony_ci /* SSP: 192M/2 */ 68362306a36Sopenharmony_ci val = readl_relaxed(crg_base + 0x20); 68462306a36Sopenharmony_ci val |= (0x2 << 24); 68562306a36Sopenharmony_ci writel_relaxed(val, crg_base + 0x20); 68662306a36Sopenharmony_ci 68762306a36Sopenharmony_ci /* UART: 192M/8 */ 68862306a36Sopenharmony_ci val = readl_relaxed(crg_base + 0x1C); 68962306a36Sopenharmony_ci val |= (0x1 << 28); 69062306a36Sopenharmony_ci writel_relaxed(val, crg_base + 0x1C); 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci iounmap(crg_base); 69362306a36Sopenharmony_ci crg_base = NULL; 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ci return 0; 69662306a36Sopenharmony_ci} 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_cistatic struct hisi_clock_data *hi3559av100_shub_clk_register( 69962306a36Sopenharmony_ci struct platform_device *pdev) 70062306a36Sopenharmony_ci{ 70162306a36Sopenharmony_ci struct hisi_clock_data *clk_data = NULL; 70262306a36Sopenharmony_ci int ret; 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ci hi3559av100_shub_default_clk_set(); 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci clk_data = hisi_clk_alloc(pdev, HI3559AV100_SHUB_NR_CLKS); 70762306a36Sopenharmony_ci if (!clk_data) 70862306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 70962306a36Sopenharmony_ci 71062306a36Sopenharmony_ci ret = hisi_clk_register_fixed_rate(hi3559av100_shub_fixed_rate_clks, 71162306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), clk_data); 71262306a36Sopenharmony_ci if (ret) 71362306a36Sopenharmony_ci return ERR_PTR(ret); 71462306a36Sopenharmony_ci 71562306a36Sopenharmony_ci ret = hisi_clk_register_mux(hi3559av100_shub_mux_clks, 71662306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_mux_clks), clk_data); 71762306a36Sopenharmony_ci if (ret) 71862306a36Sopenharmony_ci goto unregister_fixed_rate; 71962306a36Sopenharmony_ci 72062306a36Sopenharmony_ci ret = hisi_clk_register_divider(hi3559av100_shub_div_clks, 72162306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_div_clks), clk_data); 72262306a36Sopenharmony_ci if (ret) 72362306a36Sopenharmony_ci goto unregister_mux; 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci ret = hisi_clk_register_gate(hi3559av100_shub_gate_clks, 72662306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_gate_clks), clk_data); 72762306a36Sopenharmony_ci if (ret) 72862306a36Sopenharmony_ci goto unregister_factor; 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci ret = of_clk_add_provider(pdev->dev.of_node, 73162306a36Sopenharmony_ci of_clk_src_onecell_get, &clk_data->clk_data); 73262306a36Sopenharmony_ci if (ret) 73362306a36Sopenharmony_ci goto unregister_gate; 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci return clk_data; 73662306a36Sopenharmony_ci 73762306a36Sopenharmony_ciunregister_gate: 73862306a36Sopenharmony_ci hisi_clk_unregister_gate(hi3559av100_shub_gate_clks, 73962306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_gate_clks), clk_data); 74062306a36Sopenharmony_ciunregister_factor: 74162306a36Sopenharmony_ci hisi_clk_unregister_divider(hi3559av100_shub_div_clks, 74262306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_div_clks), clk_data); 74362306a36Sopenharmony_ciunregister_mux: 74462306a36Sopenharmony_ci hisi_clk_unregister_mux(hi3559av100_shub_mux_clks, 74562306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_mux_clks), clk_data); 74662306a36Sopenharmony_ciunregister_fixed_rate: 74762306a36Sopenharmony_ci hisi_clk_unregister_fixed_rate(hi3559av100_shub_fixed_rate_clks, 74862306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), clk_data); 74962306a36Sopenharmony_ci return ERR_PTR(ret); 75062306a36Sopenharmony_ci} 75162306a36Sopenharmony_ci 75262306a36Sopenharmony_cistatic void hi3559av100_shub_clk_unregister(struct platform_device *pdev) 75362306a36Sopenharmony_ci{ 75462306a36Sopenharmony_ci struct hisi_crg_dev *crg = platform_get_drvdata(pdev); 75562306a36Sopenharmony_ci 75662306a36Sopenharmony_ci of_clk_del_provider(pdev->dev.of_node); 75762306a36Sopenharmony_ci 75862306a36Sopenharmony_ci hisi_clk_unregister_gate(hi3559av100_shub_gate_clks, 75962306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_gate_clks), crg->clk_data); 76062306a36Sopenharmony_ci hisi_clk_unregister_divider(hi3559av100_shub_div_clks, 76162306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_div_clks), crg->clk_data); 76262306a36Sopenharmony_ci hisi_clk_unregister_mux(hi3559av100_shub_mux_clks, 76362306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_mux_clks), crg->clk_data); 76462306a36Sopenharmony_ci hisi_clk_unregister_fixed_rate(hi3559av100_shub_fixed_rate_clks, 76562306a36Sopenharmony_ci ARRAY_SIZE(hi3559av100_shub_fixed_rate_clks), crg->clk_data); 76662306a36Sopenharmony_ci} 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_cistatic const struct hisi_crg_funcs hi3559av100_shub_crg_funcs = { 76962306a36Sopenharmony_ci .register_clks = hi3559av100_shub_clk_register, 77062306a36Sopenharmony_ci .unregister_clks = hi3559av100_shub_clk_unregister, 77162306a36Sopenharmony_ci}; 77262306a36Sopenharmony_ci 77362306a36Sopenharmony_cistatic const struct of_device_id hi3559av100_crg_match_table[] = { 77462306a36Sopenharmony_ci { 77562306a36Sopenharmony_ci .compatible = "hisilicon,hi3559av100-clock", 77662306a36Sopenharmony_ci .data = &hi3559av100_crg_funcs 77762306a36Sopenharmony_ci }, 77862306a36Sopenharmony_ci { 77962306a36Sopenharmony_ci .compatible = "hisilicon,hi3559av100-shub-clock", 78062306a36Sopenharmony_ci .data = &hi3559av100_shub_crg_funcs 78162306a36Sopenharmony_ci }, 78262306a36Sopenharmony_ci { } 78362306a36Sopenharmony_ci}; 78462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, hi3559av100_crg_match_table); 78562306a36Sopenharmony_ci 78662306a36Sopenharmony_cistatic int hi3559av100_crg_probe(struct platform_device *pdev) 78762306a36Sopenharmony_ci{ 78862306a36Sopenharmony_ci struct hisi_crg_dev *crg; 78962306a36Sopenharmony_ci 79062306a36Sopenharmony_ci crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL); 79162306a36Sopenharmony_ci if (!crg) 79262306a36Sopenharmony_ci return -ENOMEM; 79362306a36Sopenharmony_ci 79462306a36Sopenharmony_ci crg->funcs = of_device_get_match_data(&pdev->dev); 79562306a36Sopenharmony_ci if (!crg->funcs) 79662306a36Sopenharmony_ci return -ENOENT; 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_ci crg->rstc = hisi_reset_init(pdev); 79962306a36Sopenharmony_ci if (!crg->rstc) 80062306a36Sopenharmony_ci return -ENOMEM; 80162306a36Sopenharmony_ci 80262306a36Sopenharmony_ci crg->clk_data = crg->funcs->register_clks(pdev); 80362306a36Sopenharmony_ci if (IS_ERR(crg->clk_data)) { 80462306a36Sopenharmony_ci hisi_reset_exit(crg->rstc); 80562306a36Sopenharmony_ci return PTR_ERR(crg->clk_data); 80662306a36Sopenharmony_ci } 80762306a36Sopenharmony_ci 80862306a36Sopenharmony_ci platform_set_drvdata(pdev, crg); 80962306a36Sopenharmony_ci return 0; 81062306a36Sopenharmony_ci} 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_cistatic void hi3559av100_crg_remove(struct platform_device *pdev) 81362306a36Sopenharmony_ci{ 81462306a36Sopenharmony_ci struct hisi_crg_dev *crg = platform_get_drvdata(pdev); 81562306a36Sopenharmony_ci 81662306a36Sopenharmony_ci hisi_reset_exit(crg->rstc); 81762306a36Sopenharmony_ci crg->funcs->unregister_clks(pdev); 81862306a36Sopenharmony_ci} 81962306a36Sopenharmony_ci 82062306a36Sopenharmony_cistatic struct platform_driver hi3559av100_crg_driver = { 82162306a36Sopenharmony_ci .probe = hi3559av100_crg_probe, 82262306a36Sopenharmony_ci .remove_new = hi3559av100_crg_remove, 82362306a36Sopenharmony_ci .driver = { 82462306a36Sopenharmony_ci .name = "hi3559av100-clock", 82562306a36Sopenharmony_ci .of_match_table = hi3559av100_crg_match_table, 82662306a36Sopenharmony_ci }, 82762306a36Sopenharmony_ci}; 82862306a36Sopenharmony_ci 82962306a36Sopenharmony_cistatic int __init hi3559av100_crg_init(void) 83062306a36Sopenharmony_ci{ 83162306a36Sopenharmony_ci return platform_driver_register(&hi3559av100_crg_driver); 83262306a36Sopenharmony_ci} 83362306a36Sopenharmony_cicore_initcall(hi3559av100_crg_init); 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_cistatic void __exit hi3559av100_crg_exit(void) 83662306a36Sopenharmony_ci{ 83762306a36Sopenharmony_ci platform_driver_unregister(&hi3559av100_crg_driver); 83862306a36Sopenharmony_ci} 83962306a36Sopenharmony_cimodule_exit(hi3559av100_crg_exit); 84062306a36Sopenharmony_ci 84162306a36Sopenharmony_ci 84262306a36Sopenharmony_ciMODULE_DESCRIPTION("HiSilicon Hi3559AV100 CRG Driver"); 843