spider-pic.c 11 KB

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