pmc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  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. #include <linux/clk-provider.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/clk/at91_pmc.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/irqchip/chained_irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/of_irq.h>
  21. #include <asm/proc-fns.h>
  22. #include "pmc.h"
  23. void __iomem *at91_pmc_base;
  24. EXPORT_SYMBOL_GPL(at91_pmc_base);
  25. void at91sam9_idle(void)
  26. {
  27. at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  28. cpu_do_idle();
  29. }
  30. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  31. struct clk_range *range)
  32. {
  33. u32 min, max;
  34. int ret;
  35. ret = of_property_read_u32_index(np, propname, 0, &min);
  36. if (ret)
  37. return ret;
  38. ret = of_property_read_u32_index(np, propname, 1, &max);
  39. if (ret)
  40. return ret;
  41. if (range) {
  42. range->min = min;
  43. range->max = max;
  44. }
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  48. static void pmc_irq_mask(struct irq_data *d)
  49. {
  50. struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  51. pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
  52. }
  53. static void pmc_irq_unmask(struct irq_data *d)
  54. {
  55. struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  56. pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
  57. }
  58. static int pmc_irq_set_type(struct irq_data *d, unsigned type)
  59. {
  60. if (type != IRQ_TYPE_LEVEL_HIGH) {
  61. pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. static struct irq_chip pmc_irq = {
  67. .name = "PMC",
  68. .irq_disable = pmc_irq_mask,
  69. .irq_mask = pmc_irq_mask,
  70. .irq_unmask = pmc_irq_unmask,
  71. .irq_set_type = pmc_irq_set_type,
  72. };
  73. static struct lock_class_key pmc_lock_class;
  74. static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
  75. irq_hw_number_t hw)
  76. {
  77. struct at91_pmc *pmc = h->host_data;
  78. irq_set_lockdep_class(virq, &pmc_lock_class);
  79. irq_set_chip_and_handler(virq, &pmc_irq,
  80. handle_level_irq);
  81. set_irq_flags(virq, IRQF_VALID);
  82. irq_set_chip_data(virq, pmc);
  83. return 0;
  84. }
  85. static int pmc_irq_domain_xlate(struct irq_domain *d,
  86. struct device_node *ctrlr,
  87. const u32 *intspec, unsigned int intsize,
  88. irq_hw_number_t *out_hwirq,
  89. unsigned int *out_type)
  90. {
  91. struct at91_pmc *pmc = d->host_data;
  92. const struct at91_pmc_caps *caps = pmc->caps;
  93. if (WARN_ON(intsize < 1))
  94. return -EINVAL;
  95. *out_hwirq = intspec[0];
  96. if (!(caps->available_irqs & (1 << *out_hwirq)))
  97. return -EINVAL;
  98. *out_type = IRQ_TYPE_LEVEL_HIGH;
  99. return 0;
  100. }
  101. static struct irq_domain_ops pmc_irq_ops = {
  102. .map = pmc_irq_map,
  103. .xlate = pmc_irq_domain_xlate,
  104. };
  105. static irqreturn_t pmc_irq_handler(int irq, void *data)
  106. {
  107. struct at91_pmc *pmc = (struct at91_pmc *)data;
  108. unsigned long sr;
  109. int n;
  110. sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  111. if (!sr)
  112. return IRQ_NONE;
  113. for_each_set_bit(n, &sr, BITS_PER_LONG)
  114. generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  115. return IRQ_HANDLED;
  116. }
  117. static const struct at91_pmc_caps at91rm9200_caps = {
  118. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  119. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  120. AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  121. AT91_PMC_PCK3RDY,
  122. };
  123. static const struct at91_pmc_caps at91sam9260_caps = {
  124. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  125. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  126. AT91_PMC_PCK1RDY,
  127. };
  128. static const struct at91_pmc_caps at91sam9g45_caps = {
  129. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  130. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  131. AT91_PMC_PCK1RDY,
  132. };
  133. static const struct at91_pmc_caps at91sam9n12_caps = {
  134. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  135. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  136. AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  137. AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  138. };
  139. static const struct at91_pmc_caps at91sam9x5_caps = {
  140. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  141. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  142. AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  143. AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  144. };
  145. static const struct at91_pmc_caps sama5d3_caps = {
  146. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  147. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  148. AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  149. AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  150. AT91_PMC_CFDEV,
  151. };
  152. static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  153. void __iomem *regbase, int virq,
  154. const struct at91_pmc_caps *caps)
  155. {
  156. struct at91_pmc *pmc;
  157. if (!regbase || !virq || !caps)
  158. return NULL;
  159. at91_pmc_base = regbase;
  160. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  161. if (!pmc)
  162. return NULL;
  163. spin_lock_init(&pmc->lock);
  164. pmc->regbase = regbase;
  165. pmc->virq = virq;
  166. pmc->caps = caps;
  167. pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  168. if (!pmc->irqdomain)
  169. goto out_free_pmc;
  170. pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  171. if (request_irq(pmc->virq, pmc_irq_handler, IRQF_SHARED, "pmc", pmc))
  172. goto out_remove_irqdomain;
  173. return pmc;
  174. out_remove_irqdomain:
  175. irq_domain_remove(pmc->irqdomain);
  176. out_free_pmc:
  177. kfree(pmc);
  178. return NULL;
  179. }
  180. static const struct of_device_id pmc_clk_ids[] __initconst = {
  181. /* Slow oscillator */
  182. {
  183. .compatible = "atmel,at91sam9260-clk-slow",
  184. .data = of_at91sam9260_clk_slow_setup,
  185. },
  186. /* Main clock */
  187. {
  188. .compatible = "atmel,at91rm9200-clk-main-osc",
  189. .data = of_at91rm9200_clk_main_osc_setup,
  190. },
  191. {
  192. .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  193. .data = of_at91sam9x5_clk_main_rc_osc_setup,
  194. },
  195. {
  196. .compatible = "atmel,at91rm9200-clk-main",
  197. .data = of_at91rm9200_clk_main_setup,
  198. },
  199. {
  200. .compatible = "atmel,at91sam9x5-clk-main",
  201. .data = of_at91sam9x5_clk_main_setup,
  202. },
  203. /* PLL clocks */
  204. {
  205. .compatible = "atmel,at91rm9200-clk-pll",
  206. .data = of_at91rm9200_clk_pll_setup,
  207. },
  208. {
  209. .compatible = "atmel,at91sam9g45-clk-pll",
  210. .data = of_at91sam9g45_clk_pll_setup,
  211. },
  212. {
  213. .compatible = "atmel,at91sam9g20-clk-pllb",
  214. .data = of_at91sam9g20_clk_pllb_setup,
  215. },
  216. {
  217. .compatible = "atmel,sama5d3-clk-pll",
  218. .data = of_sama5d3_clk_pll_setup,
  219. },
  220. {
  221. .compatible = "atmel,at91sam9x5-clk-plldiv",
  222. .data = of_at91sam9x5_clk_plldiv_setup,
  223. },
  224. /* Master clock */
  225. {
  226. .compatible = "atmel,at91rm9200-clk-master",
  227. .data = of_at91rm9200_clk_master_setup,
  228. },
  229. {
  230. .compatible = "atmel,at91sam9x5-clk-master",
  231. .data = of_at91sam9x5_clk_master_setup,
  232. },
  233. /* System clocks */
  234. {
  235. .compatible = "atmel,at91rm9200-clk-system",
  236. .data = of_at91rm9200_clk_sys_setup,
  237. },
  238. /* Peripheral clocks */
  239. {
  240. .compatible = "atmel,at91rm9200-clk-peripheral",
  241. .data = of_at91rm9200_clk_periph_setup,
  242. },
  243. {
  244. .compatible = "atmel,at91sam9x5-clk-peripheral",
  245. .data = of_at91sam9x5_clk_periph_setup,
  246. },
  247. /* Programmable clocks */
  248. {
  249. .compatible = "atmel,at91rm9200-clk-programmable",
  250. .data = of_at91rm9200_clk_prog_setup,
  251. },
  252. {
  253. .compatible = "atmel,at91sam9g45-clk-programmable",
  254. .data = of_at91sam9g45_clk_prog_setup,
  255. },
  256. {
  257. .compatible = "atmel,at91sam9x5-clk-programmable",
  258. .data = of_at91sam9x5_clk_prog_setup,
  259. },
  260. /* UTMI clock */
  261. #if defined(CONFIG_HAVE_AT91_UTMI)
  262. {
  263. .compatible = "atmel,at91sam9x5-clk-utmi",
  264. .data = of_at91sam9x5_clk_utmi_setup,
  265. },
  266. #endif
  267. /* USB clock */
  268. #if defined(CONFIG_HAVE_AT91_USB_CLK)
  269. {
  270. .compatible = "atmel,at91rm9200-clk-usb",
  271. .data = of_at91rm9200_clk_usb_setup,
  272. },
  273. {
  274. .compatible = "atmel,at91sam9x5-clk-usb",
  275. .data = of_at91sam9x5_clk_usb_setup,
  276. },
  277. {
  278. .compatible = "atmel,at91sam9n12-clk-usb",
  279. .data = of_at91sam9n12_clk_usb_setup,
  280. },
  281. #endif
  282. /* SMD clock */
  283. #if defined(CONFIG_HAVE_AT91_SMD)
  284. {
  285. .compatible = "atmel,at91sam9x5-clk-smd",
  286. .data = of_at91sam9x5_clk_smd_setup,
  287. },
  288. #endif
  289. { /*sentinel*/ }
  290. };
  291. static void __init of_at91_pmc_setup(struct device_node *np,
  292. const struct at91_pmc_caps *caps)
  293. {
  294. struct at91_pmc *pmc;
  295. struct device_node *childnp;
  296. void (*clk_setup)(struct device_node *, struct at91_pmc *);
  297. const struct of_device_id *clk_id;
  298. void __iomem *regbase = of_iomap(np, 0);
  299. int virq;
  300. if (!regbase)
  301. return;
  302. virq = irq_of_parse_and_map(np, 0);
  303. if (!virq)
  304. return;
  305. pmc = at91_pmc_init(np, regbase, virq, caps);
  306. if (!pmc)
  307. return;
  308. for_each_child_of_node(np, childnp) {
  309. clk_id = of_match_node(pmc_clk_ids, childnp);
  310. if (!clk_id)
  311. continue;
  312. clk_setup = clk_id->data;
  313. clk_setup(childnp, pmc);
  314. }
  315. }
  316. static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  317. {
  318. of_at91_pmc_setup(np, &at91rm9200_caps);
  319. }
  320. CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  321. of_at91rm9200_pmc_setup);
  322. static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  323. {
  324. of_at91_pmc_setup(np, &at91sam9260_caps);
  325. }
  326. CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  327. of_at91sam9260_pmc_setup);
  328. static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  329. {
  330. of_at91_pmc_setup(np, &at91sam9g45_caps);
  331. }
  332. CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  333. of_at91sam9g45_pmc_setup);
  334. static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  335. {
  336. of_at91_pmc_setup(np, &at91sam9n12_caps);
  337. }
  338. CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  339. of_at91sam9n12_pmc_setup);
  340. static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  341. {
  342. of_at91_pmc_setup(np, &at91sam9x5_caps);
  343. }
  344. CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  345. of_at91sam9x5_pmc_setup);
  346. static void __init of_sama5d3_pmc_setup(struct device_node *np)
  347. {
  348. of_at91_pmc_setup(np, &sama5d3_caps);
  349. }
  350. CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  351. of_sama5d3_pmc_setup);