irq-vic.c 13 KB

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