xref: /openbmc/linux/drivers/clk/at91/clk-utmi.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f090fb37SBoris BREZILLON /*
3f090fb37SBoris BREZILLON  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4f090fb37SBoris BREZILLON  */
5f090fb37SBoris BREZILLON 
6f090fb37SBoris BREZILLON #include <linux/clk-provider.h>
7f090fb37SBoris BREZILLON #include <linux/clkdev.h>
8f090fb37SBoris BREZILLON #include <linux/clk/at91_pmc.h>
9f090fb37SBoris BREZILLON #include <linux/of.h>
101bdf0232SBoris Brezillon #include <linux/mfd/syscon.h>
111bdf0232SBoris Brezillon #include <linux/regmap.h>
1292041a9fSLudovic Desroches #include <soc/at91/atmel-sfr.h>
13f090fb37SBoris BREZILLON 
14f090fb37SBoris BREZILLON #include "pmc.h"
15f090fb37SBoris BREZILLON 
1692041a9fSLudovic Desroches /*
1792041a9fSLudovic Desroches  * The purpose of this clock is to generate a 480 MHz signal. A different
1892041a9fSLudovic Desroches  * rate can't be configured.
1992041a9fSLudovic Desroches  */
2092041a9fSLudovic Desroches #define UTMI_RATE	480000000
21f090fb37SBoris BREZILLON 
22f090fb37SBoris BREZILLON struct clk_utmi {
23f090fb37SBoris BREZILLON 	struct clk_hw hw;
2492041a9fSLudovic Desroches 	struct regmap *regmap_pmc;
2592041a9fSLudovic Desroches 	struct regmap *regmap_sfr;
2636971566SClaudiu Beznea 	struct at91_clk_pms pms;
27f090fb37SBoris BREZILLON };
28f090fb37SBoris BREZILLON 
29f090fb37SBoris BREZILLON #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
30f090fb37SBoris BREZILLON 
clk_utmi_ready(struct regmap * regmap)311bdf0232SBoris Brezillon static inline bool clk_utmi_ready(struct regmap *regmap)
321bdf0232SBoris Brezillon {
331bdf0232SBoris Brezillon 	unsigned int status;
341bdf0232SBoris Brezillon 
351bdf0232SBoris Brezillon 	regmap_read(regmap, AT91_PMC_SR, &status);
361bdf0232SBoris Brezillon 
371bdf0232SBoris Brezillon 	return status & AT91_PMC_LOCKU;
381bdf0232SBoris Brezillon }
391bdf0232SBoris Brezillon 
clk_utmi_prepare(struct clk_hw * hw)40f090fb37SBoris BREZILLON static int clk_utmi_prepare(struct clk_hw *hw)
41f090fb37SBoris BREZILLON {
4292041a9fSLudovic Desroches 	struct clk_hw *hw_parent;
43f090fb37SBoris BREZILLON 	struct clk_utmi *utmi = to_clk_utmi(hw);
441bdf0232SBoris Brezillon 	unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
451bdf0232SBoris Brezillon 			    AT91_PMC_BIASEN;
4692041a9fSLudovic Desroches 	unsigned int utmi_ref_clk_freq;
4792041a9fSLudovic Desroches 	unsigned long parent_rate;
48f090fb37SBoris BREZILLON 
4992041a9fSLudovic Desroches 	/*
5092041a9fSLudovic Desroches 	 * If mainck rate is different from 12 MHz, we have to configure the
5192041a9fSLudovic Desroches 	 * FREQ field of the SFR_UTMICKTRIM register to generate properly
5292041a9fSLudovic Desroches 	 * the utmi clock.
5392041a9fSLudovic Desroches 	 */
5492041a9fSLudovic Desroches 	hw_parent = clk_hw_get_parent(hw);
5592041a9fSLudovic Desroches 	parent_rate = clk_hw_get_rate(hw_parent);
56f090fb37SBoris BREZILLON 
5792041a9fSLudovic Desroches 	switch (parent_rate) {
5892041a9fSLudovic Desroches 	case 12000000:
5992041a9fSLudovic Desroches 		utmi_ref_clk_freq = 0;
6092041a9fSLudovic Desroches 		break;
6192041a9fSLudovic Desroches 	case 16000000:
6292041a9fSLudovic Desroches 		utmi_ref_clk_freq = 1;
6392041a9fSLudovic Desroches 		break;
6492041a9fSLudovic Desroches 	case 24000000:
6592041a9fSLudovic Desroches 		utmi_ref_clk_freq = 2;
6692041a9fSLudovic Desroches 		break;
6792041a9fSLudovic Desroches 	/*
6892041a9fSLudovic Desroches 	 * Not supported on SAMA5D2 but it's not an issue since MAINCK
6992041a9fSLudovic Desroches 	 * maximum value is 24 MHz.
7092041a9fSLudovic Desroches 	 */
7192041a9fSLudovic Desroches 	case 48000000:
7292041a9fSLudovic Desroches 		utmi_ref_clk_freq = 3;
7392041a9fSLudovic Desroches 		break;
7492041a9fSLudovic Desroches 	default:
7592041a9fSLudovic Desroches 		pr_err("UTMICK: unsupported mainck rate\n");
7692041a9fSLudovic Desroches 		return -EINVAL;
7792041a9fSLudovic Desroches 	}
7892041a9fSLudovic Desroches 
7992041a9fSLudovic Desroches 	if (utmi->regmap_sfr) {
8092041a9fSLudovic Desroches 		regmap_update_bits(utmi->regmap_sfr, AT91_SFR_UTMICKTRIM,
8192041a9fSLudovic Desroches 				   AT91_UTMICKTRIM_FREQ, utmi_ref_clk_freq);
8292041a9fSLudovic Desroches 	} else if (utmi_ref_clk_freq) {
8392041a9fSLudovic Desroches 		pr_err("UTMICK: sfr node required\n");
8492041a9fSLudovic Desroches 		return -EINVAL;
8592041a9fSLudovic Desroches 	}
8692041a9fSLudovic Desroches 
8792041a9fSLudovic Desroches 	regmap_update_bits(utmi->regmap_pmc, AT91_CKGR_UCKR, uckr, uckr);
8892041a9fSLudovic Desroches 
8992041a9fSLudovic Desroches 	while (!clk_utmi_ready(utmi->regmap_pmc))
9099a81706SAlexandre Belloni 		cpu_relax();
91f090fb37SBoris BREZILLON 
92f090fb37SBoris BREZILLON 	return 0;
93f090fb37SBoris BREZILLON }
94f090fb37SBoris BREZILLON 
clk_utmi_is_prepared(struct clk_hw * hw)95f090fb37SBoris BREZILLON static int clk_utmi_is_prepared(struct clk_hw *hw)
96f090fb37SBoris BREZILLON {
97f090fb37SBoris BREZILLON 	struct clk_utmi *utmi = to_clk_utmi(hw);
98f090fb37SBoris BREZILLON 
9992041a9fSLudovic Desroches 	return clk_utmi_ready(utmi->regmap_pmc);
100f090fb37SBoris BREZILLON }
101f090fb37SBoris BREZILLON 
clk_utmi_unprepare(struct clk_hw * hw)102f090fb37SBoris BREZILLON static void clk_utmi_unprepare(struct clk_hw *hw)
103f090fb37SBoris BREZILLON {
104f090fb37SBoris BREZILLON 	struct clk_utmi *utmi = to_clk_utmi(hw);
105f090fb37SBoris BREZILLON 
10692041a9fSLudovic Desroches 	regmap_update_bits(utmi->regmap_pmc, AT91_CKGR_UCKR,
10792041a9fSLudovic Desroches 			   AT91_PMC_UPLLEN, 0);
108f090fb37SBoris BREZILLON }
109f090fb37SBoris BREZILLON 
clk_utmi_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)110f090fb37SBoris BREZILLON static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
111f090fb37SBoris BREZILLON 					  unsigned long parent_rate)
112f090fb37SBoris BREZILLON {
11392041a9fSLudovic Desroches 	/* UTMI clk rate is fixed. */
11492041a9fSLudovic Desroches 	return UTMI_RATE;
115f090fb37SBoris BREZILLON }
116f090fb37SBoris BREZILLON 
clk_utmi_save_context(struct clk_hw * hw)11736971566SClaudiu Beznea static int clk_utmi_save_context(struct clk_hw *hw)
11836971566SClaudiu Beznea {
11936971566SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
12036971566SClaudiu Beznea 
12136971566SClaudiu Beznea 	utmi->pms.status = clk_utmi_is_prepared(hw);
12236971566SClaudiu Beznea 
12336971566SClaudiu Beznea 	return 0;
12436971566SClaudiu Beznea }
12536971566SClaudiu Beznea 
clk_utmi_restore_context(struct clk_hw * hw)12636971566SClaudiu Beznea static void clk_utmi_restore_context(struct clk_hw *hw)
12736971566SClaudiu Beznea {
12836971566SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
12936971566SClaudiu Beznea 
13036971566SClaudiu Beznea 	if (utmi->pms.status)
13136971566SClaudiu Beznea 		clk_utmi_prepare(hw);
13236971566SClaudiu Beznea }
13336971566SClaudiu Beznea 
134f090fb37SBoris BREZILLON static const struct clk_ops utmi_ops = {
135f090fb37SBoris BREZILLON 	.prepare = clk_utmi_prepare,
136f090fb37SBoris BREZILLON 	.unprepare = clk_utmi_unprepare,
137f090fb37SBoris BREZILLON 	.is_prepared = clk_utmi_is_prepared,
138f090fb37SBoris BREZILLON 	.recalc_rate = clk_utmi_recalc_rate,
13936971566SClaudiu Beznea 	.save_context = clk_utmi_save_context,
14036971566SClaudiu Beznea 	.restore_context = clk_utmi_restore_context,
141f090fb37SBoris BREZILLON };
142f090fb37SBoris BREZILLON 
143ef396df9SClaudiu Beznea static struct clk_hw * __init
at91_clk_register_utmi_internal(struct regmap * regmap_pmc,struct regmap * regmap_sfr,const char * name,const char * parent_name,struct clk_hw * parent_hw,const struct clk_ops * ops,unsigned long flags)144ef396df9SClaudiu Beznea at91_clk_register_utmi_internal(struct regmap *regmap_pmc,
145ef396df9SClaudiu Beznea 				struct regmap *regmap_sfr,
146ef396df9SClaudiu Beznea 				const char *name, const char *parent_name,
147*077782e3SClaudiu Beznea 				struct clk_hw *parent_hw,
148ef396df9SClaudiu Beznea 				const struct clk_ops *ops, unsigned long flags)
149f090fb37SBoris BREZILLON {
150f090fb37SBoris BREZILLON 	struct clk_utmi *utmi;
151f5644f10SStephen Boyd 	struct clk_hw *hw;
152*077782e3SClaudiu Beznea 	struct clk_init_data init = {};
153f5644f10SStephen Boyd 	int ret;
154f090fb37SBoris BREZILLON 
155*077782e3SClaudiu Beznea 	if (!(parent_name || parent_hw))
156*077782e3SClaudiu Beznea 		return ERR_PTR(-EINVAL);
157*077782e3SClaudiu Beznea 
158f090fb37SBoris BREZILLON 	utmi = kzalloc(sizeof(*utmi), GFP_KERNEL);
159f090fb37SBoris BREZILLON 	if (!utmi)
160f090fb37SBoris BREZILLON 		return ERR_PTR(-ENOMEM);
161f090fb37SBoris BREZILLON 
162f090fb37SBoris BREZILLON 	init.name = name;
163ef396df9SClaudiu Beznea 	init.ops = ops;
164*077782e3SClaudiu Beznea 	if (parent_hw) {
165*077782e3SClaudiu Beznea 		init.parent_hws = parent_hw ? (const struct clk_hw **)&parent_hw : NULL;
166*077782e3SClaudiu Beznea 		init.num_parents = parent_hw ? 1 : 0;
167*077782e3SClaudiu Beznea 	} else {
168f090fb37SBoris BREZILLON 		init.parent_names = parent_name ? &parent_name : NULL;
169f090fb37SBoris BREZILLON 		init.num_parents = parent_name ? 1 : 0;
170*077782e3SClaudiu Beznea 	}
171ef396df9SClaudiu Beznea 	init.flags = flags;
172f090fb37SBoris BREZILLON 
173f090fb37SBoris BREZILLON 	utmi->hw.init = &init;
17492041a9fSLudovic Desroches 	utmi->regmap_pmc = regmap_pmc;
17592041a9fSLudovic Desroches 	utmi->regmap_sfr = regmap_sfr;
176f090fb37SBoris BREZILLON 
177f5644f10SStephen Boyd 	hw = &utmi->hw;
178f5644f10SStephen Boyd 	ret = clk_hw_register(NULL, &utmi->hw);
179f5644f10SStephen Boyd 	if (ret) {
180f090fb37SBoris BREZILLON 		kfree(utmi);
181f5644f10SStephen Boyd 		hw = ERR_PTR(ret);
182f5644f10SStephen Boyd 	}
183f090fb37SBoris BREZILLON 
184f5644f10SStephen Boyd 	return hw;
185f090fb37SBoris BREZILLON }
186ef396df9SClaudiu Beznea 
187ef396df9SClaudiu Beznea struct clk_hw * __init
at91_clk_register_utmi(struct regmap * regmap_pmc,struct regmap * regmap_sfr,const char * name,const char * parent_name,struct clk_hw * parent_hw)188ef396df9SClaudiu Beznea at91_clk_register_utmi(struct regmap *regmap_pmc, struct regmap *regmap_sfr,
189*077782e3SClaudiu Beznea 		       const char *name, const char *parent_name,
190*077782e3SClaudiu Beznea 		       struct clk_hw *parent_hw)
191ef396df9SClaudiu Beznea {
192ef396df9SClaudiu Beznea 	return at91_clk_register_utmi_internal(regmap_pmc, regmap_sfr, name,
193*077782e3SClaudiu Beznea 			parent_name, parent_hw, &utmi_ops, CLK_SET_RATE_GATE);
194ef396df9SClaudiu Beznea }
195ef396df9SClaudiu Beznea 
clk_utmi_sama7g5_prepare(struct clk_hw * hw)196ef396df9SClaudiu Beznea static int clk_utmi_sama7g5_prepare(struct clk_hw *hw)
197ef396df9SClaudiu Beznea {
198ef396df9SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
199ef396df9SClaudiu Beznea 	struct clk_hw *hw_parent;
200ef396df9SClaudiu Beznea 	unsigned long parent_rate;
201ef396df9SClaudiu Beznea 	unsigned int val;
202ef396df9SClaudiu Beznea 
203ef396df9SClaudiu Beznea 	hw_parent = clk_hw_get_parent(hw);
204ef396df9SClaudiu Beznea 	parent_rate = clk_hw_get_rate(hw_parent);
205ef396df9SClaudiu Beznea 
206ef396df9SClaudiu Beznea 	switch (parent_rate) {
207ef396df9SClaudiu Beznea 	case 16000000:
208ef396df9SClaudiu Beznea 		val = 0;
209ef396df9SClaudiu Beznea 		break;
210ef396df9SClaudiu Beznea 	case 20000000:
211ef396df9SClaudiu Beznea 		val = 2;
212ef396df9SClaudiu Beznea 		break;
213ef396df9SClaudiu Beznea 	case 24000000:
214ef396df9SClaudiu Beznea 		val = 3;
215ef396df9SClaudiu Beznea 		break;
216ef396df9SClaudiu Beznea 	case 32000000:
217ef396df9SClaudiu Beznea 		val = 5;
218ef396df9SClaudiu Beznea 		break;
219ef396df9SClaudiu Beznea 	default:
220ef396df9SClaudiu Beznea 		pr_err("UTMICK: unsupported main_xtal rate\n");
221ef396df9SClaudiu Beznea 		return -EINVAL;
222ef396df9SClaudiu Beznea 	}
223ef396df9SClaudiu Beznea 
224ef396df9SClaudiu Beznea 	regmap_write(utmi->regmap_pmc, AT91_PMC_XTALF, val);
225ef396df9SClaudiu Beznea 
226ef396df9SClaudiu Beznea 	return 0;
227ef396df9SClaudiu Beznea 
228ef396df9SClaudiu Beznea }
229ef396df9SClaudiu Beznea 
clk_utmi_sama7g5_is_prepared(struct clk_hw * hw)230ef396df9SClaudiu Beznea static int clk_utmi_sama7g5_is_prepared(struct clk_hw *hw)
231ef396df9SClaudiu Beznea {
232ef396df9SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
233ef396df9SClaudiu Beznea 	struct clk_hw *hw_parent;
234ef396df9SClaudiu Beznea 	unsigned long parent_rate;
235ef396df9SClaudiu Beznea 	unsigned int val;
236ef396df9SClaudiu Beznea 
237ef396df9SClaudiu Beznea 	hw_parent = clk_hw_get_parent(hw);
238ef396df9SClaudiu Beznea 	parent_rate = clk_hw_get_rate(hw_parent);
239ef396df9SClaudiu Beznea 
240ef396df9SClaudiu Beznea 	regmap_read(utmi->regmap_pmc, AT91_PMC_XTALF, &val);
241ef396df9SClaudiu Beznea 	switch (val & 0x7) {
242ef396df9SClaudiu Beznea 	case 0:
243ef396df9SClaudiu Beznea 		if (parent_rate == 16000000)
244ef396df9SClaudiu Beznea 			return 1;
245ef396df9SClaudiu Beznea 		break;
246ef396df9SClaudiu Beznea 	case 2:
247ef396df9SClaudiu Beznea 		if (parent_rate == 20000000)
248ef396df9SClaudiu Beznea 			return 1;
249ef396df9SClaudiu Beznea 		break;
250ef396df9SClaudiu Beznea 	case 3:
251ef396df9SClaudiu Beznea 		if (parent_rate == 24000000)
252ef396df9SClaudiu Beznea 			return 1;
253ef396df9SClaudiu Beznea 		break;
254ef396df9SClaudiu Beznea 	case 5:
255ef396df9SClaudiu Beznea 		if (parent_rate == 32000000)
256ef396df9SClaudiu Beznea 			return 1;
257ef396df9SClaudiu Beznea 		break;
258ef396df9SClaudiu Beznea 	default:
259ef396df9SClaudiu Beznea 		break;
260ef396df9SClaudiu Beznea 	}
261ef396df9SClaudiu Beznea 
262ef396df9SClaudiu Beznea 	return 0;
263ef396df9SClaudiu Beznea }
264ef396df9SClaudiu Beznea 
clk_utmi_sama7g5_save_context(struct clk_hw * hw)26536971566SClaudiu Beznea static int clk_utmi_sama7g5_save_context(struct clk_hw *hw)
26636971566SClaudiu Beznea {
26736971566SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
26836971566SClaudiu Beznea 
26936971566SClaudiu Beznea 	utmi->pms.status = clk_utmi_sama7g5_is_prepared(hw);
27036971566SClaudiu Beznea 
27136971566SClaudiu Beznea 	return 0;
27236971566SClaudiu Beznea }
27336971566SClaudiu Beznea 
clk_utmi_sama7g5_restore_context(struct clk_hw * hw)27436971566SClaudiu Beznea static void clk_utmi_sama7g5_restore_context(struct clk_hw *hw)
27536971566SClaudiu Beznea {
27636971566SClaudiu Beznea 	struct clk_utmi *utmi = to_clk_utmi(hw);
27736971566SClaudiu Beznea 
27836971566SClaudiu Beznea 	if (utmi->pms.status)
27936971566SClaudiu Beznea 		clk_utmi_sama7g5_prepare(hw);
28036971566SClaudiu Beznea }
28136971566SClaudiu Beznea 
282ef396df9SClaudiu Beznea static const struct clk_ops sama7g5_utmi_ops = {
283ef396df9SClaudiu Beznea 	.prepare = clk_utmi_sama7g5_prepare,
284ef396df9SClaudiu Beznea 	.is_prepared = clk_utmi_sama7g5_is_prepared,
285ef396df9SClaudiu Beznea 	.recalc_rate = clk_utmi_recalc_rate,
28636971566SClaudiu Beznea 	.save_context = clk_utmi_sama7g5_save_context,
28736971566SClaudiu Beznea 	.restore_context = clk_utmi_sama7g5_restore_context,
288ef396df9SClaudiu Beznea };
289ef396df9SClaudiu Beznea 
290ef396df9SClaudiu Beznea struct clk_hw * __init
at91_clk_sama7g5_register_utmi(struct regmap * regmap_pmc,const char * name,const char * parent_name,struct clk_hw * parent_hw)291ef396df9SClaudiu Beznea at91_clk_sama7g5_register_utmi(struct regmap *regmap_pmc, const char *name,
292*077782e3SClaudiu Beznea 			       const char *parent_name, struct clk_hw *parent_hw)
293ef396df9SClaudiu Beznea {
294ef396df9SClaudiu Beznea 	return at91_clk_register_utmi_internal(regmap_pmc, NULL, name,
295*077782e3SClaudiu Beznea 			parent_name, parent_hw, &sama7g5_utmi_ops, 0);
296ef396df9SClaudiu Beznea }
297