rmixl_pcix.c revision 1.1.2.2 1 /* $NetBSD: rmixl_pcix.c,v 1.1.2.2 2010/04/12 22:03:33 cliff 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 RMI XLR SoC
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: rmixl_pcix.c,v 1.1.2.2 2010/04/12 22:03:33 cliff Exp $");
44
45 #include "opt_pci.h"
46 #include "pci.h"
47
48 #include <sys/cdefs.h>
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/extent.h>
54 #include <sys/malloc.h>
55
56 #include <uvm/uvm_extern.h>
57
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60
61 #include <mips/rmi/rmixlreg.h>
62 #include <mips/rmi/rmixlvar.h>
63 #include <mips/rmi/rmixl_intr.h>
64 #include <mips/rmi/rmixl_pcixvar.h>
65
66 #include <mips/rmi/rmixl_obiovar.h>
67
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcidevs.h>
70 #include <dev/pci/pciconf.h>
71
72 #ifdef PCI_NETBSD_CONFIGURE
73 #include <mips/cache.h>
74 #endif
75
76 #include <machine/pci_machdep.h>
77
78 #ifdef PCI_DEBUG
79 int rmixl_pcix_debug = PCI_DEBUG;
80 # define DPRINTF(x) do { if (rmixl_pcix_debug) printf x ; } while (0)
81 #else
82 # define DPRINTF(x)
83 #endif
84
85 #ifndef DDB
86 # define STATIC static
87 #else
88 # define STATIC
89 #endif
90
91
92 /*
93 * XLR PCI-X Extended Configuration Registers
94 * Note:
95 * - MSI-related regs are omitted
96 * - Device mode regs are omitted
97 */
98 #define RMIXL_PCIX_ECFG_HOST_BAR0_ADDR 0x100 /* Host BAR0 Address */
99 #define RMIXL_PCIX_ECFG_HOST_BAR1_ADDR 0x104 /* Host BAR1 Address */
100 #define RMIXL_PCIX_ECFG_HOST_BAR2_ADDR 0x108 /* Host BAR2 Address */
101 #define RMIXL_PCIX_ECFG_HOST_BAR3_ADDR 0x10c /* Host BAR3 Address */
102 #define RMIXL_PCIX_ECFG_HOST_BAR4_ADDR 0x110 /* Host BAR4 Address */
103 #define RMIXL_PCIX_ECFG_HOST_BAR5_ADDR 0x114 /* Host BAR5 Address */
104 #define RMIXL_PCIX_ECFG_HOST_BAR0_SIZE 0x118 /* Host BAR0 Size */
105 #define RMIXL_PCIX_ECFG_HOST_BAR1_SIZE 0x11c /* Host BAR1 Size */
106 #define RMIXL_PCIX_ECFG_HOST_BAR2_SIZE 0x120 /* Host BAR2 Size */
107 #define RMIXL_PCIX_ECFG_HOST_BAR3_SIZE 0x124 /* Host BAR3 Size */
108 #define RMIXL_PCIX_ECFG_HOST_BAR4_SIZE 0x128 /* Host BAR4 Size */
109 #define RMIXL_PCIX_ECFG_HOST_BAR5_SIZE 0x12c /* Host BAR5 Size */
110 #define RMIXL_PCIX_ECFG_MATCH_BIT_ADDR 0x130 /* Match Bit Address BAR */
111 #define RMIXL_PCIX_ECFG_MATCH_BIT_SIZE 0x134 /* Match Bit Size BAR */
112 #define RMIXL_PCIX_ECFG_XLR_CONTROL 0x138 /* XLR Control reg */
113 #define RMIXL_PCIX_ECFG_INTR_CONTROL 0x13c /* Interrupt Control reg */
114 #define RMIXL_PCIX_ECFG_INTR_STATUS 0x140 /* Interrupt Status reg */
115 #define RMIXL_PCIX_ECFG_INTR_ERR_STATUS 0x144 /* Interrupt Error Status reg */
116 #define RMIXL_PCIX_ECFG_HOST_MODE_STS 0x178 /* Host Mode Status */
117 #define RMIXL_PCIX_ECFG_XLR_MBLE 0x17c /* XLR Match Byte Lane Enable */
118 #define RMIXL_PCIX_ECFG_HOST_XROM_ADDR 0x180 /* Host Expansion ROM Address */
119 #define RMIXL_PCIX_ECFG_HOST_XROM_SIZE 0x184 /* Host Expansion ROM Size */
120 #define RMIXL_PCIX_ECFG_HOST_MODE_CTL 0x18c /* Host Mode Control */
121 #define RMIXL_PCIX_ECFG_TXCAL_CTL 0x1a0 /* TX Calibration Preset Control */
122 #define RMIXL_PCIX_ECFG_TXCAL_COUNT 0x1a4 /* TX Calibration Preset Count */
123
124 /*
125 * RMIXL_PCIX_ECFG_INTR_CONTROL bit defines
126 */
127 #define PCIX_INTR_CONTROL_RESV __BITS(31,8)
128 #define PCIX_INTR_CONTROL_MSI1_MASK __BIT(7)
129 #define PCIX_INTR_CONTROL_MSI0_MASK __BIT(6)
130 #define PCIX_INTR_CONTROL_INTD_MASK __BIT(5)
131 #define PCIX_INTR_CONTROL_INTC_MASK __BIT(4)
132 #define PCIX_INTR_CONTROL_INTB_MASK __BIT(3)
133 #define PCIX_INTR_CONTROL_INTA_MASK __BIT(2)
134 #define PCIX_INTR_CONTROL_TMSI __BIT(1) /* Trigger MSI Interrupt */
135 #define PCIX_INTR_CONTROL_DIA __BIT(0) /* Device Interrupt through INTA Pin */
136 #define PCIX_INTR_CONTROL_MASK_ALL \
137 (PCIX_INTR_CONTROL_MSI1_MASK|PCIX_INTR_CONTROL_MSI0_MASK \
138 |PCIX_INTR_CONTROL_INTD_MASK|PCIX_INTR_CONTROL_INTC_MASK \
139 |PCIX_INTR_CONTROL_INTB_MASK|PCIX_INTR_CONTROL_INTA_MASK)
140
141 /*
142 * RMIXL_PCIX_ECFG_INTR_STATUS bit defines
143 */
144 #define PCIX_INTR_STATUS_RESV __BITS(31,6)
145 #define PCIX_INTR_STATUS_MSI1 __BIT(5)
146 #define PCIX_INTR_STATUS_MSI0 __BIT(4)
147 #define PCIX_INTR_STATUS_INTD __BIT(3)
148 #define PCIX_INTR_STATUS_INTC __BIT(2)
149 #define PCIX_INTR_STATUS_INTB __BIT(1)
150 #define PCIX_INTR_STATUS_INTA __BIT(0)
151
152 /*
153 * RMIXL_PCIX_ECFG_INTR_ERR_STATUS bit defines
154 */
155 #define PCIX_INTR_ERR_STATUS_RESa __BITS(31,5)
156 #define PCIX_INTR_ERR_STATUS_SERR __BIT(4) /* System Error */
157 #define PCIX_INTR_ERR_STATUS_RESb __BIT(3)
158 #define PCIX_INTR_ERR_STATUS_TE __BIT(2) /* Target Error */
159 #define PCIX_INTR_ERR_STATUS_IE __BIT(1) /* Initiator Error */
160 #define PCIX_INTR_ERR_STATUS_RCE __BIT(0) /* Retry Count Expired */
161 #define PCIX_INTR_ERR_STATUS_RESV \
162 (PCIX_INTR_ERR_STATUS_RESa|PCIX_INTR_ERR_STATUS_RESb)
163
164 /*
165 * RMIXL_PCIX_ECFG_HOST_MODE_CTL bit defines
166 */
167 #define PCIX_HOST_MODE_CTL_HDMSTAT __BIT(1) /* Host/Dev Mode status
168 * read-only
169 * 1 = host
170 * 0 = device
171 */
172 #define PCIX_HOST_MODE_CTL_HOSTSWRST __BIT(0) /* Host soft reset
173 * set to 1 to reset
174 * set to 0 to un-reset
175 */
176
177
178 #if BYTE_ORDER == BIG_ENDIAN
179 # define RMIXL_PCIXREG_BASE RMIXL_IO_DEV_PCIX_EB
180 #else
181 # define RMIXL_PCIXREG_BASE RMIXL_IO_DEV_PCIX_EL
182 #endif
183
184 #define RMIXL_PCIXREG_VADDR(o) \
185 (volatile uint32_t *)MIPS_PHYS_TO_KSEG1( \
186 rmixl_configuration.rc_io_pbase \
187 + RMIXL_PCIXREG_BASE + (o))
188
189 #define RMIXL_PCIXREG_READ(o) (*RMIXL_PCIXREG_VADDR(o))
190 #define RMIXL_PCIXREG_WRITE(o,v) *RMIXL_PCIXREG_VADDR(o) = (v)
191
192 /*
193 * XXX use locks
194 */
195 #define PCI_CONF_LOCK(s) (s) = splhigh()
196 #define PCI_CONF_UNLOCK(s) splx((s))
197
198
199 #define RMIXL_PCIX_CONCAT3(a,b,c) a ## b ## c
200 #define RMIXL_PCIX_BAR_INIT(reg, bar, size, align) { \
201 struct extent *ext = rmixl_configuration.rc_phys_ex; \
202 u_long region_start; \
203 uint64_t ba; \
204 int err; \
205 \
206 err = extent_alloc(ext, (size), (align), 0UL, EX_NOWAIT, \
207 ®ion_start); \
208 if (err != 0) \
209 panic("%s: extent_alloc(%p, %#lx, %#lx, %#lx, %#x, %p)",\
210 __func__, ext, size, align, 0UL, EX_NOWAIT, \
211 ®ion_start); \
212 ba = (uint64_t)region_start; \
213 ba *= (1024 * 1024); \
214 bar = RMIXL_PCIX_CONCAT3(RMIXL_PCIX_,reg,_BAR)(ba, 1); \
215 DPRINTF(("PCIX %s BAR was not enabled by firmware\n" \
216 "enabling %s at phys %#" PRIxBUSADDR ", size %lu MB\n", \
217 __STRING(reg), __STRING(reg), ba, size)); \
218 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_BRIDGE + \
219 RMIXL_PCIX_CONCAT3(RMIXLR_SBC_PCIX_,reg,_BAR), bar); \
220 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + \
221 RMIXL_PCIX_CONCAT3(RMIXLR_SBC_PCIX_,reg,_BAR)); \
222 DPRINTF(("%s: %s BAR %#x\n", __func__, __STRING(reg), bar)); \
223 }
224
225 static int rmixl_pcix_match(device_t, cfdata_t, void *);
226 static void rmixl_pcix_attach(device_t, device_t, void *);
227 static void rmixl_pcix_init(rmixl_pcix_softc_t *);
228 static void rmixl_pcix_init_errors(rmixl_pcix_softc_t *);
229 static void rmixl_pcix_attach_hook(struct device *, struct device *,
230 struct pcibus_attach_args *);
231 static void rmixl_pcix_intcfg(rmixl_pcix_softc_t *);
232 static void rmixl_pcix_errata(rmixl_pcix_softc_t *);
233 static void rmixl_conf_interrupt(void *, int, int, int, int, int *);
234 static int rmixl_pcix_bus_maxdevs(void *, int);
235 static pcitag_t rmixl_pcix_make_tag(void *, int, int, int);
236 static void rmixl_pcix_decompose_tag(void *, pcitag_t, int *, int *, int *);
237 void rmixl_pcix_tag_print(const char *restrict, void *, pcitag_t, int, vaddr_t, u_long);
238 static int rmixl_pcix_conf_setup(rmixl_pcix_softc_t *,
239 pcitag_t, int *, bus_space_tag_t *,
240 bus_space_handle_t *);
241 static pcireg_t rmixl_pcix_conf_read(void *, pcitag_t, int);
242 static void rmixl_pcix_conf_write(void *, pcitag_t, int, pcireg_t);
243
244 static int rmixl_pcix_intr_map(struct pci_attach_args *,
245 pci_intr_handle_t *);
246 static const char *
247 rmixl_pcix_intr_string(void *, pci_intr_handle_t);
248 static const struct evcnt *
249 rmixl_pcix_intr_evcnt(void *, pci_intr_handle_t);
250 static pci_intr_handle_t
251 rmixl_pcix_make_pih(u_int, u_int);
252 static void rmixl_pcix_decompose_pih(pci_intr_handle_t, u_int *, u_int *);
253 static void rmixl_pcix_intr_disestablish(void *, void *);
254 static void *rmixl_pcix_intr_establish(void *, pci_intr_handle_t,
255 int, int (*)(void *), void *);
256 static int rmixl_pcix_intr(void *);
257 static int rmixl_pcix_error_intr(void *);
258
259
260 CFATTACH_DECL_NEW(rmixl_pcix, sizeof(rmixl_pcix_softc_t),
261 rmixl_pcix_match, rmixl_pcix_attach, NULL, NULL);
262
263
264 static int rmixl_pcix_found;
265
266 #if defined(DIAGNOSTIC) || defined(DEBUG) || defined(DDB)
267 static rmixl_pcix_softc_t *rmixl_pcix_sc;
268 #endif
269
270
271 static int
272 rmixl_pcix_match(device_t parent, cfdata_t cf, void *aux)
273 {
274 uint32_t r;
275
276 /*
277 * PCI-X interface exists on XLR chips only
278 */
279 if (! cpu_rmixlr(mips_options.mips_cpu))
280 return 0;
281
282 /* XXX
283 * for now there is only one PCI-X Interface on chip
284 * and only one chip in the system
285 * this could change with furture RMI XL family designs
286 * or when we have multi-chip systems.
287 */
288 if (rmixl_pcix_found)
289 return 0;
290
291 /* read Host Mode Control register */
292 r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_MODE_CTL);
293 r &= PCIX_HOST_MODE_CTL_HDMSTAT;
294 if (r == 0)
295 return 0; /* strapped for Device Mode */
296
297 return 1;
298 }
299
300 static void
301 rmixl_pcix_attach(device_t parent, device_t self, void *aux)
302 {
303 rmixl_pcix_softc_t *sc = device_private(self);
304 struct obio_attach_args *obio = aux;
305 struct rmixl_config *rcp = &rmixl_configuration;
306 struct pcibus_attach_args pba;
307 uint32_t bar;
308
309 rmixl_pcix_found = 1;
310 #ifdef DIAGNOSTIC
311 rmixl_pcix_sc = sc;
312 #endif
313 sc->sc_dev = self;
314 sc->sc_29bit_dmat = obio->obio_29bit_dmat;
315 sc->sc_32bit_dmat = obio->obio_32bit_dmat;
316 sc->sc_64bit_dmat = obio->obio_64bit_dmat;
317 sc->sc_tmsk = obio->obio_tmsk;
318
319 aprint_normal(" RMI XLR PCI-X Interface\n");
320
321 rmixl_pcix_intcfg(sc);
322
323 rmixl_pcix_errata(sc);
324
325 /*
326 * check XLR Control Register
327 */
328 DPRINTF(("%s: XLR_CONTROL=%#x\n", __func__,
329 RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_CONTROL)));
330
331 /*
332 * HBAR[0] if a 32 bit BAR, or
333 * HBAR[0,1] if a 64 bit BAR pair
334 * must cover all RAM
335 */
336 extern u_quad_t mem_cluster_maxaddr;
337 uint64_t hbar_addr;
338 uint64_t hbar_size;
339 uint32_t hbar_size_lo, hbar_size_hi;
340 uint32_t hbar_addr_lo, hbar_addr_hi;
341
342 hbar_addr_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_ADDR);
343 hbar_addr_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_ADDR);
344 hbar_size_lo = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR0_SIZE);
345 hbar_size_hi = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_HOST_BAR1_SIZE);
346
347 hbar_addr = (u_quad_t)(hbar_addr_lo & PCI_MAPREG_MEM_ADDR_MASK);
348 hbar_size = hbar_size_lo;
349 if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0) {
350 hbar_addr |= (uint64_t)hbar_addr_hi << 32;
351 hbar_size |= (uint64_t)hbar_size_hi << 32;
352 }
353 if ((hbar_addr != 0) || (hbar_size < mem_cluster_maxaddr)) {
354 printf("%s: HostBAR0 addr %#x, size %#x\n",
355 device_xname(self), hbar_addr_lo, hbar_size_lo);
356 if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0)
357 printf("%s: HostBAR1 addr %#x, size %#x\n",
358 device_xname(self), hbar_addr_hi, hbar_size_hi);
359 panic("PCI-X Host BAR does not cover RAM range 0..%#"PRIx64,
360 mem_cluster_maxaddr);
361 }
362
363 /*
364 * check PCI-X interface byteswap setup
365 * ensure 'Match Byte Lane' is disabled
366 */
367 uint32_t mble, mba, mbs;
368 mble = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_MBLE);
369 mba = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_ADDR);
370 mbs = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_SIZE);
371 DPRINTF(("%s: MBLE=%#x, MBA=%#x, MBS=%#x\n", __func__, mble, mba, mbs));
372 if ((mble & __BIT(40)) != 0)
373 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_XLR_MBLE, 0);
374
375 /*
376 * get PCI config space base addr from SBC PCIe CFG BAR
377 * initialize it if necessary
378 */
379 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_CFG_BAR);
380 DPRINTF(("%s: PCIX_CFG_BAR %#x\n", __func__, bar));
381 if ((bar & RMIXL_PCIX_CFG_BAR_ENB) == 0) {
382 u_long n = RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
383 RMIXL_PCIX_BAR_INIT(CFG, bar, n, n);
384 }
385 rcp->rc_pci_cfg_pbase = (bus_addr_t)RMIXL_PCIX_CFG_BAR_TO_BA(bar);
386 rcp->rc_pci_cfg_size = (bus_size_t)RMIXL_PCIX_CFG_SIZE;
387
388 /*
389 * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
390 * initialize it if necessary
391 */
392 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
393 DPRINTF(("%s: PCIX_MEM_BAR %#x\n", __func__, bar));
394 if ((bar & RMIXL_PCIX_MEM_BAR_ENB) == 0) {
395 u_long n = 256; /* 256 MB */
396 RMIXL_PCIX_BAR_INIT(MEM, bar, n, n);
397 }
398 rcp->rc_pci_mem_pbase = (bus_addr_t)RMIXL_PCIX_MEM_BAR_TO_BA(bar);
399 rcp->rc_pci_mem_size = (bus_size_t)RMIXL_PCIX_MEM_BAR_TO_SIZE(bar);
400
401 /*
402 * get PCI IO space base [addr, size] from SBC PCIe IO BAR
403 * initialize it if necessary
404 */
405 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
406 DPRINTF(("%s: PCIX_IO_BAR %#x\n", __func__, bar));
407 if ((bar & RMIXL_PCIX_IO_BAR_ENB) == 0) {
408 u_long n = 32; /* 32 MB */
409 RMIXL_PCIX_BAR_INIT(IO, bar, n, n);
410 }
411 rcp->rc_pci_io_pbase = (bus_addr_t)RMIXL_PCIX_IO_BAR_TO_BA(bar);
412 rcp->rc_pci_io_size = (bus_size_t)RMIXL_PCIX_IO_BAR_TO_SIZE(bar);
413
414 /*
415 * initialize the PCI CFG bus space tag
416 */
417 rmixl_pci_cfg_bus_mem_init(&rcp->rc_pci_cfg_memt, rcp);
418 sc->sc_pci_cfg_memt = &rcp->rc_pci_cfg_memt;
419
420 /*
421 * initialize the PCI MEM and IO bus space tags
422 */
423 rmixl_pci_bus_mem_init(&rcp->rc_pci_memt, rcp);
424 rmixl_pci_bus_io_init(&rcp->rc_pci_iot, rcp);
425
426 /*
427 * initialize the extended configuration regs
428 */
429 rmixl_pcix_init_errors(sc);
430
431 /*
432 * initialize the PCI chipset tag
433 */
434 rmixl_pcix_init(sc);
435
436 /*
437 * attach the PCI bus
438 */
439 memset(&pba, 0, sizeof(pba));
440 pba.pba_memt = &rcp->rc_pci_memt;
441 pba.pba_iot = &rcp->rc_pci_iot;
442 pba.pba_dmat = sc->sc_32bit_dmat;
443 pba.pba_dmat64 = sc->sc_64bit_dmat;
444 pba.pba_pc = &sc->sc_pci_chipset;
445 pba.pba_bus = 0;
446 pba.pba_bridgetag = NULL;
447 pba.pba_intrswiz = 0;
448 pba.pba_intrtag = 0;
449 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
450 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
451
452 (void) config_found_ia(self, "pcibus", &pba, pcibusprint);
453 }
454
455 /*
456 * rmixl_pcix_intcfg - init PCI-X interrupt control
457 */
458 static void
459 rmixl_pcix_intcfg(rmixl_pcix_softc_t *sc)
460 {
461 DPRINTF(("%s\n", __func__));
462
463 /* mask all interrupts until they are established */
464 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL,
465 PCIX_INTR_CONTROL_MASK_ALL);
466
467 /*
468 * read-to-clear any pre-existing interrupts
469 * XXX MSI bits in STATUS are also documented as write 1 to clear in PRM
470 */
471 (void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
472 (void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
473
474 /* initialize the dispatch handles */
475 for (int i=0; i < RMIXL_PCIX_NINTR; i++) {
476 rmixl_pcix_intr_t *ih = &sc->sc_intr[i];
477 LIST_INIT(&ih->dispatch);
478 ih->ih = NULL;
479 ih->intrpin = i;
480 ih->enabled = false;
481 }
482
483 sc->sc_ih = rmixl_intr_establish(16, sc->sc_tmsk,
484 IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
485 rmixl_pcix_intr, sc);
486 if (sc->sc_ih == NULL)
487 panic("%s: cannot establish irq %d", __func__, 16);
488
489 sc->sc_fatal_ih = rmixl_intr_establish(24, sc->sc_tmsk,
490 IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
491 rmixl_pcix_error_intr, sc);
492 if (sc->sc_fatal_ih == NULL)
493 panic("%s: cannot establish irq %d", __func__, 24);
494
495 sc->sc_intr_init_done = true;
496 }
497
498 static void
499 rmixl_pcix_errata(rmixl_pcix_softc_t *sc)
500 {
501 /* nothing */
502 }
503
504 static void
505 rmixl_pcix_init(rmixl_pcix_softc_t *sc)
506 {
507 pci_chipset_tag_t pc = &sc->sc_pci_chipset;
508 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
509 struct extent *ioext, *memext;
510 #endif
511
512 pc->pc_conf_v = (void *)sc;
513 pc->pc_attach_hook = rmixl_pcix_attach_hook;
514 pc->pc_bus_maxdevs = rmixl_pcix_bus_maxdevs;
515 pc->pc_make_tag = rmixl_pcix_make_tag;
516 pc->pc_decompose_tag = rmixl_pcix_decompose_tag;
517 pc->pc_conf_read = rmixl_pcix_conf_read;
518 pc->pc_conf_write = rmixl_pcix_conf_write;
519
520 pc->pc_intr_v = (void *)sc;
521 pc->pc_intr_map = rmixl_pcix_intr_map;
522 pc->pc_intr_string = rmixl_pcix_intr_string;
523 pc->pc_intr_evcnt = rmixl_pcix_intr_evcnt;
524 pc->pc_intr_establish = rmixl_pcix_intr_establish;
525 pc->pc_intr_disestablish = rmixl_pcix_intr_disestablish;
526 pc->pc_conf_interrupt = rmixl_conf_interrupt;
527
528 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
529 /*
530 * Configure the PCI bus.
531 */
532 struct rmixl_config *rcp = &rmixl_configuration;
533
534 aprint_normal("%s: configuring PCI bus\n",
535 device_xname(sc->sc_dev));
536
537 ioext = extent_create("pciio",
538 rcp->rc_pci_io_pbase,
539 rcp->rc_pci_io_pbase + rcp->rc_pci_io_size - 1,
540 M_DEVBUF, NULL, 0, EX_NOWAIT);
541
542 memext = extent_create("pcimem",
543 rcp->rc_pci_mem_pbase,
544 rcp->rc_pci_mem_pbase + rcp->rc_pci_mem_size - 1,
545 M_DEVBUF, NULL, 0, EX_NOWAIT);
546
547 pci_configure_bus(pc, ioext, memext, NULL, 0,
548 mips_cache_info.mci_dcache_align);
549
550 extent_destroy(ioext);
551 extent_destroy(memext);
552 #endif
553 }
554
555 static void
556 rmixl_pcix_init_errors(rmixl_pcix_softc_t *sc)
557 {
558 /* nothing */
559 }
560
561 void
562 rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
563 {
564 DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
565 __func__, v, bus, dev, ipin, swiz, iline));
566 }
567
568 void
569 rmixl_pcix_attach_hook(struct device *parent, struct device *self,
570 struct pcibus_attach_args *pba)
571 {
572 DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
573 __func__, pba->pba_bus, pba->pba_bridgetag,
574 pba->pba_pc->pc_conf_v));
575 }
576
577 int
578 rmixl_pcix_bus_maxdevs(void *v, int busno)
579 {
580 return (32); /* XXX depends on the family of XLS SoC */
581 }
582
583 /*
584 * XLS pci tag is a 40 bit address composed thusly:
585 * 39:25 (reserved)
586 * 24 Swap (0=little, 1=big endian)
587 * 23:16 Bus number
588 * 15:11 Device number
589 * 10:8 Function number
590 * 7:0 Register number
591 *
592 * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
593 */
594 pcitag_t
595 rmixl_pcix_make_tag(void *v, int bus, int dev, int fun)
596 {
597 return ((bus << 16) | (dev << 11) | (fun << 8));
598 }
599
600 void
601 rmixl_pcix_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
602 {
603 if (bp != NULL)
604 *bp = (tag >> 16) & 0xff;
605 if (dp != NULL)
606 *dp = (tag >> 11) & 0x1f;
607 if (fp != NULL)
608 *fp = (tag >> 8) & 0x7;
609 }
610
611 void
612 rmixl_pcix_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
613 vaddr_t va, u_long r)
614 {
615 int bus, dev, fun;
616
617 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
618 printf("%s: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
619 s, bus, dev, fun, offset, va, r);
620 }
621
622 static int
623 rmixl_pcix_conf_setup(rmixl_pcix_softc_t *sc,
624 pcitag_t tag, int *offp, bus_space_tag_t *bstp,
625 bus_space_handle_t *bshp)
626 {
627 struct rmixl_config *rcp = &rmixl_configuration;
628 bus_space_tag_t bst;
629 bus_space_handle_t bsh;
630 bus_size_t size;
631 pcitag_t mask;
632 bus_addr_t ba;
633 int err;
634 static bus_space_handle_t cfg_bsh;
635 static bus_addr_t cfg_oba = -1;
636
637 /*
638 * bus space depends on offset
639 */
640 if ((*offp >= 0) && (*offp < 0x100)) {
641 mask = __BITS(15,0);
642 bst = sc->sc_pci_cfg_memt;
643 ba = rcp->rc_pci_cfg_pbase;
644 ba += (tag & ~mask);
645 *offp += (tag & mask);
646 if (ba != cfg_oba) {
647 size = (bus_size_t)(mask + 1);
648 if (cfg_oba != -1)
649 bus_space_unmap(bst, cfg_bsh, size);
650 err = bus_space_map(bst, ba, size, 0, &cfg_bsh);
651 if (err != 0) {
652 #ifdef DEBUG
653 panic("%s: bus_space_map err %d, CFG space",
654 __func__, err); /* XXX */
655 #endif
656 return -1;
657 }
658 cfg_oba = ba;
659 }
660 bsh = cfg_bsh;
661 } else {
662 #ifdef DEBUG
663 panic("%s: offset %#x: unknown", __func__, *offp);
664 #endif
665 return -1;
666 }
667
668 *bstp = bst;
669 *bshp = bsh;
670
671 return 0;
672 }
673
674 pcireg_t
675 rmixl_pcix_conf_read(void *v, pcitag_t tag, int offset)
676 {
677 rmixl_pcix_softc_t *sc = v;
678 static bus_space_handle_t bsh;
679 bus_space_tag_t bst;
680 pcireg_t rv;
681 uint64_t cfg0;
682 u_int s;
683
684 PCI_CONF_LOCK(s);
685
686 if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
687 cfg0 = rmixl_cache_err_dis();
688 rv = bus_space_read_4(bst, bsh, (bus_size_t)offset);
689 if (rmixl_cache_err_check() != 0) {
690 #ifdef DIAGNOSTIC
691 int bus, dev, fun;
692
693 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
694 printf("%s: %d/%d/%d, offset %#x: bad address\n",
695 __func__, bus, dev, fun, offset);
696 #endif
697 rv = (pcireg_t) -1;
698 }
699 rmixl_cache_err_restore(cfg0);
700 } else {
701 rv = -1;
702 }
703
704 PCI_CONF_UNLOCK(s);
705 return rv;
706 }
707
708 void
709 rmixl_pcix_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
710 {
711 rmixl_pcix_softc_t *sc = v;
712 static bus_space_handle_t bsh;
713 bus_space_tag_t bst;
714 uint64_t cfg0;
715 u_int s;
716
717 PCI_CONF_LOCK(s);
718
719 if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
720 cfg0 = rmixl_cache_err_dis();
721 bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
722 if (rmixl_cache_err_check() != 0) {
723 #ifdef DIAGNOSTIC
724 int bus, dev, fun;
725
726 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
727 printf("%s: %d/%d/%d, offset %#x: bad address\n",
728 __func__, bus, dev, fun, offset);
729 #endif
730 }
731 rmixl_cache_err_restore(cfg0);
732 }
733
734 PCI_CONF_UNLOCK(s);
735 }
736
737 int
738 rmixl_pcix_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *pih)
739 {
740 const u_int irq = 16; /* PCIX index in IRT */
741
742 #ifdef DEBUG
743 DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
744 " pa_intrpin %d, pa_intrline %d, pa_rawintrpin %d\n",
745 __func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
746 pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
747 #endif
748
749 if (pa->pa_intrpin != PCI_INTERRUPT_PIN_NONE)
750 *pih = rmixl_pcix_make_pih(pa->pa_intrpin - 1, irq);
751 else
752 *pih = ~0;
753
754 return 0;
755 }
756
757 const char *
758 rmixl_pcix_intr_string(void *v, pci_intr_handle_t pih)
759 {
760 u_int bitno, irq;
761
762 rmixl_pcix_decompose_pih(pih, &bitno, &irq);
763
764 if (! cpu_rmixlr(mips_options.mips_cpu))
765 panic("%s: cpu %#x not supported\n",
766 __func__, mips_options.mips_cpu_id);
767
768 return rmixl_intr_string(irq);
769 }
770
771 const struct evcnt *
772 rmixl_pcix_intr_evcnt(void *v, pci_intr_handle_t pih)
773 {
774 return NULL;
775 }
776
777 static pci_intr_handle_t
778 rmixl_pcix_make_pih(u_int bitno, u_int irq)
779 {
780 pci_intr_handle_t pih;
781
782 KASSERT(bitno < 64);
783 KASSERT(irq < 32);
784
785 pih = (irq << 6);
786 pih |= bitno;
787
788 return pih;
789 }
790
791 static void
792 rmixl_pcix_decompose_pih(pci_intr_handle_t pih, u_int *bitno, u_int *irq)
793 {
794 *bitno = (u_int)(pih & 0x3f);
795 *irq = (u_int)(pih >> 6);
796
797 KASSERT(*bitno < 64);
798 KASSERT(*irq < 31);
799 }
800
801 static void
802 rmixl_pcix_intr_disestablish(void *v, void *ih)
803 {
804 rmixl_pcix_softc_t *sc = v;
805 rmixl_pcix_dispatch_t *dip = ih;
806 rmixl_pcix_intr_t *pip = &sc->sc_intr[dip->bitno];;
807
808 DPRINTF(("%s: pin=%d irq=%d\n",
809 __func__, dip->bitno + 1, dip->irq));
810 KASSERT(dip->bitno < RMIXL_PCIX_NINTR);
811
812 LIST_REMOVE(dip, next);
813 evcnt_detach(&dip->count);
814 free(dip, M_DEVBUF);
815
816 if (LIST_EMPTY(&pip->dispatch)) {
817 uint32_t bit = 1 << (dip->bitno + 2);
818 uint32_t r;
819
820 r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
821 r |= bit; /* set mask */
822 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
823 DPRINTF(("%s: disabled pin %d\n", __func__, dip->bitno + 1));
824
825 pip->enabled = false;
826 }
827 }
828
829 static void *
830 rmixl_pcix_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
831 int (*func)(void *), void *arg)
832 {
833 rmixl_pcix_softc_t *sc = v;
834 u_int bitno, irq;
835 rmixl_pcix_intr_t *pip;
836 rmixl_pcix_dispatch_t *dip;
837 int s;
838
839 if (pih == ~0) {
840 DPRINTF(("%s: bad pih=%#lx, implies PCI_INTERRUPT_PIN_NONE\n",
841 __func__, pih));
842 return NULL;
843 }
844
845 rmixl_pcix_decompose_pih(pih, &bitno, &irq);
846 DPRINTF(("%s: pin=%d irq=%d\n", __func__, bitno + 1, irq));
847
848 KASSERT(bitno < RMIXL_PCIX_NINTR);
849 pip = &sc->sc_intr[bitno];
850
851 s = splhigh();
852
853 /*
854 * all PCI-X device intrs get same ipl and sc
855 */
856 KASSERT(sc == rmixl_pcix_sc);
857 KASSERT(ipl == IPL_VM);
858
859 /*
860 * allocate and initialize a dispatch handle
861 */
862 dip = malloc(sizeof(*dip), M_DEVBUF, M_NOWAIT);
863 if (dip == NULL) {
864 printf("%s: cannot malloc dispatch handle\n", __func__);
865 goto out;
866 }
867
868 dip->bitno = bitno;
869 dip->irq = irq;
870 dip->func = func;
871 dip->arg = arg;
872 snprintf(dip->count_name, sizeof(dip->count_name),
873 "pin %d", bitno + 1);
874 evcnt_attach_dynamic(&dip->count, EVCNT_TYPE_INTR, NULL,
875 "rmixl_pcix", dip->count_name);
876
877 if (pip->enabled == false) {
878 uint32_t bit = 1 << (bitno + 2);
879 uint32_t r;
880
881 r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
882 r &= ~bit; /* clear mask */
883 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
884
885 pip->sc = sc;
886 pip->ipl = ipl;
887 pip->enabled = true;
888 DPRINTF(("%s: enabled pin %d\n", __func__, bitno + 1));
889 }
890
891 LIST_INSERT_HEAD(&pip->dispatch, dip, next);
892
893 out:
894 splx(s);
895 return dip;
896 }
897
898 static int
899 rmixl_pcix_intr(void *arg)
900 {
901 rmixl_pcix_softc_t *sc = arg;
902 int rv = 0;
903
904 uint32_t status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
905 DPRINTF(("%s: %#x\n", __func__, status));
906
907 if (status != 0) {
908 for (int i=0; i < RMIXL_PCIX_NINTR; i++) {
909 uint32_t bit = 1 << i;
910 if ((status & bit) != 0) {
911 rmixl_pcix_intr_t *pip = &sc->sc_intr[i];
912 rmixl_pcix_dispatch_t *dip;
913 LIST_FOREACH(dip, &pip->dispatch, next) {
914 (void)(*dip->func)(dip->arg);
915 dip->count.ev_count++;
916 rv = 1;
917 }
918 }
919 }
920 }
921 return rv;
922 }
923
924 static int
925 rmixl_pcix_error_intr(void *arg)
926 {
927 rmixl_pcix_softc_t *sc = arg;
928 uint32_t error_status;
929
930 error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
931
932 #ifdef DIAGNOSTIC
933 printf("%s: error status %#x\n", __func__, error_status);
934 #endif
935
936 #if DDB
937 Debugger();
938 #endif
939
940 /* XXX reset and recover? */
941
942 panic("%s: error %#x\n", device_xname(sc->sc_dev), error_status);
943 }
944
945 /*
946 * rmixl_physaddr_init_pcix:
947 * called from rmixl_physaddr_init to get region addrs & sizes
948 * from PCIX CFG, ECFG, IO, MEM BARs
949 */
950 void
951 rmixl_physaddr_init_pcix(struct extent *ext)
952 {
953 u_long base;
954 u_long size;
955 uint32_t r;
956
957 r = RMIXL_PCIXREG_READ(RMIXLR_SBC_PCIX_CFG_BAR);
958 if ((r & RMIXL_PCIX_CFG_BAR_ENB) != 0) {
959 base = (u_long)(RMIXL_PCIX_CFG_BAR_TO_BA((uint64_t)r)
960 / (1024 * 1024));
961 size = (u_long)RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
962 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
963 __LINE__, "CFG", r, base * 1024 * 1024, size));
964 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
965 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
966 "failed", __func__, ext, base, size, EX_NOWAIT);
967 }
968
969 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
970 if ((r & RMIXL_PCIX_MEM_BAR_ENB) != 0) {
971 base = (u_long)(RMIXL_PCIX_MEM_BAR_TO_BA((uint64_t)r)
972 / (1024 * 1024));
973 size = (u_long)(RMIXL_PCIX_MEM_BAR_TO_SIZE((uint64_t)r)
974 / (1024 * 1024));
975 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
976 __LINE__, "MEM", r, base * 1024 * 1024, size));
977 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
978 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
979 "failed", __func__, ext, base, size, EX_NOWAIT);
980 }
981
982 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
983 if ((r & RMIXL_PCIX_IO_BAR_ENB) != 0) {
984 base = (u_long)(RMIXL_PCIX_IO_BAR_TO_BA((uint64_t)r)
985 / (1024 * 1024));
986 size = (u_long)(RMIXL_PCIX_IO_BAR_TO_SIZE((uint64_t)r)
987 / (1024 * 1024));
988 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
989 __LINE__, "IO", r, base * 1024 * 1024, size));
990 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
991 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
992 "failed", __func__, ext, base, size, EX_NOWAIT);
993 }
994 }
995
996 #ifdef DDB
997 int rmixl_pcix_intr_chk(void);
998 int
999 rmixl_pcix_intr_chk(void)
1000 {
1001 uint32_t control, status, error_status;
1002
1003 control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
1004 status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
1005 error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
1006
1007 printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
1008
1009 control |= PCIX_INTR_CONTROL_DIA;
1010 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, control);
1011
1012 control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
1013 status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
1014 error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
1015
1016 printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
1017
1018 return 0;
1019 }
1020 #endif
1021