1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
7 * Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
9 * Baikal-T1 CCU Dividers clock driver
12 #define pr_fmt(fmt) "bt1-ccu-div: " fmt
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/printk.h>
17 #include <linux/slab.h>
18 #include <linux/clk-provider.h>
19 #include <linux/reset-controller.h>
20 #include <linux/mfd/syscon.h>
22 #include <linux/of_address.h>
23 #include <linux/ioport.h>
24 #include <linux/regmap.h>
26 #include <dt-bindings/clock/bt1-ccu.h>
31 #define CCU_AXI_MAIN_BASE 0x030
32 #define CCU_AXI_DDR_BASE 0x034
33 #define CCU_AXI_SATA_BASE 0x038
34 #define CCU_AXI_GMAC0_BASE 0x03C
35 #define CCU_AXI_GMAC1_BASE 0x040
36 #define CCU_AXI_XGMAC_BASE 0x044
37 #define CCU_AXI_PCIE_M_BASE 0x048
38 #define CCU_AXI_PCIE_S_BASE 0x04C
39 #define CCU_AXI_USB_BASE 0x050
40 #define CCU_AXI_HWA_BASE 0x054
41 #define CCU_AXI_SRAM_BASE 0x058
43 #define CCU_SYS_SATA_REF_BASE 0x060
44 #define CCU_SYS_APB_BASE 0x064
45 #define CCU_SYS_GMAC0_BASE 0x068
46 #define CCU_SYS_GMAC1_BASE 0x06C
47 #define CCU_SYS_XGMAC_BASE 0x070
48 #define CCU_SYS_USB_BASE 0x074
49 #define CCU_SYS_PVT_BASE 0x078
50 #define CCU_SYS_HWA_BASE 0x07C
51 #define CCU_SYS_UART_BASE 0x084
52 #define CCU_SYS_TIMER0_BASE 0x088
53 #define CCU_SYS_TIMER1_BASE 0x08C
54 #define CCU_SYS_TIMER2_BASE 0x090
55 #define CCU_SYS_WDT_BASE 0x150
57 #define CCU_DIV_VAR_INFO(_id, _name, _pname, _base, _width, _flags, _features) \
61 .parent_name = _pname, \
63 .type = CCU_DIV_VAR, \
66 .features = _features \
69 #define CCU_DIV_GATE_INFO(_id, _name, _pname, _base, _divider) \
73 .parent_name = _pname, \
75 .type = CCU_DIV_GATE, \
79 #define CCU_DIV_BUF_INFO(_id, _name, _pname, _base, _flags) \
83 .parent_name = _pname, \
85 .type = CCU_DIV_BUF, \
89 #define CCU_DIV_FIXED_INFO(_id, _name, _pname, _divider) \
93 .parent_name = _pname, \
94 .type = CCU_DIV_FIXED, \
101 const char *parent_name
;
103 enum ccu_div_type type
;
106 unsigned int divider
;
109 unsigned long features
;
112 struct ccu_div_data
{
113 struct device_node
*np
;
114 struct regmap
*sys_regs
;
116 unsigned int divs_num
;
117 const struct ccu_div_info
*divs_info
;
118 struct ccu_div
**divs
;
120 struct ccu_rst
*rsts
;
124 * AXI Main Interconnect (axi_main_clk) and DDR AXI-bus (axi_ddr_clk) clocks
125 * must be left enabled in any case, since former one is responsible for
126 * clocking a bus between CPU cores and the rest of the SoC components, while
127 * the later is clocking the AXI-bus between DDR controller and the Main
128 * Interconnect. So should any of these clocks get to be disabled, the system
129 * will literally stop working. That's why we marked them as critical.
131 static const struct ccu_div_info axi_info
[] = {
132 CCU_DIV_VAR_INFO(CCU_AXI_MAIN_CLK
, "axi_main_clk", "pcie_clk",
133 CCU_AXI_MAIN_BASE
, 4,
134 CLK_IS_CRITICAL
, CCU_DIV_RESET_DOMAIN
),
135 CCU_DIV_VAR_INFO(CCU_AXI_DDR_CLK
, "axi_ddr_clk", "sata_clk",
137 CLK_IS_CRITICAL
| CLK_SET_RATE_GATE
,
138 CCU_DIV_RESET_DOMAIN
),
139 CCU_DIV_VAR_INFO(CCU_AXI_SATA_CLK
, "axi_sata_clk", "sata_clk",
140 CCU_AXI_SATA_BASE
, 4,
141 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
142 CCU_DIV_VAR_INFO(CCU_AXI_GMAC0_CLK
, "axi_gmac0_clk", "eth_clk",
143 CCU_AXI_GMAC0_BASE
, 4,
144 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
145 CCU_DIV_VAR_INFO(CCU_AXI_GMAC1_CLK
, "axi_gmac1_clk", "eth_clk",
146 CCU_AXI_GMAC1_BASE
, 4,
147 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
148 CCU_DIV_VAR_INFO(CCU_AXI_XGMAC_CLK
, "axi_xgmac_clk", "eth_clk",
149 CCU_AXI_XGMAC_BASE
, 4,
150 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
151 CCU_DIV_VAR_INFO(CCU_AXI_PCIE_M_CLK
, "axi_pcie_m_clk", "pcie_clk",
152 CCU_AXI_PCIE_M_BASE
, 4,
153 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
154 CCU_DIV_VAR_INFO(CCU_AXI_PCIE_S_CLK
, "axi_pcie_s_clk", "pcie_clk",
155 CCU_AXI_PCIE_S_BASE
, 4,
156 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
157 CCU_DIV_VAR_INFO(CCU_AXI_USB_CLK
, "axi_usb_clk", "sata_clk",
159 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
160 CCU_DIV_VAR_INFO(CCU_AXI_HWA_CLK
, "axi_hwa_clk", "sata_clk",
162 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
),
163 CCU_DIV_VAR_INFO(CCU_AXI_SRAM_CLK
, "axi_sram_clk", "eth_clk",
164 CCU_AXI_SRAM_BASE
, 4,
165 CLK_SET_RATE_GATE
, CCU_DIV_RESET_DOMAIN
)
169 * APB-bus clock is marked as critical since it's a main communication bus
170 * for the SoC devices registers IO-operations.
172 static const struct ccu_div_info sys_info
[] = {
173 CCU_DIV_VAR_INFO(CCU_SYS_SATA_CLK
, "sys_sata_clk",
174 "sata_clk", CCU_SYS_SATA_REF_BASE
, 4,
176 CCU_DIV_SKIP_ONE
| CCU_DIV_LOCK_SHIFTED
|
177 CCU_DIV_RESET_DOMAIN
),
178 CCU_DIV_BUF_INFO(CCU_SYS_SATA_REF_CLK
, "sys_sata_ref_clk",
179 "sys_sata_clk", CCU_SYS_SATA_REF_BASE
,
180 CLK_SET_RATE_PARENT
),
181 CCU_DIV_VAR_INFO(CCU_SYS_APB_CLK
, "sys_apb_clk",
182 "pcie_clk", CCU_SYS_APB_BASE
, 5,
183 CLK_IS_CRITICAL
, CCU_DIV_BASIC
| CCU_DIV_RESET_DOMAIN
),
184 CCU_DIV_GATE_INFO(CCU_SYS_GMAC0_TX_CLK
, "sys_gmac0_tx_clk",
185 "eth_clk", CCU_SYS_GMAC0_BASE
, 5),
186 CCU_DIV_FIXED_INFO(CCU_SYS_GMAC0_PTP_CLK
, "sys_gmac0_ptp_clk",
188 CCU_DIV_GATE_INFO(CCU_SYS_GMAC1_TX_CLK
, "sys_gmac1_tx_clk",
189 "eth_clk", CCU_SYS_GMAC1_BASE
, 5),
190 CCU_DIV_FIXED_INFO(CCU_SYS_GMAC1_PTP_CLK
, "sys_gmac1_ptp_clk",
192 CCU_DIV_GATE_INFO(CCU_SYS_XGMAC_CLK
, "sys_xgmac_clk",
193 "eth_clk", CCU_SYS_XGMAC_BASE
, 1),
194 CCU_DIV_FIXED_INFO(CCU_SYS_XGMAC_REF_CLK
, "sys_xgmac_ref_clk",
196 CCU_DIV_FIXED_INFO(CCU_SYS_XGMAC_PTP_CLK
, "sys_xgmac_ptp_clk",
198 CCU_DIV_GATE_INFO(CCU_SYS_USB_CLK
, "sys_usb_clk",
199 "eth_clk", CCU_SYS_USB_BASE
, 10),
200 CCU_DIV_VAR_INFO(CCU_SYS_PVT_CLK
, "sys_pvt_clk",
201 "ref_clk", CCU_SYS_PVT_BASE
, 5,
202 CLK_SET_RATE_GATE
, 0),
203 CCU_DIV_VAR_INFO(CCU_SYS_HWA_CLK
, "sys_hwa_clk",
204 "sata_clk", CCU_SYS_HWA_BASE
, 4,
205 CLK_SET_RATE_GATE
, 0),
206 CCU_DIV_VAR_INFO(CCU_SYS_UART_CLK
, "sys_uart_clk",
207 "eth_clk", CCU_SYS_UART_BASE
, 17,
208 CLK_SET_RATE_GATE
, 0),
209 CCU_DIV_FIXED_INFO(CCU_SYS_I2C1_CLK
, "sys_i2c1_clk",
211 CCU_DIV_FIXED_INFO(CCU_SYS_I2C2_CLK
, "sys_i2c2_clk",
213 CCU_DIV_FIXED_INFO(CCU_SYS_GPIO_CLK
, "sys_gpio_clk",
215 CCU_DIV_VAR_INFO(CCU_SYS_TIMER0_CLK
, "sys_timer0_clk",
216 "ref_clk", CCU_SYS_TIMER0_BASE
, 17,
217 CLK_SET_RATE_GATE
, CCU_DIV_BASIC
),
218 CCU_DIV_VAR_INFO(CCU_SYS_TIMER1_CLK
, "sys_timer1_clk",
219 "ref_clk", CCU_SYS_TIMER1_BASE
, 17,
220 CLK_SET_RATE_GATE
, CCU_DIV_BASIC
),
221 CCU_DIV_VAR_INFO(CCU_SYS_TIMER2_CLK
, "sys_timer2_clk",
222 "ref_clk", CCU_SYS_TIMER2_BASE
, 17,
223 CLK_SET_RATE_GATE
, CCU_DIV_BASIC
),
224 CCU_DIV_VAR_INFO(CCU_SYS_WDT_CLK
, "sys_wdt_clk",
225 "eth_clk", CCU_SYS_WDT_BASE
, 17,
226 CLK_SET_RATE_GATE
, CCU_DIV_SKIP_ONE_TO_THREE
)
229 static struct ccu_div_data
*axi_data
;
230 static struct ccu_div_data
*sys_data
;
232 static void ccu_div_set_data(struct ccu_div_data
*data
)
234 struct device_node
*np
= data
->np
;
236 if (of_device_is_compatible(np
, "baikal,bt1-ccu-axi"))
238 else if (of_device_is_compatible(np
, "baikal,bt1-ccu-sys"))
241 pr_err("Invalid DT node '%s' specified\n", of_node_full_name(np
));
244 static struct ccu_div_data
*ccu_div_get_data(struct device_node
*np
)
246 if (of_device_is_compatible(np
, "baikal,bt1-ccu-axi"))
248 else if (of_device_is_compatible(np
, "baikal,bt1-ccu-sys"))
251 pr_err("Invalid DT node '%s' specified\n", of_node_full_name(np
));
256 static struct ccu_div
*ccu_div_find_desc(struct ccu_div_data
*data
,
261 for (idx
= 0; idx
< data
->divs_num
; ++idx
) {
262 if (data
->divs_info
[idx
].id
== clk_id
)
263 return data
->divs
[idx
];
266 return ERR_PTR(-EINVAL
);
269 static struct ccu_div_data
*ccu_div_create_data(struct device_node
*np
)
271 struct ccu_div_data
*data
;
274 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
276 return ERR_PTR(-ENOMEM
);
279 if (of_device_is_compatible(np
, "baikal,bt1-ccu-axi")) {
280 data
->divs_num
= ARRAY_SIZE(axi_info
);
281 data
->divs_info
= axi_info
;
282 } else if (of_device_is_compatible(np
, "baikal,bt1-ccu-sys")) {
283 data
->divs_num
= ARRAY_SIZE(sys_info
);
284 data
->divs_info
= sys_info
;
286 pr_err("Incompatible DT node '%s' specified\n",
287 of_node_full_name(np
));
292 data
->divs
= kcalloc(data
->divs_num
, sizeof(*data
->divs
), GFP_KERNEL
);
306 static void ccu_div_free_data(struct ccu_div_data
*data
)
313 static int ccu_div_find_sys_regs(struct ccu_div_data
*data
)
315 data
->sys_regs
= syscon_node_to_regmap(data
->np
->parent
);
316 if (IS_ERR(data
->sys_regs
)) {
317 pr_err("Failed to find syscon regs for '%s'\n",
318 of_node_full_name(data
->np
));
319 return PTR_ERR(data
->sys_regs
);
325 static struct clk_hw
*ccu_div_of_clk_hw_get(struct of_phandle_args
*clkspec
,
328 struct ccu_div_data
*data
= priv
;
332 clk_id
= clkspec
->args
[0];
333 div
= ccu_div_find_desc(data
, clk_id
);
335 if (div
!= ERR_PTR(-EPROBE_DEFER
))
336 pr_info("Invalid clock ID %d specified\n", clk_id
);
338 return ERR_CAST(div
);
341 return ccu_div_get_clk_hw(div
);
344 static int ccu_div_clk_register(struct ccu_div_data
*data
, bool defer
)
348 for (idx
= 0; idx
< data
->divs_num
; ++idx
) {
349 const struct ccu_div_info
*info
= &data
->divs_info
[idx
];
350 struct ccu_div_init_data init
= {0};
352 if (!!(info
->features
& CCU_DIV_BASIC
) ^ defer
) {
353 if (!data
->divs
[idx
])
354 data
->divs
[idx
] = ERR_PTR(-EPROBE_DEFER
);
360 init
.name
= info
->name
;
361 init
.parent_name
= info
->parent_name
;
363 init
.type
= info
->type
;
364 init
.flags
= info
->flags
;
365 init
.features
= info
->features
;
367 if (init
.type
== CCU_DIV_VAR
) {
368 init
.base
= info
->base
;
369 init
.sys_regs
= data
->sys_regs
;
370 init
.width
= info
->width
;
371 } else if (init
.type
== CCU_DIV_GATE
) {
372 init
.base
= info
->base
;
373 init
.sys_regs
= data
->sys_regs
;
374 init
.divider
= info
->divider
;
375 } else if (init
.type
== CCU_DIV_BUF
) {
376 init
.base
= info
->base
;
377 init
.sys_regs
= data
->sys_regs
;
379 init
.divider
= info
->divider
;
382 data
->divs
[idx
] = ccu_div_hw_register(&init
);
383 if (IS_ERR(data
->divs
[idx
])) {
384 ret
= PTR_ERR(data
->divs
[idx
]);
385 pr_err("Couldn't register divider '%s' hw\n",
387 goto err_hw_unregister
;
394 for (--idx
; idx
>= 0; --idx
) {
395 if (!!(data
->divs_info
[idx
].features
& CCU_DIV_BASIC
) ^ defer
)
398 ccu_div_hw_unregister(data
->divs
[idx
]);
404 static void ccu_div_clk_unregister(struct ccu_div_data
*data
, bool defer
)
408 /* Uninstall only the clocks registered on the specfied stage */
409 for (idx
= 0; idx
< data
->divs_num
; ++idx
) {
410 if (!!(data
->divs_info
[idx
].features
& CCU_DIV_BASIC
) ^ defer
)
413 ccu_div_hw_unregister(data
->divs
[idx
]);
417 static int ccu_div_of_register(struct ccu_div_data
*data
)
421 ret
= of_clk_add_hw_provider(data
->np
, ccu_div_of_clk_hw_get
, data
);
423 pr_err("Couldn't register dividers '%s' clock provider\n",
424 of_node_full_name(data
->np
));
430 static int ccu_div_rst_register(struct ccu_div_data
*data
)
432 struct ccu_rst_init_data init
= {0};
434 init
.sys_regs
= data
->sys_regs
;
437 data
->rsts
= ccu_rst_hw_register(&init
);
438 if (IS_ERR(data
->rsts
)) {
439 pr_err("Couldn't register divider '%s' reset controller\n",
440 of_node_full_name(data
->np
));
441 return PTR_ERR(data
->rsts
);
447 static int ccu_div_probe(struct platform_device
*pdev
)
449 struct ccu_div_data
*data
;
452 data
= ccu_div_get_data(dev_of_node(&pdev
->dev
));
456 ret
= ccu_div_clk_register(data
, false);
460 ret
= ccu_div_rst_register(data
);
462 goto err_clk_unregister
;
467 ccu_div_clk_unregister(data
, false);
472 static const struct of_device_id ccu_div_of_match
[] = {
473 { .compatible
= "baikal,bt1-ccu-axi" },
474 { .compatible
= "baikal,bt1-ccu-sys" },
478 static struct platform_driver ccu_div_driver
= {
479 .probe
= ccu_div_probe
,
481 .name
= "clk-ccu-div",
482 .of_match_table
= ccu_div_of_match
,
483 .suppress_bind_attrs
= true,
486 builtin_platform_driver(ccu_div_driver
);
488 static __init
void ccu_div_init(struct device_node
*np
)
490 struct ccu_div_data
*data
;
493 data
= ccu_div_create_data(np
);
497 ret
= ccu_div_find_sys_regs(data
);
501 ret
= ccu_div_clk_register(data
, true);
505 ret
= ccu_div_of_register(data
);
507 goto err_clk_unregister
;
509 ccu_div_set_data(data
);
514 ccu_div_clk_unregister(data
, true);
517 ccu_div_free_data(data
);
519 CLK_OF_DECLARE_DRIVER(ccu_axi
, "baikal,bt1-ccu-axi", ccu_div_init
);
520 CLK_OF_DECLARE_DRIVER(ccu_sys
, "baikal,bt1-ccu-sys", ccu_div_init
);