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