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