diff options
author | Mark Kettenis <kettenis@cvs.openbsd.org> | 2017-03-09 20:01:11 +0000 |
---|---|---|
committer | Mark Kettenis <kettenis@cvs.openbsd.org> | 2017-03-09 20:01:11 +0000 |
commit | 79b9d3e19f3320cc0ccbb98a7cfa2807d5bf56b4 (patch) | |
tree | d4471eb793d0c7f569380303cfb9ca23f64f9a2c /sys | |
parent | 90dab7a4e7409272278fc734ffe0fb4d00a4d5b2 (diff) |
Add a "regmap" interface that allows devices to provide access to their
registers to devices that live elsewhere in the device tree.
ok patrick@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/dev/ofw/ofw_misc.c | 81 | ||||
-rw-r--r-- | sys/dev/ofw/ofw_misc.h | 29 |
2 files changed, 110 insertions, 0 deletions
diff --git a/sys/dev/ofw/ofw_misc.c b/sys/dev/ofw/ofw_misc.c new file mode 100644 index 00000000000..e3de2a1719d --- /dev/null +++ b/sys/dev/ofw/ofw_misc.c @@ -0,0 +1,81 @@ +/* $OpenBSD: ofw_misc.c,v 1.1 2017/03/09 20:01:10 kettenis Exp $ */ +/* + * Copyright (c) 2017 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/systm.h> +#include <sys/malloc.h> + +#include <machine/bus.h> + +#include <dev/ofw/openfirm.h> +#include <dev/ofw/ofw_pinctrl.h> + +struct regmap { + uint32_t rm_phandle; + bus_space_tag_t rm_tag; + bus_space_handle_t rm_handle; + bus_size_t rm_size; + + LIST_ENTRY(regmap) rm_list; +}; + +LIST_HEAD(, regmap) regmaps = LIST_HEAD_INITIALIZER(regmap); + +void +regmap_register(int node, bus_space_tag_t tag, bus_space_handle_t handle, + bus_size_t size) +{ + struct regmap *rm; + uint32_t phandle; + + phandle = OF_getpropint(node, "phandle", 0); + if (phandle) { + rm = malloc(sizeof(struct regmap), M_DEVBUF, M_WAITOK); + rm->rm_phandle = phandle; + rm->rm_tag = tag; + rm->rm_handle = handle; + rm->rm_size = size; + LIST_INSERT_HEAD(®maps, rm, rm_list); + } +} + +struct regmap * +regmap_byphandle(uint32_t phandle) +{ + struct regmap *rm; + + LIST_FOREACH(rm, ®maps, rm_list) { + if (rm->rm_phandle == phandle) + return rm; + } + + return NULL; +} + +void +regmap_write_4(struct regmap *rm, bus_size_t offset, uint32_t value) +{ + KASSERT(offset <= rm->rm_size - sizeof(uint32_t)); + bus_space_write_4(rm->rm_tag, rm->rm_handle, offset, value); +} + +uint32_t +regmap_read_4(struct regmap *rm, bus_size_t offset) +{ + KASSERT(offset <= rm->rm_size - sizeof(uint32_t)); + return bus_space_read_4(rm->rm_tag, rm->rm_handle, offset); +} diff --git a/sys/dev/ofw/ofw_misc.h b/sys/dev/ofw/ofw_misc.h new file mode 100644 index 00000000000..37bd01105ec --- /dev/null +++ b/sys/dev/ofw/ofw_misc.h @@ -0,0 +1,29 @@ +/* $OpenBSD: ofw_misc.h,v 1.1 2017/03/09 20:01:10 kettenis Exp $ */ +/* + * Copyright (c) 2017 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _DEV_OFW_MISC_H_ +#define _DEV_OFW_MISC_H_ + +void regmap_register(int, bus_space_tag_t, bus_space_handle_t, bus_size_t); + +struct regmap; +struct regmap *regmap_byphandle(uint32_t); + +uint32_t regmap_read_4(struct regmap *, bus_size_t); +void regmap_write_4(struct regmap *, bus_size_t, uint32_t); + +#endif /* _DEV_OFW_MISC_H_ */ |