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