xref: /openbsd/sys/arch/loongson/dev/pcib.c (revision 92a3ba1d)
1*92a3ba1dSmiod /*	$OpenBSD: pcib.c,v 1.4 2012/08/18 16:00:20 miod Exp $	*/
2f1558498Smiod 
3d57a735dSmiod /*-
4d57a735dSmiod  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5d57a735dSmiod  * All rights reserved.
6f1558498Smiod  *
7d57a735dSmiod  * This code is derived from software contributed to The NetBSD Foundation
8d57a735dSmiod  * by Jason R. Thorpe.
9f1558498Smiod  *
10d57a735dSmiod  * Redistribution and use in source and binary forms, with or without
11d57a735dSmiod  * modification, are permitted provided that the following conditions
12d57a735dSmiod  * are met:
13d57a735dSmiod  * 1. Redistributions of source code must retain the above copyright
14d57a735dSmiod  *    notice, this list of conditions and the following disclaimer.
15d57a735dSmiod  * 2. Redistributions in binary form must reproduce the above copyright
16d57a735dSmiod  *    notice, this list of conditions and the following disclaimer in the
17d57a735dSmiod  *    documentation and/or other materials provided with the distribution.
18d57a735dSmiod  *
19d57a735dSmiod  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d57a735dSmiod  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d57a735dSmiod  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d57a735dSmiod  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d57a735dSmiod  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d57a735dSmiod  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d57a735dSmiod  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d57a735dSmiod  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d57a735dSmiod  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d57a735dSmiod  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d57a735dSmiod  * POSSIBILITY OF SUCH DAMAGE.
30f1558498Smiod  */
31f1558498Smiod 
32f1558498Smiod #include <sys/param.h>
33f1558498Smiod #include <sys/systm.h>
34f1558498Smiod #include <sys/device.h>
35f1558498Smiod 
36d57a735dSmiod #include <machine/autoconf.h>
37f1558498Smiod #include <machine/bus.h>
38f1558498Smiod 
39f1558498Smiod #include <dev/pci/pcireg.h>
40f1558498Smiod #include <dev/pci/pcivar.h>
41d57a735dSmiod #include <dev/pci/pcidevs.h>
42f1558498Smiod 
43f1558498Smiod #include <dev/isa/isavar.h>
44f1558498Smiod 
452581a76dSpirofti #include <dev/pci/glxreg.h>
462581a76dSpirofti #include <dev/pci/glxvar.h>
47f1558498Smiod 
48f1558498Smiod #include "isa.h"
49f1558498Smiod 
50d57a735dSmiod int	pcibmatch(struct device *, void *, void *);
51f1558498Smiod void	pcibattach(struct device *, struct device *, void *);
52f1558498Smiod 
53d57a735dSmiod struct pcib_softc {
54d57a735dSmiod 	struct device	sc_dev;
55d57a735dSmiod 	bus_space_tag_t	sc_iot;
56d57a735dSmiod 	bus_space_tag_t	sc_memt;
57d57a735dSmiod 	bus_dma_tag_t	sc_dmat;
58d57a735dSmiod };
59d57a735dSmiod 
60d57a735dSmiod const struct cfattach pcib_ca = {
61d57a735dSmiod 	sizeof(struct pcib_softc), pcibmatch, pcibattach
62d57a735dSmiod };
63d57a735dSmiod 
64*92a3ba1dSmiod void	pcib_callback(struct device *);
65*92a3ba1dSmiod void	pcib_isa_attach(struct pcib_softc *, bus_space_tag_t, bus_space_tag_t,
66*92a3ba1dSmiod 	    bus_dma_tag_t);
67*92a3ba1dSmiod int	pcib_print(void *, const char *);
68*92a3ba1dSmiod 
69d57a735dSmiod struct cfdriver pcib_cd = {
70d57a735dSmiod 	NULL, "pcib", DV_DULL
71d57a735dSmiod };
72d57a735dSmiod 
73d57a735dSmiod int
pcibmatch(struct device * parent,void * match,void * aux)74d57a735dSmiod pcibmatch(struct device *parent, void *match, void *aux)
75d57a735dSmiod {
76d57a735dSmiod 	struct pci_attach_args *pa = aux;
77d57a735dSmiod 
78d57a735dSmiod 	switch (PCI_VENDOR(pa->pa_id)) {
79d57a735dSmiod 	case PCI_VENDOR_INTEL:
80d57a735dSmiod 		switch (PCI_PRODUCT(pa->pa_id)) {
81d57a735dSmiod 		case PCI_PRODUCT_INTEL_SIO:
82d57a735dSmiod 		case PCI_PRODUCT_INTEL_82371MX:
83d57a735dSmiod 		case PCI_PRODUCT_INTEL_82371AB_ISA:
84d57a735dSmiod 		case PCI_PRODUCT_INTEL_82440MX_ISA:
85d57a735dSmiod 			/* The above bridges mis-identify themselves */
86d57a735dSmiod 			return (1);
87d57a735dSmiod 		}
88d57a735dSmiod 		break;
89d57a735dSmiod 	case PCI_VENDOR_SIS:
90d57a735dSmiod 		switch (PCI_PRODUCT(pa->pa_id)) {
91d57a735dSmiod 		case PCI_PRODUCT_SIS_85C503:
92d57a735dSmiod 			/* mis-identifies itself as a miscellaneous prehistoric */
93d57a735dSmiod 			return (1);
94d57a735dSmiod 		}
95d57a735dSmiod 		break;
96d57a735dSmiod 	case PCI_VENDOR_VIATECH:
97d57a735dSmiod 		switch (PCI_PRODUCT(pa->pa_id)) {
98d57a735dSmiod 		case PCI_PRODUCT_VIATECH_VT82C686A_SMB:
99d57a735dSmiod 			/* mis-identifies itself as a ISA bridge */
100d57a735dSmiod 			return (0);
101d57a735dSmiod 		}
102d57a735dSmiod 		break;
103d57a735dSmiod 	}
104d57a735dSmiod 
105d57a735dSmiod 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
106d57a735dSmiod 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA)
107d57a735dSmiod 		return (1);
108d57a735dSmiod 
109d57a735dSmiod 	return (0);
110d57a735dSmiod }
111d57a735dSmiod 
112f1558498Smiod void
pcibattach(struct device * parent,struct device * self,void * aux)113f1558498Smiod pcibattach(struct device *parent, struct device *self, void *aux)
114f1558498Smiod {
115d57a735dSmiod 	struct pcib_softc *sc = (struct pcib_softc *)self;
116d57a735dSmiod 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
117f1558498Smiod 
118f1558498Smiod 	printf("\n");
119f1558498Smiod 
120f1558498Smiod 	/*
121*92a3ba1dSmiod 	 * If we are actually a glxpcib, we can't use softc fields
122*92a3ba1dSmiod 	 * beyond the base struct device, for this would corrupt
123*92a3ba1dSmiod 	 * the glxpcib softc. Decide to attach isa immediately in this
124*92a3ba1dSmiod 	 * case - glxpcib-based designs are not expected to have ISA
125*92a3ba1dSmiod 	 * slots and attaching isa early should not cause problems.
126*92a3ba1dSmiod 	 */
127*92a3ba1dSmiod 	if (strncmp(self->dv_xname, "glxpcib", 7) == 0) {
128*92a3ba1dSmiod 		pcib_isa_attach(sc, pa->pa_iot, pa->pa_memt, pa->pa_dmat);
129*92a3ba1dSmiod 	} else {
130*92a3ba1dSmiod 		/*
131d57a735dSmiod 		 * Wait until all PCI devices are attached before attaching isa;
132*92a3ba1dSmiod 		 * existing pcib code on other platforms mentions that not
133*92a3ba1dSmiod 		 * doing this ``might mess the interrupt setup on some
134*92a3ba1dSmiod 		 * systems'', although this is very unlikely to be the case
135*92a3ba1dSmiod 		 * on loongson.
136d57a735dSmiod 		 */
137d57a735dSmiod 		sc->sc_iot = pa->pa_iot;
138d57a735dSmiod 		sc->sc_memt = pa->pa_memt;
139d57a735dSmiod 		sc->sc_dmat = pa->pa_dmat;
140d57a735dSmiod 		config_defer(self, pcib_callback);
141d57a735dSmiod 	}
142*92a3ba1dSmiod }
143d57a735dSmiod 
144d57a735dSmiod void
pcib_callback(struct device * self)145d57a735dSmiod pcib_callback(struct device *self)
146d57a735dSmiod {
147d57a735dSmiod 	struct pcib_softc *sc = (struct pcib_softc *)self;
148*92a3ba1dSmiod 
149*92a3ba1dSmiod 	pcib_isa_attach(sc, sc->sc_iot, sc->sc_memt, sc->sc_dmat);
150*92a3ba1dSmiod }
151*92a3ba1dSmiod 
152*92a3ba1dSmiod void
pcib_isa_attach(struct pcib_softc * sc,bus_space_tag_t iot,bus_space_tag_t memt,bus_dma_tag_t dmat)153*92a3ba1dSmiod pcib_isa_attach(struct pcib_softc *sc, bus_space_tag_t iot,
154*92a3ba1dSmiod     bus_space_tag_t memt, bus_dma_tag_t dmat)
155*92a3ba1dSmiod {
156d57a735dSmiod 	struct isabus_attach_args iba;
157d57a735dSmiod 
158d57a735dSmiod 	/*
159f1558498Smiod 	 * Attach the ISA bus behind this bridge.
160f1558498Smiod 	 */
161f1558498Smiod 	memset(&iba, 0, sizeof(iba));
162f1558498Smiod 	iba.iba_busname = "isa";
163*92a3ba1dSmiod 	iba.iba_iot = iot;
164*92a3ba1dSmiod 	iba.iba_memt = memt;
165f1558498Smiod #if NISADMA > 0
166*92a3ba1dSmiod 	iba.iba_dmat = dmat;
167f1558498Smiod #endif
168d57a735dSmiod 	iba.iba_ic = sys_platform->isa_chipset;
169d57a735dSmiod 	if (iba.iba_ic != NULL)
170*92a3ba1dSmiod 		config_found(&sc->sc_dev, &iba, pcib_print);
171f1558498Smiod }
172f1558498Smiod 
173f1558498Smiod int
pcib_print(void * aux,const char * pnp)174f1558498Smiod pcib_print(void *aux, const char *pnp)
175f1558498Smiod {
176f1558498Smiod 	/* Only ISAs can attach to pcib's; easy. */
177f1558498Smiod 	if (pnp)
178f1558498Smiod 		printf("isa at %s", pnp);
179f1558498Smiod 	return (UNCONF);
180f1558498Smiod }
181