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