xref: /freebsd/sys/dev/bhnd/siba/siba_bhndb.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 
38 #include <dev/bhnd/bhnd_ids.h>
39 #include <dev/bhnd/bhndb/bhndbvar.h>
40 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
41 
42 #include "sibavar.h"
43 
44 /*
45  * Supports attachment of siba(4) bus devices via a bhndb bridge.
46  */
47 
48 static int
49 siba_bhndb_probe(device_t dev)
50 {
51 	const struct bhnd_chipid *cid;
52 
53 	/* Check bus type */
54 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
55 	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
56 		return (ENXIO);
57 
58 	/* Delegate to default probe implementation */
59 	return (siba_probe(dev));
60 }
61 
62 static int
63 siba_bhndb_attach(device_t dev)
64 {
65 	const struct bhnd_chipid	*chipid;
66 	int				 error;
67 
68 	/* Enumerate our children. */
69 	chipid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
70 	if ((error = siba_add_children(dev, chipid)))
71 		return (error);
72 
73 	/* Initialize full bridge configuration */
74 	error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev,
75 	    bhndb_siba_priority_table);
76 	if (error)
77 		return (error);
78 
79 	/* Call our superclass' implementation */
80 	return (siba_attach(dev));
81 }
82 
83 /* Suspend all references to the device's cfg register blocks */
84 static void
85 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
86 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
87 		if (dinfo->cfg[i] == NULL)
88 			continue;
89 
90 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
91 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
92 	}
93 }
94 
95 static int
96 siba_bhndb_suspend_child(device_t dev, device_t child)
97 {
98 	struct siba_devinfo	*dinfo;
99 	int			 error;
100 
101 	if (device_get_parent(child) != dev)
102 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
103 
104 	dinfo = device_get_ivars(child);
105 
106 	/* Suspend the child */
107 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
108 		return (error);
109 
110 	/* Suspend resource references to the child's config registers */
111 	siba_bhndb_suspend_cfgblocks(dev, dinfo);
112 
113 	return (0);
114 }
115 
116 static int
117 siba_bhndb_resume_child(device_t dev, device_t child)
118 {
119 	struct siba_devinfo	*dinfo;
120 	int			 error;
121 
122 	if (device_get_parent(child) != dev)
123 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
124 
125 	if (!device_is_suspended(child))
126 		return (EBUSY);
127 
128 	dinfo = device_get_ivars(child);
129 
130 	/* Resume all resource references to the child's config registers */
131 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
132 		if (dinfo->cfg[i] == NULL)
133 			continue;
134 
135 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
136 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
137 		if (error) {
138 			siba_bhndb_suspend_cfgblocks(dev, dinfo);
139 			return (error);
140 		}
141 	}
142 
143 	/* Resume the child */
144 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
145 		siba_bhndb_suspend_cfgblocks(dev, dinfo);
146 		return (error);
147 	}
148 
149 	return (0);
150 }
151 
152 static device_method_t siba_bhndb_methods[] = {
153 	/* Device interface */
154 	DEVMETHOD(device_probe,			siba_bhndb_probe),
155 	DEVMETHOD(device_attach,		siba_bhndb_attach),
156 
157 	/* Bus interface */
158 	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
159 	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
160 
161 	DEVMETHOD_END
162 };
163 
164 DEFINE_CLASS_1(bhnd, siba_bhndb_driver, siba_bhndb_methods,
165     sizeof(struct siba_softc), siba_driver);
166 
167 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
168 
169 MODULE_VERSION(siba_bhndb, 1);
170 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
171 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);