vic.c 9.9 KB

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