1 /*        $NetBSD: iq80321_pci.c,v 1.10 2018/11/16 15:06:23 jmcneill Exp $      */
2 
3 /*
4  * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed for the NetBSD Project by
20  *        Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * IQ80321 PCI interrupt support.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: iq80321_pci.c,v 1.10 2018/11/16 15:06:23 jmcneill Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/bus.h>
49 
50 #include <dev/pci/pcidevs.h>
51 #include <dev/pci/ppbreg.h>
52 
53 #include <arm/locore.h>
54 
55 #include <machine/autoconf.h>
56 
57 #include <evbarm/iq80321/iq80321reg.h>
58 #include <evbarm/iq80321/iq80321var.h>
59 
60 #include <arm/xscale/i80321reg.h>
61 #include <arm/xscale/i80321var.h>
62 
63 int       iq80321_pci_intr_map(const struct pci_attach_args *,
64               pci_intr_handle_t *);
65 const char *iq80321_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
66 const struct evcnt *iq80321_pci_intr_evcnt(void *, pci_intr_handle_t);
67 void      *iq80321_pci_intr_establish(void *, pci_intr_handle_t,
68               int, int (*func)(void *), void *, const char *);
69 void      iq80321_pci_intr_disestablish(void *, void *);
70 
71 void
iq80321_pci_init(pci_chipset_tag_t pc,void * cookie)72 iq80321_pci_init(pci_chipset_tag_t pc, void *cookie)
73 {
74 
75           pc->pc_intr_v = cookie;                 /* the i80321 softc */
76           pc->pc_intr_map = iq80321_pci_intr_map;
77           pc->pc_intr_string = iq80321_pci_intr_string;
78           pc->pc_intr_evcnt = iq80321_pci_intr_evcnt;
79           pc->pc_intr_establish = iq80321_pci_intr_establish;
80           pc->pc_intr_disestablish = iq80321_pci_intr_disestablish;
81 }
82 
83 int
iq80321_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)84 iq80321_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
85 {
86           struct i80321_softc *sc = pa->pa_pc->pc_intr_v;
87           int b, d, f;
88           uint32_t busno;
89 
90           /*
91            * The IQ80321's interrupts are routed like so:
92            *
93            *        XINT0     i82544 Gig-E
94            *
95            *        XINT1     UART
96            *
97            *        XINT2     INTA# from S-PCI-X slot
98            *
99            *        XINT3     INTB# from S-PCI-X slot
100            */
101 
102           busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
103           busno = PCIXSR_BUSNO(busno);
104           if (busno == 0xff)
105                     busno = 0;
106 
107           pci_decompose_tag(pa->pa_pc, pa->pa_intrtag, &b, &d, &f);
108 
109           /* No mappings for devices not on our bus. */
110           if (b != busno)
111                     goto no_mapping;
112 
113           switch (d) {
114           case 4:                       /* i82544 Gig-E */
115                     if (pa->pa_intrpin == 1) {
116                               *ihp = ICU_INT_XINT(0);
117                               return (0);
118                     }
119                     goto no_mapping;
120 
121           case 6:                       /* S-PCI-X slot */
122                     if (pa->pa_intrpin == 1) {
123                               *ihp = ICU_INT_XINT(2);
124                               return (0);
125                     }
126                     if (pa->pa_intrpin == 2) {
127                               *ihp = ICU_INT_XINT(3);
128                               return (0);
129                     }
130                     goto no_mapping;
131 
132           default:
133  no_mapping:
134                     printf("iq80321_pci_intr_map: no mapping for %d/%d/%d\n",
135                         pa->pa_bus, pa->pa_device, pa->pa_function);
136                     return (1);
137           }
138 
139           return (0);
140 }
141 
142 const char *
iq80321_pci_intr_string(void * v,pci_intr_handle_t ih,char * buf,size_t len)143 iq80321_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
144 {
145 
146           strlcpy(buf, i80321_irqnames[ih], len);
147           return buf;
148 }
149 
150 const struct evcnt *
iq80321_pci_intr_evcnt(void * v,pci_intr_handle_t ih)151 iq80321_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
152 {
153 
154           /* XXX For now. */
155           return (NULL);
156 }
157 
158 void *
iq80321_pci_intr_establish(void * v,pci_intr_handle_t ih,int ipl,int (* func)(void *),void * arg,const char * xname)159 iq80321_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
160     int (*func)(void *), void *arg, const char *xname)
161 {
162 
163           return (i80321_intr_establish(ih, ipl, func, arg));
164 }
165 
166 void
iq80321_pci_intr_disestablish(void * v,void * cookie)167 iq80321_pci_intr_disestablish(void *v, void *cookie)
168 {
169 
170           i80321_intr_disestablish(cookie);
171 }
172