1*c7fb772bSthorpej /* $NetBSD: acemidi.c,v 1.17 2021/08/07 16:19:15 thorpej Exp $ */
26c7d3e35Sbjh21
36c7d3e35Sbjh21 /*-
46c7d3e35Sbjh21 * Copyright (c) 2001 Ben Harris
56c7d3e35Sbjh21 * All rights reserved.
66c7d3e35Sbjh21 *
76c7d3e35Sbjh21 * Redistribution and use in source and binary forms, with or without
86c7d3e35Sbjh21 * modification, are permitted provided that the following conditions
96c7d3e35Sbjh21 * are met:
106c7d3e35Sbjh21 * 1. Redistributions of source code must retain the above copyright
116c7d3e35Sbjh21 * notice, this list of conditions and the following disclaimer.
126c7d3e35Sbjh21 * 2. Redistributions in binary form must reproduce the above copyright
136c7d3e35Sbjh21 * notice, this list of conditions and the following disclaimer in the
146c7d3e35Sbjh21 * documentation and/or other materials provided with the distribution.
156c7d3e35Sbjh21 * 3. The name of the author may not be used to endorse or promote products
166c7d3e35Sbjh21 * derived from this software without specific prior written permission.
176c7d3e35Sbjh21 *
186c7d3e35Sbjh21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196c7d3e35Sbjh21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
206c7d3e35Sbjh21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
216c7d3e35Sbjh21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
226c7d3e35Sbjh21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
236c7d3e35Sbjh21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246c7d3e35Sbjh21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256c7d3e35Sbjh21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266c7d3e35Sbjh21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
276c7d3e35Sbjh21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286c7d3e35Sbjh21 */
296c7d3e35Sbjh21
309ab2c471Slukem #include <sys/cdefs.h>
31*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: acemidi.c,v 1.17 2021/08/07 16:19:15 thorpej Exp $");
326c7d3e35Sbjh21
339ab2c471Slukem #include <sys/param.h>
346c7d3e35Sbjh21
356c7d3e35Sbjh21 #include <sys/device.h>
366c7d3e35Sbjh21 #include <sys/systm.h>
376c7d3e35Sbjh21
38a2a38285Sad #include <sys/bus.h>
396c7d3e35Sbjh21
406c7d3e35Sbjh21 #include <dev/podulebus/podulebus.h>
416c7d3e35Sbjh21 #include <dev/podulebus/podules.h>
426c7d3e35Sbjh21 #include <dev/podulebus/acemidireg.h>
436c7d3e35Sbjh21
446c7d3e35Sbjh21 #include <sys/termios.h>
456c7d3e35Sbjh21 #include <dev/ic/comvar.h>
466c7d3e35Sbjh21 #include <dev/ic/comreg.h>
476c7d3e35Sbjh21
486c7d3e35Sbjh21 struct com_acemidi_softc {
496c7d3e35Sbjh21 struct com_softc sc_com;
506c7d3e35Sbjh21 struct evcnt sc_intrcnt;
516c7d3e35Sbjh21 };
526c7d3e35Sbjh21
53607ead0eScube static int acemidi_match(device_t, cfdata_t , void *);
54607ead0eScube static void acemidi_attach(device_t, device_t, void *);
55607ead0eScube static int com_acemidi_match(device_t, cfdata_t, void *);
56607ead0eScube static void com_acemidi_attach(device_t, device_t, void *);
576c7d3e35Sbjh21
58607ead0eScube CFATTACH_DECL_NEW(acemidi, 0,
59b75a007dSthorpej acemidi_match, acemidi_attach, NULL, NULL);
606c7d3e35Sbjh21
61607ead0eScube CFATTACH_DECL_NEW(com_acemidi, sizeof(struct com_acemidi_softc),
62b75a007dSthorpej com_acemidi_match, com_acemidi_attach, NULL, NULL);
636c7d3e35Sbjh21
646c7d3e35Sbjh21 static int
acemidi_match(device_t parent,cfdata_t cf,void * aux)65607ead0eScube acemidi_match(device_t parent, cfdata_t cf, void *aux)
666c7d3e35Sbjh21 {
676c7d3e35Sbjh21 struct podulebus_attach_args *pa = aux;
686c7d3e35Sbjh21
6920518673Sbjh21 if (pa->pa_product == PODULE_MIDICONNECT)
706c7d3e35Sbjh21 return 1;
716c7d3e35Sbjh21 return 0;
726c7d3e35Sbjh21 }
736c7d3e35Sbjh21
746c7d3e35Sbjh21 static void
acemidi_attach(device_t parent,device_t self,void * aux)75607ead0eScube acemidi_attach(device_t parent, device_t self, void *aux)
766c7d3e35Sbjh21 {
77838ee1e0Sthorpej /* struct acemidi_softc *sc = device_private(self); */
786c7d3e35Sbjh21 /* struct podulebus_attach_args *pa = aux; */
796c7d3e35Sbjh21
806c7d3e35Sbjh21 printf("\n");
81*c7fb772bSthorpej config_found(self, aux, NULL, CFARGS_NONE);
826c7d3e35Sbjh21 }
836c7d3e35Sbjh21
846c7d3e35Sbjh21 static int
com_acemidi_match(device_t parent,cfdata_t cf,void * aux)85607ead0eScube com_acemidi_match(device_t parent, cfdata_t cf, void *aux)
866c7d3e35Sbjh21 {
876c7d3e35Sbjh21
886c7d3e35Sbjh21 return 1;
896c7d3e35Sbjh21 }
906c7d3e35Sbjh21
916c7d3e35Sbjh21 static void
com_acemidi_attach(device_t parent,device_t self,void * aux)92607ead0eScube com_acemidi_attach(device_t parent, device_t self, void *aux)
936c7d3e35Sbjh21 {
94838ee1e0Sthorpej struct com_acemidi_softc *sc = device_private(self);
956c7d3e35Sbjh21 struct com_softc *csc = &sc->sc_com;
966c7d3e35Sbjh21 struct podulebus_attach_args *pa = aux;
9734537908Sgdamore bus_space_handle_t ioh;
9834537908Sgdamore bus_space_tag_t iot;
9934537908Sgdamore bus_addr_t iobase;
1006c7d3e35Sbjh21
10134537908Sgdamore iot = pa->pa_fast_t;
10234537908Sgdamore iobase = pa->pa_fast_base + ACEMIDI_16550_BASE;
10334537908Sgdamore
10434537908Sgdamore bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh);
1056d487047Sthorpej com_init_regs(&csc->sc_regs, iot, ioh, iobase);
10634537908Sgdamore
1076c7d3e35Sbjh21 csc->sc_frequency = ACEMIDI_16550_FREQ;
1086c7d3e35Sbjh21
1096c7d3e35Sbjh21 com_attach_subr(csc);
1106c7d3e35Sbjh21
1116c7d3e35Sbjh21 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
112607ead0eScube device_xname(self), "intr");
1136c7d3e35Sbjh21 podulebus_irq_establish(pa->pa_ih, IPL_SERIAL, comintr, sc,
1146c7d3e35Sbjh21 &sc->sc_intrcnt);
1156c7d3e35Sbjh21 }
116f6f3c9b3Sbjh21
117f6f3c9b3Sbjh21 /*
118f6f3c9b3Sbjh21 * Stray IRQ bug:
119f6f3c9b3Sbjh21 *
120f6f3c9b3Sbjh21 * Occasionally, when receiving, we get a stray IRQ. Sometimes, the interrupt
121f6f3c9b3Sbjh21 * bit on the unixbp reads as clear. In any case, comintr() gets an IIR
122f6f3c9b3Sbjh21 * of 0xc1 (no interrupts pending).
123f6f3c9b3Sbjh21 *
124f6f3c9b3Sbjh21 * The behaviour can be observed with a logic probe:
125f6f3c9b3Sbjh21 *
126f6f3c9b3Sbjh21 * Channel 1 to PIRQ* (pin 19 on IC3 on A540 backplane)
127f6f3c9b3Sbjh21 * Channel 2 to INTR on 16550
128f6f3c9b3Sbjh21 * trigger on ch1 low, ch2 falling
129f6f3c9b3Sbjh21 * 2 us/div
130f6f3c9b3Sbjh21 *
131f6f3c9b3Sbjh21 * This catches cases where the 16550 de-asserts the interrupt before
132f6f3c9b3Sbjh21 * irq_handler is entered and disables the interrupt at unixbp (by calling
133f6f3c9b3Sbjh21 * splhigh()).
134f6f3c9b3Sbjh21 *
135f6f3c9b3Sbjh21 * This gets us 5us pulses on INTR and PIRQ*. Now to work out why.
136f6f3c9b3Sbjh21 *
137f6f3c9b3Sbjh21 * Connecting channel 3 to the CS2* pin on the 16550 shows it high throughout,
138f6f3c9b3Sbjh21 * so the interrupt isn't being cleared by the host. MR, similarly, is low
139f6f3c9b3Sbjh21 * throughout, so it's not being cleared by a reset.
140f6f3c9b3Sbjh21 */
141