xref: /netbsd-src/sys/arch/macppc/pci/u3.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* $NetBSD: u3.c,v 1.7 2013/04/30 10:16:25 macallan Exp $ */
2 
3 /*
4  * Copyright 2006 Kyma Systems LLC.
5  * All rights reserved.
6  *
7  * Written by Sanjay Lal <sanjayl@kymasys.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Kyma Systems LLC.
21  * 4. The name of Kyma Systems LLC may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY KYMA SYSTEMS LLC ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL KYMA SYSTEMS LLC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/systm.h>
43 
44 #include <dev/pci/pcivar.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_pci.h>
47 
48 #include <machine/autoconf.h>
49 #include <machine/pio.h>
50 
51 struct ibmcpc_softc
52 {
53 	device_t sc_dev;
54 	struct genppc_pci_chipset sc_pc[8];
55 	struct powerpc_bus_space sc_iot;
56 	struct powerpc_bus_space sc_memt;
57 };
58 
59 static void ibmcpc_attach(device_t, device_t, void *);
60 static int ibmcpc_match(device_t, cfdata_t, void *);
61 
62 static pcireg_t ibmcpc_conf_read(void *, pcitag_t, int);
63 static void ibmcpc_conf_write(void *, pcitag_t, int, pcireg_t);
64 
65 CFATTACH_DECL_NEW(ibmcpc, sizeof(struct ibmcpc_softc),
66               ibmcpc_match, ibmcpc_attach, NULL, NULL);
67 
68 #define PCI_DEVFN(slot,func)    ((((slot) & 0x1f) << 3) | ((func) & 0x07))
69 
70 static int
71 ibmcpc_match(device_t parent, cfdata_t cf, void *aux)
72 {
73 	struct confargs *ca = aux;
74 	char compat[32];
75 
76 	if (strcmp(ca->ca_name, "ht") != 0)
77 		return 0;
78 
79 	memset(compat, 0, sizeof(compat));
80 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
81 
82 	if (strcmp(compat, "u3-ht") != 0)
83 		return 0;
84 
85 	return 1;
86 }
87 
88 static void
89 ibmcpc_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct ibmcpc_softc *sc = device_private(self);
92 	pci_chipset_tag_t pc = sc->sc_pc;
93 	struct confargs *ca = aux;
94 	struct pcibus_attach_args pba;
95 	int node = ca->ca_node, child;
96 	uint32_t reg[6], busrange[2], i;
97 	void *pc_data;
98 	char name[32];
99 
100 	aprint_normal("\n");
101 	sc->sc_dev = self;
102 
103 	/* u3 address */
104 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 24) {
105 		return;
106 	}
107 	aprint_normal("Mapping in config space @ pa 0x%08x, size: 0x%08x\n",
108 	    reg[1], reg[2]);
109 	pc_data = mapiodev(reg[1], reg[2], false);
110 
111 	for (child = OF_child(OF_finddevice("/ht")), i = 1; child;
112 	    child = OF_peer(child), i++) {
113 
114 		memset(name, 0, sizeof(name));
115 
116 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
117 			continue;
118 
119 		if (strcmp(name, "pci") != 0)
120 			continue;
121 
122 		if (OF_getprop(child, "bus-range", busrange, 8) < 8)
123 			continue;
124 
125 		memset(&sc->sc_iot, 0, sizeof(sc->sc_iot));
126 		sc->sc_iot.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
127 		    _BUS_SPACE_IO_TYPE;
128 		sc->sc_iot.pbs_base = 0x00000000;
129 		if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_IO, child,
130 		    &sc->sc_iot, "ibmcpc io") != 0)
131 			panic("Can't init ibmcpc io tag");
132 
133 		memset(&sc->sc_memt, 0, sizeof(sc->sc_memt));
134 		sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN |
135 		    _BUS_SPACE_MEM_TYPE;
136 		sc->sc_memt.pbs_base = 0x00000000;
137 		if (ofwoea_map_space(RANGE_TYPE_PCI, RANGE_MEM, child,
138 		    &sc->sc_memt, "ibmcpc mem") != 0)
139 			panic("Can't init ibmcpc mem tag");
140 
141 		macppc_pci_get_chipset_tag(pc);
142 		pc->pc_node = child;
143 		pc->pc_bus = busrange[0];
144 		pc->pc_addr = 0x0;
145 		pc->pc_data = pc_data;
146 		pc->pc_conf_read = ibmcpc_conf_read;
147 		pc->pc_conf_write = ibmcpc_conf_write;
148 		pc->pc_memt = &sc->sc_memt;
149 		pc->pc_iot = &sc->sc_iot;
150 
151 		memset(&pba, 0, sizeof(pba));
152 		pba.pba_memt = pc->pc_memt;
153 		pba.pba_iot = pc->pc_iot;
154 		pba.pba_dmat = &pci_bus_dma_tag;
155 		pba.pba_dmat64 = NULL;
156 		pba.pba_bridgetag = NULL;
157 		pba.pba_pc = pc;
158 		pba.pba_bus = pc->pc_bus;
159 		pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
160 		config_found_ia(self, "pcibus", &pba, pcibusprint);
161 
162 		pc++;
163 	}
164 }
165 
166 static pcireg_t
167 ibmcpc_conf_read(void *cookie, pcitag_t tag, int reg)
168 {
169 	pci_chipset_tag_t pc = cookie;
170 	u_int32_t daddr = (u_int32_t) pc->pc_data;
171 	pcireg_t data;
172 	u_int32_t bus, dev, func, x, devfn;
173 
174 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
175 
176 	devfn = PCI_DEVFN(dev, func);
177 
178 	if (bus == 0) {
179 
180 		if (devfn == 0x0) {
181 
182 			data = 0xffffffff;
183 			goto done;
184 		}
185 
186 		x = daddr + ((devfn << 8) | reg);
187 	} else
188 		x = daddr + ((devfn << 8) | reg) + (bus << 16) + 0x01000000UL;
189 
190 	data = in32rb(x);
191 
192 done:
193 	return data;
194 }
195 
196 static void
197 ibmcpc_conf_write(void *cookie, pcitag_t tag, int reg, pcireg_t data)
198 {
199 	pci_chipset_tag_t pc = cookie;
200 	int32_t *daddr = pc->pc_data;
201 	u_int32_t bus, dev, func;
202 	u_int32_t x, devfn;
203 
204 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
205 
206 	devfn = PCI_DEVFN(dev, func);
207 
208 	if (bus == 0) {
209 
210 		if (devfn == 0x0) {
211 			goto done;
212 		}
213 		x = (u_int32_t) daddr + ((devfn << 8) | reg);
214 	} else
215 		x = (u_int32_t) daddr + ((devfn << 8) | reg) + (bus << 16) +
216 		    0x01000000UL;
217 
218 	out32rb(x, data);
219 
220 done:
221 	return;
222 }
223