1 /* $NetBSD: u3.c,v 1.12 2022/01/21 19:12:28 thorpej Exp $ */
2
3 /*
4 * Copyright 2006 Kyma Systems LLC.
5 * All rights reserved.
6 *
7 * Written by Sanjay Lal <sanjayl@kymasys.com>
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 * Kyma Systems LLC.
21 * 4. The name of Kyma Systems LLC 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 KYMA SYSTEMS LLC ``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 KYMA SYSTEMS LLC
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 #include <sys/cdefs.h>
39
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43
44 #include <dev/pci/pcivar.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_pci.h>
47
48 #include <machine/autoconf.h>
49 #include <machine/pio.h>
50
51 struct ibmcpc_softc
52 {
53 device_t sc_dev;
54 struct genppc_pci_chipset sc_pc[8];
55 struct powerpc_bus_space sc_iot;
56 struct powerpc_bus_space sc_memt;
57 int sc_ranges[8];
58 };
59
60 /* kinda ugly but there can be only one */
61 static struct ibmcpc_softc *cpc0 = NULL;
62
63 static void ibmcpc_attach(device_t, device_t, void *);
64 static int ibmcpc_match(device_t, cfdata_t, void *);
65
66 static pcireg_t ibmcpc_conf_read(void *, pcitag_t, int);
67 static void ibmcpc_conf_write(void *, pcitag_t, int, pcireg_t);
68
69 CFATTACH_DECL_NEW(ibmcpc, sizeof(struct ibmcpc_softc),
70 ibmcpc_match, ibmcpc_attach, NULL, NULL);
71
72 #define PCI_DEVFN(slot,func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
73
74 static int
ibmcpc_match(device_t parent,cfdata_t cf,void * aux)75 ibmcpc_match(device_t parent, cfdata_t cf, void *aux)
76 {
77 struct confargs *ca = aux;
78 char compat[32];
79
80 if (strcmp(ca->ca_name, "ht") != 0)
81 return 0;
82
83 memset(compat, 0, sizeof(compat));
84 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
85
86 if (strcmp(compat, "u3-ht") != 0)
87 return 0;
88
89 return 1;
90 }
91
92 static void
ibmcpc_attach(device_t parent,device_t self,void * aux)93 ibmcpc_attach(device_t parent, device_t self, void *aux)
94 {
95 struct ibmcpc_softc *sc = device_private(self);
96 pci_chipset_tag_t pc = sc->sc_pc;
97 struct confargs *ca = aux;
98 struct pcibus_attach_args pba;
99 int node = ca->ca_node, child;
100 uint32_t reg[6], busrange[2], i;
101 void *pc_data;
102 char name[32];
103
104 aprint_normal("\n");
105 sc->sc_dev = self;
106
107 /* u3 address */
108 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
109 return;
110 }
111 aprint_normal("Mapping in config space @ pa 0x%08x, size: 0x%08x\n",
112 reg[1], reg[2]);
113 pc_data = mapiodev(reg[1], reg[2], false);
114
115 cpc0 = sc;
116
117 for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
118 child = OF_peer(child), i++) {
119
120 memset(name, 0, sizeof(name));
121
122 if (OF_getprop(child, "name", name, sizeof(name)) == -1)
123 continue;
124
125 if (strcmp(name, "pci") != 0)
126 continue;
127
128 if (OF_getprop(child, "bus-range", busrange, 8) < 8)
129 continue;
130
131 memset(&sc->sc_iot, 0, sizeof(sc->sc_iot));
132 sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
133 _BUS_SPACE_IO_TYPE;
134 sc->sc_iot.pbs_base = 0x00000000;
135 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, child,
136 &sc->sc_iot, "ibmcpc io") != 0)
137 panic("Can't init ibmcpc io tag");
138
139 memset(&sc->sc_memt, 0, sizeof(sc->sc_memt));
140 sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
141 _BUS_SPACE_MEM_TYPE;
142 sc->sc_memt.pbs_base = 0x00000000;
143 if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, child,
144 &sc->sc_memt, "ibmcpc mem") != 0)
145 panic("Can't init ibmcpc mem tag");
146
147 macppc_pci_get_chipset_tag(pc);
148 pc->pc_node = child;
149 pc->pc_bus = busrange[0];
150 sc->sc_ranges[pc->pc_bus] = busrange[1];
151 pc->pc_addr = 0x0;
152 pc->pc_data = pc_data;
153 pc->pc_conf_read = ibmcpc_conf_read;
154 pc->pc_conf_write = ibmcpc_conf_write;
155 pc->pc_memt = &sc->sc_memt;
156 pc->pc_iot = &sc->sc_iot;
157
158 memset(&pba, 0, sizeof(pba));
159 pba.pba_memt = pc->pc_memt;
160 pba.pba_iot = pc->pc_iot;
161 pba.pba_dmat = &pci_bus_dma_tag;
162 pba.pba_dmat64 = NULL;
163 pba.pba_bridgetag = NULL;
164 pba.pba_pc = pc;
165 pba.pba_bus = pc->pc_bus;
166 pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
167
168 config_found(self, &pba, pcibusprint,
169 CFARGS(.devhandle = device_handle(self)));
170
171 pc++;
172 }
173 }
174
175 static pcireg_t
ibmcpc_conf_read(void * cookie,pcitag_t tag,int reg)176 ibmcpc_conf_read(void *cookie, pcitag_t tag, int reg)
177 {
178 pci_chipset_tag_t pc = cookie;
179 struct ibmcpc_softc *sc = cpc0;
180 u_int32_t daddr = (u_int32_t) pc->pc_data;
181 pcireg_t data;
182 u_int32_t bus, dev, func, x, devfn;
183
184 if ((unsigned int)reg >= PCI_CONF_SIZE)
185 return (pcireg_t) -1;
186
187 pci_decompose_tag(pc, tag, &bus, &dev, &func);
188
189 if ((bus < pc->pc_bus) || (bus > sc->sc_ranges[pc->pc_bus])) {
190 data = 0xffffffff;
191 goto done;
192 }
193
194 devfn = PCI_DEVFN(dev, func);
195
196 if (bus == 0) {
197
198 if (devfn == 0x0) {
199
200 data = 0xffffffff;
201 goto done;
202 }
203
204 x = daddr + ((devfn << 8) | reg);
205 } else
206 x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
207
208 data = in32rb(x);
209
210 done:
211 return data;
212 }
213
214 static void
ibmcpc_conf_write(void * cookie,pcitag_t tag,int reg,pcireg_t data)215 ibmcpc_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
216 {
217 pci_chipset_tag_t pc = cookie;
218 struct ibmcpc_softc *sc = cpc0;
219 int32_t *daddr = pc->pc_data;
220 u_int32_t bus, dev, func;
221 u_int32_t x, devfn;
222
223 if ((unsigned int)reg >= PCI_CONF_SIZE)
224 return;
225
226 pci_decompose_tag(pc, tag, &bus, &dev, &func);
227
228 if ((bus < pc->pc_bus) || (bus > sc->sc_ranges[pc->pc_bus]))
229 return;
230
231 devfn = PCI_DEVFN(dev, func);
232
233 if (bus == 0) {
234
235 if (devfn == 0x0) {
236 goto done;
237 }
238 x = (u_int32_t) daddr + ((devfn << 8) | reg);
239 } else
240 x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
241 0x01000000UL;
242
243 out32rb(x, data);
244
245 done:
246 return;
247 }
248