xref: /netbsd-src/sys/arch/arm/xscale/i80321_pci.c (revision a4ddc2c8fb9af816efe3b1c375a5530aef0e89e9)
1 /*	$NetBSD: i80321_pci.c,v 1.14 2012/10/14 14:20:57 msaitoh Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 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 i80321 I/O Processor chip.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: i80321_pci.c,v 1.14 2012/10/14 14:20:57 msaitoh Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/extent.h>
49 #include <sys/malloc.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #include <sys/bus.h>
54 
55 #include <arm/xscale/i80321reg.h>
56 #include <arm/xscale/i80321var.h>
57 
58 #include <dev/pci/ppbreg.h>
59 #include <dev/pci/pciconf.h>
60 
61 #include "opt_pci.h"
62 #include "opt_i80321.h"
63 #include "pci.h"
64 
65 void		i80321_pci_attach_hook(device_t, device_t,
66 		    struct pcibus_attach_args *);
67 int		i80321_pci_bus_maxdevs(void *, int);
68 pcitag_t	i80321_pci_make_tag(void *, int, int, int);
69 void		i80321_pci_decompose_tag(void *, pcitag_t, int *, int *,
70 		    int *);
71 pcireg_t	i80321_pci_conf_read(void *, pcitag_t, int);
72 void		i80321_pci_conf_write(void *, pcitag_t, int, pcireg_t);
73 void		i80321_pci_conf_interrupt(void *, int, int, int, int, int *);
74 
75 #define	PCI_CONF_LOCK(s)	(s) = disable_interrupts(I32_bit)
76 #define	PCI_CONF_UNLOCK(s)	restore_interrupts((s))
77 
78 void
79 i80321_pci_init(pci_chipset_tag_t pc, void *cookie)
80 {
81 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
82 	struct i80321_softc *sc = cookie;
83 	struct extent *ioext, *memext;
84 	uint32_t busno;
85 #endif
86 
87 	pc->pc_conf_v = cookie;
88 	pc->pc_attach_hook = i80321_pci_attach_hook;
89 	pc->pc_bus_maxdevs = i80321_pci_bus_maxdevs;
90 	pc->pc_make_tag = i80321_pci_make_tag;
91 	pc->pc_decompose_tag = i80321_pci_decompose_tag;
92 	pc->pc_conf_read = i80321_pci_conf_read;
93 	pc->pc_conf_write = i80321_pci_conf_write;
94 	pc->pc_conf_interrupt = i80321_pci_conf_interrupt;
95 
96 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
97 	/*
98 	 * Configure the PCI bus.
99 	 *
100 	 * XXX We need to revisit this.  We only configure the Secondary
101 	 * bus (and its children).  The bus configure code needs changes
102 	 * to support how the busses are arranged on this chip.  We also
103 	 * need to only configure devices in the private device space on
104 	 * the Secondary bus.
105 	 */
106 
107 	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
108 	busno = PCIXSR_BUSNO(busno);
109 	if (busno == 0xff)
110 		busno = 0;
111 
112 	ioext  = extent_create("pciio",
113 	    sc->sc_ioout_xlate + sc->sc_ioout_xlate_offset,
114 	    sc->sc_ioout_xlate + VERDE_OUT_XLATE_IO_WIN_SIZE - 1,
115 	    NULL, 0, EX_NOWAIT);
116 
117 #ifdef I80321_USE_DIRECT_WIN
118 	memext = extent_create("pcimem", VERDE_OUT_DIRECT_WIN_BASE + VERDE_OUT_DIRECT_WIN_SKIP,
119 	    VERDE_OUT_DIRECT_WIN_BASE + VERDE_OUT_DIRECT_WIN_SIZE- 1,
120 	    NULL, 0, EX_NOWAIT);
121 #else
122 	memext = extent_create("pcimem", sc->sc_owin[0].owin_xlate_lo,
123 	    sc->sc_owin[0].owin_xlate_lo + VERDE_OUT_XLATE_MEM_WIN_SIZE - 1,
124 	    NULL, 0, EX_NOWAIT);
125 #endif
126 
127 	aprint_normal_dev(sc->sc_dev, "configuring PCI bus\n");
128 	pci_configure_bus(pc, ioext, memext, NULL, busno, arm_dcache_align);
129 
130 	extent_destroy(ioext);
131 	extent_destroy(memext);
132 #endif
133 }
134 
135 void
136 i80321_pci_conf_interrupt(void *v, int a, int b, int c, int d, int *p)
137 {
138 }
139 
140 void
141 i80321_pci_attach_hook(device_t parent, device_t self,
142     struct pcibus_attach_args *pba)
143 {
144 
145 	/* Nothing to do. */
146 }
147 
148 int
149 i80321_pci_bus_maxdevs(void *v, int busno)
150 {
151 
152 	return (32);
153 }
154 
155 pcitag_t
156 i80321_pci_make_tag(void *v, int b, int d, int f)
157 {
158 
159 	return ((b << 16) | (d << 11) | (f << 8));
160 }
161 
162 void
163 i80321_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
164 {
165 
166 	if (bp != NULL)
167 		*bp = (tag >> 16) & 0xff;
168 	if (dp != NULL)
169 		*dp = (tag >> 11) & 0x1f;
170 	if (fp != NULL)
171 		*fp = (tag >> 8) & 0x7;
172 }
173 
174 struct pciconf_state {
175 	uint32_t ps_addr_val;
176 
177 	int ps_b, ps_d, ps_f;
178 };
179 
180 static int
181 i80321_pci_conf_setup(struct i80321_softc *sc, pcitag_t tag, int offset,
182     struct pciconf_state *ps)
183 {
184 	uint32_t busno;
185 
186 	i80321_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
187 
188 	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
189 	busno = PCIXSR_BUSNO(busno);
190 	if (busno == 0xff)
191 		busno = 0;
192 
193 	/*
194 	 * If the bus # is the same as our own, then use Type 0 cycles,
195 	 * else use Type 1.
196 	 *
197 	 * XXX We should filter out all non-private devices here!
198 	 * XXX How does private space interact with PCI-PCI bridges?
199 	 */
200 	if (ps->ps_b == busno) {
201 		if (ps->ps_d > (31 - 16))
202 			return (1);
203 		/*
204 		 * NOTE: PCI-X requires that that devices updated their
205 		 * PCIXSR on every config write with the device number
206 		 * specified in AD[15:11].  If we don't set this field,
207 		 * each device could end of thinking it is at device 0,
208 		 * which can cause a number of problems.  Doing this
209 		 * unconditionally should be OK when only PCI devices
210 		 * are present.
211 		 */
212 		ps->ps_addr_val = (1U << (ps->ps_d + 16)) |
213 		    (ps->ps_d << 11) | (ps->ps_f << 8) | offset;
214 	} else {
215 		/* The tag is already in the correct format. */
216 		ps->ps_addr_val = tag | offset | 1;
217 	}
218 
219 	return (0);
220 }
221 
222 pcireg_t
223 i80321_pci_conf_read(void *v, pcitag_t tag, int offset)
224 {
225 	struct i80321_softc *sc = v;
226 	struct pciconf_state ps;
227 	vaddr_t va;
228 	uint32_t isr;
229 	pcireg_t rv;
230 	u_int s;
231 
232 	if (i80321_pci_conf_setup(sc, tag, offset, &ps))
233 		return ((pcireg_t) -1);
234 
235 	PCI_CONF_LOCK(s);
236 
237 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCAR,
238 	    ps.ps_addr_val);
239 
240 	va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
241 	if (badaddr_read((void *) (va + ATU_OCCDR), sizeof(rv), &rv)) {
242 		isr = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_ATUISR);
243 		bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_ATUISR,
244 		    isr & (ATUISR_P_SERR_DET|ATUISR_PMA|ATUISR_PTAM|
245 			   ATUISR_PTAT|ATUISR_PMPE));
246 #if 0
247 		printf("conf_read: %d/%d/%d bad address\n",
248 		    ps.ps_b, ps.ps_d, ps.ps_f);
249 #endif
250 		rv = (pcireg_t) -1;
251 	}
252 
253 	PCI_CONF_UNLOCK(s);
254 
255 	return (rv);
256 }
257 
258 void
259 i80321_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
260 {
261 	struct i80321_softc *sc = v;
262 	struct pciconf_state ps;
263 	u_int s;
264 
265 	if (i80321_pci_conf_setup(sc, tag, offset, &ps))
266 		return;
267 
268 	PCI_CONF_LOCK(s);
269 
270 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCAR,
271 	    ps.ps_addr_val);
272 	bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ATU_OCCDR, val);
273 
274 	PCI_CONF_UNLOCK(s);
275 }
276