ifpga.c revision 1.20 1 /* $NetBSD: ifpga.c,v 1.20 2005/08/26 13:19:35 drochner Exp $ */
2
3 /*
4 * Copyright (c) 2001 ARM Ltd
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the company may not be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Integrator FPGA core logic support.
34 *
35 * The integrator board supports the core logic in an FPGA which is loaded
36 * at POR with a custom design. This code supports the default logic as the
37 * board is shipped.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ifpga.c,v 1.20 2005/08/26 13:19:35 drochner Exp $");
42
43 #include <sys/types.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 #include <sys/extent.h>
47 #include <sys/malloc.h>
48 #include <sys/null.h>
49
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pciconf.h>
52
53 #include <machine/intr.h>
54
55 #include <arm/cpufunc.h>
56
57 #include "opt_pci.h"
58 #include "pci.h"
59
60 #include <evbarm/ifpga/ifpgamem.h>
61 #include <evbarm/ifpga/ifpgavar.h>
62 #include <evbarm/ifpga/ifpgareg.h>
63 #include <evbarm/ifpga/ifpga_pcivar.h>
64 #include <evbarm/dev/v360reg.h>
65
66 #include <evbarm/integrator/int_bus_dma.h>
67 #include "locators.h"
68
69 /* Prototypes */
70 static int ifpga_match (struct device *, struct cfdata *, void *);
71 static void ifpga_attach (struct device *, struct device *, void *);
72 static int ifpga_print (void *, const char *);
73
74 /* Drive and attach structures */
75 CFATTACH_DECL(ifpga, sizeof(struct ifpga_softc),
76 ifpga_match, ifpga_attach, NULL, NULL);
77
78 int ifpga_found;
79
80 /* Default UART clock speed (we should make this a boot option). */
81 int ifpga_uart_clk = IFPGA_UART_CLK;
82
83 #if NPCI > 0
84 /* PCI handles */
85 extern struct arm32_pci_chipset ifpga_pci_chipset;
86 extern struct arm32_bus_dma_tag ifpga_pci_bus_dma_tag;
87
88 static struct bus_space ifpga_pci_io_tag;
89 static struct bus_space ifpga_pci_mem_tag;
90 #endif /* NPCI > 0 */
91
92 static struct bus_space ifpga_bs_tag;
93
94 struct ifpga_softc *ifpga_sc;
95 /*
96 * Print the configuration information for children
97 */
98
99 static int
100 ifpga_print(void *aux, const char *pnp)
101 {
102 struct ifpga_attach_args *ifa = aux;
103
104 if (ifa->ifa_addr != -1)
105 aprint_normal(" addr 0x%lx", (unsigned long)ifa->ifa_addr);
106 if (ifa->ifa_irq != -1)
107 aprint_normal(" irq %d", ifa->ifa_irq);
108
109 return UNCONF;
110 }
111
112 static int
113 ifpga_search(struct device *parent, struct cfdata *cf,
114 const int *ldesc, void *aux)
115 {
116 struct ifpga_softc *sc = (struct ifpga_softc *)parent;
117 struct ifpga_attach_args ifa;
118 int tryagain;
119
120 do {
121 ifa.ifa_iot = sc->sc_iot;
122 ifa.ifa_addr = cf->cf_iobase;
123 ifa.ifa_irq = cf->cf_irq;
124 ifa.ifa_sc_ioh = sc->sc_sc_ioh;
125
126 tryagain = 0;
127 if (config_match(parent, cf, &ifa) > 0) {
128 config_attach(parent, cf, &ifa, ifpga_print);
129 tryagain = (cf->cf_fstate == FSTATE_STAR);
130 }
131 } while (tryagain);
132
133 return 0;
134 }
135
136 static int
137 ifpga_match(struct device *parent, struct cfdata *cf, void *aux)
138 {
139 #if 0
140 struct mainbus_attach_args *ma = aux;
141
142 /* Make sure that we're looking for the IFPGA. */
143 if (strcmp(ma->ma_name, ifpga_md.md_name))
144 return 0;
145 #endif
146
147 /* We can only have one instance of the IFPGA. */
148 if (ifpga_found)
149 return 0;
150
151 return 1;
152 }
153
154 static void
155 ifpga_attach(struct device *parent, struct device *self, void *aux)
156 {
157 struct ifpga_softc *sc = (struct ifpga_softc *)self;
158 u_int id, sysclk;
159 #if defined(PCI_NETBSD_CONFIGURE) && NPCI > 0
160 struct extent *ioext, *memext, *pmemext;
161 struct ifpga_pci_softc *pci_sc;
162 struct pcibus_attach_args pci_pba;
163 #endif
164
165 ifpga_found = 1;
166
167 /* We want a memory-mapped bus space, since the I/O space is sparse. */
168 ifpga_create_mem_bs_tag(&ifpga_bs_tag, (void *)IFPGA_IO_BASE);
169
170 #if NPCI > 0
171 /* But the PCI config space is quite large, so we have a linear region
172 for that pre-allocated. */
173
174 ifpga_create_io_bs_tag(&ifpga_pci_io_tag, (void *)IFPGA_PCI_IO_VBASE);
175 ifpga_create_mem_bs_tag(&ifpga_pci_mem_tag, (void *)0);
176 #endif
177
178 sc->sc_iot = &ifpga_bs_tag;
179
180 ifpga_sc = sc;
181
182 /* Now map in the IFPGA motherboard registers. */
183 if (bus_space_map(sc->sc_iot, IFPGA_IO_SC_BASE, IFPGA_IO_SC_SIZE, 0,
184 &sc->sc_sc_ioh))
185 panic("%s: Cannot map system controller registers",
186 self->dv_xname);
187
188 id = bus_space_read_4(sc->sc_iot, sc->sc_sc_ioh, IFPGA_SC_ID);
189
190 printf(": Build %d, ", (id & IFPGA_SC_ID_BUILD_MASK) >>
191 IFPGA_SC_ID_BUILD_SHIFT);
192 switch (id & IFPGA_SC_ID_REV_MASK)
193 {
194 case IFPGA_SC_ID_REV_A:
195 printf("Rev A, ");
196 break;
197 case IFPGA_SC_ID_REV_B:
198 printf("Rev B, ");
199 break;
200 }
201
202 printf("Manufacturer ");
203 switch (id & IFPGA_SC_ID_MAN_MASK)
204 {
205 case IFPGA_SC_ID_MAN_ARM:
206 printf("ARM Ltd,");
207 break;
208 default:
209 printf("Unknown,");
210 break;
211 }
212
213 switch (id & IFPGA_SC_ID_ARCH_MASK)
214 {
215 case IFPGA_SC_ID_ARCH_ASBLE:
216 printf(" ASB, Little-endian,");
217 break;
218 case IFPGA_SC_ID_ARCH_AHBLE:
219 printf(" AHB, Little-endian,");
220 break;
221 default:
222 panic(" Unsupported bus");
223 }
224
225 printf("\n%s: FPGA ", self->dv_xname);
226
227 switch (id & IFPGA_SC_ID_FPGA_MASK)
228 {
229 case IFPGA_SC_ID_FPGA_XC4062:
230 printf("XC4062");
231 break;
232 case IFPGA_SC_ID_FPGA_XC4085:
233 printf("XC4085");
234 break;
235 default:
236 printf("unknown");
237 break;
238 }
239
240 sysclk = bus_space_read_1(sc->sc_iot, sc->sc_sc_ioh, IFPGA_SC_OSC);
241 sysclk &= IFPGA_SC_OSC_S_VDW;
242 sysclk += 8;
243
244 printf(", SYSCLK %d.%02dMHz", sysclk >> 2, (sysclk & 3) * 25);
245
246 /* Map the Interrupt controller */
247 if (bus_space_map(sc->sc_iot, IFPGA_IO_IRQ_BASE, IFPGA_IO_IRQ_SIZE,
248 BUS_SPACE_MAP_LINEAR, &sc->sc_irq_ioh))
249 panic("%s: Cannot map irq controller registers",
250 self->dv_xname);
251
252 /* We can write to the IRQ/FIQ controller now. */
253 ifpga_intr_postinit();
254
255 /* Map the core module */
256 if (bus_space_map(sc->sc_iot, IFPGA_IO_CM_BASE, IFPGA_IO_CM_SIZE, 0,
257 &sc->sc_cm_ioh))
258 panic("%s: Cannot map core module registers", self->dv_xname);
259
260 /* Map the timers */
261 if (bus_space_map(sc->sc_iot, IFPGA_IO_TMR_BASE, IFPGA_IO_TMR_SIZE, 0,
262 &sc->sc_tmr_ioh))
263 panic("%s: Cannot map timer registers", self->dv_xname);
264
265 printf("\n");
266
267 #if NPCI > 0
268 pci_sc = malloc(sizeof(struct ifpga_pci_softc), M_DEVBUF, M_WAITOK);
269 pci_sc->sc_iot = &ifpga_pci_io_tag;
270 pci_sc->sc_memt = &ifpga_pci_mem_tag;
271
272 if (bus_space_map(pci_sc->sc_iot, 0, IFPGA_PCI_IO_VSIZE, 0,
273 &pci_sc->sc_io_ioh)
274 || bus_space_map(pci_sc->sc_iot,
275 IFPGA_PCI_CONF_VBASE - IFPGA_PCI_IO_VBASE, IFPGA_PCI_CONF_VSIZE, 0,
276 &pci_sc->sc_conf_ioh)
277 || bus_space_map(pci_sc->sc_memt, IFPGA_V360_REG_BASE,
278 IFPGA_V360_REG_SIZE, 0, &pci_sc->sc_reg_ioh))
279 panic("%s: Cannot map pci memory", self->dv_xname);
280
281 {
282 pcireg_t id_reg, class_reg;
283 char buf[1000];
284
285 id_reg = bus_space_read_4(pci_sc->sc_memt, pci_sc->sc_reg_ioh,
286 V360_PCI_VENDOR);
287 class_reg = bus_space_read_4(pci_sc->sc_memt,
288 pci_sc->sc_reg_ioh, V360_PCI_CC_REV);
289
290 pci_devinfo(id_reg, class_reg, 1, buf, sizeof(buf));
291 printf("%s: %s\n", self->dv_xname, buf);
292 }
293
294 #if defined(PCI_NETBSD_CONFIGURE)
295 ioext = extent_create("pciio", 0x00000000,
296 0x00000000 + IFPGA_PCI_IO_VSIZE, M_DEVBUF, NULL, 0, EX_NOWAIT);
297 memext = extent_create("pcimem", IFPGA_PCI_APP0_BASE,
298 IFPGA_PCI_APP0_BASE + IFPGA_PCI_APP0_SIZE,
299 M_DEVBUF, NULL, 0, EX_NOWAIT);
300 pmemext = extent_create("pcipmem", IFPGA_PCI_APP1_BASE,
301 IFPGA_PCI_APP1_BASE + IFPGA_PCI_APP1_SIZE,
302 M_DEVBUF, NULL, 0, EX_NOWAIT);
303 ifpga_pci_chipset.pc_conf_v = (void *)pci_sc;
304 pci_configure_bus(&ifpga_pci_chipset, ioext, memext, pmemext, 0,
305 arm_dcache_align);
306 extent_destroy(pmemext);
307 extent_destroy(memext);
308 extent_destroy(ioext);
309
310 printf("pci_configure_bus done\n");
311 #endif /* PCI_NETBSD_CONFIGURE */
312 #endif /* NPCI > 0 */
313
314 /* Finally, search for children. */
315 config_search_ia(ifpga_search, self, "ifpga", NULL);
316
317 #if NPCI > 0
318 integrator_pci_dma_init(&ifpga_pci_bus_dma_tag);
319
320 pci_pba.pba_pc = &ifpga_pci_chipset;
321 pci_pba.pba_iot = &ifpga_pci_io_tag;
322 pci_pba.pba_memt = &ifpga_pci_mem_tag;
323 pci_pba.pba_dmat = &ifpga_pci_bus_dma_tag;
324 pci_pba.pba_dmat64 = NULL;
325 pci_pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
326 pci_pba.pba_bus = 0;
327 pci_pba.pba_bridgetag = NULL;
328
329 config_found_ia(self, "pcibus", &pci_pba, pcibusprint);
330 #endif
331 }
332
333 void
334 ifpga_reset(void)
335 {
336 bus_space_write_1(ifpga_sc->sc_iot, ifpga_sc->sc_sc_ioh,
337 IFPGA_SC_CTRLS, IFPGA_SC_CTRL_SOFTRESET);
338 }
339