vic.c 10 KB

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