tegra_pcie.c revision 1.12 1 1.12 jakllsch /* $NetBSD: tegra_pcie.c,v 1.12 2015/11/17 22:01:39 jakllsch Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "locators.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.12 jakllsch __KERNEL_RCSID(0, "$NetBSD: tegra_pcie.c,v 1.12 2015/11/17 22:01:39 jakllsch Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill #include <sys/extent.h>
41 1.1 jmcneill #include <sys/queue.h>
42 1.1 jmcneill #include <sys/mutex.h>
43 1.1 jmcneill #include <sys/kmem.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <arm/cpufunc.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/pci/pcireg.h>
48 1.1 jmcneill #include <dev/pci/pcivar.h>
49 1.1 jmcneill #include <dev/pci/pciconf.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <arm/nvidia/tegra_reg.h>
52 1.1 jmcneill #include <arm/nvidia/tegra_pciereg.h>
53 1.1 jmcneill #include <arm/nvidia/tegra_var.h>
54 1.1 jmcneill
55 1.1 jmcneill static int tegra_pcie_match(device_t, cfdata_t, void *);
56 1.1 jmcneill static void tegra_pcie_attach(device_t, device_t, void *);
57 1.1 jmcneill
58 1.12 jakllsch #define TEGRA_PCIE_NBUS 256
59 1.12 jakllsch #define TEGRA_PCIE_ECFB (1<<(12 - 8)) /* extended conf frags per bus */
60 1.12 jakllsch
61 1.1 jmcneill struct tegra_pcie_ih {
62 1.1 jmcneill int (*ih_callback)(void *);
63 1.1 jmcneill void *ih_arg;
64 1.1 jmcneill int ih_ipl;
65 1.1 jmcneill TAILQ_ENTRY(tegra_pcie_ih) ih_entry;
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill struct tegra_pcie_softc {
69 1.1 jmcneill device_t sc_dev;
70 1.1 jmcneill bus_dma_tag_t sc_dmat;
71 1.1 jmcneill bus_space_tag_t sc_bst;
72 1.1 jmcneill bus_space_handle_t sc_bsh_afi;
73 1.9 jakllsch bus_space_handle_t sc_bsh_rpconf;
74 1.1 jmcneill int sc_intr;
75 1.1 jmcneill
76 1.1 jmcneill struct arm32_pci_chipset sc_pc;
77 1.1 jmcneill
78 1.1 jmcneill void *sc_ih;
79 1.1 jmcneill
80 1.1 jmcneill kmutex_t sc_lock;
81 1.1 jmcneill
82 1.1 jmcneill TAILQ_HEAD(, tegra_pcie_ih) sc_intrs;
83 1.1 jmcneill u_int sc_intrgen;
84 1.12 jakllsch
85 1.12 jakllsch bus_space_handle_t sc_bsh_extc[TEGRA_PCIE_NBUS-1][TEGRA_PCIE_ECFB];
86 1.1 jmcneill };
87 1.1 jmcneill
88 1.1 jmcneill static int tegra_pcie_intr(void *);
89 1.1 jmcneill static void tegra_pcie_init(pci_chipset_tag_t, void *);
90 1.1 jmcneill static void tegra_pcie_enable(struct tegra_pcie_softc *);
91 1.10 jakllsch static void tegra_pcie_setup(struct tegra_pcie_softc * const);
92 1.12 jakllsch static void tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const,
93 1.12 jakllsch uint, uint);
94 1.12 jakllsch static void tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const, uint);
95 1.12 jakllsch static void tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const);
96 1.1 jmcneill
97 1.1 jmcneill static void tegra_pcie_attach_hook(device_t, device_t,
98 1.1 jmcneill struct pcibus_attach_args *);
99 1.1 jmcneill static int tegra_pcie_bus_maxdevs(void *, int);
100 1.1 jmcneill static pcitag_t tegra_pcie_make_tag(void *, int, int, int);
101 1.1 jmcneill static void tegra_pcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
102 1.1 jmcneill static pcireg_t tegra_pcie_conf_read(void *, pcitag_t, int);
103 1.1 jmcneill static void tegra_pcie_conf_write(void *, pcitag_t, int, pcireg_t);
104 1.1 jmcneill static int tegra_pcie_conf_hook(void *, int, int, int, pcireg_t);
105 1.1 jmcneill static void tegra_pcie_conf_interrupt(void *, int, int, int, int, int *);
106 1.1 jmcneill
107 1.1 jmcneill static int tegra_pcie_intr_map(const struct pci_attach_args *,
108 1.1 jmcneill pci_intr_handle_t *);
109 1.1 jmcneill static const char *tegra_pcie_intr_string(void *, pci_intr_handle_t,
110 1.1 jmcneill char *, size_t);
111 1.1 jmcneill const struct evcnt *tegra_pcie_intr_evcnt(void *, pci_intr_handle_t);
112 1.1 jmcneill static void * tegra_pcie_intr_establish(void *, pci_intr_handle_t,
113 1.1 jmcneill int, int (*)(void *), void *);
114 1.1 jmcneill static void tegra_pcie_intr_disestablish(void *, void *);
115 1.1 jmcneill
116 1.1 jmcneill CFATTACH_DECL_NEW(tegra_pcie, sizeof(struct tegra_pcie_softc),
117 1.1 jmcneill tegra_pcie_match, tegra_pcie_attach, NULL, NULL);
118 1.1 jmcneill
119 1.1 jmcneill static int
120 1.1 jmcneill tegra_pcie_match(device_t parent, cfdata_t cf, void *aux)
121 1.1 jmcneill {
122 1.1 jmcneill return 1;
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill static void
126 1.1 jmcneill tegra_pcie_attach(device_t parent, device_t self, void *aux)
127 1.1 jmcneill {
128 1.1 jmcneill struct tegra_pcie_softc * const sc = device_private(self);
129 1.1 jmcneill struct tegraio_attach_args * const tio = aux;
130 1.1 jmcneill const struct tegra_locators * const loc = &tio->tio_loc;
131 1.10 jakllsch struct extent *ioext, *memext, *pmemext;
132 1.1 jmcneill struct pcibus_attach_args pba;
133 1.1 jmcneill int error;
134 1.1 jmcneill
135 1.1 jmcneill sc->sc_dev = self;
136 1.2 jmcneill #if notyet
137 1.1 jmcneill sc->sc_dmat = tio->tio_coherent_dmat;
138 1.2 jmcneill #else
139 1.2 jmcneill sc->sc_dmat = tio->tio_dmat;
140 1.2 jmcneill #endif
141 1.1 jmcneill sc->sc_bst = tio->tio_bst;
142 1.1 jmcneill sc->sc_intr = loc->loc_intr;
143 1.1 jmcneill if (bus_space_map(sc->sc_bst, TEGRA_PCIE_AFI_BASE, TEGRA_PCIE_AFI_SIZE,
144 1.1 jmcneill 0, &sc->sc_bsh_afi) != 0)
145 1.1 jmcneill panic("couldn't map PCIE AFI");
146 1.9 jakllsch if (bus_space_map(sc->sc_bst, TEGRA_PCIE_RPCONF_BASE,
147 1.9 jakllsch TEGRA_PCIE_RPCONF_SIZE, 0, &sc->sc_bsh_rpconf) != 0)
148 1.9 jakllsch panic("couldn't map PCIE root ports");
149 1.12 jakllsch tegra_pcie_conf_map_buses(sc);
150 1.1 jmcneill
151 1.1 jmcneill TAILQ_INIT(&sc->sc_intrs);
152 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
153 1.1 jmcneill
154 1.1 jmcneill aprint_naive("\n");
155 1.1 jmcneill aprint_normal(": PCIE\n");
156 1.1 jmcneill
157 1.1 jmcneill sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL,
158 1.1 jmcneill tegra_pcie_intr, sc);
159 1.1 jmcneill if (sc->sc_ih == NULL) {
160 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt %d\n",
161 1.1 jmcneill loc->loc_intr);
162 1.1 jmcneill return;
163 1.1 jmcneill }
164 1.1 jmcneill aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
165 1.1 jmcneill
166 1.10 jakllsch tegra_pcie_setup(sc);
167 1.10 jakllsch
168 1.1 jmcneill tegra_pcie_init(&sc->sc_pc, sc);
169 1.1 jmcneill
170 1.10 jakllsch ioext = extent_create("pciio", TEGRA_PCIE_IO_BASE,
171 1.10 jakllsch TEGRA_PCIE_IO_BASE + TEGRA_PCIE_IO_SIZE - 1,
172 1.10 jakllsch NULL, 0, EX_NOWAIT);
173 1.1 jmcneill memext = extent_create("pcimem", TEGRA_PCIE_MEM_BASE,
174 1.1 jmcneill TEGRA_PCIE_MEM_BASE + TEGRA_PCIE_MEM_SIZE - 1,
175 1.1 jmcneill NULL, 0, EX_NOWAIT);
176 1.1 jmcneill pmemext = extent_create("pcipmem", TEGRA_PCIE_PMEM_BASE,
177 1.1 jmcneill TEGRA_PCIE_PMEM_BASE + TEGRA_PCIE_PMEM_SIZE - 1,
178 1.1 jmcneill NULL, 0, EX_NOWAIT);
179 1.1 jmcneill
180 1.10 jakllsch error = pci_configure_bus(&sc->sc_pc, ioext, memext, pmemext, 0,
181 1.1 jmcneill arm_dcache_align);
182 1.1 jmcneill
183 1.10 jakllsch extent_destroy(ioext);
184 1.1 jmcneill extent_destroy(memext);
185 1.1 jmcneill extent_destroy(pmemext);
186 1.1 jmcneill
187 1.1 jmcneill if (error) {
188 1.1 jmcneill aprint_error_dev(self, "configuration failed (%d)\n",
189 1.1 jmcneill error);
190 1.1 jmcneill return;
191 1.1 jmcneill }
192 1.1 jmcneill
193 1.1 jmcneill tegra_pcie_enable(sc);
194 1.1 jmcneill
195 1.1 jmcneill memset(&pba, 0, sizeof(pba));
196 1.1 jmcneill pba.pba_flags = PCI_FLAGS_MRL_OKAY |
197 1.1 jmcneill PCI_FLAGS_MRM_OKAY |
198 1.1 jmcneill PCI_FLAGS_MWI_OKAY |
199 1.10 jakllsch PCI_FLAGS_MEM_OKAY |
200 1.10 jakllsch PCI_FLAGS_IO_OKAY;
201 1.10 jakllsch pba.pba_iot = sc->sc_bst;
202 1.1 jmcneill pba.pba_memt = sc->sc_bst;
203 1.1 jmcneill pba.pba_dmat = sc->sc_dmat;
204 1.1 jmcneill pba.pba_pc = &sc->sc_pc;
205 1.1 jmcneill pba.pba_bus = 0;
206 1.1 jmcneill
207 1.1 jmcneill config_found_ia(self, "pcibus", &pba, pcibusprint);
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill static int
211 1.4 jmcneill tegra_pcie_legacy_intr(struct tegra_pcie_softc *sc)
212 1.1 jmcneill {
213 1.4 jmcneill const uint32_t msg = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
214 1.4 jmcneill AFI_MSG_REG);
215 1.1 jmcneill struct tegra_pcie_ih *pcie_ih;
216 1.4 jmcneill int rv = 0;
217 1.1 jmcneill
218 1.4 jmcneill if (msg & (AFI_MSG_INT0|AFI_MSG_INT1)) {
219 1.1 jmcneill mutex_enter(&sc->sc_lock);
220 1.1 jmcneill const u_int lastgen = sc->sc_intrgen;
221 1.1 jmcneill TAILQ_FOREACH(pcie_ih, &sc->sc_intrs, ih_entry) {
222 1.1 jmcneill int (*callback)(void *) = pcie_ih->ih_callback;
223 1.1 jmcneill void *arg = pcie_ih->ih_arg;
224 1.1 jmcneill mutex_exit(&sc->sc_lock);
225 1.4 jmcneill rv += callback(arg);
226 1.1 jmcneill mutex_enter(&sc->sc_lock);
227 1.1 jmcneill if (lastgen != sc->sc_intrgen)
228 1.1 jmcneill break;
229 1.1 jmcneill }
230 1.1 jmcneill mutex_exit(&sc->sc_lock);
231 1.4 jmcneill } else if (msg & (AFI_MSG_PM_PME0|AFI_MSG_PM_PME1)) {
232 1.4 jmcneill device_printf(sc->sc_dev, "PM PME message; AFI_MSG=%08x\n",
233 1.4 jmcneill msg);
234 1.4 jmcneill } else {
235 1.4 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_MSG_REG, msg);
236 1.4 jmcneill rv = 1;
237 1.4 jmcneill }
238 1.4 jmcneill
239 1.4 jmcneill return rv;
240 1.4 jmcneill }
241 1.4 jmcneill
242 1.4 jmcneill static int
243 1.4 jmcneill tegra_pcie_intr(void *priv)
244 1.4 jmcneill {
245 1.4 jmcneill struct tegra_pcie_softc *sc = priv;
246 1.11 jakllsch int rv;
247 1.4 jmcneill
248 1.4 jmcneill const uint32_t code = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
249 1.4 jmcneill AFI_INTR_CODE_REG);
250 1.4 jmcneill const uint32_t sig = bus_space_read_4(sc->sc_bst, sc->sc_bsh_afi,
251 1.4 jmcneill AFI_INTR_SIGNATURE_REG);
252 1.4 jmcneill
253 1.4 jmcneill switch (__SHIFTOUT(code, AFI_INTR_CODE_INT_CODE)) {
254 1.4 jmcneill case AFI_INTR_CODE_SM_MSG:
255 1.11 jakllsch rv = tegra_pcie_legacy_intr(sc);
256 1.11 jakllsch break;
257 1.1 jmcneill default:
258 1.1 jmcneill device_printf(sc->sc_dev, "intr: code %#x sig %#x\n",
259 1.1 jmcneill code, sig);
260 1.11 jakllsch rv = 1;
261 1.11 jakllsch break;
262 1.1 jmcneill }
263 1.11 jakllsch
264 1.11 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
265 1.11 jakllsch
266 1.11 jakllsch return rv;
267 1.1 jmcneill }
268 1.1 jmcneill
269 1.1 jmcneill static void
270 1.10 jakllsch tegra_pcie_setup(struct tegra_pcie_softc * const sc)
271 1.10 jakllsch {
272 1.10 jakllsch size_t i;
273 1.10 jakllsch
274 1.10 jakllsch /*
275 1.10 jakllsch * Map PCI address spaces into ARM address space via
276 1.10 jakllsch * HyperTransport-like "FPCI".
277 1.10 jakllsch */
278 1.10 jakllsch static const struct { uint32_t size, base, fpci; } pcie_init_table[] = {
279 1.10 jakllsch /*
280 1.10 jakllsch * === BEWARE ===
281 1.10 jakllsch *
282 1.10 jakllsch * We depend on our TEGRA_PCIE_IO window overlaping the
283 1.10 jakllsch * TEGRA_PCIE_A1 window to allow us to use the same
284 1.10 jakllsch * bus_space_tag for both PCI IO and Memory spaces.
285 1.10 jakllsch *
286 1.10 jakllsch * 0xfdfc000000-0xfdfdffffff is the FPCI/HyperTransport
287 1.10 jakllsch * mapping for 0x0000000-0x1ffffff of PCI IO space.
288 1.10 jakllsch */
289 1.10 jakllsch { TEGRA_PCIE_IO_SIZE >> 12, TEGRA_PCIE_IO_BASE,
290 1.10 jakllsch (0xfdfc000000 + TEGRA_PCIE_IO_BASE) >> 8 | 0, },
291 1.10 jakllsch
292 1.10 jakllsch /* HyperTransport Technology Type 1 Address Format */
293 1.10 jakllsch { TEGRA_PCIE_CONF_SIZE >> 12, TEGRA_PCIE_CONF_BASE,
294 1.10 jakllsch 0xfdff000000 >> 8 | 0, },
295 1.10 jakllsch
296 1.10 jakllsch /* 1:1 MMIO mapping */
297 1.10 jakllsch { TEGRA_PCIE_MEM_SIZE >> 12, TEGRA_PCIE_MEM_BASE,
298 1.10 jakllsch TEGRA_PCIE_MEM_BASE >> 8 | 1, },
299 1.10 jakllsch
300 1.10 jakllsch /* Extended HyperTransport Technology Type 1 Address Format */
301 1.10 jakllsch { TEGRA_PCIE_EXTC_SIZE >> 12, TEGRA_PCIE_EXTC_BASE,
302 1.10 jakllsch 0xfe10000000 >> 8 | 0, },
303 1.10 jakllsch
304 1.10 jakllsch /* 1:1 prefetchable MMIO mapping */
305 1.10 jakllsch { TEGRA_PCIE_PMEM_SIZE >> 12, TEGRA_PCIE_PMEM_BASE,
306 1.10 jakllsch TEGRA_PCIE_PMEM_BASE >> 8 | 1, },
307 1.10 jakllsch };
308 1.10 jakllsch
309 1.10 jakllsch for (i = 0; i < AFI_AXI_NBAR; i++) {
310 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
311 1.10 jakllsch AFI_AXI_BARi_SZ(i), 0);
312 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
313 1.10 jakllsch AFI_AXI_BARi_START(i), 0);
314 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
315 1.10 jakllsch AFI_FPCI_BARi(i), 0);
316 1.10 jakllsch }
317 1.10 jakllsch
318 1.10 jakllsch for (i = 0; i < __arraycount(pcie_init_table); i++) {
319 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
320 1.10 jakllsch AFI_AXI_BARi_START(i), pcie_init_table[i].base);
321 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
322 1.10 jakllsch AFI_FPCI_BARi(i), pcie_init_table[i].fpci);
323 1.10 jakllsch bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
324 1.10 jakllsch AFI_AXI_BARi_SZ(i), pcie_init_table[i].size);
325 1.10 jakllsch }
326 1.10 jakllsch }
327 1.10 jakllsch
328 1.10 jakllsch static void
329 1.1 jmcneill tegra_pcie_enable(struct tegra_pcie_softc *sc)
330 1.1 jmcneill {
331 1.4 jmcneill /* disable MSI */
332 1.4 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
333 1.4 jmcneill AFI_MSI_BAR_SZ_REG, 0);
334 1.4 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
335 1.4 jmcneill AFI_MSI_FPCI_BAR_ST_REG, 0);
336 1.4 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
337 1.4 jmcneill AFI_MSI_AXI_BAR_ST_REG, 0);
338 1.4 jmcneill
339 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
340 1.1 jmcneill AFI_SM_INTR_ENABLE_REG, 0xffffffff);
341 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
342 1.1 jmcneill AFI_AFI_INTR_ENABLE_REG, 0);
343 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi, AFI_INTR_CODE_REG, 0);
344 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_afi,
345 1.1 jmcneill AFI_INTR_MASK_REG, AFI_INTR_MASK_INT);
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.12 jakllsch static void
349 1.12 jakllsch tegra_pcie_conf_frag_map(struct tegra_pcie_softc * const sc, uint bus,
350 1.12 jakllsch uint frg)
351 1.12 jakllsch {
352 1.12 jakllsch bus_addr_t a;
353 1.12 jakllsch
354 1.12 jakllsch KASSERT(bus >= 1);
355 1.12 jakllsch KASSERT(bus < TEGRA_PCIE_NBUS);
356 1.12 jakllsch KASSERT(frg < TEGRA_PCIE_ECFB);
357 1.12 jakllsch
358 1.12 jakllsch if (sc->sc_bsh_extc[bus-1][frg] != 0) {
359 1.12 jakllsch device_printf(sc->sc_dev, "bus %u fragment %#x already "
360 1.12 jakllsch "mapped\n", bus, frg);
361 1.12 jakllsch return;
362 1.12 jakllsch }
363 1.12 jakllsch
364 1.12 jakllsch a = TEGRA_PCIE_EXTC_BASE + (bus << 16) + (frg << 24);
365 1.12 jakllsch if (bus_space_map(sc->sc_bst, a, 1 << 16, 0,
366 1.12 jakllsch &sc->sc_bsh_extc[bus-1][frg]) != 0)
367 1.12 jakllsch device_printf(sc->sc_dev, "couldn't map PCIE "
368 1.12 jakllsch "configuration for bus %u fragment %#x", bus, frg);
369 1.12 jakllsch }
370 1.12 jakllsch
371 1.12 jakllsch /* map non-non-extended configuration space for full bus range */
372 1.12 jakllsch static void
373 1.12 jakllsch tegra_pcie_conf_map_bus(struct tegra_pcie_softc * const sc, uint bus)
374 1.12 jakllsch {
375 1.12 jakllsch uint i;
376 1.12 jakllsch
377 1.12 jakllsch for (i = 1; i < TEGRA_PCIE_ECFB; i++) {
378 1.12 jakllsch tegra_pcie_conf_frag_map(sc, bus, i);
379 1.12 jakllsch }
380 1.12 jakllsch }
381 1.12 jakllsch
382 1.12 jakllsch /* map non-extended configuration space for full bus range */
383 1.12 jakllsch static void
384 1.12 jakllsch tegra_pcie_conf_map_buses(struct tegra_pcie_softc * const sc)
385 1.12 jakllsch {
386 1.12 jakllsch uint b;
387 1.12 jakllsch
388 1.12 jakllsch for (b = 1; b < TEGRA_PCIE_NBUS; b++) {
389 1.12 jakllsch tegra_pcie_conf_frag_map(sc, b, 0);
390 1.12 jakllsch }
391 1.12 jakllsch }
392 1.12 jakllsch
393 1.1 jmcneill void
394 1.1 jmcneill tegra_pcie_init(pci_chipset_tag_t pc, void *priv)
395 1.1 jmcneill {
396 1.1 jmcneill pc->pc_conf_v = priv;
397 1.1 jmcneill pc->pc_attach_hook = tegra_pcie_attach_hook;
398 1.1 jmcneill pc->pc_bus_maxdevs = tegra_pcie_bus_maxdevs;
399 1.1 jmcneill pc->pc_make_tag = tegra_pcie_make_tag;
400 1.1 jmcneill pc->pc_decompose_tag = tegra_pcie_decompose_tag;
401 1.1 jmcneill pc->pc_conf_read = tegra_pcie_conf_read;
402 1.1 jmcneill pc->pc_conf_write = tegra_pcie_conf_write;
403 1.1 jmcneill pc->pc_conf_hook = tegra_pcie_conf_hook;
404 1.1 jmcneill pc->pc_conf_interrupt = tegra_pcie_conf_interrupt;
405 1.1 jmcneill
406 1.1 jmcneill pc->pc_intr_v = priv;
407 1.1 jmcneill pc->pc_intr_map = tegra_pcie_intr_map;
408 1.1 jmcneill pc->pc_intr_string = tegra_pcie_intr_string;
409 1.1 jmcneill pc->pc_intr_evcnt = tegra_pcie_intr_evcnt;
410 1.1 jmcneill pc->pc_intr_establish = tegra_pcie_intr_establish;
411 1.1 jmcneill pc->pc_intr_disestablish = tegra_pcie_intr_disestablish;
412 1.1 jmcneill }
413 1.1 jmcneill
414 1.1 jmcneill static void
415 1.1 jmcneill tegra_pcie_attach_hook(device_t parent, device_t self,
416 1.1 jmcneill struct pcibus_attach_args *pba)
417 1.1 jmcneill {
418 1.12 jakllsch const pci_chipset_tag_t pc = pba->pba_pc;
419 1.12 jakllsch struct tegra_pcie_softc * const sc = pc->pc_conf_v;
420 1.12 jakllsch
421 1.12 jakllsch if (pba->pba_bus >= 1) {
422 1.12 jakllsch tegra_pcie_conf_map_bus(sc, pba->pba_bus);
423 1.12 jakllsch }
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill static int
427 1.1 jmcneill tegra_pcie_bus_maxdevs(void *v, int busno)
428 1.1 jmcneill {
429 1.1 jmcneill return busno == 0 ? 2 : 32;
430 1.1 jmcneill }
431 1.1 jmcneill
432 1.1 jmcneill static pcitag_t
433 1.1 jmcneill tegra_pcie_make_tag(void *v, int b, int d, int f)
434 1.1 jmcneill {
435 1.1 jmcneill return (b << 16) | (d << 11) | (f << 8);
436 1.1 jmcneill }
437 1.1 jmcneill
438 1.1 jmcneill static void
439 1.1 jmcneill tegra_pcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
440 1.1 jmcneill {
441 1.1 jmcneill if (bp)
442 1.1 jmcneill *bp = (tag >> 16) & 0xff;
443 1.1 jmcneill if (dp)
444 1.1 jmcneill *dp = (tag >> 11) & 0x1f;
445 1.1 jmcneill if (fp)
446 1.1 jmcneill *fp = (tag >> 8) & 0x7;
447 1.1 jmcneill }
448 1.1 jmcneill
449 1.1 jmcneill static pcireg_t
450 1.1 jmcneill tegra_pcie_conf_read(void *v, pcitag_t tag, int offset)
451 1.1 jmcneill {
452 1.1 jmcneill struct tegra_pcie_softc *sc = v;
453 1.1 jmcneill bus_space_handle_t bsh;
454 1.1 jmcneill int b, d, f;
455 1.1 jmcneill u_int reg;
456 1.1 jmcneill
457 1.3 msaitoh if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
458 1.3 msaitoh return (pcireg_t) -1;
459 1.3 msaitoh
460 1.1 jmcneill tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
461 1.1 jmcneill
462 1.12 jakllsch if (b >= TEGRA_PCIE_NBUS)
463 1.12 jakllsch return (pcireg_t) -1;
464 1.12 jakllsch
465 1.1 jmcneill if (b == 0) {
466 1.6 jakllsch if (d >= 2 || f != 0)
467 1.6 jakllsch return (pcireg_t) -1;
468 1.1 jmcneill reg = d * 0x1000 + offset;
469 1.9 jakllsch bsh = sc->sc_bsh_rpconf;
470 1.1 jmcneill } else {
471 1.12 jakllsch reg = (d << 11) | (f << 8) | (offset & 0xff);
472 1.12 jakllsch bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
473 1.12 jakllsch if (bsh == 0)
474 1.7 jakllsch return (pcireg_t) -1;
475 1.1 jmcneill }
476 1.1 jmcneill
477 1.1 jmcneill return bus_space_read_4(sc->sc_bst, bsh, reg);
478 1.1 jmcneill }
479 1.1 jmcneill
480 1.1 jmcneill static void
481 1.1 jmcneill tegra_pcie_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
482 1.1 jmcneill {
483 1.1 jmcneill struct tegra_pcie_softc *sc = v;
484 1.1 jmcneill bus_space_handle_t bsh;
485 1.1 jmcneill int b, d, f;
486 1.1 jmcneill u_int reg;
487 1.1 jmcneill
488 1.3 msaitoh if ((unsigned int)offset >= PCI_EXTCONF_SIZE)
489 1.3 msaitoh return;
490 1.3 msaitoh
491 1.1 jmcneill tegra_pcie_decompose_tag(v, tag, &b, &d, &f);
492 1.1 jmcneill
493 1.12 jakllsch if (b >= TEGRA_PCIE_NBUS)
494 1.12 jakllsch return;
495 1.12 jakllsch
496 1.1 jmcneill if (b == 0) {
497 1.6 jakllsch if (d >= 2 || f != 0)
498 1.6 jakllsch return;
499 1.1 jmcneill reg = d * 0x1000 + offset;
500 1.9 jakllsch bsh = sc->sc_bsh_rpconf;
501 1.1 jmcneill } else {
502 1.12 jakllsch reg = (d << 11) | (f << 8) | (offset & 0xff);
503 1.12 jakllsch bsh = sc->sc_bsh_extc[b-1][(offset >> 8) & 0xf];
504 1.12 jakllsch if (bsh == 0)
505 1.7 jakllsch return;
506 1.1 jmcneill }
507 1.1 jmcneill
508 1.1 jmcneill bus_space_write_4(sc->sc_bst, bsh, reg, val);
509 1.1 jmcneill }
510 1.1 jmcneill
511 1.1 jmcneill static int
512 1.1 jmcneill tegra_pcie_conf_hook(void *v, int b, int d, int f, pcireg_t id)
513 1.1 jmcneill {
514 1.10 jakllsch return PCI_CONF_ALL;
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill static void
518 1.1 jmcneill tegra_pcie_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
519 1.1 jmcneill int *ilinep)
520 1.1 jmcneill {
521 1.8 jakllsch const struct tegra_pcie_softc * const sc = v;
522 1.8 jakllsch
523 1.8 jakllsch *ilinep = sc->sc_intr & PCI_INTERRUPT_LINE_MASK;
524 1.1 jmcneill }
525 1.1 jmcneill
526 1.1 jmcneill static int
527 1.1 jmcneill tegra_pcie_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
528 1.1 jmcneill {
529 1.1 jmcneill if (pa->pa_intrpin == 0)
530 1.1 jmcneill return EINVAL;
531 1.1 jmcneill *ih = pa->pa_intrpin;
532 1.1 jmcneill return 0;
533 1.1 jmcneill }
534 1.5 jakllsch
535 1.1 jmcneill static const char *
536 1.1 jmcneill tegra_pcie_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
537 1.1 jmcneill {
538 1.1 jmcneill struct tegra_pcie_softc *sc = v;
539 1.1 jmcneill
540 1.1 jmcneill if (ih == PCI_INTERRUPT_PIN_NONE)
541 1.1 jmcneill return NULL;
542 1.1 jmcneill
543 1.1 jmcneill snprintf(buf, len, "irq %d", sc->sc_intr);
544 1.1 jmcneill return buf;
545 1.1 jmcneill }
546 1.1 jmcneill
547 1.1 jmcneill const struct evcnt *
548 1.1 jmcneill tegra_pcie_intr_evcnt(void *v, pci_intr_handle_t ih)
549 1.1 jmcneill {
550 1.1 jmcneill return NULL;
551 1.1 jmcneill }
552 1.1 jmcneill
553 1.1 jmcneill static void *
554 1.1 jmcneill tegra_pcie_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
555 1.1 jmcneill int (*callback)(void *), void *arg)
556 1.1 jmcneill {
557 1.1 jmcneill struct tegra_pcie_softc *sc = v;
558 1.1 jmcneill struct tegra_pcie_ih *pcie_ih;
559 1.1 jmcneill
560 1.1 jmcneill if (ih == 0)
561 1.1 jmcneill return NULL;
562 1.1 jmcneill
563 1.1 jmcneill pcie_ih = kmem_alloc(sizeof(*pcie_ih), KM_SLEEP);
564 1.1 jmcneill pcie_ih->ih_callback = callback;
565 1.1 jmcneill pcie_ih->ih_arg = arg;
566 1.1 jmcneill pcie_ih->ih_ipl = ipl;
567 1.1 jmcneill
568 1.1 jmcneill mutex_enter(&sc->sc_lock);
569 1.1 jmcneill TAILQ_INSERT_TAIL(&sc->sc_intrs, pcie_ih, ih_entry);
570 1.1 jmcneill sc->sc_intrgen++;
571 1.1 jmcneill mutex_exit(&sc->sc_lock);
572 1.1 jmcneill
573 1.1 jmcneill return pcie_ih;
574 1.1 jmcneill }
575 1.1 jmcneill
576 1.1 jmcneill static void
577 1.1 jmcneill tegra_pcie_intr_disestablish(void *v, void *vih)
578 1.1 jmcneill {
579 1.1 jmcneill struct tegra_pcie_softc *sc = v;
580 1.1 jmcneill struct tegra_pcie_ih *pcie_ih = vih;
581 1.1 jmcneill
582 1.1 jmcneill mutex_enter(&sc->sc_lock);
583 1.1 jmcneill TAILQ_REMOVE(&sc->sc_intrs, pcie_ih, ih_entry);
584 1.1 jmcneill mutex_exit(&sc->sc_lock);
585 1.1 jmcneill
586 1.1 jmcneill kmem_free(pcie_ih, sizeof(*pcie_ih));
587 1.1 jmcneill }
588