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