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