[PATCH] Implement FreeBSD helper structures

G S Niteesh Babu niteesh.gs at gmail.com
Tue Jan 19 16:20:27 UTC 2021


The following structures and functions have been implemented to
make porting of driver from FreeBSD easier.

 1) struct resource_spec
 2) struct device
 3) struct resource
 4) device_get_softc
 5) bus_alloc_resources
 6) bus_alloc_resource
 7) bus_alloc_resource_any
 8) bus_space_read_1
 9) bus_space_read_2
10) bus_space_read_4
11) bus_space_write_1
12) bus_space_write_2
13) bus_space_write_4
---
 bsps/include/rtems/freebsd-compat/bus.h      | 122 +++++++++++++++++++
 bsps/include/rtems/freebsd-compat/device.h   |  46 +++++++
 bsps/include/rtems/freebsd-compat/resource.h |  46 +++++++
 bsps/include/rtems/freebsd-compat/rman.h     |  65 ++++++++++
 bsps/shared/rtems/freebsd-compat/bus.c       | 119 ++++++++++++++++++
 spec/build/bsps/obj.yml                      |   1 +
 6 files changed, 399 insertions(+)
 create mode 100644 bsps/include/rtems/freebsd-compat/bus.h
 create mode 100644 bsps/include/rtems/freebsd-compat/device.h
 create mode 100644 bsps/include/rtems/freebsd-compat/resource.h
 create mode 100644 bsps/include/rtems/freebsd-compat/rman.h
 create mode 100644 bsps/shared/rtems/freebsd-compat/bus.c

