From 7ca2ca3a42a8698513e166b3f0c3b3babf22a886 Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 26 Oct 2014 03:41:27 +0000 Subject: [PATCH] MFC 271546: Create an interface and support routines for drivers that handle IO pin multiplexing and configuration based on FDT data. git-svn-id: svn://svn.freebsd.org/base/stable/10@273669 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/conf/files | 6 +- sys/dev/fdt/fdt_pinctrl.c | 151 +++++++++++++++++++++++++++++++++++ sys/dev/fdt/fdt_pinctrl.h | 58 ++++++++++++++ sys/dev/fdt/fdt_pinctrl_if.m | 47 +++++++++++ 4 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 sys/dev/fdt/fdt_pinctrl.c create mode 100644 sys/dev/fdt/fdt_pinctrl.h create mode 100644 sys/dev/fdt/fdt_pinctrl_if.m diff --git a/sys/conf/files b/sys/conf/files index ad882bdf7..cd33a0694 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1371,9 +1371,11 @@ dev/fatm/if_fatm.c optional fatm pci dev/fb/fbd.c optional fbd | vt dev/fb/fb_if.m standard dev/fb/splash.c optional sc splash -dev/fdt/fdt_clock.c optional fdt_clock -dev/fdt/fdt_clock_if.m optional fdt_clock +dev/fdt/fdt_clock.c optional fdt fdt_clock +dev/fdt/fdt_clock_if.m optional fdt fdt_clock dev/fdt/fdt_common.c optional fdt +dev/fdt/fdt_pinctrl.c optional fdt fdt_pinctrl +dev/fdt/fdt_pinctrl_if.m optional fdt fdt_pinctrl dev/fdt/fdt_slicer.c optional fdt cfi | fdt nand dev/fdt/fdt_static_dtb.S optional fdt fdt_dtb_static \ dependency "$S/boot/fdt/dts/${MACHINE}/${FDT_DTS_FILE}" diff --git a/sys/dev/fdt/fdt_pinctrl.c b/sys/dev/fdt/fdt_pinctrl.c new file mode 100644 index 000000000..474fb00e2 --- /dev/null +++ b/sys/dev/fdt/fdt_pinctrl.c @@ -0,0 +1,151 @@ +/*- + * Copyright (c) 2014 Ian Lepore + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include + +#include +#include + +#include "fdt_pinctrl_if.h" + +#include +#include + +int +fdt_pinctrl_configure(device_t client, u_int index) +{ + device_t pinctrl; + phandle_t *configs; + int i, nconfigs; + char name[16]; + + snprintf(name, sizeof(name), "pinctrl-%u", index); + nconfigs = OF_getprop_alloc(ofw_bus_get_node(client), name, + sizeof(*configs), (void **)&configs); + if (nconfigs < 0) + return (ENOENT); + if (nconfigs == 0) + return (0); /* Empty property is documented as valid. */ + for (i = 0; i < nconfigs; i++) { + if ((pinctrl = OF_device_from_xref(configs[i])) != NULL) + FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]); + } + free(configs, M_OFWPROP); + return (0); +} + +int +fdt_pinctrl_configure_by_name(device_t client, const char * name) +{ + char * names; + int i, offset, nameslen; + + nameslen = OF_getprop_alloc(ofw_bus_get_node(client), "pinctrl-names", + sizeof(*names), (void **)&names); + if (nameslen <= 0) + return (ENOENT); + for (i = 0, offset = 0; offset < nameslen; i++) { + if (strcmp(name, &names[offset]) == 0) + break; + offset += strlen(&names[offset]) + 1; + } + free(names, M_OFWPROP); + if (offset < nameslen) + return (fdt_pinctrl_configure(client, i)); + else + return (ENOENT); +} + +static int +pinctrl_register_children(device_t pinctrl, phandle_t parent, + const char *pinprop) +{ + phandle_t node; + + /* + * Recursively descend from parent, looking for nodes that have the + * given property, and associate the pinctrl device_t with each one. + */ + for (node = OF_child(parent); node != 0; node = OF_peer(node)) { + pinctrl_register_children(pinctrl, node, pinprop); + if (pinprop == NULL || OF_hasprop(node, pinprop)) { + OF_device_register_xref(OF_xref_from_node(node), + pinctrl); + } + } + return (0); +} + +int +fdt_pinctrl_register(device_t pinctrl, const char *pinprop) +{ + phandle_t node; + + node = ofw_bus_get_node(pinctrl); + OF_device_register_xref(OF_xref_from_node(node), pinctrl); + return (pinctrl_register_children(pinctrl, node, pinprop)); +} + +static int +pinctrl_configure_children(device_t pinctrl, phandle_t parent) +{ + phandle_t node, *configs; + int i, nconfigs; + + for (node = OF_child(parent); node != 0; node = OF_peer(node)) { + if (!fdt_is_enabled(node)) + continue; + pinctrl_configure_children(pinctrl, node); + nconfigs = OF_getencprop_alloc(node, "pinctrl-0", + sizeof(*configs), (void **)&configs); +#ifdef DEBUG + { + char name[32]; + OF_getprop(node, "name", &name, sizeof(name)); + printf("%d items in pinctrl-0 for %s\n", nconfigs, name); + } +#endif + if (nconfigs <= 0) + continue; + for (i = 0; i < nconfigs; i++) { + if (OF_device_from_xref(configs[i]) == pinctrl) + FDT_PINCTRL_CONFIGURE(pinctrl, configs[i]); + } + free(configs, M_OFWPROP); + } + return (0); +} + +int +fdt_pinctrl_configure_tree(device_t pinctrl) +{ + + return (pinctrl_configure_children(pinctrl, OF_peer(0))); +} + diff --git a/sys/dev/fdt/fdt_pinctrl.h b/sys/dev/fdt/fdt_pinctrl.h new file mode 100644 index 000000000..6ac7d60a5 --- /dev/null +++ b/sys/dev/fdt/fdt_pinctrl.h @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2014 Ian Lepore + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef DEV_FDT_PINCTRL_H +#define DEV_FDT_PINCTRL_H + +#include "fdt_pinctrl_if.h" + +/* + * Configure pins by name or index. This looks up the pinctrl-N property in + * client's fdt data by index or name, and passes each handle in it to the + * pinctrl driver for configuration. + */ +int fdt_pinctrl_configure(device_t client, u_int index); +int fdt_pinctrl_configure_by_name(device_t client, const char * name); + +/* + * Register a pinctrl driver so that it can be used by other devices which call + * fdt_pinctrl_configure(). The pinprop argument is the name of a property that + * identifies each descendent of the pinctrl node which is a pin configuration + * node whose xref phandle can be passed to FDT_PINCTRL_CONFIGURE(). If this is + * NULL, every descendant node is registered. + */ +int fdt_pinctrl_register(device_t pinctrl, const char *pinprop); + +/* + * Walk the device tree and configure pins for each enabled device whose + * pinctrl-0 property contains references to nodes which are children of the + * given pinctrl device. This helper routine is for use by pinctrl drivers. + */ +int fdt_pinctrl_configure_tree(device_t pinctrl); + +#endif /* DEV_FDT_PINCTRL_H */ + diff --git a/sys/dev/fdt/fdt_pinctrl_if.m b/sys/dev/fdt/fdt_pinctrl_if.m new file mode 100644 index 000000000..c499cdf3c --- /dev/null +++ b/sys/dev/fdt/fdt_pinctrl_if.m @@ -0,0 +1,47 @@ +#- +# Copyright (c) 2014 Ian Lepore +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ +# + +#include +#include + +# +# This is the interface that fdt_pinctrl drivers provide to other drivers. +# + +INTERFACE fdt_pinctrl; + +# +# Set pins to the specified configuration. The cfgxref arg is an xref phandle +# to a descendent node (child, grandchild, ...) of the pinctrl device node. +# Returns 0 on success or a standard errno value. +# +METHOD int configure { + device_t pinctrl; + phandle_t cfgxref; +}; + -- 2.45.0