mvpex.c revision 1.7 1 1.7 matt /* $NetBSD: mvpex.c,v 1.7 2012/09/07 04:25:37 matt Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2008 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.7 matt __KERNEL_RCSID(0, "$NetBSD: mvpex.c,v 1.7 2012/09/07 04:25:37 matt Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include "opt_pci.h"
32 1.1 kiyohara #include "pci.h"
33 1.1 kiyohara
34 1.1 kiyohara #include <sys/param.h>
35 1.1 kiyohara #include <sys/bus.h>
36 1.1 kiyohara #include <sys/device.h>
37 1.1 kiyohara #include <sys/errno.h>
38 1.1 kiyohara #include <sys/extent.h>
39 1.1 kiyohara #include <sys/evcnt.h>
40 1.1 kiyohara #include <sys/malloc.h>
41 1.1 kiyohara #include <sys/systm.h>
42 1.1 kiyohara
43 1.1 kiyohara #include <prop/proplib.h>
44 1.1 kiyohara
45 1.1 kiyohara #include <dev/pci/pcivar.h>
46 1.1 kiyohara #include <dev/pci/pcireg.h>
47 1.1 kiyohara #include <dev/pci/pciconf.h>
48 1.1 kiyohara
49 1.1 kiyohara #include <dev/marvell/mvpexreg.h>
50 1.1 kiyohara #include <dev/marvell/mvpexvar.h>
51 1.1 kiyohara #include <dev/marvell/marvellreg.h>
52 1.1 kiyohara #include <dev/marvell/marvellvar.h>
53 1.1 kiyohara
54 1.1 kiyohara #include <machine/pci_machdep.h>
55 1.1 kiyohara
56 1.1 kiyohara #include "locators.h"
57 1.1 kiyohara
58 1.1 kiyohara
59 1.1 kiyohara static int mvpex_match(device_t, struct cfdata *, void *);
60 1.1 kiyohara static void mvpex_attach(device_t, device_t, void *);
61 1.1 kiyohara
62 1.1 kiyohara static int mvpex_intr(void *);
63 1.1 kiyohara
64 1.1 kiyohara static void mvpex_init(struct mvpex_softc *);
65 1.1 kiyohara #if 0 /* shall move to pchb(4)? */
66 1.1 kiyohara static void mvpex_barinit(struct mvpex_softc *);
67 1.1 kiyohara static int mvpex_wininit(struct mvpex_softc *, int, int, int, int, uint32_t *,
68 1.1 kiyohara uint32_t *);
69 1.1 kiyohara #else
70 1.1 kiyohara static void mvpex_wininit(struct mvpex_softc *);
71 1.1 kiyohara #endif
72 1.1 kiyohara #if NPCI > 0
73 1.1 kiyohara static void mvpex_pci_config(struct mvpex_softc *, bus_space_tag_t,
74 1.1 kiyohara bus_space_tag_t, bus_dma_tag_t, pci_chipset_tag_t,
75 1.1 kiyohara u_long, u_long, u_long, u_long, int);
76 1.1 kiyohara #endif
77 1.1 kiyohara
78 1.1 kiyohara CFATTACH_DECL_NEW(mvpex_gt, sizeof(struct mvpex_softc),
79 1.1 kiyohara mvpex_match, mvpex_attach, NULL, NULL);
80 1.1 kiyohara CFATTACH_DECL_NEW(mvpex_mbus, sizeof(struct mvpex_softc),
81 1.1 kiyohara mvpex_match, mvpex_attach, NULL, NULL);
82 1.1 kiyohara
83 1.1 kiyohara
84 1.1 kiyohara /* ARGSUSED */
85 1.1 kiyohara static int
86 1.1 kiyohara mvpex_match(device_t parent, struct cfdata *match, void *aux)
87 1.1 kiyohara {
88 1.1 kiyohara struct marvell_attach_args *mva = aux;
89 1.1 kiyohara
90 1.2 kiyohara if (strcmp(mva->mva_name, match->cf_name) != 0)
91 1.1 kiyohara return 0;
92 1.1 kiyohara if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
93 1.1 kiyohara mva->mva_irq == MVA_IRQ_DEFAULT)
94 1.1 kiyohara return 0;
95 1.1 kiyohara
96 1.1 kiyohara mva->mva_size = MVPEX_SIZE;
97 1.1 kiyohara return 1;
98 1.1 kiyohara }
99 1.1 kiyohara
100 1.1 kiyohara /* ARGSUSED */
101 1.1 kiyohara static void
102 1.1 kiyohara mvpex_attach(device_t parent, device_t self, void *aux)
103 1.1 kiyohara {
104 1.1 kiyohara struct mvpex_softc *sc = device_private(self);
105 1.1 kiyohara struct marvell_attach_args *mva = aux;
106 1.1 kiyohara #if NPCI > 0
107 1.1 kiyohara prop_dictionary_t dict = device_properties(self);
108 1.1 kiyohara prop_object_t pc, iot, memt;
109 1.1 kiyohara pci_chipset_tag_t mvpex_chipset;
110 1.1 kiyohara bus_space_tag_t mvpex_io_bs_tag, mvpex_mem_bs_tag;
111 1.1 kiyohara uint64_t iostart = 0, ioend = 0, memstart = 0, memend = 0;
112 1.3 jakllsch uint32_t cl_size = 0;
113 1.1 kiyohara int i;
114 1.1 kiyohara #endif
115 1.1 kiyohara
116 1.1 kiyohara aprint_normal(": Marvell PCI Express Interface\n");
117 1.1 kiyohara aprint_naive("\n");
118 1.1 kiyohara
119 1.1 kiyohara #if NPCI > 0
120 1.1 kiyohara iot = prop_dictionary_get(dict, "io-bus-tag");
121 1.1 kiyohara if (iot == NULL) {
122 1.1 kiyohara aprint_error_dev(self, "no io-bus-tag property\n");
123 1.1 kiyohara return;
124 1.1 kiyohara }
125 1.1 kiyohara KASSERT(prop_object_type(iot) == PROP_TYPE_DATA);
126 1.1 kiyohara mvpex_io_bs_tag = __UNCONST(prop_data_data_nocopy(iot));
127 1.1 kiyohara memt = prop_dictionary_get(dict, "mem-bus-tag");
128 1.1 kiyohara if (memt == NULL) {
129 1.1 kiyohara aprint_error_dev(self, "no mem-bus-tag property\n");
130 1.1 kiyohara return;
131 1.1 kiyohara }
132 1.1 kiyohara KASSERT(prop_object_type(memt) == PROP_TYPE_DATA);
133 1.1 kiyohara mvpex_mem_bs_tag = __UNCONST(prop_data_data_nocopy(memt));
134 1.1 kiyohara pc = prop_dictionary_get(dict, "pci-chipset");
135 1.1 kiyohara if (pc == NULL) {
136 1.1 kiyohara aprint_error_dev(self, "no pci-chipset property\n");
137 1.1 kiyohara return;
138 1.1 kiyohara }
139 1.1 kiyohara KASSERT(prop_object_type(pc) == PROP_TYPE_DATA);
140 1.1 kiyohara mvpex_chipset = __UNCONST(prop_data_data_nocopy(pc));
141 1.1 kiyohara #ifdef PCI_NETBSD_CONFIGURE
142 1.1 kiyohara if (!prop_dictionary_get_uint64(dict, "iostart", &iostart)) {
143 1.1 kiyohara aprint_error_dev(self, "no iostart property\n");
144 1.1 kiyohara return;
145 1.1 kiyohara }
146 1.1 kiyohara if (!prop_dictionary_get_uint64(dict, "ioend", &ioend)) {
147 1.1 kiyohara aprint_error_dev(self, "no ioend property\n");
148 1.1 kiyohara return;
149 1.1 kiyohara }
150 1.1 kiyohara if (!prop_dictionary_get_uint64(dict, "memstart", &memstart)) {
151 1.1 kiyohara aprint_error_dev(self, "no memstart property\n");
152 1.1 kiyohara return;
153 1.1 kiyohara }
154 1.1 kiyohara if (!prop_dictionary_get_uint64(dict, "memend", &memend)) {
155 1.1 kiyohara aprint_error_dev(self, "no memend property\n");
156 1.1 kiyohara return;
157 1.1 kiyohara }
158 1.1 kiyohara if (!prop_dictionary_get_uint32(dict, "cache-line-size", &cl_size)) {
159 1.1 kiyohara aprint_error_dev(self, "no cache-line-size property\n");
160 1.1 kiyohara return;
161 1.1 kiyohara }
162 1.1 kiyohara #endif
163 1.1 kiyohara #endif
164 1.1 kiyohara
165 1.1 kiyohara sc->sc_dev = self;
166 1.1 kiyohara sc->sc_model = mva->mva_model;
167 1.1 kiyohara sc->sc_rev = mva->mva_revision;
168 1.1 kiyohara sc->sc_offset = mva->mva_offset;
169 1.1 kiyohara sc->sc_iot = mva->mva_iot;
170 1.1 kiyohara
171 1.1 kiyohara /* Map I/O registers for mvpex */
172 1.1 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
173 1.1 kiyohara mva->mva_size, &sc->sc_ioh)) {
174 1.1 kiyohara aprint_error_dev(self, "can't map registers\n");
175 1.1 kiyohara return;
176 1.1 kiyohara }
177 1.1 kiyohara mvpex_init(sc);
178 1.1 kiyohara
179 1.1 kiyohara /* XXX: looks seem good to specify level IPL_VM. */
180 1.1 kiyohara marvell_intr_establish(mva->mva_irq, IPL_VM, mvpex_intr, sc);
181 1.1 kiyohara
182 1.1 kiyohara #if NPCI > 0
183 1.1 kiyohara for (i = 0; i < PCI_INTERRUPT_PIN_MAX; i++) {
184 1.1 kiyohara sc->sc_intrtab[i].intr_pin = PCI_INTERRUPT_PIN_A + i;
185 1.1 kiyohara sc->sc_intrtab[i].intr_refcnt = 0;
186 1.1 kiyohara LIST_INIT(&sc->sc_intrtab[i].intr_list);
187 1.1 kiyohara }
188 1.1 kiyohara
189 1.1 kiyohara mvpex_pci_config(sc, mvpex_io_bs_tag, mvpex_mem_bs_tag, mva->mva_dmat,
190 1.1 kiyohara mvpex_chipset, iostart, ioend, memstart, memend, cl_size);
191 1.1 kiyohara #endif
192 1.1 kiyohara }
193 1.1 kiyohara
194 1.1 kiyohara static int
195 1.1 kiyohara mvpex_intr(void *arg)
196 1.1 kiyohara {
197 1.1 kiyohara struct mvpex_softc *sc = (struct mvpex_softc *)arg;
198 1.1 kiyohara struct mvpex_intrhand *ih;
199 1.1 kiyohara struct mvpex_intrtab *intrtab;
200 1.1 kiyohara uint32_t ic, im;
201 1.1 kiyohara int handled = 0, pin, rv, i, s;
202 1.1 kiyohara
203 1.1 kiyohara for (;;) {
204 1.1 kiyohara ic = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_IC);
205 1.1 kiyohara im = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM);
206 1.1 kiyohara ic &= im;
207 1.1 kiyohara
208 1.1 kiyohara if (!ic)
209 1.1 kiyohara break;
210 1.1 kiyohara
211 1.1 kiyohara for (i = 0, pin = PCI_INTERRUPT_PIN_A;
212 1.1 kiyohara i < PCI_INTERRUPT_PIN_MAX; pin++, i++) {
213 1.1 kiyohara if ((ic & MVPEX_I_PIN(pin)) == 0)
214 1.1 kiyohara continue;
215 1.1 kiyohara
216 1.1 kiyohara intrtab = &sc->sc_intrtab[i];
217 1.1 kiyohara LIST_FOREACH(ih, &intrtab->intr_list, ih_q) {
218 1.1 kiyohara s = _splraise(ih->ih_type);
219 1.1 kiyohara rv = (*ih->ih_func)(ih->ih_arg);
220 1.1 kiyohara splx(s);
221 1.1 kiyohara if (rv) {
222 1.1 kiyohara ih->ih_evcnt.ev_count++;
223 1.1 kiyohara handled++;
224 1.1 kiyohara }
225 1.1 kiyohara }
226 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_IC,
227 1.1 kiyohara ~MVPEX_I_PIN(pin));
228 1.1 kiyohara }
229 1.1 kiyohara }
230 1.1 kiyohara
231 1.1 kiyohara return handled;
232 1.1 kiyohara }
233 1.1 kiyohara
234 1.1 kiyohara
235 1.1 kiyohara static void
236 1.1 kiyohara mvpex_init(struct mvpex_softc *sc)
237 1.1 kiyohara {
238 1.1 kiyohara uint32_t reg;
239 1.1 kiyohara int window;
240 1.1 kiyohara
241 1.1 kiyohara /*
242 1.1 kiyohara * First implement Guideline (GL# PCI Express-2) Wrong Default Value
243 1.1 kiyohara * to Transmitter Output Current (TXAMP) Relevant for: 88F5181-A1/B0/B1
244 1.1 kiyohara * and 88F5281-B0
245 1.1 kiyohara */
246 1.1 kiyohara /* Write the read command */
247 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0x1b00, 0x80820000);
248 1.1 kiyohara reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0x1b00);
249 1.1 kiyohara /* Prepare new data for write */
250 1.1 kiyohara reg &= ~0x7; /* Clear bits [2:0] */
251 1.1 kiyohara reg |= 0x4; /* Set the new value */
252 1.1 kiyohara reg &= ~0x80000000; /* Set "write" command */
253 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0x1b00, reg);
254 1.1 kiyohara
255 1.1 kiyohara for (window = 0; window < MVPEX_NWINDOW; window++)
256 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WC(window), 0);
257 1.1 kiyohara
258 1.1 kiyohara #if 0 /* shall move to pchb(4)? */
259 1.1 kiyohara mvpex_barinit(sc);
260 1.1 kiyohara #else
261 1.1 kiyohara mvpex_wininit(sc);
262 1.1 kiyohara #endif
263 1.1 kiyohara
264 1.1 kiyohara /* Clear Interrupt Cause and Mask registers */
265 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_IC, 0);
266 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM, 0);
267 1.1 kiyohara
268 1.1 kiyohara /* now wait 60 ns to be sure the link is valid (spec compliant) */
269 1.1 kiyohara delay(1);
270 1.1 kiyohara }
271 1.1 kiyohara
272 1.1 kiyohara #if 0
273 1.1 kiyohara static int
274 1.1 kiyohara mvpex_wininit(struct mvpex_softc *sc, int window, int tbegin, int tend,
275 1.1 kiyohara int barmap, uint32_t *barbase, uint32_t *barsize)
276 1.1 kiyohara {
277 1.1 kiyohara uint32_t target, attr, base, size;
278 1.1 kiyohara int targetid;
279 1.1 kiyohara
280 1.1 kiyohara for (targetid = tbegin; targetid <= tend && window < MVPEX_NWINDOW;
281 1.1 kiyohara targetid++) {
282 1.1 kiyohara if (orion_target(targetid, &target, &attr, &base, &size) == -1)
283 1.1 kiyohara continue;
284 1.1 kiyohara if (size == 0)
285 1.1 kiyohara continue;
286 1.1 kiyohara
287 1.1 kiyohara if (base < *barbase)
288 1.1 kiyohara *barbase = base;
289 1.1 kiyohara *barsize += size;
290 1.1 kiyohara
291 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WC(window),
292 1.1 kiyohara MVPEX_WC_WINEN |
293 1.1 kiyohara barmap |
294 1.1 kiyohara MVPEX_WC_TARGET(target) |
295 1.1 kiyohara MVPEX_WC_ATTR(attr) |
296 1.1 kiyohara MVPEX_WC_SIZE(size));
297 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WB(window),
298 1.1 kiyohara MVPEX_WB_BASE(base));
299 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WR(window), 0);
300 1.1 kiyohara window++;
301 1.1 kiyohara }
302 1.1 kiyohara
303 1.1 kiyohara return window;
304 1.1 kiyohara }
305 1.1 kiyohara
306 1.1 kiyohara /* shall move to pchb(4)? */
307 1.1 kiyohara static void
308 1.1 kiyohara mvpex_barinit(struct mvpex_softc *sc)
309 1.1 kiyohara {
310 1.1 kiyohara const uint32_t barflag =
311 1.1 kiyohara PCI_MAPREG_MEM_PREFETCHABLE_MASK | PCI_MAPREG_MEM_TYPE_64BIT;
312 1.1 kiyohara uint32_t base, size;
313 1.1 kiyohara int window = 0;
314 1.1 kiyohara
315 1.1 kiyohara marvell_winparams_by_tag(device_parent(sc->sc_dev),
316 1.1 kiyohara ORION_TARGETID_INTERNALREG, NULL, NULL, &base, &size);
317 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR0INTERNAL,
318 1.1 kiyohara barflag | (base & MVPEX_BAR0INTERNAL_MASK));
319 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR0INTERNALH, 0);
320 1.1 kiyohara
321 1.1 kiyohara base = size = 0;
322 1.1 kiyohara window = mvpex_wininit(sc, window, ORION_TARGETID_SDRAM_CS0,
323 1.1 kiyohara ORION_TARGETID_SDRAM_CS3, MVPEX_WC_BARMAP_BAR1, &base, &size);
324 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR1,
325 1.1 kiyohara barflag | (base & MVPEX_BAR_MASK));
326 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR1H, 0);
327 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR1C,
328 1.1 kiyohara MVPEX_BARC_BARSIZE(size) | MVPEX_BARC_BAREN);
329 1.1 kiyohara
330 1.1 kiyohara #if 0
331 1.1 kiyohara base = size = 0;
332 1.1 kiyohara if (sc->sc_model == MARVELL_ORION_1_88F1181)
333 1.1 kiyohara window = mvpex_wininit(sc, window, ORION_TARGETID_FLASH_CS,
334 1.1 kiyohara ORION_TARGETID_DEVICE_BOOTCS,
335 1.1 kiyohara MVPEX_WC_BARMAP_BAR2, &base, &size);
336 1.1 kiyohara else {
337 1.1 kiyohara window = mvpex_wininit(sc, window,
338 1.1 kiyohara ORION_TARGETID_DEVICE_CS0, ORION_TARGETID_DEVICE_CS2,
339 1.1 kiyohara MVPEX_WC_BARMAP_BAR2, &base, &size);
340 1.1 kiyohara window = mvpex_wininit(sc, window,
341 1.1 kiyohara ORION_TARGETID_DEVICE_BOOTCS, ORION_TARGETID_DEVICE_BOOTCS,
342 1.1 kiyohara MVPEX_WC_BARMAP_BAR2, &base, &size);
343 1.1 kiyohara }
344 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR2,
345 1.1 kiyohara barflag | (base & MVPEX_BAR_MASK));
346 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR2H, 0);
347 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR2C,
348 1.1 kiyohara MVPEX_BARC_BARSIZE(size) | MVPEX_BARC_BAREN);
349 1.1 kiyohara #else
350 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_BAR2C, 0);
351 1.1 kiyohara #endif
352 1.1 kiyohara }
353 1.1 kiyohara #else
354 1.1 kiyohara static void
355 1.1 kiyohara mvpex_wininit(struct mvpex_softc *sc)
356 1.1 kiyohara {
357 1.1 kiyohara device_t pdev = device_parent(sc->sc_dev);
358 1.1 kiyohara uint64_t base;
359 1.1 kiyohara uint32_t size;
360 1.1 kiyohara int target, attr, window, rv, i;
361 1.1 kiyohara static struct {
362 1.1 kiyohara int tag;
363 1.1 kiyohara int bar;
364 1.1 kiyohara } tags[] = {
365 1.1 kiyohara { MARVELL_TAG_SDRAM_CS0, MVPEX_WC_BARMAP_BAR1 },
366 1.1 kiyohara { MARVELL_TAG_SDRAM_CS1, MVPEX_WC_BARMAP_BAR1 },
367 1.1 kiyohara { MARVELL_TAG_SDRAM_CS2, MVPEX_WC_BARMAP_BAR1 },
368 1.1 kiyohara { MARVELL_TAG_SDRAM_CS3, MVPEX_WC_BARMAP_BAR1 },
369 1.1 kiyohara
370 1.1 kiyohara { MARVELL_TAG_UNDEFINED, 0 },
371 1.1 kiyohara };
372 1.1 kiyohara
373 1.1 kiyohara for (window = 0, i = 0;
374 1.1 kiyohara tags[i].tag != MARVELL_TAG_UNDEFINED && window < MVPEX_NWINDOW;
375 1.1 kiyohara i++) {
376 1.1 kiyohara rv = marvell_winparams_by_tag(pdev, tags[i].tag,
377 1.1 kiyohara &target, &attr, &base, &size);
378 1.1 kiyohara if (rv != 0 || size == 0)
379 1.1 kiyohara continue;
380 1.1 kiyohara
381 1.1 kiyohara if (base > 0xffffffffULL) {
382 1.1 kiyohara aprint_error_dev(sc->sc_dev,
383 1.1 kiyohara "tag %d address 0x%llx not support\n",
384 1.1 kiyohara tags[i].tag, base);
385 1.1 kiyohara continue;
386 1.1 kiyohara }
387 1.1 kiyohara
388 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WC(window),
389 1.1 kiyohara MVPEX_WC_WINEN |
390 1.1 kiyohara tags[i].bar |
391 1.1 kiyohara MVPEX_WC_TARGET(target) |
392 1.1 kiyohara MVPEX_WC_ATTR(attr) |
393 1.1 kiyohara MVPEX_WC_SIZE(size));
394 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WB(window),
395 1.1 kiyohara MVPEX_WB_BASE(base));
396 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WR(window), 0);
397 1.1 kiyohara window++;
398 1.1 kiyohara }
399 1.1 kiyohara for ( ; window < MVPEX_NWINDOW; window++)
400 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_WC(window), 0);
401 1.1 kiyohara }
402 1.1 kiyohara #endif
403 1.1 kiyohara
404 1.1 kiyohara #if NPCI > 0
405 1.1 kiyohara static void
406 1.1 kiyohara mvpex_pci_config(struct mvpex_softc *sc, bus_space_tag_t iot,
407 1.1 kiyohara bus_space_tag_t memt, bus_dma_tag_t dmat, pci_chipset_tag_t pc,
408 1.1 kiyohara u_long iostart, u_long ioend, u_long memstart, u_long memend,
409 1.1 kiyohara int cacheline_size)
410 1.1 kiyohara {
411 1.1 kiyohara struct pcibus_attach_args pba;
412 1.1 kiyohara #ifdef PCI_NETBSD_CONFIGURE
413 1.1 kiyohara struct extent *ioext = NULL, *memext = NULL;
414 1.1 kiyohara #endif
415 1.1 kiyohara uint32_t stat;
416 1.1 kiyohara
417 1.1 kiyohara stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_STAT);
418 1.1 kiyohara
419 1.1 kiyohara #ifdef PCI_NETBSD_CONFIGURE
420 1.6 para ioext = extent_create("pexio", iostart, ioend, NULL, 0, EX_NOWAIT);
421 1.6 para memext = extent_create("pexmem", memstart, memend, NULL, 0, EX_NOWAIT);
422 1.1 kiyohara if (ioext != NULL && memext != NULL)
423 1.1 kiyohara pci_configure_bus(pc, ioext, memext, NULL,
424 1.1 kiyohara MVPEX_STAT_PEXBUSNUM(stat), cacheline_size);
425 1.1 kiyohara else
426 1.1 kiyohara aprint_error_dev(sc->sc_dev, "can't create extent %s%s%s\n",
427 1.1 kiyohara ioext == NULL ? "io" : "",
428 1.1 kiyohara ioext == NULL && memext == NULL ? " and " : "",
429 1.1 kiyohara memext == NULL ? "mem" : "");
430 1.1 kiyohara if (ioext != NULL)
431 1.1 kiyohara extent_destroy(ioext);
432 1.1 kiyohara if (memext != NULL)
433 1.1 kiyohara extent_destroy(memext);
434 1.1 kiyohara #endif
435 1.1 kiyohara
436 1.1 kiyohara pba.pba_iot = iot;
437 1.1 kiyohara pba.pba_memt = memt;
438 1.1 kiyohara pba.pba_dmat = dmat;
439 1.1 kiyohara pba.pba_dmat64 = NULL;
440 1.1 kiyohara pba.pba_pc = pc;
441 1.5 dyoung pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
442 1.1 kiyohara pba.pba_bus = MVPEX_STAT_PEXBUSNUM(stat);
443 1.1 kiyohara pba.pba_bridgetag = NULL;
444 1.1 kiyohara config_found_ia(sc->sc_dev, "pcibus", &pba, NULL);
445 1.1 kiyohara }
446 1.1 kiyohara
447 1.1 kiyohara
448 1.1 kiyohara /*
449 1.1 kiyohara * PCI-Express CPU dependent code
450 1.1 kiyohara */
451 1.1 kiyohara
452 1.1 kiyohara /* ARGSUSED */
453 1.1 kiyohara void
454 1.1 kiyohara mvpex_attach_hook(device_t parent, device_t self,
455 1.1 kiyohara struct pcibus_attach_args *pba)
456 1.1 kiyohara {
457 1.1 kiyohara
458 1.1 kiyohara /* Nothing */
459 1.1 kiyohara }
460 1.1 kiyohara
461 1.1 kiyohara /*
462 1.1 kiyohara * Bit map for configuration register:
463 1.1 kiyohara * [31] ConfigEn
464 1.1 kiyohara * [30:28] Reserved
465 1.1 kiyohara * [27:24] ExtRegNum (PCI Express only)
466 1.1 kiyohara * [23:16] BusNum
467 1.1 kiyohara * [15:11] DevNum
468 1.1 kiyohara * [10: 8] FunctNum
469 1.1 kiyohara * [ 7: 2] RegNum
470 1.1 kiyohara * [ 1: 0] reserved
471 1.1 kiyohara */
472 1.1 kiyohara
473 1.1 kiyohara /* ARGSUSED */
474 1.1 kiyohara int
475 1.1 kiyohara mvpex_bus_maxdevs(void *v, int busno)
476 1.1 kiyohara {
477 1.1 kiyohara
478 1.1 kiyohara return 32; /* 32 device/bus */
479 1.1 kiyohara }
480 1.1 kiyohara
481 1.1 kiyohara /* ARGSUSED */
482 1.1 kiyohara pcitag_t
483 1.1 kiyohara mvpex_make_tag(void *v, int bus, int dev, int func)
484 1.1 kiyohara {
485 1.1 kiyohara
486 1.1 kiyohara return (bus << 16) | (dev << 11) | (func << 8);
487 1.1 kiyohara }
488 1.1 kiyohara
489 1.1 kiyohara /* ARGSUSED */
490 1.1 kiyohara void
491 1.1 kiyohara mvpex_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
492 1.1 kiyohara {
493 1.1 kiyohara
494 1.1 kiyohara if (bp != NULL)
495 1.1 kiyohara *bp = (tag >> 16) & 0xff;
496 1.1 kiyohara if (dp != NULL)
497 1.1 kiyohara *dp = (tag >> 11) & 0x1f;
498 1.1 kiyohara if (fp != NULL)
499 1.1 kiyohara *fp = (tag >> 8) & 0x07;
500 1.1 kiyohara }
501 1.1 kiyohara
502 1.1 kiyohara pcireg_t
503 1.1 kiyohara mvpex_conf_read(void *v, pcitag_t tag, int reg)
504 1.1 kiyohara {
505 1.1 kiyohara struct mvpex_softc *sc = v;
506 1.1 kiyohara pcireg_t addr, pci_cs;
507 1.1 kiyohara uint32_t stat;
508 1.1 kiyohara int bus, dev, func, pexbus, pexdev;
509 1.1 kiyohara
510 1.1 kiyohara mvpex_decompose_tag(v, tag, &bus, &dev, &func);
511 1.1 kiyohara
512 1.1 kiyohara stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_STAT);
513 1.1 kiyohara pexbus = MVPEX_STAT_PEXBUSNUM(stat);
514 1.1 kiyohara pexdev = MVPEX_STAT_PEXDEVNUM(stat);
515 1.1 kiyohara if (bus != pexbus || dev != pexdev)
516 1.1 kiyohara if (stat & MVPEX_STAT_DLDOWN)
517 1.1 kiyohara return -1;
518 1.1 kiyohara
519 1.1 kiyohara if (bus == pexbus) {
520 1.1 kiyohara if (pexdev == 0) {
521 1.1 kiyohara if (dev != 1 && dev != pexdev)
522 1.1 kiyohara return -1;
523 1.1 kiyohara } else {
524 1.1 kiyohara if (dev != 0 && dev != pexdev)
525 1.1 kiyohara return -1;
526 1.1 kiyohara }
527 1.1 kiyohara if (func != 0)
528 1.1 kiyohara return -1;
529 1.1 kiyohara }
530 1.1 kiyohara
531 1.1 kiyohara addr = ((reg & 0xf00) << 24) | tag | (reg & 0xfc);
532 1.1 kiyohara
533 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_CA,
534 1.1 kiyohara addr | MVPEX_CA_CONFIGEN);
535 1.1 kiyohara if ((addr | MVPEX_CA_CONFIGEN) !=
536 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_CA))
537 1.1 kiyohara return -1;
538 1.1 kiyohara
539 1.1 kiyohara pci_cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
540 1.1 kiyohara PCI_COMMAND_STATUS_REG);
541 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh,
542 1.1 kiyohara PCI_COMMAND_STATUS_REG, pci_cs | PCI_STATUS_MASTER_ABORT);
543 1.1 kiyohara
544 1.1 kiyohara return bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_CD);
545 1.1 kiyohara }
546 1.1 kiyohara
547 1.1 kiyohara void
548 1.1 kiyohara mvpex_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
549 1.1 kiyohara {
550 1.1 kiyohara struct mvpex_softc *sc = v;
551 1.1 kiyohara pcireg_t addr;
552 1.1 kiyohara uint32_t stat;
553 1.1 kiyohara int bus, dev, func, pexbus, pexdev;
554 1.1 kiyohara
555 1.1 kiyohara mvpex_decompose_tag(v, tag, &bus, &dev, &func);
556 1.1 kiyohara
557 1.1 kiyohara stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_STAT);
558 1.1 kiyohara pexbus = MVPEX_STAT_PEXBUSNUM(stat);
559 1.1 kiyohara pexdev = MVPEX_STAT_PEXDEVNUM(stat);
560 1.1 kiyohara if (bus != pexbus || dev != pexdev)
561 1.1 kiyohara if (stat & MVPEX_STAT_DLDOWN)
562 1.1 kiyohara return;
563 1.1 kiyohara
564 1.1 kiyohara if (bus == pexbus) {
565 1.1 kiyohara if (pexdev == 0) {
566 1.1 kiyohara if (dev != 1 && dev != pexdev)
567 1.1 kiyohara return;
568 1.1 kiyohara } else {
569 1.1 kiyohara if (dev != 0 && dev != pexdev)
570 1.1 kiyohara return;
571 1.1 kiyohara }
572 1.1 kiyohara if (func != 0)
573 1.1 kiyohara return;
574 1.1 kiyohara }
575 1.1 kiyohara
576 1.1 kiyohara addr = ((reg & 0xf00) << 24) | tag | (reg & 0xfc);
577 1.1 kiyohara
578 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_CA,
579 1.1 kiyohara addr | MVPEX_CA_CONFIGEN);
580 1.1 kiyohara if ((addr | MVPEX_CA_CONFIGEN) !=
581 1.1 kiyohara bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_CA))
582 1.1 kiyohara return;
583 1.1 kiyohara
584 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_CD, data);
585 1.1 kiyohara }
586 1.1 kiyohara
587 1.1 kiyohara /* ARGSUSED */
588 1.1 kiyohara int
589 1.7 matt mvpex_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
590 1.1 kiyohara {
591 1.1 kiyohara
592 1.1 kiyohara if (bus == 0 && dev == 0) /* don't configure GT */
593 1.1 kiyohara return 0;
594 1.1 kiyohara
595 1.1 kiyohara return PCI_CONF_DEFAULT;
596 1.1 kiyohara }
597 1.1 kiyohara
598 1.1 kiyohara int
599 1.4 dyoung mvpex_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
600 1.1 kiyohara {
601 1.1 kiyohara
602 1.1 kiyohara switch (pa->pa_intrpin) {
603 1.1 kiyohara case PCI_INTERRUPT_PIN_A:
604 1.1 kiyohara case PCI_INTERRUPT_PIN_B:
605 1.1 kiyohara case PCI_INTERRUPT_PIN_C:
606 1.1 kiyohara case PCI_INTERRUPT_PIN_D:
607 1.1 kiyohara *ihp = pa->pa_intrpin;
608 1.1 kiyohara return 0;
609 1.1 kiyohara }
610 1.1 kiyohara return -1;
611 1.1 kiyohara }
612 1.1 kiyohara
613 1.1 kiyohara /* ARGSUSED */
614 1.1 kiyohara const char *
615 1.1 kiyohara mvpex_intr_string(void *v, pci_intr_handle_t pin)
616 1.1 kiyohara {
617 1.1 kiyohara static char intrstr[32];
618 1.1 kiyohara
619 1.1 kiyohara switch (pin) {
620 1.1 kiyohara case PCI_INTERRUPT_PIN_A:
621 1.1 kiyohara case PCI_INTERRUPT_PIN_B:
622 1.1 kiyohara case PCI_INTERRUPT_PIN_C:
623 1.1 kiyohara case PCI_INTERRUPT_PIN_D:
624 1.1 kiyohara break;
625 1.1 kiyohara
626 1.1 kiyohara default:
627 1.1 kiyohara return NULL;
628 1.1 kiyohara }
629 1.1 kiyohara snprintf(intrstr, sizeof(intrstr), "interrupt pin INT%c#",
630 1.1 kiyohara (char)('A' - 1 + pin));
631 1.1 kiyohara
632 1.1 kiyohara return intrstr;
633 1.1 kiyohara }
634 1.1 kiyohara
635 1.1 kiyohara /* ARGSUSED */
636 1.1 kiyohara const struct evcnt *
637 1.1 kiyohara mvpex_intr_evcnt(void *v, pci_intr_handle_t pin)
638 1.1 kiyohara {
639 1.1 kiyohara
640 1.1 kiyohara return NULL;
641 1.1 kiyohara }
642 1.1 kiyohara
643 1.1 kiyohara /*
644 1.1 kiyohara * XXXX: Shall these functions use mutex(9) instead of spl(9)?
645 1.1 kiyohara * MV78200 and MV64360 and after supports SMP.
646 1.1 kiyohara */
647 1.1 kiyohara
648 1.1 kiyohara /* ARGSUSED */
649 1.1 kiyohara void *
650 1.1 kiyohara mvpex_intr_establish(void *v, pci_intr_handle_t pin, int ipl,
651 1.1 kiyohara int (*intrhand)(void *), void *intrarg)
652 1.1 kiyohara {
653 1.1 kiyohara struct mvpex_softc *sc = (struct mvpex_softc *)v;
654 1.1 kiyohara struct mvpex_intrtab *intrtab;
655 1.1 kiyohara struct mvpex_intrhand *pexih;
656 1.1 kiyohara uint32_t mask;
657 1.1 kiyohara int ih = pin - 1, s;
658 1.1 kiyohara
659 1.1 kiyohara intrtab = &sc->sc_intrtab[ih];
660 1.1 kiyohara
661 1.1 kiyohara KASSERT(pin == intrtab->intr_pin);
662 1.1 kiyohara
663 1.1 kiyohara pexih = malloc(sizeof(*pexih), M_DEVBUF, M_NOWAIT);
664 1.1 kiyohara if (pexih == NULL)
665 1.1 kiyohara return NULL;
666 1.1 kiyohara
667 1.1 kiyohara pexih->ih_func = intrhand;
668 1.1 kiyohara pexih->ih_arg = intrarg;
669 1.1 kiyohara pexih->ih_type = ipl;
670 1.1 kiyohara pexih->ih_intrtab = intrtab;
671 1.1 kiyohara evcnt_attach_dynamic(&pexih->ih_evcnt, EVCNT_TYPE_INTR, NULL, "mvpex",
672 1.1 kiyohara mvpex_intr_string(v, pin));
673 1.1 kiyohara
674 1.1 kiyohara s = splhigh();
675 1.1 kiyohara
676 1.1 kiyohara /* First, link it into the tables. */
677 1.1 kiyohara LIST_INSERT_HEAD(&intrtab->intr_list, pexih, ih_q);
678 1.1 kiyohara
679 1.1 kiyohara /* Now enable it. */
680 1.1 kiyohara if (intrtab->intr_refcnt++ == 0) {
681 1.1 kiyohara mask = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM);
682 1.1 kiyohara mask |= MVPEX_I_PIN(intrtab->intr_pin);
683 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM, mask);
684 1.1 kiyohara }
685 1.1 kiyohara
686 1.1 kiyohara splx(s);
687 1.1 kiyohara
688 1.1 kiyohara return pexih;
689 1.1 kiyohara }
690 1.1 kiyohara
691 1.1 kiyohara void
692 1.1 kiyohara mvpex_intr_disestablish(void *v, void *ih)
693 1.1 kiyohara {
694 1.1 kiyohara struct mvpex_softc *sc = (struct mvpex_softc *)v;
695 1.1 kiyohara struct mvpex_intrtab *intrtab;
696 1.1 kiyohara struct mvpex_intrhand *pexih = ih;
697 1.1 kiyohara uint32_t mask;
698 1.1 kiyohara int s;
699 1.1 kiyohara
700 1.1 kiyohara intrtab = pexih->ih_intrtab;
701 1.1 kiyohara
702 1.1 kiyohara s = splhigh();
703 1.1 kiyohara
704 1.1 kiyohara /*
705 1.1 kiyohara * First, remove it from the table.
706 1.1 kiyohara */
707 1.1 kiyohara LIST_REMOVE(pexih, ih_q);
708 1.1 kiyohara
709 1.1 kiyohara /* Now, disable it, if there is nothing remaining on the list. */
710 1.1 kiyohara if (intrtab->intr_refcnt-- == 1) {
711 1.1 kiyohara mask = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM);
712 1.1 kiyohara mask &= ~MVPEX_I_PIN(intrtab->intr_pin);
713 1.1 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVPEX_IM, mask);
714 1.1 kiyohara }
715 1.1 kiyohara splx(s);
716 1.1 kiyohara
717 1.1 kiyohara free(pexih, M_DEVBUF);
718 1.1 kiyohara }
719 1.1 kiyohara #endif /* NPCI > 0 */
720