rmixl_pcix.c revision 1.1.2.4 1 /* $NetBSD: rmixl_pcix.c,v 1.1.2.4 2010/04/16 23:44:17 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.4 2010/04/16 23:44:17 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 aprint_error_dev(self, "HostBAR0 addr %#x, size %#x\n",
355 hbar_addr_lo, hbar_size_lo);
356 if ((hbar_size_lo & PCI_MAPREG_MEM_TYPE_64BIT) != 0)
357 aprint_error_dev(self, "HostBAR1 addr %#x, size %#x\n",
358 hbar_addr_hi, hbar_size_hi);
359 aprint_error_dev(self, "WARNING: firmware PCI-X setup error: "
360 "RAM %#"PRIx64"..%#"PRIx64" not accessible by Host BAR, "
361 "enabling DMA bounce buffers\n",
362 hbar_size, mem_cluster_maxaddr-1);
363
364 /* force use of bouce buffers for uncovered RAM */
365 if (hbar_size < ((uint64_t)1 << 32)) {
366 sc->sc_64bit_dmat = NULL;
367 sc->sc_32bit_dmat->_bounce_alloc_hi = hbar_size;
368 } else {
369 sc->sc_64bit_dmat->_bounce_alloc_hi = hbar_size;
370 }
371 }
372
373 /*
374 * check PCI-X interface byteswap setup
375 * ensure 'Match Byte Lane' is disabled
376 */
377 uint32_t mble, mba, mbs;
378 mble = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_XLR_MBLE);
379 mba = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_ADDR);
380 mbs = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_MATCH_BIT_SIZE);
381 DPRINTF(("%s: MBLE=%#x, MBA=%#x, MBS=%#x\n", __func__, mble, mba, mbs));
382 if ((mble & __BIT(40)) != 0)
383 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_XLR_MBLE, 0);
384
385 /*
386 * get PCI config space base addr from SBC PCIe CFG BAR
387 * initialize it if necessary
388 */
389 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_CFG_BAR);
390 DPRINTF(("%s: PCIX_CFG_BAR %#x\n", __func__, bar));
391 if ((bar & RMIXL_PCIX_CFG_BAR_ENB) == 0) {
392 u_long n = RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
393 RMIXL_PCIX_BAR_INIT(CFG, bar, n, n);
394 }
395 rcp->rc_pci_cfg_pbase = (bus_addr_t)RMIXL_PCIX_CFG_BAR_TO_BA(bar);
396 rcp->rc_pci_cfg_size = (bus_size_t)RMIXL_PCIX_CFG_SIZE;
397
398 /*
399 * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
400 * initialize it if necessary
401 */
402 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
403 DPRINTF(("%s: PCIX_MEM_BAR %#x\n", __func__, bar));
404 if ((bar & RMIXL_PCIX_MEM_BAR_ENB) == 0) {
405 u_long n = 256; /* 256 MB */
406 RMIXL_PCIX_BAR_INIT(MEM, bar, n, n);
407 }
408 rcp->rc_pci_mem_pbase = (bus_addr_t)RMIXL_PCIX_MEM_BAR_TO_BA(bar);
409 rcp->rc_pci_mem_size = (bus_size_t)RMIXL_PCIX_MEM_BAR_TO_SIZE(bar);
410
411 /*
412 * get PCI IO space base [addr, size] from SBC PCIe IO BAR
413 * initialize it if necessary
414 */
415 bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
416 DPRINTF(("%s: PCIX_IO_BAR %#x\n", __func__, bar));
417 if ((bar & RMIXL_PCIX_IO_BAR_ENB) == 0) {
418 u_long n = 32; /* 32 MB */
419 RMIXL_PCIX_BAR_INIT(IO, bar, n, n);
420 }
421 rcp->rc_pci_io_pbase = (bus_addr_t)RMIXL_PCIX_IO_BAR_TO_BA(bar);
422 rcp->rc_pci_io_size = (bus_size_t)RMIXL_PCIX_IO_BAR_TO_SIZE(bar);
423
424 /*
425 * initialize the PCI CFG bus space tag
426 */
427 rmixl_pci_cfg_bus_mem_init(&rcp->rc_pci_cfg_memt, rcp);
428 sc->sc_pci_cfg_memt = &rcp->rc_pci_cfg_memt;
429
430 /*
431 * initialize the PCI MEM and IO bus space tags
432 */
433 rmixl_pci_bus_mem_init(&rcp->rc_pci_memt, rcp);
434 rmixl_pci_bus_io_init(&rcp->rc_pci_iot, rcp);
435
436 /*
437 * initialize the extended configuration regs
438 */
439 rmixl_pcix_init_errors(sc);
440
441 /*
442 * initialize the PCI chipset tag
443 */
444 rmixl_pcix_init(sc);
445
446 /*
447 * attach the PCI bus
448 */
449 memset(&pba, 0, sizeof(pba));
450 pba.pba_memt = &rcp->rc_pci_memt;
451 pba.pba_iot = &rcp->rc_pci_iot;
452 pba.pba_dmat = sc->sc_32bit_dmat;
453 pba.pba_dmat64 = sc->sc_64bit_dmat;
454 pba.pba_pc = &sc->sc_pci_chipset;
455 pba.pba_bus = 0;
456 pba.pba_bridgetag = NULL;
457 pba.pba_intrswiz = 0;
458 pba.pba_intrtag = 0;
459 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
460 PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
461
462 (void) config_found_ia(self, "pcibus", &pba, pcibusprint);
463 }
464
465 /*
466 * rmixl_pcix_intcfg - init PCI-X interrupt control
467 */
468 static void
469 rmixl_pcix_intcfg(rmixl_pcix_softc_t *sc)
470 {
471 DPRINTF(("%s\n", __func__));
472
473 /* mask all interrupts until they are established */
474 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL,
475 PCIX_INTR_CONTROL_MASK_ALL);
476
477 /*
478 * read-to-clear any pre-existing interrupts
479 * XXX MSI bits in STATUS are also documented as write 1 to clear in PRM
480 */
481 (void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
482 (void)RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
483
484 /* initialize the dispatch handles */
485 for (int i=0; i < RMIXL_PCIX_NINTR; i++) {
486 rmixl_pcix_intr_t *ih = &sc->sc_intr[i];
487 LIST_INIT(&ih->dispatch);
488 ih->ih = NULL;
489 ih->intrpin = i;
490 ih->enabled = false;
491 }
492
493 sc->sc_ih = rmixl_intr_establish(16, sc->sc_tmsk,
494 IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
495 rmixl_pcix_intr, sc, false);
496 if (sc->sc_ih == NULL)
497 panic("%s: cannot establish irq %d", __func__, 16);
498
499 sc->sc_fatal_ih = rmixl_intr_establish(24, sc->sc_tmsk,
500 IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
501 rmixl_pcix_error_intr, sc, false);
502 if (sc->sc_fatal_ih == NULL)
503 panic("%s: cannot establish irq %d", __func__, 24);
504
505 sc->sc_intr_init_done = true;
506 }
507
508 static void
509 rmixl_pcix_errata(rmixl_pcix_softc_t *sc)
510 {
511 /* nothing */
512 }
513
514 static void
515 rmixl_pcix_init(rmixl_pcix_softc_t *sc)
516 {
517 pci_chipset_tag_t pc = &sc->sc_pci_chipset;
518 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
519 struct extent *ioext, *memext;
520 #endif
521
522 pc->pc_conf_v = (void *)sc;
523 pc->pc_attach_hook = rmixl_pcix_attach_hook;
524 pc->pc_bus_maxdevs = rmixl_pcix_bus_maxdevs;
525 pc->pc_make_tag = rmixl_pcix_make_tag;
526 pc->pc_decompose_tag = rmixl_pcix_decompose_tag;
527 pc->pc_conf_read = rmixl_pcix_conf_read;
528 pc->pc_conf_write = rmixl_pcix_conf_write;
529
530 pc->pc_intr_v = (void *)sc;
531 pc->pc_intr_map = rmixl_pcix_intr_map;
532 pc->pc_intr_string = rmixl_pcix_intr_string;
533 pc->pc_intr_evcnt = rmixl_pcix_intr_evcnt;
534 pc->pc_intr_establish = rmixl_pcix_intr_establish;
535 pc->pc_intr_disestablish = rmixl_pcix_intr_disestablish;
536 pc->pc_conf_interrupt = rmixl_conf_interrupt;
537
538 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
539 /*
540 * Configure the PCI bus.
541 */
542 struct rmixl_config *rcp = &rmixl_configuration;
543
544 aprint_normal_dev(sc->sc_dev, "%s: configuring PCI bus\n");
545
546 ioext = extent_create("pciio",
547 rcp->rc_pci_io_pbase,
548 rcp->rc_pci_io_pbase + rcp->rc_pci_io_size - 1,
549 M_DEVBUF, NULL, 0, EX_NOWAIT);
550
551 memext = extent_create("pcimem",
552 rcp->rc_pci_mem_pbase,
553 rcp->rc_pci_mem_pbase + rcp->rc_pci_mem_size - 1,
554 M_DEVBUF, NULL, 0, EX_NOWAIT);
555
556 pci_configure_bus(pc, ioext, memext, NULL, 0,
557 mips_cache_info.mci_dcache_align);
558
559 extent_destroy(ioext);
560 extent_destroy(memext);
561 #endif
562 }
563
564 static void
565 rmixl_pcix_init_errors(rmixl_pcix_softc_t *sc)
566 {
567 /* nothing */
568 }
569
570 void
571 rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
572 {
573 DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
574 __func__, v, bus, dev, ipin, swiz, iline));
575 }
576
577 void
578 rmixl_pcix_attach_hook(struct device *parent, struct device *self,
579 struct pcibus_attach_args *pba)
580 {
581 DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
582 __func__, pba->pba_bus, pba->pba_bridgetag,
583 pba->pba_pc->pc_conf_v));
584 }
585
586 int
587 rmixl_pcix_bus_maxdevs(void *v, int busno)
588 {
589 return (32); /* XXX depends on the family of XLS SoC */
590 }
591
592 /*
593 * XLS pci tag is a 40 bit address composed thusly:
594 * 39:25 (reserved)
595 * 24 Swap (0=little, 1=big endian)
596 * 23:16 Bus number
597 * 15:11 Device number
598 * 10:8 Function number
599 * 7:0 Register number
600 *
601 * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
602 */
603 pcitag_t
604 rmixl_pcix_make_tag(void *v, int bus, int dev, int fun)
605 {
606 return ((bus << 16) | (dev << 11) | (fun << 8));
607 }
608
609 void
610 rmixl_pcix_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
611 {
612 if (bp != NULL)
613 *bp = (tag >> 16) & 0xff;
614 if (dp != NULL)
615 *dp = (tag >> 11) & 0x1f;
616 if (fp != NULL)
617 *fp = (tag >> 8) & 0x7;
618 }
619
620 void
621 rmixl_pcix_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
622 vaddr_t va, u_long r)
623 {
624 int bus, dev, fun;
625
626 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
627 printf("%s: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
628 s, bus, dev, fun, offset, va, r);
629 }
630
631 static int
632 rmixl_pcix_conf_setup(rmixl_pcix_softc_t *sc,
633 pcitag_t tag, int *offp, bus_space_tag_t *bstp,
634 bus_space_handle_t *bshp)
635 {
636 struct rmixl_config *rcp = &rmixl_configuration;
637 bus_space_tag_t bst;
638 bus_space_handle_t bsh;
639 bus_size_t size;
640 pcitag_t mask;
641 bus_addr_t ba;
642 int err;
643 static bus_space_handle_t cfg_bsh;
644 static bus_addr_t cfg_oba = -1;
645
646 /*
647 * bus space depends on offset
648 */
649 if ((*offp >= 0) && (*offp < 0x100)) {
650 mask = __BITS(15,0);
651 bst = sc->sc_pci_cfg_memt;
652 ba = rcp->rc_pci_cfg_pbase;
653 ba += (tag & ~mask);
654 *offp += (tag & mask);
655 if (ba != cfg_oba) {
656 size = (bus_size_t)(mask + 1);
657 if (cfg_oba != -1)
658 bus_space_unmap(bst, cfg_bsh, size);
659 err = bus_space_map(bst, ba, size, 0, &cfg_bsh);
660 if (err != 0) {
661 #ifdef DEBUG
662 panic("%s: bus_space_map err %d, CFG space",
663 __func__, err); /* XXX */
664 #endif
665 return -1;
666 }
667 cfg_oba = ba;
668 }
669 bsh = cfg_bsh;
670 } else {
671 #ifdef DEBUG
672 panic("%s: offset %#x: unknown", __func__, *offp);
673 #endif
674 return -1;
675 }
676
677 *bstp = bst;
678 *bshp = bsh;
679
680 return 0;
681 }
682
683 pcireg_t
684 rmixl_pcix_conf_read(void *v, pcitag_t tag, int offset)
685 {
686 rmixl_pcix_softc_t *sc = v;
687 static bus_space_handle_t bsh;
688 bus_space_tag_t bst;
689 pcireg_t rv;
690 uint64_t cfg0;
691 u_int s;
692
693 PCI_CONF_LOCK(s);
694
695 if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
696 cfg0 = rmixl_cache_err_dis();
697 rv = bus_space_read_4(bst, bsh, (bus_size_t)offset);
698 if (rmixl_cache_err_check() != 0) {
699 #ifdef DIAGNOSTIC
700 int bus, dev, fun;
701
702 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
703 printf("%s: %d/%d/%d, offset %#x: bad address\n",
704 __func__, bus, dev, fun, offset);
705 #endif
706 rv = (pcireg_t) -1;
707 }
708 rmixl_cache_err_restore(cfg0);
709 } else {
710 rv = -1;
711 }
712
713 PCI_CONF_UNLOCK(s);
714 return rv;
715 }
716
717 void
718 rmixl_pcix_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
719 {
720 rmixl_pcix_softc_t *sc = v;
721 static bus_space_handle_t bsh;
722 bus_space_tag_t bst;
723 uint64_t cfg0;
724 u_int s;
725
726 PCI_CONF_LOCK(s);
727
728 if (rmixl_pcix_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
729 cfg0 = rmixl_cache_err_dis();
730 bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
731 if (rmixl_cache_err_check() != 0) {
732 #ifdef DIAGNOSTIC
733 int bus, dev, fun;
734
735 rmixl_pcix_decompose_tag(v, tag, &bus, &dev, &fun);
736 printf("%s: %d/%d/%d, offset %#x: bad address\n",
737 __func__, bus, dev, fun, offset);
738 #endif
739 }
740 rmixl_cache_err_restore(cfg0);
741 }
742
743 PCI_CONF_UNLOCK(s);
744 }
745
746 int
747 rmixl_pcix_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *pih)
748 {
749 const u_int irq = 16; /* PCIX index in IRT */
750
751 #ifdef DEBUG
752 DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
753 " pa_intrpin %d, pa_intrline %d, pa_rawintrpin %d\n",
754 __func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
755 pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
756 #endif
757
758 if (pa->pa_intrpin != PCI_INTERRUPT_PIN_NONE)
759 *pih = rmixl_pcix_make_pih(pa->pa_intrpin - 1, irq);
760 else
761 *pih = ~0;
762
763 return 0;
764 }
765
766 const char *
767 rmixl_pcix_intr_string(void *v, pci_intr_handle_t pih)
768 {
769 u_int bitno, irq;
770
771 rmixl_pcix_decompose_pih(pih, &bitno, &irq);
772
773 if (! cpu_rmixlr(mips_options.mips_cpu))
774 panic("%s: cpu %#x not supported\n",
775 __func__, mips_options.mips_cpu_id);
776
777 return rmixl_intr_string(irq);
778 }
779
780 const struct evcnt *
781 rmixl_pcix_intr_evcnt(void *v, pci_intr_handle_t pih)
782 {
783 return NULL;
784 }
785
786 static pci_intr_handle_t
787 rmixl_pcix_make_pih(u_int bitno, u_int irq)
788 {
789 pci_intr_handle_t pih;
790
791 KASSERT(bitno < 64);
792 KASSERT(irq < 32);
793
794 pih = (irq << 6);
795 pih |= bitno;
796
797 return pih;
798 }
799
800 static void
801 rmixl_pcix_decompose_pih(pci_intr_handle_t pih, u_int *bitno, u_int *irq)
802 {
803 *bitno = (u_int)(pih & 0x3f);
804 *irq = (u_int)(pih >> 6);
805
806 KASSERT(*bitno < 64);
807 KASSERT(*irq < 31);
808 }
809
810 static void
811 rmixl_pcix_intr_disestablish(void *v, void *ih)
812 {
813 rmixl_pcix_softc_t *sc = v;
814 rmixl_pcix_dispatch_t *dip = ih;
815 rmixl_pcix_intr_t *pip = &sc->sc_intr[dip->bitno];;
816
817 DPRINTF(("%s: pin=%d irq=%d\n",
818 __func__, dip->bitno + 1, dip->irq));
819 KASSERT(dip->bitno < RMIXL_PCIX_NINTR);
820
821 LIST_REMOVE(dip, next);
822 evcnt_detach(&dip->count);
823 free(dip, M_DEVBUF);
824
825 if (LIST_EMPTY(&pip->dispatch)) {
826 uint32_t bit = 1 << (dip->bitno + 2);
827 uint32_t r;
828
829 r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
830 r |= bit; /* set mask */
831 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
832 DPRINTF(("%s: disabled pin %d\n", __func__, dip->bitno + 1));
833
834 pip->enabled = false;
835 }
836 }
837
838 static void *
839 rmixl_pcix_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
840 int (*func)(void *), void *arg)
841 {
842 rmixl_pcix_softc_t *sc = v;
843 u_int bitno, irq;
844 rmixl_pcix_intr_t *pip;
845 rmixl_pcix_dispatch_t *dip;
846 int s;
847
848 if (pih == ~0) {
849 DPRINTF(("%s: bad pih=%#lx, implies PCI_INTERRUPT_PIN_NONE\n",
850 __func__, pih));
851 return NULL;
852 }
853
854 rmixl_pcix_decompose_pih(pih, &bitno, &irq);
855 DPRINTF(("%s: pin=%d irq=%d\n", __func__, bitno + 1, irq));
856
857 KASSERT(bitno < RMIXL_PCIX_NINTR);
858 pip = &sc->sc_intr[bitno];
859
860 s = splhigh();
861
862 /*
863 * all PCI-X device intrs get same ipl and sc
864 */
865 KASSERT(sc == rmixl_pcix_sc);
866 KASSERT(ipl == IPL_VM);
867
868 /*
869 * allocate and initialize a dispatch handle
870 */
871 dip = malloc(sizeof(*dip), M_DEVBUF, M_NOWAIT);
872 if (dip == NULL) {
873 printf("%s: cannot malloc dispatch handle\n", __func__);
874 goto out;
875 }
876
877 dip->bitno = bitno;
878 dip->irq = irq;
879 dip->func = func;
880 dip->arg = arg;
881 snprintf(dip->count_name, sizeof(dip->count_name),
882 "pin %d", bitno + 1);
883 evcnt_attach_dynamic(&dip->count, EVCNT_TYPE_INTR, NULL,
884 "rmixl_pcix", dip->count_name);
885
886 if (pip->enabled == false) {
887 uint32_t bit = 1 << (bitno + 2);
888 uint32_t r;
889
890 r = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
891 r &= ~bit; /* clear mask */
892 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, r);
893
894 pip->sc = sc;
895 pip->ipl = ipl;
896 pip->enabled = true;
897 DPRINTF(("%s: enabled pin %d\n", __func__, bitno + 1));
898 }
899
900 LIST_INSERT_HEAD(&pip->dispatch, dip, next);
901
902 out:
903 splx(s);
904 return dip;
905 }
906
907 static int
908 rmixl_pcix_intr(void *arg)
909 {
910 rmixl_pcix_softc_t *sc = arg;
911 int rv = 0;
912
913 uint32_t status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
914 DPRINTF(("%s: %#x\n", __func__, status));
915
916 if (status != 0) {
917 for (int i=0; i < RMIXL_PCIX_NINTR; i++) {
918 uint32_t bit = 1 << i;
919 if ((status & bit) != 0) {
920 rmixl_pcix_intr_t *pip = &sc->sc_intr[i];
921 rmixl_pcix_dispatch_t *dip;
922 LIST_FOREACH(dip, &pip->dispatch, next) {
923 (void)(*dip->func)(dip->arg);
924 dip->count.ev_count++;
925 rv = 1;
926 }
927 }
928 }
929 }
930 return rv;
931 }
932
933 static int
934 rmixl_pcix_error_intr(void *arg)
935 {
936 rmixl_pcix_softc_t *sc = arg;
937 uint32_t error_status;
938
939 error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
940
941 #ifdef DIAGNOSTIC
942 printf("%s: error status %#x\n", __func__, error_status);
943 #endif
944
945 #if DDB
946 Debugger();
947 #endif
948
949 /* XXX reset and recover? */
950
951 panic("%s: error %#x\n", device_xname(sc->sc_dev), error_status);
952 }
953
954 /*
955 * rmixl_physaddr_init_pcix:
956 * called from rmixl_physaddr_init to get region addrs & sizes
957 * from PCIX CFG, ECFG, IO, MEM BARs
958 */
959 void
960 rmixl_physaddr_init_pcix(struct extent *ext)
961 {
962 u_long base;
963 u_long size;
964 uint32_t r;
965
966 r = RMIXL_PCIXREG_READ(RMIXLR_SBC_PCIX_CFG_BAR);
967 if ((r & RMIXL_PCIX_CFG_BAR_ENB) != 0) {
968 base = (u_long)(RMIXL_PCIX_CFG_BAR_TO_BA((uint64_t)r)
969 / (1024 * 1024));
970 size = (u_long)RMIXL_PCIX_CFG_SIZE / (1024 * 1024);
971 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
972 __LINE__, "CFG", r, base * 1024 * 1024, size));
973 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
974 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
975 "failed", __func__, ext, base, size, EX_NOWAIT);
976 }
977
978 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_MEM_BAR);
979 if ((r & RMIXL_PCIX_MEM_BAR_ENB) != 0) {
980 base = (u_long)(RMIXL_PCIX_MEM_BAR_TO_BA((uint64_t)r)
981 / (1024 * 1024));
982 size = (u_long)(RMIXL_PCIX_MEM_BAR_TO_SIZE((uint64_t)r)
983 / (1024 * 1024));
984 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
985 __LINE__, "MEM", r, base * 1024 * 1024, size));
986 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
987 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
988 "failed", __func__, ext, base, size, EX_NOWAIT);
989 }
990
991 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXLR_SBC_PCIX_IO_BAR);
992 if ((r & RMIXL_PCIX_IO_BAR_ENB) != 0) {
993 base = (u_long)(RMIXL_PCIX_IO_BAR_TO_BA((uint64_t)r)
994 / (1024 * 1024));
995 size = (u_long)(RMIXL_PCIX_IO_BAR_TO_SIZE((uint64_t)r)
996 / (1024 * 1024));
997 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
998 __LINE__, "IO", r, base * 1024 * 1024, size));
999 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
1000 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
1001 "failed", __func__, ext, base, size, EX_NOWAIT);
1002 }
1003 }
1004
1005 #ifdef DDB
1006 int rmixl_pcix_intr_chk(void);
1007 int
1008 rmixl_pcix_intr_chk(void)
1009 {
1010 uint32_t control, status, error_status;
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 control |= PCIX_INTR_CONTROL_DIA;
1019 RMIXL_PCIXREG_WRITE(RMIXL_PCIX_ECFG_INTR_CONTROL, control);
1020
1021 control = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_CONTROL);
1022 status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_STATUS);
1023 error_status = RMIXL_PCIXREG_READ(RMIXL_PCIX_ECFG_INTR_ERR_STATUS);
1024
1025 printf("%s: %#x, %#x, %#x\n", __func__, control, status, error_status);
1026
1027 return 0;
1028 }
1029 #endif
1030