]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/ebus/ebus.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / ebus / ebus.c
1 /*-
2  * Copyright (c) 1999, 2000 Matthew R. Green
3  * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      from: NetBSD: ebus.c,v 1.26 2001/09/10 16:27:53 eeh Exp
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * UltraSPARC 5 and beyond EBus support
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45
46 #include <machine/bus.h>
47
48 #include <sys/rman.h>
49
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
53
54 #include <machine/ofw_bus.h>
55 #include <machine/resource.h>
56
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59
60 #include <sparc64/pci/ofw_pci.h>
61
62 /*
63  * The register, ranges and interrupt map properties are identical to the ISA
64  * ones.
65  */
66 #include <sparc64/isa/ofw_isa.h>
67
68 struct ebus_devinfo {
69         struct ofw_bus_devinfo  edi_obdinfo;
70         struct resource_list    edi_rl;
71 };
72
73 struct ebus_rinfo {
74         int                     eri_rtype;
75         struct rman             eri_rman;
76         struct resource         *eri_res;
77 };
78
79 struct ebus_softc {
80         struct isa_ranges       *sc_range;
81         struct ebus_rinfo       *sc_rinfo;
82
83         int                     sc_nrange;
84
85         struct ofw_bus_iinfo    sc_iinfo;
86 };
87
88 static device_probe_t ebus_probe;
89 static device_attach_t ebus_attach;
90 static bus_print_child_t ebus_print_child;
91 static bus_probe_nomatch_t ebus_probe_nomatch;
92 static bus_alloc_resource_t ebus_alloc_resource;
93 static bus_release_resource_t ebus_release_resource;
94 static bus_get_resource_list_t ebus_get_resource_list;
95 static ofw_bus_get_devinfo_t ebus_get_devinfo;
96
97 static struct ebus_devinfo *ebus_setup_dinfo(device_t, struct ebus_softc *,
98     phandle_t);
99 static void ebus_destroy_dinfo(struct ebus_devinfo *);
100 static int ebus_print_res(struct ebus_devinfo *);
101
102 static device_method_t ebus_methods[] = {
103         /* Device interface */
104         DEVMETHOD(device_probe,         ebus_probe),
105         DEVMETHOD(device_attach,        ebus_attach),
106         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
107         DEVMETHOD(device_suspend,       bus_generic_suspend),
108         DEVMETHOD(device_resume,        bus_generic_resume),
109
110         /* Bus interface */
111         DEVMETHOD(bus_print_child,      ebus_print_child),
112         DEVMETHOD(bus_probe_nomatch,    ebus_probe_nomatch),
113         DEVMETHOD(bus_alloc_resource,   ebus_alloc_resource),
114         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
115         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
116         DEVMETHOD(bus_release_resource, ebus_release_resource),
117         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
118         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
119         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
120         DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
121
122         /* ofw_bus interface */
123         DEVMETHOD(ofw_bus_get_devinfo,  ebus_get_devinfo),
124         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
125         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
126         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
127         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
128         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
129
130         KOBJMETHOD_END
131 };
132
133 static driver_t ebus_driver = {
134         "ebus",
135         ebus_methods,
136         sizeof(struct ebus_softc),
137 };
138
139 static devclass_t ebus_devclass;
140
141 DRIVER_MODULE(ebus, pci, ebus_driver, ebus_devclass, 0, 0);
142 MODULE_DEPEND(ebus, pci, 1, 1, 1);
143 MODULE_VERSION(ebus, 1);
144
145 static int
146 ebus_probe(device_t dev)
147 {
148
149         if (pci_get_class(dev) != PCIC_BRIDGE ||
150             pci_get_vendor(dev) != 0x108e ||
151             strcmp(ofw_bus_get_name(dev), "ebus") != 0)
152                 return (ENXIO);
153
154         if (pci_get_device(dev) == 0x1000)
155                 device_set_desc(dev, "PCI-EBus2 bridge");
156         else if (pci_get_device(dev) == 0x1100)
157                 device_set_desc(dev, "PCI-EBus3 bridge");
158         else
159                 return (ENXIO);
160         return (0);
161 }
162
163 static int
164 ebus_attach(device_t dev)
165 {
166         struct ebus_softc *sc;
167         struct ebus_devinfo *edi;
168         struct ebus_rinfo *eri;
169         struct resource *res;
170         device_t cdev;
171         phandle_t node;
172         int i, rnum, rid;
173
174         sc = device_get_softc(dev);
175
176         node = ofw_bus_get_node(dev);
177         sc->sc_nrange = OF_getprop_alloc(node, "ranges",
178             sizeof(*sc->sc_range), (void **)&sc->sc_range);
179         if (sc->sc_nrange == -1) {
180                 printf("ebus_attach: could not get ranges property\n");
181                 return (ENXIO);
182         }
183
184         sc->sc_rinfo = malloc(sizeof(*sc->sc_rinfo) * sc->sc_nrange, M_DEVBUF,
185             M_WAITOK | M_ZERO);
186
187         /* For every range, there must be a matching resource. */
188         for (rnum = 0; rnum < sc->sc_nrange; rnum++) {
189                 eri = &sc->sc_rinfo[rnum];
190                 eri->eri_rtype = ofw_isa_range_restype(&sc->sc_range[rnum]);
191                 rid = PCIR_BAR(rnum);
192                 res = bus_alloc_resource_any(dev, eri->eri_rtype, &rid,
193                     RF_ACTIVE);
194                 if (res == NULL) {
195                         printf("ebus_attach: failed to allocate range "
196                             "resource!\n");
197                         goto fail;
198                 }
199                 eri->eri_res = res;
200                 eri->eri_rman.rm_type = RMAN_ARRAY;
201                 eri->eri_rman.rm_descr = "EBus range";
202                 if (rman_init(&eri->eri_rman) != 0) {
203                         printf("ebus_attach: failed to initialize rman!");
204                         goto fail;
205                 }
206                 if (rman_manage_region(&eri->eri_rman, rman_get_start(res),
207                     rman_get_end(res)) != 0) {
208                         printf("ebus_attach: failed to register region!");
209                         rman_fini(&eri->eri_rman);
210                         goto fail;
211                 }
212         }
213
214         ofw_bus_setup_iinfo(node, &sc->sc_iinfo, sizeof(ofw_isa_intr_t));
215
216         /*
217          * Now attach our children.
218          */
219         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
220                 if ((edi = ebus_setup_dinfo(dev, sc, node)) == NULL)
221                         continue;
222                 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
223                         device_printf(dev, "<%s>: device_add_child failed\n",
224                             edi->edi_obdinfo.obd_name);
225                         ebus_destroy_dinfo(edi);
226                         continue;
227                 }
228                 device_set_ivars(cdev, edi);
229         }
230         return (bus_generic_attach(dev));
231
232 fail:
233         for (i = rnum; i >= 0; i--) {
234                 eri = &sc->sc_rinfo[i];
235                 if (i < rnum)
236                         rman_fini(&eri->eri_rman);
237                 if (eri->eri_res != 0) {
238                         bus_release_resource(dev, eri->eri_rtype,
239                             PCIR_BAR(rnum), eri->eri_res);
240                 }
241         }
242         free(sc->sc_rinfo, M_DEVBUF);
243         free(sc->sc_range, M_OFWPROP);
244         return (ENXIO);
245 }
246
247 static int
248 ebus_print_child(device_t dev, device_t child)
249 {
250         int retval;
251
252         retval = bus_print_child_header(dev, child);
253         retval += ebus_print_res(device_get_ivars(child));
254         retval += bus_print_child_footer(dev, child);
255         return (retval);
256 }
257
258 static void
259 ebus_probe_nomatch(device_t dev, device_t child)
260 {
261
262         device_printf(dev, "<%s>", ofw_bus_get_name(child));
263         ebus_print_res(device_get_ivars(child));
264         printf(" (no driver attached)\n");
265 }
266
267 static struct resource *
268 ebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
269     u_long start, u_long end, u_long count, u_int flags)
270 {
271         struct ebus_softc *sc;
272         struct resource_list *rl;
273         struct resource_list_entry *rle = NULL;
274         struct resource *res;
275         struct ebus_rinfo *ri;
276         bus_space_tag_t bt;
277         bus_space_handle_t bh;
278         int passthrough = (device_get_parent(child) != bus);
279         int isdefault = (start == 0UL && end == ~0UL);
280         int ridx, rv;
281
282         sc = (struct ebus_softc *)device_get_softc(bus);
283         rl = BUS_GET_RESOURCE_LIST(bus, child);
284         /*
285          * Map EBus ranges to PCI ranges.  This may include changing the
286          * allocation type.
287          */
288         switch (type) {
289         case SYS_RES_MEMORY:
290                 KASSERT(!(isdefault && passthrough),
291                     ("ebus_alloc_resource: passthrough of default alloc"));
292                 if (!passthrough) {
293                         rle = resource_list_find(rl, type, *rid);
294                         if (rle == NULL)
295                                 return (NULL);
296                         KASSERT(rle->res == NULL,
297                             ("ebus_alloc_resource: resource entry is busy"));
298                         if (isdefault) {
299                                 start = rle->start;
300                                 count = ulmax(count, rle->count);
301                                 end = ulmax(rle->end, start + count - 1);
302                         }
303                 }
304
305                 (void)ofw_isa_range_map(sc->sc_range, sc->sc_nrange,
306                     &start, &end, &ridx);
307
308                 ri = &sc->sc_rinfo[ridx];
309                 res = rman_reserve_resource(&ri->eri_rman, start, end, count,
310                     flags, child);
311                 if (res == NULL)
312                         return (NULL);
313                 rman_set_rid(res, *rid);
314                 bt = rman_get_bustag(ri->eri_res);
315                 rman_set_bustag(res, bt);
316                 rv = bus_space_subregion(bt, rman_get_bushandle(ri->eri_res),
317                     rman_get_start(res) - rman_get_start(ri->eri_res), count,
318                     &bh);
319                 if (rv != 0) {
320                         rman_release_resource(res);
321                         return (NULL);
322                 }
323                 rman_set_bushandle(res, bh);
324                 if (!passthrough)
325                         rle->res = res;
326                 return (res);
327         case SYS_RES_IRQ:
328                 return (resource_list_alloc(rl, bus, child, type, rid, start,
329                     end, count, flags));
330         }
331         return (NULL);
332 }
333
334 static int
335 ebus_release_resource(device_t bus, device_t child, int type, int rid,
336     struct resource *res)
337 {
338         struct resource_list *rl;
339         struct resource_list_entry *rle;
340         int passthrough = (device_get_parent(child) != bus);
341         int rv;
342
343         rl = BUS_GET_RESOURCE_LIST(bus, child);
344         switch (type) {
345         case SYS_RES_MEMORY:
346                 if ((rv = rman_release_resource(res)) != 0)
347                         return (rv);
348                 if (!passthrough) {
349                         rle = resource_list_find(rl, type, rid);
350                         KASSERT(rle != NULL, ("ebus_release_resource: "
351                             "resource entry not found!"));
352                         KASSERT(rle->res != NULL, ("ebus_alloc_resource: "
353                             "resource entry is not busy"));
354                         rle->res = NULL;
355                 }
356                 break;
357         case SYS_RES_IRQ:
358                 return (resource_list_release(rl, bus, child, type, rid, res));
359         default:
360                 panic("ebus_release_resource: unsupported resource type %d",
361                     type);
362         }
363         return (0);
364 }
365
366 static struct resource_list *
367 ebus_get_resource_list(device_t dev, device_t child)
368 {
369         struct ebus_devinfo *edi;
370
371         edi = device_get_ivars(child);
372         return (&edi->edi_rl);
373 }
374
375 static const struct ofw_bus_devinfo *
376 ebus_get_devinfo(device_t bus, device_t dev)
377 {
378         struct ebus_devinfo *edi;
379
380         edi = device_get_ivars(dev);
381         return (&edi->edi_obdinfo);
382 }
383
384 static struct ebus_devinfo *
385 ebus_setup_dinfo(device_t dev, struct ebus_softc *sc, phandle_t node)
386 {
387         struct ebus_devinfo *edi;
388         struct isa_regs *reg;
389         ofw_isa_intr_t *intrs;
390         ofw_pci_intr_t rintr;
391         u_int64_t start;
392         int nreg, nintr, i;
393
394         edi = malloc(sizeof(*edi), M_DEVBUF, M_ZERO | M_WAITOK);
395         if (ofw_bus_gen_setup_devinfo(&edi->edi_obdinfo, node) != 0) {
396                 free(edi, M_DEVBUF);
397                 return (NULL);
398         }
399         resource_list_init(&edi->edi_rl);
400         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
401         if (nreg == -1) {
402                 device_printf(dev, "<%s>: incomplete\n",
403                     edi->edi_obdinfo.obd_name);
404                 goto fail;
405         }
406         for (i = 0; i < nreg; i++) {
407                 start = ISA_REG_PHYS(reg + i);
408                 resource_list_add(&edi->edi_rl, SYS_RES_MEMORY, i,
409                     start, start + reg[i].size - 1, reg[i].size);
410         }
411         free(reg, M_OFWPROP);
412
413         nintr = OF_getprop_alloc(node, "interrupts",  sizeof(*intrs),
414             (void **)&intrs);
415         for (i = 0; i < nintr; i++) {
416                 rintr = ofw_isa_route_intr(dev, node, &sc->sc_iinfo, intrs[i]);
417                 if (rintr == PCI_INVALID_IRQ) {
418                         device_printf(dev,
419                             "<%s>: could not map EBus interrupt %d\n",
420                             edi->edi_obdinfo.obd_name, intrs[i]);
421                         free(intrs, M_OFWPROP);
422                         goto fail;
423                 }
424                 resource_list_add(&edi->edi_rl, SYS_RES_IRQ, i,
425                     rintr, rintr, 1);
426         }
427         free(intrs, M_OFWPROP);
428
429         return (edi);
430
431 fail:
432         ebus_destroy_dinfo(edi);
433         return (NULL);
434 }
435
436 static void
437 ebus_destroy_dinfo(struct ebus_devinfo *edi)
438 {
439
440         resource_list_free(&edi->edi_rl);
441         ofw_bus_gen_destroy_devinfo(&edi->edi_obdinfo);
442         free(edi, M_DEVBUF);
443 }
444
445 static int
446 ebus_print_res(struct ebus_devinfo *edi)
447 {
448         int retval;
449
450         retval = 0;
451         retval += resource_list_print_type(&edi->edi_rl, "addr", SYS_RES_MEMORY,
452             "%#lx");
453         retval += resource_list_print_type(&edi->edi_rl, "irq", SYS_RES_IRQ,
454             "%ld");
455         return (retval);
456 }