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