xref: /netbsd-src/sys/arch/mips/adm5120/dev/admpci.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $NetBSD: admpci.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 David Young.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or
7  * without modification, are permitted provided that the following
8  * conditions are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials provided
14  *    with the distribution.
15  * 3. The name of the author may not be used to endorse or promote
16  *    products derived from this software without specific prior
17  *    written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  */
32 /*-
33  * Copyright (c) 2006 Itronix Inc.
34  * All rights reserved.
35  *
36  * Written by Garrett D'Amore for Itronix Inc.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. The name of Itronix Inc. may not be used to endorse
47  *    or promote products derived from this software without specific
48  *    prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 #include "opt_pci.h"
64 #include "pci.h"
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: admpci.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $");
68 
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/systm.h>
73 #include <sys/errno.h>
74 #include <sys/device.h>
75 #include <sys/malloc.h>
76 #include <sys/extent.h>
77 
78 #include <uvm/uvm_extern.h>
79 
80 #include <machine/bus.h>
81 #include <machine/cpu.h>
82 #include <machine/pte.h>
83 
84 #include <dev/pci/pcivar.h>
85 #include <dev/pci/pcireg.h>
86 #include <dev/pci/pciconf.h>
87 
88 #ifdef	PCI_NETBSD_CONFIGURE
89 #include <mips/cache.h>
90 #endif
91 
92 #include <mips/adm5120/include/adm5120_mainbusvar.h>
93 #include <mips/adm5120/include/adm5120reg.h>
94 #include <mips/adm5120/include/adm5120var.h>
95 
96 #ifdef ADMPCI_DEBUG
97 int admpci_debug = 1;
98 #define	ADMPCI_DPRINTF(__fmt, ...)		\
99 do {						\
100 	if (admpci_debug)			\
101 		printf((__fmt), __VA_ARGS__);	\
102 } while (/*CONSTCOND*/0)
103 #else /* !ADMPCI_DEBUG */
104 #define	ADMPCI_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
105 #endif /* ADMPCI_DEBUG */
106 
107 #define	ADMPCI_TAG_BUS_MASK		__BITS(23, 16)
108 /* Bit 11 is reserved.  It selects the AHB-PCI bridge.  Let device 0
109  * be the bridge.  For all other device numbers, let bit[11] == 0.
110  */
111 #define	ADMPCI_TAG_DEVICE_MASK		__BITS(15, 11)
112 #define	ADMPCI_TAG_DEVICE_SUBMASK	__BITS(15, 12)
113 #define	ADMPCI_TAG_DEVICE_BRIDGE	__BIT(11)
114 #define	ADMPCI_TAG_FUNCTION_MASK	__BITS(10, 8)
115 #define	ADMPCI_TAG_REGISTER_MASK	__BITS(7, 0)
116 
117 #define	ADMPCI_MAX_DEVICE
118 
119 struct admpci_softc {
120 	struct device			sc_dev;
121 	struct mips_pci_chipset		sc_pc;
122 
123 	bus_space_tag_t			sc_memt;
124 	bus_space_tag_t			sc_iot;
125 
126 	bus_space_tag_t			sc_conft;
127 	bus_space_handle_t		sc_addrh;
128 	bus_space_handle_t		sc_datah;
129 };
130 
131 int		admpcimatch(struct device *, struct cfdata *, void *);
132 void		admpciattach(struct device *, struct device *, void *);
133 
134 #if NPCI > 0
135 static void admpci_attach_hook(struct device *, struct device *,
136     struct pcibus_attach_args *);
137 static int admpci_bus_maxdevs(void *, int);
138 static pcitag_t admpci_make_tag(void *, int, int, int);
139 static void admpci_decompose_tag(void *, pcitag_t, int *, int *, int *);
140 static pcireg_t admpci_conf_read(void *, pcitag_t, int);
141 static void admpci_conf_write(void *, pcitag_t, int, pcireg_t);
142 static const char *admpci_intr_string(void *, pci_intr_handle_t);
143 static void admpci_conf_interrupt(void *, int, int, int, int, int *);
144 static void *admpci_intr_establish(void *, pci_intr_handle_t, int,
145     int (*)(void *), void *);
146 static void admpci_intr_disestablish(void *, void *);
147 static int admpci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
148 
149 #ifdef	PCI_NETBSD_CONFIGURE
150 static struct extent	*io_ex = NULL;
151 static struct extent	*mem_ex = NULL;
152 #endif	/* PCI_NETBSD_CONFIGURE */
153 
154 #endif	/* NPCI > 0 */
155 
156 CFATTACH_DECL(admpci, sizeof(struct admpci_softc),
157     admpcimatch, admpciattach, NULL, NULL);
158 
159 int admpci_found = 0;
160 
161 /*
162  * Physical PCI addresses are 36-bits long, so we need to have
163  * adequate storage space for them.
164  */
165 #if NPCI > 0
166 #if !defined(_MIPS_PADDR_T_64BIT) && !defined(_LP64)
167 #error	"admpci requires 64 bit paddr_t!"
168 #endif
169 #endif
170 
171 int
172 admpcimatch(struct device *parent, struct cfdata *match, void *aux)
173 {
174 	struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
175 
176 	return !admpci_found && strcmp(ma->ma_name, "admpci") == 0;
177 }
178 
179 void
180 admpciattach(struct device *parent, struct device *self, void *aux)
181 {
182 	struct adm5120_config		*admc = &adm5120_configuration;
183 	struct admpci_softc		*sc = (struct admpci_softc *)self;
184 	struct mainbus_attach_args	*ma = (struct mainbus_attach_args *)aux;
185 #if NPCI > 0
186 	u_long				result;
187 	pcitag_t			tag;
188 	struct pcibus_attach_args	pba;
189 #endif
190 
191 	admpci_found = 1;
192 
193 	sc->sc_conft = ma->ma_obiot;
194 	if (bus_space_map(sc->sc_conft, ADM5120_BASE_PCI_CONFDATA, 4, 0,
195 		&sc->sc_datah) != 0) {
196 		printf(
197 		    "\n%s: unable to map PCI Configuration Data register\n",
198 		    device_xname(&sc->sc_dev));
199 		return;
200 	}
201 	if (bus_space_map(sc->sc_conft, ADM5120_BASE_PCI_CONFADDR, 4, 0,
202 		&sc->sc_addrh) != 0) {
203 		printf(
204 		    "\n%s: unable to map PCI Configuration Address register\n",
205 		    device_xname(&sc->sc_dev));
206 		return;
207 	}
208 
209 	printf(": ADM5120 Host-PCI Bridge, data %lx addr %lx, sc %p\n",
210 	    sc->sc_datah, sc->sc_addrh, (void *)sc);
211 
212 #if NPCI > 0
213 	sc->sc_memt = &admc->pcimem_space;
214 	sc->sc_iot = &admc->pciio_space;
215 
216 	sc->sc_pc.pc_conf_v = sc;
217 	sc->sc_pc.pc_attach_hook = admpci_attach_hook;
218 	sc->sc_pc.pc_bus_maxdevs = admpci_bus_maxdevs;
219 	sc->sc_pc.pc_make_tag = admpci_make_tag;
220 	sc->sc_pc.pc_decompose_tag = admpci_decompose_tag;
221 	sc->sc_pc.pc_conf_read = admpci_conf_read;
222 	sc->sc_pc.pc_conf_write = admpci_conf_write;
223 
224 	sc->sc_pc.pc_intr_v = sc;
225 	sc->sc_pc.pc_intr_map = admpci_intr_map;
226 	sc->sc_pc.pc_intr_string = admpci_intr_string;
227 	sc->sc_pc.pc_intr_establish = admpci_intr_establish;
228 	sc->sc_pc.pc_intr_disestablish = admpci_intr_disestablish;
229 	sc->sc_pc.pc_conf_interrupt = admpci_conf_interrupt;
230 
231 	tag = pci_make_tag(&sc->sc_pc, 0, 0, 0);
232 	ADMPCI_DPRINTF("%s: BAR 0x10 0x%08x\n", __func__,
233 	    pci_conf_read(&sc->sc_pc, tag, PCI_MAPREG_START));
234 
235 #ifdef PCI_NETBSD_CONFIGURE
236 	mem_ex = extent_create("pcimem",
237 	    ADM5120_BOTTOM, ADM5120_TOP,
238 	    M_DEVBUF, NULL, 0, EX_WAITOK);
239 	(void)extent_alloc_subregion(mem_ex,
240 	    ADM5120_BASE_SRAM1, ADM5120_BASE_PCI_MEM - 1,
241 	    ADM5120_BASE_PCI_MEM - ADM5120_BASE_SRAM1,
242 	    ADM5120_BASE_PCI_MEM - ADM5120_BASE_SRAM1,
243 	    0, EX_WAITOK, &result);
244 	(void)extent_alloc_subregion(mem_ex,
245 	    ADM5120_BASE_PCI_IO, ADM5120_TOP,
246 	    ADM5120_TOP - ADM5120_BASE_PCI_IO + 1,
247 	    ADM5120_TOP - ADM5120_BASE_PCI_IO + 1,
248 	    0, EX_WAITOK, &result);
249 
250 	io_ex = extent_create("pciio",
251 	    ADM5120_BASE_PCI_IO, ADM5120_BASE_PCI_CONFADDR - 1,
252 	    M_DEVBUF, NULL, 0, EX_WAITOK);
253 
254 	pci_configure_bus(&sc->sc_pc,
255 	    io_ex, mem_ex, NULL, 0, mips_dcache_align);
256 	extent_destroy(mem_ex);
257 	extent_destroy(io_ex);
258 #endif
259 
260 	pba.pba_iot = sc->sc_iot;
261 	pba.pba_memt = sc->sc_memt;
262 	/* XXX: review dma tag logic */
263 	pba.pba_dmat = ma->ma_dmat;
264 	pba.pba_dmat64 = NULL;
265 	pba.pba_pc = &sc->sc_pc;
266 	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
267 	pba.pba_bus = 0;
268 	pba.pba_bridgetag = NULL;
269 
270 	config_found_ia(self, "pcibus", &pba, pcibusprint);
271 #endif	/* NPCI > 0 */
272 }
273 
274 #if NPCI > 0
275 
276 void
277 admpci_attach_hook(struct device *parent, struct device *self,
278     struct pcibus_attach_args *pba)
279 {
280 }
281 
282 /* There are at most four devices on bus 0.  The ADM5120 has
283  * request/grant lines for 3 PCI devices: 1, 2, and 3.  The host
284  * bridge is device 0.
285  */
286 int
287 admpci_bus_maxdevs(void *v, int bus)
288 {
289 	if (bus == 0)
290 		return 4;
291 
292 	return 1 + __SHIFTOUT_MASK(ADMPCI_TAG_DEVICE_MASK);
293 }
294 
295 pcitag_t
296 admpci_make_tag(void *v, int bus, int device, int function)
297 {
298 	if (bus > __SHIFTOUT_MASK(ADMPCI_TAG_BUS_MASK) ||
299 	    device > __SHIFTOUT_MASK(ADMPCI_TAG_DEVICE_MASK) ||
300 	    function > __SHIFTOUT_MASK(ADMPCI_TAG_FUNCTION_MASK))
301 		panic("%s: bad request", __func__);
302 
303 	return __SHIFTIN(bus, ADMPCI_TAG_BUS_MASK) |
304 	       __SHIFTIN(device, ADMPCI_TAG_DEVICE_MASK) |
305 	       __SHIFTIN(function, ADMPCI_TAG_FUNCTION_MASK);
306 }
307 
308 void
309 admpci_decompose_tag(void *v, pcitag_t tag, int *b, int *d, int *f)
310 {
311 	int bus, device, function;
312 
313 	bus = __SHIFTOUT(tag, ADMPCI_TAG_BUS_MASK);
314 	device = __SHIFTOUT(tag, ADMPCI_TAG_DEVICE_MASK);
315 	function = __SHIFTOUT(tag, ADMPCI_TAG_FUNCTION_MASK);
316 
317 	if (b != NULL)
318 		*b = bus;
319 	if (d != NULL)
320 		*d = device;
321 	if (f != NULL)
322 		*f = function;
323 }
324 
325 static int
326 admpci_tag_to_addr(void *v, pcitag_t tag, int reg, bus_addr_t *addrp)
327 {
328 	int bus, device, function;
329 
330 	KASSERT(addrp != NULL);
331 	/* panics if tag is not well-formed */
332 	admpci_decompose_tag(v, tag, &bus, &device, &function);
333 	if (reg > __SHIFTOUT_MASK(ADMPCI_TAG_REGISTER_MASK))
334 		panic("%s: bad register", __func__);
335 
336 	*addrp = 0x80000000 | tag | __SHIFTIN(reg, ADMPCI_TAG_REGISTER_MASK);
337 
338 	return 0;
339 }
340 
341 static pcireg_t
342 admpci_conf_read(void *v, pcitag_t tag, int reg)
343 {
344 	int s;
345 	struct admpci_softc *sc = (struct admpci_softc *)v;
346 	uint32_t data;
347 	bus_addr_t addr;
348 
349 	ADMPCI_DPRINTF("%s: sc %p tag %lx reg %d\n", __func__, (void *)sc, tag,
350 	    reg);
351 
352 	if (admpci_tag_to_addr(v, tag, reg, &addr) == -1)
353 		return 0xffffffff;
354 
355 	ADMPCI_DPRINTF("%s: sc_addrh %lx sc_datah %lx addr %lx\n", __func__,
356 	    sc->sc_addrh, sc->sc_datah, addr);
357 
358 	s = splhigh();
359 	bus_space_write_4(sc->sc_conft, sc->sc_addrh, 0, addr);
360 	data = bus_space_read_4(sc->sc_conft, sc->sc_datah, 0);
361 	splx(s);
362 
363 	ADMPCI_DPRINTF("%s: read 0x%" PRIx32 "\n", __func__, data);
364 	return data;
365 }
366 
367 void
368 admpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
369 {
370 	int s;
371 	struct admpci_softc	*sc = (struct admpci_softc *)v;
372 	bus_addr_t addr;
373 
374 	ADMPCI_DPRINTF("%s: sc %p tag %lx reg %d\n", __func__, (void *)sc, tag,
375 	    reg);
376 
377 	if (admpci_tag_to_addr(v, tag, reg, &addr) == -1)
378 		return;
379 
380 	ADMPCI_DPRINTF("%s: sc_addrh %lx sc_datah %lx addr %lx\n", __func__,
381 	    sc->sc_addrh, sc->sc_datah, addr);
382 
383 	s = splhigh();
384 	bus_space_write_4(sc->sc_conft, sc->sc_addrh, 0, addr);
385 	bus_space_write_4(sc->sc_conft, sc->sc_datah, 0, data);
386 	splx(s);
387 }
388 
389 const char *
390 admpci_intr_string(void *v, pci_intr_handle_t ih)
391 {
392 	static char name[16];
393 
394 	(void)snprintf(name, sizeof(name), "irq %u", (unsigned)ih);
395 	return name;
396 }
397 
398 void *
399 admpci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
400     int (*handler)(void *), void *arg)
401 {
402 	return adm5120_intr_establish(ih, ipl, handler, arg);
403 }
404 
405 void
406 admpci_intr_disestablish(void *v, void *cookie)
407 {
408 	adm5120_intr_disestablish(cookie);
409 }
410 
411 void
412 admpci_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz, int *iline)
413 {
414 	/*
415 	 * We let the machdep_pci_intr_map take care of IRQ routing.
416 	 * On some platforms the BIOS may have handled this properly,
417 	 * on others it might not have.  For now we avoid clobbering
418 	 * the settings establishsed by the BIOS, so that they will be
419 	 * there if the platform logic is confident that it can rely
420 	 * on them.
421 	 */
422 }
423 
424 /*
425  * Map the bus 0 device numbers 1, 2, and 3 to IRQ 6, 7, and 8,
426  * respectively.
427  *
428  * XXX How to handle bridges?
429  */
430 static int
431 admpci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
432 {
433 	int bus, device, function;
434 
435 	admpci_decompose_tag(pa->pa_pc->pc_conf_v, pa->pa_tag,
436 	    &bus, &device, &function);
437 
438 	if (bus != 0 || device > 3)
439 		return -1;
440 
441 	*ihp = (device - 1) + 6;
442 
443 	return 0;
444 }
445 #endif
446