vic.c 12 KB

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