vrpciu.c revision 1.2 1 /* $NetBSD: vrpciu.c,v 1.2 2001/11/22 14:24:33 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
37 #include <dev/pci/pcivar.h>
38
39 #include <hpcmips/vr/icureg.h>
40 #include <hpcmips/vr/vripvar.h>
41 #include <hpcmips/vr/vrpciureg.h>
42 #include <hpcmips/vr/vrc4173bcuvar.h>
43
44 #include "pci.h"
45
46 #ifdef DEBUG
47 #define DPRINTF(args) printf args
48 #else
49 #define DPRINTF(args)
50 #endif
51
52 struct vrpciu_softc {
53 struct device sc_dev;
54
55 vrip_chipset_tag_t sc_vc;
56 bus_space_tag_t sc_iot;
57 bus_space_handle_t sc_ioh;
58 void *sc_ih;
59
60 struct vrc4173bcu_softc *sc_bcu; /* vrc4173bcu */
61
62 struct hpcmips_pci_chipset sc_pc;
63 };
64
65 static void vrpciu_write(struct vrpciu_softc *, int, u_int32_t);
66 static u_int32_t
67 vrpciu_read(struct vrpciu_softc *, int);
68 #ifdef DEBUG
69 static void vrpciu_write_2(struct vrpciu_softc *, int, u_int16_t)
70 __attribute__((unused));
71 static u_int16_t
72 vrpciu_read_2(struct vrpciu_softc *, int);
73 #endif
74 static int vrpciu_match(struct device *, struct cfdata *, void *);
75 static void vrpciu_attach(struct device *, struct device *, void *);
76 #if NPCI > 0
77 static int vrpciu_print(void *, const char *);
78 #endif
79 static int vrpciu_intr(void *);
80 static void vrpciu_attach_hook(struct device *, struct device *,
81 struct pcibus_attach_args *);
82 static int vrpciu_bus_maxdevs(pci_chipset_tag_t, int);
83 static pcitag_t vrpciu_make_tag(pci_chipset_tag_t, int, int, int);
84 static void vrpciu_decompose_tag(pci_chipset_tag_t, pcitag_t, int *, int *,
85 int *);
86 static pcireg_t vrpciu_conf_read(pci_chipset_tag_t, pcitag_t, int);
87 static void vrpciu_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
88 static void *vrpciu_intr_establish(pci_chipset_tag_t, pci_intr_handle_t,
89 int, int (*)(void *), void *);
90 static void vrpciu_intr_disestablish(pci_chipset_tag_t, void *);
91 static void *vrpciu_vrcintr_establish(pci_chipset_tag_t, int,
92 int (*)(void *), void *);
93 static void vrpciu_vrcintr_disestablish(pci_chipset_tag_t, void *);
94
95 struct cfattach vrpciu_ca = {
96 sizeof(struct vrpciu_softc), vrpciu_match, vrpciu_attach
97 };
98
99 static void
100 vrpciu_write(struct vrpciu_softc *sc, int offset, u_int32_t val)
101 {
102
103 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, val);
104 }
105
106 static u_int32_t
107 vrpciu_read(struct vrpciu_softc *sc, int offset)
108 {
109
110 return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset));
111 }
112
113 #ifdef DEBUG
114 static void
115 vrpciu_write_2(struct vrpciu_softc *sc, int offset, u_int16_t val)
116 {
117
118 bus_space_write_2(sc->sc_iot, sc->sc_ioh, offset, val);
119 }
120
121 static u_int16_t
122 vrpciu_read_2(struct vrpciu_softc *sc, int offset)
123 {
124
125 return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset));
126 }
127 #endif
128
129 static int
130 vrpciu_match(struct device *parent, struct cfdata *match, void *aux)
131 {
132
133 return (1);
134 }
135
136 static void
137 vrpciu_attach(struct device *parent, struct device *self, void *aux)
138 {
139 struct vrpciu_softc *sc = (struct vrpciu_softc *)self;
140 pci_chipset_tag_t pc = &sc->sc_pc;
141 struct vrip_attach_args *va = aux;
142 struct bus_space_tag_hpcmips *iot;
143 u_int32_t reg;
144 char tmpbuf[16];
145 #if NPCI > 0
146 struct pcibus_attach_args pba;
147 #endif
148
149 sc->sc_vc = va->va_vc;
150 sc->sc_iot = va->va_iot;
151 if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size, 0,
152 &sc->sc_ioh)) {
153 printf(": couldn't map io space\n");
154 return;
155 }
156
157 sc->sc_ih = vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
158 vrpciu_intr, sc);
159 if (sc->sc_ih == NULL) {
160 printf(": couldn't establish interrupt\n");
161 return;
162 }
163
164 /* Enable level 2 interrupt */
165 vrip_intr_setmask2(va->va_vc, sc->sc_ih, PCIINT_INT0, 1);
166
167 printf("\n");
168
169 #ifdef DEBUG
170 #define DUMP_MAW(sc, name, reg) do { \
171 printf("%s: %s =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
172 (name), (reg)); \
173 printf("%s:\tIBA/MASK =\t0x%08x/0x%08x (0x%08x - 0x%08x)\n", \
174 (sc)->sc_dev.dv_xname, \
175 reg & VRPCIU_MAW_IBAMASK, VRPCIU_MAW_ADDRMASK(reg), \
176 VRPCIU_MAW_ADDR(reg), \
177 VRPCIU_MAW_ADDR(reg) + VRPCIU_MAW_SIZE(reg)); \
178 printf("%s:\tWINEN =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
179 reg & VRPCIU_MAW_WINEN); \
180 printf("%s:\tPCIADR =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
181 VRPCIU_MAW_PCIADDR(reg)); \
182 } while (0)
183 #define DUMP_TAW(sc, name, reg) do { \
184 printf("%s: %s =\t\t0x%08x\n", (sc)->sc_dev.dv_xname, \
185 (name), (reg)); \
186 printf("%s:\tMASK =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
187 VRPCIU_TAW_ADDRMASK(reg)); \
188 printf("%s:\tWINEN =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
189 reg & VRPCIU_TAW_WINEN); \
190 printf("%s:\tIBA =\t0x%08x\n", (sc)->sc_dev.dv_xname, \
191 VRPCIU_TAW_IBA(reg)); \
192 } while (0)
193 reg = vrpciu_read(sc, VRPCIU_MMAW1REG);
194 DUMP_MAW(sc, "MMAW1", reg);
195 reg = vrpciu_read(sc, VRPCIU_MMAW2REG);
196 DUMP_MAW(sc, "MMAW2", reg);
197 reg = vrpciu_read(sc, VRPCIU_TAW1REG);
198 DUMP_TAW(sc, "TAW1", reg);
199 reg = vrpciu_read(sc, VRPCIU_TAW2REG);
200 DUMP_TAW(sc, "TAW2", reg);
201 reg = vrpciu_read(sc, VRPCIU_MIOAWREG);
202 DUMP_MAW(sc, "MIOAW", reg);
203 printf("%s: BUSERRAD =\t0x%08x\n", sc->sc_dev.dv_xname,
204 vrpciu_read(sc, VRPCIU_BUSERRADREG));
205 printf("%s: INTCNTSTA =\t0x%08x\n", sc->sc_dev.dv_xname,
206 vrpciu_read(sc, VRPCIU_INTCNTSTAREG));
207 printf("%s: EXACC =\t0x%08x\n", sc->sc_dev.dv_xname,
208 vrpciu_read(sc, VRPCIU_EXACCREG));
209 printf("%s: RECONT =\t0x%08x\n", sc->sc_dev.dv_xname,
210 vrpciu_read(sc, VRPCIU_RECONTREG));
211 printf("%s: PCIEN =\t0x%08x\n", sc->sc_dev.dv_xname,
212 vrpciu_read(sc, VRPCIU_ENREG));
213 printf("%s: CLOCKSEL =\t0x%08x\n", sc->sc_dev.dv_xname,
214 vrpciu_read(sc, VRPCIU_CLKSELREG));
215 printf("%s: TRDYV =\t0x%08x\n", sc->sc_dev.dv_xname,
216 vrpciu_read(sc, VRPCIU_TRDYVREG));
217 printf("%s: CLKRUN =\t0x%08x\n", sc->sc_dev.dv_xname,
218 vrpciu_read_2(sc, VRPCIU_CLKRUNREG));
219 printf("%s: IDREG =\t0x%08x\n", sc->sc_dev.dv_xname,
220 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_ID_REG));
221 reg = vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG);
222 printf("%s: CSR =\t\t0x%08x\n", sc->sc_dev.dv_xname, reg);
223 vrpciu_write(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG, reg);
224 printf("%s: CSR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
225 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_COMMAND_STATUS_REG));
226 printf("%s: CLASS =\t0x%08x\n", sc->sc_dev.dv_xname,
227 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_CLASS_REG));
228 printf("%s: BHLC =\t\t0x%08x\n", sc->sc_dev.dv_xname,
229 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_BHLC_REG));
230 printf("%s: MAIL =\t\t0x%08x\n", sc->sc_dev.dv_xname,
231 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MAILREG));
232 printf("%s: MBA1 =\t\t0x%08x\n", sc->sc_dev.dv_xname,
233 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MBA1REG));
234 printf("%s: MBA2 =\t\t0x%08x\n", sc->sc_dev.dv_xname,
235 vrpciu_read(sc, VRPCIU_CONF_BASE + VRPCIU_CONF_MBA2REG));
236 printf("%s: INTR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
237 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG));
238 #if 0
239 vrpciu_write(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG,
240 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG) | 0x01);
241 printf("%s: INTR =\t\t0x%08x\n", sc->sc_dev.dv_xname,
242 vrpciu_read(sc, VRPCIU_CONF_BASE + PCI_INTERRUPT_REG));
243 #endif
244 #endif
245
246 pc->pc_dev = &sc->sc_dev;
247 pc->pc_attach_hook = vrpciu_attach_hook;
248 pc->pc_bus_maxdevs = vrpciu_bus_maxdevs;
249 pc->pc_bus_devorder = vrc4173bcu_pci_bus_devorder;
250 pc->pc_make_tag = vrpciu_make_tag;
251 pc->pc_decompose_tag = vrpciu_decompose_tag;
252 pc->pc_conf_read = vrpciu_conf_read;
253 pc->pc_conf_write = vrpciu_conf_write;
254 pc->pc_intr_map = vrc4173bcu_pci_intr_map;
255 pc->pc_intr_string = vrc4173bcu_pci_intr_string;
256 pc->pc_intr_evcnt = vrc4173bcu_pci_intr_evcnt;
257 pc->pc_intr_establish = vrpciu_intr_establish;
258 pc->pc_intr_disestablish = vrpciu_intr_disestablish;
259 pc->pc_vrcintr_establish = vrpciu_vrcintr_establish;
260 pc->pc_vrcintr_disestablish = vrpciu_vrcintr_disestablish;
261
262 #if 0
263 {
264 int i;
265
266 for (i = 0; i < 8; i++)
267 printf("%s: ID_REG(0, 0, %d) = 0x%08x\n",
268 sc->sc_dev.dv_xname, i,
269 pci_conf_read(pc, pci_make_tag(pc, 0, 0, i),
270 PCI_ID_REG));
271 }
272 #endif
273
274 #if NPCI > 0
275 memset(&pba, 0, sizeof(pba));
276 pba.pba_busname = "pci";
277
278 /* For now, just inherit window mappings set by WinCE. XXX. */
279
280 iot = hpcmips_alloc_bus_space_tag();
281 reg = vrpciu_read(sc, VRPCIU_MIOAWREG);
282 snprintf(tmpbuf, sizeof(tmpbuf), "%s/iot",
283 sc->sc_dev.dv_xname);
284 hpcmips_init_bus_space(iot, (struct bus_space_tag_hpcmips *)sc->sc_iot,
285 tmpbuf, VRPCIU_MAW_ADDR(reg), VRPCIU_MAW_SIZE(reg));
286 pba.pba_iot = &iot->bst;
287
288 /*
289 * Just use system bus space tag. It works since WinCE maps
290 * PCI bus space at same offset. But this isn't right thing
291 * of course. XXX.
292 */
293 pba.pba_memt = sc->sc_iot;
294 pba.pba_dmat = &hpcmips_default_bus_dma_tag.bdt;
295 pba.pba_bus = 0;
296 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
297 PCI_FLAGS_MRL_OKAY;
298 pba.pba_pc = pc;
299
300 config_found(self, &pba, vrpciu_print);
301 #endif
302 }
303
304 #if NPCI > 0
305 static int
306 vrpciu_print(void *aux, const char *pnp)
307 {
308 struct pcibus_attach_args *pba = aux;
309
310 if (pnp != NULL)
311 printf("%s at %s", pba->pba_busname, pnp);
312 else
313 printf(" bus %d", pba->pba_bus);
314
315 return (UNCONF);
316 }
317 #endif
318
319 /*
320 * Handle PCI error interrupts.
321 */
322 int
323 vrpciu_intr(void *arg)
324 {
325 struct vrpciu_softc *sc = (struct vrpciu_softc *)arg;
326 u_int32_t isr;
327
328 isr = vrpciu_read(sc, VRPCIU_INTCNTSTAREG);
329 printf("%s: vrpciu_intr 0x%08x\n", sc->sc_dev.dv_xname, isr);
330 return ((isr & 0x0f) ? 1 : 0);
331 }
332
333 void
334 vrpciu_attach_hook(struct device *parent, struct device *self,
335 struct pcibus_attach_args *pba)
336 {
337
338 return;
339 }
340
341 int
342 vrpciu_bus_maxdevs(pci_chipset_tag_t pc, int busno)
343 {
344
345 return (32);
346 }
347
348 pcitag_t
349 vrpciu_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
350 {
351
352 return ((bus << 16) | (device << 11) | (function << 8));
353 }
354
355 void
356 vrpciu_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp,
357 int *fp)
358 {
359
360 if (bp != NULL)
361 *bp = (tag >> 16) & 0xff;
362 if (dp != NULL)
363 *dp = (tag >> 11) & 0x1f;
364 if (fp != NULL)
365 *fp = (tag >> 8) & 0x07;
366 }
367
368 pcireg_t
369 vrpciu_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
370 {
371 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
372 u_int32_t val;
373 int bus, device, function;
374
375 pci_decompose_tag(pc, tag, &bus, &device, &function);
376 if (bus == 0) {
377 if (device > 21)
378 return ((pcitag_t)-1);
379 tag = (1 << (device + 11)) | (function << 8); /* Type 0 */
380 } else
381 tag |= VRPCIU_CONF_TYPE1;
382
383 vrpciu_write(sc, VRPCIU_CONFAREG, tag | reg);
384 val = vrpciu_read(sc, VRPCIU_CONFDREG);
385 #if 0
386 printf("%s: conf_read: tag = 0x%08x, reg = 0x%x, val = 0x%08x\n",
387 sc->sc_dev.dv_xname, (u_int32_t)tag, reg, val);
388 #endif
389 return (val);
390 }
391
392 void
393 vrpciu_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg,
394 pcireg_t data)
395 {
396 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
397 int bus, device, function;
398
399 #if 0
400 printf("%s: conf_write: tag = 0x%08x, reg = 0x%x, val = 0x%08x\n",
401 sc->sc_dev.dv_xname, (u_int32_t)tag, reg, (u_int32_t)data);
402 #endif
403 vrpciu_decompose_tag(pc, tag, &bus, &device, &function);
404 if (bus == 0) {
405 if (device > 21)
406 return;
407 tag = (1 << (device + 11)) | (function << 8); /* Type 0 */
408 } else
409 tag |= VRPCIU_CONF_TYPE1;
410
411 vrpciu_write(sc, VRPCIU_CONFAREG, tag | reg);
412 vrpciu_write(sc, VRPCIU_CONFDREG, data);
413 }
414
415 void *
416 vrpciu_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
417 int (*func)(void *), void *arg)
418 {
419 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
420
421 if (ih == -1)
422 return (NULL);
423 DPRINTF(("vrpciu_intr_establish: %p\n", sc));
424 return (vrc4173bcu_intr_establish(sc->sc_bcu, ih, func, arg));
425 }
426
427 void
428 vrpciu_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
429 {
430 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
431
432 DPRINTF(("vrpciu_intr_disestablish: %p\n", sc));
433 vrc4173bcu_intr_disestablish(sc->sc_bcu, cookie);
434 }
435
436 void *
437 vrpciu_vrcintr_establish(pci_chipset_tag_t pc, int port,
438 int (*func)(void *), void *arg)
439 {
440 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
441 struct vrip_softc *vsc = (struct vrip_softc *)sc->sc_vc;
442 void *ih;
443
444 sc->sc_bcu = arg;
445 ih = hpcio_intr_establish(vsc->sc_gpio_chips[VRIP_IOCHIP_VRGIU],
446 port, HPCIO_INTR_LEVEL | HPCIO_INTR_LOW | HPCIO_INTR_HOLD,
447 func, arg);
448
449 return (ih);
450 }
451
452 void
453 vrpciu_vrcintr_disestablish(pci_chipset_tag_t pc, void *ih)
454 {
455 struct vrpciu_softc *sc = (struct vrpciu_softc *)pc->pc_dev;
456 struct vrip_softc *vsc = (struct vrip_softc *)sc->sc_vc;
457
458 return (vrip_intr_disestablish(vsc->sc_gpio_chips[VRIP_IOCHIP_VRGIU],
459 ih));
460 }
461