rmixl_pcie.c revision 1.2 1 1.2 matt /* $NetBSD: rmixl_pcie.c,v 1.2 2009/12/14 00:46:07 matt Exp $ */
2 1.2 matt
3 1.2 matt /*
4 1.2 matt * Copyright (c) 2001 Wasabi Systems, Inc.
5 1.2 matt * All rights reserved.
6 1.2 matt *
7 1.2 matt * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.2 matt *
9 1.2 matt * Redistribution and use in source and binary forms, with or without
10 1.2 matt * modification, are permitted provided that the following conditions
11 1.2 matt * are met:
12 1.2 matt * 1. Redistributions of source code must retain the above copyright
13 1.2 matt * notice, this list of conditions and the following disclaimer.
14 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
15 1.2 matt * notice, this list of conditions and the following disclaimer in the
16 1.2 matt * documentation and/or other materials provided with the distribution.
17 1.2 matt * 3. All advertising materials mentioning features or use of this software
18 1.2 matt * must display the following acknowledgement:
19 1.2 matt * This product includes software developed for the NetBSD Project by
20 1.2 matt * Wasabi Systems, Inc.
21 1.2 matt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2 matt * or promote products derived from this software without specific prior
23 1.2 matt * written permission.
24 1.2 matt *
25 1.2 matt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2 matt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
36 1.2 matt */
37 1.2 matt
38 1.2 matt /*
39 1.2 matt * PCI configuration support for RMI XLS SoC
40 1.2 matt */
41 1.2 matt
42 1.2 matt #include <sys/cdefs.h>
43 1.2 matt __KERNEL_RCSID(0, "$NetBSD: rmixl_pcie.c,v 1.2 2009/12/14 00:46:07 matt Exp $");
44 1.2 matt
45 1.2 matt #include "opt_pci.h"
46 1.2 matt #include "pci.h"
47 1.2 matt
48 1.2 matt #include <sys/cdefs.h>
49 1.2 matt
50 1.2 matt #include <sys/param.h>
51 1.2 matt #include <sys/systm.h>
52 1.2 matt #include <sys/device.h>
53 1.2 matt #include <sys/extent.h>
54 1.2 matt #include <sys/malloc.h>
55 1.2 matt
56 1.2 matt #include <uvm/uvm_extern.h>
57 1.2 matt
58 1.2 matt #include <machine/bus.h>
59 1.2 matt #include <machine/intr.h>
60 1.2 matt
61 1.2 matt #include <mips/rmi/rmixlreg.h>
62 1.2 matt #include <mips/rmi/rmixlvar.h>
63 1.2 matt #include <mips/rmi/rmixl_pcievar.h>
64 1.2 matt
65 1.2 matt #include <mips/rmi/rmixl_obiovar.h>
66 1.2 matt
67 1.2 matt #include <dev/pci/pcivar.h>
68 1.2 matt #include <dev/pci/pcidevs.h>
69 1.2 matt #include <dev/pci/pciconf.h>
70 1.2 matt
71 1.2 matt #ifdef PCI_NETBSD_CONFIGURE
72 1.2 matt #include <mips/cache.h>
73 1.2 matt #endif
74 1.2 matt
75 1.2 matt #include <machine/pci_machdep.h>
76 1.2 matt
77 1.2 matt #ifdef PCI_DEBUG
78 1.2 matt int rmixl_pcie_debug = PCI_DEBUG;
79 1.2 matt # define DPRINTF(x) do { if (rmixl_pcie_debug) printf x ; } while (0)
80 1.2 matt #else
81 1.2 matt # define DPRINTF(x)
82 1.2 matt #endif
83 1.2 matt
84 1.2 matt #ifndef DDB
85 1.2 matt # define STATIC static
86 1.2 matt #else
87 1.2 matt # define STATIC
88 1.2 matt #endif
89 1.2 matt
90 1.2 matt
91 1.2 matt /*
92 1.2 matt * XLS PCIe Extended Configuration Registers
93 1.2 matt */
94 1.2 matt #define RMIXL_PCIE_ECFG_UESR 0x104 /* Uncorrectable Error Status Reg */
95 1.2 matt #define RMIXL_PCIE_ECFG_UEMR 0x108 /* Uncorrectable Error Mask Reg */
96 1.2 matt #define RMIXL_PCIE_ECFG_UEVR 0x10c /* Uncorrectable Error seVerity Reg */
97 1.2 matt #define PCIE_ECFG_UEVR_DFLT \
98 1.2 matt (__BITS(18,17) | __BIT(31) | __BITS(5,4) | __BIT(0))
99 1.2 matt #define PCIE_ECFG_UExR_RESV (__BITS(31,21) | __BITS(11,6) | __BITS(3,1))
100 1.2 matt #define RMIXL_PCIE_ECFG_CESR 0x110 /* Correctable Error Status Reg */
101 1.2 matt #define RMIXL_PCIE_ECFG_CEMR 0x114 /* Correctable Error Mask Reg */
102 1.2 matt #define PCIE_ECFG_CExR_RESV (__BITS(31,14) | __BITS(11,9) | __BITS(5,1))
103 1.2 matt #define RMIXL_PCIE_ECFG_ACCR 0x118 /* Adv. Capabilities Control Reg */
104 1.2 matt #define RMIXL_PCIE_ECFG_HLRn(n) (0x11c + ((n) * 4)) /* Header Log Regs */
105 1.2 matt #define RMIXL_PCIE_ECFG_RECR 0x12c /* Root Error Command Reg */
106 1.2 matt #define PCIE_ECFG_RECR_RESV __BITS(31,3)
107 1.2 matt #define RMIXL_PCIE_ECFG_RESR 0x130 /* Root Error Status Reg */
108 1.2 matt #define PCIE_ECFG_RESR_RESV __BITS(26,7)
109 1.2 matt #define RMIXL_PCIE_ECFG_ESI 0x134 /* Error Source Identification Reg */
110 1.2 matt #define RMIXL_PCIE_ECFG_DSNCR 0x140 /* Dev Serial Number Capability Regs */
111 1.2 matt
112 1.2 matt static const struct {
113 1.2 matt u_int offset;
114 1.2 matt u_int32_t rw1c;
115 1.2 matt } pcie_ecfg_errs_tab[] = {
116 1.2 matt { RMIXL_PCIE_ECFG_UESR, (__BITS(20,12) | __BIT(4)) },
117 1.2 matt { RMIXL_PCIE_ECFG_CESR, (__BITS(20,12) | __BIT(4)) },
118 1.2 matt { RMIXL_PCIE_ECFG_HLRn(0), 0 },
119 1.2 matt { RMIXL_PCIE_ECFG_HLRn(1), 0 },
120 1.2 matt { RMIXL_PCIE_ECFG_HLRn(2), 0 },
121 1.2 matt { RMIXL_PCIE_ECFG_HLRn(3), 0 },
122 1.2 matt { RMIXL_PCIE_ECFG_RESR, __BITS(6,0) },
123 1.2 matt { RMIXL_PCIE_ECFG_ESI, 0 },
124 1.2 matt };
125 1.2 matt #define PCIE_ECFG_ERRS_OFFTAB_NENTRIES \
126 1.2 matt (sizeof(pcie_ecfg_errs_tab)/sizeof(pcie_ecfg_errs_tab[0]))
127 1.2 matt
128 1.2 matt static int rmixl_pcie_match(device_t, cfdata_t, void *);
129 1.2 matt static void rmixl_pcie_attach(device_t, device_t, void *);
130 1.2 matt static void rmixl_pcie_init(struct rmixl_pcie_softc *);
131 1.2 matt static void rmixl_pcie_init_ecfg(struct rmixl_pcie_softc *);
132 1.2 matt static void rmixl_pcie_attach_hook(struct device *, struct device *,
133 1.2 matt struct pcibus_attach_args *);
134 1.2 matt static void rmixl_pcie_lnkcfg_4xx(rmixl_pcie_lnktab_t *, uint32_t);
135 1.2 matt static void rmixl_pcie_lnkcfg_408Lite(rmixl_pcie_lnktab_t *, uint32_t);
136 1.2 matt static void rmixl_pcie_lnkcfg_2xx(rmixl_pcie_lnktab_t *, uint32_t);
137 1.2 matt static void rmixl_pcie_lnkcfg_1xx(rmixl_pcie_lnktab_t *, uint32_t);
138 1.2 matt static void rmixl_pcie_lnkcfg(struct rmixl_pcie_softc *);
139 1.2 matt static void rmixl_pcie_errata(struct rmixl_pcie_softc *);
140 1.2 matt static void rmixl_conf_interrupt(void *, int, int, int, int, int *);
141 1.2 matt static int rmixl_pcie_bus_maxdevs(void *, int);
142 1.2 matt static pcitag_t rmixl_tag_to_ecfg(pcitag_t);
143 1.2 matt static pcitag_t rmixl_pcie_make_tag(void *, int, int, int);
144 1.2 matt static void rmixl_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
145 1.2 matt void rmixl_pcie_tag_print(const char *restrict, void *, pcitag_t, int, vaddr_t, u_long);
146 1.2 matt static int rmixl_pcie_conf_setup(struct rmixl_pcie_softc *,
147 1.2 matt pcitag_t, int *, bus_space_tag_t *,
148 1.2 matt bus_space_handle_t *);
149 1.2 matt static pcireg_t rmixl_pcie_conf_read(void *, pcitag_t, int);
150 1.2 matt static void rmixl_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
151 1.2 matt
152 1.2 matt static int rmixl_pcie_intr_map(struct pci_attach_args *,
153 1.2 matt pci_intr_handle_t *);
154 1.2 matt static const char *
155 1.2 matt rmixl_pcie_intr_string(void *, pci_intr_handle_t);
156 1.2 matt static const struct evcnt *
157 1.2 matt rmixl_pcie_intr_evcnt(void *, pci_intr_handle_t);
158 1.2 matt static void *rmixl_pcie_intr_establish(void *, pci_intr_handle_t,
159 1.2 matt int, int (*)(void *), void *);
160 1.2 matt static void rmixl_pcie_intr_disestablish(void *, void *);
161 1.2 matt #if defined(DEBUG) || defined(DDB)
162 1.2 matt int rmixl_pcie_error_check(void);
163 1.2 matt #endif
164 1.2 matt static int _rmixl_pcie_error_check(void *);
165 1.2 matt static int rmixl_pcie_error_intr(void *);
166 1.2 matt
167 1.2 matt /*
168 1.2 matt * XXX use locks
169 1.2 matt */
170 1.2 matt #define PCI_CONF_LOCK(s) (s) = splhigh()
171 1.2 matt #define PCI_CONF_UNLOCK(s) splx((s))
172 1.2 matt
173 1.2 matt
174 1.2 matt #define RMIXL_PCIE_CONCAT3(a,b,c) a ## b ## c
175 1.2 matt #define RMIXL_PCIE_BAR_INIT(reg, bar, size, align) { \
176 1.2 matt struct extent *ext = rmixl_configuration.rc_phys_ex; \
177 1.2 matt u_long region_start; \
178 1.2 matt uint64_t ba; \
179 1.2 matt int err; \
180 1.2 matt \
181 1.2 matt err = extent_alloc(ext, (size), (align), 0UL, EX_NOWAIT, \
182 1.2 matt ®ion_start); \
183 1.2 matt if (err != 0) \
184 1.2 matt panic("%s: extent_alloc(%p, %#lx, %#lx, %#lx, %#x, %p)",\
185 1.2 matt __func__, ext, size, align, 0UL, EX_NOWAIT, \
186 1.2 matt ®ion_start); \
187 1.2 matt ba = (uint64_t)region_start; \
188 1.2 matt ba *= (1024 * 1024); \
189 1.2 matt bar = RMIXL_PCIE_CONCAT3(RMIXL_PCIE_,reg,_BAR)(ba, 1); \
190 1.2 matt DPRINTF(("PCIE %s BAR was not enabled by firmware\n" \
191 1.2 matt "enabling %s at phys %#" PRIxBUSADDR ", size %lu MB\n", \
192 1.2 matt __STRING(reg), __STRING(reg), ba, size)); \
193 1.2 matt RMIXL_IOREG_WRITE(RMIXL_IO_DEV_BRIDGE + \
194 1.2 matt RMIXL_PCIE_CONCAT3(RMIXL_SBC_PCIE_,reg,_BAR), bar); \
195 1.2 matt bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + \
196 1.2 matt RMIXL_PCIE_CONCAT3(RMIXL_SBC_PCIE_,reg,_BAR)); \
197 1.2 matt DPRINTF(("%s: %s BAR %#x\n", __func__, __STRING(reg), bar)); \
198 1.2 matt }
199 1.2 matt
200 1.2 matt
201 1.2 matt #if defined(DEBUG) || defined(DDB)
202 1.2 matt static void *rmixl_pcie_v;
203 1.2 matt #endif
204 1.2 matt
205 1.2 matt CFATTACH_DECL_NEW(rmixl_pcie, sizeof(struct rmixl_pcie_softc),
206 1.2 matt rmixl_pcie_match, rmixl_pcie_attach, NULL, NULL);
207 1.2 matt
208 1.2 matt static int rmixl_pcie_found;
209 1.2 matt
210 1.2 matt /*
211 1.2 matt * rmixl_cache_err_dis:
212 1.2 matt * - disable Cache, Data ECC, Snoop Tag Parity, Tag Parity errors
213 1.2 matt * - clear the cache error log
214 1.2 matt * - return previous value from RMIXL_PCR_L1D_CONFIG0
215 1.2 matt */
216 1.2 matt static inline uint64_t
217 1.2 matt rmixl_cache_err_dis(void)
218 1.2 matt {
219 1.2 matt uint64_t r;
220 1.2 matt
221 1.2 matt r = rmixl_mfcr(RMIXL_PCR_L1D_CONFIG0);
222 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CONFIG0, r & ~0x2e);
223 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CACHE_ERROR_LOG, 0);
224 1.2 matt return r;
225 1.2 matt }
226 1.2 matt
227 1.2 matt /*
228 1.2 matt * rmixl_cache_err_restore:
229 1.2 matt * - clear the cache error log, cache error overflow log,
230 1.2 matt * and cache interrupt registers
231 1.2 matt * - restore previous value to RMIXL_PCR_L1D_CONFIG0
232 1.2 matt */
233 1.2 matt static inline void
234 1.2 matt rmixl_cache_err_restore(uint64_t r)
235 1.2 matt {
236 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CACHE_ERROR_LOG, 0);
237 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CACHE_ERROR_OVF_LO, 0);
238 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CACHE_INTERRUPT, 0);
239 1.2 matt rmixl_mtcr(RMIXL_PCR_L1D_CONFIG0, r);
240 1.2 matt }
241 1.2 matt
242 1.2 matt static inline uint64_t
243 1.2 matt rmixl_cache_err_check(void)
244 1.2 matt {
245 1.2 matt return rmixl_mfcr(RMIXL_PCR_L1D_CACHE_ERROR_LOG);
246 1.2 matt }
247 1.2 matt
248 1.2 matt static int
249 1.2 matt rmixl_pcie_match(device_t parent, cfdata_t cf, void *aux)
250 1.2 matt {
251 1.2 matt uint32_t r;
252 1.2 matt
253 1.2 matt /* XXX
254 1.2 matt * for now there is only one PCIe Interface on chip
255 1.2 matt * this could change with furture RMI XL family designs
256 1.2 matt */
257 1.2 matt if (rmixl_pcie_found)
258 1.2 matt return 0;
259 1.2 matt
260 1.2 matt /* read GPIO Reset Configuration register */
261 1.2 matt /* XXX FIXME define the offset */
262 1.2 matt r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
263 1.2 matt r >>= 26;
264 1.2 matt r &= 3;
265 1.2 matt if (r != 0)
266 1.2 matt return 0; /* strapped for SRIO */
267 1.2 matt
268 1.2 matt return 1;
269 1.2 matt }
270 1.2 matt
271 1.2 matt static void
272 1.2 matt rmixl_pcie_attach(device_t parent, device_t self, void *aux)
273 1.2 matt {
274 1.2 matt struct rmixl_pcie_softc *sc = device_private(self);
275 1.2 matt struct obio_attach_args *obio = aux;
276 1.2 matt struct rmixl_config *rcp = &rmixl_configuration;
277 1.2 matt struct pcibus_attach_args pba;
278 1.2 matt uint32_t bar;
279 1.2 matt
280 1.2 matt rmixl_pcie_found = 1;
281 1.2 matt sc->sc_dev = self;
282 1.2 matt
283 1.2 matt aprint_normal(" RMI XLS PCIe Interface\n");
284 1.2 matt
285 1.2 matt rmixl_pcie_lnkcfg(sc);
286 1.2 matt
287 1.2 matt rmixl_pcie_errata(sc);
288 1.2 matt
289 1.2 matt sc->sc_29bit_dmat = obio->obio_29bit_dmat;
290 1.2 matt sc->sc_32bit_dmat = obio->obio_32bit_dmat;
291 1.2 matt sc->sc_64bit_dmat = obio->obio_64bit_dmat;
292 1.2 matt
293 1.2 matt /*
294 1.2 matt * get PCI config space base addr from SBC PCIe CFG BAR
295 1.2 matt * initialize it if necessary
296 1.2 matt */
297 1.2 matt bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXL_SBC_PCIE_CFG_BAR);
298 1.2 matt DPRINTF(("%s: PCIE_CFG_BAR %#x\n", __func__, bar));
299 1.2 matt if ((bar & RMIXL_PCIE_CFG_BAR_ENB) == 0) {
300 1.2 matt u_long n = RMIXL_PCIE_CFG_SIZE / (1024 * 1024);
301 1.2 matt RMIXL_PCIE_BAR_INIT(CFG, bar, n, n);
302 1.2 matt }
303 1.2 matt rcp->rc_pcie_cfg_pbase = (bus_addr_t)RMIXL_PCIE_CFG_BAR_TO_BA(bar);
304 1.2 matt rcp->rc_pcie_cfg_size = (bus_size_t)RMIXL_PCIE_CFG_SIZE;
305 1.2 matt
306 1.2 matt /*
307 1.2 matt * get PCIE Extended config space base addr from SBC PCIe ECFG BAR
308 1.2 matt * initialize it if necessary
309 1.2 matt */
310 1.2 matt bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXL_SBC_PCIE_ECFG_BAR);
311 1.2 matt DPRINTF(("%s: PCIE_ECFG_BAR %#x\n", __func__, bar));
312 1.2 matt if ((bar & RMIXL_PCIE_ECFG_BAR_ENB) == 0) {
313 1.2 matt u_long n = RMIXL_PCIE_ECFG_SIZE / (1024 * 1024);
314 1.2 matt RMIXL_PCIE_BAR_INIT(ECFG, bar, n, n);
315 1.2 matt }
316 1.2 matt rcp->rc_pcie_ecfg_pbase = (bus_addr_t)RMIXL_PCIE_ECFG_BAR_TO_BA(bar);
317 1.2 matt rcp->rc_pcie_ecfg_size = (bus_size_t)RMIXL_PCIE_ECFG_SIZE;
318 1.2 matt
319 1.2 matt /*
320 1.2 matt * get PCI MEM space base [addr, size] from SBC PCIe MEM BAR
321 1.2 matt * initialize it if necessary
322 1.2 matt */
323 1.2 matt bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXL_SBC_PCIE_MEM_BAR);
324 1.2 matt DPRINTF(("%s: PCIE_MEM_BAR %#x\n", __func__, bar));
325 1.2 matt if ((bar & RMIXL_PCIE_MEM_BAR_ENB) == 0) {
326 1.2 matt u_long n = 256; /* 256 MB */
327 1.2 matt RMIXL_PCIE_BAR_INIT(MEM, bar, n, n);
328 1.2 matt }
329 1.2 matt rcp->rc_pci_mem_pbase = (bus_addr_t)RMIXL_PCIE_MEM_BAR_TO_BA(bar);
330 1.2 matt rcp->rc_pci_mem_size = (bus_size_t)RMIXL_PCIE_MEM_BAR_TO_SIZE(bar);
331 1.2 matt
332 1.2 matt /*
333 1.2 matt * get PCI IO space base [addr, size] from SBC PCIe IO BAR
334 1.2 matt * initialize it if necessary
335 1.2 matt */
336 1.2 matt bar = RMIXL_IOREG_READ(RMIXL_IO_DEV_BRIDGE + RMIXL_SBC_PCIE_IO_BAR);
337 1.2 matt DPRINTF(("%s: PCIE_IO_BAR %#x\n", __func__, bar));
338 1.2 matt if ((bar & RMIXL_PCIE_IO_BAR_ENB) == 0) {
339 1.2 matt u_long n = 32; /* 32 MB */
340 1.2 matt RMIXL_PCIE_BAR_INIT(IO, bar, n, n);
341 1.2 matt }
342 1.2 matt rcp->rc_pci_io_pbase = (bus_addr_t)RMIXL_PCIE_IO_BAR_TO_BA(bar);
343 1.2 matt rcp->rc_pci_io_size = (bus_size_t)RMIXL_PCIE_IO_BAR_TO_SIZE(bar);
344 1.2 matt
345 1.2 matt /*
346 1.2 matt * initialize the PCI CFG, ECFG bus space tags
347 1.2 matt */
348 1.2 matt rmixl_pcie_cfg_bus_mem_init(&rcp->rc_pcie_cfg_memt, rcp);
349 1.2 matt sc->sc_pcie_cfg_memt = &rcp->rc_pcie_cfg_memt;
350 1.2 matt
351 1.2 matt rmixl_pcie_ecfg_bus_mem_init(&rcp->rc_pcie_ecfg_memt, rcp);
352 1.2 matt sc->sc_pcie_ecfg_memt = &rcp->rc_pcie_ecfg_memt;
353 1.2 matt
354 1.2 matt /*
355 1.2 matt * initialize the PCI MEM and IO bus space tags
356 1.2 matt */
357 1.2 matt rmixl_pcie_bus_mem_init(&rcp->rc_pci_memt, rcp);
358 1.2 matt rmixl_pcie_bus_io_init(&rcp->rc_pci_iot, rcp);
359 1.2 matt
360 1.2 matt /*
361 1.2 matt * initialize the extended configuration regs
362 1.2 matt */
363 1.2 matt rmixl_pcie_init_ecfg(sc);
364 1.2 matt
365 1.2 matt /*
366 1.2 matt * initialize the PCI chipset tag
367 1.2 matt */
368 1.2 matt rmixl_pcie_init(sc);
369 1.2 matt
370 1.2 matt /*
371 1.2 matt * attach the PCI bus
372 1.2 matt */
373 1.2 matt memset(&pba, 0, sizeof(pba));
374 1.2 matt pba.pba_memt = &rcp->rc_pci_memt;
375 1.2 matt pba.pba_iot = &rcp->rc_pci_iot;
376 1.2 matt pba.pba_dmat = sc->sc_29bit_dmat; /* XXX */
377 1.2 matt #ifdef NOTYET
378 1.2 matt pba.pba_dmat64 = NULL;
379 1.2 matt #endif
380 1.2 matt pba.pba_pc = &sc->sc_pci_chipset;
381 1.2 matt pba.pba_bus = 0;
382 1.2 matt pba.pba_bridgetag = NULL;
383 1.2 matt pba.pba_intrswiz = 0;
384 1.2 matt pba.pba_intrtag = 0;
385 1.2 matt pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
386 1.2 matt PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
387 1.2 matt
388 1.2 matt (void) config_found_ia(self, "pcibus", &pba, pcibusprint);
389 1.2 matt }
390 1.2 matt
391 1.2 matt /*
392 1.2 matt * rmixl_pcie_lnkcfg_4xx - link configs for XLS4xx and XLS6xx
393 1.2 matt * use IO_AD[11] and IO_AD[10], observable in
394 1.2 matt * Bits[21:20] of the GPIO Reset Configuration register
395 1.2 matt */
396 1.2 matt static void
397 1.2 matt rmixl_pcie_lnkcfg_4xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
398 1.2 matt {
399 1.2 matt u_int index;
400 1.2 matt static const rmixl_pcie_lnkcfg_t lnktab_4xx[4][4] = {
401 1.2 matt {{ LCFG_EP, 4}, {LCFG_NO, 0}, {LCFG_NO, 0}, {LCFG_NO, 0}},
402 1.2 matt {{ LCFG_RC, 4}, {LCFG_NO, 0}, {LCFG_NO, 0}, {LCFG_NO, 0}},
403 1.2 matt {{ LCFG_EP, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
404 1.2 matt {{ LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
405 1.2 matt };
406 1.2 matt static const char *lnkstr_4xx[4] = {
407 1.2 matt "EP 1x4",
408 1.2 matt "RC 1x4",
409 1.2 matt "EP 1x1, RC 4x1",
410 1.2 matt "RC 4x1"
411 1.2 matt };
412 1.2 matt index = (grcr >> 20) & 3;
413 1.2 matt ltp->ncfgs = 4;
414 1.2 matt ltp->cfg = lnktab_4xx[index];
415 1.2 matt ltp->str = lnkstr_4xx[index];
416 1.2 matt }
417 1.2 matt
418 1.2 matt /*
419 1.2 matt * rmixl_pcie_lnkcfg_408Lite - link configs for XLS408Lite and XLS04A
420 1.2 matt * use IO_AD[11] and IO_AD[10], observable in
421 1.2 matt * Bits[21:20] of the GPIO Reset Configuration register
422 1.2 matt */
423 1.2 matt static void
424 1.2 matt rmixl_pcie_lnkcfg_408Lite(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
425 1.2 matt {
426 1.2 matt u_int index;
427 1.2 matt static const rmixl_pcie_lnkcfg_t lnktab_408Lite[4][2] = {
428 1.2 matt {{ LCFG_EP, 4}, {LCFG_NO, 0}},
429 1.2 matt {{ LCFG_RC, 4}, {LCFG_NO, 0}},
430 1.2 matt {{ LCFG_EP, 1}, {LCFG_RC, 1}},
431 1.2 matt {{ LCFG_RC, 1}, {LCFG_RC, 1}},
432 1.2 matt };
433 1.2 matt static const char *lnkstr_408Lite[4] = {
434 1.2 matt "EP 1x4",
435 1.2 matt "RC 1x4",
436 1.2 matt "EP 1x1, RC 1x1",
437 1.2 matt "RC 2x1"
438 1.2 matt };
439 1.2 matt
440 1.2 matt index = (grcr >> 20) & 3;
441 1.2 matt ltp->ncfgs = 2;
442 1.2 matt ltp->cfg = lnktab_408Lite[index];
443 1.2 matt ltp->str = lnkstr_408Lite[index];
444 1.2 matt }
445 1.2 matt
446 1.2 matt /*
447 1.2 matt * rmixl_pcie_lnkcfg_2xx - link configs for XLS2xx
448 1.2 matt * use IO_AD[10], observable in Bit[20] of the
449 1.2 matt * GPIO Reset Configuration register
450 1.2 matt */
451 1.2 matt static void
452 1.2 matt rmixl_pcie_lnkcfg_2xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
453 1.2 matt {
454 1.2 matt u_int index;
455 1.2 matt static const rmixl_pcie_lnkcfg_t lnktab_2xx[2][4] = {
456 1.2 matt {{ LCFG_EP, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}},
457 1.2 matt {{ LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}, {LCFG_RC, 1}}
458 1.2 matt };
459 1.2 matt static const char *lnkstr_2xx[2] = {
460 1.2 matt "EP 1x1, RC 3x1",
461 1.2 matt "RC 4x1",
462 1.2 matt };
463 1.2 matt
464 1.2 matt index = (grcr >> 20) & 1;
465 1.2 matt ltp->ncfgs = 4;
466 1.2 matt ltp->cfg = lnktab_2xx[index];
467 1.2 matt ltp->str = lnkstr_2xx[index];
468 1.2 matt }
469 1.2 matt
470 1.2 matt /*
471 1.2 matt * rmixl_pcie_lnkcfg_1xx - link configs for XLS1xx
472 1.2 matt * use IO_AD[10], observable in Bit[20] of the
473 1.2 matt * GPIO Reset Configuration register
474 1.2 matt */
475 1.2 matt static void
476 1.2 matt rmixl_pcie_lnkcfg_1xx(rmixl_pcie_lnktab_t *ltp, uint32_t grcr)
477 1.2 matt {
478 1.2 matt u_int index;
479 1.2 matt static const rmixl_pcie_lnkcfg_t lnktab_1xx[2][2] = {
480 1.2 matt {{ LCFG_EP, 1}, {LCFG_RC, 1}},
481 1.2 matt {{ LCFG_RC, 1}, {LCFG_RC, 1}}
482 1.2 matt };
483 1.2 matt static const char *lnkstr_1xx[2] = {
484 1.2 matt "EP 1x1, RC 1x1",
485 1.2 matt "RC 2x1",
486 1.2 matt };
487 1.2 matt
488 1.2 matt index = (grcr >> 20) & 1;
489 1.2 matt ltp->ncfgs = 2;
490 1.2 matt ltp->cfg = lnktab_1xx[index];
491 1.2 matt ltp->str = lnkstr_1xx[index];
492 1.2 matt }
493 1.2 matt
494 1.2 matt /*
495 1.2 matt * rmixl_pcie_lnkcfg - determine PCI Express Link Configuration
496 1.2 matt */
497 1.2 matt static void
498 1.2 matt rmixl_pcie_lnkcfg(struct rmixl_pcie_softc *sc)
499 1.2 matt {
500 1.2 matt uint32_t r;
501 1.2 matt
502 1.2 matt /* read GPIO Reset Configuration register */
503 1.2 matt r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET_CFG);
504 1.2 matt DPRINTF(("%s: GPIO RCR %#x\n", __func__, r));
505 1.2 matt
506 1.2 matt switch (MIPS_PRID_IMPL(cpu_id)) {
507 1.2 matt case MIPS_XLS104:
508 1.2 matt case MIPS_XLS108:
509 1.2 matt rmixl_pcie_lnkcfg_1xx(&sc->sc_pcie_lnktab, r);
510 1.2 matt break;
511 1.2 matt case MIPS_XLS204:
512 1.2 matt case MIPS_XLS208:
513 1.2 matt rmixl_pcie_lnkcfg_2xx(&sc->sc_pcie_lnktab, r);
514 1.2 matt break;
515 1.2 matt case MIPS_XLS404LITE:
516 1.2 matt case MIPS_XLS408LITE:
517 1.2 matt rmixl_pcie_lnkcfg_408Lite(&sc->sc_pcie_lnktab, r);
518 1.2 matt break;
519 1.2 matt case MIPS_XLS404:
520 1.2 matt case MIPS_XLS408:
521 1.2 matt case MIPS_XLS416:
522 1.2 matt case MIPS_XLS608:
523 1.2 matt case MIPS_XLS616:
524 1.2 matt /* 6xx uses same table as 4xx */
525 1.2 matt rmixl_pcie_lnkcfg_4xx(&sc->sc_pcie_lnktab, r);
526 1.2 matt break;
527 1.2 matt default:
528 1.2 matt panic("%s: unknown RMI PRID IMPL", __func__);
529 1.2 matt }
530 1.2 matt
531 1.2 matt aprint_normal("%s: link config %s\n",
532 1.2 matt device_xname(sc->sc_dev), sc->sc_pcie_lnktab.str);
533 1.2 matt }
534 1.2 matt
535 1.2 matt static void
536 1.2 matt rmixl_pcie_errata(struct rmixl_pcie_softc *sc)
537 1.2 matt {
538 1.2 matt u_int rev;
539 1.2 matt u_int lanes;
540 1.2 matt bool e391 = false;
541 1.2 matt
542 1.2 matt /*
543 1.2 matt * 3.9.1 PCIe Link-0 Registers Reset to Incorrect Values
544 1.2 matt * check if it allies to this CPU implementation and revision
545 1.2 matt */
546 1.2 matt rev = MIPS_PRID_REV(cpu_id);
547 1.2 matt switch (MIPS_PRID_IMPL(cpu_id)) {
548 1.2 matt case MIPS_XLS104:
549 1.2 matt case MIPS_XLS108:
550 1.2 matt break;
551 1.2 matt case MIPS_XLS204:
552 1.2 matt case MIPS_XLS208:
553 1.2 matt /* stepping A0 is affected */
554 1.2 matt if (rev == 0)
555 1.2 matt e391 = true;
556 1.2 matt break;
557 1.2 matt case MIPS_XLS404LITE:
558 1.2 matt case MIPS_XLS408LITE:
559 1.2 matt break;
560 1.2 matt case MIPS_XLS404:
561 1.2 matt case MIPS_XLS408:
562 1.2 matt case MIPS_XLS416:
563 1.2 matt /* steppings A0 and A1 are affected */
564 1.2 matt if ((rev == 0) || (rev == 1))
565 1.2 matt e391 = true;
566 1.2 matt break;
567 1.2 matt case MIPS_XLS608:
568 1.2 matt case MIPS_XLS616:
569 1.2 matt break;
570 1.2 matt default:
571 1.2 matt panic("unknown RMI PRID IMPL");
572 1.2 matt }
573 1.2 matt
574 1.2 matt /*
575 1.2 matt * for XLS we only need to check entry #0
576 1.2 matt * this may need to change for later XL family chips
577 1.2 matt */
578 1.2 matt lanes = sc->sc_pcie_lnktab.cfg[0].lanes;
579 1.2 matt
580 1.2 matt if ((e391 != false) && ((lanes == 2) || (lanes == 4))) {
581 1.2 matt /*
582 1.2 matt * attempt work around for errata 3.9.1
583 1.2 matt * "PCIe Link-0 Registers Reset to Incorrect Values"
584 1.2 matt * the registers are write-once: if the firmware already wrote,
585 1.2 matt * then our writes are ignored; hope they did it right.
586 1.2 matt */
587 1.2 matt uint32_t queuectrl;
588 1.2 matt uint32_t bufdepth;
589 1.2 matt #ifdef DIAGNOSTIC
590 1.2 matt uint32_t r;
591 1.2 matt #endif
592 1.2 matt
593 1.2 matt aprint_normal("%s: attempt work around for errata 3.9.1",
594 1.2 matt device_xname(sc->sc_dev));
595 1.2 matt if (lanes == 4) {
596 1.2 matt queuectrl = 0x00018074;
597 1.2 matt bufdepth = 0x001901D1;
598 1.2 matt } else {
599 1.2 matt queuectrl = 0x00018036;
600 1.2 matt bufdepth = 0x001900D9;
601 1.2 matt }
602 1.2 matt
603 1.2 matt RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_BE +
604 1.2 matt RMIXL_VC0_POSTED_RX_QUEUE_CTRL, queuectrl);
605 1.2 matt RMIXL_IOREG_WRITE(RMIXL_IO_DEV_PCIE_BE +
606 1.2 matt RMIXL_VC0_POSTED_BUFFER_DEPTH, bufdepth);
607 1.2 matt
608 1.2 matt #ifdef DIAGNOSTIC
609 1.2 matt r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_BE +
610 1.2 matt RMIXL_VC0_POSTED_RX_QUEUE_CTRL);
611 1.2 matt printf("\nVC0_POSTED_RX_QUEUE_CTRL %#x\n", r);
612 1.2 matt
613 1.2 matt r = RMIXL_IOREG_READ(RMIXL_IO_DEV_PCIE_BE +
614 1.2 matt RMIXL_VC0_POSTED_BUFFER_DEPTH);
615 1.2 matt printf("VC0_POSTED_BUFFER_DEPTH %#x\n", r);
616 1.2 matt #endif
617 1.2 matt }
618 1.2 matt }
619 1.2 matt
620 1.2 matt static void
621 1.2 matt rmixl_pcie_init(struct rmixl_pcie_softc *sc)
622 1.2 matt {
623 1.2 matt pci_chipset_tag_t pc = &sc->sc_pci_chipset;
624 1.2 matt #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
625 1.2 matt struct extent *ioext, *memext;
626 1.2 matt #endif
627 1.2 matt
628 1.2 matt pc->pc_conf_v = (void *)sc;
629 1.2 matt pc->pc_attach_hook = rmixl_pcie_attach_hook;
630 1.2 matt pc->pc_bus_maxdevs = rmixl_pcie_bus_maxdevs;
631 1.2 matt pc->pc_make_tag = rmixl_pcie_make_tag;
632 1.2 matt pc->pc_decompose_tag = rmixl_pcie_decompose_tag;
633 1.2 matt pc->pc_conf_read = rmixl_pcie_conf_read;
634 1.2 matt pc->pc_conf_write = rmixl_pcie_conf_write;
635 1.2 matt
636 1.2 matt pc->pc_intr_v = (void *)sc;
637 1.2 matt pc->pc_intr_map = rmixl_pcie_intr_map;
638 1.2 matt pc->pc_intr_string = rmixl_pcie_intr_string;
639 1.2 matt pc->pc_intr_evcnt = rmixl_pcie_intr_evcnt;
640 1.2 matt pc->pc_intr_establish = rmixl_pcie_intr_establish;
641 1.2 matt pc->pc_intr_disestablish = rmixl_pcie_intr_disestablish;
642 1.2 matt pc->pc_conf_interrupt = rmixl_conf_interrupt;
643 1.2 matt
644 1.2 matt #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
645 1.2 matt /*
646 1.2 matt * Configure the PCI bus.
647 1.2 matt */
648 1.2 matt struct rmixl_config *rcp = &rmixl_configuration;
649 1.2 matt
650 1.2 matt aprint_normal("%s: configuring PCI bus\n",
651 1.2 matt device_xname(sc->sc_dev));
652 1.2 matt
653 1.2 matt ioext = extent_create("pciio",
654 1.2 matt rcp->rc_pci_io_pbase,
655 1.2 matt rcp->rc_pci_io_pbase + rcp->rc_pci_io_size - 1,
656 1.2 matt M_DEVBUF, NULL, 0, EX_NOWAIT);
657 1.2 matt
658 1.2 matt memext = extent_create("pcimem",
659 1.2 matt rcp->rc_pci_mem_pbase,
660 1.2 matt rcp->rc_pci_mem_pbase + rcp->rc_pci_mem_size - 1,
661 1.2 matt M_DEVBUF, NULL, 0, EX_NOWAIT);
662 1.2 matt
663 1.2 matt pci_configure_bus(pc, ioext, memext, NULL, 0, mips_dcache_align);
664 1.2 matt
665 1.2 matt extent_destroy(ioext);
666 1.2 matt extent_destroy(memext);
667 1.2 matt #endif
668 1.2 matt }
669 1.2 matt
670 1.2 matt static void
671 1.2 matt rmixl_pcie_init_ecfg(struct rmixl_pcie_softc *sc)
672 1.2 matt {
673 1.2 matt void *v;
674 1.2 matt pcitag_t tag;
675 1.2 matt pcireg_t r;
676 1.2 matt
677 1.2 matt v = sc;
678 1.2 matt tag = rmixl_pcie_make_tag(v, 0, 0, 0);
679 1.2 matt
680 1.2 matt #ifdef PCI_DEBUG
681 1.2 matt int i, offset;
682 1.2 matt static const int offtab[] =
683 1.2 matt { 0, 4, 8, 0xc, 0x10, 0x14, 0x18, 0x1c,
684 1.2 matt 0x2c, 0x30, 0x34 };
685 1.2 matt for (i=0; i < sizeof(offtab)/sizeof(offtab[0]); i++) {
686 1.2 matt offset = 0x100 + offtab[i];
687 1.2 matt r = rmixl_pcie_conf_read(v, tag, offset);
688 1.2 matt printf("%s: %#x: %#x\n", __func__, offset, r);
689 1.2 matt }
690 1.2 matt #endif
691 1.2 matt r = rmixl_pcie_conf_read(v, tag, 0x100);
692 1.2 matt if (r == -1)
693 1.2 matt return; /* cannot access */
694 1.2 matt
695 1.2 matt /* check pre-existing uncorrectable errs */
696 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UESR);
697 1.2 matt r &= ~PCIE_ECFG_UExR_RESV;
698 1.2 matt if (r != 0)
699 1.2 matt panic("%s: Uncorrectable Error Status: %#x\n",
700 1.2 matt __func__, r);
701 1.2 matt
702 1.2 matt /* unmask all uncorrectable errs */
703 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UEMR);
704 1.2 matt r &= ~PCIE_ECFG_UExR_RESV;
705 1.2 matt rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEMR, r);
706 1.2 matt
707 1.2 matt /* ensure default uncorrectable err severity confniguration */
708 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_UEVR);
709 1.2 matt r &= ~PCIE_ECFG_UExR_RESV;
710 1.2 matt r |= PCIE_ECFG_UEVR_DFLT;
711 1.2 matt rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEVR, r);
712 1.2 matt
713 1.2 matt /* check pre-existing correctable errs */
714 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_CESR);
715 1.2 matt r &= ~PCIE_ECFG_CExR_RESV;
716 1.2 matt #ifdef DIAGNOSTIC
717 1.2 matt if (r != 0)
718 1.2 matt aprint_normal("%s: Correctable Error Status: %#x\n",
719 1.2 matt device_xname(sc->sc_dev), r);
720 1.2 matt #endif
721 1.2 matt
722 1.2 matt /* unmask all correctable errs */
723 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_CEMR);
724 1.2 matt r &= ~PCIE_ECFG_CExR_RESV;
725 1.2 matt rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_UEMR, r);
726 1.2 matt
727 1.2 matt /* check pre-existing Root Error Status */
728 1.2 matt r = rmixl_pcie_conf_read(v, tag, RMIXL_PCIE_ECFG_RESR);
729 1.2 matt r &= ~PCIE_ECFG_RESR_RESV;
730 1.2 matt if (r != 0)
731 1.2 matt panic("%s: Root Error Status: %#x\n", __func__, r);
732 1.2 matt /* XXX TMP FIXME */
733 1.2 matt
734 1.2 matt /* enable all Root errs */
735 1.2 matt r = (pcireg_t)(~PCIE_ECFG_RECR_RESV);
736 1.2 matt rmixl_pcie_conf_write(v, tag, RMIXL_PCIE_ECFG_RECR, r);
737 1.2 matt
738 1.2 matt
739 1.2 matt if (MIPS_PRID_IMPL(cpu_id) == MIPS_XLS408LITE) {
740 1.2 matt /*
741 1.2 matt * establish ISR for PCIE Fatal Error interrupt
742 1.2 matt * XXX for XLS408Lite, XLS2xx, XLS1xx only
743 1.2 matt * tested on XLS408Lite only
744 1.2 matt */
745 1.2 matt (void)rmixl_intr_establish(29, IPL_HIGH,
746 1.2 matt RMIXL_INTR_LEVEL, RMIXL_INTR_HIGH,
747 1.2 matt rmixl_pcie_error_intr, v);
748 1.2 matt }
749 1.2 matt #if defined(DEBUG) || defined(DDB)
750 1.2 matt rmixl_pcie_v = v;
751 1.2 matt #endif
752 1.2 matt }
753 1.2 matt
754 1.2 matt void
755 1.2 matt rmixl_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
756 1.2 matt {
757 1.2 matt DPRINTF(("%s: %p, %d, %d, %d, %d, %p\n",
758 1.2 matt __func__, v, bus, dev, ipin, swiz, iline));
759 1.2 matt }
760 1.2 matt
761 1.2 matt void
762 1.2 matt rmixl_pcie_attach_hook(struct device *parent, struct device *self,
763 1.2 matt struct pcibus_attach_args *pba)
764 1.2 matt {
765 1.2 matt DPRINTF(("%s: pba_bus %d, pba_bridgetag %p, pc_conf_v %p\n",
766 1.2 matt __func__, pba->pba_bus, pba->pba_bridgetag,
767 1.2 matt pba->pba_pc->pc_conf_v));
768 1.2 matt }
769 1.2 matt
770 1.2 matt int
771 1.2 matt rmixl_pcie_bus_maxdevs(void *v, int busno)
772 1.2 matt {
773 1.2 matt return (32); /* XXX depends on the family of XLS SoC */
774 1.2 matt }
775 1.2 matt
776 1.2 matt /*
777 1.2 matt * rmixl_tag_to_ecfg - convert cfg address (generic tag) to ecfg address
778 1.2 matt *
779 1.2 matt * 39:29 (reserved)
780 1.2 matt * 28 Swap (0=little, 1=big endian)
781 1.2 matt * 27:20 Bus number
782 1.2 matt * 19:15 Device number
783 1.2 matt * 14:12 Function number
784 1.2 matt * 11:8 Extended Register number
785 1.2 matt * 7:0 Register number
786 1.2 matt */
787 1.2 matt static pcitag_t
788 1.2 matt rmixl_tag_to_ecfg(pcitag_t tag)
789 1.2 matt {
790 1.2 matt KASSERT((tag & __BITS(7,0)) == 0);
791 1.2 matt return (tag << 4);
792 1.2 matt }
793 1.2 matt
794 1.2 matt /*
795 1.2 matt * XLS pci tag is a 40 bit address composed thusly:
796 1.2 matt * 39:25 (reserved)
797 1.2 matt * 24 Swap (0=little, 1=big endian)
798 1.2 matt * 23:16 Bus number
799 1.2 matt * 15:11 Device number
800 1.2 matt * 10:8 Function number
801 1.2 matt * 7:0 Register number
802 1.2 matt *
803 1.2 matt * Note: this is the "native" composition for addressing CFG space, but not for ECFG space.
804 1.2 matt */
805 1.2 matt pcitag_t
806 1.2 matt rmixl_pcie_make_tag(void *v, int bus, int dev, int fun)
807 1.2 matt {
808 1.2 matt return ((bus << 16) | (dev << 11) | (fun << 8));
809 1.2 matt }
810 1.2 matt
811 1.2 matt void
812 1.2 matt rmixl_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
813 1.2 matt {
814 1.2 matt if (bp != NULL)
815 1.2 matt *bp = (tag >> 16) & 0xff;
816 1.2 matt if (dp != NULL)
817 1.2 matt *dp = (tag >> 11) & 0x1f;
818 1.2 matt if (fp != NULL)
819 1.2 matt *fp = (tag >> 8) & 0x7;
820 1.2 matt }
821 1.2 matt
822 1.2 matt void
823 1.2 matt rmixl_pcie_tag_print(const char *restrict s, void *v, pcitag_t tag, int offset,
824 1.2 matt vaddr_t va, u_long r)
825 1.2 matt {
826 1.2 matt int bus, dev, fun;
827 1.2 matt
828 1.2 matt rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
829 1.2 matt printf("%s: %d/%d/%d/%d - %#" PRIxVADDR ":%#lx\n",
830 1.2 matt s, bus, dev, fun, offset, va, r);
831 1.2 matt }
832 1.2 matt
833 1.2 matt static int
834 1.2 matt rmixl_pcie_conf_setup(struct rmixl_pcie_softc *sc,
835 1.2 matt pcitag_t tag, int *offp, bus_space_tag_t *bstp,
836 1.2 matt bus_space_handle_t *bshp)
837 1.2 matt {
838 1.2 matt struct rmixl_config *rcp = &rmixl_configuration;
839 1.2 matt bus_space_tag_t bst;
840 1.2 matt bus_space_handle_t bsh;
841 1.2 matt bus_size_t size;
842 1.2 matt pcitag_t mask;
843 1.2 matt bus_addr_t ba;
844 1.2 matt int err;
845 1.2 matt static bus_space_handle_t cfg_bsh;
846 1.2 matt static bus_addr_t cfg_oba = -1;
847 1.2 matt static bus_space_handle_t ecfg_bsh;
848 1.2 matt static bus_addr_t ecfg_oba = -1;
849 1.2 matt
850 1.2 matt /*
851 1.2 matt * bus space depends on offset
852 1.2 matt */
853 1.2 matt if ((*offp >= 0) && (*offp < 0x100)) {
854 1.2 matt mask = __BITS(15,0);
855 1.2 matt bst = sc->sc_pcie_cfg_memt;
856 1.2 matt ba = rcp->rc_pcie_cfg_pbase;
857 1.2 matt ba += (tag & ~mask);
858 1.2 matt *offp += (tag & mask);
859 1.2 matt if (ba != cfg_oba) {
860 1.2 matt size = (bus_size_t)(mask + 1);
861 1.2 matt if (cfg_oba != -1)
862 1.2 matt bus_space_unmap(bst, cfg_bsh, size);
863 1.2 matt err = bus_space_map(bst, ba, size, 0, &cfg_bsh);
864 1.2 matt if (err != 0) {
865 1.2 matt #ifdef DEBUG
866 1.2 matt panic("%s: bus_space_map err %d, CFG space",
867 1.2 matt __func__, err); /* XXX */
868 1.2 matt #endif
869 1.2 matt return -1;
870 1.2 matt }
871 1.2 matt cfg_oba = ba;
872 1.2 matt }
873 1.2 matt bsh = cfg_bsh;
874 1.2 matt } else if ((*offp >= 0x100) && (*offp <= 0x700)) {
875 1.2 matt mask = __BITS(14,0);
876 1.2 matt tag = rmixl_tag_to_ecfg(tag); /* convert to ECFG format */
877 1.2 matt bst = sc->sc_pcie_ecfg_memt;
878 1.2 matt ba = rcp->rc_pcie_ecfg_pbase;
879 1.2 matt ba += (tag & ~mask);
880 1.2 matt *offp += (tag & mask);
881 1.2 matt if (ba != ecfg_oba) {
882 1.2 matt size = (bus_size_t)(mask + 1);
883 1.2 matt if (ecfg_oba != -1)
884 1.2 matt bus_space_unmap(bst, ecfg_bsh, size);
885 1.2 matt err = bus_space_map(bst, ba, size, 0, &ecfg_bsh);
886 1.2 matt if (err != 0) {
887 1.2 matt #ifdef DEBUH
888 1.2 matt panic("%s: bus_space_map err %d, ECFG space",
889 1.2 matt __func__, err); /* XXX */
890 1.2 matt #endif
891 1.2 matt return -1;
892 1.2 matt }
893 1.2 matt ecfg_oba = ba;
894 1.2 matt }
895 1.2 matt bsh = ecfg_bsh;
896 1.2 matt } else {
897 1.2 matt #ifdef DEBUG
898 1.2 matt panic("%s: offset %#x: unknown", __func__, *offp);
899 1.2 matt #endif
900 1.2 matt return -1;
901 1.2 matt }
902 1.2 matt
903 1.2 matt *bstp = bst;
904 1.2 matt *bshp = bsh;
905 1.2 matt
906 1.2 matt return 0;
907 1.2 matt }
908 1.2 matt
909 1.2 matt pcireg_t
910 1.2 matt rmixl_pcie_conf_read(void *v, pcitag_t tag, int offset)
911 1.2 matt {
912 1.2 matt struct rmixl_pcie_softc *sc = v;
913 1.2 matt static bus_space_handle_t bsh;
914 1.2 matt bus_space_tag_t bst;
915 1.2 matt pcireg_t rv;
916 1.2 matt uint64_t cfg0;
917 1.2 matt u_int s;
918 1.2 matt
919 1.2 matt PCI_CONF_LOCK(s);
920 1.2 matt
921 1.2 matt if (rmixl_pcie_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
922 1.2 matt cfg0 = rmixl_cache_err_dis();
923 1.2 matt rv = bus_space_read_4(bst, bsh, (bus_size_t)offset);
924 1.2 matt if (rmixl_cache_err_check() != 0) {
925 1.2 matt #ifdef DIAGNOSTIC
926 1.2 matt int bus, dev, fun;
927 1.2 matt
928 1.2 matt rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
929 1.2 matt printf("%s: %d/%d/%d, offset %#x: bad address\n",
930 1.2 matt __func__, bus, dev, fun, offset);
931 1.2 matt #endif
932 1.2 matt rv = (pcireg_t) -1;
933 1.2 matt }
934 1.2 matt rmixl_cache_err_restore(cfg0);
935 1.2 matt } else {
936 1.2 matt rv = -1;
937 1.2 matt }
938 1.2 matt
939 1.2 matt PCI_CONF_UNLOCK(s);
940 1.2 matt return rv;
941 1.2 matt }
942 1.2 matt
943 1.2 matt void
944 1.2 matt rmixl_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
945 1.2 matt {
946 1.2 matt struct rmixl_pcie_softc *sc = v;
947 1.2 matt static bus_space_handle_t bsh;
948 1.2 matt bus_space_tag_t bst;
949 1.2 matt uint64_t cfg0;
950 1.2 matt u_int s;
951 1.2 matt
952 1.2 matt PCI_CONF_LOCK(s);
953 1.2 matt
954 1.2 matt if (rmixl_pcie_conf_setup(sc, tag, &offset, &bst, &bsh) == 0) {
955 1.2 matt cfg0 = rmixl_cache_err_dis();
956 1.2 matt bus_space_write_4(bst, bsh, (bus_size_t)offset, val);
957 1.2 matt if (rmixl_cache_err_check() != 0) {
958 1.2 matt #ifdef DIAGNOSTIC
959 1.2 matt int bus, dev, fun;
960 1.2 matt
961 1.2 matt rmixl_pcie_decompose_tag(v, tag, &bus, &dev, &fun);
962 1.2 matt printf("%s: %d/%d/%d, offset %#x: bad address\n",
963 1.2 matt __func__, bus, dev, fun, offset);
964 1.2 matt #endif
965 1.2 matt }
966 1.2 matt rmixl_cache_err_restore(cfg0);
967 1.2 matt }
968 1.2 matt
969 1.2 matt PCI_CONF_UNLOCK(s);
970 1.2 matt }
971 1.2 matt
972 1.2 matt int
973 1.2 matt rmixl_pcie_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *pih)
974 1.2 matt {
975 1.2 matt u_int irq;
976 1.2 matt
977 1.2 matt #ifdef DEBUG
978 1.2 matt DPRINTF(("%s: ps_bus %d, pa_intrswiz %#x, pa_intrtag %#lx,"
979 1.2 matt " pa_intrpin %d, pa_intrline %d, pa_rawintrpin %d\n",
980 1.2 matt __func__, pa->pa_bus, pa->pa_intrswiz, pa->pa_intrtag,
981 1.2 matt pa->pa_intrpin, pa->pa_intrline, pa->pa_rawintrpin));
982 1.2 matt #endif
983 1.2 matt
984 1.2 matt /*
985 1.2 matt * XXX cpu implementation specific
986 1.2 matt */
987 1.2 matt switch (MIPS_PRID_IMPL(cpu_id)) {
988 1.2 matt case MIPS_XLS408LITE:
989 1.2 matt switch (pa->pa_bus) {
990 1.2 matt case 1:
991 1.2 matt irq = 26;
992 1.2 matt break;
993 1.2 matt case 2:
994 1.2 matt irq = 27;
995 1.2 matt break;
996 1.2 matt default:
997 1.2 matt panic("%s: bad bus %d\n", __func__, pa->pa_bus);
998 1.2 matt }
999 1.2 matt break;
1000 1.2 matt case MIPS_XLS416:
1001 1.2 matt case MIPS_XLS616:
1002 1.2 matt switch (pa->pa_bus) {
1003 1.2 matt case 1:
1004 1.2 matt irq = 26;
1005 1.2 matt break;
1006 1.2 matt case 2:
1007 1.2 matt irq = 27;
1008 1.2 matt break;
1009 1.2 matt case 3:
1010 1.2 matt irq = 28;
1011 1.2 matt break;
1012 1.2 matt case 4:
1013 1.2 matt irq = 29;
1014 1.2 matt break;
1015 1.2 matt default:
1016 1.2 matt panic("%s: bad bus %d\n", __func__, pa->pa_bus);
1017 1.2 matt }
1018 1.2 matt break;
1019 1.2 matt default:
1020 1.2 matt panic("%s: cpu IMPL %#x not supported\n",
1021 1.2 matt __func__, MIPS_PRID_IMPL(cpu_id));
1022 1.2 matt }
1023 1.2 matt
1024 1.2 matt *pih = irq;
1025 1.2 matt
1026 1.2 matt return 0;
1027 1.2 matt }
1028 1.2 matt
1029 1.2 matt const char *
1030 1.2 matt rmixl_pcie_intr_string(void *v, pci_intr_handle_t pih)
1031 1.2 matt {
1032 1.2 matt const char *name = "(illegal)";
1033 1.2 matt int irq = (int)pih;
1034 1.2 matt
1035 1.2 matt switch (MIPS_PRID_IMPL(cpu_id)) {
1036 1.2 matt case MIPS_XLS408LITE:
1037 1.2 matt switch (irq) {
1038 1.2 matt case 26:
1039 1.2 matt case 27:
1040 1.2 matt name = rmixl_intr_string(irq);
1041 1.2 matt break;
1042 1.2 matt }
1043 1.2 matt break;
1044 1.2 matt case MIPS_XLS616:
1045 1.2 matt switch (irq) {
1046 1.2 matt case 26:
1047 1.2 matt case 27:
1048 1.2 matt case 28:
1049 1.2 matt case 29:
1050 1.2 matt name = rmixl_intr_string(irq);
1051 1.2 matt break;
1052 1.2 matt }
1053 1.2 matt break;
1054 1.2 matt }
1055 1.2 matt
1056 1.2 matt return name;
1057 1.2 matt }
1058 1.2 matt
1059 1.2 matt const struct evcnt *
1060 1.2 matt rmixl_pcie_intr_evcnt(void *v, pci_intr_handle_t pih)
1061 1.2 matt {
1062 1.2 matt return NULL;
1063 1.2 matt }
1064 1.2 matt
1065 1.2 matt static int
1066 1.2 matt rmixl_pcie_irq(pci_intr_handle_t pih)
1067 1.2 matt {
1068 1.2 matt return (int)pih;
1069 1.2 matt }
1070 1.2 matt
1071 1.2 matt static void *
1072 1.2 matt rmixl_pcie_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
1073 1.2 matt int (*func)(void *), void *arg)
1074 1.2 matt {
1075 1.2 matt return rmixl_intr_establish(rmixl_pcie_irq((int)pih), ipl,
1076 1.2 matt RMIXL_INTR_LEVEL, RMIXL_INTR_HIGH, func, arg);
1077 1.2 matt }
1078 1.2 matt
1079 1.2 matt static void
1080 1.2 matt rmixl_pcie_intr_disestablish(void *v, void *ih)
1081 1.2 matt {
1082 1.2 matt rmixl_intr_disestablish(ih);
1083 1.2 matt }
1084 1.2 matt
1085 1.2 matt #if defined(DEBUG) || defined(DDB)
1086 1.2 matt /* this function exists to facilitate call from ddb */
1087 1.2 matt int
1088 1.2 matt rmixl_pcie_error_check(void)
1089 1.2 matt {
1090 1.2 matt if (rmixl_pcie_v != 0)
1091 1.2 matt return _rmixl_pcie_error_check(rmixl_pcie_v);
1092 1.2 matt return -1;
1093 1.2 matt }
1094 1.2 matt #endif
1095 1.2 matt
1096 1.2 matt STATIC int
1097 1.2 matt _rmixl_pcie_error_check(void *v)
1098 1.2 matt {
1099 1.2 matt int i, offset;
1100 1.2 matt pcireg_t r;
1101 1.2 matt pcitag_t tag;
1102 1.2 matt int err=0;
1103 1.2 matt #ifdef DIAGNOSTIC
1104 1.2 matt pcireg_t regs[PCIE_ECFG_ERRS_OFFTAB_NENTRIES];
1105 1.2 matt #endif
1106 1.2 matt
1107 1.2 matt tag = rmixl_pcie_make_tag(v, 0, 0, 0); /* XXX */
1108 1.2 matt
1109 1.2 matt for (i=0; i < PCIE_ECFG_ERRS_OFFTAB_NENTRIES; i++) {
1110 1.2 matt offset = pcie_ecfg_errs_tab[i].offset;
1111 1.2 matt r = rmixl_pcie_conf_read(v, tag, offset);
1112 1.2 matt #ifdef DIAGNOSTIC
1113 1.2 matt regs[i] = r;
1114 1.2 matt #endif
1115 1.2 matt if (r != 0) {
1116 1.2 matt pcireg_t rw1c = r & pcie_ecfg_errs_tab[i].rw1c;
1117 1.2 matt if (rw1c != 0) {
1118 1.2 matt /* attempt to clear the error */
1119 1.2 matt rmixl_pcie_conf_write(v, tag, offset, rw1c);
1120 1.2 matt };
1121 1.2 matt if (offset == RMIXL_PCIE_ECFG_CESR)
1122 1.2 matt err |= 1; /* correctable */
1123 1.2 matt else
1124 1.2 matt err |= 2; /* uncorrectable */
1125 1.2 matt }
1126 1.2 matt }
1127 1.2 matt #ifdef DIAGNOSTIC
1128 1.2 matt if (err != 0) {
1129 1.2 matt for (i=0; i < PCIE_ECFG_ERRS_OFFTAB_NENTRIES; i++) {
1130 1.2 matt offset = pcie_ecfg_errs_tab[i].offset;
1131 1.2 matt printf("%s: %#x: %#x\n", __func__, offset, regs[i]);
1132 1.2 matt }
1133 1.2 matt }
1134 1.2 matt #endif
1135 1.2 matt
1136 1.2 matt return err;
1137 1.2 matt }
1138 1.2 matt
1139 1.2 matt static int
1140 1.2 matt rmixl_pcie_error_intr(void *v)
1141 1.2 matt {
1142 1.2 matt if (_rmixl_pcie_error_check(v) < 2)
1143 1.2 matt return 0; /* correctable */
1144 1.2 matt
1145 1.2 matt /* uncorrectable */
1146 1.2 matt #if DDB
1147 1.2 matt Debugger();
1148 1.2 matt #endif
1149 1.2 matt
1150 1.2 matt /* XXX reset and recover? */
1151 1.2 matt
1152 1.2 matt panic("%s\n", __func__);
1153 1.2 matt }
1154