gtpci.c revision 1.34.4.1 1 1.34.4.1 thorpej /* $NetBSD: gtpci.c,v 1.34.4.1 2021/03/23 07:14:53 thorpej Exp $ */
2 1.1 matt /*
3 1.22 kiyohara * Copyright (c) 2008, 2009 KIYOHARA Takashi
4 1.1 matt * All rights reserved.
5 1.1 matt *
6 1.1 matt * Redistribution and use in source and binary forms, with or without
7 1.1 matt * modification, are permitted provided that the following conditions
8 1.1 matt * are met:
9 1.1 matt * 1. Redistributions of source code must retain the above copyright
10 1.1 matt * notice, this list of conditions and the following disclaimer.
11 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer in the
13 1.1 matt * documentation and/or other materials provided with the distribution.
14 1.1 matt *
15 1.22 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.22 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.22 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.22 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.22 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.22 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.22 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.22 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.22 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.22 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
26 1.1 matt */
27 1.10 lukem
28 1.10 lukem #include <sys/cdefs.h>
29 1.34.4.1 thorpej __KERNEL_RCSID(0, "$NetBSD: gtpci.c,v 1.34.4.1 2021/03/23 07:14:53 thorpej Exp $");
30 1.22 kiyohara
31 1.22 kiyohara #include "opt_pci.h"
32 1.22 kiyohara #include "pci.h"
33 1.1 matt
34 1.1 matt #include <sys/param.h>
35 1.22 kiyohara #include <sys/bus.h>
36 1.1 matt #include <sys/device.h>
37 1.22 kiyohara #include <sys/errno.h>
38 1.1 matt #include <sys/malloc.h>
39 1.1 matt
40 1.22 kiyohara #include <prop/proplib.h>
41 1.1 matt
42 1.1 matt #include <dev/pci/pcireg.h>
43 1.1 matt #include <dev/pci/pcivar.h>
44 1.1 matt #include <dev/pci/pciconf.h>
45 1.22 kiyohara
46 1.1 matt #include <dev/marvell/gtpcireg.h>
47 1.1 matt #include <dev/marvell/gtpcivar.h>
48 1.22 kiyohara #include <dev/marvell/marvellreg.h>
49 1.22 kiyohara #include <dev/marvell/marvellvar.h>
50 1.1 matt
51 1.22 kiyohara #include <machine/pci_machdep.h>
52 1.1 matt
53 1.22 kiyohara #include "locators.h"
54 1.5 matt
55 1.1 matt
56 1.22 kiyohara #define GTPCI_READ(sc, r) \
57 1.22 kiyohara bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit))
58 1.22 kiyohara #define GTPCI_WRITE(sc, r, v) \
59 1.22 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit), (v))
60 1.22 kiyohara #define GTPCI_WRITE_AC(sc, r, n, v) \
61 1.22 kiyohara bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, r((sc)->sc_unit, (n)), (v))
62 1.1 matt
63 1.22 kiyohara
64 1.22 kiyohara static int gtpci_match(device_t, struct cfdata *, void *);
65 1.22 kiyohara static void gtpci_attach(device_t, device_t, void *);
66 1.22 kiyohara
67 1.25 kiyohara static void gtpci_init(struct gtpci_softc *, struct gtpci_prot *);
68 1.22 kiyohara static void gtpci_barinit(struct gtpci_softc *);
69 1.25 kiyohara static void gtpci_protinit(struct gtpci_softc *, struct gtpci_prot *);
70 1.22 kiyohara #if NPCI > 0
71 1.22 kiyohara static void gtpci_pci_config(struct gtpci_softc *, bus_space_tag_t,
72 1.22 kiyohara bus_space_tag_t, bus_dma_tag_t, pci_chipset_tag_t,
73 1.22 kiyohara u_long, u_long, u_long, u_long, int);
74 1.1 matt #endif
75 1.1 matt
76 1.1 matt
77 1.22 kiyohara CFATTACH_DECL_NEW(gtpci_gt, sizeof(struct gtpci_softc),
78 1.22 kiyohara gtpci_match, gtpci_attach, NULL, NULL);
79 1.22 kiyohara CFATTACH_DECL_NEW(gtpci_mbus, sizeof(struct gtpci_softc),
80 1.1 matt gtpci_match, gtpci_attach, NULL, NULL);
81 1.1 matt
82 1.6 matt
83 1.22 kiyohara /* ARGSUSED */
84 1.22 kiyohara static int
85 1.22 kiyohara gtpci_match(device_t parent, struct cfdata *match, void *aux)
86 1.1 matt {
87 1.22 kiyohara struct marvell_attach_args *mva = aux;
88 1.1 matt
89 1.22 kiyohara if (strcmp(mva->mva_name, match->cf_name) != 0)
90 1.22 kiyohara return 0;
91 1.1 matt
92 1.27 kiyohara if (mva->mva_unit == MVA_UNIT_DEFAULT)
93 1.27 kiyohara return 0;
94 1.22 kiyohara switch (mva->mva_model) {
95 1.22 kiyohara case MARVELL_DISCOVERY:
96 1.22 kiyohara case MARVELL_DISCOVERY_II:
97 1.22 kiyohara case MARVELL_DISCOVERY_III:
98 1.22 kiyohara #if 0 /* XXXXX */
99 1.22 kiyohara case MARVELL_DISCOVERY_LT:
100 1.22 kiyohara case MARVELL_DISCOVERY_V:
101 1.22 kiyohara case MARVELL_DISCOVERY_VI:
102 1.22 kiyohara #endif
103 1.27 kiyohara if (mva->mva_offset != MVA_OFFSET_DEFAULT)
104 1.22 kiyohara return 0;
105 1.22 kiyohara }
106 1.1 matt
107 1.22 kiyohara mva->mva_size = GTPCI_SIZE;
108 1.22 kiyohara return 1;
109 1.1 matt }
110 1.1 matt
111 1.22 kiyohara /* ARGSUSED */
112 1.22 kiyohara static void
113 1.21 cegger gtpci_attach(device_t parent, device_t self, void *aux)
114 1.1 matt {
115 1.22 kiyohara struct gtpci_softc *sc = device_private(self);
116 1.22 kiyohara struct marvell_attach_args *mva = aux;
117 1.25 kiyohara struct gtpci_prot *gtpci_prot;
118 1.25 kiyohara prop_dictionary_t dict = device_properties(self);
119 1.25 kiyohara prop_object_t prot;
120 1.22 kiyohara #if NPCI > 0
121 1.22 kiyohara prop_object_t pc, iot, memt;
122 1.22 kiyohara prop_array_t int2gpp;
123 1.22 kiyohara prop_object_t gpp;
124 1.22 kiyohara pci_chipset_tag_t gtpci_chipset;
125 1.22 kiyohara bus_space_tag_t gtpci_io_bs_tag, gtpci_mem_bs_tag;
126 1.22 kiyohara uint64_t iostart = 0, ioend = 0, memstart = 0, memend = 0;
127 1.22 kiyohara int cl_size = 0, intr;
128 1.22 kiyohara #endif
129 1.22 kiyohara
130 1.22 kiyohara aprint_normal(": Marvell PCI Interface\n");
131 1.22 kiyohara aprint_naive("\n");
132 1.22 kiyohara
133 1.25 kiyohara prot = prop_dictionary_get(dict, "prot");
134 1.25 kiyohara if (prot != NULL) {
135 1.25 kiyohara KASSERT(prop_object_type(prot) == PROP_TYPE_DATA);
136 1.25 kiyohara gtpci_prot = __UNCONST(prop_data_data_nocopy(prot));
137 1.25 kiyohara } else {
138 1.25 kiyohara aprint_verbose_dev(self, "no protection property\n");
139 1.25 kiyohara gtpci_prot = NULL;
140 1.25 kiyohara }
141 1.22 kiyohara #if NPCI > 0
142 1.22 kiyohara iot = prop_dictionary_get(dict, "io-bus-tag");
143 1.24 kiyohara if (iot != NULL) {
144 1.24 kiyohara KASSERT(prop_object_type(iot) == PROP_TYPE_DATA);
145 1.24 kiyohara gtpci_io_bs_tag = __UNCONST(prop_data_data_nocopy(iot));
146 1.24 kiyohara } else {
147 1.22 kiyohara aprint_error_dev(self, "no io-bus-tag property\n");
148 1.24 kiyohara gtpci_io_bs_tag = NULL;
149 1.24 kiyohara }
150 1.22 kiyohara memt = prop_dictionary_get(dict, "mem-bus-tag");
151 1.24 kiyohara if (memt != NULL) {
152 1.24 kiyohara KASSERT(prop_object_type(memt) == PROP_TYPE_DATA);
153 1.24 kiyohara gtpci_mem_bs_tag = __UNCONST(prop_data_data_nocopy(memt));
154 1.24 kiyohara } else {
155 1.22 kiyohara aprint_error_dev(self, "no mem-bus-tag property\n");
156 1.24 kiyohara gtpci_mem_bs_tag = NULL;
157 1.24 kiyohara }
158 1.22 kiyohara pc = prop_dictionary_get(dict, "pci-chipset");
159 1.22 kiyohara if (pc == NULL) {
160 1.22 kiyohara aprint_error_dev(self, "no pci-chipset property\n");
161 1.22 kiyohara return;
162 1.22 kiyohara }
163 1.22 kiyohara KASSERT(prop_object_type(pc) == PROP_TYPE_DATA);
164 1.22 kiyohara gtpci_chipset = __UNCONST(prop_data_data_nocopy(pc));
165 1.22 kiyohara #ifdef PCI_NETBSD_CONFIGURE
166 1.22 kiyohara if (!prop_dictionary_get_uint64(dict, "iostart", &iostart)) {
167 1.22 kiyohara aprint_error_dev(self, "no iostart property\n");
168 1.22 kiyohara return;
169 1.22 kiyohara }
170 1.22 kiyohara if (!prop_dictionary_get_uint64(dict, "ioend", &ioend)) {
171 1.22 kiyohara aprint_error_dev(self, "no ioend property\n");
172 1.22 kiyohara return;
173 1.22 kiyohara }
174 1.22 kiyohara if (!prop_dictionary_get_uint64(dict, "memstart", &memstart)) {
175 1.22 kiyohara aprint_error_dev(self, "no memstart property\n");
176 1.22 kiyohara return;
177 1.22 kiyohara }
178 1.22 kiyohara if (!prop_dictionary_get_uint64(dict, "memend", &memend)) {
179 1.22 kiyohara aprint_error_dev(self, "no memend property\n");
180 1.22 kiyohara return;
181 1.22 kiyohara }
182 1.22 kiyohara if (!prop_dictionary_get_uint32(dict, "cache-line-size", &cl_size)) {
183 1.22 kiyohara aprint_error_dev(self, "no cache-line-size property\n");
184 1.22 kiyohara return;
185 1.1 matt }
186 1.22 kiyohara #endif
187 1.22 kiyohara #endif
188 1.1 matt
189 1.22 kiyohara sc->sc_dev = self;
190 1.22 kiyohara sc->sc_model = mva->mva_model;
191 1.22 kiyohara sc->sc_rev = mva->mva_revision;
192 1.22 kiyohara sc->sc_unit = mva->mva_unit;
193 1.22 kiyohara sc->sc_iot = mva->mva_iot;
194 1.22 kiyohara if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
195 1.26 kiyohara (mva->mva_offset != MVA_OFFSET_DEFAULT) ? mva->mva_offset : 0,
196 1.22 kiyohara mva->mva_size, &sc->sc_ioh)) {
197 1.22 kiyohara aprint_error_dev(self, "can't map registers\n");
198 1.2 matt return;
199 1.2 matt }
200 1.22 kiyohara sc->sc_pc = gtpci_chipset;
201 1.25 kiyohara gtpci_init(sc, gtpci_prot);
202 1.2 matt
203 1.22 kiyohara #if NPCI > 0
204 1.22 kiyohara int2gpp = prop_dictionary_get(dict, "int2gpp");
205 1.22 kiyohara if (int2gpp != NULL) {
206 1.22 kiyohara if (prop_object_type(int2gpp) != PROP_TYPE_ARRAY) {
207 1.22 kiyohara aprint_error_dev(self, "int2gpp not an array\n");
208 1.22 kiyohara return;
209 1.22 kiyohara }
210 1.22 kiyohara aprint_normal_dev(self, "use intrrupt pin:");
211 1.22 kiyohara for (intr = PCI_INTERRUPT_PIN_A;
212 1.22 kiyohara intr <= PCI_INTERRUPT_PIN_D &&
213 1.22 kiyohara intr < prop_array_count(int2gpp);
214 1.22 kiyohara intr++) {
215 1.22 kiyohara gpp = prop_array_get(int2gpp, intr);
216 1.22 kiyohara if (prop_object_type(gpp) != PROP_TYPE_NUMBER) {
217 1.22 kiyohara aprint_error_dev(self,
218 1.22 kiyohara "int2gpp[%d] not an number\n", intr);
219 1.22 kiyohara return;
220 1.22 kiyohara }
221 1.22 kiyohara aprint_normal(" %d",
222 1.22 kiyohara (int)prop_number_integer_value(gpp));
223 1.22 kiyohara }
224 1.22 kiyohara aprint_normal("\n");
225 1.22 kiyohara }
226 1.2 matt
227 1.22 kiyohara gtpci_pci_config(sc, gtpci_io_bs_tag, gtpci_mem_bs_tag, mva->mva_dmat,
228 1.22 kiyohara gtpci_chipset, iostart, ioend, memstart, memend, cl_size);
229 1.22 kiyohara #endif
230 1.22 kiyohara }
231 1.1 matt
232 1.22 kiyohara static void
233 1.25 kiyohara gtpci_init(struct gtpci_softc *sc, struct gtpci_prot *prot)
234 1.22 kiyohara {
235 1.22 kiyohara uint32_t reg;
236 1.1 matt
237 1.22 kiyohara /* First, all disable. Also WA CQ 4382 (bit15 must set 1)*/
238 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_BARE, GTPCI_BARE_ALLDISABLE | (1 << 15));
239 1.1 matt
240 1.22 kiyohara /* Enable Internal Arbiter */
241 1.22 kiyohara reg = GTPCI_READ(sc, GTPCI_AC);
242 1.22 kiyohara reg |= GTPCI_AC_EN;
243 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_AC, reg);
244 1.22 kiyohara
245 1.22 kiyohara gtpci_barinit(sc);
246 1.25 kiyohara if (prot != NULL)
247 1.25 kiyohara gtpci_protinit(sc, prot);
248 1.22 kiyohara
249 1.22 kiyohara reg = GTPCI_READ(sc, GTPCI_ADC);
250 1.22 kiyohara reg |= GTPCI_ADC_REMAPWRDIS;
251 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_ADC, reg);
252 1.22 kiyohara
253 1.22 kiyohara /* enable CPU-2-PCI ordering */
254 1.22 kiyohara reg = GTPCI_READ(sc, GTPCI_C);
255 1.22 kiyohara reg |= GTPCI_C_CPU2PCIORDERING;
256 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_C, reg);
257 1.22 kiyohara }
258 1.22 kiyohara
259 1.22 kiyohara static void
260 1.22 kiyohara gtpci_barinit(struct gtpci_softc *sc)
261 1.22 kiyohara {
262 1.22 kiyohara static const struct {
263 1.22 kiyohara int tag;
264 1.22 kiyohara int bars[2]; /* BAR Size registers */
265 1.22 kiyohara int bare; /* Bits of Base Address Registers Enable */
266 1.22 kiyohara int func;
267 1.22 kiyohara int balow;
268 1.22 kiyohara int bahigh;
269 1.22 kiyohara } maps[] = {
270 1.22 kiyohara { MARVELL_TAG_SDRAM_CS0,
271 1.22 kiyohara { GTPCI_CS0BARS(0), GTPCI_CS0BARS(1) },
272 1.22 kiyohara GTPCI_BARE_CS0EN, 0, 0x10, 0x14 },
273 1.22 kiyohara { MARVELL_TAG_SDRAM_CS1,
274 1.22 kiyohara { GTPCI_CS1BARS(0), GTPCI_CS1BARS(1) },
275 1.22 kiyohara GTPCI_BARE_CS1EN, 0, 0x18, 0x1c },
276 1.22 kiyohara { MARVELL_TAG_SDRAM_CS2,
277 1.22 kiyohara { GTPCI_CS2BARS(0), GTPCI_CS2BARS(1) },
278 1.22 kiyohara GTPCI_BARE_CS2EN, 1, 0x10, 0x14 },
279 1.22 kiyohara { MARVELL_TAG_SDRAM_CS3,
280 1.22 kiyohara { GTPCI_CS3BARS(0), GTPCI_CS3BARS(1) },
281 1.22 kiyohara GTPCI_BARE_CS3EN, 1, 0x18, 0x1c },
282 1.22 kiyohara #if 0
283 1.22 kiyohara { ORION_TARGETID_INTERNALREG,
284 1.22 kiyohara { -1, -1 },
285 1.22 kiyohara GTPCI_BARE_INTMEMEN, 0, 0x20, 0x24 },
286 1.22 kiyohara
287 1.22 kiyohara { ORION_TARGETID_DEVICE_CS0,
288 1.22 kiyohara { GTPCI_DCS0BARS(0), GTPCI_DCS0BARS(1) },
289 1.22 kiyohara GTPCI_BARE_DEVCS0EN, 2, 0x10, 0x14 },
290 1.22 kiyohara { ORION_TARGETID_DEVICE_CS1,
291 1.22 kiyohara { GTPCI_DCS1BARS(0), GTPCI_DCS1BARS(1) },
292 1.22 kiyohara GTPCI_BARE_DEVCS1EN, 2, 0x18, 0x1c },
293 1.22 kiyohara { ORION_TARGETID_DEVICE_CS2,
294 1.22 kiyohara { GTPCI_DCS2BARS(0), GTPCI_DCS2BARS(1) },
295 1.22 kiyohara GTPCI_BARE_DEVCS2EN, 2, 0x20, 0x24 },
296 1.22 kiyohara { ORION_TARGETID_DEVICE_BOOTCS,
297 1.22 kiyohara { GTPCI_BCSBARS(0), GTPCI_BCSBARS(1) },
298 1.22 kiyohara GTPCI_BARE_BOOTCSEN, 3, 0x18, 0x1c },
299 1.22 kiyohara { P2P Mem0 BAR,
300 1.22 kiyohara { GTPCI_P2PM0BARS(0), GTPCI_P2PM0BARS(1) },
301 1.22 kiyohara GTPCI_BARE_P2PMEM0EN, 4, 0x10, 0x14 },
302 1.22 kiyohara { P2P I/O BAR,
303 1.22 kiyohara { GTPCI_P2PIOBARS(0), GTPCI_P2PIOBARS(1) },
304 1.22 kiyohara GTPCI_BARE_P2PIO0EN, 4, 0x20, 0x24 },
305 1.22 kiyohara { Expansion ROM BAR,
306 1.22 kiyohara { GTPCI_EROMBARS(0), GTPCI_EROMBARS(1) },
307 1.22 kiyohara 0, },
308 1.22 kiyohara #endif
309 1.1 matt
310 1.22 kiyohara { MARVELL_TAG_UNDEFINED,
311 1.22 kiyohara { -1, -1 },
312 1.22 kiyohara -1, -1, 0x00, 0x00 },
313 1.22 kiyohara };
314 1.22 kiyohara device_t pdev = device_parent(sc->sc_dev);
315 1.22 kiyohara uint64_t base;
316 1.31 mrg uint32_t size, bare;
317 1.31 mrg int map, rv;
318 1.22 kiyohara
319 1.22 kiyohara
320 1.22 kiyohara bare = GTPCI_BARE_ALLDISABLE;
321 1.22 kiyohara for (map = 0; maps[map].tag != MARVELL_TAG_UNDEFINED; map++) {
322 1.22 kiyohara rv = marvell_winparams_by_tag(pdev, maps[map].tag, NULL, NULL,
323 1.22 kiyohara &base, &size);
324 1.22 kiyohara if (rv != 0 || size == 0)
325 1.22 kiyohara continue;
326 1.22 kiyohara
327 1.22 kiyohara if (maps[map].bars[sc->sc_unit] != -1)
328 1.22 kiyohara bus_space_write_4(sc->sc_iot, sc->sc_ioh,
329 1.22 kiyohara maps[map].bars[sc->sc_unit], GTPCI_BARSIZE(size));
330 1.22 kiyohara bare &= ~maps[map].bare;
331 1.22 kiyohara
332 1.22 kiyohara #if 0 /* shall move to pchb(4)? */
333 1.22 kiyohara if (maps[map].func != -1) {
334 1.22 kiyohara pcitag_t tag;
335 1.22 kiyohara pcireg_t reg;
336 1.31 mrg int dev = GTPCI_P2PC_DEVNUM(p2pc);
337 1.31 mrg int bus = GTPCI_P2PC_BUSNUMBER(p2pc);
338 1.31 mrg uint32_t p2pc = GTPCI_READ(sc, GTPCI_P2PC);
339 1.22 kiyohara
340 1.22 kiyohara tag = gtpci_make_tag(NULL, bus, dev, maps[map].func);
341 1.22 kiyohara reg = gtpci_conf_read(sc, tag, maps[map].balow);
342 1.22 kiyohara reg &= ~GTPCI_BARLOW_MASK;
343 1.22 kiyohara reg |= GTPCI_BARLOW_BASE(base);
344 1.22 kiyohara gtpci_conf_write(sc, tag, maps[map].balow, reg);
345 1.22 kiyohara reg = gtpci_conf_read(sc, tag, maps[map].bahigh);
346 1.22 kiyohara reg = (base >> 16) >> 16;
347 1.22 kiyohara gtpci_conf_write(sc, tag, maps[map].bahigh, reg);
348 1.6 matt }
349 1.22 kiyohara #endif
350 1.9 scw }
351 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_BARE, bare);
352 1.5 matt }
353 1.5 matt
354 1.22 kiyohara static void
355 1.25 kiyohara gtpci_protinit(struct gtpci_softc *sc, struct gtpci_prot *ac_flags)
356 1.1 matt {
357 1.22 kiyohara enum {
358 1.25 kiyohara gt642xx = 0,
359 1.25 kiyohara mv643xx,
360 1.25 kiyohara arm_soc,
361 1.22 kiyohara };
362 1.25 kiyohara const struct gtpci_ac_rshift {
363 1.25 kiyohara uint32_t base_rshift;
364 1.25 kiyohara uint32_t size_rshift;
365 1.25 kiyohara } ac_rshifts[] = {
366 1.25 kiyohara { 20, 20, }, /* GT642xx */
367 1.25 kiyohara { 0, 0, }, /* MV643xx and after */
368 1.25 kiyohara { 0, 0, }, /* ARM SoC */
369 1.22 kiyohara };
370 1.22 kiyohara const uint32_t prot_tags[] = {
371 1.22 kiyohara MARVELL_TAG_SDRAM_CS0,
372 1.22 kiyohara MARVELL_TAG_SDRAM_CS1,
373 1.22 kiyohara MARVELL_TAG_SDRAM_CS2,
374 1.22 kiyohara MARVELL_TAG_SDRAM_CS3,
375 1.22 kiyohara MARVELL_TAG_UNDEFINED
376 1.22 kiyohara };
377 1.22 kiyohara device_t pdev = device_parent(sc->sc_dev);
378 1.22 kiyohara uint64_t acbase, base;
379 1.22 kiyohara uint32_t acsize, size;
380 1.25 kiyohara int base_rshift, size_rshift, acbl_flags, acs_flags;
381 1.22 kiyohara int prot, rv, p, t;
382 1.22 kiyohara
383 1.22 kiyohara switch (sc->sc_model) {
384 1.22 kiyohara case MARVELL_DISCOVERY:
385 1.25 kiyohara p = gt642xx;
386 1.22 kiyohara break;
387 1.1 matt
388 1.22 kiyohara case MARVELL_DISCOVERY_II:
389 1.22 kiyohara case MARVELL_DISCOVERY_III:
390 1.3 matt #if 0
391 1.22 kiyohara case MARVELL_DISCOVERY_LT:
392 1.22 kiyohara case MARVELL_DISCOVERY_V:
393 1.22 kiyohara case MARVELL_DISCOVERY_VI:
394 1.3 matt #endif
395 1.25 kiyohara p = mv643xx;
396 1.22 kiyohara break;
397 1.1 matt
398 1.22 kiyohara default:
399 1.25 kiyohara p = arm_soc;
400 1.22 kiyohara break;
401 1.22 kiyohara }
402 1.25 kiyohara base_rshift = ac_rshifts[p].base_rshift;
403 1.25 kiyohara size_rshift = ac_rshifts[p].size_rshift;
404 1.25 kiyohara acbl_flags = ac_flags->acbl_flags;
405 1.25 kiyohara acs_flags = ac_flags->acs_flags;
406 1.22 kiyohara
407 1.22 kiyohara t = 0;
408 1.22 kiyohara for (prot = 0; prot < GTPCI_NPCIAC; prot++) {
409 1.22 kiyohara acbase = acsize = 0;
410 1.22 kiyohara
411 1.22 kiyohara for ( ; prot_tags[t] != MARVELL_TAG_UNDEFINED; t++) {
412 1.22 kiyohara rv = marvell_winparams_by_tag(pdev, prot_tags[t],
413 1.22 kiyohara NULL, NULL, &base, &size);
414 1.22 kiyohara if (rv != 0 || size == 0)
415 1.22 kiyohara continue;
416 1.22 kiyohara
417 1.22 kiyohara if (acsize == 0 || base + size == acbase)
418 1.22 kiyohara acbase = base;
419 1.22 kiyohara else if (acbase + acsize != base)
420 1.22 kiyohara break;
421 1.22 kiyohara acsize += size;
422 1.6 matt }
423 1.6 matt
424 1.22 kiyohara if (acsize != 0) {
425 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACBL, prot,
426 1.25 kiyohara ((acbase & 0xffffffff) >> base_rshift) | acbl_flags);
427 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACBH, prot,
428 1.22 kiyohara (acbase >> 32) & 0xffffffff);
429 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACS, prot,
430 1.25 kiyohara ((acsize - 1) >> size_rshift) | acs_flags);
431 1.22 kiyohara } else {
432 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACBL, prot, 0);
433 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACBH, prot, 0);
434 1.22 kiyohara GTPCI_WRITE_AC(sc, GTPCI_ACS, prot, 0);
435 1.6 matt }
436 1.6 matt }
437 1.22 kiyohara return;
438 1.22 kiyohara }
439 1.6 matt
440 1.22 kiyohara #if NPCI > 0
441 1.22 kiyohara static void
442 1.22 kiyohara gtpci_pci_config(struct gtpci_softc *sc, bus_space_tag_t iot,
443 1.22 kiyohara bus_space_tag_t memt, bus_dma_tag_t dmat, pci_chipset_tag_t pc,
444 1.22 kiyohara u_long iostart, u_long ioend, u_long memstart, u_long memend,
445 1.22 kiyohara int cacheline_size)
446 1.22 kiyohara {
447 1.22 kiyohara struct pcibus_attach_args pba;
448 1.22 kiyohara uint32_t p2pc, command;
449 1.1 matt
450 1.22 kiyohara p2pc = GTPCI_READ(sc, GTPCI_P2PC);
451 1.1 matt
452 1.22 kiyohara #ifdef PCI_NETBSD_CONFIGURE
453 1.34 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
454 1.34 thorpej
455 1.34 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
456 1.34 thorpej iostart, (ioend - iostart) + 1);
457 1.34 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
458 1.34 thorpej memstart, (memend - memstart) + 1);
459 1.33 chs
460 1.34 thorpej pci_configure_bus(pc, pcires,
461 1.33 chs GTPCI_P2PC_BUSNUMBER(p2pc), cacheline_size);
462 1.33 chs
463 1.34 thorpej pciconf_resource_fini(pcires);
464 1.22 kiyohara #endif
465 1.1 matt
466 1.22 kiyohara pba.pba_iot = iot;
467 1.22 kiyohara pba.pba_memt = memt;
468 1.22 kiyohara pba.pba_dmat = dmat;
469 1.22 kiyohara pba.pba_dmat64 = NULL;
470 1.22 kiyohara pba.pba_pc = pc;
471 1.22 kiyohara if (iot == NULL || memt == NULL) {
472 1.22 kiyohara pba.pba_flags = 0;
473 1.22 kiyohara aprint_error_dev(sc->sc_dev, "");
474 1.22 kiyohara if (iot == NULL)
475 1.22 kiyohara aprint_error("io ");
476 1.22 kiyohara else
477 1.28 dyoung pba.pba_flags |= PCI_FLAGS_IO_OKAY;
478 1.22 kiyohara if (iot == NULL && memt == NULL)
479 1.22 kiyohara aprint_error("and ");
480 1.22 kiyohara if (memt == NULL)
481 1.22 kiyohara aprint_error("mem");
482 1.22 kiyohara else
483 1.28 dyoung pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
484 1.22 kiyohara aprint_error(" access disabled\n");
485 1.22 kiyohara } else
486 1.28 dyoung pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
487 1.22 kiyohara command = GTPCI_READ(sc, GTPCI_C);
488 1.22 kiyohara if (command & GTPCI_C_MRDMUL)
489 1.22 kiyohara pba.pba_flags |= PCI_FLAGS_MRM_OKAY;
490 1.22 kiyohara if (command & GTPCI_C_MRDLINE)
491 1.22 kiyohara pba.pba_flags |= PCI_FLAGS_MRL_OKAY;
492 1.22 kiyohara pba.pba_flags |= PCI_FLAGS_MWI_OKAY;
493 1.22 kiyohara pba.pba_bus = GTPCI_P2PC_BUSNUMBER(p2pc);
494 1.22 kiyohara pba.pba_bridgetag = NULL;
495 1.34.4.1 thorpej config_found(sc->sc_dev, &pba, NULL, CFARG_EOL);
496 1.1 matt }
497 1.1 matt
498 1.1 matt
499 1.22 kiyohara /*
500 1.22 kiyohara * Dependent code of PCI Interface of Marvell
501 1.22 kiyohara */
502 1.22 kiyohara
503 1.22 kiyohara /* ARGSUSED */
504 1.1 matt void
505 1.22 kiyohara gtpci_attach_hook(device_t parent, device_t self,
506 1.22 kiyohara struct pcibus_attach_args *pba)
507 1.1 matt {
508 1.22 kiyohara
509 1.22 kiyohara /* Nothing */
510 1.1 matt }
511 1.1 matt
512 1.1 matt /*
513 1.22 kiyohara * Bit map for configuration register:
514 1.22 kiyohara * [31] ConfigEn
515 1.22 kiyohara * [30:24] Reserved
516 1.22 kiyohara * [23:16] BusNum
517 1.22 kiyohara * [15:11] DevNum
518 1.22 kiyohara * [10: 8] FunctNum
519 1.22 kiyohara * [ 7: 2] RegNum
520 1.22 kiyohara * [ 1: 0] reserved
521 1.1 matt */
522 1.22 kiyohara
523 1.22 kiyohara /* ARGSUSED */
524 1.1 matt int
525 1.22 kiyohara gtpci_bus_maxdevs(void *v, int busno)
526 1.1 matt {
527 1.22 kiyohara
528 1.22 kiyohara return 32; /* 32 device/bus */
529 1.1 matt }
530 1.1 matt
531 1.22 kiyohara /* ARGSUSED */
532 1.1 matt pcitag_t
533 1.22 kiyohara gtpci_make_tag(void *v, int bus, int dev, int func)
534 1.1 matt {
535 1.22 kiyohara
536 1.22 kiyohara #if DIAGNOSTIC
537 1.22 kiyohara if (bus >= 256 || dev >= 32 || func >= 8)
538 1.22 kiyohara panic("pci_make_tag: bad request");
539 1.22 kiyohara #endif
540 1.22 kiyohara
541 1.22 kiyohara return (bus << 16) | (dev << 11) | (func << 8);
542 1.1 matt }
543 1.1 matt
544 1.22 kiyohara /* ARGSUSED */
545 1.1 matt void
546 1.22 kiyohara gtpci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
547 1.1 matt {
548 1.22 kiyohara
549 1.1 matt if (bp != NULL)
550 1.22 kiyohara *bp = (tag >> 16) & 0xff;
551 1.1 matt if (dp != NULL)
552 1.22 kiyohara *dp = (tag >> 11) & 0x1f;
553 1.1 matt if (fp != NULL)
554 1.22 kiyohara *fp = (tag >> 8) & 0x07;
555 1.1 matt }
556 1.1 matt
557 1.1 matt pcireg_t
558 1.22 kiyohara gtpci_conf_read(void *v, pcitag_t tag, int reg)
559 1.1 matt {
560 1.22 kiyohara struct gtpci_softc *sc = v;
561 1.22 kiyohara const pcireg_t addr = tag | reg;
562 1.22 kiyohara
563 1.32 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
564 1.32 msaitoh return -1;
565 1.32 msaitoh
566 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_CA, addr | GTPCI_CA_CONFIGEN);
567 1.22 kiyohara if ((addr | GTPCI_CA_CONFIGEN) != GTPCI_READ(sc, GTPCI_CA))
568 1.22 kiyohara return -1;
569 1.22 kiyohara
570 1.22 kiyohara return GTPCI_READ(sc, GTPCI_CD);
571 1.1 matt }
572 1.1 matt
573 1.1 matt void
574 1.22 kiyohara gtpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
575 1.1 matt {
576 1.22 kiyohara struct gtpci_softc *sc = v;
577 1.22 kiyohara pcireg_t addr = tag | (reg & 0xfc);
578 1.1 matt
579 1.32 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
580 1.32 msaitoh return;
581 1.32 msaitoh
582 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_CA, addr | GTPCI_CA_CONFIGEN);
583 1.22 kiyohara if ((addr | GTPCI_CA_CONFIGEN) != GTPCI_READ(sc, GTPCI_CA))
584 1.22 kiyohara return;
585 1.1 matt
586 1.22 kiyohara GTPCI_WRITE(sc, GTPCI_CD, data);
587 1.1 matt }
588 1.1 matt
589 1.22 kiyohara /* ARGSUSED */
590 1.22 kiyohara int
591 1.30 matt gtpci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
592 1.1 matt {
593 1.22 kiyohara /* Oops, We have two PCI buses. */
594 1.22 kiyohara if (dev == 0 &&
595 1.22 kiyohara PCI_VENDOR(id) == PCI_VENDOR_MARVELL) {
596 1.22 kiyohara switch (PCI_PRODUCT(id)) {
597 1.22 kiyohara case MARVELL_DISCOVERY:
598 1.22 kiyohara case MARVELL_DISCOVERY_II:
599 1.22 kiyohara case MARVELL_DISCOVERY_III:
600 1.22 kiyohara #if 0
601 1.22 kiyohara case MARVELL_DISCOVERY_LT:
602 1.22 kiyohara case MARVELL_DISCOVERY_V:
603 1.22 kiyohara case MARVELL_DISCOVERY_VI:
604 1.22 kiyohara #endif
605 1.22 kiyohara case MARVELL_ORION_1_88F5180N:
606 1.22 kiyohara case MARVELL_ORION_1_88F5181:
607 1.22 kiyohara case MARVELL_ORION_1_88F5182:
608 1.22 kiyohara case MARVELL_ORION_2_88F5281:
609 1.22 kiyohara case MARVELL_ORION_1_88W8660:
610 1.22 kiyohara /* Don't configure us. */
611 1.22 kiyohara return 0;
612 1.22 kiyohara }
613 1.22 kiyohara }
614 1.1 matt
615 1.22 kiyohara return PCI_CONF_DEFAULT;
616 1.1 matt }
617 1.22 kiyohara #endif /* NPCI > 0 */
618