xref: /freebsd/sys/dev/bhnd/cores/chipc/chipc.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5  * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com>
6  * Copyright (c) 2017 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Landon Fuller
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
20  *    redistribution must be conditioned upon including a substantially
21  *    similar Disclaimer requirement for further binary redistribution.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  */
36 
37 #include <sys/cdefs.h>
38 /*
39  * Broadcom ChipCommon driver.
40  *
41  * With the exception of some very early chipsets, the ChipCommon core
42  * has been included in all HND SoCs and chipsets based on the siba(4)
43  * and bcma(4) interconnects, providing a common interface to chipset
44  * identification, bus enumeration, UARTs, clocks, watchdog interrupts,
45  * GPIO, flash, etc.
46  */
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/bus.h>
52 #include <sys/rman.h>
53 #include <sys/malloc.h>
54 #include <sys/module.h>
55 #include <sys/mutex.h>
56 #include <sys/systm.h>
57 
58 #include <machine/bus.h>
59 #include <machine/resource.h>
60 
61 #include <dev/bhnd/bhnd.h>
62 #include <dev/bhnd/bhndvar.h>
63 
64 #include "chipcreg.h"
65 #include "chipcvar.h"
66 
67 #include "chipc_private.h"
68 
69 static struct bhnd_device_quirk chipc_quirks[];
70 
71 /* Supported device identifiers */
72 static const struct bhnd_device chipc_devices[] = {
73 	BHND_DEVICE(BCM, CC, NULL, chipc_quirks),
74 	BHND_DEVICE(BCM, 4706_CC, NULL, chipc_quirks),
75 	BHND_DEVICE_END
76 };
77 
78 /* Device quirks table */
79 static struct bhnd_device_quirk chipc_quirks[] = {
80 	/* HND OTP controller revisions */
81 	BHND_CORE_QUIRK	(HWREV_EQ (12),		CHIPC_QUIRK_OTP_HND), /* (?) */
82 	BHND_CORE_QUIRK	(HWREV_EQ (17),		CHIPC_QUIRK_OTP_HND), /* BCM4311 */
83 	BHND_CORE_QUIRK	(HWREV_EQ (22),		CHIPC_QUIRK_OTP_HND), /* BCM4312 */
84 
85 	/* IPX OTP controller revisions */
86 	BHND_CORE_QUIRK	(HWREV_EQ (21),		CHIPC_QUIRK_OTP_IPX),
87 	BHND_CORE_QUIRK	(HWREV_GTE(23),		CHIPC_QUIRK_OTP_IPX),
88 
89 	BHND_CORE_QUIRK	(HWREV_GTE(32),		CHIPC_QUIRK_SUPPORTS_SPROM),
90 	BHND_CORE_QUIRK	(HWREV_GTE(35),		CHIPC_QUIRK_SUPPORTS_CAP_EXT),
91 	BHND_CORE_QUIRK	(HWREV_GTE(49),		CHIPC_QUIRK_IPX_OTPL_SIZE),
92 
93 	/* 4706 variant quirks */
94 	BHND_CORE_QUIRK	(HWREV_EQ (38),		CHIPC_QUIRK_4706_NFLASH), /* BCM5357? */
95 	BHND_CHIP_QUIRK	(4706,	HWREV_ANY,	CHIPC_QUIRK_4706_NFLASH),
96 
97 	/* 4331 quirks*/
98 	BHND_CHIP_QUIRK	(4331,	HWREV_ANY,	CHIPC_QUIRK_4331_EXTPA_MUX_SPROM),
99 	BHND_PKG_QUIRK	(4331,	TN,		CHIPC_QUIRK_4331_GPIO2_5_MUX_SPROM),
100 	BHND_PKG_QUIRK	(4331,	TNA0,		CHIPC_QUIRK_4331_GPIO2_5_MUX_SPROM),
101 	BHND_PKG_QUIRK	(4331,	TT,		CHIPC_QUIRK_4331_EXTPA2_MUX_SPROM),
102 
103 	/* 4360 quirks */
104 	BHND_CHIP_QUIRK	(4352,	HWREV_LTE(2),	CHIPC_QUIRK_4360_FEM_MUX_SPROM),
105 	BHND_CHIP_QUIRK	(43460,	HWREV_LTE(2),	CHIPC_QUIRK_4360_FEM_MUX_SPROM),
106 	BHND_CHIP_QUIRK	(43462,	HWREV_LTE(2),	CHIPC_QUIRK_4360_FEM_MUX_SPROM),
107 	BHND_CHIP_QUIRK	(43602,	HWREV_LTE(2),	CHIPC_QUIRK_4360_FEM_MUX_SPROM),
108 
109 	BHND_DEVICE_QUIRK_END
110 };
111 
112 static int		 chipc_add_children(struct chipc_softc *sc);
113 
114 static bhnd_nvram_src	 chipc_find_nvram_src(struct chipc_softc *sc,
115 			     struct chipc_caps *caps);
116 static int		 chipc_read_caps(struct chipc_softc *sc,
117 			     struct chipc_caps *caps);
118 
119 static bool		 chipc_should_enable_muxed_sprom(
120 			     struct chipc_softc *sc);
121 static int		 chipc_enable_otp_power(struct chipc_softc *sc);
122 static void		 chipc_disable_otp_power(struct chipc_softc *sc);
123 static int		 chipc_enable_sprom_pins(struct chipc_softc *sc);
124 static void		 chipc_disable_sprom_pins(struct chipc_softc *sc);
125 
126 static int		 chipc_try_activate_resource(struct chipc_softc *sc,
127 			     device_t child, int type, int rid,
128 			     struct resource *r, bool req_direct);
129 
130 static int		 chipc_init_rman(struct chipc_softc *sc);
131 static void		 chipc_free_rman(struct chipc_softc *sc);
132 static struct rman	*chipc_get_rman(struct chipc_softc *sc, int type);
133 
134 /* quirk and capability flag convenience macros */
135 #define	CHIPC_QUIRK(_sc, _name)	\
136     ((_sc)->quirks & CHIPC_QUIRK_ ## _name)
137 
138 #define CHIPC_CAP(_sc, _name)	\
139     ((_sc)->caps._name)
140 
141 #define	CHIPC_ASSERT_QUIRK(_sc, name)	\
142     KASSERT(CHIPC_QUIRK((_sc), name), ("quirk " __STRING(_name) " not set"))
143 
144 #define	CHIPC_ASSERT_CAP(_sc, name)	\
145     KASSERT(CHIPC_CAP((_sc), name), ("capability " __STRING(_name) " not set"))
146 
147 static int
148 chipc_probe(device_t dev)
149 {
150 	const struct bhnd_device *id;
151 
152 	id = bhnd_device_lookup(dev, chipc_devices, sizeof(chipc_devices[0]));
153 	if (id == NULL)
154 		return (ENXIO);
155 
156 	bhnd_set_default_core_desc(dev);
157 	return (BUS_PROBE_DEFAULT);
158 }
159 
160 static int
161 chipc_attach(device_t dev)
162 {
163 	struct chipc_softc		*sc;
164 	int				 error;
165 
166 	sc = device_get_softc(dev);
167 	sc->dev = dev;
168 	sc->quirks = bhnd_device_quirks(dev, chipc_devices,
169 	    sizeof(chipc_devices[0]));
170 	sc->sprom_refcnt = 0;
171 
172 	CHIPC_LOCK_INIT(sc);
173 	STAILQ_INIT(&sc->mem_regions);
174 
175 	/* Set up resource management */
176 	if ((error = chipc_init_rman(sc))) {
177 		device_printf(sc->dev,
178 		    "failed to initialize chipc resource state: %d\n", error);
179 		goto failed;
180 	}
181 
182 	/* Allocate the region containing the chipc register block */
183 	if ((sc->core_region = chipc_find_region_by_rid(sc, 0)) == NULL) {
184 		error = ENXIO;
185 		goto failed;
186 	}
187 
188 	error = chipc_retain_region(sc, sc->core_region,
189 	    RF_ALLOCATED|RF_ACTIVE);
190 	if (error) {
191 		sc->core_region = NULL;
192 		goto failed;
193 	}
194 
195 	/* Save a direct reference to our chipc registers */
196 	sc->core = sc->core_region->cr_res;
197 
198 	/* Fetch and parse capability register(s) */
199 	if ((error = chipc_read_caps(sc, &sc->caps)))
200 		goto failed;
201 
202 	if (bootverbose)
203 		chipc_print_caps(sc->dev, &sc->caps);
204 
205 	/* Attach all supported child devices */
206 	if ((error = chipc_add_children(sc)))
207 		goto failed;
208 
209 	/*
210 	 * Register ourselves with the bus; we're fully initialized and can
211 	 * response to ChipCommin API requests.
212 	 *
213 	 * Since our children may need access to ChipCommon, this must be done
214 	 * before attaching our children below (via bus_generic_attach).
215 	 */
216 	if ((error = bhnd_register_provider(dev, BHND_SERVICE_CHIPC)))
217 		goto failed;
218 
219 	if ((error = bus_generic_attach(dev)))
220 		goto failed;
221 
222 	return (0);
223 
224 failed:
225 	device_delete_children(sc->dev);
226 
227 	if (sc->core_region != NULL) {
228 		chipc_release_region(sc, sc->core_region,
229 		    RF_ALLOCATED|RF_ACTIVE);
230 	}
231 
232 	chipc_free_rman(sc);
233 	CHIPC_LOCK_DESTROY(sc);
234 	return (error);
235 }
236 
237 static int
238 chipc_detach(device_t dev)
239 {
240 	struct chipc_softc	*sc;
241 	int			 error;
242 
243 	sc = device_get_softc(dev);
244 
245 	if ((error = bus_generic_detach(dev)))
246 		return (error);
247 
248 	if ((error = device_delete_children(dev)))
249 		return (error);
250 
251 	if ((error = bhnd_deregister_provider(dev, BHND_SERVICE_ANY)))
252 		return (error);
253 
254 	chipc_release_region(sc, sc->core_region, RF_ALLOCATED|RF_ACTIVE);
255 	chipc_free_rman(sc);
256 
257 	CHIPC_LOCK_DESTROY(sc);
258 
259 	return (0);
260 }
261 
262 static int
263 chipc_add_children(struct chipc_softc *sc)
264 {
265 	device_t	 child;
266 	const char	*flash_bus;
267 	int		 error;
268 
269 	/* SPROM/OTP */
270 	if (sc->caps.nvram_src == BHND_NVRAM_SRC_SPROM ||
271 	    sc->caps.nvram_src == BHND_NVRAM_SRC_OTP)
272 	{
273 		child = BUS_ADD_CHILD(sc->dev, 0, "bhnd_nvram", -1);
274 		if (child == NULL) {
275 			device_printf(sc->dev, "failed to add nvram device\n");
276 			return (ENXIO);
277 		}
278 
279 		/* Both OTP and external SPROM are mapped at CHIPC_SPROM_OTP */
280 		error = chipc_set_mem_resource(sc, child, 0, CHIPC_SPROM_OTP,
281 		    CHIPC_SPROM_OTP_SIZE, 0, 0);
282 		if (error) {
283 			device_printf(sc->dev, "failed to set OTP memory "
284 			    "resource: %d\n", error);
285 			return (error);
286 		}
287 	}
288 
289 	/*
290 	 * PMU/PWR_CTRL
291 	 *
292 	 * On AOB ("Always on Bus") devices, the PMU core (if it exists) is
293 	 * attached directly to the bhnd(4) bus -- not chipc.
294 	 */
295 	if (sc->caps.pmu && !sc->caps.aob) {
296 		child = BUS_ADD_CHILD(sc->dev, 0, "bhnd_pmu", -1);
297 		if (child == NULL) {
298 			device_printf(sc->dev, "failed to add pmu\n");
299 			return (ENXIO);
300 		}
301 	} else if (sc->caps.pwr_ctrl) {
302 		child = BUS_ADD_CHILD(sc->dev, 0, "bhnd_pwrctl", -1);
303 		if (child == NULL) {
304 			device_printf(sc->dev, "failed to add pwrctl\n");
305 			return (ENXIO);
306 		}
307 	}
308 
309 	/* GPIO */
310 	child = BUS_ADD_CHILD(sc->dev, 0, "gpio", -1);
311 	if (child == NULL) {
312 		device_printf(sc->dev, "failed to add gpio\n");
313 		return (ENXIO);
314 	}
315 
316 	error = chipc_set_mem_resource(sc, child, 0, 0, RM_MAX_END, 0, 0);
317 	if (error) {
318 		device_printf(sc->dev, "failed to set gpio memory resource: "
319 		    "%d\n", error);
320 		return (error);
321 	}
322 
323 	/* All remaining devices are SoC-only */
324 	if (bhnd_get_attach_type(sc->dev) != BHND_ATTACH_NATIVE)
325 		return (0);
326 
327 	/* UARTs */
328 	for (u_int i = 0; i < min(sc->caps.num_uarts, CHIPC_UART_MAX); i++) {
329 		int irq_rid, mem_rid;
330 
331 		irq_rid = 0;
332 		mem_rid = 0;
333 
334 		child = BUS_ADD_CHILD(sc->dev, 0, "uart", -1);
335 		if (child == NULL) {
336 			device_printf(sc->dev, "failed to add uart%u\n", i);
337 			return (ENXIO);
338 		}
339 
340 		/* Shared IRQ */
341 		error = chipc_set_irq_resource(sc, child, irq_rid, 0);
342 		if (error) {
343 			device_printf(sc->dev, "failed to set uart%u irq %u\n",
344 			    i, 0);
345 			return (error);
346 		}
347 
348 		/* UART registers are mapped sequentially */
349 		error = chipc_set_mem_resource(sc, child, mem_rid,
350 		    CHIPC_UART(i), CHIPC_UART_SIZE, 0, 0);
351 		if (error) {
352 			device_printf(sc->dev, "failed to set uart%u memory "
353 			    "resource: %d\n", i, error);
354 			return (error);
355 		}
356 	}
357 
358 	/* Flash */
359 	flash_bus = chipc_flash_bus_name(sc->caps.flash_type);
360 	if (flash_bus != NULL) {
361 		int rid;
362 
363 		child = BUS_ADD_CHILD(sc->dev, 0, flash_bus, -1);
364 		if (child == NULL) {
365 			device_printf(sc->dev, "failed to add %s device\n",
366 			    flash_bus);
367 			return (ENXIO);
368 		}
369 
370 		/* flash memory mapping */
371 		rid = 0;
372 		error = chipc_set_mem_resource(sc, child, rid, 0, RM_MAX_END, 1,
373 		    1);
374 		if (error) {
375 			device_printf(sc->dev, "failed to set flash memory "
376 			    "resource %d: %d\n", rid, error);
377 			return (error);
378 		}
379 
380 		/* flashctrl registers */
381 		rid++;
382 		error = chipc_set_mem_resource(sc, child, rid,
383 		    CHIPC_SFLASH_BASE, CHIPC_SFLASH_SIZE, 0, 0);
384 		if (error) {
385 			device_printf(sc->dev, "failed to set flash memory "
386 			    "resource %d: %d\n", rid, error);
387 			return (error);
388 		}
389 	}
390 
391 	return (0);
392 }
393 
394 /**
395  * Determine the NVRAM data source for this device.
396  *
397  * The SPROM, OTP, and flash capability flags must be fully populated in
398  * @p caps.
399  *
400  * @param sc chipc driver state.
401  * @param caps capability flags to be used to derive NVRAM configuration.
402  */
403 static bhnd_nvram_src
404 chipc_find_nvram_src(struct chipc_softc *sc, struct chipc_caps *caps)
405 {
406 	uint32_t		 otp_st, srom_ctrl;
407 
408 	/*
409 	 * We check for hardware presence in order of precedence. For example,
410 	 * SPROM is always used in preference to internal OTP if found.
411 	 */
412 	if (CHIPC_QUIRK(sc, SUPPORTS_SPROM) && caps->sprom) {
413 		srom_ctrl = bhnd_bus_read_4(sc->core, CHIPC_SPROM_CTRL);
414 		if (srom_ctrl & CHIPC_SRC_PRESENT)
415 			return (BHND_NVRAM_SRC_SPROM);
416 	}
417 
418 	/* Check for programmed OTP H/W subregion (contains SROM data) */
419 	if (CHIPC_QUIRK(sc, SUPPORTS_OTP) && caps->otp_size > 0) {
420 		/* TODO: need access to HND-OTP device */
421 		if (!CHIPC_QUIRK(sc, OTP_HND)) {
422 			device_printf(sc->dev,
423 			    "NVRAM unavailable: unsupported OTP controller.\n");
424 			return (BHND_NVRAM_SRC_UNKNOWN);
425 		}
426 
427 		otp_st = bhnd_bus_read_4(sc->core, CHIPC_OTPST);
428 		if (otp_st & CHIPC_OTPS_GUP_HW)
429 			return (BHND_NVRAM_SRC_OTP);
430 	}
431 
432 	/* Check for flash */
433 	if (caps->flash_type != CHIPC_FLASH_NONE)
434 		return (BHND_NVRAM_SRC_FLASH);
435 
436 	/* No NVRAM hardware capability declared */
437 	return (BHND_NVRAM_SRC_UNKNOWN);
438 }
439 
440 /* Read and parse chipc capabilities */
441 static int
442 chipc_read_caps(struct chipc_softc *sc, struct chipc_caps *caps)
443 {
444 	uint32_t	cap_reg;
445 	uint32_t	cap_ext_reg;
446 	uint32_t	regval;
447 
448 	/* Fetch cap registers */
449 	cap_reg = bhnd_bus_read_4(sc->core, CHIPC_CAPABILITIES);
450 	cap_ext_reg = 0;
451 	if (CHIPC_QUIRK(sc, SUPPORTS_CAP_EXT))
452 		cap_ext_reg = bhnd_bus_read_4(sc->core, CHIPC_CAPABILITIES_EXT);
453 
454 	/* Extract values */
455 	caps->num_uarts		= CHIPC_GET_BITS(cap_reg, CHIPC_CAP_NUM_UART);
456 	caps->mipseb		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_MIPSEB);
457 	caps->uart_gpio		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_UARTGPIO);
458 	caps->uart_clock	= CHIPC_GET_BITS(cap_reg, CHIPC_CAP_UCLKSEL);
459 
460 	caps->extbus_type	= CHIPC_GET_BITS(cap_reg, CHIPC_CAP_EXTBUS);
461 	caps->pwr_ctrl		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_PWR_CTL);
462 	caps->jtag_master	= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_JTAGP);
463 
464 	caps->pll_type		= CHIPC_GET_BITS(cap_reg, CHIPC_CAP_PLL);
465 	caps->backplane_64	= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_BKPLN64);
466 	caps->boot_rom		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_ROM);
467 	caps->pmu		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_PMU);
468 	caps->eci		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_ECI);
469 	caps->sprom		= CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_SPROM);
470 	caps->otp_size		= CHIPC_GET_BITS(cap_reg, CHIPC_CAP_OTP_SIZE);
471 
472 	caps->seci		= CHIPC_GET_FLAG(cap_ext_reg, CHIPC_CAP2_SECI);
473 	caps->gsio		= CHIPC_GET_FLAG(cap_ext_reg, CHIPC_CAP2_GSIO);
474 	caps->aob		= CHIPC_GET_FLAG(cap_ext_reg, CHIPC_CAP2_AOB);
475 
476 	/* Fetch OTP size for later IPX controller revisions */
477 	if (CHIPC_QUIRK(sc, IPX_OTPL_SIZE)) {
478 		regval = bhnd_bus_read_4(sc->core, CHIPC_OTPLAYOUT);
479 		caps->otp_size = CHIPC_GET_BITS(regval, CHIPC_OTPL_SIZE);
480 	}
481 
482 	/* Determine flash type and parameters */
483 	caps->cfi_width = 0;
484 	switch (CHIPC_GET_BITS(cap_reg, CHIPC_CAP_FLASH)) {
485 	case CHIPC_CAP_SFLASH_ST:
486 		caps->flash_type = CHIPC_SFLASH_ST;
487 		break;
488 	case CHIPC_CAP_SFLASH_AT:
489 		caps->flash_type = CHIPC_SFLASH_AT;
490 		break;
491 	case CHIPC_CAP_NFLASH:
492 		/* unimplemented */
493 		caps->flash_type = CHIPC_NFLASH;
494 		break;
495 	case CHIPC_CAP_PFLASH:
496 		caps->flash_type = CHIPC_PFLASH_CFI;
497 
498 		/* determine cfi width */
499 		regval = bhnd_bus_read_4(sc->core, CHIPC_FLASH_CFG);
500 		if (CHIPC_GET_FLAG(regval, CHIPC_FLASH_CFG_DS))
501 			caps->cfi_width = 2;
502 		else
503 			caps->cfi_width = 1;
504 
505 		break;
506 	case CHIPC_CAP_FLASH_NONE:
507 		caps->flash_type = CHIPC_FLASH_NONE;
508 		break;
509 
510 	}
511 
512 	/* Handle 4706_NFLASH fallback */
513 	if (CHIPC_QUIRK(sc, 4706_NFLASH) &&
514 	    CHIPC_GET_FLAG(cap_reg, CHIPC_CAP_4706_NFLASH))
515 	{
516 		caps->flash_type = CHIPC_NFLASH_4706;
517 	}
518 
519 	/* Determine NVRAM source. Must occur after the SPROM/OTP/flash
520 	 * capability flags have been populated. */
521 	caps->nvram_src = chipc_find_nvram_src(sc, caps);
522 
523 	/* Determine the SPROM offset within OTP (if any). SPROM-formatted
524 	 * data is placed within the OTP general use region. */
525 	caps->sprom_offset = 0;
526 	if (caps->nvram_src == BHND_NVRAM_SRC_OTP) {
527 		CHIPC_ASSERT_QUIRK(sc, OTP_IPX);
528 
529 		/* Bit offset to GUP HW subregion containing SPROM data */
530 		regval = bhnd_bus_read_4(sc->core, CHIPC_OTPLAYOUT);
531 		caps->sprom_offset = CHIPC_GET_BITS(regval, CHIPC_OTPL_GUP);
532 
533 		/* Convert to bytes */
534 		caps->sprom_offset /= 8;
535 	}
536 
537 	return (0);
538 }
539 
540 static int
541 chipc_suspend(device_t dev)
542 {
543 	return (bus_generic_suspend(dev));
544 }
545 
546 static int
547 chipc_resume(device_t dev)
548 {
549 	return (bus_generic_resume(dev));
550 }
551 
552 static void
553 chipc_probe_nomatch(device_t dev, device_t child)
554 {
555 	struct resource_list	*rl;
556 	const char		*name;
557 
558 	name = device_get_name(child);
559 	if (name == NULL)
560 		name = "unknown device";
561 
562 	device_printf(dev, "<%s> at", name);
563 
564 	rl = BUS_GET_RESOURCE_LIST(dev, child);
565 	if (rl != NULL) {
566 		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
567 		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
568 	}
569 
570 	printf(" (no driver attached)\n");
571 }
572 
573 static int
574 chipc_print_child(device_t dev, device_t child)
575 {
576 	struct resource_list	*rl;
577 	int			 retval = 0;
578 
579 	retval += bus_print_child_header(dev, child);
580 
581 	rl = BUS_GET_RESOURCE_LIST(dev, child);
582 	if (rl != NULL) {
583 		retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
584 		    "%#jx");
585 		retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
586 		    "%jd");
587 	}
588 
589 	retval += bus_print_child_domain(dev, child);
590 	retval += bus_print_child_footer(dev, child);
591 
592 	return (retval);
593 }
594 
595 static device_t
596 chipc_add_child(device_t dev, u_int order, const char *name, int unit)
597 {
598 	struct chipc_devinfo	*dinfo;
599 	device_t		 child;
600 
601 	child = device_add_child_ordered(dev, order, name, unit);
602 	if (child == NULL)
603 		return (NULL);
604 
605 	dinfo = malloc(sizeof(struct chipc_devinfo), M_BHND, M_NOWAIT);
606 	if (dinfo == NULL) {
607 		device_delete_child(dev, child);
608 		return (NULL);
609 	}
610 
611 	resource_list_init(&dinfo->resources);
612 	dinfo->irq_mapped = false;
613 	device_set_ivars(child, dinfo);
614 
615 	return (child);
616 }
617 
618 static void
619 chipc_child_deleted(device_t dev, device_t child)
620 {
621 	struct chipc_devinfo *dinfo = device_get_ivars(child);
622 
623 	if (dinfo != NULL) {
624 		/* Free the child's resource list */
625 		resource_list_free(&dinfo->resources);
626 
627 		/* Unmap the child's IRQ */
628 		if (dinfo->irq_mapped) {
629 			bhnd_unmap_intr(dev, dinfo->irq);
630 			dinfo->irq_mapped = false;
631 		}
632 
633 		free(dinfo, M_BHND);
634 	}
635 
636 	device_set_ivars(child, NULL);
637 }
638 
639 static struct resource_list *
640 chipc_get_resource_list(device_t dev, device_t child)
641 {
642 	struct chipc_devinfo *dinfo = device_get_ivars(child);
643 	return (&dinfo->resources);
644 }
645 
646 /* Allocate region records for the given port, and add the port's memory
647  * range to the mem_rman */
648 static int
649 chipc_rman_init_regions (struct chipc_softc *sc, bhnd_port_type type,
650     u_int port)
651 {
652 	struct	chipc_region	*cr;
653 	rman_res_t		 start, end;
654 	u_int			 num_regions;
655 	int			 error;
656 
657 	num_regions = bhnd_get_region_count(sc->dev, type, port);
658 	for (u_int region = 0; region < num_regions; region++) {
659 		/* Allocate new region record */
660 		cr = chipc_alloc_region(sc, type, port, region);
661 		if (cr == NULL)
662 			return (ENODEV);
663 
664 		/* Can't manage regions that cannot be allocated */
665 		if (cr->cr_rid < 0) {
666 			BHND_DEBUG_DEV(sc->dev, "no rid for chipc region "
667 			    "%s%u.%u", bhnd_port_type_name(type), port, region);
668 			chipc_free_region(sc, cr);
669 			continue;
670 		}
671 
672 		/* Add to rman's managed range */
673 		start = cr->cr_addr;
674 		end = cr->cr_end;
675 		if ((error = rman_manage_region(&sc->mem_rman, start, end))) {
676 			chipc_free_region(sc, cr);
677 			return (error);
678 		}
679 
680 		/* Add to region list */
681 		STAILQ_INSERT_TAIL(&sc->mem_regions, cr, cr_link);
682 	}
683 
684 	return (0);
685 }
686 
687 /* Initialize memory state for all chipc port regions */
688 static int
689 chipc_init_rman(struct chipc_softc *sc)
690 {
691 	u_int	num_ports;
692 	int	error;
693 
694 	/* Port types for which we'll register chipc_region mappings */
695 	bhnd_port_type types[] = {
696 	    BHND_PORT_DEVICE
697 	};
698 
699 	/* Initialize resource manager */
700 	sc->mem_rman.rm_start = 0;
701 	sc->mem_rman.rm_end = BUS_SPACE_MAXADDR;
702 	sc->mem_rman.rm_type = RMAN_ARRAY;
703 	sc->mem_rman.rm_descr = "ChipCommon Device Memory";
704 	if ((error = rman_init(&sc->mem_rman))) {
705 		device_printf(sc->dev, "could not initialize mem_rman: %d\n",
706 		    error);
707 		return (error);
708 	}
709 
710 	/* Populate per-port-region state */
711 	for (u_int i = 0; i < nitems(types); i++) {
712 		num_ports = bhnd_get_port_count(sc->dev, types[i]);
713 		for (u_int port = 0; port < num_ports; port++) {
714 			error = chipc_rman_init_regions(sc, types[i], port);
715 			if (error) {
716 				device_printf(sc->dev,
717 				    "region init failed for %s%u: %d\n",
718 				     bhnd_port_type_name(types[i]), port,
719 				     error);
720 
721 				goto failed;
722 			}
723 		}
724 	}
725 
726 	return (0);
727 
728 failed:
729 	chipc_free_rman(sc);
730 	return (error);
731 }
732 
733 /* Free memory management state */
734 static void
735 chipc_free_rman(struct chipc_softc *sc)
736 {
737 	struct chipc_region *cr, *cr_next;
738 
739 	STAILQ_FOREACH_SAFE(cr, &sc->mem_regions, cr_link, cr_next)
740 		chipc_free_region(sc, cr);
741 
742 	rman_fini(&sc->mem_rman);
743 }
744 
745 /**
746  * Return the rman instance for a given resource @p type, if any.
747  *
748  * @param sc The chipc device state.
749  * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
750  */
751 static struct rman *
752 chipc_get_rman(struct chipc_softc *sc, int type)
753 {
754 	switch (type) {
755 	case SYS_RES_MEMORY:
756 		return (&sc->mem_rman);
757 
758 	case SYS_RES_IRQ:
759 		/* We delegate IRQ resource management to the parent bus */
760 		return (NULL);
761 
762 	default:
763 		return (NULL);
764 	};
765 }
766 
767 static struct resource *
768 chipc_alloc_resource(device_t dev, device_t child, int type,
769     int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
770 {
771 	struct chipc_softc		*sc;
772 	struct chipc_region		*cr;
773 	struct resource_list_entry	*rle;
774 	struct resource			*rv;
775 	struct rman			*rm;
776 	int				 error;
777 	bool				 passthrough, isdefault;
778 
779 	sc = device_get_softc(dev);
780 	passthrough = (device_get_parent(child) != dev);
781 	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
782 	rle = NULL;
783 
784 	/* Fetch the resource manager, delegate request if necessary */
785 	rm = chipc_get_rman(sc, type);
786 	if (rm == NULL) {
787 		/* Requested resource type is delegated to our parent */
788 		rv = bus_generic_rl_alloc_resource(dev, child, type, rid,
789 		    start, end, count, flags);
790 		return (rv);
791 	}
792 
793 	/* Populate defaults */
794 	if (!passthrough && isdefault) {
795 		/* Fetch the resource list entry. */
796 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
797 		    type, *rid);
798 		if (rle == NULL) {
799 			device_printf(dev,
800 			    "default resource %#x type %d for child %s "
801 			    "not found\n", *rid, type,
802 			    device_get_nameunit(child));
803 			return (NULL);
804 		}
805 
806 		if (rle->res != NULL) {
807 			device_printf(dev,
808 			    "resource entry %#x type %d for child %s is busy "
809 			    "[%d]\n",
810 			    *rid, type, device_get_nameunit(child),
811 			    rman_get_flags(rle->res));
812 
813 			return (NULL);
814 		}
815 
816 		start = rle->start;
817 		end = rle->end;
818 		count = ulmax(count, rle->count);
819 	}
820 
821 	/* Locate a mapping region */
822 	if ((cr = chipc_find_region(sc, start, end)) == NULL) {
823 		/* Resource requests outside our shared port regions can be
824 		 * delegated to our parent. */
825 		rv = bus_generic_rl_alloc_resource(dev, child, type, rid,
826 		    start, end, count, flags);
827 		return (rv);
828 	}
829 
830 	/*
831 	 * As a special case, children that map the complete ChipCommon register
832 	 * block are delegated to our parent.
833 	 *
834 	 * The rman API does not support sharing resources that are not
835 	 * identical in size; since we allocate subregions to various children,
836 	 * any children that need to map the entire register block (e.g. because
837 	 * they require access to discontiguous register ranges) must make the
838 	 * allocation through our parent, where we hold a compatible
839 	 * RF_SHAREABLE allocation.
840 	 */
841 	if (cr == sc->core_region && cr->cr_addr == start &&
842 	    cr->cr_end == end && cr->cr_count == count)
843 	{
844 		rv = bus_generic_rl_alloc_resource(dev, child, type, rid,
845 		    start, end, count, flags);
846 		return (rv);
847 	}
848 
849 	/* Try to retain a region reference */
850 	if ((error = chipc_retain_region(sc, cr, RF_ALLOCATED)))
851 		return (NULL);
852 
853 	/* Make our rman reservation */
854 	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
855 	    child);
856 	if (rv == NULL) {
857 		chipc_release_region(sc, cr, RF_ALLOCATED);
858 		return (NULL);
859 	}
860 
861 	rman_set_rid(rv, *rid);
862 
863 	/* Activate */
864 	if (flags & RF_ACTIVE) {
865 		error = bus_activate_resource(child, type, *rid, rv);
866 		if (error) {
867 			device_printf(dev,
868 			    "failed to activate entry %#x type %d for "
869 				"child %s: %d\n",
870 			     *rid, type, device_get_nameunit(child), error);
871 
872 			chipc_release_region(sc, cr, RF_ALLOCATED);
873 			rman_release_resource(rv);
874 
875 			return (NULL);
876 		}
877 	}
878 
879 	/* Update child's resource list entry */
880 	if (rle != NULL) {
881 		rle->res = rv;
882 		rle->start = rman_get_start(rv);
883 		rle->end = rman_get_end(rv);
884 		rle->count = rman_get_size(rv);
885 	}
886 
887 	return (rv);
888 }
889 
890 static int
891 chipc_release_resource(device_t dev, device_t child, int type, int rid,
892     struct resource *r)
893 {
894 	struct chipc_softc		*sc;
895 	struct chipc_region		*cr;
896 	struct rman			*rm;
897 	struct resource_list_entry	*rle;
898 	int			 	 error;
899 
900 	sc = device_get_softc(dev);
901 
902 	/* Handled by parent bus? */
903 	rm = chipc_get_rman(sc, type);
904 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
905 		return (bus_generic_rl_release_resource(dev, child, type, rid,
906 		    r));
907 	}
908 
909 	/* Locate the mapping region */
910 	cr = chipc_find_region(sc, rman_get_start(r), rman_get_end(r));
911 	if (cr == NULL)
912 		return (EINVAL);
913 
914 	/* Deactivate resources */
915 	if (rman_get_flags(r) & RF_ACTIVE) {
916 		error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r);
917 		if (error)
918 			return (error);
919 	}
920 
921 	if ((error = rman_release_resource(r)))
922 		return (error);
923 
924 	/* Drop allocation reference */
925 	chipc_release_region(sc, cr, RF_ALLOCATED);
926 
927 	/* Clear reference from the resource list entry if exists */
928 	rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child), type, rid);
929 	if (rle != NULL)
930 		rle->res = NULL;
931 
932 	return (0);
933 }
934 
935 static int
936 chipc_adjust_resource(device_t dev, device_t child, int type,
937     struct resource *r, rman_res_t start, rman_res_t end)
938 {
939 	struct chipc_softc		*sc;
940 	struct chipc_region		*cr;
941 	struct rman			*rm;
942 
943 	sc = device_get_softc(dev);
944 
945 	/* Handled by parent bus? */
946 	rm = chipc_get_rman(sc, type);
947 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
948 		return (bus_generic_adjust_resource(dev, child, type, r, start,
949 		    end));
950 	}
951 
952 	/* The range is limited to the existing region mapping */
953 	cr = chipc_find_region(sc, rman_get_start(r), rman_get_end(r));
954 	if (cr == NULL)
955 		return (EINVAL);
956 
957 	if (end <= start)
958 		return (EINVAL);
959 
960 	if (start < cr->cr_addr || end > cr->cr_end)
961 		return (EINVAL);
962 
963 	/* Range falls within the existing region */
964 	return (rman_adjust_resource(r, start, end));
965 }
966 
967 /**
968  * Retain an RF_ACTIVE reference to the region mapping @p r, and
969  * configure @p r with its subregion values.
970  *
971  * @param sc Driver instance state.
972  * @param child Requesting child device.
973  * @param type resource type of @p r.
974  * @param rid resource id of @p r
975  * @param r resource to be activated.
976  * @param req_direct If true, failure to allocate a direct bhnd resource
977  * will be treated as an error. If false, the resource will not be marked
978  * as RF_ACTIVE if bhnd direct resource allocation fails.
979  */
980 static int
981 chipc_try_activate_resource(struct chipc_softc *sc, device_t child, int type,
982     int rid, struct resource *r, bool req_direct)
983 {
984 	struct rman		*rm;
985 	struct chipc_region	*cr;
986 	bhnd_size_t		 cr_offset;
987 	rman_res_t		 r_start, r_end, r_size;
988 	int			 error;
989 
990 	rm = chipc_get_rman(sc, type);
991 	if (rm == NULL || !rman_is_region_manager(r, rm))
992 		return (EINVAL);
993 
994 	r_start = rman_get_start(r);
995 	r_end = rman_get_end(r);
996 	r_size = rman_get_size(r);
997 
998 	/* Find the corresponding chipc region */
999 	cr = chipc_find_region(sc, r_start, r_end);
1000 	if (cr == NULL)
1001 		return (EINVAL);
1002 
1003 	/* Calculate subregion offset within the chipc region */
1004 	cr_offset = r_start - cr->cr_addr;
1005 
1006 	/* Retain (and activate, if necessary) the chipc region */
1007 	if ((error = chipc_retain_region(sc, cr, RF_ACTIVE)))
1008 		return (error);
1009 
1010 	/* Configure child resource with its subregion values. */
1011 	if (cr->cr_res->direct) {
1012 		error = chipc_init_child_resource(r, cr->cr_res->res,
1013 		    cr_offset, r_size);
1014 		if (error)
1015 			goto cleanup;
1016 
1017 		/* Mark active */
1018 		if ((error = rman_activate_resource(r)))
1019 			goto cleanup;
1020 	} else if (req_direct) {
1021 		error = ENOMEM;
1022 		goto cleanup;
1023 	}
1024 
1025 	return (0);
1026 
1027 cleanup:
1028 	chipc_release_region(sc, cr, RF_ACTIVE);
1029 	return (error);
1030 }
1031 
1032 static int
1033 chipc_activate_bhnd_resource(device_t dev, device_t child, int type,
1034     int rid, struct bhnd_resource *r)
1035 {
1036 	struct chipc_softc	*sc;
1037 	struct rman		*rm;
1038 	int			 error;
1039 
1040 	sc = device_get_softc(dev);
1041 
1042 	/* Delegate non-locally managed resources to parent */
1043 	rm = chipc_get_rman(sc, type);
1044 	if (rm == NULL || !rman_is_region_manager(r->res, rm)) {
1045 		return (bhnd_bus_generic_activate_resource(dev, child, type,
1046 		    rid, r));
1047 	}
1048 
1049 	/* Try activating the chipc region resource */
1050 	error = chipc_try_activate_resource(sc, child, type, rid, r->res,
1051 	    false);
1052 	if (error)
1053 		return (error);
1054 
1055 	/* Mark the child resource as direct according to the returned resource
1056 	 * state */
1057 	if (rman_get_flags(r->res) & RF_ACTIVE)
1058 		r->direct = true;
1059 
1060 	return (0);
1061 }
1062 
1063 static int
1064 chipc_activate_resource(device_t dev, device_t child, int type, int rid,
1065     struct resource *r)
1066 {
1067 	struct chipc_softc	*sc;
1068 	struct rman		*rm;
1069 
1070 	sc = device_get_softc(dev);
1071 
1072 	/* Delegate non-locally managed resources to parent */
1073 	rm = chipc_get_rman(sc, type);
1074 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
1075 		return (bus_generic_activate_resource(dev, child, type, rid,
1076 		    r));
1077 	}
1078 
1079 	/* Try activating the chipc region-based resource */
1080 	return (chipc_try_activate_resource(sc, child, type, rid, r, true));
1081 }
1082 
1083 /**
1084  * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE().
1085  */
1086 static int
1087 chipc_deactivate_resource(device_t dev, device_t child, int type,
1088     int rid, struct resource *r)
1089 {
1090 	struct chipc_softc	*sc;
1091 	struct chipc_region	*cr;
1092 	struct rman		*rm;
1093 	int			 error;
1094 
1095 	sc = device_get_softc(dev);
1096 
1097 	/* Handled by parent bus? */
1098 	rm = chipc_get_rman(sc, type);
1099 	if (rm == NULL || !rman_is_region_manager(r, rm)) {
1100 		return (bus_generic_deactivate_resource(dev, child, type, rid,
1101 		    r));
1102 	}
1103 
1104 	/* Find the corresponding chipc region */
1105 	cr = chipc_find_region(sc, rman_get_start(r), rman_get_end(r));
1106 	if (cr == NULL)
1107 		return (EINVAL);
1108 
1109 	/* Mark inactive */
1110 	if ((error = rman_deactivate_resource(r)))
1111 		return (error);
1112 
1113 	/* Drop associated RF_ACTIVE reference */
1114 	chipc_release_region(sc, cr, RF_ACTIVE);
1115 
1116 	return (0);
1117 }
1118 
1119 /**
1120  * Examine bus state and make a best effort determination of whether it's
1121  * likely safe to enable the muxed SPROM pins.
1122  *
1123  * On devices that do not use SPROM pin muxing, always returns true.
1124  *
1125  * @param sc chipc driver state.
1126  */
1127 static bool
1128 chipc_should_enable_muxed_sprom(struct chipc_softc *sc)
1129 {
1130 	device_t	*devs;
1131 	device_t	 hostb;
1132 	device_t	 parent;
1133 	int		 devcount;
1134 	int		 error;
1135 	bool		 result;
1136 
1137 	/* Nothing to do? */
1138 	if (!CHIPC_QUIRK(sc, MUX_SPROM))
1139 		return (true);
1140 
1141 	bus_topo_lock();
1142 
1143 	parent = device_get_parent(sc->dev);
1144 	hostb = bhnd_bus_find_hostb_device(parent);
1145 
1146 	if ((error = device_get_children(parent, &devs, &devcount))) {
1147 		bus_topo_unlock();
1148 		return (false);
1149 	}
1150 
1151 	/* Reject any active devices other than ChipCommon, or the
1152 	 * host bridge (if any). */
1153 	result = true;
1154 	for (int i = 0; i < devcount; i++) {
1155 		if (devs[i] == hostb || devs[i] == sc->dev)
1156 			continue;
1157 
1158 		if (!device_is_attached(devs[i]))
1159 			continue;
1160 
1161 		if (device_is_suspended(devs[i]))
1162 			continue;
1163 
1164 		/* Active device; assume SPROM is busy */
1165 		result = false;
1166 		break;
1167 	}
1168 
1169 	free(devs, M_TEMP);
1170 	bus_topo_unlock();
1171 	return (result);
1172 }
1173 
1174 static int
1175 chipc_enable_sprom(device_t dev)
1176 {
1177 	struct chipc_softc	*sc;
1178 	int			 error;
1179 
1180 	sc = device_get_softc(dev);
1181 	CHIPC_LOCK(sc);
1182 
1183 	/* Already enabled? */
1184 	if (sc->sprom_refcnt >= 1) {
1185 		sc->sprom_refcnt++;
1186 		CHIPC_UNLOCK(sc);
1187 
1188 		return (0);
1189 	}
1190 
1191 	switch (sc->caps.nvram_src) {
1192 	case BHND_NVRAM_SRC_SPROM:
1193 		error = chipc_enable_sprom_pins(sc);
1194 		break;
1195 	case BHND_NVRAM_SRC_OTP:
1196 		error = chipc_enable_otp_power(sc);
1197 		break;
1198 	default:
1199 		error = 0;
1200 		break;
1201 	}
1202 
1203 	/* Bump the reference count */
1204 	if (error == 0)
1205 		sc->sprom_refcnt++;
1206 
1207 	CHIPC_UNLOCK(sc);
1208 	return (error);
1209 }
1210 
1211 static void
1212 chipc_disable_sprom(device_t dev)
1213 {
1214 	struct chipc_softc	*sc;
1215 
1216 	sc = device_get_softc(dev);
1217 	CHIPC_LOCK(sc);
1218 
1219 	/* Check reference count, skip disable if in-use. */
1220 	KASSERT(sc->sprom_refcnt > 0, ("sprom refcnt overrelease"));
1221 	sc->sprom_refcnt--;
1222 	if (sc->sprom_refcnt > 0) {
1223 		CHIPC_UNLOCK(sc);
1224 		return;
1225 	}
1226 
1227 	switch (sc->caps.nvram_src) {
1228 	case BHND_NVRAM_SRC_SPROM:
1229 		chipc_disable_sprom_pins(sc);
1230 		break;
1231 	case BHND_NVRAM_SRC_OTP:
1232 		chipc_disable_otp_power(sc);
1233 		break;
1234 	default:
1235 		break;
1236 	}
1237 
1238 	CHIPC_UNLOCK(sc);
1239 }
1240 
1241 static int
1242 chipc_enable_otp_power(struct chipc_softc *sc)
1243 {
1244 	// TODO: Enable OTP resource via PMU, and wait up to 100 usec for
1245 	// OTPS_READY to be set in `optstatus`.
1246 	return (0);
1247 }
1248 
1249 static void
1250 chipc_disable_otp_power(struct chipc_softc *sc)
1251 {
1252 	// TODO: Disable OTP resource via PMU
1253 }
1254 
1255 /**
1256  * If required by this device, enable access to the SPROM.
1257  *
1258  * @param sc chipc driver state.
1259  */
1260 static int
1261 chipc_enable_sprom_pins(struct chipc_softc *sc)
1262 {
1263 	uint32_t		 cctrl;
1264 
1265 	CHIPC_LOCK_ASSERT(sc, MA_OWNED);
1266 	KASSERT(sc->sprom_refcnt == 0, ("sprom pins already enabled"));
1267 
1268 	/* Nothing to do? */
1269 	if (!CHIPC_QUIRK(sc, MUX_SPROM))
1270 		return (0);
1271 
1272 	/* Check whether bus is busy */
1273 	if (!chipc_should_enable_muxed_sprom(sc))
1274 		return (EBUSY);
1275 
1276 	cctrl = bhnd_bus_read_4(sc->core, CHIPC_CHIPCTRL);
1277 
1278 	/* 4331 devices */
1279 	if (CHIPC_QUIRK(sc, 4331_EXTPA_MUX_SPROM)) {
1280 		cctrl &= ~CHIPC_CCTRL4331_EXTPA_EN;
1281 
1282 		if (CHIPC_QUIRK(sc, 4331_GPIO2_5_MUX_SPROM))
1283 			cctrl &= ~CHIPC_CCTRL4331_EXTPA_ON_GPIO2_5;
1284 
1285 		if (CHIPC_QUIRK(sc, 4331_EXTPA2_MUX_SPROM))
1286 			cctrl &= ~CHIPC_CCTRL4331_EXTPA_EN2;
1287 
1288 		bhnd_bus_write_4(sc->core, CHIPC_CHIPCTRL, cctrl);
1289 		return (0);
1290 	}
1291 
1292 	/* 4360 devices */
1293 	if (CHIPC_QUIRK(sc, 4360_FEM_MUX_SPROM)) {
1294 		/* Unimplemented */
1295 	}
1296 
1297 	/* Refuse to proceed on unsupported devices with muxed SPROM pins */
1298 	device_printf(sc->dev, "muxed sprom lines on unrecognized device\n");
1299 	return (ENXIO);
1300 }
1301 
1302 /**
1303  * If required by this device, revert any GPIO/pin configuration applied
1304  * to allow SPROM access.
1305  *
1306  * @param sc chipc driver state.
1307  */
1308 static void
1309 chipc_disable_sprom_pins(struct chipc_softc *sc)
1310 {
1311 	uint32_t		 cctrl;
1312 
1313 	/* Nothing to do? */
1314 	if (!CHIPC_QUIRK(sc, MUX_SPROM))
1315 		return;
1316 
1317 	CHIPC_LOCK_ASSERT(sc, MA_OWNED);
1318 	KASSERT(sc->sprom_refcnt == 0, ("sprom pins in use"));
1319 
1320 	cctrl = bhnd_bus_read_4(sc->core, CHIPC_CHIPCTRL);
1321 
1322 	/* 4331 devices */
1323 	if (CHIPC_QUIRK(sc, 4331_EXTPA_MUX_SPROM)) {
1324 		cctrl |= CHIPC_CCTRL4331_EXTPA_EN;
1325 
1326 		if (CHIPC_QUIRK(sc, 4331_GPIO2_5_MUX_SPROM))
1327 			cctrl |= CHIPC_CCTRL4331_EXTPA_ON_GPIO2_5;
1328 
1329 		if (CHIPC_QUIRK(sc, 4331_EXTPA2_MUX_SPROM))
1330 			cctrl |= CHIPC_CCTRL4331_EXTPA_EN2;
1331 
1332 		bhnd_bus_write_4(sc->core, CHIPC_CHIPCTRL, cctrl);
1333 		return;
1334 	}
1335 
1336 	/* 4360 devices */
1337 	if (CHIPC_QUIRK(sc, 4360_FEM_MUX_SPROM)) {
1338 		/* Unimplemented */
1339 	}
1340 }
1341 
1342 static uint32_t
1343 chipc_read_chipst(device_t dev)
1344 {
1345 	struct chipc_softc *sc = device_get_softc(dev);
1346 	return (bhnd_bus_read_4(sc->core, CHIPC_CHIPST));
1347 }
1348 
1349 static void
1350 chipc_write_chipctrl(device_t dev, uint32_t value, uint32_t mask)
1351 {
1352 	struct chipc_softc	*sc;
1353 	uint32_t		 cctrl;
1354 
1355 	sc = device_get_softc(dev);
1356 
1357 	CHIPC_LOCK(sc);
1358 
1359 	cctrl = bhnd_bus_read_4(sc->core, CHIPC_CHIPCTRL);
1360 	cctrl = (cctrl & ~mask) | (value | mask);
1361 	bhnd_bus_write_4(sc->core, CHIPC_CHIPCTRL, cctrl);
1362 
1363 	CHIPC_UNLOCK(sc);
1364 }
1365 
1366 static struct chipc_caps *
1367 chipc_get_caps(device_t dev)
1368 {
1369 	struct chipc_softc	*sc;
1370 
1371 	sc = device_get_softc(dev);
1372 	return (&sc->caps);
1373 }
1374 
1375 static device_method_t chipc_methods[] = {
1376 	/* Device interface */
1377 	DEVMETHOD(device_probe,			chipc_probe),
1378 	DEVMETHOD(device_attach,		chipc_attach),
1379 	DEVMETHOD(device_detach,		chipc_detach),
1380 	DEVMETHOD(device_suspend,		chipc_suspend),
1381 	DEVMETHOD(device_resume,		chipc_resume),
1382 
1383 	/* Bus interface */
1384 	DEVMETHOD(bus_probe_nomatch,		chipc_probe_nomatch),
1385 	DEVMETHOD(bus_print_child,		chipc_print_child),
1386 
1387 	DEVMETHOD(bus_add_child,		chipc_add_child),
1388 	DEVMETHOD(bus_child_deleted,		chipc_child_deleted),
1389 
1390 	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
1391 	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
1392 	DEVMETHOD(bus_delete_resource,		bus_generic_rl_delete_resource),
1393 	DEVMETHOD(bus_alloc_resource,		chipc_alloc_resource),
1394 	DEVMETHOD(bus_release_resource,		chipc_release_resource),
1395 	DEVMETHOD(bus_adjust_resource,		chipc_adjust_resource),
1396 	DEVMETHOD(bus_activate_resource,	chipc_activate_resource),
1397 	DEVMETHOD(bus_deactivate_resource,	chipc_deactivate_resource),
1398 	DEVMETHOD(bus_get_resource_list,	chipc_get_resource_list),
1399 
1400 	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
1401 	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),
1402 	DEVMETHOD(bus_config_intr,		bus_generic_config_intr),
1403 	DEVMETHOD(bus_bind_intr,		bus_generic_bind_intr),
1404 	DEVMETHOD(bus_describe_intr,		bus_generic_describe_intr),
1405 
1406 	/* BHND bus inteface */
1407 	DEVMETHOD(bhnd_bus_activate_resource,	chipc_activate_bhnd_resource),
1408 
1409 	/* ChipCommon interface */
1410 	DEVMETHOD(bhnd_chipc_read_chipst,	chipc_read_chipst),
1411 	DEVMETHOD(bhnd_chipc_write_chipctrl,	chipc_write_chipctrl),
1412 	DEVMETHOD(bhnd_chipc_enable_sprom,	chipc_enable_sprom),
1413 	DEVMETHOD(bhnd_chipc_disable_sprom,	chipc_disable_sprom),
1414 	DEVMETHOD(bhnd_chipc_get_caps,		chipc_get_caps),
1415 
1416 	DEVMETHOD_END
1417 };
1418 
1419 DEFINE_CLASS_0(bhnd_chipc, bhnd_chipc_driver, chipc_methods, sizeof(struct chipc_softc));
1420 EARLY_DRIVER_MODULE(bhnd_chipc, bhnd, bhnd_chipc_driver, 0, 0,
1421     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
1422 MODULE_DEPEND(bhnd_chipc, bhnd, 1, 1, 1);
1423 MODULE_VERSION(bhnd_chipc, 1);
1424