Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/fdt Add a framework for enumerating devices using a ...



details:   https://anonhg.NetBSD.org/src/rev/3b17abc4857c
branches:  trunk
changeset: 342236:3b17abc4857c
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun Dec 13 17:30:40 2015 +0000

description:
Add a framework for enumerating devices using a Flattened Device Tree (FDT).

diffstat:

 sys/dev/fdt/fdt_openfirm.c   |  359 ++++++++++++++++++++++++++++
 sys/dev/fdt/fdt_openfirm.h   |   37 ++
 sys/dev/fdt/fdt_subr.c       |  547 +++++++++++++++++++++++++++++++++++++++++++
 sys/dev/fdt/fdtbus.c         |  161 ++++++++++++
 sys/dev/fdt/fdtvar.h         |  115 +++++++++
 sys/dev/fdt/files.fdt        |   22 +
 sys/dev/fdt/fixedregulator.c |  151 +++++++++++
 sys/dev/fdt/simplebus.c      |   86 ++++++
 8 files changed, 1478 insertions(+), 0 deletions(-)

diffs (truncated from 1510 to 300 lines):

diff -r ac2c3e47c484 -r 3b17abc4857c sys/dev/fdt/fdt_openfirm.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/fdt/fdt_openfirm.c        Sun Dec 13 17:30:40 2015 +0000
@@ -0,0 +1,359 @@
+/* $NetBSD: fdt_openfirm.c,v 1.1 2015/12/13 17:30:40 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: fdt_openfirm.c,v 1.1 2015/12/13 17:30:40 jmcneill Exp $");
+
+#include <sys/param.h>
+
+#include <libfdt.h>
+#include <dev/ofw/openfirm.h>
+#include <dev/fdt/fdt_openfirm.h>
+
+static const void *fdt_data;
+
+bool
+fdt_openfirm_set_data(const void *data)
+{
+       KASSERT(fdt_data == NULL);
+       if (fdt_check_header(data) != 0) {
+               return false;
+       }
+       fdt_data = data;
+       return true;
+}
+
+const void *
+fdt_openfirm_get_data(void)
+{
+       return fdt_data;
+}
+
+int
+fdt_openfirm_get_phandle(int offset)
+{
+       if (offset < 0)
+               return 0;
+
+       return offset + fdt_off_dt_struct(fdt_data);
+}
+
+int
+fdt_openfirm_get_offset(int phandle)
+{
+       const int dtoff = fdt_off_dt_struct(fdt_data);
+
+       if (phandle == -1)
+               phandle = dtoff;
+
+       if (phandle < dtoff)
+               return -1;
+
+       return phandle - dtoff;
+}
+
+int
+OF_peer(int phandle)
+{
+       int off, depth;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       if (phandle == 0) {
+               return fdt_openfirm_get_phandle(0);
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return 0;
+       }
+
+       depth = 1;
+       for (off = fdt_next_node(fdt_data, off, &depth);
+            off >= 0 && depth >= 0;
+            off = fdt_next_node(fdt_data, off, &depth)) {
+               if (depth == 1) {
+                       return fdt_openfirm_get_phandle(off);
+               }
+       }
+
+       return 0;
+}
+
+int
+OF_child(int phandle)
+{
+       int off, depth;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return 0;
+       }
+
+       depth = 0;
+       for (off = fdt_next_node(fdt_data, off, &depth);
+            off >= 0 && depth > 0;
+            off = fdt_next_node(fdt_data, off, &depth)) {
+               if (depth == 1) {
+                       return fdt_openfirm_get_phandle(off);
+               }
+       }
+
+       return 0;
+}
+
+int
+OF_parent(int phandle)
+{
+       int off;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return -1;
+       }
+
+       off = fdt_parent_offset(fdt_data, off);
+       if (off < 0) {
+               return -1;
+       }
+
+       return fdt_openfirm_get_phandle(off);
+}
+
+int
+OF_nextprop(int phandle, const char *prop, void *nextprop)
+{
+       const char *name;
+       const void *val;
+       int off, len;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return -1;
+       }
+
+       if (*prop == '\0') {
+               name = "name";
+       } else {
+               off = fdt_first_property_offset(fdt_data, off);
+               if (off < 0) {
+                       return 0;
+               }
+               if (strcmp(prop, "name") != 0) {
+                       while (off >= 0) {
+                               val = fdt_getprop_by_offset(fdt_data, off,
+                                   &name, &len);
+                               if (val == NULL) {
+                                       return -1;
+                               }
+                               off = fdt_next_property_offset(fdt_data, off);
+                               if (off < 0) {
+                                       return 0;
+                               }
+                               if (strcmp(name, prop) == 0)
+                                       break;
+                       }
+               }
+               val = fdt_getprop_by_offset(fdt_data, off, &name, &len);
+               if (val == NULL) {
+                       return -1;
+               }
+       }
+
+       strlcpy(nextprop, name, 33);
+
+       return 1;
+}
+
+int
+OF_getprop(int phandle, const char *prop, void *buf, int buflen)
+{
+       const char *name;
+       const void *val;
+       int off, len;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return -1;
+       }
+
+       if (strcmp(prop, "name") == 0) {
+               val = fdt_get_name(fdt_data, off, &len);
+               if (val) {
+                       const char *p = strchr(val, '@');
+                       if (p) {
+                               len = (uintptr_t)p - (uintptr_t)val + 1;
+                       } else {
+                               len += 1;
+                       }
+               }
+               if (val == NULL || len > buflen) {
+                       return -1;
+               }
+               char *s = buf;
+               memcpy(buf, val, len - 1);
+               s[len - 1] = '\0';
+       } else {
+               off = fdt_first_property_offset(fdt_data, off);
+               if (off < 0) {
+                       return -1;
+               }
+               while (off >= 0) {
+                       val = fdt_getprop_by_offset(fdt_data, off, &name, &len);
+                       if (val == NULL) {
+                               return -1;
+                       }
+                       if (strcmp(name, prop) == 0) {
+                               break;
+                       }
+                       off = fdt_next_property_offset(fdt_data, off);
+                       if (off < 0) {
+                               return -1;
+                       }
+               }
+               if (val == NULL || len > buflen) {
+                       return -1;
+               }
+               memcpy(buf, val, len);
+       }
+
+       return len;
+}
+
+int
+OF_getproplen(int phandle, const char *prop)
+{
+       const char *name;
+       const void *val;
+       int off, len;
+
+       if (fdt_data == NULL) {
+               return -1;
+       }
+
+       off = fdt_openfirm_get_offset(phandle);
+       if (off < 0) {
+               return -1;
+       }
+
+       if (strcmp(prop, "name") == 0) {
+               val = fdt_get_name(fdt_data, off, &len);
+               if (val) {
+                       const char *p = strchr(val, '@');
+                       if (p) {
+                               len = (uintptr_t)p - (uintptr_t)val + 1;
+                       } else {
+                               len += 1;
+                       }
+               }
+       } else {
+               off = fdt_first_property_offset(fdt_data, off);
+               if (off < 0) {
+                       return -1;
+               }



Home | Main Index | Thread Index | Old Index