lbc.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2006-2008, Juniper Networks, Inc.
5 * Copyright (c) 2008 Semihalf, Rafal Czubak
6 * Copyright (c) 2009 The FreeBSD Foundation
7 * All rights reserved.
8 *
9 * Portions of this software were developed by Semihalf
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 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "opt_platform.h"
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: stable/11/sys/powerpc/mpc85xx/lbc.c 330897 2018-03-14 03:19:51Z eadler $");
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/ktr.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/bus.h>
48#include <sys/rman.h>
49#include <machine/bus.h>
50
51#include <vm/vm.h>
52#include <vm/pmap.h>
53
54#include <dev/fdt/fdt_common.h>
55#include <dev/ofw/ofw_bus.h>
56#include <dev/ofw/ofw_bus_subr.h>
57
58#include <powerpc/mpc85xx/mpc85xx.h>
59
60#include "ofw_bus_if.h"
61#include "lbc.h"
62
63#ifdef DEBUG
64#define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
65    printf(fmt,##args); } while (0)
66#else
67#define debugf(fmt, args...)
68#endif
69
70static MALLOC_DEFINE(M_LBC, "localbus", "localbus devices information");
71
72static int lbc_probe(device_t);
73static int lbc_attach(device_t);
74static int lbc_shutdown(device_t);
75static int lbc_activate_resource(device_t bus __unused, device_t child __unused,
76    int type, int rid __unused, struct resource *r);
77static int lbc_deactivate_resource(device_t bus __unused,
78    device_t child __unused, int type __unused, int rid __unused,
79    struct resource *r);
80static struct resource *lbc_alloc_resource(device_t, device_t, int, int *,
81    rman_res_t, rman_res_t, rman_res_t, u_int);
82static int lbc_print_child(device_t, device_t);
83static int lbc_release_resource(device_t, device_t, int, int,
84    struct resource *);
85static const struct ofw_bus_devinfo *lbc_get_devinfo(device_t, device_t);
86
87/*
88 * Bus interface definition
89 */
90static device_method_t lbc_methods[] = {
91	/* Device interface */
92	DEVMETHOD(device_probe,		lbc_probe),
93	DEVMETHOD(device_attach,	lbc_attach),
94	DEVMETHOD(device_shutdown,	lbc_shutdown),
95
96	/* Bus interface */
97	DEVMETHOD(bus_print_child,	lbc_print_child),
98	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
99	DEVMETHOD(bus_teardown_intr,	NULL),
100
101	DEVMETHOD(bus_alloc_resource,	lbc_alloc_resource),
102	DEVMETHOD(bus_release_resource,	lbc_release_resource),
103	DEVMETHOD(bus_activate_resource, lbc_activate_resource),
104	DEVMETHOD(bus_deactivate_resource, lbc_deactivate_resource),
105
106	/* OFW bus interface */
107	DEVMETHOD(ofw_bus_get_devinfo,	lbc_get_devinfo),
108	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
109	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
110	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
111	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
112	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
113
114	{ 0, 0 }
115};
116
117static driver_t lbc_driver = {
118	"lbc",
119	lbc_methods,
120	sizeof(struct lbc_softc)
121};
122
123devclass_t lbc_devclass;
124
125EARLY_DRIVER_MODULE(lbc, ofwbus, lbc_driver, lbc_devclass,
126    0, 0, BUS_PASS_BUS);
127
128/*
129 * Calculate address mask used by OR(n) registers. Use memory region size to
130 * determine mask value. The size must be a power of two and within the range
131 * of 32KB - 4GB. Otherwise error code is returned. Value representing
132 * 4GB size can be passed as 0xffffffff.
133 */
134static uint32_t
135lbc_address_mask(uint32_t size)
136{
137	int n = 15;
138
139	if (size == ~0)
140		return (0);
141
142	while (n < 32) {
143		if (size == (1U << n))
144			break;
145		n++;
146	}
147
148	if (n == 32)
149		return (EINVAL);
150
151	return (0xffff8000 << (n - 15));
152}
153
154static void
155lbc_banks_unmap(struct lbc_softc *sc)
156{
157	int r;
158
159	r = 0;
160	while (r < LBC_DEV_MAX) {
161		if (sc->sc_range[r].size == 0)
162			return;
163
164		pmap_unmapdev(sc->sc_range[r].kva, sc->sc_range[r].size);
165		law_disable(OCP85XX_TGTIF_LBC, sc->sc_range[r].addr,
166		    sc->sc_range[r].size);
167		r++;
168	}
169}
170
171static int
172lbc_banks_map(struct lbc_softc *sc)
173{
174	vm_paddr_t end, start;
175	vm_size_t size;
176	u_int i, r, ranges, s;
177	int error;
178
179	bzero(sc->sc_range, sizeof(sc->sc_range));
180
181	/*
182	 * Determine number of discontiguous address ranges to program.
183	 */
184	ranges = 0;
185	for (i = 0; i < LBC_DEV_MAX; i++) {
186		size = sc->sc_banks[i].size;
187		if (size == 0)
188			continue;
189
190		start = sc->sc_banks[i].addr;
191		for (r = 0; r < ranges; r++) {
192			/* Avoid wrap-around bugs. */
193			end = sc->sc_range[r].addr - 1 + sc->sc_range[r].size;
194			if (start > 0 && end == start - 1) {
195				sc->sc_range[r].size += size;
196				break;
197			}
198			/* Avoid wrap-around bugs. */
199			end = start - 1 + size;
200			if (sc->sc_range[r].addr > 0 &&
201			    end == sc->sc_range[r].addr - 1) {
202				sc->sc_range[r].addr = start;
203				sc->sc_range[r].size += size;
204				break;
205			}
206		}
207		if (r == ranges) {
208			/* New range; add using insertion sort */
209			r = 0;
210			while (r < ranges && sc->sc_range[r].addr < start)
211				r++;
212			for (s = ranges; s > r; s--)
213				sc->sc_range[s] = sc->sc_range[s-1];
214			sc->sc_range[r].addr = start;
215			sc->sc_range[r].size = size;
216			ranges++;
217		}
218	}
219
220	/*
221	 * Ranges are sorted so quickly go over the list to merge ranges
222	 * that grew toward each other while building the ranges.
223	 */
224	r = 0;
225	while (r < ranges - 1) {
226		end = sc->sc_range[r].addr + sc->sc_range[r].size;
227		if (end != sc->sc_range[r+1].addr) {
228			r++;
229			continue;
230		}
231		sc->sc_range[r].size += sc->sc_range[r+1].size;
232		for (s = r + 1; s < ranges - 1; s++)
233			sc->sc_range[s] = sc->sc_range[s+1];
234		bzero(&sc->sc_range[s], sizeof(sc->sc_range[s]));
235		ranges--;
236	}
237
238	/*
239	 * Configure LAW for the LBC ranges and map the physical memory
240	 * range into KVA.
241	 */
242	for (r = 0; r < ranges; r++) {
243		start = sc->sc_range[r].addr;
244		size = sc->sc_range[r].size;
245		error = law_enable(OCP85XX_TGTIF_LBC, start, size);
246		if (error)
247			return (error);
248		sc->sc_range[r].kva = (vm_offset_t)pmap_mapdev(start, size);
249	}
250
251	/* XXX: need something better here? */
252	if (ranges == 0)
253		return (EINVAL);
254
255	/* Assign KVA to banks based on the enclosing range. */
256	for (i = 0; i < LBC_DEV_MAX; i++) {
257		size = sc->sc_banks[i].size;
258		if (size == 0)
259			continue;
260
261		start = sc->sc_banks[i].addr;
262		for (r = 0; r < ranges; r++) {
263			end = sc->sc_range[r].addr - 1 + sc->sc_range[r].size;
264			if (start >= sc->sc_range[r].addr &&
265			    start - 1 + size <= end)
266				break;
267		}
268		if (r < ranges) {
269			sc->sc_banks[i].kva = sc->sc_range[r].kva +
270			    (start - sc->sc_range[r].addr);
271		}
272	}
273
274	return (0);
275}
276
277static int
278lbc_banks_enable(struct lbc_softc *sc)
279{
280	uint32_t size;
281	uint32_t regval;
282	int error, i;
283
284	for (i = 0; i < LBC_DEV_MAX; i++) {
285		size = sc->sc_banks[i].size;
286		if (size == 0)
287			continue;
288
289		/*
290		 * Compute and program BR value.
291		 */
292		regval = sc->sc_banks[i].addr;
293		switch (sc->sc_banks[i].width) {
294		case 8:
295			regval |= (1 << 11);
296			break;
297		case 16:
298			regval |= (2 << 11);
299			break;
300		case 32:
301			regval |= (3 << 11);
302			break;
303		default:
304			error = EINVAL;
305			goto fail;
306		}
307		regval |= (sc->sc_banks[i].decc << 9);
308		regval |= (sc->sc_banks[i].wp << 8);
309		regval |= (sc->sc_banks[i].msel << 5);
310		regval |= (sc->sc_banks[i].atom << 2);
311		regval |= 1;
312		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
313		    LBC85XX_BR(i), regval);
314
315		/*
316		 * Compute and program OR value.
317		 */
318		regval = lbc_address_mask(size);
319		switch (sc->sc_banks[i].msel) {
320		case LBCRES_MSEL_GPCM:
321			/* TODO Add flag support for option registers */
322			regval |= 0x0ff7;
323			break;
324		case LBCRES_MSEL_FCM:
325			/* TODO Add flag support for options register */
326			regval |= 0x0796;
327			break;
328		case LBCRES_MSEL_UPMA:
329		case LBCRES_MSEL_UPMB:
330		case LBCRES_MSEL_UPMC:
331			printf("UPM mode not supported yet!");
332			error = ENOSYS;
333			goto fail;
334		}
335		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
336		    LBC85XX_OR(i), regval);
337	}
338
339	return (0);
340
341fail:
342	lbc_banks_unmap(sc);
343	return (error);
344}
345
346static void
347fdt_lbc_fixup(phandle_t node, struct lbc_softc *sc, struct lbc_devinfo *di)
348{
349	pcell_t width;
350	int bank;
351
352	if (OF_getprop(node, "bank-width", (void *)&width, sizeof(width)) <= 0)
353		return;
354
355	bank = di->di_bank;
356	if (sc->sc_banks[bank].size == 0)
357		return;
358
359	/* Express width in bits. */
360	sc->sc_banks[bank].width = width * 8;
361}
362
363static int
364fdt_lbc_reg_decode(phandle_t node, struct lbc_softc *sc,
365    struct lbc_devinfo *di)
366{
367	rman_res_t start, end, count;
368	pcell_t *reg, *regptr;
369	pcell_t addr_cells, size_cells;
370	int tuple_size, tuples;
371	int i, j, rv, bank;
372
373	if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
374		return (ENXIO);
375
376	tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
377	tuples = OF_getencprop_alloc(node, "reg", tuple_size, (void **)&reg);
378	debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
379	debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
380	if (tuples <= 0)
381		/* No 'reg' property in this node. */
382		return (0);
383
384	regptr = reg;
385	for (i = 0; i < tuples; i++) {
386
387		bank = fdt_data_get((void *)reg, 1);
388		di->di_bank = bank;
389		reg += 1;
390
391		/* Get address/size. */
392		start = count = 0;
393		for (j = 0; j < addr_cells; j++) {
394			start <<= 32;
395			start |= reg[j];
396		}
397		for (j = 0; j < size_cells; j++) {
398			count <<= 32;
399			count |= reg[addr_cells + j - 1];
400		}
401		reg += addr_cells - 1 + size_cells;
402
403		/* Calculate address range relative to VA base. */
404		start = sc->sc_banks[bank].kva + start;
405		end = start + count - 1;
406
407		debugf("reg addr bank = %d, start = %jx, end = %jx, "
408		    "count = %jx\n", bank, start, end, count);
409
410		/* Use bank (CS) cell as rid. */
411		resource_list_add(&di->di_res, SYS_RES_MEMORY, bank, start,
412		    end, count);
413	}
414	rv = 0;
415	OF_prop_free(regptr);
416	return (rv);
417}
418
419static void
420lbc_intr(void *arg)
421{
422	struct lbc_softc *sc = arg;
423	uint32_t ltesr;
424
425	ltesr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LTESR);
426	sc->sc_ltesr = ltesr;
427	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LTESR, ltesr);
428	wakeup(sc->sc_dev);
429}
430
431static int
432lbc_probe(device_t dev)
433{
434
435	if (!(ofw_bus_is_compatible(dev, "fsl,lbc") ||
436	    ofw_bus_is_compatible(dev, "fsl,elbc")))
437		return (ENXIO);
438
439	device_set_desc(dev, "Freescale Local Bus Controller");
440	return (BUS_PROBE_DEFAULT);
441}
442
443static int
444lbc_attach(device_t dev)
445{
446	struct lbc_softc *sc;
447	struct lbc_devinfo *di;
448	struct rman *rm;
449	uintmax_t offset, size;
450	vm_paddr_t start;
451	device_t cdev;
452	phandle_t node, child;
453	pcell_t *ranges, *rangesptr;
454	int tuple_size, tuples;
455	int par_addr_cells;
456	int bank, error, i, j;
457
458	sc = device_get_softc(dev);
459	sc->sc_dev = dev;
460
461	sc->sc_mrid = 0;
462	sc->sc_mres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_mrid,
463	    RF_ACTIVE);
464	if (sc->sc_mres == NULL)
465		return (ENXIO);
466
467	sc->sc_bst = rman_get_bustag(sc->sc_mres);
468	sc->sc_bsh = rman_get_bushandle(sc->sc_mres);
469
470	for (bank = 0; bank < LBC_DEV_MAX; bank++) {
471		bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_BR(bank), 0);
472		bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_OR(bank), 0);
473	}
474
475	/*
476	 * Initialize configuration register:
477	 * - enable Local Bus
478	 * - set data buffer control signal function
479	 * - disable parity byte select
480	 * - set ECC parity type
481	 * - set bus monitor timing and timer prescale
482	 */
483	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LBCR, 0);
484
485	/*
486	 * Initialize clock ratio register:
487	 * - disable PLL bypass mode
488	 * - configure LCLK delay cycles for the assertion of LALE
489	 * - set system clock divider
490	 */
491	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LCRR, 0x00030008);
492
493	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LTEDR, 0);
494	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LTESR, ~0);
495	bus_space_write_4(sc->sc_bst, sc->sc_bsh, LBC85XX_LTEIR, 0x64080001);
496
497	sc->sc_irid = 0;
498	sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
499	    RF_ACTIVE | RF_SHAREABLE);
500	if (sc->sc_ires != NULL) {
501		error = bus_setup_intr(dev, sc->sc_ires,
502		    INTR_TYPE_MISC | INTR_MPSAFE, NULL, lbc_intr, sc,
503		    &sc->sc_icookie);
504		if (error) {
505			device_printf(dev, "could not activate interrupt\n");
506			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
507			    sc->sc_ires);
508			sc->sc_ires = NULL;
509		}
510	}
511
512	sc->sc_ltesr = ~0;
513
514	rangesptr = NULL;
515
516	rm = &sc->sc_rman;
517	rm->rm_type = RMAN_ARRAY;
518	rm->rm_descr = "Local Bus Space";
519	error = rman_init(rm);
520	if (error)
521		goto fail;
522
523	error = rman_manage_region(rm, rm->rm_start, rm->rm_end);
524	if (error) {
525		rman_fini(rm);
526		goto fail;
527	}
528
529	/*
530	 * Process 'ranges' property.
531	 */
532	node = ofw_bus_get_node(dev);
533	if ((fdt_addrsize_cells(node, &sc->sc_addr_cells,
534	    &sc->sc_size_cells)) != 0) {
535		error = ENXIO;
536		goto fail;
537	}
538
539	par_addr_cells = fdt_parent_addr_cells(node);
540	if (par_addr_cells > 2) {
541		device_printf(dev, "unsupported parent #addr-cells\n");
542		error = ERANGE;
543		goto fail;
544	}
545	tuple_size = sizeof(pcell_t) * (sc->sc_addr_cells + par_addr_cells +
546	    sc->sc_size_cells);
547
548	tuples = OF_getencprop_alloc(node, "ranges", tuple_size,
549	    (void **)&ranges);
550	if (tuples < 0) {
551		device_printf(dev, "could not retrieve 'ranges' property\n");
552		error = ENXIO;
553		goto fail;
554	}
555	rangesptr = ranges;
556
557	debugf("par addr_cells = %d, addr_cells = %d, size_cells = %d, "
558	    "tuple_size = %d, tuples = %d\n", par_addr_cells,
559	    sc->sc_addr_cells, sc->sc_size_cells, tuple_size, tuples);
560
561	start = 0;
562	size = 0;
563	for (i = 0; i < tuples; i++) {
564
565		/* The first cell is the bank (chip select) number. */
566		bank = fdt_data_get(ranges, 1);
567		if (bank < 0 || bank > LBC_DEV_MAX) {
568			device_printf(dev, "bank out of range: %d\n", bank);
569			error = ERANGE;
570			goto fail;
571		}
572		ranges += 1;
573
574		/*
575		 * Remaining cells of the child address define offset into
576		 * this CS.
577		 */
578		offset = 0;
579		for (j = 0; j < sc->sc_addr_cells - 1; j++) {
580			offset <<= sizeof(pcell_t) * 8;
581			offset |= *ranges;
582			ranges++;
583		}
584
585		/* Parent bus start address of this bank. */
586		start = 0;
587		for (j = 0; j < par_addr_cells; j++) {
588			start <<= sizeof(pcell_t) * 8;
589			start |= *ranges;
590			ranges++;
591		}
592
593		size = fdt_data_get((void *)ranges, sc->sc_size_cells);
594		ranges += sc->sc_size_cells;
595		debugf("bank = %d, start = %jx, size = %jx\n", bank,
596		    (uintmax_t)start, size);
597
598		sc->sc_banks[bank].addr = start + offset;
599		sc->sc_banks[bank].size = size;
600
601		/*
602		 * Attributes for the bank.
603		 *
604		 * XXX Note there are no DT bindings defined for them at the
605		 * moment, so we need to provide some defaults.
606		 */
607		sc->sc_banks[bank].width = 16;
608		sc->sc_banks[bank].msel = LBCRES_MSEL_GPCM;
609		sc->sc_banks[bank].decc = LBCRES_DECC_DISABLED;
610		sc->sc_banks[bank].atom = LBCRES_ATOM_DISABLED;
611		sc->sc_banks[bank].wp = 0;
612	}
613
614	/*
615	 * Initialize mem-mappings for the LBC banks (i.e. chip selects).
616	 */
617	error = lbc_banks_map(sc);
618	if (error)
619		goto fail;
620
621	/*
622	 * Walk the localbus and add direct subordinates as our children.
623	 */
624	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
625
626		di = malloc(sizeof(*di), M_LBC, M_WAITOK | M_ZERO);
627
628		if (ofw_bus_gen_setup_devinfo(&di->di_ofw, child) != 0) {
629			free(di, M_LBC);
630			device_printf(dev, "could not set up devinfo\n");
631			continue;
632		}
633
634		resource_list_init(&di->di_res);
635
636		if (fdt_lbc_reg_decode(child, sc, di)) {
637			device_printf(dev, "could not process 'reg' "
638			    "property\n");
639			ofw_bus_gen_destroy_devinfo(&di->di_ofw);
640			free(di, M_LBC);
641			continue;
642		}
643
644		fdt_lbc_fixup(child, sc, di);
645
646		/* Add newbus device for this FDT node */
647		cdev = device_add_child(dev, NULL, -1);
648		if (cdev == NULL) {
649			device_printf(dev, "could not add child: %s\n",
650			    di->di_ofw.obd_name);
651			resource_list_free(&di->di_res);
652			ofw_bus_gen_destroy_devinfo(&di->di_ofw);
653			free(di, M_LBC);
654			continue;
655		}
656		debugf("added child name='%s', node=%p\n", di->di_ofw.obd_name,
657		    (void *)child);
658		device_set_ivars(cdev, di);
659	}
660
661	/*
662	 * Enable the LBC.
663	 */
664	lbc_banks_enable(sc);
665
666	OF_prop_free(rangesptr);
667	return (bus_generic_attach(dev));
668
669fail:
670	OF_prop_free(rangesptr);
671	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mrid, sc->sc_mres);
672	return (error);
673}
674
675static int
676lbc_shutdown(device_t dev)
677{
678
679	/* TODO */
680	return(0);
681}
682
683static struct resource *
684lbc_alloc_resource(device_t bus, device_t child, int type, int *rid,
685    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
686{
687	struct lbc_softc *sc;
688	struct lbc_devinfo *di;
689	struct resource_list_entry *rle;
690	struct resource *res;
691	struct rman *rm;
692	int needactivate;
693
694	/* We only support default allocations. */
695	if (!RMAN_IS_DEFAULT_RANGE(start, end))
696		return (NULL);
697
698	sc = device_get_softc(bus);
699	if (type == SYS_RES_IRQ)
700		return (bus_alloc_resource(bus, type, rid, start, end, count,
701		    flags));
702
703	/*
704	 * Request for the default allocation with a given rid: use resource
705	 * list stored in the local device info.
706	 */
707	if ((di = device_get_ivars(child)) == NULL)
708		return (NULL);
709
710	if (type == SYS_RES_IOPORT)
711		type = SYS_RES_MEMORY;
712
713	rid = &di->di_bank;
714
715	rle = resource_list_find(&di->di_res, type, *rid);
716	if (rle == NULL) {
717		device_printf(bus, "no default resources for "
718		    "rid = %d, type = %d\n", *rid, type);
719		return (NULL);
720	}
721	start = rle->start;
722	count = rle->count;
723	end = start + count - 1;
724
725	sc = device_get_softc(bus);
726
727	needactivate = flags & RF_ACTIVE;
728	flags &= ~RF_ACTIVE;
729
730	rm = &sc->sc_rman;
731
732	res = rman_reserve_resource(rm, start, end, count, flags, child);
733	if (res == NULL) {
734		device_printf(bus, "failed to reserve resource %#jx - %#jx "
735		    "(%#jx)\n", start, end, count);
736		return (NULL);
737	}
738
739	rman_set_rid(res, *rid);
740	rman_set_bustag(res, &bs_be_tag);
741	rman_set_bushandle(res, rman_get_start(res));
742
743	if (needactivate)
744		if (bus_activate_resource(child, type, *rid, res)) {
745			device_printf(child, "resource activation failed\n");
746			rman_release_resource(res);
747			return (NULL);
748		}
749
750	return (res);
751}
752
753static int
754lbc_print_child(device_t dev, device_t child)
755{
756	struct lbc_devinfo *di;
757	struct resource_list *rl;
758	int rv;
759
760	di = device_get_ivars(child);
761	rl = &di->di_res;
762
763	rv = 0;
764	rv += bus_print_child_header(dev, child);
765	rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
766	rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
767	rv += bus_print_child_footer(dev, child);
768
769	return (rv);
770}
771
772static int
773lbc_release_resource(device_t dev, device_t child, int type, int rid,
774    struct resource *res)
775{
776	int err;
777
778	if (rman_get_flags(res) & RF_ACTIVE) {
779		err = bus_deactivate_resource(child, type, rid, res);
780		if (err)
781			return (err);
782	}
783
784	return (rman_release_resource(res));
785}
786
787static int
788lbc_activate_resource(device_t bus __unused, device_t child __unused,
789    int type __unused, int rid __unused, struct resource *r)
790{
791
792	/* Child resources were already mapped, just activate. */
793	return (rman_activate_resource(r));
794}
795
796static int
797lbc_deactivate_resource(device_t bus __unused, device_t child __unused,
798    int type __unused, int rid __unused, struct resource *r)
799{
800
801	return (rman_deactivate_resource(r));
802}
803
804static const struct ofw_bus_devinfo *
805lbc_get_devinfo(device_t bus, device_t child)
806{
807	struct lbc_devinfo *di;
808
809	di = device_get_ivars(child);
810	return (&di->di_ofw);
811}
812
813void
814lbc_write_reg(device_t child, u_int off, uint32_t val)
815{
816	device_t dev;
817	struct lbc_softc *sc;
818
819	dev = device_get_parent(child);
820
821	if (off >= 0x1000) {
822		device_printf(dev, "%s(%s): invalid offset %#x\n",
823		    __func__, device_get_nameunit(child), off);
824		return;
825	}
826
827	sc = device_get_softc(dev);
828
829	if (off == LBC85XX_LTESR && sc->sc_ltesr != ~0u) {
830		sc->sc_ltesr ^= (val & sc->sc_ltesr);
831		return;
832	}
833
834	if (off == LBC85XX_LTEATR && (val & 1) == 0)
835		sc->sc_ltesr = ~0u;
836	bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, val);
837}
838
839uint32_t
840lbc_read_reg(device_t child, u_int off)
841{
842	device_t dev;
843	struct lbc_softc *sc;
844	uint32_t val;
845
846	dev = device_get_parent(child);
847
848	if (off >= 0x1000) {
849		device_printf(dev, "%s(%s): invalid offset %#x\n",
850		    __func__, device_get_nameunit(child), off);
851		return (~0U);
852	}
853
854	sc = device_get_softc(dev);
855
856	if (off == LBC85XX_LTESR && sc->sc_ltesr != ~0U)
857		val = sc->sc_ltesr;
858	else
859		val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
860	return (val);
861}
862