vic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * linux/arch/arm/common/vic.c
  3. *
  4. * Copyright (C) 1999 - 2003 ARM Limited
  5. * Copyright (C) 2000 Deep Blue Solutions Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/export.h>
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/io.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/syscore_ops.h>
  30. #include <linux/device.h>
  31. #include <linux/amba/bus.h>
  32. #include <asm/exception.h>
  33. #include <asm/mach/irq.h>
  34. #include <asm/hardware/vic.h>
  35. /**
  36. * struct vic_device - VIC PM device
  37. * @irq: The IRQ number for the base of the VIC.
  38. * @base: The register base for the VIC.
  39. * @resume_sources: A bitmask of interrupts for resume.
  40. * @resume_irqs: The IRQs enabled for resume.
  41. * @int_select: Save for VIC_INT_SELECT.
  42. * @int_enable: Save for VIC_INT_ENABLE.
  43. * @soft_int: Save for VIC_INT_SOFT.
  44. * @protect: Save for VIC_PROTECT.
  45. * @domain: The IRQ domain for the VIC.
  46. */
  47. struct vic_device {
  48. void __iomem *base;
  49. int irq;
  50. u32 resume_sources;
  51. u32 resume_irqs;
  52. u32 int_select;
  53. u32 int_enable;
  54. u32 soft_int;
  55. u32 protect;
  56. struct irq_domain *domain;
  57. };
  58. /* we cannot allocate memory when VICs are initially registered */
  59. static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
  60. static int vic_id;
  61. /**
  62. * vic_init2 - common initialisation code
  63. * @base: Base of the VIC.
  64. *
  65. * Common initialisation code for registration
  66. * and resume.
  67. */
  68. static void vic_init2(void __iomem *base)
  69. {
  70. int i;
  71. for (i = 0; i < 16; i++) {
  72. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  73. writel(VIC_VECT_CNTL_ENABLE | i, reg);
  74. }
  75. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  76. }
  77. #ifdef CONFIG_PM
  78. static void resume_one_vic(struct vic_device *vic)
  79. {
  80. void __iomem *base = vic->base;
  81. printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
  82. /* re-initialise static settings */
  83. vic_init2(base);
  84. writel(vic->int_select, base + VIC_INT_SELECT);
  85. writel(vic->protect, base + VIC_PROTECT);
  86. /* set the enabled ints and then clear the non-enabled */
  87. writel(vic->int_enable, base + VIC_INT_ENABLE);
  88. writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
  89. /* and the same for the soft-int register */
  90. writel(vic->soft_int, base + VIC_INT_SOFT);
  91. writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
  92. }
  93. static void vic_resume(void)
  94. {
  95. int id;
  96. for (id = vic_id - 1; id >= 0; id--)
  97. resume_one_vic(vic_devices + id);
  98. }
  99. static void suspend_one_vic(struct vic_device *vic)
  100. {
  101. void __iomem *base = vic->base;
  102. printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
  103. vic->int_select = readl(base + VIC_INT_SELECT);
  104. vic->int_enable = readl(base + VIC_INT_ENABLE);
  105. vic->soft_int = readl(base + VIC_INT_SOFT);
  106. vic->protect = readl(base + VIC_PROTECT);
  107. /* set the interrupts (if any) that are used for
  108. * resuming the system */
  109. writel(vic->resume_irqs, base + VIC_INT_ENABLE);
  110. writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
  111. }
  112. static int vic_suspend(void)
  113. {
  114. int id;
  115. for (id = 0; id < vic_id; id++)
  116. suspend_one_vic(vic_devices + id);
  117. return 0;
  118. }
  119. struct syscore_ops vic_syscore_ops = {
  120. .suspend = vic_suspend,
  121. .resume = vic_resume,
  122. };
  123. /**
  124. * vic_pm_init - initicall to register VIC pm
  125. *
  126. * This is called via late_initcall() to register
  127. * the resources for the VICs due to the early
  128. * nature of the VIC's registration.
  129. */
  130. static int __init vic_pm_init(void)
  131. {
  132. if (vic_id > 0)
  133. register_syscore_ops(&vic_syscore_ops);
  134. return 0;
  135. }
  136. late_initcall(vic_pm_init);
  137. #endif /* CONFIG_PM */
  138. /**
  139. * vic_register() - Register a VIC.
  140. * @base: The base address of the VIC.
  141. * @irq: The base IRQ for the VIC.
  142. * @valid_sources: bitmask of valid interrupts
  143. * @resume_sources: bitmask of interrupts allowed for resume sources.
  144. * @node: The device tree node associated with the VIC.
  145. *
  146. * Register the VIC with the system device tree so that it can be notified
  147. * of suspend and resume requests and ensure that the correct actions are
  148. * taken to re-instate the settings on resume.
  149. *
  150. * This also configures the IRQ domain for the VIC.
  151. */
  152. static void __init vic_register(void __iomem *base, unsigned int irq,
  153. u32 valid_sources, u32 resume_sources,
  154. struct device_node *node)
  155. {
  156. struct vic_device *v;
  157. if (vic_id >= ARRAY_SIZE(vic_devices)) {
  158. printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
  159. return;
  160. }
  161. v = &vic_devices[vic_id];
  162. v->base = base;
  163. v->resume_sources = resume_sources;
  164. v->irq = irq;
  165. vic_id++;
  166. v->domain = irq_domain_add_legacy(node, fls(valid_sources), irq, 0,
  167. &vic_irqdomain_ops, v);
  168. }
  169. static void vic_ack_irq(struct irq_data *d)
  170. {
  171. void __iomem *base = irq_data_get_irq_chip_data(d);
  172. unsigned int irq = d->hwirq;
  173. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  174. /* moreover, clear the soft-triggered, in case it was the reason */
  175. writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
  176. }
  177. static void vic_mask_irq(struct irq_data *d)
  178. {
  179. void __iomem *base = irq_data_get_irq_chip_data(d);
  180. unsigned int irq = d->hwirq;
  181. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  182. }
  183. static void vic_unmask_irq(struct irq_data *d)
  184. {
  185. void __iomem *base = irq_data_get_irq_chip_data(d);
  186. unsigned int irq = d->hwirq;
  187. writel(1 << irq, base + VIC_INT_ENABLE);
  188. }
  189. #if defined(CONFIG_PM)
  190. static struct vic_device *vic_from_irq(unsigned int irq)
  191. {
  192. struct vic_device *v = vic_devices;
  193. unsigned int base_irq = irq & ~31;
  194. int id;
  195. for (id = 0; id < vic_id; id++, v++) {
  196. if (v->irq == base_irq)
  197. return v;
  198. }
  199. return NULL;
  200. }
  201. static int vic_set_wake(struct irq_data *d, unsigned int on)
  202. {
  203. struct vic_device *v = vic_from_irq(d->irq);
  204. unsigned int off = d->hwirq;
  205. u32 bit = 1 << off;
  206. if (!v)
  207. return -EINVAL;
  208. if (!(bit & v->resume_sources))
  209. return -EINVAL;
  210. if (on)
  211. v->resume_irqs |= bit;
  212. else
  213. v->resume_irqs &= ~bit;
  214. return 0;
  215. }
  216. #else
  217. #define vic_set_wake NULL
  218. #endif /* CONFIG_PM */
  219. static struct irq_chip vic_chip = {
  220. .name = "VIC",
  221. .irq_ack = vic_ack_irq,
  222. .irq_mask = vic_mask_irq,
  223. .irq_unmask = vic_unmask_irq,
  224. .irq_set_wake = vic_set_wake,
  225. };
  226. static void __init vic_disable(void __iomem *base)
  227. {
  228. writel(0, base + VIC_INT_SELECT);
  229. writel(0, base + VIC_INT_ENABLE);
  230. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  231. writel(0, base + VIC_ITCR);
  232. writel(~0, base + VIC_INT_SOFT_CLEAR);
  233. }
  234. static void __init vic_clear_interrupts(void __iomem *base)
  235. {
  236. unsigned int i;
  237. writel(0, base + VIC_PL190_VECT_ADDR);
  238. for (i = 0; i < 19; i++) {
  239. unsigned int value;
  240. value = readl(base + VIC_PL190_VECT_ADDR);
  241. writel(value, base + VIC_PL190_VECT_ADDR);
  242. }
  243. }
  244. static void __init vic_set_irq_sources(void __iomem *base,
  245. unsigned int irq_start, u32 vic_sources)
  246. {
  247. unsigned int i;
  248. for (i = 0; i < 32; i++) {
  249. if (vic_sources & (1 << i)) {
  250. unsigned int irq = irq_start + i;
  251. irq_set_chip_and_handler(irq, &vic_chip,
  252. handle_level_irq);
  253. irq_set_chip_data(irq, base);
  254. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  255. }
  256. }
  257. }
  258. /*
  259. * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
  260. * The original cell has 32 interrupts, while the modified one has 64,
  261. * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
  262. * the probe function is called twice, with base set to offset 000
  263. * and 020 within the page. We call this "second block".
  264. */
  265. static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
  266. u32 vic_sources, struct device_node *node)
  267. {
  268. unsigned int i;
  269. int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
  270. /* Disable all interrupts initially. */
  271. vic_disable(base);
  272. /*
  273. * Make sure we clear all existing interrupts. The vector registers
  274. * in this cell are after the second block of general registers,
  275. * so we can address them using standard offsets, but only from
  276. * the second base address, which is 0x20 in the page
  277. */
  278. if (vic_2nd_block) {
  279. vic_clear_interrupts(base);
  280. /* ST has 16 vectors as well, but we don't enable them by now */
  281. for (i = 0; i < 16; i++) {
  282. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  283. writel(0, reg);
  284. }
  285. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  286. }
  287. vic_set_irq_sources(base, irq_start, vic_sources);
  288. vic_register(base, irq_start, vic_sources, 0, node);
  289. }
  290. void __init __vic_init(void __iomem *base, unsigned int irq_start,
  291. u32 vic_sources, u32 resume_sources,
  292. struct device_node *node)
  293. {
  294. unsigned int i;
  295. u32 cellid = 0;
  296. enum amba_vendor vendor;
  297. /* Identify which VIC cell this one is, by reading the ID */
  298. for (i = 0; i < 4; i++) {
  299. void __iomem *addr;
  300. addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
  301. cellid |= (readl(addr) & 0xff) << (8 * i);
  302. }
  303. vendor = (cellid >> 12) & 0xff;
  304. printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
  305. base, cellid, vendor);
  306. switch(vendor) {
  307. case AMBA_VENDOR_ST:
  308. vic_init_st(base, irq_start, vic_sources, node);
  309. return;
  310. default:
  311. printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
  312. /* fall through */
  313. case AMBA_VENDOR_ARM:
  314. break;
  315. }
  316. /* Disable all interrupts initially. */
  317. vic_disable(base);
  318. /* Make sure we clear all existing interrupts */
  319. vic_clear_interrupts(base);
  320. vic_init2(base);
  321. vic_set_irq_sources(base, irq_start, vic_sources);
  322. vic_register(base, irq_start, vic_sources, resume_sources, node);
  323. }
  324. /**
  325. * vic_init() - initialise a vectored interrupt controller
  326. * @base: iomem base address
  327. * @irq_start: starting interrupt number, must be muliple of 32
  328. * @vic_sources: bitmask of interrupt sources to allow
  329. * @resume_sources: bitmask of interrupt sources to allow for resume
  330. */
  331. void __init vic_init(void __iomem *base, unsigned int irq_start,
  332. u32 vic_sources, u32 resume_sources)
  333. {
  334. __vic_init(base, irq_start, vic_sources, resume_sources, NULL);
  335. }
  336. #ifdef CONFIG_OF
  337. int __init vic_of_init(struct device_node *node, struct device_node *parent)
  338. {
  339. void __iomem *regs;
  340. int irq_base;
  341. if (WARN(parent, "non-root VICs are not supported"))
  342. return -EINVAL;
  343. regs = of_iomap(node, 0);
  344. if (WARN_ON(!regs))
  345. return -EIO;
  346. irq_base = irq_alloc_descs(-1, 0, 32, numa_node_id());
  347. if (WARN_ON(irq_base < 0))
  348. goto out_unmap;
  349. __vic_init(regs, irq_base, ~0, ~0, node);
  350. return 0;
  351. out_unmap:
  352. iounmap(regs);
  353. return -EIO;
  354. }
  355. #endif /* CONFIG OF */
  356. /*
  357. * Handle each interrupt in a single VIC. Returns non-zero if we've
  358. * handled at least one interrupt. This reads the status register
  359. * before handling each interrupt, which is necessary given that
  360. * handle_IRQ may briefly re-enable interrupts for soft IRQ handling.
  361. */
  362. static int handle_one_vic(struct vic_device *vic, struct pt_regs *regs)
  363. {
  364. u32 stat, irq;
  365. int handled = 0;
  366. while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
  367. irq = ffs(stat) - 1;
  368. handle_IRQ(irq_find_mapping(vic->domain, irq), regs);
  369. handled = 1;
  370. }
  371. return handled;
  372. }
  373. /*
  374. * Keep iterating over all registered VIC's until there are no pending
  375. * interrupts.
  376. */
  377. asmlinkage void __exception_irq_entry vic_handle_irq(struct pt_regs *regs)
  378. {
  379. int i, handled;
  380. do {
  381. for (i = 0, handled = 0; i < vic_id; ++i)
  382. handled |= handle_one_vic(&vic_devices[i], regs);
  383. } while (handled);
  384. }