vic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/init.h>
  22. #include <linux/list.h>
  23. #include <linux/io.h>
  24. #include <linux/sysdev.h>
  25. #include <asm/mach/irq.h>
  26. #include <asm/hardware/vic.h>
  27. static void vic_mask_irq(unsigned int irq)
  28. {
  29. void __iomem *base = get_irq_chip_data(irq);
  30. irq &= 31;
  31. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  32. }
  33. static void vic_unmask_irq(unsigned int irq)
  34. {
  35. void __iomem *base = get_irq_chip_data(irq);
  36. irq &= 31;
  37. writel(1 << irq, base + VIC_INT_ENABLE);
  38. }
  39. /**
  40. * vic_init2 - common initialisation code
  41. * @base: Base of the VIC.
  42. *
  43. * Common initialisation code for registeration
  44. * and resume.
  45. */
  46. static void vic_init2(void __iomem *base)
  47. {
  48. int i;
  49. for (i = 0; i < 16; i++) {
  50. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  51. writel(VIC_VECT_CNTL_ENABLE | i, reg);
  52. }
  53. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  54. }
  55. #if defined(CONFIG_PM)
  56. /**
  57. * struct vic_device - VIC PM device
  58. * @sysdev: The system device which is registered.
  59. * @irq: The IRQ number for the base of the VIC.
  60. * @base: The register base for the VIC.
  61. * @resume_sources: A bitmask of interrupts for resume.
  62. * @resume_irqs: The IRQs enabled for resume.
  63. * @int_select: Save for VIC_INT_SELECT.
  64. * @int_enable: Save for VIC_INT_ENABLE.
  65. * @soft_int: Save for VIC_INT_SOFT.
  66. * @protect: Save for VIC_PROTECT.
  67. */
  68. struct vic_device {
  69. struct sys_device sysdev;
  70. void __iomem *base;
  71. int irq;
  72. u32 resume_sources;
  73. u32 resume_irqs;
  74. u32 int_select;
  75. u32 int_enable;
  76. u32 soft_int;
  77. u32 protect;
  78. };
  79. /* we cannot allocate memory when VICs are initially registered */
  80. static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
  81. static inline struct vic_device *to_vic(struct sys_device *sys)
  82. {
  83. return container_of(sys, struct vic_device, sysdev);
  84. }
  85. static int vic_id;
  86. static int vic_class_resume(struct sys_device *dev)
  87. {
  88. struct vic_device *vic = to_vic(dev);
  89. void __iomem *base = vic->base;
  90. printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
  91. /* re-initialise static settings */
  92. vic_init2(base);
  93. writel(vic->int_select, base + VIC_INT_SELECT);
  94. writel(vic->protect, base + VIC_PROTECT);
  95. /* set the enabled ints and then clear the non-enabled */
  96. writel(vic->int_enable, base + VIC_INT_ENABLE);
  97. writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
  98. /* and the same for the soft-int register */
  99. writel(vic->soft_int, base + VIC_INT_SOFT);
  100. writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
  101. return 0;
  102. }
  103. static int vic_class_suspend(struct sys_device *dev, pm_message_t state)
  104. {
  105. struct vic_device *vic = to_vic(dev);
  106. void __iomem *base = vic->base;
  107. printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
  108. vic->int_select = readl(base + VIC_INT_SELECT);
  109. vic->int_enable = readl(base + VIC_INT_ENABLE);
  110. vic->soft_int = readl(base + VIC_INT_SOFT);
  111. vic->protect = readl(base + VIC_PROTECT);
  112. /* set the interrupts (if any) that are used for
  113. * resuming the system */
  114. writel(vic->resume_irqs, base + VIC_INT_ENABLE);
  115. writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
  116. return 0;
  117. }
  118. struct sysdev_class vic_class = {
  119. .name = "vic",
  120. .suspend = vic_class_suspend,
  121. .resume = vic_class_resume,
  122. };
  123. /**
  124. * vic_pm_register - Register a VIC for later power management control
  125. * @base: The base address of the VIC.
  126. * @irq: The base IRQ for the VIC.
  127. * @resume_sources: bitmask of interrupts allowed for resume sources.
  128. *
  129. * Register the VIC with the system device tree so that it can be notified
  130. * of suspend and resume requests and ensure that the correct actions are
  131. * taken to re-instate the settings on resume.
  132. */
  133. static void __init vic_pm_register(void __iomem *base, unsigned int irq, u32 resume_sources)
  134. {
  135. struct vic_device *v;
  136. if (vic_id >= ARRAY_SIZE(vic_devices))
  137. printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
  138. else {
  139. v = &vic_devices[vic_id];
  140. v->base = base;
  141. v->resume_sources = resume_sources;
  142. v->irq = irq;
  143. vic_id++;
  144. }
  145. }
  146. /**
  147. * vic_pm_init - initicall to register VIC pm
  148. *
  149. * This is called via late_initcall() to register
  150. * the resources for the VICs due to the early
  151. * nature of the VIC's registration.
  152. */
  153. static int __init vic_pm_init(void)
  154. {
  155. struct vic_device *dev = vic_devices;
  156. int err;
  157. int id;
  158. if (vic_id == 0)
  159. return 0;
  160. err = sysdev_class_register(&vic_class);
  161. if (err) {
  162. printk(KERN_ERR "%s: cannot register class\n", __func__);
  163. return err;
  164. }
  165. for (id = 0; id < vic_id; id++, dev++) {
  166. dev->sysdev.id = id;
  167. dev->sysdev.cls = &vic_class;
  168. err = sysdev_register(&dev->sysdev);
  169. if (err) {
  170. printk(KERN_ERR "%s: failed to register device\n",
  171. __func__);
  172. return err;
  173. }
  174. }
  175. return 0;
  176. }
  177. late_initcall(vic_pm_init);
  178. static struct vic_device *vic_from_irq(unsigned int irq)
  179. {
  180. struct vic_device *v = vic_devices;
  181. unsigned int base_irq = irq & ~31;
  182. int id;
  183. for (id = 0; id < vic_id; id++, v++) {
  184. if (v->irq == base_irq)
  185. return v;
  186. }
  187. return NULL;
  188. }
  189. static int vic_set_wake(unsigned int irq, unsigned int on)
  190. {
  191. struct vic_device *v = vic_from_irq(irq);
  192. unsigned int off = irq & 31;
  193. u32 bit = 1 << off;
  194. if (!v)
  195. return -EINVAL;
  196. if (!(bit & v->resume_sources))
  197. return -EINVAL;
  198. if (on)
  199. v->resume_irqs |= bit;
  200. else
  201. v->resume_irqs &= ~bit;
  202. return 0;
  203. }
  204. #else
  205. static inline void vic_pm_register(void __iomem *base, unsigned int irq, u32 arg1) { }
  206. #define vic_set_wake NULL
  207. #endif /* CONFIG_PM */
  208. static struct irq_chip vic_chip = {
  209. .name = "VIC",
  210. .ack = vic_mask_irq,
  211. .mask = vic_mask_irq,
  212. .unmask = vic_unmask_irq,
  213. .set_wake = vic_set_wake,
  214. };
  215. /**
  216. * vic_init - initialise a vectored interrupt controller
  217. * @base: iomem base address
  218. * @irq_start: starting interrupt number, must be muliple of 32
  219. * @vic_sources: bitmask of interrupt sources to allow
  220. * @resume_sources: bitmask of interrupt sources to allow for resume
  221. */
  222. void __init vic_init(void __iomem *base, unsigned int irq_start,
  223. u32 vic_sources, u32 resume_sources)
  224. {
  225. unsigned int i;
  226. /* Disable all interrupts initially. */
  227. writel(0, base + VIC_INT_SELECT);
  228. writel(0, base + VIC_INT_ENABLE);
  229. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  230. writel(0, base + VIC_IRQ_STATUS);
  231. writel(0, base + VIC_ITCR);
  232. writel(~0, base + VIC_INT_SOFT_CLEAR);
  233. /*
  234. * Make sure we clear all existing interrupts
  235. */
  236. writel(0, base + VIC_PL190_VECT_ADDR);
  237. for (i = 0; i < 19; i++) {
  238. unsigned int value;
  239. value = readl(base + VIC_PL190_VECT_ADDR);
  240. writel(value, base + VIC_PL190_VECT_ADDR);
  241. }
  242. vic_init2(base);
  243. for (i = 0; i < 32; i++) {
  244. if (vic_sources & (1 << i)) {
  245. unsigned int irq = irq_start + i;
  246. set_irq_chip(irq, &vic_chip);
  247. set_irq_chip_data(irq, base);
  248. set_irq_handler(irq, handle_level_irq);
  249. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  250. }
  251. }
  252. vic_pm_register(base, irq_start, resume_sources);
  253. }