xref: /netbsd/sys/arch/hpcmips/dev/plumiobus.c (revision c4a72b64)
1 /*	$NetBSD: plumiobus.c,v 1.8 2002/10/02 05:26:46 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #define PLUMIOBUSDEBUG
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 
49 #include <hpcmips/tx/tx39var.h>
50 #include <hpcmips/dev/plumvar.h>
51 #include <hpcmips/dev/plumicuvar.h>
52 #include <hpcmips/dev/plumpowervar.h>
53 #include <hpcmips/dev/plumiobusreg.h>
54 #include <hpcmips/dev/plumiobusvar.h>
55 
56 #include "locators.h"
57 
58 #ifdef PLUMIOBUSDEBUG
59 int	plumiobus_debug = 0;
60 #define	DPRINTF(arg) if (plumiobus_debug) printf arg;
61 #define	DPRINTFN(n, arg) if (plumiobus_debug > (n)) printf arg;
62 #else
63 #define	DPRINTF(arg)
64 #define DPRINTFN(n, arg)
65 #endif
66 
67 int plumiobus_match(struct device *, struct cfdata *, void *);
68 void plumiobus_attach(struct device *, struct device *, void *);
69 int plumiobus_print(void *, const char *);
70 int plumiobus_search(struct device *, struct cfdata *, void *);
71 
72 struct plumisa_resource {
73 	int		pr_irq;
74 	bus_space_tag_t	pr_iot;
75 	int		pr_enabled;
76 };
77 
78 struct plumiobus_softc {
79 	struct	device		sc_dev;
80 	plum_chipset_tag_t	sc_pc;
81 	bus_space_tag_t		sc_regt;
82 	bus_space_handle_t	sc_regh;
83 	bus_space_tag_t		sc_iot;
84 	bus_space_handle_t	sc_ioh;
85 	struct plumisa_resource	sc_isa[PLUM_IOBUS_IO5CSMAX];
86 };
87 
88 CFATTACH_DECL(plumiobus, sizeof(struct plumiobus_softc),
89     plumiobus_match, plumiobus_attach, NULL, NULL);
90 
91 bus_space_tag_t __plumiobus_subregion(bus_space_tag_t, bus_addr_t,
92     bus_size_t);
93 #ifdef PLUMIOBUSDEBUG
94 void plumiobus_dump(struct plumiobus_softc *);
95 #endif
96 
97 int
98 plumiobus_match(struct device *parent, struct cfdata *cf, void *aux)
99 {
100 
101 	return (1);
102 }
103 
104 void
105 plumiobus_attach(struct device *parent, struct device *self, void *aux)
106 {
107 	struct plum_attach_args *pa = aux;
108 	struct plumiobus_softc *sc = (void*)self;
109 	struct plumisa_resource *pr;
110 
111 	sc->sc_pc	= pa->pa_pc;
112 	sc->sc_regt	= pa->pa_regt;
113 	sc->sc_iot	= pa->pa_iot;
114 
115 	if (bus_space_map(sc->sc_regt, PLUM_IOBUS_REGBASE,
116 	    PLUM_IOBUS_REGSIZE, 0, &sc->sc_regh)) {
117 		printf(": register map failed.\n");
118 		return;
119 	}
120 	printf("\n");
121 	plum_power_establish(sc->sc_pc, PLUM_PWR_IO5);
122 
123 	/* Address space <-> IRQ mapping */
124 	pr = &sc->sc_isa[IO5CS0];
125 	pr->pr_irq = PLUM_INT_EXT5IO0;
126 	pr->pr_iot = __plumiobus_subregion(
127 		sc->sc_iot,
128 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS0BASE,
129 		PLUM_IOBUS_IO5SIZE);
130 
131 	pr = &sc->sc_isa[IO5CS1];
132 	pr->pr_irq = PLUM_INT_EXT5IO1;
133 	pr->pr_iot = __plumiobus_subregion(
134 		sc->sc_iot,
135 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS1BASE,
136 		PLUM_IOBUS_IO5SIZE);
137 
138 	pr = &sc->sc_isa[IO5CS2];
139 	pr->pr_irq = PLUM_INT_EXT5IO2;
140 	pr->pr_iot = __plumiobus_subregion(
141 		sc->sc_iot,
142 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS2BASE,
143 		PLUM_IOBUS_IO5SIZE);
144 
145 	pr = &sc->sc_isa[IO5CS3];
146 	pr->pr_irq = PLUM_INT_EXT5IO3;
147 	pr->pr_iot = __plumiobus_subregion(
148 		sc->sc_iot,
149 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS3BASE,
150 		PLUM_IOBUS_IO5SIZE);
151 
152 	pr = &sc->sc_isa[IO5CS4];
153 	pr->pr_irq = PLUM_INT_EXT3IO0; /* XXX */
154 	pr->pr_iot = __plumiobus_subregion(
155 		sc->sc_iot,
156 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS4BASE,
157 		PLUM_IOBUS_IO5SIZE);
158 
159 
160 	pr = &sc->sc_isa[IO5NCS];
161 	pr->pr_irq = PLUM_INT_EXT3IO1;
162 	pr->pr_iot = __plumiobus_subregion(
163 		sc->sc_iot,
164 		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS5BASE,
165 		PLUM_IOBUS_IO5SIZE);
166 
167 #ifdef PLUMIOBUSDEBUG
168 	plumiobus_dump(sc);
169 #endif
170 
171 	config_search(plumiobus_search, self, plumiobus_print);
172 }
173 
174 /* XXX something kludge */
175 bus_space_tag_t
176 __plumiobus_subregion(bus_space_tag_t t, bus_addr_t ofs, bus_size_t size)
177 {
178 	struct hpcmips_bus_space *hbs;
179 
180 	if (!(hbs = malloc(sizeof(struct hpcmips_bus_space),
181 	    M_DEVBUF, M_NOWAIT))) {
182 		panic ("__plumiobus_subregion: no memory.");
183 	}
184 	*hbs = *t;
185 	hbs->t_base += ofs;
186 	hbs->t_size = size;
187 
188 	return (hbs);
189 }
190 
191 int
192 plumiobus_search(struct device *parent, struct cfdata *cf, void *aux)
193 {
194 	struct plumiobus_softc *sc = (void*)parent;
195 	struct plumiobus_attach_args pba;
196 	int slot;
197 
198 	/* Disallow wildcarded IO5CS slot */
199 	if (cf->cf_loc[PLUMIOBUSIFCF_SLOT] == PLUMIOBUSIFCF_SLOT_DEFAULT) {
200 		printf("plumiobus_search: wildcarded slot, skipping\n");
201 		return (0);
202 	}
203 	slot = pba.pba_slot = cf->cf_loc[PLUMIOBUSIFCF_SLOT];
204 
205 	pba.pba_pc	= sc->sc_pc;
206 	pba.pba_iot	= sc->sc_isa[slot].pr_iot;
207 	pba.pba_irq	= sc->sc_isa[slot].pr_irq;
208 	pba.pba_busname	= "plumisab";
209 
210 	if (!(sc->sc_isa[slot].pr_enabled) && /* not attached slot */
211 	    config_match(parent, cf, &pba)) {
212 		config_attach(parent, cf, &pba, plumiobus_print);
213 		sc->sc_isa[slot].pr_enabled = 1;
214 	}
215 
216 	return (0);
217 }
218 
219 int
220 plumiobus_print(void *aux, const char *pnp)
221 {
222 
223 	return (pnp ? QUIET : UNCONF);
224 }
225 
226 #ifdef PLUMIOBUSDEBUG
227 void
228 plumiobus_dump(struct plumiobus_softc *sc)
229 {
230 	bus_space_tag_t regt = sc->sc_regt;
231 	bus_space_handle_t regh = sc->sc_regh;
232 	plumreg_t reg;
233 	int i, wait;
234 
235 	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IOXBSZ_REG);
236 	printf("8bit port:");
237 	for (i = 0; i < 6; i++) {
238 		if (reg & (1 << i)) {
239 			printf(" IO5CS%d", i);
240 		}
241 	}
242 	printf("\n");
243 
244 	reg = PLUM_IOBUS_IOXCCNT_MASK &
245 	    plum_conf_read(regt, regh, PLUM_IOBUS_IOXCCNT_REG);
246 	printf(" # of wait to become from the access begining: %d clock\n",
247 	    reg + 1);
248 	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IOXACNT_REG);
249 	printf(" # of wait in access clock: ");
250 	for (i = 0; i < 5; i++) {
251 		wait = (reg >> (i * PLUM_IOBUS_IOXACNT_SHIFT))
252 		    & PLUM_IOBUS_IOXACNT_MASK;
253 		printf("[CS%d:%d] ", i, wait + 1);
254 	}
255 	printf("\n");
256 
257 	reg = PLUM_IOBUS_IOXSCNT_MASK &
258 	    plum_conf_read(regt, regh, PLUM_IOBUS_IOXSCNT_REG);
259 	printf(" # of wait during access by I/O bus : %d clock\n", reg + 1);
260 
261 	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IDEMODE_REG);
262 	if (reg & PLUM_IOBUS_IDEMODE) {
263 		printf("IO5CS3,4 IDE mode\n");
264 	}
265 }
266 #endif /* PLUMIOBUSDEBUG */
267