18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:
68c2ecf20Sopenharmony_ci *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
78c2ecf20Sopenharmony_ci *   Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Baikal-T1 CCU Dividers clock driver
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "bt1-ccu-div: " fmt
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/printk.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
188c2ecf20Sopenharmony_ci#include <linux/reset-controller.h>
198c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/of_address.h>
228c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
238c2ecf20Sopenharmony_ci#include <linux/ioport.h>
248c2ecf20Sopenharmony_ci#include <linux/regmap.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <dt-bindings/clock/bt1-ccu.h>
278c2ecf20Sopenharmony_ci#include <dt-bindings/reset/bt1-ccu.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include "ccu-div.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define CCU_AXI_MAIN_BASE		0x030
328c2ecf20Sopenharmony_ci#define CCU_AXI_DDR_BASE		0x034
338c2ecf20Sopenharmony_ci#define CCU_AXI_SATA_BASE		0x038
348c2ecf20Sopenharmony_ci#define CCU_AXI_GMAC0_BASE		0x03C
358c2ecf20Sopenharmony_ci#define CCU_AXI_GMAC1_BASE		0x040
368c2ecf20Sopenharmony_ci#define CCU_AXI_XGMAC_BASE		0x044
378c2ecf20Sopenharmony_ci#define CCU_AXI_PCIE_M_BASE		0x048
388c2ecf20Sopenharmony_ci#define CCU_AXI_PCIE_S_BASE		0x04C
398c2ecf20Sopenharmony_ci#define CCU_AXI_USB_BASE		0x050
408c2ecf20Sopenharmony_ci#define CCU_AXI_HWA_BASE		0x054
418c2ecf20Sopenharmony_ci#define CCU_AXI_SRAM_BASE		0x058
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define CCU_SYS_SATA_REF_BASE		0x060
448c2ecf20Sopenharmony_ci#define CCU_SYS_APB_BASE		0x064
458c2ecf20Sopenharmony_ci#define CCU_SYS_GMAC0_BASE		0x068
468c2ecf20Sopenharmony_ci#define CCU_SYS_GMAC1_BASE		0x06C
478c2ecf20Sopenharmony_ci#define CCU_SYS_XGMAC_BASE		0x070
488c2ecf20Sopenharmony_ci#define CCU_SYS_USB_BASE		0x074
498c2ecf20Sopenharmony_ci#define CCU_SYS_PVT_BASE		0x078
508c2ecf20Sopenharmony_ci#define CCU_SYS_HWA_BASE		0x07C
518c2ecf20Sopenharmony_ci#define CCU_SYS_UART_BASE		0x084
528c2ecf20Sopenharmony_ci#define CCU_SYS_TIMER0_BASE		0x088
538c2ecf20Sopenharmony_ci#define CCU_SYS_TIMER1_BASE		0x08C
548c2ecf20Sopenharmony_ci#define CCU_SYS_TIMER2_BASE		0x090
558c2ecf20Sopenharmony_ci#define CCU_SYS_WDT_BASE		0x150
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define CCU_DIV_VAR_INFO(_id, _name, _pname, _base, _width, _flags, _features) \
588c2ecf20Sopenharmony_ci	{								\
598c2ecf20Sopenharmony_ci		.id = _id,						\
608c2ecf20Sopenharmony_ci		.name = _name,						\
618c2ecf20Sopenharmony_ci		.parent_name = _pname,					\
628c2ecf20Sopenharmony_ci		.base = _base,						\
638c2ecf20Sopenharmony_ci		.type = CCU_DIV_VAR,					\
648c2ecf20Sopenharmony_ci		.width = _width,					\
658c2ecf20Sopenharmony_ci		.flags = _flags,					\
668c2ecf20Sopenharmony_ci		.features = _features					\
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define CCU_DIV_GATE_INFO(_id, _name, _pname, _base, _divider)	\
708c2ecf20Sopenharmony_ci	{							\
718c2ecf20Sopenharmony_ci		.id = _id,					\
728c2ecf20Sopenharmony_ci		.name = _name,					\
738c2ecf20Sopenharmony_ci		.parent_name = _pname,				\
748c2ecf20Sopenharmony_ci		.base = _base,					\
758c2ecf20Sopenharmony_ci		.type = CCU_DIV_GATE,				\
768c2ecf20Sopenharmony_ci		.divider = _divider				\
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define CCU_DIV_BUF_INFO(_id, _name, _pname, _base, _flags)	\
808c2ecf20Sopenharmony_ci	{							\
818c2ecf20Sopenharmony_ci		.id = _id,					\
828c2ecf20Sopenharmony_ci		.name = _name,					\
838c2ecf20Sopenharmony_ci		.parent_name = _pname,				\
848c2ecf20Sopenharmony_ci		.base = _base,					\
858c2ecf20Sopenharmony_ci		.type = CCU_DIV_BUF,				\
868c2ecf20Sopenharmony_ci		.flags = _flags					\
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define CCU_DIV_FIXED_INFO(_id, _name, _pname, _divider)	\
908c2ecf20Sopenharmony_ci	{							\
918c2ecf20Sopenharmony_ci		.id = _id,					\
928c2ecf20Sopenharmony_ci		.name = _name,					\
938c2ecf20Sopenharmony_ci		.parent_name = _pname,				\
948c2ecf20Sopenharmony_ci		.type = CCU_DIV_FIXED,				\
958c2ecf20Sopenharmony_ci		.divider = _divider				\
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define CCU_DIV_RST_MAP(_rst_id, _clk_id)	\
998c2ecf20Sopenharmony_ci	{					\
1008c2ecf20Sopenharmony_ci		.rst_id = _rst_id,		\
1018c2ecf20Sopenharmony_ci		.clk_id = _clk_id		\
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct ccu_div_info {
1058c2ecf20Sopenharmony_ci	unsigned int id;
1068c2ecf20Sopenharmony_ci	const char *name;
1078c2ecf20Sopenharmony_ci	const char *parent_name;
1088c2ecf20Sopenharmony_ci	unsigned int base;
1098c2ecf20Sopenharmony_ci	enum ccu_div_type type;
1108c2ecf20Sopenharmony_ci	union {
1118c2ecf20Sopenharmony_ci		unsigned int width;
1128c2ecf20Sopenharmony_ci		unsigned int divider;
1138c2ecf20Sopenharmony_ci	};
1148c2ecf20Sopenharmony_ci	unsigned long flags;
1158c2ecf20Sopenharmony_ci	unsigned long features;
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistruct ccu_div_rst_map {
1198c2ecf20Sopenharmony_ci	unsigned int rst_id;
1208c2ecf20Sopenharmony_ci	unsigned int clk_id;
1218c2ecf20Sopenharmony_ci};
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistruct ccu_div_data {
1248c2ecf20Sopenharmony_ci	struct device_node *np;
1258c2ecf20Sopenharmony_ci	struct regmap *sys_regs;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	unsigned int divs_num;
1288c2ecf20Sopenharmony_ci	const struct ccu_div_info *divs_info;
1298c2ecf20Sopenharmony_ci	struct ccu_div **divs;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	unsigned int rst_num;
1328c2ecf20Sopenharmony_ci	const struct ccu_div_rst_map *rst_map;
1338c2ecf20Sopenharmony_ci	struct reset_controller_dev rcdev;
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci#define to_ccu_div_data(_rcdev) container_of(_rcdev, struct ccu_div_data, rcdev)
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/*
1388c2ecf20Sopenharmony_ci * AXI Main Interconnect (axi_main_clk) and DDR AXI-bus (axi_ddr_clk) clocks
1398c2ecf20Sopenharmony_ci * must be left enabled in any case, since former one is responsible for
1408c2ecf20Sopenharmony_ci * clocking a bus between CPU cores and the rest of the SoC components, while
1418c2ecf20Sopenharmony_ci * the later is clocking the AXI-bus between DDR controller and the Main
1428c2ecf20Sopenharmony_ci * Interconnect. So should any of these clocks get to be disabled, the system
1438c2ecf20Sopenharmony_ci * will literally stop working. That's why we marked them as critical.
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_cistatic const struct ccu_div_info axi_info[] = {
1468c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_MAIN_CLK, "axi_main_clk", "pcie_clk",
1478c2ecf20Sopenharmony_ci			 CCU_AXI_MAIN_BASE, 4,
1488c2ecf20Sopenharmony_ci			 CLK_IS_CRITICAL, CCU_DIV_RESET_DOMAIN),
1498c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_DDR_CLK, "axi_ddr_clk", "sata_clk",
1508c2ecf20Sopenharmony_ci			 CCU_AXI_DDR_BASE, 4,
1518c2ecf20Sopenharmony_ci			 CLK_IS_CRITICAL | CLK_SET_RATE_GATE,
1528c2ecf20Sopenharmony_ci			 CCU_DIV_RESET_DOMAIN),
1538c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_SATA_CLK, "axi_sata_clk", "sata_clk",
1548c2ecf20Sopenharmony_ci			 CCU_AXI_SATA_BASE, 4,
1558c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1568c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_GMAC0_CLK, "axi_gmac0_clk", "eth_clk",
1578c2ecf20Sopenharmony_ci			 CCU_AXI_GMAC0_BASE, 4,
1588c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1598c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_GMAC1_CLK, "axi_gmac1_clk", "eth_clk",
1608c2ecf20Sopenharmony_ci			 CCU_AXI_GMAC1_BASE, 4,
1618c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1628c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_XGMAC_CLK, "axi_xgmac_clk", "eth_clk",
1638c2ecf20Sopenharmony_ci			 CCU_AXI_XGMAC_BASE, 4,
1648c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1658c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_PCIE_M_CLK, "axi_pcie_m_clk", "pcie_clk",
1668c2ecf20Sopenharmony_ci			 CCU_AXI_PCIE_M_BASE, 4,
1678c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1688c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_PCIE_S_CLK, "axi_pcie_s_clk", "pcie_clk",
1698c2ecf20Sopenharmony_ci			 CCU_AXI_PCIE_S_BASE, 4,
1708c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1718c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_USB_CLK, "axi_usb_clk", "sata_clk",
1728c2ecf20Sopenharmony_ci			 CCU_AXI_USB_BASE, 4,
1738c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1748c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_HWA_CLK, "axi_hwa_clk", "sata_clk",
1758c2ecf20Sopenharmony_ci			 CCU_AXI_HWA_BASE, 4,
1768c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN),
1778c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_AXI_SRAM_CLK, "axi_sram_clk", "eth_clk",
1788c2ecf20Sopenharmony_ci			 CCU_AXI_SRAM_BASE, 4,
1798c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_RESET_DOMAIN)
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic const struct ccu_div_rst_map axi_rst_map[] = {
1838c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_MAIN_RST, CCU_AXI_MAIN_CLK),
1848c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_DDR_RST, CCU_AXI_DDR_CLK),
1858c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_SATA_RST, CCU_AXI_SATA_CLK),
1868c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_GMAC0_RST, CCU_AXI_GMAC0_CLK),
1878c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_GMAC1_RST, CCU_AXI_GMAC1_CLK),
1888c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_XGMAC_RST, CCU_AXI_XGMAC_CLK),
1898c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_PCIE_M_RST, CCU_AXI_PCIE_M_CLK),
1908c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_PCIE_S_RST, CCU_AXI_PCIE_S_CLK),
1918c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_USB_RST, CCU_AXI_USB_CLK),
1928c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_HWA_RST, CCU_AXI_HWA_CLK),
1938c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_AXI_SRAM_RST, CCU_AXI_SRAM_CLK)
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/*
1978c2ecf20Sopenharmony_ci * APB-bus clock is marked as critical since it's a main communication bus
1988c2ecf20Sopenharmony_ci * for the SoC devices registers IO-operations.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_cistatic const struct ccu_div_info sys_info[] = {
2018c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_SATA_CLK, "sys_sata_clk",
2028c2ecf20Sopenharmony_ci			 "sata_clk", CCU_SYS_SATA_REF_BASE, 4,
2038c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE,
2048c2ecf20Sopenharmony_ci			 CCU_DIV_SKIP_ONE | CCU_DIV_LOCK_SHIFTED |
2058c2ecf20Sopenharmony_ci			 CCU_DIV_RESET_DOMAIN),
2068c2ecf20Sopenharmony_ci	CCU_DIV_BUF_INFO(CCU_SYS_SATA_REF_CLK, "sys_sata_ref_clk",
2078c2ecf20Sopenharmony_ci			 "sys_sata_clk", CCU_SYS_SATA_REF_BASE,
2088c2ecf20Sopenharmony_ci			 CLK_SET_RATE_PARENT),
2098c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_APB_CLK, "sys_apb_clk",
2108c2ecf20Sopenharmony_ci			 "pcie_clk", CCU_SYS_APB_BASE, 5,
2118c2ecf20Sopenharmony_ci			 CLK_IS_CRITICAL, CCU_DIV_RESET_DOMAIN),
2128c2ecf20Sopenharmony_ci	CCU_DIV_GATE_INFO(CCU_SYS_GMAC0_TX_CLK, "sys_gmac0_tx_clk",
2138c2ecf20Sopenharmony_ci			  "eth_clk", CCU_SYS_GMAC0_BASE, 5),
2148c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_GMAC0_PTP_CLK, "sys_gmac0_ptp_clk",
2158c2ecf20Sopenharmony_ci			   "eth_clk", 10),
2168c2ecf20Sopenharmony_ci	CCU_DIV_GATE_INFO(CCU_SYS_GMAC1_TX_CLK, "sys_gmac1_tx_clk",
2178c2ecf20Sopenharmony_ci			  "eth_clk", CCU_SYS_GMAC1_BASE, 5),
2188c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_GMAC1_PTP_CLK, "sys_gmac1_ptp_clk",
2198c2ecf20Sopenharmony_ci			   "eth_clk", 10),
2208c2ecf20Sopenharmony_ci	CCU_DIV_GATE_INFO(CCU_SYS_XGMAC_CLK, "sys_xgmac_clk",
2218c2ecf20Sopenharmony_ci			  "eth_clk", CCU_SYS_XGMAC_BASE, 1),
2228c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_XGMAC_REF_CLK, "sys_xgmac_ref_clk",
2238c2ecf20Sopenharmony_ci			   "sys_xgmac_clk", 8),
2248c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_XGMAC_PTP_CLK, "sys_xgmac_ptp_clk",
2258c2ecf20Sopenharmony_ci			   "sys_xgmac_clk", 8),
2268c2ecf20Sopenharmony_ci	CCU_DIV_GATE_INFO(CCU_SYS_USB_CLK, "sys_usb_clk",
2278c2ecf20Sopenharmony_ci			  "eth_clk", CCU_SYS_USB_BASE, 10),
2288c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_PVT_CLK, "sys_pvt_clk",
2298c2ecf20Sopenharmony_ci			 "ref_clk", CCU_SYS_PVT_BASE, 5,
2308c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2318c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_HWA_CLK, "sys_hwa_clk",
2328c2ecf20Sopenharmony_ci			 "sata_clk", CCU_SYS_HWA_BASE, 4,
2338c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2348c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_UART_CLK, "sys_uart_clk",
2358c2ecf20Sopenharmony_ci			 "eth_clk", CCU_SYS_UART_BASE, 17,
2368c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2378c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_I2C1_CLK, "sys_i2c1_clk",
2388c2ecf20Sopenharmony_ci			   "eth_clk", 10),
2398c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_I2C2_CLK, "sys_i2c2_clk",
2408c2ecf20Sopenharmony_ci			   "eth_clk", 10),
2418c2ecf20Sopenharmony_ci	CCU_DIV_FIXED_INFO(CCU_SYS_GPIO_CLK, "sys_gpio_clk",
2428c2ecf20Sopenharmony_ci			   "ref_clk", 25),
2438c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_TIMER0_CLK, "sys_timer0_clk",
2448c2ecf20Sopenharmony_ci			 "ref_clk", CCU_SYS_TIMER0_BASE, 17,
2458c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2468c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_TIMER1_CLK, "sys_timer1_clk",
2478c2ecf20Sopenharmony_ci			 "ref_clk", CCU_SYS_TIMER1_BASE, 17,
2488c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2498c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_TIMER2_CLK, "sys_timer2_clk",
2508c2ecf20Sopenharmony_ci			 "ref_clk", CCU_SYS_TIMER2_BASE, 17,
2518c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, 0),
2528c2ecf20Sopenharmony_ci	CCU_DIV_VAR_INFO(CCU_SYS_WDT_CLK, "sys_wdt_clk",
2538c2ecf20Sopenharmony_ci			 "eth_clk", CCU_SYS_WDT_BASE, 17,
2548c2ecf20Sopenharmony_ci			 CLK_SET_RATE_GATE, CCU_DIV_SKIP_ONE_TO_THREE)
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic const struct ccu_div_rst_map sys_rst_map[] = {
2588c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_SYS_SATA_REF_RST, CCU_SYS_SATA_REF_CLK),
2598c2ecf20Sopenharmony_ci	CCU_DIV_RST_MAP(CCU_SYS_APB_RST, CCU_SYS_APB_CLK),
2608c2ecf20Sopenharmony_ci};
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic struct ccu_div *ccu_div_find_desc(struct ccu_div_data *data,
2638c2ecf20Sopenharmony_ci					 unsigned int clk_id)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct ccu_div *div;
2668c2ecf20Sopenharmony_ci	int idx;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	for (idx = 0; idx < data->divs_num; ++idx) {
2698c2ecf20Sopenharmony_ci		div = data->divs[idx];
2708c2ecf20Sopenharmony_ci		if (div && div->id == clk_id)
2718c2ecf20Sopenharmony_ci			return div;
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return ERR_PTR(-EINVAL);
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int ccu_div_reset(struct reset_controller_dev *rcdev,
2788c2ecf20Sopenharmony_ci			 unsigned long rst_id)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	struct ccu_div_data *data = to_ccu_div_data(rcdev);
2818c2ecf20Sopenharmony_ci	const struct ccu_div_rst_map *map;
2828c2ecf20Sopenharmony_ci	struct ccu_div *div;
2838c2ecf20Sopenharmony_ci	int idx, ret;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	for (idx = 0, map = data->rst_map; idx < data->rst_num; ++idx, ++map) {
2868c2ecf20Sopenharmony_ci		if (map->rst_id == rst_id)
2878c2ecf20Sopenharmony_ci			break;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci	if (idx == data->rst_num) {
2908c2ecf20Sopenharmony_ci		pr_err("Invalid reset ID %lu specified\n", rst_id);
2918c2ecf20Sopenharmony_ci		return -EINVAL;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	div = ccu_div_find_desc(data, map->clk_id);
2958c2ecf20Sopenharmony_ci	if (IS_ERR(div)) {
2968c2ecf20Sopenharmony_ci		pr_err("Invalid clock ID %d in mapping\n", map->clk_id);
2978c2ecf20Sopenharmony_ci		return PTR_ERR(div);
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ret = ccu_div_reset_domain(div);
3018c2ecf20Sopenharmony_ci	if (ret) {
3028c2ecf20Sopenharmony_ci		pr_err("Reset isn't supported by divider %s\n",
3038c2ecf20Sopenharmony_ci			clk_hw_get_name(ccu_div_get_clk_hw(div)));
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return ret;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic const struct reset_control_ops ccu_div_rst_ops = {
3108c2ecf20Sopenharmony_ci	.reset = ccu_div_reset,
3118c2ecf20Sopenharmony_ci};
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic struct ccu_div_data *ccu_div_create_data(struct device_node *np)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct ccu_div_data *data;
3168c2ecf20Sopenharmony_ci	int ret;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(*data), GFP_KERNEL);
3198c2ecf20Sopenharmony_ci	if (!data)
3208c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	data->np = np;
3238c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "baikal,bt1-ccu-axi")) {
3248c2ecf20Sopenharmony_ci		data->divs_num = ARRAY_SIZE(axi_info);
3258c2ecf20Sopenharmony_ci		data->divs_info = axi_info;
3268c2ecf20Sopenharmony_ci		data->rst_num = ARRAY_SIZE(axi_rst_map);
3278c2ecf20Sopenharmony_ci		data->rst_map = axi_rst_map;
3288c2ecf20Sopenharmony_ci	} else if (of_device_is_compatible(np, "baikal,bt1-ccu-sys")) {
3298c2ecf20Sopenharmony_ci		data->divs_num = ARRAY_SIZE(sys_info);
3308c2ecf20Sopenharmony_ci		data->divs_info = sys_info;
3318c2ecf20Sopenharmony_ci		data->rst_num = ARRAY_SIZE(sys_rst_map);
3328c2ecf20Sopenharmony_ci		data->rst_map = sys_rst_map;
3338c2ecf20Sopenharmony_ci	} else {
3348c2ecf20Sopenharmony_ci		pr_err("Incompatible DT node '%s' specified\n",
3358c2ecf20Sopenharmony_ci			of_node_full_name(np));
3368c2ecf20Sopenharmony_ci		ret = -EINVAL;
3378c2ecf20Sopenharmony_ci		goto err_kfree_data;
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	data->divs = kcalloc(data->divs_num, sizeof(*data->divs), GFP_KERNEL);
3418c2ecf20Sopenharmony_ci	if (!data->divs) {
3428c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3438c2ecf20Sopenharmony_ci		goto err_kfree_data;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return data;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cierr_kfree_data:
3498c2ecf20Sopenharmony_ci	kfree(data);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void ccu_div_free_data(struct ccu_div_data *data)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	kfree(data->divs);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	kfree(data);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic int ccu_div_find_sys_regs(struct ccu_div_data *data)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	data->sys_regs = syscon_node_to_regmap(data->np->parent);
3648c2ecf20Sopenharmony_ci	if (IS_ERR(data->sys_regs)) {
3658c2ecf20Sopenharmony_ci		pr_err("Failed to find syscon regs for '%s'\n",
3668c2ecf20Sopenharmony_ci			of_node_full_name(data->np));
3678c2ecf20Sopenharmony_ci		return PTR_ERR(data->sys_regs);
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	return 0;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic struct clk_hw *ccu_div_of_clk_hw_get(struct of_phandle_args *clkspec,
3748c2ecf20Sopenharmony_ci					    void *priv)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct ccu_div_data *data = priv;
3778c2ecf20Sopenharmony_ci	struct ccu_div *div;
3788c2ecf20Sopenharmony_ci	unsigned int clk_id;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	clk_id = clkspec->args[0];
3818c2ecf20Sopenharmony_ci	div = ccu_div_find_desc(data, clk_id);
3828c2ecf20Sopenharmony_ci	if (IS_ERR(div)) {
3838c2ecf20Sopenharmony_ci		pr_info("Invalid clock ID %d specified\n", clk_id);
3848c2ecf20Sopenharmony_ci		return ERR_CAST(div);
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	return ccu_div_get_clk_hw(div);
3888c2ecf20Sopenharmony_ci}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic int ccu_div_clk_register(struct ccu_div_data *data)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	int idx, ret;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	for (idx = 0; idx < data->divs_num; ++idx) {
3958c2ecf20Sopenharmony_ci		const struct ccu_div_info *info = &data->divs_info[idx];
3968c2ecf20Sopenharmony_ci		struct ccu_div_init_data init = {0};
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		init.id = info->id;
3998c2ecf20Sopenharmony_ci		init.name = info->name;
4008c2ecf20Sopenharmony_ci		init.parent_name = info->parent_name;
4018c2ecf20Sopenharmony_ci		init.np = data->np;
4028c2ecf20Sopenharmony_ci		init.type = info->type;
4038c2ecf20Sopenharmony_ci		init.flags = info->flags;
4048c2ecf20Sopenharmony_ci		init.features = info->features;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		if (init.type == CCU_DIV_VAR) {
4078c2ecf20Sopenharmony_ci			init.base = info->base;
4088c2ecf20Sopenharmony_ci			init.sys_regs = data->sys_regs;
4098c2ecf20Sopenharmony_ci			init.width = info->width;
4108c2ecf20Sopenharmony_ci		} else if (init.type == CCU_DIV_GATE) {
4118c2ecf20Sopenharmony_ci			init.base = info->base;
4128c2ecf20Sopenharmony_ci			init.sys_regs = data->sys_regs;
4138c2ecf20Sopenharmony_ci			init.divider = info->divider;
4148c2ecf20Sopenharmony_ci		} else if (init.type == CCU_DIV_BUF) {
4158c2ecf20Sopenharmony_ci			init.base = info->base;
4168c2ecf20Sopenharmony_ci			init.sys_regs = data->sys_regs;
4178c2ecf20Sopenharmony_ci		} else {
4188c2ecf20Sopenharmony_ci			init.divider = info->divider;
4198c2ecf20Sopenharmony_ci		}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		data->divs[idx] = ccu_div_hw_register(&init);
4228c2ecf20Sopenharmony_ci		if (IS_ERR(data->divs[idx])) {
4238c2ecf20Sopenharmony_ci			ret = PTR_ERR(data->divs[idx]);
4248c2ecf20Sopenharmony_ci			pr_err("Couldn't register divider '%s' hw\n",
4258c2ecf20Sopenharmony_ci				init.name);
4268c2ecf20Sopenharmony_ci			goto err_hw_unregister;
4278c2ecf20Sopenharmony_ci		}
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	ret = of_clk_add_hw_provider(data->np, ccu_div_of_clk_hw_get, data);
4318c2ecf20Sopenharmony_ci	if (ret) {
4328c2ecf20Sopenharmony_ci		pr_err("Couldn't register dividers '%s' clock provider\n",
4338c2ecf20Sopenharmony_ci			of_node_full_name(data->np));
4348c2ecf20Sopenharmony_ci		goto err_hw_unregister;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	return 0;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cierr_hw_unregister:
4408c2ecf20Sopenharmony_ci	for (--idx; idx >= 0; --idx)
4418c2ecf20Sopenharmony_ci		ccu_div_hw_unregister(data->divs[idx]);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	return ret;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic void ccu_div_clk_unregister(struct ccu_div_data *data)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	int idx;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	of_clk_del_provider(data->np);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	for (idx = 0; idx < data->divs_num; ++idx)
4538c2ecf20Sopenharmony_ci		ccu_div_hw_unregister(data->divs[idx]);
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cistatic int ccu_div_rst_register(struct ccu_div_data *data)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	int ret;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	data->rcdev.ops = &ccu_div_rst_ops;
4618c2ecf20Sopenharmony_ci	data->rcdev.of_node = data->np;
4628c2ecf20Sopenharmony_ci	data->rcdev.nr_resets = data->rst_num;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	ret = reset_controller_register(&data->rcdev);
4658c2ecf20Sopenharmony_ci	if (ret)
4668c2ecf20Sopenharmony_ci		pr_err("Couldn't register divider '%s' reset controller\n",
4678c2ecf20Sopenharmony_ci			of_node_full_name(data->np));
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	return ret;
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic void ccu_div_init(struct device_node *np)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	struct ccu_div_data *data;
4758c2ecf20Sopenharmony_ci	int ret;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	data = ccu_div_create_data(np);
4788c2ecf20Sopenharmony_ci	if (IS_ERR(data))
4798c2ecf20Sopenharmony_ci		return;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	ret = ccu_div_find_sys_regs(data);
4828c2ecf20Sopenharmony_ci	if (ret)
4838c2ecf20Sopenharmony_ci		goto err_free_data;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	ret = ccu_div_clk_register(data);
4868c2ecf20Sopenharmony_ci	if (ret)
4878c2ecf20Sopenharmony_ci		goto err_free_data;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	ret = ccu_div_rst_register(data);
4908c2ecf20Sopenharmony_ci	if (ret)
4918c2ecf20Sopenharmony_ci		goto err_clk_unregister;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cierr_clk_unregister:
4968c2ecf20Sopenharmony_ci	ccu_div_clk_unregister(data);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cierr_free_data:
4998c2ecf20Sopenharmony_ci	ccu_div_free_data(data);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciCLK_OF_DECLARE(ccu_axi, "baikal,bt1-ccu-axi", ccu_div_init);
5038c2ecf20Sopenharmony_ciCLK_OF_DECLARE(ccu_sys, "baikal,bt1-ccu-sys", ccu_div_init);
504