u3.c revision 1.1.28.1 1 /*
2 * Copyright 2006 Kyma Systems LLC.
3 * All rights reserved.
4 *
5 * Written by Sanjay Lal <sanjayl (at) kymasys.com>
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Kyma Systems LLC.
19 * 4. The name of Kyma Systems LLC may not be used to endorse
20 * or promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY KYMA SYSTEMS LLC ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KYMA SYSTEMS LLC
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/systm.h>
41
42 #include <dev/pci/pcivar.h>
43 #include <dev/ofw/openfirm.h>
44 #include <dev/ofw/ofw_pci.h>
45
46 #include <machine/autoconf.h>
47
48 struct ibmcpc_softc
49 {
50 struct device sc_dev;
51 struct pci_bridge sc_pc[8];
52 };
53
54 static void ibmcpc_attach(struct device *, struct device *, void *);
55 static int ibmcpc_match(struct device *, struct cfdata *, void *);
56
57 static pcireg_t ibmcpc_conf_read(pci_chipset_tag_t, pcitag_t, int);
58 static void ibmcpc_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
59
60 CFATTACH_DECL(ibmcpc, sizeof(struct ibmcpc_softc),
61 ibmcpc_match, ibmcpc_attach, NULL, NULL);
62
63 #define PCI_DEVFN(slot,func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
64
65 static int
66 ibmcpc_match(struct device *parent, struct cfdata *cf, void *aux)
67 {
68 struct confargs *ca = aux;
69 char compat[32];
70
71 if (strcmp(ca->ca_name, "ht") != 0)
72 return 0;
73
74 memset(compat, 0, sizeof(compat));
75 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
76
77 if (strcmp(compat, "u3-ht") != 0)
78 return 0;
79
80 return 1;
81 }
82
83 static void
84 ibmcpc_attach(struct device *parent, struct device *self, void *aux)
85 {
86 struct ibmcpc_softc *sc = (void *) self;
87 pci_chipset_tag_t pc = sc->sc_pc;
88 struct confargs *ca = aux;
89 struct pcibus_attach_args pba;
90 int node = ca->ca_node, child;
91 u_int32_t reg[6], i;
92 void *pc_data;
93 char name[32];
94 u_int32_t busrange[2];
95
96 printf("\n");
97
98 /* u3 address */
99 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
100 return;
101 }
102 printf("Mapping in config space @ pa 0x%08x, size: 0x%08x\n", reg[1],
103 reg[2]);
104 pc_data = mapiodev(reg[1], reg[2]);
105
106 for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
107 child = OF_peer(child), i++) {
108 memset(name, 0, sizeof(name));
109
110 if (OF_getprop(child, "name", name, sizeof(name)) == -1)
111 continue;
112
113 if (strcmp(name, "pci") != 0)
114 continue;
115
116 pc->node = child;
117
118 if (OF_getprop(child, "bus-range", busrange, 8) < 8)
119 continue;
120
121 pc->bus = busrange[0];
122 pc->addr = 0x0;
123 pc->data = pc_data;
124 pc->conf_read = ibmcpc_conf_read;
125 pc->conf_write = ibmcpc_conf_write;
126 pc->memt = (bus_space_tag_t) 0;
127 pc->iot = (bus_space_tag_t) 0;
128
129 memset(&pba, 0, sizeof(pba));
130 pba.pba_memt = pc->memt;
131 pba.pba_iot = pc->iot;
132 pba.pba_dmat = &pci_bus_dma_tag;
133 pba.pba_dmat64 = NULL;
134 pba.pba_bridgetag = NULL;
135 pba.pba_pc = pc;
136 pba.pba_bus = pc->bus;
137 pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
138 config_found_ia(self, "pcibus", &pba, pcibusprint);
139
140 pc++;
141 }
142 }
143
144 static pcireg_t
145 ibmcpc_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
146 {
147 u_int32_t daddr = (u_int32_t) pc->data;
148 pcireg_t data;
149 u_int32_t bus, dev, func, x, devfn;
150
151 pci_decompose_tag(pc, tag, &bus, &dev, &func);
152
153 devfn = PCI_DEVFN(dev, func);
154
155 if (bus == 0) {
156
157 if (devfn == 0x0) {
158 data = 0xffffffff;
159 goto done;
160 }
161
162 x = daddr + ((devfn << 8) | reg);
163 }
164 else
165 x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
166
167 data = in32rb(x);
168
169 done:
170 return data;
171 }
172
173 static void
174 ibmcpc_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
175 {
176 int32_t *daddr = pc->data;
177 u_int32_t bus, dev, func;
178 u_int32_t x, devfn;
179
180 pci_decompose_tag(pc, tag, &bus, &dev, &func);
181
182 devfn = PCI_DEVFN(dev, func);
183
184 if (bus == 0) {
185 if (devfn == 0x0) {
186 goto done;
187 }
188 x = (u_int32_t) daddr + ((devfn << 8) | reg);
189 }
190 else
191 x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
192 0x01000000UL;
193
194 out32rb(x, data);
195
196 done:
197 return;
198 }
199