1 // SPDX-License-Identifier: GPL-2.0-only
3 * Purna Chandra Mandal,<purna.mandal@microchip.com>
4 * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
6 #include <dt-bindings/clock/microchip,pic32-clock.h>
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <asm/traps.h>
20 #define OSC_FRCDIV_MASK 0x07
21 #define OSC_FRCDIV_SHIFT 24
24 #define PLL_ICLK_MASK 0x01
25 #define PLL_ICLK_SHIFT 7
27 #define DECLARE_PERIPHERAL_CLOCK(__clk_name, __reg, __flags) \
29 .ctrl_reg = (__reg), \
31 .name = (__clk_name), \
32 .parent_names = (const char *[]) { \
36 .ops = &pic32_pbclk_ops, \
41 #define DECLARE_REFO_CLOCK(__clkid, __reg) \
43 .ctrl_reg = (__reg), \
45 .name = "refo" #__clkid "_clk", \
46 .parent_names = (const char *[]) { \
47 "sys_clk", "pb1_clk", "posc_clk", \
48 "frc_clk", "lprc_clk", "sosc_clk", \
49 "sys_pll", "refi" #__clkid "_clk", \
53 .flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE,\
54 .ops = &pic32_roclk_ops, \
56 .parent_map = (const u32[]) { \
57 0, 1, 2, 3, 4, 5, 7, 8, 9 \
61 static const struct pic32_ref_osc_data ref_clks
[] = {
62 DECLARE_REFO_CLOCK(1, 0x80),
63 DECLARE_REFO_CLOCK(2, 0xa0),
64 DECLARE_REFO_CLOCK(3, 0xc0),
65 DECLARE_REFO_CLOCK(4, 0xe0),
66 DECLARE_REFO_CLOCK(5, 0x100),
69 static const struct pic32_periph_clk_data periph_clocks
[] = {
70 DECLARE_PERIPHERAL_CLOCK("pb1_clk", 0x140, 0),
71 DECLARE_PERIPHERAL_CLOCK("pb2_clk", 0x150, CLK_IGNORE_UNUSED
),
72 DECLARE_PERIPHERAL_CLOCK("pb3_clk", 0x160, 0),
73 DECLARE_PERIPHERAL_CLOCK("pb4_clk", 0x170, 0),
74 DECLARE_PERIPHERAL_CLOCK("pb5_clk", 0x180, 0),
75 DECLARE_PERIPHERAL_CLOCK("pb6_clk", 0x190, 0),
76 DECLARE_PERIPHERAL_CLOCK("cpu_clk", 0x1a0, CLK_IGNORE_UNUSED
),
79 static const struct pic32_sys_clk_data sys_mux_clk
= {
81 .slew_div
= 2, /* step of div_4 -> div_2 -> no_div */
84 .parent_names
= (const char *[]) {
85 "frcdiv_clk", "sys_pll", "posc_clk",
86 "sosc_clk", "lprc_clk", "frcdiv_clk",
89 .ops
= &pic32_sclk_ops
,
91 .parent_map
= (const u32
[]) {
96 static const struct pic32_sys_pll_data sys_pll
= {
102 .parent_names
= (const char *[]) {
106 .ops
= &pic32_spll_ops
,
110 static const struct pic32_sec_osc_data sosc_clk
= {
112 .enable_mask
= BIT(1),
113 .status_mask
= BIT(4),
117 .parent_names
= NULL
,
118 .ops
= &pic32_sosc_ops
,
122 static int pic32mzda_critical_clks
[] = {
126 /* PIC32MZDA clock data */
127 struct pic32mzda_clk_data
{
128 struct clk
*clks
[MAXCLKS
];
129 struct pic32_clk_common core
;
130 struct clk_onecell_data onecell_data
;
131 struct notifier_block failsafe_notifier
;
134 static int pic32_fscm_nmi(struct notifier_block
*nb
,
135 unsigned long action
, void *data
)
137 struct pic32mzda_clk_data
*cd
;
139 cd
= container_of(nb
, struct pic32mzda_clk_data
, failsafe_notifier
);
141 /* SYSCLK is now running from BFRCCLK. Report clock failure. */
142 if (readl(cd
->core
.iobase
) & BIT(2))
143 pr_alert("pic32-clk: FSCM detected clk failure.\n");
145 /* TODO: detect reason of failure and recover accordingly */
150 static int pic32mzda_clk_probe(struct platform_device
*pdev
)
152 const char *const pll_mux_parents
[] = {"posc_clk", "frc_clk"};
153 struct device_node
*np
= pdev
->dev
.of_node
;
154 struct pic32mzda_clk_data
*cd
;
155 struct pic32_clk_common
*core
;
156 struct clk
*pll_mux_clk
, *clk
;
160 cd
= devm_kzalloc(&pdev
->dev
, sizeof(*cd
), GFP_KERNEL
);
165 core
->iobase
= of_io_request_and_map(np
, 0, of_node_full_name(np
));
166 if (IS_ERR(core
->iobase
)) {
167 dev_err(&pdev
->dev
, "pic32-clk: failed to map registers\n");
168 return PTR_ERR(core
->iobase
);
171 spin_lock_init(&core
->reg_lock
);
172 core
->dev
= &pdev
->dev
;
175 /* register fixed rate clocks */
176 clks
[POSCCLK
] = clk_register_fixed_rate(&pdev
->dev
, "posc_clk", NULL
,
178 clks
[FRCCLK
] = clk_register_fixed_rate(&pdev
->dev
, "frc_clk", NULL
,
180 clks
[BFRCCLK
] = clk_register_fixed_rate(&pdev
->dev
, "bfrc_clk", NULL
,
182 clks
[LPRCCLK
] = clk_register_fixed_rate(&pdev
->dev
, "lprc_clk", NULL
,
184 clks
[UPLLCLK
] = clk_register_fixed_rate(&pdev
->dev
, "usbphy_clk", NULL
,
186 /* fixed rate (optional) clock */
187 if (of_find_property(np
, "microchip,pic32mzda-sosc", NULL
)) {
188 pr_info("pic32-clk: dt requests SOSC.\n");
189 clks
[SOSCCLK
] = pic32_sosc_clk_register(&sosc_clk
, core
);
192 clks
[FRCDIVCLK
] = clk_register_divider(&pdev
->dev
, "frcdiv_clk",
197 CLK_DIVIDER_POWER_OF_TWO
,
200 pll_mux_clk
= clk_register_mux(&pdev
->dev
, "spll_mux_clk",
201 pll_mux_parents
, 2, 0,
202 core
->iobase
+ 0x020,
203 PLL_ICLK_SHIFT
, 1, 0, &core
->reg_lock
);
204 if (IS_ERR(pll_mux_clk
))
205 pr_err("spll_mux_clk: clk register failed\n");
208 clks
[PLLCLK
] = pic32_spll_clk_register(&sys_pll
, core
);
210 clks
[SCLK
] = pic32_sys_clk_register(&sys_mux_clk
, core
);
211 /* Peripheral bus clocks */
212 for (nr_clks
= PB1CLK
, i
= 0; nr_clks
<= PB7CLK
; i
++, nr_clks
++)
213 clks
[nr_clks
] = pic32_periph_clk_register(&periph_clocks
[i
],
215 /* Reference oscillator clock */
216 for (nr_clks
= REF1CLK
, i
= 0; nr_clks
<= REF5CLK
; i
++, nr_clks
++)
217 clks
[nr_clks
] = pic32_refo_clk_register(&ref_clks
[i
], core
);
219 /* register clkdev */
220 for (i
= 0; i
< MAXCLKS
; i
++) {
223 clk_register_clkdev(clks
[i
], NULL
, __clk_get_name(clks
[i
]));
226 /* register clock provider */
227 cd
->onecell_data
.clks
= clks
;
228 cd
->onecell_data
.clk_num
= MAXCLKS
;
229 ret
= of_clk_add_provider(np
, of_clk_src_onecell_get
,
234 /* force enable critical clocks */
235 for (i
= 0; i
< ARRAY_SIZE(pic32mzda_critical_clks
); i
++) {
236 clk
= clks
[pic32mzda_critical_clks
[i
]];
237 if (clk_prepare_enable(clk
))
238 dev_err(&pdev
->dev
, "clk_prepare_enable(%s) failed\n",
239 __clk_get_name(clk
));
242 /* register NMI for failsafe clock monitor */
243 cd
->failsafe_notifier
.notifier_call
= pic32_fscm_nmi
;
244 return register_nmi_notifier(&cd
->failsafe_notifier
);
247 static const struct of_device_id pic32mzda_clk_match_table
[] = {
248 { .compatible
= "microchip,pic32mzda-clk", },
251 MODULE_DEVICE_TABLE(of
, pic32mzda_clk_match_table
);
253 static struct platform_driver pic32mzda_clk_driver
= {
254 .probe
= pic32mzda_clk_probe
,
256 .name
= "clk-pic32mzda",
257 .of_match_table
= pic32mzda_clk_match_table
,
261 static int __init
microchip_pic32mzda_clk_init(void)
263 return platform_driver_register(&pic32mzda_clk_driver
);
265 core_initcall(microchip_pic32mzda_clk_init
);
267 MODULE_DESCRIPTION("Microchip PIC32MZDA Clock Driver");
268 MODULE_LICENSE("GPL v2");
269 MODULE_ALIAS("platform:clk-pic32mzda");