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