bcm53xx_pax.c revision 1.20 1 1.1 matt /*-
2 1.1 matt * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1 matt * All rights reserved.
4 1.1 matt *
5 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
6 1.1 matt * by Matt Thomas of 3am Software Foundry.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt *
17 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
28 1.1 matt */
29 1.1 matt
30 1.4 matt #define _ARM32_BUS_DMA_PRIVATE
31 1.1 matt #define PCIE_PRIVATE
32 1.1 matt
33 1.1 matt #include "locators.h"
34 1.1 matt
35 1.1 matt #include <sys/cdefs.h>
36 1.1 matt
37 1.20 skrll __KERNEL_RCSID(1, "$NetBSD: bcm53xx_pax.c,v 1.20 2020/10/30 18:54:36 skrll Exp $");
38 1.1 matt
39 1.11 matt #include <sys/param.h>
40 1.1 matt #include <sys/bus.h>
41 1.1 matt #include <sys/device.h>
42 1.1 matt #include <sys/intr.h>
43 1.4 matt #include <sys/kmem.h>
44 1.1 matt #include <sys/systm.h>
45 1.1 matt
46 1.1 matt #include <dev/pci/pcireg.h>
47 1.1 matt #include <dev/pci/pcivar.h>
48 1.2 matt #include <dev/pci/pciconf.h>
49 1.1 matt
50 1.12 matt #include <arm/locore.h>
51 1.12 matt
52 1.1 matt #include <arm/broadcom/bcm53xx_reg.h>
53 1.1 matt #include <arm/broadcom/bcm53xx_var.h>
54 1.1 matt
55 1.4 matt #ifndef __HAVE_PCI_CONF_HOOK
56 1.4 matt #error __HAVE_PCI_CONF_HOOK must be defined
57 1.4 matt #endif
58 1.4 matt
59 1.2 matt static const struct {
60 1.2 matt paddr_t owin_base;
61 1.2 matt psize_t owin_size;
62 1.2 matt } bcmpax_owins[] = {
63 1.2 matt [0] = { BCM53XX_PCIE0_OWIN_PBASE, BCM53XX_PCIE0_OWIN_SIZE },
64 1.2 matt [1] = { BCM53XX_PCIE1_OWIN_PBASE, BCM53XX_PCIE1_OWIN_SIZE },
65 1.2 matt [2] = { BCM53XX_PCIE2_OWIN_PBASE, BCM53XX_PCIE2_OWIN_SIZE },
66 1.2 matt };
67 1.2 matt
68 1.1 matt static int bcmpax_ccb_match(device_t, cfdata_t, void *);
69 1.1 matt static void bcmpax_ccb_attach(device_t, device_t, void *);
70 1.1 matt
71 1.4 matt struct bcmpax_intrhand {
72 1.4 matt TAILQ_ENTRY(bcmpax_intrhand) ih_link;
73 1.4 matt int (*ih_func)(void *);
74 1.4 matt void *ih_arg;
75 1.4 matt int ih_ipl;
76 1.4 matt };
77 1.4 matt
78 1.4 matt TAILQ_HEAD(bcmpax_ihqh, bcmpax_intrhand);
79 1.4 matt
80 1.1 matt struct bcmpax_softc {
81 1.1 matt device_t sc_dev;
82 1.1 matt bus_space_tag_t sc_bst;
83 1.1 matt bus_space_handle_t sc_bsh;
84 1.1 matt bus_dma_tag_t sc_dmat;
85 1.2 matt kmutex_t *sc_lock;
86 1.2 matt kmutex_t *sc_cfg_lock;
87 1.2 matt bool sc_linkup;
88 1.2 matt int sc_pba_flags;
89 1.4 matt uint32_t sc_intrgen;
90 1.2 matt struct arm32_pci_chipset sc_pc;
91 1.4 matt struct bcmpax_ihqh sc_intrs;
92 1.4 matt void *sc_ih[6];
93 1.4 matt int sc_port;
94 1.1 matt };
95 1.1 matt
96 1.1 matt static inline uint32_t
97 1.1 matt bcmpax_read_4(struct bcmpax_softc *sc, bus_size_t o)
98 1.1 matt {
99 1.1 matt return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
100 1.1 matt }
101 1.1 matt
102 1.1 matt static inline void
103 1.1 matt bcmpax_write_4(struct bcmpax_softc *sc, bus_size_t o, uint32_t v)
104 1.1 matt {
105 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
106 1.1 matt }
107 1.1 matt
108 1.2 matt static void bcmpax_attach_hook(device_t, device_t, struct pcibus_attach_args *);
109 1.2 matt static int bcmpax_bus_maxdevs(void *, int);
110 1.2 matt static pcitag_t bcmpax_make_tag(void *, int, int, int);
111 1.2 matt static void bcmpax_decompose_tag(void *, pcitag_t, int *, int *, int *);
112 1.2 matt static pcireg_t bcmpax_conf_read(void *, pcitag_t, int);
113 1.2 matt static void bcmpax_conf_write(void *, pcitag_t, int, pcireg_t);
114 1.2 matt
115 1.2 matt static int bcmpax_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
116 1.13 matt static const char *bcmpax_intr_string(void *, pci_intr_handle_t, char *, size_t);
117 1.2 matt static const struct evcnt *bcmpax_intr_evcnt(void *, pci_intr_handle_t);
118 1.2 matt static void *bcmpax_intr_establish(void *, pci_intr_handle_t, int,
119 1.17 jmcneill int (*)(void *), void *, const char *);
120 1.2 matt static void bcmpax_intr_disestablish(void *, void *);
121 1.2 matt
122 1.2 matt static int bcmpax_conf_hook(void *, int, int, int, pcireg_t);
123 1.2 matt static void bcmpax_conf_interrupt(void *, int, int, int, int, int *);
124 1.2 matt
125 1.4 matt static int bcmpax_intr(void *);
126 1.2 matt
127 1.1 matt CFATTACH_DECL_NEW(bcmpax_ccb, sizeof(struct bcmpax_softc),
128 1.1 matt bcmpax_ccb_match, bcmpax_ccb_attach, NULL, NULL);
129 1.1 matt
130 1.1 matt static int
131 1.1 matt bcmpax_ccb_match(device_t parent, cfdata_t cf, void *aux)
132 1.1 matt {
133 1.1 matt struct bcmccb_attach_args * const ccbaa = aux;
134 1.1 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
135 1.1 matt
136 1.1 matt if (strcmp(cf->cf_name, loc->loc_name))
137 1.1 matt return 0;
138 1.1 matt
139 1.1 matt #ifdef DIAGNOSTIC
140 1.1 matt const int port = cf->cf_loc[BCMCCBCF_PORT];
141 1.1 matt #endif
142 1.1 matt KASSERT(port == BCMCCBCF_PORT_DEFAULT || port == loc->loc_port);
143 1.1 matt
144 1.1 matt return 1;
145 1.1 matt }
146 1.1 matt
147 1.4 matt static int
148 1.4 matt bcmpax_iwin_init(struct bcmpax_softc *sc)
149 1.4 matt {
150 1.5 matt #if 0
151 1.4 matt uint32_t megs = (physical_end + 0xfffff - physical_start) >> 20;
152 1.16 riastrad uint32_t iwin_megs = uimin(256, megs);
153 1.4 matt #if 1
154 1.4 matt bus_addr_t iwin1_start = physical_start;
155 1.4 matt #else
156 1.4 matt bus_addr_t iwin1_start = 0;
157 1.4 matt #endif
158 1.4 matt #if 1
159 1.16 riastrad bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | uimin(megs, 128));
160 1.4 matt bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | 1);
161 1.4 matt #else
162 1.16 riastrad bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, iwin1_start | uimin(megs, 128));
163 1.4 matt bcmpax_write_4(sc, PCIE_IARR_1_LOWER, iwin1_start | 1);
164 1.4 matt #endif
165 1.4 matt bcmpax_conf_write(sc, 0, PCI_MAPREG_START+4, iwin1_start);
166 1.4 matt if (iwin_megs > 128) {
167 1.4 matt bus_addr_t iwin2_start = iwin1_start + 128*1024*1024;
168 1.4 matt #if 1
169 1.16 riastrad bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | uimin(megs - 128, 128));
170 1.4 matt bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | 1);
171 1.4 matt #else
172 1.16 riastrad bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, iwin2_start | uimin(megs - 128, 128));
173 1.4 matt bcmpax_write_4(sc, PCIE_IARR_2_LOWER, iwin2_start | 1);
174 1.4 matt #endif
175 1.4 matt bcmpax_conf_write(sc, 0, PCI_MAPREG_START+8, iwin2_start);
176 1.4 matt }
177 1.4 matt
178 1.4 matt if (megs <= iwin_megs) {
179 1.4 matt /*
180 1.4 matt * We could can DMA to all of memory so we don't need to subregion!
181 1.4 matt */
182 1.4 matt return 0;
183 1.4 matt }
184 1.4 matt
185 1.4 matt return bus_dmatag_subregion(sc->sc_dmat, physical_start,
186 1.4 matt physical_start + (iwin_megs << 20) - 1, &sc->sc_dmat, 0);
187 1.5 matt #else
188 1.5 matt bcmpax_write_4(sc, PCIE_IARR_1_LOWER, 0);
189 1.5 matt bcmpax_write_4(sc, PCIE_FUNC0_IMAP1, 0);
190 1.5 matt bcmpax_write_4(sc, PCIE_IARR_2_LOWER, 0);
191 1.5 matt bcmpax_write_4(sc, PCIE_FUNC0_IMAP2, 0);
192 1.5 matt return 0;
193 1.5 matt #endif
194 1.4 matt }
195 1.4 matt
196 1.1 matt static void
197 1.1 matt bcmpax_ccb_attach(device_t parent, device_t self, void *aux)
198 1.1 matt {
199 1.1 matt struct bcmpax_softc * const sc = device_private(self);
200 1.1 matt struct bcmccb_attach_args * const ccbaa = aux;
201 1.1 matt const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
202 1.8 matt cfdata_t cf = device_cfdata(self);
203 1.1 matt
204 1.1 matt sc->sc_dev = self;
205 1.9 matt sc->sc_dmat = &bcm53xx_coherent_dma_tag;
206 1.9 matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
207 1.8 matt if (cf->cf_flags & 2) {
208 1.9 matt sc->sc_dmat = &bcm53xx_bounce_dma_tag;
209 1.8 matt }
210 1.9 matt #endif
211 1.4 matt
212 1.1 matt sc->sc_bst = ccbaa->ccbaa_ccb_bst;
213 1.1 matt bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
214 1.1 matt loc->loc_offset, loc->loc_size, &sc->sc_bsh);
215 1.1 matt
216 1.4 matt /*
217 1.4 matt * Kick the hardware into RC mode.
218 1.4 matt */
219 1.2 matt bcmpax_write_4(sc, PCIE_CLK_CONTROL, 3);
220 1.2 matt delay(250);
221 1.2 matt bcmpax_write_4(sc, PCIE_CLK_CONTROL, 1);
222 1.4 matt
223 1.1 matt uint32_t v = bcmpax_read_4(sc, PCIE_STRAP_STATUS);
224 1.1 matt const bool enabled = (v & STRAP_PCIE_IF_ENABLE) != 0;
225 1.1 matt const bool is_v2_p = (v & STRAP_PCIE_USER_FOR_CE_GEN1) == 0;
226 1.1 matt const bool is_x2_p = (v & STRAP_PCIE_USER_FOR_CE_1LANE) == 0;
227 1.1 matt const bool is_rc_p = (v & STRAP_PCIE_USER_RC_MODE) != 0;
228 1.1 matt
229 1.1 matt aprint_naive("\n");
230 1.1 matt aprint_normal(": PCI Express V%u %u-lane %s Controller%s\n",
231 1.1 matt is_v2_p ? 2 : 1,
232 1.1 matt is_x2_p ? 2 : 1,
233 1.1 matt is_rc_p ? "RC" : "EP",
234 1.1 matt enabled ? "" : "(disabled)");
235 1.1 matt if (!enabled || !is_rc_p)
236 1.1 matt return;
237 1.2 matt
238 1.2 matt sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
239 1.2 matt sc->sc_cfg_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
240 1.2 matt
241 1.4 matt TAILQ_INIT(&sc->sc_intrs);
242 1.4 matt
243 1.2 matt sc->sc_pc.pc_conf_v = sc;
244 1.2 matt sc->sc_pc.pc_attach_hook = bcmpax_attach_hook;
245 1.2 matt sc->sc_pc.pc_bus_maxdevs = bcmpax_bus_maxdevs;
246 1.2 matt sc->sc_pc.pc_make_tag = bcmpax_make_tag;
247 1.2 matt sc->sc_pc.pc_decompose_tag = bcmpax_decompose_tag;
248 1.2 matt sc->sc_pc.pc_conf_read = bcmpax_conf_read;
249 1.2 matt sc->sc_pc.pc_conf_write = bcmpax_conf_write;
250 1.2 matt
251 1.2 matt sc->sc_pc.pc_intr_v = sc;
252 1.2 matt sc->sc_pc.pc_intr_map = bcmpax_intr_map;
253 1.2 matt sc->sc_pc.pc_intr_string = bcmpax_intr_string;
254 1.2 matt sc->sc_pc.pc_intr_evcnt = bcmpax_intr_evcnt;
255 1.2 matt sc->sc_pc.pc_intr_establish = bcmpax_intr_establish;
256 1.2 matt sc->sc_pc.pc_intr_disestablish = bcmpax_intr_disestablish;
257 1.2 matt
258 1.2 matt sc->sc_pc.pc_conf_hook = bcmpax_conf_hook;
259 1.2 matt sc->sc_pc.pc_conf_interrupt = bcmpax_conf_interrupt;
260 1.2 matt
261 1.4 matt sc->sc_pba_flags |= PCI_FLAGS_MRL_OKAY;
262 1.4 matt sc->sc_pba_flags |= PCI_FLAGS_MRM_OKAY;
263 1.4 matt sc->sc_pba_flags |= PCI_FLAGS_MWI_OKAY;
264 1.2 matt // sc->sc_pba_flags |= PCI_FLAGS_MSI_OKAY;
265 1.2 matt // sc->sc_pba_flags |= PCI_FLAGS_MSIX_OKAY;
266 1.2 matt
267 1.4 matt for (size_t i = 0; i < loc->loc_nintrs; i++) {
268 1.4 matt sc->sc_ih[i] = intr_establish(loc->loc_intrs[0] + i, IPL_VM,
269 1.4 matt IST_LEVEL, bcmpax_intr, sc);
270 1.4 matt if (sc->sc_ih[i] == NULL) {
271 1.4 matt aprint_error_dev(self,
272 1.4 matt "failed to establish interrupt #%zu (%zu)\n", i,
273 1.4 matt loc->loc_intrs[0] + i);
274 1.4 matt while (i-- > 0) {
275 1.4 matt intr_disestablish(sc->sc_ih[i]);
276 1.4 matt }
277 1.4 matt return;
278 1.4 matt }
279 1.4 matt }
280 1.4 matt aprint_normal_dev(self, "interrupting on irqs %d-%d\n",
281 1.4 matt loc->loc_intrs[0], loc->loc_intrs[0] + loc->loc_nintrs - 1);
282 1.4 matt
283 1.4 matt /*
284 1.4 matt * Enable INTA-INTD
285 1.4 matt */
286 1.4 matt bcmpax_write_4(sc, PCIE_SYS_RC_INTX_EN, 0x0f);
287 1.4 matt
288 1.2 matt int offset;
289 1.2 matt const bool ok = pci_get_capability(&sc->sc_pc, 0, PCI_CAP_PCIEXPRESS,
290 1.2 matt &offset, NULL);
291 1.2 matt KASSERT(ok);
292 1.2 matt
293 1.2 matt /*
294 1.6 matt * This will force the device to negotiate to a max of gen1.
295 1.6 matt */
296 1.8 matt if (cf->cf_flags & 1) {
297 1.10 msaitoh bcmpax_conf_write(sc, 0, offset + PCIE_LCSR2, 1);
298 1.6 matt }
299 1.6 matt
300 1.6 matt /*
301 1.4 matt * Now we wait (.25 sec) for the link to come up.
302 1.2 matt */
303 1.10 msaitoh offset += PCIE_LCSR;
304 1.2 matt for (size_t timo = 0;; timo++) {
305 1.2 matt const pcireg_t lcsr = bcmpax_conf_read(sc, 0, offset);
306 1.10 msaitoh sc->sc_linkup = __SHIFTOUT(lcsr, PCIE_LCSR_NLW) != 0
307 1.10 msaitoh && (1 || (lcsr & PCIE_LCSR_DLACTIVE) != 0);
308 1.2 matt if (sc->sc_linkup || timo == 250) {
309 1.2 matt aprint_debug_dev(self,
310 1.2 matt "lcsr=%#x nlw=%jd linkup=%d, timo=%zu\n",
311 1.10 msaitoh lcsr, __SHIFTOUT(lcsr, PCIE_LCSR_NLW),
312 1.2 matt sc->sc_linkup, timo);
313 1.2 matt break;
314 1.2 matt }
315 1.2 matt DELAY(1000);
316 1.2 matt }
317 1.2 matt
318 1.2 matt if (sc->sc_linkup) {
319 1.4 matt /*
320 1.4 matt * Enable the inbound (device->memory) map.
321 1.4 matt */
322 1.4 matt int error = bcmpax_iwin_init(sc);
323 1.4 matt if (error) {
324 1.4 matt aprint_error_dev(sc->sc_dev,
325 1.4 matt "failed to subregion dma tag: %d\n", error);
326 1.4 matt return;
327 1.4 matt }
328 1.4 matt
329 1.4 matt aprint_normal_dev(self, "iwin[1]=%#x/%#x iwin[2]=%#x/%#x\n",
330 1.4 matt bcmpax_read_4(sc, PCIE_FUNC0_IMAP1),
331 1.4 matt bcmpax_read_4(sc, PCIE_IARR_1_LOWER),
332 1.4 matt bcmpax_read_4(sc, PCIE_FUNC0_IMAP2),
333 1.4 matt bcmpax_read_4(sc, PCIE_IARR_2_LOWER));
334 1.4 matt
335 1.2 matt paddr_t base = bcmpax_owins[loc->loc_port].owin_base;
336 1.2 matt psize_t size = bcmpax_owins[loc->loc_port].owin_size;
337 1.2 matt KASSERT((size & ~PCIE_OARR_ADDR) == 0);
338 1.2 matt if (size > 0) {
339 1.2 matt bcmpax_write_4(sc, PCIE_OARR_0, base);
340 1.2 matt bcmpax_write_4(sc, PCIE_OMAP_0_LOWER, base | 1);
341 1.2 matt }
342 1.2 matt if (size > __LOWEST_SET_BIT(PCIE_OARR_ADDR)) {
343 1.2 matt paddr_t base1 = base + __LOWEST_SET_BIT(PCIE_OARR_ADDR);
344 1.2 matt bcmpax_write_4(sc, PCIE_OARR_1, base1);
345 1.2 matt bcmpax_write_4(sc, PCIE_OMAP_1_LOWER, base1 | 1);
346 1.2 matt }
347 1.2 matt
348 1.19 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
349 1.19 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
350 1.19 thorpej base, size);
351 1.19 thorpej error = pci_configure_bus(&sc->sc_pc, pcires,
352 1.19 thorpej 0, arm_pcache.dcache_line_size);
353 1.19 thorpej pciconf_resource_fini(pcires);
354 1.2 matt
355 1.2 matt if (error) {
356 1.2 matt aprint_normal_dev(self, "configuration failed\n");
357 1.2 matt return;
358 1.2 matt }
359 1.2 matt }
360 1.2 matt
361 1.2 matt struct pcibus_attach_args pba;
362 1.2 matt memset(&pba, 0, sizeof(pba));
363 1.2 matt
364 1.2 matt pba.pba_flags = sc->sc_pba_flags;
365 1.2 matt pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
366 1.2 matt pba.pba_memt = sc->sc_bst;
367 1.2 matt pba.pba_dmat = sc->sc_dmat;
368 1.2 matt pba.pba_pc = &sc->sc_pc;
369 1.2 matt pba.pba_bus = 0;
370 1.2 matt
371 1.2 matt config_found_ia(self, "pcibus", &pba, pcibusprint);
372 1.2 matt }
373 1.2 matt
374 1.2 matt static void
375 1.2 matt bcmpax_attach_hook(device_t parent, device_t self,
376 1.2 matt struct pcibus_attach_args *pba)
377 1.2 matt {
378 1.2 matt }
379 1.2 matt
380 1.2 matt static int
381 1.2 matt bcmpax_bus_maxdevs(void *v, int bus)
382 1.2 matt {
383 1.2 matt struct bcmpax_softc * const sc = v;
384 1.2 matt
385 1.2 matt if (__predict_true(sc->sc_linkup))
386 1.2 matt return bus > 1 ? 32 : 1;
387 1.2 matt
388 1.2 matt return bus ? 0 : 1;
389 1.1 matt }
390 1.2 matt
391 1.1 matt static void
392 1.1 matt bcmpax_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
393 1.1 matt {
394 1.1 matt if (busp)
395 1.1 matt *busp = __SHIFTOUT(tag, CFG_ADDR_BUS);
396 1.1 matt if (devp)
397 1.1 matt *devp = __SHIFTOUT(tag, CFG_ADDR_DEV);
398 1.1 matt if (funcp)
399 1.1 matt *funcp = __SHIFTOUT(tag, CFG_ADDR_FUNC);
400 1.1 matt }
401 1.1 matt
402 1.1 matt static pcitag_t
403 1.1 matt bcmpax_make_tag(void *v, int bus, int dev, int func)
404 1.1 matt {
405 1.1 matt return __SHIFTIN(bus, CFG_ADDR_BUS)
406 1.1 matt | __SHIFTIN(dev, CFG_ADDR_DEV)
407 1.1 matt | __SHIFTIN(func, CFG_ADDR_FUNC)
408 1.1 matt | (bus == 0 ? CFG_ADDR_TYPE0 : CFG_ADDR_TYPE1);
409 1.1 matt }
410 1.1 matt
411 1.2 matt static inline bus_size_t
412 1.1 matt bcmpax_conf_addr_write(struct bcmpax_softc *sc, pcitag_t tag)
413 1.1 matt {
414 1.1 matt if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV)) == 0) {
415 1.1 matt uint32_t reg = __SHIFTOUT(tag, CFG_ADDR_REG);
416 1.1 matt uint32_t func = __SHIFTOUT(tag, CFG_ADDR_FUNC);
417 1.2 matt bcmpax_write_4(sc, PCIE_CFG_IND_ADDR,
418 1.1 matt __SHIFTIN(func, CFG_IND_ADDR_FUNC)
419 1.1 matt | __SHIFTIN(reg, CFG_IND_ADDR_REG));
420 1.20 skrll dsb(sy);
421 1.2 matt return PCIE_CFG_IND_DATA;
422 1.1 matt }
423 1.2 matt if (sc->sc_linkup) {
424 1.2 matt bcmpax_write_4(sc, PCIE_CFG_ADDR, tag);
425 1.20 skrll dsb(sy);
426 1.2 matt return PCIE_CFG_DATA;
427 1.2 matt }
428 1.2 matt return 0;
429 1.1 matt }
430 1.1 matt
431 1.2 matt static pcireg_t
432 1.2 matt bcmpax_conf_read(void *v, pcitag_t tag, int reg)
433 1.2 matt {
434 1.2 matt struct bcmpax_softc * const sc = v;
435 1.2 matt
436 1.15 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
437 1.15 msaitoh return 0xffffffff;
438 1.15 msaitoh
439 1.2 matt /*
440 1.2 matt * Even in RC mode, the PCI Express Root Complex return itself
441 1.2 matt * as BCM Ethernet Controller!. We could change ppb.c to match it
442 1.2 matt * but we'll just lie and say we are a PPB bridge.
443 1.2 matt */
444 1.2 matt if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV|CFG_ADDR_FUNC)) == 0
445 1.2 matt && reg == PCI_CLASS_REG) {
446 1.2 matt return PCI_CLASS_CODE(PCI_CLASS_BRIDGE,
447 1.2 matt PCI_SUBCLASS_BRIDGE_PCI, 0);
448 1.2 matt }
449 1.2 matt
450 1.2 matt //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
451 1.2 matt
452 1.2 matt mutex_enter(sc->sc_cfg_lock);
453 1.2 matt bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
454 1.2 matt
455 1.2 matt //printf(" [from %#lx]:\n", data_reg);
456 1.2 matt
457 1.2 matt pcireg_t rv;
458 1.2 matt if (data_reg)
459 1.2 matt rv = bcmpax_read_4(sc, data_reg);
460 1.2 matt else
461 1.2 matt rv = 0xffffffff;
462 1.2 matt
463 1.2 matt mutex_exit(sc->sc_cfg_lock);
464 1.2 matt
465 1.2 matt //printf(" %#x\n", rv);
466 1.2 matt
467 1.2 matt return rv;
468 1.2 matt }
469 1.2 matt
470 1.2 matt static void
471 1.2 matt bcmpax_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
472 1.2 matt {
473 1.2 matt struct bcmpax_softc * const sc = v;
474 1.2 matt
475 1.15 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
476 1.15 msaitoh return;
477 1.15 msaitoh
478 1.2 matt mutex_enter(sc->sc_cfg_lock);
479 1.2 matt bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
480 1.2 matt
481 1.2 matt //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
482 1.2 matt
483 1.2 matt if (data_reg) {
484 1.2 matt //printf(" [to %#lx]:\n", data_reg);
485 1.2 matt bcmpax_write_4(sc, data_reg, val);
486 1.2 matt //printf(" %#x\n", val);
487 1.2 matt }
488 1.2 matt
489 1.2 matt mutex_exit(sc->sc_cfg_lock);
490 1.2 matt }
491 1.2 matt
492 1.2 matt static void
493 1.2 matt bcmpax_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
494 1.2 matt {
495 1.2 matt *ilinep = 5; /* (ipin + swiz) & 3; */
496 1.2 matt }
497 1.2 matt
498 1.2 matt static int
499 1.2 matt bcmpax_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
500 1.2 matt {
501 1.2 matt if (func > 0)
502 1.2 matt return 0;
503 1.2 matt
504 1.4 matt return PCI_CONF_ENABLE_MEM | PCI_CONF_MAP_MEM | PCI_CONF_ENABLE_BM;
505 1.4 matt }
506 1.4 matt
507 1.4 matt static int
508 1.4 matt bcmpax_intr(void *v)
509 1.4 matt {
510 1.4 matt struct bcmpax_softc * const sc = v;
511 1.4 matt
512 1.4 matt while (bcmpax_read_4(sc, PCIE_SYS_RC_INTX_CSR)) {
513 1.4 matt struct bcmpax_intrhand *ih;
514 1.4 matt mutex_enter(sc->sc_lock);
515 1.4 matt const uint32_t lastgen = sc->sc_intrgen;
516 1.4 matt TAILQ_FOREACH(ih, &sc->sc_intrs, ih_link) {
517 1.4 matt int (* const func)(void *) = ih->ih_func;
518 1.4 matt void * const arg = ih->ih_arg;
519 1.4 matt mutex_exit(sc->sc_lock);
520 1.4 matt int rv = (*func)(arg);
521 1.4 matt if (rv) {
522 1.4 matt return rv;
523 1.4 matt }
524 1.4 matt mutex_enter(sc->sc_lock);
525 1.4 matt /*
526 1.4 matt * Check to see if the interrupt list changed.
527 1.4 matt * If so, restart from the beginning.
528 1.4 matt */
529 1.4 matt if (lastgen != sc->sc_intrgen)
530 1.4 matt break;
531 1.4 matt }
532 1.4 matt mutex_exit(sc->sc_lock);
533 1.4 matt }
534 1.4 matt
535 1.4 matt return 0;
536 1.2 matt }
537 1.2 matt
538 1.2 matt static int
539 1.2 matt bcmpax_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *pihp)
540 1.2 matt {
541 1.4 matt if (pa->pa_intrpin == 0)
542 1.4 matt return EINVAL;
543 1.4 matt
544 1.4 matt *pihp = pa->pa_intrpin;
545 1.4 matt return 0;
546 1.2 matt }
547 1.2 matt
548 1.2 matt static const char *
549 1.13 matt bcmpax_intr_string(void *v, pci_intr_handle_t pih, char *buf, size_t len)
550 1.2 matt {
551 1.4 matt struct bcmpax_softc * const sc = v;
552 1.4 matt
553 1.13 matt if (pih) {
554 1.13 matt snprintf(buf, len, "%s int%c",
555 1.13 matt device_xname(sc->sc_dev),
556 1.13 matt (char) ('a' + pih - PCI_INTERRUPT_PIN_A));
557 1.13 matt return buf;
558 1.13 matt }
559 1.4 matt
560 1.2 matt return NULL;
561 1.2 matt }
562 1.2 matt
563 1.2 matt static const struct evcnt *
564 1.2 matt bcmpax_intr_evcnt(void *v, pci_intr_handle_t pih)
565 1.2 matt {
566 1.2 matt return NULL;
567 1.2 matt }
568 1.2 matt
569 1.2 matt static void *
570 1.2 matt bcmpax_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
571 1.17 jmcneill int (*func)(void *), void *arg, const char *xname)
572 1.2 matt {
573 1.4 matt struct bcmpax_softc * const sc = v;
574 1.4 matt
575 1.4 matt KASSERT(!cpu_intr_p());
576 1.4 matt KASSERT(!cpu_softintr_p());
577 1.4 matt KASSERT(ipl == IPL_VM);
578 1.4 matt KASSERT(func != NULL);
579 1.4 matt KASSERT(arg != NULL);
580 1.4 matt
581 1.4 matt if (pih == 0)
582 1.4 matt return NULL;
583 1.4 matt
584 1.4 matt struct bcmpax_intrhand * const ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
585 1.4 matt
586 1.4 matt ih->ih_func = func;
587 1.4 matt ih->ih_arg = arg;
588 1.4 matt
589 1.4 matt mutex_enter(sc->sc_lock);
590 1.4 matt TAILQ_INSERT_TAIL(&sc->sc_intrs, ih, ih_link);
591 1.4 matt mutex_exit(sc->sc_lock);
592 1.4 matt
593 1.4 matt return ih;
594 1.2 matt }
595 1.2 matt
596 1.2 matt static void
597 1.4 matt bcmpax_intr_disestablish(void *v, void *vih)
598 1.2 matt {
599 1.4 matt struct bcmpax_softc * const sc = v;
600 1.4 matt struct bcmpax_intrhand * const ih = vih;
601 1.4 matt
602 1.4 matt mutex_enter(sc->sc_lock);
603 1.4 matt TAILQ_REMOVE(&sc->sc_intrs, ih, ih_link);
604 1.4 matt sc->sc_intrgen++;
605 1.4 matt mutex_exit(sc->sc_lock);
606 1.4 matt
607 1.4 matt kmem_free(ih, sizeof(*ih));
608 1.2 matt }
609