pic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Support for the interrupt controllers found on Power Macintosh,
  3. * currently Apple's "Grand Central" interrupt controller in all
  4. * it's incarnations. OpenPIC support used on newer machines is
  5. * in a separate file
  6. *
  7. * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
  8. * Copyright (C) 2005 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  9. * IBM, Corp.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. */
  17. #include <linux/stddef.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/signal.h>
  21. #include <linux/pci.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/adb.h>
  25. #include <linux/pmu.h>
  26. #include <linux/module.h>
  27. #include <asm/sections.h>
  28. #include <asm/io.h>
  29. #include <asm/smp.h>
  30. #include <asm/prom.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/time.h>
  33. #include <asm/pmac_feature.h>
  34. #include <asm/mpic.h>
  35. #include "pmac.h"
  36. /*
  37. * XXX this should be in xmon.h, but putting it there means xmon.h
  38. * has to include <linux/interrupt.h> (to get irqreturn_t), which
  39. * causes all sorts of problems. -- paulus
  40. */
  41. extern irqreturn_t xmon_irq(int, void *, struct pt_regs *);
  42. #ifdef CONFIG_PPC32
  43. struct pmac_irq_hw {
  44. unsigned int event;
  45. unsigned int enable;
  46. unsigned int ack;
  47. unsigned int level;
  48. };
  49. /* Default addresses */
  50. static volatile struct pmac_irq_hw __iomem *pmac_irq_hw[4];
  51. #define GC_LEVEL_MASK 0x3ff00000
  52. #define OHARE_LEVEL_MASK 0x1ff00000
  53. #define HEATHROW_LEVEL_MASK 0x1ff00000
  54. static int max_irqs;
  55. static int max_real_irqs;
  56. static u32 level_mask[4];
  57. static DEFINE_SPINLOCK(pmac_pic_lock);
  58. #define GATWICK_IRQ_POOL_SIZE 10
  59. static struct interrupt_info gatwick_int_pool[GATWICK_IRQ_POOL_SIZE];
  60. #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
  61. static unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
  62. static unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
  63. static int pmac_irq_cascade = -1;
  64. static void __pmac_retrigger(unsigned int irq_nr)
  65. {
  66. if (irq_nr >= max_real_irqs && pmac_irq_cascade > 0) {
  67. __set_bit(irq_nr, ppc_lost_interrupts);
  68. irq_nr = pmac_irq_cascade;
  69. mb();
  70. }
  71. if (!__test_and_set_bit(irq_nr, ppc_lost_interrupts)) {
  72. atomic_inc(&ppc_n_lost_interrupts);
  73. set_dec(1);
  74. }
  75. }
  76. static void pmac_mask_and_ack_irq(unsigned int irq_nr)
  77. {
  78. unsigned long bit = 1UL << (irq_nr & 0x1f);
  79. int i = irq_nr >> 5;
  80. unsigned long flags;
  81. if ((unsigned)irq_nr >= max_irqs)
  82. return;
  83. spin_lock_irqsave(&pmac_pic_lock, flags);
  84. __clear_bit(irq_nr, ppc_cached_irq_mask);
  85. if (__test_and_clear_bit(irq_nr, ppc_lost_interrupts))
  86. atomic_dec(&ppc_n_lost_interrupts);
  87. out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
  88. out_le32(&pmac_irq_hw[i]->ack, bit);
  89. do {
  90. /* make sure ack gets to controller before we enable
  91. interrupts */
  92. mb();
  93. } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
  94. != (ppc_cached_irq_mask[i] & bit));
  95. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  96. }
  97. static void pmac_ack_irq(unsigned int irq_nr)
  98. {
  99. unsigned long bit = 1UL << (irq_nr & 0x1f);
  100. int i = irq_nr >> 5;
  101. unsigned long flags;
  102. if ((unsigned)irq_nr >= max_irqs)
  103. return;
  104. spin_lock_irqsave(&pmac_pic_lock, flags);
  105. if (__test_and_clear_bit(irq_nr, ppc_lost_interrupts))
  106. atomic_dec(&ppc_n_lost_interrupts);
  107. out_le32(&pmac_irq_hw[i]->ack, bit);
  108. (void)in_le32(&pmac_irq_hw[i]->ack);
  109. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  110. }
  111. static void __pmac_set_irq_mask(unsigned int irq_nr, int nokicklost)
  112. {
  113. unsigned long bit = 1UL << (irq_nr & 0x1f);
  114. int i = irq_nr >> 5;
  115. if ((unsigned)irq_nr >= max_irqs)
  116. return;
  117. /* enable unmasked interrupts */
  118. out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
  119. do {
  120. /* make sure mask gets to controller before we
  121. return to user */
  122. mb();
  123. } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
  124. != (ppc_cached_irq_mask[i] & bit));
  125. /*
  126. * Unfortunately, setting the bit in the enable register
  127. * when the device interrupt is already on *doesn't* set
  128. * the bit in the flag register or request another interrupt.
  129. */
  130. if (bit & ppc_cached_irq_mask[i] & in_le32(&pmac_irq_hw[i]->level))
  131. __pmac_retrigger(irq_nr);
  132. }
  133. /* When an irq gets requested for the first client, if it's an
  134. * edge interrupt, we clear any previous one on the controller
  135. */
  136. static unsigned int pmac_startup_irq(unsigned int irq_nr)
  137. {
  138. unsigned long flags;
  139. unsigned long bit = 1UL << (irq_nr & 0x1f);
  140. int i = irq_nr >> 5;
  141. spin_lock_irqsave(&pmac_pic_lock, flags);
  142. if ((irq_desc[irq_nr].status & IRQ_LEVEL) == 0)
  143. out_le32(&pmac_irq_hw[i]->ack, bit);
  144. __set_bit(irq_nr, ppc_cached_irq_mask);
  145. __pmac_set_irq_mask(irq_nr, 0);
  146. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  147. return 0;
  148. }
  149. static void pmac_mask_irq(unsigned int irq_nr)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave(&pmac_pic_lock, flags);
  153. __clear_bit(irq_nr, ppc_cached_irq_mask);
  154. __pmac_set_irq_mask(irq_nr, 0);
  155. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  156. }
  157. static void pmac_unmask_irq(unsigned int irq_nr)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&pmac_pic_lock, flags);
  161. __set_bit(irq_nr, ppc_cached_irq_mask);
  162. __pmac_set_irq_mask(irq_nr, 0);
  163. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  164. }
  165. static int pmac_retrigger(unsigned int irq_nr)
  166. {
  167. unsigned long flags;
  168. spin_lock_irqsave(&pmac_pic_lock, flags);
  169. __pmac_retrigger(irq_nr);
  170. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  171. return 1;
  172. }
  173. static struct irq_chip pmac_pic = {
  174. .typename = " PMAC-PIC ",
  175. .startup = pmac_startup_irq,
  176. .mask = pmac_mask_irq,
  177. .ack = pmac_ack_irq,
  178. .mask_ack = pmac_mask_and_ack_irq,
  179. .unmask = pmac_unmask_irq,
  180. .retrigger = pmac_retrigger,
  181. };
  182. static irqreturn_t gatwick_action(int cpl, void *dev_id, struct pt_regs *regs)
  183. {
  184. unsigned long flags;
  185. int irq, bits;
  186. int rc = IRQ_NONE;
  187. spin_lock_irqsave(&pmac_pic_lock, flags);
  188. for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) {
  189. int i = irq >> 5;
  190. bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
  191. /* We must read level interrupts from the level register */
  192. bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
  193. bits &= ppc_cached_irq_mask[i];
  194. if (bits == 0)
  195. continue;
  196. irq += __ilog2(bits);
  197. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  198. __do_IRQ(irq, regs);
  199. spin_lock_irqsave(&pmac_pic_lock, flags);
  200. rc = IRQ_HANDLED;
  201. }
  202. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  203. return rc;
  204. }
  205. static int pmac_get_irq(struct pt_regs *regs)
  206. {
  207. int irq;
  208. unsigned long bits = 0;
  209. unsigned long flags;
  210. #ifdef CONFIG_SMP
  211. void psurge_smp_message_recv(struct pt_regs *);
  212. /* IPI's are a hack on the powersurge -- Cort */
  213. if ( smp_processor_id() != 0 ) {
  214. psurge_smp_message_recv(regs);
  215. return -2; /* ignore, already handled */
  216. }
  217. #endif /* CONFIG_SMP */
  218. spin_lock_irqsave(&pmac_pic_lock, flags);
  219. for (irq = max_real_irqs; (irq -= 32) >= 0; ) {
  220. int i = irq >> 5;
  221. bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
  222. /* We must read level interrupts from the level register */
  223. bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
  224. bits &= ppc_cached_irq_mask[i];
  225. if (bits == 0)
  226. continue;
  227. irq += __ilog2(bits);
  228. break;
  229. }
  230. spin_unlock_irqrestore(&pmac_pic_lock, flags);
  231. return irq;
  232. }
  233. /* This routine will fix some missing interrupt values in the device tree
  234. * on the gatwick mac-io controller used by some PowerBooks
  235. *
  236. * Walking of OF nodes could use a bit more fixing up here, but it's not
  237. * very important as this is all boot time code on static portions of the
  238. * device-tree.
  239. *
  240. * However, the modifications done to "intrs" will have to be removed and
  241. * replaced with proper updates of the "interrupts" properties or
  242. * AAPL,interrupts, yet to be decided, once the dynamic parsing is there.
  243. */
  244. static void __init pmac_fix_gatwick_interrupts(struct device_node *gw,
  245. int irq_base)
  246. {
  247. struct device_node *node;
  248. int count;
  249. memset(gatwick_int_pool, 0, sizeof(gatwick_int_pool));
  250. count = 0;
  251. for (node = NULL; (node = of_get_next_child(gw, node)) != NULL;) {
  252. /* Fix SCC */
  253. if ((strcasecmp(node->name, "escc") == 0) && node->child) {
  254. if (node->child->n_intrs < 3) {
  255. node->child->intrs = &gatwick_int_pool[count];
  256. count += 3;
  257. }
  258. node->child->n_intrs = 3;
  259. node->child->intrs[0].line = 15+irq_base;
  260. node->child->intrs[1].line = 4+irq_base;
  261. node->child->intrs[2].line = 5+irq_base;
  262. printk(KERN_INFO "irq: fixed SCC on gatwick"
  263. " (%d,%d,%d)\n",
  264. node->child->intrs[0].line,
  265. node->child->intrs[1].line,
  266. node->child->intrs[2].line);
  267. }
  268. /* Fix media-bay & left SWIM */
  269. if (strcasecmp(node->name, "media-bay") == 0) {
  270. struct device_node* ya_node;
  271. if (node->n_intrs == 0)
  272. node->intrs = &gatwick_int_pool[count++];
  273. node->n_intrs = 1;
  274. node->intrs[0].line = 29+irq_base;
  275. printk(KERN_INFO "irq: fixed media-bay on gatwick"
  276. " (%d)\n", node->intrs[0].line);
  277. ya_node = node->child;
  278. while(ya_node) {
  279. if (strcasecmp(ya_node->name, "floppy") == 0) {
  280. if (ya_node->n_intrs < 2) {
  281. ya_node->intrs = &gatwick_int_pool[count];
  282. count += 2;
  283. }
  284. ya_node->n_intrs = 2;
  285. ya_node->intrs[0].line = 19+irq_base;
  286. ya_node->intrs[1].line = 1+irq_base;
  287. printk(KERN_INFO "irq: fixed floppy on second controller (%d,%d)\n",
  288. ya_node->intrs[0].line, ya_node->intrs[1].line);
  289. }
  290. if (strcasecmp(ya_node->name, "ata4") == 0) {
  291. if (ya_node->n_intrs < 2) {
  292. ya_node->intrs = &gatwick_int_pool[count];
  293. count += 2;
  294. }
  295. ya_node->n_intrs = 2;
  296. ya_node->intrs[0].line = 14+irq_base;
  297. ya_node->intrs[1].line = 3+irq_base;
  298. printk(KERN_INFO "irq: fixed ide on second controller (%d,%d)\n",
  299. ya_node->intrs[0].line, ya_node->intrs[1].line);
  300. }
  301. ya_node = ya_node->sibling;
  302. }
  303. }
  304. }
  305. if (count > 10) {
  306. printk("WARNING !! Gatwick interrupt pool overflow\n");
  307. printk(" GATWICK_IRQ_POOL_SIZE = %d\n", GATWICK_IRQ_POOL_SIZE);
  308. printk(" requested = %d\n", count);
  309. }
  310. }
  311. /*
  312. * The PowerBook 3400/2400/3500 can have a combo ethernet/modem
  313. * card which includes an ohare chip that acts as a second interrupt
  314. * controller. If we find this second ohare, set it up and fix the
  315. * interrupt value in the device tree for the ethernet chip.
  316. */
  317. static void __init enable_second_ohare(struct device_node *np)
  318. {
  319. unsigned char bus, devfn;
  320. unsigned short cmd;
  321. struct device_node *ether;
  322. /* This code doesn't strictly belong here, it could be part of
  323. * either the PCI initialisation or the feature code. It's kept
  324. * here for historical reasons.
  325. */
  326. if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
  327. struct pci_controller* hose =
  328. pci_find_hose_for_OF_device(np);
  329. if (!hose) {
  330. printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
  331. return;
  332. }
  333. early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
  334. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  335. cmd &= ~PCI_COMMAND_IO;
  336. early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
  337. }
  338. /* Fix interrupt for the modem/ethernet combo controller. The number
  339. * in the device tree (27) is bogus (correct for the ethernet-only
  340. * board but not the combo ethernet/modem board).
  341. * The real interrupt is 28 on the second controller -> 28+32 = 60.
  342. */
  343. ether = of_find_node_by_name(NULL, "pci1011,14");
  344. if (ether && ether->n_intrs > 0) {
  345. ether->intrs[0].line = 60;
  346. printk(KERN_INFO "irq: Fixed ethernet IRQ to %d\n",
  347. ether->intrs[0].line);
  348. }
  349. of_node_put(ether);
  350. }
  351. #ifdef CONFIG_XMON
  352. static struct irqaction xmon_action = {
  353. .handler = xmon_irq,
  354. .flags = 0,
  355. .mask = CPU_MASK_NONE,
  356. .name = "NMI - XMON"
  357. };
  358. #endif
  359. static struct irqaction gatwick_cascade_action = {
  360. .handler = gatwick_action,
  361. .flags = IRQF_DISABLED,
  362. .mask = CPU_MASK_NONE,
  363. .name = "cascade",
  364. };
  365. static void __init pmac_pic_probe_oldstyle(void)
  366. {
  367. int i;
  368. struct device_node *master = NULL;
  369. struct device_node *slave = NULL;
  370. u8 __iomem *addr;
  371. struct resource r;
  372. /* Set our get_irq function */
  373. ppc_md.get_irq = pmac_get_irq;
  374. /*
  375. * Find the interrupt controller type & node
  376. */
  377. if ((master = of_find_node_by_name(NULL, "gc")) != NULL) {
  378. max_irqs = max_real_irqs = 32;
  379. level_mask[0] = GC_LEVEL_MASK;
  380. } else if ((master = of_find_node_by_name(NULL, "ohare")) != NULL) {
  381. max_irqs = max_real_irqs = 32;
  382. level_mask[0] = OHARE_LEVEL_MASK;
  383. /* We might have a second cascaded ohare */
  384. slave = of_find_node_by_name(NULL, "pci106b,7");
  385. if (slave) {
  386. max_irqs = 64;
  387. level_mask[1] = OHARE_LEVEL_MASK;
  388. enable_second_ohare(slave);
  389. }
  390. } else if ((master = of_find_node_by_name(NULL, "mac-io")) != NULL) {
  391. max_irqs = max_real_irqs = 64;
  392. level_mask[0] = HEATHROW_LEVEL_MASK;
  393. level_mask[1] = 0;
  394. /* We might have a second cascaded heathrow */
  395. slave = of_find_node_by_name(master, "mac-io");
  396. /* Check ordering of master & slave */
  397. if (device_is_compatible(master, "gatwick")) {
  398. struct device_node *tmp;
  399. BUG_ON(slave == NULL);
  400. tmp = master;
  401. master = slave;
  402. slave = tmp;
  403. }
  404. /* We found a slave */
  405. if (slave) {
  406. max_irqs = 128;
  407. level_mask[2] = HEATHROW_LEVEL_MASK;
  408. level_mask[3] = 0;
  409. pmac_fix_gatwick_interrupts(slave, max_real_irqs);
  410. }
  411. }
  412. BUG_ON(master == NULL);
  413. /* Mark level interrupts and set handlers */
  414. for (i = 0; i < max_irqs; i++) {
  415. int level = !!(level_mask[i >> 5] & (1UL << (i & 0x1f)));
  416. if (level)
  417. irq_desc[i].status |= IRQ_LEVEL;
  418. else
  419. irq_desc[i].status |= IRQ_DELAYED_DISABLE;
  420. set_irq_chip_and_handler(i, &pmac_pic, level ?
  421. handle_level_irq : handle_edge_irq);
  422. }
  423. /* Get addresses of first controller if we have a node for it */
  424. BUG_ON(of_address_to_resource(master, 0, &r));
  425. /* Map interrupts of primary controller */
  426. addr = (u8 __iomem *) ioremap(r.start, 0x40);
  427. i = 0;
  428. pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
  429. (addr + 0x20);
  430. if (max_real_irqs > 32)
  431. pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
  432. (addr + 0x10);
  433. of_node_put(master);
  434. printk(KERN_INFO "irq: Found primary Apple PIC %s for %d irqs\n",
  435. master->full_name, max_real_irqs);
  436. /* Map interrupts of cascaded controller */
  437. if (slave && !of_address_to_resource(slave, 0, &r)) {
  438. addr = (u8 __iomem *)ioremap(r.start, 0x40);
  439. pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
  440. (addr + 0x20);
  441. if (max_irqs > 64)
  442. pmac_irq_hw[i++] =
  443. (volatile struct pmac_irq_hw __iomem *)
  444. (addr + 0x10);
  445. pmac_irq_cascade = slave->intrs[0].line;
  446. printk(KERN_INFO "irq: Found slave Apple PIC %s for %d irqs"
  447. " cascade: %d\n", slave->full_name,
  448. max_irqs - max_real_irqs, pmac_irq_cascade);
  449. }
  450. of_node_put(slave);
  451. /* Disable all interrupts in all controllers */
  452. for (i = 0; i * 32 < max_irqs; ++i)
  453. out_le32(&pmac_irq_hw[i]->enable, 0);
  454. /* Hookup cascade irq */
  455. if (slave)
  456. setup_irq(pmac_irq_cascade, &gatwick_cascade_action);
  457. printk(KERN_INFO "irq: System has %d possible interrupts\n", max_irqs);
  458. #ifdef CONFIG_XMON
  459. setup_irq(20, &xmon_action);
  460. #endif
  461. }
  462. #endif /* CONFIG_PPC32 */
  463. static void pmac_u3_cascade(unsigned int irq, struct irq_desc *desc,
  464. struct pt_regs *regs)
  465. {
  466. struct mpic *mpic = desc->handler_data;
  467. unsigned int max = 100;
  468. while(max--) {
  469. int cascade_irq = mpic_get_one_irq(mpic, regs);
  470. if (max == 99)
  471. desc->chip->eoi(irq);
  472. if (irq < 0)
  473. break;
  474. generic_handle_irq(cascade_irq, regs);
  475. };
  476. }
  477. static void __init pmac_pic_setup_mpic_nmi(struct mpic *mpic)
  478. {
  479. #if defined(CONFIG_XMON) && defined(CONFIG_PPC32)
  480. struct device_node* pswitch;
  481. int nmi_irq;
  482. pswitch = of_find_node_by_name(NULL, "programmer-switch");
  483. if (pswitch && pswitch->n_intrs) {
  484. nmi_irq = pswitch->intrs[0].line;
  485. mpic_irq_set_priority(nmi_irq, 9);
  486. setup_irq(nmi_irq, &xmon_action);
  487. }
  488. of_node_put(pswitch);
  489. #endif /* defined(CONFIG_XMON) && defined(CONFIG_PPC32) */
  490. }
  491. static struct mpic * __init pmac_setup_one_mpic(struct device_node *np,
  492. int master)
  493. {
  494. unsigned char senses[128];
  495. int offset = master ? 0 : 128;
  496. int count = master ? 128 : 124;
  497. const char *name = master ? " MPIC 1 " : " MPIC 2 ";
  498. struct resource r;
  499. struct mpic *mpic;
  500. unsigned int flags = master ? MPIC_PRIMARY : 0;
  501. int rc;
  502. rc = of_address_to_resource(np, 0, &r);
  503. if (rc)
  504. return NULL;
  505. pmac_call_feature(PMAC_FTR_ENABLE_MPIC, np, 0, 0);
  506. prom_get_irq_senses(senses, offset, offset + count);
  507. flags |= MPIC_WANTS_RESET;
  508. if (get_property(np, "big-endian", NULL))
  509. flags |= MPIC_BIG_ENDIAN;
  510. /* Primary Big Endian means HT interrupts. This is quite dodgy
  511. * but works until I find a better way
  512. */
  513. if (master && (flags & MPIC_BIG_ENDIAN))
  514. flags |= MPIC_BROKEN_U3;
  515. mpic = mpic_alloc(r.start, flags, 0, offset, count, master ? 252 : 0,
  516. senses, count, name);
  517. if (mpic == NULL)
  518. return NULL;
  519. mpic_init(mpic);
  520. return mpic;
  521. }
  522. static int __init pmac_pic_probe_mpic(void)
  523. {
  524. struct mpic *mpic1, *mpic2;
  525. struct device_node *np, *master = NULL, *slave = NULL;
  526. /* We can have up to 2 MPICs cascaded */
  527. for (np = NULL; (np = of_find_node_by_type(np, "open-pic"))
  528. != NULL;) {
  529. if (master == NULL &&
  530. get_property(np, "interrupts", NULL) == NULL)
  531. master = of_node_get(np);
  532. else if (slave == NULL)
  533. slave = of_node_get(np);
  534. if (master && slave)
  535. break;
  536. }
  537. /* Check for bogus setups */
  538. if (master == NULL && slave != NULL) {
  539. master = slave;
  540. slave = NULL;
  541. }
  542. /* Not found, default to good old pmac pic */
  543. if (master == NULL)
  544. return -ENODEV;
  545. /* Set master handler */
  546. ppc_md.get_irq = mpic_get_irq;
  547. /* Setup master */
  548. mpic1 = pmac_setup_one_mpic(master, 1);
  549. BUG_ON(mpic1 == NULL);
  550. /* Install NMI if any */
  551. pmac_pic_setup_mpic_nmi(mpic1);
  552. of_node_put(master);
  553. /* No slave, let's go out */
  554. if (slave == NULL || slave->n_intrs < 1)
  555. return 0;
  556. mpic2 = pmac_setup_one_mpic(slave, 0);
  557. if (mpic2 == NULL) {
  558. printk(KERN_ERR "Failed to setup slave MPIC\n");
  559. of_node_put(slave);
  560. return 0;
  561. }
  562. set_irq_data(slave->intrs[0].line, mpic2);
  563. set_irq_chained_handler(slave->intrs[0].line, pmac_u3_cascade);
  564. of_node_put(slave);
  565. return 0;
  566. }
  567. void __init pmac_pic_init(void)
  568. {
  569. /* We first try to detect Apple's new Core99 chipset, since mac-io
  570. * is quite different on those machines and contains an IBM MPIC2.
  571. */
  572. if (pmac_pic_probe_mpic() == 0)
  573. return;
  574. #ifdef CONFIG_PPC32
  575. pmac_pic_probe_oldstyle();
  576. #endif
  577. }
  578. #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
  579. /*
  580. * These procedures are used in implementing sleep on the powerbooks.
  581. * sleep_save_intrs() saves the states of all interrupt enables
  582. * and disables all interrupts except for the nominated one.
  583. * sleep_restore_intrs() restores the states of all interrupt enables.
  584. */
  585. unsigned long sleep_save_mask[2];
  586. /* This used to be passed by the PMU driver but that link got
  587. * broken with the new driver model. We use this tweak for now...
  588. */
  589. static int pmacpic_find_viaint(void)
  590. {
  591. int viaint = -1;
  592. #ifdef CONFIG_ADB_PMU
  593. struct device_node *np;
  594. if (pmu_get_model() != PMU_OHARE_BASED)
  595. goto not_found;
  596. np = of_find_node_by_name(NULL, "via-pmu");
  597. if (np == NULL)
  598. goto not_found;
  599. viaint = np->intrs[0].line;
  600. #endif /* CONFIG_ADB_PMU */
  601. not_found:
  602. return viaint;
  603. }
  604. static int pmacpic_suspend(struct sys_device *sysdev, pm_message_t state)
  605. {
  606. int viaint = pmacpic_find_viaint();
  607. sleep_save_mask[0] = ppc_cached_irq_mask[0];
  608. sleep_save_mask[1] = ppc_cached_irq_mask[1];
  609. ppc_cached_irq_mask[0] = 0;
  610. ppc_cached_irq_mask[1] = 0;
  611. if (viaint > 0)
  612. set_bit(viaint, ppc_cached_irq_mask);
  613. out_le32(&pmac_irq_hw[0]->enable, ppc_cached_irq_mask[0]);
  614. if (max_real_irqs > 32)
  615. out_le32(&pmac_irq_hw[1]->enable, ppc_cached_irq_mask[1]);
  616. (void)in_le32(&pmac_irq_hw[0]->event);
  617. /* make sure mask gets to controller before we return to caller */
  618. mb();
  619. (void)in_le32(&pmac_irq_hw[0]->enable);
  620. return 0;
  621. }
  622. static int pmacpic_resume(struct sys_device *sysdev)
  623. {
  624. int i;
  625. out_le32(&pmac_irq_hw[0]->enable, 0);
  626. if (max_real_irqs > 32)
  627. out_le32(&pmac_irq_hw[1]->enable, 0);
  628. mb();
  629. for (i = 0; i < max_real_irqs; ++i)
  630. if (test_bit(i, sleep_save_mask))
  631. pmac_unmask_irq(i);
  632. return 0;
  633. }
  634. #endif /* CONFIG_PM && CONFIG_PPC32 */
  635. static struct sysdev_class pmacpic_sysclass = {
  636. set_kset_name("pmac_pic"),
  637. };
  638. static struct sys_device device_pmacpic = {
  639. .id = 0,
  640. .cls = &pmacpic_sysclass,
  641. };
  642. static struct sysdev_driver driver_pmacpic = {
  643. #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
  644. .suspend = &pmacpic_suspend,
  645. .resume = &pmacpic_resume,
  646. #endif /* CONFIG_PM && CONFIG_PPC32 */
  647. };
  648. static int __init init_pmacpic_sysfs(void)
  649. {
  650. #ifdef CONFIG_PPC32
  651. if (max_irqs == 0)
  652. return -ENODEV;
  653. #endif
  654. printk(KERN_DEBUG "Registering pmac pic with sysfs...\n");
  655. sysdev_class_register(&pmacpic_sysclass);
  656. sysdev_register(&device_pmacpic);
  657. sysdev_driver_register(&pmacpic_sysclass, &driver_pmacpic);
  658. return 0;
  659. }
  660. subsys_initcall(init_pmacpic_sysfs);