mac_via.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. * QEMU m68k Macintosh VIA device support
  3. *
  4. * Copyright (c) 2011-2018 Laurent Vivier
  5. * Copyright (c) 2018 Mark Cave-Ayland
  6. *
  7. * Some parts from hw/misc/macio/cuda.c
  8. *
  9. * Copyright (c) 2004-2007 Fabrice Bellard
  10. * Copyright (c) 2007 Jocelyn Mayer
  11. *
  12. * some parts from linux-2.6.29, arch/m68k/include/asm/mac_via.h
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  15. * See the COPYING file in the top-level directory.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "exec/address-spaces.h"
  19. #include "migration/vmstate.h"
  20. #include "hw/sysbus.h"
  21. #include "hw/irq.h"
  22. #include "qemu/timer.h"
  23. #include "hw/misc/mac_via.h"
  24. #include "hw/misc/mos6522.h"
  25. #include "hw/input/adb.h"
  26. #include "system/runstate.h"
  27. #include "qapi/error.h"
  28. #include "qemu/cutils.h"
  29. #include "hw/qdev-properties.h"
  30. #include "hw/qdev-properties-system.h"
  31. #include "system/block-backend.h"
  32. #include "system/rtc.h"
  33. #include "trace.h"
  34. #include "qemu/log.h"
  35. /*
  36. * VIAs: There are two in every machine
  37. */
  38. /*
  39. * Not all of these are true post MacII I think.
  40. * CSA: probably the ones CHRP marks as 'unused' change purposes
  41. * when the IWM becomes the SWIM.
  42. * http://www.rs6000.ibm.com/resource/technology/chrpio/via5.mak.html
  43. * ftp://ftp.austin.ibm.com/pub/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
  44. *
  45. * also, http://developer.apple.com/technotes/hw/hw_09.html claims the
  46. * following changes for IIfx:
  47. * VIA1A_vSccWrReq not available and that VIA1A_vSync has moved to an IOP.
  48. * Also, "All of the functionality of VIA2 has been moved to other chips".
  49. */
  50. #define VIA1A_vSccWrReq 0x80 /*
  51. * SCC write. (input)
  52. * [CHRP] SCC WREQ: Reflects the state of the
  53. * Wait/Request pins from the SCC.
  54. * [Macintosh Family Hardware]
  55. * as CHRP on SE/30,II,IIx,IIcx,IIci.
  56. * on IIfx, "0 means an active request"
  57. */
  58. #define VIA1A_vRev8 0x40 /*
  59. * Revision 8 board ???
  60. * [CHRP] En WaitReqB: Lets the WaitReq_L
  61. * signal from port B of the SCC appear on
  62. * the PA7 input pin. Output.
  63. * [Macintosh Family] On the SE/30, this
  64. * is the bit to flip screen buffers.
  65. * 0=alternate, 1=main.
  66. * on II,IIx,IIcx,IIci,IIfx this is a bit
  67. * for Rev ID. 0=II,IIx, 1=IIcx,IIci,IIfx
  68. */
  69. #define VIA1A_vHeadSel 0x20 /*
  70. * Head select for IWM.
  71. * [CHRP] unused.
  72. * [Macintosh Family] "Floppy disk
  73. * state-control line SEL" on all but IIfx
  74. */
  75. #define VIA1A_vOverlay 0x10 /*
  76. * [Macintosh Family] On SE/30,II,IIx,IIcx
  77. * this bit enables the "Overlay" address
  78. * map in the address decoders as it is on
  79. * reset for mapping the ROM over the reset
  80. * vector. 1=use overlay map.
  81. * On the IIci,IIfx it is another bit of the
  82. * CPU ID: 0=normal IIci, 1=IIci with parity
  83. * feature or IIfx.
  84. * [CHRP] En WaitReqA: Lets the WaitReq_L
  85. * signal from port A of the SCC appear
  86. * on the PA7 input pin (CHRP). Output.
  87. * [MkLinux] "Drive Select"
  88. * (with 0x20 being 'disk head select')
  89. */
  90. #define VIA1A_vSync 0x08 /*
  91. * [CHRP] Sync Modem: modem clock select:
  92. * 1: select the external serial clock to
  93. * drive the SCC's /RTxCA pin.
  94. * 0: Select the 3.6864MHz clock to drive
  95. * the SCC cell.
  96. * [Macintosh Family] Correct on all but IIfx
  97. */
  98. /*
  99. * Macintosh Family Hardware sez: bits 0-2 of VIA1A are volume control
  100. * on Macs which had the PWM sound hardware. Reserved on newer models.
  101. * On IIci,IIfx, bits 1-2 are the rest of the CPU ID:
  102. * bit 2: 1=IIci, 0=IIfx
  103. * bit 1: 1 on both IIci and IIfx.
  104. * MkLinux sez bit 0 is 'burnin flag' in this case.
  105. * CHRP sez: VIA1A bits 0-2 and 5 are 'unused': if programmed as
  106. * inputs, these bits will read 0.
  107. */
  108. #define VIA1A_vVolume 0x07 /* Audio volume mask for PWM */
  109. #define VIA1A_CPUID0 0x02 /* CPU id bit 0 on RBV, others */
  110. #define VIA1A_CPUID1 0x04 /* CPU id bit 0 on RBV, others */
  111. #define VIA1A_CPUID2 0x10 /* CPU id bit 0 on RBV, others */
  112. #define VIA1A_CPUID3 0x40 /* CPU id bit 0 on RBV, others */
  113. #define VIA1A_CPUID_MASK (VIA1A_CPUID0 | VIA1A_CPUID1 | \
  114. VIA1A_CPUID2 | VIA1A_CPUID3)
  115. #define VIA1A_CPUID_Q800 (VIA1A_CPUID0 | VIA1A_CPUID2)
  116. /*
  117. * Info on VIA1B is from Macintosh Family Hardware & MkLinux.
  118. * CHRP offers no info.
  119. */
  120. #define VIA1B_vSound 0x80 /*
  121. * Sound enable (for compatibility with
  122. * PWM hardware) 0=enabled.
  123. * Also, on IIci w/parity, shows parity error
  124. * 0=error, 1=OK.
  125. */
  126. #define VIA1B_vMystery 0x40 /*
  127. * On IIci, parity enable. 0=enabled,1=disabled
  128. * On SE/30, vertical sync interrupt enable.
  129. * 0=enabled. This vSync interrupt shows up
  130. * as a slot $E interrupt.
  131. * On Quadra 800 this bit toggles A/UX mode which
  132. * configures the glue logic to deliver some IRQs
  133. * at different levels compared to a classic
  134. * Mac.
  135. */
  136. #define VIA1B_vADBS2 0x20 /* ADB state input bit 1 (unused on IIfx) */
  137. #define VIA1B_vADBS1 0x10 /* ADB state input bit 0 (unused on IIfx) */
  138. #define VIA1B_vADBInt 0x08 /* ADB interrupt 0=interrupt (unused on IIfx)*/
  139. #define VIA1B_vRTCEnb 0x04 /* Enable Real time clock. 0=enabled. */
  140. #define VIA1B_vRTCClk 0x02 /* Real time clock serial-clock line. */
  141. #define VIA1B_vRTCData 0x01 /* Real time clock serial-data line. */
  142. /*
  143. * VIA2 A register is the interrupt lines raised off the nubus
  144. * slots.
  145. * The below info is from 'Macintosh Family Hardware.'
  146. * MkLinux calls the 'IIci internal video IRQ' below the 'RBV slot 0 irq.'
  147. * It also notes that the slot $9 IRQ is the 'Ethernet IRQ' and
  148. * defines the 'Video IRQ' as 0x40 for the 'EVR' VIA work-alike.
  149. * Perhaps OSS uses vRAM1 and vRAM2 for ADB.
  150. */
  151. #define VIA2A_vRAM1 0x80 /* RAM size bit 1 (IIci: reserved) */
  152. #define VIA2A_vRAM0 0x40 /* RAM size bit 0 (IIci: internal video IRQ) */
  153. #define VIA2A_vIRQE 0x20 /* IRQ from slot $E */
  154. #define VIA2A_vIRQD 0x10 /* IRQ from slot $D */
  155. #define VIA2A_vIRQC 0x08 /* IRQ from slot $C */
  156. #define VIA2A_vIRQB 0x04 /* IRQ from slot $B */
  157. #define VIA2A_vIRQA 0x02 /* IRQ from slot $A */
  158. #define VIA2A_vIRQ9 0x01 /* IRQ from slot $9 */
  159. /*
  160. * RAM size bits decoded as follows:
  161. * bit1 bit0 size of ICs in bank A
  162. * 0 0 256 kbit
  163. * 0 1 1 Mbit
  164. * 1 0 4 Mbit
  165. * 1 1 16 Mbit
  166. */
  167. /*
  168. * Register B has the fun stuff in it
  169. */
  170. #define VIA2B_vVBL 0x80 /*
  171. * VBL output to VIA1 (60.15Hz) driven by
  172. * timer T1.
  173. * on IIci, parity test: 0=test mode.
  174. * [MkLinux] RBV_PARODD: 1=odd,0=even.
  175. */
  176. #define VIA2B_vSndJck 0x40 /*
  177. * External sound jack status.
  178. * 0=plug is inserted. On SE/30, always 0
  179. */
  180. #define VIA2B_vTfr0 0x20 /* Transfer mode bit 0 ack from NuBus */
  181. #define VIA2B_vTfr1 0x10 /* Transfer mode bit 1 ack from NuBus */
  182. #define VIA2B_vMode32 0x08 /*
  183. * 24/32bit switch - doubles as cache flush
  184. * on II, AMU/PMMU control.
  185. * if AMU, 0=24bit to 32bit translation
  186. * if PMMU, 1=PMMU is accessing page table.
  187. * on SE/30 tied low.
  188. * on IIx,IIcx,IIfx, unused.
  189. * on IIci/RBV, cache control. 0=flush cache.
  190. */
  191. #define VIA2B_vPower 0x04 /*
  192. * Power off, 0=shut off power.
  193. * on SE/30 this signal sent to PDS card.
  194. */
  195. #define VIA2B_vBusLk 0x02 /*
  196. * Lock NuBus transactions, 0=locked.
  197. * on SE/30 sent to PDS card.
  198. */
  199. #define VIA2B_vCDis 0x01 /*
  200. * Cache control. On IIci, 1=disable cache card
  201. * on others, 0=disable processor's instruction
  202. * and data caches.
  203. */
  204. /* interrupt flags */
  205. #define IRQ_SET 0x80
  206. /* common */
  207. #define VIA_IRQ_TIMER1 0x40
  208. #define VIA_IRQ_TIMER2 0x20
  209. /*
  210. * Apple sez: http://developer.apple.com/technotes/ov/ov_04.html
  211. * Another example of a valid function that has no ROM support is the use
  212. * of the alternate video page for page-flipping animation. Since there
  213. * is no ROM call to flip pages, it is necessary to go play with the
  214. * right bit in the VIA chip (6522 Versatile Interface Adapter).
  215. * [CSA: don't know which one this is, but it's one of 'em!]
  216. */
  217. /*
  218. * 6522 registers - see databook.
  219. * CSA: Assignments for VIA1 confirmed from CHRP spec.
  220. */
  221. /* partial address decode. 0xYYXX : XX part for RBV, YY part for VIA */
  222. /* Note: 15 VIA regs, 8 RBV regs */
  223. #define vBufB 0x0000 /* [VIA/RBV] Register B */
  224. #define vBufAH 0x0200 /* [VIA only] Buffer A, with handshake. DON'T USE! */
  225. #define vDirB 0x0400 /* [VIA only] Data Direction Register B. */
  226. #define vDirA 0x0600 /* [VIA only] Data Direction Register A. */
  227. #define vT1CL 0x0800 /* [VIA only] Timer one counter low. */
  228. #define vT1CH 0x0a00 /* [VIA only] Timer one counter high. */
  229. #define vT1LL 0x0c00 /* [VIA only] Timer one latches low. */
  230. #define vT1LH 0x0e00 /* [VIA only] Timer one latches high. */
  231. #define vT2CL 0x1000 /* [VIA only] Timer two counter low. */
  232. #define vT2CH 0x1200 /* [VIA only] Timer two counter high. */
  233. #define vSR 0x1400 /* [VIA only] Shift register. */
  234. #define vACR 0x1600 /* [VIA only] Auxiliary control register. */
  235. #define vPCR 0x1800 /* [VIA only] Peripheral control register. */
  236. /*
  237. * CHRP sez never ever to *write* this.
  238. * Mac family says never to *change* this.
  239. * In fact we need to initialize it once at start.
  240. */
  241. #define vIFR 0x1a00 /* [VIA/RBV] Interrupt flag register. */
  242. #define vIER 0x1c00 /* [VIA/RBV] Interrupt enable register. */
  243. #define vBufA 0x1e00 /* [VIA/RBV] register A (no handshake) */
  244. /* from linux 2.6 drivers/macintosh/via-macii.c */
  245. /* Bits in ACR */
  246. #define VIA1ACR_vShiftCtrl 0x1c /* Shift register control bits */
  247. #define VIA1ACR_vShiftExtClk 0x0c /* Shift on external clock */
  248. #define VIA1ACR_vShiftOut 0x10 /* Shift out if 1 */
  249. /*
  250. * Apple Macintosh Family Hardware Refenece
  251. * Table 19-10 ADB transaction states
  252. */
  253. #define ADB_STATE_NEW 0
  254. #define ADB_STATE_EVEN 1
  255. #define ADB_STATE_ODD 2
  256. #define ADB_STATE_IDLE 3
  257. #define VIA1B_vADB_StateMask (VIA1B_vADBS1 | VIA1B_vADBS2)
  258. #define VIA1B_vADB_StateShift 4
  259. #define VIA_TIMER_FREQ (783360)
  260. #define VIA_ADB_POLL_FREQ 50 /* XXX: not real */
  261. /*
  262. * Guide to the Macintosh Family Hardware ch. 12 "Displays" p. 401 gives the
  263. * precise 60Hz interrupt frequency as ~60.15Hz with a period of 16625.8 us
  264. */
  265. #define VIA_60HZ_TIMER_PERIOD_NS 16625800
  266. /* VIA returns time offset from Jan 1, 1904, not 1970 */
  267. #define RTC_OFFSET 2082844800
  268. enum {
  269. REG_0,
  270. REG_1,
  271. REG_2,
  272. REG_3,
  273. REG_TEST,
  274. REG_WPROTECT,
  275. REG_PRAM_ADDR,
  276. REG_PRAM_ADDR_LAST = REG_PRAM_ADDR + 19,
  277. REG_PRAM_SECT,
  278. REG_PRAM_SECT_LAST = REG_PRAM_SECT + 7,
  279. REG_INVALID,
  280. REG_EMPTY = 0xff,
  281. };
  282. static void via1_sixty_hz_update(MOS6522Q800VIA1State *v1s)
  283. {
  284. /* 60 Hz irq */
  285. v1s->next_sixty_hz = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  286. VIA_60HZ_TIMER_PERIOD_NS) /
  287. VIA_60HZ_TIMER_PERIOD_NS * VIA_60HZ_TIMER_PERIOD_NS;
  288. timer_mod(v1s->sixty_hz_timer, v1s->next_sixty_hz);
  289. }
  290. static void via1_one_second_update(MOS6522Q800VIA1State *v1s)
  291. {
  292. v1s->next_second = (qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000) /
  293. 1000 * 1000;
  294. timer_mod(v1s->one_second_timer, v1s->next_second);
  295. }
  296. static void via1_sixty_hz(void *opaque)
  297. {
  298. MOS6522Q800VIA1State *v1s = opaque;
  299. MOS6522State *s = MOS6522(v1s);
  300. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_60HZ_BIT);
  301. /* Negative edge trigger */
  302. qemu_irq_lower(irq);
  303. qemu_irq_raise(irq);
  304. via1_sixty_hz_update(v1s);
  305. }
  306. static void via1_one_second(void *opaque)
  307. {
  308. MOS6522Q800VIA1State *v1s = opaque;
  309. MOS6522State *s = MOS6522(v1s);
  310. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA1_IRQ_ONE_SECOND_BIT);
  311. /* Negative edge trigger */
  312. qemu_irq_lower(irq);
  313. qemu_irq_raise(irq);
  314. via1_one_second_update(v1s);
  315. }
  316. static void pram_update(MOS6522Q800VIA1State *v1s)
  317. {
  318. if (v1s->blk) {
  319. if (blk_pwrite(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0) < 0) {
  320. qemu_log("pram_update: cannot write to file\n");
  321. }
  322. }
  323. }
  324. /*
  325. * RTC Commands
  326. *
  327. * Command byte Register addressed by the command
  328. *
  329. * z00x0001 Seconds register 0 (lowest-order byte)
  330. * z00x0101 Seconds register 1
  331. * z00x1001 Seconds register 2
  332. * z00x1101 Seconds register 3 (highest-order byte)
  333. * 00110001 Test register (write-only)
  334. * 00110101 Write-Protect Register (write-only)
  335. * z010aa01 RAM address 100aa ($10-$13) (first 20 bytes only)
  336. * z1aaaa01 RAM address 0aaaa ($00-$0F) (first 20 bytes only)
  337. * z0111aaa Extended memory designator and sector number
  338. *
  339. * For a read request, z=1, for a write z=0
  340. * The letter x indicates don't care
  341. * The letter a indicates bits whose value depend on what parameter
  342. * RAM byte you want to address
  343. */
  344. static int via1_rtc_compact_cmd(uint8_t value)
  345. {
  346. uint8_t read = value & 0x80;
  347. value &= 0x7f;
  348. /* the last 2 bits of a command byte must always be 0b01 ... */
  349. if ((value & 0x78) == 0x38) {
  350. /* except for the extended memory designator */
  351. return read | (REG_PRAM_SECT + (value & 0x07));
  352. }
  353. if ((value & 0x03) == 0x01) {
  354. value >>= 2;
  355. if ((value & 0x18) == 0) {
  356. /* seconds registers */
  357. return read | (REG_0 + (value & 0x03));
  358. } else if ((value == 0x0c) && !read) {
  359. return REG_TEST;
  360. } else if ((value == 0x0d) && !read) {
  361. return REG_WPROTECT;
  362. } else if ((value & 0x1c) == 0x08) {
  363. /* RAM address 0x10 to 0x13 */
  364. return read | (REG_PRAM_ADDR + 0x10 + (value & 0x03));
  365. } else if ((value & 0x10) == 0x10) {
  366. /* RAM address 0x00 to 0x0f */
  367. return read | (REG_PRAM_ADDR + (value & 0x0f));
  368. }
  369. }
  370. return REG_INVALID;
  371. }
  372. static void via1_rtc_update(MOS6522Q800VIA1State *v1s)
  373. {
  374. MOS6522State *s = MOS6522(v1s);
  375. int cmd, sector, addr;
  376. uint32_t time;
  377. if (s->b & VIA1B_vRTCEnb) {
  378. return;
  379. }
  380. if (s->dirb & VIA1B_vRTCData) {
  381. /* send bits to the RTC */
  382. if (!(v1s->last_b & VIA1B_vRTCClk) && (s->b & VIA1B_vRTCClk)) {
  383. v1s->data_out <<= 1;
  384. v1s->data_out |= s->b & VIA1B_vRTCData;
  385. v1s->data_out_cnt++;
  386. }
  387. trace_via1_rtc_update_data_out(v1s->data_out_cnt, v1s->data_out);
  388. } else {
  389. trace_via1_rtc_update_data_in(v1s->data_in_cnt, v1s->data_in);
  390. /* receive bits from the RTC */
  391. if ((v1s->last_b & VIA1B_vRTCClk) &&
  392. !(s->b & VIA1B_vRTCClk) &&
  393. v1s->data_in_cnt) {
  394. s->b = (s->b & ~VIA1B_vRTCData) |
  395. ((v1s->data_in >> 7) & VIA1B_vRTCData);
  396. v1s->data_in <<= 1;
  397. v1s->data_in_cnt--;
  398. }
  399. return;
  400. }
  401. if (v1s->data_out_cnt != 8) {
  402. return;
  403. }
  404. v1s->data_out_cnt = 0;
  405. trace_via1_rtc_internal_status(v1s->cmd, v1s->alt, v1s->data_out);
  406. /* first byte: it's a command */
  407. if (v1s->cmd == REG_EMPTY) {
  408. cmd = via1_rtc_compact_cmd(v1s->data_out);
  409. trace_via1_rtc_internal_cmd(cmd);
  410. if (cmd == REG_INVALID) {
  411. trace_via1_rtc_cmd_invalid(v1s->data_out);
  412. return;
  413. }
  414. if (cmd & 0x80) { /* this is a read command */
  415. switch (cmd & 0x7f) {
  416. case REG_0...REG_3: /* seconds registers */
  417. /*
  418. * register 0 is lowest-order byte
  419. * register 3 is highest-order byte
  420. */
  421. time = v1s->tick_offset + (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
  422. / NANOSECONDS_PER_SECOND);
  423. trace_via1_rtc_internal_time(time);
  424. v1s->data_in = (time >> ((cmd & 0x03) << 3)) & 0xff;
  425. v1s->data_in_cnt = 8;
  426. trace_via1_rtc_cmd_seconds_read((cmd & 0x7f) - REG_0,
  427. v1s->data_in);
  428. break;
  429. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  430. /* PRAM address 0x00 -> 0x13 */
  431. v1s->data_in = v1s->PRAM[(cmd & 0x7f) - REG_PRAM_ADDR];
  432. v1s->data_in_cnt = 8;
  433. trace_via1_rtc_cmd_pram_read((cmd & 0x7f) - REG_PRAM_ADDR,
  434. v1s->data_in);
  435. break;
  436. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  437. /*
  438. * extended memory designator and sector number
  439. * the only two-byte read command
  440. */
  441. trace_via1_rtc_internal_set_cmd(cmd);
  442. v1s->cmd = cmd;
  443. break;
  444. default:
  445. g_assert_not_reached();
  446. }
  447. return;
  448. }
  449. /* this is a write command, needs a parameter */
  450. if (cmd == REG_WPROTECT || !v1s->wprotect) {
  451. trace_via1_rtc_internal_set_cmd(cmd);
  452. v1s->cmd = cmd;
  453. } else {
  454. trace_via1_rtc_internal_ignore_cmd(cmd);
  455. }
  456. return;
  457. }
  458. /* second byte: it's a parameter */
  459. if (v1s->alt == REG_EMPTY) {
  460. switch (v1s->cmd & 0x7f) {
  461. case REG_0...REG_3: /* seconds register */
  462. /* FIXME */
  463. trace_via1_rtc_cmd_seconds_write(v1s->cmd - REG_0, v1s->data_out);
  464. v1s->cmd = REG_EMPTY;
  465. break;
  466. case REG_TEST:
  467. /* device control: nothing to do */
  468. trace_via1_rtc_cmd_test_write(v1s->data_out);
  469. v1s->cmd = REG_EMPTY;
  470. break;
  471. case REG_WPROTECT:
  472. /* Write Protect register */
  473. trace_via1_rtc_cmd_wprotect_write(v1s->data_out);
  474. v1s->wprotect = !!(v1s->data_out & 0x80);
  475. v1s->cmd = REG_EMPTY;
  476. break;
  477. case REG_PRAM_ADDR...REG_PRAM_ADDR_LAST:
  478. /* PRAM address 0x00 -> 0x13 */
  479. trace_via1_rtc_cmd_pram_write(v1s->cmd - REG_PRAM_ADDR,
  480. v1s->data_out);
  481. v1s->PRAM[v1s->cmd - REG_PRAM_ADDR] = v1s->data_out;
  482. pram_update(v1s);
  483. v1s->cmd = REG_EMPTY;
  484. break;
  485. case REG_PRAM_SECT...REG_PRAM_SECT_LAST:
  486. addr = (v1s->data_out >> 2) & 0x1f;
  487. sector = (v1s->cmd & 0x7f) - REG_PRAM_SECT;
  488. if (v1s->cmd & 0x80) {
  489. /* it's a read */
  490. v1s->data_in = v1s->PRAM[sector * 32 + addr];
  491. v1s->data_in_cnt = 8;
  492. trace_via1_rtc_cmd_pram_sect_read(sector, addr,
  493. sector * 32 + addr,
  494. v1s->data_in);
  495. v1s->cmd = REG_EMPTY;
  496. } else {
  497. /* it's a write, we need one more parameter */
  498. trace_via1_rtc_internal_set_alt(addr, sector, addr);
  499. v1s->alt = addr;
  500. }
  501. break;
  502. default:
  503. g_assert_not_reached();
  504. }
  505. return;
  506. }
  507. /* third byte: it's the data of a REG_PRAM_SECT write */
  508. g_assert(REG_PRAM_SECT <= v1s->cmd && v1s->cmd <= REG_PRAM_SECT_LAST);
  509. sector = v1s->cmd - REG_PRAM_SECT;
  510. v1s->PRAM[sector * 32 + v1s->alt] = v1s->data_out;
  511. pram_update(v1s);
  512. trace_via1_rtc_cmd_pram_sect_write(sector, v1s->alt, sector * 32 + v1s->alt,
  513. v1s->data_out);
  514. v1s->alt = REG_EMPTY;
  515. v1s->cmd = REG_EMPTY;
  516. }
  517. static void adb_via_poll(void *opaque)
  518. {
  519. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  520. MOS6522State *s = MOS6522(v1s);
  521. ADBBusState *adb_bus = &v1s->adb_bus;
  522. uint8_t obuf[9];
  523. uint8_t *data = &s->sr;
  524. int olen;
  525. /*
  526. * Setting vADBInt below indicates that an autopoll reply has been
  527. * received, however we must block autopoll until the point where
  528. * the entire reply has been read back to the host
  529. */
  530. adb_autopoll_block(adb_bus);
  531. if (v1s->adb_data_in_size > 0 && v1s->adb_data_in_index == 0) {
  532. /*
  533. * For older Linux kernels that switch to IDLE mode after sending the
  534. * ADB command, detect if there is an existing response and return that
  535. * as a "fake" autopoll reply or bus timeout accordingly
  536. */
  537. *data = v1s->adb_data_out[0];
  538. olen = v1s->adb_data_in_size;
  539. s->b &= ~VIA1B_vADBInt;
  540. qemu_irq_raise(v1s->adb_data_ready);
  541. } else {
  542. /*
  543. * Otherwise poll as normal
  544. */
  545. v1s->adb_data_in_index = 0;
  546. v1s->adb_data_out_index = 0;
  547. olen = adb_poll(adb_bus, obuf, adb_bus->autopoll_mask);
  548. if (olen > 0) {
  549. /* Autopoll response */
  550. *data = obuf[0];
  551. olen--;
  552. memcpy(v1s->adb_data_in, &obuf[1], olen);
  553. v1s->adb_data_in_size = olen;
  554. s->b &= ~VIA1B_vADBInt;
  555. qemu_irq_raise(v1s->adb_data_ready);
  556. } else {
  557. *data = v1s->adb_autopoll_cmd;
  558. obuf[0] = 0xff;
  559. obuf[1] = 0xff;
  560. olen = 2;
  561. memcpy(v1s->adb_data_in, obuf, olen);
  562. v1s->adb_data_in_size = olen;
  563. s->b &= ~VIA1B_vADBInt;
  564. qemu_irq_raise(v1s->adb_data_ready);
  565. }
  566. }
  567. trace_via1_adb_poll(*data, (s->b & VIA1B_vADBInt) ? "+" : "-",
  568. adb_bus->status, v1s->adb_data_in_index, olen);
  569. }
  570. static int adb_via_send_len(uint8_t data)
  571. {
  572. /* Determine the send length from the given ADB command */
  573. uint8_t cmd = data & 0xc;
  574. uint8_t reg = data & 0x3;
  575. switch (cmd) {
  576. case 0x8:
  577. /* Listen command */
  578. switch (reg) {
  579. case 2:
  580. /* Register 2 is only used for the keyboard */
  581. return 3;
  582. case 3:
  583. /*
  584. * Fortunately our devices only implement writes
  585. * to register 3 which is fixed at 2 bytes
  586. */
  587. return 3;
  588. default:
  589. qemu_log_mask(LOG_UNIMP, "ADB unknown length for register %d\n",
  590. reg);
  591. return 1;
  592. }
  593. default:
  594. /* Talk, BusReset */
  595. return 1;
  596. }
  597. }
  598. static void adb_via_send(MOS6522Q800VIA1State *v1s, int state, uint8_t data)
  599. {
  600. MOS6522State *ms = MOS6522(v1s);
  601. ADBBusState *adb_bus = &v1s->adb_bus;
  602. uint16_t autopoll_mask;
  603. switch (state) {
  604. case ADB_STATE_NEW:
  605. /*
  606. * Command byte: vADBInt tells host autopoll data already present
  607. * in VIA shift register and ADB transceiver
  608. */
  609. adb_autopoll_block(adb_bus);
  610. if (adb_bus->status & ADB_STATUS_POLLREPLY) {
  611. /* Tell the host the existing data is from autopoll */
  612. ms->b &= ~VIA1B_vADBInt;
  613. } else {
  614. ms->b |= VIA1B_vADBInt;
  615. v1s->adb_data_out_index = 0;
  616. v1s->adb_data_out[v1s->adb_data_out_index++] = data;
  617. }
  618. trace_via1_adb_send(" NEW", data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  619. qemu_irq_raise(v1s->adb_data_ready);
  620. break;
  621. case ADB_STATE_EVEN:
  622. case ADB_STATE_ODD:
  623. ms->b |= VIA1B_vADBInt;
  624. v1s->adb_data_out[v1s->adb_data_out_index++] = data;
  625. trace_via1_adb_send(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  626. data, (ms->b & VIA1B_vADBInt) ? "+" : "-");
  627. qemu_irq_raise(v1s->adb_data_ready);
  628. break;
  629. case ADB_STATE_IDLE:
  630. ms->b |= VIA1B_vADBInt;
  631. adb_autopoll_unblock(adb_bus);
  632. trace_via1_adb_send("IDLE", data,
  633. (ms->b & VIA1B_vADBInt) ? "+" : "-");
  634. return;
  635. }
  636. /* If the command is complete, execute it */
  637. if (v1s->adb_data_out_index == adb_via_send_len(v1s->adb_data_out[0])) {
  638. v1s->adb_data_in_size = adb_request(adb_bus, v1s->adb_data_in,
  639. v1s->adb_data_out,
  640. v1s->adb_data_out_index);
  641. v1s->adb_data_in_index = 0;
  642. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  643. /*
  644. * Bus timeout (but allow first EVEN and ODD byte to indicate
  645. * timeout via vADBInt and SRQ status)
  646. */
  647. v1s->adb_data_in[0] = 0xff;
  648. v1s->adb_data_in[1] = 0xff;
  649. v1s->adb_data_in_size = 2;
  650. }
  651. /*
  652. * If last command is TALK, store it for use by autopoll and adjust
  653. * the autopoll mask accordingly
  654. */
  655. if ((v1s->adb_data_out[0] & 0xc) == 0xc) {
  656. v1s->adb_autopoll_cmd = v1s->adb_data_out[0];
  657. autopoll_mask = 1 << (v1s->adb_autopoll_cmd >> 4);
  658. adb_set_autopoll_mask(adb_bus, autopoll_mask);
  659. }
  660. }
  661. }
  662. static void adb_via_receive(MOS6522Q800VIA1State *v1s, int state, uint8_t *data)
  663. {
  664. MOS6522State *ms = MOS6522(v1s);
  665. ADBBusState *adb_bus = &v1s->adb_bus;
  666. uint16_t pending;
  667. switch (state) {
  668. case ADB_STATE_NEW:
  669. ms->b |= VIA1B_vADBInt;
  670. return;
  671. case ADB_STATE_IDLE:
  672. ms->b |= VIA1B_vADBInt;
  673. adb_autopoll_unblock(adb_bus);
  674. trace_via1_adb_receive("IDLE", *data,
  675. (ms->b & VIA1B_vADBInt) ? "+" : "-", adb_bus->status,
  676. v1s->adb_data_in_index, v1s->adb_data_in_size);
  677. break;
  678. case ADB_STATE_EVEN:
  679. case ADB_STATE_ODD:
  680. switch (v1s->adb_data_in_index) {
  681. case 0:
  682. /* First EVEN byte: vADBInt indicates bus timeout */
  683. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  684. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  685. ms->b &= ~VIA1B_vADBInt;
  686. } else {
  687. ms->b |= VIA1B_vADBInt;
  688. }
  689. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  690. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  691. adb_bus->status, v1s->adb_data_in_index,
  692. v1s->adb_data_in_size);
  693. v1s->adb_data_in_index++;
  694. break;
  695. case 1:
  696. /* First ODD byte: vADBInt indicates SRQ */
  697. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  698. pending = adb_bus->pending & ~(1 << (v1s->adb_autopoll_cmd >> 4));
  699. if (pending) {
  700. ms->b &= ~VIA1B_vADBInt;
  701. } else {
  702. ms->b |= VIA1B_vADBInt;
  703. }
  704. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  705. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  706. adb_bus->status, v1s->adb_data_in_index,
  707. v1s->adb_data_in_size);
  708. v1s->adb_data_in_index++;
  709. break;
  710. default:
  711. /*
  712. * Otherwise vADBInt indicates end of data. Note that Linux
  713. * specifically checks for the sequence 0x0 0xff to confirm the
  714. * end of the poll reply, so provide these extra bytes below to
  715. * keep it happy
  716. */
  717. if (v1s->adb_data_in_index < v1s->adb_data_in_size) {
  718. /* Next data byte */
  719. *data = v1s->adb_data_in[v1s->adb_data_in_index];
  720. ms->b |= VIA1B_vADBInt;
  721. } else if (v1s->adb_data_in_index == v1s->adb_data_in_size) {
  722. if (adb_bus->status & ADB_STATUS_BUSTIMEOUT) {
  723. /* Bus timeout (no more data) */
  724. *data = 0xff;
  725. } else {
  726. /* Return 0x0 after reply */
  727. *data = 0;
  728. }
  729. ms->b &= ~VIA1B_vADBInt;
  730. } else {
  731. /* Bus timeout (no more data) */
  732. *data = 0xff;
  733. ms->b &= ~VIA1B_vADBInt;
  734. adb_bus->status = 0;
  735. adb_autopoll_unblock(adb_bus);
  736. }
  737. trace_via1_adb_receive(state == ADB_STATE_EVEN ? "EVEN" : " ODD",
  738. *data, (ms->b & VIA1B_vADBInt) ? "+" : "-",
  739. adb_bus->status, v1s->adb_data_in_index,
  740. v1s->adb_data_in_size);
  741. if (v1s->adb_data_in_index <= v1s->adb_data_in_size) {
  742. v1s->adb_data_in_index++;
  743. }
  744. break;
  745. }
  746. qemu_irq_raise(v1s->adb_data_ready);
  747. break;
  748. }
  749. }
  750. static void via1_adb_update(MOS6522Q800VIA1State *v1s)
  751. {
  752. MOS6522State *s = MOS6522(v1s);
  753. int oldstate, state;
  754. oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  755. state = (s->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  756. if (state != oldstate) {
  757. if (s->acr & VIA1ACR_vShiftOut) {
  758. /* output mode */
  759. adb_via_send(v1s, state, s->sr);
  760. } else {
  761. /* input mode */
  762. adb_via_receive(v1s, state, &s->sr);
  763. }
  764. }
  765. }
  766. static void via1_auxmode_update(MOS6522Q800VIA1State *v1s)
  767. {
  768. MOS6522State *s = MOS6522(v1s);
  769. int oldirq, irq;
  770. oldirq = (v1s->last_b & VIA1B_vMystery) ? 1 : 0;
  771. irq = (s->b & VIA1B_vMystery) ? 1 : 0;
  772. /* Check to see if the A/UX mode bit has changed */
  773. if (irq != oldirq) {
  774. trace_via1_auxmode(irq);
  775. qemu_set_irq(v1s->auxmode_irq, irq);
  776. /*
  777. * Clear the ADB interrupt. MacOS can leave VIA1B_vADBInt asserted
  778. * (low) if a poll sequence doesn't complete before NetBSD disables
  779. * interrupts upon boot. Fortunately NetBSD switches to the so-called
  780. * "A/UX" interrupt mode after it initialises, so we can use this as
  781. * a convenient place to clear the ADB interrupt for now.
  782. */
  783. s->b |= VIA1B_vADBInt;
  784. }
  785. }
  786. /*
  787. * Addresses and real values for TimeDBRA/TimeSCCB to allow timer calibration
  788. * to succeed (NOTE: both values have been multiplied by 3 to cope with the
  789. * speed of QEMU execution on a modern host
  790. */
  791. #define MACOS_TIMEDBRA 0xd00
  792. #define MACOS_TIMESCCB 0xd02
  793. #define MACOS_TIMEDBRA_VALUE (0x2a00 * 3)
  794. #define MACOS_TIMESCCB_VALUE (0x079d * 3)
  795. static bool via1_is_toolbox_timer_calibrated(void)
  796. {
  797. /*
  798. * Indicate whether the MacOS toolbox has been calibrated by checking
  799. * for the value of our magic constants
  800. */
  801. uint16_t timedbra = lduw_be_phys(&address_space_memory, MACOS_TIMEDBRA);
  802. uint16_t timesccdb = lduw_be_phys(&address_space_memory, MACOS_TIMESCCB);
  803. return (timedbra == MACOS_TIMEDBRA_VALUE &&
  804. timesccdb == MACOS_TIMESCCB_VALUE);
  805. }
  806. static void via1_timer_calibration_hack(MOS6522Q800VIA1State *v1s, int addr,
  807. uint64_t val, int size)
  808. {
  809. /*
  810. * Work around timer calibration to ensure we that we have non-zero and
  811. * known good values for TIMEDRBA and TIMESCCDB.
  812. *
  813. * This works by attempting to detect the reset and calibration sequence
  814. * of writes to VIA1
  815. */
  816. int old_timer_hack_state = v1s->timer_hack_state;
  817. switch (v1s->timer_hack_state) {
  818. case 0:
  819. if (addr == VIA_REG_PCR && val == 0x22) {
  820. /* VIA_REG_PCR: configure VIA1 edge triggering */
  821. v1s->timer_hack_state = 1;
  822. }
  823. break;
  824. case 1:
  825. if (addr == VIA_REG_T2CL && val == 0xc) {
  826. /* VIA_REG_T2CL: low byte of 1ms counter */
  827. if (!via1_is_toolbox_timer_calibrated()) {
  828. v1s->timer_hack_state = 2;
  829. } else {
  830. v1s->timer_hack_state = 0;
  831. }
  832. }
  833. break;
  834. case 2:
  835. if (addr == VIA_REG_T2CH && val == 0x3) {
  836. /*
  837. * VIA_REG_T2CH: high byte of 1ms counter (very likely at the
  838. * start of SETUPTIMEK)
  839. */
  840. if (!via1_is_toolbox_timer_calibrated()) {
  841. v1s->timer_hack_state = 3;
  842. } else {
  843. v1s->timer_hack_state = 0;
  844. }
  845. }
  846. break;
  847. case 3:
  848. if (addr == VIA_REG_IER && val == 0x20) {
  849. /*
  850. * VIA_REG_IER: update at end of SETUPTIMEK
  851. *
  852. * Timer calibration has finished: unfortunately the values in
  853. * TIMEDBRA (0xd00) and TIMESCCDB (0xd02) are so far out they
  854. * cause divide by zero errors.
  855. *
  856. * Update them with values obtained from a real Q800 but with
  857. * a x3 scaling factor which seems to work well
  858. */
  859. stw_be_phys(&address_space_memory, MACOS_TIMEDBRA,
  860. MACOS_TIMEDBRA_VALUE);
  861. stw_be_phys(&address_space_memory, MACOS_TIMESCCB,
  862. MACOS_TIMESCCB_VALUE);
  863. v1s->timer_hack_state = 4;
  864. }
  865. break;
  866. case 4:
  867. /*
  868. * This is the normal post-calibration timer state: we should
  869. * generally remain here unless we detect the A/UX calibration
  870. * loop, or a write to VIA_REG_PCR suggesting a reset
  871. */
  872. if (addr == VIA_REG_PCR && val == 0x22) {
  873. /* Looks like there has been a reset? */
  874. v1s->timer_hack_state = 1;
  875. }
  876. if (addr == VIA_REG_T2CL && val == 0xf0) {
  877. /* VIA_REG_T2CL: low byte of counter (A/UX) */
  878. v1s->timer_hack_state = 5;
  879. }
  880. break;
  881. case 5:
  882. if (addr == VIA_REG_T2CH && val == 0x3c) {
  883. /*
  884. * VIA_REG_T2CH: high byte of counter (A/UX). We are now extremely
  885. * likely to be in the A/UX timer calibration routine, so move to
  886. * the next state where we enable the calibration hack.
  887. */
  888. v1s->timer_hack_state = 6;
  889. } else if ((addr == VIA_REG_IER && val == 0x20) ||
  890. addr == VIA_REG_T2CH) {
  891. /* We're doing something else with the timer, not calibration */
  892. v1s->timer_hack_state = 0;
  893. }
  894. break;
  895. case 6:
  896. if ((addr == VIA_REG_IER && val == 0x20) || addr == VIA_REG_T2CH) {
  897. /* End of A/UX timer calibration routine, or another write */
  898. v1s->timer_hack_state = 7;
  899. } else {
  900. v1s->timer_hack_state = 0;
  901. }
  902. break;
  903. case 7:
  904. /*
  905. * This is the normal post-calibration timer state once both the
  906. * MacOS toolbox and A/UX have been calibrated, until we see a write
  907. * to VIA_REG_PCR to suggest a reset
  908. */
  909. if (addr == VIA_REG_PCR && val == 0x22) {
  910. /* Looks like there has been a reset? */
  911. v1s->timer_hack_state = 1;
  912. }
  913. break;
  914. default:
  915. g_assert_not_reached();
  916. }
  917. if (old_timer_hack_state != v1s->timer_hack_state) {
  918. trace_via1_timer_hack_state(v1s->timer_hack_state);
  919. }
  920. }
  921. static uint64_t mos6522_q800_via1_read(void *opaque, hwaddr addr, unsigned size)
  922. {
  923. MOS6522Q800VIA1State *s = MOS6522_Q800_VIA1(opaque);
  924. MOS6522State *ms = MOS6522(s);
  925. uint64_t ret;
  926. int64_t now;
  927. addr = (addr >> 9) & 0xf;
  928. ret = mos6522_read(ms, addr, size);
  929. switch (addr) {
  930. case VIA_REG_A:
  931. case VIA_REG_ANH:
  932. /* Quadra 800 Id */
  933. ret = (ret & ~VIA1A_CPUID_MASK) | VIA1A_CPUID_Q800;
  934. break;
  935. case VIA_REG_T2CH:
  936. if (s->timer_hack_state == 6) {
  937. /*
  938. * The A/UX timer calibration loop runs continuously until 2
  939. * consecutive iterations differ by at least 0x492 timer ticks.
  940. * Modern hosts execute the timer calibration loop so fast that
  941. * this situation never occurs causing a hang on boot. Use a
  942. * similar method to Shoebill which is to randomly add 0x500 to
  943. * the T2 counter value during calibration to enable it to
  944. * eventually succeed.
  945. */
  946. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  947. if (now & 1) {
  948. ret += 0x5;
  949. }
  950. }
  951. break;
  952. }
  953. return ret;
  954. }
  955. static void mos6522_q800_via1_write(void *opaque, hwaddr addr, uint64_t val,
  956. unsigned size)
  957. {
  958. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  959. MOS6522State *ms = MOS6522(v1s);
  960. int oldstate, state;
  961. int oldsr = ms->sr;
  962. addr = (addr >> 9) & 0xf;
  963. via1_timer_calibration_hack(v1s, addr, val, size);
  964. mos6522_write(ms, addr, val, size);
  965. switch (addr) {
  966. case VIA_REG_B:
  967. via1_rtc_update(v1s);
  968. via1_adb_update(v1s);
  969. via1_auxmode_update(v1s);
  970. v1s->last_b = ms->b;
  971. break;
  972. case VIA_REG_SR:
  973. {
  974. /*
  975. * NetBSD assumes it can send its first ADB command after sending
  976. * the ADB_BUSRESET command in ADB_STATE_NEW without changing the
  977. * state back to ADB_STATE_IDLE first as detailed in the ADB
  978. * protocol.
  979. *
  980. * Add a workaround to detect this condition at the start of ADB
  981. * enumeration and send the next command written to SR after a
  982. * ADB_BUSRESET onto the bus regardless, even if we don't detect a
  983. * state transition to ADB_STATE_NEW.
  984. *
  985. * Note that in my tests the NetBSD state machine takes one ADB
  986. * operation to recover which means the probe for an ADB device at
  987. * address 1 always fails. However since the first device is at
  988. * address 2 then this will work fine, without having to come up
  989. * with a more complicated and invasive solution.
  990. */
  991. oldstate = (v1s->last_b & VIA1B_vADB_StateMask) >>
  992. VIA1B_vADB_StateShift;
  993. state = (ms->b & VIA1B_vADB_StateMask) >> VIA1B_vADB_StateShift;
  994. if (oldstate == ADB_STATE_NEW && state == ADB_STATE_NEW &&
  995. (ms->acr & VIA1ACR_vShiftOut) &&
  996. oldsr == 0 /* ADB_BUSRESET */) {
  997. trace_via1_adb_netbsd_enum_hack();
  998. adb_via_send(v1s, state, ms->sr);
  999. }
  1000. }
  1001. break;
  1002. }
  1003. }
  1004. static const MemoryRegionOps mos6522_q800_via1_ops = {
  1005. .read = mos6522_q800_via1_read,
  1006. .write = mos6522_q800_via1_write,
  1007. .endianness = DEVICE_BIG_ENDIAN,
  1008. .valid = {
  1009. .min_access_size = 1,
  1010. .max_access_size = 4,
  1011. },
  1012. };
  1013. static uint64_t mos6522_q800_via2_read(void *opaque, hwaddr addr, unsigned size)
  1014. {
  1015. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  1016. MOS6522State *ms = MOS6522(s);
  1017. uint64_t val;
  1018. addr = (addr >> 9) & 0xf;
  1019. val = mos6522_read(ms, addr, size);
  1020. switch (addr) {
  1021. case VIA_REG_IFR:
  1022. /*
  1023. * On a Q800 an emulated VIA2 is integrated into the onboard logic. The
  1024. * expectation of most OSs is that the DRQ bit is live, rather than
  1025. * latched as it would be on a real VIA so do the same here.
  1026. *
  1027. * Note: DRQ is negative edge triggered
  1028. */
  1029. val &= ~VIA2_IRQ_SCSI_DATA;
  1030. val |= (~ms->last_irq_levels & VIA2_IRQ_SCSI_DATA);
  1031. break;
  1032. }
  1033. return val;
  1034. }
  1035. static void mos6522_q800_via2_write(void *opaque, hwaddr addr, uint64_t val,
  1036. unsigned size)
  1037. {
  1038. MOS6522Q800VIA2State *s = MOS6522_Q800_VIA2(opaque);
  1039. MOS6522State *ms = MOS6522(s);
  1040. addr = (addr >> 9) & 0xf;
  1041. mos6522_write(ms, addr, val, size);
  1042. }
  1043. static const MemoryRegionOps mos6522_q800_via2_ops = {
  1044. .read = mos6522_q800_via2_read,
  1045. .write = mos6522_q800_via2_write,
  1046. .endianness = DEVICE_BIG_ENDIAN,
  1047. .valid = {
  1048. .min_access_size = 1,
  1049. .max_access_size = 4,
  1050. },
  1051. };
  1052. static void via1_postload_update_cb(void *opaque, bool running, RunState state)
  1053. {
  1054. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  1055. qemu_del_vm_change_state_handler(v1s->vmstate);
  1056. v1s->vmstate = NULL;
  1057. pram_update(v1s);
  1058. }
  1059. static int via1_post_load(void *opaque, int version_id)
  1060. {
  1061. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(opaque);
  1062. if (v1s->blk) {
  1063. v1s->vmstate = qemu_add_vm_change_state_handler(
  1064. via1_postload_update_cb, v1s);
  1065. }
  1066. return 0;
  1067. }
  1068. /* VIA 1 */
  1069. static void mos6522_q800_via1_reset_hold(Object *obj, ResetType type)
  1070. {
  1071. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
  1072. MOS6522State *ms = MOS6522(v1s);
  1073. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  1074. ADBBusState *adb_bus = &v1s->adb_bus;
  1075. if (mdc->parent_phases.hold) {
  1076. mdc->parent_phases.hold(obj, type);
  1077. }
  1078. ms->timers[0].frequency = VIA_TIMER_FREQ;
  1079. ms->timers[1].frequency = VIA_TIMER_FREQ;
  1080. ms->b = VIA1B_vADB_StateMask | VIA1B_vADBInt | VIA1B_vRTCEnb;
  1081. /* ADB/RTC */
  1082. adb_set_autopoll_enabled(adb_bus, true);
  1083. v1s->cmd = REG_EMPTY;
  1084. v1s->alt = REG_EMPTY;
  1085. /* Timer calibration hack */
  1086. v1s->timer_hack_state = 0;
  1087. }
  1088. static void mos6522_q800_via1_realize(DeviceState *dev, Error **errp)
  1089. {
  1090. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(dev);
  1091. ADBBusState *adb_bus = &v1s->adb_bus;
  1092. struct tm tm;
  1093. int ret;
  1094. v1s->one_second_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, via1_one_second,
  1095. v1s);
  1096. via1_one_second_update(v1s);
  1097. v1s->sixty_hz_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, via1_sixty_hz,
  1098. v1s);
  1099. via1_sixty_hz_update(v1s);
  1100. qemu_get_timedate(&tm, 0);
  1101. v1s->tick_offset = (uint32_t)mktimegm(&tm) + RTC_OFFSET;
  1102. adb_register_autopoll_callback(adb_bus, adb_via_poll, v1s);
  1103. v1s->adb_data_ready = qdev_get_gpio_in(dev, VIA1_IRQ_ADB_READY_BIT);
  1104. if (v1s->blk) {
  1105. int64_t len = blk_getlength(v1s->blk);
  1106. if (len < 0) {
  1107. error_setg_errno(errp, -len,
  1108. "could not get length of backing image");
  1109. return;
  1110. }
  1111. ret = blk_set_perm(v1s->blk,
  1112. BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
  1113. BLK_PERM_ALL, errp);
  1114. if (ret < 0) {
  1115. return;
  1116. }
  1117. ret = blk_pread(v1s->blk, 0, sizeof(v1s->PRAM), v1s->PRAM, 0);
  1118. if (ret < 0) {
  1119. error_setg(errp, "can't read PRAM contents");
  1120. return;
  1121. }
  1122. }
  1123. }
  1124. static void mos6522_q800_via1_init(Object *obj)
  1125. {
  1126. MOS6522Q800VIA1State *v1s = MOS6522_Q800_VIA1(obj);
  1127. SysBusDevice *sbd = SYS_BUS_DEVICE(v1s);
  1128. memory_region_init_io(&v1s->via_mem, obj, &mos6522_q800_via1_ops, v1s,
  1129. "via1", VIA_SIZE);
  1130. sysbus_init_mmio(sbd, &v1s->via_mem);
  1131. /* ADB */
  1132. qbus_init((BusState *)&v1s->adb_bus, sizeof(v1s->adb_bus),
  1133. TYPE_ADB_BUS, DEVICE(v1s), "adb.0");
  1134. /* A/UX mode */
  1135. qdev_init_gpio_out(DEVICE(obj), &v1s->auxmode_irq, 1);
  1136. }
  1137. static const VMStateDescription vmstate_q800_via1 = {
  1138. .name = "q800-via1",
  1139. .version_id = 0,
  1140. .minimum_version_id = 0,
  1141. .post_load = via1_post_load,
  1142. .fields = (const VMStateField[]) {
  1143. VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA1State, 0, vmstate_mos6522,
  1144. MOS6522State),
  1145. VMSTATE_UINT8(last_b, MOS6522Q800VIA1State),
  1146. /* RTC */
  1147. VMSTATE_BUFFER(PRAM, MOS6522Q800VIA1State),
  1148. VMSTATE_UINT32(tick_offset, MOS6522Q800VIA1State),
  1149. VMSTATE_UINT8(data_out, MOS6522Q800VIA1State),
  1150. VMSTATE_INT32(data_out_cnt, MOS6522Q800VIA1State),
  1151. VMSTATE_UINT8(data_in, MOS6522Q800VIA1State),
  1152. VMSTATE_UINT8(data_in_cnt, MOS6522Q800VIA1State),
  1153. VMSTATE_UINT8(cmd, MOS6522Q800VIA1State),
  1154. VMSTATE_INT32(wprotect, MOS6522Q800VIA1State),
  1155. VMSTATE_INT32(alt, MOS6522Q800VIA1State),
  1156. /* ADB */
  1157. VMSTATE_INT32(adb_data_in_size, MOS6522Q800VIA1State),
  1158. VMSTATE_INT32(adb_data_in_index, MOS6522Q800VIA1State),
  1159. VMSTATE_INT32(adb_data_out_index, MOS6522Q800VIA1State),
  1160. VMSTATE_BUFFER(adb_data_in, MOS6522Q800VIA1State),
  1161. VMSTATE_BUFFER(adb_data_out, MOS6522Q800VIA1State),
  1162. VMSTATE_UINT8(adb_autopoll_cmd, MOS6522Q800VIA1State),
  1163. /* Timers */
  1164. VMSTATE_TIMER_PTR(one_second_timer, MOS6522Q800VIA1State),
  1165. VMSTATE_INT64(next_second, MOS6522Q800VIA1State),
  1166. VMSTATE_TIMER_PTR(sixty_hz_timer, MOS6522Q800VIA1State),
  1167. VMSTATE_INT64(next_sixty_hz, MOS6522Q800VIA1State),
  1168. /* Timer hack */
  1169. VMSTATE_INT32(timer_hack_state, MOS6522Q800VIA1State),
  1170. VMSTATE_END_OF_LIST()
  1171. }
  1172. };
  1173. static const Property mos6522_q800_via1_properties[] = {
  1174. DEFINE_PROP_DRIVE("drive", MOS6522Q800VIA1State, blk),
  1175. };
  1176. static void mos6522_q800_via1_class_init(ObjectClass *oc, void *data)
  1177. {
  1178. DeviceClass *dc = DEVICE_CLASS(oc);
  1179. ResettableClass *rc = RESETTABLE_CLASS(oc);
  1180. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  1181. dc->realize = mos6522_q800_via1_realize;
  1182. resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via1_reset_hold,
  1183. NULL, &mdc->parent_phases);
  1184. dc->vmsd = &vmstate_q800_via1;
  1185. device_class_set_props(dc, mos6522_q800_via1_properties);
  1186. }
  1187. static const TypeInfo mos6522_q800_via1_type_info = {
  1188. .name = TYPE_MOS6522_Q800_VIA1,
  1189. .parent = TYPE_MOS6522,
  1190. .instance_size = sizeof(MOS6522Q800VIA1State),
  1191. .instance_init = mos6522_q800_via1_init,
  1192. .class_init = mos6522_q800_via1_class_init,
  1193. };
  1194. /* VIA 2 */
  1195. static void mos6522_q800_via2_portB_write(MOS6522State *s)
  1196. {
  1197. if (s->dirb & VIA2B_vPower && (s->b & VIA2B_vPower) == 0) {
  1198. /* shutdown */
  1199. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  1200. }
  1201. }
  1202. static void mos6522_q800_via2_reset_hold(Object *obj, ResetType type)
  1203. {
  1204. MOS6522State *ms = MOS6522(obj);
  1205. MOS6522DeviceClass *mdc = MOS6522_GET_CLASS(ms);
  1206. if (mdc->parent_phases.hold) {
  1207. mdc->parent_phases.hold(obj, type);
  1208. }
  1209. ms->timers[0].frequency = VIA_TIMER_FREQ;
  1210. ms->timers[1].frequency = VIA_TIMER_FREQ;
  1211. ms->dirb = 0;
  1212. ms->b = 0;
  1213. ms->dira = 0;
  1214. ms->a = 0x7f;
  1215. }
  1216. static void via2_nubus_irq_request(void *opaque, int n, int level)
  1217. {
  1218. MOS6522Q800VIA2State *v2s = opaque;
  1219. MOS6522State *s = MOS6522(v2s);
  1220. qemu_irq irq = qdev_get_gpio_in(DEVICE(s), VIA2_IRQ_NUBUS_BIT);
  1221. if (level) {
  1222. /* Port A nubus IRQ inputs are active LOW */
  1223. s->a &= ~(1 << n);
  1224. } else {
  1225. s->a |= (1 << n);
  1226. }
  1227. /* Negative edge trigger */
  1228. qemu_set_irq(irq, !level);
  1229. }
  1230. static void mos6522_q800_via2_init(Object *obj)
  1231. {
  1232. MOS6522Q800VIA2State *v2s = MOS6522_Q800_VIA2(obj);
  1233. SysBusDevice *sbd = SYS_BUS_DEVICE(v2s);
  1234. memory_region_init_io(&v2s->via_mem, obj, &mos6522_q800_via2_ops, v2s,
  1235. "via2", VIA_SIZE);
  1236. sysbus_init_mmio(sbd, &v2s->via_mem);
  1237. qdev_init_gpio_in_named(DEVICE(obj), via2_nubus_irq_request, "nubus-irq",
  1238. VIA2_NUBUS_IRQ_NB);
  1239. }
  1240. static const VMStateDescription vmstate_q800_via2 = {
  1241. .name = "q800-via2",
  1242. .version_id = 0,
  1243. .minimum_version_id = 0,
  1244. .fields = (const VMStateField[]) {
  1245. VMSTATE_STRUCT(parent_obj, MOS6522Q800VIA2State, 0, vmstate_mos6522,
  1246. MOS6522State),
  1247. VMSTATE_END_OF_LIST()
  1248. }
  1249. };
  1250. static void mos6522_q800_via2_class_init(ObjectClass *oc, void *data)
  1251. {
  1252. DeviceClass *dc = DEVICE_CLASS(oc);
  1253. ResettableClass *rc = RESETTABLE_CLASS(oc);
  1254. MOS6522DeviceClass *mdc = MOS6522_CLASS(oc);
  1255. resettable_class_set_parent_phases(rc, NULL, mos6522_q800_via2_reset_hold,
  1256. NULL, &mdc->parent_phases);
  1257. dc->vmsd = &vmstate_q800_via2;
  1258. mdc->portB_write = mos6522_q800_via2_portB_write;
  1259. }
  1260. static const TypeInfo mos6522_q800_via2_type_info = {
  1261. .name = TYPE_MOS6522_Q800_VIA2,
  1262. .parent = TYPE_MOS6522,
  1263. .instance_size = sizeof(MOS6522Q800VIA2State),
  1264. .instance_init = mos6522_q800_via2_init,
  1265. .class_init = mos6522_q800_via2_class_init,
  1266. };
  1267. static void mac_via_register_types(void)
  1268. {
  1269. type_register_static(&mos6522_q800_via1_type_info);
  1270. type_register_static(&mos6522_q800_via2_type_info);
  1271. }
  1272. type_init(mac_via_register_types);