bcm53xx_pax.c revision 1.2 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define PCIE_PRIVATE
31
32 #include "locators.h"
33
34 #include <sys/cdefs.h>
35
36 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_pax.c,v 1.2 2012/09/14 04:53:58 matt Exp $");
37
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/extent.h>
41 #include <sys/intr.h>
42 #include <sys/systm.h>
43
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pciconf.h>
47
48 #include <arm/broadcom/bcm53xx_reg.h>
49 #include <arm/broadcom/bcm53xx_var.h>
50
51 static const struct {
52 paddr_t owin_base;
53 psize_t owin_size;
54 } bcmpax_owins[] = {
55 [0] = { BCM53XX_PCIE0_OWIN_PBASE, BCM53XX_PCIE0_OWIN_SIZE },
56 [1] = { BCM53XX_PCIE1_OWIN_PBASE, BCM53XX_PCIE1_OWIN_SIZE },
57 [2] = { BCM53XX_PCIE2_OWIN_PBASE, BCM53XX_PCIE2_OWIN_SIZE },
58 };
59
60 static int bcmpax_ccb_match(device_t, cfdata_t, void *);
61 static void bcmpax_ccb_attach(device_t, device_t, void *);
62
63 struct bcmpax_softc {
64 device_t sc_dev;
65 bus_space_tag_t sc_bst;
66 bus_space_handle_t sc_bsh;
67 bus_dma_tag_t sc_dmat;
68 kmutex_t *sc_lock;
69 kmutex_t *sc_cfg_lock;
70 bool sc_linkup;
71 int sc_pba_flags;
72 struct arm32_pci_chipset sc_pc;
73 };
74
75 static inline uint32_t
76 bcmpax_read_4(struct bcmpax_softc *sc, bus_size_t o)
77 {
78 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
79 }
80
81 static inline void
82 bcmpax_write_4(struct bcmpax_softc *sc, bus_size_t o, uint32_t v)
83 {
84 bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
85 }
86
87 static void bcmpax_attach_hook(device_t, device_t, struct pcibus_attach_args *);
88 static int bcmpax_bus_maxdevs(void *, int);
89 static pcitag_t bcmpax_make_tag(void *, int, int, int);
90 static void bcmpax_decompose_tag(void *, pcitag_t, int *, int *, int *);
91 static pcireg_t bcmpax_conf_read(void *, pcitag_t, int);
92 static void bcmpax_conf_write(void *, pcitag_t, int, pcireg_t);
93
94 static int bcmpax_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
95 static const char *bcmpax_intr_string(void *, pci_intr_handle_t);
96 static const struct evcnt *bcmpax_intr_evcnt(void *, pci_intr_handle_t);
97 static void *bcmpax_intr_establish(void *, pci_intr_handle_t, int,
98 int (*)(void *), void *);
99 static void bcmpax_intr_disestablish(void *, void *);
100
101 #ifdef __HAVE_PCI_CONF_HOOK
102 static int bcmpax_conf_hook(void *, int, int, int, pcireg_t);
103 #endif
104 static void bcmpax_conf_interrupt(void *, int, int, int, int, int *);
105
106
107 CFATTACH_DECL_NEW(bcmpax_ccb, sizeof(struct bcmpax_softc),
108 bcmpax_ccb_match, bcmpax_ccb_attach, NULL, NULL);
109
110 static int
111 bcmpax_ccb_match(device_t parent, cfdata_t cf, void *aux)
112 {
113 struct bcmccb_attach_args * const ccbaa = aux;
114 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
115
116 if (strcmp(cf->cf_name, loc->loc_name))
117 return 0;
118
119 #ifdef DIAGNOSTIC
120 const int port = cf->cf_loc[BCMCCBCF_PORT];
121 #endif
122 KASSERT(port == BCMCCBCF_PORT_DEFAULT || port == loc->loc_port);
123
124 return 1;
125 }
126
127 static void
128 bcmpax_ccb_attach(device_t parent, device_t self, void *aux)
129 {
130 struct bcmpax_softc * const sc = device_private(self);
131 struct bcmccb_attach_args * const ccbaa = aux;
132 const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
133
134 sc->sc_dev = self;
135
136 sc->sc_bst = ccbaa->ccbaa_ccb_bst;
137 sc->sc_dmat = ccbaa->ccbaa_dmat;
138
139 bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
140 loc->loc_offset, loc->loc_size, &sc->sc_bsh);
141
142 bcmpax_write_4(sc, PCIE_CLK_CONTROL, 3);
143 delay(250);
144 bcmpax_write_4(sc, PCIE_CLK_CONTROL, 1);
145 // delay(100*1000);
146
147 uint32_t v = bcmpax_read_4(sc, PCIE_STRAP_STATUS);
148 const bool enabled = (v & STRAP_PCIE_IF_ENABLE) != 0;
149 const bool is_v2_p = (v & STRAP_PCIE_USER_FOR_CE_GEN1) == 0;
150 const bool is_x2_p = (v & STRAP_PCIE_USER_FOR_CE_1LANE) == 0;
151 const bool is_rc_p = (v & STRAP_PCIE_USER_RC_MODE) != 0;
152
153 aprint_naive("\n");
154 aprint_normal(": PCI Express V%u %u-lane %s Controller%s\n",
155 is_v2_p ? 2 : 1,
156 is_x2_p ? 2 : 1,
157 is_rc_p ? "RC" : "EP",
158 enabled ? "" : "(disabled)");
159 if (!enabled || !is_rc_p)
160 return;
161
162 sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
163 sc->sc_cfg_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
164
165 sc->sc_pc.pc_conf_v = sc;
166 sc->sc_pc.pc_attach_hook = bcmpax_attach_hook;
167 sc->sc_pc.pc_bus_maxdevs = bcmpax_bus_maxdevs;
168 sc->sc_pc.pc_make_tag = bcmpax_make_tag;
169 sc->sc_pc.pc_decompose_tag = bcmpax_decompose_tag;
170 sc->sc_pc.pc_conf_read = bcmpax_conf_read;
171 sc->sc_pc.pc_conf_write = bcmpax_conf_write;
172
173 sc->sc_pc.pc_intr_v = sc;
174 sc->sc_pc.pc_intr_map = bcmpax_intr_map;
175 sc->sc_pc.pc_intr_string = bcmpax_intr_string;
176 sc->sc_pc.pc_intr_evcnt = bcmpax_intr_evcnt;
177 sc->sc_pc.pc_intr_establish = bcmpax_intr_establish;
178 sc->sc_pc.pc_intr_disestablish = bcmpax_intr_disestablish;
179
180 #ifdef __HAVE_PCI_CONF_HOOK
181 sc->sc_pc.pc_conf_hook = bcmpax_conf_hook;
182 #endif
183 sc->sc_pc.pc_conf_interrupt = bcmpax_conf_interrupt;
184
185 // sc->sc_pba_flags |= PCI_FLAGS_MSI_OKAY;
186 // sc->sc_pba_flags |= PCI_FLAGS_MSIX_OKAY;
187
188 int offset;
189 const bool ok = pci_get_capability(&sc->sc_pc, 0, PCI_CAP_PCIEXPRESS,
190 &offset, NULL);
191 KASSERT(ok);
192
193 /*
194 * Now we wait (.1sec) for the link to come up.
195 */
196 offset += PCI_PCIE_LCSR;
197 for (size_t timo = 0;; timo++) {
198 const pcireg_t lcsr = bcmpax_conf_read(sc, 0, offset);
199 sc->sc_linkup = __SHIFTOUT(lcsr, PCI_PCIE_LCSR_NLW) != 0
200 && (1 || (lcsr & PCI_PCIE_LCSR_DLACTIVE) != 0);
201 if (sc->sc_linkup || timo == 250) {
202 aprint_debug_dev(self,
203 "lcsr=%#x nlw=%jd linkup=%d, timo=%zu\n",
204 lcsr, __SHIFTOUT(lcsr, PCI_PCIE_LCSR_NLW),
205 sc->sc_linkup, timo);
206 break;
207 }
208 DELAY(1000);
209 }
210
211 if (sc->sc_linkup) {
212 paddr_t base = bcmpax_owins[loc->loc_port].owin_base;
213 psize_t size = bcmpax_owins[loc->loc_port].owin_size;
214 KASSERT((size & ~PCIE_OARR_ADDR) == 0);
215 if (size > 0) {
216 bcmpax_write_4(sc, PCIE_OARR_0, base);
217 bcmpax_write_4(sc, PCIE_OMAP_0_LOWER, base | 1);
218 }
219 if (size > __LOWEST_SET_BIT(PCIE_OARR_ADDR)) {
220 paddr_t base1 = base + __LOWEST_SET_BIT(PCIE_OARR_ADDR);
221 bcmpax_write_4(sc, PCIE_OARR_1, base1);
222 bcmpax_write_4(sc, PCIE_OMAP_1_LOWER, base1 | 1);
223 }
224
225 struct extent *memext = extent_create("pcimem", base,
226 base + size, NULL, 0, EX_NOWAIT);
227
228 int error = pci_configure_bus(&sc->sc_pc,
229 NULL, memext, NULL, 0, arm_pcache.dcache_line_size);
230
231 extent_destroy(memext);
232
233 if (error) {
234 aprint_normal_dev(self, "configuration failed\n");
235 return;
236 }
237 }
238
239 struct pcibus_attach_args pba;
240 memset(&pba, 0, sizeof(pba));
241
242 pba.pba_flags = sc->sc_pba_flags;
243 pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
244 pba.pba_memt = sc->sc_bst;
245 pba.pba_dmat = sc->sc_dmat;
246 pba.pba_pc = &sc->sc_pc;
247 pba.pba_bus = 0;
248
249 config_found_ia(self, "pcibus", &pba, pcibusprint);
250 }
251
252 static void
253 bcmpax_attach_hook(device_t parent, device_t self,
254 struct pcibus_attach_args *pba)
255 {
256 }
257
258 static int
259 bcmpax_bus_maxdevs(void *v, int bus)
260 {
261 struct bcmpax_softc * const sc = v;
262
263 if (__predict_true(sc->sc_linkup))
264 return bus > 1 ? 32 : 1;
265
266 return bus ? 0 : 1;
267 }
268
269 static void
270 bcmpax_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
271 {
272 if (busp)
273 *busp = __SHIFTOUT(tag, CFG_ADDR_BUS);
274 if (devp)
275 *devp = __SHIFTOUT(tag, CFG_ADDR_DEV);
276 if (funcp)
277 *funcp = __SHIFTOUT(tag, CFG_ADDR_FUNC);
278 }
279
280 static pcitag_t
281 bcmpax_make_tag(void *v, int bus, int dev, int func)
282 {
283 return __SHIFTIN(bus, CFG_ADDR_BUS)
284 | __SHIFTIN(dev, CFG_ADDR_DEV)
285 | __SHIFTIN(func, CFG_ADDR_FUNC)
286 | (bus == 0 ? CFG_ADDR_TYPE0 : CFG_ADDR_TYPE1);
287 }
288
289 static inline bus_size_t
290 bcmpax_conf_addr_write(struct bcmpax_softc *sc, pcitag_t tag)
291 {
292 if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV)) == 0) {
293 uint32_t reg = __SHIFTOUT(tag, CFG_ADDR_REG);
294 uint32_t func = __SHIFTOUT(tag, CFG_ADDR_FUNC);
295 bcmpax_write_4(sc, PCIE_CFG_IND_ADDR,
296 __SHIFTIN(func, CFG_IND_ADDR_FUNC)
297 | __SHIFTIN(reg, CFG_IND_ADDR_REG));
298 __asm __volatile("dsb");
299 return PCIE_CFG_IND_DATA;
300 }
301 if (sc->sc_linkup) {
302 bcmpax_write_4(sc, PCIE_CFG_ADDR, tag);
303 __asm __volatile("dsb");
304 return PCIE_CFG_DATA;
305 }
306 return 0;
307 }
308
309 static pcireg_t
310 bcmpax_conf_read(void *v, pcitag_t tag, int reg)
311 {
312 struct bcmpax_softc * const sc = v;
313
314 /*
315 * Even in RC mode, the PCI Express Root Complex return itself
316 * as BCM Ethernet Controller!. We could change ppb.c to match it
317 * but we'll just lie and say we are a PPB bridge.
318 */
319 if ((tag & (CFG_ADDR_BUS|CFG_ADDR_DEV|CFG_ADDR_FUNC)) == 0
320 && reg == PCI_CLASS_REG) {
321 return PCI_CLASS_CODE(PCI_CLASS_BRIDGE,
322 PCI_SUBCLASS_BRIDGE_PCI, 0);
323 }
324
325 //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
326
327 mutex_enter(sc->sc_cfg_lock);
328 bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
329
330 //printf(" [from %#lx]:\n", data_reg);
331
332 pcireg_t rv;
333 if (data_reg)
334 rv = bcmpax_read_4(sc, data_reg);
335 else
336 rv = 0xffffffff;
337
338 mutex_exit(sc->sc_cfg_lock);
339
340 //printf(" %#x\n", rv);
341
342 return rv;
343 }
344
345 static void
346 bcmpax_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
347 {
348 struct bcmpax_softc * const sc = v;
349
350 mutex_enter(sc->sc_cfg_lock);
351 bus_size_t data_reg = bcmpax_conf_addr_write(sc, tag | reg);
352
353 //printf("%s: tag %#lx reg %#x:", __func__, tag, reg);
354
355 if (data_reg) {
356 //printf(" [to %#lx]:\n", data_reg);
357 bcmpax_write_4(sc, data_reg, val);
358 //printf(" %#x\n", val);
359 }
360
361 mutex_exit(sc->sc_cfg_lock);
362 }
363
364 static void
365 bcmpax_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *ilinep)
366 {
367 *ilinep = 5; /* (ipin + swiz) & 3; */
368 }
369
370 static int
371 bcmpax_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
372 {
373 if (func > 0)
374 return 0;
375
376 return PCI_CONF_ENABLE_MEM | PCI_CONF_MAP_MEM;
377 }
378
379 static int
380 bcmpax_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *pihp)
381 {
382 return EINVAL;
383 }
384
385 static const char *
386 bcmpax_intr_string(void *v, pci_intr_handle_t pih)
387 {
388 return NULL;
389 }
390
391 static const struct evcnt *
392 bcmpax_intr_evcnt(void *v, pci_intr_handle_t pih)
393 {
394 return NULL;
395 }
396
397 static void *
398 bcmpax_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
399 int (*func)(void *), void *arg)
400 {
401 return NULL;
402 }
403
404 static void
405 bcmpax_intr_disestablish(void *v, void *ih)
406 {
407 }
408