1*c7fb772bSthorpej /* $NetBSD: obio.c,v 1.17 2021/08/07 16:19:13 thorpej Exp $ */
27f87f176Smatt
37f87f176Smatt /*
47f87f176Smatt * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
57f87f176Smatt * All rights reserved.
67f87f176Smatt *
77f87f176Smatt * Redistribution and use in source and binary forms, with or without
87f87f176Smatt * modification, are permitted provided that the following conditions
97f87f176Smatt * are met:
107f87f176Smatt * 1. Redistributions of source code must retain the above copyright
117f87f176Smatt * notice, this list of conditions and the following disclaimer.
127f87f176Smatt * 2. Redistributions in binary form must reproduce the above copyright
137f87f176Smatt * notice, this list of conditions and the following disclaimer in the
147f87f176Smatt * documentation and/or other materials provided with the distribution.
157f87f176Smatt * 3. All advertising materials mentioning features or use of this software
167f87f176Smatt * must display the following acknowledgement:
177f87f176Smatt * This product includes software developed for the NetBSD Project by
187f87f176Smatt * Allegro Networks, Inc., and Wasabi Systems, Inc.
197f87f176Smatt * 4. The name of Allegro Networks, Inc. may not be used to endorse
207f87f176Smatt * or promote products derived from this software without specific prior
217f87f176Smatt * written permission.
227f87f176Smatt * 5. The name of Wasabi Systems, Inc. may not be used to endorse
237f87f176Smatt * or promote products derived from this software without specific prior
247f87f176Smatt * written permission.
257f87f176Smatt *
267f87f176Smatt * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
277f87f176Smatt * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
287f87f176Smatt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
297f87f176Smatt * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
307f87f176Smatt * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
317f87f176Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
327f87f176Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
337f87f176Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
347f87f176Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
357f87f176Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
367f87f176Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
377f87f176Smatt * POSSIBILITY OF SUCH DAMAGE.
387f87f176Smatt */
397f87f176Smatt
40365cbd94Slukem #include <sys/cdefs.h>
41*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.17 2021/08/07 16:19:13 thorpej Exp $");
42365cbd94Slukem
437f87f176Smatt #include "opt_marvell.h"
447f87f176Smatt
457f87f176Smatt #include <sys/param.h>
467f87f176Smatt #include <sys/types.h>
477f87f176Smatt #include <sys/extent.h>
487f87f176Smatt #include <sys/device.h>
497f87f176Smatt #include <sys/kernel.h>
507f87f176Smatt
517f87f176Smatt #include <dev/pci/pcivar.h>
527f87f176Smatt #include <dev/marvell/gtreg.h>
537f87f176Smatt #include <dev/marvell/gtvar.h>
54a748aedcSkiyohara #include <dev/marvell/gtdevbusvar.h>
55a748aedcSkiyohara #include <dev/marvell/marvellvar.h>
56a748aedcSkiyohara
57a748aedcSkiyohara #include <prop/proplib.h>
587f87f176Smatt
597f87f176Smatt #ifdef DEBUG
607f87f176Smatt #include <sys/systm.h> /* for Debugger() */
617f87f176Smatt #endif
627f87f176Smatt
6396b589fcSdrochner #include "locators.h"
6496b589fcSdrochner
65a748aedcSkiyohara
66a748aedcSkiyohara static int obio_match(device_t, cfdata_t, void *);
67a748aedcSkiyohara static void obio_attach(device_t, device_t, void *);
68a748aedcSkiyohara
697f87f176Smatt static int obio_cfprint(void *, const char *);
70a748aedcSkiyohara static int obio_cfsearch(device_t, cfdata_t, const int *, void *);
71a748aedcSkiyohara
727f87f176Smatt
737f87f176Smatt struct obio_softc {
74a748aedcSkiyohara device_t sc_dev;
75a748aedcSkiyohara bus_space_tag_t sc_iot;
767f87f176Smatt };
777f87f176Smatt
78a748aedcSkiyohara CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
79a748aedcSkiyohara obio_match, obio_attach, NULL, NULL);
807f87f176Smatt
817f87f176Smatt
82a748aedcSkiyohara /* ARGSUSED */
83a748aedcSkiyohara int
obio_match(device_t parent,cfdata_t cf,void * aux)84a748aedcSkiyohara obio_match(device_t parent, cfdata_t cf, void *aux)
85a748aedcSkiyohara {
86a748aedcSkiyohara struct marvell_attach_args *mva = aux;
877f87f176Smatt
88a748aedcSkiyohara if (strcmp(mva->mva_name, cf->cf_name) != 0)
89a748aedcSkiyohara return 0;
90a748aedcSkiyohara
91a748aedcSkiyohara #define NUM_OBIO 5
922c444bf7Skiyohara if (mva->mva_unit == MVA_UNIT_DEFAULT ||
93a748aedcSkiyohara mva->mva_unit > NUM_OBIO)
94a748aedcSkiyohara return 0;
95a748aedcSkiyohara
96a748aedcSkiyohara return 1;
97a748aedcSkiyohara }
98a748aedcSkiyohara
99a748aedcSkiyohara /* ARGSUSED */
100a748aedcSkiyohara void
obio_attach(device_t parent,device_t self,void * aux)101a748aedcSkiyohara obio_attach(device_t parent, device_t self, void *aux)
102a748aedcSkiyohara {
103a748aedcSkiyohara struct obio_softc *sc = device_private(self);
104a748aedcSkiyohara struct marvell_attach_args *mva = aux;
105a748aedcSkiyohara prop_data_t bst;
106a748aedcSkiyohara uint32_t datal, datah;
107a748aedcSkiyohara
108a748aedcSkiyohara aprint_naive("\n");
109a748aedcSkiyohara aprint_normal(": Device Bus\n");
110a748aedcSkiyohara
111a748aedcSkiyohara sc->sc_dev = self;
112a748aedcSkiyohara
113a748aedcSkiyohara if (gt_devbus_addr(parent, mva->mva_unit, &datal, &datah)) {
114a748aedcSkiyohara aprint_error_dev(self, "unknown unit number %d\n",
115a748aedcSkiyohara mva->mva_unit);
116a748aedcSkiyohara return;
117a748aedcSkiyohara }
118a748aedcSkiyohara
119a748aedcSkiyohara if (GT_LowAddr_GET(datal) > GT_HighAddr_GET(datah)) {
120a748aedcSkiyohara aprint_normal_dev(self, "disabled\n");
121a748aedcSkiyohara return;
122a748aedcSkiyohara }
123a748aedcSkiyohara
124a748aedcSkiyohara bst = prop_dictionary_get(device_properties(sc->sc_dev), "bus-tag");
125a748aedcSkiyohara if (bst != NULL) {
126a748aedcSkiyohara KASSERT(prop_object_type(bst) == PROP_TYPE_DATA);
127a748aedcSkiyohara KASSERT(prop_data_size(bst) == sizeof(bus_space_tag_t));
128a748aedcSkiyohara memcpy(&sc->sc_iot, prop_data_data_nocopy(bst),
129a748aedcSkiyohara sizeof(bus_space_tag_t));
130a748aedcSkiyohara } else
131a748aedcSkiyohara sc->sc_iot = mva->mva_iot;
132a748aedcSkiyohara if (sc->sc_iot == NULL) {
133a748aedcSkiyohara aprint_normal_dev(self, "unused\n");
134a748aedcSkiyohara return;
135a748aedcSkiyohara }
136a748aedcSkiyohara
137a748aedcSkiyohara aprint_normal_dev(self, "addr %#x-%#x\n",
138a748aedcSkiyohara GT_LowAddr_GET(datal), GT_HighAddr_GET(datah));
139a748aedcSkiyohara
1402685996bSthorpej config_search(self, NULL,
141*c7fb772bSthorpej CFARGS(.search = obio_cfsearch));
142a748aedcSkiyohara }
143a748aedcSkiyohara
1447f87f176Smatt
1457f87f176Smatt int
obio_cfprint(void * aux,const char * pnp)1467f87f176Smatt obio_cfprint(void *aux, const char *pnp)
1477f87f176Smatt {
1487f87f176Smatt struct obio_attach_args *oa = aux;
1497f87f176Smatt
150a748aedcSkiyohara if (pnp)
1517f87f176Smatt aprint_normal("%s at %s", oa->oa_name, pnp);
152a748aedcSkiyohara aprint_normal(" addr %#x size %#x", oa->oa_offset, oa->oa_size);
15396b589fcSdrochner if (oa->oa_irq != OBIOCF_IRQ_DEFAULT)
154caff5255Smatt aprint_normal(" irq %d", oa->oa_irq);
1557f87f176Smatt
156a748aedcSkiyohara return UNCONF;
1577f87f176Smatt }
1587f87f176Smatt
1597f87f176Smatt
160a748aedcSkiyohara /* ARGSUSED */
1617f87f176Smatt int
obio_cfsearch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)162a748aedcSkiyohara obio_cfsearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
1637f87f176Smatt {
164a748aedcSkiyohara struct obio_softc *sc = device_private(parent);
1657f87f176Smatt struct obio_attach_args oa;
1667f87f176Smatt
1677f87f176Smatt oa.oa_name = cf->cf_name;
168a748aedcSkiyohara oa.oa_memt = sc->sc_iot;
16996b589fcSdrochner oa.oa_offset = cf->cf_loc[OBIOCF_OFFSET];
17096b589fcSdrochner oa.oa_size = cf->cf_loc[OBIOCF_SIZE];
17196b589fcSdrochner oa.oa_irq = cf->cf_loc[OBIOCF_IRQ];
1727f87f176Smatt
1732685996bSthorpej if (config_probe(parent, cf, &oa))
174*c7fb772bSthorpej config_attach(parent, cf, &oa, obio_cfprint, CFARGS_NONE);
1757f87f176Smatt
176a748aedcSkiyohara return 0;
1777f87f176Smatt }
178