xref: /freebsd/sys/dev/bhnd/bcma/bcma_bhndb.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
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 "bcmavar.h"
43 
44 #include "bcma_eromreg.h"
45 #include "bcma_eromvar.h"
46 
47 /*
48  * Supports attachment of bcma(4) bus devices via a bhndb bridge.
49  */
50 
51 static int
52 bcma_bhndb_probe(device_t dev)
53 {
54 	const struct bhnd_chipid	*cid;
55 	int				 error;
56 
57 	/* Defer to default probe implementation */
58 	if ((error = bcma_probe(dev)) > 0)
59 		return (error);
60 
61 	/* Check bus type */
62 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
63 	if (cid->chip_type != BHND_CHIPTYPE_BCMA)
64 		return (ENXIO);
65 
66 	/* Set device description */
67 	bhnd_set_default_bus_desc(dev, cid);
68 
69 	return (error);
70 }
71 
72 static int
73 bcma_bhndb_attach(device_t dev)
74 {
75 	int error;
76 
77 	/* Perform initial attach and enumerate our children. */
78 	if ((error = bcma_attach(dev)))
79 		goto failed;
80 
81 	/* Delegate remainder to standard bhnd method implementation */
82 	if ((error = bhnd_generic_attach(dev)))
83 		goto failed;
84 
85 	return (0);
86 
87 failed:
88 	device_delete_children(dev);
89 	return (error);
90 }
91 
92 static int
93 bcma_bhndb_suspend_child(device_t dev, device_t child)
94 {
95 	struct bcma_devinfo	*dinfo;
96 	int			 error;
97 
98 	if (device_get_parent(child) != dev)
99 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
100 
101 	if (device_is_suspended(child))
102 		return (EBUSY);
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 child's agent resource  */
111 	if (dinfo->res_agent != NULL)
112 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
113 		    SYS_RES_MEMORY, dinfo->res_agent->res);
114 
115 	return (0);
116 }
117 
118 static int
119 bcma_bhndb_resume_child(device_t dev, device_t child)
120 {
121 	struct bcma_devinfo	*dinfo;
122 	int			 error;
123 
124 	if (device_get_parent(child) != dev)
125 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
126 
127 	if (!device_is_suspended(child))
128 		return (EBUSY);
129 
130 	dinfo = device_get_ivars(child);
131 
132 	/* Resume child's agent resource  */
133 	if (dinfo->res_agent != NULL) {
134 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
135 		    SYS_RES_MEMORY, dinfo->res_agent->res);
136 		if (error)
137 			return (error);
138 	}
139 
140 	/* Resume the child */
141 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
142 		/* On failure, re-suspend the agent resource */
143 		if (dinfo->res_agent != NULL) {
144 			BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
145 			    SYS_RES_MEMORY, dinfo->res_agent->res);
146 		}
147 
148 		return (error);
149 	}
150 
151 	return (0);
152 }
153 
154 static device_method_t bcma_bhndb_methods[] = {
155 	/* Device interface */
156 	DEVMETHOD(device_probe,			bcma_bhndb_probe),
157 	DEVMETHOD(device_attach,		bcma_bhndb_attach),
158 
159 	/* Bus interface */
160 	DEVMETHOD(bus_suspend_child,		bcma_bhndb_suspend_child),
161 	DEVMETHOD(bus_resume_child,		bcma_bhndb_resume_child),
162 
163 	DEVMETHOD_END
164 };
165 
166 DEFINE_CLASS_2(bhnd, bcma_bhndb_driver, bcma_bhndb_methods,
167     sizeof(struct bcma_softc), bhnd_bhndb_driver, bcma_driver);
168 
169 DRIVER_MODULE(bcma_bhndb, bhndb, bcma_bhndb_driver, bhnd_devclass, NULL, NULL);
170 
171 MODULE_VERSION(bcma_bhndb, 1);
172 MODULE_DEPEND(bcma_bhndb, bcma, 1, 1, 1);
173 MODULE_DEPEND(bcma_bhndb, bhnd, 1, 1, 1);
174 MODULE_DEPEND(bcma_bhndb, bhndb, 1, 1, 1);
175