vlpci.c revision 1.10 1 1.10 thorpej /* $NetBSD: vlpci.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $ */
2 1.1 jakllsch
3 1.1 jakllsch /*
4 1.1 jakllsch * Copyright (c) 2017 Jonathan A. Kollasch
5 1.1 jakllsch * All rights reserved.
6 1.1 jakllsch *
7 1.1 jakllsch * Redistribution and use in source and binary forms, with or without
8 1.1 jakllsch * modification, are permitted provided that the following conditions
9 1.1 jakllsch * are met:
10 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright
11 1.1 jakllsch * notice, this list of conditions and the following disclaimer.
12 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the
14 1.1 jakllsch * documentation and/or other materials provided with the distribution.
15 1.1 jakllsch *
16 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.1 jakllsch * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jakllsch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jakllsch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 1.1 jakllsch * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.1 jakllsch * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1 jakllsch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.1 jakllsch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 jakllsch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1 jakllsch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.1 jakllsch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 jakllsch */
28 1.1 jakllsch
29 1.1 jakllsch #include <sys/cdefs.h>
30 1.10 thorpej __KERNEL_RCSID(0, "$NetBSD: vlpci.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $");
31 1.1 jakllsch
32 1.1 jakllsch #include "opt_pci.h"
33 1.1 jakllsch #include "pci.h"
34 1.1 jakllsch
35 1.1 jakllsch #include <sys/param.h>
36 1.1 jakllsch #include <sys/bus.h>
37 1.1 jakllsch #include <sys/device.h>
38 1.1 jakllsch #include <sys/extent.h>
39 1.1 jakllsch #include <sys/mutex.h>
40 1.5 macallan #include <uvm/uvm.h>
41 1.5 macallan #include <machine/pio.h>
42 1.5 macallan #include <machine/pmap.h>
43 1.5 macallan #include <machine/ofw.h>
44 1.1 jakllsch
45 1.1 jakllsch #include <dev/isa/isavar.h>
46 1.1 jakllsch
47 1.1 jakllsch #include <dev/ofw/openfirm.h>
48 1.1 jakllsch
49 1.1 jakllsch #include <dev/pci/pcivar.h>
50 1.1 jakllsch #include <dev/pci/pciconf.h>
51 1.1 jakllsch #include <arm/pci_machdep.h>
52 1.1 jakllsch
53 1.8 flxd #include <shark/ofw/vlpci.h>
54 1.8 flxd
55 1.8 flxd #define VLPCI_ADDON_DEV_NO 6
56 1.8 flxd #define VLPCI_IRQ 10
57 1.8 flxd
58 1.8 flxd #define VLPCI_PCI_MEM_BASE 0x02000000
59 1.8 flxd #define VLPCI_PCI_MEM_SZ 1048576
60 1.8 flxd
61 1.8 flxd #define VLPCI_VL_MEM_BASE 0x08000000
62 1.8 flxd #define VLPCI_VL_MEM_SZ 4194304
63 1.8 flxd
64 1.1 jakllsch static int vlpci_match(device_t, struct cfdata *, void *);
65 1.1 jakllsch static void vlpci_attach(device_t, device_t, void *);
66 1.1 jakllsch
67 1.1 jakllsch static void vlpci_pc_attach_hook(device_t, device_t,
68 1.1 jakllsch struct pcibus_attach_args *);
69 1.1 jakllsch static int vlpci_pc_bus_maxdevs(void *, int);
70 1.1 jakllsch static pcitag_t vlpci_pc_make_tag(void *, int, int, int);
71 1.1 jakllsch static void vlpci_pc_decompose_tag(void *, pcitag_t, int *, int *, int *);
72 1.1 jakllsch static pcireg_t vlpci_pc_conf_read(void *, pcitag_t, int);
73 1.1 jakllsch static void vlpci_pc_conf_write(void *, pcitag_t, int, pcireg_t);
74 1.4 jakllsch
75 1.4 jakllsch static int vlpci_pc_intr_map(const struct pci_attach_args *,
76 1.4 jakllsch pci_intr_handle_t *);
77 1.4 jakllsch static const char * vlpci_pc_intr_string(void *, pci_intr_handle_t, char *,
78 1.4 jakllsch size_t);
79 1.4 jakllsch static const struct evcnt * vlpci_pc_intr_evcnt(void *, pci_intr_handle_t);
80 1.4 jakllsch static void * vlpci_pc_intr_establish(void *, pci_intr_handle_t, int,
81 1.9 macallan int (*)(void *), void *, const char *);
82 1.4 jakllsch static void vlpci_pc_intr_disestablish(void *, void *);
83 1.4 jakllsch
84 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
85 1.1 jakllsch static int vlpci_pc_conf_hook(void *, int, int, int, pcireg_t);
86 1.1 jakllsch #endif
87 1.1 jakllsch static void vlpci_pc_conf_interrupt(void *, int, int, int, int, int *);
88 1.1 jakllsch
89 1.1 jakllsch struct vlpci_softc {
90 1.1 jakllsch device_t sc_dev;
91 1.1 jakllsch kmutex_t sc_lock;
92 1.1 jakllsch bus_space_handle_t sc_conf_ioh;
93 1.1 jakllsch bus_space_handle_t sc_reg_ioh;
94 1.1 jakllsch struct arm32_pci_chipset sc_pc;
95 1.1 jakllsch };
96 1.1 jakllsch
97 1.1 jakllsch CFATTACH_DECL_NEW(vlpci, sizeof(struct vlpci_softc),
98 1.1 jakllsch vlpci_match, vlpci_attach, NULL, NULL);
99 1.1 jakllsch
100 1.10 thorpej static const struct device_compatible_entry compat_data[] = {
101 1.10 thorpej { .compat = "via,vt82c505" },
102 1.10 thorpej DEVICE_COMPAT_EOL
103 1.10 thorpej };
104 1.1 jakllsch
105 1.5 macallan vaddr_t vlpci_mem_vaddr = 0;
106 1.5 macallan paddr_t vlpci_mem_paddr;
107 1.5 macallan struct bus_space vlpci_memt;
108 1.5 macallan
109 1.1 jakllsch static void
110 1.1 jakllsch regwrite_1(struct vlpci_softc * const sc, uint8_t off, uint8_t val)
111 1.1 jakllsch {
112 1.7 flxd
113 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
114 1.8 flxd bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_IDX_OFF,
115 1.8 flxd off);
116 1.8 flxd bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_DATA_OFF,
117 1.8 flxd val);
118 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
119 1.1 jakllsch }
120 1.1 jakllsch
121 1.5 macallan static uint8_t
122 1.5 macallan regread_1(struct vlpci_softc * const sc, uint8_t off)
123 1.5 macallan {
124 1.5 macallan uint8_t reg;
125 1.5 macallan
126 1.5 macallan mutex_spin_enter(&sc->sc_lock);
127 1.8 flxd bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, VLPCI_INTREG_IDX_OFF,
128 1.8 flxd off);
129 1.8 flxd reg = bus_space_read_1(&isa_io_bs_tag, sc->sc_reg_ioh,
130 1.8 flxd VLPCI_INTREG_DATA_OFF);
131 1.5 macallan mutex_spin_exit(&sc->sc_lock);
132 1.5 macallan return reg;
133 1.5 macallan }
134 1.5 macallan
135 1.5 macallan static void
136 1.5 macallan vlpci_dump_window(struct vlpci_softc *sc, int num)
137 1.5 macallan {
138 1.8 flxd int regaddr = VLPCI_PCI_WND_HIADDR_REG(num);
139 1.5 macallan uint32_t addr, size;
140 1.5 macallan uint8_t attr;
141 1.5 macallan
142 1.5 macallan addr = regread_1(sc, regaddr) << 24;
143 1.5 macallan addr |= regread_1(sc, regaddr + 1) << 16;
144 1.5 macallan attr = regread_1(sc, regaddr + 2);
145 1.8 flxd size = 0x00010000 << __SHIFTOUT(attr, VLPCI_PCI_WND_ATTR_SZ);
146 1.7 flxd printf("memory window #%d at %08x size %08x flags %x\n", num, addr,
147 1.7 flxd size, attr);
148 1.5 macallan }
149 1.5 macallan
150 1.5 macallan static int
151 1.7 flxd vlpci_map(void *t, bus_addr_t bpa, bus_size_t size, int cacheable,
152 1.7 flxd bus_space_handle_t *bshp)
153 1.5 macallan {
154 1.7 flxd
155 1.8 flxd *bshp = vlpci_mem_vaddr - VLPCI_PCI_MEM_BASE + bpa;
156 1.7 flxd printf("%s: %08lx -> %08lx\n", __func__, bpa, *bshp);
157 1.5 macallan return(0);
158 1.5 macallan }
159 1.5 macallan
160 1.5 macallan static paddr_t
161 1.5 macallan vlpci_mmap(void *cookie, bus_addr_t addr, off_t off, int prot,
162 1.5 macallan int flags)
163 1.5 macallan {
164 1.5 macallan paddr_t ret;
165 1.5 macallan
166 1.5 macallan ret = vlpci_mem_paddr + addr + off;
167 1.5 macallan
168 1.7 flxd if (flags & BUS_SPACE_MAP_PREFETCHABLE)
169 1.5 macallan return (arm_btop(ret) | ARM32_MMAP_WRITECOMBINE);
170 1.7 flxd else
171 1.7 flxd return arm_btop(ret);
172 1.5 macallan }
173 1.5 macallan
174 1.8 flxd static void
175 1.8 flxd vlpci_steer_irq(struct vlpci_softc * const sc)
176 1.8 flxd {
177 1.8 flxd const unsigned int_ctl[] = {
178 1.8 flxd VLPCI_INT_CTL_INTA, VLPCI_INT_CTL_INTB,
179 1.8 flxd VLPCI_INT_CTL_INTC, VLPCI_INT_CTL_INTD
180 1.8 flxd };
181 1.8 flxd uint8_t val;
182 1.8 flxd
183 1.8 flxd for (size_t i = 0; i < __arraycount(int_ctl); i++) {
184 1.8 flxd val = regread_1(sc, VLPCI_INT_CTL_REG(int_ctl[i]));
185 1.8 flxd val &= ~VLPCI_INT_CTL_INT2IRQ(int_ctl[i]);
186 1.8 flxd val |= VLPCI_INT_CTL_ENA(int_ctl[i]);
187 1.8 flxd val |= __SHIFTIN(VLPCI_INT_CTL_IRQ(VLPCI_IRQ),
188 1.8 flxd VLPCI_INT_CTL_INT2IRQ(int_ctl[i]));
189 1.8 flxd regwrite_1(sc, VLPCI_INT_CTL_REG(int_ctl[i]), val);
190 1.8 flxd }
191 1.8 flxd }
192 1.8 flxd
193 1.1 jakllsch static int
194 1.1 jakllsch vlpci_match(device_t parent, struct cfdata *match, void *aux)
195 1.1 jakllsch {
196 1.1 jakllsch struct ofbus_attach_args * const oba = aux;
197 1.1 jakllsch
198 1.10 thorpej /* beat generic ofbus */
199 1.10 thorpej return of_compatible_match(oba->oba_phandle, compat_data) * 2;
200 1.1 jakllsch }
201 1.1 jakllsch
202 1.1 jakllsch static void
203 1.1 jakllsch vlpci_attach(device_t parent, device_t self, void *aux)
204 1.1 jakllsch {
205 1.1 jakllsch struct vlpci_softc * const sc = device_private(self);
206 1.1 jakllsch pci_chipset_tag_t const pc = &sc->sc_pc;
207 1.1 jakllsch struct pcibus_attach_args pba;
208 1.8 flxd pcitag_t tag;
209 1.8 flxd pcireg_t cmd;
210 1.1 jakllsch
211 1.1 jakllsch aprint_normal("\n");
212 1.1 jakllsch
213 1.1 jakllsch sc->sc_dev = self;
214 1.1 jakllsch mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
215 1.1 jakllsch memset(&pba, 0, sizeof(pba));
216 1.1 jakllsch
217 1.8 flxd if (bus_space_map(&isa_io_bs_tag, VLPCI_INTREG_BASE, VLPCI_INTREG_SZ,
218 1.1 jakllsch 0, &sc->sc_reg_ioh) != 0) {
219 1.8 flxd aprint_error_dev(self, "failed to map internal reg port\n");
220 1.1 jakllsch return;
221 1.1 jakllsch }
222 1.8 flxd if (bus_space_map(&isa_io_bs_tag, VLPCI_CFGREG_BASE, VLPCI_CFGREG_SZ,
223 1.1 jakllsch 0, &sc->sc_conf_ioh) != 0) {
224 1.8 flxd aprint_error_dev(self, "failed to map configuration port\n");
225 1.1 jakllsch return;
226 1.1 jakllsch }
227 1.1 jakllsch
228 1.1 jakllsch /* Enable VLB/PCI bridge */
229 1.8 flxd regwrite_1(sc, VLPCI_MISC_1_REG, VLPCI_MISC_1_LOCAL_PIN |
230 1.8 flxd VLPCI_MISC_1_COMPAT_ISA_BOFF);
231 1.8 flxd regwrite_1(sc, VLPCI_MISC_CTL_REG, __SHIFTIN(VLPCI_MISC_CTL_HIADDR_DIS,
232 1.8 flxd VLPCI_MISC_CTL_HIADDR) | VLPCI_MISC_CTL_IOCHCK_PIN);
233 1.8 flxd regwrite_1(sc, VLPCI_CFG_MISC_CTL_REG,
234 1.8 flxd __SHIFTIN(VLPCI_CFG_MISC_CTL_INT_CTL_CONV,
235 1.8 flxd VLPCI_CFG_MISC_CTL_INT_CTL) | VLPCI_CFG_MISC_CTL_LREQI_LGNTO_PIN);
236 1.8 flxd regwrite_1(sc, VLPCI_IRQ_MODE_REG, 0x00); /* don't do per-INTx conversions */
237 1.8 flxd vlpci_steer_irq(sc);
238 1.5 macallan /*
239 1.5 macallan * XXX
240 1.5 macallan * set memory size to 255MB, so the bridge knows which cycles go to RAM
241 1.5 macallan * shark's RAM is in the upper half of the lower 256MB, part of the
242 1.5 macallan * lower half is occupied by the graphics chip
243 1.5 macallan * ... or that's the theory. OF puts PCI BARS at 0x02000000 which
244 1.5 macallan * overlaps with when we do this and pci memory access doesn't work.
245 1.5 macallan */
246 1.8 flxd regwrite_1(sc, VLPCI_OBD_MEM_SZ_REG, 1);
247 1.5 macallan
248 1.8 flxd regwrite_1(sc, VLPCI_BUF_CTL_REG, VLPCI_BUF_CTL_PCI_DYN_ACC_DEC);
249 1.8 flxd regwrite_1(sc, VLPCI_VL_TIM_REG, VLPCI_VL_TIM_OBD_MEM_1ST_DAT);
250 1.8 flxd printf("reg 0x83 %02x\n", regread_1(sc, VLPCI_VL_TIM_REG));
251 1.5 macallan
252 1.5 macallan #if 1
253 1.5 macallan /* program window #0 to 0x08000000 */
254 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_1),
255 1.8 flxd VLPCI_PCI_WND_HIADDR_MEM(VLPCI_VL_MEM_BASE));
256 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_1),
257 1.8 flxd VLPCI_PCI_WND_LOADDR_MEM(VLPCI_VL_MEM_BASE));
258 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_1),
259 1.8 flxd VLPCI_PCI_WND_ATTR_VL |
260 1.8 flxd __SHIFTIN(VLPCI_PCI_WND_ATTR_SZ_MEM(VLPCI_VL_MEM_SZ),
261 1.8 flxd VLPCI_PCI_WND_ATTR_SZ));
262 1.5 macallan #else
263 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_1), 0x00);
264 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_1), 0x00);
265 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_1), 0x00);
266 1.5 macallan #endif
267 1.1 jakllsch
268 1.8 flxd vlpci_mem_paddr = VLPCI_PCI_MEM_BASE; /* get from OF! */
269 1.7 flxd
270 1.5 macallan /*
271 1.5 macallan * we map in 1MB at 0x02000000, so program window #1 accordingly
272 1.5 macallan */
273 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_HIADDR_REG(VLPCI_PCI_WND_NO_2),
274 1.8 flxd VLPCI_PCI_WND_HIADDR_MEM(vlpci_mem_paddr));
275 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_LOADDR_REG(VLPCI_PCI_WND_NO_2),
276 1.8 flxd VLPCI_PCI_WND_LOADDR_MEM(vlpci_mem_paddr));
277 1.8 flxd regwrite_1(sc, VLPCI_PCI_WND_ATTR_REG(VLPCI_PCI_WND_NO_2),
278 1.8 flxd VLPCI_PCI_WND_ATTR_PCI |
279 1.8 flxd __SHIFTIN(VLPCI_PCI_WND_ATTR_SZ_MEM(VLPCI_PCI_MEM_SZ),
280 1.8 flxd VLPCI_PCI_WND_ATTR_SZ));
281 1.5 macallan
282 1.5 macallan /* now map in some of the memory space */
283 1.5 macallan printf("vlpci_mem_vaddr %08lx\n", vlpci_mem_vaddr);
284 1.5 macallan memcpy(&vlpci_memt, &isa_io_bs_tag, sizeof(struct bus_space));
285 1.5 macallan vlpci_memt.bs_cookie = (void *)vlpci_mem_vaddr;
286 1.5 macallan vlpci_memt.bs_map = vlpci_map;
287 1.5 macallan vlpci_memt.bs_mmap = vlpci_mmap;
288 1.7 flxd
289 1.1 jakllsch pc->pc_conf_v = sc;
290 1.1 jakllsch pc->pc_attach_hook = vlpci_pc_attach_hook;
291 1.1 jakllsch pc->pc_bus_maxdevs = vlpci_pc_bus_maxdevs;
292 1.1 jakllsch pc->pc_make_tag = vlpci_pc_make_tag;
293 1.1 jakllsch pc->pc_decompose_tag = vlpci_pc_decompose_tag;
294 1.1 jakllsch pc->pc_conf_read = vlpci_pc_conf_read;
295 1.1 jakllsch pc->pc_conf_write = vlpci_pc_conf_write;
296 1.4 jakllsch
297 1.4 jakllsch pc->pc_intr_v = sc;
298 1.4 jakllsch pc->pc_intr_map = vlpci_pc_intr_map;
299 1.4 jakllsch pc->pc_intr_string = vlpci_pc_intr_string;
300 1.4 jakllsch pc->pc_intr_evcnt = vlpci_pc_intr_evcnt;
301 1.4 jakllsch pc->pc_intr_establish = vlpci_pc_intr_establish;
302 1.4 jakllsch pc->pc_intr_disestablish = vlpci_pc_intr_disestablish;
303 1.4 jakllsch
304 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
305 1.1 jakllsch pc->pc_conf_hook = vlpci_pc_conf_hook;
306 1.1 jakllsch #endif
307 1.1 jakllsch pc->pc_conf_interrupt = vlpci_pc_conf_interrupt;
308 1.1 jakllsch
309 1.4 jakllsch /* try to assure IO space is enabled on the default device-function */
310 1.8 flxd tag = vlpci_pc_make_tag(sc, 0, VLPCI_ADDON_DEV_NO, 0);
311 1.8 flxd cmd = vlpci_pc_conf_read(sc, tag, PCI_COMMAND_STATUS_REG);
312 1.8 flxd vlpci_pc_conf_write(sc, tag, PCI_COMMAND_STATUS_REG,
313 1.8 flxd cmd | PCI_COMMAND_IO_ENABLE);
314 1.1 jakllsch
315 1.5 macallan pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
316 1.4 jakllsch pba.pba_iot = &isa_io_bs_tag;
317 1.5 macallan pba.pba_memt = &vlpci_memt;
318 1.5 macallan pba.pba_dmat = &isa_bus_dma_tag;
319 1.1 jakllsch pba.pba_pc = &sc->sc_pc;
320 1.1 jakllsch pba.pba_bus = 0;
321 1.1 jakllsch
322 1.7 flxd printf("dma %lx %lx, %lx\n", isa_bus_dma_tag._ranges[0].dr_sysbase,
323 1.7 flxd isa_bus_dma_tag._ranges[0].dr_busbase,
324 1.7 flxd isa_bus_dma_tag._ranges[0].dr_len);
325 1.5 macallan
326 1.8 flxd vlpci_dump_window(sc, VLPCI_PCI_WND_NO_1);
327 1.8 flxd vlpci_dump_window(sc, VLPCI_PCI_WND_NO_2);
328 1.8 flxd vlpci_dump_window(sc, VLPCI_PCI_WND_NO_3);
329 1.5 macallan
330 1.1 jakllsch config_found_ia(self, "pcibus", &pba, pcibusprint);
331 1.1 jakllsch }
332 1.1 jakllsch
333 1.1 jakllsch static void
334 1.1 jakllsch vlpci_pc_attach_hook(device_t parent, device_t self,
335 1.1 jakllsch struct pcibus_attach_args *pba)
336 1.1 jakllsch {
337 1.1 jakllsch }
338 1.1 jakllsch
339 1.1 jakllsch static int
340 1.1 jakllsch vlpci_pc_bus_maxdevs(void *v, int busno)
341 1.1 jakllsch {
342 1.7 flxd
343 1.1 jakllsch return busno == 0 ? 32 : 0;
344 1.1 jakllsch }
345 1.1 jakllsch
346 1.1 jakllsch static pcitag_t
347 1.1 jakllsch vlpci_pc_make_tag(void *v, int b, int d, int f)
348 1.1 jakllsch {
349 1.7 flxd
350 1.1 jakllsch return (b << 16) | (d << 11) | (f << 8);
351 1.1 jakllsch }
352 1.1 jakllsch
353 1.1 jakllsch static void
354 1.1 jakllsch vlpci_pc_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
355 1.1 jakllsch {
356 1.7 flxd
357 1.1 jakllsch if (bp)
358 1.1 jakllsch *bp = (tag >> 16) & 0xff;
359 1.1 jakllsch if (dp)
360 1.1 jakllsch *dp = (tag >> 11) & 0x1f;
361 1.1 jakllsch if (fp)
362 1.1 jakllsch *fp = (tag >> 8) & 0x7;
363 1.1 jakllsch }
364 1.1 jakllsch
365 1.1 jakllsch static pcireg_t
366 1.1 jakllsch vlpci_pc_conf_read(void *v, pcitag_t tag, int offset)
367 1.1 jakllsch {
368 1.1 jakllsch struct vlpci_softc * const sc = v;
369 1.1 jakllsch pcireg_t ret;
370 1.1 jakllsch
371 1.1 jakllsch KASSERT((offset & 3) == 0);
372 1.3 jakllsch
373 1.3 jakllsch if (offset >= PCI_CONF_SIZE)
374 1.3 jakllsch return 0xffffffff;
375 1.1 jakllsch
376 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
377 1.8 flxd bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh,
378 1.8 flxd VLPCI_CFGREG_ADDR_OFF, 0x80000000UL|tag|offset);
379 1.8 flxd ret = bus_space_read_4(&isa_io_bs_tag, sc->sc_conf_ioh,
380 1.8 flxd VLPCI_CFGREG_DATA_OFF);
381 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
382 1.1 jakllsch
383 1.1 jakllsch #if 0
384 1.1 jakllsch device_printf(sc->sc_dev, "%s tag %x offset %x ret %x\n",
385 1.1 jakllsch __func__, (unsigned int)tag, offset, ret);
386 1.1 jakllsch #endif
387 1.1 jakllsch
388 1.1 jakllsch return ret;
389 1.1 jakllsch }
390 1.1 jakllsch
391 1.1 jakllsch static void
392 1.1 jakllsch vlpci_pc_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
393 1.1 jakllsch {
394 1.1 jakllsch struct vlpci_softc * const sc = v;
395 1.1 jakllsch
396 1.1 jakllsch KASSERT((offset & 3) == 0);
397 1.3 jakllsch
398 1.3 jakllsch if (offset >= PCI_CONF_SIZE)
399 1.3 jakllsch return;
400 1.1 jakllsch
401 1.1 jakllsch #if 0
402 1.1 jakllsch device_printf(sc->sc_dev, "%s tag %x offset %x val %x\n",
403 1.1 jakllsch __func__, (unsigned int)tag, offset, val);
404 1.1 jakllsch #endif
405 1.1 jakllsch
406 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
407 1.8 flxd bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh,
408 1.8 flxd VLPCI_CFGREG_ADDR_OFF, 0x80000000UL|tag|offset);
409 1.8 flxd bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh,
410 1.8 flxd VLPCI_CFGREG_DATA_OFF, val);
411 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
412 1.1 jakllsch }
413 1.1 jakllsch
414 1.4 jakllsch static int
415 1.4 jakllsch vlpci_pc_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
416 1.4 jakllsch {
417 1.7 flxd
418 1.4 jakllsch switch (pa->pa_intrpin) {
419 1.4 jakllsch default:
420 1.4 jakllsch case 0:
421 1.4 jakllsch return EINVAL;
422 1.4 jakllsch case 1:
423 1.4 jakllsch case 2:
424 1.4 jakllsch case 3:
425 1.4 jakllsch case 4:
426 1.8 flxd *ih = VLPCI_IRQ;
427 1.4 jakllsch return 0;
428 1.4 jakllsch }
429 1.4 jakllsch }
430 1.4 jakllsch
431 1.4 jakllsch static const char *
432 1.4 jakllsch vlpci_pc_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
433 1.4 jakllsch {
434 1.4 jakllsch
435 1.4 jakllsch if (ih == PCI_INTERRUPT_PIN_NONE)
436 1.4 jakllsch return NULL;
437 1.9 macallan snprintf(buf, len, "irq %llu", ih);
438 1.4 jakllsch return buf;
439 1.4 jakllsch }
440 1.4 jakllsch
441 1.4 jakllsch static const struct evcnt *
442 1.4 jakllsch vlpci_pc_intr_evcnt(void *v, pci_intr_handle_t ih)
443 1.4 jakllsch {
444 1.7 flxd
445 1.4 jakllsch return NULL;
446 1.4 jakllsch }
447 1.4 jakllsch
448 1.4 jakllsch static void *
449 1.4 jakllsch vlpci_pc_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
450 1.9 macallan int (*callback)(void *), void *arg, const char *foo)
451 1.4 jakllsch {
452 1.7 flxd
453 1.4 jakllsch if (pih == 0)
454 1.4 jakllsch return NULL;
455 1.4 jakllsch
456 1.4 jakllsch return isa_intr_establish(NULL, pih, IST_LEVEL, ipl, callback, arg);
457 1.4 jakllsch }
458 1.4 jakllsch
459 1.4 jakllsch static void
460 1.4 jakllsch vlpci_pc_intr_disestablish(void *v, void *w)
461 1.4 jakllsch {
462 1.6 martin
463 1.6 martin return isa_intr_disestablish(NULL, v);
464 1.4 jakllsch }
465 1.4 jakllsch
466 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
467 1.1 jakllsch static int
468 1.1 jakllsch vlpci_pc_conf_hook(void *v, int b, int d, int f, pcireg_t id)
469 1.1 jakllsch {
470 1.7 flxd
471 1.5 macallan return PCI_CONF_DEFAULT /*& ~PCI_CONF_ENABLE_BM*/;
472 1.1 jakllsch }
473 1.1 jakllsch #endif
474 1.1 jakllsch
475 1.1 jakllsch static void
476 1.1 jakllsch vlpci_pc_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
477 1.1 jakllsch int *ilinep)
478 1.1 jakllsch {
479 1.7 flxd
480 1.1 jakllsch *ilinep = 0xff; /* XXX */
481 1.1 jakllsch }
482