irq-vic.c 14 KB

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