irq-vic.c 13 KB

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