vlpci.c revision 1.4 1 1.4 jakllsch /* $NetBSD: vlpci.c,v 1.4 2017/02/27 18:30:42 jakllsch 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.4 jakllsch __KERNEL_RCSID(0, "$NetBSD: vlpci.c,v 1.4 2017/02/27 18:30:42 jakllsch 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.1 jakllsch
41 1.1 jakllsch #include <dev/isa/isavar.h>
42 1.1 jakllsch
43 1.1 jakllsch #include <dev/ofw/openfirm.h>
44 1.1 jakllsch
45 1.1 jakllsch #include <dev/pci/pcivar.h>
46 1.1 jakllsch #include <dev/pci/pciconf.h>
47 1.1 jakllsch #include <arm/pci_machdep.h>
48 1.1 jakllsch
49 1.1 jakllsch static int vlpci_match(device_t, struct cfdata *, void *);
50 1.1 jakllsch static void vlpci_attach(device_t, device_t, void *);
51 1.1 jakllsch
52 1.1 jakllsch static void vlpci_pc_attach_hook(device_t, device_t,
53 1.1 jakllsch struct pcibus_attach_args *);
54 1.1 jakllsch static int vlpci_pc_bus_maxdevs(void *, int);
55 1.1 jakllsch static pcitag_t vlpci_pc_make_tag(void *, int, int, int);
56 1.1 jakllsch static void vlpci_pc_decompose_tag(void *, pcitag_t, int *, int *, int *);
57 1.1 jakllsch static pcireg_t vlpci_pc_conf_read(void *, pcitag_t, int);
58 1.1 jakllsch static void vlpci_pc_conf_write(void *, pcitag_t, int, pcireg_t);
59 1.4 jakllsch
60 1.4 jakllsch static int vlpci_pc_intr_map(const struct pci_attach_args *,
61 1.4 jakllsch pci_intr_handle_t *);
62 1.4 jakllsch static const char * vlpci_pc_intr_string(void *, pci_intr_handle_t, char *,
63 1.4 jakllsch size_t);
64 1.4 jakllsch static const struct evcnt * vlpci_pc_intr_evcnt(void *, pci_intr_handle_t);
65 1.4 jakllsch static void * vlpci_pc_intr_establish(void *, pci_intr_handle_t, int,
66 1.4 jakllsch int (*)(void *), void *);
67 1.4 jakllsch static void vlpci_pc_intr_disestablish(void *, void *);
68 1.4 jakllsch
69 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
70 1.1 jakllsch static int vlpci_pc_conf_hook(void *, int, int, int, pcireg_t);
71 1.1 jakllsch #endif
72 1.1 jakllsch static void vlpci_pc_conf_interrupt(void *, int, int, int, int, int *);
73 1.1 jakllsch
74 1.1 jakllsch struct vlpci_softc {
75 1.1 jakllsch device_t sc_dev;
76 1.1 jakllsch kmutex_t sc_lock;
77 1.1 jakllsch bus_space_handle_t sc_conf_ioh;
78 1.1 jakllsch bus_space_handle_t sc_reg_ioh;
79 1.1 jakllsch struct arm32_pci_chipset sc_pc;
80 1.1 jakllsch };
81 1.1 jakllsch
82 1.1 jakllsch CFATTACH_DECL_NEW(vlpci, sizeof(struct vlpci_softc),
83 1.1 jakllsch vlpci_match, vlpci_attach, NULL, NULL);
84 1.1 jakllsch
85 1.1 jakllsch static const char * const compat_strings[] = { "via,vt82c505", NULL };
86 1.1 jakllsch
87 1.1 jakllsch static void
88 1.1 jakllsch regwrite_1(struct vlpci_softc * const sc, uint8_t off, uint8_t val)
89 1.1 jakllsch {
90 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
91 1.1 jakllsch bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, 0, off);
92 1.1 jakllsch bus_space_write_1(&isa_io_bs_tag, sc->sc_reg_ioh, 1, val);
93 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
94 1.1 jakllsch }
95 1.1 jakllsch
96 1.1 jakllsch static int
97 1.1 jakllsch vlpci_match(device_t parent, struct cfdata *match, void *aux)
98 1.1 jakllsch {
99 1.1 jakllsch struct ofbus_attach_args * const oba = aux;
100 1.1 jakllsch
101 1.1 jakllsch if (of_compatible(oba->oba_phandle, compat_strings) < 0)
102 1.1 jakllsch return 0;
103 1.1 jakllsch
104 1.2 flxd return 2; /* beat generic ofbus */
105 1.1 jakllsch }
106 1.1 jakllsch
107 1.1 jakllsch static void
108 1.1 jakllsch vlpci_attach(device_t parent, device_t self, void *aux)
109 1.1 jakllsch {
110 1.1 jakllsch struct vlpci_softc * const sc = device_private(self);
111 1.1 jakllsch pci_chipset_tag_t const pc = &sc->sc_pc;
112 1.1 jakllsch struct pcibus_attach_args pba;
113 1.1 jakllsch
114 1.1 jakllsch aprint_normal("\n");
115 1.1 jakllsch
116 1.1 jakllsch sc->sc_dev = self;
117 1.1 jakllsch mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
118 1.1 jakllsch memset(&pba, 0, sizeof(pba));
119 1.1 jakllsch
120 1.1 jakllsch if (bus_space_map(&isa_io_bs_tag, 0xa8, 0x2,
121 1.1 jakllsch 0, &sc->sc_reg_ioh) != 0) {
122 1.1 jakllsch aprint_error_dev(self, "failed to map 0xa8-9\n");
123 1.1 jakllsch return;
124 1.1 jakllsch }
125 1.1 jakllsch if (bus_space_map(&isa_io_bs_tag, 0xcf8, 0x8,
126 1.1 jakllsch 0, &sc->sc_conf_ioh) != 0) {
127 1.1 jakllsch aprint_error_dev(self, "failed to map 0xcf8-f\n");
128 1.1 jakllsch return;
129 1.1 jakllsch }
130 1.1 jakllsch
131 1.1 jakllsch /* Enable VLB/PCI bridge */
132 1.4 jakllsch regwrite_1(sc, 0x96, 0x18); /* enable LOCAL#, compatible mode */
133 1.4 jakllsch regwrite_1(sc, 0x93, 0x60); /* IOCHCK# on IOCHCK#/NMI */
134 1.4 jakllsch regwrite_1(sc, 0x86, 2<<5); /* invert all INTx to IRQ */
135 1.4 jakllsch regwrite_1(sc, 0x97, 0x00); /* don't do per-INTx conversions */
136 1.4 jakllsch regwrite_1(sc, 0x91, 0xbb); /* enable INT[AB] to IRQ 10 */
137 1.4 jakllsch regwrite_1(sc, 0x90, 0xbb); /* enable INT[CD] to IRQ 10 */
138 1.1 jakllsch
139 1.1 jakllsch pc->pc_conf_v = sc;
140 1.1 jakllsch pc->pc_attach_hook = vlpci_pc_attach_hook;
141 1.1 jakllsch pc->pc_bus_maxdevs = vlpci_pc_bus_maxdevs;
142 1.1 jakllsch pc->pc_make_tag = vlpci_pc_make_tag;
143 1.1 jakllsch pc->pc_decompose_tag = vlpci_pc_decompose_tag;
144 1.1 jakllsch pc->pc_conf_read = vlpci_pc_conf_read;
145 1.1 jakllsch pc->pc_conf_write = vlpci_pc_conf_write;
146 1.4 jakllsch
147 1.4 jakllsch pc->pc_intr_v = sc;
148 1.4 jakllsch pc->pc_intr_map = vlpci_pc_intr_map;
149 1.4 jakllsch pc->pc_intr_string = vlpci_pc_intr_string;
150 1.4 jakllsch pc->pc_intr_evcnt = vlpci_pc_intr_evcnt;
151 1.4 jakllsch pc->pc_intr_establish = vlpci_pc_intr_establish;
152 1.4 jakllsch pc->pc_intr_disestablish = vlpci_pc_intr_disestablish;
153 1.4 jakllsch
154 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
155 1.1 jakllsch pc->pc_conf_hook = vlpci_pc_conf_hook;
156 1.1 jakllsch #endif
157 1.1 jakllsch pc->pc_conf_interrupt = vlpci_pc_conf_interrupt;
158 1.1 jakllsch
159 1.4 jakllsch /* try to assure IO space is enabled on the default device-function */
160 1.4 jakllsch vlpci_pc_conf_write(sc, vlpci_pc_make_tag(sc, 0, 6, 0),
161 1.4 jakllsch PCI_COMMAND_STATUS_REG,
162 1.4 jakllsch vlpci_pc_conf_read(sc, vlpci_pc_make_tag(sc, 0, 6, 0),
163 1.4 jakllsch PCI_COMMAND_STATUS_REG) |
164 1.4 jakllsch PCI_COMMAND_IO_ENABLE);
165 1.1 jakllsch
166 1.4 jakllsch pba.pba_flags = PCI_FLAGS_IO_OKAY; /* try for more someday */
167 1.4 jakllsch pba.pba_iot = &isa_io_bs_tag;
168 1.1 jakllsch pba.pba_pc = &sc->sc_pc;
169 1.1 jakllsch pba.pba_bus = 0;
170 1.1 jakllsch
171 1.1 jakllsch config_found_ia(self, "pcibus", &pba, pcibusprint);
172 1.1 jakllsch }
173 1.1 jakllsch
174 1.1 jakllsch static void
175 1.1 jakllsch vlpci_pc_attach_hook(device_t parent, device_t self,
176 1.1 jakllsch struct pcibus_attach_args *pba)
177 1.1 jakllsch {
178 1.1 jakllsch }
179 1.1 jakllsch
180 1.1 jakllsch static int
181 1.1 jakllsch vlpci_pc_bus_maxdevs(void *v, int busno)
182 1.1 jakllsch {
183 1.1 jakllsch return busno == 0 ? 32 : 0;
184 1.1 jakllsch }
185 1.1 jakllsch
186 1.1 jakllsch static pcitag_t
187 1.1 jakllsch vlpci_pc_make_tag(void *v, int b, int d, int f)
188 1.1 jakllsch {
189 1.1 jakllsch return (b << 16) | (d << 11) | (f << 8);
190 1.1 jakllsch }
191 1.1 jakllsch
192 1.1 jakllsch static void
193 1.1 jakllsch vlpci_pc_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
194 1.1 jakllsch {
195 1.1 jakllsch if (bp)
196 1.1 jakllsch *bp = (tag >> 16) & 0xff;
197 1.1 jakllsch if (dp)
198 1.1 jakllsch *dp = (tag >> 11) & 0x1f;
199 1.1 jakllsch if (fp)
200 1.1 jakllsch *fp = (tag >> 8) & 0x7;
201 1.1 jakllsch }
202 1.1 jakllsch
203 1.1 jakllsch static pcireg_t
204 1.1 jakllsch vlpci_pc_conf_read(void *v, pcitag_t tag, int offset)
205 1.1 jakllsch {
206 1.1 jakllsch struct vlpci_softc * const sc = v;
207 1.1 jakllsch pcireg_t ret;
208 1.1 jakllsch
209 1.1 jakllsch KASSERT((offset & 3) == 0);
210 1.3 jakllsch
211 1.3 jakllsch if (offset >= PCI_CONF_SIZE)
212 1.3 jakllsch return 0xffffffff;
213 1.1 jakllsch
214 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
215 1.1 jakllsch bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 0,
216 1.1 jakllsch 0x80000000UL|tag|offset);
217 1.1 jakllsch ret = bus_space_read_4(&isa_io_bs_tag, sc->sc_conf_ioh, 4);
218 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
219 1.1 jakllsch
220 1.1 jakllsch #if 0
221 1.1 jakllsch device_printf(sc->sc_dev, "%s tag %x offset %x ret %x\n",
222 1.1 jakllsch __func__, (unsigned int)tag, offset, ret);
223 1.1 jakllsch #endif
224 1.1 jakllsch
225 1.1 jakllsch return ret;
226 1.1 jakllsch }
227 1.1 jakllsch
228 1.1 jakllsch static void
229 1.1 jakllsch vlpci_pc_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
230 1.1 jakllsch {
231 1.1 jakllsch struct vlpci_softc * const sc = v;
232 1.1 jakllsch
233 1.1 jakllsch KASSERT((offset & 3) == 0);
234 1.3 jakllsch
235 1.3 jakllsch if (offset >= PCI_CONF_SIZE)
236 1.3 jakllsch return;
237 1.1 jakllsch
238 1.1 jakllsch #if 0
239 1.1 jakllsch device_printf(sc->sc_dev, "%s tag %x offset %x val %x\n",
240 1.1 jakllsch __func__, (unsigned int)tag, offset, val);
241 1.1 jakllsch #endif
242 1.1 jakllsch
243 1.1 jakllsch mutex_spin_enter(&sc->sc_lock);
244 1.1 jakllsch bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 0,
245 1.1 jakllsch 0x80000000UL|tag|offset);
246 1.1 jakllsch bus_space_write_4(&isa_io_bs_tag, sc->sc_conf_ioh, 4, val);
247 1.1 jakllsch mutex_spin_exit(&sc->sc_lock);
248 1.1 jakllsch }
249 1.1 jakllsch
250 1.4 jakllsch static int
251 1.4 jakllsch vlpci_pc_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
252 1.4 jakllsch {
253 1.4 jakllsch switch (pa->pa_intrpin) {
254 1.4 jakllsch default:
255 1.4 jakllsch case 0:
256 1.4 jakllsch return EINVAL;
257 1.4 jakllsch case 1:
258 1.4 jakllsch case 2:
259 1.4 jakllsch case 3:
260 1.4 jakllsch case 4:
261 1.4 jakllsch *ih = 10;
262 1.4 jakllsch return 0;
263 1.4 jakllsch }
264 1.4 jakllsch }
265 1.4 jakllsch
266 1.4 jakllsch static const char *
267 1.4 jakllsch vlpci_pc_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
268 1.4 jakllsch {
269 1.4 jakllsch
270 1.4 jakllsch if (ih == PCI_INTERRUPT_PIN_NONE)
271 1.4 jakllsch return NULL;
272 1.4 jakllsch snprintf(buf, len, "irq %2lu", ih);
273 1.4 jakllsch return buf;
274 1.4 jakllsch }
275 1.4 jakllsch
276 1.4 jakllsch static const struct evcnt *
277 1.4 jakllsch vlpci_pc_intr_evcnt(void *v, pci_intr_handle_t ih)
278 1.4 jakllsch {
279 1.4 jakllsch return NULL;
280 1.4 jakllsch }
281 1.4 jakllsch
282 1.4 jakllsch static void *
283 1.4 jakllsch vlpci_pc_intr_establish(void *v, pci_intr_handle_t pih, int ipl,
284 1.4 jakllsch int (*callback)(void *), void *arg)
285 1.4 jakllsch {
286 1.4 jakllsch if (pih == 0)
287 1.4 jakllsch return NULL;
288 1.4 jakllsch
289 1.4 jakllsch return isa_intr_establish(NULL, pih, IST_LEVEL, ipl, callback, arg);
290 1.4 jakllsch }
291 1.4 jakllsch
292 1.4 jakllsch static void
293 1.4 jakllsch vlpci_pc_intr_disestablish(void *v, void *w)
294 1.4 jakllsch {
295 1.4 jakllsch panic("%s unimplemented", __func__);
296 1.4 jakllsch }
297 1.4 jakllsch
298 1.1 jakllsch #ifdef __HAVE_PCI_CONF_HOOK
299 1.1 jakllsch static int
300 1.1 jakllsch vlpci_pc_conf_hook(void *v, int b, int d, int f, pcireg_t id)
301 1.1 jakllsch {
302 1.1 jakllsch return PCI_CONF_DEFAULT & ~PCI_CONF_ENABLE_BM;
303 1.1 jakllsch }
304 1.1 jakllsch #endif
305 1.1 jakllsch
306 1.1 jakllsch static void
307 1.1 jakllsch vlpci_pc_conf_interrupt(void *v, int bus, int dev, int ipin, int swiz,
308 1.1 jakllsch int *ilinep)
309 1.1 jakllsch {
310 1.1 jakllsch *ilinep = 0xff; /* XXX */
311 1.1 jakllsch }
312