xref: /freebsd/sys/dev/bhnd/bhnd_erom.h (revision 2ff63af9)
1664a7497SLandon J. Fuller /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
36e778a7eSPedro F. Giffuni  *
489294a78SLandon J. Fuller  * Copyright (c) 2015-2017 Landon Fuller <landonf@FreeBSD.org>
589294a78SLandon J. Fuller  * Copyright (c) 2017 The FreeBSD Foundation
6664a7497SLandon J. Fuller  * All rights reserved.
7664a7497SLandon J. Fuller  *
889294a78SLandon J. Fuller  * Portions of this software were developed by Landon Fuller
989294a78SLandon J. Fuller  * under sponsorship from the FreeBSD Foundation.
1089294a78SLandon J. Fuller  *
11664a7497SLandon J. Fuller  * Redistribution and use in source and binary forms, with or without
12664a7497SLandon J. Fuller  * modification, are permitted provided that the following conditions
13664a7497SLandon J. Fuller  * are met:
14664a7497SLandon J. Fuller  * 1. Redistributions of source code must retain the above copyright
15664a7497SLandon J. Fuller  *    notice, this list of conditions and the following disclaimer,
16664a7497SLandon J. Fuller  *    without modification.
17664a7497SLandon J. Fuller  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18664a7497SLandon J. Fuller  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
19664a7497SLandon J. Fuller  *    redistribution must be conditioned upon including a substantially
20664a7497SLandon J. Fuller  *    similar Disclaimer requirement for further binary redistribution.
21664a7497SLandon J. Fuller  *
22664a7497SLandon J. Fuller  * NO WARRANTY
23664a7497SLandon J. Fuller  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24664a7497SLandon J. Fuller  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25664a7497SLandon J. Fuller  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26664a7497SLandon J. Fuller  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27664a7497SLandon J. Fuller  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28664a7497SLandon J. Fuller  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29664a7497SLandon J. Fuller  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30664a7497SLandon J. Fuller  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31664a7497SLandon J. Fuller  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32664a7497SLandon J. Fuller  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33664a7497SLandon J. Fuller  * THE POSSIBILITY OF SUCH DAMAGES.
34664a7497SLandon J. Fuller  *
35664a7497SLandon J. Fuller  */
36664a7497SLandon J. Fuller 
37664a7497SLandon J. Fuller #ifndef _BHND_EROM_BHND_EROM_H_
38664a7497SLandon J. Fuller #define _BHND_EROM_BHND_EROM_H_
39664a7497SLandon J. Fuller 
40664a7497SLandon J. Fuller #include <sys/param.h>
41664a7497SLandon J. Fuller #include <sys/kobj.h>
42664a7497SLandon J. Fuller #include <sys/linker_set.h>
43664a7497SLandon J. Fuller 
44664a7497SLandon J. Fuller #include <dev/bhnd/bhnd.h>
45664a7497SLandon J. Fuller #include <dev/bhnd/bhnd_erom_types.h>
46664a7497SLandon J. Fuller 
47664a7497SLandon J. Fuller #include "bhnd_erom_if.h"
48664a7497SLandon J. Fuller 
4989294a78SLandon J. Fuller /* forward declarations */
5089294a78SLandon J. Fuller struct bhnd_erom_io;
5189294a78SLandon J. Fuller struct bhnd_erom_iobus;
5289294a78SLandon J. Fuller 
5389294a78SLandon J. Fuller bhnd_erom_class_t	*bhnd_erom_probe_driver_classes(devclass_t bus_devclass,
5489294a78SLandon J. Fuller 			     struct bhnd_erom_io *eio,
5589294a78SLandon J. Fuller 			     const struct bhnd_chipid *hint,
5689294a78SLandon J. Fuller 			     struct bhnd_chipid *cid);
5789294a78SLandon J. Fuller 
58664a7497SLandon J. Fuller bhnd_erom_t		*bhnd_erom_alloc(bhnd_erom_class_t *cls,
59111d7cb2SLandon J. Fuller 			     const struct bhnd_chipid *cid,
6089294a78SLandon J. Fuller 			     struct bhnd_erom_io *eio);
61664a7497SLandon J. Fuller 
62664a7497SLandon J. Fuller int			 bhnd_erom_init_static(bhnd_erom_class_t *cls,
63664a7497SLandon J. Fuller 			     bhnd_erom_t *erom, size_t esize,
64111d7cb2SLandon J. Fuller 			     const struct bhnd_chipid *cid,
6589294a78SLandon J. Fuller 			     struct bhnd_erom_io *eio);
66664a7497SLandon J. Fuller 
67664a7497SLandon J. Fuller void			 bhnd_erom_fini_static(bhnd_erom_t *erom);
68664a7497SLandon J. Fuller 
69664a7497SLandon J. Fuller void			 bhnd_erom_free(bhnd_erom_t *erom);
70664a7497SLandon J. Fuller 
7189294a78SLandon J. Fuller struct bhnd_erom_io	*bhnd_erom_iores_new(device_t dev, int rid);
7289294a78SLandon J. Fuller int			 bhnd_erom_iobus_init(struct bhnd_erom_iobus *iobus,
7389294a78SLandon J. Fuller 			     bhnd_addr_t addr, bhnd_size_t size,
7489294a78SLandon J. Fuller 			     bus_space_tag_t bst, bus_space_handle_t bsh);
7589294a78SLandon J. Fuller 
7689294a78SLandon J. Fuller int			 bhnd_erom_io_map(struct bhnd_erom_io *eio,
7789294a78SLandon J. Fuller 			     bhnd_addr_t addr, bhnd_size_t size);
78f3524ec8SLandon J. Fuller int			 bhnd_erom_io_tell(struct bhnd_erom_io *eio,
79f3524ec8SLandon J. Fuller 			     bhnd_addr_t *addr, bhnd_size_t *size);
8089294a78SLandon J. Fuller uint32_t		 bhnd_erom_io_read(struct bhnd_erom_io *eio,
8189294a78SLandon J. Fuller 			     bhnd_size_t offset, u_int width);
8289294a78SLandon J. Fuller void			 bhnd_erom_io_fini(struct bhnd_erom_io *eio);
8389294a78SLandon J. Fuller 
84664a7497SLandon J. Fuller /**
85664a7497SLandon J. Fuller  * Abstract bhnd_erom instance state. Must be first member of all subclass
86664a7497SLandon J. Fuller  * instances.
87664a7497SLandon J. Fuller  */
88664a7497SLandon J. Fuller struct bhnd_erom {
89664a7497SLandon J. Fuller 	KOBJ_FIELDS;
90664a7497SLandon J. Fuller };
91664a7497SLandon J. Fuller 
92664a7497SLandon J. Fuller /** Number of additional bytes to reserve for statically allocated
93664a7497SLandon J. Fuller  *  bhnd_erom instances. */
94664a7497SLandon J. Fuller #define	BHND_EROM_STATIC_BYTES	64
95664a7497SLandon J. Fuller 
96664a7497SLandon J. Fuller /**
97664a7497SLandon J. Fuller  * A bhnd_erom instance structure large enough to statically allocate
98664a7497SLandon J. Fuller  * any known bhnd_erom subclass.
99664a7497SLandon J. Fuller  *
100664a7497SLandon J. Fuller  * The maximum size of subclasses is verified statically in
101664a7497SLandon J. Fuller  * BHND_EROM_DEFINE_CLASS(), and at runtime in bhnd_erom_init_static().
102664a7497SLandon J. Fuller  */
103664a7497SLandon J. Fuller struct bhnd_erom_static {
104664a7497SLandon J. Fuller 	struct bhnd_erom	obj;
105664a7497SLandon J. Fuller 	uint8_t			idata[BHND_EROM_STATIC_BYTES];
106664a7497SLandon J. Fuller };
107664a7497SLandon J. Fuller 
108664a7497SLandon J. Fuller /** Registered EROM parser class instances. */
109664a7497SLandon J. Fuller SET_DECLARE(bhnd_erom_class_set, bhnd_erom_class_t);
110664a7497SLandon J. Fuller 
111664a7497SLandon J. Fuller #define	BHND_EROM_DEFINE_CLASS(name, classvar, methods, size)	\
112664a7497SLandon J. Fuller 	DEFINE_CLASS_0(name, classvar, methods, size);		\
113664a7497SLandon J. Fuller 	BHND_EROM_CLASS_DEF(classvar);				\
114664a7497SLandon J. Fuller 	_Static_assert(size <= sizeof(struct bhnd_erom_static),	\
115664a7497SLandon J. Fuller 	    "cannot statically allocate instance data; "	\
116664a7497SLandon J. Fuller 	        "increase BHND_EROM_STATIC_BYTES");
117664a7497SLandon J. Fuller 
118664a7497SLandon J. Fuller #define	BHND_EROM_CLASS_DEF(classvar)	DATA_SET(bhnd_erom_class_set, classvar)
119664a7497SLandon J. Fuller 
120664a7497SLandon J. Fuller /**
121664a7497SLandon J. Fuller  * Probe to see if this device enumeration class supports the bhnd bus
12289294a78SLandon J. Fuller  * mapped by @p eio, returning a standard newbus device probe result
12389294a78SLandon J. Fuller  * (see BUS_PROBE_*) and the probed chip identification.
124111d7cb2SLandon J. Fuller  *
125111d7cb2SLandon J. Fuller  * @param	cls	The erom class to probe.
12689294a78SLandon J. Fuller  * @param	eio	A bus I/O instance, configured with a mapping of the
12789294a78SLandon J. Fuller  *			first bus core.
12889294a78SLandon J. Fuller  * @param	hint	Identification hint used to identify the device.
12989294a78SLandon J. Fuller  *			If chipset supports standard chip identification
13089294a78SLandon J. Fuller  *			registers within the first core, this parameter should
13189294a78SLandon J. Fuller  *			be NULL.
132111d7cb2SLandon J. Fuller  * @param[out]	cid	On success, the probed chip identifier.
133111d7cb2SLandon J. Fuller  *
134111d7cb2SLandon J. Fuller  * @retval 0		if this is the only possible device enumeration
135111d7cb2SLandon J. Fuller  *			parser for the probed bus.
136111d7cb2SLandon J. Fuller  * @retval negative	if the probe succeeds, a negative value should be
137111d7cb2SLandon J. Fuller  *			returned; the parser returning the highest negative
138111d7cb2SLandon J. Fuller  *			value will be selected to handle device enumeration.
139111d7cb2SLandon J. Fuller  * @retval ENXIO	If the bhnd bus type is not handled by this parser.
140111d7cb2SLandon J. Fuller  * @retval positive	if an error occurs during probing, a regular unix error
141111d7cb2SLandon J. Fuller  *			code should be returned.
142111d7cb2SLandon J. Fuller  */
143111d7cb2SLandon J. Fuller static inline int
bhnd_erom_probe(bhnd_erom_class_t * cls,struct bhnd_erom_io * eio,const struct bhnd_chipid * hint,struct bhnd_chipid * cid)14489294a78SLandon J. Fuller bhnd_erom_probe(bhnd_erom_class_t *cls, struct bhnd_erom_io *eio,
14589294a78SLandon J. Fuller     const struct bhnd_chipid *hint, struct bhnd_chipid *cid)
146111d7cb2SLandon J. Fuller {
14789294a78SLandon J. Fuller 	return (BHND_EROM_PROBE(cls, eio, hint, cid));
148664a7497SLandon J. Fuller }
149664a7497SLandon J. Fuller 
150664a7497SLandon J. Fuller /**
151664a7497SLandon J. Fuller  * Parse all cores descriptors in @p erom, returning the array in @p cores and
152664a7497SLandon J. Fuller  * the count in @p num_cores.
153664a7497SLandon J. Fuller  *
154664a7497SLandon J. Fuller  * The memory allocated for the table must be freed via
155664a7497SLandon J. Fuller  * bhnd_erom_free_core_table().
156664a7497SLandon J. Fuller  *
157664a7497SLandon J. Fuller  * @param	erom		The erom parser to be queried.
158664a7497SLandon J. Fuller  * @param[out]	cores		The table of parsed core descriptors.
159664a7497SLandon J. Fuller  * @param[out]	num_cores	The number of core records in @p cores.
160664a7497SLandon J. Fuller  *
161664a7497SLandon J. Fuller  * @retval 0		success
162664a7497SLandon J. Fuller  * @retval non-zero	if an error occurs, a regular unix error code will
163664a7497SLandon J. Fuller  *			be returned.
164664a7497SLandon J. Fuller  */
165664a7497SLandon J. Fuller static inline int
bhnd_erom_get_core_table(bhnd_erom_t * erom,struct bhnd_core_info ** cores,u_int * num_cores)166664a7497SLandon J. Fuller bhnd_erom_get_core_table(bhnd_erom_t *erom, struct bhnd_core_info **cores,
167664a7497SLandon J. Fuller     u_int *num_cores)
168664a7497SLandon J. Fuller {
169664a7497SLandon J. Fuller 	return (BHND_EROM_GET_CORE_TABLE(erom, cores, num_cores));
170664a7497SLandon J. Fuller }
171664a7497SLandon J. Fuller 
172664a7497SLandon J. Fuller /**
173664a7497SLandon J. Fuller  * Free any memory allocated in a previous call to BHND_EROM_GET_CORE_TABLE().
174664a7497SLandon J. Fuller  *
175664a7497SLandon J. Fuller  * @param	erom		The erom parser instance.
176664a7497SLandon J. Fuller  * @param	cores		A core table allocated by @p erom.
177664a7497SLandon J. Fuller  */
178664a7497SLandon J. Fuller static inline void
bhnd_erom_free_core_table(bhnd_erom_t * erom,struct bhnd_core_info * cores)179664a7497SLandon J. Fuller bhnd_erom_free_core_table(bhnd_erom_t *erom, struct bhnd_core_info *cores)
180664a7497SLandon J. Fuller {
181664a7497SLandon J. Fuller 	return (BHND_EROM_FREE_CORE_TABLE(erom, cores));
182664a7497SLandon J. Fuller };
183664a7497SLandon J. Fuller 
184664a7497SLandon J. Fuller /**
185664a7497SLandon J. Fuller  * Locate the first core table entry in @p erom that matches @p desc.
186664a7497SLandon J. Fuller  *
187664a7497SLandon J. Fuller  * @param	erom	The erom parser to be queried.
188664a7497SLandon J. Fuller  * @param	desc	A core match descriptor.
189664a7497SLandon J. Fuller  * @param[out]	core	On success, the matching core info record.
190664a7497SLandon J. Fuller  *
191664a7497SLandon J. Fuller  * @retval 0		success
192664a7497SLandon J. Fuller  * @retval ENOENT	No core matching @p desc was found.
193664a7497SLandon J. Fuller  * @retval non-zero	Reading or parsing failed.
194664a7497SLandon J. Fuller  */
195664a7497SLandon J. Fuller static inline int
bhnd_erom_lookup_core(bhnd_erom_t * erom,const struct bhnd_core_match * desc,struct bhnd_core_info * core)196664a7497SLandon J. Fuller bhnd_erom_lookup_core(bhnd_erom_t *erom, const struct bhnd_core_match *desc,
197664a7497SLandon J. Fuller     struct bhnd_core_info *core)
198664a7497SLandon J. Fuller {
199664a7497SLandon J. Fuller 	return (BHND_EROM_LOOKUP_CORE(erom, desc, core));
200664a7497SLandon J. Fuller }
201664a7497SLandon J. Fuller 
202664a7497SLandon J. Fuller /**
203664a7497SLandon J. Fuller  * Locate the first core table entry in @p erom that matches @p desc,
204664a7497SLandon J. Fuller  * and return the specified port region's base address and size.
205664a7497SLandon J. Fuller  *
206664a7497SLandon J. Fuller  * If a core matching @p desc is not found, or the requested port region
207664a7497SLandon J. Fuller  * is not mapped to the matching core, ENOENT is returned.
208664a7497SLandon J. Fuller  *
209664a7497SLandon J. Fuller  * @param	erom	The erom parser to be queried.
210664a7497SLandon J. Fuller  * @param	desc	A core match descriptor.
211664a7497SLandon J. Fuller  * @param	type	The port type to search for.
212664a7497SLandon J. Fuller  * @param	port	The port to search for.
213664a7497SLandon J. Fuller  * @param	region	The port region to search for.
214664a7497SLandon J. Fuller  * @param[out]	core	If not NULL, will be populated with the matched core
215664a7497SLandon J. Fuller  *			info record on success.
216664a7497SLandon J. Fuller  * @param[out]	addr	On success, the base address of the port region.
217664a7497SLandon J. Fuller  * @param[out]	size	On success, the total size of the port region.
218664a7497SLandon J. Fuller  *
219664a7497SLandon J. Fuller  * @retval 0		success
220664a7497SLandon J. Fuller  * @retval ENOENT	No core matching @p desc was found.
221664a7497SLandon J. Fuller  * @retval ENOENT	No port region matching @p type, @p port, and @p region
222664a7497SLandon J. Fuller  *			was found.
223664a7497SLandon J. Fuller  * @retval non-zero	Reading or parsing failed.
224664a7497SLandon J. Fuller  */
225664a7497SLandon J. Fuller static inline int
bhnd_erom_lookup_core_addr(bhnd_erom_t * erom,const struct bhnd_core_match * desc,bhnd_port_type type,u_int port,u_int region,struct bhnd_core_info * core,bhnd_addr_t * addr,bhnd_size_t * size)226664a7497SLandon J. Fuller bhnd_erom_lookup_core_addr(bhnd_erom_t *erom, const struct bhnd_core_match *desc,
227664a7497SLandon J. Fuller     bhnd_port_type type, u_int port, u_int region, struct bhnd_core_info *core,
228664a7497SLandon J. Fuller     bhnd_addr_t *addr, bhnd_size_t *size)
229664a7497SLandon J. Fuller {
230664a7497SLandon J. Fuller 	return (BHND_EROM_LOOKUP_CORE_ADDR(erom, desc, type, port, region,
231664a7497SLandon J. Fuller 	    core, addr, size));
232664a7497SLandon J. Fuller };
233664a7497SLandon J. Fuller 
234eb23aa80SLandon J. Fuller /**
235eb23aa80SLandon J. Fuller  * Enumerate and print all entries in @p erom.
236eb23aa80SLandon J. Fuller  *
237eb23aa80SLandon J. Fuller  * @param	erom	The erom parser to be enumerated.
238eb23aa80SLandon J. Fuller  *
239eb23aa80SLandon J. Fuller  * @retval 0		success
240eb23aa80SLandon J. Fuller  * @retval non-zero	If an error occurs parsing the EROM table, a regular
241eb23aa80SLandon J. Fuller  *			unix error code will be returned.
242eb23aa80SLandon J. Fuller  */
243eb23aa80SLandon J. Fuller static inline int
bhnd_erom_dump(bhnd_erom_t * erom)244eb23aa80SLandon J. Fuller bhnd_erom_dump(bhnd_erom_t *erom)
245eb23aa80SLandon J. Fuller {
246eb23aa80SLandon J. Fuller 	return (BHND_EROM_DUMP(erom));
247eb23aa80SLandon J. Fuller }
248eb23aa80SLandon J. Fuller 
249664a7497SLandon J. Fuller #endif /* _BHND_EROM_BHND_EROM_H_ */
250