]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/xscale/ixp425/ixp425.c
Fix small altq related copy and paste error.
[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 };
90
91 static int
92 getvbase(uint32_t hwbase, uint32_t size, uint32_t *vbase)
93 {
94         int i;
95
96         for (i = 0; i < sizeof hwvtrans / sizeof *hwvtrans; i++) {
97                 if (hwbase >= hwvtrans[i].hwbase &&
98                     hwbase + size <= hwvtrans[i].hwbase + hwvtrans[i].size) {
99                         *vbase = hwbase - hwvtrans[i].hwbase + hwvtrans[i].vbase;
100                         return (0);
101                 }
102         }
103
104         return (ENOENT);
105 }
106
107 struct arm32_dma_range *
108 bus_dma_get_range(void)
109 {
110         return (NULL);
111 }
112
113 int
114 bus_dma_get_range_nb(void)
115 {
116         return (0);
117 }
118
119 static __inline u_int32_t
120 ixp425_irq2gpio_bit(int irq)
121 {
122
123         static const uint8_t int2gpio[32] __attribute__ ((aligned(32))) = {
124                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,     /* INT#0 -> INT#5 */
125                 0x00, 0x01,                             /* GPIO#0 -> GPIO#1 */
126                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,     /* INT#8 -> INT#13 */
127                 0xff, 0xff, 0xff, 0xff, 0xff,           /* INT#14 -> INT#18 */
128                 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,     /* GPIO#2 -> GPIO#7 */
129                 0x08, 0x09, 0x0a, 0x0b, 0x0c,           /* GPIO#8 -> GPIO#12 */
130                 0xff, 0xff                              /* INT#30 -> INT#31 */
131         };
132
133         return (1U << int2gpio[irq]);
134 }
135
136 void
137 arm_mask_irq(uintptr_t nb)
138 {
139         intr_enabled &= ~(1 << nb);
140         ixp425_set_intrmask();
141         /*XXX; If it's a GPIO interrupt, ACK it know. Can it be a problem ?*/
142         if ((1 << nb) & IXP425_INT_GPIOMASK)
143                 IXPREG(IXP425_GPIO_VBASE + IXP425_GPIO_GPISR) =
144                     ixp425_irq2gpio_bit(nb);
145
146                 
147 }
148
149 void
150 arm_unmask_irq(uintptr_t nb)
151 {
152
153         intr_enabled |= (1 << nb);
154         ixp425_set_intrmask();
155 }
156
157 static __inline uint32_t
158 ixp425_irq_read(void)
159 {
160         return IXPREG(IXP425_INT_STATUS) & intr_enabled;
161 }
162
163 int
164 arm_get_next_irq(void)
165 {
166         int irq;
167
168         if ((irq = ixp425_irq_read()))
169                 return (ffs(irq) - 1);
170         return (-1);
171 }
172
173 void
174 cpu_reset(void)
175 {
176
177         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
178             IXP425_OST_WDOG_KEY, OST_WDOG_KEY_MAJICK);
179         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
180             IXP425_OST_WDOG, 0);
181         bus_space_write_4(&ixp425_bs_tag, IXP425_TIMER_VBASE,
182             IXP425_OST_WDOG_ENAB, OST_WDOG_ENAB_RST_ENA |
183             OST_WDOG_ENAB_CNT_ENA);
184         printf("Reset failed!\n");
185         for(;;);
186 }
187
188 static void
189 ixp425_identify(driver_t *driver, device_t parent)
190 {
191         BUS_ADD_CHILD(parent, 0, "ixp", 0);
192 }
193
194 static int
195 ixp425_probe(device_t dev)
196 {
197         device_set_desc(dev, "Intel IXP425");
198         return (0);
199 }
200
201 static int
202 ixp425_attach(device_t dev)
203 {
204         struct ixp425_softc *sc;
205
206         sc = device_get_softc(dev);
207         sc->sc_iot = &ixp425_bs_tag;
208         KASSERT(ixp425_softc == NULL, ("ixp425_attach called twice?"));
209         ixp425_softc = sc;
210
211         intr_enabled = 0;
212         ixp425_set_intrmask();
213         ixp425_set_intrsteer();
214
215         if (bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
216             BUS_SPACE_MAXADDR, NULL, NULL,  0xffffffff, 0xff, 0xffffffff, 0, 
217             NULL, NULL, &sc->sc_dmat))
218                 panic("couldn't create the IXP425 dma tag !");
219
220         sc->sc_irq_rman.rm_type = RMAN_ARRAY;
221         sc->sc_irq_rman.rm_descr = "IXP425 IRQs";
222         if (rman_init(&sc->sc_irq_rman) != 0 ||
223             rman_manage_region(&sc->sc_irq_rman, 0, 31) != 0)
224                 panic("ixp425_attach: failed to set up IRQ rman");
225
226         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
227         sc->sc_mem_rman.rm_descr = "IXP425 Memory";
228         if (rman_init(&sc->sc_mem_rman) != 0 ||
229             rman_manage_region(&sc->sc_mem_rman, 0, ~0) != 0)
230                 panic("ixp425_attach: failed to set up IRQ rman");
231
232         device_add_child(dev, "pcib", 0);
233         device_add_child(dev, "ixpclk", 0);
234         device_add_child(dev, "ixpwdog", 0);
235         device_add_child(dev, "ixpiic", 0);
236         device_add_child(dev, "uart", 0);
237         /* XXX these are optional, what if they are not configured? */
238         device_add_child(dev, "ixpqmgr", 0);
239         device_add_child(dev, "npe", 0);        /* NPE-B */
240         device_add_child(dev, "npe", 1);        /* NPE-C */
241         device_add_child(dev, "ata_avila", 0);  /* XXX */
242         device_add_child(dev, "led_avila", 0);
243
244         if (bus_space_map(sc->sc_iot, IXP425_GPIO_HWBASE, IXP425_GPIO_SIZE,
245             0, &sc->sc_gpio_ioh))
246                 panic("ixp425_attach: unable to map GPIO registers");
247         if (bus_space_map(sc->sc_iot, IXP425_EXP_HWBASE, IXP425_EXP_SIZE,
248             0, &sc->sc_exp_ioh))
249                 panic("ixp425_attach: unable to map Expansion Bus registers");
250
251         bus_generic_probe(dev);
252         bus_generic_attach(dev);
253
254         return (0);
255 }
256
257 static struct resource *
258 ixp425_alloc_resource(device_t dev, device_t child, int type, int *rid,
259     u_long start, u_long end, u_long count, u_int flags)
260 {
261         struct ixp425_softc *sc = device_get_softc(dev);
262         struct rman *rmanp;
263         struct resource *rv;
264         uint32_t vbase;
265         int isuart = (start == 0 && end == ~0); /* XXX how to do this right? */
266
267         rv = NULL;
268
269         switch (type) {
270         case SYS_RES_IRQ:
271                 rmanp = &sc->sc_irq_rman;
272                 if (isuart) {
273                         if (device_get_unit(dev) == 0) 
274                                 start = IXP425_INT_UART0;
275                         else
276                                 start = IXP425_INT_UART1;
277                         end = start;
278                 }
279                 break;
280
281         case SYS_RES_MEMORY:
282                 rmanp = &sc->sc_mem_rman;
283                 if (isuart) {
284                         if (device_get_unit(dev) == 0) 
285                                 start = IXP425_UART0_HWBASE;
286                         else
287                                 start = IXP425_UART1_HWBASE;
288                         end = start + 0x1000;
289                 }
290                 if (getvbase(start, end - start, &vbase))
291                         return (rv);
292                 break;
293
294         default:
295                 return (rv);
296         }
297
298         rv = rman_reserve_resource(rmanp, start, end, count, flags, child);
299         if (rv != NULL) {
300                 rman_set_rid(rv, *rid);
301                 if (type == SYS_RES_MEMORY) {
302                         rman_set_bustag(rv,
303                             isuart ? &ixp425_a4x_bs_tag : sc->sc_iot);
304                         rman_set_bushandle(rv, vbase);
305                 }
306         }
307
308         return (rv);
309 }
310
311 static int
312 ixp425_setup_intr(device_t dev, device_t child,
313     struct resource *ires, int flags, driver_intr_t *intr, void *arg,
314     void **cookiep)
315 {
316         uint32_t mask;
317         int i;
318
319         if (flags & INTR_TYPE_TTY) {
320                 /* XXX: wrong. */
321                 if (device_get_unit(dev) == 0)
322                         rman_set_start(ires, IXP425_INT_UART0);
323                 else
324                         rman_set_start(ires, IXP425_INT_UART1);
325                 rman_set_end(ires, rman_get_start(ires));
326         }
327         BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags, intr, arg,
328             cookiep);
329
330         mask = 0;
331         for (i = rman_get_start(ires); i <= rman_get_end(ires); i++)
332                 mask |= 1 << i;
333         intr_enabled |= mask;
334         ixp425_set_intrmask();
335
336         return (0);
337 }
338
339 static int
340 ixp425_teardown_intr(device_t dev, device_t child, struct resource *res,
341     void *cookie)
342 {
343         uint32_t mask;
344         int i;
345
346         mask = 0;
347         for (i = rman_get_start(res); i <= rman_get_end(res); i++)
348                 mask |= 1 << i;
349         intr_enabled &= ~mask;
350         ixp425_set_intrmask();
351
352         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, res, cookie));
353 }
354
355 static device_method_t ixp425_methods[] = {
356         /* Device interface */
357         DEVMETHOD(device_probe, ixp425_probe),
358         DEVMETHOD(device_attach, ixp425_attach),
359         DEVMETHOD(device_identify, ixp425_identify),
360
361         /* Bus interface */
362         DEVMETHOD(bus_alloc_resource, ixp425_alloc_resource),
363         DEVMETHOD(bus_setup_intr, ixp425_setup_intr),
364         DEVMETHOD(bus_teardown_intr, ixp425_teardown_intr),
365
366         {0, 0},
367 };
368
369 static driver_t ixp425_driver = {
370         "ixp",
371         ixp425_methods,
372         sizeof(struct ixp425_softc),
373 };
374 static devclass_t ixp425_devclass;
375
376 DRIVER_MODULE(ixp, nexus, ixp425_driver, ixp425_devclass, 0, 0);