tegra_pcie.c revision 1.28 1 /* $NetBSD: tegra_pcie.c,v 1.28 2020/01/07 09:57:11 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tegra_pcie.c,v 1.28 2020/01/07 09:57:11 skrll Exp $");
31
32 #include <sys/param.h>
33
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/extent.h>
37 #include <sys/intr.h>
38 #include <sys/kmem.h>
39 #include <sys/kernel.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43
44 #include <machine/cpu.h>
45
46 #include <arm/cpufunc.h>
47
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pciconf.h>
51
52 #include <arm/nvidia/tegra_reg.h>
53 #include <arm/nvidia/tegra_pciereg.h>
54 #include <arm/nvidia/tegra_pmcreg.h>
55 #include <arm/nvidia/tegra_var.h>
56
57 #include <dev/fdt/fdtvar.h>
58
59 /* Interrupt handle flags */
60 #define IH_MPSAFE 0x80000000
61
62 static int tegra_pcie_match(device_t, cfdata_t, void *);
63 static void tegra_pcie_attach(device_t, device_t, void *);
64
65 #define TEGRA_PCIE_NBUS 256
66 #define TEGRA_PCIE_ECFB (1<<(12 - 8)) /* extended conf frags per bus */
67
68 struct tegra_pcie_ih {
69 int (*ih_callback)(void *);
70 void *ih_arg;
71 int ih_ipl;
72 int ih_mpsafe;
73 TAILQ_ENTRY(tegra_pcie_ih) ih_entry;
74 };
75
76 struct tegra_pcie_softc {
77 device_t sc_dev;
78 bus_dma_tag_t sc_dmat;
79 bus_space_tag_t sc_bst;
80 bus_space_handle_t sc_bsh_afi;
81 bus_space_handle_t sc_bsh_pads;
82 bus_space_handle_t sc_bsh_rpconf;
83 int sc_phandle;
84
85 struct arm32_pci_chipset sc_pc;
86
87 void *sc_ih;
88
89 kmutex_t sc_lock;
90
91 TAILQ_HEAD(, tegra_pcie_ih) sc_intrs;
92 u_int sc_intrgen;
93
94 bus_space_handle_t sc_bsh_extc[TEGRA_PCIE_NBUS-1][TEGRA_PCIE_ECFB];
95 };
96
97 static int tegra_pcie_intr(void *);
98 static void tegra_pcie_init(pci_chipset_tag_t, void *);
99 static void tegra_pcie_enable(struct tegra_pcie_softc *);
100 static void tegra_pcie_enable_ports(struct tegra_pcie_softc *);
101 static void tegra_pcie_enable_clocks(struct tegra_pcie_softc *);
102 static void tegra_pcie_setup(struct tegra_pcie_softc * const);
103 static void tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const,
104 uint, uint);
105 static void tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const, uint);
106 static void tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const);
107
108 static void tegra_pcie_attach_hook(device_t, device_t,
109 struct pcibus_attach_args *);
110 static int tegra_pcie_bus_maxdevs(void *, int);
111 static pcitag_t tegra_pcie_make_tag(void *, int, int, int);
112 static void tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
113 static pcireg_t tegra_pcie_conf_read(void *, pcitag_t, int);
114 static void tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
115 static int tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
116 static void tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
117
118 static int tegra_pcie_intr_map(const struct pci_attach_args *,
119 pci_intr_handle_t *);
120 static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
121 char *, size_t);
122 const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
123 static int tegra_pcie_intr_setattr(void *, pci_intr_handle_t *, int,
124 uint64_t);
125 static void * tegra_pcie_intr_establish(void *, pci_intr_handle_t,
126 int, int (*)(void *), void *,
127 const char *);
128 static void tegra_pcie_intr_disestablish(void *, void *);
129
130 CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
131 tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
132
133 static int
134 tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
135 {
136 const char * const compatible[] = {
137 "nvidia,tegra210-pcie",
138 "nvidia,tegra124-pcie",
139 NULL
140 };
141 struct fdt_attach_args * const faa = aux;
142
143 return of_match_compatible(faa->faa_phandle, compatible);
144 }
145
146 static void
147 tegra_pcie_attach(device_t parent, device_t self, void *aux)
148 {
149 struct tegra_pcie_softc * const sc = device_private(self);
150 struct fdt_attach_args * const faa = aux;
151 struct extent *ioext, *memext, *pmemext;
152 struct pcibus_attach_args pba;
153 bus_addr_t afi_addr, cs_addr, pads_addr;
154 bus_size_t afi_size, cs_size, pads_size;
155 char intrstr[128];
156 int error;
157
158 if (fdtbus_get_reg_byname(faa->faa_phandle, "afi", &afi_addr, &afi_size) != 0) {
159 aprint_error(": couldn't get afi registers\n");
160 return;
161 }
162 if (fdtbus_get_reg_byname(faa->faa_phandle, "pads", &pads_addr, &pads_size) != 0) {
163 aprint_error(": couldn't get pads registers\n");
164 return;
165 }
166 #if notyet
167 if (fdtbus_get_reg(faa->faa_phandle, 2, &cs_addr, &cs_size) != 0) {
168 aprint_error(": couldn't get cs registers\n");
169 return;
170 }
171 #else
172 cs_addr = TEGRA_PCIE_RPCONF_BASE;
173 cs_size = TEGRA_PCIE_RPCONF_SIZE;
174 #endif
175
176 sc->sc_dev = self;
177 sc->sc_dmat = faa->faa_dmat;
178 sc->sc_bst = faa->faa_bst;
179 sc->sc_phandle = faa->faa_phandle;
180 error = bus_space_map(sc->sc_bst, afi_addr, afi_size, 0,
181 &sc->sc_bsh_afi);
182 if (error) {
183 aprint_error(": couldn't map afi registers: %d\n", error);
184 return;
185 }
186 error = bus_space_map(sc->sc_bst, pads_addr, pads_size, 0,
187 &sc->sc_bsh_pads);
188 if (error) {
189 aprint_error(": couldn't map pads registers: %d\n", error);
190 return;
191 }
192 error = bus_space_map(sc->sc_bst, cs_addr, cs_size,
193 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &sc->sc_bsh_rpconf);
194 if (error) {
195 aprint_error(": couldn't map cs registers: %d\n", error);
196 return;
197 }
198
199 tegra_pcie_conf_map_buses(sc);
200
201 TAILQ_INIT(&sc->sc_intrs);
202 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
203
204 aprint_naive("\n");
205 aprint_normal(": PCIE\n");
206
207 tegra_pmc_power(PMC_PARTID_PCX, true);
208 tegra_pmc_remove_clamping(PMC_PARTID_PCX);
209
210 tegra_pcie_enable_clocks(sc);
211
212 if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
213 aprint_error_dev(self, "failed to decode interrupt\n");
214 return;
215 }
216
217 sc->sc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_VM,
218 FDT_INTR_MPSAFE, tegra_pcie_intr, sc);
219 if (sc->sc_ih == NULL) {
220 aprint_error_dev(self, "failed to establish interrupt on %s\n",
221 intrstr);
222 return;
223 }
224 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
225
226 tegra_pcie_setup(sc);
227
228 tegra_pcie_init(&sc->sc_pc, sc);
229
230 ioext = extent_create("pciio", TEGRA_PCIE_IO_BASE,
231 TEGRA_PCIE_IO_BASE + TEGRA_PCIE_IO_SIZE - 1,
232 NULL, 0, EX_NOWAIT);
233 memext = extent_create("pcimem", TEGRA_PCIE_MEM_BASE,
234 TEGRA_PCIE_MEM_BASE + TEGRA_PCIE_MEM_SIZE - 1,
235 NULL, 0, EX_NOWAIT);
236 pmemext = extent_create("pcipmem", TEGRA_PCIE_PMEM_BASE,
237 TEGRA_PCIE_PMEM_BASE + TEGRA_PCIE_PMEM_SIZE - 1,
238 NULL, 0, EX_NOWAIT);
239
240 error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, 0,
241 arm_dcache_align);
242
243 extent_destroy(ioext);
244 extent_destroy(memext);
245 extent_destroy(pmemext);
246
247 if (error) {
248 aprint_error_dev(self, "configuration failed (%d)\n",
249 error);
250 return;
251 }
252
253 tegra_pcie_enable(sc);
254
255 tegra_pcie_enable_ports(sc);
256
257 memset(&pba, 0, sizeof(pba));
258 pba.pba_flags = PCI_FLAGS_MRL_OKAY |
259 PCI_FLAGS_MRM_OKAY |
260 PCI_FLAGS_MWI_OKAY |
261 PCI_FLAGS_MEM_OKAY |
262 PCI_FLAGS_IO_OKAY;
263 pba.pba_iot = sc->sc_bst;
264 pba.pba_memt = sc->sc_bst;
265 pba.pba_dmat = sc->sc_dmat;
266 pba.pba_pc = &sc->sc_pc;
267 pba.pba_bus = 0;
268
269 config_found_ia(self, "pcibus", &pba, pcibusprint);
270 }
271
272 static int
273 tegra_pcie_legacy_intr(struct tegra_pcie_softc *sc)
274 {
275 const uint32_t msg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
276 AFI_MSG_REG);
277 struct tegra_pcie_ih *pcie_ih;
278 int rv = 0;
279
280 if (msg & (AFI_MSG_INT0|AFI_MSG_INT1)) {
281 mutex_enter(&sc->sc_lock);
282 const u_int lastgen = sc->sc_intrgen;
283 TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
284 int (*callback)(void *) = pcie_ih->ih_callback;
285 void *arg = pcie_ih->ih_arg;
286 const int mpsafe = pcie_ih->ih_mpsafe;
287 mutex_exit(&sc->sc_lock);
288
289 if (!mpsafe)
290 KERNEL_LOCK(1, curlwp);
291 rv += callback(arg);
292 if (!mpsafe)
293 KERNEL_UNLOCK_ONE(curlwp);
294
295 mutex_enter(&sc->sc_lock);
296 if (lastgen != sc->sc_intrgen)
297 break;
298 }
299 mutex_exit(&sc->sc_lock);
300 } else if (msg & (AFI_MSG_PM_PME0|AFI_MSG_PM_PME1)) {
301 device_printf(sc->sc_dev, "PM PME message; AFI_MSG=%08x\n",
302 msg);
303 } else {
304 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_MSG_REG, msg);
305 rv = 1;
306 }
307
308 return rv;
309 }
310
311 static int
312 tegra_pcie_intr(void *priv)
313 {
314 struct tegra_pcie_softc *sc = priv;
315 int rv;
316
317 const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
318 AFI_INTR_CODE_REG);
319 const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
320 AFI_INTR_SIGNATURE_REG);
321
322 switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
323 case AFI_INTR_CODE_SM_MSG:
324 rv = tegra_pcie_legacy_intr(sc);
325 break;
326 default:
327 device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
328 code, sig);
329 rv = 1;
330 break;
331 }
332
333 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
334
335 return rv;
336 }
337
338 static void
339 tegra_pcie_enable_clocks(struct tegra_pcie_softc * const sc)
340 {
341 const char *clock_names[] = { "pex", "afi", "pll_e", "cml" };
342 const char *reset_names[] = { "pex", "afi", "pcie_x" };
343 struct fdtbus_reset *rst;
344 struct clk *clk;
345 int n;
346
347 for (n = 0; n < __arraycount(clock_names); n++) {
348 clk = fdtbus_clock_get(sc->sc_phandle, clock_names[n]);
349 if (clk == NULL || clk_enable(clk) != 0)
350 aprint_error_dev(sc->sc_dev, "couldn't enable clock %s\n",
351 clock_names[n]);
352 }
353
354 for (n = 0; n < __arraycount(reset_names); n++) {
355 rst = fdtbus_reset_get(sc->sc_phandle, reset_names[n]);
356 if (rst == NULL || fdtbus_reset_deassert(rst) != 0)
357 aprint_error_dev(sc->sc_dev, "couldn't de-assert reset %s\n",
358 reset_names[n]);
359 }
360 }
361
362 #if 0
363 static void
364 tegra_pcie_reset_port(struct tegra_pcie_softc * const sc, int index)
365 {
366 uint32_t val;
367
368 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
369 val &= ~AFI_PEXn_CTRL_RST_L;
370 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
371
372 delay(2000);
373
374 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
375 val |= AFI_PEXn_CTRL_RST_L;
376 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
377 }
378 #endif
379
380 static void
381 tegra_pcie_enable_ports(struct tegra_pcie_softc * const sc)
382 {
383 struct fdtbus_phy *phy;
384 const u_int *data;
385 int child, len, n;
386 uint32_t val;
387
388 for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
389 if (!fdtbus_status_okay(child))
390 continue;
391
392 /* Enable PHYs */
393 for (n = 0; (phy = fdtbus_phy_get_index(child, n)) != NULL; n++)
394 if (fdtbus_phy_enable(phy, true) != 0)
395 aprint_error_dev(sc->sc_dev, "couldn't enable %s phy #%d\n",
396 fdtbus_get_string(child, "name"), n);
397
398 data = fdtbus_get_prop(child, "reg", &len);
399 if (data == NULL || len < 4)
400 continue;
401 const u_int index = ((be32toh(data[0]) >> 11) & 0x1f) - 1;
402
403 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index));
404 val |= AFI_PEXn_CTRL_CLKREQ_EN;
405 val |= AFI_PEXn_CTRL_REFCLK_EN;
406 val |= AFI_PEXn_CTRL_REFCLK_OVERRIDE_EN;
407 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXn_CTRL_REG(index), val);
408
409 #if 0
410 tegra_pcie_reset_port(sc, index);
411 #endif
412
413 }
414 }
415
416 static void
417 tegra_pcie_setup(struct tegra_pcie_softc * const sc)
418 {
419 uint32_t val, cfg, lanes;
420 int child, len;
421 const u_int *data;
422 size_t i;
423
424 /* Enable PLLE control */
425 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PLLE_CONTROL_REG);
426 val &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
427 val |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
428 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PLLE_CONTROL_REG, val);
429
430 /* Disable PEX clock bias pad power down */
431 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PEXBIAS_CTRL_REG, 0);
432
433 /* Configure PCIE mode and enable ports */
434 cfg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PCIE_CONFIG_REG);
435 cfg |= AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(0);
436 cfg |= AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(1);
437 cfg &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG;
438
439 lanes = 0;
440 for (child = OF_child(sc->sc_phandle); child; child = OF_peer(child)) {
441 if (!fdtbus_status_okay(child))
442 continue;
443 data = fdtbus_get_prop(child, "reg", &len);
444 if (data == NULL || len < 4)
445 continue;
446 const u_int index = ((be32toh(data[0]) >> 11) & 0x1f) - 1;
447 if (of_getprop_uint32(child, "nvidia,num-lanes", &val) != 0)
448 continue;
449 lanes |= (val << (index << 3));
450 cfg &= ~AFI_PCIE_CONFIG_PCIECn_DISABLE_DEVICE(index);
451 }
452
453 switch (lanes) {
454 case 0x0104:
455 aprint_normal_dev(sc->sc_dev, "lane config: x4 x1\n");
456 cfg |= __SHIFTIN(AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_4_1,
457 AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG);
458 break;
459 case 0x0102:
460 aprint_normal_dev(sc->sc_dev, "lane config: x2 x1\n");
461 cfg |= __SHIFTIN(AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_2_1,
462 AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG);
463 break;
464 }
465
466 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_PCIE_CONFIG_REG, cfg);
467
468 /* Configure refclk pad */
469 const char * const tegra124_compat[] = { "nvidia,tegra124-pcie", NULL };
470 if (of_match_compatible(sc->sc_phandle, tegra124_compat))
471 bus_space_write_4(sc->sc_bst, sc->sc_bsh_pads, PADS_REFCLK_CFG0_REG,
472 0x44ac44ac);
473 const char * const tegra210_compat[] = { "nvidia,tegra210-pcie", NULL };
474 if (of_match_compatible(sc->sc_phandle, tegra210_compat))
475 bus_space_write_4(sc->sc_bst, sc->sc_bsh_pads, PADS_REFCLK_CFG0_REG,
476 0x90b890b8);
477
478 /*
479 * Map PCI address spaces into ARM address space via
480 * HyperTransport-like "FPCI".
481 */
482 static const struct { uint32_t size, base, fpci; } pcie_init_table[] = {
483 /*
484 * === BEWARE ===
485 *
486 * We depend on our TEGRA_PCIE_IO window overlaping the
487 * TEGRA_PCIE_A1 window to allow us to use the same
488 * bus_space_tag for both PCI IO and Memory spaces.
489 *
490 * 0xfdfc000000-0xfdfdffffff is the FPCI/HyperTransport
491 * mapping for 0x0000000-0x1ffffff of PCI IO space.
492 */
493 { TEGRA_PCIE_IO_SIZE >> 12, TEGRA_PCIE_IO_BASE,
494 (0xfdfc000000 + TEGRA_PCIE_IO_BASE) >> 8 | 0, },
495
496 /* HyperTransport Technology Type 1 Address Format */
497 { TEGRA_PCIE_CONF_SIZE >> 12, TEGRA_PCIE_CONF_BASE,
498 0xfdff000000 >> 8 | 0, },
499
500 /* 1:1 MMIO mapping */
501 { TEGRA_PCIE_MEM_SIZE >> 12, TEGRA_PCIE_MEM_BASE,
502 TEGRA_PCIE_MEM_BASE >> 8 | 1, },
503
504 /* Extended HyperTransport Technology Type 1 Address Format */
505 { TEGRA_PCIE_EXTC_SIZE >> 12, TEGRA_PCIE_EXTC_BASE,
506 0xfe10000000 >> 8 | 0, },
507
508 /* 1:1 prefetchable MMIO mapping */
509 { TEGRA_PCIE_PMEM_SIZE >> 12, TEGRA_PCIE_PMEM_BASE,
510 TEGRA_PCIE_PMEM_BASE >> 8 | 1, },
511 };
512
513 for (i = 0; i < AFI_AXI_NBAR; i++) {
514 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
515 AFI_AXI_BARi_SZ(i), 0);
516 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
517 AFI_AXI_BARi_START(i), 0);
518 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
519 AFI_FPCI_BARi(i), 0);
520 }
521
522 for (i = 0; i < __arraycount(pcie_init_table); i++) {
523 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
524 AFI_AXI_BARi_START(i), pcie_init_table[i].base);
525 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
526 AFI_FPCI_BARi(i), pcie_init_table[i].fpci);
527 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
528 AFI_AXI_BARi_SZ(i), pcie_init_table[i].size);
529 }
530 }
531
532 static void
533 tegra_pcie_enable(struct tegra_pcie_softc *sc)
534 {
535 /* disable MSI */
536 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
537 AFI_MSI_BAR_SZ_REG, 0);
538 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
539 AFI_MSI_FPCI_BAR_ST_REG, 0);
540 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
541 AFI_MSI_AXI_BAR_ST_REG, 0);
542
543 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
544 AFI_SM_INTR_ENABLE_REG, 0xffffffff);
545 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
546 AFI_AFI_INTR_ENABLE_REG, 0);
547 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
548 bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
549 AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
550 }
551
552 static void
553 tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const sc, uint bus,
554 uint frg)
555 {
556 bus_addr_t a;
557
558 KASSERT(bus >= 1);
559 KASSERT(bus < TEGRA_PCIE_NBUS);
560 KASSERT(frg < TEGRA_PCIE_ECFB);
561
562 if (sc->sc_bsh_extc[bus-1][frg] != 0) {
563 device_printf(sc->sc_dev, "bus %u fragment %#x already "
564 "mapped\n", bus, frg);
565 return;
566 }
567
568 a = TEGRA_PCIE_EXTC_BASE + (bus << 16) + (frg << 24);
569 if (bus_space_map(sc->sc_bst, a, 1 << 16,
570 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED,
571 &sc->sc_bsh_extc[bus-1][frg]) != 0)
572 device_printf(sc->sc_dev, "couldn't map PCIE "
573 "configuration for bus %u fragment %#x", bus, frg);
574 }
575
576 /* map non-non-extended configuration space for full bus range */
577 static void
578 tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const sc, uint bus)
579 {
580 uint i;
581
582 for (i = 1; i < TEGRA_PCIE_ECFB; i++) {
583 tegra_pcie_conf_frag_map(sc, bus, i);
584 }
585 }
586
587 /* map non-extended configuration space for full bus range */
588 static void
589 tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const sc)
590 {
591 uint b;
592
593 for (b = 1; b < TEGRA_PCIE_NBUS; b++) {
594 tegra_pcie_conf_frag_map(sc, b, 0);
595 }
596 }
597
598 void
599 tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
600 {
601 pc->pc_conf_v = priv;
602 pc->pc_attach_hook = tegra_pcie_attach_hook;
603 pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
604 pc->pc_make_tag = tegra_pcie_make_tag;
605 pc->pc_decompose_tag = tegra_pcie_decompose_tag;
606 pc->pc_conf_read = tegra_pcie_conf_read;
607 pc->pc_conf_write = tegra_pcie_conf_write;
608 pc->pc_conf_hook = tegra_pcie_conf_hook;
609 pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
610
611 pc->pc_intr_v = priv;
612 pc->pc_intr_map = tegra_pcie_intr_map;
613 pc->pc_intr_string = tegra_pcie_intr_string;
614 pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
615 pc->pc_intr_setattr = tegra_pcie_intr_setattr;
616 pc->pc_intr_establish = tegra_pcie_intr_establish;
617 pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
618 }
619
620 static void
621 tegra_pcie_attach_hook(device_t parent, device_t self,
622 struct pcibus_attach_args *pba)
623 {
624 const pci_chipset_tag_t pc = pba->pba_pc;
625 struct tegra_pcie_softc * const sc = pc->pc_conf_v;
626
627 if (pba->pba_bus >= 1) {
628 tegra_pcie_conf_map_bus(sc, pba->pba_bus);
629 }
630 }
631
632 static int
633 tegra_pcie_bus_maxdevs(void *v, int busno)
634 {
635 return busno == 0 ? 2 : 32;
636 }
637
638 static pcitag_t
639 tegra_pcie_make_tag(void *v, int b, int d, int f)
640 {
641 return (b << 16) | (d << 11) | (f << 8);
642 }
643
644 static void
645 tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
646 {
647 if (bp)
648 *bp = (tag >> 16) & 0xff;
649 if (dp)
650 *dp = (tag >> 11) & 0x1f;
651 if (fp)
652 *fp = (tag >> 8) & 0x7;
653 }
654
655 static pcireg_t
656 tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
657 {
658 struct tegra_pcie_softc *sc = v;
659 bus_space_handle_t bsh;
660 int b, d, f;
661 u_int reg;
662
663 if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
664 return (pcireg_t) -1;
665
666 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
667
668 if (b >= TEGRA_PCIE_NBUS)
669 return (pcireg_t) -1;
670
671 if (b == 0) {
672 if (d >= 2 || f != 0)
673 return (pcireg_t) -1;
674 reg = d * 0x1000 + offset;
675 bsh = sc->sc_bsh_rpconf;
676 } else {
677 reg = (d << 11) | (f << 8) | (offset & 0xff);
678 bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
679 if (bsh == 0)
680 return (pcireg_t) -1;
681 }
682
683 return bus_space_read_4(sc->sc_bst, bsh, reg);
684 }
685
686 static void
687 tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
688 {
689 struct tegra_pcie_softc *sc = v;
690 bus_space_handle_t bsh;
691 int b, d, f;
692 u_int reg;
693
694 if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
695 return;
696
697 tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
698
699 if (b >= TEGRA_PCIE_NBUS)
700 return;
701
702 if (b == 0) {
703 if (d >= 2 || f != 0)
704 return;
705 reg = d * 0x1000 + offset;
706 bsh = sc->sc_bsh_rpconf;
707 } else {
708 reg = (d << 11) | (f << 8) | (offset & 0xff);
709 bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
710 if (bsh == 0)
711 return;
712 }
713
714 bus_space_write_4(sc->sc_bst, bsh, reg, val);
715 }
716
717 static int
718 tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
719 {
720 return PCI_CONF_DEFAULT & ~PCI_CONF_ENABLE_BM;
721 }
722
723 static void
724 tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
725 int *ilinep)
726 {
727 *ilinep = 5;
728 }
729
730 static int
731 tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
732 {
733 if (pa->pa_intrpin == 0)
734 return EINVAL;
735 *ih = pa->pa_intrpin;
736 return 0;
737 }
738
739 static const char *
740 tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
741 {
742 struct tegra_pcie_softc *sc = v;
743
744 if (ih == PCI_INTERRUPT_PIN_NONE)
745 return NULL;
746
747 if (!fdtbus_intr_str(sc->sc_phandle, 0, buf, len))
748 return NULL;
749
750 return buf;
751 }
752
753 const struct evcnt *
754 tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
755 {
756 return NULL;
757 }
758
759 static int
760 tegra_pcie_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
761 {
762 switch (attr) {
763 case PCI_INTR_MPSAFE:
764 if (data)
765 *ih |= IH_MPSAFE;
766 else
767 *ih &= ~IH_MPSAFE;
768 return 0;
769 default:
770 return ENODEV;
771 }
772 }
773
774 static void *
775 tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
776 int (*callback)(void *), void *arg, const char *xname)
777 {
778 struct tegra_pcie_softc *sc = v;
779 struct tegra_pcie_ih *pcie_ih;
780
781 if (ih == 0)
782 return NULL;
783
784 pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
785 pcie_ih->ih_callback = callback;
786 pcie_ih->ih_arg = arg;
787 pcie_ih->ih_ipl = ipl;
788 pcie_ih->ih_mpsafe = (ih & IH_MPSAFE) != 0;
789
790 mutex_enter(&sc->sc_lock);
791 TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
792 sc->sc_intrgen++;
793 mutex_exit(&sc->sc_lock);
794
795 return pcie_ih;
796 }
797
798 static void
799 tegra_pcie_intr_disestablish(void *v, void *vih)
800 {
801 struct tegra_pcie_softc *sc = v;
802 struct tegra_pcie_ih *pcie_ih = vih;
803
804 mutex_enter(&sc->sc_lock);
805 TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
806 mutex_exit(&sc->sc_lock);
807
808 kmem_free(pcie_ih, sizeof(*pcie_ih));
809 }
810