xref: /freebsd/sys/dev/bhnd/bhndb/bhnd_bhndb.c (revision 3494f7c0)
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/param.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/module.h>
38 
39 #include <dev/bhnd/bhnd_ids.h>
40 #include <dev/bhnd/bhnd.h>
41 
42 #include "bhnd_pwrctl_hostb_if.h"
43 
44 #include "bhndbvar.h"
45 
46 /*
47  * bhnd(4) driver mix-in providing a shared common methods for
48  * bhnd devices attached via a bhndb bridge.
49  */
50 
51 static int
52 bhnd_bhndb_read_board_info(device_t dev, device_t child,
53     struct bhnd_board_info *info)
54 {
55 	int	error;
56 
57 	/* Initialize with NVRAM-derived values */
58 	if ((error = bhnd_bus_generic_read_board_info(dev, child, info)))
59 		return (error);
60 
61 	/* Let the bridge fill in any additional data */
62 	return (BHNDB_POPULATE_BOARD_INFO(device_get_parent(dev), dev, info));
63 }
64 
65 static bhnd_attach_type
66 bhnd_bhndb_get_attach_type(device_t dev, device_t child)
67 {
68 	/* It's safe to assume that a bridged device is always an adapter */
69 	return (BHND_ATTACH_ADAPTER);
70 }
71 
72 static bool
73 bhnd_bhndb_is_hw_disabled(device_t dev, device_t child)
74 {
75 	struct bhnd_core_info core = bhnd_get_core_info(child);
76 
77 	/* Delegate to parent bridge */
78 	return (BHNDB_IS_CORE_DISABLED(device_get_parent(dev), dev, &core));
79 }
80 
81 static device_t
82 bhnd_bhndb_find_hostb_device(device_t dev)
83 {
84 	struct bhnd_core_info	 core;
85 	struct bhnd_core_match	 md;
86 	int			 error;
87 
88 	/* Ask the bridge for the hostb core info */
89 	if ((error = BHNDB_GET_HOSTB_CORE(device_get_parent(dev), dev, &core)))
90 		return (NULL);
91 
92 	/* Find the corresponding bus device */
93 	md = bhnd_core_get_match_desc(&core);
94 	return (bhnd_bus_match_child(dev, &md));
95 }
96 
97 static int
98 bhnd_bhndb_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
99 {
100 	/* Delegate to parent bridge */
101 	return (BHND_BUS_MAP_INTR(device_get_parent(dev), child, intr, irq));
102 }
103 
104 static void
105 bhnd_bhndb_unmap_intr(device_t dev, device_t child, rman_res_t irq)
106 {
107 	/* Delegate to parent bridge */
108 	return (BHND_BUS_UNMAP_INTR(device_get_parent(dev), child, irq));
109 }
110 
111 static bhnd_clksrc
112 bhnd_bhndb_pwrctl_get_clksrc(device_t dev, device_t child,
113 	bhnd_clock clock)
114 {
115 	/* Delegate to parent bridge */
116 	return (BHND_PWRCTL_HOSTB_GET_CLKSRC(device_get_parent(dev), child,
117 	    clock));
118 }
119 
120 static int
121 bhnd_bhndb_pwrctl_gate_clock(device_t dev, device_t child,
122 	bhnd_clock clock)
123 {
124 	/* Delegate to parent bridge */
125 	return (BHND_PWRCTL_HOSTB_GATE_CLOCK(device_get_parent(dev), child,
126 	    clock));
127 }
128 
129 static int
130 bhnd_bhndb_pwrctl_ungate_clock(device_t dev, device_t child,
131 	bhnd_clock clock)
132 {
133 	/* Delegate to parent bridge */
134 	return (BHND_PWRCTL_HOSTB_UNGATE_CLOCK(device_get_parent(dev), child,
135 	    clock));
136 }
137 
138 static int
139 bhnd_bhndb_setup_intr(device_t dev, device_t child, struct resource *irq,
140     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
141     void **cookiep)
142 {
143 	device_t	core, bus;
144 	int		error;
145 
146 	/* Find the actual bus-attached child core */
147 	core = child;
148 	while ((bus = device_get_parent(core)) != NULL) {
149 		if (bus == dev)
150 			break;
151 
152 		core = bus;
153 	}
154 
155 	KASSERT(core != NULL, ("%s is not a child of %s",
156 	    device_get_nameunit(child), device_get_nameunit(dev)));
157 
158 	/* Ask our bridge to enable interrupt routing for the child core */
159 	error = BHNDB_ROUTE_INTERRUPTS(device_get_parent(dev), core);
160 	if (error)
161 		return (error);
162 
163 	/* Delegate actual interrupt setup to the default bhnd bus
164 	 * implementation */
165 	return (bhnd_generic_setup_intr(dev, child, irq, flags, filter, intr,
166 	    arg, cookiep));
167 }
168 
169 static device_method_t bhnd_bhndb_methods[] = {
170 	/* Bus interface */
171 	DEVMETHOD(bus_setup_intr,			bhnd_bhndb_setup_intr),
172 
173 	/* BHND interface */
174 	DEVMETHOD(bhnd_bus_get_attach_type,		bhnd_bhndb_get_attach_type),
175 	DEVMETHOD(bhnd_bus_is_hw_disabled,		bhnd_bhndb_is_hw_disabled),
176 	DEVMETHOD(bhnd_bus_find_hostb_device,		bhnd_bhndb_find_hostb_device),
177 	DEVMETHOD(bhnd_bus_read_board_info,		bhnd_bhndb_read_board_info),
178 	DEVMETHOD(bhnd_bus_map_intr,			bhnd_bhndb_map_intr),
179 	DEVMETHOD(bhnd_bus_unmap_intr,			bhnd_bhndb_unmap_intr),
180 
181 	/* BHND PWRCTL hostb interface */
182 	DEVMETHOD(bhnd_pwrctl_hostb_get_clksrc,		bhnd_bhndb_pwrctl_get_clksrc),
183 	DEVMETHOD(bhnd_pwrctl_hostb_gate_clock,		bhnd_bhndb_pwrctl_gate_clock),
184 	DEVMETHOD(bhnd_pwrctl_hostb_ungate_clock,	bhnd_bhndb_pwrctl_ungate_clock),
185 
186 	DEVMETHOD_END
187 };
188 
189 DEFINE_CLASS_0(bhnd, bhnd_bhndb_driver, bhnd_bhndb_methods, 0);
190