xref: /freebsd/sys/dev/bhnd/bhndb/bhnd_bhndb.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * Copyright (c) 2017 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Landon Fuller
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17  *    redistribution must be conditioned upon including a substantially
18  *    similar Disclaimer requirement for further binary redistribution.
19  *
20  * NO WARRANTY
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGES.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/module.h>
41 
42 #include <dev/bhnd/bhnd_ids.h>
43 #include <dev/bhnd/bhnd.h>
44 
45 #include "bhnd_pwrctl_hostb_if.h"
46 
47 #include "bhndbvar.h"
48 
49 /*
50  * bhnd(4) driver mix-in providing a shared common methods for
51  * bhnd devices attached via a bhndb bridge.
52  */
53 
54 static int
55 bhnd_bhndb_read_board_info(device_t dev, device_t child,
56     struct bhnd_board_info *info)
57 {
58 	int	error;
59 
60 	/* Initialize with NVRAM-derived values */
61 	if ((error = bhnd_bus_generic_read_board_info(dev, child, info)))
62 		return (error);
63 
64 	/* Let the bridge fill in any additional data */
65 	return (BHNDB_POPULATE_BOARD_INFO(device_get_parent(dev), dev, info));
66 }
67 
68 static bhnd_attach_type
69 bhnd_bhndb_get_attach_type(device_t dev, device_t child)
70 {
71 	/* It's safe to assume that a bridged device is always an adapter */
72 	return (BHND_ATTACH_ADAPTER);
73 }
74 
75 static bool
76 bhnd_bhndb_is_hw_disabled(device_t dev, device_t child)
77 {
78 	struct bhnd_core_info core = bhnd_get_core_info(child);
79 
80 	/* Delegate to parent bridge */
81 	return (BHNDB_IS_CORE_DISABLED(device_get_parent(dev), dev, &core));
82 }
83 
84 static device_t
85 bhnd_bhndb_find_hostb_device(device_t dev)
86 {
87 	struct bhnd_core_info	 core;
88 	struct bhnd_core_match	 md;
89 	int			 error;
90 
91 	/* Ask the bridge for the hostb core info */
92 	if ((error = BHNDB_GET_HOSTB_CORE(device_get_parent(dev), dev, &core)))
93 		return (NULL);
94 
95 	/* Find the corresponding bus device */
96 	md = bhnd_core_get_match_desc(&core);
97 	return (bhnd_bus_match_child(dev, &md));
98 }
99 
100 static int
101 bhnd_bhndb_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
102 {
103 	/* Delegate to parent bridge */
104 	return (BHND_BUS_MAP_INTR(device_get_parent(dev), child, intr, irq));
105 }
106 
107 static void
108 bhnd_bhndb_unmap_intr(device_t dev, device_t child, rman_res_t irq)
109 {
110 	/* Delegate to parent bridge */
111 	return (BHND_BUS_UNMAP_INTR(device_get_parent(dev), child, irq));
112 }
113 
114 static bhnd_clksrc
115 bhnd_bhndb_pwrctl_get_clksrc(device_t dev, device_t child,
116 	bhnd_clock clock)
117 {
118 	/* Delegate to parent bridge */
119 	return (BHND_PWRCTL_HOSTB_GET_CLKSRC(device_get_parent(dev), child,
120 	    clock));
121 }
122 
123 static int
124 bhnd_bhndb_pwrctl_gate_clock(device_t dev, device_t child,
125 	bhnd_clock clock)
126 {
127 	/* Delegate to parent bridge */
128 	return (BHND_PWRCTL_HOSTB_GATE_CLOCK(device_get_parent(dev), child,
129 	    clock));
130 }
131 
132 static int
133 bhnd_bhndb_pwrctl_ungate_clock(device_t dev, device_t child,
134 	bhnd_clock clock)
135 {
136 	/* Delegate to parent bridge */
137 	return (BHND_PWRCTL_HOSTB_UNGATE_CLOCK(device_get_parent(dev), child,
138 	    clock));
139 }
140 
141 static int
142 bhnd_bhndb_setup_intr(device_t dev, device_t child, struct resource *irq,
143     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
144     void **cookiep)
145 {
146 	device_t	core, bus;
147 	int		error;
148 
149 	/* Find the actual bus-attached child core */
150 	core = child;
151 	while ((bus = device_get_parent(core)) != NULL) {
152 		if (bus == dev)
153 			break;
154 
155 		core = bus;
156 	}
157 
158 	KASSERT(core != NULL, ("%s is not a child of %s",
159 	    device_get_nameunit(child), device_get_nameunit(dev)));
160 
161 	/* Ask our bridge to enable interrupt routing for the child core */
162 	error = BHNDB_ROUTE_INTERRUPTS(device_get_parent(dev), core);
163 	if (error)
164 		return (error);
165 
166 	/* Delegate actual interrupt setup to the default bhnd bus
167 	 * implementation */
168 	return (bhnd_generic_setup_intr(dev, child, irq, flags, filter, intr,
169 	    arg, cookiep));
170 }
171 
172 static device_method_t bhnd_bhndb_methods[] = {
173 	/* Bus interface */
174 	DEVMETHOD(bus_setup_intr,			bhnd_bhndb_setup_intr),
175 
176 	/* BHND interface */
177 	DEVMETHOD(bhnd_bus_get_attach_type,		bhnd_bhndb_get_attach_type),
178 	DEVMETHOD(bhnd_bus_is_hw_disabled,		bhnd_bhndb_is_hw_disabled),
179 	DEVMETHOD(bhnd_bus_find_hostb_device,		bhnd_bhndb_find_hostb_device),
180 	DEVMETHOD(bhnd_bus_read_board_info,		bhnd_bhndb_read_board_info),
181 	DEVMETHOD(bhnd_bus_map_intr,			bhnd_bhndb_map_intr),
182 	DEVMETHOD(bhnd_bus_unmap_intr,			bhnd_bhndb_unmap_intr),
183 
184 	/* BHND PWRCTL hostb interface */
185 	DEVMETHOD(bhnd_pwrctl_hostb_get_clksrc,		bhnd_bhndb_pwrctl_get_clksrc),
186 	DEVMETHOD(bhnd_pwrctl_hostb_gate_clock,		bhnd_bhndb_pwrctl_gate_clock),
187 	DEVMETHOD(bhnd_pwrctl_hostb_ungate_clock,	bhnd_bhndb_pwrctl_ungate_clock),
188 
189 	DEVMETHOD_END
190 };
191 
192 DEFINE_CLASS_0(bhnd, bhnd_bhndb_driver, bhnd_bhndb_methods, 0);
193