]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/nlm/xlp_simplebus.c
MFH
[FreeBSD/FreeBSD.git] / sys / mips / nlm / xlp_simplebus.c
1 /*-
2  * Copyright (c) 2015 Broadcom Corporation
3  * (based on sys/dev/fdt/simplebus.c)
4  * Copyright (c) 2013 Nathan Whitehorn
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38
39 #include <vm/vm.h>
40 #include <vm/vm_param.h>
41 #include <vm/pmap.h>
42
43 #include <machine/bus.h>
44 #include <machine/intr_machdep.h>
45
46 #include <mips/nlm/hal/haldefs.h>
47 #include <mips/nlm/interrupt.h>
48 #include <mips/nlm/hal/iomap.h>
49 #include <mips/nlm/hal/mips-extns.h>
50 #include <mips/nlm/hal/pcibus.h>
51 #include <mips/nlm/xlp.h>
52
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56
57 #include <dev/fdt/simplebus.h>
58
59 /* flash memory region for chipselects */
60 #define GBU_MEM_BASE    0x16000000UL
61 #define GBU_MEM_LIMIT   0x17ffffffUL
62
63 /*
64  * Device registers in pci ecfg memory region for devices without regular PCI BARs
65  */
66 #define PCI_ECFG_BASE   XLP_DEFAULT_IO_BASE
67 #define PCI_ECFG_LIMIT  (XLP_DEFAULT_IO_BASE + 0x0fffffff)
68
69 /*
70  * Bus interface.
71  */
72 static int              xlp_simplebus_probe(device_t dev);
73 static struct resource *xlp_simplebus_alloc_resource(device_t, device_t, int,
74     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
75 static int              xlp_simplebus_activate_resource(device_t, device_t, int,
76     int, struct resource *);
77 static int              xlp_simplebus_setup_intr(device_t, device_t,
78     struct resource *, int, driver_filter_t *, driver_intr_t *, void *, void **);
79
80 /*
81  * ofw_bus interface
82  */
83 static int              xlp_simplebus_ofw_map_intr(device_t, device_t, phandle_t,
84     int, pcell_t *);
85
86 static devclass_t simplebus_devclass;
87 static device_method_t xlp_simplebus_methods[] = {
88         /* Device interface */
89         DEVMETHOD(device_probe,         xlp_simplebus_probe),
90
91         DEVMETHOD(bus_alloc_resource,   xlp_simplebus_alloc_resource),
92         DEVMETHOD(bus_activate_resource, xlp_simplebus_activate_resource),
93         DEVMETHOD(bus_setup_intr,       xlp_simplebus_setup_intr),
94
95         DEVMETHOD(ofw_bus_map_intr,     xlp_simplebus_ofw_map_intr),
96         DEVMETHOD_END
97 };
98
99 DEFINE_CLASS_1(simplebus, xlp_simplebus_driver, xlp_simplebus_methods,
100     sizeof(struct simplebus_softc), simplebus_driver);
101 DRIVER_MODULE(xlp_simplebus, ofwbus, xlp_simplebus_driver, simplebus_devclass,
102     0, 0);
103
104 static struct rman irq_rman, port_rman, mem_rman, pci_ecfg_rman, gbu_rman;
105
106 static void
107 xlp_simplebus_init_resources(void)
108 {
109         irq_rman.rm_start = 0;
110         irq_rman.rm_end = 255;
111         irq_rman.rm_type = RMAN_ARRAY;
112         irq_rman.rm_descr = "PCI Mapped Interrupts";
113         if (rman_init(&irq_rman)
114             || rman_manage_region(&irq_rman, 0, 255))
115                 panic("xlp_simplebus_init_resources irq_rman");
116
117         port_rman.rm_start = 0;
118         port_rman.rm_end = ~0ul;
119         port_rman.rm_type = RMAN_ARRAY;
120         port_rman.rm_descr = "I/O ports";
121         if (rman_init(&port_rman)
122             || rman_manage_region(&port_rman, PCIE_IO_BASE, PCIE_IO_LIMIT))
123                 panic("xlp_simplebus_init_resources port_rman");
124
125         mem_rman.rm_start = 0;
126         mem_rman.rm_end = ~0ul;
127         mem_rman.rm_type = RMAN_ARRAY;
128         mem_rman.rm_descr = "I/O memory";
129         if (rman_init(&mem_rman)
130             || rman_manage_region(&mem_rman, PCIE_MEM_BASE, PCIE_MEM_LIMIT))
131                 panic("xlp_simplebus_init_resources mem_rman");
132
133         pci_ecfg_rman.rm_start = 0;
134         pci_ecfg_rman.rm_end = ~0ul;
135         pci_ecfg_rman.rm_type = RMAN_ARRAY;
136         pci_ecfg_rman.rm_descr = "PCI ECFG IO";
137         if (rman_init(&pci_ecfg_rman) || rman_manage_region(&pci_ecfg_rman,
138             PCI_ECFG_BASE, PCI_ECFG_LIMIT))
139                 panic("xlp_simplebus_init_resources pci_ecfg_rman");
140
141         gbu_rman.rm_start = 0;
142         gbu_rman.rm_end = ~0ul;
143         gbu_rman.rm_type = RMAN_ARRAY;
144         gbu_rman.rm_descr = "Flash region";
145         if (rman_init(&gbu_rman)
146             || rman_manage_region(&gbu_rman, GBU_MEM_BASE, GBU_MEM_LIMIT))
147                 panic("xlp_simplebus_init_resources gbu_rman");
148 }
149
150 static int
151 xlp_simplebus_probe(device_t dev)
152 {
153
154         if (!ofw_bus_status_okay(dev))
155                 return (ENXIO);
156
157         /*
158          * FDT data puts a "simple-bus" compatible string on many things that
159          * have children but aren't really busses in our world.  Without a
160          * ranges property we will fail to attach, so just fail to probe too.
161          */
162         if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
163             ofw_bus_has_prop(dev, "ranges")) &&
164             (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
165              "soc") != 0))
166                 return (ENXIO);
167
168         xlp_simplebus_init_resources();
169         device_set_desc(dev, "XLP SoC bus");
170
171         return (BUS_PROBE_SPECIFIC);
172 }
173
174 static struct resource *
175 xlp_simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
176     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
177 {
178         struct rman                     *rm;
179         struct resource                 *rv;
180         struct resource_list_entry      *rle;
181         struct simplebus_softc          *sc;
182         struct simplebus_devinfo        *di;
183         bus_space_tag_t                 bustag;
184         int j, isdefault, passthrough, needsactivate;
185
186         passthrough = (device_get_parent(child) != bus);
187         needsactivate = flags & RF_ACTIVE;
188         sc = device_get_softc(bus);
189         di = device_get_ivars(child);
190         rle = NULL;
191         bustag = NULL;
192
193         if (!passthrough) {
194                 isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
195                 if (isdefault) {
196                         rle = resource_list_find(&di->rl, type, *rid);
197                         if (rle == NULL)
198                                 return (NULL);
199                         if (rle->res != NULL)
200                                 panic("%s: resource entry is busy", __func__);
201                         start = rle->start;
202                         count = ulmax(count, rle->count);
203                         end = ulmax(rle->end, start + count - 1);
204                 }
205                 if (type == SYS_RES_MEMORY) {
206                         /* Remap through ranges property */
207                         for (j = 0; j < sc->nranges; j++) {
208                                 if (start >= sc->ranges[j].bus && end <
209                                     sc->ranges[j].bus + sc->ranges[j].size) {
210                                         start -= sc->ranges[j].bus;
211                                         start += sc->ranges[j].host;
212                                         end -= sc->ranges[j].bus;
213                                         end += sc->ranges[j].host;
214                                         break;
215                                 }
216                         }
217                         if (j == sc->nranges && sc->nranges != 0) {
218                                 if (bootverbose)
219                                         device_printf(bus, "Could not map resource "
220                                             "%#lx-%#lx\n", start, end);
221                                 return (NULL);
222                         }
223                 }
224         }
225         switch (type) {
226         case SYS_RES_IRQ:
227                 rm = &irq_rman;
228                 break;
229         case SYS_RES_IOPORT:
230                 rm = &port_rman;
231                 bustag = rmi_bus_space;
232                 break;
233         case SYS_RES_MEMORY:
234                 if (start >= GBU_MEM_BASE && end <= GBU_MEM_LIMIT) {
235                         rm = &gbu_rman;
236                         bustag = rmi_bus_space;
237                 } else if (start >= PCI_ECFG_BASE && end <= PCI_ECFG_LIMIT) {
238                         rm = &pci_ecfg_rman;
239                         bustag = rmi_uart_bus_space;
240                 } else if (start >= PCIE_MEM_BASE && end <= PCIE_MEM_LIMIT) {
241                         rm = &mem_rman;
242                         bustag = rmi_bus_space;
243                 } else {
244                         if (bootverbose)
245                                 device_printf(bus, "Invalid MEM range"
246                                             "%#lx-%#lx\n", start, end);
247                         return (NULL);
248                 }
249                 break;
250         default:
251                 return (NULL);
252         }
253
254         rv = rman_reserve_resource(rm, start, end, count, flags, child);
255         if (rv == 0) {
256                 device_printf(bus, "%s: could not reserve resource for %s\n",
257                     __func__, device_get_nameunit(child));
258                 return (NULL);
259         }
260
261         rman_set_rid(rv, *rid);
262         if (bustag != NULL)
263                 rman_set_bustag(rv, bustag);
264
265         if (needsactivate) {
266                 if (bus_activate_resource(child, type, *rid, rv)) {
267                         device_printf(bus, "%s: could not activate resource\n",
268                             __func__);
269                         rman_release_resource(rv);
270                         return (NULL);
271                 }
272         }
273
274         return (rv);
275 }
276
277 static int
278 xlp_simplebus_activate_resource(device_t bus, device_t child, int type, int rid,
279     struct resource *r)
280 {
281         void *vaddr;
282         vm_paddr_t paddr;
283         vm_size_t psize;
284
285         /*
286          * If this is a memory resource, use pmap_mapdev to map it.
287          */
288         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
289                 paddr = rman_get_start(r);
290                 psize = rman_get_size(r);
291                 vaddr = pmap_mapdev(paddr, psize);
292
293                 rman_set_virtual(r, vaddr);
294                 rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr);
295         }
296
297         return (rman_activate_resource(r));
298 }
299
300 static int
301 xlp_simplebus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
302     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
303 {
304         register_t s;
305         int irq;
306
307         /* setup irq */
308         s = intr_disable();
309         irq = rman_get_start(res);
310         cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg,
311             irq, flags, cookiep);
312         intr_restore(s);
313         return (0);
314 }
315
316 static int
317 xlp_simplebus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
318     pcell_t *irq)
319 {
320
321         return ((int)irq[0]);
322 }