diff --git a/bsps/include/rtems/freebsd-compat/bus.h b/bsps/include/rtems/freebsd-compat/bus.h
new file mode 100644
index 0000000000..4bd3ab311a
--- /dev/null
+++ b/bsps/include/rtems/freebsd-compat/bus.h
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup FreeBSDCompat
+ *
+ * @brief
+ */
+
+/*
+ * Copyright (C) <2020> Niteesh Babu <niteesh.gs at gmail.com>
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
+ */
+
+#ifndef _FREEBSD_COMPAT_BUS_H
+#define _FREEBSD_COMPAT_BUS_H
+
+#include <sys/types.h>
+#include "device.h"
+#include "rman.h"
+
+struct resource_spec {
+  int type;
+  int rid;
+  int flags;
+};
+#define RESOURCE_SPEC_END {-1, 0, 0}
+
+/*
+ * Write a 1, 2, 4, or 8 byte quantity to bus space
+ * described by tag/handle/offset.
+ */
+static inline void
+bus_space_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t ofs,
+    uint8_t val)
+{
+  uint8_t volatile *bsp = (uint8_t volatile *)(bsh + ofs);
+  *bsp = val;
+}
+
+static inline void
+bus_space_write_2(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t ofs,
+    uint16_t val)
+{
+  uint16_t volatile *bsp = (uint16_t volatile *)(bsh + ofs);
+  *bsp = val;
+}
+
+static inline void
+bus_space_write_4(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t ofs,
+    uint32_t val)
+{
+  uint32_t volatile *bsp = (uint32_t volatile *)(bsh + ofs);
+  *bsp = val;
+}
+
+/*
+ * Read a 1, 2, 4, or 8 byte quantity from bus space
+ * described by tag/handle/offset.
+ */
+static inline uint8_t
+bus_space_read_1(bus_space_tag_t tag, bus_space_handle_t handle,
+     bus_size_t offset)
+{
+
+  return (*(volatile uint8_t *)(handle + offset));
+}
+
+static inline uint16_t
+bus_space_read_2(bus_space_tag_t tag, bus_space_handle_t handle,
+     bus_size_t offset)
+{
+  return (*(volatile uint16_t *)(handle + offset));
+}
+
+static inline uint32_t
+bus_space_read_4(bus_space_tag_t tag, bus_space_handle_t handle,
+        bus_size_t offset)
+{
+  return (*(volatile uint32_t *)(handle + offset));
+}
+
+bus_space_handle_t rman_get_bushandle(struct resource *);
+bus_space_tag_t rman_get_bustag(struct resource *);
+
+void	*device_get_softc(device_t dev);
+
+int	bus_alloc_resources(device_t dev, struct resource_spec *rs,
+          struct resource **res);
+
+struct	resource *bus_alloc_resource(device_t dev, int type, int *rid,
+             rman_res_t start, rman_res_t end,
+             rman_res_t count, u_int flags);
+
+static __inline struct resource *
+bus_alloc_resource_any(device_t dev, int type, int *rid, uint32_t flags)
+{
+  return (bus_alloc_resource(dev, type, rid, 0, ~0, 1, flags));
+}
+
+#endif /* _FREEBSD_COMPAT_BUS_H_ */
diff --git a/bsps/include/rtems/freebsd-compat/device.h b/bsps/include/rtems/freebsd-compat/device.h
new file mode 100644
index 0000000000..c646bb5986
--- /dev/null
+++ b/bsps/include/rtems/freebsd-compat/device.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup FreeBSDCompat
+ *
+ * @brief
+ */
+
+/*
+ * Copyright (C) <2020> Niteesh Babu <niteesh.gs at gmail.com>
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
+ */
+
+#ifndef _FREEBSD_COMPAT_DEVICE_H
+#define _FREEBSD_COMPAT_DEVICE_H
+
+struct device {
+    void *softc;
+    unsigned int node;
+};
+
+typedef struct device *device_t;
+
+#endif /* _FREEBSD_COMPAT_DEVICE_H */
diff --git a/bsps/include/rtems/freebsd-compat/resource.h b/bsps/include/rtems/freebsd-compat/resource.h
new file mode 100644
index 0000000000..f84fd60e21
--- /dev/null
+++ b/bsps/include/rtems/freebsd-compat/resource.h
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup LIBFREEBSD
+ *
+ * @brief
+ */
+
+/*
+ * Copyright (C) <2020> Niteesh Babu <niteesh.gs at gmail.com>
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
+ */
+
+
+#ifndef _FREEBSD_COMPAT_RESOURCE_H
+#define _FREEBSD_COMPAT_RESOURCE_H
+
+#define SYS_RES_IRQ 1
+#define SYS_RES_DRQ 2
+#define SYS_RES_MEMORY 3
+#define SYS_RES_IOPORT 4
+#define SYS_RES_GPIO 5
+
+#endif /* _FREEBSD_COMPAT_RESOURCE_H */
diff --git a/bsps/include/rtems/freebsd-compat/rman.h b/bsps/include/rtems/freebsd-compat/rman.h
new file mode 100644
index 0000000000..cf359789e6
--- /dev/null
+++ b/bsps/include/rtems/freebsd-compat/rman.h
@@ -0,0 +1,65 @@
+
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup FreeBSDCompat
+ *
+ * @brief
+ */
+
+/*
+ * Copyright (C) <2020> Niteesh Babu <niteesh.gs at gmail.com>
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
+ */
+
+#ifndef _FREEBSD_COMPAT_RMAN_H
+#define _FREEBSD_COMPAT_RMAN_H
+
+#include <stdint.h>
+
+typedef uint32_t bus_size_t;
+
+/*
+ * Access methods for bus resources and address space.
+ */
+typedef int       bus_space_tag_t;
+typedef uintptr_t bus_space_handle_t;
+
+#define	RF_ALLOCATED    0x0001	/* resource has been reserved */
+#define	RF_ACTIVE       0x0002	/* resource allocation has been activated */
+#define	RF_SHAREABLE    0x0004	/* resource permits contemporaneous sharing */
+#define	RF_SPARE1       0x0008
+#define	RF_SPARE2       0x0010
+#define	RF_FIRSTSHARE   0x0020	/* first in sharing list */
+#define	RF_PREFETCHABLE 0x0040	/* resource is prefetchable */
+#define	RF_OPTIONAL     0x0080	/* for bus_alloc_resources() */
+#define	RF_UNMAPPED     0x0100	/* don't map resource when activating */
+
+struct resource {
+  bus_space_tag_t     r_bustag;
+  bus_space_handle_t  r_bushandle;/* bus_space handle */
+};
+
+#endif /* _FREEBSD_COMPAT_RMAN_H */
diff --git a/bsps/shared/rtems/freebsd-compat/bus.c b/bsps/shared/rtems/freebsd-compat/bus.c
new file mode 100644
index 0000000000..7ce91b80e6
--- /dev/null
+++ b/bsps/shared/rtems/freebsd-compat/bus.c
@@ -0,0 +1,119 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup LIBFREEBSD
+ *
+ * @brief
+ */
+
+/*
+ * Copyright (C) <2020> Niteesh Babu <niteesh.gs at gmail.com>
+ *
+ * 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 COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
+ */
+
+#include <rtems/bspIo.h>
+#include <libfdt.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <ofw/ofw.h>
+#include <rtems/freebsd-compat/bus.h>
+#include <rtems/freebsd-compat/resource.h>
+
+typedef unsigned int u_int;
+
+/**
+ * @brief Return the device's softc field
+ *
+ * The softc is allocated and zeroed when a driver is attached, based
+ * on the size field of the driver.
+ */
+void *
+device_get_softc(device_t dev)
+{
+
+  return (dev->softc);
+}
+
+bus_space_handle_t
+rman_get_bushandle(struct resource *r)
+{
+
+  return (r->r_bushandle);
+}
+
+bus_space_tag_t
+rman_get_bustag(struct resource *r)
+{
+
+  return (r->r_bustag);
+}
+
+int
+bus_alloc_resources(device_t dev, struct resource_spec *rs,
+    struct resource **res)
+{
+  int i;
+
+  for (i = 0; rs[i].type != -1; i++) {
+    res[i] = bus_alloc_resource_any(dev,
+    rs[i].type, &rs[i].rid, rs[i].flags);
+
+    if (res[i] == NULL) {
+      return (-1);
+    }
+  }
+  return (0);
+}
+
+struct resource *
+bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
+    rman_res_t end, rman_res_t count, u_int flags)
+{
+  int node;
+  int rv;
+  rtems_ofw_memory_area reg[*rid + 1];
+  struct resource *res;
+
+  node = dev->node;
+
+  /*
+   * We only support querying register values from FDT.
+   */
+  assert(type == SYS_RES_MEMORY);
+
+  res = (struct resource *)malloc(sizeof(struct resource));
+
+  if (res != NULL) {
+    rv = rtems_ofw_get_reg(node, &reg[0], sizeof(reg));
+    if (rv == -1) {
+      return NULL;
+    }
+    res->r_bushandle = reg[*rid].start;
+  }
+
+  return (res);
+}
diff --git a/spec/build/bsps/obj.yml b/spec/build/bsps/obj.yml
index 0ea07fc83d..7cf12cd27a 100644
--- a/spec/build/bsps/obj.yml
+++ b/spec/build/bsps/obj.yml
@@ -109,4 +109,5 @@ source:
 - bsps/shared/start/bootcard.c
 - bsps/shared/rtems-version.c
 - bsps/shared/ofw/ofw.c
+- bsps/shared/rtems/freebsd-compat/bus.c
 type: build
-- 
2.17.1



More information about the devel mailing list