1/*	$NetBSD: obio.c,v 1.17 2021/08/07 16:19:13 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed for the NetBSD Project by
18 *      Allegro Networks, Inc., and Wasabi Systems, Inc.
19 * 4. The name of Allegro Networks, Inc. may not be used to endorse
20 *    or promote products derived from this software without specific prior
21 *    written permission.
22 * 5. The name of Wasabi Systems, Inc. may not be used to endorse
23 *    or promote products derived from this software without specific prior
24 *    written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
27 * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.17 2021/08/07 16:19:13 thorpej Exp $");
42
43#include "opt_marvell.h"
44
45#include <sys/param.h>
46#include <sys/types.h>
47#include <sys/extent.h>
48#include <sys/device.h>
49#include <sys/kernel.h>
50
51#include <dev/pci/pcivar.h>
52#include <dev/marvell/gtreg.h>
53#include <dev/marvell/gtvar.h>
54#include <dev/marvell/gtdevbusvar.h>
55#include <dev/marvell/marvellvar.h>
56
57#include <prop/proplib.h>
58
59#ifdef DEBUG
60#include <sys/systm.h>	/* for Debugger() */
61#endif
62
63#include "locators.h"
64
65
66static int obio_match(device_t, cfdata_t, void *);
67static void obio_attach(device_t, device_t, void *);
68
69static int obio_cfprint(void *, const char *);
70static int obio_cfsearch(device_t, cfdata_t, const int *, void *);
71
72
73struct obio_softc {
74	device_t sc_dev;
75	bus_space_tag_t sc_iot;
76};
77
78CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
79    obio_match, obio_attach, NULL, NULL);
80
81
82/* ARGSUSED */
83int
84obio_match(device_t parent, cfdata_t cf, void *aux)
85{
86	struct marvell_attach_args *mva = aux;
87
88	if (strcmp(mva->mva_name, cf->cf_name) != 0)
89		return 0;
90
91#define NUM_OBIO	5
92	if (mva->mva_unit == MVA_UNIT_DEFAULT ||
93	    mva->mva_unit > NUM_OBIO)
94		return 0;
95
96	return 1;
97}
98
99/* ARGSUSED */
100void
101obio_attach(device_t parent, device_t self, void *aux)
102{
103	struct obio_softc *sc = device_private(self);
104	struct marvell_attach_args *mva = aux;
105	prop_data_t bst;
106	uint32_t datal, datah;
107
108	aprint_naive("\n");
109	aprint_normal(": Device Bus\n");
110
111	sc->sc_dev = self;
112
113	if (gt_devbus_addr(parent, mva->mva_unit, &datal, &datah)) {
114		aprint_error_dev(self, "unknown unit number %d\n",
115		    mva->mva_unit);
116		return;
117	}
118
119	if (GT_LowAddr_GET(datal) > GT_HighAddr_GET(datah)) {
120		aprint_normal_dev(self, "disabled\n");
121		return;
122	}
123
124	bst = prop_dictionary_get(device_properties(sc->sc_dev), "bus-tag");
125	if (bst != NULL) {
126		KASSERT(prop_object_type(bst) == PROP_TYPE_DATA);
127		KASSERT(prop_data_size(bst) == sizeof(bus_space_tag_t));
128		memcpy(&sc->sc_iot, prop_data_data_nocopy(bst),
129		    sizeof(bus_space_tag_t));
130	} else
131		sc->sc_iot = mva->mva_iot;
132	if (sc->sc_iot == NULL) {
133		aprint_normal_dev(self, "unused\n");
134		return;
135	}
136
137	aprint_normal_dev(self, "addr %#x-%#x\n",
138	    GT_LowAddr_GET(datal), GT_HighAddr_GET(datah));
139
140	config_search(self, NULL,
141	    CFARGS(.search = obio_cfsearch));
142}
143
144
145int
146obio_cfprint(void *aux, const char *pnp)
147{
148	struct obio_attach_args *oa = aux;
149
150	if (pnp)
151		aprint_normal("%s at %s", oa->oa_name, pnp);
152	aprint_normal(" addr %#x size %#x", oa->oa_offset, oa->oa_size);
153	if (oa->oa_irq != OBIOCF_IRQ_DEFAULT)
154		aprint_normal(" irq %d", oa->oa_irq);
155
156	return UNCONF;
157}
158
159
160/* ARGSUSED */
161int
162obio_cfsearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
163{
164	struct obio_softc *sc = device_private(parent);
165	struct obio_attach_args oa;
166
167	oa.oa_name = cf->cf_name;
168	oa.oa_memt = sc->sc_iot;
169	oa.oa_offset = cf->cf_loc[OBIOCF_OFFSET];
170	oa.oa_size = cf->cf_loc[OBIOCF_SIZE];
171	oa.oa_irq = cf->cf_loc[OBIOCF_IRQ];
172
173	if (config_probe(parent, cf, &oa))
174		config_attach(parent, cf, &oa, obio_cfprint, CFARGS_NONE);
175
176	return 0;
177}
178