xref: /netbsd/sys/arch/hpcmips/isa/isa_machdep.c (revision bf9ec67e)
1 /*	$NetBSD: isa_machdep.c,v 1.24 2002/01/31 17:56:35 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 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 #include "opt_vr41xx.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/reboot.h>
44 
45 #include <dev/isa/isavar.h>
46 #include <dev/isa/isareg.h>
47 
48 #include <machine/platid.h>
49 #include <machine/platid_mask.h>
50 #include <machine/bus.h>
51 #include <machine/bus_space_hpcmips.h>
52 #include <machine/debug.h>
53 
54 #include <dev/hpc/hpciovar.h>
55 
56 #include <hpcmips/vr/vripif.h>
57 
58 #include "locators.h"
59 
60 #define VRISADEBUG
61 
62 #ifdef VRISADEBUG
63 #ifndef VRISADEBUG_CONF
64 #define VRISADEBUG_CONF 0
65 #endif /* VRISADEBUG_CONF */
66 int vrisa_debug = VRISADEBUG_CONF;
67 #define DPRINTF(arg) if (vrisa_debug) printf arg;
68 #define DBITDISP(mask) if (vrisa_debug) dbg_bit_print(mask);
69 #define VPRINTF(arg) if (bootverbose || vrisa_debug) printf arg;
70 #else /* VRISADEBUG */
71 #define DPRINTF(arg)
72 #define DBITDISP(mask)
73 #define VPRINTF(arg) if (bootverbose) printf arg;
74 #endif /* VRISADEBUG */
75 
76 /*
77  * intrrupt no. encoding:
78  *
79  * 0x0000000f ISA IRQ#
80  * 0x00ff0000 GPIO port#
81  * 0x01000000 interrupt signal hold/through	(1:hold/0:though)
82  * 0x02000000 interrupt detection level		(1:low /0:high	)
83  * 0x04000000 interrupt detection trigger	(1:edge/0:level	)
84  */
85 #define INTR_IRQ(i)	(((i)>> 0) & 0x0f)
86 #define INTR_PORT(i)	(((i)>>16) & 0xff)
87 #define INTR_MODE(i)	(((i)>>24) & 0x07)
88 #define INTR_NIRQS	16
89 
90 int	vrisabprint(void *, const char *);
91 int	vrisabmatch(struct device *, struct cfdata *, void *);
92 void	vrisabattach(struct device *, struct device *, void *);
93 
94 struct vrisab_softc {
95 	struct device sc_dev;
96 	hpcio_chip_t sc_hc;
97 	int sc_intr_map[INTR_NIRQS]; /* ISA <-> GIU inerrupt line mapping */
98 	struct hpcmips_isa_chipset sc_isa_ic;
99 };
100 
101 struct cfattach vrisab_ca = {
102 	sizeof(struct vrisab_softc), vrisabmatch, vrisabattach
103 };
104 
105 #ifdef DEBUG_FIND_PCIC
106 #include <mips/cpuregs.h>
107 #warning DEBUG_FIND_PCIC
108 static void __find_pcic(void);
109 #endif
110 
111 #ifdef DEBUG_FIND_COMPORT
112 #include <mips/cpuregs.h>
113 #include <dev/ic/ns16550reg.h>
114 #include <dev/ic/comreg.h>
115 #warning DEBUG_FIND_COMPORT
116 static void __find_comport(void);
117 #endif
118 
119 int
120 vrisabmatch(struct device *parent, struct cfdata *match, void *aux)
121 {
122 	struct hpcio_attach_args *haa = aux;
123 	platid_mask_t mask;
124 	int n;
125 
126 	if (strcmp(haa->haa_busname, match->cf_driver->cd_name))
127 		return (0);
128 
129 	if (match->cf_loc[HPCIOIFCF_PLATFORM] == HPCIOIFCF_PLATFORM_DEFAULT)
130 		return (1);
131 
132 	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
133 	if ((n = platid_match(&platid, &mask)) != 0)
134 		return (n + 2);
135 
136 	return (0);
137 }
138 
139 void
140 vrisabattach(struct device *parent, struct device *self, void *aux)
141 {
142 	struct hpcio_attach_args *haa = aux;
143 	struct vrisab_softc *sc = (void*)self;
144 	struct isabus_attach_args iba;
145 	struct bus_space_tag_hpcmips *iot, *memt;
146 	bus_addr_t offset;
147 	int i;
148 
149 	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, VRIP_IOCHIP_VRGIU);
150 	sc->sc_isa_ic.ic_sc = sc;
151 
152 	iba.iba_busname = "isa";
153 	iba.iba_ic	= &sc->sc_isa_ic;
154 	iba.iba_dmat    = 0; /* XXX not yet */
155 
156 	/* Allocate ISA memory space */
157 	memt = hpcmips_alloc_bus_space_tag();
158 	offset = sc->sc_dev.dv_cfdata->cf_loc[VRISABIFCF_ISAMEMOFFSET];
159 	hpcmips_init_bus_space(memt,
160 	    (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA mem",
161 	    VR_ISA_MEM_BASE + offset, VR_ISA_MEM_SIZE - offset);
162 	iba.iba_memt = &memt->bst;
163 
164 	/* Allocate ISA port space */
165 	iot = hpcmips_alloc_bus_space_tag();
166 	offset = sc->sc_dev.dv_cfdata->cf_loc[VRISABIFCF_ISAPORTOFFSET];
167 	hpcmips_init_bus_space(iot,
168 	    (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA port",
169 	    VR_ISA_PORT_BASE + offset, VR_ISA_PORT_SIZE - offset);
170 	iba.iba_iot = &iot->bst;
171 
172 #ifdef DEBUG_FIND_PCIC
173 #warning DEBUG_FIND_PCIC
174 	__find_pcic();
175 #else
176 	/* Initialize ISA IRQ <-> GPIO mapping */
177 	for (i = 0; i < INTR_NIRQS; i++)
178 		sc->sc_intr_map[i] = -1;
179 	printf(": ISA port %#x-%#x mem %#x-%#x\n",
180 	    iot->base, iot->base + iot->size,
181 	    memt->base, memt->base + memt->size);
182 	config_found(self, &iba, vrisabprint);
183 #endif
184 
185 #ifdef DEBUG_FIND_COMPORT
186 #warning DEBUG_FIND_COMPORT
187 	__find_comport();
188 #endif
189 }
190 
191 int
192 vrisabprint(void *aux, const char *pnp)
193 {
194 	if (pnp)
195 		return (QUIET);
196 
197 	return (UNCONF);
198 }
199 
200 void
201 isa_attach_hook(struct device *parent, struct device *self,
202     struct isabus_attach_args *iba)
203 {
204 
205 }
206 
207 const struct evcnt *
208 isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
209 {
210 
211 	/* XXX for now, no evcnt parent reported */
212 	return (NULL);
213 }
214 
215 void *
216 isa_intr_establish(isa_chipset_tag_t ic, int intr, int type, int level,
217     int (*ih_fun)(void*), void *ih_arg)
218 {
219 	struct vrisab_softc *sc = ic->ic_sc;
220 	int port, irq, mode;
221 
222 	static int intr_modes[8] = {
223 		HPCIO_INTR_LEVEL_HIGH_THROUGH,
224 		HPCIO_INTR_LEVEL_HIGH_HOLD,
225 		HPCIO_INTR_LEVEL_LOW_THROUGH,
226 		HPCIO_INTR_LEVEL_LOW_HOLD,
227 		HPCIO_INTR_EDGE_THROUGH,
228 		HPCIO_INTR_EDGE_HOLD,
229 		HPCIO_INTR_EDGE_THROUGH,
230 		HPCIO_INTR_EDGE_HOLD,
231 	};
232 #ifdef VRISADEBUG
233 	static char* intr_mode_names[8] = {
234 		"level high through",
235 		"level high hold",
236 		"level low through",
237 		"level low hold",
238 		"edge through",
239 		"edge hold",
240 		"edge through",
241 		"edge hold",
242 	};
243 #endif /* VRISADEBUG */
244 	/*
245 	 * ISA IRQ <-> GPIO port mapping
246 	 */
247 	irq = INTR_IRQ(intr);
248 	if (sc->sc_intr_map[irq] != -1) {
249 		/* already mapped */
250 		intr = sc->sc_intr_map[irq];
251 	} else {
252 		/* not mapped yet */
253 		sc->sc_intr_map[irq] = intr; /* Register it */
254 	}
255 	mode = INTR_MODE(intr);
256 	port = INTR_PORT(intr);
257 
258 	VPRINTF(("ISA IRQ %d -> %s port %d, %s\n",
259 	    irq, sc->sc_hc->hc_name, port, intr_mode_names[mode]));
260 
261 	/* Call Vr routine */
262 	return (hpcio_intr_establish(sc->sc_hc, port, intr_modes[mode],
263 	    ih_fun, ih_arg));
264 }
265 
266 void
267 isa_intr_disestablish(ic, arg)
268 	isa_chipset_tag_t ic;
269 	void *arg;
270 {
271 	struct vrisab_softc *sc = ic->ic_sc;
272 	/* Call Vr routine */
273 	hpcio_intr_disestablish(sc->sc_hc, arg);
274 }
275 
276 int
277 isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
278 {
279 	/* XXX not coded yet. this is temporary XXX */
280 	DPRINTF(("isa_intr_alloc:"));
281 	DBITDISP(mask);
282 	*irq = (ffs(mask) -1); /* XXX */
283 
284 	return (0);
285 }
286 
287 #ifdef DEBUG_FIND_PCIC
288 #warning DEBUG_FIND_PCIC
289 static void
290 __find_pcic(void)
291 {
292 	int i, j, step, found;
293 	u_int32_t addr;
294 	u_int8_t reg;
295 	int __read_revid (u_int32_t port)
296 	    {
297 		    addr = MIPS_PHYS_TO_KSEG1(i + port);
298 		    printf("%#x\r", i);
299 		    for (found = 0, j = 0; j < 0x100; j += 0x40) {
300 			    *((volatile u_int8_t *)addr) = j;
301 			    reg = *((volatile u_int8_t *)(addr + 1));
302 #ifdef DEBUG_FIND_PCIC_I82365SL_ONLY
303 			    if (reg == 0x82 || reg == 0x83) {
304 #else
305 			    if ((reg & 0xc0) == 0x80) {
306 #endif
307 					    found++;
308 			    }
309 			    if (found)
310 				    printf("\nfound %d socket at %#x"
311 					"(base from %#x)\n", found, addr,
312 					i + port - VR_ISA_PORT_BASE);
313 		    }
314 	    }
315 	step = 0x1000000;
316 	printf("\nFinding PCIC. Trying ISA port %#x-%#x step %#x\n",
317 	    VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE, step);
318 	for (i = VR_ISA_PORT_BASE; i < VR_ISA_PORT_BASE+VR_ISA_PORT_SIZE;
319 	    i+= step) {
320 		__read_revid (0x3e0);
321 		__read_revid (0x3e2);
322 	}
323 }
324 #endif /* DEBUG_FIND_PCIC */
325 
326 
327 #ifdef DEBUG_FIND_COMPORT
328 #warning DEBUG_FIND_COMPORT
329 
330 static int probe_com(u_int32_t);
331 
332 static int
333 probe_com(u_int32_t port_addr)
334 {
335 	u_int32_t addr;
336 	u_int8_t ubtmp1, ubtmp2;
337 
338 	addr = MIPS_PHYS_TO_KSEG1(port_addr);
339 
340 	*((volatile u_int8_t *)(addr + com_cfcr)) = LCR_8BITS;
341 	*((volatile u_int8_t *)(addr + com_iir)) = 0;
342 
343 	ubtmp1 = *((volatile u_int8_t *)(addr + com_cfcr));
344 	ubtmp2 = *((volatile u_int8_t *)(addr + com_iir));
345 
346 	if ((ubtmp1 != LCR_8BITS) || ((ubtmp2 & 0x38) != 0)) {
347 		return (0);
348 	}
349 
350 	return (1);
351 }
352 
353 static void
354 __find_comport()
355 {
356 	int found;
357 	u_int32_t port, step;
358 
359 	found = 0;
360 	step = 0x08;
361 
362 	printf("Searching COM port. Trying ISA port %#x-%#x step %#x\n",
363 	    VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE - 1, step );
364 
365 	for (port = VR_ISA_PORT_BASE;
366 	    port < (VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE); port += step){
367 		if (probe_com(port)) {
368 			found++;
369 			printf("found %d at %#x\n", found, port);
370 		}
371 	}
372 }
373 #endif /* DEBUG_FIND_COMPORT */
374