xref: /freebsd/sys/dev/bhnd/bhndb/bhnd_bhndb.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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 
76 static bool
77 bhnd_bhndb_is_hw_disabled(device_t dev, device_t child)
78 {
79 	struct bhnd_core_info core = bhnd_get_core_info(child);
80 
81 	/* Delegate to parent bridge */
82 	return (BHNDB_IS_CORE_DISABLED(device_get_parent(dev), dev, &core));
83 }
84 
85 
86 static device_t
87 bhnd_bhndb_find_hostb_device(device_t dev)
88 {
89 	struct bhnd_core_info	 core;
90 	struct bhnd_core_match	 md;
91 	int			 error;
92 
93 	/* Ask the bridge for the hostb core info */
94 	if ((error = BHNDB_GET_HOSTB_CORE(device_get_parent(dev), dev, &core)))
95 		return (NULL);
96 
97 	/* Find the corresponding bus device */
98 	md = bhnd_core_get_match_desc(&core);
99 	return (bhnd_bus_match_child(dev, &md));
100 }
101 
102 static int
103 bhnd_bhndb_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
104 {
105 	/* Delegate to parent bridge */
106 	return (BHND_BUS_MAP_INTR(device_get_parent(dev), child, intr, irq));
107 }
108 
109 static void
110 bhnd_bhndb_unmap_intr(device_t dev, device_t child, rman_res_t irq)
111 {
112 	/* Delegate to parent bridge */
113 	return (BHND_BUS_UNMAP_INTR(device_get_parent(dev), child, irq));
114 }
115 
116 static bhnd_clksrc
117 bhnd_bhndb_pwrctl_get_clksrc(device_t dev, device_t child,
118 	bhnd_clock clock)
119 {
120 	/* Delegate to parent bridge */
121 	return (BHND_PWRCTL_HOSTB_GET_CLKSRC(device_get_parent(dev), child,
122 	    clock));
123 }
124 
125 static int
126 bhnd_bhndb_pwrctl_gate_clock(device_t dev, device_t child,
127 	bhnd_clock clock)
128 {
129 	/* Delegate to parent bridge */
130 	return (BHND_PWRCTL_HOSTB_GATE_CLOCK(device_get_parent(dev), child,
131 	    clock));
132 }
133 
134 static int
135 bhnd_bhndb_pwrctl_ungate_clock(device_t dev, device_t child,
136 	bhnd_clock clock)
137 {
138 	/* Delegate to parent bridge */
139 	return (BHND_PWRCTL_HOSTB_UNGATE_CLOCK(device_get_parent(dev), child,
140 	    clock));
141 }
142 
143 static int
144 bhnd_bhndb_setup_intr(device_t dev, device_t child, struct resource *irq,
145     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
146     void **cookiep)
147 {
148 	device_t	core, bus;
149 	int		error;
150 
151 	/* Find the actual bus-attached child core */
152 	core = child;
153 	while ((bus = device_get_parent(core)) != NULL) {
154 		if (bus == dev)
155 			break;
156 
157 		core = bus;
158 	}
159 
160 	KASSERT(core != NULL, ("%s is not a child of %s",
161 	    device_get_nameunit(child), device_get_nameunit(dev)));
162 
163 	/* Ask our bridge to enable interrupt routing for the child core */
164 	error = BHNDB_ROUTE_INTERRUPTS(device_get_parent(dev), core);
165 	if (error)
166 		return (error);
167 
168 	/* Delegate actual interrupt setup to the default bhnd bus
169 	 * implementation */
170 	return (bhnd_generic_setup_intr(dev, child, irq, flags, filter, intr,
171 	    arg, cookiep));
172 }
173 
174 static device_method_t bhnd_bhndb_methods[] = {
175 	/* Bus interface */
176 	DEVMETHOD(bus_setup_intr,			bhnd_bhndb_setup_intr),
177 
178 	/* BHND interface */
179 	DEVMETHOD(bhnd_bus_get_attach_type,		bhnd_bhndb_get_attach_type),
180 	DEVMETHOD(bhnd_bus_is_hw_disabled,		bhnd_bhndb_is_hw_disabled),
181 	DEVMETHOD(bhnd_bus_find_hostb_device,		bhnd_bhndb_find_hostb_device),
182 	DEVMETHOD(bhnd_bus_read_board_info,		bhnd_bhndb_read_board_info),
183 	DEVMETHOD(bhnd_bus_map_intr,			bhnd_bhndb_map_intr),
184 	DEVMETHOD(bhnd_bus_unmap_intr,			bhnd_bhndb_unmap_intr),
185 
186 	/* BHND PWRCTL hostb interface */
187 	DEVMETHOD(bhnd_pwrctl_hostb_get_clksrc,		bhnd_bhndb_pwrctl_get_clksrc),
188 	DEVMETHOD(bhnd_pwrctl_hostb_gate_clock,		bhnd_bhndb_pwrctl_gate_clock),
189 	DEVMETHOD(bhnd_pwrctl_hostb_ungate_clock,	bhnd_bhndb_pwrctl_ungate_clock),
190 
191 	DEVMETHOD_END
192 };
193 
194 DEFINE_CLASS_0(bhnd, bhnd_bhndb_driver, bhnd_bhndb_methods, 0);
195