]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/xscale/ixp425/ixp425.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / arm / xscale / ixp425 / ixp425.c
1 /*      $NetBSD: ixp425.c,v 1.10 2005/12/11 12:16:51 christos Exp $ */
2
3 /*
4  * Copyright (c) 2003
5  *      Ichiro FUKUHARA <ichiro@ichiro.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Ichiro FUKUHARA.
19  * 4. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #define _ARM32_BUS_DMA_PRIVATE
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/rman.h>
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <arm/xscale/ixp425/ixp425reg.h>
53 #include <arm/xscale/ixp425/ixp425var.h>
54 #include <arm/xscale/ixp425/ixp425_intr.h>
55
56 #include <dev/pci/pcireg.h>
57
58 volatile uint32_t intr_enabled;
59 uint32_t intr_steer = 0;
60
61 struct  ixp425_softc *ixp425_softc = NULL;
62
63 static int      ixp425_probe(device_t);
64 static void     ixp425_identify(driver_t *, device_t);
65 static int      ixp425_attach(device_t);
66
67 static struct {
68         uint32_t        hwbase;
69         uint32_t        size;
70         uint32_t        vbase;
71 } hwvtrans[] = {
72         { IXP425_IO_HWBASE,     IXP425_IO_SIZE,         IXP425_IO_VBASE },
73         { IXP425_EXP_HWBASE,    IXP425_EXP_SIZE,        IXP425_EXP_VBASE },
74         { IXP425_PCI_HWBASE,    IXP425_PCI_SIZE,        IXP425_PCI_VBASE },
75         { IXP425_PCI_MEM_HWBASE,IXP425_PCI_MEM_SIZE,    IXP425_PCI_MEM_VBASE },
76 #if 0
77         { IXP425_PCI_IO_HWBASE, IXP425_PCI_IO_SIZE,     IXP425_PCI_IO_VBASE },
78 #endif
79         { IXP425_MCU_HWBASE,    IXP425_MCU_SIZE,        IXP425_MCU_VBASE },
80         { IXP425_QMGR_HWBASE,   IXP425_QMGR_SIZE,       IXP425_QMGR_VBASE },
81         { IXP425_NPE_A_HWBASE,  IXP425_NPE_A_SIZE,      IXP425_NPE_A_VBASE },
82         { IXP425_NPE_B_HWBASE,  IXP425_NPE_B_SIZE,      IXP425_NPE_B_VBASE },
83         { IXP425_NPE_C_HWBASE,  IXP425_NPE_C_SIZE,      IXP425_NPE_C_VBASE },
84         { IXP425_MAC_A_HWBASE,  IXP425_MAC_A_SIZE,      IXP425_MAC_A_VBASE },
85         { IXP425_MAC_B_HWBASE,  IXP425_MAC_B_SIZE,      IXP425_MAC_B_VBASE },
86         /* Gateworks Avila IDE/CF is mapped here */
87         { IXP425_EXP_BUS_CS1_HWBASE, IXP425_EXP_BUS_SIZE,
88           IXP425_EXP_BUS_CS1_VBASE },
89         { IXP425_EXP_BUS_CS2_HWBASE, IXP425_EXP_BUS_SIZE,
90           IXP425_EXP_BUS_CS2_VBASE },
91         /* ADI Pronghorn Metro IDE/CF is mapped here */
92         { IXP425_EXP_BUS_CS3_HWBASE, IXP425_EXP_BUS_SIZE,
93           IXP425_EXP_BUS_CS3_VBASE },
94         { IXP425_EXP_BUS_CS4_HWBASE, IXP425_EXP_BUS_SIZE,
95           IXP425_EXP_BUS_CS4_VBASE },
96 };
97
98 int
99 getvbase(uint32_t hwbase, uint32_t size, uint32_t *vbase)
100 {
101         int i;
102
103         for (i = 0; i < sizeof hwvtrans / sizeof *hwvtrans; i++) {
104                 if (hwbase >= hwvtrans[i].hwbase &&
105                     hwbase + size <= hwvtrans[i].hwbase + hwvtrans[i].size) {
106                         *vbase = hwbase - hwvtrans[i].hwbase + hwvtrans[i].vbase;
107                         return (0);
108                 }
109         }
110
111         return (ENOENT);
112 }
113
114 struct arm32_dma_range *
115 bus_dma_get_range(void)
116 {
117         return (NULL);
118 }
119
120 int
121 bus_dma_get_range_nb(void)
122 {
123         return (0);
124 }
125
126 static __inline u_int32_t
127 ixp425_irq2gpio_bit(int irq)
128 {
129
130         static const uint8_t int2gpio[32] __attribute__ ((aligned(32))) = {
131                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,     /* INT#0 -> INT#5 */
132                 0x00, 0x01,                             /* GPIO#0 -> GPIO#1 */
133                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,     /* INT#8 -> INT#13 */
134                 0xff, 0xff, 0xff, 0xff, 0xff,           /* INT#14 -> INT#18 */
135                 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,     /* GPIO#2 -> GPIO#7 */
136                 0x08, 0x09, 0x0a, 0x0b, 0x0c,           /* GPIO#8 -> GPIO#12 */
137                 0xff, 0xff                              /* INT#30 -> INT#31 */
138         };
139
140         return (1U << int2gpio[irq]);
141 }
142
143 void
144 arm_mask_irq(uintptr_t nb)
145 {
146         int i;
147         
148         i = disable_interrupts(I32_bit);
149         intr_enabled &= ~(1 << nb);
150         ixp425_set_intrmask();
151         restore_interrupts(i);
152         /*XXX; If it's a GPIO interrupt, ACK it know. Can it be a problem ?*/
153         if ((1 << nb) & IXP425_INT_GPIOMASK)
154                 IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
155                     ixp425_irq2gpio_bit(nb);
156 }
157
158 void
159 arm_unmask_irq(uintptr_t nb)
160 {
161         int i;
162         
163         i = disable_interrupts(I32_bit);
164         intr_enabled |= (1 << nb);
165         ixp425_set_intrmask();
166         restore_interrupts(i);
167 }
168
169 static __inline uint32_t
170 ixp425_irq_read(void)
171 {
172         return IXPREG(IXP425_INT_STATUS) & intr_enabled;
173 }
174
175 int
176 arm_get_next_irq(void)
177 {
178         int irq;
179
180         if ((irq = ixp425_irq_read()))
181                 return (ffs(irq) - 1);
182         return (-1);
183 }
184
185 void
186 cpu_reset(void)
187 {
188
189         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
190             IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
191         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
192             IXP425_OST_WDOG, 0);
193         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
194             IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA |
195             OST_WDOG_ENAB_CNT_ENA);
196         printf("Reset failed!\n");
197         for(;;);
198 }
199
200 static void
201 ixp425_identify(driver_t *driver, device_t parent)
202 {
203         BUS_ADD_CHILD(parent, 0, "ixp", 0);
204 }
205
206 static int
207 ixp425_probe(device_t dev)
208 {
209         device_set_desc(dev, "Intel IXP425");
210         return (0);
211 }
212
213 static int
214 ixp425_attach(device_t dev)
215 {
216         struct ixp425_softc *sc;
217
218         sc = device_get_softc(dev);
219         sc->sc_iot = &ixp425_bs_tag;
220         KASSERT(ixp425_softc == NULL, ("ixp425_attach called twice?"));
221         ixp425_softc = sc;
222
223         intr_enabled = 0;
224         ixp425_set_intrmask();
225         ixp425_set_intrsteer();
226
227         if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
228             BUS_SPACE_MAXADDR, NULL, NULL,  0xffffffff, 0xff, 0xffffffff, 0, 
229             NULL, NULL, &sc->sc_dmat))
230                 panic("couldn't create the IXP425 dma tag !");
231
232         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
233         sc->sc_irq_rman.rm_descr = "IXP425 IRQs";
234         if (rman_init(&sc->sc_irq_rman) != 0 ||
235             rman_manage_region(&sc->sc_irq_rman, 0, 31) != 0)
236                 panic("ixp425_attach: failed to set up IRQ rman");
237
238         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
239         sc->sc_mem_rman.rm_descr = "IXP425 Memory";
240         if (rman_init(&sc->sc_mem_rman) != 0 ||
241             rman_manage_region(&sc->sc_mem_rman, 0, ~0) != 0)
242                 panic("ixp425_attach: failed to set up memory rman");
243
244         BUS_ADD_CHILD(dev, 0, "pcib", 0);
245         BUS_ADD_CHILD(dev, 0, "ixpclk", 0);
246         BUS_ADD_CHILD(dev, 0, "ixpiic", 0);
247         /* XXX move to hints? */
248         BUS_ADD_CHILD(dev, 0, "ixpwdog", 0);
249
250         /* attach wired devices via hints */
251         bus_enumerate_hinted_children(dev);
252
253         if (bus_space_map(sc->sc_iot, IXP425_GPIO_HWBASE, IXP425_GPIO_SIZE,
254             0, &sc->sc_gpio_ioh))
255                 panic("ixp425_attach: unable to map GPIO registers");
256         if (bus_space_map(sc->sc_iot, IXP425_EXP_HWBASE, IXP425_EXP_SIZE,
257             0, &sc->sc_exp_ioh))
258                 panic("ixp425_attach: unable to map Expansion Bus registers");
259
260         bus_generic_probe(dev);
261         bus_generic_attach(dev);
262
263         return (0);
264 }
265
266 static void
267 ixp425_hinted_child(device_t bus, const char *dname, int dunit)
268 {
269         device_t child;
270         struct ixp425_ivar *ivar;
271
272         child = BUS_ADD_CHILD(bus, 0, dname, dunit);
273         ivar = IXP425_IVAR(child);
274         resource_int_value(dname, dunit, "addr", &ivar->addr);
275         resource_int_value(dname, dunit, "irq", &ivar->irq);
276 }
277
278 static device_t
279 ixp425_add_child(device_t dev, int order, const char *name, int unit)
280 {
281         device_t child;
282         struct ixp425_ivar *ivar;
283
284         child = device_add_child_ordered(dev, order, name, unit);
285         if (child == NULL)
286                 return NULL;
287         ivar = malloc(sizeof(struct ixp425_ivar), M_DEVBUF, M_NOWAIT);
288         if (ivar == NULL) {
289                 device_delete_child(dev, child);
290                 return NULL;
291         }
292         ivar->addr = 0;
293         ivar->irq = -1;
294         device_set_ivars(child, ivar);
295         return child;
296 }
297
298 static int
299 ixp425_read_ivar(device_t bus, device_t child, int which, u_char *result)
300 {
301         struct ixp425_ivar *ivar = IXP425_IVAR(child);
302
303         switch (which) {
304         case IXP425_IVAR_ADDR:
305                 if (ivar->addr != 0) {
306                         *(uint32_t *)result = ivar->addr;
307                         return 0;
308                 }
309                 break;
310         case IXP425_IVAR_IRQ:
311                 if (ivar->irq != -1) {
312                         *(int *)result = ivar->irq;
313                         return 0;
314                 }
315                 break;
316         }
317         return EINVAL;
318 }
319
320 static struct resource *
321 ixp425_alloc_resource(device_t dev, device_t child, int type, int *rid,
322     u_long start, u_long end, u_long count, u_int flags)
323 {
324         struct ixp425_softc *sc = device_get_softc(dev);
325         struct rman *rmanp;
326         struct resource *rv;
327         uint32_t vbase, addr;
328         int irq;
329
330         switch (type) {
331         case SYS_RES_IRQ:
332                 rmanp = &sc->sc_irq_rman;
333                 /* override per hints */
334                 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_IRQ, &irq) == 0)
335                         start = end = irq;
336                 rv = rman_reserve_resource(rmanp, start, end, count,
337                         flags, child);
338                 if (rv != NULL)
339                         rman_set_rid(rv, *rid);
340                 break;
341
342         case SYS_RES_MEMORY:
343                 rmanp = &sc->sc_mem_rman;
344                 /* override per hints */
345                 if (BUS_READ_IVAR(dev, child, IXP425_IVAR_ADDR, &addr) == 0) {
346                         start = addr;
347                         end = start + 0x1000;   /* XXX */
348                 }
349                 if (getvbase(start, end - start, &vbase))
350                         return NULL;
351                 rv = rman_reserve_resource(rmanp, start, end, count,
352                         flags, child);
353                 if (rv != NULL) {
354                         rman_set_rid(rv, *rid);
355                         if (strcmp(device_get_name(child), "uart") == 0)
356                                 rman_set_bustag(rv, &ixp425_a4x_bs_tag);
357                         else
358                                 rman_set_bustag(rv, sc->sc_iot);
359                         rman_set_bushandle(rv, vbase);
360                 }
361                 break;
362         default:
363                 rv = NULL;
364                 break;
365         }
366         return rv;
367 }
368
369 static int
370 ixp425_setup_intr(device_t dev, device_t child,
371     struct resource *ires, int flags, driver_filter_t *filt, 
372     driver_intr_t *intr, void *arg, void **cookiep)    
373 {
374         uint32_t mask;
375         int i;
376
377         BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, filt, intr,
378              arg, cookiep);
379
380         mask = 0;
381         for (i = rman_get_start(ires); i <= rman_get_end(ires); i++)
382                 mask |= 1 << i;
383         intr_enabled |= mask;
384         ixp425_set_intrmask();
385
386         return (0);
387 }
388
389 static int
390 ixp425_teardown_intr(device_t dev, device_t child, struct resource *res,
391     void *cookie)
392 {
393         uint32_t mask;
394         int i;
395
396         mask = 0;
397         for (i = rman_get_start(res); i <= rman_get_end(res); i++)
398                 mask |= 1 << i;
399         intr_enabled &= ~mask;
400         ixp425_set_intrmask();
401
402         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
403 }
404
405 static device_method_t ixp425_methods[] = {
406         /* Device interface */
407         DEVMETHOD(device_probe, ixp425_probe),
408         DEVMETHOD(device_attach, ixp425_attach),
409         DEVMETHOD(device_identify, ixp425_identify),
410
411         /* Bus interface */
412         DEVMETHOD(bus_add_child, ixp425_add_child),
413         DEVMETHOD(bus_hinted_child, ixp425_hinted_child),
414         DEVMETHOD(bus_read_ivar, ixp425_read_ivar),
415
416         DEVMETHOD(bus_alloc_resource, ixp425_alloc_resource),
417         DEVMETHOD(bus_setup_intr, ixp425_setup_intr),
418         DEVMETHOD(bus_teardown_intr, ixp425_teardown_intr),
419
420         {0, 0},
421 };
422
423 static driver_t ixp425_driver = {
424         "ixp",
425         ixp425_methods,
426         sizeof(struct ixp425_softc),
427 };
428 static devclass_t ixp425_devclass;
429
430 DRIVER_MODULE(ixp, nexus, ixp425_driver, ixp425_devclass, 0, 0);