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