1/*	$NetBSD: sgc.c,v 1.4 2021/08/07 16:18:53 thorpej Exp $	*/
2/*	$OpenBSD: sgc.c,v 1.6 2010/04/15 20:35:21 miod Exp $	*/
3
4/*
5 * Copyright (c) 2005, Miodrag Vallat
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30/*
31 * SGC bus attachment and mapping glue.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/device.h>
37#include <sys/kernel.h>
38#include <sys/bus.h>
39#include <sys/cpu.h>
40
41#include <machine/autoconf.h>
42#include <machine/hp300spu.h>
43
44#include <hp300/dev/sgcreg.h>
45#include <hp300/dev/sgcvar.h>
46
47#ifdef SGC_DEBUG
48#define DPRINTF(x)	printf x
49#else
50#define DPRINTF(x)	do {} while (/* CONSTCOND */ 0)
51#endif
52
53struct sgc_softc {
54	device_t sc_dev;
55	struct bus_space_tag sc_tag;
56};
57
58static int	sgcmatch(device_t, cfdata_t, void *);
59static void	sgcattach(device_t, device_t, void *);
60static int	sgcprint(void *, const char *);
61
62CFATTACH_DECL_NEW(sgc, sizeof(struct sgc_softc),
63    sgcmatch, sgcattach, NULL, NULL);
64
65int
66sgcmatch(device_t parent, cfdata_t cf, void *aux)
67{
68	static int sgc_matched = 0;
69
70	/* Allow only one instance. */
71	if (sgc_matched)
72		return 0;
73
74	/*
75	 * Leave out machines which can not have an SGC bus.
76	 */
77
78	switch (machineid) {
79#if 0
80	case HP_362:
81	case HP_382:
82#endif
83	case HP_400:
84	case HP_425:
85	case HP_433:
86		return sgc_matched = 1;
87	default:
88		return 0;
89	}
90}
91
92void
93sgcattach(device_t parent, device_t self, void *aux)
94{
95	struct sgc_softc *sc;
96	struct sgc_attach_args saa;
97	paddr_t pa;
98	void *va;
99	int slot, rv;
100	bus_space_tag_t bst;
101	bus_space_handle_t bsh;
102
103	sc = device_private(self);
104	sc->sc_dev = self;
105	aprint_normal("\n");
106
107	bst = &sc->sc_tag;
108	memset(bst, 0, sizeof(struct bus_space_tag));
109	bst->bustype = HP300_BUS_SPACE_SGC;
110
111	for (slot = 0; slot < SGC_NSLOTS; slot++) {
112		pa = sgc_slottopa(slot);
113		if (bus_space_map(bst, pa, PAGE_SIZE, 0, &bsh) != 0) {
114			aprint_error_dev(self, "can't map slot %d\n", slot);
115			continue;
116		}
117		va = bus_space_vaddr(bst, bsh);
118
119		/* Check for hardware. */
120		rv = badaddr(va);
121		bus_space_unmap(bst, bsh, PAGE_SIZE);
122
123		if (rv != 0) {
124			DPRINTF(("%s: no valid device at slot %d\n",
125			    device_xname(self), slot));
126			continue;
127		}
128
129		memset(&saa, 0, sizeof(saa));
130		saa.saa_iot = bst;
131		saa.saa_slot = slot;
132
133		/* Attach matching device. */
134		config_found(self, &saa, sgcprint, CFARGS_NONE);
135	}
136}
137
138int
139sgcprint(void *aux, const char *pnp)
140{
141	struct sgc_attach_args *saa = aux;
142
143	if (pnp)
144		aprint_normal("unknown SGC card at %s", pnp);
145	aprint_normal(" slot %d", saa->saa_slot);
146	return UNCONF;
147}
148
149/*
150 * Convert a slot number to a system physical address.
151 * This is needed for bus_space.
152 */
153paddr_t
154sgc_slottopa(int slot)
155{
156	paddr_t rval;
157
158	if (slot < 0 || slot >= SGC_NSLOTS)
159		rval = 0;
160	else
161		rval = SGC_BASE + (slot * SGC_DEVSIZE);
162
163	return rval;
164}
165