vrpciu.c revision 1.4 1 /* $NetBSD: vrpciu.c,v 1.4 2002/01/13 14:18:32 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 2001 Enami Tsugutomo.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/device.h>
32
33 #include <machine/bus.h>
34 #include <machine/bus_space_hpcmips.h>
35 #include <machine/bus_dma_hpcmips.h>
36 #include <machine/config_hook.h>
37
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcidevs.h>
40
41 #include <hpcmips/vr/icureg.h>
42 #include <hpcmips/vr/vripvar.h>
43 #include <hpcmips/vr/vrpciureg.h>
44
45 #include "pci.h"
46
47 #ifdef DEBUG
48 #define DPRINTF(args) printf args
49 #else
50 #define DPRINTF(args)
51 #endif
52
53 struct vrpciu_softc {
54 struct device sc_dev;
55
56 vrip_chipset_tag_t sc_vc;
57 bus_space_tag_t sc_iot;
58 bus_space_handle_t sc_ioh;
59 void *sc_ih;
60
61 struct vrc4173bcu_softc *sc_bcu; /* vrc4173bcu */
62
63 struct hpcmips_pci_chipset sc_pc;
64 };
65
66 static void vrpciu_write(struct vrpciu_softc *, int, u_int32_t);
67 static u_int32_t
68 vrpciu_read(struct vrpciu_softc *, int);
69 #ifdef DEBUG
70 static void vrpciu_write_2(struct vrpciu_softc *, int, u_int16_t)
71 __attribute__((unused));
72 static u_int16_t
73 vrpciu_read_2(struct vrpciu_softc *, int);
74 #endif
75 static int vrpciu_match(struct device *, struct cfdata *, void *);
76 static void vrpciu_attach(struct device *, struct device *, void *);
77 #if NPCI > 0
78 static int vrpciu_print(void *, const char *);
79 #endif
80 static int vrpciu_intr(void *);
81 static void vrpciu_attach_hook(struct device *, struct device *,
82 struct pcibus_attach_args *);
83 static int vrpciu_bus_maxdevs(pci_chipset_tag_t, int);
84 static int vrpciu_bus_devorder(pci_chipset_tag_t, int, char *);
85 static pcitag_t vrpciu_make_tag(pci_chipset_tag_t, int, int, int);
86 static void vrpciu_decompose_tag(pci_chipset_tag_t, pcitag_t, int *, int *,
87 int *);
88 static pcireg_t vrpciu_conf_read(pci_chipset_tag_t, pcitag_t, int);
89 static void vrpciu_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
90 static int vrpciu_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
91 static const char *vrpciu_intr_string(pci_chipset_tag_t, pci_intr_handle_t);
92 static const struct evcnt *vrpciu_intr_evcnt(pci_chipset_tag_t,
93 pci_intr_handle_t);
94 static void *vrpciu_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
95 int, int (*)(void *), void *);
96 static void vrpciu_intr_disestablish(pci_chipset_tag_t, void *);
97
98 struct cfattach vrpciu_ca = {
99 sizeof(struct vrpciu_softc), vrpciu_match, vrpciu_attach
100 };
101
102 static void
103 vrpciu_write(struct vrpciu_softc *sc, int offset, u_int32_t val)
104 {
105
106 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
107 }
108
109 static u_int32_t
110 vrpciu_read(struct vrpciu_softc *sc, int offset)
111 {
112
113 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset));
114 }
115
116 #ifdef DEBUG
117 static void
118 vrpciu_write_2(struct vrpciu_softc *sc, int offset, u_int16_t val)
119 {
120
121 bus_space_write_2(sc->sc_iot, sc->sc_ioh, offset, val);
122 }
123
124 static u_int16_t
125 vrpciu_read_2(struct vrpciu_softc *sc, int offset)
126 {
127
128 return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset));
129 }
130 #endif
131
132 static int
133 vrpciu_match(struct device *parent, struct cfdata *match, void *aux)
134 {
135
136 return (1);
137 }
138
139 static void
140 vrpciu_attach(struct device *parent, struct device *self, void *aux)
141 {
142 struct vrpciu_softc *sc = (struct vrpciu_softc *)self;
143 pci_chipset_tag_t pc = &sc->sc_pc;
144 struct vrip_attach_args *va = aux;
145 struct bus_space_tag_hpcmips *iot;
146 u_int32_t reg;
147 char tmpbuf[16];
148 #if NPCI > 0
149 struct pcibus_attach_args pba;
150 #endif
151
152 sc->sc_vc = va->va_vc;
153 sc->sc_iot = va->va_iot;
154 if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size, 0,
155 &sc->sc_ioh)) {
156 printf(": couldn't map io space\n");
157 return;
158 }
159
160 sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
161 vrpciu_intr, sc);
162 if (sc->sc_ih == NULL) {
163 printf(": couldn't establish interrupt\n");
164 return;
165 }
166
167 /* Enable level 2 interrupt */
168 vrip_intr_setmask2(va->va_vc, sc->sc_ih, PCIINT_INT0, 1);
169
170 printf("\n");
171
172 #ifdef DEBUG
173 #define DUMP_MAW(sc, name, reg) do { \
174 printf("%s: %s =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
175 (name), (reg)); \
176 printf("%s:\tIBA/MASK =\t0x%08x/0x%08x (0x%08x - 0x%08x)\n", \
177 (sc)->sc_dev.dv_xname, \
178 reg & VRPCIU_MAW_IBAMASK, VRPCIU_MAW_ADDRMASK(reg), \
179 VRPCIU_MAW_ADDR(reg), \
180 VRPCIU_MAW_ADDR(reg) + VRPCIU_MAW_SIZE(reg)); \
181 printf("%s:\tWINEN =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
182 reg & VRPCIU_MAW_WINEN); \
183 printf("%s:\tPCIADR =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
184 VRPCIU_MAW_PCIADDR(reg)); \
185 } while (0)
186 #define DUMP_TAW(sc, name, reg) do { \
187 printf("%s: %s =\t\t0x%08x\n", (sc)->sc_dev.dv_xname, \
188 (name), (reg)); \
189 printf("%s:\tMASK =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
190 VRPCIU_TAW_ADDRMASK(reg)); \
191 printf("%s:\tWINEN =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
192 reg & VRPCIU_TAW_WINEN); \
193 printf("%s:\tIBA =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
194 VRPCIU_TAW_IBA(reg)); \
195 } while (0)
196 reg = vrpciu_read(sc, VRPCIU_MMAW1REG);
197 DUMP_MAW(sc, "MMAW1", reg);
198 reg = vrpciu_read(sc, VRPCIU_MMAW2REG);
199 DUMP_MAW(sc, "MMAW2", reg);
200 reg = vrpciu_read(sc, VRPCIU_TAW1REG);
201 DUMP_TAW(sc, "TAW1", reg);
202 reg = vrpciu_read(sc, VRPCIU_TAW2REG);
203 DUMP_TAW(sc, "TAW2", reg);
204 reg = vrpciu_read(sc, VRPCIU_MIOAWREG);
205 DUMP_MAW(sc, "MIOAW", reg);
206 printf("%s: BUSERRAD =\t0x%08x\n", sc->sc_dev.dv_xname,
207 vrpciu_read(sc, VRPCIU_BUSERRADREG));
208 printf("%s: INTCNTSTA =\t0x%08x\n", sc->sc_dev.dv_xname,
209 vrpciu_read(sc, VRPCIU_INTCNTSTAREG));
210 printf("%s: EXACC =\t0x%08x\n", sc->sc_dev.dv_xname,
211 vrpciu_read(sc, VRPCIU_EXACCREG));
212 printf("%s: RECONT =\t0x%08x\n", sc->sc_dev.dv_xname,
213 vrpciu_read(sc, VRPCIU_RECONTREG));
214 printf("%s: PCIEN =\t0x%08x\n", sc->sc_dev.dv_xname,
215 vrpciu_read(sc, VRPCIU_ENREG));
216 printf("%s: CLOCKSEL =\t0x%08x\n", sc->sc_dev.dv_xname,
217 vrpciu_read(sc, VRPCIU_CLKSELREG));
218 printf("%s: TRDYV =\t0x%08x\n", sc->sc_dev.dv_xname,
219 vrpciu_read(sc, VRPCIU_TRDYVREG));
220 printf("%s: CLKRUN =\t0x%08x\n", sc->sc_dev.dv_xname,
221 vrpciu_read_2(sc, VRPCIU_CLKRUNREG));
222 printf("%s: IDREG =\t0x%08x\n", sc->sc_dev.dv_xname,
223 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_ID_REG));
224 reg = vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG);
225 printf("%s: CSR =\t\t0x%08x\n", sc->sc_dev.dv_xname, reg);
226 vrpciu_write(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG, reg);
227 printf("%s: CSR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
228 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG));
229 printf("%s: CLASS =\t0x%08x\n", sc->sc_dev.dv_xname,
230 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_CLASS_REG));
231 printf("%s: BHLC =\t\t0x%08x\n", sc->sc_dev.dv_xname,
232 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_BHLC_REG));
233 printf("%s: MAIL =\t\t0x%08x\n", sc->sc_dev.dv_xname,
234 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MAILREG));
235 printf("%s: MBA1 =\t\t0x%08x\n", sc->sc_dev.dv_xname,
236 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MBA1REG));
237 printf("%s: MBA2 =\t\t0x%08x\n", sc->sc_dev.dv_xname,
238 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MBA2REG));
239 printf("%s: INTR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
240 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG));
241 #if 0
242 vrpciu_write(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG,
243 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG) | 0x01);
244 printf("%s: INTR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
245 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG));
246 #endif
247 #endif
248
249 pc->pc_dev = &sc->sc_dev;
250 pc->pc_attach_hook = vrpciu_attach_hook;
251 pc->pc_bus_maxdevs = vrpciu_bus_maxdevs;
252 pc->pc_bus_devorder = vrpciu_bus_devorder;
253 pc->pc_make_tag = vrpciu_make_tag;
254 pc->pc_decompose_tag = vrpciu_decompose_tag;
255 pc->pc_conf_read = vrpciu_conf_read;
256 pc->pc_conf_write = vrpciu_conf_write;
257 pc->pc_intr_map = vrpciu_intr_map;
258 pc->pc_intr_string = vrpciu_intr_string;
259 pc->pc_intr_evcnt = vrpciu_intr_evcnt;
260 pc->pc_intr_establish = vrpciu_intr_establish;
261 pc->pc_intr_disestablish = vrpciu_intr_disestablish;
262
263 #if 0
264 {
265 int i;
266
267 for (i = 0; i < 8; i++)
268 printf("%s: ID_REG(0, 0, %d) = 0x%08x\n",
269 sc->sc_dev.dv_xname, i,
270 pci_conf_read(pc, pci_make_tag(pc, 0, 0, i),
271 PCI_ID_REG));
272 }
273 #endif
274
275 #if NPCI > 0
276 memset(&pba, 0, sizeof(pba));
277 pba.pba_busname = "pci";
278
279 /* For now, just inherit window mappings set by WinCE. XXX. */
280
281 iot = hpcmips_alloc_bus_space_tag();
282 reg = vrpciu_read(sc, VRPCIU_MIOAWREG);
283 snprintf(tmpbuf, sizeof(tmpbuf), "%s/iot",
284 sc->sc_dev.dv_xname);
285 hpcmips_init_bus_space(iot, (struct bus_space_tag_hpcmips *)sc->sc_iot,
286 tmpbuf, VRPCIU_MAW_ADDR(reg), VRPCIU_MAW_SIZE(reg));
287 pba.pba_iot = &iot->bst;
288
289 /*
290 * Just use system bus space tag. It works since WinCE maps
291 * PCI bus space at same offset. But this isn't right thing
292 * of course. XXX.
293 */
294 pba.pba_memt = sc->sc_iot;
295 pba.pba_dmat = &hpcmips_default_bus_dma_tag.bdt;
296 pba.pba_bus = 0;
297 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
298 PCI_FLAGS_MRL_OKAY;
299 pba.pba_pc = pc;
300
301 config_found(self, &pba, vrpciu_print);
302 #endif
303 }
304
305 #if NPCI > 0
306 static int
307 vrpciu_print(void *aux, const char *pnp)
308 {
309 struct pcibus_attach_args *pba = aux;
310
311 if (pnp != NULL)
312 printf("%s at %s", pba->pba_busname, pnp);
313 else
314 printf(" bus %d", pba->pba_bus);
315
316 return (UNCONF);
317 }
318 #endif
319
320 /*
321 * Handle PCI error interrupts.
322 */
323 int
324 vrpciu_intr(void *arg)
325 {
326 struct vrpciu_softc *sc = (struct vrpciu_softc *)arg;
327 u_int32_t isr;
328
329 isr = vrpciu_read(sc, VRPCIU_INTCNTSTAREG);
330 printf("%s: vrpciu_intr 0x%08x\n", sc->sc_dev.dv_xname, isr);
331 return ((isr & 0x0f) ? 1 : 0);
332 }
333
334 void
335 vrpciu_attach_hook(struct device *parent, struct device *self,
336 struct pcibus_attach_args *pba)
337 {
338
339 return;
340 }
341
342 int
343 vrpciu_bus_maxdevs(pci_chipset_tag_t pc, int busno)
344 {
345
346 return (32);
347 }
348
349 int
350 vrpciu_bus_devorder(pci_chipset_tag_t pc, int busno, char *devs)
351 {
352 int i, dev;
353 char priorities[32];
354 static pcireg_t ids[] = {
355 /* these devices should be attached first */
356 PCI_ID_CODE(PCI_VENDOR_NEC, PCI_PRODUCT_NEC_VRC4173_BCU),
357 };
358
359 /* scan PCI devices and check the id table */
360 memset(priorities, 0, sizeof(priorities));
361 for (dev = 0; dev < 32; dev++) {
362 pcireg_t id;
363 id = pci_conf_read(pc, pci_make_tag(pc, 0, dev, 0),PCI_ID_REG);
364 for (i = 0; i < sizeof(ids)/sizeof(*ids); i++)
365 if (id == ids[i])
366 priorities[dev] = 1;
367 }
368
369 /* fill order array */
370 for (i = 1; 0 <= i; i--)
371 for (dev = 0; dev < 32; dev++)
372 if (priorities[dev] == i)
373 *devs++ = dev;
374
375 return (32);
376 }
377
378 pcitag_t
379 vrpciu_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
380 {
381
382 return ((bus << 16) | (device << 11) | (function << 8));
383 }
384
385 void
386 vrpciu_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp,
387 int *fp)
388 {
389
390 if (bp != NULL)
391 *bp = (tag >> 16) & 0xff;
392 if (dp != NULL)
393 *dp = (tag >> 11) & 0x1f;
394 if (fp != NULL)
395 *fp = (tag >> 8) & 0x07;
396 }
397
398 pcireg_t
399 vrpciu_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
400 {
401 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
402 u_int32_t val;
403 int bus, device, function;
404
405 pci_decompose_tag(pc, tag, &bus, &device, &function);
406 if (bus == 0) {
407 if (device > 21)
408 return ((pcitag_t)-1);
409 tag = (1 << (device + 11)) | (function << 8); /* Type 0 */
410 } else
411 tag |= VRPCIU_CONF_TYPE1;
412
413 vrpciu_write(sc, VRPCIU_CONFAREG, tag | reg);
414 val = vrpciu_read(sc, VRPCIU_CONFDREG);
415 #if 0
416 printf("%s: conf_read: tag = 0x%08x, reg = 0x%x, val = 0x%08x\n",
417 sc->sc_dev.dv_xname, (u_int32_t)tag, reg, val);
418 #endif
419 return (val);
420 }
421
422 void
423 vrpciu_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg,
424 pcireg_t data)
425 {
426 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
427 int bus, device, function;
428
429 #if 0
430 printf("%s: conf_write: tag = 0x%08x, reg = 0x%x, val = 0x%08x\n",
431 sc->sc_dev.dv_xname, (u_int32_t)tag, reg, (u_int32_t)data);
432 #endif
433 vrpciu_decompose_tag(pc, tag, &bus, &device, &function);
434 if (bus == 0) {
435 if (device > 21)
436 return;
437 tag = (1 << (device + 11)) | (function << 8); /* Type 0 */
438 } else
439 tag |= VRPCIU_CONF_TYPE1;
440
441 vrpciu_write(sc, VRPCIU_CONFAREG, tag | reg);
442 vrpciu_write(sc, VRPCIU_CONFDREG, data);
443 }
444
445 int
446 vrpciu_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
447 {
448 pci_chipset_tag_t pc = pa->pa_pc;
449 pcitag_t intrtag = pa->pa_intrtag;
450 int bus, dev, func;
451 #ifdef DEBUG
452 int line = pa->pa_intrline;
453 int pin = pa->pa_intrpin;
454 #endif
455
456 pci_decompose_tag(pc, intrtag, &bus, &dev, &func);
457 DPRINTF(("%s(%d, %d, %d): line = %d, pin = %d\n", pc->pc_dev->dv_xname,
458 bus, dev, func, line, pin));
459
460 *ihp = CONFIG_HOOK_PCIINTR_ID(bus, dev, func);
461
462 return (0);
463 }
464
465 const char *
466 vrpciu_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
467 {
468 static char irqstr[sizeof("pciintr") + 16];
469
470 snprintf(irqstr, sizeof(irqstr), "pciintr %d:%d:%d",
471 CONFIG_HOOK_PCIINTR_BUS((int)ih),
472 CONFIG_HOOK_PCIINTR_DEVICE((int)ih),
473 CONFIG_HOOK_PCIINTR_FUNCTION((int)ih));
474
475 return (irqstr);
476 }
477
478 const struct evcnt *
479 vrpciu_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
480 {
481
482 /* XXX for now, no evcnt parent reported */
483
484 return (NULL);
485 }
486
487 void *
488 vrpciu_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
489 int (*func)(void *), void *arg)
490 {
491
492 if (ih == -1)
493 return (NULL);
494 DPRINTF(("vrpciu_intr_establish: %p\n", sc));
495
496 return (config_hook(CONFIG_HOOK_PCIINTR, ih, CONFIG_HOOK_EXCLUSIVE,
497 (int (*)(void *, int, long, void *))func, arg));
498 }
499
500 void
501 vrpciu_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
502 {
503
504 DPRINTF(("vrpciu_intr_disestablish: %p\n", sc));
505 config_unhook(cookie);
506 }
507