xref: /freebsd/sys/dev/bhnd/bhndb/bhnd_bhndb.c (revision 5c831a5bd61576cacb48b39f8eeb47b92707a355)
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 "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 
74 static bool
75 bhnd_bhndb_is_hw_disabled(device_t dev, device_t child)
76 {
77 	struct bhnd_core_info core = bhnd_get_core_info(child);
78 
79 	/* Delegate to parent bridge */
80 	return (BHNDB_IS_CORE_DISABLED(device_get_parent(dev), dev, &core));
81 }
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_assign_intr(device_t dev, device_t child, int rid)
102 {
103 	/* Delegate to parent bridge */
104 	return (BHND_BUS_ASSIGN_INTR(device_get_parent(dev), child, rid));
105 }
106 
107 static bhnd_clksrc
108 bhnd_bhndb_pwrctl_get_clksrc(device_t dev, device_t child,
109 	bhnd_clock clock)
110 {
111 	/* Delegate to parent bridge */
112 	return (BHND_BUS_PWRCTL_GET_CLKSRC(device_get_parent(dev), child,
113 	    clock));
114 }
115 
116 static int
117 bhnd_bhndb_pwrctl_gate_clock(device_t dev, device_t child,
118 	bhnd_clock clock)
119 {
120 	/* Delegate to parent bridge */
121 	return (BHND_BUS_PWRCTL_GATE_CLOCK(device_get_parent(dev), child,
122 	    clock));
123 }
124 
125 static int
126 bhnd_bhndb_pwrctl_ungate_clock(device_t dev, device_t child,
127 	bhnd_clock clock)
128 {
129 	/* Delegate to parent bridge */
130 	return (BHND_BUS_PWRCTL_UNGATE_CLOCK(device_get_parent(dev), child,
131 	    clock));
132 }
133 
134 static device_method_t bhnd_bhndb_methods[] = {
135 	/* BHND interface */
136 	DEVMETHOD(bhnd_bus_get_attach_type,	bhnd_bhndb_get_attach_type),
137 	DEVMETHOD(bhnd_bus_is_hw_disabled,	bhnd_bhndb_is_hw_disabled),
138 	DEVMETHOD(bhnd_bus_find_hostb_device,	bhnd_bhndb_find_hostb_device),
139 	DEVMETHOD(bhnd_bus_read_board_info,	bhnd_bhndb_read_board_info),
140 	DEVMETHOD(bhnd_bus_assign_intr,		bhnd_bhndb_assign_intr),
141 
142 	DEVMETHOD(bhnd_bus_pwrctl_get_clksrc,	bhnd_bhndb_pwrctl_get_clksrc),
143 	DEVMETHOD(bhnd_bus_pwrctl_gate_clock,	bhnd_bhndb_pwrctl_gate_clock),
144 	DEVMETHOD(bhnd_bus_pwrctl_ungate_clock,	bhnd_bhndb_pwrctl_ungate_clock),
145 
146 	DEVMETHOD_END
147 };
148 
149 DEFINE_CLASS_0(bhnd, bhnd_bhndb_driver, bhnd_bhndb_methods, 0);
150