vic.c 10 KB

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