xref: /freebsd/sys/dev/bhnd/bhnd_erom_if.m (revision 315ee00f)
1#-
2# Copyright (c) 2016-2017 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# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30#include <sys/param.h>
31#include <sys/bus.h>
32
33#include <machine/bus.h>
34#include <sys/rman.h>
35#include <machine/resource.h>
36
37#include <dev/bhnd/bhnd.h>
38#include <dev/bhnd/bhnd_erom_types.h>
39
40INTERFACE bhnd_erom;
41
42#
43# bhnd(4) device enumeration.
44#
45# Provides a common parser interface to the incompatible device enumeration
46# tables used by bhnd(4) buses.
47#
48
49HEADER {
50	/* forward declarations */
51	struct bhnd_erom_io;
52};
53
54/**
55 * Probe to see if this device enumeration class supports the bhnd bus at
56 * @p addr, returning a standard newbus device probe result (see BUS_PROBE_*)
57 * and the probed chip identification.
58 *
59 * @param	cls		The erom class to probe.
60 * @param	eio		A bus I/O instance, configured with a mapping of
61 *				the first bus core.
62 * @param	base_addr	Address of the first bus core.
63 * @param	hint		Hint used to identify the device. If chipset
64 *				supports standard chip identification registers
65 *				within the first core, this parameter should be
66 *				NULL.
67 * @param[out]	cid		On success, the probed chip identifier.
68 *
69 * @retval 0		if this is the only possible device enumeration
70 *			parser for the probed bus.
71 * @retval negative	if the probe succeeds, a negative value should be
72 *			returned; the parser returning the highest negative
73 *			value will be selected to handle device enumeration.
74 * @retval ENXIO	If the bhnd bus type is not handled by this parser.
75 * @retval positive	if an error occurs during probing, a regular unix error
76 *			code should be returned.
77 */
78STATICMETHOD int probe {
79	bhnd_erom_class_t		*cls;
80	struct bhnd_erom_io		*eio;
81	const struct bhnd_chipid	*hint;
82	struct bhnd_chipid		*cid;
83};
84
85/**
86 * Initialize a device enumeration table parser.
87 *
88 * @param erom		The erom parser to initialize.
89 * @param cid		The device's chip identifier.
90 * @param eio		The bus I/O instance to use when reading the device
91 *			enumeration table. On success, the erom parser assumes
92 *			ownership of this instance.
93 * @retval 0		success
94 * @retval non-zero	if an error occurs initializing the EROM parser,
95 *			a regular unix error code will be returned.
96 */
97METHOD int init {
98	bhnd_erom_t			*erom;
99	const struct bhnd_chipid	*cid;
100	struct bhnd_erom_io		*eio;
101};
102
103/**
104 * Release all resources held by @p erom.
105 *
106 * @param	erom	An erom parser instance previously initialized via
107 *			BHND_EROM_INIT() or BHND_EROM_INIT_STATIC().
108 */
109METHOD void fini {
110	bhnd_erom_t	*erom;
111};
112
113/**
114 * Parse all cores descriptors, returning the array in @p cores and the count
115 * in @p num_cores.
116 *
117 * The memory allocated for the table must be freed via
118 * BHND_EROM_FREE_CORE_TABLE().
119 *
120 * @param	erom		The erom parser to be queried.
121 * @param[out]	cores		The table of parsed core descriptors.
122 * @param[out]	num_cores	The number of core records in @p cores.
123 *
124 * @retval 0		success
125 * @retval non-zero	if an error occurs, a regular unix error code will
126 *			be returned.
127 */
128METHOD int get_core_table {
129	bhnd_erom_t		*erom;
130	struct bhnd_core_info	**cores;
131	u_int			*num_cores;
132};
133
134/**
135 * Free any memory allocated in a previous call to BHND_EROM_GET_CORE_TABLE().
136 *
137 * @param	erom		The erom parser instance.
138 * @param	cores		A core table allocated by @p erom.
139 */
140METHOD void free_core_table {
141	bhnd_erom_t		*erom;
142	struct bhnd_core_info	*cores;
143};
144
145/**
146 * Locate the first core table entry in @p erom that matches @p desc.
147 *
148 * @param	erom	The erom parser to be queried.
149 * @param	desc	A core match descriptor.
150 * @param[out]	core	On success, the matching core info record.
151 *
152 * @retval 0		success
153 * @retval ENOENT	No core matching @p desc was found.
154 * @retval non-zero	Reading or parsing failed.
155 */
156METHOD int lookup_core {
157	bhnd_erom_t			*erom;
158	const struct bhnd_core_match	*desc;
159	struct bhnd_core_info		*core;
160};
161
162/**
163 * Locate the first core table entry in @p erom that matches @p desc,
164 * and return the specified port region's base address and size.
165 *
166 * If a core matching @p desc is not found, or the requested port region
167 * is not mapped to the matching core, ENOENT is returned.
168 *
169 * @param	erom	The erom parser to be queried.
170 * @param	desc	A core match descriptor.
171 * @param	type	The port type to search for.
172 * @param	port	The port to search for.
173 * @param	region	The port region to search for.
174 * @param[out]	core	If not NULL, will be populated with the matched core
175 *			info record on success.
176 * @param[out]	addr	On success, the base address of the port region.
177 * @param[out]	size	On success, the total size of the port region.
178 *
179 * @retval 0		success
180 * @retval ENOENT	No core matching @p desc was found.
181 * @retval ENOENT	No port region matching @p type, @p port, and @p region
182 *			was found.
183 * @retval non-zero	Reading or parsing failed.
184 */
185METHOD int lookup_core_addr {
186	bhnd_erom_t			*erom;
187	const struct bhnd_core_match	*desc;
188	bhnd_port_type			 type;
189	u_int				 port;
190	u_int				 region;
191	struct bhnd_core_info		*core;
192	bhnd_addr_t			*addr;
193	bhnd_size_t			*size;
194};
195
196/**
197 * Enumerate and print all EROM table entries.
198 *
199 * @param	erom	The erom parser to be enumerated.
200 *
201 * @retval 0		success
202 * @retval non-zero	If an error occurs reading the EROM table, a regular
203 *			unix error code will be returned.
204 */
205METHOD int dump {
206	bhnd_erom_t			*erom;
207};
208