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