spider-pic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * External Interrupt Controller on Spider South Bridge
  4. *
  5. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  6. *
  7. * Author: Arnd Bergmann <[email protected]>
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/ioport.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/pgtable.h>
  15. #include <asm/io.h>
  16. #include "interrupt.h"
  17. /* register layout taken from Spider spec, table 7.4-4 */
  18. enum {
  19. TIR_DEN = 0x004, /* Detection Enable Register */
  20. TIR_MSK = 0x084, /* Mask Level Register */
  21. TIR_EDC = 0x0c0, /* Edge Detection Clear Register */
  22. TIR_PNDA = 0x100, /* Pending Register A */
  23. TIR_PNDB = 0x104, /* Pending Register B */
  24. TIR_CS = 0x144, /* Current Status Register */
  25. TIR_LCSA = 0x150, /* Level Current Status Register A */
  26. TIR_LCSB = 0x154, /* Level Current Status Register B */
  27. TIR_LCSC = 0x158, /* Level Current Status Register C */
  28. TIR_LCSD = 0x15c, /* Level Current Status Register D */
  29. TIR_CFGA = 0x200, /* Setting Register A0 */
  30. TIR_CFGB = 0x204, /* Setting Register B0 */
  31. /* 0x208 ... 0x3ff Setting Register An/Bn */
  32. TIR_PPNDA = 0x400, /* Packet Pending Register A */
  33. TIR_PPNDB = 0x404, /* Packet Pending Register B */
  34. TIR_PIERA = 0x408, /* Packet Output Error Register A */
  35. TIR_PIERB = 0x40c, /* Packet Output Error Register B */
  36. TIR_PIEN = 0x444, /* Packet Output Enable Register */
  37. TIR_PIPND = 0x454, /* Packet Output Pending Register */
  38. TIRDID = 0x484, /* Spider Device ID Register */
  39. REISTIM = 0x500, /* Reissue Command Timeout Time Setting */
  40. REISTIMEN = 0x504, /* Reissue Command Timeout Setting */
  41. REISWAITEN = 0x508, /* Reissue Wait Control*/
  42. };
  43. #define SPIDER_CHIP_COUNT 4
  44. #define SPIDER_SRC_COUNT 64
  45. #define SPIDER_IRQ_INVALID 63
  46. struct spider_pic {
  47. struct irq_domain *host;
  48. void __iomem *regs;
  49. unsigned int node_id;
  50. };
  51. static struct spider_pic spider_pics[SPIDER_CHIP_COUNT];
  52. static struct spider_pic *spider_irq_data_to_pic(struct irq_data *d)
  53. {
  54. return irq_data_get_irq_chip_data(d);
  55. }
  56. static void __iomem *spider_get_irq_config(struct spider_pic *pic,
  57. unsigned int src)
  58. {
  59. return pic->regs + TIR_CFGA + 8 * src;
  60. }
  61. static void spider_unmask_irq(struct irq_data *d)
  62. {
  63. struct spider_pic *pic = spider_irq_data_to_pic(d);
  64. void __iomem *cfg = spider_get_irq_config(pic, irqd_to_hwirq(d));
  65. out_be32(cfg, in_be32(cfg) | 0x30000000u);
  66. }
  67. static void spider_mask_irq(struct irq_data *d)
  68. {
  69. struct spider_pic *pic = spider_irq_data_to_pic(d);
  70. void __iomem *cfg = spider_get_irq_config(pic, irqd_to_hwirq(d));
  71. out_be32(cfg, in_be32(cfg) & ~0x30000000u);
  72. }
  73. static void spider_ack_irq(struct irq_data *d)
  74. {
  75. struct spider_pic *pic = spider_irq_data_to_pic(d);
  76. unsigned int src = irqd_to_hwirq(d);
  77. /* Reset edge detection logic if necessary
  78. */
  79. if (irqd_is_level_type(d))
  80. return;
  81. /* Only interrupts 47 to 50 can be set to edge */
  82. if (src < 47 || src > 50)
  83. return;
  84. /* Perform the clear of the edge logic */
  85. out_be32(pic->regs + TIR_EDC, 0x100 | (src & 0xf));
  86. }
  87. static int spider_set_irq_type(struct irq_data *d, unsigned int type)
  88. {
  89. unsigned int sense = type & IRQ_TYPE_SENSE_MASK;
  90. struct spider_pic *pic = spider_irq_data_to_pic(d);
  91. unsigned int hw = irqd_to_hwirq(d);
  92. void __iomem *cfg = spider_get_irq_config(pic, hw);
  93. u32 old_mask;
  94. u32 ic;
  95. /* Note that only level high is supported for most interrupts */
  96. if (sense != IRQ_TYPE_NONE && sense != IRQ_TYPE_LEVEL_HIGH &&
  97. (hw < 47 || hw > 50))
  98. return -EINVAL;
  99. /* Decode sense type */
  100. switch(sense) {
  101. case IRQ_TYPE_EDGE_RISING:
  102. ic = 0x3;
  103. break;
  104. case IRQ_TYPE_EDGE_FALLING:
  105. ic = 0x2;
  106. break;
  107. case IRQ_TYPE_LEVEL_LOW:
  108. ic = 0x0;
  109. break;
  110. case IRQ_TYPE_LEVEL_HIGH:
  111. case IRQ_TYPE_NONE:
  112. ic = 0x1;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. /* Configure the source. One gross hack that was there before and
  118. * that I've kept around is the priority to the BE which I set to
  119. * be the same as the interrupt source number. I don't know whether
  120. * that's supposed to make any kind of sense however, we'll have to
  121. * decide that, but for now, I'm not changing the behaviour.
  122. */
  123. old_mask = in_be32(cfg) & 0x30000000u;
  124. out_be32(cfg, old_mask | (ic << 24) | (0x7 << 16) |
  125. (pic->node_id << 4) | 0xe);
  126. out_be32(cfg + 4, (0x2 << 16) | (hw & 0xff));
  127. return 0;
  128. }
  129. static struct irq_chip spider_pic = {
  130. .name = "SPIDER",
  131. .irq_unmask = spider_unmask_irq,
  132. .irq_mask = spider_mask_irq,
  133. .irq_ack = spider_ack_irq,
  134. .irq_set_type = spider_set_irq_type,
  135. };
  136. static int spider_host_map(struct irq_domain *h, unsigned int virq,
  137. irq_hw_number_t hw)
  138. {
  139. irq_set_chip_data(virq, h->host_data);
  140. irq_set_chip_and_handler(virq, &spider_pic, handle_level_irq);
  141. /* Set default irq type */
  142. irq_set_irq_type(virq, IRQ_TYPE_NONE);
  143. return 0;
  144. }
  145. static int spider_host_xlate(struct irq_domain *h, struct device_node *ct,
  146. const u32 *intspec, unsigned int intsize,
  147. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  148. {
  149. /* Spider interrupts have 2 cells, first is the interrupt source,
  150. * second, well, I don't know for sure yet ... We mask the top bits
  151. * because old device-trees encode a node number in there
  152. */
  153. *out_hwirq = intspec[0] & 0x3f;
  154. *out_flags = IRQ_TYPE_LEVEL_HIGH;
  155. return 0;
  156. }
  157. static const struct irq_domain_ops spider_host_ops = {
  158. .map = spider_host_map,
  159. .xlate = spider_host_xlate,
  160. };
  161. static void spider_irq_cascade(struct irq_desc *desc)
  162. {
  163. struct irq_chip *chip = irq_desc_get_chip(desc);
  164. struct spider_pic *pic = irq_desc_get_handler_data(desc);
  165. unsigned int cs;
  166. cs = in_be32(pic->regs + TIR_CS) >> 24;
  167. if (cs != SPIDER_IRQ_INVALID)
  168. generic_handle_domain_irq(pic->host, cs);
  169. chip->irq_eoi(&desc->irq_data);
  170. }
  171. /* For hooking up the cascade we have a problem. Our device-tree is
  172. * crap and we don't know on which BE iic interrupt we are hooked on at
  173. * least not the "standard" way. We can reconstitute it based on two
  174. * informations though: which BE node we are connected to and whether
  175. * we are connected to IOIF0 or IOIF1. Right now, we really only care
  176. * about the IBM cell blade and we know that its firmware gives us an
  177. * interrupt-map property which is pretty strange.
  178. */
  179. static unsigned int __init spider_find_cascade_and_node(struct spider_pic *pic)
  180. {
  181. unsigned int virq;
  182. const u32 *imap, *tmp;
  183. int imaplen, intsize, unit;
  184. struct device_node *iic;
  185. struct device_node *of_node;
  186. of_node = irq_domain_get_of_node(pic->host);
  187. /* First, we check whether we have a real "interrupts" in the device
  188. * tree in case the device-tree is ever fixed
  189. */
  190. virq = irq_of_parse_and_map(of_node, 0);
  191. if (virq)
  192. return virq;
  193. /* Now do the horrible hacks */
  194. tmp = of_get_property(of_node, "#interrupt-cells", NULL);
  195. if (tmp == NULL)
  196. return 0;
  197. intsize = *tmp;
  198. imap = of_get_property(of_node, "interrupt-map", &imaplen);
  199. if (imap == NULL || imaplen < (intsize + 1))
  200. return 0;
  201. iic = of_find_node_by_phandle(imap[intsize]);
  202. if (iic == NULL)
  203. return 0;
  204. imap += intsize + 1;
  205. tmp = of_get_property(iic, "#interrupt-cells", NULL);
  206. if (tmp == NULL) {
  207. of_node_put(iic);
  208. return 0;
  209. }
  210. intsize = *tmp;
  211. /* Assume unit is last entry of interrupt specifier */
  212. unit = imap[intsize - 1];
  213. /* Ok, we have a unit, now let's try to get the node */
  214. tmp = of_get_property(iic, "ibm,interrupt-server-ranges", NULL);
  215. if (tmp == NULL) {
  216. of_node_put(iic);
  217. return 0;
  218. }
  219. /* ugly as hell but works for now */
  220. pic->node_id = (*tmp) >> 1;
  221. of_node_put(iic);
  222. /* Ok, now let's get cracking. You may ask me why I just didn't match
  223. * the iic host from the iic OF node, but that way I'm still compatible
  224. * with really really old old firmwares for which we don't have a node
  225. */
  226. /* Manufacture an IIC interrupt number of class 2 */
  227. virq = irq_create_mapping(NULL,
  228. (pic->node_id << IIC_IRQ_NODE_SHIFT) |
  229. (2 << IIC_IRQ_CLASS_SHIFT) |
  230. unit);
  231. if (!virq)
  232. printk(KERN_ERR "spider_pic: failed to map cascade !");
  233. return virq;
  234. }
  235. static void __init spider_init_one(struct device_node *of_node, int chip,
  236. unsigned long addr)
  237. {
  238. struct spider_pic *pic = &spider_pics[chip];
  239. int i, virq;
  240. /* Map registers */
  241. pic->regs = ioremap(addr, 0x1000);
  242. if (pic->regs == NULL)
  243. panic("spider_pic: can't map registers !");
  244. /* Allocate a host */
  245. pic->host = irq_domain_add_linear(of_node, SPIDER_SRC_COUNT,
  246. &spider_host_ops, pic);
  247. if (pic->host == NULL)
  248. panic("spider_pic: can't allocate irq host !");
  249. /* Go through all sources and disable them */
  250. for (i = 0; i < SPIDER_SRC_COUNT; i++) {
  251. void __iomem *cfg = pic->regs + TIR_CFGA + 8 * i;
  252. out_be32(cfg, in_be32(cfg) & ~0x30000000u);
  253. }
  254. /* do not mask any interrupts because of level */
  255. out_be32(pic->regs + TIR_MSK, 0x0);
  256. /* enable interrupt packets to be output */
  257. out_be32(pic->regs + TIR_PIEN, in_be32(pic->regs + TIR_PIEN) | 0x1);
  258. /* Hook up the cascade interrupt to the iic and nodeid */
  259. virq = spider_find_cascade_and_node(pic);
  260. if (!virq)
  261. return;
  262. irq_set_handler_data(virq, pic);
  263. irq_set_chained_handler(virq, spider_irq_cascade);
  264. printk(KERN_INFO "spider_pic: node %d, addr: 0x%lx %pOF\n",
  265. pic->node_id, addr, of_node);
  266. /* Enable the interrupt detection enable bit. Do this last! */
  267. out_be32(pic->regs + TIR_DEN, in_be32(pic->regs + TIR_DEN) | 0x1);
  268. }
  269. void __init spider_init_IRQ(void)
  270. {
  271. struct resource r;
  272. struct device_node *dn;
  273. int chip = 0;
  274. /* XXX node numbers are totally bogus. We _hope_ we get the device
  275. * nodes in the right order here but that's definitely not guaranteed,
  276. * we need to get the node from the device tree instead.
  277. * There is currently no proper property for it (but our whole
  278. * device-tree is bogus anyway) so all we can do is pray or maybe test
  279. * the address and deduce the node-id
  280. */
  281. for_each_node_by_name(dn, "interrupt-controller") {
  282. if (of_device_is_compatible(dn, "CBEA,platform-spider-pic")) {
  283. if (of_address_to_resource(dn, 0, &r)) {
  284. printk(KERN_WARNING "spider-pic: Failed\n");
  285. continue;
  286. }
  287. } else if (of_device_is_compatible(dn, "sti,platform-spider-pic")
  288. && (chip < 2)) {
  289. static long hard_coded_pics[] =
  290. { 0x24000008000ul, 0x34000008000ul};
  291. r.start = hard_coded_pics[chip];
  292. } else
  293. continue;
  294. spider_init_one(dn, chip++, r.start);
  295. }
  296. }