uninorth.c revision 1.9 1 1.9 lukem /* $NetBSD: uninorth.c,v 1.9 2003/07/15 02:43:34 lukem Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.1 tsubai * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 1.1 tsubai *
6 1.1 tsubai * Redistribution and use in source and binary forms, with or without
7 1.1 tsubai * modification, are permitted provided that the following conditions
8 1.1 tsubai * are met:
9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
10 1.1 tsubai * notice, this list of conditions and the following disclaimer.
11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
13 1.1 tsubai * documentation and/or other materials provided with the distribution.
14 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products
15 1.1 tsubai * derived from this software without specific prior written permission.
16 1.1 tsubai *
17 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 tsubai */
28 1.9 lukem
29 1.9 lukem #include <sys/cdefs.h>
30 1.9 lukem __KERNEL_RCSID(0, "$NetBSD: uninorth.c,v 1.9 2003/07/15 02:43:34 lukem Exp $");
31 1.1 tsubai
32 1.1 tsubai #include <sys/param.h>
33 1.1 tsubai #include <sys/device.h>
34 1.1 tsubai #include <sys/systm.h>
35 1.1 tsubai
36 1.1 tsubai #include <dev/pci/pcivar.h>
37 1.1 tsubai #include <dev/ofw/openfirm.h>
38 1.1 tsubai #include <dev/ofw/ofw_pci.h>
39 1.1 tsubai
40 1.1 tsubai #include <machine/autoconf.h>
41 1.1 tsubai
42 1.1 tsubai struct uninorth_softc {
43 1.1 tsubai struct device sc_dev;
44 1.1 tsubai struct pci_bridge sc_pc;
45 1.1 tsubai };
46 1.1 tsubai
47 1.1 tsubai void uninorth_attach __P((struct device *, struct device *, void *));
48 1.1 tsubai int uninorth_match __P((struct device *, struct cfdata *, void *));
49 1.1 tsubai int uninorth_print __P((void *, const char *));
50 1.1 tsubai
51 1.1 tsubai pcireg_t uninorth_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
52 1.1 tsubai void uninorth_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
53 1.1 tsubai
54 1.6 thorpej CFATTACH_DECL(uninorth, sizeof(struct uninorth_softc),
55 1.6 thorpej uninorth_match, uninorth_attach, NULL, NULL);
56 1.1 tsubai
57 1.1 tsubai int
58 1.1 tsubai uninorth_match(parent, cf, aux)
59 1.1 tsubai struct device *parent;
60 1.1 tsubai struct cfdata *cf;
61 1.1 tsubai void *aux;
62 1.1 tsubai {
63 1.1 tsubai struct confargs *ca = aux;
64 1.1 tsubai char compat[32];
65 1.1 tsubai
66 1.1 tsubai if (strcmp(ca->ca_name, "pci") != 0)
67 1.1 tsubai return 0;
68 1.1 tsubai
69 1.2 wiz memset(compat, 0, sizeof(compat));
70 1.1 tsubai OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
71 1.1 tsubai if (strcmp(compat, "uni-north") != 0)
72 1.1 tsubai return 0;
73 1.1 tsubai
74 1.1 tsubai return 1;
75 1.1 tsubai }
76 1.1 tsubai
77 1.1 tsubai void
78 1.1 tsubai uninorth_attach(parent, self, aux)
79 1.1 tsubai struct device *parent, *self;
80 1.1 tsubai void *aux;
81 1.1 tsubai {
82 1.1 tsubai struct uninorth_softc *sc = (void *)self;
83 1.1 tsubai pci_chipset_tag_t pc = &sc->sc_pc;
84 1.1 tsubai struct confargs *ca = aux;
85 1.1 tsubai struct pcibus_attach_args pba;
86 1.1 tsubai int len, child, node = ca->ca_node;
87 1.1 tsubai u_int32_t reg[2], busrange[2];
88 1.1 tsubai struct ranges {
89 1.1 tsubai u_int32_t pci_hi, pci_mid, pci_lo;
90 1.1 tsubai u_int32_t host;
91 1.1 tsubai u_int32_t size_hi, size_lo;
92 1.1 tsubai } ranges[6], *rp = ranges;
93 1.1 tsubai
94 1.1 tsubai printf("\n");
95 1.1 tsubai
96 1.1 tsubai /* UniNorth address */
97 1.1 tsubai if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
98 1.1 tsubai return;
99 1.1 tsubai
100 1.1 tsubai /* PCI bus number */
101 1.1 tsubai if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
102 1.1 tsubai return;
103 1.1 tsubai
104 1.1 tsubai pc->node = node;
105 1.1 tsubai pc->addr = mapiodev(reg[0] + 0x800000, 4);
106 1.1 tsubai pc->data = mapiodev(reg[0] + 0xc00000, 8);
107 1.1 tsubai pc->bus = busrange[0];
108 1.1 tsubai pc->conf_read = uninorth_conf_read;
109 1.1 tsubai pc->conf_write = uninorth_conf_write;
110 1.1 tsubai pc->memt = (bus_space_tag_t)0;
111 1.1 tsubai
112 1.1 tsubai /* find i/o tag */
113 1.1 tsubai len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
114 1.1 tsubai if (len == -1)
115 1.1 tsubai return;
116 1.1 tsubai while (len >= sizeof(ranges[0])) {
117 1.1 tsubai if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
118 1.1 tsubai OFW_PCI_PHYS_HI_SPACE_IO)
119 1.1 tsubai pc->iot = (bus_space_tag_t)rp->host;
120 1.1 tsubai len -= sizeof(ranges[0]);
121 1.1 tsubai rp++;
122 1.1 tsubai }
123 1.1 tsubai
124 1.1 tsubai /* XXX enable gmac ethernet */
125 1.1 tsubai for (child = OF_child(node); child; child = OF_peer(child)) {
126 1.1 tsubai volatile int *gmac_gbclock_en = (void *)0xf8000020;
127 1.1 tsubai char compat[32];
128 1.1 tsubai
129 1.2 wiz memset(compat, 0, sizeof(compat));
130 1.1 tsubai OF_getprop(child, "compatible", compat, sizeof(compat));
131 1.1 tsubai if (strcmp(compat, "gmac") == 0)
132 1.1 tsubai *gmac_gbclock_en |= 0x02;
133 1.1 tsubai }
134 1.1 tsubai
135 1.2 wiz memset(&pba, 0, sizeof(pba));
136 1.1 tsubai pba.pba_busname = "pci";
137 1.1 tsubai pba.pba_memt = pc->memt;
138 1.1 tsubai pba.pba_iot = pc->iot;
139 1.1 tsubai pba.pba_dmat = &pci_bus_dma_tag;
140 1.8 fvdl pba.pba_dmat64 = NULL;
141 1.1 tsubai pba.pba_bus = pc->bus;
142 1.4 thorpej pba.pba_bridgetag = NULL;
143 1.1 tsubai pba.pba_pc = pc;
144 1.1 tsubai pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
145 1.1 tsubai
146 1.1 tsubai config_found(self, &pba, uninorth_print);
147 1.1 tsubai }
148 1.1 tsubai
149 1.1 tsubai int
150 1.1 tsubai uninorth_print(aux, pnp)
151 1.1 tsubai void *aux;
152 1.1 tsubai const char *pnp;
153 1.1 tsubai {
154 1.1 tsubai struct pcibus_attach_args *pa = aux;
155 1.1 tsubai
156 1.1 tsubai if (pnp)
157 1.7 thorpej aprint_normal("%s at %s", pa->pba_busname, pnp);
158 1.7 thorpej aprint_normal(" bus %d", pa->pba_bus);
159 1.1 tsubai return UNCONF;
160 1.1 tsubai }
161 1.1 tsubai
162 1.1 tsubai pcireg_t
163 1.1 tsubai uninorth_conf_read(pc, tag, reg)
164 1.1 tsubai pci_chipset_tag_t pc;
165 1.1 tsubai pcitag_t tag;
166 1.1 tsubai int reg;
167 1.1 tsubai {
168 1.1 tsubai int32_t *daddr = pc->data;
169 1.1 tsubai pcireg_t data;
170 1.1 tsubai int bus, dev, func, s;
171 1.1 tsubai u_int32_t x;
172 1.1 tsubai
173 1.1 tsubai /* UniNorth seems to have a 64bit data port */
174 1.1 tsubai if (reg & 0x04)
175 1.1 tsubai daddr++;
176 1.1 tsubai
177 1.1 tsubai pci_decompose_tag(pc, tag, &bus, &dev, &func);
178 1.1 tsubai
179 1.1 tsubai /*
180 1.1 tsubai * bandit's minimum device number of the first bus is 11.
181 1.1 tsubai * So we behave as if there is no device when dev < 11.
182 1.1 tsubai */
183 1.1 tsubai if (func > 7)
184 1.1 tsubai panic("pci_conf_read: func > 7");
185 1.1 tsubai
186 1.1 tsubai if (bus == pc->bus) {
187 1.3 nathanw if (dev < 11)
188 1.3 nathanw return 0xffffffff;
189 1.1 tsubai x = (1 << dev) | (func << 8) | reg;
190 1.1 tsubai } else
191 1.1 tsubai x = tag | reg | 1;
192 1.1 tsubai
193 1.1 tsubai s = splhigh();
194 1.1 tsubai
195 1.1 tsubai out32rb(pc->addr, x);
196 1.1 tsubai in32rb(pc->addr);
197 1.1 tsubai data = 0xffffffff;
198 1.1 tsubai if (!badaddr(daddr, 4))
199 1.1 tsubai data = in32rb(daddr);
200 1.1 tsubai out32rb(pc->addr, 0);
201 1.1 tsubai in32rb(pc->addr);
202 1.1 tsubai splx(s);
203 1.1 tsubai
204 1.1 tsubai return data;
205 1.1 tsubai }
206 1.1 tsubai
207 1.1 tsubai void
208 1.1 tsubai uninorth_conf_write(pc, tag, reg, data)
209 1.1 tsubai pci_chipset_tag_t pc;
210 1.1 tsubai pcitag_t tag;
211 1.1 tsubai int reg;
212 1.1 tsubai pcireg_t data;
213 1.1 tsubai {
214 1.1 tsubai int32_t *daddr = pc->data;
215 1.1 tsubai int bus, dev, func, s;
216 1.1 tsubai u_int32_t x;
217 1.1 tsubai
218 1.1 tsubai /* UniNorth seems to have a 64bit data port */
219 1.1 tsubai if (reg & 0x04)
220 1.1 tsubai daddr++;
221 1.1 tsubai
222 1.1 tsubai pci_decompose_tag(pc, tag, &bus, &dev, &func);
223 1.1 tsubai
224 1.1 tsubai if (func > 7)
225 1.1 tsubai panic("pci_conf_write: func > 7");
226 1.1 tsubai
227 1.1 tsubai if (bus == pc->bus) {
228 1.1 tsubai if (dev < 11)
229 1.1 tsubai panic("pci_conf_write: dev < 11");
230 1.1 tsubai x = (1 << dev) | (func << 8) | reg;
231 1.1 tsubai } else
232 1.1 tsubai x = tag | reg | 1;
233 1.1 tsubai
234 1.1 tsubai s = splhigh();
235 1.1 tsubai
236 1.1 tsubai out32rb(pc->addr, x);
237 1.1 tsubai in32rb(pc->addr);
238 1.1 tsubai out32rb(daddr, data);
239 1.1 tsubai out32rb(pc->addr, 0);
240 1.1 tsubai in32rb(pc->addr);
241 1.1 tsubai
242 1.1 tsubai splx(s);
243 1.1 tsubai }
244