arpci.c revision 1.2 1 1.2 matt /* $NetBSD: arpci.c,v 1.2 2011/07/10 23:13:22 matt 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.2 matt __KERNEL_RCSID(0, "$NetBSD: arpci.c,v 1.2 2011/07/10 23:13:22 matt 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.1 matt #include <mips/atheros/include/arbusvar.h>
42 1.1 matt #include <mips/atheros/include/ar9344reg.h>
43 1.1 matt
44 1.1 matt #define PCI_CMD_CFG_READ 0xa
45 1.1 matt #define PCI_CMD_CFG_WRITE 0xb
46 1.1 matt
47 1.1 matt struct arpci_softc {
48 1.1 matt device_t sc_dev;
49 1.1 matt bus_dma_tag_t sc_dmat;
50 1.1 matt bus_space_tag_t sc_bst;
51 1.1 matt bus_space_handle_t sc_bsh;
52 1.1 matt struct mips_bus_space sc_memt;
53 1.1 matt struct mips_pci_chipset sc_pc;
54 1.2 matt bool sc_pcie;
55 1.1 matt u_int sc_pba_flags;
56 1.1 matt };
57 1.1 matt
58 1.1 matt static void arpci_bus_mem_init(bus_space_tag_t, void *);
59 1.1 matt
60 1.1 matt static void
61 1.1 matt arpci_attach_hook(device_t parent, device_t self,
62 1.1 matt struct pcibus_attach_args *pba)
63 1.1 matt {
64 1.1 matt }
65 1.1 matt
66 1.1 matt static int
67 1.1 matt arpci_bus_maxdevs(void *v, int busno)
68 1.1 matt {
69 1.2 matt struct arpci_softc * const sc = v;
70 1.1 matt
71 1.1 matt if (busno == 0)
72 1.2 matt return (sc->sc_pcie ? 1 : 22);
73 1.1 matt
74 1.1 matt return 32;
75 1.1 matt }
76 1.1 matt
77 1.1 matt static pcitag_t
78 1.1 matt arpci_make_tag(void *v, int bus, int dev, int func)
79 1.1 matt {
80 1.1 matt if (bus == 0 && dev == 0) {
81 1.1 matt /*
82 1.1 matt * Local access
83 1.1 matt */
84 1.1 matt return (func << 8);
85 1.1 matt }
86 1.1 matt
87 1.1 matt if (bus == 0 && dev < 21) {
88 1.1 matt /*
89 1.1 matt * Type 0 can only access 21 (32 - 11) devices starting at * device 0 (0 is needed for inbound transactions).
90 1.1 matt * AD[11:32] encodes the idsel for the transaction
91 1.1 matt * (only one bit can be set).
92 1.1 matt * AD[8:11] contains function
93 1.1 matt * AD[2:7] contains the register offset.
94 1.1 matt * AD[0:1] must be zero.
95 1.1 matt */
96 1.1 matt return (1 << (dev + 11)) | (func << 8);
97 1.1 matt }
98 1.1 matt
99 1.1 matt /*
100 1.1 matt * Type 1 Confugration Transaction.
101 1.1 matt */
102 1.1 matt return (bus << 16) | (dev << 11) | (func << 8) | 1;
103 1.1 matt }
104 1.1 matt
105 1.1 matt static void
106 1.1 matt arpci_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
107 1.1 matt {
108 1.1 matt if (tag & 1) {
109 1.1 matt if (busp)
110 1.1 matt *busp = (tag >> 16) & 255;
111 1.1 matt if (devp)
112 1.1 matt *devp = (tag >> 11) & 31;
113 1.1 matt } else {
114 1.1 matt if (busp)
115 1.1 matt *busp = 0;
116 1.1 matt if (devp) {
117 1.1 matt if (tag & ~0x7ff) {
118 1.1 matt *devp = ffs(tag >> 11) - 1;
119 1.1 matt } else {
120 1.1 matt *devp = 0;
121 1.1 matt }
122 1.1 matt }
123 1.1 matt }
124 1.1 matt if (funcp)
125 1.1 matt *funcp = (tag >> 8) & 7;
126 1.1 matt }
127 1.1 matt
128 1.1 matt static pcireg_t
129 1.1 matt arpci_conf_read(void *v, pcitag_t tag, int reg)
130 1.1 matt {
131 1.1 matt struct arpci_softc * const sc = v;
132 1.1 matt pcireg_t rv = 0xffffffff;
133 1.1 matt
134 1.1 matt if ((tag & 0x00ff0001) == 1) {
135 1.1 matt KASSERT(((tag >> 11) & 31) > 20);
136 1.1 matt /*
137 1.1 matt * This was a type 0 transaction for a device > 20 which
138 1.1 matt * we can't support.
139 1.1 matt */
140 1.1 matt return rv;
141 1.1 matt }
142 1.1 matt
143 1.1 matt tag |= reg & -4;
144 1.1 matt
145 1.1 matt #if 0
146 1.1 matt bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR);
147 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR,
148 1.1 matt bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR) & 3);
149 1.1 matt #endif
150 1.1 matt
151 1.1 matt bus_space_handle_t addr = sc->sc_bsh;
152 1.1 matt if ((tag & ~0x7fe) == 0) {
153 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
154 1.1 matt AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_READ | tag);
155 1.1 matt addr += AR7100_PCI_LCL_CFG_RDATA;
156 1.2 matt printf("%s: tag %#lx: ", __func__, tag);
157 1.1 matt } else {
158 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
159 1.1 matt AR7100_PCI_CFG_ADDR, tag);
160 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
161 1.1 matt AR7100_PCI_CFG_CMD, PCI_CMD_CFG_READ);
162 1.1 matt addr += AR7100_PCI_CFG_RDATA;
163 1.1 matt printf("%s: AD[0:31] 0x%08lx: ", __func__, tag);
164 1.1 matt }
165 1.1 matt
166 1.1 matt rv = kfetch_32((void *)addr, 0xffffffff);
167 1.1 matt printf("%#x\n", rv);
168 1.1 matt
169 1.1 matt return rv;
170 1.1 matt }
171 1.1 matt
172 1.1 matt static void
173 1.1 matt arpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
174 1.1 matt {
175 1.1 matt struct arpci_softc * const sc = v;
176 1.1 matt
177 1.1 matt if ((tag & 0x00ff0001) == 1) {
178 1.1 matt KASSERT(((tag >> 11) & 31) > 20);
179 1.1 matt /*
180 1.1 matt * This was a type 0 transaction for a device > 20 which
181 1.1 matt * we can't support.
182 1.1 matt */
183 1.1 matt return;
184 1.1 matt }
185 1.1 matt
186 1.1 matt tag |= reg & -4;
187 1.1 matt
188 1.1 matt if ((tag & ~0x7fe) == 0) {
189 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
190 1.1 matt AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_WRITE | tag);
191 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
192 1.1 matt AR7100_PCI_LCL_CFG_WDATA, data);
193 1.1 matt } else {
194 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
195 1.1 matt AR7100_PCI_CFG_ADDR, tag);
196 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
197 1.1 matt AR7100_PCI_CFG_CMD, PCI_CMD_CFG_WRITE);
198 1.1 matt bus_space_write_4(sc->sc_bst, sc->sc_bsh,
199 1.1 matt AR7100_PCI_CFG_WDATA, data);
200 1.1 matt }
201 1.1 matt }
202 1.1 matt
203 1.1 matt static int
204 1.1 matt arpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
205 1.1 matt {
206 1.1 matt return EINVAL;
207 1.1 matt }
208 1.1 matt
209 1.1 matt static const char *
210 1.1 matt arpci_intr_string(void *v, pci_intr_handle_t ih)
211 1.1 matt {
212 1.1 matt return NULL;
213 1.1 matt }
214 1.1 matt
215 1.1 matt static const struct evcnt *
216 1.1 matt arpci_intr_evcnt(void *v, pci_intr_handle_t ih)
217 1.1 matt {
218 1.1 matt return NULL;
219 1.1 matt }
220 1.1 matt
221 1.1 matt static void *
222 1.1 matt arpci_intr_establish(void *v, pci_intr_handle_t ih,
223 1.1 matt int ipl, int (*func)(void *), void *arg)
224 1.1 matt {
225 1.1 matt return NULL;
226 1.1 matt }
227 1.1 matt
228 1.1 matt static void
229 1.1 matt arpci_intr_disestablish(void *v, void *cookie)
230 1.1 matt {
231 1.1 matt }
232 1.1 matt
233 1.1 matt static void
234 1.1 matt arpci_conf_interrupt(void *v, int bus, int dev, int func, int swiz, int *ilinep)
235 1.1 matt {
236 1.1 matt }
237 1.1 matt
238 1.1 matt static void
239 1.1 matt arpci_chipset_init(struct arpci_softc *sc)
240 1.1 matt {
241 1.1 matt pci_chipset_tag_t pc = &sc->sc_pc;
242 1.1 matt
243 1.1 matt pc->pc_conf_v = sc;
244 1.1 matt pc->pc_attach_hook = arpci_attach_hook;
245 1.1 matt pc->pc_bus_maxdevs = arpci_bus_maxdevs;
246 1.1 matt pc->pc_make_tag = arpci_make_tag;
247 1.1 matt pc->pc_decompose_tag = arpci_decompose_tag;
248 1.1 matt pc->pc_conf_read = arpci_conf_read;
249 1.1 matt pc->pc_conf_write = arpci_conf_write;
250 1.1 matt
251 1.1 matt pc->pc_intr_v = sc;
252 1.1 matt pc->pc_intr_map = arpci_intr_map;
253 1.1 matt pc->pc_intr_string = arpci_intr_string;
254 1.1 matt pc->pc_intr_evcnt = arpci_intr_evcnt;
255 1.1 matt pc->pc_intr_establish = arpci_intr_establish;
256 1.1 matt pc->pc_intr_disestablish = arpci_intr_disestablish;
257 1.1 matt
258 1.1 matt pc->pc_conf_interrupt = arpci_conf_interrupt;
259 1.1 matt
260 1.1 matt #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
261 1.1 matt //pc->pc_pciide_compat_intr_establish = arpci_pciide_compat_intr_establish;
262 1.1 matt #endif
263 1.1 matt }
264 1.1 matt
265 1.1 matt static int
266 1.1 matt arpci_match(device_t parent, cfdata_t cf, void *aux)
267 1.1 matt {
268 1.1 matt struct arbus_attach_args * const aa = aux;
269 1.1 matt bus_space_handle_t bsh;
270 1.1 matt
271 1.1 matt if (strcmp(aa->aa_name, cf->cf_name) != 0)
272 1.1 matt return 0;
273 1.1 matt
274 1.1 matt if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &bsh))
275 1.1 matt return 0;
276 1.1 matt
277 1.1 matt bus_space_unmap(aa->aa_bst, bsh, aa->aa_size);
278 1.1 matt
279 1.1 matt return 1;
280 1.1 matt }
281 1.1 matt
282 1.1 matt static void
283 1.1 matt arpci_attach(device_t parent, device_t self, void *aux)
284 1.1 matt {
285 1.1 matt struct arbus_attach_args * const aa = aux;
286 1.1 matt struct arpci_softc * const sc = device_private(self);
287 1.1 matt
288 1.1 matt sc->sc_dev = self;
289 1.1 matt sc->sc_bst = aa->aa_bst;
290 1.1 matt sc->sc_dmat = aa->aa_dmat;
291 1.2 matt sc->sc_pcie = (strcmp(device_cfdata(self)->cf_name, "arpcie") == 0);
292 1.1 matt
293 1.1 matt if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0,
294 1.1 matt &sc->sc_bsh)) {
295 1.1 matt aprint_error(": failed to map registers\n");
296 1.1 matt return;
297 1.1 matt }
298 1.1 matt
299 1.2 matt aprint_normal(": PCI%s bus\n", (sc->sc_pcie ? "-Express x1" : ""));
300 1.1 matt arpci_bus_mem_init(&sc->sc_memt, sc);
301 1.1 matt arpci_chipset_init(sc);
302 1.1 matt
303 1.1 matt sc->sc_pba_flags |= PCI_FLAGS_MEM_OKAY;
304 1.1 matt
305 1.1 matt struct pcibus_attach_args pba;
306 1.1 matt memset(&pba, 0, sizeof(pba));
307 1.1 matt
308 1.1 matt pba.pba_flags = sc->sc_pba_flags;
309 1.1 matt if (pba.pba_flags & PCI_FLAGS_MEM_OKAY)
310 1.1 matt pba.pba_memt = &sc->sc_memt;
311 1.1 matt pba.pba_dmat = aa->aa_dmat;
312 1.1 matt pba.pba_pc = &sc->sc_pc;
313 1.1 matt pba.pba_bus = 0;
314 1.1 matt
315 1.1 matt config_found_ia(self, "pcibus", &pba, pcibusprint);
316 1.1 matt }
317 1.1 matt
318 1.1 matt CFATTACH_DECL_NEW(arpci, sizeof(struct arpci_softc),
319 1.1 matt arpci_match, arpci_attach, NULL, NULL);
320 1.2 matt CFATTACH_DECL_NEW(arpcie, sizeof(struct arpci_softc),
321 1.2 matt arpci_match, arpci_attach, NULL, NULL);
322 1.1 matt
323 1.1 matt #define CHIP arpci
324 1.1 matt #define CHIP_LITTLE_ENDIAN /* defined */
325 1.1 matt #define CHIP_MEM /* defined */
326 1.1 matt #define CHIP_EXTENT /* defined */
327 1.1 matt #define CHIP_EX_MALLOC_SAFE(v) true
328 1.1 matt #define CHIP_W1_BUS_START(v) 0x10000000UL
329 1.1 matt #define CHIP_W1_BUS_END(v) 0x16ffffffUL
330 1.1 matt #define CHIP_W1_SYS_START(v) CHIP_W1_BUS_START(v)
331 1.1 matt #define CHIP_W1_SYS_END(v) CHIP_W1_BUS_END(v)
332 1.1 matt
333 1.1 matt #include <mips/mips/bus_space_alignstride_chipdep.c>
334