xref: /openbmc/linux/drivers/clk/microchip/clk-pic32mzda.c (revision a38c94106e0dc77490ff820450c0c4af3b57fbc7)
1ce6e1188SPurna Chandra Mandal /*
2ce6e1188SPurna Chandra Mandal  * Purna Chandra Mandal,<purna.mandal@microchip.com>
3ce6e1188SPurna Chandra Mandal  * Copyright (C) 2015 Microchip Technology Inc.  All rights reserved.
4ce6e1188SPurna Chandra Mandal  *
5ce6e1188SPurna Chandra Mandal  * This program is free software; you can distribute it and/or modify it
6ce6e1188SPurna Chandra Mandal  * under the terms of the GNU General Public License (Version 2) as
7ce6e1188SPurna Chandra Mandal  * published by the Free Software Foundation.
8ce6e1188SPurna Chandra Mandal  *
9ce6e1188SPurna Chandra Mandal  * This program is distributed in the hope it will be useful, but WITHOUT
10ce6e1188SPurna Chandra Mandal  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11ce6e1188SPurna Chandra Mandal  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12ce6e1188SPurna Chandra Mandal  * for more details.
13ce6e1188SPurna Chandra Mandal  */
14ce6e1188SPurna Chandra Mandal #include <dt-bindings/clock/microchip,pic32-clock.h>
15ce6e1188SPurna Chandra Mandal #include <linux/clk.h>
16ce6e1188SPurna Chandra Mandal #include <linux/clk-provider.h>
17ce6e1188SPurna Chandra Mandal #include <linux/clkdev.h>
18ce6e1188SPurna Chandra Mandal #include <linux/module.h>
19ce6e1188SPurna Chandra Mandal #include <linux/of_address.h>
20ce6e1188SPurna Chandra Mandal #include <linux/of_platform.h>
21ce6e1188SPurna Chandra Mandal #include <linux/platform_device.h>
22ce6e1188SPurna Chandra Mandal #include <asm/traps.h>
23ce6e1188SPurna Chandra Mandal 
24ce6e1188SPurna Chandra Mandal #include "clk-core.h"
25ce6e1188SPurna Chandra Mandal 
26ce6e1188SPurna Chandra Mandal /* FRC Postscaler */
27ce6e1188SPurna Chandra Mandal #define OSC_FRCDIV_MASK		0x07
28ce6e1188SPurna Chandra Mandal #define OSC_FRCDIV_SHIFT	24
29ce6e1188SPurna Chandra Mandal 
30ce6e1188SPurna Chandra Mandal /* SPLL fields */
31ce6e1188SPurna Chandra Mandal #define PLL_ICLK_MASK		0x01
32ce6e1188SPurna Chandra Mandal #define PLL_ICLK_SHIFT		7
33ce6e1188SPurna Chandra Mandal 
34ce6e1188SPurna Chandra Mandal #define DECLARE_PERIPHERAL_CLOCK(__clk_name, __reg, __flags)	\
35ce6e1188SPurna Chandra Mandal 	{							\
36ce6e1188SPurna Chandra Mandal 		.ctrl_reg = (__reg),				\
37ce6e1188SPurna Chandra Mandal 		.init_data = {					\
38ce6e1188SPurna Chandra Mandal 			.name = (__clk_name),			\
39ce6e1188SPurna Chandra Mandal 			.parent_names = (const char *[]) {	\
40ce6e1188SPurna Chandra Mandal 				"sys_clk"			\
41ce6e1188SPurna Chandra Mandal 			},					\
42ce6e1188SPurna Chandra Mandal 			.num_parents = 1,			\
43ce6e1188SPurna Chandra Mandal 			.ops = &pic32_pbclk_ops,		\
44ce6e1188SPurna Chandra Mandal 			.flags = (__flags),			\
45ce6e1188SPurna Chandra Mandal 		},						\
46ce6e1188SPurna Chandra Mandal 	}
47ce6e1188SPurna Chandra Mandal 
48ce6e1188SPurna Chandra Mandal #define DECLARE_REFO_CLOCK(__clkid, __reg)				\
49ce6e1188SPurna Chandra Mandal 	{								\
50ce6e1188SPurna Chandra Mandal 		.ctrl_reg = (__reg),					\
51ce6e1188SPurna Chandra Mandal 		.init_data = {						\
52ce6e1188SPurna Chandra Mandal 			.name = "refo" #__clkid "_clk",			\
53ce6e1188SPurna Chandra Mandal 			.parent_names = (const char *[]) {		\
54ce6e1188SPurna Chandra Mandal 				"sys_clk", "pb1_clk", "posc_clk",	\
55ce6e1188SPurna Chandra Mandal 				"frc_clk", "lprc_clk", "sosc_clk",	\
56ce6e1188SPurna Chandra Mandal 				"sys_pll", "refi" #__clkid "_clk",	\
57ce6e1188SPurna Chandra Mandal 				"bfrc_clk",				\
58ce6e1188SPurna Chandra Mandal 			},						\
59ce6e1188SPurna Chandra Mandal 			.num_parents = 9,				\
60ce6e1188SPurna Chandra Mandal 			.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE,\
61ce6e1188SPurna Chandra Mandal 			.ops = &pic32_roclk_ops,			\
62ce6e1188SPurna Chandra Mandal 		},							\
63ce6e1188SPurna Chandra Mandal 		.parent_map = (const u32[]) {				\
64ce6e1188SPurna Chandra Mandal 			0, 1, 2, 3, 4, 5, 7, 8, 9			\
65ce6e1188SPurna Chandra Mandal 		},							\
66ce6e1188SPurna Chandra Mandal 	}
67ce6e1188SPurna Chandra Mandal 
68ce6e1188SPurna Chandra Mandal static const struct pic32_ref_osc_data ref_clks[] = {
69ce6e1188SPurna Chandra Mandal 	DECLARE_REFO_CLOCK(1, 0x80),
70ce6e1188SPurna Chandra Mandal 	DECLARE_REFO_CLOCK(2, 0xa0),
71ce6e1188SPurna Chandra Mandal 	DECLARE_REFO_CLOCK(3, 0xc0),
72ce6e1188SPurna Chandra Mandal 	DECLARE_REFO_CLOCK(4, 0xe0),
73ce6e1188SPurna Chandra Mandal 	DECLARE_REFO_CLOCK(5, 0x100),
74ce6e1188SPurna Chandra Mandal };
75ce6e1188SPurna Chandra Mandal 
76ce6e1188SPurna Chandra Mandal static const struct pic32_periph_clk_data periph_clocks[] = {
77ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb1_clk", 0x140, 0),
78ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb2_clk", 0x150, CLK_IGNORE_UNUSED),
79ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb3_clk", 0x160, 0),
80ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb4_clk", 0x170, 0),
81ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb5_clk", 0x180, 0),
82ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("pb6_clk", 0x190, 0),
83ce6e1188SPurna Chandra Mandal 	DECLARE_PERIPHERAL_CLOCK("cpu_clk", 0x1a0, CLK_IGNORE_UNUSED),
84ce6e1188SPurna Chandra Mandal };
85ce6e1188SPurna Chandra Mandal 
86ce6e1188SPurna Chandra Mandal static const struct pic32_sys_clk_data sys_mux_clk = {
87ce6e1188SPurna Chandra Mandal 	.slew_reg = 0x1c0,
88ce6e1188SPurna Chandra Mandal 	.slew_div = 2, /* step of div_4 -> div_2 -> no_div */
89ce6e1188SPurna Chandra Mandal 	.init_data = {
90ce6e1188SPurna Chandra Mandal 		.name = "sys_clk",
91ce6e1188SPurna Chandra Mandal 		.parent_names = (const char *[]) {
92ce6e1188SPurna Chandra Mandal 			"frcdiv_clk", "sys_pll", "posc_clk",
93ce6e1188SPurna Chandra Mandal 			"sosc_clk", "lprc_clk", "frcdiv_clk",
94ce6e1188SPurna Chandra Mandal 		},
95ce6e1188SPurna Chandra Mandal 		.num_parents = 6,
96ce6e1188SPurna Chandra Mandal 		.ops = &pic32_sclk_ops,
97ce6e1188SPurna Chandra Mandal 	},
98ce6e1188SPurna Chandra Mandal 	.parent_map = (const u32[]) {
99ce6e1188SPurna Chandra Mandal 		0, 1, 2, 4, 5, 7,
100ce6e1188SPurna Chandra Mandal 	},
101ce6e1188SPurna Chandra Mandal };
102ce6e1188SPurna Chandra Mandal 
103ce6e1188SPurna Chandra Mandal static const struct pic32_sys_pll_data sys_pll = {
104ce6e1188SPurna Chandra Mandal 	.ctrl_reg = 0x020,
105ce6e1188SPurna Chandra Mandal 	.status_reg = 0x1d0,
106ce6e1188SPurna Chandra Mandal 	.lock_mask = BIT(7),
107ce6e1188SPurna Chandra Mandal 	.init_data = {
108ce6e1188SPurna Chandra Mandal 		.name = "sys_pll",
109ce6e1188SPurna Chandra Mandal 		.parent_names = (const char *[]) {
110ce6e1188SPurna Chandra Mandal 			"spll_mux_clk"
111ce6e1188SPurna Chandra Mandal 		},
112ce6e1188SPurna Chandra Mandal 		.num_parents = 1,
113ce6e1188SPurna Chandra Mandal 		.ops = &pic32_spll_ops,
114ce6e1188SPurna Chandra Mandal 	},
115ce6e1188SPurna Chandra Mandal };
116ce6e1188SPurna Chandra Mandal 
117ce6e1188SPurna Chandra Mandal static const struct pic32_sec_osc_data sosc_clk = {
118ce6e1188SPurna Chandra Mandal 	.status_reg = 0x1d0,
119ce6e1188SPurna Chandra Mandal 	.enable_mask = BIT(1),
120ce6e1188SPurna Chandra Mandal 	.status_mask = BIT(4),
121*a38c9410SPurna Chandra Mandal 	.fixed_rate = 32768,
122ce6e1188SPurna Chandra Mandal 	.init_data = {
123ce6e1188SPurna Chandra Mandal 		.name = "sosc_clk",
124ce6e1188SPurna Chandra Mandal 		.parent_names = NULL,
125ce6e1188SPurna Chandra Mandal 		.ops = &pic32_sosc_ops,
126ce6e1188SPurna Chandra Mandal 	},
127ce6e1188SPurna Chandra Mandal };
128ce6e1188SPurna Chandra Mandal 
129ce6e1188SPurna Chandra Mandal static int pic32mzda_critical_clks[] = {
130ce6e1188SPurna Chandra Mandal 	PB2CLK, PB7CLK
131ce6e1188SPurna Chandra Mandal };
132ce6e1188SPurna Chandra Mandal 
133ce6e1188SPurna Chandra Mandal /* PIC32MZDA clock data */
134ce6e1188SPurna Chandra Mandal struct pic32mzda_clk_data {
135ce6e1188SPurna Chandra Mandal 	struct clk *clks[MAXCLKS];
136ce6e1188SPurna Chandra Mandal 	struct pic32_clk_common core;
137ce6e1188SPurna Chandra Mandal 	struct clk_onecell_data onecell_data;
138ce6e1188SPurna Chandra Mandal 	struct notifier_block failsafe_notifier;
139ce6e1188SPurna Chandra Mandal };
140ce6e1188SPurna Chandra Mandal 
141ce6e1188SPurna Chandra Mandal static int pic32_fscm_nmi(struct notifier_block *nb,
142ce6e1188SPurna Chandra Mandal 			  unsigned long action, void *data)
143ce6e1188SPurna Chandra Mandal {
144ce6e1188SPurna Chandra Mandal 	struct pic32mzda_clk_data *cd;
145ce6e1188SPurna Chandra Mandal 
146ce6e1188SPurna Chandra Mandal 	cd  = container_of(nb, struct pic32mzda_clk_data, failsafe_notifier);
147ce6e1188SPurna Chandra Mandal 
148ce6e1188SPurna Chandra Mandal 	/* SYSCLK is now running from BFRCCLK. Report clock failure. */
149ce6e1188SPurna Chandra Mandal 	if (readl(cd->core.iobase) & BIT(2))
150ce6e1188SPurna Chandra Mandal 		pr_alert("pic32-clk: FSCM detected clk failure.\n");
151ce6e1188SPurna Chandra Mandal 
152ce6e1188SPurna Chandra Mandal 	/* TODO: detect reason of failure and recover accordingly */
153ce6e1188SPurna Chandra Mandal 
154ce6e1188SPurna Chandra Mandal 	return NOTIFY_OK;
155ce6e1188SPurna Chandra Mandal }
156ce6e1188SPurna Chandra Mandal 
157ce6e1188SPurna Chandra Mandal static int pic32mzda_clk_probe(struct platform_device *pdev)
158ce6e1188SPurna Chandra Mandal {
159ce6e1188SPurna Chandra Mandal 	const char *const pll_mux_parents[] = {"posc_clk", "frc_clk"};
160ce6e1188SPurna Chandra Mandal 	struct device_node *np = pdev->dev.of_node;
161ce6e1188SPurna Chandra Mandal 	struct pic32mzda_clk_data *cd;
162ce6e1188SPurna Chandra Mandal 	struct pic32_clk_common *core;
163ce6e1188SPurna Chandra Mandal 	struct clk *pll_mux_clk, *clk;
164ce6e1188SPurna Chandra Mandal 	struct clk **clks;
165ce6e1188SPurna Chandra Mandal 	int nr_clks, i, ret;
166ce6e1188SPurna Chandra Mandal 
167ce6e1188SPurna Chandra Mandal 	cd = devm_kzalloc(&pdev->dev, sizeof(*cd), GFP_KERNEL);
168ce6e1188SPurna Chandra Mandal 	if (!cd)
169ce6e1188SPurna Chandra Mandal 		return -ENOMEM;
170ce6e1188SPurna Chandra Mandal 
171ce6e1188SPurna Chandra Mandal 	core = &cd->core;
172ce6e1188SPurna Chandra Mandal 	core->iobase = of_io_request_and_map(np, 0, of_node_full_name(np));
173ce6e1188SPurna Chandra Mandal 	if (IS_ERR(core->iobase)) {
174ce6e1188SPurna Chandra Mandal 		dev_err(&pdev->dev, "pic32-clk: failed to map registers\n");
175ce6e1188SPurna Chandra Mandal 		return PTR_ERR(core->iobase);
176ce6e1188SPurna Chandra Mandal 	}
177ce6e1188SPurna Chandra Mandal 
178ce6e1188SPurna Chandra Mandal 	spin_lock_init(&core->reg_lock);
179ce6e1188SPurna Chandra Mandal 	core->dev = &pdev->dev;
180ce6e1188SPurna Chandra Mandal 	clks = &cd->clks[0];
181ce6e1188SPurna Chandra Mandal 
182ce6e1188SPurna Chandra Mandal 	/* register fixed rate clocks */
183ce6e1188SPurna Chandra Mandal 	clks[POSCCLK] = clk_register_fixed_rate(&pdev->dev, "posc_clk", NULL,
1843c7f4f54SStephen Boyd 						0, 24000000);
185ce6e1188SPurna Chandra Mandal 	clks[FRCCLK] =  clk_register_fixed_rate(&pdev->dev, "frc_clk", NULL,
1863c7f4f54SStephen Boyd 						0, 8000000);
187ce6e1188SPurna Chandra Mandal 	clks[BFRCCLK] = clk_register_fixed_rate(&pdev->dev, "bfrc_clk", NULL,
1883c7f4f54SStephen Boyd 						0, 8000000);
189ce6e1188SPurna Chandra Mandal 	clks[LPRCCLK] = clk_register_fixed_rate(&pdev->dev, "lprc_clk", NULL,
1903c7f4f54SStephen Boyd 						0, 32000);
191ce6e1188SPurna Chandra Mandal 	clks[UPLLCLK] = clk_register_fixed_rate(&pdev->dev, "usbphy_clk", NULL,
1923c7f4f54SStephen Boyd 						0, 24000000);
193ce6e1188SPurna Chandra Mandal 	/* fixed rate (optional) clock */
194ce6e1188SPurna Chandra Mandal 	if (of_find_property(np, "microchip,pic32mzda-sosc", NULL)) {
195ce6e1188SPurna Chandra Mandal 		pr_info("pic32-clk: dt requests SOSC.\n");
196ce6e1188SPurna Chandra Mandal 		clks[SOSCCLK] = pic32_sosc_clk_register(&sosc_clk, core);
197ce6e1188SPurna Chandra Mandal 	}
198ce6e1188SPurna Chandra Mandal 	/* divider clock */
199ce6e1188SPurna Chandra Mandal 	clks[FRCDIVCLK] = clk_register_divider(&pdev->dev, "frcdiv_clk",
200ce6e1188SPurna Chandra Mandal 					       "frc_clk", 0,
201ce6e1188SPurna Chandra Mandal 					       core->iobase,
202ce6e1188SPurna Chandra Mandal 					       OSC_FRCDIV_SHIFT,
203ce6e1188SPurna Chandra Mandal 					       OSC_FRCDIV_MASK,
204ce6e1188SPurna Chandra Mandal 					       CLK_DIVIDER_POWER_OF_TWO,
205ce6e1188SPurna Chandra Mandal 					       &core->reg_lock);
206ce6e1188SPurna Chandra Mandal 	/* PLL ICLK mux */
207ce6e1188SPurna Chandra Mandal 	pll_mux_clk = clk_register_mux(&pdev->dev, "spll_mux_clk",
208ce6e1188SPurna Chandra Mandal 				       pll_mux_parents, 2, 0,
209ce6e1188SPurna Chandra Mandal 				       core->iobase + 0x020,
210ce6e1188SPurna Chandra Mandal 				       PLL_ICLK_SHIFT, 1, 0, &core->reg_lock);
211ce6e1188SPurna Chandra Mandal 	if (IS_ERR(pll_mux_clk))
212ce6e1188SPurna Chandra Mandal 		pr_err("spll_mux_clk: clk register failed\n");
213ce6e1188SPurna Chandra Mandal 
214ce6e1188SPurna Chandra Mandal 	/* PLL */
215ce6e1188SPurna Chandra Mandal 	clks[PLLCLK] = pic32_spll_clk_register(&sys_pll, core);
216ce6e1188SPurna Chandra Mandal 	/* SYSTEM clock */
217ce6e1188SPurna Chandra Mandal 	clks[SCLK] = pic32_sys_clk_register(&sys_mux_clk, core);
218ce6e1188SPurna Chandra Mandal 	/* Peripheral bus clocks */
219ce6e1188SPurna Chandra Mandal 	for (nr_clks = PB1CLK, i = 0; nr_clks <= PB7CLK; i++, nr_clks++)
220ce6e1188SPurna Chandra Mandal 		clks[nr_clks] = pic32_periph_clk_register(&periph_clocks[i],
221ce6e1188SPurna Chandra Mandal 							  core);
222ce6e1188SPurna Chandra Mandal 	/* Reference oscillator clock */
223ce6e1188SPurna Chandra Mandal 	for (nr_clks = REF1CLK, i = 0; nr_clks <= REF5CLK; i++, nr_clks++)
224ce6e1188SPurna Chandra Mandal 		clks[nr_clks] = pic32_refo_clk_register(&ref_clks[i], core);
225ce6e1188SPurna Chandra Mandal 
226ce6e1188SPurna Chandra Mandal 	/* register clkdev */
227ce6e1188SPurna Chandra Mandal 	for (i = 0; i < MAXCLKS; i++) {
228ce6e1188SPurna Chandra Mandal 		if (IS_ERR(clks[i]))
229ce6e1188SPurna Chandra Mandal 			continue;
230ce6e1188SPurna Chandra Mandal 		clk_register_clkdev(clks[i], NULL, __clk_get_name(clks[i]));
231ce6e1188SPurna Chandra Mandal 	}
232ce6e1188SPurna Chandra Mandal 
233ce6e1188SPurna Chandra Mandal 	/* register clock provider */
234ce6e1188SPurna Chandra Mandal 	cd->onecell_data.clks = clks;
235ce6e1188SPurna Chandra Mandal 	cd->onecell_data.clk_num = MAXCLKS;
236ce6e1188SPurna Chandra Mandal 	ret = of_clk_add_provider(np, of_clk_src_onecell_get,
237ce6e1188SPurna Chandra Mandal 				  &cd->onecell_data);
238ce6e1188SPurna Chandra Mandal 	if (ret)
239ce6e1188SPurna Chandra Mandal 		return ret;
240ce6e1188SPurna Chandra Mandal 
241ce6e1188SPurna Chandra Mandal 	/* force enable critical clocks */
242ce6e1188SPurna Chandra Mandal 	for (i = 0; i < ARRAY_SIZE(pic32mzda_critical_clks); i++) {
243ce6e1188SPurna Chandra Mandal 		clk = clks[pic32mzda_critical_clks[i]];
244ce6e1188SPurna Chandra Mandal 		if (clk_prepare_enable(clk))
245ce6e1188SPurna Chandra Mandal 			dev_err(&pdev->dev, "clk_prepare_enable(%s) failed\n",
246ce6e1188SPurna Chandra Mandal 				__clk_get_name(clk));
247ce6e1188SPurna Chandra Mandal 	}
248ce6e1188SPurna Chandra Mandal 
249ce6e1188SPurna Chandra Mandal 	/* register NMI for failsafe clock monitor */
250ce6e1188SPurna Chandra Mandal 	cd->failsafe_notifier.notifier_call = pic32_fscm_nmi;
251ce6e1188SPurna Chandra Mandal 	return register_nmi_notifier(&cd->failsafe_notifier);
252ce6e1188SPurna Chandra Mandal }
253ce6e1188SPurna Chandra Mandal 
254ce6e1188SPurna Chandra Mandal static const struct of_device_id pic32mzda_clk_match_table[] = {
255ce6e1188SPurna Chandra Mandal 	{ .compatible = "microchip,pic32mzda-clk", },
256ce6e1188SPurna Chandra Mandal 	{ }
257ce6e1188SPurna Chandra Mandal };
258ce6e1188SPurna Chandra Mandal MODULE_DEVICE_TABLE(of, pic32mzda_clk_match_table);
259ce6e1188SPurna Chandra Mandal 
260ce6e1188SPurna Chandra Mandal static struct platform_driver pic32mzda_clk_driver = {
261ce6e1188SPurna Chandra Mandal 	.probe		= pic32mzda_clk_probe,
262ce6e1188SPurna Chandra Mandal 	.driver		= {
263ce6e1188SPurna Chandra Mandal 		.name	= "clk-pic32mzda",
264ce6e1188SPurna Chandra Mandal 		.of_match_table = pic32mzda_clk_match_table,
265ce6e1188SPurna Chandra Mandal 	},
266ce6e1188SPurna Chandra Mandal };
267ce6e1188SPurna Chandra Mandal 
268ce6e1188SPurna Chandra Mandal static int __init microchip_pic32mzda_clk_init(void)
269ce6e1188SPurna Chandra Mandal {
270ce6e1188SPurna Chandra Mandal 	return platform_driver_register(&pic32mzda_clk_driver);
271ce6e1188SPurna Chandra Mandal }
272ce6e1188SPurna Chandra Mandal core_initcall(microchip_pic32mzda_clk_init);
273ce6e1188SPurna Chandra Mandal 
274ce6e1188SPurna Chandra Mandal MODULE_DESCRIPTION("Microchip PIC32MZDA Clock Driver");
275ce6e1188SPurna Chandra Mandal MODULE_LICENSE("GPL v2");
276ce6e1188SPurna Chandra Mandal MODULE_ALIAS("platform:clk-pic32mzda");
277