Deleted Added
full compact
macio.c (157895) macio.c (174782)
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 *
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: head/sys/powerpc/powermac/macio.c 157895 2006-04-20 04:19:10Z imp $
27 * $FreeBSD: head/sys/powerpc/powermac/macio.c 174782 2007-12-19 18:00:50Z marcel $
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 */
62struct 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
69static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
70
71static int macio_probe(device_t);
72static int macio_attach(device_t);
73static int macio_print_child(device_t dev, device_t child);
74static void macio_probe_nomatch(device_t, device_t);
75static struct resource *macio_alloc_resource(device_t, device_t, int, int *,
76 u_long, u_long, u_long, u_int);
77static int macio_activate_resource(device_t, device_t, int, int,
78 struct resource *);
79static int macio_deactivate_resource(device_t, device_t, int, int,
80 struct resource *);
81static int macio_release_resource(device_t, device_t, int, int,
82 struct resource *);
83static struct resource_list *macio_get_resource_list (device_t, device_t);
84static ofw_bus_get_devinfo_t macio_get_devinfo;
85
86/*
87 * Bus interface definition
88 */
89static 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
121static driver_t macio_pci_driver = {
122 "macio",
123 macio_methods,
124 sizeof(struct macio_softc)
125};
126
127devclass_t macio_devclass;
128
129DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
130
131/*
132 * PCI ID search table
133 */
134static 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
152struct macio_quirk_entry {
153 const char *mq_name;
154 int mq_quirks;
155};
156
157static 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
164static int
165macio_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 */
179static void
180macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
181{
182 int intr;
183
184 if (dinfo->mdi_ninterrupts >= 5) {
185 printf("macio: device has more than 5 interrupts\n");
186 return;
187 }
188
189 if (OF_getprop(devnode, "interrupts", &intr, sizeof(intr)) == -1) {
190 if (OF_getprop(devnode, "AAPL,interrupts", &intr,
191 sizeof(intr)) == -1)
192 return;
193 }
194
195 if (intr == -1)
196 return;
197
198 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
199 dinfo->mdi_ninterrupts, intr, intr, 1);
200
201 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr;
202 dinfo->mdi_ninterrupts++;
203}
204
205
206static void
207macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
208{
209 struct macio_reg *reg;
210 int i, nreg;
211
212 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
213 if (nreg == -1)
214 return;
215
216 for (i = 0; i < nreg; i++) {
217 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
218 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
219 reg[i].mr_size);
220 }
221}
222
223/*
224 * PCI probe
225 */
226static int
227macio_probe(device_t dev)
228{
229 int i;
230 u_int32_t devid;
231
232 devid = pci_get_devid(dev);
233 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
234 if (devid == macio_pci_devlist[i].mpd_devid) {
235 device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
236 return (0);
237 }
238 }
239
240 return (ENXIO);
241}
242
243/*
244 * PCI attach: scan Open Firmware child nodes, and attach these as children
245 * of the macio bus
246 */
247static int
248macio_attach(device_t dev)
249{
250 struct macio_softc *sc;
251 struct macio_devinfo *dinfo;
252 phandle_t root;
253 phandle_t child;
254 phandle_t subchild;
255 device_t cdev;
256 u_int reg[3];
257 int quirks;
258
259 sc = device_get_softc(dev);
260 root = sc->sc_node = OF_finddevice("mac-io");
261
262 /*
263 * Locate the device node and it's base address
264 */
265 if (OF_getprop(root, "assigned-addresses",
266 reg, sizeof(reg)) < sizeof(reg)) {
267 return (ENXIO);
268 }
269
270 sc->sc_base = reg[2];
271 sc->sc_size = MACIO_REG_SIZE;
272
273 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
274 sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
275 if (rman_init(&sc->sc_mem_rman) != 0) {
276 device_printf(dev,
277 "failed to init mem range resources\n");
278 return (ENXIO);
279 }
280 rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
281
282 /*
283 * Iterate through the sub-devices
284 */
285 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
286 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
287 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
288 0) {
289 free(dinfo, M_MACIO);
290 continue;
291 }
292 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
293 if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
294 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
295 free(dinfo, M_MACIO);
296 continue;
297 }
298 resource_list_init(&dinfo->mdi_resources);
299 dinfo->mdi_ninterrupts = 0;
300 macio_add_intr(child, dinfo);
301 macio_add_reg(child, dinfo);
302 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
303 for (subchild = OF_child(child); subchild != 0;
304 subchild = OF_peer(subchild))
305 macio_add_intr(subchild, dinfo);
306 cdev = device_add_child(dev, NULL, -1);
307 if (cdev == NULL) {
308 device_printf(dev, "<%s>: device_add_child failed\n",
309 dinfo->mdi_obdinfo.obd_name);
310 resource_list_free(&dinfo->mdi_resources);
311 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
312 free(dinfo, M_MACIO);
313 continue;
314 }
315 device_set_ivars(cdev, dinfo);
316 }
317
318 return (bus_generic_attach(dev));
319}
320
321
322static int
323macio_print_child(device_t dev, device_t child)
324{
325 struct macio_devinfo *dinfo;
326 struct resource_list *rl;
327 int retval = 0;
328
329 dinfo = device_get_ivars(child);
330 rl = &dinfo->mdi_resources;
331
332 retval += bus_print_child_header(dev, child);
333
334 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
335 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
336
337 retval += bus_print_child_footer(dev, child);
338
339 return (retval);
340}
341
342
343static void
344macio_probe_nomatch(device_t dev, device_t child)
345{
346 struct macio_devinfo *dinfo;
347 struct resource_list *rl;
348 const char *type;
349
350 if (bootverbose) {
351 dinfo = device_get_ivars(child);
352 rl = &dinfo->mdi_resources;
353
354 if ((type = ofw_bus_get_type(child)) == NULL)
355 type = "(unknown)";
356 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
357 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
358 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
359 printf(" (no driver attached)\n");
360 }
361}
362
363
364static struct resource *
365macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
366 u_long start, u_long end, u_long count, u_int flags)
367{
368 struct macio_softc *sc;
369 int needactivate;
370 struct resource *rv;
371 struct rman *rm;
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 */
62struct 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
69static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
70
71static int macio_probe(device_t);
72static int macio_attach(device_t);
73static int macio_print_child(device_t dev, device_t child);
74static void macio_probe_nomatch(device_t, device_t);
75static struct resource *macio_alloc_resource(device_t, device_t, int, int *,
76 u_long, u_long, u_long, u_int);
77static int macio_activate_resource(device_t, device_t, int, int,
78 struct resource *);
79static int macio_deactivate_resource(device_t, device_t, int, int,
80 struct resource *);
81static int macio_release_resource(device_t, device_t, int, int,
82 struct resource *);
83static struct resource_list *macio_get_resource_list (device_t, device_t);
84static ofw_bus_get_devinfo_t macio_get_devinfo;
85
86/*
87 * Bus interface definition
88 */
89static 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
121static driver_t macio_pci_driver = {
122 "macio",
123 macio_methods,
124 sizeof(struct macio_softc)
125};
126
127devclass_t macio_devclass;
128
129DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
130
131/*
132 * PCI ID search table
133 */
134static 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
152struct macio_quirk_entry {
153 const char *mq_name;
154 int mq_quirks;
155};
156
157static 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
164static int
165macio_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 */
179static void
180macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
181{
182 int intr;
183
184 if (dinfo->mdi_ninterrupts >= 5) {
185 printf("macio: device has more than 5 interrupts\n");
186 return;
187 }
188
189 if (OF_getprop(devnode, "interrupts", &intr, sizeof(intr)) == -1) {
190 if (OF_getprop(devnode, "AAPL,interrupts", &intr,
191 sizeof(intr)) == -1)
192 return;
193 }
194
195 if (intr == -1)
196 return;
197
198 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
199 dinfo->mdi_ninterrupts, intr, intr, 1);
200
201 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr;
202 dinfo->mdi_ninterrupts++;
203}
204
205
206static void
207macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
208{
209 struct macio_reg *reg;
210 int i, nreg;
211
212 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
213 if (nreg == -1)
214 return;
215
216 for (i = 0; i < nreg; i++) {
217 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
218 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
219 reg[i].mr_size);
220 }
221}
222
223/*
224 * PCI probe
225 */
226static int
227macio_probe(device_t dev)
228{
229 int i;
230 u_int32_t devid;
231
232 devid = pci_get_devid(dev);
233 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
234 if (devid == macio_pci_devlist[i].mpd_devid) {
235 device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
236 return (0);
237 }
238 }
239
240 return (ENXIO);
241}
242
243/*
244 * PCI attach: scan Open Firmware child nodes, and attach these as children
245 * of the macio bus
246 */
247static int
248macio_attach(device_t dev)
249{
250 struct macio_softc *sc;
251 struct macio_devinfo *dinfo;
252 phandle_t root;
253 phandle_t child;
254 phandle_t subchild;
255 device_t cdev;
256 u_int reg[3];
257 int quirks;
258
259 sc = device_get_softc(dev);
260 root = sc->sc_node = OF_finddevice("mac-io");
261
262 /*
263 * Locate the device node and it's base address
264 */
265 if (OF_getprop(root, "assigned-addresses",
266 reg, sizeof(reg)) < sizeof(reg)) {
267 return (ENXIO);
268 }
269
270 sc->sc_base = reg[2];
271 sc->sc_size = MACIO_REG_SIZE;
272
273 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
274 sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
275 if (rman_init(&sc->sc_mem_rman) != 0) {
276 device_printf(dev,
277 "failed to init mem range resources\n");
278 return (ENXIO);
279 }
280 rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
281
282 /*
283 * Iterate through the sub-devices
284 */
285 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
286 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
287 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
288 0) {
289 free(dinfo, M_MACIO);
290 continue;
291 }
292 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
293 if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
294 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
295 free(dinfo, M_MACIO);
296 continue;
297 }
298 resource_list_init(&dinfo->mdi_resources);
299 dinfo->mdi_ninterrupts = 0;
300 macio_add_intr(child, dinfo);
301 macio_add_reg(child, dinfo);
302 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
303 for (subchild = OF_child(child); subchild != 0;
304 subchild = OF_peer(subchild))
305 macio_add_intr(subchild, dinfo);
306 cdev = device_add_child(dev, NULL, -1);
307 if (cdev == NULL) {
308 device_printf(dev, "<%s>: device_add_child failed\n",
309 dinfo->mdi_obdinfo.obd_name);
310 resource_list_free(&dinfo->mdi_resources);
311 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
312 free(dinfo, M_MACIO);
313 continue;
314 }
315 device_set_ivars(cdev, dinfo);
316 }
317
318 return (bus_generic_attach(dev));
319}
320
321
322static int
323macio_print_child(device_t dev, device_t child)
324{
325 struct macio_devinfo *dinfo;
326 struct resource_list *rl;
327 int retval = 0;
328
329 dinfo = device_get_ivars(child);
330 rl = &dinfo->mdi_resources;
331
332 retval += bus_print_child_header(dev, child);
333
334 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
335 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
336
337 retval += bus_print_child_footer(dev, child);
338
339 return (retval);
340}
341
342
343static void
344macio_probe_nomatch(device_t dev, device_t child)
345{
346 struct macio_devinfo *dinfo;
347 struct resource_list *rl;
348 const char *type;
349
350 if (bootverbose) {
351 dinfo = device_get_ivars(child);
352 rl = &dinfo->mdi_resources;
353
354 if ((type = ofw_bus_get_type(child)) == NULL)
355 type = "(unknown)";
356 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
357 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
358 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
359 printf(" (no driver attached)\n");
360 }
361}
362
363
364static struct resource *
365macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
366 u_long start, u_long end, u_long count, u_int flags)
367{
368 struct macio_softc *sc;
369 int needactivate;
370 struct resource *rv;
371 struct rman *rm;
372 bus_space_tag_t tagval;
373 u_long adjstart, adjend, adjcount;
374 struct macio_devinfo *dinfo;
375 struct resource_list_entry *rle;
376
377 sc = device_get_softc(bus);
378 dinfo = device_get_ivars(child);
379
380 needactivate = flags & RF_ACTIVE;
381 flags &= ~RF_ACTIVE;
382
383 switch (type) {
384 case SYS_RES_MEMORY:
385 case SYS_RES_IOPORT:
386 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
387 *rid);
388 if (rle == NULL) {
389 device_printf(bus, "no rle for %s memory %d\n",
390 device_get_nameunit(child), *rid);
391 return (NULL);
392 }
393
394 if (start < rle->start)
395 adjstart = rle->start;
396 else if (start > rle->end)
397 adjstart = rle->end;
398 else
399 adjstart = start;
400
401 if (end < rle->start)
402 adjend = rle->start;
403 else if (end > rle->end)
404 adjend = rle->end;
405 else
406 adjend = end;
407
408 adjcount = adjend - adjstart;
409
410 rm = &sc->sc_mem_rman;
372 u_long adjstart, adjend, adjcount;
373 struct macio_devinfo *dinfo;
374 struct resource_list_entry *rle;
375
376 sc = device_get_softc(bus);
377 dinfo = device_get_ivars(child);
378
379 needactivate = flags & RF_ACTIVE;
380 flags &= ~RF_ACTIVE;
381
382 switch (type) {
383 case SYS_RES_MEMORY:
384 case SYS_RES_IOPORT:
385 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
386 *rid);
387 if (rle == NULL) {
388 device_printf(bus, "no rle for %s memory %d\n",
389 device_get_nameunit(child), *rid);
390 return (NULL);
391 }
392
393 if (start < rle->start)
394 adjstart = rle->start;
395 else if (start > rle->end)
396 adjstart = rle->end;
397 else
398 adjstart = start;
399
400 if (end < rle->start)
401 adjend = rle->start;
402 else if (end > rle->end)
403 adjend = rle->end;
404 else
405 adjend = end;
406
407 adjcount = adjend - adjstart;
408
409 rm = &sc->sc_mem_rman;
411
412 tagval = PPC_BUS_SPACE_MEM;
413 break;
414
415 case SYS_RES_IRQ:
416 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
417 *rid);
418 if (rle == NULL) {
419 if (dinfo->mdi_ninterrupts >= 5) {
420 device_printf(bus,
421 "%s has more than 5 interrupts\n",
422 device_get_nameunit(child));
423 return (NULL);
424 }
425 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
426 dinfo->mdi_ninterrupts, start, start, 1);
427
428 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
429 dinfo->mdi_ninterrupts++;
430 }
431
432 return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
433 type, rid, start, end, count, flags));
410 break;
411
412 case SYS_RES_IRQ:
413 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
414 *rid);
415 if (rle == NULL) {
416 if (dinfo->mdi_ninterrupts >= 5) {
417 device_printf(bus,
418 "%s has more than 5 interrupts\n",
419 device_get_nameunit(child));
420 return (NULL);
421 }
422 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
423 dinfo->mdi_ninterrupts, start, start, 1);
424
425 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
426 dinfo->mdi_ninterrupts++;
427 }
428
429 return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
430 type, rid, start, end, count, flags));
434 break;
435
436 default:
437 device_printf(bus, "unknown resource request from %s\n",
438 device_get_nameunit(child));
439 return (NULL);
440 }
441
442 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
443 child);
444 if (rv == NULL) {
445 device_printf(bus,
446 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
447 adjstart, adjend, adjcount, device_get_nameunit(child));
448 return (NULL);
449 }
450
451 rman_set_rid(rv, *rid);
431
432 default:
433 device_printf(bus, "unknown resource request from %s\n",
434 device_get_nameunit(child));
435 return (NULL);
436 }
437
438 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
439 child);
440 if (rv == NULL) {
441 device_printf(bus,
442 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
443 adjstart, adjend, adjcount, device_get_nameunit(child));
444 return (NULL);
445 }
446
447 rman_set_rid(rv, *rid);
452 rman_set_bustag(rv, tagval);
448 rman_set_bustag(rv, &bs_le_tag);
453 rman_set_bushandle(rv, rman_get_start(rv));
454
455 if (needactivate) {
456 if (bus_activate_resource(child, type, *rid, rv) != 0) {
457 device_printf(bus,
458 "failed to activate resource for %s\n",
459 device_get_nameunit(child));
460 rman_release_resource(rv);
461 return (NULL);
462 }
463 }
464
465 return (rv);
466}
467
468
469static int
470macio_release_resource(device_t bus, device_t child, int type, int rid,
471 struct resource *res)
472{
473 if (rman_get_flags(res) & RF_ACTIVE) {
474 int error = bus_deactivate_resource(child, type, rid, res);
475 if (error)
476 return error;
477 }
478
479 return (rman_release_resource(res));
480}
481
482
483static int
484macio_activate_resource(device_t bus, device_t child, int type, int rid,
485 struct resource *res)
486{
487 struct macio_softc *sc;
488 void *p;
489
490 sc = device_get_softc(bus);
491
492 if (type == SYS_RES_IRQ)
493 return (bus_activate_resource(bus, type, rid, res));
494
495 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
496 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
497 (vm_size_t)rman_get_size(res));
498 if (p == NULL)
499 return (ENOMEM);
500 rman_set_virtual(res, p);
501 rman_set_bushandle(res, (u_long)p);
502 }
503
504 return (rman_activate_resource(res));
505}
506
507
508static int
509macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
510 struct resource *res)
511{
512 /*
513 * If this is a memory resource, unmap it.
514 */
515 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
516 u_int32_t psize;
517
518 psize = rman_get_size(res);
519 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
520 }
521
522 return (rman_deactivate_resource(res));
523}
524
525
526static struct resource_list *
527macio_get_resource_list (device_t dev, device_t child)
528{
529 struct macio_devinfo *dinfo;
530
531 dinfo = device_get_ivars(child);
532 return (&dinfo->mdi_resources);
533}
534
535static const struct ofw_bus_devinfo *
536macio_get_devinfo(device_t dev, device_t child)
537{
538 struct macio_devinfo *dinfo;
539
540 dinfo = device_get_ivars(child);
541 return (&dinfo->mdi_obdinfo);
542}
449 rman_set_bushandle(rv, rman_get_start(rv));
450
451 if (needactivate) {
452 if (bus_activate_resource(child, type, *rid, rv) != 0) {
453 device_printf(bus,
454 "failed to activate resource for %s\n",
455 device_get_nameunit(child));
456 rman_release_resource(rv);
457 return (NULL);
458 }
459 }
460
461 return (rv);
462}
463
464
465static int
466macio_release_resource(device_t bus, device_t child, int type, int rid,
467 struct resource *res)
468{
469 if (rman_get_flags(res) & RF_ACTIVE) {
470 int error = bus_deactivate_resource(child, type, rid, res);
471 if (error)
472 return error;
473 }
474
475 return (rman_release_resource(res));
476}
477
478
479static int
480macio_activate_resource(device_t bus, device_t child, int type, int rid,
481 struct resource *res)
482{
483 struct macio_softc *sc;
484 void *p;
485
486 sc = device_get_softc(bus);
487
488 if (type == SYS_RES_IRQ)
489 return (bus_activate_resource(bus, type, rid, res));
490
491 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
492 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
493 (vm_size_t)rman_get_size(res));
494 if (p == NULL)
495 return (ENOMEM);
496 rman_set_virtual(res, p);
497 rman_set_bushandle(res, (u_long)p);
498 }
499
500 return (rman_activate_resource(res));
501}
502
503
504static int
505macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
506 struct resource *res)
507{
508 /*
509 * If this is a memory resource, unmap it.
510 */
511 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
512 u_int32_t psize;
513
514 psize = rman_get_size(res);
515 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
516 }
517
518 return (rman_deactivate_resource(res));
519}
520
521
522static struct resource_list *
523macio_get_resource_list (device_t dev, device_t child)
524{
525 struct macio_devinfo *dinfo;
526
527 dinfo = device_get_ivars(child);
528 return (&dinfo->mdi_resources);
529}
530
531static const struct ofw_bus_devinfo *
532macio_get_devinfo(device_t dev, device_t child)
533{
534 struct macio_devinfo *dinfo;
535
536 dinfo = device_get_ivars(child);
537 return (&dinfo->mdi_obdinfo);
538}