]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/powerpc/powermac/macio.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / powerpc / powermac / macio.c
1 /*-
2  * Copyright 2002 by Peter Grehan. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 /*
31  * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC.
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42
43 #include <machine/vmparam.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <machine/pmap.h>
47
48 #include <machine/resource.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 <powerpc/powermac/maciovar.h>
55
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
58
59 /*
60  * Macio softc
61  */
62 struct macio_softc {
63         phandle_t    sc_node;
64         vm_offset_t  sc_base;
65         vm_offset_t  sc_size;
66         struct rman  sc_mem_rman;
67 };
68
69 static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
70
71 static int  macio_probe(device_t);
72 static int  macio_attach(device_t);
73 static int  macio_print_child(device_t dev, device_t child);
74 static void macio_probe_nomatch(device_t, device_t);
75 static struct   resource *macio_alloc_resource(device_t, device_t, int, int *,
76                                                u_long, u_long, u_long, u_int);
77 static int  macio_activate_resource(device_t, device_t, int, int,
78                                     struct resource *);
79 static int  macio_deactivate_resource(device_t, device_t, int, int,
80                                       struct resource *);
81 static int  macio_release_resource(device_t, device_t, int, int,
82                                    struct resource *);
83 static struct resource_list *macio_get_resource_list (device_t, device_t);
84 static ofw_bus_get_devinfo_t macio_get_devinfo;
85
86 /*
87  * Bus interface definition
88  */
89 static device_method_t macio_methods[] = {
90         /* Device interface */
91         DEVMETHOD(device_probe,         macio_probe),
92         DEVMETHOD(device_attach,        macio_attach),
93         DEVMETHOD(device_detach,        bus_generic_detach),
94         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
95         DEVMETHOD(device_suspend,       bus_generic_suspend),
96         DEVMETHOD(device_resume,        bus_generic_resume),
97         
98         /* Bus interface */
99         DEVMETHOD(bus_print_child,      macio_print_child),
100         DEVMETHOD(bus_probe_nomatch,    macio_probe_nomatch),
101         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
102         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),     
103
104         DEVMETHOD(bus_alloc_resource,   macio_alloc_resource),
105         DEVMETHOD(bus_release_resource, macio_release_resource),
106         DEVMETHOD(bus_activate_resource, macio_activate_resource),
107         DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource),
108         DEVMETHOD(bus_get_resource_list, macio_get_resource_list),      
109
110         /* ofw_bus interface */
111         DEVMETHOD(ofw_bus_get_devinfo,  macio_get_devinfo),
112         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
113         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
114         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
115         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
116         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
117
118         { 0, 0 }
119 };
120
121 static driver_t macio_pci_driver = {
122         "macio",
123         macio_methods,
124         sizeof(struct macio_softc)
125 };
126
127 devclass_t macio_devclass;
128
129 DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
130
131 /*
132  * PCI ID search table
133  */
134 static struct macio_pci_dev {
135         u_int32_t  mpd_devid;
136         char    *mpd_desc;
137 } macio_pci_devlist[] = {
138         { 0x0017106b, "Paddington I/O Controller" },
139         { 0x0022106b, "KeyLargo I/O Controller" },
140         { 0x0025106b, "Pangea I/O Controller" },
141         { 0x003e106b, "Intrepid I/O Controller" },
142         { 0, NULL }
143 };
144
145 /*
146  * Devices to exclude from the probe
147  * XXX some of these may be required in the future...
148  */
149 #define MACIO_QUIRK_IGNORE              0x00000001
150 #define MACIO_QUIRK_CHILD_HAS_INTR      0x00000002
151
152 struct macio_quirk_entry {
153         const char      *mq_name;
154         int             mq_quirks;
155 };
156
157 static struct macio_quirk_entry macio_quirks[] = {
158         { "escc-legacy",                MACIO_QUIRK_IGNORE },
159         { "timer",                      MACIO_QUIRK_IGNORE },
160         { "escc",                       MACIO_QUIRK_CHILD_HAS_INTR },
161         { NULL,                         0 }
162 };
163
164 static int
165 macio_get_quirks(const char *name)
166 {
167         struct  macio_quirk_entry *mqe;
168
169         for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
170                 if (strcmp(name, mqe->mq_name) == 0)
171                         return (mqe->mq_quirks);
172         return (0);
173 }
174
175
176 /*
177  * Add an interrupt to the dev's resource list if present
178  */
179 static void
180 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
181 {
182         int     *intr;
183         int     i, nintr;
184         phandle_t iparent;
185         int     icells;
186
187         if (dinfo->mdi_ninterrupts >= 6) {
188                 printf("macio: device has more than 6 interrupts\n");
189                 return;
190         }
191
192         icells = 1;
193         
194         if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent))
195                 OF_getprop(iparent, "#interrupt-cells", &icells, sizeof(icells));
196
197         nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr), 
198                 (void **)&intr);
199         if (nintr == -1) {
200                 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts", 
201                         sizeof(*intr), (void **)&intr);
202                 if (nintr == -1)
203                         return;
204         }
205
206         if (intr[0] == -1)
207                 return;
208
209         for (i = 0; i < nintr; i+=icells) {
210                 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
211                     dinfo->mdi_ninterrupts, intr[i], intr[i], 1);
212
213                 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr[i];
214                 dinfo->mdi_ninterrupts++;
215         }
216 }
217
218
219 static void
220 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
221 {
222         struct  macio_reg *reg;
223         int     i, nreg;
224
225         nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
226         if (nreg == -1)
227                 return;
228
229         for (i = 0; i < nreg; i++) {
230                 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
231                     reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
232                     reg[i].mr_size);
233         }
234 }
235
236 /*
237  * PCI probe
238  */
239 static int
240 macio_probe(device_t dev)
241 {
242         int i;
243         u_int32_t devid;
244         
245         devid = pci_get_devid(dev);
246         for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
247                 if (devid == macio_pci_devlist[i].mpd_devid) {
248                         device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
249                         return (0);
250                 }
251         }
252         
253         return (ENXIO); 
254 }
255
256 /*
257  * PCI attach: scan Open Firmware child nodes, and attach these as children
258  * of the macio bus
259  */
260 static int 
261 macio_attach(device_t dev)
262 {
263         struct macio_softc *sc;
264         struct macio_devinfo *dinfo;
265         phandle_t  root;
266         phandle_t  child;
267         phandle_t  subchild;
268         device_t cdev;
269         u_int reg[3];
270         int quirks;
271
272         sc = device_get_softc(dev);
273         root = sc->sc_node = OF_finddevice("mac-io");
274         
275         /*
276          * Locate the device node and it's base address
277          */
278         if (OF_getprop(root, "assigned-addresses", 
279                        reg, sizeof(reg)) < sizeof(reg)) {
280                 return (ENXIO);
281         }
282
283         sc->sc_base = reg[2];
284         sc->sc_size = MACIO_REG_SIZE;
285
286         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
287         sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
288         if (rman_init(&sc->sc_mem_rman) != 0) {
289                 device_printf(dev,
290                               "failed to init mem range resources\n");
291                 return (ENXIO);
292         }
293         rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);   
294
295         /*
296          * Iterate through the sub-devices
297          */
298         for (child = OF_child(root); child != 0; child = OF_peer(child)) {
299                 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
300                 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
301                     0) {
302                         free(dinfo, M_MACIO);
303                         continue;
304                 }
305                 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
306                 if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
307                         ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
308                         free(dinfo, M_MACIO);
309                         continue;
310                 }
311                 resource_list_init(&dinfo->mdi_resources);
312                 dinfo->mdi_ninterrupts = 0;
313                 macio_add_intr(child, dinfo);
314                 macio_add_reg(child, dinfo);
315                 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
316                         for (subchild = OF_child(child); subchild != 0;
317                             subchild = OF_peer(subchild))
318                                 macio_add_intr(subchild, dinfo);
319                 cdev = device_add_child(dev, NULL, -1);
320                 if (cdev == NULL) {
321                         device_printf(dev, "<%s>: device_add_child failed\n",
322                             dinfo->mdi_obdinfo.obd_name);
323                         resource_list_free(&dinfo->mdi_resources);
324                         ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
325                         free(dinfo, M_MACIO);
326                         continue;
327                 }
328                 device_set_ivars(cdev, dinfo);
329         }
330
331         return (bus_generic_attach(dev));
332 }
333
334
335 static int
336 macio_print_child(device_t dev, device_t child)
337 {
338         struct macio_devinfo *dinfo;
339         struct resource_list *rl;
340         int retval = 0;
341
342         dinfo = device_get_ivars(child);
343         rl = &dinfo->mdi_resources;
344
345         retval += bus_print_child_header(dev, child);
346
347         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
348         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
349
350         retval += bus_print_child_footer(dev, child);
351
352         return (retval);
353 }
354
355
356 static void
357 macio_probe_nomatch(device_t dev, device_t child)
358 {
359         struct macio_devinfo *dinfo;
360         struct resource_list *rl;
361         const char *type;
362
363         if (bootverbose) {
364                 dinfo = device_get_ivars(child);
365                 rl = &dinfo->mdi_resources;
366
367                 if ((type = ofw_bus_get_type(child)) == NULL)
368                         type = "(unknown)";
369                 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
370                 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
371                 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
372                 printf(" (no driver attached)\n");
373         }
374 }
375
376
377 static struct resource *
378 macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
379                      u_long start, u_long end, u_long count, u_int flags)
380 {
381         struct          macio_softc *sc;
382         int             needactivate;
383         struct          resource *rv;
384         struct          rman *rm;
385         bus_space_tag_t tagval;
386         u_long          adjstart, adjend, adjcount;
387         struct          macio_devinfo *dinfo;
388         struct          resource_list_entry *rle;
389
390         sc = device_get_softc(bus);
391         dinfo = device_get_ivars(child);
392
393         needactivate = flags & RF_ACTIVE;
394         flags &= ~RF_ACTIVE;
395
396         switch (type) {
397         case SYS_RES_MEMORY:
398         case SYS_RES_IOPORT:
399                 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
400                     *rid);
401                 if (rle == NULL) {
402                         device_printf(bus, "no rle for %s memory %d\n",
403                             device_get_nameunit(child), *rid);
404                         return (NULL);
405                 }
406
407                 if (start < rle->start)
408                         adjstart = rle->start;
409                 else if (start > rle->end)
410                         adjstart = rle->end;
411                 else
412                         adjstart = start;
413
414                 if (end < rle->start)
415                         adjend = rle->start;
416                 else if (end > rle->end)
417                         adjend = rle->end;
418                 else
419                         adjend = end;
420
421                 adjcount = adjend - adjstart;
422
423                 rm = &sc->sc_mem_rman;
424
425                 tagval = PPC_BUS_SPACE_MEM;
426                 break;
427
428         case SYS_RES_IRQ:
429                 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
430                     *rid);
431                 if (rle == NULL) {
432                         if (dinfo->mdi_ninterrupts >= 6) {
433                                 device_printf(bus,
434                                     "%s has more than 6 interrupts\n",
435                                     device_get_nameunit(child));
436                                 return (NULL);
437                         }
438                         resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
439                             dinfo->mdi_ninterrupts, start, start, 1);
440
441                         dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
442                         dinfo->mdi_ninterrupts++;
443                 }
444
445                 return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
446                     type, rid, start, end, count, flags));
447                 break;
448
449         default:
450                 device_printf(bus, "unknown resource request from %s\n",
451                               device_get_nameunit(child));
452                 return (NULL);
453         }
454
455         rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
456             child);
457         if (rv == NULL) {
458                 device_printf(bus,
459                     "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
460                     adjstart, adjend, adjcount, device_get_nameunit(child));
461                 return (NULL);
462         }
463
464         rman_set_rid(rv, *rid);
465         rman_set_bustag(rv, tagval);
466         rman_set_bushandle(rv, rman_get_start(rv));
467
468         if (needactivate) {
469                 if (bus_activate_resource(child, type, *rid, rv) != 0) {
470                         device_printf(bus,
471                                       "failed to activate resource for %s\n",
472                                       device_get_nameunit(child));
473                         rman_release_resource(rv);
474                         return (NULL);
475                 }
476         }
477
478         return (rv);
479 }
480
481
482 static int
483 macio_release_resource(device_t bus, device_t child, int type, int rid,
484                        struct resource *res)
485 {
486         if (rman_get_flags(res) & RF_ACTIVE) {
487                 int error = bus_deactivate_resource(child, type, rid, res);
488                 if (error)
489                         return error;
490         }
491
492         return (rman_release_resource(res));
493 }
494
495
496 static int
497 macio_activate_resource(device_t bus, device_t child, int type, int rid,
498                            struct resource *res)
499 {
500         struct macio_softc *sc;
501         void    *p;
502
503         sc = device_get_softc(bus);
504
505         if (type == SYS_RES_IRQ)
506                 return (bus_activate_resource(bus, type, rid, res));
507
508         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
509                 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
510                                 (vm_size_t)rman_get_size(res));
511                 if (p == NULL)
512                         return (ENOMEM);
513                 rman_set_virtual(res, p);
514                 rman_set_bushandle(res, (u_long)p);
515         }
516
517         return (rman_activate_resource(res));
518 }
519
520
521 static int
522 macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
523                           struct resource *res)
524 {
525         /*
526          * If this is a memory resource, unmap it.
527          */
528         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
529                 u_int32_t psize;
530
531                 psize = rman_get_size(res);
532                 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
533         }
534
535         return (rman_deactivate_resource(res));
536 }
537
538
539 static struct resource_list *
540 macio_get_resource_list (device_t dev, device_t child)
541 {
542         struct macio_devinfo *dinfo;
543
544         dinfo = device_get_ivars(child);
545         return (&dinfo->mdi_resources);
546 }
547
548 static const struct ofw_bus_devinfo *
549 macio_get_devinfo(device_t dev, device_t child)
550 {
551         struct macio_devinfo *dinfo;
552
553         dinfo = device_get_ivars(child);
554         return (&dinfo->mdi_obdinfo);
555 }