xref: /freebsd/sys/dev/bhnd/bhndb/bhnd_bhndb.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 
38 #include <dev/bhnd/bhnd_ids.h>
39 #include <dev/bhnd/bhnd.h>
40 
41 #include "bhndbvar.h"
42 
43 /*
44  * bhnd(4) driver mix-in providing a shared common methods for
45  * bhnd devices attached via a bhndb bridge.
46  */
47 
48 static int
49 bhnd_bhndb_read_board_info(device_t dev, device_t child,
50     struct bhnd_board_info *info)
51 {
52 	int	error;
53 
54 	/* Initialize with NVRAM-derived values */
55 	if ((error = bhnd_bus_generic_read_board_info(dev, child, info)))
56 		return (error);
57 
58 	/* Let the bridge fill in any additional data */
59 	return (BHNDB_POPULATE_BOARD_INFO(device_get_parent(dev), dev, info));
60 }
61 
62 static bhnd_attach_type
63 bhnd_bhndb_get_attach_type(device_t dev, device_t child)
64 {
65 	/* It's safe to assume that a bridged device is always an adapter */
66 	return (BHND_ATTACH_ADAPTER);
67 }
68 
69 
70 static bool
71 bhnd_bhndb_is_hw_disabled(device_t dev, device_t child)
72 {
73 	struct bhnd_core_info core = bhnd_get_core_info(child);
74 
75 	/* Delegate to parent bridge */
76 	return (BHNDB_IS_CORE_DISABLED(device_get_parent(dev), dev, &core));
77 }
78 
79 
80 static device_t
81 bhnd_bhndb_find_hostb_device(device_t dev)
82 {
83 	struct bhnd_core_info	 core;
84 	struct bhnd_core_match	 md;
85 	int			 error;
86 
87 	/* Ask the bridge for the hostb core info */
88 	if ((error = BHNDB_GET_HOSTB_CORE(device_get_parent(dev), dev, &core)))
89 		return (NULL);
90 
91 	/* Find the corresponding bus device */
92 	md = bhnd_core_get_match_desc(&core);
93 	return (bhnd_match_child(dev, &md));
94 }
95 
96 static int
97 bhnd_bhndb_assign_intr(device_t dev, device_t child, int rid)
98 {
99 	/* Delegate to parent bridge */
100 	return (BHND_BUS_ASSIGN_INTR(device_get_parent(dev), child, rid));
101 }
102 
103 static bhnd_clksrc
104 bhnd_bhndb_pwrctl_get_clksrc(device_t dev, device_t child,
105 	bhnd_clock clock)
106 {
107 	/* Delegate to parent bridge */
108 	return (BHND_BUS_PWRCTL_GET_CLKSRC(device_get_parent(dev), child,
109 	    clock));
110 }
111 
112 static int
113 bhnd_bhndb_pwrctl_gate_clock(device_t dev, device_t child,
114 	bhnd_clock clock)
115 {
116 	/* Delegate to parent bridge */
117 	return (BHND_BUS_PWRCTL_GATE_CLOCK(device_get_parent(dev), child,
118 	    clock));
119 }
120 
121 static int
122 bhnd_bhndb_pwrctl_ungate_clock(device_t dev, device_t child,
123 	bhnd_clock clock)
124 {
125 	/* Delegate to parent bridge */
126 	return (BHND_BUS_PWRCTL_UNGATE_CLOCK(device_get_parent(dev), child,
127 	    clock));
128 }
129 
130 static device_method_t bhnd_bhndb_methods[] = {
131 	/* BHND interface */
132 	DEVMETHOD(bhnd_bus_get_attach_type,	bhnd_bhndb_get_attach_type),
133 	DEVMETHOD(bhnd_bus_is_hw_disabled,	bhnd_bhndb_is_hw_disabled),
134 	DEVMETHOD(bhnd_bus_find_hostb_device,	bhnd_bhndb_find_hostb_device),
135 	DEVMETHOD(bhnd_bus_read_board_info,	bhnd_bhndb_read_board_info),
136 	DEVMETHOD(bhnd_bus_assign_intr,		bhnd_bhndb_assign_intr),
137 
138 	DEVMETHOD(bhnd_bus_pwrctl_get_clksrc,	bhnd_bhndb_pwrctl_get_clksrc),
139 	DEVMETHOD(bhnd_bus_pwrctl_gate_clock,	bhnd_bhndb_pwrctl_gate_clock),
140 	DEVMETHOD(bhnd_bus_pwrctl_ungate_clock,	bhnd_bhndb_pwrctl_ungate_clock),
141 
142 	DEVMETHOD_END
143 };
144 
145 DEFINE_CLASS_0(bhnd, bhnd_bhndb_driver, bhnd_bhndb_methods, 0);
146