uninorth.c revision 1.9 1 /* $NetBSD: uninorth.c,v 1.9 2003/07/15 02:43:34 lukem 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/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: uninorth.c,v 1.9 2003/07/15 02:43:34 lukem Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35
36 #include <dev/pci/pcivar.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39
40 #include <machine/autoconf.h>
41
42 struct uninorth_softc {
43 struct device sc_dev;
44 struct pci_bridge sc_pc;
45 };
46
47 void uninorth_attach __P((struct device *, struct device *, void *));
48 int uninorth_match __P((struct device *, struct cfdata *, void *));
49 int uninorth_print __P((void *, const char *));
50
51 pcireg_t uninorth_conf_read __P((pci_chipset_tag_t, pcitag_t, int));
52 void uninorth_conf_write __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t));
53
54 CFATTACH_DECL(uninorth, sizeof(struct uninorth_softc),
55 uninorth_match, uninorth_attach, NULL, NULL);
56
57 int
58 uninorth_match(parent, cf, aux)
59 struct device *parent;
60 struct cfdata *cf;
61 void *aux;
62 {
63 struct confargs *ca = aux;
64 char compat[32];
65
66 if (strcmp(ca->ca_name, "pci") != 0)
67 return 0;
68
69 memset(compat, 0, sizeof(compat));
70 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
71 if (strcmp(compat, "uni-north") != 0)
72 return 0;
73
74 return 1;
75 }
76
77 void
78 uninorth_attach(parent, self, aux)
79 struct device *parent, *self;
80 void *aux;
81 {
82 struct uninorth_softc *sc = (void *)self;
83 pci_chipset_tag_t pc = &sc->sc_pc;
84 struct confargs *ca = aux;
85 struct pcibus_attach_args pba;
86 int len, child, node = ca->ca_node;
87 u_int32_t reg[2], busrange[2];
88 struct ranges {
89 u_int32_t pci_hi, pci_mid, pci_lo;
90 u_int32_t host;
91 u_int32_t size_hi, size_lo;
92 } ranges[6], *rp = ranges;
93
94 printf("\n");
95
96 /* UniNorth address */
97 if (OF_getprop(node, "reg", reg, sizeof(reg)) < 8)
98 return;
99
100 /* PCI bus number */
101 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
102 return;
103
104 pc->node = node;
105 pc->addr = mapiodev(reg[0] + 0x800000, 4);
106 pc->data = mapiodev(reg[0] + 0xc00000, 8);
107 pc->bus = busrange[0];
108 pc->conf_read = uninorth_conf_read;
109 pc->conf_write = uninorth_conf_write;
110 pc->memt = (bus_space_tag_t)0;
111
112 /* find i/o tag */
113 len = OF_getprop(node, "ranges", ranges, sizeof(ranges));
114 if (len == -1)
115 return;
116 while (len >= sizeof(ranges[0])) {
117 if ((rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
118 OFW_PCI_PHYS_HI_SPACE_IO)
119 pc->iot = (bus_space_tag_t)rp->host;
120 len -= sizeof(ranges[0]);
121 rp++;
122 }
123
124 /* XXX enable gmac ethernet */
125 for (child = OF_child(node); child; child = OF_peer(child)) {
126 volatile int *gmac_gbclock_en = (void *)0xf8000020;
127 char compat[32];
128
129 memset(compat, 0, sizeof(compat));
130 OF_getprop(child, "compatible", compat, sizeof(compat));
131 if (strcmp(compat, "gmac") == 0)
132 *gmac_gbclock_en |= 0x02;
133 }
134
135 memset(&pba, 0, sizeof(pba));
136 pba.pba_busname = "pci";
137 pba.pba_memt = pc->memt;
138 pba.pba_iot = pc->iot;
139 pba.pba_dmat = &pci_bus_dma_tag;
140 pba.pba_dmat64 = NULL;
141 pba.pba_bus = pc->bus;
142 pba.pba_bridgetag = NULL;
143 pba.pba_pc = pc;
144 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
145
146 config_found(self, &pba, uninorth_print);
147 }
148
149 int
150 uninorth_print(aux, pnp)
151 void *aux;
152 const char *pnp;
153 {
154 struct pcibus_attach_args *pa = aux;
155
156 if (pnp)
157 aprint_normal("%s at %s", pa->pba_busname, pnp);
158 aprint_normal(" bus %d", pa->pba_bus);
159 return UNCONF;
160 }
161
162 pcireg_t
163 uninorth_conf_read(pc, tag, reg)
164 pci_chipset_tag_t pc;
165 pcitag_t tag;
166 int reg;
167 {
168 int32_t *daddr = pc->data;
169 pcireg_t data;
170 int bus, dev, func, s;
171 u_int32_t x;
172
173 /* UniNorth seems to have a 64bit data port */
174 if (reg & 0x04)
175 daddr++;
176
177 pci_decompose_tag(pc, tag, &bus, &dev, &func);
178
179 /*
180 * bandit's minimum device number of the first bus is 11.
181 * So we behave as if there is no device when dev < 11.
182 */
183 if (func > 7)
184 panic("pci_conf_read: func > 7");
185
186 if (bus == pc->bus) {
187 if (dev < 11)
188 return 0xffffffff;
189 x = (1 << dev) | (func << 8) | reg;
190 } else
191 x = tag | reg | 1;
192
193 s = splhigh();
194
195 out32rb(pc->addr, x);
196 in32rb(pc->addr);
197 data = 0xffffffff;
198 if (!badaddr(daddr, 4))
199 data = in32rb(daddr);
200 out32rb(pc->addr, 0);
201 in32rb(pc->addr);
202 splx(s);
203
204 return data;
205 }
206
207 void
208 uninorth_conf_write(pc, tag, reg, data)
209 pci_chipset_tag_t pc;
210 pcitag_t tag;
211 int reg;
212 pcireg_t data;
213 {
214 int32_t *daddr = pc->data;
215 int bus, dev, func, s;
216 u_int32_t x;
217
218 /* UniNorth seems to have a 64bit data port */
219 if (reg & 0x04)
220 daddr++;
221
222 pci_decompose_tag(pc, tag, &bus, &dev, &func);
223
224 if (func > 7)
225 panic("pci_conf_write: func > 7");
226
227 if (bus == pc->bus) {
228 if (dev < 11)
229 panic("pci_conf_write: dev < 11");
230 x = (1 << dev) | (func << 8) | reg;
231 } else
232 x = tag | reg | 1;
233
234 s = splhigh();
235
236 out32rb(pc->addr, x);
237 in32rb(pc->addr);
238 out32rb(daddr, data);
239 out32rb(pc->addr, 0);
240 in32rb(pc->addr);
241
242 splx(s);
243 }
244