blob: 481146029b2ed5d59c34c7ae30b342a1b0ece0a9 [file] [log] [blame]
Boris BREZILLON0ad61252013-12-02 15:07:02 +01001/*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 */
10
11#include <linux/clk-provider.h>
12#include <linux/clkdev.h>
13#include <linux/clk/at91_pmc.h>
14#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/io.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/irqchip/chained_irq.h>
20#include <linux/irqdomain.h>
21#include <linux/of_irq.h>
Boris Brezillon863a81c2014-09-05 09:54:13 +020022#include <linux/mfd/syscon.h>
Boris BREZILLON0ad61252013-12-02 15:07:02 +010023
24#include <asm/proc-fns.h>
25
26#include "pmc.h"
27
28void __iomem *at91_pmc_base;
29EXPORT_SYMBOL_GPL(at91_pmc_base);
30
Alexandre Belloni29ee5062015-01-15 15:59:31 +010031void at91rm9200_idle(void)
32{
33 /*
34 * Disable the processor clock. The processor will be automatically
35 * re-enabled by an interrupt or by a reset.
36 */
37 at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
38}
39
Boris BREZILLON0ad61252013-12-02 15:07:02 +010040void at91sam9_idle(void)
41{
42 at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
43 cpu_do_idle();
44}
45
46int of_at91_get_clk_range(struct device_node *np, const char *propname,
47 struct clk_range *range)
48{
49 u32 min, max;
50 int ret;
51
52 ret = of_property_read_u32_index(np, propname, 0, &min);
53 if (ret)
54 return ret;
55
56 ret = of_property_read_u32_index(np, propname, 1, &max);
57 if (ret)
58 return ret;
59
60 if (range) {
61 range->min = min;
62 range->max = max;
63 }
64
65 return 0;
66}
67EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
68
69static void pmc_irq_mask(struct irq_data *d)
70{
71 struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
72
73 pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
74}
75
76static void pmc_irq_unmask(struct irq_data *d)
77{
78 struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
79
80 pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
81}
82
83static int pmc_irq_set_type(struct irq_data *d, unsigned type)
84{
85 if (type != IRQ_TYPE_LEVEL_HIGH) {
86 pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
87 return -EINVAL;
88 }
89
90 return 0;
91}
92
Boris BREZILLON947f5b12015-03-02 10:18:16 +010093static void pmc_irq_suspend(struct irq_data *d)
94{
95 struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
96
97 pmc->imr = pmc_read(pmc, AT91_PMC_IMR);
98 pmc_write(pmc, AT91_PMC_IDR, pmc->imr);
99}
100
101static void pmc_irq_resume(struct irq_data *d)
102{
103 struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
104
105 pmc_write(pmc, AT91_PMC_IER, pmc->imr);
106}
107
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100108static struct irq_chip pmc_irq = {
109 .name = "PMC",
110 .irq_disable = pmc_irq_mask,
111 .irq_mask = pmc_irq_mask,
112 .irq_unmask = pmc_irq_unmask,
113 .irq_set_type = pmc_irq_set_type,
Boris BREZILLON947f5b12015-03-02 10:18:16 +0100114 .irq_suspend = pmc_irq_suspend,
115 .irq_resume = pmc_irq_resume,
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100116};
117
118static struct lock_class_key pmc_lock_class;
119
120static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
121 irq_hw_number_t hw)
122{
123 struct at91_pmc *pmc = h->host_data;
124
125 irq_set_lockdep_class(virq, &pmc_lock_class);
126
127 irq_set_chip_and_handler(virq, &pmc_irq,
128 handle_level_irq);
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100129 irq_set_chip_data(virq, pmc);
130
131 return 0;
132}
133
134static int pmc_irq_domain_xlate(struct irq_domain *d,
135 struct device_node *ctrlr,
136 const u32 *intspec, unsigned int intsize,
137 irq_hw_number_t *out_hwirq,
138 unsigned int *out_type)
139{
140 struct at91_pmc *pmc = d->host_data;
141 const struct at91_pmc_caps *caps = pmc->caps;
142
143 if (WARN_ON(intsize < 1))
144 return -EINVAL;
145
146 *out_hwirq = intspec[0];
147
148 if (!(caps->available_irqs & (1 << *out_hwirq)))
149 return -EINVAL;
150
151 *out_type = IRQ_TYPE_LEVEL_HIGH;
152
153 return 0;
154}
155
Krzysztof Kozlowskid6c27672015-04-27 21:52:38 +0900156static const struct irq_domain_ops pmc_irq_ops = {
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100157 .map = pmc_irq_map,
158 .xlate = pmc_irq_domain_xlate,
159};
160
161static irqreturn_t pmc_irq_handler(int irq, void *data)
162{
163 struct at91_pmc *pmc = (struct at91_pmc *)data;
164 unsigned long sr;
165 int n;
166
167 sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
168 if (!sr)
169 return IRQ_NONE;
170
171 for_each_set_bit(n, &sr, BITS_PER_LONG)
172 generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
173
174 return IRQ_HANDLED;
175}
176
177static const struct at91_pmc_caps at91rm9200_caps = {
178 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
179 AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
180 AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
181 AT91_PMC_PCK3RDY,
182};
183
184static const struct at91_pmc_caps at91sam9260_caps = {
185 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
186 AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
187 AT91_PMC_PCK1RDY,
188};
189
190static const struct at91_pmc_caps at91sam9g45_caps = {
191 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
192 AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
193 AT91_PMC_PCK1RDY,
194};
195
196static const struct at91_pmc_caps at91sam9n12_caps = {
197 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
198 AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
199 AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
200 AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
201};
202
203static const struct at91_pmc_caps at91sam9x5_caps = {
204 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
205 AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
206 AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
207 AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
208};
209
Nicolas Ferrea5752e52015-06-18 14:43:29 +0200210static const struct at91_pmc_caps sama5d2_caps = {
211 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
212 AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
213 AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
214 AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
215 AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
216};
217
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100218static const struct at91_pmc_caps sama5d3_caps = {
219 .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
220 AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
221 AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
222 AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
223 AT91_PMC_CFDEV,
224};
225
226static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
Boris Brezillon863a81c2014-09-05 09:54:13 +0200227 struct regmap *regmap,
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100228 void __iomem *regbase, int virq,
229 const struct at91_pmc_caps *caps)
230{
231 struct at91_pmc *pmc;
232
233 if (!regbase || !virq || !caps)
234 return NULL;
235
236 at91_pmc_base = regbase;
237
238 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
239 if (!pmc)
240 return NULL;
241
242 spin_lock_init(&pmc->lock);
Boris Brezillon863a81c2014-09-05 09:54:13 +0200243 pmc->regmap = regmap;
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100244 pmc->virq = virq;
245 pmc->caps = caps;
246
247 pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
248
249 if (!pmc->irqdomain)
250 goto out_free_pmc;
251
252 pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
Boris BREZILLON947f5b12015-03-02 10:18:16 +0100253 if (request_irq(pmc->virq, pmc_irq_handler,
254 IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100255 goto out_remove_irqdomain;
256
257 return pmc;
258
259out_remove_irqdomain:
260 irq_domain_remove(pmc->irqdomain);
261out_free_pmc:
262 kfree(pmc);
263
264 return NULL;
265}
266
Boris BREZILLON7736c712013-12-16 22:25:27 +0100267static const struct of_device_id pmc_clk_ids[] __initconst = {
Boris BREZILLON80eded62014-05-07 18:02:15 +0200268 /* Slow oscillator */
269 {
270 .compatible = "atmel,at91sam9260-clk-slow",
271 .data = of_at91sam9260_clk_slow_setup,
272 },
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200273 /* Main clock */
274 {
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200275 .compatible = "atmel,at91rm9200-clk-main-osc",
276 .data = of_at91rm9200_clk_main_osc_setup,
277 },
278 {
279 .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
280 .data = of_at91sam9x5_clk_main_rc_osc_setup,
281 },
282 {
Boris BREZILLON38d34c32013-10-11 10:44:49 +0200283 .compatible = "atmel,at91rm9200-clk-main",
284 .data = of_at91rm9200_clk_main_setup,
285 },
Boris BREZILLON27cb1c22014-05-07 18:00:08 +0200286 {
287 .compatible = "atmel,at91sam9x5-clk-main",
288 .data = of_at91sam9x5_clk_main_setup,
289 },
Boris BREZILLON1a748d22013-10-11 10:48:26 +0200290 /* PLL clocks */
291 {
292 .compatible = "atmel,at91rm9200-clk-pll",
293 .data = of_at91rm9200_clk_pll_setup,
294 },
295 {
296 .compatible = "atmel,at91sam9g45-clk-pll",
297 .data = of_at91sam9g45_clk_pll_setup,
298 },
299 {
300 .compatible = "atmel,at91sam9g20-clk-pllb",
301 .data = of_at91sam9g20_clk_pllb_setup,
302 },
303 {
304 .compatible = "atmel,sama5d3-clk-pll",
305 .data = of_sama5d3_clk_pll_setup,
306 },
307 {
308 .compatible = "atmel,at91sam9x5-clk-plldiv",
309 .data = of_at91sam9x5_clk_plldiv_setup,
310 },
Boris BREZILLONe442d232013-10-11 10:51:23 +0200311 /* Master clock */
312 {
313 .compatible = "atmel,at91rm9200-clk-master",
314 .data = of_at91rm9200_clk_master_setup,
315 },
316 {
317 .compatible = "atmel,at91sam9x5-clk-master",
318 .data = of_at91sam9x5_clk_master_setup,
319 },
Boris BREZILLON5fba62e2013-10-11 11:41:41 +0200320 /* System clocks */
321 {
322 .compatible = "atmel,at91rm9200-clk-system",
323 .data = of_at91rm9200_clk_sys_setup,
324 },
Boris BREZILLON61140672013-10-11 11:44:36 +0200325 /* Peripheral clocks */
326 {
327 .compatible = "atmel,at91rm9200-clk-peripheral",
328 .data = of_at91rm9200_clk_periph_setup,
329 },
330 {
331 .compatible = "atmel,at91sam9x5-clk-peripheral",
332 .data = of_at91sam9x5_clk_periph_setup,
333 },
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200334 /* Programmable clocks */
Boris BREZILLON1f22f8b2013-10-11 11:57:04 +0200335 {
336 .compatible = "atmel,at91rm9200-clk-programmable",
337 .data = of_at91rm9200_clk_prog_setup,
338 },
339 {
340 .compatible = "atmel,at91sam9g45-clk-programmable",
341 .data = of_at91sam9g45_clk_prog_setup,
342 },
343 {
344 .compatible = "atmel,at91sam9x5-clk-programmable",
345 .data = of_at91sam9x5_clk_prog_setup,
346 },
Boris BREZILLONf090fb32013-10-11 12:22:06 +0200347 /* UTMI clock */
348#if defined(CONFIG_HAVE_AT91_UTMI)
349 {
350 .compatible = "atmel,at91sam9x5-clk-utmi",
351 .data = of_at91sam9x5_clk_utmi_setup,
352 },
353#endif
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200354 /* USB clock */
355#if defined(CONFIG_HAVE_AT91_USB_CLK)
356 {
357 .compatible = "atmel,at91rm9200-clk-usb",
358 .data = of_at91rm9200_clk_usb_setup,
359 },
360 {
361 .compatible = "atmel,at91sam9x5-clk-usb",
362 .data = of_at91sam9x5_clk_usb_setup,
363 },
364 {
365 .compatible = "atmel,at91sam9n12-clk-usb",
366 .data = of_at91sam9n12_clk_usb_setup,
367 },
368#endif
Boris BREZILLONa9c06882013-10-11 13:27:06 +0200369 /* SMD clock */
370#if defined(CONFIG_HAVE_AT91_SMD)
371 {
372 .compatible = "atmel,at91sam9x5-clk-smd",
373 .data = of_at91sam9x5_clk_smd_setup,
374 },
375#endif
Alexandre Bellonibcc5fd42014-09-15 18:15:53 +0200376#if defined(CONFIG_HAVE_AT91_H32MX)
377 {
378 .compatible = "atmel,sama5d4-clk-h32mx",
379 .data = of_sama5d4_clk_h32mx_setup,
380 },
381#endif
Nicolas Ferredf70aee2015-07-31 11:43:12 +0200382#if defined(CONFIG_HAVE_AT91_GENERATED_CLK)
383 {
384 .compatible = "atmel,sama5d2-clk-generated",
385 .data = of_sama5d2_clk_generated_setup,
386 },
387#endif
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100388 { /*sentinel*/ }
389};
390
391static void __init of_at91_pmc_setup(struct device_node *np,
392 const struct at91_pmc_caps *caps)
393{
394 struct at91_pmc *pmc;
395 struct device_node *childnp;
396 void (*clk_setup)(struct device_node *, struct at91_pmc *);
397 const struct of_device_id *clk_id;
398 void __iomem *regbase = of_iomap(np, 0);
Boris Brezillon863a81c2014-09-05 09:54:13 +0200399 struct regmap *regmap;
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100400 int virq;
401
Boris Brezillon863a81c2014-09-05 09:54:13 +0200402 regmap = syscon_node_to_regmap(np);
403 if (IS_ERR(regmap))
404 panic("Could not retrieve syscon regmap");
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100405
406 virq = irq_of_parse_and_map(np, 0);
407 if (!virq)
408 return;
409
Boris Brezillon863a81c2014-09-05 09:54:13 +0200410 pmc = at91_pmc_init(np, regmap, regbase, virq, caps);
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100411 if (!pmc)
412 return;
413 for_each_child_of_node(np, childnp) {
414 clk_id = of_match_node(pmc_clk_ids, childnp);
415 if (!clk_id)
416 continue;
417 clk_setup = clk_id->data;
418 clk_setup(childnp, pmc);
419 }
420}
421
422static void __init of_at91rm9200_pmc_setup(struct device_node *np)
423{
424 of_at91_pmc_setup(np, &at91rm9200_caps);
425}
426CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
427 of_at91rm9200_pmc_setup);
428
429static void __init of_at91sam9260_pmc_setup(struct device_node *np)
430{
431 of_at91_pmc_setup(np, &at91sam9260_caps);
432}
433CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
434 of_at91sam9260_pmc_setup);
435
436static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
437{
438 of_at91_pmc_setup(np, &at91sam9g45_caps);
439}
440CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
441 of_at91sam9g45_pmc_setup);
442
443static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
444{
445 of_at91_pmc_setup(np, &at91sam9n12_caps);
446}
447CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
448 of_at91sam9n12_pmc_setup);
449
450static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
451{
452 of_at91_pmc_setup(np, &at91sam9x5_caps);
453}
454CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
455 of_at91sam9x5_pmc_setup);
456
Nicolas Ferrea5752e52015-06-18 14:43:29 +0200457static void __init of_sama5d2_pmc_setup(struct device_node *np)
458{
459 of_at91_pmc_setup(np, &sama5d2_caps);
460}
461CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
462 of_sama5d2_pmc_setup);
463
Boris BREZILLON0ad61252013-12-02 15:07:02 +0100464static void __init of_sama5d3_pmc_setup(struct device_node *np)
465{
466 of_at91_pmc_setup(np, &sama5d3_caps);
467}
468CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
469 of_sama5d3_pmc_setup);