pmc.c 10 KB

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