pmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. set_irq_flags(virq, IRQF_VALID);
  103. irq_set_chip_data(virq, pmc);
  104. return 0;
  105. }
  106. static int pmc_irq_domain_xlate(struct irq_domain *d,
  107. struct device_node *ctrlr,
  108. const u32 *intspec, unsigned int intsize,
  109. irq_hw_number_t *out_hwirq,
  110. unsigned int *out_type)
  111. {
  112. struct at91_pmc *pmc = d->host_data;
  113. const struct at91_pmc_caps *caps = pmc->caps;
  114. if (WARN_ON(intsize < 1))
  115. return -EINVAL;
  116. *out_hwirq = intspec[0];
  117. if (!(caps->available_irqs & (1 << *out_hwirq)))
  118. return -EINVAL;
  119. *out_type = IRQ_TYPE_LEVEL_HIGH;
  120. return 0;
  121. }
  122. static const struct irq_domain_ops pmc_irq_ops = {
  123. .map = pmc_irq_map,
  124. .xlate = pmc_irq_domain_xlate,
  125. };
  126. static irqreturn_t pmc_irq_handler(int irq, void *data)
  127. {
  128. struct at91_pmc *pmc = (struct at91_pmc *)data;
  129. unsigned long sr;
  130. int n;
  131. sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  132. if (!sr)
  133. return IRQ_NONE;
  134. for_each_set_bit(n, &sr, BITS_PER_LONG)
  135. generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  136. return IRQ_HANDLED;
  137. }
  138. static const struct at91_pmc_caps at91rm9200_caps = {
  139. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  140. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  141. AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  142. AT91_PMC_PCK3RDY,
  143. };
  144. static const struct at91_pmc_caps at91sam9260_caps = {
  145. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  146. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  147. AT91_PMC_PCK1RDY,
  148. };
  149. static const struct at91_pmc_caps at91sam9g45_caps = {
  150. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  151. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  152. AT91_PMC_PCK1RDY,
  153. };
  154. static const struct at91_pmc_caps at91sam9n12_caps = {
  155. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  156. AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  157. AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  158. AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  159. };
  160. static const struct at91_pmc_caps at91sam9x5_caps = {
  161. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  162. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  163. AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  164. AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  165. };
  166. static const struct at91_pmc_caps sama5d3_caps = {
  167. .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  168. AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  169. AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  170. AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  171. AT91_PMC_CFDEV,
  172. };
  173. static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  174. void __iomem *regbase, int virq,
  175. const struct at91_pmc_caps *caps)
  176. {
  177. struct at91_pmc *pmc;
  178. if (!regbase || !virq || !caps)
  179. return NULL;
  180. at91_pmc_base = regbase;
  181. pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  182. if (!pmc)
  183. return NULL;
  184. spin_lock_init(&pmc->lock);
  185. pmc->regbase = regbase;
  186. pmc->virq = virq;
  187. pmc->caps = caps;
  188. pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  189. if (!pmc->irqdomain)
  190. goto out_free_pmc;
  191. pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  192. if (request_irq(pmc->virq, pmc_irq_handler,
  193. IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
  194. goto out_remove_irqdomain;
  195. return pmc;
  196. out_remove_irqdomain:
  197. irq_domain_remove(pmc->irqdomain);
  198. out_free_pmc:
  199. kfree(pmc);
  200. return NULL;
  201. }
  202. static const struct of_device_id pmc_clk_ids[] __initconst = {
  203. /* Slow oscillator */
  204. {
  205. .compatible = "atmel,at91sam9260-clk-slow",
  206. .data = of_at91sam9260_clk_slow_setup,
  207. },
  208. /* Main clock */
  209. {
  210. .compatible = "atmel,at91rm9200-clk-main-osc",
  211. .data = of_at91rm9200_clk_main_osc_setup,
  212. },
  213. {
  214. .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  215. .data = of_at91sam9x5_clk_main_rc_osc_setup,
  216. },
  217. {
  218. .compatible = "atmel,at91rm9200-clk-main",
  219. .data = of_at91rm9200_clk_main_setup,
  220. },
  221. {
  222. .compatible = "atmel,at91sam9x5-clk-main",
  223. .data = of_at91sam9x5_clk_main_setup,
  224. },
  225. /* PLL clocks */
  226. {
  227. .compatible = "atmel,at91rm9200-clk-pll",
  228. .data = of_at91rm9200_clk_pll_setup,
  229. },
  230. {
  231. .compatible = "atmel,at91sam9g45-clk-pll",
  232. .data = of_at91sam9g45_clk_pll_setup,
  233. },
  234. {
  235. .compatible = "atmel,at91sam9g20-clk-pllb",
  236. .data = of_at91sam9g20_clk_pllb_setup,
  237. },
  238. {
  239. .compatible = "atmel,sama5d3-clk-pll",
  240. .data = of_sama5d3_clk_pll_setup,
  241. },
  242. {
  243. .compatible = "atmel,at91sam9x5-clk-plldiv",
  244. .data = of_at91sam9x5_clk_plldiv_setup,
  245. },
  246. /* Master clock */
  247. {
  248. .compatible = "atmel,at91rm9200-clk-master",
  249. .data = of_at91rm9200_clk_master_setup,
  250. },
  251. {
  252. .compatible = "atmel,at91sam9x5-clk-master",
  253. .data = of_at91sam9x5_clk_master_setup,
  254. },
  255. /* System clocks */
  256. {
  257. .compatible = "atmel,at91rm9200-clk-system",
  258. .data = of_at91rm9200_clk_sys_setup,
  259. },
  260. /* Peripheral clocks */
  261. {
  262. .compatible = "atmel,at91rm9200-clk-peripheral",
  263. .data = of_at91rm9200_clk_periph_setup,
  264. },
  265. {
  266. .compatible = "atmel,at91sam9x5-clk-peripheral",
  267. .data = of_at91sam9x5_clk_periph_setup,
  268. },
  269. /* Programmable clocks */
  270. {
  271. .compatible = "atmel,at91rm9200-clk-programmable",
  272. .data = of_at91rm9200_clk_prog_setup,
  273. },
  274. {
  275. .compatible = "atmel,at91sam9g45-clk-programmable",
  276. .data = of_at91sam9g45_clk_prog_setup,
  277. },
  278. {
  279. .compatible = "atmel,at91sam9x5-clk-programmable",
  280. .data = of_at91sam9x5_clk_prog_setup,
  281. },
  282. /* UTMI clock */
  283. #if defined(CONFIG_HAVE_AT91_UTMI)
  284. {
  285. .compatible = "atmel,at91sam9x5-clk-utmi",
  286. .data = of_at91sam9x5_clk_utmi_setup,
  287. },
  288. #endif
  289. /* USB clock */
  290. #if defined(CONFIG_HAVE_AT91_USB_CLK)
  291. {
  292. .compatible = "atmel,at91rm9200-clk-usb",
  293. .data = of_at91rm9200_clk_usb_setup,
  294. },
  295. {
  296. .compatible = "atmel,at91sam9x5-clk-usb",
  297. .data = of_at91sam9x5_clk_usb_setup,
  298. },
  299. {
  300. .compatible = "atmel,at91sam9n12-clk-usb",
  301. .data = of_at91sam9n12_clk_usb_setup,
  302. },
  303. #endif
  304. /* SMD clock */
  305. #if defined(CONFIG_HAVE_AT91_SMD)
  306. {
  307. .compatible = "atmel,at91sam9x5-clk-smd",
  308. .data = of_at91sam9x5_clk_smd_setup,
  309. },
  310. #endif
  311. #if defined(CONFIG_HAVE_AT91_H32MX)
  312. {
  313. .compatible = "atmel,sama5d4-clk-h32mx",
  314. .data = of_sama5d4_clk_h32mx_setup,
  315. },
  316. #endif
  317. { /*sentinel*/ }
  318. };
  319. static void __init of_at91_pmc_setup(struct device_node *np,
  320. const struct at91_pmc_caps *caps)
  321. {
  322. struct at91_pmc *pmc;
  323. struct device_node *childnp;
  324. void (*clk_setup)(struct device_node *, struct at91_pmc *);
  325. const struct of_device_id *clk_id;
  326. void __iomem *regbase = of_iomap(np, 0);
  327. int virq;
  328. if (!regbase)
  329. return;
  330. virq = irq_of_parse_and_map(np, 0);
  331. if (!virq)
  332. return;
  333. pmc = at91_pmc_init(np, regbase, virq, caps);
  334. if (!pmc)
  335. return;
  336. for_each_child_of_node(np, childnp) {
  337. clk_id = of_match_node(pmc_clk_ids, childnp);
  338. if (!clk_id)
  339. continue;
  340. clk_setup = clk_id->data;
  341. clk_setup(childnp, pmc);
  342. }
  343. }
  344. static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  345. {
  346. of_at91_pmc_setup(np, &at91rm9200_caps);
  347. }
  348. CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  349. of_at91rm9200_pmc_setup);
  350. static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  351. {
  352. of_at91_pmc_setup(np, &at91sam9260_caps);
  353. }
  354. CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  355. of_at91sam9260_pmc_setup);
  356. static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  357. {
  358. of_at91_pmc_setup(np, &at91sam9g45_caps);
  359. }
  360. CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  361. of_at91sam9g45_pmc_setup);
  362. static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  363. {
  364. of_at91_pmc_setup(np, &at91sam9n12_caps);
  365. }
  366. CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  367. of_at91sam9n12_pmc_setup);
  368. static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  369. {
  370. of_at91_pmc_setup(np, &at91sam9x5_caps);
  371. }
  372. CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  373. of_at91sam9x5_pmc_setup);
  374. static void __init of_sama5d3_pmc_setup(struct device_node *np)
  375. {
  376. of_at91_pmc_setup(np, &sama5d3_caps);
  377. }
  378. CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  379. of_sama5d3_pmc_setup);