gemini_pci.c revision 1.5 1 /* $NetBSD: gemini_pci.c,v 1.5 2008/11/13 07:21:59 cliff Exp $ */
2
3 /* adapted from:
4 * NetBSD: i80312_pci.c,v 1.9 2005/12/11 12:16:51 christos Exp
5 */
6
7 /*
8 * Copyright (c) 2001 Wasabi Systems, Inc.
9 * All rights reserved.
10 *
11 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed for the NetBSD Project by
24 * Wasabi Systems, Inc.
25 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
26 * or promote products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*
43 * PCI configuration support for i80312 Companion I/O chip.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: gemini_pci.c,v 1.5 2008/11/13 07:21:59 cliff Exp $");
48
49 #include <sys/cdefs.h>
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/extent.h>
55 #include <sys/malloc.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61
62 #include <arm/pic/picvar.h>
63
64 #include <arm/gemini/gemini_reg.h>
65 #include <arm/gemini/gemini_pcivar.h>
66 #include <arm/gemini/gemini_obiovar.h>
67
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/pcidevs.h>
70 #include <dev/pci/pciconf.h>
71
72 #include <machine/pci_machdep.h>
73
74 #include "opt_gemini.h"
75 #include "opt_pci.h"
76 #include "pci.h"
77
78 void gemini_pci_attach_hook(struct device *, struct device *,
79 struct pcibus_attach_args *);
80 int gemini_pci_bus_maxdevs(void *, int);
81 pcitag_t gemini_pci_make_tag(void *, int, int, int);
82 void gemini_pci_decompose_tag(void *, pcitag_t, int *, int *,
83 int *);
84 pcireg_t gemini_pci_conf_read(void *, pcitag_t, int);
85 void gemini_pci_conf_write(void *, pcitag_t, int, pcireg_t);
86 int gemini_pci_conf_hook(pci_chipset_tag_t, int, int, int,
87 pcireg_t);
88
89 int gemini_pci_intr_map(struct pci_attach_args *,
90 pci_intr_handle_t *);
91 const char *gemini_pci_intr_string(void *, pci_intr_handle_t);
92 const struct evcnt *gemini_pci_intr_evcnt(void *, pci_intr_handle_t);
93 void *gemini_pci_intr_establish(void *, pci_intr_handle_t,
94 int, int (*)(void *), void *);
95 void gemini_pci_intr_disestablish(void *, void *);
96 int gemini_pci_intr_handler(void *v);
97
98 #define PCI_CONF_LOCK(s) (s) = disable_interrupts(I32_bit)
99 #define PCI_CONF_UNLOCK(s) restore_interrupts((s))
100
101 int gemini_pci_debug=0;
102
103 struct gemini_pci_intrq {
104 SIMPLEQ_ENTRY(gemini_pci_intrq) iq_q;
105 int (*iq_func)(void *);
106 void *iq_arg;
107 void *iq_ih;
108 };
109
110 static SIMPLEQ_HEAD(, gemini_pci_intrq) gemini_pci_intrq =
111 SIMPLEQ_HEAD_INITIALIZER(gemini_pci_intrq);
112
113 static inline int
114 gemini_pci_intrq_empty(void)
115 {
116 return SIMPLEQ_EMPTY(&gemini_pci_intrq);
117 }
118
119 static inline void *
120 gemini_pci_intrq_insert(void *ih, int (*func)(void *), void *arg)
121 {
122 struct gemini_pci_intrq *iqp;
123
124 iqp = malloc(sizeof(*iqp), M_DEVBUF, M_NOWAIT|M_ZERO);
125 if (iqp == NULL) {
126 printf("gemini_pci_intrq_insert: malloc failed\n");
127 return NULL;
128 }
129
130 iqp->iq_func = func;
131 iqp->iq_arg = arg;
132 iqp->iq_ih = ih;
133 SIMPLEQ_INSERT_TAIL(&gemini_pci_intrq, iqp, iq_q);
134
135 return (void *)iqp;
136 }
137
138 static inline void
139 gemini_pci_intrq_remove(void *cookie)
140 {
141 struct gemini_pci_intrq *iqp;
142
143 SIMPLEQ_FOREACH(iqp, &gemini_pci_intrq, iq_q) {
144 if ((void *)iqp == cookie) {
145 SIMPLEQ_REMOVE(&gemini_pci_intrq,
146 iqp, gemini_pci_intrq, iq_q);
147 free(iqp, M_DEVBUF);
148 return;
149 }
150 }
151 }
152
153 static inline int
154 gemini_pci_intrq_dispatch(void)
155 {
156 struct gemini_pci_intrq *iqp;
157
158 SIMPLEQ_FOREACH(iqp, &gemini_pci_intrq, iq_q) {
159 (*iqp->iq_func)(iqp->iq_arg);
160 }
161
162 return 1;
163 }
164
165 void
166 gemini_pci_init(pci_chipset_tag_t pc, void *cookie)
167 {
168 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
169 struct obio_softc *sc = cookie;
170 struct extent *ioext, *memext;
171 #endif
172
173 pc->pc_conf_v = cookie;
174 pc->pc_attach_hook = gemini_pci_attach_hook;
175 pc->pc_bus_maxdevs = gemini_pci_bus_maxdevs;
176 pc->pc_make_tag = gemini_pci_make_tag;
177 pc->pc_decompose_tag = gemini_pci_decompose_tag;
178 pc->pc_conf_read = gemini_pci_conf_read;
179 pc->pc_conf_write = gemini_pci_conf_write;
180
181 pc->pc_intr_v = cookie;
182 pc->pc_intr_map = gemini_pci_intr_map;
183 pc->pc_intr_string = gemini_pci_intr_string;
184 pc->pc_intr_evcnt = gemini_pci_intr_evcnt;
185 pc->pc_intr_establish = gemini_pci_intr_establish;
186 pc->pc_intr_disestablish = gemini_pci_intr_disestablish;
187
188 pc->pc_conf_hook = gemini_pci_conf_hook;
189
190 /*
191 * initialize copy of CFG_CMD
192 */
193 sc->sc_pci_chipset.pc_cfg_cmd =
194 gemini_pci_conf_read(sc, 0, GEMINI_PCI_CFG_CMD);
195
196 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
197 /*
198 * Configure the PCI bus.
199 *
200 * XXX We need to revisit this. We only configure the Secondary
201 * bus (and its children). The bus configure code needs changes
202 * to support how the busses are arranged on this chip. We also
203 * need to only configure devices in the private device space on
204 * the Secondary bus.
205 */
206
207 aprint_normal("%s: configuring Secondary PCI bus\n",
208 sc->sc_dev.dv_xname);
209
210 /*
211 * XXX PCI IO addr should be inherited ?
212 */
213 ioext = extent_create("pciio",
214 GEMINI_PCIIO_BASE,
215 GEMINI_PCIIO_BASE + GEMINI_PCIIO_SIZE - 1,
216 M_DEVBUF, NULL, 0, EX_NOWAIT);
217
218 /*
219 * XXX PCI mem addr should be inherited ?
220 */
221 memext = extent_create("pcimem",
222 GEMINI_PCIMEM_BASE,
223 GEMINI_PCIMEM_BASE + GEMINI_PCIMEM_SIZE - 1,
224 M_DEVBUF, NULL, 0, EX_NOWAIT);
225
226 pci_configure_bus(pc, ioext, memext, NULL, 0, arm_dcache_align);
227
228 gemini_pci_conf_write(sc, 0, GEMINI_PCI_CFG_REG_MEM1,
229 PCI_CFG_REG_MEM_BASE((GEMINI_DRAM_BASE + (GEMINI_BUSBASE * 1024 * 1024)))
230 | gemini_pci_cfg_reg_mem_size(MEMSIZE * 1024 * 1024));
231
232 extent_destroy(ioext);
233 extent_destroy(memext);
234 #endif
235 }
236
237 void
238 pci_conf_interrupt(pci_chipset_tag_t pc, int a, int b, int c, int d, int *p)
239 {
240 }
241
242 int
243 gemini_pci_conf_hook(pci_chipset_tag_t pc, int bus, int device, int function, pcireg_t id)
244 {
245 int rv;
246
247 rv = PCI_CONF_ALL;
248
249 return rv;
250 }
251
252 void
253 gemini_pci_attach_hook(struct device *parent, struct device *self,
254 struct pcibus_attach_args *pba)
255 {
256 /* Nothing to do. */
257 }
258
259 int
260 gemini_pci_bus_maxdevs(void *v, int busno)
261 {
262 return (32);
263 }
264
265 pcitag_t
266 gemini_pci_make_tag(void *v, int b, int d, int f)
267 {
268 return ((b << 16) | (d << 11) | (f << 8));
269 }
270
271 void
272 gemini_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
273 {
274 if (bp != NULL)
275 *bp = (tag >> 16) & 0xff;
276 if (dp != NULL)
277 *dp = (tag >> 11) & 0x1f;
278 if (fp != NULL)
279 *fp = (tag >> 8) & 0x7;
280 }
281
282 struct pciconf_state {
283 uint32_t ps_addr_val;
284 int ps_b, ps_d, ps_f;
285 };
286
287 static int
288 gemini_pci_conf_setup(struct obio_softc *sc, pcitag_t tag, int offset,
289 struct pciconf_state *ps)
290 {
291 gemini_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
292
293 ps->ps_addr_val =
294 PCI_CFG_CMD_ENB
295 | PCI_CFG_CMD_BUSn(ps->ps_b)
296 | PCI_CFG_CMD_DEVn(ps->ps_d)
297 | PCI_CFG_CMD_FUNCn(ps->ps_f)
298 | PCI_CFG_CMD_REGn(offset);
299
300 return (0);
301 }
302
303 pcireg_t
304 gemini_pci_conf_read(void *v, pcitag_t tag, int offset)
305 {
306 struct obio_softc *sc = v;
307 struct pciconf_state ps;
308 vaddr_t va;
309 pcireg_t rv;
310 u_int s;
311
312 if (gemini_pci_conf_setup(sc, tag, offset, &ps))
313 return ((pcireg_t) -1);
314
315 PCI_CONF_LOCK(s);
316
317 if (sc->sc_pci_chipset.pc_cfg_cmd != ps.ps_addr_val) {
318 sc->sc_pci_chipset.pc_cfg_cmd = ps.ps_addr_val;
319 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh,
320 GEMINI_PCI_CFG_CMD, ps.ps_addr_val);
321 }
322
323 va = (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_pcicfg_ioh);
324 if (badaddr_read((void *) (va + GEMINI_PCI_CFG_DATA), sizeof(rv), &rv)) {
325 /*
326 * XXX Clear the Master Abort
327 */
328 #if 1
329 printf("conf_read: %d/%d/%d bad address\n",
330 ps.ps_b, ps.ps_d, ps.ps_f);
331 #endif
332 rv = (pcireg_t) -1;
333 }
334
335 PCI_CONF_UNLOCK(s);
336
337 if (gemini_pci_debug) {
338 printf("conf_read: tag %#lx, %d/%d/%d, ps_addr_val %#x, rv %#x\n",
339 tag, ps.ps_b, ps.ps_d, ps.ps_f, ps.ps_addr_val, rv);
340 Debugger();
341 }
342
343 return (rv);
344 }
345
346 void
347 gemini_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
348 {
349 struct obio_softc *sc = v;
350 struct pciconf_state ps;
351 u_int s;
352
353 if (gemini_pci_conf_setup(sc, tag, offset, &ps))
354 return;
355
356 PCI_CONF_LOCK(s);
357
358 if (sc->sc_pci_chipset.pc_cfg_cmd != ps.ps_addr_val) {
359 sc->sc_pci_chipset.pc_cfg_cmd = ps.ps_addr_val;
360 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh,
361 GEMINI_PCI_CFG_CMD, ps.ps_addr_val);
362 }
363
364 bus_space_write_4(sc->sc_iot, sc->sc_pcicfg_ioh,
365 GEMINI_PCI_CFG_DATA, val);
366
367 PCI_CONF_UNLOCK(s);
368 }
369
370 int
371 gemini_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
372 {
373 int irq;
374
375 irq = 8;
376
377 *ihp = irq;
378 return 0;
379 }
380
381 const char *
382 gemini_pci_intr_string(void *v, pci_intr_handle_t ih)
383 {
384 const char *name = "pci";
385
386 return (name);
387 }
388
389 const struct evcnt *
390 gemini_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
391 {
392 return NULL;
393 }
394
395 void *
396 gemini_pci_intr_establish(void *v, pci_intr_handle_t pci_ih, int ipl,
397 int (*func)(void *), void *arg)
398 {
399 pcireg_t r;
400 void *ih=NULL;
401 int irq;
402 void *cookie;
403
404 irq = (int)pci_ih;
405
406 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2);
407 r |= CFG_REG_CTL2_INTMASK_INT_ABCD;
408 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r);
409
410 if (gemini_pci_intrq_empty())
411 ih = intr_establish(irq, ipl, IST_LEVEL_HIGH,
412 gemini_pci_intr_handler, v);
413
414 cookie = gemini_pci_intrq_insert(ih, func, arg);
415 if (cookie == NULL) {
416 if (gemini_pci_intrq_empty())
417 intr_disestablish(ih);
418 }
419
420 return cookie;
421 }
422
423 void
424 gemini_pci_intr_disestablish(void *v, void *cookie)
425 {
426 pcireg_t r;
427 struct gemini_pci_intrq *iqp = (struct gemini_pci_intrq *)cookie;;
428 void *ih = iqp->iq_ih;
429
430 gemini_pci_intrq_remove(cookie);
431 if (gemini_pci_intrq_empty()) {
432 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2);
433 r &= ~CFG_REG_CTL2_INTMASK_INT_ABCD;
434 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r);
435 intr_disestablish(ih);
436 }
437 }
438
439 int
440 gemini_pci_intr_handler(void *v)
441 {
442 pcireg_t r;
443 int rv;
444
445 /*
446 * dispatch PCI device interrupt handlers
447 */
448 rv = gemini_pci_intrq_dispatch();
449
450 /*
451 * ack Gemini PCI interrupts
452 */
453 r = gemini_pci_conf_read(v, 0, GEMINI_PCI_CFG_REG_CTL2);
454 gemini_pci_conf_write(v, 0, GEMINI_PCI_CFG_REG_CTL2, r);
455
456 return rv;
457 }
458
459