xref: /freebsd/sys/powerpc/powernv/opal_i2cm.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 #include <machine/bus.h>
45 
46 #ifdef FDT
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #endif
50 
51 struct opal_i2cm_softc
52 {
53 
54 };
55 
56 static int opal_i2cm_attach(device_t);
57 static int opal_i2cm_probe(device_t);
58 static const struct ofw_bus_devinfo *
59     opal_i2cm_get_devinfo(device_t, device_t);
60 
61 static device_method_t opal_i2cm_methods[] = {
62 	/* Device interface */
63 	DEVMETHOD(device_probe,		opal_i2cm_probe),
64 	DEVMETHOD(device_attach,	opal_i2cm_attach),
65 
66 	/* ofw_bus interface */
67 	DEVMETHOD(ofw_bus_get_devinfo,	opal_i2cm_get_devinfo),
68 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
69 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
70 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
71 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
72 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
73 
74 	DEVMETHOD_END
75 };
76 
77 static int
78 opal_i2cm_probe(device_t dev)
79 {
80 
81 	if (!(ofw_bus_is_compatible(dev, "ibm,centaur-i2cm") ||
82 	    ofw_bus_is_compatible(dev, "ibm,power8-i2cm")))
83 		return (ENXIO);
84 
85 	device_set_desc(dev, "centaur-i2cm");
86 	return (0);
87 }
88 
89 static int
90 opal_i2cm_attach(device_t dev)
91 {
92 	phandle_t child;
93 	device_t cdev;
94 	struct ofw_bus_devinfo *dinfo;
95 
96 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
97 	    child = OF_peer(child)) {
98 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
99 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
100 			free(dinfo, M_DEVBUF);
101 			continue;
102 		}
103 		cdev = device_add_child(dev, NULL, -1);
104 		if (cdev == NULL) {
105 			device_printf(dev, "<%s>: device_add_child failed\n",
106 			    dinfo->obd_name);
107 			ofw_bus_gen_destroy_devinfo(dinfo);
108 			free(dinfo, M_DEVBUF);
109 			continue;
110 		}
111 		device_set_ivars(cdev, dinfo);
112 	}
113 
114 	return (bus_generic_attach(dev));
115 }
116 
117 static const struct ofw_bus_devinfo *
118 opal_i2cm_get_devinfo(device_t dev, device_t child)
119 {
120         return (device_get_ivars(child));
121 }
122 
123 DEFINE_CLASS_0(opal_i2cm, opal_i2cm_driver, opal_i2cm_methods,
124     sizeof(struct opal_i2cm_softc));
125 DRIVER_MODULE(opal_i2cm, powernv_xscom, opal_i2cm_driver, NULL, NULL);
126 DRIVER_MODULE(opal_i2cm, powernv_centaur, opal_i2cm_driver, NULL, NULL);
127