xref: /netbsd-src/sys/arch/arm/xscale/i80312_pci.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: i80312_pci.c,v 1.16 2015/10/02 05:22:50 msaitoh Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * PCI configuration support for i80312 Companion I/O chip.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: i80312_pci.c,v 1.16 2015/10/02 05:22:50 msaitoh Exp $");
44 
45 #include "opt_pci.h"
46 #include "pci.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51 #include <sys/extent.h>
52 #include <sys/malloc.h>
53 #include <sys/bus.h>
54 
55 #include <uvm/uvm_extern.h>
56 
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pciconf.h>
59 #include <dev/pci/ppbreg.h>
60 
61 #include <arm/locore.h>
62 
63 #include <arm/xscale/i80312reg.h>
64 #include <arm/xscale/i80312var.h>
65 
66 void		i80312_pci_attach_hook(device_t, device_t,
67 		    struct pcibus_attach_args *);
68 int		i80312_pci_bus_maxdevs(void *, int);
69 pcitag_t	i80312_pci_make_tag(void *, int, int, int);
70 void		i80312_pci_decompose_tag(void *, pcitag_t, int *, int *,
71 		    int *);
72 pcireg_t	i80312_pci_conf_read(void *, pcitag_t, int);
73 void		i80312_pci_conf_write(void *, pcitag_t, int, pcireg_t);
74 void		i80312_pci_conf_interrupt(void *, int, int, int, int, int *);
75 
76 #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
77 #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
78 
79 void
80 i80312_pci_init(pci_chipset_tag_t pc, void *cookie)
81 {
82 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
83 	struct i80312_softc *sc = cookie;
84 	struct extent *ioext, *memext;
85 	pcireg_t binfo;
86 	int sbus;
87 #endif
88 
89 	pc->pc_conf_v = cookie;
90 	pc->pc_attach_hook = i80312_pci_attach_hook;
91 	pc->pc_bus_maxdevs = i80312_pci_bus_maxdevs;
92 	pc->pc_make_tag = i80312_pci_make_tag;
93 	pc->pc_decompose_tag = i80312_pci_decompose_tag;
94 	pc->pc_conf_read = i80312_pci_conf_read;
95 	pc->pc_conf_write = i80312_pci_conf_write;
96 	pc->pc_conf_interrupt = i80312_pci_conf_interrupt;
97 
98 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
99 	/*
100 	 * Configure the PCI bus.
101 	 *
102 	 * XXX We need to revisit this.  We only configure the Secondary
103 	 * bus (and its children).  The bus configure code needs changes
104 	 * to support how the busses are arranged on this chip.  We also
105 	 * need to only configure devices in the private device space on
106 	 * the Secondary bus.
107 	 */
108 
109 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PPB_REG_BUSINFO);
110 	/* pbus = PPB_BUSINFO_PRIMARY(binfo); */
111 	sbus = PPB_BUSINFO_SECONDARY(binfo);
112 
113 	ioext  = extent_create("pciio", sc->sc_sioout_base,
114 	    sc->sc_sioout_base + sc->sc_sioout_size - 1,
115 	    NULL, 0, EX_NOWAIT);
116 	memext = extent_create("pcimem", sc->sc_smemout_base,
117 	    sc->sc_smemout_base + sc->sc_smemout_size - 1,
118 	    NULL, 0, EX_NOWAIT);
119 
120 	aprint_normal_dev(sc->sc_dev, "configuring Secondary PCI bus\n");
121 	pci_configure_bus(pc, ioext, memext, NULL, sbus, arm_dcache_align);
122 
123 	extent_destroy(ioext);
124 	extent_destroy(memext);
125 #endif
126 }
127 
128 void
129 i80312_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
130 {
131 }
132 
133 void
134 i80312_pci_attach_hook(device_t parent, device_t self,
135     struct pcibus_attach_args *pba)
136 {
137 
138 	/* Nothing to do. */
139 }
140 
141 int
142 i80312_pci_bus_maxdevs(void *v, int busno)
143 {
144 
145 	return (32);
146 }
147 
148 pcitag_t
149 i80312_pci_make_tag(void *v, int b, int d, int f)
150 {
151 
152 	return ((b << 16) | (d << 11) | (f << 8));
153 }
154 
155 void
156 i80312_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
157 {
158 
159 	if (bp != NULL)
160 		*bp = (tag >> 16) & 0xff;
161 	if (dp != NULL)
162 		*dp = (tag >> 11) & 0x1f;
163 	if (fp != NULL)
164 		*fp = (tag >> 8) & 0x7;
165 }
166 
167 struct pciconf_state {
168 	bus_addr_t ps_addr_reg;
169 	bus_addr_t ps_data_reg;
170 	bus_addr_t ps_csr_reg;
171 	uint32_t ps_addr_val;
172 
173 	int ps_b, ps_d, ps_f;
174 };
175 
176 static int
177 i80312_pci_conf_setup(struct i80312_softc *sc, pcitag_t tag, int offset,
178     struct pciconf_state *ps)
179 {
180 	pcireg_t binfo;
181 	int pbus, sbus;
182 
183 	if ((unsigned int)offset >= PCI_CONF_SIZE)
184 		return (1);
185 
186 	i80312_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
187 
188 	binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PPB_REG_BUSINFO);
189 	pbus = PPB_BUSINFO_PRIMARY(binfo);
190 	sbus = PPB_BUSINFO_SECONDARY(binfo);
191 
192 	/*
193 	 * If the bus # is the Primary bus #, use the Primary
194 	 * Address/Data registers, otherwise use the Secondary
195 	 * Address/Data registers.
196 	 */
197 	if (ps->ps_b == pbus) {
198 		ps->ps_addr_reg = I80312_ATU_POCCA;
199 		ps->ps_data_reg = I80312_ATU_POCCD;
200 		ps->ps_csr_reg = PCI_COMMAND_STATUS_REG;
201 	} else {
202 		ps->ps_addr_reg = I80312_ATU_SOCCA;
203 		ps->ps_data_reg = I80312_ATU_SOCCD;
204 		ps->ps_csr_reg = I80312_ATU_SACS;
205 	}
206 
207 	/*
208 	 * If the bus # is the Primary or Secondary bus #, then use
209 	 * Type 0 cycles, else use Type 1.
210 	 *
211 	 * XXX We should filter out all non-private devices here!
212 	 * XXX How does private space interact with PCI-PCI bridges?
213 	 */
214 	if (ps->ps_b == pbus || ps->ps_b == sbus) {
215 		if (ps->ps_d > (31 - 11))
216 			return (1);
217 		ps->ps_addr_val = (1U << (ps->ps_d + 11)) | (ps->ps_f << 8) |
218 		    offset;
219 	} else {
220 		/* The tag is already in the correct format. */
221 		ps->ps_addr_val = tag | offset | 1;
222 	}
223 
224 	return (0);
225 }
226 
227 pcireg_t
228 i80312_pci_conf_read(void *v, pcitag_t tag, int offset)
229 {
230 	struct i80312_softc *sc = v;
231 	struct pciconf_state ps;
232 	vaddr_t va;
233 	pcireg_t rv;
234 	u_int s;
235 
236 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
237 		return ((pcireg_t) -1);
238 
239 	PCI_CONF_LOCK(s);
240 
241 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
242 	    ps.ps_addr_val);
243 
244 	va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
245 	if (badaddr_read((void *) (va + ps.ps_data_reg), sizeof(rv), &rv)) {
246 		/*
247 		 * Clear the Master Abort by reading the PCI
248 		 * Status Register.
249 		 */
250 		(void) bus_space_read_4(sc->sc_st, sc->sc_atu_sh,
251 		    ps.ps_csr_reg);
252 #if 0
253 		printf("conf_read: %d/%d/%d bad address\n",
254 		    ps.ps_b, ps.ps_d, ps.ps_f);
255 #endif
256 		rv = (pcireg_t) -1;
257 	}
258 
259 	PCI_CONF_UNLOCK(s);
260 
261 	return (rv);
262 }
263 
264 void
265 i80312_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
266 {
267 	struct i80312_softc *sc = v;
268 	struct pciconf_state ps;
269 	u_int s;
270 
271 	if (i80312_pci_conf_setup(sc, tag, offset, &ps))
272 		return;
273 
274 	PCI_CONF_LOCK(s);
275 
276 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
277 	    ps.ps_addr_val);
278 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg, val);
279 
280 	PCI_CONF_UNLOCK(s);
281 }
282