q800-glue.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * QEMU q800 logic GLUE (General Logic Unit)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "cpu.h"
  24. #include "hw/m68k/q800-glue.h"
  25. #include "hw/boards.h"
  26. #include "hw/irq.h"
  27. #include "hw/nmi.h"
  28. #include "hw/qdev-properties.h"
  29. #include "migration/vmstate.h"
  30. /*
  31. * The GLUE (General Logic Unit) is an Apple custom integrated circuit chip
  32. * that performs a variety of functions (RAM management, clock generation, ...).
  33. * The GLUE chip receives interrupt requests from various devices,
  34. * assign priority to each, and asserts one or more interrupt line to the
  35. * CPU.
  36. */
  37. /*
  38. * The GLUE logic on the Quadra 800 supports 2 different IRQ routing modes
  39. * controlled from the VIA1 auxmode GPIO (port B bit 6) which are documented
  40. * in NetBSD as follows:
  41. *
  42. * A/UX mode (Linux, NetBSD, auxmode GPIO low)
  43. *
  44. * Level 0: Spurious: ignored
  45. * Level 1: Software
  46. * Level 2: VIA2 (except ethernet, sound)
  47. * Level 3: Ethernet
  48. * Level 4: Serial (SCC)
  49. * Level 5: Sound
  50. * Level 6: VIA1
  51. * Level 7: NMIs: parity errors, RESET button, YANCC error
  52. *
  53. * Classic mode (default: used by MacOS, A/UX 3.0.1, auxmode GPIO high)
  54. *
  55. * Level 0: Spurious: ignored
  56. * Level 1: VIA1 (clock, ADB)
  57. * Level 2: VIA2 (NuBus, SCSI)
  58. * Level 3:
  59. * Level 4: Serial (SCC)
  60. * Level 5:
  61. * Level 6:
  62. * Level 7: Non-maskable: parity errors, RESET button
  63. *
  64. * Note that despite references to A/UX mode in Linux and NetBSD, at least
  65. * A/UX 3.0.1 still uses Classic mode.
  66. */
  67. static void GLUE_set_irq(void *opaque, int irq, int level)
  68. {
  69. GLUEState *s = opaque;
  70. int i;
  71. if (s->auxmode) {
  72. /* Classic mode */
  73. switch (irq) {
  74. case GLUE_IRQ_IN_VIA1:
  75. irq = 0;
  76. break;
  77. case GLUE_IRQ_IN_VIA2:
  78. irq = 1;
  79. break;
  80. case GLUE_IRQ_IN_SONIC:
  81. /* Route to VIA2 instead */
  82. qemu_set_irq(s->irqs[GLUE_IRQ_NUBUS_9], level);
  83. return;
  84. case GLUE_IRQ_IN_ESCC:
  85. irq = 3;
  86. break;
  87. case GLUE_IRQ_IN_NMI:
  88. irq = 6;
  89. break;
  90. case GLUE_IRQ_IN_ASC:
  91. /* Route to VIA2 instead, negative edge-triggered */
  92. qemu_set_irq(s->irqs[GLUE_IRQ_ASC], !level);
  93. return;
  94. default:
  95. g_assert_not_reached();
  96. }
  97. } else {
  98. /* A/UX mode */
  99. switch (irq) {
  100. case GLUE_IRQ_IN_VIA1:
  101. irq = 5;
  102. break;
  103. case GLUE_IRQ_IN_VIA2:
  104. irq = 1;
  105. break;
  106. case GLUE_IRQ_IN_SONIC:
  107. irq = 2;
  108. break;
  109. case GLUE_IRQ_IN_ESCC:
  110. irq = 3;
  111. break;
  112. case GLUE_IRQ_IN_NMI:
  113. irq = 6;
  114. break;
  115. case GLUE_IRQ_IN_ASC:
  116. irq = 4;
  117. break;
  118. default:
  119. g_assert_not_reached();
  120. }
  121. }
  122. if (level) {
  123. s->ipr |= 1 << irq;
  124. } else {
  125. s->ipr &= ~(1 << irq);
  126. }
  127. for (i = 7; i >= 0; i--) {
  128. if ((s->ipr >> i) & 1) {
  129. m68k_set_irq_level(s->cpu, i + 1, i + 25);
  130. return;
  131. }
  132. }
  133. m68k_set_irq_level(s->cpu, 0, 0);
  134. }
  135. static void glue_auxmode_set_irq(void *opaque, int irq, int level)
  136. {
  137. GLUEState *s = GLUE(opaque);
  138. s->auxmode = level;
  139. }
  140. static void glue_nmi(NMIState *n, int cpu_index, Error **errp)
  141. {
  142. GLUEState *s = GLUE(n);
  143. /* Hold NMI active for 100ms */
  144. GLUE_set_irq(s, GLUE_IRQ_IN_NMI, 1);
  145. timer_mod(s->nmi_release, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 100);
  146. }
  147. static void glue_nmi_release(void *opaque)
  148. {
  149. GLUEState *s = GLUE(opaque);
  150. GLUE_set_irq(s, GLUE_IRQ_IN_NMI, 0);
  151. }
  152. static void glue_reset_hold(Object *obj, ResetType type)
  153. {
  154. GLUEState *s = GLUE(obj);
  155. s->ipr = 0;
  156. s->auxmode = 0;
  157. timer_del(s->nmi_release);
  158. }
  159. static const VMStateDescription vmstate_glue = {
  160. .name = "q800-glue",
  161. .version_id = 0,
  162. .minimum_version_id = 0,
  163. .fields = (const VMStateField[]) {
  164. VMSTATE_UINT8(ipr, GLUEState),
  165. VMSTATE_UINT8(auxmode, GLUEState),
  166. VMSTATE_TIMER_PTR(nmi_release, GLUEState),
  167. VMSTATE_END_OF_LIST(),
  168. },
  169. };
  170. /*
  171. * If the m68k CPU implemented its inbound irq lines as GPIO lines
  172. * rather than via the m68k_set_irq_level() function we would not need
  173. * this cpu link property and could instead provide outbound IRQ lines
  174. * that the board could wire up to the CPU.
  175. */
  176. static const Property glue_properties[] = {
  177. DEFINE_PROP_LINK("cpu", GLUEState, cpu, TYPE_M68K_CPU, M68kCPU *),
  178. };
  179. static void glue_finalize(Object *obj)
  180. {
  181. GLUEState *s = GLUE(obj);
  182. timer_free(s->nmi_release);
  183. }
  184. static void glue_init(Object *obj)
  185. {
  186. DeviceState *dev = DEVICE(obj);
  187. GLUEState *s = GLUE(dev);
  188. qdev_init_gpio_in(dev, GLUE_set_irq, 8);
  189. qdev_init_gpio_in_named(dev, glue_auxmode_set_irq, "auxmode", 1);
  190. qdev_init_gpio_out(dev, s->irqs, 2);
  191. /* NMI release timer */
  192. s->nmi_release = timer_new_ms(QEMU_CLOCK_VIRTUAL, glue_nmi_release, s);
  193. }
  194. static void glue_class_init(ObjectClass *klass, void *data)
  195. {
  196. DeviceClass *dc = DEVICE_CLASS(klass);
  197. ResettableClass *rc = RESETTABLE_CLASS(klass);
  198. NMIClass *nc = NMI_CLASS(klass);
  199. dc->vmsd = &vmstate_glue;
  200. device_class_set_props(dc, glue_properties);
  201. rc->phases.hold = glue_reset_hold;
  202. nc->nmi_monitor_handler = glue_nmi;
  203. }
  204. static const TypeInfo glue_info_types[] = {
  205. {
  206. .name = TYPE_GLUE,
  207. .parent = TYPE_SYS_BUS_DEVICE,
  208. .instance_size = sizeof(GLUEState),
  209. .instance_init = glue_init,
  210. .instance_finalize = glue_finalize,
  211. .class_init = glue_class_init,
  212. .interfaces = (InterfaceInfo[]) {
  213. { TYPE_NMI },
  214. { }
  215. },
  216. },
  217. };
  218. DEFINE_TYPES(glue_info_types)