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