vic.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/syscore_ops.h>
  25. #include <linux/device.h>
  26. #include <linux/amba/bus.h>
  27. #include <asm/mach/irq.h>
  28. #include <asm/hardware/vic.h>
  29. #ifdef CONFIG_PM
  30. /**
  31. * struct vic_device - VIC PM device
  32. * @irq: The IRQ number for the base of the VIC.
  33. * @base: The register base for the VIC.
  34. * @resume_sources: A bitmask of interrupts for resume.
  35. * @resume_irqs: The IRQs enabled for resume.
  36. * @int_select: Save for VIC_INT_SELECT.
  37. * @int_enable: Save for VIC_INT_ENABLE.
  38. * @soft_int: Save for VIC_INT_SOFT.
  39. * @protect: Save for VIC_PROTECT.
  40. */
  41. struct vic_device {
  42. void __iomem *base;
  43. int irq;
  44. u32 resume_sources;
  45. u32 resume_irqs;
  46. u32 int_select;
  47. u32 int_enable;
  48. u32 soft_int;
  49. u32 protect;
  50. };
  51. /* we cannot allocate memory when VICs are initially registered */
  52. static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
  53. static int vic_id;
  54. #endif /* CONFIG_PM */
  55. /**
  56. * vic_init2 - common initialisation code
  57. * @base: Base of the VIC.
  58. *
  59. * Common initialisation code for registration
  60. * and resume.
  61. */
  62. static void vic_init2(void __iomem *base)
  63. {
  64. int i;
  65. for (i = 0; i < 16; i++) {
  66. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  67. writel(VIC_VECT_CNTL_ENABLE | i, reg);
  68. }
  69. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  70. }
  71. #ifdef CONFIG_PM
  72. static void resume_one_vic(struct vic_device *vic)
  73. {
  74. void __iomem *base = vic->base;
  75. printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
  76. /* re-initialise static settings */
  77. vic_init2(base);
  78. writel(vic->int_select, base + VIC_INT_SELECT);
  79. writel(vic->protect, base + VIC_PROTECT);
  80. /* set the enabled ints and then clear the non-enabled */
  81. writel(vic->int_enable, base + VIC_INT_ENABLE);
  82. writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
  83. /* and the same for the soft-int register */
  84. writel(vic->soft_int, base + VIC_INT_SOFT);
  85. writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
  86. }
  87. static void vic_resume(void)
  88. {
  89. int id;
  90. for (id = vic_id - 1; id >= 0; id--)
  91. resume_one_vic(vic_devices + id);
  92. }
  93. static void suspend_one_vic(struct vic_device *vic)
  94. {
  95. void __iomem *base = vic->base;
  96. printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
  97. vic->int_select = readl(base + VIC_INT_SELECT);
  98. vic->int_enable = readl(base + VIC_INT_ENABLE);
  99. vic->soft_int = readl(base + VIC_INT_SOFT);
  100. vic->protect = readl(base + VIC_PROTECT);
  101. /* set the interrupts (if any) that are used for
  102. * resuming the system */
  103. writel(vic->resume_irqs, base + VIC_INT_ENABLE);
  104. writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
  105. }
  106. static int vic_suspend(void)
  107. {
  108. int id;
  109. for (id = 0; id < vic_id; id++)
  110. suspend_one_vic(vic_devices + id);
  111. return 0;
  112. }
  113. struct syscore_ops vic_syscore_ops = {
  114. .suspend = vic_suspend,
  115. .resume = vic_resume,
  116. };
  117. /**
  118. * vic_pm_init - initicall to register VIC pm
  119. *
  120. * This is called via late_initcall() to register
  121. * the resources for the VICs due to the early
  122. * nature of the VIC's registration.
  123. */
  124. static int __init vic_pm_init(void)
  125. {
  126. if (vic_id > 0)
  127. register_syscore_ops(&vic_syscore_ops);
  128. return 0;
  129. }
  130. late_initcall(vic_pm_init);
  131. /**
  132. * vic_pm_register - Register a VIC for later power management control
  133. * @base: The base address of the VIC.
  134. * @irq: The base IRQ for the VIC.
  135. * @resume_sources: bitmask of interrupts allowed for resume sources.
  136. *
  137. * Register the VIC with the system device tree so that it can be notified
  138. * of suspend and resume requests and ensure that the correct actions are
  139. * taken to re-instate the settings on resume.
  140. */
  141. static void __init vic_pm_register(void __iomem *base, unsigned int irq, u32 resume_sources)
  142. {
  143. struct vic_device *v;
  144. if (vic_id >= ARRAY_SIZE(vic_devices))
  145. printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
  146. else {
  147. v = &vic_devices[vic_id];
  148. v->base = base;
  149. v->resume_sources = resume_sources;
  150. v->irq = irq;
  151. vic_id++;
  152. }
  153. }
  154. #else
  155. static inline void vic_pm_register(void __iomem *base, unsigned int irq, u32 arg1) { }
  156. #endif /* CONFIG_PM */
  157. static void vic_ack_irq(struct irq_data *d)
  158. {
  159. void __iomem *base = irq_data_get_irq_chip_data(d);
  160. unsigned int irq = d->irq & 31;
  161. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  162. /* moreover, clear the soft-triggered, in case it was the reason */
  163. writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
  164. }
  165. static void vic_mask_irq(struct irq_data *d)
  166. {
  167. void __iomem *base = irq_data_get_irq_chip_data(d);
  168. unsigned int irq = d->irq & 31;
  169. writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
  170. }
  171. static void vic_unmask_irq(struct irq_data *d)
  172. {
  173. void __iomem *base = irq_data_get_irq_chip_data(d);
  174. unsigned int irq = d->irq & 31;
  175. writel(1 << irq, base + VIC_INT_ENABLE);
  176. }
  177. #if defined(CONFIG_PM)
  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(struct irq_data *d, unsigned int on)
  190. {
  191. struct vic_device *v = vic_from_irq(d->irq);
  192. unsigned int off = d->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. #define vic_set_wake NULL
  206. #endif /* CONFIG_PM */
  207. static struct irq_chip vic_chip = {
  208. .name = "VIC",
  209. .irq_ack = vic_ack_irq,
  210. .irq_mask = vic_mask_irq,
  211. .irq_unmask = vic_unmask_irq,
  212. .irq_set_wake = vic_set_wake,
  213. };
  214. static void __init vic_disable(void __iomem *base)
  215. {
  216. writel(0, base + VIC_INT_SELECT);
  217. writel(0, base + VIC_INT_ENABLE);
  218. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  219. writel(0, base + VIC_IRQ_STATUS);
  220. writel(0, base + VIC_ITCR);
  221. writel(~0, base + VIC_INT_SOFT_CLEAR);
  222. }
  223. static void __init vic_clear_interrupts(void __iomem *base)
  224. {
  225. unsigned int i;
  226. writel(0, base + VIC_PL190_VECT_ADDR);
  227. for (i = 0; i < 19; i++) {
  228. unsigned int value;
  229. value = readl(base + VIC_PL190_VECT_ADDR);
  230. writel(value, base + VIC_PL190_VECT_ADDR);
  231. }
  232. }
  233. static void __init vic_set_irq_sources(void __iomem *base,
  234. unsigned int irq_start, u32 vic_sources)
  235. {
  236. unsigned int i;
  237. for (i = 0; i < 32; i++) {
  238. if (vic_sources & (1 << i)) {
  239. unsigned int irq = irq_start + i;
  240. irq_set_chip_and_handler(irq, &vic_chip,
  241. handle_level_irq);
  242. irq_set_chip_data(irq, base);
  243. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  244. }
  245. }
  246. }
  247. /*
  248. * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
  249. * The original cell has 32 interrupts, while the modified one has 64,
  250. * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
  251. * the probe function is called twice, with base set to offset 000
  252. * and 020 within the page. We call this "second block".
  253. */
  254. static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
  255. u32 vic_sources)
  256. {
  257. unsigned int i;
  258. int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
  259. /* Disable all interrupts initially. */
  260. vic_disable(base);
  261. /*
  262. * Make sure we clear all existing interrupts. The vector registers
  263. * in this cell are after the second block of general registers,
  264. * so we can address them using standard offsets, but only from
  265. * the second base address, which is 0x20 in the page
  266. */
  267. if (vic_2nd_block) {
  268. vic_clear_interrupts(base);
  269. /* ST has 16 vectors as well, but we don't enable them by now */
  270. for (i = 0; i < 16; i++) {
  271. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  272. writel(0, reg);
  273. }
  274. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  275. }
  276. vic_set_irq_sources(base, irq_start, vic_sources);
  277. }
  278. /**
  279. * vic_init - initialise a vectored interrupt controller
  280. * @base: iomem base address
  281. * @irq_start: starting interrupt number, must be muliple of 32
  282. * @vic_sources: bitmask of interrupt sources to allow
  283. * @resume_sources: bitmask of interrupt sources to allow for resume
  284. */
  285. void __init vic_init(void __iomem *base, unsigned int irq_start,
  286. u32 vic_sources, u32 resume_sources)
  287. {
  288. unsigned int i;
  289. u32 cellid = 0;
  290. enum amba_vendor vendor;
  291. /* Identify which VIC cell this one is, by reading the ID */
  292. for (i = 0; i < 4; i++) {
  293. u32 addr = ((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
  294. cellid |= (readl(addr) & 0xff) << (8 * i);
  295. }
  296. vendor = (cellid >> 12) & 0xff;
  297. printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
  298. base, cellid, vendor);
  299. switch(vendor) {
  300. case AMBA_VENDOR_ST:
  301. vic_init_st(base, irq_start, vic_sources);
  302. return;
  303. default:
  304. printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
  305. /* fall through */
  306. case AMBA_VENDOR_ARM:
  307. break;
  308. }
  309. /* Disable all interrupts initially. */
  310. vic_disable(base);
  311. /* Make sure we clear all existing interrupts */
  312. vic_clear_interrupts(base);
  313. vic_init2(base);
  314. vic_set_irq_sources(base, irq_start, vic_sources);
  315. vic_pm_register(base, irq_start, resume_sources);
  316. }