arpci.c revision 1.7 1 1.7 thorpej /* $NetBSD: arpci.c,v 1.7 2021/08/07 16:18:58 thorpej Exp $ */
2 1.1 matt /*-
3 1.1 matt * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 matt * All rights reserved.
5 1.1 matt *
6 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
7 1.1 matt * by Matt Thomas of 3am Software Foundry.
8 1.1 matt *
9 1.1 matt * Redistribution and use in source and binary forms, with or without
10 1.1 matt * modification, are permitted provided that the following conditions
11 1.1 matt * are met:
12 1.1 matt * 1. Redistributions of source code must retain the above copyright
13 1.1 matt * notice, this list of conditions and the following disclaimer.
14 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 matt * notice, this list of conditions and the following disclaimer in the
16 1.1 matt * documentation and/or other materials provided with the distribution.
17 1.1 matt *
18 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
29 1.1 matt */
30 1.1 matt
31 1.1 matt #include <sys/cdefs.h>
32 1.1 matt
33 1.7 thorpej __KERNEL_RCSID(0, "$NetBSD: arpci.c,v 1.7 2021/08/07 16:18:58 thorpej Exp $");
34 1.1 matt
35 1.1 matt #include <sys/param.h>
36 1.1 matt #include <sys/bus.h>
37 1.1 matt #include <sys/device.h>
38 1.1 matt
39 1.1 matt #include <dev/pci/pcivar.h>
40 1.1 matt
41 1.4 matt #include <mips/locore.h>
42 1.4 matt
43 1.1 matt #include <mips/atheros/include/arbusvar.h>
44 1.1 matt #include <mips/atheros/include/ar9344reg.h>
45 1.1 matt
46 1.1 matt #define PCI_CMD_CFG_READ 0xa
47 1.1 matt #define PCI_CMD_CFG_WRITE 0xb
48 1.1 matt
49 1.1 matt struct arpci_softc {
50 1.1 matt device_t sc_dev;
51 1.1 matt bus_dma_tag_t sc_dmat;
52 1.1 matt bus_space_tag_t sc_bst;
53 1.1 matt bus_space_handle_t sc_bsh;
54 1.1 matt struct mips_bus_space sc_memt;
55 1.1 matt struct mips_pci_chipset sc_pc;
56 1.2 matt bool sc_pcie;
57 1.1 matt u_int sc_pba_flags;
58 1.1 matt };
59 1.1 matt
60 1.1 matt static void arpci_bus_mem_init(bus_space_tag_t, void *);
61 1.1 matt
62 1.1 matt static void
63 1.1 matt arpci_attach_hook(device_t parent, device_t self,
64 1.1 matt struct pcibus_attach_args *pba)
65 1.1 matt {
66 1.1 matt }
67 1.1 matt
68 1.1 matt static int
69 1.1 matt arpci_bus_maxdevs(void *v, int busno)
70 1.1 matt {
71 1.2 matt struct arpci_softc * const sc = v;
72 1.1 matt
73 1.1 matt if (busno == 0)
74 1.2 matt return (sc->sc_pcie ? 1 : 22);
75 1.1 matt
76 1.1 matt return 32;
77 1.1 matt }
78 1.1 matt
79 1.1 matt static pcitag_t
80 1.1 matt arpci_make_tag(void *v, int bus, int dev, int func)
81 1.1 matt {
82 1.1 matt if (bus == 0 && dev == 0) {
83 1.1 matt /*
84 1.1 matt * Local access
85 1.1 matt */
86 1.1 matt return (func << 8);
87 1.1 matt }
88 1.1 matt
89 1.1 matt if (bus == 0 && dev < 21) {
90 1.1 matt /*
91 1.1 matt * Type 0 can only access 21 (32 - 11) devices starting at * device 0 (0 is needed for inbound transactions).
92 1.1 matt * AD[11:32] encodes the idsel for the transaction
93 1.1 matt * (only one bit can be set).
94 1.1 matt * AD[8:11] contains function
95 1.1 matt * AD[2:7] contains the register offset.
96 1.1 matt * AD[0:1] must be zero.
97 1.1 matt */
98 1.1 matt return (1 << (dev + 11)) | (func << 8);
99 1.1 matt }
100 1.1 matt
101 1.1 matt /*
102 1.1 matt * Type 1 Confugration Transaction.
103 1.1 matt */
104 1.1 matt return (bus << 16) | (dev << 11) | (func << 8) | 1;
105 1.1 matt }
106 1.1 matt
107 1.1 matt static void
108 1.1 matt arpci_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
109 1.1 matt {
110 1.1 matt if (tag & 1) {
111 1.1 matt if (busp)
112 1.1 matt *busp = (tag >> 16) & 255;
113 1.1 matt if (devp)
114 1.1 matt *devp = (tag >> 11) & 31;
115 1.1 matt } else {
116 1.1 matt if (busp)
117 1.1 matt *busp = 0;
118 1.1 matt if (devp) {
119 1.1 matt if (tag & ~0x7ff) {
120 1.1 matt *devp = ffs(tag >> 11) - 1;
121 1.1 matt } else {
122 1.1 matt *devp = 0;
123 1.1 matt }
124 1.1 matt }
125 1.1 matt }
126 1.1 matt if (funcp)
127 1.1 matt *funcp = (tag >> 8) & 7;
128 1.1 matt }
129 1.1 matt
130 1.1 matt static pcireg_t
131 1.1 matt arpci_conf_read(void *v, pcitag_t tag, int reg)
132 1.1 matt {
133 1.1 matt struct arpci_softc * const sc = v;
134 1.1 matt pcireg_t rv = 0xffffffff;
135 1.1 matt
136 1.5 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
137 1.5 msaitoh return rv;
138 1.5 msaitoh
139 1.1 matt if ((tag & 0x00ff0001) == 1) {
140 1.1 matt KASSERT(((tag >> 11) & 31) > 20);
141 1.1 matt /*
142 1.1 matt * This was a type 0 transaction for a device > 20 which
143 1.1 matt * we can't support.
144 1.1 matt */
145 1.1 matt return rv;
146 1.1 matt }
147 1.1 matt
148 1.1 matt tag |= reg & -4;
149 1.1 matt
150 1.1 matt #if 0
151 1.1 matt bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR);
152 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR,
153 1.1 matt bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR) & 3);
154 1.1 matt #endif
155 1.1 matt
156 1.1 matt bus_space_handle_t addr = sc->sc_bsh;
157 1.1 matt if ((tag & ~0x7fe) == 0) {
158 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
159 1.1 matt AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_READ | tag);
160 1.1 matt addr += AR7100_PCI_LCL_CFG_RDATA;
161 1.2 matt printf("%s: tag %#lx: ", __func__, tag);
162 1.1 matt } else {
163 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
164 1.1 matt AR7100_PCI_CFG_ADDR, tag);
165 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
166 1.1 matt AR7100_PCI_CFG_CMD, PCI_CMD_CFG_READ);
167 1.1 matt addr += AR7100_PCI_CFG_RDATA;
168 1.1 matt printf("%s: AD[0:31] 0x%08lx: ", __func__, tag);
169 1.1 matt }
170 1.1 matt
171 1.1 matt rv = kfetch_32((void *)addr, 0xffffffff);
172 1.1 matt printf("%#x\n", rv);
173 1.1 matt
174 1.1 matt return rv;
175 1.1 matt }
176 1.1 matt
177 1.1 matt static void
178 1.1 matt arpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
179 1.1 matt {
180 1.1 matt struct arpci_softc * const sc = v;
181 1.1 matt
182 1.5 msaitoh if ((unsigned int)reg >= PCI_CONF_SIZE)
183 1.5 msaitoh return;
184 1.5 msaitoh
185 1.1 matt if ((tag & 0x00ff0001) == 1) {
186 1.1 matt KASSERT(((tag >> 11) & 31) > 20);
187 1.1 matt /*
188 1.1 matt * This was a type 0 transaction for a device > 20 which
189 1.1 matt * we can't support.
190 1.1 matt */
191 1.1 matt return;
192 1.1 matt }
193 1.1 matt
194 1.1 matt tag |= reg & -4;
195 1.1 matt
196 1.1 matt if ((tag & ~0x7fe) == 0) {
197 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
198 1.1 matt AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_WRITE | tag);
199 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
200 1.1 matt AR7100_PCI_LCL_CFG_WDATA, data);
201 1.1 matt } else {
202 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
203 1.1 matt AR7100_PCI_CFG_ADDR, tag);
204 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
205 1.1 matt AR7100_PCI_CFG_CMD, PCI_CMD_CFG_WRITE);
206 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
207 1.1 matt AR7100_PCI_CFG_WDATA, data);
208 1.1 matt }
209 1.1 matt }
210 1.1 matt
211 1.1 matt static int
212 1.1 matt arpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
213 1.1 matt {
214 1.1 matt return EINVAL;
215 1.1 matt }
216 1.1 matt
217 1.1 matt static const char *
218 1.3 christos arpci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
219 1.1 matt {
220 1.3 christos snprintf(buf, len, "fixme!");
221 1.3 christos return buf;
222 1.1 matt }
223 1.1 matt
224 1.1 matt static const struct evcnt *
225 1.1 matt arpci_intr_evcnt(void *v, pci_intr_handle_t ih)
226 1.1 matt {
227 1.1 matt return NULL;
228 1.1 matt }
229 1.1 matt
230 1.1 matt static void *
231 1.1 matt arpci_intr_establish(void *v, pci_intr_handle_t ih,
232 1.1 matt int ipl, int (*func)(void *), void *arg)
233 1.1 matt {
234 1.1 matt return NULL;
235 1.1 matt }
236 1.1 matt
237 1.1 matt static void
238 1.1 matt arpci_intr_disestablish(void *v, void *cookie)
239 1.1 matt {
240 1.1 matt }
241 1.1 matt
242 1.1 matt static void
243 1.1 matt arpci_conf_interrupt(void *v, int bus, int dev, int func, int swiz, int *ilinep)
244 1.1 matt {
245 1.1 matt }
246 1.1 matt
247 1.1 matt static void
248 1.1 matt arpci_chipset_init(struct arpci_softc *sc)
249 1.1 matt {
250 1.1 matt pci_chipset_tag_t pc = &sc->sc_pc;
251 1.1 matt
252 1.1 matt pc->pc_conf_v = sc;
253 1.1 matt pc->pc_attach_hook = arpci_attach_hook;
254 1.1 matt pc->pc_bus_maxdevs = arpci_bus_maxdevs;
255 1.1 matt pc->pc_make_tag = arpci_make_tag;
256 1.1 matt pc->pc_decompose_tag = arpci_decompose_tag;
257 1.1 matt pc->pc_conf_read = arpci_conf_read;
258 1.1 matt pc->pc_conf_write = arpci_conf_write;
259 1.1 matt
260 1.1 matt pc->pc_intr_v = sc;
261 1.1 matt pc->pc_intr_map = arpci_intr_map;
262 1.1 matt pc->pc_intr_string = arpci_intr_string;
263 1.1 matt pc->pc_intr_evcnt = arpci_intr_evcnt;
264 1.1 matt pc->pc_intr_establish = arpci_intr_establish;
265 1.1 matt pc->pc_intr_disestablish = arpci_intr_disestablish;
266 1.1 matt
267 1.1 matt pc->pc_conf_interrupt = arpci_conf_interrupt;
268 1.1 matt
269 1.1 matt #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
270 1.1 matt //pc->pc_pciide_compat_intr_establish = arpci_pciide_compat_intr_establish;
271 1.1 matt #endif
272 1.1 matt }
273 1.1 matt
274 1.1 matt static int
275 1.1 matt arpci_match(device_t parent, cfdata_t cf, void *aux)
276 1.1 matt {
277 1.1 matt struct arbus_attach_args * const aa = aux;
278 1.1 matt bus_space_handle_t bsh;
279 1.1 matt
280 1.1 matt if (strcmp(aa->aa_name, cf->cf_name) != 0)
281 1.1 matt return 0;
282 1.1 matt
283 1.1 matt if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &bsh))
284 1.1 matt return 0;
285 1.1 matt
286 1.1 matt bus_space_unmap(aa->aa_bst, bsh, aa->aa_size);
287 1.1 matt
288 1.1 matt return 1;
289 1.1 matt }
290 1.1 matt
291 1.1 matt static void
292 1.1 matt arpci_attach(device_t parent, device_t self, void *aux)
293 1.1 matt {
294 1.1 matt struct arbus_attach_args * const aa = aux;
295 1.1 matt struct arpci_softc * const sc = device_private(self);
296 1.1 matt
297 1.1 matt sc->sc_dev = self;
298 1.1 matt sc->sc_bst = aa->aa_bst;
299 1.1 matt sc->sc_dmat = aa->aa_dmat;
300 1.2 matt sc->sc_pcie = (strcmp(device_cfdata(self)->cf_name, "arpcie") == 0);
301 1.1 matt
302 1.1 matt if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0,
303 1.1 matt &sc->sc_bsh)) {
304 1.1 matt aprint_error(": failed to map registers\n");
305 1.1 matt return;
306 1.1 matt }
307 1.1 matt
308 1.2 matt aprint_normal(": PCI%s bus\n", (sc->sc_pcie ? "-Express x1" : ""));
309 1.1 matt arpci_bus_mem_init(&sc->sc_memt, sc);
310 1.1 matt arpci_chipset_init(sc);
311 1.1 matt
312 1.1 matt sc->sc_pba_flags |= PCI_FLAGS_MEM_OKAY;
313 1.1 matt
314 1.1 matt struct pcibus_attach_args pba;
315 1.1 matt memset(&pba, 0, sizeof(pba));
316 1.1 matt
317 1.1 matt pba.pba_flags = sc->sc_pba_flags;
318 1.1 matt if (pba.pba_flags & PCI_FLAGS_MEM_OKAY)
319 1.1 matt pba.pba_memt = &sc->sc_memt;
320 1.1 matt pba.pba_dmat = aa->aa_dmat;
321 1.1 matt pba.pba_pc = &sc->sc_pc;
322 1.1 matt pba.pba_bus = 0;
323 1.1 matt
324 1.7 thorpej config_found(self, &pba, pcibusprint, CFARGS_NONE);
325 1.1 matt }
326 1.1 matt
327 1.1 matt CFATTACH_DECL_NEW(arpci, sizeof(struct arpci_softc),
328 1.1 matt arpci_match, arpci_attach, NULL, NULL);
329 1.2 matt CFATTACH_DECL_NEW(arpcie, sizeof(struct arpci_softc),
330 1.2 matt arpci_match, arpci_attach, NULL, NULL);
331 1.1 matt
332 1.1 matt #define CHIP arpci
333 1.1 matt #define CHIP_LITTLE_ENDIAN /* defined */
334 1.1 matt #define CHIP_MEM /* defined */
335 1.1 matt #define CHIP_EXTENT /* defined */
336 1.1 matt #define CHIP_EX_MALLOC_SAFE(v) true
337 1.1 matt #define CHIP_W1_BUS_START(v) 0x10000000UL
338 1.1 matt #define CHIP_W1_BUS_END(v) 0x16ffffffUL
339 1.1 matt #define CHIP_W1_SYS_START(v) CHIP_W1_BUS_START(v)
340 1.1 matt #define CHIP_W1_SYS_END(v) CHIP_W1_BUS_END(v)
341 1.1 matt
342 1.1 matt #include <mips/mips/bus_space_alignstride_chipdep.c>
343