2
0

omap_intc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * TI OMAP interrupt controller emulation.
  3. *
  4. * Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
  5. * Copyright (C) 2007-2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 or
  10. * (at your option) version 3 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/irq.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/arm/omap.h"
  24. #include "hw/sysbus.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/module.h"
  27. #include "qapi/error.h"
  28. /* Interrupt Handlers */
  29. struct omap_intr_handler_bank_s {
  30. uint32_t irqs;
  31. uint32_t inputs;
  32. uint32_t mask;
  33. uint32_t fiq;
  34. uint32_t sens_edge;
  35. uint32_t swi;
  36. unsigned char priority[32];
  37. };
  38. struct OMAPIntcState {
  39. SysBusDevice parent_obj;
  40. qemu_irq *pins;
  41. qemu_irq parent_intr[2];
  42. MemoryRegion mmio;
  43. void *iclk;
  44. void *fclk;
  45. unsigned char nbanks;
  46. int level_only;
  47. uint32_t size;
  48. /* state */
  49. uint32_t new_agr[2];
  50. int sir_intr[2];
  51. int autoidle;
  52. uint32_t mask;
  53. struct omap_intr_handler_bank_s bank[3];
  54. };
  55. static void omap_inth_sir_update(OMAPIntcState *s, int is_fiq)
  56. {
  57. int i, j, sir_intr, p_intr, p;
  58. uint32_t level;
  59. sir_intr = 0;
  60. p_intr = 255;
  61. /* Find the interrupt line with the highest dynamic priority.
  62. * Note: 0 denotes the highest priority.
  63. * If all interrupts have the same priority, the default order is IRQ_N,
  64. * IRQ_N-1,...,IRQ_0. */
  65. for (j = 0; j < s->nbanks; ++j) {
  66. level = s->bank[j].irqs & ~s->bank[j].mask &
  67. (is_fiq ? s->bank[j].fiq : ~s->bank[j].fiq);
  68. while (level != 0) {
  69. i = ctz32(level);
  70. p = s->bank[j].priority[i];
  71. if (p <= p_intr) {
  72. p_intr = p;
  73. sir_intr = 32 * j + i;
  74. }
  75. level &= level - 1;
  76. }
  77. }
  78. s->sir_intr[is_fiq] = sir_intr;
  79. }
  80. static inline void omap_inth_update(OMAPIntcState *s, int is_fiq)
  81. {
  82. int i;
  83. uint32_t has_intr = 0;
  84. for (i = 0; i < s->nbanks; ++i)
  85. has_intr |= s->bank[i].irqs & ~s->bank[i].mask &
  86. (is_fiq ? s->bank[i].fiq : ~s->bank[i].fiq);
  87. if (s->new_agr[is_fiq] & has_intr & s->mask) {
  88. s->new_agr[is_fiq] = 0;
  89. omap_inth_sir_update(s, is_fiq);
  90. qemu_set_irq(s->parent_intr[is_fiq], 1);
  91. }
  92. }
  93. #define INT_FALLING_EDGE 0
  94. #define INT_LOW_LEVEL 1
  95. static void omap_set_intr(void *opaque, int irq, int req)
  96. {
  97. OMAPIntcState *ih = opaque;
  98. uint32_t rise;
  99. struct omap_intr_handler_bank_s *bank = &ih->bank[irq >> 5];
  100. int n = irq & 31;
  101. if (req) {
  102. rise = ~bank->irqs & (1 << n);
  103. if (~bank->sens_edge & (1 << n))
  104. rise &= ~bank->inputs;
  105. bank->inputs |= (1 << n);
  106. if (rise) {
  107. bank->irqs |= rise;
  108. omap_inth_update(ih, 0);
  109. omap_inth_update(ih, 1);
  110. }
  111. } else {
  112. rise = bank->sens_edge & bank->irqs & (1 << n);
  113. bank->irqs &= ~rise;
  114. bank->inputs &= ~(1 << n);
  115. }
  116. }
  117. static uint64_t omap_inth_read(void *opaque, hwaddr addr,
  118. unsigned size)
  119. {
  120. OMAPIntcState *s = opaque;
  121. int i, offset = addr;
  122. int bank_no = offset >> 8;
  123. int line_no;
  124. struct omap_intr_handler_bank_s *bank = &s->bank[bank_no];
  125. offset &= 0xff;
  126. switch (offset) {
  127. case 0x00: /* ITR */
  128. return bank->irqs;
  129. case 0x04: /* MIR */
  130. return bank->mask;
  131. case 0x10: /* SIR_IRQ_CODE */
  132. case 0x14: /* SIR_FIQ_CODE */
  133. if (bank_no != 0)
  134. break;
  135. line_no = s->sir_intr[(offset - 0x10) >> 2];
  136. bank = &s->bank[line_no >> 5];
  137. i = line_no & 31;
  138. if (((bank->sens_edge >> i) & 1) == INT_FALLING_EDGE)
  139. bank->irqs &= ~(1 << i);
  140. return line_no;
  141. case 0x18: /* CONTROL_REG */
  142. if (bank_no != 0)
  143. break;
  144. return 0;
  145. case 0x1c: /* ILR0 */
  146. case 0x20: /* ILR1 */
  147. case 0x24: /* ILR2 */
  148. case 0x28: /* ILR3 */
  149. case 0x2c: /* ILR4 */
  150. case 0x30: /* ILR5 */
  151. case 0x34: /* ILR6 */
  152. case 0x38: /* ILR7 */
  153. case 0x3c: /* ILR8 */
  154. case 0x40: /* ILR9 */
  155. case 0x44: /* ILR10 */
  156. case 0x48: /* ILR11 */
  157. case 0x4c: /* ILR12 */
  158. case 0x50: /* ILR13 */
  159. case 0x54: /* ILR14 */
  160. case 0x58: /* ILR15 */
  161. case 0x5c: /* ILR16 */
  162. case 0x60: /* ILR17 */
  163. case 0x64: /* ILR18 */
  164. case 0x68: /* ILR19 */
  165. case 0x6c: /* ILR20 */
  166. case 0x70: /* ILR21 */
  167. case 0x74: /* ILR22 */
  168. case 0x78: /* ILR23 */
  169. case 0x7c: /* ILR24 */
  170. case 0x80: /* ILR25 */
  171. case 0x84: /* ILR26 */
  172. case 0x88: /* ILR27 */
  173. case 0x8c: /* ILR28 */
  174. case 0x90: /* ILR29 */
  175. case 0x94: /* ILR30 */
  176. case 0x98: /* ILR31 */
  177. i = (offset - 0x1c) >> 2;
  178. return (bank->priority[i] << 2) |
  179. (((bank->sens_edge >> i) & 1) << 1) |
  180. ((bank->fiq >> i) & 1);
  181. case 0x9c: /* ISR */
  182. return 0x00000000;
  183. }
  184. OMAP_BAD_REG(addr);
  185. return 0;
  186. }
  187. static void omap_inth_write(void *opaque, hwaddr addr,
  188. uint64_t value, unsigned size)
  189. {
  190. OMAPIntcState *s = opaque;
  191. int i, offset = addr;
  192. int bank_no = offset >> 8;
  193. struct omap_intr_handler_bank_s *bank = &s->bank[bank_no];
  194. offset &= 0xff;
  195. switch (offset) {
  196. case 0x00: /* ITR */
  197. /* Important: ignore the clearing if the IRQ is level-triggered and
  198. the input bit is 1 */
  199. bank->irqs &= value | (bank->inputs & bank->sens_edge);
  200. return;
  201. case 0x04: /* MIR */
  202. bank->mask = value;
  203. omap_inth_update(s, 0);
  204. omap_inth_update(s, 1);
  205. return;
  206. case 0x10: /* SIR_IRQ_CODE */
  207. case 0x14: /* SIR_FIQ_CODE */
  208. OMAP_RO_REG(addr);
  209. break;
  210. case 0x18: /* CONTROL_REG */
  211. if (bank_no != 0)
  212. break;
  213. if (value & 2) {
  214. qemu_set_irq(s->parent_intr[1], 0);
  215. s->new_agr[1] = ~0;
  216. omap_inth_update(s, 1);
  217. }
  218. if (value & 1) {
  219. qemu_set_irq(s->parent_intr[0], 0);
  220. s->new_agr[0] = ~0;
  221. omap_inth_update(s, 0);
  222. }
  223. return;
  224. case 0x1c: /* ILR0 */
  225. case 0x20: /* ILR1 */
  226. case 0x24: /* ILR2 */
  227. case 0x28: /* ILR3 */
  228. case 0x2c: /* ILR4 */
  229. case 0x30: /* ILR5 */
  230. case 0x34: /* ILR6 */
  231. case 0x38: /* ILR7 */
  232. case 0x3c: /* ILR8 */
  233. case 0x40: /* ILR9 */
  234. case 0x44: /* ILR10 */
  235. case 0x48: /* ILR11 */
  236. case 0x4c: /* ILR12 */
  237. case 0x50: /* ILR13 */
  238. case 0x54: /* ILR14 */
  239. case 0x58: /* ILR15 */
  240. case 0x5c: /* ILR16 */
  241. case 0x60: /* ILR17 */
  242. case 0x64: /* ILR18 */
  243. case 0x68: /* ILR19 */
  244. case 0x6c: /* ILR20 */
  245. case 0x70: /* ILR21 */
  246. case 0x74: /* ILR22 */
  247. case 0x78: /* ILR23 */
  248. case 0x7c: /* ILR24 */
  249. case 0x80: /* ILR25 */
  250. case 0x84: /* ILR26 */
  251. case 0x88: /* ILR27 */
  252. case 0x8c: /* ILR28 */
  253. case 0x90: /* ILR29 */
  254. case 0x94: /* ILR30 */
  255. case 0x98: /* ILR31 */
  256. i = (offset - 0x1c) >> 2;
  257. bank->priority[i] = (value >> 2) & 0x1f;
  258. bank->sens_edge &= ~(1 << i);
  259. bank->sens_edge |= ((value >> 1) & 1) << i;
  260. bank->fiq &= ~(1 << i);
  261. bank->fiq |= (value & 1) << i;
  262. return;
  263. case 0x9c: /* ISR */
  264. for (i = 0; i < 32; i ++)
  265. if (value & (1 << i)) {
  266. omap_set_intr(s, 32 * bank_no + i, 1);
  267. return;
  268. }
  269. return;
  270. }
  271. OMAP_BAD_REG(addr);
  272. }
  273. static const MemoryRegionOps omap_inth_mem_ops = {
  274. .read = omap_inth_read,
  275. .write = omap_inth_write,
  276. .endianness = DEVICE_NATIVE_ENDIAN,
  277. .valid = {
  278. .min_access_size = 4,
  279. .max_access_size = 4,
  280. },
  281. };
  282. static void omap_inth_reset(DeviceState *dev)
  283. {
  284. OMAPIntcState *s = OMAP_INTC(dev);
  285. int i;
  286. for (i = 0; i < s->nbanks; ++i){
  287. s->bank[i].irqs = 0x00000000;
  288. s->bank[i].mask = 0xffffffff;
  289. s->bank[i].sens_edge = 0x00000000;
  290. s->bank[i].fiq = 0x00000000;
  291. s->bank[i].inputs = 0x00000000;
  292. s->bank[i].swi = 0x00000000;
  293. memset(s->bank[i].priority, 0, sizeof(s->bank[i].priority));
  294. if (s->level_only)
  295. s->bank[i].sens_edge = 0xffffffff;
  296. }
  297. s->new_agr[0] = ~0;
  298. s->new_agr[1] = ~0;
  299. s->sir_intr[0] = 0;
  300. s->sir_intr[1] = 0;
  301. s->autoidle = 0;
  302. s->mask = ~0;
  303. qemu_set_irq(s->parent_intr[0], 0);
  304. qemu_set_irq(s->parent_intr[1], 0);
  305. }
  306. static void omap_intc_init(Object *obj)
  307. {
  308. DeviceState *dev = DEVICE(obj);
  309. OMAPIntcState *s = OMAP_INTC(obj);
  310. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  311. s->nbanks = 1;
  312. sysbus_init_irq(sbd, &s->parent_intr[0]);
  313. sysbus_init_irq(sbd, &s->parent_intr[1]);
  314. qdev_init_gpio_in(dev, omap_set_intr, s->nbanks * 32);
  315. memory_region_init_io(&s->mmio, obj, &omap_inth_mem_ops, s,
  316. "omap-intc", s->size);
  317. sysbus_init_mmio(sbd, &s->mmio);
  318. }
  319. static void omap_intc_realize(DeviceState *dev, Error **errp)
  320. {
  321. OMAPIntcState *s = OMAP_INTC(dev);
  322. if (!s->iclk) {
  323. error_setg(errp, "omap-intc: clk not connected");
  324. }
  325. }
  326. void omap_intc_set_iclk(OMAPIntcState *intc, omap_clk clk)
  327. {
  328. intc->iclk = clk;
  329. }
  330. void omap_intc_set_fclk(OMAPIntcState *intc, omap_clk clk)
  331. {
  332. intc->fclk = clk;
  333. }
  334. static const Property omap_intc_properties[] = {
  335. DEFINE_PROP_UINT32("size", OMAPIntcState, size, 0x100),
  336. };
  337. static void omap_intc_class_init(ObjectClass *klass, void *data)
  338. {
  339. DeviceClass *dc = DEVICE_CLASS(klass);
  340. device_class_set_legacy_reset(dc, omap_inth_reset);
  341. device_class_set_props(dc, omap_intc_properties);
  342. /* Reason: pointer property "clk" */
  343. dc->user_creatable = false;
  344. dc->realize = omap_intc_realize;
  345. }
  346. static const TypeInfo omap_intc_info = {
  347. .name = TYPE_OMAP_INTC,
  348. .parent = TYPE_SYS_BUS_DEVICE,
  349. .instance_size = sizeof(OMAPIntcState),
  350. .instance_init = omap_intc_init,
  351. .class_init = omap_intc_class_init,
  352. };
  353. static void omap_intc_register_types(void)
  354. {
  355. type_register_static(&omap_intc_info);
  356. }
  357. type_init(omap_intc_register_types)