2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
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.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/of_address.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>
23 #include <asm/proc-fns.h>
27 void __iomem
*at91_pmc_base
;
28 EXPORT_SYMBOL_GPL(at91_pmc_base
);
30 void at91sam9_idle(void)
32 at91_pmc_write(AT91_PMC_SCDR
, AT91_PMC_PCK
);
36 int of_at91_get_clk_range(struct device_node
*np
, const char *propname
,
37 struct clk_range
*range
)
42 ret
= of_property_read_u32_index(np
, propname
, 0, &min
);
46 ret
= of_property_read_u32_index(np
, propname
, 1, &max
);
57 EXPORT_SYMBOL_GPL(of_at91_get_clk_range
);
59 static void pmc_irq_mask(struct irq_data
*d
)
61 struct at91_pmc
*pmc
= irq_data_get_irq_chip_data(d
);
63 pmc_write(pmc
, AT91_PMC_IDR
, 1 << d
->hwirq
);
66 static void pmc_irq_unmask(struct irq_data
*d
)
68 struct at91_pmc
*pmc
= irq_data_get_irq_chip_data(d
);
70 pmc_write(pmc
, AT91_PMC_IER
, 1 << d
->hwirq
);
73 static int pmc_irq_set_type(struct irq_data
*d
, unsigned type
)
75 if (type
!= IRQ_TYPE_LEVEL_HIGH
) {
76 pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
83 static struct irq_chip pmc_irq
= {
85 .irq_disable
= pmc_irq_mask
,
86 .irq_mask
= pmc_irq_mask
,
87 .irq_unmask
= pmc_irq_unmask
,
88 .irq_set_type
= pmc_irq_set_type
,
91 static struct lock_class_key pmc_lock_class
;
93 static int pmc_irq_map(struct irq_domain
*h
, unsigned int virq
,
96 struct at91_pmc
*pmc
= h
->host_data
;
98 irq_set_lockdep_class(virq
, &pmc_lock_class
);
100 irq_set_chip_and_handler(virq
, &pmc_irq
,
102 set_irq_flags(virq
, IRQF_VALID
);
103 irq_set_chip_data(virq
, pmc
);
108 static int pmc_irq_domain_xlate(struct irq_domain
*d
,
109 struct device_node
*ctrlr
,
110 const u32
*intspec
, unsigned int intsize
,
111 irq_hw_number_t
*out_hwirq
,
112 unsigned int *out_type
)
114 struct at91_pmc
*pmc
= d
->host_data
;
115 const struct at91_pmc_caps
*caps
= pmc
->caps
;
117 if (WARN_ON(intsize
< 1))
120 *out_hwirq
= intspec
[0];
122 if (!(caps
->available_irqs
& (1 << *out_hwirq
)))
125 *out_type
= IRQ_TYPE_LEVEL_HIGH
;
130 static struct irq_domain_ops pmc_irq_ops
= {
132 .xlate
= pmc_irq_domain_xlate
,
135 static irqreturn_t
pmc_irq_handler(int irq
, void *data
)
137 struct at91_pmc
*pmc
= (struct at91_pmc
*)data
;
141 sr
= pmc_read(pmc
, AT91_PMC_SR
) & pmc_read(pmc
, AT91_PMC_IMR
);
145 for_each_set_bit(n
, &sr
, BITS_PER_LONG
)
146 generic_handle_irq(irq_find_mapping(pmc
->irqdomain
, n
));
151 static const struct at91_pmc_caps at91rm9200_caps
= {
152 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_LOCKB
|
153 AT91_PMC_MCKRDY
| AT91_PMC_PCK0RDY
|
154 AT91_PMC_PCK1RDY
| AT91_PMC_PCK2RDY
|
158 static const struct at91_pmc_caps at91sam9260_caps
= {
159 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_LOCKB
|
160 AT91_PMC_MCKRDY
| AT91_PMC_PCK0RDY
|
164 static const struct at91_pmc_caps at91sam9g45_caps
= {
165 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_MCKRDY
|
166 AT91_PMC_LOCKU
| AT91_PMC_PCK0RDY
|
170 static const struct at91_pmc_caps at91sam9n12_caps
= {
171 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_LOCKB
|
172 AT91_PMC_MCKRDY
| AT91_PMC_PCK0RDY
|
173 AT91_PMC_PCK1RDY
| AT91_PMC_MOSCSELS
|
174 AT91_PMC_MOSCRCS
| AT91_PMC_CFDEV
,
177 static const struct at91_pmc_caps at91sam9x5_caps
= {
178 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_MCKRDY
|
179 AT91_PMC_LOCKU
| AT91_PMC_PCK0RDY
|
180 AT91_PMC_PCK1RDY
| AT91_PMC_MOSCSELS
|
181 AT91_PMC_MOSCRCS
| AT91_PMC_CFDEV
,
184 static const struct at91_pmc_caps sama5d3_caps
= {
185 .available_irqs
= AT91_PMC_MOSCS
| AT91_PMC_LOCKA
| AT91_PMC_MCKRDY
|
186 AT91_PMC_LOCKU
| AT91_PMC_PCK0RDY
|
187 AT91_PMC_PCK1RDY
| AT91_PMC_PCK2RDY
|
188 AT91_PMC_MOSCSELS
| AT91_PMC_MOSCRCS
|
192 static struct at91_pmc
*__init
at91_pmc_init(struct device_node
*np
,
193 void __iomem
*regbase
, int virq
,
194 const struct at91_pmc_caps
*caps
)
196 struct at91_pmc
*pmc
;
198 if (!regbase
|| !virq
|| !caps
)
201 at91_pmc_base
= regbase
;
203 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
207 spin_lock_init(&pmc
->lock
);
208 pmc
->regbase
= regbase
;
212 pmc
->irqdomain
= irq_domain_add_linear(np
, 32, &pmc_irq_ops
, pmc
);
217 pmc_write(pmc
, AT91_PMC_IDR
, 0xffffffff);
218 if (request_irq(pmc
->virq
, pmc_irq_handler
, IRQF_SHARED
, "pmc", pmc
))
219 goto out_remove_irqdomain
;
223 out_remove_irqdomain
:
224 irq_domain_remove(pmc
->irqdomain
);
231 static const struct of_device_id pmc_clk_ids
[] __initconst
= {
234 .compatible
= "atmel,at91rm9200-clk-main",
235 .data
= of_at91rm9200_clk_main_setup
,
239 .compatible
= "atmel,at91rm9200-clk-pll",
240 .data
= of_at91rm9200_clk_pll_setup
,
243 .compatible
= "atmel,at91sam9g45-clk-pll",
244 .data
= of_at91sam9g45_clk_pll_setup
,
247 .compatible
= "atmel,at91sam9g20-clk-pllb",
248 .data
= of_at91sam9g20_clk_pllb_setup
,
251 .compatible
= "atmel,sama5d3-clk-pll",
252 .data
= of_sama5d3_clk_pll_setup
,
255 .compatible
= "atmel,at91sam9x5-clk-plldiv",
256 .data
= of_at91sam9x5_clk_plldiv_setup
,
260 .compatible
= "atmel,at91rm9200-clk-master",
261 .data
= of_at91rm9200_clk_master_setup
,
264 .compatible
= "atmel,at91sam9x5-clk-master",
265 .data
= of_at91sam9x5_clk_master_setup
,
269 .compatible
= "atmel,at91rm9200-clk-system",
270 .data
= of_at91rm9200_clk_sys_setup
,
272 /* Peripheral clocks */
274 .compatible
= "atmel,at91rm9200-clk-peripheral",
275 .data
= of_at91rm9200_clk_periph_setup
,
278 .compatible
= "atmel,at91sam9x5-clk-peripheral",
279 .data
= of_at91sam9x5_clk_periph_setup
,
281 /* Programmable clocks */
283 .compatible
= "atmel,at91rm9200-clk-programmable",
284 .data
= of_at91rm9200_clk_prog_setup
,
287 .compatible
= "atmel,at91sam9g45-clk-programmable",
288 .data
= of_at91sam9g45_clk_prog_setup
,
291 .compatible
= "atmel,at91sam9x5-clk-programmable",
292 .data
= of_at91sam9x5_clk_prog_setup
,
295 #if defined(CONFIG_HAVE_AT91_UTMI)
297 .compatible
= "atmel,at91sam9x5-clk-utmi",
298 .data
= of_at91sam9x5_clk_utmi_setup
,
302 #if defined(CONFIG_HAVE_AT91_USB_CLK)
304 .compatible
= "atmel,at91rm9200-clk-usb",
305 .data
= of_at91rm9200_clk_usb_setup
,
308 .compatible
= "atmel,at91sam9x5-clk-usb",
309 .data
= of_at91sam9x5_clk_usb_setup
,
312 .compatible
= "atmel,at91sam9n12-clk-usb",
313 .data
= of_at91sam9n12_clk_usb_setup
,
317 #if defined(CONFIG_HAVE_AT91_SMD)
319 .compatible
= "atmel,at91sam9x5-clk-smd",
320 .data
= of_at91sam9x5_clk_smd_setup
,
326 static void __init
of_at91_pmc_setup(struct device_node
*np
,
327 const struct at91_pmc_caps
*caps
)
329 struct at91_pmc
*pmc
;
330 struct device_node
*childnp
;
331 void (*clk_setup
)(struct device_node
*, struct at91_pmc
*);
332 const struct of_device_id
*clk_id
;
333 void __iomem
*regbase
= of_iomap(np
, 0);
339 virq
= irq_of_parse_and_map(np
, 0);
343 pmc
= at91_pmc_init(np
, regbase
, virq
, caps
);
346 for_each_child_of_node(np
, childnp
) {
347 clk_id
= of_match_node(pmc_clk_ids
, childnp
);
350 clk_setup
= clk_id
->data
;
351 clk_setup(childnp
, pmc
);
355 static void __init
of_at91rm9200_pmc_setup(struct device_node
*np
)
357 of_at91_pmc_setup(np
, &at91rm9200_caps
);
359 CLK_OF_DECLARE(at91rm9200_clk_pmc
, "atmel,at91rm9200-pmc",
360 of_at91rm9200_pmc_setup
);
362 static void __init
of_at91sam9260_pmc_setup(struct device_node
*np
)
364 of_at91_pmc_setup(np
, &at91sam9260_caps
);
366 CLK_OF_DECLARE(at91sam9260_clk_pmc
, "atmel,at91sam9260-pmc",
367 of_at91sam9260_pmc_setup
);
369 static void __init
of_at91sam9g45_pmc_setup(struct device_node
*np
)
371 of_at91_pmc_setup(np
, &at91sam9g45_caps
);
373 CLK_OF_DECLARE(at91sam9g45_clk_pmc
, "atmel,at91sam9g45-pmc",
374 of_at91sam9g45_pmc_setup
);
376 static void __init
of_at91sam9n12_pmc_setup(struct device_node
*np
)
378 of_at91_pmc_setup(np
, &at91sam9n12_caps
);
380 CLK_OF_DECLARE(at91sam9n12_clk_pmc
, "atmel,at91sam9n12-pmc",
381 of_at91sam9n12_pmc_setup
);
383 static void __init
of_at91sam9x5_pmc_setup(struct device_node
*np
)
385 of_at91_pmc_setup(np
, &at91sam9x5_caps
);
387 CLK_OF_DECLARE(at91sam9x5_clk_pmc
, "atmel,at91sam9x5-pmc",
388 of_at91sam9x5_pmc_setup
);
390 static void __init
of_sama5d3_pmc_setup(struct device_node
*np
)
392 of_at91_pmc_setup(np
, &sama5d3_caps
);
394 CLK_OF_DECLARE(sama5d3_clk_pmc
, "atmel,sama5d3-pmc",
395 of_sama5d3_pmc_setup
);