xref: /netbsd-src/sys/arch/powerpc/ibm4xx/pci/pci_machdep.c (revision 91fd88684309ecac48e8adf1fd299e7d6393b729)
1*91fd8868Srin /*	$NetBSD: pci_machdep.c,v 1.13 2022/10/15 04:47:37 rin Exp $	*/
2dc98452dSshige 
3dc98452dSshige /*
4dc98452dSshige  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
5dc98452dSshige  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
6dc98452dSshige  *
7dc98452dSshige  * Redistribution and use in source and binary forms, with or without
8dc98452dSshige  * modification, are permitted provided that the following conditions
9dc98452dSshige  * are met:
10dc98452dSshige  * 1. Redistributions of source code must retain the above copyright
11dc98452dSshige  *    notice, this list of conditions and the following disclaimer.
12dc98452dSshige  * 2. Redistributions in binary form must reproduce the above copyright
13dc98452dSshige  *    notice, this list of conditions and the following disclaimer in the
14dc98452dSshige  *    documentation and/or other materials provided with the distribution.
15dc98452dSshige  * 3. All advertising materials mentioning features or use of this software
16dc98452dSshige  *    must display the following acknowledgement:
17dc98452dSshige  *	This product includes software developed by Charles M. Hannum.
18dc98452dSshige  * 4. The name of the author may not be used to endorse or promote products
19dc98452dSshige  *    derived from this software without specific prior written permission.
20dc98452dSshige  *
21dc98452dSshige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22dc98452dSshige  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23dc98452dSshige  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24dc98452dSshige  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25dc98452dSshige  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26dc98452dSshige  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27dc98452dSshige  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28dc98452dSshige  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29dc98452dSshige  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30dc98452dSshige  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31dc98452dSshige  */
32dc98452dSshige 
33dc98452dSshige /*
34dc98452dSshige  * Machine-specific functions for PCI autoconfiguration.
35dc98452dSshige  *
36dc98452dSshige  * On PCs, there are two methods of generating PCI configuration cycles.
37dc98452dSshige  * We try to detect the appropriate mechanism for this machine and set
38dc98452dSshige  * up a few function pointers to access the correct method directly.
39dc98452dSshige  *
40dc98452dSshige  * The configuration method can be hard-coded in the config file by
41dc98452dSshige  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42dc98452dSshige  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43dc98452dSshige  */
44dc98452dSshige 
45dc98452dSshige #include <sys/cdefs.h>
46*91fd8868Srin __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.13 2022/10/15 04:47:37 rin Exp $");
47af8c068dSrin 
48af8c068dSrin #ifdef _KERNEL_OPT
49af8c068dSrin #include "opt_pci.h"
50af8c068dSrin #endif
51dc98452dSshige 
52dc98452dSshige #include <sys/types.h>
53dc98452dSshige #include <sys/param.h>
54dc98452dSshige #include <sys/time.h>
55dc98452dSshige #include <sys/systm.h>
56dc98452dSshige #include <sys/errno.h>
57dc98452dSshige #include <sys/device.h>
58dc98452dSshige #include <sys/extent.h>
591fd2c684Smatt #include <sys/bus.h>
601fd2c684Smatt #include <sys/intr.h>
61dc98452dSshige 
62dc98452dSshige #include <uvm/uvm_extern.h>
63dc98452dSshige 
64dc98452dSshige #include <dev/pci/pcivar.h>
65dc98452dSshige #include <dev/pci/pcireg.h>
66dc98452dSshige #include <dev/pci/pcidevs.h>
67dc98452dSshige #include <dev/pci/pciconf.h>
68dc98452dSshige 
69dc98452dSshige #include <powerpc/ibm4xx/ibm405gp.h>
70dd2488a8Smatt #include <powerpc/ibm4xx/pci_machdep.h>
71dc98452dSshige #include <powerpc/ibm4xx/dev/pcicreg.h>
72dc98452dSshige 
73*91fd8868Srin #ifdef DHT_FIXUP_PDCIDE
74*91fd8868Srin #include <dev/pci/pciidereg.h>
75*91fd8868Srin #endif
76*91fd8868Srin 
77dc98452dSshige static struct powerpc_bus_space pci_iot = {
78dc98452dSshige 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
79dc98452dSshige 	0x00000000,
80dc98452dSshige 	IBM405GP_PCIC0_BASE,		/* extent base */
81dc98452dSshige 	IBM405GP_PCIC0_BASE + 8,	/* extent limit */
82dc98452dSshige };
83dc98452dSshige 
84dc98452dSshige static bus_space_handle_t pci_ioh;
85dc98452dSshige 
86dc98452dSshige void
ibm4xx_pci_machdep_init(void)87dd2488a8Smatt ibm4xx_pci_machdep_init(void)
88dc98452dSshige {
89dc98452dSshige 
90dc98452dSshige 	if (pci_ioh == 0 &&
91dc98452dSshige 	   (bus_space_init(&pci_iot, "pcicfg", NULL, 0) ||
92dc98452dSshige 	    bus_space_map(&pci_iot, IBM405GP_PCIC0_BASE, 8, 0, &pci_ioh)))
93dc98452dSshige 		panic("Cannot map PCI registers");
94dc98452dSshige }
95dc98452dSshige 
96dc98452dSshige void
ibm4xx_pci_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)97dd2488a8Smatt ibm4xx_pci_attach_hook(device_t parent, device_t self,
98dc98452dSshige     struct pcibus_attach_args *pba)
99dc98452dSshige {
100dc98452dSshige 
101dc98452dSshige #ifdef PCI_CONFIGURE_VERBOSE
102dc98452dSshige 	printf("pci_attach_hook\n");
103dc98452dSshige 	ibm4xx_show_pci_map();
104dc98452dSshige #endif
105dc98452dSshige 	ibm4xx_setup_pci();
106dc98452dSshige #ifdef PCI_CONFIGURE_VERBOSE
107dc98452dSshige 	ibm4xx_show_pci_map();
108dc98452dSshige #endif
109dc98452dSshige }
110dc98452dSshige 
111dc98452dSshige pcitag_t
ibm4xx_pci_make_tag(void * v,int bus,int device,int function)112dd2488a8Smatt ibm4xx_pci_make_tag(void *v, int bus, int device, int function)
113dc98452dSshige {
114dc98452dSshige 	pcitag_t tag;
115dc98452dSshige 
116dc98452dSshige 	if (bus >= 256 || device >= 32 || function >= 8)
117dc98452dSshige 		panic("pci_make_tag: bad request");
118dc98452dSshige 
119dc98452dSshige 	/* XXX magic number */
120dc98452dSshige 	tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
121dc98452dSshige 
122dc98452dSshige 	return tag;
123dc98452dSshige }
124dc98452dSshige 
125dc98452dSshige void
ibm4xx_pci_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)126dd2488a8Smatt ibm4xx_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
127dc98452dSshige {
128dc98452dSshige 
129dc98452dSshige 	if (bp != NULL)
130dc98452dSshige 		*bp = (tag >> 16) & 0xff;
131dc98452dSshige 	if (dp != NULL)
132dc98452dSshige 		*dp = (tag >> 11) & 0x1f;
133dc98452dSshige 	if (fp != NULL)
134dc98452dSshige 		*fp = (tag >> 8) & 0x07;
135dc98452dSshige }
136dc98452dSshige 
137dc98452dSshige pcireg_t
ibm4xx_pci_conf_read(void * v,pcitag_t tag,int reg)138dd2488a8Smatt ibm4xx_pci_conf_read(void *v, pcitag_t tag, int reg)
139dc98452dSshige {
140dc98452dSshige 	pcireg_t data;
141dc98452dSshige 
142605f564fSmsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
143605f564fSmsaitoh 		return (pcireg_t) -1;
144605f564fSmsaitoh 
145dc98452dSshige 	/* 405GT BIOS disables interrupts here. Should we? --Art */
146dc98452dSshige 	bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, tag | reg);
147dc98452dSshige 	data = bus_space_read_4(&pci_iot, pci_ioh, PCIC_CFGDATA);
148dc98452dSshige 	/* 405GP pass2 errata #6 */
149dc98452dSshige 	bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, 0);
150dc98452dSshige 	return data;
151dc98452dSshige }
152dc98452dSshige 
153dc98452dSshige void
ibm4xx_pci_conf_write(void * v,pcitag_t tag,int reg,pcireg_t data)154dd2488a8Smatt ibm4xx_pci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
155dc98452dSshige {
156dc98452dSshige 
157605f564fSmsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
158605f564fSmsaitoh 		return;
159605f564fSmsaitoh 
160dc98452dSshige 	bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, tag | reg);
161dc98452dSshige 	bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGDATA, data);
162dc98452dSshige 	/* 405GP pass2 errata #6 */
163dc98452dSshige 	bus_space_write_4(&pci_iot, pci_ioh, PCIC_CFGADDR, 0);
164dc98452dSshige }
165dc98452dSshige 
16648ced055Sad int
ibm4xx_pci_intr_setattr(void * v,pci_intr_handle_t * ihp,int attr,uint64_t data)167dd2488a8Smatt ibm4xx_pci_intr_setattr(void *v, pci_intr_handle_t *ihp, int attr,
168dd2488a8Smatt     uint64_t data)
16948ced055Sad {
17048ced055Sad 
17148ced055Sad 	switch (attr) {
17248ced055Sad 	case PCI_INTR_MPSAFE:
17348ced055Sad 		return 0;
17448ced055Sad 	default:
17548ced055Sad 		return ENODEV;
17648ced055Sad 	}
17748ced055Sad }
17848ced055Sad 
179dc98452dSshige /* Avoid overconfiguration */
180dc98452dSshige int
ibm4xx_pci_conf_hook(void * v,int bus,int dev,int func,pcireg_t id)181dd2488a8Smatt ibm4xx_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
182dc98452dSshige {
183dc98452dSshige 
184dc98452dSshige 	if ((PCI_VENDOR(id) == PCI_VENDOR_IBM && PCI_PRODUCT(id) == PCI_PRODUCT_IBM_405GP) ||
185dc98452dSshige 	    (PCI_VENDOR(id) == PCI_VENDOR_INTEL && PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_80960_RP)) {
186dc98452dSshige 		/* Don't configure the bridge and PCI probe. */
187dc98452dSshige 		return 0;
188dc98452dSshige 	}
189*91fd8868Srin 
190*91fd8868Srin #ifdef DHT_FIXUP_PDCIDE
191*91fd8868Srin 	/*
192*91fd8868Srin 	 * Initialize PDC20265 to native-PCI mode. This should be done
193*91fd8868Srin 	 * *before* pci_do_device_query(). Otherwise, we will fail to
194*91fd8868Srin 	 * configure native-PCI IO registers.
195*91fd8868Srin 	 */
196*91fd8868Srin 	if (PCI_VENDOR(id) == PCI_VENDOR_PROMISE &&
197*91fd8868Srin 	    PCI_PRODUCT(id) == PCI_PRODUCT_PROMISE_PDC20265) {
198*91fd8868Srin 		pcitag_t tag;
199*91fd8868Srin 		pcireg_t csr;
200*91fd8868Srin 
201*91fd8868Srin 		tag = ibm4xx_pci_make_tag(v, bus, dev, func);
202*91fd8868Srin 		csr = ibm4xx_pci_conf_read(v, tag, PCI_CLASS_REG);
203*91fd8868Srin 		csr |= (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1))
204*91fd8868Srin 		    << PCI_INTERFACE_SHIFT;
205*91fd8868Srin 		ibm4xx_pci_conf_write(v, tag, PCI_CLASS_REG, csr);
206*91fd8868Srin         }
207*91fd8868Srin #endif
208*91fd8868Srin 
20984308388Sgdamore 	return PCI_CONF_DEFAULT;
210dc98452dSshige }
211