u3.c revision 1.1.28.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.28.1 macallan static void ibmcpc_attach(struct device *, struct device *, void *);
55 1.1.28.1 macallan static int ibmcpc_match(struct device *, struct cfdata *, void *);
56 1.1 sanjayl
57 1.1.28.1 macallan static pcireg_t ibmcpc_conf_read(pci_chipset_tag_t, pcitag_t, int);
58 1.1.28.1 macallan static void ibmcpc_conf_write(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.28.1 macallan static int
66 1.1.28.1 macallan ibmcpc_match(struct device *parent, struct cfdata *cf, void *aux)
67 1.1 sanjayl {
68 1.1 sanjayl struct confargs *ca = aux;
69 1.1 sanjayl char compat[32];
70 1.1 sanjayl
71 1.1 sanjayl if (strcmp(ca->ca_name, "ht") != 0)
72 1.1 sanjayl return 0;
73 1.1 sanjayl
74 1.1 sanjayl memset(compat, 0, sizeof(compat));
75 1.1 sanjayl OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
76 1.1 sanjayl
77 1.1 sanjayl if (strcmp(compat, "u3-ht") != 0)
78 1.1 sanjayl return 0;
79 1.1 sanjayl
80 1.1 sanjayl return 1;
81 1.1 sanjayl }
82 1.1 sanjayl
83 1.1.28.1 macallan static void
84 1.1.28.1 macallan ibmcpc_attach(struct device *parent, struct device *self, void *aux)
85 1.1 sanjayl {
86 1.1 sanjayl struct ibmcpc_softc *sc = (void *) self;
87 1.1 sanjayl pci_chipset_tag_t pc = sc->sc_pc;
88 1.1 sanjayl struct confargs *ca = aux;
89 1.1 sanjayl struct pcibus_attach_args pba;
90 1.1 sanjayl int node = ca->ca_node, child;
91 1.1 sanjayl u_int32_t reg[6], i;
92 1.1 sanjayl void *pc_data;
93 1.1 sanjayl char name[32];
94 1.1 sanjayl u_int32_t busrange[2];
95 1.1 sanjayl
96 1.1 sanjayl printf("\n");
97 1.1 sanjayl
98 1.1 sanjayl /* u3 address */
99 1.1 sanjayl if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
100 1.1 sanjayl return;
101 1.1 sanjayl }
102 1.1 sanjayl printf("Mapping in config space @ pa 0x%08x, size: 0x%08x\n", reg[1],
103 1.1 sanjayl reg[2]);
104 1.1 sanjayl pc_data = mapiodev(reg[1], reg[2]);
105 1.1 sanjayl
106 1.1 sanjayl for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
107 1.1 sanjayl child = OF_peer(child), i++) {
108 1.1 sanjayl memset(name, 0, sizeof(name));
109 1.1 sanjayl
110 1.1 sanjayl if (OF_getprop(child, "name", name, sizeof(name)) == -1)
111 1.1 sanjayl continue;
112 1.1 sanjayl
113 1.1 sanjayl if (strcmp(name, "pci") != 0)
114 1.1 sanjayl continue;
115 1.1 sanjayl
116 1.1 sanjayl pc->node = child;
117 1.1 sanjayl
118 1.1 sanjayl if (OF_getprop(child, "bus-range", busrange, 8) < 8)
119 1.1 sanjayl continue;
120 1.1 sanjayl
121 1.1 sanjayl pc->bus = busrange[0];
122 1.1 sanjayl pc->addr = 0x0;
123 1.1 sanjayl pc->data = pc_data;
124 1.1 sanjayl pc->conf_read = ibmcpc_conf_read;
125 1.1 sanjayl pc->conf_write = ibmcpc_conf_write;
126 1.1 sanjayl pc->memt = (bus_space_tag_t) 0;
127 1.1 sanjayl pc->iot = (bus_space_tag_t) 0;
128 1.1 sanjayl
129 1.1 sanjayl memset(&pba, 0, sizeof(pba));
130 1.1 sanjayl pba.pba_memt = pc->memt;
131 1.1 sanjayl pba.pba_iot = pc->iot;
132 1.1 sanjayl pba.pba_dmat = &pci_bus_dma_tag;
133 1.1 sanjayl pba.pba_dmat64 = NULL;
134 1.1 sanjayl pba.pba_bridgetag = NULL;
135 1.1 sanjayl pba.pba_pc = pc;
136 1.1 sanjayl pba.pba_bus = pc->bus;
137 1.1 sanjayl pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
138 1.1 sanjayl config_found_ia(self, "pcibus", &pba, pcibusprint);
139 1.1 sanjayl
140 1.1 sanjayl pc++;
141 1.1 sanjayl }
142 1.1 sanjayl }
143 1.1 sanjayl
144 1.1.28.1 macallan static pcireg_t
145 1.1.28.1 macallan ibmcpc_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
146 1.1 sanjayl {
147 1.1 sanjayl u_int32_t daddr = (u_int32_t) pc->data;
148 1.1 sanjayl pcireg_t data;
149 1.1 sanjayl u_int32_t bus, dev, func, x, devfn;
150 1.1 sanjayl
151 1.1 sanjayl pci_decompose_tag(pc, tag, &bus, &dev, &func);
152 1.1 sanjayl
153 1.1 sanjayl devfn = PCI_DEVFN(dev, func);
154 1.1 sanjayl
155 1.1 sanjayl if (bus == 0) {
156 1.1 sanjayl
157 1.1 sanjayl if (devfn == 0x0) {
158 1.1 sanjayl data = 0xffffffff;
159 1.1 sanjayl goto done;
160 1.1 sanjayl }
161 1.1 sanjayl
162 1.1 sanjayl x = daddr + ((devfn << 8) | reg);
163 1.1 sanjayl }
164 1.1 sanjayl else
165 1.1 sanjayl x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
166 1.1 sanjayl
167 1.1 sanjayl data = in32rb(x);
168 1.1 sanjayl
169 1.1 sanjayl done:
170 1.1 sanjayl return data;
171 1.1 sanjayl }
172 1.1 sanjayl
173 1.1.28.1 macallan static void
174 1.1.28.1 macallan ibmcpc_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
175 1.1 sanjayl {
176 1.1 sanjayl int32_t *daddr = pc->data;
177 1.1 sanjayl u_int32_t bus, dev, func;
178 1.1 sanjayl u_int32_t x, devfn;
179 1.1 sanjayl
180 1.1 sanjayl pci_decompose_tag(pc, tag, &bus, &dev, &func);
181 1.1 sanjayl
182 1.1 sanjayl devfn = PCI_DEVFN(dev, func);
183 1.1 sanjayl
184 1.1 sanjayl if (bus == 0) {
185 1.1 sanjayl if (devfn == 0x0) {
186 1.1 sanjayl goto done;
187 1.1 sanjayl }
188 1.1 sanjayl x = (u_int32_t) daddr + ((devfn << 8) | reg);
189 1.1 sanjayl }
190 1.1 sanjayl else
191 1.1 sanjayl x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
192 1.1 sanjayl 0x01000000UL;
193 1.1 sanjayl
194 1.1 sanjayl out32rb(x, data);
195 1.1 sanjayl
196 1.1 sanjayl done:
197 1.1 sanjayl return;
198 1.1 sanjayl }
199