xref: /netbsd-src/sys/arch/arm/xscale/i80312_pci.c (revision f82ca6eefb335bf699131a4ebe4cc00c8911db8a)
1*f82ca6eeSskrll /*	$NetBSD: i80312_pci.c,v 1.20 2022/09/27 06:36:43 skrll Exp $	*/
2660b98b7Sthorpej 
3660b98b7Sthorpej /*
4660b98b7Sthorpej  * Copyright (c) 2001 Wasabi Systems, Inc.
5660b98b7Sthorpej  * All rights reserved.
6660b98b7Sthorpej  *
7660b98b7Sthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8660b98b7Sthorpej  *
9660b98b7Sthorpej  * Redistribution and use in source and binary forms, with or without
10660b98b7Sthorpej  * modification, are permitted provided that the following conditions
11660b98b7Sthorpej  * are met:
12660b98b7Sthorpej  * 1. Redistributions of source code must retain the above copyright
13660b98b7Sthorpej  *    notice, this list of conditions and the following disclaimer.
14660b98b7Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
15660b98b7Sthorpej  *    notice, this list of conditions and the following disclaimer in the
16660b98b7Sthorpej  *    documentation and/or other materials provided with the distribution.
17660b98b7Sthorpej  * 3. All advertising materials mentioning features or use of this software
18660b98b7Sthorpej  *    must display the following acknowledgement:
19660b98b7Sthorpej  *	This product includes software developed for the NetBSD Project by
20660b98b7Sthorpej  *	Wasabi Systems, Inc.
21660b98b7Sthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22660b98b7Sthorpej  *    or promote products derived from this software without specific prior
23660b98b7Sthorpej  *    written permission.
24660b98b7Sthorpej  *
25660b98b7Sthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26660b98b7Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27660b98b7Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28660b98b7Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29660b98b7Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30660b98b7Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31660b98b7Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32660b98b7Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33660b98b7Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34660b98b7Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35660b98b7Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
36660b98b7Sthorpej  */
37660b98b7Sthorpej 
38660b98b7Sthorpej /*
39660b98b7Sthorpej  * PCI configuration support for i80312 Companion I/O chip.
40660b98b7Sthorpej  */
41660b98b7Sthorpej 
4208716eaeSlukem #include <sys/cdefs.h>
43*f82ca6eeSskrll __KERNEL_RCSID(0, "$NetBSD: i80312_pci.c,v 1.20 2022/09/27 06:36:43 skrll Exp $");
44213e0bd3Smatt 
45213e0bd3Smatt #include "opt_pci.h"
46213e0bd3Smatt #include "pci.h"
4708716eaeSlukem 
48660b98b7Sthorpej #include <sys/param.h>
49660b98b7Sthorpej #include <sys/systm.h>
50660b98b7Sthorpej #include <sys/device.h>
51213e0bd3Smatt #include <sys/bus.h>
52660b98b7Sthorpej 
53660b98b7Sthorpej #include <uvm/uvm_extern.h>
54660b98b7Sthorpej 
55213e0bd3Smatt #include <dev/pci/pcivar.h>
56213e0bd3Smatt #include <dev/pci/pciconf.h>
57213e0bd3Smatt #include <dev/pci/ppbreg.h>
58213e0bd3Smatt 
59213e0bd3Smatt #include <arm/locore.h>
60660b98b7Sthorpej 
61660b98b7Sthorpej #include <arm/xscale/i80312reg.h>
62660b98b7Sthorpej #include <arm/xscale/i80312var.h>
63660b98b7Sthorpej 
64a2b8c7fbSmsaitoh void		i80312_pci_attach_hook(device_t, device_t,
65660b98b7Sthorpej 		    struct pcibus_attach_args *);
66660b98b7Sthorpej int		i80312_pci_bus_maxdevs(void *, int);
67660b98b7Sthorpej pcitag_t	i80312_pci_make_tag(void *, int, int, int);
68660b98b7Sthorpej void		i80312_pci_decompose_tag(void *, pcitag_t, int *, int *,
69660b98b7Sthorpej 		    int *);
70660b98b7Sthorpej pcireg_t	i80312_pci_conf_read(void *, pcitag_t, int);
71660b98b7Sthorpej void		i80312_pci_conf_write(void *, pcitag_t, int, pcireg_t);
7265993b39Smatt void		i80312_pci_conf_interrupt(void *, int, int, int, int, int *);
73660b98b7Sthorpej 
74660b98b7Sthorpej #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
75660b98b7Sthorpej #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
76660b98b7Sthorpej 
77660b98b7Sthorpej void
i80312_pci_init(pci_chipset_tag_t pc,void * cookie)78660b98b7Sthorpej i80312_pci_init(pci_chipset_tag_t pc, void *cookie)
79660b98b7Sthorpej {
80d32191e3Sthorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
81d32191e3Sthorpej 	struct i80312_softc *sc = cookie;
82ca8ce3aeSthorpej 	struct pciconf_resources *pcires;
83d32191e3Sthorpej 	pcireg_t binfo;
84b8f85410Smartin 	int sbus;
85d32191e3Sthorpej #endif
86660b98b7Sthorpej 
87660b98b7Sthorpej 	pc->pc_conf_v = cookie;
88660b98b7Sthorpej 	pc->pc_attach_hook = i80312_pci_attach_hook;
89660b98b7Sthorpej 	pc->pc_bus_maxdevs = i80312_pci_bus_maxdevs;
90660b98b7Sthorpej 	pc->pc_make_tag = i80312_pci_make_tag;
91660b98b7Sthorpej 	pc->pc_decompose_tag = i80312_pci_decompose_tag;
92660b98b7Sthorpej 	pc->pc_conf_read = i80312_pci_conf_read;
93660b98b7Sthorpej 	pc->pc_conf_write = i80312_pci_conf_write;
9465993b39Smatt 	pc->pc_conf_interrupt = i80312_pci_conf_interrupt;
95d32191e3Sthorpej 
96d32191e3Sthorpej #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
97d32191e3Sthorpej 	/*
98d32191e3Sthorpej 	 * Configure the PCI bus.
99d32191e3Sthorpej 	 *
100e90eccc5Sthorpej 	 * XXX We need to revisit this.  We only configure the Secondary
101e90eccc5Sthorpej 	 * bus (and its children).  The bus configure code needs changes
102e90eccc5Sthorpej 	 * to support how the busses are arranged on this chip.  We also
103e90eccc5Sthorpej 	 * need to only configure devices in the private device space on
104e90eccc5Sthorpej 	 * the Secondary bus.
105d32191e3Sthorpej 	 */
106d32191e3Sthorpej 
10779770474Smsaitoh 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
10879770474Smsaitoh 	/* pbus = PCI_BRIDGE_BUS_NUM_PRIMARY(binfo); */
10979770474Smsaitoh 	sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(binfo);
110d32191e3Sthorpej 
111ca8ce3aeSthorpej 	pcires = pciconf_resource_init();
112ca8ce3aeSthorpej 
113ca8ce3aeSthorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
114ca8ce3aeSthorpej 	    sc->sc_sioout_base, sc->sc_sioout_size);
115ca8ce3aeSthorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
116ca8ce3aeSthorpej 	    sc->sc_smemout_base, sc->sc_smemout_size);
117d32191e3Sthorpej 
118a2b8c7fbSmsaitoh 	aprint_normal_dev(sc->sc_dev, "configuring Secondary PCI bus\n");
119ca8ce3aeSthorpej 	pci_configure_bus(pc, pcires, sbus, arm_dcache_align);
120d32191e3Sthorpej 
121ca8ce3aeSthorpej 	pciconf_resource_fini(pcires);
122d32191e3Sthorpej #endif
123d32191e3Sthorpej }
124d32191e3Sthorpej 
125d32191e3Sthorpej void
i80312_pci_conf_interrupt(void * v,int a,int b,int c,int d,int * p)12665993b39Smatt i80312_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
127d32191e3Sthorpej {
128660b98b7Sthorpej }
129660b98b7Sthorpej 
130660b98b7Sthorpej void
i80312_pci_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)131a2b8c7fbSmsaitoh i80312_pci_attach_hook(device_t parent, device_t self,
132660b98b7Sthorpej     struct pcibus_attach_args *pba)
133660b98b7Sthorpej {
134660b98b7Sthorpej 
135660b98b7Sthorpej 	/* Nothing to do. */
136660b98b7Sthorpej }
137660b98b7Sthorpej 
138660b98b7Sthorpej int
i80312_pci_bus_maxdevs(void * v,int busno)139660b98b7Sthorpej i80312_pci_bus_maxdevs(void *v, int busno)
140660b98b7Sthorpej {
141660b98b7Sthorpej 
142660b98b7Sthorpej 	return (32);
143660b98b7Sthorpej }
144660b98b7Sthorpej 
145660b98b7Sthorpej pcitag_t
i80312_pci_make_tag(void * v,int b,int d,int f)146660b98b7Sthorpej i80312_pci_make_tag(void *v, int b, int d, int f)
147660b98b7Sthorpej {
148660b98b7Sthorpej 
149660b98b7Sthorpej 	return ((b << 16) | (d << 11) | (f << 8));
150660b98b7Sthorpej }
151660b98b7Sthorpej 
152660b98b7Sthorpej void
i80312_pci_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)153660b98b7Sthorpej i80312_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
154660b98b7Sthorpej {
155660b98b7Sthorpej 
156660b98b7Sthorpej 	if (bp != NULL)
157660b98b7Sthorpej 		*bp = (tag >> 16) & 0xff;
158660b98b7Sthorpej 	if (dp != NULL)
159660b98b7Sthorpej 		*dp = (tag >> 11) & 0x1f;
160660b98b7Sthorpej 	if (fp != NULL)
161660b98b7Sthorpej 		*fp = (tag >> 8) & 0x7;
162660b98b7Sthorpej }
163660b98b7Sthorpej 
164660b98b7Sthorpej struct pciconf_state {
165660b98b7Sthorpej 	bus_addr_t ps_addr_reg;
166660b98b7Sthorpej 	bus_addr_t ps_data_reg;
16782c11eecSthorpej 	bus_addr_t ps_csr_reg;
168660b98b7Sthorpej 	uint32_t ps_addr_val;
169660b98b7Sthorpej 
170660b98b7Sthorpej 	int ps_b, ps_d, ps_f;
171660b98b7Sthorpej };
172660b98b7Sthorpej 
173660b98b7Sthorpej static int
i80312_pci_conf_setup(struct i80312_softc * sc,pcitag_t tag,int offset,struct pciconf_state * ps)174660b98b7Sthorpej i80312_pci_conf_setup(struct i80312_softc *sc, pcitag_t tag, int offset,
175660b98b7Sthorpej     struct pciconf_state *ps)
176660b98b7Sthorpej {
177660b98b7Sthorpej 	pcireg_t binfo;
178660b98b7Sthorpej 	int pbus, sbus;
179660b98b7Sthorpej 
180605f564fSmsaitoh 	if ((unsigned int)offset >= PCI_CONF_SIZE)
181605f564fSmsaitoh 		return (1);
182605f564fSmsaitoh 
183660b98b7Sthorpej 	i80312_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
184660b98b7Sthorpej 
18579770474Smsaitoh 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
18679770474Smsaitoh 	pbus = PCI_BRIDGE_BUS_NUM_PRIMARY(binfo);
18779770474Smsaitoh 	sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(binfo);
188660b98b7Sthorpej 
189660b98b7Sthorpej 	/*
190660b98b7Sthorpej 	 * If the bus # is the Primary bus #, use the Primary
191660b98b7Sthorpej 	 * Address/Data registers, otherwise use the Secondary
192660b98b7Sthorpej 	 * Address/Data registers.
193660b98b7Sthorpej 	 */
194660b98b7Sthorpej 	if (ps->ps_b == pbus) {
195660b98b7Sthorpej 		ps->ps_addr_reg = I80312_ATU_POCCA;
196660b98b7Sthorpej 		ps->ps_data_reg = I80312_ATU_POCCD;
19782c11eecSthorpej 		ps->ps_csr_reg = PCI_COMMAND_STATUS_REG;
198660b98b7Sthorpej 	} else {
199660b98b7Sthorpej 		ps->ps_addr_reg = I80312_ATU_SOCCA;
200660b98b7Sthorpej 		ps->ps_data_reg = I80312_ATU_SOCCD;
20182c11eecSthorpej 		ps->ps_csr_reg = I80312_ATU_SACS;
202660b98b7Sthorpej 	}
203660b98b7Sthorpej 
204660b98b7Sthorpej 	/*
205660b98b7Sthorpej 	 * If the bus # is the Primary or Secondary bus #, then use
206660b98b7Sthorpej 	 * Type 0 cycles, else use Type 1.
207660b98b7Sthorpej 	 *
208660b98b7Sthorpej 	 * XXX We should filter out all non-private devices here!
209660b98b7Sthorpej 	 * XXX How does private space interact with PCI-PCI bridges?
210660b98b7Sthorpej 	 */
211660b98b7Sthorpej 	if (ps->ps_b == pbus || ps->ps_b == sbus) {
212660b98b7Sthorpej 		if (ps->ps_d > (31 - 11))
213660b98b7Sthorpej 			return (1);
214660b98b7Sthorpej 		ps->ps_addr_val = (1U << (ps->ps_d + 11)) | (ps->ps_f << 8) |
215660b98b7Sthorpej 		    offset;
216660b98b7Sthorpej 	} else {
217660b98b7Sthorpej 		/* The tag is already in the correct format. */
218660b98b7Sthorpej 		ps->ps_addr_val = tag | offset | 1;
219660b98b7Sthorpej 	}
220660b98b7Sthorpej 
221660b98b7Sthorpej 	return (0);
222660b98b7Sthorpej }
223660b98b7Sthorpej 
224660b98b7Sthorpej pcireg_t
i80312_pci_conf_read(void * v,pcitag_t tag,int offset)225660b98b7Sthorpej i80312_pci_conf_read(void *v, pcitag_t tag, int offset)
226660b98b7Sthorpej {
227660b98b7Sthorpej 	struct i80312_softc *sc = v;
228660b98b7Sthorpej 	struct pciconf_state ps;
229660b98b7Sthorpej 	vaddr_t va;
230660b98b7Sthorpej 	pcireg_t rv;
231660b98b7Sthorpej 	u_int s;
232660b98b7Sthorpej 
233660b98b7Sthorpej 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
234660b98b7Sthorpej 		return ((pcireg_t) -1);
235660b98b7Sthorpej 
236660b98b7Sthorpej 	PCI_CONF_LOCK(s);
237660b98b7Sthorpej 
238660b98b7Sthorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
239660b98b7Sthorpej 	    ps.ps_addr_val);
240660b98b7Sthorpej 
241660b98b7Sthorpej 	va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
242660b98b7Sthorpej 	if (badaddr_read((void *) (va + ps.ps_data_reg), sizeof(rv), &rv)) {
24382c11eecSthorpej 		/*
24482c11eecSthorpej 		 * Clear the Master Abort by reading the PCI
24582c11eecSthorpej 		 * Status Register.
24682c11eecSthorpej 		 */
24782c11eecSthorpej 		(void) bus_space_read_4(sc->sc_st, sc->sc_atu_sh,
24882c11eecSthorpej 		    ps.ps_csr_reg);
24982c11eecSthorpej #if 0
250660b98b7Sthorpej 		printf("conf_read: %d/%d/%d bad address\n",
251660b98b7Sthorpej 		    ps.ps_b, ps.ps_d, ps.ps_f);
25282c11eecSthorpej #endif
253660b98b7Sthorpej 		rv = (pcireg_t) -1;
254660b98b7Sthorpej 	}
255660b98b7Sthorpej 
256660b98b7Sthorpej 	PCI_CONF_UNLOCK(s);
257660b98b7Sthorpej 
258660b98b7Sthorpej 	return (rv);
259660b98b7Sthorpej }
260660b98b7Sthorpej 
261660b98b7Sthorpej void
i80312_pci_conf_write(void * v,pcitag_t tag,int offset,pcireg_t val)262660b98b7Sthorpej i80312_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
263660b98b7Sthorpej {
264660b98b7Sthorpej 	struct i80312_softc *sc = v;
265660b98b7Sthorpej 	struct pciconf_state ps;
266660b98b7Sthorpej 	u_int s;
267660b98b7Sthorpej 
268660b98b7Sthorpej 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
269660b98b7Sthorpej 		return;
270660b98b7Sthorpej 
271660b98b7Sthorpej 	PCI_CONF_LOCK(s);
272660b98b7Sthorpej 
273660b98b7Sthorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
274660b98b7Sthorpej 	    ps.ps_addr_val);
275660b98b7Sthorpej 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg, val);
276660b98b7Sthorpej 
277660b98b7Sthorpej 	PCI_CONF_UNLOCK(s);
278660b98b7Sthorpej }
279