irq-metag-ext.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * Meta External interrupt code.
  3. *
  4. * Copyright (C) 2005-2012 Imagination Technologies Ltd.
  5. *
  6. * External interrupts on Meta are configured at two-levels, in the CPU core and
  7. * in the external trigger block. Interrupts from SoC peripherals are
  8. * multiplexed onto a single Meta CPU "trigger" - traditionally it has always
  9. * been trigger 2 (TR2). For info on how de-multiplexing happens check out
  10. * meta_intc_irq_demux().
  11. */
  12. #include <linux/interrupt.h>
  13. #include <linux/irqchip/metag-ext.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/slab.h>
  18. #include <linux/syscore_ops.h>
  19. #include <asm/irq.h>
  20. #include <asm/hwthread.h>
  21. #define HWSTAT_STRIDE 8
  22. #define HWVEC_BLK_STRIDE 0x1000
  23. /**
  24. * struct meta_intc_priv - private meta external interrupt data
  25. * @nr_banks: Number of interrupt banks
  26. * @domain: IRQ domain for all banks of external IRQs
  27. * @unmasked: Record of unmasked IRQs
  28. * @levels_altered: Record of altered level bits
  29. */
  30. struct meta_intc_priv {
  31. unsigned int nr_banks;
  32. struct irq_domain *domain;
  33. unsigned long unmasked[4];
  34. #ifdef CONFIG_METAG_SUSPEND_MEM
  35. unsigned long levels_altered[4];
  36. #endif
  37. };
  38. /* Private data for the one and only external interrupt controller */
  39. static struct meta_intc_priv meta_intc_priv;
  40. /**
  41. * meta_intc_offset() - Get the offset into the bank of a hardware IRQ number
  42. * @hw: Hardware IRQ number (within external trigger block)
  43. *
  44. * Returns: Bit offset into the IRQ's bank registers
  45. */
  46. static unsigned int meta_intc_offset(irq_hw_number_t hw)
  47. {
  48. return hw & 0x1f;
  49. }
  50. /**
  51. * meta_intc_bank() - Get the bank number of a hardware IRQ number
  52. * @hw: Hardware IRQ number (within external trigger block)
  53. *
  54. * Returns: Bank number indicating which register the IRQ's bits are
  55. */
  56. static unsigned int meta_intc_bank(irq_hw_number_t hw)
  57. {
  58. return hw >> 5;
  59. }
  60. /**
  61. * meta_intc_stat_addr() - Get the address of a HWSTATEXT register
  62. * @hw: Hardware IRQ number (within external trigger block)
  63. *
  64. * Returns: Address of a HWSTATEXT register containing the status bit for
  65. * the specified hardware IRQ number
  66. */
  67. static void __iomem *meta_intc_stat_addr(irq_hw_number_t hw)
  68. {
  69. return (void __iomem *)(HWSTATEXT +
  70. HWSTAT_STRIDE * meta_intc_bank(hw));
  71. }
  72. /**
  73. * meta_intc_level_addr() - Get the address of a HWLEVELEXT register
  74. * @hw: Hardware IRQ number (within external trigger block)
  75. *
  76. * Returns: Address of a HWLEVELEXT register containing the sense bit for
  77. * the specified hardware IRQ number
  78. */
  79. static void __iomem *meta_intc_level_addr(irq_hw_number_t hw)
  80. {
  81. return (void __iomem *)(HWLEVELEXT +
  82. HWSTAT_STRIDE * meta_intc_bank(hw));
  83. }
  84. /**
  85. * meta_intc_mask_addr() - Get the address of a HWMASKEXT register
  86. * @hw: Hardware IRQ number (within external trigger block)
  87. *
  88. * Returns: Address of a HWMASKEXT register containing the mask bit for the
  89. * specified hardware IRQ number
  90. */
  91. static void __iomem *meta_intc_mask_addr(irq_hw_number_t hw)
  92. {
  93. return (void __iomem *)(HWMASKEXT +
  94. HWSTAT_STRIDE * meta_intc_bank(hw));
  95. }
  96. /**
  97. * meta_intc_vec_addr() - Get the vector address of a hardware interrupt
  98. * @hw: Hardware IRQ number (within external trigger block)
  99. *
  100. * Returns: Address of a HWVECEXT register controlling the core trigger to
  101. * vector the IRQ onto
  102. */
  103. static inline void __iomem *meta_intc_vec_addr(irq_hw_number_t hw)
  104. {
  105. return (void __iomem *)(HWVEC0EXT +
  106. HWVEC_BLK_STRIDE * meta_intc_bank(hw) +
  107. HWVECnEXT_STRIDE * meta_intc_offset(hw));
  108. }
  109. /**
  110. * meta_intc_startup_irq() - set up an external irq
  111. * @data: data for the external irq to start up
  112. *
  113. * Multiplex interrupts for irq onto TR2. Clear any pending interrupts and
  114. * unmask irq, both using the appropriate callbacks.
  115. */
  116. static unsigned int meta_intc_startup_irq(struct irq_data *data)
  117. {
  118. irq_hw_number_t hw = data->hwirq;
  119. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  120. int thread = hard_processor_id();
  121. /* Perform any necessary acking. */
  122. if (data->chip->irq_ack)
  123. data->chip->irq_ack(data);
  124. /* Wire up this interrupt to the core with HWVECxEXT. */
  125. metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR2(thread)), vec_addr);
  126. /* Perform any necessary unmasking. */
  127. data->chip->irq_unmask(data);
  128. return 0;
  129. }
  130. /**
  131. * meta_intc_shutdown_irq() - turn off an external irq
  132. * @data: data for the external irq to turn off
  133. *
  134. * Mask irq using the appropriate callback and stop muxing it onto TR2.
  135. */
  136. static void meta_intc_shutdown_irq(struct irq_data *data)
  137. {
  138. irq_hw_number_t hw = data->hwirq;
  139. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  140. /* Mask the IRQ */
  141. data->chip->irq_mask(data);
  142. /*
  143. * Disable the IRQ at the core by removing the interrupt from
  144. * the HW vector mapping.
  145. */
  146. metag_out32(0, vec_addr);
  147. }
  148. /**
  149. * meta_intc_ack_irq() - acknowledge an external irq
  150. * @data: data for the external irq to ack
  151. *
  152. * Clear down an edge interrupt in the status register.
  153. */
  154. static void meta_intc_ack_irq(struct irq_data *data)
  155. {
  156. irq_hw_number_t hw = data->hwirq;
  157. unsigned int bit = 1 << meta_intc_offset(hw);
  158. void __iomem *stat_addr = meta_intc_stat_addr(hw);
  159. /* Ack the int, if it is still 'on'.
  160. * NOTE - this only works for edge triggered interrupts.
  161. */
  162. if (metag_in32(stat_addr) & bit)
  163. metag_out32(bit, stat_addr);
  164. }
  165. /**
  166. * record_irq_is_masked() - record the IRQ masked so it doesn't get handled
  167. * @data: data for the external irq to record
  168. *
  169. * This should get called whenever an external IRQ is masked (by whichever
  170. * callback is used). It records the IRQ masked so that it doesn't get handled
  171. * if it still shows up in the status register.
  172. */
  173. static void record_irq_is_masked(struct irq_data *data)
  174. {
  175. struct meta_intc_priv *priv = &meta_intc_priv;
  176. irq_hw_number_t hw = data->hwirq;
  177. clear_bit(meta_intc_offset(hw), &priv->unmasked[meta_intc_bank(hw)]);
  178. }
  179. /**
  180. * record_irq_is_unmasked() - record the IRQ unmasked so it can be handled
  181. * @data: data for the external irq to record
  182. *
  183. * This should get called whenever an external IRQ is unmasked (by whichever
  184. * callback is used). It records the IRQ unmasked so that it gets handled if it
  185. * shows up in the status register.
  186. */
  187. static void record_irq_is_unmasked(struct irq_data *data)
  188. {
  189. struct meta_intc_priv *priv = &meta_intc_priv;
  190. irq_hw_number_t hw = data->hwirq;
  191. set_bit(meta_intc_offset(hw), &priv->unmasked[meta_intc_bank(hw)]);
  192. }
  193. /*
  194. * For use by wrapper IRQ drivers
  195. */
  196. /**
  197. * meta_intc_mask_irq_simple() - minimal mask used by wrapper IRQ drivers
  198. * @data: data for the external irq being masked
  199. *
  200. * This should be called by any wrapper IRQ driver mask functions. it doesn't do
  201. * any masking but records the IRQ as masked so that the core code knows the
  202. * mask has taken place. It is the callers responsibility to ensure that the IRQ
  203. * won't trigger an interrupt to the core.
  204. */
  205. void meta_intc_mask_irq_simple(struct irq_data *data)
  206. {
  207. record_irq_is_masked(data);
  208. }
  209. /**
  210. * meta_intc_unmask_irq_simple() - minimal unmask used by wrapper IRQ drivers
  211. * @data: data for the external irq being unmasked
  212. *
  213. * This should be called by any wrapper IRQ driver unmask functions. it doesn't
  214. * do any unmasking but records the IRQ as unmasked so that the core code knows
  215. * the unmask has taken place. It is the callers responsibility to ensure that
  216. * the IRQ can now trigger an interrupt to the core.
  217. */
  218. void meta_intc_unmask_irq_simple(struct irq_data *data)
  219. {
  220. record_irq_is_unmasked(data);
  221. }
  222. /**
  223. * meta_intc_mask_irq() - mask an external irq using HWMASKEXT
  224. * @data: data for the external irq to mask
  225. *
  226. * This is a default implementation of a mask function which makes use of the
  227. * HWMASKEXT registers available in newer versions.
  228. *
  229. * Earlier versions without these registers should use SoC level IRQ masking
  230. * which call the meta_intc_*_simple() functions above, or if that isn't
  231. * available should use the fallback meta_intc_*_nomask() functions below.
  232. */
  233. static void meta_intc_mask_irq(struct irq_data *data)
  234. {
  235. irq_hw_number_t hw = data->hwirq;
  236. unsigned int bit = 1 << meta_intc_offset(hw);
  237. void __iomem *mask_addr = meta_intc_mask_addr(hw);
  238. unsigned long flags;
  239. record_irq_is_masked(data);
  240. /* update the interrupt mask */
  241. __global_lock2(flags);
  242. metag_out32(metag_in32(mask_addr) & ~bit, mask_addr);
  243. __global_unlock2(flags);
  244. }
  245. /**
  246. * meta_intc_unmask_irq() - unmask an external irq using HWMASKEXT
  247. * @data: data for the external irq to unmask
  248. *
  249. * This is a default implementation of an unmask function which makes use of the
  250. * HWMASKEXT registers available on new versions. It should be paired with
  251. * meta_intc_mask_irq() above.
  252. */
  253. static void meta_intc_unmask_irq(struct irq_data *data)
  254. {
  255. irq_hw_number_t hw = data->hwirq;
  256. unsigned int bit = 1 << meta_intc_offset(hw);
  257. void __iomem *mask_addr = meta_intc_mask_addr(hw);
  258. unsigned long flags;
  259. record_irq_is_unmasked(data);
  260. /* update the interrupt mask */
  261. __global_lock2(flags);
  262. metag_out32(metag_in32(mask_addr) | bit, mask_addr);
  263. __global_unlock2(flags);
  264. }
  265. /**
  266. * meta_intc_mask_irq_nomask() - mask an external irq by unvectoring
  267. * @data: data for the external irq to mask
  268. *
  269. * This is the version of the mask function for older versions which don't have
  270. * HWMASKEXT registers, or a SoC level means of masking IRQs. Instead the IRQ is
  271. * unvectored from the core and retriggered if necessary later.
  272. */
  273. static void meta_intc_mask_irq_nomask(struct irq_data *data)
  274. {
  275. irq_hw_number_t hw = data->hwirq;
  276. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  277. record_irq_is_masked(data);
  278. /* there is no interrupt mask, so unvector the interrupt */
  279. metag_out32(0, vec_addr);
  280. }
  281. /**
  282. * meta_intc_unmask_edge_irq_nomask() - unmask an edge irq by revectoring
  283. * @data: data for the external irq to unmask
  284. *
  285. * This is the version of the unmask function for older versions which don't
  286. * have HWMASKEXT registers, or a SoC level means of masking IRQs. Instead the
  287. * IRQ is revectored back to the core and retriggered if necessary.
  288. *
  289. * The retriggering done by this function is specific to edge interrupts.
  290. */
  291. static void meta_intc_unmask_edge_irq_nomask(struct irq_data *data)
  292. {
  293. irq_hw_number_t hw = data->hwirq;
  294. unsigned int bit = 1 << meta_intc_offset(hw);
  295. void __iomem *stat_addr = meta_intc_stat_addr(hw);
  296. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  297. unsigned int thread = hard_processor_id();
  298. record_irq_is_unmasked(data);
  299. /* there is no interrupt mask, so revector the interrupt */
  300. metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR2(thread)), vec_addr);
  301. /*
  302. * Re-trigger interrupt
  303. *
  304. * Writing a 1 toggles, and a 0->1 transition triggers. We only
  305. * retrigger if the status bit is already set, which means we
  306. * need to clear it first. Retriggering is fundamentally racy
  307. * because if the interrupt fires again after we clear it we
  308. * could end up clearing it again and the interrupt handler
  309. * thinking it hasn't fired. Therefore we need to keep trying to
  310. * retrigger until the bit is set.
  311. */
  312. if (metag_in32(stat_addr) & bit) {
  313. metag_out32(bit, stat_addr);
  314. while (!(metag_in32(stat_addr) & bit))
  315. metag_out32(bit, stat_addr);
  316. }
  317. }
  318. /**
  319. * meta_intc_unmask_level_irq_nomask() - unmask a level irq by revectoring
  320. * @data: data for the external irq to unmask
  321. *
  322. * This is the version of the unmask function for older versions which don't
  323. * have HWMASKEXT registers, or a SoC level means of masking IRQs. Instead the
  324. * IRQ is revectored back to the core and retriggered if necessary.
  325. *
  326. * The retriggering done by this function is specific to level interrupts.
  327. */
  328. static void meta_intc_unmask_level_irq_nomask(struct irq_data *data)
  329. {
  330. irq_hw_number_t hw = data->hwirq;
  331. unsigned int bit = 1 << meta_intc_offset(hw);
  332. void __iomem *stat_addr = meta_intc_stat_addr(hw);
  333. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  334. unsigned int thread = hard_processor_id();
  335. record_irq_is_unmasked(data);
  336. /* there is no interrupt mask, so revector the interrupt */
  337. metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR2(thread)), vec_addr);
  338. /* Re-trigger interrupt */
  339. /* Writing a 1 triggers interrupt */
  340. if (metag_in32(stat_addr) & bit)
  341. metag_out32(bit, stat_addr);
  342. }
  343. /**
  344. * meta_intc_irq_set_type() - set the type of an external irq
  345. * @data: data for the external irq to set the type of
  346. * @flow_type: new irq flow type
  347. *
  348. * Set the flow type of an external interrupt. This updates the irq chip and irq
  349. * handler depending on whether the irq is edge or level sensitive (the polarity
  350. * is ignored), and also sets up the bit in HWLEVELEXT so the hardware knows
  351. * when to trigger.
  352. */
  353. static int meta_intc_irq_set_type(struct irq_data *data, unsigned int flow_type)
  354. {
  355. #ifdef CONFIG_METAG_SUSPEND_MEM
  356. struct meta_intc_priv *priv = &meta_intc_priv;
  357. #endif
  358. irq_hw_number_t hw = data->hwirq;
  359. unsigned int bit = 1 << meta_intc_offset(hw);
  360. void __iomem *level_addr = meta_intc_level_addr(hw);
  361. unsigned long flags;
  362. unsigned int level;
  363. /* update the chip/handler */
  364. if (flow_type & IRQ_TYPE_LEVEL_MASK)
  365. irq_set_chip_handler_name_locked(data, &meta_intc_level_chip,
  366. handle_level_irq, NULL);
  367. else
  368. irq_set_chip_handler_name_locked(data, &meta_intc_edge_chip,
  369. handle_edge_irq, NULL);
  370. /* and clear/set the bit in HWLEVELEXT */
  371. __global_lock2(flags);
  372. level = metag_in32(level_addr);
  373. if (flow_type & IRQ_TYPE_LEVEL_MASK)
  374. level |= bit;
  375. else
  376. level &= ~bit;
  377. metag_out32(level, level_addr);
  378. #ifdef CONFIG_METAG_SUSPEND_MEM
  379. priv->levels_altered[meta_intc_bank(hw)] |= bit;
  380. #endif
  381. __global_unlock2(flags);
  382. return 0;
  383. }
  384. /**
  385. * meta_intc_irq_demux() - external irq de-multiplexer
  386. * @irq: the virtual interrupt number
  387. * @desc: the interrupt description structure for this irq
  388. *
  389. * The cpu receives an interrupt on TR2 when a SoC interrupt has occurred. It is
  390. * this function's job to demux this irq and figure out exactly which external
  391. * irq needs servicing.
  392. *
  393. * Whilst using TR2 to detect external interrupts is a software convention it is
  394. * (hopefully) unlikely to change.
  395. */
  396. static void meta_intc_irq_demux(struct irq_desc *desc)
  397. {
  398. struct meta_intc_priv *priv = &meta_intc_priv;
  399. irq_hw_number_t hw;
  400. unsigned int bank, irq_no, status;
  401. void __iomem *stat_addr = meta_intc_stat_addr(0);
  402. /*
  403. * Locate which interrupt has caused our handler to run.
  404. */
  405. for (bank = 0; bank < priv->nr_banks; ++bank) {
  406. /* Which interrupts are currently pending in this bank? */
  407. recalculate:
  408. status = metag_in32(stat_addr) & priv->unmasked[bank];
  409. for (hw = bank*32; status; status >>= 1, ++hw) {
  410. if (status & 0x1) {
  411. /*
  412. * Map the hardware IRQ number to a virtual
  413. * Linux IRQ number.
  414. */
  415. irq_no = irq_linear_revmap(priv->domain, hw);
  416. /*
  417. * Only fire off external interrupts that are
  418. * registered to be handled by the kernel.
  419. * Other external interrupts are probably being
  420. * handled by other Meta hardware threads.
  421. */
  422. generic_handle_irq(irq_no);
  423. /*
  424. * The handler may have re-enabled interrupts
  425. * which could have caused a nested invocation
  426. * of this code and make the copy of the
  427. * status register we are using invalid.
  428. */
  429. goto recalculate;
  430. }
  431. }
  432. stat_addr += HWSTAT_STRIDE;
  433. }
  434. }
  435. #ifdef CONFIG_SMP
  436. /**
  437. * meta_intc_set_affinity() - set the affinity for an interrupt
  438. * @data: data for the external irq to set the affinity of
  439. * @cpumask: cpu mask representing cpus which can handle the interrupt
  440. * @force: whether to force (ignored)
  441. *
  442. * Revector the specified external irq onto a specific cpu's TR2 trigger, so
  443. * that that cpu tends to be the one who handles it.
  444. */
  445. static int meta_intc_set_affinity(struct irq_data *data,
  446. const struct cpumask *cpumask, bool force)
  447. {
  448. irq_hw_number_t hw = data->hwirq;
  449. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  450. unsigned int cpu, thread;
  451. /*
  452. * Wire up this interrupt from HWVECxEXT to the Meta core.
  453. *
  454. * Note that we can't wire up HWVECxEXT to interrupt more than
  455. * one cpu (the interrupt code doesn't support it), so we just
  456. * pick the first cpu we find in 'cpumask'.
  457. */
  458. cpu = cpumask_any_and(cpumask, cpu_online_mask);
  459. thread = cpu_2_hwthread_id[cpu];
  460. metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR2(thread)), vec_addr);
  461. return 0;
  462. }
  463. #else
  464. #define meta_intc_set_affinity NULL
  465. #endif
  466. #ifdef CONFIG_PM_SLEEP
  467. #define META_INTC_CHIP_FLAGS (IRQCHIP_MASK_ON_SUSPEND \
  468. | IRQCHIP_SKIP_SET_WAKE)
  469. #else
  470. #define META_INTC_CHIP_FLAGS 0
  471. #endif
  472. /* public edge/level irq chips which SoCs can override */
  473. struct irq_chip meta_intc_edge_chip = {
  474. .irq_startup = meta_intc_startup_irq,
  475. .irq_shutdown = meta_intc_shutdown_irq,
  476. .irq_ack = meta_intc_ack_irq,
  477. .irq_mask = meta_intc_mask_irq,
  478. .irq_unmask = meta_intc_unmask_irq,
  479. .irq_set_type = meta_intc_irq_set_type,
  480. .irq_set_affinity = meta_intc_set_affinity,
  481. .flags = META_INTC_CHIP_FLAGS,
  482. };
  483. struct irq_chip meta_intc_level_chip = {
  484. .irq_startup = meta_intc_startup_irq,
  485. .irq_shutdown = meta_intc_shutdown_irq,
  486. .irq_set_type = meta_intc_irq_set_type,
  487. .irq_mask = meta_intc_mask_irq,
  488. .irq_unmask = meta_intc_unmask_irq,
  489. .irq_set_affinity = meta_intc_set_affinity,
  490. .flags = META_INTC_CHIP_FLAGS,
  491. };
  492. /**
  493. * meta_intc_map() - map an external irq
  494. * @d: irq domain of external trigger block
  495. * @irq: virtual irq number
  496. * @hw: hardware irq number within external trigger block
  497. *
  498. * This sets up a virtual irq for a specified hardware interrupt. The irq chip
  499. * and handler is configured, using the HWLEVELEXT registers to determine
  500. * edge/level flow type. These registers will have been set when the irq type is
  501. * set (or set to a default at init time).
  502. */
  503. static int meta_intc_map(struct irq_domain *d, unsigned int irq,
  504. irq_hw_number_t hw)
  505. {
  506. unsigned int bit = 1 << meta_intc_offset(hw);
  507. void __iomem *level_addr = meta_intc_level_addr(hw);
  508. /* Go by the current sense in the HWLEVELEXT register */
  509. if (metag_in32(level_addr) & bit)
  510. irq_set_chip_and_handler(irq, &meta_intc_level_chip,
  511. handle_level_irq);
  512. else
  513. irq_set_chip_and_handler(irq, &meta_intc_edge_chip,
  514. handle_edge_irq);
  515. return 0;
  516. }
  517. static const struct irq_domain_ops meta_intc_domain_ops = {
  518. .map = meta_intc_map,
  519. .xlate = irq_domain_xlate_twocell,
  520. };
  521. #ifdef CONFIG_METAG_SUSPEND_MEM
  522. /**
  523. * struct meta_intc_context - suspend context
  524. * @levels: State of HWLEVELEXT registers
  525. * @masks: State of HWMASKEXT registers
  526. * @vectors: State of HWVECEXT registers
  527. * @txvecint: State of TxVECINT registers
  528. *
  529. * This structure stores the IRQ state across suspend.
  530. */
  531. struct meta_intc_context {
  532. u32 levels[4];
  533. u32 masks[4];
  534. u8 vectors[4*32];
  535. u8 txvecint[4][4];
  536. };
  537. /* suspend context */
  538. static struct meta_intc_context *meta_intc_context;
  539. /**
  540. * meta_intc_suspend() - store irq state
  541. *
  542. * To avoid interfering with other threads we only save the IRQ state of IRQs in
  543. * use by Linux.
  544. */
  545. static int meta_intc_suspend(void)
  546. {
  547. struct meta_intc_priv *priv = &meta_intc_priv;
  548. int i, j;
  549. irq_hw_number_t hw;
  550. unsigned int bank;
  551. unsigned long flags;
  552. struct meta_intc_context *context;
  553. void __iomem *level_addr, *mask_addr, *vec_addr;
  554. u32 mask, bit;
  555. context = kzalloc(sizeof(*context), GFP_ATOMIC);
  556. if (!context)
  557. return -ENOMEM;
  558. hw = 0;
  559. level_addr = meta_intc_level_addr(0);
  560. mask_addr = meta_intc_mask_addr(0);
  561. for (bank = 0; bank < priv->nr_banks; ++bank) {
  562. vec_addr = meta_intc_vec_addr(hw);
  563. /* create mask of interrupts in use */
  564. mask = 0;
  565. for (bit = 1; bit; bit <<= 1) {
  566. i = irq_linear_revmap(priv->domain, hw);
  567. /* save mapped irqs which are enabled or have actions */
  568. if (i && (!irqd_irq_disabled(irq_get_irq_data(i)) ||
  569. irq_has_action(i))) {
  570. mask |= bit;
  571. /* save trigger vector */
  572. context->vectors[hw] = metag_in32(vec_addr);
  573. }
  574. ++hw;
  575. vec_addr += HWVECnEXT_STRIDE;
  576. }
  577. /* save level state if any IRQ levels altered */
  578. if (priv->levels_altered[bank])
  579. context->levels[bank] = metag_in32(level_addr);
  580. /* save mask state if any IRQs in use */
  581. if (mask)
  582. context->masks[bank] = metag_in32(mask_addr);
  583. level_addr += HWSTAT_STRIDE;
  584. mask_addr += HWSTAT_STRIDE;
  585. }
  586. /* save trigger matrixing */
  587. __global_lock2(flags);
  588. for (i = 0; i < 4; ++i)
  589. for (j = 0; j < 4; ++j)
  590. context->txvecint[i][j] = metag_in32(T0VECINT_BHALT +
  591. TnVECINT_STRIDE*i +
  592. 8*j);
  593. __global_unlock2(flags);
  594. meta_intc_context = context;
  595. return 0;
  596. }
  597. /**
  598. * meta_intc_resume() - restore saved irq state
  599. *
  600. * Restore the saved IRQ state and drop it.
  601. */
  602. static void meta_intc_resume(void)
  603. {
  604. struct meta_intc_priv *priv = &meta_intc_priv;
  605. int i, j;
  606. irq_hw_number_t hw;
  607. unsigned int bank;
  608. unsigned long flags;
  609. struct meta_intc_context *context = meta_intc_context;
  610. void __iomem *level_addr, *mask_addr, *vec_addr;
  611. u32 mask, bit, tmp;
  612. meta_intc_context = NULL;
  613. hw = 0;
  614. level_addr = meta_intc_level_addr(0);
  615. mask_addr = meta_intc_mask_addr(0);
  616. for (bank = 0; bank < priv->nr_banks; ++bank) {
  617. vec_addr = meta_intc_vec_addr(hw);
  618. /* create mask of interrupts in use */
  619. mask = 0;
  620. for (bit = 1; bit; bit <<= 1) {
  621. i = irq_linear_revmap(priv->domain, hw);
  622. /* restore mapped irqs, enabled or with actions */
  623. if (i && (!irqd_irq_disabled(irq_get_irq_data(i)) ||
  624. irq_has_action(i))) {
  625. mask |= bit;
  626. /* restore trigger vector */
  627. metag_out32(context->vectors[hw], vec_addr);
  628. }
  629. ++hw;
  630. vec_addr += HWVECnEXT_STRIDE;
  631. }
  632. if (mask) {
  633. /* restore mask state */
  634. __global_lock2(flags);
  635. tmp = metag_in32(mask_addr);
  636. tmp = (tmp & ~mask) | (context->masks[bank] & mask);
  637. metag_out32(tmp, mask_addr);
  638. __global_unlock2(flags);
  639. }
  640. mask = priv->levels_altered[bank];
  641. if (mask) {
  642. /* restore level state */
  643. __global_lock2(flags);
  644. tmp = metag_in32(level_addr);
  645. tmp = (tmp & ~mask) | (context->levels[bank] & mask);
  646. metag_out32(tmp, level_addr);
  647. __global_unlock2(flags);
  648. }
  649. level_addr += HWSTAT_STRIDE;
  650. mask_addr += HWSTAT_STRIDE;
  651. }
  652. /* restore trigger matrixing */
  653. __global_lock2(flags);
  654. for (i = 0; i < 4; ++i) {
  655. for (j = 0; j < 4; ++j) {
  656. metag_out32(context->txvecint[i][j],
  657. T0VECINT_BHALT +
  658. TnVECINT_STRIDE*i +
  659. 8*j);
  660. }
  661. }
  662. __global_unlock2(flags);
  663. kfree(context);
  664. }
  665. static struct syscore_ops meta_intc_syscore_ops = {
  666. .suspend = meta_intc_suspend,
  667. .resume = meta_intc_resume,
  668. };
  669. static void __init meta_intc_init_syscore_ops(struct meta_intc_priv *priv)
  670. {
  671. register_syscore_ops(&meta_intc_syscore_ops);
  672. }
  673. #else
  674. #define meta_intc_init_syscore_ops(priv) do {} while (0)
  675. #endif
  676. /**
  677. * meta_intc_init_cpu() - register with a Meta cpu
  678. * @priv: private interrupt controller data
  679. * @cpu: the CPU to register on
  680. *
  681. * Configure @cpu's TR2 irq so that we can demux external irqs.
  682. */
  683. static void __init meta_intc_init_cpu(struct meta_intc_priv *priv, int cpu)
  684. {
  685. unsigned int thread = cpu_2_hwthread_id[cpu];
  686. unsigned int signum = TBID_SIGNUM_TR2(thread);
  687. int irq = tbisig_map(signum);
  688. /* Register the multiplexed IRQ handler */
  689. irq_set_chained_handler(irq, meta_intc_irq_demux);
  690. irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
  691. }
  692. /**
  693. * meta_intc_no_mask() - indicate lack of HWMASKEXT registers
  694. *
  695. * Called from SoC code (or init code below) to dynamically indicate the lack of
  696. * HWMASKEXT registers (for example depending on some SoC revision register).
  697. * This alters the irq mask and unmask callbacks to use the fallback
  698. * unvectoring/retriggering technique instead of using HWMASKEXT registers.
  699. */
  700. void __init meta_intc_no_mask(void)
  701. {
  702. meta_intc_edge_chip.irq_mask = meta_intc_mask_irq_nomask;
  703. meta_intc_edge_chip.irq_unmask = meta_intc_unmask_edge_irq_nomask;
  704. meta_intc_level_chip.irq_mask = meta_intc_mask_irq_nomask;
  705. meta_intc_level_chip.irq_unmask = meta_intc_unmask_level_irq_nomask;
  706. }
  707. /**
  708. * init_external_IRQ() - initialise the external irq controller
  709. *
  710. * Set up the external irq controller using device tree properties. This is
  711. * called from init_IRQ().
  712. */
  713. int __init init_external_IRQ(void)
  714. {
  715. struct meta_intc_priv *priv = &meta_intc_priv;
  716. struct device_node *node;
  717. int ret, cpu;
  718. u32 val;
  719. bool no_masks = false;
  720. node = of_find_compatible_node(NULL, NULL, "img,meta-intc");
  721. if (!node)
  722. return -ENOENT;
  723. /* Get number of banks */
  724. ret = of_property_read_u32(node, "num-banks", &val);
  725. if (ret) {
  726. pr_err("meta-intc: No num-banks property found\n");
  727. return ret;
  728. }
  729. if (val < 1 || val > 4) {
  730. pr_err("meta-intc: num-banks (%u) out of range\n", val);
  731. return -EINVAL;
  732. }
  733. priv->nr_banks = val;
  734. /* Are any mask registers present? */
  735. if (of_get_property(node, "no-mask", NULL))
  736. no_masks = true;
  737. /* No HWMASKEXT registers present? */
  738. if (no_masks)
  739. meta_intc_no_mask();
  740. /* Set up an IRQ domain */
  741. /*
  742. * This is a legacy IRQ domain for now until all the platform setup code
  743. * has been converted to devicetree.
  744. */
  745. priv->domain = irq_domain_add_linear(node, priv->nr_banks*32,
  746. &meta_intc_domain_ops, priv);
  747. if (unlikely(!priv->domain)) {
  748. pr_err("meta-intc: cannot add IRQ domain\n");
  749. return -ENOMEM;
  750. }
  751. /* Setup TR2 for all cpus. */
  752. for_each_possible_cpu(cpu)
  753. meta_intc_init_cpu(priv, cpu);
  754. /* Set up system suspend/resume callbacks */
  755. meta_intc_init_syscore_ops(priv);
  756. pr_info("meta-intc: External IRQ controller initialised (%u IRQs)\n",
  757. priv->nr_banks*32);
  758. return 0;
  759. }