bhnd_subr.c revision 298278
174462Salfred/*-
274462Salfred * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3261057Smav * All rights reserved.
4261057Smav *
5261057Smav * Redistribution and use in source and binary forms, with or without
68870Srgrimes * modification, are permitted provided that the following conditions
7261057Smav * are met:
8261057Smav * 1. Redistributions of source code must retain the above copyright
9261057Smav *    notice, this list of conditions and the following disclaimer,
10261057Smav *    without modification.
11261057Smav * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12261057Smav *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13261057Smav *    redistribution must be conditioned upon including a substantially
14261057Smav *    similar Disclaimer requirement for further binary redistribution.
15261057Smav *
16261057Smav * NO WARRANTY
17261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18261057Smav * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19261057Smav * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20261057Smav * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21261057Smav * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22261057Smav * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25261057Smav * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27261057Smav * THE POSSIBILITY OF SUCH DAMAGES.
28261057Smav */
291901Swollman
301901Swollman#include <sys/cdefs.h>
311901Swollman__FBSDID("$FreeBSD: head/sys/dev/bhnd/bhnd_subr.c 298278 2016-04-19 15:56:39Z adrian $");
32136581Sobrien
3374462Salfred#include <sys/types.h>
341901Swollman#include <sys/bus.h>
3592990Sobrien#include <sys/systm.h>
3692990Sobrien
371901Swollman#include <machine/bus.h>
381901Swollman#include <sys/rman.h>
391901Swollman#include <machine/resource.h>
401901Swollman
411901Swollman#include <dev/bhnd/cores/chipc/chipcreg.h>
421901Swollman
431901Swollman#include "bhndreg.h"
441901Swollman#include "bhndvar.h"
4574462Salfred
4674462Salfred/* BHND core device description table. */
4774462Salfredstatic const struct bhnd_core_desc {
481901Swollman	uint16_t	 vendor;
491901Swollman	uint16_t	 device;
501901Swollman	bhnd_devclass_t	 class;
5174462Salfred	const char	*desc;
521901Swollman} bhnd_core_descs[] = {
531901Swollman	#define	BHND_CDESC(_mfg, _cid, _cls, _desc)		\
541901Swollman	    { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid,	\
551901Swollman		BHND_DEVCLASS_ ## _cls, _desc }
561901Swollman
571901Swollman	BHND_CDESC(BCM, CC,		CC,		"ChipCommon I/O Controller"),
581901Swollman	BHND_CDESC(BCM, ILINE20,	OTHER,		"iLine20 HPNA"),
591901Swollman	BHND_CDESC(BCM, SRAM,		RAM,		"SRAM"),
6074462Salfred	BHND_CDESC(BCM, SDRAM,		RAM,		"SDRAM"),
6174462Salfred	BHND_CDESC(BCM, PCI,		PCI,		"PCI Bridge"),
6274462Salfred	BHND_CDESC(BCM, MIPS,		CPU,		"MIPS Core"),
638870Srgrimes	BHND_CDESC(BCM, ENET,		ENET_MAC,	"Fast Ethernet MAC"),
648870Srgrimes	BHND_CDESC(BCM, CODEC,		OTHER,		"V.90 Modem Codec"),
651901Swollman	BHND_CDESC(BCM, USB,		OTHER,		"USB 1.1 Device/Host Controller"),
661901Swollman	BHND_CDESC(BCM, ADSL,		OTHER,		"ADSL Core"),
671901Swollman	BHND_CDESC(BCM, ILINE100,	OTHER,		"iLine100 HPNA"),
681901Swollman	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 */
158const char *
159bhnd_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 */
176const char *
177bhnd_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
190static const struct bhnd_core_desc *
191bhnd_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 */
212const char *
213bhnd_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 */
229bhnd_devclass_t
230bhnd_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 */
245const char *
246bhnd_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 */
256bhnd_devclass_t
257bhnd_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 */
268struct bhnd_core_info
269bhnd_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 */
290device_t
291bhnd_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 */
314device_t
315bhnd_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
335done:
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 */
350const struct bhnd_core_info *
351bhnd_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 */
373const struct bhnd_core_info *
374bhnd_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 */
398bool
399bhnd_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 */
436bool
437bhnd_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 */
459bool
460bhnd_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 */
482const struct bhnd_device *
483bhnd_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 */
518uint32_t
519bhnd_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 */
564int
565bhnd_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 */
594void
595bhnd_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 */
620struct bhnd_chipid
621bhnd_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 */
650int
651bhnd_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
694cleanup:
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 */
707void
708bhnd_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 */
731void
732bhnd_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 */
746bool
747bhnd_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 */
764bool
765bhnd_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 */
780const struct bhnd_chipid *
781bhnd_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 */
796struct bhnd_resource *
797bhnd_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
831failed:
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 */
845int
846bhnd_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 */
865int
866bhnd_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 */
883int
884bhnd_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};