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