1*6e54367aSthorpej /* $NetBSD: ess_ofisa.c,v 1.32 2021/01/27 03:10:21 thorpej Exp $ */
2c0ca4781Saugustss
3c0ca4781Saugustss /*-
4c0ca4781Saugustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5c0ca4781Saugustss * All rights reserved.
6c0ca4781Saugustss *
7c0ca4781Saugustss * This code is derived from software contributed to The NetBSD Foundation
8c0ca4781Saugustss * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9c0ca4781Saugustss * NASA Ames Research Center.
10c0ca4781Saugustss *
11c0ca4781Saugustss * Redistribution and use in source and binary forms, with or without
12c0ca4781Saugustss * modification, are permitted provided that the following conditions
13c0ca4781Saugustss * are met:
14c0ca4781Saugustss * 1. Redistributions of source code must retain the above copyright
15c0ca4781Saugustss * notice, this list of conditions and the following disclaimer.
16c0ca4781Saugustss * 2. Redistributions in binary form must reproduce the above copyright
17c0ca4781Saugustss * notice, this list of conditions and the following disclaimer in the
18c0ca4781Saugustss * documentation and/or other materials provided with the distribution.
19c0ca4781Saugustss *
20c0ca4781Saugustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21c0ca4781Saugustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22c0ca4781Saugustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23c0ca4781Saugustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24c0ca4781Saugustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25c0ca4781Saugustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26c0ca4781Saugustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27c0ca4781Saugustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28c0ca4781Saugustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29c0ca4781Saugustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30c0ca4781Saugustss * POSSIBILITY OF SUCH DAMAGE.
31c0ca4781Saugustss */
32c0ca4781Saugustss
33ab5d9d2bSlukem #include <sys/cdefs.h>
34*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: ess_ofisa.c,v 1.32 2021/01/27 03:10:21 thorpej Exp $");
35ab5d9d2bSlukem
36c0ca4781Saugustss #include <sys/param.h>
37c0ca4781Saugustss #include <sys/systm.h>
38c0ca4781Saugustss #include <sys/device.h>
39c0ca4781Saugustss
40a2a38285Sad #include <sys/bus.h>
41a2a38285Sad #include <sys/intr.h>
42c0ca4781Saugustss
43c0ca4781Saugustss #include <sys/audioio.h>
44e622eac4Sisaki #include <dev/audio/audio_if.h>
45c0ca4781Saugustss
46c0ca4781Saugustss #include <dev/ofw/openfirm.h>
47c0ca4781Saugustss #include <dev/isa/isavar.h>
48c0ca4781Saugustss #include <dev/ofisa/ofisavar.h>
49c0ca4781Saugustss
50c0ca4781Saugustss #include <dev/isa/essreg.h>
51c0ca4781Saugustss #include <dev/isa/essvar.h>
52c0ca4781Saugustss
537cf29912Scegger int ess_ofisa_match(device_t, cfdata_t, void *);
547cf29912Scegger void ess_ofisa_attach(device_t, device_t, void *);
55c0ca4781Saugustss
569cb526aaStsutsui CFATTACH_DECL_NEW(ess_ofisa, sizeof(struct ess_softc),
57c9b3657cSthorpej ess_ofisa_match, ess_ofisa_attach, NULL, NULL);
58c0ca4781Saugustss
591ebde598Sthorpej static const struct device_compatible_entry compat_data[] = {
601ebde598Sthorpej { .compat = "ESST,es1887-codec" }, /* ESS 1887 */
611ebde598Sthorpej { .compat = "ESST,es1888-codec" }, /* ESS 1888 */
621ebde598Sthorpej { .compat = "ESST,es888-codec" }, /* ESS 888 */
6318f3098cSthorpej DEVICE_COMPAT_EOL
641ebde598Sthorpej };
651ebde598Sthorpej
66c0ca4781Saugustss int
ess_ofisa_match(device_t parent,cfdata_t cf,void * aux)677cf29912Scegger ess_ofisa_match(device_t parent, cfdata_t cf, void *aux)
68c0ca4781Saugustss {
69c0ca4781Saugustss struct ofisa_attach_args *aa = aux;
70c0ca4781Saugustss
711ebde598Sthorpej /* beat generic SB */
72*6e54367aSthorpej return of_compatible_match(aa->oba.oba_phandle, compat_data) ? 10 : 0;
73c0ca4781Saugustss }
74c0ca4781Saugustss
75c0ca4781Saugustss void
ess_ofisa_attach(device_t parent,device_t self,void * aux)767cf29912Scegger ess_ofisa_attach(device_t parent, device_t self, void *aux)
77c0ca4781Saugustss {
78838ee1e0Sthorpej struct ess_softc *sc = device_private(self);
79c0ca4781Saugustss struct ofisa_attach_args *aa = aux;
80c0ca4781Saugustss struct ofisa_reg_desc reg;
81c0ca4781Saugustss struct ofisa_intr_desc intr[2];
82c0ca4781Saugustss struct ofisa_dma_desc dma[2];
83c0ca4781Saugustss int n, ndrq;
84c0ca4781Saugustss
859cb526aaStsutsui sc->sc_dev = self;
869cb526aaStsutsui
87c0ca4781Saugustss /*
88c0ca4781Saugustss * We're living on an OFW. We have to ask the OFW what our
89c0ca4781Saugustss * registers and interrupts properties look like.
90c0ca4781Saugustss *
91c0ca4781Saugustss * We expect:
92c0ca4781Saugustss *
93c0ca4781Saugustss * 1 i/o register region
946b407a79Saugustss * 1 or 2 interrupts
951ffa7b76Swiz * 2 DMA channels
96c0ca4781Saugustss */
97c0ca4781Saugustss
98c0ca4781Saugustss n = ofisa_reg_get(aa->oba.oba_phandle, ®, 1);
99c0ca4781Saugustss if (n != 1) {
1009cb526aaStsutsui aprint_error(": error getting register data\n");
101c0ca4781Saugustss return;
102c0ca4781Saugustss }
103c0ca4781Saugustss if (reg.type != OFISA_REG_TYPE_IO) {
1049cb526aaStsutsui aprint_error(": register type not i/o\n");
105c0ca4781Saugustss return;
106c0ca4781Saugustss }
107c0ca4781Saugustss if (reg.len != ESS_NPORT) {
1089cb526aaStsutsui aprint_error(": weird register size (%lu, expected %d)\n",
109c0ca4781Saugustss (unsigned long)reg.len, ESS_NPORT);
110c0ca4781Saugustss return;
111c0ca4781Saugustss }
112c0ca4781Saugustss
113c0ca4781Saugustss n = ofisa_intr_get(aa->oba.oba_phandle, intr, 2);
1146b407a79Saugustss if (n == 1) {
1159e041fd8Smycroft sc->sc_audio1.irq = intr[0].irq;
1169e041fd8Smycroft sc->sc_audio1.ist = intr[0].share;
1179e041fd8Smycroft sc->sc_audio2.irq = intr[0].irq;
1189e041fd8Smycroft sc->sc_audio2.ist = intr[0].share;
1196b407a79Saugustss } else if (n == 2) {
1209e041fd8Smycroft sc->sc_audio1.irq = intr[0].irq;
1219e041fd8Smycroft sc->sc_audio1.ist = intr[0].share;
1229e041fd8Smycroft sc->sc_audio2.irq = intr[1].irq;
1239e041fd8Smycroft sc->sc_audio2.ist = intr[1].share;
1246b407a79Saugustss } else {
1259cb526aaStsutsui aprint_error(": error getting interrupt data\n");
1266b407a79Saugustss return;
1276b407a79Saugustss }
128c0ca4781Saugustss
129c0ca4781Saugustss ndrq = ofisa_dma_get(aa->oba.oba_phandle, dma, 2);
130c0ca4781Saugustss if (ndrq != 2) {
1319cb526aaStsutsui aprint_error(": error getting DMA data\n");
132c0ca4781Saugustss return;
133c0ca4781Saugustss }
1349e041fd8Smycroft sc->sc_audio1.drq = dma[0].drq;
1359e041fd8Smycroft sc->sc_audio2.drq = dma[1].drq;
136c0ca4781Saugustss
137c0ca4781Saugustss sc->sc_ic = aa->ic;
138c0ca4781Saugustss
139c0ca4781Saugustss sc->sc_iot = aa->iot;
140c0ca4781Saugustss sc->sc_iobase = reg.addr;
141c0ca4781Saugustss if (bus_space_map(sc->sc_iot, sc->sc_iobase, reg.len, 0,
142c0ca4781Saugustss &sc->sc_ioh)) {
1439cb526aaStsutsui aprint_error(": unable to map register space\n");
144c0ca4781Saugustss return;
145c0ca4781Saugustss }
146c0ca4781Saugustss
14746b2a2fcSnathanw /*
14846b2a2fcSnathanw * The Shark firmware doesn't program the ESS ISA address registers.
14946b2a2fcSnathanw * Do that here instead of inside essmatch() since we want to defer
15046b2a2fcSnathanw * to the firmware on other platforms.
15146b2a2fcSnathanw */
15246b2a2fcSnathanw if (ess_config_addr(sc))
15346b2a2fcSnathanw return;
154c0ca4781Saugustss if (essmatch(sc) == 0) {
1559cb526aaStsutsui aprint_error(": essmatch failed\n");
156c0ca4781Saugustss return;
157c0ca4781Saugustss }
158c0ca4781Saugustss
159c035549eSchristos ofisa_print_model(self, aa->oba.oba_phandle);
160c0ca4781Saugustss
1617ba5b8cdSdrochner essattach(sc, 0);
162c0ca4781Saugustss }
163