xref: /freebsd/sys/dev/bhnd/bcma/bcma_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 "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 
56 	/* Check bus type */
57 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
58 	if (cid->chip_type != BHND_CHIPTYPE_BCMA)
59 		return (ENXIO);
60 
61 	/* Delegate to default probe implementation */
62 	return (bcma_probe(dev));
63 }
64 
65 static int
66 bcma_bhndb_attach(device_t dev)
67 {
68 	const struct bhnd_chipid	*cid;
69 	struct resource			*erom_res;
70 	int				 error;
71 	int				 rid;
72 
73 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
74 
75 	/* Map the EROM resource and enumerate our children. */
76 	rid = 0;
77 	erom_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, cid->enum_addr,
78 		cid->enum_addr + BCMA_EROM_TABLE_SIZE, BCMA_EROM_TABLE_SIZE,
79 		RF_ACTIVE);
80 	if (erom_res == NULL) {
81 		device_printf(dev, "failed to allocate EROM resource\n");
82 		return (ENXIO);
83 	}
84 
85 	error = bcma_add_children(dev, erom_res, BCMA_EROM_TABLE_START);
86 
87 	/* Clean up */
88 	bus_release_resource(dev, SYS_RES_MEMORY, rid, erom_res);
89 	if (error)
90 		return (error);
91 
92 	/* Initialize full bridge configuration */
93 	error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev,
94 	    bhndb_bcma_priority_table);
95 	if (error)
96 		return (error);
97 
98 	/* Call our superclass' implementation */
99 	return (bcma_attach(dev));
100 }
101 
102 static int
103 bcma_bhndb_suspend_child(device_t dev, device_t child)
104 {
105 	struct bcma_devinfo	*dinfo;
106 	int			 error;
107 
108 	if (device_get_parent(child) != dev)
109 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
110 
111 	if (device_is_suspended(child))
112 		return (EBUSY);
113 
114 	dinfo = device_get_ivars(child);
115 
116 	/* Suspend the child */
117 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
118 		return (error);
119 
120 	/* Suspend child's agent resource  */
121 	if (dinfo->res_agent != NULL)
122 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
123 		    SYS_RES_MEMORY, dinfo->res_agent->res);
124 
125 	return (0);
126 }
127 
128 static int
129 bcma_bhndb_resume_child(device_t dev, device_t child)
130 {
131 	struct bcma_devinfo	*dinfo;
132 	int			 error;
133 
134 	if (device_get_parent(child) != dev)
135 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
136 
137 	if (!device_is_suspended(child))
138 		return (EBUSY);
139 
140 	dinfo = device_get_ivars(child);
141 
142 	/* Resume child's agent resource  */
143 	if (dinfo->res_agent != NULL) {
144 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
145 		    SYS_RES_MEMORY, dinfo->res_agent->res);
146 		if (error)
147 			return (error);
148 	}
149 
150 	/* Resume the child */
151 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
152 		/* On failure, re-suspend the agent resource */
153 		if (dinfo->res_agent != NULL) {
154 			BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
155 			    SYS_RES_MEMORY, dinfo->res_agent->res);
156 		}
157 
158 		return (error);
159 	}
160 
161 	return (0);
162 }
163 
164 static device_method_t bcma_bhndb_methods[] = {
165 	/* Device interface */
166 	DEVMETHOD(device_probe,			bcma_bhndb_probe),
167 	DEVMETHOD(device_attach,		bcma_bhndb_attach),
168 
169 	/* Bus interface */
170 	DEVMETHOD(bus_suspend_child,		bcma_bhndb_suspend_child),
171 	DEVMETHOD(bus_resume_child,		bcma_bhndb_resume_child),
172 
173 	DEVMETHOD_END
174 };
175 
176 DEFINE_CLASS_1(bhnd, bcma_bhndb_driver, bcma_bhndb_methods,
177     sizeof(struct bcma_softc), bcma_driver);
178 
179 DRIVER_MODULE(bcma_bhndb, bhndb, bcma_bhndb_driver, bhnd_devclass, NULL, NULL);
180 
181 MODULE_VERSION(bcma_bhndb, 1);
182 MODULE_DEPEND(bcma_bhndb, bcma, 1, 1, 1);
183 MODULE_DEPEND(bcma_bhndb, bhndb, 1, 1, 1);
184