xref: /freebsd/sys/dev/bhnd/bhnd_subr.c (revision e94f204a324d7dc60476c32be5af474a736c50d4)
1 /*-
2  * Copyright (c) 2015 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/types.h>
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/systm.h>
37 
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40 #include <machine/resource.h>
41 
42 #include <dev/bhnd/cores/chipc/chipcreg.h>
43 
44 #include "bhndreg.h"
45 #include "bhndvar.h"
46 
47 /* BHND core device description table. */
48 static const struct bhnd_core_desc {
49 	uint16_t	 vendor;
50 	uint16_t	 device;
51 	bhnd_devclass_t	 class;
52 	const char	*desc;
53 } bhnd_core_descs[] = {
54 	#define	BHND_CDESC(_mfg, _cid, _cls, _desc)		\
55 	    { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid,	\
56 		BHND_DEVCLASS_ ## _cls, _desc }
57 
58 	BHND_CDESC(BCM, CC,		CC,		"ChipCommon I/O Controller"),
59 	BHND_CDESC(BCM, ILINE20,	OTHER,		"iLine20 HPNA"),
60 	BHND_CDESC(BCM, SRAM,		RAM,		"SRAM"),
61 	BHND_CDESC(BCM, SDRAM,		RAM,		"SDRAM"),
62 	BHND_CDESC(BCM, PCI,		PCI,		"PCI Bridge"),
63 	BHND_CDESC(BCM, MIPS,		CPU,		"MIPS Core"),
64 	BHND_CDESC(BCM, ENET,		ENET_MAC,	"Fast Ethernet MAC"),
65 	BHND_CDESC(BCM, CODEC,		OTHER,		"V.90 Modem Codec"),
66 	BHND_CDESC(BCM, USB,		OTHER,		"USB 1.1 Device/Host Controller"),
67 	BHND_CDESC(BCM, ADSL,		OTHER,		"ADSL Core"),
68 	BHND_CDESC(BCM, ILINE100,	OTHER,		"iLine100 HPNA"),
69 	BHND_CDESC(BCM, IPSEC,		OTHER,		"IPsec Accelerator"),
70 	BHND_CDESC(BCM, UTOPIA,		OTHER,		"UTOPIA ATM Core"),
71 	BHND_CDESC(BCM, PCMCIA,		PCCARD,		"PCMCIA Bridge"),
72 	BHND_CDESC(BCM, SOCRAM,		RAM,		"Internal Memory"),
73 	BHND_CDESC(BCM, MEMC,		MEMC,		"MEMC SDRAM Controller"),
74 	BHND_CDESC(BCM, OFDM,		OTHER,		"OFDM PHY"),
75 	BHND_CDESC(BCM, EXTIF,		OTHER,		"External Interface"),
76 	BHND_CDESC(BCM, D11,		WLAN,		"802.11 MAC/PHY/Radio"),
77 	BHND_CDESC(BCM, APHY,		WLAN_PHY,	"802.11a PHY"),
78 	BHND_CDESC(BCM, BPHY,		WLAN_PHY,	"802.11b PHY"),
79 	BHND_CDESC(BCM, GPHY,		WLAN_PHY,	"802.11g PHY"),
80 	BHND_CDESC(BCM, MIPS33,		CPU,		"MIPS 3302 Core"),
81 	BHND_CDESC(BCM, USB11H,		OTHER,		"USB 1.1 Host Controller"),
82 	BHND_CDESC(BCM, USB11D,		OTHER,		"USB 1.1 Device Core"),
83 	BHND_CDESC(BCM, USB20H,		OTHER,		"USB 2.0 Host Controller"),
84 	BHND_CDESC(BCM, USB20D,		OTHER,		"USB 2.0 Device Core"),
85 	BHND_CDESC(BCM, SDIOH,		OTHER,		"SDIO Host Controller"),
86 	BHND_CDESC(BCM, ROBO,		OTHER,		"RoboSwitch"),
87 	BHND_CDESC(BCM, ATA100,		OTHER,		"Parallel ATA Controller"),
88 	BHND_CDESC(BCM, SATAXOR,	OTHER,		"SATA DMA/XOR Controller"),
89 	BHND_CDESC(BCM, GIGETH,		ENET_MAC,	"Gigabit Ethernet MAC"),
90 	BHND_CDESC(BCM, PCIE,		PCIE,		"PCIe Bridge"),
91 	BHND_CDESC(BCM, NPHY,		WLAN_PHY,	"802.11n 2x2 PHY"),
92 	BHND_CDESC(BCM, SRAMC,		MEMC,		"SRAM Controller"),
93 	BHND_CDESC(BCM, MINIMAC,	OTHER,		"MINI MAC/PHY"),
94 	BHND_CDESC(BCM, ARM11,		CPU,		"ARM1176 CPU"),
95 	BHND_CDESC(BCM, ARM7S,		CPU,		"ARM7TDMI-S CPU"),
96 	BHND_CDESC(BCM, LPPHY,		WLAN_PHY,	"802.11a/b/g PHY"),
97 	BHND_CDESC(BCM, PMU,		PMU,		"PMU"),
98 	BHND_CDESC(BCM, SSNPHY,		WLAN_PHY,	"802.11n Single-Stream PHY"),
99 	BHND_CDESC(BCM, SDIOD,		OTHER,		"SDIO Device Core"),
100 	BHND_CDESC(BCM, ARMCM3,		CPU,		"ARM Cortex-M3 CPU"),
101 	BHND_CDESC(BCM, HTPHY,		WLAN_PHY,	"802.11n 4x4 PHY"),
102 	BHND_CDESC(BCM, MIPS74K,	CPU,		"MIPS74k CPU"),
103 	BHND_CDESC(BCM, GMAC,		ENET_MAC,	"Gigabit MAC core"),
104 	BHND_CDESC(BCM, DMEMC,		MEMC,		"DDR1/DDR2 Memory Controller"),
105 	BHND_CDESC(BCM, PCIERC,		OTHER,		"PCIe Root Complex"),
106 	BHND_CDESC(BCM, OCP,		SOC_BRIDGE,	"OCP to OCP Bridge"),
107 	BHND_CDESC(BCM, SC,		OTHER,		"Shared Common Core"),
108 	BHND_CDESC(BCM, AHB,		SOC_BRIDGE,	"OCP to AHB Bridge"),
109 	BHND_CDESC(BCM, SPIH,		OTHER,		"SPI Host Controller"),
110 	BHND_CDESC(BCM, I2S,		OTHER,		"I2S Digital Audio Interface"),
111 	BHND_CDESC(BCM, DMEMS,		MEMC,		"SDR/DDR1 Memory Controller"),
112 	BHND_CDESC(BCM, UBUS_SHIM,	OTHER,		"BCM6362/UBUS WLAN SHIM"),
113 	BHND_CDESC(BCM, PCIE2,		PCIE,		"PCIe Bridge (Gen2)"),
114 
115 	BHND_CDESC(ARM, APB_BRIDGE,	SOC_BRIDGE,	"BP135 AMBA3 AXI to APB Bridge"),
116 	BHND_CDESC(ARM, PL301,		SOC_ROUTER,	"PL301 AMBA3 Interconnect"),
117 	BHND_CDESC(ARM, EROM,		EROM,		"PL366 Device Enumeration ROM"),
118 	BHND_CDESC(ARM, OOB_ROUTER,	OTHER,		"PL367 OOB Interrupt Router"),
119 	BHND_CDESC(ARM, AXI_UNMAPPED,	OTHER,		"Unmapped Address Ranges"),
120 
121 	BHND_CDESC(BCM, 4706_CC,	CC,		"ChipCommon I/O Controller"),
122 	BHND_CDESC(BCM, NS_PCIE2,	PCIE,		"PCIe Bridge (Gen2)"),
123 	BHND_CDESC(BCM, NS_DMA,		OTHER,		"DMA engine"),
124 	BHND_CDESC(BCM, NS_SDIO,	OTHER,		"SDIO 3.0 Host Controller"),
125 	BHND_CDESC(BCM, NS_USB20H,	OTHER,		"USB 2.0 Host Controller"),
126 	BHND_CDESC(BCM, NS_USB30H,	OTHER,		"USB 3.0 Host Controller"),
127 	BHND_CDESC(BCM, NS_A9JTAG,	OTHER,		"ARM Cortex A9 JTAG Interface"),
128 	BHND_CDESC(BCM, NS_DDR23_MEMC,	MEMC,		"Denali DDR2/DD3 Memory Controller"),
129 	BHND_CDESC(BCM, NS_ROM,		NVRAM,		"System ROM"),
130 	BHND_CDESC(BCM, NS_NAND,	NVRAM,		"NAND Flash Controller"),
131 	BHND_CDESC(BCM, NS_QSPI,	NVRAM,		"QSPI Flash Controller"),
132 	BHND_CDESC(BCM, NS_CC_B,	CC_B,		"ChipCommon B Auxiliary I/O Controller"),
133 	BHND_CDESC(BCM, 4706_SOCRAM,	RAM,		"Internal Memory"),
134 	BHND_CDESC(BCM, IHOST_ARMCA9,	CPU,		"ARM Cortex A9 CPU"),
135 	BHND_CDESC(BCM, 4706_GMAC_CMN,	ENET,		"Gigabit MAC (Common)"),
136 	BHND_CDESC(BCM, 4706_GMAC,	ENET_MAC,	"Gigabit MAC"),
137 	BHND_CDESC(BCM, AMEMC,		MEMC,		"Denali DDR1/DDR2 Memory Controller"),
138 #undef	BHND_CDESC
139 
140 	/* Derived from inspection of the BCM4331 cores that provide PrimeCell
141 	 * IDs. Due to lack of documentation, the surmised device name/purpose
142 	 * provided here may be incorrect. */
143 	{ BHND_MFGID_ARM,	BHND_PRIMEID_EROM,	BHND_DEVCLASS_OTHER,
144 	    "PL364 Device Enumeration ROM" },
145 	{ BHND_MFGID_ARM,	BHND_PRIMEID_SWRAP,	BHND_DEVCLASS_OTHER,
146 	    "PL368 Device Management Interface" },
147 	{ BHND_MFGID_ARM,	BHND_PRIMEID_MWRAP,	BHND_DEVCLASS_OTHER,
148 	    "PL369 Device Management Interface" },
149 
150 	{ 0, 0, 0, NULL }
151 };
152 
153 /**
154  * Return the name for a given JEP106 manufacturer ID.
155  *
156  * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
157  * JEP106 continuation code.
158  */
159 const char *
160 bhnd_vendor_name(uint16_t vendor)
161 {
162 	switch (vendor) {
163 	case BHND_MFGID_ARM:
164 		return "ARM";
165 	case BHND_MFGID_BCM:
166 		return "Broadcom";
167 	case BHND_MFGID_MIPS:
168 		return "MIPS";
169 	default:
170 		return "unknown";
171 	}
172 }
173 
174 /**
175  * Return the name of a port type.
176  */
177 const char *
178 bhnd_port_type_name(bhnd_port_type port_type)
179 {
180 	switch (port_type) {
181 	case BHND_PORT_DEVICE:
182 		return ("device");
183 	case BHND_PORT_BRIDGE:
184 		return ("bridge");
185 	case BHND_PORT_AGENT:
186 		return ("agent");
187 	}
188 }
189 
190 
191 static const struct bhnd_core_desc *
192 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
193 {
194 	for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
195 		if (bhnd_core_descs[i].vendor != vendor)
196 			continue;
197 
198 		if (bhnd_core_descs[i].device != device)
199 			continue;
200 
201 		return (&bhnd_core_descs[i]);
202 	}
203 
204 	return (NULL);
205 }
206 
207 /**
208  * Return a human-readable name for a BHND core.
209  *
210  * @param vendor The core designer's JEDEC-106 Manufacturer ID
211  * @param device The core identifier.
212  */
213 const char *
214 bhnd_find_core_name(uint16_t vendor, uint16_t device)
215 {
216 	const struct bhnd_core_desc *desc;
217 
218 	if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
219 		return ("unknown");
220 
221 	return desc->desc;
222 }
223 
224 /**
225  * Return the device class for a BHND core.
226  *
227  * @param vendor The core designer's JEDEC-106 Manufacturer ID
228  * @param device The core identifier.
229  */
230 bhnd_devclass_t
231 bhnd_find_core_class(uint16_t vendor, uint16_t device)
232 {
233 	const struct bhnd_core_desc *desc;
234 
235 	if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
236 		return (BHND_DEVCLASS_OTHER);
237 
238 	return desc->class;
239 }
240 
241 /**
242  * Return a human-readable name for a BHND core.
243  *
244  * @param ci The core's info record.
245  */
246 const char *
247 bhnd_core_name(const struct bhnd_core_info *ci)
248 {
249 	return bhnd_find_core_name(ci->vendor, ci->device);
250 }
251 
252 /**
253  * Return the device class for a BHND core.
254  *
255  * @param ci The core's info record.
256  */
257 bhnd_devclass_t
258 bhnd_core_class(const struct bhnd_core_info *ci)
259 {
260 	return bhnd_find_core_class(ci->vendor, ci->device);
261 }
262 
263 /**
264  * Initialize a core info record with data from from a bhnd-attached @p dev.
265  *
266  * @param dev A bhnd device.
267  * @param core The record to be initialized.
268  */
269 struct bhnd_core_info
270 bhnd_get_core_info(device_t dev) {
271 	return (struct bhnd_core_info) {
272 		.vendor		= bhnd_get_vendor(dev),
273 		.device		= bhnd_get_device(dev),
274 		.hwrev		= bhnd_get_hwrev(dev),
275 		.core_idx	= bhnd_get_core_index(dev),
276 		.unit		= bhnd_get_core_unit(dev)
277 	};
278 }
279 
280 /**
281  * Find a @p class child device with @p unit on @p dev.
282  *
283  * @param parent The bhnd-compatible bus to be searched.
284  * @param class The device class to match on.
285  * @param unit The device unit number; specify -1 to return the first match
286  * regardless of unit number.
287  *
288  * @retval device_t if a matching child device is found.
289  * @retval NULL if no matching child device is found.
290  */
291 device_t
292 bhnd_find_child(device_t dev, bhnd_devclass_t class, int unit)
293 {
294 	struct bhnd_core_match md = {
295 		.vendor = BHND_MFGID_INVALID,
296 		.device = BHND_COREID_INVALID,
297 		.hwrev.start = BHND_HWREV_INVALID,
298 		.hwrev.end = BHND_HWREV_INVALID,
299 		.class = class,
300 		.unit = unit
301 	};
302 
303 	return bhnd_match_child(dev, &md);
304 }
305 
306 /**
307  * Find the first child device on @p dev that matches @p desc.
308  *
309  * @param parent The bhnd-compatible bus to be searched.
310  * @param desc A match descriptor.
311  *
312  * @retval device_t if a matching child device is found.
313  * @retval NULL if no matching child device is found.
314  */
315 device_t
316 bhnd_match_child(device_t dev, const struct bhnd_core_match *desc)
317 {
318 	device_t	*devlistp;
319 	device_t	 match;
320 	int		 devcnt;
321 	int		 error;
322 
323 	error = device_get_children(dev, &devlistp, &devcnt);
324 	if (error != 0)
325 		return (NULL);
326 
327 	match = NULL;
328 	for (int i = 0; i < devcnt; i++) {
329 		device_t dev = devlistp[i];
330 		if (bhnd_device_matches(dev, desc)) {
331 			match = dev;
332 			goto done;
333 		}
334 	}
335 
336 done:
337 	free(devlistp, M_TEMP);
338 	return match;
339 }
340 
341 /**
342  * Find the first core in @p cores that matches @p desc.
343  *
344  * @param cores The table to search.
345  * @param num_cores The length of @p cores.
346  * @param desc A match descriptor.
347  *
348  * @retval bhnd_core_info if a matching core is found.
349  * @retval NULL if no matching core is found.
350  */
351 const struct bhnd_core_info *
352 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,
353     const struct bhnd_core_match *desc)
354 {
355 	for (u_int i = 0; i < num_cores; i++) {
356 		if (bhnd_core_matches(&cores[i], desc))
357 			return &cores[i];
358 	}
359 
360 	return (NULL);
361 }
362 
363 
364 /**
365  * Find the first core in @p cores with the given @p class.
366  *
367  * @param cores The table to search.
368  * @param num_cores The length of @p cores.
369  * @param desc A match descriptor.
370  *
371  * @retval bhnd_core_info if a matching core is found.
372  * @retval NULL if no matching core is found.
373  */
374 const struct bhnd_core_info *
375 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
376     bhnd_devclass_t class)
377 {
378 	struct bhnd_core_match md = {
379 		.vendor = BHND_MFGID_INVALID,
380 		.device = BHND_COREID_INVALID,
381 		.hwrev.start = BHND_HWREV_INVALID,
382 		.hwrev.end = BHND_HWREV_INVALID,
383 		.class = class,
384 		.unit = -1
385 	};
386 
387 	return bhnd_match_core(cores, num_cores, &md);
388 }
389 
390 /**
391  * Return true if the @p core matches @p desc.
392  *
393  * @param core A bhnd core descriptor.
394  * @param desc A match descriptor to compare against @p core.
395  *
396  * @retval true if @p core matches @p match
397  * @retval false if @p core does not match @p match.
398  */
399 bool
400 bhnd_core_matches(const struct bhnd_core_info *core,
401     const struct bhnd_core_match *desc)
402 {
403 	if (desc->vendor != BHND_MFGID_INVALID &&
404 	    desc->vendor != core->vendor)
405 		return (false);
406 
407 	if (desc->device != BHND_COREID_INVALID &&
408 	    desc->device != core->device)
409 		return (false);
410 
411 	if (desc->unit != -1 && desc->unit != core->unit)
412 		return (false);
413 
414 	if (!bhnd_hwrev_matches(core->hwrev, &desc->hwrev))
415 		return (false);
416 
417 	if (desc->hwrev.end != BHND_HWREV_INVALID &&
418 	    desc->hwrev.end < core->hwrev)
419 		return (false);
420 
421 	if (desc->class != BHND_DEVCLASS_INVALID &&
422 	    desc->class != bhnd_core_class(core))
423 		return (false);
424 
425 	return true;
426 }
427 
428 /**
429  * Return true if the @p hwrev matches @p desc.
430  *
431  * @param hwrev A bhnd hardware revision.
432  * @param desc A match descriptor to compare against @p core.
433  *
434  * @retval true if @p hwrev matches @p match
435  * @retval false if @p hwrev does not match @p match.
436  */
437 bool
438 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
439 {
440 	if (desc->start != BHND_HWREV_INVALID &&
441 	    desc->start > hwrev)
442 		return false;
443 
444 	if (desc->end != BHND_HWREV_INVALID &&
445 	    desc->end < hwrev)
446 		return false;
447 
448 	return true;
449 }
450 
451 /**
452  * Return true if the @p dev matches @p desc.
453  *
454  * @param dev A bhnd device.
455  * @param desc A match descriptor to compare against @p dev.
456  *
457  * @retval true if @p dev matches @p match
458  * @retval false if @p dev does not match @p match.
459  */
460 bool
461 bhnd_device_matches(device_t dev, const struct bhnd_core_match *desc)
462 {
463 	struct bhnd_core_info ci = {
464 		.vendor = bhnd_get_vendor(dev),
465 		.device = bhnd_get_device(dev),
466 		.unit = bhnd_get_core_unit(dev),
467 		.hwrev = bhnd_get_hwrev(dev)
468 	};
469 
470 	return bhnd_core_matches(&ci, desc);
471 }
472 
473 /**
474  * Search @p table for an entry matching @p dev.
475  *
476  * @param dev A bhnd device to match against @p table.
477  * @param table The device table to search.
478  * @param entry_size The @p table entry size, in bytes.
479  *
480  * @retval bhnd_device the first matching device, if any.
481  * @retval NULL if no matching device is found in @p table.
482  */
483 const struct bhnd_device *
484 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
485     size_t entry_size)
486 {
487 	const struct bhnd_device *entry;
488 
489 	for (entry = table; entry->desc != NULL; entry =
490 	    (const struct bhnd_device *) ((const char *) entry + entry_size))
491 	{
492 		/* match core info */
493 		if (!bhnd_device_matches(dev, &entry->core))
494 			continue;
495 
496 		/* match device flags */
497 		if (entry->device_flags & BHND_DF_HOSTB) {
498 			if (!bhnd_is_hostb_device(dev))
499 				continue;
500 		}
501 
502 		/* device found */
503 		return (entry);
504 	}
505 
506 	/* not found */
507 	return (NULL);
508 }
509 
510 /**
511  * Scan @p table for all quirk flags applicable to @p dev.
512  *
513  * @param dev A bhnd device to match against @p table.
514  * @param table The device table to search.
515  * @param entry_size The @p table entry size, in bytes.
516  *
517  * @return returns all matching quirk flags.
518  */
519 uint32_t
520 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
521     size_t entry_size)
522 {
523 	const struct bhnd_device	*dent;
524 	const struct bhnd_device_quirk	*qtable, *qent;
525 	uint32_t			 quirks;
526 	uint16_t			 hwrev;
527 
528 	hwrev = bhnd_get_hwrev(dev);
529 	quirks = 0;
530 
531 	/* Find the quirk table */
532 	if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL) {
533 		/* This is almost certainly a (caller) implementation bug */
534 		device_printf(dev, "quirk lookup did not match any device\n");
535 		return (0);
536 	}
537 
538 	/* Quirks aren't a mandatory field */
539 	if ((qtable = dent->quirks_table) == NULL)
540 		return (0);
541 
542 	/* Collect matching quirk entries */
543 	for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
544 		if (bhnd_hwrev_matches(hwrev, &qent->hwrev))
545 			quirks |= qent->quirks;
546 	}
547 
548 	return (quirks);
549 }
550 
551 
552 /**
553  * Allocate bhnd(4) resources defined in @p rs from a parent bus.
554  *
555  * @param dev The device requesting ownership of the resources.
556  * @param rs A standard bus resource specification. This will be updated
557  * with the allocated resource's RIDs.
558  * @param res On success, the allocated bhnd resources.
559  *
560  * @retval 0 success
561  * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
562  * 		    all allocated resources will be released and a regular
563  * 		    unix error code will be returned.
564  */
565 int
566 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
567     struct bhnd_resource **res)
568 {
569 	/* Initialize output array */
570 	for (u_int i = 0; rs[i].type != -1; i++)
571 		res[i] = NULL;
572 
573 	for (u_int i = 0; rs[i].type != -1; i++) {
574 		res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
575 		    rs[i].flags);
576 
577 		/* Clean up all allocations on failure */
578 		if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
579 			bhnd_release_resources(dev, rs, res);
580 			return (ENXIO);
581 		}
582 	}
583 
584 	return (0);
585 };
586 
587 /**
588  * Release bhnd(4) resources defined in @p rs from a parent bus.
589  *
590  * @param dev The device that owns the resources.
591  * @param rs A standard bus resource specification previously initialized
592  * by @p bhnd_alloc_resources.
593  * @param res The bhnd resources to be released.
594  */
595 void
596 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
597     struct bhnd_resource **res)
598 {
599 	for (u_int i = 0; rs[i].type != -1; i++) {
600 		if (res[i] == NULL)
601 			continue;
602 
603 		bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
604 		res[i] = NULL;
605 	}
606 }
607 
608 /**
609  * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID
610  * register, returning its bhnd_chipid representation.
611  *
612  * @param idreg The CHIPC_ID register value.
613  * @param enum_addr The enumeration address to include in the result.
614  *
615  * @warning
616  * On early siba(4) devices, the ChipCommon core does not provide
617  * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions
618  * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return
619  * an invalid `ncores` value.
620  */
621 struct bhnd_chipid
622 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr)
623 {
624 	struct bhnd_chipid result;
625 
626 	/* Fetch the basic chip info */
627 	result.chip_id = CHIPC_GET_ATTR(idreg, ID_CHIP);
628 	result.chip_pkg = CHIPC_GET_ATTR(idreg, ID_PKG);
629 	result.chip_rev = CHIPC_GET_ATTR(idreg, ID_REV);
630 	result.chip_type = CHIPC_GET_ATTR(idreg, ID_BUS);
631 	result.ncores = CHIPC_GET_ATTR(idreg, ID_NUMCORE);
632 
633 	result.enum_addr = enum_addr;
634 
635 	return (result);
636 }
637 
638 /**
639  * Allocate the resource defined by @p rs via @p dev, use it
640  * to read the ChipCommon ID register relative to @p chipc_offset,
641  * then release the resource.
642  *
643  * @param dev The device owning @p rs.
644  * @param rs A resource spec that encompasses the ChipCommon register block.
645  * @param chipc_offset The offset of the ChipCommon registers within @p rs.
646  * @param[out] result the chip identification data.
647  *
648  * @retval 0 success
649  * @retval non-zero if the ChipCommon identification data could not be read.
650  */
651 int
652 bhnd_read_chipid(device_t dev, struct resource_spec *rs,
653     bus_size_t chipc_offset, struct bhnd_chipid *result)
654 {
655 	struct resource			*res;
656 	uint32_t			 reg;
657 	int				 error, rid, rtype;
658 
659 	/* Allocate the ChipCommon window resource and fetch the chipid data */
660 	rid = rs->rid;
661 	rtype = rs->type;
662 	res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
663 	if (res == NULL) {
664 		device_printf(dev,
665 		    "failed to allocate bhnd chipc resource\n");
666 		return (ENXIO);
667 	}
668 
669 	/* Fetch the basic chip info */
670 	reg = bus_read_4(res, chipc_offset + CHIPC_ID);
671 	*result = bhnd_parse_chipid(reg, 0x0);
672 
673 	/* Fetch the enum base address */
674 	error = 0;
675 	switch (result->chip_type) {
676 	case BHND_CHIPTYPE_SIBA:
677 		result->enum_addr = BHND_DEFAULT_CHIPC_ADDR;
678 		break;
679 	case BHND_CHIPTYPE_BCMA:
680 	case BHND_CHIPTYPE_BCMA_ALT:
681 		result->enum_addr = bus_read_4(res, chipc_offset +
682 		    CHIPC_EROMPTR);
683 		break;
684 	case BHND_CHIPTYPE_UBUS:
685 		device_printf(dev, "unsupported ubus/bcm63xx chip type");
686 		error = ENODEV;
687 		goto cleanup;
688 	default:
689 		device_printf(dev, "unknown chip type %hhu\n",
690 		    result->chip_type);
691 		error = ENODEV;
692 		goto cleanup;
693 	}
694 
695 cleanup:
696 	/* Clean up */
697 	bus_release_resource(dev, rtype, rid, res);
698 	return (error);
699 }
700 
701 /**
702  * Using the bhnd(4) bus-level core information and a custom core name,
703  * populate @p dev's device description.
704  *
705  * @param dev A bhnd-bus attached device.
706  * @param dev_name The core's name (e.g. "SDIO Device Core")
707  */
708 void
709 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
710 {
711 	const char *vendor_name;
712 	char *desc;
713 
714 	vendor_name = bhnd_get_vendor_name(dev);
715 	asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
716 	    bhnd_get_hwrev(dev));
717 
718 	if (desc != NULL) {
719 		device_set_desc_copy(dev, desc);
720 		free(desc, M_BHND);
721 	} else {
722 		device_set_desc(dev, dev_name);
723 	}
724 }
725 
726 /**
727  * Using the bhnd(4) bus-level core information, populate @p dev's device
728  * description.
729  *
730  * @param dev A bhnd-bus attached device.
731  */
732 void
733 bhnd_set_default_core_desc(device_t dev)
734 {
735 	bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
736 }
737 
738 /**
739  * Helper function for implementing BHND_BUS_IS_HOSTB_DEVICE().
740  *
741  * If a parent device is available, this implementation delegates the
742  * request to the BHND_BUS_IS_HOSTB_DEVICE() method on the parent of @p dev.
743  *
744  * If no parent device is available (i.e. on a the bus root), false
745  * is returned.
746  */
747 bool
748 bhnd_bus_generic_is_hostb_device(device_t dev, device_t child) {
749 	if (device_get_parent(dev) != NULL)
750 		return (BHND_BUS_IS_HOSTB_DEVICE(device_get_parent(dev),
751 		    child));
752 
753 	return (false);
754 }
755 
756 /**
757  * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
758  *
759  * If a parent device is available, this implementation delegates the
760  * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
761  *
762  * If no parent device is available (i.e. on a the bus root), the hardware
763  * is assumed to be usable and false is returned.
764  */
765 bool
766 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
767 {
768 	if (device_get_parent(dev) != NULL)
769 		return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
770 
771 	return (false);
772 }
773 
774 /**
775  * Helper function for implementing BHND_BUS_GET_CHIPID().
776  *
777  * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
778  * method on the parent of @p dev. If no parent exists, the implementation
779  * will panic.
780  */
781 const struct bhnd_chipid *
782 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
783 {
784 	if (device_get_parent(dev) != NULL)
785 		return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
786 
787 	panic("missing BHND_BUS_GET_CHIPID()");
788 }
789 
790 /**
791  * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
792  *
793  * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
794  * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
795  * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
796  */
797 struct bhnd_resource *
798 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
799 	int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
800 	u_int flags)
801 {
802 	struct bhnd_resource	*br;
803 	struct resource		*res;
804 	int			 error;
805 
806 	br = NULL;
807 	res = NULL;
808 
809 	/* Allocate the real bus resource (without activating it) */
810 	res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
811 	    (flags & ~RF_ACTIVE));
812 	if (res == NULL)
813 		return (NULL);
814 
815 	/* Allocate our bhnd resource wrapper. */
816 	br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
817 	if (br == NULL)
818 		goto failed;
819 
820 	br->direct = false;
821 	br->res = res;
822 
823 	/* Attempt activation */
824 	if (flags & RF_ACTIVE) {
825 		error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
826 		if (error)
827 			goto failed;
828 	}
829 
830 	return (br);
831 
832 failed:
833 	if (res != NULL)
834 		BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
835 
836 	free(br, M_BHND);
837 	return (NULL);
838 }
839 
840 /**
841  * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
842  *
843  * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
844  * the backing resource to BUS_RELEASE_RESOURCE().
845  */
846 int
847 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
848     int rid, struct bhnd_resource *r)
849 {
850 	int error;
851 
852 	if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
853 		return (error);
854 
855 	free(r, M_BHND);
856 	return (0);
857 }
858 
859 
860 /**
861  * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
862  *
863  * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
864  * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
865  */
866 int
867 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
868     int rid, struct bhnd_resource *r)
869 {
870 	/* Try to delegate to the parent */
871 	if (device_get_parent(dev) != NULL)
872 		return (BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
873 		    child, type, rid, r));
874 
875 	return (EINVAL);
876 };
877 
878 /**
879  * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
880  *
881  * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
882  * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
883  */
884 int
885 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
886     int type, int rid, struct bhnd_resource *r)
887 {
888 	if (device_get_parent(dev) != NULL)
889 		return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
890 		    child, type, rid, r));
891 
892 	return (EINVAL);
893 };
894