xref: /freebsd/sys/powerpc/powernv/powernv_xscom.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * Copyright (c) 2017-2018 QCM Technologies.
3  * Copyright (c) 2017-2018 Semihalf.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, 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 
28 #include "opt_platform.h"
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mbuf.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42 #include <machine/bus.h>
43 
44 #ifdef FDT
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #endif
48 
49 struct powernv_xscom_softc
50 {
51 
52 };
53 
54 static int powernv_xscom_attach(device_t);
55 static int powernv_xscom_probe(device_t);
56 static const struct ofw_bus_devinfo *
57     powernv_xscom_get_devinfo(device_t, device_t);
58 
59 static device_method_t powernv_xscom_methods[] = {
60 	/* Device interface */
61 	DEVMETHOD(device_probe,		powernv_xscom_probe),
62 	DEVMETHOD(device_attach,	powernv_xscom_attach),
63 
64 	/* ofw_bus interface */
65 	DEVMETHOD(ofw_bus_get_devinfo,	powernv_xscom_get_devinfo),
66 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
67 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
68 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
69 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
70 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
71 
72 	DEVMETHOD_END
73 };
74 
75 static int
76 powernv_xscom_probe(device_t dev)
77 {
78 
79 	if (!(ofw_bus_is_compatible(dev, "ibm,xscom")))
80 		return (ENXIO);
81 
82 	device_set_desc(dev, "xscom");
83 	return (0);
84 }
85 
86 static int
87 powernv_xscom_attach(device_t dev)
88 {
89 	phandle_t child;
90 	device_t cdev;
91 	struct ofw_bus_devinfo *dinfo;
92 
93 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
94 	    child = OF_peer(child)) {
95 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
96 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
97 			free(dinfo, M_DEVBUF);
98 			continue;
99 		}
100 		cdev = device_add_child(dev, NULL, -1);
101 		if (cdev == NULL) {
102 			device_printf(dev, "<%s>: device_add_child failed\n",
103 			    dinfo->obd_name);
104 			ofw_bus_gen_destroy_devinfo(dinfo);
105 			free(dinfo, M_DEVBUF);
106 			continue;
107 		}
108 		device_set_ivars(cdev, dinfo);
109 	}
110 
111 	return (bus_generic_attach(dev));
112 }
113 
114 static const struct ofw_bus_devinfo *
115 powernv_xscom_get_devinfo(device_t dev, device_t child)
116 {
117         return (device_get_ivars(child));
118 }
119 
120 DEFINE_CLASS_0(powernv_xscom, powernv_xscom_driver, powernv_xscom_methods,
121     sizeof(struct powernv_xscom_softc));
122 DRIVER_MODULE(powernv_xscom, ofwbus, powernv_xscom_driver, NULL, NULL);
123