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. * @desc: the interrupt description structure for this irq
  387. *
  388. * The cpu receives an interrupt on TR2 when a SoC interrupt has occurred. It is
  389. * this function's job to demux this irq and figure out exactly which external
  390. * irq needs servicing.
  391. *
  392. * Whilst using TR2 to detect external interrupts is a software convention it is
  393. * (hopefully) unlikely to change.
  394. */
  395. static void meta_intc_irq_demux(struct irq_desc *desc)
  396. {
  397. struct meta_intc_priv *priv = &meta_intc_priv;
  398. irq_hw_number_t hw;
  399. unsigned int bank, irq_no, status;
  400. void __iomem *stat_addr = meta_intc_stat_addr(0);
  401. /*
  402. * Locate which interrupt has caused our handler to run.
  403. */
  404. for (bank = 0; bank < priv->nr_banks; ++bank) {
  405. /* Which interrupts are currently pending in this bank? */
  406. recalculate:
  407. status = metag_in32(stat_addr) & priv->unmasked[bank];
  408. for (hw = bank*32; status; status >>= 1, ++hw) {
  409. if (status & 0x1) {
  410. /*
  411. * Map the hardware IRQ number to a virtual
  412. * Linux IRQ number.
  413. */
  414. irq_no = irq_linear_revmap(priv->domain, hw);
  415. /*
  416. * Only fire off external interrupts that are
  417. * registered to be handled by the kernel.
  418. * Other external interrupts are probably being
  419. * handled by other Meta hardware threads.
  420. */
  421. generic_handle_irq(irq_no);
  422. /*
  423. * The handler may have re-enabled interrupts
  424. * which could have caused a nested invocation
  425. * of this code and make the copy of the
  426. * status register we are using invalid.
  427. */
  428. goto recalculate;
  429. }
  430. }
  431. stat_addr += HWSTAT_STRIDE;
  432. }
  433. }
  434. #ifdef CONFIG_SMP
  435. /**
  436. * meta_intc_set_affinity() - set the affinity for an interrupt
  437. * @data: data for the external irq to set the affinity of
  438. * @cpumask: cpu mask representing cpus which can handle the interrupt
  439. * @force: whether to force (ignored)
  440. *
  441. * Revector the specified external irq onto a specific cpu's TR2 trigger, so
  442. * that that cpu tends to be the one who handles it.
  443. */
  444. static int meta_intc_set_affinity(struct irq_data *data,
  445. const struct cpumask *cpumask, bool force)
  446. {
  447. irq_hw_number_t hw = data->hwirq;
  448. void __iomem *vec_addr = meta_intc_vec_addr(hw);
  449. unsigned int cpu, thread;
  450. /*
  451. * Wire up this interrupt from HWVECxEXT to the Meta core.
  452. *
  453. * Note that we can't wire up HWVECxEXT to interrupt more than
  454. * one cpu (the interrupt code doesn't support it), so we just
  455. * pick the first cpu we find in 'cpumask'.
  456. */
  457. cpu = cpumask_any_and(cpumask, cpu_online_mask);
  458. thread = cpu_2_hwthread_id[cpu];
  459. metag_out32(TBI_TRIG_VEC(TBID_SIGNUM_TR2(thread)), vec_addr);
  460. return 0;
  461. }
  462. #else
  463. #define meta_intc_set_affinity NULL
  464. #endif
  465. #ifdef CONFIG_PM_SLEEP
  466. #define META_INTC_CHIP_FLAGS (IRQCHIP_MASK_ON_SUSPEND \
  467. | IRQCHIP_SKIP_SET_WAKE)
  468. #else
  469. #define META_INTC_CHIP_FLAGS 0
  470. #endif
  471. /* public edge/level irq chips which SoCs can override */
  472. struct irq_chip meta_intc_edge_chip = {
  473. .irq_startup = meta_intc_startup_irq,
  474. .irq_shutdown = meta_intc_shutdown_irq,
  475. .irq_ack = meta_intc_ack_irq,
  476. .irq_mask = meta_intc_mask_irq,
  477. .irq_unmask = meta_intc_unmask_irq,
  478. .irq_set_type = meta_intc_irq_set_type,
  479. .irq_set_affinity = meta_intc_set_affinity,
  480. .flags = META_INTC_CHIP_FLAGS,
  481. };
  482. struct irq_chip meta_intc_level_chip = {
  483. .irq_startup = meta_intc_startup_irq,
  484. .irq_shutdown = meta_intc_shutdown_irq,
  485. .irq_set_type = meta_intc_irq_set_type,
  486. .irq_mask = meta_intc_mask_irq,
  487. .irq_unmask = meta_intc_unmask_irq,
  488. .irq_set_affinity = meta_intc_set_affinity,
  489. .flags = META_INTC_CHIP_FLAGS,
  490. };
  491. /**
  492. * meta_intc_map() - map an external irq
  493. * @d: irq domain of external trigger block
  494. * @irq: virtual irq number
  495. * @hw: hardware irq number within external trigger block
  496. *
  497. * This sets up a virtual irq for a specified hardware interrupt. The irq chip
  498. * and handler is configured, using the HWLEVELEXT registers to determine
  499. * edge/level flow type. These registers will have been set when the irq type is
  500. * set (or set to a default at init time).
  501. */
  502. static int meta_intc_map(struct irq_domain *d, unsigned int irq,
  503. irq_hw_number_t hw)
  504. {
  505. unsigned int bit = 1 << meta_intc_offset(hw);
  506. void __iomem *level_addr = meta_intc_level_addr(hw);
  507. /* Go by the current sense in the HWLEVELEXT register */
  508. if (metag_in32(level_addr) & bit)
  509. irq_set_chip_and_handler(irq, &meta_intc_level_chip,
  510. handle_level_irq);
  511. else
  512. irq_set_chip_and_handler(irq, &meta_intc_edge_chip,
  513. handle_edge_irq);
  514. return 0;
  515. }
  516. static const struct irq_domain_ops meta_intc_domain_ops = {
  517. .map = meta_intc_map,
  518. .xlate = irq_domain_xlate_twocell,
  519. };
  520. #ifdef CONFIG_METAG_SUSPEND_MEM
  521. /**
  522. * struct meta_intc_context - suspend context
  523. * @levels: State of HWLEVELEXT registers
  524. * @masks: State of HWMASKEXT registers
  525. * @vectors: State of HWVECEXT registers
  526. * @txvecint: State of TxVECINT registers
  527. *
  528. * This structure stores the IRQ state across suspend.
  529. */
  530. struct meta_intc_context {
  531. u32 levels[4];
  532. u32 masks[4];
  533. u8 vectors[4*32];
  534. u8 txvecint[4][4];
  535. };
  536. /* suspend context */
  537. static struct meta_intc_context *meta_intc_context;
  538. /**
  539. * meta_intc_suspend() - store irq state
  540. *
  541. * To avoid interfering with other threads we only save the IRQ state of IRQs in
  542. * use by Linux.
  543. */
  544. static int meta_intc_suspend(void)
  545. {
  546. struct meta_intc_priv *priv = &meta_intc_priv;
  547. int i, j;
  548. irq_hw_number_t hw;
  549. unsigned int bank;
  550. unsigned long flags;
  551. struct meta_intc_context *context;
  552. void __iomem *level_addr, *mask_addr, *vec_addr;
  553. u32 mask, bit;
  554. context = kzalloc(sizeof(*context), GFP_ATOMIC);
  555. if (!context)
  556. return -ENOMEM;
  557. hw = 0;
  558. level_addr = meta_intc_level_addr(0);
  559. mask_addr = meta_intc_mask_addr(0);
  560. for (bank = 0; bank < priv->nr_banks; ++bank) {
  561. vec_addr = meta_intc_vec_addr(hw);
  562. /* create mask of interrupts in use */
  563. mask = 0;
  564. for (bit = 1; bit; bit <<= 1) {
  565. i = irq_linear_revmap(priv->domain, hw);
  566. /* save mapped irqs which are enabled or have actions */
  567. if (i && (!irqd_irq_disabled(irq_get_irq_data(i)) ||
  568. irq_has_action(i))) {
  569. mask |= bit;
  570. /* save trigger vector */
  571. context->vectors[hw] = metag_in32(vec_addr);
  572. }
  573. ++hw;
  574. vec_addr += HWVECnEXT_STRIDE;
  575. }
  576. /* save level state if any IRQ levels altered */
  577. if (priv->levels_altered[bank])
  578. context->levels[bank] = metag_in32(level_addr);
  579. /* save mask state if any IRQs in use */
  580. if (mask)
  581. context->masks[bank] = metag_in32(mask_addr);
  582. level_addr += HWSTAT_STRIDE;
  583. mask_addr += HWSTAT_STRIDE;
  584. }
  585. /* save trigger matrixing */
  586. __global_lock2(flags);
  587. for (i = 0; i < 4; ++i)
  588. for (j = 0; j < 4; ++j)
  589. context->txvecint[i][j] = metag_in32(T0VECINT_BHALT +
  590. TnVECINT_STRIDE*i +
  591. 8*j);
  592. __global_unlock2(flags);
  593. meta_intc_context = context;
  594. return 0;
  595. }
  596. /**
  597. * meta_intc_resume() - restore saved irq state
  598. *
  599. * Restore the saved IRQ state and drop it.
  600. */
  601. static void meta_intc_resume(void)
  602. {
  603. struct meta_intc_priv *priv = &meta_intc_priv;
  604. int i, j;
  605. irq_hw_number_t hw;
  606. unsigned int bank;
  607. unsigned long flags;
  608. struct meta_intc_context *context = meta_intc_context;
  609. void __iomem *level_addr, *mask_addr, *vec_addr;
  610. u32 mask, bit, tmp;
  611. meta_intc_context = NULL;
  612. hw = 0;
  613. level_addr = meta_intc_level_addr(0);
  614. mask_addr = meta_intc_mask_addr(0);
  615. for (bank = 0; bank < priv->nr_banks; ++bank) {
  616. vec_addr = meta_intc_vec_addr(hw);
  617. /* create mask of interrupts in use */
  618. mask = 0;
  619. for (bit = 1; bit; bit <<= 1) {
  620. i = irq_linear_revmap(priv->domain, hw);
  621. /* restore mapped irqs, enabled or with actions */
  622. if (i && (!irqd_irq_disabled(irq_get_irq_data(i)) ||
  623. irq_has_action(i))) {
  624. mask |= bit;
  625. /* restore trigger vector */
  626. metag_out32(context->vectors[hw], vec_addr);
  627. }
  628. ++hw;
  629. vec_addr += HWVECnEXT_STRIDE;
  630. }
  631. if (mask) {
  632. /* restore mask state */
  633. __global_lock2(flags);
  634. tmp = metag_in32(mask_addr);
  635. tmp = (tmp & ~mask) | (context->masks[bank] & mask);
  636. metag_out32(tmp, mask_addr);
  637. __global_unlock2(flags);
  638. }
  639. mask = priv->levels_altered[bank];
  640. if (mask) {
  641. /* restore level state */
  642. __global_lock2(flags);
  643. tmp = metag_in32(level_addr);
  644. tmp = (tmp & ~mask) | (context->levels[bank] & mask);
  645. metag_out32(tmp, level_addr);
  646. __global_unlock2(flags);
  647. }
  648. level_addr += HWSTAT_STRIDE;
  649. mask_addr += HWSTAT_STRIDE;
  650. }
  651. /* restore trigger matrixing */
  652. __global_lock2(flags);
  653. for (i = 0; i < 4; ++i) {
  654. for (j = 0; j < 4; ++j) {
  655. metag_out32(context->txvecint[i][j],
  656. T0VECINT_BHALT +
  657. TnVECINT_STRIDE*i +
  658. 8*j);
  659. }
  660. }
  661. __global_unlock2(flags);
  662. kfree(context);
  663. }
  664. static struct syscore_ops meta_intc_syscore_ops = {
  665. .suspend = meta_intc_suspend,
  666. .resume = meta_intc_resume,
  667. };
  668. static void __init meta_intc_init_syscore_ops(struct meta_intc_priv *priv)
  669. {
  670. register_syscore_ops(&meta_intc_syscore_ops);
  671. }
  672. #else
  673. #define meta_intc_init_syscore_ops(priv) do {} while (0)
  674. #endif
  675. /**
  676. * meta_intc_init_cpu() - register with a Meta cpu
  677. * @priv: private interrupt controller data
  678. * @cpu: the CPU to register on
  679. *
  680. * Configure @cpu's TR2 irq so that we can demux external irqs.
  681. */
  682. static void __init meta_intc_init_cpu(struct meta_intc_priv *priv, int cpu)
  683. {
  684. unsigned int thread = cpu_2_hwthread_id[cpu];
  685. unsigned int signum = TBID_SIGNUM_TR2(thread);
  686. int irq = tbisig_map(signum);
  687. /* Register the multiplexed IRQ handler */
  688. irq_set_chained_handler(irq, meta_intc_irq_demux);
  689. irq_set_irq_type(irq, IRQ_TYPE_LEVEL_LOW);
  690. }
  691. /**
  692. * meta_intc_no_mask() - indicate lack of HWMASKEXT registers
  693. *
  694. * Called from SoC code (or init code below) to dynamically indicate the lack of
  695. * HWMASKEXT registers (for example depending on some SoC revision register).
  696. * This alters the irq mask and unmask callbacks to use the fallback
  697. * unvectoring/retriggering technique instead of using HWMASKEXT registers.
  698. */
  699. void __init meta_intc_no_mask(void)
  700. {
  701. meta_intc_edge_chip.irq_mask = meta_intc_mask_irq_nomask;
  702. meta_intc_edge_chip.irq_unmask = meta_intc_unmask_edge_irq_nomask;
  703. meta_intc_level_chip.irq_mask = meta_intc_mask_irq_nomask;
  704. meta_intc_level_chip.irq_unmask = meta_intc_unmask_level_irq_nomask;
  705. }
  706. /**
  707. * init_external_IRQ() - initialise the external irq controller
  708. *
  709. * Set up the external irq controller using device tree properties. This is
  710. * called from init_IRQ().
  711. */
  712. int __init init_external_IRQ(void)
  713. {
  714. struct meta_intc_priv *priv = &meta_intc_priv;
  715. struct device_node *node;
  716. int ret, cpu;
  717. u32 val;
  718. bool no_masks = false;
  719. node = of_find_compatible_node(NULL, NULL, "img,meta-intc");
  720. if (!node)
  721. return -ENOENT;
  722. /* Get number of banks */
  723. ret = of_property_read_u32(node, "num-banks", &val);
  724. if (ret) {
  725. pr_err("meta-intc: No num-banks property found\n");
  726. return ret;
  727. }
  728. if (val < 1 || val > 4) {
  729. pr_err("meta-intc: num-banks (%u) out of range\n", val);
  730. return -EINVAL;
  731. }
  732. priv->nr_banks = val;
  733. /* Are any mask registers present? */
  734. if (of_get_property(node, "no-mask", NULL))
  735. no_masks = true;
  736. /* No HWMASKEXT registers present? */
  737. if (no_masks)
  738. meta_intc_no_mask();
  739. /* Set up an IRQ domain */
  740. /*
  741. * This is a legacy IRQ domain for now until all the platform setup code
  742. * has been converted to devicetree.
  743. */
  744. priv->domain = irq_domain_add_linear(node, priv->nr_banks*32,
  745. &meta_intc_domain_ops, priv);
  746. if (unlikely(!priv->domain)) {
  747. pr_err("meta-intc: cannot add IRQ domain\n");
  748. return -ENOMEM;
  749. }
  750. /* Setup TR2 for all cpus. */
  751. for_each_possible_cpu(cpu)
  752. meta_intc_init_cpu(priv, cpu);
  753. /* Set up system suspend/resume callbacks */
  754. meta_intc_init_syscore_ops(priv);
  755. pr_info("meta-intc: External IRQ controller initialised (%u IRQs)\n",
  756. priv->nr_banks*32);
  757. return 0;
  758. }