1*47bd93c3Sandvar /* $NetBSD: pcib.c,v 1.11 2022/05/17 05:05:20 andvar Exp $ */
2d974db0aSgarbled
3d974db0aSgarbled /*-
4d974db0aSgarbled * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
5d974db0aSgarbled * All rights reserved.
6d974db0aSgarbled *
7d974db0aSgarbled * This code is derived from software contributed to The NetBSD Foundation
8d974db0aSgarbled * by Jason R. Thorpe.
9d974db0aSgarbled *
10d974db0aSgarbled * Redistribution and use in source and binary forms, with or without
11d974db0aSgarbled * modification, are permitted provided that the following conditions
12d974db0aSgarbled * are met:
13d974db0aSgarbled * 1. Redistributions of source code must retain the above copyright
14d974db0aSgarbled * notice, this list of conditions and the following disclaimer.
15d974db0aSgarbled * 2. Redistributions in binary form must reproduce the above copyright
16d974db0aSgarbled * notice, this list of conditions and the following disclaimer in the
17d974db0aSgarbled * documentation and/or other materials provided with the distribution.
18d974db0aSgarbled *
19d974db0aSgarbled * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d974db0aSgarbled * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d974db0aSgarbled * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229c63b7e8Smartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239c63b7e8Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d974db0aSgarbled * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d974db0aSgarbled * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d974db0aSgarbled * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d974db0aSgarbled * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d974db0aSgarbled * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d974db0aSgarbled * POSSIBILITY OF SUCH DAMAGE.
30d974db0aSgarbled */
31d974db0aSgarbled
32d974db0aSgarbled #include <sys/cdefs.h>
33*47bd93c3Sandvar __KERNEL_RCSID(0, "$NetBSD: pcib.c,v 1.11 2022/05/17 05:05:20 andvar Exp $");
3416031f7dSrin
3516031f7dSrin #include "isa.h"
3616031f7dSrin #include "isadma.h"
37d974db0aSgarbled
38d974db0aSgarbled #include <sys/types.h>
39d974db0aSgarbled #include <sys/param.h>
40d974db0aSgarbled #include <sys/systm.h>
41d974db0aSgarbled #include <sys/device.h>
42d974db0aSgarbled
43cf10107dSdyoung #include <sys/bus.h>
44d974db0aSgarbled
45d974db0aSgarbled #include <dev/isa/isavar.h>
46d974db0aSgarbled
47d974db0aSgarbled #include <dev/pci/pcivar.h>
48d974db0aSgarbled #include <dev/pci/pcireg.h>
49d974db0aSgarbled
50d974db0aSgarbled #include <dev/pci/pcidevs.h>
51d974db0aSgarbled
52666d4c1dSmatt int pcibmatch(device_t, cfdata_t, void *);
53666d4c1dSmatt void pcibattach(device_t, device_t, void *);
54d974db0aSgarbled
55d974db0aSgarbled struct pcib_softc {
56666d4c1dSmatt device_t sc_dev;
57d974db0aSgarbled struct powerpc_isa_chipset *sc_chipset;
58d974db0aSgarbled };
59d974db0aSgarbled
60d974db0aSgarbled extern struct powerpc_isa_chipset genppc_ict;
61d974db0aSgarbled extern struct genppc_pci_chipset *genppc_pct;
62d974db0aSgarbled
63666d4c1dSmatt CFATTACH_DECL_NEW(pcib, sizeof(struct pcib_softc),
64d974db0aSgarbled pcibmatch, pcibattach, NULL, NULL);
65d974db0aSgarbled
66666d4c1dSmatt void pcib_callback(device_t);
67d974db0aSgarbled
68d974db0aSgarbled int
pcibmatch(device_t parent,cfdata_t cf,void * aux)69666d4c1dSmatt pcibmatch(device_t parent, cfdata_t cf, void *aux)
70d974db0aSgarbled {
71d974db0aSgarbled struct pci_attach_args *pa = aux;
72d974db0aSgarbled
73d974db0aSgarbled /*
74d974db0aSgarbled * Match PCI-ISA bridge.
75d974db0aSgarbled */
76d974db0aSgarbled if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
77d974db0aSgarbled PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
78d974db0aSgarbled return (1);
79d974db0aSgarbled }
80d974db0aSgarbled
81d974db0aSgarbled /*
82d974db0aSgarbled * some special cases:
83d974db0aSgarbled */
84d974db0aSgarbled switch (PCI_VENDOR(pa->pa_id)) {
85d974db0aSgarbled case PCI_VENDOR_INTEL:
86d974db0aSgarbled switch (PCI_PRODUCT(pa->pa_id)) {
87d974db0aSgarbled case PCI_PRODUCT_INTEL_SIO:
88d974db0aSgarbled /*
89d974db0aSgarbled * The Intel SIO identifies itself as a
90d974db0aSgarbled * miscellaneous prehistoric.
91d974db0aSgarbled */
92d974db0aSgarbled return (1);
93d974db0aSgarbled }
94d974db0aSgarbled break;
95d974db0aSgarbled }
96d974db0aSgarbled
97d974db0aSgarbled return (0);
98d974db0aSgarbled }
99d974db0aSgarbled
100d974db0aSgarbled void
pcibattach(device_t parent,device_t self,void * aux)101666d4c1dSmatt pcibattach(device_t parent, device_t self, void *aux)
102d974db0aSgarbled {
103d974db0aSgarbled struct pci_attach_args *pa = aux;
104666d4c1dSmatt struct pcib_softc *sc = device_private(self);
105d974db0aSgarbled char devinfo[256];
106d974db0aSgarbled u_int32_t v;
107d974db0aSgarbled int lvlmask = 0;
108d974db0aSgarbled #ifdef prep
109d974db0aSgarbled prop_bool_t rav;
110d974db0aSgarbled #endif
111d974db0aSgarbled
112666d4c1dSmatt sc->sc_dev = self;
113666d4c1dSmatt
114d974db0aSgarbled /*
115d974db0aSgarbled * Just print out a description and defer configuration
116d974db0aSgarbled * until all PCI devices have been attached.
117d974db0aSgarbled */
118d974db0aSgarbled pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
119d974db0aSgarbled aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
120d974db0aSgarbled PCI_REVISION(pa->pa_class));
121d974db0aSgarbled
122d974db0aSgarbled v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40);
123d974db0aSgarbled if ((v & 0x20) == 0) {
124036ca983Smatt aprint_verbose_dev(self, "PIRQ[0-3] not used\n");
125d974db0aSgarbled } else {
126d974db0aSgarbled v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x60);
127d974db0aSgarbled if ((v & 0x80808080) == 0x80808080) {
128036ca983Smatt aprint_verbose_dev(self, "PIRQ[0-3] disabled\n");
129d974db0aSgarbled } else {
130d974db0aSgarbled int i;
131666d4c1dSmatt aprint_verbose("%s:", device_xname(self));
132d974db0aSgarbled for (i = 0; i < 4; i++, v >>= 8) {
133d974db0aSgarbled if ((v & 0x80) == 0 && (v & 0x0f) != 0) {
134d974db0aSgarbled aprint_verbose(" PIRQ[%d]=%d", i,
135d974db0aSgarbled v & 0x0f);
136d974db0aSgarbled lvlmask |= (1 << (v & 0x0f));
137d974db0aSgarbled }
138d974db0aSgarbled }
139d974db0aSgarbled aprint_verbose("\n");
140d974db0aSgarbled }
141d974db0aSgarbled }
142d974db0aSgarbled
143d974db0aSgarbled /*
144d974db0aSgarbled * If we have an 83C553F-G sitting on a RAVEN host bridge,
145d974db0aSgarbled * then we need to rewire some interrupts.
146d974db0aSgarbled * The IDE Interrupt Routing Control Register lives at 0x43,
147d974db0aSgarbled * and defaults to 0xEF, which means the primary controller
148d974db0aSgarbled * interrupts on ivr-14, and the secondary on ivr-15. We
149d974db0aSgarbled * reset it to 0xEE to fire them both at ivr-14.
150d974db0aSgarbled * We have to rewrite the interrupt map, because the bridge map told
151d974db0aSgarbled * us that the interrupt is MPIC 0, which is the bridge intr for
152d974db0aSgarbled * the 8259.
153d974db0aSgarbled * Additionally, sometimes the PCI Interrupt Routing Control Register
154*47bd93c3Sandvar * is improperly initialized, causing all sorts of weird interrupt
155d974db0aSgarbled * issues on the machine. The manual says it should default to
156d974db0aSgarbled * 0000h (index 45-44h) however it would appear that PPCBUG is
157d974db0aSgarbled * setting it up differently. Reset it to 0000h.
158d974db0aSgarbled */
159d974db0aSgarbled
16080a83a2bSgarbled if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SYMPHONY &&
16180a83a2bSgarbled PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYMPHONY_83C553) {
16280a83a2bSgarbled v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x44) & 0xffff0000;
16380a83a2bSgarbled pci_conf_write(pa->pa_pc, pa->pa_tag, 0x44, v);
16480a83a2bSgarbled }
16580a83a2bSgarbled
166d974db0aSgarbled #ifdef prep
167d974db0aSgarbled rav = prop_dictionary_get(device_properties(parent),
168d974db0aSgarbled "prep-raven-pchb");
169d974db0aSgarbled
170d974db0aSgarbled if (rav != NULL && prop_bool_true(rav) &&
171d974db0aSgarbled PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SYMPHONY &&
172d974db0aSgarbled PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SYMPHONY_83C553) {
173d974db0aSgarbled
174d974db0aSgarbled prop_dictionary_t dict, devsub;
175d974db0aSgarbled prop_number_t pinsub;
176d974db0aSgarbled struct genppc_pci_chipset_businfo *pbi;
177d974db0aSgarbled
178d974db0aSgarbled v = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x40) & 0x00ffffff;
179d974db0aSgarbled v |= 0xee000000;
180d974db0aSgarbled pci_conf_write(pa->pa_pc, pa->pa_tag, 0x40, v);
181d974db0aSgarbled
182d974db0aSgarbled pbi = SIMPLEQ_FIRST(&genppc_pct->pc_pbi);
183d974db0aSgarbled dict = prop_dictionary_get(pbi->pbi_properties,
184d974db0aSgarbled "prep-pci-intrmap");
185d974db0aSgarbled devsub = prop_dictionary_get(dict, "devfunc-11");
186d974db0aSgarbled pinsub = prop_number_create_integer(14);
187d974db0aSgarbled prop_dictionary_set(devsub, "pin-A", pinsub);
188d974db0aSgarbled prop_object_release(pinsub);
189666d4c1dSmatt aprint_verbose_dev(self, "setting pciide irq to 14\n");
190d974db0aSgarbled /* irq 14 is level */
191d974db0aSgarbled lvlmask = 0x0040;
192d974db0aSgarbled }
193d974db0aSgarbled #endif /* prep */
194d974db0aSgarbled
195d974db0aSgarbled config_defer(self, pcib_callback);
196d974db0aSgarbled }
197d974db0aSgarbled
198d974db0aSgarbled void
pcib_callback(device_t self)199666d4c1dSmatt pcib_callback(device_t self)
200d974db0aSgarbled {
201666d4c1dSmatt struct pcib_softc *sc = device_private(self);
202d974db0aSgarbled #if NISA > 0
203d974db0aSgarbled struct isabus_attach_args iba;
204d974db0aSgarbled
205d974db0aSgarbled /*
206d974db0aSgarbled * Attach the ISA bus behind this bridge.
207d974db0aSgarbled */
208d974db0aSgarbled memset(&iba, 0, sizeof(iba));
209d974db0aSgarbled sc->sc_chipset = &genppc_ict;
210d974db0aSgarbled iba.iba_ic = sc->sc_chipset;
211d974db0aSgarbled iba.iba_iot = &genppc_isa_io_space_tag;
212d974db0aSgarbled iba.iba_memt = &genppc_isa_mem_space_tag;
213d974db0aSgarbled #if NISADMA > 0
214d974db0aSgarbled iba.iba_dmat = &isa_bus_dma_tag;
215d974db0aSgarbled #endif
216c7fb772bSthorpej config_found(sc->sc_dev, &iba, isabusprint, CFARGS_NONE);
217d974db0aSgarbled #endif
218d974db0aSgarbled }
219