vic.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. /* The PL190 cell from ARM has been modified by ST, so handle both here */
  216. static void vik_init_st(void __iomem *base, unsigned int irq_start,
  217. u32 vic_sources);
  218. enum vic_vendor {
  219. VENDOR_ARM = 0x41,
  220. VENDOR_ST = 0x80,
  221. };
  222. /**
  223. * vic_init - initialise a vectored interrupt controller
  224. * @base: iomem base address
  225. * @irq_start: starting interrupt number, must be muliple of 32
  226. * @vic_sources: bitmask of interrupt sources to allow
  227. * @resume_sources: bitmask of interrupt sources to allow for resume
  228. */
  229. void __init vic_init(void __iomem *base, unsigned int irq_start,
  230. u32 vic_sources, u32 resume_sources)
  231. {
  232. unsigned int i;
  233. u32 cellid = 0;
  234. enum vic_vendor vendor;
  235. /* Identify which VIC cell this one is, by reading the ID */
  236. for (i = 0; i < 4; i++) {
  237. u32 addr = ((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
  238. cellid |= (readl(addr) & 0xff) << (8 * i);
  239. }
  240. vendor = (cellid >> 12) & 0xff;
  241. printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
  242. base, cellid, vendor);
  243. switch(vendor) {
  244. case VENDOR_ST:
  245. vik_init_st(base, irq_start, vic_sources);
  246. return;
  247. default:
  248. printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
  249. /* fall through */
  250. case VENDOR_ARM:
  251. break;
  252. }
  253. /* Disable all interrupts initially. */
  254. writel(0, base + VIC_INT_SELECT);
  255. writel(0, base + VIC_INT_ENABLE);
  256. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  257. writel(0, base + VIC_IRQ_STATUS);
  258. writel(0, base + VIC_ITCR);
  259. writel(~0, base + VIC_INT_SOFT_CLEAR);
  260. /*
  261. * Make sure we clear all existing interrupts
  262. */
  263. writel(0, base + VIC_PL190_VECT_ADDR);
  264. for (i = 0; i < 19; i++) {
  265. unsigned int value;
  266. value = readl(base + VIC_PL190_VECT_ADDR);
  267. writel(value, base + VIC_PL190_VECT_ADDR);
  268. }
  269. vic_init2(base);
  270. for (i = 0; i < 32; i++) {
  271. if (vic_sources & (1 << i)) {
  272. unsigned int irq = irq_start + i;
  273. set_irq_chip(irq, &vic_chip);
  274. set_irq_chip_data(irq, base);
  275. set_irq_handler(irq, handle_level_irq);
  276. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  277. }
  278. }
  279. vic_pm_register(base, irq_start, resume_sources);
  280. }
  281. /*
  282. * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
  283. * The original cell has 32 interrupts, while the modified one has 64,
  284. * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
  285. * the probe function is called twice, with base set to offset 000
  286. * and 020 within the page. We call this "second block".
  287. */
  288. static void __init vik_init_st(void __iomem *base, unsigned int irq_start,
  289. u32 vic_sources)
  290. {
  291. unsigned int i;
  292. int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
  293. /* Disable all interrupts initially. */
  294. writel(0, base + VIC_INT_SELECT);
  295. writel(0, base + VIC_INT_ENABLE);
  296. writel(~0, base + VIC_INT_ENABLE_CLEAR);
  297. writel(0, base + VIC_IRQ_STATUS);
  298. writel(0, base + VIC_ITCR);
  299. writel(~0, base + VIC_INT_SOFT_CLEAR);
  300. /*
  301. * Make sure we clear all existing interrupts. The vector registers
  302. * in this cell are after the second block of general registers,
  303. * so we can address them using standard offsets, but only from
  304. * the second base address, which is 0x20 in the page
  305. */
  306. if (vic_2nd_block) {
  307. writel(0, base + VIC_PL190_VECT_ADDR);
  308. for (i = 0; i < 19; i++) {
  309. unsigned int value;
  310. value = readl(base + VIC_PL190_VECT_ADDR);
  311. writel(value, base + VIC_PL190_VECT_ADDR);
  312. }
  313. /* ST has 16 vectors as well, but we don't enable them by now */
  314. for (i = 0; i < 16; i++) {
  315. void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
  316. writel(0, reg);
  317. }
  318. writel(32, base + VIC_PL190_DEF_VECT_ADDR);
  319. }
  320. for (i = 0; i < 32; i++) {
  321. if (vic_sources & (1 << i)) {
  322. unsigned int irq = irq_start + i;
  323. set_irq_chip(irq, &vic_chip);
  324. set_irq_chip_data(irq, base);
  325. set_irq_handler(irq, handle_level_irq);
  326. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  327. }
  328. }
  329. }