1298633Sbr/*-
2298633Sbr * Copyright (c) 2015 Ian Lepore <ian@freebsd.org>
3298633Sbr * All rights reserved.
4298633Sbr *
5298633Sbr * Redistribution and use in source and binary forms, with or without
6298633Sbr * modification, are permitted provided that the following conditions
7298633Sbr * are met:
8298633Sbr * 1. Redistributions of source code must retain the above copyright
9298633Sbr *    notice, this list of conditions and the following disclaimer.
10298633Sbr * 2. Redistributions in binary form must reproduce the above copyright
11298633Sbr *    notice, this list of conditions and the following disclaimer in the
12298633Sbr *    documentation and/or other materials provided with the distribution.
13298633Sbr *
14298633Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15298633Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16298633Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17298633Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18298633Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19298633Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20298633Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21298633Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22298633Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23298633Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24298633Sbr * SUCH DAMAGE.
25298633Sbr */
26298633Sbr
27298633Sbr#include <sys/cdefs.h>
28298633Sbr__FBSDID("$FreeBSD$");
29298633Sbr
30298633Sbr#include <sys/param.h>
31298633Sbr#include <sys/bus.h>
32298633Sbr
33298633Sbr#include <machine/bus.h>
34298633Sbr
35298633Sbr#include <dev/ofw/openfirm.h>
36298633Sbr#include <dev/ofw/ofw_subr.h>
37298633Sbr
38298633Sbrextern struct bus_space memmap_bus;
39298633Sbr
40298633Sbrint
41298633SbrOF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
42298633Sbr    bus_space_handle_t *handle, bus_size_t *sz)
43298633Sbr{
44298633Sbr	bus_addr_t addr;
45298633Sbr	bus_size_t size;
46298633Sbr	int err;
47298633Sbr
48298633Sbr	err = ofw_reg_to_paddr(dev, regno, &addr, &size, NULL);
49298633Sbr	if (err != 0)
50298633Sbr		return (err);
51298633Sbr
52298633Sbr	*tag = &memmap_bus;
53298633Sbr
54298633Sbr	if (sz != NULL)
55298633Sbr		*sz = size;
56298633Sbr
57298633Sbr	return (bus_space_map(*tag, addr, size, 0, handle));
58298633Sbr}
59